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 * arg);
73 static int do_bufconfig_ioctl(comedi_device * dev, void *arg);
74 static int do_devinfo_ioctl(comedi_device * dev, comedi_devinfo * arg,
76 static int do_subdinfo_ioctl(comedi_device * dev, comedi_subdinfo * arg,
78 static int do_chaninfo_ioctl(comedi_device * dev, comedi_chaninfo * arg);
79 static int do_bufinfo_ioctl(comedi_device * dev, void *arg, void *file);
80 static int do_cmd_ioctl(comedi_device * dev, void *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, void *arg, void *file);
85 static int do_insnlist_ioctl(comedi_device * dev, void *arg, void *file);
86 static int do_insn_ioctl(comedi_device * dev, void *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, (void *)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, (void *)arg);
185 rc = do_devinfo_ioctl(dev, (void *)arg, file);
187 case COMEDI_SUBDINFO:
188 rc = do_subdinfo_ioctl(dev, (void *)arg, file);
190 case COMEDI_CHANINFO:
191 rc = do_chaninfo_ioctl(dev, (void *)arg);
193 case COMEDI_RANGEINFO:
194 rc = do_rangeinfo_ioctl(dev, (void *)arg);
197 rc = do_bufinfo_ioctl(dev, (void *)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, (void *)arg, file);
212 rc = do_cmdtest_ioctl(dev, (void *)arg, file);
214 case COMEDI_INSNLIST:
215 rc = do_insnlist_ioctl(dev, (void *)arg, file);
218 rc = do_insn_ioctl(dev, (void *)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, comedi_devconfig * arg)
250 unsigned char *aux_data = NULL;
253 if (!capable(CAP_SYS_ADMIN))
257 if (is_device_busy(dev))
261 struct module *driver_module = dev->driver->module;
262 comedi_device_detach(dev);
263 module_put(driver_module);
268 if (copy_from_user(&it, arg, sizeof(comedi_devconfig)))
271 it.board_name[COMEDI_NAMELEN - 1] = 0;
273 if (comedi_aux_data(it.options, 0) &&
274 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
276 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
280 aux_data = vmalloc(aux_len);
284 if (copy_from_user(aux_data,
285 comedi_aux_data(it.options, 0), aux_len)) {
289 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
290 (unsigned long)aux_data;
291 if (sizeof(void *) > sizeof(int)) {
292 bit_shift = sizeof(int) * 8;
293 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
294 ((unsigned long)aux_data) >> bit_shift;
296 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
299 ret = comedi_device_attach(dev, &it);
302 if(!try_module_get(dev->driver->module)) {
303 comedi_device_detach(dev);
316 buffer configuration ioctl
319 pointer to bufconfig structure
325 modified bufconfig at arg
328 static int do_bufconfig_ioctl(comedi_device * dev, void *arg)
335 if (copy_from_user(&bc, arg, sizeof(comedi_bufconfig)))
338 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
341 s = dev->subdevices + bc.subdevice;
345 DPRINTK("subdevice does not have async capability\n");
351 if (bc.maximum_size) {
352 if (!capable(CAP_SYS_ADMIN))
355 async->max_bufsize = bc.maximum_size;
359 retval = resize_async_buffer(dev, s, async, bc.size);
360 if(retval < 0) return retval;
363 bc.size = async->prealloc_bufsz;
364 bc.maximum_size = async->max_bufsize;
367 if (copy_to_user(arg, &bc, sizeof(comedi_bufconfig)))
378 pointer to devinfo structure
387 static int do_devinfo_ioctl(comedi_device * dev, comedi_devinfo * arg,
390 comedi_devinfo devinfo;
391 const unsigned minor = iminor(file->f_dentry->d_inode);
392 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
393 comedi_subdevice *read_subdev = comedi_get_read_subdevice(dev_file_info);
394 comedi_subdevice *write_subdev = comedi_get_write_subdevice(dev_file_info);
396 memset(&devinfo, 0, sizeof(devinfo));
398 /* fill devinfo structure */
399 devinfo.version_code = COMEDI_VERSION_CODE;
400 devinfo.n_subdevs = dev->n_subdevices;
401 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
402 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
405 devinfo.read_subdevice = read_subdev - dev->subdevices;
407 devinfo.read_subdevice = -1;
410 devinfo.write_subdevice = write_subdev - dev->subdevices;
412 devinfo.write_subdevice = -1;
415 if (copy_to_user(arg, &devinfo, sizeof(comedi_devinfo)))
426 pointer to array of subdevice info structures
432 array of subdevice info structures at arg
435 static int do_subdinfo_ioctl(comedi_device * dev, comedi_subdinfo * arg,
439 comedi_subdinfo *tmp, *us;
442 tmp = kcalloc(dev->n_subdevices, sizeof(comedi_subdinfo), GFP_KERNEL);
446 /* fill subdinfo structs */
447 for (i = 0; i < dev->n_subdevices; i++) {
448 s = dev->subdevices + i;
452 us->n_chan = s->n_chan;
453 us->subd_flags = s->subdev_flags;
454 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
455 us->subd_flags |= SDF_RUNNING;
456 #define TIMER_nanosec 5 /* backwards compatibility */
457 us->timer_type = TIMER_nanosec;
458 us->len_chanlist = s->len_chanlist;
459 us->maxdata = s->maxdata;
460 if (s->range_table) {
462 (i << 24) | (0 << 16) | (s->
463 range_table->length);
465 us->range_type = 0; /* XXX */
467 us->flags = s->flags;
470 us->subd_flags |= SDF_BUSY;
472 us->subd_flags |= SDF_BUSY_OWNER;
474 us->subd_flags |= SDF_LOCKED;
476 us->subd_flags |= SDF_LOCK_OWNER;
477 if (!s->maxdata && s->maxdata_list)
478 us->subd_flags |= SDF_MAXDATA;
480 us->subd_flags |= SDF_FLAGS;
481 if (s->range_table_list)
482 us->subd_flags |= SDF_RANGETYPE;
484 us->subd_flags |= SDF_CMD;
486 if (s->insn_bits != &insn_inval)
487 us->insn_bits_support = COMEDI_SUPPORTED;
489 us->insn_bits_support = COMEDI_UNSUPPORTED;
491 us->settling_time_0 = s->settling_time_0;
494 ret = copy_to_user(arg, tmp,
495 dev->n_subdevices * sizeof(comedi_subdinfo));
499 return ret ? -EFAULT : 0;
507 pointer to chaninfo structure
510 chaninfo structure at arg
513 arrays at elements of chaninfo structure
516 static int do_chaninfo_ioctl(comedi_device * dev, comedi_chaninfo * arg)
521 if (copy_from_user(&it, arg, sizeof(comedi_chaninfo)))
524 if (it.subdev >= dev->n_subdevices)
526 s = dev->subdevices + it.subdev;
528 if (it.maxdata_list) {
529 if (s->maxdata || !s->maxdata_list)
531 if (copy_to_user(it.maxdata_list, s->maxdata_list,
532 s->n_chan * sizeof(lsampl_t)))
539 if (copy_to_user(it.flaglist, s->flaglist,
540 s->n_chan * sizeof(unsigned int)))
547 if (!s->range_table_list)
549 for (i = 0; i < s->n_chan; i++) {
552 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
553 (s->range_table_list[i]->length);
554 put_user(x, it.rangelist + i);
556 //if(copy_to_user(it.rangelist,s->range_type_list,s->n_chan*sizeof(unsigned int)))
565 buffer information ioctl
568 pointer to bufinfo structure
574 modified bufinfo at arg
577 static int do_bufinfo_ioctl(comedi_device * dev, void *arg, void *file)
584 if (copy_from_user(&bi, arg, sizeof(comedi_bufinfo)))
587 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
590 s = dev->subdevices + bi.subdevice;
592 if (s->lock && s->lock != file)
597 DPRINTK("subdevice does not have async capability\n");
598 bi.buf_write_ptr = 0;
600 bi.buf_write_count = 0;
601 bi.buf_read_count = 0;
603 bi.bytes_written = 0;
608 bi.bytes_written = 0;
609 goto copyback_position;
614 if ((s->subdev_flags & SDF_CMD_READ) != 0) {
616 bi.bytes_read = comedi_buf_read_alloc(async,
618 comedi_buf_read_free(async, bi.bytes_read);
620 if (async->buf_write_count == async->buf_read_count) {
621 if (!(comedi_get_subdevice_runflags(s) & (SRF_RUNNING
623 do_become_nonbusy(dev, s);
625 if (bi.bytes_read == 0) {
626 if ((comedi_get_subdevice_runflags(s)
627 & (SRF_RUNNING | SRF_ERROR))
635 if ((s->subdev_flags & SDF_CMD_WRITE) != 0) {
636 if (bi.bytes_written) {
637 bi.bytes_written = comedi_buf_write_alloc(async,
639 comedi_buf_write_free(async, bi.bytes_written);
641 if (bi.bytes_written == 0) {
642 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
643 if ((comedi_get_subdevice_runflags(s)
655 bi.buf_write_count = async->buf_write_count;
656 bi.buf_write_ptr = async->buf_write_ptr;
657 bi.buf_read_count = async->buf_read_count;
658 bi.buf_read_ptr = async->buf_read_ptr;
661 if (copy_to_user(arg, &bi, sizeof(comedi_bufinfo)))
667 static int parse_insn(comedi_device * dev, comedi_insn * insn, lsampl_t * data,
671 * synchronous instructions
674 * pointer to sync cmd structure
677 * sync cmd struct at arg
684 static int do_insnlist_ioctl(comedi_device * dev, void *arg, void *file)
686 comedi_insnlist insnlist;
687 comedi_insn *insns = NULL;
688 lsampl_t *data = NULL;
689 unsigned int max_samples;
693 if (copy_from_user(&insnlist, arg, sizeof(comedi_insnlist)))
696 if (insnlist.n_insns <= ULONG_MAX / sizeof(comedi_insn))
697 insns = kmalloc(sizeof(comedi_insn) * insnlist.n_insns,
700 DPRINTK("kmalloc failed\n");
705 if (copy_from_user(insns, insnlist.insns,
706 sizeof(comedi_insn) * insnlist.n_insns)) {
707 DPRINTK("copy_from_user failed\n");
713 for (i = 0; i < insnlist.n_insns; i++) {
714 if (max_samples < insns[i].n)
715 max_samples = insns[i].n;
719 if (max_samples <= ULONG_MAX / sizeof(lsampl_t))
720 data = kmalloc(sizeof(lsampl_t) * max_samples,
723 DPRINTK("kmalloc failed\n");
729 for (i = 0; i < insnlist.n_insns; i++) {
730 if (insns[i].insn & INSN_MASK_WRITE) {
731 if (copy_from_user(data, insns[i].data,
732 insns[i].n * sizeof(lsampl_t))) {
733 DPRINTK("copy_from_user failed\n");
738 ret = parse_insn(dev, insns + i, data, file);
741 if (insns[i].insn & INSN_MASK_READ) {
742 if (copy_to_user(insns[i].data, data,
743 insns[i].n * sizeof(lsampl_t))) {
744 DPRINTK("copy_to_user failed\n");
764 static int check_insn_config_length(comedi_insn * insn, lsampl_t * data)
766 if(insn->n < 1) return -EINVAL;
769 case INSN_CONFIG_DIO_OUTPUT:
770 case INSN_CONFIG_DIO_INPUT:
771 case INSN_CONFIG_DISARM:
772 case INSN_CONFIG_RESET:
776 case INSN_CONFIG_ARM:
777 case INSN_CONFIG_DIO_QUERY:
778 case INSN_CONFIG_BLOCK_SIZE:
779 case INSN_CONFIG_FILTER:
780 case INSN_CONFIG_SERIAL_CLOCK:
781 case INSN_CONFIG_BIDIRECTIONAL_DATA:
782 case INSN_CONFIG_ALT_SOURCE:
783 case INSN_CONFIG_SET_COUNTER_MODE:
784 case INSN_CONFIG_8254_READ_STATUS:
785 case INSN_CONFIG_SET_ROUTING:
786 case INSN_CONFIG_GET_ROUTING:
787 case INSN_CONFIG_GET_PWM_STATUS:
788 case INSN_CONFIG_PWM_SET_PERIOD:
789 case INSN_CONFIG_PWM_GET_PERIOD:
793 case INSN_CONFIG_SET_GATE_SRC:
794 case INSN_CONFIG_GET_GATE_SRC:
795 case INSN_CONFIG_SET_CLOCK_SRC:
796 case INSN_CONFIG_GET_CLOCK_SRC:
797 case INSN_CONFIG_SET_OTHER_SRC:
798 case INSN_CONFIG_GET_COUNTER_STATUS:
799 case INSN_CONFIG_PWM_SET_H_BRIDGE:
800 case INSN_CONFIG_PWM_GET_H_BRIDGE:
801 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
805 case INSN_CONFIG_PWM_OUTPUT:
806 case INSN_CONFIG_ANALOG_TRIG:
810 //by default we allow the insn since we don't have checks for all possible cases yet
813 ("comedi: no check for data length of config insn id %i is implemented.\n"
814 " Add a check to %s in %s.\n"
815 " Assuming n=%i is correct.\n", data[0], __FUNCTION__,
823 static int parse_insn(comedi_device * dev, comedi_insn * insn, lsampl_t * data,
830 if (insn->insn & INSN_MASK_SPECIAL) {
831 /* a non-subdevice instruction */
833 switch (insn->insn) {
843 do_gettimeofday(&tv);
845 data[1] = tv.tv_usec;
851 if (insn->n != 1 || data[0] >= 100000) {
855 udelay(data[0] / 1000);
863 if (insn->subdev >= dev->n_subdevices) {
864 DPRINTK("%d not usable subdevice\n",
869 s = dev->subdevices + insn->subdev;
871 DPRINTK("no async\n");
875 if (!s->async->inttrig) {
876 DPRINTK("no inttrig\n");
880 ret = s->async->inttrig(dev, s, insn->data[0]);
885 DPRINTK("invalid insn\n");
890 /* a subdevice instruction */
893 if (insn->subdev >= dev->n_subdevices) {
894 DPRINTK("subdevice %d out of range\n", insn->subdev);
898 s = dev->subdevices + insn->subdev;
900 if (s->type == COMEDI_SUBD_UNUSED) {
901 DPRINTK("%d not usable subdevice\n", insn->subdev);
906 /* are we locked? (ioctl lock) */
907 if (s->lock && s->lock != file) {
908 DPRINTK("device locked\n");
913 if ((ret = check_chanlist(s, 1, &insn->chanspec)) < 0) {
915 DPRINTK("bad chanspec\n");
923 /* This looks arbitrary. It is. */
924 s->busy = &parse_insn;
925 switch (insn->insn) {
927 ret = s->insn_read(dev, s, insn, data);
930 maxdata = s->maxdata_list
931 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
933 for (i = 0; i < insn->n; ++i) {
934 if (data[i] > maxdata) {
936 DPRINTK("bad data value(s)\n");
941 ret = s->insn_write(dev, s, insn, data);
947 /* Most drivers ignore the base channel in
948 * insn->chanspec. Deal with it here if
949 * the subdevice has <= 32 channels. */
954 if (s->n_chan <= 32) {
955 shift = CR_CHAN(insn->chanspec);
964 ret = s->insn_bits(dev, s, insn, data);
971 ret = check_insn_config_length(insn, data);
974 ret = s->insn_config(dev, s, insn, data);
990 * synchronous instructions
996 * comedi_insn struct at arg
1002 static int do_insn_ioctl(comedi_device * dev, void *arg, void *file)
1005 lsampl_t *data = NULL;
1008 if (copy_from_user(&insn, arg, sizeof(comedi_insn))) {
1014 if (insn.n <= ULONG_MAX / sizeof(lsampl_t))
1015 data = kmalloc(sizeof(lsampl_t) * insn.n, GFP_KERNEL);
1022 if (insn.insn & INSN_MASK_WRITE) {
1023 if (copy_from_user(data, insn.data, insn.n * sizeof(lsampl_t))) {
1028 ret = parse_insn(dev, &insn, data, file);
1031 if (insn.insn & INSN_MASK_READ) {
1032 if (copy_to_user(insn.data, data, insn.n * sizeof(lsampl_t))) {
1051 pointer to cmd structure
1054 cmd structure at arg
1058 modified cmd structure at arg
1061 static int do_cmd_ioctl(comedi_device * dev, void *arg, void *file)
1063 comedi_cmd user_cmd;
1064 comedi_subdevice *s;
1065 comedi_async *async;
1067 unsigned int *chanlist_saver = NULL;
1069 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
1070 DPRINTK("bad cmd address\n");
1073 // save user's chanlist pointer so it can be restored later
1074 chanlist_saver = user_cmd.chanlist;
1076 if (user_cmd.subdev >= dev->n_subdevices) {
1077 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1081 s = dev->subdevices + user_cmd.subdev;
1084 if (s->type == COMEDI_SUBD_UNUSED) {
1085 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1089 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1090 DPRINTK("subdevice %i does not support commands\n",
1095 /* are we locked? (ioctl lock) */
1096 if (s->lock && s->lock != file) {
1097 DPRINTK("subdevice locked\n");
1103 DPRINTK("subdevice busy\n");
1108 /* make sure channel/gain list isn't too long */
1109 if (user_cmd.chanlist_len > s->len_chanlist) {
1110 DPRINTK("channel/gain list too long %u > %d\n",
1111 user_cmd.chanlist_len, s->len_chanlist);
1116 /* make sure channel/gain list isn't too short */
1117 if (user_cmd.chanlist_len < 1) {
1118 DPRINTK("channel/gain list too short %u < 1\n",
1119 user_cmd.chanlist_len);
1124 if (async->cmd.chanlist)
1125 kfree(async->cmd.chanlist);
1126 async->cmd = user_cmd;
1127 async->cmd.data = NULL;
1128 /* load channel/gain list */
1129 if (async->cmd.chanlist_len <= ULONG_MAX / sizeof(int))
1130 async->cmd.chanlist =
1131 kmalloc(async->cmd.chanlist_len * sizeof(int),
1134 async->cmd.chanlist = NULL;
1135 if (!async->cmd.chanlist) {
1136 DPRINTK("allocation failed\n");
1141 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1142 async->cmd.chanlist_len * sizeof(int))) {
1143 DPRINTK("fault reading chanlist\n");
1148 /* make sure each element in channel/gain list is valid */
1149 if ((ret = check_chanlist(s, async->cmd.chanlist_len,
1150 async->cmd.chanlist)) < 0) {
1151 DPRINTK("bad chanlist\n");
1155 ret = s->do_cmdtest(dev, s, &async->cmd);
1157 if (async->cmd.flags & TRIG_BOGUS || ret) {
1158 DPRINTK("test returned %d\n", ret);
1159 user_cmd = async->cmd;
1160 // restore chanlist pointer before copying back
1161 user_cmd.chanlist = chanlist_saver;
1162 user_cmd.data = NULL;
1163 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1164 DPRINTK("fault writing cmd\n");
1172 if (!async->prealloc_bufsz) {
1174 DPRINTK("no buffer (?)\n");
1178 comedi_reset_async_buf(async);
1181 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1183 if (async->cmd.flags & TRIG_WAKE_EOS) {
1184 async->cb_mask |= COMEDI_CB_EOS;
1187 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1189 #ifdef CONFIG_COMEDI_RT
1190 if (async->cmd.flags & TRIG_RT) {
1191 if (comedi_switch_to_rt(dev) == 0)
1192 comedi_set_subdevice_runflags(s, SRF_RT, SRF_RT);
1196 ret = s->do_cmd(dev, s);
1201 do_become_nonbusy(dev, s);
1208 command testing ioctl
1211 pointer to cmd structure
1214 cmd structure at arg
1218 modified cmd structure at arg
1221 static int do_cmdtest_ioctl(comedi_device * dev, void *arg, void *file)
1223 comedi_cmd user_cmd;
1224 comedi_subdevice *s;
1226 unsigned int *chanlist = NULL;
1227 unsigned int *chanlist_saver = NULL;
1229 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
1230 DPRINTK("bad cmd address\n");
1233 // save user's chanlist pointer so it can be restored later
1234 chanlist_saver = user_cmd.chanlist;
1236 if (user_cmd.subdev >= dev->n_subdevices) {
1237 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1241 s = dev->subdevices + user_cmd.subdev;
1242 if (s->type == COMEDI_SUBD_UNUSED) {
1243 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1247 if (!s->do_cmd || !s->do_cmdtest) {
1248 DPRINTK("subdevice %i does not support commands\n",
1253 /* make sure channel/gain list isn't too long */
1254 if (user_cmd.chanlist_len > s->len_chanlist) {
1255 DPRINTK("channel/gain list too long %d > %d\n",
1256 user_cmd.chanlist_len, s->len_chanlist);
1261 /* load channel/gain list */
1262 if (user_cmd.chanlist) {
1263 if (user_cmd.chanlist_len <= ULONG_MAX / sizeof(int))
1265 kmalloc(user_cmd.chanlist_len * sizeof(int),
1268 DPRINTK("allocation failed\n");
1273 if (copy_from_user(chanlist, user_cmd.chanlist,
1274 user_cmd.chanlist_len * sizeof(int))) {
1275 DPRINTK("fault reading chanlist\n");
1280 /* make sure each element in channel/gain list is valid */
1281 if ((ret = check_chanlist(s, user_cmd.chanlist_len,
1283 DPRINTK("bad chanlist\n");
1287 user_cmd.chanlist = chanlist;
1290 ret = s->do_cmdtest(dev, s, &user_cmd);
1292 // restore chanlist pointer before copying back
1293 user_cmd.chanlist = chanlist_saver;
1295 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1296 DPRINTK("bad cmd address\n");
1322 static int do_lock_ioctl(comedi_device * dev, unsigned int arg, void *file)
1325 unsigned long flags;
1326 comedi_subdevice *s;
1328 if (arg >= dev->n_subdevices)
1330 s = dev->subdevices + arg;
1332 comedi_spin_lock_irqsave(&s->spin_lock, flags);
1333 if (s->busy || s->lock) {
1338 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
1345 ret = s->lock_f(dev, s);
1364 This function isn't protected by the semaphore, since
1365 we already own the lock.
1367 static int do_unlock_ioctl(comedi_device * dev, unsigned int arg, void *file)
1369 comedi_subdevice *s;
1371 if (arg >= dev->n_subdevices)
1373 s = dev->subdevices + arg;
1378 if (s->lock && s->lock != file)
1381 if (s->lock == file) {
1395 cancel acquisition ioctl
1407 static int do_cancel_ioctl(comedi_device * dev, unsigned int arg, void *file)
1409 comedi_subdevice *s;
1411 if (arg >= dev->n_subdevices)
1413 s = dev->subdevices + arg;
1414 if (s->async == NULL)
1417 if (s->lock && s->lock != file)
1423 if (s->busy != file)
1426 return do_cancel(dev, s);
1431 instructs driver to synchronize buffers
1443 static int do_poll_ioctl(comedi_device * dev, unsigned int arg, void *file)
1445 comedi_subdevice *s;
1447 if (arg >= dev->n_subdevices)
1449 s = dev->subdevices + arg;
1451 if (s->lock && s->lock != file)
1457 if (s->busy != file)
1461 return s->poll(dev, s);
1466 static int do_cancel(comedi_device * dev, comedi_subdevice * s)
1470 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1471 ret = s->cancel(dev, s);
1473 do_become_nonbusy(dev, s);
1478 void comedi_vm_open(struct vm_area_struct *area)
1480 comedi_async *async;
1483 async = area->vm_private_data;
1484 dev = async->subdevice->device;
1486 mutex_lock(&dev->mutex);
1487 async->mmap_count++;
1488 mutex_unlock(&dev->mutex);
1491 void comedi_vm_close(struct vm_area_struct *area)
1493 comedi_async *async;
1496 async = area->vm_private_data;
1497 dev = async->subdevice->device;
1499 mutex_lock(&dev->mutex);
1500 async->mmap_count--;
1501 mutex_unlock(&dev->mutex);
1504 static struct vm_operations_struct comedi_vm_ops = {
1505 .open = comedi_vm_open,
1506 .close = comedi_vm_close,
1509 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1511 const unsigned minor = iminor(file->f_dentry->d_inode);
1512 comedi_async *async = NULL;
1513 unsigned long start = vma->vm_start;
1518 comedi_subdevice *s;
1519 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1521 if (dev_file_info==NULL) return -ENODEV;
1522 dev = dev_file_info->device;
1523 if (dev==NULL) return -ENODEV;
1525 mutex_lock(&dev->mutex);
1526 if (!dev->attached) {
1527 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1531 if (vma->vm_flags & VM_WRITE) {
1532 s = comedi_get_write_subdevice(dev_file_info);
1534 s = comedi_get_read_subdevice(dev_file_info);
1541 if (async == NULL) {
1546 if (vma->vm_pgoff != 0) {
1547 DPRINTK("comedi: mmap() offset must be 0.\n");
1552 size = vma->vm_end - vma->vm_start;
1553 if (size > async->prealloc_bufsz) {
1557 if (size & (~PAGE_MASK)) {
1562 n_pages = size >> PAGE_SHIFT;
1563 for (i = 0; i < n_pages; ++i) {
1564 if (remap_pfn_range(vma, start,
1565 page_to_pfn(virt_to_page(async->
1566 buf_page_list[i].virt_addr)),
1567 PAGE_SIZE, PAGE_SHARED)) {
1574 vma->vm_ops = &comedi_vm_ops;
1575 vma->vm_private_data = async;
1577 async->mmap_count++;
1581 mutex_unlock(&dev->mutex);
1585 static unsigned int comedi_poll(struct file *file, poll_table * wait)
1587 unsigned int mask = 0;
1588 const unsigned minor = iminor(file->f_dentry->d_inode);
1589 comedi_subdevice *read_subdev;
1590 comedi_subdevice *write_subdev;
1591 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1593 if (dev_file_info==NULL) return -ENODEV;
1594 dev = dev_file_info->device;
1595 if (dev==NULL) return -ENODEV;
1597 mutex_lock(&dev->mutex);
1598 if (!dev->attached) {
1599 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1600 mutex_unlock(&dev->mutex);
1605 read_subdev = comedi_get_read_subdevice(dev_file_info);
1607 poll_wait(file, &read_subdev->async->wait_head, wait);
1608 if (!read_subdev->busy
1609 || comedi_buf_read_n_available(read_subdev->async) > 0
1610 || !(comedi_get_subdevice_runflags(read_subdev) &
1612 mask |= POLLIN | POLLRDNORM;
1615 write_subdev = comedi_get_write_subdevice(dev_file_info);
1617 poll_wait(file, &write_subdev->async->wait_head, wait);
1618 comedi_buf_write_alloc(write_subdev->async, write_subdev->async->prealloc_bufsz);
1619 if (!write_subdev->busy
1620 || !(comedi_get_subdevice_runflags(write_subdev) &
1622 || comedi_buf_write_n_allocated(write_subdev->async) >=
1623 bytes_per_sample(write_subdev->async->subdevice)) {
1624 mask |= POLLOUT | POLLWRNORM;
1628 mutex_unlock(&dev->mutex);
1632 static ssize_t comedi_write(struct file *file, const char *buf, size_t nbytes,
1635 comedi_subdevice *s;
1636 comedi_async *async;
1637 int n, m, count = 0, retval = 0;
1638 DECLARE_WAITQUEUE(wait, current);
1639 const unsigned minor = iminor(file->f_dentry->d_inode);
1640 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1642 if (dev_file_info==NULL) return -ENODEV;
1643 dev = dev_file_info->device;
1644 if (dev==NULL) return -ENODEV;
1646 if (!dev->attached) {
1647 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1652 s = comedi_get_write_subdevice(dev_file_info);
1667 if (s->busy != file) {
1671 add_wait_queue(&async->wait_head, &wait);
1672 while (nbytes > 0 && !retval) {
1673 set_current_state(TASK_INTERRUPTIBLE);
1675 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1677 if (comedi_get_subdevice_runflags(s) &
1683 do_become_nonbusy(dev, s);
1691 if (async->buf_write_ptr + m > async->prealloc_bufsz) {
1692 m = async->prealloc_bufsz - async->buf_write_ptr;
1694 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1695 if (m > comedi_buf_write_n_allocated(async)) {
1696 m = comedi_buf_write_n_allocated(async);
1702 if (file->f_flags & O_NONBLOCK) {
1707 if (signal_pending(current)) {
1708 retval = -ERESTARTSYS;
1714 if (s->busy != file) {
1721 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1727 comedi_buf_write_free(async, n);
1733 break; /* makes device work like a pipe */
1735 set_current_state(TASK_RUNNING);
1736 remove_wait_queue(&async->wait_head, &wait);
1739 return (count ? count : retval);
1742 static ssize_t comedi_read(struct file *file, char *buf, size_t nbytes,
1745 comedi_subdevice *s;
1746 comedi_async *async;
1747 int n, m, count = 0, retval = 0;
1748 DECLARE_WAITQUEUE(wait, current);
1749 const unsigned minor = iminor(file->f_dentry->d_inode);
1750 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1752 if (dev_file_info==NULL) return -ENODEV;
1753 dev = dev_file_info->device;
1754 if (dev==NULL) return -ENODEV;
1756 if (!dev->attached) {
1757 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1762 s = comedi_get_read_subdevice(dev_file_info);
1776 if (s->busy != file) {
1781 add_wait_queue(&async->wait_head, &wait);
1782 while (nbytes > 0 && !retval) {
1783 set_current_state(TASK_INTERRUPTIBLE);
1787 m = comedi_buf_read_n_available(async);
1788 //printk("%d available\n",m);
1789 if (async->buf_read_ptr + m > async->prealloc_bufsz) {
1790 m = async->prealloc_bufsz - async->buf_read_ptr;
1792 //printk("%d contiguous\n",m);
1797 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1798 do_become_nonbusy(dev, s);
1799 if (comedi_get_subdevice_runflags(s) &
1807 if (file->f_flags & O_NONBLOCK) {
1812 if (signal_pending(current)) {
1813 retval = -ERESTARTSYS;
1820 if (s->busy != file) {
1826 m = copy_to_user(buf, async->prealloc_buf +
1827 async->buf_read_ptr, n);
1833 comedi_buf_read_alloc(async, n);
1834 comedi_buf_read_free(async, n);
1840 break; /* makes device work like a pipe */
1842 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
1843 async->buf_read_count - async->buf_write_count == 0) {
1844 do_become_nonbusy(dev, s);
1846 set_current_state(TASK_RUNNING);
1847 remove_wait_queue(&async->wait_head, &wait);
1850 return (count ? count : retval);
1854 This function restores a subdevice to an idle state.
1856 void do_become_nonbusy(comedi_device * dev, comedi_subdevice * s)
1858 comedi_async *async = s->async;
1860 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1861 #ifdef CONFIG_COMEDI_RT
1862 if (comedi_get_subdevice_runflags(s) & SRF_RT) {
1863 comedi_switch_to_non_rt(dev);
1864 comedi_set_subdevice_runflags(s, SRF_RT, 0);
1868 comedi_reset_async_buf(async);
1869 async->inttrig = NULL;
1871 printk("BUG: (?) do_become_nonbusy called with async=0\n");
1877 static int comedi_open(struct inode *inode, struct file *file)
1879 const unsigned minor = iminor(inode);
1880 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1881 comedi_device *dev = dev_file_info ? dev_file_info->device : NULL;
1883 DPRINTK("invalid minor number\n");
1887 /* This is slightly hacky, but we want module autoloading
1889 * case: user opens device, attached -> ok
1890 * case: user opens device, unattached, in_request_module=0 -> autoload
1891 * case: user opens device, unattached, in_request_module=1 -> fail
1892 * case: root opens device, attached -> ok
1893 * case: root opens device, unattached, in_request_module=1 -> ok
1894 * (typically called from modprobe)
1895 * case: root opens device, unattached, in_request_module=0 -> autoload
1897 * The last could be changed to "-> ok", which would deny root
1900 mutex_lock(&dev->mutex);
1903 if (!capable(CAP_SYS_MODULE) && dev->in_request_module) {
1904 DPRINTK("in request module\n");
1905 mutex_unlock(&dev->mutex);
1908 if (capable(CAP_SYS_MODULE) && dev->in_request_module)
1911 dev->in_request_module = 1;
1914 mutex_unlock(&dev->mutex);
1915 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1916 mutex_lock(&dev->mutex);
1919 dev->in_request_module = 0;
1921 if (!dev->attached && !capable(CAP_SYS_MODULE)) {
1922 DPRINTK("not attached and not CAP_SYS_MODULE\n");
1923 mutex_unlock(&dev->mutex);
1927 __module_get(THIS_MODULE);
1929 if (dev->attached) {
1930 if (!try_module_get(dev->driver->module)) {
1931 module_put(THIS_MODULE);
1932 mutex_unlock(&dev->mutex);
1937 if (dev->attached && dev->use_count == 0 && dev->open) {
1938 int rc = dev->open(dev);
1941 module_put(dev->driver->module);
1942 module_put(THIS_MODULE);
1943 mutex_unlock(&dev->mutex);
1950 mutex_unlock(&dev->mutex);
1955 static int comedi_close(struct inode *inode, struct file *file)
1957 const unsigned minor = iminor(inode);
1958 comedi_subdevice *s = NULL;
1960 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1962 if (dev_file_info==NULL) return -ENODEV;
1963 dev = dev_file_info->device;
1964 if (dev==NULL) return -ENODEV;
1966 mutex_lock(&dev->mutex);
1968 if (dev->subdevices) {
1969 for (i = 0; i < dev->n_subdevices; i++) {
1970 s = dev->subdevices + i;
1972 if (s->busy == file) {
1975 if (s->lock == file) {
1980 if (dev->attached && dev->use_count == 1 && dev->close) {
1984 module_put(THIS_MODULE);
1985 if (dev->attached) {
1986 module_put(dev->driver->module);
1991 mutex_unlock(&dev->mutex);
1993 if (file->f_flags & FASYNC) {
1994 comedi_fasync(-1, file, 0);
2000 static int comedi_fasync(int fd, struct file *file, int on)
2002 const unsigned minor = iminor(file->f_dentry->d_inode);
2003 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
2005 if (dev_file_info==NULL) return -ENODEV;
2006 dev = dev_file_info->device;
2007 if (dev==NULL) return -ENODEV;
2009 return fasync_helper(fd, file, on, &dev->async_queue);
2012 const struct file_operations comedi_fops = {
2014 #ifdef HAVE_UNLOCKED_IOCTL
2015 unlocked_ioctl:comedi_unlocked_ioctl,
2019 #ifdef HAVE_COMPAT_IOCTL
2020 compat_ioctl:comedi_compat_ioctl,
2023 release:comedi_close,
2028 fasync:comedi_fasync,
2031 struct class *comedi_class = NULL;
2032 static struct cdev comedi_cdev;
2034 static void comedi_cleanup_legacy_minors(void)
2037 for (i = 0; i < comedi_num_legacy_minors; i++) {
2038 comedi_free_board_minor(i);
2042 static int __init comedi_init(void)
2047 printk("comedi: version " COMEDI_RELEASE
2048 " - http://www.comedi.org\n");
2050 if(comedi_num_legacy_minors < 0 || comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS)
2052 printk("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid "
2053 "values are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
2056 /* comedi is unusable if both comedi_autoconfig and comedi_num_legacy_minors are zero,
2057 so we might as well adjust the defaults in that case */
2058 if(comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2060 comedi_num_legacy_minors = 16;
2063 memset(comedi_file_info_table, 0, sizeof(struct comedi_device_file_info*) * COMEDI_NUM_MINORS);
2065 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2066 COMEDI_NUM_MINORS, "comedi");
2069 cdev_init(&comedi_cdev, &comedi_fops);
2070 comedi_cdev.owner = THIS_MODULE;
2071 kobject_set_name(&comedi_cdev.kobj, "comedi");
2072 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2073 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2077 comedi_class = class_create(THIS_MODULE, "comedi");
2078 if (IS_ERR(comedi_class)) {
2079 printk("comedi: failed to create class");
2080 cdev_del(&comedi_cdev);
2081 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2083 return PTR_ERR(comedi_class);
2086 /* XXX requires /proc interface */
2089 // create devices files for legacy/manual use
2090 for (i = 0; i < comedi_num_legacy_minors; i++) {
2092 minor = comedi_alloc_board_minor(NULL);
2095 comedi_cleanup_legacy_minors();
2096 cdev_del(&comedi_cdev);
2097 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2105 comedi_register_ioctl32();
2110 static void __exit comedi_cleanup(void)
2114 comedi_cleanup_legacy_minors();
2115 for(i = 0; i < COMEDI_NUM_MINORS; ++i)
2117 BUG_ON(comedi_file_info_table[i]);
2120 class_destroy(comedi_class);
2121 cdev_del(&comedi_cdev);
2122 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2124 comedi_proc_cleanup();
2126 comedi_rt_cleanup();
2128 comedi_unregister_ioctl32();
2131 module_init(comedi_init);
2132 module_exit(comedi_cleanup);
2134 void comedi_error(const comedi_device * dev, const char *s)
2136 rt_printk("comedi%d: %s: %s\n", dev->minor, dev->driver->driver_name,
2140 void comedi_event(comedi_device * dev, comedi_subdevice * s)
2142 comedi_async *async = s->async;
2143 unsigned runflags = 0;
2144 unsigned runflags_mask = 0;
2146 //DPRINTK("comedi_event 0x%x\n",mask);
2148 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2152 events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW))
2154 runflags_mask |= SRF_RUNNING;
2156 /* remember if an error event has occured, so an error
2157 * can be returned the next time the user does a read() */
2158 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2159 runflags_mask |= SRF_ERROR;
2160 runflags |= SRF_ERROR;
2162 if (runflags_mask) {
2163 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2164 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2167 if (async->cb_mask & s->async->events) {
2168 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2171 #ifdef CONFIG_COMEDI_RT
2173 comedi_rt_pend_wakeup(&async->wait_head);
2175 printk("BUG: comedi_event() code unreachable\n");
2178 wake_up_interruptible(&async->wait_head);
2179 if (s->subdev_flags & SDF_CMD_READ) {
2180 kill_fasync(&dev->async_queue, SIGIO,
2183 if (s->subdev_flags & SDF_CMD_WRITE) {
2184 kill_fasync(&dev->async_queue, SIGIO,
2190 async->cb_func(s->async->events, async->cb_arg);
2191 /* XXX bug here. If subdevice A is rt, and
2192 * subdevice B tries to callback to a normal
2193 * linux kernel function, it will be at the
2194 * wrong priority. Since this isn't very
2195 * common, I'm not going to worry about it. */
2198 s->async->events = 0;
2201 void comedi_set_subdevice_runflags(comedi_subdevice * s, unsigned mask,
2204 unsigned long flags;
2206 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2207 s->runflags &= ~mask;
2208 s->runflags |= (bits & mask);
2209 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2212 unsigned comedi_get_subdevice_runflags(comedi_subdevice * s)
2214 unsigned long flags;
2217 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2218 runflags = s->runflags;
2219 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2223 static int is_device_busy(comedi_device * dev)
2225 comedi_subdevice *s;
2231 for (i = 0; i < dev->n_subdevices; i++) {
2232 s = dev->subdevices + i;
2235 if (s->async && s->async->mmap_count)
2242 void comedi_device_init(comedi_device *dev)
2244 memset(dev, 0, sizeof(comedi_device));
2245 spin_lock_init(&dev->spinlock);
2246 mutex_init(&dev->mutex);
2250 void comedi_device_cleanup(comedi_device *dev)
2252 if(dev == NULL) return;
2253 mutex_lock(&dev->mutex);
2254 comedi_device_detach(dev);
2255 mutex_unlock(&dev->mutex);
2256 mutex_destroy(&dev->mutex);
2259 int comedi_alloc_board_minor(struct device *hardware_device)
2261 unsigned long flags;
2262 struct comedi_device_file_info *info;
2263 comedi_device_create_t *csdev;
2267 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2268 if(info == NULL) return -ENOMEM;
2269 info->device = kzalloc(sizeof(comedi_device), GFP_KERNEL);
2270 if(info->device == NULL)
2275 comedi_device_init(info->device);
2276 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2277 for(i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2279 if(comedi_file_info_table[i] == NULL)
2281 comedi_file_info_table[i] = info;
2285 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2286 if(i == COMEDI_NUM_BOARD_MINORS)
2288 comedi_device_cleanup(info->device);
2289 kfree(info->device);
2291 printk("comedi: error: ran out of minor numbers for board device files.\n");
2294 info->device->minor = i;
2295 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
2296 MKDEV(COMEDI_MAJOR, i), NULL, hardware_device, "comedi%i", i);
2297 if(!IS_ERR(csdev)) {
2298 info->device->class_dev = csdev;
2300 COMEDI_DEV_SET_DRVDATA(csdev, info);
2301 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_read_buffer_kb);
2304 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_read_buffer_kb.attr.name);
2305 comedi_free_board_minor(i);
2308 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_read_buffer_kb);
2311 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_read_buffer_kb.attr.name);
2312 comedi_free_board_minor(i);
2315 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_write_buffer_kb);
2318 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_write_buffer_kb.attr.name);
2319 comedi_free_board_minor(i);
2322 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_write_buffer_kb);
2325 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_write_buffer_kb.attr.name);
2326 comedi_free_board_minor(i);
2332 void comedi_free_board_minor(unsigned minor)
2334 unsigned long flags;
2335 struct comedi_device_file_info *info;
2337 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2338 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2339 info = comedi_file_info_table[minor];
2340 comedi_file_info_table[minor] = NULL;
2341 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2345 comedi_device *dev = info->device;
2350 COMEDI_DEVICE_DESTROY(comedi_class,
2351 MKDEV(COMEDI_MAJOR, dev->minor));
2353 comedi_device_cleanup(dev);
2360 int comedi_alloc_subdevice_minor(comedi_device *dev, comedi_subdevice *s)
2362 unsigned long flags;
2363 struct comedi_device_file_info *info;
2364 comedi_device_create_t *csdev;
2368 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2369 if(info == NULL) return -ENOMEM;
2371 info->read_subdevice = s;
2372 info->write_subdevice = s;
2373 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2374 for(i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i)
2376 if(comedi_file_info_table[i] == NULL)
2378 comedi_file_info_table[i] = info;
2382 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2383 if(i == COMEDI_NUM_MINORS)
2386 printk("comedi: error: ran out of minor numbers for board device files.\n");
2390 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
2391 MKDEV(COMEDI_MAJOR, i), NULL, NULL, "comedi%i_subd%i", dev->minor, (int)(s - dev->subdevices));
2394 s->class_dev = csdev;
2396 COMEDI_DEV_SET_DRVDATA(csdev, info);
2397 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_read_buffer_kb);
2400 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_read_buffer_kb.attr.name);
2401 comedi_free_subdevice_minor(s);
2404 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_read_buffer_kb);
2407 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_read_buffer_kb.attr.name);
2408 comedi_free_subdevice_minor(s);
2411 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_write_buffer_kb);
2414 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_write_buffer_kb.attr.name);
2415 comedi_free_subdevice_minor(s);
2418 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_write_buffer_kb);
2421 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_write_buffer_kb.attr.name);
2422 comedi_free_subdevice_minor(s);
2428 void comedi_free_subdevice_minor(comedi_subdevice *s)
2430 unsigned long flags;
2431 struct comedi_device_file_info *info;
2433 if(s == NULL) return;
2434 if(s->minor < 0) return;
2436 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2437 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2439 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2440 info = comedi_file_info_table[s->minor];
2441 comedi_file_info_table[s->minor] = NULL;
2442 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2446 COMEDI_DEVICE_DESTROY(comedi_class,
2447 MKDEV(COMEDI_MAJOR, s->minor));
2448 s->class_dev = NULL;
2453 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2455 unsigned long flags;
2456 struct comedi_device_file_info *info;
2458 BUG_ON(minor >= COMEDI_NUM_MINORS);
2459 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2460 info = comedi_file_info_table[minor];
2461 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2465 static int resize_async_buffer(comedi_device *dev,
2466 comedi_subdevice *s, comedi_async *async, unsigned new_size)
2470 if (new_size > async->max_bufsize)
2474 DPRINTK("subdevice is busy, cannot resize buffer\n");
2477 if (async->mmap_count) {
2478 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2482 if (!async->prealloc_buf)
2485 /* make sure buffer is an integral number of pages
2487 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2489 retval = comedi_buf_alloc(dev, s, new_size);
2493 if (s->buf_change) {
2494 retval = s->buf_change(dev, s, new_size);
2499 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2500 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
2504 // sysfs attribute file functions
2506 static const unsigned bytes_per_kibi = 1024;
2508 static COMEDI_DECLARE_ATTR_SHOW(show_max_read_buffer_kb, dev, buf)
2511 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2512 unsigned max_buffer_size_kb = 0;
2513 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2515 mutex_lock(&info->device->mutex);
2516 if(read_subdevice &&
2517 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2518 read_subdevice->async)
2520 max_buffer_size_kb = read_subdevice->async->max_bufsize / bytes_per_kibi;
2522 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2523 mutex_unlock(&info->device->mutex);
2528 static COMEDI_DECLARE_ATTR_STORE(store_max_read_buffer_kb, dev, buf, count)
2530 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2531 unsigned long new_max_size_kb;
2532 uint64_t new_max_size;
2533 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2535 if(strict_strtoul(buf, 10, &new_max_size_kb))
2539 if(new_max_size_kb != (uint32_t)new_max_size_kb) return -EINVAL;
2540 new_max_size = ((uint64_t)new_max_size_kb) * bytes_per_kibi;
2541 if(new_max_size != (uint32_t)new_max_size) return -EINVAL;
2543 mutex_lock(&info->device->mutex);
2544 if(read_subdevice == NULL ||
2545 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2546 read_subdevice->async == NULL)
2548 mutex_unlock(&info->device->mutex);
2551 read_subdevice->async->max_bufsize = new_max_size;
2552 mutex_unlock(&info->device->mutex);
2557 static COMEDI_DECLARE_ATTR_SHOW(show_read_buffer_kb, dev, buf)
2560 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2561 unsigned buffer_size_kb = 0;
2562 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2564 mutex_lock(&info->device->mutex);
2565 if(read_subdevice &&
2566 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2567 read_subdevice->async)
2569 buffer_size_kb = read_subdevice->async->prealloc_bufsz / bytes_per_kibi;
2571 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2572 mutex_unlock(&info->device->mutex);
2577 static COMEDI_DECLARE_ATTR_STORE(store_read_buffer_kb, dev, buf, count)
2579 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2580 unsigned long new_size_kb;
2583 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2585 if(strict_strtoul(buf, 10, &new_size_kb))
2589 if(new_size_kb != (uint32_t)new_size_kb) return -EINVAL;
2590 new_size = ((uint64_t)new_size_kb) * bytes_per_kibi;
2591 if(new_size != (uint32_t)new_size) return -EINVAL;
2593 mutex_lock(&info->device->mutex);
2594 if(read_subdevice == NULL ||
2595 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2596 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);
2605 if(retval < 0) return retval;
2609 static COMEDI_DECLARE_ATTR_SHOW(show_max_write_buffer_kb, dev, buf)
2612 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2613 unsigned max_buffer_size_kb = 0;
2614 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2616 mutex_lock(&info->device->mutex);
2617 if(write_subdevice &&
2618 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2619 write_subdevice->async)
2621 max_buffer_size_kb = write_subdevice->async->max_bufsize / bytes_per_kibi;
2623 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2624 mutex_unlock(&info->device->mutex);
2629 static COMEDI_DECLARE_ATTR_STORE(store_max_write_buffer_kb, dev, buf, count)
2631 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2632 unsigned long new_max_size_kb;
2633 uint64_t new_max_size;
2634 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2636 if(strict_strtoul(buf, 10, &new_max_size_kb))
2640 if(new_max_size_kb != (uint32_t)new_max_size_kb) return -EINVAL;
2641 new_max_size = ((uint64_t)new_max_size_kb) * bytes_per_kibi;
2642 if(new_max_size != (uint32_t)new_max_size) return -EINVAL;
2644 mutex_lock(&info->device->mutex);
2645 if(write_subdevice == NULL ||
2646 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2647 write_subdevice->async == NULL)
2649 mutex_unlock(&info->device->mutex);
2652 write_subdevice->async->max_bufsize = new_max_size;
2653 mutex_unlock(&info->device->mutex);
2658 static COMEDI_DECLARE_ATTR_SHOW(show_write_buffer_kb, dev, buf)
2661 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2662 unsigned buffer_size_kb = 0;
2663 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2665 mutex_lock(&info->device->mutex);
2666 if(write_subdevice &&
2667 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2668 write_subdevice->async)
2670 buffer_size_kb = write_subdevice->async->prealloc_bufsz / bytes_per_kibi;
2672 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2673 mutex_unlock(&info->device->mutex);
2678 static COMEDI_DECLARE_ATTR_STORE(store_write_buffer_kb, dev, buf, count)
2680 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2681 unsigned long new_size_kb;
2684 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2686 if(strict_strtoul(buf, 10, &new_size_kb))
2690 if(new_size_kb != (uint32_t)new_size_kb) return -EINVAL;
2691 new_size = ((uint64_t)new_size_kb) * bytes_per_kibi;
2692 if(new_size != (uint32_t)new_size) return -EINVAL;
2694 mutex_lock(&info->device->mutex);
2695 if(write_subdevice == NULL ||
2696 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2697 write_subdevice->async == NULL)
2699 mutex_unlock(&info->device->mutex);
2702 retval = resize_async_buffer(info->device, write_subdevice,
2703 write_subdevice->async, new_size);
2704 mutex_unlock(&info->device->mutex);
2706 if(retval < 0) return retval;