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_FIRST_SUBDEVICE_MINOR COMEDI_NUM_BOARD_MINORS
55 #ifdef CONFIG_COMEDI_DEBUG
57 EXPORT_SYMBOL(comedi_debug);
58 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
59 MODULE_PARM_DESC(comedi_debug,
60 "enable comedi core and driver debugging if non-zero (default 0)"
64 static int comedi_num_legacy_minors;
65 module_param(comedi_num_legacy_minors, int, S_IRUGO);
66 MODULE_PARM_DESC(comedi_num_legacy_minors,
67 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
70 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
71 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
72 MODULE_PARM_DESC(comedi_default_buf_size_kb,
73 "default asynchronous buffer size in KiB (default "
74 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
76 unsigned int comedi_default_buf_maxsize_kb
77 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
78 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
79 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
80 "default maximum size of asynchronous buffer in KiB (default "
81 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
83 struct comedi_file_info {
84 struct comedi_device *device;
85 struct comedi_subdevice *read_subdevice;
86 struct comedi_subdevice *write_subdevice;
87 struct device *hardware_device;
90 static DEFINE_MUTEX(comedi_file_info_table_lock);
91 static struct comedi_file_info *comedi_file_info_table[COMEDI_NUM_MINORS];
93 static struct class *comedi_class;
94 static struct cdev comedi_cdev;
96 static void comedi_device_init(struct comedi_device *dev)
98 spin_lock_init(&dev->spinlock);
99 mutex_init(&dev->mutex);
103 static void comedi_device_cleanup(struct comedi_device *dev)
105 struct module *driver_module = NULL;
109 mutex_lock(&dev->mutex);
111 driver_module = dev->driver->module;
112 comedi_device_detach(dev);
113 while (dev->use_count > 0) {
115 module_put(driver_module);
116 module_put(THIS_MODULE);
119 mutex_unlock(&dev->mutex);
120 mutex_destroy(&dev->mutex);
123 static struct comedi_file_info *comedi_clear_minor(unsigned minor)
125 struct comedi_file_info *info;
127 mutex_lock(&comedi_file_info_table_lock);
128 info = comedi_file_info_table[minor];
129 comedi_file_info_table[minor] = NULL;
130 mutex_unlock(&comedi_file_info_table_lock);
134 static void comedi_free_board_file_info(struct comedi_file_info *info)
137 struct comedi_device *dev = info->device;
139 if (dev->class_dev) {
140 device_destroy(comedi_class,
141 MKDEV(COMEDI_MAJOR, dev->minor));
143 comedi_device_cleanup(dev);
150 static struct comedi_file_info *comedi_file_info_from_minor(unsigned minor)
152 struct comedi_file_info *info;
154 BUG_ON(minor >= COMEDI_NUM_MINORS);
155 mutex_lock(&comedi_file_info_table_lock);
156 info = comedi_file_info_table[minor];
157 mutex_unlock(&comedi_file_info_table_lock);
161 static struct comedi_device *
162 comedi_dev_from_file_info(struct comedi_file_info *info)
164 return info ? info->device : NULL;
167 struct comedi_device *comedi_dev_from_minor(unsigned minor)
169 return comedi_dev_from_file_info(comedi_file_info_from_minor(minor));
171 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
173 static struct comedi_subdevice *
174 comedi_read_subdevice(const struct comedi_file_info *info)
176 if (info->read_subdevice)
177 return info->read_subdevice;
179 return info->device->read_subdev;
183 static struct comedi_subdevice *
184 comedi_write_subdevice(const struct comedi_file_info *info)
186 if (info->write_subdevice)
187 return info->write_subdevice;
189 return info->device->write_subdev;
193 static int resize_async_buffer(struct comedi_device *dev,
194 struct comedi_subdevice *s,
195 struct comedi_async *async, unsigned new_size)
199 if (new_size > async->max_bufsize)
203 DPRINTK("subdevice is busy, cannot resize buffer\n");
206 if (async->mmap_count) {
207 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
211 if (!async->prealloc_buf)
214 /* make sure buffer is an integral number of pages
216 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
218 retval = comedi_buf_alloc(dev, s, new_size);
223 retval = s->buf_change(dev, s, new_size);
228 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
229 dev->minor, s->index, async->prealloc_bufsz);
233 /* sysfs attribute files */
235 static ssize_t show_max_read_buffer_kb(struct device *dev,
236 struct device_attribute *attr, char *buf)
238 struct comedi_file_info *info = dev_get_drvdata(dev);
239 struct comedi_subdevice *s = comedi_read_subdevice(info);
240 unsigned int size = 0;
242 mutex_lock(&info->device->mutex);
243 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
244 size = s->async->max_bufsize / 1024;
245 mutex_unlock(&info->device->mutex);
247 return snprintf(buf, PAGE_SIZE, "%i\n", size);
250 static ssize_t store_max_read_buffer_kb(struct device *dev,
251 struct device_attribute *attr,
252 const char *buf, size_t count)
254 struct comedi_file_info *info = dev_get_drvdata(dev);
255 struct comedi_subdevice *s = comedi_read_subdevice(info);
259 err = kstrtouint(buf, 10, &size);
262 if (size > (UINT_MAX / 1024))
266 mutex_lock(&info->device->mutex);
267 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
268 s->async->max_bufsize = size;
271 mutex_unlock(&info->device->mutex);
273 return err ? err : count;
276 static ssize_t show_read_buffer_kb(struct device *dev,
277 struct device_attribute *attr, char *buf)
279 struct comedi_file_info *info = dev_get_drvdata(dev);
280 struct comedi_subdevice *s = comedi_read_subdevice(info);
281 unsigned int size = 0;
283 mutex_lock(&info->device->mutex);
284 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
285 size = s->async->prealloc_bufsz / 1024;
286 mutex_unlock(&info->device->mutex);
288 return snprintf(buf, PAGE_SIZE, "%i\n", size);
291 static ssize_t store_read_buffer_kb(struct device *dev,
292 struct device_attribute *attr,
293 const char *buf, size_t count)
295 struct comedi_file_info *info = dev_get_drvdata(dev);
296 struct comedi_subdevice *s = comedi_read_subdevice(info);
300 err = kstrtouint(buf, 10, &size);
303 if (size > (UINT_MAX / 1024))
307 mutex_lock(&info->device->mutex);
308 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
309 err = resize_async_buffer(info->device, s, s->async, size);
312 mutex_unlock(&info->device->mutex);
314 return err ? err : count;
317 static ssize_t show_max_write_buffer_kb(struct device *dev,
318 struct device_attribute *attr,
321 struct comedi_file_info *info = dev_get_drvdata(dev);
322 struct comedi_subdevice *s = comedi_write_subdevice(info);
323 unsigned int size = 0;
325 mutex_lock(&info->device->mutex);
326 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
327 size = s->async->max_bufsize / 1024;
328 mutex_unlock(&info->device->mutex);
330 return snprintf(buf, PAGE_SIZE, "%i\n", size);
333 static ssize_t store_max_write_buffer_kb(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf, size_t count)
337 struct comedi_file_info *info = dev_get_drvdata(dev);
338 struct comedi_subdevice *s = comedi_write_subdevice(info);
342 err = kstrtouint(buf, 10, &size);
345 if (size > (UINT_MAX / 1024))
349 mutex_lock(&info->device->mutex);
350 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
351 s->async->max_bufsize = size;
354 mutex_unlock(&info->device->mutex);
356 return err ? err : count;
359 static ssize_t show_write_buffer_kb(struct device *dev,
360 struct device_attribute *attr, char *buf)
362 struct comedi_file_info *info = dev_get_drvdata(dev);
363 struct comedi_subdevice *s = comedi_write_subdevice(info);
364 unsigned int size = 0;
366 mutex_lock(&info->device->mutex);
367 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
368 size = s->async->prealloc_bufsz / 1024;
369 mutex_unlock(&info->device->mutex);
371 return snprintf(buf, PAGE_SIZE, "%i\n", size);
374 static ssize_t store_write_buffer_kb(struct device *dev,
375 struct device_attribute *attr,
376 const char *buf, size_t count)
378 struct comedi_file_info *info = dev_get_drvdata(dev);
379 struct comedi_subdevice *s = comedi_write_subdevice(info);
383 err = kstrtouint(buf, 10, &size);
386 if (size > (UINT_MAX / 1024))
390 mutex_lock(&info->device->mutex);
391 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
392 err = resize_async_buffer(info->device, s, s->async, size);
395 mutex_unlock(&info->device->mutex);
397 return err ? err : count;
400 static struct device_attribute comedi_dev_attrs[] = {
401 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
402 show_max_read_buffer_kb, store_max_read_buffer_kb),
403 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
404 show_read_buffer_kb, store_read_buffer_kb),
405 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
406 show_max_write_buffer_kb, store_max_write_buffer_kb),
407 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
408 show_write_buffer_kb, store_write_buffer_kb),
412 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
413 unsigned mask, unsigned bits)
417 spin_lock_irqsave(&s->spin_lock, flags);
418 s->runflags &= ~mask;
419 s->runflags |= (bits & mask);
420 spin_unlock_irqrestore(&s->spin_lock, flags);
423 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
428 spin_lock_irqsave(&s->spin_lock, flags);
429 runflags = s->runflags;
430 spin_unlock_irqrestore(&s->spin_lock, flags);
434 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
436 unsigned runflags = comedi_get_subdevice_runflags(s);
438 return (runflags & SRF_RUNNING) ? true : false;
440 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
442 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
444 unsigned runflags = comedi_get_subdevice_runflags(s);
446 return (runflags & SRF_ERROR) ? true : false;
449 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
451 unsigned runflags = comedi_get_subdevice_runflags(s);
453 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
457 This function restores a subdevice to an idle state.
459 static void do_become_nonbusy(struct comedi_device *dev,
460 struct comedi_subdevice *s)
462 struct comedi_async *async = s->async;
464 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
466 comedi_buf_reset(async);
467 async->inttrig = NULL;
468 kfree(async->cmd.chanlist);
469 async->cmd.chanlist = NULL;
471 dev_err(dev->class_dev,
472 "BUG: (?) do_become_nonbusy called with async=NULL\n");
478 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
482 if (comedi_is_subdevice_running(s) && s->cancel)
483 ret = s->cancel(dev, s);
485 do_become_nonbusy(dev, s);
490 static int is_device_busy(struct comedi_device *dev)
492 struct comedi_subdevice *s;
498 for (i = 0; i < dev->n_subdevices; i++) {
499 s = &dev->subdevices[i];
502 if (s->async && s->async->mmap_count)
514 pointer to devconfig structure
517 devconfig structure at arg
522 static int do_devconfig_ioctl(struct comedi_device *dev,
523 struct comedi_devconfig __user *arg)
525 struct comedi_devconfig it;
527 if (!capable(CAP_SYS_ADMIN))
531 if (is_device_busy(dev))
534 struct module *driver_module = dev->driver->module;
535 comedi_device_detach(dev);
536 module_put(driver_module);
541 if (copy_from_user(&it, arg, sizeof(it)))
544 it.board_name[COMEDI_NAMELEN - 1] = 0;
546 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
547 dev_warn(dev->class_dev,
548 "comedi_config --init_data is deprecated\n");
552 if (dev->minor >= comedi_num_legacy_minors)
553 /* don't re-use dynamically allocated comedi devices */
556 /* This increments the driver module count on success. */
557 return comedi_device_attach(dev, &it);
562 buffer configuration ioctl
565 pointer to bufconfig structure
571 modified bufconfig at arg
574 static int do_bufconfig_ioctl(struct comedi_device *dev,
575 struct comedi_bufconfig __user *arg)
577 struct comedi_bufconfig bc;
578 struct comedi_async *async;
579 struct comedi_subdevice *s;
582 if (copy_from_user(&bc, arg, sizeof(bc)))
585 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
588 s = &dev->subdevices[bc.subdevice];
592 DPRINTK("subdevice does not have async capability\n");
598 if (bc.maximum_size) {
599 if (!capable(CAP_SYS_ADMIN))
602 async->max_bufsize = bc.maximum_size;
606 retval = resize_async_buffer(dev, s, async, bc.size);
611 bc.size = async->prealloc_bufsz;
612 bc.maximum_size = async->max_bufsize;
615 if (copy_to_user(arg, &bc, sizeof(bc)))
626 pointer to devinfo structure
635 static int do_devinfo_ioctl(struct comedi_device *dev,
636 struct comedi_devinfo __user *arg,
639 const unsigned minor = iminor(file_inode(file));
640 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
641 struct comedi_subdevice *s;
642 struct comedi_devinfo devinfo;
644 memset(&devinfo, 0, sizeof(devinfo));
646 /* fill devinfo structure */
647 devinfo.version_code = COMEDI_VERSION_CODE;
648 devinfo.n_subdevs = dev->n_subdevices;
649 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
650 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
652 s = comedi_read_subdevice(info);
654 devinfo.read_subdevice = s->index;
656 devinfo.read_subdevice = -1;
658 s = comedi_write_subdevice(info);
660 devinfo.write_subdevice = s->index;
662 devinfo.write_subdevice = -1;
664 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
675 pointer to array of subdevice info structures
681 array of subdevice info structures at arg
684 static int do_subdinfo_ioctl(struct comedi_device *dev,
685 struct comedi_subdinfo __user *arg, void *file)
688 struct comedi_subdinfo *tmp, *us;
689 struct comedi_subdevice *s;
691 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
695 /* fill subdinfo structs */
696 for (i = 0; i < dev->n_subdevices; i++) {
697 s = &dev->subdevices[i];
701 us->n_chan = s->n_chan;
702 us->subd_flags = s->subdev_flags;
703 if (comedi_is_subdevice_running(s))
704 us->subd_flags |= SDF_RUNNING;
705 #define TIMER_nanosec 5 /* backwards compatibility */
706 us->timer_type = TIMER_nanosec;
707 us->len_chanlist = s->len_chanlist;
708 us->maxdata = s->maxdata;
709 if (s->range_table) {
711 (i << 24) | (0 << 16) | (s->range_table->length);
713 us->range_type = 0; /* XXX */
715 us->flags = s->flags;
718 us->subd_flags |= SDF_BUSY;
720 us->subd_flags |= SDF_BUSY_OWNER;
722 us->subd_flags |= SDF_LOCKED;
724 us->subd_flags |= SDF_LOCK_OWNER;
725 if (!s->maxdata && s->maxdata_list)
726 us->subd_flags |= SDF_MAXDATA;
728 us->subd_flags |= SDF_FLAGS;
729 if (s->range_table_list)
730 us->subd_flags |= SDF_RANGETYPE;
732 us->subd_flags |= SDF_CMD;
734 if (s->insn_bits != &insn_inval)
735 us->insn_bits_support = COMEDI_SUPPORTED;
737 us->insn_bits_support = COMEDI_UNSUPPORTED;
739 us->settling_time_0 = s->settling_time_0;
742 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
746 return ret ? -EFAULT : 0;
754 pointer to chaninfo structure
757 chaninfo structure at arg
760 arrays at elements of chaninfo structure
763 static int do_chaninfo_ioctl(struct comedi_device *dev,
764 struct comedi_chaninfo __user *arg)
766 struct comedi_subdevice *s;
767 struct comedi_chaninfo it;
769 if (copy_from_user(&it, arg, sizeof(it)))
772 if (it.subdev >= dev->n_subdevices)
774 s = &dev->subdevices[it.subdev];
776 if (it.maxdata_list) {
777 if (s->maxdata || !s->maxdata_list)
779 if (copy_to_user(it.maxdata_list, s->maxdata_list,
780 s->n_chan * sizeof(unsigned int)))
787 if (copy_to_user(it.flaglist, s->flaglist,
788 s->n_chan * sizeof(unsigned int)))
795 if (!s->range_table_list)
797 for (i = 0; i < s->n_chan; i++) {
800 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
801 (s->range_table_list[i]->length);
802 if (put_user(x, it.rangelist + i))
806 if (copy_to_user(it.rangelist, s->range_type_list,
807 s->n_chan * sizeof(unsigned int)))
817 buffer information ioctl
820 pointer to bufinfo structure
826 modified bufinfo at arg
829 static int do_bufinfo_ioctl(struct comedi_device *dev,
830 struct comedi_bufinfo __user *arg, void *file)
832 struct comedi_bufinfo bi;
833 struct comedi_subdevice *s;
834 struct comedi_async *async;
836 if (copy_from_user(&bi, arg, sizeof(bi)))
839 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
842 s = &dev->subdevices[bi.subdevice];
844 if (s->lock && s->lock != file)
850 DPRINTK("subdevice does not have async capability\n");
851 bi.buf_write_ptr = 0;
853 bi.buf_write_count = 0;
854 bi.buf_read_count = 0;
856 bi.bytes_written = 0;
861 bi.bytes_written = 0;
862 goto copyback_position;
867 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
868 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
869 comedi_buf_read_free(async, bi.bytes_read);
871 if (comedi_is_subdevice_idle(s) &&
872 async->buf_write_count == async->buf_read_count) {
873 do_become_nonbusy(dev, s);
877 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
879 comedi_buf_write_alloc(async, bi.bytes_written);
880 comedi_buf_write_free(async, bi.bytes_written);
884 bi.buf_write_count = async->buf_write_count;
885 bi.buf_write_ptr = async->buf_write_ptr;
886 bi.buf_read_count = async->buf_read_count;
887 bi.buf_read_ptr = async->buf_read_ptr;
890 if (copy_to_user(arg, &bi, sizeof(bi)))
896 static int check_insn_config_length(struct comedi_insn *insn,
903 case INSN_CONFIG_DIO_OUTPUT:
904 case INSN_CONFIG_DIO_INPUT:
905 case INSN_CONFIG_DISARM:
906 case INSN_CONFIG_RESET:
910 case INSN_CONFIG_ARM:
911 case INSN_CONFIG_DIO_QUERY:
912 case INSN_CONFIG_BLOCK_SIZE:
913 case INSN_CONFIG_FILTER:
914 case INSN_CONFIG_SERIAL_CLOCK:
915 case INSN_CONFIG_BIDIRECTIONAL_DATA:
916 case INSN_CONFIG_ALT_SOURCE:
917 case INSN_CONFIG_SET_COUNTER_MODE:
918 case INSN_CONFIG_8254_READ_STATUS:
919 case INSN_CONFIG_SET_ROUTING:
920 case INSN_CONFIG_GET_ROUTING:
921 case INSN_CONFIG_GET_PWM_STATUS:
922 case INSN_CONFIG_PWM_SET_PERIOD:
923 case INSN_CONFIG_PWM_GET_PERIOD:
927 case INSN_CONFIG_SET_GATE_SRC:
928 case INSN_CONFIG_GET_GATE_SRC:
929 case INSN_CONFIG_SET_CLOCK_SRC:
930 case INSN_CONFIG_GET_CLOCK_SRC:
931 case INSN_CONFIG_SET_OTHER_SRC:
932 case INSN_CONFIG_GET_COUNTER_STATUS:
933 case INSN_CONFIG_PWM_SET_H_BRIDGE:
934 case INSN_CONFIG_PWM_GET_H_BRIDGE:
935 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
939 case INSN_CONFIG_PWM_OUTPUT:
940 case INSN_CONFIG_ANALOG_TRIG:
944 case INSN_CONFIG_DIGITAL_TRIG:
948 /* by default we allow the insn since we don't have checks for
949 * all possible cases yet */
951 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
953 pr_warn("comedi: Add a check to %s in %s.\n",
955 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
961 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
962 unsigned int *data, void *file)
964 struct comedi_subdevice *s;
968 if (insn->insn & INSN_MASK_SPECIAL) {
969 /* a non-subdevice instruction */
971 switch (insn->insn) {
981 do_gettimeofday(&tv);
983 data[1] = tv.tv_usec;
989 if (insn->n != 1 || data[0] >= 100000) {
993 udelay(data[0] / 1000);
1001 if (insn->subdev >= dev->n_subdevices) {
1002 DPRINTK("%d not usable subdevice\n",
1007 s = &dev->subdevices[insn->subdev];
1009 DPRINTK("no async\n");
1013 if (!s->async->inttrig) {
1014 DPRINTK("no inttrig\n");
1018 ret = s->async->inttrig(dev, s, data[0]);
1023 DPRINTK("invalid insn\n");
1028 /* a subdevice instruction */
1029 unsigned int maxdata;
1031 if (insn->subdev >= dev->n_subdevices) {
1032 DPRINTK("subdevice %d out of range\n", insn->subdev);
1036 s = &dev->subdevices[insn->subdev];
1038 if (s->type == COMEDI_SUBD_UNUSED) {
1039 DPRINTK("%d not usable subdevice\n", insn->subdev);
1044 /* are we locked? (ioctl lock) */
1045 if (s->lock && s->lock != file) {
1046 DPRINTK("device locked\n");
1051 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1054 DPRINTK("bad chanspec\n");
1062 /* This looks arbitrary. It is. */
1063 s->busy = &parse_insn;
1064 switch (insn->insn) {
1066 ret = s->insn_read(dev, s, insn, data);
1069 maxdata = s->maxdata_list
1070 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1072 for (i = 0; i < insn->n; ++i) {
1073 if (data[i] > maxdata) {
1075 DPRINTK("bad data value(s)\n");
1080 ret = s->insn_write(dev, s, insn, data);
1086 /* Most drivers ignore the base channel in
1087 * insn->chanspec. Fix this here if
1088 * the subdevice has <= 32 channels. */
1090 unsigned int orig_mask;
1092 orig_mask = data[0];
1093 if (s->n_chan <= 32) {
1094 shift = CR_CHAN(insn->chanspec);
1102 ret = s->insn_bits(dev, s, insn, data);
1103 data[0] = orig_mask;
1109 ret = check_insn_config_length(insn, data);
1112 ret = s->insn_config(dev, s, insn, data);
1128 * synchronous instructions
1131 * pointer to sync cmd structure
1134 * sync cmd struct at arg
1141 /* arbitrary limits */
1142 #define MAX_SAMPLES 256
1143 static int do_insnlist_ioctl(struct comedi_device *dev,
1144 struct comedi_insnlist __user *arg, void *file)
1146 struct comedi_insnlist insnlist;
1147 struct comedi_insn *insns = NULL;
1148 unsigned int *data = NULL;
1152 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1155 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1157 DPRINTK("kmalloc failed\n");
1162 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1164 DPRINTK("kmalloc failed\n");
1169 if (copy_from_user(insns, insnlist.insns,
1170 sizeof(*insns) * insnlist.n_insns)) {
1171 DPRINTK("copy_from_user failed\n");
1176 for (i = 0; i < insnlist.n_insns; i++) {
1177 if (insns[i].n > MAX_SAMPLES) {
1178 DPRINTK("number of samples too large\n");
1182 if (insns[i].insn & INSN_MASK_WRITE) {
1183 if (copy_from_user(data, insns[i].data,
1184 insns[i].n * sizeof(unsigned int))) {
1185 DPRINTK("copy_from_user failed\n");
1190 ret = parse_insn(dev, insns + i, data, file);
1193 if (insns[i].insn & INSN_MASK_READ) {
1194 if (copy_to_user(insns[i].data, data,
1195 insns[i].n * sizeof(unsigned int))) {
1196 DPRINTK("copy_to_user failed\n");
1216 * synchronous instructions
1222 * struct comedi_insn struct at arg
1228 static int do_insn_ioctl(struct comedi_device *dev,
1229 struct comedi_insn __user *arg, void *file)
1231 struct comedi_insn insn;
1232 unsigned int *data = NULL;
1235 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1241 if (copy_from_user(&insn, arg, sizeof(insn))) {
1246 /* This is where the behavior of insn and insnlist deviate. */
1247 if (insn.n > MAX_SAMPLES)
1248 insn.n = MAX_SAMPLES;
1249 if (insn.insn & INSN_MASK_WRITE) {
1250 if (copy_from_user(data,
1252 insn.n * sizeof(unsigned int))) {
1257 ret = parse_insn(dev, &insn, data, file);
1260 if (insn.insn & INSN_MASK_READ) {
1261 if (copy_to_user(insn.data,
1263 insn.n * sizeof(unsigned int))) {
1276 static int do_cmd_ioctl(struct comedi_device *dev,
1277 struct comedi_cmd __user *arg, void *file)
1279 struct comedi_cmd cmd;
1280 struct comedi_subdevice *s;
1281 struct comedi_async *async;
1283 unsigned int __user *user_chanlist;
1285 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1286 DPRINTK("bad cmd address\n");
1289 /* save user's chanlist pointer so it can be restored later */
1290 user_chanlist = (unsigned int __user *)cmd.chanlist;
1292 if (cmd.subdev >= dev->n_subdevices) {
1293 DPRINTK("%d no such subdevice\n", cmd.subdev);
1297 s = &dev->subdevices[cmd.subdev];
1300 if (s->type == COMEDI_SUBD_UNUSED) {
1301 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1305 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1306 DPRINTK("subdevice %i does not support commands\n",
1311 /* are we locked? (ioctl lock) */
1312 if (s->lock && s->lock != file) {
1313 DPRINTK("subdevice locked\n");
1319 DPRINTK("subdevice busy\n");
1324 /* make sure channel/gain list isn't too long */
1325 if (cmd.chanlist_len > s->len_chanlist) {
1326 DPRINTK("channel/gain list too long %u > %d\n",
1327 cmd.chanlist_len, s->len_chanlist);
1332 /* make sure channel/gain list isn't too short */
1333 if (cmd.chanlist_len < 1) {
1334 DPRINTK("channel/gain list too short %u < 1\n",
1341 async->cmd.data = NULL;
1342 /* load channel/gain list */
1343 async->cmd.chanlist =
1344 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1345 if (!async->cmd.chanlist) {
1346 DPRINTK("allocation failed\n");
1351 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1352 async->cmd.chanlist_len * sizeof(int))) {
1353 DPRINTK("fault reading chanlist\n");
1358 /* make sure each element in channel/gain list is valid */
1359 ret = comedi_check_chanlist(s,
1360 async->cmd.chanlist_len,
1361 async->cmd.chanlist);
1363 DPRINTK("bad chanlist\n");
1367 ret = s->do_cmdtest(dev, s, &async->cmd);
1369 if (async->cmd.flags & TRIG_BOGUS || ret) {
1370 DPRINTK("test returned %d\n", ret);
1372 /* restore chanlist pointer before copying back */
1373 cmd.chanlist = (unsigned int __force *)user_chanlist;
1375 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1376 DPRINTK("fault writing cmd\n");
1384 if (!async->prealloc_bufsz) {
1386 DPRINTK("no buffer (?)\n");
1390 comedi_buf_reset(async);
1393 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1395 if (async->cmd.flags & TRIG_WAKE_EOS)
1396 async->cb_mask |= COMEDI_CB_EOS;
1398 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1400 ret = s->do_cmd(dev, s);
1405 do_become_nonbusy(dev, s);
1412 command testing ioctl
1415 pointer to cmd structure
1418 cmd structure at arg
1422 modified cmd structure at arg
1425 static int do_cmdtest_ioctl(struct comedi_device *dev,
1426 struct comedi_cmd __user *arg, void *file)
1428 struct comedi_cmd cmd;
1429 struct comedi_subdevice *s;
1431 unsigned int *chanlist = NULL;
1432 unsigned int __user *user_chanlist;
1434 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1435 DPRINTK("bad cmd address\n");
1438 /* save user's chanlist pointer so it can be restored later */
1439 user_chanlist = (unsigned int __user *)cmd.chanlist;
1441 if (cmd.subdev >= dev->n_subdevices) {
1442 DPRINTK("%d no such subdevice\n", cmd.subdev);
1446 s = &dev->subdevices[cmd.subdev];
1447 if (s->type == COMEDI_SUBD_UNUSED) {
1448 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1452 if (!s->do_cmd || !s->do_cmdtest) {
1453 DPRINTK("subdevice %i does not support commands\n",
1458 /* make sure channel/gain list isn't too long */
1459 if (cmd.chanlist_len > s->len_chanlist) {
1460 DPRINTK("channel/gain list too long %d > %d\n",
1461 cmd.chanlist_len, s->len_chanlist);
1466 /* load channel/gain list */
1469 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1471 DPRINTK("allocation failed\n");
1476 if (copy_from_user(chanlist, user_chanlist,
1477 cmd.chanlist_len * sizeof(int))) {
1478 DPRINTK("fault reading chanlist\n");
1483 /* make sure each element in channel/gain list is valid */
1484 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1486 DPRINTK("bad chanlist\n");
1490 cmd.chanlist = chanlist;
1493 ret = s->do_cmdtest(dev, s, &cmd);
1495 /* restore chanlist pointer before copying back */
1496 cmd.chanlist = (unsigned int __force *)user_chanlist;
1498 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1499 DPRINTK("bad cmd address\n");
1524 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1528 unsigned long flags;
1529 struct comedi_subdevice *s;
1531 if (arg >= dev->n_subdevices)
1533 s = &dev->subdevices[arg];
1535 spin_lock_irqsave(&s->spin_lock, flags);
1536 if (s->busy || s->lock)
1540 spin_unlock_irqrestore(&s->spin_lock, flags);
1547 ret = s->lock_f(dev, s);
1566 This function isn't protected by the semaphore, since
1567 we already own the lock.
1569 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1572 struct comedi_subdevice *s;
1574 if (arg >= dev->n_subdevices)
1576 s = &dev->subdevices[arg];
1581 if (s->lock && s->lock != file)
1584 if (s->lock == file) {
1598 cancel acquisition ioctl
1610 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1613 struct comedi_subdevice *s;
1615 if (arg >= dev->n_subdevices)
1617 s = &dev->subdevices[arg];
1618 if (s->async == NULL)
1621 if (s->lock && s->lock != file)
1627 if (s->busy != file)
1630 return do_cancel(dev, s);
1635 instructs driver to synchronize buffers
1647 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1650 struct comedi_subdevice *s;
1652 if (arg >= dev->n_subdevices)
1654 s = &dev->subdevices[arg];
1656 if (s->lock && s->lock != file)
1662 if (s->busy != file)
1666 return s->poll(dev, s);
1671 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1674 const unsigned minor = iminor(file_inode(file));
1675 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1676 struct comedi_device *dev = comedi_dev_from_file_info(info);
1682 mutex_lock(&dev->mutex);
1684 /* Device config is special, because it must work on
1685 * an unconfigured device. */
1686 if (cmd == COMEDI_DEVCONFIG) {
1687 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1688 /* Device config not appropriate on non-board minors. */
1692 rc = do_devconfig_ioctl(dev,
1693 (struct comedi_devconfig __user *)arg);
1696 dev->minor >= comedi_num_legacy_minors) {
1697 /* Successfully unconfigured a dynamically
1698 * allocated device. Try and remove it. */
1699 info = comedi_clear_minor(dev->minor);
1701 mutex_unlock(&dev->mutex);
1702 comedi_free_board_file_info(info);
1710 if (!dev->attached) {
1711 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1717 case COMEDI_BUFCONFIG:
1718 rc = do_bufconfig_ioctl(dev,
1719 (struct comedi_bufconfig __user *)arg);
1721 case COMEDI_DEVINFO:
1722 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1725 case COMEDI_SUBDINFO:
1726 rc = do_subdinfo_ioctl(dev,
1727 (struct comedi_subdinfo __user *)arg,
1730 case COMEDI_CHANINFO:
1731 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1733 case COMEDI_RANGEINFO:
1734 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1736 case COMEDI_BUFINFO:
1737 rc = do_bufinfo_ioctl(dev,
1738 (struct comedi_bufinfo __user *)arg,
1742 rc = do_lock_ioctl(dev, arg, file);
1745 rc = do_unlock_ioctl(dev, arg, file);
1748 rc = do_cancel_ioctl(dev, arg, file);
1751 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1753 case COMEDI_CMDTEST:
1754 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1757 case COMEDI_INSNLIST:
1758 rc = do_insnlist_ioctl(dev,
1759 (struct comedi_insnlist __user *)arg,
1763 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1767 rc = do_poll_ioctl(dev, arg, file);
1775 mutex_unlock(&dev->mutex);
1779 static void comedi_vm_open(struct vm_area_struct *area)
1781 struct comedi_async *async;
1782 struct comedi_device *dev;
1784 async = area->vm_private_data;
1785 dev = async->subdevice->device;
1787 mutex_lock(&dev->mutex);
1788 async->mmap_count++;
1789 mutex_unlock(&dev->mutex);
1792 static void comedi_vm_close(struct vm_area_struct *area)
1794 struct comedi_async *async;
1795 struct comedi_device *dev;
1797 async = area->vm_private_data;
1798 dev = async->subdevice->device;
1800 mutex_lock(&dev->mutex);
1801 async->mmap_count--;
1802 mutex_unlock(&dev->mutex);
1805 static struct vm_operations_struct comedi_vm_ops = {
1806 .open = comedi_vm_open,
1807 .close = comedi_vm_close,
1810 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1812 const unsigned minor = iminor(file_inode(file));
1813 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1814 struct comedi_device *dev = comedi_dev_from_file_info(info);
1815 struct comedi_subdevice *s;
1816 struct comedi_async *async;
1817 unsigned long start = vma->vm_start;
1826 mutex_lock(&dev->mutex);
1828 if (!dev->attached) {
1829 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1834 if (vma->vm_flags & VM_WRITE)
1835 s = comedi_write_subdevice(info);
1837 s = comedi_read_subdevice(info);
1849 if (vma->vm_pgoff != 0) {
1850 DPRINTK("comedi: mmap() offset must be 0.\n");
1855 size = vma->vm_end - vma->vm_start;
1856 if (size > async->prealloc_bufsz) {
1860 if (size & (~PAGE_MASK)) {
1865 n_pages = size >> PAGE_SHIFT;
1866 for (i = 0; i < n_pages; ++i) {
1867 struct comedi_buf_page *buf = &async->buf_page_list[i];
1869 if (remap_pfn_range(vma, start,
1870 page_to_pfn(virt_to_page(buf->virt_addr)),
1871 PAGE_SIZE, PAGE_SHARED)) {
1878 vma->vm_ops = &comedi_vm_ops;
1879 vma->vm_private_data = async;
1881 async->mmap_count++;
1885 mutex_unlock(&dev->mutex);
1889 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1891 unsigned int mask = 0;
1892 const unsigned minor = iminor(file_inode(file));
1893 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1894 struct comedi_device *dev = comedi_dev_from_file_info(info);
1895 struct comedi_subdevice *s;
1900 mutex_lock(&dev->mutex);
1902 if (!dev->attached) {
1903 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1907 s = comedi_read_subdevice(info);
1908 if (s && s->async) {
1909 poll_wait(file, &s->async->wait_head, wait);
1910 if (!s->busy || !comedi_is_subdevice_running(s) ||
1911 comedi_buf_read_n_available(s->async) > 0)
1912 mask |= POLLIN | POLLRDNORM;
1915 s = comedi_write_subdevice(info);
1916 if (s && s->async) {
1917 unsigned int bps = bytes_per_sample(s->async->subdevice);
1919 poll_wait(file, &s->async->wait_head, wait);
1920 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1921 if (!s->busy || !comedi_is_subdevice_running(s) ||
1922 comedi_buf_write_n_allocated(s->async) >= bps)
1923 mask |= POLLOUT | POLLWRNORM;
1927 mutex_unlock(&dev->mutex);
1931 static ssize_t comedi_write(struct file *file, const char __user *buf,
1932 size_t nbytes, loff_t *offset)
1934 struct comedi_subdevice *s;
1935 struct comedi_async *async;
1936 int n, m, count = 0, retval = 0;
1937 DECLARE_WAITQUEUE(wait, current);
1938 const unsigned minor = iminor(file_inode(file));
1939 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1940 struct comedi_device *dev = comedi_dev_from_file_info(info);
1945 if (!dev->attached) {
1946 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1950 s = comedi_write_subdevice(info);
1951 if (!s || !s->async)
1956 if (!s->busy || !nbytes)
1958 if (s->busy != file)
1961 add_wait_queue(&async->wait_head, &wait);
1962 while (nbytes > 0 && !retval) {
1963 set_current_state(TASK_INTERRUPTIBLE);
1965 if (!comedi_is_subdevice_running(s)) {
1967 if (comedi_is_subdevice_in_error(s))
1971 do_become_nonbusy(dev, s);
1979 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1980 m = async->prealloc_bufsz - async->buf_write_ptr;
1981 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1982 if (m > comedi_buf_write_n_allocated(async))
1983 m = comedi_buf_write_n_allocated(async);
1988 if (file->f_flags & O_NONBLOCK) {
1993 if (signal_pending(current)) {
1994 retval = -ERESTARTSYS;
1999 if (s->busy != file) {
2006 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2012 comedi_buf_write_free(async, n);
2018 break; /* makes device work like a pipe */
2020 set_current_state(TASK_RUNNING);
2021 remove_wait_queue(&async->wait_head, &wait);
2023 return count ? count : retval;
2026 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2029 struct comedi_subdevice *s;
2030 struct comedi_async *async;
2031 int n, m, count = 0, retval = 0;
2032 DECLARE_WAITQUEUE(wait, current);
2033 const unsigned minor = iminor(file_inode(file));
2034 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
2035 struct comedi_device *dev = comedi_dev_from_file_info(info);
2040 if (!dev->attached) {
2041 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2045 s = comedi_read_subdevice(info);
2046 if (!s || !s->async)
2050 if (!s->busy || !nbytes)
2052 if (s->busy != file)
2055 add_wait_queue(&async->wait_head, &wait);
2056 while (nbytes > 0 && !retval) {
2057 set_current_state(TASK_INTERRUPTIBLE);
2061 m = comedi_buf_read_n_available(async);
2062 /* printk("%d available\n",m); */
2063 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2064 m = async->prealloc_bufsz - async->buf_read_ptr;
2065 /* printk("%d contiguous\n",m); */
2070 if (!comedi_is_subdevice_running(s)) {
2071 do_become_nonbusy(dev, s);
2072 if (comedi_is_subdevice_in_error(s))
2078 if (file->f_flags & O_NONBLOCK) {
2083 if (signal_pending(current)) {
2084 retval = -ERESTARTSYS;
2091 if (s->busy != file) {
2097 m = copy_to_user(buf, async->prealloc_buf +
2098 async->buf_read_ptr, n);
2104 comedi_buf_read_alloc(async, n);
2105 comedi_buf_read_free(async, n);
2111 break; /* makes device work like a pipe */
2113 if (comedi_is_subdevice_idle(s) &&
2114 async->buf_read_count - async->buf_write_count == 0) {
2115 do_become_nonbusy(dev, s);
2117 set_current_state(TASK_RUNNING);
2118 remove_wait_queue(&async->wait_head, &wait);
2120 return count ? count : retval;
2123 static int comedi_open(struct inode *inode, struct file *file)
2125 const unsigned minor = iminor(inode);
2126 struct comedi_device *dev = comedi_dev_from_minor(minor);
2129 DPRINTK("invalid minor number\n");
2133 /* This is slightly hacky, but we want module autoloading
2135 * case: user opens device, attached -> ok
2136 * case: user opens device, unattached, !in_request_module -> autoload
2137 * case: user opens device, unattached, in_request_module -> fail
2138 * case: root opens device, attached -> ok
2139 * case: root opens device, unattached, in_request_module -> ok
2140 * (typically called from modprobe)
2141 * case: root opens device, unattached, !in_request_module -> autoload
2143 * The last could be changed to "-> ok", which would deny root
2146 mutex_lock(&dev->mutex);
2149 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2150 DPRINTK("in request module\n");
2151 mutex_unlock(&dev->mutex);
2154 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2157 dev->in_request_module = true;
2160 mutex_unlock(&dev->mutex);
2161 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2162 mutex_lock(&dev->mutex);
2165 dev->in_request_module = false;
2167 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2168 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2169 mutex_unlock(&dev->mutex);
2173 __module_get(THIS_MODULE);
2175 if (dev->attached) {
2176 if (!try_module_get(dev->driver->module)) {
2177 module_put(THIS_MODULE);
2178 mutex_unlock(&dev->mutex);
2183 if (dev->attached && dev->use_count == 0 && dev->open) {
2184 int rc = dev->open(dev);
2186 module_put(dev->driver->module);
2187 module_put(THIS_MODULE);
2188 mutex_unlock(&dev->mutex);
2195 mutex_unlock(&dev->mutex);
2200 static int comedi_fasync(int fd, struct file *file, int on)
2202 const unsigned minor = iminor(file_inode(file));
2203 struct comedi_device *dev = comedi_dev_from_minor(minor);
2208 return fasync_helper(fd, file, on, &dev->async_queue);
2211 static int comedi_close(struct inode *inode, struct file *file)
2213 const unsigned minor = iminor(inode);
2214 struct comedi_device *dev = comedi_dev_from_minor(minor);
2215 struct comedi_subdevice *s = NULL;
2221 mutex_lock(&dev->mutex);
2223 if (dev->subdevices) {
2224 for (i = 0; i < dev->n_subdevices; i++) {
2225 s = &dev->subdevices[i];
2227 if (s->busy == file)
2229 if (s->lock == file)
2233 if (dev->attached && dev->use_count == 1 && dev->close)
2236 module_put(THIS_MODULE);
2238 module_put(dev->driver->module);
2242 mutex_unlock(&dev->mutex);
2244 if (file->f_flags & FASYNC)
2245 comedi_fasync(-1, file, 0);
2250 static const struct file_operations comedi_fops = {
2251 .owner = THIS_MODULE,
2252 .unlocked_ioctl = comedi_unlocked_ioctl,
2253 .compat_ioctl = comedi_compat_ioctl,
2254 .open = comedi_open,
2255 .release = comedi_close,
2256 .read = comedi_read,
2257 .write = comedi_write,
2258 .mmap = comedi_mmap,
2259 .poll = comedi_poll,
2260 .fasync = comedi_fasync,
2261 .llseek = noop_llseek,
2264 void comedi_error(const struct comedi_device *dev, const char *s)
2266 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2268 EXPORT_SYMBOL(comedi_error);
2270 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2272 struct comedi_async *async = s->async;
2273 unsigned runflags = 0;
2274 unsigned runflags_mask = 0;
2276 /* DPRINTK("comedi_event 0x%x\n",mask); */
2278 if (!comedi_is_subdevice_running(s))
2282 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2283 COMEDI_CB_OVERFLOW)) {
2284 runflags_mask |= SRF_RUNNING;
2286 /* remember if an error event has occurred, so an error
2287 * can be returned the next time the user does a read() */
2288 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2289 runflags_mask |= SRF_ERROR;
2290 runflags |= SRF_ERROR;
2292 if (runflags_mask) {
2293 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2294 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2297 if (async->cb_mask & s->async->events) {
2298 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2299 wake_up_interruptible(&async->wait_head);
2300 if (s->subdev_flags & SDF_CMD_READ)
2301 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2302 if (s->subdev_flags & SDF_CMD_WRITE)
2303 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2306 async->cb_func(s->async->events, async->cb_arg);
2309 s->async->events = 0;
2311 EXPORT_SYMBOL(comedi_event);
2313 /* Note: the ->mutex is pre-locked on successful return */
2314 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2316 struct comedi_file_info *info;
2317 struct comedi_device *dev;
2318 struct device *csdev;
2321 info = kzalloc(sizeof(*info), GFP_KERNEL);
2323 return ERR_PTR(-ENOMEM);
2324 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2327 return ERR_PTR(-ENOMEM);
2330 info->hardware_device = hardware_device;
2331 comedi_device_init(dev);
2332 comedi_set_hw_dev(dev, hardware_device);
2333 mutex_lock(&dev->mutex);
2334 mutex_lock(&comedi_file_info_table_lock);
2335 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2336 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2337 if (comedi_file_info_table[i] == NULL) {
2338 comedi_file_info_table[i] = info;
2342 mutex_unlock(&comedi_file_info_table_lock);
2343 if (i == COMEDI_NUM_BOARD_MINORS) {
2344 mutex_unlock(&dev->mutex);
2345 comedi_device_cleanup(dev);
2348 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2349 return ERR_PTR(-EBUSY);
2352 csdev = device_create(comedi_class, hardware_device,
2353 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2355 dev->class_dev = csdev;
2356 dev_set_drvdata(csdev, info);
2358 /* Note: dev->mutex needs to be unlocked by the caller. */
2362 static void comedi_free_board_minor(unsigned minor)
2364 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2365 comedi_free_board_file_info(comedi_clear_minor(minor));
2368 void comedi_release_hardware_device(struct device *hardware_device)
2371 struct comedi_file_info *info;
2373 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2375 mutex_lock(&comedi_file_info_table_lock);
2376 info = comedi_file_info_table[minor];
2377 if (info && info->hardware_device == hardware_device) {
2378 comedi_file_info_table[minor] = NULL;
2379 mutex_unlock(&comedi_file_info_table_lock);
2380 comedi_free_board_file_info(info);
2383 mutex_unlock(&comedi_file_info_table_lock);
2387 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2389 struct comedi_device *dev = s->device;
2390 struct comedi_file_info *info;
2391 struct device *csdev;
2394 info = kzalloc(sizeof(*info), GFP_KERNEL);
2398 if (s->subdev_flags & SDF_CMD_READ)
2399 info->read_subdevice = s;
2400 if (s->subdev_flags & SDF_CMD_WRITE)
2401 info->write_subdevice = s;
2402 mutex_lock(&comedi_file_info_table_lock);
2403 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2404 if (comedi_file_info_table[i] == NULL) {
2405 comedi_file_info_table[i] = info;
2409 mutex_unlock(&comedi_file_info_table_lock);
2410 if (i == COMEDI_NUM_MINORS) {
2412 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2416 csdev = device_create(comedi_class, dev->class_dev,
2417 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2418 dev->minor, s->index);
2420 s->class_dev = csdev;
2421 dev_set_drvdata(csdev, info);
2426 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2428 struct comedi_file_info *info;
2435 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2436 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2438 info = comedi_clear_minor(s->minor);
2440 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2441 s->class_dev = NULL;
2446 static void comedi_cleanup_board_minors(void)
2450 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2451 comedi_free_board_minor(i);
2454 static int __init comedi_init(void)
2459 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2461 if (comedi_num_legacy_minors < 0 ||
2462 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2463 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2464 COMEDI_NUM_BOARD_MINORS);
2468 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2469 COMEDI_NUM_MINORS, "comedi");
2472 cdev_init(&comedi_cdev, &comedi_fops);
2473 comedi_cdev.owner = THIS_MODULE;
2474 kobject_set_name(&comedi_cdev.kobj, "comedi");
2475 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2476 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2480 comedi_class = class_create(THIS_MODULE, "comedi");
2481 if (IS_ERR(comedi_class)) {
2482 pr_err("comedi: failed to create class\n");
2483 cdev_del(&comedi_cdev);
2484 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2486 return PTR_ERR(comedi_class);
2489 comedi_class->dev_attrs = comedi_dev_attrs;
2491 /* XXX requires /proc interface */
2494 /* create devices files for legacy/manual use */
2495 for (i = 0; i < comedi_num_legacy_minors; i++) {
2496 struct comedi_device *dev;
2497 dev = comedi_alloc_board_minor(NULL);
2499 comedi_cleanup_board_minors();
2500 cdev_del(&comedi_cdev);
2501 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2503 return PTR_ERR(dev);
2505 /* comedi_alloc_board_minor() locked the mutex */
2506 mutex_unlock(&dev->mutex);
2512 module_init(comedi_init);
2514 static void __exit comedi_cleanup(void)
2518 comedi_cleanup_board_minors();
2519 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2520 BUG_ON(comedi_file_info_table[i]);
2522 class_destroy(comedi_class);
2523 cdev_del(&comedi_cdev);
2524 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2526 comedi_proc_cleanup();
2528 module_exit(comedi_cleanup);
2530 MODULE_AUTHOR("http://www.comedi.org");
2531 MODULE_DESCRIPTION("Comedi core module");
2532 MODULE_LICENSE("GPL");