GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / comedi / kcomedilib / kcomedilib_main.c
blob149ffcace4b36bc91e9922cea118f9b25cc3d01b
1 /*
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 #define __NO_VERSION__
25 #include <linux/module.h>
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>
36 #include "../comedi.h"
37 #include "../comedilib.h"
38 #include "../comedidev.h"
40 MODULE_AUTHOR("David Schleef <ds@schleef.org>");
41 MODULE_DESCRIPTION("Comedi kernel library");
42 MODULE_LICENSE("GPL");
44 struct comedi_device *comedi_open(const char *filename)
46 struct comedi_device_file_info *dev_file_info;
47 struct comedi_device *dev;
48 unsigned int minor;
50 if (strncmp(filename, "/dev/comedi", 11) != 0)
51 return NULL;
53 minor = simple_strtoul(filename + 11, NULL, 0);
55 if (minor >= COMEDI_NUM_BOARD_MINORS)
56 return NULL;
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;
63 if (dev == NULL || !dev->attached)
64 return NULL;
66 if (!try_module_get(dev->driver->module))
67 return NULL;
69 return dev;
71 EXPORT_SYMBOL(comedi_open);
73 int comedi_close(struct comedi_device *d)
75 struct comedi_device *dev = (struct comedi_device *)d;
77 module_put(dev->driver->module);
79 return 0;
81 EXPORT_SYMBOL(comedi_close);
83 static int comedi_do_insn(struct comedi_device *dev, struct comedi_insn *insn)
85 struct comedi_subdevice *s;
86 int ret = 0;
88 /* a subdevice instruction */
89 if (insn->subdev >= dev->n_subdevices) {
90 ret = -EINVAL;
91 goto error;
93 s = dev->subdevices + insn->subdev;
95 if (s->type == COMEDI_SUBD_UNUSED) {
96 printk(KERN_ERR "%d not useable subdevice\n", insn->subdev);
97 ret = -EIO;
98 goto error;
102 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
103 if (ret < 0) {
104 printk(KERN_ERR "bad chanspec\n");
105 ret = -EINVAL;
106 goto error;
109 if (s->busy) {
110 ret = -EBUSY;
111 goto error;
113 s->busy = dev;
115 switch (insn->insn) {
116 case INSN_BITS:
117 ret = s->insn_bits(dev, s, insn, insn->data);
118 break;
119 case INSN_CONFIG:
120 ret = s->insn_config(dev, s, insn, insn->data);
121 break;
122 default:
123 ret = -EINVAL;
124 break;
127 s->busy = NULL;
128 error:
130 return ret;
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;
140 insn.n = 1;
141 insn.data = &io;
142 insn.subdev = subdev;
143 insn.chanspec = CR_PACK(chan, 0, 0);
145 return comedi_do_insn(dev, &insn);
147 EXPORT_SYMBOL(comedi_dio_config);
149 int comedi_dio_bitfield(struct comedi_device *dev, unsigned int subdev,
150 unsigned int mask, unsigned int *bits)
152 struct comedi_insn insn;
153 unsigned int data[2];
154 int ret;
156 memset(&insn, 0, sizeof(insn));
157 insn.insn = INSN_BITS;
158 insn.n = 2;
159 insn.data = data;
160 insn.subdev = subdev;
162 data[0] = mask;
163 data[1] = *bits;
165 ret = comedi_do_insn(dev, &insn);
167 *bits = data[1];
169 return ret;
171 EXPORT_SYMBOL(comedi_dio_bitfield);
173 int comedi_find_subdevice_by_type(struct comedi_device *dev, int type,
174 unsigned int subd)
176 if (subd > dev->n_subdevices)
177 return -ENODEV;
179 for (; subd < dev->n_subdevices; subd++) {
180 if (dev->subdevices[subd].type == type)
181 return subd;
183 return -1;
185 EXPORT_SYMBOL(comedi_find_subdevice_by_type);
187 int comedi_get_n_channels(struct comedi_device *dev, unsigned int subdevice)
189 struct comedi_subdevice *s = dev->subdevices + subdevice;
191 return s->n_chan;
193 EXPORT_SYMBOL(comedi_get_n_channels);