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 #include "comedi_compat32.h"
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/fcntl.h>
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/kmod.h>
38 #include <linux/poll.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/vmalloc.h>
43 #include "comedidev.h"
44 #include <linux/cdev.h>
45 #include <linux/stat.h>
48 #include <linux/uaccess.h>
50 #include "comedi_internal.h"
52 MODULE_AUTHOR("http://www.comedi.org");
53 MODULE_DESCRIPTION("Comedi core module");
54 MODULE_LICENSE("GPL");
56 #ifdef CONFIG_COMEDI_DEBUG
58 EXPORT_SYMBOL(comedi_debug);
59 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
60 MODULE_PARM_DESC(comedi_debug,
61 "enable comedi core and driver debugging if non-zero (default 0)"
65 bool comedi_autoconfig = 1;
66 module_param(comedi_autoconfig, bool, S_IRUGO);
67 MODULE_PARM_DESC(comedi_autoconfig,
68 "enable drivers to auto-configure comedi devices (default 1)");
70 static int comedi_num_legacy_minors;
71 module_param(comedi_num_legacy_minors, int, S_IRUGO);
72 MODULE_PARM_DESC(comedi_num_legacy_minors,
73 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
76 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
77 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
78 MODULE_PARM_DESC(comedi_default_buf_size_kb,
79 "default asynchronous buffer size in KiB (default "
80 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
82 unsigned int comedi_default_buf_maxsize_kb
83 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
84 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
85 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
86 "default maximum size of asynchronous buffer in KiB (default "
87 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
89 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
90 static struct comedi_device_file_info
91 *comedi_file_info_table[COMEDI_NUM_MINORS];
93 static void do_become_nonbusy(struct comedi_device *dev,
94 struct comedi_subdevice *s);
95 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
97 static int comedi_fasync(int fd, struct file *file, int on);
99 static int is_device_busy(struct comedi_device *dev);
101 static int resize_async_buffer(struct comedi_device *dev,
102 struct comedi_subdevice *s,
103 struct comedi_async *async, unsigned new_size)
107 if (new_size > async->max_bufsize)
111 DPRINTK("subdevice is busy, cannot resize buffer\n");
114 if (async->mmap_count) {
115 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
119 if (!async->prealloc_buf)
122 /* make sure buffer is an integral number of pages
124 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
126 retval = comedi_buf_alloc(dev, s, new_size);
131 retval = s->buf_change(dev, s, new_size);
136 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
137 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
141 /* sysfs attribute files */
143 static ssize_t show_max_read_buffer_kb(struct device *dev,
144 struct device_attribute *attr, char *buf)
146 struct comedi_device_file_info *info = dev_get_drvdata(dev);
147 struct comedi_subdevice *s = comedi_get_read_subdevice(info);
148 unsigned int size = 0;
150 mutex_lock(&info->device->mutex);
151 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
152 size = s->async->max_bufsize / 1024;
153 mutex_unlock(&info->device->mutex);
155 return snprintf(buf, PAGE_SIZE, "%i\n", size);
158 static ssize_t store_max_read_buffer_kb(struct device *dev,
159 struct device_attribute *attr,
160 const char *buf, size_t count)
162 struct comedi_device_file_info *info = dev_get_drvdata(dev);
163 struct comedi_subdevice *s = comedi_get_read_subdevice(info);
167 err = kstrtouint(buf, 10, &size);
170 if (size > (UINT_MAX / 1024))
174 mutex_lock(&info->device->mutex);
175 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
176 s->async->max_bufsize = size;
179 mutex_unlock(&info->device->mutex);
181 return err ? err : count;
184 static ssize_t show_read_buffer_kb(struct device *dev,
185 struct device_attribute *attr, char *buf)
187 struct comedi_device_file_info *info = dev_get_drvdata(dev);
188 struct comedi_subdevice *s = comedi_get_read_subdevice(info);
189 unsigned int size = 0;
191 mutex_lock(&info->device->mutex);
192 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
193 size = s->async->prealloc_bufsz / 1024;
194 mutex_unlock(&info->device->mutex);
196 return snprintf(buf, PAGE_SIZE, "%i\n", size);
199 static ssize_t store_read_buffer_kb(struct device *dev,
200 struct device_attribute *attr,
201 const char *buf, size_t count)
203 struct comedi_device_file_info *info = dev_get_drvdata(dev);
204 struct comedi_subdevice *s = comedi_get_read_subdevice(info);
208 err = kstrtouint(buf, 10, &size);
211 if (size > (UINT_MAX / 1024))
215 mutex_lock(&info->device->mutex);
216 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
217 err = resize_async_buffer(info->device, s, s->async, size);
220 mutex_unlock(&info->device->mutex);
222 return err ? err : count;
225 static ssize_t show_max_write_buffer_kb(struct device *dev,
226 struct device_attribute *attr,
229 struct comedi_device_file_info *info = dev_get_drvdata(dev);
230 struct comedi_subdevice *s = comedi_get_write_subdevice(info);
231 unsigned int size = 0;
233 mutex_lock(&info->device->mutex);
234 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
235 size = s->async->max_bufsize / 1024;
236 mutex_unlock(&info->device->mutex);
238 return snprintf(buf, PAGE_SIZE, "%i\n", size);
241 static ssize_t store_max_write_buffer_kb(struct device *dev,
242 struct device_attribute *attr,
243 const char *buf, size_t count)
245 struct comedi_device_file_info *info = dev_get_drvdata(dev);
246 struct comedi_subdevice *s = comedi_get_write_subdevice(info);
250 err = kstrtouint(buf, 10, &size);
253 if (size > (UINT_MAX / 1024))
257 mutex_lock(&info->device->mutex);
258 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
259 s->async->max_bufsize = size;
262 mutex_unlock(&info->device->mutex);
264 return err ? err : count;
267 static ssize_t show_write_buffer_kb(struct device *dev,
268 struct device_attribute *attr, char *buf)
270 struct comedi_device_file_info *info = dev_get_drvdata(dev);
271 struct comedi_subdevice *s = comedi_get_write_subdevice(info);
272 unsigned int size = 0;
274 mutex_lock(&info->device->mutex);
275 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
276 size = s->async->prealloc_bufsz / 1024;
277 mutex_unlock(&info->device->mutex);
279 return snprintf(buf, PAGE_SIZE, "%i\n", size);
282 static ssize_t store_write_buffer_kb(struct device *dev,
283 struct device_attribute *attr,
284 const char *buf, size_t count)
286 struct comedi_device_file_info *info = dev_get_drvdata(dev);
287 struct comedi_subdevice *s = comedi_get_write_subdevice(info);
291 err = kstrtouint(buf, 10, &size);
294 if (size > (UINT_MAX / 1024))
298 mutex_lock(&info->device->mutex);
299 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
300 err = resize_async_buffer(info->device, s, s->async, size);
303 mutex_unlock(&info->device->mutex);
305 return err ? err : count;
308 static struct device_attribute comedi_dev_attrs[] = {
309 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
310 show_max_read_buffer_kb, store_max_read_buffer_kb),
311 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
312 show_read_buffer_kb, store_read_buffer_kb),
313 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
314 show_max_write_buffer_kb, store_max_write_buffer_kb),
315 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
316 show_write_buffer_kb, store_write_buffer_kb),
325 pointer to devconfig structure
328 devconfig structure at arg
333 static int do_devconfig_ioctl(struct comedi_device *dev,
334 struct comedi_devconfig __user *arg)
336 struct comedi_devconfig it;
338 unsigned char *aux_data = NULL;
341 if (!capable(CAP_SYS_ADMIN))
345 if (is_device_busy(dev))
348 struct module *driver_module = dev->driver->module;
349 comedi_device_detach(dev);
350 module_put(driver_module);
355 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
358 it.board_name[COMEDI_NAMELEN - 1] = 0;
360 if (comedi_aux_data(it.options, 0) &&
361 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
363 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
367 aux_data = vmalloc(aux_len);
371 if (copy_from_user(aux_data,
372 (unsigned char __user *
373 )comedi_aux_data(it.options, 0), aux_len)) {
377 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
378 (unsigned long)aux_data;
379 if (sizeof(void *) > sizeof(int)) {
380 bit_shift = sizeof(int) * 8;
381 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
382 ((unsigned long)aux_data) >> bit_shift;
384 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
387 ret = comedi_device_attach(dev, &it);
389 if (!try_module_get(dev->driver->module)) {
390 comedi_device_detach(dev);
403 buffer configuration ioctl
406 pointer to bufconfig structure
412 modified bufconfig at arg
415 static int do_bufconfig_ioctl(struct comedi_device *dev,
416 struct comedi_bufconfig __user *arg)
418 struct comedi_bufconfig bc;
419 struct comedi_async *async;
420 struct comedi_subdevice *s;
423 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
426 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
429 s = &dev->subdevices[bc.subdevice];
433 DPRINTK("subdevice does not have async capability\n");
439 if (bc.maximum_size) {
440 if (!capable(CAP_SYS_ADMIN))
443 async->max_bufsize = bc.maximum_size;
447 retval = resize_async_buffer(dev, s, async, bc.size);
452 bc.size = async->prealloc_bufsz;
453 bc.maximum_size = async->max_bufsize;
456 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
467 pointer to devinfo structure
476 static int do_devinfo_ioctl(struct comedi_device *dev,
477 struct comedi_devinfo __user *arg,
480 struct comedi_devinfo devinfo;
481 const unsigned minor = iminor(file->f_dentry->d_inode);
482 struct comedi_device_file_info *dev_file_info =
483 comedi_get_device_file_info(minor);
484 struct comedi_subdevice *read_subdev =
485 comedi_get_read_subdevice(dev_file_info);
486 struct comedi_subdevice *write_subdev =
487 comedi_get_write_subdevice(dev_file_info);
489 memset(&devinfo, 0, sizeof(devinfo));
491 /* fill devinfo structure */
492 devinfo.version_code = COMEDI_VERSION_CODE;
493 devinfo.n_subdevs = dev->n_subdevices;
494 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
495 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
498 devinfo.read_subdevice = read_subdev - dev->subdevices;
500 devinfo.read_subdevice = -1;
503 devinfo.write_subdevice = write_subdev - dev->subdevices;
505 devinfo.write_subdevice = -1;
507 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
518 pointer to array of subdevice info structures
524 array of subdevice info structures at arg
527 static int do_subdinfo_ioctl(struct comedi_device *dev,
528 struct comedi_subdinfo __user *arg, void *file)
531 struct comedi_subdinfo *tmp, *us;
532 struct comedi_subdevice *s;
535 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
540 /* fill subdinfo structs */
541 for (i = 0; i < dev->n_subdevices; i++) {
542 s = &dev->subdevices[i];
546 us->n_chan = s->n_chan;
547 us->subd_flags = s->subdev_flags;
548 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
549 us->subd_flags |= SDF_RUNNING;
550 #define TIMER_nanosec 5 /* backwards compatibility */
551 us->timer_type = TIMER_nanosec;
552 us->len_chanlist = s->len_chanlist;
553 us->maxdata = s->maxdata;
554 if (s->range_table) {
556 (i << 24) | (0 << 16) | (s->range_table->length);
558 us->range_type = 0; /* XXX */
560 us->flags = s->flags;
563 us->subd_flags |= SDF_BUSY;
565 us->subd_flags |= SDF_BUSY_OWNER;
567 us->subd_flags |= SDF_LOCKED;
569 us->subd_flags |= SDF_LOCK_OWNER;
570 if (!s->maxdata && s->maxdata_list)
571 us->subd_flags |= SDF_MAXDATA;
573 us->subd_flags |= SDF_FLAGS;
574 if (s->range_table_list)
575 us->subd_flags |= SDF_RANGETYPE;
577 us->subd_flags |= SDF_CMD;
579 if (s->insn_bits != &insn_inval)
580 us->insn_bits_support = COMEDI_SUPPORTED;
582 us->insn_bits_support = COMEDI_UNSUPPORTED;
584 us->settling_time_0 = s->settling_time_0;
587 ret = copy_to_user(arg, tmp,
588 dev->n_subdevices * sizeof(struct comedi_subdinfo));
592 return ret ? -EFAULT : 0;
600 pointer to chaninfo structure
603 chaninfo structure at arg
606 arrays at elements of chaninfo structure
609 static int do_chaninfo_ioctl(struct comedi_device *dev,
610 struct comedi_chaninfo __user *arg)
612 struct comedi_subdevice *s;
613 struct comedi_chaninfo it;
615 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
618 if (it.subdev >= dev->n_subdevices)
620 s = &dev->subdevices[it.subdev];
622 if (it.maxdata_list) {
623 if (s->maxdata || !s->maxdata_list)
625 if (copy_to_user(it.maxdata_list, s->maxdata_list,
626 s->n_chan * sizeof(unsigned int)))
633 if (copy_to_user(it.flaglist, s->flaglist,
634 s->n_chan * sizeof(unsigned int)))
641 if (!s->range_table_list)
643 for (i = 0; i < s->n_chan; i++) {
646 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
647 (s->range_table_list[i]->length);
648 if (put_user(x, it.rangelist + i))
652 if (copy_to_user(it.rangelist, s->range_type_list,
653 s->n_chan * sizeof(unsigned int)))
663 buffer information ioctl
666 pointer to bufinfo structure
672 modified bufinfo at arg
675 static int do_bufinfo_ioctl(struct comedi_device *dev,
676 struct comedi_bufinfo __user *arg, void *file)
678 struct comedi_bufinfo bi;
679 struct comedi_subdevice *s;
680 struct comedi_async *async;
682 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
685 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
688 s = &dev->subdevices[bi.subdevice];
690 if (s->lock && s->lock != file)
696 DPRINTK("subdevice does not have async capability\n");
697 bi.buf_write_ptr = 0;
699 bi.buf_write_count = 0;
700 bi.buf_read_count = 0;
702 bi.bytes_written = 0;
707 bi.bytes_written = 0;
708 goto copyback_position;
713 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
714 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
715 comedi_buf_read_free(async, bi.bytes_read);
717 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
719 && async->buf_write_count == async->buf_read_count) {
720 do_become_nonbusy(dev, s);
724 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
726 comedi_buf_write_alloc(async, bi.bytes_written);
727 comedi_buf_write_free(async, bi.bytes_written);
731 bi.buf_write_count = async->buf_write_count;
732 bi.buf_write_ptr = async->buf_write_ptr;
733 bi.buf_read_count = async->buf_read_count;
734 bi.buf_read_ptr = async->buf_read_ptr;
737 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
743 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
744 unsigned int *data, void *file);
747 * synchronous instructions
750 * pointer to sync cmd structure
753 * sync cmd struct at arg
760 /* arbitrary limits */
761 #define MAX_SAMPLES 256
762 static int do_insnlist_ioctl(struct comedi_device *dev,
763 struct comedi_insnlist __user *arg, void *file)
765 struct comedi_insnlist insnlist;
766 struct comedi_insn *insns = NULL;
767 unsigned int *data = NULL;
771 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
774 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
776 DPRINTK("kmalloc failed\n");
782 kcalloc(insnlist.n_insns, sizeof(struct comedi_insn), GFP_KERNEL);
784 DPRINTK("kmalloc failed\n");
789 if (copy_from_user(insns, insnlist.insns,
790 sizeof(struct comedi_insn) * insnlist.n_insns)) {
791 DPRINTK("copy_from_user failed\n");
796 for (i = 0; i < insnlist.n_insns; i++) {
797 if (insns[i].n > MAX_SAMPLES) {
798 DPRINTK("number of samples too large\n");
802 if (insns[i].insn & INSN_MASK_WRITE) {
803 if (copy_from_user(data, insns[i].data,
804 insns[i].n * sizeof(unsigned int))) {
805 DPRINTK("copy_from_user failed\n");
810 ret = parse_insn(dev, insns + i, data, file);
813 if (insns[i].insn & INSN_MASK_READ) {
814 if (copy_to_user(insns[i].data, data,
815 insns[i].n * sizeof(unsigned int))) {
816 DPRINTK("copy_to_user failed\n");
834 static int check_insn_config_length(struct comedi_insn *insn,
841 case INSN_CONFIG_DIO_OUTPUT:
842 case INSN_CONFIG_DIO_INPUT:
843 case INSN_CONFIG_DISARM:
844 case INSN_CONFIG_RESET:
848 case INSN_CONFIG_ARM:
849 case INSN_CONFIG_DIO_QUERY:
850 case INSN_CONFIG_BLOCK_SIZE:
851 case INSN_CONFIG_FILTER:
852 case INSN_CONFIG_SERIAL_CLOCK:
853 case INSN_CONFIG_BIDIRECTIONAL_DATA:
854 case INSN_CONFIG_ALT_SOURCE:
855 case INSN_CONFIG_SET_COUNTER_MODE:
856 case INSN_CONFIG_8254_READ_STATUS:
857 case INSN_CONFIG_SET_ROUTING:
858 case INSN_CONFIG_GET_ROUTING:
859 case INSN_CONFIG_GET_PWM_STATUS:
860 case INSN_CONFIG_PWM_SET_PERIOD:
861 case INSN_CONFIG_PWM_GET_PERIOD:
865 case INSN_CONFIG_SET_GATE_SRC:
866 case INSN_CONFIG_GET_GATE_SRC:
867 case INSN_CONFIG_SET_CLOCK_SRC:
868 case INSN_CONFIG_GET_CLOCK_SRC:
869 case INSN_CONFIG_SET_OTHER_SRC:
870 case INSN_CONFIG_GET_COUNTER_STATUS:
871 case INSN_CONFIG_PWM_SET_H_BRIDGE:
872 case INSN_CONFIG_PWM_GET_H_BRIDGE:
873 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
877 case INSN_CONFIG_PWM_OUTPUT:
878 case INSN_CONFIG_ANALOG_TRIG:
882 case INSN_CONFIG_DIGITAL_TRIG:
886 /* by default we allow the insn since we don't have checks for
887 * all possible cases yet */
889 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
891 pr_warn("comedi: Add a check to %s in %s.\n",
893 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
899 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
900 unsigned int *data, void *file)
902 struct comedi_subdevice *s;
906 if (insn->insn & INSN_MASK_SPECIAL) {
907 /* a non-subdevice instruction */
909 switch (insn->insn) {
919 do_gettimeofday(&tv);
921 data[1] = tv.tv_usec;
927 if (insn->n != 1 || data[0] >= 100000) {
931 udelay(data[0] / 1000);
939 if (insn->subdev >= dev->n_subdevices) {
940 DPRINTK("%d not usable subdevice\n",
945 s = &dev->subdevices[insn->subdev];
947 DPRINTK("no async\n");
951 if (!s->async->inttrig) {
952 DPRINTK("no inttrig\n");
956 ret = s->async->inttrig(dev, s, data[0]);
961 DPRINTK("invalid insn\n");
966 /* a subdevice instruction */
967 unsigned int maxdata;
969 if (insn->subdev >= dev->n_subdevices) {
970 DPRINTK("subdevice %d out of range\n", insn->subdev);
974 s = &dev->subdevices[insn->subdev];
976 if (s->type == COMEDI_SUBD_UNUSED) {
977 DPRINTK("%d not usable subdevice\n", insn->subdev);
982 /* are we locked? (ioctl lock) */
983 if (s->lock && s->lock != file) {
984 DPRINTK("device locked\n");
989 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
992 DPRINTK("bad chanspec\n");
1000 /* This looks arbitrary. It is. */
1001 s->busy = &parse_insn;
1002 switch (insn->insn) {
1004 ret = s->insn_read(dev, s, insn, data);
1007 maxdata = s->maxdata_list
1008 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1010 for (i = 0; i < insn->n; ++i) {
1011 if (data[i] > maxdata) {
1013 DPRINTK("bad data value(s)\n");
1018 ret = s->insn_write(dev, s, insn, data);
1024 /* Most drivers ignore the base channel in
1025 * insn->chanspec. Fix this here if
1026 * the subdevice has <= 32 channels. */
1028 unsigned int orig_mask;
1030 orig_mask = data[0];
1031 if (s->n_chan <= 32) {
1032 shift = CR_CHAN(insn->chanspec);
1040 ret = s->insn_bits(dev, s, insn, data);
1041 data[0] = orig_mask;
1047 ret = check_insn_config_length(insn, data);
1050 ret = s->insn_config(dev, s, insn, data);
1066 * synchronous instructions
1072 * struct comedi_insn struct at arg
1078 static int do_insn_ioctl(struct comedi_device *dev,
1079 struct comedi_insn __user *arg, void *file)
1081 struct comedi_insn insn;
1082 unsigned int *data = NULL;
1085 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1091 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
1096 /* This is where the behavior of insn and insnlist deviate. */
1097 if (insn.n > MAX_SAMPLES)
1098 insn.n = MAX_SAMPLES;
1099 if (insn.insn & INSN_MASK_WRITE) {
1100 if (copy_from_user(data,
1102 insn.n * sizeof(unsigned int))) {
1107 ret = parse_insn(dev, &insn, data, file);
1110 if (insn.insn & INSN_MASK_READ) {
1111 if (copy_to_user(insn.data,
1113 insn.n * sizeof(unsigned int))) {
1126 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
1127 unsigned mask, unsigned bits)
1129 unsigned long flags;
1131 spin_lock_irqsave(&s->spin_lock, flags);
1132 s->runflags &= ~mask;
1133 s->runflags |= (bits & mask);
1134 spin_unlock_irqrestore(&s->spin_lock, flags);
1137 static int do_cmd_ioctl(struct comedi_device *dev,
1138 struct comedi_cmd __user *arg, void *file)
1140 struct comedi_cmd cmd;
1141 struct comedi_subdevice *s;
1142 struct comedi_async *async;
1144 unsigned int __user *user_chanlist;
1146 if (copy_from_user(&cmd, arg, sizeof(struct comedi_cmd))) {
1147 DPRINTK("bad cmd address\n");
1150 /* save user's chanlist pointer so it can be restored later */
1151 user_chanlist = (unsigned int __user *)cmd.chanlist;
1153 if (cmd.subdev >= dev->n_subdevices) {
1154 DPRINTK("%d no such subdevice\n", cmd.subdev);
1158 s = &dev->subdevices[cmd.subdev];
1161 if (s->type == COMEDI_SUBD_UNUSED) {
1162 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1166 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1167 DPRINTK("subdevice %i does not support commands\n",
1172 /* are we locked? (ioctl lock) */
1173 if (s->lock && s->lock != file) {
1174 DPRINTK("subdevice locked\n");
1180 DPRINTK("subdevice busy\n");
1185 /* make sure channel/gain list isn't too long */
1186 if (cmd.chanlist_len > s->len_chanlist) {
1187 DPRINTK("channel/gain list too long %u > %d\n",
1188 cmd.chanlist_len, s->len_chanlist);
1193 /* make sure channel/gain list isn't too short */
1194 if (cmd.chanlist_len < 1) {
1195 DPRINTK("channel/gain list too short %u < 1\n",
1202 async->cmd.data = NULL;
1203 /* load channel/gain list */
1204 async->cmd.chanlist =
1205 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1206 if (!async->cmd.chanlist) {
1207 DPRINTK("allocation failed\n");
1212 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1213 async->cmd.chanlist_len * sizeof(int))) {
1214 DPRINTK("fault reading chanlist\n");
1219 /* make sure each element in channel/gain list is valid */
1220 ret = comedi_check_chanlist(s,
1221 async->cmd.chanlist_len,
1222 async->cmd.chanlist);
1224 DPRINTK("bad chanlist\n");
1228 ret = s->do_cmdtest(dev, s, &async->cmd);
1230 if (async->cmd.flags & TRIG_BOGUS || ret) {
1231 DPRINTK("test returned %d\n", ret);
1233 /* restore chanlist pointer before copying back */
1234 cmd.chanlist = (unsigned int __force *)user_chanlist;
1236 if (copy_to_user(arg, &cmd, sizeof(struct comedi_cmd))) {
1237 DPRINTK("fault writing cmd\n");
1245 if (!async->prealloc_bufsz) {
1247 DPRINTK("no buffer (?)\n");
1251 comedi_reset_async_buf(async);
1254 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1256 if (async->cmd.flags & TRIG_WAKE_EOS)
1257 async->cb_mask |= COMEDI_CB_EOS;
1259 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1261 ret = s->do_cmd(dev, s);
1266 do_become_nonbusy(dev, s);
1273 command testing ioctl
1276 pointer to cmd structure
1279 cmd structure at arg
1283 modified cmd structure at arg
1286 static int do_cmdtest_ioctl(struct comedi_device *dev,
1287 struct comedi_cmd __user *arg, void *file)
1289 struct comedi_cmd cmd;
1290 struct comedi_subdevice *s;
1292 unsigned int *chanlist = NULL;
1293 unsigned int __user *user_chanlist;
1295 if (copy_from_user(&cmd, arg, sizeof(struct comedi_cmd))) {
1296 DPRINTK("bad cmd address\n");
1299 /* save user's chanlist pointer so it can be restored later */
1300 user_chanlist = (unsigned int __user *)cmd.chanlist;
1302 if (cmd.subdev >= dev->n_subdevices) {
1303 DPRINTK("%d no such subdevice\n", cmd.subdev);
1307 s = &dev->subdevices[cmd.subdev];
1308 if (s->type == COMEDI_SUBD_UNUSED) {
1309 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1313 if (!s->do_cmd || !s->do_cmdtest) {
1314 DPRINTK("subdevice %i does not support commands\n",
1319 /* make sure channel/gain list isn't too long */
1320 if (cmd.chanlist_len > s->len_chanlist) {
1321 DPRINTK("channel/gain list too long %d > %d\n",
1322 cmd.chanlist_len, s->len_chanlist);
1327 /* load channel/gain list */
1330 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1332 DPRINTK("allocation failed\n");
1337 if (copy_from_user(chanlist, user_chanlist,
1338 cmd.chanlist_len * sizeof(int))) {
1339 DPRINTK("fault reading chanlist\n");
1344 /* make sure each element in channel/gain list is valid */
1345 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1347 DPRINTK("bad chanlist\n");
1351 cmd.chanlist = chanlist;
1354 ret = s->do_cmdtest(dev, s, &cmd);
1356 /* restore chanlist pointer before copying back */
1357 cmd.chanlist = (unsigned int __force *)user_chanlist;
1359 if (copy_to_user(arg, &cmd, sizeof(struct comedi_cmd))) {
1360 DPRINTK("bad cmd address\n");
1385 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1389 unsigned long flags;
1390 struct comedi_subdevice *s;
1392 if (arg >= dev->n_subdevices)
1394 s = &dev->subdevices[arg];
1396 spin_lock_irqsave(&s->spin_lock, flags);
1397 if (s->busy || s->lock)
1401 spin_unlock_irqrestore(&s->spin_lock, flags);
1408 ret = s->lock_f(dev, s);
1427 This function isn't protected by the semaphore, since
1428 we already own the lock.
1430 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1433 struct comedi_subdevice *s;
1435 if (arg >= dev->n_subdevices)
1437 s = &dev->subdevices[arg];
1442 if (s->lock && s->lock != file)
1445 if (s->lock == file) {
1459 cancel acquisition ioctl
1471 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1474 struct comedi_subdevice *s;
1476 if (arg >= dev->n_subdevices)
1478 s = &dev->subdevices[arg];
1479 if (s->async == NULL)
1482 if (s->lock && s->lock != file)
1488 if (s->busy != file)
1491 return do_cancel(dev, s);
1496 instructs driver to synchronize buffers
1508 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1511 struct comedi_subdevice *s;
1513 if (arg >= dev->n_subdevices)
1515 s = &dev->subdevices[arg];
1517 if (s->lock && s->lock != file)
1523 if (s->busy != file)
1527 return s->poll(dev, s);
1532 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1535 const unsigned minor = iminor(file->f_dentry->d_inode);
1536 struct comedi_device_file_info *dev_file_info =
1537 comedi_get_device_file_info(minor);
1538 struct comedi_device *dev;
1541 if (dev_file_info == NULL || dev_file_info->device == NULL)
1543 dev = dev_file_info->device;
1545 mutex_lock(&dev->mutex);
1547 /* Device config is special, because it must work on
1548 * an unconfigured device. */
1549 if (cmd == COMEDI_DEVCONFIG) {
1550 rc = do_devconfig_ioctl(dev,
1551 (struct comedi_devconfig __user *)arg);
1555 if (!dev->attached) {
1556 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1562 case COMEDI_BUFCONFIG:
1563 rc = do_bufconfig_ioctl(dev,
1564 (struct comedi_bufconfig __user *)arg);
1566 case COMEDI_DEVINFO:
1567 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1570 case COMEDI_SUBDINFO:
1571 rc = do_subdinfo_ioctl(dev,
1572 (struct comedi_subdinfo __user *)arg,
1575 case COMEDI_CHANINFO:
1576 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1578 case COMEDI_RANGEINFO:
1579 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1581 case COMEDI_BUFINFO:
1582 rc = do_bufinfo_ioctl(dev,
1583 (struct comedi_bufinfo __user *)arg,
1587 rc = do_lock_ioctl(dev, arg, file);
1590 rc = do_unlock_ioctl(dev, arg, file);
1593 rc = do_cancel_ioctl(dev, arg, file);
1596 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1598 case COMEDI_CMDTEST:
1599 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1602 case COMEDI_INSNLIST:
1603 rc = do_insnlist_ioctl(dev,
1604 (struct comedi_insnlist __user *)arg,
1608 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1612 rc = do_poll_ioctl(dev, arg, file);
1620 mutex_unlock(&dev->mutex);
1624 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1628 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1629 ret = s->cancel(dev, s);
1631 do_become_nonbusy(dev, s);
1637 static void comedi_vm_open(struct vm_area_struct *area)
1639 struct comedi_async *async;
1640 struct comedi_device *dev;
1642 async = area->vm_private_data;
1643 dev = async->subdevice->device;
1645 mutex_lock(&dev->mutex);
1646 async->mmap_count++;
1647 mutex_unlock(&dev->mutex);
1650 static void comedi_vm_close(struct vm_area_struct *area)
1652 struct comedi_async *async;
1653 struct comedi_device *dev;
1655 async = area->vm_private_data;
1656 dev = async->subdevice->device;
1658 mutex_lock(&dev->mutex);
1659 async->mmap_count--;
1660 mutex_unlock(&dev->mutex);
1663 static struct vm_operations_struct comedi_vm_ops = {
1664 .open = comedi_vm_open,
1665 .close = comedi_vm_close,
1668 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1670 const unsigned minor = iminor(file->f_dentry->d_inode);
1671 struct comedi_async *async = NULL;
1672 unsigned long start = vma->vm_start;
1677 struct comedi_subdevice *s;
1678 struct comedi_device_file_info *dev_file_info;
1679 struct comedi_device *dev;
1681 dev_file_info = comedi_get_device_file_info(minor);
1682 if (dev_file_info == NULL)
1684 dev = dev_file_info->device;
1688 mutex_lock(&dev->mutex);
1689 if (!dev->attached) {
1690 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1694 if (vma->vm_flags & VM_WRITE)
1695 s = comedi_get_write_subdevice(dev_file_info);
1697 s = comedi_get_read_subdevice(dev_file_info);
1704 if (async == NULL) {
1709 if (vma->vm_pgoff != 0) {
1710 DPRINTK("comedi: mmap() offset must be 0.\n");
1715 size = vma->vm_end - vma->vm_start;
1716 if (size > async->prealloc_bufsz) {
1720 if (size & (~PAGE_MASK)) {
1725 n_pages = size >> PAGE_SHIFT;
1726 for (i = 0; i < n_pages; ++i) {
1727 if (remap_pfn_range(vma, start,
1728 page_to_pfn(virt_to_page
1729 (async->buf_page_list
1730 [i].virt_addr)), PAGE_SIZE,
1738 vma->vm_ops = &comedi_vm_ops;
1739 vma->vm_private_data = async;
1741 async->mmap_count++;
1745 mutex_unlock(&dev->mutex);
1749 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1751 unsigned int mask = 0;
1752 const unsigned minor = iminor(file->f_dentry->d_inode);
1753 struct comedi_subdevice *read_subdev;
1754 struct comedi_subdevice *write_subdev;
1755 struct comedi_device_file_info *dev_file_info;
1756 struct comedi_device *dev;
1757 dev_file_info = comedi_get_device_file_info(minor);
1759 if (dev_file_info == NULL)
1761 dev = dev_file_info->device;
1765 mutex_lock(&dev->mutex);
1766 if (!dev->attached) {
1767 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1768 mutex_unlock(&dev->mutex);
1773 read_subdev = comedi_get_read_subdevice(dev_file_info);
1775 poll_wait(file, &read_subdev->async->wait_head, wait);
1776 if (!read_subdev->busy
1777 || comedi_buf_read_n_available(read_subdev->async) > 0
1778 || !(comedi_get_subdevice_runflags(read_subdev) &
1780 mask |= POLLIN | POLLRDNORM;
1783 write_subdev = comedi_get_write_subdevice(dev_file_info);
1785 poll_wait(file, &write_subdev->async->wait_head, wait);
1786 comedi_buf_write_alloc(write_subdev->async,
1787 write_subdev->async->prealloc_bufsz);
1788 if (!write_subdev->busy
1789 || !(comedi_get_subdevice_runflags(write_subdev) &
1791 || comedi_buf_write_n_allocated(write_subdev->async) >=
1792 bytes_per_sample(write_subdev->async->subdevice)) {
1793 mask |= POLLOUT | POLLWRNORM;
1797 mutex_unlock(&dev->mutex);
1801 static ssize_t comedi_write(struct file *file, const char __user *buf,
1802 size_t nbytes, loff_t *offset)
1804 struct comedi_subdevice *s;
1805 struct comedi_async *async;
1806 int n, m, count = 0, retval = 0;
1807 DECLARE_WAITQUEUE(wait, current);
1808 const unsigned minor = iminor(file->f_dentry->d_inode);
1809 struct comedi_device_file_info *dev_file_info;
1810 struct comedi_device *dev;
1811 dev_file_info = comedi_get_device_file_info(minor);
1813 if (dev_file_info == NULL)
1815 dev = dev_file_info->device;
1819 if (!dev->attached) {
1820 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1825 s = comedi_get_write_subdevice(dev_file_info);
1840 if (s->busy != file) {
1844 add_wait_queue(&async->wait_head, &wait);
1845 while (nbytes > 0 && !retval) {
1846 set_current_state(TASK_INTERRUPTIBLE);
1848 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1850 if (comedi_get_subdevice_runflags(s) &
1856 do_become_nonbusy(dev, s);
1864 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1865 m = async->prealloc_bufsz - async->buf_write_ptr;
1866 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1867 if (m > comedi_buf_write_n_allocated(async))
1868 m = comedi_buf_write_n_allocated(async);
1873 if (file->f_flags & O_NONBLOCK) {
1878 if (signal_pending(current)) {
1879 retval = -ERESTARTSYS;
1884 if (s->busy != file) {
1891 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1897 comedi_buf_write_free(async, n);
1903 break; /* makes device work like a pipe */
1905 set_current_state(TASK_RUNNING);
1906 remove_wait_queue(&async->wait_head, &wait);
1909 return count ? count : retval;
1912 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
1915 struct comedi_subdevice *s;
1916 struct comedi_async *async;
1917 int n, m, count = 0, retval = 0;
1918 DECLARE_WAITQUEUE(wait, current);
1919 const unsigned minor = iminor(file->f_dentry->d_inode);
1920 struct comedi_device_file_info *dev_file_info;
1921 struct comedi_device *dev;
1922 dev_file_info = comedi_get_device_file_info(minor);
1924 if (dev_file_info == NULL)
1926 dev = dev_file_info->device;
1930 if (!dev->attached) {
1931 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1936 s = comedi_get_read_subdevice(dev_file_info);
1950 if (s->busy != file) {
1955 add_wait_queue(&async->wait_head, &wait);
1956 while (nbytes > 0 && !retval) {
1957 set_current_state(TASK_INTERRUPTIBLE);
1961 m = comedi_buf_read_n_available(async);
1962 /* printk("%d available\n",m); */
1963 if (async->buf_read_ptr + m > async->prealloc_bufsz)
1964 m = async->prealloc_bufsz - async->buf_read_ptr;
1965 /* printk("%d contiguous\n",m); */
1970 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1971 do_become_nonbusy(dev, s);
1972 if (comedi_get_subdevice_runflags(s) &
1980 if (file->f_flags & O_NONBLOCK) {
1985 if (signal_pending(current)) {
1986 retval = -ERESTARTSYS;
1993 if (s->busy != file) {
1999 m = copy_to_user(buf, async->prealloc_buf +
2000 async->buf_read_ptr, n);
2006 comedi_buf_read_alloc(async, n);
2007 comedi_buf_read_free(async, n);
2013 break; /* makes device work like a pipe */
2015 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
2016 async->buf_read_count - async->buf_write_count == 0) {
2017 do_become_nonbusy(dev, s);
2019 set_current_state(TASK_RUNNING);
2020 remove_wait_queue(&async->wait_head, &wait);
2023 return count ? count : retval;
2027 This function restores a subdevice to an idle state.
2029 static void do_become_nonbusy(struct comedi_device *dev,
2030 struct comedi_subdevice *s)
2032 struct comedi_async *async = s->async;
2034 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
2036 comedi_reset_async_buf(async);
2037 async->inttrig = NULL;
2038 kfree(async->cmd.chanlist);
2039 async->cmd.chanlist = NULL;
2041 dev_err(dev->class_dev,
2042 "BUG: (?) do_become_nonbusy called with async=NULL\n");
2048 static int comedi_open(struct inode *inode, struct file *file)
2050 const unsigned minor = iminor(inode);
2051 struct comedi_device_file_info *dev_file_info =
2052 comedi_get_device_file_info(minor);
2053 struct comedi_device *dev =
2054 dev_file_info ? dev_file_info->device : NULL;
2057 DPRINTK("invalid minor number\n");
2061 /* This is slightly hacky, but we want module autoloading
2063 * case: user opens device, attached -> ok
2064 * case: user opens device, unattached, in_request_module=0 -> autoload
2065 * case: user opens device, unattached, in_request_module=1 -> fail
2066 * case: root opens device, attached -> ok
2067 * case: root opens device, unattached, in_request_module=1 -> ok
2068 * (typically called from modprobe)
2069 * case: root opens device, unattached, in_request_module=0 -> autoload
2071 * The last could be changed to "-> ok", which would deny root
2074 mutex_lock(&dev->mutex);
2077 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2078 DPRINTK("in request module\n");
2079 mutex_unlock(&dev->mutex);
2082 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2085 dev->in_request_module = 1;
2088 mutex_unlock(&dev->mutex);
2089 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2090 mutex_lock(&dev->mutex);
2093 dev->in_request_module = 0;
2095 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2096 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2097 mutex_unlock(&dev->mutex);
2101 __module_get(THIS_MODULE);
2103 if (dev->attached) {
2104 if (!try_module_get(dev->driver->module)) {
2105 module_put(THIS_MODULE);
2106 mutex_unlock(&dev->mutex);
2111 if (dev->attached && dev->use_count == 0 && dev->open) {
2112 int rc = dev->open(dev);
2114 module_put(dev->driver->module);
2115 module_put(THIS_MODULE);
2116 mutex_unlock(&dev->mutex);
2123 mutex_unlock(&dev->mutex);
2128 static int comedi_close(struct inode *inode, struct file *file)
2130 const unsigned minor = iminor(inode);
2131 struct comedi_subdevice *s = NULL;
2133 struct comedi_device_file_info *dev_file_info;
2134 struct comedi_device *dev;
2135 dev_file_info = comedi_get_device_file_info(minor);
2137 if (dev_file_info == NULL)
2139 dev = dev_file_info->device;
2143 mutex_lock(&dev->mutex);
2145 if (dev->subdevices) {
2146 for (i = 0; i < dev->n_subdevices; i++) {
2147 s = &dev->subdevices[i];
2149 if (s->busy == file)
2151 if (s->lock == file)
2155 if (dev->attached && dev->use_count == 1 && dev->close)
2158 module_put(THIS_MODULE);
2160 module_put(dev->driver->module);
2164 mutex_unlock(&dev->mutex);
2166 if (file->f_flags & FASYNC)
2167 comedi_fasync(-1, file, 0);
2172 static int comedi_fasync(int fd, struct file *file, int on)
2174 const unsigned minor = iminor(file->f_dentry->d_inode);
2175 struct comedi_device_file_info *dev_file_info;
2176 struct comedi_device *dev;
2177 dev_file_info = comedi_get_device_file_info(minor);
2179 if (dev_file_info == NULL)
2181 dev = dev_file_info->device;
2185 return fasync_helper(fd, file, on, &dev->async_queue);
2188 static const struct file_operations comedi_fops = {
2189 .owner = THIS_MODULE,
2190 .unlocked_ioctl = comedi_unlocked_ioctl,
2191 .compat_ioctl = comedi_compat_ioctl,
2192 .open = comedi_open,
2193 .release = comedi_close,
2194 .read = comedi_read,
2195 .write = comedi_write,
2196 .mmap = comedi_mmap,
2197 .poll = comedi_poll,
2198 .fasync = comedi_fasync,
2199 .llseek = noop_llseek,
2202 static struct class *comedi_class;
2203 static struct cdev comedi_cdev;
2205 static void comedi_cleanup_legacy_minors(void)
2209 for (i = 0; i < comedi_num_legacy_minors; i++)
2210 comedi_free_board_minor(i);
2213 static int __init comedi_init(void)
2218 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2220 if (comedi_num_legacy_minors < 0 ||
2221 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2222 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2223 COMEDI_NUM_BOARD_MINORS);
2228 * comedi is unusable if both comedi_autoconfig and
2229 * comedi_num_legacy_minors are zero, so we might as well adjust the
2230 * defaults in that case
2232 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2233 comedi_num_legacy_minors = 16;
2235 memset(comedi_file_info_table, 0,
2236 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
2238 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2239 COMEDI_NUM_MINORS, "comedi");
2242 cdev_init(&comedi_cdev, &comedi_fops);
2243 comedi_cdev.owner = THIS_MODULE;
2244 kobject_set_name(&comedi_cdev.kobj, "comedi");
2245 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2246 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2250 comedi_class = class_create(THIS_MODULE, "comedi");
2251 if (IS_ERR(comedi_class)) {
2252 pr_err("comedi: failed to create class\n");
2253 cdev_del(&comedi_cdev);
2254 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2256 return PTR_ERR(comedi_class);
2259 comedi_class->dev_attrs = comedi_dev_attrs;
2261 /* XXX requires /proc interface */
2264 /* create devices files for legacy/manual use */
2265 for (i = 0; i < comedi_num_legacy_minors; i++) {
2267 minor = comedi_alloc_board_minor(NULL);
2269 comedi_cleanup_legacy_minors();
2270 cdev_del(&comedi_cdev);
2271 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2280 static void __exit comedi_cleanup(void)
2284 comedi_cleanup_legacy_minors();
2285 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2286 BUG_ON(comedi_file_info_table[i]);
2288 class_destroy(comedi_class);
2289 cdev_del(&comedi_cdev);
2290 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2292 comedi_proc_cleanup();
2295 module_init(comedi_init);
2296 module_exit(comedi_cleanup);
2298 void comedi_error(const struct comedi_device *dev, const char *s)
2300 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2302 EXPORT_SYMBOL(comedi_error);
2304 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2306 struct comedi_async *async = s->async;
2307 unsigned runflags = 0;
2308 unsigned runflags_mask = 0;
2310 /* DPRINTK("comedi_event 0x%x\n",mask); */
2312 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2316 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2317 COMEDI_CB_OVERFLOW)) {
2318 runflags_mask |= SRF_RUNNING;
2320 /* remember if an error event has occurred, so an error
2321 * can be returned the next time the user does a read() */
2322 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2323 runflags_mask |= SRF_ERROR;
2324 runflags |= SRF_ERROR;
2326 if (runflags_mask) {
2327 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2328 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2331 if (async->cb_mask & s->async->events) {
2332 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2333 wake_up_interruptible(&async->wait_head);
2334 if (s->subdev_flags & SDF_CMD_READ)
2335 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2336 if (s->subdev_flags & SDF_CMD_WRITE)
2337 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2340 async->cb_func(s->async->events, async->cb_arg);
2343 s->async->events = 0;
2345 EXPORT_SYMBOL(comedi_event);
2347 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
2349 unsigned long flags;
2352 spin_lock_irqsave(&s->spin_lock, flags);
2353 runflags = s->runflags;
2354 spin_unlock_irqrestore(&s->spin_lock, flags);
2357 EXPORT_SYMBOL(comedi_get_subdevice_runflags);
2359 static int is_device_busy(struct comedi_device *dev)
2361 struct comedi_subdevice *s;
2367 for (i = 0; i < dev->n_subdevices; i++) {
2368 s = &dev->subdevices[i];
2371 if (s->async && s->async->mmap_count)
2378 static void comedi_device_init(struct comedi_device *dev)
2380 memset(dev, 0, sizeof(struct comedi_device));
2381 spin_lock_init(&dev->spinlock);
2382 mutex_init(&dev->mutex);
2386 static void comedi_device_cleanup(struct comedi_device *dev)
2390 mutex_lock(&dev->mutex);
2391 comedi_device_detach(dev);
2392 mutex_unlock(&dev->mutex);
2393 mutex_destroy(&dev->mutex);
2396 int comedi_alloc_board_minor(struct device *hardware_device)
2398 struct comedi_device_file_info *info;
2399 struct device *csdev;
2402 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2405 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2406 if (info->device == NULL) {
2410 info->hardware_device = hardware_device;
2411 comedi_device_init(info->device);
2412 spin_lock(&comedi_file_info_table_lock);
2413 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2414 if (comedi_file_info_table[i] == NULL) {
2415 comedi_file_info_table[i] = info;
2419 spin_unlock(&comedi_file_info_table_lock);
2420 if (i == COMEDI_NUM_BOARD_MINORS) {
2421 comedi_device_cleanup(info->device);
2422 kfree(info->device);
2424 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2427 info->device->minor = i;
2428 csdev = device_create(comedi_class, hardware_device,
2429 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2431 info->device->class_dev = csdev;
2432 dev_set_drvdata(csdev, info);
2437 void comedi_free_board_minor(unsigned minor)
2439 struct comedi_device_file_info *info;
2441 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2442 spin_lock(&comedi_file_info_table_lock);
2443 info = comedi_file_info_table[minor];
2444 comedi_file_info_table[minor] = NULL;
2445 spin_unlock(&comedi_file_info_table_lock);
2448 struct comedi_device *dev = info->device;
2450 if (dev->class_dev) {
2451 device_destroy(comedi_class,
2452 MKDEV(COMEDI_MAJOR, dev->minor));
2454 comedi_device_cleanup(dev);
2461 int comedi_find_board_minor(struct device *hardware_device)
2464 struct comedi_device_file_info *info;
2466 for (minor = 0; minor < COMEDI_NUM_BOARD_MINORS; minor++) {
2467 spin_lock(&comedi_file_info_table_lock);
2468 info = comedi_file_info_table[minor];
2469 if (info && info->hardware_device == hardware_device) {
2470 spin_unlock(&comedi_file_info_table_lock);
2473 spin_unlock(&comedi_file_info_table_lock);
2478 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2479 struct comedi_subdevice *s)
2481 struct comedi_device_file_info *info;
2482 struct device *csdev;
2485 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2489 info->read_subdevice = s;
2490 info->write_subdevice = s;
2491 spin_lock(&comedi_file_info_table_lock);
2492 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2493 if (comedi_file_info_table[i] == NULL) {
2494 comedi_file_info_table[i] = info;
2498 spin_unlock(&comedi_file_info_table_lock);
2499 if (i == COMEDI_NUM_MINORS) {
2501 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2505 csdev = device_create(comedi_class, dev->class_dev,
2506 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2507 dev->minor, (int)(s - dev->subdevices));
2509 s->class_dev = csdev;
2510 dev_set_drvdata(csdev, info);
2515 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2517 struct comedi_device_file_info *info;
2524 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2525 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2527 spin_lock(&comedi_file_info_table_lock);
2528 info = comedi_file_info_table[s->minor];
2529 comedi_file_info_table[s->minor] = NULL;
2530 spin_unlock(&comedi_file_info_table_lock);
2533 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2534 s->class_dev = NULL;
2539 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2541 struct comedi_device_file_info *info;
2543 BUG_ON(minor >= COMEDI_NUM_MINORS);
2544 spin_lock(&comedi_file_info_table_lock);
2545 info = comedi_file_info_table[minor];
2546 spin_unlock(&comedi_file_info_table_lock);
2549 EXPORT_SYMBOL_GPL(comedi_get_device_file_info);