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 comedi_t *comedi_open(const char *filename)
47 struct comedi_device_file_info *dev_file_info;
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))
70 return (comedi_t *) dev;
73 comedi_t *comedi_open_old(unsigned int minor)
75 struct comedi_device_file_info *dev_file_info;
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)
89 return (comedi_t *) dev;
92 int comedi_close(comedi_t * d)
94 comedi_device *dev = (comedi_device *) d;
96 module_put(dev->driver->module);
101 int comedi_loglevel(int newlevel)
106 void comedi_perror(const char *message)
108 rt_printk("%s: unknown error\n", message);
111 char *comedi_strerror(int err)
113 return "unknown error";
116 int comedi_fileno(comedi_t * d)
118 comedi_device *dev = (comedi_device *) d;
120 /* return something random */
124 int comedi_command(comedi_t * d, comedi_cmd * cmd)
126 comedi_device *dev = (comedi_device *) d;
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 #ifdef CONFIG_COMEDI_RT
154 if (comedi_switch_to_rt(dev) == 0)
157 comedi_set_subdevice_runflags(s, ~0, runflags);
159 comedi_reset_async_buf(async);
161 return s->do_cmd(dev, s);
164 int comedi_command_test(comedi_t * d, comedi_cmd * cmd)
166 comedi_device *dev = (comedi_device *) d;
169 if (cmd->subdev >= dev->n_subdevices)
172 s = dev->subdevices + cmd->subdev;
173 if (s->type == COMEDI_SUBD_UNUSED)
176 if (s->async == NULL)
179 return s->do_cmdtest(dev, s, cmd);
184 * perform an instruction
186 int comedi_do_insn(comedi_t * d, comedi_insn * insn)
188 comedi_device *dev = (comedi_device *) d;
192 if (insn->insn & INSN_MASK_SPECIAL) {
193 switch (insn->insn) {
198 do_gettimeofday(&tv);
199 insn->data[0] = tv.tv_sec;
200 insn->data[1] = tv.tv_usec;
206 /* XXX isn't the value supposed to be nanosecs? */
207 if (insn->n != 1 || insn->data[0] >= 100) {
211 comedi_udelay(insn->data[0]);
219 if (insn->subdev >= dev->n_subdevices) {
220 rt_printk("%d not usable subdevice\n",
225 s = dev->subdevices + insn->subdev;
227 rt_printk("no async\n");
231 if (!s->async->inttrig) {
232 rt_printk("no inttrig\n");
236 ret = s->async->inttrig(dev, s, insn->data[0]);
244 /* a subdevice instruction */
245 if (insn->subdev >= dev->n_subdevices) {
249 s = dev->subdevices + insn->subdev;
251 if (s->type == COMEDI_SUBD_UNUSED) {
252 rt_printk("%d not useable subdevice\n", insn->subdev);
259 if ((ret = check_chanlist(s, 1, &insn->chanspec)) < 0) {
260 rt_printk("bad chanspec\n");
271 switch (insn->insn) {
273 ret = s->insn_read(dev, s, insn, insn->data);
276 ret = s->insn_write(dev, s, insn, insn->data);
279 ret = s->insn_bits(dev, s, insn, insn->data);
282 /* XXX should check instruction length */
283 ret = s->insn_config(dev, s, insn, insn->data);
295 /* XXX do we want this? -- abbotti #if'ed it out for now. */
296 if (ret != insn->n) {
297 rt_printk("BUG: result of insn != insn.n\n");
321 - ioctl/rt lock (this type)
322 - lock while subdevice busy
323 - lock while subdevice being programmed
326 int comedi_lock(comedi_t * d, unsigned int subdevice)
328 comedi_device *dev = (comedi_device *) d;
333 if (subdevice >= dev->n_subdevices) {
336 s = dev->subdevices + subdevice;
338 comedi_spin_lock_irqsave(&s->spin_lock, flags);
350 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
369 int comedi_unlock(comedi_t * d, unsigned int subdevice)
371 comedi_device *dev = (comedi_device *) d;
377 if (subdevice >= dev->n_subdevices) {
380 s = dev->subdevices + subdevice;
384 comedi_spin_lock_irqsave(&s->spin_lock, flags);
388 } else if (s->lock && s->lock != (void *)d) {
395 async->cb_func = NULL;
396 async->cb_arg = NULL;
402 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
409 cancel acquisition ioctl
421 int comedi_cancel(comedi_t * d, unsigned int subdevice)
423 comedi_device *dev = (comedi_device *) d;
427 if (subdevice >= dev->n_subdevices) {
430 s = dev->subdevices + subdevice;
432 if (s->lock && s->lock != d)
443 if (!s->cancel || !s->async)
446 if ((ret = s->cancel(dev, s)))
449 #ifdef CONFIG_COMEDI_RT
450 if (comedi_get_subdevice_runflags(s) & SRF_RT) {
451 comedi_switch_to_non_rt(dev);
454 comedi_set_subdevice_runflags(s, SRF_RUNNING | SRF_RT, 0);
455 s->async->inttrig = NULL;
462 registration of callback functions
464 int comedi_register_callback(comedi_t * d, unsigned int subdevice,
465 unsigned int mask, int (*cb) (unsigned int, void *), void *arg)
467 comedi_device *dev = (comedi_device *) d;
471 if (subdevice >= dev->n_subdevices) {
474 s = dev->subdevices + subdevice;
477 if (s->type == COMEDI_SUBD_UNUSED || !async)
480 /* are we locked? (ioctl lock) */
481 if (s->lock && s->lock != d)
490 async->cb_func = NULL;
491 async->cb_arg = NULL;
493 async->cb_mask = mask;
501 int comedi_poll(comedi_t * d, unsigned int subdevice)
503 comedi_device *dev = (comedi_device *) d;
504 comedi_subdevice *s = dev->subdevices;
507 if (subdevice >= dev->n_subdevices) {
510 s = dev->subdevices + subdevice;
513 if (s->type == COMEDI_SUBD_UNUSED || !async)
516 /* are we locked? (ioctl lock) */
517 if (s->lock && s->lock != d)
520 /* are we running? XXX wrong? */
524 return s->poll(dev, s);
527 /* WARNING: not portable */
528 int comedi_map(comedi_t * d, unsigned int subdevice, void *ptr)
530 comedi_device *dev = (comedi_device *) d;
533 if (subdevice >= dev->n_subdevices) {
536 s = dev->subdevices + subdevice;
542 *((void **)ptr) = s->async->prealloc_buf;
545 /* XXX no reference counting */
550 /* WARNING: not portable */
551 int comedi_unmap(comedi_t * d, unsigned int subdevice)
553 comedi_device *dev = (comedi_device *) d;
556 if (subdevice >= dev->n_subdevices) {
559 s = dev->subdevices + subdevice;
564 /* XXX no reference counting */