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>
29 #include <linux/ioport.h>
31 #include <linux/slab.h>
32 #include <linux/kmod.h>
33 #include <linux/poll.h>
34 #include <linux/init.h>
35 #include <linux/device.h>
36 #include <linux/vmalloc.h>
38 #include "comedidev.h"
39 #include <linux/cdev.h>
40 #include <linux/stat.h>
43 #include <linux/uaccess.h>
45 #include "comedi_internal.h"
47 #define COMEDI_NUM_MINORS 0x100
48 #define COMEDI_NUM_SUBDEVICE_MINORS \
49 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
51 #ifdef CONFIG_COMEDI_DEBUG
53 EXPORT_SYMBOL_GPL(comedi_debug);
54 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
55 MODULE_PARM_DESC(comedi_debug,
56 "enable comedi core and driver debugging if non-zero (default 0)"
60 static int comedi_num_legacy_minors;
61 module_param(comedi_num_legacy_minors, int, S_IRUGO);
62 MODULE_PARM_DESC(comedi_num_legacy_minors,
63 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
66 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
67 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
68 MODULE_PARM_DESC(comedi_default_buf_size_kb,
69 "default asynchronous buffer size in KiB (default "
70 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
72 unsigned int comedi_default_buf_maxsize_kb
73 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
74 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
75 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
76 "default maximum size of asynchronous buffer in KiB (default "
77 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
79 static DEFINE_MUTEX(comedi_board_minor_table_lock);
80 static struct comedi_device
81 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
83 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
84 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
85 static struct comedi_subdevice
86 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
88 static struct class *comedi_class;
89 static struct cdev comedi_cdev;
91 static void comedi_device_init(struct comedi_device *dev)
93 spin_lock_init(&dev->spinlock);
94 mutex_init(&dev->mutex);
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 show_max_read_buffer_kb(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 store_max_read_buffer_kb(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;
318 static ssize_t show_read_buffer_kb(struct device *csdev,
319 struct device_attribute *attr, char *buf)
321 unsigned int minor = MINOR(csdev->devt);
322 struct comedi_device *dev;
323 struct comedi_subdevice *s;
324 unsigned int size = 0;
326 dev = comedi_dev_from_minor(minor);
330 mutex_lock(&dev->mutex);
331 s = comedi_read_subdevice(dev, minor);
332 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
333 size = s->async->prealloc_bufsz / 1024;
334 mutex_unlock(&dev->mutex);
336 return snprintf(buf, PAGE_SIZE, "%i\n", size);
339 static ssize_t store_read_buffer_kb(struct device *csdev,
340 struct device_attribute *attr,
341 const char *buf, size_t count)
343 unsigned int minor = MINOR(csdev->devt);
344 struct comedi_device *dev;
345 struct comedi_subdevice *s;
349 err = kstrtouint(buf, 10, &size);
352 if (size > (UINT_MAX / 1024))
356 dev = comedi_dev_from_minor(minor);
360 mutex_lock(&dev->mutex);
361 s = comedi_read_subdevice(dev, minor);
362 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
363 err = resize_async_buffer(dev, s, s->async, size);
366 mutex_unlock(&dev->mutex);
368 return err ? err : count;
371 static ssize_t show_max_write_buffer_kb(struct device *csdev,
372 struct device_attribute *attr,
375 unsigned int minor = MINOR(csdev->devt);
376 struct comedi_device *dev;
377 struct comedi_subdevice *s;
378 unsigned int size = 0;
380 dev = comedi_dev_from_minor(minor);
384 mutex_lock(&dev->mutex);
385 s = comedi_write_subdevice(dev, minor);
386 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
387 size = s->async->max_bufsize / 1024;
388 mutex_unlock(&dev->mutex);
390 return snprintf(buf, PAGE_SIZE, "%i\n", size);
393 static ssize_t store_max_write_buffer_kb(struct device *csdev,
394 struct device_attribute *attr,
395 const char *buf, size_t count)
397 unsigned int minor = MINOR(csdev->devt);
398 struct comedi_device *dev;
399 struct comedi_subdevice *s;
403 err = kstrtouint(buf, 10, &size);
406 if (size > (UINT_MAX / 1024))
410 dev = comedi_dev_from_minor(minor);
414 mutex_lock(&dev->mutex);
415 s = comedi_write_subdevice(dev, minor);
416 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
417 s->async->max_bufsize = size;
420 mutex_unlock(&dev->mutex);
422 return err ? err : count;
425 static ssize_t show_write_buffer_kb(struct device *csdev,
426 struct device_attribute *attr, char *buf)
428 unsigned int minor = MINOR(csdev->devt);
429 struct comedi_device *dev;
430 struct comedi_subdevice *s;
431 unsigned int size = 0;
433 dev = comedi_dev_from_minor(minor);
437 mutex_lock(&dev->mutex);
438 s = comedi_write_subdevice(dev, minor);
439 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
440 size = s->async->prealloc_bufsz / 1024;
441 mutex_unlock(&dev->mutex);
443 return snprintf(buf, PAGE_SIZE, "%i\n", size);
446 static ssize_t store_write_buffer_kb(struct device *csdev,
447 struct device_attribute *attr,
448 const char *buf, size_t count)
450 unsigned int minor = MINOR(csdev->devt);
451 struct comedi_device *dev;
452 struct comedi_subdevice *s;
456 err = kstrtouint(buf, 10, &size);
459 if (size > (UINT_MAX / 1024))
463 dev = comedi_dev_from_minor(minor);
467 mutex_lock(&dev->mutex);
468 s = comedi_write_subdevice(dev, minor);
469 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
470 err = resize_async_buffer(dev, s, s->async, size);
473 mutex_unlock(&dev->mutex);
475 return err ? err : count;
478 static struct device_attribute comedi_dev_attrs[] = {
479 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
480 show_max_read_buffer_kb, store_max_read_buffer_kb),
481 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
482 show_read_buffer_kb, store_read_buffer_kb),
483 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
484 show_max_write_buffer_kb, store_max_write_buffer_kb),
485 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
486 show_write_buffer_kb, store_write_buffer_kb),
490 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
491 unsigned mask, unsigned bits)
495 spin_lock_irqsave(&s->spin_lock, flags);
496 s->runflags &= ~mask;
497 s->runflags |= (bits & mask);
498 spin_unlock_irqrestore(&s->spin_lock, flags);
501 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
506 spin_lock_irqsave(&s->spin_lock, flags);
507 runflags = s->runflags;
508 spin_unlock_irqrestore(&s->spin_lock, flags);
512 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
514 unsigned runflags = comedi_get_subdevice_runflags(s);
516 return (runflags & SRF_RUNNING) ? true : false;
518 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
520 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
522 unsigned runflags = comedi_get_subdevice_runflags(s);
524 return (runflags & SRF_ERROR) ? true : false;
527 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
529 unsigned runflags = comedi_get_subdevice_runflags(s);
531 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
535 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
536 * @s: comedi_subdevice struct
537 * @size: size of the memory to allocate
539 * This also sets the subdevice runflags to allow the core to automatically
540 * free the private data during the detach.
542 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
544 s->private = kzalloc(size, GFP_KERNEL);
546 comedi_set_subdevice_runflags(s, ~0, SRF_FREE_SPRIV);
549 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
552 This function restores a subdevice to an idle state.
554 static void do_become_nonbusy(struct comedi_device *dev,
555 struct comedi_subdevice *s)
557 struct comedi_async *async = s->async;
559 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
561 comedi_buf_reset(async);
562 async->inttrig = NULL;
563 kfree(async->cmd.chanlist);
564 async->cmd.chanlist = NULL;
566 dev_err(dev->class_dev,
567 "BUG: (?) do_become_nonbusy called with async=NULL\n");
573 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
577 if (comedi_is_subdevice_running(s) && s->cancel)
578 ret = s->cancel(dev, s);
580 do_become_nonbusy(dev, s);
585 static int is_device_busy(struct comedi_device *dev)
587 struct comedi_subdevice *s;
593 for (i = 0; i < dev->n_subdevices; i++) {
594 s = &dev->subdevices[i];
597 if (s->async && s->async->mmap_count)
609 pointer to devconfig structure
612 devconfig structure at arg
617 static int do_devconfig_ioctl(struct comedi_device *dev,
618 struct comedi_devconfig __user *arg)
620 struct comedi_devconfig it;
622 if (!capable(CAP_SYS_ADMIN))
626 if (is_device_busy(dev))
629 struct module *driver_module = dev->driver->module;
630 comedi_device_detach(dev);
631 module_put(driver_module);
636 if (copy_from_user(&it, arg, sizeof(it)))
639 it.board_name[COMEDI_NAMELEN - 1] = 0;
641 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
642 dev_warn(dev->class_dev,
643 "comedi_config --init_data is deprecated\n");
647 if (dev->minor >= comedi_num_legacy_minors)
648 /* don't re-use dynamically allocated comedi devices */
651 /* This increments the driver module count on success. */
652 return comedi_device_attach(dev, &it);
657 buffer configuration ioctl
660 pointer to bufconfig structure
666 modified bufconfig at arg
669 static int do_bufconfig_ioctl(struct comedi_device *dev,
670 struct comedi_bufconfig __user *arg)
672 struct comedi_bufconfig bc;
673 struct comedi_async *async;
674 struct comedi_subdevice *s;
677 if (copy_from_user(&bc, arg, sizeof(bc)))
680 if (bc.subdevice >= dev->n_subdevices)
683 s = &dev->subdevices[bc.subdevice];
687 DPRINTK("subdevice does not have async capability\n");
693 if (bc.maximum_size) {
694 if (!capable(CAP_SYS_ADMIN))
697 async->max_bufsize = bc.maximum_size;
701 retval = resize_async_buffer(dev, s, async, bc.size);
706 bc.size = async->prealloc_bufsz;
707 bc.maximum_size = async->max_bufsize;
710 if (copy_to_user(arg, &bc, sizeof(bc)))
721 pointer to devinfo structure
730 static int do_devinfo_ioctl(struct comedi_device *dev,
731 struct comedi_devinfo __user *arg,
734 const unsigned minor = iminor(file_inode(file));
735 struct comedi_subdevice *s;
736 struct comedi_devinfo devinfo;
738 memset(&devinfo, 0, sizeof(devinfo));
740 /* fill devinfo structure */
741 devinfo.version_code = COMEDI_VERSION_CODE;
742 devinfo.n_subdevs = dev->n_subdevices;
743 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
744 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
746 s = comedi_read_subdevice(dev, minor);
748 devinfo.read_subdevice = s->index;
750 devinfo.read_subdevice = -1;
752 s = comedi_write_subdevice(dev, minor);
754 devinfo.write_subdevice = s->index;
756 devinfo.write_subdevice = -1;
758 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
769 pointer to array of subdevice info structures
775 array of subdevice info structures at arg
778 static int do_subdinfo_ioctl(struct comedi_device *dev,
779 struct comedi_subdinfo __user *arg, void *file)
782 struct comedi_subdinfo *tmp, *us;
783 struct comedi_subdevice *s;
785 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
789 /* fill subdinfo structs */
790 for (i = 0; i < dev->n_subdevices; i++) {
791 s = &dev->subdevices[i];
795 us->n_chan = s->n_chan;
796 us->subd_flags = s->subdev_flags;
797 if (comedi_is_subdevice_running(s))
798 us->subd_flags |= SDF_RUNNING;
799 #define TIMER_nanosec 5 /* backwards compatibility */
800 us->timer_type = TIMER_nanosec;
801 us->len_chanlist = s->len_chanlist;
802 us->maxdata = s->maxdata;
803 if (s->range_table) {
805 (i << 24) | (0 << 16) | (s->range_table->length);
807 us->range_type = 0; /* XXX */
809 us->flags = s->flags;
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;
822 us->subd_flags |= SDF_FLAGS;
823 if (s->range_table_list)
824 us->subd_flags |= SDF_RANGETYPE;
826 us->subd_flags |= SDF_CMD;
828 if (s->insn_bits != &insn_inval)
829 us->insn_bits_support = COMEDI_SUPPORTED;
831 us->insn_bits_support = COMEDI_UNSUPPORTED;
833 us->settling_time_0 = s->settling_time_0;
836 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
840 return ret ? -EFAULT : 0;
848 pointer to chaninfo structure
851 chaninfo structure at arg
854 arrays at elements of chaninfo structure
857 static int do_chaninfo_ioctl(struct comedi_device *dev,
858 struct comedi_chaninfo __user *arg)
860 struct comedi_subdevice *s;
861 struct comedi_chaninfo it;
863 if (copy_from_user(&it, arg, sizeof(it)))
866 if (it.subdev >= dev->n_subdevices)
868 s = &dev->subdevices[it.subdev];
870 if (it.maxdata_list) {
871 if (s->maxdata || !s->maxdata_list)
873 if (copy_to_user(it.maxdata_list, s->maxdata_list,
874 s->n_chan * sizeof(unsigned int)))
881 if (copy_to_user(it.flaglist, s->flaglist,
882 s->n_chan * sizeof(unsigned int)))
889 if (!s->range_table_list)
891 for (i = 0; i < s->n_chan; i++) {
894 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
895 (s->range_table_list[i]->length);
896 if (put_user(x, it.rangelist + i))
900 if (copy_to_user(it.rangelist, s->range_type_list,
901 s->n_chan * sizeof(unsigned int)))
911 buffer information ioctl
914 pointer to bufinfo structure
920 modified bufinfo at arg
923 static int do_bufinfo_ioctl(struct comedi_device *dev,
924 struct comedi_bufinfo __user *arg, void *file)
926 struct comedi_bufinfo bi;
927 struct comedi_subdevice *s;
928 struct comedi_async *async;
930 if (copy_from_user(&bi, arg, sizeof(bi)))
933 if (bi.subdevice >= dev->n_subdevices)
936 s = &dev->subdevices[bi.subdevice];
938 if (s->lock && s->lock != file)
944 DPRINTK("subdevice does not have async capability\n");
945 bi.buf_write_ptr = 0;
947 bi.buf_write_count = 0;
948 bi.buf_read_count = 0;
950 bi.bytes_written = 0;
955 bi.bytes_written = 0;
956 goto copyback_position;
961 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
962 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
963 comedi_buf_read_free(async, bi.bytes_read);
965 if (comedi_is_subdevice_idle(s) &&
966 async->buf_write_count == async->buf_read_count) {
967 do_become_nonbusy(dev, s);
971 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
973 comedi_buf_write_alloc(async, bi.bytes_written);
974 comedi_buf_write_free(async, bi.bytes_written);
978 bi.buf_write_count = async->buf_write_count;
979 bi.buf_write_ptr = async->buf_write_ptr;
980 bi.buf_read_count = async->buf_read_count;
981 bi.buf_read_ptr = async->buf_read_ptr;
984 if (copy_to_user(arg, &bi, sizeof(bi)))
990 static int check_insn_config_length(struct comedi_insn *insn,
997 case INSN_CONFIG_DIO_OUTPUT:
998 case INSN_CONFIG_DIO_INPUT:
999 case INSN_CONFIG_DISARM:
1000 case INSN_CONFIG_RESET:
1004 case INSN_CONFIG_ARM:
1005 case INSN_CONFIG_DIO_QUERY:
1006 case INSN_CONFIG_BLOCK_SIZE:
1007 case INSN_CONFIG_FILTER:
1008 case INSN_CONFIG_SERIAL_CLOCK:
1009 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1010 case INSN_CONFIG_ALT_SOURCE:
1011 case INSN_CONFIG_SET_COUNTER_MODE:
1012 case INSN_CONFIG_8254_READ_STATUS:
1013 case INSN_CONFIG_SET_ROUTING:
1014 case INSN_CONFIG_GET_ROUTING:
1015 case INSN_CONFIG_GET_PWM_STATUS:
1016 case INSN_CONFIG_PWM_SET_PERIOD:
1017 case INSN_CONFIG_PWM_GET_PERIOD:
1021 case INSN_CONFIG_SET_GATE_SRC:
1022 case INSN_CONFIG_GET_GATE_SRC:
1023 case INSN_CONFIG_SET_CLOCK_SRC:
1024 case INSN_CONFIG_GET_CLOCK_SRC:
1025 case INSN_CONFIG_SET_OTHER_SRC:
1026 case INSN_CONFIG_GET_COUNTER_STATUS:
1027 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1028 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1029 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1033 case INSN_CONFIG_PWM_OUTPUT:
1034 case INSN_CONFIG_ANALOG_TRIG:
1038 case INSN_CONFIG_DIGITAL_TRIG:
1042 /* by default we allow the insn since we don't have checks for
1043 * all possible cases yet */
1045 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1047 pr_warn("comedi: Add a check to %s in %s.\n",
1048 __func__, __FILE__);
1049 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1055 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1056 unsigned int *data, void *file)
1058 struct comedi_subdevice *s;
1062 if (insn->insn & INSN_MASK_SPECIAL) {
1063 /* a non-subdevice instruction */
1065 switch (insn->insn) {
1075 do_gettimeofday(&tv);
1076 data[0] = tv.tv_sec;
1077 data[1] = tv.tv_usec;
1083 if (insn->n != 1 || data[0] >= 100000) {
1087 udelay(data[0] / 1000);
1095 if (insn->subdev >= dev->n_subdevices) {
1096 DPRINTK("%d not usable subdevice\n",
1101 s = &dev->subdevices[insn->subdev];
1103 DPRINTK("no async\n");
1107 if (!s->async->inttrig) {
1108 DPRINTK("no inttrig\n");
1112 ret = s->async->inttrig(dev, s, data[0]);
1117 DPRINTK("invalid insn\n");
1122 /* a subdevice instruction */
1123 unsigned int maxdata;
1125 if (insn->subdev >= dev->n_subdevices) {
1126 DPRINTK("subdevice %d out of range\n", insn->subdev);
1130 s = &dev->subdevices[insn->subdev];
1132 if (s->type == COMEDI_SUBD_UNUSED) {
1133 DPRINTK("%d not usable subdevice\n", insn->subdev);
1138 /* are we locked? (ioctl lock) */
1139 if (s->lock && s->lock != file) {
1140 DPRINTK("device locked\n");
1145 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1148 DPRINTK("bad chanspec\n");
1156 /* This looks arbitrary. It is. */
1157 s->busy = &parse_insn;
1158 switch (insn->insn) {
1160 ret = s->insn_read(dev, s, insn, data);
1163 maxdata = s->maxdata_list
1164 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1166 for (i = 0; i < insn->n; ++i) {
1167 if (data[i] > maxdata) {
1169 DPRINTK("bad data value(s)\n");
1174 ret = s->insn_write(dev, s, insn, data);
1180 /* Most drivers ignore the base channel in
1181 * insn->chanspec. Fix this here if
1182 * the subdevice has <= 32 channels. */
1184 unsigned int orig_mask;
1186 orig_mask = data[0];
1187 if (s->n_chan <= 32) {
1188 shift = CR_CHAN(insn->chanspec);
1196 ret = s->insn_bits(dev, s, insn, data);
1197 data[0] = orig_mask;
1203 ret = check_insn_config_length(insn, data);
1206 ret = s->insn_config(dev, s, insn, data);
1222 * synchronous instructions
1225 * pointer to sync cmd structure
1228 * sync cmd struct at arg
1235 /* arbitrary limits */
1236 #define MAX_SAMPLES 256
1237 static int do_insnlist_ioctl(struct comedi_device *dev,
1238 struct comedi_insnlist __user *arg, void *file)
1240 struct comedi_insnlist insnlist;
1241 struct comedi_insn *insns = NULL;
1242 unsigned int *data = NULL;
1246 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1249 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1251 DPRINTK("kmalloc failed\n");
1256 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1258 DPRINTK("kmalloc failed\n");
1263 if (copy_from_user(insns, insnlist.insns,
1264 sizeof(*insns) * insnlist.n_insns)) {
1265 DPRINTK("copy_from_user failed\n");
1270 for (i = 0; i < insnlist.n_insns; i++) {
1271 if (insns[i].n > MAX_SAMPLES) {
1272 DPRINTK("number of samples too large\n");
1276 if (insns[i].insn & INSN_MASK_WRITE) {
1277 if (copy_from_user(data, insns[i].data,
1278 insns[i].n * sizeof(unsigned int))) {
1279 DPRINTK("copy_from_user failed\n");
1284 ret = parse_insn(dev, insns + i, data, file);
1287 if (insns[i].insn & INSN_MASK_READ) {
1288 if (copy_to_user(insns[i].data, data,
1289 insns[i].n * sizeof(unsigned int))) {
1290 DPRINTK("copy_to_user failed\n");
1310 * synchronous instructions
1316 * struct comedi_insn struct at arg
1322 static int do_insn_ioctl(struct comedi_device *dev,
1323 struct comedi_insn __user *arg, void *file)
1325 struct comedi_insn insn;
1326 unsigned int *data = NULL;
1329 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1335 if (copy_from_user(&insn, arg, sizeof(insn))) {
1340 /* This is where the behavior of insn and insnlist deviate. */
1341 if (insn.n > MAX_SAMPLES)
1342 insn.n = MAX_SAMPLES;
1343 if (insn.insn & INSN_MASK_WRITE) {
1344 if (copy_from_user(data,
1346 insn.n * sizeof(unsigned int))) {
1351 ret = parse_insn(dev, &insn, data, file);
1354 if (insn.insn & INSN_MASK_READ) {
1355 if (copy_to_user(insn.data,
1357 insn.n * sizeof(unsigned int))) {
1370 static int do_cmd_ioctl(struct comedi_device *dev,
1371 struct comedi_cmd __user *arg, void *file)
1373 struct comedi_cmd cmd;
1374 struct comedi_subdevice *s;
1375 struct comedi_async *async;
1377 unsigned int __user *user_chanlist;
1379 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1380 DPRINTK("bad cmd address\n");
1383 /* save user's chanlist pointer so it can be restored later */
1384 user_chanlist = (unsigned int __user *)cmd.chanlist;
1386 if (cmd.subdev >= dev->n_subdevices) {
1387 DPRINTK("%d no such subdevice\n", cmd.subdev);
1391 s = &dev->subdevices[cmd.subdev];
1394 if (s->type == COMEDI_SUBD_UNUSED) {
1395 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1399 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1400 DPRINTK("subdevice %i does not support commands\n",
1405 /* are we locked? (ioctl lock) */
1406 if (s->lock && s->lock != file) {
1407 DPRINTK("subdevice locked\n");
1413 DPRINTK("subdevice busy\n");
1418 /* make sure channel/gain list isn't too long */
1419 if (cmd.chanlist_len > s->len_chanlist) {
1420 DPRINTK("channel/gain list too long %u > %d\n",
1421 cmd.chanlist_len, s->len_chanlist);
1426 /* make sure channel/gain list isn't too short */
1427 if (cmd.chanlist_len < 1) {
1428 DPRINTK("channel/gain list too short %u < 1\n",
1435 async->cmd.data = NULL;
1436 /* load channel/gain list */
1437 async->cmd.chanlist =
1438 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1439 if (!async->cmd.chanlist) {
1440 DPRINTK("allocation failed\n");
1445 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1446 async->cmd.chanlist_len * sizeof(int))) {
1447 DPRINTK("fault reading chanlist\n");
1452 /* make sure each element in channel/gain list is valid */
1453 ret = comedi_check_chanlist(s,
1454 async->cmd.chanlist_len,
1455 async->cmd.chanlist);
1457 DPRINTK("bad chanlist\n");
1461 ret = s->do_cmdtest(dev, s, &async->cmd);
1463 if (async->cmd.flags & TRIG_BOGUS || ret) {
1464 DPRINTK("test returned %d\n", ret);
1466 /* restore chanlist pointer before copying back */
1467 cmd.chanlist = (unsigned int __force *)user_chanlist;
1469 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1470 DPRINTK("fault writing cmd\n");
1478 if (!async->prealloc_bufsz) {
1480 DPRINTK("no buffer (?)\n");
1484 comedi_buf_reset(async);
1487 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1489 if (async->cmd.flags & TRIG_WAKE_EOS)
1490 async->cb_mask |= COMEDI_CB_EOS;
1492 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1494 ret = s->do_cmd(dev, s);
1499 do_become_nonbusy(dev, s);
1506 command testing ioctl
1509 pointer to cmd structure
1512 cmd structure at arg
1516 modified cmd structure at arg
1519 static int do_cmdtest_ioctl(struct comedi_device *dev,
1520 struct comedi_cmd __user *arg, void *file)
1522 struct comedi_cmd cmd;
1523 struct comedi_subdevice *s;
1525 unsigned int *chanlist = NULL;
1526 unsigned int __user *user_chanlist;
1528 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1529 DPRINTK("bad cmd address\n");
1532 /* save user's chanlist pointer so it can be restored later */
1533 user_chanlist = (unsigned int __user *)cmd.chanlist;
1535 if (cmd.subdev >= dev->n_subdevices) {
1536 DPRINTK("%d no such subdevice\n", cmd.subdev);
1540 s = &dev->subdevices[cmd.subdev];
1541 if (s->type == COMEDI_SUBD_UNUSED) {
1542 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1546 if (!s->do_cmd || !s->do_cmdtest) {
1547 DPRINTK("subdevice %i does not support commands\n",
1552 /* make sure channel/gain list isn't too long */
1553 if (cmd.chanlist_len > s->len_chanlist) {
1554 DPRINTK("channel/gain list too long %d > %d\n",
1555 cmd.chanlist_len, s->len_chanlist);
1560 /* load channel/gain list */
1563 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1565 DPRINTK("allocation failed\n");
1570 if (copy_from_user(chanlist, user_chanlist,
1571 cmd.chanlist_len * sizeof(int))) {
1572 DPRINTK("fault reading chanlist\n");
1577 /* make sure each element in channel/gain list is valid */
1578 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1580 DPRINTK("bad chanlist\n");
1584 cmd.chanlist = chanlist;
1587 ret = s->do_cmdtest(dev, s, &cmd);
1589 /* restore chanlist pointer before copying back */
1590 cmd.chanlist = (unsigned int __force *)user_chanlist;
1592 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1593 DPRINTK("bad cmd address\n");
1618 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1622 unsigned long flags;
1623 struct comedi_subdevice *s;
1625 if (arg >= dev->n_subdevices)
1627 s = &dev->subdevices[arg];
1629 spin_lock_irqsave(&s->spin_lock, flags);
1630 if (s->busy || s->lock)
1634 spin_unlock_irqrestore(&s->spin_lock, flags);
1641 ret = s->lock_f(dev, s);
1660 This function isn't protected by the semaphore, since
1661 we already own the lock.
1663 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1666 struct comedi_subdevice *s;
1668 if (arg >= dev->n_subdevices)
1670 s = &dev->subdevices[arg];
1675 if (s->lock && s->lock != file)
1678 if (s->lock == file) {
1692 cancel acquisition ioctl
1704 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1707 struct comedi_subdevice *s;
1709 if (arg >= dev->n_subdevices)
1711 s = &dev->subdevices[arg];
1712 if (s->async == NULL)
1715 if (s->lock && s->lock != file)
1721 if (s->busy != file)
1724 return do_cancel(dev, s);
1729 instructs driver to synchronize buffers
1741 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1744 struct comedi_subdevice *s;
1746 if (arg >= dev->n_subdevices)
1748 s = &dev->subdevices[arg];
1750 if (s->lock && s->lock != file)
1756 if (s->busy != file)
1760 return s->poll(dev, s);
1765 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1768 const unsigned minor = iminor(file_inode(file));
1769 struct comedi_device *dev = comedi_dev_from_minor(minor);
1775 mutex_lock(&dev->mutex);
1777 /* Device config is special, because it must work on
1778 * an unconfigured device. */
1779 if (cmd == COMEDI_DEVCONFIG) {
1780 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1781 /* Device config not appropriate on non-board minors. */
1785 rc = do_devconfig_ioctl(dev,
1786 (struct comedi_devconfig __user *)arg);
1789 dev->minor >= comedi_num_legacy_minors) {
1790 /* Successfully unconfigured a dynamically
1791 * allocated device. Try and remove it. */
1792 if (comedi_clear_board_dev(dev)) {
1793 mutex_unlock(&dev->mutex);
1794 comedi_free_board_dev(dev);
1802 if (!dev->attached) {
1803 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1809 case COMEDI_BUFCONFIG:
1810 rc = do_bufconfig_ioctl(dev,
1811 (struct comedi_bufconfig __user *)arg);
1813 case COMEDI_DEVINFO:
1814 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1817 case COMEDI_SUBDINFO:
1818 rc = do_subdinfo_ioctl(dev,
1819 (struct comedi_subdinfo __user *)arg,
1822 case COMEDI_CHANINFO:
1823 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1825 case COMEDI_RANGEINFO:
1826 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1828 case COMEDI_BUFINFO:
1829 rc = do_bufinfo_ioctl(dev,
1830 (struct comedi_bufinfo __user *)arg,
1834 rc = do_lock_ioctl(dev, arg, file);
1837 rc = do_unlock_ioctl(dev, arg, file);
1840 rc = do_cancel_ioctl(dev, arg, file);
1843 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1845 case COMEDI_CMDTEST:
1846 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1849 case COMEDI_INSNLIST:
1850 rc = do_insnlist_ioctl(dev,
1851 (struct comedi_insnlist __user *)arg,
1855 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1859 rc = do_poll_ioctl(dev, arg, file);
1867 mutex_unlock(&dev->mutex);
1871 static void comedi_vm_open(struct vm_area_struct *area)
1873 struct comedi_async *async;
1874 struct comedi_device *dev;
1876 async = area->vm_private_data;
1877 dev = async->subdevice->device;
1879 mutex_lock(&dev->mutex);
1880 async->mmap_count++;
1881 mutex_unlock(&dev->mutex);
1884 static void comedi_vm_close(struct vm_area_struct *area)
1886 struct comedi_async *async;
1887 struct comedi_device *dev;
1889 async = area->vm_private_data;
1890 dev = async->subdevice->device;
1892 mutex_lock(&dev->mutex);
1893 async->mmap_count--;
1894 mutex_unlock(&dev->mutex);
1897 static struct vm_operations_struct comedi_vm_ops = {
1898 .open = comedi_vm_open,
1899 .close = comedi_vm_close,
1902 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1904 const unsigned minor = iminor(file_inode(file));
1905 struct comedi_device *dev = comedi_dev_from_minor(minor);
1906 struct comedi_subdevice *s;
1907 struct comedi_async *async;
1908 unsigned long start = vma->vm_start;
1917 mutex_lock(&dev->mutex);
1919 if (!dev->attached) {
1920 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1925 if (vma->vm_flags & VM_WRITE)
1926 s = comedi_write_subdevice(dev, minor);
1928 s = comedi_read_subdevice(dev, minor);
1940 if (vma->vm_pgoff != 0) {
1941 DPRINTK("comedi: mmap() offset must be 0.\n");
1946 size = vma->vm_end - vma->vm_start;
1947 if (size > async->prealloc_bufsz) {
1951 if (size & (~PAGE_MASK)) {
1956 n_pages = size >> PAGE_SHIFT;
1957 for (i = 0; i < n_pages; ++i) {
1958 struct comedi_buf_page *buf = &async->buf_page_list[i];
1960 if (remap_pfn_range(vma, start,
1961 page_to_pfn(virt_to_page(buf->virt_addr)),
1962 PAGE_SIZE, PAGE_SHARED)) {
1969 vma->vm_ops = &comedi_vm_ops;
1970 vma->vm_private_data = async;
1972 async->mmap_count++;
1976 mutex_unlock(&dev->mutex);
1980 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1982 unsigned int mask = 0;
1983 const unsigned minor = iminor(file_inode(file));
1984 struct comedi_device *dev = comedi_dev_from_minor(minor);
1985 struct comedi_subdevice *s;
1990 mutex_lock(&dev->mutex);
1992 if (!dev->attached) {
1993 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1997 s = comedi_read_subdevice(dev, minor);
1998 if (s && s->async) {
1999 poll_wait(file, &s->async->wait_head, wait);
2000 if (!s->busy || !comedi_is_subdevice_running(s) ||
2001 comedi_buf_read_n_available(s->async) > 0)
2002 mask |= POLLIN | POLLRDNORM;
2005 s = comedi_write_subdevice(dev, minor);
2006 if (s && s->async) {
2007 unsigned int bps = bytes_per_sample(s->async->subdevice);
2009 poll_wait(file, &s->async->wait_head, wait);
2010 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2011 if (!s->busy || !comedi_is_subdevice_running(s) ||
2012 comedi_buf_write_n_allocated(s->async) >= bps)
2013 mask |= POLLOUT | POLLWRNORM;
2017 mutex_unlock(&dev->mutex);
2021 static ssize_t comedi_write(struct file *file, const char __user *buf,
2022 size_t nbytes, loff_t *offset)
2024 struct comedi_subdevice *s;
2025 struct comedi_async *async;
2026 int n, m, count = 0, retval = 0;
2027 DECLARE_WAITQUEUE(wait, current);
2028 const unsigned minor = iminor(file_inode(file));
2029 struct comedi_device *dev = comedi_dev_from_minor(minor);
2034 if (!dev->attached) {
2035 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2039 s = comedi_write_subdevice(dev, minor);
2040 if (!s || !s->async)
2045 if (!s->busy || !nbytes)
2047 if (s->busy != file)
2050 add_wait_queue(&async->wait_head, &wait);
2051 while (nbytes > 0 && !retval) {
2052 set_current_state(TASK_INTERRUPTIBLE);
2054 if (!comedi_is_subdevice_running(s)) {
2056 if (comedi_is_subdevice_in_error(s))
2060 do_become_nonbusy(dev, s);
2068 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2069 m = async->prealloc_bufsz - async->buf_write_ptr;
2070 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2071 if (m > comedi_buf_write_n_allocated(async))
2072 m = comedi_buf_write_n_allocated(async);
2077 if (file->f_flags & O_NONBLOCK) {
2082 if (signal_pending(current)) {
2083 retval = -ERESTARTSYS;
2088 if (s->busy != file) {
2095 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2101 comedi_buf_write_free(async, n);
2107 break; /* makes device work like a pipe */
2109 set_current_state(TASK_RUNNING);
2110 remove_wait_queue(&async->wait_head, &wait);
2112 return count ? count : retval;
2115 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2118 struct comedi_subdevice *s;
2119 struct comedi_async *async;
2120 int n, m, count = 0, retval = 0;
2121 DECLARE_WAITQUEUE(wait, current);
2122 const unsigned minor = iminor(file_inode(file));
2123 struct comedi_device *dev = comedi_dev_from_minor(minor);
2128 if (!dev->attached) {
2129 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2133 s = comedi_read_subdevice(dev, minor);
2134 if (!s || !s->async)
2138 if (!s->busy || !nbytes)
2140 if (s->busy != file)
2143 add_wait_queue(&async->wait_head, &wait);
2144 while (nbytes > 0 && !retval) {
2145 set_current_state(TASK_INTERRUPTIBLE);
2149 m = comedi_buf_read_n_available(async);
2150 /* printk("%d available\n",m); */
2151 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2152 m = async->prealloc_bufsz - async->buf_read_ptr;
2153 /* printk("%d contiguous\n",m); */
2158 if (!comedi_is_subdevice_running(s)) {
2159 do_become_nonbusy(dev, s);
2160 if (comedi_is_subdevice_in_error(s))
2166 if (file->f_flags & O_NONBLOCK) {
2171 if (signal_pending(current)) {
2172 retval = -ERESTARTSYS;
2179 if (s->busy != file) {
2185 m = copy_to_user(buf, async->prealloc_buf +
2186 async->buf_read_ptr, n);
2192 comedi_buf_read_alloc(async, n);
2193 comedi_buf_read_free(async, n);
2199 break; /* makes device work like a pipe */
2201 if (comedi_is_subdevice_idle(s) &&
2202 async->buf_read_count - async->buf_write_count == 0) {
2203 do_become_nonbusy(dev, s);
2205 set_current_state(TASK_RUNNING);
2206 remove_wait_queue(&async->wait_head, &wait);
2208 return count ? count : retval;
2211 static int comedi_open(struct inode *inode, struct file *file)
2213 const unsigned minor = iminor(inode);
2214 struct comedi_device *dev = comedi_dev_from_minor(minor);
2217 DPRINTK("invalid minor number\n");
2221 /* This is slightly hacky, but we want module autoloading
2223 * case: user opens device, attached -> ok
2224 * case: user opens device, unattached, !in_request_module -> autoload
2225 * case: user opens device, unattached, in_request_module -> fail
2226 * case: root opens device, attached -> ok
2227 * case: root opens device, unattached, in_request_module -> ok
2228 * (typically called from modprobe)
2229 * case: root opens device, unattached, !in_request_module -> autoload
2231 * The last could be changed to "-> ok", which would deny root
2234 mutex_lock(&dev->mutex);
2237 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2238 DPRINTK("in request module\n");
2239 mutex_unlock(&dev->mutex);
2242 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2245 dev->in_request_module = true;
2248 mutex_unlock(&dev->mutex);
2249 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2250 mutex_lock(&dev->mutex);
2253 dev->in_request_module = false;
2255 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2256 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2257 mutex_unlock(&dev->mutex);
2261 __module_get(THIS_MODULE);
2263 if (dev->attached) {
2264 if (!try_module_get(dev->driver->module)) {
2265 module_put(THIS_MODULE);
2266 mutex_unlock(&dev->mutex);
2271 if (dev->attached && dev->use_count == 0 && dev->open) {
2272 int rc = dev->open(dev);
2274 module_put(dev->driver->module);
2275 module_put(THIS_MODULE);
2276 mutex_unlock(&dev->mutex);
2283 mutex_unlock(&dev->mutex);
2288 static int comedi_fasync(int fd, struct file *file, int on)
2290 const unsigned minor = iminor(file_inode(file));
2291 struct comedi_device *dev = comedi_dev_from_minor(minor);
2296 return fasync_helper(fd, file, on, &dev->async_queue);
2299 static int comedi_close(struct inode *inode, struct file *file)
2301 const unsigned minor = iminor(inode);
2302 struct comedi_device *dev = comedi_dev_from_minor(minor);
2303 struct comedi_subdevice *s = NULL;
2309 mutex_lock(&dev->mutex);
2311 if (dev->subdevices) {
2312 for (i = 0; i < dev->n_subdevices; i++) {
2313 s = &dev->subdevices[i];
2315 if (s->busy == file)
2317 if (s->lock == file)
2321 if (dev->attached && dev->use_count == 1 && dev->close)
2324 module_put(THIS_MODULE);
2326 module_put(dev->driver->module);
2330 mutex_unlock(&dev->mutex);
2335 static const struct file_operations comedi_fops = {
2336 .owner = THIS_MODULE,
2337 .unlocked_ioctl = comedi_unlocked_ioctl,
2338 .compat_ioctl = comedi_compat_ioctl,
2339 .open = comedi_open,
2340 .release = comedi_close,
2341 .read = comedi_read,
2342 .write = comedi_write,
2343 .mmap = comedi_mmap,
2344 .poll = comedi_poll,
2345 .fasync = comedi_fasync,
2346 .llseek = noop_llseek,
2349 void comedi_error(const struct comedi_device *dev, const char *s)
2351 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2353 EXPORT_SYMBOL_GPL(comedi_error);
2355 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2357 struct comedi_async *async = s->async;
2358 unsigned runflags = 0;
2359 unsigned runflags_mask = 0;
2361 /* DPRINTK("comedi_event 0x%x\n",mask); */
2363 if (!comedi_is_subdevice_running(s))
2367 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2368 COMEDI_CB_OVERFLOW)) {
2369 runflags_mask |= SRF_RUNNING;
2371 /* remember if an error event has occurred, so an error
2372 * can be returned the next time the user does a read() */
2373 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2374 runflags_mask |= SRF_ERROR;
2375 runflags |= SRF_ERROR;
2377 if (runflags_mask) {
2378 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2379 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2382 if (async->cb_mask & s->async->events) {
2383 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2384 wake_up_interruptible(&async->wait_head);
2385 if (s->subdev_flags & SDF_CMD_READ)
2386 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2387 if (s->subdev_flags & SDF_CMD_WRITE)
2388 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2391 async->cb_func(s->async->events, async->cb_arg);
2394 s->async->events = 0;
2396 EXPORT_SYMBOL_GPL(comedi_event);
2398 /* Note: the ->mutex is pre-locked on successful return */
2399 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2401 struct comedi_device *dev;
2402 struct device *csdev;
2405 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2407 return ERR_PTR(-ENOMEM);
2408 comedi_device_init(dev);
2409 comedi_set_hw_dev(dev, hardware_device);
2410 mutex_lock(&dev->mutex);
2411 mutex_lock(&comedi_board_minor_table_lock);
2412 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2413 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2414 if (comedi_board_minor_table[i] == NULL) {
2415 comedi_board_minor_table[i] = dev;
2419 mutex_unlock(&comedi_board_minor_table_lock);
2420 if (i == COMEDI_NUM_BOARD_MINORS) {
2421 mutex_unlock(&dev->mutex);
2422 comedi_device_cleanup(dev);
2424 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2425 return ERR_PTR(-EBUSY);
2428 csdev = device_create(comedi_class, hardware_device,
2429 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2431 dev->class_dev = csdev;
2433 /* Note: dev->mutex needs to be unlocked by the caller. */
2437 static void comedi_free_board_minor(unsigned minor)
2439 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2440 comedi_free_board_dev(comedi_clear_board_minor(minor));
2443 void comedi_release_hardware_device(struct device *hardware_device)
2446 struct comedi_device *dev;
2448 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2450 mutex_lock(&comedi_board_minor_table_lock);
2451 dev = comedi_board_minor_table[minor];
2452 if (dev && dev->hw_dev == hardware_device) {
2453 comedi_board_minor_table[minor] = NULL;
2454 mutex_unlock(&comedi_board_minor_table_lock);
2455 comedi_free_board_dev(dev);
2458 mutex_unlock(&comedi_board_minor_table_lock);
2462 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2464 struct comedi_device *dev = s->device;
2465 struct device *csdev;
2468 mutex_lock(&comedi_subdevice_minor_table_lock);
2469 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2470 if (comedi_subdevice_minor_table[i] == NULL) {
2471 comedi_subdevice_minor_table[i] = s;
2475 mutex_unlock(&comedi_subdevice_minor_table_lock);
2476 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2477 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2480 i += COMEDI_NUM_BOARD_MINORS;
2482 csdev = device_create(comedi_class, dev->class_dev,
2483 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2484 dev->minor, s->index);
2486 s->class_dev = csdev;
2491 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2500 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2501 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2503 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2504 mutex_lock(&comedi_subdevice_minor_table_lock);
2505 if (s == comedi_subdevice_minor_table[i])
2506 comedi_subdevice_minor_table[i] = NULL;
2507 mutex_unlock(&comedi_subdevice_minor_table_lock);
2509 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2510 s->class_dev = NULL;
2514 static void comedi_cleanup_board_minors(void)
2518 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2519 comedi_free_board_minor(i);
2522 static int __init comedi_init(void)
2527 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2529 if (comedi_num_legacy_minors < 0 ||
2530 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2531 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2532 COMEDI_NUM_BOARD_MINORS);
2536 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2537 COMEDI_NUM_MINORS, "comedi");
2540 cdev_init(&comedi_cdev, &comedi_fops);
2541 comedi_cdev.owner = THIS_MODULE;
2542 kobject_set_name(&comedi_cdev.kobj, "comedi");
2543 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2544 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2548 comedi_class = class_create(THIS_MODULE, "comedi");
2549 if (IS_ERR(comedi_class)) {
2550 pr_err("comedi: failed to create class\n");
2551 cdev_del(&comedi_cdev);
2552 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2554 return PTR_ERR(comedi_class);
2557 comedi_class->dev_attrs = comedi_dev_attrs;
2559 /* XXX requires /proc interface */
2562 /* create devices files for legacy/manual use */
2563 for (i = 0; i < comedi_num_legacy_minors; i++) {
2564 struct comedi_device *dev;
2565 dev = comedi_alloc_board_minor(NULL);
2567 comedi_cleanup_board_minors();
2568 cdev_del(&comedi_cdev);
2569 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2571 return PTR_ERR(dev);
2573 /* comedi_alloc_board_minor() locked the mutex */
2574 mutex_unlock(&dev->mutex);
2580 module_init(comedi_init);
2582 static void __exit comedi_cleanup(void)
2586 comedi_cleanup_board_minors();
2587 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2588 BUG_ON(comedi_board_minor_table[i]);
2589 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2590 BUG_ON(comedi_subdevice_minor_table[i]);
2592 class_destroy(comedi_class);
2593 cdev_del(&comedi_cdev);
2594 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2596 comedi_proc_cleanup();
2598 module_exit(comedi_cleanup);
2600 MODULE_AUTHOR("http://www.comedi.org");
2601 MODULE_DESCRIPTION("Comedi core module");
2602 MODULE_LICENSE("GPL");