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 #ifdef CONFIG_COMEDI_DEBUG
54 EXPORT_SYMBOL(comedi_debug);
55 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(comedi_debug,
57 "enable comedi core and driver debugging if non-zero (default 0)"
61 static int comedi_num_legacy_minors;
62 module_param(comedi_num_legacy_minors, int, S_IRUGO);
63 MODULE_PARM_DESC(comedi_num_legacy_minors,
64 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
67 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
68 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
69 MODULE_PARM_DESC(comedi_default_buf_size_kb,
70 "default asynchronous buffer size in KiB (default "
71 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
73 unsigned int comedi_default_buf_maxsize_kb
74 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
75 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
76 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
77 "default maximum size of asynchronous buffer in KiB (default "
78 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
80 struct comedi_file_info {
81 struct comedi_device *device;
82 struct comedi_subdevice *read_subdevice;
83 struct comedi_subdevice *write_subdevice;
84 struct device *hardware_device;
87 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
88 static struct comedi_file_info *comedi_file_info_table[COMEDI_NUM_MINORS];
90 static struct comedi_file_info *comedi_clear_minor(unsigned minor);
91 static void comedi_free_board_file_info(struct comedi_file_info *info);
93 static struct comedi_file_info *comedi_file_info_from_minor(unsigned minor)
95 struct comedi_file_info *info;
97 BUG_ON(minor >= COMEDI_NUM_MINORS);
98 spin_lock(&comedi_file_info_table_lock);
99 info = comedi_file_info_table[minor];
100 spin_unlock(&comedi_file_info_table_lock);
104 static struct comedi_device *
105 comedi_dev_from_file_info(struct comedi_file_info *info)
107 return info ? info->device : NULL;
110 struct comedi_device *comedi_dev_from_minor(unsigned minor)
112 return comedi_dev_from_file_info(comedi_file_info_from_minor(minor));
114 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
116 static struct comedi_subdevice *
117 comedi_read_subdevice(const struct comedi_file_info *info)
119 if (info->read_subdevice)
120 return info->read_subdevice;
122 return info->device->read_subdev;
126 static struct comedi_subdevice *
127 comedi_write_subdevice(const struct comedi_file_info *info)
129 if (info->write_subdevice)
130 return info->write_subdevice;
132 return info->device->write_subdev;
136 static int resize_async_buffer(struct comedi_device *dev,
137 struct comedi_subdevice *s,
138 struct comedi_async *async, unsigned new_size)
142 if (new_size > async->max_bufsize)
146 DPRINTK("subdevice is busy, cannot resize buffer\n");
149 if (async->mmap_count) {
150 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
154 if (!async->prealloc_buf)
157 /* make sure buffer is an integral number of pages
159 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
161 retval = comedi_buf_alloc(dev, s, new_size);
166 retval = s->buf_change(dev, s, new_size);
171 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
172 dev->minor, s->index, async->prealloc_bufsz);
176 /* sysfs attribute files */
178 static ssize_t show_max_read_buffer_kb(struct device *dev,
179 struct device_attribute *attr, char *buf)
181 struct comedi_file_info *info = dev_get_drvdata(dev);
182 struct comedi_subdevice *s = comedi_read_subdevice(info);
183 unsigned int size = 0;
185 mutex_lock(&info->device->mutex);
186 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
187 size = s->async->max_bufsize / 1024;
188 mutex_unlock(&info->device->mutex);
190 return snprintf(buf, PAGE_SIZE, "%i\n", size);
193 static ssize_t store_max_read_buffer_kb(struct device *dev,
194 struct device_attribute *attr,
195 const char *buf, size_t count)
197 struct comedi_file_info *info = dev_get_drvdata(dev);
198 struct comedi_subdevice *s = comedi_read_subdevice(info);
202 err = kstrtouint(buf, 10, &size);
205 if (size > (UINT_MAX / 1024))
209 mutex_lock(&info->device->mutex);
210 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
211 s->async->max_bufsize = size;
214 mutex_unlock(&info->device->mutex);
216 return err ? err : count;
219 static ssize_t show_read_buffer_kb(struct device *dev,
220 struct device_attribute *attr, char *buf)
222 struct comedi_file_info *info = dev_get_drvdata(dev);
223 struct comedi_subdevice *s = comedi_read_subdevice(info);
224 unsigned int size = 0;
226 mutex_lock(&info->device->mutex);
227 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
228 size = s->async->prealloc_bufsz / 1024;
229 mutex_unlock(&info->device->mutex);
231 return snprintf(buf, PAGE_SIZE, "%i\n", size);
234 static ssize_t store_read_buffer_kb(struct device *dev,
235 struct device_attribute *attr,
236 const char *buf, size_t count)
238 struct comedi_file_info *info = dev_get_drvdata(dev);
239 struct comedi_subdevice *s = comedi_read_subdevice(info);
243 err = kstrtouint(buf, 10, &size);
246 if (size > (UINT_MAX / 1024))
250 mutex_lock(&info->device->mutex);
251 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
252 err = resize_async_buffer(info->device, s, s->async, size);
255 mutex_unlock(&info->device->mutex);
257 return err ? err : count;
260 static ssize_t show_max_write_buffer_kb(struct device *dev,
261 struct device_attribute *attr,
264 struct comedi_file_info *info = dev_get_drvdata(dev);
265 struct comedi_subdevice *s = comedi_write_subdevice(info);
266 unsigned int size = 0;
268 mutex_lock(&info->device->mutex);
269 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
270 size = s->async->max_bufsize / 1024;
271 mutex_unlock(&info->device->mutex);
273 return snprintf(buf, PAGE_SIZE, "%i\n", size);
276 static ssize_t store_max_write_buffer_kb(struct device *dev,
277 struct device_attribute *attr,
278 const char *buf, size_t count)
280 struct comedi_file_info *info = dev_get_drvdata(dev);
281 struct comedi_subdevice *s = comedi_write_subdevice(info);
285 err = kstrtouint(buf, 10, &size);
288 if (size > (UINT_MAX / 1024))
292 mutex_lock(&info->device->mutex);
293 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
294 s->async->max_bufsize = size;
297 mutex_unlock(&info->device->mutex);
299 return err ? err : count;
302 static ssize_t show_write_buffer_kb(struct device *dev,
303 struct device_attribute *attr, char *buf)
305 struct comedi_file_info *info = dev_get_drvdata(dev);
306 struct comedi_subdevice *s = comedi_write_subdevice(info);
307 unsigned int size = 0;
309 mutex_lock(&info->device->mutex);
310 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
311 size = s->async->prealloc_bufsz / 1024;
312 mutex_unlock(&info->device->mutex);
314 return snprintf(buf, PAGE_SIZE, "%i\n", size);
317 static ssize_t store_write_buffer_kb(struct device *dev,
318 struct device_attribute *attr,
319 const char *buf, size_t count)
321 struct comedi_file_info *info = dev_get_drvdata(dev);
322 struct comedi_subdevice *s = comedi_write_subdevice(info);
326 err = kstrtouint(buf, 10, &size);
329 if (size > (UINT_MAX / 1024))
333 mutex_lock(&info->device->mutex);
334 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
335 err = resize_async_buffer(info->device, s, s->async, size);
338 mutex_unlock(&info->device->mutex);
340 return err ? err : count;
343 static struct device_attribute comedi_dev_attrs[] = {
344 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
345 show_max_read_buffer_kb, store_max_read_buffer_kb),
346 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
347 show_read_buffer_kb, store_read_buffer_kb),
348 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
349 show_max_write_buffer_kb, store_max_write_buffer_kb),
350 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
351 show_write_buffer_kb, store_write_buffer_kb),
355 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
356 unsigned mask, unsigned bits)
360 spin_lock_irqsave(&s->spin_lock, flags);
361 s->runflags &= ~mask;
362 s->runflags |= (bits & mask);
363 spin_unlock_irqrestore(&s->spin_lock, flags);
366 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
371 spin_lock_irqsave(&s->spin_lock, flags);
372 runflags = s->runflags;
373 spin_unlock_irqrestore(&s->spin_lock, flags);
377 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
379 unsigned runflags = comedi_get_subdevice_runflags(s);
381 return (runflags & SRF_RUNNING) ? true : false;
383 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
385 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
387 unsigned runflags = comedi_get_subdevice_runflags(s);
389 return (runflags & SRF_ERROR) ? true : false;
392 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
394 unsigned runflags = comedi_get_subdevice_runflags(s);
396 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
400 This function restores a subdevice to an idle state.
402 static void do_become_nonbusy(struct comedi_device *dev,
403 struct comedi_subdevice *s)
405 struct comedi_async *async = s->async;
407 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
409 comedi_buf_reset(async);
410 async->inttrig = NULL;
411 kfree(async->cmd.chanlist);
412 async->cmd.chanlist = NULL;
414 dev_err(dev->class_dev,
415 "BUG: (?) do_become_nonbusy called with async=NULL\n");
421 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
425 if (comedi_is_subdevice_running(s) && s->cancel)
426 ret = s->cancel(dev, s);
428 do_become_nonbusy(dev, s);
433 static int is_device_busy(struct comedi_device *dev)
435 struct comedi_subdevice *s;
441 for (i = 0; i < dev->n_subdevices; i++) {
442 s = &dev->subdevices[i];
445 if (s->async && s->async->mmap_count)
457 pointer to devconfig structure
460 devconfig structure at arg
465 static int do_devconfig_ioctl(struct comedi_device *dev,
466 struct comedi_devconfig __user *arg)
468 struct comedi_devconfig it;
471 if (!capable(CAP_SYS_ADMIN))
475 if (is_device_busy(dev))
478 struct module *driver_module = dev->driver->module;
479 comedi_device_detach(dev);
480 module_put(driver_module);
485 if (copy_from_user(&it, arg, sizeof(it)))
488 it.board_name[COMEDI_NAMELEN - 1] = 0;
490 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
491 dev_warn(dev->class_dev,
492 "comedi_config --init_data is deprecated\n");
496 if (dev->minor >= comedi_num_legacy_minors)
497 /* don't re-use dynamically allocated comedi devices */
500 ret = comedi_device_attach(dev, &it);
502 if (!try_module_get(dev->driver->module)) {
503 comedi_device_detach(dev);
513 buffer configuration ioctl
516 pointer to bufconfig structure
522 modified bufconfig at arg
525 static int do_bufconfig_ioctl(struct comedi_device *dev,
526 struct comedi_bufconfig __user *arg)
528 struct comedi_bufconfig bc;
529 struct comedi_async *async;
530 struct comedi_subdevice *s;
533 if (copy_from_user(&bc, arg, sizeof(bc)))
536 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
539 s = &dev->subdevices[bc.subdevice];
543 DPRINTK("subdevice does not have async capability\n");
549 if (bc.maximum_size) {
550 if (!capable(CAP_SYS_ADMIN))
553 async->max_bufsize = bc.maximum_size;
557 retval = resize_async_buffer(dev, s, async, bc.size);
562 bc.size = async->prealloc_bufsz;
563 bc.maximum_size = async->max_bufsize;
566 if (copy_to_user(arg, &bc, sizeof(bc)))
577 pointer to devinfo structure
586 static int do_devinfo_ioctl(struct comedi_device *dev,
587 struct comedi_devinfo __user *arg,
590 const unsigned minor = iminor(file_inode(file));
591 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
592 struct comedi_subdevice *s;
593 struct comedi_devinfo devinfo;
595 memset(&devinfo, 0, sizeof(devinfo));
597 /* fill devinfo structure */
598 devinfo.version_code = COMEDI_VERSION_CODE;
599 devinfo.n_subdevs = dev->n_subdevices;
600 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
601 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
603 s = comedi_read_subdevice(info);
605 devinfo.read_subdevice = s->index;
607 devinfo.read_subdevice = -1;
609 s = comedi_write_subdevice(info);
611 devinfo.write_subdevice = s->index;
613 devinfo.write_subdevice = -1;
615 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
626 pointer to array of subdevice info structures
632 array of subdevice info structures at arg
635 static int do_subdinfo_ioctl(struct comedi_device *dev,
636 struct comedi_subdinfo __user *arg, void *file)
639 struct comedi_subdinfo *tmp, *us;
640 struct comedi_subdevice *s;
642 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
646 /* fill subdinfo structs */
647 for (i = 0; i < dev->n_subdevices; i++) {
648 s = &dev->subdevices[i];
652 us->n_chan = s->n_chan;
653 us->subd_flags = s->subdev_flags;
654 if (comedi_is_subdevice_running(s))
655 us->subd_flags |= SDF_RUNNING;
656 #define TIMER_nanosec 5 /* backwards compatibility */
657 us->timer_type = TIMER_nanosec;
658 us->len_chanlist = s->len_chanlist;
659 us->maxdata = s->maxdata;
660 if (s->range_table) {
662 (i << 24) | (0 << 16) | (s->range_table->length);
664 us->range_type = 0; /* XXX */
666 us->flags = s->flags;
669 us->subd_flags |= SDF_BUSY;
671 us->subd_flags |= SDF_BUSY_OWNER;
673 us->subd_flags |= SDF_LOCKED;
675 us->subd_flags |= SDF_LOCK_OWNER;
676 if (!s->maxdata && s->maxdata_list)
677 us->subd_flags |= SDF_MAXDATA;
679 us->subd_flags |= SDF_FLAGS;
680 if (s->range_table_list)
681 us->subd_flags |= SDF_RANGETYPE;
683 us->subd_flags |= SDF_CMD;
685 if (s->insn_bits != &insn_inval)
686 us->insn_bits_support = COMEDI_SUPPORTED;
688 us->insn_bits_support = COMEDI_UNSUPPORTED;
690 us->settling_time_0 = s->settling_time_0;
693 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
697 return ret ? -EFAULT : 0;
705 pointer to chaninfo structure
708 chaninfo structure at arg
711 arrays at elements of chaninfo structure
714 static int do_chaninfo_ioctl(struct comedi_device *dev,
715 struct comedi_chaninfo __user *arg)
717 struct comedi_subdevice *s;
718 struct comedi_chaninfo it;
720 if (copy_from_user(&it, arg, sizeof(it)))
723 if (it.subdev >= dev->n_subdevices)
725 s = &dev->subdevices[it.subdev];
727 if (it.maxdata_list) {
728 if (s->maxdata || !s->maxdata_list)
730 if (copy_to_user(it.maxdata_list, s->maxdata_list,
731 s->n_chan * sizeof(unsigned int)))
738 if (copy_to_user(it.flaglist, s->flaglist,
739 s->n_chan * sizeof(unsigned int)))
746 if (!s->range_table_list)
748 for (i = 0; i < s->n_chan; i++) {
751 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
752 (s->range_table_list[i]->length);
753 if (put_user(x, it.rangelist + i))
757 if (copy_to_user(it.rangelist, s->range_type_list,
758 s->n_chan * sizeof(unsigned int)))
768 buffer information ioctl
771 pointer to bufinfo structure
777 modified bufinfo at arg
780 static int do_bufinfo_ioctl(struct comedi_device *dev,
781 struct comedi_bufinfo __user *arg, void *file)
783 struct comedi_bufinfo bi;
784 struct comedi_subdevice *s;
785 struct comedi_async *async;
787 if (copy_from_user(&bi, arg, sizeof(bi)))
790 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
793 s = &dev->subdevices[bi.subdevice];
795 if (s->lock && s->lock != file)
801 DPRINTK("subdevice does not have async capability\n");
802 bi.buf_write_ptr = 0;
804 bi.buf_write_count = 0;
805 bi.buf_read_count = 0;
807 bi.bytes_written = 0;
812 bi.bytes_written = 0;
813 goto copyback_position;
818 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
819 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
820 comedi_buf_read_free(async, bi.bytes_read);
822 if (comedi_is_subdevice_idle(s) &&
823 async->buf_write_count == async->buf_read_count) {
824 do_become_nonbusy(dev, s);
828 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
830 comedi_buf_write_alloc(async, bi.bytes_written);
831 comedi_buf_write_free(async, bi.bytes_written);
835 bi.buf_write_count = async->buf_write_count;
836 bi.buf_write_ptr = async->buf_write_ptr;
837 bi.buf_read_count = async->buf_read_count;
838 bi.buf_read_ptr = async->buf_read_ptr;
841 if (copy_to_user(arg, &bi, sizeof(bi)))
847 static int check_insn_config_length(struct comedi_insn *insn,
854 case INSN_CONFIG_DIO_OUTPUT:
855 case INSN_CONFIG_DIO_INPUT:
856 case INSN_CONFIG_DISARM:
857 case INSN_CONFIG_RESET:
861 case INSN_CONFIG_ARM:
862 case INSN_CONFIG_DIO_QUERY:
863 case INSN_CONFIG_BLOCK_SIZE:
864 case INSN_CONFIG_FILTER:
865 case INSN_CONFIG_SERIAL_CLOCK:
866 case INSN_CONFIG_BIDIRECTIONAL_DATA:
867 case INSN_CONFIG_ALT_SOURCE:
868 case INSN_CONFIG_SET_COUNTER_MODE:
869 case INSN_CONFIG_8254_READ_STATUS:
870 case INSN_CONFIG_SET_ROUTING:
871 case INSN_CONFIG_GET_ROUTING:
872 case INSN_CONFIG_GET_PWM_STATUS:
873 case INSN_CONFIG_PWM_SET_PERIOD:
874 case INSN_CONFIG_PWM_GET_PERIOD:
878 case INSN_CONFIG_SET_GATE_SRC:
879 case INSN_CONFIG_GET_GATE_SRC:
880 case INSN_CONFIG_SET_CLOCK_SRC:
881 case INSN_CONFIG_GET_CLOCK_SRC:
882 case INSN_CONFIG_SET_OTHER_SRC:
883 case INSN_CONFIG_GET_COUNTER_STATUS:
884 case INSN_CONFIG_PWM_SET_H_BRIDGE:
885 case INSN_CONFIG_PWM_GET_H_BRIDGE:
886 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
890 case INSN_CONFIG_PWM_OUTPUT:
891 case INSN_CONFIG_ANALOG_TRIG:
895 case INSN_CONFIG_DIGITAL_TRIG:
899 /* by default we allow the insn since we don't have checks for
900 * all possible cases yet */
902 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
904 pr_warn("comedi: Add a check to %s in %s.\n",
906 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
912 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
913 unsigned int *data, void *file)
915 struct comedi_subdevice *s;
919 if (insn->insn & INSN_MASK_SPECIAL) {
920 /* a non-subdevice instruction */
922 switch (insn->insn) {
932 do_gettimeofday(&tv);
934 data[1] = tv.tv_usec;
940 if (insn->n != 1 || data[0] >= 100000) {
944 udelay(data[0] / 1000);
952 if (insn->subdev >= dev->n_subdevices) {
953 DPRINTK("%d not usable subdevice\n",
958 s = &dev->subdevices[insn->subdev];
960 DPRINTK("no async\n");
964 if (!s->async->inttrig) {
965 DPRINTK("no inttrig\n");
969 ret = s->async->inttrig(dev, s, data[0]);
974 DPRINTK("invalid insn\n");
979 /* a subdevice instruction */
980 unsigned int maxdata;
982 if (insn->subdev >= dev->n_subdevices) {
983 DPRINTK("subdevice %d out of range\n", insn->subdev);
987 s = &dev->subdevices[insn->subdev];
989 if (s->type == COMEDI_SUBD_UNUSED) {
990 DPRINTK("%d not usable subdevice\n", insn->subdev);
995 /* are we locked? (ioctl lock) */
996 if (s->lock && s->lock != file) {
997 DPRINTK("device locked\n");
1002 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1005 DPRINTK("bad chanspec\n");
1013 /* This looks arbitrary. It is. */
1014 s->busy = &parse_insn;
1015 switch (insn->insn) {
1017 ret = s->insn_read(dev, s, insn, data);
1020 maxdata = s->maxdata_list
1021 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1023 for (i = 0; i < insn->n; ++i) {
1024 if (data[i] > maxdata) {
1026 DPRINTK("bad data value(s)\n");
1031 ret = s->insn_write(dev, s, insn, data);
1037 /* Most drivers ignore the base channel in
1038 * insn->chanspec. Fix this here if
1039 * the subdevice has <= 32 channels. */
1041 unsigned int orig_mask;
1043 orig_mask = data[0];
1044 if (s->n_chan <= 32) {
1045 shift = CR_CHAN(insn->chanspec);
1053 ret = s->insn_bits(dev, s, insn, data);
1054 data[0] = orig_mask;
1060 ret = check_insn_config_length(insn, data);
1063 ret = s->insn_config(dev, s, insn, data);
1079 * synchronous instructions
1082 * pointer to sync cmd structure
1085 * sync cmd struct at arg
1092 /* arbitrary limits */
1093 #define MAX_SAMPLES 256
1094 static int do_insnlist_ioctl(struct comedi_device *dev,
1095 struct comedi_insnlist __user *arg, void *file)
1097 struct comedi_insnlist insnlist;
1098 struct comedi_insn *insns = NULL;
1099 unsigned int *data = NULL;
1103 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1106 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1108 DPRINTK("kmalloc failed\n");
1113 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1115 DPRINTK("kmalloc failed\n");
1120 if (copy_from_user(insns, insnlist.insns,
1121 sizeof(*insns) * insnlist.n_insns)) {
1122 DPRINTK("copy_from_user failed\n");
1127 for (i = 0; i < insnlist.n_insns; i++) {
1128 if (insns[i].n > MAX_SAMPLES) {
1129 DPRINTK("number of samples too large\n");
1133 if (insns[i].insn & INSN_MASK_WRITE) {
1134 if (copy_from_user(data, insns[i].data,
1135 insns[i].n * sizeof(unsigned int))) {
1136 DPRINTK("copy_from_user failed\n");
1141 ret = parse_insn(dev, insns + i, data, file);
1144 if (insns[i].insn & INSN_MASK_READ) {
1145 if (copy_to_user(insns[i].data, data,
1146 insns[i].n * sizeof(unsigned int))) {
1147 DPRINTK("copy_to_user failed\n");
1167 * synchronous instructions
1173 * struct comedi_insn struct at arg
1179 static int do_insn_ioctl(struct comedi_device *dev,
1180 struct comedi_insn __user *arg, void *file)
1182 struct comedi_insn insn;
1183 unsigned int *data = NULL;
1186 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1192 if (copy_from_user(&insn, arg, sizeof(insn))) {
1197 /* This is where the behavior of insn and insnlist deviate. */
1198 if (insn.n > MAX_SAMPLES)
1199 insn.n = MAX_SAMPLES;
1200 if (insn.insn & INSN_MASK_WRITE) {
1201 if (copy_from_user(data,
1203 insn.n * sizeof(unsigned int))) {
1208 ret = parse_insn(dev, &insn, data, file);
1211 if (insn.insn & INSN_MASK_READ) {
1212 if (copy_to_user(insn.data,
1214 insn.n * sizeof(unsigned int))) {
1227 static int do_cmd_ioctl(struct comedi_device *dev,
1228 struct comedi_cmd __user *arg, void *file)
1230 struct comedi_cmd cmd;
1231 struct comedi_subdevice *s;
1232 struct comedi_async *async;
1234 unsigned int __user *user_chanlist;
1236 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1237 DPRINTK("bad cmd address\n");
1240 /* save user's chanlist pointer so it can be restored later */
1241 user_chanlist = (unsigned int __user *)cmd.chanlist;
1243 if (cmd.subdev >= dev->n_subdevices) {
1244 DPRINTK("%d no such subdevice\n", cmd.subdev);
1248 s = &dev->subdevices[cmd.subdev];
1251 if (s->type == COMEDI_SUBD_UNUSED) {
1252 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1256 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1257 DPRINTK("subdevice %i does not support commands\n",
1262 /* are we locked? (ioctl lock) */
1263 if (s->lock && s->lock != file) {
1264 DPRINTK("subdevice locked\n");
1270 DPRINTK("subdevice busy\n");
1275 /* make sure channel/gain list isn't too long */
1276 if (cmd.chanlist_len > s->len_chanlist) {
1277 DPRINTK("channel/gain list too long %u > %d\n",
1278 cmd.chanlist_len, s->len_chanlist);
1283 /* make sure channel/gain list isn't too short */
1284 if (cmd.chanlist_len < 1) {
1285 DPRINTK("channel/gain list too short %u < 1\n",
1292 async->cmd.data = NULL;
1293 /* load channel/gain list */
1294 async->cmd.chanlist =
1295 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1296 if (!async->cmd.chanlist) {
1297 DPRINTK("allocation failed\n");
1302 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1303 async->cmd.chanlist_len * sizeof(int))) {
1304 DPRINTK("fault reading chanlist\n");
1309 /* make sure each element in channel/gain list is valid */
1310 ret = comedi_check_chanlist(s,
1311 async->cmd.chanlist_len,
1312 async->cmd.chanlist);
1314 DPRINTK("bad chanlist\n");
1318 ret = s->do_cmdtest(dev, s, &async->cmd);
1320 if (async->cmd.flags & TRIG_BOGUS || ret) {
1321 DPRINTK("test returned %d\n", ret);
1323 /* restore chanlist pointer before copying back */
1324 cmd.chanlist = (unsigned int __force *)user_chanlist;
1326 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1327 DPRINTK("fault writing cmd\n");
1335 if (!async->prealloc_bufsz) {
1337 DPRINTK("no buffer (?)\n");
1341 comedi_buf_reset(async);
1344 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1346 if (async->cmd.flags & TRIG_WAKE_EOS)
1347 async->cb_mask |= COMEDI_CB_EOS;
1349 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1351 ret = s->do_cmd(dev, s);
1356 do_become_nonbusy(dev, s);
1363 command testing ioctl
1366 pointer to cmd structure
1369 cmd structure at arg
1373 modified cmd structure at arg
1376 static int do_cmdtest_ioctl(struct comedi_device *dev,
1377 struct comedi_cmd __user *arg, void *file)
1379 struct comedi_cmd cmd;
1380 struct comedi_subdevice *s;
1382 unsigned int *chanlist = NULL;
1383 unsigned int __user *user_chanlist;
1385 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1386 DPRINTK("bad cmd address\n");
1389 /* save user's chanlist pointer so it can be restored later */
1390 user_chanlist = (unsigned int __user *)cmd.chanlist;
1392 if (cmd.subdev >= dev->n_subdevices) {
1393 DPRINTK("%d no such subdevice\n", cmd.subdev);
1397 s = &dev->subdevices[cmd.subdev];
1398 if (s->type == COMEDI_SUBD_UNUSED) {
1399 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1403 if (!s->do_cmd || !s->do_cmdtest) {
1404 DPRINTK("subdevice %i does not support commands\n",
1409 /* make sure channel/gain list isn't too long */
1410 if (cmd.chanlist_len > s->len_chanlist) {
1411 DPRINTK("channel/gain list too long %d > %d\n",
1412 cmd.chanlist_len, s->len_chanlist);
1417 /* load channel/gain list */
1420 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1422 DPRINTK("allocation failed\n");
1427 if (copy_from_user(chanlist, user_chanlist,
1428 cmd.chanlist_len * sizeof(int))) {
1429 DPRINTK("fault reading chanlist\n");
1434 /* make sure each element in channel/gain list is valid */
1435 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1437 DPRINTK("bad chanlist\n");
1441 cmd.chanlist = chanlist;
1444 ret = s->do_cmdtest(dev, s, &cmd);
1446 /* restore chanlist pointer before copying back */
1447 cmd.chanlist = (unsigned int __force *)user_chanlist;
1449 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1450 DPRINTK("bad cmd address\n");
1475 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1479 unsigned long flags;
1480 struct comedi_subdevice *s;
1482 if (arg >= dev->n_subdevices)
1484 s = &dev->subdevices[arg];
1486 spin_lock_irqsave(&s->spin_lock, flags);
1487 if (s->busy || s->lock)
1491 spin_unlock_irqrestore(&s->spin_lock, flags);
1498 ret = s->lock_f(dev, s);
1517 This function isn't protected by the semaphore, since
1518 we already own the lock.
1520 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1523 struct comedi_subdevice *s;
1525 if (arg >= dev->n_subdevices)
1527 s = &dev->subdevices[arg];
1532 if (s->lock && s->lock != file)
1535 if (s->lock == file) {
1549 cancel acquisition ioctl
1561 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1564 struct comedi_subdevice *s;
1566 if (arg >= dev->n_subdevices)
1568 s = &dev->subdevices[arg];
1569 if (s->async == NULL)
1572 if (s->lock && s->lock != file)
1578 if (s->busy != file)
1581 return do_cancel(dev, s);
1586 instructs driver to synchronize buffers
1598 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1601 struct comedi_subdevice *s;
1603 if (arg >= dev->n_subdevices)
1605 s = &dev->subdevices[arg];
1607 if (s->lock && s->lock != file)
1613 if (s->busy != file)
1617 return s->poll(dev, s);
1622 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1625 const unsigned minor = iminor(file_inode(file));
1626 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1627 struct comedi_device *dev = comedi_dev_from_file_info(info);
1633 mutex_lock(&dev->mutex);
1635 /* Device config is special, because it must work on
1636 * an unconfigured device. */
1637 if (cmd == COMEDI_DEVCONFIG) {
1638 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1639 /* Device config not appropriate on non-board minors. */
1643 rc = do_devconfig_ioctl(dev,
1644 (struct comedi_devconfig __user *)arg);
1647 dev->minor >= comedi_num_legacy_minors) {
1648 /* Successfully unconfigured a dynamically
1649 * allocated device. Try and remove it. */
1650 info = comedi_clear_minor(dev->minor);
1652 mutex_unlock(&dev->mutex);
1653 comedi_free_board_file_info(info);
1661 if (!dev->attached) {
1662 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1668 case COMEDI_BUFCONFIG:
1669 rc = do_bufconfig_ioctl(dev,
1670 (struct comedi_bufconfig __user *)arg);
1672 case COMEDI_DEVINFO:
1673 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1676 case COMEDI_SUBDINFO:
1677 rc = do_subdinfo_ioctl(dev,
1678 (struct comedi_subdinfo __user *)arg,
1681 case COMEDI_CHANINFO:
1682 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1684 case COMEDI_RANGEINFO:
1685 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1687 case COMEDI_BUFINFO:
1688 rc = do_bufinfo_ioctl(dev,
1689 (struct comedi_bufinfo __user *)arg,
1693 rc = do_lock_ioctl(dev, arg, file);
1696 rc = do_unlock_ioctl(dev, arg, file);
1699 rc = do_cancel_ioctl(dev, arg, file);
1702 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1704 case COMEDI_CMDTEST:
1705 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1708 case COMEDI_INSNLIST:
1709 rc = do_insnlist_ioctl(dev,
1710 (struct comedi_insnlist __user *)arg,
1714 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1718 rc = do_poll_ioctl(dev, arg, file);
1726 mutex_unlock(&dev->mutex);
1730 static void comedi_vm_open(struct vm_area_struct *area)
1732 struct comedi_async *async;
1733 struct comedi_device *dev;
1735 async = area->vm_private_data;
1736 dev = async->subdevice->device;
1738 mutex_lock(&dev->mutex);
1739 async->mmap_count++;
1740 mutex_unlock(&dev->mutex);
1743 static void comedi_vm_close(struct vm_area_struct *area)
1745 struct comedi_async *async;
1746 struct comedi_device *dev;
1748 async = area->vm_private_data;
1749 dev = async->subdevice->device;
1751 mutex_lock(&dev->mutex);
1752 async->mmap_count--;
1753 mutex_unlock(&dev->mutex);
1756 static struct vm_operations_struct comedi_vm_ops = {
1757 .open = comedi_vm_open,
1758 .close = comedi_vm_close,
1761 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1763 const unsigned minor = iminor(file_inode(file));
1764 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1765 struct comedi_device *dev = comedi_dev_from_file_info(info);
1766 struct comedi_subdevice *s;
1767 struct comedi_async *async;
1768 unsigned long start = vma->vm_start;
1777 mutex_lock(&dev->mutex);
1779 if (!dev->attached) {
1780 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1785 if (vma->vm_flags & VM_WRITE)
1786 s = comedi_write_subdevice(info);
1788 s = comedi_read_subdevice(info);
1800 if (vma->vm_pgoff != 0) {
1801 DPRINTK("comedi: mmap() offset must be 0.\n");
1806 size = vma->vm_end - vma->vm_start;
1807 if (size > async->prealloc_bufsz) {
1811 if (size & (~PAGE_MASK)) {
1816 n_pages = size >> PAGE_SHIFT;
1817 for (i = 0; i < n_pages; ++i) {
1818 struct comedi_buf_page *buf = &async->buf_page_list[i];
1820 if (remap_pfn_range(vma, start,
1821 page_to_pfn(virt_to_page(buf->virt_addr)),
1822 PAGE_SIZE, PAGE_SHARED)) {
1829 vma->vm_ops = &comedi_vm_ops;
1830 vma->vm_private_data = async;
1832 async->mmap_count++;
1836 mutex_unlock(&dev->mutex);
1840 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1842 unsigned int mask = 0;
1843 const unsigned minor = iminor(file_inode(file));
1844 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1845 struct comedi_device *dev = comedi_dev_from_file_info(info);
1846 struct comedi_subdevice *s;
1851 mutex_lock(&dev->mutex);
1853 if (!dev->attached) {
1854 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1858 s = comedi_read_subdevice(info);
1859 if (s && s->async) {
1860 poll_wait(file, &s->async->wait_head, wait);
1861 if (!s->busy || !comedi_is_subdevice_running(s) ||
1862 comedi_buf_read_n_available(s->async) > 0)
1863 mask |= POLLIN | POLLRDNORM;
1866 s = comedi_write_subdevice(info);
1867 if (s && s->async) {
1868 unsigned int bps = bytes_per_sample(s->async->subdevice);
1870 poll_wait(file, &s->async->wait_head, wait);
1871 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1872 if (!s->busy || !comedi_is_subdevice_running(s) ||
1873 comedi_buf_write_n_allocated(s->async) >= bps)
1874 mask |= POLLOUT | POLLWRNORM;
1878 mutex_unlock(&dev->mutex);
1882 static ssize_t comedi_write(struct file *file, const char __user *buf,
1883 size_t nbytes, loff_t *offset)
1885 struct comedi_subdevice *s;
1886 struct comedi_async *async;
1887 int n, m, count = 0, retval = 0;
1888 DECLARE_WAITQUEUE(wait, current);
1889 const unsigned minor = iminor(file_inode(file));
1890 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1891 struct comedi_device *dev = comedi_dev_from_file_info(info);
1896 if (!dev->attached) {
1897 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1901 s = comedi_write_subdevice(info);
1902 if (!s || !s->async)
1907 if (!s->busy || !nbytes)
1909 if (s->busy != file)
1912 add_wait_queue(&async->wait_head, &wait);
1913 while (nbytes > 0 && !retval) {
1914 set_current_state(TASK_INTERRUPTIBLE);
1916 if (!comedi_is_subdevice_running(s)) {
1918 if (comedi_is_subdevice_in_error(s))
1922 do_become_nonbusy(dev, s);
1930 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1931 m = async->prealloc_bufsz - async->buf_write_ptr;
1932 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1933 if (m > comedi_buf_write_n_allocated(async))
1934 m = comedi_buf_write_n_allocated(async);
1939 if (file->f_flags & O_NONBLOCK) {
1944 if (signal_pending(current)) {
1945 retval = -ERESTARTSYS;
1950 if (s->busy != file) {
1957 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1963 comedi_buf_write_free(async, n);
1969 break; /* makes device work like a pipe */
1971 set_current_state(TASK_RUNNING);
1972 remove_wait_queue(&async->wait_head, &wait);
1974 return count ? count : retval;
1977 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
1980 struct comedi_subdevice *s;
1981 struct comedi_async *async;
1982 int n, m, count = 0, retval = 0;
1983 DECLARE_WAITQUEUE(wait, current);
1984 const unsigned minor = iminor(file_inode(file));
1985 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1986 struct comedi_device *dev = comedi_dev_from_file_info(info);
1991 if (!dev->attached) {
1992 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1996 s = comedi_read_subdevice(info);
1997 if (!s || !s->async)
2001 if (!s->busy || !nbytes)
2003 if (s->busy != file)
2006 add_wait_queue(&async->wait_head, &wait);
2007 while (nbytes > 0 && !retval) {
2008 set_current_state(TASK_INTERRUPTIBLE);
2012 m = comedi_buf_read_n_available(async);
2013 /* printk("%d available\n",m); */
2014 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2015 m = async->prealloc_bufsz - async->buf_read_ptr;
2016 /* printk("%d contiguous\n",m); */
2021 if (!comedi_is_subdevice_running(s)) {
2022 do_become_nonbusy(dev, s);
2023 if (comedi_is_subdevice_in_error(s))
2029 if (file->f_flags & O_NONBLOCK) {
2034 if (signal_pending(current)) {
2035 retval = -ERESTARTSYS;
2042 if (s->busy != file) {
2048 m = copy_to_user(buf, async->prealloc_buf +
2049 async->buf_read_ptr, n);
2055 comedi_buf_read_alloc(async, n);
2056 comedi_buf_read_free(async, n);
2062 break; /* makes device work like a pipe */
2064 if (comedi_is_subdevice_idle(s) &&
2065 async->buf_read_count - async->buf_write_count == 0) {
2066 do_become_nonbusy(dev, s);
2068 set_current_state(TASK_RUNNING);
2069 remove_wait_queue(&async->wait_head, &wait);
2071 return count ? count : retval;
2074 static int comedi_open(struct inode *inode, struct file *file)
2076 const unsigned minor = iminor(inode);
2077 struct comedi_device *dev = comedi_dev_from_minor(minor);
2080 DPRINTK("invalid minor number\n");
2084 /* This is slightly hacky, but we want module autoloading
2086 * case: user opens device, attached -> ok
2087 * case: user opens device, unattached, !in_request_module -> autoload
2088 * case: user opens device, unattached, in_request_module -> fail
2089 * case: root opens device, attached -> ok
2090 * case: root opens device, unattached, in_request_module -> ok
2091 * (typically called from modprobe)
2092 * case: root opens device, unattached, !in_request_module -> autoload
2094 * The last could be changed to "-> ok", which would deny root
2097 mutex_lock(&dev->mutex);
2100 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2101 DPRINTK("in request module\n");
2102 mutex_unlock(&dev->mutex);
2105 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2108 dev->in_request_module = true;
2111 mutex_unlock(&dev->mutex);
2112 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2113 mutex_lock(&dev->mutex);
2116 dev->in_request_module = false;
2118 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2119 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2120 mutex_unlock(&dev->mutex);
2124 __module_get(THIS_MODULE);
2126 if (dev->attached) {
2127 if (!try_module_get(dev->driver->module)) {
2128 module_put(THIS_MODULE);
2129 mutex_unlock(&dev->mutex);
2134 if (dev->attached && dev->use_count == 0 && dev->open) {
2135 int rc = dev->open(dev);
2137 module_put(dev->driver->module);
2138 module_put(THIS_MODULE);
2139 mutex_unlock(&dev->mutex);
2146 mutex_unlock(&dev->mutex);
2151 static int comedi_fasync(int fd, struct file *file, int on)
2153 const unsigned minor = iminor(file_inode(file));
2154 struct comedi_device *dev = comedi_dev_from_minor(minor);
2159 return fasync_helper(fd, file, on, &dev->async_queue);
2162 static int comedi_close(struct inode *inode, struct file *file)
2164 const unsigned minor = iminor(inode);
2165 struct comedi_device *dev = comedi_dev_from_minor(minor);
2166 struct comedi_subdevice *s = NULL;
2172 mutex_lock(&dev->mutex);
2174 if (dev->subdevices) {
2175 for (i = 0; i < dev->n_subdevices; i++) {
2176 s = &dev->subdevices[i];
2178 if (s->busy == file)
2180 if (s->lock == file)
2184 if (dev->attached && dev->use_count == 1 && dev->close)
2187 module_put(THIS_MODULE);
2189 module_put(dev->driver->module);
2193 mutex_unlock(&dev->mutex);
2195 if (file->f_flags & FASYNC)
2196 comedi_fasync(-1, file, 0);
2201 static const struct file_operations comedi_fops = {
2202 .owner = THIS_MODULE,
2203 .unlocked_ioctl = comedi_unlocked_ioctl,
2204 .compat_ioctl = comedi_compat_ioctl,
2205 .open = comedi_open,
2206 .release = comedi_close,
2207 .read = comedi_read,
2208 .write = comedi_write,
2209 .mmap = comedi_mmap,
2210 .poll = comedi_poll,
2211 .fasync = comedi_fasync,
2212 .llseek = noop_llseek,
2215 static struct class *comedi_class;
2216 static struct cdev comedi_cdev;
2218 void comedi_error(const struct comedi_device *dev, const char *s)
2220 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2222 EXPORT_SYMBOL(comedi_error);
2224 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2226 struct comedi_async *async = s->async;
2227 unsigned runflags = 0;
2228 unsigned runflags_mask = 0;
2230 /* DPRINTK("comedi_event 0x%x\n",mask); */
2232 if (!comedi_is_subdevice_running(s))
2236 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2237 COMEDI_CB_OVERFLOW)) {
2238 runflags_mask |= SRF_RUNNING;
2240 /* remember if an error event has occurred, so an error
2241 * can be returned the next time the user does a read() */
2242 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2243 runflags_mask |= SRF_ERROR;
2244 runflags |= SRF_ERROR;
2246 if (runflags_mask) {
2247 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2248 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2251 if (async->cb_mask & s->async->events) {
2252 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2253 wake_up_interruptible(&async->wait_head);
2254 if (s->subdev_flags & SDF_CMD_READ)
2255 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2256 if (s->subdev_flags & SDF_CMD_WRITE)
2257 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2260 async->cb_func(s->async->events, async->cb_arg);
2263 s->async->events = 0;
2265 EXPORT_SYMBOL(comedi_event);
2267 static void comedi_device_init(struct comedi_device *dev)
2269 spin_lock_init(&dev->spinlock);
2270 mutex_init(&dev->mutex);
2274 static void comedi_device_cleanup(struct comedi_device *dev)
2276 struct module *driver_module = NULL;
2280 mutex_lock(&dev->mutex);
2282 driver_module = dev->driver->module;
2283 comedi_device_detach(dev);
2284 while (dev->use_count > 0) {
2286 module_put(driver_module);
2287 module_put(THIS_MODULE);
2290 mutex_unlock(&dev->mutex);
2291 mutex_destroy(&dev->mutex);
2294 /* Note: the ->mutex is pre-locked on successful return */
2295 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2297 struct comedi_file_info *info;
2298 struct comedi_device *dev;
2299 struct device *csdev;
2302 info = kzalloc(sizeof(*info), GFP_KERNEL);
2304 return ERR_PTR(-ENOMEM);
2305 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2308 return ERR_PTR(-ENOMEM);
2311 info->hardware_device = hardware_device;
2312 comedi_device_init(dev);
2313 mutex_lock(&dev->mutex);
2314 spin_lock(&comedi_file_info_table_lock);
2315 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2316 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2317 if (comedi_file_info_table[i] == NULL) {
2318 comedi_file_info_table[i] = info;
2322 spin_unlock(&comedi_file_info_table_lock);
2323 if (i == COMEDI_NUM_BOARD_MINORS) {
2324 mutex_unlock(&dev->mutex);
2325 comedi_device_cleanup(dev);
2328 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2329 return ERR_PTR(-EBUSY);
2332 csdev = device_create(comedi_class, hardware_device,
2333 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2335 dev->class_dev = csdev;
2336 dev_set_drvdata(csdev, info);
2338 /* Note: dev->mutex needs to be unlocked by the caller. */
2342 static struct comedi_file_info *comedi_clear_minor(unsigned minor)
2344 struct comedi_file_info *info;
2346 spin_lock(&comedi_file_info_table_lock);
2347 info = comedi_file_info_table[minor];
2348 comedi_file_info_table[minor] = NULL;
2349 spin_unlock(&comedi_file_info_table_lock);
2353 static void comedi_free_board_file_info(struct comedi_file_info *info)
2356 struct comedi_device *dev = info->device;
2358 if (dev->class_dev) {
2359 device_destroy(comedi_class,
2360 MKDEV(COMEDI_MAJOR, dev->minor));
2362 comedi_device_cleanup(dev);
2369 static void comedi_free_board_minor(unsigned minor)
2371 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2372 comedi_free_board_file_info(comedi_clear_minor(minor));
2375 void comedi_release_hardware_device(struct device *hardware_device)
2378 struct comedi_file_info *info;
2380 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2382 spin_lock(&comedi_file_info_table_lock);
2383 info = comedi_file_info_table[minor];
2384 if (info && info->hardware_device == hardware_device) {
2385 comedi_file_info_table[minor] = NULL;
2386 spin_unlock(&comedi_file_info_table_lock);
2387 comedi_free_board_file_info(info);
2390 spin_unlock(&comedi_file_info_table_lock);
2394 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2396 struct comedi_device *dev = s->device;
2397 struct comedi_file_info *info;
2398 struct device *csdev;
2401 info = kzalloc(sizeof(*info), GFP_KERNEL);
2405 if (s->subdev_flags & SDF_CMD_READ)
2406 info->read_subdevice = s;
2407 if (s->subdev_flags & SDF_CMD_WRITE)
2408 info->write_subdevice = s;
2409 spin_lock(&comedi_file_info_table_lock);
2410 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2411 if (comedi_file_info_table[i] == NULL) {
2412 comedi_file_info_table[i] = info;
2416 spin_unlock(&comedi_file_info_table_lock);
2417 if (i == COMEDI_NUM_MINORS) {
2419 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2423 csdev = device_create(comedi_class, dev->class_dev,
2424 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2425 dev->minor, s->index);
2427 s->class_dev = csdev;
2428 dev_set_drvdata(csdev, info);
2433 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2435 struct comedi_file_info *info;
2442 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2443 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2445 info = comedi_clear_minor(s->minor);
2447 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2448 s->class_dev = NULL;
2453 static void comedi_cleanup_board_minors(void)
2457 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2458 comedi_free_board_minor(i);
2461 static int __init comedi_init(void)
2466 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2468 if (comedi_num_legacy_minors < 0 ||
2469 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2470 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2471 COMEDI_NUM_BOARD_MINORS);
2475 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2476 COMEDI_NUM_MINORS, "comedi");
2479 cdev_init(&comedi_cdev, &comedi_fops);
2480 comedi_cdev.owner = THIS_MODULE;
2481 kobject_set_name(&comedi_cdev.kobj, "comedi");
2482 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2483 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2487 comedi_class = class_create(THIS_MODULE, "comedi");
2488 if (IS_ERR(comedi_class)) {
2489 pr_err("comedi: failed to create class\n");
2490 cdev_del(&comedi_cdev);
2491 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2493 return PTR_ERR(comedi_class);
2496 comedi_class->dev_attrs = comedi_dev_attrs;
2498 /* XXX requires /proc interface */
2501 /* create devices files for legacy/manual use */
2502 for (i = 0; i < comedi_num_legacy_minors; i++) {
2503 struct comedi_device *dev;
2504 dev = comedi_alloc_board_minor(NULL);
2506 comedi_cleanup_board_minors();
2507 cdev_del(&comedi_cdev);
2508 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2510 return PTR_ERR(dev);
2512 /* comedi_alloc_board_minor() locked the mutex */
2513 mutex_unlock(&dev->mutex);
2519 module_init(comedi_init);
2521 static void __exit comedi_cleanup(void)
2525 comedi_cleanup_board_minors();
2526 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2527 BUG_ON(comedi_file_info_table[i]);
2529 class_destroy(comedi_class);
2530 cdev_del(&comedi_cdev);
2531 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2533 comedi_proc_cleanup();
2535 module_exit(comedi_cleanup);
2537 MODULE_AUTHOR("http://www.comedi.org");
2538 MODULE_DESCRIPTION("Comedi core module");
2539 MODULE_LICENSE("GPL");