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 #define COMEDI_NUM_MINORS 0x100
54 #ifdef CONFIG_COMEDI_DEBUG
56 EXPORT_SYMBOL(comedi_debug);
57 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
58 MODULE_PARM_DESC(comedi_debug,
59 "enable comedi core and driver debugging if non-zero (default 0)"
63 static int comedi_num_legacy_minors;
64 module_param(comedi_num_legacy_minors, int, S_IRUGO);
65 MODULE_PARM_DESC(comedi_num_legacy_minors,
66 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
69 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
70 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
71 MODULE_PARM_DESC(comedi_default_buf_size_kb,
72 "default asynchronous buffer size in KiB (default "
73 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
75 unsigned int comedi_default_buf_maxsize_kb
76 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
77 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
78 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
79 "default maximum size of asynchronous buffer in KiB (default "
80 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
82 struct comedi_file_info {
83 struct comedi_device *device;
84 struct comedi_subdevice *read_subdevice;
85 struct comedi_subdevice *write_subdevice;
86 struct device *hardware_device;
89 static DEFINE_MUTEX(comedi_file_info_table_lock);
90 static struct comedi_file_info *comedi_file_info_table[COMEDI_NUM_MINORS];
92 static struct class *comedi_class;
93 static struct cdev comedi_cdev;
95 static void comedi_device_init(struct comedi_device *dev)
97 spin_lock_init(&dev->spinlock);
98 mutex_init(&dev->mutex);
102 static void comedi_device_cleanup(struct comedi_device *dev)
104 struct module *driver_module = NULL;
108 mutex_lock(&dev->mutex);
110 driver_module = dev->driver->module;
111 comedi_device_detach(dev);
112 while (dev->use_count > 0) {
114 module_put(driver_module);
115 module_put(THIS_MODULE);
118 mutex_unlock(&dev->mutex);
119 mutex_destroy(&dev->mutex);
122 static struct comedi_file_info *comedi_clear_minor(unsigned minor)
124 struct comedi_file_info *info;
126 mutex_lock(&comedi_file_info_table_lock);
127 info = comedi_file_info_table[minor];
128 comedi_file_info_table[minor] = NULL;
129 mutex_unlock(&comedi_file_info_table_lock);
133 static void comedi_free_board_file_info(struct comedi_file_info *info)
136 struct comedi_device *dev = info->device;
138 if (dev->class_dev) {
139 device_destroy(comedi_class,
140 MKDEV(COMEDI_MAJOR, dev->minor));
142 comedi_device_cleanup(dev);
149 static struct comedi_file_info *comedi_file_info_from_minor(unsigned minor)
151 struct comedi_file_info *info;
153 BUG_ON(minor >= COMEDI_NUM_MINORS);
154 mutex_lock(&comedi_file_info_table_lock);
155 info = comedi_file_info_table[minor];
156 mutex_unlock(&comedi_file_info_table_lock);
160 static struct comedi_device *
161 comedi_dev_from_file_info(struct comedi_file_info *info)
163 return info ? info->device : NULL;
166 struct comedi_device *comedi_dev_from_minor(unsigned minor)
168 return comedi_dev_from_file_info(comedi_file_info_from_minor(minor));
170 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
172 static struct comedi_subdevice *
173 comedi_read_subdevice(const struct comedi_file_info *info)
175 if (info->read_subdevice)
176 return info->read_subdevice;
178 return info->device->read_subdev;
182 static struct comedi_subdevice *
183 comedi_write_subdevice(const struct comedi_file_info *info)
185 if (info->write_subdevice)
186 return info->write_subdevice;
188 return info->device->write_subdev;
192 static int resize_async_buffer(struct comedi_device *dev,
193 struct comedi_subdevice *s,
194 struct comedi_async *async, unsigned new_size)
198 if (new_size > async->max_bufsize)
202 DPRINTK("subdevice is busy, cannot resize buffer\n");
205 if (async->mmap_count) {
206 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
210 if (!async->prealloc_buf)
213 /* make sure buffer is an integral number of pages
215 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
217 retval = comedi_buf_alloc(dev, s, new_size);
222 retval = s->buf_change(dev, s, new_size);
227 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
228 dev->minor, s->index, async->prealloc_bufsz);
232 /* sysfs attribute files */
234 static ssize_t show_max_read_buffer_kb(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct comedi_file_info *info = dev_get_drvdata(dev);
238 struct comedi_subdevice *s = comedi_read_subdevice(info);
239 unsigned int size = 0;
241 mutex_lock(&info->device->mutex);
242 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
243 size = s->async->max_bufsize / 1024;
244 mutex_unlock(&info->device->mutex);
246 return snprintf(buf, PAGE_SIZE, "%i\n", size);
249 static ssize_t store_max_read_buffer_kb(struct device *dev,
250 struct device_attribute *attr,
251 const char *buf, size_t count)
253 struct comedi_file_info *info = dev_get_drvdata(dev);
254 struct comedi_subdevice *s = comedi_read_subdevice(info);
258 err = kstrtouint(buf, 10, &size);
261 if (size > (UINT_MAX / 1024))
265 mutex_lock(&info->device->mutex);
266 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
267 s->async->max_bufsize = size;
270 mutex_unlock(&info->device->mutex);
272 return err ? err : count;
275 static ssize_t show_read_buffer_kb(struct device *dev,
276 struct device_attribute *attr, char *buf)
278 struct comedi_file_info *info = dev_get_drvdata(dev);
279 struct comedi_subdevice *s = comedi_read_subdevice(info);
280 unsigned int size = 0;
282 mutex_lock(&info->device->mutex);
283 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
284 size = s->async->prealloc_bufsz / 1024;
285 mutex_unlock(&info->device->mutex);
287 return snprintf(buf, PAGE_SIZE, "%i\n", size);
290 static ssize_t store_read_buffer_kb(struct device *dev,
291 struct device_attribute *attr,
292 const char *buf, size_t count)
294 struct comedi_file_info *info = dev_get_drvdata(dev);
295 struct comedi_subdevice *s = comedi_read_subdevice(info);
299 err = kstrtouint(buf, 10, &size);
302 if (size > (UINT_MAX / 1024))
306 mutex_lock(&info->device->mutex);
307 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
308 err = resize_async_buffer(info->device, s, s->async, size);
311 mutex_unlock(&info->device->mutex);
313 return err ? err : count;
316 static ssize_t show_max_write_buffer_kb(struct device *dev,
317 struct device_attribute *attr,
320 struct comedi_file_info *info = dev_get_drvdata(dev);
321 struct comedi_subdevice *s = comedi_write_subdevice(info);
322 unsigned int size = 0;
324 mutex_lock(&info->device->mutex);
325 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
326 size = s->async->max_bufsize / 1024;
327 mutex_unlock(&info->device->mutex);
329 return snprintf(buf, PAGE_SIZE, "%i\n", size);
332 static ssize_t store_max_write_buffer_kb(struct device *dev,
333 struct device_attribute *attr,
334 const char *buf, size_t count)
336 struct comedi_file_info *info = dev_get_drvdata(dev);
337 struct comedi_subdevice *s = comedi_write_subdevice(info);
341 err = kstrtouint(buf, 10, &size);
344 if (size > (UINT_MAX / 1024))
348 mutex_lock(&info->device->mutex);
349 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
350 s->async->max_bufsize = size;
353 mutex_unlock(&info->device->mutex);
355 return err ? err : count;
358 static ssize_t show_write_buffer_kb(struct device *dev,
359 struct device_attribute *attr, char *buf)
361 struct comedi_file_info *info = dev_get_drvdata(dev);
362 struct comedi_subdevice *s = comedi_write_subdevice(info);
363 unsigned int size = 0;
365 mutex_lock(&info->device->mutex);
366 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
367 size = s->async->prealloc_bufsz / 1024;
368 mutex_unlock(&info->device->mutex);
370 return snprintf(buf, PAGE_SIZE, "%i\n", size);
373 static ssize_t store_write_buffer_kb(struct device *dev,
374 struct device_attribute *attr,
375 const char *buf, size_t count)
377 struct comedi_file_info *info = dev_get_drvdata(dev);
378 struct comedi_subdevice *s = comedi_write_subdevice(info);
382 err = kstrtouint(buf, 10, &size);
385 if (size > (UINT_MAX / 1024))
389 mutex_lock(&info->device->mutex);
390 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
391 err = resize_async_buffer(info->device, s, s->async, size);
394 mutex_unlock(&info->device->mutex);
396 return err ? err : count;
399 static struct device_attribute comedi_dev_attrs[] = {
400 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
401 show_max_read_buffer_kb, store_max_read_buffer_kb),
402 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
403 show_read_buffer_kb, store_read_buffer_kb),
404 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
405 show_max_write_buffer_kb, store_max_write_buffer_kb),
406 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
407 show_write_buffer_kb, store_write_buffer_kb),
411 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
412 unsigned mask, unsigned bits)
416 spin_lock_irqsave(&s->spin_lock, flags);
417 s->runflags &= ~mask;
418 s->runflags |= (bits & mask);
419 spin_unlock_irqrestore(&s->spin_lock, flags);
422 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
427 spin_lock_irqsave(&s->spin_lock, flags);
428 runflags = s->runflags;
429 spin_unlock_irqrestore(&s->spin_lock, flags);
433 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
435 unsigned runflags = comedi_get_subdevice_runflags(s);
437 return (runflags & SRF_RUNNING) ? true : false;
439 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
441 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
443 unsigned runflags = comedi_get_subdevice_runflags(s);
445 return (runflags & SRF_ERROR) ? true : false;
448 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
450 unsigned runflags = comedi_get_subdevice_runflags(s);
452 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
456 This function restores a subdevice to an idle state.
458 static void do_become_nonbusy(struct comedi_device *dev,
459 struct comedi_subdevice *s)
461 struct comedi_async *async = s->async;
463 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
465 comedi_buf_reset(async);
466 async->inttrig = NULL;
467 kfree(async->cmd.chanlist);
468 async->cmd.chanlist = NULL;
470 dev_err(dev->class_dev,
471 "BUG: (?) do_become_nonbusy called with async=NULL\n");
477 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
481 if (comedi_is_subdevice_running(s) && s->cancel)
482 ret = s->cancel(dev, s);
484 do_become_nonbusy(dev, s);
489 static int is_device_busy(struct comedi_device *dev)
491 struct comedi_subdevice *s;
497 for (i = 0; i < dev->n_subdevices; i++) {
498 s = &dev->subdevices[i];
501 if (s->async && s->async->mmap_count)
513 pointer to devconfig structure
516 devconfig structure at arg
521 static int do_devconfig_ioctl(struct comedi_device *dev,
522 struct comedi_devconfig __user *arg)
524 struct comedi_devconfig it;
526 if (!capable(CAP_SYS_ADMIN))
530 if (is_device_busy(dev))
533 struct module *driver_module = dev->driver->module;
534 comedi_device_detach(dev);
535 module_put(driver_module);
540 if (copy_from_user(&it, arg, sizeof(it)))
543 it.board_name[COMEDI_NAMELEN - 1] = 0;
545 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
546 dev_warn(dev->class_dev,
547 "comedi_config --init_data is deprecated\n");
551 if (dev->minor >= comedi_num_legacy_minors)
552 /* don't re-use dynamically allocated comedi devices */
555 /* This increments the driver module count on success. */
556 return comedi_device_attach(dev, &it);
561 buffer configuration ioctl
564 pointer to bufconfig structure
570 modified bufconfig at arg
573 static int do_bufconfig_ioctl(struct comedi_device *dev,
574 struct comedi_bufconfig __user *arg)
576 struct comedi_bufconfig bc;
577 struct comedi_async *async;
578 struct comedi_subdevice *s;
581 if (copy_from_user(&bc, arg, sizeof(bc)))
584 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
587 s = &dev->subdevices[bc.subdevice];
591 DPRINTK("subdevice does not have async capability\n");
597 if (bc.maximum_size) {
598 if (!capable(CAP_SYS_ADMIN))
601 async->max_bufsize = bc.maximum_size;
605 retval = resize_async_buffer(dev, s, async, bc.size);
610 bc.size = async->prealloc_bufsz;
611 bc.maximum_size = async->max_bufsize;
614 if (copy_to_user(arg, &bc, sizeof(bc)))
625 pointer to devinfo structure
634 static int do_devinfo_ioctl(struct comedi_device *dev,
635 struct comedi_devinfo __user *arg,
638 const unsigned minor = iminor(file_inode(file));
639 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
640 struct comedi_subdevice *s;
641 struct comedi_devinfo devinfo;
643 memset(&devinfo, 0, sizeof(devinfo));
645 /* fill devinfo structure */
646 devinfo.version_code = COMEDI_VERSION_CODE;
647 devinfo.n_subdevs = dev->n_subdevices;
648 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
649 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
651 s = comedi_read_subdevice(info);
653 devinfo.read_subdevice = s->index;
655 devinfo.read_subdevice = -1;
657 s = comedi_write_subdevice(info);
659 devinfo.write_subdevice = s->index;
661 devinfo.write_subdevice = -1;
663 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
674 pointer to array of subdevice info structures
680 array of subdevice info structures at arg
683 static int do_subdinfo_ioctl(struct comedi_device *dev,
684 struct comedi_subdinfo __user *arg, void *file)
687 struct comedi_subdinfo *tmp, *us;
688 struct comedi_subdevice *s;
690 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
694 /* fill subdinfo structs */
695 for (i = 0; i < dev->n_subdevices; i++) {
696 s = &dev->subdevices[i];
700 us->n_chan = s->n_chan;
701 us->subd_flags = s->subdev_flags;
702 if (comedi_is_subdevice_running(s))
703 us->subd_flags |= SDF_RUNNING;
704 #define TIMER_nanosec 5 /* backwards compatibility */
705 us->timer_type = TIMER_nanosec;
706 us->len_chanlist = s->len_chanlist;
707 us->maxdata = s->maxdata;
708 if (s->range_table) {
710 (i << 24) | (0 << 16) | (s->range_table->length);
712 us->range_type = 0; /* XXX */
714 us->flags = s->flags;
717 us->subd_flags |= SDF_BUSY;
719 us->subd_flags |= SDF_BUSY_OWNER;
721 us->subd_flags |= SDF_LOCKED;
723 us->subd_flags |= SDF_LOCK_OWNER;
724 if (!s->maxdata && s->maxdata_list)
725 us->subd_flags |= SDF_MAXDATA;
727 us->subd_flags |= SDF_FLAGS;
728 if (s->range_table_list)
729 us->subd_flags |= SDF_RANGETYPE;
731 us->subd_flags |= SDF_CMD;
733 if (s->insn_bits != &insn_inval)
734 us->insn_bits_support = COMEDI_SUPPORTED;
736 us->insn_bits_support = COMEDI_UNSUPPORTED;
738 us->settling_time_0 = s->settling_time_0;
741 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
745 return ret ? -EFAULT : 0;
753 pointer to chaninfo structure
756 chaninfo structure at arg
759 arrays at elements of chaninfo structure
762 static int do_chaninfo_ioctl(struct comedi_device *dev,
763 struct comedi_chaninfo __user *arg)
765 struct comedi_subdevice *s;
766 struct comedi_chaninfo it;
768 if (copy_from_user(&it, arg, sizeof(it)))
771 if (it.subdev >= dev->n_subdevices)
773 s = &dev->subdevices[it.subdev];
775 if (it.maxdata_list) {
776 if (s->maxdata || !s->maxdata_list)
778 if (copy_to_user(it.maxdata_list, s->maxdata_list,
779 s->n_chan * sizeof(unsigned int)))
786 if (copy_to_user(it.flaglist, s->flaglist,
787 s->n_chan * sizeof(unsigned int)))
794 if (!s->range_table_list)
796 for (i = 0; i < s->n_chan; i++) {
799 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
800 (s->range_table_list[i]->length);
801 if (put_user(x, it.rangelist + i))
805 if (copy_to_user(it.rangelist, s->range_type_list,
806 s->n_chan * sizeof(unsigned int)))
816 buffer information ioctl
819 pointer to bufinfo structure
825 modified bufinfo at arg
828 static int do_bufinfo_ioctl(struct comedi_device *dev,
829 struct comedi_bufinfo __user *arg, void *file)
831 struct comedi_bufinfo bi;
832 struct comedi_subdevice *s;
833 struct comedi_async *async;
835 if (copy_from_user(&bi, arg, sizeof(bi)))
838 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
841 s = &dev->subdevices[bi.subdevice];
843 if (s->lock && s->lock != file)
849 DPRINTK("subdevice does not have async capability\n");
850 bi.buf_write_ptr = 0;
852 bi.buf_write_count = 0;
853 bi.buf_read_count = 0;
855 bi.bytes_written = 0;
860 bi.bytes_written = 0;
861 goto copyback_position;
866 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
867 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
868 comedi_buf_read_free(async, bi.bytes_read);
870 if (comedi_is_subdevice_idle(s) &&
871 async->buf_write_count == async->buf_read_count) {
872 do_become_nonbusy(dev, s);
876 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
878 comedi_buf_write_alloc(async, bi.bytes_written);
879 comedi_buf_write_free(async, bi.bytes_written);
883 bi.buf_write_count = async->buf_write_count;
884 bi.buf_write_ptr = async->buf_write_ptr;
885 bi.buf_read_count = async->buf_read_count;
886 bi.buf_read_ptr = async->buf_read_ptr;
889 if (copy_to_user(arg, &bi, sizeof(bi)))
895 static int check_insn_config_length(struct comedi_insn *insn,
902 case INSN_CONFIG_DIO_OUTPUT:
903 case INSN_CONFIG_DIO_INPUT:
904 case INSN_CONFIG_DISARM:
905 case INSN_CONFIG_RESET:
909 case INSN_CONFIG_ARM:
910 case INSN_CONFIG_DIO_QUERY:
911 case INSN_CONFIG_BLOCK_SIZE:
912 case INSN_CONFIG_FILTER:
913 case INSN_CONFIG_SERIAL_CLOCK:
914 case INSN_CONFIG_BIDIRECTIONAL_DATA:
915 case INSN_CONFIG_ALT_SOURCE:
916 case INSN_CONFIG_SET_COUNTER_MODE:
917 case INSN_CONFIG_8254_READ_STATUS:
918 case INSN_CONFIG_SET_ROUTING:
919 case INSN_CONFIG_GET_ROUTING:
920 case INSN_CONFIG_GET_PWM_STATUS:
921 case INSN_CONFIG_PWM_SET_PERIOD:
922 case INSN_CONFIG_PWM_GET_PERIOD:
926 case INSN_CONFIG_SET_GATE_SRC:
927 case INSN_CONFIG_GET_GATE_SRC:
928 case INSN_CONFIG_SET_CLOCK_SRC:
929 case INSN_CONFIG_GET_CLOCK_SRC:
930 case INSN_CONFIG_SET_OTHER_SRC:
931 case INSN_CONFIG_GET_COUNTER_STATUS:
932 case INSN_CONFIG_PWM_SET_H_BRIDGE:
933 case INSN_CONFIG_PWM_GET_H_BRIDGE:
934 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
938 case INSN_CONFIG_PWM_OUTPUT:
939 case INSN_CONFIG_ANALOG_TRIG:
943 case INSN_CONFIG_DIGITAL_TRIG:
947 /* by default we allow the insn since we don't have checks for
948 * all possible cases yet */
950 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
952 pr_warn("comedi: Add a check to %s in %s.\n",
954 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
960 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
961 unsigned int *data, void *file)
963 struct comedi_subdevice *s;
967 if (insn->insn & INSN_MASK_SPECIAL) {
968 /* a non-subdevice instruction */
970 switch (insn->insn) {
980 do_gettimeofday(&tv);
982 data[1] = tv.tv_usec;
988 if (insn->n != 1 || data[0] >= 100000) {
992 udelay(data[0] / 1000);
1000 if (insn->subdev >= dev->n_subdevices) {
1001 DPRINTK("%d not usable subdevice\n",
1006 s = &dev->subdevices[insn->subdev];
1008 DPRINTK("no async\n");
1012 if (!s->async->inttrig) {
1013 DPRINTK("no inttrig\n");
1017 ret = s->async->inttrig(dev, s, data[0]);
1022 DPRINTK("invalid insn\n");
1027 /* a subdevice instruction */
1028 unsigned int maxdata;
1030 if (insn->subdev >= dev->n_subdevices) {
1031 DPRINTK("subdevice %d out of range\n", insn->subdev);
1035 s = &dev->subdevices[insn->subdev];
1037 if (s->type == COMEDI_SUBD_UNUSED) {
1038 DPRINTK("%d not usable subdevice\n", insn->subdev);
1043 /* are we locked? (ioctl lock) */
1044 if (s->lock && s->lock != file) {
1045 DPRINTK("device locked\n");
1050 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1053 DPRINTK("bad chanspec\n");
1061 /* This looks arbitrary. It is. */
1062 s->busy = &parse_insn;
1063 switch (insn->insn) {
1065 ret = s->insn_read(dev, s, insn, data);
1068 maxdata = s->maxdata_list
1069 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1071 for (i = 0; i < insn->n; ++i) {
1072 if (data[i] > maxdata) {
1074 DPRINTK("bad data value(s)\n");
1079 ret = s->insn_write(dev, s, insn, data);
1085 /* Most drivers ignore the base channel in
1086 * insn->chanspec. Fix this here if
1087 * the subdevice has <= 32 channels. */
1089 unsigned int orig_mask;
1091 orig_mask = data[0];
1092 if (s->n_chan <= 32) {
1093 shift = CR_CHAN(insn->chanspec);
1101 ret = s->insn_bits(dev, s, insn, data);
1102 data[0] = orig_mask;
1108 ret = check_insn_config_length(insn, data);
1111 ret = s->insn_config(dev, s, insn, data);
1127 * synchronous instructions
1130 * pointer to sync cmd structure
1133 * sync cmd struct at arg
1140 /* arbitrary limits */
1141 #define MAX_SAMPLES 256
1142 static int do_insnlist_ioctl(struct comedi_device *dev,
1143 struct comedi_insnlist __user *arg, void *file)
1145 struct comedi_insnlist insnlist;
1146 struct comedi_insn *insns = NULL;
1147 unsigned int *data = NULL;
1151 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1154 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1156 DPRINTK("kmalloc failed\n");
1161 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1163 DPRINTK("kmalloc failed\n");
1168 if (copy_from_user(insns, insnlist.insns,
1169 sizeof(*insns) * insnlist.n_insns)) {
1170 DPRINTK("copy_from_user failed\n");
1175 for (i = 0; i < insnlist.n_insns; i++) {
1176 if (insns[i].n > MAX_SAMPLES) {
1177 DPRINTK("number of samples too large\n");
1181 if (insns[i].insn & INSN_MASK_WRITE) {
1182 if (copy_from_user(data, insns[i].data,
1183 insns[i].n * sizeof(unsigned int))) {
1184 DPRINTK("copy_from_user failed\n");
1189 ret = parse_insn(dev, insns + i, data, file);
1192 if (insns[i].insn & INSN_MASK_READ) {
1193 if (copy_to_user(insns[i].data, data,
1194 insns[i].n * sizeof(unsigned int))) {
1195 DPRINTK("copy_to_user failed\n");
1215 * synchronous instructions
1221 * struct comedi_insn struct at arg
1227 static int do_insn_ioctl(struct comedi_device *dev,
1228 struct comedi_insn __user *arg, void *file)
1230 struct comedi_insn insn;
1231 unsigned int *data = NULL;
1234 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1240 if (copy_from_user(&insn, arg, sizeof(insn))) {
1245 /* This is where the behavior of insn and insnlist deviate. */
1246 if (insn.n > MAX_SAMPLES)
1247 insn.n = MAX_SAMPLES;
1248 if (insn.insn & INSN_MASK_WRITE) {
1249 if (copy_from_user(data,
1251 insn.n * sizeof(unsigned int))) {
1256 ret = parse_insn(dev, &insn, data, file);
1259 if (insn.insn & INSN_MASK_READ) {
1260 if (copy_to_user(insn.data,
1262 insn.n * sizeof(unsigned int))) {
1275 static int do_cmd_ioctl(struct comedi_device *dev,
1276 struct comedi_cmd __user *arg, void *file)
1278 struct comedi_cmd cmd;
1279 struct comedi_subdevice *s;
1280 struct comedi_async *async;
1282 unsigned int __user *user_chanlist;
1284 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1285 DPRINTK("bad cmd address\n");
1288 /* save user's chanlist pointer so it can be restored later */
1289 user_chanlist = (unsigned int __user *)cmd.chanlist;
1291 if (cmd.subdev >= dev->n_subdevices) {
1292 DPRINTK("%d no such subdevice\n", cmd.subdev);
1296 s = &dev->subdevices[cmd.subdev];
1299 if (s->type == COMEDI_SUBD_UNUSED) {
1300 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1304 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1305 DPRINTK("subdevice %i does not support commands\n",
1310 /* are we locked? (ioctl lock) */
1311 if (s->lock && s->lock != file) {
1312 DPRINTK("subdevice locked\n");
1318 DPRINTK("subdevice busy\n");
1323 /* make sure channel/gain list isn't too long */
1324 if (cmd.chanlist_len > s->len_chanlist) {
1325 DPRINTK("channel/gain list too long %u > %d\n",
1326 cmd.chanlist_len, s->len_chanlist);
1331 /* make sure channel/gain list isn't too short */
1332 if (cmd.chanlist_len < 1) {
1333 DPRINTK("channel/gain list too short %u < 1\n",
1340 async->cmd.data = NULL;
1341 /* load channel/gain list */
1342 async->cmd.chanlist =
1343 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1344 if (!async->cmd.chanlist) {
1345 DPRINTK("allocation failed\n");
1350 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1351 async->cmd.chanlist_len * sizeof(int))) {
1352 DPRINTK("fault reading chanlist\n");
1357 /* make sure each element in channel/gain list is valid */
1358 ret = comedi_check_chanlist(s,
1359 async->cmd.chanlist_len,
1360 async->cmd.chanlist);
1362 DPRINTK("bad chanlist\n");
1366 ret = s->do_cmdtest(dev, s, &async->cmd);
1368 if (async->cmd.flags & TRIG_BOGUS || ret) {
1369 DPRINTK("test returned %d\n", ret);
1371 /* restore chanlist pointer before copying back */
1372 cmd.chanlist = (unsigned int __force *)user_chanlist;
1374 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1375 DPRINTK("fault writing cmd\n");
1383 if (!async->prealloc_bufsz) {
1385 DPRINTK("no buffer (?)\n");
1389 comedi_buf_reset(async);
1392 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1394 if (async->cmd.flags & TRIG_WAKE_EOS)
1395 async->cb_mask |= COMEDI_CB_EOS;
1397 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1399 ret = s->do_cmd(dev, s);
1404 do_become_nonbusy(dev, s);
1411 command testing ioctl
1414 pointer to cmd structure
1417 cmd structure at arg
1421 modified cmd structure at arg
1424 static int do_cmdtest_ioctl(struct comedi_device *dev,
1425 struct comedi_cmd __user *arg, void *file)
1427 struct comedi_cmd cmd;
1428 struct comedi_subdevice *s;
1430 unsigned int *chanlist = NULL;
1431 unsigned int __user *user_chanlist;
1433 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1434 DPRINTK("bad cmd address\n");
1437 /* save user's chanlist pointer so it can be restored later */
1438 user_chanlist = (unsigned int __user *)cmd.chanlist;
1440 if (cmd.subdev >= dev->n_subdevices) {
1441 DPRINTK("%d no such subdevice\n", cmd.subdev);
1445 s = &dev->subdevices[cmd.subdev];
1446 if (s->type == COMEDI_SUBD_UNUSED) {
1447 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1451 if (!s->do_cmd || !s->do_cmdtest) {
1452 DPRINTK("subdevice %i does not support commands\n",
1457 /* make sure channel/gain list isn't too long */
1458 if (cmd.chanlist_len > s->len_chanlist) {
1459 DPRINTK("channel/gain list too long %d > %d\n",
1460 cmd.chanlist_len, s->len_chanlist);
1465 /* load channel/gain list */
1468 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1470 DPRINTK("allocation failed\n");
1475 if (copy_from_user(chanlist, user_chanlist,
1476 cmd.chanlist_len * sizeof(int))) {
1477 DPRINTK("fault reading chanlist\n");
1482 /* make sure each element in channel/gain list is valid */
1483 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1485 DPRINTK("bad chanlist\n");
1489 cmd.chanlist = chanlist;
1492 ret = s->do_cmdtest(dev, s, &cmd);
1494 /* restore chanlist pointer before copying back */
1495 cmd.chanlist = (unsigned int __force *)user_chanlist;
1497 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1498 DPRINTK("bad cmd address\n");
1523 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1527 unsigned long flags;
1528 struct comedi_subdevice *s;
1530 if (arg >= dev->n_subdevices)
1532 s = &dev->subdevices[arg];
1534 spin_lock_irqsave(&s->spin_lock, flags);
1535 if (s->busy || s->lock)
1539 spin_unlock_irqrestore(&s->spin_lock, flags);
1546 ret = s->lock_f(dev, s);
1565 This function isn't protected by the semaphore, since
1566 we already own the lock.
1568 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1571 struct comedi_subdevice *s;
1573 if (arg >= dev->n_subdevices)
1575 s = &dev->subdevices[arg];
1580 if (s->lock && s->lock != file)
1583 if (s->lock == file) {
1597 cancel acquisition ioctl
1609 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1612 struct comedi_subdevice *s;
1614 if (arg >= dev->n_subdevices)
1616 s = &dev->subdevices[arg];
1617 if (s->async == NULL)
1620 if (s->lock && s->lock != file)
1626 if (s->busy != file)
1629 return do_cancel(dev, s);
1634 instructs driver to synchronize buffers
1646 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1649 struct comedi_subdevice *s;
1651 if (arg >= dev->n_subdevices)
1653 s = &dev->subdevices[arg];
1655 if (s->lock && s->lock != file)
1661 if (s->busy != file)
1665 return s->poll(dev, s);
1670 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1673 const unsigned minor = iminor(file_inode(file));
1674 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1675 struct comedi_device *dev = comedi_dev_from_file_info(info);
1681 mutex_lock(&dev->mutex);
1683 /* Device config is special, because it must work on
1684 * an unconfigured device. */
1685 if (cmd == COMEDI_DEVCONFIG) {
1686 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1687 /* Device config not appropriate on non-board minors. */
1691 rc = do_devconfig_ioctl(dev,
1692 (struct comedi_devconfig __user *)arg);
1695 dev->minor >= comedi_num_legacy_minors) {
1696 /* Successfully unconfigured a dynamically
1697 * allocated device. Try and remove it. */
1698 info = comedi_clear_minor(dev->minor);
1700 mutex_unlock(&dev->mutex);
1701 comedi_free_board_file_info(info);
1709 if (!dev->attached) {
1710 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1716 case COMEDI_BUFCONFIG:
1717 rc = do_bufconfig_ioctl(dev,
1718 (struct comedi_bufconfig __user *)arg);
1720 case COMEDI_DEVINFO:
1721 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1724 case COMEDI_SUBDINFO:
1725 rc = do_subdinfo_ioctl(dev,
1726 (struct comedi_subdinfo __user *)arg,
1729 case COMEDI_CHANINFO:
1730 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1732 case COMEDI_RANGEINFO:
1733 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1735 case COMEDI_BUFINFO:
1736 rc = do_bufinfo_ioctl(dev,
1737 (struct comedi_bufinfo __user *)arg,
1741 rc = do_lock_ioctl(dev, arg, file);
1744 rc = do_unlock_ioctl(dev, arg, file);
1747 rc = do_cancel_ioctl(dev, arg, file);
1750 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1752 case COMEDI_CMDTEST:
1753 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1756 case COMEDI_INSNLIST:
1757 rc = do_insnlist_ioctl(dev,
1758 (struct comedi_insnlist __user *)arg,
1762 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1766 rc = do_poll_ioctl(dev, arg, file);
1774 mutex_unlock(&dev->mutex);
1778 static void comedi_vm_open(struct vm_area_struct *area)
1780 struct comedi_async *async;
1781 struct comedi_device *dev;
1783 async = area->vm_private_data;
1784 dev = async->subdevice->device;
1786 mutex_lock(&dev->mutex);
1787 async->mmap_count++;
1788 mutex_unlock(&dev->mutex);
1791 static void comedi_vm_close(struct vm_area_struct *area)
1793 struct comedi_async *async;
1794 struct comedi_device *dev;
1796 async = area->vm_private_data;
1797 dev = async->subdevice->device;
1799 mutex_lock(&dev->mutex);
1800 async->mmap_count--;
1801 mutex_unlock(&dev->mutex);
1804 static struct vm_operations_struct comedi_vm_ops = {
1805 .open = comedi_vm_open,
1806 .close = comedi_vm_close,
1809 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1811 const unsigned minor = iminor(file_inode(file));
1812 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1813 struct comedi_device *dev = comedi_dev_from_file_info(info);
1814 struct comedi_subdevice *s;
1815 struct comedi_async *async;
1816 unsigned long start = vma->vm_start;
1825 mutex_lock(&dev->mutex);
1827 if (!dev->attached) {
1828 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1833 if (vma->vm_flags & VM_WRITE)
1834 s = comedi_write_subdevice(info);
1836 s = comedi_read_subdevice(info);
1848 if (vma->vm_pgoff != 0) {
1849 DPRINTK("comedi: mmap() offset must be 0.\n");
1854 size = vma->vm_end - vma->vm_start;
1855 if (size > async->prealloc_bufsz) {
1859 if (size & (~PAGE_MASK)) {
1864 n_pages = size >> PAGE_SHIFT;
1865 for (i = 0; i < n_pages; ++i) {
1866 struct comedi_buf_page *buf = &async->buf_page_list[i];
1868 if (remap_pfn_range(vma, start,
1869 page_to_pfn(virt_to_page(buf->virt_addr)),
1870 PAGE_SIZE, PAGE_SHARED)) {
1877 vma->vm_ops = &comedi_vm_ops;
1878 vma->vm_private_data = async;
1880 async->mmap_count++;
1884 mutex_unlock(&dev->mutex);
1888 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1890 unsigned int mask = 0;
1891 const unsigned minor = iminor(file_inode(file));
1892 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1893 struct comedi_device *dev = comedi_dev_from_file_info(info);
1894 struct comedi_subdevice *s;
1899 mutex_lock(&dev->mutex);
1901 if (!dev->attached) {
1902 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1906 s = comedi_read_subdevice(info);
1907 if (s && s->async) {
1908 poll_wait(file, &s->async->wait_head, wait);
1909 if (!s->busy || !comedi_is_subdevice_running(s) ||
1910 comedi_buf_read_n_available(s->async) > 0)
1911 mask |= POLLIN | POLLRDNORM;
1914 s = comedi_write_subdevice(info);
1915 if (s && s->async) {
1916 unsigned int bps = bytes_per_sample(s->async->subdevice);
1918 poll_wait(file, &s->async->wait_head, wait);
1919 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1920 if (!s->busy || !comedi_is_subdevice_running(s) ||
1921 comedi_buf_write_n_allocated(s->async) >= bps)
1922 mask |= POLLOUT | POLLWRNORM;
1926 mutex_unlock(&dev->mutex);
1930 static ssize_t comedi_write(struct file *file, const char __user *buf,
1931 size_t nbytes, loff_t *offset)
1933 struct comedi_subdevice *s;
1934 struct comedi_async *async;
1935 int n, m, count = 0, retval = 0;
1936 DECLARE_WAITQUEUE(wait, current);
1937 const unsigned minor = iminor(file_inode(file));
1938 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1939 struct comedi_device *dev = comedi_dev_from_file_info(info);
1944 if (!dev->attached) {
1945 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1949 s = comedi_write_subdevice(info);
1950 if (!s || !s->async)
1955 if (!s->busy || !nbytes)
1957 if (s->busy != file)
1960 add_wait_queue(&async->wait_head, &wait);
1961 while (nbytes > 0 && !retval) {
1962 set_current_state(TASK_INTERRUPTIBLE);
1964 if (!comedi_is_subdevice_running(s)) {
1966 if (comedi_is_subdevice_in_error(s))
1970 do_become_nonbusy(dev, s);
1978 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1979 m = async->prealloc_bufsz - async->buf_write_ptr;
1980 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1981 if (m > comedi_buf_write_n_allocated(async))
1982 m = comedi_buf_write_n_allocated(async);
1987 if (file->f_flags & O_NONBLOCK) {
1992 if (signal_pending(current)) {
1993 retval = -ERESTARTSYS;
1998 if (s->busy != file) {
2005 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2011 comedi_buf_write_free(async, n);
2017 break; /* makes device work like a pipe */
2019 set_current_state(TASK_RUNNING);
2020 remove_wait_queue(&async->wait_head, &wait);
2022 return count ? count : retval;
2025 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2028 struct comedi_subdevice *s;
2029 struct comedi_async *async;
2030 int n, m, count = 0, retval = 0;
2031 DECLARE_WAITQUEUE(wait, current);
2032 const unsigned minor = iminor(file_inode(file));
2033 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
2034 struct comedi_device *dev = comedi_dev_from_file_info(info);
2039 if (!dev->attached) {
2040 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2044 s = comedi_read_subdevice(info);
2045 if (!s || !s->async)
2049 if (!s->busy || !nbytes)
2051 if (s->busy != file)
2054 add_wait_queue(&async->wait_head, &wait);
2055 while (nbytes > 0 && !retval) {
2056 set_current_state(TASK_INTERRUPTIBLE);
2060 m = comedi_buf_read_n_available(async);
2061 /* printk("%d available\n",m); */
2062 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2063 m = async->prealloc_bufsz - async->buf_read_ptr;
2064 /* printk("%d contiguous\n",m); */
2069 if (!comedi_is_subdevice_running(s)) {
2070 do_become_nonbusy(dev, s);
2071 if (comedi_is_subdevice_in_error(s))
2077 if (file->f_flags & O_NONBLOCK) {
2082 if (signal_pending(current)) {
2083 retval = -ERESTARTSYS;
2090 if (s->busy != file) {
2096 m = copy_to_user(buf, async->prealloc_buf +
2097 async->buf_read_ptr, n);
2103 comedi_buf_read_alloc(async, n);
2104 comedi_buf_read_free(async, n);
2110 break; /* makes device work like a pipe */
2112 if (comedi_is_subdevice_idle(s) &&
2113 async->buf_read_count - async->buf_write_count == 0) {
2114 do_become_nonbusy(dev, s);
2116 set_current_state(TASK_RUNNING);
2117 remove_wait_queue(&async->wait_head, &wait);
2119 return count ? count : retval;
2122 static int comedi_open(struct inode *inode, struct file *file)
2124 const unsigned minor = iminor(inode);
2125 struct comedi_device *dev = comedi_dev_from_minor(minor);
2128 DPRINTK("invalid minor number\n");
2132 /* This is slightly hacky, but we want module autoloading
2134 * case: user opens device, attached -> ok
2135 * case: user opens device, unattached, !in_request_module -> autoload
2136 * case: user opens device, unattached, in_request_module -> fail
2137 * case: root opens device, attached -> ok
2138 * case: root opens device, unattached, in_request_module -> ok
2139 * (typically called from modprobe)
2140 * case: root opens device, unattached, !in_request_module -> autoload
2142 * The last could be changed to "-> ok", which would deny root
2145 mutex_lock(&dev->mutex);
2148 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2149 DPRINTK("in request module\n");
2150 mutex_unlock(&dev->mutex);
2153 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2156 dev->in_request_module = true;
2159 mutex_unlock(&dev->mutex);
2160 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2161 mutex_lock(&dev->mutex);
2164 dev->in_request_module = false;
2166 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2167 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2168 mutex_unlock(&dev->mutex);
2172 __module_get(THIS_MODULE);
2174 if (dev->attached) {
2175 if (!try_module_get(dev->driver->module)) {
2176 module_put(THIS_MODULE);
2177 mutex_unlock(&dev->mutex);
2182 if (dev->attached && dev->use_count == 0 && dev->open) {
2183 int rc = dev->open(dev);
2185 module_put(dev->driver->module);
2186 module_put(THIS_MODULE);
2187 mutex_unlock(&dev->mutex);
2194 mutex_unlock(&dev->mutex);
2199 static int comedi_fasync(int fd, struct file *file, int on)
2201 const unsigned minor = iminor(file_inode(file));
2202 struct comedi_device *dev = comedi_dev_from_minor(minor);
2207 return fasync_helper(fd, file, on, &dev->async_queue);
2210 static int comedi_close(struct inode *inode, struct file *file)
2212 const unsigned minor = iminor(inode);
2213 struct comedi_device *dev = comedi_dev_from_minor(minor);
2214 struct comedi_subdevice *s = NULL;
2220 mutex_lock(&dev->mutex);
2222 if (dev->subdevices) {
2223 for (i = 0; i < dev->n_subdevices; i++) {
2224 s = &dev->subdevices[i];
2226 if (s->busy == file)
2228 if (s->lock == file)
2232 if (dev->attached && dev->use_count == 1 && dev->close)
2235 module_put(THIS_MODULE);
2237 module_put(dev->driver->module);
2241 mutex_unlock(&dev->mutex);
2243 if (file->f_flags & FASYNC)
2244 comedi_fasync(-1, file, 0);
2249 static const struct file_operations comedi_fops = {
2250 .owner = THIS_MODULE,
2251 .unlocked_ioctl = comedi_unlocked_ioctl,
2252 .compat_ioctl = comedi_compat_ioctl,
2253 .open = comedi_open,
2254 .release = comedi_close,
2255 .read = comedi_read,
2256 .write = comedi_write,
2257 .mmap = comedi_mmap,
2258 .poll = comedi_poll,
2259 .fasync = comedi_fasync,
2260 .llseek = noop_llseek,
2263 void comedi_error(const struct comedi_device *dev, const char *s)
2265 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2267 EXPORT_SYMBOL(comedi_error);
2269 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2271 struct comedi_async *async = s->async;
2272 unsigned runflags = 0;
2273 unsigned runflags_mask = 0;
2275 /* DPRINTK("comedi_event 0x%x\n",mask); */
2277 if (!comedi_is_subdevice_running(s))
2281 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2282 COMEDI_CB_OVERFLOW)) {
2283 runflags_mask |= SRF_RUNNING;
2285 /* remember if an error event has occurred, so an error
2286 * can be returned the next time the user does a read() */
2287 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2288 runflags_mask |= SRF_ERROR;
2289 runflags |= SRF_ERROR;
2291 if (runflags_mask) {
2292 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2293 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2296 if (async->cb_mask & s->async->events) {
2297 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2298 wake_up_interruptible(&async->wait_head);
2299 if (s->subdev_flags & SDF_CMD_READ)
2300 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2301 if (s->subdev_flags & SDF_CMD_WRITE)
2302 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2305 async->cb_func(s->async->events, async->cb_arg);
2308 s->async->events = 0;
2310 EXPORT_SYMBOL(comedi_event);
2312 /* Note: the ->mutex is pre-locked on successful return */
2313 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2315 struct comedi_file_info *info;
2316 struct comedi_device *dev;
2317 struct device *csdev;
2320 info = kzalloc(sizeof(*info), GFP_KERNEL);
2322 return ERR_PTR(-ENOMEM);
2323 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2326 return ERR_PTR(-ENOMEM);
2329 info->hardware_device = hardware_device;
2330 comedi_device_init(dev);
2331 comedi_set_hw_dev(dev, hardware_device);
2332 mutex_lock(&dev->mutex);
2333 mutex_lock(&comedi_file_info_table_lock);
2334 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2335 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2336 if (comedi_file_info_table[i] == NULL) {
2337 comedi_file_info_table[i] = info;
2341 mutex_unlock(&comedi_file_info_table_lock);
2342 if (i == COMEDI_NUM_BOARD_MINORS) {
2343 mutex_unlock(&dev->mutex);
2344 comedi_device_cleanup(dev);
2347 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2348 return ERR_PTR(-EBUSY);
2351 csdev = device_create(comedi_class, hardware_device,
2352 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2354 dev->class_dev = csdev;
2355 dev_set_drvdata(csdev, info);
2357 /* Note: dev->mutex needs to be unlocked by the caller. */
2361 static void comedi_free_board_minor(unsigned minor)
2363 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2364 comedi_free_board_file_info(comedi_clear_minor(minor));
2367 void comedi_release_hardware_device(struct device *hardware_device)
2370 struct comedi_file_info *info;
2372 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2374 mutex_lock(&comedi_file_info_table_lock);
2375 info = comedi_file_info_table[minor];
2376 if (info && info->hardware_device == hardware_device) {
2377 comedi_file_info_table[minor] = NULL;
2378 mutex_unlock(&comedi_file_info_table_lock);
2379 comedi_free_board_file_info(info);
2382 mutex_unlock(&comedi_file_info_table_lock);
2386 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2388 struct comedi_device *dev = s->device;
2389 struct comedi_file_info *info;
2390 struct device *csdev;
2393 info = kzalloc(sizeof(*info), GFP_KERNEL);
2397 if (s->subdev_flags & SDF_CMD_READ)
2398 info->read_subdevice = s;
2399 if (s->subdev_flags & SDF_CMD_WRITE)
2400 info->write_subdevice = s;
2401 mutex_lock(&comedi_file_info_table_lock);
2402 for (i = COMEDI_NUM_BOARD_MINORS; i < COMEDI_NUM_MINORS; ++i) {
2403 if (comedi_file_info_table[i] == NULL) {
2404 comedi_file_info_table[i] = info;
2408 mutex_unlock(&comedi_file_info_table_lock);
2409 if (i == COMEDI_NUM_MINORS) {
2411 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2415 csdev = device_create(comedi_class, dev->class_dev,
2416 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2417 dev->minor, s->index);
2419 s->class_dev = csdev;
2420 dev_set_drvdata(csdev, info);
2425 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2427 struct comedi_file_info *info;
2434 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2435 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2437 info = comedi_clear_minor(s->minor);
2439 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2440 s->class_dev = NULL;
2445 static void comedi_cleanup_board_minors(void)
2449 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2450 comedi_free_board_minor(i);
2453 static int __init comedi_init(void)
2458 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2460 if (comedi_num_legacy_minors < 0 ||
2461 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2462 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2463 COMEDI_NUM_BOARD_MINORS);
2467 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2468 COMEDI_NUM_MINORS, "comedi");
2471 cdev_init(&comedi_cdev, &comedi_fops);
2472 comedi_cdev.owner = THIS_MODULE;
2473 kobject_set_name(&comedi_cdev.kobj, "comedi");
2474 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2475 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2479 comedi_class = class_create(THIS_MODULE, "comedi");
2480 if (IS_ERR(comedi_class)) {
2481 pr_err("comedi: failed to create class\n");
2482 cdev_del(&comedi_cdev);
2483 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2485 return PTR_ERR(comedi_class);
2488 comedi_class->dev_attrs = comedi_dev_attrs;
2490 /* XXX requires /proc interface */
2493 /* create devices files for legacy/manual use */
2494 for (i = 0; i < comedi_num_legacy_minors; i++) {
2495 struct comedi_device *dev;
2496 dev = comedi_alloc_board_minor(NULL);
2498 comedi_cleanup_board_minors();
2499 cdev_del(&comedi_cdev);
2500 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2502 return PTR_ERR(dev);
2504 /* comedi_alloc_board_minor() locked the mutex */
2505 mutex_unlock(&dev->mutex);
2511 module_init(comedi_init);
2513 static void __exit comedi_cleanup(void)
2517 comedi_cleanup_board_minors();
2518 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2519 BUG_ON(comedi_file_info_table[i]);
2521 class_destroy(comedi_class);
2522 cdev_del(&comedi_cdev);
2523 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2525 comedi_proc_cleanup();
2527 module_exit(comedi_cleanup);
2529 MODULE_AUTHOR("http://www.comedi.org");
2530 MODULE_DESCRIPTION("Comedi core module");
2531 MODULE_LICENSE("GPL");