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 #ifdef CONFIG_COMEDI_DEBUG
54 EXPORT_SYMBOL(comedi_debug);
55 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(comedi_debug,
57 "enable comedi core and driver debugging if non-zero (default 0)"
61 static int comedi_num_legacy_minors;
62 module_param(comedi_num_legacy_minors, int, S_IRUGO);
63 MODULE_PARM_DESC(comedi_num_legacy_minors,
64 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
67 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
68 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
69 MODULE_PARM_DESC(comedi_default_buf_size_kb,
70 "default asynchronous buffer size in KiB (default "
71 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
73 unsigned int comedi_default_buf_maxsize_kb
74 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
75 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
76 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
77 "default maximum size of asynchronous buffer in KiB (default "
78 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
80 struct comedi_file_info {
81 struct comedi_device *device;
82 struct comedi_subdevice *read_subdevice;
83 struct comedi_subdevice *write_subdevice;
84 struct device *hardware_device;
87 static DEFINE_MUTEX(comedi_file_info_table_lock);
88 static struct comedi_file_info *comedi_file_info_table[COMEDI_NUM_MINORS];
90 static struct class *comedi_class;
91 static struct cdev comedi_cdev;
93 static void comedi_device_init(struct comedi_device *dev)
95 spin_lock_init(&dev->spinlock);
96 mutex_init(&dev->mutex);
100 static void comedi_device_cleanup(struct comedi_device *dev)
102 struct module *driver_module = NULL;
106 mutex_lock(&dev->mutex);
108 driver_module = dev->driver->module;
109 comedi_device_detach(dev);
110 while (dev->use_count > 0) {
112 module_put(driver_module);
113 module_put(THIS_MODULE);
116 mutex_unlock(&dev->mutex);
117 mutex_destroy(&dev->mutex);
120 static struct comedi_file_info *comedi_clear_minor(unsigned minor)
122 struct comedi_file_info *info;
124 mutex_lock(&comedi_file_info_table_lock);
125 info = comedi_file_info_table[minor];
126 comedi_file_info_table[minor] = NULL;
127 mutex_unlock(&comedi_file_info_table_lock);
131 static void comedi_free_board_file_info(struct comedi_file_info *info)
134 struct comedi_device *dev = info->device;
136 if (dev->class_dev) {
137 device_destroy(comedi_class,
138 MKDEV(COMEDI_MAJOR, dev->minor));
140 comedi_device_cleanup(dev);
147 static struct comedi_file_info *comedi_file_info_from_minor(unsigned minor)
149 struct comedi_file_info *info;
151 BUG_ON(minor >= COMEDI_NUM_MINORS);
152 mutex_lock(&comedi_file_info_table_lock);
153 info = comedi_file_info_table[minor];
154 mutex_unlock(&comedi_file_info_table_lock);
158 static struct comedi_device *
159 comedi_dev_from_file_info(struct comedi_file_info *info)
161 return info ? info->device : NULL;
164 struct comedi_device *comedi_dev_from_minor(unsigned minor)
166 return comedi_dev_from_file_info(comedi_file_info_from_minor(minor));
168 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
170 static struct comedi_subdevice *
171 comedi_read_subdevice(const struct comedi_file_info *info)
173 if (info->read_subdevice)
174 return info->read_subdevice;
176 return info->device->read_subdev;
180 static struct comedi_subdevice *
181 comedi_write_subdevice(const struct comedi_file_info *info)
183 if (info->write_subdevice)
184 return info->write_subdevice;
186 return info->device->write_subdev;
190 static int resize_async_buffer(struct comedi_device *dev,
191 struct comedi_subdevice *s,
192 struct comedi_async *async, unsigned new_size)
196 if (new_size > async->max_bufsize)
200 DPRINTK("subdevice is busy, cannot resize buffer\n");
203 if (async->mmap_count) {
204 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
208 if (!async->prealloc_buf)
211 /* make sure buffer is an integral number of pages
213 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
215 retval = comedi_buf_alloc(dev, s, new_size);
220 retval = s->buf_change(dev, s, new_size);
225 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
226 dev->minor, s->index, async->prealloc_bufsz);
230 /* sysfs attribute files */
232 static ssize_t show_max_read_buffer_kb(struct device *dev,
233 struct device_attribute *attr, char *buf)
235 struct comedi_file_info *info = dev_get_drvdata(dev);
236 struct comedi_subdevice *s = comedi_read_subdevice(info);
237 unsigned int size = 0;
239 mutex_lock(&info->device->mutex);
240 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
241 size = s->async->max_bufsize / 1024;
242 mutex_unlock(&info->device->mutex);
244 return snprintf(buf, PAGE_SIZE, "%i\n", size);
247 static ssize_t store_max_read_buffer_kb(struct device *dev,
248 struct device_attribute *attr,
249 const char *buf, size_t count)
251 struct comedi_file_info *info = dev_get_drvdata(dev);
252 struct comedi_subdevice *s = comedi_read_subdevice(info);
256 err = kstrtouint(buf, 10, &size);
259 if (size > (UINT_MAX / 1024))
263 mutex_lock(&info->device->mutex);
264 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
265 s->async->max_bufsize = size;
268 mutex_unlock(&info->device->mutex);
270 return err ? err : count;
273 static ssize_t show_read_buffer_kb(struct device *dev,
274 struct device_attribute *attr, char *buf)
276 struct comedi_file_info *info = dev_get_drvdata(dev);
277 struct comedi_subdevice *s = comedi_read_subdevice(info);
278 unsigned int size = 0;
280 mutex_lock(&info->device->mutex);
281 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
282 size = s->async->prealloc_bufsz / 1024;
283 mutex_unlock(&info->device->mutex);
285 return snprintf(buf, PAGE_SIZE, "%i\n", size);
288 static ssize_t store_read_buffer_kb(struct device *dev,
289 struct device_attribute *attr,
290 const char *buf, size_t count)
292 struct comedi_file_info *info = dev_get_drvdata(dev);
293 struct comedi_subdevice *s = comedi_read_subdevice(info);
297 err = kstrtouint(buf, 10, &size);
300 if (size > (UINT_MAX / 1024))
304 mutex_lock(&info->device->mutex);
305 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
306 err = resize_async_buffer(info->device, s, s->async, size);
309 mutex_unlock(&info->device->mutex);
311 return err ? err : count;
314 static ssize_t show_max_write_buffer_kb(struct device *dev,
315 struct device_attribute *attr,
318 struct comedi_file_info *info = dev_get_drvdata(dev);
319 struct comedi_subdevice *s = comedi_write_subdevice(info);
320 unsigned int size = 0;
322 mutex_lock(&info->device->mutex);
323 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
324 size = s->async->max_bufsize / 1024;
325 mutex_unlock(&info->device->mutex);
327 return snprintf(buf, PAGE_SIZE, "%i\n", size);
330 static ssize_t store_max_write_buffer_kb(struct device *dev,
331 struct device_attribute *attr,
332 const char *buf, size_t count)
334 struct comedi_file_info *info = dev_get_drvdata(dev);
335 struct comedi_subdevice *s = comedi_write_subdevice(info);
339 err = kstrtouint(buf, 10, &size);
342 if (size > (UINT_MAX / 1024))
346 mutex_lock(&info->device->mutex);
347 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
348 s->async->max_bufsize = size;
351 mutex_unlock(&info->device->mutex);
353 return err ? err : count;
356 static ssize_t show_write_buffer_kb(struct device *dev,
357 struct device_attribute *attr, char *buf)
359 struct comedi_file_info *info = dev_get_drvdata(dev);
360 struct comedi_subdevice *s = comedi_write_subdevice(info);
361 unsigned int size = 0;
363 mutex_lock(&info->device->mutex);
364 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
365 size = s->async->prealloc_bufsz / 1024;
366 mutex_unlock(&info->device->mutex);
368 return snprintf(buf, PAGE_SIZE, "%i\n", size);
371 static ssize_t store_write_buffer_kb(struct device *dev,
372 struct device_attribute *attr,
373 const char *buf, size_t count)
375 struct comedi_file_info *info = dev_get_drvdata(dev);
376 struct comedi_subdevice *s = comedi_write_subdevice(info);
380 err = kstrtouint(buf, 10, &size);
383 if (size > (UINT_MAX / 1024))
387 mutex_lock(&info->device->mutex);
388 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
389 err = resize_async_buffer(info->device, s, s->async, size);
392 mutex_unlock(&info->device->mutex);
394 return err ? err : count;
397 static struct device_attribute comedi_dev_attrs[] = {
398 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
399 show_max_read_buffer_kb, store_max_read_buffer_kb),
400 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
401 show_read_buffer_kb, store_read_buffer_kb),
402 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
403 show_max_write_buffer_kb, store_max_write_buffer_kb),
404 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
405 show_write_buffer_kb, store_write_buffer_kb),
409 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
410 unsigned mask, unsigned bits)
414 spin_lock_irqsave(&s->spin_lock, flags);
415 s->runflags &= ~mask;
416 s->runflags |= (bits & mask);
417 spin_unlock_irqrestore(&s->spin_lock, flags);
420 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
425 spin_lock_irqsave(&s->spin_lock, flags);
426 runflags = s->runflags;
427 spin_unlock_irqrestore(&s->spin_lock, flags);
431 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
433 unsigned runflags = comedi_get_subdevice_runflags(s);
435 return (runflags & SRF_RUNNING) ? true : false;
437 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
439 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
441 unsigned runflags = comedi_get_subdevice_runflags(s);
443 return (runflags & SRF_ERROR) ? true : false;
446 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
448 unsigned runflags = comedi_get_subdevice_runflags(s);
450 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
454 This function restores a subdevice to an idle state.
456 static void do_become_nonbusy(struct comedi_device *dev,
457 struct comedi_subdevice *s)
459 struct comedi_async *async = s->async;
461 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
463 comedi_buf_reset(async);
464 async->inttrig = NULL;
465 kfree(async->cmd.chanlist);
466 async->cmd.chanlist = NULL;
468 dev_err(dev->class_dev,
469 "BUG: (?) do_become_nonbusy called with async=NULL\n");
475 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
479 if (comedi_is_subdevice_running(s) && s->cancel)
480 ret = s->cancel(dev, s);
482 do_become_nonbusy(dev, s);
487 static int is_device_busy(struct comedi_device *dev)
489 struct comedi_subdevice *s;
495 for (i = 0; i < dev->n_subdevices; i++) {
496 s = &dev->subdevices[i];
499 if (s->async && s->async->mmap_count)
511 pointer to devconfig structure
514 devconfig structure at arg
519 static int do_devconfig_ioctl(struct comedi_device *dev,
520 struct comedi_devconfig __user *arg)
522 struct comedi_devconfig it;
524 if (!capable(CAP_SYS_ADMIN))
528 if (is_device_busy(dev))
531 struct module *driver_module = dev->driver->module;
532 comedi_device_detach(dev);
533 module_put(driver_module);
538 if (copy_from_user(&it, arg, sizeof(it)))
541 it.board_name[COMEDI_NAMELEN - 1] = 0;
543 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
544 dev_warn(dev->class_dev,
545 "comedi_config --init_data is deprecated\n");
549 if (dev->minor >= comedi_num_legacy_minors)
550 /* don't re-use dynamically allocated comedi devices */
553 /* This increments the driver module count on success. */
554 return comedi_device_attach(dev, &it);
559 buffer configuration ioctl
562 pointer to bufconfig structure
568 modified bufconfig at arg
571 static int do_bufconfig_ioctl(struct comedi_device *dev,
572 struct comedi_bufconfig __user *arg)
574 struct comedi_bufconfig bc;
575 struct comedi_async *async;
576 struct comedi_subdevice *s;
579 if (copy_from_user(&bc, arg, sizeof(bc)))
582 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
585 s = &dev->subdevices[bc.subdevice];
589 DPRINTK("subdevice does not have async capability\n");
595 if (bc.maximum_size) {
596 if (!capable(CAP_SYS_ADMIN))
599 async->max_bufsize = bc.maximum_size;
603 retval = resize_async_buffer(dev, s, async, bc.size);
608 bc.size = async->prealloc_bufsz;
609 bc.maximum_size = async->max_bufsize;
612 if (copy_to_user(arg, &bc, sizeof(bc)))
623 pointer to devinfo structure
632 static int do_devinfo_ioctl(struct comedi_device *dev,
633 struct comedi_devinfo __user *arg,
636 const unsigned minor = iminor(file_inode(file));
637 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
638 struct comedi_subdevice *s;
639 struct comedi_devinfo devinfo;
641 memset(&devinfo, 0, sizeof(devinfo));
643 /* fill devinfo structure */
644 devinfo.version_code = COMEDI_VERSION_CODE;
645 devinfo.n_subdevs = dev->n_subdevices;
646 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
647 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
649 s = comedi_read_subdevice(info);
651 devinfo.read_subdevice = s->index;
653 devinfo.read_subdevice = -1;
655 s = comedi_write_subdevice(info);
657 devinfo.write_subdevice = s->index;
659 devinfo.write_subdevice = -1;
661 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
672 pointer to array of subdevice info structures
678 array of subdevice info structures at arg
681 static int do_subdinfo_ioctl(struct comedi_device *dev,
682 struct comedi_subdinfo __user *arg, void *file)
685 struct comedi_subdinfo *tmp, *us;
686 struct comedi_subdevice *s;
688 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
692 /* fill subdinfo structs */
693 for (i = 0; i < dev->n_subdevices; i++) {
694 s = &dev->subdevices[i];
698 us->n_chan = s->n_chan;
699 us->subd_flags = s->subdev_flags;
700 if (comedi_is_subdevice_running(s))
701 us->subd_flags |= SDF_RUNNING;
702 #define TIMER_nanosec 5 /* backwards compatibility */
703 us->timer_type = TIMER_nanosec;
704 us->len_chanlist = s->len_chanlist;
705 us->maxdata = s->maxdata;
706 if (s->range_table) {
708 (i << 24) | (0 << 16) | (s->range_table->length);
710 us->range_type = 0; /* XXX */
712 us->flags = s->flags;
715 us->subd_flags |= SDF_BUSY;
717 us->subd_flags |= SDF_BUSY_OWNER;
719 us->subd_flags |= SDF_LOCKED;
721 us->subd_flags |= SDF_LOCK_OWNER;
722 if (!s->maxdata && s->maxdata_list)
723 us->subd_flags |= SDF_MAXDATA;
725 us->subd_flags |= SDF_FLAGS;
726 if (s->range_table_list)
727 us->subd_flags |= SDF_RANGETYPE;
729 us->subd_flags |= SDF_CMD;
731 if (s->insn_bits != &insn_inval)
732 us->insn_bits_support = COMEDI_SUPPORTED;
734 us->insn_bits_support = COMEDI_UNSUPPORTED;
736 us->settling_time_0 = s->settling_time_0;
739 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
743 return ret ? -EFAULT : 0;
751 pointer to chaninfo structure
754 chaninfo structure at arg
757 arrays at elements of chaninfo structure
760 static int do_chaninfo_ioctl(struct comedi_device *dev,
761 struct comedi_chaninfo __user *arg)
763 struct comedi_subdevice *s;
764 struct comedi_chaninfo it;
766 if (copy_from_user(&it, arg, sizeof(it)))
769 if (it.subdev >= dev->n_subdevices)
771 s = &dev->subdevices[it.subdev];
773 if (it.maxdata_list) {
774 if (s->maxdata || !s->maxdata_list)
776 if (copy_to_user(it.maxdata_list, s->maxdata_list,
777 s->n_chan * sizeof(unsigned int)))
784 if (copy_to_user(it.flaglist, s->flaglist,
785 s->n_chan * sizeof(unsigned int)))
792 if (!s->range_table_list)
794 for (i = 0; i < s->n_chan; i++) {
797 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
798 (s->range_table_list[i]->length);
799 if (put_user(x, it.rangelist + i))
803 if (copy_to_user(it.rangelist, s->range_type_list,
804 s->n_chan * sizeof(unsigned int)))
814 buffer information ioctl
817 pointer to bufinfo structure
823 modified bufinfo at arg
826 static int do_bufinfo_ioctl(struct comedi_device *dev,
827 struct comedi_bufinfo __user *arg, void *file)
829 struct comedi_bufinfo bi;
830 struct comedi_subdevice *s;
831 struct comedi_async *async;
833 if (copy_from_user(&bi, arg, sizeof(bi)))
836 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
839 s = &dev->subdevices[bi.subdevice];
841 if (s->lock && s->lock != file)
847 DPRINTK("subdevice does not have async capability\n");
848 bi.buf_write_ptr = 0;
850 bi.buf_write_count = 0;
851 bi.buf_read_count = 0;
853 bi.bytes_written = 0;
858 bi.bytes_written = 0;
859 goto copyback_position;
864 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
865 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
866 comedi_buf_read_free(async, bi.bytes_read);
868 if (comedi_is_subdevice_idle(s) &&
869 async->buf_write_count == async->buf_read_count) {
870 do_become_nonbusy(dev, s);
874 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
876 comedi_buf_write_alloc(async, bi.bytes_written);
877 comedi_buf_write_free(async, bi.bytes_written);
881 bi.buf_write_count = async->buf_write_count;
882 bi.buf_write_ptr = async->buf_write_ptr;
883 bi.buf_read_count = async->buf_read_count;
884 bi.buf_read_ptr = async->buf_read_ptr;
887 if (copy_to_user(arg, &bi, sizeof(bi)))
893 static int check_insn_config_length(struct comedi_insn *insn,
900 case INSN_CONFIG_DIO_OUTPUT:
901 case INSN_CONFIG_DIO_INPUT:
902 case INSN_CONFIG_DISARM:
903 case INSN_CONFIG_RESET:
907 case INSN_CONFIG_ARM:
908 case INSN_CONFIG_DIO_QUERY:
909 case INSN_CONFIG_BLOCK_SIZE:
910 case INSN_CONFIG_FILTER:
911 case INSN_CONFIG_SERIAL_CLOCK:
912 case INSN_CONFIG_BIDIRECTIONAL_DATA:
913 case INSN_CONFIG_ALT_SOURCE:
914 case INSN_CONFIG_SET_COUNTER_MODE:
915 case INSN_CONFIG_8254_READ_STATUS:
916 case INSN_CONFIG_SET_ROUTING:
917 case INSN_CONFIG_GET_ROUTING:
918 case INSN_CONFIG_GET_PWM_STATUS:
919 case INSN_CONFIG_PWM_SET_PERIOD:
920 case INSN_CONFIG_PWM_GET_PERIOD:
924 case INSN_CONFIG_SET_GATE_SRC:
925 case INSN_CONFIG_GET_GATE_SRC:
926 case INSN_CONFIG_SET_CLOCK_SRC:
927 case INSN_CONFIG_GET_CLOCK_SRC:
928 case INSN_CONFIG_SET_OTHER_SRC:
929 case INSN_CONFIG_GET_COUNTER_STATUS:
930 case INSN_CONFIG_PWM_SET_H_BRIDGE:
931 case INSN_CONFIG_PWM_GET_H_BRIDGE:
932 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
936 case INSN_CONFIG_PWM_OUTPUT:
937 case INSN_CONFIG_ANALOG_TRIG:
941 case INSN_CONFIG_DIGITAL_TRIG:
945 /* by default we allow the insn since we don't have checks for
946 * all possible cases yet */
948 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
950 pr_warn("comedi: Add a check to %s in %s.\n",
952 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
958 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
959 unsigned int *data, void *file)
961 struct comedi_subdevice *s;
965 if (insn->insn & INSN_MASK_SPECIAL) {
966 /* a non-subdevice instruction */
968 switch (insn->insn) {
978 do_gettimeofday(&tv);
980 data[1] = tv.tv_usec;
986 if (insn->n != 1 || data[0] >= 100000) {
990 udelay(data[0] / 1000);
998 if (insn->subdev >= dev->n_subdevices) {
999 DPRINTK("%d not usable subdevice\n",
1004 s = &dev->subdevices[insn->subdev];
1006 DPRINTK("no async\n");
1010 if (!s->async->inttrig) {
1011 DPRINTK("no inttrig\n");
1015 ret = s->async->inttrig(dev, s, data[0]);
1020 DPRINTK("invalid insn\n");
1025 /* a subdevice instruction */
1026 unsigned int maxdata;
1028 if (insn->subdev >= dev->n_subdevices) {
1029 DPRINTK("subdevice %d out of range\n", insn->subdev);
1033 s = &dev->subdevices[insn->subdev];
1035 if (s->type == COMEDI_SUBD_UNUSED) {
1036 DPRINTK("%d not usable subdevice\n", insn->subdev);
1041 /* are we locked? (ioctl lock) */
1042 if (s->lock && s->lock != file) {
1043 DPRINTK("device locked\n");
1048 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1051 DPRINTK("bad chanspec\n");
1059 /* This looks arbitrary. It is. */
1060 s->busy = &parse_insn;
1061 switch (insn->insn) {
1063 ret = s->insn_read(dev, s, insn, data);
1066 maxdata = s->maxdata_list
1067 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1069 for (i = 0; i < insn->n; ++i) {
1070 if (data[i] > maxdata) {
1072 DPRINTK("bad data value(s)\n");
1077 ret = s->insn_write(dev, s, insn, data);
1083 /* Most drivers ignore the base channel in
1084 * insn->chanspec. Fix this here if
1085 * the subdevice has <= 32 channels. */
1087 unsigned int orig_mask;
1089 orig_mask = data[0];
1090 if (s->n_chan <= 32) {
1091 shift = CR_CHAN(insn->chanspec);
1099 ret = s->insn_bits(dev, s, insn, data);
1100 data[0] = orig_mask;
1106 ret = check_insn_config_length(insn, data);
1109 ret = s->insn_config(dev, s, insn, data);
1125 * synchronous instructions
1128 * pointer to sync cmd structure
1131 * sync cmd struct at arg
1138 /* arbitrary limits */
1139 #define MAX_SAMPLES 256
1140 static int do_insnlist_ioctl(struct comedi_device *dev,
1141 struct comedi_insnlist __user *arg, void *file)
1143 struct comedi_insnlist insnlist;
1144 struct comedi_insn *insns = NULL;
1145 unsigned int *data = NULL;
1149 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1152 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1154 DPRINTK("kmalloc failed\n");
1159 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1161 DPRINTK("kmalloc failed\n");
1166 if (copy_from_user(insns, insnlist.insns,
1167 sizeof(*insns) * insnlist.n_insns)) {
1168 DPRINTK("copy_from_user failed\n");
1173 for (i = 0; i < insnlist.n_insns; i++) {
1174 if (insns[i].n > MAX_SAMPLES) {
1175 DPRINTK("number of samples too large\n");
1179 if (insns[i].insn & INSN_MASK_WRITE) {
1180 if (copy_from_user(data, insns[i].data,
1181 insns[i].n * sizeof(unsigned int))) {
1182 DPRINTK("copy_from_user failed\n");
1187 ret = parse_insn(dev, insns + i, data, file);
1190 if (insns[i].insn & INSN_MASK_READ) {
1191 if (copy_to_user(insns[i].data, data,
1192 insns[i].n * sizeof(unsigned int))) {
1193 DPRINTK("copy_to_user failed\n");
1213 * synchronous instructions
1219 * struct comedi_insn struct at arg
1225 static int do_insn_ioctl(struct comedi_device *dev,
1226 struct comedi_insn __user *arg, void *file)
1228 struct comedi_insn insn;
1229 unsigned int *data = NULL;
1232 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1238 if (copy_from_user(&insn, arg, sizeof(insn))) {
1243 /* This is where the behavior of insn and insnlist deviate. */
1244 if (insn.n > MAX_SAMPLES)
1245 insn.n = MAX_SAMPLES;
1246 if (insn.insn & INSN_MASK_WRITE) {
1247 if (copy_from_user(data,
1249 insn.n * sizeof(unsigned int))) {
1254 ret = parse_insn(dev, &insn, data, file);
1257 if (insn.insn & INSN_MASK_READ) {
1258 if (copy_to_user(insn.data,
1260 insn.n * sizeof(unsigned int))) {
1273 static int do_cmd_ioctl(struct comedi_device *dev,
1274 struct comedi_cmd __user *arg, void *file)
1276 struct comedi_cmd cmd;
1277 struct comedi_subdevice *s;
1278 struct comedi_async *async;
1280 unsigned int __user *user_chanlist;
1282 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1283 DPRINTK("bad cmd address\n");
1286 /* save user's chanlist pointer so it can be restored later */
1287 user_chanlist = (unsigned int __user *)cmd.chanlist;
1289 if (cmd.subdev >= dev->n_subdevices) {
1290 DPRINTK("%d no such subdevice\n", cmd.subdev);
1294 s = &dev->subdevices[cmd.subdev];
1297 if (s->type == COMEDI_SUBD_UNUSED) {
1298 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1302 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1303 DPRINTK("subdevice %i does not support commands\n",
1308 /* are we locked? (ioctl lock) */
1309 if (s->lock && s->lock != file) {
1310 DPRINTK("subdevice locked\n");
1316 DPRINTK("subdevice busy\n");
1321 /* make sure channel/gain list isn't too long */
1322 if (cmd.chanlist_len > s->len_chanlist) {
1323 DPRINTK("channel/gain list too long %u > %d\n",
1324 cmd.chanlist_len, s->len_chanlist);
1329 /* make sure channel/gain list isn't too short */
1330 if (cmd.chanlist_len < 1) {
1331 DPRINTK("channel/gain list too short %u < 1\n",
1338 async->cmd.data = NULL;
1339 /* load channel/gain list */
1340 async->cmd.chanlist =
1341 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1342 if (!async->cmd.chanlist) {
1343 DPRINTK("allocation failed\n");
1348 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1349 async->cmd.chanlist_len * sizeof(int))) {
1350 DPRINTK("fault reading chanlist\n");
1355 /* make sure each element in channel/gain list is valid */
1356 ret = comedi_check_chanlist(s,
1357 async->cmd.chanlist_len,
1358 async->cmd.chanlist);
1360 DPRINTK("bad chanlist\n");
1364 ret = s->do_cmdtest(dev, s, &async->cmd);
1366 if (async->cmd.flags & TRIG_BOGUS || ret) {
1367 DPRINTK("test returned %d\n", ret);
1369 /* restore chanlist pointer before copying back */
1370 cmd.chanlist = (unsigned int __force *)user_chanlist;
1372 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1373 DPRINTK("fault writing cmd\n");
1381 if (!async->prealloc_bufsz) {
1383 DPRINTK("no buffer (?)\n");
1387 comedi_buf_reset(async);
1390 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1392 if (async->cmd.flags & TRIG_WAKE_EOS)
1393 async->cb_mask |= COMEDI_CB_EOS;
1395 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1397 ret = s->do_cmd(dev, s);
1402 do_become_nonbusy(dev, s);
1409 command testing ioctl
1412 pointer to cmd structure
1415 cmd structure at arg
1419 modified cmd structure at arg
1422 static int do_cmdtest_ioctl(struct comedi_device *dev,
1423 struct comedi_cmd __user *arg, void *file)
1425 struct comedi_cmd cmd;
1426 struct comedi_subdevice *s;
1428 unsigned int *chanlist = NULL;
1429 unsigned int __user *user_chanlist;
1431 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1432 DPRINTK("bad cmd address\n");
1435 /* save user's chanlist pointer so it can be restored later */
1436 user_chanlist = (unsigned int __user *)cmd.chanlist;
1438 if (cmd.subdev >= dev->n_subdevices) {
1439 DPRINTK("%d no such subdevice\n", cmd.subdev);
1443 s = &dev->subdevices[cmd.subdev];
1444 if (s->type == COMEDI_SUBD_UNUSED) {
1445 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1449 if (!s->do_cmd || !s->do_cmdtest) {
1450 DPRINTK("subdevice %i does not support commands\n",
1455 /* make sure channel/gain list isn't too long */
1456 if (cmd.chanlist_len > s->len_chanlist) {
1457 DPRINTK("channel/gain list too long %d > %d\n",
1458 cmd.chanlist_len, s->len_chanlist);
1463 /* load channel/gain list */
1466 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1468 DPRINTK("allocation failed\n");
1473 if (copy_from_user(chanlist, user_chanlist,
1474 cmd.chanlist_len * sizeof(int))) {
1475 DPRINTK("fault reading chanlist\n");
1480 /* make sure each element in channel/gain list is valid */
1481 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1483 DPRINTK("bad chanlist\n");
1487 cmd.chanlist = chanlist;
1490 ret = s->do_cmdtest(dev, s, &cmd);
1492 /* restore chanlist pointer before copying back */
1493 cmd.chanlist = (unsigned int __force *)user_chanlist;
1495 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1496 DPRINTK("bad cmd address\n");
1521 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1525 unsigned long flags;
1526 struct comedi_subdevice *s;
1528 if (arg >= dev->n_subdevices)
1530 s = &dev->subdevices[arg];
1532 spin_lock_irqsave(&s->spin_lock, flags);
1533 if (s->busy || s->lock)
1537 spin_unlock_irqrestore(&s->spin_lock, flags);
1544 ret = s->lock_f(dev, s);
1563 This function isn't protected by the semaphore, since
1564 we already own the lock.
1566 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1569 struct comedi_subdevice *s;
1571 if (arg >= dev->n_subdevices)
1573 s = &dev->subdevices[arg];
1578 if (s->lock && s->lock != file)
1581 if (s->lock == file) {
1595 cancel acquisition ioctl
1607 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1610 struct comedi_subdevice *s;
1612 if (arg >= dev->n_subdevices)
1614 s = &dev->subdevices[arg];
1615 if (s->async == NULL)
1618 if (s->lock && s->lock != file)
1624 if (s->busy != file)
1627 return do_cancel(dev, s);
1632 instructs driver to synchronize buffers
1644 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1647 struct comedi_subdevice *s;
1649 if (arg >= dev->n_subdevices)
1651 s = &dev->subdevices[arg];
1653 if (s->lock && s->lock != file)
1659 if (s->busy != file)
1663 return s->poll(dev, s);
1668 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1671 const unsigned minor = iminor(file_inode(file));
1672 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1673 struct comedi_device *dev = comedi_dev_from_file_info(info);
1679 mutex_lock(&dev->mutex);
1681 /* Device config is special, because it must work on
1682 * an unconfigured device. */
1683 if (cmd == COMEDI_DEVCONFIG) {
1684 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1685 /* Device config not appropriate on non-board minors. */
1689 rc = do_devconfig_ioctl(dev,
1690 (struct comedi_devconfig __user *)arg);
1693 dev->minor >= comedi_num_legacy_minors) {
1694 /* Successfully unconfigured a dynamically
1695 * allocated device. Try and remove it. */
1696 info = comedi_clear_minor(dev->minor);
1698 mutex_unlock(&dev->mutex);
1699 comedi_free_board_file_info(info);
1707 if (!dev->attached) {
1708 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1714 case COMEDI_BUFCONFIG:
1715 rc = do_bufconfig_ioctl(dev,
1716 (struct comedi_bufconfig __user *)arg);
1718 case COMEDI_DEVINFO:
1719 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1722 case COMEDI_SUBDINFO:
1723 rc = do_subdinfo_ioctl(dev,
1724 (struct comedi_subdinfo __user *)arg,
1727 case COMEDI_CHANINFO:
1728 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1730 case COMEDI_RANGEINFO:
1731 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1733 case COMEDI_BUFINFO:
1734 rc = do_bufinfo_ioctl(dev,
1735 (struct comedi_bufinfo __user *)arg,
1739 rc = do_lock_ioctl(dev, arg, file);
1742 rc = do_unlock_ioctl(dev, arg, file);
1745 rc = do_cancel_ioctl(dev, arg, file);
1748 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1750 case COMEDI_CMDTEST:
1751 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1754 case COMEDI_INSNLIST:
1755 rc = do_insnlist_ioctl(dev,
1756 (struct comedi_insnlist __user *)arg,
1760 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1764 rc = do_poll_ioctl(dev, arg, file);
1772 mutex_unlock(&dev->mutex);
1776 static void comedi_vm_open(struct vm_area_struct *area)
1778 struct comedi_async *async;
1779 struct comedi_device *dev;
1781 async = area->vm_private_data;
1782 dev = async->subdevice->device;
1784 mutex_lock(&dev->mutex);
1785 async->mmap_count++;
1786 mutex_unlock(&dev->mutex);
1789 static void comedi_vm_close(struct vm_area_struct *area)
1791 struct comedi_async *async;
1792 struct comedi_device *dev;
1794 async = area->vm_private_data;
1795 dev = async->subdevice->device;
1797 mutex_lock(&dev->mutex);
1798 async->mmap_count--;
1799 mutex_unlock(&dev->mutex);
1802 static struct vm_operations_struct comedi_vm_ops = {
1803 .open = comedi_vm_open,
1804 .close = comedi_vm_close,
1807 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1809 const unsigned minor = iminor(file_inode(file));
1810 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1811 struct comedi_device *dev = comedi_dev_from_file_info(info);
1812 struct comedi_subdevice *s;
1813 struct comedi_async *async;
1814 unsigned long start = vma->vm_start;
1823 mutex_lock(&dev->mutex);
1825 if (!dev->attached) {
1826 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1831 if (vma->vm_flags & VM_WRITE)
1832 s = comedi_write_subdevice(info);
1834 s = comedi_read_subdevice(info);
1846 if (vma->vm_pgoff != 0) {
1847 DPRINTK("comedi: mmap() offset must be 0.\n");
1852 size = vma->vm_end - vma->vm_start;
1853 if (size > async->prealloc_bufsz) {
1857 if (size & (~PAGE_MASK)) {
1862 n_pages = size >> PAGE_SHIFT;
1863 for (i = 0; i < n_pages; ++i) {
1864 struct comedi_buf_page *buf = &async->buf_page_list[i];
1866 if (remap_pfn_range(vma, start,
1867 page_to_pfn(virt_to_page(buf->virt_addr)),
1868 PAGE_SIZE, PAGE_SHARED)) {
1875 vma->vm_ops = &comedi_vm_ops;
1876 vma->vm_private_data = async;
1878 async->mmap_count++;
1882 mutex_unlock(&dev->mutex);
1886 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1888 unsigned int mask = 0;
1889 const unsigned minor = iminor(file_inode(file));
1890 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1891 struct comedi_device *dev = comedi_dev_from_file_info(info);
1892 struct comedi_subdevice *s;
1897 mutex_lock(&dev->mutex);
1899 if (!dev->attached) {
1900 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1904 s = comedi_read_subdevice(info);
1905 if (s && s->async) {
1906 poll_wait(file, &s->async->wait_head, wait);
1907 if (!s->busy || !comedi_is_subdevice_running(s) ||
1908 comedi_buf_read_n_available(s->async) > 0)
1909 mask |= POLLIN | POLLRDNORM;
1912 s = comedi_write_subdevice(info);
1913 if (s && s->async) {
1914 unsigned int bps = bytes_per_sample(s->async->subdevice);
1916 poll_wait(file, &s->async->wait_head, wait);
1917 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1918 if (!s->busy || !comedi_is_subdevice_running(s) ||
1919 comedi_buf_write_n_allocated(s->async) >= bps)
1920 mask |= POLLOUT | POLLWRNORM;
1924 mutex_unlock(&dev->mutex);
1928 static ssize_t comedi_write(struct file *file, const char __user *buf,
1929 size_t nbytes, loff_t *offset)
1931 struct comedi_subdevice *s;
1932 struct comedi_async *async;
1933 int n, m, count = 0, retval = 0;
1934 DECLARE_WAITQUEUE(wait, current);
1935 const unsigned minor = iminor(file_inode(file));
1936 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1937 struct comedi_device *dev = comedi_dev_from_file_info(info);
1942 if (!dev->attached) {
1943 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1947 s = comedi_write_subdevice(info);
1948 if (!s || !s->async)
1953 if (!s->busy || !nbytes)
1955 if (s->busy != file)
1958 add_wait_queue(&async->wait_head, &wait);
1959 while (nbytes > 0 && !retval) {
1960 set_current_state(TASK_INTERRUPTIBLE);
1962 if (!comedi_is_subdevice_running(s)) {
1964 if (comedi_is_subdevice_in_error(s))
1968 do_become_nonbusy(dev, s);
1976 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1977 m = async->prealloc_bufsz - async->buf_write_ptr;
1978 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1979 if (m > comedi_buf_write_n_allocated(async))
1980 m = comedi_buf_write_n_allocated(async);
1985 if (file->f_flags & O_NONBLOCK) {
1990 if (signal_pending(current)) {
1991 retval = -ERESTARTSYS;
1996 if (s->busy != file) {
2003 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2009 comedi_buf_write_free(async, n);
2015 break; /* makes device work like a pipe */
2017 set_current_state(TASK_RUNNING);
2018 remove_wait_queue(&async->wait_head, &wait);
2020 return count ? count : retval;
2023 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2026 struct comedi_subdevice *s;
2027 struct comedi_async *async;
2028 int n, m, count = 0, retval = 0;
2029 DECLARE_WAITQUEUE(wait, current);
2030 const unsigned minor = iminor(file_inode(file));
2031 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
2032 struct comedi_device *dev = comedi_dev_from_file_info(info);
2037 if (!dev->attached) {
2038 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2042 s = comedi_read_subdevice(info);
2043 if (!s || !s->async)
2047 if (!s->busy || !nbytes)
2049 if (s->busy != file)
2052 add_wait_queue(&async->wait_head, &wait);
2053 while (nbytes > 0 && !retval) {
2054 set_current_state(TASK_INTERRUPTIBLE);
2058 m = comedi_buf_read_n_available(async);
2059 /* printk("%d available\n",m); */
2060 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2061 m = async->prealloc_bufsz - async->buf_read_ptr;
2062 /* printk("%d contiguous\n",m); */
2067 if (!comedi_is_subdevice_running(s)) {
2068 do_become_nonbusy(dev, s);
2069 if (comedi_is_subdevice_in_error(s))
2075 if (file->f_flags & O_NONBLOCK) {
2080 if (signal_pending(current)) {
2081 retval = -ERESTARTSYS;
2088 if (s->busy != file) {
2094 m = copy_to_user(buf, async->prealloc_buf +
2095 async->buf_read_ptr, n);
2101 comedi_buf_read_alloc(async, n);
2102 comedi_buf_read_free(async, n);
2108 break; /* makes device work like a pipe */
2110 if (comedi_is_subdevice_idle(s) &&
2111 async->buf_read_count - async->buf_write_count == 0) {
2112 do_become_nonbusy(dev, s);
2114 set_current_state(TASK_RUNNING);
2115 remove_wait_queue(&async->wait_head, &wait);
2117 return count ? count : retval;
2120 static int comedi_open(struct inode *inode, struct file *file)
2122 const unsigned minor = iminor(inode);
2123 struct comedi_device *dev = comedi_dev_from_minor(minor);
2126 DPRINTK("invalid minor number\n");
2130 /* This is slightly hacky, but we want module autoloading
2132 * case: user opens device, attached -> ok
2133 * case: user opens device, unattached, !in_request_module -> autoload
2134 * case: user opens device, unattached, in_request_module -> fail
2135 * case: root opens device, attached -> ok
2136 * case: root opens device, unattached, in_request_module -> ok
2137 * (typically called from modprobe)
2138 * case: root opens device, unattached, !in_request_module -> autoload
2140 * The last could be changed to "-> ok", which would deny root
2143 mutex_lock(&dev->mutex);
2146 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2147 DPRINTK("in request module\n");
2148 mutex_unlock(&dev->mutex);
2151 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2154 dev->in_request_module = true;
2157 mutex_unlock(&dev->mutex);
2158 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2159 mutex_lock(&dev->mutex);
2162 dev->in_request_module = false;
2164 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2165 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2166 mutex_unlock(&dev->mutex);
2170 __module_get(THIS_MODULE);
2172 if (dev->attached) {
2173 if (!try_module_get(dev->driver->module)) {
2174 module_put(THIS_MODULE);
2175 mutex_unlock(&dev->mutex);
2180 if (dev->attached && dev->use_count == 0 && dev->open) {
2181 int rc = dev->open(dev);
2183 module_put(dev->driver->module);
2184 module_put(THIS_MODULE);
2185 mutex_unlock(&dev->mutex);
2192 mutex_unlock(&dev->mutex);
2197 static int comedi_fasync(int fd, struct file *file, int on)
2199 const unsigned minor = iminor(file_inode(file));
2200 struct comedi_device *dev = comedi_dev_from_minor(minor);
2205 return fasync_helper(fd, file, on, &dev->async_queue);
2208 static int comedi_close(struct inode *inode, struct file *file)
2210 const unsigned minor = iminor(inode);
2211 struct comedi_device *dev = comedi_dev_from_minor(minor);
2212 struct comedi_subdevice *s = NULL;
2218 mutex_lock(&dev->mutex);
2220 if (dev->subdevices) {
2221 for (i = 0; i < dev->n_subdevices; i++) {
2222 s = &dev->subdevices[i];
2224 if (s->busy == file)
2226 if (s->lock == file)
2230 if (dev->attached && dev->use_count == 1 && dev->close)
2233 module_put(THIS_MODULE);
2235 module_put(dev->driver->module);
2239 mutex_unlock(&dev->mutex);
2241 if (file->f_flags & FASYNC)
2242 comedi_fasync(-1, file, 0);
2247 static const struct file_operations comedi_fops = {
2248 .owner = THIS_MODULE,
2249 .unlocked_ioctl = comedi_unlocked_ioctl,
2250 .compat_ioctl = comedi_compat_ioctl,
2251 .open = comedi_open,
2252 .release = comedi_close,
2253 .read = comedi_read,
2254 .write = comedi_write,
2255 .mmap = comedi_mmap,
2256 .poll = comedi_poll,
2257 .fasync = comedi_fasync,
2258 .llseek = noop_llseek,
2261 void comedi_error(const struct comedi_device *dev, const char *s)
2263 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2265 EXPORT_SYMBOL(comedi_error);
2267 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2269 struct comedi_async *async = s->async;
2270 unsigned runflags = 0;
2271 unsigned runflags_mask = 0;
2273 /* DPRINTK("comedi_event 0x%x\n",mask); */
2275 if (!comedi_is_subdevice_running(s))
2279 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2280 COMEDI_CB_OVERFLOW)) {
2281 runflags_mask |= SRF_RUNNING;
2283 /* remember if an error event has occurred, so an error
2284 * can be returned the next time the user does a read() */
2285 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2286 runflags_mask |= SRF_ERROR;
2287 runflags |= SRF_ERROR;
2289 if (runflags_mask) {
2290 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2291 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2294 if (async->cb_mask & s->async->events) {
2295 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2296 wake_up_interruptible(&async->wait_head);
2297 if (s->subdev_flags & SDF_CMD_READ)
2298 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2299 if (s->subdev_flags & SDF_CMD_WRITE)
2300 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2303 async->cb_func(s->async->events, async->cb_arg);
2306 s->async->events = 0;
2308 EXPORT_SYMBOL(comedi_event);
2310 /* Note: the ->mutex is pre-locked on successful return */
2311 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2313 struct comedi_file_info *info;
2314 struct comedi_device *dev;
2315 struct device *csdev;
2318 info = kzalloc(sizeof(*info), GFP_KERNEL);
2320 return ERR_PTR(-ENOMEM);
2321 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2324 return ERR_PTR(-ENOMEM);
2327 info->hardware_device = hardware_device;
2328 comedi_device_init(dev);
2329 comedi_set_hw_dev(dev, hardware_device);
2330 mutex_lock(&dev->mutex);
2331 mutex_lock(&comedi_file_info_table_lock);
2332 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2333 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2334 if (comedi_file_info_table[i] == NULL) {
2335 comedi_file_info_table[i] = info;
2339 mutex_unlock(&comedi_file_info_table_lock);
2340 if (i == COMEDI_NUM_BOARD_MINORS) {
2341 mutex_unlock(&dev->mutex);
2342 comedi_device_cleanup(dev);
2345 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2346 return ERR_PTR(-EBUSY);
2349 csdev = device_create(comedi_class, hardware_device,
2350 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2352 dev->class_dev = csdev;
2353 dev_set_drvdata(csdev, info);
2355 /* Note: dev->mutex needs to be unlocked by the caller. */
2359 static void comedi_free_board_minor(unsigned minor)
2361 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2362 comedi_free_board_file_info(comedi_clear_minor(minor));
2365 void comedi_release_hardware_device(struct device *hardware_device)
2368 struct comedi_file_info *info;
2370 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2372 mutex_lock(&comedi_file_info_table_lock);
2373 info = comedi_file_info_table[minor];
2374 if (info && info->hardware_device == hardware_device) {
2375 comedi_file_info_table[minor] = NULL;
2376 mutex_unlock(&comedi_file_info_table_lock);
2377 comedi_free_board_file_info(info);
2380 mutex_unlock(&comedi_file_info_table_lock);
2384 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2386 struct comedi_device *dev = s->device;
2387 struct comedi_file_info *info;
2388 struct device *csdev;
2391 info = kzalloc(sizeof(*info), GFP_KERNEL);
2395 if (s->subdev_flags & SDF_CMD_READ)
2396 info->read_subdevice = s;
2397 if (s->subdev_flags & SDF_CMD_WRITE)
2398 info->write_subdevice = s;
2399 mutex_lock(&comedi_file_info_table_lock);
2400 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2401 if (comedi_file_info_table[i] == NULL) {
2402 comedi_file_info_table[i] = info;
2406 mutex_unlock(&comedi_file_info_table_lock);
2407 if (i == COMEDI_NUM_MINORS) {
2409 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2413 csdev = device_create(comedi_class, dev->class_dev,
2414 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2415 dev->minor, s->index);
2417 s->class_dev = csdev;
2418 dev_set_drvdata(csdev, info);
2423 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2425 struct comedi_file_info *info;
2432 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2433 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2435 info = comedi_clear_minor(s->minor);
2437 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2438 s->class_dev = NULL;
2443 static void comedi_cleanup_board_minors(void)
2447 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2448 comedi_free_board_minor(i);
2451 static int __init comedi_init(void)
2456 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2458 if (comedi_num_legacy_minors < 0 ||
2459 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2460 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2461 COMEDI_NUM_BOARD_MINORS);
2465 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2466 COMEDI_NUM_MINORS, "comedi");
2469 cdev_init(&comedi_cdev, &comedi_fops);
2470 comedi_cdev.owner = THIS_MODULE;
2471 kobject_set_name(&comedi_cdev.kobj, "comedi");
2472 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2473 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2477 comedi_class = class_create(THIS_MODULE, "comedi");
2478 if (IS_ERR(comedi_class)) {
2479 pr_err("comedi: failed to create class\n");
2480 cdev_del(&comedi_cdev);
2481 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2483 return PTR_ERR(comedi_class);
2486 comedi_class->dev_attrs = comedi_dev_attrs;
2488 /* XXX requires /proc interface */
2491 /* create devices files for legacy/manual use */
2492 for (i = 0; i < comedi_num_legacy_minors; i++) {
2493 struct comedi_device *dev;
2494 dev = comedi_alloc_board_minor(NULL);
2496 comedi_cleanup_board_minors();
2497 cdev_del(&comedi_cdev);
2498 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2500 return PTR_ERR(dev);
2502 /* comedi_alloc_board_minor() locked the mutex */
2503 mutex_unlock(&dev->mutex);
2509 module_init(comedi_init);
2511 static void __exit comedi_cleanup(void)
2515 comedi_cleanup_board_minors();
2516 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2517 BUG_ON(comedi_file_info_table[i]);
2519 class_destroy(comedi_class);
2520 cdev_del(&comedi_cdev);
2521 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2523 comedi_proc_cleanup();
2525 module_exit(comedi_cleanup);
2527 MODULE_AUTHOR("http://www.comedi.org");
2528 MODULE_DESCRIPTION("Comedi core module");
2529 MODULE_LICENSE("GPL");