Merge git://git.kvack.org/~bcrl/aio-next
[linux-2.6-block.git] / drivers / staging / comedi / kcomedilib / kcomedilib_main.c
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 <ds@schleef.org>
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
19 #include <linux/module.h>
20
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/fcntl.h>
25 #include <linux/mm.h>
26 #include <linux/io.h>
27
28 #include "../comedi.h"
29 #include "../comedilib.h"
30 #include "../comedidev.h"
31
32 MODULE_AUTHOR("David Schleef <ds@schleef.org>");
33 MODULE_DESCRIPTION("Comedi kernel library");
34 MODULE_LICENSE("GPL");
35
36 struct comedi_device *comedi_open(const char *filename)
37 {
38         struct comedi_device *dev;
39         unsigned int minor;
40
41         if (strncmp(filename, "/dev/comedi", 11) != 0)
42                 return NULL;
43
44         minor = simple_strtoul(filename + 11, NULL, 0);
45
46         if (minor >= COMEDI_NUM_BOARD_MINORS)
47                 return NULL;
48
49         dev = comedi_dev_from_minor(minor);
50
51         if (!dev || !dev->attached)
52                 return NULL;
53
54         if (!try_module_get(dev->driver->module))
55                 return NULL;
56
57         return dev;
58 }
59 EXPORT_SYMBOL_GPL(comedi_open);
60
61 int comedi_close(struct comedi_device *d)
62 {
63         struct comedi_device *dev = (struct comedi_device *)d;
64
65         module_put(dev->driver->module);
66
67         return 0;
68 }
69 EXPORT_SYMBOL_GPL(comedi_close);
70
71 static int comedi_do_insn(struct comedi_device *dev,
72                           struct comedi_insn *insn,
73                           unsigned int *data)
74 {
75         struct comedi_subdevice *s;
76         int ret = 0;
77
78         /* a subdevice instruction */
79         if (insn->subdev >= dev->n_subdevices) {
80                 ret = -EINVAL;
81                 goto error;
82         }
83         s = &dev->subdevices[insn->subdev];
84
85         if (s->type == COMEDI_SUBD_UNUSED) {
86                 dev_err(dev->class_dev,
87                         "%d not useable subdevice\n", insn->subdev);
88                 ret = -EIO;
89                 goto error;
90         }
91
92         /* XXX check lock */
93
94         ret = comedi_check_chanlist(s, 1, &insn->chanspec);
95         if (ret < 0) {
96                 dev_err(dev->class_dev, "bad chanspec\n");
97                 ret = -EINVAL;
98                 goto error;
99         }
100
101         if (s->busy) {
102                 ret = -EBUSY;
103                 goto error;
104         }
105         s->busy = dev;
106
107         switch (insn->insn) {
108         case INSN_BITS:
109                 ret = s->insn_bits(dev, s, insn, data);
110                 break;
111         case INSN_CONFIG:
112                 /* XXX should check instruction length */
113                 ret = s->insn_config(dev, s, insn, data);
114                 break;
115         default:
116                 ret = -EINVAL;
117                 break;
118         }
119
120         s->busy = NULL;
121 error:
122
123         return ret;
124 }
125
126 int comedi_dio_get_config(struct comedi_device *dev, unsigned int subdev,
127                           unsigned int chan, unsigned int *io)
128 {
129         struct comedi_insn insn;
130         unsigned int data[2];
131         int ret;
132
133         memset(&insn, 0, sizeof(insn));
134         insn.insn = INSN_CONFIG;
135         insn.n = 2;
136         insn.subdev = subdev;
137         insn.chanspec = CR_PACK(chan, 0, 0);
138         data[0] = INSN_CONFIG_DIO_QUERY;
139         data[1] = 0;
140         ret = comedi_do_insn(dev, &insn, data);
141         if (ret >= 0)
142                 *io = data[1];
143         return ret;
144 }
145 EXPORT_SYMBOL_GPL(comedi_dio_get_config);
146
147 int comedi_dio_config(struct comedi_device *dev, unsigned int subdev,
148                       unsigned int chan, unsigned int io)
149 {
150         struct comedi_insn insn;
151
152         memset(&insn, 0, sizeof(insn));
153         insn.insn = INSN_CONFIG;
154         insn.n = 1;
155         insn.subdev = subdev;
156         insn.chanspec = CR_PACK(chan, 0, 0);
157
158         return comedi_do_insn(dev, &insn, &io);
159 }
160 EXPORT_SYMBOL_GPL(comedi_dio_config);
161
162 int comedi_dio_bitfield2(struct comedi_device *dev, unsigned int subdev,
163                          unsigned int mask, unsigned int *bits,
164                          unsigned int base_channel)
165 {
166         struct comedi_insn insn;
167         unsigned int data[2];
168         unsigned int n_chan;
169         unsigned int shift;
170         int ret;
171
172         if (subdev >= dev->n_subdevices)
173                 return -EINVAL;
174
175         base_channel = CR_CHAN(base_channel);
176         n_chan = comedi_get_n_channels(dev, subdev);
177         if (base_channel >= n_chan)
178                 return -EINVAL;
179
180         memset(&insn, 0, sizeof(insn));
181         insn.insn = INSN_BITS;
182         insn.chanspec = base_channel;
183         insn.n = 2;
184         insn.subdev = subdev;
185
186         data[0] = mask;
187         data[1] = *bits;
188
189         /*
190          * Most drivers ignore the base channel in insn->chanspec.
191          * Fix this here if the subdevice has <= 32 channels.
192          */
193         if (n_chan <= 32) {
194                 shift = base_channel;
195                 if (shift) {
196                         insn.chanspec = 0;
197                         data[0] <<= shift;
198                         data[1] <<= shift;
199                 }
200         } else {
201                 shift = 0;
202         }
203
204         ret = comedi_do_insn(dev, &insn, data);
205         *bits = data[1] >> shift;
206         return ret;
207 }
208 EXPORT_SYMBOL_GPL(comedi_dio_bitfield2);
209
210 int comedi_find_subdevice_by_type(struct comedi_device *dev, int type,
211                                   unsigned int subd)
212 {
213         struct comedi_subdevice *s;
214
215         if (subd > dev->n_subdevices)
216                 return -ENODEV;
217
218         for (; subd < dev->n_subdevices; subd++) {
219                 s = &dev->subdevices[subd];
220                 if (s->type == type)
221                         return subd;
222         }
223         return -1;
224 }
225 EXPORT_SYMBOL_GPL(comedi_find_subdevice_by_type);
226
227 int comedi_get_n_channels(struct comedi_device *dev, unsigned int subdevice)
228 {
229         struct comedi_subdevice *s = &dev->subdevices[subdevice];
230
231         return s->n_chan;
232 }
233 EXPORT_SYMBOL_GPL(comedi_get_n_channels);