5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "comedi_compat32.h"
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/fcntl.h>
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/kmod.h>
38 #include <linux/poll.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/vmalloc.h>
43 #include "comedidev.h"
44 #include <linux/cdev.h>
45 #include <linux/stat.h>
48 #include <linux/uaccess.h>
50 #include "comedi_internal.h"
52 #define COMEDI_NUM_MINORS 0x100
53 #define COMEDI_NUM_SUBDEVICE_MINORS \
54 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
56 #ifdef CONFIG_COMEDI_DEBUG
58 EXPORT_SYMBOL(comedi_debug);
59 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
60 MODULE_PARM_DESC(comedi_debug,
61 "enable comedi core and driver debugging if non-zero (default 0)"
65 static int comedi_num_legacy_minors;
66 module_param(comedi_num_legacy_minors, int, S_IRUGO);
67 MODULE_PARM_DESC(comedi_num_legacy_minors,
68 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
71 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
72 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
73 MODULE_PARM_DESC(comedi_default_buf_size_kb,
74 "default asynchronous buffer size in KiB (default "
75 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
77 unsigned int comedi_default_buf_maxsize_kb
78 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
79 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
80 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
81 "default maximum size of asynchronous buffer in KiB (default "
82 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
84 struct comedi_file_info {
85 struct comedi_device *device;
86 struct comedi_subdevice *read_subdevice;
87 struct comedi_subdevice *write_subdevice;
90 static DEFINE_MUTEX(comedi_board_minor_table_lock);
91 static struct comedi_file_info
92 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
94 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
95 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
96 static struct comedi_file_info
97 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
99 static struct class *comedi_class;
100 static struct cdev comedi_cdev;
102 static void comedi_device_init(struct comedi_device *dev)
104 spin_lock_init(&dev->spinlock);
105 mutex_init(&dev->mutex);
109 static void comedi_device_cleanup(struct comedi_device *dev)
111 struct module *driver_module = NULL;
115 mutex_lock(&dev->mutex);
117 driver_module = dev->driver->module;
118 comedi_device_detach(dev);
119 while (dev->use_count > 0) {
121 module_put(driver_module);
122 module_put(THIS_MODULE);
125 mutex_unlock(&dev->mutex);
126 mutex_destroy(&dev->mutex);
129 static struct comedi_file_info *comedi_clear_board_minor(unsigned minor)
131 struct comedi_file_info *info;
133 mutex_lock(&comedi_board_minor_table_lock);
134 info = comedi_board_minor_table[minor];
135 comedi_board_minor_table[minor] = NULL;
136 mutex_unlock(&comedi_board_minor_table_lock);
140 static struct comedi_file_info *comedi_clear_subdevice_minor(unsigned minor)
142 struct comedi_file_info *info;
143 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
145 mutex_lock(&comedi_subdevice_minor_table_lock);
146 info = comedi_subdevice_minor_table[i];
147 comedi_subdevice_minor_table[i] = NULL;
148 mutex_unlock(&comedi_subdevice_minor_table_lock);
152 static void comedi_free_board_file_info(struct comedi_file_info *info)
155 struct comedi_device *dev = info->device;
157 if (dev->class_dev) {
158 device_destroy(comedi_class,
159 MKDEV(COMEDI_MAJOR, dev->minor));
161 comedi_device_cleanup(dev);
168 static struct comedi_file_info
169 *comedi_file_info_from_board_minor(unsigned minor)
171 struct comedi_file_info *info;
173 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
174 mutex_lock(&comedi_board_minor_table_lock);
175 info = comedi_board_minor_table[minor];
176 mutex_unlock(&comedi_board_minor_table_lock);
180 static struct comedi_file_info
181 *comedi_file_info_from_subdevice_minor(unsigned minor)
183 struct comedi_file_info *info;
184 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
186 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
187 mutex_lock(&comedi_subdevice_minor_table_lock);
188 info = comedi_subdevice_minor_table[i];
189 mutex_unlock(&comedi_subdevice_minor_table_lock);
193 static struct comedi_file_info *comedi_file_info_from_minor(unsigned minor)
195 if (minor < COMEDI_NUM_BOARD_MINORS)
196 return comedi_file_info_from_board_minor(minor);
198 return comedi_file_info_from_subdevice_minor(minor);
201 static struct comedi_device *
202 comedi_dev_from_file_info(struct comedi_file_info *info)
204 return info ? info->device : NULL;
207 struct comedi_device *comedi_dev_from_minor(unsigned minor)
209 return comedi_dev_from_file_info(comedi_file_info_from_minor(minor));
211 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
213 static struct comedi_subdevice *
214 comedi_read_subdevice(const struct comedi_file_info *info)
216 if (info->read_subdevice)
217 return info->read_subdevice;
219 return info->device->read_subdev;
223 static struct comedi_subdevice *
224 comedi_write_subdevice(const struct comedi_file_info *info)
226 if (info->write_subdevice)
227 return info->write_subdevice;
229 return info->device->write_subdev;
233 static int resize_async_buffer(struct comedi_device *dev,
234 struct comedi_subdevice *s,
235 struct comedi_async *async, unsigned new_size)
239 if (new_size > async->max_bufsize)
243 DPRINTK("subdevice is busy, cannot resize buffer\n");
246 if (async->mmap_count) {
247 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
251 if (!async->prealloc_buf)
254 /* make sure buffer is an integral number of pages
256 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
258 retval = comedi_buf_alloc(dev, s, new_size);
263 retval = s->buf_change(dev, s, new_size);
268 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
269 dev->minor, s->index, async->prealloc_bufsz);
273 /* sysfs attribute files */
275 static ssize_t show_max_read_buffer_kb(struct device *dev,
276 struct device_attribute *attr, char *buf)
278 struct comedi_file_info *info = dev_get_drvdata(dev);
279 struct comedi_subdevice *s = comedi_read_subdevice(info);
280 unsigned int size = 0;
282 mutex_lock(&info->device->mutex);
283 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
284 size = s->async->max_bufsize / 1024;
285 mutex_unlock(&info->device->mutex);
287 return snprintf(buf, PAGE_SIZE, "%i\n", size);
290 static ssize_t store_max_read_buffer_kb(struct device *dev,
291 struct device_attribute *attr,
292 const char *buf, size_t count)
294 struct comedi_file_info *info = dev_get_drvdata(dev);
295 struct comedi_subdevice *s = comedi_read_subdevice(info);
299 err = kstrtouint(buf, 10, &size);
302 if (size > (UINT_MAX / 1024))
306 mutex_lock(&info->device->mutex);
307 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
308 s->async->max_bufsize = size;
311 mutex_unlock(&info->device->mutex);
313 return err ? err : count;
316 static ssize_t show_read_buffer_kb(struct device *dev,
317 struct device_attribute *attr, char *buf)
319 struct comedi_file_info *info = dev_get_drvdata(dev);
320 struct comedi_subdevice *s = comedi_read_subdevice(info);
321 unsigned int size = 0;
323 mutex_lock(&info->device->mutex);
324 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
325 size = s->async->prealloc_bufsz / 1024;
326 mutex_unlock(&info->device->mutex);
328 return snprintf(buf, PAGE_SIZE, "%i\n", size);
331 static ssize_t store_read_buffer_kb(struct device *dev,
332 struct device_attribute *attr,
333 const char *buf, size_t count)
335 struct comedi_file_info *info = dev_get_drvdata(dev);
336 struct comedi_subdevice *s = comedi_read_subdevice(info);
340 err = kstrtouint(buf, 10, &size);
343 if (size > (UINT_MAX / 1024))
347 mutex_lock(&info->device->mutex);
348 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
349 err = resize_async_buffer(info->device, s, s->async, size);
352 mutex_unlock(&info->device->mutex);
354 return err ? err : count;
357 static ssize_t show_max_write_buffer_kb(struct device *dev,
358 struct device_attribute *attr,
361 struct comedi_file_info *info = dev_get_drvdata(dev);
362 struct comedi_subdevice *s = comedi_write_subdevice(info);
363 unsigned int size = 0;
365 mutex_lock(&info->device->mutex);
366 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
367 size = s->async->max_bufsize / 1024;
368 mutex_unlock(&info->device->mutex);
370 return snprintf(buf, PAGE_SIZE, "%i\n", size);
373 static ssize_t store_max_write_buffer_kb(struct device *dev,
374 struct device_attribute *attr,
375 const char *buf, size_t count)
377 struct comedi_file_info *info = dev_get_drvdata(dev);
378 struct comedi_subdevice *s = comedi_write_subdevice(info);
382 err = kstrtouint(buf, 10, &size);
385 if (size > (UINT_MAX / 1024))
389 mutex_lock(&info->device->mutex);
390 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
391 s->async->max_bufsize = size;
394 mutex_unlock(&info->device->mutex);
396 return err ? err : count;
399 static ssize_t show_write_buffer_kb(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 struct comedi_file_info *info = dev_get_drvdata(dev);
403 struct comedi_subdevice *s = comedi_write_subdevice(info);
404 unsigned int size = 0;
406 mutex_lock(&info->device->mutex);
407 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
408 size = s->async->prealloc_bufsz / 1024;
409 mutex_unlock(&info->device->mutex);
411 return snprintf(buf, PAGE_SIZE, "%i\n", size);
414 static ssize_t store_write_buffer_kb(struct device *dev,
415 struct device_attribute *attr,
416 const char *buf, size_t count)
418 struct comedi_file_info *info = dev_get_drvdata(dev);
419 struct comedi_subdevice *s = comedi_write_subdevice(info);
423 err = kstrtouint(buf, 10, &size);
426 if (size > (UINT_MAX / 1024))
430 mutex_lock(&info->device->mutex);
431 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
432 err = resize_async_buffer(info->device, s, s->async, size);
435 mutex_unlock(&info->device->mutex);
437 return err ? err : count;
440 static struct device_attribute comedi_dev_attrs[] = {
441 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
442 show_max_read_buffer_kb, store_max_read_buffer_kb),
443 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
444 show_read_buffer_kb, store_read_buffer_kb),
445 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
446 show_max_write_buffer_kb, store_max_write_buffer_kb),
447 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
448 show_write_buffer_kb, store_write_buffer_kb),
452 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
453 unsigned mask, unsigned bits)
457 spin_lock_irqsave(&s->spin_lock, flags);
458 s->runflags &= ~mask;
459 s->runflags |= (bits & mask);
460 spin_unlock_irqrestore(&s->spin_lock, flags);
463 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
468 spin_lock_irqsave(&s->spin_lock, flags);
469 runflags = s->runflags;
470 spin_unlock_irqrestore(&s->spin_lock, flags);
474 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
476 unsigned runflags = comedi_get_subdevice_runflags(s);
478 return (runflags & SRF_RUNNING) ? true : false;
480 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
482 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
484 unsigned runflags = comedi_get_subdevice_runflags(s);
486 return (runflags & SRF_ERROR) ? true : false;
489 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
491 unsigned runflags = comedi_get_subdevice_runflags(s);
493 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
497 This function restores a subdevice to an idle state.
499 static void do_become_nonbusy(struct comedi_device *dev,
500 struct comedi_subdevice *s)
502 struct comedi_async *async = s->async;
504 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
506 comedi_buf_reset(async);
507 async->inttrig = NULL;
508 kfree(async->cmd.chanlist);
509 async->cmd.chanlist = NULL;
511 dev_err(dev->class_dev,
512 "BUG: (?) do_become_nonbusy called with async=NULL\n");
518 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
522 if (comedi_is_subdevice_running(s) && s->cancel)
523 ret = s->cancel(dev, s);
525 do_become_nonbusy(dev, s);
530 static int is_device_busy(struct comedi_device *dev)
532 struct comedi_subdevice *s;
538 for (i = 0; i < dev->n_subdevices; i++) {
539 s = &dev->subdevices[i];
542 if (s->async && s->async->mmap_count)
554 pointer to devconfig structure
557 devconfig structure at arg
562 static int do_devconfig_ioctl(struct comedi_device *dev,
563 struct comedi_devconfig __user *arg)
565 struct comedi_devconfig it;
567 if (!capable(CAP_SYS_ADMIN))
571 if (is_device_busy(dev))
574 struct module *driver_module = dev->driver->module;
575 comedi_device_detach(dev);
576 module_put(driver_module);
581 if (copy_from_user(&it, arg, sizeof(it)))
584 it.board_name[COMEDI_NAMELEN - 1] = 0;
586 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
587 dev_warn(dev->class_dev,
588 "comedi_config --init_data is deprecated\n");
592 if (dev->minor >= comedi_num_legacy_minors)
593 /* don't re-use dynamically allocated comedi devices */
596 /* This increments the driver module count on success. */
597 return comedi_device_attach(dev, &it);
602 buffer configuration ioctl
605 pointer to bufconfig structure
611 modified bufconfig at arg
614 static int do_bufconfig_ioctl(struct comedi_device *dev,
615 struct comedi_bufconfig __user *arg)
617 struct comedi_bufconfig bc;
618 struct comedi_async *async;
619 struct comedi_subdevice *s;
622 if (copy_from_user(&bc, arg, sizeof(bc)))
625 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
628 s = &dev->subdevices[bc.subdevice];
632 DPRINTK("subdevice does not have async capability\n");
638 if (bc.maximum_size) {
639 if (!capable(CAP_SYS_ADMIN))
642 async->max_bufsize = bc.maximum_size;
646 retval = resize_async_buffer(dev, s, async, bc.size);
651 bc.size = async->prealloc_bufsz;
652 bc.maximum_size = async->max_bufsize;
655 if (copy_to_user(arg, &bc, sizeof(bc)))
666 pointer to devinfo structure
675 static int do_devinfo_ioctl(struct comedi_device *dev,
676 struct comedi_devinfo __user *arg,
679 const unsigned minor = iminor(file_inode(file));
680 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
681 struct comedi_subdevice *s;
682 struct comedi_devinfo devinfo;
684 memset(&devinfo, 0, sizeof(devinfo));
686 /* fill devinfo structure */
687 devinfo.version_code = COMEDI_VERSION_CODE;
688 devinfo.n_subdevs = dev->n_subdevices;
689 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
690 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
692 s = comedi_read_subdevice(info);
694 devinfo.read_subdevice = s->index;
696 devinfo.read_subdevice = -1;
698 s = comedi_write_subdevice(info);
700 devinfo.write_subdevice = s->index;
702 devinfo.write_subdevice = -1;
704 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
715 pointer to array of subdevice info structures
721 array of subdevice info structures at arg
724 static int do_subdinfo_ioctl(struct comedi_device *dev,
725 struct comedi_subdinfo __user *arg, void *file)
728 struct comedi_subdinfo *tmp, *us;
729 struct comedi_subdevice *s;
731 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
735 /* fill subdinfo structs */
736 for (i = 0; i < dev->n_subdevices; i++) {
737 s = &dev->subdevices[i];
741 us->n_chan = s->n_chan;
742 us->subd_flags = s->subdev_flags;
743 if (comedi_is_subdevice_running(s))
744 us->subd_flags |= SDF_RUNNING;
745 #define TIMER_nanosec 5 /* backwards compatibility */
746 us->timer_type = TIMER_nanosec;
747 us->len_chanlist = s->len_chanlist;
748 us->maxdata = s->maxdata;
749 if (s->range_table) {
751 (i << 24) | (0 << 16) | (s->range_table->length);
753 us->range_type = 0; /* XXX */
755 us->flags = s->flags;
758 us->subd_flags |= SDF_BUSY;
760 us->subd_flags |= SDF_BUSY_OWNER;
762 us->subd_flags |= SDF_LOCKED;
764 us->subd_flags |= SDF_LOCK_OWNER;
765 if (!s->maxdata && s->maxdata_list)
766 us->subd_flags |= SDF_MAXDATA;
768 us->subd_flags |= SDF_FLAGS;
769 if (s->range_table_list)
770 us->subd_flags |= SDF_RANGETYPE;
772 us->subd_flags |= SDF_CMD;
774 if (s->insn_bits != &insn_inval)
775 us->insn_bits_support = COMEDI_SUPPORTED;
777 us->insn_bits_support = COMEDI_UNSUPPORTED;
779 us->settling_time_0 = s->settling_time_0;
782 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
786 return ret ? -EFAULT : 0;
794 pointer to chaninfo structure
797 chaninfo structure at arg
800 arrays at elements of chaninfo structure
803 static int do_chaninfo_ioctl(struct comedi_device *dev,
804 struct comedi_chaninfo __user *arg)
806 struct comedi_subdevice *s;
807 struct comedi_chaninfo it;
809 if (copy_from_user(&it, arg, sizeof(it)))
812 if (it.subdev >= dev->n_subdevices)
814 s = &dev->subdevices[it.subdev];
816 if (it.maxdata_list) {
817 if (s->maxdata || !s->maxdata_list)
819 if (copy_to_user(it.maxdata_list, s->maxdata_list,
820 s->n_chan * sizeof(unsigned int)))
827 if (copy_to_user(it.flaglist, s->flaglist,
828 s->n_chan * sizeof(unsigned int)))
835 if (!s->range_table_list)
837 for (i = 0; i < s->n_chan; i++) {
840 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
841 (s->range_table_list[i]->length);
842 if (put_user(x, it.rangelist + i))
846 if (copy_to_user(it.rangelist, s->range_type_list,
847 s->n_chan * sizeof(unsigned int)))
857 buffer information ioctl
860 pointer to bufinfo structure
866 modified bufinfo at arg
869 static int do_bufinfo_ioctl(struct comedi_device *dev,
870 struct comedi_bufinfo __user *arg, void *file)
872 struct comedi_bufinfo bi;
873 struct comedi_subdevice *s;
874 struct comedi_async *async;
876 if (copy_from_user(&bi, arg, sizeof(bi)))
879 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
882 s = &dev->subdevices[bi.subdevice];
884 if (s->lock && s->lock != file)
890 DPRINTK("subdevice does not have async capability\n");
891 bi.buf_write_ptr = 0;
893 bi.buf_write_count = 0;
894 bi.buf_read_count = 0;
896 bi.bytes_written = 0;
901 bi.bytes_written = 0;
902 goto copyback_position;
907 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
908 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
909 comedi_buf_read_free(async, bi.bytes_read);
911 if (comedi_is_subdevice_idle(s) &&
912 async->buf_write_count == async->buf_read_count) {
913 do_become_nonbusy(dev, s);
917 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
919 comedi_buf_write_alloc(async, bi.bytes_written);
920 comedi_buf_write_free(async, bi.bytes_written);
924 bi.buf_write_count = async->buf_write_count;
925 bi.buf_write_ptr = async->buf_write_ptr;
926 bi.buf_read_count = async->buf_read_count;
927 bi.buf_read_ptr = async->buf_read_ptr;
930 if (copy_to_user(arg, &bi, sizeof(bi)))
936 static int check_insn_config_length(struct comedi_insn *insn,
943 case INSN_CONFIG_DIO_OUTPUT:
944 case INSN_CONFIG_DIO_INPUT:
945 case INSN_CONFIG_DISARM:
946 case INSN_CONFIG_RESET:
950 case INSN_CONFIG_ARM:
951 case INSN_CONFIG_DIO_QUERY:
952 case INSN_CONFIG_BLOCK_SIZE:
953 case INSN_CONFIG_FILTER:
954 case INSN_CONFIG_SERIAL_CLOCK:
955 case INSN_CONFIG_BIDIRECTIONAL_DATA:
956 case INSN_CONFIG_ALT_SOURCE:
957 case INSN_CONFIG_SET_COUNTER_MODE:
958 case INSN_CONFIG_8254_READ_STATUS:
959 case INSN_CONFIG_SET_ROUTING:
960 case INSN_CONFIG_GET_ROUTING:
961 case INSN_CONFIG_GET_PWM_STATUS:
962 case INSN_CONFIG_PWM_SET_PERIOD:
963 case INSN_CONFIG_PWM_GET_PERIOD:
967 case INSN_CONFIG_SET_GATE_SRC:
968 case INSN_CONFIG_GET_GATE_SRC:
969 case INSN_CONFIG_SET_CLOCK_SRC:
970 case INSN_CONFIG_GET_CLOCK_SRC:
971 case INSN_CONFIG_SET_OTHER_SRC:
972 case INSN_CONFIG_GET_COUNTER_STATUS:
973 case INSN_CONFIG_PWM_SET_H_BRIDGE:
974 case INSN_CONFIG_PWM_GET_H_BRIDGE:
975 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
979 case INSN_CONFIG_PWM_OUTPUT:
980 case INSN_CONFIG_ANALOG_TRIG:
984 case INSN_CONFIG_DIGITAL_TRIG:
988 /* by default we allow the insn since we don't have checks for
989 * all possible cases yet */
991 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
993 pr_warn("comedi: Add a check to %s in %s.\n",
995 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1001 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1002 unsigned int *data, void *file)
1004 struct comedi_subdevice *s;
1008 if (insn->insn & INSN_MASK_SPECIAL) {
1009 /* a non-subdevice instruction */
1011 switch (insn->insn) {
1021 do_gettimeofday(&tv);
1022 data[0] = tv.tv_sec;
1023 data[1] = tv.tv_usec;
1029 if (insn->n != 1 || data[0] >= 100000) {
1033 udelay(data[0] / 1000);
1041 if (insn->subdev >= dev->n_subdevices) {
1042 DPRINTK("%d not usable subdevice\n",
1047 s = &dev->subdevices[insn->subdev];
1049 DPRINTK("no async\n");
1053 if (!s->async->inttrig) {
1054 DPRINTK("no inttrig\n");
1058 ret = s->async->inttrig(dev, s, data[0]);
1063 DPRINTK("invalid insn\n");
1068 /* a subdevice instruction */
1069 unsigned int maxdata;
1071 if (insn->subdev >= dev->n_subdevices) {
1072 DPRINTK("subdevice %d out of range\n", insn->subdev);
1076 s = &dev->subdevices[insn->subdev];
1078 if (s->type == COMEDI_SUBD_UNUSED) {
1079 DPRINTK("%d not usable subdevice\n", insn->subdev);
1084 /* are we locked? (ioctl lock) */
1085 if (s->lock && s->lock != file) {
1086 DPRINTK("device locked\n");
1091 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1094 DPRINTK("bad chanspec\n");
1102 /* This looks arbitrary. It is. */
1103 s->busy = &parse_insn;
1104 switch (insn->insn) {
1106 ret = s->insn_read(dev, s, insn, data);
1109 maxdata = s->maxdata_list
1110 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1112 for (i = 0; i < insn->n; ++i) {
1113 if (data[i] > maxdata) {
1115 DPRINTK("bad data value(s)\n");
1120 ret = s->insn_write(dev, s, insn, data);
1126 /* Most drivers ignore the base channel in
1127 * insn->chanspec. Fix this here if
1128 * the subdevice has <= 32 channels. */
1130 unsigned int orig_mask;
1132 orig_mask = data[0];
1133 if (s->n_chan <= 32) {
1134 shift = CR_CHAN(insn->chanspec);
1142 ret = s->insn_bits(dev, s, insn, data);
1143 data[0] = orig_mask;
1149 ret = check_insn_config_length(insn, data);
1152 ret = s->insn_config(dev, s, insn, data);
1168 * synchronous instructions
1171 * pointer to sync cmd structure
1174 * sync cmd struct at arg
1181 /* arbitrary limits */
1182 #define MAX_SAMPLES 256
1183 static int do_insnlist_ioctl(struct comedi_device *dev,
1184 struct comedi_insnlist __user *arg, void *file)
1186 struct comedi_insnlist insnlist;
1187 struct comedi_insn *insns = NULL;
1188 unsigned int *data = NULL;
1192 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1195 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1197 DPRINTK("kmalloc failed\n");
1202 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1204 DPRINTK("kmalloc failed\n");
1209 if (copy_from_user(insns, insnlist.insns,
1210 sizeof(*insns) * insnlist.n_insns)) {
1211 DPRINTK("copy_from_user failed\n");
1216 for (i = 0; i < insnlist.n_insns; i++) {
1217 if (insns[i].n > MAX_SAMPLES) {
1218 DPRINTK("number of samples too large\n");
1222 if (insns[i].insn & INSN_MASK_WRITE) {
1223 if (copy_from_user(data, insns[i].data,
1224 insns[i].n * sizeof(unsigned int))) {
1225 DPRINTK("copy_from_user failed\n");
1230 ret = parse_insn(dev, insns + i, data, file);
1233 if (insns[i].insn & INSN_MASK_READ) {
1234 if (copy_to_user(insns[i].data, data,
1235 insns[i].n * sizeof(unsigned int))) {
1236 DPRINTK("copy_to_user failed\n");
1256 * synchronous instructions
1262 * struct comedi_insn struct at arg
1268 static int do_insn_ioctl(struct comedi_device *dev,
1269 struct comedi_insn __user *arg, void *file)
1271 struct comedi_insn insn;
1272 unsigned int *data = NULL;
1275 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1281 if (copy_from_user(&insn, arg, sizeof(insn))) {
1286 /* This is where the behavior of insn and insnlist deviate. */
1287 if (insn.n > MAX_SAMPLES)
1288 insn.n = MAX_SAMPLES;
1289 if (insn.insn & INSN_MASK_WRITE) {
1290 if (copy_from_user(data,
1292 insn.n * sizeof(unsigned int))) {
1297 ret = parse_insn(dev, &insn, data, file);
1300 if (insn.insn & INSN_MASK_READ) {
1301 if (copy_to_user(insn.data,
1303 insn.n * sizeof(unsigned int))) {
1316 static int do_cmd_ioctl(struct comedi_device *dev,
1317 struct comedi_cmd __user *arg, void *file)
1319 struct comedi_cmd cmd;
1320 struct comedi_subdevice *s;
1321 struct comedi_async *async;
1323 unsigned int __user *user_chanlist;
1325 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1326 DPRINTK("bad cmd address\n");
1329 /* save user's chanlist pointer so it can be restored later */
1330 user_chanlist = (unsigned int __user *)cmd.chanlist;
1332 if (cmd.subdev >= dev->n_subdevices) {
1333 DPRINTK("%d no such subdevice\n", cmd.subdev);
1337 s = &dev->subdevices[cmd.subdev];
1340 if (s->type == COMEDI_SUBD_UNUSED) {
1341 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1345 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1346 DPRINTK("subdevice %i does not support commands\n",
1351 /* are we locked? (ioctl lock) */
1352 if (s->lock && s->lock != file) {
1353 DPRINTK("subdevice locked\n");
1359 DPRINTK("subdevice busy\n");
1364 /* make sure channel/gain list isn't too long */
1365 if (cmd.chanlist_len > s->len_chanlist) {
1366 DPRINTK("channel/gain list too long %u > %d\n",
1367 cmd.chanlist_len, s->len_chanlist);
1372 /* make sure channel/gain list isn't too short */
1373 if (cmd.chanlist_len < 1) {
1374 DPRINTK("channel/gain list too short %u < 1\n",
1381 async->cmd.data = NULL;
1382 /* load channel/gain list */
1383 async->cmd.chanlist =
1384 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1385 if (!async->cmd.chanlist) {
1386 DPRINTK("allocation failed\n");
1391 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1392 async->cmd.chanlist_len * sizeof(int))) {
1393 DPRINTK("fault reading chanlist\n");
1398 /* make sure each element in channel/gain list is valid */
1399 ret = comedi_check_chanlist(s,
1400 async->cmd.chanlist_len,
1401 async->cmd.chanlist);
1403 DPRINTK("bad chanlist\n");
1407 ret = s->do_cmdtest(dev, s, &async->cmd);
1409 if (async->cmd.flags & TRIG_BOGUS || ret) {
1410 DPRINTK("test returned %d\n", ret);
1412 /* restore chanlist pointer before copying back */
1413 cmd.chanlist = (unsigned int __force *)user_chanlist;
1415 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1416 DPRINTK("fault writing cmd\n");
1424 if (!async->prealloc_bufsz) {
1426 DPRINTK("no buffer (?)\n");
1430 comedi_buf_reset(async);
1433 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1435 if (async->cmd.flags & TRIG_WAKE_EOS)
1436 async->cb_mask |= COMEDI_CB_EOS;
1438 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1440 ret = s->do_cmd(dev, s);
1445 do_become_nonbusy(dev, s);
1452 command testing ioctl
1455 pointer to cmd structure
1458 cmd structure at arg
1462 modified cmd structure at arg
1465 static int do_cmdtest_ioctl(struct comedi_device *dev,
1466 struct comedi_cmd __user *arg, void *file)
1468 struct comedi_cmd cmd;
1469 struct comedi_subdevice *s;
1471 unsigned int *chanlist = NULL;
1472 unsigned int __user *user_chanlist;
1474 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1475 DPRINTK("bad cmd address\n");
1478 /* save user's chanlist pointer so it can be restored later */
1479 user_chanlist = (unsigned int __user *)cmd.chanlist;
1481 if (cmd.subdev >= dev->n_subdevices) {
1482 DPRINTK("%d no such subdevice\n", cmd.subdev);
1486 s = &dev->subdevices[cmd.subdev];
1487 if (s->type == COMEDI_SUBD_UNUSED) {
1488 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1492 if (!s->do_cmd || !s->do_cmdtest) {
1493 DPRINTK("subdevice %i does not support commands\n",
1498 /* make sure channel/gain list isn't too long */
1499 if (cmd.chanlist_len > s->len_chanlist) {
1500 DPRINTK("channel/gain list too long %d > %d\n",
1501 cmd.chanlist_len, s->len_chanlist);
1506 /* load channel/gain list */
1509 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1511 DPRINTK("allocation failed\n");
1516 if (copy_from_user(chanlist, user_chanlist,
1517 cmd.chanlist_len * sizeof(int))) {
1518 DPRINTK("fault reading chanlist\n");
1523 /* make sure each element in channel/gain list is valid */
1524 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1526 DPRINTK("bad chanlist\n");
1530 cmd.chanlist = chanlist;
1533 ret = s->do_cmdtest(dev, s, &cmd);
1535 /* restore chanlist pointer before copying back */
1536 cmd.chanlist = (unsigned int __force *)user_chanlist;
1538 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1539 DPRINTK("bad cmd address\n");
1564 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1568 unsigned long flags;
1569 struct comedi_subdevice *s;
1571 if (arg >= dev->n_subdevices)
1573 s = &dev->subdevices[arg];
1575 spin_lock_irqsave(&s->spin_lock, flags);
1576 if (s->busy || s->lock)
1580 spin_unlock_irqrestore(&s->spin_lock, flags);
1587 ret = s->lock_f(dev, s);
1606 This function isn't protected by the semaphore, since
1607 we already own the lock.
1609 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1612 struct comedi_subdevice *s;
1614 if (arg >= dev->n_subdevices)
1616 s = &dev->subdevices[arg];
1621 if (s->lock && s->lock != file)
1624 if (s->lock == file) {
1638 cancel acquisition ioctl
1650 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1653 struct comedi_subdevice *s;
1655 if (arg >= dev->n_subdevices)
1657 s = &dev->subdevices[arg];
1658 if (s->async == NULL)
1661 if (s->lock && s->lock != file)
1667 if (s->busy != file)
1670 return do_cancel(dev, s);
1675 instructs driver to synchronize buffers
1687 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1690 struct comedi_subdevice *s;
1692 if (arg >= dev->n_subdevices)
1694 s = &dev->subdevices[arg];
1696 if (s->lock && s->lock != file)
1702 if (s->busy != file)
1706 return s->poll(dev, s);
1711 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1714 const unsigned minor = iminor(file_inode(file));
1715 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1716 struct comedi_device *dev = comedi_dev_from_file_info(info);
1722 mutex_lock(&dev->mutex);
1724 /* Device config is special, because it must work on
1725 * an unconfigured device. */
1726 if (cmd == COMEDI_DEVCONFIG) {
1727 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1728 /* Device config not appropriate on non-board minors. */
1732 rc = do_devconfig_ioctl(dev,
1733 (struct comedi_devconfig __user *)arg);
1736 dev->minor >= comedi_num_legacy_minors) {
1737 /* Successfully unconfigured a dynamically
1738 * allocated device. Try and remove it. */
1739 info = comedi_clear_board_minor(dev->minor);
1741 mutex_unlock(&dev->mutex);
1742 comedi_free_board_file_info(info);
1750 if (!dev->attached) {
1751 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1757 case COMEDI_BUFCONFIG:
1758 rc = do_bufconfig_ioctl(dev,
1759 (struct comedi_bufconfig __user *)arg);
1761 case COMEDI_DEVINFO:
1762 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1765 case COMEDI_SUBDINFO:
1766 rc = do_subdinfo_ioctl(dev,
1767 (struct comedi_subdinfo __user *)arg,
1770 case COMEDI_CHANINFO:
1771 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1773 case COMEDI_RANGEINFO:
1774 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1776 case COMEDI_BUFINFO:
1777 rc = do_bufinfo_ioctl(dev,
1778 (struct comedi_bufinfo __user *)arg,
1782 rc = do_lock_ioctl(dev, arg, file);
1785 rc = do_unlock_ioctl(dev, arg, file);
1788 rc = do_cancel_ioctl(dev, arg, file);
1791 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1793 case COMEDI_CMDTEST:
1794 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1797 case COMEDI_INSNLIST:
1798 rc = do_insnlist_ioctl(dev,
1799 (struct comedi_insnlist __user *)arg,
1803 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1807 rc = do_poll_ioctl(dev, arg, file);
1815 mutex_unlock(&dev->mutex);
1819 static void comedi_vm_open(struct vm_area_struct *area)
1821 struct comedi_async *async;
1822 struct comedi_device *dev;
1824 async = area->vm_private_data;
1825 dev = async->subdevice->device;
1827 mutex_lock(&dev->mutex);
1828 async->mmap_count++;
1829 mutex_unlock(&dev->mutex);
1832 static void comedi_vm_close(struct vm_area_struct *area)
1834 struct comedi_async *async;
1835 struct comedi_device *dev;
1837 async = area->vm_private_data;
1838 dev = async->subdevice->device;
1840 mutex_lock(&dev->mutex);
1841 async->mmap_count--;
1842 mutex_unlock(&dev->mutex);
1845 static struct vm_operations_struct comedi_vm_ops = {
1846 .open = comedi_vm_open,
1847 .close = comedi_vm_close,
1850 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1852 const unsigned minor = iminor(file_inode(file));
1853 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1854 struct comedi_device *dev = comedi_dev_from_file_info(info);
1855 struct comedi_subdevice *s;
1856 struct comedi_async *async;
1857 unsigned long start = vma->vm_start;
1866 mutex_lock(&dev->mutex);
1868 if (!dev->attached) {
1869 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1874 if (vma->vm_flags & VM_WRITE)
1875 s = comedi_write_subdevice(info);
1877 s = comedi_read_subdevice(info);
1889 if (vma->vm_pgoff != 0) {
1890 DPRINTK("comedi: mmap() offset must be 0.\n");
1895 size = vma->vm_end - vma->vm_start;
1896 if (size > async->prealloc_bufsz) {
1900 if (size & (~PAGE_MASK)) {
1905 n_pages = size >> PAGE_SHIFT;
1906 for (i = 0; i < n_pages; ++i) {
1907 struct comedi_buf_page *buf = &async->buf_page_list[i];
1909 if (remap_pfn_range(vma, start,
1910 page_to_pfn(virt_to_page(buf->virt_addr)),
1911 PAGE_SIZE, PAGE_SHARED)) {
1918 vma->vm_ops = &comedi_vm_ops;
1919 vma->vm_private_data = async;
1921 async->mmap_count++;
1925 mutex_unlock(&dev->mutex);
1929 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1931 unsigned int mask = 0;
1932 const unsigned minor = iminor(file_inode(file));
1933 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1934 struct comedi_device *dev = comedi_dev_from_file_info(info);
1935 struct comedi_subdevice *s;
1940 mutex_lock(&dev->mutex);
1942 if (!dev->attached) {
1943 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1947 s = comedi_read_subdevice(info);
1948 if (s && s->async) {
1949 poll_wait(file, &s->async->wait_head, wait);
1950 if (!s->busy || !comedi_is_subdevice_running(s) ||
1951 comedi_buf_read_n_available(s->async) > 0)
1952 mask |= POLLIN | POLLRDNORM;
1955 s = comedi_write_subdevice(info);
1956 if (s && s->async) {
1957 unsigned int bps = bytes_per_sample(s->async->subdevice);
1959 poll_wait(file, &s->async->wait_head, wait);
1960 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
1961 if (!s->busy || !comedi_is_subdevice_running(s) ||
1962 comedi_buf_write_n_allocated(s->async) >= bps)
1963 mask |= POLLOUT | POLLWRNORM;
1967 mutex_unlock(&dev->mutex);
1971 static ssize_t comedi_write(struct file *file, const char __user *buf,
1972 size_t nbytes, loff_t *offset)
1974 struct comedi_subdevice *s;
1975 struct comedi_async *async;
1976 int n, m, count = 0, retval = 0;
1977 DECLARE_WAITQUEUE(wait, current);
1978 const unsigned minor = iminor(file_inode(file));
1979 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1980 struct comedi_device *dev = comedi_dev_from_file_info(info);
1985 if (!dev->attached) {
1986 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1990 s = comedi_write_subdevice(info);
1991 if (!s || !s->async)
1996 if (!s->busy || !nbytes)
1998 if (s->busy != file)
2001 add_wait_queue(&async->wait_head, &wait);
2002 while (nbytes > 0 && !retval) {
2003 set_current_state(TASK_INTERRUPTIBLE);
2005 if (!comedi_is_subdevice_running(s)) {
2007 if (comedi_is_subdevice_in_error(s))
2011 do_become_nonbusy(dev, s);
2019 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2020 m = async->prealloc_bufsz - async->buf_write_ptr;
2021 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2022 if (m > comedi_buf_write_n_allocated(async))
2023 m = comedi_buf_write_n_allocated(async);
2028 if (file->f_flags & O_NONBLOCK) {
2033 if (signal_pending(current)) {
2034 retval = -ERESTARTSYS;
2039 if (s->busy != file) {
2046 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2052 comedi_buf_write_free(async, n);
2058 break; /* makes device work like a pipe */
2060 set_current_state(TASK_RUNNING);
2061 remove_wait_queue(&async->wait_head, &wait);
2063 return count ? count : retval;
2066 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2069 struct comedi_subdevice *s;
2070 struct comedi_async *async;
2071 int n, m, count = 0, retval = 0;
2072 DECLARE_WAITQUEUE(wait, current);
2073 const unsigned minor = iminor(file_inode(file));
2074 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
2075 struct comedi_device *dev = comedi_dev_from_file_info(info);
2080 if (!dev->attached) {
2081 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2085 s = comedi_read_subdevice(info);
2086 if (!s || !s->async)
2090 if (!s->busy || !nbytes)
2092 if (s->busy != file)
2095 add_wait_queue(&async->wait_head, &wait);
2096 while (nbytes > 0 && !retval) {
2097 set_current_state(TASK_INTERRUPTIBLE);
2101 m = comedi_buf_read_n_available(async);
2102 /* printk("%d available\n",m); */
2103 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2104 m = async->prealloc_bufsz - async->buf_read_ptr;
2105 /* printk("%d contiguous\n",m); */
2110 if (!comedi_is_subdevice_running(s)) {
2111 do_become_nonbusy(dev, s);
2112 if (comedi_is_subdevice_in_error(s))
2118 if (file->f_flags & O_NONBLOCK) {
2123 if (signal_pending(current)) {
2124 retval = -ERESTARTSYS;
2131 if (s->busy != file) {
2137 m = copy_to_user(buf, async->prealloc_buf +
2138 async->buf_read_ptr, n);
2144 comedi_buf_read_alloc(async, n);
2145 comedi_buf_read_free(async, n);
2151 break; /* makes device work like a pipe */
2153 if (comedi_is_subdevice_idle(s) &&
2154 async->buf_read_count - async->buf_write_count == 0) {
2155 do_become_nonbusy(dev, s);
2157 set_current_state(TASK_RUNNING);
2158 remove_wait_queue(&async->wait_head, &wait);
2160 return count ? count : retval;
2163 static int comedi_open(struct inode *inode, struct file *file)
2165 const unsigned minor = iminor(inode);
2166 struct comedi_device *dev = comedi_dev_from_minor(minor);
2169 DPRINTK("invalid minor number\n");
2173 /* This is slightly hacky, but we want module autoloading
2175 * case: user opens device, attached -> ok
2176 * case: user opens device, unattached, !in_request_module -> autoload
2177 * case: user opens device, unattached, in_request_module -> fail
2178 * case: root opens device, attached -> ok
2179 * case: root opens device, unattached, in_request_module -> ok
2180 * (typically called from modprobe)
2181 * case: root opens device, unattached, !in_request_module -> autoload
2183 * The last could be changed to "-> ok", which would deny root
2186 mutex_lock(&dev->mutex);
2189 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2190 DPRINTK("in request module\n");
2191 mutex_unlock(&dev->mutex);
2194 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2197 dev->in_request_module = true;
2200 mutex_unlock(&dev->mutex);
2201 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2202 mutex_lock(&dev->mutex);
2205 dev->in_request_module = false;
2207 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2208 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2209 mutex_unlock(&dev->mutex);
2213 __module_get(THIS_MODULE);
2215 if (dev->attached) {
2216 if (!try_module_get(dev->driver->module)) {
2217 module_put(THIS_MODULE);
2218 mutex_unlock(&dev->mutex);
2223 if (dev->attached && dev->use_count == 0 && dev->open) {
2224 int rc = dev->open(dev);
2226 module_put(dev->driver->module);
2227 module_put(THIS_MODULE);
2228 mutex_unlock(&dev->mutex);
2235 mutex_unlock(&dev->mutex);
2240 static int comedi_fasync(int fd, struct file *file, int on)
2242 const unsigned minor = iminor(file_inode(file));
2243 struct comedi_device *dev = comedi_dev_from_minor(minor);
2248 return fasync_helper(fd, file, on, &dev->async_queue);
2251 static int comedi_close(struct inode *inode, struct file *file)
2253 const unsigned minor = iminor(inode);
2254 struct comedi_device *dev = comedi_dev_from_minor(minor);
2255 struct comedi_subdevice *s = NULL;
2261 mutex_lock(&dev->mutex);
2263 if (dev->subdevices) {
2264 for (i = 0; i < dev->n_subdevices; i++) {
2265 s = &dev->subdevices[i];
2267 if (s->busy == file)
2269 if (s->lock == file)
2273 if (dev->attached && dev->use_count == 1 && dev->close)
2276 module_put(THIS_MODULE);
2278 module_put(dev->driver->module);
2282 mutex_unlock(&dev->mutex);
2284 if (file->f_flags & FASYNC)
2285 comedi_fasync(-1, file, 0);
2290 static const struct file_operations comedi_fops = {
2291 .owner = THIS_MODULE,
2292 .unlocked_ioctl = comedi_unlocked_ioctl,
2293 .compat_ioctl = comedi_compat_ioctl,
2294 .open = comedi_open,
2295 .release = comedi_close,
2296 .read = comedi_read,
2297 .write = comedi_write,
2298 .mmap = comedi_mmap,
2299 .poll = comedi_poll,
2300 .fasync = comedi_fasync,
2301 .llseek = noop_llseek,
2304 void comedi_error(const struct comedi_device *dev, const char *s)
2306 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2308 EXPORT_SYMBOL(comedi_error);
2310 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2312 struct comedi_async *async = s->async;
2313 unsigned runflags = 0;
2314 unsigned runflags_mask = 0;
2316 /* DPRINTK("comedi_event 0x%x\n",mask); */
2318 if (!comedi_is_subdevice_running(s))
2322 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2323 COMEDI_CB_OVERFLOW)) {
2324 runflags_mask |= SRF_RUNNING;
2326 /* remember if an error event has occurred, so an error
2327 * can be returned the next time the user does a read() */
2328 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2329 runflags_mask |= SRF_ERROR;
2330 runflags |= SRF_ERROR;
2332 if (runflags_mask) {
2333 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2334 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2337 if (async->cb_mask & s->async->events) {
2338 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2339 wake_up_interruptible(&async->wait_head);
2340 if (s->subdev_flags & SDF_CMD_READ)
2341 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2342 if (s->subdev_flags & SDF_CMD_WRITE)
2343 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2346 async->cb_func(s->async->events, async->cb_arg);
2349 s->async->events = 0;
2351 EXPORT_SYMBOL(comedi_event);
2353 /* Note: the ->mutex is pre-locked on successful return */
2354 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2356 struct comedi_file_info *info;
2357 struct comedi_device *dev;
2358 struct device *csdev;
2361 info = kzalloc(sizeof(*info), GFP_KERNEL);
2363 return ERR_PTR(-ENOMEM);
2364 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2367 return ERR_PTR(-ENOMEM);
2370 comedi_device_init(dev);
2371 comedi_set_hw_dev(dev, hardware_device);
2372 mutex_lock(&dev->mutex);
2373 mutex_lock(&comedi_board_minor_table_lock);
2374 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2375 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2376 if (comedi_board_minor_table[i] == NULL) {
2377 comedi_board_minor_table[i] = info;
2381 mutex_unlock(&comedi_board_minor_table_lock);
2382 if (i == COMEDI_NUM_BOARD_MINORS) {
2383 mutex_unlock(&dev->mutex);
2384 comedi_device_cleanup(dev);
2387 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2388 return ERR_PTR(-EBUSY);
2391 csdev = device_create(comedi_class, hardware_device,
2392 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2394 dev->class_dev = csdev;
2395 dev_set_drvdata(csdev, info);
2397 /* Note: dev->mutex needs to be unlocked by the caller. */
2401 static void comedi_free_board_minor(unsigned minor)
2403 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2404 comedi_free_board_file_info(comedi_clear_board_minor(minor));
2407 void comedi_release_hardware_device(struct device *hardware_device)
2410 struct comedi_file_info *info;
2412 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2414 mutex_lock(&comedi_board_minor_table_lock);
2415 info = comedi_board_minor_table[minor];
2416 if (info && info->device->hw_dev == hardware_device) {
2417 comedi_board_minor_table[minor] = NULL;
2418 mutex_unlock(&comedi_board_minor_table_lock);
2419 comedi_free_board_file_info(info);
2422 mutex_unlock(&comedi_board_minor_table_lock);
2426 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2428 struct comedi_device *dev = s->device;
2429 struct comedi_file_info *info;
2430 struct device *csdev;
2433 info = kzalloc(sizeof(*info), GFP_KERNEL);
2437 if (s->subdev_flags & SDF_CMD_READ)
2438 info->read_subdevice = s;
2439 if (s->subdev_flags & SDF_CMD_WRITE)
2440 info->write_subdevice = s;
2441 mutex_lock(&comedi_subdevice_minor_table_lock);
2442 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2443 if (comedi_subdevice_minor_table[i] == NULL) {
2444 comedi_subdevice_minor_table[i] = info;
2448 mutex_unlock(&comedi_subdevice_minor_table_lock);
2449 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2451 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2454 i += COMEDI_NUM_BOARD_MINORS;
2456 csdev = device_create(comedi_class, dev->class_dev,
2457 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2458 dev->minor, s->index);
2460 s->class_dev = csdev;
2461 dev_set_drvdata(csdev, info);
2466 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2468 struct comedi_file_info *info;
2475 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2476 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2478 info = comedi_clear_subdevice_minor(s->minor);
2480 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2481 s->class_dev = NULL;
2486 static void comedi_cleanup_board_minors(void)
2490 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2491 comedi_free_board_minor(i);
2494 static int __init comedi_init(void)
2499 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2501 if (comedi_num_legacy_minors < 0 ||
2502 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2503 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2504 COMEDI_NUM_BOARD_MINORS);
2508 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2509 COMEDI_NUM_MINORS, "comedi");
2512 cdev_init(&comedi_cdev, &comedi_fops);
2513 comedi_cdev.owner = THIS_MODULE;
2514 kobject_set_name(&comedi_cdev.kobj, "comedi");
2515 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2516 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2520 comedi_class = class_create(THIS_MODULE, "comedi");
2521 if (IS_ERR(comedi_class)) {
2522 pr_err("comedi: failed to create class\n");
2523 cdev_del(&comedi_cdev);
2524 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2526 return PTR_ERR(comedi_class);
2529 comedi_class->dev_attrs = comedi_dev_attrs;
2531 /* XXX requires /proc interface */
2534 /* create devices files for legacy/manual use */
2535 for (i = 0; i < comedi_num_legacy_minors; i++) {
2536 struct comedi_device *dev;
2537 dev = comedi_alloc_board_minor(NULL);
2539 comedi_cleanup_board_minors();
2540 cdev_del(&comedi_cdev);
2541 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2543 return PTR_ERR(dev);
2545 /* comedi_alloc_board_minor() locked the mutex */
2546 mutex_unlock(&dev->mutex);
2552 module_init(comedi_init);
2554 static void __exit comedi_cleanup(void)
2558 comedi_cleanup_board_minors();
2559 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2560 BUG_ON(comedi_board_minor_table[i]);
2561 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2562 BUG_ON(comedi_subdevice_minor_table[i]);
2564 class_destroy(comedi_class);
2565 cdev_del(&comedi_cdev);
2566 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2568 comedi_proc_cleanup();
2570 module_exit(comedi_cleanup);
2572 MODULE_AUTHOR("http://www.comedi.org");
2573 MODULE_DESCRIPTION("Comedi core module");
2574 MODULE_LICENSE("GPL");