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.
21 #include "comedi_compat32.h"
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/fcntl.h>
28 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/kmod.h>
32 #include <linux/poll.h>
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/vmalloc.h>
37 #include "comedidev.h"
38 #include <linux/cdev.h>
39 #include <linux/stat.h>
42 #include <linux/uaccess.h>
44 #include "comedi_internal.h"
46 #define COMEDI_NUM_MINORS 0x100
47 #define COMEDI_NUM_SUBDEVICE_MINORS \
48 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
50 #ifdef CONFIG_COMEDI_DEBUG
52 EXPORT_SYMBOL_GPL(comedi_debug);
53 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
54 MODULE_PARM_DESC(comedi_debug,
55 "enable comedi core and driver debugging if non-zero (default 0)"
59 static int comedi_num_legacy_minors;
60 module_param(comedi_num_legacy_minors, int, S_IRUGO);
61 MODULE_PARM_DESC(comedi_num_legacy_minors,
62 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
65 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
66 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
67 MODULE_PARM_DESC(comedi_default_buf_size_kb,
68 "default asynchronous buffer size in KiB (default "
69 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
71 unsigned int comedi_default_buf_maxsize_kb
72 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
73 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
74 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
75 "default maximum size of asynchronous buffer in KiB (default "
76 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
78 static DEFINE_MUTEX(comedi_board_minor_table_lock);
79 static struct comedi_device
80 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
82 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
83 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
84 static struct comedi_subdevice
85 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
87 static struct class *comedi_class;
88 static struct cdev comedi_cdev;
90 static void comedi_device_init(struct comedi_device *dev)
92 kref_init(&dev->refcount);
93 spin_lock_init(&dev->spinlock);
94 mutex_init(&dev->mutex);
95 init_rwsem(&dev->attach_lock);
99 static void comedi_dev_kref_release(struct kref *kref)
101 struct comedi_device *dev =
102 container_of(kref, struct comedi_device, refcount);
104 mutex_destroy(&dev->mutex);
108 int comedi_dev_put(struct comedi_device *dev)
111 return kref_put(&dev->refcount, comedi_dev_kref_release);
114 EXPORT_SYMBOL_GPL(comedi_dev_put);
116 static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
119 kref_get(&dev->refcount);
123 static void comedi_device_cleanup(struct comedi_device *dev)
125 struct module *driver_module = NULL;
129 mutex_lock(&dev->mutex);
131 driver_module = dev->driver->module;
132 comedi_device_detach(dev);
133 while (dev->use_count > 0) {
135 module_put(driver_module);
136 module_put(THIS_MODULE);
139 mutex_unlock(&dev->mutex);
142 static bool comedi_clear_board_dev(struct comedi_device *dev)
144 unsigned int i = dev->minor;
145 bool cleared = false;
147 mutex_lock(&comedi_board_minor_table_lock);
148 if (dev == comedi_board_minor_table[i]) {
149 comedi_board_minor_table[i] = NULL;
152 mutex_unlock(&comedi_board_minor_table_lock);
156 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
158 struct comedi_device *dev;
160 mutex_lock(&comedi_board_minor_table_lock);
161 dev = comedi_board_minor_table[minor];
162 comedi_board_minor_table[minor] = NULL;
163 mutex_unlock(&comedi_board_minor_table_lock);
167 static void comedi_free_board_dev(struct comedi_device *dev)
170 if (dev->class_dev) {
171 device_destroy(comedi_class,
172 MKDEV(COMEDI_MAJOR, dev->minor));
174 comedi_device_cleanup(dev);
179 static struct comedi_subdevice
180 *comedi_subdevice_from_minor(unsigned minor)
182 struct comedi_subdevice *s;
183 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
185 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
186 mutex_lock(&comedi_subdevice_minor_table_lock);
187 s = comedi_subdevice_minor_table[i];
188 mutex_unlock(&comedi_subdevice_minor_table_lock);
192 static struct comedi_device *comedi_dev_from_board_minor(unsigned minor)
194 struct comedi_device *dev;
196 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
197 mutex_lock(&comedi_board_minor_table_lock);
198 dev = comedi_board_minor_table[minor];
199 mutex_unlock(&comedi_board_minor_table_lock);
203 static struct comedi_device *comedi_dev_from_subdevice_minor(unsigned minor)
205 struct comedi_subdevice *s;
207 s = comedi_subdevice_from_minor(minor);
208 return s ? s->device : NULL;
211 struct comedi_device *comedi_dev_from_minor(unsigned minor)
213 if (minor < COMEDI_NUM_BOARD_MINORS)
214 return comedi_dev_from_board_minor(minor);
216 return comedi_dev_from_subdevice_minor(minor);
218 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
220 static struct comedi_device *comedi_dev_get_from_board_minor(unsigned minor)
222 struct comedi_device *dev;
224 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
225 mutex_lock(&comedi_board_minor_table_lock);
226 dev = comedi_dev_get(comedi_board_minor_table[minor]);
227 mutex_unlock(&comedi_board_minor_table_lock);
231 static struct comedi_device *comedi_dev_get_from_subdevice_minor(unsigned minor)
233 struct comedi_device *dev;
234 struct comedi_subdevice *s;
235 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
237 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
238 mutex_lock(&comedi_subdevice_minor_table_lock);
239 s = comedi_subdevice_minor_table[i];
240 dev = comedi_dev_get(s ? s->device : NULL);
241 mutex_unlock(&comedi_subdevice_minor_table_lock);
245 struct comedi_device *comedi_dev_get_from_minor(unsigned minor)
247 if (minor < COMEDI_NUM_BOARD_MINORS)
248 return comedi_dev_get_from_board_minor(minor);
250 return comedi_dev_get_from_subdevice_minor(minor);
252 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
254 static struct comedi_subdevice *
255 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
257 struct comedi_subdevice *s;
259 if (minor >= COMEDI_NUM_BOARD_MINORS) {
260 s = comedi_subdevice_from_minor(minor);
261 if (!s || s->device != dev)
263 if (s->subdev_flags & SDF_CMD_READ)
266 return dev->read_subdev;
269 static struct comedi_subdevice *
270 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
272 struct comedi_subdevice *s;
274 if (minor >= COMEDI_NUM_BOARD_MINORS) {
275 s = comedi_subdevice_from_minor(minor);
276 if (!s || s->device != dev)
278 if (s->subdev_flags & SDF_CMD_WRITE)
281 return dev->write_subdev;
284 static int resize_async_buffer(struct comedi_device *dev,
285 struct comedi_subdevice *s,
286 struct comedi_async *async, unsigned new_size)
290 if (new_size > async->max_bufsize)
294 DPRINTK("subdevice is busy, cannot resize buffer\n");
297 if (async->mmap_count) {
298 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
302 /* make sure buffer is an integral number of pages
304 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
306 retval = comedi_buf_alloc(dev, s, new_size);
311 retval = s->buf_change(dev, s, new_size);
316 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
317 dev->minor, s->index, async->prealloc_bufsz);
321 /* sysfs attribute files */
323 static ssize_t max_read_buffer_kb_show(struct device *csdev,
324 struct device_attribute *attr, char *buf)
326 unsigned int minor = MINOR(csdev->devt);
327 struct comedi_device *dev;
328 struct comedi_subdevice *s;
329 unsigned int size = 0;
331 dev = comedi_dev_get_from_minor(minor);
335 mutex_lock(&dev->mutex);
336 s = comedi_read_subdevice(dev, minor);
337 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
338 size = s->async->max_bufsize / 1024;
339 mutex_unlock(&dev->mutex);
342 return snprintf(buf, PAGE_SIZE, "%i\n", size);
345 static ssize_t max_read_buffer_kb_store(struct device *csdev,
346 struct device_attribute *attr,
347 const char *buf, size_t count)
349 unsigned int minor = MINOR(csdev->devt);
350 struct comedi_device *dev;
351 struct comedi_subdevice *s;
355 err = kstrtouint(buf, 10, &size);
358 if (size > (UINT_MAX / 1024))
362 dev = comedi_dev_get_from_minor(minor);
366 mutex_lock(&dev->mutex);
367 s = comedi_read_subdevice(dev, minor);
368 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
369 s->async->max_bufsize = size;
372 mutex_unlock(&dev->mutex);
375 return err ? err : count;
377 static DEVICE_ATTR_RW(max_read_buffer_kb);
379 static ssize_t read_buffer_kb_show(struct device *csdev,
380 struct device_attribute *attr, char *buf)
382 unsigned int minor = MINOR(csdev->devt);
383 struct comedi_device *dev;
384 struct comedi_subdevice *s;
385 unsigned int size = 0;
387 dev = comedi_dev_get_from_minor(minor);
391 mutex_lock(&dev->mutex);
392 s = comedi_read_subdevice(dev, minor);
393 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
394 size = s->async->prealloc_bufsz / 1024;
395 mutex_unlock(&dev->mutex);
398 return snprintf(buf, PAGE_SIZE, "%i\n", size);
401 static ssize_t read_buffer_kb_store(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_get_from_minor(minor);
422 mutex_lock(&dev->mutex);
423 s = comedi_read_subdevice(dev, minor);
424 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
425 err = resize_async_buffer(dev, s, s->async, size);
428 mutex_unlock(&dev->mutex);
431 return err ? err : count;
433 static DEVICE_ATTR_RW(read_buffer_kb);
435 static ssize_t max_write_buffer_kb_show(struct device *csdev,
436 struct device_attribute *attr,
439 unsigned int minor = MINOR(csdev->devt);
440 struct comedi_device *dev;
441 struct comedi_subdevice *s;
442 unsigned int size = 0;
444 dev = comedi_dev_get_from_minor(minor);
448 mutex_lock(&dev->mutex);
449 s = comedi_write_subdevice(dev, minor);
450 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
451 size = s->async->max_bufsize / 1024;
452 mutex_unlock(&dev->mutex);
455 return snprintf(buf, PAGE_SIZE, "%i\n", size);
458 static ssize_t max_write_buffer_kb_store(struct device *csdev,
459 struct device_attribute *attr,
460 const char *buf, size_t count)
462 unsigned int minor = MINOR(csdev->devt);
463 struct comedi_device *dev;
464 struct comedi_subdevice *s;
468 err = kstrtouint(buf, 10, &size);
471 if (size > (UINT_MAX / 1024))
475 dev = comedi_dev_get_from_minor(minor);
479 mutex_lock(&dev->mutex);
480 s = comedi_write_subdevice(dev, minor);
481 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
482 s->async->max_bufsize = size;
485 mutex_unlock(&dev->mutex);
488 return err ? err : count;
490 static DEVICE_ATTR_RW(max_write_buffer_kb);
492 static ssize_t write_buffer_kb_show(struct device *csdev,
493 struct device_attribute *attr, char *buf)
495 unsigned int minor = MINOR(csdev->devt);
496 struct comedi_device *dev;
497 struct comedi_subdevice *s;
498 unsigned int size = 0;
500 dev = comedi_dev_get_from_minor(minor);
504 mutex_lock(&dev->mutex);
505 s = comedi_write_subdevice(dev, minor);
506 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
507 size = s->async->prealloc_bufsz / 1024;
508 mutex_unlock(&dev->mutex);
511 return snprintf(buf, PAGE_SIZE, "%i\n", size);
514 static ssize_t write_buffer_kb_store(struct device *csdev,
515 struct device_attribute *attr,
516 const char *buf, size_t count)
518 unsigned int minor = MINOR(csdev->devt);
519 struct comedi_device *dev;
520 struct comedi_subdevice *s;
524 err = kstrtouint(buf, 10, &size);
527 if (size > (UINT_MAX / 1024))
531 dev = comedi_dev_get_from_minor(minor);
535 mutex_lock(&dev->mutex);
536 s = comedi_write_subdevice(dev, minor);
537 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
538 err = resize_async_buffer(dev, s, s->async, size);
541 mutex_unlock(&dev->mutex);
544 return err ? err : count;
546 static DEVICE_ATTR_RW(write_buffer_kb);
548 static struct attribute *comedi_dev_attrs[] = {
549 &dev_attr_max_read_buffer_kb.attr,
550 &dev_attr_read_buffer_kb.attr,
551 &dev_attr_max_write_buffer_kb.attr,
552 &dev_attr_write_buffer_kb.attr,
555 ATTRIBUTE_GROUPS(comedi_dev);
557 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
558 unsigned mask, unsigned bits)
562 spin_lock_irqsave(&s->spin_lock, flags);
563 s->runflags &= ~mask;
564 s->runflags |= (bits & mask);
565 spin_unlock_irqrestore(&s->spin_lock, flags);
568 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
573 spin_lock_irqsave(&s->spin_lock, flags);
574 runflags = s->runflags;
575 spin_unlock_irqrestore(&s->spin_lock, flags);
579 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
581 unsigned runflags = comedi_get_subdevice_runflags(s);
583 return (runflags & SRF_RUNNING) ? true : false;
585 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
587 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
589 unsigned runflags = comedi_get_subdevice_runflags(s);
591 return (runflags & SRF_ERROR) ? true : false;
594 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
596 unsigned runflags = comedi_get_subdevice_runflags(s);
598 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
602 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
603 * @s: comedi_subdevice struct
604 * @size: size of the memory to allocate
606 * This also sets the subdevice runflags to allow the core to automatically
607 * free the private data during the detach.
609 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
611 s->private = kzalloc(size, GFP_KERNEL);
613 s->runflags |= SRF_FREE_SPRIV;
616 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
619 This function restores a subdevice to an idle state.
621 static void do_become_nonbusy(struct comedi_device *dev,
622 struct comedi_subdevice *s)
624 struct comedi_async *async = s->async;
626 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
628 comedi_buf_reset(async);
629 async->inttrig = NULL;
630 kfree(async->cmd.chanlist);
631 async->cmd.chanlist = NULL;
633 wake_up_interruptible_all(&s->async->wait_head);
635 dev_err(dev->class_dev,
636 "BUG: (?) do_become_nonbusy called with async=NULL\n");
641 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
645 if (comedi_is_subdevice_running(s) && s->cancel)
646 ret = s->cancel(dev, s);
648 do_become_nonbusy(dev, s);
653 void comedi_device_cancel_all(struct comedi_device *dev)
655 struct comedi_subdevice *s;
661 for (i = 0; i < dev->n_subdevices; i++) {
662 s = &dev->subdevices[i];
668 static int is_device_busy(struct comedi_device *dev)
670 struct comedi_subdevice *s;
676 for (i = 0; i < dev->n_subdevices; i++) {
677 s = &dev->subdevices[i];
680 if (s->async && s->async->mmap_count)
692 pointer to devconfig structure
695 devconfig structure at arg
700 static int do_devconfig_ioctl(struct comedi_device *dev,
701 struct comedi_devconfig __user *arg)
703 struct comedi_devconfig it;
705 if (!capable(CAP_SYS_ADMIN))
709 if (is_device_busy(dev))
712 struct module *driver_module = dev->driver->module;
713 comedi_device_detach(dev);
714 module_put(driver_module);
719 if (copy_from_user(&it, arg, sizeof(it)))
722 it.board_name[COMEDI_NAMELEN - 1] = 0;
724 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
725 dev_warn(dev->class_dev,
726 "comedi_config --init_data is deprecated\n");
730 if (dev->minor >= comedi_num_legacy_minors)
731 /* don't re-use dynamically allocated comedi devices */
734 /* This increments the driver module count on success. */
735 return comedi_device_attach(dev, &it);
740 buffer configuration ioctl
743 pointer to bufconfig structure
749 modified bufconfig at arg
752 static int do_bufconfig_ioctl(struct comedi_device *dev,
753 struct comedi_bufconfig __user *arg)
755 struct comedi_bufconfig bc;
756 struct comedi_async *async;
757 struct comedi_subdevice *s;
760 if (copy_from_user(&bc, arg, sizeof(bc)))
763 if (bc.subdevice >= dev->n_subdevices)
766 s = &dev->subdevices[bc.subdevice];
770 DPRINTK("subdevice does not have async capability\n");
776 if (bc.maximum_size) {
777 if (!capable(CAP_SYS_ADMIN))
780 async->max_bufsize = bc.maximum_size;
784 retval = resize_async_buffer(dev, s, async, bc.size);
789 bc.size = async->prealloc_bufsz;
790 bc.maximum_size = async->max_bufsize;
793 if (copy_to_user(arg, &bc, sizeof(bc)))
804 pointer to devinfo structure
813 static int do_devinfo_ioctl(struct comedi_device *dev,
814 struct comedi_devinfo __user *arg,
817 const unsigned minor = iminor(file_inode(file));
818 struct comedi_subdevice *s;
819 struct comedi_devinfo devinfo;
821 memset(&devinfo, 0, sizeof(devinfo));
823 /* fill devinfo structure */
824 devinfo.version_code = COMEDI_VERSION_CODE;
825 devinfo.n_subdevs = dev->n_subdevices;
826 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
827 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
829 s = comedi_read_subdevice(dev, minor);
831 devinfo.read_subdevice = s->index;
833 devinfo.read_subdevice = -1;
835 s = comedi_write_subdevice(dev, minor);
837 devinfo.write_subdevice = s->index;
839 devinfo.write_subdevice = -1;
841 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
852 pointer to array of subdevice info structures
858 array of subdevice info structures at arg
861 static int do_subdinfo_ioctl(struct comedi_device *dev,
862 struct comedi_subdinfo __user *arg, void *file)
865 struct comedi_subdinfo *tmp, *us;
866 struct comedi_subdevice *s;
868 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
872 /* fill subdinfo structs */
873 for (i = 0; i < dev->n_subdevices; i++) {
874 s = &dev->subdevices[i];
878 us->n_chan = s->n_chan;
879 us->subd_flags = s->subdev_flags;
880 if (comedi_is_subdevice_running(s))
881 us->subd_flags |= SDF_RUNNING;
882 #define TIMER_nanosec 5 /* backwards compatibility */
883 us->timer_type = TIMER_nanosec;
884 us->len_chanlist = s->len_chanlist;
885 us->maxdata = s->maxdata;
886 if (s->range_table) {
888 (i << 24) | (0 << 16) | (s->range_table->length);
890 us->range_type = 0; /* XXX */
894 us->subd_flags |= SDF_BUSY;
896 us->subd_flags |= SDF_BUSY_OWNER;
898 us->subd_flags |= SDF_LOCKED;
900 us->subd_flags |= SDF_LOCK_OWNER;
901 if (!s->maxdata && s->maxdata_list)
902 us->subd_flags |= SDF_MAXDATA;
903 if (s->range_table_list)
904 us->subd_flags |= SDF_RANGETYPE;
906 us->subd_flags |= SDF_CMD;
908 if (s->insn_bits != &insn_inval)
909 us->insn_bits_support = COMEDI_SUPPORTED;
911 us->insn_bits_support = COMEDI_UNSUPPORTED;
914 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
918 return ret ? -EFAULT : 0;
926 pointer to chaninfo structure
929 chaninfo structure at arg
932 arrays at elements of chaninfo structure
935 static int do_chaninfo_ioctl(struct comedi_device *dev,
936 struct comedi_chaninfo __user *arg)
938 struct comedi_subdevice *s;
939 struct comedi_chaninfo it;
941 if (copy_from_user(&it, arg, sizeof(it)))
944 if (it.subdev >= dev->n_subdevices)
946 s = &dev->subdevices[it.subdev];
948 if (it.maxdata_list) {
949 if (s->maxdata || !s->maxdata_list)
951 if (copy_to_user(it.maxdata_list, s->maxdata_list,
952 s->n_chan * sizeof(unsigned int)))
957 return -EINVAL; /* flaglist not supported */
962 if (!s->range_table_list)
964 for (i = 0; i < s->n_chan; i++) {
967 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
968 (s->range_table_list[i]->length);
969 if (put_user(x, it.rangelist + i))
973 if (copy_to_user(it.rangelist, s->range_type_list,
974 s->n_chan * sizeof(unsigned int)))
984 buffer information ioctl
987 pointer to bufinfo structure
993 modified bufinfo at arg
996 static int do_bufinfo_ioctl(struct comedi_device *dev,
997 struct comedi_bufinfo __user *arg, void *file)
999 struct comedi_bufinfo bi;
1000 struct comedi_subdevice *s;
1001 struct comedi_async *async;
1003 if (copy_from_user(&bi, arg, sizeof(bi)))
1006 if (bi.subdevice >= dev->n_subdevices)
1009 s = &dev->subdevices[bi.subdevice];
1011 if (s->lock && s->lock != file)
1017 DPRINTK("subdevice does not have async capability\n");
1018 bi.buf_write_ptr = 0;
1019 bi.buf_read_ptr = 0;
1020 bi.buf_write_count = 0;
1021 bi.buf_read_count = 0;
1023 bi.bytes_written = 0;
1028 bi.bytes_written = 0;
1029 goto copyback_position;
1031 if (s->busy != file)
1034 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
1035 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
1036 comedi_buf_read_free(async, bi.bytes_read);
1038 if (comedi_is_subdevice_idle(s) &&
1039 async->buf_write_count == async->buf_read_count) {
1040 do_become_nonbusy(dev, s);
1044 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
1046 comedi_buf_write_alloc(async, bi.bytes_written);
1047 comedi_buf_write_free(async, bi.bytes_written);
1051 bi.buf_write_count = async->buf_write_count;
1052 bi.buf_write_ptr = async->buf_write_ptr;
1053 bi.buf_read_count = async->buf_read_count;
1054 bi.buf_read_ptr = async->buf_read_ptr;
1057 if (copy_to_user(arg, &bi, sizeof(bi)))
1063 static int check_insn_config_length(struct comedi_insn *insn,
1070 case INSN_CONFIG_DIO_OUTPUT:
1071 case INSN_CONFIG_DIO_INPUT:
1072 case INSN_CONFIG_DISARM:
1073 case INSN_CONFIG_RESET:
1077 case INSN_CONFIG_ARM:
1078 case INSN_CONFIG_DIO_QUERY:
1079 case INSN_CONFIG_BLOCK_SIZE:
1080 case INSN_CONFIG_FILTER:
1081 case INSN_CONFIG_SERIAL_CLOCK:
1082 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1083 case INSN_CONFIG_ALT_SOURCE:
1084 case INSN_CONFIG_SET_COUNTER_MODE:
1085 case INSN_CONFIG_8254_READ_STATUS:
1086 case INSN_CONFIG_SET_ROUTING:
1087 case INSN_CONFIG_GET_ROUTING:
1088 case INSN_CONFIG_GET_PWM_STATUS:
1089 case INSN_CONFIG_PWM_SET_PERIOD:
1090 case INSN_CONFIG_PWM_GET_PERIOD:
1094 case INSN_CONFIG_SET_GATE_SRC:
1095 case INSN_CONFIG_GET_GATE_SRC:
1096 case INSN_CONFIG_SET_CLOCK_SRC:
1097 case INSN_CONFIG_GET_CLOCK_SRC:
1098 case INSN_CONFIG_SET_OTHER_SRC:
1099 case INSN_CONFIG_GET_COUNTER_STATUS:
1100 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1101 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1102 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1106 case INSN_CONFIG_PWM_OUTPUT:
1107 case INSN_CONFIG_ANALOG_TRIG:
1111 case INSN_CONFIG_DIGITAL_TRIG:
1115 /* by default we allow the insn since we don't have checks for
1116 * all possible cases yet */
1118 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1120 pr_warn("comedi: Add a check to %s in %s.\n",
1121 __func__, __FILE__);
1122 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1128 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1129 unsigned int *data, void *file)
1131 struct comedi_subdevice *s;
1135 if (insn->insn & INSN_MASK_SPECIAL) {
1136 /* a non-subdevice instruction */
1138 switch (insn->insn) {
1148 do_gettimeofday(&tv);
1149 data[0] = tv.tv_sec;
1150 data[1] = tv.tv_usec;
1156 if (insn->n != 1 || data[0] >= 100000) {
1160 udelay(data[0] / 1000);
1168 if (insn->subdev >= dev->n_subdevices) {
1169 DPRINTK("%d not usable subdevice\n",
1174 s = &dev->subdevices[insn->subdev];
1176 DPRINTK("no async\n");
1180 if (!s->async->inttrig) {
1181 DPRINTK("no inttrig\n");
1185 ret = s->async->inttrig(dev, s, data[0]);
1190 DPRINTK("invalid insn\n");
1195 /* a subdevice instruction */
1196 unsigned int maxdata;
1198 if (insn->subdev >= dev->n_subdevices) {
1199 DPRINTK("subdevice %d out of range\n", insn->subdev);
1203 s = &dev->subdevices[insn->subdev];
1205 if (s->type == COMEDI_SUBD_UNUSED) {
1206 DPRINTK("%d not usable subdevice\n", insn->subdev);
1211 /* are we locked? (ioctl lock) */
1212 if (s->lock && s->lock != file) {
1213 DPRINTK("device locked\n");
1218 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1221 DPRINTK("bad chanspec\n");
1229 /* This looks arbitrary. It is. */
1230 s->busy = &parse_insn;
1231 switch (insn->insn) {
1233 ret = s->insn_read(dev, s, insn, data);
1236 maxdata = s->maxdata_list
1237 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1239 for (i = 0; i < insn->n; ++i) {
1240 if (data[i] > maxdata) {
1242 DPRINTK("bad data value(s)\n");
1247 ret = s->insn_write(dev, s, insn, data);
1253 /* Most drivers ignore the base channel in
1254 * insn->chanspec. Fix this here if
1255 * the subdevice has <= 32 channels. */
1257 unsigned int orig_mask;
1259 orig_mask = data[0];
1260 if (s->n_chan <= 32) {
1261 shift = CR_CHAN(insn->chanspec);
1269 ret = s->insn_bits(dev, s, insn, data);
1270 data[0] = orig_mask;
1276 ret = check_insn_config_length(insn, data);
1279 ret = s->insn_config(dev, s, insn, data);
1295 * synchronous instructions
1298 * pointer to sync cmd structure
1301 * sync cmd struct at arg
1308 /* arbitrary limits */
1309 #define MAX_SAMPLES 256
1310 static int do_insnlist_ioctl(struct comedi_device *dev,
1311 struct comedi_insnlist __user *arg, void *file)
1313 struct comedi_insnlist insnlist;
1314 struct comedi_insn *insns = NULL;
1315 unsigned int *data = NULL;
1319 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1322 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1324 DPRINTK("kmalloc failed\n");
1329 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1331 DPRINTK("kmalloc failed\n");
1336 if (copy_from_user(insns, insnlist.insns,
1337 sizeof(*insns) * insnlist.n_insns)) {
1338 DPRINTK("copy_from_user failed\n");
1343 for (i = 0; i < insnlist.n_insns; i++) {
1344 if (insns[i].n > MAX_SAMPLES) {
1345 DPRINTK("number of samples too large\n");
1349 if (insns[i].insn & INSN_MASK_WRITE) {
1350 if (copy_from_user(data, insns[i].data,
1351 insns[i].n * sizeof(unsigned int))) {
1352 DPRINTK("copy_from_user failed\n");
1357 ret = parse_insn(dev, insns + i, data, file);
1360 if (insns[i].insn & INSN_MASK_READ) {
1361 if (copy_to_user(insns[i].data, data,
1362 insns[i].n * sizeof(unsigned int))) {
1363 DPRINTK("copy_to_user failed\n");
1383 * synchronous instructions
1389 * struct comedi_insn struct at arg
1395 static int do_insn_ioctl(struct comedi_device *dev,
1396 struct comedi_insn __user *arg, void *file)
1398 struct comedi_insn insn;
1399 unsigned int *data = NULL;
1402 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1408 if (copy_from_user(&insn, arg, sizeof(insn))) {
1413 /* This is where the behavior of insn and insnlist deviate. */
1414 if (insn.n > MAX_SAMPLES)
1415 insn.n = MAX_SAMPLES;
1416 if (insn.insn & INSN_MASK_WRITE) {
1417 if (copy_from_user(data,
1419 insn.n * sizeof(unsigned int))) {
1424 ret = parse_insn(dev, &insn, data, file);
1427 if (insn.insn & INSN_MASK_READ) {
1428 if (copy_to_user(insn.data,
1430 insn.n * sizeof(unsigned int))) {
1443 static int do_cmd_ioctl(struct comedi_device *dev,
1444 struct comedi_cmd __user *arg, void *file)
1446 struct comedi_cmd cmd;
1447 struct comedi_subdevice *s;
1448 struct comedi_async *async;
1450 unsigned int __user *user_chanlist;
1452 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1453 DPRINTK("bad cmd address\n");
1456 /* save user's chanlist pointer so it can be restored later */
1457 user_chanlist = (unsigned int __user *)cmd.chanlist;
1459 if (cmd.subdev >= dev->n_subdevices) {
1460 DPRINTK("%d no such subdevice\n", cmd.subdev);
1464 s = &dev->subdevices[cmd.subdev];
1467 if (s->type == COMEDI_SUBD_UNUSED) {
1468 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1472 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1473 DPRINTK("subdevice %i does not support commands\n",
1478 /* are we locked? (ioctl lock) */
1479 if (s->lock && s->lock != file) {
1480 DPRINTK("subdevice locked\n");
1486 DPRINTK("subdevice busy\n");
1490 /* make sure channel/gain list isn't too long */
1491 if (cmd.chanlist_len > s->len_chanlist) {
1492 DPRINTK("channel/gain list too long %u > %d\n",
1493 cmd.chanlist_len, s->len_chanlist);
1497 /* make sure channel/gain list isn't too short */
1498 if (cmd.chanlist_len < 1) {
1499 DPRINTK("channel/gain list too short %u < 1\n",
1505 async->cmd.data = NULL;
1506 /* load channel/gain list */
1507 async->cmd.chanlist = memdup_user(user_chanlist,
1508 async->cmd.chanlist_len * sizeof(int));
1509 if (IS_ERR(async->cmd.chanlist)) {
1510 ret = PTR_ERR(async->cmd.chanlist);
1511 DPRINTK("memdup_user failed with code %d\n", ret);
1515 /* make sure each element in channel/gain list is valid */
1516 ret = comedi_check_chanlist(s,
1517 async->cmd.chanlist_len,
1518 async->cmd.chanlist);
1520 DPRINTK("bad chanlist\n");
1524 ret = s->do_cmdtest(dev, s, &async->cmd);
1526 if (async->cmd.flags & TRIG_BOGUS || ret) {
1527 DPRINTK("test returned %d\n", ret);
1529 /* restore chanlist pointer before copying back */
1530 cmd.chanlist = (unsigned int __force *)user_chanlist;
1532 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1533 DPRINTK("fault writing cmd\n");
1541 if (!async->prealloc_bufsz) {
1543 DPRINTK("no buffer (?)\n");
1547 comedi_buf_reset(async);
1550 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1552 if (async->cmd.flags & TRIG_WAKE_EOS)
1553 async->cb_mask |= COMEDI_CB_EOS;
1555 comedi_set_subdevice_runflags(s, SRF_ERROR | SRF_RUNNING, SRF_RUNNING);
1557 /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
1558 * comedi_read() or comedi_write() */
1560 ret = s->do_cmd(dev, s);
1565 do_become_nonbusy(dev, s);
1572 command testing ioctl
1575 pointer to cmd structure
1578 cmd structure at arg
1582 modified cmd structure at arg
1585 static int do_cmdtest_ioctl(struct comedi_device *dev,
1586 struct comedi_cmd __user *arg, void *file)
1588 struct comedi_cmd cmd;
1589 struct comedi_subdevice *s;
1591 unsigned int *chanlist = NULL;
1592 unsigned int __user *user_chanlist;
1594 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1595 DPRINTK("bad cmd address\n");
1598 /* save user's chanlist pointer so it can be restored later */
1599 user_chanlist = (unsigned int __user *)cmd.chanlist;
1601 if (cmd.subdev >= dev->n_subdevices) {
1602 DPRINTK("%d no such subdevice\n", cmd.subdev);
1606 s = &dev->subdevices[cmd.subdev];
1607 if (s->type == COMEDI_SUBD_UNUSED) {
1608 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1612 if (!s->do_cmd || !s->do_cmdtest) {
1613 DPRINTK("subdevice %i does not support commands\n",
1618 /* make sure channel/gain list isn't too long */
1619 if (cmd.chanlist_len > s->len_chanlist) {
1620 DPRINTK("channel/gain list too long %d > %d\n",
1621 cmd.chanlist_len, s->len_chanlist);
1626 /* load channel/gain list */
1628 chanlist = memdup_user(user_chanlist,
1629 cmd.chanlist_len * sizeof(int));
1630 if (IS_ERR(chanlist)) {
1631 ret = PTR_ERR(chanlist);
1632 DPRINTK("memdup_user exited with code %d", ret);
1636 /* make sure each element in channel/gain list is valid */
1637 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1639 DPRINTK("bad chanlist\n");
1643 cmd.chanlist = chanlist;
1646 ret = s->do_cmdtest(dev, s, &cmd);
1648 /* restore chanlist pointer before copying back */
1649 cmd.chanlist = (unsigned int __force *)user_chanlist;
1651 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1652 DPRINTK("bad cmd address\n");
1677 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1681 unsigned long flags;
1682 struct comedi_subdevice *s;
1684 if (arg >= dev->n_subdevices)
1686 s = &dev->subdevices[arg];
1688 spin_lock_irqsave(&s->spin_lock, flags);
1689 if (s->busy || s->lock)
1693 spin_unlock_irqrestore(&s->spin_lock, flags);
1700 ret = s->lock_f(dev, s);
1719 This function isn't protected by the semaphore, since
1720 we already own the lock.
1722 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1725 struct comedi_subdevice *s;
1727 if (arg >= dev->n_subdevices)
1729 s = &dev->subdevices[arg];
1734 if (s->lock && s->lock != file)
1737 if (s->lock == file) {
1751 cancel acquisition ioctl
1763 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1766 struct comedi_subdevice *s;
1769 if (arg >= dev->n_subdevices)
1771 s = &dev->subdevices[arg];
1772 if (s->async == NULL)
1775 if (s->lock && s->lock != file)
1781 if (s->busy != file)
1784 ret = do_cancel(dev, s);
1791 instructs driver to synchronize buffers
1803 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1806 struct comedi_subdevice *s;
1808 if (arg >= dev->n_subdevices)
1810 s = &dev->subdevices[arg];
1812 if (s->lock && s->lock != file)
1818 if (s->busy != file)
1822 return s->poll(dev, s);
1827 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1830 const unsigned minor = iminor(file_inode(file));
1831 struct comedi_device *dev = comedi_dev_from_minor(minor);
1837 mutex_lock(&dev->mutex);
1839 /* Device config is special, because it must work on
1840 * an unconfigured device. */
1841 if (cmd == COMEDI_DEVCONFIG) {
1842 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1843 /* Device config not appropriate on non-board minors. */
1847 rc = do_devconfig_ioctl(dev,
1848 (struct comedi_devconfig __user *)arg);
1851 dev->minor >= comedi_num_legacy_minors) {
1852 /* Successfully unconfigured a dynamically
1853 * allocated device. Try and remove it. */
1854 if (comedi_clear_board_dev(dev)) {
1855 mutex_unlock(&dev->mutex);
1856 comedi_free_board_dev(dev);
1864 if (!dev->attached) {
1865 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1871 case COMEDI_BUFCONFIG:
1872 rc = do_bufconfig_ioctl(dev,
1873 (struct comedi_bufconfig __user *)arg);
1875 case COMEDI_DEVINFO:
1876 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1879 case COMEDI_SUBDINFO:
1880 rc = do_subdinfo_ioctl(dev,
1881 (struct comedi_subdinfo __user *)arg,
1884 case COMEDI_CHANINFO:
1885 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1887 case COMEDI_RANGEINFO:
1888 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1890 case COMEDI_BUFINFO:
1891 rc = do_bufinfo_ioctl(dev,
1892 (struct comedi_bufinfo __user *)arg,
1896 rc = do_lock_ioctl(dev, arg, file);
1899 rc = do_unlock_ioctl(dev, arg, file);
1902 rc = do_cancel_ioctl(dev, arg, file);
1905 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1907 case COMEDI_CMDTEST:
1908 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1911 case COMEDI_INSNLIST:
1912 rc = do_insnlist_ioctl(dev,
1913 (struct comedi_insnlist __user *)arg,
1917 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1921 rc = do_poll_ioctl(dev, arg, file);
1929 mutex_unlock(&dev->mutex);
1933 static void comedi_vm_open(struct vm_area_struct *area)
1935 struct comedi_async *async;
1936 struct comedi_device *dev;
1938 async = area->vm_private_data;
1939 dev = async->subdevice->device;
1941 mutex_lock(&dev->mutex);
1942 async->mmap_count++;
1943 mutex_unlock(&dev->mutex);
1946 static void comedi_vm_close(struct vm_area_struct *area)
1948 struct comedi_async *async;
1949 struct comedi_device *dev;
1951 async = area->vm_private_data;
1952 dev = async->subdevice->device;
1954 mutex_lock(&dev->mutex);
1955 async->mmap_count--;
1956 mutex_unlock(&dev->mutex);
1959 static struct vm_operations_struct comedi_vm_ops = {
1960 .open = comedi_vm_open,
1961 .close = comedi_vm_close,
1964 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1966 const unsigned minor = iminor(file_inode(file));
1967 struct comedi_device *dev = comedi_dev_from_minor(minor);
1968 struct comedi_subdevice *s;
1969 struct comedi_async *async;
1970 unsigned long start = vma->vm_start;
1979 mutex_lock(&dev->mutex);
1981 if (!dev->attached) {
1982 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1987 if (vma->vm_flags & VM_WRITE)
1988 s = comedi_write_subdevice(dev, minor);
1990 s = comedi_read_subdevice(dev, minor);
2002 if (vma->vm_pgoff != 0) {
2003 DPRINTK("comedi: mmap() offset must be 0.\n");
2008 size = vma->vm_end - vma->vm_start;
2009 if (size > async->prealloc_bufsz) {
2013 if (size & (~PAGE_MASK)) {
2018 n_pages = size >> PAGE_SHIFT;
2019 for (i = 0; i < n_pages; ++i) {
2020 struct comedi_buf_page *buf = &async->buf_page_list[i];
2022 if (remap_pfn_range(vma, start,
2023 page_to_pfn(virt_to_page(buf->virt_addr)),
2024 PAGE_SIZE, PAGE_SHARED)) {
2031 vma->vm_ops = &comedi_vm_ops;
2032 vma->vm_private_data = async;
2034 async->mmap_count++;
2038 mutex_unlock(&dev->mutex);
2042 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2044 unsigned int mask = 0;
2045 const unsigned minor = iminor(file_inode(file));
2046 struct comedi_device *dev = comedi_dev_from_minor(minor);
2047 struct comedi_subdevice *s;
2052 mutex_lock(&dev->mutex);
2054 if (!dev->attached) {
2055 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2059 s = comedi_read_subdevice(dev, minor);
2060 if (s && s->async) {
2061 poll_wait(file, &s->async->wait_head, wait);
2062 if (!s->busy || !comedi_is_subdevice_running(s) ||
2063 comedi_buf_read_n_available(s->async) > 0)
2064 mask |= POLLIN | POLLRDNORM;
2067 s = comedi_write_subdevice(dev, minor);
2068 if (s && s->async) {
2069 unsigned int bps = bytes_per_sample(s->async->subdevice);
2071 poll_wait(file, &s->async->wait_head, wait);
2072 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2073 if (!s->busy || !comedi_is_subdevice_running(s) ||
2074 comedi_buf_write_n_allocated(s->async) >= bps)
2075 mask |= POLLOUT | POLLWRNORM;
2079 mutex_unlock(&dev->mutex);
2083 static ssize_t comedi_write(struct file *file, const char __user *buf,
2084 size_t nbytes, loff_t *offset)
2086 struct comedi_subdevice *s;
2087 struct comedi_async *async;
2088 int n, m, count = 0, retval = 0;
2089 DECLARE_WAITQUEUE(wait, current);
2090 const unsigned minor = iminor(file_inode(file));
2091 struct comedi_device *dev = comedi_dev_from_minor(minor);
2092 bool on_wait_queue = false;
2094 unsigned int old_detach_count;
2099 /* Protect against device detachment during operation. */
2100 down_read(&dev->attach_lock);
2101 attach_locked = true;
2102 old_detach_count = dev->detach_count;
2104 if (!dev->attached) {
2105 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2110 s = comedi_write_subdevice(dev, minor);
2111 if (!s || !s->async) {
2118 if (!s->busy || !nbytes)
2120 if (s->busy != file) {
2125 add_wait_queue(&async->wait_head, &wait);
2126 on_wait_queue = true;
2127 while (nbytes > 0 && !retval) {
2128 set_current_state(TASK_INTERRUPTIBLE);
2130 if (!comedi_is_subdevice_running(s)) {
2132 struct comedi_subdevice *new_s;
2134 if (comedi_is_subdevice_in_error(s))
2139 * To avoid deadlock, cannot acquire dev->mutex
2140 * while dev->attach_lock is held. Need to
2141 * remove task from the async wait queue before
2142 * releasing dev->attach_lock, as it might not
2143 * be valid afterwards.
2145 remove_wait_queue(&async->wait_head, &wait);
2146 on_wait_queue = false;
2147 up_read(&dev->attach_lock);
2148 attach_locked = false;
2149 mutex_lock(&dev->mutex);
2151 * Become non-busy unless things have changed
2152 * behind our back. Checking dev->detach_count
2153 * is unchanged ought to be sufficient (unless
2154 * there have been 2**32 detaches in the
2155 * meantime!), but check the subdevice pointer
2156 * as well just in case.
2158 new_s = comedi_write_subdevice(dev, minor);
2159 if (dev->attached &&
2160 old_detach_count == dev->detach_count &&
2161 s == new_s && new_s->async == async)
2162 do_become_nonbusy(dev, s);
2163 mutex_unlock(&dev->mutex);
2171 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2172 m = async->prealloc_bufsz - async->buf_write_ptr;
2173 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2174 if (m > comedi_buf_write_n_allocated(async))
2175 m = comedi_buf_write_n_allocated(async);
2180 if (file->f_flags & O_NONBLOCK) {
2185 if (signal_pending(current)) {
2186 retval = -ERESTARTSYS;
2191 if (s->busy != file) {
2198 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2204 comedi_buf_write_free(async, n);
2210 break; /* makes device work like a pipe */
2214 remove_wait_queue(&async->wait_head, &wait);
2215 set_current_state(TASK_RUNNING);
2217 up_read(&dev->attach_lock);
2219 return count ? count : retval;
2222 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2225 struct comedi_subdevice *s;
2226 struct comedi_async *async;
2227 int n, m, count = 0, retval = 0;
2228 DECLARE_WAITQUEUE(wait, current);
2229 const unsigned minor = iminor(file_inode(file));
2230 struct comedi_device *dev = comedi_dev_from_minor(minor);
2231 unsigned int old_detach_count;
2232 bool become_nonbusy = false;
2238 /* Protect against device detachment during operation. */
2239 down_read(&dev->attach_lock);
2240 attach_locked = true;
2241 old_detach_count = dev->detach_count;
2243 if (!dev->attached) {
2244 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2249 s = comedi_read_subdevice(dev, minor);
2250 if (!s || !s->async) {
2256 if (!s->busy || !nbytes)
2258 if (s->busy != file) {
2263 add_wait_queue(&async->wait_head, &wait);
2264 while (nbytes > 0 && !retval) {
2265 set_current_state(TASK_INTERRUPTIBLE);
2269 m = comedi_buf_read_n_available(async);
2270 /* printk("%d available\n",m); */
2271 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2272 m = async->prealloc_bufsz - async->buf_read_ptr;
2273 /* printk("%d contiguous\n",m); */
2278 if (!comedi_is_subdevice_running(s)) {
2279 if (comedi_is_subdevice_in_error(s))
2283 become_nonbusy = true;
2286 if (file->f_flags & O_NONBLOCK) {
2291 if (signal_pending(current)) {
2292 retval = -ERESTARTSYS;
2299 if (s->busy != file) {
2305 m = copy_to_user(buf, async->prealloc_buf +
2306 async->buf_read_ptr, n);
2312 comedi_buf_read_alloc(async, n);
2313 comedi_buf_read_free(async, n);
2319 break; /* makes device work like a pipe */
2321 remove_wait_queue(&async->wait_head, &wait);
2322 set_current_state(TASK_RUNNING);
2323 if (become_nonbusy || comedi_is_subdevice_idle(s)) {
2324 struct comedi_subdevice *new_s;
2327 * To avoid deadlock, cannot acquire dev->mutex
2328 * while dev->attach_lock is held.
2330 up_read(&dev->attach_lock);
2331 attach_locked = false;
2332 mutex_lock(&dev->mutex);
2334 * Check device hasn't become detached behind our back.
2335 * Checking dev->detach_count is unchanged ought to be
2336 * sufficient (unless there have been 2**32 detaches in the
2337 * meantime!), but check the subdevice pointer as well just in
2340 new_s = comedi_read_subdevice(dev, minor);
2341 if (dev->attached && old_detach_count == dev->detach_count &&
2342 s == new_s && new_s->async == async) {
2343 if (become_nonbusy ||
2344 async->buf_read_count - async->buf_write_count == 0)
2345 do_become_nonbusy(dev, s);
2347 mutex_unlock(&dev->mutex);
2351 up_read(&dev->attach_lock);
2353 return count ? count : retval;
2356 static int comedi_open(struct inode *inode, struct file *file)
2358 const unsigned minor = iminor(inode);
2359 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2363 DPRINTK("invalid minor number\n");
2367 /* This is slightly hacky, but we want module autoloading
2369 * case: user opens device, attached -> ok
2370 * case: user opens device, unattached, !in_request_module -> autoload
2371 * case: user opens device, unattached, in_request_module -> fail
2372 * case: root opens device, attached -> ok
2373 * case: root opens device, unattached, in_request_module -> ok
2374 * (typically called from modprobe)
2375 * case: root opens device, unattached, !in_request_module -> autoload
2377 * The last could be changed to "-> ok", which would deny root
2380 mutex_lock(&dev->mutex);
2383 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2384 DPRINTK("in request module\n");
2388 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2391 dev->in_request_module = true;
2394 mutex_unlock(&dev->mutex);
2395 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2396 mutex_lock(&dev->mutex);
2399 dev->in_request_module = false;
2401 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2402 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2407 __module_get(THIS_MODULE);
2409 if (dev->attached) {
2410 if (!try_module_get(dev->driver->module)) {
2411 module_put(THIS_MODULE);
2417 if (dev->attached && dev->use_count == 0 && dev->open) {
2418 rc = dev->open(dev);
2420 module_put(dev->driver->module);
2421 module_put(THIS_MODULE);
2430 mutex_unlock(&dev->mutex);
2432 comedi_dev_put(dev);
2436 static int comedi_fasync(int fd, struct file *file, int on)
2438 const unsigned minor = iminor(file_inode(file));
2439 struct comedi_device *dev = comedi_dev_from_minor(minor);
2444 return fasync_helper(fd, file, on, &dev->async_queue);
2447 static int comedi_close(struct inode *inode, struct file *file)
2449 const unsigned minor = iminor(inode);
2450 struct comedi_device *dev = comedi_dev_from_minor(minor);
2451 struct comedi_subdevice *s = NULL;
2457 mutex_lock(&dev->mutex);
2459 if (dev->subdevices) {
2460 for (i = 0; i < dev->n_subdevices; i++) {
2461 s = &dev->subdevices[i];
2463 if (s->busy == file)
2465 if (s->lock == file)
2469 if (dev->attached && dev->use_count == 1 && dev->close)
2472 module_put(THIS_MODULE);
2474 module_put(dev->driver->module);
2478 mutex_unlock(&dev->mutex);
2479 comedi_dev_put(dev);
2484 static const struct file_operations comedi_fops = {
2485 .owner = THIS_MODULE,
2486 .unlocked_ioctl = comedi_unlocked_ioctl,
2487 .compat_ioctl = comedi_compat_ioctl,
2488 .open = comedi_open,
2489 .release = comedi_close,
2490 .read = comedi_read,
2491 .write = comedi_write,
2492 .mmap = comedi_mmap,
2493 .poll = comedi_poll,
2494 .fasync = comedi_fasync,
2495 .llseek = noop_llseek,
2498 void comedi_error(const struct comedi_device *dev, const char *s)
2500 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2502 EXPORT_SYMBOL_GPL(comedi_error);
2504 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2506 struct comedi_async *async = s->async;
2507 unsigned runflags = 0;
2508 unsigned runflags_mask = 0;
2510 /* DPRINTK("comedi_event 0x%x\n",mask); */
2512 if (!comedi_is_subdevice_running(s))
2516 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2517 COMEDI_CB_OVERFLOW)) {
2518 runflags_mask |= SRF_RUNNING;
2520 /* remember if an error event has occurred, so an error
2521 * can be returned the next time the user does a read() */
2522 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2523 runflags_mask |= SRF_ERROR;
2524 runflags |= SRF_ERROR;
2526 if (runflags_mask) {
2527 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2528 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2531 if (async->cb_mask & s->async->events) {
2532 wake_up_interruptible(&async->wait_head);
2533 if (s->subdev_flags & SDF_CMD_READ)
2534 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2535 if (s->subdev_flags & SDF_CMD_WRITE)
2536 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2538 s->async->events = 0;
2540 EXPORT_SYMBOL_GPL(comedi_event);
2542 /* Note: the ->mutex is pre-locked on successful return */
2543 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2545 struct comedi_device *dev;
2546 struct device *csdev;
2549 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2551 return ERR_PTR(-ENOMEM);
2552 comedi_device_init(dev);
2553 comedi_set_hw_dev(dev, hardware_device);
2554 mutex_lock(&dev->mutex);
2555 mutex_lock(&comedi_board_minor_table_lock);
2556 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2557 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2558 if (comedi_board_minor_table[i] == NULL) {
2559 comedi_board_minor_table[i] = dev;
2563 mutex_unlock(&comedi_board_minor_table_lock);
2564 if (i == COMEDI_NUM_BOARD_MINORS) {
2565 mutex_unlock(&dev->mutex);
2566 comedi_device_cleanup(dev);
2567 comedi_dev_put(dev);
2568 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2569 return ERR_PTR(-EBUSY);
2572 csdev = device_create(comedi_class, hardware_device,
2573 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2575 dev->class_dev = csdev;
2577 /* Note: dev->mutex needs to be unlocked by the caller. */
2581 static void comedi_free_board_minor(unsigned minor)
2583 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2584 comedi_free_board_dev(comedi_clear_board_minor(minor));
2587 void comedi_release_hardware_device(struct device *hardware_device)
2590 struct comedi_device *dev;
2592 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2594 mutex_lock(&comedi_board_minor_table_lock);
2595 dev = comedi_board_minor_table[minor];
2596 if (dev && dev->hw_dev == hardware_device) {
2597 comedi_board_minor_table[minor] = NULL;
2598 mutex_unlock(&comedi_board_minor_table_lock);
2599 comedi_free_board_dev(dev);
2602 mutex_unlock(&comedi_board_minor_table_lock);
2606 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2608 struct comedi_device *dev = s->device;
2609 struct device *csdev;
2612 mutex_lock(&comedi_subdevice_minor_table_lock);
2613 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2614 if (comedi_subdevice_minor_table[i] == NULL) {
2615 comedi_subdevice_minor_table[i] = s;
2619 mutex_unlock(&comedi_subdevice_minor_table_lock);
2620 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2621 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2624 i += COMEDI_NUM_BOARD_MINORS;
2626 csdev = device_create(comedi_class, dev->class_dev,
2627 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2628 dev->minor, s->index);
2630 s->class_dev = csdev;
2635 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2644 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2645 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2647 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2648 mutex_lock(&comedi_subdevice_minor_table_lock);
2649 if (s == comedi_subdevice_minor_table[i])
2650 comedi_subdevice_minor_table[i] = NULL;
2651 mutex_unlock(&comedi_subdevice_minor_table_lock);
2653 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2654 s->class_dev = NULL;
2658 static void comedi_cleanup_board_minors(void)
2662 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2663 comedi_free_board_minor(i);
2666 static int __init comedi_init(void)
2671 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2673 if (comedi_num_legacy_minors < 0 ||
2674 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2675 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2676 COMEDI_NUM_BOARD_MINORS);
2680 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2681 COMEDI_NUM_MINORS, "comedi");
2684 cdev_init(&comedi_cdev, &comedi_fops);
2685 comedi_cdev.owner = THIS_MODULE;
2686 kobject_set_name(&comedi_cdev.kobj, "comedi");
2687 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2688 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2692 comedi_class = class_create(THIS_MODULE, "comedi");
2693 if (IS_ERR(comedi_class)) {
2694 pr_err("comedi: failed to create class\n");
2695 cdev_del(&comedi_cdev);
2696 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2698 return PTR_ERR(comedi_class);
2701 comedi_class->dev_groups = comedi_dev_groups;
2703 /* XXX requires /proc interface */
2706 /* create devices files for legacy/manual use */
2707 for (i = 0; i < comedi_num_legacy_minors; i++) {
2708 struct comedi_device *dev;
2709 dev = comedi_alloc_board_minor(NULL);
2711 comedi_cleanup_board_minors();
2712 cdev_del(&comedi_cdev);
2713 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2715 return PTR_ERR(dev);
2717 /* comedi_alloc_board_minor() locked the mutex */
2718 mutex_unlock(&dev->mutex);
2724 module_init(comedi_init);
2726 static void __exit comedi_cleanup(void)
2730 comedi_cleanup_board_minors();
2731 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2732 BUG_ON(comedi_board_minor_table[i]);
2733 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2734 BUG_ON(comedi_subdevice_minor_table[i]);
2736 class_destroy(comedi_class);
2737 cdev_del(&comedi_cdev);
2738 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2740 comedi_proc_cleanup();
2742 module_exit(comedi_cleanup);
2744 MODULE_AUTHOR("http://www.comedi.org");
2745 MODULE_DESCRIPTION("Comedi core module");
2746 MODULE_LICENSE("GPL");