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.
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 struct
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 structure
135 * @dev: comedi_device struct
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 by 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 struct
671 * Return true if an asynchronous comedi command is active on the comedi
672 * subdevice, else return 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 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
691 unsigned runflags = comedi_get_subdevice_runflags(s);
693 return !(runflags & COMEDI_SRF_BUSY_MASK);
696 bool comedi_can_auto_free_spriv(struct comedi_subdevice *s)
698 unsigned runflags = __comedi_get_subdevice_runflags(s);
700 return runflags & COMEDI_SRF_FREE_SPRIV;
704 * comedi_set_spriv_auto_free - mark subdevice private data as freeable
705 * @s: comedi_subdevice struct
707 * Mark the subdevice as having a pointer to private data that can be
708 * automatically freed by the comedi core during the detach.
710 void comedi_set_spriv_auto_free(struct comedi_subdevice *s)
712 __comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV);
714 EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free);
717 * comedi_alloc_spriv - Allocate memory for the subdevice private data.
718 * @s: comedi_subdevice struct
719 * @size: size of the memory to allocate
721 * This also sets the subdevice runflags to allow the core to automatically
722 * free the private data during the detach.
724 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
726 s->private = kzalloc(size, GFP_KERNEL);
728 comedi_set_spriv_auto_free(s);
731 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
734 * This function restores a subdevice to an idle state.
736 static void do_become_nonbusy(struct comedi_device *dev,
737 struct comedi_subdevice *s)
739 struct comedi_async *async = s->async;
741 comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
744 async->inttrig = NULL;
745 kfree(async->cmd.chanlist);
746 async->cmd.chanlist = NULL;
748 wake_up_interruptible_all(&async->wait_head);
750 dev_err(dev->class_dev,
751 "BUG: (?) do_become_nonbusy called with async=NULL\n");
756 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
760 if (comedi_is_subdevice_running(s) && s->cancel)
761 ret = s->cancel(dev, s);
763 do_become_nonbusy(dev, s);
768 void comedi_device_cancel_all(struct comedi_device *dev)
770 struct comedi_subdevice *s;
776 for (i = 0; i < dev->n_subdevices; i++) {
777 s = &dev->subdevices[i];
783 static int is_device_busy(struct comedi_device *dev)
785 struct comedi_subdevice *s;
791 for (i = 0; i < dev->n_subdevices; i++) {
792 s = &dev->subdevices[i];
795 if (s->async && comedi_buf_is_mmapped(s))
803 * COMEDI_DEVCONFIG ioctl
804 * attaches (and configures) or detaches a legacy device
807 * pointer to comedi_devconfig structure (NULL if detaching)
810 * comedi_devconfig structure (if attaching)
815 static int do_devconfig_ioctl(struct comedi_device *dev,
816 struct comedi_devconfig __user *arg)
818 struct comedi_devconfig it;
820 if (!capable(CAP_SYS_ADMIN))
824 if (is_device_busy(dev))
827 struct module *driver_module = dev->driver->module;
829 comedi_device_detach(dev);
830 module_put(driver_module);
835 if (copy_from_user(&it, arg, sizeof(it)))
838 it.board_name[COMEDI_NAMELEN - 1] = 0;
840 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
841 dev_warn(dev->class_dev,
842 "comedi_config --init_data is deprecated\n");
846 if (dev->minor >= comedi_num_legacy_minors)
847 /* don't re-use dynamically allocated comedi devices */
850 /* This increments the driver module count on success. */
851 return comedi_device_attach(dev, &it);
855 * COMEDI_BUFCONFIG ioctl
856 * buffer configuration
859 * pointer to comedi_bufconfig structure
862 * comedi_bufconfig structure
865 * modified comedi_bufconfig structure
867 static int do_bufconfig_ioctl(struct comedi_device *dev,
868 struct comedi_bufconfig __user *arg)
870 struct comedi_bufconfig bc;
871 struct comedi_async *async;
872 struct comedi_subdevice *s;
875 if (copy_from_user(&bc, arg, sizeof(bc)))
878 if (bc.subdevice >= dev->n_subdevices)
881 s = &dev->subdevices[bc.subdevice];
885 dev_dbg(dev->class_dev,
886 "subdevice does not have async capability\n");
892 if (bc.maximum_size) {
893 if (!capable(CAP_SYS_ADMIN))
896 async->max_bufsize = bc.maximum_size;
900 retval = resize_async_buffer(dev, s, bc.size);
905 bc.size = async->prealloc_bufsz;
906 bc.maximum_size = async->max_bufsize;
909 if (copy_to_user(arg, &bc, sizeof(bc)))
916 * COMEDI_DEVINFO ioctl
920 * pointer to comedi_devinfo structure
926 * comedi_devinfo structure
928 static int do_devinfo_ioctl(struct comedi_device *dev,
929 struct comedi_devinfo __user *arg,
932 struct comedi_subdevice *s;
933 struct comedi_devinfo devinfo;
935 memset(&devinfo, 0, sizeof(devinfo));
937 /* fill devinfo structure */
938 devinfo.version_code = COMEDI_VERSION_CODE;
939 devinfo.n_subdevs = dev->n_subdevices;
940 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
941 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
943 s = comedi_file_read_subdevice(file);
945 devinfo.read_subdevice = s->index;
947 devinfo.read_subdevice = -1;
949 s = comedi_file_write_subdevice(file);
951 devinfo.write_subdevice = s->index;
953 devinfo.write_subdevice = -1;
955 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
962 * COMEDI_SUBDINFO ioctl
966 * pointer to array of comedi_subdinfo structures
972 * array of comedi_subdinfo structures
974 static int do_subdinfo_ioctl(struct comedi_device *dev,
975 struct comedi_subdinfo __user *arg, void *file)
978 struct comedi_subdinfo *tmp, *us;
979 struct comedi_subdevice *s;
981 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
985 /* fill subdinfo structs */
986 for (i = 0; i < dev->n_subdevices; i++) {
987 s = &dev->subdevices[i];
991 us->n_chan = s->n_chan;
992 us->subd_flags = s->subdev_flags;
993 if (comedi_is_subdevice_running(s))
994 us->subd_flags |= SDF_RUNNING;
995 #define TIMER_nanosec 5 /* backwards compatibility */
996 us->timer_type = TIMER_nanosec;
997 us->len_chanlist = s->len_chanlist;
998 us->maxdata = s->maxdata;
999 if (s->range_table) {
1001 (i << 24) | (0 << 16) | (s->range_table->length);
1003 us->range_type = 0; /* XXX */
1007 us->subd_flags |= SDF_BUSY;
1008 if (s->busy == file)
1009 us->subd_flags |= SDF_BUSY_OWNER;
1011 us->subd_flags |= SDF_LOCKED;
1012 if (s->lock == file)
1013 us->subd_flags |= SDF_LOCK_OWNER;
1014 if (!s->maxdata && s->maxdata_list)
1015 us->subd_flags |= SDF_MAXDATA;
1016 if (s->range_table_list)
1017 us->subd_flags |= SDF_RANGETYPE;
1019 us->subd_flags |= SDF_CMD;
1021 if (s->insn_bits != &insn_inval)
1022 us->insn_bits_support = COMEDI_SUPPORTED;
1024 us->insn_bits_support = COMEDI_UNSUPPORTED;
1027 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
1031 return ret ? -EFAULT : 0;
1035 * COMEDI_CHANINFO ioctl
1036 * subdevice channel info
1039 * pointer to comedi_chaninfo structure
1042 * comedi_chaninfo structure
1045 * array of maxdata values to chaninfo->maxdata_list if requested
1046 * array of range table lengths to chaninfo->range_table_list if requested
1048 static int do_chaninfo_ioctl(struct comedi_device *dev,
1049 struct comedi_chaninfo __user *arg)
1051 struct comedi_subdevice *s;
1052 struct comedi_chaninfo it;
1054 if (copy_from_user(&it, arg, sizeof(it)))
1057 if (it.subdev >= dev->n_subdevices)
1059 s = &dev->subdevices[it.subdev];
1061 if (it.maxdata_list) {
1062 if (s->maxdata || !s->maxdata_list)
1064 if (copy_to_user(it.maxdata_list, s->maxdata_list,
1065 s->n_chan * sizeof(unsigned int)))
1070 return -EINVAL; /* flaglist not supported */
1075 if (!s->range_table_list)
1077 for (i = 0; i < s->n_chan; i++) {
1080 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
1081 (s->range_table_list[i]->length);
1082 if (put_user(x, it.rangelist + i))
1091 * COMEDI_BUFINFO ioctl
1092 * buffer information
1095 * pointer to comedi_bufinfo structure
1098 * comedi_bufinfo structure
1101 * modified comedi_bufinfo structure
1103 static int do_bufinfo_ioctl(struct comedi_device *dev,
1104 struct comedi_bufinfo __user *arg, void *file)
1106 struct comedi_bufinfo bi;
1107 struct comedi_subdevice *s;
1108 struct comedi_async *async;
1110 if (copy_from_user(&bi, arg, sizeof(bi)))
1113 if (bi.subdevice >= dev->n_subdevices)
1116 s = &dev->subdevices[bi.subdevice];
1121 dev_dbg(dev->class_dev,
1122 "subdevice does not have async capability\n");
1123 bi.buf_write_ptr = 0;
1124 bi.buf_read_ptr = 0;
1125 bi.buf_write_count = 0;
1126 bi.buf_read_count = 0;
1128 bi.bytes_written = 0;
1133 bi.bytes_written = 0;
1134 goto copyback_position;
1136 if (s->busy != file)
1139 if (bi.bytes_read && !(async->cmd.flags & CMDF_WRITE)) {
1140 bi.bytes_read = comedi_buf_read_alloc(s, bi.bytes_read);
1141 comedi_buf_read_free(s, bi.bytes_read);
1143 if (comedi_is_subdevice_idle(s) &&
1144 comedi_buf_n_bytes_ready(s) == 0) {
1145 do_become_nonbusy(dev, s);
1149 if (bi.bytes_written && (async->cmd.flags & CMDF_WRITE)) {
1151 comedi_buf_write_alloc(s, bi.bytes_written);
1152 comedi_buf_write_free(s, bi.bytes_written);
1156 bi.buf_write_count = async->buf_write_count;
1157 bi.buf_write_ptr = async->buf_write_ptr;
1158 bi.buf_read_count = async->buf_read_count;
1159 bi.buf_read_ptr = async->buf_read_ptr;
1162 if (copy_to_user(arg, &bi, sizeof(bi)))
1168 static int check_insn_config_length(struct comedi_insn *insn,
1175 case INSN_CONFIG_DIO_OUTPUT:
1176 case INSN_CONFIG_DIO_INPUT:
1177 case INSN_CONFIG_DISARM:
1178 case INSN_CONFIG_RESET:
1182 case INSN_CONFIG_ARM:
1183 case INSN_CONFIG_DIO_QUERY:
1184 case INSN_CONFIG_BLOCK_SIZE:
1185 case INSN_CONFIG_FILTER:
1186 case INSN_CONFIG_SERIAL_CLOCK:
1187 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1188 case INSN_CONFIG_ALT_SOURCE:
1189 case INSN_CONFIG_SET_COUNTER_MODE:
1190 case INSN_CONFIG_8254_READ_STATUS:
1191 case INSN_CONFIG_SET_ROUTING:
1192 case INSN_CONFIG_GET_ROUTING:
1193 case INSN_CONFIG_GET_PWM_STATUS:
1194 case INSN_CONFIG_PWM_SET_PERIOD:
1195 case INSN_CONFIG_PWM_GET_PERIOD:
1199 case INSN_CONFIG_SET_GATE_SRC:
1200 case INSN_CONFIG_GET_GATE_SRC:
1201 case INSN_CONFIG_SET_CLOCK_SRC:
1202 case INSN_CONFIG_GET_CLOCK_SRC:
1203 case INSN_CONFIG_SET_OTHER_SRC:
1204 case INSN_CONFIG_GET_COUNTER_STATUS:
1205 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1206 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1207 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1211 case INSN_CONFIG_PWM_OUTPUT:
1212 case INSN_CONFIG_ANALOG_TRIG:
1216 case INSN_CONFIG_DIGITAL_TRIG:
1221 * by default we allow the insn since we don't have checks for
1222 * all possible cases yet
1225 pr_warn("No check for data length of config insn id %i is implemented\n",
1227 pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1228 pr_warn("Assuming n=%i is correct\n", insn->n);
1234 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1235 unsigned int *data, void *file)
1237 struct comedi_subdevice *s;
1241 if (insn->insn & INSN_MASK_SPECIAL) {
1242 /* a non-subdevice instruction */
1244 switch (insn->insn) {
1254 do_gettimeofday(&tv);
1255 data[0] = tv.tv_sec;
1256 data[1] = tv.tv_usec;
1262 if (insn->n != 1 || data[0] >= 100000) {
1266 udelay(data[0] / 1000);
1274 if (insn->subdev >= dev->n_subdevices) {
1275 dev_dbg(dev->class_dev,
1276 "%d not usable subdevice\n",
1281 s = &dev->subdevices[insn->subdev];
1283 dev_dbg(dev->class_dev, "no async\n");
1287 if (!s->async->inttrig) {
1288 dev_dbg(dev->class_dev, "no inttrig\n");
1292 ret = s->async->inttrig(dev, s, data[0]);
1297 dev_dbg(dev->class_dev, "invalid insn\n");
1302 /* a subdevice instruction */
1303 unsigned int maxdata;
1305 if (insn->subdev >= dev->n_subdevices) {
1306 dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1311 s = &dev->subdevices[insn->subdev];
1313 if (s->type == COMEDI_SUBD_UNUSED) {
1314 dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1320 /* are we locked? (ioctl lock) */
1321 if (s->lock && s->lock != file) {
1322 dev_dbg(dev->class_dev, "device locked\n");
1327 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1330 dev_dbg(dev->class_dev, "bad chanspec\n");
1338 /* This looks arbitrary. It is. */
1339 s->busy = &parse_insn;
1340 switch (insn->insn) {
1342 ret = s->insn_read(dev, s, insn, data);
1343 if (ret == -ETIMEDOUT) {
1344 dev_dbg(dev->class_dev,
1345 "subdevice %d read instruction timed out\n",
1350 maxdata = s->maxdata_list
1351 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1353 for (i = 0; i < insn->n; ++i) {
1354 if (data[i] > maxdata) {
1356 dev_dbg(dev->class_dev,
1357 "bad data value(s)\n");
1362 ret = s->insn_write(dev, s, insn, data);
1363 if (ret == -ETIMEDOUT) {
1364 dev_dbg(dev->class_dev,
1365 "subdevice %d write instruction timed out\n",
1375 * Most drivers ignore the base channel in
1376 * insn->chanspec. Fix this here if
1377 * the subdevice has <= 32 channels.
1379 unsigned int orig_mask = data[0];
1380 unsigned int shift = 0;
1382 if (s->n_chan <= 32) {
1383 shift = CR_CHAN(insn->chanspec);
1390 ret = s->insn_bits(dev, s, insn, data);
1391 data[0] = orig_mask;
1397 ret = check_insn_config_length(insn, data);
1400 ret = s->insn_config(dev, s, insn, data);
1415 * COMEDI_INSNLIST ioctl
1416 * synchronous instruction list
1419 * pointer to comedi_insnlist structure
1422 * comedi_insnlist structure
1423 * array of comedi_insn structures from insnlist->insns pointer
1424 * data (for writes) from insns[].data pointers
1427 * data (for reads) to insns[].data pointers
1429 /* arbitrary limits */
1430 #define MAX_SAMPLES 256
1431 static int do_insnlist_ioctl(struct comedi_device *dev,
1432 struct comedi_insnlist __user *arg, void *file)
1434 struct comedi_insnlist insnlist;
1435 struct comedi_insn *insns = NULL;
1436 unsigned int *data = NULL;
1440 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1443 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1449 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1455 if (copy_from_user(insns, insnlist.insns,
1456 sizeof(*insns) * insnlist.n_insns)) {
1457 dev_dbg(dev->class_dev, "copy_from_user failed\n");
1462 for (i = 0; i < insnlist.n_insns; i++) {
1463 if (insns[i].n > MAX_SAMPLES) {
1464 dev_dbg(dev->class_dev,
1465 "number of samples too large\n");
1469 if (insns[i].insn & INSN_MASK_WRITE) {
1470 if (copy_from_user(data, insns[i].data,
1471 insns[i].n * sizeof(unsigned int))) {
1472 dev_dbg(dev->class_dev,
1473 "copy_from_user failed\n");
1478 ret = parse_insn(dev, insns + i, data, file);
1481 if (insns[i].insn & INSN_MASK_READ) {
1482 if (copy_to_user(insns[i].data, data,
1483 insns[i].n * sizeof(unsigned int))) {
1484 dev_dbg(dev->class_dev,
1485 "copy_to_user failed\n");
1505 * synchronous instruction
1508 * pointer to comedi_insn structure
1511 * comedi_insn structure
1512 * data (for writes) from insn->data pointer
1515 * data (for reads) to insn->data pointer
1517 static int do_insn_ioctl(struct comedi_device *dev,
1518 struct comedi_insn __user *arg, void *file)
1520 struct comedi_insn insn;
1521 unsigned int *data = NULL;
1524 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1530 if (copy_from_user(&insn, arg, sizeof(insn))) {
1535 /* This is where the behavior of insn and insnlist deviate. */
1536 if (insn.n > MAX_SAMPLES)
1537 insn.n = MAX_SAMPLES;
1538 if (insn.insn & INSN_MASK_WRITE) {
1539 if (copy_from_user(data,
1541 insn.n * sizeof(unsigned int))) {
1546 ret = parse_insn(dev, &insn, data, file);
1549 if (insn.insn & INSN_MASK_READ) {
1550 if (copy_to_user(insn.data,
1552 insn.n * sizeof(unsigned int))) {
1565 static int __comedi_get_user_cmd(struct comedi_device *dev,
1566 struct comedi_cmd __user *arg,
1567 struct comedi_cmd *cmd)
1569 struct comedi_subdevice *s;
1571 if (copy_from_user(cmd, arg, sizeof(*cmd))) {
1572 dev_dbg(dev->class_dev, "bad cmd address\n");
1576 if (cmd->subdev >= dev->n_subdevices) {
1577 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
1581 s = &dev->subdevices[cmd->subdev];
1583 if (s->type == COMEDI_SUBD_UNUSED) {
1584 dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1589 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1590 dev_dbg(dev->class_dev,
1591 "subdevice %d does not support commands\n",
1596 /* make sure channel/gain list isn't too long */
1597 if (cmd->chanlist_len > s->len_chanlist) {
1598 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1599 cmd->chanlist_len, s->len_chanlist);
1604 * Set the CMDF_WRITE flag to the correct state if the subdevice
1605 * supports only "read" commands or only "write" commands.
1607 switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
1609 cmd->flags &= ~CMDF_WRITE;
1612 cmd->flags |= CMDF_WRITE;
1621 static int __comedi_get_user_chanlist(struct comedi_device *dev,
1622 struct comedi_subdevice *s,
1623 unsigned int __user *user_chanlist,
1624 struct comedi_cmd *cmd)
1626 unsigned int *chanlist;
1629 cmd->chanlist = NULL;
1630 chanlist = memdup_user(user_chanlist,
1631 cmd->chanlist_len * sizeof(unsigned int));
1632 if (IS_ERR(chanlist))
1633 return PTR_ERR(chanlist);
1635 /* make sure each element in channel/gain list is valid */
1636 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1642 cmd->chanlist = chanlist;
1649 * asynchronous acquisition command set-up
1652 * pointer to comedi_cmd structure
1655 * comedi_cmd structure
1656 * channel/range list from cmd->chanlist pointer
1659 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1661 static int do_cmd_ioctl(struct comedi_device *dev,
1662 struct comedi_cmd __user *arg, void *file)
1664 struct comedi_cmd cmd;
1665 struct comedi_subdevice *s;
1666 struct comedi_async *async;
1667 unsigned int __user *user_chanlist;
1670 /* get the user's cmd and do some simple validation */
1671 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1675 /* save user's chanlist pointer so it can be restored later */
1676 user_chanlist = (unsigned int __user *)cmd.chanlist;
1678 s = &dev->subdevices[cmd.subdev];
1681 /* are we locked? (ioctl lock) */
1682 if (s->lock && s->lock != file) {
1683 dev_dbg(dev->class_dev, "subdevice locked\n");
1689 dev_dbg(dev->class_dev, "subdevice busy\n");
1693 /* make sure channel/gain list isn't too short */
1694 if (cmd.chanlist_len < 1) {
1695 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
1701 async->cmd.data = NULL;
1703 /* load channel/gain list */
1704 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1708 ret = s->do_cmdtest(dev, s, &async->cmd);
1710 if (async->cmd.flags & CMDF_BOGUS || ret) {
1711 dev_dbg(dev->class_dev, "test returned %d\n", ret);
1713 /* restore chanlist pointer before copying back */
1714 cmd.chanlist = (unsigned int __force *)user_chanlist;
1716 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1717 dev_dbg(dev->class_dev, "fault writing cmd\n");
1725 if (!async->prealloc_bufsz) {
1727 dev_dbg(dev->class_dev, "no buffer (?)\n");
1731 comedi_buf_reset(s);
1733 async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
1734 if (async->cmd.flags & CMDF_WAKE_EOS)
1735 async->cb_mask |= COMEDI_CB_EOS;
1737 comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
1738 COMEDI_SRF_RUNNING);
1741 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1742 * race with comedi_read() or comedi_write().
1745 ret = s->do_cmd(dev, s);
1750 do_become_nonbusy(dev, s);
1756 * COMEDI_CMDTEST ioctl
1757 * asynchronous acquisition command testing
1760 * pointer to comedi_cmd structure
1763 * comedi_cmd structure
1764 * channel/range list from cmd->chanlist pointer
1767 * possibly modified comedi_cmd structure
1769 static int do_cmdtest_ioctl(struct comedi_device *dev,
1770 struct comedi_cmd __user *arg, void *file)
1772 struct comedi_cmd cmd;
1773 struct comedi_subdevice *s;
1774 unsigned int __user *user_chanlist;
1777 /* get the user's cmd and do some simple validation */
1778 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1782 /* save user's chanlist pointer so it can be restored later */
1783 user_chanlist = (unsigned int __user *)cmd.chanlist;
1785 s = &dev->subdevices[cmd.subdev];
1787 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1788 if (user_chanlist) {
1789 /* load channel/gain list */
1790 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1795 ret = s->do_cmdtest(dev, s, &cmd);
1797 kfree(cmd.chanlist); /* free kernel copy of user chanlist */
1799 /* restore chanlist pointer before copying back */
1800 cmd.chanlist = (unsigned int __force *)user_chanlist;
1802 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1803 dev_dbg(dev->class_dev, "bad cmd address\n");
1823 static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
1827 unsigned long flags;
1828 struct comedi_subdevice *s;
1830 if (arg >= dev->n_subdevices)
1832 s = &dev->subdevices[arg];
1834 spin_lock_irqsave(&s->spin_lock, flags);
1835 if (s->busy || s->lock)
1839 spin_unlock_irqrestore(&s->spin_lock, flags);
1845 * COMEDI_UNLOCK ioctl
1857 static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
1860 struct comedi_subdevice *s;
1862 if (arg >= dev->n_subdevices)
1864 s = &dev->subdevices[arg];
1869 if (s->lock && s->lock != file)
1872 if (s->lock == file)
1879 * COMEDI_CANCEL ioctl
1880 * cancel asynchronous acquisition
1891 static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
1894 struct comedi_subdevice *s;
1896 if (arg >= dev->n_subdevices)
1898 s = &dev->subdevices[arg];
1905 if (s->busy != file)
1908 return do_cancel(dev, s);
1913 * instructs driver to synchronize buffers
1924 static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
1927 struct comedi_subdevice *s;
1929 if (arg >= dev->n_subdevices)
1931 s = &dev->subdevices[arg];
1936 if (s->busy != file)
1940 return s->poll(dev, s);
1946 * COMEDI_SETRSUBD ioctl
1947 * sets the current "read" subdevice on a per-file basis
1958 static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1961 struct comedi_file *cfp = file->private_data;
1962 struct comedi_subdevice *s_old, *s_new;
1964 if (arg >= dev->n_subdevices)
1967 s_new = &dev->subdevices[arg];
1968 s_old = comedi_file_read_subdevice(file);
1970 return 0; /* no change */
1972 if (!(s_new->subdev_flags & SDF_CMD_READ))
1976 * Check the file isn't still busy handling a "read" command on the
1977 * old subdevice (if any).
1979 if (s_old && s_old->busy == file && s_old->async &&
1980 !(s_old->async->cmd.flags & CMDF_WRITE))
1983 ACCESS_ONCE(cfp->read_subdev) = s_new;
1988 * COMEDI_SETWSUBD ioctl
1989 * sets the current "write" subdevice on a per-file basis
2000 static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
2003 struct comedi_file *cfp = file->private_data;
2004 struct comedi_subdevice *s_old, *s_new;
2006 if (arg >= dev->n_subdevices)
2009 s_new = &dev->subdevices[arg];
2010 s_old = comedi_file_write_subdevice(file);
2012 return 0; /* no change */
2014 if (!(s_new->subdev_flags & SDF_CMD_WRITE))
2018 * Check the file isn't still busy handling a "write" command on the
2019 * old subdevice (if any).
2021 if (s_old && s_old->busy == file && s_old->async &&
2022 (s_old->async->cmd.flags & CMDF_WRITE))
2025 ACCESS_ONCE(cfp->write_subdev) = s_new;
2029 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
2032 unsigned minor = iminor(file_inode(file));
2033 struct comedi_file *cfp = file->private_data;
2034 struct comedi_device *dev = cfp->dev;
2037 mutex_lock(&dev->mutex);
2040 * Device config is special, because it must work on
2041 * an unconfigured device.
2043 if (cmd == COMEDI_DEVCONFIG) {
2044 if (minor >= COMEDI_NUM_BOARD_MINORS) {
2045 /* Device config not appropriate on non-board minors. */
2049 rc = do_devconfig_ioctl(dev,
2050 (struct comedi_devconfig __user *)arg);
2053 dev->minor >= comedi_num_legacy_minors) {
2055 * Successfully unconfigured a dynamically
2056 * allocated device. Try and remove it.
2058 if (comedi_clear_board_dev(dev)) {
2059 mutex_unlock(&dev->mutex);
2060 comedi_free_board_dev(dev);
2068 if (!dev->attached) {
2069 dev_dbg(dev->class_dev, "no driver attached\n");
2075 case COMEDI_BUFCONFIG:
2076 rc = do_bufconfig_ioctl(dev,
2077 (struct comedi_bufconfig __user *)arg);
2079 case COMEDI_DEVINFO:
2080 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
2083 case COMEDI_SUBDINFO:
2084 rc = do_subdinfo_ioctl(dev,
2085 (struct comedi_subdinfo __user *)arg,
2088 case COMEDI_CHANINFO:
2089 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
2091 case COMEDI_RANGEINFO:
2092 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
2094 case COMEDI_BUFINFO:
2095 rc = do_bufinfo_ioctl(dev,
2096 (struct comedi_bufinfo __user *)arg,
2100 rc = do_lock_ioctl(dev, arg, file);
2103 rc = do_unlock_ioctl(dev, arg, file);
2106 rc = do_cancel_ioctl(dev, arg, file);
2109 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
2111 case COMEDI_CMDTEST:
2112 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
2115 case COMEDI_INSNLIST:
2116 rc = do_insnlist_ioctl(dev,
2117 (struct comedi_insnlist __user *)arg,
2121 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
2125 rc = do_poll_ioctl(dev, arg, file);
2127 case COMEDI_SETRSUBD:
2128 rc = do_setrsubd_ioctl(dev, arg, file);
2130 case COMEDI_SETWSUBD:
2131 rc = do_setwsubd_ioctl(dev, arg, file);
2139 mutex_unlock(&dev->mutex);
2143 static void comedi_vm_open(struct vm_area_struct *area)
2145 struct comedi_buf_map *bm;
2147 bm = area->vm_private_data;
2148 comedi_buf_map_get(bm);
2151 static void comedi_vm_close(struct vm_area_struct *area)
2153 struct comedi_buf_map *bm;
2155 bm = area->vm_private_data;
2156 comedi_buf_map_put(bm);
2159 static struct vm_operations_struct comedi_vm_ops = {
2160 .open = comedi_vm_open,
2161 .close = comedi_vm_close,
2164 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2166 struct comedi_file *cfp = file->private_data;
2167 struct comedi_device *dev = cfp->dev;
2168 struct comedi_subdevice *s;
2169 struct comedi_async *async;
2170 struct comedi_buf_map *bm = NULL;
2171 unsigned long start = vma->vm_start;
2178 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2179 * and down-reading &dev->attach_lock should normally succeed without
2180 * contention unless the device is in the process of being attached
2183 if (!down_read_trylock(&dev->attach_lock))
2186 if (!dev->attached) {
2187 dev_dbg(dev->class_dev, "no driver attached\n");
2192 if (vma->vm_flags & VM_WRITE)
2193 s = comedi_file_write_subdevice(file);
2195 s = comedi_file_read_subdevice(file);
2207 if (vma->vm_pgoff != 0) {
2208 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
2213 size = vma->vm_end - vma->vm_start;
2214 if (size > async->prealloc_bufsz) {
2218 if (size & (~PAGE_MASK)) {
2223 n_pages = size >> PAGE_SHIFT;
2225 /* get reference to current buf map (if any) */
2226 bm = comedi_buf_map_from_subdev_get(s);
2227 if (!bm || n_pages > bm->n_pages) {
2231 for (i = 0; i < n_pages; ++i) {
2232 struct comedi_buf_page *buf = &bm->page_list[i];
2234 if (remap_pfn_range(vma, start,
2235 page_to_pfn(virt_to_page(buf->virt_addr)),
2236 PAGE_SIZE, PAGE_SHARED)) {
2243 vma->vm_ops = &comedi_vm_ops;
2244 vma->vm_private_data = bm;
2246 vma->vm_ops->open(vma);
2250 up_read(&dev->attach_lock);
2251 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
2255 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2257 unsigned int mask = 0;
2258 struct comedi_file *cfp = file->private_data;
2259 struct comedi_device *dev = cfp->dev;
2260 struct comedi_subdevice *s;
2262 mutex_lock(&dev->mutex);
2264 if (!dev->attached) {
2265 dev_dbg(dev->class_dev, "no driver attached\n");
2269 s = comedi_file_read_subdevice(file);
2270 if (s && s->async) {
2271 poll_wait(file, &s->async->wait_head, wait);
2272 if (!s->busy || !comedi_is_subdevice_running(s) ||
2273 (s->async->cmd.flags & CMDF_WRITE) ||
2274 comedi_buf_read_n_available(s) > 0)
2275 mask |= POLLIN | POLLRDNORM;
2278 s = comedi_file_write_subdevice(file);
2279 if (s && s->async) {
2280 unsigned int bps = comedi_bytes_per_sample(s);
2282 poll_wait(file, &s->async->wait_head, wait);
2283 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
2284 if (!s->busy || !comedi_is_subdevice_running(s) ||
2285 !(s->async->cmd.flags & CMDF_WRITE) ||
2286 comedi_buf_write_n_allocated(s) >= bps)
2287 mask |= POLLOUT | POLLWRNORM;
2291 mutex_unlock(&dev->mutex);
2295 static ssize_t comedi_write(struct file *file, const char __user *buf,
2296 size_t nbytes, loff_t *offset)
2298 struct comedi_subdevice *s;
2299 struct comedi_async *async;
2300 int n, m, count = 0, retval = 0;
2301 DECLARE_WAITQUEUE(wait, current);
2302 struct comedi_file *cfp = file->private_data;
2303 struct comedi_device *dev = cfp->dev;
2304 bool on_wait_queue = false;
2306 unsigned int old_detach_count;
2308 /* Protect against device detachment during operation. */
2309 down_read(&dev->attach_lock);
2310 attach_locked = true;
2311 old_detach_count = dev->detach_count;
2313 if (!dev->attached) {
2314 dev_dbg(dev->class_dev, "no driver attached\n");
2319 s = comedi_file_write_subdevice(file);
2320 if (!s || !s->async) {
2327 if (!s->busy || !nbytes)
2329 if (s->busy != file) {
2333 if (!(async->cmd.flags & CMDF_WRITE)) {
2338 add_wait_queue(&async->wait_head, &wait);
2339 on_wait_queue = true;
2340 while (nbytes > 0 && !retval) {
2343 set_current_state(TASK_INTERRUPTIBLE);
2345 runflags = comedi_get_subdevice_runflags(s);
2346 if (!comedi_is_runflags_running(runflags)) {
2348 struct comedi_subdevice *new_s;
2350 if (comedi_is_runflags_in_error(runflags))
2355 * To avoid deadlock, cannot acquire dev->mutex
2356 * while dev->attach_lock is held. Need to
2357 * remove task from the async wait queue before
2358 * releasing dev->attach_lock, as it might not
2359 * be valid afterwards.
2361 remove_wait_queue(&async->wait_head, &wait);
2362 on_wait_queue = false;
2363 up_read(&dev->attach_lock);
2364 attach_locked = false;
2365 mutex_lock(&dev->mutex);
2367 * Become non-busy unless things have changed
2368 * behind our back. Checking dev->detach_count
2369 * is unchanged ought to be sufficient (unless
2370 * there have been 2**32 detaches in the
2371 * meantime!), but check the subdevice pointer
2372 * as well just in case.
2374 new_s = comedi_file_write_subdevice(file);
2375 if (dev->attached &&
2376 old_detach_count == dev->detach_count &&
2377 s == new_s && new_s->async == async)
2378 do_become_nonbusy(dev, s);
2379 mutex_unlock(&dev->mutex);
2387 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2388 m = async->prealloc_bufsz - async->buf_write_ptr;
2389 comedi_buf_write_alloc(s, async->prealloc_bufsz);
2390 if (m > comedi_buf_write_n_allocated(s))
2391 m = comedi_buf_write_n_allocated(s);
2396 if (file->f_flags & O_NONBLOCK) {
2401 if (signal_pending(current)) {
2402 retval = -ERESTARTSYS;
2407 if (s->busy != file) {
2411 if (!(async->cmd.flags & CMDF_WRITE)) {
2418 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2424 comedi_buf_write_free(s, n);
2430 break; /* makes device work like a pipe */
2434 remove_wait_queue(&async->wait_head, &wait);
2435 set_current_state(TASK_RUNNING);
2437 up_read(&dev->attach_lock);
2439 return count ? count : retval;
2442 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2445 struct comedi_subdevice *s;
2446 struct comedi_async *async;
2447 int n, m, count = 0, retval = 0;
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 || !nbytes)
2475 if (s->busy != file) {
2479 if (async->cmd.flags & CMDF_WRITE) {
2484 add_wait_queue(&async->wait_head, &wait);
2485 while (nbytes > 0 && !retval) {
2486 set_current_state(TASK_INTERRUPTIBLE);
2490 m = comedi_buf_read_n_available(s);
2491 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2492 m = async->prealloc_bufsz - async->buf_read_ptr;
2497 unsigned runflags = comedi_get_subdevice_runflags(s);
2499 if (!comedi_is_runflags_running(runflags)) {
2500 if (comedi_is_runflags_in_error(runflags))
2504 become_nonbusy = true;
2507 if (file->f_flags & O_NONBLOCK) {
2512 if (signal_pending(current)) {
2513 retval = -ERESTARTSYS;
2520 if (s->busy != file) {
2524 if (async->cmd.flags & CMDF_WRITE) {
2530 m = copy_to_user(buf, async->prealloc_buf +
2531 async->buf_read_ptr, n);
2537 comedi_buf_read_alloc(s, n);
2538 comedi_buf_read_free(s, n);
2544 break; /* makes device work like a pipe */
2546 remove_wait_queue(&async->wait_head, &wait);
2547 set_current_state(TASK_RUNNING);
2548 if (become_nonbusy || comedi_is_subdevice_idle(s)) {
2549 struct comedi_subdevice *new_s;
2552 * To avoid deadlock, cannot acquire dev->mutex
2553 * while dev->attach_lock is held.
2555 up_read(&dev->attach_lock);
2556 attach_locked = false;
2557 mutex_lock(&dev->mutex);
2559 * Check device hasn't become detached behind our back.
2560 * Checking dev->detach_count is unchanged ought to be
2561 * sufficient (unless there have been 2**32 detaches in the
2562 * meantime!), but check the subdevice pointer as well just in
2565 new_s = comedi_file_read_subdevice(file);
2566 if (dev->attached && old_detach_count == dev->detach_count &&
2567 s == new_s && new_s->async == async) {
2568 if (become_nonbusy || comedi_buf_n_bytes_ready(s) == 0)
2569 do_become_nonbusy(dev, s);
2571 mutex_unlock(&dev->mutex);
2575 up_read(&dev->attach_lock);
2577 return count ? count : retval;
2580 static int comedi_open(struct inode *inode, struct file *file)
2582 const unsigned minor = iminor(inode);
2583 struct comedi_file *cfp;
2584 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2588 pr_debug("invalid minor number\n");
2592 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2598 mutex_lock(&dev->mutex);
2599 if (!dev->attached && !capable(CAP_SYS_ADMIN)) {
2600 dev_dbg(dev->class_dev, "not attached and not CAP_SYS_ADMIN\n");
2604 if (dev->attached && dev->use_count == 0) {
2605 if (!try_module_get(dev->driver->module)) {
2610 rc = dev->open(dev);
2612 module_put(dev->driver->module);
2619 file->private_data = cfp;
2620 comedi_file_reset(file);
2624 mutex_unlock(&dev->mutex);
2626 comedi_dev_put(dev);
2632 static int comedi_fasync(int fd, struct file *file, int on)
2634 struct comedi_file *cfp = file->private_data;
2635 struct comedi_device *dev = cfp->dev;
2637 return fasync_helper(fd, file, on, &dev->async_queue);
2640 static int comedi_close(struct inode *inode, struct file *file)
2642 struct comedi_file *cfp = file->private_data;
2643 struct comedi_device *dev = cfp->dev;
2644 struct comedi_subdevice *s = NULL;
2647 mutex_lock(&dev->mutex);
2649 if (dev->subdevices) {
2650 for (i = 0; i < dev->n_subdevices; i++) {
2651 s = &dev->subdevices[i];
2653 if (s->busy == file)
2655 if (s->lock == file)
2659 if (dev->attached && dev->use_count == 1) {
2662 module_put(dev->driver->module);
2667 mutex_unlock(&dev->mutex);
2668 comedi_dev_put(dev);
2674 static const struct file_operations comedi_fops = {
2675 .owner = THIS_MODULE,
2676 .unlocked_ioctl = comedi_unlocked_ioctl,
2677 .compat_ioctl = comedi_compat_ioctl,
2678 .open = comedi_open,
2679 .release = comedi_close,
2680 .read = comedi_read,
2681 .write = comedi_write,
2682 .mmap = comedi_mmap,
2683 .poll = comedi_poll,
2684 .fasync = comedi_fasync,
2685 .llseek = noop_llseek,
2689 * comedi_event - handle events for asynchronous comedi command
2690 * @dev: comedi_device struct
2691 * @s: comedi_subdevice struct associated with dev
2692 * Context: interrupt (usually), s->spin_lock spin-lock not held
2694 * If an asynchronous comedi command is active on the subdevice, process
2695 * any COMEDI_CB_... event flags that have been set, usually by an
2696 * interrupt handler. These may change the run state of the asynchronous
2697 * command, wake a task, and/or send a SIGIO signal.
2699 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2701 struct comedi_async *async = s->async;
2702 unsigned int events;
2704 unsigned long flags;
2706 spin_lock_irqsave(&s->spin_lock, flags);
2708 events = async->events;
2710 if (!__comedi_is_subdevice_running(s)) {
2711 spin_unlock_irqrestore(&s->spin_lock, flags);
2715 if (events & COMEDI_CB_CANCEL_MASK)
2716 __comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
2719 * Remember if an error event has occurred, so an error can be
2720 * returned the next time the user does a read() or write().
2722 if (events & COMEDI_CB_ERROR_MASK)
2723 __comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
2725 if (async->cb_mask & events) {
2726 wake_up_interruptible(&async->wait_head);
2727 si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
2730 spin_unlock_irqrestore(&s->spin_lock, flags);
2733 kill_fasync(&dev->async_queue, SIGIO, si_code);
2735 EXPORT_SYMBOL_GPL(comedi_event);
2737 /* Note: the ->mutex is pre-locked on successful return */
2738 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2740 struct comedi_device *dev;
2741 struct device *csdev;
2744 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2746 return ERR_PTR(-ENOMEM);
2747 comedi_device_init(dev);
2748 comedi_set_hw_dev(dev, hardware_device);
2749 mutex_lock(&dev->mutex);
2750 mutex_lock(&comedi_board_minor_table_lock);
2751 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2752 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2753 if (!comedi_board_minor_table[i]) {
2754 comedi_board_minor_table[i] = dev;
2758 mutex_unlock(&comedi_board_minor_table_lock);
2759 if (i == COMEDI_NUM_BOARD_MINORS) {
2760 mutex_unlock(&dev->mutex);
2761 comedi_device_cleanup(dev);
2762 comedi_dev_put(dev);
2763 dev_err(hardware_device,
2764 "ran out of minor numbers for board device files\n");
2765 return ERR_PTR(-EBUSY);
2768 csdev = device_create(comedi_class, hardware_device,
2769 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2771 dev->class_dev = get_device(csdev);
2773 /* Note: dev->mutex needs to be unlocked by the caller. */
2777 static void comedi_free_board_minor(unsigned minor)
2779 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2780 comedi_free_board_dev(comedi_clear_board_minor(minor));
2783 void comedi_release_hardware_device(struct device *hardware_device)
2786 struct comedi_device *dev;
2788 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2790 mutex_lock(&comedi_board_minor_table_lock);
2791 dev = comedi_board_minor_table[minor];
2792 if (dev && dev->hw_dev == hardware_device) {
2793 comedi_board_minor_table[minor] = NULL;
2794 mutex_unlock(&comedi_board_minor_table_lock);
2795 comedi_free_board_dev(dev);
2798 mutex_unlock(&comedi_board_minor_table_lock);
2802 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2804 struct comedi_device *dev = s->device;
2805 struct device *csdev;
2808 mutex_lock(&comedi_subdevice_minor_table_lock);
2809 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2810 if (!comedi_subdevice_minor_table[i]) {
2811 comedi_subdevice_minor_table[i] = s;
2815 mutex_unlock(&comedi_subdevice_minor_table_lock);
2816 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2817 dev_err(dev->class_dev,
2818 "ran out of minor numbers for subdevice files\n");
2821 i += COMEDI_NUM_BOARD_MINORS;
2823 csdev = device_create(comedi_class, dev->class_dev,
2824 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2825 dev->minor, s->index);
2827 s->class_dev = csdev;
2832 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2838 if (s->minor < COMEDI_NUM_BOARD_MINORS ||
2839 s->minor >= COMEDI_NUM_MINORS)
2842 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2843 mutex_lock(&comedi_subdevice_minor_table_lock);
2844 if (s == comedi_subdevice_minor_table[i])
2845 comedi_subdevice_minor_table[i] = NULL;
2846 mutex_unlock(&comedi_subdevice_minor_table_lock);
2848 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2849 s->class_dev = NULL;
2853 static void comedi_cleanup_board_minors(void)
2857 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2858 comedi_free_board_minor(i);
2861 static int __init comedi_init(void)
2866 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
2868 if (comedi_num_legacy_minors < 0 ||
2869 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2870 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2871 COMEDI_NUM_BOARD_MINORS);
2875 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2876 COMEDI_NUM_MINORS, "comedi");
2879 cdev_init(&comedi_cdev, &comedi_fops);
2880 comedi_cdev.owner = THIS_MODULE;
2882 retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
2884 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2889 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2890 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2894 comedi_class = class_create(THIS_MODULE, "comedi");
2895 if (IS_ERR(comedi_class)) {
2896 pr_err("failed to create class\n");
2897 cdev_del(&comedi_cdev);
2898 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2900 return PTR_ERR(comedi_class);
2903 comedi_class->dev_groups = comedi_dev_groups;
2905 /* XXX requires /proc interface */
2908 /* create devices files for legacy/manual use */
2909 for (i = 0; i < comedi_num_legacy_minors; i++) {
2910 struct comedi_device *dev;
2912 dev = comedi_alloc_board_minor(NULL);
2914 comedi_cleanup_board_minors();
2915 cdev_del(&comedi_cdev);
2916 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2918 return PTR_ERR(dev);
2920 /* comedi_alloc_board_minor() locked the mutex */
2921 mutex_unlock(&dev->mutex);
2926 module_init(comedi_init);
2928 static void __exit comedi_cleanup(void)
2930 comedi_cleanup_board_minors();
2931 class_destroy(comedi_class);
2932 cdev_del(&comedi_cdev);
2933 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2935 comedi_proc_cleanup();
2937 module_exit(comedi_cleanup);
2939 MODULE_AUTHOR("http://www.comedi.org");
2940 MODULE_DESCRIPTION("Comedi core module");
2941 MODULE_LICENSE("GPL");