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_compat32.h"
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/fcntl.h>
34 #include <linux/delay.h>
35 #include <linux/ioport.h>
37 #include <linux/slab.h>
38 #include <linux/kmod.h>
39 #include <linux/poll.h>
40 #include <linux/init.h>
41 #include <linux/device.h>
42 #include <linux/vmalloc.h>
44 #include "comedidev.h"
45 #include <linux/cdev.h>
46 #include <linux/stat.h>
49 #include <linux/uaccess.h>
51 #include "comedi_internal.h"
53 MODULE_AUTHOR("http://www.comedi.org");
54 MODULE_DESCRIPTION("Comedi core module");
55 MODULE_LICENSE("GPL");
57 #ifdef CONFIG_COMEDI_DEBUG
59 EXPORT_SYMBOL(comedi_debug);
60 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
61 MODULE_PARM_DESC(comedi_debug,
62 "enable comedi core and driver debugging if non-zero (default 0)"
66 bool comedi_autoconfig = 1;
67 module_param(comedi_autoconfig, bool, S_IRUGO);
68 MODULE_PARM_DESC(comedi_autoconfig,
69 "enable drivers to auto-configure comedi devices (default 1)");
71 static int comedi_num_legacy_minors;
72 module_param(comedi_num_legacy_minors, int, S_IRUGO);
73 MODULE_PARM_DESC(comedi_num_legacy_minors,
74 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
77 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
78 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
79 MODULE_PARM_DESC(comedi_default_buf_size_kb,
80 "default asynchronous buffer size in KiB (default "
81 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
83 unsigned int comedi_default_buf_maxsize_kb
84 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
85 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
86 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
87 "default maximum size of asynchronous buffer in KiB (default "
88 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
90 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
91 static struct comedi_device_file_info
92 *comedi_file_info_table[COMEDI_NUM_MINORS];
94 static void do_become_nonbusy(struct comedi_device *dev,
95 struct comedi_subdevice *s);
96 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
98 static int comedi_fasync(int fd, struct file *file, int on);
100 static int is_device_busy(struct comedi_device *dev);
102 static int resize_async_buffer(struct comedi_device *dev,
103 struct comedi_subdevice *s,
104 struct comedi_async *async, unsigned new_size)
108 if (new_size > async->max_bufsize)
112 DPRINTK("subdevice is busy, cannot resize buffer\n");
115 if (async->mmap_count) {
116 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
120 if (!async->prealloc_buf)
123 /* make sure buffer is an integral number of pages
125 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
127 retval = comedi_buf_alloc(dev, s, new_size);
132 retval = s->buf_change(dev, s, new_size);
137 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
138 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
142 /* sysfs attribute files */
144 static ssize_t show_max_read_buffer_kb(struct device *dev,
145 struct device_attribute *attr, char *buf)
147 struct comedi_device_file_info *info = dev_get_drvdata(dev);
148 struct comedi_subdevice *s = comedi_get_read_subdevice(info);
149 unsigned int size = 0;
151 mutex_lock(&info->device->mutex);
152 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
153 size = s->async->max_bufsize / 1024;
154 mutex_unlock(&info->device->mutex);
156 return snprintf(buf, PAGE_SIZE, "%i\n", size);
159 static ssize_t store_max_read_buffer_kb(struct device *dev,
160 struct device_attribute *attr,
161 const char *buf, size_t count)
163 struct comedi_device_file_info *info = dev_get_drvdata(dev);
164 struct comedi_subdevice *s = comedi_get_read_subdevice(info);
168 err = kstrtouint(buf, 10, &size);
171 if (size > (UINT_MAX / 1024))
175 mutex_lock(&info->device->mutex);
176 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
177 s->async->max_bufsize = size;
180 mutex_unlock(&info->device->mutex);
182 return err ? err : count;
185 static ssize_t show_read_buffer_kb(struct device *dev,
186 struct device_attribute *attr, char *buf)
188 struct comedi_device_file_info *info = dev_get_drvdata(dev);
189 struct comedi_subdevice *s = comedi_get_read_subdevice(info);
190 unsigned int size = 0;
192 mutex_lock(&info->device->mutex);
193 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
194 size = s->async->prealloc_bufsz / 1024;
195 mutex_unlock(&info->device->mutex);
197 return snprintf(buf, PAGE_SIZE, "%i\n", size);
200 static ssize_t store_read_buffer_kb(struct device *dev,
201 struct device_attribute *attr,
202 const char *buf, size_t count)
204 struct comedi_device_file_info *info = dev_get_drvdata(dev);
205 struct comedi_subdevice *s = comedi_get_read_subdevice(info);
209 err = kstrtouint(buf, 10, &size);
212 if (size > (UINT_MAX / 1024))
216 mutex_lock(&info->device->mutex);
217 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
218 err = resize_async_buffer(info->device, s, s->async, size);
221 mutex_unlock(&info->device->mutex);
223 return err ? err : count;
226 static ssize_t show_max_write_buffer_kb(struct device *dev,
227 struct device_attribute *attr,
230 struct comedi_device_file_info *info = dev_get_drvdata(dev);
231 struct comedi_subdevice *s = comedi_get_write_subdevice(info);
232 unsigned int size = 0;
234 mutex_lock(&info->device->mutex);
235 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
236 size = s->async->max_bufsize / 1024;
237 mutex_unlock(&info->device->mutex);
239 return snprintf(buf, PAGE_SIZE, "%i\n", size);
242 static ssize_t store_max_write_buffer_kb(struct device *dev,
243 struct device_attribute *attr,
244 const char *buf, size_t count)
246 struct comedi_device_file_info *info = dev_get_drvdata(dev);
247 struct comedi_subdevice *s = comedi_get_write_subdevice(info);
251 err = kstrtouint(buf, 10, &size);
254 if (size > (UINT_MAX / 1024))
258 mutex_lock(&info->device->mutex);
259 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
260 s->async->max_bufsize = size;
263 mutex_unlock(&info->device->mutex);
265 return err ? err : count;
268 static ssize_t show_write_buffer_kb(struct device *dev,
269 struct device_attribute *attr, char *buf)
271 struct comedi_device_file_info *info = dev_get_drvdata(dev);
272 struct comedi_subdevice *s = comedi_get_write_subdevice(info);
273 unsigned int size = 0;
275 mutex_lock(&info->device->mutex);
276 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
277 size = s->async->prealloc_bufsz / 1024;
278 mutex_unlock(&info->device->mutex);
280 return snprintf(buf, PAGE_SIZE, "%i\n", size);
283 static ssize_t store_write_buffer_kb(struct device *dev,
284 struct device_attribute *attr,
285 const char *buf, size_t count)
287 struct comedi_device_file_info *info = dev_get_drvdata(dev);
288 struct comedi_subdevice *s = comedi_get_write_subdevice(info);
292 err = kstrtouint(buf, 10, &size);
295 if (size > (UINT_MAX / 1024))
299 mutex_lock(&info->device->mutex);
300 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
301 err = resize_async_buffer(info->device, s, s->async, size);
304 mutex_unlock(&info->device->mutex);
306 return err ? err : count;
309 static struct device_attribute comedi_dev_attrs[] = {
310 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
311 show_max_read_buffer_kb, store_max_read_buffer_kb),
312 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
313 show_read_buffer_kb, store_read_buffer_kb),
314 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
315 show_max_write_buffer_kb, store_max_write_buffer_kb),
316 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
317 show_write_buffer_kb, store_write_buffer_kb),
326 pointer to devconfig structure
329 devconfig structure at arg
334 static int do_devconfig_ioctl(struct comedi_device *dev,
335 struct comedi_devconfig __user *arg)
337 struct comedi_devconfig it;
339 unsigned char *aux_data = NULL;
342 if (!capable(CAP_SYS_ADMIN))
346 if (is_device_busy(dev))
349 struct module *driver_module = dev->driver->module;
350 comedi_device_detach(dev);
351 module_put(driver_module);
356 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
359 it.board_name[COMEDI_NAMELEN - 1] = 0;
361 if (comedi_aux_data(it.options, 0) &&
362 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
364 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
368 aux_data = vmalloc(aux_len);
372 if (copy_from_user(aux_data,
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 /* by default we allow the insn since we don't have checks for
883 * all possible cases yet */
886 "comedi: no check for data length of config insn id "
887 "%i is implemented.\n"
888 " Add a check to %s in %s.\n"
889 " Assuming n=%i is correct.\n", data[0], __func__,
897 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
898 unsigned int *data, void *file)
900 struct comedi_subdevice *s;
904 if (insn->insn & INSN_MASK_SPECIAL) {
905 /* a non-subdevice instruction */
907 switch (insn->insn) {
917 do_gettimeofday(&tv);
919 data[1] = tv.tv_usec;
925 if (insn->n != 1 || data[0] >= 100000) {
929 udelay(data[0] / 1000);
937 if (insn->subdev >= dev->n_subdevices) {
938 DPRINTK("%d not usable subdevice\n",
943 s = dev->subdevices + insn->subdev;
945 DPRINTK("no async\n");
949 if (!s->async->inttrig) {
950 DPRINTK("no inttrig\n");
954 ret = s->async->inttrig(dev, s, insn->data[0]);
959 DPRINTK("invalid insn\n");
964 /* a subdevice instruction */
965 unsigned int maxdata;
967 if (insn->subdev >= dev->n_subdevices) {
968 DPRINTK("subdevice %d out of range\n", insn->subdev);
972 s = dev->subdevices + insn->subdev;
974 if (s->type == COMEDI_SUBD_UNUSED) {
975 DPRINTK("%d not usable subdevice\n", insn->subdev);
980 /* are we locked? (ioctl lock) */
981 if (s->lock && s->lock != file) {
982 DPRINTK("device locked\n");
987 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
990 DPRINTK("bad chanspec\n");
998 /* This looks arbitrary. It is. */
999 s->busy = &parse_insn;
1000 switch (insn->insn) {
1002 ret = s->insn_read(dev, s, insn, data);
1005 maxdata = s->maxdata_list
1006 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1008 for (i = 0; i < insn->n; ++i) {
1009 if (data[i] > maxdata) {
1011 DPRINTK("bad data value(s)\n");
1016 ret = s->insn_write(dev, s, insn, data);
1022 /* Most drivers ignore the base channel in
1023 * insn->chanspec. Fix this here if
1024 * the subdevice has <= 32 channels. */
1026 unsigned int orig_mask;
1028 orig_mask = data[0];
1029 if (s->n_chan <= 32) {
1030 shift = CR_CHAN(insn->chanspec);
1038 ret = s->insn_bits(dev, s, insn, data);
1039 data[0] = orig_mask;
1045 ret = check_insn_config_length(insn, data);
1048 ret = s->insn_config(dev, s, insn, data);
1064 * synchronous instructions
1070 * struct comedi_insn struct at arg
1076 static int do_insn_ioctl(struct comedi_device *dev,
1077 struct comedi_insn __user *arg, void *file)
1079 struct comedi_insn insn;
1080 unsigned int *data = NULL;
1083 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1089 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
1094 /* This is where the behavior of insn and insnlist deviate. */
1095 if (insn.n > MAX_SAMPLES)
1096 insn.n = MAX_SAMPLES;
1097 if (insn.insn & INSN_MASK_WRITE) {
1098 if (copy_from_user(data,
1100 insn.n * sizeof(unsigned int))) {
1105 ret = parse_insn(dev, &insn, data, file);
1108 if (insn.insn & INSN_MASK_READ) {
1109 if (copy_to_user(insn.data,
1111 insn.n * sizeof(unsigned int))) {
1124 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
1125 unsigned mask, unsigned bits)
1127 unsigned long flags;
1129 spin_lock_irqsave(&s->spin_lock, flags);
1130 s->runflags &= ~mask;
1131 s->runflags |= (bits & mask);
1132 spin_unlock_irqrestore(&s->spin_lock, flags);
1135 static int do_cmd_ioctl(struct comedi_device *dev,
1136 struct comedi_cmd __user *cmd, void *file)
1138 struct comedi_cmd user_cmd;
1139 struct comedi_subdevice *s;
1140 struct comedi_async *async;
1142 unsigned int __user *chanlist_saver = NULL;
1144 if (copy_from_user(&user_cmd, cmd, sizeof(struct comedi_cmd))) {
1145 DPRINTK("bad cmd address\n");
1148 /* save user's chanlist pointer so it can be restored later */
1149 chanlist_saver = user_cmd.chanlist;
1151 if (user_cmd.subdev >= dev->n_subdevices) {
1152 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1156 s = dev->subdevices + user_cmd.subdev;
1159 if (s->type == COMEDI_SUBD_UNUSED) {
1160 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1164 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1165 DPRINTK("subdevice %i does not support commands\n",
1170 /* are we locked? (ioctl lock) */
1171 if (s->lock && s->lock != file) {
1172 DPRINTK("subdevice locked\n");
1178 DPRINTK("subdevice busy\n");
1183 /* make sure channel/gain list isn't too long */
1184 if (user_cmd.chanlist_len > s->len_chanlist) {
1185 DPRINTK("channel/gain list too long %u > %d\n",
1186 user_cmd.chanlist_len, s->len_chanlist);
1191 /* make sure channel/gain list isn't too short */
1192 if (user_cmd.chanlist_len < 1) {
1193 DPRINTK("channel/gain list too short %u < 1\n",
1194 user_cmd.chanlist_len);
1199 kfree(async->cmd.chanlist);
1200 async->cmd = user_cmd;
1201 async->cmd.data = NULL;
1202 /* load channel/gain list */
1203 async->cmd.chanlist =
1204 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1205 if (!async->cmd.chanlist) {
1206 DPRINTK("allocation failed\n");
1211 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1212 async->cmd.chanlist_len * sizeof(int))) {
1213 DPRINTK("fault reading chanlist\n");
1218 /* make sure each element in channel/gain list is valid */
1219 ret = comedi_check_chanlist(s,
1220 async->cmd.chanlist_len,
1221 async->cmd.chanlist);
1223 DPRINTK("bad chanlist\n");
1227 ret = s->do_cmdtest(dev, s, &async->cmd);
1229 if (async->cmd.flags & TRIG_BOGUS || ret) {
1230 DPRINTK("test returned %d\n", ret);
1231 user_cmd = async->cmd;
1232 /* restore chanlist pointer before copying back */
1233 user_cmd.chanlist = chanlist_saver;
1234 user_cmd.data = NULL;
1235 if (copy_to_user(cmd, &user_cmd, sizeof(struct comedi_cmd))) {
1236 DPRINTK("fault writing cmd\n");
1244 if (!async->prealloc_bufsz) {
1246 DPRINTK("no buffer (?)\n");
1250 comedi_reset_async_buf(async);
1253 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1255 if (async->cmd.flags & TRIG_WAKE_EOS)
1256 async->cb_mask |= COMEDI_CB_EOS;
1258 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1260 ret = s->do_cmd(dev, s);
1265 do_become_nonbusy(dev, s);
1272 command testing ioctl
1275 pointer to cmd structure
1278 cmd structure at arg
1282 modified cmd structure at arg
1285 static int do_cmdtest_ioctl(struct comedi_device *dev,
1286 struct comedi_cmd __user *arg, void *file)
1288 struct comedi_cmd user_cmd;
1289 struct comedi_subdevice *s;
1291 unsigned int *chanlist = NULL;
1292 unsigned int __user *chanlist_saver = NULL;
1294 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
1295 DPRINTK("bad cmd address\n");
1298 /* save user's chanlist pointer so it can be restored later */
1299 chanlist_saver = user_cmd.chanlist;
1301 if (user_cmd.subdev >= dev->n_subdevices) {
1302 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1306 s = dev->subdevices + user_cmd.subdev;
1307 if (s->type == COMEDI_SUBD_UNUSED) {
1308 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1312 if (!s->do_cmd || !s->do_cmdtest) {
1313 DPRINTK("subdevice %i does not support commands\n",
1318 /* make sure channel/gain list isn't too long */
1319 if (user_cmd.chanlist_len > s->len_chanlist) {
1320 DPRINTK("channel/gain list too long %d > %d\n",
1321 user_cmd.chanlist_len, s->len_chanlist);
1326 /* load channel/gain list */
1327 if (user_cmd.chanlist) {
1329 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1331 DPRINTK("allocation failed\n");
1336 if (copy_from_user(chanlist, user_cmd.chanlist,
1337 user_cmd.chanlist_len * sizeof(int))) {
1338 DPRINTK("fault reading chanlist\n");
1343 /* make sure each element in channel/gain list is valid */
1344 ret = comedi_check_chanlist(s, user_cmd.chanlist_len, chanlist);
1346 DPRINTK("bad chanlist\n");
1350 user_cmd.chanlist = chanlist;
1353 ret = s->do_cmdtest(dev, s, &user_cmd);
1355 /* restore chanlist pointer before copying back */
1356 user_cmd.chanlist = chanlist_saver;
1358 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
1359 DPRINTK("bad cmd address\n");
1384 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1388 unsigned long flags;
1389 struct comedi_subdevice *s;
1391 if (arg >= dev->n_subdevices)
1393 s = dev->subdevices + arg;
1395 spin_lock_irqsave(&s->spin_lock, flags);
1396 if (s->busy || s->lock)
1400 spin_unlock_irqrestore(&s->spin_lock, flags);
1407 ret = s->lock_f(dev, s);
1426 This function isn't protected by the semaphore, since
1427 we already own the lock.
1429 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1432 struct comedi_subdevice *s;
1434 if (arg >= dev->n_subdevices)
1436 s = dev->subdevices + arg;
1441 if (s->lock && s->lock != file)
1444 if (s->lock == file) {
1458 cancel acquisition ioctl
1470 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1473 struct comedi_subdevice *s;
1475 if (arg >= dev->n_subdevices)
1477 s = dev->subdevices + arg;
1478 if (s->async == NULL)
1481 if (s->lock && s->lock != file)
1487 if (s->busy != file)
1490 return do_cancel(dev, s);
1495 instructs driver to synchronize buffers
1507 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1510 struct comedi_subdevice *s;
1512 if (arg >= dev->n_subdevices)
1514 s = dev->subdevices + arg;
1516 if (s->lock && s->lock != file)
1522 if (s->busy != file)
1526 return s->poll(dev, s);
1531 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1534 const unsigned minor = iminor(file->f_dentry->d_inode);
1535 struct comedi_device_file_info *dev_file_info =
1536 comedi_get_device_file_info(minor);
1537 struct comedi_device *dev;
1540 if (dev_file_info == NULL || dev_file_info->device == NULL)
1542 dev = dev_file_info->device;
1544 mutex_lock(&dev->mutex);
1546 /* Device config is special, because it must work on
1547 * an unconfigured device. */
1548 if (cmd == COMEDI_DEVCONFIG) {
1549 rc = do_devconfig_ioctl(dev,
1550 (struct comedi_devconfig __user *)arg);
1554 if (!dev->attached) {
1555 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1561 case COMEDI_BUFCONFIG:
1562 rc = do_bufconfig_ioctl(dev,
1563 (struct comedi_bufconfig __user *)arg);
1565 case COMEDI_DEVINFO:
1566 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1569 case COMEDI_SUBDINFO:
1570 rc = do_subdinfo_ioctl(dev,
1571 (struct comedi_subdinfo __user *)arg,
1574 case COMEDI_CHANINFO:
1575 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1577 case COMEDI_RANGEINFO:
1578 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1580 case COMEDI_BUFINFO:
1581 rc = do_bufinfo_ioctl(dev,
1582 (struct comedi_bufinfo __user *)arg,
1586 rc = do_lock_ioctl(dev, arg, file);
1589 rc = do_unlock_ioctl(dev, arg, file);
1592 rc = do_cancel_ioctl(dev, arg, file);
1595 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1597 case COMEDI_CMDTEST:
1598 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1601 case COMEDI_INSNLIST:
1602 rc = do_insnlist_ioctl(dev,
1603 (struct comedi_insnlist __user *)arg,
1607 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1611 rc = do_poll_ioctl(dev, arg, file);
1619 mutex_unlock(&dev->mutex);
1623 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1627 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1628 ret = s->cancel(dev, s);
1630 do_become_nonbusy(dev, s);
1636 static void comedi_vm_open(struct vm_area_struct *area)
1638 struct comedi_async *async;
1639 struct comedi_device *dev;
1641 async = area->vm_private_data;
1642 dev = async->subdevice->device;
1644 mutex_lock(&dev->mutex);
1645 async->mmap_count++;
1646 mutex_unlock(&dev->mutex);
1649 static void comedi_vm_close(struct vm_area_struct *area)
1651 struct comedi_async *async;
1652 struct comedi_device *dev;
1654 async = area->vm_private_data;
1655 dev = async->subdevice->device;
1657 mutex_lock(&dev->mutex);
1658 async->mmap_count--;
1659 mutex_unlock(&dev->mutex);
1662 static struct vm_operations_struct comedi_vm_ops = {
1663 .open = comedi_vm_open,
1664 .close = comedi_vm_close,
1667 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1669 const unsigned minor = iminor(file->f_dentry->d_inode);
1670 struct comedi_async *async = NULL;
1671 unsigned long start = vma->vm_start;
1676 struct comedi_subdevice *s;
1677 struct comedi_device_file_info *dev_file_info;
1678 struct comedi_device *dev;
1680 dev_file_info = comedi_get_device_file_info(minor);
1681 if (dev_file_info == NULL)
1683 dev = dev_file_info->device;
1687 mutex_lock(&dev->mutex);
1688 if (!dev->attached) {
1689 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1693 if (vma->vm_flags & VM_WRITE)
1694 s = comedi_get_write_subdevice(dev_file_info);
1696 s = comedi_get_read_subdevice(dev_file_info);
1703 if (async == NULL) {
1708 if (vma->vm_pgoff != 0) {
1709 DPRINTK("comedi: mmap() offset must be 0.\n");
1714 size = vma->vm_end - vma->vm_start;
1715 if (size > async->prealloc_bufsz) {
1719 if (size & (~PAGE_MASK)) {
1724 n_pages = size >> PAGE_SHIFT;
1725 for (i = 0; i < n_pages; ++i) {
1726 if (remap_pfn_range(vma, start,
1727 page_to_pfn(virt_to_page
1728 (async->buf_page_list
1729 [i].virt_addr)), PAGE_SIZE,
1737 vma->vm_ops = &comedi_vm_ops;
1738 vma->vm_private_data = async;
1740 async->mmap_count++;
1744 mutex_unlock(&dev->mutex);
1748 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1750 unsigned int mask = 0;
1751 const unsigned minor = iminor(file->f_dentry->d_inode);
1752 struct comedi_subdevice *read_subdev;
1753 struct comedi_subdevice *write_subdev;
1754 struct comedi_device_file_info *dev_file_info;
1755 struct comedi_device *dev;
1756 dev_file_info = comedi_get_device_file_info(minor);
1758 if (dev_file_info == NULL)
1760 dev = dev_file_info->device;
1764 mutex_lock(&dev->mutex);
1765 if (!dev->attached) {
1766 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1767 mutex_unlock(&dev->mutex);
1772 read_subdev = comedi_get_read_subdevice(dev_file_info);
1774 poll_wait(file, &read_subdev->async->wait_head, wait);
1775 if (!read_subdev->busy
1776 || comedi_buf_read_n_available(read_subdev->async) > 0
1777 || !(comedi_get_subdevice_runflags(read_subdev) &
1779 mask |= POLLIN | POLLRDNORM;
1782 write_subdev = comedi_get_write_subdevice(dev_file_info);
1784 poll_wait(file, &write_subdev->async->wait_head, wait);
1785 comedi_buf_write_alloc(write_subdev->async,
1786 write_subdev->async->prealloc_bufsz);
1787 if (!write_subdev->busy
1788 || !(comedi_get_subdevice_runflags(write_subdev) &
1790 || comedi_buf_write_n_allocated(write_subdev->async) >=
1791 bytes_per_sample(write_subdev->async->subdevice)) {
1792 mask |= POLLOUT | POLLWRNORM;
1796 mutex_unlock(&dev->mutex);
1800 static ssize_t comedi_write(struct file *file, const char __user *buf,
1801 size_t nbytes, loff_t *offset)
1803 struct comedi_subdevice *s;
1804 struct comedi_async *async;
1805 int n, m, count = 0, retval = 0;
1806 DECLARE_WAITQUEUE(wait, current);
1807 const unsigned minor = iminor(file->f_dentry->d_inode);
1808 struct comedi_device_file_info *dev_file_info;
1809 struct comedi_device *dev;
1810 dev_file_info = comedi_get_device_file_info(minor);
1812 if (dev_file_info == NULL)
1814 dev = dev_file_info->device;
1818 if (!dev->attached) {
1819 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1824 s = comedi_get_write_subdevice(dev_file_info);
1839 if (s->busy != file) {
1843 add_wait_queue(&async->wait_head, &wait);
1844 while (nbytes > 0 && !retval) {
1845 set_current_state(TASK_INTERRUPTIBLE);
1847 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1849 if (comedi_get_subdevice_runflags(s) &
1855 do_become_nonbusy(dev, s);
1863 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1864 m = async->prealloc_bufsz - async->buf_write_ptr;
1865 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1866 if (m > comedi_buf_write_n_allocated(async))
1867 m = comedi_buf_write_n_allocated(async);
1872 if (file->f_flags & O_NONBLOCK) {
1877 if (signal_pending(current)) {
1878 retval = -ERESTARTSYS;
1883 if (s->busy != file) {
1890 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1896 comedi_buf_write_free(async, n);
1902 break; /* makes device work like a pipe */
1904 set_current_state(TASK_RUNNING);
1905 remove_wait_queue(&async->wait_head, &wait);
1908 return count ? count : retval;
1911 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
1914 struct comedi_subdevice *s;
1915 struct comedi_async *async;
1916 int n, m, count = 0, retval = 0;
1917 DECLARE_WAITQUEUE(wait, current);
1918 const unsigned minor = iminor(file->f_dentry->d_inode);
1919 struct comedi_device_file_info *dev_file_info;
1920 struct comedi_device *dev;
1921 dev_file_info = comedi_get_device_file_info(minor);
1923 if (dev_file_info == NULL)
1925 dev = dev_file_info->device;
1929 if (!dev->attached) {
1930 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1935 s = comedi_get_read_subdevice(dev_file_info);
1949 if (s->busy != file) {
1954 add_wait_queue(&async->wait_head, &wait);
1955 while (nbytes > 0 && !retval) {
1956 set_current_state(TASK_INTERRUPTIBLE);
1960 m = comedi_buf_read_n_available(async);
1961 /* printk("%d available\n",m); */
1962 if (async->buf_read_ptr + m > async->prealloc_bufsz)
1963 m = async->prealloc_bufsz - async->buf_read_ptr;
1964 /* printk("%d contiguous\n",m); */
1969 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1970 do_become_nonbusy(dev, s);
1971 if (comedi_get_subdevice_runflags(s) &
1979 if (file->f_flags & O_NONBLOCK) {
1984 if (signal_pending(current)) {
1985 retval = -ERESTARTSYS;
1992 if (s->busy != file) {
1998 m = copy_to_user(buf, async->prealloc_buf +
1999 async->buf_read_ptr, n);
2005 comedi_buf_read_alloc(async, n);
2006 comedi_buf_read_free(async, n);
2012 break; /* makes device work like a pipe */
2014 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
2015 async->buf_read_count - async->buf_write_count == 0) {
2016 do_become_nonbusy(dev, s);
2018 set_current_state(TASK_RUNNING);
2019 remove_wait_queue(&async->wait_head, &wait);
2022 return count ? count : retval;
2026 This function restores a subdevice to an idle state.
2028 void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
2030 struct comedi_async *async = s->async;
2032 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
2034 comedi_reset_async_buf(async);
2035 async->inttrig = NULL;
2038 "BUG: (?) do_become_nonbusy called with async=0\n");
2044 static int comedi_open(struct inode *inode, struct file *file)
2046 const unsigned minor = iminor(inode);
2047 struct comedi_device_file_info *dev_file_info =
2048 comedi_get_device_file_info(minor);
2049 struct comedi_device *dev =
2050 dev_file_info ? dev_file_info->device : NULL;
2053 DPRINTK("invalid minor number\n");
2057 /* This is slightly hacky, but we want module autoloading
2059 * case: user opens device, attached -> ok
2060 * case: user opens device, unattached, in_request_module=0 -> autoload
2061 * case: user opens device, unattached, in_request_module=1 -> fail
2062 * case: root opens device, attached -> ok
2063 * case: root opens device, unattached, in_request_module=1 -> ok
2064 * (typically called from modprobe)
2065 * case: root opens device, unattached, in_request_module=0 -> autoload
2067 * The last could be changed to "-> ok", which would deny root
2070 mutex_lock(&dev->mutex);
2073 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2074 DPRINTK("in request module\n");
2075 mutex_unlock(&dev->mutex);
2078 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2081 dev->in_request_module = 1;
2084 mutex_unlock(&dev->mutex);
2085 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2086 mutex_lock(&dev->mutex);
2089 dev->in_request_module = 0;
2091 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2092 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2093 mutex_unlock(&dev->mutex);
2097 __module_get(THIS_MODULE);
2099 if (dev->attached) {
2100 if (!try_module_get(dev->driver->module)) {
2101 module_put(THIS_MODULE);
2102 mutex_unlock(&dev->mutex);
2107 if (dev->attached && dev->use_count == 0 && dev->open) {
2108 int rc = dev->open(dev);
2110 module_put(dev->driver->module);
2111 module_put(THIS_MODULE);
2112 mutex_unlock(&dev->mutex);
2119 mutex_unlock(&dev->mutex);
2124 static int comedi_close(struct inode *inode, struct file *file)
2126 const unsigned minor = iminor(inode);
2127 struct comedi_subdevice *s = NULL;
2129 struct comedi_device_file_info *dev_file_info;
2130 struct comedi_device *dev;
2131 dev_file_info = comedi_get_device_file_info(minor);
2133 if (dev_file_info == NULL)
2135 dev = dev_file_info->device;
2139 mutex_lock(&dev->mutex);
2141 if (dev->subdevices) {
2142 for (i = 0; i < dev->n_subdevices; i++) {
2143 s = dev->subdevices + i;
2145 if (s->busy == file)
2147 if (s->lock == file)
2151 if (dev->attached && dev->use_count == 1 && dev->close)
2154 module_put(THIS_MODULE);
2156 module_put(dev->driver->module);
2160 mutex_unlock(&dev->mutex);
2162 if (file->f_flags & FASYNC)
2163 comedi_fasync(-1, file, 0);
2168 static int comedi_fasync(int fd, struct file *file, int on)
2170 const unsigned minor = iminor(file->f_dentry->d_inode);
2171 struct comedi_device_file_info *dev_file_info;
2172 struct comedi_device *dev;
2173 dev_file_info = comedi_get_device_file_info(minor);
2175 if (dev_file_info == NULL)
2177 dev = dev_file_info->device;
2181 return fasync_helper(fd, file, on, &dev->async_queue);
2184 static const struct file_operations comedi_fops = {
2185 .owner = THIS_MODULE,
2186 .unlocked_ioctl = comedi_unlocked_ioctl,
2187 .compat_ioctl = comedi_compat_ioctl,
2188 .open = comedi_open,
2189 .release = comedi_close,
2190 .read = comedi_read,
2191 .write = comedi_write,
2192 .mmap = comedi_mmap,
2193 .poll = comedi_poll,
2194 .fasync = comedi_fasync,
2195 .llseek = noop_llseek,
2198 static struct class *comedi_class;
2199 static struct cdev comedi_cdev;
2201 static void comedi_cleanup_legacy_minors(void)
2205 for (i = 0; i < comedi_num_legacy_minors; i++)
2206 comedi_free_board_minor(i);
2209 static int __init comedi_init(void)
2214 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
2215 " - http://www.comedi.org\n");
2217 if (comedi_num_legacy_minors < 0 ||
2218 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2219 printk(KERN_ERR "comedi: error: invalid value for module "
2220 "parameter \"comedi_num_legacy_minors\". Valid values "
2221 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
2226 * comedi is unusable if both comedi_autoconfig and
2227 * comedi_num_legacy_minors are zero, so we might as well adjust the
2228 * defaults in that case
2230 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2231 comedi_num_legacy_minors = 16;
2233 memset(comedi_file_info_table, 0,
2234 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
2236 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2237 COMEDI_NUM_MINORS, "comedi");
2240 cdev_init(&comedi_cdev, &comedi_fops);
2241 comedi_cdev.owner = THIS_MODULE;
2242 kobject_set_name(&comedi_cdev.kobj, "comedi");
2243 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2244 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2248 comedi_class = class_create(THIS_MODULE, "comedi");
2249 if (IS_ERR(comedi_class)) {
2250 printk(KERN_ERR "comedi: failed to create class");
2251 cdev_del(&comedi_cdev);
2252 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2254 return PTR_ERR(comedi_class);
2257 comedi_class->dev_attrs = comedi_dev_attrs;
2259 /* XXX requires /proc interface */
2262 /* create devices files for legacy/manual use */
2263 for (i = 0; i < comedi_num_legacy_minors; i++) {
2265 minor = comedi_alloc_board_minor(NULL);
2267 comedi_cleanup_legacy_minors();
2268 cdev_del(&comedi_cdev);
2269 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2278 static void __exit comedi_cleanup(void)
2282 comedi_cleanup_legacy_minors();
2283 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2284 BUG_ON(comedi_file_info_table[i]);
2286 class_destroy(comedi_class);
2287 cdev_del(&comedi_cdev);
2288 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2290 comedi_proc_cleanup();
2293 module_init(comedi_init);
2294 module_exit(comedi_cleanup);
2296 void comedi_error(const struct comedi_device *dev, const char *s)
2298 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
2299 dev->driver->driver_name, s);
2301 EXPORT_SYMBOL(comedi_error);
2303 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2305 struct comedi_async *async = s->async;
2306 unsigned runflags = 0;
2307 unsigned runflags_mask = 0;
2309 /* DPRINTK("comedi_event 0x%x\n",mask); */
2311 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2315 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2316 COMEDI_CB_OVERFLOW)) {
2317 runflags_mask |= SRF_RUNNING;
2319 /* remember if an error event has occurred, so an error
2320 * can be returned the next time the user does a read() */
2321 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2322 runflags_mask |= SRF_ERROR;
2323 runflags |= SRF_ERROR;
2325 if (runflags_mask) {
2326 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2327 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2330 if (async->cb_mask & s->async->events) {
2331 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2332 wake_up_interruptible(&async->wait_head);
2333 if (s->subdev_flags & SDF_CMD_READ)
2334 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2335 if (s->subdev_flags & SDF_CMD_WRITE)
2336 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2339 async->cb_func(s->async->events, async->cb_arg);
2342 s->async->events = 0;
2344 EXPORT_SYMBOL(comedi_event);
2346 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
2348 unsigned long flags;
2351 spin_lock_irqsave(&s->spin_lock, flags);
2352 runflags = s->runflags;
2353 spin_unlock_irqrestore(&s->spin_lock, flags);
2356 EXPORT_SYMBOL(comedi_get_subdevice_runflags);
2358 static int is_device_busy(struct comedi_device *dev)
2360 struct comedi_subdevice *s;
2366 for (i = 0; i < dev->n_subdevices; i++) {
2367 s = dev->subdevices + i;
2370 if (s->async && s->async->mmap_count)
2377 static void comedi_device_init(struct comedi_device *dev)
2379 memset(dev, 0, sizeof(struct comedi_device));
2380 spin_lock_init(&dev->spinlock);
2381 mutex_init(&dev->mutex);
2385 static void comedi_device_cleanup(struct comedi_device *dev)
2389 mutex_lock(&dev->mutex);
2390 comedi_device_detach(dev);
2391 mutex_unlock(&dev->mutex);
2392 mutex_destroy(&dev->mutex);
2395 int comedi_alloc_board_minor(struct device *hardware_device)
2397 struct comedi_device_file_info *info;
2398 struct device *csdev;
2401 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2404 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2405 if (info->device == NULL) {
2409 info->hardware_device = hardware_device;
2410 comedi_device_init(info->device);
2411 spin_lock(&comedi_file_info_table_lock);
2412 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2413 if (comedi_file_info_table[i] == NULL) {
2414 comedi_file_info_table[i] = info;
2418 spin_unlock(&comedi_file_info_table_lock);
2419 if (i == COMEDI_NUM_BOARD_MINORS) {
2420 comedi_device_cleanup(info->device);
2421 kfree(info->device);
2425 "ran out of minor numbers for board device files.\n");
2428 info->device->minor = i;
2429 csdev = device_create(comedi_class, hardware_device,
2430 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2432 info->device->class_dev = csdev;
2433 dev_set_drvdata(csdev, info);
2438 void comedi_free_board_minor(unsigned minor)
2440 struct comedi_device_file_info *info;
2442 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2443 spin_lock(&comedi_file_info_table_lock);
2444 info = comedi_file_info_table[minor];
2445 comedi_file_info_table[minor] = NULL;
2446 spin_unlock(&comedi_file_info_table_lock);
2449 struct comedi_device *dev = info->device;
2451 if (dev->class_dev) {
2452 device_destroy(comedi_class,
2453 MKDEV(COMEDI_MAJOR, dev->minor));
2455 comedi_device_cleanup(dev);
2462 int comedi_find_board_minor(struct device *hardware_device)
2465 struct comedi_device_file_info *info;
2467 for (minor = 0; minor < COMEDI_NUM_BOARD_MINORS; minor++) {
2468 spin_lock(&comedi_file_info_table_lock);
2469 info = comedi_file_info_table[minor];
2470 if (info && info->hardware_device == hardware_device) {
2471 spin_unlock(&comedi_file_info_table_lock);
2474 spin_unlock(&comedi_file_info_table_lock);
2479 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2480 struct comedi_subdevice *s)
2482 struct comedi_device_file_info *info;
2483 struct device *csdev;
2486 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2490 info->read_subdevice = s;
2491 info->write_subdevice = s;
2492 spin_lock(&comedi_file_info_table_lock);
2493 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2494 if (comedi_file_info_table[i] == NULL) {
2495 comedi_file_info_table[i] = info;
2499 spin_unlock(&comedi_file_info_table_lock);
2500 if (i == COMEDI_NUM_MINORS) {
2504 "ran out of minor numbers for board device files.\n");
2508 csdev = device_create(comedi_class, dev->class_dev,
2509 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2510 dev->minor, (int)(s - dev->subdevices));
2512 s->class_dev = csdev;
2513 dev_set_drvdata(csdev, info);
2518 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2520 struct comedi_device_file_info *info;
2527 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2528 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2530 spin_lock(&comedi_file_info_table_lock);
2531 info = comedi_file_info_table[s->minor];
2532 comedi_file_info_table[s->minor] = NULL;
2533 spin_unlock(&comedi_file_info_table_lock);
2536 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2537 s->class_dev = NULL;
2542 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2544 struct comedi_device_file_info *info;
2546 BUG_ON(minor >= COMEDI_NUM_MINORS);
2547 spin_lock(&comedi_file_info_table_lock);
2548 info = comedi_file_info_table[minor];
2549 spin_unlock(&comedi_file_info_table_lock);
2552 EXPORT_SYMBOL_GPL(comedi_get_device_file_info);