5 * COMEDI - Linux Control and Measurement Device Interface
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.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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"
47 * comedi_subdevice "runflags"
48 * COMEDI_SRF_RT: DEPRECATED: command is running real-time
49 * COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred
50 * since the last command was started
51 * COMEDI_SRF_RUNNING: command is running
52 * COMEDI_SRF_FREE_SPRIV: free s->private on detach
54 * COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy"
56 #define COMEDI_SRF_RT BIT(1)
57 #define COMEDI_SRF_ERROR BIT(2)
58 #define COMEDI_SRF_RUNNING BIT(27)
59 #define COMEDI_SRF_FREE_SPRIV BIT(31)
61 #define COMEDI_SRF_BUSY_MASK (COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING)
64 * struct comedi_file - Per-file private data for COMEDI device
65 * @dev: COMEDI device.
66 * @read_subdev: Current "read" subdevice.
67 * @write_subdev: Current "write" subdevice.
68 * @last_detach_count: Last known detach count.
69 * @last_attached: Last known attached/detached state.
72 struct comedi_device *dev;
73 struct comedi_subdevice *read_subdev;
74 struct comedi_subdevice *write_subdev;
75 unsigned int last_detach_count;
79 #define COMEDI_NUM_MINORS 0x100
80 #define COMEDI_NUM_SUBDEVICE_MINORS \
81 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
83 static int comedi_num_legacy_minors;
84 module_param(comedi_num_legacy_minors, int, S_IRUGO);
85 MODULE_PARM_DESC(comedi_num_legacy_minors,
86 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
89 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
90 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
91 MODULE_PARM_DESC(comedi_default_buf_size_kb,
92 "default asynchronous buffer size in KiB (default "
93 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
95 unsigned int comedi_default_buf_maxsize_kb
96 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
97 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
98 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
99 "default maximum size of asynchronous buffer in KiB (default "
100 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
102 static DEFINE_MUTEX(comedi_board_minor_table_lock);
103 static struct comedi_device
104 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
106 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
107 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
108 static struct comedi_subdevice
109 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
111 static struct class *comedi_class;
112 static struct cdev comedi_cdev;
114 static void comedi_device_init(struct comedi_device *dev)
116 kref_init(&dev->refcount);
117 spin_lock_init(&dev->spinlock);
118 mutex_init(&dev->mutex);
119 init_rwsem(&dev->attach_lock);
123 static void comedi_dev_kref_release(struct kref *kref)
125 struct comedi_device *dev =
126 container_of(kref, struct comedi_device, refcount);
128 mutex_destroy(&dev->mutex);
129 put_device(dev->class_dev);
134 * comedi_dev_put() - Release a use of a COMEDI device
135 * @dev: COMEDI device.
137 * Must be called when a user of a COMEDI device is finished with it.
138 * When the last user of the COMEDI device calls this function, the
139 * COMEDI device is destroyed.
141 * Return: 1 if the COMEDI device is destroyed by this call or @dev is
142 * NULL, otherwise return 0. Callers must not assume the COMEDI
143 * device is still valid if this function returns 0.
145 int comedi_dev_put(struct comedi_device *dev)
148 return kref_put(&dev->refcount, comedi_dev_kref_release);
151 EXPORT_SYMBOL_GPL(comedi_dev_put);
153 static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
156 kref_get(&dev->refcount);
160 static void comedi_device_cleanup(struct comedi_device *dev)
162 struct module *driver_module = NULL;
166 mutex_lock(&dev->mutex);
168 driver_module = dev->driver->module;
169 comedi_device_detach(dev);
170 if (driver_module && dev->use_count)
171 module_put(driver_module);
172 mutex_unlock(&dev->mutex);
175 static bool comedi_clear_board_dev(struct comedi_device *dev)
177 unsigned int i = dev->minor;
178 bool cleared = false;
180 mutex_lock(&comedi_board_minor_table_lock);
181 if (dev == comedi_board_minor_table[i]) {
182 comedi_board_minor_table[i] = NULL;
185 mutex_unlock(&comedi_board_minor_table_lock);
189 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
191 struct comedi_device *dev;
193 mutex_lock(&comedi_board_minor_table_lock);
194 dev = comedi_board_minor_table[minor];
195 comedi_board_minor_table[minor] = NULL;
196 mutex_unlock(&comedi_board_minor_table_lock);
200 static void comedi_free_board_dev(struct comedi_device *dev)
203 comedi_device_cleanup(dev);
204 if (dev->class_dev) {
205 device_destroy(comedi_class,
206 MKDEV(COMEDI_MAJOR, dev->minor));
212 static struct comedi_subdevice
213 *comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned minor)
215 struct comedi_subdevice *s;
216 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
218 mutex_lock(&comedi_subdevice_minor_table_lock);
219 s = comedi_subdevice_minor_table[i];
220 if (s && s->device != dev)
222 mutex_unlock(&comedi_subdevice_minor_table_lock);
226 static struct comedi_device *comedi_dev_get_from_board_minor(unsigned minor)
228 struct comedi_device *dev;
230 mutex_lock(&comedi_board_minor_table_lock);
231 dev = comedi_dev_get(comedi_board_minor_table[minor]);
232 mutex_unlock(&comedi_board_minor_table_lock);
236 static struct comedi_device *comedi_dev_get_from_subdevice_minor(unsigned minor)
238 struct comedi_device *dev;
239 struct comedi_subdevice *s;
240 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
242 mutex_lock(&comedi_subdevice_minor_table_lock);
243 s = comedi_subdevice_minor_table[i];
244 dev = comedi_dev_get(s ? s->device : NULL);
245 mutex_unlock(&comedi_subdevice_minor_table_lock);
250 * comedi_dev_get_from_minor() - Get COMEDI device by minor device number
251 * @minor: Minor device number.
253 * Finds the COMEDI device associated with the minor device number, if any,
254 * and increments its reference count. The COMEDI device is prevented from
255 * being freed until a matching call is made to comedi_dev_put().
257 * Return: A pointer to the COMEDI device if it exists, with its usage
258 * reference incremented. Return NULL if no COMEDI device exists with the
259 * specified minor device number.
261 struct comedi_device *comedi_dev_get_from_minor(unsigned minor)
263 if (minor < COMEDI_NUM_BOARD_MINORS)
264 return comedi_dev_get_from_board_minor(minor);
266 return comedi_dev_get_from_subdevice_minor(minor);
268 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
270 static struct comedi_subdevice *
271 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
273 struct comedi_subdevice *s;
275 if (minor >= COMEDI_NUM_BOARD_MINORS) {
276 s = comedi_subdevice_from_minor(dev, minor);
277 if (!s || (s->subdev_flags & SDF_CMD_READ))
280 return dev->read_subdev;
283 static struct comedi_subdevice *
284 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
286 struct comedi_subdevice *s;
288 if (minor >= COMEDI_NUM_BOARD_MINORS) {
289 s = comedi_subdevice_from_minor(dev, minor);
290 if (!s || (s->subdev_flags & SDF_CMD_WRITE))
293 return dev->write_subdev;
296 static void comedi_file_reset(struct file *file)
298 struct comedi_file *cfp = file->private_data;
299 struct comedi_device *dev = cfp->dev;
300 struct comedi_subdevice *s, *read_s, *write_s;
301 unsigned int minor = iminor(file_inode(file));
303 read_s = dev->read_subdev;
304 write_s = dev->write_subdev;
305 if (minor >= COMEDI_NUM_BOARD_MINORS) {
306 s = comedi_subdevice_from_minor(dev, minor);
307 if (!s || s->subdev_flags & SDF_CMD_READ)
309 if (!s || s->subdev_flags & SDF_CMD_WRITE)
312 cfp->last_attached = dev->attached;
313 cfp->last_detach_count = dev->detach_count;
314 ACCESS_ONCE(cfp->read_subdev) = read_s;
315 ACCESS_ONCE(cfp->write_subdev) = write_s;
318 static void comedi_file_check(struct file *file)
320 struct comedi_file *cfp = file->private_data;
321 struct comedi_device *dev = cfp->dev;
323 if (cfp->last_attached != dev->attached ||
324 cfp->last_detach_count != dev->detach_count)
325 comedi_file_reset(file);
328 static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file)
330 struct comedi_file *cfp = file->private_data;
332 comedi_file_check(file);
333 return ACCESS_ONCE(cfp->read_subdev);
336 static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file)
338 struct comedi_file *cfp = file->private_data;
340 comedi_file_check(file);
341 return ACCESS_ONCE(cfp->write_subdev);
344 static int resize_async_buffer(struct comedi_device *dev,
345 struct comedi_subdevice *s, unsigned new_size)
347 struct comedi_async *async = s->async;
350 if (new_size > async->max_bufsize)
354 dev_dbg(dev->class_dev,
355 "subdevice is busy, cannot resize buffer\n");
358 if (comedi_buf_is_mmapped(s)) {
359 dev_dbg(dev->class_dev,
360 "subdevice is mmapped, cannot resize buffer\n");
364 /* make sure buffer is an integral number of pages (we round up) */
365 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
367 retval = comedi_buf_alloc(dev, s, new_size);
372 retval = s->buf_change(dev, s);
377 dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
378 s->index, async->prealloc_bufsz);
382 /* sysfs attribute files */
384 static ssize_t max_read_buffer_kb_show(struct device *csdev,
385 struct device_attribute *attr, char *buf)
387 unsigned int minor = MINOR(csdev->devt);
388 struct comedi_device *dev;
389 struct comedi_subdevice *s;
390 unsigned int size = 0;
392 dev = comedi_dev_get_from_minor(minor);
396 mutex_lock(&dev->mutex);
397 s = comedi_read_subdevice(dev, minor);
398 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
399 size = s->async->max_bufsize / 1024;
400 mutex_unlock(&dev->mutex);
403 return snprintf(buf, PAGE_SIZE, "%u\n", size);
406 static ssize_t max_read_buffer_kb_store(struct device *csdev,
407 struct device_attribute *attr,
408 const char *buf, size_t count)
410 unsigned int minor = MINOR(csdev->devt);
411 struct comedi_device *dev;
412 struct comedi_subdevice *s;
416 err = kstrtouint(buf, 10, &size);
419 if (size > (UINT_MAX / 1024))
423 dev = comedi_dev_get_from_minor(minor);
427 mutex_lock(&dev->mutex);
428 s = comedi_read_subdevice(dev, minor);
429 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
430 s->async->max_bufsize = size;
433 mutex_unlock(&dev->mutex);
436 return err ? err : count;
438 static DEVICE_ATTR_RW(max_read_buffer_kb);
440 static ssize_t read_buffer_kb_show(struct device *csdev,
441 struct device_attribute *attr, char *buf)
443 unsigned int minor = MINOR(csdev->devt);
444 struct comedi_device *dev;
445 struct comedi_subdevice *s;
446 unsigned int size = 0;
448 dev = comedi_dev_get_from_minor(minor);
452 mutex_lock(&dev->mutex);
453 s = comedi_read_subdevice(dev, minor);
454 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
455 size = s->async->prealloc_bufsz / 1024;
456 mutex_unlock(&dev->mutex);
459 return snprintf(buf, PAGE_SIZE, "%u\n", size);
462 static ssize_t read_buffer_kb_store(struct device *csdev,
463 struct device_attribute *attr,
464 const char *buf, size_t count)
466 unsigned int minor = MINOR(csdev->devt);
467 struct comedi_device *dev;
468 struct comedi_subdevice *s;
472 err = kstrtouint(buf, 10, &size);
475 if (size > (UINT_MAX / 1024))
479 dev = comedi_dev_get_from_minor(minor);
483 mutex_lock(&dev->mutex);
484 s = comedi_read_subdevice(dev, minor);
485 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
486 err = resize_async_buffer(dev, s, size);
489 mutex_unlock(&dev->mutex);
492 return err ? err : count;
494 static DEVICE_ATTR_RW(read_buffer_kb);
496 static ssize_t max_write_buffer_kb_show(struct device *csdev,
497 struct device_attribute *attr,
500 unsigned int minor = MINOR(csdev->devt);
501 struct comedi_device *dev;
502 struct comedi_subdevice *s;
503 unsigned int size = 0;
505 dev = comedi_dev_get_from_minor(minor);
509 mutex_lock(&dev->mutex);
510 s = comedi_write_subdevice(dev, minor);
511 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
512 size = s->async->max_bufsize / 1024;
513 mutex_unlock(&dev->mutex);
516 return snprintf(buf, PAGE_SIZE, "%u\n", size);
519 static ssize_t max_write_buffer_kb_store(struct device *csdev,
520 struct device_attribute *attr,
521 const char *buf, size_t count)
523 unsigned int minor = MINOR(csdev->devt);
524 struct comedi_device *dev;
525 struct comedi_subdevice *s;
529 err = kstrtouint(buf, 10, &size);
532 if (size > (UINT_MAX / 1024))
536 dev = comedi_dev_get_from_minor(minor);
540 mutex_lock(&dev->mutex);
541 s = comedi_write_subdevice(dev, minor);
542 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
543 s->async->max_bufsize = size;
546 mutex_unlock(&dev->mutex);
549 return err ? err : count;
551 static DEVICE_ATTR_RW(max_write_buffer_kb);
553 static ssize_t write_buffer_kb_show(struct device *csdev,
554 struct device_attribute *attr, char *buf)
556 unsigned int minor = MINOR(csdev->devt);
557 struct comedi_device *dev;
558 struct comedi_subdevice *s;
559 unsigned int size = 0;
561 dev = comedi_dev_get_from_minor(minor);
565 mutex_lock(&dev->mutex);
566 s = comedi_write_subdevice(dev, minor);
567 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
568 size = s->async->prealloc_bufsz / 1024;
569 mutex_unlock(&dev->mutex);
572 return snprintf(buf, PAGE_SIZE, "%u\n", size);
575 static ssize_t write_buffer_kb_store(struct device *csdev,
576 struct device_attribute *attr,
577 const char *buf, size_t count)
579 unsigned int minor = MINOR(csdev->devt);
580 struct comedi_device *dev;
581 struct comedi_subdevice *s;
585 err = kstrtouint(buf, 10, &size);
588 if (size > (UINT_MAX / 1024))
592 dev = comedi_dev_get_from_minor(minor);
596 mutex_lock(&dev->mutex);
597 s = comedi_write_subdevice(dev, minor);
598 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
599 err = resize_async_buffer(dev, s, size);
602 mutex_unlock(&dev->mutex);
605 return err ? err : count;
607 static DEVICE_ATTR_RW(write_buffer_kb);
609 static struct attribute *comedi_dev_attrs[] = {
610 &dev_attr_max_read_buffer_kb.attr,
611 &dev_attr_read_buffer_kb.attr,
612 &dev_attr_max_write_buffer_kb.attr,
613 &dev_attr_write_buffer_kb.attr,
616 ATTRIBUTE_GROUPS(comedi_dev);
618 static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s,
621 s->runflags &= ~bits;
624 static void __comedi_set_subdevice_runflags(struct comedi_subdevice *s,
630 static void comedi_update_subdevice_runflags(struct comedi_subdevice *s,
631 unsigned mask, unsigned bits)
635 spin_lock_irqsave(&s->spin_lock, flags);
636 __comedi_clear_subdevice_runflags(s, mask);
637 __comedi_set_subdevice_runflags(s, bits & mask);
638 spin_unlock_irqrestore(&s->spin_lock, flags);
641 static unsigned __comedi_get_subdevice_runflags(struct comedi_subdevice *s)
646 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
651 spin_lock_irqsave(&s->spin_lock, flags);
652 runflags = __comedi_get_subdevice_runflags(s);
653 spin_unlock_irqrestore(&s->spin_lock, flags);
657 static bool comedi_is_runflags_running(unsigned runflags)
659 return runflags & COMEDI_SRF_RUNNING;
662 static bool comedi_is_runflags_in_error(unsigned runflags)
664 return runflags & COMEDI_SRF_ERROR;
668 * comedi_is_subdevice_running() - Check if async command running on subdevice
669 * @s: COMEDI subdevice.
671 * Return: %true if an asynchronous COMEDI command is active on the
672 * subdevice, else %false.
674 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
676 unsigned runflags = comedi_get_subdevice_runflags(s);
678 return comedi_is_runflags_running(runflags);
680 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
682 static bool __comedi_is_subdevice_running(struct comedi_subdevice *s)
684 unsigned runflags = __comedi_get_subdevice_runflags(s);
686 return comedi_is_runflags_running(runflags);
689 bool comedi_can_auto_free_spriv(struct comedi_subdevice *s)
691 unsigned runflags = __comedi_get_subdevice_runflags(s);
693 return runflags & COMEDI_SRF_FREE_SPRIV;
697 * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
698 * @s: COMEDI subdevice.
700 * Mark the subdevice as having a pointer to private data that can be
701 * automatically freed when the COMEDI device is detached from the low-level
704 void comedi_set_spriv_auto_free(struct comedi_subdevice *s)
706 __comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV);
708 EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free);
711 * comedi_alloc_spriv - Allocate memory for the subdevice private data
712 * @s: COMEDI subdevice.
713 * @size: Size of the memory to allocate.
715 * Allocate memory for the subdevice private data and point @s->private
716 * to it. The memory will be freed automatically when the COMEDI device
717 * is detached from the low-level driver.
719 * Return: A pointer to the allocated memory @s->private on success.
720 * Return NULL on failure.
722 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
724 s->private = kzalloc(size, GFP_KERNEL);
726 comedi_set_spriv_auto_free(s);
729 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
732 * This function restores a subdevice to an idle state.
734 static void do_become_nonbusy(struct comedi_device *dev,
735 struct comedi_subdevice *s)
737 struct comedi_async *async = s->async;
739 comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
742 async->inttrig = NULL;
743 kfree(async->cmd.chanlist);
744 async->cmd.chanlist = NULL;
746 wake_up_interruptible_all(&async->wait_head);
748 dev_err(dev->class_dev,
749 "BUG: (?) do_become_nonbusy called with async=NULL\n");
754 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
758 if (comedi_is_subdevice_running(s) && s->cancel)
759 ret = s->cancel(dev, s);
761 do_become_nonbusy(dev, s);
766 void comedi_device_cancel_all(struct comedi_device *dev)
768 struct comedi_subdevice *s;
774 for (i = 0; i < dev->n_subdevices; i++) {
775 s = &dev->subdevices[i];
781 static int is_device_busy(struct comedi_device *dev)
783 struct comedi_subdevice *s;
789 for (i = 0; i < dev->n_subdevices; i++) {
790 s = &dev->subdevices[i];
793 if (s->async && comedi_buf_is_mmapped(s))
801 * COMEDI_DEVCONFIG ioctl
802 * attaches (and configures) or detaches a legacy device
805 * pointer to comedi_devconfig structure (NULL if detaching)
808 * comedi_devconfig structure (if attaching)
813 static int do_devconfig_ioctl(struct comedi_device *dev,
814 struct comedi_devconfig __user *arg)
816 struct comedi_devconfig it;
818 if (!capable(CAP_SYS_ADMIN))
822 if (is_device_busy(dev))
825 struct module *driver_module = dev->driver->module;
827 comedi_device_detach(dev);
828 module_put(driver_module);
833 if (copy_from_user(&it, arg, sizeof(it)))
836 it.board_name[COMEDI_NAMELEN - 1] = 0;
838 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
839 dev_warn(dev->class_dev,
840 "comedi_config --init_data is deprecated\n");
844 if (dev->minor >= comedi_num_legacy_minors)
845 /* don't re-use dynamically allocated comedi devices */
848 /* This increments the driver module count on success. */
849 return comedi_device_attach(dev, &it);
853 * COMEDI_BUFCONFIG ioctl
854 * buffer configuration
857 * pointer to comedi_bufconfig structure
860 * comedi_bufconfig structure
863 * modified comedi_bufconfig structure
865 static int do_bufconfig_ioctl(struct comedi_device *dev,
866 struct comedi_bufconfig __user *arg)
868 struct comedi_bufconfig bc;
869 struct comedi_async *async;
870 struct comedi_subdevice *s;
873 if (copy_from_user(&bc, arg, sizeof(bc)))
876 if (bc.subdevice >= dev->n_subdevices)
879 s = &dev->subdevices[bc.subdevice];
883 dev_dbg(dev->class_dev,
884 "subdevice does not have async capability\n");
890 if (bc.maximum_size) {
891 if (!capable(CAP_SYS_ADMIN))
894 async->max_bufsize = bc.maximum_size;
898 retval = resize_async_buffer(dev, s, bc.size);
903 bc.size = async->prealloc_bufsz;
904 bc.maximum_size = async->max_bufsize;
907 if (copy_to_user(arg, &bc, sizeof(bc)))
914 * COMEDI_DEVINFO ioctl
918 * pointer to comedi_devinfo structure
924 * comedi_devinfo structure
926 static int do_devinfo_ioctl(struct comedi_device *dev,
927 struct comedi_devinfo __user *arg,
930 struct comedi_subdevice *s;
931 struct comedi_devinfo devinfo;
933 memset(&devinfo, 0, sizeof(devinfo));
935 /* fill devinfo structure */
936 devinfo.version_code = COMEDI_VERSION_CODE;
937 devinfo.n_subdevs = dev->n_subdevices;
938 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
939 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
941 s = comedi_file_read_subdevice(file);
943 devinfo.read_subdevice = s->index;
945 devinfo.read_subdevice = -1;
947 s = comedi_file_write_subdevice(file);
949 devinfo.write_subdevice = s->index;
951 devinfo.write_subdevice = -1;
953 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
960 * COMEDI_SUBDINFO ioctl
964 * pointer to array of comedi_subdinfo structures
970 * array of comedi_subdinfo structures
972 static int do_subdinfo_ioctl(struct comedi_device *dev,
973 struct comedi_subdinfo __user *arg, void *file)
976 struct comedi_subdinfo *tmp, *us;
977 struct comedi_subdevice *s;
979 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
983 /* fill subdinfo structs */
984 for (i = 0; i < dev->n_subdevices; i++) {
985 s = &dev->subdevices[i];
989 us->n_chan = s->n_chan;
990 us->subd_flags = s->subdev_flags;
991 if (comedi_is_subdevice_running(s))
992 us->subd_flags |= SDF_RUNNING;
993 #define TIMER_nanosec 5 /* backwards compatibility */
994 us->timer_type = TIMER_nanosec;
995 us->len_chanlist = s->len_chanlist;
996 us->maxdata = s->maxdata;
997 if (s->range_table) {
999 (i << 24) | (0 << 16) | (s->range_table->length);
1001 us->range_type = 0; /* XXX */
1005 us->subd_flags |= SDF_BUSY;
1006 if (s->busy == file)
1007 us->subd_flags |= SDF_BUSY_OWNER;
1009 us->subd_flags |= SDF_LOCKED;
1010 if (s->lock == file)
1011 us->subd_flags |= SDF_LOCK_OWNER;
1012 if (!s->maxdata && s->maxdata_list)
1013 us->subd_flags |= SDF_MAXDATA;
1014 if (s->range_table_list)
1015 us->subd_flags |= SDF_RANGETYPE;
1017 us->subd_flags |= SDF_CMD;
1019 if (s->insn_bits != &insn_inval)
1020 us->insn_bits_support = COMEDI_SUPPORTED;
1022 us->insn_bits_support = COMEDI_UNSUPPORTED;
1025 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
1029 return ret ? -EFAULT : 0;
1033 * COMEDI_CHANINFO ioctl
1034 * subdevice channel info
1037 * pointer to comedi_chaninfo structure
1040 * comedi_chaninfo structure
1043 * array of maxdata values to chaninfo->maxdata_list if requested
1044 * array of range table lengths to chaninfo->range_table_list if requested
1046 static int do_chaninfo_ioctl(struct comedi_device *dev,
1047 struct comedi_chaninfo __user *arg)
1049 struct comedi_subdevice *s;
1050 struct comedi_chaninfo it;
1052 if (copy_from_user(&it, arg, sizeof(it)))
1055 if (it.subdev >= dev->n_subdevices)
1057 s = &dev->subdevices[it.subdev];
1059 if (it.maxdata_list) {
1060 if (s->maxdata || !s->maxdata_list)
1062 if (copy_to_user(it.maxdata_list, s->maxdata_list,
1063 s->n_chan * sizeof(unsigned int)))
1068 return -EINVAL; /* flaglist not supported */
1073 if (!s->range_table_list)
1075 for (i = 0; i < s->n_chan; i++) {
1078 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
1079 (s->range_table_list[i]->length);
1080 if (put_user(x, it.rangelist + i))
1089 * COMEDI_BUFINFO ioctl
1090 * buffer information
1093 * pointer to comedi_bufinfo structure
1096 * comedi_bufinfo structure
1099 * modified comedi_bufinfo structure
1101 static int do_bufinfo_ioctl(struct comedi_device *dev,
1102 struct comedi_bufinfo __user *arg, void *file)
1104 struct comedi_bufinfo bi;
1105 struct comedi_subdevice *s;
1106 struct comedi_async *async;
1107 unsigned int runflags;
1109 bool become_nonbusy = false;
1111 if (copy_from_user(&bi, arg, sizeof(bi)))
1114 if (bi.subdevice >= dev->n_subdevices)
1117 s = &dev->subdevices[bi.subdevice];
1121 if (!async || s->busy != file)
1124 runflags = comedi_get_subdevice_runflags(s);
1125 if (!(async->cmd.flags & CMDF_WRITE)) {
1126 /* command was set up in "read" direction */
1127 if (bi.bytes_read) {
1128 comedi_buf_read_alloc(s, bi.bytes_read);
1129 bi.bytes_read = comedi_buf_read_free(s, bi.bytes_read);
1132 * If nothing left to read, and command has stopped, and
1133 * {"read" position not updated or command stopped normally},
1134 * then become non-busy.
1136 if (comedi_buf_read_n_available(s) == 0 &&
1137 !comedi_is_runflags_running(runflags) &&
1138 (bi.bytes_read == 0 ||
1139 !comedi_is_runflags_in_error(runflags))) {
1140 become_nonbusy = true;
1141 if (comedi_is_runflags_in_error(runflags))
1144 bi.bytes_written = 0;
1146 /* command was set up in "write" direction */
1147 if (!comedi_is_runflags_running(runflags)) {
1148 bi.bytes_written = 0;
1149 become_nonbusy = true;
1150 if (comedi_is_runflags_in_error(runflags))
1152 } else if (bi.bytes_written) {
1153 comedi_buf_write_alloc(s, bi.bytes_written);
1155 comedi_buf_write_free(s, bi.bytes_written);
1160 bi.buf_write_count = async->buf_write_count;
1161 bi.buf_write_ptr = async->buf_write_ptr;
1162 bi.buf_read_count = async->buf_read_count;
1163 bi.buf_read_ptr = async->buf_read_ptr;
1166 do_become_nonbusy(dev, s);
1171 if (copy_to_user(arg, &bi, sizeof(bi)))
1177 static int check_insn_config_length(struct comedi_insn *insn,
1184 case INSN_CONFIG_DIO_OUTPUT:
1185 case INSN_CONFIG_DIO_INPUT:
1186 case INSN_CONFIG_DISARM:
1187 case INSN_CONFIG_RESET:
1191 case INSN_CONFIG_ARM:
1192 case INSN_CONFIG_DIO_QUERY:
1193 case INSN_CONFIG_BLOCK_SIZE:
1194 case INSN_CONFIG_FILTER:
1195 case INSN_CONFIG_SERIAL_CLOCK:
1196 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1197 case INSN_CONFIG_ALT_SOURCE:
1198 case INSN_CONFIG_SET_COUNTER_MODE:
1199 case INSN_CONFIG_8254_READ_STATUS:
1200 case INSN_CONFIG_SET_ROUTING:
1201 case INSN_CONFIG_GET_ROUTING:
1202 case INSN_CONFIG_GET_PWM_STATUS:
1203 case INSN_CONFIG_PWM_SET_PERIOD:
1204 case INSN_CONFIG_PWM_GET_PERIOD:
1208 case INSN_CONFIG_SET_GATE_SRC:
1209 case INSN_CONFIG_GET_GATE_SRC:
1210 case INSN_CONFIG_SET_CLOCK_SRC:
1211 case INSN_CONFIG_GET_CLOCK_SRC:
1212 case INSN_CONFIG_SET_OTHER_SRC:
1213 case INSN_CONFIG_GET_COUNTER_STATUS:
1214 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1215 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1216 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1220 case INSN_CONFIG_PWM_OUTPUT:
1221 case INSN_CONFIG_ANALOG_TRIG:
1225 case INSN_CONFIG_DIGITAL_TRIG:
1230 * by default we allow the insn since we don't have checks for
1231 * all possible cases yet
1234 pr_warn("No check for data length of config insn id %i is implemented\n",
1236 pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1237 pr_warn("Assuming n=%i is correct\n", insn->n);
1243 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1244 unsigned int *data, void *file)
1246 struct comedi_subdevice *s;
1250 if (insn->insn & INSN_MASK_SPECIAL) {
1251 /* a non-subdevice instruction */
1253 switch (insn->insn) {
1263 do_gettimeofday(&tv);
1264 data[0] = tv.tv_sec;
1265 data[1] = tv.tv_usec;
1271 if (insn->n != 1 || data[0] >= 100000) {
1275 udelay(data[0] / 1000);
1283 if (insn->subdev >= dev->n_subdevices) {
1284 dev_dbg(dev->class_dev,
1285 "%d not usable subdevice\n",
1290 s = &dev->subdevices[insn->subdev];
1292 dev_dbg(dev->class_dev, "no async\n");
1296 if (!s->async->inttrig) {
1297 dev_dbg(dev->class_dev, "no inttrig\n");
1301 ret = s->async->inttrig(dev, s, data[0]);
1306 dev_dbg(dev->class_dev, "invalid insn\n");
1311 /* a subdevice instruction */
1312 unsigned int maxdata;
1314 if (insn->subdev >= dev->n_subdevices) {
1315 dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1320 s = &dev->subdevices[insn->subdev];
1322 if (s->type == COMEDI_SUBD_UNUSED) {
1323 dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1329 /* are we locked? (ioctl lock) */
1330 if (s->lock && s->lock != file) {
1331 dev_dbg(dev->class_dev, "device locked\n");
1336 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1339 dev_dbg(dev->class_dev, "bad chanspec\n");
1347 /* This looks arbitrary. It is. */
1348 s->busy = parse_insn;
1349 switch (insn->insn) {
1351 ret = s->insn_read(dev, s, insn, data);
1352 if (ret == -ETIMEDOUT) {
1353 dev_dbg(dev->class_dev,
1354 "subdevice %d read instruction timed out\n",
1359 maxdata = s->maxdata_list
1360 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1362 for (i = 0; i < insn->n; ++i) {
1363 if (data[i] > maxdata) {
1365 dev_dbg(dev->class_dev,
1366 "bad data value(s)\n");
1371 ret = s->insn_write(dev, s, insn, data);
1372 if (ret == -ETIMEDOUT) {
1373 dev_dbg(dev->class_dev,
1374 "subdevice %d write instruction timed out\n",
1384 * Most drivers ignore the base channel in
1385 * insn->chanspec. Fix this here if
1386 * the subdevice has <= 32 channels.
1388 unsigned int orig_mask = data[0];
1389 unsigned int shift = 0;
1391 if (s->n_chan <= 32) {
1392 shift = CR_CHAN(insn->chanspec);
1399 ret = s->insn_bits(dev, s, insn, data);
1400 data[0] = orig_mask;
1406 ret = check_insn_config_length(insn, data);
1409 ret = s->insn_config(dev, s, insn, data);
1424 * COMEDI_INSNLIST ioctl
1425 * synchronous instruction list
1428 * pointer to comedi_insnlist structure
1431 * comedi_insnlist structure
1432 * array of comedi_insn structures from insnlist->insns pointer
1433 * data (for writes) from insns[].data pointers
1436 * data (for reads) to insns[].data pointers
1438 /* arbitrary limits */
1439 #define MAX_SAMPLES 256
1440 static int do_insnlist_ioctl(struct comedi_device *dev,
1441 struct comedi_insnlist __user *arg, void *file)
1443 struct comedi_insnlist insnlist;
1444 struct comedi_insn *insns = NULL;
1445 unsigned int *data = NULL;
1449 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1452 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1458 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1464 if (copy_from_user(insns, insnlist.insns,
1465 sizeof(*insns) * insnlist.n_insns)) {
1466 dev_dbg(dev->class_dev, "copy_from_user failed\n");
1471 for (i = 0; i < insnlist.n_insns; i++) {
1472 if (insns[i].n > MAX_SAMPLES) {
1473 dev_dbg(dev->class_dev,
1474 "number of samples too large\n");
1478 if (insns[i].insn & INSN_MASK_WRITE) {
1479 if (copy_from_user(data, insns[i].data,
1480 insns[i].n * sizeof(unsigned int))) {
1481 dev_dbg(dev->class_dev,
1482 "copy_from_user failed\n");
1487 ret = parse_insn(dev, insns + i, data, file);
1490 if (insns[i].insn & INSN_MASK_READ) {
1491 if (copy_to_user(insns[i].data, data,
1492 insns[i].n * sizeof(unsigned int))) {
1493 dev_dbg(dev->class_dev,
1494 "copy_to_user failed\n");
1514 * synchronous instruction
1517 * pointer to comedi_insn structure
1520 * comedi_insn structure
1521 * data (for writes) from insn->data pointer
1524 * data (for reads) to insn->data pointer
1526 static int do_insn_ioctl(struct comedi_device *dev,
1527 struct comedi_insn __user *arg, void *file)
1529 struct comedi_insn insn;
1530 unsigned int *data = NULL;
1533 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1539 if (copy_from_user(&insn, arg, sizeof(insn))) {
1544 /* This is where the behavior of insn and insnlist deviate. */
1545 if (insn.n > MAX_SAMPLES)
1546 insn.n = MAX_SAMPLES;
1547 if (insn.insn & INSN_MASK_WRITE) {
1548 if (copy_from_user(data,
1550 insn.n * sizeof(unsigned int))) {
1555 ret = parse_insn(dev, &insn, data, file);
1558 if (insn.insn & INSN_MASK_READ) {
1559 if (copy_to_user(insn.data,
1561 insn.n * sizeof(unsigned int))) {
1574 static int __comedi_get_user_cmd(struct comedi_device *dev,
1575 struct comedi_cmd __user *arg,
1576 struct comedi_cmd *cmd)
1578 struct comedi_subdevice *s;
1580 if (copy_from_user(cmd, arg, sizeof(*cmd))) {
1581 dev_dbg(dev->class_dev, "bad cmd address\n");
1585 if (cmd->subdev >= dev->n_subdevices) {
1586 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
1590 s = &dev->subdevices[cmd->subdev];
1592 if (s->type == COMEDI_SUBD_UNUSED) {
1593 dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1598 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1599 dev_dbg(dev->class_dev,
1600 "subdevice %d does not support commands\n",
1605 /* make sure channel/gain list isn't too long */
1606 if (cmd->chanlist_len > s->len_chanlist) {
1607 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1608 cmd->chanlist_len, s->len_chanlist);
1613 * Set the CMDF_WRITE flag to the correct state if the subdevice
1614 * supports only "read" commands or only "write" commands.
1616 switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
1618 cmd->flags &= ~CMDF_WRITE;
1621 cmd->flags |= CMDF_WRITE;
1630 static int __comedi_get_user_chanlist(struct comedi_device *dev,
1631 struct comedi_subdevice *s,
1632 unsigned int __user *user_chanlist,
1633 struct comedi_cmd *cmd)
1635 unsigned int *chanlist;
1638 cmd->chanlist = NULL;
1639 chanlist = memdup_user(user_chanlist,
1640 cmd->chanlist_len * sizeof(unsigned int));
1641 if (IS_ERR(chanlist))
1642 return PTR_ERR(chanlist);
1644 /* make sure each element in channel/gain list is valid */
1645 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1651 cmd->chanlist = chanlist;
1658 * asynchronous acquisition command set-up
1661 * pointer to comedi_cmd structure
1664 * comedi_cmd structure
1665 * channel/range list from cmd->chanlist pointer
1668 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1670 static int do_cmd_ioctl(struct comedi_device *dev,
1671 struct comedi_cmd __user *arg, void *file)
1673 struct comedi_cmd cmd;
1674 struct comedi_subdevice *s;
1675 struct comedi_async *async;
1676 unsigned int __user *user_chanlist;
1679 /* get the user's cmd and do some simple validation */
1680 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1684 /* save user's chanlist pointer so it can be restored later */
1685 user_chanlist = (unsigned int __user *)cmd.chanlist;
1687 s = &dev->subdevices[cmd.subdev];
1690 /* are we locked? (ioctl lock) */
1691 if (s->lock && s->lock != file) {
1692 dev_dbg(dev->class_dev, "subdevice locked\n");
1698 dev_dbg(dev->class_dev, "subdevice busy\n");
1702 /* make sure channel/gain list isn't too short */
1703 if (cmd.chanlist_len < 1) {
1704 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
1710 async->cmd.data = NULL;
1712 /* load channel/gain list */
1713 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1717 ret = s->do_cmdtest(dev, s, &async->cmd);
1719 if (async->cmd.flags & CMDF_BOGUS || ret) {
1720 dev_dbg(dev->class_dev, "test returned %d\n", ret);
1722 /* restore chanlist pointer before copying back */
1723 cmd.chanlist = (unsigned int __force *)user_chanlist;
1725 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1726 dev_dbg(dev->class_dev, "fault writing cmd\n");
1734 if (!async->prealloc_bufsz) {
1736 dev_dbg(dev->class_dev, "no buffer (?)\n");
1740 comedi_buf_reset(s);
1742 async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
1743 if (async->cmd.flags & CMDF_WAKE_EOS)
1744 async->cb_mask |= COMEDI_CB_EOS;
1746 comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
1747 COMEDI_SRF_RUNNING);
1750 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1751 * race with comedi_read() or comedi_write().
1754 ret = s->do_cmd(dev, s);
1759 do_become_nonbusy(dev, s);
1765 * COMEDI_CMDTEST ioctl
1766 * asynchronous acquisition command testing
1769 * pointer to comedi_cmd structure
1772 * comedi_cmd structure
1773 * channel/range list from cmd->chanlist pointer
1776 * possibly modified comedi_cmd structure
1778 static int do_cmdtest_ioctl(struct comedi_device *dev,
1779 struct comedi_cmd __user *arg, void *file)
1781 struct comedi_cmd cmd;
1782 struct comedi_subdevice *s;
1783 unsigned int __user *user_chanlist;
1786 /* get the user's cmd and do some simple validation */
1787 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1791 /* save user's chanlist pointer so it can be restored later */
1792 user_chanlist = (unsigned int __user *)cmd.chanlist;
1794 s = &dev->subdevices[cmd.subdev];
1796 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1797 if (user_chanlist) {
1798 /* load channel/gain list */
1799 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1804 ret = s->do_cmdtest(dev, s, &cmd);
1806 kfree(cmd.chanlist); /* free kernel copy of user chanlist */
1808 /* restore chanlist pointer before copying back */
1809 cmd.chanlist = (unsigned int __force *)user_chanlist;
1811 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1812 dev_dbg(dev->class_dev, "bad cmd address\n");
1832 static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
1836 unsigned long flags;
1837 struct comedi_subdevice *s;
1839 if (arg >= dev->n_subdevices)
1841 s = &dev->subdevices[arg];
1843 spin_lock_irqsave(&s->spin_lock, flags);
1844 if (s->busy || s->lock)
1848 spin_unlock_irqrestore(&s->spin_lock, flags);
1854 * COMEDI_UNLOCK ioctl
1866 static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
1869 struct comedi_subdevice *s;
1871 if (arg >= dev->n_subdevices)
1873 s = &dev->subdevices[arg];
1878 if (s->lock && s->lock != file)
1881 if (s->lock == file)
1888 * COMEDI_CANCEL ioctl
1889 * cancel asynchronous acquisition
1900 static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
1903 struct comedi_subdevice *s;
1905 if (arg >= dev->n_subdevices)
1907 s = &dev->subdevices[arg];
1914 if (s->busy != file)
1917 return do_cancel(dev, s);
1922 * instructs driver to synchronize buffers
1933 static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
1936 struct comedi_subdevice *s;
1938 if (arg >= dev->n_subdevices)
1940 s = &dev->subdevices[arg];
1945 if (s->busy != file)
1949 return s->poll(dev, s);
1955 * COMEDI_SETRSUBD ioctl
1956 * sets the current "read" subdevice on a per-file basis
1967 static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1970 struct comedi_file *cfp = file->private_data;
1971 struct comedi_subdevice *s_old, *s_new;
1973 if (arg >= dev->n_subdevices)
1976 s_new = &dev->subdevices[arg];
1977 s_old = comedi_file_read_subdevice(file);
1979 return 0; /* no change */
1981 if (!(s_new->subdev_flags & SDF_CMD_READ))
1985 * Check the file isn't still busy handling a "read" command on the
1986 * old subdevice (if any).
1988 if (s_old && s_old->busy == file && s_old->async &&
1989 !(s_old->async->cmd.flags & CMDF_WRITE))
1992 ACCESS_ONCE(cfp->read_subdev) = s_new;
1997 * COMEDI_SETWSUBD ioctl
1998 * sets the current "write" subdevice on a per-file basis
2009 static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
2012 struct comedi_file *cfp = file->private_data;
2013 struct comedi_subdevice *s_old, *s_new;
2015 if (arg >= dev->n_subdevices)
2018 s_new = &dev->subdevices[arg];
2019 s_old = comedi_file_write_subdevice(file);
2021 return 0; /* no change */
2023 if (!(s_new->subdev_flags & SDF_CMD_WRITE))
2027 * Check the file isn't still busy handling a "write" command on the
2028 * old subdevice (if any).
2030 if (s_old && s_old->busy == file && s_old->async &&
2031 (s_old->async->cmd.flags & CMDF_WRITE))
2034 ACCESS_ONCE(cfp->write_subdev) = s_new;
2038 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
2041 unsigned minor = iminor(file_inode(file));
2042 struct comedi_file *cfp = file->private_data;
2043 struct comedi_device *dev = cfp->dev;
2046 mutex_lock(&dev->mutex);
2049 * Device config is special, because it must work on
2050 * an unconfigured device.
2052 if (cmd == COMEDI_DEVCONFIG) {
2053 if (minor >= COMEDI_NUM_BOARD_MINORS) {
2054 /* Device config not appropriate on non-board minors. */
2058 rc = do_devconfig_ioctl(dev,
2059 (struct comedi_devconfig __user *)arg);
2062 dev->minor >= comedi_num_legacy_minors) {
2064 * Successfully unconfigured a dynamically
2065 * allocated device. Try and remove it.
2067 if (comedi_clear_board_dev(dev)) {
2068 mutex_unlock(&dev->mutex);
2069 comedi_free_board_dev(dev);
2077 if (!dev->attached) {
2078 dev_dbg(dev->class_dev, "no driver attached\n");
2084 case COMEDI_BUFCONFIG:
2085 rc = do_bufconfig_ioctl(dev,
2086 (struct comedi_bufconfig __user *)arg);
2088 case COMEDI_DEVINFO:
2089 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
2092 case COMEDI_SUBDINFO:
2093 rc = do_subdinfo_ioctl(dev,
2094 (struct comedi_subdinfo __user *)arg,
2097 case COMEDI_CHANINFO:
2098 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
2100 case COMEDI_RANGEINFO:
2101 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
2103 case COMEDI_BUFINFO:
2104 rc = do_bufinfo_ioctl(dev,
2105 (struct comedi_bufinfo __user *)arg,
2109 rc = do_lock_ioctl(dev, arg, file);
2112 rc = do_unlock_ioctl(dev, arg, file);
2115 rc = do_cancel_ioctl(dev, arg, file);
2118 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
2120 case COMEDI_CMDTEST:
2121 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
2124 case COMEDI_INSNLIST:
2125 rc = do_insnlist_ioctl(dev,
2126 (struct comedi_insnlist __user *)arg,
2130 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
2134 rc = do_poll_ioctl(dev, arg, file);
2136 case COMEDI_SETRSUBD:
2137 rc = do_setrsubd_ioctl(dev, arg, file);
2139 case COMEDI_SETWSUBD:
2140 rc = do_setwsubd_ioctl(dev, arg, file);
2148 mutex_unlock(&dev->mutex);
2152 static void comedi_vm_open(struct vm_area_struct *area)
2154 struct comedi_buf_map *bm;
2156 bm = area->vm_private_data;
2157 comedi_buf_map_get(bm);
2160 static void comedi_vm_close(struct vm_area_struct *area)
2162 struct comedi_buf_map *bm;
2164 bm = area->vm_private_data;
2165 comedi_buf_map_put(bm);
2168 static const struct vm_operations_struct comedi_vm_ops = {
2169 .open = comedi_vm_open,
2170 .close = comedi_vm_close,
2173 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2175 struct comedi_file *cfp = file->private_data;
2176 struct comedi_device *dev = cfp->dev;
2177 struct comedi_subdevice *s;
2178 struct comedi_async *async;
2179 struct comedi_buf_map *bm = NULL;
2180 unsigned long start = vma->vm_start;
2187 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2188 * and down-reading &dev->attach_lock should normally succeed without
2189 * contention unless the device is in the process of being attached
2192 if (!down_read_trylock(&dev->attach_lock))
2195 if (!dev->attached) {
2196 dev_dbg(dev->class_dev, "no driver attached\n");
2201 if (vma->vm_flags & VM_WRITE)
2202 s = comedi_file_write_subdevice(file);
2204 s = comedi_file_read_subdevice(file);
2216 if (vma->vm_pgoff != 0) {
2217 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
2222 size = vma->vm_end - vma->vm_start;
2223 if (size > async->prealloc_bufsz) {
2227 if (offset_in_page(size)) {
2232 n_pages = size >> PAGE_SHIFT;
2234 /* get reference to current buf map (if any) */
2235 bm = comedi_buf_map_from_subdev_get(s);
2236 if (!bm || n_pages > bm->n_pages) {
2240 for (i = 0; i < n_pages; ++i) {
2241 struct comedi_buf_page *buf = &bm->page_list[i];
2243 if (remap_pfn_range(vma, start,
2244 page_to_pfn(virt_to_page(buf->virt_addr)),
2245 PAGE_SIZE, PAGE_SHARED)) {
2252 vma->vm_ops = &comedi_vm_ops;
2253 vma->vm_private_data = bm;
2255 vma->vm_ops->open(vma);
2259 up_read(&dev->attach_lock);
2260 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
2264 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2266 unsigned int mask = 0;
2267 struct comedi_file *cfp = file->private_data;
2268 struct comedi_device *dev = cfp->dev;
2269 struct comedi_subdevice *s, *s_read;
2271 down_read(&dev->attach_lock);
2273 if (!dev->attached) {
2274 dev_dbg(dev->class_dev, "no driver attached\n");
2278 s = comedi_file_read_subdevice(file);
2280 if (s && s->async) {
2281 poll_wait(file, &s->async->wait_head, wait);
2282 if (s->busy != file || !comedi_is_subdevice_running(s) ||
2283 (s->async->cmd.flags & CMDF_WRITE) ||
2284 comedi_buf_read_n_available(s) > 0)
2285 mask |= POLLIN | POLLRDNORM;
2288 s = comedi_file_write_subdevice(file);
2289 if (s && s->async) {
2290 unsigned int bps = comedi_bytes_per_sample(s);
2293 poll_wait(file, &s->async->wait_head, wait);
2294 if (s->busy != file || !comedi_is_subdevice_running(s) ||
2295 !(s->async->cmd.flags & CMDF_WRITE) ||
2296 comedi_buf_write_n_available(s) >= bps)
2297 mask |= POLLOUT | POLLWRNORM;
2301 up_read(&dev->attach_lock);
2305 static ssize_t comedi_write(struct file *file, const char __user *buf,
2306 size_t nbytes, loff_t *offset)
2308 struct comedi_subdevice *s;
2309 struct comedi_async *async;
2313 DECLARE_WAITQUEUE(wait, current);
2314 struct comedi_file *cfp = file->private_data;
2315 struct comedi_device *dev = cfp->dev;
2316 bool become_nonbusy = false;
2318 unsigned int old_detach_count;
2320 /* Protect against device detachment during operation. */
2321 down_read(&dev->attach_lock);
2322 attach_locked = true;
2323 old_detach_count = dev->detach_count;
2325 if (!dev->attached) {
2326 dev_dbg(dev->class_dev, "no driver attached\n");
2331 s = comedi_file_write_subdevice(file);
2332 if (!s || !s->async) {
2338 if (s->busy != file || !(async->cmd.flags & CMDF_WRITE)) {
2343 add_wait_queue(&async->wait_head, &wait);
2344 while (count == 0 && !retval) {
2346 unsigned int wp, n1, n2;
2348 set_current_state(TASK_INTERRUPTIBLE);
2350 runflags = comedi_get_subdevice_runflags(s);
2351 if (!comedi_is_runflags_running(runflags)) {
2352 if (comedi_is_runflags_in_error(runflags))
2354 if (retval || nbytes)
2355 become_nonbusy = true;
2361 /* Allocate all free buffer space. */
2362 comedi_buf_write_alloc(s, async->prealloc_bufsz);
2363 m = comedi_buf_write_n_allocated(s);
2364 n = min_t(size_t, m, nbytes);
2367 if (file->f_flags & O_NONBLOCK) {
2372 if (signal_pending(current)) {
2373 retval = -ERESTARTSYS;
2376 if (s->busy != file ||
2377 !(async->cmd.flags & CMDF_WRITE)) {
2384 wp = async->buf_write_ptr;
2385 n1 = min(n, async->prealloc_bufsz - wp);
2387 m = copy_from_user(async->prealloc_buf + wp, buf, n1);
2391 m = copy_from_user(async->prealloc_buf, buf + n1, n2);
2396 comedi_buf_write_free(s, n);
2403 remove_wait_queue(&async->wait_head, &wait);
2404 set_current_state(TASK_RUNNING);
2405 if (become_nonbusy && count == 0) {
2406 struct comedi_subdevice *new_s;
2409 * To avoid deadlock, cannot acquire dev->mutex
2410 * while dev->attach_lock is held.
2412 up_read(&dev->attach_lock);
2413 attach_locked = false;
2414 mutex_lock(&dev->mutex);
2416 * Check device hasn't become detached behind our back.
2417 * Checking dev->detach_count is unchanged ought to be
2418 * sufficient (unless there have been 2**32 detaches in the
2419 * meantime!), but check the subdevice pointer as well just in
2422 * Also check the subdevice is still in a suitable state to
2423 * become non-busy in case it changed behind our back.
2425 new_s = comedi_file_write_subdevice(file);
2426 if (dev->attached && old_detach_count == dev->detach_count &&
2427 s == new_s && new_s->async == async && s->busy == file &&
2428 (async->cmd.flags & CMDF_WRITE) &&
2429 !comedi_is_subdevice_running(s))
2430 do_become_nonbusy(dev, s);
2431 mutex_unlock(&dev->mutex);
2435 up_read(&dev->attach_lock);
2437 return count ? count : retval;
2440 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2443 struct comedi_subdevice *s;
2444 struct comedi_async *async;
2448 DECLARE_WAITQUEUE(wait, current);
2449 struct comedi_file *cfp = file->private_data;
2450 struct comedi_device *dev = cfp->dev;
2451 unsigned int old_detach_count;
2452 bool become_nonbusy = false;
2455 /* Protect against device detachment during operation. */
2456 down_read(&dev->attach_lock);
2457 attach_locked = true;
2458 old_detach_count = dev->detach_count;
2460 if (!dev->attached) {
2461 dev_dbg(dev->class_dev, "no driver attached\n");
2466 s = comedi_file_read_subdevice(file);
2467 if (!s || !s->async) {
2473 if (s->busy != file || (async->cmd.flags & CMDF_WRITE)) {
2478 add_wait_queue(&async->wait_head, &wait);
2479 while (count == 0 && !retval) {
2480 unsigned int rp, n1, n2;
2482 set_current_state(TASK_INTERRUPTIBLE);
2484 m = comedi_buf_read_n_available(s);
2485 n = min_t(size_t, m, nbytes);
2488 unsigned runflags = comedi_get_subdevice_runflags(s);
2490 if (!comedi_is_runflags_running(runflags)) {
2491 if (comedi_is_runflags_in_error(runflags))
2493 if (retval || nbytes)
2494 become_nonbusy = true;
2499 if (file->f_flags & O_NONBLOCK) {
2504 if (signal_pending(current)) {
2505 retval = -ERESTARTSYS;
2508 if (s->busy != file ||
2509 (async->cmd.flags & CMDF_WRITE)) {
2515 rp = async->buf_read_ptr;
2516 n1 = min(n, async->prealloc_bufsz - rp);
2518 m = copy_to_user(buf, async->prealloc_buf + rp, n1);
2522 m = copy_to_user(buf + n1, async->prealloc_buf, n2);
2528 comedi_buf_read_alloc(s, n);
2529 comedi_buf_read_free(s, n);
2536 remove_wait_queue(&async->wait_head, &wait);
2537 set_current_state(TASK_RUNNING);
2538 if (become_nonbusy && count == 0) {
2539 struct comedi_subdevice *new_s;
2542 * To avoid deadlock, cannot acquire dev->mutex
2543 * while dev->attach_lock is held.
2545 up_read(&dev->attach_lock);
2546 attach_locked = false;
2547 mutex_lock(&dev->mutex);
2549 * Check device hasn't become detached behind our back.
2550 * Checking dev->detach_count is unchanged ought to be
2551 * sufficient (unless there have been 2**32 detaches in the
2552 * meantime!), but check the subdevice pointer as well just in
2555 * Also check the subdevice is still in a suitable state to
2556 * become non-busy in case it changed behind our back.
2558 new_s = comedi_file_read_subdevice(file);
2559 if (dev->attached && old_detach_count == dev->detach_count &&
2560 s == new_s && new_s->async == async && s->busy == file &&
2561 !(async->cmd.flags & CMDF_WRITE) &&
2562 !comedi_is_subdevice_running(s) &&
2563 comedi_buf_read_n_available(s) == 0)
2564 do_become_nonbusy(dev, s);
2565 mutex_unlock(&dev->mutex);
2569 up_read(&dev->attach_lock);
2571 return count ? count : retval;
2574 static int comedi_open(struct inode *inode, struct file *file)
2576 const unsigned minor = iminor(inode);
2577 struct comedi_file *cfp;
2578 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2582 pr_debug("invalid minor number\n");
2586 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2592 mutex_lock(&dev->mutex);
2593 if (!dev->attached && !capable(CAP_SYS_ADMIN)) {
2594 dev_dbg(dev->class_dev, "not attached and not CAP_SYS_ADMIN\n");
2598 if (dev->attached && dev->use_count == 0) {
2599 if (!try_module_get(dev->driver->module)) {
2604 rc = dev->open(dev);
2606 module_put(dev->driver->module);
2613 file->private_data = cfp;
2614 comedi_file_reset(file);
2618 mutex_unlock(&dev->mutex);
2620 comedi_dev_put(dev);
2626 static int comedi_fasync(int fd, struct file *file, int on)
2628 struct comedi_file *cfp = file->private_data;
2629 struct comedi_device *dev = cfp->dev;
2631 return fasync_helper(fd, file, on, &dev->async_queue);
2634 static int comedi_close(struct inode *inode, struct file *file)
2636 struct comedi_file *cfp = file->private_data;
2637 struct comedi_device *dev = cfp->dev;
2638 struct comedi_subdevice *s = NULL;
2641 mutex_lock(&dev->mutex);
2643 if (dev->subdevices) {
2644 for (i = 0; i < dev->n_subdevices; i++) {
2645 s = &dev->subdevices[i];
2647 if (s->busy == file)
2649 if (s->lock == file)
2653 if (dev->attached && dev->use_count == 1) {
2656 module_put(dev->driver->module);
2661 mutex_unlock(&dev->mutex);
2662 comedi_dev_put(dev);
2668 static const struct file_operations comedi_fops = {
2669 .owner = THIS_MODULE,
2670 .unlocked_ioctl = comedi_unlocked_ioctl,
2671 .compat_ioctl = comedi_compat_ioctl,
2672 .open = comedi_open,
2673 .release = comedi_close,
2674 .read = comedi_read,
2675 .write = comedi_write,
2676 .mmap = comedi_mmap,
2677 .poll = comedi_poll,
2678 .fasync = comedi_fasync,
2679 .llseek = noop_llseek,
2683 * comedi_event() - Handle events for asynchronous COMEDI command
2684 * @dev: COMEDI device.
2685 * @s: COMEDI subdevice.
2686 * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
2688 * If an asynchronous COMEDI command is active on the subdevice, process
2689 * any %COMEDI_CB_... event flags that have been set, usually by an
2690 * interrupt handler. These may change the run state of the asynchronous
2691 * command, wake a task, and/or send a %SIGIO signal.
2693 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2695 struct comedi_async *async = s->async;
2696 unsigned int events;
2698 unsigned long flags;
2700 spin_lock_irqsave(&s->spin_lock, flags);
2702 events = async->events;
2704 if (!__comedi_is_subdevice_running(s)) {
2705 spin_unlock_irqrestore(&s->spin_lock, flags);
2709 if (events & COMEDI_CB_CANCEL_MASK)
2710 __comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
2713 * Remember if an error event has occurred, so an error can be
2714 * returned the next time the user does a read() or write().
2716 if (events & COMEDI_CB_ERROR_MASK)
2717 __comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
2719 if (async->cb_mask & events) {
2720 wake_up_interruptible(&async->wait_head);
2721 si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
2724 spin_unlock_irqrestore(&s->spin_lock, flags);
2727 kill_fasync(&dev->async_queue, SIGIO, si_code);
2729 EXPORT_SYMBOL_GPL(comedi_event);
2731 /* Note: the ->mutex is pre-locked on successful return */
2732 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2734 struct comedi_device *dev;
2735 struct device *csdev;
2738 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2740 return ERR_PTR(-ENOMEM);
2741 comedi_device_init(dev);
2742 comedi_set_hw_dev(dev, hardware_device);
2743 mutex_lock(&dev->mutex);
2744 mutex_lock(&comedi_board_minor_table_lock);
2745 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2746 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2747 if (!comedi_board_minor_table[i]) {
2748 comedi_board_minor_table[i] = dev;
2752 mutex_unlock(&comedi_board_minor_table_lock);
2753 if (i == COMEDI_NUM_BOARD_MINORS) {
2754 mutex_unlock(&dev->mutex);
2755 comedi_device_cleanup(dev);
2756 comedi_dev_put(dev);
2757 dev_err(hardware_device,
2758 "ran out of minor numbers for board device files\n");
2759 return ERR_PTR(-EBUSY);
2762 csdev = device_create(comedi_class, hardware_device,
2763 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2765 dev->class_dev = get_device(csdev);
2767 /* Note: dev->mutex needs to be unlocked by the caller. */
2771 void comedi_release_hardware_device(struct device *hardware_device)
2774 struct comedi_device *dev;
2776 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2778 mutex_lock(&comedi_board_minor_table_lock);
2779 dev = comedi_board_minor_table[minor];
2780 if (dev && dev->hw_dev == hardware_device) {
2781 comedi_board_minor_table[minor] = NULL;
2782 mutex_unlock(&comedi_board_minor_table_lock);
2783 comedi_free_board_dev(dev);
2786 mutex_unlock(&comedi_board_minor_table_lock);
2790 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2792 struct comedi_device *dev = s->device;
2793 struct device *csdev;
2796 mutex_lock(&comedi_subdevice_minor_table_lock);
2797 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2798 if (!comedi_subdevice_minor_table[i]) {
2799 comedi_subdevice_minor_table[i] = s;
2803 mutex_unlock(&comedi_subdevice_minor_table_lock);
2804 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2805 dev_err(dev->class_dev,
2806 "ran out of minor numbers for subdevice files\n");
2809 i += COMEDI_NUM_BOARD_MINORS;
2811 csdev = device_create(comedi_class, dev->class_dev,
2812 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2813 dev->minor, s->index);
2815 s->class_dev = csdev;
2820 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2826 if (s->minor < COMEDI_NUM_BOARD_MINORS ||
2827 s->minor >= COMEDI_NUM_MINORS)
2830 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2831 mutex_lock(&comedi_subdevice_minor_table_lock);
2832 if (s == comedi_subdevice_minor_table[i])
2833 comedi_subdevice_minor_table[i] = NULL;
2834 mutex_unlock(&comedi_subdevice_minor_table_lock);
2836 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2837 s->class_dev = NULL;
2841 static void comedi_cleanup_board_minors(void)
2843 struct comedi_device *dev;
2846 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
2847 dev = comedi_clear_board_minor(i);
2848 comedi_free_board_dev(dev);
2852 static int __init comedi_init(void)
2857 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
2859 if (comedi_num_legacy_minors < 0 ||
2860 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2861 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2862 COMEDI_NUM_BOARD_MINORS);
2866 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2867 COMEDI_NUM_MINORS, "comedi");
2870 cdev_init(&comedi_cdev, &comedi_fops);
2871 comedi_cdev.owner = THIS_MODULE;
2873 retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
2875 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2880 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2881 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2885 comedi_class = class_create(THIS_MODULE, "comedi");
2886 if (IS_ERR(comedi_class)) {
2887 pr_err("failed to create class\n");
2888 cdev_del(&comedi_cdev);
2889 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2891 return PTR_ERR(comedi_class);
2894 comedi_class->dev_groups = comedi_dev_groups;
2896 /* XXX requires /proc interface */
2899 /* create devices files for legacy/manual use */
2900 for (i = 0; i < comedi_num_legacy_minors; i++) {
2901 struct comedi_device *dev;
2903 dev = comedi_alloc_board_minor(NULL);
2905 comedi_cleanup_board_minors();
2906 cdev_del(&comedi_cdev);
2907 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2909 return PTR_ERR(dev);
2911 /* comedi_alloc_board_minor() locked the mutex */
2912 mutex_unlock(&dev->mutex);
2917 module_init(comedi_init);
2919 static void __exit comedi_cleanup(void)
2921 comedi_cleanup_board_minors();
2922 class_destroy(comedi_class);
2923 cdev_del(&comedi_cdev);
2924 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2926 comedi_proc_cleanup();
2928 module_exit(comedi_cleanup);
2930 MODULE_AUTHOR("http://www.comedi.org");
2931 MODULE_DESCRIPTION("Comedi core module");
2932 MODULE_LICENSE("GPL");