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.
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"
46 #define COMEDI_NUM_MINORS 0x100
47 #define COMEDI_NUM_SUBDEVICE_MINORS \
48 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
50 #ifdef CONFIG_COMEDI_DEBUG
52 EXPORT_SYMBOL_GPL(comedi_debug);
53 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
54 MODULE_PARM_DESC(comedi_debug,
55 "enable comedi core and driver debugging if non-zero (default 0)"
59 static int comedi_num_legacy_minors;
60 module_param(comedi_num_legacy_minors, int, S_IRUGO);
61 MODULE_PARM_DESC(comedi_num_legacy_minors,
62 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
65 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
66 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
67 MODULE_PARM_DESC(comedi_default_buf_size_kb,
68 "default asynchronous buffer size in KiB (default "
69 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
71 unsigned int comedi_default_buf_maxsize_kb
72 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
73 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
74 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
75 "default maximum size of asynchronous buffer in KiB (default "
76 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
78 static DEFINE_MUTEX(comedi_board_minor_table_lock);
79 static struct comedi_device
80 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
82 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
83 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
84 static struct comedi_subdevice
85 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
87 static struct class *comedi_class;
88 static struct cdev comedi_cdev;
90 static void comedi_device_init(struct comedi_device *dev)
92 kref_init(&dev->refcount);
93 spin_lock_init(&dev->spinlock);
94 mutex_init(&dev->mutex);
95 init_rwsem(&dev->attach_lock);
99 static void comedi_dev_kref_release(struct kref *kref)
101 struct comedi_device *dev =
102 container_of(kref, struct comedi_device, refcount);
104 mutex_destroy(&dev->mutex);
108 int comedi_dev_put(struct comedi_device *dev)
111 return kref_put(&dev->refcount, comedi_dev_kref_release);
114 EXPORT_SYMBOL_GPL(comedi_dev_put);
116 static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
119 kref_get(&dev->refcount);
123 static void comedi_device_cleanup(struct comedi_device *dev)
125 struct module *driver_module = NULL;
129 mutex_lock(&dev->mutex);
131 driver_module = dev->driver->module;
132 comedi_device_detach(dev);
133 while (dev->use_count > 0) {
135 module_put(driver_module);
136 module_put(THIS_MODULE);
139 mutex_unlock(&dev->mutex);
142 static bool comedi_clear_board_dev(struct comedi_device *dev)
144 unsigned int i = dev->minor;
145 bool cleared = false;
147 mutex_lock(&comedi_board_minor_table_lock);
148 if (dev == comedi_board_minor_table[i]) {
149 comedi_board_minor_table[i] = NULL;
152 mutex_unlock(&comedi_board_minor_table_lock);
156 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
158 struct comedi_device *dev;
160 mutex_lock(&comedi_board_minor_table_lock);
161 dev = comedi_board_minor_table[minor];
162 comedi_board_minor_table[minor] = NULL;
163 mutex_unlock(&comedi_board_minor_table_lock);
167 static void comedi_free_board_dev(struct comedi_device *dev)
170 if (dev->class_dev) {
171 device_destroy(comedi_class,
172 MKDEV(COMEDI_MAJOR, dev->minor));
174 comedi_device_cleanup(dev);
179 static struct comedi_subdevice
180 *comedi_subdevice_from_minor(unsigned minor)
182 struct comedi_subdevice *s;
183 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
185 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
186 mutex_lock(&comedi_subdevice_minor_table_lock);
187 s = comedi_subdevice_minor_table[i];
188 mutex_unlock(&comedi_subdevice_minor_table_lock);
192 static struct comedi_device *comedi_dev_from_board_minor(unsigned minor)
194 struct comedi_device *dev;
196 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
197 mutex_lock(&comedi_board_minor_table_lock);
198 dev = comedi_board_minor_table[minor];
199 mutex_unlock(&comedi_board_minor_table_lock);
203 static struct comedi_device *comedi_dev_from_subdevice_minor(unsigned minor)
205 struct comedi_subdevice *s;
207 s = comedi_subdevice_from_minor(minor);
208 return s ? s->device : NULL;
211 struct comedi_device *comedi_dev_from_minor(unsigned minor)
213 if (minor < COMEDI_NUM_BOARD_MINORS)
214 return comedi_dev_from_board_minor(minor);
216 return comedi_dev_from_subdevice_minor(minor);
218 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
220 static struct comedi_device *comedi_dev_get_from_board_minor(unsigned minor)
222 struct comedi_device *dev;
224 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
225 mutex_lock(&comedi_board_minor_table_lock);
226 dev = comedi_dev_get(comedi_board_minor_table[minor]);
227 mutex_unlock(&comedi_board_minor_table_lock);
231 static struct comedi_device *comedi_dev_get_from_subdevice_minor(unsigned minor)
233 struct comedi_device *dev;
234 struct comedi_subdevice *s;
235 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
237 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
238 mutex_lock(&comedi_subdevice_minor_table_lock);
239 s = comedi_subdevice_minor_table[i];
240 dev = comedi_dev_get(s ? s->device : NULL);
241 mutex_unlock(&comedi_subdevice_minor_table_lock);
245 struct comedi_device *comedi_dev_get_from_minor(unsigned minor)
247 if (minor < COMEDI_NUM_BOARD_MINORS)
248 return comedi_dev_get_from_board_minor(minor);
250 return comedi_dev_get_from_subdevice_minor(minor);
252 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
254 static struct comedi_subdevice *
255 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
257 struct comedi_subdevice *s;
259 if (minor >= COMEDI_NUM_BOARD_MINORS) {
260 s = comedi_subdevice_from_minor(minor);
261 if (!s || s->device != dev)
263 if (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(minor);
276 if (!s || s->device != dev)
278 if (s->subdev_flags & SDF_CMD_WRITE)
281 return dev->write_subdev;
284 static int resize_async_buffer(struct comedi_device *dev,
285 struct comedi_subdevice *s,
286 struct comedi_async *async, unsigned new_size)
290 if (new_size > async->max_bufsize)
294 DPRINTK("subdevice is busy, cannot resize buffer\n");
297 if (async->mmap_count) {
298 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
302 /* make sure buffer is an integral number of pages
304 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
306 retval = comedi_buf_alloc(dev, s, new_size);
311 retval = s->buf_change(dev, s, new_size);
316 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
317 dev->minor, s->index, async->prealloc_bufsz);
321 /* sysfs attribute files */
323 static ssize_t max_read_buffer_kb_show(struct device *csdev,
324 struct device_attribute *attr, char *buf)
326 unsigned int minor = MINOR(csdev->devt);
327 struct comedi_device *dev;
328 struct comedi_subdevice *s;
329 unsigned int size = 0;
331 dev = comedi_dev_from_minor(minor);
335 mutex_lock(&dev->mutex);
336 s = comedi_read_subdevice(dev, minor);
337 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
338 size = s->async->max_bufsize / 1024;
339 mutex_unlock(&dev->mutex);
341 return snprintf(buf, PAGE_SIZE, "%i\n", size);
344 static ssize_t max_read_buffer_kb_store(struct device *csdev,
345 struct device_attribute *attr,
346 const char *buf, size_t count)
348 unsigned int minor = MINOR(csdev->devt);
349 struct comedi_device *dev;
350 struct comedi_subdevice *s;
354 err = kstrtouint(buf, 10, &size);
357 if (size > (UINT_MAX / 1024))
361 dev = comedi_dev_from_minor(minor);
365 mutex_lock(&dev->mutex);
366 s = comedi_read_subdevice(dev, minor);
367 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
368 s->async->max_bufsize = size;
371 mutex_unlock(&dev->mutex);
373 return err ? err : count;
375 static DEVICE_ATTR_RW(max_read_buffer_kb);
377 static ssize_t read_buffer_kb_show(struct device *csdev,
378 struct device_attribute *attr, char *buf)
380 unsigned int minor = MINOR(csdev->devt);
381 struct comedi_device *dev;
382 struct comedi_subdevice *s;
383 unsigned int size = 0;
385 dev = comedi_dev_from_minor(minor);
389 mutex_lock(&dev->mutex);
390 s = comedi_read_subdevice(dev, minor);
391 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
392 size = s->async->prealloc_bufsz / 1024;
393 mutex_unlock(&dev->mutex);
395 return snprintf(buf, PAGE_SIZE, "%i\n", size);
398 static ssize_t read_buffer_kb_store(struct device *csdev,
399 struct device_attribute *attr,
400 const char *buf, size_t count)
402 unsigned int minor = MINOR(csdev->devt);
403 struct comedi_device *dev;
404 struct comedi_subdevice *s;
408 err = kstrtouint(buf, 10, &size);
411 if (size > (UINT_MAX / 1024))
415 dev = comedi_dev_from_minor(minor);
419 mutex_lock(&dev->mutex);
420 s = comedi_read_subdevice(dev, minor);
421 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
422 err = resize_async_buffer(dev, s, s->async, size);
425 mutex_unlock(&dev->mutex);
427 return err ? err : count;
429 static DEVICE_ATTR_RW(read_buffer_kb);
431 static ssize_t max_write_buffer_kb_show(struct device *csdev,
432 struct device_attribute *attr,
435 unsigned int minor = MINOR(csdev->devt);
436 struct comedi_device *dev;
437 struct comedi_subdevice *s;
438 unsigned int size = 0;
440 dev = comedi_dev_from_minor(minor);
444 mutex_lock(&dev->mutex);
445 s = comedi_write_subdevice(dev, minor);
446 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
447 size = s->async->max_bufsize / 1024;
448 mutex_unlock(&dev->mutex);
450 return snprintf(buf, PAGE_SIZE, "%i\n", size);
453 static ssize_t max_write_buffer_kb_store(struct device *csdev,
454 struct device_attribute *attr,
455 const char *buf, size_t count)
457 unsigned int minor = MINOR(csdev->devt);
458 struct comedi_device *dev;
459 struct comedi_subdevice *s;
463 err = kstrtouint(buf, 10, &size);
466 if (size > (UINT_MAX / 1024))
470 dev = comedi_dev_from_minor(minor);
474 mutex_lock(&dev->mutex);
475 s = comedi_write_subdevice(dev, minor);
476 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
477 s->async->max_bufsize = size;
480 mutex_unlock(&dev->mutex);
482 return err ? err : count;
484 static DEVICE_ATTR_RW(max_write_buffer_kb);
486 static ssize_t write_buffer_kb_show(struct device *csdev,
487 struct device_attribute *attr, char *buf)
489 unsigned int minor = MINOR(csdev->devt);
490 struct comedi_device *dev;
491 struct comedi_subdevice *s;
492 unsigned int size = 0;
494 dev = comedi_dev_from_minor(minor);
498 mutex_lock(&dev->mutex);
499 s = comedi_write_subdevice(dev, minor);
500 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
501 size = s->async->prealloc_bufsz / 1024;
502 mutex_unlock(&dev->mutex);
504 return snprintf(buf, PAGE_SIZE, "%i\n", size);
507 static ssize_t write_buffer_kb_store(struct device *csdev,
508 struct device_attribute *attr,
509 const char *buf, size_t count)
511 unsigned int minor = MINOR(csdev->devt);
512 struct comedi_device *dev;
513 struct comedi_subdevice *s;
517 err = kstrtouint(buf, 10, &size);
520 if (size > (UINT_MAX / 1024))
524 dev = comedi_dev_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 err = resize_async_buffer(dev, s, s->async, size);
534 mutex_unlock(&dev->mutex);
536 return err ? err : count;
538 static DEVICE_ATTR_RW(write_buffer_kb);
540 static struct attribute *comedi_dev_attrs[] = {
541 &dev_attr_max_read_buffer_kb.attr,
542 &dev_attr_read_buffer_kb.attr,
543 &dev_attr_max_write_buffer_kb.attr,
544 &dev_attr_write_buffer_kb.attr,
547 ATTRIBUTE_GROUPS(comedi_dev);
549 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
550 unsigned mask, unsigned bits)
554 spin_lock_irqsave(&s->spin_lock, flags);
555 s->runflags &= ~mask;
556 s->runflags |= (bits & mask);
557 spin_unlock_irqrestore(&s->spin_lock, flags);
560 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
565 spin_lock_irqsave(&s->spin_lock, flags);
566 runflags = s->runflags;
567 spin_unlock_irqrestore(&s->spin_lock, flags);
571 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
573 unsigned runflags = comedi_get_subdevice_runflags(s);
575 return (runflags & SRF_RUNNING) ? true : false;
577 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
579 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
581 unsigned runflags = comedi_get_subdevice_runflags(s);
583 return (runflags & SRF_ERROR) ? true : false;
586 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
588 unsigned runflags = comedi_get_subdevice_runflags(s);
590 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
594 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
595 * @s: comedi_subdevice struct
596 * @size: size of the memory to allocate
598 * This also sets the subdevice runflags to allow the core to automatically
599 * free the private data during the detach.
601 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
603 s->private = kzalloc(size, GFP_KERNEL);
605 s->runflags |= SRF_FREE_SPRIV;
608 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
611 This function restores a subdevice to an idle state.
613 static void do_become_nonbusy(struct comedi_device *dev,
614 struct comedi_subdevice *s)
616 struct comedi_async *async = s->async;
618 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
620 comedi_buf_reset(async);
621 async->inttrig = NULL;
622 kfree(async->cmd.chanlist);
623 async->cmd.chanlist = NULL;
625 wake_up_interruptible_all(&s->async->wait_head);
627 dev_err(dev->class_dev,
628 "BUG: (?) do_become_nonbusy called with async=NULL\n");
633 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
637 if (comedi_is_subdevice_running(s) && s->cancel)
638 ret = s->cancel(dev, s);
640 do_become_nonbusy(dev, s);
645 void comedi_device_cancel_all(struct comedi_device *dev)
647 struct comedi_subdevice *s;
653 for (i = 0; i < dev->n_subdevices; i++) {
654 s = &dev->subdevices[i];
660 static int is_device_busy(struct comedi_device *dev)
662 struct comedi_subdevice *s;
668 for (i = 0; i < dev->n_subdevices; i++) {
669 s = &dev->subdevices[i];
672 if (s->async && s->async->mmap_count)
684 pointer to devconfig structure
687 devconfig structure at arg
692 static int do_devconfig_ioctl(struct comedi_device *dev,
693 struct comedi_devconfig __user *arg)
695 struct comedi_devconfig it;
697 if (!capable(CAP_SYS_ADMIN))
701 if (is_device_busy(dev))
704 struct module *driver_module = dev->driver->module;
705 comedi_device_detach(dev);
706 module_put(driver_module);
711 if (copy_from_user(&it, arg, sizeof(it)))
714 it.board_name[COMEDI_NAMELEN - 1] = 0;
716 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
717 dev_warn(dev->class_dev,
718 "comedi_config --init_data is deprecated\n");
722 if (dev->minor >= comedi_num_legacy_minors)
723 /* don't re-use dynamically allocated comedi devices */
726 /* This increments the driver module count on success. */
727 return comedi_device_attach(dev, &it);
732 buffer configuration ioctl
735 pointer to bufconfig structure
741 modified bufconfig at arg
744 static int do_bufconfig_ioctl(struct comedi_device *dev,
745 struct comedi_bufconfig __user *arg)
747 struct comedi_bufconfig bc;
748 struct comedi_async *async;
749 struct comedi_subdevice *s;
752 if (copy_from_user(&bc, arg, sizeof(bc)))
755 if (bc.subdevice >= dev->n_subdevices)
758 s = &dev->subdevices[bc.subdevice];
762 DPRINTK("subdevice does not have async capability\n");
768 if (bc.maximum_size) {
769 if (!capable(CAP_SYS_ADMIN))
772 async->max_bufsize = bc.maximum_size;
776 retval = resize_async_buffer(dev, s, async, bc.size);
781 bc.size = async->prealloc_bufsz;
782 bc.maximum_size = async->max_bufsize;
785 if (copy_to_user(arg, &bc, sizeof(bc)))
796 pointer to devinfo structure
805 static int do_devinfo_ioctl(struct comedi_device *dev,
806 struct comedi_devinfo __user *arg,
809 const unsigned minor = iminor(file_inode(file));
810 struct comedi_subdevice *s;
811 struct comedi_devinfo devinfo;
813 memset(&devinfo, 0, sizeof(devinfo));
815 /* fill devinfo structure */
816 devinfo.version_code = COMEDI_VERSION_CODE;
817 devinfo.n_subdevs = dev->n_subdevices;
818 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
819 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
821 s = comedi_read_subdevice(dev, minor);
823 devinfo.read_subdevice = s->index;
825 devinfo.read_subdevice = -1;
827 s = comedi_write_subdevice(dev, minor);
829 devinfo.write_subdevice = s->index;
831 devinfo.write_subdevice = -1;
833 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
844 pointer to array of subdevice info structures
850 array of subdevice info structures at arg
853 static int do_subdinfo_ioctl(struct comedi_device *dev,
854 struct comedi_subdinfo __user *arg, void *file)
857 struct comedi_subdinfo *tmp, *us;
858 struct comedi_subdevice *s;
860 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
864 /* fill subdinfo structs */
865 for (i = 0; i < dev->n_subdevices; i++) {
866 s = &dev->subdevices[i];
870 us->n_chan = s->n_chan;
871 us->subd_flags = s->subdev_flags;
872 if (comedi_is_subdevice_running(s))
873 us->subd_flags |= SDF_RUNNING;
874 #define TIMER_nanosec 5 /* backwards compatibility */
875 us->timer_type = TIMER_nanosec;
876 us->len_chanlist = s->len_chanlist;
877 us->maxdata = s->maxdata;
878 if (s->range_table) {
880 (i << 24) | (0 << 16) | (s->range_table->length);
882 us->range_type = 0; /* XXX */
886 us->subd_flags |= SDF_BUSY;
888 us->subd_flags |= SDF_BUSY_OWNER;
890 us->subd_flags |= SDF_LOCKED;
892 us->subd_flags |= SDF_LOCK_OWNER;
893 if (!s->maxdata && s->maxdata_list)
894 us->subd_flags |= SDF_MAXDATA;
895 if (s->range_table_list)
896 us->subd_flags |= SDF_RANGETYPE;
898 us->subd_flags |= SDF_CMD;
900 if (s->insn_bits != &insn_inval)
901 us->insn_bits_support = COMEDI_SUPPORTED;
903 us->insn_bits_support = COMEDI_UNSUPPORTED;
906 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
910 return ret ? -EFAULT : 0;
918 pointer to chaninfo structure
921 chaninfo structure at arg
924 arrays at elements of chaninfo structure
927 static int do_chaninfo_ioctl(struct comedi_device *dev,
928 struct comedi_chaninfo __user *arg)
930 struct comedi_subdevice *s;
931 struct comedi_chaninfo it;
933 if (copy_from_user(&it, arg, sizeof(it)))
936 if (it.subdev >= dev->n_subdevices)
938 s = &dev->subdevices[it.subdev];
940 if (it.maxdata_list) {
941 if (s->maxdata || !s->maxdata_list)
943 if (copy_to_user(it.maxdata_list, s->maxdata_list,
944 s->n_chan * sizeof(unsigned int)))
949 return -EINVAL; /* flaglist not supported */
954 if (!s->range_table_list)
956 for (i = 0; i < s->n_chan; i++) {
959 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
960 (s->range_table_list[i]->length);
961 if (put_user(x, it.rangelist + i))
965 if (copy_to_user(it.rangelist, s->range_type_list,
966 s->n_chan * sizeof(unsigned int)))
976 buffer information ioctl
979 pointer to bufinfo structure
985 modified bufinfo at arg
988 static int do_bufinfo_ioctl(struct comedi_device *dev,
989 struct comedi_bufinfo __user *arg, void *file)
991 struct comedi_bufinfo bi;
992 struct comedi_subdevice *s;
993 struct comedi_async *async;
995 if (copy_from_user(&bi, arg, sizeof(bi)))
998 if (bi.subdevice >= dev->n_subdevices)
1001 s = &dev->subdevices[bi.subdevice];
1003 if (s->lock && s->lock != file)
1009 DPRINTK("subdevice does not have async capability\n");
1010 bi.buf_write_ptr = 0;
1011 bi.buf_read_ptr = 0;
1012 bi.buf_write_count = 0;
1013 bi.buf_read_count = 0;
1015 bi.bytes_written = 0;
1020 bi.bytes_written = 0;
1021 goto copyback_position;
1023 if (s->busy != file)
1026 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
1027 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
1028 comedi_buf_read_free(async, bi.bytes_read);
1030 if (comedi_is_subdevice_idle(s) &&
1031 async->buf_write_count == async->buf_read_count) {
1032 do_become_nonbusy(dev, s);
1036 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
1038 comedi_buf_write_alloc(async, bi.bytes_written);
1039 comedi_buf_write_free(async, bi.bytes_written);
1043 bi.buf_write_count = async->buf_write_count;
1044 bi.buf_write_ptr = async->buf_write_ptr;
1045 bi.buf_read_count = async->buf_read_count;
1046 bi.buf_read_ptr = async->buf_read_ptr;
1049 if (copy_to_user(arg, &bi, sizeof(bi)))
1055 static int check_insn_config_length(struct comedi_insn *insn,
1062 case INSN_CONFIG_DIO_OUTPUT:
1063 case INSN_CONFIG_DIO_INPUT:
1064 case INSN_CONFIG_DISARM:
1065 case INSN_CONFIG_RESET:
1069 case INSN_CONFIG_ARM:
1070 case INSN_CONFIG_DIO_QUERY:
1071 case INSN_CONFIG_BLOCK_SIZE:
1072 case INSN_CONFIG_FILTER:
1073 case INSN_CONFIG_SERIAL_CLOCK:
1074 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1075 case INSN_CONFIG_ALT_SOURCE:
1076 case INSN_CONFIG_SET_COUNTER_MODE:
1077 case INSN_CONFIG_8254_READ_STATUS:
1078 case INSN_CONFIG_SET_ROUTING:
1079 case INSN_CONFIG_GET_ROUTING:
1080 case INSN_CONFIG_GET_PWM_STATUS:
1081 case INSN_CONFIG_PWM_SET_PERIOD:
1082 case INSN_CONFIG_PWM_GET_PERIOD:
1086 case INSN_CONFIG_SET_GATE_SRC:
1087 case INSN_CONFIG_GET_GATE_SRC:
1088 case INSN_CONFIG_SET_CLOCK_SRC:
1089 case INSN_CONFIG_GET_CLOCK_SRC:
1090 case INSN_CONFIG_SET_OTHER_SRC:
1091 case INSN_CONFIG_GET_COUNTER_STATUS:
1092 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1093 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1094 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1098 case INSN_CONFIG_PWM_OUTPUT:
1099 case INSN_CONFIG_ANALOG_TRIG:
1103 case INSN_CONFIG_DIGITAL_TRIG:
1107 /* by default we allow the insn since we don't have checks for
1108 * all possible cases yet */
1110 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1112 pr_warn("comedi: Add a check to %s in %s.\n",
1113 __func__, __FILE__);
1114 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1120 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1121 unsigned int *data, void *file)
1123 struct comedi_subdevice *s;
1127 if (insn->insn & INSN_MASK_SPECIAL) {
1128 /* a non-subdevice instruction */
1130 switch (insn->insn) {
1140 do_gettimeofday(&tv);
1141 data[0] = tv.tv_sec;
1142 data[1] = tv.tv_usec;
1148 if (insn->n != 1 || data[0] >= 100000) {
1152 udelay(data[0] / 1000);
1160 if (insn->subdev >= dev->n_subdevices) {
1161 DPRINTK("%d not usable subdevice\n",
1166 s = &dev->subdevices[insn->subdev];
1168 DPRINTK("no async\n");
1172 if (!s->async->inttrig) {
1173 DPRINTK("no inttrig\n");
1177 ret = s->async->inttrig(dev, s, data[0]);
1182 DPRINTK("invalid insn\n");
1187 /* a subdevice instruction */
1188 unsigned int maxdata;
1190 if (insn->subdev >= dev->n_subdevices) {
1191 DPRINTK("subdevice %d out of range\n", insn->subdev);
1195 s = &dev->subdevices[insn->subdev];
1197 if (s->type == COMEDI_SUBD_UNUSED) {
1198 DPRINTK("%d not usable subdevice\n", insn->subdev);
1203 /* are we locked? (ioctl lock) */
1204 if (s->lock && s->lock != file) {
1205 DPRINTK("device locked\n");
1210 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1213 DPRINTK("bad chanspec\n");
1221 /* This looks arbitrary. It is. */
1222 s->busy = &parse_insn;
1223 switch (insn->insn) {
1225 ret = s->insn_read(dev, s, insn, data);
1228 maxdata = s->maxdata_list
1229 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1231 for (i = 0; i < insn->n; ++i) {
1232 if (data[i] > maxdata) {
1234 DPRINTK("bad data value(s)\n");
1239 ret = s->insn_write(dev, s, insn, data);
1245 /* Most drivers ignore the base channel in
1246 * insn->chanspec. Fix this here if
1247 * the subdevice has <= 32 channels. */
1249 unsigned int orig_mask;
1251 orig_mask = data[0];
1252 if (s->n_chan <= 32) {
1253 shift = CR_CHAN(insn->chanspec);
1261 ret = s->insn_bits(dev, s, insn, data);
1262 data[0] = orig_mask;
1268 ret = check_insn_config_length(insn, data);
1271 ret = s->insn_config(dev, s, insn, data);
1287 * synchronous instructions
1290 * pointer to sync cmd structure
1293 * sync cmd struct at arg
1300 /* arbitrary limits */
1301 #define MAX_SAMPLES 256
1302 static int do_insnlist_ioctl(struct comedi_device *dev,
1303 struct comedi_insnlist __user *arg, void *file)
1305 struct comedi_insnlist insnlist;
1306 struct comedi_insn *insns = NULL;
1307 unsigned int *data = NULL;
1311 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1314 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1316 DPRINTK("kmalloc failed\n");
1321 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1323 DPRINTK("kmalloc failed\n");
1328 if (copy_from_user(insns, insnlist.insns,
1329 sizeof(*insns) * insnlist.n_insns)) {
1330 DPRINTK("copy_from_user failed\n");
1335 for (i = 0; i < insnlist.n_insns; i++) {
1336 if (insns[i].n > MAX_SAMPLES) {
1337 DPRINTK("number of samples too large\n");
1341 if (insns[i].insn & INSN_MASK_WRITE) {
1342 if (copy_from_user(data, insns[i].data,
1343 insns[i].n * sizeof(unsigned int))) {
1344 DPRINTK("copy_from_user failed\n");
1349 ret = parse_insn(dev, insns + i, data, file);
1352 if (insns[i].insn & INSN_MASK_READ) {
1353 if (copy_to_user(insns[i].data, data,
1354 insns[i].n * sizeof(unsigned int))) {
1355 DPRINTK("copy_to_user failed\n");
1375 * synchronous instructions
1381 * struct comedi_insn struct at arg
1387 static int do_insn_ioctl(struct comedi_device *dev,
1388 struct comedi_insn __user *arg, void *file)
1390 struct comedi_insn insn;
1391 unsigned int *data = NULL;
1394 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1400 if (copy_from_user(&insn, arg, sizeof(insn))) {
1405 /* This is where the behavior of insn and insnlist deviate. */
1406 if (insn.n > MAX_SAMPLES)
1407 insn.n = MAX_SAMPLES;
1408 if (insn.insn & INSN_MASK_WRITE) {
1409 if (copy_from_user(data,
1411 insn.n * sizeof(unsigned int))) {
1416 ret = parse_insn(dev, &insn, data, file);
1419 if (insn.insn & INSN_MASK_READ) {
1420 if (copy_to_user(insn.data,
1422 insn.n * sizeof(unsigned int))) {
1435 static int do_cmd_ioctl(struct comedi_device *dev,
1436 struct comedi_cmd __user *arg, void *file)
1438 struct comedi_cmd cmd;
1439 struct comedi_subdevice *s;
1440 struct comedi_async *async;
1442 unsigned int __user *user_chanlist;
1444 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1445 DPRINTK("bad cmd address\n");
1448 /* save user's chanlist pointer so it can be restored later */
1449 user_chanlist = (unsigned int __user *)cmd.chanlist;
1451 if (cmd.subdev >= dev->n_subdevices) {
1452 DPRINTK("%d no such subdevice\n", cmd.subdev);
1456 s = &dev->subdevices[cmd.subdev];
1459 if (s->type == COMEDI_SUBD_UNUSED) {
1460 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1464 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1465 DPRINTK("subdevice %i does not support commands\n",
1470 /* are we locked? (ioctl lock) */
1471 if (s->lock && s->lock != file) {
1472 DPRINTK("subdevice locked\n");
1478 DPRINTK("subdevice busy\n");
1482 /* make sure channel/gain list isn't too long */
1483 if (cmd.chanlist_len > s->len_chanlist) {
1484 DPRINTK("channel/gain list too long %u > %d\n",
1485 cmd.chanlist_len, s->len_chanlist);
1489 /* make sure channel/gain list isn't too short */
1490 if (cmd.chanlist_len < 1) {
1491 DPRINTK("channel/gain list too short %u < 1\n",
1497 async->cmd.data = NULL;
1498 /* load channel/gain list */
1499 async->cmd.chanlist = memdup_user(user_chanlist,
1500 async->cmd.chanlist_len * sizeof(int));
1501 if (IS_ERR(async->cmd.chanlist)) {
1502 ret = PTR_ERR(async->cmd.chanlist);
1503 DPRINTK("memdup_user failed with code %d\n", ret);
1507 /* make sure each element in channel/gain list is valid */
1508 ret = comedi_check_chanlist(s,
1509 async->cmd.chanlist_len,
1510 async->cmd.chanlist);
1512 DPRINTK("bad chanlist\n");
1516 ret = s->do_cmdtest(dev, s, &async->cmd);
1518 if (async->cmd.flags & TRIG_BOGUS || ret) {
1519 DPRINTK("test returned %d\n", ret);
1521 /* restore chanlist pointer before copying back */
1522 cmd.chanlist = (unsigned int __force *)user_chanlist;
1524 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1525 DPRINTK("fault writing cmd\n");
1533 if (!async->prealloc_bufsz) {
1535 DPRINTK("no buffer (?)\n");
1539 comedi_buf_reset(async);
1542 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1544 if (async->cmd.flags & TRIG_WAKE_EOS)
1545 async->cb_mask |= COMEDI_CB_EOS;
1547 comedi_set_subdevice_runflags(s, SRF_ERROR | SRF_RUNNING, SRF_RUNNING);
1549 /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
1550 * comedi_read() or comedi_write() */
1552 ret = s->do_cmd(dev, s);
1557 do_become_nonbusy(dev, s);
1564 command testing ioctl
1567 pointer to cmd structure
1570 cmd structure at arg
1574 modified cmd structure at arg
1577 static int do_cmdtest_ioctl(struct comedi_device *dev,
1578 struct comedi_cmd __user *arg, void *file)
1580 struct comedi_cmd cmd;
1581 struct comedi_subdevice *s;
1583 unsigned int *chanlist = NULL;
1584 unsigned int __user *user_chanlist;
1586 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1587 DPRINTK("bad cmd address\n");
1590 /* save user's chanlist pointer so it can be restored later */
1591 user_chanlist = (unsigned int __user *)cmd.chanlist;
1593 if (cmd.subdev >= dev->n_subdevices) {
1594 DPRINTK("%d no such subdevice\n", cmd.subdev);
1598 s = &dev->subdevices[cmd.subdev];
1599 if (s->type == COMEDI_SUBD_UNUSED) {
1600 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1604 if (!s->do_cmd || !s->do_cmdtest) {
1605 DPRINTK("subdevice %i does not support commands\n",
1610 /* make sure channel/gain list isn't too long */
1611 if (cmd.chanlist_len > s->len_chanlist) {
1612 DPRINTK("channel/gain list too long %d > %d\n",
1613 cmd.chanlist_len, s->len_chanlist);
1618 /* load channel/gain list */
1620 chanlist = memdup_user(user_chanlist,
1621 cmd.chanlist_len * sizeof(int));
1622 if (IS_ERR(chanlist)) {
1623 ret = PTR_ERR(chanlist);
1624 DPRINTK("memdup_user exited with code %d", ret);
1628 /* make sure each element in channel/gain list is valid */
1629 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1631 DPRINTK("bad chanlist\n");
1635 cmd.chanlist = chanlist;
1638 ret = s->do_cmdtest(dev, s, &cmd);
1640 /* restore chanlist pointer before copying back */
1641 cmd.chanlist = (unsigned int __force *)user_chanlist;
1643 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1644 DPRINTK("bad cmd address\n");
1669 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1673 unsigned long flags;
1674 struct comedi_subdevice *s;
1676 if (arg >= dev->n_subdevices)
1678 s = &dev->subdevices[arg];
1680 spin_lock_irqsave(&s->spin_lock, flags);
1681 if (s->busy || s->lock)
1685 spin_unlock_irqrestore(&s->spin_lock, flags);
1692 ret = s->lock_f(dev, s);
1711 This function isn't protected by the semaphore, since
1712 we already own the lock.
1714 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1717 struct comedi_subdevice *s;
1719 if (arg >= dev->n_subdevices)
1721 s = &dev->subdevices[arg];
1726 if (s->lock && s->lock != file)
1729 if (s->lock == file) {
1743 cancel acquisition ioctl
1755 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1758 struct comedi_subdevice *s;
1761 if (arg >= dev->n_subdevices)
1763 s = &dev->subdevices[arg];
1764 if (s->async == NULL)
1767 if (s->lock && s->lock != file)
1773 if (s->busy != file)
1776 ret = do_cancel(dev, s);
1783 instructs driver to synchronize buffers
1795 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1798 struct comedi_subdevice *s;
1800 if (arg >= dev->n_subdevices)
1802 s = &dev->subdevices[arg];
1804 if (s->lock && s->lock != file)
1810 if (s->busy != file)
1814 return s->poll(dev, s);
1819 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1822 const unsigned minor = iminor(file_inode(file));
1823 struct comedi_device *dev = comedi_dev_from_minor(minor);
1829 mutex_lock(&dev->mutex);
1831 /* Device config is special, because it must work on
1832 * an unconfigured device. */
1833 if (cmd == COMEDI_DEVCONFIG) {
1834 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1835 /* Device config not appropriate on non-board minors. */
1839 rc = do_devconfig_ioctl(dev,
1840 (struct comedi_devconfig __user *)arg);
1843 dev->minor >= comedi_num_legacy_minors) {
1844 /* Successfully unconfigured a dynamically
1845 * allocated device. Try and remove it. */
1846 if (comedi_clear_board_dev(dev)) {
1847 mutex_unlock(&dev->mutex);
1848 comedi_free_board_dev(dev);
1856 if (!dev->attached) {
1857 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1863 case COMEDI_BUFCONFIG:
1864 rc = do_bufconfig_ioctl(dev,
1865 (struct comedi_bufconfig __user *)arg);
1867 case COMEDI_DEVINFO:
1868 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1871 case COMEDI_SUBDINFO:
1872 rc = do_subdinfo_ioctl(dev,
1873 (struct comedi_subdinfo __user *)arg,
1876 case COMEDI_CHANINFO:
1877 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1879 case COMEDI_RANGEINFO:
1880 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1882 case COMEDI_BUFINFO:
1883 rc = do_bufinfo_ioctl(dev,
1884 (struct comedi_bufinfo __user *)arg,
1888 rc = do_lock_ioctl(dev, arg, file);
1891 rc = do_unlock_ioctl(dev, arg, file);
1894 rc = do_cancel_ioctl(dev, arg, file);
1897 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1899 case COMEDI_CMDTEST:
1900 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1903 case COMEDI_INSNLIST:
1904 rc = do_insnlist_ioctl(dev,
1905 (struct comedi_insnlist __user *)arg,
1909 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1913 rc = do_poll_ioctl(dev, arg, file);
1921 mutex_unlock(&dev->mutex);
1925 static void comedi_vm_open(struct vm_area_struct *area)
1927 struct comedi_async *async;
1928 struct comedi_device *dev;
1930 async = area->vm_private_data;
1931 dev = async->subdevice->device;
1933 mutex_lock(&dev->mutex);
1934 async->mmap_count++;
1935 mutex_unlock(&dev->mutex);
1938 static void comedi_vm_close(struct vm_area_struct *area)
1940 struct comedi_async *async;
1941 struct comedi_device *dev;
1943 async = area->vm_private_data;
1944 dev = async->subdevice->device;
1946 mutex_lock(&dev->mutex);
1947 async->mmap_count--;
1948 mutex_unlock(&dev->mutex);
1951 static struct vm_operations_struct comedi_vm_ops = {
1952 .open = comedi_vm_open,
1953 .close = comedi_vm_close,
1956 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1958 const unsigned minor = iminor(file_inode(file));
1959 struct comedi_device *dev = comedi_dev_from_minor(minor);
1960 struct comedi_subdevice *s;
1961 struct comedi_async *async;
1962 unsigned long start = vma->vm_start;
1971 mutex_lock(&dev->mutex);
1973 if (!dev->attached) {
1974 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1979 if (vma->vm_flags & VM_WRITE)
1980 s = comedi_write_subdevice(dev, minor);
1982 s = comedi_read_subdevice(dev, minor);
1994 if (vma->vm_pgoff != 0) {
1995 DPRINTK("comedi: mmap() offset must be 0.\n");
2000 size = vma->vm_end - vma->vm_start;
2001 if (size > async->prealloc_bufsz) {
2005 if (size & (~PAGE_MASK)) {
2010 n_pages = size >> PAGE_SHIFT;
2011 for (i = 0; i < n_pages; ++i) {
2012 struct comedi_buf_page *buf = &async->buf_page_list[i];
2014 if (remap_pfn_range(vma, start,
2015 page_to_pfn(virt_to_page(buf->virt_addr)),
2016 PAGE_SIZE, PAGE_SHARED)) {
2023 vma->vm_ops = &comedi_vm_ops;
2024 vma->vm_private_data = async;
2026 async->mmap_count++;
2030 mutex_unlock(&dev->mutex);
2034 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2036 unsigned int mask = 0;
2037 const unsigned minor = iminor(file_inode(file));
2038 struct comedi_device *dev = comedi_dev_from_minor(minor);
2039 struct comedi_subdevice *s;
2044 mutex_lock(&dev->mutex);
2046 if (!dev->attached) {
2047 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2051 s = comedi_read_subdevice(dev, minor);
2052 if (s && s->async) {
2053 poll_wait(file, &s->async->wait_head, wait);
2054 if (!s->busy || !comedi_is_subdevice_running(s) ||
2055 comedi_buf_read_n_available(s->async) > 0)
2056 mask |= POLLIN | POLLRDNORM;
2059 s = comedi_write_subdevice(dev, minor);
2060 if (s && s->async) {
2061 unsigned int bps = bytes_per_sample(s->async->subdevice);
2063 poll_wait(file, &s->async->wait_head, wait);
2064 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2065 if (!s->busy || !comedi_is_subdevice_running(s) ||
2066 comedi_buf_write_n_allocated(s->async) >= bps)
2067 mask |= POLLOUT | POLLWRNORM;
2071 mutex_unlock(&dev->mutex);
2075 static ssize_t comedi_write(struct file *file, const char __user *buf,
2076 size_t nbytes, loff_t *offset)
2078 struct comedi_subdevice *s;
2079 struct comedi_async *async;
2080 int n, m, count = 0, retval = 0;
2081 DECLARE_WAITQUEUE(wait, current);
2082 const unsigned minor = iminor(file_inode(file));
2083 struct comedi_device *dev = comedi_dev_from_minor(minor);
2084 bool on_wait_queue = false;
2086 unsigned int old_detach_count;
2091 /* Protect against device detachment during operation. */
2092 down_read(&dev->attach_lock);
2093 attach_locked = true;
2094 old_detach_count = dev->detach_count;
2096 if (!dev->attached) {
2097 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2102 s = comedi_write_subdevice(dev, minor);
2103 if (!s || !s->async) {
2110 if (!s->busy || !nbytes)
2112 if (s->busy != file) {
2117 add_wait_queue(&async->wait_head, &wait);
2118 on_wait_queue = true;
2119 while (nbytes > 0 && !retval) {
2120 set_current_state(TASK_INTERRUPTIBLE);
2122 if (!comedi_is_subdevice_running(s)) {
2124 struct comedi_subdevice *new_s;
2126 if (comedi_is_subdevice_in_error(s))
2131 * To avoid deadlock, cannot acquire dev->mutex
2132 * while dev->attach_lock is held. Need to
2133 * remove task from the async wait queue before
2134 * releasing dev->attach_lock, as it might not
2135 * be valid afterwards.
2137 remove_wait_queue(&async->wait_head, &wait);
2138 on_wait_queue = false;
2139 up_read(&dev->attach_lock);
2140 attach_locked = false;
2141 mutex_lock(&dev->mutex);
2143 * Become non-busy unless things have changed
2144 * behind our back. Checking dev->detach_count
2145 * is unchanged ought to be sufficient (unless
2146 * there have been 2**32 detaches in the
2147 * meantime!), but check the subdevice pointer
2148 * as well just in case.
2150 new_s = comedi_write_subdevice(dev, minor);
2151 if (dev->attached &&
2152 old_detach_count == dev->detach_count &&
2153 s == new_s && new_s->async == async)
2154 do_become_nonbusy(dev, s);
2155 mutex_unlock(&dev->mutex);
2163 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2164 m = async->prealloc_bufsz - async->buf_write_ptr;
2165 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2166 if (m > comedi_buf_write_n_allocated(async))
2167 m = comedi_buf_write_n_allocated(async);
2172 if (file->f_flags & O_NONBLOCK) {
2177 if (signal_pending(current)) {
2178 retval = -ERESTARTSYS;
2183 if (s->busy != file) {
2190 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2196 comedi_buf_write_free(async, n);
2202 break; /* makes device work like a pipe */
2206 remove_wait_queue(&async->wait_head, &wait);
2207 set_current_state(TASK_RUNNING);
2209 up_read(&dev->attach_lock);
2211 return count ? count : retval;
2214 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2217 struct comedi_subdevice *s;
2218 struct comedi_async *async;
2219 int n, m, count = 0, retval = 0;
2220 DECLARE_WAITQUEUE(wait, current);
2221 const unsigned minor = iminor(file_inode(file));
2222 struct comedi_device *dev = comedi_dev_from_minor(minor);
2223 unsigned int old_detach_count;
2224 bool become_nonbusy = false;
2230 /* Protect against device detachment during operation. */
2231 down_read(&dev->attach_lock);
2232 attach_locked = true;
2233 old_detach_count = dev->detach_count;
2235 if (!dev->attached) {
2236 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2241 s = comedi_read_subdevice(dev, minor);
2242 if (!s || !s->async) {
2248 if (!s->busy || !nbytes)
2250 if (s->busy != file) {
2255 add_wait_queue(&async->wait_head, &wait);
2256 while (nbytes > 0 && !retval) {
2257 set_current_state(TASK_INTERRUPTIBLE);
2261 m = comedi_buf_read_n_available(async);
2262 /* printk("%d available\n",m); */
2263 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2264 m = async->prealloc_bufsz - async->buf_read_ptr;
2265 /* printk("%d contiguous\n",m); */
2270 if (!comedi_is_subdevice_running(s)) {
2271 if (comedi_is_subdevice_in_error(s))
2275 become_nonbusy = true;
2278 if (file->f_flags & O_NONBLOCK) {
2283 if (signal_pending(current)) {
2284 retval = -ERESTARTSYS;
2291 if (s->busy != file) {
2297 m = copy_to_user(buf, async->prealloc_buf +
2298 async->buf_read_ptr, n);
2304 comedi_buf_read_alloc(async, n);
2305 comedi_buf_read_free(async, n);
2311 break; /* makes device work like a pipe */
2313 remove_wait_queue(&async->wait_head, &wait);
2314 set_current_state(TASK_RUNNING);
2315 if (become_nonbusy || comedi_is_subdevice_idle(s)) {
2316 struct comedi_subdevice *new_s;
2319 * To avoid deadlock, cannot acquire dev->mutex
2320 * while dev->attach_lock is held.
2322 up_read(&dev->attach_lock);
2323 attach_locked = false;
2324 mutex_lock(&dev->mutex);
2326 * Check device hasn't become detached behind our back.
2327 * Checking dev->detach_count is unchanged ought to be
2328 * sufficient (unless there have been 2**32 detaches in the
2329 * meantime!), but check the subdevice pointer as well just in
2332 new_s = comedi_read_subdevice(dev, minor);
2333 if (dev->attached && old_detach_count == dev->detach_count &&
2334 s == new_s && new_s->async == async) {
2335 if (become_nonbusy ||
2336 async->buf_read_count - async->buf_write_count == 0)
2337 do_become_nonbusy(dev, s);
2339 mutex_unlock(&dev->mutex);
2343 up_read(&dev->attach_lock);
2345 return count ? count : retval;
2348 static int comedi_open(struct inode *inode, struct file *file)
2350 const unsigned minor = iminor(inode);
2351 struct comedi_device *dev = comedi_dev_from_minor(minor);
2354 DPRINTK("invalid minor number\n");
2358 /* This is slightly hacky, but we want module autoloading
2360 * case: user opens device, attached -> ok
2361 * case: user opens device, unattached, !in_request_module -> autoload
2362 * case: user opens device, unattached, in_request_module -> fail
2363 * case: root opens device, attached -> ok
2364 * case: root opens device, unattached, in_request_module -> ok
2365 * (typically called from modprobe)
2366 * case: root opens device, unattached, !in_request_module -> autoload
2368 * The last could be changed to "-> ok", which would deny root
2371 mutex_lock(&dev->mutex);
2374 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2375 DPRINTK("in request module\n");
2376 mutex_unlock(&dev->mutex);
2379 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2382 dev->in_request_module = true;
2385 mutex_unlock(&dev->mutex);
2386 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2387 mutex_lock(&dev->mutex);
2390 dev->in_request_module = false;
2392 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2393 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2394 mutex_unlock(&dev->mutex);
2398 __module_get(THIS_MODULE);
2400 if (dev->attached) {
2401 if (!try_module_get(dev->driver->module)) {
2402 module_put(THIS_MODULE);
2403 mutex_unlock(&dev->mutex);
2408 if (dev->attached && dev->use_count == 0 && dev->open) {
2409 int rc = dev->open(dev);
2411 module_put(dev->driver->module);
2412 module_put(THIS_MODULE);
2413 mutex_unlock(&dev->mutex);
2420 mutex_unlock(&dev->mutex);
2425 static int comedi_fasync(int fd, struct file *file, int on)
2427 const unsigned minor = iminor(file_inode(file));
2428 struct comedi_device *dev = comedi_dev_from_minor(minor);
2433 return fasync_helper(fd, file, on, &dev->async_queue);
2436 static int comedi_close(struct inode *inode, struct file *file)
2438 const unsigned minor = iminor(inode);
2439 struct comedi_device *dev = comedi_dev_from_minor(minor);
2440 struct comedi_subdevice *s = NULL;
2446 mutex_lock(&dev->mutex);
2448 if (dev->subdevices) {
2449 for (i = 0; i < dev->n_subdevices; i++) {
2450 s = &dev->subdevices[i];
2452 if (s->busy == file)
2454 if (s->lock == file)
2458 if (dev->attached && dev->use_count == 1 && dev->close)
2461 module_put(THIS_MODULE);
2463 module_put(dev->driver->module);
2467 mutex_unlock(&dev->mutex);
2472 static const struct file_operations comedi_fops = {
2473 .owner = THIS_MODULE,
2474 .unlocked_ioctl = comedi_unlocked_ioctl,
2475 .compat_ioctl = comedi_compat_ioctl,
2476 .open = comedi_open,
2477 .release = comedi_close,
2478 .read = comedi_read,
2479 .write = comedi_write,
2480 .mmap = comedi_mmap,
2481 .poll = comedi_poll,
2482 .fasync = comedi_fasync,
2483 .llseek = noop_llseek,
2486 void comedi_error(const struct comedi_device *dev, const char *s)
2488 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2490 EXPORT_SYMBOL_GPL(comedi_error);
2492 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2494 struct comedi_async *async = s->async;
2495 unsigned runflags = 0;
2496 unsigned runflags_mask = 0;
2498 /* DPRINTK("comedi_event 0x%x\n",mask); */
2500 if (!comedi_is_subdevice_running(s))
2504 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2505 COMEDI_CB_OVERFLOW)) {
2506 runflags_mask |= SRF_RUNNING;
2508 /* remember if an error event has occurred, so an error
2509 * can be returned the next time the user does a read() */
2510 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2511 runflags_mask |= SRF_ERROR;
2512 runflags |= SRF_ERROR;
2514 if (runflags_mask) {
2515 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2516 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2519 if (async->cb_mask & s->async->events) {
2520 wake_up_interruptible(&async->wait_head);
2521 if (s->subdev_flags & SDF_CMD_READ)
2522 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2523 if (s->subdev_flags & SDF_CMD_WRITE)
2524 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2526 s->async->events = 0;
2528 EXPORT_SYMBOL_GPL(comedi_event);
2530 /* Note: the ->mutex is pre-locked on successful return */
2531 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2533 struct comedi_device *dev;
2534 struct device *csdev;
2537 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2539 return ERR_PTR(-ENOMEM);
2540 comedi_device_init(dev);
2541 comedi_set_hw_dev(dev, hardware_device);
2542 mutex_lock(&dev->mutex);
2543 mutex_lock(&comedi_board_minor_table_lock);
2544 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2545 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2546 if (comedi_board_minor_table[i] == NULL) {
2547 comedi_board_minor_table[i] = dev;
2551 mutex_unlock(&comedi_board_minor_table_lock);
2552 if (i == COMEDI_NUM_BOARD_MINORS) {
2553 mutex_unlock(&dev->mutex);
2554 comedi_device_cleanup(dev);
2555 comedi_dev_put(dev);
2556 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2557 return ERR_PTR(-EBUSY);
2560 csdev = device_create(comedi_class, hardware_device,
2561 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2563 dev->class_dev = csdev;
2565 /* Note: dev->mutex needs to be unlocked by the caller. */
2569 static void comedi_free_board_minor(unsigned minor)
2571 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2572 comedi_free_board_dev(comedi_clear_board_minor(minor));
2575 void comedi_release_hardware_device(struct device *hardware_device)
2578 struct comedi_device *dev;
2580 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2582 mutex_lock(&comedi_board_minor_table_lock);
2583 dev = comedi_board_minor_table[minor];
2584 if (dev && dev->hw_dev == hardware_device) {
2585 comedi_board_minor_table[minor] = NULL;
2586 mutex_unlock(&comedi_board_minor_table_lock);
2587 comedi_free_board_dev(dev);
2590 mutex_unlock(&comedi_board_minor_table_lock);
2594 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2596 struct comedi_device *dev = s->device;
2597 struct device *csdev;
2600 mutex_lock(&comedi_subdevice_minor_table_lock);
2601 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2602 if (comedi_subdevice_minor_table[i] == NULL) {
2603 comedi_subdevice_minor_table[i] = s;
2607 mutex_unlock(&comedi_subdevice_minor_table_lock);
2608 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2609 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2612 i += COMEDI_NUM_BOARD_MINORS;
2614 csdev = device_create(comedi_class, dev->class_dev,
2615 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2616 dev->minor, s->index);
2618 s->class_dev = csdev;
2623 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2632 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2633 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2635 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2636 mutex_lock(&comedi_subdevice_minor_table_lock);
2637 if (s == comedi_subdevice_minor_table[i])
2638 comedi_subdevice_minor_table[i] = NULL;
2639 mutex_unlock(&comedi_subdevice_minor_table_lock);
2641 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2642 s->class_dev = NULL;
2646 static void comedi_cleanup_board_minors(void)
2650 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2651 comedi_free_board_minor(i);
2654 static int __init comedi_init(void)
2659 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2661 if (comedi_num_legacy_minors < 0 ||
2662 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2663 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2664 COMEDI_NUM_BOARD_MINORS);
2668 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2669 COMEDI_NUM_MINORS, "comedi");
2672 cdev_init(&comedi_cdev, &comedi_fops);
2673 comedi_cdev.owner = THIS_MODULE;
2674 kobject_set_name(&comedi_cdev.kobj, "comedi");
2675 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2676 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2680 comedi_class = class_create(THIS_MODULE, "comedi");
2681 if (IS_ERR(comedi_class)) {
2682 pr_err("comedi: failed to create class\n");
2683 cdev_del(&comedi_cdev);
2684 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2686 return PTR_ERR(comedi_class);
2689 comedi_class->dev_groups = comedi_dev_groups;
2691 /* XXX requires /proc interface */
2694 /* create devices files for legacy/manual use */
2695 for (i = 0; i < comedi_num_legacy_minors; i++) {
2696 struct comedi_device *dev;
2697 dev = comedi_alloc_board_minor(NULL);
2699 comedi_cleanup_board_minors();
2700 cdev_del(&comedi_cdev);
2701 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2703 return PTR_ERR(dev);
2705 /* comedi_alloc_board_minor() locked the mutex */
2706 mutex_unlock(&dev->mutex);
2712 module_init(comedi_init);
2714 static void __exit comedi_cleanup(void)
2718 comedi_cleanup_board_minors();
2719 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2720 BUG_ON(comedi_board_minor_table[i]);
2721 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2722 BUG_ON(comedi_subdevice_minor_table[i]);
2724 class_destroy(comedi_class);
2725 cdev_del(&comedi_cdev);
2726 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2728 comedi_proc_cleanup();
2730 module_exit(comedi_cleanup);
2732 MODULE_AUTHOR("http://www.comedi.org");
2733 MODULE_DESCRIPTION("Comedi core module");
2734 MODULE_LICENSE("GPL");