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 "comedidev.h"
46 #include <linux/cdev.h>
47 #include <linux/stat.h>
50 #include <linux/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 EXPORT_SYMBOL(comedi_debug);
61 module_param(comedi_debug, int, 0644);
64 int comedi_autoconfig = 1;
65 module_param(comedi_autoconfig, bool, 0444);
67 static int comedi_num_legacy_minors;
68 module_param(comedi_num_legacy_minors, int, 0444);
70 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
71 static struct comedi_device_file_info
72 *comedi_file_info_table[COMEDI_NUM_MINORS];
74 static int do_devconfig_ioctl(struct comedi_device *dev,
75 struct comedi_devconfig __user *arg);
76 static int do_bufconfig_ioctl(struct comedi_device *dev,
77 struct comedi_bufconfig __user *arg);
78 static int do_devinfo_ioctl(struct comedi_device *dev,
79 struct comedi_devinfo __user *arg,
81 static int do_subdinfo_ioctl(struct comedi_device *dev,
82 struct comedi_subdinfo __user *arg, void *file);
83 static int do_chaninfo_ioctl(struct comedi_device *dev,
84 struct comedi_chaninfo __user *arg);
85 static int do_bufinfo_ioctl(struct comedi_device *dev,
86 struct comedi_bufinfo __user *arg, void *file);
87 static int do_cmd_ioctl(struct comedi_device *dev,
88 struct comedi_cmd __user *arg, void *file);
89 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
91 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
93 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
95 static int do_cmdtest_ioctl(struct comedi_device *dev,
96 struct comedi_cmd __user *arg, void *file);
97 static int do_insnlist_ioctl(struct comedi_device *dev,
98 struct comedi_insnlist __user *arg, void *file);
99 static int do_insn_ioctl(struct comedi_device *dev,
100 struct comedi_insn __user *arg, void *file);
101 static int do_poll_ioctl(struct comedi_device *dev, unsigned int subd,
104 static void do_become_nonbusy(struct comedi_device *dev,
105 struct comedi_subdevice *s);
106 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
108 static int comedi_fasync(int fd, struct file *file, int on);
110 static int is_device_busy(struct comedi_device *dev);
111 static int resize_async_buffer(struct comedi_device *dev,
112 struct comedi_subdevice *s,
113 struct comedi_async *async, unsigned new_size);
115 /* declarations for sysfs attribute files */
116 static struct device_attribute dev_attr_max_read_buffer_kb;
117 static struct device_attribute dev_attr_read_buffer_kb;
118 static struct device_attribute dev_attr_max_write_buffer_kb;
119 static struct device_attribute dev_attr_write_buffer_kb;
121 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
124 const unsigned minor = iminor(file->f_dentry->d_inode);
125 struct comedi_device_file_info *dev_file_info =
126 comedi_get_device_file_info(minor);
127 struct comedi_device *dev;
130 if (dev_file_info == NULL || dev_file_info->device == NULL)
132 dev = dev_file_info->device;
134 mutex_lock(&dev->mutex);
136 /* Device config is special, because it must work on
137 * an unconfigured device. */
138 if (cmd == COMEDI_DEVCONFIG) {
139 if (minor >= COMEDI_NUM_BOARD_MINORS) {
140 /* Device config not appropriate on non-board minors. */
144 rc = do_devconfig_ioctl(dev,
145 (struct comedi_devconfig __user *)arg);
147 /* Evade comedi_auto_unconfig(). */
148 dev_file_info->hardware_device = NULL;
152 if (!dev->attached) {
153 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
159 case COMEDI_BUFCONFIG:
160 rc = do_bufconfig_ioctl(dev,
161 (struct comedi_bufconfig __user *)arg);
164 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
167 case COMEDI_SUBDINFO:
168 rc = do_subdinfo_ioctl(dev,
169 (struct comedi_subdinfo __user *)arg,
172 case COMEDI_CHANINFO:
173 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
175 case COMEDI_RANGEINFO:
176 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
179 rc = do_bufinfo_ioctl(dev,
180 (struct comedi_bufinfo __user *)arg,
184 rc = do_lock_ioctl(dev, arg, file);
187 rc = do_unlock_ioctl(dev, arg, file);
190 rc = do_cancel_ioctl(dev, arg, file);
193 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
196 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
199 case COMEDI_INSNLIST:
200 rc = do_insnlist_ioctl(dev,
201 (struct comedi_insnlist __user *)arg,
205 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
209 rc = do_poll_ioctl(dev, arg, file);
217 mutex_unlock(&dev->mutex);
226 pointer to devconfig structure
229 devconfig structure at arg
234 static int do_devconfig_ioctl(struct comedi_device *dev,
235 struct comedi_devconfig __user *arg)
237 struct comedi_devconfig it;
239 unsigned char *aux_data = NULL;
242 if (!capable(CAP_SYS_ADMIN))
246 if (is_device_busy(dev))
249 struct module *driver_module = dev->driver->module;
250 comedi_device_detach(dev);
251 module_put(driver_module);
256 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
259 it.board_name[COMEDI_NAMELEN - 1] = 0;
261 if (comedi_aux_data(it.options, 0) &&
262 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
264 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
268 aux_data = vmalloc(aux_len);
272 if (copy_from_user(aux_data,
273 comedi_aux_data(it.options, 0), aux_len)) {
277 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
278 (unsigned long)aux_data;
279 if (sizeof(void *) > sizeof(int)) {
280 bit_shift = sizeof(int) * 8;
281 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
282 ((unsigned long)aux_data) >> bit_shift;
284 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
287 ret = comedi_device_attach(dev, &it);
289 if (!try_module_get(dev->driver->module)) {
290 comedi_device_detach(dev);
303 buffer configuration ioctl
306 pointer to bufconfig structure
312 modified bufconfig at arg
315 static int do_bufconfig_ioctl(struct comedi_device *dev,
316 struct comedi_bufconfig __user *arg)
318 struct comedi_bufconfig bc;
319 struct comedi_async *async;
320 struct comedi_subdevice *s;
323 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
326 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
329 s = dev->subdevices + bc.subdevice;
333 DPRINTK("subdevice does not have async capability\n");
339 if (bc.maximum_size) {
340 if (!capable(CAP_SYS_ADMIN))
343 async->max_bufsize = bc.maximum_size;
347 retval = resize_async_buffer(dev, s, async, bc.size);
352 bc.size = async->prealloc_bufsz;
353 bc.maximum_size = async->max_bufsize;
356 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
367 pointer to devinfo structure
376 static int do_devinfo_ioctl(struct comedi_device *dev,
377 struct comedi_devinfo __user *arg,
380 struct comedi_devinfo devinfo;
381 const unsigned minor = iminor(file->f_dentry->d_inode);
382 struct comedi_device_file_info *dev_file_info =
383 comedi_get_device_file_info(minor);
384 struct comedi_subdevice *read_subdev =
385 comedi_get_read_subdevice(dev_file_info);
386 struct comedi_subdevice *write_subdev =
387 comedi_get_write_subdevice(dev_file_info);
389 memset(&devinfo, 0, sizeof(devinfo));
391 /* fill devinfo structure */
392 devinfo.version_code = COMEDI_VERSION_CODE;
393 devinfo.n_subdevs = dev->n_subdevices;
394 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
395 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
398 devinfo.read_subdevice = read_subdev - dev->subdevices;
400 devinfo.read_subdevice = -1;
403 devinfo.write_subdevice = write_subdev - dev->subdevices;
405 devinfo.write_subdevice = -1;
407 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
418 pointer to array of subdevice info structures
424 array of subdevice info structures at arg
427 static int do_subdinfo_ioctl(struct comedi_device *dev,
428 struct comedi_subdinfo __user *arg, void *file)
431 struct comedi_subdinfo *tmp, *us;
432 struct comedi_subdevice *s;
435 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
440 /* fill subdinfo structs */
441 for (i = 0; i < dev->n_subdevices; i++) {
442 s = dev->subdevices + i;
446 us->n_chan = s->n_chan;
447 us->subd_flags = s->subdev_flags;
448 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
449 us->subd_flags |= SDF_RUNNING;
450 #define TIMER_nanosec 5 /* backwards compatibility */
451 us->timer_type = TIMER_nanosec;
452 us->len_chanlist = s->len_chanlist;
453 us->maxdata = s->maxdata;
454 if (s->range_table) {
456 (i << 24) | (0 << 16) | (s->range_table->length);
458 us->range_type = 0; /* XXX */
460 us->flags = s->flags;
463 us->subd_flags |= SDF_BUSY;
465 us->subd_flags |= SDF_BUSY_OWNER;
467 us->subd_flags |= SDF_LOCKED;
469 us->subd_flags |= SDF_LOCK_OWNER;
470 if (!s->maxdata && s->maxdata_list)
471 us->subd_flags |= SDF_MAXDATA;
473 us->subd_flags |= SDF_FLAGS;
474 if (s->range_table_list)
475 us->subd_flags |= SDF_RANGETYPE;
477 us->subd_flags |= SDF_CMD;
479 if (s->insn_bits != &insn_inval)
480 us->insn_bits_support = COMEDI_SUPPORTED;
482 us->insn_bits_support = COMEDI_UNSUPPORTED;
484 us->settling_time_0 = s->settling_time_0;
487 ret = copy_to_user(arg, tmp,
488 dev->n_subdevices * sizeof(struct comedi_subdinfo));
492 return ret ? -EFAULT : 0;
500 pointer to chaninfo structure
503 chaninfo structure at arg
506 arrays at elements of chaninfo structure
509 static int do_chaninfo_ioctl(struct comedi_device *dev,
510 struct comedi_chaninfo __user *arg)
512 struct comedi_subdevice *s;
513 struct comedi_chaninfo it;
515 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
518 if (it.subdev >= dev->n_subdevices)
520 s = dev->subdevices + it.subdev;
522 if (it.maxdata_list) {
523 if (s->maxdata || !s->maxdata_list)
525 if (copy_to_user(it.maxdata_list, s->maxdata_list,
526 s->n_chan * sizeof(unsigned int)))
533 if (copy_to_user(it.flaglist, s->flaglist,
534 s->n_chan * sizeof(unsigned int)))
541 if (!s->range_table_list)
543 for (i = 0; i < s->n_chan; i++) {
546 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
547 (s->range_table_list[i]->length);
548 if (put_user(x, it.rangelist + i))
552 if (copy_to_user(it.rangelist, s->range_type_list,
553 s->n_chan * sizeof(unsigned int)))
563 buffer information ioctl
566 pointer to bufinfo structure
572 modified bufinfo at arg
575 static int do_bufinfo_ioctl(struct comedi_device *dev,
576 struct comedi_bufinfo __user *arg, void *file)
578 struct comedi_bufinfo bi;
579 struct comedi_subdevice *s;
580 struct comedi_async *async;
582 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
585 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
588 s = dev->subdevices + bi.subdevice;
590 if (s->lock && s->lock != file)
596 DPRINTK("subdevice does not have async capability\n");
597 bi.buf_write_ptr = 0;
599 bi.buf_write_count = 0;
600 bi.buf_read_count = 0;
602 bi.bytes_written = 0;
607 bi.bytes_written = 0;
608 goto copyback_position;
613 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
614 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
615 comedi_buf_read_free(async, bi.bytes_read);
617 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
619 && async->buf_write_count == async->buf_read_count) {
620 do_become_nonbusy(dev, s);
624 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
626 comedi_buf_write_alloc(async, bi.bytes_written);
627 comedi_buf_write_free(async, bi.bytes_written);
631 bi.buf_write_count = async->buf_write_count;
632 bi.buf_write_ptr = async->buf_write_ptr;
633 bi.buf_read_count = async->buf_read_count;
634 bi.buf_read_ptr = async->buf_read_ptr;
637 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
643 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
644 unsigned int *data, void *file);
647 * synchronous instructions
650 * pointer to sync cmd structure
653 * sync cmd struct at arg
660 /* arbitrary limits */
661 #define MAX_SAMPLES 256
662 static int do_insnlist_ioctl(struct comedi_device *dev,
663 struct comedi_insnlist __user *arg, void *file)
665 struct comedi_insnlist insnlist;
666 struct comedi_insn *insns = NULL;
667 unsigned int *data = NULL;
671 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
674 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
676 DPRINTK("kmalloc failed\n");
682 kcalloc(insnlist.n_insns, sizeof(struct comedi_insn), GFP_KERNEL);
684 DPRINTK("kmalloc failed\n");
689 if (copy_from_user(insns, insnlist.insns,
690 sizeof(struct comedi_insn) * insnlist.n_insns)) {
691 DPRINTK("copy_from_user failed\n");
696 for (i = 0; i < insnlist.n_insns; i++) {
697 if (insns[i].n > MAX_SAMPLES) {
698 DPRINTK("number of samples too large\n");
702 if (insns[i].insn & INSN_MASK_WRITE) {
703 if (copy_from_user(data, insns[i].data,
704 insns[i].n * sizeof(unsigned int))) {
705 DPRINTK("copy_from_user failed\n");
710 ret = parse_insn(dev, insns + i, data, file);
713 if (insns[i].insn & INSN_MASK_READ) {
714 if (copy_to_user(insns[i].data, data,
715 insns[i].n * sizeof(unsigned int))) {
716 DPRINTK("copy_to_user failed\n");
734 static int check_insn_config_length(struct comedi_insn *insn,
741 case INSN_CONFIG_DIO_OUTPUT:
742 case INSN_CONFIG_DIO_INPUT:
743 case INSN_CONFIG_DISARM:
744 case INSN_CONFIG_RESET:
748 case INSN_CONFIG_ARM:
749 case INSN_CONFIG_DIO_QUERY:
750 case INSN_CONFIG_BLOCK_SIZE:
751 case INSN_CONFIG_FILTER:
752 case INSN_CONFIG_SERIAL_CLOCK:
753 case INSN_CONFIG_BIDIRECTIONAL_DATA:
754 case INSN_CONFIG_ALT_SOURCE:
755 case INSN_CONFIG_SET_COUNTER_MODE:
756 case INSN_CONFIG_8254_READ_STATUS:
757 case INSN_CONFIG_SET_ROUTING:
758 case INSN_CONFIG_GET_ROUTING:
759 case INSN_CONFIG_GET_PWM_STATUS:
760 case INSN_CONFIG_PWM_SET_PERIOD:
761 case INSN_CONFIG_PWM_GET_PERIOD:
765 case INSN_CONFIG_SET_GATE_SRC:
766 case INSN_CONFIG_GET_GATE_SRC:
767 case INSN_CONFIG_SET_CLOCK_SRC:
768 case INSN_CONFIG_GET_CLOCK_SRC:
769 case INSN_CONFIG_SET_OTHER_SRC:
770 case INSN_CONFIG_GET_COUNTER_STATUS:
771 case INSN_CONFIG_PWM_SET_H_BRIDGE:
772 case INSN_CONFIG_PWM_GET_H_BRIDGE:
773 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
777 case INSN_CONFIG_PWM_OUTPUT:
778 case INSN_CONFIG_ANALOG_TRIG:
782 /* by default we allow the insn since we don't have checks for
783 * all possible cases yet */
786 "comedi: no check for data length of config insn id "
787 "%i is implemented.\n"
788 " Add a check to %s in %s.\n"
789 " Assuming n=%i is correct.\n", data[0], __func__,
797 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
798 unsigned int *data, void *file)
800 struct comedi_subdevice *s;
804 if (insn->insn & INSN_MASK_SPECIAL) {
805 /* a non-subdevice instruction */
807 switch (insn->insn) {
817 do_gettimeofday(&tv);
819 data[1] = tv.tv_usec;
825 if (insn->n != 1 || data[0] >= 100000) {
829 udelay(data[0] / 1000);
837 if (insn->subdev >= dev->n_subdevices) {
838 DPRINTK("%d not usable subdevice\n",
843 s = dev->subdevices + insn->subdev;
845 DPRINTK("no async\n");
849 if (!s->async->inttrig) {
850 DPRINTK("no inttrig\n");
854 ret = s->async->inttrig(dev, s, data[0]);
859 DPRINTK("invalid insn\n");
864 /* a subdevice instruction */
865 unsigned int maxdata;
867 if (insn->subdev >= dev->n_subdevices) {
868 DPRINTK("subdevice %d out of range\n", insn->subdev);
872 s = dev->subdevices + insn->subdev;
874 if (s->type == COMEDI_SUBD_UNUSED) {
875 DPRINTK("%d not usable subdevice\n", insn->subdev);
880 /* are we locked? (ioctl lock) */
881 if (s->lock && s->lock != file) {
882 DPRINTK("device locked\n");
887 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
890 DPRINTK("bad chanspec\n");
898 /* This looks arbitrary. It is. */
899 s->busy = &parse_insn;
900 switch (insn->insn) {
902 ret = s->insn_read(dev, s, insn, data);
905 maxdata = s->maxdata_list
906 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
908 for (i = 0; i < insn->n; ++i) {
909 if (data[i] > maxdata) {
911 DPRINTK("bad data value(s)\n");
916 ret = s->insn_write(dev, s, insn, data);
922 /* Most drivers ignore the base channel in
923 * insn->chanspec. Fix this here if
924 * the subdevice has <= 32 channels. */
926 unsigned int orig_mask;
929 if (s->n_chan <= 32) {
930 shift = CR_CHAN(insn->chanspec);
938 ret = s->insn_bits(dev, s, insn, data);
945 ret = check_insn_config_length(insn, data);
948 ret = s->insn_config(dev, s, insn, data);
964 * synchronous instructions
970 * struct comedi_insn struct at arg
976 static int do_insn_ioctl(struct comedi_device *dev,
977 struct comedi_insn __user *arg, void *file)
979 struct comedi_insn insn;
980 unsigned int *data = NULL;
983 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
989 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
994 /* This is where the behavior of insn and insnlist deviate. */
995 if (insn.n > MAX_SAMPLES)
996 insn.n = MAX_SAMPLES;
997 if (insn.insn & INSN_MASK_WRITE) {
998 if (copy_from_user(data,
1000 insn.n * sizeof(unsigned int))) {
1005 ret = parse_insn(dev, &insn, data, file);
1008 if (insn.insn & INSN_MASK_READ) {
1009 if (copy_to_user(insn.data,
1011 insn.n * sizeof(unsigned int))) {
1024 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
1025 unsigned mask, unsigned bits)
1027 unsigned long flags;
1029 spin_lock_irqsave(&s->spin_lock, flags);
1030 s->runflags &= ~mask;
1031 s->runflags |= (bits & mask);
1032 spin_unlock_irqrestore(&s->spin_lock, flags);
1035 static int do_cmd_ioctl(struct comedi_device *dev,
1036 struct comedi_cmd __user *cmd, void *file)
1038 struct comedi_cmd user_cmd;
1039 struct comedi_subdevice *s;
1040 struct comedi_async *async;
1042 unsigned int __user *chanlist_saver = NULL;
1044 if (copy_from_user(&user_cmd, cmd, sizeof(struct comedi_cmd))) {
1045 DPRINTK("bad cmd address\n");
1048 /* save user's chanlist pointer so it can be restored later */
1049 chanlist_saver = user_cmd.chanlist;
1051 if (user_cmd.subdev >= dev->n_subdevices) {
1052 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1056 s = dev->subdevices + user_cmd.subdev;
1059 if (s->type == COMEDI_SUBD_UNUSED) {
1060 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1064 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1065 DPRINTK("subdevice %i does not support commands\n",
1070 /* are we locked? (ioctl lock) */
1071 if (s->lock && s->lock != file) {
1072 DPRINTK("subdevice locked\n");
1078 DPRINTK("subdevice busy\n");
1082 /* make sure channel/gain list isn't too long */
1083 if (user_cmd.chanlist_len > s->len_chanlist) {
1084 DPRINTK("channel/gain list too long %u > %d\n",
1085 user_cmd.chanlist_len, s->len_chanlist);
1089 /* make sure channel/gain list isn't too short */
1090 if (user_cmd.chanlist_len < 1) {
1091 DPRINTK("channel/gain list too short %u < 1\n",
1092 user_cmd.chanlist_len);
1096 async->cmd = user_cmd;
1097 async->cmd.data = NULL;
1098 /* load channel/gain list */
1099 async->cmd.chanlist =
1100 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1101 if (!async->cmd.chanlist) {
1102 DPRINTK("allocation failed\n");
1106 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1107 async->cmd.chanlist_len * sizeof(int))) {
1108 DPRINTK("fault reading chanlist\n");
1113 /* make sure each element in channel/gain list is valid */
1114 ret = comedi_check_chanlist(s,
1115 async->cmd.chanlist_len,
1116 async->cmd.chanlist);
1118 DPRINTK("bad chanlist\n");
1122 ret = s->do_cmdtest(dev, s, &async->cmd);
1124 if (async->cmd.flags & TRIG_BOGUS || ret) {
1125 DPRINTK("test returned %d\n", ret);
1126 user_cmd = async->cmd;
1127 /* restore chanlist pointer before copying back */
1128 user_cmd.chanlist = chanlist_saver;
1129 user_cmd.data = NULL;
1130 if (copy_to_user(cmd, &user_cmd, sizeof(struct comedi_cmd))) {
1131 DPRINTK("fault writing cmd\n");
1139 if (!async->prealloc_bufsz) {
1141 DPRINTK("no buffer (?)\n");
1145 comedi_reset_async_buf(async);
1148 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1150 if (async->cmd.flags & TRIG_WAKE_EOS)
1151 async->cb_mask |= COMEDI_CB_EOS;
1153 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1155 /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
1156 * comedi_read() or comedi_write() */
1158 ret = s->do_cmd(dev, s);
1163 do_become_nonbusy(dev, s);
1170 command testing ioctl
1173 pointer to cmd structure
1176 cmd structure at arg
1180 modified cmd structure at arg
1183 static int do_cmdtest_ioctl(struct comedi_device *dev,
1184 struct comedi_cmd __user *arg, void *file)
1186 struct comedi_cmd user_cmd;
1187 struct comedi_subdevice *s;
1189 unsigned int *chanlist = NULL;
1190 unsigned int __user *chanlist_saver = NULL;
1192 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
1193 DPRINTK("bad cmd address\n");
1196 /* save user's chanlist pointer so it can be restored later */
1197 chanlist_saver = user_cmd.chanlist;
1199 if (user_cmd.subdev >= dev->n_subdevices) {
1200 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1204 s = dev->subdevices + user_cmd.subdev;
1205 if (s->type == COMEDI_SUBD_UNUSED) {
1206 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1210 if (!s->do_cmd || !s->do_cmdtest) {
1211 DPRINTK("subdevice %i does not support commands\n",
1216 /* make sure channel/gain list isn't too long */
1217 if (user_cmd.chanlist_len > s->len_chanlist) {
1218 DPRINTK("channel/gain list too long %d > %d\n",
1219 user_cmd.chanlist_len, s->len_chanlist);
1224 /* load channel/gain list */
1225 if (user_cmd.chanlist) {
1227 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1229 DPRINTK("allocation failed\n");
1234 if (copy_from_user(chanlist, user_cmd.chanlist,
1235 user_cmd.chanlist_len * sizeof(int))) {
1236 DPRINTK("fault reading chanlist\n");
1241 /* make sure each element in channel/gain list is valid */
1242 ret = comedi_check_chanlist(s, user_cmd.chanlist_len, chanlist);
1244 DPRINTK("bad chanlist\n");
1248 user_cmd.chanlist = chanlist;
1251 ret = s->do_cmdtest(dev, s, &user_cmd);
1253 /* restore chanlist pointer before copying back */
1254 user_cmd.chanlist = chanlist_saver;
1256 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
1257 DPRINTK("bad cmd address\n");
1282 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1286 unsigned long flags;
1287 struct comedi_subdevice *s;
1289 if (arg >= dev->n_subdevices)
1291 s = dev->subdevices + arg;
1293 spin_lock_irqsave(&s->spin_lock, flags);
1294 if (s->busy || s->lock)
1298 spin_unlock_irqrestore(&s->spin_lock, flags);
1305 ret = s->lock_f(dev, s);
1324 This function isn't protected by the semaphore, since
1325 we already own the lock.
1327 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1330 struct comedi_subdevice *s;
1332 if (arg >= dev->n_subdevices)
1334 s = dev->subdevices + arg;
1339 if (s->lock && s->lock != file)
1342 if (s->lock == file) {
1356 cancel acquisition ioctl
1368 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1371 struct comedi_subdevice *s;
1374 if (arg >= dev->n_subdevices)
1376 s = dev->subdevices + arg;
1377 if (s->async == NULL)
1380 if (s->lock && s->lock != file)
1386 if (s->busy != file)
1389 ret = do_cancel(dev, s);
1390 if (comedi_get_subdevice_runflags(s) & SRF_USER)
1391 wake_up_interruptible(&s->async->wait_head);
1398 instructs driver to synchronize buffers
1410 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1413 struct comedi_subdevice *s;
1415 if (arg >= dev->n_subdevices)
1417 s = dev->subdevices + arg;
1419 if (s->lock && s->lock != file)
1425 if (s->busy != file)
1429 return s->poll(dev, s);
1434 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1438 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1439 ret = s->cancel(dev, s);
1441 do_become_nonbusy(dev, s);
1447 static void comedi_vm_open(struct vm_area_struct *area)
1449 struct comedi_async *async;
1450 struct comedi_device *dev;
1452 async = area->vm_private_data;
1453 dev = async->subdevice->device;
1455 mutex_lock(&dev->mutex);
1456 async->mmap_count++;
1457 mutex_unlock(&dev->mutex);
1460 static void comedi_vm_close(struct vm_area_struct *area)
1462 struct comedi_async *async;
1463 struct comedi_device *dev;
1465 async = area->vm_private_data;
1466 dev = async->subdevice->device;
1468 mutex_lock(&dev->mutex);
1469 async->mmap_count--;
1470 mutex_unlock(&dev->mutex);
1473 static struct vm_operations_struct comedi_vm_ops = {
1474 .open = comedi_vm_open,
1475 .close = comedi_vm_close,
1478 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1480 const unsigned minor = iminor(file->f_dentry->d_inode);
1481 struct comedi_async *async = NULL;
1482 unsigned long start = vma->vm_start;
1487 struct comedi_subdevice *s;
1488 struct comedi_device_file_info *dev_file_info;
1489 struct comedi_device *dev;
1491 dev_file_info = comedi_get_device_file_info(minor);
1492 if (dev_file_info == NULL)
1494 dev = dev_file_info->device;
1498 mutex_lock(&dev->mutex);
1499 if (!dev->attached) {
1500 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1504 if (vma->vm_flags & VM_WRITE)
1505 s = comedi_get_write_subdevice(dev_file_info);
1507 s = comedi_get_read_subdevice(dev_file_info);
1514 if (async == NULL) {
1519 if (vma->vm_pgoff != 0) {
1520 DPRINTK("comedi: mmap() offset must be 0.\n");
1525 size = vma->vm_end - vma->vm_start;
1526 if (size > async->prealloc_bufsz) {
1530 if (size & (~PAGE_MASK)) {
1535 n_pages = size >> PAGE_SHIFT;
1536 for (i = 0; i < n_pages; ++i) {
1537 if (remap_pfn_range(vma, start,
1538 page_to_pfn(virt_to_page
1539 (async->buf_page_list
1540 [i].virt_addr)), PAGE_SIZE,
1548 vma->vm_ops = &comedi_vm_ops;
1549 vma->vm_private_data = async;
1551 async->mmap_count++;
1555 mutex_unlock(&dev->mutex);
1559 static unsigned int comedi_poll(struct file *file, poll_table * wait)
1561 unsigned int mask = 0;
1562 const unsigned minor = iminor(file->f_dentry->d_inode);
1563 struct comedi_subdevice *read_subdev;
1564 struct comedi_subdevice *write_subdev;
1565 struct comedi_device_file_info *dev_file_info;
1566 struct comedi_device *dev;
1567 dev_file_info = comedi_get_device_file_info(minor);
1569 if (dev_file_info == NULL)
1571 dev = dev_file_info->device;
1575 mutex_lock(&dev->mutex);
1576 if (!dev->attached) {
1577 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1578 mutex_unlock(&dev->mutex);
1583 read_subdev = comedi_get_read_subdevice(dev_file_info);
1584 if (read_subdev && read_subdev->async) {
1585 poll_wait(file, &read_subdev->async->wait_head, wait);
1586 if (!read_subdev->busy
1587 || comedi_buf_read_n_available(read_subdev->async) > 0
1588 || !(comedi_get_subdevice_runflags(read_subdev) &
1590 mask |= POLLIN | POLLRDNORM;
1593 write_subdev = comedi_get_write_subdevice(dev_file_info);
1594 if (write_subdev && write_subdev->async) {
1595 poll_wait(file, &write_subdev->async->wait_head, wait);
1596 comedi_buf_write_alloc(write_subdev->async,
1597 write_subdev->async->prealloc_bufsz);
1598 if (!write_subdev->busy
1599 || !(comedi_get_subdevice_runflags(write_subdev) &
1601 || comedi_buf_write_n_allocated(write_subdev->async) >=
1602 bytes_per_sample(write_subdev->async->subdevice)) {
1603 mask |= POLLOUT | POLLWRNORM;
1607 mutex_unlock(&dev->mutex);
1611 static ssize_t comedi_write(struct file *file, const char __user *buf,
1612 size_t nbytes, loff_t *offset)
1614 struct comedi_subdevice *s;
1615 struct comedi_async *async;
1616 int n, m, count = 0, retval = 0;
1617 DECLARE_WAITQUEUE(wait, current);
1618 const unsigned minor = iminor(file->f_dentry->d_inode);
1619 struct comedi_device_file_info *dev_file_info;
1620 struct comedi_device *dev;
1621 dev_file_info = comedi_get_device_file_info(minor);
1623 if (dev_file_info == NULL)
1625 dev = dev_file_info->device;
1629 if (!dev->attached) {
1630 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1635 s = comedi_get_write_subdevice(dev_file_info);
1636 if (s == NULL || s->async == NULL) {
1650 if (s->busy != file) {
1654 add_wait_queue(&async->wait_head, &wait);
1655 while (nbytes > 0 && !retval) {
1656 set_current_state(TASK_INTERRUPTIBLE);
1658 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1660 mutex_lock(&dev->mutex);
1661 if (comedi_get_subdevice_runflags(s) &
1667 do_become_nonbusy(dev, s);
1668 mutex_unlock(&dev->mutex);
1676 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1677 m = async->prealloc_bufsz - async->buf_write_ptr;
1678 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1679 if (m > comedi_buf_write_n_allocated(async))
1680 m = comedi_buf_write_n_allocated(async);
1685 if (file->f_flags & O_NONBLOCK) {
1690 if (signal_pending(current)) {
1691 retval = -ERESTARTSYS;
1696 if (s->busy != file) {
1703 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1709 comedi_buf_write_free(async, n);
1715 break; /* makes device work like a pipe */
1717 set_current_state(TASK_RUNNING);
1718 remove_wait_queue(&async->wait_head, &wait);
1721 return count ? count : retval;
1724 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
1727 struct comedi_subdevice *s;
1728 struct comedi_async *async;
1729 int n, m, count = 0, retval = 0;
1730 DECLARE_WAITQUEUE(wait, current);
1731 const unsigned minor = iminor(file->f_dentry->d_inode);
1732 struct comedi_device_file_info *dev_file_info;
1733 struct comedi_device *dev;
1734 dev_file_info = comedi_get_device_file_info(minor);
1736 if (dev_file_info == NULL)
1738 dev = dev_file_info->device;
1742 if (!dev->attached) {
1743 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1748 s = comedi_get_read_subdevice(dev_file_info);
1749 if (s == NULL || s->async == NULL) {
1762 if (s->busy != file) {
1767 add_wait_queue(&async->wait_head, &wait);
1768 while (nbytes > 0 && !retval) {
1769 set_current_state(TASK_INTERRUPTIBLE);
1773 m = comedi_buf_read_n_available(async);
1774 /* printk("%d available\n",m); */
1775 if (async->buf_read_ptr + m > async->prealloc_bufsz)
1776 m = async->prealloc_bufsz - async->buf_read_ptr;
1777 /* printk("%d contiguous\n",m); */
1782 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1783 mutex_lock(&dev->mutex);
1784 do_become_nonbusy(dev, s);
1785 if (comedi_get_subdevice_runflags(s) &
1791 mutex_unlock(&dev->mutex);
1794 if (file->f_flags & O_NONBLOCK) {
1799 if (signal_pending(current)) {
1800 retval = -ERESTARTSYS;
1807 if (s->busy != file) {
1813 m = copy_to_user(buf, async->prealloc_buf +
1814 async->buf_read_ptr, n);
1820 comedi_buf_read_alloc(async, n);
1821 comedi_buf_read_free(async, n);
1827 break; /* makes device work like a pipe */
1829 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING))) {
1830 mutex_lock(&dev->mutex);
1831 if (async->buf_read_count - async->buf_write_count == 0)
1832 do_become_nonbusy(dev, s);
1833 mutex_unlock(&dev->mutex);
1835 set_current_state(TASK_RUNNING);
1836 remove_wait_queue(&async->wait_head, &wait);
1839 return count ? count : retval;
1843 This function restores a subdevice to an idle state.
1845 void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
1847 struct comedi_async *async = s->async;
1849 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1851 comedi_reset_async_buf(async);
1852 async->inttrig = NULL;
1853 kfree(async->cmd.chanlist);
1854 async->cmd.chanlist = NULL;
1857 "BUG: (?) do_become_nonbusy called with async=0\n");
1863 static int comedi_open(struct inode *inode, struct file *file)
1865 const unsigned minor = iminor(inode);
1866 struct comedi_device_file_info *dev_file_info =
1867 comedi_get_device_file_info(minor);
1868 struct comedi_device *dev =
1869 dev_file_info ? dev_file_info->device : NULL;
1872 DPRINTK("invalid minor number\n");
1876 /* This is slightly hacky, but we want module autoloading
1878 * case: user opens device, attached -> ok
1879 * case: user opens device, unattached, in_request_module=0 -> autoload
1880 * case: user opens device, unattached, in_request_module=1 -> fail
1881 * case: root opens device, attached -> ok
1882 * case: root opens device, unattached, in_request_module=1 -> ok
1883 * (typically called from modprobe)
1884 * case: root opens device, unattached, in_request_module=0 -> autoload
1886 * The last could be changed to "-> ok", which would deny root
1889 mutex_lock(&dev->mutex);
1892 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
1893 DPRINTK("in request module\n");
1894 mutex_unlock(&dev->mutex);
1897 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
1900 dev->in_request_module = 1;
1903 mutex_unlock(&dev->mutex);
1904 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1905 mutex_lock(&dev->mutex);
1908 dev->in_request_module = 0;
1910 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
1911 DPRINTK("not attached and not CAP_NET_ADMIN\n");
1912 mutex_unlock(&dev->mutex);
1916 __module_get(THIS_MODULE);
1918 if (dev->attached) {
1919 if (!try_module_get(dev->driver->module)) {
1920 module_put(THIS_MODULE);
1921 mutex_unlock(&dev->mutex);
1926 if (dev->attached && dev->use_count == 0 && dev->open) {
1927 int rc = dev->open(dev);
1929 module_put(dev->driver->module);
1930 module_put(THIS_MODULE);
1931 mutex_unlock(&dev->mutex);
1938 mutex_unlock(&dev->mutex);
1943 static int comedi_close(struct inode *inode, struct file *file)
1945 const unsigned minor = iminor(inode);
1946 struct comedi_subdevice *s = NULL;
1948 struct comedi_device_file_info *dev_file_info;
1949 struct comedi_device *dev;
1950 dev_file_info = comedi_get_device_file_info(minor);
1952 if (dev_file_info == NULL)
1954 dev = dev_file_info->device;
1958 mutex_lock(&dev->mutex);
1960 if (dev->subdevices) {
1961 for (i = 0; i < dev->n_subdevices; i++) {
1962 s = dev->subdevices + i;
1964 if (s->busy == file)
1966 if (s->lock == file)
1970 if (dev->attached && dev->use_count == 1 && dev->close)
1973 module_put(THIS_MODULE);
1975 module_put(dev->driver->module);
1979 mutex_unlock(&dev->mutex);
1981 if (file->f_flags & FASYNC)
1982 comedi_fasync(-1, file, 0);
1987 static int comedi_fasync(int fd, struct file *file, int on)
1989 const unsigned minor = iminor(file->f_dentry->d_inode);
1990 struct comedi_device_file_info *dev_file_info;
1991 struct comedi_device *dev;
1992 dev_file_info = comedi_get_device_file_info(minor);
1994 if (dev_file_info == NULL)
1996 dev = dev_file_info->device;
2000 return fasync_helper(fd, file, on, &dev->async_queue);
2003 const struct file_operations comedi_fops = {
2004 .owner = THIS_MODULE,
2005 .unlocked_ioctl = comedi_unlocked_ioctl,
2006 .compat_ioctl = comedi_compat_ioctl,
2007 .open = comedi_open,
2008 .release = comedi_close,
2009 .read = comedi_read,
2010 .write = comedi_write,
2011 .mmap = comedi_mmap,
2012 .poll = comedi_poll,
2013 .fasync = comedi_fasync,
2014 .llseek = noop_llseek,
2017 struct class *comedi_class;
2018 static struct cdev comedi_cdev;
2020 static void comedi_cleanup_legacy_minors(void)
2024 for (i = 0; i < comedi_num_legacy_minors; i++)
2025 comedi_free_board_minor(i);
2028 static int __init comedi_init(void)
2033 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
2034 " - http://www.comedi.org\n");
2036 if (comedi_num_legacy_minors < 0 ||
2037 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2038 printk(KERN_ERR "comedi: error: invalid value for module "
2039 "parameter \"comedi_num_legacy_minors\". Valid values "
2040 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
2045 * comedi is unusable if both comedi_autoconfig and
2046 * comedi_num_legacy_minors are zero, so we might as well adjust the
2047 * defaults in that case
2049 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2050 comedi_num_legacy_minors = 16;
2052 memset(comedi_file_info_table, 0,
2053 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
2055 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2056 COMEDI_NUM_MINORS, "comedi");
2059 cdev_init(&comedi_cdev, &comedi_fops);
2060 comedi_cdev.owner = THIS_MODULE;
2061 kobject_set_name(&comedi_cdev.kobj, "comedi");
2062 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2063 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2067 comedi_class = class_create(THIS_MODULE, "comedi");
2068 if (IS_ERR(comedi_class)) {
2069 printk(KERN_ERR "comedi: failed to create class");
2070 cdev_del(&comedi_cdev);
2071 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2073 return PTR_ERR(comedi_class);
2076 /* XXX requires /proc interface */
2079 /* create devices files for legacy/manual use */
2080 for (i = 0; i < comedi_num_legacy_minors; i++) {
2082 minor = comedi_alloc_board_minor(NULL);
2084 comedi_cleanup_legacy_minors();
2085 cdev_del(&comedi_cdev);
2086 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2095 static void __exit comedi_cleanup(void)
2099 comedi_cleanup_legacy_minors();
2100 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2101 BUG_ON(comedi_file_info_table[i]);
2103 class_destroy(comedi_class);
2104 cdev_del(&comedi_cdev);
2105 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2107 comedi_proc_cleanup();
2110 module_init(comedi_init);
2111 module_exit(comedi_cleanup);
2113 void comedi_error(const struct comedi_device *dev, const char *s)
2115 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
2116 dev->driver->driver_name, s);
2118 EXPORT_SYMBOL(comedi_error);
2120 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2122 struct comedi_async *async = s->async;
2123 unsigned runflags = 0;
2124 unsigned runflags_mask = 0;
2126 /* DPRINTK("comedi_event 0x%x\n",mask); */
2128 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2132 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2133 COMEDI_CB_OVERFLOW)) {
2134 runflags_mask |= SRF_RUNNING;
2136 /* remember if an error event has occurred, so an error
2137 * can be returned the next time the user does a read() */
2138 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2139 runflags_mask |= SRF_ERROR;
2140 runflags |= SRF_ERROR;
2142 if (runflags_mask) {
2143 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2144 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2147 if (async->cb_mask & s->async->events) {
2148 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2149 wake_up_interruptible(&async->wait_head);
2150 if (s->subdev_flags & SDF_CMD_READ)
2151 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2152 if (s->subdev_flags & SDF_CMD_WRITE)
2153 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2156 async->cb_func(s->async->events, async->cb_arg);
2159 s->async->events = 0;
2161 EXPORT_SYMBOL(comedi_event);
2163 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
2165 unsigned long flags;
2168 spin_lock_irqsave(&s->spin_lock, flags);
2169 runflags = s->runflags;
2170 spin_unlock_irqrestore(&s->spin_lock, flags);
2173 EXPORT_SYMBOL(comedi_get_subdevice_runflags);
2175 static int is_device_busy(struct comedi_device *dev)
2177 struct comedi_subdevice *s;
2183 for (i = 0; i < dev->n_subdevices; i++) {
2184 s = dev->subdevices + i;
2187 if (s->async && s->async->mmap_count)
2194 static void comedi_device_init(struct comedi_device *dev)
2196 memset(dev, 0, sizeof(struct comedi_device));
2197 spin_lock_init(&dev->spinlock);
2198 mutex_init(&dev->mutex);
2202 static void comedi_device_cleanup(struct comedi_device *dev)
2206 mutex_lock(&dev->mutex);
2207 comedi_device_detach(dev);
2208 mutex_unlock(&dev->mutex);
2209 mutex_destroy(&dev->mutex);
2212 int comedi_alloc_board_minor(struct device *hardware_device)
2214 unsigned long flags;
2215 struct comedi_device_file_info *info;
2216 struct device *csdev;
2220 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2223 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2224 if (info->device == NULL) {
2228 info->hardware_device = hardware_device;
2229 comedi_device_init(info->device);
2230 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2231 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2232 if (comedi_file_info_table[i] == NULL) {
2233 comedi_file_info_table[i] = info;
2237 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2238 if (i == COMEDI_NUM_BOARD_MINORS) {
2239 comedi_device_cleanup(info->device);
2240 kfree(info->device);
2244 "ran out of minor numbers for board device files.\n");
2247 info->device->minor = i;
2248 csdev = device_create(comedi_class, hardware_device,
2249 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2251 info->device->class_dev = csdev;
2252 dev_set_drvdata(csdev, info);
2253 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2257 "failed to create sysfs attribute file \"%s\".\n",
2258 dev_attr_max_read_buffer_kb.attr.name);
2259 comedi_free_board_minor(i);
2262 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2266 "failed to create sysfs attribute file \"%s\".\n",
2267 dev_attr_read_buffer_kb.attr.name);
2268 comedi_free_board_minor(i);
2271 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2275 "failed to create sysfs attribute file \"%s\".\n",
2276 dev_attr_max_write_buffer_kb.attr.name);
2277 comedi_free_board_minor(i);
2280 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2284 "failed to create sysfs attribute file \"%s\".\n",
2285 dev_attr_write_buffer_kb.attr.name);
2286 comedi_free_board_minor(i);
2292 void comedi_free_board_minor(unsigned minor)
2294 unsigned long flags;
2295 struct comedi_device_file_info *info;
2297 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2298 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2299 info = comedi_file_info_table[minor];
2300 comedi_file_info_table[minor] = NULL;
2301 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2304 struct comedi_device *dev = info->device;
2306 if (dev->class_dev) {
2307 device_destroy(comedi_class,
2308 MKDEV(COMEDI_MAJOR, dev->minor));
2310 comedi_device_cleanup(dev);
2317 int comedi_find_board_minor(struct device *hardware_device)
2320 struct comedi_device_file_info *info;
2322 for (minor = 0; minor < COMEDI_NUM_BOARD_MINORS; minor++) {
2323 spin_lock(&comedi_file_info_table_lock);
2324 info = comedi_file_info_table[minor];
2325 if (info && info->hardware_device == hardware_device) {
2326 spin_unlock(&comedi_file_info_table_lock);
2329 spin_unlock(&comedi_file_info_table_lock);
2334 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2335 struct comedi_subdevice *s)
2337 unsigned long flags;
2338 struct comedi_device_file_info *info;
2339 struct device *csdev;
2343 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2347 info->read_subdevice = s;
2348 info->write_subdevice = s;
2349 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2350 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2351 if (comedi_file_info_table[i] == NULL) {
2352 comedi_file_info_table[i] = info;
2356 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2357 if (i == COMEDI_NUM_MINORS) {
2361 "ran out of minor numbers for board device files.\n");
2365 csdev = device_create(comedi_class, dev->class_dev,
2366 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2367 dev->minor, (int)(s - dev->subdevices));
2369 s->class_dev = csdev;
2370 dev_set_drvdata(csdev, info);
2371 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2375 "failed to create sysfs attribute file \"%s\".\n",
2376 dev_attr_max_read_buffer_kb.attr.name);
2377 comedi_free_subdevice_minor(s);
2380 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2384 "failed to create sysfs attribute file \"%s\".\n",
2385 dev_attr_read_buffer_kb.attr.name);
2386 comedi_free_subdevice_minor(s);
2389 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2393 "failed to create sysfs attribute file \"%s\".\n",
2394 dev_attr_max_write_buffer_kb.attr.name);
2395 comedi_free_subdevice_minor(s);
2398 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2402 "failed to create sysfs attribute file \"%s\".\n",
2403 dev_attr_write_buffer_kb.attr.name);
2404 comedi_free_subdevice_minor(s);
2410 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2412 unsigned long flags;
2413 struct comedi_device_file_info *info;
2420 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2421 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2423 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2424 info = comedi_file_info_table[s->minor];
2425 comedi_file_info_table[s->minor] = NULL;
2426 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2429 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2430 s->class_dev = NULL;
2435 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2437 unsigned long flags;
2438 struct comedi_device_file_info *info;
2440 BUG_ON(minor >= COMEDI_NUM_MINORS);
2441 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2442 info = comedi_file_info_table[minor];
2443 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2446 EXPORT_SYMBOL_GPL(comedi_get_device_file_info);
2448 static int resize_async_buffer(struct comedi_device *dev,
2449 struct comedi_subdevice *s,
2450 struct comedi_async *async, unsigned new_size)
2454 if (new_size > async->max_bufsize)
2458 DPRINTK("subdevice is busy, cannot resize buffer\n");
2461 if (async->mmap_count) {
2462 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2466 if (!async->prealloc_buf)
2469 /* make sure buffer is an integral number of pages
2471 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2473 retval = comedi_buf_alloc(dev, s, new_size);
2477 if (s->buf_change) {
2478 retval = s->buf_change(dev, s, new_size);
2483 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2484 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
2488 /* sysfs attribute files */
2490 static const unsigned bytes_per_kibi = 1024;
2492 static ssize_t show_max_read_buffer_kb(struct device *dev,
2493 struct device_attribute *attr, char *buf)
2496 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2497 unsigned max_buffer_size_kb = 0;
2498 struct comedi_subdevice *const read_subdevice =
2499 comedi_get_read_subdevice(info);
2501 mutex_lock(&info->device->mutex);
2502 if (read_subdevice &&
2503 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2504 read_subdevice->async) {
2505 max_buffer_size_kb = read_subdevice->async->max_bufsize /
2508 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2509 mutex_unlock(&info->device->mutex);
2514 static ssize_t store_max_read_buffer_kb(struct device *dev,
2515 struct device_attribute *attr,
2516 const char *buf, size_t count)
2518 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2519 unsigned long new_max_size_kb;
2520 uint64_t new_max_size;
2521 struct comedi_subdevice *const read_subdevice =
2522 comedi_get_read_subdevice(info);
2524 if (strict_strtoul(buf, 10, &new_max_size_kb))
2526 if (new_max_size_kb != (uint32_t) new_max_size_kb)
2528 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2529 if (new_max_size != (uint32_t) new_max_size)
2532 mutex_lock(&info->device->mutex);
2533 if (read_subdevice == NULL ||
2534 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2535 read_subdevice->async == NULL) {
2536 mutex_unlock(&info->device->mutex);
2539 read_subdevice->async->max_bufsize = new_max_size;
2540 mutex_unlock(&info->device->mutex);
2545 static struct device_attribute dev_attr_max_read_buffer_kb = {
2547 .name = "max_read_buffer_kb",
2548 .mode = S_IRUGO | S_IWUSR},
2549 .show = &show_max_read_buffer_kb,
2550 .store = &store_max_read_buffer_kb
2553 static ssize_t show_read_buffer_kb(struct device *dev,
2554 struct device_attribute *attr, char *buf)
2557 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2558 unsigned buffer_size_kb = 0;
2559 struct comedi_subdevice *const read_subdevice =
2560 comedi_get_read_subdevice(info);
2562 mutex_lock(&info->device->mutex);
2563 if (read_subdevice &&
2564 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2565 read_subdevice->async) {
2566 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
2569 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2570 mutex_unlock(&info->device->mutex);
2575 static ssize_t store_read_buffer_kb(struct device *dev,
2576 struct device_attribute *attr,
2577 const char *buf, size_t count)
2579 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2580 unsigned long new_size_kb;
2583 struct comedi_subdevice *const read_subdevice =
2584 comedi_get_read_subdevice(info);
2586 if (strict_strtoul(buf, 10, &new_size_kb))
2588 if (new_size_kb != (uint32_t) new_size_kb)
2590 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2591 if (new_size != (uint32_t) new_size)
2594 mutex_lock(&info->device->mutex);
2595 if (read_subdevice == NULL ||
2596 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2597 read_subdevice->async == NULL) {
2598 mutex_unlock(&info->device->mutex);
2601 retval = resize_async_buffer(info->device, read_subdevice,
2602 read_subdevice->async, new_size);
2603 mutex_unlock(&info->device->mutex);
2610 static struct device_attribute dev_attr_read_buffer_kb = {
2612 .name = "read_buffer_kb",
2613 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
2614 .show = &show_read_buffer_kb,
2615 .store = &store_read_buffer_kb
2618 static ssize_t show_max_write_buffer_kb(struct device *dev,
2619 struct device_attribute *attr,
2623 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2624 unsigned max_buffer_size_kb = 0;
2625 struct comedi_subdevice *const write_subdevice =
2626 comedi_get_write_subdevice(info);
2628 mutex_lock(&info->device->mutex);
2629 if (write_subdevice &&
2630 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2631 write_subdevice->async) {
2632 max_buffer_size_kb = write_subdevice->async->max_bufsize /
2635 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2636 mutex_unlock(&info->device->mutex);
2641 static ssize_t store_max_write_buffer_kb(struct device *dev,
2642 struct device_attribute *attr,
2643 const char *buf, size_t count)
2645 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2646 unsigned long new_max_size_kb;
2647 uint64_t new_max_size;
2648 struct comedi_subdevice *const write_subdevice =
2649 comedi_get_write_subdevice(info);
2651 if (strict_strtoul(buf, 10, &new_max_size_kb))
2653 if (new_max_size_kb != (uint32_t) new_max_size_kb)
2655 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2656 if (new_max_size != (uint32_t) new_max_size)
2659 mutex_lock(&info->device->mutex);
2660 if (write_subdevice == NULL ||
2661 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2662 write_subdevice->async == NULL) {
2663 mutex_unlock(&info->device->mutex);
2666 write_subdevice->async->max_bufsize = new_max_size;
2667 mutex_unlock(&info->device->mutex);
2672 static struct device_attribute dev_attr_max_write_buffer_kb = {
2674 .name = "max_write_buffer_kb",
2675 .mode = S_IRUGO | S_IWUSR},
2676 .show = &show_max_write_buffer_kb,
2677 .store = &store_max_write_buffer_kb
2680 static ssize_t show_write_buffer_kb(struct device *dev,
2681 struct device_attribute *attr, char *buf)
2684 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2685 unsigned buffer_size_kb = 0;
2686 struct comedi_subdevice *const write_subdevice =
2687 comedi_get_write_subdevice(info);
2689 mutex_lock(&info->device->mutex);
2690 if (write_subdevice &&
2691 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2692 write_subdevice->async) {
2693 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
2696 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2697 mutex_unlock(&info->device->mutex);
2702 static ssize_t store_write_buffer_kb(struct device *dev,
2703 struct device_attribute *attr,
2704 const char *buf, size_t count)
2706 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2707 unsigned long new_size_kb;
2710 struct comedi_subdevice *const write_subdevice =
2711 comedi_get_write_subdevice(info);
2713 if (strict_strtoul(buf, 10, &new_size_kb))
2715 if (new_size_kb != (uint32_t) new_size_kb)
2717 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2718 if (new_size != (uint32_t) new_size)
2721 mutex_lock(&info->device->mutex);
2722 if (write_subdevice == NULL ||
2723 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2724 write_subdevice->async == NULL) {
2725 mutex_unlock(&info->device->mutex);
2728 retval = resize_async_buffer(info->device, write_subdevice,
2729 write_subdevice->async, new_size);
2730 mutex_unlock(&info->device->mutex);
2737 static struct device_attribute dev_attr_write_buffer_kb = {
2739 .name = "write_buffer_kb",
2740 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
2741 .show = &show_write_buffer_kb,
2742 .store = &store_write_buffer_kb