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>
47 #include <linux/stat.h>
50 #include <linux/uaccess.h>
54 MODULE_AUTHOR("http://www.comedi.org");
55 MODULE_DESCRIPTION("Comedi core module");
56 MODULE_LICENSE("GPL");
58 #ifdef CONFIG_COMEDI_DEBUG
60 EXPORT_SYMBOL(comedi_debug);
61 module_param(comedi_debug, int, 0644);
64 int comedi_autoconfig = 1;
65 module_param(comedi_autoconfig, bool, 0444);
67 static int comedi_num_legacy_minors;
68 module_param(comedi_num_legacy_minors, int, 0444);
70 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
71 static struct comedi_device_file_info
72 *comedi_file_info_table[COMEDI_NUM_MINORS];
74 static int do_devconfig_ioctl(struct comedi_device *dev,
75 struct comedi_devconfig __user *arg);
76 static int do_bufconfig_ioctl(struct comedi_device *dev,
77 struct comedi_bufconfig __user *arg);
78 static int do_devinfo_ioctl(struct comedi_device *dev,
79 struct comedi_devinfo __user *arg,
81 static int do_subdinfo_ioctl(struct comedi_device *dev,
82 struct comedi_subdinfo __user *arg, void *file);
83 static int do_chaninfo_ioctl(struct comedi_device *dev,
84 struct comedi_chaninfo __user *arg);
85 static int do_bufinfo_ioctl(struct comedi_device *dev,
86 struct comedi_bufinfo __user *arg, void *file);
87 static int do_cmd_ioctl(struct comedi_device *dev,
88 struct comedi_cmd __user *arg, void *file);
89 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
91 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
93 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
95 static int do_cmdtest_ioctl(struct comedi_device *dev,
96 struct comedi_cmd __user *arg, void *file);
97 static int do_insnlist_ioctl(struct comedi_device *dev,
98 struct comedi_insnlist __user *arg, void *file);
99 static int do_insn_ioctl(struct comedi_device *dev,
100 struct comedi_insn __user *arg, void *file);
101 static int do_poll_ioctl(struct comedi_device *dev, unsigned int subd,
104 extern void do_become_nonbusy(struct comedi_device *dev,
105 struct comedi_subdevice *s);
106 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
108 static int comedi_fasync(int fd, struct file *file, int on);
110 static int is_device_busy(struct comedi_device *dev);
111 static int resize_async_buffer(struct comedi_device *dev,
112 struct comedi_subdevice *s,
113 struct comedi_async *async, unsigned new_size);
115 /* declarations for sysfs attribute files */
116 static struct device_attribute dev_attr_max_read_buffer_kb;
117 static struct device_attribute dev_attr_read_buffer_kb;
118 static struct device_attribute dev_attr_max_write_buffer_kb;
119 static struct device_attribute dev_attr_write_buffer_kb;
121 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
124 const unsigned minor = iminor(file->f_dentry->d_inode);
125 struct comedi_device_file_info *dev_file_info =
126 comedi_get_device_file_info(minor);
127 struct comedi_device *dev;
130 if (dev_file_info == NULL || dev_file_info->device == NULL)
132 dev = dev_file_info->device;
134 mutex_lock(&dev->mutex);
136 /* Device config is special, because it must work on
137 * an unconfigured device. */
138 if (cmd == COMEDI_DEVCONFIG) {
139 rc = do_devconfig_ioctl(dev,
140 (struct comedi_devconfig __user *)arg);
144 if (!dev->attached) {
145 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
151 case COMEDI_BUFCONFIG:
152 rc = do_bufconfig_ioctl(dev,
153 (struct comedi_bufconfig __user *)arg);
156 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
159 case COMEDI_SUBDINFO:
160 rc = do_subdinfo_ioctl(dev,
161 (struct comedi_subdinfo __user *)arg,
164 case COMEDI_CHANINFO:
165 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
167 case COMEDI_RANGEINFO:
168 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
171 rc = do_bufinfo_ioctl(dev,
172 (struct comedi_bufinfo __user *)arg,
176 rc = do_lock_ioctl(dev, arg, file);
179 rc = do_unlock_ioctl(dev, arg, file);
182 rc = do_cancel_ioctl(dev, arg, file);
185 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
188 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
191 case COMEDI_INSNLIST:
192 rc = do_insnlist_ioctl(dev,
193 (struct comedi_insnlist __user *)arg,
197 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
201 rc = do_poll_ioctl(dev, arg, file);
209 mutex_unlock(&dev->mutex);
218 pointer to devconfig structure
221 devconfig structure at arg
226 static int do_devconfig_ioctl(struct comedi_device *dev,
227 struct comedi_devconfig __user *arg)
229 struct comedi_devconfig it;
231 unsigned char *aux_data = NULL;
234 if (!capable(CAP_SYS_ADMIN))
238 if (is_device_busy(dev))
241 struct module *driver_module = dev->driver->module;
242 comedi_device_detach(dev);
243 module_put(driver_module);
248 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
251 it.board_name[COMEDI_NAMELEN - 1] = 0;
253 if (comedi_aux_data(it.options, 0) &&
254 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
256 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
260 aux_data = vmalloc(aux_len);
264 if (copy_from_user(aux_data,
265 comedi_aux_data(it.options, 0), aux_len)) {
269 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
270 (unsigned long)aux_data;
271 if (sizeof(void *) > sizeof(int)) {
272 bit_shift = sizeof(int) * 8;
273 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
274 ((unsigned long)aux_data) >> bit_shift;
276 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
279 ret = comedi_device_attach(dev, &it);
281 if (!try_module_get(dev->driver->module)) {
282 comedi_device_detach(dev);
295 buffer configuration ioctl
298 pointer to bufconfig structure
304 modified bufconfig at arg
307 static int do_bufconfig_ioctl(struct comedi_device *dev,
308 struct comedi_bufconfig __user *arg)
310 struct comedi_bufconfig bc;
311 struct comedi_async *async;
312 struct comedi_subdevice *s;
315 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
318 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
321 s = dev->subdevices + bc.subdevice;
325 DPRINTK("subdevice does not have async capability\n");
331 if (bc.maximum_size) {
332 if (!capable(CAP_SYS_ADMIN))
335 async->max_bufsize = bc.maximum_size;
339 retval = resize_async_buffer(dev, s, async, bc.size);
344 bc.size = async->prealloc_bufsz;
345 bc.maximum_size = async->max_bufsize;
348 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
359 pointer to devinfo structure
368 static int do_devinfo_ioctl(struct comedi_device *dev,
369 struct comedi_devinfo __user *arg,
372 struct comedi_devinfo devinfo;
373 const unsigned minor = iminor(file->f_dentry->d_inode);
374 struct comedi_device_file_info *dev_file_info =
375 comedi_get_device_file_info(minor);
376 struct comedi_subdevice *read_subdev =
377 comedi_get_read_subdevice(dev_file_info);
378 struct comedi_subdevice *write_subdev =
379 comedi_get_write_subdevice(dev_file_info);
381 memset(&devinfo, 0, sizeof(devinfo));
383 /* fill devinfo structure */
384 devinfo.version_code = COMEDI_VERSION_CODE;
385 devinfo.n_subdevs = dev->n_subdevices;
386 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
387 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
390 devinfo.read_subdevice = read_subdev - dev->subdevices;
392 devinfo.read_subdevice = -1;
395 devinfo.write_subdevice = write_subdev - dev->subdevices;
397 devinfo.write_subdevice = -1;
399 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
410 pointer to array of subdevice info structures
416 array of subdevice info structures at arg
419 static int do_subdinfo_ioctl(struct comedi_device *dev,
420 struct comedi_subdinfo __user *arg, void *file)
423 struct comedi_subdinfo *tmp, *us;
424 struct comedi_subdevice *s;
427 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
432 /* fill subdinfo structs */
433 for (i = 0; i < dev->n_subdevices; i++) {
434 s = dev->subdevices + i;
438 us->n_chan = s->n_chan;
439 us->subd_flags = s->subdev_flags;
440 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
441 us->subd_flags |= SDF_RUNNING;
442 #define TIMER_nanosec 5 /* backwards compatibility */
443 us->timer_type = TIMER_nanosec;
444 us->len_chanlist = s->len_chanlist;
445 us->maxdata = s->maxdata;
446 if (s->range_table) {
448 (i << 24) | (0 << 16) | (s->range_table->length);
450 us->range_type = 0; /* XXX */
452 us->flags = s->flags;
455 us->subd_flags |= SDF_BUSY;
457 us->subd_flags |= SDF_BUSY_OWNER;
459 us->subd_flags |= SDF_LOCKED;
461 us->subd_flags |= SDF_LOCK_OWNER;
462 if (!s->maxdata && s->maxdata_list)
463 us->subd_flags |= SDF_MAXDATA;
465 us->subd_flags |= SDF_FLAGS;
466 if (s->range_table_list)
467 us->subd_flags |= SDF_RANGETYPE;
469 us->subd_flags |= SDF_CMD;
471 if (s->insn_bits != &insn_inval)
472 us->insn_bits_support = COMEDI_SUPPORTED;
474 us->insn_bits_support = COMEDI_UNSUPPORTED;
476 us->settling_time_0 = s->settling_time_0;
479 ret = copy_to_user(arg, tmp,
480 dev->n_subdevices * sizeof(struct comedi_subdinfo));
484 return ret ? -EFAULT : 0;
492 pointer to chaninfo structure
495 chaninfo structure at arg
498 arrays at elements of chaninfo structure
501 static int do_chaninfo_ioctl(struct comedi_device *dev,
502 struct comedi_chaninfo __user *arg)
504 struct comedi_subdevice *s;
505 struct comedi_chaninfo it;
507 if (copy_from_user(&it, arg, sizeof(struct comedi_chaninfo)))
510 if (it.subdev >= dev->n_subdevices)
512 s = dev->subdevices + it.subdev;
514 if (it.maxdata_list) {
515 if (s->maxdata || !s->maxdata_list)
517 if (copy_to_user(it.maxdata_list, s->maxdata_list,
518 s->n_chan * sizeof(unsigned int)))
525 if (copy_to_user(it.flaglist, s->flaglist,
526 s->n_chan * sizeof(unsigned int)))
533 if (!s->range_table_list)
535 for (i = 0; i < s->n_chan; i++) {
538 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
539 (s->range_table_list[i]->length);
540 put_user(x, it.rangelist + i);
543 if (copy_to_user(it.rangelist, s->range_type_list,
544 s->n_chan * sizeof(unsigned int)))
554 buffer information ioctl
557 pointer to bufinfo structure
563 modified bufinfo at arg
566 static int do_bufinfo_ioctl(struct comedi_device *dev,
567 struct comedi_bufinfo __user *arg, void *file)
569 struct comedi_bufinfo bi;
570 struct comedi_subdevice *s;
571 struct comedi_async *async;
573 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
576 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
579 s = dev->subdevices + bi.subdevice;
581 if (s->lock && s->lock != file)
587 DPRINTK("subdevice does not have async capability\n");
588 bi.buf_write_ptr = 0;
590 bi.buf_write_count = 0;
591 bi.buf_read_count = 0;
593 bi.bytes_written = 0;
598 bi.bytes_written = 0;
599 goto copyback_position;
604 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
605 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
606 comedi_buf_read_free(async, bi.bytes_read);
608 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
610 && async->buf_write_count == async->buf_read_count) {
611 do_become_nonbusy(dev, s);
615 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
617 comedi_buf_write_alloc(async, bi.bytes_written);
618 comedi_buf_write_free(async, bi.bytes_written);
622 bi.buf_write_count = async->buf_write_count;
623 bi.buf_write_ptr = async->buf_write_ptr;
624 bi.buf_read_count = async->buf_read_count;
625 bi.buf_read_ptr = async->buf_read_ptr;
628 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
634 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
635 unsigned int *data, void *file);
638 * synchronous instructions
641 * pointer to sync cmd structure
644 * sync cmd struct at arg
651 /* arbitrary limits */
652 #define MAX_SAMPLES 256
653 static int do_insnlist_ioctl(struct comedi_device *dev,
654 struct comedi_insnlist __user *arg, void *file)
656 struct comedi_insnlist insnlist;
657 struct comedi_insn *insns = NULL;
658 unsigned int *data = NULL;
662 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
665 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
667 DPRINTK("kmalloc failed\n");
673 kmalloc(sizeof(struct comedi_insn) * insnlist.n_insns, GFP_KERNEL);
675 DPRINTK("kmalloc failed\n");
680 if (copy_from_user(insns, insnlist.insns,
681 sizeof(struct comedi_insn) * insnlist.n_insns)) {
682 DPRINTK("copy_from_user failed\n");
687 for (i = 0; i < insnlist.n_insns; i++) {
688 if (insns[i].n > MAX_SAMPLES) {
689 DPRINTK("number of samples too large\n");
693 if (insns[i].insn & INSN_MASK_WRITE) {
694 if (copy_from_user(data, insns[i].data,
695 insns[i].n * sizeof(unsigned int))) {
696 DPRINTK("copy_from_user failed\n");
701 ret = parse_insn(dev, insns + i, data, file);
704 if (insns[i].insn & INSN_MASK_READ) {
705 if (copy_to_user(insns[i].data, data,
706 insns[i].n * sizeof(unsigned int))) {
707 DPRINTK("copy_to_user failed\n");
725 static int check_insn_config_length(struct comedi_insn *insn,
732 case INSN_CONFIG_DIO_OUTPUT:
733 case INSN_CONFIG_DIO_INPUT:
734 case INSN_CONFIG_DISARM:
735 case INSN_CONFIG_RESET:
739 case INSN_CONFIG_ARM:
740 case INSN_CONFIG_DIO_QUERY:
741 case INSN_CONFIG_BLOCK_SIZE:
742 case INSN_CONFIG_FILTER:
743 case INSN_CONFIG_SERIAL_CLOCK:
744 case INSN_CONFIG_BIDIRECTIONAL_DATA:
745 case INSN_CONFIG_ALT_SOURCE:
746 case INSN_CONFIG_SET_COUNTER_MODE:
747 case INSN_CONFIG_8254_READ_STATUS:
748 case INSN_CONFIG_SET_ROUTING:
749 case INSN_CONFIG_GET_ROUTING:
750 case INSN_CONFIG_GET_PWM_STATUS:
751 case INSN_CONFIG_PWM_SET_PERIOD:
752 case INSN_CONFIG_PWM_GET_PERIOD:
756 case INSN_CONFIG_SET_GATE_SRC:
757 case INSN_CONFIG_GET_GATE_SRC:
758 case INSN_CONFIG_SET_CLOCK_SRC:
759 case INSN_CONFIG_GET_CLOCK_SRC:
760 case INSN_CONFIG_SET_OTHER_SRC:
761 case INSN_CONFIG_GET_COUNTER_STATUS:
762 case INSN_CONFIG_PWM_SET_H_BRIDGE:
763 case INSN_CONFIG_PWM_GET_H_BRIDGE:
764 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
768 case INSN_CONFIG_PWM_OUTPUT:
769 case INSN_CONFIG_ANALOG_TRIG:
773 /* by default we allow the insn since we don't have checks for
774 * all possible cases yet */
777 "comedi: no check for data length of config insn id "
778 "%i is implemented.\n"
779 " Add a check to %s in %s.\n"
780 " Assuming n=%i is correct.\n", data[0], __func__,
788 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
789 unsigned int *data, void *file)
791 struct comedi_subdevice *s;
795 if (insn->insn & INSN_MASK_SPECIAL) {
796 /* a non-subdevice instruction */
798 switch (insn->insn) {
808 do_gettimeofday(&tv);
810 data[1] = tv.tv_usec;
816 if (insn->n != 1 || data[0] >= 100000) {
820 udelay(data[0] / 1000);
828 if (insn->subdev >= dev->n_subdevices) {
829 DPRINTK("%d not usable subdevice\n",
834 s = dev->subdevices + insn->subdev;
836 DPRINTK("no async\n");
840 if (!s->async->inttrig) {
841 DPRINTK("no inttrig\n");
845 ret = s->async->inttrig(dev, s, insn->data[0]);
850 DPRINTK("invalid insn\n");
855 /* a subdevice instruction */
856 unsigned int maxdata;
858 if (insn->subdev >= dev->n_subdevices) {
859 DPRINTK("subdevice %d out of range\n", insn->subdev);
863 s = dev->subdevices + insn->subdev;
865 if (s->type == COMEDI_SUBD_UNUSED) {
866 DPRINTK("%d not usable subdevice\n", insn->subdev);
871 /* are we locked? (ioctl lock) */
872 if (s->lock && s->lock != file) {
873 DPRINTK("device locked\n");
878 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
881 DPRINTK("bad chanspec\n");
889 /* This looks arbitrary. It is. */
890 s->busy = &parse_insn;
891 switch (insn->insn) {
893 ret = s->insn_read(dev, s, insn, data);
896 maxdata = s->maxdata_list
897 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
899 for (i = 0; i < insn->n; ++i) {
900 if (data[i] > maxdata) {
902 DPRINTK("bad data value(s)\n");
907 ret = s->insn_write(dev, s, insn, data);
914 ret = s->insn_bits(dev, s, insn, data);
917 ret = check_insn_config_length(insn, data);
920 ret = s->insn_config(dev, s, insn, data);
936 * synchronous instructions
942 * struct comedi_insn struct at arg
948 static int do_insn_ioctl(struct comedi_device *dev,
949 struct comedi_insn __user *arg, void *file)
951 struct comedi_insn insn;
952 unsigned int *data = NULL;
955 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
961 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
966 /* This is where the behavior of insn and insnlist deviate. */
967 if (insn.n > MAX_SAMPLES)
968 insn.n = MAX_SAMPLES;
969 if (insn.insn & INSN_MASK_WRITE) {
970 if (copy_from_user(data,
972 insn.n * sizeof(unsigned int))) {
977 ret = parse_insn(dev, &insn, data, file);
980 if (insn.insn & INSN_MASK_READ) {
981 if (copy_to_user(insn.data,
983 insn.n * sizeof(unsigned int))) {
996 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
997 unsigned mask, unsigned bits)
1001 spin_lock_irqsave(&s->spin_lock, flags);
1002 s->runflags &= ~mask;
1003 s->runflags |= (bits & mask);
1004 spin_unlock_irqrestore(&s->spin_lock, flags);
1007 static int do_cmd_ioctl(struct comedi_device *dev,
1008 struct comedi_cmd __user *cmd, void *file)
1010 struct comedi_cmd user_cmd;
1011 struct comedi_subdevice *s;
1012 struct comedi_async *async;
1014 unsigned int __user *chanlist_saver = NULL;
1016 if (copy_from_user(&user_cmd, cmd, sizeof(struct comedi_cmd))) {
1017 DPRINTK("bad cmd address\n");
1020 /* save user's chanlist pointer so it can be restored later */
1021 chanlist_saver = user_cmd.chanlist;
1023 if (user_cmd.subdev >= dev->n_subdevices) {
1024 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1028 s = dev->subdevices + user_cmd.subdev;
1031 if (s->type == COMEDI_SUBD_UNUSED) {
1032 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1036 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1037 DPRINTK("subdevice %i does not support commands\n",
1042 /* are we locked? (ioctl lock) */
1043 if (s->lock && s->lock != file) {
1044 DPRINTK("subdevice locked\n");
1050 DPRINTK("subdevice busy\n");
1055 /* make sure channel/gain list isn't too long */
1056 if (user_cmd.chanlist_len > s->len_chanlist) {
1057 DPRINTK("channel/gain list too long %u > %d\n",
1058 user_cmd.chanlist_len, s->len_chanlist);
1063 /* make sure channel/gain list isn't too short */
1064 if (user_cmd.chanlist_len < 1) {
1065 DPRINTK("channel/gain list too short %u < 1\n",
1066 user_cmd.chanlist_len);
1071 kfree(async->cmd.chanlist);
1072 async->cmd = user_cmd;
1073 async->cmd.data = NULL;
1074 /* load channel/gain list */
1075 async->cmd.chanlist =
1076 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1077 if (!async->cmd.chanlist) {
1078 DPRINTK("allocation failed\n");
1083 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1084 async->cmd.chanlist_len * sizeof(int))) {
1085 DPRINTK("fault reading chanlist\n");
1090 /* make sure each element in channel/gain list is valid */
1091 ret = comedi_check_chanlist(s,
1092 async->cmd.chanlist_len,
1093 async->cmd.chanlist);
1095 DPRINTK("bad chanlist\n");
1099 ret = s->do_cmdtest(dev, s, &async->cmd);
1101 if (async->cmd.flags & TRIG_BOGUS || ret) {
1102 DPRINTK("test returned %d\n", ret);
1103 user_cmd = async->cmd;
1104 /* restore chanlist pointer before copying back */
1105 user_cmd.chanlist = chanlist_saver;
1106 user_cmd.data = NULL;
1107 if (copy_to_user(cmd, &user_cmd, sizeof(struct comedi_cmd))) {
1108 DPRINTK("fault writing cmd\n");
1116 if (!async->prealloc_bufsz) {
1118 DPRINTK("no buffer (?)\n");
1122 comedi_reset_async_buf(async);
1125 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1127 if (async->cmd.flags & TRIG_WAKE_EOS)
1128 async->cb_mask |= COMEDI_CB_EOS;
1130 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1132 ret = s->do_cmd(dev, s);
1137 do_become_nonbusy(dev, s);
1144 command testing ioctl
1147 pointer to cmd structure
1150 cmd structure at arg
1154 modified cmd structure at arg
1157 static int do_cmdtest_ioctl(struct comedi_device *dev,
1158 struct comedi_cmd __user *arg, void *file)
1160 struct comedi_cmd user_cmd;
1161 struct comedi_subdevice *s;
1163 unsigned int *chanlist = NULL;
1164 unsigned int __user *chanlist_saver = NULL;
1166 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
1167 DPRINTK("bad cmd address\n");
1170 /* save user's chanlist pointer so it can be restored later */
1171 chanlist_saver = user_cmd.chanlist;
1173 if (user_cmd.subdev >= dev->n_subdevices) {
1174 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1178 s = dev->subdevices + user_cmd.subdev;
1179 if (s->type == COMEDI_SUBD_UNUSED) {
1180 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1184 if (!s->do_cmd || !s->do_cmdtest) {
1185 DPRINTK("subdevice %i does not support commands\n",
1190 /* make sure channel/gain list isn't too long */
1191 if (user_cmd.chanlist_len > s->len_chanlist) {
1192 DPRINTK("channel/gain list too long %d > %d\n",
1193 user_cmd.chanlist_len, s->len_chanlist);
1198 /* load channel/gain list */
1199 if (user_cmd.chanlist) {
1201 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1203 DPRINTK("allocation failed\n");
1208 if (copy_from_user(chanlist, user_cmd.chanlist,
1209 user_cmd.chanlist_len * sizeof(int))) {
1210 DPRINTK("fault reading chanlist\n");
1215 /* make sure each element in channel/gain list is valid */
1216 ret = comedi_check_chanlist(s, user_cmd.chanlist_len, chanlist);
1218 DPRINTK("bad chanlist\n");
1222 user_cmd.chanlist = chanlist;
1225 ret = s->do_cmdtest(dev, s, &user_cmd);
1227 /* restore chanlist pointer before copying back */
1228 user_cmd.chanlist = chanlist_saver;
1230 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
1231 DPRINTK("bad cmd address\n");
1256 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1260 unsigned long flags;
1261 struct comedi_subdevice *s;
1263 if (arg >= dev->n_subdevices)
1265 s = dev->subdevices + arg;
1267 spin_lock_irqsave(&s->spin_lock, flags);
1268 if (s->busy || s->lock)
1272 spin_unlock_irqrestore(&s->spin_lock, flags);
1279 ret = s->lock_f(dev, s);
1298 This function isn't protected by the semaphore, since
1299 we already own the lock.
1301 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1304 struct comedi_subdevice *s;
1306 if (arg >= dev->n_subdevices)
1308 s = dev->subdevices + arg;
1313 if (s->lock && s->lock != file)
1316 if (s->lock == file) {
1330 cancel acquisition ioctl
1342 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1345 struct comedi_subdevice *s;
1347 if (arg >= dev->n_subdevices)
1349 s = dev->subdevices + arg;
1350 if (s->async == NULL)
1353 if (s->lock && s->lock != file)
1359 if (s->busy != file)
1362 return do_cancel(dev, s);
1367 instructs driver to synchronize buffers
1379 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1382 struct comedi_subdevice *s;
1384 if (arg >= dev->n_subdevices)
1386 s = dev->subdevices + arg;
1388 if (s->lock && s->lock != file)
1394 if (s->busy != file)
1398 return s->poll(dev, s);
1403 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1407 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1408 ret = s->cancel(dev, s);
1410 do_become_nonbusy(dev, s);
1415 static void comedi_unmap(struct vm_area_struct *area)
1417 struct comedi_async *async;
1418 struct comedi_device *dev;
1420 async = area->vm_private_data;
1421 dev = async->subdevice->device;
1423 mutex_lock(&dev->mutex);
1424 async->mmap_count--;
1425 mutex_unlock(&dev->mutex);
1428 static struct vm_operations_struct comedi_vm_ops = {
1429 .close = comedi_unmap,
1432 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1434 const unsigned minor = iminor(file->f_dentry->d_inode);
1435 struct comedi_device_file_info *dev_file_info =
1436 comedi_get_device_file_info(minor);
1437 struct comedi_device *dev = dev_file_info->device;
1438 struct comedi_async *async = NULL;
1439 unsigned long start = vma->vm_start;
1444 struct comedi_subdevice *s;
1446 mutex_lock(&dev->mutex);
1447 if (!dev->attached) {
1448 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1452 if (vma->vm_flags & VM_WRITE)
1453 s = comedi_get_write_subdevice(dev_file_info);
1455 s = comedi_get_read_subdevice(dev_file_info);
1462 if (async == NULL) {
1467 if (vma->vm_pgoff != 0) {
1468 DPRINTK("comedi: mmap() offset must be 0.\n");
1473 size = vma->vm_end - vma->vm_start;
1474 if (size > async->prealloc_bufsz) {
1478 if (size & (~PAGE_MASK)) {
1483 n_pages = size >> PAGE_SHIFT;
1484 for (i = 0; i < n_pages; ++i) {
1485 if (remap_pfn_range(vma, start,
1486 page_to_pfn(virt_to_page
1487 (async->buf_page_list
1488 [i].virt_addr)), PAGE_SIZE,
1496 vma->vm_ops = &comedi_vm_ops;
1497 vma->vm_private_data = async;
1499 async->mmap_count++;
1503 mutex_unlock(&dev->mutex);
1507 static unsigned int comedi_poll(struct file *file, poll_table * wait)
1509 unsigned int mask = 0;
1510 const unsigned minor = iminor(file->f_dentry->d_inode);
1511 struct comedi_device_file_info *dev_file_info =
1512 comedi_get_device_file_info(minor);
1513 struct comedi_device *dev = dev_file_info->device;
1514 struct comedi_subdevice *read_subdev;
1515 struct comedi_subdevice *write_subdev;
1517 mutex_lock(&dev->mutex);
1518 if (!dev->attached) {
1519 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1520 mutex_unlock(&dev->mutex);
1525 read_subdev = comedi_get_read_subdevice(dev_file_info);
1527 poll_wait(file, &read_subdev->async->wait_head, wait);
1528 if (!read_subdev->busy
1529 || comedi_buf_read_n_available(read_subdev->async) > 0
1530 || !(comedi_get_subdevice_runflags(read_subdev) &
1532 mask |= POLLIN | POLLRDNORM;
1535 write_subdev = comedi_get_write_subdevice(dev_file_info);
1537 poll_wait(file, &write_subdev->async->wait_head, wait);
1538 comedi_buf_write_alloc(write_subdev->async,
1539 write_subdev->async->prealloc_bufsz);
1540 if (!write_subdev->busy
1541 || !(comedi_get_subdevice_runflags(write_subdev) &
1543 || comedi_buf_write_n_allocated(write_subdev->async) >=
1544 bytes_per_sample(write_subdev->async->subdevice)) {
1545 mask |= POLLOUT | POLLWRNORM;
1549 mutex_unlock(&dev->mutex);
1553 static ssize_t comedi_write(struct file *file, const char __user *buf,
1554 size_t nbytes, loff_t *offset)
1556 struct comedi_subdevice *s;
1557 struct comedi_async *async;
1558 int n, m, count = 0, retval = 0;
1559 DECLARE_WAITQUEUE(wait, current);
1560 const unsigned minor = iminor(file->f_dentry->d_inode);
1561 struct comedi_device_file_info *dev_file_info =
1562 comedi_get_device_file_info(minor);
1563 struct comedi_device *dev = dev_file_info->device;
1565 if (!dev->attached) {
1566 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1571 s = comedi_get_write_subdevice(dev_file_info);
1586 if (s->busy != file) {
1590 add_wait_queue(&async->wait_head, &wait);
1591 while (nbytes > 0 && !retval) {
1592 set_current_state(TASK_INTERRUPTIBLE);
1594 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1596 if (comedi_get_subdevice_runflags(s) &
1602 do_become_nonbusy(dev, s);
1610 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1611 m = async->prealloc_bufsz - async->buf_write_ptr;
1612 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1613 if (m > comedi_buf_write_n_allocated(async))
1614 m = comedi_buf_write_n_allocated(async);
1619 if (file->f_flags & O_NONBLOCK) {
1623 if (signal_pending(current)) {
1624 retval = -ERESTARTSYS;
1630 if (s->busy != file) {
1637 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1643 comedi_buf_write_free(async, n);
1649 break; /* makes device work like a pipe */
1651 set_current_state(TASK_RUNNING);
1652 remove_wait_queue(&async->wait_head, &wait);
1655 return count ? count : retval;
1658 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
1661 struct comedi_subdevice *s;
1662 struct comedi_async *async;
1663 int n, m, count = 0, retval = 0;
1664 DECLARE_WAITQUEUE(wait, current);
1665 const unsigned minor = iminor(file->f_dentry->d_inode);
1666 struct comedi_device_file_info *dev_file_info =
1667 comedi_get_device_file_info(minor);
1668 struct comedi_device *dev = dev_file_info->device;
1670 if (!dev->attached) {
1671 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1676 s = comedi_get_read_subdevice(dev_file_info);
1690 if (s->busy != file) {
1695 add_wait_queue(&async->wait_head, &wait);
1696 while (nbytes > 0 && !retval) {
1697 set_current_state(TASK_INTERRUPTIBLE);
1701 m = comedi_buf_read_n_available(async);
1702 /* printk("%d available\n",m); */
1703 if (async->buf_read_ptr + m > async->prealloc_bufsz)
1704 m = async->prealloc_bufsz - async->buf_read_ptr;
1705 /* printk("%d contiguous\n",m); */
1710 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1711 do_become_nonbusy(dev, s);
1712 if (comedi_get_subdevice_runflags(s) &
1720 if (file->f_flags & O_NONBLOCK) {
1724 if (signal_pending(current)) {
1725 retval = -ERESTARTSYS;
1733 if (s->busy != file) {
1739 m = copy_to_user(buf, async->prealloc_buf +
1740 async->buf_read_ptr, n);
1746 comedi_buf_read_alloc(async, n);
1747 comedi_buf_read_free(async, n);
1753 break; /* makes device work like a pipe */
1755 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
1756 async->buf_read_count - async->buf_write_count == 0) {
1757 do_become_nonbusy(dev, s);
1759 set_current_state(TASK_RUNNING);
1760 remove_wait_queue(&async->wait_head, &wait);
1763 return count ? count : retval;
1767 This function restores a subdevice to an idle state.
1769 void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
1771 struct comedi_async *async = s->async;
1773 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1775 comedi_reset_async_buf(async);
1776 async->inttrig = NULL;
1779 "BUG: (?) do_become_nonbusy called with async=0\n");
1785 static int comedi_open(struct inode *inode, struct file *file)
1787 const unsigned minor = iminor(inode);
1788 struct comedi_device_file_info *dev_file_info =
1789 comedi_get_device_file_info(minor);
1790 struct comedi_device *dev =
1791 dev_file_info ? dev_file_info->device : NULL;
1794 DPRINTK("invalid minor number\n");
1798 /* This is slightly hacky, but we want module autoloading
1800 * case: user opens device, attached -> ok
1801 * case: user opens device, unattached, in_request_module=0 -> autoload
1802 * case: user opens device, unattached, in_request_module=1 -> fail
1803 * case: root opens device, attached -> ok
1804 * case: root opens device, unattached, in_request_module=1 -> ok
1805 * (typically called from modprobe)
1806 * case: root opens device, unattached, in_request_module=0 -> autoload
1808 * The last could be changed to "-> ok", which would deny root
1811 mutex_lock(&dev->mutex);
1814 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
1815 DPRINTK("in request module\n");
1816 mutex_unlock(&dev->mutex);
1819 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
1822 dev->in_request_module = 1;
1825 mutex_unlock(&dev->mutex);
1826 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1827 mutex_lock(&dev->mutex);
1830 dev->in_request_module = 0;
1832 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
1833 DPRINTK("not attached and not CAP_NET_ADMIN\n");
1834 mutex_unlock(&dev->mutex);
1838 __module_get(THIS_MODULE);
1840 if (dev->attached) {
1841 if (!try_module_get(dev->driver->module)) {
1842 module_put(THIS_MODULE);
1843 mutex_unlock(&dev->mutex);
1848 if (dev->attached && dev->use_count == 0 && dev->open)
1853 mutex_unlock(&dev->mutex);
1858 static int comedi_close(struct inode *inode, struct file *file)
1860 const unsigned minor = iminor(inode);
1861 struct comedi_device_file_info *dev_file_info =
1862 comedi_get_device_file_info(minor);
1863 struct comedi_device *dev = dev_file_info->device;
1864 struct comedi_subdevice *s = NULL;
1867 mutex_lock(&dev->mutex);
1869 if (dev->subdevices) {
1870 for (i = 0; i < dev->n_subdevices; i++) {
1871 s = dev->subdevices + i;
1873 if (s->busy == file)
1875 if (s->lock == file)
1879 if (dev->attached && dev->use_count == 1 && dev->close)
1882 module_put(THIS_MODULE);
1884 module_put(dev->driver->module);
1888 mutex_unlock(&dev->mutex);
1890 if (file->f_flags & FASYNC)
1891 comedi_fasync(-1, file, 0);
1896 static int comedi_fasync(int fd, struct file *file, int on)
1898 const unsigned minor = iminor(file->f_dentry->d_inode);
1899 struct comedi_device_file_info *dev_file_info =
1900 comedi_get_device_file_info(minor);
1902 struct comedi_device *dev = dev_file_info->device;
1904 return fasync_helper(fd, file, on, &dev->async_queue);
1907 const struct file_operations comedi_fops = {
1908 .owner = THIS_MODULE,
1909 .unlocked_ioctl = comedi_unlocked_ioctl,
1910 .compat_ioctl = comedi_compat_ioctl,
1911 .open = comedi_open,
1912 .release = comedi_close,
1913 .read = comedi_read,
1914 .write = comedi_write,
1915 .mmap = comedi_mmap,
1916 .poll = comedi_poll,
1917 .fasync = comedi_fasync,
1920 struct class *comedi_class;
1921 static struct cdev comedi_cdev;
1923 static void comedi_cleanup_legacy_minors(void)
1927 for (i = 0; i < comedi_num_legacy_minors; i++)
1928 comedi_free_board_minor(i);
1931 static int __init comedi_init(void)
1936 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
1937 " - http://www.comedi.org\n");
1939 if (comedi_num_legacy_minors < 0 ||
1940 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
1941 printk(KERN_ERR "comedi: error: invalid value for module "
1942 "parameter \"comedi_num_legacy_minors\". Valid values "
1943 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
1948 * comedi is unusable if both comedi_autoconfig and
1949 * comedi_num_legacy_minors are zero, so we might as well adjust the
1950 * defaults in that case
1952 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
1953 comedi_num_legacy_minors = 16;
1955 memset(comedi_file_info_table, 0,
1956 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
1958 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1959 COMEDI_NUM_MINORS, "comedi");
1962 cdev_init(&comedi_cdev, &comedi_fops);
1963 comedi_cdev.owner = THIS_MODULE;
1964 kobject_set_name(&comedi_cdev.kobj, "comedi");
1965 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
1966 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1970 comedi_class = class_create(THIS_MODULE, "comedi");
1971 if (IS_ERR(comedi_class)) {
1972 printk(KERN_ERR "comedi: failed to create class");
1973 cdev_del(&comedi_cdev);
1974 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1976 return PTR_ERR(comedi_class);
1979 /* XXX requires /proc interface */
1982 /* create devices files for legacy/manual use */
1983 for (i = 0; i < comedi_num_legacy_minors; i++) {
1985 minor = comedi_alloc_board_minor(NULL);
1987 comedi_cleanup_legacy_minors();
1988 cdev_del(&comedi_cdev);
1989 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1998 static void __exit comedi_cleanup(void)
2002 comedi_cleanup_legacy_minors();
2003 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2004 BUG_ON(comedi_file_info_table[i]);
2006 class_destroy(comedi_class);
2007 cdev_del(&comedi_cdev);
2008 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2010 comedi_proc_cleanup();
2013 module_init(comedi_init);
2014 module_exit(comedi_cleanup);
2016 void comedi_error(const struct comedi_device *dev, const char *s)
2018 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
2019 dev->driver->driver_name, s);
2021 EXPORT_SYMBOL(comedi_error);
2023 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2025 struct comedi_async *async = s->async;
2026 unsigned runflags = 0;
2027 unsigned runflags_mask = 0;
2029 /* DPRINTK("comedi_event 0x%x\n",mask); */
2031 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2035 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2036 COMEDI_CB_OVERFLOW)) {
2037 runflags_mask |= SRF_RUNNING;
2039 /* remember if an error event has occured, so an error
2040 * can be returned the next time the user does a read() */
2041 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2042 runflags_mask |= SRF_ERROR;
2043 runflags |= SRF_ERROR;
2045 if (runflags_mask) {
2046 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2047 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2050 if (async->cb_mask & s->async->events) {
2051 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2052 wake_up_interruptible(&async->wait_head);
2053 if (s->subdev_flags & SDF_CMD_READ)
2054 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2055 if (s->subdev_flags & SDF_CMD_WRITE)
2056 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2059 async->cb_func(s->async->events, async->cb_arg);
2062 s->async->events = 0;
2064 EXPORT_SYMBOL(comedi_event);
2066 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
2068 unsigned long flags;
2071 spin_lock_irqsave(&s->spin_lock, flags);
2072 runflags = s->runflags;
2073 spin_unlock_irqrestore(&s->spin_lock, flags);
2076 EXPORT_SYMBOL(comedi_get_subdevice_runflags);
2078 static int is_device_busy(struct comedi_device *dev)
2080 struct comedi_subdevice *s;
2086 for (i = 0; i < dev->n_subdevices; i++) {
2087 s = dev->subdevices + i;
2090 if (s->async && s->async->mmap_count)
2097 static void comedi_device_init(struct comedi_device *dev)
2099 memset(dev, 0, sizeof(struct comedi_device));
2100 spin_lock_init(&dev->spinlock);
2101 mutex_init(&dev->mutex);
2105 static void comedi_device_cleanup(struct comedi_device *dev)
2109 mutex_lock(&dev->mutex);
2110 comedi_device_detach(dev);
2111 mutex_unlock(&dev->mutex);
2112 mutex_destroy(&dev->mutex);
2115 int comedi_alloc_board_minor(struct device *hardware_device)
2117 unsigned long flags;
2118 struct comedi_device_file_info *info;
2119 struct device *csdev;
2123 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2126 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2127 if (info->device == NULL) {
2131 comedi_device_init(info->device);
2132 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2133 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2134 if (comedi_file_info_table[i] == NULL) {
2135 comedi_file_info_table[i] = info;
2139 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2140 if (i == COMEDI_NUM_BOARD_MINORS) {
2141 comedi_device_cleanup(info->device);
2142 kfree(info->device);
2146 "ran out of minor numbers for board device files.\n");
2149 info->device->minor = i;
2150 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
2151 MKDEV(COMEDI_MAJOR, i), NULL,
2152 hardware_device, "comedi%i", i);
2154 info->device->class_dev = csdev;
2155 dev_set_drvdata(csdev, info);
2156 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2160 "failed to create sysfs attribute file \"%s\".\n",
2161 dev_attr_max_read_buffer_kb.attr.name);
2162 comedi_free_board_minor(i);
2165 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2169 "failed to create sysfs attribute file \"%s\".\n",
2170 dev_attr_read_buffer_kb.attr.name);
2171 comedi_free_board_minor(i);
2174 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2178 "failed to create sysfs attribute file \"%s\".\n",
2179 dev_attr_max_write_buffer_kb.attr.name);
2180 comedi_free_board_minor(i);
2183 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2187 "failed to create sysfs attribute file \"%s\".\n",
2188 dev_attr_write_buffer_kb.attr.name);
2189 comedi_free_board_minor(i);
2195 void comedi_free_board_minor(unsigned minor)
2197 unsigned long flags;
2198 struct comedi_device_file_info *info;
2200 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2201 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2202 info = comedi_file_info_table[minor];
2203 comedi_file_info_table[minor] = NULL;
2204 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2207 struct comedi_device *dev = info->device;
2209 if (dev->class_dev) {
2210 device_destroy(comedi_class,
2211 MKDEV(COMEDI_MAJOR, dev->minor));
2213 comedi_device_cleanup(dev);
2220 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2221 struct comedi_subdevice *s)
2223 unsigned long flags;
2224 struct comedi_device_file_info *info;
2225 struct device *csdev;
2229 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2233 info->read_subdevice = s;
2234 info->write_subdevice = s;
2235 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2236 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2237 if (comedi_file_info_table[i] == NULL) {
2238 comedi_file_info_table[i] = info;
2242 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2243 if (i == COMEDI_NUM_MINORS) {
2247 "ran out of minor numbers for board device files.\n");
2251 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
2252 MKDEV(COMEDI_MAJOR, i), NULL, NULL,
2253 "comedi%i_subd%i", dev->minor,
2254 (int)(s - dev->subdevices));
2256 s->class_dev = csdev;
2257 dev_set_drvdata(csdev, info);
2258 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2262 "failed to create sysfs attribute file \"%s\".\n",
2263 dev_attr_max_read_buffer_kb.attr.name);
2264 comedi_free_subdevice_minor(s);
2267 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2271 "failed to create sysfs attribute file \"%s\".\n",
2272 dev_attr_read_buffer_kb.attr.name);
2273 comedi_free_subdevice_minor(s);
2276 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2280 "failed to create sysfs attribute file \"%s\".\n",
2281 dev_attr_max_write_buffer_kb.attr.name);
2282 comedi_free_subdevice_minor(s);
2285 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2289 "failed to create sysfs attribute file \"%s\".\n",
2290 dev_attr_write_buffer_kb.attr.name);
2291 comedi_free_subdevice_minor(s);
2297 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2299 unsigned long flags;
2300 struct comedi_device_file_info *info;
2307 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2308 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2310 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2311 info = comedi_file_info_table[s->minor];
2312 comedi_file_info_table[s->minor] = NULL;
2313 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2316 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2317 s->class_dev = NULL;
2322 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2324 unsigned long flags;
2325 struct comedi_device_file_info *info;
2327 BUG_ON(minor >= COMEDI_NUM_MINORS);
2328 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2329 info = comedi_file_info_table[minor];
2330 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2333 EXPORT_SYMBOL_GPL(comedi_get_device_file_info);
2335 static int resize_async_buffer(struct comedi_device *dev,
2336 struct comedi_subdevice *s,
2337 struct comedi_async *async, unsigned new_size)
2341 if (new_size > async->max_bufsize)
2345 DPRINTK("subdevice is busy, cannot resize buffer\n");
2348 if (async->mmap_count) {
2349 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2353 if (!async->prealloc_buf)
2356 /* make sure buffer is an integral number of pages
2358 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2360 retval = comedi_buf_alloc(dev, s, new_size);
2364 if (s->buf_change) {
2365 retval = s->buf_change(dev, s, new_size);
2370 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2371 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
2375 /* sysfs attribute files */
2377 static const unsigned bytes_per_kibi = 1024;
2379 static ssize_t show_max_read_buffer_kb(struct device *dev,
2380 struct device_attribute *attr, char *buf)
2383 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2384 unsigned max_buffer_size_kb = 0;
2385 struct comedi_subdevice *const read_subdevice =
2386 comedi_get_read_subdevice(info);
2388 mutex_lock(&info->device->mutex);
2389 if (read_subdevice &&
2390 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2391 read_subdevice->async) {
2392 max_buffer_size_kb = read_subdevice->async->max_bufsize /
2395 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2396 mutex_unlock(&info->device->mutex);
2401 static ssize_t store_max_read_buffer_kb(struct device *dev,
2402 struct device_attribute *attr,
2403 const char *buf, size_t count)
2405 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2406 unsigned long new_max_size_kb;
2407 uint64_t new_max_size;
2408 struct comedi_subdevice *const read_subdevice =
2409 comedi_get_read_subdevice(info);
2411 if (strict_strtoul(buf, 10, &new_max_size_kb))
2413 if (new_max_size_kb != (uint32_t) new_max_size_kb)
2415 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2416 if (new_max_size != (uint32_t) new_max_size)
2419 mutex_lock(&info->device->mutex);
2420 if (read_subdevice == NULL ||
2421 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2422 read_subdevice->async == NULL) {
2423 mutex_unlock(&info->device->mutex);
2426 read_subdevice->async->max_bufsize = new_max_size;
2427 mutex_unlock(&info->device->mutex);
2432 static struct device_attribute dev_attr_max_read_buffer_kb = {
2434 .name = "max_read_buffer_kb",
2435 .mode = S_IRUGO | S_IWUSR},
2436 .show = &show_max_read_buffer_kb,
2437 .store = &store_max_read_buffer_kb
2440 static ssize_t show_read_buffer_kb(struct device *dev,
2441 struct device_attribute *attr, char *buf)
2444 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2445 unsigned buffer_size_kb = 0;
2446 struct comedi_subdevice *const read_subdevice =
2447 comedi_get_read_subdevice(info);
2449 mutex_lock(&info->device->mutex);
2450 if (read_subdevice &&
2451 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2452 read_subdevice->async) {
2453 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
2456 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2457 mutex_unlock(&info->device->mutex);
2462 static ssize_t store_read_buffer_kb(struct device *dev,
2463 struct device_attribute *attr,
2464 const char *buf, size_t count)
2466 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2467 unsigned long new_size_kb;
2470 struct comedi_subdevice *const read_subdevice =
2471 comedi_get_read_subdevice(info);
2473 if (strict_strtoul(buf, 10, &new_size_kb))
2475 if (new_size_kb != (uint32_t) new_size_kb)
2477 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2478 if (new_size != (uint32_t) new_size)
2481 mutex_lock(&info->device->mutex);
2482 if (read_subdevice == NULL ||
2483 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2484 read_subdevice->async == NULL) {
2485 mutex_unlock(&info->device->mutex);
2488 retval = resize_async_buffer(info->device, read_subdevice,
2489 read_subdevice->async, new_size);
2490 mutex_unlock(&info->device->mutex);
2497 static struct device_attribute dev_attr_read_buffer_kb = {
2499 .name = "read_buffer_kb",
2500 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
2501 .show = &show_read_buffer_kb,
2502 .store = &store_read_buffer_kb
2505 static ssize_t show_max_write_buffer_kb(struct device *dev,
2506 struct device_attribute *attr,
2510 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2511 unsigned max_buffer_size_kb = 0;
2512 struct comedi_subdevice *const write_subdevice =
2513 comedi_get_write_subdevice(info);
2515 mutex_lock(&info->device->mutex);
2516 if (write_subdevice &&
2517 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2518 write_subdevice->async) {
2519 max_buffer_size_kb = write_subdevice->async->max_bufsize /
2522 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2523 mutex_unlock(&info->device->mutex);
2528 static ssize_t store_max_write_buffer_kb(struct device *dev,
2529 struct device_attribute *attr,
2530 const char *buf, size_t count)
2532 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2533 unsigned long new_max_size_kb;
2534 uint64_t new_max_size;
2535 struct comedi_subdevice *const write_subdevice =
2536 comedi_get_write_subdevice(info);
2538 if (strict_strtoul(buf, 10, &new_max_size_kb))
2540 if (new_max_size_kb != (uint32_t) new_max_size_kb)
2542 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2543 if (new_max_size != (uint32_t) new_max_size)
2546 mutex_lock(&info->device->mutex);
2547 if (write_subdevice == NULL ||
2548 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2549 write_subdevice->async == NULL) {
2550 mutex_unlock(&info->device->mutex);
2553 write_subdevice->async->max_bufsize = new_max_size;
2554 mutex_unlock(&info->device->mutex);
2559 static struct device_attribute dev_attr_max_write_buffer_kb = {
2561 .name = "max_write_buffer_kb",
2562 .mode = S_IRUGO | S_IWUSR},
2563 .show = &show_max_write_buffer_kb,
2564 .store = &store_max_write_buffer_kb
2567 static ssize_t show_write_buffer_kb(struct device *dev,
2568 struct device_attribute *attr, char *buf)
2571 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2572 unsigned buffer_size_kb = 0;
2573 struct comedi_subdevice *const write_subdevice =
2574 comedi_get_write_subdevice(info);
2576 mutex_lock(&info->device->mutex);
2577 if (write_subdevice &&
2578 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2579 write_subdevice->async) {
2580 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
2583 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2584 mutex_unlock(&info->device->mutex);
2589 static ssize_t store_write_buffer_kb(struct device *dev,
2590 struct device_attribute *attr,
2591 const char *buf, size_t count)
2593 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2594 unsigned long new_size_kb;
2597 struct comedi_subdevice *const write_subdevice =
2598 comedi_get_write_subdevice(info);
2600 if (strict_strtoul(buf, 10, &new_size_kb))
2602 if (new_size_kb != (uint32_t) new_size_kb)
2604 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2605 if (new_size != (uint32_t) new_size)
2608 mutex_lock(&info->device->mutex);
2609 if (write_subdevice == NULL ||
2610 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2611 write_subdevice->async == NULL) {
2612 mutex_unlock(&info->device->mutex);
2615 retval = resize_async_buffer(info->device, write_subdevice,
2616 write_subdevice->async, new_size);
2617 mutex_unlock(&info->device->mutex);
2624 static struct device_attribute dev_attr_write_buffer_kb = {
2626 .name = "write_buffer_kb",
2627 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
2628 .show = &show_write_buffer_kb,
2629 .store = &store_write_buffer_kb