5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "comedi_compat32.h"
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/fcntl.h>
28 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/kmod.h>
32 #include <linux/poll.h>
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/vmalloc.h>
37 #include "comedidev.h"
38 #include <linux/cdev.h>
39 #include <linux/stat.h>
42 #include <linux/uaccess.h>
44 #include "comedi_internal.h"
46 #define COMEDI_NUM_MINORS 0x100
47 #define COMEDI_NUM_SUBDEVICE_MINORS \
48 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
50 #ifdef CONFIG_COMEDI_DEBUG
52 EXPORT_SYMBOL_GPL(comedi_debug);
53 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
54 MODULE_PARM_DESC(comedi_debug,
55 "enable comedi core and driver debugging if non-zero (default 0)"
59 static int comedi_num_legacy_minors;
60 module_param(comedi_num_legacy_minors, int, S_IRUGO);
61 MODULE_PARM_DESC(comedi_num_legacy_minors,
62 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
65 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
66 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
67 MODULE_PARM_DESC(comedi_default_buf_size_kb,
68 "default asynchronous buffer size in KiB (default "
69 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
71 unsigned int comedi_default_buf_maxsize_kb
72 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
73 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
74 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
75 "default maximum size of asynchronous buffer in KiB (default "
76 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
78 static DEFINE_MUTEX(comedi_board_minor_table_lock);
79 static struct comedi_device
80 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
82 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
83 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
84 static struct comedi_subdevice
85 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
87 static struct class *comedi_class;
88 static struct cdev comedi_cdev;
90 static void comedi_device_init(struct comedi_device *dev)
92 spin_lock_init(&dev->spinlock);
93 mutex_init(&dev->mutex);
94 init_rwsem(&dev->attach_lock);
98 static void comedi_device_cleanup(struct comedi_device *dev)
100 struct module *driver_module = NULL;
104 mutex_lock(&dev->mutex);
106 driver_module = dev->driver->module;
107 comedi_device_detach(dev);
108 while (dev->use_count > 0) {
110 module_put(driver_module);
111 module_put(THIS_MODULE);
114 mutex_unlock(&dev->mutex);
115 mutex_destroy(&dev->mutex);
118 static bool comedi_clear_board_dev(struct comedi_device *dev)
120 unsigned int i = dev->minor;
121 bool cleared = false;
123 mutex_lock(&comedi_board_minor_table_lock);
124 if (dev == comedi_board_minor_table[i]) {
125 comedi_board_minor_table[i] = NULL;
128 mutex_unlock(&comedi_board_minor_table_lock);
132 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
134 struct comedi_device *dev;
136 mutex_lock(&comedi_board_minor_table_lock);
137 dev = comedi_board_minor_table[minor];
138 comedi_board_minor_table[minor] = NULL;
139 mutex_unlock(&comedi_board_minor_table_lock);
143 static void comedi_free_board_dev(struct comedi_device *dev)
146 if (dev->class_dev) {
147 device_destroy(comedi_class,
148 MKDEV(COMEDI_MAJOR, dev->minor));
150 comedi_device_cleanup(dev);
155 static struct comedi_subdevice
156 *comedi_subdevice_from_minor(unsigned minor)
158 struct comedi_subdevice *s;
159 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
161 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
162 mutex_lock(&comedi_subdevice_minor_table_lock);
163 s = comedi_subdevice_minor_table[i];
164 mutex_unlock(&comedi_subdevice_minor_table_lock);
168 static struct comedi_device *comedi_dev_from_board_minor(unsigned minor)
170 struct comedi_device *dev;
172 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
173 mutex_lock(&comedi_board_minor_table_lock);
174 dev = comedi_board_minor_table[minor];
175 mutex_unlock(&comedi_board_minor_table_lock);
179 static struct comedi_device *comedi_dev_from_subdevice_minor(unsigned minor)
181 struct comedi_subdevice *s;
183 s = comedi_subdevice_from_minor(minor);
184 return s ? s->device : NULL;
187 struct comedi_device *comedi_dev_from_minor(unsigned minor)
189 if (minor < COMEDI_NUM_BOARD_MINORS)
190 return comedi_dev_from_board_minor(minor);
192 return comedi_dev_from_subdevice_minor(minor);
194 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
196 static struct comedi_subdevice *
197 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
199 struct comedi_subdevice *s;
201 if (minor >= COMEDI_NUM_BOARD_MINORS) {
202 s = comedi_subdevice_from_minor(minor);
203 if (!s || s->device != dev)
205 if (s->subdev_flags & SDF_CMD_READ)
208 return dev->read_subdev;
211 static struct comedi_subdevice *
212 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
214 struct comedi_subdevice *s;
216 if (minor >= COMEDI_NUM_BOARD_MINORS) {
217 s = comedi_subdevice_from_minor(minor);
218 if (!s || s->device != dev)
220 if (s->subdev_flags & SDF_CMD_WRITE)
223 return dev->write_subdev;
226 static int resize_async_buffer(struct comedi_device *dev,
227 struct comedi_subdevice *s,
228 struct comedi_async *async, unsigned new_size)
232 if (new_size > async->max_bufsize)
236 DPRINTK("subdevice is busy, cannot resize buffer\n");
239 if (async->mmap_count) {
240 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
244 /* make sure buffer is an integral number of pages
246 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
248 retval = comedi_buf_alloc(dev, s, new_size);
253 retval = s->buf_change(dev, s, new_size);
258 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
259 dev->minor, s->index, async->prealloc_bufsz);
263 /* sysfs attribute files */
265 static ssize_t max_read_buffer_kb_show(struct device *csdev,
266 struct device_attribute *attr, char *buf)
268 unsigned int minor = MINOR(csdev->devt);
269 struct comedi_device *dev;
270 struct comedi_subdevice *s;
271 unsigned int size = 0;
273 dev = comedi_dev_from_minor(minor);
277 mutex_lock(&dev->mutex);
278 s = comedi_read_subdevice(dev, minor);
279 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
280 size = s->async->max_bufsize / 1024;
281 mutex_unlock(&dev->mutex);
283 return snprintf(buf, PAGE_SIZE, "%i\n", size);
286 static ssize_t max_read_buffer_kb_store(struct device *csdev,
287 struct device_attribute *attr,
288 const char *buf, size_t count)
290 unsigned int minor = MINOR(csdev->devt);
291 struct comedi_device *dev;
292 struct comedi_subdevice *s;
296 err = kstrtouint(buf, 10, &size);
299 if (size > (UINT_MAX / 1024))
303 dev = comedi_dev_from_minor(minor);
307 mutex_lock(&dev->mutex);
308 s = comedi_read_subdevice(dev, minor);
309 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
310 s->async->max_bufsize = size;
313 mutex_unlock(&dev->mutex);
315 return err ? err : count;
317 static DEVICE_ATTR_RW(max_read_buffer_kb);
319 static ssize_t read_buffer_kb_show(struct device *csdev,
320 struct device_attribute *attr, char *buf)
322 unsigned int minor = MINOR(csdev->devt);
323 struct comedi_device *dev;
324 struct comedi_subdevice *s;
325 unsigned int size = 0;
327 dev = comedi_dev_from_minor(minor);
331 mutex_lock(&dev->mutex);
332 s = comedi_read_subdevice(dev, minor);
333 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
334 size = s->async->prealloc_bufsz / 1024;
335 mutex_unlock(&dev->mutex);
337 return snprintf(buf, PAGE_SIZE, "%i\n", size);
340 static ssize_t read_buffer_kb_store(struct device *csdev,
341 struct device_attribute *attr,
342 const char *buf, size_t count)
344 unsigned int minor = MINOR(csdev->devt);
345 struct comedi_device *dev;
346 struct comedi_subdevice *s;
350 err = kstrtouint(buf, 10, &size);
353 if (size > (UINT_MAX / 1024))
357 dev = comedi_dev_from_minor(minor);
361 mutex_lock(&dev->mutex);
362 s = comedi_read_subdevice(dev, minor);
363 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
364 err = resize_async_buffer(dev, s, s->async, size);
367 mutex_unlock(&dev->mutex);
369 return err ? err : count;
371 static DEVICE_ATTR_RW(read_buffer_kb);
373 static ssize_t max_write_buffer_kb_show(struct device *csdev,
374 struct device_attribute *attr,
377 unsigned int minor = MINOR(csdev->devt);
378 struct comedi_device *dev;
379 struct comedi_subdevice *s;
380 unsigned int size = 0;
382 dev = comedi_dev_from_minor(minor);
386 mutex_lock(&dev->mutex);
387 s = comedi_write_subdevice(dev, minor);
388 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
389 size = s->async->max_bufsize / 1024;
390 mutex_unlock(&dev->mutex);
392 return snprintf(buf, PAGE_SIZE, "%i\n", size);
395 static ssize_t max_write_buffer_kb_store(struct device *csdev,
396 struct device_attribute *attr,
397 const char *buf, size_t count)
399 unsigned int minor = MINOR(csdev->devt);
400 struct comedi_device *dev;
401 struct comedi_subdevice *s;
405 err = kstrtouint(buf, 10, &size);
408 if (size > (UINT_MAX / 1024))
412 dev = comedi_dev_from_minor(minor);
416 mutex_lock(&dev->mutex);
417 s = comedi_write_subdevice(dev, minor);
418 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
419 s->async->max_bufsize = size;
422 mutex_unlock(&dev->mutex);
424 return err ? err : count;
426 static DEVICE_ATTR_RW(max_write_buffer_kb);
428 static ssize_t write_buffer_kb_show(struct device *csdev,
429 struct device_attribute *attr, char *buf)
431 unsigned int minor = MINOR(csdev->devt);
432 struct comedi_device *dev;
433 struct comedi_subdevice *s;
434 unsigned int size = 0;
436 dev = comedi_dev_from_minor(minor);
440 mutex_lock(&dev->mutex);
441 s = comedi_write_subdevice(dev, minor);
442 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
443 size = s->async->prealloc_bufsz / 1024;
444 mutex_unlock(&dev->mutex);
446 return snprintf(buf, PAGE_SIZE, "%i\n", size);
449 static ssize_t write_buffer_kb_store(struct device *csdev,
450 struct device_attribute *attr,
451 const char *buf, size_t count)
453 unsigned int minor = MINOR(csdev->devt);
454 struct comedi_device *dev;
455 struct comedi_subdevice *s;
459 err = kstrtouint(buf, 10, &size);
462 if (size > (UINT_MAX / 1024))
466 dev = comedi_dev_from_minor(minor);
470 mutex_lock(&dev->mutex);
471 s = comedi_write_subdevice(dev, minor);
472 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
473 err = resize_async_buffer(dev, s, s->async, size);
476 mutex_unlock(&dev->mutex);
478 return err ? err : count;
480 static DEVICE_ATTR_RW(write_buffer_kb);
482 static struct attribute *comedi_dev_attrs[] = {
483 &dev_attr_max_read_buffer_kb.attr,
484 &dev_attr_read_buffer_kb.attr,
485 &dev_attr_max_write_buffer_kb.attr,
486 &dev_attr_write_buffer_kb.attr,
489 ATTRIBUTE_GROUPS(comedi_dev);
491 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
492 unsigned mask, unsigned bits)
496 spin_lock_irqsave(&s->spin_lock, flags);
497 s->runflags &= ~mask;
498 s->runflags |= (bits & mask);
499 spin_unlock_irqrestore(&s->spin_lock, flags);
502 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
507 spin_lock_irqsave(&s->spin_lock, flags);
508 runflags = s->runflags;
509 spin_unlock_irqrestore(&s->spin_lock, flags);
513 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
515 unsigned runflags = comedi_get_subdevice_runflags(s);
517 return (runflags & SRF_RUNNING) ? true : false;
519 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
521 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
523 unsigned runflags = comedi_get_subdevice_runflags(s);
525 return (runflags & SRF_ERROR) ? true : false;
528 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
530 unsigned runflags = comedi_get_subdevice_runflags(s);
532 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
536 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
537 * @s: comedi_subdevice struct
538 * @size: size of the memory to allocate
540 * This also sets the subdevice runflags to allow the core to automatically
541 * free the private data during the detach.
543 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
545 s->private = kzalloc(size, GFP_KERNEL);
547 s->runflags |= SRF_FREE_SPRIV;
550 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
553 This function restores a subdevice to an idle state.
555 static void do_become_nonbusy(struct comedi_device *dev,
556 struct comedi_subdevice *s)
558 struct comedi_async *async = s->async;
560 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
562 comedi_buf_reset(async);
563 async->inttrig = NULL;
564 kfree(async->cmd.chanlist);
565 async->cmd.chanlist = NULL;
567 wake_up_interruptible_all(&s->async->wait_head);
569 dev_err(dev->class_dev,
570 "BUG: (?) do_become_nonbusy called with async=NULL\n");
575 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
579 if (comedi_is_subdevice_running(s) && s->cancel)
580 ret = s->cancel(dev, s);
582 do_become_nonbusy(dev, s);
587 void comedi_device_cancel_all(struct comedi_device *dev)
589 struct comedi_subdevice *s;
595 for (i = 0; i < dev->n_subdevices; i++) {
596 s = &dev->subdevices[i];
602 static int is_device_busy(struct comedi_device *dev)
604 struct comedi_subdevice *s;
610 for (i = 0; i < dev->n_subdevices; i++) {
611 s = &dev->subdevices[i];
614 if (s->async && s->async->mmap_count)
626 pointer to devconfig structure
629 devconfig structure at arg
634 static int do_devconfig_ioctl(struct comedi_device *dev,
635 struct comedi_devconfig __user *arg)
637 struct comedi_devconfig it;
639 if (!capable(CAP_SYS_ADMIN))
643 if (is_device_busy(dev))
646 struct module *driver_module = dev->driver->module;
647 comedi_device_detach(dev);
648 module_put(driver_module);
653 if (copy_from_user(&it, arg, sizeof(it)))
656 it.board_name[COMEDI_NAMELEN - 1] = 0;
658 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
659 dev_warn(dev->class_dev,
660 "comedi_config --init_data is deprecated\n");
664 if (dev->minor >= comedi_num_legacy_minors)
665 /* don't re-use dynamically allocated comedi devices */
668 /* This increments the driver module count on success. */
669 return comedi_device_attach(dev, &it);
674 buffer configuration ioctl
677 pointer to bufconfig structure
683 modified bufconfig at arg
686 static int do_bufconfig_ioctl(struct comedi_device *dev,
687 struct comedi_bufconfig __user *arg)
689 struct comedi_bufconfig bc;
690 struct comedi_async *async;
691 struct comedi_subdevice *s;
694 if (copy_from_user(&bc, arg, sizeof(bc)))
697 if (bc.subdevice >= dev->n_subdevices)
700 s = &dev->subdevices[bc.subdevice];
704 DPRINTK("subdevice does not have async capability\n");
710 if (bc.maximum_size) {
711 if (!capable(CAP_SYS_ADMIN))
714 async->max_bufsize = bc.maximum_size;
718 retval = resize_async_buffer(dev, s, async, bc.size);
723 bc.size = async->prealloc_bufsz;
724 bc.maximum_size = async->max_bufsize;
727 if (copy_to_user(arg, &bc, sizeof(bc)))
738 pointer to devinfo structure
747 static int do_devinfo_ioctl(struct comedi_device *dev,
748 struct comedi_devinfo __user *arg,
751 const unsigned minor = iminor(file_inode(file));
752 struct comedi_subdevice *s;
753 struct comedi_devinfo devinfo;
755 memset(&devinfo, 0, sizeof(devinfo));
757 /* fill devinfo structure */
758 devinfo.version_code = COMEDI_VERSION_CODE;
759 devinfo.n_subdevs = dev->n_subdevices;
760 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
761 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
763 s = comedi_read_subdevice(dev, minor);
765 devinfo.read_subdevice = s->index;
767 devinfo.read_subdevice = -1;
769 s = comedi_write_subdevice(dev, minor);
771 devinfo.write_subdevice = s->index;
773 devinfo.write_subdevice = -1;
775 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
786 pointer to array of subdevice info structures
792 array of subdevice info structures at arg
795 static int do_subdinfo_ioctl(struct comedi_device *dev,
796 struct comedi_subdinfo __user *arg, void *file)
799 struct comedi_subdinfo *tmp, *us;
800 struct comedi_subdevice *s;
802 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
806 /* fill subdinfo structs */
807 for (i = 0; i < dev->n_subdevices; i++) {
808 s = &dev->subdevices[i];
812 us->n_chan = s->n_chan;
813 us->subd_flags = s->subdev_flags;
814 if (comedi_is_subdevice_running(s))
815 us->subd_flags |= SDF_RUNNING;
816 #define TIMER_nanosec 5 /* backwards compatibility */
817 us->timer_type = TIMER_nanosec;
818 us->len_chanlist = s->len_chanlist;
819 us->maxdata = s->maxdata;
820 if (s->range_table) {
822 (i << 24) | (0 << 16) | (s->range_table->length);
824 us->range_type = 0; /* XXX */
828 us->subd_flags |= SDF_BUSY;
830 us->subd_flags |= SDF_BUSY_OWNER;
832 us->subd_flags |= SDF_LOCKED;
834 us->subd_flags |= SDF_LOCK_OWNER;
835 if (!s->maxdata && s->maxdata_list)
836 us->subd_flags |= SDF_MAXDATA;
837 if (s->range_table_list)
838 us->subd_flags |= SDF_RANGETYPE;
840 us->subd_flags |= SDF_CMD;
842 if (s->insn_bits != &insn_inval)
843 us->insn_bits_support = COMEDI_SUPPORTED;
845 us->insn_bits_support = COMEDI_UNSUPPORTED;
848 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
852 return ret ? -EFAULT : 0;
860 pointer to chaninfo structure
863 chaninfo structure at arg
866 arrays at elements of chaninfo structure
869 static int do_chaninfo_ioctl(struct comedi_device *dev,
870 struct comedi_chaninfo __user *arg)
872 struct comedi_subdevice *s;
873 struct comedi_chaninfo it;
875 if (copy_from_user(&it, arg, sizeof(it)))
878 if (it.subdev >= dev->n_subdevices)
880 s = &dev->subdevices[it.subdev];
882 if (it.maxdata_list) {
883 if (s->maxdata || !s->maxdata_list)
885 if (copy_to_user(it.maxdata_list, s->maxdata_list,
886 s->n_chan * sizeof(unsigned int)))
891 return -EINVAL; /* flaglist not supported */
896 if (!s->range_table_list)
898 for (i = 0; i < s->n_chan; i++) {
901 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
902 (s->range_table_list[i]->length);
903 if (put_user(x, it.rangelist + i))
907 if (copy_to_user(it.rangelist, s->range_type_list,
908 s->n_chan * sizeof(unsigned int)))
918 buffer information ioctl
921 pointer to bufinfo structure
927 modified bufinfo at arg
930 static int do_bufinfo_ioctl(struct comedi_device *dev,
931 struct comedi_bufinfo __user *arg, void *file)
933 struct comedi_bufinfo bi;
934 struct comedi_subdevice *s;
935 struct comedi_async *async;
937 if (copy_from_user(&bi, arg, sizeof(bi)))
940 if (bi.subdevice >= dev->n_subdevices)
943 s = &dev->subdevices[bi.subdevice];
945 if (s->lock && s->lock != file)
951 DPRINTK("subdevice does not have async capability\n");
952 bi.buf_write_ptr = 0;
954 bi.buf_write_count = 0;
955 bi.buf_read_count = 0;
957 bi.bytes_written = 0;
962 bi.bytes_written = 0;
963 goto copyback_position;
968 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
969 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
970 comedi_buf_read_free(async, bi.bytes_read);
972 if (comedi_is_subdevice_idle(s) &&
973 async->buf_write_count == async->buf_read_count) {
974 do_become_nonbusy(dev, s);
978 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
980 comedi_buf_write_alloc(async, bi.bytes_written);
981 comedi_buf_write_free(async, bi.bytes_written);
985 bi.buf_write_count = async->buf_write_count;
986 bi.buf_write_ptr = async->buf_write_ptr;
987 bi.buf_read_count = async->buf_read_count;
988 bi.buf_read_ptr = async->buf_read_ptr;
991 if (copy_to_user(arg, &bi, sizeof(bi)))
997 static int check_insn_config_length(struct comedi_insn *insn,
1004 case INSN_CONFIG_DIO_OUTPUT:
1005 case INSN_CONFIG_DIO_INPUT:
1006 case INSN_CONFIG_DISARM:
1007 case INSN_CONFIG_RESET:
1011 case INSN_CONFIG_ARM:
1012 case INSN_CONFIG_DIO_QUERY:
1013 case INSN_CONFIG_BLOCK_SIZE:
1014 case INSN_CONFIG_FILTER:
1015 case INSN_CONFIG_SERIAL_CLOCK:
1016 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1017 case INSN_CONFIG_ALT_SOURCE:
1018 case INSN_CONFIG_SET_COUNTER_MODE:
1019 case INSN_CONFIG_8254_READ_STATUS:
1020 case INSN_CONFIG_SET_ROUTING:
1021 case INSN_CONFIG_GET_ROUTING:
1022 case INSN_CONFIG_GET_PWM_STATUS:
1023 case INSN_CONFIG_PWM_SET_PERIOD:
1024 case INSN_CONFIG_PWM_GET_PERIOD:
1028 case INSN_CONFIG_SET_GATE_SRC:
1029 case INSN_CONFIG_GET_GATE_SRC:
1030 case INSN_CONFIG_SET_CLOCK_SRC:
1031 case INSN_CONFIG_GET_CLOCK_SRC:
1032 case INSN_CONFIG_SET_OTHER_SRC:
1033 case INSN_CONFIG_GET_COUNTER_STATUS:
1034 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1035 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1036 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1040 case INSN_CONFIG_PWM_OUTPUT:
1041 case INSN_CONFIG_ANALOG_TRIG:
1045 case INSN_CONFIG_DIGITAL_TRIG:
1049 /* by default we allow the insn since we don't have checks for
1050 * all possible cases yet */
1052 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1054 pr_warn("comedi: Add a check to %s in %s.\n",
1055 __func__, __FILE__);
1056 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1062 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1063 unsigned int *data, void *file)
1065 struct comedi_subdevice *s;
1069 if (insn->insn & INSN_MASK_SPECIAL) {
1070 /* a non-subdevice instruction */
1072 switch (insn->insn) {
1082 do_gettimeofday(&tv);
1083 data[0] = tv.tv_sec;
1084 data[1] = tv.tv_usec;
1090 if (insn->n != 1 || data[0] >= 100000) {
1094 udelay(data[0] / 1000);
1102 if (insn->subdev >= dev->n_subdevices) {
1103 DPRINTK("%d not usable subdevice\n",
1108 s = &dev->subdevices[insn->subdev];
1110 DPRINTK("no async\n");
1114 if (!s->async->inttrig) {
1115 DPRINTK("no inttrig\n");
1119 ret = s->async->inttrig(dev, s, data[0]);
1124 DPRINTK("invalid insn\n");
1129 /* a subdevice instruction */
1130 unsigned int maxdata;
1132 if (insn->subdev >= dev->n_subdevices) {
1133 DPRINTK("subdevice %d out of range\n", insn->subdev);
1137 s = &dev->subdevices[insn->subdev];
1139 if (s->type == COMEDI_SUBD_UNUSED) {
1140 DPRINTK("%d not usable subdevice\n", insn->subdev);
1145 /* are we locked? (ioctl lock) */
1146 if (s->lock && s->lock != file) {
1147 DPRINTK("device locked\n");
1152 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1155 DPRINTK("bad chanspec\n");
1163 /* This looks arbitrary. It is. */
1164 s->busy = &parse_insn;
1165 switch (insn->insn) {
1167 ret = s->insn_read(dev, s, insn, data);
1170 maxdata = s->maxdata_list
1171 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1173 for (i = 0; i < insn->n; ++i) {
1174 if (data[i] > maxdata) {
1176 DPRINTK("bad data value(s)\n");
1181 ret = s->insn_write(dev, s, insn, data);
1187 /* Most drivers ignore the base channel in
1188 * insn->chanspec. Fix this here if
1189 * the subdevice has <= 32 channels. */
1191 unsigned int orig_mask;
1193 orig_mask = data[0];
1194 if (s->n_chan <= 32) {
1195 shift = CR_CHAN(insn->chanspec);
1203 ret = s->insn_bits(dev, s, insn, data);
1204 data[0] = orig_mask;
1210 ret = check_insn_config_length(insn, data);
1213 ret = s->insn_config(dev, s, insn, data);
1229 * synchronous instructions
1232 * pointer to sync cmd structure
1235 * sync cmd struct at arg
1242 /* arbitrary limits */
1243 #define MAX_SAMPLES 256
1244 static int do_insnlist_ioctl(struct comedi_device *dev,
1245 struct comedi_insnlist __user *arg, void *file)
1247 struct comedi_insnlist insnlist;
1248 struct comedi_insn *insns = NULL;
1249 unsigned int *data = NULL;
1253 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1256 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1258 DPRINTK("kmalloc failed\n");
1263 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1265 DPRINTK("kmalloc failed\n");
1270 if (copy_from_user(insns, insnlist.insns,
1271 sizeof(*insns) * insnlist.n_insns)) {
1272 DPRINTK("copy_from_user failed\n");
1277 for (i = 0; i < insnlist.n_insns; i++) {
1278 if (insns[i].n > MAX_SAMPLES) {
1279 DPRINTK("number of samples too large\n");
1283 if (insns[i].insn & INSN_MASK_WRITE) {
1284 if (copy_from_user(data, insns[i].data,
1285 insns[i].n * sizeof(unsigned int))) {
1286 DPRINTK("copy_from_user failed\n");
1291 ret = parse_insn(dev, insns + i, data, file);
1294 if (insns[i].insn & INSN_MASK_READ) {
1295 if (copy_to_user(insns[i].data, data,
1296 insns[i].n * sizeof(unsigned int))) {
1297 DPRINTK("copy_to_user failed\n");
1317 * synchronous instructions
1323 * struct comedi_insn struct at arg
1329 static int do_insn_ioctl(struct comedi_device *dev,
1330 struct comedi_insn __user *arg, void *file)
1332 struct comedi_insn insn;
1333 unsigned int *data = NULL;
1336 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1342 if (copy_from_user(&insn, arg, sizeof(insn))) {
1347 /* This is where the behavior of insn and insnlist deviate. */
1348 if (insn.n > MAX_SAMPLES)
1349 insn.n = MAX_SAMPLES;
1350 if (insn.insn & INSN_MASK_WRITE) {
1351 if (copy_from_user(data,
1353 insn.n * sizeof(unsigned int))) {
1358 ret = parse_insn(dev, &insn, data, file);
1361 if (insn.insn & INSN_MASK_READ) {
1362 if (copy_to_user(insn.data,
1364 insn.n * sizeof(unsigned int))) {
1377 static int do_cmd_ioctl(struct comedi_device *dev,
1378 struct comedi_cmd __user *arg, void *file)
1380 struct comedi_cmd cmd;
1381 struct comedi_subdevice *s;
1382 struct comedi_async *async;
1384 unsigned int __user *user_chanlist;
1386 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1387 DPRINTK("bad cmd address\n");
1390 /* save user's chanlist pointer so it can be restored later */
1391 user_chanlist = (unsigned int __user *)cmd.chanlist;
1393 if (cmd.subdev >= dev->n_subdevices) {
1394 DPRINTK("%d no such subdevice\n", cmd.subdev);
1398 s = &dev->subdevices[cmd.subdev];
1401 if (s->type == COMEDI_SUBD_UNUSED) {
1402 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1406 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1407 DPRINTK("subdevice %i does not support commands\n",
1412 /* are we locked? (ioctl lock) */
1413 if (s->lock && s->lock != file) {
1414 DPRINTK("subdevice locked\n");
1420 DPRINTK("subdevice busy\n");
1424 /* make sure channel/gain list isn't too long */
1425 if (cmd.chanlist_len > s->len_chanlist) {
1426 DPRINTK("channel/gain list too long %u > %d\n",
1427 cmd.chanlist_len, s->len_chanlist);
1431 /* make sure channel/gain list isn't too short */
1432 if (cmd.chanlist_len < 1) {
1433 DPRINTK("channel/gain list too short %u < 1\n",
1439 async->cmd.data = NULL;
1440 /* load channel/gain list */
1441 async->cmd.chanlist = memdup_user(user_chanlist,
1442 async->cmd.chanlist_len * sizeof(int));
1443 if (IS_ERR(async->cmd.chanlist)) {
1444 ret = PTR_ERR(async->cmd.chanlist);
1445 DPRINTK("memdup_user failed with code %d\n", ret);
1449 /* make sure each element in channel/gain list is valid */
1450 ret = comedi_check_chanlist(s,
1451 async->cmd.chanlist_len,
1452 async->cmd.chanlist);
1454 DPRINTK("bad chanlist\n");
1458 ret = s->do_cmdtest(dev, s, &async->cmd);
1460 if (async->cmd.flags & TRIG_BOGUS || ret) {
1461 DPRINTK("test returned %d\n", ret);
1463 /* restore chanlist pointer before copying back */
1464 cmd.chanlist = (unsigned int __force *)user_chanlist;
1466 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1467 DPRINTK("fault writing cmd\n");
1475 if (!async->prealloc_bufsz) {
1477 DPRINTK("no buffer (?)\n");
1481 comedi_buf_reset(async);
1484 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1486 if (async->cmd.flags & TRIG_WAKE_EOS)
1487 async->cb_mask |= COMEDI_CB_EOS;
1489 comedi_set_subdevice_runflags(s, SRF_ERROR | SRF_RUNNING, SRF_RUNNING);
1491 /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
1492 * comedi_read() or comedi_write() */
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 */
1562 chanlist = memdup_user(user_chanlist,
1563 cmd.chanlist_len * sizeof(int));
1564 if (IS_ERR(chanlist)) {
1565 ret = PTR_ERR(chanlist);
1566 DPRINTK("memdup_user exited with code %d", ret);
1570 /* make sure each element in channel/gain list is valid */
1571 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1573 DPRINTK("bad chanlist\n");
1577 cmd.chanlist = chanlist;
1580 ret = s->do_cmdtest(dev, s, &cmd);
1582 /* restore chanlist pointer before copying back */
1583 cmd.chanlist = (unsigned int __force *)user_chanlist;
1585 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1586 DPRINTK("bad cmd address\n");
1611 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1615 unsigned long flags;
1616 struct comedi_subdevice *s;
1618 if (arg >= dev->n_subdevices)
1620 s = &dev->subdevices[arg];
1622 spin_lock_irqsave(&s->spin_lock, flags);
1623 if (s->busy || s->lock)
1627 spin_unlock_irqrestore(&s->spin_lock, flags);
1634 ret = s->lock_f(dev, s);
1653 This function isn't protected by the semaphore, since
1654 we already own the lock.
1656 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1659 struct comedi_subdevice *s;
1661 if (arg >= dev->n_subdevices)
1663 s = &dev->subdevices[arg];
1668 if (s->lock && s->lock != file)
1671 if (s->lock == file) {
1685 cancel acquisition ioctl
1697 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1700 struct comedi_subdevice *s;
1703 if (arg >= dev->n_subdevices)
1705 s = &dev->subdevices[arg];
1706 if (s->async == NULL)
1709 if (s->lock && s->lock != file)
1715 if (s->busy != file)
1718 ret = do_cancel(dev, s);
1725 instructs driver to synchronize buffers
1737 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1740 struct comedi_subdevice *s;
1742 if (arg >= dev->n_subdevices)
1744 s = &dev->subdevices[arg];
1746 if (s->lock && s->lock != file)
1752 if (s->busy != file)
1756 return s->poll(dev, s);
1761 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1764 const unsigned minor = iminor(file_inode(file));
1765 struct comedi_device *dev = comedi_dev_from_minor(minor);
1771 mutex_lock(&dev->mutex);
1773 /* Device config is special, because it must work on
1774 * an unconfigured device. */
1775 if (cmd == COMEDI_DEVCONFIG) {
1776 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1777 /* Device config not appropriate on non-board minors. */
1781 rc = do_devconfig_ioctl(dev,
1782 (struct comedi_devconfig __user *)arg);
1785 dev->minor >= comedi_num_legacy_minors) {
1786 /* Successfully unconfigured a dynamically
1787 * allocated device. Try and remove it. */
1788 if (comedi_clear_board_dev(dev)) {
1789 mutex_unlock(&dev->mutex);
1790 comedi_free_board_dev(dev);
1798 if (!dev->attached) {
1799 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1805 case COMEDI_BUFCONFIG:
1806 rc = do_bufconfig_ioctl(dev,
1807 (struct comedi_bufconfig __user *)arg);
1809 case COMEDI_DEVINFO:
1810 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1813 case COMEDI_SUBDINFO:
1814 rc = do_subdinfo_ioctl(dev,
1815 (struct comedi_subdinfo __user *)arg,
1818 case COMEDI_CHANINFO:
1819 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1821 case COMEDI_RANGEINFO:
1822 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1824 case COMEDI_BUFINFO:
1825 rc = do_bufinfo_ioctl(dev,
1826 (struct comedi_bufinfo __user *)arg,
1830 rc = do_lock_ioctl(dev, arg, file);
1833 rc = do_unlock_ioctl(dev, arg, file);
1836 rc = do_cancel_ioctl(dev, arg, file);
1839 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1841 case COMEDI_CMDTEST:
1842 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1845 case COMEDI_INSNLIST:
1846 rc = do_insnlist_ioctl(dev,
1847 (struct comedi_insnlist __user *)arg,
1851 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1855 rc = do_poll_ioctl(dev, arg, file);
1863 mutex_unlock(&dev->mutex);
1867 static void comedi_vm_open(struct vm_area_struct *area)
1869 struct comedi_async *async;
1870 struct comedi_device *dev;
1872 async = area->vm_private_data;
1873 dev = async->subdevice->device;
1875 mutex_lock(&dev->mutex);
1876 async->mmap_count++;
1877 mutex_unlock(&dev->mutex);
1880 static void comedi_vm_close(struct vm_area_struct *area)
1882 struct comedi_async *async;
1883 struct comedi_device *dev;
1885 async = area->vm_private_data;
1886 dev = async->subdevice->device;
1888 mutex_lock(&dev->mutex);
1889 async->mmap_count--;
1890 mutex_unlock(&dev->mutex);
1893 static struct vm_operations_struct comedi_vm_ops = {
1894 .open = comedi_vm_open,
1895 .close = comedi_vm_close,
1898 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1900 const unsigned minor = iminor(file_inode(file));
1901 struct comedi_device *dev = comedi_dev_from_minor(minor);
1902 struct comedi_subdevice *s;
1903 struct comedi_async *async;
1904 unsigned long start = vma->vm_start;
1913 mutex_lock(&dev->mutex);
1915 if (!dev->attached) {
1916 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1921 if (vma->vm_flags & VM_WRITE)
1922 s = comedi_write_subdevice(dev, minor);
1924 s = comedi_read_subdevice(dev, minor);
1936 if (vma->vm_pgoff != 0) {
1937 DPRINTK("comedi: mmap() offset must be 0.\n");
1942 size = vma->vm_end - vma->vm_start;
1943 if (size > async->prealloc_bufsz) {
1947 if (size & (~PAGE_MASK)) {
1952 n_pages = size >> PAGE_SHIFT;
1953 for (i = 0; i < n_pages; ++i) {
1954 struct comedi_buf_page *buf = &async->buf_page_list[i];
1956 if (remap_pfn_range(vma, start,
1957 page_to_pfn(virt_to_page(buf->virt_addr)),
1958 PAGE_SIZE, PAGE_SHARED)) {
1965 vma->vm_ops = &comedi_vm_ops;
1966 vma->vm_private_data = async;
1968 async->mmap_count++;
1972 mutex_unlock(&dev->mutex);
1976 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1978 unsigned int mask = 0;
1979 const unsigned minor = iminor(file_inode(file));
1980 struct comedi_device *dev = comedi_dev_from_minor(minor);
1981 struct comedi_subdevice *s;
1986 mutex_lock(&dev->mutex);
1988 if (!dev->attached) {
1989 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1993 s = comedi_read_subdevice(dev, minor);
1994 if (s && s->async) {
1995 poll_wait(file, &s->async->wait_head, wait);
1996 if (!s->busy || !comedi_is_subdevice_running(s) ||
1997 comedi_buf_read_n_available(s->async) > 0)
1998 mask |= POLLIN | POLLRDNORM;
2001 s = comedi_write_subdevice(dev, minor);
2002 if (s && s->async) {
2003 unsigned int bps = bytes_per_sample(s->async->subdevice);
2005 poll_wait(file, &s->async->wait_head, wait);
2006 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2007 if (!s->busy || !comedi_is_subdevice_running(s) ||
2008 comedi_buf_write_n_allocated(s->async) >= bps)
2009 mask |= POLLOUT | POLLWRNORM;
2013 mutex_unlock(&dev->mutex);
2017 static ssize_t comedi_write(struct file *file, const char __user *buf,
2018 size_t nbytes, loff_t *offset)
2020 struct comedi_subdevice *s;
2021 struct comedi_async *async;
2022 int n, m, count = 0, retval = 0;
2023 DECLARE_WAITQUEUE(wait, current);
2024 const unsigned minor = iminor(file_inode(file));
2025 struct comedi_device *dev = comedi_dev_from_minor(minor);
2030 if (!dev->attached) {
2031 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2035 s = comedi_write_subdevice(dev, minor);
2036 if (!s || !s->async)
2041 if (!s->busy || !nbytes)
2043 if (s->busy != file)
2046 add_wait_queue(&async->wait_head, &wait);
2047 while (nbytes > 0 && !retval) {
2048 set_current_state(TASK_INTERRUPTIBLE);
2050 if (!comedi_is_subdevice_running(s)) {
2052 mutex_lock(&dev->mutex);
2053 if (comedi_is_subdevice_in_error(s))
2057 do_become_nonbusy(dev, s);
2058 mutex_unlock(&dev->mutex);
2066 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2067 m = async->prealloc_bufsz - async->buf_write_ptr;
2068 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2069 if (m > comedi_buf_write_n_allocated(async))
2070 m = comedi_buf_write_n_allocated(async);
2075 if (file->f_flags & O_NONBLOCK) {
2080 if (signal_pending(current)) {
2081 retval = -ERESTARTSYS;
2086 if (s->busy != file) {
2093 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2099 comedi_buf_write_free(async, n);
2105 break; /* makes device work like a pipe */
2107 set_current_state(TASK_RUNNING);
2108 remove_wait_queue(&async->wait_head, &wait);
2110 return count ? count : retval;
2113 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2116 struct comedi_subdevice *s;
2117 struct comedi_async *async;
2118 int n, m, count = 0, retval = 0;
2119 DECLARE_WAITQUEUE(wait, current);
2120 const unsigned minor = iminor(file_inode(file));
2121 struct comedi_device *dev = comedi_dev_from_minor(minor);
2126 if (!dev->attached) {
2127 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2131 s = comedi_read_subdevice(dev, minor);
2132 if (!s || !s->async)
2136 if (!s->busy || !nbytes)
2138 if (s->busy != file)
2141 add_wait_queue(&async->wait_head, &wait);
2142 while (nbytes > 0 && !retval) {
2143 set_current_state(TASK_INTERRUPTIBLE);
2147 m = comedi_buf_read_n_available(async);
2148 /* printk("%d available\n",m); */
2149 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2150 m = async->prealloc_bufsz - async->buf_read_ptr;
2151 /* printk("%d contiguous\n",m); */
2156 if (!comedi_is_subdevice_running(s)) {
2157 mutex_lock(&dev->mutex);
2158 do_become_nonbusy(dev, s);
2159 if (comedi_is_subdevice_in_error(s))
2163 mutex_unlock(&dev->mutex);
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 mutex_lock(&dev->mutex);
2203 if (async->buf_read_count - async->buf_write_count == 0)
2204 do_become_nonbusy(dev, s);
2205 mutex_unlock(&dev->mutex);
2207 set_current_state(TASK_RUNNING);
2208 remove_wait_queue(&async->wait_head, &wait);
2210 return count ? count : retval;
2213 static int comedi_open(struct inode *inode, struct file *file)
2215 const unsigned minor = iminor(inode);
2216 struct comedi_device *dev = comedi_dev_from_minor(minor);
2219 DPRINTK("invalid minor number\n");
2223 /* This is slightly hacky, but we want module autoloading
2225 * case: user opens device, attached -> ok
2226 * case: user opens device, unattached, !in_request_module -> autoload
2227 * case: user opens device, unattached, in_request_module -> fail
2228 * case: root opens device, attached -> ok
2229 * case: root opens device, unattached, in_request_module -> ok
2230 * (typically called from modprobe)
2231 * case: root opens device, unattached, !in_request_module -> autoload
2233 * The last could be changed to "-> ok", which would deny root
2236 mutex_lock(&dev->mutex);
2239 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2240 DPRINTK("in request module\n");
2241 mutex_unlock(&dev->mutex);
2244 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2247 dev->in_request_module = true;
2250 mutex_unlock(&dev->mutex);
2251 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2252 mutex_lock(&dev->mutex);
2255 dev->in_request_module = false;
2257 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2258 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2259 mutex_unlock(&dev->mutex);
2263 __module_get(THIS_MODULE);
2265 if (dev->attached) {
2266 if (!try_module_get(dev->driver->module)) {
2267 module_put(THIS_MODULE);
2268 mutex_unlock(&dev->mutex);
2273 if (dev->attached && dev->use_count == 0 && dev->open) {
2274 int rc = dev->open(dev);
2276 module_put(dev->driver->module);
2277 module_put(THIS_MODULE);
2278 mutex_unlock(&dev->mutex);
2285 mutex_unlock(&dev->mutex);
2290 static int comedi_fasync(int fd, struct file *file, int on)
2292 const unsigned minor = iminor(file_inode(file));
2293 struct comedi_device *dev = comedi_dev_from_minor(minor);
2298 return fasync_helper(fd, file, on, &dev->async_queue);
2301 static int comedi_close(struct inode *inode, struct file *file)
2303 const unsigned minor = iminor(inode);
2304 struct comedi_device *dev = comedi_dev_from_minor(minor);
2305 struct comedi_subdevice *s = NULL;
2311 mutex_lock(&dev->mutex);
2313 if (dev->subdevices) {
2314 for (i = 0; i < dev->n_subdevices; i++) {
2315 s = &dev->subdevices[i];
2317 if (s->busy == file)
2319 if (s->lock == file)
2323 if (dev->attached && dev->use_count == 1 && dev->close)
2326 module_put(THIS_MODULE);
2328 module_put(dev->driver->module);
2332 mutex_unlock(&dev->mutex);
2337 static const struct file_operations comedi_fops = {
2338 .owner = THIS_MODULE,
2339 .unlocked_ioctl = comedi_unlocked_ioctl,
2340 .compat_ioctl = comedi_compat_ioctl,
2341 .open = comedi_open,
2342 .release = comedi_close,
2343 .read = comedi_read,
2344 .write = comedi_write,
2345 .mmap = comedi_mmap,
2346 .poll = comedi_poll,
2347 .fasync = comedi_fasync,
2348 .llseek = noop_llseek,
2351 void comedi_error(const struct comedi_device *dev, const char *s)
2353 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2355 EXPORT_SYMBOL_GPL(comedi_error);
2357 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2359 struct comedi_async *async = s->async;
2360 unsigned runflags = 0;
2361 unsigned runflags_mask = 0;
2363 /* DPRINTK("comedi_event 0x%x\n",mask); */
2365 if (!comedi_is_subdevice_running(s))
2369 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2370 COMEDI_CB_OVERFLOW)) {
2371 runflags_mask |= SRF_RUNNING;
2373 /* remember if an error event has occurred, so an error
2374 * can be returned the next time the user does a read() */
2375 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2376 runflags_mask |= SRF_ERROR;
2377 runflags |= SRF_ERROR;
2379 if (runflags_mask) {
2380 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2381 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2384 if (async->cb_mask & s->async->events) {
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);
2391 s->async->events = 0;
2393 EXPORT_SYMBOL_GPL(comedi_event);
2395 /* Note: the ->mutex is pre-locked on successful return */
2396 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2398 struct comedi_device *dev;
2399 struct device *csdev;
2402 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2404 return ERR_PTR(-ENOMEM);
2405 comedi_device_init(dev);
2406 comedi_set_hw_dev(dev, hardware_device);
2407 mutex_lock(&dev->mutex);
2408 mutex_lock(&comedi_board_minor_table_lock);
2409 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2410 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2411 if (comedi_board_minor_table[i] == NULL) {
2412 comedi_board_minor_table[i] = dev;
2416 mutex_unlock(&comedi_board_minor_table_lock);
2417 if (i == COMEDI_NUM_BOARD_MINORS) {
2418 mutex_unlock(&dev->mutex);
2419 comedi_device_cleanup(dev);
2421 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2422 return ERR_PTR(-EBUSY);
2425 csdev = device_create(comedi_class, hardware_device,
2426 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2428 dev->class_dev = csdev;
2430 /* Note: dev->mutex needs to be unlocked by the caller. */
2434 static void comedi_free_board_minor(unsigned minor)
2436 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2437 comedi_free_board_dev(comedi_clear_board_minor(minor));
2440 void comedi_release_hardware_device(struct device *hardware_device)
2443 struct comedi_device *dev;
2445 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2447 mutex_lock(&comedi_board_minor_table_lock);
2448 dev = comedi_board_minor_table[minor];
2449 if (dev && dev->hw_dev == hardware_device) {
2450 comedi_board_minor_table[minor] = NULL;
2451 mutex_unlock(&comedi_board_minor_table_lock);
2452 comedi_free_board_dev(dev);
2455 mutex_unlock(&comedi_board_minor_table_lock);
2459 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2461 struct comedi_device *dev = s->device;
2462 struct device *csdev;
2465 mutex_lock(&comedi_subdevice_minor_table_lock);
2466 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2467 if (comedi_subdevice_minor_table[i] == NULL) {
2468 comedi_subdevice_minor_table[i] = s;
2472 mutex_unlock(&comedi_subdevice_minor_table_lock);
2473 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2474 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2477 i += COMEDI_NUM_BOARD_MINORS;
2479 csdev = device_create(comedi_class, dev->class_dev,
2480 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2481 dev->minor, s->index);
2483 s->class_dev = csdev;
2488 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2497 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2498 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2500 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2501 mutex_lock(&comedi_subdevice_minor_table_lock);
2502 if (s == comedi_subdevice_minor_table[i])
2503 comedi_subdevice_minor_table[i] = NULL;
2504 mutex_unlock(&comedi_subdevice_minor_table_lock);
2506 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2507 s->class_dev = NULL;
2511 static void comedi_cleanup_board_minors(void)
2515 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2516 comedi_free_board_minor(i);
2519 static int __init comedi_init(void)
2524 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2526 if (comedi_num_legacy_minors < 0 ||
2527 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2528 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2529 COMEDI_NUM_BOARD_MINORS);
2533 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2534 COMEDI_NUM_MINORS, "comedi");
2537 cdev_init(&comedi_cdev, &comedi_fops);
2538 comedi_cdev.owner = THIS_MODULE;
2539 kobject_set_name(&comedi_cdev.kobj, "comedi");
2540 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2541 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2545 comedi_class = class_create(THIS_MODULE, "comedi");
2546 if (IS_ERR(comedi_class)) {
2547 pr_err("comedi: failed to create class\n");
2548 cdev_del(&comedi_cdev);
2549 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2551 return PTR_ERR(comedi_class);
2554 comedi_class->dev_groups = comedi_dev_groups;
2556 /* XXX requires /proc interface */
2559 /* create devices files for legacy/manual use */
2560 for (i = 0; i < comedi_num_legacy_minors; i++) {
2561 struct comedi_device *dev;
2562 dev = comedi_alloc_board_minor(NULL);
2564 comedi_cleanup_board_minors();
2565 cdev_del(&comedi_cdev);
2566 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2568 return PTR_ERR(dev);
2570 /* comedi_alloc_board_minor() locked the mutex */
2571 mutex_unlock(&dev->mutex);
2577 module_init(comedi_init);
2579 static void __exit comedi_cleanup(void)
2583 comedi_cleanup_board_minors();
2584 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2585 BUG_ON(comedi_board_minor_table[i]);
2586 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2587 BUG_ON(comedi_subdevice_minor_table[i]);
2589 class_destroy(comedi_class);
2590 cdev_del(&comedi_cdev);
2591 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2593 comedi_proc_cleanup();
2595 module_exit(comedi_cleanup);
2597 MODULE_AUTHOR("http://www.comedi.org");
2598 MODULE_DESCRIPTION("Comedi core module");
2599 MODULE_LICENSE("GPL");