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 <linux/comedidev.h>
46 #include <linux/cdev.h>
47 #include <linux/stat.h>
50 #include <asm/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 module_param(comedi_debug, int, 0644);
63 int comedi_autoconfig = 1;
64 module_param(comedi_autoconfig, bool, 0444);
66 int comedi_num_legacy_minors = 0;
67 module_param(comedi_num_legacy_minors, int, 0444);
69 static DEFINE_SPINLOCK(comedi_file_info_table_lock);
70 static struct comedi_device_file_info* 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 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);
95 static int resize_async_buffer(comedi_device *dev,
96 comedi_subdevice *s, comedi_async *async, unsigned new_size);
98 //declarations for sysfs attribute files
99 struct device_attribute dev_attr_max_read_buffer_kb;
100 struct device_attribute dev_attr_read_buffer_kb;
101 struct device_attribute dev_attr_max_write_buffer_kb;
102 struct device_attribute dev_attr_write_buffer_kb;
104 #ifdef HAVE_UNLOCKED_IOCTL
105 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
108 static int comedi_ioctl(struct inode *inode, struct file *file,
109 unsigned int cmd, unsigned long arg)
112 const unsigned minor = iminor(file->f_dentry->d_inode);
113 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
117 if(dev_file_info == NULL || dev_file_info->device == NULL) return -ENODEV;
118 dev = dev_file_info->device;
120 mutex_lock(&dev->mutex);
122 /* Device config is special, because it must work on
123 * an unconfigured device. */
124 if (cmd == COMEDI_DEVCONFIG) {
125 rc = do_devconfig_ioctl(dev, (void *)arg);
129 if (!dev->attached) {
130 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
136 case COMEDI_BUFCONFIG:
137 rc = do_bufconfig_ioctl(dev, (void *)arg);
140 rc = do_devinfo_ioctl(dev, (void *)arg, file);
142 case COMEDI_SUBDINFO:
143 rc = do_subdinfo_ioctl(dev, (void *)arg, file);
145 case COMEDI_CHANINFO:
146 rc = do_chaninfo_ioctl(dev, (void *)arg);
148 case COMEDI_RANGEINFO:
149 rc = do_rangeinfo_ioctl(dev, (void *)arg);
152 rc = do_bufinfo_ioctl(dev, (void *)arg);
155 rc = do_lock_ioctl(dev, arg, file);
158 rc = do_unlock_ioctl(dev, arg, file);
161 rc = do_cancel_ioctl(dev, arg, file);
164 rc = do_cmd_ioctl(dev, (void *)arg, file);
167 rc = do_cmdtest_ioctl(dev, (void *)arg, file);
169 case COMEDI_INSNLIST:
170 rc = do_insnlist_ioctl(dev, (void *)arg, file);
173 rc = do_insn_ioctl(dev, (void *)arg, file);
176 rc = do_poll_ioctl(dev, arg, file);
184 mutex_unlock(&dev->mutex);
193 pointer to devconfig structure
196 devconfig structure at arg
201 static int do_devconfig_ioctl(comedi_device * dev, comedi_devconfig * arg)
205 unsigned char *aux_data = NULL;
208 if (!capable(CAP_SYS_ADMIN))
212 if (is_device_busy(dev))
216 struct module *driver_module = dev->driver->module;
217 comedi_device_detach(dev);
218 module_put(driver_module);
223 if (copy_from_user(&it, arg, sizeof(comedi_devconfig)))
226 it.board_name[COMEDI_NAMELEN - 1] = 0;
228 if (comedi_aux_data(it.options, 0) &&
229 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
231 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
235 aux_data = vmalloc(aux_len);
239 if (copy_from_user(aux_data,
240 comedi_aux_data(it.options, 0), aux_len)) {
244 it.options[COMEDI_DEVCONF_AUX_DATA_LO] =
245 (unsigned long)aux_data;
246 if (sizeof(void *) > sizeof(int)) {
247 bit_shift = sizeof(int) * 8;
248 it.options[COMEDI_DEVCONF_AUX_DATA_HI] =
249 ((unsigned long)aux_data) >> bit_shift;
251 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
254 ret = comedi_device_attach(dev, &it);
257 if(!try_module_get(dev->driver->module)) {
258 comedi_device_detach(dev);
271 buffer configuration ioctl
274 pointer to bufconfig structure
280 modified bufconfig at arg
283 static int do_bufconfig_ioctl(comedi_device * dev, void *arg)
290 if (copy_from_user(&bc, arg, sizeof(comedi_bufconfig)))
293 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
296 s = dev->subdevices + bc.subdevice;
300 DPRINTK("subdevice does not have async capability\n");
306 if (bc.maximum_size) {
307 if (!capable(CAP_SYS_ADMIN))
310 async->max_bufsize = bc.maximum_size;
314 retval = resize_async_buffer(dev, s, async, bc.size);
315 if(retval < 0) return retval;
318 bc.size = async->prealloc_bufsz;
319 bc.maximum_size = async->max_bufsize;
322 if (copy_to_user(arg, &bc, sizeof(comedi_bufconfig)))
333 pointer to devinfo structure
342 static int do_devinfo_ioctl(comedi_device * dev, comedi_devinfo * arg,
345 comedi_devinfo devinfo;
346 const unsigned minor = iminor(file->f_dentry->d_inode);
347 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
348 comedi_subdevice *read_subdev = comedi_get_read_subdevice(dev_file_info);
349 comedi_subdevice *write_subdev = comedi_get_write_subdevice(dev_file_info);
351 memset(&devinfo, 0, sizeof(devinfo));
353 /* fill devinfo structure */
354 devinfo.version_code = COMEDI_VERSION_CODE;
355 devinfo.n_subdevs = dev->n_subdevices;
356 memcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
357 memcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
360 devinfo.read_subdevice = read_subdev - dev->subdevices;
362 devinfo.read_subdevice = -1;
365 devinfo.write_subdevice = write_subdev - dev->subdevices;
367 devinfo.write_subdevice = -1;
370 if (copy_to_user(arg, &devinfo, sizeof(comedi_devinfo)))
381 pointer to array of subdevice info structures
387 array of subdevice info structures at arg
390 static int do_subdinfo_ioctl(comedi_device * dev, comedi_subdinfo * arg,
394 comedi_subdinfo *tmp, *us;
397 tmp = kcalloc(dev->n_subdevices, sizeof(comedi_subdinfo), GFP_KERNEL);
401 /* fill subdinfo structs */
402 for (i = 0; i < dev->n_subdevices; i++) {
403 s = dev->subdevices + i;
407 us->n_chan = s->n_chan;
408 us->subd_flags = s->subdev_flags;
409 if (comedi_get_subdevice_runflags(s) & SRF_RUNNING)
410 us->subd_flags |= SDF_RUNNING;
411 #define TIMER_nanosec 5 /* backwards compatibility */
412 us->timer_type = TIMER_nanosec;
413 us->len_chanlist = s->len_chanlist;
414 us->maxdata = s->maxdata;
415 if (s->range_table) {
417 (i << 24) | (0 << 16) | (s->
418 range_table->length);
420 us->range_type = 0; /* XXX */
422 us->flags = s->flags;
425 us->subd_flags |= SDF_BUSY;
427 us->subd_flags |= SDF_BUSY_OWNER;
429 us->subd_flags |= SDF_LOCKED;
431 us->subd_flags |= SDF_LOCK_OWNER;
432 if (!s->maxdata && s->maxdata_list)
433 us->subd_flags |= SDF_MAXDATA;
435 us->subd_flags |= SDF_FLAGS;
436 if (s->range_table_list)
437 us->subd_flags |= SDF_RANGETYPE;
439 us->subd_flags |= SDF_CMD;
441 if (s->insn_bits != &insn_inval)
442 us->insn_bits_support = COMEDI_SUPPORTED;
444 us->insn_bits_support = COMEDI_UNSUPPORTED;
446 us->settling_time_0 = s->settling_time_0;
449 ret = copy_to_user(arg, tmp,
450 dev->n_subdevices * sizeof(comedi_subdinfo));
454 return ret ? -EFAULT : 0;
462 pointer to chaninfo structure
465 chaninfo structure at arg
468 arrays at elements of chaninfo structure
471 static int do_chaninfo_ioctl(comedi_device * dev, comedi_chaninfo * arg)
476 if (copy_from_user(&it, arg, sizeof(comedi_chaninfo)))
479 if (it.subdev >= dev->n_subdevices)
481 s = dev->subdevices + it.subdev;
483 if (it.maxdata_list) {
484 if (s->maxdata || !s->maxdata_list)
486 if (copy_to_user(it.maxdata_list, s->maxdata_list,
487 s->n_chan * sizeof(lsampl_t)))
494 if (copy_to_user(it.flaglist, s->flaglist,
495 s->n_chan * sizeof(unsigned int)))
502 if (!s->range_table_list)
504 for (i = 0; i < s->n_chan; i++) {
507 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
508 (s->range_table_list[i]->length);
509 put_user(x, it.rangelist + i);
511 //if(copy_to_user(it.rangelist,s->range_type_list,s->n_chan*sizeof(unsigned int)))
520 buffer information ioctl
523 pointer to bufinfo structure
529 modified bufinfo at arg
532 static int do_bufinfo_ioctl(comedi_device * dev, void *arg)
538 if (copy_from_user(&bi, arg, sizeof(comedi_bufinfo)))
541 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
544 s = dev->subdevices + bi.subdevice;
548 DPRINTK("subdevice does not have async capability\n");
549 bi.buf_write_ptr = 0;
551 bi.buf_write_count = 0;
552 bi.buf_read_count = 0;
556 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
557 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
558 comedi_buf_read_free(async, bi.bytes_read);
560 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
562 && async->buf_write_count == async->buf_read_count) {
563 do_become_nonbusy(dev, s);
567 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
569 comedi_buf_write_alloc(async, bi.bytes_written);
570 comedi_buf_write_free(async, bi.bytes_written);
573 bi.buf_write_count = async->buf_write_count;
574 bi.buf_write_ptr = async->buf_write_ptr;
575 bi.buf_read_count = async->buf_read_count;
576 bi.buf_read_ptr = async->buf_read_ptr;
579 if (copy_to_user(arg, &bi, sizeof(comedi_bufinfo)))
585 static int parse_insn(comedi_device * dev, comedi_insn * insn, lsampl_t * data,
589 * synchronous instructions
592 * pointer to sync cmd structure
595 * sync cmd struct at arg
602 /* arbitrary limits */
603 #define MAX_SAMPLES 256
604 static int do_insnlist_ioctl(comedi_device * dev, void *arg, void *file)
606 comedi_insnlist insnlist;
607 comedi_insn *insns = NULL;
608 lsampl_t *data = NULL;
612 if (copy_from_user(&insnlist, arg, sizeof(comedi_insnlist)))
615 data = kmalloc(sizeof(lsampl_t) * MAX_SAMPLES, GFP_KERNEL);
617 DPRINTK("kmalloc failed\n");
622 insns = kmalloc(sizeof(comedi_insn) * insnlist.n_insns, GFP_KERNEL);
624 DPRINTK("kmalloc failed\n");
629 if (copy_from_user(insns, insnlist.insns,
630 sizeof(comedi_insn) * insnlist.n_insns)) {
631 DPRINTK("copy_from_user failed\n");
636 for (i = 0; i < insnlist.n_insns; i++) {
637 if (insns[i].n > MAX_SAMPLES) {
638 DPRINTK("number of samples too large\n");
642 if (insns[i].insn & INSN_MASK_WRITE) {
643 if (copy_from_user(data, insns[i].data,
644 insns[i].n * sizeof(lsampl_t))) {
645 DPRINTK("copy_from_user failed\n");
650 ret = parse_insn(dev, insns + i, data, file);
653 if (insns[i].insn & INSN_MASK_READ) {
654 if (copy_to_user(insns[i].data, data,
655 insns[i].n * sizeof(lsampl_t))) {
656 DPRINTK("copy_to_user failed\n");
676 static int check_insn_config_length(comedi_insn * insn, lsampl_t * data)
678 if(insn->n < 1) return -EINVAL;
681 case INSN_CONFIG_DIO_OUTPUT:
682 case INSN_CONFIG_DIO_INPUT:
683 case INSN_CONFIG_DISARM:
684 case INSN_CONFIG_RESET:
688 case INSN_CONFIG_ARM:
689 case INSN_CONFIG_DIO_QUERY:
690 case INSN_CONFIG_BLOCK_SIZE:
691 case INSN_CONFIG_FILTER:
692 case INSN_CONFIG_SERIAL_CLOCK:
693 case INSN_CONFIG_BIDIRECTIONAL_DATA:
694 case INSN_CONFIG_ALT_SOURCE:
695 case INSN_CONFIG_SET_COUNTER_MODE:
696 case INSN_CONFIG_8254_READ_STATUS:
697 case INSN_CONFIG_SET_ROUTING:
698 case INSN_CONFIG_GET_ROUTING:
699 case INSN_CONFIG_GET_PWM_STATUS:
700 case INSN_CONFIG_PWM_SET_PERIOD:
701 case INSN_CONFIG_PWM_GET_PERIOD:
705 case INSN_CONFIG_SET_GATE_SRC:
706 case INSN_CONFIG_GET_GATE_SRC:
707 case INSN_CONFIG_SET_CLOCK_SRC:
708 case INSN_CONFIG_GET_CLOCK_SRC:
709 case INSN_CONFIG_SET_OTHER_SRC:
710 case INSN_CONFIG_GET_COUNTER_STATUS:
711 case INSN_CONFIG_PWM_SET_H_BRIDGE:
712 case INSN_CONFIG_PWM_GET_H_BRIDGE:
713 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
717 case INSN_CONFIG_PWM_OUTPUT:
718 case INSN_CONFIG_ANALOG_TRIG:
722 //by default we allow the insn since we don't have checks for all possible cases yet
725 ("comedi: no check for data length of config insn id %i is implemented.\n"
726 " Add a check to %s in %s.\n"
727 " Assuming n=%i is correct.\n", data[0], __FUNCTION__,
735 static int parse_insn(comedi_device * dev, comedi_insn * insn, lsampl_t * data,
742 if (insn->insn & INSN_MASK_SPECIAL) {
743 /* a non-subdevice instruction */
745 switch (insn->insn) {
755 do_gettimeofday(&tv);
757 data[1] = tv.tv_usec;
763 if (insn->n != 1 || data[0] >= 100000) {
767 udelay(data[0] / 1000);
775 if (insn->subdev >= dev->n_subdevices) {
776 DPRINTK("%d not usable subdevice\n",
781 s = dev->subdevices + insn->subdev;
783 DPRINTK("no async\n");
787 if (!s->async->inttrig) {
788 DPRINTK("no inttrig\n");
792 ret = s->async->inttrig(dev, s, insn->data[0]);
797 DPRINTK("invalid insn\n");
802 /* a subdevice instruction */
805 if (insn->subdev >= dev->n_subdevices) {
806 DPRINTK("subdevice %d out of range\n", insn->subdev);
810 s = dev->subdevices + insn->subdev;
812 if (s->type == COMEDI_SUBD_UNUSED) {
813 DPRINTK("%d not usable subdevice\n", insn->subdev);
818 /* are we locked? (ioctl lock) */
819 if (s->lock && s->lock != file) {
820 DPRINTK("device locked\n");
825 if ((ret = check_chanlist(s, 1, &insn->chanspec)) < 0) {
827 DPRINTK("bad chanspec\n");
835 /* This looks arbitrary. It is. */
836 s->busy = &parse_insn;
837 switch (insn->insn) {
839 ret = s->insn_read(dev, s, insn, data);
842 maxdata = s->maxdata_list
843 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
845 for (i = 0; i < insn->n; ++i) {
846 if (data[i] > maxdata) {
848 DPRINTK("bad data value(s)\n");
853 ret = s->insn_write(dev, s, insn, data);
860 ret = s->insn_bits(dev, s, insn, data);
863 ret = check_insn_config_length(insn, data);
866 ret = s->insn_config(dev, s, insn, data);
882 * synchronous instructions
888 * comedi_insn struct at arg
894 static int do_insn_ioctl(comedi_device * dev, void *arg, void *file)
897 lsampl_t *data = NULL;
900 data = kmalloc(sizeof(lsampl_t) * MAX_SAMPLES, GFP_KERNEL);
906 if (copy_from_user(&insn, arg, sizeof(comedi_insn))) {
911 /* This is where the behavior of insn and insnlist deviate. */
912 if (insn.n > MAX_SAMPLES)
913 insn.n = MAX_SAMPLES;
914 if (insn.insn & INSN_MASK_WRITE) {
915 if (copy_from_user(data, insn.data, insn.n * sizeof(lsampl_t))) {
920 ret = parse_insn(dev, &insn, data, file);
923 if (insn.insn & INSN_MASK_READ) {
924 if (copy_to_user(insn.data, data, insn.n * sizeof(lsampl_t))) {
943 pointer to cmd structure
950 modified cmd structure at arg
953 static int do_cmd_ioctl(comedi_device * dev, void *arg, void *file)
959 unsigned int *chanlist_saver = NULL;
961 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
962 DPRINTK("bad cmd address\n");
965 // save user's chanlist pointer so it can be restored later
966 chanlist_saver = user_cmd.chanlist;
968 if (user_cmd.subdev >= dev->n_subdevices) {
969 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
973 s = dev->subdevices + user_cmd.subdev;
976 if (s->type == COMEDI_SUBD_UNUSED) {
977 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
981 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
982 DPRINTK("subdevice %i does not support commands\n",
987 /* are we locked? (ioctl lock) */
988 if (s->lock && s->lock != file) {
989 DPRINTK("subdevice locked\n");
995 DPRINTK("subdevice busy\n");
1000 /* make sure channel/gain list isn't too long */
1001 if (user_cmd.chanlist_len > s->len_chanlist) {
1002 DPRINTK("channel/gain list too long %u > %d\n",
1003 user_cmd.chanlist_len, s->len_chanlist);
1008 /* make sure channel/gain list isn't too short */
1009 if (user_cmd.chanlist_len < 1) {
1010 DPRINTK("channel/gain list too short %u < 1\n",
1011 user_cmd.chanlist_len);
1016 if (async->cmd.chanlist)
1017 kfree(async->cmd.chanlist);
1018 async->cmd = user_cmd;
1019 async->cmd.data = NULL;
1020 /* load channel/gain list */
1021 async->cmd.chanlist =
1022 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1023 if (!async->cmd.chanlist) {
1024 DPRINTK("allocation failed\n");
1029 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1030 async->cmd.chanlist_len * sizeof(int))) {
1031 DPRINTK("fault reading chanlist\n");
1036 /* make sure each element in channel/gain list is valid */
1037 if ((ret = check_chanlist(s, async->cmd.chanlist_len,
1038 async->cmd.chanlist)) < 0) {
1039 DPRINTK("bad chanlist\n");
1043 ret = s->do_cmdtest(dev, s, &async->cmd);
1045 if (async->cmd.flags & TRIG_BOGUS || ret) {
1046 DPRINTK("test returned %d\n", ret);
1047 user_cmd = async->cmd;
1048 // restore chanlist pointer before copying back
1049 user_cmd.chanlist = chanlist_saver;
1050 user_cmd.data = NULL;
1051 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1052 DPRINTK("fault writing cmd\n");
1060 if (!async->prealloc_bufsz) {
1062 DPRINTK("no buffer (?)\n");
1066 comedi_reset_async_buf(async);
1069 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1071 if (async->cmd.flags & TRIG_WAKE_EOS) {
1072 async->cb_mask |= COMEDI_CB_EOS;
1075 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1077 #ifdef CONFIG_COMEDI_RT
1078 if (async->cmd.flags & TRIG_RT) {
1079 if (comedi_switch_to_rt(dev) == 0)
1080 comedi_set_subdevice_runflags(s, SRF_RT, SRF_RT);
1084 ret = s->do_cmd(dev, s);
1089 do_become_nonbusy(dev, s);
1096 command testing ioctl
1099 pointer to cmd structure
1102 cmd structure at arg
1106 modified cmd structure at arg
1109 static int do_cmdtest_ioctl(comedi_device * dev, void *arg, void *file)
1111 comedi_cmd user_cmd;
1112 comedi_subdevice *s;
1114 unsigned int *chanlist = NULL;
1115 unsigned int *chanlist_saver = NULL;
1117 if (copy_from_user(&user_cmd, arg, sizeof(comedi_cmd))) {
1118 DPRINTK("bad cmd address\n");
1121 // save user's chanlist pointer so it can be restored later
1122 chanlist_saver = user_cmd.chanlist;
1124 if (user_cmd.subdev >= dev->n_subdevices) {
1125 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1129 s = dev->subdevices + user_cmd.subdev;
1130 if (s->type == COMEDI_SUBD_UNUSED) {
1131 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1135 if (!s->do_cmd || !s->do_cmdtest) {
1136 DPRINTK("subdevice %i does not support commands\n",
1141 /* make sure channel/gain list isn't too long */
1142 if (user_cmd.chanlist_len > s->len_chanlist) {
1143 DPRINTK("channel/gain list too long %d > %d\n",
1144 user_cmd.chanlist_len, s->len_chanlist);
1149 /* load channel/gain list */
1150 if (user_cmd.chanlist) {
1152 kmalloc(user_cmd.chanlist_len * sizeof(int),
1155 DPRINTK("allocation failed\n");
1160 if (copy_from_user(chanlist, user_cmd.chanlist,
1161 user_cmd.chanlist_len * sizeof(int))) {
1162 DPRINTK("fault reading chanlist\n");
1167 /* make sure each element in channel/gain list is valid */
1168 if ((ret = check_chanlist(s, user_cmd.chanlist_len,
1170 DPRINTK("bad chanlist\n");
1174 user_cmd.chanlist = chanlist;
1177 ret = s->do_cmdtest(dev, s, &user_cmd);
1179 // restore chanlist pointer before copying back
1180 user_cmd.chanlist = chanlist_saver;
1182 if (copy_to_user(arg, &user_cmd, sizeof(comedi_cmd))) {
1183 DPRINTK("bad cmd address\n");
1209 static int do_lock_ioctl(comedi_device * dev, unsigned int arg, void *file)
1212 unsigned long flags;
1213 comedi_subdevice *s;
1215 if (arg >= dev->n_subdevices)
1217 s = dev->subdevices + arg;
1219 comedi_spin_lock_irqsave(&s->spin_lock, flags);
1220 if (s->busy || s->lock) {
1225 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
1232 ret = s->lock_f(dev, s);
1251 This function isn't protected by the semaphore, since
1252 we already own the lock.
1254 static int do_unlock_ioctl(comedi_device * dev, unsigned int arg, void *file)
1256 comedi_subdevice *s;
1258 if (arg >= dev->n_subdevices)
1260 s = dev->subdevices + arg;
1265 if (s->lock && s->lock != file)
1268 if (s->lock == file) {
1282 cancel acquisition ioctl
1294 static int do_cancel_ioctl(comedi_device * dev, unsigned int arg, void *file)
1296 comedi_subdevice *s;
1298 if (arg >= dev->n_subdevices)
1300 s = dev->subdevices + arg;
1301 if (s->async == NULL)
1304 if (s->lock && s->lock != file)
1310 if (s->busy != file)
1313 return do_cancel(dev, s);
1318 instructs driver to synchronize buffers
1330 static int do_poll_ioctl(comedi_device * dev, unsigned int arg, void *file)
1332 comedi_subdevice *s;
1334 if (arg >= dev->n_subdevices)
1336 s = dev->subdevices + arg;
1338 if (s->lock && s->lock != file)
1344 if (s->busy != file)
1348 return s->poll(dev, s);
1353 static int do_cancel(comedi_device * dev, comedi_subdevice * s)
1357 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1358 ret = s->cancel(dev, s);
1360 do_become_nonbusy(dev, s);
1365 void comedi_unmap(struct vm_area_struct *area)
1367 comedi_async *async;
1370 async = area->vm_private_data;
1371 dev = async->subdevice->device;
1373 mutex_lock(&dev->mutex);
1374 async->mmap_count--;
1375 mutex_unlock(&dev->mutex);
1378 static struct vm_operations_struct comedi_vm_ops = {
1382 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1384 const unsigned minor = iminor(file->f_dentry->d_inode);
1385 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1386 comedi_device *dev = dev_file_info->device;
1387 comedi_async *async = NULL;
1388 unsigned long start = vma->vm_start;
1393 comedi_subdevice *s;
1395 mutex_lock(&dev->mutex);
1396 if (!dev->attached) {
1397 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1401 if (vma->vm_flags & VM_WRITE) {
1402 s = comedi_get_write_subdevice(dev_file_info);
1404 s = comedi_get_read_subdevice(dev_file_info);
1411 if (async == NULL) {
1416 if (vma->vm_pgoff != 0) {
1417 DPRINTK("comedi: mmap() offset must be 0.\n");
1422 size = vma->vm_end - vma->vm_start;
1423 if (size > async->prealloc_bufsz) {
1427 if (size & (~PAGE_MASK)) {
1432 n_pages = size >> PAGE_SHIFT;
1433 for (i = 0; i < n_pages; ++i) {
1434 if (remap_pfn_range(vma, start,
1435 page_to_pfn(virt_to_page(async->
1436 buf_page_list[i].virt_addr)),
1437 PAGE_SIZE, PAGE_SHARED)) {
1444 vma->vm_ops = &comedi_vm_ops;
1445 vma->vm_private_data = async;
1447 async->mmap_count++;
1451 mutex_unlock(&dev->mutex);
1455 static unsigned int comedi_poll(struct file *file, poll_table * wait)
1457 unsigned int mask = 0;
1458 const unsigned minor = iminor(file->f_dentry->d_inode);
1459 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1460 comedi_device *dev = dev_file_info->device;
1461 comedi_subdevice *read_subdev;
1462 comedi_subdevice *write_subdev;
1464 mutex_lock(&dev->mutex);
1465 if (!dev->attached) {
1466 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1467 mutex_unlock(&dev->mutex);
1472 read_subdev = comedi_get_read_subdevice(dev_file_info);
1474 poll_wait(file, &read_subdev->async->wait_head, wait);
1475 if (!read_subdev->busy
1476 || comedi_buf_read_n_available(read_subdev->async) > 0
1477 || !(comedi_get_subdevice_runflags(read_subdev) &
1479 mask |= POLLIN | POLLRDNORM;
1482 write_subdev = comedi_get_write_subdevice(dev_file_info);
1484 poll_wait(file, &write_subdev->async->wait_head, wait);
1485 comedi_buf_write_alloc(write_subdev->async, write_subdev->async->prealloc_bufsz);
1486 if (!write_subdev->busy
1487 || !(comedi_get_subdevice_runflags(write_subdev) &
1489 || comedi_buf_write_n_allocated(write_subdev->async) >=
1490 bytes_per_sample(write_subdev->async->subdevice)) {
1491 mask |= POLLOUT | POLLWRNORM;
1495 mutex_unlock(&dev->mutex);
1499 static ssize_t comedi_write(struct file *file, const char *buf, size_t nbytes,
1502 comedi_subdevice *s;
1503 comedi_async *async;
1504 int n, m, count = 0, retval = 0;
1505 DECLARE_WAITQUEUE(wait, current);
1506 const unsigned minor = iminor(file->f_dentry->d_inode);
1507 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1508 comedi_device *dev = dev_file_info->device;
1510 if (!dev->attached) {
1511 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1516 s = comedi_get_write_subdevice(dev_file_info);
1531 if (s->busy != file) {
1535 add_wait_queue(&async->wait_head, &wait);
1536 while (nbytes > 0 && !retval) {
1537 set_current_state(TASK_INTERRUPTIBLE);
1542 if (async->buf_write_ptr + m > async->prealloc_bufsz) {
1543 m = async->prealloc_bufsz - async->buf_write_ptr;
1545 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1546 if (m > comedi_buf_write_n_allocated(async)) {
1547 m = comedi_buf_write_n_allocated(async);
1553 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1554 if (comedi_get_subdevice_runflags(s) &
1560 do_become_nonbusy(dev, s);
1563 if (file->f_flags & O_NONBLOCK) {
1567 if (signal_pending(current)) {
1568 retval = -ERESTARTSYS;
1575 if (s->busy != file) {
1582 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1588 comedi_buf_write_free(async, n);
1594 break; /* makes device work like a pipe */
1596 set_current_state(TASK_RUNNING);
1597 remove_wait_queue(&async->wait_head, &wait);
1600 return (count ? count : retval);
1603 static ssize_t comedi_read(struct file *file, char *buf, size_t nbytes,
1606 comedi_subdevice *s;
1607 comedi_async *async;
1608 int n, m, count = 0, retval = 0;
1609 DECLARE_WAITQUEUE(wait, current);
1610 const unsigned minor = iminor(file->f_dentry->d_inode);
1611 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1612 comedi_device *dev = dev_file_info->device;
1614 if (!dev->attached) {
1615 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1620 s = comedi_get_read_subdevice(dev_file_info);
1634 if (s->busy != file) {
1639 add_wait_queue(&async->wait_head, &wait);
1640 while (nbytes > 0 && !retval) {
1641 set_current_state(TASK_INTERRUPTIBLE);
1645 m = comedi_buf_read_n_available(async);
1646 //printk("%d available\n",m);
1647 if (async->buf_read_ptr + m > async->prealloc_bufsz) {
1648 m = async->prealloc_bufsz - async->buf_read_ptr;
1650 //printk("%d contiguous\n",m);
1655 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1656 do_become_nonbusy(dev, s);
1657 if (comedi_get_subdevice_runflags(s) &
1665 if (file->f_flags & O_NONBLOCK) {
1669 if (signal_pending(current)) {
1670 retval = -ERESTARTSYS;
1678 if (s->busy != file) {
1684 m = copy_to_user(buf, async->prealloc_buf +
1685 async->buf_read_ptr, n);
1691 comedi_buf_read_alloc(async, n);
1692 comedi_buf_read_free(async, n);
1698 break; /* makes device work like a pipe */
1700 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
1701 async->buf_read_count - async->buf_write_count == 0) {
1702 do_become_nonbusy(dev, s);
1704 set_current_state(TASK_RUNNING);
1705 remove_wait_queue(&async->wait_head, &wait);
1708 return (count ? count : retval);
1712 This function restores a subdevice to an idle state.
1714 void do_become_nonbusy(comedi_device * dev, comedi_subdevice * s)
1716 comedi_async *async = s->async;
1718 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1719 #ifdef CONFIG_COMEDI_RT
1720 if (comedi_get_subdevice_runflags(s) & SRF_RT) {
1721 comedi_switch_to_non_rt(dev);
1722 comedi_set_subdevice_runflags(s, SRF_RT, 0);
1726 comedi_reset_async_buf(async);
1727 async->inttrig = NULL;
1729 printk("BUG: (?) do_become_nonbusy called with async=0\n");
1735 static int comedi_open(struct inode *inode, struct file *file)
1737 const unsigned minor = iminor(inode);
1738 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1739 comedi_device *dev = dev_file_info ? dev_file_info->device : NULL;
1741 DPRINTK("invalid minor number\n");
1745 /* This is slightly hacky, but we want module autoloading
1747 * case: user opens device, attached -> ok
1748 * case: user opens device, unattached, in_request_module=0 -> autoload
1749 * case: user opens device, unattached, in_request_module=1 -> fail
1750 * case: root opens device, attached -> ok
1751 * case: root opens device, unattached, in_request_module=1 -> ok
1752 * (typically called from modprobe)
1753 * case: root opens device, unattached, in_request_module=0 -> autoload
1755 * The last could be changed to "-> ok", which would deny root
1758 mutex_lock(&dev->mutex);
1761 if (!capable(CAP_SYS_MODULE) && dev->in_request_module) {
1762 DPRINTK("in request module\n");
1763 mutex_unlock(&dev->mutex);
1766 if (capable(CAP_SYS_MODULE) && dev->in_request_module)
1769 dev->in_request_module = 1;
1772 mutex_unlock(&dev->mutex);
1773 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1774 mutex_lock(&dev->mutex);
1777 dev->in_request_module = 0;
1779 if (!dev->attached && !capable(CAP_SYS_MODULE)) {
1780 DPRINTK("not attached and not CAP_SYS_MODULE\n");
1781 mutex_unlock(&dev->mutex);
1785 __module_get(THIS_MODULE);
1787 if (dev->attached) {
1788 if (!try_module_get(dev->driver->module)) {
1789 module_put(THIS_MODULE);
1790 mutex_unlock(&dev->mutex);
1795 if (dev->attached && dev->use_count == 0 && dev->open) {
1801 mutex_unlock(&dev->mutex);
1806 static int comedi_close(struct inode *inode, struct file *file)
1808 const unsigned minor = iminor(inode);
1809 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1810 comedi_device *dev = dev_file_info->device;
1811 comedi_subdevice *s = NULL;
1814 mutex_lock(&dev->mutex);
1816 if (dev->subdevices) {
1817 for (i = 0; i < dev->n_subdevices; i++) {
1818 s = dev->subdevices + i;
1820 if (s->busy == file) {
1823 if (s->lock == file) {
1828 if (dev->attached && dev->use_count == 1 && dev->close) {
1832 module_put(THIS_MODULE);
1833 if (dev->attached) {
1834 module_put(dev->driver->module);
1839 mutex_unlock(&dev->mutex);
1841 if (file->f_flags & FASYNC) {
1842 comedi_fasync(-1, file, 0);
1848 static int comedi_fasync(int fd, struct file *file, int on)
1850 const unsigned minor = iminor(file->f_dentry->d_inode);
1851 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(minor);
1852 comedi_device *dev = dev_file_info->device;
1854 return fasync_helper(fd, file, on, &dev->async_queue);
1857 const struct file_operations comedi_fops = {
1859 #ifdef HAVE_UNLOCKED_IOCTL
1860 unlocked_ioctl:comedi_unlocked_ioctl,
1864 #ifdef HAVE_COMPAT_IOCTL
1865 compat_ioctl:comedi_compat_ioctl,
1868 release:comedi_close,
1873 fasync:comedi_fasync,
1876 struct class *comedi_class = NULL;
1877 static struct cdev comedi_cdev;
1879 static void comedi_cleanup_legacy_minors(void)
1882 for (i = 0; i < comedi_num_legacy_minors; i++) {
1883 comedi_free_board_minor(i);
1887 static int __init comedi_init(void)
1892 printk("comedi: version " COMEDI_RELEASE
1893 " - http://www.comedi.org\n");
1895 if(comedi_num_legacy_minors < 0 || comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS)
1897 printk("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid "
1898 "values are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
1901 /* comedi is unusable if both comedi_autoconfig and comedi_num_legacy_minors are zero,
1902 so we might as well adjust the defaults in that case */
1903 if(comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
1905 comedi_num_legacy_minors = 16;
1908 memset(comedi_file_info_table, 0, sizeof(struct comedi_device_file_info*) * COMEDI_NUM_MINORS);
1910 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1911 COMEDI_NUM_MINORS, "comedi");
1914 cdev_init(&comedi_cdev, &comedi_fops);
1915 comedi_cdev.owner = THIS_MODULE;
1916 kobject_set_name(&comedi_cdev.kobj, "comedi");
1917 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
1918 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1922 comedi_class = class_create(THIS_MODULE, "comedi");
1923 if (IS_ERR(comedi_class)) {
1924 printk("comedi: failed to create class");
1925 cdev_del(&comedi_cdev);
1926 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
1928 return PTR_ERR(comedi_class);
1931 /* XXX requires /proc interface */
1934 // create devices files for legacy/manual use
1935 for (i = 0; i < comedi_num_legacy_minors; i++) {
1937 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)
1962 BUG_ON(comedi_file_info_table[i]);
1965 class_destroy(comedi_class);
1966 cdev_del(&comedi_cdev);
1967 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
1969 comedi_proc_cleanup();
1971 comedi_rt_cleanup();
1973 comedi_unregister_ioctl32();
1976 module_init(comedi_init);
1977 module_exit(comedi_cleanup);
1979 void comedi_error(const comedi_device * dev, const char *s)
1981 rt_printk("comedi%d: %s: %s\n", dev->minor, dev->driver->driver_name,
1985 void comedi_event(comedi_device * dev, comedi_subdevice * s)
1987 comedi_async *async = s->async;
1988 unsigned runflags = 0;
1989 unsigned runflags_mask = 0;
1991 //DPRINTK("comedi_event 0x%x\n",mask);
1993 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
1997 events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW))
1999 runflags_mask |= SRF_RUNNING;
2001 /* remember if an error event has occured, so an error
2002 * can be returned the next time the user does a read() */
2003 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2004 runflags_mask |= SRF_ERROR;
2005 runflags |= SRF_ERROR;
2007 if (runflags_mask) {
2008 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2009 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2012 if (async->cb_mask & s->async->events) {
2013 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2016 #ifdef CONFIG_COMEDI_RT
2018 comedi_rt_pend_wakeup(&async->wait_head);
2020 printk("BUG: comedi_event() code unreachable\n");
2023 wake_up_interruptible(&async->wait_head);
2024 if (s->subdev_flags & SDF_CMD_READ) {
2025 kill_fasync(&dev->async_queue, SIGIO,
2028 if (s->subdev_flags & SDF_CMD_WRITE) {
2029 kill_fasync(&dev->async_queue, SIGIO,
2035 async->cb_func(s->async->events, async->cb_arg);
2036 /* XXX bug here. If subdevice A is rt, and
2037 * subdevice B tries to callback to a normal
2038 * linux kernel function, it will be at the
2039 * wrong priority. Since this isn't very
2040 * common, I'm not going to worry about it. */
2043 s->async->events = 0;
2046 void comedi_set_subdevice_runflags(comedi_subdevice * s, unsigned mask,
2049 unsigned long flags;
2051 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2052 s->runflags &= ~mask;
2053 s->runflags |= (bits & mask);
2054 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2057 unsigned comedi_get_subdevice_runflags(comedi_subdevice * s)
2059 unsigned long flags;
2062 comedi_spin_lock_irqsave(&s->spin_lock, flags);
2063 runflags = s->runflags;
2064 comedi_spin_unlock_irqrestore(&s->spin_lock, flags);
2068 static int is_device_busy(comedi_device * dev)
2070 comedi_subdevice *s;
2076 for (i = 0; i < dev->n_subdevices; i++) {
2077 s = dev->subdevices + i;
2080 if (s->async && s->async->mmap_count)
2087 void comedi_device_init(comedi_device *dev)
2089 memset(dev, 0, sizeof(comedi_device));
2090 spin_lock_init(&dev->spinlock);
2091 mutex_init(&dev->mutex);
2095 void comedi_device_cleanup(comedi_device *dev)
2097 if(dev == NULL) return;
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;
2112 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2113 if(info == NULL) return -ENOMEM;
2114 info->device = kzalloc(sizeof(comedi_device), GFP_KERNEL);
2115 if(info->device == NULL)
2120 comedi_device_init(info->device);
2121 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2122 for(i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2124 if(comedi_file_info_table[i] == NULL)
2126 comedi_file_info_table[i] = info;
2130 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2131 if(i == COMEDI_NUM_BOARD_MINORS)
2133 comedi_device_cleanup(info->device);
2134 kfree(info->device);
2136 printk("comedi: error: ran out of minor numbers for board device files.\n");
2139 info->device->minor = i;
2140 csdev = COMEDI_DEVICE_CREATE(comedi_class, NULL,
2141 MKDEV(COMEDI_MAJOR, i), NULL, hardware_device, "comedi%i", i);
2142 if(!IS_ERR(csdev)) {
2143 info->device->class_dev = csdev;
2145 dev_set_drvdata(csdev, info);
2146 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2149 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_read_buffer_kb.attr.name);
2150 comedi_free_board_minor(i);
2153 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2156 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_read_buffer_kb.attr.name);
2157 comedi_free_board_minor(i);
2160 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2163 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_write_buffer_kb.attr.name);
2164 comedi_free_board_minor(i);
2167 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2170 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_write_buffer_kb.attr.name);
2171 comedi_free_board_minor(i);
2177 void comedi_free_board_minor(unsigned minor)
2179 unsigned long flags;
2180 struct comedi_device_file_info *info;
2182 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2183 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2184 info = comedi_file_info_table[minor];
2185 comedi_file_info_table[minor] = NULL;
2186 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2190 comedi_device *dev = info->device;
2195 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, dev->minor));
2197 comedi_device_cleanup(dev);
2204 int comedi_alloc_subdevice_minor(comedi_device *dev, comedi_subdevice *s)
2206 unsigned long flags;
2207 struct comedi_device_file_info *info;
2208 device_create_result_type *csdev;
2212 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2213 if(info == NULL) return -ENOMEM;
2215 info->read_subdevice = s;
2216 info->write_subdevice = s;
2217 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2218 for(i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i)
2220 if(comedi_file_info_table[i] == NULL)
2222 comedi_file_info_table[i] = info;
2226 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2227 if(i == COMEDI_NUM_MINORS)
2230 printk("comedi: error: ran out of minor numbers for board device files.\n");
2234 csdev = COMEDI_DEVICE_CREATE(comedi_class, dev->class_dev,
2235 MKDEV(COMEDI_MAJOR, i), NULL, NULL, "comedi%i_subd%i", dev->minor, (int)(s - dev->subdevices));
2238 s->class_dev = csdev;
2240 dev_set_drvdata(csdev, info);
2241 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2244 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_read_buffer_kb.attr.name);
2245 comedi_free_subdevice_minor(s);
2248 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2251 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_read_buffer_kb.attr.name);
2252 comedi_free_subdevice_minor(s);
2255 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2258 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_max_write_buffer_kb.attr.name);
2259 comedi_free_subdevice_minor(s);
2262 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2265 printk(KERN_ERR "comedi: failed to create sysfs attribute file \"%s\".\n", dev_attr_write_buffer_kb.attr.name);
2266 comedi_free_subdevice_minor(s);
2272 void comedi_free_subdevice_minor(comedi_subdevice *s)
2274 unsigned long flags;
2275 struct comedi_device_file_info *info;
2277 if(s == NULL) return;
2278 if(s->minor < 0) return;
2280 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2281 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2283 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2284 info = comedi_file_info_table[s->minor];
2285 comedi_file_info_table[s->minor] = NULL;
2286 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2290 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2291 s->class_dev = NULL;
2296 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2298 unsigned long flags;
2299 struct comedi_device_file_info *info;
2301 BUG_ON(minor >= COMEDI_NUM_MINORS);
2302 comedi_spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2303 info = comedi_file_info_table[minor];
2304 comedi_spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2308 static int resize_async_buffer(comedi_device *dev,
2309 comedi_subdevice *s, comedi_async *async, unsigned new_size)
2313 if (new_size > async->max_bufsize)
2317 DPRINTK("subdevice is busy, cannot resize buffer\n");
2320 if (async->mmap_count) {
2321 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2325 if (!async->prealloc_buf)
2328 /* make sure buffer is an integral number of pages
2330 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2332 retval = comedi_buf_alloc(dev, s, new_size);
2336 if (s->buf_change) {
2337 retval = s->buf_change(dev, s, new_size);
2342 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2343 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
2347 // sysfs attribute files
2349 static const unsigned bytes_per_kibi = 1024;
2351 ssize_t show_max_read_buffer_kb(struct device *dev,
2352 struct device_attribute *attr, char *buf)
2355 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2356 unsigned max_buffer_size_kb = 0;
2357 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2359 mutex_lock(&info->device->mutex);
2360 if(read_subdevice &&
2361 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2362 read_subdevice->async)
2364 max_buffer_size_kb = read_subdevice->async->max_bufsize / bytes_per_kibi;
2366 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2367 mutex_unlock(&info->device->mutex);
2372 ssize_t store_max_read_buffer_kb(struct device *dev, struct device_attribute *attr,
2373 const char *buf, size_t count)
2375 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2376 unsigned long new_max_size_kb;
2377 uint64_t new_max_size;
2378 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2380 if(strict_strtoul(buf, 10, &new_max_size_kb))
2384 if(new_max_size_kb != (uint32_t)new_max_size_kb) return -EINVAL;
2385 new_max_size = ((uint64_t)new_max_size_kb) * bytes_per_kibi;
2386 if(new_max_size != (uint32_t)new_max_size) return -EINVAL;
2388 mutex_lock(&info->device->mutex);
2389 if(read_subdevice == NULL ||
2390 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2391 read_subdevice->async == NULL)
2393 mutex_unlock(&info->device->mutex);
2396 read_subdevice->async->max_bufsize = new_max_size;
2397 mutex_unlock(&info->device->mutex);
2402 struct device_attribute dev_attr_max_read_buffer_kb =
2405 .name = "max_read_buffer_kb",
2406 .mode = S_IRUGO | S_IWUSR
2408 .show = &show_max_read_buffer_kb,
2409 .store = &store_max_read_buffer_kb
2412 ssize_t show_read_buffer_kb(struct device *dev,
2413 struct device_attribute *attr, char *buf)
2416 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2417 unsigned buffer_size_kb = 0;
2418 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2420 mutex_lock(&info->device->mutex);
2421 if(read_subdevice &&
2422 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2423 read_subdevice->async)
2425 buffer_size_kb = read_subdevice->async->prealloc_bufsz / bytes_per_kibi;
2427 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2428 mutex_unlock(&info->device->mutex);
2433 ssize_t store_read_buffer_kb(struct device *dev, struct device_attribute *attr,
2434 const char *buf, size_t count)
2436 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2437 unsigned long new_size_kb;
2440 comedi_subdevice * const read_subdevice = comedi_get_read_subdevice(info);
2442 if(strict_strtoul(buf, 10, &new_size_kb))
2446 if(new_size_kb != (uint32_t)new_size_kb) return -EINVAL;
2447 new_size = ((uint64_t)new_size_kb) * bytes_per_kibi;
2448 if(new_size != (uint32_t)new_size) return -EINVAL;
2450 mutex_lock(&info->device->mutex);
2451 if(read_subdevice == NULL ||
2452 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2453 read_subdevice->async == NULL)
2455 mutex_unlock(&info->device->mutex);
2458 retval = resize_async_buffer(info->device, read_subdevice,
2459 read_subdevice->async, new_size);
2460 mutex_unlock(&info->device->mutex);
2462 if(retval < 0) return retval;
2466 struct device_attribute dev_attr_read_buffer_kb =
2469 .name = "read_buffer_kb",
2470 .mode = S_IRUGO | S_IWUSR | S_IWGRP
2472 .show = &show_read_buffer_kb,
2473 .store = &store_read_buffer_kb
2476 ssize_t show_max_write_buffer_kb(struct device *dev,
2477 struct device_attribute *attr, char *buf)
2480 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2481 unsigned max_buffer_size_kb = 0;
2482 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2484 mutex_lock(&info->device->mutex);
2485 if(write_subdevice &&
2486 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2487 write_subdevice->async)
2489 max_buffer_size_kb = write_subdevice->async->max_bufsize / bytes_per_kibi;
2491 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2492 mutex_unlock(&info->device->mutex);
2497 ssize_t store_max_write_buffer_kb(struct device *dev, struct device_attribute *attr,
2498 const char *buf, size_t count)
2500 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2501 unsigned long new_max_size_kb;
2502 uint64_t new_max_size;
2503 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2505 if(strict_strtoul(buf, 10, &new_max_size_kb))
2509 if(new_max_size_kb != (uint32_t)new_max_size_kb) return -EINVAL;
2510 new_max_size = ((uint64_t)new_max_size_kb) * bytes_per_kibi;
2511 if(new_max_size != (uint32_t)new_max_size) return -EINVAL;
2513 mutex_lock(&info->device->mutex);
2514 if(write_subdevice == NULL ||
2515 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2516 write_subdevice->async == NULL)
2518 mutex_unlock(&info->device->mutex);
2521 write_subdevice->async->max_bufsize = new_max_size;
2522 mutex_unlock(&info->device->mutex);
2527 struct device_attribute dev_attr_max_write_buffer_kb =
2530 .name = "max_write_buffer_kb",
2531 .mode = S_IRUGO | S_IWUSR
2533 .show = &show_max_write_buffer_kb,
2534 .store = &store_max_write_buffer_kb
2537 ssize_t show_write_buffer_kb(struct device *dev,
2538 struct device_attribute *attr, char *buf)
2541 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2542 unsigned buffer_size_kb = 0;
2543 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2545 mutex_lock(&info->device->mutex);
2546 if(write_subdevice &&
2547 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2548 write_subdevice->async)
2550 buffer_size_kb = write_subdevice->async->prealloc_bufsz / bytes_per_kibi;
2552 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2553 mutex_unlock(&info->device->mutex);
2558 ssize_t store_write_buffer_kb(struct device *dev, struct device_attribute *attr,
2559 const char *buf, size_t count)
2561 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2562 unsigned long new_size_kb;
2565 comedi_subdevice * const write_subdevice = comedi_get_write_subdevice(info);
2567 if(strict_strtoul(buf, 10, &new_size_kb))
2571 if(new_size_kb != (uint32_t)new_size_kb) return -EINVAL;
2572 new_size = ((uint64_t)new_size_kb) * bytes_per_kibi;
2573 if(new_size != (uint32_t)new_size) return -EINVAL;
2575 mutex_lock(&info->device->mutex);
2576 if(write_subdevice == NULL ||
2577 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2578 write_subdevice->async == NULL)
2580 mutex_unlock(&info->device->mutex);
2583 retval = resize_async_buffer(info->device, write_subdevice,
2584 write_subdevice->async, new_size);
2585 mutex_unlock(&info->device->mutex);
2587 if(retval < 0) return retval;
2591 struct device_attribute dev_attr_write_buffer_kb =
2594 .name = "write_buffer_kb",
2595 .mode = S_IRUGO | S_IWUSR | S_IWGRP
2597 .show = &show_write_buffer_kb,
2598 .store = &store_write_buffer_kb