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 async->cmd.chanlist =
1130 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1131 if (!async->cmd.chanlist) {
1132 DPRINTK("allocation failed\n");
1137 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1138 async->cmd.chanlist_len * sizeof(int))) {
1139 DPRINTK("fault reading chanlist\n");
1144 /* make sure each element in channel/gain list is valid */
1145 if ((ret = check_chanlist(s, async->cmd.chanlist_len,
1146 async->cmd.chanlist)) < 0) {
1147 DPRINTK("bad chanlist\n");
1151 ret = s->do_cmdtest(dev, s, &async->cmd);
1153 if (async->cmd.flags & TRIG_BOGUS || ret) {
1154 DPRINTK("test returned %d\n", ret);
1155 user_cmd = async->cmd;
1156 // restore chanlist pointer before copying back
1157 user_cmd.chanlist = chanlist_saver;
1158 user_cmd.data = NULL;
1159 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1160 DPRINTK("fault writing cmd\n");
1168 if (!async->prealloc_bufsz) {
1170 DPRINTK("no buffer (?)\n");
1174 comedi_reset_async_buf(async);
1177 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1179 if (async->cmd.flags & TRIG_WAKE_EOS) {
1180 async->cb_mask |= COMEDI_CB_EOS;
1183 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1185 #ifdef CONFIG_COMEDI_RT
1186 if (async->cmd.flags & TRIG_RT) {
1187 if (comedi_switch_to_rt(dev) == 0)
1188 comedi_set_subdevice_runflags(s, SRF_RT, SRF_RT);
1192 ret = s->do_cmd(dev, s);
1197 do_become_nonbusy(dev, s);
1204 command testing ioctl
1207 pointer to cmd structure
1210 cmd structure at arg
1214 modified cmd structure at arg
1217 static int do_cmdtest_ioctl(comedi_device * dev, void *arg, void *file)
1219 comedi_cmd user_cmd;
1220 comedi_subdevice *s;
1222 unsigned int *chanlist = NULL;
1223 unsigned int *chanlist_saver = NULL;
1225 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
1226 DPRINTK("bad cmd address\n");
1229 // save user's chanlist pointer so it can be restored later
1230 chanlist_saver = user_cmd.chanlist;
1232 if (user_cmd.subdev >= dev->n_subdevices) {
1233 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1237 s = dev->subdevices + user_cmd.subdev;
1238 if (s->type == COMEDI_SUBD_UNUSED) {
1239 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1243 if (!s->do_cmd || !s->do_cmdtest) {
1244 DPRINTK("subdevice %i does not support commands\n",
1249 /* make sure channel/gain list isn't too long */
1250 if (user_cmd.chanlist_len > s->len_chanlist) {
1251 DPRINTK("channel/gain list too long %d > %d\n",
1252 user_cmd.chanlist_len, s->len_chanlist);
1257 /* load channel/gain list */
1258 if (user_cmd.chanlist) {
1260 kmalloc(user_cmd.chanlist_len * sizeof(int),
1263 DPRINTK("allocation failed\n");
1268 if (copy_from_user(chanlist, user_cmd.chanlist,
1269 user_cmd.chanlist_len * sizeof(int))) {
1270 DPRINTK("fault reading chanlist\n");
1275 /* make sure each element in channel/gain list is valid */
1276 if ((ret = check_chanlist(s, user_cmd.chanlist_len,
1278 DPRINTK("bad chanlist\n");
1282 user_cmd.chanlist = chanlist;
1285 ret = s->do_cmdtest(dev, s, &user_cmd);
1287 // restore chanlist pointer before copying back
1288 user_cmd.chanlist = chanlist_saver;
1290 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1291 DPRINTK("bad cmd address\n");
1317 static int do_lock_ioctl(comedi_device * dev, unsigned int arg, void *file)
1320 unsigned long flags;
1321 comedi_subdevice *s;
1323 if (arg >= dev->n_subdevices)
1325 s = dev->subdevices + arg;
1327 comedi_spin_lock_irqsave(&s->spin_lock, flags);
1328 if (s->busy || s->lock) {
1333 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
1340 ret = s->lock_f(dev, s);
1359 This function isn't protected by the semaphore, since
1360 we already own the lock.
1362 static int do_unlock_ioctl(comedi_device * dev, unsigned int arg, void *file)
1364 comedi_subdevice *s;
1366 if (arg >= dev->n_subdevices)
1368 s = dev->subdevices + arg;
1373 if (s->lock && s->lock != file)
1376 if (s->lock == file) {
1390 cancel acquisition ioctl
1402 static int do_cancel_ioctl(comedi_device * dev, unsigned int arg, void *file)
1404 comedi_subdevice *s;
1406 if (arg >= dev->n_subdevices)
1408 s = dev->subdevices + arg;
1409 if (s->async == NULL)
1412 if (s->lock && s->lock != file)
1418 if (s->busy != file)
1421 return do_cancel(dev, s);
1426 instructs driver to synchronize buffers
1438 static int do_poll_ioctl(comedi_device * dev, unsigned int arg, void *file)
1440 comedi_subdevice *s;
1442 if (arg >= dev->n_subdevices)
1444 s = dev->subdevices + arg;
1446 if (s->lock && s->lock != file)
1452 if (s->busy != file)
1456 return s->poll(dev, s);
1461 static int do_cancel(comedi_device * dev, comedi_subdevice * s)
1465 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1466 ret = s->cancel(dev, s);
1468 do_become_nonbusy(dev, s);
1473 void comedi_vm_open(struct vm_area_struct *area)
1475 comedi_async *async;
1478 async = area->vm_private_data;
1479 dev = async->subdevice->device;
1481 mutex_lock(&dev->mutex);
1482 async->mmap_count++;
1483 mutex_unlock(&dev->mutex);
1486 void comedi_vm_close(struct vm_area_struct *area)
1488 comedi_async *async;
1491 async = area->vm_private_data;
1492 dev = async->subdevice->device;
1494 mutex_lock(&dev->mutex);
1495 async->mmap_count--;
1496 mutex_unlock(&dev->mutex);
1499 static struct vm_operations_struct comedi_vm_ops = {
1500 .open = comedi_vm_open,
1501 .close = comedi_vm_close,
1504 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1506 const unsigned minor = iminor(file->f_dentry->d_inode);
1507 comedi_async *async = NULL;
1508 unsigned long start = vma->vm_start;
1513 comedi_subdevice *s;
1514 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1516 if (dev_file_info==NULL) return -ENODEV;
1517 dev = dev_file_info->device;
1518 if (dev==NULL) return -ENODEV;
1520 mutex_lock(&dev->mutex);
1521 if (!dev->attached) {
1522 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1526 if (vma->vm_flags & VM_WRITE) {
1527 s = comedi_get_write_subdevice(dev_file_info);
1529 s = comedi_get_read_subdevice(dev_file_info);
1536 if (async == NULL) {
1541 if (vma->vm_pgoff != 0) {
1542 DPRINTK("comedi: mmap() offset must be 0.\n");
1547 size = vma->vm_end - vma->vm_start;
1548 if (size > async->prealloc_bufsz) {
1552 if (size & (~PAGE_MASK)) {
1557 n_pages = size >> PAGE_SHIFT;
1558 for (i = 0; i < n_pages; ++i) {
1559 if (remap_pfn_range(vma, start,
1560 page_to_pfn(virt_to_page(async->
1561 buf_page_list[i].virt_addr)),
1562 PAGE_SIZE, PAGE_SHARED)) {
1569 vma->vm_ops = &comedi_vm_ops;
1570 vma->vm_private_data = async;
1572 async->mmap_count++;
1576 mutex_unlock(&dev->mutex);
1580 static unsigned int comedi_poll(struct file *file, poll_table * wait)
1582 unsigned int mask = 0;
1583 const unsigned minor = iminor(file->f_dentry->d_inode);
1584 comedi_subdevice *read_subdev;
1585 comedi_subdevice *write_subdev;
1586 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1588 if (dev_file_info==NULL) return -ENODEV;
1589 dev = dev_file_info->device;
1590 if (dev==NULL) return -ENODEV;
1592 mutex_lock(&dev->mutex);
1593 if (!dev->attached) {
1594 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1595 mutex_unlock(&dev->mutex);
1600 read_subdev = comedi_get_read_subdevice(dev_file_info);
1602 poll_wait(file, &read_subdev->async->wait_head, wait);
1603 if (!read_subdev->busy
1604 || comedi_buf_read_n_available(read_subdev->async) > 0
1605 || !(comedi_get_subdevice_runflags(read_subdev) &
1607 mask |= POLLIN | POLLRDNORM;
1610 write_subdev = comedi_get_write_subdevice(dev_file_info);
1612 poll_wait(file, &write_subdev->async->wait_head, wait);
1613 comedi_buf_write_alloc(write_subdev->async, write_subdev->async->prealloc_bufsz);
1614 if (!write_subdev->busy
1615 || !(comedi_get_subdevice_runflags(write_subdev) &
1617 || comedi_buf_write_n_allocated(write_subdev->async) >=
1618 bytes_per_sample(write_subdev->async->subdevice)) {
1619 mask |= POLLOUT | POLLWRNORM;
1623 mutex_unlock(&dev->mutex);
1627 static ssize_t comedi_write(struct file *file, const char *buf, size_t nbytes,
1630 comedi_subdevice *s;
1631 comedi_async *async;
1632 int n, m, count = 0, retval = 0;
1633 DECLARE_WAITQUEUE(wait, current);
1634 const unsigned minor = iminor(file->f_dentry->d_inode);
1635 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1637 if (dev_file_info==NULL) return -ENODEV;
1638 dev = dev_file_info->device;
1639 if (dev==NULL) return -ENODEV;
1641 if (!dev->attached) {
1642 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1647 s = comedi_get_write_subdevice(dev_file_info);
1662 if (s->busy != file) {
1666 add_wait_queue(&async->wait_head, &wait);
1667 while (nbytes > 0 && !retval) {
1668 set_current_state(TASK_INTERRUPTIBLE);
1670 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1672 if (comedi_get_subdevice_runflags(s) &
1678 do_become_nonbusy(dev, s);
1686 if (async->buf_write_ptr + m > async->prealloc_bufsz) {
1687 m = async->prealloc_bufsz - async->buf_write_ptr;
1689 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1690 if (m > comedi_buf_write_n_allocated(async)) {
1691 m = comedi_buf_write_n_allocated(async);
1697 if (file->f_flags & O_NONBLOCK) {
1702 if (signal_pending(current)) {
1703 retval = -ERESTARTSYS;
1709 if (s->busy != file) {
1716 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1722 comedi_buf_write_free(async, n);
1728 break; /* makes device work like a pipe */
1730 set_current_state(TASK_RUNNING);
1731 remove_wait_queue(&async->wait_head, &wait);
1734 return (count ? count : retval);
1737 static ssize_t comedi_read(struct file *file, char *buf, size_t nbytes,
1740 comedi_subdevice *s;
1741 comedi_async *async;
1742 int n, m, count = 0, retval = 0;
1743 DECLARE_WAITQUEUE(wait, current);
1744 const unsigned minor = iminor(file->f_dentry->d_inode);
1745 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1747 if (dev_file_info==NULL) return -ENODEV;
1748 dev = dev_file_info->device;
1749 if (dev==NULL) return -ENODEV;
1751 if (!dev->attached) {
1752 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1757 s = comedi_get_read_subdevice(dev_file_info);
1771 if (s->busy != file) {
1776 add_wait_queue(&async->wait_head, &wait);
1777 while (nbytes > 0 && !retval) {
1778 set_current_state(TASK_INTERRUPTIBLE);
1782 m = comedi_buf_read_n_available(async);
1783 //printk("%d available\n",m);
1784 if (async->buf_read_ptr + m > async->prealloc_bufsz) {
1785 m = async->prealloc_bufsz - async->buf_read_ptr;
1787 //printk("%d contiguous\n",m);
1792 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1793 do_become_nonbusy(dev, s);
1794 if (comedi_get_subdevice_runflags(s) &
1802 if (file->f_flags & O_NONBLOCK) {
1807 if (signal_pending(current)) {
1808 retval = -ERESTARTSYS;
1815 if (s->busy != file) {
1821 m = copy_to_user(buf, async->prealloc_buf +
1822 async->buf_read_ptr, n);
1828 comedi_buf_read_alloc(async, n);
1829 comedi_buf_read_free(async, n);
1835 break; /* makes device work like a pipe */
1837 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
1838 async->buf_read_count - async->buf_write_count == 0) {
1839 do_become_nonbusy(dev, s);
1841 set_current_state(TASK_RUNNING);
1842 remove_wait_queue(&async->wait_head, &wait);
1845 return (count ? count : retval);
1849 This function restores a subdevice to an idle state.
1851 void do_become_nonbusy(comedi_device * dev, comedi_subdevice * s)
1853 comedi_async *async = s->async;
1855 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1856 #ifdef CONFIG_COMEDI_RT
1857 if (comedi_get_subdevice_runflags(s) & SRF_RT) {
1858 comedi_switch_to_non_rt(dev);
1859 comedi_set_subdevice_runflags(s, SRF_RT, 0);
1863 comedi_reset_async_buf(async);
1864 async->inttrig = NULL;
1866 printk("BUG: (?) do_become_nonbusy called with async=0\n");
1872 static int comedi_open(struct inode *inode, struct file *file)
1874 const unsigned minor = iminor(inode);
1875 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1876 comedi_device *dev = dev_file_info ? dev_file_info->device : NULL;
1878 DPRINTK("invalid minor number\n");
1882 /* This is slightly hacky, but we want module autoloading
1884 * case: user opens device, attached -> ok
1885 * case: user opens device, unattached, in_request_module=0 -> autoload
1886 * case: user opens device, unattached, in_request_module=1 -> fail
1887 * case: root opens device, attached -> ok
1888 * case: root opens device, unattached, in_request_module=1 -> ok
1889 * (typically called from modprobe)
1890 * case: root opens device, unattached, in_request_module=0 -> autoload
1892 * The last could be changed to "-> ok", which would deny root
1895 mutex_lock(&dev->mutex);
1898 if (!capable(CAP_SYS_MODULE) && dev->in_request_module) {
1899 DPRINTK("in request module\n");
1900 mutex_unlock(&dev->mutex);
1903 if (capable(CAP_SYS_MODULE) && dev->in_request_module)
1906 dev->in_request_module = 1;
1909 mutex_unlock(&dev->mutex);
1910 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1911 mutex_lock(&dev->mutex);
1914 dev->in_request_module = 0;
1916 if (!dev->attached && !capable(CAP_SYS_MODULE)) {
1917 DPRINTK("not attached and not CAP_SYS_MODULE\n");
1918 mutex_unlock(&dev->mutex);
1922 __module_get(THIS_MODULE);
1924 if (dev->attached) {
1925 if (!try_module_get(dev->driver->module)) {
1926 module_put(THIS_MODULE);
1927 mutex_unlock(&dev->mutex);
1932 if (dev->attached && dev->use_count == 0 && dev->open) {
1933 int rc = dev->open(dev);
1936 module_put(dev->driver->module);
1937 module_put(THIS_MODULE);
1938 mutex_unlock(&dev->mutex);
1945 mutex_unlock(&dev->mutex);
1950 static int comedi_close(struct inode *inode, struct file *file)
1952 const unsigned minor = iminor(inode);
1953 comedi_subdevice *s = NULL;
1955 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1957 if (dev_file_info==NULL) return -ENODEV;
1958 dev = dev_file_info->device;
1959 if (dev==NULL) return -ENODEV;
1961 mutex_lock(&dev->mutex);
1963 if (dev->subdevices) {
1964 for (i = 0; i < dev->n_subdevices; i++) {
1965 s = dev->subdevices + i;
1967 if (s->busy == file) {
1970 if (s->lock == file) {
1975 if (dev->attached && dev->use_count == 1 && dev->close) {
1979 module_put(THIS_MODULE);
1980 if (dev->attached) {
1981 module_put(dev->driver->module);
1986 mutex_unlock(&dev->mutex);
1988 if (file->f_flags & FASYNC) {
1989 comedi_fasync(-1, file, 0);
1995 static int comedi_fasync(int fd, struct file *file, int on)
1997 const unsigned minor = iminor(file->f_dentry->d_inode);
1998 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
2000 if (dev_file_info==NULL) return -ENODEV;
2001 dev = dev_file_info->device;
2002 if (dev==NULL) return -ENODEV;
2004 return fasync_helper(fd, file, on, &dev->async_queue);
2007 const struct file_operations comedi_fops = {
2009 #ifdef HAVE_UNLOCKED_IOCTL
2010 unlocked_ioctl:comedi_unlocked_ioctl,
2014 #ifdef HAVE_COMPAT_IOCTL
2015 compat_ioctl:comedi_compat_ioctl,
2018 release:comedi_close,
2023 fasync:comedi_fasync,
2026 struct class *comedi_class = NULL;
2027 static struct cdev comedi_cdev;
2029 static void comedi_cleanup_legacy_minors(void)
2032 for (i = 0; i < comedi_num_legacy_minors; i++) {
2033 comedi_free_board_minor(i);
2037 static int __init comedi_init(void)
2042 printk("comedi: version " COMEDI_RELEASE
2043 " - http://www.comedi.org\n");
2045 if(comedi_num_legacy_minors < 0 || comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS)
2047 printk("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid "
2048 "values are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
2051 /* comedi is unusable if both comedi_autoconfig and comedi_num_legacy_minors are zero,
2052 so we might as well adjust the defaults in that case */
2053 if(comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2055 comedi_num_legacy_minors = 16;
2058 memset(comedi_file_info_table, 0, sizeof(struct comedi_device_file_info*) * COMEDI_NUM_MINORS);
2060 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2061 COMEDI_NUM_MINORS, "comedi");
2064 cdev_init(&comedi_cdev, &comedi_fops);
2065 comedi_cdev.owner = THIS_MODULE;
2066 kobject_set_name(&comedi_cdev.kobj, "comedi");
2067 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2068 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2072 comedi_class = class_create(THIS_MODULE, "comedi");
2073 if (IS_ERR(comedi_class)) {
2074 printk("comedi: failed to create class");
2075 cdev_del(&comedi_cdev);
2076 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2078 return PTR_ERR(comedi_class);
2081 /* XXX requires /proc interface */
2084 // create devices files for legacy/manual use
2085 for (i = 0; i < comedi_num_legacy_minors; i++) {
2087 minor = comedi_alloc_board_minor(NULL);
2090 comedi_cleanup_legacy_minors();
2091 cdev_del(&comedi_cdev);
2092 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2100 comedi_register_ioctl32();
2105 static void __exit comedi_cleanup(void)
2109 comedi_cleanup_legacy_minors();
2110 for(i = 0; i < COMEDI_NUM_MINORS; ++i)
2112 BUG_ON(comedi_file_info_table[i]);
2115 class_destroy(comedi_class);
2116 cdev_del(&comedi_cdev);
2117 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2119 comedi_proc_cleanup();
2121 comedi_rt_cleanup();
2123 comedi_unregister_ioctl32();
2126 module_init(comedi_init);
2127 module_exit(comedi_cleanup);
2129 void comedi_error(const comedi_device * dev, const char *s)
2131 rt_printk("comedi%d: %s: %s\n", dev->minor, dev->driver->driver_name,
2135 void comedi_event(comedi_device * dev, comedi_subdevice * s)
2137 comedi_async *async = s->async;
2138 unsigned runflags = 0;
2139 unsigned runflags_mask = 0;
2141 //DPRINTK("comedi_event 0x%x\n",mask);
2143 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2147 events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW))
2149 runflags_mask |= SRF_RUNNING;
2151 /* remember if an error event has occured, so an error
2152 * can be returned the next time the user does a read() */
2153 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2154 runflags_mask |= SRF_ERROR;
2155 runflags |= SRF_ERROR;
2157 if (runflags_mask) {
2158 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2159 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2162 if (async->cb_mask & s->async->events) {
2163 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2166 #ifdef CONFIG_COMEDI_RT
2168 comedi_rt_pend_wakeup(&async->wait_head);
2170 printk("BUG: comedi_event() code unreachable\n");
2173 wake_up_interruptible(&async->wait_head);
2174 if (s->subdev_flags & SDF_CMD_READ) {
2175 kill_fasync(&dev->async_queue, SIGIO,
2178 if (s->subdev_flags & SDF_CMD_WRITE) {
2179 kill_fasync(&dev->async_queue, SIGIO,
2185 async->cb_func(s->async->events, async->cb_arg);
2186 /* XXX bug here. If subdevice A is rt, and
2187 * subdevice B tries to callback to a normal
2188 * linux kernel function, it will be at the
2189 * wrong priority. Since this isn't very
2190 * common, I'm not going to worry about it. */
2193 s->async->events = 0;
2196 void comedi_set_subdevice_runflags(comedi_subdevice * s, unsigned mask,
2199 unsigned long flags;
2201 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2202 s->runflags &= ~mask;
2203 s->runflags |= (bits & mask);
2204 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2207 unsigned comedi_get_subdevice_runflags(comedi_subdevice * s)
2209 unsigned long flags;
2212 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2213 runflags = s->runflags;
2214 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2218 static int is_device_busy(comedi_device * dev)
2220 comedi_subdevice *s;
2226 for (i = 0; i < dev->n_subdevices; i++) {
2227 s = dev->subdevices + i;
2230 if (s->async && s->async->mmap_count)
2237 void comedi_device_init(comedi_device *dev)
2239 memset(dev, 0, sizeof(comedi_device));
2240 spin_lock_init(&dev->spinlock);
2241 mutex_init(&dev->mutex);
2245 void comedi_device_cleanup(comedi_device *dev)
2247 if(dev == NULL) return;
2248 mutex_lock(&dev->mutex);
2249 comedi_device_detach(dev);
2250 mutex_unlock(&dev->mutex);
2251 mutex_destroy(&dev->mutex);
2254 int comedi_alloc_board_minor(struct device *hardware_device)
2256 unsigned long flags;
2257 struct comedi_device_file_info *info;
2258 comedi_device_create_t *csdev;
2262 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2263 if(info == NULL) return -ENOMEM;
2264 info->device = kzalloc(sizeof(comedi_device), GFP_KERNEL);
2265 if(info->device == NULL)
2270 comedi_device_init(info->device);
2271 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2272 for(i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2274 if(comedi_file_info_table[i] == NULL)
2276 comedi_file_info_table[i] = info;
2280 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2281 if(i == COMEDI_NUM_BOARD_MINORS)
2283 comedi_device_cleanup(info->device);
2284 kfree(info->device);
2286 printk("comedi: error: ran out of minor numbers for board device files.\n");
2289 info->device->minor = i;
2290 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
2291 MKDEV(COMEDI_MAJOR, i), NULL, hardware_device, "comedi%i", i);
2292 if(!IS_ERR(csdev)) {
2293 info->device->class_dev = csdev;
2295 COMEDI_DEV_SET_DRVDATA(csdev, info);
2296 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_read_buffer_kb);
2299 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_read_buffer_kb.attr.name);
2300 comedi_free_board_minor(i);
2303 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_read_buffer_kb);
2306 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_read_buffer_kb.attr.name);
2307 comedi_free_board_minor(i);
2310 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_write_buffer_kb);
2313 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_write_buffer_kb.attr.name);
2314 comedi_free_board_minor(i);
2317 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_write_buffer_kb);
2320 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_write_buffer_kb.attr.name);
2321 comedi_free_board_minor(i);
2327 void comedi_free_board_minor(unsigned minor)
2329 unsigned long flags;
2330 struct comedi_device_file_info *info;
2332 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2333 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2334 info = comedi_file_info_table[minor];
2335 comedi_file_info_table[minor] = NULL;
2336 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2340 comedi_device *dev = info->device;
2345 COMEDI_DEVICE_DESTROY(comedi_class,
2346 MKDEV(COMEDI_MAJOR, dev->minor));
2348 comedi_device_cleanup(dev);
2355 int comedi_alloc_subdevice_minor(comedi_device *dev, comedi_subdevice *s)
2357 unsigned long flags;
2358 struct comedi_device_file_info *info;
2359 comedi_device_create_t *csdev;
2363 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2364 if(info == NULL) return -ENOMEM;
2366 info->read_subdevice = s;
2367 info->write_subdevice = s;
2368 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2369 for(i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i)
2371 if(comedi_file_info_table[i] == NULL)
2373 comedi_file_info_table[i] = info;
2377 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2378 if(i == COMEDI_NUM_MINORS)
2381 printk("comedi: error: ran out of minor numbers for board device files.\n");
2385 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
2386 MKDEV(COMEDI_MAJOR, i), NULL, NULL, "comedi%i_subd%i", dev->minor, (int)(s - dev->subdevices));
2389 s->class_dev = csdev;
2391 COMEDI_DEV_SET_DRVDATA(csdev, info);
2392 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_read_buffer_kb);
2395 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_read_buffer_kb.attr.name);
2396 comedi_free_subdevice_minor(s);
2399 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_read_buffer_kb);
2402 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_read_buffer_kb.attr.name);
2403 comedi_free_subdevice_minor(s);
2406 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_max_write_buffer_kb);
2409 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_write_buffer_kb.attr.name);
2410 comedi_free_subdevice_minor(s);
2413 retval = COMEDI_DEVICE_CREATE_FILE(csdev, &dev_attr_write_buffer_kb);
2416 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_write_buffer_kb.attr.name);
2417 comedi_free_subdevice_minor(s);
2423 void comedi_free_subdevice_minor(comedi_subdevice *s)
2425 unsigned long flags;
2426 struct comedi_device_file_info *info;
2428 if(s == NULL) return;
2429 if(s->minor < 0) return;
2431 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2432 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2434 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2435 info = comedi_file_info_table[s->minor];
2436 comedi_file_info_table[s->minor] = NULL;
2437 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2441 COMEDI_DEVICE_DESTROY(comedi_class,
2442 MKDEV(COMEDI_MAJOR, s->minor));
2443 s->class_dev = NULL;
2448 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2450 unsigned long flags;
2451 struct comedi_device_file_info *info;
2453 BUG_ON(minor >= COMEDI_NUM_MINORS);
2454 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2455 info = comedi_file_info_table[minor];
2456 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2460 static int resize_async_buffer(comedi_device *dev,
2461 comedi_subdevice *s, comedi_async *async, unsigned new_size)
2465 if (new_size > async->max_bufsize)
2469 DPRINTK("subdevice is busy, cannot resize buffer\n");
2472 if (async->mmap_count) {
2473 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2477 if (!async->prealloc_buf)
2480 /* make sure buffer is an integral number of pages
2482 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2484 retval = comedi_buf_alloc(dev, s, new_size);
2488 if (s->buf_change) {
2489 retval = s->buf_change(dev, s, new_size);
2494 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2495 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
2499 // sysfs attribute file functions
2501 static const unsigned bytes_per_kibi = 1024;
2503 static COMEDI_DECLARE_ATTR_SHOW(show_max_read_buffer_kb, dev, buf)
2506 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2507 unsigned max_buffer_size_kb = 0;
2508 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2510 mutex_lock(&info->device->mutex);
2511 if(read_subdevice &&
2512 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2513 read_subdevice->async)
2515 max_buffer_size_kb = read_subdevice->async->max_bufsize / bytes_per_kibi;
2517 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2518 mutex_unlock(&info->device->mutex);
2523 static COMEDI_DECLARE_ATTR_STORE(store_max_read_buffer_kb, dev, buf, count)
2525 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2526 unsigned long new_max_size_kb;
2527 uint64_t new_max_size;
2528 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2530 if(strict_strtoul(buf, 10, &new_max_size_kb))
2534 if(new_max_size_kb != (uint32_t)new_max_size_kb) return -EINVAL;
2535 new_max_size = ((uint64_t)new_max_size_kb) * bytes_per_kibi;
2536 if(new_max_size != (uint32_t)new_max_size) return -EINVAL;
2538 mutex_lock(&info->device->mutex);
2539 if(read_subdevice == NULL ||
2540 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2541 read_subdevice->async == NULL)
2543 mutex_unlock(&info->device->mutex);
2546 read_subdevice->async->max_bufsize = new_max_size;
2547 mutex_unlock(&info->device->mutex);
2552 static COMEDI_DECLARE_ATTR_SHOW(show_read_buffer_kb, dev, buf)
2555 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2556 unsigned buffer_size_kb = 0;
2557 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2559 mutex_lock(&info->device->mutex);
2560 if(read_subdevice &&
2561 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2562 read_subdevice->async)
2564 buffer_size_kb = read_subdevice->async->prealloc_bufsz / bytes_per_kibi;
2566 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2567 mutex_unlock(&info->device->mutex);
2572 static COMEDI_DECLARE_ATTR_STORE(store_read_buffer_kb, dev, buf, count)
2574 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2575 unsigned long new_size_kb;
2578 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2580 if(strict_strtoul(buf, 10, &new_size_kb))
2584 if(new_size_kb != (uint32_t)new_size_kb) return -EINVAL;
2585 new_size = ((uint64_t)new_size_kb) * bytes_per_kibi;
2586 if(new_size != (uint32_t)new_size) return -EINVAL;
2588 mutex_lock(&info->device->mutex);
2589 if(read_subdevice == NULL ||
2590 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2591 read_subdevice->async == NULL)
2593 mutex_unlock(&info->device->mutex);
2596 retval = resize_async_buffer(info->device, read_subdevice,
2597 read_subdevice->async, new_size);
2598 mutex_unlock(&info->device->mutex);
2600 if(retval < 0) return retval;
2604 static COMEDI_DECLARE_ATTR_SHOW(show_max_write_buffer_kb, dev, buf)
2607 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2608 unsigned max_buffer_size_kb = 0;
2609 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2611 mutex_lock(&info->device->mutex);
2612 if(write_subdevice &&
2613 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2614 write_subdevice->async)
2616 max_buffer_size_kb = write_subdevice->async->max_bufsize / bytes_per_kibi;
2618 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2619 mutex_unlock(&info->device->mutex);
2624 static COMEDI_DECLARE_ATTR_STORE(store_max_write_buffer_kb, dev, buf, count)
2626 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2627 unsigned long new_max_size_kb;
2628 uint64_t new_max_size;
2629 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2631 if(strict_strtoul(buf, 10, &new_max_size_kb))
2635 if(new_max_size_kb != (uint32_t)new_max_size_kb) return -EINVAL;
2636 new_max_size = ((uint64_t)new_max_size_kb) * bytes_per_kibi;
2637 if(new_max_size != (uint32_t)new_max_size) return -EINVAL;
2639 mutex_lock(&info->device->mutex);
2640 if(write_subdevice == NULL ||
2641 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2642 write_subdevice->async == NULL)
2644 mutex_unlock(&info->device->mutex);
2647 write_subdevice->async->max_bufsize = new_max_size;
2648 mutex_unlock(&info->device->mutex);
2653 static COMEDI_DECLARE_ATTR_SHOW(show_write_buffer_kb, dev, buf)
2656 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2657 unsigned buffer_size_kb = 0;
2658 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2660 mutex_lock(&info->device->mutex);
2661 if(write_subdevice &&
2662 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2663 write_subdevice->async)
2665 buffer_size_kb = write_subdevice->async->prealloc_bufsz / bytes_per_kibi;
2667 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2668 mutex_unlock(&info->device->mutex);
2673 static COMEDI_DECLARE_ATTR_STORE(store_write_buffer_kb, dev, buf, count)
2675 struct comedi_device_file_info *info = COMEDI_DEV_GET_DRVDATA(dev);
2676 unsigned long new_size_kb;
2679 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2681 if(strict_strtoul(buf, 10, &new_size_kb))
2685 if(new_size_kb != (uint32_t)new_size_kb) return -EINVAL;
2686 new_size = ((uint64_t)new_size_kb) * bytes_per_kibi;
2687 if(new_size != (uint32_t)new_size) return -EINVAL;
2689 mutex_lock(&info->device->mutex);
2690 if(write_subdevice == NULL ||
2691 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2692 write_subdevice->async == NULL)
2694 mutex_unlock(&info->device->mutex);
2697 retval = resize_async_buffer(info->device, write_subdevice,
2698 write_subdevice->async, new_size);
2699 mutex_unlock(&info->device->mutex);
2701 if(retval < 0) return retval;