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.
26 #define __NO_VERSION__
27 #include "comedi_fops.h"
28 #include "comedi_compat32.h"
30 #include <linux/module.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/fcntl.h>
35 #include <linux/delay.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/kmod.h>
40 #include <linux/poll.h>
41 #include <linux/init.h>
42 #include <linux/device.h>
43 #include <linux/vmalloc.h>
45 #include <linux/comedidev.h>
46 #include <linux/cdev.h>
47 #include <linux/stat.h>
50 #include <asm/uaccess.h>
54 MODULE_AUTHOR("http://www.comedi.org");
55 MODULE_DESCRIPTION("Comedi core module");
56 MODULE_LICENSE("GPL");
58 #ifdef CONFIG_COMEDI_DEBUG
60 module_param(comedi_debug, int, 0644);
63 int comedi_autoconfig = 1;
64 module_param(comedi_autoconfig, bool, 0444);
66 int comedi_num_legacy_minors = 0;
67 module_param(comedi_num_legacy_minors, int, 0444);
69 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
70 static struct comedi_device_file_info* comedi_file_info_table[COMEDI_NUM_MINORS];
72 static int do_devconfig_ioctl(comedi_device * dev, comedi_devconfig __user * arg);
73 static int do_bufconfig_ioctl(comedi_device * dev, comedi_bufconfig __user *arg);
74 static int do_devinfo_ioctl(comedi_device * dev, comedi_devinfo __user * arg,
76 static int do_subdinfo_ioctl(comedi_device * dev, comedi_subdinfo __user * arg,
78 static int do_chaninfo_ioctl(comedi_device * dev, comedi_chaninfo __user * arg);
79 static int do_bufinfo_ioctl(comedi_device * dev, comedi_bufinfo __user *arg, void *file);
80 static int do_cmd_ioctl(comedi_device * dev, comedi_cmd __user *arg, void *file);
81 static int do_lock_ioctl(comedi_device * dev, unsigned int arg, void *file);
82 static int do_unlock_ioctl(comedi_device * dev, unsigned int arg, void *file);
83 static int do_cancel_ioctl(comedi_device * dev, unsigned int arg, void *file);
84 static int do_cmdtest_ioctl(comedi_device * dev, comedi_cmd __user *arg, void *file);
85 static int do_insnlist_ioctl(comedi_device * dev, comedi_insnlist __user *arg, void *file);
86 static int do_insn_ioctl(comedi_device * dev, comedi_insn __user *arg, void *file);
87 static int do_poll_ioctl(comedi_device * dev, unsigned int subd, void *file);
89 void do_become_nonbusy(comedi_device * dev, comedi_subdevice * s);
90 static int do_cancel(comedi_device * dev, comedi_subdevice * s);
92 static int comedi_fasync(int fd, struct file *file, int on);
94 static int is_device_busy(comedi_device * dev);
95 static int resize_async_buffer(comedi_device *dev,
96 comedi_subdevice *s, comedi_async *async, unsigned new_size);
98 // sysfs attribute files
100 static COMEDI_DECLARE_ATTR_SHOW(show_max_read_buffer_kb, dev, buf);
101 static COMEDI_DECLARE_ATTR_STORE(store_max_read_buffer_kb, dev, buf, count);
102 static comedi_device_attribute_t dev_attr_max_read_buffer_kb =
105 .name = "max_read_buffer_kb",
106 .mode = S_IRUGO | S_IWUSR
108 .show = &show_max_read_buffer_kb,
109 .store = &store_max_read_buffer_kb
112 static COMEDI_DECLARE_ATTR_SHOW(show_read_buffer_kb, dev, buf);
113 static COMEDI_DECLARE_ATTR_STORE(store_read_buffer_kb, dev, buf, count);
114 static comedi_device_attribute_t dev_attr_read_buffer_kb =
117 .name = "read_buffer_kb",
118 .mode = S_IRUGO | S_IWUSR | S_IWGRP
120 .show = &show_read_buffer_kb,
121 .store = &store_read_buffer_kb
124 static COMEDI_DECLARE_ATTR_SHOW(show_max_write_buffer_kb, dev, buf);
125 static COMEDI_DECLARE_ATTR_STORE(store_max_write_buffer_kb, dev, buf, count);
126 static comedi_device_attribute_t dev_attr_max_write_buffer_kb =
129 .name = "max_write_buffer_kb",
130 .mode = S_IRUGO | S_IWUSR
132 .show = &show_max_write_buffer_kb,
133 .store = &store_max_write_buffer_kb
136 static COMEDI_DECLARE_ATTR_SHOW(show_write_buffer_kb, dev, buf);
137 static COMEDI_DECLARE_ATTR_STORE(store_write_buffer_kb, dev, buf, count);
138 static comedi_device_attribute_t dev_attr_write_buffer_kb =
141 .name = "write_buffer_kb",
142 .mode = S_IRUGO | S_IWUSR | S_IWGRP
144 .show = &show_write_buffer_kb,
145 .store = &store_write_buffer_kb
148 #ifdef HAVE_UNLOCKED_IOCTL
149 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
152 static int comedi_ioctl(struct inode *inode, struct file *file,
153 unsigned int cmd, unsigned long arg)
156 const unsigned minor = iminor(file->f_dentry->d_inode);
157 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
161 if (dev_file_info == NULL) return -ENODEV;
162 dev = dev_file_info->device;
163 if (dev == NULL) return -ENODEV;
165 mutex_lock(&dev->mutex);
167 /* Device config is special, because it must work on
168 * an unconfigured device. */
169 if (cmd == COMEDI_DEVCONFIG) {
170 rc = do_devconfig_ioctl(dev, (comedi_devconfig __user *)arg);
174 if (!dev->attached) {
175 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
181 case COMEDI_BUFCONFIG:
182 rc = do_bufconfig_ioctl(dev, (comedi_bufconfig __user *)arg);
185 rc = do_devinfo_ioctl(dev, (comedi_devinfo __user *)arg, file);
187 case COMEDI_SUBDINFO:
188 rc = do_subdinfo_ioctl(dev, (comedi_subdinfo __user *)arg, file);
190 case COMEDI_CHANINFO:
191 rc = do_chaninfo_ioctl(dev, (comedi_chaninfo __user *)arg);
193 case COMEDI_RANGEINFO:
194 rc = do_rangeinfo_ioctl(dev, (comedi_rangeinfo __user *)arg);
197 rc = do_bufinfo_ioctl(dev, (comedi_bufinfo __user *)arg, file);
200 rc = do_lock_ioctl(dev, arg, file);
203 rc = do_unlock_ioctl(dev, arg, file);
206 rc = do_cancel_ioctl(dev, arg, file);
209 rc = do_cmd_ioctl(dev, (comedi_cmd __user *)arg, file);
212 rc = do_cmdtest_ioctl(dev, (comedi_cmd __user *)arg, file);
214 case COMEDI_INSNLIST:
215 rc = do_insnlist_ioctl(dev, (comedi_insnlist __user *)arg, file);
218 rc = do_insn_ioctl(dev, (comedi_insn __user *)arg, file);
221 rc = do_poll_ioctl(dev, arg, file);
229 mutex_unlock(&dev->mutex);
238 pointer to devconfig structure
241 devconfig structure at arg
246 static int do_devconfig_ioctl(comedi_device * dev,
247 comedi_devconfig __user * arg)
251 unsigned char *aux_data = NULL;
254 if (!capable(CAP_SYS_ADMIN))
258 if (is_device_busy(dev))
262 struct module *driver_module = dev->driver->module;
263 comedi_device_detach(dev);
264 module_put(driver_module);
269 if (copy_from_user(&it, arg, sizeof(comedi_devconfig)))
272 it.board_name[COMEDI_NAMELEN - 1] = 0;
274 if (comedi_aux_data(it.options, 0) &&
275 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
277 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
281 aux_data = vmalloc(aux_len);
285 if (copy_from_user(aux_data,
286 (void __user *)comedi_aux_data(it.options, 0),
291 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
292 (unsigned long)aux_data;
293 if (sizeof(void *) > sizeof(int)) {
294 bit_shift = sizeof(int) * 8;
295 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
296 ((unsigned long)aux_data) >> bit_shift;
298 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
301 ret = comedi_device_attach(dev, &it);
304 if(!try_module_get(dev->driver->module)) {
305 comedi_device_detach(dev);
318 buffer configuration ioctl
321 pointer to bufconfig structure
327 modified bufconfig at arg
330 static int do_bufconfig_ioctl(comedi_device * dev, comedi_bufconfig __user *arg)
337 if (copy_from_user(&bc, arg, sizeof(comedi_bufconfig)))
340 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
343 s = dev->subdevices + bc.subdevice;
347 DPRINTK("subdevice does not have async capability\n");
353 if (bc.maximum_size) {
354 if (!capable(CAP_SYS_ADMIN))
357 async->max_bufsize = bc.maximum_size;
361 retval = resize_async_buffer(dev, s, async, bc.size);
362 if(retval < 0) return retval;
365 bc.size = async->prealloc_bufsz;
366 bc.maximum_size = async->max_bufsize;
369 if (copy_to_user(arg, &bc, sizeof(comedi_bufconfig)))
380 pointer to devinfo structure
389 static int do_devinfo_ioctl(comedi_device * dev, comedi_devinfo __user * arg,
392 comedi_devinfo devinfo;
393 const unsigned minor = iminor(file->f_dentry->d_inode);
394 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
395 comedi_subdevice *read_subdev = comedi_get_read_subdevice(dev_file_info);
396 comedi_subdevice *write_subdev = comedi_get_write_subdevice(dev_file_info);
398 memset(&devinfo, 0, sizeof(devinfo));
400 /* fill devinfo structure */
401 devinfo.version_code = COMEDI_VERSION_CODE;
402 devinfo.n_subdevs = dev->n_subdevices;
403 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
404 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
407 devinfo.read_subdevice = read_subdev - dev->subdevices;
409 devinfo.read_subdevice = -1;
412 devinfo.write_subdevice = write_subdev - dev->subdevices;
414 devinfo.write_subdevice = -1;
417 if (copy_to_user(arg, &devinfo, sizeof(comedi_devinfo)))
428 pointer to array of subdevice info structures
434 array of subdevice info structures at arg
437 static int do_subdinfo_ioctl(comedi_device * dev, comedi_subdinfo __user * arg,
441 comedi_subdinfo *tmp, *us;
444 tmp = kcalloc(dev->n_subdevices, sizeof(comedi_subdinfo), GFP_KERNEL);
448 /* fill subdinfo structs */
449 for (i = 0; i < dev->n_subdevices; i++) {
450 s = dev->subdevices + i;
454 us->n_chan = s->n_chan;
455 us->subd_flags = s->subdev_flags;
456 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
457 us->subd_flags |= SDF_RUNNING;
458 #define TIMER_nanosec 5 /* backwards compatibility */
459 us->timer_type = TIMER_nanosec;
460 us->len_chanlist = s->len_chanlist;
461 us->maxdata = s->maxdata;
462 if (s->range_table) {
464 (i << 24) | (0 << 16) | (s->
465 range_table->length);
467 us->range_type = 0; /* XXX */
469 us->flags = s->flags;
472 us->subd_flags |= SDF_BUSY;
474 us->subd_flags |= SDF_BUSY_OWNER;
476 us->subd_flags |= SDF_LOCKED;
478 us->subd_flags |= SDF_LOCK_OWNER;
479 if (!s->maxdata && s->maxdata_list)
480 us->subd_flags |= SDF_MAXDATA;
482 us->subd_flags |= SDF_FLAGS;
483 if (s->range_table_list)
484 us->subd_flags |= SDF_RANGETYPE;
486 us->subd_flags |= SDF_CMD;
488 if (s->insn_bits != &insn_inval)
489 us->insn_bits_support = COMEDI_SUPPORTED;
491 us->insn_bits_support = COMEDI_UNSUPPORTED;
493 us->settling_time_0 = s->settling_time_0;
496 ret = copy_to_user(arg, tmp,
497 dev->n_subdevices * sizeof(comedi_subdinfo));
501 return ret ? -EFAULT : 0;
509 pointer to chaninfo structure
512 chaninfo structure at arg
515 arrays at elements of chaninfo structure
518 static int do_chaninfo_ioctl(comedi_device * dev, comedi_chaninfo __user * arg)
523 if (copy_from_user(&it, arg, sizeof(comedi_chaninfo)))
526 if (it.subdev >= dev->n_subdevices)
528 s = dev->subdevices + it.subdev;
530 if (it.maxdata_list) {
531 if (s->maxdata || !s->maxdata_list)
533 if (copy_to_user((lsampl_t __user *)it.maxdata_list,
534 s->maxdata_list, s->n_chan * sizeof(lsampl_t)))
541 if (copy_to_user((unsigned int __user *)it.flaglist,
542 s->flaglist, s->n_chan * sizeof(unsigned int)))
549 if (!s->range_table_list)
551 for (i = 0; i < s->n_chan; i++) {
554 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
555 (s->range_table_list[i]->length);
556 put_user(x, (unsigned int __user *)it.rangelist + i);
558 //if(copy_to_user((unsigned int __user *)it.rangelist,s->range_type_list,s->n_chan*sizeof(unsigned int)))
567 buffer information ioctl
570 pointer to bufinfo structure
576 modified bufinfo at arg
579 static int do_bufinfo_ioctl(comedi_device * dev, comedi_bufinfo __user *arg,
587 if (copy_from_user(&bi, arg, sizeof(comedi_bufinfo)))
590 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
593 s = dev->subdevices + bi.subdevice;
595 if (s->lock && s->lock != file)
600 DPRINTK("subdevice does not have async capability\n");
601 bi.buf_write_ptr = 0;
603 bi.buf_write_count = 0;
604 bi.buf_read_count = 0;
606 bi.bytes_written = 0;
611 bi.bytes_written = 0;
612 goto copyback_position;
617 if ((s->subdev_flags & SDF_CMD_READ) != 0) {
619 bi.bytes_read = comedi_buf_read_alloc(async,
621 comedi_buf_read_free(async, bi.bytes_read);
623 if (async->buf_write_count == async->buf_read_count) {
624 if (!(comedi_get_subdevice_runflags(s) & (SRF_RUNNING
626 do_become_nonbusy(dev, s);
628 if (bi.bytes_read == 0) {
629 if ((comedi_get_subdevice_runflags(s)
630 & (SRF_RUNNING | SRF_ERROR))
638 if ((s->subdev_flags & SDF_CMD_WRITE) != 0) {
639 if (bi.bytes_written) {
640 bi.bytes_written = comedi_buf_write_alloc(async,
642 comedi_buf_write_free(async, bi.bytes_written);
644 if (bi.bytes_written == 0) {
645 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
646 if ((comedi_get_subdevice_runflags(s)
658 bi.buf_write_count = async->buf_write_count;
659 bi.buf_write_ptr = async->buf_write_ptr;
660 bi.buf_read_count = async->buf_read_count;
661 bi.buf_read_ptr = async->buf_read_ptr;
664 if (copy_to_user(arg, &bi, sizeof(comedi_bufinfo)))
670 static int parse_insn(comedi_device * dev, comedi_insn * insn, lsampl_t * data,
674 * synchronous instructions
677 * pointer to sync cmd structure
680 * sync cmd struct at arg
687 static int do_insnlist_ioctl(comedi_device * dev, comedi_insnlist __user *arg,
690 comedi_insnlist insnlist;
691 comedi_insn *insns = NULL;
692 lsampl_t *data = NULL;
693 unsigned int max_samples;
697 if (copy_from_user(&insnlist, arg, sizeof(comedi_insnlist)))
700 if (insnlist.n_insns <= ULONG_MAX / sizeof(comedi_insn))
701 insns = kmalloc(sizeof(comedi_insn) * insnlist.n_insns,
704 DPRINTK("kmalloc failed\n");
709 if (copy_from_user(insns, (comedi_insn __user *)insnlist.insns,
710 sizeof(comedi_insn) * insnlist.n_insns)) {
711 DPRINTK("copy_from_user failed\n");
717 for (i = 0; i < insnlist.n_insns; i++) {
718 if (max_samples < insns[i].n)
719 max_samples = insns[i].n;
723 if (max_samples <= ULONG_MAX / sizeof(lsampl_t))
724 data = kmalloc(sizeof(lsampl_t) * max_samples,
727 DPRINTK("kmalloc failed\n");
733 for (i = 0; i < insnlist.n_insns; i++) {
734 if (insns[i].insn & INSN_MASK_WRITE) {
735 if (copy_from_user(data,
736 (lsampl_t __user *)insns[i].data,
737 insns[i].n * sizeof(lsampl_t))) {
738 DPRINTK("copy_from_user failed\n");
743 ret = parse_insn(dev, insns + i, data, file);
746 if (insns[i].insn & INSN_MASK_READ) {
747 if (copy_to_user((lsampl_t __user *)insns[i].data, data,
748 insns[i].n * sizeof(lsampl_t))) {
749 DPRINTK("copy_to_user failed\n");
769 static int check_insn_config_length(comedi_insn * insn, lsampl_t * data)
771 if(insn->n < 1) return -EINVAL;
774 case INSN_CONFIG_DIO_OUTPUT:
775 case INSN_CONFIG_DIO_INPUT:
776 case INSN_CONFIG_DISARM:
777 case INSN_CONFIG_RESET:
781 case INSN_CONFIG_ARM:
782 case INSN_CONFIG_DIO_QUERY:
783 case INSN_CONFIG_BLOCK_SIZE:
784 case INSN_CONFIG_FILTER:
785 case INSN_CONFIG_SERIAL_CLOCK:
786 case INSN_CONFIG_BIDIRECTIONAL_DATA:
787 case INSN_CONFIG_ALT_SOURCE:
788 case INSN_CONFIG_SET_COUNTER_MODE:
789 case INSN_CONFIG_8254_READ_STATUS:
790 case INSN_CONFIG_SET_ROUTING:
791 case INSN_CONFIG_GET_ROUTING:
792 case INSN_CONFIG_GET_PWM_STATUS:
793 case INSN_CONFIG_PWM_SET_PERIOD:
794 case INSN_CONFIG_PWM_GET_PERIOD:
798 case INSN_CONFIG_SET_GATE_SRC:
799 case INSN_CONFIG_GET_GATE_SRC:
800 case INSN_CONFIG_SET_CLOCK_SRC:
801 case INSN_CONFIG_GET_CLOCK_SRC:
802 case INSN_CONFIG_SET_OTHER_SRC:
803 case INSN_CONFIG_GET_COUNTER_STATUS:
804 case INSN_CONFIG_PWM_SET_H_BRIDGE:
805 case INSN_CONFIG_PWM_GET_H_BRIDGE:
806 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
810 case INSN_CONFIG_PWM_OUTPUT:
811 case INSN_CONFIG_ANALOG_TRIG:
815 //by default we allow the insn since we don't have checks for all possible cases yet
818 ("comedi: no check for data length of config insn id %i is implemented.\n"
819 " Add a check to %s in %s.\n"
820 " Assuming n=%i is correct.\n", data[0], __FUNCTION__,
828 static int parse_insn(comedi_device * dev, comedi_insn * insn, lsampl_t * data,
835 if (insn->insn & INSN_MASK_SPECIAL) {
836 /* a non-subdevice instruction */
838 switch (insn->insn) {
848 do_gettimeofday(&tv);
850 data[1] = tv.tv_usec;
856 if (insn->n != 1 || data[0] >= 100000) {
860 udelay(data[0] / 1000);
868 if (insn->subdev >= dev->n_subdevices) {
869 DPRINTK("%d not usable subdevice\n",
874 s = dev->subdevices + insn->subdev;
876 DPRINTK("no async\n");
880 if (!s->async->inttrig) {
881 DPRINTK("no inttrig\n");
885 ret = s->async->inttrig(dev, s, insn->data[0]);
890 DPRINTK("invalid insn\n");
895 /* a subdevice instruction */
898 if (insn->subdev >= dev->n_subdevices) {
899 DPRINTK("subdevice %d out of range\n", insn->subdev);
903 s = dev->subdevices + insn->subdev;
905 if (s->type == COMEDI_SUBD_UNUSED) {
906 DPRINTK("%d not usable subdevice\n", insn->subdev);
911 /* are we locked? (ioctl lock) */
912 if (s->lock && s->lock != file) {
913 DPRINTK("device locked\n");
918 if ((ret = check_chanlist(s, 1, &insn->chanspec)) < 0) {
920 DPRINTK("bad chanspec\n");
928 /* This looks arbitrary. It is. */
929 s->busy = &parse_insn;
930 switch (insn->insn) {
932 ret = s->insn_read(dev, s, insn, data);
935 maxdata = s->maxdata_list
936 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
938 for (i = 0; i < insn->n; ++i) {
939 if (data[i] > maxdata) {
941 DPRINTK("bad data value(s)\n");
946 ret = s->insn_write(dev, s, insn, data);
952 /* Most drivers ignore the base channel in
953 * insn->chanspec. Deal with it here if
954 * the subdevice has <= 32 channels. */
959 if (s->n_chan <= 32) {
960 shift = CR_CHAN(insn->chanspec);
969 ret = s->insn_bits(dev, s, insn, data);
976 ret = check_insn_config_length(insn, data);
979 ret = s->insn_config(dev, s, insn, data);
995 * synchronous instructions
1001 * comedi_insn struct at arg
1007 static int do_insn_ioctl(comedi_device * dev, comedi_insn __user *arg,
1011 lsampl_t *data = NULL;
1014 if (copy_from_user(&insn, arg, sizeof(comedi_insn))) {
1020 if (insn.n <= ULONG_MAX / sizeof(lsampl_t))
1021 data = kmalloc(sizeof(lsampl_t) * insn.n, GFP_KERNEL);
1028 if (insn.insn & INSN_MASK_WRITE) {
1029 if (copy_from_user(data, (lsampl_t __user *)insn.data,
1030 insn.n * sizeof(lsampl_t))) {
1035 ret = parse_insn(dev, &insn, data, file);
1038 if (insn.insn & INSN_MASK_READ) {
1039 if (copy_to_user((lsampl_t __user *)insn.data, data,
1040 insn.n * sizeof(lsampl_t))) {
1059 pointer to cmd structure
1062 cmd structure at arg
1066 modified cmd structure at arg
1069 static int do_cmd_ioctl(comedi_device * dev, comedi_cmd __user *arg, void *file)
1071 comedi_cmd user_cmd;
1072 comedi_subdevice *s;
1073 comedi_async *async;
1075 unsigned int *chanlist_saver = NULL;
1077 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
1078 DPRINTK("bad cmd address\n");
1081 // save user's chanlist pointer so it can be restored later
1082 chanlist_saver = user_cmd.chanlist;
1084 if (user_cmd.subdev >= dev->n_subdevices) {
1085 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1089 s = dev->subdevices + user_cmd.subdev;
1092 if (s->type == COMEDI_SUBD_UNUSED) {
1093 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1097 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1098 DPRINTK("subdevice %i does not support commands\n",
1103 /* are we locked? (ioctl lock) */
1104 if (s->lock && s->lock != file) {
1105 DPRINTK("subdevice locked\n");
1111 DPRINTK("subdevice busy\n");
1116 /* make sure channel/gain list isn't too long */
1117 if (user_cmd.chanlist_len > s->len_chanlist) {
1118 DPRINTK("channel/gain list too long %u > %d\n",
1119 user_cmd.chanlist_len, s->len_chanlist);
1124 /* make sure channel/gain list isn't too short */
1125 if (user_cmd.chanlist_len < 1) {
1126 DPRINTK("channel/gain list too short %u < 1\n",
1127 user_cmd.chanlist_len);
1132 if (async->cmd.chanlist)
1133 kfree(async->cmd.chanlist);
1134 async->cmd = user_cmd;
1135 async->cmd.data = NULL;
1136 /* load channel/gain list */
1137 async->cmd.chanlist =
1138 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1139 if (!async->cmd.chanlist) {
1140 DPRINTK("allocation failed\n");
1145 if (copy_from_user(async->cmd.chanlist,
1146 (unsigned int __user *)user_cmd.chanlist,
1147 async->cmd.chanlist_len * sizeof(int))) {
1148 DPRINTK("fault reading chanlist\n");
1153 /* make sure each element in channel/gain list is valid */
1154 if ((ret = check_chanlist(s, async->cmd.chanlist_len,
1155 async->cmd.chanlist)) < 0) {
1156 DPRINTK("bad chanlist\n");
1160 ret = s->do_cmdtest(dev, s, &async->cmd);
1162 if (async->cmd.flags & TRIG_BOGUS || ret) {
1163 DPRINTK("test returned %d\n", ret);
1164 user_cmd = async->cmd;
1165 // restore chanlist pointer before copying back
1166 user_cmd.chanlist = chanlist_saver;
1167 user_cmd.data = NULL;
1168 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1169 DPRINTK("fault writing cmd\n");
1177 if (!async->prealloc_bufsz) {
1179 DPRINTK("no buffer (?)\n");
1183 comedi_reset_async_buf(async);
1186 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1188 if (async->cmd.flags & TRIG_WAKE_EOS) {
1189 async->cb_mask |= COMEDI_CB_EOS;
1192 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1194 #ifdef CONFIG_COMEDI_RT
1195 if (async->cmd.flags & TRIG_RT) {
1196 if (comedi_switch_to_rt(dev) == 0)
1197 comedi_set_subdevice_runflags(s, SRF_RT, SRF_RT);
1201 ret = s->do_cmd(dev, s);
1206 do_become_nonbusy(dev, s);
1213 command testing ioctl
1216 pointer to cmd structure
1219 cmd structure at arg
1223 modified cmd structure at arg
1226 static int do_cmdtest_ioctl(comedi_device * dev, comedi_cmd __user *arg,
1229 comedi_cmd user_cmd;
1230 comedi_subdevice *s;
1232 unsigned int *chanlist = NULL;
1233 unsigned int *chanlist_saver = NULL;
1235 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
1236 DPRINTK("bad cmd address\n");
1239 // save user's chanlist pointer so it can be restored later
1240 chanlist_saver = user_cmd.chanlist;
1242 if (user_cmd.subdev >= dev->n_subdevices) {
1243 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1247 s = dev->subdevices + user_cmd.subdev;
1248 if (s->type == COMEDI_SUBD_UNUSED) {
1249 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1253 if (!s->do_cmd || !s->do_cmdtest) {
1254 DPRINTK("subdevice %i does not support commands\n",
1259 /* make sure channel/gain list isn't too long */
1260 if (user_cmd.chanlist_len > s->len_chanlist) {
1261 DPRINTK("channel/gain list too long %d > %d\n",
1262 user_cmd.chanlist_len, s->len_chanlist);
1267 /* load channel/gain list */
1268 if (user_cmd.chanlist) {
1270 kmalloc(user_cmd.chanlist_len * sizeof(int),
1273 DPRINTK("allocation failed\n");
1278 if (copy_from_user(chanlist,
1279 (unsigned int __user *)user_cmd.chanlist,
1280 user_cmd.chanlist_len * sizeof(int))) {
1281 DPRINTK("fault reading chanlist\n");
1286 /* make sure each element in channel/gain list is valid */
1287 if ((ret = check_chanlist(s, user_cmd.chanlist_len,
1289 DPRINTK("bad chanlist\n");
1293 user_cmd.chanlist = chanlist;
1296 ret = s->do_cmdtest(dev, s, &user_cmd);
1298 // restore chanlist pointer before copying back
1299 user_cmd.chanlist = chanlist_saver;
1301 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1302 DPRINTK("bad cmd address\n");
1328 static int do_lock_ioctl(comedi_device * dev, unsigned int arg, void *file)
1331 unsigned long flags;
1332 comedi_subdevice *s;
1334 if (arg >= dev->n_subdevices)
1336 s = dev->subdevices + arg;
1338 comedi_spin_lock_irqsave(&s->spin_lock, flags);
1339 if (s->busy || s->lock) {
1344 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
1351 ret = s->lock_f(dev, s);
1370 This function isn't protected by the semaphore, since
1371 we already own the lock.
1373 static int do_unlock_ioctl(comedi_device * dev, unsigned int arg, void *file)
1375 comedi_subdevice *s;
1377 if (arg >= dev->n_subdevices)
1379 s = dev->subdevices + arg;
1384 if (s->lock && s->lock != file)
1387 if (s->lock == file) {
1401 cancel acquisition ioctl
1413 static int do_cancel_ioctl(comedi_device * dev, unsigned int arg, void *file)
1415 comedi_subdevice *s;
1417 if (arg >= dev->n_subdevices)
1419 s = dev->subdevices + arg;
1420 if (s->async == NULL)
1423 if (s->lock && s->lock != file)
1429 if (s->busy != file)
1432 return do_cancel(dev, s);
1437 instructs driver to synchronize buffers
1449 static int do_poll_ioctl(comedi_device * dev, unsigned int arg, void *file)
1451 comedi_subdevice *s;
1453 if (arg >= dev->n_subdevices)
1455 s = dev->subdevices + arg;
1457 if (s->lock && s->lock != file)
1463 if (s->busy != file)
1467 return s->poll(dev, s);
1472 static int do_cancel(comedi_device * dev, comedi_subdevice * s)
1476 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1477 ret = s->cancel(dev, s);
1479 do_become_nonbusy(dev, s);
1484 void comedi_vm_open(struct vm_area_struct *area)
1486 comedi_async *async;
1489 async = area->vm_private_data;
1490 dev = async->subdevice->device;
1492 mutex_lock(&dev->mutex);
1493 async->mmap_count++;
1494 mutex_unlock(&dev->mutex);
1497 void comedi_vm_close(struct vm_area_struct *area)
1499 comedi_async *async;
1502 async = area->vm_private_data;
1503 dev = async->subdevice->device;
1505 mutex_lock(&dev->mutex);
1506 async->mmap_count--;
1507 mutex_unlock(&dev->mutex);
1510 static struct vm_operations_struct comedi_vm_ops = {
1511 .open = comedi_vm_open,
1512 .close = comedi_vm_close,
1515 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1517 const unsigned minor = iminor(file->f_dentry->d_inode);
1518 comedi_async *async = NULL;
1519 unsigned long start = vma->vm_start;
1524 comedi_subdevice *s;
1525 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1527 if (dev_file_info==NULL) return -ENODEV;
1528 dev = dev_file_info->device;
1529 if (dev==NULL) return -ENODEV;
1531 mutex_lock(&dev->mutex);
1532 if (!dev->attached) {
1533 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1537 if (vma->vm_flags & VM_WRITE) {
1538 s = comedi_get_write_subdevice(dev_file_info);
1540 s = comedi_get_read_subdevice(dev_file_info);
1547 if (async == NULL) {
1552 if (vma->vm_pgoff != 0) {
1553 DPRINTK("comedi: mmap() offset must be 0.\n");
1558 size = vma->vm_end - vma->vm_start;
1559 if (size > async->prealloc_bufsz) {
1563 if (size & (~PAGE_MASK)) {
1568 n_pages = size >> PAGE_SHIFT;
1569 for (i = 0; i < n_pages; ++i) {
1570 if (remap_pfn_range(vma, start,
1571 page_to_pfn(virt_to_page(async->
1572 buf_page_list[i].virt_addr)),
1573 PAGE_SIZE, PAGE_SHARED)) {
1580 vma->vm_ops = &comedi_vm_ops;
1581 vma->vm_private_data = async;
1583 async->mmap_count++;
1587 mutex_unlock(&dev->mutex);
1591 static unsigned int comedi_poll(struct file *file, poll_table * wait)
1593 unsigned int mask = 0;
1594 const unsigned minor = iminor(file->f_dentry->d_inode);
1595 comedi_subdevice *read_subdev;
1596 comedi_subdevice *write_subdev;
1597 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1599 if (dev_file_info==NULL) return -ENODEV;
1600 dev = dev_file_info->device;
1601 if (dev==NULL) return -ENODEV;
1603 mutex_lock(&dev->mutex);
1604 if (!dev->attached) {
1605 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1606 mutex_unlock(&dev->mutex);
1611 read_subdev = comedi_get_read_subdevice(dev_file_info);
1613 poll_wait(file, &read_subdev->async->wait_head, wait);
1614 if (!read_subdev->busy
1615 || comedi_buf_read_n_available(read_subdev->async) > 0
1616 || !(comedi_get_subdevice_runflags(read_subdev) &
1618 mask |= POLLIN | POLLRDNORM;
1621 write_subdev = comedi_get_write_subdevice(dev_file_info);
1623 poll_wait(file, &write_subdev->async->wait_head, wait);
1624 comedi_buf_write_alloc(write_subdev->async, write_subdev->async->prealloc_bufsz);
1625 if (!write_subdev->busy
1626 || !(comedi_get_subdevice_runflags(write_subdev) &
1628 || comedi_buf_write_n_allocated(write_subdev->async) >=
1629 bytes_per_sample(write_subdev->async->subdevice)) {
1630 mask |= POLLOUT | POLLWRNORM;
1634 mutex_unlock(&dev->mutex);
1638 static ssize_t comedi_write(struct file *file, const char __user *buf,
1639 size_t nbytes, loff_t * offset)
1641 comedi_subdevice *s;
1642 comedi_async *async;
1643 int n, m, count = 0, retval = 0;
1644 DECLARE_WAITQUEUE(wait, current);
1645 const unsigned minor = iminor(file->f_dentry->d_inode);
1646 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1648 if (dev_file_info==NULL) return -ENODEV;
1649 dev = dev_file_info->device;
1650 if (dev==NULL) return -ENODEV;
1652 if (!dev->attached) {
1653 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1658 s = comedi_get_write_subdevice(dev_file_info);
1673 if (s->busy != file) {
1677 add_wait_queue(&async->wait_head, &wait);
1678 while (nbytes > 0 && !retval) {
1679 set_current_state(TASK_INTERRUPTIBLE);
1681 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1683 if (comedi_get_subdevice_runflags(s) &
1689 do_become_nonbusy(dev, s);
1697 if (async->buf_write_ptr + m > async->prealloc_bufsz) {
1698 m = async->prealloc_bufsz - async->buf_write_ptr;
1700 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1701 if (m > comedi_buf_write_n_allocated(async)) {
1702 m = comedi_buf_write_n_allocated(async);
1708 if (file->f_flags & O_NONBLOCK) {
1713 if (signal_pending(current)) {
1714 retval = -ERESTARTSYS;
1720 if (s->busy != file) {
1727 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1733 comedi_buf_write_free(async, n);
1739 break; /* makes device work like a pipe */
1741 set_current_state(TASK_RUNNING);
1742 remove_wait_queue(&async->wait_head, &wait);
1745 return (count ? count : retval);
1748 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
1751 comedi_subdevice *s;
1752 comedi_async *async;
1753 int n, m, count = 0, retval = 0;
1754 DECLARE_WAITQUEUE(wait, current);
1755 const unsigned minor = iminor(file->f_dentry->d_inode);
1756 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1758 if (dev_file_info==NULL) return -ENODEV;
1759 dev = dev_file_info->device;
1760 if (dev==NULL) return -ENODEV;
1762 if (!dev->attached) {
1763 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1768 s = comedi_get_read_subdevice(dev_file_info);
1782 if (s->busy != file) {
1787 add_wait_queue(&async->wait_head, &wait);
1788 while (nbytes > 0 && !retval) {
1789 set_current_state(TASK_INTERRUPTIBLE);
1793 m = comedi_buf_read_n_available(async);
1794 //printk("%d available\n",m);
1795 if (async->buf_read_ptr + m > async->prealloc_bufsz) {
1796 m = async->prealloc_bufsz - async->buf_read_ptr;
1798 //printk("%d contiguous\n",m);
1803 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1804 do_become_nonbusy(dev, s);
1805 if (comedi_get_subdevice_runflags(s) &
1813 if (file->f_flags & O_NONBLOCK) {
1818 if (signal_pending(current)) {
1819 retval = -ERESTARTSYS;
1826 if (s->busy != file) {
1832 m = copy_to_user(buf, async->prealloc_buf +
1833 async->buf_read_ptr, n);
1839 comedi_buf_read_alloc(async, n);
1840 comedi_buf_read_free(async, n);
1846 break; /* makes device work like a pipe */
1848 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
1849 async->buf_read_count - async->buf_write_count == 0) {
1850 do_become_nonbusy(dev, s);
1852 set_current_state(TASK_RUNNING);
1853 remove_wait_queue(&async->wait_head, &wait);
1856 return (count ? count : retval);
1860 This function restores a subdevice to an idle state.
1862 void do_become_nonbusy(comedi_device * dev, comedi_subdevice * s)
1864 comedi_async *async = s->async;
1866 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1867 #ifdef CONFIG_COMEDI_RT
1868 if (comedi_get_subdevice_runflags(s) & SRF_RT) {
1869 comedi_switch_to_non_rt(dev);
1870 comedi_set_subdevice_runflags(s, SRF_RT, 0);
1874 comedi_reset_async_buf(async);
1875 async->inttrig = NULL;
1877 printk("BUG: (?) do_become_nonbusy called with async=0\n");
1883 static int comedi_open(struct inode *inode, struct file *file)
1885 const unsigned minor = iminor(inode);
1886 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1887 comedi_device *dev = dev_file_info ? dev_file_info->device : NULL;
1889 DPRINTK("invalid minor number\n");
1893 /* This is slightly hacky, but we want module autoloading
1895 * case: user opens device, attached -> ok
1896 * case: user opens device, unattached, in_request_module=0 -> autoload
1897 * case: user opens device, unattached, in_request_module=1 -> fail
1898 * case: root opens device, attached -> ok
1899 * case: root opens device, unattached, in_request_module=1 -> ok
1900 * (typically called from modprobe)
1901 * case: root opens device, unattached, in_request_module=0 -> autoload
1903 * The last could be changed to "-> ok", which would deny root
1906 mutex_lock(&dev->mutex);
1909 if (!capable(CAP_SYS_MODULE) && dev->in_request_module) {
1910 DPRINTK("in request module\n");
1911 mutex_unlock(&dev->mutex);
1914 if (capable(CAP_SYS_MODULE) && dev->in_request_module)
1917 dev->in_request_module = 1;
1920 mutex_unlock(&dev->mutex);
1921 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1922 mutex_lock(&dev->mutex);
1925 dev->in_request_module = 0;
1927 if (!dev->attached && !capable(CAP_SYS_MODULE)) {
1928 DPRINTK("not attached and not CAP_SYS_MODULE\n");
1929 mutex_unlock(&dev->mutex);
1933 __module_get(THIS_MODULE);
1935 if (dev->attached) {
1936 if (!try_module_get(dev->driver->module)) {
1937 module_put(THIS_MODULE);
1938 mutex_unlock(&dev->mutex);
1943 if (dev->attached && dev->use_count == 0 && dev->open) {
1944 int rc = dev->open(dev);
1947 module_put(dev->driver->module);
1948 module_put(THIS_MODULE);
1949 mutex_unlock(&dev->mutex);
1956 mutex_unlock(&dev->mutex);
1961 static int comedi_close(struct inode *inode, struct file *file)
1963 const unsigned minor = iminor(inode);
1964 comedi_subdevice *s = NULL;
1966 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1968 if (dev_file_info==NULL) return -ENODEV;
1969 dev = dev_file_info->device;
1970 if (dev==NULL) return -ENODEV;
1972 mutex_lock(&dev->mutex);
1974 if (dev->subdevices) {
1975 for (i = 0; i < dev->n_subdevices; i++) {
1976 s = dev->subdevices + i;
1978 if (s->busy == file) {
1981 if (s->lock == file) {
1986 if (dev->attached && dev->use_count == 1 && dev->close) {
1990 module_put(THIS_MODULE);
1991 if (dev->attached) {
1992 module_put(dev->driver->module);
1997 mutex_unlock(&dev->mutex);
1999 if (file->f_flags & FASYNC) {
2000 comedi_fasync(-1, file, 0);
2006 static int comedi_fasync(int fd, struct file *file, int on)
2008 const unsigned minor = iminor(file->f_dentry->d_inode);
2009 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
2011 if (dev_file_info==NULL) return -ENODEV;
2012 dev = dev_file_info->device;
2013 if (dev==NULL) return -ENODEV;
2015 return fasync_helper(fd, file, on, &dev->async_queue);
2018 const struct file_operations comedi_fops = {
2020 #ifdef HAVE_UNLOCKED_IOCTL
2021 unlocked_ioctl:comedi_unlocked_ioctl,
2025 #ifdef HAVE_COMPAT_IOCTL
2026 compat_ioctl:comedi_compat_ioctl,
2029 release:comedi_close,
2034 fasync:comedi_fasync,
2037 struct class *comedi_class = NULL;
2038 static struct cdev comedi_cdev;
2040 static void comedi_cleanup_legacy_minors(void)
2043 for (i = 0; i < comedi_num_legacy_minors; i++) {
2044 comedi_free_board_minor(i);
2048 static int __init comedi_init(void)
2053 printk("comedi: version " COMEDI_RELEASE
2054 " - http://www.comedi.org\n");
2056 if(comedi_num_legacy_minors < 0 || comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS)
2058 printk("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid "
2059 "values are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
2062 /* comedi is unusable if both comedi_autoconfig and comedi_num_legacy_minors are zero,
2063 so we might as well adjust the defaults in that case */
2064 if(comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2066 comedi_num_legacy_minors = 16;
2069 memset(comedi_file_info_table, 0, sizeof(struct comedi_device_file_info*) * COMEDI_NUM_MINORS);
2071 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2072 COMEDI_NUM_MINORS, "comedi");
2075 cdev_init(&comedi_cdev, &comedi_fops);
2076 comedi_cdev.owner = THIS_MODULE;
2077 kobject_set_name(&comedi_cdev.kobj, "comedi");
2078 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2079 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2083 comedi_class = class_create(THIS_MODULE, "comedi");
2084 if (IS_ERR(comedi_class)) {
2085 printk("comedi: failed to create class");
2086 cdev_del(&comedi_cdev);
2087 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2089 return PTR_ERR(comedi_class);
2092 /* XXX requires /proc interface */
2095 // create devices files for legacy/manual use
2096 for (i = 0; i < comedi_num_legacy_minors; i++) {
2098 minor = comedi_alloc_board_minor(NULL);
2101 comedi_cleanup_legacy_minors();
2102 cdev_del(&comedi_cdev);
2103 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2111 comedi_register_ioctl32();
2116 static void __exit comedi_cleanup(void)
2120 comedi_cleanup_legacy_minors();
2121 for(i = 0; i < COMEDI_NUM_MINORS; ++i)
2123 BUG_ON(comedi_file_info_table[i]);
2126 class_destroy(comedi_class);
2127 cdev_del(&comedi_cdev);
2128 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2130 comedi_proc_cleanup();
2132 comedi_rt_cleanup();
2134 comedi_unregister_ioctl32();
2137 module_init(comedi_init);
2138 module_exit(comedi_cleanup);
2140 void comedi_error(const comedi_device * dev, const char *s)
2142 rt_printk("comedi%d: %s: %s\n", dev->minor, dev->driver->driver_name,
2146 void comedi_event(comedi_device * dev, comedi_subdevice * s)
2148 comedi_async *async = s->async;
2149 unsigned runflags = 0;
2150 unsigned runflags_mask = 0;
2152 //DPRINTK("comedi_event 0x%x\n",mask);
2154 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2158 events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW))
2160 runflags_mask |= SRF_RUNNING;
2162 /* remember if an error event has occured, so an error
2163 * can be returned the next time the user does a read() */
2164 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2165 runflags_mask |= SRF_ERROR;
2166 runflags |= SRF_ERROR;
2168 if (runflags_mask) {
2169 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2170 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2173 if (async->cb_mask & s->async->events) {
2174 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2177 #ifdef CONFIG_COMEDI_RT
2179 comedi_rt_pend_wakeup(&async->wait_head);
2181 printk("BUG: comedi_event() code unreachable\n");
2184 wake_up_interruptible(&async->wait_head);
2185 if (s->subdev_flags & SDF_CMD_READ) {
2186 kill_fasync(&dev->async_queue, SIGIO,
2189 if (s->subdev_flags & SDF_CMD_WRITE) {
2190 kill_fasync(&dev->async_queue, SIGIO,
2196 async->cb_func(s->async->events, async->cb_arg);
2197 /* XXX bug here. If subdevice A is rt, and
2198 * subdevice B tries to callback to a normal
2199 * linux kernel function, it will be at the
2200 * wrong priority. Since this isn't very
2201 * common, I'm not going to worry about it. */
2204 s->async->events = 0;
2207 void comedi_set_subdevice_runflags(comedi_subdevice * s, unsigned mask,
2210 unsigned long flags;
2212 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2213 s->runflags &= ~mask;
2214 s->runflags |= (bits & mask);
2215 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2218 unsigned comedi_get_subdevice_runflags(comedi_subdevice * s)
2220 unsigned long flags;
2223 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2224 runflags = s->runflags;
2225 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2229 static int is_device_busy(comedi_device * dev)
2231 comedi_subdevice *s;
2237 for (i = 0; i < dev->n_subdevices; i++) {
2238 s = dev->subdevices + i;
2241 if (s->async && s->async->mmap_count)
2248 void comedi_device_init(comedi_device *dev)
2250 memset(dev, 0, sizeof(comedi_device));
2251 spin_lock_init(&dev->spinlock);
2252 mutex_init(&dev->mutex);
2256 void comedi_device_cleanup(comedi_device *dev)
2258 if(dev == NULL) return;
2259 mutex_lock(&dev->mutex);
2260 comedi_device_detach(dev);
2261 mutex_unlock(&dev->mutex);
2262 mutex_destroy(&dev->mutex);
2265 int comedi_alloc_board_minor(struct device *hardware_device)
2267 unsigned long flags;
2268 struct comedi_device_file_info *info;
2269 comedi_device_create_t *csdev;
2273 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2274 if(info == NULL) return -ENOMEM;
2275 info->device = kzalloc(sizeof(comedi_device), GFP_KERNEL);
2276 if(info->device == NULL)
2281 comedi_device_init(info->device);
2282 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2283 for(i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2285 if(comedi_file_info_table[i] == NULL)
2287 comedi_file_info_table[i] = info;
2291 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2292 if(i == COMEDI_NUM_BOARD_MINORS)
2294 comedi_device_cleanup(info->device);
2295 kfree(info->device);
2297 printk("comedi: error: ran out of minor numbers for board device files.\n");
2300 info->device->minor = i;
2301 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
2302 MKDEV(COMEDI_MAJOR, i), NULL, hardware_device, "comedi%i", i);
2303 if(!IS_ERR(csdev)) {
2304 info->device->class_dev = csdev;
2306 COMEDI_DEV_SET_DRVDATA(csdev, info);
2307 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_read_buffer_kb);
2310 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_read_buffer_kb.attr.name);
2311 comedi_free_board_minor(i);
2314 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_read_buffer_kb);
2317 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_read_buffer_kb.attr.name);
2318 comedi_free_board_minor(i);
2321 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_write_buffer_kb);
2324 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_write_buffer_kb.attr.name);
2325 comedi_free_board_minor(i);
2328 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_write_buffer_kb);
2331 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_write_buffer_kb.attr.name);
2332 comedi_free_board_minor(i);
2338 void comedi_free_board_minor(unsigned minor)
2340 unsigned long flags;
2341 struct comedi_device_file_info *info;
2343 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2344 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2345 info = comedi_file_info_table[minor];
2346 comedi_file_info_table[minor] = NULL;
2347 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2351 comedi_device *dev = info->device;
2356 COMEDI_DEVICE_DESTROY(comedi_class,
2357 MKDEV(COMEDI_MAJOR, dev->minor));
2359 comedi_device_cleanup(dev);
2366 int comedi_alloc_subdevice_minor(comedi_device *dev, comedi_subdevice *s)
2368 unsigned long flags;
2369 struct comedi_device_file_info *info;
2370 comedi_device_create_t *csdev;
2374 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2375 if(info == NULL) return -ENOMEM;
2377 info->read_subdevice = s;
2378 info->write_subdevice = s;
2379 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2380 for(i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i)
2382 if(comedi_file_info_table[i] == NULL)
2384 comedi_file_info_table[i] = info;
2388 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2389 if(i == COMEDI_NUM_MINORS)
2392 printk("comedi: error: ran out of minor numbers for board device files.\n");
2396 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
2397 MKDEV(COMEDI_MAJOR, i), NULL, NULL, "comedi%i_subd%i", dev->minor, (int)(s - dev->subdevices));
2400 s->class_dev = csdev;
2402 COMEDI_DEV_SET_DRVDATA(csdev, info);
2403 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_read_buffer_kb);
2406 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_read_buffer_kb.attr.name);
2407 comedi_free_subdevice_minor(s);
2410 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_read_buffer_kb);
2413 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_read_buffer_kb.attr.name);
2414 comedi_free_subdevice_minor(s);
2417 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_write_buffer_kb);
2420 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_write_buffer_kb.attr.name);
2421 comedi_free_subdevice_minor(s);
2424 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_write_buffer_kb);
2427 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_write_buffer_kb.attr.name);
2428 comedi_free_subdevice_minor(s);
2434 void comedi_free_subdevice_minor(comedi_subdevice *s)
2436 unsigned long flags;
2437 struct comedi_device_file_info *info;
2439 if(s == NULL) return;
2440 if(s->minor < 0) return;
2442 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2443 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2445 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2446 info = comedi_file_info_table[s->minor];
2447 comedi_file_info_table[s->minor] = NULL;
2448 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2452 COMEDI_DEVICE_DESTROY(comedi_class,
2453 MKDEV(COMEDI_MAJOR, s->minor));
2454 s->class_dev = NULL;
2459 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2461 unsigned long flags;
2462 struct comedi_device_file_info *info;
2464 BUG_ON(minor >= COMEDI_NUM_MINORS);
2465 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2466 info = comedi_file_info_table[minor];
2467 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2471 static int resize_async_buffer(comedi_device *dev,
2472 comedi_subdevice *s, comedi_async *async, unsigned new_size)
2476 if (new_size > async->max_bufsize)
2480 DPRINTK("subdevice is busy, cannot resize buffer\n");
2483 if (async->mmap_count) {
2484 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2488 if (!async->prealloc_buf)
2491 /* make sure buffer is an integral number of pages
2493 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2495 retval = comedi_buf_alloc(dev, s, new_size);
2499 if (s->buf_change) {
2500 retval = s->buf_change(dev, s, new_size);
2505 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2506 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
2510 // sysfs attribute file functions
2512 static const unsigned bytes_per_kibi = 1024;
2514 static COMEDI_DECLARE_ATTR_SHOW(show_max_read_buffer_kb, dev, buf)
2517 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2518 unsigned max_buffer_size_kb = 0;
2519 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2521 mutex_lock(&info->device->mutex);
2522 if(read_subdevice &&
2523 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2524 read_subdevice->async)
2526 max_buffer_size_kb = read_subdevice->async->max_bufsize / bytes_per_kibi;
2528 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2529 mutex_unlock(&info->device->mutex);
2534 static COMEDI_DECLARE_ATTR_STORE(store_max_read_buffer_kb, dev, buf, count)
2536 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2537 unsigned long new_max_size_kb;
2538 uint64_t new_max_size;
2539 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2541 if(strict_strtoul(buf, 10, &new_max_size_kb))
2545 if(new_max_size_kb != (uint32_t)new_max_size_kb) return -EINVAL;
2546 new_max_size = ((uint64_t)new_max_size_kb) * bytes_per_kibi;
2547 if(new_max_size != (uint32_t)new_max_size) return -EINVAL;
2549 mutex_lock(&info->device->mutex);
2550 if(read_subdevice == NULL ||
2551 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2552 read_subdevice->async == NULL)
2554 mutex_unlock(&info->device->mutex);
2557 read_subdevice->async->max_bufsize = new_max_size;
2558 mutex_unlock(&info->device->mutex);
2563 static COMEDI_DECLARE_ATTR_SHOW(show_read_buffer_kb, dev, buf)
2566 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2567 unsigned buffer_size_kb = 0;
2568 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2570 mutex_lock(&info->device->mutex);
2571 if(read_subdevice &&
2572 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2573 read_subdevice->async)
2575 buffer_size_kb = read_subdevice->async->prealloc_bufsz / bytes_per_kibi;
2577 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2578 mutex_unlock(&info->device->mutex);
2583 static COMEDI_DECLARE_ATTR_STORE(store_read_buffer_kb, dev, buf, count)
2585 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2586 unsigned long new_size_kb;
2589 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2591 if(strict_strtoul(buf, 10, &new_size_kb))
2595 if(new_size_kb != (uint32_t)new_size_kb) return -EINVAL;
2596 new_size = ((uint64_t)new_size_kb) * bytes_per_kibi;
2597 if(new_size != (uint32_t)new_size) return -EINVAL;
2599 mutex_lock(&info->device->mutex);
2600 if(read_subdevice == NULL ||
2601 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2602 read_subdevice->async == NULL)
2604 mutex_unlock(&info->device->mutex);
2607 retval = resize_async_buffer(info->device, read_subdevice,
2608 read_subdevice->async, new_size);
2609 mutex_unlock(&info->device->mutex);
2611 if(retval < 0) return retval;
2615 static COMEDI_DECLARE_ATTR_SHOW(show_max_write_buffer_kb, dev, buf)
2618 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2619 unsigned max_buffer_size_kb = 0;
2620 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2622 mutex_lock(&info->device->mutex);
2623 if(write_subdevice &&
2624 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2625 write_subdevice->async)
2627 max_buffer_size_kb = write_subdevice->async->max_bufsize / bytes_per_kibi;
2629 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2630 mutex_unlock(&info->device->mutex);
2635 static COMEDI_DECLARE_ATTR_STORE(store_max_write_buffer_kb, dev, buf, count)
2637 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2638 unsigned long new_max_size_kb;
2639 uint64_t new_max_size;
2640 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2642 if(strict_strtoul(buf, 10, &new_max_size_kb))
2646 if(new_max_size_kb != (uint32_t)new_max_size_kb) return -EINVAL;
2647 new_max_size = ((uint64_t)new_max_size_kb) * bytes_per_kibi;
2648 if(new_max_size != (uint32_t)new_max_size) return -EINVAL;
2650 mutex_lock(&info->device->mutex);
2651 if(write_subdevice == NULL ||
2652 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2653 write_subdevice->async == NULL)
2655 mutex_unlock(&info->device->mutex);
2658 write_subdevice->async->max_bufsize = new_max_size;
2659 mutex_unlock(&info->device->mutex);
2664 static COMEDI_DECLARE_ATTR_SHOW(show_write_buffer_kb, dev, buf)
2667 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2668 unsigned buffer_size_kb = 0;
2669 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2671 mutex_lock(&info->device->mutex);
2672 if(write_subdevice &&
2673 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2674 write_subdevice->async)
2676 buffer_size_kb = write_subdevice->async->prealloc_bufsz / bytes_per_kibi;
2678 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2679 mutex_unlock(&info->device->mutex);
2684 static COMEDI_DECLARE_ATTR_STORE(store_write_buffer_kb, dev, buf, count)
2686 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2687 unsigned long new_size_kb;
2690 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2692 if(strict_strtoul(buf, 10, &new_size_kb))
2696 if(new_size_kb != (uint32_t)new_size_kb) return -EINVAL;
2697 new_size = ((uint64_t)new_size_kb) * bytes_per_kibi;
2698 if(new_size != (uint32_t)new_size) return -EINVAL;
2700 mutex_lock(&info->device->mutex);
2701 if(write_subdevice == NULL ||
2702 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2703 write_subdevice->async == NULL)
2705 mutex_unlock(&info->device->mutex);
2708 retval = resize_async_buffer(info->device, write_subdevice,
2709 write_subdevice->async, new_size);
2710 mutex_unlock(&info->device->mutex);
2712 if(retval < 0) return retval;