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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "comedi_compat32.h"
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/fcntl.h>
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/kmod.h>
38 #include <linux/poll.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/vmalloc.h>
43 #include "comedidev.h"
44 #include <linux/cdev.h>
45 #include <linux/stat.h>
48 #include <linux/uaccess.h>
50 #include "comedi_internal.h"
52 #define COMEDI_NUM_MINORS 0x100
53 #define COMEDI_NUM_SUBDEVICE_MINORS \
54 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
56 #ifdef CONFIG_COMEDI_DEBUG
58 EXPORT_SYMBOL_GPL(comedi_debug);
59 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
60 MODULE_PARM_DESC(comedi_debug,
61 "enable comedi core and driver debugging if non-zero (default 0)"
65 static int comedi_num_legacy_minors;
66 module_param(comedi_num_legacy_minors, int, S_IRUGO);
67 MODULE_PARM_DESC(comedi_num_legacy_minors,
68 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
71 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
72 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
73 MODULE_PARM_DESC(comedi_default_buf_size_kb,
74 "default asynchronous buffer size in KiB (default "
75 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
77 unsigned int comedi_default_buf_maxsize_kb
78 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
79 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
80 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
81 "default maximum size of asynchronous buffer in KiB (default "
82 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
84 static DEFINE_MUTEX(comedi_board_minor_table_lock);
85 static struct comedi_device
86 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
88 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
89 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
90 static struct comedi_subdevice
91 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
93 static struct class *comedi_class;
94 static struct cdev comedi_cdev;
96 static void comedi_device_init(struct comedi_device *dev)
98 spin_lock_init(&dev->spinlock);
99 mutex_init(&dev->mutex);
103 static void comedi_device_cleanup(struct comedi_device *dev)
105 struct module *driver_module = NULL;
109 mutex_lock(&dev->mutex);
111 driver_module = dev->driver->module;
112 comedi_device_detach(dev);
113 while (dev->use_count > 0) {
115 module_put(driver_module);
116 module_put(THIS_MODULE);
119 mutex_unlock(&dev->mutex);
120 mutex_destroy(&dev->mutex);
123 static bool comedi_clear_board_dev(struct comedi_device *dev)
125 unsigned int i = dev->minor;
126 bool cleared = false;
128 mutex_lock(&comedi_board_minor_table_lock);
129 if (dev == comedi_board_minor_table[i]) {
130 comedi_board_minor_table[i] = NULL;
133 mutex_unlock(&comedi_board_minor_table_lock);
137 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
139 struct comedi_device *dev;
141 mutex_lock(&comedi_board_minor_table_lock);
142 dev = comedi_board_minor_table[minor];
143 comedi_board_minor_table[minor] = NULL;
144 mutex_unlock(&comedi_board_minor_table_lock);
148 static void comedi_free_board_dev(struct comedi_device *dev)
151 if (dev->class_dev) {
152 device_destroy(comedi_class,
153 MKDEV(COMEDI_MAJOR, dev->minor));
155 comedi_device_cleanup(dev);
160 static struct comedi_subdevice
161 *comedi_subdevice_from_minor(unsigned minor)
163 struct comedi_subdevice *s;
164 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
166 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
167 mutex_lock(&comedi_subdevice_minor_table_lock);
168 s = comedi_subdevice_minor_table[i];
169 mutex_unlock(&comedi_subdevice_minor_table_lock);
173 static struct comedi_device *comedi_dev_from_board_minor(unsigned minor)
175 struct comedi_device *dev;
177 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
178 mutex_lock(&comedi_board_minor_table_lock);
179 dev = comedi_board_minor_table[minor];
180 mutex_unlock(&comedi_board_minor_table_lock);
184 static struct comedi_device *comedi_dev_from_subdevice_minor(unsigned minor)
186 struct comedi_subdevice *s;
188 s = comedi_subdevice_from_minor(minor);
189 return s ? s->device : NULL;
192 struct comedi_device *comedi_dev_from_minor(unsigned minor)
194 if (minor < COMEDI_NUM_BOARD_MINORS)
195 return comedi_dev_from_board_minor(minor);
197 return comedi_dev_from_subdevice_minor(minor);
199 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
201 static struct comedi_subdevice *
202 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
204 struct comedi_subdevice *s;
206 if (minor >= COMEDI_NUM_BOARD_MINORS) {
207 s = comedi_subdevice_from_minor(minor);
208 if (!s || s->device != dev)
210 if (s->subdev_flags & SDF_CMD_READ)
213 return dev->read_subdev;
216 static struct comedi_subdevice *
217 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
219 struct comedi_subdevice *s;
221 if (minor >= COMEDI_NUM_BOARD_MINORS) {
222 s = comedi_subdevice_from_minor(minor);
223 if (!s || s->device != dev)
225 if (s->subdev_flags & SDF_CMD_WRITE)
228 return dev->write_subdev;
231 static int resize_async_buffer(struct comedi_device *dev,
232 struct comedi_subdevice *s,
233 struct comedi_async *async, unsigned new_size)
237 if (new_size > async->max_bufsize)
241 DPRINTK("subdevice is busy, cannot resize buffer\n");
244 if (async->mmap_count) {
245 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
249 /* make sure buffer is an integral number of pages
251 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
253 retval = comedi_buf_alloc(dev, s, new_size);
258 retval = s->buf_change(dev, s, new_size);
263 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
264 dev->minor, s->index, async->prealloc_bufsz);
268 /* sysfs attribute files */
270 static ssize_t show_max_read_buffer_kb(struct device *csdev,
271 struct device_attribute *attr, char *buf)
273 unsigned int minor = MINOR(csdev->devt);
274 struct comedi_device *dev;
275 struct comedi_subdevice *s;
276 unsigned int size = 0;
278 dev = comedi_dev_from_minor(minor);
282 mutex_lock(&dev->mutex);
283 s = comedi_read_subdevice(dev, minor);
284 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
285 size = s->async->max_bufsize / 1024;
286 mutex_unlock(&dev->mutex);
288 return snprintf(buf, PAGE_SIZE, "%i\n", size);
291 static ssize_t store_max_read_buffer_kb(struct device *csdev,
292 struct device_attribute *attr,
293 const char *buf, size_t count)
295 unsigned int minor = MINOR(csdev->devt);
296 struct comedi_device *dev;
297 struct comedi_subdevice *s;
301 err = kstrtouint(buf, 10, &size);
304 if (size > (UINT_MAX / 1024))
308 dev = comedi_dev_from_minor(minor);
312 mutex_lock(&dev->mutex);
313 s = comedi_read_subdevice(dev, minor);
314 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
315 s->async->max_bufsize = size;
318 mutex_unlock(&dev->mutex);
320 return err ? err : count;
323 static ssize_t show_read_buffer_kb(struct device *csdev,
324 struct device_attribute *attr, char *buf)
326 unsigned int minor = MINOR(csdev->devt);
327 struct comedi_device *dev;
328 struct comedi_subdevice *s;
329 unsigned int size = 0;
331 dev = comedi_dev_from_minor(minor);
335 mutex_lock(&dev->mutex);
336 s = comedi_read_subdevice(dev, minor);
337 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
338 size = s->async->prealloc_bufsz / 1024;
339 mutex_unlock(&dev->mutex);
341 return snprintf(buf, PAGE_SIZE, "%i\n", size);
344 static ssize_t store_read_buffer_kb(struct device *csdev,
345 struct device_attribute *attr,
346 const char *buf, size_t count)
348 unsigned int minor = MINOR(csdev->devt);
349 struct comedi_device *dev;
350 struct comedi_subdevice *s;
354 err = kstrtouint(buf, 10, &size);
357 if (size > (UINT_MAX / 1024))
361 dev = comedi_dev_from_minor(minor);
365 mutex_lock(&dev->mutex);
366 s = comedi_read_subdevice(dev, minor);
367 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
368 err = resize_async_buffer(dev, s, s->async, size);
371 mutex_unlock(&dev->mutex);
373 return err ? err : count;
376 static ssize_t show_max_write_buffer_kb(struct device *csdev,
377 struct device_attribute *attr,
380 unsigned int minor = MINOR(csdev->devt);
381 struct comedi_device *dev;
382 struct comedi_subdevice *s;
383 unsigned int size = 0;
385 dev = comedi_dev_from_minor(minor);
389 mutex_lock(&dev->mutex);
390 s = comedi_write_subdevice(dev, minor);
391 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
392 size = s->async->max_bufsize / 1024;
393 mutex_unlock(&dev->mutex);
395 return snprintf(buf, PAGE_SIZE, "%i\n", size);
398 static ssize_t store_max_write_buffer_kb(struct device *csdev,
399 struct device_attribute *attr,
400 const char *buf, size_t count)
402 unsigned int minor = MINOR(csdev->devt);
403 struct comedi_device *dev;
404 struct comedi_subdevice *s;
408 err = kstrtouint(buf, 10, &size);
411 if (size > (UINT_MAX / 1024))
415 dev = comedi_dev_from_minor(minor);
419 mutex_lock(&dev->mutex);
420 s = comedi_write_subdevice(dev, minor);
421 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
422 s->async->max_bufsize = size;
425 mutex_unlock(&dev->mutex);
427 return err ? err : count;
430 static ssize_t show_write_buffer_kb(struct device *csdev,
431 struct device_attribute *attr, char *buf)
433 unsigned int minor = MINOR(csdev->devt);
434 struct comedi_device *dev;
435 struct comedi_subdevice *s;
436 unsigned int size = 0;
438 dev = comedi_dev_from_minor(minor);
442 mutex_lock(&dev->mutex);
443 s = comedi_write_subdevice(dev, minor);
444 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
445 size = s->async->prealloc_bufsz / 1024;
446 mutex_unlock(&dev->mutex);
448 return snprintf(buf, PAGE_SIZE, "%i\n", size);
451 static ssize_t store_write_buffer_kb(struct device *csdev,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
455 unsigned int minor = MINOR(csdev->devt);
456 struct comedi_device *dev;
457 struct comedi_subdevice *s;
461 err = kstrtouint(buf, 10, &size);
464 if (size > (UINT_MAX / 1024))
468 dev = comedi_dev_from_minor(minor);
472 mutex_lock(&dev->mutex);
473 s = comedi_write_subdevice(dev, minor);
474 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
475 err = resize_async_buffer(dev, s, s->async, size);
478 mutex_unlock(&dev->mutex);
480 return err ? err : count;
483 static struct device_attribute comedi_dev_attrs[] = {
484 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
485 show_max_read_buffer_kb, store_max_read_buffer_kb),
486 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
487 show_read_buffer_kb, store_read_buffer_kb),
488 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
489 show_max_write_buffer_kb, store_max_write_buffer_kb),
490 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
491 show_write_buffer_kb, store_write_buffer_kb),
495 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
496 unsigned mask, unsigned bits)
500 spin_lock_irqsave(&s->spin_lock, flags);
501 s->runflags &= ~mask;
502 s->runflags |= (bits & mask);
503 spin_unlock_irqrestore(&s->spin_lock, flags);
506 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
511 spin_lock_irqsave(&s->spin_lock, flags);
512 runflags = s->runflags;
513 spin_unlock_irqrestore(&s->spin_lock, flags);
517 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
519 unsigned runflags = comedi_get_subdevice_runflags(s);
521 return (runflags & SRF_RUNNING) ? true : false;
523 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
525 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
527 unsigned runflags = comedi_get_subdevice_runflags(s);
529 return (runflags & SRF_ERROR) ? true : false;
532 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
534 unsigned runflags = comedi_get_subdevice_runflags(s);
536 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
540 This function restores a subdevice to an idle state.
542 static void do_become_nonbusy(struct comedi_device *dev,
543 struct comedi_subdevice *s)
545 struct comedi_async *async = s->async;
547 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
549 comedi_buf_reset(async);
550 async->inttrig = NULL;
551 kfree(async->cmd.chanlist);
552 async->cmd.chanlist = NULL;
554 dev_err(dev->class_dev,
555 "BUG: (?) do_become_nonbusy called with async=NULL\n");
561 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
565 if (comedi_is_subdevice_running(s) && s->cancel)
566 ret = s->cancel(dev, s);
568 do_become_nonbusy(dev, s);
573 static int is_device_busy(struct comedi_device *dev)
575 struct comedi_subdevice *s;
581 for (i = 0; i < dev->n_subdevices; i++) {
582 s = &dev->subdevices[i];
585 if (s->async && s->async->mmap_count)
597 pointer to devconfig structure
600 devconfig structure at arg
605 static int do_devconfig_ioctl(struct comedi_device *dev,
606 struct comedi_devconfig __user *arg)
608 struct comedi_devconfig it;
610 if (!capable(CAP_SYS_ADMIN))
614 if (is_device_busy(dev))
617 struct module *driver_module = dev->driver->module;
618 comedi_device_detach(dev);
619 module_put(driver_module);
624 if (copy_from_user(&it, arg, sizeof(it)))
627 it.board_name[COMEDI_NAMELEN - 1] = 0;
629 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
630 dev_warn(dev->class_dev,
631 "comedi_config --init_data is deprecated\n");
635 if (dev->minor >= comedi_num_legacy_minors)
636 /* don't re-use dynamically allocated comedi devices */
639 /* This increments the driver module count on success. */
640 return comedi_device_attach(dev, &it);
645 buffer configuration ioctl
648 pointer to bufconfig structure
654 modified bufconfig at arg
657 static int do_bufconfig_ioctl(struct comedi_device *dev,
658 struct comedi_bufconfig __user *arg)
660 struct comedi_bufconfig bc;
661 struct comedi_async *async;
662 struct comedi_subdevice *s;
665 if (copy_from_user(&bc, arg, sizeof(bc)))
668 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
671 s = &dev->subdevices[bc.subdevice];
675 DPRINTK("subdevice does not have async capability\n");
681 if (bc.maximum_size) {
682 if (!capable(CAP_SYS_ADMIN))
685 async->max_bufsize = bc.maximum_size;
689 retval = resize_async_buffer(dev, s, async, bc.size);
694 bc.size = async->prealloc_bufsz;
695 bc.maximum_size = async->max_bufsize;
698 if (copy_to_user(arg, &bc, sizeof(bc)))
709 pointer to devinfo structure
718 static int do_devinfo_ioctl(struct comedi_device *dev,
719 struct comedi_devinfo __user *arg,
722 const unsigned minor = iminor(file_inode(file));
723 struct comedi_subdevice *s;
724 struct comedi_devinfo devinfo;
726 memset(&devinfo, 0, sizeof(devinfo));
728 /* fill devinfo structure */
729 devinfo.version_code = COMEDI_VERSION_CODE;
730 devinfo.n_subdevs = dev->n_subdevices;
731 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
732 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
734 s = comedi_read_subdevice(dev, minor);
736 devinfo.read_subdevice = s->index;
738 devinfo.read_subdevice = -1;
740 s = comedi_write_subdevice(dev, minor);
742 devinfo.write_subdevice = s->index;
744 devinfo.write_subdevice = -1;
746 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
757 pointer to array of subdevice info structures
763 array of subdevice info structures at arg
766 static int do_subdinfo_ioctl(struct comedi_device *dev,
767 struct comedi_subdinfo __user *arg, void *file)
770 struct comedi_subdinfo *tmp, *us;
771 struct comedi_subdevice *s;
773 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
777 /* fill subdinfo structs */
778 for (i = 0; i < dev->n_subdevices; i++) {
779 s = &dev->subdevices[i];
783 us->n_chan = s->n_chan;
784 us->subd_flags = s->subdev_flags;
785 if (comedi_is_subdevice_running(s))
786 us->subd_flags |= SDF_RUNNING;
787 #define TIMER_nanosec 5 /* backwards compatibility */
788 us->timer_type = TIMER_nanosec;
789 us->len_chanlist = s->len_chanlist;
790 us->maxdata = s->maxdata;
791 if (s->range_table) {
793 (i << 24) | (0 << 16) | (s->range_table->length);
795 us->range_type = 0; /* XXX */
797 us->flags = s->flags;
800 us->subd_flags |= SDF_BUSY;
802 us->subd_flags |= SDF_BUSY_OWNER;
804 us->subd_flags |= SDF_LOCKED;
806 us->subd_flags |= SDF_LOCK_OWNER;
807 if (!s->maxdata && s->maxdata_list)
808 us->subd_flags |= SDF_MAXDATA;
810 us->subd_flags |= SDF_FLAGS;
811 if (s->range_table_list)
812 us->subd_flags |= SDF_RANGETYPE;
814 us->subd_flags |= SDF_CMD;
816 if (s->insn_bits != &insn_inval)
817 us->insn_bits_support = COMEDI_SUPPORTED;
819 us->insn_bits_support = COMEDI_UNSUPPORTED;
821 us->settling_time_0 = s->settling_time_0;
824 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
828 return ret ? -EFAULT : 0;
836 pointer to chaninfo structure
839 chaninfo structure at arg
842 arrays at elements of chaninfo structure
845 static int do_chaninfo_ioctl(struct comedi_device *dev,
846 struct comedi_chaninfo __user *arg)
848 struct comedi_subdevice *s;
849 struct comedi_chaninfo it;
851 if (copy_from_user(&it, arg, sizeof(it)))
854 if (it.subdev >= dev->n_subdevices)
856 s = &dev->subdevices[it.subdev];
858 if (it.maxdata_list) {
859 if (s->maxdata || !s->maxdata_list)
861 if (copy_to_user(it.maxdata_list, s->maxdata_list,
862 s->n_chan * sizeof(unsigned int)))
869 if (copy_to_user(it.flaglist, s->flaglist,
870 s->n_chan * sizeof(unsigned int)))
877 if (!s->range_table_list)
879 for (i = 0; i < s->n_chan; i++) {
882 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
883 (s->range_table_list[i]->length);
884 if (put_user(x, it.rangelist + i))
888 if (copy_to_user(it.rangelist, s->range_type_list,
889 s->n_chan * sizeof(unsigned int)))
899 buffer information ioctl
902 pointer to bufinfo structure
908 modified bufinfo at arg
911 static int do_bufinfo_ioctl(struct comedi_device *dev,
912 struct comedi_bufinfo __user *arg, void *file)
914 struct comedi_bufinfo bi;
915 struct comedi_subdevice *s;
916 struct comedi_async *async;
918 if (copy_from_user(&bi, arg, sizeof(bi)))
921 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
924 s = &dev->subdevices[bi.subdevice];
926 if (s->lock && s->lock != file)
932 DPRINTK("subdevice does not have async capability\n");
933 bi.buf_write_ptr = 0;
935 bi.buf_write_count = 0;
936 bi.buf_read_count = 0;
938 bi.bytes_written = 0;
943 bi.bytes_written = 0;
944 goto copyback_position;
949 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
950 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
951 comedi_buf_read_free(async, bi.bytes_read);
953 if (comedi_is_subdevice_idle(s) &&
954 async->buf_write_count == async->buf_read_count) {
955 do_become_nonbusy(dev, s);
959 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
961 comedi_buf_write_alloc(async, bi.bytes_written);
962 comedi_buf_write_free(async, bi.bytes_written);
966 bi.buf_write_count = async->buf_write_count;
967 bi.buf_write_ptr = async->buf_write_ptr;
968 bi.buf_read_count = async->buf_read_count;
969 bi.buf_read_ptr = async->buf_read_ptr;
972 if (copy_to_user(arg, &bi, sizeof(bi)))
978 static int check_insn_config_length(struct comedi_insn *insn,
985 case INSN_CONFIG_DIO_OUTPUT:
986 case INSN_CONFIG_DIO_INPUT:
987 case INSN_CONFIG_DISARM:
988 case INSN_CONFIG_RESET:
992 case INSN_CONFIG_ARM:
993 case INSN_CONFIG_DIO_QUERY:
994 case INSN_CONFIG_BLOCK_SIZE:
995 case INSN_CONFIG_FILTER:
996 case INSN_CONFIG_SERIAL_CLOCK:
997 case INSN_CONFIG_BIDIRECTIONAL_DATA:
998 case INSN_CONFIG_ALT_SOURCE:
999 case INSN_CONFIG_SET_COUNTER_MODE:
1000 case INSN_CONFIG_8254_READ_STATUS:
1001 case INSN_CONFIG_SET_ROUTING:
1002 case INSN_CONFIG_GET_ROUTING:
1003 case INSN_CONFIG_GET_PWM_STATUS:
1004 case INSN_CONFIG_PWM_SET_PERIOD:
1005 case INSN_CONFIG_PWM_GET_PERIOD:
1009 case INSN_CONFIG_SET_GATE_SRC:
1010 case INSN_CONFIG_GET_GATE_SRC:
1011 case INSN_CONFIG_SET_CLOCK_SRC:
1012 case INSN_CONFIG_GET_CLOCK_SRC:
1013 case INSN_CONFIG_SET_OTHER_SRC:
1014 case INSN_CONFIG_GET_COUNTER_STATUS:
1015 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1016 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1017 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1021 case INSN_CONFIG_PWM_OUTPUT:
1022 case INSN_CONFIG_ANALOG_TRIG:
1026 case INSN_CONFIG_DIGITAL_TRIG:
1030 /* by default we allow the insn since we don't have checks for
1031 * all possible cases yet */
1033 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1035 pr_warn("comedi: Add a check to %s in %s.\n",
1036 __func__, __FILE__);
1037 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1043 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1044 unsigned int *data, void *file)
1046 struct comedi_subdevice *s;
1050 if (insn->insn & INSN_MASK_SPECIAL) {
1051 /* a non-subdevice instruction */
1053 switch (insn->insn) {
1063 do_gettimeofday(&tv);
1064 data[0] = tv.tv_sec;
1065 data[1] = tv.tv_usec;
1071 if (insn->n != 1 || data[0] >= 100000) {
1075 udelay(data[0] / 1000);
1083 if (insn->subdev >= dev->n_subdevices) {
1084 DPRINTK("%d not usable subdevice\n",
1089 s = &dev->subdevices[insn->subdev];
1091 DPRINTK("no async\n");
1095 if (!s->async->inttrig) {
1096 DPRINTK("no inttrig\n");
1100 ret = s->async->inttrig(dev, s, data[0]);
1105 DPRINTK("invalid insn\n");
1110 /* a subdevice instruction */
1111 unsigned int maxdata;
1113 if (insn->subdev >= dev->n_subdevices) {
1114 DPRINTK("subdevice %d out of range\n", insn->subdev);
1118 s = &dev->subdevices[insn->subdev];
1120 if (s->type == COMEDI_SUBD_UNUSED) {
1121 DPRINTK("%d not usable subdevice\n", insn->subdev);
1126 /* are we locked? (ioctl lock) */
1127 if (s->lock && s->lock != file) {
1128 DPRINTK("device locked\n");
1133 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1136 DPRINTK("bad chanspec\n");
1144 /* This looks arbitrary. It is. */
1145 s->busy = &parse_insn;
1146 switch (insn->insn) {
1148 ret = s->insn_read(dev, s, insn, data);
1151 maxdata = s->maxdata_list
1152 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1154 for (i = 0; i < insn->n; ++i) {
1155 if (data[i] > maxdata) {
1157 DPRINTK("bad data value(s)\n");
1162 ret = s->insn_write(dev, s, insn, data);
1168 /* Most drivers ignore the base channel in
1169 * insn->chanspec. Fix this here if
1170 * the subdevice has <= 32 channels. */
1172 unsigned int orig_mask;
1174 orig_mask = data[0];
1175 if (s->n_chan <= 32) {
1176 shift = CR_CHAN(insn->chanspec);
1184 ret = s->insn_bits(dev, s, insn, data);
1185 data[0] = orig_mask;
1191 ret = check_insn_config_length(insn, data);
1194 ret = s->insn_config(dev, s, insn, data);
1210 * synchronous instructions
1213 * pointer to sync cmd structure
1216 * sync cmd struct at arg
1223 /* arbitrary limits */
1224 #define MAX_SAMPLES 256
1225 static int do_insnlist_ioctl(struct comedi_device *dev,
1226 struct comedi_insnlist __user *arg, void *file)
1228 struct comedi_insnlist insnlist;
1229 struct comedi_insn *insns = NULL;
1230 unsigned int *data = NULL;
1234 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1237 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1239 DPRINTK("kmalloc failed\n");
1244 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1246 DPRINTK("kmalloc failed\n");
1251 if (copy_from_user(insns, insnlist.insns,
1252 sizeof(*insns) * insnlist.n_insns)) {
1253 DPRINTK("copy_from_user failed\n");
1258 for (i = 0; i < insnlist.n_insns; i++) {
1259 if (insns[i].n > MAX_SAMPLES) {
1260 DPRINTK("number of samples too large\n");
1264 if (insns[i].insn & INSN_MASK_WRITE) {
1265 if (copy_from_user(data, insns[i].data,
1266 insns[i].n * sizeof(unsigned int))) {
1267 DPRINTK("copy_from_user failed\n");
1272 ret = parse_insn(dev, insns + i, data, file);
1275 if (insns[i].insn & INSN_MASK_READ) {
1276 if (copy_to_user(insns[i].data, data,
1277 insns[i].n * sizeof(unsigned int))) {
1278 DPRINTK("copy_to_user failed\n");
1298 * synchronous instructions
1304 * struct comedi_insn struct at arg
1310 static int do_insn_ioctl(struct comedi_device *dev,
1311 struct comedi_insn __user *arg, void *file)
1313 struct comedi_insn insn;
1314 unsigned int *data = NULL;
1317 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1323 if (copy_from_user(&insn, arg, sizeof(insn))) {
1328 /* This is where the behavior of insn and insnlist deviate. */
1329 if (insn.n > MAX_SAMPLES)
1330 insn.n = MAX_SAMPLES;
1331 if (insn.insn & INSN_MASK_WRITE) {
1332 if (copy_from_user(data,
1334 insn.n * sizeof(unsigned int))) {
1339 ret = parse_insn(dev, &insn, data, file);
1342 if (insn.insn & INSN_MASK_READ) {
1343 if (copy_to_user(insn.data,
1345 insn.n * sizeof(unsigned int))) {
1358 static int do_cmd_ioctl(struct comedi_device *dev,
1359 struct comedi_cmd __user *arg, void *file)
1361 struct comedi_cmd cmd;
1362 struct comedi_subdevice *s;
1363 struct comedi_async *async;
1365 unsigned int __user *user_chanlist;
1367 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1368 DPRINTK("bad cmd address\n");
1371 /* save user's chanlist pointer so it can be restored later */
1372 user_chanlist = (unsigned int __user *)cmd.chanlist;
1374 if (cmd.subdev >= dev->n_subdevices) {
1375 DPRINTK("%d no such subdevice\n", cmd.subdev);
1379 s = &dev->subdevices[cmd.subdev];
1382 if (s->type == COMEDI_SUBD_UNUSED) {
1383 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1387 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1388 DPRINTK("subdevice %i does not support commands\n",
1393 /* are we locked? (ioctl lock) */
1394 if (s->lock && s->lock != file) {
1395 DPRINTK("subdevice locked\n");
1401 DPRINTK("subdevice busy\n");
1405 /* make sure channel/gain list isn't too long */
1406 if (cmd.chanlist_len > s->len_chanlist) {
1407 DPRINTK("channel/gain list too long %u > %d\n",
1408 cmd.chanlist_len, s->len_chanlist);
1412 /* make sure channel/gain list isn't too short */
1413 if (cmd.chanlist_len < 1) {
1414 DPRINTK("channel/gain list too short %u < 1\n",
1420 async->cmd.data = NULL;
1421 /* load channel/gain list */
1422 async->cmd.chanlist =
1423 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1424 if (!async->cmd.chanlist) {
1425 DPRINTK("allocation failed\n");
1429 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1430 async->cmd.chanlist_len * sizeof(int))) {
1431 DPRINTK("fault reading chanlist\n");
1436 /* make sure each element in channel/gain list is valid */
1437 ret = comedi_check_chanlist(s,
1438 async->cmd.chanlist_len,
1439 async->cmd.chanlist);
1441 DPRINTK("bad chanlist\n");
1445 ret = s->do_cmdtest(dev, s, &async->cmd);
1447 if (async->cmd.flags & TRIG_BOGUS || ret) {
1448 DPRINTK("test returned %d\n", ret);
1450 /* restore chanlist pointer before copying back */
1451 cmd.chanlist = (unsigned int __force *)user_chanlist;
1453 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1454 DPRINTK("fault writing cmd\n");
1462 if (!async->prealloc_bufsz) {
1464 DPRINTK("no buffer (?)\n");
1468 comedi_buf_reset(async);
1471 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1473 if (async->cmd.flags & TRIG_WAKE_EOS)
1474 async->cb_mask |= COMEDI_CB_EOS;
1476 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1478 /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
1479 * comedi_read() or comedi_write() */
1481 ret = s->do_cmd(dev, s);
1486 do_become_nonbusy(dev, s);
1493 command testing ioctl
1496 pointer to cmd structure
1499 cmd structure at arg
1503 modified cmd structure at arg
1506 static int do_cmdtest_ioctl(struct comedi_device *dev,
1507 struct comedi_cmd __user *arg, void *file)
1509 struct comedi_cmd cmd;
1510 struct comedi_subdevice *s;
1512 unsigned int *chanlist = NULL;
1513 unsigned int __user *user_chanlist;
1515 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1516 DPRINTK("bad cmd address\n");
1519 /* save user's chanlist pointer so it can be restored later */
1520 user_chanlist = (unsigned int __user *)cmd.chanlist;
1522 if (cmd.subdev >= dev->n_subdevices) {
1523 DPRINTK("%d no such subdevice\n", cmd.subdev);
1527 s = &dev->subdevices[cmd.subdev];
1528 if (s->type == COMEDI_SUBD_UNUSED) {
1529 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1533 if (!s->do_cmd || !s->do_cmdtest) {
1534 DPRINTK("subdevice %i does not support commands\n",
1539 /* make sure channel/gain list isn't too long */
1540 if (cmd.chanlist_len > s->len_chanlist) {
1541 DPRINTK("channel/gain list too long %d > %d\n",
1542 cmd.chanlist_len, s->len_chanlist);
1547 /* load channel/gain list */
1550 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1552 DPRINTK("allocation failed\n");
1557 if (copy_from_user(chanlist, user_chanlist,
1558 cmd.chanlist_len * sizeof(int))) {
1559 DPRINTK("fault reading chanlist\n");
1564 /* make sure each element in channel/gain list is valid */
1565 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1567 DPRINTK("bad chanlist\n");
1571 cmd.chanlist = chanlist;
1574 ret = s->do_cmdtest(dev, s, &cmd);
1576 /* restore chanlist pointer before copying back */
1577 cmd.chanlist = (unsigned int __force *)user_chanlist;
1579 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1580 DPRINTK("bad cmd address\n");
1605 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1609 unsigned long flags;
1610 struct comedi_subdevice *s;
1612 if (arg >= dev->n_subdevices)
1614 s = &dev->subdevices[arg];
1616 spin_lock_irqsave(&s->spin_lock, flags);
1617 if (s->busy || s->lock)
1621 spin_unlock_irqrestore(&s->spin_lock, flags);
1628 ret = s->lock_f(dev, s);
1647 This function isn't protected by the semaphore, since
1648 we already own the lock.
1650 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1653 struct comedi_subdevice *s;
1655 if (arg >= dev->n_subdevices)
1657 s = &dev->subdevices[arg];
1662 if (s->lock && s->lock != file)
1665 if (s->lock == file) {
1679 cancel acquisition ioctl
1691 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1694 struct comedi_subdevice *s;
1697 if (arg >= dev->n_subdevices)
1699 s = &dev->subdevices[arg];
1700 if (s->async == NULL)
1703 if (s->lock && s->lock != file)
1709 if (s->busy != file)
1712 ret = do_cancel(dev, s);
1713 if (comedi_get_subdevice_runflags(s) & SRF_USER)
1714 wake_up_interruptible(&s->async->wait_head);
1721 instructs driver to synchronize buffers
1733 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1736 struct comedi_subdevice *s;
1738 if (arg >= dev->n_subdevices)
1740 s = &dev->subdevices[arg];
1742 if (s->lock && s->lock != file)
1748 if (s->busy != file)
1752 return s->poll(dev, s);
1757 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1760 const unsigned minor = iminor(file_inode(file));
1761 struct comedi_device *dev = comedi_dev_from_minor(minor);
1767 mutex_lock(&dev->mutex);
1769 /* Device config is special, because it must work on
1770 * an unconfigured device. */
1771 if (cmd == COMEDI_DEVCONFIG) {
1772 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1773 /* Device config not appropriate on non-board minors. */
1777 rc = do_devconfig_ioctl(dev,
1778 (struct comedi_devconfig __user *)arg);
1781 dev->minor >= comedi_num_legacy_minors) {
1782 /* Successfully unconfigured a dynamically
1783 * allocated device. Try and remove it. */
1784 if (comedi_clear_board_dev(dev)) {
1785 mutex_unlock(&dev->mutex);
1786 comedi_free_board_dev(dev);
1794 if (!dev->attached) {
1795 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1801 case COMEDI_BUFCONFIG:
1802 rc = do_bufconfig_ioctl(dev,
1803 (struct comedi_bufconfig __user *)arg);
1805 case COMEDI_DEVINFO:
1806 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1809 case COMEDI_SUBDINFO:
1810 rc = do_subdinfo_ioctl(dev,
1811 (struct comedi_subdinfo __user *)arg,
1814 case COMEDI_CHANINFO:
1815 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1817 case COMEDI_RANGEINFO:
1818 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1820 case COMEDI_BUFINFO:
1821 rc = do_bufinfo_ioctl(dev,
1822 (struct comedi_bufinfo __user *)arg,
1826 rc = do_lock_ioctl(dev, arg, file);
1829 rc = do_unlock_ioctl(dev, arg, file);
1832 rc = do_cancel_ioctl(dev, arg, file);
1835 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1837 case COMEDI_CMDTEST:
1838 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1841 case COMEDI_INSNLIST:
1842 rc = do_insnlist_ioctl(dev,
1843 (struct comedi_insnlist __user *)arg,
1847 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1851 rc = do_poll_ioctl(dev, arg, file);
1859 mutex_unlock(&dev->mutex);
1863 static void comedi_vm_open(struct vm_area_struct *area)
1865 struct comedi_async *async;
1866 struct comedi_device *dev;
1868 async = area->vm_private_data;
1869 dev = async->subdevice->device;
1871 mutex_lock(&dev->mutex);
1872 async->mmap_count++;
1873 mutex_unlock(&dev->mutex);
1876 static void comedi_vm_close(struct vm_area_struct *area)
1878 struct comedi_async *async;
1879 struct comedi_device *dev;
1881 async = area->vm_private_data;
1882 dev = async->subdevice->device;
1884 mutex_lock(&dev->mutex);
1885 async->mmap_count--;
1886 mutex_unlock(&dev->mutex);
1889 static struct vm_operations_struct comedi_vm_ops = {
1890 .open = comedi_vm_open,
1891 .close = comedi_vm_close,
1894 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1896 const unsigned minor = iminor(file_inode(file));
1897 struct comedi_device *dev = comedi_dev_from_minor(minor);
1898 struct comedi_subdevice *s;
1899 struct comedi_async *async;
1900 unsigned long start = vma->vm_start;
1909 mutex_lock(&dev->mutex);
1911 if (!dev->attached) {
1912 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1917 if (vma->vm_flags & VM_WRITE)
1918 s = comedi_write_subdevice(dev, minor);
1920 s = comedi_read_subdevice(dev, minor);
1932 if (vma->vm_pgoff != 0) {
1933 DPRINTK("comedi: mmap() offset must be 0.\n");
1938 size = vma->vm_end - vma->vm_start;
1939 if (size > async->prealloc_bufsz) {
1943 if (size & (~PAGE_MASK)) {
1948 n_pages = size >> PAGE_SHIFT;
1949 for (i = 0; i < n_pages; ++i) {
1950 struct comedi_buf_page *buf = &async->buf_page_list[i];
1952 if (remap_pfn_range(vma, start,
1953 page_to_pfn(virt_to_page(buf->virt_addr)),
1954 PAGE_SIZE, PAGE_SHARED)) {
1961 vma->vm_ops = &comedi_vm_ops;
1962 vma->vm_private_data = async;
1964 async->mmap_count++;
1968 mutex_unlock(&dev->mutex);
1972 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1974 unsigned int mask = 0;
1975 const unsigned minor = iminor(file_inode(file));
1976 struct comedi_device *dev = comedi_dev_from_minor(minor);
1977 struct comedi_subdevice *s;
1982 mutex_lock(&dev->mutex);
1984 if (!dev->attached) {
1985 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1989 s = comedi_read_subdevice(dev, minor);
1990 if (s && s->async) {
1991 poll_wait(file, &s->async->wait_head, wait);
1992 if (!s->busy || !comedi_is_subdevice_running(s) ||
1993 comedi_buf_read_n_available(s->async) > 0)
1994 mask |= POLLIN | POLLRDNORM;
1997 s = comedi_write_subdevice(dev, minor);
1998 if (s && s->async) {
1999 unsigned int bps = bytes_per_sample(s->async->subdevice);
2001 poll_wait(file, &s->async->wait_head, wait);
2002 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2003 if (!s->busy || !comedi_is_subdevice_running(s) ||
2004 comedi_buf_write_n_allocated(s->async) >= bps)
2005 mask |= POLLOUT | POLLWRNORM;
2009 mutex_unlock(&dev->mutex);
2013 static ssize_t comedi_write(struct file *file, const char __user *buf,
2014 size_t nbytes, loff_t *offset)
2016 struct comedi_subdevice *s;
2017 struct comedi_async *async;
2018 int n, m, count = 0, retval = 0;
2019 DECLARE_WAITQUEUE(wait, current);
2020 const unsigned minor = iminor(file_inode(file));
2021 struct comedi_device *dev = comedi_dev_from_minor(minor);
2026 if (!dev->attached) {
2027 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2031 s = comedi_write_subdevice(dev, minor);
2032 if (!s || !s->async)
2037 if (!s->busy || !nbytes)
2039 if (s->busy != file)
2042 add_wait_queue(&async->wait_head, &wait);
2043 while (nbytes > 0 && !retval) {
2044 set_current_state(TASK_INTERRUPTIBLE);
2046 if (!comedi_is_subdevice_running(s)) {
2048 mutex_lock(&dev->mutex);
2049 if (comedi_is_subdevice_in_error(s))
2053 do_become_nonbusy(dev, s);
2054 mutex_unlock(&dev->mutex);
2062 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2063 m = async->prealloc_bufsz - async->buf_write_ptr;
2064 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2065 if (m > comedi_buf_write_n_allocated(async))
2066 m = comedi_buf_write_n_allocated(async);
2071 if (file->f_flags & O_NONBLOCK) {
2076 if (signal_pending(current)) {
2077 retval = -ERESTARTSYS;
2082 if (s->busy != file) {
2089 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2095 comedi_buf_write_free(async, n);
2101 break; /* makes device work like a pipe */
2103 set_current_state(TASK_RUNNING);
2104 remove_wait_queue(&async->wait_head, &wait);
2106 return count ? count : retval;
2109 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2112 struct comedi_subdevice *s;
2113 struct comedi_async *async;
2114 int n, m, count = 0, retval = 0;
2115 DECLARE_WAITQUEUE(wait, current);
2116 const unsigned minor = iminor(file_inode(file));
2117 struct comedi_device *dev = comedi_dev_from_minor(minor);
2122 if (!dev->attached) {
2123 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2127 s = comedi_read_subdevice(dev, minor);
2128 if (!s || !s->async)
2132 if (!s->busy || !nbytes)
2134 if (s->busy != file)
2137 add_wait_queue(&async->wait_head, &wait);
2138 while (nbytes > 0 && !retval) {
2139 set_current_state(TASK_INTERRUPTIBLE);
2143 m = comedi_buf_read_n_available(async);
2144 /* printk("%d available\n",m); */
2145 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2146 m = async->prealloc_bufsz - async->buf_read_ptr;
2147 /* printk("%d contiguous\n",m); */
2152 if (!comedi_is_subdevice_running(s)) {
2153 mutex_lock(&dev->mutex);
2154 do_become_nonbusy(dev, s);
2155 if (comedi_is_subdevice_in_error(s))
2159 mutex_unlock(&dev->mutex);
2162 if (file->f_flags & O_NONBLOCK) {
2167 if (signal_pending(current)) {
2168 retval = -ERESTARTSYS;
2175 if (s->busy != file) {
2181 m = copy_to_user(buf, async->prealloc_buf +
2182 async->buf_read_ptr, n);
2188 comedi_buf_read_alloc(async, n);
2189 comedi_buf_read_free(async, n);
2195 break; /* makes device work like a pipe */
2197 if (comedi_is_subdevice_idle(s)) {
2198 mutex_lock(&dev->mutex);
2199 if (async->buf_read_count - async->buf_write_count == 0)
2200 do_become_nonbusy(dev, s);
2201 mutex_unlock(&dev->mutex);
2203 set_current_state(TASK_RUNNING);
2204 remove_wait_queue(&async->wait_head, &wait);
2206 return count ? count : retval;
2209 static int comedi_open(struct inode *inode, struct file *file)
2211 const unsigned minor = iminor(inode);
2212 struct comedi_device *dev = comedi_dev_from_minor(minor);
2215 DPRINTK("invalid minor number\n");
2219 /* This is slightly hacky, but we want module autoloading
2221 * case: user opens device, attached -> ok
2222 * case: user opens device, unattached, !in_request_module -> autoload
2223 * case: user opens device, unattached, in_request_module -> fail
2224 * case: root opens device, attached -> ok
2225 * case: root opens device, unattached, in_request_module -> ok
2226 * (typically called from modprobe)
2227 * case: root opens device, unattached, !in_request_module -> autoload
2229 * The last could be changed to "-> ok", which would deny root
2232 mutex_lock(&dev->mutex);
2235 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2236 DPRINTK("in request module\n");
2237 mutex_unlock(&dev->mutex);
2240 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2243 dev->in_request_module = true;
2246 mutex_unlock(&dev->mutex);
2247 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2248 mutex_lock(&dev->mutex);
2251 dev->in_request_module = false;
2253 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2254 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2255 mutex_unlock(&dev->mutex);
2259 __module_get(THIS_MODULE);
2261 if (dev->attached) {
2262 if (!try_module_get(dev->driver->module)) {
2263 module_put(THIS_MODULE);
2264 mutex_unlock(&dev->mutex);
2269 if (dev->attached && dev->use_count == 0 && dev->open) {
2270 int rc = dev->open(dev);
2272 module_put(dev->driver->module);
2273 module_put(THIS_MODULE);
2274 mutex_unlock(&dev->mutex);
2281 mutex_unlock(&dev->mutex);
2286 static int comedi_fasync(int fd, struct file *file, int on)
2288 const unsigned minor = iminor(file_inode(file));
2289 struct comedi_device *dev = comedi_dev_from_minor(minor);
2294 return fasync_helper(fd, file, on, &dev->async_queue);
2297 static int comedi_close(struct inode *inode, struct file *file)
2299 const unsigned minor = iminor(inode);
2300 struct comedi_device *dev = comedi_dev_from_minor(minor);
2301 struct comedi_subdevice *s = NULL;
2307 mutex_lock(&dev->mutex);
2309 if (dev->subdevices) {
2310 for (i = 0; i < dev->n_subdevices; i++) {
2311 s = &dev->subdevices[i];
2313 if (s->busy == file)
2315 if (s->lock == file)
2319 if (dev->attached && dev->use_count == 1 && dev->close)
2322 module_put(THIS_MODULE);
2324 module_put(dev->driver->module);
2328 mutex_unlock(&dev->mutex);
2330 if (file->f_flags & FASYNC)
2331 comedi_fasync(-1, file, 0);
2336 static const struct file_operations comedi_fops = {
2337 .owner = THIS_MODULE,
2338 .unlocked_ioctl = comedi_unlocked_ioctl,
2339 .compat_ioctl = comedi_compat_ioctl,
2340 .open = comedi_open,
2341 .release = comedi_close,
2342 .read = comedi_read,
2343 .write = comedi_write,
2344 .mmap = comedi_mmap,
2345 .poll = comedi_poll,
2346 .fasync = comedi_fasync,
2347 .llseek = noop_llseek,
2350 void comedi_error(const struct comedi_device *dev, const char *s)
2352 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2354 EXPORT_SYMBOL_GPL(comedi_error);
2356 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2358 struct comedi_async *async = s->async;
2359 unsigned runflags = 0;
2360 unsigned runflags_mask = 0;
2362 /* DPRINTK("comedi_event 0x%x\n",mask); */
2364 if (!comedi_is_subdevice_running(s))
2368 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2369 COMEDI_CB_OVERFLOW)) {
2370 runflags_mask |= SRF_RUNNING;
2372 /* remember if an error event has occurred, so an error
2373 * can be returned the next time the user does a read() */
2374 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2375 runflags_mask |= SRF_ERROR;
2376 runflags |= SRF_ERROR;
2378 if (runflags_mask) {
2379 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2380 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2383 if (async->cb_mask & s->async->events) {
2384 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2385 wake_up_interruptible(&async->wait_head);
2386 if (s->subdev_flags & SDF_CMD_READ)
2387 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2388 if (s->subdev_flags & SDF_CMD_WRITE)
2389 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2392 async->cb_func(s->async->events, async->cb_arg);
2395 s->async->events = 0;
2397 EXPORT_SYMBOL_GPL(comedi_event);
2399 /* Note: the ->mutex is pre-locked on successful return */
2400 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2402 struct comedi_device *dev;
2403 struct device *csdev;
2406 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2408 return ERR_PTR(-ENOMEM);
2409 comedi_device_init(dev);
2410 comedi_set_hw_dev(dev, hardware_device);
2411 mutex_lock(&dev->mutex);
2412 mutex_lock(&comedi_board_minor_table_lock);
2413 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2414 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2415 if (comedi_board_minor_table[i] == NULL) {
2416 comedi_board_minor_table[i] = dev;
2420 mutex_unlock(&comedi_board_minor_table_lock);
2421 if (i == COMEDI_NUM_BOARD_MINORS) {
2422 mutex_unlock(&dev->mutex);
2423 comedi_device_cleanup(dev);
2425 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2426 return ERR_PTR(-EBUSY);
2429 csdev = device_create(comedi_class, hardware_device,
2430 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2432 dev->class_dev = csdev;
2434 /* Note: dev->mutex needs to be unlocked by the caller. */
2438 static void comedi_free_board_minor(unsigned minor)
2440 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2441 comedi_free_board_dev(comedi_clear_board_minor(minor));
2444 void comedi_release_hardware_device(struct device *hardware_device)
2447 struct comedi_device *dev;
2449 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2451 mutex_lock(&comedi_board_minor_table_lock);
2452 dev = comedi_board_minor_table[minor];
2453 if (dev && dev->hw_dev == hardware_device) {
2454 comedi_board_minor_table[minor] = NULL;
2455 mutex_unlock(&comedi_board_minor_table_lock);
2456 comedi_free_board_dev(dev);
2459 mutex_unlock(&comedi_board_minor_table_lock);
2463 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2465 struct comedi_device *dev = s->device;
2466 struct device *csdev;
2469 mutex_lock(&comedi_subdevice_minor_table_lock);
2470 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2471 if (comedi_subdevice_minor_table[i] == NULL) {
2472 comedi_subdevice_minor_table[i] = s;
2476 mutex_unlock(&comedi_subdevice_minor_table_lock);
2477 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2478 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2481 i += COMEDI_NUM_BOARD_MINORS;
2483 csdev = device_create(comedi_class, dev->class_dev,
2484 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2485 dev->minor, s->index);
2487 s->class_dev = csdev;
2492 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2501 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2502 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2504 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2505 mutex_lock(&comedi_subdevice_minor_table_lock);
2506 if (s == comedi_subdevice_minor_table[i])
2507 comedi_subdevice_minor_table[i] = NULL;
2508 mutex_unlock(&comedi_subdevice_minor_table_lock);
2510 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2511 s->class_dev = NULL;
2515 static void comedi_cleanup_board_minors(void)
2519 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2520 comedi_free_board_minor(i);
2523 static int __init comedi_init(void)
2528 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2530 if (comedi_num_legacy_minors < 0 ||
2531 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2532 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2533 COMEDI_NUM_BOARD_MINORS);
2537 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2538 COMEDI_NUM_MINORS, "comedi");
2541 cdev_init(&comedi_cdev, &comedi_fops);
2542 comedi_cdev.owner = THIS_MODULE;
2543 kobject_set_name(&comedi_cdev.kobj, "comedi");
2544 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2545 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2549 comedi_class = class_create(THIS_MODULE, "comedi");
2550 if (IS_ERR(comedi_class)) {
2551 pr_err("comedi: failed to create class\n");
2552 cdev_del(&comedi_cdev);
2553 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2555 return PTR_ERR(comedi_class);
2558 comedi_class->dev_attrs = comedi_dev_attrs;
2560 /* XXX requires /proc interface */
2563 /* create devices files for legacy/manual use */
2564 for (i = 0; i < comedi_num_legacy_minors; i++) {
2565 struct comedi_device *dev;
2566 dev = comedi_alloc_board_minor(NULL);
2568 comedi_cleanup_board_minors();
2569 cdev_del(&comedi_cdev);
2570 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2572 return PTR_ERR(dev);
2574 /* comedi_alloc_board_minor() locked the mutex */
2575 mutex_unlock(&dev->mutex);
2581 module_init(comedi_init);
2583 static void __exit comedi_cleanup(void)
2587 comedi_cleanup_board_minors();
2588 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2589 BUG_ON(comedi_board_minor_table[i]);
2590 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2591 BUG_ON(comedi_subdevice_minor_table[i]);
2593 class_destroy(comedi_class);
2594 cdev_del(&comedi_cdev);
2595 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2597 comedi_proc_cleanup();
2599 module_exit(comedi_cleanup);
2601 MODULE_AUTHOR("http://www.comedi.org");
2602 MODULE_DESCRIPTION("Comedi core module");
2603 MODULE_LICENSE("GPL");