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>
34 #include <linux/slab.h>
37 #include "../comedi.h"
38 #include "../comedilib.h"
39 #include "../comedidev.h"
41 MODULE_AUTHOR("David Schleef <ds@schleef.org>");
42 MODULE_DESCRIPTION("Comedi kernel library");
43 MODULE_LICENSE("GPL");
45 void *comedi_open(const char *filename)
47 struct comedi_device_file_info *dev_file_info;
48 struct comedi_device *dev;
51 if (strncmp(filename, "/dev/comedi", 11) != 0)
54 minor = simple_strtoul(filename + 11, NULL, 0);
56 if (minor >= COMEDI_NUM_BOARD_MINORS)
59 dev_file_info = comedi_get_device_file_info(minor);
60 if (dev_file_info == NULL)
62 dev = dev_file_info->device;
64 if (dev == NULL || !dev->attached)
67 if (!try_module_get(dev->driver->module))
73 void *comedi_open_old(unsigned int minor)
75 struct comedi_device_file_info *dev_file_info;
76 struct comedi_device *dev;
78 if (minor >= COMEDI_NUM_MINORS)
81 dev_file_info = comedi_get_device_file_info(minor);
82 if (dev_file_info == NULL)
84 dev = dev_file_info->device;
86 if (dev == NULL || !dev->attached)
92 int comedi_close(void *d)
94 struct comedi_device *dev = (struct comedi_device *)d;
96 module_put(dev->driver->module);
101 int comedi_loglevel(int newlevel)
106 void comedi_perror(const char *message)
108 printk("%s: unknown error\n", message);
111 char *comedi_strerror(int err)
113 return "unknown error";
116 int comedi_fileno(void *d)
118 struct comedi_device *dev = (struct comedi_device *)d;
120 /* return something random */
124 int comedi_command(void *d, struct comedi_cmd *cmd)
126 struct comedi_device *dev = (struct comedi_device *)d;
127 struct comedi_subdevice *s;
128 struct comedi_async *async;
131 if (cmd->subdev >= dev->n_subdevices)
134 s = dev->subdevices + cmd->subdev;
135 if (s->type == COMEDI_SUBD_UNUSED)
146 if (async->cb_mask & COMEDI_CB_EOS)
147 cmd->flags |= TRIG_WAKE_EOS;
151 runflags = SRF_RUNNING;
153 comedi_set_subdevice_runflags(s, ~0, runflags);
155 comedi_reset_async_buf(async);
157 return s->do_cmd(dev, s);
160 int comedi_command_test(void *d, struct comedi_cmd *cmd)
162 struct comedi_device *dev = (struct comedi_device *)d;
163 struct comedi_subdevice *s;
165 if (cmd->subdev >= dev->n_subdevices)
168 s = dev->subdevices + cmd->subdev;
169 if (s->type == COMEDI_SUBD_UNUSED)
172 if (s->async == NULL)
175 return s->do_cmdtest(dev, s, cmd);
180 * perform an instruction
182 int comedi_do_insn(void *d, struct comedi_insn *insn)
184 struct comedi_device *dev = (struct comedi_device *)d;
185 struct comedi_subdevice *s;
188 if (insn->insn & INSN_MASK_SPECIAL) {
189 switch (insn->insn) {
194 do_gettimeofday(&tv);
195 insn->data[0] = tv.tv_sec;
196 insn->data[1] = tv.tv_usec;
202 /* XXX isn't the value supposed to be nanosecs? */
203 if (insn->n != 1 || insn->data[0] >= 100) {
207 udelay(insn->data[0]);
215 if (insn->subdev >= dev->n_subdevices) {
216 printk("%d not usable subdevice\n",
221 s = dev->subdevices + insn->subdev;
223 printk("no async\n");
227 if (!s->async->inttrig) {
228 printk("no inttrig\n");
232 ret = s->async->inttrig(dev, s, insn->data[0]);
240 /* a subdevice instruction */
241 if (insn->subdev >= dev->n_subdevices) {
245 s = dev->subdevices + insn->subdev;
247 if (s->type == COMEDI_SUBD_UNUSED) {
248 printk("%d not useable subdevice\n", insn->subdev);
255 ret = check_chanlist(s, 1, &insn->chanspec);
257 printk("bad chanspec\n");
268 switch (insn->insn) {
270 ret = s->insn_read(dev, s, insn, insn->data);
273 ret = s->insn_write(dev, s, insn, insn->data);
276 ret = s->insn_bits(dev, s, insn, insn->data);
279 /* XXX should check instruction length */
280 ret = s->insn_config(dev, s, insn, insn->data);
292 /* XXX do we want this? -- abbotti #if'ed it out for now. */
293 if (ret != insn->n) {
294 printk("BUG: result of insn != insn.n\n");
318 - ioctl/rt lock (this type)
319 - lock while subdevice busy
320 - lock while subdevice being programmed
323 int comedi_lock(void *d, unsigned int subdevice)
325 struct comedi_device *dev = (struct comedi_device *)d;
326 struct comedi_subdevice *s;
330 if (subdevice >= dev->n_subdevices)
333 s = dev->subdevices + subdevice;
335 spin_lock_irqsave(&s->spin_lock, flags);
347 spin_unlock_irqrestore(&s->spin_lock, flags);
366 int comedi_unlock(void *d, unsigned int subdevice)
368 struct comedi_device *dev = (struct comedi_device *)d;
369 struct comedi_subdevice *s;
371 struct comedi_async *async;
374 if (subdevice >= dev->n_subdevices)
377 s = dev->subdevices + subdevice;
381 spin_lock_irqsave(&s->spin_lock, flags);
385 } else if (s->lock && s->lock != (void *)d) {
392 async->cb_func = NULL;
393 async->cb_arg = NULL;
399 spin_unlock_irqrestore(&s->spin_lock, flags);
406 cancel acquisition ioctl
418 int comedi_cancel(void *d, unsigned int subdevice)
420 struct comedi_device *dev = (struct comedi_device *)d;
421 struct comedi_subdevice *s;
424 if (subdevice >= dev->n_subdevices)
427 s = dev->subdevices + subdevice;
429 if (s->lock && s->lock != d)
440 if (!s->cancel || !s->async)
443 ret = s->cancel(dev, s);
448 comedi_set_subdevice_runflags(s, SRF_RUNNING | SRF_RT, 0);
449 s->async->inttrig = NULL;
456 registration of callback functions
458 int comedi_register_callback(void *d, unsigned int subdevice,
459 unsigned int mask, int (*cb) (unsigned int,
462 struct comedi_device *dev = (struct comedi_device *)d;
463 struct comedi_subdevice *s;
464 struct comedi_async *async;
466 if (subdevice >= dev->n_subdevices)
469 s = dev->subdevices + subdevice;
472 if (s->type == COMEDI_SUBD_UNUSED || !async)
475 /* are we locked? (ioctl lock) */
476 if (s->lock && s->lock != d)
485 async->cb_func = NULL;
486 async->cb_arg = NULL;
488 async->cb_mask = mask;
496 int comedi_poll(void *d, unsigned int subdevice)
498 struct comedi_device *dev = (struct comedi_device *)d;
499 struct comedi_subdevice *s = dev->subdevices;
500 struct comedi_async *async;
502 if (subdevice >= dev->n_subdevices)
505 s = dev->subdevices + subdevice;
508 if (s->type == COMEDI_SUBD_UNUSED || !async)
511 /* are we locked? (ioctl lock) */
512 if (s->lock && s->lock != d)
515 /* are we running? XXX wrong? */
519 return s->poll(dev, s);
522 /* WARNING: not portable */
523 int comedi_map(void *d, unsigned int subdevice, void *ptr)
525 struct comedi_device *dev = (struct comedi_device *)d;
526 struct comedi_subdevice *s;
528 if (subdevice >= dev->n_subdevices)
531 s = dev->subdevices + subdevice;
537 *((void **)ptr) = s->async->prealloc_buf;
539 /* XXX no reference counting */
544 /* WARNING: not portable */
545 int comedi_unmap(void *d, unsigned int subdevice)
547 struct comedi_device *dev = (struct comedi_device *)d;
548 struct comedi_subdevice *s;
550 if (subdevice >= dev->n_subdevices)
553 s = dev->subdevices + subdevice;
558 /* XXX no reference counting */