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
53 #define COMEDI_NUM_SUBDEVICE_MINORS \
54 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
56 #ifdef CONFIG_COMEDI_DEBUG
58 EXPORT_SYMBOL_GPL(comedi_debug);
59 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
60 MODULE_PARM_DESC(comedi_debug,
61 "enable comedi core and driver debugging if non-zero (default 0)"
65 static int comedi_num_legacy_minors;
66 module_param(comedi_num_legacy_minors, int, S_IRUGO);
67 MODULE_PARM_DESC(comedi_num_legacy_minors,
68 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
71 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
72 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
73 MODULE_PARM_DESC(comedi_default_buf_size_kb,
74 "default asynchronous buffer size in KiB (default "
75 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
77 unsigned int comedi_default_buf_maxsize_kb
78 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
79 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
80 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
81 "default maximum size of asynchronous buffer in KiB (default "
82 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
84 static DEFINE_MUTEX(comedi_board_minor_table_lock);
85 static struct comedi_device
86 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
88 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
89 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
90 static struct comedi_subdevice
91 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
93 static struct class *comedi_class;
94 static struct cdev comedi_cdev;
96 static void comedi_device_init(struct comedi_device *dev)
98 spin_lock_init(&dev->spinlock);
99 mutex_init(&dev->mutex);
103 static void comedi_device_cleanup(struct comedi_device *dev)
105 struct module *driver_module = NULL;
109 mutex_lock(&dev->mutex);
111 driver_module = dev->driver->module;
112 comedi_device_detach(dev);
113 while (dev->use_count > 0) {
115 module_put(driver_module);
116 module_put(THIS_MODULE);
119 mutex_unlock(&dev->mutex);
120 mutex_destroy(&dev->mutex);
123 static bool comedi_clear_board_dev(struct comedi_device *dev)
125 unsigned int i = dev->minor;
126 bool cleared = false;
128 mutex_lock(&comedi_board_minor_table_lock);
129 if (dev == comedi_board_minor_table[i]) {
130 comedi_board_minor_table[i] = NULL;
133 mutex_unlock(&comedi_board_minor_table_lock);
137 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
139 struct comedi_device *dev;
141 mutex_lock(&comedi_board_minor_table_lock);
142 dev = comedi_board_minor_table[minor];
143 comedi_board_minor_table[minor] = NULL;
144 mutex_unlock(&comedi_board_minor_table_lock);
148 static void comedi_free_board_dev(struct comedi_device *dev)
151 if (dev->class_dev) {
152 device_destroy(comedi_class,
153 MKDEV(COMEDI_MAJOR, dev->minor));
155 comedi_device_cleanup(dev);
160 static struct comedi_subdevice
161 *comedi_subdevice_from_minor(unsigned minor)
163 struct comedi_subdevice *s;
164 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
166 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
167 mutex_lock(&comedi_subdevice_minor_table_lock);
168 s = comedi_subdevice_minor_table[i];
169 mutex_unlock(&comedi_subdevice_minor_table_lock);
173 static struct comedi_device *comedi_dev_from_board_minor(unsigned minor)
175 struct comedi_device *dev;
177 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
178 mutex_lock(&comedi_board_minor_table_lock);
179 dev = comedi_board_minor_table[minor];
180 mutex_unlock(&comedi_board_minor_table_lock);
184 static struct comedi_device *comedi_dev_from_subdevice_minor(unsigned minor)
186 struct comedi_subdevice *s;
188 s = comedi_subdevice_from_minor(minor);
189 return s ? s->device : NULL;
192 struct comedi_device *comedi_dev_from_minor(unsigned minor)
194 if (minor < COMEDI_NUM_BOARD_MINORS)
195 return comedi_dev_from_board_minor(minor);
197 return comedi_dev_from_subdevice_minor(minor);
199 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
201 static struct comedi_subdevice *
202 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
204 struct comedi_subdevice *s;
206 if (minor >= COMEDI_NUM_BOARD_MINORS) {
207 s = comedi_subdevice_from_minor(minor);
208 if (!s || s->device != dev)
210 if (s->subdev_flags & SDF_CMD_READ)
213 return dev->read_subdev;
216 static struct comedi_subdevice *
217 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
219 struct comedi_subdevice *s;
221 if (minor >= COMEDI_NUM_BOARD_MINORS) {
222 s = comedi_subdevice_from_minor(minor);
223 if (!s || s->device != dev)
225 if (s->subdev_flags & SDF_CMD_WRITE)
228 return dev->write_subdev;
231 static int resize_async_buffer(struct comedi_device *dev,
232 struct comedi_subdevice *s,
233 struct comedi_async *async, unsigned new_size)
237 if (new_size > async->max_bufsize)
241 DPRINTK("subdevice is busy, cannot resize buffer\n");
244 if (async->mmap_count) {
245 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
249 if (!async->prealloc_buf)
252 /* make sure buffer is an integral number of pages
254 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
256 retval = comedi_buf_alloc(dev, s, new_size);
261 retval = s->buf_change(dev, s, new_size);
266 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
267 dev->minor, s->index, async->prealloc_bufsz);
271 /* sysfs attribute files */
273 static ssize_t show_max_read_buffer_kb(struct device *csdev,
274 struct device_attribute *attr, char *buf)
276 unsigned int minor = MINOR(csdev->devt);
277 struct comedi_device *dev;
278 struct comedi_subdevice *s;
279 unsigned int size = 0;
281 dev = comedi_dev_from_minor(minor);
285 mutex_lock(&dev->mutex);
286 s = comedi_read_subdevice(dev, minor);
287 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
288 size = s->async->max_bufsize / 1024;
289 mutex_unlock(&dev->mutex);
291 return snprintf(buf, PAGE_SIZE, "%i\n", size);
294 static ssize_t store_max_read_buffer_kb(struct device *csdev,
295 struct device_attribute *attr,
296 const char *buf, size_t count)
298 unsigned int minor = MINOR(csdev->devt);
299 struct comedi_device *dev;
300 struct comedi_subdevice *s;
304 err = kstrtouint(buf, 10, &size);
307 if (size > (UINT_MAX / 1024))
311 dev = comedi_dev_from_minor(minor);
315 mutex_lock(&dev->mutex);
316 s = comedi_read_subdevice(dev, minor);
317 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
318 s->async->max_bufsize = size;
321 mutex_unlock(&dev->mutex);
323 return err ? err : count;
326 static ssize_t show_read_buffer_kb(struct device *csdev,
327 struct device_attribute *attr, char *buf)
329 unsigned int minor = MINOR(csdev->devt);
330 struct comedi_device *dev;
331 struct comedi_subdevice *s;
332 unsigned int size = 0;
334 dev = comedi_dev_from_minor(minor);
338 mutex_lock(&dev->mutex);
339 s = comedi_read_subdevice(dev, minor);
340 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
341 size = s->async->prealloc_bufsz / 1024;
342 mutex_unlock(&dev->mutex);
344 return snprintf(buf, PAGE_SIZE, "%i\n", size);
347 static ssize_t store_read_buffer_kb(struct device *csdev,
348 struct device_attribute *attr,
349 const char *buf, size_t count)
351 unsigned int minor = MINOR(csdev->devt);
352 struct comedi_device *dev;
353 struct comedi_subdevice *s;
357 err = kstrtouint(buf, 10, &size);
360 if (size > (UINT_MAX / 1024))
364 dev = comedi_dev_from_minor(minor);
368 mutex_lock(&dev->mutex);
369 s = comedi_read_subdevice(dev, minor);
370 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
371 err = resize_async_buffer(dev, s, s->async, size);
374 mutex_unlock(&dev->mutex);
376 return err ? err : count;
379 static ssize_t show_max_write_buffer_kb(struct device *csdev,
380 struct device_attribute *attr,
383 unsigned int minor = MINOR(csdev->devt);
384 struct comedi_device *dev;
385 struct comedi_subdevice *s;
386 unsigned int size = 0;
388 dev = comedi_dev_from_minor(minor);
392 mutex_lock(&dev->mutex);
393 s = comedi_write_subdevice(dev, minor);
394 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
395 size = s->async->max_bufsize / 1024;
396 mutex_unlock(&dev->mutex);
398 return snprintf(buf, PAGE_SIZE, "%i\n", size);
401 static ssize_t store_max_write_buffer_kb(struct device *csdev,
402 struct device_attribute *attr,
403 const char *buf, size_t count)
405 unsigned int minor = MINOR(csdev->devt);
406 struct comedi_device *dev;
407 struct comedi_subdevice *s;
411 err = kstrtouint(buf, 10, &size);
414 if (size > (UINT_MAX / 1024))
418 dev = comedi_dev_from_minor(minor);
422 mutex_lock(&dev->mutex);
423 s = comedi_write_subdevice(dev, minor);
424 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
425 s->async->max_bufsize = size;
428 mutex_unlock(&dev->mutex);
430 return err ? err : count;
433 static ssize_t show_write_buffer_kb(struct device *csdev,
434 struct device_attribute *attr, char *buf)
436 unsigned int minor = MINOR(csdev->devt);
437 struct comedi_device *dev;
438 struct comedi_subdevice *s;
439 unsigned int size = 0;
441 dev = comedi_dev_from_minor(minor);
445 mutex_lock(&dev->mutex);
446 s = comedi_write_subdevice(dev, minor);
447 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
448 size = s->async->prealloc_bufsz / 1024;
449 mutex_unlock(&dev->mutex);
451 return snprintf(buf, PAGE_SIZE, "%i\n", size);
454 static ssize_t store_write_buffer_kb(struct device *csdev,
455 struct device_attribute *attr,
456 const char *buf, size_t count)
458 unsigned int minor = MINOR(csdev->devt);
459 struct comedi_device *dev;
460 struct comedi_subdevice *s;
464 err = kstrtouint(buf, 10, &size);
467 if (size > (UINT_MAX / 1024))
471 dev = comedi_dev_from_minor(minor);
475 mutex_lock(&dev->mutex);
476 s = comedi_write_subdevice(dev, minor);
477 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
478 err = resize_async_buffer(dev, s, s->async, size);
481 mutex_unlock(&dev->mutex);
483 return err ? err : count;
486 static struct device_attribute comedi_dev_attrs[] = {
487 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
488 show_max_read_buffer_kb, store_max_read_buffer_kb),
489 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
490 show_read_buffer_kb, store_read_buffer_kb),
491 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
492 show_max_write_buffer_kb, store_max_write_buffer_kb),
493 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
494 show_write_buffer_kb, store_write_buffer_kb),
498 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
499 unsigned mask, unsigned bits)
503 spin_lock_irqsave(&s->spin_lock, flags);
504 s->runflags &= ~mask;
505 s->runflags |= (bits & mask);
506 spin_unlock_irqrestore(&s->spin_lock, flags);
509 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
514 spin_lock_irqsave(&s->spin_lock, flags);
515 runflags = s->runflags;
516 spin_unlock_irqrestore(&s->spin_lock, flags);
520 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
522 unsigned runflags = comedi_get_subdevice_runflags(s);
524 return (runflags & SRF_RUNNING) ? true : false;
526 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
528 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
530 unsigned runflags = comedi_get_subdevice_runflags(s);
532 return (runflags & SRF_ERROR) ? true : false;
535 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
537 unsigned runflags = comedi_get_subdevice_runflags(s);
539 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
543 This function restores a subdevice to an idle state.
545 static void do_become_nonbusy(struct comedi_device *dev,
546 struct comedi_subdevice *s)
548 struct comedi_async *async = s->async;
550 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
552 comedi_buf_reset(async);
553 async->inttrig = NULL;
554 kfree(async->cmd.chanlist);
555 async->cmd.chanlist = NULL;
557 dev_err(dev->class_dev,
558 "BUG: (?) do_become_nonbusy called with async=NULL\n");
564 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
568 if (comedi_is_subdevice_running(s) && s->cancel)
569 ret = s->cancel(dev, s);
571 do_become_nonbusy(dev, s);
576 static int is_device_busy(struct comedi_device *dev)
578 struct comedi_subdevice *s;
584 for (i = 0; i < dev->n_subdevices; i++) {
585 s = &dev->subdevices[i];
588 if (s->async && s->async->mmap_count)
600 pointer to devconfig structure
603 devconfig structure at arg
608 static int do_devconfig_ioctl(struct comedi_device *dev,
609 struct comedi_devconfig __user *arg)
611 struct comedi_devconfig it;
613 if (!capable(CAP_SYS_ADMIN))
617 if (is_device_busy(dev))
620 struct module *driver_module = dev->driver->module;
621 comedi_device_detach(dev);
622 module_put(driver_module);
627 if (copy_from_user(&it, arg, sizeof(it)))
630 it.board_name[COMEDI_NAMELEN - 1] = 0;
632 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
633 dev_warn(dev->class_dev,
634 "comedi_config --init_data is deprecated\n");
638 if (dev->minor >= comedi_num_legacy_minors)
639 /* don't re-use dynamically allocated comedi devices */
642 /* This increments the driver module count on success. */
643 return comedi_device_attach(dev, &it);
648 buffer configuration ioctl
651 pointer to bufconfig structure
657 modified bufconfig at arg
660 static int do_bufconfig_ioctl(struct comedi_device *dev,
661 struct comedi_bufconfig __user *arg)
663 struct comedi_bufconfig bc;
664 struct comedi_async *async;
665 struct comedi_subdevice *s;
668 if (copy_from_user(&bc, arg, sizeof(bc)))
671 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
674 s = &dev->subdevices[bc.subdevice];
678 DPRINTK("subdevice does not have async capability\n");
684 if (bc.maximum_size) {
685 if (!capable(CAP_SYS_ADMIN))
688 async->max_bufsize = bc.maximum_size;
692 retval = resize_async_buffer(dev, s, async, bc.size);
697 bc.size = async->prealloc_bufsz;
698 bc.maximum_size = async->max_bufsize;
701 if (copy_to_user(arg, &bc, sizeof(bc)))
712 pointer to devinfo structure
721 static int do_devinfo_ioctl(struct comedi_device *dev,
722 struct comedi_devinfo __user *arg,
725 const unsigned minor = iminor(file_inode(file));
726 struct comedi_subdevice *s;
727 struct comedi_devinfo devinfo;
729 memset(&devinfo, 0, sizeof(devinfo));
731 /* fill devinfo structure */
732 devinfo.version_code = COMEDI_VERSION_CODE;
733 devinfo.n_subdevs = dev->n_subdevices;
734 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
735 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
737 s = comedi_read_subdevice(dev, minor);
739 devinfo.read_subdevice = s->index;
741 devinfo.read_subdevice = -1;
743 s = comedi_write_subdevice(dev, minor);
745 devinfo.write_subdevice = s->index;
747 devinfo.write_subdevice = -1;
749 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
760 pointer to array of subdevice info structures
766 array of subdevice info structures at arg
769 static int do_subdinfo_ioctl(struct comedi_device *dev,
770 struct comedi_subdinfo __user *arg, void *file)
773 struct comedi_subdinfo *tmp, *us;
774 struct comedi_subdevice *s;
776 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
780 /* fill subdinfo structs */
781 for (i = 0; i < dev->n_subdevices; i++) {
782 s = &dev->subdevices[i];
786 us->n_chan = s->n_chan;
787 us->subd_flags = s->subdev_flags;
788 if (comedi_is_subdevice_running(s))
789 us->subd_flags |= SDF_RUNNING;
790 #define TIMER_nanosec 5 /* backwards compatibility */
791 us->timer_type = TIMER_nanosec;
792 us->len_chanlist = s->len_chanlist;
793 us->maxdata = s->maxdata;
794 if (s->range_table) {
796 (i << 24) | (0 << 16) | (s->range_table->length);
798 us->range_type = 0; /* XXX */
800 us->flags = s->flags;
803 us->subd_flags |= SDF_BUSY;
805 us->subd_flags |= SDF_BUSY_OWNER;
807 us->subd_flags |= SDF_LOCKED;
809 us->subd_flags |= SDF_LOCK_OWNER;
810 if (!s->maxdata && s->maxdata_list)
811 us->subd_flags |= SDF_MAXDATA;
813 us->subd_flags |= SDF_FLAGS;
814 if (s->range_table_list)
815 us->subd_flags |= SDF_RANGETYPE;
817 us->subd_flags |= SDF_CMD;
819 if (s->insn_bits != &insn_inval)
820 us->insn_bits_support = COMEDI_SUPPORTED;
822 us->insn_bits_support = COMEDI_UNSUPPORTED;
824 us->settling_time_0 = s->settling_time_0;
827 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
831 return ret ? -EFAULT : 0;
839 pointer to chaninfo structure
842 chaninfo structure at arg
845 arrays at elements of chaninfo structure
848 static int do_chaninfo_ioctl(struct comedi_device *dev,
849 struct comedi_chaninfo __user *arg)
851 struct comedi_subdevice *s;
852 struct comedi_chaninfo it;
854 if (copy_from_user(&it, arg, sizeof(it)))
857 if (it.subdev >= dev->n_subdevices)
859 s = &dev->subdevices[it.subdev];
861 if (it.maxdata_list) {
862 if (s->maxdata || !s->maxdata_list)
864 if (copy_to_user(it.maxdata_list, s->maxdata_list,
865 s->n_chan * sizeof(unsigned int)))
872 if (copy_to_user(it.flaglist, s->flaglist,
873 s->n_chan * sizeof(unsigned int)))
880 if (!s->range_table_list)
882 for (i = 0; i < s->n_chan; i++) {
885 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
886 (s->range_table_list[i]->length);
887 if (put_user(x, it.rangelist + i))
891 if (copy_to_user(it.rangelist, s->range_type_list,
892 s->n_chan * sizeof(unsigned int)))
902 buffer information ioctl
905 pointer to bufinfo structure
911 modified bufinfo at arg
914 static int do_bufinfo_ioctl(struct comedi_device *dev,
915 struct comedi_bufinfo __user *arg, void *file)
917 struct comedi_bufinfo bi;
918 struct comedi_subdevice *s;
919 struct comedi_async *async;
921 if (copy_from_user(&bi, arg, sizeof(bi)))
924 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
927 s = &dev->subdevices[bi.subdevice];
929 if (s->lock && s->lock != file)
935 DPRINTK("subdevice does not have async capability\n");
936 bi.buf_write_ptr = 0;
938 bi.buf_write_count = 0;
939 bi.buf_read_count = 0;
941 bi.bytes_written = 0;
946 bi.bytes_written = 0;
947 goto copyback_position;
952 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
953 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
954 comedi_buf_read_free(async, bi.bytes_read);
956 if (comedi_is_subdevice_idle(s) &&
957 async->buf_write_count == async->buf_read_count) {
958 do_become_nonbusy(dev, s);
962 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
964 comedi_buf_write_alloc(async, bi.bytes_written);
965 comedi_buf_write_free(async, bi.bytes_written);
969 bi.buf_write_count = async->buf_write_count;
970 bi.buf_write_ptr = async->buf_write_ptr;
971 bi.buf_read_count = async->buf_read_count;
972 bi.buf_read_ptr = async->buf_read_ptr;
975 if (copy_to_user(arg, &bi, sizeof(bi)))
981 static int check_insn_config_length(struct comedi_insn *insn,
988 case INSN_CONFIG_DIO_OUTPUT:
989 case INSN_CONFIG_DIO_INPUT:
990 case INSN_CONFIG_DISARM:
991 case INSN_CONFIG_RESET:
995 case INSN_CONFIG_ARM:
996 case INSN_CONFIG_DIO_QUERY:
997 case INSN_CONFIG_BLOCK_SIZE:
998 case INSN_CONFIG_FILTER:
999 case INSN_CONFIG_SERIAL_CLOCK:
1000 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1001 case INSN_CONFIG_ALT_SOURCE:
1002 case INSN_CONFIG_SET_COUNTER_MODE:
1003 case INSN_CONFIG_8254_READ_STATUS:
1004 case INSN_CONFIG_SET_ROUTING:
1005 case INSN_CONFIG_GET_ROUTING:
1006 case INSN_CONFIG_GET_PWM_STATUS:
1007 case INSN_CONFIG_PWM_SET_PERIOD:
1008 case INSN_CONFIG_PWM_GET_PERIOD:
1012 case INSN_CONFIG_SET_GATE_SRC:
1013 case INSN_CONFIG_GET_GATE_SRC:
1014 case INSN_CONFIG_SET_CLOCK_SRC:
1015 case INSN_CONFIG_GET_CLOCK_SRC:
1016 case INSN_CONFIG_SET_OTHER_SRC:
1017 case INSN_CONFIG_GET_COUNTER_STATUS:
1018 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1019 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1020 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1024 case INSN_CONFIG_PWM_OUTPUT:
1025 case INSN_CONFIG_ANALOG_TRIG:
1029 case INSN_CONFIG_DIGITAL_TRIG:
1033 /* by default we allow the insn since we don't have checks for
1034 * all possible cases yet */
1036 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1038 pr_warn("comedi: Add a check to %s in %s.\n",
1039 __func__, __FILE__);
1040 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1046 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1047 unsigned int *data, void *file)
1049 struct comedi_subdevice *s;
1053 if (insn->insn & INSN_MASK_SPECIAL) {
1054 /* a non-subdevice instruction */
1056 switch (insn->insn) {
1066 do_gettimeofday(&tv);
1067 data[0] = tv.tv_sec;
1068 data[1] = tv.tv_usec;
1074 if (insn->n != 1 || data[0] >= 100000) {
1078 udelay(data[0] / 1000);
1086 if (insn->subdev >= dev->n_subdevices) {
1087 DPRINTK("%d not usable subdevice\n",
1092 s = &dev->subdevices[insn->subdev];
1094 DPRINTK("no async\n");
1098 if (!s->async->inttrig) {
1099 DPRINTK("no inttrig\n");
1103 ret = s->async->inttrig(dev, s, data[0]);
1108 DPRINTK("invalid insn\n");
1113 /* a subdevice instruction */
1114 unsigned int maxdata;
1116 if (insn->subdev >= dev->n_subdevices) {
1117 DPRINTK("subdevice %d out of range\n", insn->subdev);
1121 s = &dev->subdevices[insn->subdev];
1123 if (s->type == COMEDI_SUBD_UNUSED) {
1124 DPRINTK("%d not usable subdevice\n", insn->subdev);
1129 /* are we locked? (ioctl lock) */
1130 if (s->lock && s->lock != file) {
1131 DPRINTK("device locked\n");
1136 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1139 DPRINTK("bad chanspec\n");
1147 /* This looks arbitrary. It is. */
1148 s->busy = &parse_insn;
1149 switch (insn->insn) {
1151 ret = s->insn_read(dev, s, insn, data);
1154 maxdata = s->maxdata_list
1155 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1157 for (i = 0; i < insn->n; ++i) {
1158 if (data[i] > maxdata) {
1160 DPRINTK("bad data value(s)\n");
1165 ret = s->insn_write(dev, s, insn, data);
1171 /* Most drivers ignore the base channel in
1172 * insn->chanspec. Fix this here if
1173 * the subdevice has <= 32 channels. */
1175 unsigned int orig_mask;
1177 orig_mask = data[0];
1178 if (s->n_chan <= 32) {
1179 shift = CR_CHAN(insn->chanspec);
1187 ret = s->insn_bits(dev, s, insn, data);
1188 data[0] = orig_mask;
1194 ret = check_insn_config_length(insn, data);
1197 ret = s->insn_config(dev, s, insn, data);
1213 * synchronous instructions
1216 * pointer to sync cmd structure
1219 * sync cmd struct at arg
1226 /* arbitrary limits */
1227 #define MAX_SAMPLES 256
1228 static int do_insnlist_ioctl(struct comedi_device *dev,
1229 struct comedi_insnlist __user *arg, void *file)
1231 struct comedi_insnlist insnlist;
1232 struct comedi_insn *insns = NULL;
1233 unsigned int *data = NULL;
1237 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1240 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1242 DPRINTK("kmalloc failed\n");
1247 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1249 DPRINTK("kmalloc failed\n");
1254 if (copy_from_user(insns, insnlist.insns,
1255 sizeof(*insns) * insnlist.n_insns)) {
1256 DPRINTK("copy_from_user failed\n");
1261 for (i = 0; i < insnlist.n_insns; i++) {
1262 if (insns[i].n > MAX_SAMPLES) {
1263 DPRINTK("number of samples too large\n");
1267 if (insns[i].insn & INSN_MASK_WRITE) {
1268 if (copy_from_user(data, insns[i].data,
1269 insns[i].n * sizeof(unsigned int))) {
1270 DPRINTK("copy_from_user failed\n");
1275 ret = parse_insn(dev, insns + i, data, file);
1278 if (insns[i].insn & INSN_MASK_READ) {
1279 if (copy_to_user(insns[i].data, data,
1280 insns[i].n * sizeof(unsigned int))) {
1281 DPRINTK("copy_to_user failed\n");
1301 * synchronous instructions
1307 * struct comedi_insn struct at arg
1313 static int do_insn_ioctl(struct comedi_device *dev,
1314 struct comedi_insn __user *arg, void *file)
1316 struct comedi_insn insn;
1317 unsigned int *data = NULL;
1320 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1326 if (copy_from_user(&insn, arg, sizeof(insn))) {
1331 /* This is where the behavior of insn and insnlist deviate. */
1332 if (insn.n > MAX_SAMPLES)
1333 insn.n = MAX_SAMPLES;
1334 if (insn.insn & INSN_MASK_WRITE) {
1335 if (copy_from_user(data,
1337 insn.n * sizeof(unsigned int))) {
1342 ret = parse_insn(dev, &insn, data, file);
1345 if (insn.insn & INSN_MASK_READ) {
1346 if (copy_to_user(insn.data,
1348 insn.n * sizeof(unsigned int))) {
1361 static int do_cmd_ioctl(struct comedi_device *dev,
1362 struct comedi_cmd __user *arg, void *file)
1364 struct comedi_cmd cmd;
1365 struct comedi_subdevice *s;
1366 struct comedi_async *async;
1368 unsigned int __user *user_chanlist;
1370 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1371 DPRINTK("bad cmd address\n");
1374 /* save user's chanlist pointer so it can be restored later */
1375 user_chanlist = (unsigned int __user *)cmd.chanlist;
1377 if (cmd.subdev >= dev->n_subdevices) {
1378 DPRINTK("%d no such subdevice\n", cmd.subdev);
1382 s = &dev->subdevices[cmd.subdev];
1385 if (s->type == COMEDI_SUBD_UNUSED) {
1386 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1390 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1391 DPRINTK("subdevice %i does not support commands\n",
1396 /* are we locked? (ioctl lock) */
1397 if (s->lock && s->lock != file) {
1398 DPRINTK("subdevice locked\n");
1404 DPRINTK("subdevice busy\n");
1409 /* make sure channel/gain list isn't too long */
1410 if (cmd.chanlist_len > s->len_chanlist) {
1411 DPRINTK("channel/gain list too long %u > %d\n",
1412 cmd.chanlist_len, s->len_chanlist);
1417 /* make sure channel/gain list isn't too short */
1418 if (cmd.chanlist_len < 1) {
1419 DPRINTK("channel/gain list too short %u < 1\n",
1426 async->cmd.data = NULL;
1427 /* load channel/gain list */
1428 async->cmd.chanlist =
1429 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1430 if (!async->cmd.chanlist) {
1431 DPRINTK("allocation failed\n");
1436 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1437 async->cmd.chanlist_len * sizeof(int))) {
1438 DPRINTK("fault reading chanlist\n");
1443 /* make sure each element in channel/gain list is valid */
1444 ret = comedi_check_chanlist(s,
1445 async->cmd.chanlist_len,
1446 async->cmd.chanlist);
1448 DPRINTK("bad chanlist\n");
1452 ret = s->do_cmdtest(dev, s, &async->cmd);
1454 if (async->cmd.flags & TRIG_BOGUS || ret) {
1455 DPRINTK("test returned %d\n", ret);
1457 /* restore chanlist pointer before copying back */
1458 cmd.chanlist = (unsigned int __force *)user_chanlist;
1460 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1461 DPRINTK("fault writing cmd\n");
1469 if (!async->prealloc_bufsz) {
1471 DPRINTK("no buffer (?)\n");
1475 comedi_buf_reset(async);
1478 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1480 if (async->cmd.flags & TRIG_WAKE_EOS)
1481 async->cb_mask |= COMEDI_CB_EOS;
1483 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1485 ret = s->do_cmd(dev, s);
1490 do_become_nonbusy(dev, s);
1497 command testing ioctl
1500 pointer to cmd structure
1503 cmd structure at arg
1507 modified cmd structure at arg
1510 static int do_cmdtest_ioctl(struct comedi_device *dev,
1511 struct comedi_cmd __user *arg, void *file)
1513 struct comedi_cmd cmd;
1514 struct comedi_subdevice *s;
1516 unsigned int *chanlist = NULL;
1517 unsigned int __user *user_chanlist;
1519 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1520 DPRINTK("bad cmd address\n");
1523 /* save user's chanlist pointer so it can be restored later */
1524 user_chanlist = (unsigned int __user *)cmd.chanlist;
1526 if (cmd.subdev >= dev->n_subdevices) {
1527 DPRINTK("%d no such subdevice\n", cmd.subdev);
1531 s = &dev->subdevices[cmd.subdev];
1532 if (s->type == COMEDI_SUBD_UNUSED) {
1533 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1537 if (!s->do_cmd || !s->do_cmdtest) {
1538 DPRINTK("subdevice %i does not support commands\n",
1543 /* make sure channel/gain list isn't too long */
1544 if (cmd.chanlist_len > s->len_chanlist) {
1545 DPRINTK("channel/gain list too long %d > %d\n",
1546 cmd.chanlist_len, s->len_chanlist);
1551 /* load channel/gain list */
1554 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1556 DPRINTK("allocation failed\n");
1561 if (copy_from_user(chanlist, user_chanlist,
1562 cmd.chanlist_len * sizeof(int))) {
1563 DPRINTK("fault reading chanlist\n");
1568 /* make sure each element in channel/gain list is valid */
1569 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1571 DPRINTK("bad chanlist\n");
1575 cmd.chanlist = chanlist;
1578 ret = s->do_cmdtest(dev, s, &cmd);
1580 /* restore chanlist pointer before copying back */
1581 cmd.chanlist = (unsigned int __force *)user_chanlist;
1583 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1584 DPRINTK("bad cmd address\n");
1609 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1613 unsigned long flags;
1614 struct comedi_subdevice *s;
1616 if (arg >= dev->n_subdevices)
1618 s = &dev->subdevices[arg];
1620 spin_lock_irqsave(&s->spin_lock, flags);
1621 if (s->busy || s->lock)
1625 spin_unlock_irqrestore(&s->spin_lock, flags);
1632 ret = s->lock_f(dev, s);
1651 This function isn't protected by the semaphore, since
1652 we already own the lock.
1654 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1657 struct comedi_subdevice *s;
1659 if (arg >= dev->n_subdevices)
1661 s = &dev->subdevices[arg];
1666 if (s->lock && s->lock != file)
1669 if (s->lock == file) {
1683 cancel acquisition ioctl
1695 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1698 struct comedi_subdevice *s;
1700 if (arg >= dev->n_subdevices)
1702 s = &dev->subdevices[arg];
1703 if (s->async == NULL)
1706 if (s->lock && s->lock != file)
1712 if (s->busy != file)
1715 return do_cancel(dev, s);
1720 instructs driver to synchronize buffers
1732 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1735 struct comedi_subdevice *s;
1737 if (arg >= dev->n_subdevices)
1739 s = &dev->subdevices[arg];
1741 if (s->lock && s->lock != file)
1747 if (s->busy != file)
1751 return s->poll(dev, s);
1756 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1759 const unsigned minor = iminor(file_inode(file));
1760 struct comedi_device *dev = comedi_dev_from_minor(minor);
1766 mutex_lock(&dev->mutex);
1768 /* Device config is special, because it must work on
1769 * an unconfigured device. */
1770 if (cmd == COMEDI_DEVCONFIG) {
1771 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1772 /* Device config not appropriate on non-board minors. */
1776 rc = do_devconfig_ioctl(dev,
1777 (struct comedi_devconfig __user *)arg);
1780 dev->minor >= comedi_num_legacy_minors) {
1781 /* Successfully unconfigured a dynamically
1782 * allocated device. Try and remove it. */
1783 if (comedi_clear_board_dev(dev)) {
1784 mutex_unlock(&dev->mutex);
1785 comedi_free_board_dev(dev);
1793 if (!dev->attached) {
1794 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1800 case COMEDI_BUFCONFIG:
1801 rc = do_bufconfig_ioctl(dev,
1802 (struct comedi_bufconfig __user *)arg);
1804 case COMEDI_DEVINFO:
1805 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1808 case COMEDI_SUBDINFO:
1809 rc = do_subdinfo_ioctl(dev,
1810 (struct comedi_subdinfo __user *)arg,
1813 case COMEDI_CHANINFO:
1814 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1816 case COMEDI_RANGEINFO:
1817 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1819 case COMEDI_BUFINFO:
1820 rc = do_bufinfo_ioctl(dev,
1821 (struct comedi_bufinfo __user *)arg,
1825 rc = do_lock_ioctl(dev, arg, file);
1828 rc = do_unlock_ioctl(dev, arg, file);
1831 rc = do_cancel_ioctl(dev, arg, file);
1834 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1836 case COMEDI_CMDTEST:
1837 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1840 case COMEDI_INSNLIST:
1841 rc = do_insnlist_ioctl(dev,
1842 (struct comedi_insnlist __user *)arg,
1846 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1850 rc = do_poll_ioctl(dev, arg, file);
1858 mutex_unlock(&dev->mutex);
1862 static void comedi_vm_open(struct vm_area_struct *area)
1864 struct comedi_async *async;
1865 struct comedi_device *dev;
1867 async = area->vm_private_data;
1868 dev = async->subdevice->device;
1870 mutex_lock(&dev->mutex);
1871 async->mmap_count++;
1872 mutex_unlock(&dev->mutex);
1875 static void comedi_vm_close(struct vm_area_struct *area)
1877 struct comedi_async *async;
1878 struct comedi_device *dev;
1880 async = area->vm_private_data;
1881 dev = async->subdevice->device;
1883 mutex_lock(&dev->mutex);
1884 async->mmap_count--;
1885 mutex_unlock(&dev->mutex);
1888 static struct vm_operations_struct comedi_vm_ops = {
1889 .open = comedi_vm_open,
1890 .close = comedi_vm_close,
1893 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1895 const unsigned minor = iminor(file_inode(file));
1896 struct comedi_device *dev = comedi_dev_from_minor(minor);
1897 struct comedi_subdevice *s;
1898 struct comedi_async *async;
1899 unsigned long start = vma->vm_start;
1908 mutex_lock(&dev->mutex);
1910 if (!dev->attached) {
1911 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1916 if (vma->vm_flags & VM_WRITE)
1917 s = comedi_write_subdevice(dev, minor);
1919 s = comedi_read_subdevice(dev, minor);
1931 if (vma->vm_pgoff != 0) {
1932 DPRINTK("comedi: mmap() offset must be 0.\n");
1937 size = vma->vm_end - vma->vm_start;
1938 if (size > async->prealloc_bufsz) {
1942 if (size & (~PAGE_MASK)) {
1947 n_pages = size >> PAGE_SHIFT;
1948 for (i = 0; i < n_pages; ++i) {
1949 struct comedi_buf_page *buf = &async->buf_page_list[i];
1951 if (remap_pfn_range(vma, start,
1952 page_to_pfn(virt_to_page(buf->virt_addr)),
1953 PAGE_SIZE, PAGE_SHARED)) {
1960 vma->vm_ops = &comedi_vm_ops;
1961 vma->vm_private_data = async;
1963 async->mmap_count++;
1967 mutex_unlock(&dev->mutex);
1971 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1973 unsigned int mask = 0;
1974 const unsigned minor = iminor(file_inode(file));
1975 struct comedi_device *dev = comedi_dev_from_minor(minor);
1976 struct comedi_subdevice *s;
1981 mutex_lock(&dev->mutex);
1983 if (!dev->attached) {
1984 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1988 s = comedi_read_subdevice(dev, minor);
1989 if (s && s->async) {
1990 poll_wait(file, &s->async->wait_head, wait);
1991 if (!s->busy || !comedi_is_subdevice_running(s) ||
1992 comedi_buf_read_n_available(s->async) > 0)
1993 mask |= POLLIN | POLLRDNORM;
1996 s = comedi_write_subdevice(dev, minor);
1997 if (s && s->async) {
1998 unsigned int bps = bytes_per_sample(s->async->subdevice);
2000 poll_wait(file, &s->async->wait_head, wait);
2001 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2002 if (!s->busy || !comedi_is_subdevice_running(s) ||
2003 comedi_buf_write_n_allocated(s->async) >= bps)
2004 mask |= POLLOUT | POLLWRNORM;
2008 mutex_unlock(&dev->mutex);
2012 static ssize_t comedi_write(struct file *file, const char __user *buf,
2013 size_t nbytes, loff_t *offset)
2015 struct comedi_subdevice *s;
2016 struct comedi_async *async;
2017 int n, m, count = 0, retval = 0;
2018 DECLARE_WAITQUEUE(wait, current);
2019 const unsigned minor = iminor(file_inode(file));
2020 struct comedi_device *dev = comedi_dev_from_minor(minor);
2025 if (!dev->attached) {
2026 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2030 s = comedi_write_subdevice(dev, minor);
2031 if (!s || !s->async)
2036 if (!s->busy || !nbytes)
2038 if (s->busy != file)
2041 add_wait_queue(&async->wait_head, &wait);
2042 while (nbytes > 0 && !retval) {
2043 set_current_state(TASK_INTERRUPTIBLE);
2045 if (!comedi_is_subdevice_running(s)) {
2047 if (comedi_is_subdevice_in_error(s))
2051 do_become_nonbusy(dev, s);
2059 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2060 m = async->prealloc_bufsz - async->buf_write_ptr;
2061 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2062 if (m > comedi_buf_write_n_allocated(async))
2063 m = comedi_buf_write_n_allocated(async);
2068 if (file->f_flags & O_NONBLOCK) {
2073 if (signal_pending(current)) {
2074 retval = -ERESTARTSYS;
2079 if (s->busy != file) {
2086 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2092 comedi_buf_write_free(async, n);
2098 break; /* makes device work like a pipe */
2100 set_current_state(TASK_RUNNING);
2101 remove_wait_queue(&async->wait_head, &wait);
2103 return count ? count : retval;
2106 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2109 struct comedi_subdevice *s;
2110 struct comedi_async *async;
2111 int n, m, count = 0, retval = 0;
2112 DECLARE_WAITQUEUE(wait, current);
2113 const unsigned minor = iminor(file_inode(file));
2114 struct comedi_device *dev = comedi_dev_from_minor(minor);
2119 if (!dev->attached) {
2120 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2124 s = comedi_read_subdevice(dev, minor);
2125 if (!s || !s->async)
2129 if (!s->busy || !nbytes)
2131 if (s->busy != file)
2134 add_wait_queue(&async->wait_head, &wait);
2135 while (nbytes > 0 && !retval) {
2136 set_current_state(TASK_INTERRUPTIBLE);
2140 m = comedi_buf_read_n_available(async);
2141 /* printk("%d available\n",m); */
2142 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2143 m = async->prealloc_bufsz - async->buf_read_ptr;
2144 /* printk("%d contiguous\n",m); */
2149 if (!comedi_is_subdevice_running(s)) {
2150 do_become_nonbusy(dev, s);
2151 if (comedi_is_subdevice_in_error(s))
2157 if (file->f_flags & O_NONBLOCK) {
2162 if (signal_pending(current)) {
2163 retval = -ERESTARTSYS;
2170 if (s->busy != file) {
2176 m = copy_to_user(buf, async->prealloc_buf +
2177 async->buf_read_ptr, n);
2183 comedi_buf_read_alloc(async, n);
2184 comedi_buf_read_free(async, n);
2190 break; /* makes device work like a pipe */
2192 if (comedi_is_subdevice_idle(s) &&
2193 async->buf_read_count - async->buf_write_count == 0) {
2194 do_become_nonbusy(dev, s);
2196 set_current_state(TASK_RUNNING);
2197 remove_wait_queue(&async->wait_head, &wait);
2199 return count ? count : retval;
2202 static int comedi_open(struct inode *inode, struct file *file)
2204 const unsigned minor = iminor(inode);
2205 struct comedi_device *dev = comedi_dev_from_minor(minor);
2208 DPRINTK("invalid minor number\n");
2212 /* This is slightly hacky, but we want module autoloading
2214 * case: user opens device, attached -> ok
2215 * case: user opens device, unattached, !in_request_module -> autoload
2216 * case: user opens device, unattached, in_request_module -> fail
2217 * case: root opens device, attached -> ok
2218 * case: root opens device, unattached, in_request_module -> ok
2219 * (typically called from modprobe)
2220 * case: root opens device, unattached, !in_request_module -> autoload
2222 * The last could be changed to "-> ok", which would deny root
2225 mutex_lock(&dev->mutex);
2228 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2229 DPRINTK("in request module\n");
2230 mutex_unlock(&dev->mutex);
2233 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2236 dev->in_request_module = true;
2239 mutex_unlock(&dev->mutex);
2240 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2241 mutex_lock(&dev->mutex);
2244 dev->in_request_module = false;
2246 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2247 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2248 mutex_unlock(&dev->mutex);
2252 __module_get(THIS_MODULE);
2254 if (dev->attached) {
2255 if (!try_module_get(dev->driver->module)) {
2256 module_put(THIS_MODULE);
2257 mutex_unlock(&dev->mutex);
2262 if (dev->attached && dev->use_count == 0 && dev->open) {
2263 int rc = dev->open(dev);
2265 module_put(dev->driver->module);
2266 module_put(THIS_MODULE);
2267 mutex_unlock(&dev->mutex);
2274 mutex_unlock(&dev->mutex);
2279 static int comedi_fasync(int fd, struct file *file, int on)
2281 const unsigned minor = iminor(file_inode(file));
2282 struct comedi_device *dev = comedi_dev_from_minor(minor);
2287 return fasync_helper(fd, file, on, &dev->async_queue);
2290 static int comedi_close(struct inode *inode, struct file *file)
2292 const unsigned minor = iminor(inode);
2293 struct comedi_device *dev = comedi_dev_from_minor(minor);
2294 struct comedi_subdevice *s = NULL;
2300 mutex_lock(&dev->mutex);
2302 if (dev->subdevices) {
2303 for (i = 0; i < dev->n_subdevices; i++) {
2304 s = &dev->subdevices[i];
2306 if (s->busy == file)
2308 if (s->lock == file)
2312 if (dev->attached && dev->use_count == 1 && dev->close)
2315 module_put(THIS_MODULE);
2317 module_put(dev->driver->module);
2321 mutex_unlock(&dev->mutex);
2323 if (file->f_flags & FASYNC)
2324 comedi_fasync(-1, file, 0);
2329 static const struct file_operations comedi_fops = {
2330 .owner = THIS_MODULE,
2331 .unlocked_ioctl = comedi_unlocked_ioctl,
2332 .compat_ioctl = comedi_compat_ioctl,
2333 .open = comedi_open,
2334 .release = comedi_close,
2335 .read = comedi_read,
2336 .write = comedi_write,
2337 .mmap = comedi_mmap,
2338 .poll = comedi_poll,
2339 .fasync = comedi_fasync,
2340 .llseek = noop_llseek,
2343 void comedi_error(const struct comedi_device *dev, const char *s)
2345 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2347 EXPORT_SYMBOL_GPL(comedi_error);
2349 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2351 struct comedi_async *async = s->async;
2352 unsigned runflags = 0;
2353 unsigned runflags_mask = 0;
2355 /* DPRINTK("comedi_event 0x%x\n",mask); */
2357 if (!comedi_is_subdevice_running(s))
2361 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2362 COMEDI_CB_OVERFLOW)) {
2363 runflags_mask |= SRF_RUNNING;
2365 /* remember if an error event has occurred, so an error
2366 * can be returned the next time the user does a read() */
2367 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2368 runflags_mask |= SRF_ERROR;
2369 runflags |= SRF_ERROR;
2371 if (runflags_mask) {
2372 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2373 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2376 if (async->cb_mask & s->async->events) {
2377 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2378 wake_up_interruptible(&async->wait_head);
2379 if (s->subdev_flags & SDF_CMD_READ)
2380 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2381 if (s->subdev_flags & SDF_CMD_WRITE)
2382 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2385 async->cb_func(s->async->events, async->cb_arg);
2388 s->async->events = 0;
2390 EXPORT_SYMBOL_GPL(comedi_event);
2392 /* Note: the ->mutex is pre-locked on successful return */
2393 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2395 struct comedi_device *dev;
2396 struct device *csdev;
2399 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2401 return ERR_PTR(-ENOMEM);
2402 comedi_device_init(dev);
2403 comedi_set_hw_dev(dev, hardware_device);
2404 mutex_lock(&dev->mutex);
2405 mutex_lock(&comedi_board_minor_table_lock);
2406 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2407 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2408 if (comedi_board_minor_table[i] == NULL) {
2409 comedi_board_minor_table[i] = dev;
2413 mutex_unlock(&comedi_board_minor_table_lock);
2414 if (i == COMEDI_NUM_BOARD_MINORS) {
2415 mutex_unlock(&dev->mutex);
2416 comedi_device_cleanup(dev);
2418 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2419 return ERR_PTR(-EBUSY);
2422 csdev = device_create(comedi_class, hardware_device,
2423 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2425 dev->class_dev = csdev;
2427 /* Note: dev->mutex needs to be unlocked by the caller. */
2431 static void comedi_free_board_minor(unsigned minor)
2433 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2434 comedi_free_board_dev(comedi_clear_board_minor(minor));
2437 void comedi_release_hardware_device(struct device *hardware_device)
2440 struct comedi_device *dev;
2442 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2444 mutex_lock(&comedi_board_minor_table_lock);
2445 dev = comedi_board_minor_table[minor];
2446 if (dev && dev->hw_dev == hardware_device) {
2447 comedi_board_minor_table[minor] = NULL;
2448 mutex_unlock(&comedi_board_minor_table_lock);
2449 comedi_free_board_dev(dev);
2452 mutex_unlock(&comedi_board_minor_table_lock);
2456 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2458 struct comedi_device *dev = s->device;
2459 struct device *csdev;
2462 mutex_lock(&comedi_subdevice_minor_table_lock);
2463 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2464 if (comedi_subdevice_minor_table[i] == NULL) {
2465 comedi_subdevice_minor_table[i] = s;
2469 mutex_unlock(&comedi_subdevice_minor_table_lock);
2470 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2471 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2474 i += COMEDI_NUM_BOARD_MINORS;
2476 csdev = device_create(comedi_class, dev->class_dev,
2477 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2478 dev->minor, s->index);
2480 s->class_dev = csdev;
2485 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2494 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2495 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2497 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2498 mutex_lock(&comedi_subdevice_minor_table_lock);
2499 if (s == comedi_subdevice_minor_table[i])
2500 comedi_subdevice_minor_table[i] = NULL;
2501 mutex_unlock(&comedi_subdevice_minor_table_lock);
2503 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2504 s->class_dev = NULL;
2508 static void comedi_cleanup_board_minors(void)
2512 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2513 comedi_free_board_minor(i);
2516 static int __init comedi_init(void)
2521 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2523 if (comedi_num_legacy_minors < 0 ||
2524 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2525 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2526 COMEDI_NUM_BOARD_MINORS);
2530 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2531 COMEDI_NUM_MINORS, "comedi");
2534 cdev_init(&comedi_cdev, &comedi_fops);
2535 comedi_cdev.owner = THIS_MODULE;
2536 kobject_set_name(&comedi_cdev.kobj, "comedi");
2537 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2538 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2542 comedi_class = class_create(THIS_MODULE, "comedi");
2543 if (IS_ERR(comedi_class)) {
2544 pr_err("comedi: failed to create class\n");
2545 cdev_del(&comedi_cdev);
2546 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2548 return PTR_ERR(comedi_class);
2551 comedi_class->dev_attrs = comedi_dev_attrs;
2553 /* XXX requires /proc interface */
2556 /* create devices files for legacy/manual use */
2557 for (i = 0; i < comedi_num_legacy_minors; i++) {
2558 struct comedi_device *dev;
2559 dev = comedi_alloc_board_minor(NULL);
2561 comedi_cleanup_board_minors();
2562 cdev_del(&comedi_cdev);
2563 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2565 return PTR_ERR(dev);
2567 /* comedi_alloc_board_minor() locked the mutex */
2568 mutex_unlock(&dev->mutex);
2574 module_init(comedi_init);
2576 static void __exit comedi_cleanup(void)
2580 comedi_cleanup_board_minors();
2581 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2582 BUG_ON(comedi_board_minor_table[i]);
2583 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2584 BUG_ON(comedi_subdevice_minor_table[i]);
2586 class_destroy(comedi_class);
2587 cdev_del(&comedi_cdev);
2588 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2590 comedi_proc_cleanup();
2592 module_exit(comedi_cleanup);
2594 MODULE_AUTHOR("http://www.comedi.org");
2595 MODULE_DESCRIPTION("Comedi core module");
2596 MODULE_LICENSE("GPL");