1 // SPDX-License-Identifier: GPL-2.0+
6 * COMEDI - Linux Control and Measurement Device Interface
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include "comedi_compat32.h"
14 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/sched/signal.h>
18 #include <linux/fcntl.h>
19 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/poll.h>
23 #include <linux/device.h>
25 #include "comedidev.h"
26 #include <linux/cdev.h>
29 #include <linux/uaccess.h>
31 #include "comedi_internal.h"
34 * comedi_subdevice "runflags"
35 * COMEDI_SRF_RT: DEPRECATED: command is running real-time
36 * COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred
37 * since the last command was started
38 * COMEDI_SRF_RUNNING: command is running
39 * COMEDI_SRF_FREE_SPRIV: free s->private on detach
41 * COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy"
43 #define COMEDI_SRF_RT BIT(1)
44 #define COMEDI_SRF_ERROR BIT(2)
45 #define COMEDI_SRF_RUNNING BIT(27)
46 #define COMEDI_SRF_FREE_SPRIV BIT(31)
48 #define COMEDI_SRF_BUSY_MASK (COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING)
51 * struct comedi_file - Per-file private data for COMEDI device
52 * @dev: COMEDI device.
53 * @read_subdev: Current "read" subdevice.
54 * @write_subdev: Current "write" subdevice.
55 * @last_detach_count: Last known detach count.
56 * @last_attached: Last known attached/detached state.
59 struct comedi_device *dev;
60 struct comedi_subdevice *read_subdev;
61 struct comedi_subdevice *write_subdev;
62 unsigned int last_detach_count;
66 #define COMEDI_NUM_MINORS 0x100
67 #define COMEDI_NUM_SUBDEVICE_MINORS \
68 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
70 static unsigned short comedi_num_legacy_minors;
71 module_param(comedi_num_legacy_minors, ushort, 0444);
72 MODULE_PARM_DESC(comedi_num_legacy_minors,
73 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
76 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
77 module_param(comedi_default_buf_size_kb, uint, 0644);
78 MODULE_PARM_DESC(comedi_default_buf_size_kb,
79 "default asynchronous buffer size in KiB (default "
80 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
82 unsigned int comedi_default_buf_maxsize_kb
83 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
84 module_param(comedi_default_buf_maxsize_kb, uint, 0644);
85 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
86 "default maximum size of asynchronous buffer in KiB (default "
87 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
89 static DEFINE_MUTEX(comedi_board_minor_table_lock);
90 static struct comedi_device
91 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
93 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
94 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
95 static struct comedi_subdevice
96 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
98 static struct class *comedi_class;
99 static struct cdev comedi_cdev;
101 static void comedi_device_init(struct comedi_device *dev)
103 kref_init(&dev->refcount);
104 spin_lock_init(&dev->spinlock);
105 mutex_init(&dev->mutex);
106 init_rwsem(&dev->attach_lock);
110 static void comedi_dev_kref_release(struct kref *kref)
112 struct comedi_device *dev =
113 container_of(kref, struct comedi_device, refcount);
115 mutex_destroy(&dev->mutex);
116 put_device(dev->class_dev);
121 * comedi_dev_put() - Release a use of a COMEDI device
122 * @dev: COMEDI device.
124 * Must be called when a user of a COMEDI device is finished with it.
125 * When the last user of the COMEDI device calls this function, the
126 * COMEDI device is destroyed.
128 * Return: 1 if the COMEDI device is destroyed by this call or @dev is
129 * NULL, otherwise return 0. Callers must not assume the COMEDI
130 * device is still valid if this function returns 0.
132 int comedi_dev_put(struct comedi_device *dev)
135 return kref_put(&dev->refcount, comedi_dev_kref_release);
138 EXPORT_SYMBOL_GPL(comedi_dev_put);
140 static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
143 kref_get(&dev->refcount);
147 static void comedi_device_cleanup(struct comedi_device *dev)
149 struct module *driver_module = NULL;
153 mutex_lock(&dev->mutex);
155 driver_module = dev->driver->module;
156 comedi_device_detach(dev);
157 if (driver_module && dev->use_count)
158 module_put(driver_module);
159 mutex_unlock(&dev->mutex);
162 static bool comedi_clear_board_dev(struct comedi_device *dev)
164 unsigned int i = dev->minor;
165 bool cleared = false;
167 mutex_lock(&comedi_board_minor_table_lock);
168 if (dev == comedi_board_minor_table[i]) {
169 comedi_board_minor_table[i] = NULL;
172 mutex_unlock(&comedi_board_minor_table_lock);
176 static struct comedi_device *comedi_clear_board_minor(unsigned int minor)
178 struct comedi_device *dev;
180 mutex_lock(&comedi_board_minor_table_lock);
181 dev = comedi_board_minor_table[minor];
182 comedi_board_minor_table[minor] = NULL;
183 mutex_unlock(&comedi_board_minor_table_lock);
187 static void comedi_free_board_dev(struct comedi_device *dev)
190 comedi_device_cleanup(dev);
191 if (dev->class_dev) {
192 device_destroy(comedi_class,
193 MKDEV(COMEDI_MAJOR, dev->minor));
199 static struct comedi_subdevice *
200 comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned int minor)
202 struct comedi_subdevice *s;
203 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
205 mutex_lock(&comedi_subdevice_minor_table_lock);
206 s = comedi_subdevice_minor_table[i];
207 if (s && s->device != dev)
209 mutex_unlock(&comedi_subdevice_minor_table_lock);
213 static struct comedi_device *comedi_dev_get_from_board_minor(unsigned int minor)
215 struct comedi_device *dev;
217 mutex_lock(&comedi_board_minor_table_lock);
218 dev = comedi_dev_get(comedi_board_minor_table[minor]);
219 mutex_unlock(&comedi_board_minor_table_lock);
223 static struct comedi_device *
224 comedi_dev_get_from_subdevice_minor(unsigned int minor)
226 struct comedi_device *dev;
227 struct comedi_subdevice *s;
228 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
230 mutex_lock(&comedi_subdevice_minor_table_lock);
231 s = comedi_subdevice_minor_table[i];
232 dev = comedi_dev_get(s ? s->device : NULL);
233 mutex_unlock(&comedi_subdevice_minor_table_lock);
238 * comedi_dev_get_from_minor() - Get COMEDI device by minor device number
239 * @minor: Minor device number.
241 * Finds the COMEDI device associated with the minor device number, if any,
242 * and increments its reference count. The COMEDI device is prevented from
243 * being freed until a matching call is made to comedi_dev_put().
245 * Return: A pointer to the COMEDI device if it exists, with its usage
246 * reference incremented. Return NULL if no COMEDI device exists with the
247 * specified minor device number.
249 struct comedi_device *comedi_dev_get_from_minor(unsigned int minor)
251 if (minor < COMEDI_NUM_BOARD_MINORS)
252 return comedi_dev_get_from_board_minor(minor);
254 return comedi_dev_get_from_subdevice_minor(minor);
256 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
258 static struct comedi_subdevice *
259 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
261 struct comedi_subdevice *s;
263 if (minor >= COMEDI_NUM_BOARD_MINORS) {
264 s = comedi_subdevice_from_minor(dev, minor);
265 if (!s || (s->subdev_flags & SDF_CMD_READ))
268 return dev->read_subdev;
271 static struct comedi_subdevice *
272 comedi_write_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_WRITE))
281 return dev->write_subdev;
284 static void comedi_file_reset(struct file *file)
286 struct comedi_file *cfp = file->private_data;
287 struct comedi_device *dev = cfp->dev;
288 struct comedi_subdevice *s, *read_s, *write_s;
289 unsigned int minor = iminor(file_inode(file));
291 read_s = dev->read_subdev;
292 write_s = dev->write_subdev;
293 if (minor >= COMEDI_NUM_BOARD_MINORS) {
294 s = comedi_subdevice_from_minor(dev, minor);
295 if (!s || s->subdev_flags & SDF_CMD_READ)
297 if (!s || s->subdev_flags & SDF_CMD_WRITE)
300 cfp->last_attached = dev->attached;
301 cfp->last_detach_count = dev->detach_count;
302 WRITE_ONCE(cfp->read_subdev, read_s);
303 WRITE_ONCE(cfp->write_subdev, write_s);
306 static void comedi_file_check(struct file *file)
308 struct comedi_file *cfp = file->private_data;
309 struct comedi_device *dev = cfp->dev;
311 if (cfp->last_attached != dev->attached ||
312 cfp->last_detach_count != dev->detach_count)
313 comedi_file_reset(file);
316 static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file)
318 struct comedi_file *cfp = file->private_data;
320 comedi_file_check(file);
321 return READ_ONCE(cfp->read_subdev);
324 static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file)
326 struct comedi_file *cfp = file->private_data;
328 comedi_file_check(file);
329 return READ_ONCE(cfp->write_subdev);
332 static int resize_async_buffer(struct comedi_device *dev,
333 struct comedi_subdevice *s,
334 unsigned int new_size)
336 struct comedi_async *async = s->async;
339 if (new_size > async->max_bufsize)
343 dev_dbg(dev->class_dev,
344 "subdevice is busy, cannot resize buffer\n");
347 if (comedi_buf_is_mmapped(s)) {
348 dev_dbg(dev->class_dev,
349 "subdevice is mmapped, cannot resize buffer\n");
353 /* make sure buffer is an integral number of pages (we round up) */
354 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
356 retval = comedi_buf_alloc(dev, s, new_size);
361 retval = s->buf_change(dev, s);
366 dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
367 s->index, async->prealloc_bufsz);
371 /* sysfs attribute files */
373 static ssize_t max_read_buffer_kb_show(struct device *csdev,
374 struct device_attribute *attr, char *buf)
376 unsigned int minor = MINOR(csdev->devt);
377 struct comedi_device *dev;
378 struct comedi_subdevice *s;
379 unsigned int size = 0;
381 dev = comedi_dev_get_from_minor(minor);
385 mutex_lock(&dev->mutex);
386 s = comedi_read_subdevice(dev, minor);
387 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
388 size = s->async->max_bufsize / 1024;
389 mutex_unlock(&dev->mutex);
392 return snprintf(buf, PAGE_SIZE, "%u\n", size);
395 static ssize_t max_read_buffer_kb_store(struct device *csdev,
396 struct device_attribute *attr,
397 const char *buf, size_t count)
399 unsigned int minor = MINOR(csdev->devt);
400 struct comedi_device *dev;
401 struct comedi_subdevice *s;
405 err = kstrtouint(buf, 10, &size);
408 if (size > (UINT_MAX / 1024))
412 dev = comedi_dev_get_from_minor(minor);
416 mutex_lock(&dev->mutex);
417 s = comedi_read_subdevice(dev, minor);
418 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
419 s->async->max_bufsize = size;
422 mutex_unlock(&dev->mutex);
425 return err ? err : count;
427 static DEVICE_ATTR_RW(max_read_buffer_kb);
429 static ssize_t read_buffer_kb_show(struct device *csdev,
430 struct device_attribute *attr, char *buf)
432 unsigned int minor = MINOR(csdev->devt);
433 struct comedi_device *dev;
434 struct comedi_subdevice *s;
435 unsigned int size = 0;
437 dev = comedi_dev_get_from_minor(minor);
441 mutex_lock(&dev->mutex);
442 s = comedi_read_subdevice(dev, minor);
443 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
444 size = s->async->prealloc_bufsz / 1024;
445 mutex_unlock(&dev->mutex);
448 return snprintf(buf, PAGE_SIZE, "%u\n", size);
451 static ssize_t read_buffer_kb_store(struct device *csdev,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
455 unsigned int minor = MINOR(csdev->devt);
456 struct comedi_device *dev;
457 struct comedi_subdevice *s;
461 err = kstrtouint(buf, 10, &size);
464 if (size > (UINT_MAX / 1024))
468 dev = comedi_dev_get_from_minor(minor);
472 mutex_lock(&dev->mutex);
473 s = comedi_read_subdevice(dev, minor);
474 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
475 err = resize_async_buffer(dev, s, size);
478 mutex_unlock(&dev->mutex);
481 return err ? err : count;
483 static DEVICE_ATTR_RW(read_buffer_kb);
485 static ssize_t max_write_buffer_kb_show(struct device *csdev,
486 struct device_attribute *attr,
489 unsigned int minor = MINOR(csdev->devt);
490 struct comedi_device *dev;
491 struct comedi_subdevice *s;
492 unsigned int size = 0;
494 dev = comedi_dev_get_from_minor(minor);
498 mutex_lock(&dev->mutex);
499 s = comedi_write_subdevice(dev, minor);
500 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
501 size = s->async->max_bufsize / 1024;
502 mutex_unlock(&dev->mutex);
505 return snprintf(buf, PAGE_SIZE, "%u\n", size);
508 static ssize_t max_write_buffer_kb_store(struct device *csdev,
509 struct device_attribute *attr,
510 const char *buf, size_t count)
512 unsigned int minor = MINOR(csdev->devt);
513 struct comedi_device *dev;
514 struct comedi_subdevice *s;
518 err = kstrtouint(buf, 10, &size);
521 if (size > (UINT_MAX / 1024))
525 dev = comedi_dev_get_from_minor(minor);
529 mutex_lock(&dev->mutex);
530 s = comedi_write_subdevice(dev, minor);
531 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
532 s->async->max_bufsize = size;
535 mutex_unlock(&dev->mutex);
538 return err ? err : count;
540 static DEVICE_ATTR_RW(max_write_buffer_kb);
542 static ssize_t write_buffer_kb_show(struct device *csdev,
543 struct device_attribute *attr, char *buf)
545 unsigned int minor = MINOR(csdev->devt);
546 struct comedi_device *dev;
547 struct comedi_subdevice *s;
548 unsigned int size = 0;
550 dev = comedi_dev_get_from_minor(minor);
554 mutex_lock(&dev->mutex);
555 s = comedi_write_subdevice(dev, minor);
556 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
557 size = s->async->prealloc_bufsz / 1024;
558 mutex_unlock(&dev->mutex);
561 return snprintf(buf, PAGE_SIZE, "%u\n", size);
564 static ssize_t write_buffer_kb_store(struct device *csdev,
565 struct device_attribute *attr,
566 const char *buf, size_t count)
568 unsigned int minor = MINOR(csdev->devt);
569 struct comedi_device *dev;
570 struct comedi_subdevice *s;
574 err = kstrtouint(buf, 10, &size);
577 if (size > (UINT_MAX / 1024))
581 dev = comedi_dev_get_from_minor(minor);
585 mutex_lock(&dev->mutex);
586 s = comedi_write_subdevice(dev, minor);
587 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
588 err = resize_async_buffer(dev, s, size);
591 mutex_unlock(&dev->mutex);
594 return err ? err : count;
596 static DEVICE_ATTR_RW(write_buffer_kb);
598 static struct attribute *comedi_dev_attrs[] = {
599 &dev_attr_max_read_buffer_kb.attr,
600 &dev_attr_read_buffer_kb.attr,
601 &dev_attr_max_write_buffer_kb.attr,
602 &dev_attr_write_buffer_kb.attr,
605 ATTRIBUTE_GROUPS(comedi_dev);
607 static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s,
610 s->runflags &= ~bits;
613 static void __comedi_set_subdevice_runflags(struct comedi_subdevice *s,
619 static void comedi_update_subdevice_runflags(struct comedi_subdevice *s,
625 spin_lock_irqsave(&s->spin_lock, flags);
626 __comedi_clear_subdevice_runflags(s, mask);
627 __comedi_set_subdevice_runflags(s, bits & mask);
628 spin_unlock_irqrestore(&s->spin_lock, flags);
631 static unsigned int __comedi_get_subdevice_runflags(struct comedi_subdevice *s)
636 static unsigned int comedi_get_subdevice_runflags(struct comedi_subdevice *s)
639 unsigned int runflags;
641 spin_lock_irqsave(&s->spin_lock, flags);
642 runflags = __comedi_get_subdevice_runflags(s);
643 spin_unlock_irqrestore(&s->spin_lock, flags);
647 static bool comedi_is_runflags_running(unsigned int runflags)
649 return runflags & COMEDI_SRF_RUNNING;
652 static bool comedi_is_runflags_in_error(unsigned int runflags)
654 return runflags & COMEDI_SRF_ERROR;
658 * comedi_is_subdevice_running() - Check if async command running on subdevice
659 * @s: COMEDI subdevice.
661 * Return: %true if an asynchronous COMEDI command is active on the
662 * subdevice, else %false.
664 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
666 unsigned int runflags = comedi_get_subdevice_runflags(s);
668 return comedi_is_runflags_running(runflags);
670 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
672 static bool __comedi_is_subdevice_running(struct comedi_subdevice *s)
674 unsigned int runflags = __comedi_get_subdevice_runflags(s);
676 return comedi_is_runflags_running(runflags);
679 bool comedi_can_auto_free_spriv(struct comedi_subdevice *s)
681 unsigned int runflags = __comedi_get_subdevice_runflags(s);
683 return runflags & COMEDI_SRF_FREE_SPRIV;
687 * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
688 * @s: COMEDI subdevice.
690 * Mark the subdevice as having a pointer to private data that can be
691 * automatically freed when the COMEDI device is detached from the low-level
694 void comedi_set_spriv_auto_free(struct comedi_subdevice *s)
696 __comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV);
698 EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free);
701 * comedi_alloc_spriv - Allocate memory for the subdevice private data
702 * @s: COMEDI subdevice.
703 * @size: Size of the memory to allocate.
705 * Allocate memory for the subdevice private data and point @s->private
706 * to it. The memory will be freed automatically when the COMEDI device
707 * is detached from the low-level driver.
709 * Return: A pointer to the allocated memory @s->private on success.
710 * Return NULL on failure.
712 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
714 s->private = kzalloc(size, GFP_KERNEL);
716 comedi_set_spriv_auto_free(s);
719 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
722 * This function restores a subdevice to an idle state.
724 static void do_become_nonbusy(struct comedi_device *dev,
725 struct comedi_subdevice *s)
727 struct comedi_async *async = s->async;
729 comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
732 async->inttrig = NULL;
733 kfree(async->cmd.chanlist);
734 async->cmd.chanlist = NULL;
736 wake_up_interruptible_all(&async->wait_head);
738 dev_err(dev->class_dev,
739 "BUG: (?) %s called with async=NULL\n", __func__);
744 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
748 if (comedi_is_subdevice_running(s) && s->cancel)
749 ret = s->cancel(dev, s);
751 do_become_nonbusy(dev, s);
756 void comedi_device_cancel_all(struct comedi_device *dev)
758 struct comedi_subdevice *s;
764 for (i = 0; i < dev->n_subdevices; i++) {
765 s = &dev->subdevices[i];
771 static int is_device_busy(struct comedi_device *dev)
773 struct comedi_subdevice *s;
779 for (i = 0; i < dev->n_subdevices; i++) {
780 s = &dev->subdevices[i];
783 if (s->async && comedi_buf_is_mmapped(s))
791 * COMEDI_DEVCONFIG ioctl
792 * attaches (and configures) or detaches a legacy device
795 * pointer to comedi_devconfig structure (NULL if detaching)
798 * comedi_devconfig structure (if attaching)
803 static int do_devconfig_ioctl(struct comedi_device *dev,
804 struct comedi_devconfig __user *arg)
806 struct comedi_devconfig it;
808 if (!capable(CAP_SYS_ADMIN))
812 if (is_device_busy(dev))
815 struct module *driver_module = dev->driver->module;
817 comedi_device_detach(dev);
818 module_put(driver_module);
823 if (copy_from_user(&it, arg, sizeof(it)))
826 it.board_name[COMEDI_NAMELEN - 1] = 0;
828 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
829 dev_warn(dev->class_dev,
830 "comedi_config --init_data is deprecated\n");
834 if (dev->minor >= comedi_num_legacy_minors)
835 /* don't re-use dynamically allocated comedi devices */
838 /* This increments the driver module count on success. */
839 return comedi_device_attach(dev, &it);
843 * COMEDI_BUFCONFIG ioctl
844 * buffer configuration
847 * pointer to comedi_bufconfig structure
850 * comedi_bufconfig structure
853 * modified comedi_bufconfig structure
855 static int do_bufconfig_ioctl(struct comedi_device *dev,
856 struct comedi_bufconfig __user *arg)
858 struct comedi_bufconfig bc;
859 struct comedi_async *async;
860 struct comedi_subdevice *s;
863 if (copy_from_user(&bc, arg, sizeof(bc)))
866 if (bc.subdevice >= dev->n_subdevices)
869 s = &dev->subdevices[bc.subdevice];
873 dev_dbg(dev->class_dev,
874 "subdevice does not have async capability\n");
880 if (bc.maximum_size) {
881 if (!capable(CAP_SYS_ADMIN))
884 async->max_bufsize = bc.maximum_size;
888 retval = resize_async_buffer(dev, s, bc.size);
893 bc.size = async->prealloc_bufsz;
894 bc.maximum_size = async->max_bufsize;
897 if (copy_to_user(arg, &bc, sizeof(bc)))
904 * COMEDI_DEVINFO ioctl
908 * pointer to comedi_devinfo structure
914 * comedi_devinfo structure
916 static int do_devinfo_ioctl(struct comedi_device *dev,
917 struct comedi_devinfo __user *arg,
920 struct comedi_subdevice *s;
921 struct comedi_devinfo devinfo;
923 memset(&devinfo, 0, sizeof(devinfo));
925 /* fill devinfo structure */
926 devinfo.version_code = COMEDI_VERSION_CODE;
927 devinfo.n_subdevs = dev->n_subdevices;
928 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
929 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
931 s = comedi_file_read_subdevice(file);
933 devinfo.read_subdevice = s->index;
935 devinfo.read_subdevice = -1;
937 s = comedi_file_write_subdevice(file);
939 devinfo.write_subdevice = s->index;
941 devinfo.write_subdevice = -1;
943 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
950 * COMEDI_SUBDINFO ioctl
954 * pointer to array of comedi_subdinfo structures
960 * array of comedi_subdinfo structures
962 static int do_subdinfo_ioctl(struct comedi_device *dev,
963 struct comedi_subdinfo __user *arg, void *file)
966 struct comedi_subdinfo *tmp, *us;
967 struct comedi_subdevice *s;
969 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
973 /* fill subdinfo structs */
974 for (i = 0; i < dev->n_subdevices; i++) {
975 s = &dev->subdevices[i];
979 us->n_chan = s->n_chan;
980 us->subd_flags = s->subdev_flags;
981 if (comedi_is_subdevice_running(s))
982 us->subd_flags |= SDF_RUNNING;
983 #define TIMER_nanosec 5 /* backwards compatibility */
984 us->timer_type = TIMER_nanosec;
985 us->len_chanlist = s->len_chanlist;
986 us->maxdata = s->maxdata;
987 if (s->range_table) {
989 (i << 24) | (0 << 16) | (s->range_table->length);
991 us->range_type = 0; /* XXX */
995 us->subd_flags |= SDF_BUSY;
997 us->subd_flags |= SDF_BUSY_OWNER;
999 us->subd_flags |= SDF_LOCKED;
1000 if (s->lock == file)
1001 us->subd_flags |= SDF_LOCK_OWNER;
1002 if (!s->maxdata && s->maxdata_list)
1003 us->subd_flags |= SDF_MAXDATA;
1004 if (s->range_table_list)
1005 us->subd_flags |= SDF_RANGETYPE;
1007 us->subd_flags |= SDF_CMD;
1009 if (s->insn_bits != &insn_inval)
1010 us->insn_bits_support = COMEDI_SUPPORTED;
1012 us->insn_bits_support = COMEDI_UNSUPPORTED;
1015 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
1019 return ret ? -EFAULT : 0;
1023 * COMEDI_CHANINFO ioctl
1024 * subdevice channel info
1027 * pointer to comedi_chaninfo structure
1030 * comedi_chaninfo structure
1033 * array of maxdata values to chaninfo->maxdata_list if requested
1034 * array of range table lengths to chaninfo->range_table_list if requested
1036 static int do_chaninfo_ioctl(struct comedi_device *dev,
1037 struct comedi_chaninfo __user *arg)
1039 struct comedi_subdevice *s;
1040 struct comedi_chaninfo it;
1042 if (copy_from_user(&it, arg, sizeof(it)))
1045 if (it.subdev >= dev->n_subdevices)
1047 s = &dev->subdevices[it.subdev];
1049 if (it.maxdata_list) {
1050 if (s->maxdata || !s->maxdata_list)
1052 if (copy_to_user(it.maxdata_list, s->maxdata_list,
1053 s->n_chan * sizeof(unsigned int)))
1058 return -EINVAL; /* flaglist not supported */
1063 if (!s->range_table_list)
1065 for (i = 0; i < s->n_chan; i++) {
1068 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
1069 (s->range_table_list[i]->length);
1070 if (put_user(x, it.rangelist + i))
1079 * COMEDI_BUFINFO ioctl
1080 * buffer information
1083 * pointer to comedi_bufinfo structure
1086 * comedi_bufinfo structure
1089 * modified comedi_bufinfo structure
1091 static int do_bufinfo_ioctl(struct comedi_device *dev,
1092 struct comedi_bufinfo __user *arg, void *file)
1094 struct comedi_bufinfo bi;
1095 struct comedi_subdevice *s;
1096 struct comedi_async *async;
1097 unsigned int runflags;
1099 bool become_nonbusy = false;
1101 if (copy_from_user(&bi, arg, sizeof(bi)))
1104 if (bi.subdevice >= dev->n_subdevices)
1107 s = &dev->subdevices[bi.subdevice];
1111 if (!async || s->busy != file)
1114 runflags = comedi_get_subdevice_runflags(s);
1115 if (!(async->cmd.flags & CMDF_WRITE)) {
1116 /* command was set up in "read" direction */
1117 if (bi.bytes_read) {
1118 comedi_buf_read_alloc(s, bi.bytes_read);
1119 bi.bytes_read = comedi_buf_read_free(s, bi.bytes_read);
1122 * If nothing left to read, and command has stopped, and
1123 * {"read" position not updated or command stopped normally},
1124 * then become non-busy.
1126 if (comedi_buf_read_n_available(s) == 0 &&
1127 !comedi_is_runflags_running(runflags) &&
1128 (bi.bytes_read == 0 ||
1129 !comedi_is_runflags_in_error(runflags))) {
1130 become_nonbusy = true;
1131 if (comedi_is_runflags_in_error(runflags))
1134 bi.bytes_written = 0;
1136 /* command was set up in "write" direction */
1137 if (!comedi_is_runflags_running(runflags)) {
1138 bi.bytes_written = 0;
1139 become_nonbusy = true;
1140 if (comedi_is_runflags_in_error(runflags))
1142 } else if (bi.bytes_written) {
1143 comedi_buf_write_alloc(s, bi.bytes_written);
1145 comedi_buf_write_free(s, bi.bytes_written);
1150 bi.buf_write_count = async->buf_write_count;
1151 bi.buf_write_ptr = async->buf_write_ptr;
1152 bi.buf_read_count = async->buf_read_count;
1153 bi.buf_read_ptr = async->buf_read_ptr;
1156 do_become_nonbusy(dev, s);
1161 if (copy_to_user(arg, &bi, sizeof(bi)))
1167 static int check_insn_config_length(struct comedi_insn *insn,
1174 case INSN_CONFIG_DIO_OUTPUT:
1175 case INSN_CONFIG_DIO_INPUT:
1176 case INSN_CONFIG_DISARM:
1177 case INSN_CONFIG_RESET:
1181 case INSN_CONFIG_ARM:
1182 case INSN_CONFIG_DIO_QUERY:
1183 case INSN_CONFIG_BLOCK_SIZE:
1184 case INSN_CONFIG_FILTER:
1185 case INSN_CONFIG_SERIAL_CLOCK:
1186 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1187 case INSN_CONFIG_ALT_SOURCE:
1188 case INSN_CONFIG_SET_COUNTER_MODE:
1189 case INSN_CONFIG_8254_READ_STATUS:
1190 case INSN_CONFIG_SET_ROUTING:
1191 case INSN_CONFIG_GET_ROUTING:
1192 case INSN_CONFIG_GET_PWM_STATUS:
1193 case INSN_CONFIG_PWM_SET_PERIOD:
1194 case INSN_CONFIG_PWM_GET_PERIOD:
1198 case INSN_CONFIG_SET_GATE_SRC:
1199 case INSN_CONFIG_GET_GATE_SRC:
1200 case INSN_CONFIG_SET_CLOCK_SRC:
1201 case INSN_CONFIG_GET_CLOCK_SRC:
1202 case INSN_CONFIG_SET_OTHER_SRC:
1203 case INSN_CONFIG_GET_COUNTER_STATUS:
1204 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1205 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1206 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1210 case INSN_CONFIG_PWM_OUTPUT:
1211 case INSN_CONFIG_ANALOG_TRIG:
1215 case INSN_CONFIG_DIGITAL_TRIG:
1220 * by default we allow the insn since we don't have checks for
1221 * all possible cases yet
1224 pr_warn("No check for data length of config insn id %i is implemented\n",
1226 pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1227 pr_warn("Assuming n=%i is correct\n", insn->n);
1233 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1234 unsigned int *data, void *file)
1236 struct comedi_subdevice *s;
1240 if (insn->insn & INSN_MASK_SPECIAL) {
1241 /* a non-subdevice instruction */
1243 switch (insn->insn) {
1246 struct timespec64 tv;
1253 ktime_get_real_ts64(&tv);
1254 /* unsigned data safe until 2106 */
1255 data[0] = (unsigned int)tv.tv_sec;
1256 data[1] = tv.tv_nsec / NSEC_PER_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 WRITE_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 WRITE_ONCE(cfp->write_subdev, s_new);
2029 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
2032 unsigned int 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 int comedi_vm_access(struct vm_area_struct *vma, unsigned long addr,
2160 void *buf, int len, int write)
2162 struct comedi_buf_map *bm = vma->vm_private_data;
2163 unsigned long offset =
2164 addr - vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT);
2168 if (len > vma->vm_end - addr)
2169 len = vma->vm_end - addr;
2170 return comedi_buf_map_access(bm, offset, buf, len, write);
2173 static const struct vm_operations_struct comedi_vm_ops = {
2174 .open = comedi_vm_open,
2175 .close = comedi_vm_close,
2176 .access = comedi_vm_access,
2179 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2181 struct comedi_file *cfp = file->private_data;
2182 struct comedi_device *dev = cfp->dev;
2183 struct comedi_subdevice *s;
2184 struct comedi_async *async;
2185 struct comedi_buf_map *bm = NULL;
2186 unsigned long start = vma->vm_start;
2193 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2194 * and down-reading &dev->attach_lock should normally succeed without
2195 * contention unless the device is in the process of being attached
2198 if (!down_read_trylock(&dev->attach_lock))
2201 if (!dev->attached) {
2202 dev_dbg(dev->class_dev, "no driver attached\n");
2207 if (vma->vm_flags & VM_WRITE)
2208 s = comedi_file_write_subdevice(file);
2210 s = comedi_file_read_subdevice(file);
2222 if (vma->vm_pgoff != 0) {
2223 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
2228 size = vma->vm_end - vma->vm_start;
2229 if (size > async->prealloc_bufsz) {
2233 if (offset_in_page(size)) {
2238 n_pages = vma_pages(vma);
2240 /* get reference to current buf map (if any) */
2241 bm = comedi_buf_map_from_subdev_get(s);
2242 if (!bm || n_pages > bm->n_pages) {
2246 for (i = 0; i < n_pages; ++i) {
2247 struct comedi_buf_page *buf = &bm->page_list[i];
2249 if (remap_pfn_range(vma, start,
2250 page_to_pfn(virt_to_page(buf->virt_addr)),
2251 PAGE_SIZE, PAGE_SHARED)) {
2258 vma->vm_ops = &comedi_vm_ops;
2259 vma->vm_private_data = bm;
2261 vma->vm_ops->open(vma);
2265 up_read(&dev->attach_lock);
2266 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
2270 static __poll_t comedi_poll(struct file *file, poll_table *wait)
2273 struct comedi_file *cfp = file->private_data;
2274 struct comedi_device *dev = cfp->dev;
2275 struct comedi_subdevice *s, *s_read;
2277 down_read(&dev->attach_lock);
2279 if (!dev->attached) {
2280 dev_dbg(dev->class_dev, "no driver attached\n");
2284 s = comedi_file_read_subdevice(file);
2286 if (s && s->async) {
2287 poll_wait(file, &s->async->wait_head, wait);
2288 if (s->busy != file || !comedi_is_subdevice_running(s) ||
2289 (s->async->cmd.flags & CMDF_WRITE) ||
2290 comedi_buf_read_n_available(s) > 0)
2291 mask |= EPOLLIN | EPOLLRDNORM;
2294 s = comedi_file_write_subdevice(file);
2295 if (s && s->async) {
2296 unsigned int bps = comedi_bytes_per_sample(s);
2299 poll_wait(file, &s->async->wait_head, wait);
2300 if (s->busy != file || !comedi_is_subdevice_running(s) ||
2301 !(s->async->cmd.flags & CMDF_WRITE) ||
2302 comedi_buf_write_n_available(s) >= bps)
2303 mask |= EPOLLOUT | EPOLLWRNORM;
2307 up_read(&dev->attach_lock);
2311 static ssize_t comedi_write(struct file *file, const char __user *buf,
2312 size_t nbytes, loff_t *offset)
2314 struct comedi_subdevice *s;
2315 struct comedi_async *async;
2319 DECLARE_WAITQUEUE(wait, current);
2320 struct comedi_file *cfp = file->private_data;
2321 struct comedi_device *dev = cfp->dev;
2322 bool become_nonbusy = false;
2324 unsigned int old_detach_count;
2326 /* Protect against device detachment during operation. */
2327 down_read(&dev->attach_lock);
2328 attach_locked = true;
2329 old_detach_count = dev->detach_count;
2331 if (!dev->attached) {
2332 dev_dbg(dev->class_dev, "no driver attached\n");
2337 s = comedi_file_write_subdevice(file);
2338 if (!s || !s->async) {
2344 if (s->busy != file || !(async->cmd.flags & CMDF_WRITE)) {
2349 add_wait_queue(&async->wait_head, &wait);
2350 while (count == 0 && !retval) {
2351 unsigned int runflags;
2352 unsigned int wp, n1, n2;
2354 set_current_state(TASK_INTERRUPTIBLE);
2356 runflags = comedi_get_subdevice_runflags(s);
2357 if (!comedi_is_runflags_running(runflags)) {
2358 if (comedi_is_runflags_in_error(runflags))
2360 if (retval || nbytes)
2361 become_nonbusy = true;
2367 /* Allocate all free buffer space. */
2368 comedi_buf_write_alloc(s, async->prealloc_bufsz);
2369 m = comedi_buf_write_n_allocated(s);
2370 n = min_t(size_t, m, nbytes);
2373 if (file->f_flags & O_NONBLOCK) {
2378 if (signal_pending(current)) {
2379 retval = -ERESTARTSYS;
2382 if (s->busy != file ||
2383 !(async->cmd.flags & CMDF_WRITE)) {
2390 set_current_state(TASK_RUNNING);
2391 wp = async->buf_write_ptr;
2392 n1 = min(n, async->prealloc_bufsz - wp);
2394 m = copy_from_user(async->prealloc_buf + wp, buf, n1);
2398 m = copy_from_user(async->prealloc_buf, buf + n1, n2);
2403 comedi_buf_write_free(s, n);
2410 remove_wait_queue(&async->wait_head, &wait);
2411 set_current_state(TASK_RUNNING);
2412 if (become_nonbusy && count == 0) {
2413 struct comedi_subdevice *new_s;
2416 * To avoid deadlock, cannot acquire dev->mutex
2417 * while dev->attach_lock is held.
2419 up_read(&dev->attach_lock);
2420 attach_locked = false;
2421 mutex_lock(&dev->mutex);
2423 * Check device hasn't become detached behind our back.
2424 * Checking dev->detach_count is unchanged ought to be
2425 * sufficient (unless there have been 2**32 detaches in the
2426 * meantime!), but check the subdevice pointer as well just in
2429 * Also check the subdevice is still in a suitable state to
2430 * become non-busy in case it changed behind our back.
2432 new_s = comedi_file_write_subdevice(file);
2433 if (dev->attached && old_detach_count == dev->detach_count &&
2434 s == new_s && new_s->async == async && s->busy == file &&
2435 (async->cmd.flags & CMDF_WRITE) &&
2436 !comedi_is_subdevice_running(s))
2437 do_become_nonbusy(dev, s);
2438 mutex_unlock(&dev->mutex);
2442 up_read(&dev->attach_lock);
2444 return count ? count : retval;
2447 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2450 struct comedi_subdevice *s;
2451 struct comedi_async *async;
2455 DECLARE_WAITQUEUE(wait, current);
2456 struct comedi_file *cfp = file->private_data;
2457 struct comedi_device *dev = cfp->dev;
2458 unsigned int old_detach_count;
2459 bool become_nonbusy = false;
2462 /* Protect against device detachment during operation. */
2463 down_read(&dev->attach_lock);
2464 attach_locked = true;
2465 old_detach_count = dev->detach_count;
2467 if (!dev->attached) {
2468 dev_dbg(dev->class_dev, "no driver attached\n");
2473 s = comedi_file_read_subdevice(file);
2474 if (!s || !s->async) {
2480 if (s->busy != file || (async->cmd.flags & CMDF_WRITE)) {
2485 add_wait_queue(&async->wait_head, &wait);
2486 while (count == 0 && !retval) {
2487 unsigned int rp, n1, n2;
2489 set_current_state(TASK_INTERRUPTIBLE);
2491 m = comedi_buf_read_n_available(s);
2492 n = min_t(size_t, m, nbytes);
2495 unsigned int runflags =
2496 comedi_get_subdevice_runflags(s);
2498 if (!comedi_is_runflags_running(runflags)) {
2499 if (comedi_is_runflags_in_error(runflags))
2501 if (retval || nbytes)
2502 become_nonbusy = true;
2507 if (file->f_flags & O_NONBLOCK) {
2512 if (signal_pending(current)) {
2513 retval = -ERESTARTSYS;
2516 if (s->busy != file ||
2517 (async->cmd.flags & CMDF_WRITE)) {
2524 set_current_state(TASK_RUNNING);
2525 rp = async->buf_read_ptr;
2526 n1 = min(n, async->prealloc_bufsz - rp);
2528 m = copy_to_user(buf, async->prealloc_buf + rp, n1);
2532 m = copy_to_user(buf + n1, async->prealloc_buf, n2);
2538 comedi_buf_read_alloc(s, n);
2539 comedi_buf_read_free(s, n);
2546 remove_wait_queue(&async->wait_head, &wait);
2547 set_current_state(TASK_RUNNING);
2548 if (become_nonbusy && count == 0) {
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 * Also check the subdevice is still in a suitable state to
2566 * become non-busy in case it changed behind our back.
2568 new_s = comedi_file_read_subdevice(file);
2569 if (dev->attached && old_detach_count == dev->detach_count &&
2570 s == new_s && new_s->async == async && s->busy == file &&
2571 !(async->cmd.flags & CMDF_WRITE) &&
2572 !comedi_is_subdevice_running(s) &&
2573 comedi_buf_read_n_available(s) == 0)
2574 do_become_nonbusy(dev, s);
2575 mutex_unlock(&dev->mutex);
2579 up_read(&dev->attach_lock);
2581 return count ? count : retval;
2584 static int comedi_open(struct inode *inode, struct file *file)
2586 const unsigned int minor = iminor(inode);
2587 struct comedi_file *cfp;
2588 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2592 pr_debug("invalid minor number\n");
2596 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2602 mutex_lock(&dev->mutex);
2603 if (!dev->attached && !capable(CAP_SYS_ADMIN)) {
2604 dev_dbg(dev->class_dev, "not attached and not CAP_SYS_ADMIN\n");
2608 if (dev->attached && dev->use_count == 0) {
2609 if (!try_module_get(dev->driver->module)) {
2614 rc = dev->open(dev);
2616 module_put(dev->driver->module);
2623 file->private_data = cfp;
2624 comedi_file_reset(file);
2628 mutex_unlock(&dev->mutex);
2630 comedi_dev_put(dev);
2636 static int comedi_fasync(int fd, struct file *file, int on)
2638 struct comedi_file *cfp = file->private_data;
2639 struct comedi_device *dev = cfp->dev;
2641 return fasync_helper(fd, file, on, &dev->async_queue);
2644 static int comedi_close(struct inode *inode, struct file *file)
2646 struct comedi_file *cfp = file->private_data;
2647 struct comedi_device *dev = cfp->dev;
2648 struct comedi_subdevice *s = NULL;
2651 mutex_lock(&dev->mutex);
2653 if (dev->subdevices) {
2654 for (i = 0; i < dev->n_subdevices; i++) {
2655 s = &dev->subdevices[i];
2657 if (s->busy == file)
2659 if (s->lock == file)
2663 if (dev->attached && dev->use_count == 1) {
2666 module_put(dev->driver->module);
2671 mutex_unlock(&dev->mutex);
2672 comedi_dev_put(dev);
2678 static const struct file_operations comedi_fops = {
2679 .owner = THIS_MODULE,
2680 .unlocked_ioctl = comedi_unlocked_ioctl,
2681 .compat_ioctl = comedi_compat_ioctl,
2682 .open = comedi_open,
2683 .release = comedi_close,
2684 .read = comedi_read,
2685 .write = comedi_write,
2686 .mmap = comedi_mmap,
2687 .poll = comedi_poll,
2688 .fasync = comedi_fasync,
2689 .llseek = noop_llseek,
2693 * comedi_event() - Handle events for asynchronous COMEDI command
2694 * @dev: COMEDI device.
2695 * @s: COMEDI subdevice.
2696 * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
2698 * If an asynchronous COMEDI command is active on the subdevice, process
2699 * any %COMEDI_CB_... event flags that have been set, usually by an
2700 * interrupt handler. These may change the run state of the asynchronous
2701 * command, wake a task, and/or send a %SIGIO signal.
2703 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2705 struct comedi_async *async = s->async;
2706 unsigned int events;
2708 unsigned long flags;
2710 spin_lock_irqsave(&s->spin_lock, flags);
2712 events = async->events;
2714 if (!__comedi_is_subdevice_running(s)) {
2715 spin_unlock_irqrestore(&s->spin_lock, flags);
2719 if (events & COMEDI_CB_CANCEL_MASK)
2720 __comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
2723 * Remember if an error event has occurred, so an error can be
2724 * returned the next time the user does a read() or write().
2726 if (events & COMEDI_CB_ERROR_MASK)
2727 __comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
2729 if (async->cb_mask & events) {
2730 wake_up_interruptible(&async->wait_head);
2731 si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
2734 spin_unlock_irqrestore(&s->spin_lock, flags);
2737 kill_fasync(&dev->async_queue, SIGIO, si_code);
2739 EXPORT_SYMBOL_GPL(comedi_event);
2741 /* Note: the ->mutex is pre-locked on successful return */
2742 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2744 struct comedi_device *dev;
2745 struct device *csdev;
2748 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2750 return ERR_PTR(-ENOMEM);
2751 comedi_device_init(dev);
2752 comedi_set_hw_dev(dev, hardware_device);
2753 mutex_lock(&dev->mutex);
2754 mutex_lock(&comedi_board_minor_table_lock);
2755 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2756 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2757 if (!comedi_board_minor_table[i]) {
2758 comedi_board_minor_table[i] = dev;
2762 mutex_unlock(&comedi_board_minor_table_lock);
2763 if (i == COMEDI_NUM_BOARD_MINORS) {
2764 mutex_unlock(&dev->mutex);
2765 comedi_device_cleanup(dev);
2766 comedi_dev_put(dev);
2767 dev_err(hardware_device,
2768 "ran out of minor numbers for board device files\n");
2769 return ERR_PTR(-EBUSY);
2772 csdev = device_create(comedi_class, hardware_device,
2773 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2775 dev->class_dev = get_device(csdev);
2777 /* Note: dev->mutex needs to be unlocked by the caller. */
2781 void comedi_release_hardware_device(struct device *hardware_device)
2784 struct comedi_device *dev;
2786 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2788 mutex_lock(&comedi_board_minor_table_lock);
2789 dev = comedi_board_minor_table[minor];
2790 if (dev && dev->hw_dev == hardware_device) {
2791 comedi_board_minor_table[minor] = NULL;
2792 mutex_unlock(&comedi_board_minor_table_lock);
2793 comedi_free_board_dev(dev);
2796 mutex_unlock(&comedi_board_minor_table_lock);
2800 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2802 struct comedi_device *dev = s->device;
2803 struct device *csdev;
2806 mutex_lock(&comedi_subdevice_minor_table_lock);
2807 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2808 if (!comedi_subdevice_minor_table[i]) {
2809 comedi_subdevice_minor_table[i] = s;
2813 mutex_unlock(&comedi_subdevice_minor_table_lock);
2814 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2815 dev_err(dev->class_dev,
2816 "ran out of minor numbers for subdevice files\n");
2819 i += COMEDI_NUM_BOARD_MINORS;
2821 csdev = device_create(comedi_class, dev->class_dev,
2822 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2823 dev->minor, s->index);
2825 s->class_dev = csdev;
2830 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2836 if (s->minor < COMEDI_NUM_BOARD_MINORS ||
2837 s->minor >= COMEDI_NUM_MINORS)
2840 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2841 mutex_lock(&comedi_subdevice_minor_table_lock);
2842 if (s == comedi_subdevice_minor_table[i])
2843 comedi_subdevice_minor_table[i] = NULL;
2844 mutex_unlock(&comedi_subdevice_minor_table_lock);
2846 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2847 s->class_dev = NULL;
2851 static void comedi_cleanup_board_minors(void)
2853 struct comedi_device *dev;
2856 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
2857 dev = comedi_clear_board_minor(i);
2858 comedi_free_board_dev(dev);
2862 static int __init comedi_init(void)
2867 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
2869 if (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");
2880 cdev_init(&comedi_cdev, &comedi_fops);
2881 comedi_cdev.owner = THIS_MODULE;
2883 retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
2885 goto out_unregister_chrdev_region;
2887 retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
2890 goto out_unregister_chrdev_region;
2892 comedi_class = class_create(THIS_MODULE, "comedi");
2893 if (IS_ERR(comedi_class)) {
2894 retval = PTR_ERR(comedi_class);
2895 pr_err("failed to create class\n");
2899 comedi_class->dev_groups = comedi_dev_groups;
2901 /* create devices files for legacy/manual use */
2902 for (i = 0; i < comedi_num_legacy_minors; i++) {
2903 struct comedi_device *dev;
2905 dev = comedi_alloc_board_minor(NULL);
2907 retval = PTR_ERR(dev);
2908 goto out_cleanup_board_minors;
2910 /* comedi_alloc_board_minor() locked the mutex */
2911 mutex_unlock(&dev->mutex);
2914 /* XXX requires /proc interface */
2919 out_cleanup_board_minors:
2920 comedi_cleanup_board_minors();
2921 class_destroy(comedi_class);
2923 cdev_del(&comedi_cdev);
2924 out_unregister_chrdev_region:
2925 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2928 module_init(comedi_init);
2930 static void __exit comedi_cleanup(void)
2932 comedi_cleanup_board_minors();
2933 class_destroy(comedi_class);
2934 cdev_del(&comedi_cdev);
2935 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2937 comedi_proc_cleanup();
2939 module_exit(comedi_cleanup);
2941 MODULE_AUTHOR("http://www.comedi.org");
2942 MODULE_DESCRIPTION("Comedi core module");
2943 MODULE_LICENSE("GPL");