Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
kcomedilib_main.c
Go to the documentation of this file.
1 /*
2  kcomedilib/kcomedilib.c
3  a comedlib interface for kernel modules
4 
5  COMEDI - Linux Control and Measurement Device Interface
6  Copyright (C) 1997-2000 David A. Schleef <[email protected]>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 
22 */
23 
24 #define __NO_VERSION__
25 #include <linux/module.h>
26 
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/fcntl.h>
31 #include <linux/delay.h>
32 #include <linux/ioport.h>
33 #include <linux/mm.h>
34 #include <linux/io.h>
35 
36 #include "../comedi.h"
37 #include "../comedilib.h"
38 #include "../comedidev.h"
39 
40 MODULE_AUTHOR("David Schleef <[email protected]>");
41 MODULE_DESCRIPTION("Comedi kernel library");
42 MODULE_LICENSE("GPL");
43 
44 struct comedi_device *comedi_open(const char *filename)
45 {
46  struct comedi_device_file_info *dev_file_info;
47  struct comedi_device *dev;
48  unsigned int minor;
49 
50  if (strncmp(filename, "/dev/comedi", 11) != 0)
51  return NULL;
52 
53  minor = simple_strtoul(filename + 11, NULL, 0);
54 
55  if (minor >= COMEDI_NUM_BOARD_MINORS)
56  return NULL;
57 
58  dev_file_info = comedi_get_device_file_info(minor);
59  if (dev_file_info == NULL)
60  return NULL;
61  dev = dev_file_info->device;
62 
63  if (dev == NULL || !dev->attached)
64  return NULL;
65 
66  if (!try_module_get(dev->driver->module))
67  return NULL;
68 
69  return dev;
70 }
71 EXPORT_SYMBOL(comedi_open);
72 
74 {
75  struct comedi_device *dev = (struct comedi_device *)d;
76 
77  module_put(dev->driver->module);
78 
79  return 0;
80 }
81 EXPORT_SYMBOL(comedi_close);
82 
83 static int comedi_do_insn(struct comedi_device *dev,
84  struct comedi_insn *insn,
85  unsigned int *data)
86 {
87  struct comedi_subdevice *s;
88  int ret = 0;
89 
90  /* a subdevice instruction */
91  if (insn->subdev >= dev->n_subdevices) {
92  ret = -EINVAL;
93  goto error;
94  }
95  s = &dev->subdevices[insn->subdev];
96 
97  if (s->type == COMEDI_SUBD_UNUSED) {
98  printk(KERN_ERR "%d not useable subdevice\n", insn->subdev);
99  ret = -EIO;
100  goto error;
101  }
102 
103  /* XXX check lock */
104 
105  ret = comedi_check_chanlist(s, 1, &insn->chanspec);
106  if (ret < 0) {
107  printk(KERN_ERR "bad chanspec\n");
108  ret = -EINVAL;
109  goto error;
110  }
111 
112  if (s->busy) {
113  ret = -EBUSY;
114  goto error;
115  }
116  s->busy = dev;
117 
118  switch (insn->insn) {
119  case INSN_BITS:
120  ret = s->insn_bits(dev, s, insn, data);
121  break;
122  case INSN_CONFIG:
123  /* XXX should check instruction length */
124  ret = s->insn_config(dev, s, insn, data);
125  break;
126  default:
127  ret = -EINVAL;
128  break;
129  }
130 
131  s->busy = NULL;
132 error:
133 
134  return ret;
135 }
136 
137 int comedi_dio_config(struct comedi_device *dev, unsigned int subdev,
138  unsigned int chan, unsigned int io)
139 {
140  struct comedi_insn insn;
141 
142  memset(&insn, 0, sizeof(insn));
143  insn.insn = INSN_CONFIG;
144  insn.n = 1;
145  insn.subdev = subdev;
146  insn.chanspec = CR_PACK(chan, 0, 0);
147 
148  return comedi_do_insn(dev, &insn, &io);
149 }
151 
152 int comedi_dio_bitfield(struct comedi_device *dev, unsigned int subdev,
153  unsigned int mask, unsigned int *bits)
154 {
155  struct comedi_insn insn;
156  unsigned int data[2];
157  int ret;
158 
159  memset(&insn, 0, sizeof(insn));
160  insn.insn = INSN_BITS;
161  insn.n = 2;
162  insn.subdev = subdev;
163 
164  data[0] = mask;
165  data[1] = *bits;
166 
167  ret = comedi_do_insn(dev, &insn, data);
168 
169  *bits = data[1];
170 
171  return ret;
172 }
174 
176  unsigned int subd)
177 {
178  struct comedi_subdevice *s;
179 
180  if (subd > dev->n_subdevices)
181  return -ENODEV;
182 
183  for (; subd < dev->n_subdevices; subd++) {
184  s = &dev->subdevices[subd];
185  if (s->type == type)
186  return subd;
187  }
188  return -1;
189 }
191 
192 int comedi_get_n_channels(struct comedi_device *dev, unsigned int subdevice)
193 {
194  struct comedi_subdevice *s = &dev->subdevices[subdevice];
195 
196  return s->n_chan;
197 }