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);
116 int comedi_dev_put(struct comedi_device *dev)
119 return kref_put(&dev->refcount, comedi_dev_kref_release);
122 EXPORT_SYMBOL_GPL(comedi_dev_put);
124 static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
127 kref_get(&dev->refcount);
131 static void comedi_device_cleanup(struct comedi_device *dev)
133 struct module *driver_module = NULL;
137 mutex_lock(&dev->mutex);
139 driver_module = dev->driver->module;
140 comedi_device_detach(dev);
141 if (driver_module && dev->use_count)
142 module_put(driver_module);
143 mutex_unlock(&dev->mutex);
146 static bool comedi_clear_board_dev(struct comedi_device *dev)
148 unsigned int i = dev->minor;
149 bool cleared = false;
151 mutex_lock(&comedi_board_minor_table_lock);
152 if (dev == comedi_board_minor_table[i]) {
153 comedi_board_minor_table[i] = NULL;
156 mutex_unlock(&comedi_board_minor_table_lock);
160 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
162 struct comedi_device *dev;
164 mutex_lock(&comedi_board_minor_table_lock);
165 dev = comedi_board_minor_table[minor];
166 comedi_board_minor_table[minor] = NULL;
167 mutex_unlock(&comedi_board_minor_table_lock);
171 static void comedi_free_board_dev(struct comedi_device *dev)
174 comedi_device_cleanup(dev);
175 if (dev->class_dev) {
176 device_destroy(comedi_class,
177 MKDEV(COMEDI_MAJOR, dev->minor));
183 static struct comedi_subdevice
184 *comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned minor)
186 struct comedi_subdevice *s;
187 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
189 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
190 mutex_lock(&comedi_subdevice_minor_table_lock);
191 s = comedi_subdevice_minor_table[i];
192 if (s && s->device != dev)
194 mutex_unlock(&comedi_subdevice_minor_table_lock);
198 static struct comedi_device *comedi_dev_get_from_board_minor(unsigned minor)
200 struct comedi_device *dev;
202 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
203 mutex_lock(&comedi_board_minor_table_lock);
204 dev = comedi_dev_get(comedi_board_minor_table[minor]);
205 mutex_unlock(&comedi_board_minor_table_lock);
209 static struct comedi_device *comedi_dev_get_from_subdevice_minor(unsigned minor)
211 struct comedi_device *dev;
212 struct comedi_subdevice *s;
213 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
215 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
216 mutex_lock(&comedi_subdevice_minor_table_lock);
217 s = comedi_subdevice_minor_table[i];
218 dev = comedi_dev_get(s ? s->device : NULL);
219 mutex_unlock(&comedi_subdevice_minor_table_lock);
223 struct comedi_device *comedi_dev_get_from_minor(unsigned minor)
225 if (minor < COMEDI_NUM_BOARD_MINORS)
226 return comedi_dev_get_from_board_minor(minor);
228 return comedi_dev_get_from_subdevice_minor(minor);
230 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
232 static struct comedi_subdevice *
233 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
235 struct comedi_subdevice *s;
237 if (minor >= COMEDI_NUM_BOARD_MINORS) {
238 s = comedi_subdevice_from_minor(dev, minor);
239 if (s == NULL || (s->subdev_flags & SDF_CMD_READ))
242 return dev->read_subdev;
245 static struct comedi_subdevice *
246 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
248 struct comedi_subdevice *s;
250 if (minor >= COMEDI_NUM_BOARD_MINORS) {
251 s = comedi_subdevice_from_minor(dev, minor);
252 if (s == NULL || (s->subdev_flags & SDF_CMD_WRITE))
255 return dev->write_subdev;
258 static void comedi_file_reset(struct file *file)
260 struct comedi_file *cfp = file->private_data;
261 struct comedi_device *dev = cfp->dev;
262 struct comedi_subdevice *s, *read_s, *write_s;
263 unsigned int minor = iminor(file_inode(file));
265 read_s = dev->read_subdev;
266 write_s = dev->write_subdev;
267 if (minor >= COMEDI_NUM_BOARD_MINORS) {
268 s = comedi_subdevice_from_minor(dev, minor);
269 if (s == NULL || s->subdev_flags & SDF_CMD_READ)
271 if (s == NULL || s->subdev_flags & SDF_CMD_WRITE)
274 cfp->last_attached = dev->attached;
275 cfp->last_detach_count = dev->detach_count;
276 ACCESS_ONCE(cfp->read_subdev) = read_s;
277 ACCESS_ONCE(cfp->write_subdev) = write_s;
280 static void comedi_file_check(struct file *file)
282 struct comedi_file *cfp = file->private_data;
283 struct comedi_device *dev = cfp->dev;
285 if (cfp->last_attached != dev->attached ||
286 cfp->last_detach_count != dev->detach_count)
287 comedi_file_reset(file);
290 static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file)
292 struct comedi_file *cfp = file->private_data;
294 comedi_file_check(file);
295 return ACCESS_ONCE(cfp->read_subdev);
298 static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file)
300 struct comedi_file *cfp = file->private_data;
302 comedi_file_check(file);
303 return ACCESS_ONCE(cfp->write_subdev);
306 static int resize_async_buffer(struct comedi_device *dev,
307 struct comedi_subdevice *s, unsigned new_size)
309 struct comedi_async *async = s->async;
312 if (new_size > async->max_bufsize)
316 dev_dbg(dev->class_dev,
317 "subdevice is busy, cannot resize buffer\n");
320 if (comedi_buf_is_mmapped(s)) {
321 dev_dbg(dev->class_dev,
322 "subdevice is mmapped, cannot resize buffer\n");
326 /* make sure buffer is an integral number of pages
328 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
330 retval = comedi_buf_alloc(dev, s, new_size);
335 retval = s->buf_change(dev, s);
340 dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
341 s->index, async->prealloc_bufsz);
345 /* sysfs attribute files */
347 static ssize_t max_read_buffer_kb_show(struct device *csdev,
348 struct device_attribute *attr, char *buf)
350 unsigned int minor = MINOR(csdev->devt);
351 struct comedi_device *dev;
352 struct comedi_subdevice *s;
353 unsigned int size = 0;
355 dev = comedi_dev_get_from_minor(minor);
359 mutex_lock(&dev->mutex);
360 s = comedi_read_subdevice(dev, minor);
361 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
362 size = s->async->max_bufsize / 1024;
363 mutex_unlock(&dev->mutex);
366 return snprintf(buf, PAGE_SIZE, "%u\n", size);
369 static ssize_t max_read_buffer_kb_store(struct device *csdev,
370 struct device_attribute *attr,
371 const char *buf, size_t count)
373 unsigned int minor = MINOR(csdev->devt);
374 struct comedi_device *dev;
375 struct comedi_subdevice *s;
379 err = kstrtouint(buf, 10, &size);
382 if (size > (UINT_MAX / 1024))
386 dev = comedi_dev_get_from_minor(minor);
390 mutex_lock(&dev->mutex);
391 s = comedi_read_subdevice(dev, minor);
392 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
393 s->async->max_bufsize = size;
396 mutex_unlock(&dev->mutex);
399 return err ? err : count;
401 static DEVICE_ATTR_RW(max_read_buffer_kb);
403 static ssize_t read_buffer_kb_show(struct device *csdev,
404 struct device_attribute *attr, char *buf)
406 unsigned int minor = MINOR(csdev->devt);
407 struct comedi_device *dev;
408 struct comedi_subdevice *s;
409 unsigned int size = 0;
411 dev = comedi_dev_get_from_minor(minor);
415 mutex_lock(&dev->mutex);
416 s = comedi_read_subdevice(dev, minor);
417 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
418 size = s->async->prealloc_bufsz / 1024;
419 mutex_unlock(&dev->mutex);
422 return snprintf(buf, PAGE_SIZE, "%u\n", size);
425 static ssize_t read_buffer_kb_store(struct device *csdev,
426 struct device_attribute *attr,
427 const char *buf, size_t count)
429 unsigned int minor = MINOR(csdev->devt);
430 struct comedi_device *dev;
431 struct comedi_subdevice *s;
435 err = kstrtouint(buf, 10, &size);
438 if (size > (UINT_MAX / 1024))
442 dev = comedi_dev_get_from_minor(minor);
446 mutex_lock(&dev->mutex);
447 s = comedi_read_subdevice(dev, minor);
448 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
449 err = resize_async_buffer(dev, s, size);
452 mutex_unlock(&dev->mutex);
455 return err ? err : count;
457 static DEVICE_ATTR_RW(read_buffer_kb);
459 static ssize_t max_write_buffer_kb_show(struct device *csdev,
460 struct device_attribute *attr,
463 unsigned int minor = MINOR(csdev->devt);
464 struct comedi_device *dev;
465 struct comedi_subdevice *s;
466 unsigned int size = 0;
468 dev = comedi_dev_get_from_minor(minor);
472 mutex_lock(&dev->mutex);
473 s = comedi_write_subdevice(dev, minor);
474 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
475 size = s->async->max_bufsize / 1024;
476 mutex_unlock(&dev->mutex);
479 return snprintf(buf, PAGE_SIZE, "%u\n", size);
482 static ssize_t max_write_buffer_kb_store(struct device *csdev,
483 struct device_attribute *attr,
484 const char *buf, size_t count)
486 unsigned int minor = MINOR(csdev->devt);
487 struct comedi_device *dev;
488 struct comedi_subdevice *s;
492 err = kstrtouint(buf, 10, &size);
495 if (size > (UINT_MAX / 1024))
499 dev = comedi_dev_get_from_minor(minor);
503 mutex_lock(&dev->mutex);
504 s = comedi_write_subdevice(dev, minor);
505 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
506 s->async->max_bufsize = size;
509 mutex_unlock(&dev->mutex);
512 return err ? err : count;
514 static DEVICE_ATTR_RW(max_write_buffer_kb);
516 static ssize_t write_buffer_kb_show(struct device *csdev,
517 struct device_attribute *attr, char *buf)
519 unsigned int minor = MINOR(csdev->devt);
520 struct comedi_device *dev;
521 struct comedi_subdevice *s;
522 unsigned int size = 0;
524 dev = comedi_dev_get_from_minor(minor);
528 mutex_lock(&dev->mutex);
529 s = comedi_write_subdevice(dev, minor);
530 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
531 size = s->async->prealloc_bufsz / 1024;
532 mutex_unlock(&dev->mutex);
535 return snprintf(buf, PAGE_SIZE, "%u\n", size);
538 static ssize_t write_buffer_kb_store(struct device *csdev,
539 struct device_attribute *attr,
540 const char *buf, size_t count)
542 unsigned int minor = MINOR(csdev->devt);
543 struct comedi_device *dev;
544 struct comedi_subdevice *s;
548 err = kstrtouint(buf, 10, &size);
551 if (size > (UINT_MAX / 1024))
555 dev = comedi_dev_get_from_minor(minor);
559 mutex_lock(&dev->mutex);
560 s = comedi_write_subdevice(dev, minor);
561 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
562 err = resize_async_buffer(dev, s, size);
565 mutex_unlock(&dev->mutex);
568 return err ? err : count;
570 static DEVICE_ATTR_RW(write_buffer_kb);
572 static struct attribute *comedi_dev_attrs[] = {
573 &dev_attr_max_read_buffer_kb.attr,
574 &dev_attr_read_buffer_kb.attr,
575 &dev_attr_max_write_buffer_kb.attr,
576 &dev_attr_write_buffer_kb.attr,
579 ATTRIBUTE_GROUPS(comedi_dev);
581 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
582 unsigned mask, unsigned bits)
586 spin_lock_irqsave(&s->spin_lock, flags);
587 s->runflags &= ~mask;
588 s->runflags |= (bits & mask);
589 spin_unlock_irqrestore(&s->spin_lock, flags);
592 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
597 spin_lock_irqsave(&s->spin_lock, flags);
598 runflags = s->runflags;
599 spin_unlock_irqrestore(&s->spin_lock, flags);
603 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
605 unsigned runflags = comedi_get_subdevice_runflags(s);
607 return (runflags & COMEDI_SRF_RUNNING) ? true : false;
609 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
611 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
613 unsigned runflags = comedi_get_subdevice_runflags(s);
615 return (runflags & COMEDI_SRF_ERROR) ? true : false;
618 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
620 unsigned runflags = comedi_get_subdevice_runflags(s);
622 return (runflags & COMEDI_SRF_BUSY_MASK) ? false : true;
626 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
627 * @s: comedi_subdevice struct
628 * @size: size of the memory to allocate
630 * This also sets the subdevice runflags to allow the core to automatically
631 * free the private data during the detach.
633 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
635 s->private = kzalloc(size, GFP_KERNEL);
637 s->runflags |= COMEDI_SRF_FREE_SPRIV;
640 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
643 This function restores a subdevice to an idle state.
645 static void do_become_nonbusy(struct comedi_device *dev,
646 struct comedi_subdevice *s)
648 struct comedi_async *async = s->async;
650 comedi_set_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
653 async->inttrig = NULL;
654 kfree(async->cmd.chanlist);
655 async->cmd.chanlist = NULL;
657 wake_up_interruptible_all(&s->async->wait_head);
659 dev_err(dev->class_dev,
660 "BUG: (?) do_become_nonbusy called with async=NULL\n");
665 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
669 if (comedi_is_subdevice_running(s) && s->cancel)
670 ret = s->cancel(dev, s);
672 do_become_nonbusy(dev, s);
677 void comedi_device_cancel_all(struct comedi_device *dev)
679 struct comedi_subdevice *s;
685 for (i = 0; i < dev->n_subdevices; i++) {
686 s = &dev->subdevices[i];
692 static int is_device_busy(struct comedi_device *dev)
694 struct comedi_subdevice *s;
700 for (i = 0; i < dev->n_subdevices; i++) {
701 s = &dev->subdevices[i];
704 if (s->async && comedi_buf_is_mmapped(s))
716 pointer to devconfig structure
719 devconfig structure at arg
724 static int do_devconfig_ioctl(struct comedi_device *dev,
725 struct comedi_devconfig __user *arg)
727 struct comedi_devconfig it;
729 if (!capable(CAP_SYS_ADMIN))
733 if (is_device_busy(dev))
736 struct module *driver_module = dev->driver->module;
738 comedi_device_detach(dev);
739 module_put(driver_module);
744 if (copy_from_user(&it, arg, sizeof(it)))
747 it.board_name[COMEDI_NAMELEN - 1] = 0;
749 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
750 dev_warn(dev->class_dev,
751 "comedi_config --init_data is deprecated\n");
755 if (dev->minor >= comedi_num_legacy_minors)
756 /* don't re-use dynamically allocated comedi devices */
759 /* This increments the driver module count on success. */
760 return comedi_device_attach(dev, &it);
765 buffer configuration ioctl
768 pointer to bufconfig structure
774 modified bufconfig at arg
777 static int do_bufconfig_ioctl(struct comedi_device *dev,
778 struct comedi_bufconfig __user *arg)
780 struct comedi_bufconfig bc;
781 struct comedi_async *async;
782 struct comedi_subdevice *s;
785 if (copy_from_user(&bc, arg, sizeof(bc)))
788 if (bc.subdevice >= dev->n_subdevices)
791 s = &dev->subdevices[bc.subdevice];
795 dev_dbg(dev->class_dev,
796 "subdevice does not have async capability\n");
802 if (bc.maximum_size) {
803 if (!capable(CAP_SYS_ADMIN))
806 async->max_bufsize = bc.maximum_size;
810 retval = resize_async_buffer(dev, s, bc.size);
815 bc.size = async->prealloc_bufsz;
816 bc.maximum_size = async->max_bufsize;
819 if (copy_to_user(arg, &bc, sizeof(bc)))
830 pointer to devinfo structure
839 static int do_devinfo_ioctl(struct comedi_device *dev,
840 struct comedi_devinfo __user *arg,
843 struct comedi_subdevice *s;
844 struct comedi_devinfo devinfo;
846 memset(&devinfo, 0, sizeof(devinfo));
848 /* fill devinfo structure */
849 devinfo.version_code = COMEDI_VERSION_CODE;
850 devinfo.n_subdevs = dev->n_subdevices;
851 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
852 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
854 s = comedi_file_read_subdevice(file);
856 devinfo.read_subdevice = s->index;
858 devinfo.read_subdevice = -1;
860 s = comedi_file_write_subdevice(file);
862 devinfo.write_subdevice = s->index;
864 devinfo.write_subdevice = -1;
866 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
877 pointer to array of subdevice info structures
883 array of subdevice info structures at arg
886 static int do_subdinfo_ioctl(struct comedi_device *dev,
887 struct comedi_subdinfo __user *arg, void *file)
890 struct comedi_subdinfo *tmp, *us;
891 struct comedi_subdevice *s;
893 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
897 /* fill subdinfo structs */
898 for (i = 0; i < dev->n_subdevices; i++) {
899 s = &dev->subdevices[i];
903 us->n_chan = s->n_chan;
904 us->subd_flags = s->subdev_flags;
905 if (comedi_is_subdevice_running(s))
906 us->subd_flags |= SDF_RUNNING;
907 #define TIMER_nanosec 5 /* backwards compatibility */
908 us->timer_type = TIMER_nanosec;
909 us->len_chanlist = s->len_chanlist;
910 us->maxdata = s->maxdata;
911 if (s->range_table) {
913 (i << 24) | (0 << 16) | (s->range_table->length);
915 us->range_type = 0; /* XXX */
919 us->subd_flags |= SDF_BUSY;
921 us->subd_flags |= SDF_BUSY_OWNER;
923 us->subd_flags |= SDF_LOCKED;
925 us->subd_flags |= SDF_LOCK_OWNER;
926 if (!s->maxdata && s->maxdata_list)
927 us->subd_flags |= SDF_MAXDATA;
928 if (s->range_table_list)
929 us->subd_flags |= SDF_RANGETYPE;
931 us->subd_flags |= SDF_CMD;
933 if (s->insn_bits != &insn_inval)
934 us->insn_bits_support = COMEDI_SUPPORTED;
936 us->insn_bits_support = COMEDI_UNSUPPORTED;
939 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
943 return ret ? -EFAULT : 0;
951 pointer to chaninfo structure
954 chaninfo structure at arg
957 arrays at elements of chaninfo structure
960 static int do_chaninfo_ioctl(struct comedi_device *dev,
961 struct comedi_chaninfo __user *arg)
963 struct comedi_subdevice *s;
964 struct comedi_chaninfo it;
966 if (copy_from_user(&it, arg, sizeof(it)))
969 if (it.subdev >= dev->n_subdevices)
971 s = &dev->subdevices[it.subdev];
973 if (it.maxdata_list) {
974 if (s->maxdata || !s->maxdata_list)
976 if (copy_to_user(it.maxdata_list, s->maxdata_list,
977 s->n_chan * sizeof(unsigned int)))
982 return -EINVAL; /* flaglist not supported */
987 if (!s->range_table_list)
989 for (i = 0; i < s->n_chan; i++) {
992 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
993 (s->range_table_list[i]->length);
994 if (put_user(x, it.rangelist + i))
998 if (copy_to_user(it.rangelist, s->range_type_list,
999 s->n_chan * sizeof(unsigned int)))
1009 buffer information ioctl
1012 pointer to bufinfo structure
1018 modified bufinfo at arg
1021 static int do_bufinfo_ioctl(struct comedi_device *dev,
1022 struct comedi_bufinfo __user *arg, void *file)
1024 struct comedi_bufinfo bi;
1025 struct comedi_subdevice *s;
1026 struct comedi_async *async;
1028 if (copy_from_user(&bi, arg, sizeof(bi)))
1031 if (bi.subdevice >= dev->n_subdevices)
1034 s = &dev->subdevices[bi.subdevice];
1039 dev_dbg(dev->class_dev,
1040 "subdevice does not have async capability\n");
1041 bi.buf_write_ptr = 0;
1042 bi.buf_read_ptr = 0;
1043 bi.buf_write_count = 0;
1044 bi.buf_read_count = 0;
1046 bi.bytes_written = 0;
1051 bi.bytes_written = 0;
1052 goto copyback_position;
1054 if (s->busy != file)
1057 if (bi.bytes_read && !(async->cmd.flags & CMDF_WRITE)) {
1058 bi.bytes_read = comedi_buf_read_alloc(s, bi.bytes_read);
1059 comedi_buf_read_free(s, bi.bytes_read);
1061 if (comedi_is_subdevice_idle(s) &&
1062 comedi_buf_n_bytes_ready(s) == 0) {
1063 do_become_nonbusy(dev, s);
1067 if (bi.bytes_written && (async->cmd.flags & CMDF_WRITE)) {
1069 comedi_buf_write_alloc(s, bi.bytes_written);
1070 comedi_buf_write_free(s, bi.bytes_written);
1074 bi.buf_write_count = async->buf_write_count;
1075 bi.buf_write_ptr = async->buf_write_ptr;
1076 bi.buf_read_count = async->buf_read_count;
1077 bi.buf_read_ptr = async->buf_read_ptr;
1080 if (copy_to_user(arg, &bi, sizeof(bi)))
1086 static int check_insn_config_length(struct comedi_insn *insn,
1093 case INSN_CONFIG_DIO_OUTPUT:
1094 case INSN_CONFIG_DIO_INPUT:
1095 case INSN_CONFIG_DISARM:
1096 case INSN_CONFIG_RESET:
1100 case INSN_CONFIG_ARM:
1101 case INSN_CONFIG_DIO_QUERY:
1102 case INSN_CONFIG_BLOCK_SIZE:
1103 case INSN_CONFIG_FILTER:
1104 case INSN_CONFIG_SERIAL_CLOCK:
1105 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1106 case INSN_CONFIG_ALT_SOURCE:
1107 case INSN_CONFIG_SET_COUNTER_MODE:
1108 case INSN_CONFIG_8254_READ_STATUS:
1109 case INSN_CONFIG_SET_ROUTING:
1110 case INSN_CONFIG_GET_ROUTING:
1111 case INSN_CONFIG_GET_PWM_STATUS:
1112 case INSN_CONFIG_PWM_SET_PERIOD:
1113 case INSN_CONFIG_PWM_GET_PERIOD:
1117 case INSN_CONFIG_SET_GATE_SRC:
1118 case INSN_CONFIG_GET_GATE_SRC:
1119 case INSN_CONFIG_SET_CLOCK_SRC:
1120 case INSN_CONFIG_GET_CLOCK_SRC:
1121 case INSN_CONFIG_SET_OTHER_SRC:
1122 case INSN_CONFIG_GET_COUNTER_STATUS:
1123 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1124 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1125 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1129 case INSN_CONFIG_PWM_OUTPUT:
1130 case INSN_CONFIG_ANALOG_TRIG:
1134 case INSN_CONFIG_DIGITAL_TRIG:
1138 /* by default we allow the insn since we don't have checks for
1139 * all possible cases yet */
1141 pr_warn("No check for data length of config insn id %i is implemented\n",
1143 pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
1144 pr_warn("Assuming n=%i is correct\n", insn->n);
1150 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1151 unsigned int *data, void *file)
1153 struct comedi_subdevice *s;
1157 if (insn->insn & INSN_MASK_SPECIAL) {
1158 /* a non-subdevice instruction */
1160 switch (insn->insn) {
1170 do_gettimeofday(&tv);
1171 data[0] = tv.tv_sec;
1172 data[1] = tv.tv_usec;
1178 if (insn->n != 1 || data[0] >= 100000) {
1182 udelay(data[0] / 1000);
1190 if (insn->subdev >= dev->n_subdevices) {
1191 dev_dbg(dev->class_dev,
1192 "%d not usable subdevice\n",
1197 s = &dev->subdevices[insn->subdev];
1199 dev_dbg(dev->class_dev, "no async\n");
1203 if (!s->async->inttrig) {
1204 dev_dbg(dev->class_dev, "no inttrig\n");
1208 ret = s->async->inttrig(dev, s, data[0]);
1213 dev_dbg(dev->class_dev, "invalid insn\n");
1218 /* a subdevice instruction */
1219 unsigned int maxdata;
1221 if (insn->subdev >= dev->n_subdevices) {
1222 dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1227 s = &dev->subdevices[insn->subdev];
1229 if (s->type == COMEDI_SUBD_UNUSED) {
1230 dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1236 /* are we locked? (ioctl lock) */
1237 if (s->lock && s->lock != file) {
1238 dev_dbg(dev->class_dev, "device locked\n");
1243 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1246 dev_dbg(dev->class_dev, "bad chanspec\n");
1254 /* This looks arbitrary. It is. */
1255 s->busy = &parse_insn;
1256 switch (insn->insn) {
1258 ret = s->insn_read(dev, s, insn, data);
1259 if (ret == -ETIMEDOUT) {
1260 dev_dbg(dev->class_dev,
1261 "subdevice %d read instruction timed out\n",
1266 maxdata = s->maxdata_list
1267 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1269 for (i = 0; i < insn->n; ++i) {
1270 if (data[i] > maxdata) {
1272 dev_dbg(dev->class_dev,
1273 "bad data value(s)\n");
1278 ret = s->insn_write(dev, s, insn, data);
1279 if (ret == -ETIMEDOUT) {
1280 dev_dbg(dev->class_dev,
1281 "subdevice %d write instruction timed out\n",
1290 /* Most drivers ignore the base channel in
1291 * insn->chanspec. Fix this here if
1292 * the subdevice has <= 32 channels. */
1293 unsigned int orig_mask = data[0];
1294 unsigned int shift = 0;
1296 if (s->n_chan <= 32) {
1297 shift = CR_CHAN(insn->chanspec);
1304 ret = s->insn_bits(dev, s, insn, data);
1305 data[0] = orig_mask;
1311 ret = check_insn_config_length(insn, data);
1314 ret = s->insn_config(dev, s, insn, data);
1330 * synchronous instructions
1333 * pointer to sync cmd structure
1336 * sync cmd struct at arg
1343 /* arbitrary limits */
1344 #define MAX_SAMPLES 256
1345 static int do_insnlist_ioctl(struct comedi_device *dev,
1346 struct comedi_insnlist __user *arg, void *file)
1348 struct comedi_insnlist insnlist;
1349 struct comedi_insn *insns = NULL;
1350 unsigned int *data = NULL;
1354 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1357 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1363 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1369 if (copy_from_user(insns, insnlist.insns,
1370 sizeof(*insns) * insnlist.n_insns)) {
1371 dev_dbg(dev->class_dev, "copy_from_user failed\n");
1376 for (i = 0; i < insnlist.n_insns; i++) {
1377 if (insns[i].n > MAX_SAMPLES) {
1378 dev_dbg(dev->class_dev,
1379 "number of samples too large\n");
1383 if (insns[i].insn & INSN_MASK_WRITE) {
1384 if (copy_from_user(data, insns[i].data,
1385 insns[i].n * sizeof(unsigned int))) {
1386 dev_dbg(dev->class_dev,
1387 "copy_from_user failed\n");
1392 ret = parse_insn(dev, insns + i, data, file);
1395 if (insns[i].insn & INSN_MASK_READ) {
1396 if (copy_to_user(insns[i].data, data,
1397 insns[i].n * sizeof(unsigned int))) {
1398 dev_dbg(dev->class_dev,
1399 "copy_to_user failed\n");
1419 * synchronous instructions
1425 * struct comedi_insn struct at arg
1431 static int do_insn_ioctl(struct comedi_device *dev,
1432 struct comedi_insn __user *arg, void *file)
1434 struct comedi_insn insn;
1435 unsigned int *data = NULL;
1438 data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
1444 if (copy_from_user(&insn, arg, sizeof(insn))) {
1449 /* This is where the behavior of insn and insnlist deviate. */
1450 if (insn.n > MAX_SAMPLES)
1451 insn.n = MAX_SAMPLES;
1452 if (insn.insn & INSN_MASK_WRITE) {
1453 if (copy_from_user(data,
1455 insn.n * sizeof(unsigned int))) {
1460 ret = parse_insn(dev, &insn, data, file);
1463 if (insn.insn & INSN_MASK_READ) {
1464 if (copy_to_user(insn.data,
1466 insn.n * sizeof(unsigned int))) {
1479 static int __comedi_get_user_cmd(struct comedi_device *dev,
1480 struct comedi_cmd __user *arg,
1481 struct comedi_cmd *cmd)
1483 struct comedi_subdevice *s;
1485 if (copy_from_user(cmd, arg, sizeof(*cmd))) {
1486 dev_dbg(dev->class_dev, "bad cmd address\n");
1490 if (cmd->subdev >= dev->n_subdevices) {
1491 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
1495 s = &dev->subdevices[cmd->subdev];
1497 if (s->type == COMEDI_SUBD_UNUSED) {
1498 dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1503 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1504 dev_dbg(dev->class_dev,
1505 "subdevice %d does not support commands\n",
1510 /* make sure channel/gain list isn't too long */
1511 if (cmd->chanlist_len > s->len_chanlist) {
1512 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1513 cmd->chanlist_len, s->len_chanlist);
1518 * Set the CMDF_WRITE flag to the correct state if the subdevice
1519 * supports only "read" commands or only "write" commands.
1521 switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
1523 cmd->flags &= ~CMDF_WRITE;
1526 cmd->flags |= CMDF_WRITE;
1535 static int __comedi_get_user_chanlist(struct comedi_device *dev,
1536 struct comedi_subdevice *s,
1537 unsigned int __user *user_chanlist,
1538 struct comedi_cmd *cmd)
1540 unsigned int *chanlist;
1543 cmd->chanlist = NULL;
1544 chanlist = memdup_user(user_chanlist,
1545 cmd->chanlist_len * sizeof(unsigned int));
1546 if (IS_ERR(chanlist))
1547 return PTR_ERR(chanlist);
1549 /* make sure each element in channel/gain list is valid */
1550 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1556 cmd->chanlist = chanlist;
1561 static int do_cmd_ioctl(struct comedi_device *dev,
1562 struct comedi_cmd __user *arg, void *file)
1564 struct comedi_cmd cmd;
1565 struct comedi_subdevice *s;
1566 struct comedi_async *async;
1567 unsigned int __user *user_chanlist;
1570 /* get the user's cmd and do some simple validation */
1571 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1575 /* save user's chanlist pointer so it can be restored later */
1576 user_chanlist = (unsigned int __user *)cmd.chanlist;
1578 s = &dev->subdevices[cmd.subdev];
1581 /* are we locked? (ioctl lock) */
1582 if (s->lock && s->lock != file) {
1583 dev_dbg(dev->class_dev, "subdevice locked\n");
1589 dev_dbg(dev->class_dev, "subdevice busy\n");
1593 /* make sure channel/gain list isn't too short */
1594 if (cmd.chanlist_len < 1) {
1595 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
1601 async->cmd.data = NULL;
1603 /* load channel/gain list */
1604 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1608 ret = s->do_cmdtest(dev, s, &async->cmd);
1610 if (async->cmd.flags & CMDF_BOGUS || ret) {
1611 dev_dbg(dev->class_dev, "test returned %d\n", ret);
1613 /* restore chanlist pointer before copying back */
1614 cmd.chanlist = (unsigned int __force *)user_chanlist;
1616 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1617 dev_dbg(dev->class_dev, "fault writing cmd\n");
1625 if (!async->prealloc_bufsz) {
1627 dev_dbg(dev->class_dev, "no buffer (?)\n");
1631 comedi_buf_reset(s);
1633 async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
1634 if (async->cmd.flags & CMDF_WAKE_EOS)
1635 async->cb_mask |= COMEDI_CB_EOS;
1637 comedi_set_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
1638 COMEDI_SRF_RUNNING);
1641 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1642 * race with comedi_read() or comedi_write().
1645 ret = s->do_cmd(dev, s);
1650 do_become_nonbusy(dev, s);
1657 command testing ioctl
1660 pointer to cmd structure
1663 cmd structure at arg
1667 modified cmd structure at arg
1670 static int do_cmdtest_ioctl(struct comedi_device *dev,
1671 struct comedi_cmd __user *arg, void *file)
1673 struct comedi_cmd cmd;
1674 struct comedi_subdevice *s;
1675 unsigned int __user *user_chanlist;
1678 /* get the user's cmd and do some simple validation */
1679 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1683 /* save user's chanlist pointer so it can be restored later */
1684 user_chanlist = (unsigned int __user *)cmd.chanlist;
1686 s = &dev->subdevices[cmd.subdev];
1688 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1689 if (user_chanlist) {
1690 /* load channel/gain list */
1691 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1696 ret = s->do_cmdtest(dev, s, &cmd);
1698 kfree(cmd.chanlist); /* free kernel copy of user chanlist */
1700 /* restore chanlist pointer before copying back */
1701 cmd.chanlist = (unsigned int __force *)user_chanlist;
1703 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1704 dev_dbg(dev->class_dev, "bad cmd address\n");
1726 static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
1730 unsigned long flags;
1731 struct comedi_subdevice *s;
1733 if (arg >= dev->n_subdevices)
1735 s = &dev->subdevices[arg];
1737 spin_lock_irqsave(&s->spin_lock, flags);
1738 if (s->busy || s->lock)
1742 spin_unlock_irqrestore(&s->spin_lock, flags);
1760 This function isn't protected by the semaphore, since
1761 we already own the lock.
1763 static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
1766 struct comedi_subdevice *s;
1768 if (arg >= dev->n_subdevices)
1770 s = &dev->subdevices[arg];
1775 if (s->lock && s->lock != file)
1778 if (s->lock == file)
1786 cancel acquisition ioctl
1798 static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
1801 struct comedi_subdevice *s;
1803 if (arg >= dev->n_subdevices)
1805 s = &dev->subdevices[arg];
1806 if (s->async == NULL)
1812 if (s->busy != file)
1815 return do_cancel(dev, s);
1820 instructs driver to synchronize buffers
1832 static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
1835 struct comedi_subdevice *s;
1837 if (arg >= dev->n_subdevices)
1839 s = &dev->subdevices[arg];
1844 if (s->busy != file)
1848 return s->poll(dev, s);
1854 * COMEDI_SETRSUBD ioctl
1855 * sets the current "read" subdevice on a per-file basis
1866 static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1869 struct comedi_file *cfp = file->private_data;
1870 struct comedi_subdevice *s_old, *s_new;
1872 if (arg >= dev->n_subdevices)
1875 s_new = &dev->subdevices[arg];
1876 s_old = comedi_file_read_subdevice(file);
1878 return 0; /* no change */
1880 if (!(s_new->subdev_flags & SDF_CMD_READ))
1884 * Check the file isn't still busy handling a "read" command on the
1885 * old subdevice (if any).
1887 if (s_old && s_old->busy == file && s_old->async &&
1888 !(s_old->async->cmd.flags & CMDF_WRITE))
1891 ACCESS_ONCE(cfp->read_subdev) = s_new;
1896 * COMEDI_SETWSUBD ioctl
1897 * sets the current "write" subdevice on a per-file basis
1908 static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
1911 struct comedi_file *cfp = file->private_data;
1912 struct comedi_subdevice *s_old, *s_new;
1914 if (arg >= dev->n_subdevices)
1917 s_new = &dev->subdevices[arg];
1918 s_old = comedi_file_write_subdevice(file);
1920 return 0; /* no change */
1922 if (!(s_new->subdev_flags & SDF_CMD_WRITE))
1926 * Check the file isn't still busy handling a "write" command on the
1927 * old subdevice (if any).
1929 if (s_old && s_old->busy == file && s_old->async &&
1930 (s_old->async->cmd.flags & CMDF_WRITE))
1933 ACCESS_ONCE(cfp->write_subdev) = s_new;
1937 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1940 unsigned minor = iminor(file_inode(file));
1941 struct comedi_file *cfp = file->private_data;
1942 struct comedi_device *dev = cfp->dev;
1945 mutex_lock(&dev->mutex);
1947 /* Device config is special, because it must work on
1948 * an unconfigured device. */
1949 if (cmd == COMEDI_DEVCONFIG) {
1950 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1951 /* Device config not appropriate on non-board minors. */
1955 rc = do_devconfig_ioctl(dev,
1956 (struct comedi_devconfig __user *)arg);
1959 dev->minor >= comedi_num_legacy_minors) {
1960 /* Successfully unconfigured a dynamically
1961 * allocated device. Try and remove it. */
1962 if (comedi_clear_board_dev(dev)) {
1963 mutex_unlock(&dev->mutex);
1964 comedi_free_board_dev(dev);
1972 if (!dev->attached) {
1973 dev_dbg(dev->class_dev, "no driver attached\n");
1979 case COMEDI_BUFCONFIG:
1980 rc = do_bufconfig_ioctl(dev,
1981 (struct comedi_bufconfig __user *)arg);
1983 case COMEDI_DEVINFO:
1984 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1987 case COMEDI_SUBDINFO:
1988 rc = do_subdinfo_ioctl(dev,
1989 (struct comedi_subdinfo __user *)arg,
1992 case COMEDI_CHANINFO:
1993 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1995 case COMEDI_RANGEINFO:
1996 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1998 case COMEDI_BUFINFO:
1999 rc = do_bufinfo_ioctl(dev,
2000 (struct comedi_bufinfo __user *)arg,
2004 rc = do_lock_ioctl(dev, arg, file);
2007 rc = do_unlock_ioctl(dev, arg, file);
2010 rc = do_cancel_ioctl(dev, arg, file);
2013 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
2015 case COMEDI_CMDTEST:
2016 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
2019 case COMEDI_INSNLIST:
2020 rc = do_insnlist_ioctl(dev,
2021 (struct comedi_insnlist __user *)arg,
2025 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
2029 rc = do_poll_ioctl(dev, arg, file);
2031 case COMEDI_SETRSUBD:
2032 rc = do_setrsubd_ioctl(dev, arg, file);
2034 case COMEDI_SETWSUBD:
2035 rc = do_setwsubd_ioctl(dev, arg, file);
2043 mutex_unlock(&dev->mutex);
2047 static void comedi_vm_open(struct vm_area_struct *area)
2049 struct comedi_buf_map *bm;
2051 bm = area->vm_private_data;
2052 comedi_buf_map_get(bm);
2055 static void comedi_vm_close(struct vm_area_struct *area)
2057 struct comedi_buf_map *bm;
2059 bm = area->vm_private_data;
2060 comedi_buf_map_put(bm);
2063 static struct vm_operations_struct comedi_vm_ops = {
2064 .open = comedi_vm_open,
2065 .close = comedi_vm_close,
2068 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
2070 struct comedi_file *cfp = file->private_data;
2071 struct comedi_device *dev = cfp->dev;
2072 struct comedi_subdevice *s;
2073 struct comedi_async *async;
2074 struct comedi_buf_map *bm = NULL;
2075 unsigned long start = vma->vm_start;
2082 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2083 * and down-reading &dev->attach_lock should normally succeed without
2084 * contention unless the device is in the process of being attached
2087 if (!down_read_trylock(&dev->attach_lock))
2090 if (!dev->attached) {
2091 dev_dbg(dev->class_dev, "no driver attached\n");
2096 if (vma->vm_flags & VM_WRITE)
2097 s = comedi_file_write_subdevice(file);
2099 s = comedi_file_read_subdevice(file);
2111 if (vma->vm_pgoff != 0) {
2112 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
2117 size = vma->vm_end - vma->vm_start;
2118 if (size > async->prealloc_bufsz) {
2122 if (size & (~PAGE_MASK)) {
2127 n_pages = size >> PAGE_SHIFT;
2129 /* get reference to current buf map (if any) */
2130 bm = comedi_buf_map_from_subdev_get(s);
2131 if (!bm || n_pages > bm->n_pages) {
2135 for (i = 0; i < n_pages; ++i) {
2136 struct comedi_buf_page *buf = &bm->page_list[i];
2138 if (remap_pfn_range(vma, start,
2139 page_to_pfn(virt_to_page(buf->virt_addr)),
2140 PAGE_SIZE, PAGE_SHARED)) {
2147 vma->vm_ops = &comedi_vm_ops;
2148 vma->vm_private_data = bm;
2150 vma->vm_ops->open(vma);
2154 up_read(&dev->attach_lock);
2155 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
2159 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2161 unsigned int mask = 0;
2162 struct comedi_file *cfp = file->private_data;
2163 struct comedi_device *dev = cfp->dev;
2164 struct comedi_subdevice *s;
2166 mutex_lock(&dev->mutex);
2168 if (!dev->attached) {
2169 dev_dbg(dev->class_dev, "no driver attached\n");
2173 s = comedi_file_read_subdevice(file);
2174 if (s && s->async) {
2175 poll_wait(file, &s->async->wait_head, wait);
2176 if (!s->busy || !comedi_is_subdevice_running(s) ||
2177 (s->async->cmd.flags & CMDF_WRITE) ||
2178 comedi_buf_read_n_available(s) > 0)
2179 mask |= POLLIN | POLLRDNORM;
2182 s = comedi_file_write_subdevice(file);
2183 if (s && s->async) {
2184 unsigned int bps = comedi_bytes_per_sample(s);
2186 poll_wait(file, &s->async->wait_head, wait);
2187 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
2188 if (!s->busy || !comedi_is_subdevice_running(s) ||
2189 !(s->async->cmd.flags & CMDF_WRITE) ||
2190 comedi_buf_write_n_allocated(s) >= bps)
2191 mask |= POLLOUT | POLLWRNORM;
2195 mutex_unlock(&dev->mutex);
2199 static ssize_t comedi_write(struct file *file, const char __user *buf,
2200 size_t nbytes, loff_t *offset)
2202 struct comedi_subdevice *s;
2203 struct comedi_async *async;
2204 int n, m, count = 0, retval = 0;
2205 DECLARE_WAITQUEUE(wait, current);
2206 struct comedi_file *cfp = file->private_data;
2207 struct comedi_device *dev = cfp->dev;
2208 bool on_wait_queue = false;
2210 unsigned int old_detach_count;
2212 /* Protect against device detachment during operation. */
2213 down_read(&dev->attach_lock);
2214 attach_locked = true;
2215 old_detach_count = dev->detach_count;
2217 if (!dev->attached) {
2218 dev_dbg(dev->class_dev, "no driver attached\n");
2223 s = comedi_file_write_subdevice(file);
2224 if (!s || !s->async) {
2231 if (!s->busy || !nbytes)
2233 if (s->busy != file) {
2237 if (!(async->cmd.flags & CMDF_WRITE)) {
2242 add_wait_queue(&async->wait_head, &wait);
2243 on_wait_queue = true;
2244 while (nbytes > 0 && !retval) {
2245 set_current_state(TASK_INTERRUPTIBLE);
2247 if (!comedi_is_subdevice_running(s)) {
2249 struct comedi_subdevice *new_s;
2251 if (comedi_is_subdevice_in_error(s))
2256 * To avoid deadlock, cannot acquire dev->mutex
2257 * while dev->attach_lock is held. Need to
2258 * remove task from the async wait queue before
2259 * releasing dev->attach_lock, as it might not
2260 * be valid afterwards.
2262 remove_wait_queue(&async->wait_head, &wait);
2263 on_wait_queue = false;
2264 up_read(&dev->attach_lock);
2265 attach_locked = false;
2266 mutex_lock(&dev->mutex);
2268 * Become non-busy unless things have changed
2269 * behind our back. Checking dev->detach_count
2270 * is unchanged ought to be sufficient (unless
2271 * there have been 2**32 detaches in the
2272 * meantime!), but check the subdevice pointer
2273 * as well just in case.
2275 new_s = comedi_file_write_subdevice(file);
2276 if (dev->attached &&
2277 old_detach_count == dev->detach_count &&
2278 s == new_s && new_s->async == async)
2279 do_become_nonbusy(dev, s);
2280 mutex_unlock(&dev->mutex);
2288 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2289 m = async->prealloc_bufsz - async->buf_write_ptr;
2290 comedi_buf_write_alloc(s, async->prealloc_bufsz);
2291 if (m > comedi_buf_write_n_allocated(s))
2292 m = comedi_buf_write_n_allocated(s);
2297 if (file->f_flags & O_NONBLOCK) {
2302 if (signal_pending(current)) {
2303 retval = -ERESTARTSYS;
2308 if (s->busy != file) {
2312 if (!(async->cmd.flags & CMDF_WRITE)) {
2319 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2325 comedi_buf_write_free(s, n);
2331 break; /* makes device work like a pipe */
2335 remove_wait_queue(&async->wait_head, &wait);
2336 set_current_state(TASK_RUNNING);
2338 up_read(&dev->attach_lock);
2340 return count ? count : retval;
2343 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2346 struct comedi_subdevice *s;
2347 struct comedi_async *async;
2348 int n, m, count = 0, retval = 0;
2349 DECLARE_WAITQUEUE(wait, current);
2350 struct comedi_file *cfp = file->private_data;
2351 struct comedi_device *dev = cfp->dev;
2352 unsigned int old_detach_count;
2353 bool become_nonbusy = false;
2356 /* Protect against device detachment during operation. */
2357 down_read(&dev->attach_lock);
2358 attach_locked = true;
2359 old_detach_count = dev->detach_count;
2361 if (!dev->attached) {
2362 dev_dbg(dev->class_dev, "no driver attached\n");
2367 s = comedi_file_read_subdevice(file);
2368 if (!s || !s->async) {
2374 if (!s->busy || !nbytes)
2376 if (s->busy != file) {
2380 if (async->cmd.flags & CMDF_WRITE) {
2385 add_wait_queue(&async->wait_head, &wait);
2386 while (nbytes > 0 && !retval) {
2387 set_current_state(TASK_INTERRUPTIBLE);
2391 m = comedi_buf_read_n_available(s);
2392 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2393 m = async->prealloc_bufsz - async->buf_read_ptr;
2398 if (!comedi_is_subdevice_running(s)) {
2399 if (comedi_is_subdevice_in_error(s))
2403 become_nonbusy = true;
2406 if (file->f_flags & O_NONBLOCK) {
2411 if (signal_pending(current)) {
2412 retval = -ERESTARTSYS;
2419 if (s->busy != file) {
2423 if (async->cmd.flags & CMDF_WRITE) {
2429 m = copy_to_user(buf, async->prealloc_buf +
2430 async->buf_read_ptr, n);
2436 comedi_buf_read_alloc(s, n);
2437 comedi_buf_read_free(s, n);
2443 break; /* makes device work like a pipe */
2445 remove_wait_queue(&async->wait_head, &wait);
2446 set_current_state(TASK_RUNNING);
2447 if (become_nonbusy || comedi_is_subdevice_idle(s)) {
2448 struct comedi_subdevice *new_s;
2451 * To avoid deadlock, cannot acquire dev->mutex
2452 * while dev->attach_lock is held.
2454 up_read(&dev->attach_lock);
2455 attach_locked = false;
2456 mutex_lock(&dev->mutex);
2458 * Check device hasn't become detached behind our back.
2459 * Checking dev->detach_count is unchanged ought to be
2460 * sufficient (unless there have been 2**32 detaches in the
2461 * meantime!), but check the subdevice pointer as well just in
2464 new_s = comedi_file_read_subdevice(file);
2465 if (dev->attached && old_detach_count == dev->detach_count &&
2466 s == new_s && new_s->async == async) {
2467 if (become_nonbusy || comedi_buf_n_bytes_ready(s) == 0)
2468 do_become_nonbusy(dev, s);
2470 mutex_unlock(&dev->mutex);
2474 up_read(&dev->attach_lock);
2476 return count ? count : retval;
2479 static int comedi_open(struct inode *inode, struct file *file)
2481 const unsigned minor = iminor(inode);
2482 struct comedi_file *cfp;
2483 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2487 pr_debug("invalid minor number\n");
2491 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
2497 mutex_lock(&dev->mutex);
2498 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2499 dev_dbg(dev->class_dev, "not attached and not CAP_NET_ADMIN\n");
2503 if (dev->attached && dev->use_count == 0) {
2504 if (!try_module_get(dev->driver->module)) {
2509 rc = dev->open(dev);
2511 module_put(dev->driver->module);
2518 file->private_data = cfp;
2519 comedi_file_reset(file);
2523 mutex_unlock(&dev->mutex);
2525 comedi_dev_put(dev);
2531 static int comedi_fasync(int fd, struct file *file, int on)
2533 struct comedi_file *cfp = file->private_data;
2534 struct comedi_device *dev = cfp->dev;
2536 return fasync_helper(fd, file, on, &dev->async_queue);
2539 static int comedi_close(struct inode *inode, struct file *file)
2541 struct comedi_file *cfp = file->private_data;
2542 struct comedi_device *dev = cfp->dev;
2543 struct comedi_subdevice *s = NULL;
2546 mutex_lock(&dev->mutex);
2548 if (dev->subdevices) {
2549 for (i = 0; i < dev->n_subdevices; i++) {
2550 s = &dev->subdevices[i];
2552 if (s->busy == file)
2554 if (s->lock == file)
2558 if (dev->attached && dev->use_count == 1) {
2561 module_put(dev->driver->module);
2566 mutex_unlock(&dev->mutex);
2567 comedi_dev_put(dev);
2573 static const struct file_operations comedi_fops = {
2574 .owner = THIS_MODULE,
2575 .unlocked_ioctl = comedi_unlocked_ioctl,
2576 .compat_ioctl = comedi_compat_ioctl,
2577 .open = comedi_open,
2578 .release = comedi_close,
2579 .read = comedi_read,
2580 .write = comedi_write,
2581 .mmap = comedi_mmap,
2582 .poll = comedi_poll,
2583 .fasync = comedi_fasync,
2584 .llseek = noop_llseek,
2587 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2589 struct comedi_async *async = s->async;
2590 unsigned runflags = 0;
2591 unsigned runflags_mask = 0;
2593 if (!comedi_is_subdevice_running(s))
2596 if (s->async->events & COMEDI_CB_CANCEL_MASK)
2597 runflags_mask |= COMEDI_SRF_RUNNING;
2600 * Remember if an error event has occurred, so an error
2601 * can be returned the next time the user does a read().
2603 if (s->async->events & COMEDI_CB_ERROR_MASK) {
2604 runflags_mask |= COMEDI_SRF_ERROR;
2605 runflags |= COMEDI_SRF_ERROR;
2607 if (runflags_mask) {
2609 * Sets COMEDI_SRF_ERROR and COMEDI_SRF_RUNNING together
2612 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2615 if (async->cb_mask & s->async->events) {
2616 wake_up_interruptible(&async->wait_head);
2617 if (s->subdev_flags & SDF_CMD_READ)
2618 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2619 if (s->subdev_flags & SDF_CMD_WRITE)
2620 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2622 s->async->events = 0;
2624 EXPORT_SYMBOL_GPL(comedi_event);
2626 /* Note: the ->mutex is pre-locked on successful return */
2627 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2629 struct comedi_device *dev;
2630 struct device *csdev;
2633 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2635 return ERR_PTR(-ENOMEM);
2636 comedi_device_init(dev);
2637 comedi_set_hw_dev(dev, hardware_device);
2638 mutex_lock(&dev->mutex);
2639 mutex_lock(&comedi_board_minor_table_lock);
2640 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2641 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2642 if (comedi_board_minor_table[i] == NULL) {
2643 comedi_board_minor_table[i] = dev;
2647 mutex_unlock(&comedi_board_minor_table_lock);
2648 if (i == COMEDI_NUM_BOARD_MINORS) {
2649 mutex_unlock(&dev->mutex);
2650 comedi_device_cleanup(dev);
2651 comedi_dev_put(dev);
2652 pr_err("ran out of minor numbers for board device files\n");
2653 return ERR_PTR(-EBUSY);
2656 csdev = device_create(comedi_class, hardware_device,
2657 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2659 dev->class_dev = get_device(csdev);
2661 /* Note: dev->mutex needs to be unlocked by the caller. */
2665 static void comedi_free_board_minor(unsigned minor)
2667 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2668 comedi_free_board_dev(comedi_clear_board_minor(minor));
2671 void comedi_release_hardware_device(struct device *hardware_device)
2674 struct comedi_device *dev;
2676 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2678 mutex_lock(&comedi_board_minor_table_lock);
2679 dev = comedi_board_minor_table[minor];
2680 if (dev && dev->hw_dev == hardware_device) {
2681 comedi_board_minor_table[minor] = NULL;
2682 mutex_unlock(&comedi_board_minor_table_lock);
2683 comedi_free_board_dev(dev);
2686 mutex_unlock(&comedi_board_minor_table_lock);
2690 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2692 struct comedi_device *dev = s->device;
2693 struct device *csdev;
2696 mutex_lock(&comedi_subdevice_minor_table_lock);
2697 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2698 if (comedi_subdevice_minor_table[i] == NULL) {
2699 comedi_subdevice_minor_table[i] = s;
2703 mutex_unlock(&comedi_subdevice_minor_table_lock);
2704 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2705 pr_err("ran out of minor numbers for subdevice files\n");
2708 i += COMEDI_NUM_BOARD_MINORS;
2710 csdev = device_create(comedi_class, dev->class_dev,
2711 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2712 dev->minor, s->index);
2714 s->class_dev = csdev;
2719 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2728 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2729 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2731 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2732 mutex_lock(&comedi_subdevice_minor_table_lock);
2733 if (s == comedi_subdevice_minor_table[i])
2734 comedi_subdevice_minor_table[i] = NULL;
2735 mutex_unlock(&comedi_subdevice_minor_table_lock);
2737 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2738 s->class_dev = NULL;
2742 static void comedi_cleanup_board_minors(void)
2746 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2747 comedi_free_board_minor(i);
2750 static int __init comedi_init(void)
2755 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
2757 if (comedi_num_legacy_minors < 0 ||
2758 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2759 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2760 COMEDI_NUM_BOARD_MINORS);
2764 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2765 COMEDI_NUM_MINORS, "comedi");
2768 cdev_init(&comedi_cdev, &comedi_fops);
2769 comedi_cdev.owner = THIS_MODULE;
2771 retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
2773 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2778 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2779 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2783 comedi_class = class_create(THIS_MODULE, "comedi");
2784 if (IS_ERR(comedi_class)) {
2785 pr_err("failed to create class\n");
2786 cdev_del(&comedi_cdev);
2787 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2789 return PTR_ERR(comedi_class);
2792 comedi_class->dev_groups = comedi_dev_groups;
2794 /* XXX requires /proc interface */
2797 /* create devices files for legacy/manual use */
2798 for (i = 0; i < comedi_num_legacy_minors; i++) {
2799 struct comedi_device *dev;
2801 dev = comedi_alloc_board_minor(NULL);
2803 comedi_cleanup_board_minors();
2804 cdev_del(&comedi_cdev);
2805 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2807 return PTR_ERR(dev);
2809 /* comedi_alloc_board_minor() locked the mutex */
2810 mutex_unlock(&dev->mutex);
2815 module_init(comedi_init);
2817 static void __exit comedi_cleanup(void)
2821 comedi_cleanup_board_minors();
2822 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2823 BUG_ON(comedi_board_minor_table[i]);
2824 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2825 BUG_ON(comedi_subdevice_minor_table[i]);
2827 class_destroy(comedi_class);
2828 cdev_del(&comedi_cdev);
2829 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2831 comedi_proc_cleanup();
2833 module_exit(comedi_cleanup);
2835 MODULE_AUTHOR("http://www.comedi.org");
2836 MODULE_DESCRIPTION("Comedi core module");
2837 MODULE_LICENSE("GPL");