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);
94 init_rwsem(&dev->attach_lock);
98 static void comedi_device_cleanup(struct comedi_device *dev)
100 struct module *driver_module = NULL;
104 mutex_lock(&dev->mutex);
106 driver_module = dev->driver->module;
107 comedi_device_detach(dev);
108 while (dev->use_count > 0) {
110 module_put(driver_module);
111 module_put(THIS_MODULE);
114 mutex_unlock(&dev->mutex);
115 mutex_destroy(&dev->mutex);
118 static bool comedi_clear_board_dev(struct comedi_device *dev)
120 unsigned int i = dev->minor;
121 bool cleared = false;
123 mutex_lock(&comedi_board_minor_table_lock);
124 if (dev == comedi_board_minor_table[i]) {
125 comedi_board_minor_table[i] = NULL;
128 mutex_unlock(&comedi_board_minor_table_lock);
132 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
134 struct comedi_device *dev;
136 mutex_lock(&comedi_board_minor_table_lock);
137 dev = comedi_board_minor_table[minor];
138 comedi_board_minor_table[minor] = NULL;
139 mutex_unlock(&comedi_board_minor_table_lock);
143 static void comedi_free_board_dev(struct comedi_device *dev)
146 if (dev->class_dev) {
147 device_destroy(comedi_class,
148 MKDEV(COMEDI_MAJOR, dev->minor));
150 comedi_device_cleanup(dev);
155 static struct comedi_subdevice
156 *comedi_subdevice_from_minor(unsigned minor)
158 struct comedi_subdevice *s;
159 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
161 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
162 mutex_lock(&comedi_subdevice_minor_table_lock);
163 s = comedi_subdevice_minor_table[i];
164 mutex_unlock(&comedi_subdevice_minor_table_lock);
168 static struct comedi_device *comedi_dev_from_board_minor(unsigned minor)
170 struct comedi_device *dev;
172 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
173 mutex_lock(&comedi_board_minor_table_lock);
174 dev = comedi_board_minor_table[minor];
175 mutex_unlock(&comedi_board_minor_table_lock);
179 static struct comedi_device *comedi_dev_from_subdevice_minor(unsigned minor)
181 struct comedi_subdevice *s;
183 s = comedi_subdevice_from_minor(minor);
184 return s ? s->device : NULL;
187 struct comedi_device *comedi_dev_from_minor(unsigned minor)
189 if (minor < COMEDI_NUM_BOARD_MINORS)
190 return comedi_dev_from_board_minor(minor);
192 return comedi_dev_from_subdevice_minor(minor);
194 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
196 static struct comedi_subdevice *
197 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
199 struct comedi_subdevice *s;
201 if (minor >= COMEDI_NUM_BOARD_MINORS) {
202 s = comedi_subdevice_from_minor(minor);
203 if (!s || s->device != dev)
205 if (s->subdev_flags & SDF_CMD_READ)
208 return dev->read_subdev;
211 static struct comedi_subdevice *
212 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
214 struct comedi_subdevice *s;
216 if (minor >= COMEDI_NUM_BOARD_MINORS) {
217 s = comedi_subdevice_from_minor(minor);
218 if (!s || s->device != dev)
220 if (s->subdev_flags & SDF_CMD_WRITE)
223 return dev->write_subdev;
226 static int resize_async_buffer(struct comedi_device *dev,
227 struct comedi_subdevice *s,
228 struct comedi_async *async, unsigned new_size)
232 if (new_size > async->max_bufsize)
236 DPRINTK("subdevice is busy, cannot resize buffer\n");
239 if (async->mmap_count) {
240 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
244 /* make sure buffer is an integral number of pages
246 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
248 retval = comedi_buf_alloc(dev, s, new_size);
253 retval = s->buf_change(dev, s, new_size);
258 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
259 dev->minor, s->index, async->prealloc_bufsz);
263 /* sysfs attribute files */
265 static ssize_t max_read_buffer_kb_show(struct device *csdev,
266 struct device_attribute *attr, char *buf)
268 unsigned int minor = MINOR(csdev->devt);
269 struct comedi_device *dev;
270 struct comedi_subdevice *s;
271 unsigned int size = 0;
273 dev = comedi_dev_from_minor(minor);
277 mutex_lock(&dev->mutex);
278 s = comedi_read_subdevice(dev, minor);
279 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
280 size = s->async->max_bufsize / 1024;
281 mutex_unlock(&dev->mutex);
283 return snprintf(buf, PAGE_SIZE, "%i\n", size);
286 static ssize_t max_read_buffer_kb_store(struct device *csdev,
287 struct device_attribute *attr,
288 const char *buf, size_t count)
290 unsigned int minor = MINOR(csdev->devt);
291 struct comedi_device *dev;
292 struct comedi_subdevice *s;
296 err = kstrtouint(buf, 10, &size);
299 if (size > (UINT_MAX / 1024))
303 dev = comedi_dev_from_minor(minor);
307 mutex_lock(&dev->mutex);
308 s = comedi_read_subdevice(dev, minor);
309 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
310 s->async->max_bufsize = size;
313 mutex_unlock(&dev->mutex);
315 return err ? err : count;
317 static DEVICE_ATTR_RW(max_read_buffer_kb);
319 static ssize_t read_buffer_kb_show(struct device *csdev,
320 struct device_attribute *attr, char *buf)
322 unsigned int minor = MINOR(csdev->devt);
323 struct comedi_device *dev;
324 struct comedi_subdevice *s;
325 unsigned int size = 0;
327 dev = comedi_dev_from_minor(minor);
331 mutex_lock(&dev->mutex);
332 s = comedi_read_subdevice(dev, minor);
333 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
334 size = s->async->prealloc_bufsz / 1024;
335 mutex_unlock(&dev->mutex);
337 return snprintf(buf, PAGE_SIZE, "%i\n", size);
340 static ssize_t read_buffer_kb_store(struct device *csdev,
341 struct device_attribute *attr,
342 const char *buf, size_t count)
344 unsigned int minor = MINOR(csdev->devt);
345 struct comedi_device *dev;
346 struct comedi_subdevice *s;
350 err = kstrtouint(buf, 10, &size);
353 if (size > (UINT_MAX / 1024))
357 dev = comedi_dev_from_minor(minor);
361 mutex_lock(&dev->mutex);
362 s = comedi_read_subdevice(dev, minor);
363 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
364 err = resize_async_buffer(dev, s, s->async, size);
367 mutex_unlock(&dev->mutex);
369 return err ? err : count;
371 static DEVICE_ATTR_RW(read_buffer_kb);
373 static ssize_t max_write_buffer_kb_show(struct device *csdev,
374 struct device_attribute *attr,
377 unsigned int minor = MINOR(csdev->devt);
378 struct comedi_device *dev;
379 struct comedi_subdevice *s;
380 unsigned int size = 0;
382 dev = comedi_dev_from_minor(minor);
386 mutex_lock(&dev->mutex);
387 s = comedi_write_subdevice(dev, minor);
388 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
389 size = s->async->max_bufsize / 1024;
390 mutex_unlock(&dev->mutex);
392 return snprintf(buf, PAGE_SIZE, "%i\n", size);
395 static ssize_t max_write_buffer_kb_store(struct device *csdev,
396 struct device_attribute *attr,
397 const char *buf, size_t count)
399 unsigned int minor = MINOR(csdev->devt);
400 struct comedi_device *dev;
401 struct comedi_subdevice *s;
405 err = kstrtouint(buf, 10, &size);
408 if (size > (UINT_MAX / 1024))
412 dev = comedi_dev_from_minor(minor);
416 mutex_lock(&dev->mutex);
417 s = comedi_write_subdevice(dev, minor);
418 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
419 s->async->max_bufsize = size;
422 mutex_unlock(&dev->mutex);
424 return err ? err : count;
426 static DEVICE_ATTR_RW(max_write_buffer_kb);
428 static ssize_t write_buffer_kb_show(struct device *csdev,
429 struct device_attribute *attr, char *buf)
431 unsigned int minor = MINOR(csdev->devt);
432 struct comedi_device *dev;
433 struct comedi_subdevice *s;
434 unsigned int size = 0;
436 dev = comedi_dev_from_minor(minor);
440 mutex_lock(&dev->mutex);
441 s = comedi_write_subdevice(dev, minor);
442 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
443 size = s->async->prealloc_bufsz / 1024;
444 mutex_unlock(&dev->mutex);
446 return snprintf(buf, PAGE_SIZE, "%i\n", size);
449 static ssize_t write_buffer_kb_store(struct device *csdev,
450 struct device_attribute *attr,
451 const char *buf, size_t count)
453 unsigned int minor = MINOR(csdev->devt);
454 struct comedi_device *dev;
455 struct comedi_subdevice *s;
459 err = kstrtouint(buf, 10, &size);
462 if (size > (UINT_MAX / 1024))
466 dev = comedi_dev_from_minor(minor);
470 mutex_lock(&dev->mutex);
471 s = comedi_write_subdevice(dev, minor);
472 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
473 err = resize_async_buffer(dev, s, s->async, size);
476 mutex_unlock(&dev->mutex);
478 return err ? err : count;
480 static DEVICE_ATTR_RW(write_buffer_kb);
482 static struct attribute *comedi_dev_attrs[] = {
483 &dev_attr_max_read_buffer_kb.attr,
484 &dev_attr_read_buffer_kb.attr,
485 &dev_attr_max_write_buffer_kb.attr,
486 &dev_attr_write_buffer_kb.attr,
489 ATTRIBUTE_GROUPS(comedi_dev);
491 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
492 unsigned mask, unsigned bits)
496 spin_lock_irqsave(&s->spin_lock, flags);
497 s->runflags &= ~mask;
498 s->runflags |= (bits & mask);
499 spin_unlock_irqrestore(&s->spin_lock, flags);
502 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
507 spin_lock_irqsave(&s->spin_lock, flags);
508 runflags = s->runflags;
509 spin_unlock_irqrestore(&s->spin_lock, flags);
513 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
515 unsigned runflags = comedi_get_subdevice_runflags(s);
517 return (runflags & SRF_RUNNING) ? true : false;
519 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
521 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
523 unsigned runflags = comedi_get_subdevice_runflags(s);
525 return (runflags & SRF_ERROR) ? true : false;
528 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
530 unsigned runflags = comedi_get_subdevice_runflags(s);
532 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
536 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
537 * @s: comedi_subdevice struct
538 * @size: size of the memory to allocate
540 * This also sets the subdevice runflags to allow the core to automatically
541 * free the private data during the detach.
543 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
545 s->private = kzalloc(size, GFP_KERNEL);
547 s->runflags |= SRF_FREE_SPRIV;
550 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
553 This function restores a subdevice to an idle state.
555 static void do_become_nonbusy(struct comedi_device *dev,
556 struct comedi_subdevice *s)
558 struct comedi_async *async = s->async;
560 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
562 comedi_buf_reset(async);
563 async->inttrig = NULL;
564 kfree(async->cmd.chanlist);
565 async->cmd.chanlist = NULL;
567 dev_err(dev->class_dev,
568 "BUG: (?) do_become_nonbusy called with async=NULL\n");
574 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
578 if (comedi_is_subdevice_running(s) && s->cancel)
579 ret = s->cancel(dev, s);
581 do_become_nonbusy(dev, s);
586 static int is_device_busy(struct comedi_device *dev)
588 struct comedi_subdevice *s;
594 for (i = 0; i < dev->n_subdevices; i++) {
595 s = &dev->subdevices[i];
598 if (s->async && s->async->mmap_count)
610 pointer to devconfig structure
613 devconfig structure at arg
618 static int do_devconfig_ioctl(struct comedi_device *dev,
619 struct comedi_devconfig __user *arg)
621 struct comedi_devconfig it;
623 if (!capable(CAP_SYS_ADMIN))
627 if (is_device_busy(dev))
630 struct module *driver_module = dev->driver->module;
631 comedi_device_detach(dev);
632 module_put(driver_module);
637 if (copy_from_user(&it, arg, sizeof(it)))
640 it.board_name[COMEDI_NAMELEN - 1] = 0;
642 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
643 dev_warn(dev->class_dev,
644 "comedi_config --init_data is deprecated\n");
648 if (dev->minor >= comedi_num_legacy_minors)
649 /* don't re-use dynamically allocated comedi devices */
652 /* This increments the driver module count on success. */
653 return comedi_device_attach(dev, &it);
658 buffer configuration ioctl
661 pointer to bufconfig structure
667 modified bufconfig at arg
670 static int do_bufconfig_ioctl(struct comedi_device *dev,
671 struct comedi_bufconfig __user *arg)
673 struct comedi_bufconfig bc;
674 struct comedi_async *async;
675 struct comedi_subdevice *s;
678 if (copy_from_user(&bc, arg, sizeof(bc)))
681 if (bc.subdevice >= dev->n_subdevices)
684 s = &dev->subdevices[bc.subdevice];
688 DPRINTK("subdevice does not have async capability\n");
694 if (bc.maximum_size) {
695 if (!capable(CAP_SYS_ADMIN))
698 async->max_bufsize = bc.maximum_size;
702 retval = resize_async_buffer(dev, s, async, bc.size);
707 bc.size = async->prealloc_bufsz;
708 bc.maximum_size = async->max_bufsize;
711 if (copy_to_user(arg, &bc, sizeof(bc)))
722 pointer to devinfo structure
731 static int do_devinfo_ioctl(struct comedi_device *dev,
732 struct comedi_devinfo __user *arg,
735 const unsigned minor = iminor(file_inode(file));
736 struct comedi_subdevice *s;
737 struct comedi_devinfo devinfo;
739 memset(&devinfo, 0, sizeof(devinfo));
741 /* fill devinfo structure */
742 devinfo.version_code = COMEDI_VERSION_CODE;
743 devinfo.n_subdevs = dev->n_subdevices;
744 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
745 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
747 s = comedi_read_subdevice(dev, minor);
749 devinfo.read_subdevice = s->index;
751 devinfo.read_subdevice = -1;
753 s = comedi_write_subdevice(dev, minor);
755 devinfo.write_subdevice = s->index;
757 devinfo.write_subdevice = -1;
759 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
770 pointer to array of subdevice info structures
776 array of subdevice info structures at arg
779 static int do_subdinfo_ioctl(struct comedi_device *dev,
780 struct comedi_subdinfo __user *arg, void *file)
783 struct comedi_subdinfo *tmp, *us;
784 struct comedi_subdevice *s;
786 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
790 /* fill subdinfo structs */
791 for (i = 0; i < dev->n_subdevices; i++) {
792 s = &dev->subdevices[i];
796 us->n_chan = s->n_chan;
797 us->subd_flags = s->subdev_flags;
798 if (comedi_is_subdevice_running(s))
799 us->subd_flags |= SDF_RUNNING;
800 #define TIMER_nanosec 5 /* backwards compatibility */
801 us->timer_type = TIMER_nanosec;
802 us->len_chanlist = s->len_chanlist;
803 us->maxdata = s->maxdata;
804 if (s->range_table) {
806 (i << 24) | (0 << 16) | (s->range_table->length);
808 us->range_type = 0; /* XXX */
812 us->subd_flags |= SDF_BUSY;
814 us->subd_flags |= SDF_BUSY_OWNER;
816 us->subd_flags |= SDF_LOCKED;
818 us->subd_flags |= SDF_LOCK_OWNER;
819 if (!s->maxdata && s->maxdata_list)
820 us->subd_flags |= SDF_MAXDATA;
821 if (s->range_table_list)
822 us->subd_flags |= SDF_RANGETYPE;
824 us->subd_flags |= SDF_CMD;
826 if (s->insn_bits != &insn_inval)
827 us->insn_bits_support = COMEDI_SUPPORTED;
829 us->insn_bits_support = COMEDI_UNSUPPORTED;
832 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
836 return ret ? -EFAULT : 0;
844 pointer to chaninfo structure
847 chaninfo structure at arg
850 arrays at elements of chaninfo structure
853 static int do_chaninfo_ioctl(struct comedi_device *dev,
854 struct comedi_chaninfo __user *arg)
856 struct comedi_subdevice *s;
857 struct comedi_chaninfo it;
859 if (copy_from_user(&it, arg, sizeof(it)))
862 if (it.subdev >= dev->n_subdevices)
864 s = &dev->subdevices[it.subdev];
866 if (it.maxdata_list) {
867 if (s->maxdata || !s->maxdata_list)
869 if (copy_to_user(it.maxdata_list, s->maxdata_list,
870 s->n_chan * sizeof(unsigned int)))
875 return -EINVAL; /* flaglist not supported */
880 if (!s->range_table_list)
882 for (i = 0; i < s->n_chan; i++) {
885 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
886 (s->range_table_list[i]->length);
887 if (put_user(x, it.rangelist + i))
891 if (copy_to_user(it.rangelist, s->range_type_list,
892 s->n_chan * sizeof(unsigned int)))
902 buffer information ioctl
905 pointer to bufinfo structure
911 modified bufinfo at arg
914 static int do_bufinfo_ioctl(struct comedi_device *dev,
915 struct comedi_bufinfo __user *arg, void *file)
917 struct comedi_bufinfo bi;
918 struct comedi_subdevice *s;
919 struct comedi_async *async;
921 if (copy_from_user(&bi, arg, sizeof(bi)))
924 if (bi.subdevice >= dev->n_subdevices)
927 s = &dev->subdevices[bi.subdevice];
929 if (s->lock && s->lock != file)
935 DPRINTK("subdevice does not have async capability\n");
936 bi.buf_write_ptr = 0;
938 bi.buf_write_count = 0;
939 bi.buf_read_count = 0;
941 bi.bytes_written = 0;
946 bi.bytes_written = 0;
947 goto copyback_position;
952 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
953 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
954 comedi_buf_read_free(async, bi.bytes_read);
956 if (comedi_is_subdevice_idle(s) &&
957 async->buf_write_count == async->buf_read_count) {
958 do_become_nonbusy(dev, s);
962 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
964 comedi_buf_write_alloc(async, bi.bytes_written);
965 comedi_buf_write_free(async, bi.bytes_written);
969 bi.buf_write_count = async->buf_write_count;
970 bi.buf_write_ptr = async->buf_write_ptr;
971 bi.buf_read_count = async->buf_read_count;
972 bi.buf_read_ptr = async->buf_read_ptr;
975 if (copy_to_user(arg, &bi, sizeof(bi)))
981 static int check_insn_config_length(struct comedi_insn *insn,
988 case INSN_CONFIG_DIO_OUTPUT:
989 case INSN_CONFIG_DIO_INPUT:
990 case INSN_CONFIG_DISARM:
991 case INSN_CONFIG_RESET:
995 case INSN_CONFIG_ARM:
996 case INSN_CONFIG_DIO_QUERY:
997 case INSN_CONFIG_BLOCK_SIZE:
998 case INSN_CONFIG_FILTER:
999 case INSN_CONFIG_SERIAL_CLOCK:
1000 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1001 case INSN_CONFIG_ALT_SOURCE:
1002 case INSN_CONFIG_SET_COUNTER_MODE:
1003 case INSN_CONFIG_8254_READ_STATUS:
1004 case INSN_CONFIG_SET_ROUTING:
1005 case INSN_CONFIG_GET_ROUTING:
1006 case INSN_CONFIG_GET_PWM_STATUS:
1007 case INSN_CONFIG_PWM_SET_PERIOD:
1008 case INSN_CONFIG_PWM_GET_PERIOD:
1012 case INSN_CONFIG_SET_GATE_SRC:
1013 case INSN_CONFIG_GET_GATE_SRC:
1014 case INSN_CONFIG_SET_CLOCK_SRC:
1015 case INSN_CONFIG_GET_CLOCK_SRC:
1016 case INSN_CONFIG_SET_OTHER_SRC:
1017 case INSN_CONFIG_GET_COUNTER_STATUS:
1018 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1019 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1020 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1024 case INSN_CONFIG_PWM_OUTPUT:
1025 case INSN_CONFIG_ANALOG_TRIG:
1029 case INSN_CONFIG_DIGITAL_TRIG:
1033 /* by default we allow the insn since we don't have checks for
1034 * all possible cases yet */
1036 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1038 pr_warn("comedi: Add a check to %s in %s.\n",
1039 __func__, __FILE__);
1040 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1046 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1047 unsigned int *data, void *file)
1049 struct comedi_subdevice *s;
1053 if (insn->insn & INSN_MASK_SPECIAL) {
1054 /* a non-subdevice instruction */
1056 switch (insn->insn) {
1066 do_gettimeofday(&tv);
1067 data[0] = tv.tv_sec;
1068 data[1] = tv.tv_usec;
1074 if (insn->n != 1 || data[0] >= 100000) {
1078 udelay(data[0] / 1000);
1086 if (insn->subdev >= dev->n_subdevices) {
1087 DPRINTK("%d not usable subdevice\n",
1092 s = &dev->subdevices[insn->subdev];
1094 DPRINTK("no async\n");
1098 if (!s->async->inttrig) {
1099 DPRINTK("no inttrig\n");
1103 ret = s->async->inttrig(dev, s, data[0]);
1108 DPRINTK("invalid insn\n");
1113 /* a subdevice instruction */
1114 unsigned int maxdata;
1116 if (insn->subdev >= dev->n_subdevices) {
1117 DPRINTK("subdevice %d out of range\n", insn->subdev);
1121 s = &dev->subdevices[insn->subdev];
1123 if (s->type == COMEDI_SUBD_UNUSED) {
1124 DPRINTK("%d not usable subdevice\n", insn->subdev);
1129 /* are we locked? (ioctl lock) */
1130 if (s->lock && s->lock != file) {
1131 DPRINTK("device locked\n");
1136 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1139 DPRINTK("bad chanspec\n");
1147 /* This looks arbitrary. It is. */
1148 s->busy = &parse_insn;
1149 switch (insn->insn) {
1151 ret = s->insn_read(dev, s, insn, data);
1154 maxdata = s->maxdata_list
1155 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1157 for (i = 0; i < insn->n; ++i) {
1158 if (data[i] > maxdata) {
1160 DPRINTK("bad data value(s)\n");
1165 ret = s->insn_write(dev, s, insn, data);
1171 /* Most drivers ignore the base channel in
1172 * insn->chanspec. Fix this here if
1173 * the subdevice has <= 32 channels. */
1175 unsigned int orig_mask;
1177 orig_mask = data[0];
1178 if (s->n_chan <= 32) {
1179 shift = CR_CHAN(insn->chanspec);
1187 ret = s->insn_bits(dev, s, insn, data);
1188 data[0] = orig_mask;
1194 ret = check_insn_config_length(insn, data);
1197 ret = s->insn_config(dev, s, insn, data);
1213 * synchronous instructions
1216 * pointer to sync cmd structure
1219 * sync cmd struct at arg
1226 /* arbitrary limits */
1227 #define MAX_SAMPLES 256
1228 static int do_insnlist_ioctl(struct comedi_device *dev,
1229 struct comedi_insnlist __user *arg, void *file)
1231 struct comedi_insnlist insnlist;
1232 struct comedi_insn *insns = NULL;
1233 unsigned int *data = NULL;
1237 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1240 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1242 DPRINTK("kmalloc failed\n");
1247 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1249 DPRINTK("kmalloc failed\n");
1254 if (copy_from_user(insns, insnlist.insns,
1255 sizeof(*insns) * insnlist.n_insns)) {
1256 DPRINTK("copy_from_user failed\n");
1261 for (i = 0; i < insnlist.n_insns; i++) {
1262 if (insns[i].n > MAX_SAMPLES) {
1263 DPRINTK("number of samples too large\n");
1267 if (insns[i].insn & INSN_MASK_WRITE) {
1268 if (copy_from_user(data, insns[i].data,
1269 insns[i].n * sizeof(unsigned int))) {
1270 DPRINTK("copy_from_user failed\n");
1275 ret = parse_insn(dev, insns + i, data, file);
1278 if (insns[i].insn & INSN_MASK_READ) {
1279 if (copy_to_user(insns[i].data, data,
1280 insns[i].n * sizeof(unsigned int))) {
1281 DPRINTK("copy_to_user failed\n");
1301 * synchronous instructions
1307 * struct comedi_insn struct at arg
1313 static int do_insn_ioctl(struct comedi_device *dev,
1314 struct comedi_insn __user *arg, void *file)
1316 struct comedi_insn insn;
1317 unsigned int *data = NULL;
1320 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1326 if (copy_from_user(&insn, arg, sizeof(insn))) {
1331 /* This is where the behavior of insn and insnlist deviate. */
1332 if (insn.n > MAX_SAMPLES)
1333 insn.n = MAX_SAMPLES;
1334 if (insn.insn & INSN_MASK_WRITE) {
1335 if (copy_from_user(data,
1337 insn.n * sizeof(unsigned int))) {
1342 ret = parse_insn(dev, &insn, data, file);
1345 if (insn.insn & INSN_MASK_READ) {
1346 if (copy_to_user(insn.data,
1348 insn.n * sizeof(unsigned int))) {
1361 static int do_cmd_ioctl(struct comedi_device *dev,
1362 struct comedi_cmd __user *arg, void *file)
1364 struct comedi_cmd cmd;
1365 struct comedi_subdevice *s;
1366 struct comedi_async *async;
1368 unsigned int __user *user_chanlist;
1370 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1371 DPRINTK("bad cmd address\n");
1374 /* save user's chanlist pointer so it can be restored later */
1375 user_chanlist = (unsigned int __user *)cmd.chanlist;
1377 if (cmd.subdev >= dev->n_subdevices) {
1378 DPRINTK("%d no such subdevice\n", cmd.subdev);
1382 s = &dev->subdevices[cmd.subdev];
1385 if (s->type == COMEDI_SUBD_UNUSED) {
1386 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1390 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1391 DPRINTK("subdevice %i does not support commands\n",
1396 /* are we locked? (ioctl lock) */
1397 if (s->lock && s->lock != file) {
1398 DPRINTK("subdevice locked\n");
1404 DPRINTK("subdevice busy\n");
1408 /* make sure channel/gain list isn't too long */
1409 if (cmd.chanlist_len > s->len_chanlist) {
1410 DPRINTK("channel/gain list too long %u > %d\n",
1411 cmd.chanlist_len, s->len_chanlist);
1415 /* make sure channel/gain list isn't too short */
1416 if (cmd.chanlist_len < 1) {
1417 DPRINTK("channel/gain list too short %u < 1\n",
1423 async->cmd.data = NULL;
1424 /* load channel/gain list */
1425 async->cmd.chanlist = memdup_user(user_chanlist,
1426 async->cmd.chanlist_len * sizeof(int));
1427 if (IS_ERR(async->cmd.chanlist)) {
1428 ret = PTR_ERR(async->cmd.chanlist);
1429 DPRINTK("memdup_user failed with code %d\n", ret);
1433 /* make sure each element in channel/gain list is valid */
1434 ret = comedi_check_chanlist(s,
1435 async->cmd.chanlist_len,
1436 async->cmd.chanlist);
1438 DPRINTK("bad chanlist\n");
1442 ret = s->do_cmdtest(dev, s, &async->cmd);
1444 if (async->cmd.flags & TRIG_BOGUS || ret) {
1445 DPRINTK("test returned %d\n", ret);
1447 /* restore chanlist pointer before copying back */
1448 cmd.chanlist = (unsigned int __force *)user_chanlist;
1450 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1451 DPRINTK("fault writing cmd\n");
1459 if (!async->prealloc_bufsz) {
1461 DPRINTK("no buffer (?)\n");
1465 comedi_buf_reset(async);
1468 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1470 if (async->cmd.flags & TRIG_WAKE_EOS)
1471 async->cb_mask |= COMEDI_CB_EOS;
1473 comedi_set_subdevice_runflags(s, SRF_ERROR | SRF_RUNNING, 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 wake_up_interruptible(&s->async->wait_head);
1710 instructs driver to synchronize buffers
1722 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1725 struct comedi_subdevice *s;
1727 if (arg >= dev->n_subdevices)
1729 s = &dev->subdevices[arg];
1731 if (s->lock && s->lock != file)
1737 if (s->busy != file)
1741 return s->poll(dev, s);
1746 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1749 const unsigned minor = iminor(file_inode(file));
1750 struct comedi_device *dev = comedi_dev_from_minor(minor);
1756 mutex_lock(&dev->mutex);
1758 /* Device config is special, because it must work on
1759 * an unconfigured device. */
1760 if (cmd == COMEDI_DEVCONFIG) {
1761 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1762 /* Device config not appropriate on non-board minors. */
1766 rc = do_devconfig_ioctl(dev,
1767 (struct comedi_devconfig __user *)arg);
1770 dev->minor >= comedi_num_legacy_minors) {
1771 /* Successfully unconfigured a dynamically
1772 * allocated device. Try and remove it. */
1773 if (comedi_clear_board_dev(dev)) {
1774 mutex_unlock(&dev->mutex);
1775 comedi_free_board_dev(dev);
1783 if (!dev->attached) {
1784 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1790 case COMEDI_BUFCONFIG:
1791 rc = do_bufconfig_ioctl(dev,
1792 (struct comedi_bufconfig __user *)arg);
1794 case COMEDI_DEVINFO:
1795 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1798 case COMEDI_SUBDINFO:
1799 rc = do_subdinfo_ioctl(dev,
1800 (struct comedi_subdinfo __user *)arg,
1803 case COMEDI_CHANINFO:
1804 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1806 case COMEDI_RANGEINFO:
1807 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1809 case COMEDI_BUFINFO:
1810 rc = do_bufinfo_ioctl(dev,
1811 (struct comedi_bufinfo __user *)arg,
1815 rc = do_lock_ioctl(dev, arg, file);
1818 rc = do_unlock_ioctl(dev, arg, file);
1821 rc = do_cancel_ioctl(dev, arg, file);
1824 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1826 case COMEDI_CMDTEST:
1827 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1830 case COMEDI_INSNLIST:
1831 rc = do_insnlist_ioctl(dev,
1832 (struct comedi_insnlist __user *)arg,
1836 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1840 rc = do_poll_ioctl(dev, arg, file);
1848 mutex_unlock(&dev->mutex);
1852 static void comedi_vm_open(struct vm_area_struct *area)
1854 struct comedi_async *async;
1855 struct comedi_device *dev;
1857 async = area->vm_private_data;
1858 dev = async->subdevice->device;
1860 mutex_lock(&dev->mutex);
1861 async->mmap_count++;
1862 mutex_unlock(&dev->mutex);
1865 static void comedi_vm_close(struct vm_area_struct *area)
1867 struct comedi_async *async;
1868 struct comedi_device *dev;
1870 async = area->vm_private_data;
1871 dev = async->subdevice->device;
1873 mutex_lock(&dev->mutex);
1874 async->mmap_count--;
1875 mutex_unlock(&dev->mutex);
1878 static struct vm_operations_struct comedi_vm_ops = {
1879 .open = comedi_vm_open,
1880 .close = comedi_vm_close,
1883 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1885 const unsigned minor = iminor(file_inode(file));
1886 struct comedi_device *dev = comedi_dev_from_minor(minor);
1887 struct comedi_subdevice *s;
1888 struct comedi_async *async;
1889 unsigned long start = vma->vm_start;
1898 mutex_lock(&dev->mutex);
1900 if (!dev->attached) {
1901 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1906 if (vma->vm_flags & VM_WRITE)
1907 s = comedi_write_subdevice(dev, minor);
1909 s = comedi_read_subdevice(dev, minor);
1921 if (vma->vm_pgoff != 0) {
1922 DPRINTK("comedi: mmap() offset must be 0.\n");
1927 size = vma->vm_end - vma->vm_start;
1928 if (size > async->prealloc_bufsz) {
1932 if (size & (~PAGE_MASK)) {
1937 n_pages = size >> PAGE_SHIFT;
1938 for (i = 0; i < n_pages; ++i) {
1939 struct comedi_buf_page *buf = &async->buf_page_list[i];
1941 if (remap_pfn_range(vma, start,
1942 page_to_pfn(virt_to_page(buf->virt_addr)),
1943 PAGE_SIZE, PAGE_SHARED)) {
1950 vma->vm_ops = &comedi_vm_ops;
1951 vma->vm_private_data = async;
1953 async->mmap_count++;
1957 mutex_unlock(&dev->mutex);
1961 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1963 unsigned int mask = 0;
1964 const unsigned minor = iminor(file_inode(file));
1965 struct comedi_device *dev = comedi_dev_from_minor(minor);
1966 struct comedi_subdevice *s;
1971 mutex_lock(&dev->mutex);
1973 if (!dev->attached) {
1974 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1978 s = comedi_read_subdevice(dev, minor);
1979 if (s && s->async) {
1980 poll_wait(file, &s->async->wait_head, wait);
1981 if (!s->busy || !comedi_is_subdevice_running(s) ||
1982 comedi_buf_read_n_available(s->async) > 0)
1983 mask |= POLLIN | POLLRDNORM;
1986 s = comedi_write_subdevice(dev, minor);
1987 if (s && s->async) {
1988 unsigned int bps = bytes_per_sample(s->async->subdevice);
1990 poll_wait(file, &s->async->wait_head, wait);
1991 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1992 if (!s->busy || !comedi_is_subdevice_running(s) ||
1993 comedi_buf_write_n_allocated(s->async) >= bps)
1994 mask |= POLLOUT | POLLWRNORM;
1998 mutex_unlock(&dev->mutex);
2002 static ssize_t comedi_write(struct file *file, const char __user *buf,
2003 size_t nbytes, loff_t *offset)
2005 struct comedi_subdevice *s;
2006 struct comedi_async *async;
2007 int n, m, count = 0, retval = 0;
2008 DECLARE_WAITQUEUE(wait, current);
2009 const unsigned minor = iminor(file_inode(file));
2010 struct comedi_device *dev = comedi_dev_from_minor(minor);
2015 if (!dev->attached) {
2016 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2020 s = comedi_write_subdevice(dev, minor);
2021 if (!s || !s->async)
2026 if (!s->busy || !nbytes)
2028 if (s->busy != file)
2031 add_wait_queue(&async->wait_head, &wait);
2032 while (nbytes > 0 && !retval) {
2033 set_current_state(TASK_INTERRUPTIBLE);
2035 if (!comedi_is_subdevice_running(s)) {
2037 mutex_lock(&dev->mutex);
2038 if (comedi_is_subdevice_in_error(s))
2042 do_become_nonbusy(dev, s);
2043 mutex_unlock(&dev->mutex);
2051 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2052 m = async->prealloc_bufsz - async->buf_write_ptr;
2053 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2054 if (m > comedi_buf_write_n_allocated(async))
2055 m = comedi_buf_write_n_allocated(async);
2060 if (file->f_flags & O_NONBLOCK) {
2065 if (signal_pending(current)) {
2066 retval = -ERESTARTSYS;
2071 if (s->busy != file) {
2078 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2084 comedi_buf_write_free(async, n);
2090 break; /* makes device work like a pipe */
2092 set_current_state(TASK_RUNNING);
2093 remove_wait_queue(&async->wait_head, &wait);
2095 return count ? count : retval;
2098 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2101 struct comedi_subdevice *s;
2102 struct comedi_async *async;
2103 int n, m, count = 0, retval = 0;
2104 DECLARE_WAITQUEUE(wait, current);
2105 const unsigned minor = iminor(file_inode(file));
2106 struct comedi_device *dev = comedi_dev_from_minor(minor);
2111 if (!dev->attached) {
2112 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2116 s = comedi_read_subdevice(dev, minor);
2117 if (!s || !s->async)
2121 if (!s->busy || !nbytes)
2123 if (s->busy != file)
2126 add_wait_queue(&async->wait_head, &wait);
2127 while (nbytes > 0 && !retval) {
2128 set_current_state(TASK_INTERRUPTIBLE);
2132 m = comedi_buf_read_n_available(async);
2133 /* printk("%d available\n",m); */
2134 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2135 m = async->prealloc_bufsz - async->buf_read_ptr;
2136 /* printk("%d contiguous\n",m); */
2141 if (!comedi_is_subdevice_running(s)) {
2142 mutex_lock(&dev->mutex);
2143 do_become_nonbusy(dev, s);
2144 if (comedi_is_subdevice_in_error(s))
2148 mutex_unlock(&dev->mutex);
2151 if (file->f_flags & O_NONBLOCK) {
2156 if (signal_pending(current)) {
2157 retval = -ERESTARTSYS;
2164 if (s->busy != file) {
2170 m = copy_to_user(buf, async->prealloc_buf +
2171 async->buf_read_ptr, n);
2177 comedi_buf_read_alloc(async, n);
2178 comedi_buf_read_free(async, n);
2184 break; /* makes device work like a pipe */
2186 if (comedi_is_subdevice_idle(s)) {
2187 mutex_lock(&dev->mutex);
2188 if (async->buf_read_count - async->buf_write_count == 0)
2189 do_become_nonbusy(dev, s);
2190 mutex_unlock(&dev->mutex);
2192 set_current_state(TASK_RUNNING);
2193 remove_wait_queue(&async->wait_head, &wait);
2195 return count ? count : retval;
2198 static int comedi_open(struct inode *inode, struct file *file)
2200 const unsigned minor = iminor(inode);
2201 struct comedi_device *dev = comedi_dev_from_minor(minor);
2204 DPRINTK("invalid minor number\n");
2208 /* This is slightly hacky, but we want module autoloading
2210 * case: user opens device, attached -> ok
2211 * case: user opens device, unattached, !in_request_module -> autoload
2212 * case: user opens device, unattached, in_request_module -> fail
2213 * case: root opens device, attached -> ok
2214 * case: root opens device, unattached, in_request_module -> ok
2215 * (typically called from modprobe)
2216 * case: root opens device, unattached, !in_request_module -> autoload
2218 * The last could be changed to "-> ok", which would deny root
2221 mutex_lock(&dev->mutex);
2224 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2225 DPRINTK("in request module\n");
2226 mutex_unlock(&dev->mutex);
2229 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2232 dev->in_request_module = true;
2235 mutex_unlock(&dev->mutex);
2236 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2237 mutex_lock(&dev->mutex);
2240 dev->in_request_module = false;
2242 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2243 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2244 mutex_unlock(&dev->mutex);
2248 __module_get(THIS_MODULE);
2250 if (dev->attached) {
2251 if (!try_module_get(dev->driver->module)) {
2252 module_put(THIS_MODULE);
2253 mutex_unlock(&dev->mutex);
2258 if (dev->attached && dev->use_count == 0 && dev->open) {
2259 int rc = dev->open(dev);
2261 module_put(dev->driver->module);
2262 module_put(THIS_MODULE);
2263 mutex_unlock(&dev->mutex);
2270 mutex_unlock(&dev->mutex);
2275 static int comedi_fasync(int fd, struct file *file, int on)
2277 const unsigned minor = iminor(file_inode(file));
2278 struct comedi_device *dev = comedi_dev_from_minor(minor);
2283 return fasync_helper(fd, file, on, &dev->async_queue);
2286 static int comedi_close(struct inode *inode, struct file *file)
2288 const unsigned minor = iminor(inode);
2289 struct comedi_device *dev = comedi_dev_from_minor(minor);
2290 struct comedi_subdevice *s = NULL;
2296 mutex_lock(&dev->mutex);
2298 if (dev->subdevices) {
2299 for (i = 0; i < dev->n_subdevices; i++) {
2300 s = &dev->subdevices[i];
2302 if (s->busy == file)
2304 if (s->lock == file)
2308 if (dev->attached && dev->use_count == 1 && dev->close)
2311 module_put(THIS_MODULE);
2313 module_put(dev->driver->module);
2317 mutex_unlock(&dev->mutex);
2322 static const struct file_operations comedi_fops = {
2323 .owner = THIS_MODULE,
2324 .unlocked_ioctl = comedi_unlocked_ioctl,
2325 .compat_ioctl = comedi_compat_ioctl,
2326 .open = comedi_open,
2327 .release = comedi_close,
2328 .read = comedi_read,
2329 .write = comedi_write,
2330 .mmap = comedi_mmap,
2331 .poll = comedi_poll,
2332 .fasync = comedi_fasync,
2333 .llseek = noop_llseek,
2336 void comedi_error(const struct comedi_device *dev, const char *s)
2338 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2340 EXPORT_SYMBOL_GPL(comedi_error);
2342 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2344 struct comedi_async *async = s->async;
2345 unsigned runflags = 0;
2346 unsigned runflags_mask = 0;
2348 /* DPRINTK("comedi_event 0x%x\n",mask); */
2350 if (!comedi_is_subdevice_running(s))
2354 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2355 COMEDI_CB_OVERFLOW)) {
2356 runflags_mask |= SRF_RUNNING;
2358 /* remember if an error event has occurred, so an error
2359 * can be returned the next time the user does a read() */
2360 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2361 runflags_mask |= SRF_ERROR;
2362 runflags |= SRF_ERROR;
2364 if (runflags_mask) {
2365 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2366 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2369 if (async->cb_mask & s->async->events) {
2370 wake_up_interruptible(&async->wait_head);
2371 if (s->subdev_flags & SDF_CMD_READ)
2372 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2373 if (s->subdev_flags & SDF_CMD_WRITE)
2374 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2376 s->async->events = 0;
2378 EXPORT_SYMBOL_GPL(comedi_event);
2380 /* Note: the ->mutex is pre-locked on successful return */
2381 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2383 struct comedi_device *dev;
2384 struct device *csdev;
2387 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2389 return ERR_PTR(-ENOMEM);
2390 comedi_device_init(dev);
2391 comedi_set_hw_dev(dev, hardware_device);
2392 mutex_lock(&dev->mutex);
2393 mutex_lock(&comedi_board_minor_table_lock);
2394 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2395 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2396 if (comedi_board_minor_table[i] == NULL) {
2397 comedi_board_minor_table[i] = dev;
2401 mutex_unlock(&comedi_board_minor_table_lock);
2402 if (i == COMEDI_NUM_BOARD_MINORS) {
2403 mutex_unlock(&dev->mutex);
2404 comedi_device_cleanup(dev);
2406 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2407 return ERR_PTR(-EBUSY);
2410 csdev = device_create(comedi_class, hardware_device,
2411 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2413 dev->class_dev = csdev;
2415 /* Note: dev->mutex needs to be unlocked by the caller. */
2419 static void comedi_free_board_minor(unsigned minor)
2421 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2422 comedi_free_board_dev(comedi_clear_board_minor(minor));
2425 void comedi_release_hardware_device(struct device *hardware_device)
2428 struct comedi_device *dev;
2430 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2432 mutex_lock(&comedi_board_minor_table_lock);
2433 dev = comedi_board_minor_table[minor];
2434 if (dev && dev->hw_dev == hardware_device) {
2435 comedi_board_minor_table[minor] = NULL;
2436 mutex_unlock(&comedi_board_minor_table_lock);
2437 comedi_free_board_dev(dev);
2440 mutex_unlock(&comedi_board_minor_table_lock);
2444 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2446 struct comedi_device *dev = s->device;
2447 struct device *csdev;
2450 mutex_lock(&comedi_subdevice_minor_table_lock);
2451 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2452 if (comedi_subdevice_minor_table[i] == NULL) {
2453 comedi_subdevice_minor_table[i] = s;
2457 mutex_unlock(&comedi_subdevice_minor_table_lock);
2458 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2459 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2462 i += COMEDI_NUM_BOARD_MINORS;
2464 csdev = device_create(comedi_class, dev->class_dev,
2465 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2466 dev->minor, s->index);
2468 s->class_dev = csdev;
2473 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2482 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2483 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2485 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2486 mutex_lock(&comedi_subdevice_minor_table_lock);
2487 if (s == comedi_subdevice_minor_table[i])
2488 comedi_subdevice_minor_table[i] = NULL;
2489 mutex_unlock(&comedi_subdevice_minor_table_lock);
2491 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2492 s->class_dev = NULL;
2496 static void comedi_cleanup_board_minors(void)
2500 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2501 comedi_free_board_minor(i);
2504 static int __init comedi_init(void)
2509 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2511 if (comedi_num_legacy_minors < 0 ||
2512 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2513 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2514 COMEDI_NUM_BOARD_MINORS);
2518 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2519 COMEDI_NUM_MINORS, "comedi");
2522 cdev_init(&comedi_cdev, &comedi_fops);
2523 comedi_cdev.owner = THIS_MODULE;
2524 kobject_set_name(&comedi_cdev.kobj, "comedi");
2525 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2526 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2530 comedi_class = class_create(THIS_MODULE, "comedi");
2531 if (IS_ERR(comedi_class)) {
2532 pr_err("comedi: failed to create class\n");
2533 cdev_del(&comedi_cdev);
2534 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2536 return PTR_ERR(comedi_class);
2539 comedi_class->dev_groups = comedi_dev_groups;
2541 /* XXX requires /proc interface */
2544 /* create devices files for legacy/manual use */
2545 for (i = 0; i < comedi_num_legacy_minors; i++) {
2546 struct comedi_device *dev;
2547 dev = comedi_alloc_board_minor(NULL);
2549 comedi_cleanup_board_minors();
2550 cdev_del(&comedi_cdev);
2551 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2553 return PTR_ERR(dev);
2555 /* comedi_alloc_board_minor() locked the mutex */
2556 mutex_unlock(&dev->mutex);
2562 module_init(comedi_init);
2564 static void __exit comedi_cleanup(void)
2568 comedi_cleanup_board_minors();
2569 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2570 BUG_ON(comedi_board_minor_table[i]);
2571 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2572 BUG_ON(comedi_subdevice_minor_table[i]);
2574 class_destroy(comedi_class);
2575 cdev_del(&comedi_cdev);
2576 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2578 comedi_proc_cleanup();
2580 module_exit(comedi_cleanup);
2582 MODULE_AUTHOR("http://www.comedi.org");
2583 MODULE_DESCRIPTION("Comedi core module");
2584 MODULE_LICENSE("GPL");