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 spin_lock_init(&dev->spinlock);
93 mutex_init(&dev->mutex);
97 static void comedi_device_cleanup(struct comedi_device *dev)
99 struct module *driver_module = NULL;
103 mutex_lock(&dev->mutex);
105 driver_module = dev->driver->module;
106 comedi_device_detach(dev);
107 while (dev->use_count > 0) {
109 module_put(driver_module);
110 module_put(THIS_MODULE);
113 mutex_unlock(&dev->mutex);
114 mutex_destroy(&dev->mutex);
117 static bool comedi_clear_board_dev(struct comedi_device *dev)
119 unsigned int i = dev->minor;
120 bool cleared = false;
122 mutex_lock(&comedi_board_minor_table_lock);
123 if (dev == comedi_board_minor_table[i]) {
124 comedi_board_minor_table[i] = NULL;
127 mutex_unlock(&comedi_board_minor_table_lock);
131 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
133 struct comedi_device *dev;
135 mutex_lock(&comedi_board_minor_table_lock);
136 dev = comedi_board_minor_table[minor];
137 comedi_board_minor_table[minor] = NULL;
138 mutex_unlock(&comedi_board_minor_table_lock);
142 static void comedi_free_board_dev(struct comedi_device *dev)
145 if (dev->class_dev) {
146 device_destroy(comedi_class,
147 MKDEV(COMEDI_MAJOR, dev->minor));
149 comedi_device_cleanup(dev);
154 static struct comedi_subdevice
155 *comedi_subdevice_from_minor(unsigned minor)
157 struct comedi_subdevice *s;
158 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
160 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
161 mutex_lock(&comedi_subdevice_minor_table_lock);
162 s = comedi_subdevice_minor_table[i];
163 mutex_unlock(&comedi_subdevice_minor_table_lock);
167 static struct comedi_device *comedi_dev_from_board_minor(unsigned minor)
169 struct comedi_device *dev;
171 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
172 mutex_lock(&comedi_board_minor_table_lock);
173 dev = comedi_board_minor_table[minor];
174 mutex_unlock(&comedi_board_minor_table_lock);
178 static struct comedi_device *comedi_dev_from_subdevice_minor(unsigned minor)
180 struct comedi_subdevice *s;
182 s = comedi_subdevice_from_minor(minor);
183 return s ? s->device : NULL;
186 struct comedi_device *comedi_dev_from_minor(unsigned minor)
188 if (minor < COMEDI_NUM_BOARD_MINORS)
189 return comedi_dev_from_board_minor(minor);
191 return comedi_dev_from_subdevice_minor(minor);
193 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
195 static struct comedi_subdevice *
196 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
198 struct comedi_subdevice *s;
200 if (minor >= COMEDI_NUM_BOARD_MINORS) {
201 s = comedi_subdevice_from_minor(minor);
202 if (!s || s->device != dev)
204 if (s->subdev_flags & SDF_CMD_READ)
207 return dev->read_subdev;
210 static struct comedi_subdevice *
211 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
213 struct comedi_subdevice *s;
215 if (minor >= COMEDI_NUM_BOARD_MINORS) {
216 s = comedi_subdevice_from_minor(minor);
217 if (!s || s->device != dev)
219 if (s->subdev_flags & SDF_CMD_WRITE)
222 return dev->write_subdev;
225 static int resize_async_buffer(struct comedi_device *dev,
226 struct comedi_subdevice *s,
227 struct comedi_async *async, unsigned new_size)
231 if (new_size > async->max_bufsize)
235 DPRINTK("subdevice is busy, cannot resize buffer\n");
238 if (async->mmap_count) {
239 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
243 /* make sure buffer is an integral number of pages
245 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
247 retval = comedi_buf_alloc(dev, s, new_size);
252 retval = s->buf_change(dev, s, new_size);
257 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
258 dev->minor, s->index, async->prealloc_bufsz);
262 /* sysfs attribute files */
264 static ssize_t max_read_buffer_kb_show(struct device *csdev,
265 struct device_attribute *attr, char *buf)
267 unsigned int minor = MINOR(csdev->devt);
268 struct comedi_device *dev;
269 struct comedi_subdevice *s;
270 unsigned int size = 0;
272 dev = comedi_dev_from_minor(minor);
276 mutex_lock(&dev->mutex);
277 s = comedi_read_subdevice(dev, minor);
278 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
279 size = s->async->max_bufsize / 1024;
280 mutex_unlock(&dev->mutex);
282 return snprintf(buf, PAGE_SIZE, "%i\n", size);
285 static ssize_t max_read_buffer_kb_store(struct device *csdev,
286 struct device_attribute *attr,
287 const char *buf, size_t count)
289 unsigned int minor = MINOR(csdev->devt);
290 struct comedi_device *dev;
291 struct comedi_subdevice *s;
295 err = kstrtouint(buf, 10, &size);
298 if (size > (UINT_MAX / 1024))
302 dev = comedi_dev_from_minor(minor);
306 mutex_lock(&dev->mutex);
307 s = comedi_read_subdevice(dev, minor);
308 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
309 s->async->max_bufsize = size;
312 mutex_unlock(&dev->mutex);
314 return err ? err : count;
316 static DEVICE_ATTR_RW(max_read_buffer_kb);
318 static ssize_t read_buffer_kb_show(struct device *csdev,
319 struct device_attribute *attr, char *buf)
321 unsigned int minor = MINOR(csdev->devt);
322 struct comedi_device *dev;
323 struct comedi_subdevice *s;
324 unsigned int size = 0;
326 dev = comedi_dev_from_minor(minor);
330 mutex_lock(&dev->mutex);
331 s = comedi_read_subdevice(dev, minor);
332 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
333 size = s->async->prealloc_bufsz / 1024;
334 mutex_unlock(&dev->mutex);
336 return snprintf(buf, PAGE_SIZE, "%i\n", size);
339 static ssize_t read_buffer_kb_store(struct device *csdev,
340 struct device_attribute *attr,
341 const char *buf, size_t count)
343 unsigned int minor = MINOR(csdev->devt);
344 struct comedi_device *dev;
345 struct comedi_subdevice *s;
349 err = kstrtouint(buf, 10, &size);
352 if (size > (UINT_MAX / 1024))
356 dev = comedi_dev_from_minor(minor);
360 mutex_lock(&dev->mutex);
361 s = comedi_read_subdevice(dev, minor);
362 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
363 err = resize_async_buffer(dev, s, s->async, size);
366 mutex_unlock(&dev->mutex);
368 return err ? err : count;
370 static DEVICE_ATTR_RW(read_buffer_kb);
372 static ssize_t max_write_buffer_kb_show(struct device *csdev,
373 struct device_attribute *attr,
376 unsigned int minor = MINOR(csdev->devt);
377 struct comedi_device *dev;
378 struct comedi_subdevice *s;
379 unsigned int size = 0;
381 dev = comedi_dev_from_minor(minor);
385 mutex_lock(&dev->mutex);
386 s = comedi_write_subdevice(dev, minor);
387 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
388 size = s->async->max_bufsize / 1024;
389 mutex_unlock(&dev->mutex);
391 return snprintf(buf, PAGE_SIZE, "%i\n", size);
394 static ssize_t max_write_buffer_kb_store(struct device *csdev,
395 struct device_attribute *attr,
396 const char *buf, size_t count)
398 unsigned int minor = MINOR(csdev->devt);
399 struct comedi_device *dev;
400 struct comedi_subdevice *s;
404 err = kstrtouint(buf, 10, &size);
407 if (size > (UINT_MAX / 1024))
411 dev = comedi_dev_from_minor(minor);
415 mutex_lock(&dev->mutex);
416 s = comedi_write_subdevice(dev, minor);
417 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
418 s->async->max_bufsize = size;
421 mutex_unlock(&dev->mutex);
423 return err ? err : count;
425 static DEVICE_ATTR_RW(max_write_buffer_kb);
427 static ssize_t write_buffer_kb_show(struct device *csdev,
428 struct device_attribute *attr, char *buf)
430 unsigned int minor = MINOR(csdev->devt);
431 struct comedi_device *dev;
432 struct comedi_subdevice *s;
433 unsigned int size = 0;
435 dev = comedi_dev_from_minor(minor);
439 mutex_lock(&dev->mutex);
440 s = comedi_write_subdevice(dev, minor);
441 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
442 size = s->async->prealloc_bufsz / 1024;
443 mutex_unlock(&dev->mutex);
445 return snprintf(buf, PAGE_SIZE, "%i\n", size);
448 static ssize_t write_buffer_kb_store(struct device *csdev,
449 struct device_attribute *attr,
450 const char *buf, size_t count)
452 unsigned int minor = MINOR(csdev->devt);
453 struct comedi_device *dev;
454 struct comedi_subdevice *s;
458 err = kstrtouint(buf, 10, &size);
461 if (size > (UINT_MAX / 1024))
465 dev = comedi_dev_from_minor(minor);
469 mutex_lock(&dev->mutex);
470 s = comedi_write_subdevice(dev, minor);
471 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
472 err = resize_async_buffer(dev, s, s->async, size);
475 mutex_unlock(&dev->mutex);
477 return err ? err : count;
479 static DEVICE_ATTR_RW(write_buffer_kb);
481 static struct attribute *comedi_dev_attrs[] = {
482 &dev_attr_max_read_buffer_kb.attr,
483 &dev_attr_read_buffer_kb.attr,
484 &dev_attr_max_write_buffer_kb.attr,
485 &dev_attr_write_buffer_kb.attr,
488 ATTRIBUTE_GROUPS(comedi_dev);
490 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
491 unsigned mask, unsigned bits)
495 spin_lock_irqsave(&s->spin_lock, flags);
496 s->runflags &= ~mask;
497 s->runflags |= (bits & mask);
498 spin_unlock_irqrestore(&s->spin_lock, flags);
501 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
506 spin_lock_irqsave(&s->spin_lock, flags);
507 runflags = s->runflags;
508 spin_unlock_irqrestore(&s->spin_lock, flags);
512 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
514 unsigned runflags = comedi_get_subdevice_runflags(s);
516 return (runflags & SRF_RUNNING) ? true : false;
518 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
520 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
522 unsigned runflags = comedi_get_subdevice_runflags(s);
524 return (runflags & SRF_ERROR) ? true : false;
527 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
529 unsigned runflags = comedi_get_subdevice_runflags(s);
531 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
535 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
536 * @s: comedi_subdevice struct
537 * @size: size of the memory to allocate
539 * This also sets the subdevice runflags to allow the core to automatically
540 * free the private data during the detach.
542 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
544 s->private = kzalloc(size, GFP_KERNEL);
546 s->runflags |= SRF_FREE_SPRIV;
549 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
552 This function restores a subdevice to an idle state.
554 static void do_become_nonbusy(struct comedi_device *dev,
555 struct comedi_subdevice *s)
557 struct comedi_async *async = s->async;
559 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
561 comedi_buf_reset(async);
562 async->inttrig = NULL;
563 kfree(async->cmd.chanlist);
564 async->cmd.chanlist = NULL;
566 dev_err(dev->class_dev,
567 "BUG: (?) do_become_nonbusy called with async=NULL\n");
573 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
577 if (comedi_is_subdevice_running(s) && s->cancel)
578 ret = s->cancel(dev, s);
580 do_become_nonbusy(dev, s);
585 static int is_device_busy(struct comedi_device *dev)
587 struct comedi_subdevice *s;
593 for (i = 0; i < dev->n_subdevices; i++) {
594 s = &dev->subdevices[i];
597 if (s->async && s->async->mmap_count)
609 pointer to devconfig structure
612 devconfig structure at arg
617 static int do_devconfig_ioctl(struct comedi_device *dev,
618 struct comedi_devconfig __user *arg)
620 struct comedi_devconfig it;
622 if (!capable(CAP_SYS_ADMIN))
626 if (is_device_busy(dev))
629 struct module *driver_module = dev->driver->module;
630 comedi_device_detach(dev);
631 module_put(driver_module);
636 if (copy_from_user(&it, arg, sizeof(it)))
639 it.board_name[COMEDI_NAMELEN - 1] = 0;
641 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
642 dev_warn(dev->class_dev,
643 "comedi_config --init_data is deprecated\n");
647 if (dev->minor >= comedi_num_legacy_minors)
648 /* don't re-use dynamically allocated comedi devices */
651 /* This increments the driver module count on success. */
652 return comedi_device_attach(dev, &it);
657 buffer configuration ioctl
660 pointer to bufconfig structure
666 modified bufconfig at arg
669 static int do_bufconfig_ioctl(struct comedi_device *dev,
670 struct comedi_bufconfig __user *arg)
672 struct comedi_bufconfig bc;
673 struct comedi_async *async;
674 struct comedi_subdevice *s;
677 if (copy_from_user(&bc, arg, sizeof(bc)))
680 if (bc.subdevice >= dev->n_subdevices)
683 s = &dev->subdevices[bc.subdevice];
687 DPRINTK("subdevice does not have async capability\n");
693 if (bc.maximum_size) {
694 if (!capable(CAP_SYS_ADMIN))
697 async->max_bufsize = bc.maximum_size;
701 retval = resize_async_buffer(dev, s, async, bc.size);
706 bc.size = async->prealloc_bufsz;
707 bc.maximum_size = async->max_bufsize;
710 if (copy_to_user(arg, &bc, sizeof(bc)))
721 pointer to devinfo structure
730 static int do_devinfo_ioctl(struct comedi_device *dev,
731 struct comedi_devinfo __user *arg,
734 const unsigned minor = iminor(file_inode(file));
735 struct comedi_subdevice *s;
736 struct comedi_devinfo devinfo;
738 memset(&devinfo, 0, sizeof(devinfo));
740 /* fill devinfo structure */
741 devinfo.version_code = COMEDI_VERSION_CODE;
742 devinfo.n_subdevs = dev->n_subdevices;
743 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
744 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
746 s = comedi_read_subdevice(dev, minor);
748 devinfo.read_subdevice = s->index;
750 devinfo.read_subdevice = -1;
752 s = comedi_write_subdevice(dev, minor);
754 devinfo.write_subdevice = s->index;
756 devinfo.write_subdevice = -1;
758 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
769 pointer to array of subdevice info structures
775 array of subdevice info structures at arg
778 static int do_subdinfo_ioctl(struct comedi_device *dev,
779 struct comedi_subdinfo __user *arg, void *file)
782 struct comedi_subdinfo *tmp, *us;
783 struct comedi_subdevice *s;
785 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
789 /* fill subdinfo structs */
790 for (i = 0; i < dev->n_subdevices; i++) {
791 s = &dev->subdevices[i];
795 us->n_chan = s->n_chan;
796 us->subd_flags = s->subdev_flags;
797 if (comedi_is_subdevice_running(s))
798 us->subd_flags |= SDF_RUNNING;
799 #define TIMER_nanosec 5 /* backwards compatibility */
800 us->timer_type = TIMER_nanosec;
801 us->len_chanlist = s->len_chanlist;
802 us->maxdata = s->maxdata;
803 if (s->range_table) {
805 (i << 24) | (0 << 16) | (s->range_table->length);
807 us->range_type = 0; /* XXX */
811 us->subd_flags |= SDF_BUSY;
813 us->subd_flags |= SDF_BUSY_OWNER;
815 us->subd_flags |= SDF_LOCKED;
817 us->subd_flags |= SDF_LOCK_OWNER;
818 if (!s->maxdata && s->maxdata_list)
819 us->subd_flags |= SDF_MAXDATA;
820 if (s->range_table_list)
821 us->subd_flags |= SDF_RANGETYPE;
823 us->subd_flags |= SDF_CMD;
825 if (s->insn_bits != &insn_inval)
826 us->insn_bits_support = COMEDI_SUPPORTED;
828 us->insn_bits_support = COMEDI_UNSUPPORTED;
831 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
835 return ret ? -EFAULT : 0;
843 pointer to chaninfo structure
846 chaninfo structure at arg
849 arrays at elements of chaninfo structure
852 static int do_chaninfo_ioctl(struct comedi_device *dev,
853 struct comedi_chaninfo __user *arg)
855 struct comedi_subdevice *s;
856 struct comedi_chaninfo it;
858 if (copy_from_user(&it, arg, sizeof(it)))
861 if (it.subdev >= dev->n_subdevices)
863 s = &dev->subdevices[it.subdev];
865 if (it.maxdata_list) {
866 if (s->maxdata || !s->maxdata_list)
868 if (copy_to_user(it.maxdata_list, s->maxdata_list,
869 s->n_chan * sizeof(unsigned int)))
874 return -EINVAL; /* flaglist not supported */
879 if (!s->range_table_list)
881 for (i = 0; i < s->n_chan; i++) {
884 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
885 (s->range_table_list[i]->length);
886 if (put_user(x, it.rangelist + i))
890 if (copy_to_user(it.rangelist, s->range_type_list,
891 s->n_chan * sizeof(unsigned int)))
901 buffer information ioctl
904 pointer to bufinfo structure
910 modified bufinfo at arg
913 static int do_bufinfo_ioctl(struct comedi_device *dev,
914 struct comedi_bufinfo __user *arg, void *file)
916 struct comedi_bufinfo bi;
917 struct comedi_subdevice *s;
918 struct comedi_async *async;
920 if (copy_from_user(&bi, arg, sizeof(bi)))
923 if (bi.subdevice >= dev->n_subdevices)
926 s = &dev->subdevices[bi.subdevice];
928 if (s->lock && s->lock != file)
934 DPRINTK("subdevice does not have async capability\n");
935 bi.buf_write_ptr = 0;
937 bi.buf_write_count = 0;
938 bi.buf_read_count = 0;
940 bi.bytes_written = 0;
945 bi.bytes_written = 0;
946 goto copyback_position;
951 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
952 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
953 comedi_buf_read_free(async, bi.bytes_read);
955 if (comedi_is_subdevice_idle(s) &&
956 async->buf_write_count == async->buf_read_count) {
957 do_become_nonbusy(dev, s);
961 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
963 comedi_buf_write_alloc(async, bi.bytes_written);
964 comedi_buf_write_free(async, bi.bytes_written);
968 bi.buf_write_count = async->buf_write_count;
969 bi.buf_write_ptr = async->buf_write_ptr;
970 bi.buf_read_count = async->buf_read_count;
971 bi.buf_read_ptr = async->buf_read_ptr;
974 if (copy_to_user(arg, &bi, sizeof(bi)))
980 static int check_insn_config_length(struct comedi_insn *insn,
987 case INSN_CONFIG_DIO_OUTPUT:
988 case INSN_CONFIG_DIO_INPUT:
989 case INSN_CONFIG_DISARM:
990 case INSN_CONFIG_RESET:
994 case INSN_CONFIG_ARM:
995 case INSN_CONFIG_DIO_QUERY:
996 case INSN_CONFIG_BLOCK_SIZE:
997 case INSN_CONFIG_FILTER:
998 case INSN_CONFIG_SERIAL_CLOCK:
999 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1000 case INSN_CONFIG_ALT_SOURCE:
1001 case INSN_CONFIG_SET_COUNTER_MODE:
1002 case INSN_CONFIG_8254_READ_STATUS:
1003 case INSN_CONFIG_SET_ROUTING:
1004 case INSN_CONFIG_GET_ROUTING:
1005 case INSN_CONFIG_GET_PWM_STATUS:
1006 case INSN_CONFIG_PWM_SET_PERIOD:
1007 case INSN_CONFIG_PWM_GET_PERIOD:
1011 case INSN_CONFIG_SET_GATE_SRC:
1012 case INSN_CONFIG_GET_GATE_SRC:
1013 case INSN_CONFIG_SET_CLOCK_SRC:
1014 case INSN_CONFIG_GET_CLOCK_SRC:
1015 case INSN_CONFIG_SET_OTHER_SRC:
1016 case INSN_CONFIG_GET_COUNTER_STATUS:
1017 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1018 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1019 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1023 case INSN_CONFIG_PWM_OUTPUT:
1024 case INSN_CONFIG_ANALOG_TRIG:
1028 case INSN_CONFIG_DIGITAL_TRIG:
1032 /* by default we allow the insn since we don't have checks for
1033 * all possible cases yet */
1035 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1037 pr_warn("comedi: Add a check to %s in %s.\n",
1038 __func__, __FILE__);
1039 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1045 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1046 unsigned int *data, void *file)
1048 struct comedi_subdevice *s;
1052 if (insn->insn & INSN_MASK_SPECIAL) {
1053 /* a non-subdevice instruction */
1055 switch (insn->insn) {
1065 do_gettimeofday(&tv);
1066 data[0] = tv.tv_sec;
1067 data[1] = tv.tv_usec;
1073 if (insn->n != 1 || data[0] >= 100000) {
1077 udelay(data[0] / 1000);
1085 if (insn->subdev >= dev->n_subdevices) {
1086 DPRINTK("%d not usable subdevice\n",
1091 s = &dev->subdevices[insn->subdev];
1093 DPRINTK("no async\n");
1097 if (!s->async->inttrig) {
1098 DPRINTK("no inttrig\n");
1102 ret = s->async->inttrig(dev, s, data[0]);
1107 DPRINTK("invalid insn\n");
1112 /* a subdevice instruction */
1113 unsigned int maxdata;
1115 if (insn->subdev >= dev->n_subdevices) {
1116 DPRINTK("subdevice %d out of range\n", insn->subdev);
1120 s = &dev->subdevices[insn->subdev];
1122 if (s->type == COMEDI_SUBD_UNUSED) {
1123 DPRINTK("%d not usable subdevice\n", insn->subdev);
1128 /* are we locked? (ioctl lock) */
1129 if (s->lock && s->lock != file) {
1130 DPRINTK("device locked\n");
1135 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1138 DPRINTK("bad chanspec\n");
1146 /* This looks arbitrary. It is. */
1147 s->busy = &parse_insn;
1148 switch (insn->insn) {
1150 ret = s->insn_read(dev, s, insn, data);
1153 maxdata = s->maxdata_list
1154 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1156 for (i = 0; i < insn->n; ++i) {
1157 if (data[i] > maxdata) {
1159 DPRINTK("bad data value(s)\n");
1164 ret = s->insn_write(dev, s, insn, data);
1170 /* Most drivers ignore the base channel in
1171 * insn->chanspec. Fix this here if
1172 * the subdevice has <= 32 channels. */
1174 unsigned int orig_mask;
1176 orig_mask = data[0];
1177 if (s->n_chan <= 32) {
1178 shift = CR_CHAN(insn->chanspec);
1186 ret = s->insn_bits(dev, s, insn, data);
1187 data[0] = orig_mask;
1193 ret = check_insn_config_length(insn, data);
1196 ret = s->insn_config(dev, s, insn, data);
1212 * synchronous instructions
1215 * pointer to sync cmd structure
1218 * sync cmd struct at arg
1225 /* arbitrary limits */
1226 #define MAX_SAMPLES 256
1227 static int do_insnlist_ioctl(struct comedi_device *dev,
1228 struct comedi_insnlist __user *arg, void *file)
1230 struct comedi_insnlist insnlist;
1231 struct comedi_insn *insns = NULL;
1232 unsigned int *data = NULL;
1236 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1239 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1241 DPRINTK("kmalloc failed\n");
1246 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1248 DPRINTK("kmalloc failed\n");
1253 if (copy_from_user(insns, insnlist.insns,
1254 sizeof(*insns) * insnlist.n_insns)) {
1255 DPRINTK("copy_from_user failed\n");
1260 for (i = 0; i < insnlist.n_insns; i++) {
1261 if (insns[i].n > MAX_SAMPLES) {
1262 DPRINTK("number of samples too large\n");
1266 if (insns[i].insn & INSN_MASK_WRITE) {
1267 if (copy_from_user(data, insns[i].data,
1268 insns[i].n * sizeof(unsigned int))) {
1269 DPRINTK("copy_from_user failed\n");
1274 ret = parse_insn(dev, insns + i, data, file);
1277 if (insns[i].insn & INSN_MASK_READ) {
1278 if (copy_to_user(insns[i].data, data,
1279 insns[i].n * sizeof(unsigned int))) {
1280 DPRINTK("copy_to_user failed\n");
1300 * synchronous instructions
1306 * struct comedi_insn struct at arg
1312 static int do_insn_ioctl(struct comedi_device *dev,
1313 struct comedi_insn __user *arg, void *file)
1315 struct comedi_insn insn;
1316 unsigned int *data = NULL;
1319 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1325 if (copy_from_user(&insn, arg, sizeof(insn))) {
1330 /* This is where the behavior of insn and insnlist deviate. */
1331 if (insn.n > MAX_SAMPLES)
1332 insn.n = MAX_SAMPLES;
1333 if (insn.insn & INSN_MASK_WRITE) {
1334 if (copy_from_user(data,
1336 insn.n * sizeof(unsigned int))) {
1341 ret = parse_insn(dev, &insn, data, file);
1344 if (insn.insn & INSN_MASK_READ) {
1345 if (copy_to_user(insn.data,
1347 insn.n * sizeof(unsigned int))) {
1360 static int do_cmd_ioctl(struct comedi_device *dev,
1361 struct comedi_cmd __user *arg, void *file)
1363 struct comedi_cmd cmd;
1364 struct comedi_subdevice *s;
1365 struct comedi_async *async;
1367 unsigned int __user *user_chanlist;
1369 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1370 DPRINTK("bad cmd address\n");
1373 /* save user's chanlist pointer so it can be restored later */
1374 user_chanlist = (unsigned int __user *)cmd.chanlist;
1376 if (cmd.subdev >= dev->n_subdevices) {
1377 DPRINTK("%d no such subdevice\n", cmd.subdev);
1381 s = &dev->subdevices[cmd.subdev];
1384 if (s->type == COMEDI_SUBD_UNUSED) {
1385 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1389 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1390 DPRINTK("subdevice %i does not support commands\n",
1395 /* are we locked? (ioctl lock) */
1396 if (s->lock && s->lock != file) {
1397 DPRINTK("subdevice locked\n");
1403 DPRINTK("subdevice busy\n");
1407 /* make sure channel/gain list isn't too long */
1408 if (cmd.chanlist_len > s->len_chanlist) {
1409 DPRINTK("channel/gain list too long %u > %d\n",
1410 cmd.chanlist_len, s->len_chanlist);
1414 /* make sure channel/gain list isn't too short */
1415 if (cmd.chanlist_len < 1) {
1416 DPRINTK("channel/gain list too short %u < 1\n",
1422 async->cmd.data = NULL;
1423 /* load channel/gain list */
1424 async->cmd.chanlist = memdup_user(user_chanlist,
1425 async->cmd.chanlist_len * sizeof(int));
1426 if (IS_ERR(async->cmd.chanlist)) {
1427 ret = PTR_ERR(async->cmd.chanlist);
1428 DPRINTK("memdup_user failed with code %d\n", ret);
1432 /* make sure each element in channel/gain list is valid */
1433 ret = comedi_check_chanlist(s,
1434 async->cmd.chanlist_len,
1435 async->cmd.chanlist);
1437 DPRINTK("bad chanlist\n");
1441 ret = s->do_cmdtest(dev, s, &async->cmd);
1443 if (async->cmd.flags & TRIG_BOGUS || ret) {
1444 DPRINTK("test returned %d\n", ret);
1446 /* restore chanlist pointer before copying back */
1447 cmd.chanlist = (unsigned int __force *)user_chanlist;
1449 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1450 DPRINTK("fault writing cmd\n");
1458 if (!async->prealloc_bufsz) {
1460 DPRINTK("no buffer (?)\n");
1464 comedi_buf_reset(async);
1467 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1469 if (async->cmd.flags & TRIG_WAKE_EOS)
1470 async->cb_mask |= COMEDI_CB_EOS;
1472 comedi_set_subdevice_runflags(s, SRF_USER | SRF_ERROR | SRF_RUNNING,
1473 SRF_USER | SRF_RUNNING);
1475 /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
1476 * comedi_read() or comedi_write() */
1478 ret = s->do_cmd(dev, s);
1483 do_become_nonbusy(dev, s);
1490 command testing ioctl
1493 pointer to cmd structure
1496 cmd structure at arg
1500 modified cmd structure at arg
1503 static int do_cmdtest_ioctl(struct comedi_device *dev,
1504 struct comedi_cmd __user *arg, void *file)
1506 struct comedi_cmd cmd;
1507 struct comedi_subdevice *s;
1509 unsigned int *chanlist = NULL;
1510 unsigned int __user *user_chanlist;
1512 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1513 DPRINTK("bad cmd address\n");
1516 /* save user's chanlist pointer so it can be restored later */
1517 user_chanlist = (unsigned int __user *)cmd.chanlist;
1519 if (cmd.subdev >= dev->n_subdevices) {
1520 DPRINTK("%d no such subdevice\n", cmd.subdev);
1524 s = &dev->subdevices[cmd.subdev];
1525 if (s->type == COMEDI_SUBD_UNUSED) {
1526 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1530 if (!s->do_cmd || !s->do_cmdtest) {
1531 DPRINTK("subdevice %i does not support commands\n",
1536 /* make sure channel/gain list isn't too long */
1537 if (cmd.chanlist_len > s->len_chanlist) {
1538 DPRINTK("channel/gain list too long %d > %d\n",
1539 cmd.chanlist_len, s->len_chanlist);
1544 /* load channel/gain list */
1546 chanlist = memdup_user(user_chanlist,
1547 cmd.chanlist_len * sizeof(int));
1548 if (IS_ERR(chanlist)) {
1549 ret = PTR_ERR(chanlist);
1550 DPRINTK("memdup_user exited with code %d", ret);
1554 /* make sure each element in channel/gain list is valid */
1555 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1557 DPRINTK("bad chanlist\n");
1561 cmd.chanlist = chanlist;
1564 ret = s->do_cmdtest(dev, s, &cmd);
1566 /* restore chanlist pointer before copying back */
1567 cmd.chanlist = (unsigned int __force *)user_chanlist;
1569 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1570 DPRINTK("bad cmd address\n");
1595 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1599 unsigned long flags;
1600 struct comedi_subdevice *s;
1602 if (arg >= dev->n_subdevices)
1604 s = &dev->subdevices[arg];
1606 spin_lock_irqsave(&s->spin_lock, flags);
1607 if (s->busy || s->lock)
1611 spin_unlock_irqrestore(&s->spin_lock, flags);
1618 ret = s->lock_f(dev, s);
1637 This function isn't protected by the semaphore, since
1638 we already own the lock.
1640 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1643 struct comedi_subdevice *s;
1645 if (arg >= dev->n_subdevices)
1647 s = &dev->subdevices[arg];
1652 if (s->lock && s->lock != file)
1655 if (s->lock == file) {
1669 cancel acquisition ioctl
1681 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1684 struct comedi_subdevice *s;
1687 if (arg >= dev->n_subdevices)
1689 s = &dev->subdevices[arg];
1690 if (s->async == NULL)
1693 if (s->lock && s->lock != file)
1699 if (s->busy != file)
1702 ret = do_cancel(dev, s);
1703 if (comedi_get_subdevice_runflags(s) & SRF_USER)
1704 wake_up_interruptible(&s->async->wait_head);
1711 instructs driver to synchronize buffers
1723 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1726 struct comedi_subdevice *s;
1728 if (arg >= dev->n_subdevices)
1730 s = &dev->subdevices[arg];
1732 if (s->lock && s->lock != file)
1738 if (s->busy != file)
1742 return s->poll(dev, s);
1747 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1750 const unsigned minor = iminor(file_inode(file));
1751 struct comedi_device *dev = comedi_dev_from_minor(minor);
1757 mutex_lock(&dev->mutex);
1759 /* Device config is special, because it must work on
1760 * an unconfigured device. */
1761 if (cmd == COMEDI_DEVCONFIG) {
1762 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1763 /* Device config not appropriate on non-board minors. */
1767 rc = do_devconfig_ioctl(dev,
1768 (struct comedi_devconfig __user *)arg);
1771 dev->minor >= comedi_num_legacy_minors) {
1772 /* Successfully unconfigured a dynamically
1773 * allocated device. Try and remove it. */
1774 if (comedi_clear_board_dev(dev)) {
1775 mutex_unlock(&dev->mutex);
1776 comedi_free_board_dev(dev);
1784 if (!dev->attached) {
1785 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1791 case COMEDI_BUFCONFIG:
1792 rc = do_bufconfig_ioctl(dev,
1793 (struct comedi_bufconfig __user *)arg);
1795 case COMEDI_DEVINFO:
1796 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1799 case COMEDI_SUBDINFO:
1800 rc = do_subdinfo_ioctl(dev,
1801 (struct comedi_subdinfo __user *)arg,
1804 case COMEDI_CHANINFO:
1805 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1807 case COMEDI_RANGEINFO:
1808 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1810 case COMEDI_BUFINFO:
1811 rc = do_bufinfo_ioctl(dev,
1812 (struct comedi_bufinfo __user *)arg,
1816 rc = do_lock_ioctl(dev, arg, file);
1819 rc = do_unlock_ioctl(dev, arg, file);
1822 rc = do_cancel_ioctl(dev, arg, file);
1825 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1827 case COMEDI_CMDTEST:
1828 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1831 case COMEDI_INSNLIST:
1832 rc = do_insnlist_ioctl(dev,
1833 (struct comedi_insnlist __user *)arg,
1837 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1841 rc = do_poll_ioctl(dev, arg, file);
1849 mutex_unlock(&dev->mutex);
1853 static void comedi_vm_open(struct vm_area_struct *area)
1855 struct comedi_async *async;
1856 struct comedi_device *dev;
1858 async = area->vm_private_data;
1859 dev = async->subdevice->device;
1861 mutex_lock(&dev->mutex);
1862 async->mmap_count++;
1863 mutex_unlock(&dev->mutex);
1866 static void comedi_vm_close(struct vm_area_struct *area)
1868 struct comedi_async *async;
1869 struct comedi_device *dev;
1871 async = area->vm_private_data;
1872 dev = async->subdevice->device;
1874 mutex_lock(&dev->mutex);
1875 async->mmap_count--;
1876 mutex_unlock(&dev->mutex);
1879 static struct vm_operations_struct comedi_vm_ops = {
1880 .open = comedi_vm_open,
1881 .close = comedi_vm_close,
1884 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1886 const unsigned minor = iminor(file_inode(file));
1887 struct comedi_device *dev = comedi_dev_from_minor(minor);
1888 struct comedi_subdevice *s;
1889 struct comedi_async *async;
1890 unsigned long start = vma->vm_start;
1899 mutex_lock(&dev->mutex);
1901 if (!dev->attached) {
1902 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1907 if (vma->vm_flags & VM_WRITE)
1908 s = comedi_write_subdevice(dev, minor);
1910 s = comedi_read_subdevice(dev, minor);
1922 if (vma->vm_pgoff != 0) {
1923 DPRINTK("comedi: mmap() offset must be 0.\n");
1928 size = vma->vm_end - vma->vm_start;
1929 if (size > async->prealloc_bufsz) {
1933 if (size & (~PAGE_MASK)) {
1938 n_pages = size >> PAGE_SHIFT;
1939 for (i = 0; i < n_pages; ++i) {
1940 struct comedi_buf_page *buf = &async->buf_page_list[i];
1942 if (remap_pfn_range(vma, start,
1943 page_to_pfn(virt_to_page(buf->virt_addr)),
1944 PAGE_SIZE, PAGE_SHARED)) {
1951 vma->vm_ops = &comedi_vm_ops;
1952 vma->vm_private_data = async;
1954 async->mmap_count++;
1958 mutex_unlock(&dev->mutex);
1962 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1964 unsigned int mask = 0;
1965 const unsigned minor = iminor(file_inode(file));
1966 struct comedi_device *dev = comedi_dev_from_minor(minor);
1967 struct comedi_subdevice *s;
1972 mutex_lock(&dev->mutex);
1974 if (!dev->attached) {
1975 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1979 s = comedi_read_subdevice(dev, minor);
1980 if (s && s->async) {
1981 poll_wait(file, &s->async->wait_head, wait);
1982 if (!s->busy || !comedi_is_subdevice_running(s) ||
1983 comedi_buf_read_n_available(s->async) > 0)
1984 mask |= POLLIN | POLLRDNORM;
1987 s = comedi_write_subdevice(dev, minor);
1988 if (s && s->async) {
1989 unsigned int bps = bytes_per_sample(s->async->subdevice);
1991 poll_wait(file, &s->async->wait_head, wait);
1992 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1993 if (!s->busy || !comedi_is_subdevice_running(s) ||
1994 comedi_buf_write_n_allocated(s->async) >= bps)
1995 mask |= POLLOUT | POLLWRNORM;
1999 mutex_unlock(&dev->mutex);
2003 static ssize_t comedi_write(struct file *file, const char __user *buf,
2004 size_t nbytes, loff_t *offset)
2006 struct comedi_subdevice *s;
2007 struct comedi_async *async;
2008 int n, m, count = 0, retval = 0;
2009 DECLARE_WAITQUEUE(wait, current);
2010 const unsigned minor = iminor(file_inode(file));
2011 struct comedi_device *dev = comedi_dev_from_minor(minor);
2016 if (!dev->attached) {
2017 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2021 s = comedi_write_subdevice(dev, minor);
2022 if (!s || !s->async)
2027 if (!s->busy || !nbytes)
2029 if (s->busy != file)
2032 add_wait_queue(&async->wait_head, &wait);
2033 while (nbytes > 0 && !retval) {
2034 set_current_state(TASK_INTERRUPTIBLE);
2036 if (!comedi_is_subdevice_running(s)) {
2038 mutex_lock(&dev->mutex);
2039 if (comedi_is_subdevice_in_error(s))
2043 do_become_nonbusy(dev, s);
2044 mutex_unlock(&dev->mutex);
2052 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2053 m = async->prealloc_bufsz - async->buf_write_ptr;
2054 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2055 if (m > comedi_buf_write_n_allocated(async))
2056 m = comedi_buf_write_n_allocated(async);
2061 if (file->f_flags & O_NONBLOCK) {
2066 if (signal_pending(current)) {
2067 retval = -ERESTARTSYS;
2072 if (s->busy != file) {
2079 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2085 comedi_buf_write_free(async, n);
2091 break; /* makes device work like a pipe */
2093 set_current_state(TASK_RUNNING);
2094 remove_wait_queue(&async->wait_head, &wait);
2096 return count ? count : retval;
2099 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2102 struct comedi_subdevice *s;
2103 struct comedi_async *async;
2104 int n, m, count = 0, retval = 0;
2105 DECLARE_WAITQUEUE(wait, current);
2106 const unsigned minor = iminor(file_inode(file));
2107 struct comedi_device *dev = comedi_dev_from_minor(minor);
2112 if (!dev->attached) {
2113 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2117 s = comedi_read_subdevice(dev, minor);
2118 if (!s || !s->async)
2122 if (!s->busy || !nbytes)
2124 if (s->busy != file)
2127 add_wait_queue(&async->wait_head, &wait);
2128 while (nbytes > 0 && !retval) {
2129 set_current_state(TASK_INTERRUPTIBLE);
2133 m = comedi_buf_read_n_available(async);
2134 /* printk("%d available\n",m); */
2135 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2136 m = async->prealloc_bufsz - async->buf_read_ptr;
2137 /* printk("%d contiguous\n",m); */
2142 if (!comedi_is_subdevice_running(s)) {
2143 mutex_lock(&dev->mutex);
2144 do_become_nonbusy(dev, s);
2145 if (comedi_is_subdevice_in_error(s))
2149 mutex_unlock(&dev->mutex);
2152 if (file->f_flags & O_NONBLOCK) {
2157 if (signal_pending(current)) {
2158 retval = -ERESTARTSYS;
2165 if (s->busy != file) {
2171 m = copy_to_user(buf, async->prealloc_buf +
2172 async->buf_read_ptr, n);
2178 comedi_buf_read_alloc(async, n);
2179 comedi_buf_read_free(async, n);
2185 break; /* makes device work like a pipe */
2187 if (comedi_is_subdevice_idle(s)) {
2188 mutex_lock(&dev->mutex);
2189 if (async->buf_read_count - async->buf_write_count == 0)
2190 do_become_nonbusy(dev, s);
2191 mutex_unlock(&dev->mutex);
2193 set_current_state(TASK_RUNNING);
2194 remove_wait_queue(&async->wait_head, &wait);
2196 return count ? count : retval;
2199 static int comedi_open(struct inode *inode, struct file *file)
2201 const unsigned minor = iminor(inode);
2202 struct comedi_device *dev = comedi_dev_from_minor(minor);
2205 DPRINTK("invalid minor number\n");
2209 /* This is slightly hacky, but we want module autoloading
2211 * case: user opens device, attached -> ok
2212 * case: user opens device, unattached, !in_request_module -> autoload
2213 * case: user opens device, unattached, in_request_module -> fail
2214 * case: root opens device, attached -> ok
2215 * case: root opens device, unattached, in_request_module -> ok
2216 * (typically called from modprobe)
2217 * case: root opens device, unattached, !in_request_module -> autoload
2219 * The last could be changed to "-> ok", which would deny root
2222 mutex_lock(&dev->mutex);
2225 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2226 DPRINTK("in request module\n");
2227 mutex_unlock(&dev->mutex);
2230 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2233 dev->in_request_module = true;
2236 mutex_unlock(&dev->mutex);
2237 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2238 mutex_lock(&dev->mutex);
2241 dev->in_request_module = false;
2243 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2244 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2245 mutex_unlock(&dev->mutex);
2249 __module_get(THIS_MODULE);
2251 if (dev->attached) {
2252 if (!try_module_get(dev->driver->module)) {
2253 module_put(THIS_MODULE);
2254 mutex_unlock(&dev->mutex);
2259 if (dev->attached && dev->use_count == 0 && dev->open) {
2260 int rc = dev->open(dev);
2262 module_put(dev->driver->module);
2263 module_put(THIS_MODULE);
2264 mutex_unlock(&dev->mutex);
2271 mutex_unlock(&dev->mutex);
2276 static int comedi_fasync(int fd, struct file *file, int on)
2278 const unsigned minor = iminor(file_inode(file));
2279 struct comedi_device *dev = comedi_dev_from_minor(minor);
2284 return fasync_helper(fd, file, on, &dev->async_queue);
2287 static int comedi_close(struct inode *inode, struct file *file)
2289 const unsigned minor = iminor(inode);
2290 struct comedi_device *dev = comedi_dev_from_minor(minor);
2291 struct comedi_subdevice *s = NULL;
2297 mutex_lock(&dev->mutex);
2299 if (dev->subdevices) {
2300 for (i = 0; i < dev->n_subdevices; i++) {
2301 s = &dev->subdevices[i];
2303 if (s->busy == file)
2305 if (s->lock == file)
2309 if (dev->attached && dev->use_count == 1 && dev->close)
2312 module_put(THIS_MODULE);
2314 module_put(dev->driver->module);
2318 mutex_unlock(&dev->mutex);
2323 static const struct file_operations comedi_fops = {
2324 .owner = THIS_MODULE,
2325 .unlocked_ioctl = comedi_unlocked_ioctl,
2326 .compat_ioctl = comedi_compat_ioctl,
2327 .open = comedi_open,
2328 .release = comedi_close,
2329 .read = comedi_read,
2330 .write = comedi_write,
2331 .mmap = comedi_mmap,
2332 .poll = comedi_poll,
2333 .fasync = comedi_fasync,
2334 .llseek = noop_llseek,
2337 void comedi_error(const struct comedi_device *dev, const char *s)
2339 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2341 EXPORT_SYMBOL_GPL(comedi_error);
2343 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2345 struct comedi_async *async = s->async;
2346 unsigned runflags = 0;
2347 unsigned runflags_mask = 0;
2349 /* DPRINTK("comedi_event 0x%x\n",mask); */
2351 if (!comedi_is_subdevice_running(s))
2355 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2356 COMEDI_CB_OVERFLOW)) {
2357 runflags_mask |= SRF_RUNNING;
2359 /* remember if an error event has occurred, so an error
2360 * can be returned the next time the user does a read() */
2361 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2362 runflags_mask |= SRF_ERROR;
2363 runflags |= SRF_ERROR;
2365 if (runflags_mask) {
2366 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2367 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2370 if (async->cb_mask & s->async->events) {
2371 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2372 wake_up_interruptible(&async->wait_head);
2373 if (s->subdev_flags & SDF_CMD_READ)
2374 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2375 if (s->subdev_flags & SDF_CMD_WRITE)
2376 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2379 async->cb_func(s->async->events, async->cb_arg);
2382 s->async->events = 0;
2384 EXPORT_SYMBOL_GPL(comedi_event);
2386 /* Note: the ->mutex is pre-locked on successful return */
2387 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2389 struct comedi_device *dev;
2390 struct device *csdev;
2393 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2395 return ERR_PTR(-ENOMEM);
2396 comedi_device_init(dev);
2397 comedi_set_hw_dev(dev, hardware_device);
2398 mutex_lock(&dev->mutex);
2399 mutex_lock(&comedi_board_minor_table_lock);
2400 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2401 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2402 if (comedi_board_minor_table[i] == NULL) {
2403 comedi_board_minor_table[i] = dev;
2407 mutex_unlock(&comedi_board_minor_table_lock);
2408 if (i == COMEDI_NUM_BOARD_MINORS) {
2409 mutex_unlock(&dev->mutex);
2410 comedi_device_cleanup(dev);
2412 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2413 return ERR_PTR(-EBUSY);
2416 csdev = device_create(comedi_class, hardware_device,
2417 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2419 dev->class_dev = csdev;
2421 /* Note: dev->mutex needs to be unlocked by the caller. */
2425 static void comedi_free_board_minor(unsigned minor)
2427 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2428 comedi_free_board_dev(comedi_clear_board_minor(minor));
2431 void comedi_release_hardware_device(struct device *hardware_device)
2434 struct comedi_device *dev;
2436 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2438 mutex_lock(&comedi_board_minor_table_lock);
2439 dev = comedi_board_minor_table[minor];
2440 if (dev && dev->hw_dev == hardware_device) {
2441 comedi_board_minor_table[minor] = NULL;
2442 mutex_unlock(&comedi_board_minor_table_lock);
2443 comedi_free_board_dev(dev);
2446 mutex_unlock(&comedi_board_minor_table_lock);
2450 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2452 struct comedi_device *dev = s->device;
2453 struct device *csdev;
2456 mutex_lock(&comedi_subdevice_minor_table_lock);
2457 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2458 if (comedi_subdevice_minor_table[i] == NULL) {
2459 comedi_subdevice_minor_table[i] = s;
2463 mutex_unlock(&comedi_subdevice_minor_table_lock);
2464 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2465 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2468 i += COMEDI_NUM_BOARD_MINORS;
2470 csdev = device_create(comedi_class, dev->class_dev,
2471 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2472 dev->minor, s->index);
2474 s->class_dev = csdev;
2479 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2488 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2489 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2491 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2492 mutex_lock(&comedi_subdevice_minor_table_lock);
2493 if (s == comedi_subdevice_minor_table[i])
2494 comedi_subdevice_minor_table[i] = NULL;
2495 mutex_unlock(&comedi_subdevice_minor_table_lock);
2497 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2498 s->class_dev = NULL;
2502 static void comedi_cleanup_board_minors(void)
2506 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2507 comedi_free_board_minor(i);
2510 static int __init comedi_init(void)
2515 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2517 if (comedi_num_legacy_minors < 0 ||
2518 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2519 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2520 COMEDI_NUM_BOARD_MINORS);
2524 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2525 COMEDI_NUM_MINORS, "comedi");
2528 cdev_init(&comedi_cdev, &comedi_fops);
2529 comedi_cdev.owner = THIS_MODULE;
2530 kobject_set_name(&comedi_cdev.kobj, "comedi");
2531 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2532 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2536 comedi_class = class_create(THIS_MODULE, "comedi");
2537 if (IS_ERR(comedi_class)) {
2538 pr_err("comedi: failed to create class\n");
2539 cdev_del(&comedi_cdev);
2540 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2542 return PTR_ERR(comedi_class);
2545 comedi_class->dev_groups = comedi_dev_groups;
2547 /* XXX requires /proc interface */
2550 /* create devices files for legacy/manual use */
2551 for (i = 0; i < comedi_num_legacy_minors; i++) {
2552 struct comedi_device *dev;
2553 dev = comedi_alloc_board_minor(NULL);
2555 comedi_cleanup_board_minors();
2556 cdev_del(&comedi_cdev);
2557 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2559 return PTR_ERR(dev);
2561 /* comedi_alloc_board_minor() locked the mutex */
2562 mutex_unlock(&dev->mutex);
2568 module_init(comedi_init);
2570 static void __exit comedi_cleanup(void)
2574 comedi_cleanup_board_minors();
2575 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2576 BUG_ON(comedi_board_minor_table[i]);
2577 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2578 BUG_ON(comedi_subdevice_minor_table[i]);
2580 class_destroy(comedi_class);
2581 cdev_del(&comedi_cdev);
2582 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2584 comedi_proc_cleanup();
2586 module_exit(comedi_cleanup);
2588 MODULE_AUTHOR("http://www.comedi.org");
2589 MODULE_DESCRIPTION("Comedi core module");
2590 MODULE_LICENSE("GPL");