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 int comedi_autoconfig = 1;
63 module_param(comedi_autoconfig, bool, 0444);
65 int comedi_num_legacy_minors = 0;
66 module_param(comedi_num_legacy_minors, int, 0444);
68 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
69 static struct comedi_device_file_info
70 *comedi_file_info_table[COMEDI_NUM_MINORS];
72 static int do_devconfig_ioctl(comedi_device *dev, comedi_devconfig *arg);
73 static int do_bufconfig_ioctl(comedi_device *dev, void *arg);
74 static int do_devinfo_ioctl(comedi_device *dev, comedi_devinfo *arg,
76 static int do_subdinfo_ioctl(comedi_device *dev, comedi_subdinfo *arg,
78 static int do_chaninfo_ioctl(comedi_device *dev, comedi_chaninfo *arg);
79 static int do_bufinfo_ioctl(comedi_device *dev, void *arg);
80 static int do_cmd_ioctl(comedi_device *dev, void *arg, void *file);
81 static int do_lock_ioctl(comedi_device *dev, unsigned int arg, void *file);
82 static int do_unlock_ioctl(comedi_device *dev, unsigned int arg, void *file);
83 static int do_cancel_ioctl(comedi_device *dev, unsigned int arg, void *file);
84 static int do_cmdtest_ioctl(comedi_device *dev, void *arg, void *file);
85 static int do_insnlist_ioctl(comedi_device *dev, void *arg, void *file);
86 static int do_insn_ioctl(comedi_device *dev, void *arg, void *file);
87 static int do_poll_ioctl(comedi_device *dev, unsigned int subd, void *file);
89 extern void do_become_nonbusy(comedi_device *dev, comedi_subdevice *s);
90 static int do_cancel(comedi_device *dev, comedi_subdevice *s);
92 static int comedi_fasync(int fd, struct file *file, int on);
94 static int is_device_busy(comedi_device *dev);
96 #ifdef HAVE_UNLOCKED_IOCTL
97 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
100 static int comedi_ioctl(struct inode *inode, struct file *file,
101 unsigned int cmd, unsigned long arg)
104 const unsigned minor = iminor(file->f_dentry->d_inode);
105 struct comedi_device_file_info *dev_file_info =
106 comedi_get_device_file_info(minor);
107 comedi_device *dev = dev_file_info->device;
110 mutex_lock(&dev->mutex);
112 /* Device config is special, because it must work on
113 * an unconfigured device. */
114 if (cmd == COMEDI_DEVCONFIG) {
115 rc = do_devconfig_ioctl(dev, (void *)arg);
119 if (!dev->attached) {
120 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
126 case COMEDI_BUFCONFIG:
127 rc = do_bufconfig_ioctl(dev, (void *)arg);
130 rc = do_devinfo_ioctl(dev, (void *)arg, file);
132 case COMEDI_SUBDINFO:
133 rc = do_subdinfo_ioctl(dev, (void *)arg, file);
135 case COMEDI_CHANINFO:
136 rc = do_chaninfo_ioctl(dev, (void *)arg);
138 case COMEDI_RANGEINFO:
139 rc = do_rangeinfo_ioctl(dev, (void *)arg);
142 rc = do_bufinfo_ioctl(dev, (void *)arg);
145 rc = do_lock_ioctl(dev, arg, file);
148 rc = do_unlock_ioctl(dev, arg, file);
151 rc = do_cancel_ioctl(dev, arg, file);
154 rc = do_cmd_ioctl(dev, (void *)arg, file);
157 rc = do_cmdtest_ioctl(dev, (void *)arg, file);
159 case COMEDI_INSNLIST:
160 rc = do_insnlist_ioctl(dev, (void *)arg, file);
163 rc = do_insn_ioctl(dev, (void *)arg, file);
166 rc = do_poll_ioctl(dev, arg, file);
174 mutex_unlock(&dev->mutex);
183 pointer to devconfig structure
186 devconfig structure at arg
191 static int do_devconfig_ioctl(comedi_device *dev, comedi_devconfig *arg)
195 unsigned char *aux_data = NULL;
198 if (!capable(CAP_SYS_ADMIN))
202 if (is_device_busy(dev))
205 struct module *driver_module = dev->driver->module;
206 comedi_device_detach(dev);
207 module_put(driver_module);
212 if (copy_from_user(&it, arg, sizeof(comedi_devconfig)))
215 it.board_name[COMEDI_NAMELEN - 1] = 0;
217 if (comedi_aux_data(it.options, 0) &&
218 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
220 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
224 aux_data = vmalloc(aux_len);
228 if (copy_from_user(aux_data,
229 comedi_aux_data(it.options, 0), aux_len)) {
233 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
234 (unsigned long)aux_data;
235 if (sizeof(void *) > sizeof(int)) {
236 bit_shift = sizeof(int) * 8;
237 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
238 ((unsigned long)aux_data) >> bit_shift;
240 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
243 ret = comedi_device_attach(dev, &it);
245 if (!try_module_get(dev->driver->module)) {
246 comedi_device_detach(dev);
259 buffer configuration ioctl
262 pointer to bufconfig structure
268 modified bufconfig at arg
271 static int do_bufconfig_ioctl(comedi_device *dev, void *arg)
278 if (copy_from_user(&bc, arg, sizeof(comedi_bufconfig)))
281 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
284 s = dev->subdevices + bc.subdevice;
288 DPRINTK("subdevice does not have async capability\n");
294 if (bc.maximum_size) {
295 if (!capable(CAP_SYS_ADMIN))
298 async->max_bufsize = bc.maximum_size;
302 if (bc.size > async->max_bufsize)
306 DPRINTK("subdevice is busy, cannot resize buffer\n");
309 if (async->mmap_count) {
310 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
314 if (!async->prealloc_buf)
317 /* make sure buffer is an integral number of pages
319 bc.size = (bc.size + PAGE_SIZE - 1) & PAGE_MASK;
321 ret = comedi_buf_alloc(dev, s, bc.size);
326 ret = s->buf_change(dev, s, bc.size);
331 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
332 dev->minor, bc.subdevice, async->prealloc_bufsz);
335 bc.size = async->prealloc_bufsz;
336 bc.maximum_size = async->max_bufsize;
339 if (copy_to_user(arg, &bc, sizeof(comedi_bufconfig)))
350 pointer to devinfo structure
359 static int do_devinfo_ioctl(comedi_device *dev, comedi_devinfo *arg,
362 comedi_devinfo devinfo;
363 const unsigned minor = iminor(file->f_dentry->d_inode);
364 struct comedi_device_file_info *dev_file_info =
365 comedi_get_device_file_info(minor);
366 comedi_subdevice *read_subdev =
367 comedi_get_read_subdevice(dev_file_info);
368 comedi_subdevice *write_subdev =
369 comedi_get_write_subdevice(dev_file_info);
371 memset(&devinfo, 0, sizeof(devinfo));
373 /* fill devinfo structure */
374 devinfo.version_code = COMEDI_VERSION_CODE;
375 devinfo.n_subdevs = dev->n_subdevices;
376 memcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
377 memcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
380 devinfo.read_subdevice = read_subdev - dev->subdevices;
382 devinfo.read_subdevice = -1;
385 devinfo.write_subdevice = write_subdev - dev->subdevices;
387 devinfo.write_subdevice = -1;
389 if (copy_to_user(arg, &devinfo, sizeof(comedi_devinfo)))
400 pointer to array of subdevice info structures
406 array of subdevice info structures at arg
409 static int do_subdinfo_ioctl(comedi_device *dev, comedi_subdinfo *arg,
413 comedi_subdinfo *tmp, *us;
416 tmp = kcalloc(dev->n_subdevices, sizeof(comedi_subdinfo), GFP_KERNEL);
420 /* fill subdinfo structs */
421 for (i = 0; i < dev->n_subdevices; i++) {
422 s = dev->subdevices + i;
426 us->n_chan = s->n_chan;
427 us->subd_flags = s->subdev_flags;
428 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
429 us->subd_flags |= SDF_RUNNING;
430 #define TIMER_nanosec 5 /* backwards compatibility */
431 us->timer_type = TIMER_nanosec;
432 us->len_chanlist = s->len_chanlist;
433 us->maxdata = s->maxdata;
434 if (s->range_table) {
436 (i << 24) | (0 << 16) | (s->range_table->length);
438 us->range_type = 0; /* XXX */
440 us->flags = s->flags;
443 us->subd_flags |= SDF_BUSY;
445 us->subd_flags |= SDF_BUSY_OWNER;
447 us->subd_flags |= SDF_LOCKED;
449 us->subd_flags |= SDF_LOCK_OWNER;
450 if (!s->maxdata && s->maxdata_list)
451 us->subd_flags |= SDF_MAXDATA;
453 us->subd_flags |= SDF_FLAGS;
454 if (s->range_table_list)
455 us->subd_flags |= SDF_RANGETYPE;
457 us->subd_flags |= SDF_CMD;
459 if (s->insn_bits != &insn_inval)
460 us->insn_bits_support = COMEDI_SUPPORTED;
462 us->insn_bits_support = COMEDI_UNSUPPORTED;
464 us->settling_time_0 = s->settling_time_0;
467 ret = copy_to_user(arg, tmp,
468 dev->n_subdevices * sizeof(comedi_subdinfo));
472 return ret ? -EFAULT : 0;
480 pointer to chaninfo structure
483 chaninfo structure at arg
486 arrays at elements of chaninfo structure
489 static int do_chaninfo_ioctl(comedi_device *dev, comedi_chaninfo *arg)
494 if (copy_from_user(&it, arg, sizeof(comedi_chaninfo)))
497 if (it.subdev >= dev->n_subdevices)
499 s = dev->subdevices + it.subdev;
501 if (it.maxdata_list) {
502 if (s->maxdata || !s->maxdata_list)
504 if (copy_to_user(it.maxdata_list, s->maxdata_list,
505 s->n_chan * sizeof(lsampl_t)))
512 if (copy_to_user(it.flaglist, s->flaglist,
513 s->n_chan * sizeof(unsigned int)))
520 if (!s->range_table_list)
522 for (i = 0; i < s->n_chan; i++) {
525 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
526 (s->range_table_list[i]->length);
527 put_user(x, it.rangelist + i);
530 if (copy_to_user(it.rangelist, s->range_type_list,
531 s->n_chan*sizeof(unsigned int)))
541 buffer information ioctl
544 pointer to bufinfo structure
550 modified bufinfo at arg
553 static int do_bufinfo_ioctl(comedi_device *dev, void *arg)
559 if (copy_from_user(&bi, arg, sizeof(comedi_bufinfo)))
562 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
565 s = dev->subdevices + bi.subdevice;
569 DPRINTK("subdevice does not have async capability\n");
570 bi.buf_write_ptr = 0;
572 bi.buf_write_count = 0;
573 bi.buf_read_count = 0;
577 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
578 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
579 comedi_buf_read_free(async, bi.bytes_read);
581 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
583 && async->buf_write_count == async->buf_read_count) {
584 do_become_nonbusy(dev, s);
588 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
590 comedi_buf_write_alloc(async, bi.bytes_written);
591 comedi_buf_write_free(async, bi.bytes_written);
594 bi.buf_write_count = async->buf_write_count;
595 bi.buf_write_ptr = async->buf_write_ptr;
596 bi.buf_read_count = async->buf_read_count;
597 bi.buf_read_ptr = async->buf_read_ptr;
600 if (copy_to_user(arg, &bi, sizeof(comedi_bufinfo)))
606 static int parse_insn(comedi_device *dev, comedi_insn *insn, lsampl_t *data,
610 * synchronous instructions
613 * pointer to sync cmd structure
616 * sync cmd struct at arg
623 /* arbitrary limits */
624 #define MAX_SAMPLES 256
625 static int do_insnlist_ioctl(comedi_device *dev, void *arg, void *file)
627 comedi_insnlist insnlist;
628 comedi_insn *insns = NULL;
629 lsampl_t *data = NULL;
633 if (copy_from_user(&insnlist, arg, sizeof(comedi_insnlist)))
636 data = kmalloc(sizeof(lsampl_t) * MAX_SAMPLES, GFP_KERNEL);
638 DPRINTK("kmalloc failed\n");
643 insns = kmalloc(sizeof(comedi_insn) * insnlist.n_insns, GFP_KERNEL);
645 DPRINTK("kmalloc failed\n");
650 if (copy_from_user(insns, insnlist.insns,
651 sizeof(comedi_insn) * insnlist.n_insns)) {
652 DPRINTK("copy_from_user failed\n");
657 for (i = 0; i < insnlist.n_insns; i++) {
658 if (insns[i].n > MAX_SAMPLES) {
659 DPRINTK("number of samples too large\n");
663 if (insns[i].insn & INSN_MASK_WRITE) {
664 if (copy_from_user(data, insns[i].data,
665 insns[i].n * sizeof(lsampl_t))) {
666 DPRINTK("copy_from_user failed\n");
671 ret = parse_insn(dev, insns + i, data, file);
674 if (insns[i].insn & INSN_MASK_READ) {
675 if (copy_to_user(insns[i].data, data,
676 insns[i].n * sizeof(lsampl_t))) {
677 DPRINTK("copy_to_user failed\n");
695 static int check_insn_config_length(comedi_insn *insn, lsampl_t *data)
701 case INSN_CONFIG_DIO_OUTPUT:
702 case INSN_CONFIG_DIO_INPUT:
703 case INSN_CONFIG_DISARM:
704 case INSN_CONFIG_RESET:
708 case INSN_CONFIG_ARM:
709 case INSN_CONFIG_DIO_QUERY:
710 case INSN_CONFIG_BLOCK_SIZE:
711 case INSN_CONFIG_FILTER:
712 case INSN_CONFIG_SERIAL_CLOCK:
713 case INSN_CONFIG_BIDIRECTIONAL_DATA:
714 case INSN_CONFIG_ALT_SOURCE:
715 case INSN_CONFIG_SET_COUNTER_MODE:
716 case INSN_CONFIG_8254_READ_STATUS:
717 case INSN_CONFIG_SET_ROUTING:
718 case INSN_CONFIG_GET_ROUTING:
719 case INSN_CONFIG_GET_PWM_STATUS:
720 case INSN_CONFIG_PWM_SET_PERIOD:
721 case INSN_CONFIG_PWM_GET_PERIOD:
725 case INSN_CONFIG_SET_GATE_SRC:
726 case INSN_CONFIG_GET_GATE_SRC:
727 case INSN_CONFIG_SET_CLOCK_SRC:
728 case INSN_CONFIG_GET_CLOCK_SRC:
729 case INSN_CONFIG_SET_OTHER_SRC:
730 case INSN_CONFIG_GET_COUNTER_STATUS:
731 case INSN_CONFIG_PWM_SET_H_BRIDGE:
732 case INSN_CONFIG_PWM_GET_H_BRIDGE:
733 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
737 case INSN_CONFIG_PWM_OUTPUT:
738 case INSN_CONFIG_ANALOG_TRIG:
742 /* by default we allow the insn since we don't have checks for
743 * all possible cases yet */
745 rt_printk("comedi: no check for data length of config insn id "
746 "%i is implemented.\n"
747 " Add a check to %s in %s.\n"
748 " Assuming n=%i is correct.\n", data[0], __func__,
756 static int parse_insn(comedi_device *dev, comedi_insn *insn, lsampl_t *data,
763 if (insn->insn & INSN_MASK_SPECIAL) {
764 /* a non-subdevice instruction */
766 switch (insn->insn) {
776 do_gettimeofday(&tv);
778 data[1] = tv.tv_usec;
784 if (insn->n != 1 || data[0] >= 100000) {
788 udelay(data[0] / 1000);
796 if (insn->subdev >= dev->n_subdevices) {
797 DPRINTK("%d not usable subdevice\n",
802 s = dev->subdevices + insn->subdev;
804 DPRINTK("no async\n");
808 if (!s->async->inttrig) {
809 DPRINTK("no inttrig\n");
813 ret = s->async->inttrig(dev, s, insn->data[0]);
818 DPRINTK("invalid insn\n");
823 /* a subdevice instruction */
826 if (insn->subdev >= dev->n_subdevices) {
827 DPRINTK("subdevice %d out of range\n", insn->subdev);
831 s = dev->subdevices + insn->subdev;
833 if (s->type == COMEDI_SUBD_UNUSED) {
834 DPRINTK("%d not usable subdevice\n", insn->subdev);
839 /* are we locked? (ioctl lock) */
840 if (s->lock && s->lock != file) {
841 DPRINTK("device locked\n");
846 ret = check_chanlist(s, 1, &insn->chanspec);
849 DPRINTK("bad chanspec\n");
857 /* This looks arbitrary. It is. */
858 s->busy = &parse_insn;
859 switch (insn->insn) {
861 ret = s->insn_read(dev, s, insn, data);
864 maxdata = s->maxdata_list
865 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
867 for (i = 0; i < insn->n; ++i) {
868 if (data[i] > maxdata) {
870 DPRINTK("bad data value(s)\n");
875 ret = s->insn_write(dev, s, insn, data);
882 ret = s->insn_bits(dev, s, insn, data);
885 ret = check_insn_config_length(insn, data);
888 ret = s->insn_config(dev, s, insn, data);
904 * synchronous instructions
910 * comedi_insn struct at arg
916 static int do_insn_ioctl(comedi_device *dev, void *arg, void *file)
919 lsampl_t *data = NULL;
922 data = kmalloc(sizeof(lsampl_t) * MAX_SAMPLES, GFP_KERNEL);
928 if (copy_from_user(&insn, arg, sizeof(comedi_insn))) {
933 /* This is where the behavior of insn and insnlist deviate. */
934 if (insn.n > MAX_SAMPLES)
935 insn.n = MAX_SAMPLES;
936 if (insn.insn & INSN_MASK_WRITE) {
937 if (copy_from_user(data, insn.data, insn.n * sizeof(lsampl_t))) {
942 ret = parse_insn(dev, &insn, data, file);
945 if (insn.insn & INSN_MASK_READ) {
946 if (copy_to_user(insn.data, data, insn.n * sizeof(lsampl_t))) {
964 pointer to cmd structure
971 modified cmd structure at arg
974 static int do_cmd_ioctl(comedi_device *dev, void *arg, void *file)
980 unsigned int *chanlist_saver = NULL;
982 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
983 DPRINTK("bad cmd address\n");
986 /* save user's chanlist pointer so it can be restored later */
987 chanlist_saver = user_cmd.chanlist;
989 if (user_cmd.subdev >= dev->n_subdevices) {
990 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
994 s = dev->subdevices + user_cmd.subdev;
997 if (s->type == COMEDI_SUBD_UNUSED) {
998 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1002 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1003 DPRINTK("subdevice %i does not support commands\n",
1008 /* are we locked? (ioctl lock) */
1009 if (s->lock && s->lock != file) {
1010 DPRINTK("subdevice locked\n");
1016 DPRINTK("subdevice busy\n");
1021 /* make sure channel/gain list isn't too long */
1022 if (user_cmd.chanlist_len > s->len_chanlist) {
1023 DPRINTK("channel/gain list too long %u > %d\n",
1024 user_cmd.chanlist_len, s->len_chanlist);
1029 /* make sure channel/gain list isn't too short */
1030 if (user_cmd.chanlist_len < 1) {
1031 DPRINTK("channel/gain list too short %u < 1\n",
1032 user_cmd.chanlist_len);
1037 kfree(async->cmd.chanlist);
1038 async->cmd = user_cmd;
1039 async->cmd.data = NULL;
1040 /* load channel/gain list */
1041 async->cmd.chanlist =
1042 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1043 if (!async->cmd.chanlist) {
1044 DPRINTK("allocation failed\n");
1049 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1050 async->cmd.chanlist_len * sizeof(int))) {
1051 DPRINTK("fault reading chanlist\n");
1056 /* make sure each element in channel/gain list is valid */
1057 ret = check_chanlist(s, async->cmd.chanlist_len, async->cmd.chanlist);
1059 DPRINTK("bad chanlist\n");
1063 ret = s->do_cmdtest(dev, s, &async->cmd);
1065 if (async->cmd.flags & TRIG_BOGUS || ret) {
1066 DPRINTK("test returned %d\n", ret);
1067 user_cmd = async->cmd;
1068 /* restore chanlist pointer before copying back */
1069 user_cmd.chanlist = chanlist_saver;
1070 user_cmd.data = NULL;
1071 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1072 DPRINTK("fault writing cmd\n");
1080 if (!async->prealloc_bufsz) {
1082 DPRINTK("no buffer (?)\n");
1086 comedi_reset_async_buf(async);
1089 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1091 if (async->cmd.flags & TRIG_WAKE_EOS)
1092 async->cb_mask |= COMEDI_CB_EOS;
1094 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1096 #ifdef CONFIG_COMEDI_RT
1097 if (async->cmd.flags & TRIG_RT) {
1098 if (comedi_switch_to_rt(dev) == 0)
1099 comedi_set_subdevice_runflags(s, SRF_RT, SRF_RT);
1103 ret = s->do_cmd(dev, s);
1108 do_become_nonbusy(dev, s);
1115 command testing ioctl
1118 pointer to cmd structure
1121 cmd structure at arg
1125 modified cmd structure at arg
1128 static int do_cmdtest_ioctl(comedi_device *dev, void *arg, void *file)
1130 comedi_cmd user_cmd;
1131 comedi_subdevice *s;
1133 unsigned int *chanlist = NULL;
1134 unsigned int *chanlist_saver = NULL;
1136 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
1137 DPRINTK("bad cmd address\n");
1140 /* save user's chanlist pointer so it can be restored later */
1141 chanlist_saver = user_cmd.chanlist;
1143 if (user_cmd.subdev >= dev->n_subdevices) {
1144 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1148 s = dev->subdevices + user_cmd.subdev;
1149 if (s->type == COMEDI_SUBD_UNUSED) {
1150 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1154 if (!s->do_cmd || !s->do_cmdtest) {
1155 DPRINTK("subdevice %i does not support commands\n",
1160 /* make sure channel/gain list isn't too long */
1161 if (user_cmd.chanlist_len > s->len_chanlist) {
1162 DPRINTK("channel/gain list too long %d > %d\n",
1163 user_cmd.chanlist_len, s->len_chanlist);
1168 /* load channel/gain list */
1169 if (user_cmd.chanlist) {
1171 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1173 DPRINTK("allocation failed\n");
1178 if (copy_from_user(chanlist, user_cmd.chanlist,
1179 user_cmd.chanlist_len * sizeof(int))) {
1180 DPRINTK("fault reading chanlist\n");
1185 /* make sure each element in channel/gain list is valid */
1186 ret = check_chanlist(s, user_cmd.chanlist_len, chanlist);
1188 DPRINTK("bad chanlist\n");
1192 user_cmd.chanlist = chanlist;
1195 ret = s->do_cmdtest(dev, s, &user_cmd);
1197 /* restore chanlist pointer before copying back */
1198 user_cmd.chanlist = chanlist_saver;
1200 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1201 DPRINTK("bad cmd address\n");
1226 static int do_lock_ioctl(comedi_device *dev, unsigned int arg, void *file)
1229 unsigned long flags;
1230 comedi_subdevice *s;
1232 if (arg >= dev->n_subdevices)
1234 s = dev->subdevices + arg;
1236 comedi_spin_lock_irqsave(&s->spin_lock, flags);
1237 if (s->busy || s->lock)
1241 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
1248 ret = s->lock_f(dev, s);
1267 This function isn't protected by the semaphore, since
1268 we already own the lock.
1270 static int do_unlock_ioctl(comedi_device *dev, unsigned int arg, void *file)
1272 comedi_subdevice *s;
1274 if (arg >= dev->n_subdevices)
1276 s = dev->subdevices + arg;
1281 if (s->lock && s->lock != file)
1284 if (s->lock == file) {
1298 cancel acquisition ioctl
1310 static int do_cancel_ioctl(comedi_device *dev, unsigned int arg, void *file)
1312 comedi_subdevice *s;
1314 if (arg >= dev->n_subdevices)
1316 s = dev->subdevices + arg;
1317 if (s->async == NULL)
1320 if (s->lock && s->lock != file)
1326 if (s->busy != file)
1329 return do_cancel(dev, s);
1334 instructs driver to synchronize buffers
1346 static int do_poll_ioctl(comedi_device *dev, unsigned int arg, void *file)
1348 comedi_subdevice *s;
1350 if (arg >= dev->n_subdevices)
1352 s = dev->subdevices + arg;
1354 if (s->lock && s->lock != file)
1360 if (s->busy != file)
1364 return s->poll(dev, s);
1369 static int do_cancel(comedi_device *dev, comedi_subdevice *s)
1373 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1374 ret = s->cancel(dev, s);
1376 do_become_nonbusy(dev, s);
1381 void comedi_unmap(struct vm_area_struct *area)
1383 comedi_async *async;
1386 async = area->vm_private_data;
1387 dev = async->subdevice->device;
1389 mutex_lock(&dev->mutex);
1390 async->mmap_count--;
1391 mutex_unlock(&dev->mutex);
1394 static struct vm_operations_struct comedi_vm_ops = {
1395 .close = comedi_unmap,
1398 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1400 const unsigned minor = iminor(file->f_dentry->d_inode);
1401 struct comedi_device_file_info *dev_file_info =
1402 comedi_get_device_file_info(minor);
1403 comedi_device *dev = dev_file_info->device;
1404 comedi_async *async = NULL;
1405 unsigned long start = vma->vm_start;
1410 comedi_subdevice *s;
1412 mutex_lock(&dev->mutex);
1413 if (!dev->attached) {
1414 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1418 if (vma->vm_flags & VM_WRITE)
1419 s = comedi_get_write_subdevice(dev_file_info);
1421 s = comedi_get_read_subdevice(dev_file_info);
1428 if (async == NULL) {
1433 if (vma->vm_pgoff != 0) {
1434 DPRINTK("comedi: mmap() offset must be 0.\n");
1439 size = vma->vm_end - vma->vm_start;
1440 if (size > async->prealloc_bufsz) {
1444 if (size & (~PAGE_MASK)) {
1449 n_pages = size >> PAGE_SHIFT;
1450 for (i = 0; i < n_pages; ++i) {
1451 if (remap_pfn_range(vma, start,
1452 page_to_pfn(virt_to_page(async->
1455 PAGE_SIZE, PAGE_SHARED)) {
1462 vma->vm_ops = &comedi_vm_ops;
1463 vma->vm_private_data = async;
1465 async->mmap_count++;
1469 mutex_unlock(&dev->mutex);
1473 static unsigned int comedi_poll(struct file *file, poll_table *wait)
1475 unsigned int mask = 0;
1476 const unsigned minor = iminor(file->f_dentry->d_inode);
1477 struct comedi_device_file_info *dev_file_info =
1478 comedi_get_device_file_info(minor);
1479 comedi_device *dev = dev_file_info->device;
1480 comedi_subdevice *read_subdev;
1481 comedi_subdevice *write_subdev;
1483 mutex_lock(&dev->mutex);
1484 if (!dev->attached) {
1485 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1486 mutex_unlock(&dev->mutex);
1491 read_subdev = comedi_get_read_subdevice(dev_file_info);
1493 poll_wait(file, &read_subdev->async->wait_head, wait);
1494 if (!read_subdev->busy
1495 || comedi_buf_read_n_available(read_subdev->async) > 0
1496 || !(comedi_get_subdevice_runflags(read_subdev) &
1498 mask |= POLLIN | POLLRDNORM;
1501 write_subdev = comedi_get_write_subdevice(dev_file_info);
1503 poll_wait(file, &write_subdev->async->wait_head, wait);
1504 comedi_buf_write_alloc(write_subdev->async,
1505 write_subdev->async->prealloc_bufsz);
1506 if (!write_subdev->busy
1507 || !(comedi_get_subdevice_runflags(write_subdev) &
1509 || comedi_buf_write_n_allocated(write_subdev->async) >=
1510 bytes_per_sample(write_subdev->async->subdevice)) {
1511 mask |= POLLOUT | POLLWRNORM;
1515 mutex_unlock(&dev->mutex);
1519 static ssize_t comedi_write(struct file *file, const char *buf, size_t nbytes,
1522 comedi_subdevice *s;
1523 comedi_async *async;
1524 int n, m, count = 0, retval = 0;
1525 DECLARE_WAITQUEUE(wait, current);
1526 const unsigned minor = iminor(file->f_dentry->d_inode);
1527 struct comedi_device_file_info *dev_file_info =
1528 comedi_get_device_file_info(minor);
1529 comedi_device *dev = dev_file_info->device;
1531 if (!dev->attached) {
1532 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1537 s = comedi_get_write_subdevice(dev_file_info);
1552 if (s->busy != file) {
1556 add_wait_queue(&async->wait_head, &wait);
1557 while (nbytes > 0 && !retval) {
1558 set_current_state(TASK_INTERRUPTIBLE);
1563 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1564 m = async->prealloc_bufsz - async->buf_write_ptr;
1565 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1566 if (m > comedi_buf_write_n_allocated(async))
1567 m = comedi_buf_write_n_allocated(async);
1572 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1573 if (comedi_get_subdevice_runflags(s) &
1579 do_become_nonbusy(dev, s);
1582 if (file->f_flags & O_NONBLOCK) {
1586 if (signal_pending(current)) {
1587 retval = -ERESTARTSYS;
1593 if (s->busy != file) {
1600 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1606 comedi_buf_write_free(async, n);
1612 break; /* makes device work like a pipe */
1614 set_current_state(TASK_RUNNING);
1615 remove_wait_queue(&async->wait_head, &wait);
1618 return count ? count : retval;
1621 static ssize_t comedi_read(struct file *file, char *buf, size_t nbytes,
1624 comedi_subdevice *s;
1625 comedi_async *async;
1626 int n, m, count = 0, retval = 0;
1627 DECLARE_WAITQUEUE(wait, current);
1628 const unsigned minor = iminor(file->f_dentry->d_inode);
1629 struct comedi_device_file_info *dev_file_info =
1630 comedi_get_device_file_info(minor);
1631 comedi_device *dev = dev_file_info->device;
1633 if (!dev->attached) {
1634 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1639 s = comedi_get_read_subdevice(dev_file_info);
1653 if (s->busy != file) {
1658 add_wait_queue(&async->wait_head, &wait);
1659 while (nbytes > 0 && !retval) {
1660 set_current_state(TASK_INTERRUPTIBLE);
1664 m = comedi_buf_read_n_available(async);
1665 /* printk("%d available\n",m); */
1666 if (async->buf_read_ptr + m > async->prealloc_bufsz)
1667 m = async->prealloc_bufsz - async->buf_read_ptr;
1668 /* printk("%d contiguous\n",m); */
1673 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1674 do_become_nonbusy(dev, s);
1675 if (comedi_get_subdevice_runflags(s) &
1683 if (file->f_flags & O_NONBLOCK) {
1687 if (signal_pending(current)) {
1688 retval = -ERESTARTSYS;
1696 if (s->busy != file) {
1702 m = copy_to_user(buf, async->prealloc_buf +
1703 async->buf_read_ptr, n);
1709 comedi_buf_read_alloc(async, n);
1710 comedi_buf_read_free(async, n);
1716 break; /* makes device work like a pipe */
1718 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
1719 async->buf_read_count - async->buf_write_count == 0) {
1720 do_become_nonbusy(dev, s);
1722 set_current_state(TASK_RUNNING);
1723 remove_wait_queue(&async->wait_head, &wait);
1726 return count ? count : retval;
1730 This function restores a subdevice to an idle state.
1732 void do_become_nonbusy(comedi_device *dev, comedi_subdevice *s)
1734 comedi_async *async = s->async;
1736 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1737 #ifdef CONFIG_COMEDI_RT
1738 if (comedi_get_subdevice_runflags(s) & SRF_RT) {
1739 comedi_switch_to_non_rt(dev);
1740 comedi_set_subdevice_runflags(s, SRF_RT, 0);
1744 comedi_reset_async_buf(async);
1745 async->inttrig = NULL;
1748 "BUG: (?) do_become_nonbusy called with async=0\n");
1754 static int comedi_open(struct inode *inode, struct file *file)
1757 const unsigned minor = iminor(inode);
1758 struct comedi_device_file_info *dev_file_info =
1759 comedi_get_device_file_info(minor);
1760 comedi_device *dev = dev_file_info->device;
1762 DPRINTK("invalid minor number\n");
1766 /* This is slightly hacky, but we want module autoloading
1768 * case: user opens device, attached -> ok
1769 * case: user opens device, unattached, in_request_module=0 -> autoload
1770 * case: user opens device, unattached, in_request_module=1 -> fail
1771 * case: root opens device, attached -> ok
1772 * case: root opens device, unattached, in_request_module=1 -> ok
1773 * (typically called from modprobe)
1774 * case: root opens device, unattached, in_request_module=0 -> autoload
1776 * The last could be changed to "-> ok", which would deny root
1779 mutex_lock(&dev->mutex);
1782 if (!capable(CAP_SYS_MODULE) && dev->in_request_module) {
1783 DPRINTK("in request module\n");
1784 mutex_unlock(&dev->mutex);
1787 if (capable(CAP_SYS_MODULE) && dev->in_request_module)
1790 dev->in_request_module = 1;
1792 sprintf(mod, "char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1794 mutex_unlock(&dev->mutex);
1795 request_module(mod);
1796 mutex_lock(&dev->mutex);
1799 dev->in_request_module = 0;
1801 if (!dev->attached && !capable(CAP_SYS_MODULE)) {
1802 DPRINTK("not attached and not CAP_SYS_MODULE\n");
1803 mutex_unlock(&dev->mutex);
1807 __module_get(THIS_MODULE);
1809 if (dev->attached) {
1810 if (!try_module_get(dev->driver->module)) {
1811 module_put(THIS_MODULE);
1812 mutex_unlock(&dev->mutex);
1817 if (dev->attached && dev->use_count == 0 && dev->open)
1822 mutex_unlock(&dev->mutex);
1827 static int comedi_close(struct inode *inode, struct file *file)
1829 const unsigned minor = iminor(inode);
1830 struct comedi_device_file_info *dev_file_info =
1831 comedi_get_device_file_info(minor);
1832 comedi_device *dev = dev_file_info->device;
1833 comedi_subdevice *s = NULL;
1836 mutex_lock(&dev->mutex);
1838 if (dev->subdevices) {
1839 for (i = 0; i < dev->n_subdevices; i++) {
1840 s = dev->subdevices + i;
1842 if (s->busy == file)
1844 if (s->lock == file)
1848 if (dev->attached && dev->use_count == 1 && dev->close)
1851 module_put(THIS_MODULE);
1853 module_put(dev->driver->module);
1857 mutex_unlock(&dev->mutex);
1859 if (file->f_flags & FASYNC)
1860 comedi_fasync(-1, file, 0);
1865 static int comedi_fasync(int fd, struct file *file, int on)
1867 const unsigned minor = iminor(file->f_dentry->d_inode);
1868 struct comedi_device_file_info *dev_file_info =
1869 comedi_get_device_file_info(minor);
1871 comedi_device *dev = dev_file_info->device;
1873 return fasync_helper(fd, file, on, &dev->async_queue);
1876 const struct file_operations comedi_fops = {
1877 .owner = THIS_MODULE,
1878 #ifdef HAVE_UNLOCKED_IOCTL
1879 .unlocked_ioctl = comedi_unlocked_ioctl,
1881 .ioctl = comedi_ioctl,
1883 #ifdef HAVE_COMPAT_IOCTL
1884 .compat_ioctl = comedi_compat_ioctl,
1886 .open = comedi_open,
1887 .release = comedi_close,
1888 .read = comedi_read,
1889 .write = comedi_write,
1890 .mmap = comedi_mmap,
1891 .poll = comedi_poll,
1892 .fasync = comedi_fasync,
1895 struct class *comedi_class;
1896 static struct cdev comedi_cdev;
1898 static void comedi_cleanup_legacy_minors(void)
1902 for (i = 0; i < comedi_num_legacy_minors; i++)
1903 comedi_free_board_minor(i);
1906 static int __init comedi_init(void)
1911 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
1912 " - http://www.comedi.org\n");
1914 memset(comedi_file_info_table, 0,
1915 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
1917 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1918 COMEDI_NUM_MINORS, "comedi");
1921 cdev_init(&comedi_cdev, &comedi_fops);
1922 comedi_cdev.owner = THIS_MODULE;
1923 kobject_set_name(&comedi_cdev.kobj, "comedi");
1924 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
1925 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1929 comedi_class = class_create(THIS_MODULE, "comedi");
1930 if (IS_ERR(comedi_class)) {
1931 printk("comedi: failed to create class");
1932 cdev_del(&comedi_cdev);
1933 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1935 return PTR_ERR(comedi_class);
1938 /* XXX requires /proc interface */
1941 /* create devices files for legacy/manual use */
1942 for (i = 0; i < comedi_num_legacy_minors; i++) {
1944 minor = comedi_alloc_board_minor(NULL);
1946 comedi_cleanup_legacy_minors();
1947 cdev_del(&comedi_cdev);
1948 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1956 comedi_register_ioctl32();
1961 static void __exit comedi_cleanup(void)
1965 comedi_cleanup_legacy_minors();
1966 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
1967 BUG_ON(comedi_file_info_table[i]);
1970 class_destroy(comedi_class);
1971 cdev_del(&comedi_cdev);
1972 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
1974 comedi_proc_cleanup();
1976 comedi_rt_cleanup();
1978 comedi_unregister_ioctl32();
1981 module_init(comedi_init);
1982 module_exit(comedi_cleanup);
1984 void comedi_error(const comedi_device *dev, const char *s)
1986 rt_printk("comedi%d: %s: %s\n", dev->minor, dev->driver->driver_name,
1990 void comedi_event(comedi_device *dev, comedi_subdevice *s)
1992 comedi_async *async = s->async;
1993 unsigned runflags = 0;
1994 unsigned runflags_mask = 0;
1996 /* DPRINTK("comedi_event 0x%x\n",mask); */
1998 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2002 events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2003 runflags_mask |= SRF_RUNNING;
2005 /* remember if an error event has occured, so an error
2006 * can be returned the next time the user does a read() */
2007 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2008 runflags_mask |= SRF_ERROR;
2009 runflags |= SRF_ERROR;
2011 if (runflags_mask) {
2012 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2013 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2016 if (async->cb_mask & s->async->events) {
2017 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2020 #ifdef CONFIG_COMEDI_RT
2022 comedi_rt_pend_wakeup(&async->wait_head);
2025 ("BUG: comedi_event() code unreachable\n");
2028 wake_up_interruptible(&async->wait_head);
2029 if (s->subdev_flags & SDF_CMD_READ) {
2030 kill_fasync(&dev->async_queue, SIGIO,
2033 if (s->subdev_flags & SDF_CMD_WRITE) {
2034 kill_fasync(&dev->async_queue, SIGIO,
2040 async->cb_func(s->async->events, async->cb_arg);
2041 /* XXX bug here. If subdevice A is rt, and
2042 * subdevice B tries to callback to a normal
2043 * linux kernel function, it will be at the
2044 * wrong priority. Since this isn't very
2045 * common, I'm not going to worry about it. */
2048 s->async->events = 0;
2051 void comedi_set_subdevice_runflags(comedi_subdevice *s, unsigned mask,
2054 unsigned long flags;
2056 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2057 s->runflags &= ~mask;
2058 s->runflags |= (bits & mask);
2059 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2062 unsigned comedi_get_subdevice_runflags(comedi_subdevice *s)
2064 unsigned long flags;
2067 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2068 runflags = s->runflags;
2069 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2073 static int is_device_busy(comedi_device *dev)
2075 comedi_subdevice *s;
2081 for (i = 0; i < dev->n_subdevices; i++) {
2082 s = dev->subdevices + i;
2085 if (s->async && s->async->mmap_count)
2092 void comedi_device_init(comedi_device *dev)
2094 memset(dev, 0, sizeof(comedi_device));
2095 spin_lock_init(&dev->spinlock);
2096 mutex_init(&dev->mutex);
2100 void comedi_device_cleanup(comedi_device *dev)
2104 mutex_lock(&dev->mutex);
2105 comedi_device_detach(dev);
2106 mutex_unlock(&dev->mutex);
2107 mutex_destroy(&dev->mutex);
2110 int comedi_alloc_board_minor(struct device *hardware_device)
2112 unsigned long flags;
2113 struct comedi_device_file_info *info;
2114 device_create_result_type *csdev;
2117 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2120 info->device = kzalloc(sizeof(comedi_device), GFP_KERNEL);
2121 if (info->device == NULL) {
2125 comedi_device_init(info->device);
2126 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2127 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2128 if (comedi_file_info_table[i] == NULL) {
2129 comedi_file_info_table[i] = info;
2133 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2134 if (i == COMEDI_NUM_BOARD_MINORS) {
2135 comedi_device_cleanup(info->device);
2136 kfree(info->device);
2139 ("comedi: error: ran out of minor numbers for board device files.\n");
2142 info->device->minor = i;
2143 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
2144 MKDEV(COMEDI_MAJOR, i), NULL,
2145 hardware_device, "comedi%i", i);
2147 info->device->class_dev = csdev;
2152 void comedi_free_board_minor(unsigned minor)
2154 unsigned long flags;
2155 struct comedi_device_file_info *info;
2157 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2158 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2159 info = comedi_file_info_table[minor];
2160 comedi_file_info_table[minor] = NULL;
2161 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2164 comedi_device *dev = info->device;
2166 if (dev->class_dev) {
2167 device_destroy(comedi_class,
2168 MKDEV(COMEDI_MAJOR, dev->minor));
2170 comedi_device_cleanup(dev);
2177 int comedi_alloc_subdevice_minor(comedi_device *dev, comedi_subdevice *s)
2179 unsigned long flags;
2180 struct comedi_device_file_info *info;
2181 device_create_result_type *csdev;
2184 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2188 info->read_subdevice = s;
2189 info->write_subdevice = s;
2190 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2191 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2192 if (comedi_file_info_table[i] == NULL) {
2193 comedi_file_info_table[i] = info;
2197 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2198 if (i == COMEDI_NUM_MINORS) {
2201 ("comedi: error: ran out of minor numbers for board device files.\n");
2205 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
2206 MKDEV(COMEDI_MAJOR, i), NULL, NULL,
2207 "comedi%i_subd%i", dev->minor,
2208 (int)(s - dev->subdevices));
2210 s->class_dev = csdev;
2215 void comedi_free_subdevice_minor(comedi_subdevice *s)
2217 unsigned long flags;
2218 struct comedi_device_file_info *info;
2225 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2226 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2228 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2229 info = comedi_file_info_table[s->minor];
2230 comedi_file_info_table[s->minor] = NULL;
2231 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2234 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2235 s->class_dev = NULL;
2240 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2242 unsigned long flags;
2243 struct comedi_device_file_info *info;
2245 BUG_ON(minor >= COMEDI_NUM_MINORS);
2246 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2247 info = comedi_file_info_table[minor];
2248 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);