5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define __NO_VERSION__
27 #include "comedi_fops.h"
28 #include "comedi_compat32.h"
30 #include <linux/module.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/fcntl.h>
35 #include <linux/delay.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/kmod.h>
40 #include <linux/poll.h>
41 #include <linux/init.h>
42 #include <linux/device.h>
43 #include <linux/vmalloc.h>
45 #include "comedidev.h"
46 #include <linux/cdev.h>
49 #include <linux/uaccess.h>
51 /* #include "kvmem.h" */
53 MODULE_AUTHOR("http://www.comedi.org");
54 MODULE_DESCRIPTION("Comedi core module");
55 MODULE_LICENSE("GPL");
57 #ifdef CONFIG_COMEDI_DEBUG
59 module_param(comedi_debug, int, 0644);
62 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
63 static struct comedi_device_file_info
64 *comedi_file_info_table[COMEDI_NUM_MINORS];
66 static int do_devconfig_ioctl(comedi_device *dev, comedi_devconfig *arg);
67 static int do_bufconfig_ioctl(comedi_device *dev, void *arg);
68 static int do_devinfo_ioctl(comedi_device *dev, comedi_devinfo *arg,
70 static int do_subdinfo_ioctl(comedi_device *dev, comedi_subdinfo *arg,
72 static int do_chaninfo_ioctl(comedi_device *dev, comedi_chaninfo *arg);
73 static int do_bufinfo_ioctl(comedi_device *dev, void *arg);
74 static int do_cmd_ioctl(comedi_device *dev, void *arg, void *file);
75 static int do_lock_ioctl(comedi_device *dev, unsigned int arg, void *file);
76 static int do_unlock_ioctl(comedi_device *dev, unsigned int arg, void *file);
77 static int do_cancel_ioctl(comedi_device *dev, unsigned int arg, void *file);
78 static int do_cmdtest_ioctl(comedi_device *dev, void *arg, void *file);
79 static int do_insnlist_ioctl(comedi_device *dev, void *arg, void *file);
80 static int do_insn_ioctl(comedi_device *dev, void *arg, void *file);
81 static int do_poll_ioctl(comedi_device *dev, unsigned int subd, void *file);
83 extern void do_become_nonbusy(comedi_device *dev, comedi_subdevice *s);
84 static int do_cancel(comedi_device *dev, comedi_subdevice *s);
86 static int comedi_fasync(int fd, struct file *file, int on);
88 static int is_device_busy(comedi_device *dev);
90 #ifdef HAVE_UNLOCKED_IOCTL
91 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
94 static int comedi_ioctl(struct inode *inode, struct file *file,
95 unsigned int cmd, unsigned long arg)
98 const unsigned minor = iminor(file->f_dentry->d_inode);
99 struct comedi_device_file_info *dev_file_info =
100 comedi_get_device_file_info(minor);
101 comedi_device *dev = dev_file_info->device;
104 mutex_lock(&dev->mutex);
106 /* Device config is special, because it must work on
107 * an unconfigured device. */
108 if (cmd == COMEDI_DEVCONFIG) {
109 rc = do_devconfig_ioctl(dev, (void *)arg);
113 if (!dev->attached) {
114 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
120 case COMEDI_BUFCONFIG:
121 rc = do_bufconfig_ioctl(dev, (void *)arg);
124 rc = do_devinfo_ioctl(dev, (void *)arg, file);
126 case COMEDI_SUBDINFO:
127 rc = do_subdinfo_ioctl(dev, (void *)arg, file);
129 case COMEDI_CHANINFO:
130 rc = do_chaninfo_ioctl(dev, (void *)arg);
132 case COMEDI_RANGEINFO:
133 rc = do_rangeinfo_ioctl(dev, (void *)arg);
136 rc = do_bufinfo_ioctl(dev, (void *)arg);
139 rc = do_lock_ioctl(dev, arg, file);
142 rc = do_unlock_ioctl(dev, arg, file);
145 rc = do_cancel_ioctl(dev, arg, file);
148 rc = do_cmd_ioctl(dev, (void *)arg, file);
151 rc = do_cmdtest_ioctl(dev, (void *)arg, file);
153 case COMEDI_INSNLIST:
154 rc = do_insnlist_ioctl(dev, (void *)arg, file);
157 rc = do_insn_ioctl(dev, (void *)arg, file);
160 rc = do_poll_ioctl(dev, arg, file);
168 mutex_unlock(&dev->mutex);
177 pointer to devconfig structure
180 devconfig structure at arg
185 static int do_devconfig_ioctl(comedi_device *dev, comedi_devconfig *arg)
189 unsigned char *aux_data = NULL;
192 if (!capable(CAP_SYS_ADMIN))
196 if (is_device_busy(dev))
199 struct module *driver_module = dev->driver->module;
200 comedi_device_detach(dev);
201 module_put(driver_module);
206 if (copy_from_user(&it, arg, sizeof(comedi_devconfig)))
209 it.board_name[COMEDI_NAMELEN - 1] = 0;
211 if (comedi_aux_data(it.options, 0) &&
212 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
214 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
218 aux_data = vmalloc(aux_len);
222 if (copy_from_user(aux_data,
223 comedi_aux_data(it.options, 0), aux_len)) {
227 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
228 (unsigned long)aux_data;
229 if (sizeof(void *) > sizeof(int)) {
230 bit_shift = sizeof(int) * 8;
231 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
232 ((unsigned long)aux_data) >> bit_shift;
234 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
237 ret = comedi_device_attach(dev, &it);
239 if (!try_module_get(dev->driver->module)) {
240 comedi_device_detach(dev);
253 buffer configuration ioctl
256 pointer to bufconfig structure
262 modified bufconfig at arg
265 static int do_bufconfig_ioctl(comedi_device *dev, void *arg)
272 if (copy_from_user(&bc, arg, sizeof(comedi_bufconfig)))
275 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
278 s = dev->subdevices + bc.subdevice;
282 DPRINTK("subdevice does not have async capability\n");
288 if (bc.maximum_size) {
289 if (!capable(CAP_SYS_ADMIN))
292 async->max_bufsize = bc.maximum_size;
296 if (bc.size > async->max_bufsize)
300 DPRINTK("subdevice is busy, cannot resize buffer\n");
303 if (async->mmap_count) {
304 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
308 if (!async->prealloc_buf)
311 /* make sure buffer is an integral number of pages
313 bc.size = (bc.size + PAGE_SIZE - 1) & PAGE_MASK;
315 ret = comedi_buf_alloc(dev, s, bc.size);
320 ret = s->buf_change(dev, s, bc.size);
325 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
326 dev->minor, bc.subdevice, async->prealloc_bufsz);
329 bc.size = async->prealloc_bufsz;
330 bc.maximum_size = async->max_bufsize;
333 if (copy_to_user(arg, &bc, sizeof(comedi_bufconfig)))
344 pointer to devinfo structure
353 static int do_devinfo_ioctl(comedi_device *dev, comedi_devinfo *arg,
356 comedi_devinfo devinfo;
357 const unsigned minor = iminor(file->f_dentry->d_inode);
358 struct comedi_device_file_info *dev_file_info =
359 comedi_get_device_file_info(minor);
360 comedi_subdevice *read_subdev =
361 comedi_get_read_subdevice(dev_file_info);
362 comedi_subdevice *write_subdev =
363 comedi_get_write_subdevice(dev_file_info);
365 memset(&devinfo, 0, sizeof(devinfo));
367 /* fill devinfo structure */
368 devinfo.version_code = COMEDI_VERSION_CODE;
369 devinfo.n_subdevs = dev->n_subdevices;
370 memcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
371 memcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
374 devinfo.read_subdevice = read_subdev - dev->subdevices;
376 devinfo.read_subdevice = -1;
379 devinfo.write_subdevice = write_subdev - dev->subdevices;
381 devinfo.write_subdevice = -1;
383 if (copy_to_user(arg, &devinfo, sizeof(comedi_devinfo)))
394 pointer to array of subdevice info structures
400 array of subdevice info structures at arg
403 static int do_subdinfo_ioctl(comedi_device *dev, comedi_subdinfo *arg,
407 comedi_subdinfo *tmp, *us;
410 tmp = kcalloc(dev->n_subdevices, sizeof(comedi_subdinfo), GFP_KERNEL);
414 /* fill subdinfo structs */
415 for (i = 0; i < dev->n_subdevices; i++) {
416 s = dev->subdevices + i;
420 us->n_chan = s->n_chan;
421 us->subd_flags = s->subdev_flags;
422 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
423 us->subd_flags |= SDF_RUNNING;
424 #define TIMER_nanosec 5 /* backwards compatibility */
425 us->timer_type = TIMER_nanosec;
426 us->len_chanlist = s->len_chanlist;
427 us->maxdata = s->maxdata;
428 if (s->range_table) {
430 (i << 24) | (0 << 16) | (s->range_table->length);
432 us->range_type = 0; /* XXX */
434 us->flags = s->flags;
437 us->subd_flags |= SDF_BUSY;
439 us->subd_flags |= SDF_BUSY_OWNER;
441 us->subd_flags |= SDF_LOCKED;
443 us->subd_flags |= SDF_LOCK_OWNER;
444 if (!s->maxdata && s->maxdata_list)
445 us->subd_flags |= SDF_MAXDATA;
447 us->subd_flags |= SDF_FLAGS;
448 if (s->range_table_list)
449 us->subd_flags |= SDF_RANGETYPE;
451 us->subd_flags |= SDF_CMD;
453 if (s->insn_bits != &insn_inval)
454 us->insn_bits_support = COMEDI_SUPPORTED;
456 us->insn_bits_support = COMEDI_UNSUPPORTED;
458 us->settling_time_0 = s->settling_time_0;
461 ret = copy_to_user(arg, tmp,
462 dev->n_subdevices * sizeof(comedi_subdinfo));
466 return ret ? -EFAULT : 0;
474 pointer to chaninfo structure
477 chaninfo structure at arg
480 arrays at elements of chaninfo structure
483 static int do_chaninfo_ioctl(comedi_device *dev, comedi_chaninfo *arg)
488 if (copy_from_user(&it, arg, sizeof(comedi_chaninfo)))
491 if (it.subdev >= dev->n_subdevices)
493 s = dev->subdevices + it.subdev;
495 if (it.maxdata_list) {
496 if (s->maxdata || !s->maxdata_list)
498 if (copy_to_user(it.maxdata_list, s->maxdata_list,
499 s->n_chan * sizeof(lsampl_t)))
506 if (copy_to_user(it.flaglist, s->flaglist,
507 s->n_chan * sizeof(unsigned int)))
514 if (!s->range_table_list)
516 for (i = 0; i < s->n_chan; i++) {
519 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
520 (s->range_table_list[i]->length);
521 put_user(x, it.rangelist + i);
524 if (copy_to_user(it.rangelist, s->range_type_list,
525 s->n_chan*sizeof(unsigned int)))
535 buffer information ioctl
538 pointer to bufinfo structure
544 modified bufinfo at arg
547 static int do_bufinfo_ioctl(comedi_device *dev, void *arg)
553 if (copy_from_user(&bi, arg, sizeof(comedi_bufinfo)))
556 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
559 s = dev->subdevices + bi.subdevice;
563 DPRINTK("subdevice does not have async capability\n");
564 bi.buf_write_ptr = 0;
566 bi.buf_write_count = 0;
567 bi.buf_read_count = 0;
571 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
572 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
573 comedi_buf_read_free(async, bi.bytes_read);
575 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
577 && async->buf_write_count == async->buf_read_count) {
578 do_become_nonbusy(dev, s);
582 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
584 comedi_buf_write_alloc(async, bi.bytes_written);
585 comedi_buf_write_free(async, bi.bytes_written);
588 bi.buf_write_count = async->buf_write_count;
589 bi.buf_write_ptr = async->buf_write_ptr;
590 bi.buf_read_count = async->buf_read_count;
591 bi.buf_read_ptr = async->buf_read_ptr;
594 if (copy_to_user(arg, &bi, sizeof(comedi_bufinfo)))
600 static int parse_insn(comedi_device *dev, comedi_insn *insn, lsampl_t *data,
604 * synchronous instructions
607 * pointer to sync cmd structure
610 * sync cmd struct at arg
617 /* arbitrary limits */
618 #define MAX_SAMPLES 256
619 static int do_insnlist_ioctl(comedi_device *dev, void *arg, void *file)
621 comedi_insnlist insnlist;
622 comedi_insn *insns = NULL;
623 lsampl_t *data = NULL;
627 if (copy_from_user(&insnlist, arg, sizeof(comedi_insnlist)))
630 data = kmalloc(sizeof(lsampl_t) * MAX_SAMPLES, GFP_KERNEL);
632 DPRINTK("kmalloc failed\n");
637 insns = kmalloc(sizeof(comedi_insn) * insnlist.n_insns, GFP_KERNEL);
639 DPRINTK("kmalloc failed\n");
644 if (copy_from_user(insns, insnlist.insns,
645 sizeof(comedi_insn) * insnlist.n_insns)) {
646 DPRINTK("copy_from_user failed\n");
651 for (i = 0; i < insnlist.n_insns; i++) {
652 if (insns[i].n > MAX_SAMPLES) {
653 DPRINTK("number of samples too large\n");
657 if (insns[i].insn & INSN_MASK_WRITE) {
658 if (copy_from_user(data, insns[i].data,
659 insns[i].n * sizeof(lsampl_t))) {
660 DPRINTK("copy_from_user failed\n");
665 ret = parse_insn(dev, insns + i, data, file);
668 if (insns[i].insn & INSN_MASK_READ) {
669 if (copy_to_user(insns[i].data, data,
670 insns[i].n * sizeof(lsampl_t))) {
671 DPRINTK("copy_to_user failed\n");
689 static int check_insn_config_length(comedi_insn *insn, lsampl_t *data)
695 case INSN_CONFIG_DIO_OUTPUT:
696 case INSN_CONFIG_DIO_INPUT:
697 case INSN_CONFIG_DISARM:
698 case INSN_CONFIG_RESET:
702 case INSN_CONFIG_ARM:
703 case INSN_CONFIG_DIO_QUERY:
704 case INSN_CONFIG_BLOCK_SIZE:
705 case INSN_CONFIG_FILTER:
706 case INSN_CONFIG_SERIAL_CLOCK:
707 case INSN_CONFIG_BIDIRECTIONAL_DATA:
708 case INSN_CONFIG_ALT_SOURCE:
709 case INSN_CONFIG_SET_COUNTER_MODE:
710 case INSN_CONFIG_8254_READ_STATUS:
711 case INSN_CONFIG_SET_ROUTING:
712 case INSN_CONFIG_GET_ROUTING:
713 case INSN_CONFIG_GET_PWM_STATUS:
714 case INSN_CONFIG_PWM_SET_PERIOD:
715 case INSN_CONFIG_PWM_GET_PERIOD:
719 case INSN_CONFIG_SET_GATE_SRC:
720 case INSN_CONFIG_GET_GATE_SRC:
721 case INSN_CONFIG_SET_CLOCK_SRC:
722 case INSN_CONFIG_GET_CLOCK_SRC:
723 case INSN_CONFIG_SET_OTHER_SRC:
724 case INSN_CONFIG_GET_COUNTER_STATUS:
725 case INSN_CONFIG_PWM_SET_H_BRIDGE:
726 case INSN_CONFIG_PWM_GET_H_BRIDGE:
727 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
731 case INSN_CONFIG_PWM_OUTPUT:
732 case INSN_CONFIG_ANALOG_TRIG:
736 /* by default we allow the insn since we don't have checks for
737 * all possible cases yet */
739 rt_printk("comedi: no check for data length of config insn id "
740 "%i is implemented.\n"
741 " Add a check to %s in %s.\n"
742 " Assuming n=%i is correct.\n", data[0], __func__,
750 static int parse_insn(comedi_device *dev, comedi_insn *insn, lsampl_t *data,
757 if (insn->insn & INSN_MASK_SPECIAL) {
758 /* a non-subdevice instruction */
760 switch (insn->insn) {
770 do_gettimeofday(&tv);
772 data[1] = tv.tv_usec;
778 if (insn->n != 1 || data[0] >= 100000) {
782 udelay(data[0] / 1000);
790 if (insn->subdev >= dev->n_subdevices) {
791 DPRINTK("%d not usable subdevice\n",
796 s = dev->subdevices + insn->subdev;
798 DPRINTK("no async\n");
802 if (!s->async->inttrig) {
803 DPRINTK("no inttrig\n");
807 ret = s->async->inttrig(dev, s, insn->data[0]);
812 DPRINTK("invalid insn\n");
817 /* a subdevice instruction */
820 if (insn->subdev >= dev->n_subdevices) {
821 DPRINTK("subdevice %d out of range\n", insn->subdev);
825 s = dev->subdevices + insn->subdev;
827 if (s->type == COMEDI_SUBD_UNUSED) {
828 DPRINTK("%d not usable subdevice\n", insn->subdev);
833 /* are we locked? (ioctl lock) */
834 if (s->lock && s->lock != file) {
835 DPRINTK("device locked\n");
840 ret = check_chanlist(s, 1, &insn->chanspec);
843 DPRINTK("bad chanspec\n");
851 /* This looks arbitrary. It is. */
852 s->busy = &parse_insn;
853 switch (insn->insn) {
855 ret = s->insn_read(dev, s, insn, data);
858 maxdata = s->maxdata_list
859 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
861 for (i = 0; i < insn->n; ++i) {
862 if (data[i] > maxdata) {
864 DPRINTK("bad data value(s)\n");
869 ret = s->insn_write(dev, s, insn, data);
876 ret = s->insn_bits(dev, s, insn, data);
879 ret = check_insn_config_length(insn, data);
882 ret = s->insn_config(dev, s, insn, data);
898 * synchronous instructions
904 * comedi_insn struct at arg
910 static int do_insn_ioctl(comedi_device *dev, void *arg, void *file)
913 lsampl_t *data = NULL;
916 data = kmalloc(sizeof(lsampl_t) * MAX_SAMPLES, GFP_KERNEL);
922 if (copy_from_user(&insn, arg, sizeof(comedi_insn))) {
927 /* This is where the behavior of insn and insnlist deviate. */
928 if (insn.n > MAX_SAMPLES)
929 insn.n = MAX_SAMPLES;
930 if (insn.insn & INSN_MASK_WRITE) {
931 if (copy_from_user(data, insn.data, insn.n * sizeof(lsampl_t))) {
936 ret = parse_insn(dev, &insn, data, file);
939 if (insn.insn & INSN_MASK_READ) {
940 if (copy_to_user(insn.data, data, insn.n * sizeof(lsampl_t))) {
958 pointer to cmd structure
965 modified cmd structure at arg
968 static int do_cmd_ioctl(comedi_device *dev, void *arg, void *file)
974 unsigned int *chanlist_saver = NULL;
976 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
977 DPRINTK("bad cmd address\n");
980 /* save user's chanlist pointer so it can be restored later */
981 chanlist_saver = user_cmd.chanlist;
983 if (user_cmd.subdev >= dev->n_subdevices) {
984 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
988 s = dev->subdevices + user_cmd.subdev;
991 if (s->type == COMEDI_SUBD_UNUSED) {
992 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
996 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
997 DPRINTK("subdevice %i does not support commands\n",
1002 /* are we locked? (ioctl lock) */
1003 if (s->lock && s->lock != file) {
1004 DPRINTK("subdevice locked\n");
1010 DPRINTK("subdevice busy\n");
1015 /* make sure channel/gain list isn't too long */
1016 if (user_cmd.chanlist_len > s->len_chanlist) {
1017 DPRINTK("channel/gain list too long %u > %d\n",
1018 user_cmd.chanlist_len, s->len_chanlist);
1023 /* make sure channel/gain list isn't too short */
1024 if (user_cmd.chanlist_len < 1) {
1025 DPRINTK("channel/gain list too short %u < 1\n",
1026 user_cmd.chanlist_len);
1031 kfree(async->cmd.chanlist);
1032 async->cmd = user_cmd;
1033 async->cmd.data = NULL;
1034 /* load channel/gain list */
1035 async->cmd.chanlist =
1036 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1037 if (!async->cmd.chanlist) {
1038 DPRINTK("allocation failed\n");
1043 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1044 async->cmd.chanlist_len * sizeof(int))) {
1045 DPRINTK("fault reading chanlist\n");
1050 /* make sure each element in channel/gain list is valid */
1051 ret = check_chanlist(s, async->cmd.chanlist_len, async->cmd.chanlist);
1053 DPRINTK("bad chanlist\n");
1057 ret = s->do_cmdtest(dev, s, &async->cmd);
1059 if (async->cmd.flags & TRIG_BOGUS || ret) {
1060 DPRINTK("test returned %d\n", ret);
1061 user_cmd = async->cmd;
1062 /* restore chanlist pointer before copying back */
1063 user_cmd.chanlist = chanlist_saver;
1064 user_cmd.data = NULL;
1065 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1066 DPRINTK("fault writing cmd\n");
1074 if (!async->prealloc_bufsz) {
1076 DPRINTK("no buffer (?)\n");
1080 comedi_reset_async_buf(async);
1083 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1085 if (async->cmd.flags & TRIG_WAKE_EOS)
1086 async->cb_mask |= COMEDI_CB_EOS;
1088 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1090 #ifdef CONFIG_COMEDI_RT
1091 if (async->cmd.flags & TRIG_RT) {
1092 if (comedi_switch_to_rt(dev) == 0)
1093 comedi_set_subdevice_runflags(s, SRF_RT, SRF_RT);
1097 ret = s->do_cmd(dev, s);
1102 do_become_nonbusy(dev, s);
1109 command testing ioctl
1112 pointer to cmd structure
1115 cmd structure at arg
1119 modified cmd structure at arg
1122 static int do_cmdtest_ioctl(comedi_device *dev, void *arg, void *file)
1124 comedi_cmd user_cmd;
1125 comedi_subdevice *s;
1127 unsigned int *chanlist = NULL;
1128 unsigned int *chanlist_saver = NULL;
1130 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
1131 DPRINTK("bad cmd address\n");
1134 /* save user's chanlist pointer so it can be restored later */
1135 chanlist_saver = user_cmd.chanlist;
1137 if (user_cmd.subdev >= dev->n_subdevices) {
1138 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1142 s = dev->subdevices + user_cmd.subdev;
1143 if (s->type == COMEDI_SUBD_UNUSED) {
1144 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1148 if (!s->do_cmd || !s->do_cmdtest) {
1149 DPRINTK("subdevice %i does not support commands\n",
1154 /* make sure channel/gain list isn't too long */
1155 if (user_cmd.chanlist_len > s->len_chanlist) {
1156 DPRINTK("channel/gain list too long %d > %d\n",
1157 user_cmd.chanlist_len, s->len_chanlist);
1162 /* load channel/gain list */
1163 if (user_cmd.chanlist) {
1165 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1167 DPRINTK("allocation failed\n");
1172 if (copy_from_user(chanlist, user_cmd.chanlist,
1173 user_cmd.chanlist_len * sizeof(int))) {
1174 DPRINTK("fault reading chanlist\n");
1179 /* make sure each element in channel/gain list is valid */
1180 ret = check_chanlist(s, user_cmd.chanlist_len, chanlist);
1182 DPRINTK("bad chanlist\n");
1186 user_cmd.chanlist = chanlist;
1189 ret = s->do_cmdtest(dev, s, &user_cmd);
1191 /* restore chanlist pointer before copying back */
1192 user_cmd.chanlist = chanlist_saver;
1194 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1195 DPRINTK("bad cmd address\n");
1220 static int do_lock_ioctl(comedi_device *dev, unsigned int arg, void *file)
1223 unsigned long flags;
1224 comedi_subdevice *s;
1226 if (arg >= dev->n_subdevices)
1228 s = dev->subdevices + arg;
1230 comedi_spin_lock_irqsave(&s->spin_lock, flags);
1231 if (s->busy || s->lock)
1235 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
1242 ret = s->lock_f(dev, s);
1261 This function isn't protected by the semaphore, since
1262 we already own the lock.
1264 static int do_unlock_ioctl(comedi_device *dev, unsigned int arg, void *file)
1266 comedi_subdevice *s;
1268 if (arg >= dev->n_subdevices)
1270 s = dev->subdevices + arg;
1275 if (s->lock && s->lock != file)
1278 if (s->lock == file) {
1292 cancel acquisition ioctl
1304 static int do_cancel_ioctl(comedi_device *dev, unsigned int arg, void *file)
1306 comedi_subdevice *s;
1308 if (arg >= dev->n_subdevices)
1310 s = dev->subdevices + arg;
1311 if (s->async == NULL)
1314 if (s->lock && s->lock != file)
1320 if (s->busy != file)
1323 return do_cancel(dev, s);
1328 instructs driver to synchronize buffers
1340 static int do_poll_ioctl(comedi_device *dev, unsigned int arg, void *file)
1342 comedi_subdevice *s;
1344 if (arg >= dev->n_subdevices)
1346 s = dev->subdevices + arg;
1348 if (s->lock && s->lock != file)
1354 if (s->busy != file)
1358 return s->poll(dev, s);
1363 static int do_cancel(comedi_device *dev, comedi_subdevice *s)
1367 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1368 ret = s->cancel(dev, s);
1370 do_become_nonbusy(dev, s);
1375 void comedi_unmap(struct vm_area_struct *area)
1377 comedi_async *async;
1380 async = area->vm_private_data;
1381 dev = async->subdevice->device;
1383 mutex_lock(&dev->mutex);
1384 async->mmap_count--;
1385 mutex_unlock(&dev->mutex);
1388 static struct vm_operations_struct comedi_vm_ops = {
1389 .close = comedi_unmap,
1392 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1394 const unsigned minor = iminor(file->f_dentry->d_inode);
1395 struct comedi_device_file_info *dev_file_info =
1396 comedi_get_device_file_info(minor);
1397 comedi_device *dev = dev_file_info->device;
1398 comedi_async *async = NULL;
1399 unsigned long start = vma->vm_start;
1404 comedi_subdevice *s;
1406 mutex_lock(&dev->mutex);
1407 if (!dev->attached) {
1408 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1412 if (vma->vm_flags & VM_WRITE)
1413 s = comedi_get_write_subdevice(dev_file_info);
1415 s = comedi_get_read_subdevice(dev_file_info);
1422 if (async == NULL) {
1427 if (vma->vm_pgoff != 0) {
1428 DPRINTK("comedi: mmap() offset must be 0.\n");
1433 size = vma->vm_end - vma->vm_start;
1434 if (size > async->prealloc_bufsz) {
1438 if (size & (~PAGE_MASK)) {
1443 n_pages = size >> PAGE_SHIFT;
1444 for (i = 0; i < n_pages; ++i) {
1445 if (remap_pfn_range(vma, start,
1446 page_to_pfn(virt_to_page(async->
1449 PAGE_SIZE, PAGE_SHARED)) {
1456 vma->vm_ops = &comedi_vm_ops;
1457 vma->vm_private_data = async;
1459 async->mmap_count++;
1463 mutex_unlock(&dev->mutex);
1467 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1469 unsigned int mask = 0;
1470 const unsigned minor = iminor(file->f_dentry->d_inode);
1471 struct comedi_device_file_info *dev_file_info =
1472 comedi_get_device_file_info(minor);
1473 comedi_device *dev = dev_file_info->device;
1474 comedi_subdevice *read_subdev;
1475 comedi_subdevice *write_subdev;
1477 mutex_lock(&dev->mutex);
1478 if (!dev->attached) {
1479 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1480 mutex_unlock(&dev->mutex);
1485 read_subdev = comedi_get_read_subdevice(dev_file_info);
1487 poll_wait(file, &read_subdev->async->wait_head, wait);
1488 if (!read_subdev->busy
1489 || comedi_buf_read_n_available(read_subdev->async) > 0
1490 || !(comedi_get_subdevice_runflags(read_subdev) &
1492 mask |= POLLIN | POLLRDNORM;
1495 write_subdev = comedi_get_write_subdevice(dev_file_info);
1497 poll_wait(file, &write_subdev->async->wait_head, wait);
1498 comedi_buf_write_alloc(write_subdev->async,
1499 write_subdev->async->prealloc_bufsz);
1500 if (!write_subdev->busy
1501 || !(comedi_get_subdevice_runflags(write_subdev) &
1503 || comedi_buf_write_n_allocated(write_subdev->async) >=
1504 bytes_per_sample(write_subdev->async->subdevice)) {
1505 mask |= POLLOUT | POLLWRNORM;
1509 mutex_unlock(&dev->mutex);
1513 static ssize_t comedi_write(struct file *file, const char *buf, size_t nbytes,
1516 comedi_subdevice *s;
1517 comedi_async *async;
1518 int n, m, count = 0, retval = 0;
1519 DECLARE_WAITQUEUE(wait, current);
1520 const unsigned minor = iminor(file->f_dentry->d_inode);
1521 struct comedi_device_file_info *dev_file_info =
1522 comedi_get_device_file_info(minor);
1523 comedi_device *dev = dev_file_info->device;
1525 if (!dev->attached) {
1526 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1531 s = comedi_get_write_subdevice(dev_file_info);
1546 if (s->busy != file) {
1550 add_wait_queue(&async->wait_head, &wait);
1551 while (nbytes > 0 && !retval) {
1552 set_current_state(TASK_INTERRUPTIBLE);
1557 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1558 m = async->prealloc_bufsz - async->buf_write_ptr;
1559 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1560 if (m > comedi_buf_write_n_allocated(async))
1561 m = comedi_buf_write_n_allocated(async);
1566 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1567 if (comedi_get_subdevice_runflags(s) &
1573 do_become_nonbusy(dev, s);
1576 if (file->f_flags & O_NONBLOCK) {
1580 if (signal_pending(current)) {
1581 retval = -ERESTARTSYS;
1587 if (s->busy != file) {
1594 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1600 comedi_buf_write_free(async, n);
1606 break; /* makes device work like a pipe */
1608 set_current_state(TASK_RUNNING);
1609 remove_wait_queue(&async->wait_head, &wait);
1612 return count ? count : retval;
1615 static ssize_t comedi_read(struct file *file, char *buf, size_t nbytes,
1618 comedi_subdevice *s;
1619 comedi_async *async;
1620 int n, m, count = 0, retval = 0;
1621 DECLARE_WAITQUEUE(wait, current);
1622 const unsigned minor = iminor(file->f_dentry->d_inode);
1623 struct comedi_device_file_info *dev_file_info =
1624 comedi_get_device_file_info(minor);
1625 comedi_device *dev = dev_file_info->device;
1627 if (!dev->attached) {
1628 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1633 s = comedi_get_read_subdevice(dev_file_info);
1647 if (s->busy != file) {
1652 add_wait_queue(&async->wait_head, &wait);
1653 while (nbytes > 0 && !retval) {
1654 set_current_state(TASK_INTERRUPTIBLE);
1658 m = comedi_buf_read_n_available(async);
1659 /* printk("%d available\n",m); */
1660 if (async->buf_read_ptr + m > async->prealloc_bufsz)
1661 m = async->prealloc_bufsz - async->buf_read_ptr;
1662 /* printk("%d contiguous\n",m); */
1667 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1668 do_become_nonbusy(dev, s);
1669 if (comedi_get_subdevice_runflags(s) &
1677 if (file->f_flags & O_NONBLOCK) {
1681 if (signal_pending(current)) {
1682 retval = -ERESTARTSYS;
1690 if (s->busy != file) {
1696 m = copy_to_user(buf, async->prealloc_buf +
1697 async->buf_read_ptr, n);
1703 comedi_buf_read_alloc(async, n);
1704 comedi_buf_read_free(async, n);
1710 break; /* makes device work like a pipe */
1712 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
1713 async->buf_read_count - async->buf_write_count == 0) {
1714 do_become_nonbusy(dev, s);
1716 set_current_state(TASK_RUNNING);
1717 remove_wait_queue(&async->wait_head, &wait);
1720 return count ? count : retval;
1724 This function restores a subdevice to an idle state.
1726 void do_become_nonbusy(comedi_device *dev, comedi_subdevice *s)
1728 comedi_async *async = s->async;
1730 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1731 #ifdef CONFIG_COMEDI_RT
1732 if (comedi_get_subdevice_runflags(s) & SRF_RT) {
1733 comedi_switch_to_non_rt(dev);
1734 comedi_set_subdevice_runflags(s, SRF_RT, 0);
1738 comedi_reset_async_buf(async);
1739 async->inttrig = NULL;
1742 "BUG: (?) do_become_nonbusy called with async=0\n");
1748 static int comedi_open(struct inode *inode, struct file *file)
1751 const unsigned minor = iminor(inode);
1752 struct comedi_device_file_info *dev_file_info =
1753 comedi_get_device_file_info(minor);
1754 comedi_device *dev = dev_file_info->device;
1756 DPRINTK("invalid minor number\n");
1760 /* This is slightly hacky, but we want module autoloading
1762 * case: user opens device, attached -> ok
1763 * case: user opens device, unattached, in_request_module=0 -> autoload
1764 * case: user opens device, unattached, in_request_module=1 -> fail
1765 * case: root opens device, attached -> ok
1766 * case: root opens device, unattached, in_request_module=1 -> ok
1767 * (typically called from modprobe)
1768 * case: root opens device, unattached, in_request_module=0 -> autoload
1770 * The last could be changed to "-> ok", which would deny root
1773 mutex_lock(&dev->mutex);
1776 if (!capable(CAP_SYS_MODULE) && dev->in_request_module) {
1777 DPRINTK("in request module\n");
1778 mutex_unlock(&dev->mutex);
1781 if (capable(CAP_SYS_MODULE) && dev->in_request_module)
1784 dev->in_request_module = 1;
1786 sprintf(mod, "char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1788 mutex_unlock(&dev->mutex);
1789 request_module(mod);
1790 mutex_lock(&dev->mutex);
1793 dev->in_request_module = 0;
1795 if (!dev->attached && !capable(CAP_SYS_MODULE)) {
1796 DPRINTK("not attached and not CAP_SYS_MODULE\n");
1797 mutex_unlock(&dev->mutex);
1801 __module_get(THIS_MODULE);
1803 if (dev->attached) {
1804 if (!try_module_get(dev->driver->module)) {
1805 module_put(THIS_MODULE);
1806 mutex_unlock(&dev->mutex);
1811 if (dev->attached && dev->use_count == 0 && dev->open)
1816 mutex_unlock(&dev->mutex);
1821 static int comedi_close(struct inode *inode, struct file *file)
1823 const unsigned minor = iminor(inode);
1824 struct comedi_device_file_info *dev_file_info =
1825 comedi_get_device_file_info(minor);
1826 comedi_device *dev = dev_file_info->device;
1827 comedi_subdevice *s = NULL;
1830 mutex_lock(&dev->mutex);
1832 if (dev->subdevices) {
1833 for (i = 0; i < dev->n_subdevices; i++) {
1834 s = dev->subdevices + i;
1836 if (s->busy == file)
1838 if (s->lock == file)
1842 if (dev->attached && dev->use_count == 1 && dev->close)
1845 module_put(THIS_MODULE);
1847 module_put(dev->driver->module);
1851 mutex_unlock(&dev->mutex);
1853 if (file->f_flags & FASYNC)
1854 comedi_fasync(-1, file, 0);
1859 static int comedi_fasync(int fd, struct file *file, int on)
1861 const unsigned minor = iminor(file->f_dentry->d_inode);
1862 struct comedi_device_file_info *dev_file_info =
1863 comedi_get_device_file_info(minor);
1865 comedi_device *dev = dev_file_info->device;
1867 return fasync_helper(fd, file, on, &dev->async_queue);
1870 const struct file_operations comedi_fops = {
1871 .owner = THIS_MODULE,
1872 #ifdef HAVE_UNLOCKED_IOCTL
1873 .unlocked_ioctl = comedi_unlocked_ioctl,
1875 .ioctl = comedi_ioctl,
1877 #ifdef HAVE_COMPAT_IOCTL
1878 .compat_ioctl = comedi_compat_ioctl,
1880 .open = comedi_open,
1881 .release = comedi_close,
1882 .read = comedi_read,
1883 .write = comedi_write,
1884 .mmap = comedi_mmap,
1885 .poll = comedi_poll,
1886 .fasync = comedi_fasync,
1889 struct class *comedi_class;
1890 static struct cdev comedi_cdev;
1892 static void comedi_cleanup_legacy_minors(void)
1896 for (i = 0; i < COMEDI_NUM_LEGACY_MINORS; i++)
1897 comedi_free_board_minor(i);
1900 static int __init comedi_init(void)
1905 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
1906 " - http://www.comedi.org\n");
1908 memset(comedi_file_info_table, 0,
1909 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
1911 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1912 COMEDI_NUM_MINORS, "comedi");
1915 cdev_init(&comedi_cdev, &comedi_fops);
1916 comedi_cdev.owner = THIS_MODULE;
1917 kobject_set_name(&comedi_cdev.kobj, "comedi");
1918 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
1919 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1923 comedi_class = class_create(THIS_MODULE, "comedi");
1924 if (IS_ERR(comedi_class)) {
1925 printk("comedi: failed to create class");
1926 cdev_del(&comedi_cdev);
1927 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1929 return PTR_ERR(comedi_class);
1932 /* XXX requires /proc interface */
1935 /* create devices files for legacy/manual use */
1936 for (i = 0; i < COMEDI_NUM_LEGACY_MINORS; i++) {
1938 minor = comedi_alloc_board_minor(NULL);
1940 comedi_cleanup_legacy_minors();
1941 cdev_del(&comedi_cdev);
1942 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1950 comedi_register_ioctl32();
1955 static void __exit comedi_cleanup(void)
1959 comedi_cleanup_legacy_minors();
1960 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
1961 BUG_ON(comedi_file_info_table[i]);
1964 class_destroy(comedi_class);
1965 cdev_del(&comedi_cdev);
1966 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
1968 comedi_proc_cleanup();
1970 comedi_rt_cleanup();
1972 comedi_unregister_ioctl32();
1975 module_init(comedi_init);
1976 module_exit(comedi_cleanup);
1978 void comedi_error(const comedi_device *dev, const char *s)
1980 rt_printk("comedi%d: %s: %s\n", dev->minor, dev->driver->driver_name,
1984 void comedi_event(comedi_device *dev, comedi_subdevice *s)
1986 comedi_async *async = s->async;
1987 unsigned runflags = 0;
1988 unsigned runflags_mask = 0;
1990 /* DPRINTK("comedi_event 0x%x\n",mask); */
1992 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
1996 events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
1997 runflags_mask |= SRF_RUNNING;
1999 /* remember if an error event has occured, so an error
2000 * can be returned the next time the user does a read() */
2001 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2002 runflags_mask |= SRF_ERROR;
2003 runflags |= SRF_ERROR;
2005 if (runflags_mask) {
2006 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2007 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2010 if (async->cb_mask & s->async->events) {
2011 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2014 #ifdef CONFIG_COMEDI_RT
2016 comedi_rt_pend_wakeup(&async->wait_head);
2019 ("BUG: comedi_event() code unreachable\n");
2022 wake_up_interruptible(&async->wait_head);
2023 if (s->subdev_flags & SDF_CMD_READ) {
2024 kill_fasync(&dev->async_queue, SIGIO,
2027 if (s->subdev_flags & SDF_CMD_WRITE) {
2028 kill_fasync(&dev->async_queue, SIGIO,
2034 async->cb_func(s->async->events, async->cb_arg);
2035 /* XXX bug here. If subdevice A is rt, and
2036 * subdevice B tries to callback to a normal
2037 * linux kernel function, it will be at the
2038 * wrong priority. Since this isn't very
2039 * common, I'm not going to worry about it. */
2042 s->async->events = 0;
2045 void comedi_set_subdevice_runflags(comedi_subdevice *s, unsigned mask,
2048 unsigned long flags;
2050 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2051 s->runflags &= ~mask;
2052 s->runflags |= (bits & mask);
2053 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2056 unsigned comedi_get_subdevice_runflags(comedi_subdevice *s)
2058 unsigned long flags;
2061 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2062 runflags = s->runflags;
2063 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2067 static int is_device_busy(comedi_device *dev)
2069 comedi_subdevice *s;
2075 for (i = 0; i < dev->n_subdevices; i++) {
2076 s = dev->subdevices + i;
2079 if (s->async && s->async->mmap_count)
2086 void comedi_device_init(comedi_device *dev)
2088 memset(dev, 0, sizeof(comedi_device));
2089 spin_lock_init(&dev->spinlock);
2090 mutex_init(&dev->mutex);
2094 void comedi_device_cleanup(comedi_device *dev)
2098 mutex_lock(&dev->mutex);
2099 comedi_device_detach(dev);
2100 mutex_unlock(&dev->mutex);
2101 mutex_destroy(&dev->mutex);
2104 int comedi_alloc_board_minor(struct device *hardware_device)
2106 unsigned long flags;
2107 struct comedi_device_file_info *info;
2108 device_create_result_type *csdev;
2111 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2114 info->device = kzalloc(sizeof(comedi_device), GFP_KERNEL);
2115 if (info->device == NULL) {
2119 comedi_device_init(info->device);
2120 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2121 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2122 if (comedi_file_info_table[i] == NULL) {
2123 comedi_file_info_table[i] = info;
2127 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2128 if (i == COMEDI_NUM_BOARD_MINORS) {
2129 comedi_device_cleanup(info->device);
2130 kfree(info->device);
2133 ("comedi: error: ran out of minor numbers for board device files.\n");
2136 info->device->minor = i;
2137 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
2138 MKDEV(COMEDI_MAJOR, i), NULL,
2139 hardware_device, "comedi%i", i);
2141 info->device->class_dev = csdev;
2146 void comedi_free_board_minor(unsigned minor)
2148 unsigned long flags;
2149 struct comedi_device_file_info *info;
2151 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2152 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2153 info = comedi_file_info_table[minor];
2154 comedi_file_info_table[minor] = NULL;
2155 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2158 comedi_device *dev = info->device;
2160 if (dev->class_dev) {
2161 device_destroy(comedi_class,
2162 MKDEV(COMEDI_MAJOR, dev->minor));
2164 comedi_device_cleanup(dev);
2171 int comedi_alloc_subdevice_minor(comedi_device *dev, comedi_subdevice *s)
2173 unsigned long flags;
2174 struct comedi_device_file_info *info;
2175 device_create_result_type *csdev;
2178 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2182 info->read_subdevice = s;
2183 info->write_subdevice = s;
2184 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2185 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2186 if (comedi_file_info_table[i] == NULL) {
2187 comedi_file_info_table[i] = info;
2191 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2192 if (i == COMEDI_NUM_MINORS) {
2195 ("comedi: error: ran out of minor numbers for board device files.\n");
2199 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
2200 MKDEV(COMEDI_MAJOR, i), NULL, NULL,
2201 "comedi%i_subd%i", dev->minor,
2202 (int)(s - dev->subdevices));
2204 s->class_dev = csdev;
2209 void comedi_free_subdevice_minor(comedi_subdevice *s)
2211 unsigned long flags;
2212 struct comedi_device_file_info *info;
2219 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2220 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2222 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2223 info = comedi_file_info_table[s->minor];
2224 comedi_file_info_table[s->minor] = NULL;
2225 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2228 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2229 s->class_dev = NULL;
2234 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2236 unsigned long flags;
2237 struct comedi_device_file_info *info;
2239 BUG_ON(minor >= COMEDI_NUM_MINORS);
2240 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2241 info = comedi_file_info_table[minor];
2242 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);