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 #define __NO_VERSION__
27 #include "comedi_fops.h"
28 #include "comedi_compat32.h"
30 #include <linux/module.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/fcntl.h>
35 #include <linux/delay.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/kmod.h>
40 #include <linux/poll.h>
41 #include <linux/init.h>
42 #include <linux/device.h>
43 #include <linux/vmalloc.h>
45 #include "comedidev.h"
46 #include <linux/cdev.h>
47 #include <linux/stat.h>
50 #include <linux/uaccess.h>
54 MODULE_AUTHOR("http://www.comedi.org");
55 MODULE_DESCRIPTION("Comedi core module");
56 MODULE_LICENSE("GPL");
58 #ifdef CONFIG_COMEDI_DEBUG
60 EXPORT_SYMBOL(comedi_debug);
61 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
62 MODULE_PARM_DESC(comedi_debug,
63 "enable comedi core and driver debugging if non-zero (default 0)"
67 bool comedi_autoconfig = 1;
68 module_param(comedi_autoconfig, bool, S_IRUGO);
69 MODULE_PARM_DESC(comedi_autoconfig,
70 "enable drivers to auto-configure comedi devices (default 1)");
72 static int comedi_num_legacy_minors;
73 module_param(comedi_num_legacy_minors, int, S_IRUGO);
74 MODULE_PARM_DESC(comedi_num_legacy_minors,
75 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
78 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
79 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
80 MODULE_PARM_DESC(comedi_default_buf_size_kb,
81 "default asynchronous buffer size in KiB (default "
82 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
84 unsigned int comedi_default_buf_maxsize_kb
85 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
86 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
87 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
88 "default maximum size of asynchronous buffer in KiB (default "
89 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
91 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
92 static struct comedi_device_file_info
93 *comedi_file_info_table[COMEDI_NUM_MINORS];
95 static int do_devconfig_ioctl(struct comedi_device *dev,
96 struct comedi_devconfig __user *arg);
97 static int do_bufconfig_ioctl(struct comedi_device *dev,
98 struct comedi_bufconfig __user *arg);
99 static int do_devinfo_ioctl(struct comedi_device *dev,
100 struct comedi_devinfo __user *arg,
102 static int do_subdinfo_ioctl(struct comedi_device *dev,
103 struct comedi_subdinfo __user *arg, void *file);
104 static int do_chaninfo_ioctl(struct comedi_device *dev,
105 struct comedi_chaninfo __user *arg);
106 static int do_bufinfo_ioctl(struct comedi_device *dev,
107 struct comedi_bufinfo __user *arg, void *file);
108 static int do_cmd_ioctl(struct comedi_device *dev,
109 struct comedi_cmd __user *arg, void *file);
110 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
112 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
114 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
116 static int do_cmdtest_ioctl(struct comedi_device *dev,
117 struct comedi_cmd __user *arg, void *file);
118 static int do_insnlist_ioctl(struct comedi_device *dev,
119 struct comedi_insnlist __user *arg, void *file);
120 static int do_insn_ioctl(struct comedi_device *dev,
121 struct comedi_insn __user *arg, void *file);
122 static int do_poll_ioctl(struct comedi_device *dev, unsigned int subd,
125 static void do_become_nonbusy(struct comedi_device *dev,
126 struct comedi_subdevice *s);
127 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
129 static int comedi_fasync(int fd, struct file *file, int on);
131 static int is_device_busy(struct comedi_device *dev);
133 static int resize_async_buffer(struct comedi_device *dev,
134 struct comedi_subdevice *s,
135 struct comedi_async *async, unsigned new_size)
139 if (new_size > async->max_bufsize)
143 DPRINTK("subdevice is busy, cannot resize buffer\n");
146 if (async->mmap_count) {
147 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
151 if (!async->prealloc_buf)
154 /* make sure buffer is an integral number of pages
156 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
158 retval = comedi_buf_alloc(dev, s, new_size);
163 retval = s->buf_change(dev, s, new_size);
168 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
169 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
173 /* sysfs attribute files */
175 static const unsigned bytes_per_kibi = 1024;
177 static ssize_t show_max_read_buffer_kb(struct device *dev,
178 struct device_attribute *attr, char *buf)
181 struct comedi_device_file_info *info = dev_get_drvdata(dev);
182 unsigned max_buffer_size_kb = 0;
183 struct comedi_subdevice *const read_subdevice =
184 comedi_get_read_subdevice(info);
186 mutex_lock(&info->device->mutex);
187 if (read_subdevice &&
188 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
189 read_subdevice->async) {
190 max_buffer_size_kb = read_subdevice->async->max_bufsize /
193 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
194 mutex_unlock(&info->device->mutex);
199 static ssize_t store_max_read_buffer_kb(struct device *dev,
200 struct device_attribute *attr,
201 const char *buf, size_t count)
203 struct comedi_device_file_info *info = dev_get_drvdata(dev);
204 unsigned int new_max_size_kb;
205 unsigned int new_max_size;
207 struct comedi_subdevice *const read_subdevice =
208 comedi_get_read_subdevice(info);
210 ret = kstrtouint(buf, 10, &new_max_size_kb);
213 if (new_max_size_kb > (UINT_MAX / bytes_per_kibi))
215 new_max_size = new_max_size_kb * bytes_per_kibi;
217 mutex_lock(&info->device->mutex);
218 if (read_subdevice == NULL ||
219 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
220 read_subdevice->async == NULL) {
221 mutex_unlock(&info->device->mutex);
224 read_subdevice->async->max_bufsize = new_max_size;
225 mutex_unlock(&info->device->mutex);
230 static ssize_t show_read_buffer_kb(struct device *dev,
231 struct device_attribute *attr, char *buf)
234 struct comedi_device_file_info *info = dev_get_drvdata(dev);
235 unsigned buffer_size_kb = 0;
236 struct comedi_subdevice *const read_subdevice =
237 comedi_get_read_subdevice(info);
239 mutex_lock(&info->device->mutex);
240 if (read_subdevice &&
241 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
242 read_subdevice->async) {
243 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
246 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
247 mutex_unlock(&info->device->mutex);
252 static ssize_t store_read_buffer_kb(struct device *dev,
253 struct device_attribute *attr,
254 const char *buf, size_t count)
256 struct comedi_device_file_info *info = dev_get_drvdata(dev);
257 unsigned int new_size_kb;
258 unsigned int new_size;
261 struct comedi_subdevice *const read_subdevice =
262 comedi_get_read_subdevice(info);
264 ret = kstrtouint(buf, 10, &new_size_kb);
267 if (new_size_kb > (UINT_MAX / bytes_per_kibi))
269 new_size = new_size_kb * bytes_per_kibi;
271 mutex_lock(&info->device->mutex);
272 if (read_subdevice == NULL ||
273 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
274 read_subdevice->async == NULL) {
275 mutex_unlock(&info->device->mutex);
278 retval = resize_async_buffer(info->device, read_subdevice,
279 read_subdevice->async, new_size);
280 mutex_unlock(&info->device->mutex);
287 static ssize_t show_max_write_buffer_kb(struct device *dev,
288 struct device_attribute *attr,
292 struct comedi_device_file_info *info = dev_get_drvdata(dev);
293 unsigned max_buffer_size_kb = 0;
294 struct comedi_subdevice *const write_subdevice =
295 comedi_get_write_subdevice(info);
297 mutex_lock(&info->device->mutex);
298 if (write_subdevice &&
299 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
300 write_subdevice->async) {
301 max_buffer_size_kb = write_subdevice->async->max_bufsize /
304 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
305 mutex_unlock(&info->device->mutex);
310 static ssize_t store_max_write_buffer_kb(struct device *dev,
311 struct device_attribute *attr,
312 const char *buf, size_t count)
314 struct comedi_device_file_info *info = dev_get_drvdata(dev);
315 unsigned int new_max_size_kb;
316 unsigned int new_max_size;
318 struct comedi_subdevice *const write_subdevice =
319 comedi_get_write_subdevice(info);
321 ret = kstrtouint(buf, 10, &new_max_size_kb);
324 if (new_max_size_kb > (UINT_MAX / bytes_per_kibi))
326 new_max_size = new_max_size_kb * bytes_per_kibi;
328 mutex_lock(&info->device->mutex);
329 if (write_subdevice == NULL ||
330 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
331 write_subdevice->async == NULL) {
332 mutex_unlock(&info->device->mutex);
335 write_subdevice->async->max_bufsize = new_max_size;
336 mutex_unlock(&info->device->mutex);
341 static ssize_t show_write_buffer_kb(struct device *dev,
342 struct device_attribute *attr, char *buf)
345 struct comedi_device_file_info *info = dev_get_drvdata(dev);
346 unsigned buffer_size_kb = 0;
347 struct comedi_subdevice *const write_subdevice =
348 comedi_get_write_subdevice(info);
350 mutex_lock(&info->device->mutex);
351 if (write_subdevice &&
352 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
353 write_subdevice->async) {
354 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
357 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
358 mutex_unlock(&info->device->mutex);
363 static ssize_t store_write_buffer_kb(struct device *dev,
364 struct device_attribute *attr,
365 const char *buf, size_t count)
367 struct comedi_device_file_info *info = dev_get_drvdata(dev);
368 unsigned int new_size_kb;
369 unsigned int new_size;
372 struct comedi_subdevice *const write_subdevice =
373 comedi_get_write_subdevice(info);
375 ret = kstrtouint(buf, 10, &new_size_kb);
378 if (new_size_kb > (UINT_MAX / bytes_per_kibi))
380 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
382 mutex_lock(&info->device->mutex);
383 if (write_subdevice == NULL ||
384 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
385 write_subdevice->async == NULL) {
386 mutex_unlock(&info->device->mutex);
389 retval = resize_async_buffer(info->device, write_subdevice,
390 write_subdevice->async, new_size);
391 mutex_unlock(&info->device->mutex);
398 static struct device_attribute comedi_dev_attrs[] = {
399 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
400 show_max_read_buffer_kb, store_max_read_buffer_kb),
401 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
402 show_read_buffer_kb, store_read_buffer_kb),
403 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
404 show_max_write_buffer_kb, store_max_write_buffer_kb),
405 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
406 show_write_buffer_kb, store_write_buffer_kb),
410 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
413 const unsigned minor = iminor(file->f_dentry->d_inode);
414 struct comedi_device_file_info *dev_file_info =
415 comedi_get_device_file_info(minor);
416 struct comedi_device *dev;
419 if (dev_file_info == NULL || dev_file_info->device == NULL)
421 dev = dev_file_info->device;
423 mutex_lock(&dev->mutex);
425 /* Device config is special, because it must work on
426 * an unconfigured device. */
427 if (cmd == COMEDI_DEVCONFIG) {
428 rc = do_devconfig_ioctl(dev,
429 (struct comedi_devconfig __user *)arg);
433 if (!dev->attached) {
434 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
440 case COMEDI_BUFCONFIG:
441 rc = do_bufconfig_ioctl(dev,
442 (struct comedi_bufconfig __user *)arg);
445 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
448 case COMEDI_SUBDINFO:
449 rc = do_subdinfo_ioctl(dev,
450 (struct comedi_subdinfo __user *)arg,
453 case COMEDI_CHANINFO:
454 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
456 case COMEDI_RANGEINFO:
457 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
460 rc = do_bufinfo_ioctl(dev,
461 (struct comedi_bufinfo __user *)arg,
465 rc = do_lock_ioctl(dev, arg, file);
468 rc = do_unlock_ioctl(dev, arg, file);
471 rc = do_cancel_ioctl(dev, arg, file);
474 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
477 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
480 case COMEDI_INSNLIST:
481 rc = do_insnlist_ioctl(dev,
482 (struct comedi_insnlist __user *)arg,
486 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
490 rc = do_poll_ioctl(dev, arg, file);
498 mutex_unlock(&dev->mutex);
507 pointer to devconfig structure
510 devconfig structure at arg
515 static int do_devconfig_ioctl(struct comedi_device *dev,
516 struct comedi_devconfig __user *arg)
518 struct comedi_devconfig it;
520 unsigned char *aux_data = NULL;
523 if (!capable(CAP_SYS_ADMIN))
527 if (is_device_busy(dev))
530 struct module *driver_module = dev->driver->module;
531 comedi_device_detach(dev);
532 module_put(driver_module);
537 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
540 it.board_name[COMEDI_NAMELEN - 1] = 0;
542 if (comedi_aux_data(it.options, 0) &&
543 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
545 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
549 aux_data = vmalloc(aux_len);
553 if (copy_from_user(aux_data,
554 comedi_aux_data(it.options, 0), aux_len)) {
558 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
559 (unsigned long)aux_data;
560 if (sizeof(void *) > sizeof(int)) {
561 bit_shift = sizeof(int) * 8;
562 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
563 ((unsigned long)aux_data) >> bit_shift;
565 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
568 ret = comedi_device_attach(dev, &it);
570 if (!try_module_get(dev->driver->module)) {
571 comedi_device_detach(dev);
584 buffer configuration ioctl
587 pointer to bufconfig structure
593 modified bufconfig at arg
596 static int do_bufconfig_ioctl(struct comedi_device *dev,
597 struct comedi_bufconfig __user *arg)
599 struct comedi_bufconfig bc;
600 struct comedi_async *async;
601 struct comedi_subdevice *s;
604 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
607 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
610 s = dev->subdevices + bc.subdevice;
614 DPRINTK("subdevice does not have async capability\n");
620 if (bc.maximum_size) {
621 if (!capable(CAP_SYS_ADMIN))
624 async->max_bufsize = bc.maximum_size;
628 retval = resize_async_buffer(dev, s, async, bc.size);
633 bc.size = async->prealloc_bufsz;
634 bc.maximum_size = async->max_bufsize;
637 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
648 pointer to devinfo structure
657 static int do_devinfo_ioctl(struct comedi_device *dev,
658 struct comedi_devinfo __user *arg,
661 struct comedi_devinfo devinfo;
662 const unsigned minor = iminor(file->f_dentry->d_inode);
663 struct comedi_device_file_info *dev_file_info =
664 comedi_get_device_file_info(minor);
665 struct comedi_subdevice *read_subdev =
666 comedi_get_read_subdevice(dev_file_info);
667 struct comedi_subdevice *write_subdev =
668 comedi_get_write_subdevice(dev_file_info);
670 memset(&devinfo, 0, sizeof(devinfo));
672 /* fill devinfo structure */
673 devinfo.version_code = COMEDI_VERSION_CODE;
674 devinfo.n_subdevs = dev->n_subdevices;
675 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
676 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
679 devinfo.read_subdevice = read_subdev - dev->subdevices;
681 devinfo.read_subdevice = -1;
684 devinfo.write_subdevice = write_subdev - dev->subdevices;
686 devinfo.write_subdevice = -1;
688 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
699 pointer to array of subdevice info structures
705 array of subdevice info structures at arg
708 static int do_subdinfo_ioctl(struct comedi_device *dev,
709 struct comedi_subdinfo __user *arg, void *file)
712 struct comedi_subdinfo *tmp, *us;
713 struct comedi_subdevice *s;
716 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
721 /* fill subdinfo structs */
722 for (i = 0; i < dev->n_subdevices; i++) {
723 s = dev->subdevices + i;
727 us->n_chan = s->n_chan;
728 us->subd_flags = s->subdev_flags;
729 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
730 us->subd_flags |= SDF_RUNNING;
731 #define TIMER_nanosec 5 /* backwards compatibility */
732 us->timer_type = TIMER_nanosec;
733 us->len_chanlist = s->len_chanlist;
734 us->maxdata = s->maxdata;
735 if (s->range_table) {
737 (i << 24) | (0 << 16) | (s->range_table->length);
739 us->range_type = 0; /* XXX */
741 us->flags = s->flags;
744 us->subd_flags |= SDF_BUSY;
746 us->subd_flags |= SDF_BUSY_OWNER;
748 us->subd_flags |= SDF_LOCKED;
750 us->subd_flags |= SDF_LOCK_OWNER;
751 if (!s->maxdata && s->maxdata_list)
752 us->subd_flags |= SDF_MAXDATA;
754 us->subd_flags |= SDF_FLAGS;
755 if (s->range_table_list)
756 us->subd_flags |= SDF_RANGETYPE;
758 us->subd_flags |= SDF_CMD;
760 if (s->insn_bits != &insn_inval)
761 us->insn_bits_support = COMEDI_SUPPORTED;
763 us->insn_bits_support = COMEDI_UNSUPPORTED;
765 us->settling_time_0 = s->settling_time_0;
768 ret = copy_to_user(arg, tmp,
769 dev->n_subdevices * sizeof(struct comedi_subdinfo));
773 return ret ? -EFAULT : 0;
781 pointer to chaninfo structure
784 chaninfo structure at arg
787 arrays at elements of chaninfo structure
790 static int do_chaninfo_ioctl(struct comedi_device *dev,
791 struct comedi_chaninfo __user *arg)
793 struct comedi_subdevice *s;
794 struct comedi_chaninfo it;
796 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
799 if (it.subdev >= dev->n_subdevices)
801 s = dev->subdevices + it.subdev;
803 if (it.maxdata_list) {
804 if (s->maxdata || !s->maxdata_list)
806 if (copy_to_user(it.maxdata_list, s->maxdata_list,
807 s->n_chan * sizeof(unsigned int)))
814 if (copy_to_user(it.flaglist, s->flaglist,
815 s->n_chan * sizeof(unsigned int)))
822 if (!s->range_table_list)
824 for (i = 0; i < s->n_chan; i++) {
827 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
828 (s->range_table_list[i]->length);
829 if (put_user(x, it.rangelist + i))
833 if (copy_to_user(it.rangelist, s->range_type_list,
834 s->n_chan * sizeof(unsigned int)))
844 buffer information ioctl
847 pointer to bufinfo structure
853 modified bufinfo at arg
856 static int do_bufinfo_ioctl(struct comedi_device *dev,
857 struct comedi_bufinfo __user *arg, void *file)
859 struct comedi_bufinfo bi;
860 struct comedi_subdevice *s;
861 struct comedi_async *async;
863 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
866 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
869 s = dev->subdevices + bi.subdevice;
871 if (s->lock && s->lock != file)
877 DPRINTK("subdevice does not have async capability\n");
878 bi.buf_write_ptr = 0;
880 bi.buf_write_count = 0;
881 bi.buf_read_count = 0;
883 bi.bytes_written = 0;
888 bi.bytes_written = 0;
889 goto copyback_position;
894 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
895 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
896 comedi_buf_read_free(async, bi.bytes_read);
898 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
900 && async->buf_write_count == async->buf_read_count) {
901 do_become_nonbusy(dev, s);
905 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
907 comedi_buf_write_alloc(async, bi.bytes_written);
908 comedi_buf_write_free(async, bi.bytes_written);
912 bi.buf_write_count = async->buf_write_count;
913 bi.buf_write_ptr = async->buf_write_ptr;
914 bi.buf_read_count = async->buf_read_count;
915 bi.buf_read_ptr = async->buf_read_ptr;
918 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
924 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
925 unsigned int *data, void *file);
928 * synchronous instructions
931 * pointer to sync cmd structure
934 * sync cmd struct at arg
941 /* arbitrary limits */
942 #define MAX_SAMPLES 256
943 static int do_insnlist_ioctl(struct comedi_device *dev,
944 struct comedi_insnlist __user *arg, void *file)
946 struct comedi_insnlist insnlist;
947 struct comedi_insn *insns = NULL;
948 unsigned int *data = NULL;
952 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
955 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
957 DPRINTK("kmalloc failed\n");
963 kcalloc(insnlist.n_insns, sizeof(struct comedi_insn), GFP_KERNEL);
965 DPRINTK("kmalloc failed\n");
970 if (copy_from_user(insns, insnlist.insns,
971 sizeof(struct comedi_insn) * insnlist.n_insns)) {
972 DPRINTK("copy_from_user failed\n");
977 for (i = 0; i < insnlist.n_insns; i++) {
978 if (insns[i].n > MAX_SAMPLES) {
979 DPRINTK("number of samples too large\n");
983 if (insns[i].insn & INSN_MASK_WRITE) {
984 if (copy_from_user(data, insns[i].data,
985 insns[i].n * sizeof(unsigned int))) {
986 DPRINTK("copy_from_user failed\n");
991 ret = parse_insn(dev, insns + i, data, file);
994 if (insns[i].insn & INSN_MASK_READ) {
995 if (copy_to_user(insns[i].data, data,
996 insns[i].n * sizeof(unsigned int))) {
997 DPRINTK("copy_to_user failed\n");
1015 static int check_insn_config_length(struct comedi_insn *insn,
1022 case INSN_CONFIG_DIO_OUTPUT:
1023 case INSN_CONFIG_DIO_INPUT:
1024 case INSN_CONFIG_DISARM:
1025 case INSN_CONFIG_RESET:
1029 case INSN_CONFIG_ARM:
1030 case INSN_CONFIG_DIO_QUERY:
1031 case INSN_CONFIG_BLOCK_SIZE:
1032 case INSN_CONFIG_FILTER:
1033 case INSN_CONFIG_SERIAL_CLOCK:
1034 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1035 case INSN_CONFIG_ALT_SOURCE:
1036 case INSN_CONFIG_SET_COUNTER_MODE:
1037 case INSN_CONFIG_8254_READ_STATUS:
1038 case INSN_CONFIG_SET_ROUTING:
1039 case INSN_CONFIG_GET_ROUTING:
1040 case INSN_CONFIG_GET_PWM_STATUS:
1041 case INSN_CONFIG_PWM_SET_PERIOD:
1042 case INSN_CONFIG_PWM_GET_PERIOD:
1046 case INSN_CONFIG_SET_GATE_SRC:
1047 case INSN_CONFIG_GET_GATE_SRC:
1048 case INSN_CONFIG_SET_CLOCK_SRC:
1049 case INSN_CONFIG_GET_CLOCK_SRC:
1050 case INSN_CONFIG_SET_OTHER_SRC:
1051 case INSN_CONFIG_GET_COUNTER_STATUS:
1052 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1053 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1054 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1058 case INSN_CONFIG_PWM_OUTPUT:
1059 case INSN_CONFIG_ANALOG_TRIG:
1063 /* by default we allow the insn since we don't have checks for
1064 * all possible cases yet */
1067 "comedi: no check for data length of config insn id "
1068 "%i is implemented.\n"
1069 " Add a check to %s in %s.\n"
1070 " Assuming n=%i is correct.\n", data[0], __func__,
1078 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1079 unsigned int *data, void *file)
1081 struct comedi_subdevice *s;
1085 if (insn->insn & INSN_MASK_SPECIAL) {
1086 /* a non-subdevice instruction */
1088 switch (insn->insn) {
1098 do_gettimeofday(&tv);
1099 data[0] = tv.tv_sec;
1100 data[1] = tv.tv_usec;
1106 if (insn->n != 1 || data[0] >= 100000) {
1110 udelay(data[0] / 1000);
1118 if (insn->subdev >= dev->n_subdevices) {
1119 DPRINTK("%d not usable subdevice\n",
1124 s = dev->subdevices + insn->subdev;
1126 DPRINTK("no async\n");
1130 if (!s->async->inttrig) {
1131 DPRINTK("no inttrig\n");
1135 ret = s->async->inttrig(dev, s, insn->data[0]);
1140 DPRINTK("invalid insn\n");
1145 /* a subdevice instruction */
1146 unsigned int maxdata;
1148 if (insn->subdev >= dev->n_subdevices) {
1149 DPRINTK("subdevice %d out of range\n", insn->subdev);
1153 s = dev->subdevices + insn->subdev;
1155 if (s->type == COMEDI_SUBD_UNUSED) {
1156 DPRINTK("%d not usable subdevice\n", insn->subdev);
1161 /* are we locked? (ioctl lock) */
1162 if (s->lock && s->lock != file) {
1163 DPRINTK("device locked\n");
1168 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1171 DPRINTK("bad chanspec\n");
1179 /* This looks arbitrary. It is. */
1180 s->busy = &parse_insn;
1181 switch (insn->insn) {
1183 ret = s->insn_read(dev, s, insn, data);
1186 maxdata = s->maxdata_list
1187 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1189 for (i = 0; i < insn->n; ++i) {
1190 if (data[i] > maxdata) {
1192 DPRINTK("bad data value(s)\n");
1197 ret = s->insn_write(dev, s, insn, data);
1203 /* Most drivers ignore the base channel in
1204 * insn->chanspec. Fix this here if
1205 * the subdevice has <= 32 channels. */
1207 unsigned int orig_mask;
1209 orig_mask = data[0];
1210 if (s->n_chan <= 32) {
1211 shift = CR_CHAN(insn->chanspec);
1219 ret = s->insn_bits(dev, s, insn, data);
1220 data[0] = orig_mask;
1226 ret = check_insn_config_length(insn, data);
1229 ret = s->insn_config(dev, s, insn, data);
1245 * synchronous instructions
1251 * struct comedi_insn struct at arg
1257 static int do_insn_ioctl(struct comedi_device *dev,
1258 struct comedi_insn __user *arg, void *file)
1260 struct comedi_insn insn;
1261 unsigned int *data = NULL;
1264 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1270 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
1275 /* This is where the behavior of insn and insnlist deviate. */
1276 if (insn.n > MAX_SAMPLES)
1277 insn.n = MAX_SAMPLES;
1278 if (insn.insn & INSN_MASK_WRITE) {
1279 if (copy_from_user(data,
1281 insn.n * sizeof(unsigned int))) {
1286 ret = parse_insn(dev, &insn, data, file);
1289 if (insn.insn & INSN_MASK_READ) {
1290 if (copy_to_user(insn.data,
1292 insn.n * sizeof(unsigned int))) {
1305 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
1306 unsigned mask, unsigned bits)
1308 unsigned long flags;
1310 spin_lock_irqsave(&s->spin_lock, flags);
1311 s->runflags &= ~mask;
1312 s->runflags |= (bits & mask);
1313 spin_unlock_irqrestore(&s->spin_lock, flags);
1316 static int do_cmd_ioctl(struct comedi_device *dev,
1317 struct comedi_cmd __user *cmd, void *file)
1319 struct comedi_cmd user_cmd;
1320 struct comedi_subdevice *s;
1321 struct comedi_async *async;
1323 unsigned int __user *chanlist_saver = NULL;
1325 if (copy_from_user(&user_cmd, cmd, sizeof(struct comedi_cmd))) {
1326 DPRINTK("bad cmd address\n");
1329 /* save user's chanlist pointer so it can be restored later */
1330 chanlist_saver = user_cmd.chanlist;
1332 if (user_cmd.subdev >= dev->n_subdevices) {
1333 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1337 s = dev->subdevices + user_cmd.subdev;
1340 if (s->type == COMEDI_SUBD_UNUSED) {
1341 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1345 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1346 DPRINTK("subdevice %i does not support commands\n",
1351 /* are we locked? (ioctl lock) */
1352 if (s->lock && s->lock != file) {
1353 DPRINTK("subdevice locked\n");
1359 DPRINTK("subdevice busy\n");
1364 /* make sure channel/gain list isn't too long */
1365 if (user_cmd.chanlist_len > s->len_chanlist) {
1366 DPRINTK("channel/gain list too long %u > %d\n",
1367 user_cmd.chanlist_len, s->len_chanlist);
1372 /* make sure channel/gain list isn't too short */
1373 if (user_cmd.chanlist_len < 1) {
1374 DPRINTK("channel/gain list too short %u < 1\n",
1375 user_cmd.chanlist_len);
1380 kfree(async->cmd.chanlist);
1381 async->cmd = user_cmd;
1382 async->cmd.data = NULL;
1383 /* load channel/gain list */
1384 async->cmd.chanlist =
1385 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1386 if (!async->cmd.chanlist) {
1387 DPRINTK("allocation failed\n");
1392 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1393 async->cmd.chanlist_len * sizeof(int))) {
1394 DPRINTK("fault reading chanlist\n");
1399 /* make sure each element in channel/gain list is valid */
1400 ret = comedi_check_chanlist(s,
1401 async->cmd.chanlist_len,
1402 async->cmd.chanlist);
1404 DPRINTK("bad chanlist\n");
1408 ret = s->do_cmdtest(dev, s, &async->cmd);
1410 if (async->cmd.flags & TRIG_BOGUS || ret) {
1411 DPRINTK("test returned %d\n", ret);
1412 user_cmd = async->cmd;
1413 /* restore chanlist pointer before copying back */
1414 user_cmd.chanlist = chanlist_saver;
1415 user_cmd.data = NULL;
1416 if (copy_to_user(cmd, &user_cmd, sizeof(struct comedi_cmd))) {
1417 DPRINTK("fault writing cmd\n");
1425 if (!async->prealloc_bufsz) {
1427 DPRINTK("no buffer (?)\n");
1431 comedi_reset_async_buf(async);
1434 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1436 if (async->cmd.flags & TRIG_WAKE_EOS)
1437 async->cb_mask |= COMEDI_CB_EOS;
1439 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1441 ret = s->do_cmd(dev, s);
1446 do_become_nonbusy(dev, s);
1453 command testing ioctl
1456 pointer to cmd structure
1459 cmd structure at arg
1463 modified cmd structure at arg
1466 static int do_cmdtest_ioctl(struct comedi_device *dev,
1467 struct comedi_cmd __user *arg, void *file)
1469 struct comedi_cmd user_cmd;
1470 struct comedi_subdevice *s;
1472 unsigned int *chanlist = NULL;
1473 unsigned int __user *chanlist_saver = NULL;
1475 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
1476 DPRINTK("bad cmd address\n");
1479 /* save user's chanlist pointer so it can be restored later */
1480 chanlist_saver = user_cmd.chanlist;
1482 if (user_cmd.subdev >= dev->n_subdevices) {
1483 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1487 s = dev->subdevices + user_cmd.subdev;
1488 if (s->type == COMEDI_SUBD_UNUSED) {
1489 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1493 if (!s->do_cmd || !s->do_cmdtest) {
1494 DPRINTK("subdevice %i does not support commands\n",
1499 /* make sure channel/gain list isn't too long */
1500 if (user_cmd.chanlist_len > s->len_chanlist) {
1501 DPRINTK("channel/gain list too long %d > %d\n",
1502 user_cmd.chanlist_len, s->len_chanlist);
1507 /* load channel/gain list */
1508 if (user_cmd.chanlist) {
1510 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1512 DPRINTK("allocation failed\n");
1517 if (copy_from_user(chanlist, user_cmd.chanlist,
1518 user_cmd.chanlist_len * sizeof(int))) {
1519 DPRINTK("fault reading chanlist\n");
1524 /* make sure each element in channel/gain list is valid */
1525 ret = comedi_check_chanlist(s, user_cmd.chanlist_len, chanlist);
1527 DPRINTK("bad chanlist\n");
1531 user_cmd.chanlist = chanlist;
1534 ret = s->do_cmdtest(dev, s, &user_cmd);
1536 /* restore chanlist pointer before copying back */
1537 user_cmd.chanlist = chanlist_saver;
1539 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
1540 DPRINTK("bad cmd address\n");
1565 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1569 unsigned long flags;
1570 struct comedi_subdevice *s;
1572 if (arg >= dev->n_subdevices)
1574 s = dev->subdevices + arg;
1576 spin_lock_irqsave(&s->spin_lock, flags);
1577 if (s->busy || s->lock)
1581 spin_unlock_irqrestore(&s->spin_lock, flags);
1588 ret = s->lock_f(dev, s);
1607 This function isn't protected by the semaphore, since
1608 we already own the lock.
1610 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1613 struct comedi_subdevice *s;
1615 if (arg >= dev->n_subdevices)
1617 s = dev->subdevices + arg;
1622 if (s->lock && s->lock != file)
1625 if (s->lock == file) {
1639 cancel acquisition ioctl
1651 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1654 struct comedi_subdevice *s;
1656 if (arg >= dev->n_subdevices)
1658 s = dev->subdevices + arg;
1659 if (s->async == NULL)
1662 if (s->lock && s->lock != file)
1668 if (s->busy != file)
1671 return do_cancel(dev, s);
1676 instructs driver to synchronize buffers
1688 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1691 struct comedi_subdevice *s;
1693 if (arg >= dev->n_subdevices)
1695 s = dev->subdevices + arg;
1697 if (s->lock && s->lock != file)
1703 if (s->busy != file)
1707 return s->poll(dev, s);
1712 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1716 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1717 ret = s->cancel(dev, s);
1719 do_become_nonbusy(dev, s);
1725 static void comedi_vm_open(struct vm_area_struct *area)
1727 struct comedi_async *async;
1728 struct comedi_device *dev;
1730 async = area->vm_private_data;
1731 dev = async->subdevice->device;
1733 mutex_lock(&dev->mutex);
1734 async->mmap_count++;
1735 mutex_unlock(&dev->mutex);
1738 static void comedi_vm_close(struct vm_area_struct *area)
1740 struct comedi_async *async;
1741 struct comedi_device *dev;
1743 async = area->vm_private_data;
1744 dev = async->subdevice->device;
1746 mutex_lock(&dev->mutex);
1747 async->mmap_count--;
1748 mutex_unlock(&dev->mutex);
1751 static struct vm_operations_struct comedi_vm_ops = {
1752 .open = comedi_vm_open,
1753 .close = comedi_vm_close,
1756 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1758 const unsigned minor = iminor(file->f_dentry->d_inode);
1759 struct comedi_async *async = NULL;
1760 unsigned long start = vma->vm_start;
1765 struct comedi_subdevice *s;
1766 struct comedi_device_file_info *dev_file_info;
1767 struct comedi_device *dev;
1769 dev_file_info = comedi_get_device_file_info(minor);
1770 if (dev_file_info == NULL)
1772 dev = dev_file_info->device;
1776 mutex_lock(&dev->mutex);
1777 if (!dev->attached) {
1778 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1782 if (vma->vm_flags & VM_WRITE)
1783 s = comedi_get_write_subdevice(dev_file_info);
1785 s = comedi_get_read_subdevice(dev_file_info);
1792 if (async == NULL) {
1797 if (vma->vm_pgoff != 0) {
1798 DPRINTK("comedi: mmap() offset must be 0.\n");
1803 size = vma->vm_end - vma->vm_start;
1804 if (size > async->prealloc_bufsz) {
1808 if (size & (~PAGE_MASK)) {
1813 n_pages = size >> PAGE_SHIFT;
1814 for (i = 0; i < n_pages; ++i) {
1815 if (remap_pfn_range(vma, start,
1816 page_to_pfn(virt_to_page
1817 (async->buf_page_list
1818 [i].virt_addr)), PAGE_SIZE,
1826 vma->vm_ops = &comedi_vm_ops;
1827 vma->vm_private_data = async;
1829 async->mmap_count++;
1833 mutex_unlock(&dev->mutex);
1837 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1839 unsigned int mask = 0;
1840 const unsigned minor = iminor(file->f_dentry->d_inode);
1841 struct comedi_subdevice *read_subdev;
1842 struct comedi_subdevice *write_subdev;
1843 struct comedi_device_file_info *dev_file_info;
1844 struct comedi_device *dev;
1845 dev_file_info = comedi_get_device_file_info(minor);
1847 if (dev_file_info == NULL)
1849 dev = dev_file_info->device;
1853 mutex_lock(&dev->mutex);
1854 if (!dev->attached) {
1855 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1856 mutex_unlock(&dev->mutex);
1861 read_subdev = comedi_get_read_subdevice(dev_file_info);
1863 poll_wait(file, &read_subdev->async->wait_head, wait);
1864 if (!read_subdev->busy
1865 || comedi_buf_read_n_available(read_subdev->async) > 0
1866 || !(comedi_get_subdevice_runflags(read_subdev) &
1868 mask |= POLLIN | POLLRDNORM;
1871 write_subdev = comedi_get_write_subdevice(dev_file_info);
1873 poll_wait(file, &write_subdev->async->wait_head, wait);
1874 comedi_buf_write_alloc(write_subdev->async,
1875 write_subdev->async->prealloc_bufsz);
1876 if (!write_subdev->busy
1877 || !(comedi_get_subdevice_runflags(write_subdev) &
1879 || comedi_buf_write_n_allocated(write_subdev->async) >=
1880 bytes_per_sample(write_subdev->async->subdevice)) {
1881 mask |= POLLOUT | POLLWRNORM;
1885 mutex_unlock(&dev->mutex);
1889 static ssize_t comedi_write(struct file *file, const char __user *buf,
1890 size_t nbytes, loff_t *offset)
1892 struct comedi_subdevice *s;
1893 struct comedi_async *async;
1894 int n, m, count = 0, retval = 0;
1895 DECLARE_WAITQUEUE(wait, current);
1896 const unsigned minor = iminor(file->f_dentry->d_inode);
1897 struct comedi_device_file_info *dev_file_info;
1898 struct comedi_device *dev;
1899 dev_file_info = comedi_get_device_file_info(minor);
1901 if (dev_file_info == NULL)
1903 dev = dev_file_info->device;
1907 if (!dev->attached) {
1908 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1913 s = comedi_get_write_subdevice(dev_file_info);
1928 if (s->busy != file) {
1932 add_wait_queue(&async->wait_head, &wait);
1933 while (nbytes > 0 && !retval) {
1934 set_current_state(TASK_INTERRUPTIBLE);
1936 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1938 if (comedi_get_subdevice_runflags(s) &
1944 do_become_nonbusy(dev, s);
1952 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1953 m = async->prealloc_bufsz - async->buf_write_ptr;
1954 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1955 if (m > comedi_buf_write_n_allocated(async))
1956 m = comedi_buf_write_n_allocated(async);
1961 if (file->f_flags & O_NONBLOCK) {
1966 if (signal_pending(current)) {
1967 retval = -ERESTARTSYS;
1972 if (s->busy != file) {
1979 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1985 comedi_buf_write_free(async, n);
1991 break; /* makes device work like a pipe */
1993 set_current_state(TASK_RUNNING);
1994 remove_wait_queue(&async->wait_head, &wait);
1997 return count ? count : retval;
2000 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2003 struct comedi_subdevice *s;
2004 struct comedi_async *async;
2005 int n, m, count = 0, retval = 0;
2006 DECLARE_WAITQUEUE(wait, current);
2007 const unsigned minor = iminor(file->f_dentry->d_inode);
2008 struct comedi_device_file_info *dev_file_info;
2009 struct comedi_device *dev;
2010 dev_file_info = comedi_get_device_file_info(minor);
2012 if (dev_file_info == NULL)
2014 dev = dev_file_info->device;
2018 if (!dev->attached) {
2019 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2024 s = comedi_get_read_subdevice(dev_file_info);
2038 if (s->busy != file) {
2043 add_wait_queue(&async->wait_head, &wait);
2044 while (nbytes > 0 && !retval) {
2045 set_current_state(TASK_INTERRUPTIBLE);
2049 m = comedi_buf_read_n_available(async);
2050 /* printk("%d available\n",m); */
2051 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2052 m = async->prealloc_bufsz - async->buf_read_ptr;
2053 /* printk("%d contiguous\n",m); */
2058 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
2059 do_become_nonbusy(dev, s);
2060 if (comedi_get_subdevice_runflags(s) &
2068 if (file->f_flags & O_NONBLOCK) {
2073 if (signal_pending(current)) {
2074 retval = -ERESTARTSYS;
2081 if (s->busy != file) {
2087 m = copy_to_user(buf, async->prealloc_buf +
2088 async->buf_read_ptr, n);
2094 comedi_buf_read_alloc(async, n);
2095 comedi_buf_read_free(async, n);
2101 break; /* makes device work like a pipe */
2103 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
2104 async->buf_read_count - async->buf_write_count == 0) {
2105 do_become_nonbusy(dev, s);
2107 set_current_state(TASK_RUNNING);
2108 remove_wait_queue(&async->wait_head, &wait);
2111 return count ? count : retval;
2115 This function restores a subdevice to an idle state.
2117 void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
2119 struct comedi_async *async = s->async;
2121 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
2123 comedi_reset_async_buf(async);
2124 async->inttrig = NULL;
2127 "BUG: (?) do_become_nonbusy called with async=0\n");
2133 static int comedi_open(struct inode *inode, struct file *file)
2135 const unsigned minor = iminor(inode);
2136 struct comedi_device_file_info *dev_file_info =
2137 comedi_get_device_file_info(minor);
2138 struct comedi_device *dev =
2139 dev_file_info ? dev_file_info->device : NULL;
2142 DPRINTK("invalid minor number\n");
2146 /* This is slightly hacky, but we want module autoloading
2148 * case: user opens device, attached -> ok
2149 * case: user opens device, unattached, in_request_module=0 -> autoload
2150 * case: user opens device, unattached, in_request_module=1 -> fail
2151 * case: root opens device, attached -> ok
2152 * case: root opens device, unattached, in_request_module=1 -> ok
2153 * (typically called from modprobe)
2154 * case: root opens device, unattached, in_request_module=0 -> autoload
2156 * The last could be changed to "-> ok", which would deny root
2159 mutex_lock(&dev->mutex);
2162 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2163 DPRINTK("in request module\n");
2164 mutex_unlock(&dev->mutex);
2167 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2170 dev->in_request_module = 1;
2173 mutex_unlock(&dev->mutex);
2174 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2175 mutex_lock(&dev->mutex);
2178 dev->in_request_module = 0;
2180 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2181 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2182 mutex_unlock(&dev->mutex);
2186 __module_get(THIS_MODULE);
2188 if (dev->attached) {
2189 if (!try_module_get(dev->driver->module)) {
2190 module_put(THIS_MODULE);
2191 mutex_unlock(&dev->mutex);
2196 if (dev->attached && dev->use_count == 0 && dev->open) {
2197 int rc = dev->open(dev);
2199 module_put(dev->driver->module);
2200 module_put(THIS_MODULE);
2201 mutex_unlock(&dev->mutex);
2208 mutex_unlock(&dev->mutex);
2213 static int comedi_close(struct inode *inode, struct file *file)
2215 const unsigned minor = iminor(inode);
2216 struct comedi_subdevice *s = NULL;
2218 struct comedi_device_file_info *dev_file_info;
2219 struct comedi_device *dev;
2220 dev_file_info = comedi_get_device_file_info(minor);
2222 if (dev_file_info == NULL)
2224 dev = dev_file_info->device;
2228 mutex_lock(&dev->mutex);
2230 if (dev->subdevices) {
2231 for (i = 0; i < dev->n_subdevices; i++) {
2232 s = dev->subdevices + i;
2234 if (s->busy == file)
2236 if (s->lock == file)
2240 if (dev->attached && dev->use_count == 1 && dev->close)
2243 module_put(THIS_MODULE);
2245 module_put(dev->driver->module);
2249 mutex_unlock(&dev->mutex);
2251 if (file->f_flags & FASYNC)
2252 comedi_fasync(-1, file, 0);
2257 static int comedi_fasync(int fd, struct file *file, int on)
2259 const unsigned minor = iminor(file->f_dentry->d_inode);
2260 struct comedi_device_file_info *dev_file_info;
2261 struct comedi_device *dev;
2262 dev_file_info = comedi_get_device_file_info(minor);
2264 if (dev_file_info == NULL)
2266 dev = dev_file_info->device;
2270 return fasync_helper(fd, file, on, &dev->async_queue);
2273 const struct file_operations comedi_fops = {
2274 .owner = THIS_MODULE,
2275 .unlocked_ioctl = comedi_unlocked_ioctl,
2276 .compat_ioctl = comedi_compat_ioctl,
2277 .open = comedi_open,
2278 .release = comedi_close,
2279 .read = comedi_read,
2280 .write = comedi_write,
2281 .mmap = comedi_mmap,
2282 .poll = comedi_poll,
2283 .fasync = comedi_fasync,
2284 .llseek = noop_llseek,
2287 struct class *comedi_class;
2288 static struct cdev comedi_cdev;
2290 static void comedi_cleanup_legacy_minors(void)
2294 for (i = 0; i < comedi_num_legacy_minors; i++)
2295 comedi_free_board_minor(i);
2298 static int __init comedi_init(void)
2303 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
2304 " - http://www.comedi.org\n");
2306 if (comedi_num_legacy_minors < 0 ||
2307 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2308 printk(KERN_ERR "comedi: error: invalid value for module "
2309 "parameter \"comedi_num_legacy_minors\". Valid values "
2310 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
2315 * comedi is unusable if both comedi_autoconfig and
2316 * comedi_num_legacy_minors are zero, so we might as well adjust the
2317 * defaults in that case
2319 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2320 comedi_num_legacy_minors = 16;
2322 memset(comedi_file_info_table, 0,
2323 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
2325 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2326 COMEDI_NUM_MINORS, "comedi");
2329 cdev_init(&comedi_cdev, &comedi_fops);
2330 comedi_cdev.owner = THIS_MODULE;
2331 kobject_set_name(&comedi_cdev.kobj, "comedi");
2332 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2333 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2337 comedi_class = class_create(THIS_MODULE, "comedi");
2338 if (IS_ERR(comedi_class)) {
2339 printk(KERN_ERR "comedi: failed to create class");
2340 cdev_del(&comedi_cdev);
2341 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2343 return PTR_ERR(comedi_class);
2346 comedi_class->dev_attrs = comedi_dev_attrs;
2348 /* XXX requires /proc interface */
2351 /* create devices files for legacy/manual use */
2352 for (i = 0; i < comedi_num_legacy_minors; i++) {
2354 minor = comedi_alloc_board_minor(NULL);
2356 comedi_cleanup_legacy_minors();
2357 cdev_del(&comedi_cdev);
2358 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2367 static void __exit comedi_cleanup(void)
2371 comedi_cleanup_legacy_minors();
2372 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2373 BUG_ON(comedi_file_info_table[i]);
2375 class_destroy(comedi_class);
2376 cdev_del(&comedi_cdev);
2377 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2379 comedi_proc_cleanup();
2382 module_init(comedi_init);
2383 module_exit(comedi_cleanup);
2385 void comedi_error(const struct comedi_device *dev, const char *s)
2387 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
2388 dev->driver->driver_name, s);
2390 EXPORT_SYMBOL(comedi_error);
2392 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2394 struct comedi_async *async = s->async;
2395 unsigned runflags = 0;
2396 unsigned runflags_mask = 0;
2398 /* DPRINTK("comedi_event 0x%x\n",mask); */
2400 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2404 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2405 COMEDI_CB_OVERFLOW)) {
2406 runflags_mask |= SRF_RUNNING;
2408 /* remember if an error event has occurred, so an error
2409 * can be returned the next time the user does a read() */
2410 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2411 runflags_mask |= SRF_ERROR;
2412 runflags |= SRF_ERROR;
2414 if (runflags_mask) {
2415 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2416 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2419 if (async->cb_mask & s->async->events) {
2420 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2421 wake_up_interruptible(&async->wait_head);
2422 if (s->subdev_flags & SDF_CMD_READ)
2423 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2424 if (s->subdev_flags & SDF_CMD_WRITE)
2425 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2428 async->cb_func(s->async->events, async->cb_arg);
2431 s->async->events = 0;
2433 EXPORT_SYMBOL(comedi_event);
2435 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
2437 unsigned long flags;
2440 spin_lock_irqsave(&s->spin_lock, flags);
2441 runflags = s->runflags;
2442 spin_unlock_irqrestore(&s->spin_lock, flags);
2445 EXPORT_SYMBOL(comedi_get_subdevice_runflags);
2447 static int is_device_busy(struct comedi_device *dev)
2449 struct comedi_subdevice *s;
2455 for (i = 0; i < dev->n_subdevices; i++) {
2456 s = dev->subdevices + i;
2459 if (s->async && s->async->mmap_count)
2466 static void comedi_device_init(struct comedi_device *dev)
2468 memset(dev, 0, sizeof(struct comedi_device));
2469 spin_lock_init(&dev->spinlock);
2470 mutex_init(&dev->mutex);
2474 static void comedi_device_cleanup(struct comedi_device *dev)
2478 mutex_lock(&dev->mutex);
2479 comedi_device_detach(dev);
2480 mutex_unlock(&dev->mutex);
2481 mutex_destroy(&dev->mutex);
2484 int comedi_alloc_board_minor(struct device *hardware_device)
2486 struct comedi_device_file_info *info;
2487 struct device *csdev;
2490 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2493 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2494 if (info->device == NULL) {
2498 info->hardware_device = hardware_device;
2499 comedi_device_init(info->device);
2500 spin_lock(&comedi_file_info_table_lock);
2501 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2502 if (comedi_file_info_table[i] == NULL) {
2503 comedi_file_info_table[i] = info;
2507 spin_unlock(&comedi_file_info_table_lock);
2508 if (i == COMEDI_NUM_BOARD_MINORS) {
2509 comedi_device_cleanup(info->device);
2510 kfree(info->device);
2514 "ran out of minor numbers for board device files.\n");
2517 info->device->minor = i;
2518 csdev = device_create(comedi_class, hardware_device,
2519 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2521 info->device->class_dev = csdev;
2522 dev_set_drvdata(csdev, info);
2527 void comedi_free_board_minor(unsigned minor)
2529 struct comedi_device_file_info *info;
2531 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2532 spin_lock(&comedi_file_info_table_lock);
2533 info = comedi_file_info_table[minor];
2534 comedi_file_info_table[minor] = NULL;
2535 spin_unlock(&comedi_file_info_table_lock);
2538 struct comedi_device *dev = info->device;
2540 if (dev->class_dev) {
2541 device_destroy(comedi_class,
2542 MKDEV(COMEDI_MAJOR, dev->minor));
2544 comedi_device_cleanup(dev);
2551 int comedi_find_board_minor(struct device *hardware_device)
2554 struct comedi_device_file_info *info;
2556 for (minor = 0; minor < COMEDI_NUM_BOARD_MINORS; minor++) {
2557 spin_lock(&comedi_file_info_table_lock);
2558 info = comedi_file_info_table[minor];
2559 if (info && info->hardware_device == hardware_device) {
2560 spin_unlock(&comedi_file_info_table_lock);
2563 spin_unlock(&comedi_file_info_table_lock);
2568 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2569 struct comedi_subdevice *s)
2571 struct comedi_device_file_info *info;
2572 struct device *csdev;
2575 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2579 info->read_subdevice = s;
2580 info->write_subdevice = s;
2581 spin_lock(&comedi_file_info_table_lock);
2582 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2583 if (comedi_file_info_table[i] == NULL) {
2584 comedi_file_info_table[i] = info;
2588 spin_unlock(&comedi_file_info_table_lock);
2589 if (i == COMEDI_NUM_MINORS) {
2593 "ran out of minor numbers for board device files.\n");
2597 csdev = device_create(comedi_class, dev->class_dev,
2598 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2599 dev->minor, (int)(s - dev->subdevices));
2601 s->class_dev = csdev;
2602 dev_set_drvdata(csdev, info);
2607 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2609 struct comedi_device_file_info *info;
2616 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2617 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2619 spin_lock(&comedi_file_info_table_lock);
2620 info = comedi_file_info_table[s->minor];
2621 comedi_file_info_table[s->minor] = NULL;
2622 spin_unlock(&comedi_file_info_table_lock);
2625 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2626 s->class_dev = NULL;
2631 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2633 struct comedi_device_file_info *info;
2635 BUG_ON(minor >= COMEDI_NUM_MINORS);
2636 spin_lock(&comedi_file_info_table_lock);
2637 info = comedi_file_info_table[minor];
2638 spin_unlock(&comedi_file_info_table_lock);
2641 EXPORT_SYMBOL_GPL(comedi_get_device_file_info);