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 || (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 || (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 || s->subdev_flags & SDF_CMD_READ)
295 if (!s || 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_clear_subdevice_runflags(struct comedi_subdevice *s,
607 s->runflags &= ~bits;
610 static void __comedi_set_subdevice_runflags(struct comedi_subdevice *s,
616 static void comedi_update_subdevice_runflags(struct comedi_subdevice *s,
617 unsigned mask, unsigned bits)
621 spin_lock_irqsave(&s->spin_lock, flags);
622 __comedi_clear_subdevice_runflags(s, mask);
623 __comedi_set_subdevice_runflags(s, bits & mask);
624 spin_unlock_irqrestore(&s->spin_lock, flags);
627 static unsigned __comedi_get_subdevice_runflags(struct comedi_subdevice *s)
632 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
637 spin_lock_irqsave(&s->spin_lock, flags);
638 runflags = __comedi_get_subdevice_runflags(s);
639 spin_unlock_irqrestore(&s->spin_lock, flags);
643 static bool comedi_is_runflags_running(unsigned runflags)
645 return runflags & COMEDI_SRF_RUNNING;
648 static bool comedi_is_runflags_in_error(unsigned runflags)
650 return runflags & COMEDI_SRF_ERROR;
654 * comedi_is_subdevice_running - check if async command running on subdevice
655 * @s: comedi_subdevice struct
657 * Return true if an asynchronous comedi command is active on the comedi
658 * subdevice, else return false.
660 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
662 unsigned runflags = comedi_get_subdevice_runflags(s);
664 return comedi_is_runflags_running(runflags);
666 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
668 static bool __comedi_is_subdevice_running(struct comedi_subdevice *s)
670 unsigned runflags = __comedi_get_subdevice_runflags(s);
672 return comedi_is_runflags_running(runflags);
675 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
677 unsigned runflags = comedi_get_subdevice_runflags(s);
679 return !(runflags & COMEDI_SRF_BUSY_MASK);
683 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
684 * @s: comedi_subdevice struct
685 * @size: size of the memory to allocate
687 * This also sets the subdevice runflags to allow the core to automatically
688 * free the private data during the detach.
690 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
692 s->private = kzalloc(size, GFP_KERNEL);
694 s->runflags |= COMEDI_SRF_FREE_SPRIV;
697 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
700 * This function restores a subdevice to an idle state.
702 static void do_become_nonbusy(struct comedi_device *dev,
703 struct comedi_subdevice *s)
705 struct comedi_async *async = s->async;
707 comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
710 async->inttrig = NULL;
711 kfree(async->cmd.chanlist);
712 async->cmd.chanlist = NULL;
714 wake_up_interruptible_all(&async->wait_head);
716 dev_err(dev->class_dev,
717 "BUG: (?) do_become_nonbusy called with async=NULL\n");
722 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
726 if (comedi_is_subdevice_running(s) && s->cancel)
727 ret = s->cancel(dev, s);
729 do_become_nonbusy(dev, s);
734 void comedi_device_cancel_all(struct comedi_device *dev)
736 struct comedi_subdevice *s;
742 for (i = 0; i < dev->n_subdevices; i++) {
743 s = &dev->subdevices[i];
749 static int is_device_busy(struct comedi_device *dev)
751 struct comedi_subdevice *s;
757 for (i = 0; i < dev->n_subdevices; i++) {
758 s = &dev->subdevices[i];
761 if (s->async && comedi_buf_is_mmapped(s))
769 * COMEDI_DEVCONFIG ioctl
770 * attaches (and configures) or detaches a legacy device
773 * pointer to comedi_devconfig structure (NULL if detaching)
776 * comedi_devconfig structure (if attaching)
781 static int do_devconfig_ioctl(struct comedi_device *dev,
782 struct comedi_devconfig __user *arg)
784 struct comedi_devconfig it;
786 if (!capable(CAP_SYS_ADMIN))
790 if (is_device_busy(dev))
793 struct module *driver_module = dev->driver->module;
795 comedi_device_detach(dev);
796 module_put(driver_module);
801 if (copy_from_user(&it, arg, sizeof(it)))
804 it.board_name[COMEDI_NAMELEN - 1] = 0;
806 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
807 dev_warn(dev->class_dev,
808 "comedi_config --init_data is deprecated\n");
812 if (dev->minor >= comedi_num_legacy_minors)
813 /* don't re-use dynamically allocated comedi devices */
816 /* This increments the driver module count on success. */
817 return comedi_device_attach(dev, &it);
821 * COMEDI_BUFCONFIG ioctl
822 * buffer configuration
825 * pointer to comedi_bufconfig structure
828 * comedi_bufconfig structure
831 * modified comedi_bufconfig structure
833 static int do_bufconfig_ioctl(struct comedi_device *dev,
834 struct comedi_bufconfig __user *arg)
836 struct comedi_bufconfig bc;
837 struct comedi_async *async;
838 struct comedi_subdevice *s;
841 if (copy_from_user(&bc, arg, sizeof(bc)))
844 if (bc.subdevice >= dev->n_subdevices)
847 s = &dev->subdevices[bc.subdevice];
851 dev_dbg(dev->class_dev,
852 "subdevice does not have async capability\n");
858 if (bc.maximum_size) {
859 if (!capable(CAP_SYS_ADMIN))
862 async->max_bufsize = bc.maximum_size;
866 retval = resize_async_buffer(dev, s, bc.size);
871 bc.size = async->prealloc_bufsz;
872 bc.maximum_size = async->max_bufsize;
875 if (copy_to_user(arg, &bc, sizeof(bc)))
882 * COMEDI_DEVINFO ioctl
886 * pointer to comedi_devinfo structure
892 * comedi_devinfo structure
894 static int do_devinfo_ioctl(struct comedi_device *dev,
895 struct comedi_devinfo __user *arg,
898 struct comedi_subdevice *s;
899 struct comedi_devinfo devinfo;
901 memset(&devinfo, 0, sizeof(devinfo));
903 /* fill devinfo structure */
904 devinfo.version_code = COMEDI_VERSION_CODE;
905 devinfo.n_subdevs = dev->n_subdevices;
906 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
907 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
909 s = comedi_file_read_subdevice(file);
911 devinfo.read_subdevice = s->index;
913 devinfo.read_subdevice = -1;
915 s = comedi_file_write_subdevice(file);
917 devinfo.write_subdevice = s->index;
919 devinfo.write_subdevice = -1;
921 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
928 * COMEDI_SUBDINFO ioctl
932 * pointer to array of comedi_subdinfo structures
938 * array of comedi_subdinfo structures
940 static int do_subdinfo_ioctl(struct comedi_device *dev,
941 struct comedi_subdinfo __user *arg, void *file)
944 struct comedi_subdinfo *tmp, *us;
945 struct comedi_subdevice *s;
947 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
951 /* fill subdinfo structs */
952 for (i = 0; i < dev->n_subdevices; i++) {
953 s = &dev->subdevices[i];
957 us->n_chan = s->n_chan;
958 us->subd_flags = s->subdev_flags;
959 if (comedi_is_subdevice_running(s))
960 us->subd_flags |= SDF_RUNNING;
961 #define TIMER_nanosec 5 /* backwards compatibility */
962 us->timer_type = TIMER_nanosec;
963 us->len_chanlist = s->len_chanlist;
964 us->maxdata = s->maxdata;
965 if (s->range_table) {
967 (i << 24) | (0 << 16) | (s->range_table->length);
969 us->range_type = 0; /* XXX */
973 us->subd_flags |= SDF_BUSY;
975 us->subd_flags |= SDF_BUSY_OWNER;
977 us->subd_flags |= SDF_LOCKED;
979 us->subd_flags |= SDF_LOCK_OWNER;
980 if (!s->maxdata && s->maxdata_list)
981 us->subd_flags |= SDF_MAXDATA;
982 if (s->range_table_list)
983 us->subd_flags |= SDF_RANGETYPE;
985 us->subd_flags |= SDF_CMD;
987 if (s->insn_bits != &insn_inval)
988 us->insn_bits_support = COMEDI_SUPPORTED;
990 us->insn_bits_support = COMEDI_UNSUPPORTED;
993 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
997 return ret ? -EFAULT : 0;
1001 * COMEDI_CHANINFO ioctl
1002 * subdevice channel info
1005 * pointer to comedi_chaninfo structure
1008 * comedi_chaninfo structure
1011 * array of maxdata values to chaninfo->maxdata_list if requested
1012 * array of range table lengths to chaninfo->range_table_list if requested
1014 static int do_chaninfo_ioctl(struct comedi_device *dev,
1015 struct comedi_chaninfo __user *arg)
1017 struct comedi_subdevice *s;
1018 struct comedi_chaninfo it;
1020 if (copy_from_user(&it, arg, sizeof(it)))
1023 if (it.subdev >= dev->n_subdevices)
1025 s = &dev->subdevices[it.subdev];
1027 if (it.maxdata_list) {
1028 if (s->maxdata || !s->maxdata_list)
1030 if (copy_to_user(it.maxdata_list, s->maxdata_list,
1031 s->n_chan * sizeof(unsigned int)))
1036 return -EINVAL; /* flaglist not supported */
1041 if (!s->range_table_list)
1043 for (i = 0; i < s->n_chan; i++) {
1046 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
1047 (s->range_table_list[i]->length);
1048 if (put_user(x, it.rangelist + i))
1052 if (copy_to_user(it.rangelist, s->range_type_list,
1053 s->n_chan * sizeof(unsigned int)))
1062 * COMEDI_BUFINFO ioctl
1063 * buffer information
1066 * pointer to comedi_bufinfo structure
1069 * comedi_bufinfo structure
1072 * modified comedi_bufinfo structure
1074 static int do_bufinfo_ioctl(struct comedi_device *dev,
1075 struct comedi_bufinfo __user *arg, void *file)
1077 struct comedi_bufinfo bi;
1078 struct comedi_subdevice *s;
1079 struct comedi_async *async;
1081 if (copy_from_user(&bi, arg, sizeof(bi)))
1084 if (bi.subdevice >= dev->n_subdevices)
1087 s = &dev->subdevices[bi.subdevice];
1092 dev_dbg(dev->class_dev,
1093 "subdevice does not have async capability\n");
1094 bi.buf_write_ptr = 0;
1095 bi.buf_read_ptr = 0;
1096 bi.buf_write_count = 0;
1097 bi.buf_read_count = 0;
1099 bi.bytes_written = 0;
1104 bi.bytes_written = 0;
1105 goto copyback_position;
1107 if (s->busy != file)
1110 if (bi.bytes_read && !(async->cmd.flags & CMDF_WRITE)) {
1111 bi.bytes_read = comedi_buf_read_alloc(s, bi.bytes_read);
1112 comedi_buf_read_free(s, bi.bytes_read);
1114 if (comedi_is_subdevice_idle(s) &&
1115 comedi_buf_n_bytes_ready(s) == 0) {
1116 do_become_nonbusy(dev, s);
1120 if (bi.bytes_written && (async->cmd.flags & CMDF_WRITE)) {
1122 comedi_buf_write_alloc(s, bi.bytes_written);
1123 comedi_buf_write_free(s, bi.bytes_written);
1127 bi.buf_write_count = async->buf_write_count;
1128 bi.buf_write_ptr = async->buf_write_ptr;
1129 bi.buf_read_count = async->buf_read_count;
1130 bi.buf_read_ptr = async->buf_read_ptr;
1133 if (copy_to_user(arg, &bi, sizeof(bi)))
1139 static int check_insn_config_length(struct comedi_insn *insn,
1146 case INSN_CONFIG_DIO_OUTPUT:
1147 case INSN_CONFIG_DIO_INPUT:
1148 case INSN_CONFIG_DISARM:
1149 case INSN_CONFIG_RESET:
1153 case INSN_CONFIG_ARM:
1154 case INSN_CONFIG_DIO_QUERY:
1155 case INSN_CONFIG_BLOCK_SIZE:
1156 case INSN_CONFIG_FILTER:
1157 case INSN_CONFIG_SERIAL_CLOCK:
1158 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1159 case INSN_CONFIG_ALT_SOURCE:
1160 case INSN_CONFIG_SET_COUNTER_MODE:
1161 case INSN_CONFIG_8254_READ_STATUS:
1162 case INSN_CONFIG_SET_ROUTING:
1163 case INSN_CONFIG_GET_ROUTING:
1164 case INSN_CONFIG_GET_PWM_STATUS:
1165 case INSN_CONFIG_PWM_SET_PERIOD:
1166 case INSN_CONFIG_PWM_GET_PERIOD:
1170 case INSN_CONFIG_SET_GATE_SRC:
1171 case INSN_CONFIG_GET_GATE_SRC:
1172 case INSN_CONFIG_SET_CLOCK_SRC:
1173 case INSN_CONFIG_GET_CLOCK_SRC:
1174 case INSN_CONFIG_SET_OTHER_SRC:
1175 case INSN_CONFIG_GET_COUNTER_STATUS:
1176 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1177 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1178 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1182 case INSN_CONFIG_PWM_OUTPUT:
1183 case INSN_CONFIG_ANALOG_TRIG:
1187 case INSN_CONFIG_DIGITAL_TRIG:
1192 * by default we allow the insn since we don't have checks for
1193 * all possible cases yet
1196 pr_warn("No check for data length of config insn id %i is implemented\n",
1198 pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1199 pr_warn("Assuming n=%i is correct\n", insn->n);
1205 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1206 unsigned int *data, void *file)
1208 struct comedi_subdevice *s;
1212 if (insn->insn & INSN_MASK_SPECIAL) {
1213 /* a non-subdevice instruction */
1215 switch (insn->insn) {
1225 do_gettimeofday(&tv);
1226 data[0] = tv.tv_sec;
1227 data[1] = tv.tv_usec;
1233 if (insn->n != 1 || data[0] >= 100000) {
1237 udelay(data[0] / 1000);
1245 if (insn->subdev >= dev->n_subdevices) {
1246 dev_dbg(dev->class_dev,
1247 "%d not usable subdevice\n",
1252 s = &dev->subdevices[insn->subdev];
1254 dev_dbg(dev->class_dev, "no async\n");
1258 if (!s->async->inttrig) {
1259 dev_dbg(dev->class_dev, "no inttrig\n");
1263 ret = s->async->inttrig(dev, s, data[0]);
1268 dev_dbg(dev->class_dev, "invalid insn\n");
1273 /* a subdevice instruction */
1274 unsigned int maxdata;
1276 if (insn->subdev >= dev->n_subdevices) {
1277 dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1282 s = &dev->subdevices[insn->subdev];
1284 if (s->type == COMEDI_SUBD_UNUSED) {
1285 dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1291 /* are we locked? (ioctl lock) */
1292 if (s->lock && s->lock != file) {
1293 dev_dbg(dev->class_dev, "device locked\n");
1298 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1301 dev_dbg(dev->class_dev, "bad chanspec\n");
1309 /* This looks arbitrary. It is. */
1310 s->busy = &parse_insn;
1311 switch (insn->insn) {
1313 ret = s->insn_read(dev, s, insn, data);
1314 if (ret == -ETIMEDOUT) {
1315 dev_dbg(dev->class_dev,
1316 "subdevice %d read instruction timed out\n",
1321 maxdata = s->maxdata_list
1322 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1324 for (i = 0; i < insn->n; ++i) {
1325 if (data[i] > maxdata) {
1327 dev_dbg(dev->class_dev,
1328 "bad data value(s)\n");
1333 ret = s->insn_write(dev, s, insn, data);
1334 if (ret == -ETIMEDOUT) {
1335 dev_dbg(dev->class_dev,
1336 "subdevice %d write instruction timed out\n",
1346 * Most drivers ignore the base channel in
1347 * insn->chanspec. Fix this here if
1348 * the subdevice has <= 32 channels.
1350 unsigned int orig_mask = data[0];
1351 unsigned int shift = 0;
1353 if (s->n_chan <= 32) {
1354 shift = CR_CHAN(insn->chanspec);
1361 ret = s->insn_bits(dev, s, insn, data);
1362 data[0] = orig_mask;
1368 ret = check_insn_config_length(insn, data);
1371 ret = s->insn_config(dev, s, insn, data);
1386 * COMEDI_INSNLIST ioctl
1387 * synchronous instruction list
1390 * pointer to comedi_insnlist structure
1393 * comedi_insnlist structure
1394 * array of comedi_insn structures from insnlist->insns pointer
1395 * data (for writes) from insns[].data pointers
1398 * data (for reads) to insns[].data pointers
1400 /* arbitrary limits */
1401 #define MAX_SAMPLES 256
1402 static int do_insnlist_ioctl(struct comedi_device *dev,
1403 struct comedi_insnlist __user *arg, void *file)
1405 struct comedi_insnlist insnlist;
1406 struct comedi_insn *insns = NULL;
1407 unsigned int *data = NULL;
1411 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1414 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1420 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1426 if (copy_from_user(insns, insnlist.insns,
1427 sizeof(*insns) * insnlist.n_insns)) {
1428 dev_dbg(dev->class_dev, "copy_from_user failed\n");
1433 for (i = 0; i < insnlist.n_insns; i++) {
1434 if (insns[i].n > MAX_SAMPLES) {
1435 dev_dbg(dev->class_dev,
1436 "number of samples too large\n");
1440 if (insns[i].insn & INSN_MASK_WRITE) {
1441 if (copy_from_user(data, insns[i].data,
1442 insns[i].n * sizeof(unsigned int))) {
1443 dev_dbg(dev->class_dev,
1444 "copy_from_user failed\n");
1449 ret = parse_insn(dev, insns + i, data, file);
1452 if (insns[i].insn & INSN_MASK_READ) {
1453 if (copy_to_user(insns[i].data, data,
1454 insns[i].n * sizeof(unsigned int))) {
1455 dev_dbg(dev->class_dev,
1456 "copy_to_user failed\n");
1476 * synchronous instruction
1479 * pointer to comedi_insn structure
1482 * comedi_insn structure
1483 * data (for writes) from insn->data pointer
1486 * data (for reads) to insn->data pointer
1488 static int do_insn_ioctl(struct comedi_device *dev,
1489 struct comedi_insn __user *arg, void *file)
1491 struct comedi_insn insn;
1492 unsigned int *data = NULL;
1495 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1501 if (copy_from_user(&insn, arg, sizeof(insn))) {
1506 /* This is where the behavior of insn and insnlist deviate. */
1507 if (insn.n > MAX_SAMPLES)
1508 insn.n = MAX_SAMPLES;
1509 if (insn.insn & INSN_MASK_WRITE) {
1510 if (copy_from_user(data,
1512 insn.n * sizeof(unsigned int))) {
1517 ret = parse_insn(dev, &insn, data, file);
1520 if (insn.insn & INSN_MASK_READ) {
1521 if (copy_to_user(insn.data,
1523 insn.n * sizeof(unsigned int))) {
1536 static int __comedi_get_user_cmd(struct comedi_device *dev,
1537 struct comedi_cmd __user *arg,
1538 struct comedi_cmd *cmd)
1540 struct comedi_subdevice *s;
1542 if (copy_from_user(cmd, arg, sizeof(*cmd))) {
1543 dev_dbg(dev->class_dev, "bad cmd address\n");
1547 if (cmd->subdev >= dev->n_subdevices) {
1548 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
1552 s = &dev->subdevices[cmd->subdev];
1554 if (s->type == COMEDI_SUBD_UNUSED) {
1555 dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1560 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1561 dev_dbg(dev->class_dev,
1562 "subdevice %d does not support commands\n",
1567 /* make sure channel/gain list isn't too long */
1568 if (cmd->chanlist_len > s->len_chanlist) {
1569 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1570 cmd->chanlist_len, s->len_chanlist);
1575 * Set the CMDF_WRITE flag to the correct state if the subdevice
1576 * supports only "read" commands or only "write" commands.
1578 switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
1580 cmd->flags &= ~CMDF_WRITE;
1583 cmd->flags |= CMDF_WRITE;
1592 static int __comedi_get_user_chanlist(struct comedi_device *dev,
1593 struct comedi_subdevice *s,
1594 unsigned int __user *user_chanlist,
1595 struct comedi_cmd *cmd)
1597 unsigned int *chanlist;
1600 cmd->chanlist = NULL;
1601 chanlist = memdup_user(user_chanlist,
1602 cmd->chanlist_len * sizeof(unsigned int));
1603 if (IS_ERR(chanlist))
1604 return PTR_ERR(chanlist);
1606 /* make sure each element in channel/gain list is valid */
1607 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1613 cmd->chanlist = chanlist;
1620 * asynchronous acquisition command set-up
1623 * pointer to comedi_cmd structure
1626 * comedi_cmd structure
1627 * channel/range list from cmd->chanlist pointer
1630 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1632 static int do_cmd_ioctl(struct comedi_device *dev,
1633 struct comedi_cmd __user *arg, void *file)
1635 struct comedi_cmd cmd;
1636 struct comedi_subdevice *s;
1637 struct comedi_async *async;
1638 unsigned int __user *user_chanlist;
1641 /* get the user's cmd and do some simple validation */
1642 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1646 /* save user's chanlist pointer so it can be restored later */
1647 user_chanlist = (unsigned int __user *)cmd.chanlist;
1649 s = &dev->subdevices[cmd.subdev];
1652 /* are we locked? (ioctl lock) */
1653 if (s->lock && s->lock != file) {
1654 dev_dbg(dev->class_dev, "subdevice locked\n");
1660 dev_dbg(dev->class_dev, "subdevice busy\n");
1664 /* make sure channel/gain list isn't too short */
1665 if (cmd.chanlist_len < 1) {
1666 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
1672 async->cmd.data = NULL;
1674 /* load channel/gain list */
1675 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1679 ret = s->do_cmdtest(dev, s, &async->cmd);
1681 if (async->cmd.flags & CMDF_BOGUS || ret) {
1682 dev_dbg(dev->class_dev, "test returned %d\n", ret);
1684 /* restore chanlist pointer before copying back */
1685 cmd.chanlist = (unsigned int __force *)user_chanlist;
1687 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1688 dev_dbg(dev->class_dev, "fault writing cmd\n");
1696 if (!async->prealloc_bufsz) {
1698 dev_dbg(dev->class_dev, "no buffer (?)\n");
1702 comedi_buf_reset(s);
1704 async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
1705 if (async->cmd.flags & CMDF_WAKE_EOS)
1706 async->cb_mask |= COMEDI_CB_EOS;
1708 comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
1709 COMEDI_SRF_RUNNING);
1712 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1713 * race with comedi_read() or comedi_write().
1716 ret = s->do_cmd(dev, s);
1721 do_become_nonbusy(dev, s);
1727 * COMEDI_CMDTEST ioctl
1728 * asynchronous aquisition command testing
1731 * pointer to comedi_cmd structure
1734 * comedi_cmd structure
1735 * channel/range list from cmd->chanlist pointer
1738 * possibly modified comedi_cmd structure
1740 static int do_cmdtest_ioctl(struct comedi_device *dev,
1741 struct comedi_cmd __user *arg, void *file)
1743 struct comedi_cmd cmd;
1744 struct comedi_subdevice *s;
1745 unsigned int __user *user_chanlist;
1748 /* get the user's cmd and do some simple validation */
1749 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1753 /* save user's chanlist pointer so it can be restored later */
1754 user_chanlist = (unsigned int __user *)cmd.chanlist;
1756 s = &dev->subdevices[cmd.subdev];
1758 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1759 if (user_chanlist) {
1760 /* load channel/gain list */
1761 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1766 ret = s->do_cmdtest(dev, s, &cmd);
1768 kfree(cmd.chanlist); /* free kernel copy of user chanlist */
1770 /* restore chanlist pointer before copying back */
1771 cmd.chanlist = (unsigned int __force *)user_chanlist;
1773 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1774 dev_dbg(dev->class_dev, "bad cmd address\n");
1794 static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
1798 unsigned long flags;
1799 struct comedi_subdevice *s;
1801 if (arg >= dev->n_subdevices)
1803 s = &dev->subdevices[arg];
1805 spin_lock_irqsave(&s->spin_lock, flags);
1806 if (s->busy || s->lock)
1810 spin_unlock_irqrestore(&s->spin_lock, flags);
1816 * COMEDI_UNLOCK ioctl
1828 static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
1831 struct comedi_subdevice *s;
1833 if (arg >= dev->n_subdevices)
1835 s = &dev->subdevices[arg];
1840 if (s->lock && s->lock != file)
1843 if (s->lock == file)
1850 * COMEDI_CANCEL ioctl
1851 * cancel asynchronous acquisition
1862 static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
1865 struct comedi_subdevice *s;
1867 if (arg >= dev->n_subdevices)
1869 s = &dev->subdevices[arg];
1876 if (s->busy != file)
1879 return do_cancel(dev, s);
1884 * instructs driver to synchronize buffers
1895 static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
1898 struct comedi_subdevice *s;
1900 if (arg >= dev->n_subdevices)
1902 s = &dev->subdevices[arg];
1907 if (s->busy != file)
1911 return s->poll(dev, s);
1917 * COMEDI_SETRSUBD ioctl
1918 * sets the current "read" subdevice on a per-file basis
1929 static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1932 struct comedi_file *cfp = file->private_data;
1933 struct comedi_subdevice *s_old, *s_new;
1935 if (arg >= dev->n_subdevices)
1938 s_new = &dev->subdevices[arg];
1939 s_old = comedi_file_read_subdevice(file);
1941 return 0; /* no change */
1943 if (!(s_new->subdev_flags & SDF_CMD_READ))
1947 * Check the file isn't still busy handling a "read" command on the
1948 * old subdevice (if any).
1950 if (s_old && s_old->busy == file && s_old->async &&
1951 !(s_old->async->cmd.flags & CMDF_WRITE))
1954 ACCESS_ONCE(cfp->read_subdev) = s_new;
1959 * COMEDI_SETWSUBD ioctl
1960 * sets the current "write" subdevice on a per-file basis
1971 static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1974 struct comedi_file *cfp = file->private_data;
1975 struct comedi_subdevice *s_old, *s_new;
1977 if (arg >= dev->n_subdevices)
1980 s_new = &dev->subdevices[arg];
1981 s_old = comedi_file_write_subdevice(file);
1983 return 0; /* no change */
1985 if (!(s_new->subdev_flags & SDF_CMD_WRITE))
1989 * Check the file isn't still busy handling a "write" command on the
1990 * old subdevice (if any).
1992 if (s_old && s_old->busy == file && s_old->async &&
1993 (s_old->async->cmd.flags & CMDF_WRITE))
1996 ACCESS_ONCE(cfp->write_subdev) = s_new;
2000 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
2003 unsigned minor = iminor(file_inode(file));
2004 struct comedi_file *cfp = file->private_data;
2005 struct comedi_device *dev = cfp->dev;
2008 mutex_lock(&dev->mutex);
2011 * Device config is special, because it must work on
2012 * an unconfigured device.
2014 if (cmd == COMEDI_DEVCONFIG) {
2015 if (minor >= COMEDI_NUM_BOARD_MINORS) {
2016 /* Device config not appropriate on non-board minors. */
2020 rc = do_devconfig_ioctl(dev,
2021 (struct comedi_devconfig __user *)arg);
2024 dev->minor >= comedi_num_legacy_minors) {
2026 * Successfully unconfigured a dynamically
2027 * allocated device. Try and remove it.
2029 if (comedi_clear_board_dev(dev)) {
2030 mutex_unlock(&dev->mutex);
2031 comedi_free_board_dev(dev);
2039 if (!dev->attached) {
2040 dev_dbg(dev->class_dev, "no driver attached\n");
2046 case COMEDI_BUFCONFIG:
2047 rc = do_bufconfig_ioctl(dev,
2048 (struct comedi_bufconfig __user *)arg);
2050 case COMEDI_DEVINFO:
2051 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
2054 case COMEDI_SUBDINFO:
2055 rc = do_subdinfo_ioctl(dev,
2056 (struct comedi_subdinfo __user *)arg,
2059 case COMEDI_CHANINFO:
2060 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
2062 case COMEDI_RANGEINFO:
2063 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
2065 case COMEDI_BUFINFO:
2066 rc = do_bufinfo_ioctl(dev,
2067 (struct comedi_bufinfo __user *)arg,
2071 rc = do_lock_ioctl(dev, arg, file);
2074 rc = do_unlock_ioctl(dev, arg, file);
2077 rc = do_cancel_ioctl(dev, arg, file);
2080 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
2082 case COMEDI_CMDTEST:
2083 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
2086 case COMEDI_INSNLIST:
2087 rc = do_insnlist_ioctl(dev,
2088 (struct comedi_insnlist __user *)arg,
2092 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
2096 rc = do_poll_ioctl(dev, arg, file);
2098 case COMEDI_SETRSUBD:
2099 rc = do_setrsubd_ioctl(dev, arg, file);
2101 case COMEDI_SETWSUBD:
2102 rc = do_setwsubd_ioctl(dev, arg, file);
2110 mutex_unlock(&dev->mutex);
2114 static void comedi_vm_open(struct vm_area_struct *area)
2116 struct comedi_buf_map *bm;
2118 bm = area->vm_private_data;
2119 comedi_buf_map_get(bm);
2122 static void comedi_vm_close(struct vm_area_struct *area)
2124 struct comedi_buf_map *bm;
2126 bm = area->vm_private_data;
2127 comedi_buf_map_put(bm);
2130 static struct vm_operations_struct comedi_vm_ops = {
2131 .open = comedi_vm_open,
2132 .close = comedi_vm_close,
2135 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2137 struct comedi_file *cfp = file->private_data;
2138 struct comedi_device *dev = cfp->dev;
2139 struct comedi_subdevice *s;
2140 struct comedi_async *async;
2141 struct comedi_buf_map *bm = NULL;
2142 unsigned long start = vma->vm_start;
2149 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2150 * and down-reading &dev->attach_lock should normally succeed without
2151 * contention unless the device is in the process of being attached
2154 if (!down_read_trylock(&dev->attach_lock))
2157 if (!dev->attached) {
2158 dev_dbg(dev->class_dev, "no driver attached\n");
2163 if (vma->vm_flags & VM_WRITE)
2164 s = comedi_file_write_subdevice(file);
2166 s = comedi_file_read_subdevice(file);
2178 if (vma->vm_pgoff != 0) {
2179 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
2184 size = vma->vm_end - vma->vm_start;
2185 if (size > async->prealloc_bufsz) {
2189 if (size & (~PAGE_MASK)) {
2194 n_pages = size >> PAGE_SHIFT;
2196 /* get reference to current buf map (if any) */
2197 bm = comedi_buf_map_from_subdev_get(s);
2198 if (!bm || n_pages > bm->n_pages) {
2202 for (i = 0; i < n_pages; ++i) {
2203 struct comedi_buf_page *buf = &bm->page_list[i];
2205 if (remap_pfn_range(vma, start,
2206 page_to_pfn(virt_to_page(buf->virt_addr)),
2207 PAGE_SIZE, PAGE_SHARED)) {
2214 vma->vm_ops = &comedi_vm_ops;
2215 vma->vm_private_data = bm;
2217 vma->vm_ops->open(vma);
2221 up_read(&dev->attach_lock);
2222 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
2226 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2228 unsigned int mask = 0;
2229 struct comedi_file *cfp = file->private_data;
2230 struct comedi_device *dev = cfp->dev;
2231 struct comedi_subdevice *s;
2233 mutex_lock(&dev->mutex);
2235 if (!dev->attached) {
2236 dev_dbg(dev->class_dev, "no driver attached\n");
2240 s = comedi_file_read_subdevice(file);
2241 if (s && s->async) {
2242 poll_wait(file, &s->async->wait_head, wait);
2243 if (!s->busy || !comedi_is_subdevice_running(s) ||
2244 (s->async->cmd.flags & CMDF_WRITE) ||
2245 comedi_buf_read_n_available(s) > 0)
2246 mask |= POLLIN | POLLRDNORM;
2249 s = comedi_file_write_subdevice(file);
2250 if (s && s->async) {
2251 unsigned int bps = comedi_bytes_per_sample(s);
2253 poll_wait(file, &s->async->wait_head, wait);
2254 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
2255 if (!s->busy || !comedi_is_subdevice_running(s) ||
2256 !(s->async->cmd.flags & CMDF_WRITE) ||
2257 comedi_buf_write_n_allocated(s) >= bps)
2258 mask |= POLLOUT | POLLWRNORM;
2262 mutex_unlock(&dev->mutex);
2266 static ssize_t comedi_write(struct file *file, const char __user *buf,
2267 size_t nbytes, loff_t *offset)
2269 struct comedi_subdevice *s;
2270 struct comedi_async *async;
2271 int n, m, count = 0, retval = 0;
2272 DECLARE_WAITQUEUE(wait, current);
2273 struct comedi_file *cfp = file->private_data;
2274 struct comedi_device *dev = cfp->dev;
2275 bool on_wait_queue = false;
2277 unsigned int old_detach_count;
2279 /* Protect against device detachment during operation. */
2280 down_read(&dev->attach_lock);
2281 attach_locked = true;
2282 old_detach_count = dev->detach_count;
2284 if (!dev->attached) {
2285 dev_dbg(dev->class_dev, "no driver attached\n");
2290 s = comedi_file_write_subdevice(file);
2291 if (!s || !s->async) {
2298 if (!s->busy || !nbytes)
2300 if (s->busy != file) {
2304 if (!(async->cmd.flags & CMDF_WRITE)) {
2309 add_wait_queue(&async->wait_head, &wait);
2310 on_wait_queue = true;
2311 while (nbytes > 0 && !retval) {
2314 set_current_state(TASK_INTERRUPTIBLE);
2316 runflags = comedi_get_subdevice_runflags(s);
2317 if (!comedi_is_runflags_running(runflags)) {
2319 struct comedi_subdevice *new_s;
2321 if (comedi_is_runflags_in_error(runflags))
2326 * To avoid deadlock, cannot acquire dev->mutex
2327 * while dev->attach_lock is held. Need to
2328 * remove task from the async wait queue before
2329 * releasing dev->attach_lock, as it might not
2330 * be valid afterwards.
2332 remove_wait_queue(&async->wait_head, &wait);
2333 on_wait_queue = false;
2334 up_read(&dev->attach_lock);
2335 attach_locked = false;
2336 mutex_lock(&dev->mutex);
2338 * Become non-busy unless things have changed
2339 * behind our back. Checking dev->detach_count
2340 * is unchanged ought to be sufficient (unless
2341 * there have been 2**32 detaches in the
2342 * meantime!), but check the subdevice pointer
2343 * as well just in case.
2345 new_s = comedi_file_write_subdevice(file);
2346 if (dev->attached &&
2347 old_detach_count == dev->detach_count &&
2348 s == new_s && new_s->async == async)
2349 do_become_nonbusy(dev, s);
2350 mutex_unlock(&dev->mutex);
2358 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2359 m = async->prealloc_bufsz - async->buf_write_ptr;
2360 comedi_buf_write_alloc(s, async->prealloc_bufsz);
2361 if (m > comedi_buf_write_n_allocated(s))
2362 m = comedi_buf_write_n_allocated(s);
2367 if (file->f_flags & O_NONBLOCK) {
2372 if (signal_pending(current)) {
2373 retval = -ERESTARTSYS;
2378 if (s->busy != file) {
2382 if (!(async->cmd.flags & CMDF_WRITE)) {
2389 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2395 comedi_buf_write_free(s, n);
2401 break; /* makes device work like a pipe */
2405 remove_wait_queue(&async->wait_head, &wait);
2406 set_current_state(TASK_RUNNING);
2408 up_read(&dev->attach_lock);
2410 return count ? count : retval;
2413 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2416 struct comedi_subdevice *s;
2417 struct comedi_async *async;
2418 int n, m, count = 0, retval = 0;
2419 DECLARE_WAITQUEUE(wait, current);
2420 struct comedi_file *cfp = file->private_data;
2421 struct comedi_device *dev = cfp->dev;
2422 unsigned int old_detach_count;
2423 bool become_nonbusy = false;
2426 /* Protect against device detachment during operation. */
2427 down_read(&dev->attach_lock);
2428 attach_locked = true;
2429 old_detach_count = dev->detach_count;
2431 if (!dev->attached) {
2432 dev_dbg(dev->class_dev, "no driver attached\n");
2437 s = comedi_file_read_subdevice(file);
2438 if (!s || !s->async) {
2444 if (!s->busy || !nbytes)
2446 if (s->busy != file) {
2450 if (async->cmd.flags & CMDF_WRITE) {
2455 add_wait_queue(&async->wait_head, &wait);
2456 while (nbytes > 0 && !retval) {
2457 set_current_state(TASK_INTERRUPTIBLE);
2461 m = comedi_buf_read_n_available(s);
2462 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2463 m = async->prealloc_bufsz - async->buf_read_ptr;
2468 unsigned runflags = comedi_get_subdevice_runflags(s);
2470 if (!comedi_is_runflags_running(runflags)) {
2471 if (comedi_is_runflags_in_error(runflags))
2475 become_nonbusy = true;
2478 if (file->f_flags & O_NONBLOCK) {
2483 if (signal_pending(current)) {
2484 retval = -ERESTARTSYS;
2491 if (s->busy != file) {
2495 if (async->cmd.flags & CMDF_WRITE) {
2501 m = copy_to_user(buf, async->prealloc_buf +
2502 async->buf_read_ptr, n);
2508 comedi_buf_read_alloc(s, n);
2509 comedi_buf_read_free(s, n);
2515 break; /* makes device work like a pipe */
2517 remove_wait_queue(&async->wait_head, &wait);
2518 set_current_state(TASK_RUNNING);
2519 if (become_nonbusy || comedi_is_subdevice_idle(s)) {
2520 struct comedi_subdevice *new_s;
2523 * To avoid deadlock, cannot acquire dev->mutex
2524 * while dev->attach_lock is held.
2526 up_read(&dev->attach_lock);
2527 attach_locked = false;
2528 mutex_lock(&dev->mutex);
2530 * Check device hasn't become detached behind our back.
2531 * Checking dev->detach_count is unchanged ought to be
2532 * sufficient (unless there have been 2**32 detaches in the
2533 * meantime!), but check the subdevice pointer as well just in
2536 new_s = comedi_file_read_subdevice(file);
2537 if (dev->attached && old_detach_count == dev->detach_count &&
2538 s == new_s && new_s->async == async) {
2539 if (become_nonbusy || comedi_buf_n_bytes_ready(s) == 0)
2540 do_become_nonbusy(dev, s);
2542 mutex_unlock(&dev->mutex);
2546 up_read(&dev->attach_lock);
2548 return count ? count : retval;
2551 static int comedi_open(struct inode *inode, struct file *file)
2553 const unsigned minor = iminor(inode);
2554 struct comedi_file *cfp;
2555 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2559 pr_debug("invalid minor number\n");
2563 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2569 mutex_lock(&dev->mutex);
2570 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2571 dev_dbg(dev->class_dev, "not attached and not CAP_NET_ADMIN\n");
2575 if (dev->attached && dev->use_count == 0) {
2576 if (!try_module_get(dev->driver->module)) {
2581 rc = dev->open(dev);
2583 module_put(dev->driver->module);
2590 file->private_data = cfp;
2591 comedi_file_reset(file);
2595 mutex_unlock(&dev->mutex);
2597 comedi_dev_put(dev);
2603 static int comedi_fasync(int fd, struct file *file, int on)
2605 struct comedi_file *cfp = file->private_data;
2606 struct comedi_device *dev = cfp->dev;
2608 return fasync_helper(fd, file, on, &dev->async_queue);
2611 static int comedi_close(struct inode *inode, struct file *file)
2613 struct comedi_file *cfp = file->private_data;
2614 struct comedi_device *dev = cfp->dev;
2615 struct comedi_subdevice *s = NULL;
2618 mutex_lock(&dev->mutex);
2620 if (dev->subdevices) {
2621 for (i = 0; i < dev->n_subdevices; i++) {
2622 s = &dev->subdevices[i];
2624 if (s->busy == file)
2626 if (s->lock == file)
2630 if (dev->attached && dev->use_count == 1) {
2633 module_put(dev->driver->module);
2638 mutex_unlock(&dev->mutex);
2639 comedi_dev_put(dev);
2645 static const struct file_operations comedi_fops = {
2646 .owner = THIS_MODULE,
2647 .unlocked_ioctl = comedi_unlocked_ioctl,
2648 .compat_ioctl = comedi_compat_ioctl,
2649 .open = comedi_open,
2650 .release = comedi_close,
2651 .read = comedi_read,
2652 .write = comedi_write,
2653 .mmap = comedi_mmap,
2654 .poll = comedi_poll,
2655 .fasync = comedi_fasync,
2656 .llseek = noop_llseek,
2660 * comedi_event - handle events for asynchronous comedi command
2661 * @dev: comedi_device struct
2662 * @s: comedi_subdevice struct associated with dev
2663 * Context: interrupt (usually), s->spin_lock spin-lock not held
2665 * If an asynchronous comedi command is active on the subdevice, process
2666 * any COMEDI_CB_... event flags that have been set, usually by an
2667 * interrupt handler. These may change the run state of the asynchronous
2668 * command, wake a task, and/or send a SIGIO signal.
2670 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2672 struct comedi_async *async = s->async;
2673 unsigned int events;
2675 unsigned long flags;
2677 spin_lock_irqsave(&s->spin_lock, flags);
2679 events = async->events;
2681 if (!__comedi_is_subdevice_running(s)) {
2682 spin_unlock_irqrestore(&s->spin_lock, flags);
2686 if (events & COMEDI_CB_CANCEL_MASK)
2687 __comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
2690 * Remember if an error event has occurred, so an error can be
2691 * returned the next time the user does a read() or write().
2693 if (events & COMEDI_CB_ERROR_MASK)
2694 __comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
2696 if (async->cb_mask & events) {
2697 wake_up_interruptible(&async->wait_head);
2698 si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
2701 spin_unlock_irqrestore(&s->spin_lock, flags);
2704 kill_fasync(&dev->async_queue, SIGIO, si_code);
2706 EXPORT_SYMBOL_GPL(comedi_event);
2708 /* Note: the ->mutex is pre-locked on successful return */
2709 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2711 struct comedi_device *dev;
2712 struct device *csdev;
2715 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2717 return ERR_PTR(-ENOMEM);
2718 comedi_device_init(dev);
2719 comedi_set_hw_dev(dev, hardware_device);
2720 mutex_lock(&dev->mutex);
2721 mutex_lock(&comedi_board_minor_table_lock);
2722 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2723 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2724 if (!comedi_board_minor_table[i]) {
2725 comedi_board_minor_table[i] = dev;
2729 mutex_unlock(&comedi_board_minor_table_lock);
2730 if (i == COMEDI_NUM_BOARD_MINORS) {
2731 mutex_unlock(&dev->mutex);
2732 comedi_device_cleanup(dev);
2733 comedi_dev_put(dev);
2734 dev_err(hardware_device,
2735 "ran out of minor numbers for board device files\n");
2736 return ERR_PTR(-EBUSY);
2739 csdev = device_create(comedi_class, hardware_device,
2740 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2742 dev->class_dev = get_device(csdev);
2744 /* Note: dev->mutex needs to be unlocked by the caller. */
2748 static void comedi_free_board_minor(unsigned minor)
2750 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2751 comedi_free_board_dev(comedi_clear_board_minor(minor));
2754 void comedi_release_hardware_device(struct device *hardware_device)
2757 struct comedi_device *dev;
2759 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2761 mutex_lock(&comedi_board_minor_table_lock);
2762 dev = comedi_board_minor_table[minor];
2763 if (dev && dev->hw_dev == hardware_device) {
2764 comedi_board_minor_table[minor] = NULL;
2765 mutex_unlock(&comedi_board_minor_table_lock);
2766 comedi_free_board_dev(dev);
2769 mutex_unlock(&comedi_board_minor_table_lock);
2773 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2775 struct comedi_device *dev = s->device;
2776 struct device *csdev;
2779 mutex_lock(&comedi_subdevice_minor_table_lock);
2780 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2781 if (!comedi_subdevice_minor_table[i]) {
2782 comedi_subdevice_minor_table[i] = s;
2786 mutex_unlock(&comedi_subdevice_minor_table_lock);
2787 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2788 dev_err(dev->class_dev,
2789 "ran out of minor numbers for subdevice files\n");
2792 i += COMEDI_NUM_BOARD_MINORS;
2794 csdev = device_create(comedi_class, dev->class_dev,
2795 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2796 dev->minor, s->index);
2798 s->class_dev = csdev;
2803 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2812 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2813 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2815 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2816 mutex_lock(&comedi_subdevice_minor_table_lock);
2817 if (s == comedi_subdevice_minor_table[i])
2818 comedi_subdevice_minor_table[i] = NULL;
2819 mutex_unlock(&comedi_subdevice_minor_table_lock);
2821 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2822 s->class_dev = NULL;
2826 static void comedi_cleanup_board_minors(void)
2830 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2831 comedi_free_board_minor(i);
2834 static int __init comedi_init(void)
2839 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
2841 if (comedi_num_legacy_minors < 0 ||
2842 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2843 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2844 COMEDI_NUM_BOARD_MINORS);
2848 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2849 COMEDI_NUM_MINORS, "comedi");
2852 cdev_init(&comedi_cdev, &comedi_fops);
2853 comedi_cdev.owner = THIS_MODULE;
2855 retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
2857 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2862 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2863 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2867 comedi_class = class_create(THIS_MODULE, "comedi");
2868 if (IS_ERR(comedi_class)) {
2869 pr_err("failed to create class\n");
2870 cdev_del(&comedi_cdev);
2871 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2873 return PTR_ERR(comedi_class);
2876 comedi_class->dev_groups = comedi_dev_groups;
2878 /* XXX requires /proc interface */
2881 /* create devices files for legacy/manual use */
2882 for (i = 0; i < comedi_num_legacy_minors; i++) {
2883 struct comedi_device *dev;
2885 dev = comedi_alloc_board_minor(NULL);
2887 comedi_cleanup_board_minors();
2888 cdev_del(&comedi_cdev);
2889 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2891 return PTR_ERR(dev);
2893 /* comedi_alloc_board_minor() locked the mutex */
2894 mutex_unlock(&dev->mutex);
2899 module_init(comedi_init);
2901 static void __exit comedi_cleanup(void)
2905 comedi_cleanup_board_minors();
2906 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2907 BUG_ON(comedi_board_minor_table[i]);
2908 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2909 BUG_ON(comedi_subdevice_minor_table[i]);
2911 class_destroy(comedi_class);
2912 cdev_del(&comedi_cdev);
2913 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2915 comedi_proc_cleanup();
2917 module_exit(comedi_cleanup);
2919 MODULE_AUTHOR("http://www.comedi.org");
2920 MODULE_DESCRIPTION("Comedi core module");
2921 MODULE_LICENSE("GPL");