staging: comedi: fix oops for USB DAQ devices.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / comedi / comedi_fops.c
blobbfda750b42b41c612df2d2128411649c89f731c6
1 /*
2 comedi/comedi_fops.c
3 comedi kernel module
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.
24 #undef DEBUG
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>
37 #include <linux/mm.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>
44 #include <linux/fs.h>
45 #include "comedidev.h"
46 #include <linux/cdev.h>
47 #include <linux/stat.h>
49 #include <linux/io.h>
50 #include <linux/uaccess.h>
52 #include "internal.h"
54 MODULE_AUTHOR("http://www.comedi.org");
55 MODULE_DESCRIPTION("Comedi core module");
56 MODULE_LICENSE("GPL");
58 #ifdef CONFIG_COMEDI_DEBUG
59 int comedi_debug;
60 EXPORT_SYMBOL(comedi_debug);
61 module_param(comedi_debug, int, 0644);
62 #endif
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,
80 struct file *file);
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,
90 void *file);
91 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
92 void *file);
93 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
94 void *file);
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,
102 void *file);
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,
122 unsigned long arg)
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;
128 int rc;
130 if (dev_file_info == NULL || dev_file_info->device == NULL)
131 return -ENODEV;
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);
141 goto done;
144 if (!dev->attached) {
145 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
146 rc = -ENODEV;
147 goto done;
150 switch (cmd) {
151 case COMEDI_BUFCONFIG:
152 rc = do_bufconfig_ioctl(dev,
153 (struct comedi_bufconfig __user *)arg);
154 break;
155 case COMEDI_DEVINFO:
156 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
157 file);
158 break;
159 case COMEDI_SUBDINFO:
160 rc = do_subdinfo_ioctl(dev,
161 (struct comedi_subdinfo __user *)arg,
162 file);
163 break;
164 case COMEDI_CHANINFO:
165 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
166 break;
167 case COMEDI_RANGEINFO:
168 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
169 break;
170 case COMEDI_BUFINFO:
171 rc = do_bufinfo_ioctl(dev,
172 (struct comedi_bufinfo __user *)arg,
173 file);
174 break;
175 case COMEDI_LOCK:
176 rc = do_lock_ioctl(dev, arg, file);
177 break;
178 case COMEDI_UNLOCK:
179 rc = do_unlock_ioctl(dev, arg, file);
180 break;
181 case COMEDI_CANCEL:
182 rc = do_cancel_ioctl(dev, arg, file);
183 break;
184 case COMEDI_CMD:
185 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
186 break;
187 case COMEDI_CMDTEST:
188 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
189 file);
190 break;
191 case COMEDI_INSNLIST:
192 rc = do_insnlist_ioctl(dev,
193 (struct comedi_insnlist __user *)arg,
194 file);
195 break;
196 case COMEDI_INSN:
197 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
198 file);
199 break;
200 case COMEDI_POLL:
201 rc = do_poll_ioctl(dev, arg, file);
202 break;
203 default:
204 rc = -ENOTTY;
205 break;
208 done:
209 mutex_unlock(&dev->mutex);
210 return rc;
214 COMEDI_DEVCONFIG
215 device config ioctl
217 arg:
218 pointer to devconfig structure
220 reads:
221 devconfig structure at arg
223 writes:
224 none
226 static int do_devconfig_ioctl(struct comedi_device *dev,
227 struct comedi_devconfig __user *arg)
229 struct comedi_devconfig it;
230 int ret;
231 unsigned char *aux_data = NULL;
232 int aux_len;
234 if (!capable(CAP_SYS_ADMIN))
235 return -EPERM;
237 if (arg == NULL) {
238 if (is_device_busy(dev))
239 return -EBUSY;
240 if (dev->attached) {
241 struct module *driver_module = dev->driver->module;
242 comedi_device_detach(dev);
243 module_put(driver_module);
245 return 0;
248 if (copy_from_user(&it, arg, sizeof(struct comedi_devconfig)))
249 return -EFAULT;
251 it.board_name[COMEDI_NAMELEN - 1] = 0;
253 if (comedi_aux_data(it.options, 0) &&
254 it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
255 int bit_shift;
256 aux_len = it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
257 if (aux_len < 0)
258 return -EFAULT;
260 aux_data = vmalloc(aux_len);
261 if (!aux_data)
262 return -ENOMEM;
264 if (copy_from_user(aux_data,
265 comedi_aux_data(it.options, 0), aux_len)) {
266 vfree(aux_data);
267 return -EFAULT;
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;
275 } else
276 it.options[COMEDI_DEVCONF_AUX_DATA_HI] = 0;
279 ret = comedi_device_attach(dev, &it);
280 if (ret == 0) {
281 if (!try_module_get(dev->driver->module)) {
282 comedi_device_detach(dev);
283 return -ENOSYS;
287 if (aux_data)
288 vfree(aux_data);
290 return ret;
294 COMEDI_BUFCONFIG
295 buffer configuration ioctl
297 arg:
298 pointer to bufconfig structure
300 reads:
301 bufconfig at arg
303 writes:
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;
313 int retval = 0;
315 if (copy_from_user(&bc, arg, sizeof(struct comedi_bufconfig)))
316 return -EFAULT;
318 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
319 return -EINVAL;
321 s = dev->subdevices + bc.subdevice;
322 async = s->async;
324 if (!async) {
325 DPRINTK("subdevice does not have async capability\n");
326 bc.size = 0;
327 bc.maximum_size = 0;
328 goto copyback;
331 if (bc.maximum_size) {
332 if (!capable(CAP_SYS_ADMIN))
333 return -EPERM;
335 async->max_bufsize = bc.maximum_size;
338 if (bc.size) {
339 retval = resize_async_buffer(dev, s, async, bc.size);
340 if (retval < 0)
341 return retval;
344 bc.size = async->prealloc_bufsz;
345 bc.maximum_size = async->max_bufsize;
347 copyback:
348 if (copy_to_user(arg, &bc, sizeof(struct comedi_bufconfig)))
349 return -EFAULT;
351 return 0;
355 COMEDI_DEVINFO
356 device info ioctl
358 arg:
359 pointer to devinfo structure
361 reads:
362 none
364 writes:
365 devinfo structure
368 static int do_devinfo_ioctl(struct comedi_device *dev,
369 struct comedi_devinfo __user *arg,
370 struct file *file)
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);
389 if (read_subdev)
390 devinfo.read_subdevice = read_subdev - dev->subdevices;
391 else
392 devinfo.read_subdevice = -1;
394 if (write_subdev)
395 devinfo.write_subdevice = write_subdev - dev->subdevices;
396 else
397 devinfo.write_subdevice = -1;
399 if (copy_to_user(arg, &devinfo, sizeof(struct comedi_devinfo)))
400 return -EFAULT;
402 return 0;
406 COMEDI_SUBDINFO
407 subdevice info ioctl
409 arg:
410 pointer to array of subdevice info structures
412 reads:
413 none
415 writes:
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)
422 int ret, i;
423 struct comedi_subdinfo *tmp, *us;
424 struct comedi_subdevice *s;
426 tmp =
427 kcalloc(dev->n_subdevices, sizeof(struct comedi_subdinfo),
428 GFP_KERNEL);
429 if (!tmp)
430 return -ENOMEM;
432 /* fill subdinfo structs */
433 for (i = 0; i < dev->n_subdevices; i++) {
434 s = dev->subdevices + i;
435 us = tmp + i;
437 us->type = s->type;
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) {
447 us->range_type =
448 (i << 24) | (0 << 16) | (s->range_table->length);
449 } else {
450 us->range_type = 0; /* XXX */
452 us->flags = s->flags;
454 if (s->busy)
455 us->subd_flags |= SDF_BUSY;
456 if (s->busy == file)
457 us->subd_flags |= SDF_BUSY_OWNER;
458 if (s->lock)
459 us->subd_flags |= SDF_LOCKED;
460 if (s->lock == file)
461 us->subd_flags |= SDF_LOCK_OWNER;
462 if (!s->maxdata && s->maxdata_list)
463 us->subd_flags |= SDF_MAXDATA;
464 if (s->flaglist)
465 us->subd_flags |= SDF_FLAGS;
466 if (s->range_table_list)
467 us->subd_flags |= SDF_RANGETYPE;
468 if (s->do_cmd)
469 us->subd_flags |= SDF_CMD;
471 if (s->insn_bits != &insn_inval)
472 us->insn_bits_support = COMEDI_SUPPORTED;
473 else
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));
482 kfree(tmp);
484 return ret ? -EFAULT : 0;
488 COMEDI_CHANINFO
489 subdevice info ioctl
491 arg:
492 pointer to chaninfo structure
494 reads:
495 chaninfo structure at arg
497 writes:
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)))
508 return -EFAULT;
510 if (it.subdev >= dev->n_subdevices)
511 return -EINVAL;
512 s = dev->subdevices + it.subdev;
514 if (it.maxdata_list) {
515 if (s->maxdata || !s->maxdata_list)
516 return -EINVAL;
517 if (copy_to_user(it.maxdata_list, s->maxdata_list,
518 s->n_chan * sizeof(unsigned int)))
519 return -EFAULT;
522 if (it.flaglist) {
523 if (!s->flaglist)
524 return -EINVAL;
525 if (copy_to_user(it.flaglist, s->flaglist,
526 s->n_chan * sizeof(unsigned int)))
527 return -EFAULT;
530 if (it.rangelist) {
531 int i;
533 if (!s->range_table_list)
534 return -EINVAL;
535 for (i = 0; i < s->n_chan; i++) {
536 int x;
538 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
539 (s->range_table_list[i]->length);
540 if (put_user(x, it.rangelist + i))
541 return -EFAULT;
543 #if 0
544 if (copy_to_user(it.rangelist, s->range_type_list,
545 s->n_chan * sizeof(unsigned int)))
546 return -EFAULT;
547 #endif
550 return 0;
554 COMEDI_BUFINFO
555 buffer information ioctl
557 arg:
558 pointer to bufinfo structure
560 reads:
561 bufinfo at arg
563 writes:
564 modified bufinfo at arg
567 static int do_bufinfo_ioctl(struct comedi_device *dev,
568 struct comedi_bufinfo __user *arg, void *file)
570 struct comedi_bufinfo bi;
571 struct comedi_subdevice *s;
572 struct comedi_async *async;
574 if (copy_from_user(&bi, arg, sizeof(struct comedi_bufinfo)))
575 return -EFAULT;
577 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
578 return -EINVAL;
580 s = dev->subdevices + bi.subdevice;
582 if (s->lock && s->lock != file)
583 return -EACCES;
585 async = s->async;
587 if (!async) {
588 DPRINTK("subdevice does not have async capability\n");
589 bi.buf_write_ptr = 0;
590 bi.buf_read_ptr = 0;
591 bi.buf_write_count = 0;
592 bi.buf_read_count = 0;
593 bi.bytes_read = 0;
594 bi.bytes_written = 0;
595 goto copyback;
597 if (!s->busy) {
598 bi.bytes_read = 0;
599 bi.bytes_written = 0;
600 goto copyback_position;
602 if (s->busy != file)
603 return -EACCES;
605 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
606 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
607 comedi_buf_read_free(async, bi.bytes_read);
609 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR |
610 SRF_RUNNING))
611 && async->buf_write_count == async->buf_read_count) {
612 do_become_nonbusy(dev, s);
616 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
617 bi.bytes_written =
618 comedi_buf_write_alloc(async, bi.bytes_written);
619 comedi_buf_write_free(async, bi.bytes_written);
622 copyback_position:
623 bi.buf_write_count = async->buf_write_count;
624 bi.buf_write_ptr = async->buf_write_ptr;
625 bi.buf_read_count = async->buf_read_count;
626 bi.buf_read_ptr = async->buf_read_ptr;
628 copyback:
629 if (copy_to_user(arg, &bi, sizeof(struct comedi_bufinfo)))
630 return -EFAULT;
632 return 0;
635 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
636 unsigned int *data, void *file);
638 * COMEDI_INSNLIST
639 * synchronous instructions
641 * arg:
642 * pointer to sync cmd structure
644 * reads:
645 * sync cmd struct at arg
646 * instruction list
647 * data (for writes)
649 * writes:
650 * data (for reads)
652 /* arbitrary limits */
653 #define MAX_SAMPLES 256
654 static int do_insnlist_ioctl(struct comedi_device *dev,
655 struct comedi_insnlist __user *arg, void *file)
657 struct comedi_insnlist insnlist;
658 struct comedi_insn *insns = NULL;
659 unsigned int *data = NULL;
660 int i = 0;
661 int ret = 0;
663 if (copy_from_user(&insnlist, arg, sizeof(struct comedi_insnlist)))
664 return -EFAULT;
666 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
667 if (!data) {
668 DPRINTK("kmalloc failed\n");
669 ret = -ENOMEM;
670 goto error;
673 insns =
674 kmalloc(sizeof(struct comedi_insn) * insnlist.n_insns, GFP_KERNEL);
675 if (!insns) {
676 DPRINTK("kmalloc failed\n");
677 ret = -ENOMEM;
678 goto error;
681 if (copy_from_user(insns, insnlist.insns,
682 sizeof(struct comedi_insn) * insnlist.n_insns)) {
683 DPRINTK("copy_from_user failed\n");
684 ret = -EFAULT;
685 goto error;
688 for (i = 0; i < insnlist.n_insns; i++) {
689 if (insns[i].n > MAX_SAMPLES) {
690 DPRINTK("number of samples too large\n");
691 ret = -EINVAL;
692 goto error;
694 if (insns[i].insn & INSN_MASK_WRITE) {
695 if (copy_from_user(data, insns[i].data,
696 insns[i].n * sizeof(unsigned int))) {
697 DPRINTK("copy_from_user failed\n");
698 ret = -EFAULT;
699 goto error;
702 ret = parse_insn(dev, insns + i, data, file);
703 if (ret < 0)
704 goto error;
705 if (insns[i].insn & INSN_MASK_READ) {
706 if (copy_to_user(insns[i].data, data,
707 insns[i].n * sizeof(unsigned int))) {
708 DPRINTK("copy_to_user failed\n");
709 ret = -EFAULT;
710 goto error;
713 if (need_resched())
714 schedule();
717 error:
718 kfree(insns);
719 kfree(data);
721 if (ret < 0)
722 return ret;
723 return i;
726 static int check_insn_config_length(struct comedi_insn *insn,
727 unsigned int *data)
729 if (insn->n < 1)
730 return -EINVAL;
732 switch (data[0]) {
733 case INSN_CONFIG_DIO_OUTPUT:
734 case INSN_CONFIG_DIO_INPUT:
735 case INSN_CONFIG_DISARM:
736 case INSN_CONFIG_RESET:
737 if (insn->n == 1)
738 return 0;
739 break;
740 case INSN_CONFIG_ARM:
741 case INSN_CONFIG_DIO_QUERY:
742 case INSN_CONFIG_BLOCK_SIZE:
743 case INSN_CONFIG_FILTER:
744 case INSN_CONFIG_SERIAL_CLOCK:
745 case INSN_CONFIG_BIDIRECTIONAL_DATA:
746 case INSN_CONFIG_ALT_SOURCE:
747 case INSN_CONFIG_SET_COUNTER_MODE:
748 case INSN_CONFIG_8254_READ_STATUS:
749 case INSN_CONFIG_SET_ROUTING:
750 case INSN_CONFIG_GET_ROUTING:
751 case INSN_CONFIG_GET_PWM_STATUS:
752 case INSN_CONFIG_PWM_SET_PERIOD:
753 case INSN_CONFIG_PWM_GET_PERIOD:
754 if (insn->n == 2)
755 return 0;
756 break;
757 case INSN_CONFIG_SET_GATE_SRC:
758 case INSN_CONFIG_GET_GATE_SRC:
759 case INSN_CONFIG_SET_CLOCK_SRC:
760 case INSN_CONFIG_GET_CLOCK_SRC:
761 case INSN_CONFIG_SET_OTHER_SRC:
762 case INSN_CONFIG_GET_COUNTER_STATUS:
763 case INSN_CONFIG_PWM_SET_H_BRIDGE:
764 case INSN_CONFIG_PWM_GET_H_BRIDGE:
765 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
766 if (insn->n == 3)
767 return 0;
768 break;
769 case INSN_CONFIG_PWM_OUTPUT:
770 case INSN_CONFIG_ANALOG_TRIG:
771 if (insn->n == 5)
772 return 0;
773 break;
774 /* by default we allow the insn since we don't have checks for
775 * all possible cases yet */
776 default:
777 printk(KERN_WARNING
778 "comedi: no check for data length of config insn id "
779 "%i is implemented.\n"
780 " Add a check to %s in %s.\n"
781 " Assuming n=%i is correct.\n", data[0], __func__,
782 __FILE__, insn->n);
783 return 0;
784 break;
786 return -EINVAL;
789 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
790 unsigned int *data, void *file)
792 struct comedi_subdevice *s;
793 int ret = 0;
794 int i;
796 if (insn->insn & INSN_MASK_SPECIAL) {
797 /* a non-subdevice instruction */
799 switch (insn->insn) {
800 case INSN_GTOD:
802 struct timeval tv;
804 if (insn->n != 2) {
805 ret = -EINVAL;
806 break;
809 do_gettimeofday(&tv);
810 data[0] = tv.tv_sec;
811 data[1] = tv.tv_usec;
812 ret = 2;
814 break;
816 case INSN_WAIT:
817 if (insn->n != 1 || data[0] >= 100000) {
818 ret = -EINVAL;
819 break;
821 udelay(data[0] / 1000);
822 ret = 1;
823 break;
824 case INSN_INTTRIG:
825 if (insn->n != 1) {
826 ret = -EINVAL;
827 break;
829 if (insn->subdev >= dev->n_subdevices) {
830 DPRINTK("%d not usable subdevice\n",
831 insn->subdev);
832 ret = -EINVAL;
833 break;
835 s = dev->subdevices + insn->subdev;
836 if (!s->async) {
837 DPRINTK("no async\n");
838 ret = -EINVAL;
839 break;
841 if (!s->async->inttrig) {
842 DPRINTK("no inttrig\n");
843 ret = -EAGAIN;
844 break;
846 ret = s->async->inttrig(dev, s, insn->data[0]);
847 if (ret >= 0)
848 ret = 1;
849 break;
850 default:
851 DPRINTK("invalid insn\n");
852 ret = -EINVAL;
853 break;
855 } else {
856 /* a subdevice instruction */
857 unsigned int maxdata;
859 if (insn->subdev >= dev->n_subdevices) {
860 DPRINTK("subdevice %d out of range\n", insn->subdev);
861 ret = -EINVAL;
862 goto out;
864 s = dev->subdevices + insn->subdev;
866 if (s->type == COMEDI_SUBD_UNUSED) {
867 DPRINTK("%d not usable subdevice\n", insn->subdev);
868 ret = -EIO;
869 goto out;
872 /* are we locked? (ioctl lock) */
873 if (s->lock && s->lock != file) {
874 DPRINTK("device locked\n");
875 ret = -EACCES;
876 goto out;
879 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
880 if (ret < 0) {
881 ret = -EINVAL;
882 DPRINTK("bad chanspec\n");
883 goto out;
886 if (s->busy) {
887 ret = -EBUSY;
888 goto out;
890 /* This looks arbitrary. It is. */
891 s->busy = &parse_insn;
892 switch (insn->insn) {
893 case INSN_READ:
894 ret = s->insn_read(dev, s, insn, data);
895 break;
896 case INSN_WRITE:
897 maxdata = s->maxdata_list
898 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
899 : s->maxdata;
900 for (i = 0; i < insn->n; ++i) {
901 if (data[i] > maxdata) {
902 ret = -EINVAL;
903 DPRINTK("bad data value(s)\n");
904 break;
907 if (ret == 0)
908 ret = s->insn_write(dev, s, insn, data);
909 break;
910 case INSN_BITS:
911 if (insn->n != 2) {
912 ret = -EINVAL;
913 } else {
914 /* Most drivers ignore the base channel in
915 * insn->chanspec. Fix this here if
916 * the subdevice has <= 32 channels. */
917 unsigned int shift;
918 unsigned int orig_mask;
920 orig_mask = data[0];
921 if (s->n_chan <= 32) {
922 shift = CR_CHAN(insn->chanspec);
923 if (shift > 0) {
924 insn->chanspec = 0;
925 data[0] <<= shift;
926 data[1] <<= shift;
928 } else
929 shift = 0;
930 ret = s->insn_bits(dev, s, insn, data);
931 data[0] = orig_mask;
932 if (shift > 0)
933 data[1] >>= shift;
935 break;
936 case INSN_CONFIG:
937 ret = check_insn_config_length(insn, data);
938 if (ret)
939 break;
940 ret = s->insn_config(dev, s, insn, data);
941 break;
942 default:
943 ret = -EINVAL;
944 break;
947 s->busy = NULL;
950 out:
951 return ret;
955 * COMEDI_INSN
956 * synchronous instructions
958 * arg:
959 * pointer to insn
961 * reads:
962 * struct comedi_insn struct at arg
963 * data (for writes)
965 * writes:
966 * data (for reads)
968 static int do_insn_ioctl(struct comedi_device *dev,
969 struct comedi_insn __user *arg, void *file)
971 struct comedi_insn insn;
972 unsigned int *data = NULL;
973 int ret = 0;
975 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
976 if (!data) {
977 ret = -ENOMEM;
978 goto error;
981 if (copy_from_user(&insn, arg, sizeof(struct comedi_insn))) {
982 ret = -EFAULT;
983 goto error;
986 /* This is where the behavior of insn and insnlist deviate. */
987 if (insn.n > MAX_SAMPLES)
988 insn.n = MAX_SAMPLES;
989 if (insn.insn & INSN_MASK_WRITE) {
990 if (copy_from_user(data,
991 insn.data,
992 insn.n * sizeof(unsigned int))) {
993 ret = -EFAULT;
994 goto error;
997 ret = parse_insn(dev, &insn, data, file);
998 if (ret < 0)
999 goto error;
1000 if (insn.insn & INSN_MASK_READ) {
1001 if (copy_to_user(insn.data,
1002 data,
1003 insn.n * sizeof(unsigned int))) {
1004 ret = -EFAULT;
1005 goto error;
1008 ret = insn.n;
1010 error:
1011 kfree(data);
1013 return ret;
1016 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
1017 unsigned mask, unsigned bits)
1019 unsigned long flags;
1021 spin_lock_irqsave(&s->spin_lock, flags);
1022 s->runflags &= ~mask;
1023 s->runflags |= (bits & mask);
1024 spin_unlock_irqrestore(&s->spin_lock, flags);
1027 static int do_cmd_ioctl(struct comedi_device *dev,
1028 struct comedi_cmd __user *cmd, void *file)
1030 struct comedi_cmd user_cmd;
1031 struct comedi_subdevice *s;
1032 struct comedi_async *async;
1033 int ret = 0;
1034 unsigned int __user *chanlist_saver = NULL;
1036 if (copy_from_user(&user_cmd, cmd, sizeof(struct comedi_cmd))) {
1037 DPRINTK("bad cmd address\n");
1038 return -EFAULT;
1040 /* save user's chanlist pointer so it can be restored later */
1041 chanlist_saver = user_cmd.chanlist;
1043 if (user_cmd.subdev >= dev->n_subdevices) {
1044 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1045 return -ENODEV;
1048 s = dev->subdevices + user_cmd.subdev;
1049 async = s->async;
1051 if (s->type == COMEDI_SUBD_UNUSED) {
1052 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1053 return -EIO;
1056 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1057 DPRINTK("subdevice %i does not support commands\n",
1058 user_cmd.subdev);
1059 return -EIO;
1062 /* are we locked? (ioctl lock) */
1063 if (s->lock && s->lock != file) {
1064 DPRINTK("subdevice locked\n");
1065 return -EACCES;
1068 /* are we busy? */
1069 if (s->busy) {
1070 DPRINTK("subdevice busy\n");
1071 return -EBUSY;
1073 s->busy = file;
1075 /* make sure channel/gain list isn't too long */
1076 if (user_cmd.chanlist_len > s->len_chanlist) {
1077 DPRINTK("channel/gain list too long %u > %d\n",
1078 user_cmd.chanlist_len, s->len_chanlist);
1079 ret = -EINVAL;
1080 goto cleanup;
1083 /* make sure channel/gain list isn't too short */
1084 if (user_cmd.chanlist_len < 1) {
1085 DPRINTK("channel/gain list too short %u < 1\n",
1086 user_cmd.chanlist_len);
1087 ret = -EINVAL;
1088 goto cleanup;
1091 kfree(async->cmd.chanlist);
1092 async->cmd = user_cmd;
1093 async->cmd.data = NULL;
1094 /* load channel/gain list */
1095 async->cmd.chanlist =
1096 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1097 if (!async->cmd.chanlist) {
1098 DPRINTK("allocation failed\n");
1099 ret = -ENOMEM;
1100 goto cleanup;
1103 if (copy_from_user(async->cmd.chanlist, user_cmd.chanlist,
1104 async->cmd.chanlist_len * sizeof(int))) {
1105 DPRINTK("fault reading chanlist\n");
1106 ret = -EFAULT;
1107 goto cleanup;
1110 /* make sure each element in channel/gain list is valid */
1111 ret = comedi_check_chanlist(s,
1112 async->cmd.chanlist_len,
1113 async->cmd.chanlist);
1114 if (ret < 0) {
1115 DPRINTK("bad chanlist\n");
1116 goto cleanup;
1119 ret = s->do_cmdtest(dev, s, &async->cmd);
1121 if (async->cmd.flags & TRIG_BOGUS || ret) {
1122 DPRINTK("test returned %d\n", ret);
1123 user_cmd = async->cmd;
1124 /* restore chanlist pointer before copying back */
1125 user_cmd.chanlist = chanlist_saver;
1126 user_cmd.data = NULL;
1127 if (copy_to_user(cmd, &user_cmd, sizeof(struct comedi_cmd))) {
1128 DPRINTK("fault writing cmd\n");
1129 ret = -EFAULT;
1130 goto cleanup;
1132 ret = -EAGAIN;
1133 goto cleanup;
1136 if (!async->prealloc_bufsz) {
1137 ret = -ENOMEM;
1138 DPRINTK("no buffer (?)\n");
1139 goto cleanup;
1142 comedi_reset_async_buf(async);
1144 async->cb_mask =
1145 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1146 COMEDI_CB_OVERFLOW;
1147 if (async->cmd.flags & TRIG_WAKE_EOS)
1148 async->cb_mask |= COMEDI_CB_EOS;
1150 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1152 ret = s->do_cmd(dev, s);
1153 if (ret == 0)
1154 return 0;
1156 cleanup:
1157 do_become_nonbusy(dev, s);
1159 return ret;
1163 COMEDI_CMDTEST
1164 command testing ioctl
1166 arg:
1167 pointer to cmd structure
1169 reads:
1170 cmd structure at arg
1171 channel/range list
1173 writes:
1174 modified cmd structure at arg
1177 static int do_cmdtest_ioctl(struct comedi_device *dev,
1178 struct comedi_cmd __user *arg, void *file)
1180 struct comedi_cmd user_cmd;
1181 struct comedi_subdevice *s;
1182 int ret = 0;
1183 unsigned int *chanlist = NULL;
1184 unsigned int __user *chanlist_saver = NULL;
1186 if (copy_from_user(&user_cmd, arg, sizeof(struct comedi_cmd))) {
1187 DPRINTK("bad cmd address\n");
1188 return -EFAULT;
1190 /* save user's chanlist pointer so it can be restored later */
1191 chanlist_saver = user_cmd.chanlist;
1193 if (user_cmd.subdev >= dev->n_subdevices) {
1194 DPRINTK("%d no such subdevice\n", user_cmd.subdev);
1195 return -ENODEV;
1198 s = dev->subdevices + user_cmd.subdev;
1199 if (s->type == COMEDI_SUBD_UNUSED) {
1200 DPRINTK("%d not valid subdevice\n", user_cmd.subdev);
1201 return -EIO;
1204 if (!s->do_cmd || !s->do_cmdtest) {
1205 DPRINTK("subdevice %i does not support commands\n",
1206 user_cmd.subdev);
1207 return -EIO;
1210 /* make sure channel/gain list isn't too long */
1211 if (user_cmd.chanlist_len > s->len_chanlist) {
1212 DPRINTK("channel/gain list too long %d > %d\n",
1213 user_cmd.chanlist_len, s->len_chanlist);
1214 ret = -EINVAL;
1215 goto cleanup;
1218 /* load channel/gain list */
1219 if (user_cmd.chanlist) {
1220 chanlist =
1221 kmalloc(user_cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1222 if (!chanlist) {
1223 DPRINTK("allocation failed\n");
1224 ret = -ENOMEM;
1225 goto cleanup;
1228 if (copy_from_user(chanlist, user_cmd.chanlist,
1229 user_cmd.chanlist_len * sizeof(int))) {
1230 DPRINTK("fault reading chanlist\n");
1231 ret = -EFAULT;
1232 goto cleanup;
1235 /* make sure each element in channel/gain list is valid */
1236 ret = comedi_check_chanlist(s, user_cmd.chanlist_len, chanlist);
1237 if (ret < 0) {
1238 DPRINTK("bad chanlist\n");
1239 goto cleanup;
1242 user_cmd.chanlist = chanlist;
1245 ret = s->do_cmdtest(dev, s, &user_cmd);
1247 /* restore chanlist pointer before copying back */
1248 user_cmd.chanlist = chanlist_saver;
1250 if (copy_to_user(arg, &user_cmd, sizeof(struct comedi_cmd))) {
1251 DPRINTK("bad cmd address\n");
1252 ret = -EFAULT;
1253 goto cleanup;
1255 cleanup:
1256 kfree(chanlist);
1258 return ret;
1262 COMEDI_LOCK
1263 lock subdevice
1265 arg:
1266 subdevice number
1268 reads:
1269 none
1271 writes:
1272 none
1276 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1277 void *file)
1279 int ret = 0;
1280 unsigned long flags;
1281 struct comedi_subdevice *s;
1283 if (arg >= dev->n_subdevices)
1284 return -EINVAL;
1285 s = dev->subdevices + arg;
1287 spin_lock_irqsave(&s->spin_lock, flags);
1288 if (s->busy || s->lock)
1289 ret = -EBUSY;
1290 else
1291 s->lock = file;
1292 spin_unlock_irqrestore(&s->spin_lock, flags);
1294 #if 0
1295 if (ret < 0)
1296 return ret;
1298 if (s->lock_f)
1299 ret = s->lock_f(dev, s);
1300 #endif
1302 return ret;
1306 COMEDI_UNLOCK
1307 unlock subdevice
1309 arg:
1310 subdevice number
1312 reads:
1313 none
1315 writes:
1316 none
1318 This function isn't protected by the semaphore, since
1319 we already own the lock.
1321 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1322 void *file)
1324 struct comedi_subdevice *s;
1326 if (arg >= dev->n_subdevices)
1327 return -EINVAL;
1328 s = dev->subdevices + arg;
1330 if (s->busy)
1331 return -EBUSY;
1333 if (s->lock && s->lock != file)
1334 return -EACCES;
1336 if (s->lock == file) {
1337 #if 0
1338 if (s->unlock)
1339 s->unlock(dev, s);
1340 #endif
1342 s->lock = NULL;
1345 return 0;
1349 COMEDI_CANCEL
1350 cancel acquisition ioctl
1352 arg:
1353 subdevice number
1355 reads:
1356 nothing
1358 writes:
1359 nothing
1362 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1363 void *file)
1365 struct comedi_subdevice *s;
1367 if (arg >= dev->n_subdevices)
1368 return -EINVAL;
1369 s = dev->subdevices + arg;
1370 if (s->async == NULL)
1371 return -EINVAL;
1373 if (s->lock && s->lock != file)
1374 return -EACCES;
1376 if (!s->busy)
1377 return 0;
1379 if (s->busy != file)
1380 return -EBUSY;
1382 return do_cancel(dev, s);
1386 COMEDI_POLL ioctl
1387 instructs driver to synchronize buffers
1389 arg:
1390 subdevice number
1392 reads:
1393 nothing
1395 writes:
1396 nothing
1399 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1400 void *file)
1402 struct comedi_subdevice *s;
1404 if (arg >= dev->n_subdevices)
1405 return -EINVAL;
1406 s = dev->subdevices + arg;
1408 if (s->lock && s->lock != file)
1409 return -EACCES;
1411 if (!s->busy)
1412 return 0;
1414 if (s->busy != file)
1415 return -EBUSY;
1417 if (s->poll)
1418 return s->poll(dev, s);
1420 return -EINVAL;
1423 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1425 int ret = 0;
1427 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) && s->cancel)
1428 ret = s->cancel(dev, s);
1430 do_become_nonbusy(dev, s);
1432 return ret;
1435 static void comedi_unmap(struct vm_area_struct *area)
1437 struct comedi_async *async;
1438 struct comedi_device *dev;
1440 async = area->vm_private_data;
1441 dev = async->subdevice->device;
1443 mutex_lock(&dev->mutex);
1444 async->mmap_count--;
1445 mutex_unlock(&dev->mutex);
1448 static struct vm_operations_struct comedi_vm_ops = {
1449 .close = comedi_unmap,
1452 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1454 const unsigned minor = iminor(file->f_dentry->d_inode);
1455 struct comedi_async *async = NULL;
1456 unsigned long start = vma->vm_start;
1457 unsigned long size;
1458 int n_pages;
1459 int i;
1460 int retval;
1461 struct comedi_subdevice *s;
1462 struct comedi_device_file_info *dev_file_info;
1463 struct comedi_device *dev;
1465 dev_file_info = comedi_get_device_file_info(minor);
1466 if (dev_file_info == NULL)
1467 return -ENODEV;
1468 dev = dev_file_info->device;
1469 if (dev == NULL)
1470 return -ENODEV;
1472 mutex_lock(&dev->mutex);
1473 if (!dev->attached) {
1474 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1475 retval = -ENODEV;
1476 goto done;
1478 if (vma->vm_flags & VM_WRITE)
1479 s = comedi_get_write_subdevice(dev_file_info);
1480 else
1481 s = comedi_get_read_subdevice(dev_file_info);
1483 if (s == NULL) {
1484 retval = -EINVAL;
1485 goto done;
1487 async = s->async;
1488 if (async == NULL) {
1489 retval = -EINVAL;
1490 goto done;
1493 if (vma->vm_pgoff != 0) {
1494 DPRINTK("comedi: mmap() offset must be 0.\n");
1495 retval = -EINVAL;
1496 goto done;
1499 size = vma->vm_end - vma->vm_start;
1500 if (size > async->prealloc_bufsz) {
1501 retval = -EFAULT;
1502 goto done;
1504 if (size & (~PAGE_MASK)) {
1505 retval = -EFAULT;
1506 goto done;
1509 n_pages = size >> PAGE_SHIFT;
1510 for (i = 0; i < n_pages; ++i) {
1511 if (remap_pfn_range(vma, start,
1512 page_to_pfn(virt_to_page
1513 (async->buf_page_list
1514 [i].virt_addr)), PAGE_SIZE,
1515 PAGE_SHARED)) {
1516 retval = -EAGAIN;
1517 goto done;
1519 start += PAGE_SIZE;
1522 vma->vm_ops = &comedi_vm_ops;
1523 vma->vm_private_data = async;
1525 async->mmap_count++;
1527 retval = 0;
1528 done:
1529 mutex_unlock(&dev->mutex);
1530 return retval;
1533 static unsigned int comedi_poll(struct file *file, poll_table * wait)
1535 unsigned int mask = 0;
1536 const unsigned minor = iminor(file->f_dentry->d_inode);
1537 struct comedi_subdevice *read_subdev;
1538 struct comedi_subdevice *write_subdev;
1539 struct comedi_device_file_info *dev_file_info;
1540 struct comedi_device *dev;
1541 dev_file_info = comedi_get_device_file_info(minor);
1543 if (dev_file_info == NULL)
1544 return -ENODEV;
1545 dev = dev_file_info->device;
1546 if (dev == NULL)
1547 return -ENODEV;
1549 mutex_lock(&dev->mutex);
1550 if (!dev->attached) {
1551 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1552 mutex_unlock(&dev->mutex);
1553 return 0;
1556 mask = 0;
1557 read_subdev = comedi_get_read_subdevice(dev_file_info);
1558 if (read_subdev) {
1559 poll_wait(file, &read_subdev->async->wait_head, wait);
1560 if (!read_subdev->busy
1561 || comedi_buf_read_n_available(read_subdev->async) > 0
1562 || !(comedi_get_subdevice_runflags(read_subdev) &
1563 SRF_RUNNING)) {
1564 mask |= POLLIN | POLLRDNORM;
1567 write_subdev = comedi_get_write_subdevice(dev_file_info);
1568 if (write_subdev) {
1569 poll_wait(file, &write_subdev->async->wait_head, wait);
1570 comedi_buf_write_alloc(write_subdev->async,
1571 write_subdev->async->prealloc_bufsz);
1572 if (!write_subdev->busy
1573 || !(comedi_get_subdevice_runflags(write_subdev) &
1574 SRF_RUNNING)
1575 || comedi_buf_write_n_allocated(write_subdev->async) >=
1576 bytes_per_sample(write_subdev->async->subdevice)) {
1577 mask |= POLLOUT | POLLWRNORM;
1581 mutex_unlock(&dev->mutex);
1582 return mask;
1585 static ssize_t comedi_write(struct file *file, const char __user *buf,
1586 size_t nbytes, loff_t *offset)
1588 struct comedi_subdevice *s;
1589 struct comedi_async *async;
1590 int n, m, count = 0, retval = 0;
1591 DECLARE_WAITQUEUE(wait, current);
1592 const unsigned minor = iminor(file->f_dentry->d_inode);
1593 struct comedi_device_file_info *dev_file_info;
1594 struct comedi_device *dev;
1595 dev_file_info = comedi_get_device_file_info(minor);
1597 if (dev_file_info == NULL)
1598 return -ENODEV;
1599 dev = dev_file_info->device;
1600 if (dev == NULL)
1601 return -ENODEV;
1603 if (!dev->attached) {
1604 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1605 retval = -ENODEV;
1606 goto done;
1609 s = comedi_get_write_subdevice(dev_file_info);
1610 if (s == NULL) {
1611 retval = -EIO;
1612 goto done;
1614 async = s->async;
1616 if (!nbytes) {
1617 retval = 0;
1618 goto done;
1620 if (!s->busy) {
1621 retval = 0;
1622 goto done;
1624 if (s->busy != file) {
1625 retval = -EACCES;
1626 goto done;
1628 add_wait_queue(&async->wait_head, &wait);
1629 while (nbytes > 0 && !retval) {
1630 set_current_state(TASK_INTERRUPTIBLE);
1632 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1633 if (count == 0) {
1634 if (comedi_get_subdevice_runflags(s) &
1635 SRF_ERROR) {
1636 retval = -EPIPE;
1637 } else {
1638 retval = 0;
1640 do_become_nonbusy(dev, s);
1642 break;
1645 n = nbytes;
1647 m = n;
1648 if (async->buf_write_ptr + m > async->prealloc_bufsz)
1649 m = async->prealloc_bufsz - async->buf_write_ptr;
1650 comedi_buf_write_alloc(async, async->prealloc_bufsz);
1651 if (m > comedi_buf_write_n_allocated(async))
1652 m = comedi_buf_write_n_allocated(async);
1653 if (m < n)
1654 n = m;
1656 if (n == 0) {
1657 if (file->f_flags & O_NONBLOCK) {
1658 retval = -EAGAIN;
1659 break;
1661 if (signal_pending(current)) {
1662 retval = -ERESTARTSYS;
1663 break;
1665 schedule();
1666 if (!s->busy)
1667 break;
1668 if (s->busy != file) {
1669 retval = -EACCES;
1670 break;
1672 continue;
1675 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
1676 buf, n);
1677 if (m) {
1678 n -= m;
1679 retval = -EFAULT;
1681 comedi_buf_write_free(async, n);
1683 count += n;
1684 nbytes -= n;
1686 buf += n;
1687 break; /* makes device work like a pipe */
1689 set_current_state(TASK_RUNNING);
1690 remove_wait_queue(&async->wait_head, &wait);
1692 done:
1693 return count ? count : retval;
1696 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
1697 loff_t *offset)
1699 struct comedi_subdevice *s;
1700 struct comedi_async *async;
1701 int n, m, count = 0, retval = 0;
1702 DECLARE_WAITQUEUE(wait, current);
1703 const unsigned minor = iminor(file->f_dentry->d_inode);
1704 struct comedi_device_file_info *dev_file_info;
1705 struct comedi_device *dev;
1706 dev_file_info = comedi_get_device_file_info(minor);
1708 if (dev_file_info == NULL)
1709 return -ENODEV;
1710 dev = dev_file_info->device;
1711 if (dev == NULL)
1712 return -ENODEV;
1714 if (!dev->attached) {
1715 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1716 retval = -ENODEV;
1717 goto done;
1720 s = comedi_get_read_subdevice(dev_file_info);
1721 if (s == NULL) {
1722 retval = -EIO;
1723 goto done;
1725 async = s->async;
1726 if (!nbytes) {
1727 retval = 0;
1728 goto done;
1730 if (!s->busy) {
1731 retval = 0;
1732 goto done;
1734 if (s->busy != file) {
1735 retval = -EACCES;
1736 goto done;
1739 add_wait_queue(&async->wait_head, &wait);
1740 while (nbytes > 0 && !retval) {
1741 set_current_state(TASK_INTERRUPTIBLE);
1743 n = nbytes;
1745 m = comedi_buf_read_n_available(async);
1746 /* printk("%d available\n",m); */
1747 if (async->buf_read_ptr + m > async->prealloc_bufsz)
1748 m = async->prealloc_bufsz - async->buf_read_ptr;
1749 /* printk("%d contiguous\n",m); */
1750 if (m < n)
1751 n = m;
1753 if (n == 0) {
1754 if (!(comedi_get_subdevice_runflags(s) & SRF_RUNNING)) {
1755 do_become_nonbusy(dev, s);
1756 if (comedi_get_subdevice_runflags(s) &
1757 SRF_ERROR) {
1758 retval = -EPIPE;
1759 } else {
1760 retval = 0;
1762 break;
1764 if (file->f_flags & O_NONBLOCK) {
1765 retval = -EAGAIN;
1766 break;
1768 if (signal_pending(current)) {
1769 retval = -ERESTARTSYS;
1770 break;
1772 schedule();
1773 if (!s->busy) {
1774 retval = 0;
1775 break;
1777 if (s->busy != file) {
1778 retval = -EACCES;
1779 break;
1781 continue;
1783 m = copy_to_user(buf, async->prealloc_buf +
1784 async->buf_read_ptr, n);
1785 if (m) {
1786 n -= m;
1787 retval = -EFAULT;
1790 comedi_buf_read_alloc(async, n);
1791 comedi_buf_read_free(async, n);
1793 count += n;
1794 nbytes -= n;
1796 buf += n;
1797 break; /* makes device work like a pipe */
1799 if (!(comedi_get_subdevice_runflags(s) & (SRF_ERROR | SRF_RUNNING)) &&
1800 async->buf_read_count - async->buf_write_count == 0) {
1801 do_become_nonbusy(dev, s);
1803 set_current_state(TASK_RUNNING);
1804 remove_wait_queue(&async->wait_head, &wait);
1806 done:
1807 return count ? count : retval;
1811 This function restores a subdevice to an idle state.
1813 void do_become_nonbusy(struct comedi_device *dev, struct comedi_subdevice *s)
1815 struct comedi_async *async = s->async;
1817 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
1818 if (async) {
1819 comedi_reset_async_buf(async);
1820 async->inttrig = NULL;
1821 } else {
1822 printk(KERN_ERR
1823 "BUG: (?) do_become_nonbusy called with async=0\n");
1826 s->busy = NULL;
1829 static int comedi_open(struct inode *inode, struct file *file)
1831 const unsigned minor = iminor(inode);
1832 struct comedi_device_file_info *dev_file_info =
1833 comedi_get_device_file_info(minor);
1834 struct comedi_device *dev =
1835 dev_file_info ? dev_file_info->device : NULL;
1837 if (dev == NULL) {
1838 DPRINTK("invalid minor number\n");
1839 return -ENODEV;
1842 /* This is slightly hacky, but we want module autoloading
1843 * to work for root.
1844 * case: user opens device, attached -> ok
1845 * case: user opens device, unattached, in_request_module=0 -> autoload
1846 * case: user opens device, unattached, in_request_module=1 -> fail
1847 * case: root opens device, attached -> ok
1848 * case: root opens device, unattached, in_request_module=1 -> ok
1849 * (typically called from modprobe)
1850 * case: root opens device, unattached, in_request_module=0 -> autoload
1852 * The last could be changed to "-> ok", which would deny root
1853 * autoloading.
1855 mutex_lock(&dev->mutex);
1856 if (dev->attached)
1857 goto ok;
1858 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
1859 DPRINTK("in request module\n");
1860 mutex_unlock(&dev->mutex);
1861 return -ENODEV;
1863 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
1864 goto ok;
1866 dev->in_request_module = 1;
1868 #ifdef CONFIG_KMOD
1869 mutex_unlock(&dev->mutex);
1870 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
1871 mutex_lock(&dev->mutex);
1872 #endif
1874 dev->in_request_module = 0;
1876 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
1877 DPRINTK("not attached and not CAP_NET_ADMIN\n");
1878 mutex_unlock(&dev->mutex);
1879 return -ENODEV;
1882 __module_get(THIS_MODULE);
1884 if (dev->attached) {
1885 if (!try_module_get(dev->driver->module)) {
1886 module_put(THIS_MODULE);
1887 mutex_unlock(&dev->mutex);
1888 return -ENOSYS;
1892 if (dev->attached && dev->use_count == 0 && dev->open) {
1893 int rc = dev->open(dev);
1894 if (rc < 0) {
1895 module_put(dev->driver->module);
1896 module_put(THIS_MODULE);
1897 mutex_unlock(&dev->mutex);
1898 return rc;
1902 dev->use_count++;
1904 mutex_unlock(&dev->mutex);
1906 return 0;
1909 static int comedi_close(struct inode *inode, struct file *file)
1911 const unsigned minor = iminor(inode);
1912 struct comedi_subdevice *s = NULL;
1913 int i;
1914 struct comedi_device_file_info *dev_file_info;
1915 struct comedi_device *dev;
1916 dev_file_info = comedi_get_device_file_info(minor);
1918 if (dev_file_info == NULL)
1919 return -ENODEV;
1920 dev = dev_file_info->device;
1921 if (dev == NULL)
1922 return -ENODEV;
1924 mutex_lock(&dev->mutex);
1926 if (dev->subdevices) {
1927 for (i = 0; i < dev->n_subdevices; i++) {
1928 s = dev->subdevices + i;
1930 if (s->busy == file)
1931 do_cancel(dev, s);
1932 if (s->lock == file)
1933 s->lock = NULL;
1936 if (dev->attached && dev->use_count == 1 && dev->close)
1937 dev->close(dev);
1939 module_put(THIS_MODULE);
1940 if (dev->attached)
1941 module_put(dev->driver->module);
1943 dev->use_count--;
1945 mutex_unlock(&dev->mutex);
1947 if (file->f_flags & FASYNC)
1948 comedi_fasync(-1, file, 0);
1950 return 0;
1953 static int comedi_fasync(int fd, struct file *file, int on)
1955 const unsigned minor = iminor(file->f_dentry->d_inode);
1956 struct comedi_device_file_info *dev_file_info;
1957 struct comedi_device *dev;
1958 dev_file_info = comedi_get_device_file_info(minor);
1960 if (dev_file_info == NULL)
1961 return -ENODEV;
1962 dev = dev_file_info->device;
1963 if (dev == NULL)
1964 return -ENODEV;
1966 return fasync_helper(fd, file, on, &dev->async_queue);
1969 const struct file_operations comedi_fops = {
1970 .owner = THIS_MODULE,
1971 .unlocked_ioctl = comedi_unlocked_ioctl,
1972 .compat_ioctl = comedi_compat_ioctl,
1973 .open = comedi_open,
1974 .release = comedi_close,
1975 .read = comedi_read,
1976 .write = comedi_write,
1977 .mmap = comedi_mmap,
1978 .poll = comedi_poll,
1979 .fasync = comedi_fasync,
1980 .llseek = noop_llseek,
1983 struct class *comedi_class;
1984 static struct cdev comedi_cdev;
1986 static void comedi_cleanup_legacy_minors(void)
1988 unsigned i;
1990 for (i = 0; i < comedi_num_legacy_minors; i++)
1991 comedi_free_board_minor(i);
1994 static int __init comedi_init(void)
1996 int i;
1997 int retval;
1999 printk(KERN_INFO "comedi: version " COMEDI_RELEASE
2000 " - http://www.comedi.org\n");
2002 if (comedi_num_legacy_minors < 0 ||
2003 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2004 printk(KERN_ERR "comedi: error: invalid value for module "
2005 "parameter \"comedi_num_legacy_minors\". Valid values "
2006 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS);
2007 return -EINVAL;
2011 * comedi is unusable if both comedi_autoconfig and
2012 * comedi_num_legacy_minors are zero, so we might as well adjust the
2013 * defaults in that case
2015 if (comedi_autoconfig == 0 && comedi_num_legacy_minors == 0)
2016 comedi_num_legacy_minors = 16;
2018 memset(comedi_file_info_table, 0,
2019 sizeof(struct comedi_device_file_info *) * COMEDI_NUM_MINORS);
2021 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2022 COMEDI_NUM_MINORS, "comedi");
2023 if (retval)
2024 return -EIO;
2025 cdev_init(&comedi_cdev, &comedi_fops);
2026 comedi_cdev.owner = THIS_MODULE;
2027 kobject_set_name(&comedi_cdev.kobj, "comedi");
2028 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2029 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2030 COMEDI_NUM_MINORS);
2031 return -EIO;
2033 comedi_class = class_create(THIS_MODULE, "comedi");
2034 if (IS_ERR(comedi_class)) {
2035 printk(KERN_ERR "comedi: failed to create class");
2036 cdev_del(&comedi_cdev);
2037 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2038 COMEDI_NUM_MINORS);
2039 return PTR_ERR(comedi_class);
2042 /* XXX requires /proc interface */
2043 comedi_proc_init();
2045 /* create devices files for legacy/manual use */
2046 for (i = 0; i < comedi_num_legacy_minors; i++) {
2047 int minor;
2048 minor = comedi_alloc_board_minor(NULL);
2049 if (minor < 0) {
2050 comedi_cleanup_legacy_minors();
2051 cdev_del(&comedi_cdev);
2052 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2053 COMEDI_NUM_MINORS);
2054 return minor;
2058 return 0;
2061 static void __exit comedi_cleanup(void)
2063 int i;
2065 comedi_cleanup_legacy_minors();
2066 for (i = 0; i < COMEDI_NUM_MINORS; ++i)
2067 BUG_ON(comedi_file_info_table[i]);
2069 class_destroy(comedi_class);
2070 cdev_del(&comedi_cdev);
2071 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2073 comedi_proc_cleanup();
2076 module_init(comedi_init);
2077 module_exit(comedi_cleanup);
2079 void comedi_error(const struct comedi_device *dev, const char *s)
2081 printk(KERN_ERR "comedi%d: %s: %s\n", dev->minor,
2082 dev->driver->driver_name, s);
2084 EXPORT_SYMBOL(comedi_error);
2086 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2088 struct comedi_async *async = s->async;
2089 unsigned runflags = 0;
2090 unsigned runflags_mask = 0;
2092 /* DPRINTK("comedi_event 0x%x\n",mask); */
2094 if ((comedi_get_subdevice_runflags(s) & SRF_RUNNING) == 0)
2095 return;
2097 if (s->
2098 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2099 COMEDI_CB_OVERFLOW)) {
2100 runflags_mask |= SRF_RUNNING;
2102 /* remember if an error event has occurred, so an error
2103 * can be returned the next time the user does a read() */
2104 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2105 runflags_mask |= SRF_ERROR;
2106 runflags |= SRF_ERROR;
2108 if (runflags_mask) {
2109 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2110 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2113 if (async->cb_mask & s->async->events) {
2114 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2115 wake_up_interruptible(&async->wait_head);
2116 if (s->subdev_flags & SDF_CMD_READ)
2117 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2118 if (s->subdev_flags & SDF_CMD_WRITE)
2119 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2120 } else {
2121 if (async->cb_func)
2122 async->cb_func(s->async->events, async->cb_arg);
2125 s->async->events = 0;
2127 EXPORT_SYMBOL(comedi_event);
2129 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
2131 unsigned long flags;
2132 unsigned runflags;
2134 spin_lock_irqsave(&s->spin_lock, flags);
2135 runflags = s->runflags;
2136 spin_unlock_irqrestore(&s->spin_lock, flags);
2137 return runflags;
2139 EXPORT_SYMBOL(comedi_get_subdevice_runflags);
2141 static int is_device_busy(struct comedi_device *dev)
2143 struct comedi_subdevice *s;
2144 int i;
2146 if (!dev->attached)
2147 return 0;
2149 for (i = 0; i < dev->n_subdevices; i++) {
2150 s = dev->subdevices + i;
2151 if (s->busy)
2152 return 1;
2153 if (s->async && s->async->mmap_count)
2154 return 1;
2157 return 0;
2160 static void comedi_device_init(struct comedi_device *dev)
2162 memset(dev, 0, sizeof(struct comedi_device));
2163 spin_lock_init(&dev->spinlock);
2164 mutex_init(&dev->mutex);
2165 dev->minor = -1;
2168 static void comedi_device_cleanup(struct comedi_device *dev)
2170 if (dev == NULL)
2171 return;
2172 mutex_lock(&dev->mutex);
2173 comedi_device_detach(dev);
2174 mutex_unlock(&dev->mutex);
2175 mutex_destroy(&dev->mutex);
2178 int comedi_alloc_board_minor(struct device *hardware_device)
2180 unsigned long flags;
2181 struct comedi_device_file_info *info;
2182 struct device *csdev;
2183 unsigned i;
2184 int retval;
2186 info = kzalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2187 if (info == NULL)
2188 return -ENOMEM;
2189 info->device = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2190 if (info->device == NULL) {
2191 kfree(info);
2192 return -ENOMEM;
2194 comedi_device_init(info->device);
2195 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2196 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) {
2197 if (comedi_file_info_table[i] == NULL) {
2198 comedi_file_info_table[i] = info;
2199 break;
2202 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2203 if (i == COMEDI_NUM_BOARD_MINORS) {
2204 comedi_device_cleanup(info->device);
2205 kfree(info->device);
2206 kfree(info);
2207 printk(KERN_ERR
2208 "comedi: error: "
2209 "ran out of minor numbers for board device files.\n");
2210 return -EBUSY;
2212 info->device->minor = i;
2213 csdev = device_create(comedi_class, hardware_device,
2214 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2215 if (!IS_ERR(csdev))
2216 info->device->class_dev = csdev;
2217 dev_set_drvdata(csdev, info);
2218 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2219 if (retval) {
2220 printk(KERN_ERR
2221 "comedi: "
2222 "failed to create sysfs attribute file \"%s\".\n",
2223 dev_attr_max_read_buffer_kb.attr.name);
2224 comedi_free_board_minor(i);
2225 return retval;
2227 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2228 if (retval) {
2229 printk(KERN_ERR
2230 "comedi: "
2231 "failed to create sysfs attribute file \"%s\".\n",
2232 dev_attr_read_buffer_kb.attr.name);
2233 comedi_free_board_minor(i);
2234 return retval;
2236 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2237 if (retval) {
2238 printk(KERN_ERR
2239 "comedi: "
2240 "failed to create sysfs attribute file \"%s\".\n",
2241 dev_attr_max_write_buffer_kb.attr.name);
2242 comedi_free_board_minor(i);
2243 return retval;
2245 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2246 if (retval) {
2247 printk(KERN_ERR
2248 "comedi: "
2249 "failed to create sysfs attribute file \"%s\".\n",
2250 dev_attr_write_buffer_kb.attr.name);
2251 comedi_free_board_minor(i);
2252 return retval;
2254 return i;
2257 void comedi_free_board_minor(unsigned minor)
2259 unsigned long flags;
2260 struct comedi_device_file_info *info;
2262 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2263 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2264 info = comedi_file_info_table[minor];
2265 comedi_file_info_table[minor] = NULL;
2266 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2268 if (info) {
2269 struct comedi_device *dev = info->device;
2270 if (dev) {
2271 if (dev->class_dev) {
2272 device_destroy(comedi_class,
2273 MKDEV(COMEDI_MAJOR, dev->minor));
2275 comedi_device_cleanup(dev);
2276 kfree(dev);
2278 kfree(info);
2282 int comedi_alloc_subdevice_minor(struct comedi_device *dev,
2283 struct comedi_subdevice *s)
2285 unsigned long flags;
2286 struct comedi_device_file_info *info;
2287 struct device *csdev;
2288 unsigned i;
2289 int retval;
2291 info = kmalloc(sizeof(struct comedi_device_file_info), GFP_KERNEL);
2292 if (info == NULL)
2293 return -ENOMEM;
2294 info->device = dev;
2295 info->read_subdevice = s;
2296 info->write_subdevice = s;
2297 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2298 for (i = COMEDI_FIRST_SUBDEVICE_MINOR; i < COMEDI_NUM_MINORS; ++i) {
2299 if (comedi_file_info_table[i] == NULL) {
2300 comedi_file_info_table[i] = info;
2301 break;
2304 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2305 if (i == COMEDI_NUM_MINORS) {
2306 kfree(info);
2307 printk(KERN_ERR
2308 "comedi: error: "
2309 "ran out of minor numbers for board device files.\n");
2310 return -EBUSY;
2312 s->minor = i;
2313 csdev = device_create(comedi_class, dev->class_dev,
2314 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2315 dev->minor, (int)(s - dev->subdevices));
2316 if (!IS_ERR(csdev))
2317 s->class_dev = csdev;
2318 dev_set_drvdata(csdev, info);
2319 retval = device_create_file(csdev, &dev_attr_max_read_buffer_kb);
2320 if (retval) {
2321 printk(KERN_ERR
2322 "comedi: "
2323 "failed to create sysfs attribute file \"%s\".\n",
2324 dev_attr_max_read_buffer_kb.attr.name);
2325 comedi_free_subdevice_minor(s);
2326 return retval;
2328 retval = device_create_file(csdev, &dev_attr_read_buffer_kb);
2329 if (retval) {
2330 printk(KERN_ERR
2331 "comedi: "
2332 "failed to create sysfs attribute file \"%s\".\n",
2333 dev_attr_read_buffer_kb.attr.name);
2334 comedi_free_subdevice_minor(s);
2335 return retval;
2337 retval = device_create_file(csdev, &dev_attr_max_write_buffer_kb);
2338 if (retval) {
2339 printk(KERN_ERR
2340 "comedi: "
2341 "failed to create sysfs attribute file \"%s\".\n",
2342 dev_attr_max_write_buffer_kb.attr.name);
2343 comedi_free_subdevice_minor(s);
2344 return retval;
2346 retval = device_create_file(csdev, &dev_attr_write_buffer_kb);
2347 if (retval) {
2348 printk(KERN_ERR
2349 "comedi: "
2350 "failed to create sysfs attribute file \"%s\".\n",
2351 dev_attr_write_buffer_kb.attr.name);
2352 comedi_free_subdevice_minor(s);
2353 return retval;
2355 return i;
2358 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2360 unsigned long flags;
2361 struct comedi_device_file_info *info;
2363 if (s == NULL)
2364 return;
2365 if (s->minor < 0)
2366 return;
2368 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2369 BUG_ON(s->minor < COMEDI_FIRST_SUBDEVICE_MINOR);
2371 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2372 info = comedi_file_info_table[s->minor];
2373 comedi_file_info_table[s->minor] = NULL;
2374 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2376 if (s->class_dev) {
2377 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2378 s->class_dev = NULL;
2380 kfree(info);
2383 struct comedi_device_file_info *comedi_get_device_file_info(unsigned minor)
2385 unsigned long flags;
2386 struct comedi_device_file_info *info;
2388 BUG_ON(minor >= COMEDI_NUM_MINORS);
2389 spin_lock_irqsave(&comedi_file_info_table_lock, flags);
2390 info = comedi_file_info_table[minor];
2391 spin_unlock_irqrestore(&comedi_file_info_table_lock, flags);
2392 return info;
2394 EXPORT_SYMBOL_GPL(comedi_get_device_file_info);
2396 static int resize_async_buffer(struct comedi_device *dev,
2397 struct comedi_subdevice *s,
2398 struct comedi_async *async, unsigned new_size)
2400 int retval;
2402 if (new_size > async->max_bufsize)
2403 return -EPERM;
2405 if (s->busy) {
2406 DPRINTK("subdevice is busy, cannot resize buffer\n");
2407 return -EBUSY;
2409 if (async->mmap_count) {
2410 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2411 return -EBUSY;
2414 if (!async->prealloc_buf)
2415 return -EINVAL;
2417 /* make sure buffer is an integral number of pages
2418 * (we round up) */
2419 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2421 retval = comedi_buf_alloc(dev, s, new_size);
2422 if (retval < 0)
2423 return retval;
2425 if (s->buf_change) {
2426 retval = s->buf_change(dev, s, new_size);
2427 if (retval < 0)
2428 return retval;
2431 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2432 dev->minor, (int)(s - dev->subdevices), async->prealloc_bufsz);
2433 return 0;
2436 /* sysfs attribute files */
2438 static const unsigned bytes_per_kibi = 1024;
2440 static ssize_t show_max_read_buffer_kb(struct device *dev,
2441 struct device_attribute *attr, char *buf)
2443 ssize_t retval;
2444 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2445 unsigned max_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 max_buffer_size_kb = read_subdevice->async->max_bufsize /
2454 bytes_per_kibi;
2456 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2457 mutex_unlock(&info->device->mutex);
2459 return retval;
2462 static ssize_t store_max_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_max_size_kb;
2468 uint64_t new_max_size;
2469 struct comedi_subdevice *const read_subdevice =
2470 comedi_get_read_subdevice(info);
2472 if (strict_strtoul(buf, 10, &new_max_size_kb))
2473 return -EINVAL;
2474 if (new_max_size_kb != (uint32_t) new_max_size_kb)
2475 return -EINVAL;
2476 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2477 if (new_max_size != (uint32_t) new_max_size)
2478 return -EINVAL;
2480 mutex_lock(&info->device->mutex);
2481 if (read_subdevice == NULL ||
2482 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2483 read_subdevice->async == NULL) {
2484 mutex_unlock(&info->device->mutex);
2485 return -EINVAL;
2487 read_subdevice->async->max_bufsize = new_max_size;
2488 mutex_unlock(&info->device->mutex);
2490 return count;
2493 static struct device_attribute dev_attr_max_read_buffer_kb = {
2494 .attr = {
2495 .name = "max_read_buffer_kb",
2496 .mode = S_IRUGO | S_IWUSR},
2497 .show = &show_max_read_buffer_kb,
2498 .store = &store_max_read_buffer_kb
2501 static ssize_t show_read_buffer_kb(struct device *dev,
2502 struct device_attribute *attr, char *buf)
2504 ssize_t retval;
2505 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2506 unsigned buffer_size_kb = 0;
2507 struct comedi_subdevice *const read_subdevice =
2508 comedi_get_read_subdevice(info);
2510 mutex_lock(&info->device->mutex);
2511 if (read_subdevice &&
2512 (read_subdevice->subdev_flags & SDF_CMD_READ) &&
2513 read_subdevice->async) {
2514 buffer_size_kb = read_subdevice->async->prealloc_bufsz /
2515 bytes_per_kibi;
2517 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2518 mutex_unlock(&info->device->mutex);
2520 return retval;
2523 static ssize_t store_read_buffer_kb(struct device *dev,
2524 struct device_attribute *attr,
2525 const char *buf, size_t count)
2527 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2528 unsigned long new_size_kb;
2529 uint64_t new_size;
2530 int retval;
2531 struct comedi_subdevice *const read_subdevice =
2532 comedi_get_read_subdevice(info);
2534 if (strict_strtoul(buf, 10, &new_size_kb))
2535 return -EINVAL;
2536 if (new_size_kb != (uint32_t) new_size_kb)
2537 return -EINVAL;
2538 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2539 if (new_size != (uint32_t) new_size)
2540 return -EINVAL;
2542 mutex_lock(&info->device->mutex);
2543 if (read_subdevice == NULL ||
2544 (read_subdevice->subdev_flags & SDF_CMD_READ) == 0 ||
2545 read_subdevice->async == NULL) {
2546 mutex_unlock(&info->device->mutex);
2547 return -EINVAL;
2549 retval = resize_async_buffer(info->device, read_subdevice,
2550 read_subdevice->async, new_size);
2551 mutex_unlock(&info->device->mutex);
2553 if (retval < 0)
2554 return retval;
2555 return count;
2558 static struct device_attribute dev_attr_read_buffer_kb = {
2559 .attr = {
2560 .name = "read_buffer_kb",
2561 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
2562 .show = &show_read_buffer_kb,
2563 .store = &store_read_buffer_kb
2566 static ssize_t show_max_write_buffer_kb(struct device *dev,
2567 struct device_attribute *attr,
2568 char *buf)
2570 ssize_t retval;
2571 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2572 unsigned max_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 max_buffer_size_kb = write_subdevice->async->max_bufsize /
2581 bytes_per_kibi;
2583 retval = snprintf(buf, PAGE_SIZE, "%i\n", max_buffer_size_kb);
2584 mutex_unlock(&info->device->mutex);
2586 return retval;
2589 static ssize_t store_max_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_max_size_kb;
2595 uint64_t new_max_size;
2596 struct comedi_subdevice *const write_subdevice =
2597 comedi_get_write_subdevice(info);
2599 if (strict_strtoul(buf, 10, &new_max_size_kb))
2600 return -EINVAL;
2601 if (new_max_size_kb != (uint32_t) new_max_size_kb)
2602 return -EINVAL;
2603 new_max_size = ((uint64_t) new_max_size_kb) * bytes_per_kibi;
2604 if (new_max_size != (uint32_t) new_max_size)
2605 return -EINVAL;
2607 mutex_lock(&info->device->mutex);
2608 if (write_subdevice == NULL ||
2609 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2610 write_subdevice->async == NULL) {
2611 mutex_unlock(&info->device->mutex);
2612 return -EINVAL;
2614 write_subdevice->async->max_bufsize = new_max_size;
2615 mutex_unlock(&info->device->mutex);
2617 return count;
2620 static struct device_attribute dev_attr_max_write_buffer_kb = {
2621 .attr = {
2622 .name = "max_write_buffer_kb",
2623 .mode = S_IRUGO | S_IWUSR},
2624 .show = &show_max_write_buffer_kb,
2625 .store = &store_max_write_buffer_kb
2628 static ssize_t show_write_buffer_kb(struct device *dev,
2629 struct device_attribute *attr, char *buf)
2631 ssize_t retval;
2632 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2633 unsigned buffer_size_kb = 0;
2634 struct comedi_subdevice *const write_subdevice =
2635 comedi_get_write_subdevice(info);
2637 mutex_lock(&info->device->mutex);
2638 if (write_subdevice &&
2639 (write_subdevice->subdev_flags & SDF_CMD_WRITE) &&
2640 write_subdevice->async) {
2641 buffer_size_kb = write_subdevice->async->prealloc_bufsz /
2642 bytes_per_kibi;
2644 retval = snprintf(buf, PAGE_SIZE, "%i\n", buffer_size_kb);
2645 mutex_unlock(&info->device->mutex);
2647 return retval;
2650 static ssize_t store_write_buffer_kb(struct device *dev,
2651 struct device_attribute *attr,
2652 const char *buf, size_t count)
2654 struct comedi_device_file_info *info = dev_get_drvdata(dev);
2655 unsigned long new_size_kb;
2656 uint64_t new_size;
2657 int retval;
2658 struct comedi_subdevice *const write_subdevice =
2659 comedi_get_write_subdevice(info);
2661 if (strict_strtoul(buf, 10, &new_size_kb))
2662 return -EINVAL;
2663 if (new_size_kb != (uint32_t) new_size_kb)
2664 return -EINVAL;
2665 new_size = ((uint64_t) new_size_kb) * bytes_per_kibi;
2666 if (new_size != (uint32_t) new_size)
2667 return -EINVAL;
2669 mutex_lock(&info->device->mutex);
2670 if (write_subdevice == NULL ||
2671 (write_subdevice->subdev_flags & SDF_CMD_WRITE) == 0 ||
2672 write_subdevice->async == NULL) {
2673 mutex_unlock(&info->device->mutex);
2674 return -EINVAL;
2676 retval = resize_async_buffer(info->device, write_subdevice,
2677 write_subdevice->async, new_size);
2678 mutex_unlock(&info->device->mutex);
2680 if (retval < 0)
2681 return retval;
2682 return count;
2685 static struct device_attribute dev_attr_write_buffer_kb = {
2686 .attr = {
2687 .name = "write_buffer_kb",
2688 .mode = S_IRUGO | S_IWUSR | S_IWGRP},
2689 .show = &show_write_buffer_kb,
2690 .store = &store_write_buffer_kb