5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "comedi_compat32.h"
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/fcntl.h>
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/kmod.h>
38 #include <linux/poll.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/vmalloc.h>
43 #include "comedidev.h"
44 #include <linux/cdev.h>
45 #include <linux/stat.h>
48 #include <linux/uaccess.h>
50 #include "comedi_internal.h"
52 #define COMEDI_NUM_MINORS 0x100
53 #define COMEDI_NUM_SUBDEVICE_MINORS \
54 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
56 #ifdef CONFIG_COMEDI_DEBUG
58 EXPORT_SYMBOL(comedi_debug);
59 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
60 MODULE_PARM_DESC(comedi_debug,
61 "enable comedi core and driver debugging if non-zero (default 0)"
65 static int comedi_num_legacy_minors;
66 module_param(comedi_num_legacy_minors, int, S_IRUGO);
67 MODULE_PARM_DESC(comedi_num_legacy_minors,
68 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
71 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
72 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
73 MODULE_PARM_DESC(comedi_default_buf_size_kb,
74 "default asynchronous buffer size in KiB (default "
75 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
77 unsigned int comedi_default_buf_maxsize_kb
78 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
79 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
80 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
81 "default maximum size of asynchronous buffer in KiB (default "
82 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
84 struct comedi_file_info {
85 struct comedi_device *device;
86 struct comedi_subdevice *read_subdevice;
87 struct comedi_subdevice *write_subdevice;
90 static DEFINE_MUTEX(comedi_board_minor_table_lock);
91 static struct comedi_file_info
92 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
94 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
95 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
96 static struct comedi_file_info
97 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
99 static struct class *comedi_class;
100 static struct cdev comedi_cdev;
102 static void comedi_device_init(struct comedi_device *dev)
104 spin_lock_init(&dev->spinlock);
105 mutex_init(&dev->mutex);
109 static void comedi_device_cleanup(struct comedi_device *dev)
111 struct module *driver_module = NULL;
115 mutex_lock(&dev->mutex);
117 driver_module = dev->driver->module;
118 comedi_device_detach(dev);
119 while (dev->use_count > 0) {
121 module_put(driver_module);
122 module_put(THIS_MODULE);
125 mutex_unlock(&dev->mutex);
126 mutex_destroy(&dev->mutex);
129 static struct comedi_file_info *comedi_clear_board_minor(unsigned minor)
131 struct comedi_file_info *info;
133 mutex_lock(&comedi_board_minor_table_lock);
134 info = comedi_board_minor_table[minor];
135 comedi_board_minor_table[minor] = NULL;
136 mutex_unlock(&comedi_board_minor_table_lock);
140 static void comedi_free_board_file_info(struct comedi_file_info *info)
143 struct comedi_device *dev = info->device;
145 if (dev->class_dev) {
146 device_destroy(comedi_class,
147 MKDEV(COMEDI_MAJOR, dev->minor));
149 comedi_device_cleanup(dev);
156 static struct comedi_file_info
157 *comedi_file_info_from_board_minor(unsigned minor)
159 struct comedi_file_info *info;
161 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
162 mutex_lock(&comedi_board_minor_table_lock);
163 info = comedi_board_minor_table[minor];
164 mutex_unlock(&comedi_board_minor_table_lock);
168 static struct comedi_file_info
169 *comedi_file_info_from_subdevice_minor(unsigned minor)
171 struct comedi_file_info *info;
172 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
174 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
175 mutex_lock(&comedi_subdevice_minor_table_lock);
176 info = comedi_subdevice_minor_table[i];
177 mutex_unlock(&comedi_subdevice_minor_table_lock);
181 static struct comedi_device *
182 comedi_dev_from_file_info(struct comedi_file_info *info)
184 return info ? info->device : NULL;
187 static struct comedi_device *comedi_dev_from_board_minor(unsigned minor)
189 struct comedi_file_info *info;
191 info = comedi_file_info_from_board_minor(minor);
192 return comedi_dev_from_file_info(info);
195 static struct comedi_device *comedi_dev_from_subdevice_minor(unsigned minor)
197 struct comedi_file_info *info;
199 info = comedi_file_info_from_subdevice_minor(minor);
200 return comedi_dev_from_file_info(info);
203 struct comedi_device *comedi_dev_from_minor(unsigned minor)
205 if (minor < COMEDI_NUM_BOARD_MINORS)
206 return comedi_dev_from_board_minor(minor);
208 return comedi_dev_from_subdevice_minor(minor);
210 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
212 static struct comedi_subdevice *
213 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
215 struct comedi_file_info *info;
217 if (minor >= COMEDI_NUM_BOARD_MINORS) {
218 info = comedi_file_info_from_subdevice_minor(minor);
219 if (!info || info->device != dev)
221 if (info->read_subdevice)
222 return info->read_subdevice;
224 return dev->read_subdev;
227 static struct comedi_subdevice *
228 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
230 struct comedi_file_info *info;
232 if (minor >= COMEDI_NUM_BOARD_MINORS) {
233 info = comedi_file_info_from_subdevice_minor(minor);
234 if (!info || info->device != dev)
236 if (info->write_subdevice)
237 return info->write_subdevice;
239 return dev->write_subdev;
242 static int resize_async_buffer(struct comedi_device *dev,
243 struct comedi_subdevice *s,
244 struct comedi_async *async, unsigned new_size)
248 if (new_size > async->max_bufsize)
252 DPRINTK("subdevice is busy, cannot resize buffer\n");
255 if (async->mmap_count) {
256 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
260 if (!async->prealloc_buf)
263 /* make sure buffer is an integral number of pages
265 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
267 retval = comedi_buf_alloc(dev, s, new_size);
272 retval = s->buf_change(dev, s, new_size);
277 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
278 dev->minor, s->index, async->prealloc_bufsz);
282 /* sysfs attribute files */
284 static ssize_t show_max_read_buffer_kb(struct device *csdev,
285 struct device_attribute *attr, char *buf)
287 unsigned int minor = MINOR(csdev->devt);
288 struct comedi_device *dev;
289 struct comedi_subdevice *s;
290 unsigned int size = 0;
292 dev = comedi_dev_from_minor(minor);
296 mutex_lock(&dev->mutex);
297 s = comedi_read_subdevice(dev, minor);
298 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
299 size = s->async->max_bufsize / 1024;
300 mutex_unlock(&dev->mutex);
302 return snprintf(buf, PAGE_SIZE, "%i\n", size);
305 static ssize_t store_max_read_buffer_kb(struct device *csdev,
306 struct device_attribute *attr,
307 const char *buf, size_t count)
309 unsigned int minor = MINOR(csdev->devt);
310 struct comedi_device *dev;
311 struct comedi_subdevice *s;
315 err = kstrtouint(buf, 10, &size);
318 if (size > (UINT_MAX / 1024))
322 dev = comedi_dev_from_minor(minor);
326 mutex_lock(&dev->mutex);
327 s = comedi_read_subdevice(dev, minor);
328 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
329 s->async->max_bufsize = size;
332 mutex_unlock(&dev->mutex);
334 return err ? err : count;
337 static ssize_t show_read_buffer_kb(struct device *csdev,
338 struct device_attribute *attr, char *buf)
340 unsigned int minor = MINOR(csdev->devt);
341 struct comedi_device *dev;
342 struct comedi_subdevice *s;
343 unsigned int size = 0;
345 dev = comedi_dev_from_minor(minor);
349 mutex_lock(&dev->mutex);
350 s = comedi_read_subdevice(dev, minor);
351 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
352 size = s->async->prealloc_bufsz / 1024;
353 mutex_unlock(&dev->mutex);
355 return snprintf(buf, PAGE_SIZE, "%i\n", size);
358 static ssize_t store_read_buffer_kb(struct device *csdev,
359 struct device_attribute *attr,
360 const char *buf, size_t count)
362 unsigned int minor = MINOR(csdev->devt);
363 struct comedi_device *dev;
364 struct comedi_subdevice *s;
368 err = kstrtouint(buf, 10, &size);
371 if (size > (UINT_MAX / 1024))
375 dev = comedi_dev_from_minor(minor);
379 mutex_lock(&dev->mutex);
380 s = comedi_read_subdevice(dev, minor);
381 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
382 err = resize_async_buffer(dev, s, s->async, size);
385 mutex_unlock(&dev->mutex);
387 return err ? err : count;
390 static ssize_t show_max_write_buffer_kb(struct device *csdev,
391 struct device_attribute *attr,
394 unsigned int minor = MINOR(csdev->devt);
395 struct comedi_device *dev;
396 struct comedi_subdevice *s;
397 unsigned int size = 0;
399 dev = comedi_dev_from_minor(minor);
403 mutex_lock(&dev->mutex);
404 s = comedi_write_subdevice(dev, minor);
405 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
406 size = s->async->max_bufsize / 1024;
407 mutex_unlock(&dev->mutex);
409 return snprintf(buf, PAGE_SIZE, "%i\n", size);
412 static ssize_t store_max_write_buffer_kb(struct device *csdev,
413 struct device_attribute *attr,
414 const char *buf, size_t count)
416 unsigned int minor = MINOR(csdev->devt);
417 struct comedi_device *dev;
418 struct comedi_subdevice *s;
422 err = kstrtouint(buf, 10, &size);
425 if (size > (UINT_MAX / 1024))
429 dev = comedi_dev_from_minor(minor);
433 mutex_lock(&dev->mutex);
434 s = comedi_write_subdevice(dev, minor);
435 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
436 s->async->max_bufsize = size;
439 mutex_unlock(&dev->mutex);
441 return err ? err : count;
444 static ssize_t show_write_buffer_kb(struct device *csdev,
445 struct device_attribute *attr, char *buf)
447 unsigned int minor = MINOR(csdev->devt);
448 struct comedi_device *dev;
449 struct comedi_subdevice *s;
450 unsigned int size = 0;
452 dev = comedi_dev_from_minor(minor);
456 mutex_lock(&dev->mutex);
457 s = comedi_write_subdevice(dev, minor);
458 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
459 size = s->async->prealloc_bufsz / 1024;
460 mutex_unlock(&dev->mutex);
462 return snprintf(buf, PAGE_SIZE, "%i\n", size);
465 static ssize_t store_write_buffer_kb(struct device *csdev,
466 struct device_attribute *attr,
467 const char *buf, size_t count)
469 unsigned int minor = MINOR(csdev->devt);
470 struct comedi_device *dev;
471 struct comedi_subdevice *s;
475 err = kstrtouint(buf, 10, &size);
478 if (size > (UINT_MAX / 1024))
482 dev = comedi_dev_from_minor(minor);
486 mutex_lock(&dev->mutex);
487 s = comedi_write_subdevice(dev, minor);
488 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
489 err = resize_async_buffer(dev, s, s->async, size);
492 mutex_unlock(&dev->mutex);
494 return err ? err : count;
497 static struct device_attribute comedi_dev_attrs[] = {
498 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
499 show_max_read_buffer_kb, store_max_read_buffer_kb),
500 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
501 show_read_buffer_kb, store_read_buffer_kb),
502 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
503 show_max_write_buffer_kb, store_max_write_buffer_kb),
504 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
505 show_write_buffer_kb, store_write_buffer_kb),
509 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
510 unsigned mask, unsigned bits)
514 spin_lock_irqsave(&s->spin_lock, flags);
515 s->runflags &= ~mask;
516 s->runflags |= (bits & mask);
517 spin_unlock_irqrestore(&s->spin_lock, flags);
520 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
525 spin_lock_irqsave(&s->spin_lock, flags);
526 runflags = s->runflags;
527 spin_unlock_irqrestore(&s->spin_lock, flags);
531 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
533 unsigned runflags = comedi_get_subdevice_runflags(s);
535 return (runflags & SRF_RUNNING) ? true : false;
537 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
539 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
541 unsigned runflags = comedi_get_subdevice_runflags(s);
543 return (runflags & SRF_ERROR) ? true : false;
546 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
548 unsigned runflags = comedi_get_subdevice_runflags(s);
550 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
554 This function restores a subdevice to an idle state.
556 static void do_become_nonbusy(struct comedi_device *dev,
557 struct comedi_subdevice *s)
559 struct comedi_async *async = s->async;
561 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
563 comedi_buf_reset(async);
564 async->inttrig = NULL;
565 kfree(async->cmd.chanlist);
566 async->cmd.chanlist = NULL;
568 dev_err(dev->class_dev,
569 "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 static int is_device_busy(struct comedi_device *dev)
589 struct comedi_subdevice *s;
595 for (i = 0; i < dev->n_subdevices; i++) {
596 s = &dev->subdevices[i];
599 if (s->async && s->async->mmap_count)
611 pointer to devconfig structure
614 devconfig structure at arg
619 static int do_devconfig_ioctl(struct comedi_device *dev,
620 struct comedi_devconfig __user *arg)
622 struct comedi_devconfig it;
624 if (!capable(CAP_SYS_ADMIN))
628 if (is_device_busy(dev))
631 struct module *driver_module = dev->driver->module;
632 comedi_device_detach(dev);
633 module_put(driver_module);
638 if (copy_from_user(&it, arg, sizeof(it)))
641 it.board_name[COMEDI_NAMELEN - 1] = 0;
643 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
644 dev_warn(dev->class_dev,
645 "comedi_config --init_data is deprecated\n");
649 if (dev->minor >= comedi_num_legacy_minors)
650 /* don't re-use dynamically allocated comedi devices */
653 /* This increments the driver module count on success. */
654 return comedi_device_attach(dev, &it);
659 buffer configuration ioctl
662 pointer to bufconfig structure
668 modified bufconfig at arg
671 static int do_bufconfig_ioctl(struct comedi_device *dev,
672 struct comedi_bufconfig __user *arg)
674 struct comedi_bufconfig bc;
675 struct comedi_async *async;
676 struct comedi_subdevice *s;
679 if (copy_from_user(&bc, arg, sizeof(bc)))
682 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
685 s = &dev->subdevices[bc.subdevice];
689 DPRINTK("subdevice does not have async capability\n");
695 if (bc.maximum_size) {
696 if (!capable(CAP_SYS_ADMIN))
699 async->max_bufsize = bc.maximum_size;
703 retval = resize_async_buffer(dev, s, async, bc.size);
708 bc.size = async->prealloc_bufsz;
709 bc.maximum_size = async->max_bufsize;
712 if (copy_to_user(arg, &bc, sizeof(bc)))
723 pointer to devinfo structure
732 static int do_devinfo_ioctl(struct comedi_device *dev,
733 struct comedi_devinfo __user *arg,
736 const unsigned minor = iminor(file_inode(file));
737 struct comedi_subdevice *s;
738 struct comedi_devinfo devinfo;
740 memset(&devinfo, 0, sizeof(devinfo));
742 /* fill devinfo structure */
743 devinfo.version_code = COMEDI_VERSION_CODE;
744 devinfo.n_subdevs = dev->n_subdevices;
745 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
746 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
748 s = comedi_read_subdevice(dev, minor);
750 devinfo.read_subdevice = s->index;
752 devinfo.read_subdevice = -1;
754 s = comedi_write_subdevice(dev, minor);
756 devinfo.write_subdevice = s->index;
758 devinfo.write_subdevice = -1;
760 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
771 pointer to array of subdevice info structures
777 array of subdevice info structures at arg
780 static int do_subdinfo_ioctl(struct comedi_device *dev,
781 struct comedi_subdinfo __user *arg, void *file)
784 struct comedi_subdinfo *tmp, *us;
785 struct comedi_subdevice *s;
787 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
791 /* fill subdinfo structs */
792 for (i = 0; i < dev->n_subdevices; i++) {
793 s = &dev->subdevices[i];
797 us->n_chan = s->n_chan;
798 us->subd_flags = s->subdev_flags;
799 if (comedi_is_subdevice_running(s))
800 us->subd_flags |= SDF_RUNNING;
801 #define TIMER_nanosec 5 /* backwards compatibility */
802 us->timer_type = TIMER_nanosec;
803 us->len_chanlist = s->len_chanlist;
804 us->maxdata = s->maxdata;
805 if (s->range_table) {
807 (i << 24) | (0 << 16) | (s->range_table->length);
809 us->range_type = 0; /* XXX */
811 us->flags = s->flags;
814 us->subd_flags |= SDF_BUSY;
816 us->subd_flags |= SDF_BUSY_OWNER;
818 us->subd_flags |= SDF_LOCKED;
820 us->subd_flags |= SDF_LOCK_OWNER;
821 if (!s->maxdata && s->maxdata_list)
822 us->subd_flags |= SDF_MAXDATA;
824 us->subd_flags |= SDF_FLAGS;
825 if (s->range_table_list)
826 us->subd_flags |= SDF_RANGETYPE;
828 us->subd_flags |= SDF_CMD;
830 if (s->insn_bits != &insn_inval)
831 us->insn_bits_support = COMEDI_SUPPORTED;
833 us->insn_bits_support = COMEDI_UNSUPPORTED;
835 us->settling_time_0 = s->settling_time_0;
838 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
842 return ret ? -EFAULT : 0;
850 pointer to chaninfo structure
853 chaninfo structure at arg
856 arrays at elements of chaninfo structure
859 static int do_chaninfo_ioctl(struct comedi_device *dev,
860 struct comedi_chaninfo __user *arg)
862 struct comedi_subdevice *s;
863 struct comedi_chaninfo it;
865 if (copy_from_user(&it, arg, sizeof(it)))
868 if (it.subdev >= dev->n_subdevices)
870 s = &dev->subdevices[it.subdev];
872 if (it.maxdata_list) {
873 if (s->maxdata || !s->maxdata_list)
875 if (copy_to_user(it.maxdata_list, s->maxdata_list,
876 s->n_chan * sizeof(unsigned int)))
883 if (copy_to_user(it.flaglist, s->flaglist,
884 s->n_chan * sizeof(unsigned int)))
891 if (!s->range_table_list)
893 for (i = 0; i < s->n_chan; i++) {
896 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
897 (s->range_table_list[i]->length);
898 if (put_user(x, it.rangelist + i))
902 if (copy_to_user(it.rangelist, s->range_type_list,
903 s->n_chan * sizeof(unsigned int)))
913 buffer information ioctl
916 pointer to bufinfo structure
922 modified bufinfo at arg
925 static int do_bufinfo_ioctl(struct comedi_device *dev,
926 struct comedi_bufinfo __user *arg, void *file)
928 struct comedi_bufinfo bi;
929 struct comedi_subdevice *s;
930 struct comedi_async *async;
932 if (copy_from_user(&bi, arg, sizeof(bi)))
935 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
938 s = &dev->subdevices[bi.subdevice];
940 if (s->lock && s->lock != file)
946 DPRINTK("subdevice does not have async capability\n");
947 bi.buf_write_ptr = 0;
949 bi.buf_write_count = 0;
950 bi.buf_read_count = 0;
952 bi.bytes_written = 0;
957 bi.bytes_written = 0;
958 goto copyback_position;
963 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
964 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
965 comedi_buf_read_free(async, bi.bytes_read);
967 if (comedi_is_subdevice_idle(s) &&
968 async->buf_write_count == async->buf_read_count) {
969 do_become_nonbusy(dev, s);
973 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
975 comedi_buf_write_alloc(async, bi.bytes_written);
976 comedi_buf_write_free(async, bi.bytes_written);
980 bi.buf_write_count = async->buf_write_count;
981 bi.buf_write_ptr = async->buf_write_ptr;
982 bi.buf_read_count = async->buf_read_count;
983 bi.buf_read_ptr = async->buf_read_ptr;
986 if (copy_to_user(arg, &bi, sizeof(bi)))
992 static int check_insn_config_length(struct comedi_insn *insn,
999 case INSN_CONFIG_DIO_OUTPUT:
1000 case INSN_CONFIG_DIO_INPUT:
1001 case INSN_CONFIG_DISARM:
1002 case INSN_CONFIG_RESET:
1006 case INSN_CONFIG_ARM:
1007 case INSN_CONFIG_DIO_QUERY:
1008 case INSN_CONFIG_BLOCK_SIZE:
1009 case INSN_CONFIG_FILTER:
1010 case INSN_CONFIG_SERIAL_CLOCK:
1011 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1012 case INSN_CONFIG_ALT_SOURCE:
1013 case INSN_CONFIG_SET_COUNTER_MODE:
1014 case INSN_CONFIG_8254_READ_STATUS:
1015 case INSN_CONFIG_SET_ROUTING:
1016 case INSN_CONFIG_GET_ROUTING:
1017 case INSN_CONFIG_GET_PWM_STATUS:
1018 case INSN_CONFIG_PWM_SET_PERIOD:
1019 case INSN_CONFIG_PWM_GET_PERIOD:
1023 case INSN_CONFIG_SET_GATE_SRC:
1024 case INSN_CONFIG_GET_GATE_SRC:
1025 case INSN_CONFIG_SET_CLOCK_SRC:
1026 case INSN_CONFIG_GET_CLOCK_SRC:
1027 case INSN_CONFIG_SET_OTHER_SRC:
1028 case INSN_CONFIG_GET_COUNTER_STATUS:
1029 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1030 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1031 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1035 case INSN_CONFIG_PWM_OUTPUT:
1036 case INSN_CONFIG_ANALOG_TRIG:
1040 case INSN_CONFIG_DIGITAL_TRIG:
1044 /* by default we allow the insn since we don't have checks for
1045 * all possible cases yet */
1047 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1049 pr_warn("comedi: Add a check to %s in %s.\n",
1050 __func__, __FILE__);
1051 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1057 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1058 unsigned int *data, void *file)
1060 struct comedi_subdevice *s;
1064 if (insn->insn & INSN_MASK_SPECIAL) {
1065 /* a non-subdevice instruction */
1067 switch (insn->insn) {
1077 do_gettimeofday(&tv);
1078 data[0] = tv.tv_sec;
1079 data[1] = tv.tv_usec;
1085 if (insn->n != 1 || data[0] >= 100000) {
1089 udelay(data[0] / 1000);
1097 if (insn->subdev >= dev->n_subdevices) {
1098 DPRINTK("%d not usable subdevice\n",
1103 s = &dev->subdevices[insn->subdev];
1105 DPRINTK("no async\n");
1109 if (!s->async->inttrig) {
1110 DPRINTK("no inttrig\n");
1114 ret = s->async->inttrig(dev, s, data[0]);
1119 DPRINTK("invalid insn\n");
1124 /* a subdevice instruction */
1125 unsigned int maxdata;
1127 if (insn->subdev >= dev->n_subdevices) {
1128 DPRINTK("subdevice %d out of range\n", insn->subdev);
1132 s = &dev->subdevices[insn->subdev];
1134 if (s->type == COMEDI_SUBD_UNUSED) {
1135 DPRINTK("%d not usable subdevice\n", insn->subdev);
1140 /* are we locked? (ioctl lock) */
1141 if (s->lock && s->lock != file) {
1142 DPRINTK("device locked\n");
1147 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1150 DPRINTK("bad chanspec\n");
1158 /* This looks arbitrary. It is. */
1159 s->busy = &parse_insn;
1160 switch (insn->insn) {
1162 ret = s->insn_read(dev, s, insn, data);
1165 maxdata = s->maxdata_list
1166 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1168 for (i = 0; i < insn->n; ++i) {
1169 if (data[i] > maxdata) {
1171 DPRINTK("bad data value(s)\n");
1176 ret = s->insn_write(dev, s, insn, data);
1182 /* Most drivers ignore the base channel in
1183 * insn->chanspec. Fix this here if
1184 * the subdevice has <= 32 channels. */
1186 unsigned int orig_mask;
1188 orig_mask = data[0];
1189 if (s->n_chan <= 32) {
1190 shift = CR_CHAN(insn->chanspec);
1198 ret = s->insn_bits(dev, s, insn, data);
1199 data[0] = orig_mask;
1205 ret = check_insn_config_length(insn, data);
1208 ret = s->insn_config(dev, s, insn, data);
1224 * synchronous instructions
1227 * pointer to sync cmd structure
1230 * sync cmd struct at arg
1237 /* arbitrary limits */
1238 #define MAX_SAMPLES 256
1239 static int do_insnlist_ioctl(struct comedi_device *dev,
1240 struct comedi_insnlist __user *arg, void *file)
1242 struct comedi_insnlist insnlist;
1243 struct comedi_insn *insns = NULL;
1244 unsigned int *data = NULL;
1248 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1251 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1253 DPRINTK("kmalloc failed\n");
1258 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1260 DPRINTK("kmalloc failed\n");
1265 if (copy_from_user(insns, insnlist.insns,
1266 sizeof(*insns) * insnlist.n_insns)) {
1267 DPRINTK("copy_from_user failed\n");
1272 for (i = 0; i < insnlist.n_insns; i++) {
1273 if (insns[i].n > MAX_SAMPLES) {
1274 DPRINTK("number of samples too large\n");
1278 if (insns[i].insn & INSN_MASK_WRITE) {
1279 if (copy_from_user(data, insns[i].data,
1280 insns[i].n * sizeof(unsigned int))) {
1281 DPRINTK("copy_from_user failed\n");
1286 ret = parse_insn(dev, insns + i, data, file);
1289 if (insns[i].insn & INSN_MASK_READ) {
1290 if (copy_to_user(insns[i].data, data,
1291 insns[i].n * sizeof(unsigned int))) {
1292 DPRINTK("copy_to_user failed\n");
1312 * synchronous instructions
1318 * struct comedi_insn struct at arg
1324 static int do_insn_ioctl(struct comedi_device *dev,
1325 struct comedi_insn __user *arg, void *file)
1327 struct comedi_insn insn;
1328 unsigned int *data = NULL;
1331 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1337 if (copy_from_user(&insn, arg, sizeof(insn))) {
1342 /* This is where the behavior of insn and insnlist deviate. */
1343 if (insn.n > MAX_SAMPLES)
1344 insn.n = MAX_SAMPLES;
1345 if (insn.insn & INSN_MASK_WRITE) {
1346 if (copy_from_user(data,
1348 insn.n * sizeof(unsigned int))) {
1353 ret = parse_insn(dev, &insn, data, file);
1356 if (insn.insn & INSN_MASK_READ) {
1357 if (copy_to_user(insn.data,
1359 insn.n * sizeof(unsigned int))) {
1372 static int do_cmd_ioctl(struct comedi_device *dev,
1373 struct comedi_cmd __user *arg, void *file)
1375 struct comedi_cmd cmd;
1376 struct comedi_subdevice *s;
1377 struct comedi_async *async;
1379 unsigned int __user *user_chanlist;
1381 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1382 DPRINTK("bad cmd address\n");
1385 /* save user's chanlist pointer so it can be restored later */
1386 user_chanlist = (unsigned int __user *)cmd.chanlist;
1388 if (cmd.subdev >= dev->n_subdevices) {
1389 DPRINTK("%d no such subdevice\n", cmd.subdev);
1393 s = &dev->subdevices[cmd.subdev];
1396 if (s->type == COMEDI_SUBD_UNUSED) {
1397 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1401 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1402 DPRINTK("subdevice %i does not support commands\n",
1407 /* are we locked? (ioctl lock) */
1408 if (s->lock && s->lock != file) {
1409 DPRINTK("subdevice locked\n");
1415 DPRINTK("subdevice busy\n");
1420 /* make sure channel/gain list isn't too long */
1421 if (cmd.chanlist_len > s->len_chanlist) {
1422 DPRINTK("channel/gain list too long %u > %d\n",
1423 cmd.chanlist_len, s->len_chanlist);
1428 /* make sure channel/gain list isn't too short */
1429 if (cmd.chanlist_len < 1) {
1430 DPRINTK("channel/gain list too short %u < 1\n",
1437 async->cmd.data = NULL;
1438 /* load channel/gain list */
1439 async->cmd.chanlist =
1440 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1441 if (!async->cmd.chanlist) {
1442 DPRINTK("allocation failed\n");
1447 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1448 async->cmd.chanlist_len * sizeof(int))) {
1449 DPRINTK("fault reading chanlist\n");
1454 /* make sure each element in channel/gain list is valid */
1455 ret = comedi_check_chanlist(s,
1456 async->cmd.chanlist_len,
1457 async->cmd.chanlist);
1459 DPRINTK("bad chanlist\n");
1463 ret = s->do_cmdtest(dev, s, &async->cmd);
1465 if (async->cmd.flags & TRIG_BOGUS || ret) {
1466 DPRINTK("test returned %d\n", ret);
1468 /* restore chanlist pointer before copying back */
1469 cmd.chanlist = (unsigned int __force *)user_chanlist;
1471 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1472 DPRINTK("fault writing cmd\n");
1480 if (!async->prealloc_bufsz) {
1482 DPRINTK("no buffer (?)\n");
1486 comedi_buf_reset(async);
1489 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1491 if (async->cmd.flags & TRIG_WAKE_EOS)
1492 async->cb_mask |= COMEDI_CB_EOS;
1494 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1496 ret = s->do_cmd(dev, s);
1501 do_become_nonbusy(dev, s);
1508 command testing ioctl
1511 pointer to cmd structure
1514 cmd structure at arg
1518 modified cmd structure at arg
1521 static int do_cmdtest_ioctl(struct comedi_device *dev,
1522 struct comedi_cmd __user *arg, void *file)
1524 struct comedi_cmd cmd;
1525 struct comedi_subdevice *s;
1527 unsigned int *chanlist = NULL;
1528 unsigned int __user *user_chanlist;
1530 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1531 DPRINTK("bad cmd address\n");
1534 /* save user's chanlist pointer so it can be restored later */
1535 user_chanlist = (unsigned int __user *)cmd.chanlist;
1537 if (cmd.subdev >= dev->n_subdevices) {
1538 DPRINTK("%d no such subdevice\n", cmd.subdev);
1542 s = &dev->subdevices[cmd.subdev];
1543 if (s->type == COMEDI_SUBD_UNUSED) {
1544 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1548 if (!s->do_cmd || !s->do_cmdtest) {
1549 DPRINTK("subdevice %i does not support commands\n",
1554 /* make sure channel/gain list isn't too long */
1555 if (cmd.chanlist_len > s->len_chanlist) {
1556 DPRINTK("channel/gain list too long %d > %d\n",
1557 cmd.chanlist_len, s->len_chanlist);
1562 /* load channel/gain list */
1565 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1567 DPRINTK("allocation failed\n");
1572 if (copy_from_user(chanlist, user_chanlist,
1573 cmd.chanlist_len * sizeof(int))) {
1574 DPRINTK("fault reading chanlist\n");
1579 /* make sure each element in channel/gain list is valid */
1580 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1582 DPRINTK("bad chanlist\n");
1586 cmd.chanlist = chanlist;
1589 ret = s->do_cmdtest(dev, s, &cmd);
1591 /* restore chanlist pointer before copying back */
1592 cmd.chanlist = (unsigned int __force *)user_chanlist;
1594 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1595 DPRINTK("bad cmd address\n");
1620 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1624 unsigned long flags;
1625 struct comedi_subdevice *s;
1627 if (arg >= dev->n_subdevices)
1629 s = &dev->subdevices[arg];
1631 spin_lock_irqsave(&s->spin_lock, flags);
1632 if (s->busy || s->lock)
1636 spin_unlock_irqrestore(&s->spin_lock, flags);
1643 ret = s->lock_f(dev, s);
1662 This function isn't protected by the semaphore, since
1663 we already own the lock.
1665 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1668 struct comedi_subdevice *s;
1670 if (arg >= dev->n_subdevices)
1672 s = &dev->subdevices[arg];
1677 if (s->lock && s->lock != file)
1680 if (s->lock == file) {
1694 cancel acquisition ioctl
1706 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1709 struct comedi_subdevice *s;
1711 if (arg >= dev->n_subdevices)
1713 s = &dev->subdevices[arg];
1714 if (s->async == NULL)
1717 if (s->lock && s->lock != file)
1723 if (s->busy != file)
1726 return do_cancel(dev, s);
1731 instructs driver to synchronize buffers
1743 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1746 struct comedi_subdevice *s;
1748 if (arg >= dev->n_subdevices)
1750 s = &dev->subdevices[arg];
1752 if (s->lock && s->lock != file)
1758 if (s->busy != file)
1762 return s->poll(dev, s);
1767 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1770 const unsigned minor = iminor(file_inode(file));
1771 struct comedi_device *dev = comedi_dev_from_minor(minor);
1777 mutex_lock(&dev->mutex);
1779 /* Device config is special, because it must work on
1780 * an unconfigured device. */
1781 if (cmd == COMEDI_DEVCONFIG) {
1782 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1783 /* Device config not appropriate on non-board minors. */
1787 rc = do_devconfig_ioctl(dev,
1788 (struct comedi_devconfig __user *)arg);
1791 dev->minor >= comedi_num_legacy_minors) {
1792 /* Successfully unconfigured a dynamically
1793 * allocated device. Try and remove it. */
1794 struct comedi_file_info *info;
1795 info = comedi_clear_board_minor(dev->minor);
1797 mutex_unlock(&dev->mutex);
1798 comedi_free_board_file_info(info);
1806 if (!dev->attached) {
1807 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1813 case COMEDI_BUFCONFIG:
1814 rc = do_bufconfig_ioctl(dev,
1815 (struct comedi_bufconfig __user *)arg);
1817 case COMEDI_DEVINFO:
1818 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1821 case COMEDI_SUBDINFO:
1822 rc = do_subdinfo_ioctl(dev,
1823 (struct comedi_subdinfo __user *)arg,
1826 case COMEDI_CHANINFO:
1827 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1829 case COMEDI_RANGEINFO:
1830 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1832 case COMEDI_BUFINFO:
1833 rc = do_bufinfo_ioctl(dev,
1834 (struct comedi_bufinfo __user *)arg,
1838 rc = do_lock_ioctl(dev, arg, file);
1841 rc = do_unlock_ioctl(dev, arg, file);
1844 rc = do_cancel_ioctl(dev, arg, file);
1847 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1849 case COMEDI_CMDTEST:
1850 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1853 case COMEDI_INSNLIST:
1854 rc = do_insnlist_ioctl(dev,
1855 (struct comedi_insnlist __user *)arg,
1859 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1863 rc = do_poll_ioctl(dev, arg, file);
1871 mutex_unlock(&dev->mutex);
1875 static void comedi_vm_open(struct vm_area_struct *area)
1877 struct comedi_async *async;
1878 struct comedi_device *dev;
1880 async = area->vm_private_data;
1881 dev = async->subdevice->device;
1883 mutex_lock(&dev->mutex);
1884 async->mmap_count++;
1885 mutex_unlock(&dev->mutex);
1888 static void comedi_vm_close(struct vm_area_struct *area)
1890 struct comedi_async *async;
1891 struct comedi_device *dev;
1893 async = area->vm_private_data;
1894 dev = async->subdevice->device;
1896 mutex_lock(&dev->mutex);
1897 async->mmap_count--;
1898 mutex_unlock(&dev->mutex);
1901 static struct vm_operations_struct comedi_vm_ops = {
1902 .open = comedi_vm_open,
1903 .close = comedi_vm_close,
1906 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1908 const unsigned minor = iminor(file_inode(file));
1909 struct comedi_device *dev = comedi_dev_from_minor(minor);
1910 struct comedi_subdevice *s;
1911 struct comedi_async *async;
1912 unsigned long start = vma->vm_start;
1921 mutex_lock(&dev->mutex);
1923 if (!dev->attached) {
1924 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1929 if (vma->vm_flags & VM_WRITE)
1930 s = comedi_write_subdevice(dev, minor);
1932 s = comedi_read_subdevice(dev, minor);
1944 if (vma->vm_pgoff != 0) {
1945 DPRINTK("comedi: mmap() offset must be 0.\n");
1950 size = vma->vm_end - vma->vm_start;
1951 if (size > async->prealloc_bufsz) {
1955 if (size & (~PAGE_MASK)) {
1960 n_pages = size >> PAGE_SHIFT;
1961 for (i = 0; i < n_pages; ++i) {
1962 struct comedi_buf_page *buf = &async->buf_page_list[i];
1964 if (remap_pfn_range(vma, start,
1965 page_to_pfn(virt_to_page(buf->virt_addr)),
1966 PAGE_SIZE, PAGE_SHARED)) {
1973 vma->vm_ops = &comedi_vm_ops;
1974 vma->vm_private_data = async;
1976 async->mmap_count++;
1980 mutex_unlock(&dev->mutex);
1984 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1986 unsigned int mask = 0;
1987 const unsigned minor = iminor(file_inode(file));
1988 struct comedi_device *dev = comedi_dev_from_minor(minor);
1989 struct comedi_subdevice *s;
1994 mutex_lock(&dev->mutex);
1996 if (!dev->attached) {
1997 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2001 s = comedi_read_subdevice(dev, minor);
2002 if (s && s->async) {
2003 poll_wait(file, &s->async->wait_head, wait);
2004 if (!s->busy || !comedi_is_subdevice_running(s) ||
2005 comedi_buf_read_n_available(s->async) > 0)
2006 mask |= POLLIN | POLLRDNORM;
2009 s = comedi_write_subdevice(dev, minor);
2010 if (s && s->async) {
2011 unsigned int bps = bytes_per_sample(s->async->subdevice);
2013 poll_wait(file, &s->async->wait_head, wait);
2014 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2015 if (!s->busy || !comedi_is_subdevice_running(s) ||
2016 comedi_buf_write_n_allocated(s->async) >= bps)
2017 mask |= POLLOUT | POLLWRNORM;
2021 mutex_unlock(&dev->mutex);
2025 static ssize_t comedi_write(struct file *file, const char __user *buf,
2026 size_t nbytes, loff_t *offset)
2028 struct comedi_subdevice *s;
2029 struct comedi_async *async;
2030 int n, m, count = 0, retval = 0;
2031 DECLARE_WAITQUEUE(wait, current);
2032 const unsigned minor = iminor(file_inode(file));
2033 struct comedi_device *dev = comedi_dev_from_minor(minor);
2038 if (!dev->attached) {
2039 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2043 s = comedi_write_subdevice(dev, minor);
2044 if (!s || !s->async)
2049 if (!s->busy || !nbytes)
2051 if (s->busy != file)
2054 add_wait_queue(&async->wait_head, &wait);
2055 while (nbytes > 0 && !retval) {
2056 set_current_state(TASK_INTERRUPTIBLE);
2058 if (!comedi_is_subdevice_running(s)) {
2060 if (comedi_is_subdevice_in_error(s))
2064 do_become_nonbusy(dev, s);
2072 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2073 m = async->prealloc_bufsz - async->buf_write_ptr;
2074 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2075 if (m > comedi_buf_write_n_allocated(async))
2076 m = comedi_buf_write_n_allocated(async);
2081 if (file->f_flags & O_NONBLOCK) {
2086 if (signal_pending(current)) {
2087 retval = -ERESTARTSYS;
2092 if (s->busy != file) {
2099 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2105 comedi_buf_write_free(async, n);
2111 break; /* makes device work like a pipe */
2113 set_current_state(TASK_RUNNING);
2114 remove_wait_queue(&async->wait_head, &wait);
2116 return count ? count : retval;
2119 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2122 struct comedi_subdevice *s;
2123 struct comedi_async *async;
2124 int n, m, count = 0, retval = 0;
2125 DECLARE_WAITQUEUE(wait, current);
2126 const unsigned minor = iminor(file_inode(file));
2127 struct comedi_device *dev = comedi_dev_from_minor(minor);
2132 if (!dev->attached) {
2133 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2137 s = comedi_read_subdevice(dev, minor);
2138 if (!s || !s->async)
2142 if (!s->busy || !nbytes)
2144 if (s->busy != file)
2147 add_wait_queue(&async->wait_head, &wait);
2148 while (nbytes > 0 && !retval) {
2149 set_current_state(TASK_INTERRUPTIBLE);
2153 m = comedi_buf_read_n_available(async);
2154 /* printk("%d available\n",m); */
2155 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2156 m = async->prealloc_bufsz - async->buf_read_ptr;
2157 /* printk("%d contiguous\n",m); */
2162 if (!comedi_is_subdevice_running(s)) {
2163 do_become_nonbusy(dev, s);
2164 if (comedi_is_subdevice_in_error(s))
2170 if (file->f_flags & O_NONBLOCK) {
2175 if (signal_pending(current)) {
2176 retval = -ERESTARTSYS;
2183 if (s->busy != file) {
2189 m = copy_to_user(buf, async->prealloc_buf +
2190 async->buf_read_ptr, n);
2196 comedi_buf_read_alloc(async, n);
2197 comedi_buf_read_free(async, n);
2203 break; /* makes device work like a pipe */
2205 if (comedi_is_subdevice_idle(s) &&
2206 async->buf_read_count - async->buf_write_count == 0) {
2207 do_become_nonbusy(dev, s);
2209 set_current_state(TASK_RUNNING);
2210 remove_wait_queue(&async->wait_head, &wait);
2212 return count ? count : retval;
2215 static int comedi_open(struct inode *inode, struct file *file)
2217 const unsigned minor = iminor(inode);
2218 struct comedi_device *dev = comedi_dev_from_minor(minor);
2221 DPRINTK("invalid minor number\n");
2225 /* This is slightly hacky, but we want module autoloading
2227 * case: user opens device, attached -> ok
2228 * case: user opens device, unattached, !in_request_module -> autoload
2229 * case: user opens device, unattached, in_request_module -> fail
2230 * case: root opens device, attached -> ok
2231 * case: root opens device, unattached, in_request_module -> ok
2232 * (typically called from modprobe)
2233 * case: root opens device, unattached, !in_request_module -> autoload
2235 * The last could be changed to "-> ok", which would deny root
2238 mutex_lock(&dev->mutex);
2241 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2242 DPRINTK("in request module\n");
2243 mutex_unlock(&dev->mutex);
2246 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2249 dev->in_request_module = true;
2252 mutex_unlock(&dev->mutex);
2253 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2254 mutex_lock(&dev->mutex);
2257 dev->in_request_module = false;
2259 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2260 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2261 mutex_unlock(&dev->mutex);
2265 __module_get(THIS_MODULE);
2267 if (dev->attached) {
2268 if (!try_module_get(dev->driver->module)) {
2269 module_put(THIS_MODULE);
2270 mutex_unlock(&dev->mutex);
2275 if (dev->attached && dev->use_count == 0 && dev->open) {
2276 int rc = dev->open(dev);
2278 module_put(dev->driver->module);
2279 module_put(THIS_MODULE);
2280 mutex_unlock(&dev->mutex);
2287 mutex_unlock(&dev->mutex);
2292 static int comedi_fasync(int fd, struct file *file, int on)
2294 const unsigned minor = iminor(file_inode(file));
2295 struct comedi_device *dev = comedi_dev_from_minor(minor);
2300 return fasync_helper(fd, file, on, &dev->async_queue);
2303 static int comedi_close(struct inode *inode, struct file *file)
2305 const unsigned minor = iminor(inode);
2306 struct comedi_device *dev = comedi_dev_from_minor(minor);
2307 struct comedi_subdevice *s = NULL;
2313 mutex_lock(&dev->mutex);
2315 if (dev->subdevices) {
2316 for (i = 0; i < dev->n_subdevices; i++) {
2317 s = &dev->subdevices[i];
2319 if (s->busy == file)
2321 if (s->lock == file)
2325 if (dev->attached && dev->use_count == 1 && dev->close)
2328 module_put(THIS_MODULE);
2330 module_put(dev->driver->module);
2334 mutex_unlock(&dev->mutex);
2336 if (file->f_flags & FASYNC)
2337 comedi_fasync(-1, file, 0);
2342 static const struct file_operations comedi_fops = {
2343 .owner = THIS_MODULE,
2344 .unlocked_ioctl = comedi_unlocked_ioctl,
2345 .compat_ioctl = comedi_compat_ioctl,
2346 .open = comedi_open,
2347 .release = comedi_close,
2348 .read = comedi_read,
2349 .write = comedi_write,
2350 .mmap = comedi_mmap,
2351 .poll = comedi_poll,
2352 .fasync = comedi_fasync,
2353 .llseek = noop_llseek,
2356 void comedi_error(const struct comedi_device *dev, const char *s)
2358 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2360 EXPORT_SYMBOL(comedi_error);
2362 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2364 struct comedi_async *async = s->async;
2365 unsigned runflags = 0;
2366 unsigned runflags_mask = 0;
2368 /* DPRINTK("comedi_event 0x%x\n",mask); */
2370 if (!comedi_is_subdevice_running(s))
2374 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2375 COMEDI_CB_OVERFLOW)) {
2376 runflags_mask |= SRF_RUNNING;
2378 /* remember if an error event has occurred, so an error
2379 * can be returned the next time the user does a read() */
2380 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2381 runflags_mask |= SRF_ERROR;
2382 runflags |= SRF_ERROR;
2384 if (runflags_mask) {
2385 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2386 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2389 if (async->cb_mask & s->async->events) {
2390 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2391 wake_up_interruptible(&async->wait_head);
2392 if (s->subdev_flags & SDF_CMD_READ)
2393 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2394 if (s->subdev_flags & SDF_CMD_WRITE)
2395 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2398 async->cb_func(s->async->events, async->cb_arg);
2401 s->async->events = 0;
2403 EXPORT_SYMBOL(comedi_event);
2405 /* Note: the ->mutex is pre-locked on successful return */
2406 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2408 struct comedi_file_info *info;
2409 struct comedi_device *dev;
2410 struct device *csdev;
2413 info = kzalloc(sizeof(*info), GFP_KERNEL);
2415 return ERR_PTR(-ENOMEM);
2416 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2419 return ERR_PTR(-ENOMEM);
2422 comedi_device_init(dev);
2423 comedi_set_hw_dev(dev, hardware_device);
2424 mutex_lock(&dev->mutex);
2425 mutex_lock(&comedi_board_minor_table_lock);
2426 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2427 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2428 if (comedi_board_minor_table[i] == NULL) {
2429 comedi_board_minor_table[i] = info;
2433 mutex_unlock(&comedi_board_minor_table_lock);
2434 if (i == COMEDI_NUM_BOARD_MINORS) {
2435 mutex_unlock(&dev->mutex);
2436 comedi_device_cleanup(dev);
2439 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2440 return ERR_PTR(-EBUSY);
2443 csdev = device_create(comedi_class, hardware_device,
2444 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2446 dev->class_dev = csdev;
2448 /* Note: dev->mutex needs to be unlocked by the caller. */
2452 static void comedi_free_board_minor(unsigned minor)
2454 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2455 comedi_free_board_file_info(comedi_clear_board_minor(minor));
2458 void comedi_release_hardware_device(struct device *hardware_device)
2461 struct comedi_file_info *info;
2463 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2465 mutex_lock(&comedi_board_minor_table_lock);
2466 info = comedi_board_minor_table[minor];
2467 if (info && info->device->hw_dev == hardware_device) {
2468 comedi_board_minor_table[minor] = NULL;
2469 mutex_unlock(&comedi_board_minor_table_lock);
2470 comedi_free_board_file_info(info);
2473 mutex_unlock(&comedi_board_minor_table_lock);
2477 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2479 struct comedi_device *dev = s->device;
2480 struct comedi_file_info *info;
2481 struct device *csdev;
2484 info = kzalloc(sizeof(*info), GFP_KERNEL);
2488 if (s->subdev_flags & SDF_CMD_READ)
2489 info->read_subdevice = s;
2490 if (s->subdev_flags & SDF_CMD_WRITE)
2491 info->write_subdevice = s;
2492 mutex_lock(&comedi_subdevice_minor_table_lock);
2493 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2494 if (comedi_subdevice_minor_table[i] == NULL) {
2495 comedi_subdevice_minor_table[i] = info;
2499 mutex_unlock(&comedi_subdevice_minor_table_lock);
2500 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2502 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2505 i += COMEDI_NUM_BOARD_MINORS;
2507 csdev = device_create(comedi_class, dev->class_dev,
2508 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2509 dev->minor, s->index);
2511 s->class_dev = csdev;
2516 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2518 struct comedi_file_info *info;
2526 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2527 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2529 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2530 mutex_lock(&comedi_subdevice_minor_table_lock);
2531 info = comedi_subdevice_minor_table[i];
2532 comedi_subdevice_minor_table[i] = NULL;
2533 mutex_unlock(&comedi_subdevice_minor_table_lock);
2535 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2536 s->class_dev = NULL;
2541 static void comedi_cleanup_board_minors(void)
2545 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2546 comedi_free_board_minor(i);
2549 static int __init comedi_init(void)
2554 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2556 if (comedi_num_legacy_minors < 0 ||
2557 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2558 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2559 COMEDI_NUM_BOARD_MINORS);
2563 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2564 COMEDI_NUM_MINORS, "comedi");
2567 cdev_init(&comedi_cdev, &comedi_fops);
2568 comedi_cdev.owner = THIS_MODULE;
2569 kobject_set_name(&comedi_cdev.kobj, "comedi");
2570 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2571 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2575 comedi_class = class_create(THIS_MODULE, "comedi");
2576 if (IS_ERR(comedi_class)) {
2577 pr_err("comedi: failed to create class\n");
2578 cdev_del(&comedi_cdev);
2579 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2581 return PTR_ERR(comedi_class);
2584 comedi_class->dev_attrs = comedi_dev_attrs;
2586 /* XXX requires /proc interface */
2589 /* create devices files for legacy/manual use */
2590 for (i = 0; i < comedi_num_legacy_minors; i++) {
2591 struct comedi_device *dev;
2592 dev = comedi_alloc_board_minor(NULL);
2594 comedi_cleanup_board_minors();
2595 cdev_del(&comedi_cdev);
2596 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2598 return PTR_ERR(dev);
2600 /* comedi_alloc_board_minor() locked the mutex */
2601 mutex_unlock(&dev->mutex);
2607 module_init(comedi_init);
2609 static void __exit comedi_cleanup(void)
2613 comedi_cleanup_board_minors();
2614 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2615 BUG_ON(comedi_board_minor_table[i]);
2616 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2617 BUG_ON(comedi_subdevice_minor_table[i]);
2619 class_destroy(comedi_class);
2620 cdev_del(&comedi_cdev);
2621 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2623 comedi_proc_cleanup();
2625 module_exit(comedi_cleanup);
2627 MODULE_AUTHOR("http://www.comedi.org");
2628 MODULE_DESCRIPTION("Comedi core module");
2629 MODULE_LICENSE("GPL");