5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define __NO_VERSION__
27 #include "comedi_fops.h"
28 #include "comedi_compat32.h"
30 #include <linux/module.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/fcntl.h>
35 #include <linux/delay.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/kmod.h>
40 #include <linux/poll.h>
41 #include <linux/init.h>
42 #include <linux/device.h>
43 #include <linux/vmalloc.h>
45 #include "comedidev.h"
46 #include <linux/cdev.h>
47 #include <linux/stat.h>
50 #include <linux/uaccess.h>
54 MODULE_AUTHOR("http://www.comedi.org");
55 MODULE_DESCRIPTION("Comedi core module");
56 MODULE_LICENSE("GPL");
58 #ifdef CONFIG_COMEDI_DEBUG
60 EXPORT_SYMBOL(comedi_debug);
61 module_param(comedi_debug, int, 0644);
64 int comedi_autoconfig = 1;
65 module_param(comedi_autoconfig, bool, 0444);
67 static int comedi_num_legacy_minors;
68 module_param(comedi_num_legacy_minors, int, 0444);
70 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
71 static struct comedi_device_file_info
72 *comedi_file_info_table[COMEDI_NUM_MINORS];
74 static int do_devconfig_ioctl(struct comedi_device *dev,
75 struct comedi_devconfig __user *arg);
76 static int do_bufconfig_ioctl(struct comedi_device *dev,
77 struct comedi_bufconfig __user *arg);
78 static int do_devinfo_ioctl(struct comedi_device *dev,
79 struct comedi_devinfo __user *arg,
81 static int do_subdinfo_ioctl(struct comedi_device *dev,
82 struct comedi_subdinfo __user *arg, void *file);
83 static int do_chaninfo_ioctl(struct comedi_device *dev,
84 struct comedi_chaninfo __user *arg);
85 static int do_bufinfo_ioctl(struct comedi_device *dev,
86 struct comedi_bufinfo __user *arg, void *file);
87 static int do_cmd_ioctl(struct comedi_device *dev,
88 struct comedi_cmd __user *arg, void *file);
89 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
91 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
93 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
95 static int do_cmdtest_ioctl(struct comedi_device *dev,
96 struct comedi_cmd __user *arg, void *file);
97 static int do_insnlist_ioctl(struct comedi_device *dev,
98 struct comedi_insnlist __user *arg, void *file);
99 static int do_insn_ioctl(struct comedi_device *dev,
100 struct comedi_insn __user *arg, void *file);
101 static int do_poll_ioctl(struct comedi_device *dev, unsigned int subd,
104 static void do_become_nonbusy(struct comedi_device *dev,
105 struct comedi_subdevice *s);
106 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
108 static int comedi_fasync(int fd, struct file *file, int on);
110 static int is_device_busy(struct comedi_device *dev);
111 static int resize_async_buffer(struct comedi_device *dev,
112 struct comedi_subdevice *s,
113 struct comedi_async *async, unsigned new_size);
115 /* declarations for sysfs attribute files */
116 static struct device_attribute dev_attr_max_read_buffer_kb;
117 static struct device_attribute dev_attr_read_buffer_kb;
118 static struct device_attribute dev_attr_max_write_buffer_kb;
119 static struct device_attribute dev_attr_write_buffer_kb;
121 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
124 const unsigned minor = iminor(file->f_dentry->d_inode);
125 struct comedi_device_file_info *dev_file_info =
126 comedi_get_device_file_info(minor);
127 struct comedi_device *dev;
130 if (dev_file_info == NULL || dev_file_info->device == NULL)
132 dev = dev_file_info->device;
134 mutex_lock(&dev->mutex);
136 /* Device config is special, because it must work on
137 * an unconfigured device. */
138 if (cmd == COMEDI_DEVCONFIG) {
139 if (minor >= COMEDI_NUM_BOARD_MINORS) {
140 /* Device config not appropriate on non-board minors. */
144 rc = do_devconfig_ioctl(dev,
145 (struct comedi_devconfig __user *)arg);
149 if (!dev->attached) {
150 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
156 case COMEDI_BUFCONFIG:
157 rc = do_bufconfig_ioctl(dev,
158 (struct comedi_bufconfig __user *)arg);
161 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
164 case COMEDI_SUBDINFO:
165 rc = do_subdinfo_ioctl(dev,
166 (struct comedi_subdinfo __user *)arg,
169 case COMEDI_CHANINFO:
170 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
172 case COMEDI_RANGEINFO:
173 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
176 rc = do_bufinfo_ioctl(dev,
177 (struct comedi_bufinfo __user *)arg,
181 rc = do_lock_ioctl(dev, arg, file);
184 rc = do_unlock_ioctl(dev, arg, file);
187 rc = do_cancel_ioctl(dev, arg, file);
190 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
193 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
196 case COMEDI_INSNLIST:
197 rc = do_insnlist_ioctl(dev,
198 (struct comedi_insnlist __user *)arg,
202 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
206 rc = do_poll_ioctl(dev, arg, file);
214 mutex_unlock(&dev->mutex);
223 pointer to devconfig structure
226 devconfig structure at arg
231 static int do_devconfig_ioctl(struct comedi_device *dev,
232 struct comedi_devconfig __user *arg)
234 struct comedi_devconfig it;
236 unsigned char *aux_data = NULL;
239 if (!capable(CAP_SYS_ADMIN))
243 if (is_device_busy(dev))
246 struct module *driver_module = dev->driver->module;
247 comedi_device_detach(dev);
248 module_put(driver_module);
253 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
256 it.board_name[COMEDI_NAMELEN - 1] = 0;
258 if (comedi_aux_data(it.options, 0) &&
259 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
261 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
265 aux_data = vmalloc(aux_len);
269 if (copy_from_user(aux_data,
270 comedi_aux_data(it.options, 0), aux_len)) {
274 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
275 (unsigned long)aux_data;
276 if (sizeof(void *) > sizeof(int)) {
277 bit_shift = sizeof(int) * 8;
278 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
279 ((unsigned long)aux_data) >> bit_shift;
281 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
284 ret = comedi_device_attach(dev, &it);
286 if (!try_module_get(dev->driver->module)) {
287 comedi_device_detach(dev);
300 buffer configuration ioctl
303 pointer to bufconfig structure
309 modified bufconfig at arg
312 static int do_bufconfig_ioctl(struct comedi_device *dev,
313 struct comedi_bufconfig __user *arg)
315 struct comedi_bufconfig bc;
316 struct comedi_async *async;
317 struct comedi_subdevice *s;
320 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
323 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
326 s = dev->subdevices + bc.subdevice;
330 DPRINTK("subdevice does not have async capability\n");
336 if (bc.maximum_size) {
337 if (!capable(CAP_SYS_ADMIN))
340 async->max_bufsize = bc.maximum_size;
344 retval = resize_async_buffer(dev, s, async, bc.size);
349 bc.size = async->prealloc_bufsz;
350 bc.maximum_size = async->max_bufsize;
353 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
364 pointer to devinfo structure
373 static int do_devinfo_ioctl(struct comedi_device *dev,
374 struct comedi_devinfo __user *arg,
377 struct comedi_devinfo devinfo;
378 const unsigned minor = iminor(file->f_dentry->d_inode);
379 struct comedi_device_file_info *dev_file_info =
380 comedi_get_device_file_info(minor);
381 struct comedi_subdevice *read_subdev =
382 comedi_get_read_subdevice(dev_file_info);
383 struct comedi_subdevice *write_subdev =
384 comedi_get_write_subdevice(dev_file_info);
386 memset(&devinfo, 0, sizeof(devinfo));
388 /* fill devinfo structure */
389 devinfo.version_code = COMEDI_VERSION_CODE;
390 devinfo.n_subdevs = dev->n_subdevices;
391 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
392 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
395 devinfo.read_subdevice = read_subdev - dev->subdevices;
397 devinfo.read_subdevice = -1;
400 devinfo.write_subdevice = write_subdev - dev->subdevices;
402 devinfo.write_subdevice = -1;
404 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
415 pointer to array of subdevice info structures
421 array of subdevice info structures at arg
424 static int do_subdinfo_ioctl(struct comedi_device *dev,
425 struct comedi_subdinfo __user *arg, void *file)
428 struct comedi_subdinfo *tmp, *us;
429 struct comedi_subdevice *s;
432 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
437 /* fill subdinfo structs */
438 for (i = 0; i < dev->n_subdevices; i++) {
439 s = dev->subdevices + i;
443 us->n_chan = s->n_chan;
444 us->subd_flags = s->subdev_flags;
445 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
446 us->subd_flags |= SDF_RUNNING;
447 #define TIMER_nanosec 5 /* backwards compatibility */
448 us->timer_type = TIMER_nanosec;
449 us->len_chanlist = s->len_chanlist;
450 us->maxdata = s->maxdata;
451 if (s->range_table) {
453 (i << 24) | (0 << 16) | (s->range_table->length);
455 us->range_type = 0; /* XXX */
457 us->flags = s->flags;
460 us->subd_flags |= SDF_BUSY;
462 us->subd_flags |= SDF_BUSY_OWNER;
464 us->subd_flags |= SDF_LOCKED;
466 us->subd_flags |= SDF_LOCK_OWNER;
467 if (!s->maxdata && s->maxdata_list)
468 us->subd_flags |= SDF_MAXDATA;
470 us->subd_flags |= SDF_FLAGS;
471 if (s->range_table_list)
472 us->subd_flags |= SDF_RANGETYPE;
474 us->subd_flags |= SDF_CMD;
476 if (s->insn_bits != &insn_inval)
477 us->insn_bits_support = COMEDI_SUPPORTED;
479 us->insn_bits_support = COMEDI_UNSUPPORTED;
481 us->settling_time_0 = s->settling_time_0;
484 ret = copy_to_user(arg, tmp,
485 dev->n_subdevices * sizeof(struct comedi_subdinfo));
489 return ret ? -EFAULT : 0;
497 pointer to chaninfo structure
500 chaninfo structure at arg
503 arrays at elements of chaninfo structure
506 static int do_chaninfo_ioctl(struct comedi_device *dev,
507 struct comedi_chaninfo __user *arg)
509 struct comedi_subdevice *s;
510 struct comedi_chaninfo it;
512 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
515 if (it.subdev >= dev->n_subdevices)
517 s = dev->subdevices + it.subdev;
519 if (it.maxdata_list) {
520 if (s->maxdata || !s->maxdata_list)
522 if (copy_to_user(it.maxdata_list, s->maxdata_list,
523 s->n_chan * sizeof(unsigned int)))
530 if (copy_to_user(it.flaglist, s->flaglist,
531 s->n_chan * sizeof(unsigned int)))
538 if (!s->range_table_list)
540 for (i = 0; i < s->n_chan; i++) {
543 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
544 (s->range_table_list[i]->length);
545 if (put_user(x, it.rangelist + i))
549 if (copy_to_user(it.rangelist, s->range_type_list,
550 s->n_chan * sizeof(unsigned int)))
560 buffer information ioctl
563 pointer to bufinfo structure
569 modified bufinfo at arg
572 static int do_bufinfo_ioctl(struct comedi_device *dev,
573 struct comedi_bufinfo __user *arg, void *file)
575 struct comedi_bufinfo bi;
576 struct comedi_subdevice *s;
577 struct comedi_async *async;
579 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
582 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
585 s = dev->subdevices + bi.subdevice;
587 if (s->lock && s->lock != file)
593 DPRINTK("subdevice does not have async capability\n");
594 bi.buf_write_ptr = 0;
596 bi.buf_write_count = 0;
597 bi.buf_read_count = 0;
599 bi.bytes_written = 0;
604 bi.bytes_written = 0;
605 goto copyback_position;
610 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
611 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
612 comedi_buf_read_free(async, bi.bytes_read);
614 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
616 && async->buf_write_count == async->buf_read_count) {
617 do_become_nonbusy(dev, s);
621 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
623 comedi_buf_write_alloc(async, bi.bytes_written);
624 comedi_buf_write_free(async, bi.bytes_written);
628 bi.buf_write_count = async->buf_write_count;
629 bi.buf_write_ptr = async->buf_write_ptr;
630 bi.buf_read_count = async->buf_read_count;
631 bi.buf_read_ptr = async->buf_read_ptr;
634 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
640 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
641 unsigned int *data, void *file);
644 * synchronous instructions
647 * pointer to sync cmd structure
650 * sync cmd struct at arg
657 /* arbitrary limits */
658 #define MAX_SAMPLES 256
659 static int do_insnlist_ioctl(struct comedi_device *dev,
660 struct comedi_insnlist __user *arg, void *file)
662 struct comedi_insnlist insnlist;
663 struct comedi_insn *insns = NULL;
664 unsigned int *data = NULL;
668 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
671 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
673 DPRINTK("kmalloc failed\n");
679 kcalloc(insnlist.n_insns, sizeof(struct comedi_insn), GFP_KERNEL);
681 DPRINTK("kmalloc failed\n");
686 if (copy_from_user(insns, insnlist.insns,
687 sizeof(struct comedi_insn) * insnlist.n_insns)) {
688 DPRINTK("copy_from_user failed\n");
693 for (i = 0; i < insnlist.n_insns; i++) {
694 if (insns[i].n > MAX_SAMPLES) {
695 DPRINTK("number of samples too large\n");
699 if (insns[i].insn & INSN_MASK_WRITE) {
700 if (copy_from_user(data, insns[i].data,
701 insns[i].n * sizeof(unsigned int))) {
702 DPRINTK("copy_from_user failed\n");
707 ret = parse_insn(dev, insns + i, data, file);
710 if (insns[i].insn & INSN_MASK_READ) {
711 if (copy_to_user(insns[i].data, data,
712 insns[i].n * sizeof(unsigned int))) {
713 DPRINTK("copy_to_user failed\n");
731 static int check_insn_config_length(struct comedi_insn *insn,
738 case INSN_CONFIG_DIO_OUTPUT:
739 case INSN_CONFIG_DIO_INPUT:
740 case INSN_CONFIG_DISARM:
741 case INSN_CONFIG_RESET:
745 case INSN_CONFIG_ARM:
746 case INSN_CONFIG_DIO_QUERY:
747 case INSN_CONFIG_BLOCK_SIZE:
748 case INSN_CONFIG_FILTER:
749 case INSN_CONFIG_SERIAL_CLOCK:
750 case INSN_CONFIG_BIDIRECTIONAL_DATA:
751 case INSN_CONFIG_ALT_SOURCE:
752 case INSN_CONFIG_SET_COUNTER_MODE:
753 case INSN_CONFIG_8254_READ_STATUS:
754 case INSN_CONFIG_SET_ROUTING:
755 case INSN_CONFIG_GET_ROUTING:
756 case INSN_CONFIG_GET_PWM_STATUS:
757 case INSN_CONFIG_PWM_SET_PERIOD:
758 case INSN_CONFIG_PWM_GET_PERIOD:
762 case INSN_CONFIG_SET_GATE_SRC:
763 case INSN_CONFIG_GET_GATE_SRC:
764 case INSN_CONFIG_SET_CLOCK_SRC:
765 case INSN_CONFIG_GET_CLOCK_SRC:
766 case INSN_CONFIG_SET_OTHER_SRC:
767 case INSN_CONFIG_GET_COUNTER_STATUS:
768 case INSN_CONFIG_PWM_SET_H_BRIDGE:
769 case INSN_CONFIG_PWM_GET_H_BRIDGE:
770 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
774 case INSN_CONFIG_PWM_OUTPUT:
775 case INSN_CONFIG_ANALOG_TRIG:
779 /* by default we allow the insn since we don't have checks for
780 * all possible cases yet */
783 "comedi: no check for data length of config insn id "
784 "%i is implemented.\n"
785 " Add a check to %s in %s.\n"
786 " Assuming n=%i is correct.\n", data[0], __func__,
794 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
795 unsigned int *data, void *file)
797 struct comedi_subdevice *s;
801 if (insn->insn & INSN_MASK_SPECIAL) {
802 /* a non-subdevice instruction */
804 switch (insn->insn) {
814 do_gettimeofday(&tv);
816 data[1] = tv.tv_usec;
822 if (insn->n != 1 || data[0] >= 100000) {
826 udelay(data[0] / 1000);
834 if (insn->subdev >= dev->n_subdevices) {
835 DPRINTK("%d not usable subdevice\n",
840 s = dev->subdevices + insn->subdev;
842 DPRINTK("no async\n");
846 if (!s->async->inttrig) {
847 DPRINTK("no inttrig\n");
851 ret = s->async->inttrig(dev, s, data[0]);
856 DPRINTK("invalid insn\n");
861 /* a subdevice instruction */
862 unsigned int maxdata;
864 if (insn->subdev >= dev->n_subdevices) {
865 DPRINTK("subdevice %d out of range\n", insn->subdev);
869 s = dev->subdevices + insn->subdev;
871 if (s->type == COMEDI_SUBD_UNUSED) {
872 DPRINTK("%d not usable subdevice\n", insn->subdev);
877 /* are we locked? (ioctl lock) */
878 if (s->lock && s->lock != file) {
879 DPRINTK("device locked\n");
884 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
887 DPRINTK("bad chanspec\n");
895 /* This looks arbitrary. It is. */
896 s->busy = &parse_insn;
897 switch (insn->insn) {
899 ret = s->insn_read(dev, s, insn, data);
902 maxdata = s->maxdata_list
903 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
905 for (i = 0; i < insn->n; ++i) {
906 if (data[i] > maxdata) {
908 DPRINTK("bad data value(s)\n");
913 ret = s->insn_write(dev, s, insn, data);
919 /* Most drivers ignore the base channel in
920 * insn->chanspec. Fix this here if
921 * the subdevice has <= 32 channels. */
923 unsigned int orig_mask;
926 if (s->n_chan <= 32) {
927 shift = CR_CHAN(insn->chanspec);
935 ret = s->insn_bits(dev, s, insn, data);
942 ret = check_insn_config_length(insn, data);
945 ret = s->insn_config(dev, s, insn, data);
961 * synchronous instructions
967 * struct comedi_insn struct at arg
973 static int do_insn_ioctl(struct comedi_device *dev,
974 struct comedi_insn __user *arg, void *file)
976 struct comedi_insn insn;
977 unsigned int *data = NULL;
980 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
986 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
991 /* This is where the behavior of insn and insnlist deviate. */
992 if (insn.n > MAX_SAMPLES)
993 insn.n = MAX_SAMPLES;
994 if (insn.insn & INSN_MASK_WRITE) {
995 if (copy_from_user(data,
997 insn.n * sizeof(unsigned int))) {
1002 ret = parse_insn(dev, &insn, data, file);
1005 if (insn.insn & INSN_MASK_READ) {
1006 if (copy_to_user(insn.data,
1008 insn.n * sizeof(unsigned int))) {
1021 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
1022 unsigned mask, unsigned bits)
1024 unsigned long flags;
1026 spin_lock_irqsave(&s->spin_lock, flags);
1027 s->runflags &= ~mask;
1028 s->runflags |= (bits & mask);
1029 spin_unlock_irqrestore(&s->spin_lock, flags);
1032 static int do_cmd_ioctl(struct comedi_device *dev,
1033 struct comedi_cmd __user *cmd, void *file)
1035 struct comedi_cmd user_cmd;
1036 struct comedi_subdevice *s;
1037 struct comedi_async *async;
1039 unsigned int __user *chanlist_saver = NULL;
1041 if (copy_from_user(&user_cmd, cmd, sizeof(struct comedi_cmd))) {
1042 DPRINTK("bad cmd address\n");
1045 /* save user's chanlist pointer so it can be restored later */
1046 chanlist_saver = user_cmd.chanlist;
1048 if (user_cmd.subdev >= dev->n_subdevices) {
1049 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1053 s = dev->subdevices + user_cmd.subdev;
1056 if (s->type == COMEDI_SUBD_UNUSED) {
1057 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1061 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1062 DPRINTK("subdevice %i does not support commands\n",
1067 /* are we locked? (ioctl lock) */
1068 if (s->lock && s->lock != file) {
1069 DPRINTK("subdevice locked\n");
1075 DPRINTK("subdevice busy\n");
1080 /* make sure channel/gain list isn't too long */
1081 if (user_cmd.chanlist_len > s->len_chanlist) {
1082 DPRINTK("channel/gain list too long %u > %d\n",
1083 user_cmd.chanlist_len, s->len_chanlist);
1088 /* make sure channel/gain list isn't too short */
1089 if (user_cmd.chanlist_len < 1) {
1090 DPRINTK("channel/gain list too short %u < 1\n",
1091 user_cmd.chanlist_len);
1096 async->cmd = user_cmd;
1097 async->cmd.data = NULL;
1098 /* load channel/gain list */
1099 async->cmd.chanlist =
1100 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1101 if (!async->cmd.chanlist) {
1102 DPRINTK("allocation failed\n");
1107 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1108 async->cmd.chanlist_len * sizeof(int))) {
1109 DPRINTK("fault reading chanlist\n");
1114 /* make sure each element in channel/gain list is valid */
1115 ret = comedi_check_chanlist(s,
1116 async->cmd.chanlist_len,
1117 async->cmd.chanlist);
1119 DPRINTK("bad chanlist\n");
1123 ret = s->do_cmdtest(dev, s, &async->cmd);
1125 if (async->cmd.flags & TRIG_BOGUS || ret) {
1126 DPRINTK("test returned %d\n", ret);
1127 user_cmd = async->cmd;
1128 /* restore chanlist pointer before copying back */
1129 user_cmd.chanlist = chanlist_saver;
1130 user_cmd.data = NULL;
1131 if (copy_to_user(cmd, &user_cmd, sizeof(struct comedi_cmd))) {
1132 DPRINTK("fault writing cmd\n");
1140 if (!async->prealloc_bufsz) {
1142 DPRINTK("no buffer (?)\n");
1146 comedi_reset_async_buf(async);
1149 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1151 if (async->cmd.flags & TRIG_WAKE_EOS)
1152 async->cb_mask |= COMEDI_CB_EOS;
1154 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1156 ret = s->do_cmd(dev, s);
1161 do_become_nonbusy(dev, s);
1168 command testing ioctl
1171 pointer to cmd structure
1174 cmd structure at arg
1178 modified cmd structure at arg
1181 static int do_cmdtest_ioctl(struct comedi_device *dev,
1182 struct comedi_cmd __user *arg, void *file)
1184 struct comedi_cmd user_cmd;
1185 struct comedi_subdevice *s;
1187 unsigned int *chanlist = NULL;
1188 unsigned int __user *chanlist_saver = NULL;
1190 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
1191 DPRINTK("bad cmd address\n");
1194 /* save user's chanlist pointer so it can be restored later */
1195 chanlist_saver = user_cmd.chanlist;
1197 if (user_cmd.subdev >= dev->n_subdevices) {
1198 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1202 s = dev->subdevices + user_cmd.subdev;
1203 if (s->type == COMEDI_SUBD_UNUSED) {
1204 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1208 if (!s->do_cmd || !s->do_cmdtest) {
1209 DPRINTK("subdevice %i does not support commands\n",
1214 /* make sure channel/gain list isn't too long */
1215 if (user_cmd.chanlist_len > s->len_chanlist) {
1216 DPRINTK("channel/gain list too long %d > %d\n",
1217 user_cmd.chanlist_len, s->len_chanlist);
1222 /* load channel/gain list */
1223 if (user_cmd.chanlist) {
1225 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1227 DPRINTK("allocation failed\n");
1232 if (copy_from_user(chanlist, user_cmd.chanlist,
1233 user_cmd.chanlist_len * sizeof(int))) {
1234 DPRINTK("fault reading chanlist\n");
1239 /* make sure each element in channel/gain list is valid */
1240 ret = comedi_check_chanlist(s, user_cmd.chanlist_len, chanlist);
1242 DPRINTK("bad chanlist\n");
1246 user_cmd.chanlist = chanlist;
1249 ret = s->do_cmdtest(dev, s, &user_cmd);
1251 /* restore chanlist pointer before copying back */
1252 user_cmd.chanlist = chanlist_saver;
1254 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
1255 DPRINTK("bad cmd address\n");
1280 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1284 unsigned long flags;
1285 struct comedi_subdevice *s;
1287 if (arg >= dev->n_subdevices)
1289 s = dev->subdevices + arg;
1291 spin_lock_irqsave(&s->spin_lock, flags);
1292 if (s->busy || s->lock)
1296 spin_unlock_irqrestore(&s->spin_lock, flags);
1303 ret = s->lock_f(dev, s);
1322 This function isn't protected by the semaphore, since
1323 we already own the lock.
1325 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1328 struct comedi_subdevice *s;
1330 if (arg >= dev->n_subdevices)
1332 s = dev->subdevices + arg;
1337 if (s->lock && s->lock != file)
1340 if (s->lock == file) {
1354 cancel acquisition ioctl
1366 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1369 struct comedi_subdevice *s;
1371 if (arg >= dev->n_subdevices)
1373 s = dev->subdevices + arg;
1374 if (s->async == NULL)
1377 if (s->lock && s->lock != file)
1383 if (s->busy != file)
1386 return do_cancel(dev, s);
1391 instructs driver to synchronize buffers
1403 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1406 struct comedi_subdevice *s;
1408 if (arg >= dev->n_subdevices)
1410 s = dev->subdevices + arg;
1412 if (s->lock && s->lock != file)
1418 if (s->busy != file)
1422 return s->poll(dev, s);
1427 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1431 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1432 ret = s->cancel(dev, s);
1434 do_become_nonbusy(dev, s);
1440 static void comedi_vm_open(struct vm_area_struct *area)
1442 struct comedi_async *async;
1443 struct comedi_device *dev;
1445 async = area->vm_private_data;
1446 dev = async->subdevice->device;
1448 mutex_lock(&dev->mutex);
1449 async->mmap_count++;
1450 mutex_unlock(&dev->mutex);
1453 static void comedi_vm_close(struct vm_area_struct *area)
1455 struct comedi_async *async;
1456 struct comedi_device *dev;
1458 async = area->vm_private_data;
1459 dev = async->subdevice->device;
1461 mutex_lock(&dev->mutex);
1462 async->mmap_count--;
1463 mutex_unlock(&dev->mutex);
1466 static struct vm_operations_struct comedi_vm_ops = {
1467 .open = comedi_vm_open,
1468 .close = comedi_vm_close,
1471 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1473 const unsigned minor = iminor(file->f_dentry->d_inode);
1474 struct comedi_async *async = NULL;
1475 unsigned long start = vma->vm_start;
1480 struct comedi_subdevice *s;
1481 struct comedi_device_file_info *dev_file_info;
1482 struct comedi_device *dev;
1484 dev_file_info = comedi_get_device_file_info(minor);
1485 if (dev_file_info == NULL)
1487 dev = dev_file_info->device;
1491 mutex_lock(&dev->mutex);
1492 if (!dev->attached) {
1493 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1497 if (vma->vm_flags & VM_WRITE)
1498 s = comedi_get_write_subdevice(dev_file_info);
1500 s = comedi_get_read_subdevice(dev_file_info);
1507 if (async == NULL) {
1512 if (vma->vm_pgoff != 0) {
1513 DPRINTK("comedi: mmap() offset must be 0.\n");
1518 size = vma->vm_end - vma->vm_start;
1519 if (size > async->prealloc_bufsz) {
1523 if (size & (~PAGE_MASK)) {
1528 n_pages = size >> PAGE_SHIFT;
1529 for (i = 0; i < n_pages; ++i) {
1530 if (remap_pfn_range(vma, start,
1531 page_to_pfn(virt_to_page
1532 (async->buf_page_list
1533 [i].virt_addr)), PAGE_SIZE,
1541 vma->vm_ops = &comedi_vm_ops;
1542 vma->vm_private_data = async;
1544 async->mmap_count++;
1548 mutex_unlock(&dev->mutex);
1552 static unsigned int comedi_poll(struct file *file, poll_table * wait)
1554 unsigned int mask = 0;
1555 const unsigned minor = iminor(file->f_dentry->d_inode);
1556 struct comedi_subdevice *read_subdev;
1557 struct comedi_subdevice *write_subdev;
1558 struct comedi_device_file_info *dev_file_info;
1559 struct comedi_device *dev;
1560 dev_file_info = comedi_get_device_file_info(minor);
1562 if (dev_file_info == NULL)
1564 dev = dev_file_info->device;
1568 mutex_lock(&dev->mutex);
1569 if (!dev->attached) {
1570 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1571 mutex_unlock(&dev->mutex);
1576 read_subdev = comedi_get_read_subdevice(dev_file_info);
1577 if (read_subdev && read_subdev->async) {
1578 poll_wait(file, &read_subdev->async->wait_head, wait);
1579 if (!read_subdev->busy
1580 || comedi_buf_read_n_available(read_subdev->async) > 0
1581 || !(comedi_get_subdevice_runflags(read_subdev) &
1583 mask |= POLLIN | POLLRDNORM;
1586 write_subdev = comedi_get_write_subdevice(dev_file_info);
1587 if (write_subdev && write_subdev->async) {
1588 poll_wait(file, &write_subdev->async->wait_head, wait);
1589 comedi_buf_write_alloc(write_subdev->async,
1590 write_subdev->async->prealloc_bufsz);
1591 if (!write_subdev->busy
1592 || !(comedi_get_subdevice_runflags(write_subdev) &
1594 || comedi_buf_write_n_allocated(write_subdev->async) >=
1595 bytes_per_sample(write_subdev->async->subdevice)) {
1596 mask |= POLLOUT | POLLWRNORM;
1600 mutex_unlock(&dev->mutex);
1604 static ssize_t comedi_write(struct file *file, const char __user *buf,
1605 size_t nbytes, loff_t *offset)
1607 struct comedi_subdevice *s;
1608 struct comedi_async *async;
1609 int n, m, count = 0, retval = 0;
1610 DECLARE_WAITQUEUE(wait, current);
1611 const unsigned minor = iminor(file->f_dentry->d_inode);
1612 struct comedi_device_file_info *dev_file_info;
1613 struct comedi_device *dev;
1614 dev_file_info = comedi_get_device_file_info(minor);
1616 if (dev_file_info == NULL)
1618 dev = dev_file_info->device;
1622 if (!dev->attached) {
1623 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1628 s = comedi_get_write_subdevice(dev_file_info);
1629 if (s == NULL || s->async == NULL) {
1643 if (s->busy != file) {
1647 add_wait_queue(&async->wait_head, &wait);
1648 while (nbytes > 0 && !retval) {
1649 set_current_state(TASK_INTERRUPTIBLE);
1651 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1653 if (comedi_get_subdevice_runflags(s) &
1659 do_become_nonbusy(dev, s);
1667 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1668 m = async->prealloc_bufsz - async->buf_write_ptr;
1669 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1670 if (m > comedi_buf_write_n_allocated(async))
1671 m = comedi_buf_write_n_allocated(async);
1676 if (file->f_flags & O_NONBLOCK) {
1681 if (signal_pending(current)) {
1682 retval = -ERESTARTSYS;
1687 if (s->busy != file) {
1694 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1700 comedi_buf_write_free(async, n);
1706 break; /* makes device work like a pipe */
1708 set_current_state(TASK_RUNNING);
1709 remove_wait_queue(&async->wait_head, &wait);
1712 return count ? count : retval;
1715 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
1718 struct comedi_subdevice *s;
1719 struct comedi_async *async;
1720 int n, m, count = 0, retval = 0;
1721 DECLARE_WAITQUEUE(wait, current);
1722 const unsigned minor = iminor(file->f_dentry->d_inode);
1723 struct comedi_device_file_info *dev_file_info;
1724 struct comedi_device *dev;
1725 dev_file_info = comedi_get_device_file_info(minor);
1727 if (dev_file_info == NULL)
1729 dev = dev_file_info->device;
1733 if (!dev->attached) {
1734 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1739 s = comedi_get_read_subdevice(dev_file_info);
1740 if (s == NULL || s->async == NULL) {
1753 if (s->busy != file) {
1758 add_wait_queue(&async->wait_head, &wait);
1759 while (nbytes > 0 && !retval) {
1760 set_current_state(TASK_INTERRUPTIBLE);
1764 m = comedi_buf_read_n_available(async);
1765 /* printk("%d available\n",m); */
1766 if (async->buf_read_ptr + m > async->prealloc_bufsz)
1767 m = async->prealloc_bufsz - async->buf_read_ptr;
1768 /* printk("%d contiguous\n",m); */
1773 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1774 do_become_nonbusy(dev, s);
1775 if (comedi_get_subdevice_runflags(s) &
1783 if (file->f_flags & O_NONBLOCK) {
1788 if (signal_pending(current)) {
1789 retval = -ERESTARTSYS;
1796 if (s->busy != file) {
1802 m = copy_to_user(buf, async->prealloc_buf +
1803 async->buf_read_ptr, n);
1809 comedi_buf_read_alloc(async, n);
1810 comedi_buf_read_free(async, n);
1816 break; /* makes device work like a pipe */
1818 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
1819 async->buf_read_count - async->buf_write_count == 0) {
1820 do_become_nonbusy(dev, s);
1822 set_current_state(TASK_RUNNING);
1823 remove_wait_queue(&async->wait_head, &wait);
1826 return count ? count : retval;
1830 This function restores a subdevice to an idle state.
1832 void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
1834 struct comedi_async *async = s->async;
1836 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1838 comedi_reset_async_buf(async);
1839 async->inttrig = NULL;
1840 kfree(async->cmd.chanlist);
1841 async->cmd.chanlist = NULL;
1844 "BUG: (?) do_become_nonbusy called with async=0\n");
1850 static int comedi_open(struct inode *inode, struct file *file)
1852 const unsigned minor = iminor(inode);
1853 struct comedi_device_file_info *dev_file_info =
1854 comedi_get_device_file_info(minor);
1855 struct comedi_device *dev =
1856 dev_file_info ? dev_file_info->device : NULL;
1859 DPRINTK("invalid minor number\n");
1863 /* This is slightly hacky, but we want module autoloading
1865 * case: user opens device, attached -> ok
1866 * case: user opens device, unattached, in_request_module=0 -> autoload
1867 * case: user opens device, unattached, in_request_module=1 -> fail
1868 * case: root opens device, attached -> ok
1869 * case: root opens device, unattached, in_request_module=1 -> ok
1870 * (typically called from modprobe)
1871 * case: root opens device, unattached, in_request_module=0 -> autoload
1873 * The last could be changed to "-> ok", which would deny root
1876 mutex_lock(&dev->mutex);
1879 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
1880 DPRINTK("in request module\n");
1881 mutex_unlock(&dev->mutex);
1884 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
1887 dev->in_request_module = 1;
1890 mutex_unlock(&dev->mutex);
1891 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1892 mutex_lock(&dev->mutex);
1895 dev->in_request_module = 0;
1897 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
1898 DPRINTK("not attached and not CAP_NET_ADMIN\n");
1899 mutex_unlock(&dev->mutex);
1903 __module_get(THIS_MODULE);
1905 if (dev->attached) {
1906 if (!try_module_get(dev->driver->module)) {
1907 module_put(THIS_MODULE);
1908 mutex_unlock(&dev->mutex);
1913 if (dev->attached && dev->use_count == 0 && dev->open) {
1914 int rc = dev->open(dev);
1916 module_put(dev->driver->module);
1917 module_put(THIS_MODULE);
1918 mutex_unlock(&dev->mutex);
1925 mutex_unlock(&dev->mutex);
1930 static int comedi_close(struct inode *inode, struct file *file)
1932 const unsigned minor = iminor(inode);
1933 struct comedi_subdevice *s = NULL;
1935 struct comedi_device_file_info *dev_file_info;
1936 struct comedi_device *dev;
1937 dev_file_info = comedi_get_device_file_info(minor);
1939 if (dev_file_info == NULL)
1941 dev = dev_file_info->device;
1945 mutex_lock(&dev->mutex);
1947 if (dev->subdevices) {
1948 for (i = 0; i < dev->n_subdevices; i++) {
1949 s = dev->subdevices + i;
1951 if (s->busy == file)
1953 if (s->lock == file)
1957 if (dev->attached && dev->use_count == 1 && dev->close)
1960 module_put(THIS_MODULE);
1962 module_put(dev->driver->module);
1966 mutex_unlock(&dev->mutex);
1968 if (file->f_flags & FASYNC)
1969 comedi_fasync(-1, file, 0);
1974 static int comedi_fasync(int fd, struct file *file, int on)
1976 const unsigned minor = iminor(file->f_dentry->d_inode);
1977 struct comedi_device_file_info *dev_file_info;
1978 struct comedi_device *dev;
1979 dev_file_info = comedi_get_device_file_info(minor);
1981 if (dev_file_info == NULL)
1983 dev = dev_file_info->device;
1987 return fasync_helper(fd, file, on, &dev->async_queue);
1990 const struct file_operations comedi_fops = {
1991 .owner = THIS_MODULE,
1992 .unlocked_ioctl = comedi_unlocked_ioctl,
1993 .compat_ioctl = comedi_compat_ioctl,
1994 .open = comedi_open,
1995 .release = comedi_close,
1996 .read = comedi_read,
1997 .write = comedi_write,
1998 .mmap = comedi_mmap,
1999 .poll = comedi_poll,
2000 .fasync = comedi_fasync,
2001 .llseek = noop_llseek,
2004 struct class *comedi_class;
2005 static struct cdev comedi_cdev;
2007 static void comedi_cleanup_legacy_minors(void)
2011 for (i = 0; i < comedi_num_legacy_minors; i++)
2012 comedi_free_board_minor(i);
2015 static int __init comedi_init(void)
2020 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
2021 " - http://www.comedi.org\n");
2023 if (comedi_num_legacy_minors < 0 ||
2024 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2025 printk(KERN_ERR "comedi: error: invalid value for module "
2026 "parameter \"comedi_num_legacy_minors\". Valid values "
2027 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
2032 * comedi is unusable if both comedi_autoconfig and
2033 * comedi_num_legacy_minors are zero, so we might as well adjust the
2034 * defaults in that case
2036 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2037 comedi_num_legacy_minors = 16;
2039 memset(comedi_file_info_table, 0,
2040 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
2042 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2043 COMEDI_NUM_MINORS, "comedi");
2046 cdev_init(&comedi_cdev, &comedi_fops);
2047 comedi_cdev.owner = THIS_MODULE;
2048 kobject_set_name(&comedi_cdev.kobj, "comedi");
2049 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2050 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2054 comedi_class = class_create(THIS_MODULE, "comedi");
2055 if (IS_ERR(comedi_class)) {
2056 printk(KERN_ERR "comedi: failed to create class");
2057 cdev_del(&comedi_cdev);
2058 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2060 return PTR_ERR(comedi_class);
2063 /* XXX requires /proc interface */
2066 /* create devices files for legacy/manual use */
2067 for (i = 0; i < comedi_num_legacy_minors; i++) {
2069 minor = comedi_alloc_board_minor(NULL);
2071 comedi_cleanup_legacy_minors();
2072 cdev_del(&comedi_cdev);
2073 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2082 static void __exit comedi_cleanup(void)
2086 comedi_cleanup_legacy_minors();
2087 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2088 BUG_ON(comedi_file_info_table[i]);
2090 class_destroy(comedi_class);
2091 cdev_del(&comedi_cdev);
2092 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2094 comedi_proc_cleanup();
2097 module_init(comedi_init);
2098 module_exit(comedi_cleanup);
2100 void comedi_error(const struct comedi_device *dev, const char *s)
2102 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
2103 dev->driver->driver_name, s);
2105 EXPORT_SYMBOL(comedi_error);
2107 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2109 struct comedi_async *async = s->async;
2110 unsigned runflags = 0;
2111 unsigned runflags_mask = 0;
2113 /* DPRINTK("comedi_event 0x%x\n",mask); */
2115 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2119 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2120 COMEDI_CB_OVERFLOW)) {
2121 runflags_mask |= SRF_RUNNING;
2123 /* remember if an error event has occurred, so an error
2124 * can be returned the next time the user does a read() */
2125 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2126 runflags_mask |= SRF_ERROR;
2127 runflags |= SRF_ERROR;
2129 if (runflags_mask) {
2130 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2131 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2134 if (async->cb_mask & s->async->events) {
2135 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2136 wake_up_interruptible(&async->wait_head);
2137 if (s->subdev_flags & SDF_CMD_READ)
2138 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2139 if (s->subdev_flags & SDF_CMD_WRITE)
2140 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2143 async->cb_func(s->async->events, async->cb_arg);
2146 s->async->events = 0;
2148 EXPORT_SYMBOL(comedi_event);
2150 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
2152 unsigned long flags;
2155 spin_lock_irqsave(&s->spin_lock, flags);
2156 runflags = s->runflags;
2157 spin_unlock_irqrestore(&s->spin_lock, flags);
2160 EXPORT_SYMBOL(comedi_get_subdevice_runflags);
2162 static int is_device_busy(struct comedi_device *dev)
2164 struct comedi_subdevice *s;
2170 for (i = 0; i < dev->n_subdevices; i++) {
2171 s = dev->subdevices + i;
2174 if (s->async && s->async->mmap_count)
2181 static void comedi_device_init(struct comedi_device *dev)
2183 memset(dev, 0, sizeof(struct comedi_device));
2184 spin_lock_init(&dev->spinlock);
2185 mutex_init(&dev->mutex);
2189 static void comedi_device_cleanup(struct comedi_device *dev)
2193 mutex_lock(&dev->mutex);
2194 comedi_device_detach(dev);
2195 mutex_unlock(&dev->mutex);
2196 mutex_destroy(&dev->mutex);
2199 int comedi_alloc_board_minor(struct device *hardware_device)
2201 unsigned long flags;
2202 struct comedi_device_file_info *info;
2203 struct device *csdev;
2207 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2210 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2211 if (info->device == NULL) {
2215 info->hardware_device = hardware_device;
2216 comedi_device_init(info->device);
2217 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2218 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2219 if (comedi_file_info_table[i] == NULL) {
2220 comedi_file_info_table[i] = info;
2224 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2225 if (i == COMEDI_NUM_BOARD_MINORS) {
2226 comedi_device_cleanup(info->device);
2227 kfree(info->device);
2231 "ran out of minor numbers for board device files.\n");
2234 info->device->minor = i;
2235 csdev = device_create(comedi_class, hardware_device,
2236 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2238 info->device->class_dev = csdev;
2239 dev_set_drvdata(csdev, info);
2240 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2244 "failed to create sysfs attribute file \"%s\".\n",
2245 dev_attr_max_read_buffer_kb.attr.name);
2246 comedi_free_board_minor(i);
2249 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2253 "failed to create sysfs attribute file \"%s\".\n",
2254 dev_attr_read_buffer_kb.attr.name);
2255 comedi_free_board_minor(i);
2258 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2262 "failed to create sysfs attribute file \"%s\".\n",
2263 dev_attr_max_write_buffer_kb.attr.name);
2264 comedi_free_board_minor(i);
2267 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2271 "failed to create sysfs attribute file \"%s\".\n",
2272 dev_attr_write_buffer_kb.attr.name);
2273 comedi_free_board_minor(i);
2279 void comedi_free_board_minor(unsigned minor)
2281 unsigned long flags;
2282 struct comedi_device_file_info *info;
2284 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2285 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2286 info = comedi_file_info_table[minor];
2287 comedi_file_info_table[minor] = NULL;
2288 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2291 struct comedi_device *dev = info->device;
2293 if (dev->class_dev) {
2294 device_destroy(comedi_class,
2295 MKDEV(COMEDI_MAJOR, dev->minor));
2297 comedi_device_cleanup(dev);
2304 int comedi_find_board_minor(struct device *hardware_device)
2307 struct comedi_device_file_info *info;
2309 for (minor = 0; minor < COMEDI_NUM_BOARD_MINORS; minor++) {
2310 spin_lock(&comedi_file_info_table_lock);
2311 info = comedi_file_info_table[minor];
2312 if (info && info->hardware_device == hardware_device) {
2313 spin_unlock(&comedi_file_info_table_lock);
2316 spin_unlock(&comedi_file_info_table_lock);
2321 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2322 struct comedi_subdevice *s)
2324 unsigned long flags;
2325 struct comedi_device_file_info *info;
2326 struct device *csdev;
2330 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2334 info->read_subdevice = s;
2335 info->write_subdevice = s;
2336 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2337 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2338 if (comedi_file_info_table[i] == NULL) {
2339 comedi_file_info_table[i] = info;
2343 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2344 if (i == COMEDI_NUM_MINORS) {
2348 "ran out of minor numbers for board device files.\n");
2352 csdev = device_create(comedi_class, dev->class_dev,
2353 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2354 dev->minor, (int)(s - dev->subdevices));
2356 s->class_dev = csdev;
2357 dev_set_drvdata(csdev, info);
2358 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2362 "failed to create sysfs attribute file \"%s\".\n",
2363 dev_attr_max_read_buffer_kb.attr.name);
2364 comedi_free_subdevice_minor(s);
2367 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2371 "failed to create sysfs attribute file \"%s\".\n",
2372 dev_attr_read_buffer_kb.attr.name);
2373 comedi_free_subdevice_minor(s);
2376 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2380 "failed to create sysfs attribute file \"%s\".\n",
2381 dev_attr_max_write_buffer_kb.attr.name);
2382 comedi_free_subdevice_minor(s);
2385 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2389 "failed to create sysfs attribute file \"%s\".\n",
2390 dev_attr_write_buffer_kb.attr.name);
2391 comedi_free_subdevice_minor(s);
2397 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2399 unsigned long flags;
2400 struct comedi_device_file_info *info;
2407 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2408 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2410 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2411 info = comedi_file_info_table[s->minor];
2412 comedi_file_info_table[s->minor] = NULL;
2413 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2416 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2417 s->class_dev = NULL;
2422 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2424 unsigned long flags;
2425 struct comedi_device_file_info *info;
2427 BUG_ON(minor >= COMEDI_NUM_MINORS);
2428 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2429 info = comedi_file_info_table[minor];
2430 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2433 EXPORT_SYMBOL_GPL(comedi_get_device_file_info);
2435 static int resize_async_buffer(struct comedi_device *dev,
2436 struct comedi_subdevice *s,
2437 struct comedi_async *async, unsigned new_size)
2441 if (new_size > async->max_bufsize)
2445 DPRINTK("subdevice is busy, cannot resize buffer\n");
2448 if (async->mmap_count) {
2449 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2453 if (!async->prealloc_buf)
2456 /* make sure buffer is an integral number of pages
2458 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2460 retval = comedi_buf_alloc(dev, s, new_size);
2464 if (s->buf_change) {
2465 retval = s->buf_change(dev, s, new_size);
2470 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2471 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
2475 /* sysfs attribute files */
2477 static const unsigned bytes_per_kibi = 1024;
2479 static ssize_t show_max_read_buffer_kb(struct device *dev,
2480 struct device_attribute *attr, char *buf)
2483 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2484 unsigned max_buffer_size_kb = 0;
2485 struct comedi_subdevice *const read_subdevice =
2486 comedi_get_read_subdevice(info);
2488 mutex_lock(&info->device->mutex);
2489 if (read_subdevice &&
2490 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2491 read_subdevice->async) {
2492 max_buffer_size_kb = read_subdevice->async->max_bufsize /
2495 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2496 mutex_unlock(&info->device->mutex);
2501 static ssize_t store_max_read_buffer_kb(struct device *dev,
2502 struct device_attribute *attr,
2503 const char *buf, size_t count)
2505 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2506 unsigned long new_max_size_kb;
2507 uint64_t new_max_size;
2508 struct comedi_subdevice *const read_subdevice =
2509 comedi_get_read_subdevice(info);
2511 if (strict_strtoul(buf, 10, &new_max_size_kb))
2513 if (new_max_size_kb != (uint32_t) new_max_size_kb)
2515 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2516 if (new_max_size != (uint32_t) new_max_size)
2519 mutex_lock(&info->device->mutex);
2520 if (read_subdevice == NULL ||
2521 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2522 read_subdevice->async == NULL) {
2523 mutex_unlock(&info->device->mutex);
2526 read_subdevice->async->max_bufsize = new_max_size;
2527 mutex_unlock(&info->device->mutex);
2532 static struct device_attribute dev_attr_max_read_buffer_kb = {
2534 .name = "max_read_buffer_kb",
2535 .mode = S_IRUGO | S_IWUSR},
2536 .show = &show_max_read_buffer_kb,
2537 .store = &store_max_read_buffer_kb
2540 static ssize_t show_read_buffer_kb(struct device *dev,
2541 struct device_attribute *attr, char *buf)
2544 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2545 unsigned buffer_size_kb = 0;
2546 struct comedi_subdevice *const read_subdevice =
2547 comedi_get_read_subdevice(info);
2549 mutex_lock(&info->device->mutex);
2550 if (read_subdevice &&
2551 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2552 read_subdevice->async) {
2553 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
2556 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2557 mutex_unlock(&info->device->mutex);
2562 static ssize_t store_read_buffer_kb(struct device *dev,
2563 struct device_attribute *attr,
2564 const char *buf, size_t count)
2566 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2567 unsigned long new_size_kb;
2570 struct comedi_subdevice *const read_subdevice =
2571 comedi_get_read_subdevice(info);
2573 if (strict_strtoul(buf, 10, &new_size_kb))
2575 if (new_size_kb != (uint32_t) new_size_kb)
2577 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2578 if (new_size != (uint32_t) new_size)
2581 mutex_lock(&info->device->mutex);
2582 if (read_subdevice == NULL ||
2583 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2584 read_subdevice->async == NULL) {
2585 mutex_unlock(&info->device->mutex);
2588 retval = resize_async_buffer(info->device, read_subdevice,
2589 read_subdevice->async, new_size);
2590 mutex_unlock(&info->device->mutex);
2597 static struct device_attribute dev_attr_read_buffer_kb = {
2599 .name = "read_buffer_kb",
2600 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
2601 .show = &show_read_buffer_kb,
2602 .store = &store_read_buffer_kb
2605 static ssize_t show_max_write_buffer_kb(struct device *dev,
2606 struct device_attribute *attr,
2610 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2611 unsigned max_buffer_size_kb = 0;
2612 struct comedi_subdevice *const write_subdevice =
2613 comedi_get_write_subdevice(info);
2615 mutex_lock(&info->device->mutex);
2616 if (write_subdevice &&
2617 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2618 write_subdevice->async) {
2619 max_buffer_size_kb = write_subdevice->async->max_bufsize /
2622 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2623 mutex_unlock(&info->device->mutex);
2628 static ssize_t store_max_write_buffer_kb(struct device *dev,
2629 struct device_attribute *attr,
2630 const char *buf, size_t count)
2632 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2633 unsigned long new_max_size_kb;
2634 uint64_t new_max_size;
2635 struct comedi_subdevice *const write_subdevice =
2636 comedi_get_write_subdevice(info);
2638 if (strict_strtoul(buf, 10, &new_max_size_kb))
2640 if (new_max_size_kb != (uint32_t) new_max_size_kb)
2642 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2643 if (new_max_size != (uint32_t) new_max_size)
2646 mutex_lock(&info->device->mutex);
2647 if (write_subdevice == NULL ||
2648 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2649 write_subdevice->async == NULL) {
2650 mutex_unlock(&info->device->mutex);
2653 write_subdevice->async->max_bufsize = new_max_size;
2654 mutex_unlock(&info->device->mutex);
2659 static struct device_attribute dev_attr_max_write_buffer_kb = {
2661 .name = "max_write_buffer_kb",
2662 .mode = S_IRUGO | S_IWUSR},
2663 .show = &show_max_write_buffer_kb,
2664 .store = &store_max_write_buffer_kb
2667 static ssize_t show_write_buffer_kb(struct device *dev,
2668 struct device_attribute *attr, char *buf)
2671 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2672 unsigned buffer_size_kb = 0;
2673 struct comedi_subdevice *const write_subdevice =
2674 comedi_get_write_subdevice(info);
2676 mutex_lock(&info->device->mutex);
2677 if (write_subdevice &&
2678 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2679 write_subdevice->async) {
2680 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
2683 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2684 mutex_unlock(&info->device->mutex);
2689 static ssize_t store_write_buffer_kb(struct device *dev,
2690 struct device_attribute *attr,
2691 const char *buf, size_t count)
2693 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2694 unsigned long new_size_kb;
2697 struct comedi_subdevice *const write_subdevice =
2698 comedi_get_write_subdevice(info);
2700 if (strict_strtoul(buf, 10, &new_size_kb))
2702 if (new_size_kb != (uint32_t) new_size_kb)
2704 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2705 if (new_size != (uint32_t) new_size)
2708 mutex_lock(&info->device->mutex);
2709 if (write_subdevice == NULL ||
2710 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2711 write_subdevice->async == NULL) {
2712 mutex_unlock(&info->device->mutex);
2715 retval = resize_async_buffer(info->device, write_subdevice,
2716 write_subdevice->async, new_size);
2717 mutex_unlock(&info->device->mutex);
2724 static struct device_attribute dev_attr_write_buffer_kb = {
2726 .name = "write_buffer_kb",
2727 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
2728 .show = &show_write_buffer_kb,
2729 .store = &store_write_buffer_kb