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 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 comedi_set_subdevice_runflags(s, ~0, SRF_FREE_SPRIV);
550 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
553 This function restores a subdevice to an idle state.
555 static void do_become_nonbusy(struct comedi_device *dev,
556 struct comedi_subdevice *s)
558 struct comedi_async *async = s->async;
560 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
562 comedi_buf_reset(async);
563 async->inttrig = NULL;
564 kfree(async->cmd.chanlist);
565 async->cmd.chanlist = NULL;
567 dev_err(dev->class_dev,
568 "BUG: (?) do_become_nonbusy called with async=NULL\n");
574 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
578 if (comedi_is_subdevice_running(s) && s->cancel)
579 ret = s->cancel(dev, s);
581 do_become_nonbusy(dev, s);
586 static int is_device_busy(struct comedi_device *dev)
588 struct comedi_subdevice *s;
594 for (i = 0; i < dev->n_subdevices; i++) {
595 s = &dev->subdevices[i];
598 if (s->async && s->async->mmap_count)
610 pointer to devconfig structure
613 devconfig structure at arg
618 static int do_devconfig_ioctl(struct comedi_device *dev,
619 struct comedi_devconfig __user *arg)
621 struct comedi_devconfig it;
623 if (!capable(CAP_SYS_ADMIN))
627 if (is_device_busy(dev))
630 struct module *driver_module = dev->driver->module;
631 comedi_device_detach(dev);
632 module_put(driver_module);
637 if (copy_from_user(&it, arg, sizeof(it)))
640 it.board_name[COMEDI_NAMELEN - 1] = 0;
642 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
643 dev_warn(dev->class_dev,
644 "comedi_config --init_data is deprecated\n");
648 if (dev->minor >= comedi_num_legacy_minors)
649 /* don't re-use dynamically allocated comedi devices */
652 /* This increments the driver module count on success. */
653 return comedi_device_attach(dev, &it);
658 buffer configuration ioctl
661 pointer to bufconfig structure
667 modified bufconfig at arg
670 static int do_bufconfig_ioctl(struct comedi_device *dev,
671 struct comedi_bufconfig __user *arg)
673 struct comedi_bufconfig bc;
674 struct comedi_async *async;
675 struct comedi_subdevice *s;
678 if (copy_from_user(&bc, arg, sizeof(bc)))
681 if (bc.subdevice >= dev->n_subdevices)
684 s = &dev->subdevices[bc.subdevice];
688 DPRINTK("subdevice does not have async capability\n");
694 if (bc.maximum_size) {
695 if (!capable(CAP_SYS_ADMIN))
698 async->max_bufsize = bc.maximum_size;
702 retval = resize_async_buffer(dev, s, async, bc.size);
707 bc.size = async->prealloc_bufsz;
708 bc.maximum_size = async->max_bufsize;
711 if (copy_to_user(arg, &bc, sizeof(bc)))
722 pointer to devinfo structure
731 static int do_devinfo_ioctl(struct comedi_device *dev,
732 struct comedi_devinfo __user *arg,
735 const unsigned minor = iminor(file_inode(file));
736 struct comedi_subdevice *s;
737 struct comedi_devinfo devinfo;
739 memset(&devinfo, 0, sizeof(devinfo));
741 /* fill devinfo structure */
742 devinfo.version_code = COMEDI_VERSION_CODE;
743 devinfo.n_subdevs = dev->n_subdevices;
744 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
745 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
747 s = comedi_read_subdevice(dev, minor);
749 devinfo.read_subdevice = s->index;
751 devinfo.read_subdevice = -1;
753 s = comedi_write_subdevice(dev, minor);
755 devinfo.write_subdevice = s->index;
757 devinfo.write_subdevice = -1;
759 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
770 pointer to array of subdevice info structures
776 array of subdevice info structures at arg
779 static int do_subdinfo_ioctl(struct comedi_device *dev,
780 struct comedi_subdinfo __user *arg, void *file)
783 struct comedi_subdinfo *tmp, *us;
784 struct comedi_subdevice *s;
786 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
790 /* fill subdinfo structs */
791 for (i = 0; i < dev->n_subdevices; i++) {
792 s = &dev->subdevices[i];
796 us->n_chan = s->n_chan;
797 us->subd_flags = s->subdev_flags;
798 if (comedi_is_subdevice_running(s))
799 us->subd_flags |= SDF_RUNNING;
800 #define TIMER_nanosec 5 /* backwards compatibility */
801 us->timer_type = TIMER_nanosec;
802 us->len_chanlist = s->len_chanlist;
803 us->maxdata = s->maxdata;
804 if (s->range_table) {
806 (i << 24) | (0 << 16) | (s->range_table->length);
808 us->range_type = 0; /* XXX */
810 us->flags = s->flags;
813 us->subd_flags |= SDF_BUSY;
815 us->subd_flags |= SDF_BUSY_OWNER;
817 us->subd_flags |= SDF_LOCKED;
819 us->subd_flags |= SDF_LOCK_OWNER;
820 if (!s->maxdata && s->maxdata_list)
821 us->subd_flags |= SDF_MAXDATA;
823 us->subd_flags |= SDF_FLAGS;
824 if (s->range_table_list)
825 us->subd_flags |= SDF_RANGETYPE;
827 us->subd_flags |= SDF_CMD;
829 if (s->insn_bits != &insn_inval)
830 us->insn_bits_support = COMEDI_SUPPORTED;
832 us->insn_bits_support = COMEDI_UNSUPPORTED;
834 us->settling_time_0 = s->settling_time_0;
837 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
841 return ret ? -EFAULT : 0;
849 pointer to chaninfo structure
852 chaninfo structure at arg
855 arrays at elements of chaninfo structure
858 static int do_chaninfo_ioctl(struct comedi_device *dev,
859 struct comedi_chaninfo __user *arg)
861 struct comedi_subdevice *s;
862 struct comedi_chaninfo it;
864 if (copy_from_user(&it, arg, sizeof(it)))
867 if (it.subdev >= dev->n_subdevices)
869 s = &dev->subdevices[it.subdev];
871 if (it.maxdata_list) {
872 if (s->maxdata || !s->maxdata_list)
874 if (copy_to_user(it.maxdata_list, s->maxdata_list,
875 s->n_chan * sizeof(unsigned int)))
882 if (copy_to_user(it.flaglist, s->flaglist,
883 s->n_chan * sizeof(unsigned int)))
890 if (!s->range_table_list)
892 for (i = 0; i < s->n_chan; i++) {
895 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
896 (s->range_table_list[i]->length);
897 if (put_user(x, it.rangelist + i))
901 if (copy_to_user(it.rangelist, s->range_type_list,
902 s->n_chan * sizeof(unsigned int)))
912 buffer information ioctl
915 pointer to bufinfo structure
921 modified bufinfo at arg
924 static int do_bufinfo_ioctl(struct comedi_device *dev,
925 struct comedi_bufinfo __user *arg, void *file)
927 struct comedi_bufinfo bi;
928 struct comedi_subdevice *s;
929 struct comedi_async *async;
931 if (copy_from_user(&bi, arg, sizeof(bi)))
934 if (bi.subdevice >= dev->n_subdevices)
937 s = &dev->subdevices[bi.subdevice];
939 if (s->lock && s->lock != file)
945 DPRINTK("subdevice does not have async capability\n");
946 bi.buf_write_ptr = 0;
948 bi.buf_write_count = 0;
949 bi.buf_read_count = 0;
951 bi.bytes_written = 0;
956 bi.bytes_written = 0;
957 goto copyback_position;
962 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
963 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
964 comedi_buf_read_free(async, bi.bytes_read);
966 if (comedi_is_subdevice_idle(s) &&
967 async->buf_write_count == async->buf_read_count) {
968 do_become_nonbusy(dev, s);
972 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
974 comedi_buf_write_alloc(async, bi.bytes_written);
975 comedi_buf_write_free(async, bi.bytes_written);
979 bi.buf_write_count = async->buf_write_count;
980 bi.buf_write_ptr = async->buf_write_ptr;
981 bi.buf_read_count = async->buf_read_count;
982 bi.buf_read_ptr = async->buf_read_ptr;
985 if (copy_to_user(arg, &bi, sizeof(bi)))
991 static int check_insn_config_length(struct comedi_insn *insn,
998 case INSN_CONFIG_DIO_OUTPUT:
999 case INSN_CONFIG_DIO_INPUT:
1000 case INSN_CONFIG_DISARM:
1001 case INSN_CONFIG_RESET:
1005 case INSN_CONFIG_ARM:
1006 case INSN_CONFIG_DIO_QUERY:
1007 case INSN_CONFIG_BLOCK_SIZE:
1008 case INSN_CONFIG_FILTER:
1009 case INSN_CONFIG_SERIAL_CLOCK:
1010 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1011 case INSN_CONFIG_ALT_SOURCE:
1012 case INSN_CONFIG_SET_COUNTER_MODE:
1013 case INSN_CONFIG_8254_READ_STATUS:
1014 case INSN_CONFIG_SET_ROUTING:
1015 case INSN_CONFIG_GET_ROUTING:
1016 case INSN_CONFIG_GET_PWM_STATUS:
1017 case INSN_CONFIG_PWM_SET_PERIOD:
1018 case INSN_CONFIG_PWM_GET_PERIOD:
1022 case INSN_CONFIG_SET_GATE_SRC:
1023 case INSN_CONFIG_GET_GATE_SRC:
1024 case INSN_CONFIG_SET_CLOCK_SRC:
1025 case INSN_CONFIG_GET_CLOCK_SRC:
1026 case INSN_CONFIG_SET_OTHER_SRC:
1027 case INSN_CONFIG_GET_COUNTER_STATUS:
1028 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1029 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1030 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1034 case INSN_CONFIG_PWM_OUTPUT:
1035 case INSN_CONFIG_ANALOG_TRIG:
1039 case INSN_CONFIG_DIGITAL_TRIG:
1043 /* by default we allow the insn since we don't have checks for
1044 * all possible cases yet */
1046 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1048 pr_warn("comedi: Add a check to %s in %s.\n",
1049 __func__, __FILE__);
1050 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1056 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1057 unsigned int *data, void *file)
1059 struct comedi_subdevice *s;
1063 if (insn->insn & INSN_MASK_SPECIAL) {
1064 /* a non-subdevice instruction */
1066 switch (insn->insn) {
1076 do_gettimeofday(&tv);
1077 data[0] = tv.tv_sec;
1078 data[1] = tv.tv_usec;
1084 if (insn->n != 1 || data[0] >= 100000) {
1088 udelay(data[0] / 1000);
1096 if (insn->subdev >= dev->n_subdevices) {
1097 DPRINTK("%d not usable subdevice\n",
1102 s = &dev->subdevices[insn->subdev];
1104 DPRINTK("no async\n");
1108 if (!s->async->inttrig) {
1109 DPRINTK("no inttrig\n");
1113 ret = s->async->inttrig(dev, s, data[0]);
1118 DPRINTK("invalid insn\n");
1123 /* a subdevice instruction */
1124 unsigned int maxdata;
1126 if (insn->subdev >= dev->n_subdevices) {
1127 DPRINTK("subdevice %d out of range\n", insn->subdev);
1131 s = &dev->subdevices[insn->subdev];
1133 if (s->type == COMEDI_SUBD_UNUSED) {
1134 DPRINTK("%d not usable subdevice\n", insn->subdev);
1139 /* are we locked? (ioctl lock) */
1140 if (s->lock && s->lock != file) {
1141 DPRINTK("device locked\n");
1146 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1149 DPRINTK("bad chanspec\n");
1157 /* This looks arbitrary. It is. */
1158 s->busy = &parse_insn;
1159 switch (insn->insn) {
1161 ret = s->insn_read(dev, s, insn, data);
1164 maxdata = s->maxdata_list
1165 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1167 for (i = 0; i < insn->n; ++i) {
1168 if (data[i] > maxdata) {
1170 DPRINTK("bad data value(s)\n");
1175 ret = s->insn_write(dev, s, insn, data);
1181 /* Most drivers ignore the base channel in
1182 * insn->chanspec. Fix this here if
1183 * the subdevice has <= 32 channels. */
1185 unsigned int orig_mask;
1187 orig_mask = data[0];
1188 if (s->n_chan <= 32) {
1189 shift = CR_CHAN(insn->chanspec);
1197 ret = s->insn_bits(dev, s, insn, data);
1198 data[0] = orig_mask;
1204 ret = check_insn_config_length(insn, data);
1207 ret = s->insn_config(dev, s, insn, data);
1223 * synchronous instructions
1226 * pointer to sync cmd structure
1229 * sync cmd struct at arg
1236 /* arbitrary limits */
1237 #define MAX_SAMPLES 256
1238 static int do_insnlist_ioctl(struct comedi_device *dev,
1239 struct comedi_insnlist __user *arg, void *file)
1241 struct comedi_insnlist insnlist;
1242 struct comedi_insn *insns = NULL;
1243 unsigned int *data = NULL;
1247 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1250 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1252 DPRINTK("kmalloc failed\n");
1257 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1259 DPRINTK("kmalloc failed\n");
1264 if (copy_from_user(insns, insnlist.insns,
1265 sizeof(*insns) * insnlist.n_insns)) {
1266 DPRINTK("copy_from_user failed\n");
1271 for (i = 0; i < insnlist.n_insns; i++) {
1272 if (insns[i].n > MAX_SAMPLES) {
1273 DPRINTK("number of samples too large\n");
1277 if (insns[i].insn & INSN_MASK_WRITE) {
1278 if (copy_from_user(data, insns[i].data,
1279 insns[i].n * sizeof(unsigned int))) {
1280 DPRINTK("copy_from_user failed\n");
1285 ret = parse_insn(dev, insns + i, data, file);
1288 if (insns[i].insn & INSN_MASK_READ) {
1289 if (copy_to_user(insns[i].data, data,
1290 insns[i].n * sizeof(unsigned int))) {
1291 DPRINTK("copy_to_user failed\n");
1311 * synchronous instructions
1317 * struct comedi_insn struct at arg
1323 static int do_insn_ioctl(struct comedi_device *dev,
1324 struct comedi_insn __user *arg, void *file)
1326 struct comedi_insn insn;
1327 unsigned int *data = NULL;
1330 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1336 if (copy_from_user(&insn, arg, sizeof(insn))) {
1341 /* This is where the behavior of insn and insnlist deviate. */
1342 if (insn.n > MAX_SAMPLES)
1343 insn.n = MAX_SAMPLES;
1344 if (insn.insn & INSN_MASK_WRITE) {
1345 if (copy_from_user(data,
1347 insn.n * sizeof(unsigned int))) {
1352 ret = parse_insn(dev, &insn, data, file);
1355 if (insn.insn & INSN_MASK_READ) {
1356 if (copy_to_user(insn.data,
1358 insn.n * sizeof(unsigned int))) {
1371 static int do_cmd_ioctl(struct comedi_device *dev,
1372 struct comedi_cmd __user *arg, void *file)
1374 struct comedi_cmd cmd;
1375 struct comedi_subdevice *s;
1376 struct comedi_async *async;
1378 unsigned int __user *user_chanlist;
1380 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1381 DPRINTK("bad cmd address\n");
1384 /* save user's chanlist pointer so it can be restored later */
1385 user_chanlist = (unsigned int __user *)cmd.chanlist;
1387 if (cmd.subdev >= dev->n_subdevices) {
1388 DPRINTK("%d no such subdevice\n", cmd.subdev);
1392 s = &dev->subdevices[cmd.subdev];
1395 if (s->type == COMEDI_SUBD_UNUSED) {
1396 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1400 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1401 DPRINTK("subdevice %i does not support commands\n",
1406 /* are we locked? (ioctl lock) */
1407 if (s->lock && s->lock != file) {
1408 DPRINTK("subdevice locked\n");
1414 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);
1425 /* make sure channel/gain list isn't too short */
1426 if (cmd.chanlist_len < 1) {
1427 DPRINTK("channel/gain list too short %u < 1\n",
1433 async->cmd.data = NULL;
1434 /* load channel/gain list */
1435 async->cmd.chanlist =
1436 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1437 if (!async->cmd.chanlist) {
1438 DPRINTK("allocation failed\n");
1442 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1443 async->cmd.chanlist_len * sizeof(int))) {
1444 DPRINTK("fault reading chanlist\n");
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, ~0, SRF_USER | 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 */
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;
1710 if (arg >= dev->n_subdevices)
1712 s = &dev->subdevices[arg];
1713 if (s->async == NULL)
1716 if (s->lock && s->lock != file)
1722 if (s->busy != file)
1725 ret = do_cancel(dev, s);
1726 if (comedi_get_subdevice_runflags(s) & SRF_USER)
1727 wake_up_interruptible(&s->async->wait_head);
1734 instructs driver to synchronize buffers
1746 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1749 struct comedi_subdevice *s;
1751 if (arg >= dev->n_subdevices)
1753 s = &dev->subdevices[arg];
1755 if (s->lock && s->lock != file)
1761 if (s->busy != file)
1765 return s->poll(dev, s);
1770 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1773 const unsigned minor = iminor(file_inode(file));
1774 struct comedi_device *dev = comedi_dev_from_minor(minor);
1780 mutex_lock(&dev->mutex);
1782 /* Device config is special, because it must work on
1783 * an unconfigured device. */
1784 if (cmd == COMEDI_DEVCONFIG) {
1785 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1786 /* Device config not appropriate on non-board minors. */
1790 rc = do_devconfig_ioctl(dev,
1791 (struct comedi_devconfig __user *)arg);
1794 dev->minor >= comedi_num_legacy_minors) {
1795 /* Successfully unconfigured a dynamically
1796 * allocated device. Try and remove it. */
1797 if (comedi_clear_board_dev(dev)) {
1798 mutex_unlock(&dev->mutex);
1799 comedi_free_board_dev(dev);
1807 if (!dev->attached) {
1808 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1814 case COMEDI_BUFCONFIG:
1815 rc = do_bufconfig_ioctl(dev,
1816 (struct comedi_bufconfig __user *)arg);
1818 case COMEDI_DEVINFO:
1819 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1822 case COMEDI_SUBDINFO:
1823 rc = do_subdinfo_ioctl(dev,
1824 (struct comedi_subdinfo __user *)arg,
1827 case COMEDI_CHANINFO:
1828 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1830 case COMEDI_RANGEINFO:
1831 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1833 case COMEDI_BUFINFO:
1834 rc = do_bufinfo_ioctl(dev,
1835 (struct comedi_bufinfo __user *)arg,
1839 rc = do_lock_ioctl(dev, arg, file);
1842 rc = do_unlock_ioctl(dev, arg, file);
1845 rc = do_cancel_ioctl(dev, arg, file);
1848 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1850 case COMEDI_CMDTEST:
1851 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1854 case COMEDI_INSNLIST:
1855 rc = do_insnlist_ioctl(dev,
1856 (struct comedi_insnlist __user *)arg,
1860 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1864 rc = do_poll_ioctl(dev, arg, file);
1872 mutex_unlock(&dev->mutex);
1876 static void comedi_vm_open(struct vm_area_struct *area)
1878 struct comedi_async *async;
1879 struct comedi_device *dev;
1881 async = area->vm_private_data;
1882 dev = async->subdevice->device;
1884 mutex_lock(&dev->mutex);
1885 async->mmap_count++;
1886 mutex_unlock(&dev->mutex);
1889 static void comedi_vm_close(struct vm_area_struct *area)
1891 struct comedi_async *async;
1892 struct comedi_device *dev;
1894 async = area->vm_private_data;
1895 dev = async->subdevice->device;
1897 mutex_lock(&dev->mutex);
1898 async->mmap_count--;
1899 mutex_unlock(&dev->mutex);
1902 static struct vm_operations_struct comedi_vm_ops = {
1903 .open = comedi_vm_open,
1904 .close = comedi_vm_close,
1907 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1909 const unsigned minor = iminor(file_inode(file));
1910 struct comedi_device *dev = comedi_dev_from_minor(minor);
1911 struct comedi_subdevice *s;
1912 struct comedi_async *async;
1913 unsigned long start = vma->vm_start;
1922 mutex_lock(&dev->mutex);
1924 if (!dev->attached) {
1925 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1930 if (vma->vm_flags & VM_WRITE)
1931 s = comedi_write_subdevice(dev, minor);
1933 s = comedi_read_subdevice(dev, minor);
1945 if (vma->vm_pgoff != 0) {
1946 DPRINTK("comedi: mmap() offset must be 0.\n");
1951 size = vma->vm_end - vma->vm_start;
1952 if (size > async->prealloc_bufsz) {
1956 if (size & (~PAGE_MASK)) {
1961 n_pages = size >> PAGE_SHIFT;
1962 for (i = 0; i < n_pages; ++i) {
1963 struct comedi_buf_page *buf = &async->buf_page_list[i];
1965 if (remap_pfn_range(vma, start,
1966 page_to_pfn(virt_to_page(buf->virt_addr)),
1967 PAGE_SIZE, PAGE_SHARED)) {
1974 vma->vm_ops = &comedi_vm_ops;
1975 vma->vm_private_data = async;
1977 async->mmap_count++;
1981 mutex_unlock(&dev->mutex);
1985 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1987 unsigned int mask = 0;
1988 const unsigned minor = iminor(file_inode(file));
1989 struct comedi_device *dev = comedi_dev_from_minor(minor);
1990 struct comedi_subdevice *s;
1995 mutex_lock(&dev->mutex);
1997 if (!dev->attached) {
1998 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2002 s = comedi_read_subdevice(dev, minor);
2003 if (s && s->async) {
2004 poll_wait(file, &s->async->wait_head, wait);
2005 if (!s->busy || !comedi_is_subdevice_running(s) ||
2006 comedi_buf_read_n_available(s->async) > 0)
2007 mask |= POLLIN | POLLRDNORM;
2010 s = comedi_write_subdevice(dev, minor);
2011 if (s && s->async) {
2012 unsigned int bps = bytes_per_sample(s->async->subdevice);
2014 poll_wait(file, &s->async->wait_head, wait);
2015 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2016 if (!s->busy || !comedi_is_subdevice_running(s) ||
2017 comedi_buf_write_n_allocated(s->async) >= bps)
2018 mask |= POLLOUT | POLLWRNORM;
2022 mutex_unlock(&dev->mutex);
2026 static ssize_t comedi_write(struct file *file, const char __user *buf,
2027 size_t nbytes, loff_t *offset)
2029 struct comedi_subdevice *s;
2030 struct comedi_async *async;
2031 int n, m, count = 0, retval = 0;
2032 DECLARE_WAITQUEUE(wait, current);
2033 const unsigned minor = iminor(file_inode(file));
2034 struct comedi_device *dev = comedi_dev_from_minor(minor);
2039 if (!dev->attached) {
2040 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2044 s = comedi_write_subdevice(dev, minor);
2045 if (!s || !s->async)
2050 if (!s->busy || !nbytes)
2052 if (s->busy != file)
2055 add_wait_queue(&async->wait_head, &wait);
2056 while (nbytes > 0 && !retval) {
2057 set_current_state(TASK_INTERRUPTIBLE);
2059 if (!comedi_is_subdevice_running(s)) {
2061 mutex_lock(&dev->mutex);
2062 if (comedi_is_subdevice_in_error(s))
2066 do_become_nonbusy(dev, s);
2067 mutex_unlock(&dev->mutex);
2075 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2076 m = async->prealloc_bufsz - async->buf_write_ptr;
2077 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2078 if (m > comedi_buf_write_n_allocated(async))
2079 m = comedi_buf_write_n_allocated(async);
2084 if (file->f_flags & O_NONBLOCK) {
2089 if (signal_pending(current)) {
2090 retval = -ERESTARTSYS;
2095 if (s->busy != file) {
2102 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2108 comedi_buf_write_free(async, n);
2114 break; /* makes device work like a pipe */
2116 set_current_state(TASK_RUNNING);
2117 remove_wait_queue(&async->wait_head, &wait);
2119 return count ? count : retval;
2122 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2125 struct comedi_subdevice *s;
2126 struct comedi_async *async;
2127 int n, m, count = 0, retval = 0;
2128 DECLARE_WAITQUEUE(wait, current);
2129 const unsigned minor = iminor(file_inode(file));
2130 struct comedi_device *dev = comedi_dev_from_minor(minor);
2135 if (!dev->attached) {
2136 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2140 s = comedi_read_subdevice(dev, minor);
2141 if (!s || !s->async)
2145 if (!s->busy || !nbytes)
2147 if (s->busy != file)
2150 add_wait_queue(&async->wait_head, &wait);
2151 while (nbytes > 0 && !retval) {
2152 set_current_state(TASK_INTERRUPTIBLE);
2156 m = comedi_buf_read_n_available(async);
2157 /* printk("%d available\n",m); */
2158 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2159 m = async->prealloc_bufsz - async->buf_read_ptr;
2160 /* printk("%d contiguous\n",m); */
2165 if (!comedi_is_subdevice_running(s)) {
2166 mutex_lock(&dev->mutex);
2167 do_become_nonbusy(dev, s);
2168 if (comedi_is_subdevice_in_error(s))
2172 mutex_unlock(&dev->mutex);
2175 if (file->f_flags & O_NONBLOCK) {
2180 if (signal_pending(current)) {
2181 retval = -ERESTARTSYS;
2188 if (s->busy != file) {
2194 m = copy_to_user(buf, async->prealloc_buf +
2195 async->buf_read_ptr, n);
2201 comedi_buf_read_alloc(async, n);
2202 comedi_buf_read_free(async, n);
2208 break; /* makes device work like a pipe */
2210 if (comedi_is_subdevice_idle(s)) {
2211 mutex_lock(&dev->mutex);
2212 if (async->buf_read_count - async->buf_write_count == 0)
2213 do_become_nonbusy(dev, s);
2214 mutex_unlock(&dev->mutex);
2216 set_current_state(TASK_RUNNING);
2217 remove_wait_queue(&async->wait_head, &wait);
2219 return count ? count : retval;
2222 static int comedi_open(struct inode *inode, struct file *file)
2224 const unsigned minor = iminor(inode);
2225 struct comedi_device *dev = comedi_dev_from_minor(minor);
2228 DPRINTK("invalid minor number\n");
2232 /* This is slightly hacky, but we want module autoloading
2234 * case: user opens device, attached -> ok
2235 * case: user opens device, unattached, !in_request_module -> autoload
2236 * case: user opens device, unattached, in_request_module -> fail
2237 * case: root opens device, attached -> ok
2238 * case: root opens device, unattached, in_request_module -> ok
2239 * (typically called from modprobe)
2240 * case: root opens device, unattached, !in_request_module -> autoload
2242 * The last could be changed to "-> ok", which would deny root
2245 mutex_lock(&dev->mutex);
2248 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2249 DPRINTK("in request module\n");
2250 mutex_unlock(&dev->mutex);
2253 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2256 dev->in_request_module = true;
2259 mutex_unlock(&dev->mutex);
2260 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2261 mutex_lock(&dev->mutex);
2264 dev->in_request_module = false;
2266 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2267 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2268 mutex_unlock(&dev->mutex);
2272 __module_get(THIS_MODULE);
2274 if (dev->attached) {
2275 if (!try_module_get(dev->driver->module)) {
2276 module_put(THIS_MODULE);
2277 mutex_unlock(&dev->mutex);
2282 if (dev->attached && dev->use_count == 0 && dev->open) {
2283 int rc = dev->open(dev);
2285 module_put(dev->driver->module);
2286 module_put(THIS_MODULE);
2287 mutex_unlock(&dev->mutex);
2294 mutex_unlock(&dev->mutex);
2299 static int comedi_fasync(int fd, struct file *file, int on)
2301 const unsigned minor = iminor(file_inode(file));
2302 struct comedi_device *dev = comedi_dev_from_minor(minor);
2307 return fasync_helper(fd, file, on, &dev->async_queue);
2310 static int comedi_close(struct inode *inode, struct file *file)
2312 const unsigned minor = iminor(inode);
2313 struct comedi_device *dev = comedi_dev_from_minor(minor);
2314 struct comedi_subdevice *s = NULL;
2320 mutex_lock(&dev->mutex);
2322 if (dev->subdevices) {
2323 for (i = 0; i < dev->n_subdevices; i++) {
2324 s = &dev->subdevices[i];
2326 if (s->busy == file)
2328 if (s->lock == file)
2332 if (dev->attached && dev->use_count == 1 && dev->close)
2335 module_put(THIS_MODULE);
2337 module_put(dev->driver->module);
2341 mutex_unlock(&dev->mutex);
2346 static const struct file_operations comedi_fops = {
2347 .owner = THIS_MODULE,
2348 .unlocked_ioctl = comedi_unlocked_ioctl,
2349 .compat_ioctl = comedi_compat_ioctl,
2350 .open = comedi_open,
2351 .release = comedi_close,
2352 .read = comedi_read,
2353 .write = comedi_write,
2354 .mmap = comedi_mmap,
2355 .poll = comedi_poll,
2356 .fasync = comedi_fasync,
2357 .llseek = noop_llseek,
2360 void comedi_error(const struct comedi_device *dev, const char *s)
2362 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2364 EXPORT_SYMBOL_GPL(comedi_error);
2366 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2368 struct comedi_async *async = s->async;
2369 unsigned runflags = 0;
2370 unsigned runflags_mask = 0;
2372 /* DPRINTK("comedi_event 0x%x\n",mask); */
2374 if (!comedi_is_subdevice_running(s))
2378 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2379 COMEDI_CB_OVERFLOW)) {
2380 runflags_mask |= SRF_RUNNING;
2382 /* remember if an error event has occurred, so an error
2383 * can be returned the next time the user does a read() */
2384 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2385 runflags_mask |= SRF_ERROR;
2386 runflags |= SRF_ERROR;
2388 if (runflags_mask) {
2389 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2390 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2393 if (async->cb_mask & s->async->events) {
2394 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2395 wake_up_interruptible(&async->wait_head);
2396 if (s->subdev_flags & SDF_CMD_READ)
2397 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2398 if (s->subdev_flags & SDF_CMD_WRITE)
2399 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2402 async->cb_func(s->async->events, async->cb_arg);
2405 s->async->events = 0;
2407 EXPORT_SYMBOL_GPL(comedi_event);
2409 /* Note: the ->mutex is pre-locked on successful return */
2410 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2412 struct comedi_device *dev;
2413 struct device *csdev;
2416 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2418 return ERR_PTR(-ENOMEM);
2419 comedi_device_init(dev);
2420 comedi_set_hw_dev(dev, hardware_device);
2421 mutex_lock(&dev->mutex);
2422 mutex_lock(&comedi_board_minor_table_lock);
2423 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2424 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2425 if (comedi_board_minor_table[i] == NULL) {
2426 comedi_board_minor_table[i] = dev;
2430 mutex_unlock(&comedi_board_minor_table_lock);
2431 if (i == COMEDI_NUM_BOARD_MINORS) {
2432 mutex_unlock(&dev->mutex);
2433 comedi_device_cleanup(dev);
2435 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2436 return ERR_PTR(-EBUSY);
2439 csdev = device_create(comedi_class, hardware_device,
2440 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2442 dev->class_dev = csdev;
2444 /* Note: dev->mutex needs to be unlocked by the caller. */
2448 static void comedi_free_board_minor(unsigned minor)
2450 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2451 comedi_free_board_dev(comedi_clear_board_minor(minor));
2454 void comedi_release_hardware_device(struct device *hardware_device)
2457 struct comedi_device *dev;
2459 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2461 mutex_lock(&comedi_board_minor_table_lock);
2462 dev = comedi_board_minor_table[minor];
2463 if (dev && dev->hw_dev == hardware_device) {
2464 comedi_board_minor_table[minor] = NULL;
2465 mutex_unlock(&comedi_board_minor_table_lock);
2466 comedi_free_board_dev(dev);
2469 mutex_unlock(&comedi_board_minor_table_lock);
2473 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2475 struct comedi_device *dev = s->device;
2476 struct device *csdev;
2479 mutex_lock(&comedi_subdevice_minor_table_lock);
2480 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2481 if (comedi_subdevice_minor_table[i] == NULL) {
2482 comedi_subdevice_minor_table[i] = s;
2486 mutex_unlock(&comedi_subdevice_minor_table_lock);
2487 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2488 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2491 i += COMEDI_NUM_BOARD_MINORS;
2493 csdev = device_create(comedi_class, dev->class_dev,
2494 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2495 dev->minor, s->index);
2497 s->class_dev = csdev;
2502 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2511 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2512 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2514 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2515 mutex_lock(&comedi_subdevice_minor_table_lock);
2516 if (s == comedi_subdevice_minor_table[i])
2517 comedi_subdevice_minor_table[i] = NULL;
2518 mutex_unlock(&comedi_subdevice_minor_table_lock);
2520 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2521 s->class_dev = NULL;
2525 static void comedi_cleanup_board_minors(void)
2529 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2530 comedi_free_board_minor(i);
2533 static int __init comedi_init(void)
2538 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2540 if (comedi_num_legacy_minors < 0 ||
2541 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2542 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2543 COMEDI_NUM_BOARD_MINORS);
2547 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2548 COMEDI_NUM_MINORS, "comedi");
2551 cdev_init(&comedi_cdev, &comedi_fops);
2552 comedi_cdev.owner = THIS_MODULE;
2553 kobject_set_name(&comedi_cdev.kobj, "comedi");
2554 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2555 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2559 comedi_class = class_create(THIS_MODULE, "comedi");
2560 if (IS_ERR(comedi_class)) {
2561 pr_err("comedi: failed to create class\n");
2562 cdev_del(&comedi_cdev);
2563 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2565 return PTR_ERR(comedi_class);
2568 comedi_class->dev_groups = comedi_dev_groups;
2570 /* XXX requires /proc interface */
2573 /* create devices files for legacy/manual use */
2574 for (i = 0; i < comedi_num_legacy_minors; i++) {
2575 struct comedi_device *dev;
2576 dev = comedi_alloc_board_minor(NULL);
2578 comedi_cleanup_board_minors();
2579 cdev_del(&comedi_cdev);
2580 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2582 return PTR_ERR(dev);
2584 /* comedi_alloc_board_minor() locked the mutex */
2585 mutex_unlock(&dev->mutex);
2591 module_init(comedi_init);
2593 static void __exit comedi_cleanup(void)
2597 comedi_cleanup_board_minors();
2598 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2599 BUG_ON(comedi_board_minor_table[i]);
2600 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2601 BUG_ON(comedi_subdevice_minor_table[i]);
2603 class_destroy(comedi_class);
2604 cdev_del(&comedi_cdev);
2605 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2607 comedi_proc_cleanup();
2609 module_exit(comedi_cleanup);
2611 MODULE_AUTHOR("http://www.comedi.org");
2612 MODULE_DESCRIPTION("Comedi core module");
2613 MODULE_LICENSE("GPL");