5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define __NO_VERSION__
27 #include "comedi_fops.h"
28 #include "comedi_compat32.h"
30 #include <linux/module.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/fcntl.h>
35 #include <linux/delay.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/kmod.h>
40 #include <linux/poll.h>
41 #include <linux/init.h>
42 #include <linux/device.h>
43 #include <linux/vmalloc.h>
45 #include "comedidev.h"
46 #include <linux/cdev.h>
47 #include <linux/stat.h>
50 #include <linux/uaccess.h>
52 /* #include "kvmem.h" */
54 MODULE_AUTHOR("http://www.comedi.org");
55 MODULE_DESCRIPTION("Comedi core module");
56 MODULE_LICENSE("GPL");
58 #ifdef CONFIG_COMEDI_DEBUG
60 EXPORT_SYMBOL(comedi_debug);
61 module_param(comedi_debug, int, 0644);
64 int comedi_autoconfig = 1;
65 module_param(comedi_autoconfig, bool, 0444);
67 int comedi_num_legacy_minors;
68 module_param(comedi_num_legacy_minors, int, 0444);
70 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
71 static struct comedi_device_file_info
72 *comedi_file_info_table[COMEDI_NUM_MINORS];
74 static int do_devconfig_ioctl(struct comedi_device *dev,
75 struct comedi_devconfig *arg);
76 static int do_bufconfig_ioctl(struct comedi_device *dev, void *arg);
77 static int do_devinfo_ioctl(struct comedi_device *dev,
78 struct comedi_devinfo *arg, struct file *file);
79 static int do_subdinfo_ioctl(struct comedi_device *dev,
80 struct comedi_subdinfo *arg, void *file);
81 static int do_chaninfo_ioctl(struct comedi_device *dev,
82 struct comedi_chaninfo *arg);
83 static int do_bufinfo_ioctl(struct comedi_device *dev, void *arg);
84 static int do_cmd_ioctl(struct comedi_device *dev, void *arg, void *file);
85 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
87 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
89 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
91 static int do_cmdtest_ioctl(struct comedi_device *dev, void *arg, void *file);
92 static int do_insnlist_ioctl(struct comedi_device *dev, void *arg, void *file);
93 static int do_insn_ioctl(struct comedi_device *dev, void *arg, void *file);
94 static int do_poll_ioctl(struct comedi_device *dev, unsigned int subd,
97 extern void do_become_nonbusy(struct comedi_device *dev,
98 struct comedi_subdevice *s);
99 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
101 static int comedi_fasync(int fd, struct file *file, int on);
103 static int is_device_busy(struct comedi_device *dev);
104 static int resize_async_buffer(struct comedi_device *dev,
105 struct comedi_subdevice *s,
106 struct comedi_async *async, unsigned new_size);
108 /* declarations for sysfs attribute files */
109 static struct device_attribute dev_attr_max_read_buffer_kb;
110 static struct device_attribute dev_attr_read_buffer_kb;
111 static struct device_attribute dev_attr_max_write_buffer_kb;
112 static struct device_attribute dev_attr_write_buffer_kb;
114 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
117 const unsigned minor = iminor(file->f_dentry->d_inode);
118 struct comedi_device_file_info *dev_file_info =
119 comedi_get_device_file_info(minor);
120 struct comedi_device *dev;
123 if (dev_file_info == NULL || dev_file_info->device == NULL)
125 dev = dev_file_info->device;
127 mutex_lock(&dev->mutex);
129 /* Device config is special, because it must work on
130 * an unconfigured device. */
131 if (cmd == COMEDI_DEVCONFIG) {
132 rc = do_devconfig_ioctl(dev, (void *)arg);
136 if (!dev->attached) {
137 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
143 case COMEDI_BUFCONFIG:
144 rc = do_bufconfig_ioctl(dev, (void *)arg);
147 rc = do_devinfo_ioctl(dev, (void *)arg, file);
149 case COMEDI_SUBDINFO:
150 rc = do_subdinfo_ioctl(dev, (void *)arg, file);
152 case COMEDI_CHANINFO:
153 rc = do_chaninfo_ioctl(dev, (void *)arg);
155 case COMEDI_RANGEINFO:
156 rc = do_rangeinfo_ioctl(dev, (void *)arg);
159 rc = do_bufinfo_ioctl(dev, (void *)arg);
162 rc = do_lock_ioctl(dev, arg, file);
165 rc = do_unlock_ioctl(dev, arg, file);
168 rc = do_cancel_ioctl(dev, arg, file);
171 rc = do_cmd_ioctl(dev, (void *)arg, file);
174 rc = do_cmdtest_ioctl(dev, (void *)arg, file);
176 case COMEDI_INSNLIST:
177 rc = do_insnlist_ioctl(dev, (void *)arg, file);
180 rc = do_insn_ioctl(dev, (void *)arg, file);
183 rc = do_poll_ioctl(dev, arg, file);
191 mutex_unlock(&dev->mutex);
200 pointer to devconfig structure
203 devconfig structure at arg
208 static int do_devconfig_ioctl(struct comedi_device *dev,
209 struct comedi_devconfig *arg)
211 struct comedi_devconfig it;
213 unsigned char *aux_data = NULL;
216 if (!capable(CAP_SYS_ADMIN))
220 if (is_device_busy(dev))
223 struct module *driver_module = dev->driver->module;
224 comedi_device_detach(dev);
225 module_put(driver_module);
230 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
233 it.board_name[COMEDI_NAMELEN - 1] = 0;
235 if (comedi_aux_data(it.options, 0) &&
236 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
238 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
242 aux_data = vmalloc(aux_len);
246 if (copy_from_user(aux_data,
247 comedi_aux_data(it.options, 0), aux_len)) {
251 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
252 (unsigned long)aux_data;
253 if (sizeof(void *) > sizeof(int)) {
254 bit_shift = sizeof(int) * 8;
255 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
256 ((unsigned long)aux_data) >> bit_shift;
258 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
261 ret = comedi_device_attach(dev, &it);
263 if (!try_module_get(dev->driver->module)) {
264 comedi_device_detach(dev);
277 buffer configuration ioctl
280 pointer to bufconfig structure
286 modified bufconfig at arg
289 static int do_bufconfig_ioctl(struct comedi_device *dev, void *arg)
291 struct comedi_bufconfig bc;
292 struct comedi_async *async;
293 struct comedi_subdevice *s;
296 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
299 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
302 s = dev->subdevices + bc.subdevice;
306 DPRINTK("subdevice does not have async capability\n");
312 if (bc.maximum_size) {
313 if (!capable(CAP_SYS_ADMIN))
316 async->max_bufsize = bc.maximum_size;
320 retval = resize_async_buffer(dev, s, async, bc.size);
325 bc.size = async->prealloc_bufsz;
326 bc.maximum_size = async->max_bufsize;
329 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
340 pointer to devinfo structure
349 static int do_devinfo_ioctl(struct comedi_device *dev,
350 struct comedi_devinfo *arg, struct file *file)
352 struct comedi_devinfo devinfo;
353 const unsigned minor = iminor(file->f_dentry->d_inode);
354 struct comedi_device_file_info *dev_file_info =
355 comedi_get_device_file_info(minor);
356 struct comedi_subdevice *read_subdev =
357 comedi_get_read_subdevice(dev_file_info);
358 struct comedi_subdevice *write_subdev =
359 comedi_get_write_subdevice(dev_file_info);
361 memset(&devinfo, 0, sizeof(devinfo));
363 /* fill devinfo structure */
364 devinfo.version_code = COMEDI_VERSION_CODE;
365 devinfo.n_subdevs = dev->n_subdevices;
366 memcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
367 memcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
370 devinfo.read_subdevice = read_subdev - dev->subdevices;
372 devinfo.read_subdevice = -1;
375 devinfo.write_subdevice = write_subdev - dev->subdevices;
377 devinfo.write_subdevice = -1;
379 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
390 pointer to array of subdevice info structures
396 array of subdevice info structures at arg
399 static int do_subdinfo_ioctl(struct comedi_device *dev,
400 struct comedi_subdinfo *arg, void *file)
403 struct comedi_subdinfo *tmp, *us;
404 struct comedi_subdevice *s;
407 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
412 /* fill subdinfo structs */
413 for (i = 0; i < dev->n_subdevices; i++) {
414 s = dev->subdevices + i;
418 us->n_chan = s->n_chan;
419 us->subd_flags = s->subdev_flags;
420 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
421 us->subd_flags |= SDF_RUNNING;
422 #define TIMER_nanosec 5 /* backwards compatibility */
423 us->timer_type = TIMER_nanosec;
424 us->len_chanlist = s->len_chanlist;
425 us->maxdata = s->maxdata;
426 if (s->range_table) {
428 (i << 24) | (0 << 16) | (s->range_table->length);
430 us->range_type = 0; /* XXX */
432 us->flags = s->flags;
435 us->subd_flags |= SDF_BUSY;
437 us->subd_flags |= SDF_BUSY_OWNER;
439 us->subd_flags |= SDF_LOCKED;
441 us->subd_flags |= SDF_LOCK_OWNER;
442 if (!s->maxdata && s->maxdata_list)
443 us->subd_flags |= SDF_MAXDATA;
445 us->subd_flags |= SDF_FLAGS;
446 if (s->range_table_list)
447 us->subd_flags |= SDF_RANGETYPE;
449 us->subd_flags |= SDF_CMD;
451 if (s->insn_bits != &insn_inval)
452 us->insn_bits_support = COMEDI_SUPPORTED;
454 us->insn_bits_support = COMEDI_UNSUPPORTED;
456 us->settling_time_0 = s->settling_time_0;
459 ret = copy_to_user(arg, tmp,
460 dev->n_subdevices * sizeof(struct comedi_subdinfo));
464 return ret ? -EFAULT : 0;
472 pointer to chaninfo structure
475 chaninfo structure at arg
478 arrays at elements of chaninfo structure
481 static int do_chaninfo_ioctl(struct comedi_device *dev,
482 struct comedi_chaninfo *arg)
484 struct comedi_subdevice *s;
485 struct comedi_chaninfo it;
487 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
490 if (it.subdev >= dev->n_subdevices)
492 s = dev->subdevices + it.subdev;
494 if (it.maxdata_list) {
495 if (s->maxdata || !s->maxdata_list)
497 if (copy_to_user(it.maxdata_list, s->maxdata_list,
498 s->n_chan * sizeof(unsigned int)))
505 if (copy_to_user(it.flaglist, s->flaglist,
506 s->n_chan * sizeof(unsigned int)))
513 if (!s->range_table_list)
515 for (i = 0; i < s->n_chan; i++) {
518 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
519 (s->range_table_list[i]->length);
520 put_user(x, it.rangelist + i);
523 if (copy_to_user(it.rangelist, s->range_type_list,
524 s->n_chan * sizeof(unsigned int)))
534 buffer information ioctl
537 pointer to bufinfo structure
543 modified bufinfo at arg
546 static int do_bufinfo_ioctl(struct comedi_device *dev, void *arg)
548 struct comedi_bufinfo bi;
549 struct comedi_subdevice *s;
550 struct comedi_async *async;
552 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
555 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
558 s = dev->subdevices + bi.subdevice;
562 DPRINTK("subdevice does not have async capability\n");
563 bi.buf_write_ptr = 0;
565 bi.buf_write_count = 0;
566 bi.buf_read_count = 0;
570 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
571 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
572 comedi_buf_read_free(async, bi.bytes_read);
574 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
576 && async->buf_write_count == async->buf_read_count) {
577 do_become_nonbusy(dev, s);
581 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
583 comedi_buf_write_alloc(async, bi.bytes_written);
584 comedi_buf_write_free(async, bi.bytes_written);
587 bi.buf_write_count = async->buf_write_count;
588 bi.buf_write_ptr = async->buf_write_ptr;
589 bi.buf_read_count = async->buf_read_count;
590 bi.buf_read_ptr = async->buf_read_ptr;
593 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
599 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
600 unsigned int *data, void *file);
603 * synchronous instructions
606 * pointer to sync cmd structure
609 * sync cmd struct at arg
616 /* arbitrary limits */
617 #define MAX_SAMPLES 256
618 static int do_insnlist_ioctl(struct comedi_device *dev, void *arg, void *file)
620 struct comedi_insnlist insnlist;
621 struct comedi_insn *insns = NULL;
622 unsigned int *data = NULL;
626 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
629 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
631 DPRINTK("kmalloc failed\n");
637 kmalloc(sizeof(struct comedi_insn) * insnlist.n_insns, GFP_KERNEL);
639 DPRINTK("kmalloc failed\n");
644 if (copy_from_user(insns, insnlist.insns,
645 sizeof(struct comedi_insn) * insnlist.n_insns)) {
646 DPRINTK("copy_from_user failed\n");
651 for (i = 0; i < insnlist.n_insns; i++) {
652 if (insns[i].n > MAX_SAMPLES) {
653 DPRINTK("number of samples too large\n");
657 if (insns[i].insn & INSN_MASK_WRITE) {
658 if (copy_from_user(data, insns[i].data,
659 insns[i].n * sizeof(unsigned int))) {
660 DPRINTK("copy_from_user failed\n");
665 ret = parse_insn(dev, insns + i, data, file);
668 if (insns[i].insn & INSN_MASK_READ) {
669 if (copy_to_user(insns[i].data, data,
670 insns[i].n * sizeof(unsigned int))) {
671 DPRINTK("copy_to_user failed\n");
689 static int check_insn_config_length(struct comedi_insn *insn,
696 case INSN_CONFIG_DIO_OUTPUT:
697 case INSN_CONFIG_DIO_INPUT:
698 case INSN_CONFIG_DISARM:
699 case INSN_CONFIG_RESET:
703 case INSN_CONFIG_ARM:
704 case INSN_CONFIG_DIO_QUERY:
705 case INSN_CONFIG_BLOCK_SIZE:
706 case INSN_CONFIG_FILTER:
707 case INSN_CONFIG_SERIAL_CLOCK:
708 case INSN_CONFIG_BIDIRECTIONAL_DATA:
709 case INSN_CONFIG_ALT_SOURCE:
710 case INSN_CONFIG_SET_COUNTER_MODE:
711 case INSN_CONFIG_8254_READ_STATUS:
712 case INSN_CONFIG_SET_ROUTING:
713 case INSN_CONFIG_GET_ROUTING:
714 case INSN_CONFIG_GET_PWM_STATUS:
715 case INSN_CONFIG_PWM_SET_PERIOD:
716 case INSN_CONFIG_PWM_GET_PERIOD:
720 case INSN_CONFIG_SET_GATE_SRC:
721 case INSN_CONFIG_GET_GATE_SRC:
722 case INSN_CONFIG_SET_CLOCK_SRC:
723 case INSN_CONFIG_GET_CLOCK_SRC:
724 case INSN_CONFIG_SET_OTHER_SRC:
725 case INSN_CONFIG_GET_COUNTER_STATUS:
726 case INSN_CONFIG_PWM_SET_H_BRIDGE:
727 case INSN_CONFIG_PWM_GET_H_BRIDGE:
728 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
732 case INSN_CONFIG_PWM_OUTPUT:
733 case INSN_CONFIG_ANALOG_TRIG:
737 /* by default we allow the insn since we don't have checks for
738 * all possible cases yet */
741 "comedi: no check for data length of config insn id "
742 "%i is implemented.\n"
743 " Add a check to %s in %s.\n"
744 " Assuming n=%i is correct.\n", data[0], __func__,
752 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
753 unsigned int *data, void *file)
755 struct comedi_subdevice *s;
759 if (insn->insn & INSN_MASK_SPECIAL) {
760 /* a non-subdevice instruction */
762 switch (insn->insn) {
772 do_gettimeofday(&tv);
774 data[1] = tv.tv_usec;
780 if (insn->n != 1 || data[0] >= 100000) {
784 udelay(data[0] / 1000);
792 if (insn->subdev >= dev->n_subdevices) {
793 DPRINTK("%d not usable subdevice\n",
798 s = dev->subdevices + insn->subdev;
800 DPRINTK("no async\n");
804 if (!s->async->inttrig) {
805 DPRINTK("no inttrig\n");
809 ret = s->async->inttrig(dev, s, insn->data[0]);
814 DPRINTK("invalid insn\n");
819 /* a subdevice instruction */
820 unsigned int maxdata;
822 if (insn->subdev >= dev->n_subdevices) {
823 DPRINTK("subdevice %d out of range\n", insn->subdev);
827 s = dev->subdevices + insn->subdev;
829 if (s->type == COMEDI_SUBD_UNUSED) {
830 DPRINTK("%d not usable subdevice\n", insn->subdev);
835 /* are we locked? (ioctl lock) */
836 if (s->lock && s->lock != file) {
837 DPRINTK("device locked\n");
842 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
845 DPRINTK("bad chanspec\n");
853 /* This looks arbitrary. It is. */
854 s->busy = &parse_insn;
855 switch (insn->insn) {
857 ret = s->insn_read(dev, s, insn, data);
860 maxdata = s->maxdata_list
861 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
863 for (i = 0; i < insn->n; ++i) {
864 if (data[i] > maxdata) {
866 DPRINTK("bad data value(s)\n");
871 ret = s->insn_write(dev, s, insn, data);
878 ret = s->insn_bits(dev, s, insn, data);
881 ret = check_insn_config_length(insn, data);
884 ret = s->insn_config(dev, s, insn, data);
900 * synchronous instructions
906 * struct comedi_insn struct at arg
912 static int do_insn_ioctl(struct comedi_device *dev, void *arg, void *file)
914 struct comedi_insn insn;
915 unsigned int *data = NULL;
918 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
924 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
929 /* This is where the behavior of insn and insnlist deviate. */
930 if (insn.n > MAX_SAMPLES)
931 insn.n = MAX_SAMPLES;
932 if (insn.insn & INSN_MASK_WRITE) {
934 (data, insn.data, insn.n * sizeof(unsigned int))) {
939 ret = parse_insn(dev, &insn, data, file);
942 if (insn.insn & INSN_MASK_READ) {
944 (insn.data, data, insn.n * sizeof(unsigned int))) {
957 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
958 unsigned mask, unsigned bits)
962 spin_lock_irqsave(&s->spin_lock, flags);
963 s->runflags &= ~mask;
964 s->runflags |= (bits & mask);
965 spin_unlock_irqrestore(&s->spin_lock, flags);
973 pointer to cmd structure
980 modified cmd structure at arg
983 static int do_cmd_ioctl(struct comedi_device *dev, void *arg, void *file)
985 struct comedi_cmd user_cmd;
986 struct comedi_subdevice *s;
987 struct comedi_async *async;
989 unsigned int *chanlist_saver = NULL;
991 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
992 DPRINTK("bad cmd address\n");
995 /* save user's chanlist pointer so it can be restored later */
996 chanlist_saver = user_cmd.chanlist;
998 if (user_cmd.subdev >= dev->n_subdevices) {
999 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1003 s = dev->subdevices + user_cmd.subdev;
1006 if (s->type == COMEDI_SUBD_UNUSED) {
1007 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1011 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1012 DPRINTK("subdevice %i does not support commands\n",
1017 /* are we locked? (ioctl lock) */
1018 if (s->lock && s->lock != file) {
1019 DPRINTK("subdevice locked\n");
1025 DPRINTK("subdevice busy\n");
1030 /* make sure channel/gain list isn't too long */
1031 if (user_cmd.chanlist_len > s->len_chanlist) {
1032 DPRINTK("channel/gain list too long %u > %d\n",
1033 user_cmd.chanlist_len, s->len_chanlist);
1038 /* make sure channel/gain list isn't too short */
1039 if (user_cmd.chanlist_len < 1) {
1040 DPRINTK("channel/gain list too short %u < 1\n",
1041 user_cmd.chanlist_len);
1046 kfree(async->cmd.chanlist);
1047 async->cmd = user_cmd;
1048 async->cmd.data = NULL;
1049 /* load channel/gain list */
1050 async->cmd.chanlist =
1051 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1052 if (!async->cmd.chanlist) {
1053 DPRINTK("allocation failed\n");
1058 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1059 async->cmd.chanlist_len * sizeof(int))) {
1060 DPRINTK("fault reading chanlist\n");
1065 /* make sure each element in channel/gain list is valid */
1066 ret = comedi_check_chanlist(s, async->cmd.chanlist_len, async->cmd.chanlist);
1068 DPRINTK("bad chanlist\n");
1072 ret = s->do_cmdtest(dev, s, &async->cmd);
1074 if (async->cmd.flags & TRIG_BOGUS || ret) {
1075 DPRINTK("test returned %d\n", ret);
1076 user_cmd = async->cmd;
1077 /* restore chanlist pointer before copying back */
1078 user_cmd.chanlist = chanlist_saver;
1079 user_cmd.data = NULL;
1080 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
1081 DPRINTK("fault writing cmd\n");
1089 if (!async->prealloc_bufsz) {
1091 DPRINTK("no buffer (?)\n");
1095 comedi_reset_async_buf(async);
1098 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1100 if (async->cmd.flags & TRIG_WAKE_EOS)
1101 async->cb_mask |= COMEDI_CB_EOS;
1103 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1105 ret = s->do_cmd(dev, s);
1110 do_become_nonbusy(dev, s);
1117 command testing ioctl
1120 pointer to cmd structure
1123 cmd structure at arg
1127 modified cmd structure at arg
1130 static int do_cmdtest_ioctl(struct comedi_device *dev, void *arg, void *file)
1132 struct comedi_cmd user_cmd;
1133 struct comedi_subdevice *s;
1135 unsigned int *chanlist = NULL;
1136 unsigned int *chanlist_saver = NULL;
1138 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
1139 DPRINTK("bad cmd address\n");
1142 /* save user's chanlist pointer so it can be restored later */
1143 chanlist_saver = user_cmd.chanlist;
1145 if (user_cmd.subdev >= dev->n_subdevices) {
1146 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1150 s = dev->subdevices + user_cmd.subdev;
1151 if (s->type == COMEDI_SUBD_UNUSED) {
1152 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1156 if (!s->do_cmd || !s->do_cmdtest) {
1157 DPRINTK("subdevice %i does not support commands\n",
1162 /* make sure channel/gain list isn't too long */
1163 if (user_cmd.chanlist_len > s->len_chanlist) {
1164 DPRINTK("channel/gain list too long %d > %d\n",
1165 user_cmd.chanlist_len, s->len_chanlist);
1170 /* load channel/gain list */
1171 if (user_cmd.chanlist) {
1173 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1175 DPRINTK("allocation failed\n");
1180 if (copy_from_user(chanlist, user_cmd.chanlist,
1181 user_cmd.chanlist_len * sizeof(int))) {
1182 DPRINTK("fault reading chanlist\n");
1187 /* make sure each element in channel/gain list is valid */
1188 ret = comedi_check_chanlist(s, user_cmd.chanlist_len, chanlist);
1190 DPRINTK("bad chanlist\n");
1194 user_cmd.chanlist = chanlist;
1197 ret = s->do_cmdtest(dev, s, &user_cmd);
1199 /* restore chanlist pointer before copying back */
1200 user_cmd.chanlist = chanlist_saver;
1202 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
1203 DPRINTK("bad cmd address\n");
1228 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1232 unsigned long flags;
1233 struct comedi_subdevice *s;
1235 if (arg >= dev->n_subdevices)
1237 s = dev->subdevices + arg;
1239 spin_lock_irqsave(&s->spin_lock, flags);
1240 if (s->busy || s->lock)
1244 spin_unlock_irqrestore(&s->spin_lock, flags);
1251 ret = s->lock_f(dev, s);
1270 This function isn't protected by the semaphore, since
1271 we already own the lock.
1273 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1276 struct comedi_subdevice *s;
1278 if (arg >= dev->n_subdevices)
1280 s = dev->subdevices + arg;
1285 if (s->lock && s->lock != file)
1288 if (s->lock == file) {
1302 cancel acquisition ioctl
1314 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1317 struct comedi_subdevice *s;
1319 if (arg >= dev->n_subdevices)
1321 s = dev->subdevices + arg;
1322 if (s->async == NULL)
1325 if (s->lock && s->lock != file)
1331 if (s->busy != file)
1334 return do_cancel(dev, s);
1339 instructs driver to synchronize buffers
1351 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1354 struct comedi_subdevice *s;
1356 if (arg >= dev->n_subdevices)
1358 s = dev->subdevices + arg;
1360 if (s->lock && s->lock != file)
1366 if (s->busy != file)
1370 return s->poll(dev, s);
1375 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1379 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1380 ret = s->cancel(dev, s);
1382 do_become_nonbusy(dev, s);
1387 void comedi_unmap(struct vm_area_struct *area)
1389 struct comedi_async *async;
1390 struct comedi_device *dev;
1392 async = area->vm_private_data;
1393 dev = async->subdevice->device;
1395 mutex_lock(&dev->mutex);
1396 async->mmap_count--;
1397 mutex_unlock(&dev->mutex);
1400 static struct vm_operations_struct comedi_vm_ops = {
1401 .close = comedi_unmap,
1404 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1406 const unsigned minor = iminor(file->f_dentry->d_inode);
1407 struct comedi_device_file_info *dev_file_info =
1408 comedi_get_device_file_info(minor);
1409 struct comedi_device *dev = dev_file_info->device;
1410 struct comedi_async *async = NULL;
1411 unsigned long start = vma->vm_start;
1416 struct comedi_subdevice *s;
1418 mutex_lock(&dev->mutex);
1419 if (!dev->attached) {
1420 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1424 if (vma->vm_flags & VM_WRITE)
1425 s = comedi_get_write_subdevice(dev_file_info);
1427 s = comedi_get_read_subdevice(dev_file_info);
1434 if (async == NULL) {
1439 if (vma->vm_pgoff != 0) {
1440 DPRINTK("comedi: mmap() offset must be 0.\n");
1445 size = vma->vm_end - vma->vm_start;
1446 if (size > async->prealloc_bufsz) {
1450 if (size & (~PAGE_MASK)) {
1455 n_pages = size >> PAGE_SHIFT;
1456 for (i = 0; i < n_pages; ++i) {
1457 if (remap_pfn_range(vma, start,
1458 page_to_pfn(virt_to_page
1459 (async->buf_page_list
1460 [i].virt_addr)), PAGE_SIZE,
1468 vma->vm_ops = &comedi_vm_ops;
1469 vma->vm_private_data = async;
1471 async->mmap_count++;
1475 mutex_unlock(&dev->mutex);
1479 static unsigned int comedi_poll(struct file *file, poll_table * wait)
1481 unsigned int mask = 0;
1482 const unsigned minor = iminor(file->f_dentry->d_inode);
1483 struct comedi_device_file_info *dev_file_info =
1484 comedi_get_device_file_info(minor);
1485 struct comedi_device *dev = dev_file_info->device;
1486 struct comedi_subdevice *read_subdev;
1487 struct comedi_subdevice *write_subdev;
1489 mutex_lock(&dev->mutex);
1490 if (!dev->attached) {
1491 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1492 mutex_unlock(&dev->mutex);
1497 read_subdev = comedi_get_read_subdevice(dev_file_info);
1499 poll_wait(file, &read_subdev->async->wait_head, wait);
1500 if (!read_subdev->busy
1501 || comedi_buf_read_n_available(read_subdev->async) > 0
1502 || !(comedi_get_subdevice_runflags(read_subdev) &
1504 mask |= POLLIN | POLLRDNORM;
1507 write_subdev = comedi_get_write_subdevice(dev_file_info);
1509 poll_wait(file, &write_subdev->async->wait_head, wait);
1510 comedi_buf_write_alloc(write_subdev->async,
1511 write_subdev->async->prealloc_bufsz);
1512 if (!write_subdev->busy
1513 || !(comedi_get_subdevice_runflags(write_subdev) &
1515 || comedi_buf_write_n_allocated(write_subdev->async) >=
1516 bytes_per_sample(write_subdev->async->subdevice)) {
1517 mask |= POLLOUT | POLLWRNORM;
1521 mutex_unlock(&dev->mutex);
1525 static ssize_t comedi_write(struct file *file, const char *buf, size_t nbytes,
1528 struct comedi_subdevice *s;
1529 struct comedi_async *async;
1530 int n, m, count = 0, retval = 0;
1531 DECLARE_WAITQUEUE(wait, current);
1532 const unsigned minor = iminor(file->f_dentry->d_inode);
1533 struct comedi_device_file_info *dev_file_info =
1534 comedi_get_device_file_info(minor);
1535 struct comedi_device *dev = dev_file_info->device;
1537 if (!dev->attached) {
1538 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1543 s = comedi_get_write_subdevice(dev_file_info);
1558 if (s->busy != file) {
1562 add_wait_queue(&async->wait_head, &wait);
1563 while (nbytes > 0 && !retval) {
1564 set_current_state(TASK_INTERRUPTIBLE);
1569 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1570 m = async->prealloc_bufsz - async->buf_write_ptr;
1571 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1572 if (m > comedi_buf_write_n_allocated(async))
1573 m = comedi_buf_write_n_allocated(async);
1578 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1579 if (comedi_get_subdevice_runflags(s) &
1585 do_become_nonbusy(dev, s);
1588 if (file->f_flags & O_NONBLOCK) {
1592 if (signal_pending(current)) {
1593 retval = -ERESTARTSYS;
1599 if (s->busy != file) {
1606 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1612 comedi_buf_write_free(async, n);
1618 break; /* makes device work like a pipe */
1620 set_current_state(TASK_RUNNING);
1621 remove_wait_queue(&async->wait_head, &wait);
1624 return count ? count : retval;
1627 static ssize_t comedi_read(struct file *file, char *buf, size_t nbytes,
1630 struct comedi_subdevice *s;
1631 struct 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 =
1636 comedi_get_device_file_info(minor);
1637 struct comedi_device *dev = dev_file_info->device;
1639 if (!dev->attached) {
1640 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1645 s = comedi_get_read_subdevice(dev_file_info);
1659 if (s->busy != file) {
1664 add_wait_queue(&async->wait_head, &wait);
1665 while (nbytes > 0 && !retval) {
1666 set_current_state(TASK_INTERRUPTIBLE);
1670 m = comedi_buf_read_n_available(async);
1671 /* printk("%d available\n",m); */
1672 if (async->buf_read_ptr + m > async->prealloc_bufsz)
1673 m = async->prealloc_bufsz - async->buf_read_ptr;
1674 /* printk("%d contiguous\n",m); */
1679 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1680 do_become_nonbusy(dev, s);
1681 if (comedi_get_subdevice_runflags(s) &
1689 if (file->f_flags & O_NONBLOCK) {
1693 if (signal_pending(current)) {
1694 retval = -ERESTARTSYS;
1702 if (s->busy != file) {
1708 m = copy_to_user(buf, async->prealloc_buf +
1709 async->buf_read_ptr, n);
1715 comedi_buf_read_alloc(async, n);
1716 comedi_buf_read_free(async, n);
1722 break; /* makes device work like a pipe */
1724 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
1725 async->buf_read_count - async->buf_write_count == 0) {
1726 do_become_nonbusy(dev, s);
1728 set_current_state(TASK_RUNNING);
1729 remove_wait_queue(&async->wait_head, &wait);
1732 return count ? count : retval;
1736 This function restores a subdevice to an idle state.
1738 void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
1740 struct comedi_async *async = s->async;
1742 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1744 comedi_reset_async_buf(async);
1745 async->inttrig = NULL;
1748 "BUG: (?) do_become_nonbusy called with async=0\n");
1754 static int comedi_open(struct inode *inode, struct file *file)
1756 const unsigned minor = iminor(inode);
1757 struct comedi_device_file_info *dev_file_info =
1758 comedi_get_device_file_info(minor);
1759 struct comedi_device *dev =
1760 dev_file_info ? dev_file_info->device : NULL;
1763 DPRINTK("invalid minor number\n");
1767 /* This is slightly hacky, but we want module autoloading
1769 * case: user opens device, attached -> ok
1770 * case: user opens device, unattached, in_request_module=0 -> autoload
1771 * case: user opens device, unattached, in_request_module=1 -> fail
1772 * case: root opens device, attached -> ok
1773 * case: root opens device, unattached, in_request_module=1 -> ok
1774 * (typically called from modprobe)
1775 * case: root opens device, unattached, in_request_module=0 -> autoload
1777 * The last could be changed to "-> ok", which would deny root
1780 mutex_lock(&dev->mutex);
1783 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
1784 DPRINTK("in request module\n");
1785 mutex_unlock(&dev->mutex);
1788 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
1791 dev->in_request_module = 1;
1794 mutex_unlock(&dev->mutex);
1795 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1796 mutex_lock(&dev->mutex);
1799 dev->in_request_module = 0;
1801 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
1802 DPRINTK("not attached and not CAP_NET_ADMIN\n");
1803 mutex_unlock(&dev->mutex);
1807 __module_get(THIS_MODULE);
1809 if (dev->attached) {
1810 if (!try_module_get(dev->driver->module)) {
1811 module_put(THIS_MODULE);
1812 mutex_unlock(&dev->mutex);
1817 if (dev->attached && dev->use_count == 0 && dev->open)
1822 mutex_unlock(&dev->mutex);
1827 static int comedi_close(struct inode *inode, struct file *file)
1829 const unsigned minor = iminor(inode);
1830 struct comedi_device_file_info *dev_file_info =
1831 comedi_get_device_file_info(minor);
1832 struct comedi_device *dev = dev_file_info->device;
1833 struct comedi_subdevice *s = NULL;
1836 mutex_lock(&dev->mutex);
1838 if (dev->subdevices) {
1839 for (i = 0; i < dev->n_subdevices; i++) {
1840 s = dev->subdevices + i;
1842 if (s->busy == file)
1844 if (s->lock == file)
1848 if (dev->attached && dev->use_count == 1 && dev->close)
1851 module_put(THIS_MODULE);
1853 module_put(dev->driver->module);
1857 mutex_unlock(&dev->mutex);
1859 if (file->f_flags & FASYNC)
1860 comedi_fasync(-1, file, 0);
1865 static int comedi_fasync(int fd, struct file *file, int on)
1867 const unsigned minor = iminor(file->f_dentry->d_inode);
1868 struct comedi_device_file_info *dev_file_info =
1869 comedi_get_device_file_info(minor);
1871 struct comedi_device *dev = dev_file_info->device;
1873 return fasync_helper(fd, file, on, &dev->async_queue);
1876 const struct file_operations comedi_fops = {
1877 .owner = THIS_MODULE,
1878 .unlocked_ioctl = comedi_unlocked_ioctl,
1879 .compat_ioctl = comedi_compat_ioctl,
1880 .open = comedi_open,
1881 .release = comedi_close,
1882 .read = comedi_read,
1883 .write = comedi_write,
1884 .mmap = comedi_mmap,
1885 .poll = comedi_poll,
1886 .fasync = comedi_fasync,
1889 struct class *comedi_class;
1890 static struct cdev comedi_cdev;
1892 static void comedi_cleanup_legacy_minors(void)
1896 for (i = 0; i < comedi_num_legacy_minors; i++)
1897 comedi_free_board_minor(i);
1900 static int __init comedi_init(void)
1905 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
1906 " - http://www.comedi.org\n");
1908 if (comedi_num_legacy_minors < 0 ||
1909 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
1910 printk(KERN_ERR "comedi: error: invalid value for module "
1911 "parameter \"comedi_num_legacy_minors\". Valid values "
1912 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
1917 * comedi is unusable if both comedi_autoconfig and
1918 * comedi_num_legacy_minors are zero, so we might as well adjust the
1919 * defaults in that case
1921 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
1922 comedi_num_legacy_minors = 16;
1924 memset(comedi_file_info_table, 0,
1925 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
1927 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1928 COMEDI_NUM_MINORS, "comedi");
1931 cdev_init(&comedi_cdev, &comedi_fops);
1932 comedi_cdev.owner = THIS_MODULE;
1933 kobject_set_name(&comedi_cdev.kobj, "comedi");
1934 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
1935 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1939 comedi_class = class_create(THIS_MODULE, "comedi");
1940 if (IS_ERR(comedi_class)) {
1941 printk(KERN_ERR "comedi: failed to create class");
1942 cdev_del(&comedi_cdev);
1943 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1945 return PTR_ERR(comedi_class);
1948 /* XXX requires /proc interface */
1951 /* create devices files for legacy/manual use */
1952 for (i = 0; i < comedi_num_legacy_minors; i++) {
1954 minor = comedi_alloc_board_minor(NULL);
1956 comedi_cleanup_legacy_minors();
1957 cdev_del(&comedi_cdev);
1958 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1967 static void __exit comedi_cleanup(void)
1971 comedi_cleanup_legacy_minors();
1972 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
1973 BUG_ON(comedi_file_info_table[i]);
1975 class_destroy(comedi_class);
1976 cdev_del(&comedi_cdev);
1977 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
1979 comedi_proc_cleanup();
1982 module_init(comedi_init);
1983 module_exit(comedi_cleanup);
1985 void comedi_error(const struct comedi_device *dev, const char *s)
1987 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
1988 dev->driver->driver_name, s);
1990 EXPORT_SYMBOL(comedi_error);
1992 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
1994 struct comedi_async *async = s->async;
1995 unsigned runflags = 0;
1996 unsigned runflags_mask = 0;
1998 /* DPRINTK("comedi_event 0x%x\n",mask); */
2000 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2004 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2005 COMEDI_CB_OVERFLOW)) {
2006 runflags_mask |= SRF_RUNNING;
2008 /* remember if an error event has occured, so an error
2009 * can be returned the next time the user does a read() */
2010 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2011 runflags_mask |= SRF_ERROR;
2012 runflags |= SRF_ERROR;
2014 if (runflags_mask) {
2015 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2016 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2019 if (async->cb_mask & s->async->events) {
2020 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2021 wake_up_interruptible(&async->wait_head);
2022 if (s->subdev_flags & SDF_CMD_READ)
2023 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2024 if (s->subdev_flags & SDF_CMD_WRITE)
2025 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2028 async->cb_func(s->async->events, async->cb_arg);
2031 s->async->events = 0;
2033 EXPORT_SYMBOL(comedi_event);
2035 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
2037 unsigned long flags;
2040 spin_lock_irqsave(&s->spin_lock, flags);
2041 runflags = s->runflags;
2042 spin_unlock_irqrestore(&s->spin_lock, flags);
2045 EXPORT_SYMBOL(comedi_get_subdevice_runflags);
2047 static int is_device_busy(struct comedi_device *dev)
2049 struct comedi_subdevice *s;
2055 for (i = 0; i < dev->n_subdevices; i++) {
2056 s = dev->subdevices + i;
2059 if (s->async && s->async->mmap_count)
2066 void comedi_device_init(struct comedi_device *dev)
2068 memset(dev, 0, sizeof(struct comedi_device));
2069 spin_lock_init(&dev->spinlock);
2070 mutex_init(&dev->mutex);
2074 void comedi_device_cleanup(struct comedi_device *dev)
2078 mutex_lock(&dev->mutex);
2079 comedi_device_detach(dev);
2080 mutex_unlock(&dev->mutex);
2081 mutex_destroy(&dev->mutex);
2084 int comedi_alloc_board_minor(struct device *hardware_device)
2086 unsigned long flags;
2087 struct comedi_device_file_info *info;
2088 struct device *csdev;
2092 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2095 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2096 if (info->device == NULL) {
2100 comedi_device_init(info->device);
2101 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2102 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2103 if (comedi_file_info_table[i] == NULL) {
2104 comedi_file_info_table[i] = info;
2108 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2109 if (i == COMEDI_NUM_BOARD_MINORS) {
2110 comedi_device_cleanup(info->device);
2111 kfree(info->device);
2115 "comedi: error: ran out of minor numbers for board device files.\n");
2118 info->device->minor = i;
2119 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
2120 MKDEV(COMEDI_MAJOR, i), NULL,
2121 hardware_device, "comedi%i", i);
2123 info->device->class_dev = csdev;
2124 dev_set_drvdata(csdev, info);
2125 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2128 "comedi: failed to create sysfs attribute file \"%s\".\n",
2129 dev_attr_max_read_buffer_kb.attr.name);
2130 comedi_free_board_minor(i);
2133 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2136 "comedi: failed to create sysfs attribute file \"%s\".\n",
2137 dev_attr_read_buffer_kb.attr.name);
2138 comedi_free_board_minor(i);
2141 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2144 "comedi: failed to create sysfs attribute file \"%s\".\n",
2145 dev_attr_max_write_buffer_kb.attr.name);
2146 comedi_free_board_minor(i);
2149 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2152 "comedi: failed to create sysfs attribute file \"%s\".\n",
2153 dev_attr_write_buffer_kb.attr.name);
2154 comedi_free_board_minor(i);
2159 EXPORT_SYMBOL_GPL(comedi_alloc_board_minor);
2161 void comedi_free_board_minor(unsigned minor)
2163 unsigned long flags;
2164 struct comedi_device_file_info *info;
2166 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2167 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2168 info = comedi_file_info_table[minor];
2169 comedi_file_info_table[minor] = NULL;
2170 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2173 struct comedi_device *dev = info->device;
2175 if (dev->class_dev) {
2176 device_destroy(comedi_class,
2177 MKDEV(COMEDI_MAJOR, dev->minor));
2179 comedi_device_cleanup(dev);
2185 EXPORT_SYMBOL_GPL(comedi_free_board_minor);
2187 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2188 struct comedi_subdevice *s)
2190 unsigned long flags;
2191 struct comedi_device_file_info *info;
2192 struct device *csdev;
2196 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2200 info->read_subdevice = s;
2201 info->write_subdevice = s;
2202 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2203 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2204 if (comedi_file_info_table[i] == NULL) {
2205 comedi_file_info_table[i] = info;
2209 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2210 if (i == COMEDI_NUM_MINORS) {
2213 "comedi: error: ran out of minor numbers for board device files.\n");
2217 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
2218 MKDEV(COMEDI_MAJOR, i), NULL, NULL,
2219 "comedi%i_subd%i", dev->minor,
2220 (int)(s - dev->subdevices));
2222 s->class_dev = csdev;
2223 dev_set_drvdata(csdev, info);
2224 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2227 "comedi: failed to create sysfs attribute file \"%s\".\n",
2228 dev_attr_max_read_buffer_kb.attr.name);
2229 comedi_free_subdevice_minor(s);
2232 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2235 "comedi: failed to create sysfs attribute file \"%s\".\n",
2236 dev_attr_read_buffer_kb.attr.name);
2237 comedi_free_subdevice_minor(s);
2240 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2243 "comedi: failed to create sysfs attribute file \"%s\".\n",
2244 dev_attr_max_write_buffer_kb.attr.name);
2245 comedi_free_subdevice_minor(s);
2248 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2251 "comedi: failed to create sysfs attribute file \"%s\".\n",
2252 dev_attr_write_buffer_kb.attr.name);
2253 comedi_free_subdevice_minor(s);
2259 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2261 unsigned long flags;
2262 struct comedi_device_file_info *info;
2269 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2270 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2272 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2273 info = comedi_file_info_table[s->minor];
2274 comedi_file_info_table[s->minor] = NULL;
2275 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2278 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2279 s->class_dev = NULL;
2284 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2286 unsigned long flags;
2287 struct comedi_device_file_info *info;
2289 BUG_ON(minor >= COMEDI_NUM_MINORS);
2290 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2291 info = comedi_file_info_table[minor];
2292 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2295 EXPORT_SYMBOL_GPL(comedi_get_device_file_info);
2297 static int resize_async_buffer(struct comedi_device *dev,
2298 struct comedi_subdevice *s,
2299 struct comedi_async *async, unsigned new_size)
2303 if (new_size > async->max_bufsize)
2307 DPRINTK("subdevice is busy, cannot resize buffer\n");
2310 if (async->mmap_count) {
2311 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2315 if (!async->prealloc_buf)
2318 /* make sure buffer is an integral number of pages
2320 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2322 retval = comedi_buf_alloc(dev, s, new_size);
2326 if (s->buf_change) {
2327 retval = s->buf_change(dev, s, new_size);
2332 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2333 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
2337 /* sysfs attribute files */
2339 static const unsigned bytes_per_kibi = 1024;
2341 static ssize_t show_max_read_buffer_kb(struct device *dev,
2342 struct device_attribute *attr, char *buf)
2345 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2346 unsigned max_buffer_size_kb = 0;
2347 struct comedi_subdevice *const read_subdevice =
2348 comedi_get_read_subdevice(info);
2350 mutex_lock(&info->device->mutex);
2351 if (read_subdevice &&
2352 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2353 read_subdevice->async) {
2354 max_buffer_size_kb = read_subdevice->async->max_bufsize /
2357 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2358 mutex_unlock(&info->device->mutex);
2363 static ssize_t store_max_read_buffer_kb(struct device *dev,
2364 struct device_attribute *attr,
2365 const char *buf, size_t count)
2367 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2368 unsigned long new_max_size_kb;
2369 uint64_t new_max_size;
2370 struct comedi_subdevice *const read_subdevice =
2371 comedi_get_read_subdevice(info);
2373 if (strict_strtoul(buf, 10, &new_max_size_kb))
2375 if (new_max_size_kb != (uint32_t) new_max_size_kb)
2377 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2378 if (new_max_size != (uint32_t) new_max_size)
2381 mutex_lock(&info->device->mutex);
2382 if (read_subdevice == NULL ||
2383 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2384 read_subdevice->async == NULL) {
2385 mutex_unlock(&info->device->mutex);
2388 read_subdevice->async->max_bufsize = new_max_size;
2389 mutex_unlock(&info->device->mutex);
2394 static struct device_attribute dev_attr_max_read_buffer_kb = {
2396 .name = "max_read_buffer_kb",
2397 .mode = S_IRUGO | S_IWUSR},
2398 .show = &show_max_read_buffer_kb,
2399 .store = &store_max_read_buffer_kb
2402 static ssize_t show_read_buffer_kb(struct device *dev,
2403 struct device_attribute *attr, char *buf)
2406 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2407 unsigned buffer_size_kb = 0;
2408 struct comedi_subdevice *const read_subdevice =
2409 comedi_get_read_subdevice(info);
2411 mutex_lock(&info->device->mutex);
2412 if (read_subdevice &&
2413 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2414 read_subdevice->async) {
2415 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
2418 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2419 mutex_unlock(&info->device->mutex);
2424 static ssize_t store_read_buffer_kb(struct device *dev,
2425 struct device_attribute *attr,
2426 const char *buf, size_t count)
2428 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2429 unsigned long new_size_kb;
2432 struct comedi_subdevice *const read_subdevice =
2433 comedi_get_read_subdevice(info);
2435 if (strict_strtoul(buf, 10, &new_size_kb))
2437 if (new_size_kb != (uint32_t) new_size_kb)
2439 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2440 if (new_size != (uint32_t) new_size)
2443 mutex_lock(&info->device->mutex);
2444 if (read_subdevice == NULL ||
2445 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2446 read_subdevice->async == NULL) {
2447 mutex_unlock(&info->device->mutex);
2450 retval = resize_async_buffer(info->device, read_subdevice,
2451 read_subdevice->async, new_size);
2452 mutex_unlock(&info->device->mutex);
2459 static struct device_attribute dev_attr_read_buffer_kb = {
2461 .name = "read_buffer_kb",
2462 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
2463 .show = &show_read_buffer_kb,
2464 .store = &store_read_buffer_kb
2467 static ssize_t show_max_write_buffer_kb(struct device *dev,
2468 struct device_attribute *attr,
2472 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2473 unsigned max_buffer_size_kb = 0;
2474 struct comedi_subdevice *const write_subdevice =
2475 comedi_get_write_subdevice(info);
2477 mutex_lock(&info->device->mutex);
2478 if (write_subdevice &&
2479 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2480 write_subdevice->async) {
2481 max_buffer_size_kb = write_subdevice->async->max_bufsize /
2484 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2485 mutex_unlock(&info->device->mutex);
2490 static ssize_t store_max_write_buffer_kb(struct device *dev,
2491 struct device_attribute *attr,
2492 const char *buf, size_t count)
2494 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2495 unsigned long new_max_size_kb;
2496 uint64_t new_max_size;
2497 struct comedi_subdevice *const write_subdevice =
2498 comedi_get_write_subdevice(info);
2500 if (strict_strtoul(buf, 10, &new_max_size_kb))
2502 if (new_max_size_kb != (uint32_t) new_max_size_kb)
2504 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2505 if (new_max_size != (uint32_t) new_max_size)
2508 mutex_lock(&info->device->mutex);
2509 if (write_subdevice == NULL ||
2510 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2511 write_subdevice->async == NULL) {
2512 mutex_unlock(&info->device->mutex);
2515 write_subdevice->async->max_bufsize = new_max_size;
2516 mutex_unlock(&info->device->mutex);
2521 static struct device_attribute dev_attr_max_write_buffer_kb = {
2523 .name = "max_write_buffer_kb",
2524 .mode = S_IRUGO | S_IWUSR},
2525 .show = &show_max_write_buffer_kb,
2526 .store = &store_max_write_buffer_kb
2529 static ssize_t show_write_buffer_kb(struct device *dev,
2530 struct device_attribute *attr, char *buf)
2533 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2534 unsigned buffer_size_kb = 0;
2535 struct comedi_subdevice *const write_subdevice =
2536 comedi_get_write_subdevice(info);
2538 mutex_lock(&info->device->mutex);
2539 if (write_subdevice &&
2540 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2541 write_subdevice->async) {
2542 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
2545 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2546 mutex_unlock(&info->device->mutex);
2551 static ssize_t store_write_buffer_kb(struct device *dev,
2552 struct device_attribute *attr,
2553 const char *buf, size_t count)
2555 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2556 unsigned long new_size_kb;
2559 struct comedi_subdevice *const write_subdevice =
2560 comedi_get_write_subdevice(info);
2562 if (strict_strtoul(buf, 10, &new_size_kb))
2564 if (new_size_kb != (uint32_t) new_size_kb)
2566 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2567 if (new_size != (uint32_t) new_size)
2570 mutex_lock(&info->device->mutex);
2571 if (write_subdevice == NULL ||
2572 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2573 write_subdevice->async == NULL) {
2574 mutex_unlock(&info->device->mutex);
2577 retval = resize_async_buffer(info->device, write_subdevice,
2578 write_subdevice->async, new_size);
2579 mutex_unlock(&info->device->mutex);
2586 static struct device_attribute dev_attr_write_buffer_kb = {
2588 .name = "write_buffer_kb",
2589 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
2590 .show = &show_write_buffer_kb,
2591 .store = &store_write_buffer_kb