5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include "comedi_compat32.h"
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/fcntl.h>
28 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/kmod.h>
32 #include <linux/poll.h>
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/vmalloc.h>
37 #include "comedidev.h"
38 #include <linux/cdev.h>
39 #include <linux/stat.h>
42 #include <linux/uaccess.h>
44 #include "comedi_internal.h"
47 * struct comedi_file - per-file private data for comedi device
48 * @dev: comedi_device struct
49 * @read_subdev: current "read" subdevice
50 * @write_subdev: current "write" subdevice
51 * @last_detach_count: last known detach count
52 * @last_attached: last known attached/detached state
55 struct comedi_device *dev;
56 struct comedi_subdevice *read_subdev;
57 struct comedi_subdevice *write_subdev;
58 unsigned int last_detach_count;
62 #define COMEDI_NUM_MINORS 0x100
63 #define COMEDI_NUM_SUBDEVICE_MINORS \
64 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
66 static int comedi_num_legacy_minors;
67 module_param(comedi_num_legacy_minors, int, S_IRUGO);
68 MODULE_PARM_DESC(comedi_num_legacy_minors,
69 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
72 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
73 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
74 MODULE_PARM_DESC(comedi_default_buf_size_kb,
75 "default asynchronous buffer size in KiB (default "
76 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
78 unsigned int comedi_default_buf_maxsize_kb
79 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
80 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
81 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
82 "default maximum size of asynchronous buffer in KiB (default "
83 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
85 static DEFINE_MUTEX(comedi_board_minor_table_lock);
86 static struct comedi_device
87 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
89 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
90 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
91 static struct comedi_subdevice
92 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
94 static struct class *comedi_class;
95 static struct cdev comedi_cdev;
97 static void comedi_device_init(struct comedi_device *dev)
99 kref_init(&dev->refcount);
100 spin_lock_init(&dev->spinlock);
101 mutex_init(&dev->mutex);
102 init_rwsem(&dev->attach_lock);
106 static void comedi_dev_kref_release(struct kref *kref)
108 struct comedi_device *dev =
109 container_of(kref, struct comedi_device, refcount);
111 mutex_destroy(&dev->mutex);
112 put_device(dev->class_dev);
117 * comedi_dev_put - release a use of a comedi device structure
118 * @dev: comedi_device struct
120 * Must be called when a user of a comedi device is finished with it.
121 * When the last user of the comedi device calls this function, the
122 * comedi device is destroyed.
124 * Return 1 if the comedi device is destroyed by this call or dev is
125 * NULL, otherwise return 0. Callers must not assume the comedi
126 * device is still valid if this function returns 0.
128 int comedi_dev_put(struct comedi_device *dev)
131 return kref_put(&dev->refcount, comedi_dev_kref_release);
134 EXPORT_SYMBOL_GPL(comedi_dev_put);
136 static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
139 kref_get(&dev->refcount);
143 static void comedi_device_cleanup(struct comedi_device *dev)
145 struct module *driver_module = NULL;
149 mutex_lock(&dev->mutex);
151 driver_module = dev->driver->module;
152 comedi_device_detach(dev);
153 if (driver_module && dev->use_count)
154 module_put(driver_module);
155 mutex_unlock(&dev->mutex);
158 static bool comedi_clear_board_dev(struct comedi_device *dev)
160 unsigned int i = dev->minor;
161 bool cleared = false;
163 mutex_lock(&comedi_board_minor_table_lock);
164 if (dev == comedi_board_minor_table[i]) {
165 comedi_board_minor_table[i] = NULL;
168 mutex_unlock(&comedi_board_minor_table_lock);
172 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
174 struct comedi_device *dev;
176 mutex_lock(&comedi_board_minor_table_lock);
177 dev = comedi_board_minor_table[minor];
178 comedi_board_minor_table[minor] = NULL;
179 mutex_unlock(&comedi_board_minor_table_lock);
183 static void comedi_free_board_dev(struct comedi_device *dev)
186 comedi_device_cleanup(dev);
187 if (dev->class_dev) {
188 device_destroy(comedi_class,
189 MKDEV(COMEDI_MAJOR, dev->minor));
195 static struct comedi_subdevice
196 *comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned minor)
198 struct comedi_subdevice *s;
199 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
201 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
202 mutex_lock(&comedi_subdevice_minor_table_lock);
203 s = comedi_subdevice_minor_table[i];
204 if (s && s->device != dev)
206 mutex_unlock(&comedi_subdevice_minor_table_lock);
210 static struct comedi_device *comedi_dev_get_from_board_minor(unsigned minor)
212 struct comedi_device *dev;
214 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
215 mutex_lock(&comedi_board_minor_table_lock);
216 dev = comedi_dev_get(comedi_board_minor_table[minor]);
217 mutex_unlock(&comedi_board_minor_table_lock);
221 static struct comedi_device *comedi_dev_get_from_subdevice_minor(unsigned minor)
223 struct comedi_device *dev;
224 struct comedi_subdevice *s;
225 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
227 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
228 mutex_lock(&comedi_subdevice_minor_table_lock);
229 s = comedi_subdevice_minor_table[i];
230 dev = comedi_dev_get(s ? s->device : NULL);
231 mutex_unlock(&comedi_subdevice_minor_table_lock);
236 * comedi_dev_get_from_minor - get comedi device by minor device number
237 * @minor: minor device number
239 * Finds the comedi device associated by the minor device number, if any,
240 * and increments its reference count. The comedi device is prevented from
241 * being freed until a matching call is made to comedi_dev_put().
243 * Return a pointer to the comedi device if it exists, with its usage
244 * reference incremented. Return NULL if no comedi device exists with the
245 * specified minor device number.
247 struct comedi_device *comedi_dev_get_from_minor(unsigned minor)
249 if (minor < COMEDI_NUM_BOARD_MINORS)
250 return comedi_dev_get_from_board_minor(minor);
252 return comedi_dev_get_from_subdevice_minor(minor);
254 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
256 static struct comedi_subdevice *
257 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
259 struct comedi_subdevice *s;
261 if (minor >= COMEDI_NUM_BOARD_MINORS) {
262 s = comedi_subdevice_from_minor(dev, minor);
263 if (s == NULL || (s->subdev_flags & SDF_CMD_READ))
266 return dev->read_subdev;
269 static struct comedi_subdevice *
270 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
272 struct comedi_subdevice *s;
274 if (minor >= COMEDI_NUM_BOARD_MINORS) {
275 s = comedi_subdevice_from_minor(dev, minor);
276 if (s == NULL || (s->subdev_flags & SDF_CMD_WRITE))
279 return dev->write_subdev;
282 static void comedi_file_reset(struct file *file)
284 struct comedi_file *cfp = file->private_data;
285 struct comedi_device *dev = cfp->dev;
286 struct comedi_subdevice *s, *read_s, *write_s;
287 unsigned int minor = iminor(file_inode(file));
289 read_s = dev->read_subdev;
290 write_s = dev->write_subdev;
291 if (minor >= COMEDI_NUM_BOARD_MINORS) {
292 s = comedi_subdevice_from_minor(dev, minor);
293 if (s == NULL || s->subdev_flags & SDF_CMD_READ)
295 if (s == NULL || s->subdev_flags & SDF_CMD_WRITE)
298 cfp->last_attached = dev->attached;
299 cfp->last_detach_count = dev->detach_count;
300 ACCESS_ONCE(cfp->read_subdev) = read_s;
301 ACCESS_ONCE(cfp->write_subdev) = write_s;
304 static void comedi_file_check(struct file *file)
306 struct comedi_file *cfp = file->private_data;
307 struct comedi_device *dev = cfp->dev;
309 if (cfp->last_attached != dev->attached ||
310 cfp->last_detach_count != dev->detach_count)
311 comedi_file_reset(file);
314 static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file)
316 struct comedi_file *cfp = file->private_data;
318 comedi_file_check(file);
319 return ACCESS_ONCE(cfp->read_subdev);
322 static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file)
324 struct comedi_file *cfp = file->private_data;
326 comedi_file_check(file);
327 return ACCESS_ONCE(cfp->write_subdev);
330 static int resize_async_buffer(struct comedi_device *dev,
331 struct comedi_subdevice *s, unsigned new_size)
333 struct comedi_async *async = s->async;
336 if (new_size > async->max_bufsize)
340 dev_dbg(dev->class_dev,
341 "subdevice is busy, cannot resize buffer\n");
344 if (comedi_buf_is_mmapped(s)) {
345 dev_dbg(dev->class_dev,
346 "subdevice is mmapped, cannot resize buffer\n");
350 /* make sure buffer is an integral number of pages (we round up) */
351 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
353 retval = comedi_buf_alloc(dev, s, new_size);
358 retval = s->buf_change(dev, s);
363 dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
364 s->index, async->prealloc_bufsz);
368 /* sysfs attribute files */
370 static ssize_t max_read_buffer_kb_show(struct device *csdev,
371 struct device_attribute *attr, char *buf)
373 unsigned int minor = MINOR(csdev->devt);
374 struct comedi_device *dev;
375 struct comedi_subdevice *s;
376 unsigned int size = 0;
378 dev = comedi_dev_get_from_minor(minor);
382 mutex_lock(&dev->mutex);
383 s = comedi_read_subdevice(dev, minor);
384 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
385 size = s->async->max_bufsize / 1024;
386 mutex_unlock(&dev->mutex);
389 return snprintf(buf, PAGE_SIZE, "%u\n", size);
392 static ssize_t max_read_buffer_kb_store(struct device *csdev,
393 struct device_attribute *attr,
394 const char *buf, size_t count)
396 unsigned int minor = MINOR(csdev->devt);
397 struct comedi_device *dev;
398 struct comedi_subdevice *s;
402 err = kstrtouint(buf, 10, &size);
405 if (size > (UINT_MAX / 1024))
409 dev = comedi_dev_get_from_minor(minor);
413 mutex_lock(&dev->mutex);
414 s = comedi_read_subdevice(dev, minor);
415 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
416 s->async->max_bufsize = size;
419 mutex_unlock(&dev->mutex);
422 return err ? err : count;
424 static DEVICE_ATTR_RW(max_read_buffer_kb);
426 static ssize_t read_buffer_kb_show(struct device *csdev,
427 struct device_attribute *attr, char *buf)
429 unsigned int minor = MINOR(csdev->devt);
430 struct comedi_device *dev;
431 struct comedi_subdevice *s;
432 unsigned int size = 0;
434 dev = comedi_dev_get_from_minor(minor);
438 mutex_lock(&dev->mutex);
439 s = comedi_read_subdevice(dev, minor);
440 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
441 size = s->async->prealloc_bufsz / 1024;
442 mutex_unlock(&dev->mutex);
445 return snprintf(buf, PAGE_SIZE, "%u\n", size);
448 static ssize_t read_buffer_kb_store(struct device *csdev,
449 struct device_attribute *attr,
450 const char *buf, size_t count)
452 unsigned int minor = MINOR(csdev->devt);
453 struct comedi_device *dev;
454 struct comedi_subdevice *s;
458 err = kstrtouint(buf, 10, &size);
461 if (size > (UINT_MAX / 1024))
465 dev = comedi_dev_get_from_minor(minor);
469 mutex_lock(&dev->mutex);
470 s = comedi_read_subdevice(dev, minor);
471 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
472 err = resize_async_buffer(dev, s, size);
475 mutex_unlock(&dev->mutex);
478 return err ? err : count;
480 static DEVICE_ATTR_RW(read_buffer_kb);
482 static ssize_t max_write_buffer_kb_show(struct device *csdev,
483 struct device_attribute *attr,
486 unsigned int minor = MINOR(csdev->devt);
487 struct comedi_device *dev;
488 struct comedi_subdevice *s;
489 unsigned int size = 0;
491 dev = comedi_dev_get_from_minor(minor);
495 mutex_lock(&dev->mutex);
496 s = comedi_write_subdevice(dev, minor);
497 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
498 size = s->async->max_bufsize / 1024;
499 mutex_unlock(&dev->mutex);
502 return snprintf(buf, PAGE_SIZE, "%u\n", size);
505 static ssize_t max_write_buffer_kb_store(struct device *csdev,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
509 unsigned int minor = MINOR(csdev->devt);
510 struct comedi_device *dev;
511 struct comedi_subdevice *s;
515 err = kstrtouint(buf, 10, &size);
518 if (size > (UINT_MAX / 1024))
522 dev = comedi_dev_get_from_minor(minor);
526 mutex_lock(&dev->mutex);
527 s = comedi_write_subdevice(dev, minor);
528 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
529 s->async->max_bufsize = size;
532 mutex_unlock(&dev->mutex);
535 return err ? err : count;
537 static DEVICE_ATTR_RW(max_write_buffer_kb);
539 static ssize_t write_buffer_kb_show(struct device *csdev,
540 struct device_attribute *attr, char *buf)
542 unsigned int minor = MINOR(csdev->devt);
543 struct comedi_device *dev;
544 struct comedi_subdevice *s;
545 unsigned int size = 0;
547 dev = comedi_dev_get_from_minor(minor);
551 mutex_lock(&dev->mutex);
552 s = comedi_write_subdevice(dev, minor);
553 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
554 size = s->async->prealloc_bufsz / 1024;
555 mutex_unlock(&dev->mutex);
558 return snprintf(buf, PAGE_SIZE, "%u\n", size);
561 static ssize_t write_buffer_kb_store(struct device *csdev,
562 struct device_attribute *attr,
563 const char *buf, size_t count)
565 unsigned int minor = MINOR(csdev->devt);
566 struct comedi_device *dev;
567 struct comedi_subdevice *s;
571 err = kstrtouint(buf, 10, &size);
574 if (size > (UINT_MAX / 1024))
578 dev = comedi_dev_get_from_minor(minor);
582 mutex_lock(&dev->mutex);
583 s = comedi_write_subdevice(dev, minor);
584 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
585 err = resize_async_buffer(dev, s, size);
588 mutex_unlock(&dev->mutex);
591 return err ? err : count;
593 static DEVICE_ATTR_RW(write_buffer_kb);
595 static struct attribute *comedi_dev_attrs[] = {
596 &dev_attr_max_read_buffer_kb.attr,
597 &dev_attr_read_buffer_kb.attr,
598 &dev_attr_max_write_buffer_kb.attr,
599 &dev_attr_write_buffer_kb.attr,
602 ATTRIBUTE_GROUPS(comedi_dev);
604 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
605 unsigned mask, unsigned bits)
609 spin_lock_irqsave(&s->spin_lock, flags);
610 s->runflags &= ~mask;
611 s->runflags |= (bits & mask);
612 spin_unlock_irqrestore(&s->spin_lock, flags);
615 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
620 spin_lock_irqsave(&s->spin_lock, flags);
621 runflags = s->runflags;
622 spin_unlock_irqrestore(&s->spin_lock, flags);
627 * comedi_is_subdevice_running - check if async command running on subdevice
628 * @s: comedi_subdevice struct
630 * Return true if an asynchronous comedi command is active on the comedi
631 * subdevice, else return false.
633 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
635 unsigned runflags = comedi_get_subdevice_runflags(s);
637 return (runflags & COMEDI_SRF_RUNNING) ? true : false;
639 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
641 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
643 unsigned runflags = comedi_get_subdevice_runflags(s);
645 return (runflags & COMEDI_SRF_ERROR) ? true : false;
648 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
650 unsigned runflags = comedi_get_subdevice_runflags(s);
652 return (runflags & COMEDI_SRF_BUSY_MASK) ? false : true;
656 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
657 * @s: comedi_subdevice struct
658 * @size: size of the memory to allocate
660 * This also sets the subdevice runflags to allow the core to automatically
661 * free the private data during the detach.
663 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
665 s->private = kzalloc(size, GFP_KERNEL);
667 s->runflags |= COMEDI_SRF_FREE_SPRIV;
670 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
673 * This function restores a subdevice to an idle state.
675 static void do_become_nonbusy(struct comedi_device *dev,
676 struct comedi_subdevice *s)
678 struct comedi_async *async = s->async;
680 comedi_set_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
683 async->inttrig = NULL;
684 kfree(async->cmd.chanlist);
685 async->cmd.chanlist = NULL;
687 wake_up_interruptible_all(&s->async->wait_head);
689 dev_err(dev->class_dev,
690 "BUG: (?) do_become_nonbusy called with async=NULL\n");
695 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
699 if (comedi_is_subdevice_running(s) && s->cancel)
700 ret = s->cancel(dev, s);
702 do_become_nonbusy(dev, s);
707 void comedi_device_cancel_all(struct comedi_device *dev)
709 struct comedi_subdevice *s;
715 for (i = 0; i < dev->n_subdevices; i++) {
716 s = &dev->subdevices[i];
722 static int is_device_busy(struct comedi_device *dev)
724 struct comedi_subdevice *s;
730 for (i = 0; i < dev->n_subdevices; i++) {
731 s = &dev->subdevices[i];
734 if (s->async && comedi_buf_is_mmapped(s))
742 * COMEDI_DEVCONFIG ioctl
743 * attaches (and configures) or detaches a legacy device
746 * pointer to comedi_devconfig structure (NULL if detaching)
749 * comedi_devconfig structure (if attaching)
754 static int do_devconfig_ioctl(struct comedi_device *dev,
755 struct comedi_devconfig __user *arg)
757 struct comedi_devconfig it;
759 if (!capable(CAP_SYS_ADMIN))
763 if (is_device_busy(dev))
766 struct module *driver_module = dev->driver->module;
768 comedi_device_detach(dev);
769 module_put(driver_module);
774 if (copy_from_user(&it, arg, sizeof(it)))
777 it.board_name[COMEDI_NAMELEN - 1] = 0;
779 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
780 dev_warn(dev->class_dev,
781 "comedi_config --init_data is deprecated\n");
785 if (dev->minor >= comedi_num_legacy_minors)
786 /* don't re-use dynamically allocated comedi devices */
789 /* This increments the driver module count on success. */
790 return comedi_device_attach(dev, &it);
794 * COMEDI_BUFCONFIG ioctl
795 * buffer configuration
798 * pointer to comedi_bufconfig structure
801 * comedi_bufconfig structure
804 * modified comedi_bufconfig structure
806 static int do_bufconfig_ioctl(struct comedi_device *dev,
807 struct comedi_bufconfig __user *arg)
809 struct comedi_bufconfig bc;
810 struct comedi_async *async;
811 struct comedi_subdevice *s;
814 if (copy_from_user(&bc, arg, sizeof(bc)))
817 if (bc.subdevice >= dev->n_subdevices)
820 s = &dev->subdevices[bc.subdevice];
824 dev_dbg(dev->class_dev,
825 "subdevice does not have async capability\n");
831 if (bc.maximum_size) {
832 if (!capable(CAP_SYS_ADMIN))
835 async->max_bufsize = bc.maximum_size;
839 retval = resize_async_buffer(dev, s, bc.size);
844 bc.size = async->prealloc_bufsz;
845 bc.maximum_size = async->max_bufsize;
848 if (copy_to_user(arg, &bc, sizeof(bc)))
855 * COMEDI_DEVINFO ioctl
859 * pointer to comedi_devinfo structure
865 * comedi_devinfo structure
867 static int do_devinfo_ioctl(struct comedi_device *dev,
868 struct comedi_devinfo __user *arg,
871 struct comedi_subdevice *s;
872 struct comedi_devinfo devinfo;
874 memset(&devinfo, 0, sizeof(devinfo));
876 /* fill devinfo structure */
877 devinfo.version_code = COMEDI_VERSION_CODE;
878 devinfo.n_subdevs = dev->n_subdevices;
879 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
880 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
882 s = comedi_file_read_subdevice(file);
884 devinfo.read_subdevice = s->index;
886 devinfo.read_subdevice = -1;
888 s = comedi_file_write_subdevice(file);
890 devinfo.write_subdevice = s->index;
892 devinfo.write_subdevice = -1;
894 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
901 * COMEDI_SUBDINFO ioctl
905 * pointer to array of comedi_subdinfo structures
911 * array of comedi_subdinfo structures
913 static int do_subdinfo_ioctl(struct comedi_device *dev,
914 struct comedi_subdinfo __user *arg, void *file)
917 struct comedi_subdinfo *tmp, *us;
918 struct comedi_subdevice *s;
920 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
924 /* fill subdinfo structs */
925 for (i = 0; i < dev->n_subdevices; i++) {
926 s = &dev->subdevices[i];
930 us->n_chan = s->n_chan;
931 us->subd_flags = s->subdev_flags;
932 if (comedi_is_subdevice_running(s))
933 us->subd_flags |= SDF_RUNNING;
934 #define TIMER_nanosec 5 /* backwards compatibility */
935 us->timer_type = TIMER_nanosec;
936 us->len_chanlist = s->len_chanlist;
937 us->maxdata = s->maxdata;
938 if (s->range_table) {
940 (i << 24) | (0 << 16) | (s->range_table->length);
942 us->range_type = 0; /* XXX */
946 us->subd_flags |= SDF_BUSY;
948 us->subd_flags |= SDF_BUSY_OWNER;
950 us->subd_flags |= SDF_LOCKED;
952 us->subd_flags |= SDF_LOCK_OWNER;
953 if (!s->maxdata && s->maxdata_list)
954 us->subd_flags |= SDF_MAXDATA;
955 if (s->range_table_list)
956 us->subd_flags |= SDF_RANGETYPE;
958 us->subd_flags |= SDF_CMD;
960 if (s->insn_bits != &insn_inval)
961 us->insn_bits_support = COMEDI_SUPPORTED;
963 us->insn_bits_support = COMEDI_UNSUPPORTED;
966 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
970 return ret ? -EFAULT : 0;
974 * COMEDI_CHANINFO ioctl
975 * subdevice channel info
978 * pointer to comedi_chaninfo structure
981 * comedi_chaninfo structure
984 * array of maxdata values to chaninfo->maxdata_list if requested
985 * array of range table lengths to chaninfo->range_table_list if requested
987 static int do_chaninfo_ioctl(struct comedi_device *dev,
988 struct comedi_chaninfo __user *arg)
990 struct comedi_subdevice *s;
991 struct comedi_chaninfo it;
993 if (copy_from_user(&it, arg, sizeof(it)))
996 if (it.subdev >= dev->n_subdevices)
998 s = &dev->subdevices[it.subdev];
1000 if (it.maxdata_list) {
1001 if (s->maxdata || !s->maxdata_list)
1003 if (copy_to_user(it.maxdata_list, s->maxdata_list,
1004 s->n_chan * sizeof(unsigned int)))
1009 return -EINVAL; /* flaglist not supported */
1014 if (!s->range_table_list)
1016 for (i = 0; i < s->n_chan; i++) {
1019 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
1020 (s->range_table_list[i]->length);
1021 if (put_user(x, it.rangelist + i))
1025 if (copy_to_user(it.rangelist, s->range_type_list,
1026 s->n_chan * sizeof(unsigned int)))
1035 * COMEDI_BUFINFO ioctl
1036 * buffer information
1039 * pointer to comedi_bufinfo structure
1042 * comedi_bufinfo structure
1045 * modified comedi_bufinfo structure
1047 static int do_bufinfo_ioctl(struct comedi_device *dev,
1048 struct comedi_bufinfo __user *arg, void *file)
1050 struct comedi_bufinfo bi;
1051 struct comedi_subdevice *s;
1052 struct comedi_async *async;
1054 if (copy_from_user(&bi, arg, sizeof(bi)))
1057 if (bi.subdevice >= dev->n_subdevices)
1060 s = &dev->subdevices[bi.subdevice];
1065 dev_dbg(dev->class_dev,
1066 "subdevice does not have async capability\n");
1067 bi.buf_write_ptr = 0;
1068 bi.buf_read_ptr = 0;
1069 bi.buf_write_count = 0;
1070 bi.buf_read_count = 0;
1072 bi.bytes_written = 0;
1077 bi.bytes_written = 0;
1078 goto copyback_position;
1080 if (s->busy != file)
1083 if (bi.bytes_read && !(async->cmd.flags & CMDF_WRITE)) {
1084 bi.bytes_read = comedi_buf_read_alloc(s, bi.bytes_read);
1085 comedi_buf_read_free(s, bi.bytes_read);
1087 if (comedi_is_subdevice_idle(s) &&
1088 comedi_buf_n_bytes_ready(s) == 0) {
1089 do_become_nonbusy(dev, s);
1093 if (bi.bytes_written && (async->cmd.flags & CMDF_WRITE)) {
1095 comedi_buf_write_alloc(s, bi.bytes_written);
1096 comedi_buf_write_free(s, bi.bytes_written);
1100 bi.buf_write_count = async->buf_write_count;
1101 bi.buf_write_ptr = async->buf_write_ptr;
1102 bi.buf_read_count = async->buf_read_count;
1103 bi.buf_read_ptr = async->buf_read_ptr;
1106 if (copy_to_user(arg, &bi, sizeof(bi)))
1112 static int check_insn_config_length(struct comedi_insn *insn,
1119 case INSN_CONFIG_DIO_OUTPUT:
1120 case INSN_CONFIG_DIO_INPUT:
1121 case INSN_CONFIG_DISARM:
1122 case INSN_CONFIG_RESET:
1126 case INSN_CONFIG_ARM:
1127 case INSN_CONFIG_DIO_QUERY:
1128 case INSN_CONFIG_BLOCK_SIZE:
1129 case INSN_CONFIG_FILTER:
1130 case INSN_CONFIG_SERIAL_CLOCK:
1131 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1132 case INSN_CONFIG_ALT_SOURCE:
1133 case INSN_CONFIG_SET_COUNTER_MODE:
1134 case INSN_CONFIG_8254_READ_STATUS:
1135 case INSN_CONFIG_SET_ROUTING:
1136 case INSN_CONFIG_GET_ROUTING:
1137 case INSN_CONFIG_GET_PWM_STATUS:
1138 case INSN_CONFIG_PWM_SET_PERIOD:
1139 case INSN_CONFIG_PWM_GET_PERIOD:
1143 case INSN_CONFIG_SET_GATE_SRC:
1144 case INSN_CONFIG_GET_GATE_SRC:
1145 case INSN_CONFIG_SET_CLOCK_SRC:
1146 case INSN_CONFIG_GET_CLOCK_SRC:
1147 case INSN_CONFIG_SET_OTHER_SRC:
1148 case INSN_CONFIG_GET_COUNTER_STATUS:
1149 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1150 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1151 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1155 case INSN_CONFIG_PWM_OUTPUT:
1156 case INSN_CONFIG_ANALOG_TRIG:
1160 case INSN_CONFIG_DIGITAL_TRIG:
1165 * by default we allow the insn since we don't have checks for
1166 * all possible cases yet
1169 pr_warn("No check for data length of config insn id %i is implemented\n",
1171 pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1172 pr_warn("Assuming n=%i is correct\n", insn->n);
1178 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1179 unsigned int *data, void *file)
1181 struct comedi_subdevice *s;
1185 if (insn->insn & INSN_MASK_SPECIAL) {
1186 /* a non-subdevice instruction */
1188 switch (insn->insn) {
1198 do_gettimeofday(&tv);
1199 data[0] = tv.tv_sec;
1200 data[1] = tv.tv_usec;
1206 if (insn->n != 1 || data[0] >= 100000) {
1210 udelay(data[0] / 1000);
1218 if (insn->subdev >= dev->n_subdevices) {
1219 dev_dbg(dev->class_dev,
1220 "%d not usable subdevice\n",
1225 s = &dev->subdevices[insn->subdev];
1227 dev_dbg(dev->class_dev, "no async\n");
1231 if (!s->async->inttrig) {
1232 dev_dbg(dev->class_dev, "no inttrig\n");
1236 ret = s->async->inttrig(dev, s, data[0]);
1241 dev_dbg(dev->class_dev, "invalid insn\n");
1246 /* a subdevice instruction */
1247 unsigned int maxdata;
1249 if (insn->subdev >= dev->n_subdevices) {
1250 dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1255 s = &dev->subdevices[insn->subdev];
1257 if (s->type == COMEDI_SUBD_UNUSED) {
1258 dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1264 /* are we locked? (ioctl lock) */
1265 if (s->lock && s->lock != file) {
1266 dev_dbg(dev->class_dev, "device locked\n");
1271 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1274 dev_dbg(dev->class_dev, "bad chanspec\n");
1282 /* This looks arbitrary. It is. */
1283 s->busy = &parse_insn;
1284 switch (insn->insn) {
1286 ret = s->insn_read(dev, s, insn, data);
1287 if (ret == -ETIMEDOUT) {
1288 dev_dbg(dev->class_dev,
1289 "subdevice %d read instruction timed out\n",
1294 maxdata = s->maxdata_list
1295 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1297 for (i = 0; i < insn->n; ++i) {
1298 if (data[i] > maxdata) {
1300 dev_dbg(dev->class_dev,
1301 "bad data value(s)\n");
1306 ret = s->insn_write(dev, s, insn, data);
1307 if (ret == -ETIMEDOUT) {
1308 dev_dbg(dev->class_dev,
1309 "subdevice %d write instruction timed out\n",
1319 * Most drivers ignore the base channel in
1320 * insn->chanspec. Fix this here if
1321 * the subdevice has <= 32 channels.
1323 unsigned int orig_mask = data[0];
1324 unsigned int shift = 0;
1326 if (s->n_chan <= 32) {
1327 shift = CR_CHAN(insn->chanspec);
1334 ret = s->insn_bits(dev, s, insn, data);
1335 data[0] = orig_mask;
1341 ret = check_insn_config_length(insn, data);
1344 ret = s->insn_config(dev, s, insn, data);
1359 * COMEDI_INSNLIST ioctl
1360 * synchronous instruction list
1363 * pointer to comedi_insnlist structure
1366 * comedi_insnlist structure
1367 * array of comedi_insn structures from insnlist->insns pointer
1368 * data (for writes) from insns[].data pointers
1371 * data (for reads) to insns[].data pointers
1373 /* arbitrary limits */
1374 #define MAX_SAMPLES 256
1375 static int do_insnlist_ioctl(struct comedi_device *dev,
1376 struct comedi_insnlist __user *arg, void *file)
1378 struct comedi_insnlist insnlist;
1379 struct comedi_insn *insns = NULL;
1380 unsigned int *data = NULL;
1384 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1387 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1393 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1399 if (copy_from_user(insns, insnlist.insns,
1400 sizeof(*insns) * insnlist.n_insns)) {
1401 dev_dbg(dev->class_dev, "copy_from_user failed\n");
1406 for (i = 0; i < insnlist.n_insns; i++) {
1407 if (insns[i].n > MAX_SAMPLES) {
1408 dev_dbg(dev->class_dev,
1409 "number of samples too large\n");
1413 if (insns[i].insn & INSN_MASK_WRITE) {
1414 if (copy_from_user(data, insns[i].data,
1415 insns[i].n * sizeof(unsigned int))) {
1416 dev_dbg(dev->class_dev,
1417 "copy_from_user failed\n");
1422 ret = parse_insn(dev, insns + i, data, file);
1425 if (insns[i].insn & INSN_MASK_READ) {
1426 if (copy_to_user(insns[i].data, data,
1427 insns[i].n * sizeof(unsigned int))) {
1428 dev_dbg(dev->class_dev,
1429 "copy_to_user failed\n");
1449 * synchronous instruction
1452 * pointer to comedi_insn structure
1455 * comedi_insn structure
1456 * data (for writes) from insn->data pointer
1459 * data (for reads) to insn->data pointer
1461 static int do_insn_ioctl(struct comedi_device *dev,
1462 struct comedi_insn __user *arg, void *file)
1464 struct comedi_insn insn;
1465 unsigned int *data = NULL;
1468 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1474 if (copy_from_user(&insn, arg, sizeof(insn))) {
1479 /* This is where the behavior of insn and insnlist deviate. */
1480 if (insn.n > MAX_SAMPLES)
1481 insn.n = MAX_SAMPLES;
1482 if (insn.insn & INSN_MASK_WRITE) {
1483 if (copy_from_user(data,
1485 insn.n * sizeof(unsigned int))) {
1490 ret = parse_insn(dev, &insn, data, file);
1493 if (insn.insn & INSN_MASK_READ) {
1494 if (copy_to_user(insn.data,
1496 insn.n * sizeof(unsigned int))) {
1509 static int __comedi_get_user_cmd(struct comedi_device *dev,
1510 struct comedi_cmd __user *arg,
1511 struct comedi_cmd *cmd)
1513 struct comedi_subdevice *s;
1515 if (copy_from_user(cmd, arg, sizeof(*cmd))) {
1516 dev_dbg(dev->class_dev, "bad cmd address\n");
1520 if (cmd->subdev >= dev->n_subdevices) {
1521 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
1525 s = &dev->subdevices[cmd->subdev];
1527 if (s->type == COMEDI_SUBD_UNUSED) {
1528 dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1533 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1534 dev_dbg(dev->class_dev,
1535 "subdevice %d does not support commands\n",
1540 /* make sure channel/gain list isn't too long */
1541 if (cmd->chanlist_len > s->len_chanlist) {
1542 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1543 cmd->chanlist_len, s->len_chanlist);
1548 * Set the CMDF_WRITE flag to the correct state if the subdevice
1549 * supports only "read" commands or only "write" commands.
1551 switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
1553 cmd->flags &= ~CMDF_WRITE;
1556 cmd->flags |= CMDF_WRITE;
1565 static int __comedi_get_user_chanlist(struct comedi_device *dev,
1566 struct comedi_subdevice *s,
1567 unsigned int __user *user_chanlist,
1568 struct comedi_cmd *cmd)
1570 unsigned int *chanlist;
1573 cmd->chanlist = NULL;
1574 chanlist = memdup_user(user_chanlist,
1575 cmd->chanlist_len * sizeof(unsigned int));
1576 if (IS_ERR(chanlist))
1577 return PTR_ERR(chanlist);
1579 /* make sure each element in channel/gain list is valid */
1580 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1586 cmd->chanlist = chanlist;
1593 * asynchronous acquisition command set-up
1596 * pointer to comedi_cmd structure
1599 * comedi_cmd structure
1600 * channel/range list from cmd->chanlist pointer
1603 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1605 static int do_cmd_ioctl(struct comedi_device *dev,
1606 struct comedi_cmd __user *arg, void *file)
1608 struct comedi_cmd cmd;
1609 struct comedi_subdevice *s;
1610 struct comedi_async *async;
1611 unsigned int __user *user_chanlist;
1614 /* get the user's cmd and do some simple validation */
1615 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1619 /* save user's chanlist pointer so it can be restored later */
1620 user_chanlist = (unsigned int __user *)cmd.chanlist;
1622 s = &dev->subdevices[cmd.subdev];
1625 /* are we locked? (ioctl lock) */
1626 if (s->lock && s->lock != file) {
1627 dev_dbg(dev->class_dev, "subdevice locked\n");
1633 dev_dbg(dev->class_dev, "subdevice busy\n");
1637 /* make sure channel/gain list isn't too short */
1638 if (cmd.chanlist_len < 1) {
1639 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
1645 async->cmd.data = NULL;
1647 /* load channel/gain list */
1648 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1652 ret = s->do_cmdtest(dev, s, &async->cmd);
1654 if (async->cmd.flags & CMDF_BOGUS || ret) {
1655 dev_dbg(dev->class_dev, "test returned %d\n", ret);
1657 /* restore chanlist pointer before copying back */
1658 cmd.chanlist = (unsigned int __force *)user_chanlist;
1660 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1661 dev_dbg(dev->class_dev, "fault writing cmd\n");
1669 if (!async->prealloc_bufsz) {
1671 dev_dbg(dev->class_dev, "no buffer (?)\n");
1675 comedi_buf_reset(s);
1677 async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
1678 if (async->cmd.flags & CMDF_WAKE_EOS)
1679 async->cb_mask |= COMEDI_CB_EOS;
1681 comedi_set_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
1682 COMEDI_SRF_RUNNING);
1685 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1686 * race with comedi_read() or comedi_write().
1689 ret = s->do_cmd(dev, s);
1694 do_become_nonbusy(dev, s);
1700 * COMEDI_CMDTEST ioctl
1701 * asynchronous aquisition command testing
1704 * pointer to comedi_cmd structure
1707 * comedi_cmd structure
1708 * channel/range list from cmd->chanlist pointer
1711 * possibly modified comedi_cmd structure
1713 static int do_cmdtest_ioctl(struct comedi_device *dev,
1714 struct comedi_cmd __user *arg, void *file)
1716 struct comedi_cmd cmd;
1717 struct comedi_subdevice *s;
1718 unsigned int __user *user_chanlist;
1721 /* get the user's cmd and do some simple validation */
1722 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1726 /* save user's chanlist pointer so it can be restored later */
1727 user_chanlist = (unsigned int __user *)cmd.chanlist;
1729 s = &dev->subdevices[cmd.subdev];
1731 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1732 if (user_chanlist) {
1733 /* load channel/gain list */
1734 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1739 ret = s->do_cmdtest(dev, s, &cmd);
1741 kfree(cmd.chanlist); /* free kernel copy of user chanlist */
1743 /* restore chanlist pointer before copying back */
1744 cmd.chanlist = (unsigned int __force *)user_chanlist;
1746 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1747 dev_dbg(dev->class_dev, "bad cmd address\n");
1767 static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
1771 unsigned long flags;
1772 struct comedi_subdevice *s;
1774 if (arg >= dev->n_subdevices)
1776 s = &dev->subdevices[arg];
1778 spin_lock_irqsave(&s->spin_lock, flags);
1779 if (s->busy || s->lock)
1783 spin_unlock_irqrestore(&s->spin_lock, flags);
1789 * COMEDI_UNLOCK ioctl
1801 static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
1804 struct comedi_subdevice *s;
1806 if (arg >= dev->n_subdevices)
1808 s = &dev->subdevices[arg];
1813 if (s->lock && s->lock != file)
1816 if (s->lock == file)
1823 * COMEDI_CANCEL ioctl
1824 * cancel asynchronous acquisition
1835 static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
1838 struct comedi_subdevice *s;
1840 if (arg >= dev->n_subdevices)
1842 s = &dev->subdevices[arg];
1843 if (s->async == NULL)
1849 if (s->busy != file)
1852 return do_cancel(dev, s);
1857 * instructs driver to synchronize buffers
1868 static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
1871 struct comedi_subdevice *s;
1873 if (arg >= dev->n_subdevices)
1875 s = &dev->subdevices[arg];
1880 if (s->busy != file)
1884 return s->poll(dev, s);
1890 * COMEDI_SETRSUBD ioctl
1891 * sets the current "read" subdevice on a per-file basis
1902 static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1905 struct comedi_file *cfp = file->private_data;
1906 struct comedi_subdevice *s_old, *s_new;
1908 if (arg >= dev->n_subdevices)
1911 s_new = &dev->subdevices[arg];
1912 s_old = comedi_file_read_subdevice(file);
1914 return 0; /* no change */
1916 if (!(s_new->subdev_flags & SDF_CMD_READ))
1920 * Check the file isn't still busy handling a "read" command on the
1921 * old subdevice (if any).
1923 if (s_old && s_old->busy == file && s_old->async &&
1924 !(s_old->async->cmd.flags & CMDF_WRITE))
1927 ACCESS_ONCE(cfp->read_subdev) = s_new;
1932 * COMEDI_SETWSUBD ioctl
1933 * sets the current "write" subdevice on a per-file basis
1944 static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1947 struct comedi_file *cfp = file->private_data;
1948 struct comedi_subdevice *s_old, *s_new;
1950 if (arg >= dev->n_subdevices)
1953 s_new = &dev->subdevices[arg];
1954 s_old = comedi_file_write_subdevice(file);
1956 return 0; /* no change */
1958 if (!(s_new->subdev_flags & SDF_CMD_WRITE))
1962 * Check the file isn't still busy handling a "write" command on the
1963 * old subdevice (if any).
1965 if (s_old && s_old->busy == file && s_old->async &&
1966 (s_old->async->cmd.flags & CMDF_WRITE))
1969 ACCESS_ONCE(cfp->write_subdev) = s_new;
1973 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1976 unsigned minor = iminor(file_inode(file));
1977 struct comedi_file *cfp = file->private_data;
1978 struct comedi_device *dev = cfp->dev;
1981 mutex_lock(&dev->mutex);
1984 * Device config is special, because it must work on
1985 * an unconfigured device.
1987 if (cmd == COMEDI_DEVCONFIG) {
1988 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1989 /* Device config not appropriate on non-board minors. */
1993 rc = do_devconfig_ioctl(dev,
1994 (struct comedi_devconfig __user *)arg);
1997 dev->minor >= comedi_num_legacy_minors) {
1999 * Successfully unconfigured a dynamically
2000 * allocated device. Try and remove it.
2002 if (comedi_clear_board_dev(dev)) {
2003 mutex_unlock(&dev->mutex);
2004 comedi_free_board_dev(dev);
2012 if (!dev->attached) {
2013 dev_dbg(dev->class_dev, "no driver attached\n");
2019 case COMEDI_BUFCONFIG:
2020 rc = do_bufconfig_ioctl(dev,
2021 (struct comedi_bufconfig __user *)arg);
2023 case COMEDI_DEVINFO:
2024 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
2027 case COMEDI_SUBDINFO:
2028 rc = do_subdinfo_ioctl(dev,
2029 (struct comedi_subdinfo __user *)arg,
2032 case COMEDI_CHANINFO:
2033 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
2035 case COMEDI_RANGEINFO:
2036 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
2038 case COMEDI_BUFINFO:
2039 rc = do_bufinfo_ioctl(dev,
2040 (struct comedi_bufinfo __user *)arg,
2044 rc = do_lock_ioctl(dev, arg, file);
2047 rc = do_unlock_ioctl(dev, arg, file);
2050 rc = do_cancel_ioctl(dev, arg, file);
2053 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
2055 case COMEDI_CMDTEST:
2056 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
2059 case COMEDI_INSNLIST:
2060 rc = do_insnlist_ioctl(dev,
2061 (struct comedi_insnlist __user *)arg,
2065 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
2069 rc = do_poll_ioctl(dev, arg, file);
2071 case COMEDI_SETRSUBD:
2072 rc = do_setrsubd_ioctl(dev, arg, file);
2074 case COMEDI_SETWSUBD:
2075 rc = do_setwsubd_ioctl(dev, arg, file);
2083 mutex_unlock(&dev->mutex);
2087 static void comedi_vm_open(struct vm_area_struct *area)
2089 struct comedi_buf_map *bm;
2091 bm = area->vm_private_data;
2092 comedi_buf_map_get(bm);
2095 static void comedi_vm_close(struct vm_area_struct *area)
2097 struct comedi_buf_map *bm;
2099 bm = area->vm_private_data;
2100 comedi_buf_map_put(bm);
2103 static struct vm_operations_struct comedi_vm_ops = {
2104 .open = comedi_vm_open,
2105 .close = comedi_vm_close,
2108 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2110 struct comedi_file *cfp = file->private_data;
2111 struct comedi_device *dev = cfp->dev;
2112 struct comedi_subdevice *s;
2113 struct comedi_async *async;
2114 struct comedi_buf_map *bm = NULL;
2115 unsigned long start = vma->vm_start;
2122 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2123 * and down-reading &dev->attach_lock should normally succeed without
2124 * contention unless the device is in the process of being attached
2127 if (!down_read_trylock(&dev->attach_lock))
2130 if (!dev->attached) {
2131 dev_dbg(dev->class_dev, "no driver attached\n");
2136 if (vma->vm_flags & VM_WRITE)
2137 s = comedi_file_write_subdevice(file);
2139 s = comedi_file_read_subdevice(file);
2151 if (vma->vm_pgoff != 0) {
2152 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
2157 size = vma->vm_end - vma->vm_start;
2158 if (size > async->prealloc_bufsz) {
2162 if (size & (~PAGE_MASK)) {
2167 n_pages = size >> PAGE_SHIFT;
2169 /* get reference to current buf map (if any) */
2170 bm = comedi_buf_map_from_subdev_get(s);
2171 if (!bm || n_pages > bm->n_pages) {
2175 for (i = 0; i < n_pages; ++i) {
2176 struct comedi_buf_page *buf = &bm->page_list[i];
2178 if (remap_pfn_range(vma, start,
2179 page_to_pfn(virt_to_page(buf->virt_addr)),
2180 PAGE_SIZE, PAGE_SHARED)) {
2187 vma->vm_ops = &comedi_vm_ops;
2188 vma->vm_private_data = bm;
2190 vma->vm_ops->open(vma);
2194 up_read(&dev->attach_lock);
2195 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
2199 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2201 unsigned int mask = 0;
2202 struct comedi_file *cfp = file->private_data;
2203 struct comedi_device *dev = cfp->dev;
2204 struct comedi_subdevice *s;
2206 mutex_lock(&dev->mutex);
2208 if (!dev->attached) {
2209 dev_dbg(dev->class_dev, "no driver attached\n");
2213 s = comedi_file_read_subdevice(file);
2214 if (s && s->async) {
2215 poll_wait(file, &s->async->wait_head, wait);
2216 if (!s->busy || !comedi_is_subdevice_running(s) ||
2217 (s->async->cmd.flags & CMDF_WRITE) ||
2218 comedi_buf_read_n_available(s) > 0)
2219 mask |= POLLIN | POLLRDNORM;
2222 s = comedi_file_write_subdevice(file);
2223 if (s && s->async) {
2224 unsigned int bps = comedi_bytes_per_sample(s);
2226 poll_wait(file, &s->async->wait_head, wait);
2227 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
2228 if (!s->busy || !comedi_is_subdevice_running(s) ||
2229 !(s->async->cmd.flags & CMDF_WRITE) ||
2230 comedi_buf_write_n_allocated(s) >= bps)
2231 mask |= POLLOUT | POLLWRNORM;
2235 mutex_unlock(&dev->mutex);
2239 static ssize_t comedi_write(struct file *file, const char __user *buf,
2240 size_t nbytes, loff_t *offset)
2242 struct comedi_subdevice *s;
2243 struct comedi_async *async;
2244 int n, m, count = 0, retval = 0;
2245 DECLARE_WAITQUEUE(wait, current);
2246 struct comedi_file *cfp = file->private_data;
2247 struct comedi_device *dev = cfp->dev;
2248 bool on_wait_queue = false;
2250 unsigned int old_detach_count;
2252 /* Protect against device detachment during operation. */
2253 down_read(&dev->attach_lock);
2254 attach_locked = true;
2255 old_detach_count = dev->detach_count;
2257 if (!dev->attached) {
2258 dev_dbg(dev->class_dev, "no driver attached\n");
2263 s = comedi_file_write_subdevice(file);
2264 if (!s || !s->async) {
2271 if (!s->busy || !nbytes)
2273 if (s->busy != file) {
2277 if (!(async->cmd.flags & CMDF_WRITE)) {
2282 add_wait_queue(&async->wait_head, &wait);
2283 on_wait_queue = true;
2284 while (nbytes > 0 && !retval) {
2285 set_current_state(TASK_INTERRUPTIBLE);
2287 if (!comedi_is_subdevice_running(s)) {
2289 struct comedi_subdevice *new_s;
2291 if (comedi_is_subdevice_in_error(s))
2296 * To avoid deadlock, cannot acquire dev->mutex
2297 * while dev->attach_lock is held. Need to
2298 * remove task from the async wait queue before
2299 * releasing dev->attach_lock, as it might not
2300 * be valid afterwards.
2302 remove_wait_queue(&async->wait_head, &wait);
2303 on_wait_queue = false;
2304 up_read(&dev->attach_lock);
2305 attach_locked = false;
2306 mutex_lock(&dev->mutex);
2308 * Become non-busy unless things have changed
2309 * behind our back. Checking dev->detach_count
2310 * is unchanged ought to be sufficient (unless
2311 * there have been 2**32 detaches in the
2312 * meantime!), but check the subdevice pointer
2313 * as well just in case.
2315 new_s = comedi_file_write_subdevice(file);
2316 if (dev->attached &&
2317 old_detach_count == dev->detach_count &&
2318 s == new_s && new_s->async == async)
2319 do_become_nonbusy(dev, s);
2320 mutex_unlock(&dev->mutex);
2328 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2329 m = async->prealloc_bufsz - async->buf_write_ptr;
2330 comedi_buf_write_alloc(s, async->prealloc_bufsz);
2331 if (m > comedi_buf_write_n_allocated(s))
2332 m = comedi_buf_write_n_allocated(s);
2337 if (file->f_flags & O_NONBLOCK) {
2342 if (signal_pending(current)) {
2343 retval = -ERESTARTSYS;
2348 if (s->busy != file) {
2352 if (!(async->cmd.flags & CMDF_WRITE)) {
2359 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2365 comedi_buf_write_free(s, n);
2371 break; /* makes device work like a pipe */
2375 remove_wait_queue(&async->wait_head, &wait);
2376 set_current_state(TASK_RUNNING);
2378 up_read(&dev->attach_lock);
2380 return count ? count : retval;
2383 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2386 struct comedi_subdevice *s;
2387 struct comedi_async *async;
2388 int n, m, count = 0, retval = 0;
2389 DECLARE_WAITQUEUE(wait, current);
2390 struct comedi_file *cfp = file->private_data;
2391 struct comedi_device *dev = cfp->dev;
2392 unsigned int old_detach_count;
2393 bool become_nonbusy = false;
2396 /* Protect against device detachment during operation. */
2397 down_read(&dev->attach_lock);
2398 attach_locked = true;
2399 old_detach_count = dev->detach_count;
2401 if (!dev->attached) {
2402 dev_dbg(dev->class_dev, "no driver attached\n");
2407 s = comedi_file_read_subdevice(file);
2408 if (!s || !s->async) {
2414 if (!s->busy || !nbytes)
2416 if (s->busy != file) {
2420 if (async->cmd.flags & CMDF_WRITE) {
2425 add_wait_queue(&async->wait_head, &wait);
2426 while (nbytes > 0 && !retval) {
2427 set_current_state(TASK_INTERRUPTIBLE);
2431 m = comedi_buf_read_n_available(s);
2432 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2433 m = async->prealloc_bufsz - async->buf_read_ptr;
2438 if (!comedi_is_subdevice_running(s)) {
2439 if (comedi_is_subdevice_in_error(s))
2443 become_nonbusy = true;
2446 if (file->f_flags & O_NONBLOCK) {
2451 if (signal_pending(current)) {
2452 retval = -ERESTARTSYS;
2459 if (s->busy != file) {
2463 if (async->cmd.flags & CMDF_WRITE) {
2469 m = copy_to_user(buf, async->prealloc_buf +
2470 async->buf_read_ptr, n);
2476 comedi_buf_read_alloc(s, n);
2477 comedi_buf_read_free(s, n);
2483 break; /* makes device work like a pipe */
2485 remove_wait_queue(&async->wait_head, &wait);
2486 set_current_state(TASK_RUNNING);
2487 if (become_nonbusy || comedi_is_subdevice_idle(s)) {
2488 struct comedi_subdevice *new_s;
2491 * To avoid deadlock, cannot acquire dev->mutex
2492 * while dev->attach_lock is held.
2494 up_read(&dev->attach_lock);
2495 attach_locked = false;
2496 mutex_lock(&dev->mutex);
2498 * Check device hasn't become detached behind our back.
2499 * Checking dev->detach_count is unchanged ought to be
2500 * sufficient (unless there have been 2**32 detaches in the
2501 * meantime!), but check the subdevice pointer as well just in
2504 new_s = comedi_file_read_subdevice(file);
2505 if (dev->attached && old_detach_count == dev->detach_count &&
2506 s == new_s && new_s->async == async) {
2507 if (become_nonbusy || comedi_buf_n_bytes_ready(s) == 0)
2508 do_become_nonbusy(dev, s);
2510 mutex_unlock(&dev->mutex);
2514 up_read(&dev->attach_lock);
2516 return count ? count : retval;
2519 static int comedi_open(struct inode *inode, struct file *file)
2521 const unsigned minor = iminor(inode);
2522 struct comedi_file *cfp;
2523 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2527 pr_debug("invalid minor number\n");
2531 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2537 mutex_lock(&dev->mutex);
2538 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2539 dev_dbg(dev->class_dev, "not attached and not CAP_NET_ADMIN\n");
2543 if (dev->attached && dev->use_count == 0) {
2544 if (!try_module_get(dev->driver->module)) {
2549 rc = dev->open(dev);
2551 module_put(dev->driver->module);
2558 file->private_data = cfp;
2559 comedi_file_reset(file);
2563 mutex_unlock(&dev->mutex);
2565 comedi_dev_put(dev);
2571 static int comedi_fasync(int fd, struct file *file, int on)
2573 struct comedi_file *cfp = file->private_data;
2574 struct comedi_device *dev = cfp->dev;
2576 return fasync_helper(fd, file, on, &dev->async_queue);
2579 static int comedi_close(struct inode *inode, struct file *file)
2581 struct comedi_file *cfp = file->private_data;
2582 struct comedi_device *dev = cfp->dev;
2583 struct comedi_subdevice *s = NULL;
2586 mutex_lock(&dev->mutex);
2588 if (dev->subdevices) {
2589 for (i = 0; i < dev->n_subdevices; i++) {
2590 s = &dev->subdevices[i];
2592 if (s->busy == file)
2594 if (s->lock == file)
2598 if (dev->attached && dev->use_count == 1) {
2601 module_put(dev->driver->module);
2606 mutex_unlock(&dev->mutex);
2607 comedi_dev_put(dev);
2613 static const struct file_operations comedi_fops = {
2614 .owner = THIS_MODULE,
2615 .unlocked_ioctl = comedi_unlocked_ioctl,
2616 .compat_ioctl = comedi_compat_ioctl,
2617 .open = comedi_open,
2618 .release = comedi_close,
2619 .read = comedi_read,
2620 .write = comedi_write,
2621 .mmap = comedi_mmap,
2622 .poll = comedi_poll,
2623 .fasync = comedi_fasync,
2624 .llseek = noop_llseek,
2628 * comedi_event - handle events for asynchronous comedi command
2629 * @dev: comedi_device struct
2630 * @s: comedi_subdevice struct associated with dev
2631 * Context: interrupt (usually), s->spin_lock spin-lock not held
2633 * If an asynchronous comedi command is active on the subdevice, process
2634 * any COMEDI_CB_... event flags that have been set, usually by an
2635 * interrupt handler. These may change the run state of the asynchronous
2636 * command, wake a task, and/or send a SIGIO signal.
2638 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2640 struct comedi_async *async = s->async;
2641 unsigned runflags = 0;
2642 unsigned runflags_mask = 0;
2644 if (!comedi_is_subdevice_running(s))
2647 if (s->async->events & COMEDI_CB_CANCEL_MASK)
2648 runflags_mask |= COMEDI_SRF_RUNNING;
2651 * Remember if an error event has occurred, so an error
2652 * can be returned the next time the user does a read().
2654 if (s->async->events & COMEDI_CB_ERROR_MASK) {
2655 runflags_mask |= COMEDI_SRF_ERROR;
2656 runflags |= COMEDI_SRF_ERROR;
2658 if (runflags_mask) {
2660 * Sets COMEDI_SRF_ERROR and COMEDI_SRF_RUNNING together
2663 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2666 if (async->cb_mask & s->async->events) {
2667 wake_up_interruptible(&async->wait_head);
2668 if (s->subdev_flags & SDF_CMD_READ)
2669 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2670 if (s->subdev_flags & SDF_CMD_WRITE)
2671 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2673 s->async->events = 0;
2675 EXPORT_SYMBOL_GPL(comedi_event);
2677 /* Note: the ->mutex is pre-locked on successful return */
2678 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2680 struct comedi_device *dev;
2681 struct device *csdev;
2684 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2686 return ERR_PTR(-ENOMEM);
2687 comedi_device_init(dev);
2688 comedi_set_hw_dev(dev, hardware_device);
2689 mutex_lock(&dev->mutex);
2690 mutex_lock(&comedi_board_minor_table_lock);
2691 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2692 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2693 if (comedi_board_minor_table[i] == NULL) {
2694 comedi_board_minor_table[i] = dev;
2698 mutex_unlock(&comedi_board_minor_table_lock);
2699 if (i == COMEDI_NUM_BOARD_MINORS) {
2700 mutex_unlock(&dev->mutex);
2701 comedi_device_cleanup(dev);
2702 comedi_dev_put(dev);
2703 pr_err("ran out of minor numbers for board device files\n");
2704 return ERR_PTR(-EBUSY);
2707 csdev = device_create(comedi_class, hardware_device,
2708 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2710 dev->class_dev = get_device(csdev);
2712 /* Note: dev->mutex needs to be unlocked by the caller. */
2716 static void comedi_free_board_minor(unsigned minor)
2718 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2719 comedi_free_board_dev(comedi_clear_board_minor(minor));
2722 void comedi_release_hardware_device(struct device *hardware_device)
2725 struct comedi_device *dev;
2727 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2729 mutex_lock(&comedi_board_minor_table_lock);
2730 dev = comedi_board_minor_table[minor];
2731 if (dev && dev->hw_dev == hardware_device) {
2732 comedi_board_minor_table[minor] = NULL;
2733 mutex_unlock(&comedi_board_minor_table_lock);
2734 comedi_free_board_dev(dev);
2737 mutex_unlock(&comedi_board_minor_table_lock);
2741 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2743 struct comedi_device *dev = s->device;
2744 struct device *csdev;
2747 mutex_lock(&comedi_subdevice_minor_table_lock);
2748 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2749 if (comedi_subdevice_minor_table[i] == NULL) {
2750 comedi_subdevice_minor_table[i] = s;
2754 mutex_unlock(&comedi_subdevice_minor_table_lock);
2755 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2756 pr_err("ran out of minor numbers for subdevice files\n");
2759 i += COMEDI_NUM_BOARD_MINORS;
2761 csdev = device_create(comedi_class, dev->class_dev,
2762 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2763 dev->minor, s->index);
2765 s->class_dev = csdev;
2770 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2779 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2780 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2782 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2783 mutex_lock(&comedi_subdevice_minor_table_lock);
2784 if (s == comedi_subdevice_minor_table[i])
2785 comedi_subdevice_minor_table[i] = NULL;
2786 mutex_unlock(&comedi_subdevice_minor_table_lock);
2788 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2789 s->class_dev = NULL;
2793 static void comedi_cleanup_board_minors(void)
2797 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2798 comedi_free_board_minor(i);
2801 static int __init comedi_init(void)
2806 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
2808 if (comedi_num_legacy_minors < 0 ||
2809 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2810 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2811 COMEDI_NUM_BOARD_MINORS);
2815 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2816 COMEDI_NUM_MINORS, "comedi");
2819 cdev_init(&comedi_cdev, &comedi_fops);
2820 comedi_cdev.owner = THIS_MODULE;
2822 retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
2824 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2829 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2830 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2834 comedi_class = class_create(THIS_MODULE, "comedi");
2835 if (IS_ERR(comedi_class)) {
2836 pr_err("failed to create class\n");
2837 cdev_del(&comedi_cdev);
2838 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2840 return PTR_ERR(comedi_class);
2843 comedi_class->dev_groups = comedi_dev_groups;
2845 /* XXX requires /proc interface */
2848 /* create devices files for legacy/manual use */
2849 for (i = 0; i < comedi_num_legacy_minors; i++) {
2850 struct comedi_device *dev;
2852 dev = comedi_alloc_board_minor(NULL);
2854 comedi_cleanup_board_minors();
2855 cdev_del(&comedi_cdev);
2856 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2858 return PTR_ERR(dev);
2860 /* comedi_alloc_board_minor() locked the mutex */
2861 mutex_unlock(&dev->mutex);
2866 module_init(comedi_init);
2868 static void __exit comedi_cleanup(void)
2872 comedi_cleanup_board_minors();
2873 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2874 BUG_ON(comedi_board_minor_table[i]);
2875 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2876 BUG_ON(comedi_subdevice_minor_table[i]);
2878 class_destroy(comedi_class);
2879 cdev_del(&comedi_cdev);
2880 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2882 comedi_proc_cleanup();
2884 module_exit(comedi_cleanup);
2886 MODULE_AUTHOR("http://www.comedi.org");
2887 MODULE_DESCRIPTION("Comedi core module");
2888 MODULE_LICENSE("GPL");