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.
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.
24 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/fcntl.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
35 #include "../comedi.h"
36 #include "../comedilib.h"
37 #include "../comedidev.h"
39 MODULE_AUTHOR("David Schleef <ds@schleef.org>");
40 MODULE_DESCRIPTION("Comedi kernel library");
41 MODULE_LICENSE("GPL");
43 struct comedi_device *comedi_open(const char *filename)
45 struct comedi_device *dev;
48 if (strncmp(filename, "/dev/comedi", 11) != 0)
51 minor = simple_strtoul(filename + 11, NULL, 0);
53 if (minor >= COMEDI_NUM_BOARD_MINORS)
56 dev = comedi_dev_from_minor(minor);
58 if (!dev || !dev->attached)
61 if (!try_module_get(dev->driver->module))
66 EXPORT_SYMBOL_GPL(comedi_open);
68 int comedi_close(struct comedi_device *d)
70 struct comedi_device *dev = (struct comedi_device *)d;
72 module_put(dev->driver->module);
76 EXPORT_SYMBOL_GPL(comedi_close);
78 static int comedi_do_insn(struct comedi_device *dev,
79 struct comedi_insn *insn,
82 struct comedi_subdevice *s;
85 /* a subdevice instruction */
86 if (insn->subdev >= dev->n_subdevices) {
90 s = &dev->subdevices[insn->subdev];
92 if (s->type == COMEDI_SUBD_UNUSED) {
93 dev_err(dev->class_dev,
94 "%d not useable subdevice\n", insn->subdev);
101 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
103 dev_err(dev->class_dev, "bad chanspec\n");
114 switch (insn->insn) {
116 ret = s->insn_bits(dev, s, insn, data);
119 /* XXX should check instruction length */
120 ret = s->insn_config(dev, s, insn, data);
133 int comedi_dio_config(struct comedi_device *dev, unsigned int subdev,
134 unsigned int chan, unsigned int io)
136 struct comedi_insn insn;
138 memset(&insn, 0, sizeof(insn));
139 insn.insn = INSN_CONFIG;
141 insn.subdev = subdev;
142 insn.chanspec = CR_PACK(chan, 0, 0);
144 return comedi_do_insn(dev, &insn, &io);
146 EXPORT_SYMBOL_GPL(comedi_dio_config);
148 int comedi_dio_bitfield(struct comedi_device *dev, unsigned int subdev,
149 unsigned int mask, unsigned int *bits)
151 struct comedi_insn insn;
152 unsigned int data[2];
155 memset(&insn, 0, sizeof(insn));
156 insn.insn = INSN_BITS;
158 insn.subdev = subdev;
163 ret = comedi_do_insn(dev, &insn, data);
169 EXPORT_SYMBOL_GPL(comedi_dio_bitfield);
171 int comedi_find_subdevice_by_type(struct comedi_device *dev, int type,
174 struct comedi_subdevice *s;
176 if (subd > dev->n_subdevices)
179 for (; subd < dev->n_subdevices; subd++) {
180 s = &dev->subdevices[subd];
186 EXPORT_SYMBOL_GPL(comedi_find_subdevice_by_type);
188 int comedi_get_n_channels(struct comedi_device *dev, unsigned int subdevice)
190 struct comedi_subdevice *s = &dev->subdevices[subdevice];
194 EXPORT_SYMBOL_GPL(comedi_get_n_channels);