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;
41 if (strncmp(filename, "/dev/comedi", 11) != 0)
44 minor = simple_strtoul(filename + 11, NULL, 0);
46 if (minor >= COMEDI_NUM_BOARD_MINORS)
49 dev = comedi_dev_from_minor(minor);
51 if (!dev || !dev->attached)
54 if (!try_module_get(dev->driver->module))
59 EXPORT_SYMBOL_GPL(comedi_open);
61 int comedi_close(struct comedi_device *d)
63 struct comedi_device *dev = (struct comedi_device *)d;
65 module_put(dev->driver->module);
69 EXPORT_SYMBOL_GPL(comedi_close);
71 static int comedi_do_insn(struct comedi_device *dev,
72 struct comedi_insn *insn,
75 struct comedi_subdevice *s;
78 /* a subdevice instruction */
79 if (insn->subdev >= dev->n_subdevices) {
83 s = &dev->subdevices[insn->subdev];
85 if (s->type == COMEDI_SUBD_UNUSED) {
86 dev_err(dev->class_dev,
87 "%d not useable subdevice\n", insn->subdev);
94 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
96 dev_err(dev->class_dev, "bad chanspec\n");
107 switch (insn->insn) {
109 ret = s->insn_bits(dev, s, insn, data);
112 /* XXX should check instruction length */
113 ret = s->insn_config(dev, s, insn, data);
126 int comedi_dio_get_config(struct comedi_device *dev, unsigned int subdev,
127 unsigned int chan, unsigned int *io)
129 struct comedi_insn insn;
130 unsigned int data[2];
133 memset(&insn, 0, sizeof(insn));
134 insn.insn = INSN_CONFIG;
136 insn.subdev = subdev;
137 insn.chanspec = CR_PACK(chan, 0, 0);
138 data[0] = INSN_CONFIG_DIO_QUERY;
140 ret = comedi_do_insn(dev, &insn, data);
145 EXPORT_SYMBOL_GPL(comedi_dio_get_config);
147 int comedi_dio_config(struct comedi_device *dev, unsigned int subdev,
148 unsigned int chan, unsigned int io)
150 struct comedi_insn insn;
152 memset(&insn, 0, sizeof(insn));
153 insn.insn = INSN_CONFIG;
155 insn.subdev = subdev;
156 insn.chanspec = CR_PACK(chan, 0, 0);
158 return comedi_do_insn(dev, &insn, &io);
160 EXPORT_SYMBOL_GPL(comedi_dio_config);
162 int comedi_dio_bitfield2(struct comedi_device *dev, unsigned int subdev,
163 unsigned int mask, unsigned int *bits,
164 unsigned int base_channel)
166 struct comedi_insn insn;
167 unsigned int data[2];
172 if (subdev >= dev->n_subdevices)
175 base_channel = CR_CHAN(base_channel);
176 n_chan = comedi_get_n_channels(dev, subdev);
177 if (base_channel >= n_chan)
180 memset(&insn, 0, sizeof(insn));
181 insn.insn = INSN_BITS;
182 insn.chanspec = base_channel;
184 insn.subdev = subdev;
190 * Most drivers ignore the base channel in insn->chanspec.
191 * Fix this here if the subdevice has <= 32 channels.
194 shift = base_channel;
204 ret = comedi_do_insn(dev, &insn, data);
205 *bits = data[1] >> shift;
208 EXPORT_SYMBOL_GPL(comedi_dio_bitfield2);
210 int comedi_find_subdevice_by_type(struct comedi_device *dev, int type,
213 struct comedi_subdevice *s;
215 if (subd > dev->n_subdevices)
218 for (; subd < dev->n_subdevices; subd++) {
219 s = &dev->subdevices[subd];
225 EXPORT_SYMBOL_GPL(comedi_find_subdevice_by_type);
227 int comedi_get_n_channels(struct comedi_device *dev, unsigned int subdevice)
229 struct comedi_subdevice *s = &dev->subdevices[subdevice];
233 EXPORT_SYMBOL_GPL(comedi_get_n_channels);