2 * kcomedilib/kcomedilib.c
3 * a comedlib interface for kernel modules
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
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.
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.
19 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/fcntl.h>
28 #include "../comedi.h"
29 #include "../comedilib.h"
30 #include "../comedidev.h"
32 MODULE_AUTHOR("David Schleef <ds@schleef.org>");
33 MODULE_DESCRIPTION("Comedi kernel library");
34 MODULE_LICENSE("GPL");
36 struct comedi_device *comedi_open(const char *filename)
38 struct comedi_device *dev, *retval = NULL;
41 if (strncmp(filename, "/dev/comedi", 11) != 0)
44 if (kstrtouint(filename + 11, 0, &minor))
47 if (minor >= COMEDI_NUM_BOARD_MINORS)
50 dev = comedi_dev_get_from_minor(minor);
54 down_read(&dev->attach_lock);
59 up_read(&dev->attach_lock);
66 EXPORT_SYMBOL_GPL(comedi_open);
68 int comedi_close(struct comedi_device *dev)
73 EXPORT_SYMBOL_GPL(comedi_close);
75 static int comedi_do_insn(struct comedi_device *dev,
76 struct comedi_insn *insn,
79 struct comedi_subdevice *s;
82 mutex_lock(&dev->mutex);
89 /* a subdevice instruction */
90 if (insn->subdev >= dev->n_subdevices) {
94 s = &dev->subdevices[insn->subdev];
96 if (s->type == COMEDI_SUBD_UNUSED) {
97 dev_err(dev->class_dev,
98 "%d not usable subdevice\n", insn->subdev);
105 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
107 dev_err(dev->class_dev, "bad chanspec\n");
118 switch (insn->insn) {
120 ret = s->insn_bits(dev, s, insn, data);
123 /* XXX should check instruction length */
124 ret = s->insn_config(dev, s, insn, data);
134 mutex_unlock(&dev->mutex);
138 int comedi_dio_get_config(struct comedi_device *dev, unsigned int subdev,
139 unsigned int chan, unsigned int *io)
141 struct comedi_insn insn;
142 unsigned int data[2];
145 memset(&insn, 0, sizeof(insn));
146 insn.insn = INSN_CONFIG;
148 insn.subdev = subdev;
149 insn.chanspec = CR_PACK(chan, 0, 0);
150 data[0] = INSN_CONFIG_DIO_QUERY;
152 ret = comedi_do_insn(dev, &insn, data);
157 EXPORT_SYMBOL_GPL(comedi_dio_get_config);
159 int comedi_dio_config(struct comedi_device *dev, unsigned int subdev,
160 unsigned int chan, unsigned int io)
162 struct comedi_insn insn;
164 memset(&insn, 0, sizeof(insn));
165 insn.insn = INSN_CONFIG;
167 insn.subdev = subdev;
168 insn.chanspec = CR_PACK(chan, 0, 0);
170 return comedi_do_insn(dev, &insn, &io);
172 EXPORT_SYMBOL_GPL(comedi_dio_config);
174 int comedi_dio_bitfield2(struct comedi_device *dev, unsigned int subdev,
175 unsigned int mask, unsigned int *bits,
176 unsigned int base_channel)
178 struct comedi_insn insn;
179 unsigned int data[2];
184 base_channel = CR_CHAN(base_channel);
185 n_chan = comedi_get_n_channels(dev, subdev);
186 if (base_channel >= n_chan)
189 memset(&insn, 0, sizeof(insn));
190 insn.insn = INSN_BITS;
191 insn.chanspec = base_channel;
193 insn.subdev = subdev;
199 * Most drivers ignore the base channel in insn->chanspec.
200 * Fix this here if the subdevice has <= 32 channels.
203 shift = base_channel;
213 ret = comedi_do_insn(dev, &insn, data);
214 *bits = data[1] >> shift;
217 EXPORT_SYMBOL_GPL(comedi_dio_bitfield2);
219 int comedi_find_subdevice_by_type(struct comedi_device *dev, int type,
222 struct comedi_subdevice *s;
225 down_read(&dev->attach_lock);
227 for (; subd < dev->n_subdevices; subd++) {
228 s = &dev->subdevices[subd];
229 if (s->type == type) {
234 up_read(&dev->attach_lock);
237 EXPORT_SYMBOL_GPL(comedi_find_subdevice_by_type);
239 int comedi_get_n_channels(struct comedi_device *dev, unsigned int subdevice)
243 down_read(&dev->attach_lock);
244 if (!dev->attached || subdevice >= dev->n_subdevices)
247 n = dev->subdevices[subdevice].n_chan;
248 up_read(&dev->attach_lock);
252 EXPORT_SYMBOL_GPL(comedi_get_n_channels);
254 static int __init kcomedilib_module_init(void)
259 static void __exit kcomedilib_module_exit(void)
263 module_init(kcomedilib_module_init);
264 module_exit(kcomedilib_module_exit);