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, 0444);
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, 0644);
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, 0644);
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 int 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 int 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 int 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 *
237 comedi_dev_get_from_subdevice_minor(unsigned int minor)
239 struct comedi_device *dev;
240 struct comedi_subdevice *s;
241 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
243 mutex_lock(&comedi_subdevice_minor_table_lock);
244 s = comedi_subdevice_minor_table[i];
245 dev = comedi_dev_get(s ? s->device : NULL);
246 mutex_unlock(&comedi_subdevice_minor_table_lock);
251 * comedi_dev_get_from_minor() - Get COMEDI device by minor device number
252 * @minor: Minor device number.
254 * Finds the COMEDI device associated with the minor device number, if any,
255 * and increments its reference count. The COMEDI device is prevented from
256 * being freed until a matching call is made to comedi_dev_put().
258 * Return: A pointer to the COMEDI device if it exists, with its usage
259 * reference incremented. Return NULL if no COMEDI device exists with the
260 * specified minor device number.
262 struct comedi_device *comedi_dev_get_from_minor(unsigned int minor)
264 if (minor < COMEDI_NUM_BOARD_MINORS)
265 return comedi_dev_get_from_board_minor(minor);
267 return comedi_dev_get_from_subdevice_minor(minor);
269 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
271 static struct comedi_subdevice *
272 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
274 struct comedi_subdevice *s;
276 if (minor >= COMEDI_NUM_BOARD_MINORS) {
277 s = comedi_subdevice_from_minor(dev, minor);
278 if (!s || (s->subdev_flags & SDF_CMD_READ))
281 return dev->read_subdev;
284 static struct comedi_subdevice *
285 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
287 struct comedi_subdevice *s;
289 if (minor >= COMEDI_NUM_BOARD_MINORS) {
290 s = comedi_subdevice_from_minor(dev, minor);
291 if (!s || (s->subdev_flags & SDF_CMD_WRITE))
294 return dev->write_subdev;
297 static void comedi_file_reset(struct file *file)
299 struct comedi_file *cfp = file->private_data;
300 struct comedi_device *dev = cfp->dev;
301 struct comedi_subdevice *s, *read_s, *write_s;
302 unsigned int minor = iminor(file_inode(file));
304 read_s = dev->read_subdev;
305 write_s = dev->write_subdev;
306 if (minor >= COMEDI_NUM_BOARD_MINORS) {
307 s = comedi_subdevice_from_minor(dev, minor);
308 if (!s || s->subdev_flags & SDF_CMD_READ)
310 if (!s || s->subdev_flags & SDF_CMD_WRITE)
313 cfp->last_attached = dev->attached;
314 cfp->last_detach_count = dev->detach_count;
315 WRITE_ONCE(cfp->read_subdev, read_s);
316 WRITE_ONCE(cfp->write_subdev, write_s);
319 static void comedi_file_check(struct file *file)
321 struct comedi_file *cfp = file->private_data;
322 struct comedi_device *dev = cfp->dev;
324 if (cfp->last_attached != dev->attached ||
325 cfp->last_detach_count != dev->detach_count)
326 comedi_file_reset(file);
329 static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file)
331 struct comedi_file *cfp = file->private_data;
333 comedi_file_check(file);
334 return READ_ONCE(cfp->read_subdev);
337 static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file)
339 struct comedi_file *cfp = file->private_data;
341 comedi_file_check(file);
342 return READ_ONCE(cfp->write_subdev);
345 static int resize_async_buffer(struct comedi_device *dev,
346 struct comedi_subdevice *s,
347 unsigned int new_size)
349 struct comedi_async *async = s->async;
352 if (new_size > async->max_bufsize)
356 dev_dbg(dev->class_dev,
357 "subdevice is busy, cannot resize buffer\n");
360 if (comedi_buf_is_mmapped(s)) {
361 dev_dbg(dev->class_dev,
362 "subdevice is mmapped, cannot resize buffer\n");
366 /* make sure buffer is an integral number of pages (we round up) */
367 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
369 retval = comedi_buf_alloc(dev, s, new_size);
374 retval = s->buf_change(dev, s);
379 dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
380 s->index, async->prealloc_bufsz);
384 /* sysfs attribute files */
386 static ssize_t max_read_buffer_kb_show(struct device *csdev,
387 struct device_attribute *attr, char *buf)
389 unsigned int minor = MINOR(csdev->devt);
390 struct comedi_device *dev;
391 struct comedi_subdevice *s;
392 unsigned int size = 0;
394 dev = comedi_dev_get_from_minor(minor);
398 mutex_lock(&dev->mutex);
399 s = comedi_read_subdevice(dev, minor);
400 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
401 size = s->async->max_bufsize / 1024;
402 mutex_unlock(&dev->mutex);
405 return snprintf(buf, PAGE_SIZE, "%u\n", size);
408 static ssize_t max_read_buffer_kb_store(struct device *csdev,
409 struct device_attribute *attr,
410 const char *buf, size_t count)
412 unsigned int minor = MINOR(csdev->devt);
413 struct comedi_device *dev;
414 struct comedi_subdevice *s;
418 err = kstrtouint(buf, 10, &size);
421 if (size > (UINT_MAX / 1024))
425 dev = comedi_dev_get_from_minor(minor);
429 mutex_lock(&dev->mutex);
430 s = comedi_read_subdevice(dev, minor);
431 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
432 s->async->max_bufsize = size;
435 mutex_unlock(&dev->mutex);
438 return err ? err : count;
440 static DEVICE_ATTR_RW(max_read_buffer_kb);
442 static ssize_t read_buffer_kb_show(struct device *csdev,
443 struct device_attribute *attr, char *buf)
445 unsigned int minor = MINOR(csdev->devt);
446 struct comedi_device *dev;
447 struct comedi_subdevice *s;
448 unsigned int size = 0;
450 dev = comedi_dev_get_from_minor(minor);
454 mutex_lock(&dev->mutex);
455 s = comedi_read_subdevice(dev, minor);
456 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
457 size = s->async->prealloc_bufsz / 1024;
458 mutex_unlock(&dev->mutex);
461 return snprintf(buf, PAGE_SIZE, "%u\n", size);
464 static ssize_t read_buffer_kb_store(struct device *csdev,
465 struct device_attribute *attr,
466 const char *buf, size_t count)
468 unsigned int minor = MINOR(csdev->devt);
469 struct comedi_device *dev;
470 struct comedi_subdevice *s;
474 err = kstrtouint(buf, 10, &size);
477 if (size > (UINT_MAX / 1024))
481 dev = comedi_dev_get_from_minor(minor);
485 mutex_lock(&dev->mutex);
486 s = comedi_read_subdevice(dev, minor);
487 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
488 err = resize_async_buffer(dev, s, size);
491 mutex_unlock(&dev->mutex);
494 return err ? err : count;
496 static DEVICE_ATTR_RW(read_buffer_kb);
498 static ssize_t max_write_buffer_kb_show(struct device *csdev,
499 struct device_attribute *attr,
502 unsigned int minor = MINOR(csdev->devt);
503 struct comedi_device *dev;
504 struct comedi_subdevice *s;
505 unsigned int size = 0;
507 dev = comedi_dev_get_from_minor(minor);
511 mutex_lock(&dev->mutex);
512 s = comedi_write_subdevice(dev, minor);
513 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
514 size = s->async->max_bufsize / 1024;
515 mutex_unlock(&dev->mutex);
518 return snprintf(buf, PAGE_SIZE, "%u\n", size);
521 static ssize_t max_write_buffer_kb_store(struct device *csdev,
522 struct device_attribute *attr,
523 const char *buf, size_t count)
525 unsigned int minor = MINOR(csdev->devt);
526 struct comedi_device *dev;
527 struct comedi_subdevice *s;
531 err = kstrtouint(buf, 10, &size);
534 if (size > (UINT_MAX / 1024))
538 dev = comedi_dev_get_from_minor(minor);
542 mutex_lock(&dev->mutex);
543 s = comedi_write_subdevice(dev, minor);
544 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
545 s->async->max_bufsize = size;
548 mutex_unlock(&dev->mutex);
551 return err ? err : count;
553 static DEVICE_ATTR_RW(max_write_buffer_kb);
555 static ssize_t write_buffer_kb_show(struct device *csdev,
556 struct device_attribute *attr, char *buf)
558 unsigned int minor = MINOR(csdev->devt);
559 struct comedi_device *dev;
560 struct comedi_subdevice *s;
561 unsigned int size = 0;
563 dev = comedi_dev_get_from_minor(minor);
567 mutex_lock(&dev->mutex);
568 s = comedi_write_subdevice(dev, minor);
569 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
570 size = s->async->prealloc_bufsz / 1024;
571 mutex_unlock(&dev->mutex);
574 return snprintf(buf, PAGE_SIZE, "%u\n", size);
577 static ssize_t write_buffer_kb_store(struct device *csdev,
578 struct device_attribute *attr,
579 const char *buf, size_t count)
581 unsigned int minor = MINOR(csdev->devt);
582 struct comedi_device *dev;
583 struct comedi_subdevice *s;
587 err = kstrtouint(buf, 10, &size);
590 if (size > (UINT_MAX / 1024))
594 dev = comedi_dev_get_from_minor(minor);
598 mutex_lock(&dev->mutex);
599 s = comedi_write_subdevice(dev, minor);
600 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
601 err = resize_async_buffer(dev, s, size);
604 mutex_unlock(&dev->mutex);
607 return err ? err : count;
609 static DEVICE_ATTR_RW(write_buffer_kb);
611 static struct attribute *comedi_dev_attrs[] = {
612 &dev_attr_max_read_buffer_kb.attr,
613 &dev_attr_read_buffer_kb.attr,
614 &dev_attr_max_write_buffer_kb.attr,
615 &dev_attr_write_buffer_kb.attr,
618 ATTRIBUTE_GROUPS(comedi_dev);
620 static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s,
623 s->runflags &= ~bits;
626 static void __comedi_set_subdevice_runflags(struct comedi_subdevice *s,
632 static void comedi_update_subdevice_runflags(struct comedi_subdevice *s,
638 spin_lock_irqsave(&s->spin_lock, flags);
639 __comedi_clear_subdevice_runflags(s, mask);
640 __comedi_set_subdevice_runflags(s, bits & mask);
641 spin_unlock_irqrestore(&s->spin_lock, flags);
644 static unsigned int __comedi_get_subdevice_runflags(struct comedi_subdevice *s)
649 static unsigned int comedi_get_subdevice_runflags(struct comedi_subdevice *s)
652 unsigned int runflags;
654 spin_lock_irqsave(&s->spin_lock, flags);
655 runflags = __comedi_get_subdevice_runflags(s);
656 spin_unlock_irqrestore(&s->spin_lock, flags);
660 static bool comedi_is_runflags_running(unsigned int runflags)
662 return runflags & COMEDI_SRF_RUNNING;
665 static bool comedi_is_runflags_in_error(unsigned int runflags)
667 return runflags & COMEDI_SRF_ERROR;
671 * comedi_is_subdevice_running() - Check if async command running on subdevice
672 * @s: COMEDI subdevice.
674 * Return: %true if an asynchronous COMEDI command is active on the
675 * subdevice, else %false.
677 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
679 unsigned int runflags = comedi_get_subdevice_runflags(s);
681 return comedi_is_runflags_running(runflags);
683 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
685 static bool __comedi_is_subdevice_running(struct comedi_subdevice *s)
687 unsigned int runflags = __comedi_get_subdevice_runflags(s);
689 return comedi_is_runflags_running(runflags);
692 bool comedi_can_auto_free_spriv(struct comedi_subdevice *s)
694 unsigned int runflags = __comedi_get_subdevice_runflags(s);
696 return runflags & COMEDI_SRF_FREE_SPRIV;
700 * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
701 * @s: COMEDI subdevice.
703 * Mark the subdevice as having a pointer to private data that can be
704 * automatically freed when the COMEDI device is detached from the low-level
707 void comedi_set_spriv_auto_free(struct comedi_subdevice *s)
709 __comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV);
711 EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free);
714 * comedi_alloc_spriv - Allocate memory for the subdevice private data
715 * @s: COMEDI subdevice.
716 * @size: Size of the memory to allocate.
718 * Allocate memory for the subdevice private data and point @s->private
719 * to it. The memory will be freed automatically when the COMEDI device
720 * is detached from the low-level driver.
722 * Return: A pointer to the allocated memory @s->private on success.
723 * Return NULL on failure.
725 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
727 s->private = kzalloc(size, GFP_KERNEL);
729 comedi_set_spriv_auto_free(s);
732 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
735 * This function restores a subdevice to an idle state.
737 static void do_become_nonbusy(struct comedi_device *dev,
738 struct comedi_subdevice *s)
740 struct comedi_async *async = s->async;
742 comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
745 async->inttrig = NULL;
746 kfree(async->cmd.chanlist);
747 async->cmd.chanlist = NULL;
749 wake_up_interruptible_all(&async->wait_head);
751 dev_err(dev->class_dev,
752 "BUG: (?) do_become_nonbusy called with async=NULL\n");
757 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
761 if (comedi_is_subdevice_running(s) && s->cancel)
762 ret = s->cancel(dev, s);
764 do_become_nonbusy(dev, s);
769 void comedi_device_cancel_all(struct comedi_device *dev)
771 struct comedi_subdevice *s;
777 for (i = 0; i < dev->n_subdevices; i++) {
778 s = &dev->subdevices[i];
784 static int is_device_busy(struct comedi_device *dev)
786 struct comedi_subdevice *s;
792 for (i = 0; i < dev->n_subdevices; i++) {
793 s = &dev->subdevices[i];
796 if (s->async && comedi_buf_is_mmapped(s))
804 * COMEDI_DEVCONFIG ioctl
805 * attaches (and configures) or detaches a legacy device
808 * pointer to comedi_devconfig structure (NULL if detaching)
811 * comedi_devconfig structure (if attaching)
816 static int do_devconfig_ioctl(struct comedi_device *dev,
817 struct comedi_devconfig __user *arg)
819 struct comedi_devconfig it;
821 if (!capable(CAP_SYS_ADMIN))
825 if (is_device_busy(dev))
828 struct module *driver_module = dev->driver->module;
830 comedi_device_detach(dev);
831 module_put(driver_module);
836 if (copy_from_user(&it, arg, sizeof(it)))
839 it.board_name[COMEDI_NAMELEN - 1] = 0;
841 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
842 dev_warn(dev->class_dev,
843 "comedi_config --init_data is deprecated\n");
847 if (dev->minor >= comedi_num_legacy_minors)
848 /* don't re-use dynamically allocated comedi devices */
851 /* This increments the driver module count on success. */
852 return comedi_device_attach(dev, &it);
856 * COMEDI_BUFCONFIG ioctl
857 * buffer configuration
860 * pointer to comedi_bufconfig structure
863 * comedi_bufconfig structure
866 * modified comedi_bufconfig structure
868 static int do_bufconfig_ioctl(struct comedi_device *dev,
869 struct comedi_bufconfig __user *arg)
871 struct comedi_bufconfig bc;
872 struct comedi_async *async;
873 struct comedi_subdevice *s;
876 if (copy_from_user(&bc, arg, sizeof(bc)))
879 if (bc.subdevice >= dev->n_subdevices)
882 s = &dev->subdevices[bc.subdevice];
886 dev_dbg(dev->class_dev,
887 "subdevice does not have async capability\n");
893 if (bc.maximum_size) {
894 if (!capable(CAP_SYS_ADMIN))
897 async->max_bufsize = bc.maximum_size;
901 retval = resize_async_buffer(dev, s, bc.size);
906 bc.size = async->prealloc_bufsz;
907 bc.maximum_size = async->max_bufsize;
910 if (copy_to_user(arg, &bc, sizeof(bc)))
917 * COMEDI_DEVINFO ioctl
921 * pointer to comedi_devinfo structure
927 * comedi_devinfo structure
929 static int do_devinfo_ioctl(struct comedi_device *dev,
930 struct comedi_devinfo __user *arg,
933 struct comedi_subdevice *s;
934 struct comedi_devinfo devinfo;
936 memset(&devinfo, 0, sizeof(devinfo));
938 /* fill devinfo structure */
939 devinfo.version_code = COMEDI_VERSION_CODE;
940 devinfo.n_subdevs = dev->n_subdevices;
941 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
942 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
944 s = comedi_file_read_subdevice(file);
946 devinfo.read_subdevice = s->index;
948 devinfo.read_subdevice = -1;
950 s = comedi_file_write_subdevice(file);
952 devinfo.write_subdevice = s->index;
954 devinfo.write_subdevice = -1;
956 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
963 * COMEDI_SUBDINFO ioctl
967 * pointer to array of comedi_subdinfo structures
973 * array of comedi_subdinfo structures
975 static int do_subdinfo_ioctl(struct comedi_device *dev,
976 struct comedi_subdinfo __user *arg, void *file)
979 struct comedi_subdinfo *tmp, *us;
980 struct comedi_subdevice *s;
982 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
986 /* fill subdinfo structs */
987 for (i = 0; i < dev->n_subdevices; i++) {
988 s = &dev->subdevices[i];
992 us->n_chan = s->n_chan;
993 us->subd_flags = s->subdev_flags;
994 if (comedi_is_subdevice_running(s))
995 us->subd_flags |= SDF_RUNNING;
996 #define TIMER_nanosec 5 /* backwards compatibility */
997 us->timer_type = TIMER_nanosec;
998 us->len_chanlist = s->len_chanlist;
999 us->maxdata = s->maxdata;
1000 if (s->range_table) {
1002 (i << 24) | (0 << 16) | (s->range_table->length);
1004 us->range_type = 0; /* XXX */
1008 us->subd_flags |= SDF_BUSY;
1009 if (s->busy == file)
1010 us->subd_flags |= SDF_BUSY_OWNER;
1012 us->subd_flags |= SDF_LOCKED;
1013 if (s->lock == file)
1014 us->subd_flags |= SDF_LOCK_OWNER;
1015 if (!s->maxdata && s->maxdata_list)
1016 us->subd_flags |= SDF_MAXDATA;
1017 if (s->range_table_list)
1018 us->subd_flags |= SDF_RANGETYPE;
1020 us->subd_flags |= SDF_CMD;
1022 if (s->insn_bits != &insn_inval)
1023 us->insn_bits_support = COMEDI_SUPPORTED;
1025 us->insn_bits_support = COMEDI_UNSUPPORTED;
1028 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
1032 return ret ? -EFAULT : 0;
1036 * COMEDI_CHANINFO ioctl
1037 * subdevice channel info
1040 * pointer to comedi_chaninfo structure
1043 * comedi_chaninfo structure
1046 * array of maxdata values to chaninfo->maxdata_list if requested
1047 * array of range table lengths to chaninfo->range_table_list if requested
1049 static int do_chaninfo_ioctl(struct comedi_device *dev,
1050 struct comedi_chaninfo __user *arg)
1052 struct comedi_subdevice *s;
1053 struct comedi_chaninfo it;
1055 if (copy_from_user(&it, arg, sizeof(it)))
1058 if (it.subdev >= dev->n_subdevices)
1060 s = &dev->subdevices[it.subdev];
1062 if (it.maxdata_list) {
1063 if (s->maxdata || !s->maxdata_list)
1065 if (copy_to_user(it.maxdata_list, s->maxdata_list,
1066 s->n_chan * sizeof(unsigned int)))
1071 return -EINVAL; /* flaglist not supported */
1076 if (!s->range_table_list)
1078 for (i = 0; i < s->n_chan; i++) {
1081 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
1082 (s->range_table_list[i]->length);
1083 if (put_user(x, it.rangelist + i))
1092 * COMEDI_BUFINFO ioctl
1093 * buffer information
1096 * pointer to comedi_bufinfo structure
1099 * comedi_bufinfo structure
1102 * modified comedi_bufinfo structure
1104 static int do_bufinfo_ioctl(struct comedi_device *dev,
1105 struct comedi_bufinfo __user *arg, void *file)
1107 struct comedi_bufinfo bi;
1108 struct comedi_subdevice *s;
1109 struct comedi_async *async;
1110 unsigned int runflags;
1112 bool become_nonbusy = false;
1114 if (copy_from_user(&bi, arg, sizeof(bi)))
1117 if (bi.subdevice >= dev->n_subdevices)
1120 s = &dev->subdevices[bi.subdevice];
1124 if (!async || s->busy != file)
1127 runflags = comedi_get_subdevice_runflags(s);
1128 if (!(async->cmd.flags & CMDF_WRITE)) {
1129 /* command was set up in "read" direction */
1130 if (bi.bytes_read) {
1131 comedi_buf_read_alloc(s, bi.bytes_read);
1132 bi.bytes_read = comedi_buf_read_free(s, bi.bytes_read);
1135 * If nothing left to read, and command has stopped, and
1136 * {"read" position not updated or command stopped normally},
1137 * then become non-busy.
1139 if (comedi_buf_read_n_available(s) == 0 &&
1140 !comedi_is_runflags_running(runflags) &&
1141 (bi.bytes_read == 0 ||
1142 !comedi_is_runflags_in_error(runflags))) {
1143 become_nonbusy = true;
1144 if (comedi_is_runflags_in_error(runflags))
1147 bi.bytes_written = 0;
1149 /* command was set up in "write" direction */
1150 if (!comedi_is_runflags_running(runflags)) {
1151 bi.bytes_written = 0;
1152 become_nonbusy = true;
1153 if (comedi_is_runflags_in_error(runflags))
1155 } else if (bi.bytes_written) {
1156 comedi_buf_write_alloc(s, bi.bytes_written);
1158 comedi_buf_write_free(s, bi.bytes_written);
1163 bi.buf_write_count = async->buf_write_count;
1164 bi.buf_write_ptr = async->buf_write_ptr;
1165 bi.buf_read_count = async->buf_read_count;
1166 bi.buf_read_ptr = async->buf_read_ptr;
1169 do_become_nonbusy(dev, s);
1174 if (copy_to_user(arg, &bi, sizeof(bi)))
1180 static int check_insn_config_length(struct comedi_insn *insn,
1187 case INSN_CONFIG_DIO_OUTPUT:
1188 case INSN_CONFIG_DIO_INPUT:
1189 case INSN_CONFIG_DISARM:
1190 case INSN_CONFIG_RESET:
1194 case INSN_CONFIG_ARM:
1195 case INSN_CONFIG_DIO_QUERY:
1196 case INSN_CONFIG_BLOCK_SIZE:
1197 case INSN_CONFIG_FILTER:
1198 case INSN_CONFIG_SERIAL_CLOCK:
1199 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1200 case INSN_CONFIG_ALT_SOURCE:
1201 case INSN_CONFIG_SET_COUNTER_MODE:
1202 case INSN_CONFIG_8254_READ_STATUS:
1203 case INSN_CONFIG_SET_ROUTING:
1204 case INSN_CONFIG_GET_ROUTING:
1205 case INSN_CONFIG_GET_PWM_STATUS:
1206 case INSN_CONFIG_PWM_SET_PERIOD:
1207 case INSN_CONFIG_PWM_GET_PERIOD:
1211 case INSN_CONFIG_SET_GATE_SRC:
1212 case INSN_CONFIG_GET_GATE_SRC:
1213 case INSN_CONFIG_SET_CLOCK_SRC:
1214 case INSN_CONFIG_GET_CLOCK_SRC:
1215 case INSN_CONFIG_SET_OTHER_SRC:
1216 case INSN_CONFIG_GET_COUNTER_STATUS:
1217 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1218 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1219 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1223 case INSN_CONFIG_PWM_OUTPUT:
1224 case INSN_CONFIG_ANALOG_TRIG:
1228 case INSN_CONFIG_DIGITAL_TRIG:
1233 * by default we allow the insn since we don't have checks for
1234 * all possible cases yet
1237 pr_warn("No check for data length of config insn id %i is implemented\n",
1239 pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1240 pr_warn("Assuming n=%i is correct\n", insn->n);
1246 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1247 unsigned int *data, void *file)
1249 struct comedi_subdevice *s;
1253 if (insn->insn & INSN_MASK_SPECIAL) {
1254 /* a non-subdevice instruction */
1256 switch (insn->insn) {
1259 struct timespec64 tv;
1266 ktime_get_real_ts64(&tv);
1267 /* unsigned data safe until 2106 */
1268 data[0] = (unsigned int)tv.tv_sec;
1269 data[1] = tv.tv_nsec / NSEC_PER_USEC;
1275 if (insn->n != 1 || data[0] >= 100000) {
1279 udelay(data[0] / 1000);
1287 if (insn->subdev >= dev->n_subdevices) {
1288 dev_dbg(dev->class_dev,
1289 "%d not usable subdevice\n",
1294 s = &dev->subdevices[insn->subdev];
1296 dev_dbg(dev->class_dev, "no async\n");
1300 if (!s->async->inttrig) {
1301 dev_dbg(dev->class_dev, "no inttrig\n");
1305 ret = s->async->inttrig(dev, s, data[0]);
1310 dev_dbg(dev->class_dev, "invalid insn\n");
1315 /* a subdevice instruction */
1316 unsigned int maxdata;
1318 if (insn->subdev >= dev->n_subdevices) {
1319 dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1324 s = &dev->subdevices[insn->subdev];
1326 if (s->type == COMEDI_SUBD_UNUSED) {
1327 dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1333 /* are we locked? (ioctl lock) */
1334 if (s->lock && s->lock != file) {
1335 dev_dbg(dev->class_dev, "device locked\n");
1340 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1343 dev_dbg(dev->class_dev, "bad chanspec\n");
1351 /* This looks arbitrary. It is. */
1352 s->busy = parse_insn;
1353 switch (insn->insn) {
1355 ret = s->insn_read(dev, s, insn, data);
1356 if (ret == -ETIMEDOUT) {
1357 dev_dbg(dev->class_dev,
1358 "subdevice %d read instruction timed out\n",
1363 maxdata = s->maxdata_list
1364 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1366 for (i = 0; i < insn->n; ++i) {
1367 if (data[i] > maxdata) {
1369 dev_dbg(dev->class_dev,
1370 "bad data value(s)\n");
1375 ret = s->insn_write(dev, s, insn, data);
1376 if (ret == -ETIMEDOUT) {
1377 dev_dbg(dev->class_dev,
1378 "subdevice %d write instruction timed out\n",
1388 * Most drivers ignore the base channel in
1389 * insn->chanspec. Fix this here if
1390 * the subdevice has <= 32 channels.
1392 unsigned int orig_mask = data[0];
1393 unsigned int shift = 0;
1395 if (s->n_chan <= 32) {
1396 shift = CR_CHAN(insn->chanspec);
1403 ret = s->insn_bits(dev, s, insn, data);
1404 data[0] = orig_mask;
1410 ret = check_insn_config_length(insn, data);
1413 ret = s->insn_config(dev, s, insn, data);
1428 * COMEDI_INSNLIST ioctl
1429 * synchronous instruction list
1432 * pointer to comedi_insnlist structure
1435 * comedi_insnlist structure
1436 * array of comedi_insn structures from insnlist->insns pointer
1437 * data (for writes) from insns[].data pointers
1440 * data (for reads) to insns[].data pointers
1442 /* arbitrary limits */
1443 #define MAX_SAMPLES 256
1444 static int do_insnlist_ioctl(struct comedi_device *dev,
1445 struct comedi_insnlist __user *arg, void *file)
1447 struct comedi_insnlist insnlist;
1448 struct comedi_insn *insns = NULL;
1449 unsigned int *data = NULL;
1453 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1456 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1462 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1468 if (copy_from_user(insns, insnlist.insns,
1469 sizeof(*insns) * insnlist.n_insns)) {
1470 dev_dbg(dev->class_dev, "copy_from_user failed\n");
1475 for (i = 0; i < insnlist.n_insns; i++) {
1476 if (insns[i].n > MAX_SAMPLES) {
1477 dev_dbg(dev->class_dev,
1478 "number of samples too large\n");
1482 if (insns[i].insn & INSN_MASK_WRITE) {
1483 if (copy_from_user(data, insns[i].data,
1484 insns[i].n * sizeof(unsigned int))) {
1485 dev_dbg(dev->class_dev,
1486 "copy_from_user failed\n");
1491 ret = parse_insn(dev, insns + i, data, file);
1494 if (insns[i].insn & INSN_MASK_READ) {
1495 if (copy_to_user(insns[i].data, data,
1496 insns[i].n * sizeof(unsigned int))) {
1497 dev_dbg(dev->class_dev,
1498 "copy_to_user failed\n");
1518 * synchronous instruction
1521 * pointer to comedi_insn structure
1524 * comedi_insn structure
1525 * data (for writes) from insn->data pointer
1528 * data (for reads) to insn->data pointer
1530 static int do_insn_ioctl(struct comedi_device *dev,
1531 struct comedi_insn __user *arg, void *file)
1533 struct comedi_insn insn;
1534 unsigned int *data = NULL;
1537 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1543 if (copy_from_user(&insn, arg, sizeof(insn))) {
1548 /* This is where the behavior of insn and insnlist deviate. */
1549 if (insn.n > MAX_SAMPLES)
1550 insn.n = MAX_SAMPLES;
1551 if (insn.insn & INSN_MASK_WRITE) {
1552 if (copy_from_user(data,
1554 insn.n * sizeof(unsigned int))) {
1559 ret = parse_insn(dev, &insn, data, file);
1562 if (insn.insn & INSN_MASK_READ) {
1563 if (copy_to_user(insn.data,
1565 insn.n * sizeof(unsigned int))) {
1578 static int __comedi_get_user_cmd(struct comedi_device *dev,
1579 struct comedi_cmd __user *arg,
1580 struct comedi_cmd *cmd)
1582 struct comedi_subdevice *s;
1584 if (copy_from_user(cmd, arg, sizeof(*cmd))) {
1585 dev_dbg(dev->class_dev, "bad cmd address\n");
1589 if (cmd->subdev >= dev->n_subdevices) {
1590 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
1594 s = &dev->subdevices[cmd->subdev];
1596 if (s->type == COMEDI_SUBD_UNUSED) {
1597 dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1602 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1603 dev_dbg(dev->class_dev,
1604 "subdevice %d does not support commands\n",
1609 /* make sure channel/gain list isn't too long */
1610 if (cmd->chanlist_len > s->len_chanlist) {
1611 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1612 cmd->chanlist_len, s->len_chanlist);
1617 * Set the CMDF_WRITE flag to the correct state if the subdevice
1618 * supports only "read" commands or only "write" commands.
1620 switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
1622 cmd->flags &= ~CMDF_WRITE;
1625 cmd->flags |= CMDF_WRITE;
1634 static int __comedi_get_user_chanlist(struct comedi_device *dev,
1635 struct comedi_subdevice *s,
1636 unsigned int __user *user_chanlist,
1637 struct comedi_cmd *cmd)
1639 unsigned int *chanlist;
1642 cmd->chanlist = NULL;
1643 chanlist = memdup_user(user_chanlist,
1644 cmd->chanlist_len * sizeof(unsigned int));
1645 if (IS_ERR(chanlist))
1646 return PTR_ERR(chanlist);
1648 /* make sure each element in channel/gain list is valid */
1649 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1655 cmd->chanlist = chanlist;
1662 * asynchronous acquisition command set-up
1665 * pointer to comedi_cmd structure
1668 * comedi_cmd structure
1669 * channel/range list from cmd->chanlist pointer
1672 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1674 static int do_cmd_ioctl(struct comedi_device *dev,
1675 struct comedi_cmd __user *arg, void *file)
1677 struct comedi_cmd cmd;
1678 struct comedi_subdevice *s;
1679 struct comedi_async *async;
1680 unsigned int __user *user_chanlist;
1683 /* get the user's cmd and do some simple validation */
1684 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1688 /* save user's chanlist pointer so it can be restored later */
1689 user_chanlist = (unsigned int __user *)cmd.chanlist;
1691 s = &dev->subdevices[cmd.subdev];
1694 /* are we locked? (ioctl lock) */
1695 if (s->lock && s->lock != file) {
1696 dev_dbg(dev->class_dev, "subdevice locked\n");
1702 dev_dbg(dev->class_dev, "subdevice busy\n");
1706 /* make sure channel/gain list isn't too short */
1707 if (cmd.chanlist_len < 1) {
1708 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
1714 async->cmd.data = NULL;
1716 /* load channel/gain list */
1717 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1721 ret = s->do_cmdtest(dev, s, &async->cmd);
1723 if (async->cmd.flags & CMDF_BOGUS || ret) {
1724 dev_dbg(dev->class_dev, "test returned %d\n", ret);
1726 /* restore chanlist pointer before copying back */
1727 cmd.chanlist = (unsigned int __force *)user_chanlist;
1729 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1730 dev_dbg(dev->class_dev, "fault writing cmd\n");
1738 if (!async->prealloc_bufsz) {
1740 dev_dbg(dev->class_dev, "no buffer (?)\n");
1744 comedi_buf_reset(s);
1746 async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
1747 if (async->cmd.flags & CMDF_WAKE_EOS)
1748 async->cb_mask |= COMEDI_CB_EOS;
1750 comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
1751 COMEDI_SRF_RUNNING);
1754 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1755 * race with comedi_read() or comedi_write().
1758 ret = s->do_cmd(dev, s);
1763 do_become_nonbusy(dev, s);
1769 * COMEDI_CMDTEST ioctl
1770 * asynchronous acquisition command testing
1773 * pointer to comedi_cmd structure
1776 * comedi_cmd structure
1777 * channel/range list from cmd->chanlist pointer
1780 * possibly modified comedi_cmd structure
1782 static int do_cmdtest_ioctl(struct comedi_device *dev,
1783 struct comedi_cmd __user *arg, void *file)
1785 struct comedi_cmd cmd;
1786 struct comedi_subdevice *s;
1787 unsigned int __user *user_chanlist;
1790 /* get the user's cmd and do some simple validation */
1791 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1795 /* save user's chanlist pointer so it can be restored later */
1796 user_chanlist = (unsigned int __user *)cmd.chanlist;
1798 s = &dev->subdevices[cmd.subdev];
1800 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1801 if (user_chanlist) {
1802 /* load channel/gain list */
1803 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1808 ret = s->do_cmdtest(dev, s, &cmd);
1810 kfree(cmd.chanlist); /* free kernel copy of user chanlist */
1812 /* restore chanlist pointer before copying back */
1813 cmd.chanlist = (unsigned int __force *)user_chanlist;
1815 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1816 dev_dbg(dev->class_dev, "bad cmd address\n");
1836 static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
1840 unsigned long flags;
1841 struct comedi_subdevice *s;
1843 if (arg >= dev->n_subdevices)
1845 s = &dev->subdevices[arg];
1847 spin_lock_irqsave(&s->spin_lock, flags);
1848 if (s->busy || s->lock)
1852 spin_unlock_irqrestore(&s->spin_lock, flags);
1858 * COMEDI_UNLOCK ioctl
1870 static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
1873 struct comedi_subdevice *s;
1875 if (arg >= dev->n_subdevices)
1877 s = &dev->subdevices[arg];
1882 if (s->lock && s->lock != file)
1885 if (s->lock == file)
1892 * COMEDI_CANCEL ioctl
1893 * cancel asynchronous acquisition
1904 static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
1907 struct comedi_subdevice *s;
1909 if (arg >= dev->n_subdevices)
1911 s = &dev->subdevices[arg];
1918 if (s->busy != file)
1921 return do_cancel(dev, s);
1926 * instructs driver to synchronize buffers
1937 static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
1940 struct comedi_subdevice *s;
1942 if (arg >= dev->n_subdevices)
1944 s = &dev->subdevices[arg];
1949 if (s->busy != file)
1953 return s->poll(dev, s);
1959 * COMEDI_SETRSUBD ioctl
1960 * sets the current "read" subdevice on a per-file basis
1971 static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1974 struct comedi_file *cfp = file->private_data;
1975 struct comedi_subdevice *s_old, *s_new;
1977 if (arg >= dev->n_subdevices)
1980 s_new = &dev->subdevices[arg];
1981 s_old = comedi_file_read_subdevice(file);
1983 return 0; /* no change */
1985 if (!(s_new->subdev_flags & SDF_CMD_READ))
1989 * Check the file isn't still busy handling a "read" command on the
1990 * old subdevice (if any).
1992 if (s_old && s_old->busy == file && s_old->async &&
1993 !(s_old->async->cmd.flags & CMDF_WRITE))
1996 WRITE_ONCE(cfp->read_subdev, s_new);
2001 * COMEDI_SETWSUBD ioctl
2002 * sets the current "write" subdevice on a per-file basis
2013 static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
2016 struct comedi_file *cfp = file->private_data;
2017 struct comedi_subdevice *s_old, *s_new;
2019 if (arg >= dev->n_subdevices)
2022 s_new = &dev->subdevices[arg];
2023 s_old = comedi_file_write_subdevice(file);
2025 return 0; /* no change */
2027 if (!(s_new->subdev_flags & SDF_CMD_WRITE))
2031 * Check the file isn't still busy handling a "write" command on the
2032 * old subdevice (if any).
2034 if (s_old && s_old->busy == file && s_old->async &&
2035 (s_old->async->cmd.flags & CMDF_WRITE))
2038 WRITE_ONCE(cfp->write_subdev, s_new);
2042 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
2045 unsigned int minor = iminor(file_inode(file));
2046 struct comedi_file *cfp = file->private_data;
2047 struct comedi_device *dev = cfp->dev;
2050 mutex_lock(&dev->mutex);
2053 * Device config is special, because it must work on
2054 * an unconfigured device.
2056 if (cmd == COMEDI_DEVCONFIG) {
2057 if (minor >= COMEDI_NUM_BOARD_MINORS) {
2058 /* Device config not appropriate on non-board minors. */
2062 rc = do_devconfig_ioctl(dev,
2063 (struct comedi_devconfig __user *)arg);
2066 dev->minor >= comedi_num_legacy_minors) {
2068 * Successfully unconfigured a dynamically
2069 * allocated device. Try and remove it.
2071 if (comedi_clear_board_dev(dev)) {
2072 mutex_unlock(&dev->mutex);
2073 comedi_free_board_dev(dev);
2081 if (!dev->attached) {
2082 dev_dbg(dev->class_dev, "no driver attached\n");
2088 case COMEDI_BUFCONFIG:
2089 rc = do_bufconfig_ioctl(dev,
2090 (struct comedi_bufconfig __user *)arg);
2092 case COMEDI_DEVINFO:
2093 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
2096 case COMEDI_SUBDINFO:
2097 rc = do_subdinfo_ioctl(dev,
2098 (struct comedi_subdinfo __user *)arg,
2101 case COMEDI_CHANINFO:
2102 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
2104 case COMEDI_RANGEINFO:
2105 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
2107 case COMEDI_BUFINFO:
2108 rc = do_bufinfo_ioctl(dev,
2109 (struct comedi_bufinfo __user *)arg,
2113 rc = do_lock_ioctl(dev, arg, file);
2116 rc = do_unlock_ioctl(dev, arg, file);
2119 rc = do_cancel_ioctl(dev, arg, file);
2122 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
2124 case COMEDI_CMDTEST:
2125 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
2128 case COMEDI_INSNLIST:
2129 rc = do_insnlist_ioctl(dev,
2130 (struct comedi_insnlist __user *)arg,
2134 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
2138 rc = do_poll_ioctl(dev, arg, file);
2140 case COMEDI_SETRSUBD:
2141 rc = do_setrsubd_ioctl(dev, arg, file);
2143 case COMEDI_SETWSUBD:
2144 rc = do_setwsubd_ioctl(dev, arg, file);
2152 mutex_unlock(&dev->mutex);
2156 static void comedi_vm_open(struct vm_area_struct *area)
2158 struct comedi_buf_map *bm;
2160 bm = area->vm_private_data;
2161 comedi_buf_map_get(bm);
2164 static void comedi_vm_close(struct vm_area_struct *area)
2166 struct comedi_buf_map *bm;
2168 bm = area->vm_private_data;
2169 comedi_buf_map_put(bm);
2172 static const struct vm_operations_struct comedi_vm_ops = {
2173 .open = comedi_vm_open,
2174 .close = comedi_vm_close,
2177 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2179 struct comedi_file *cfp = file->private_data;
2180 struct comedi_device *dev = cfp->dev;
2181 struct comedi_subdevice *s;
2182 struct comedi_async *async;
2183 struct comedi_buf_map *bm = NULL;
2184 unsigned long start = vma->vm_start;
2191 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2192 * and down-reading &dev->attach_lock should normally succeed without
2193 * contention unless the device is in the process of being attached
2196 if (!down_read_trylock(&dev->attach_lock))
2199 if (!dev->attached) {
2200 dev_dbg(dev->class_dev, "no driver attached\n");
2205 if (vma->vm_flags & VM_WRITE)
2206 s = comedi_file_write_subdevice(file);
2208 s = comedi_file_read_subdevice(file);
2220 if (vma->vm_pgoff != 0) {
2221 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
2226 size = vma->vm_end - vma->vm_start;
2227 if (size > async->prealloc_bufsz) {
2231 if (offset_in_page(size)) {
2236 n_pages = vma_pages(vma);
2238 /* get reference to current buf map (if any) */
2239 bm = comedi_buf_map_from_subdev_get(s);
2240 if (!bm || n_pages > bm->n_pages) {
2244 for (i = 0; i < n_pages; ++i) {
2245 struct comedi_buf_page *buf = &bm->page_list[i];
2247 if (remap_pfn_range(vma, start,
2248 page_to_pfn(virt_to_page(buf->virt_addr)),
2249 PAGE_SIZE, PAGE_SHARED)) {
2256 vma->vm_ops = &comedi_vm_ops;
2257 vma->vm_private_data = bm;
2259 vma->vm_ops->open(vma);
2263 up_read(&dev->attach_lock);
2264 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
2268 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2270 unsigned int mask = 0;
2271 struct comedi_file *cfp = file->private_data;
2272 struct comedi_device *dev = cfp->dev;
2273 struct comedi_subdevice *s, *s_read;
2275 down_read(&dev->attach_lock);
2277 if (!dev->attached) {
2278 dev_dbg(dev->class_dev, "no driver attached\n");
2282 s = comedi_file_read_subdevice(file);
2284 if (s && s->async) {
2285 poll_wait(file, &s->async->wait_head, wait);
2286 if (s->busy != file || !comedi_is_subdevice_running(s) ||
2287 (s->async->cmd.flags & CMDF_WRITE) ||
2288 comedi_buf_read_n_available(s) > 0)
2289 mask |= POLLIN | POLLRDNORM;
2292 s = comedi_file_write_subdevice(file);
2293 if (s && s->async) {
2294 unsigned int bps = comedi_bytes_per_sample(s);
2297 poll_wait(file, &s->async->wait_head, wait);
2298 if (s->busy != file || !comedi_is_subdevice_running(s) ||
2299 !(s->async->cmd.flags & CMDF_WRITE) ||
2300 comedi_buf_write_n_available(s) >= bps)
2301 mask |= POLLOUT | POLLWRNORM;
2305 up_read(&dev->attach_lock);
2309 static ssize_t comedi_write(struct file *file, const char __user *buf,
2310 size_t nbytes, loff_t *offset)
2312 struct comedi_subdevice *s;
2313 struct comedi_async *async;
2317 DECLARE_WAITQUEUE(wait, current);
2318 struct comedi_file *cfp = file->private_data;
2319 struct comedi_device *dev = cfp->dev;
2320 bool become_nonbusy = false;
2322 unsigned int old_detach_count;
2324 /* Protect against device detachment during operation. */
2325 down_read(&dev->attach_lock);
2326 attach_locked = true;
2327 old_detach_count = dev->detach_count;
2329 if (!dev->attached) {
2330 dev_dbg(dev->class_dev, "no driver attached\n");
2335 s = comedi_file_write_subdevice(file);
2336 if (!s || !s->async) {
2342 if (s->busy != file || !(async->cmd.flags & CMDF_WRITE)) {
2347 add_wait_queue(&async->wait_head, &wait);
2348 while (count == 0 && !retval) {
2349 unsigned int runflags;
2350 unsigned int wp, n1, n2;
2352 set_current_state(TASK_INTERRUPTIBLE);
2354 runflags = comedi_get_subdevice_runflags(s);
2355 if (!comedi_is_runflags_running(runflags)) {
2356 if (comedi_is_runflags_in_error(runflags))
2358 if (retval || nbytes)
2359 become_nonbusy = true;
2365 /* Allocate all free buffer space. */
2366 comedi_buf_write_alloc(s, async->prealloc_bufsz);
2367 m = comedi_buf_write_n_allocated(s);
2368 n = min_t(size_t, m, nbytes);
2371 if (file->f_flags & O_NONBLOCK) {
2376 if (signal_pending(current)) {
2377 retval = -ERESTARTSYS;
2380 if (s->busy != file ||
2381 !(async->cmd.flags & CMDF_WRITE)) {
2388 wp = async->buf_write_ptr;
2389 n1 = min(n, async->prealloc_bufsz - wp);
2391 m = copy_from_user(async->prealloc_buf + wp, buf, n1);
2395 m = copy_from_user(async->prealloc_buf, buf + n1, n2);
2400 comedi_buf_write_free(s, n);
2407 remove_wait_queue(&async->wait_head, &wait);
2408 set_current_state(TASK_RUNNING);
2409 if (become_nonbusy && count == 0) {
2410 struct comedi_subdevice *new_s;
2413 * To avoid deadlock, cannot acquire dev->mutex
2414 * while dev->attach_lock is held.
2416 up_read(&dev->attach_lock);
2417 attach_locked = false;
2418 mutex_lock(&dev->mutex);
2420 * Check device hasn't become detached behind our back.
2421 * Checking dev->detach_count is unchanged ought to be
2422 * sufficient (unless there have been 2**32 detaches in the
2423 * meantime!), but check the subdevice pointer as well just in
2426 * Also check the subdevice is still in a suitable state to
2427 * become non-busy in case it changed behind our back.
2429 new_s = comedi_file_write_subdevice(file);
2430 if (dev->attached && old_detach_count == dev->detach_count &&
2431 s == new_s && new_s->async == async && s->busy == file &&
2432 (async->cmd.flags & CMDF_WRITE) &&
2433 !comedi_is_subdevice_running(s))
2434 do_become_nonbusy(dev, s);
2435 mutex_unlock(&dev->mutex);
2439 up_read(&dev->attach_lock);
2441 return count ? count : retval;
2444 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2447 struct comedi_subdevice *s;
2448 struct comedi_async *async;
2452 DECLARE_WAITQUEUE(wait, current);
2453 struct comedi_file *cfp = file->private_data;
2454 struct comedi_device *dev = cfp->dev;
2455 unsigned int old_detach_count;
2456 bool become_nonbusy = false;
2459 /* Protect against device detachment during operation. */
2460 down_read(&dev->attach_lock);
2461 attach_locked = true;
2462 old_detach_count = dev->detach_count;
2464 if (!dev->attached) {
2465 dev_dbg(dev->class_dev, "no driver attached\n");
2470 s = comedi_file_read_subdevice(file);
2471 if (!s || !s->async) {
2477 if (s->busy != file || (async->cmd.flags & CMDF_WRITE)) {
2482 add_wait_queue(&async->wait_head, &wait);
2483 while (count == 0 && !retval) {
2484 unsigned int rp, n1, n2;
2486 set_current_state(TASK_INTERRUPTIBLE);
2488 m = comedi_buf_read_n_available(s);
2489 n = min_t(size_t, m, nbytes);
2492 unsigned int runflags =
2493 comedi_get_subdevice_runflags(s);
2495 if (!comedi_is_runflags_running(runflags)) {
2496 if (comedi_is_runflags_in_error(runflags))
2498 if (retval || nbytes)
2499 become_nonbusy = true;
2504 if (file->f_flags & O_NONBLOCK) {
2509 if (signal_pending(current)) {
2510 retval = -ERESTARTSYS;
2513 if (s->busy != file ||
2514 (async->cmd.flags & CMDF_WRITE)) {
2520 rp = async->buf_read_ptr;
2521 n1 = min(n, async->prealloc_bufsz - rp);
2523 m = copy_to_user(buf, async->prealloc_buf + rp, n1);
2527 m = copy_to_user(buf + n1, async->prealloc_buf, n2);
2533 comedi_buf_read_alloc(s, n);
2534 comedi_buf_read_free(s, n);
2541 remove_wait_queue(&async->wait_head, &wait);
2542 set_current_state(TASK_RUNNING);
2543 if (become_nonbusy && count == 0) {
2544 struct comedi_subdevice *new_s;
2547 * To avoid deadlock, cannot acquire dev->mutex
2548 * while dev->attach_lock is held.
2550 up_read(&dev->attach_lock);
2551 attach_locked = false;
2552 mutex_lock(&dev->mutex);
2554 * Check device hasn't become detached behind our back.
2555 * Checking dev->detach_count is unchanged ought to be
2556 * sufficient (unless there have been 2**32 detaches in the
2557 * meantime!), but check the subdevice pointer as well just in
2560 * Also check the subdevice is still in a suitable state to
2561 * become non-busy in case it changed behind our back.
2563 new_s = comedi_file_read_subdevice(file);
2564 if (dev->attached && old_detach_count == dev->detach_count &&
2565 s == new_s && new_s->async == async && s->busy == file &&
2566 !(async->cmd.flags & CMDF_WRITE) &&
2567 !comedi_is_subdevice_running(s) &&
2568 comedi_buf_read_n_available(s) == 0)
2569 do_become_nonbusy(dev, s);
2570 mutex_unlock(&dev->mutex);
2574 up_read(&dev->attach_lock);
2576 return count ? count : retval;
2579 static int comedi_open(struct inode *inode, struct file *file)
2581 const unsigned int minor = iminor(inode);
2582 struct comedi_file *cfp;
2583 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2587 pr_debug("invalid minor number\n");
2591 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2597 mutex_lock(&dev->mutex);
2598 if (!dev->attached && !capable(CAP_SYS_ADMIN)) {
2599 dev_dbg(dev->class_dev, "not attached and not CAP_SYS_ADMIN\n");
2603 if (dev->attached && dev->use_count == 0) {
2604 if (!try_module_get(dev->driver->module)) {
2609 rc = dev->open(dev);
2611 module_put(dev->driver->module);
2618 file->private_data = cfp;
2619 comedi_file_reset(file);
2623 mutex_unlock(&dev->mutex);
2625 comedi_dev_put(dev);
2631 static int comedi_fasync(int fd, struct file *file, int on)
2633 struct comedi_file *cfp = file->private_data;
2634 struct comedi_device *dev = cfp->dev;
2636 return fasync_helper(fd, file, on, &dev->async_queue);
2639 static int comedi_close(struct inode *inode, struct file *file)
2641 struct comedi_file *cfp = file->private_data;
2642 struct comedi_device *dev = cfp->dev;
2643 struct comedi_subdevice *s = NULL;
2646 mutex_lock(&dev->mutex);
2648 if (dev->subdevices) {
2649 for (i = 0; i < dev->n_subdevices; i++) {
2650 s = &dev->subdevices[i];
2652 if (s->busy == file)
2654 if (s->lock == file)
2658 if (dev->attached && dev->use_count == 1) {
2661 module_put(dev->driver->module);
2666 mutex_unlock(&dev->mutex);
2667 comedi_dev_put(dev);
2673 static const struct file_operations comedi_fops = {
2674 .owner = THIS_MODULE,
2675 .unlocked_ioctl = comedi_unlocked_ioctl,
2676 .compat_ioctl = comedi_compat_ioctl,
2677 .open = comedi_open,
2678 .release = comedi_close,
2679 .read = comedi_read,
2680 .write = comedi_write,
2681 .mmap = comedi_mmap,
2682 .poll = comedi_poll,
2683 .fasync = comedi_fasync,
2684 .llseek = noop_llseek,
2688 * comedi_event() - Handle events for asynchronous COMEDI command
2689 * @dev: COMEDI device.
2690 * @s: COMEDI subdevice.
2691 * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
2693 * If an asynchronous COMEDI command is active on the subdevice, process
2694 * any %COMEDI_CB_... event flags that have been set, usually by an
2695 * interrupt handler. These may change the run state of the asynchronous
2696 * command, wake a task, and/or send a %SIGIO signal.
2698 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2700 struct comedi_async *async = s->async;
2701 unsigned int events;
2703 unsigned long flags;
2705 spin_lock_irqsave(&s->spin_lock, flags);
2707 events = async->events;
2709 if (!__comedi_is_subdevice_running(s)) {
2710 spin_unlock_irqrestore(&s->spin_lock, flags);
2714 if (events & COMEDI_CB_CANCEL_MASK)
2715 __comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
2718 * Remember if an error event has occurred, so an error can be
2719 * returned the next time the user does a read() or write().
2721 if (events & COMEDI_CB_ERROR_MASK)
2722 __comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
2724 if (async->cb_mask & events) {
2725 wake_up_interruptible(&async->wait_head);
2726 si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
2729 spin_unlock_irqrestore(&s->spin_lock, flags);
2732 kill_fasync(&dev->async_queue, SIGIO, si_code);
2734 EXPORT_SYMBOL_GPL(comedi_event);
2736 /* Note: the ->mutex is pre-locked on successful return */
2737 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2739 struct comedi_device *dev;
2740 struct device *csdev;
2743 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2745 return ERR_PTR(-ENOMEM);
2746 comedi_device_init(dev);
2747 comedi_set_hw_dev(dev, hardware_device);
2748 mutex_lock(&dev->mutex);
2749 mutex_lock(&comedi_board_minor_table_lock);
2750 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2751 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2752 if (!comedi_board_minor_table[i]) {
2753 comedi_board_minor_table[i] = dev;
2757 mutex_unlock(&comedi_board_minor_table_lock);
2758 if (i == COMEDI_NUM_BOARD_MINORS) {
2759 mutex_unlock(&dev->mutex);
2760 comedi_device_cleanup(dev);
2761 comedi_dev_put(dev);
2762 dev_err(hardware_device,
2763 "ran out of minor numbers for board device files\n");
2764 return ERR_PTR(-EBUSY);
2767 csdev = device_create(comedi_class, hardware_device,
2768 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2770 dev->class_dev = get_device(csdev);
2772 /* Note: dev->mutex needs to be unlocked by the caller. */
2776 void comedi_release_hardware_device(struct device *hardware_device)
2779 struct comedi_device *dev;
2781 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2783 mutex_lock(&comedi_board_minor_table_lock);
2784 dev = comedi_board_minor_table[minor];
2785 if (dev && dev->hw_dev == hardware_device) {
2786 comedi_board_minor_table[minor] = NULL;
2787 mutex_unlock(&comedi_board_minor_table_lock);
2788 comedi_free_board_dev(dev);
2791 mutex_unlock(&comedi_board_minor_table_lock);
2795 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2797 struct comedi_device *dev = s->device;
2798 struct device *csdev;
2801 mutex_lock(&comedi_subdevice_minor_table_lock);
2802 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2803 if (!comedi_subdevice_minor_table[i]) {
2804 comedi_subdevice_minor_table[i] = s;
2808 mutex_unlock(&comedi_subdevice_minor_table_lock);
2809 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2810 dev_err(dev->class_dev,
2811 "ran out of minor numbers for subdevice files\n");
2814 i += COMEDI_NUM_BOARD_MINORS;
2816 csdev = device_create(comedi_class, dev->class_dev,
2817 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2818 dev->minor, s->index);
2820 s->class_dev = csdev;
2825 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2831 if (s->minor < COMEDI_NUM_BOARD_MINORS ||
2832 s->minor >= COMEDI_NUM_MINORS)
2835 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2836 mutex_lock(&comedi_subdevice_minor_table_lock);
2837 if (s == comedi_subdevice_minor_table[i])
2838 comedi_subdevice_minor_table[i] = NULL;
2839 mutex_unlock(&comedi_subdevice_minor_table_lock);
2841 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2842 s->class_dev = NULL;
2846 static void comedi_cleanup_board_minors(void)
2848 struct comedi_device *dev;
2851 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
2852 dev = comedi_clear_board_minor(i);
2853 comedi_free_board_dev(dev);
2857 static int __init comedi_init(void)
2862 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
2864 if (comedi_num_legacy_minors < 0 ||
2865 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2866 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2867 COMEDI_NUM_BOARD_MINORS);
2871 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2872 COMEDI_NUM_MINORS, "comedi");
2875 cdev_init(&comedi_cdev, &comedi_fops);
2876 comedi_cdev.owner = THIS_MODULE;
2878 retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
2880 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2885 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2886 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2890 comedi_class = class_create(THIS_MODULE, "comedi");
2891 if (IS_ERR(comedi_class)) {
2892 pr_err("failed to create class\n");
2893 cdev_del(&comedi_cdev);
2894 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2896 return PTR_ERR(comedi_class);
2899 comedi_class->dev_groups = comedi_dev_groups;
2901 /* XXX requires /proc interface */
2904 /* create devices files for legacy/manual use */
2905 for (i = 0; i < comedi_num_legacy_minors; i++) {
2906 struct comedi_device *dev;
2908 dev = comedi_alloc_board_minor(NULL);
2910 comedi_cleanup_board_minors();
2911 cdev_del(&comedi_cdev);
2912 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2914 return PTR_ERR(dev);
2916 /* comedi_alloc_board_minor() locked the mutex */
2917 mutex_unlock(&dev->mutex);
2922 module_init(comedi_init);
2924 static void __exit comedi_cleanup(void)
2926 comedi_cleanup_board_minors();
2927 class_destroy(comedi_class);
2928 cdev_del(&comedi_cdev);
2929 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2931 comedi_proc_cleanup();
2933 module_exit(comedi_cleanup);
2935 MODULE_AUTHOR("http://www.comedi.org");
2936 MODULE_DESCRIPTION("Comedi core module");
2937 MODULE_LICENSE("GPL");