5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define __NO_VERSION__
27 #include "comedi_fops.h"
28 #include "comedi_compat32.h"
30 #include <linux/module.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/fcntl.h>
35 #include <linux/delay.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/kmod.h>
40 #include <linux/poll.h>
41 #include <linux/init.h>
42 #include <linux/device.h>
43 #include <linux/vmalloc.h>
45 #include "comedidev.h"
46 #include <linux/cdev.h>
47 #include <linux/stat.h>
50 #include <linux/uaccess.h>
52 /* #include "kvmem.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
71 *comedi_file_info_table
[COMEDI_NUM_MINORS
];
73 static int do_devconfig_ioctl(struct comedi_device
*dev
,
74 struct comedi_devconfig
*arg
);
75 static int do_bufconfig_ioctl(struct comedi_device
*dev
, void *arg
);
76 static int do_devinfo_ioctl(struct comedi_device
*dev
,
77 struct comedi_devinfo
*arg
, struct file
*file
);
78 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
79 struct comedi_subdinfo
*arg
, void *file
);
80 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
81 struct comedi_chaninfo
*arg
);
82 static int do_bufinfo_ioctl(struct comedi_device
*dev
, void *arg
);
83 static int do_cmd_ioctl(struct comedi_device
*dev
, void *arg
, void *file
);
84 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
86 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
88 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
90 static int do_cmdtest_ioctl(struct comedi_device
*dev
, void *arg
, void *file
);
91 static int do_insnlist_ioctl(struct comedi_device
*dev
, void *arg
, void *file
);
92 static int do_insn_ioctl(struct comedi_device
*dev
, void *arg
, void *file
);
93 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int subd
,
96 extern void do_become_nonbusy(struct comedi_device
*dev
,
97 struct comedi_subdevice
*s
);
98 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
100 static int comedi_fasync(int fd
, struct file
*file
, int on
);
102 static int is_device_busy(struct comedi_device
*dev
);
103 static int resize_async_buffer(struct comedi_device
*dev
,
104 struct comedi_subdevice
*s
,
105 struct comedi_async
*async
, unsigned new_size
);
107 /* declarations for sysfs attribute files */
108 static struct device_attribute dev_attr_max_read_buffer_kb
;
109 static struct device_attribute dev_attr_read_buffer_kb
;
110 static struct device_attribute dev_attr_max_write_buffer_kb
;
111 static struct device_attribute dev_attr_write_buffer_kb
;
113 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
116 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
117 struct comedi_device_file_info
*dev_file_info
=
118 comedi_get_device_file_info(minor
);
119 struct comedi_device
*dev
;
122 if (dev_file_info
== NULL
|| dev_file_info
->device
== NULL
)
124 dev
= dev_file_info
->device
;
126 mutex_lock(&dev
->mutex
);
128 /* Device config is special, because it must work on
129 * an unconfigured device. */
130 if (cmd
== COMEDI_DEVCONFIG
) {
131 rc
= do_devconfig_ioctl(dev
, (void *)arg
);
135 if (!dev
->attached
) {
136 DPRINTK("no driver configured on /dev/comedi%i\n", dev
->minor
);
142 case COMEDI_BUFCONFIG
:
143 rc
= do_bufconfig_ioctl(dev
, (void *)arg
);
146 rc
= do_devinfo_ioctl(dev
, (void *)arg
, file
);
148 case COMEDI_SUBDINFO
:
149 rc
= do_subdinfo_ioctl(dev
, (void *)arg
, file
);
151 case COMEDI_CHANINFO
:
152 rc
= do_chaninfo_ioctl(dev
, (void *)arg
);
154 case COMEDI_RANGEINFO
:
155 rc
= do_rangeinfo_ioctl(dev
, (void *)arg
);
158 rc
= do_bufinfo_ioctl(dev
, (void *)arg
);
161 rc
= do_lock_ioctl(dev
, arg
, file
);
164 rc
= do_unlock_ioctl(dev
, arg
, file
);
167 rc
= do_cancel_ioctl(dev
, arg
, file
);
170 rc
= do_cmd_ioctl(dev
, (void *)arg
, file
);
173 rc
= do_cmdtest_ioctl(dev
, (void *)arg
, file
);
175 case COMEDI_INSNLIST
:
176 rc
= do_insnlist_ioctl(dev
, (void *)arg
, file
);
179 rc
= do_insn_ioctl(dev
, (void *)arg
, file
);
182 rc
= do_poll_ioctl(dev
, arg
, file
);
190 mutex_unlock(&dev
->mutex
);
199 pointer to devconfig structure
202 devconfig structure at arg
207 static int do_devconfig_ioctl(struct comedi_device
*dev
,
208 struct comedi_devconfig
*arg
)
210 struct comedi_devconfig it
;
212 unsigned char *aux_data
= NULL
;
215 if (!capable(CAP_SYS_ADMIN
))
219 if (is_device_busy(dev
))
222 struct module
*driver_module
= dev
->driver
->module
;
223 comedi_device_detach(dev
);
224 module_put(driver_module
);
229 if (copy_from_user(&it
, arg
, sizeof(struct comedi_devconfig
)))
232 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
234 if (comedi_aux_data(it
.options
, 0) &&
235 it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
237 aux_len
= it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
];
241 aux_data
= vmalloc(aux_len
);
245 if (copy_from_user(aux_data
,
246 comedi_aux_data(it
.options
, 0), aux_len
)) {
250 it
.options
[COMEDI_DEVCONF_AUX_DATA_LO
] =
251 (unsigned long)aux_data
;
252 if (sizeof(void *) > sizeof(int)) {
253 bit_shift
= sizeof(int) * 8;
254 it
.options
[COMEDI_DEVCONF_AUX_DATA_HI
] =
255 ((unsigned long)aux_data
) >> bit_shift
;
257 it
.options
[COMEDI_DEVCONF_AUX_DATA_HI
] = 0;
260 ret
= comedi_device_attach(dev
, &it
);
262 if (!try_module_get(dev
->driver
->module
)) {
263 comedi_device_detach(dev
);
276 buffer configuration ioctl
279 pointer to bufconfig structure
285 modified bufconfig at arg
288 static int do_bufconfig_ioctl(struct comedi_device
*dev
, void *arg
)
290 struct comedi_bufconfig bc
;
291 struct comedi_async
*async
;
292 struct comedi_subdevice
*s
;
295 if (copy_from_user(&bc
, arg
, sizeof(struct comedi_bufconfig
)))
298 if (bc
.subdevice
>= dev
->n_subdevices
|| bc
.subdevice
< 0)
301 s
= dev
->subdevices
+ bc
.subdevice
;
305 DPRINTK("subdevice does not have async capability\n");
311 if (bc
.maximum_size
) {
312 if (!capable(CAP_SYS_ADMIN
))
315 async
->max_bufsize
= bc
.maximum_size
;
319 retval
= resize_async_buffer(dev
, s
, async
, bc
.size
);
324 bc
.size
= async
->prealloc_bufsz
;
325 bc
.maximum_size
= async
->max_bufsize
;
328 if (copy_to_user(arg
, &bc
, sizeof(struct comedi_bufconfig
)))
339 pointer to devinfo structure
348 static int do_devinfo_ioctl(struct comedi_device
*dev
,
349 struct comedi_devinfo
*arg
, struct file
*file
)
351 struct comedi_devinfo devinfo
;
352 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
353 struct comedi_device_file_info
*dev_file_info
=
354 comedi_get_device_file_info(minor
);
355 struct comedi_subdevice
*read_subdev
=
356 comedi_get_read_subdevice(dev_file_info
);
357 struct comedi_subdevice
*write_subdev
=
358 comedi_get_write_subdevice(dev_file_info
);
360 memset(&devinfo
, 0, sizeof(devinfo
));
362 /* fill devinfo structure */
363 devinfo
.version_code
= COMEDI_VERSION_CODE
;
364 devinfo
.n_subdevs
= dev
->n_subdevices
;
365 memcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
366 memcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
369 devinfo
.read_subdevice
= read_subdev
- dev
->subdevices
;
371 devinfo
.read_subdevice
= -1;
374 devinfo
.write_subdevice
= write_subdev
- dev
->subdevices
;
376 devinfo
.write_subdevice
= -1;
378 if (copy_to_user(arg
, &devinfo
, sizeof(struct comedi_devinfo
)))
389 pointer to array of subdevice info structures
395 array of subdevice info structures at arg
398 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
399 struct comedi_subdinfo
*arg
, void *file
)
402 struct comedi_subdinfo
*tmp
, *us
;
403 struct comedi_subdevice
*s
;
406 kcalloc(dev
->n_subdevices
, sizeof(struct comedi_subdinfo
),
411 /* fill subdinfo structs */
412 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
413 s
= dev
->subdevices
+ i
;
417 us
->n_chan
= s
->n_chan
;
418 us
->subd_flags
= s
->subdev_flags
;
419 if (comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)
420 us
->subd_flags
|= SDF_RUNNING
;
421 #define TIMER_nanosec 5 /* backwards compatibility */
422 us
->timer_type
= TIMER_nanosec
;
423 us
->len_chanlist
= s
->len_chanlist
;
424 us
->maxdata
= s
->maxdata
;
425 if (s
->range_table
) {
427 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
429 us
->range_type
= 0; /* XXX */
431 us
->flags
= s
->flags
;
434 us
->subd_flags
|= SDF_BUSY
;
436 us
->subd_flags
|= SDF_BUSY_OWNER
;
438 us
->subd_flags
|= SDF_LOCKED
;
440 us
->subd_flags
|= SDF_LOCK_OWNER
;
441 if (!s
->maxdata
&& s
->maxdata_list
)
442 us
->subd_flags
|= SDF_MAXDATA
;
444 us
->subd_flags
|= SDF_FLAGS
;
445 if (s
->range_table_list
)
446 us
->subd_flags
|= SDF_RANGETYPE
;
448 us
->subd_flags
|= SDF_CMD
;
450 if (s
->insn_bits
!= &insn_inval
)
451 us
->insn_bits_support
= COMEDI_SUPPORTED
;
453 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
455 us
->settling_time_0
= s
->settling_time_0
;
458 ret
= copy_to_user(arg
, tmp
,
459 dev
->n_subdevices
* sizeof(struct comedi_subdinfo
));
463 return ret
? -EFAULT
: 0;
471 pointer to chaninfo structure
474 chaninfo structure at arg
477 arrays at elements of chaninfo structure
480 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
481 struct comedi_chaninfo
*arg
)
483 struct comedi_subdevice
*s
;
484 struct comedi_chaninfo it
;
486 if (copy_from_user(&it
, arg
, sizeof(struct comedi_chaninfo
)))
489 if (it
.subdev
>= dev
->n_subdevices
)
491 s
= dev
->subdevices
+ it
.subdev
;
493 if (it
.maxdata_list
) {
494 if (s
->maxdata
|| !s
->maxdata_list
)
496 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
497 s
->n_chan
* sizeof(unsigned int)))
504 if (copy_to_user(it
.flaglist
, s
->flaglist
,
505 s
->n_chan
* sizeof(unsigned int)))
512 if (!s
->range_table_list
)
514 for (i
= 0; i
< s
->n_chan
; i
++) {
517 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
518 (s
->range_table_list
[i
]->length
);
519 put_user(x
, it
.rangelist
+ i
);
522 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
523 s
->n_chan
* sizeof(unsigned int)))
533 buffer information ioctl
536 pointer to bufinfo structure
542 modified bufinfo at arg
545 static int do_bufinfo_ioctl(struct comedi_device
*dev
, void *arg
)
547 struct comedi_bufinfo bi
;
548 struct comedi_subdevice
*s
;
549 struct comedi_async
*async
;
551 if (copy_from_user(&bi
, arg
, sizeof(struct comedi_bufinfo
)))
554 if (bi
.subdevice
>= dev
->n_subdevices
|| bi
.subdevice
< 0)
557 s
= dev
->subdevices
+ bi
.subdevice
;
561 DPRINTK("subdevice does not have async capability\n");
562 bi
.buf_write_ptr
= 0;
564 bi
.buf_write_count
= 0;
565 bi
.buf_read_count
= 0;
569 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
570 bi
.bytes_read
= comedi_buf_read_alloc(async
, bi
.bytes_read
);
571 comedi_buf_read_free(async
, bi
.bytes_read
);
573 if (!(comedi_get_subdevice_runflags(s
) & (SRF_ERROR
|
575 && async
->buf_write_count
== async
->buf_read_count
) {
576 do_become_nonbusy(dev
, s
);
580 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
582 comedi_buf_write_alloc(async
, bi
.bytes_written
);
583 comedi_buf_write_free(async
, bi
.bytes_written
);
586 bi
.buf_write_count
= async
->buf_write_count
;
587 bi
.buf_write_ptr
= async
->buf_write_ptr
;
588 bi
.buf_read_count
= async
->buf_read_count
;
589 bi
.buf_read_ptr
= async
->buf_read_ptr
;
592 if (copy_to_user(arg
, &bi
, sizeof(struct comedi_bufinfo
)))
598 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
599 unsigned int *data
, void *file
);
602 * synchronous instructions
605 * pointer to sync cmd structure
608 * sync cmd struct at arg
615 /* arbitrary limits */
616 #define MAX_SAMPLES 256
617 static int do_insnlist_ioctl(struct comedi_device
*dev
, void *arg
, void *file
)
619 struct comedi_insnlist insnlist
;
620 struct comedi_insn
*insns
= NULL
;
621 unsigned int *data
= NULL
;
625 if (copy_from_user(&insnlist
, arg
, sizeof(struct comedi_insnlist
)))
628 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
630 DPRINTK("kmalloc failed\n");
636 kmalloc(sizeof(struct comedi_insn
) * insnlist
.n_insns
, GFP_KERNEL
);
638 DPRINTK("kmalloc failed\n");
643 if (copy_from_user(insns
, insnlist
.insns
,
644 sizeof(struct comedi_insn
) * insnlist
.n_insns
)) {
645 DPRINTK("copy_from_user failed\n");
650 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
651 if (insns
[i
].n
> MAX_SAMPLES
) {
652 DPRINTK("number of samples too large\n");
656 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
657 if (copy_from_user(data
, insns
[i
].data
,
658 insns
[i
].n
* sizeof(unsigned int))) {
659 DPRINTK("copy_from_user failed\n");
664 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
667 if (insns
[i
].insn
& INSN_MASK_READ
) {
668 if (copy_to_user(insns
[i
].data
, data
,
669 insns
[i
].n
* sizeof(unsigned int))) {
670 DPRINTK("copy_to_user failed\n");
688 static int check_insn_config_length(struct comedi_insn
*insn
,
695 case INSN_CONFIG_DIO_OUTPUT
:
696 case INSN_CONFIG_DIO_INPUT
:
697 case INSN_CONFIG_DISARM
:
698 case INSN_CONFIG_RESET
:
702 case INSN_CONFIG_ARM
:
703 case INSN_CONFIG_DIO_QUERY
:
704 case INSN_CONFIG_BLOCK_SIZE
:
705 case INSN_CONFIG_FILTER
:
706 case INSN_CONFIG_SERIAL_CLOCK
:
707 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
708 case INSN_CONFIG_ALT_SOURCE
:
709 case INSN_CONFIG_SET_COUNTER_MODE
:
710 case INSN_CONFIG_8254_READ_STATUS
:
711 case INSN_CONFIG_SET_ROUTING
:
712 case INSN_CONFIG_GET_ROUTING
:
713 case INSN_CONFIG_GET_PWM_STATUS
:
714 case INSN_CONFIG_PWM_SET_PERIOD
:
715 case INSN_CONFIG_PWM_GET_PERIOD
:
719 case INSN_CONFIG_SET_GATE_SRC
:
720 case INSN_CONFIG_GET_GATE_SRC
:
721 case INSN_CONFIG_SET_CLOCK_SRC
:
722 case INSN_CONFIG_GET_CLOCK_SRC
:
723 case INSN_CONFIG_SET_OTHER_SRC
:
724 case INSN_CONFIG_GET_COUNTER_STATUS
:
725 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
726 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
727 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
731 case INSN_CONFIG_PWM_OUTPUT
:
732 case INSN_CONFIG_ANALOG_TRIG
:
736 /* by default we allow the insn since we don't have checks for
737 * all possible cases yet */
739 printk("comedi: no check for data length of config insn id "
740 "%i is implemented.\n"
741 " Add a check to %s in %s.\n"
742 " Assuming n=%i is correct.\n", data
[0], __func__
,
750 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
751 unsigned int *data
, void *file
)
753 struct comedi_subdevice
*s
;
757 if (insn
->insn
& INSN_MASK_SPECIAL
) {
758 /* a non-subdevice instruction */
760 switch (insn
->insn
) {
770 do_gettimeofday(&tv
);
772 data
[1] = tv
.tv_usec
;
778 if (insn
->n
!= 1 || data
[0] >= 100000) {
782 udelay(data
[0] / 1000);
790 if (insn
->subdev
>= dev
->n_subdevices
) {
791 DPRINTK("%d not usable subdevice\n",
796 s
= dev
->subdevices
+ insn
->subdev
;
798 DPRINTK("no async\n");
802 if (!s
->async
->inttrig
) {
803 DPRINTK("no inttrig\n");
807 ret
= s
->async
->inttrig(dev
, s
, insn
->data
[0]);
812 DPRINTK("invalid insn\n");
817 /* a subdevice instruction */
818 unsigned int maxdata
;
820 if (insn
->subdev
>= dev
->n_subdevices
) {
821 DPRINTK("subdevice %d out of range\n", insn
->subdev
);
825 s
= dev
->subdevices
+ insn
->subdev
;
827 if (s
->type
== COMEDI_SUBD_UNUSED
) {
828 DPRINTK("%d not usable subdevice\n", insn
->subdev
);
833 /* are we locked? (ioctl lock) */
834 if (s
->lock
&& s
->lock
!= file
) {
835 DPRINTK("device locked\n");
840 ret
= check_chanlist(s
, 1, &insn
->chanspec
);
843 DPRINTK("bad chanspec\n");
851 /* This looks arbitrary. It is. */
852 s
->busy
= &parse_insn
;
853 switch (insn
->insn
) {
855 ret
= s
->insn_read(dev
, s
, insn
, data
);
858 maxdata
= s
->maxdata_list
859 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
861 for (i
= 0; i
< insn
->n
; ++i
) {
862 if (data
[i
] > maxdata
) {
864 DPRINTK("bad data value(s)\n");
869 ret
= s
->insn_write(dev
, s
, insn
, data
);
876 ret
= s
->insn_bits(dev
, s
, insn
, data
);
879 ret
= check_insn_config_length(insn
, data
);
882 ret
= s
->insn_config(dev
, s
, insn
, data
);
898 * synchronous instructions
904 * struct comedi_insn struct at arg
910 static int do_insn_ioctl(struct comedi_device
*dev
, void *arg
, void *file
)
912 struct comedi_insn insn
;
913 unsigned int *data
= NULL
;
916 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
922 if (copy_from_user(&insn
, arg
, sizeof(struct comedi_insn
))) {
927 /* This is where the behavior of insn and insnlist deviate. */
928 if (insn
.n
> MAX_SAMPLES
)
929 insn
.n
= MAX_SAMPLES
;
930 if (insn
.insn
& INSN_MASK_WRITE
) {
932 (data
, insn
.data
, insn
.n
* sizeof(unsigned int))) {
937 ret
= parse_insn(dev
, &insn
, data
, file
);
940 if (insn
.insn
& INSN_MASK_READ
) {
942 (insn
.data
, data
, insn
.n
* sizeof(unsigned int))) {
960 pointer to cmd structure
967 modified cmd structure at arg
970 static int do_cmd_ioctl(struct comedi_device
*dev
, void *arg
, void *file
)
972 struct comedi_cmd user_cmd
;
973 struct comedi_subdevice
*s
;
974 struct comedi_async
*async
;
976 unsigned int *chanlist_saver
= NULL
;
978 if (copy_from_user(&user_cmd
, arg
, sizeof(struct comedi_cmd
))) {
979 DPRINTK("bad cmd address\n");
982 /* save user's chanlist pointer so it can be restored later */
983 chanlist_saver
= user_cmd
.chanlist
;
985 if (user_cmd
.subdev
>= dev
->n_subdevices
) {
986 DPRINTK("%d no such subdevice\n", user_cmd
.subdev
);
990 s
= dev
->subdevices
+ user_cmd
.subdev
;
993 if (s
->type
== COMEDI_SUBD_UNUSED
) {
994 DPRINTK("%d not valid subdevice\n", user_cmd
.subdev
);
998 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
999 DPRINTK("subdevice %i does not support commands\n",
1004 /* are we locked? (ioctl lock) */
1005 if (s
->lock
&& s
->lock
!= file
) {
1006 DPRINTK("subdevice locked\n");
1012 DPRINTK("subdevice busy\n");
1017 /* make sure channel/gain list isn't too long */
1018 if (user_cmd
.chanlist_len
> s
->len_chanlist
) {
1019 DPRINTK("channel/gain list too long %u > %d\n",
1020 user_cmd
.chanlist_len
, s
->len_chanlist
);
1025 /* make sure channel/gain list isn't too short */
1026 if (user_cmd
.chanlist_len
< 1) {
1027 DPRINTK("channel/gain list too short %u < 1\n",
1028 user_cmd
.chanlist_len
);
1033 kfree(async
->cmd
.chanlist
);
1034 async
->cmd
= user_cmd
;
1035 async
->cmd
.data
= NULL
;
1036 /* load channel/gain list */
1037 async
->cmd
.chanlist
=
1038 kmalloc(async
->cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1039 if (!async
->cmd
.chanlist
) {
1040 DPRINTK("allocation failed\n");
1045 if (copy_from_user(async
->cmd
.chanlist
, user_cmd
.chanlist
,
1046 async
->cmd
.chanlist_len
* sizeof(int))) {
1047 DPRINTK("fault reading chanlist\n");
1052 /* make sure each element in channel/gain list is valid */
1053 ret
= check_chanlist(s
, async
->cmd
.chanlist_len
, async
->cmd
.chanlist
);
1055 DPRINTK("bad chanlist\n");
1059 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1061 if (async
->cmd
.flags
& TRIG_BOGUS
|| ret
) {
1062 DPRINTK("test returned %d\n", ret
);
1063 user_cmd
= async
->cmd
;
1064 /* restore chanlist pointer before copying back */
1065 user_cmd
.chanlist
= chanlist_saver
;
1066 user_cmd
.data
= NULL
;
1067 if (copy_to_user(arg
, &user_cmd
, sizeof(struct comedi_cmd
))) {
1068 DPRINTK("fault writing cmd\n");
1076 if (!async
->prealloc_bufsz
) {
1078 DPRINTK("no buffer (?)\n");
1082 comedi_reset_async_buf(async
);
1085 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1087 if (async
->cmd
.flags
& TRIG_WAKE_EOS
)
1088 async
->cb_mask
|= COMEDI_CB_EOS
;
1090 comedi_set_subdevice_runflags(s
, ~0, SRF_USER
| SRF_RUNNING
);
1092 ret
= s
->do_cmd(dev
, s
);
1097 do_become_nonbusy(dev
, s
);
1104 command testing ioctl
1107 pointer to cmd structure
1110 cmd structure at arg
1114 modified cmd structure at arg
1117 static int do_cmdtest_ioctl(struct comedi_device
*dev
, void *arg
, void *file
)
1119 struct comedi_cmd user_cmd
;
1120 struct comedi_subdevice
*s
;
1122 unsigned int *chanlist
= NULL
;
1123 unsigned int *chanlist_saver
= NULL
;
1125 if (copy_from_user(&user_cmd
, arg
, sizeof(struct comedi_cmd
))) {
1126 DPRINTK("bad cmd address\n");
1129 /* save user's chanlist pointer so it can be restored later */
1130 chanlist_saver
= user_cmd
.chanlist
;
1132 if (user_cmd
.subdev
>= dev
->n_subdevices
) {
1133 DPRINTK("%d no such subdevice\n", user_cmd
.subdev
);
1137 s
= dev
->subdevices
+ user_cmd
.subdev
;
1138 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1139 DPRINTK("%d not valid subdevice\n", user_cmd
.subdev
);
1143 if (!s
->do_cmd
|| !s
->do_cmdtest
) {
1144 DPRINTK("subdevice %i does not support commands\n",
1149 /* make sure channel/gain list isn't too long */
1150 if (user_cmd
.chanlist_len
> s
->len_chanlist
) {
1151 DPRINTK("channel/gain list too long %d > %d\n",
1152 user_cmd
.chanlist_len
, s
->len_chanlist
);
1157 /* load channel/gain list */
1158 if (user_cmd
.chanlist
) {
1160 kmalloc(user_cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1162 DPRINTK("allocation failed\n");
1167 if (copy_from_user(chanlist
, user_cmd
.chanlist
,
1168 user_cmd
.chanlist_len
* sizeof(int))) {
1169 DPRINTK("fault reading chanlist\n");
1174 /* make sure each element in channel/gain list is valid */
1175 ret
= check_chanlist(s
, user_cmd
.chanlist_len
, chanlist
);
1177 DPRINTK("bad chanlist\n");
1181 user_cmd
.chanlist
= chanlist
;
1184 ret
= s
->do_cmdtest(dev
, s
, &user_cmd
);
1186 /* restore chanlist pointer before copying back */
1187 user_cmd
.chanlist
= chanlist_saver
;
1189 if (copy_to_user(arg
, &user_cmd
, sizeof(struct comedi_cmd
))) {
1190 DPRINTK("bad cmd address\n");
1215 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1219 unsigned long flags
;
1220 struct comedi_subdevice
*s
;
1222 if (arg
>= dev
->n_subdevices
)
1224 s
= dev
->subdevices
+ arg
;
1226 spin_lock_irqsave(&s
->spin_lock
, flags
);
1227 if (s
->busy
|| s
->lock
)
1231 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1238 ret
= s
->lock_f(dev
, s
);
1257 This function isn't protected by the semaphore, since
1258 we already own the lock.
1260 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1263 struct comedi_subdevice
*s
;
1265 if (arg
>= dev
->n_subdevices
)
1267 s
= dev
->subdevices
+ arg
;
1272 if (s
->lock
&& s
->lock
!= file
)
1275 if (s
->lock
== file
) {
1289 cancel acquisition ioctl
1301 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1304 struct comedi_subdevice
*s
;
1306 if (arg
>= dev
->n_subdevices
)
1308 s
= dev
->subdevices
+ arg
;
1309 if (s
->async
== NULL
)
1312 if (s
->lock
&& s
->lock
!= file
)
1318 if (s
->busy
!= file
)
1321 return do_cancel(dev
, s
);
1326 instructs driver to synchronize buffers
1338 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1341 struct comedi_subdevice
*s
;
1343 if (arg
>= dev
->n_subdevices
)
1345 s
= dev
->subdevices
+ arg
;
1347 if (s
->lock
&& s
->lock
!= file
)
1353 if (s
->busy
!= file
)
1357 return s
->poll(dev
, s
);
1362 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1366 if ((comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) && s
->cancel
)
1367 ret
= s
->cancel(dev
, s
);
1369 do_become_nonbusy(dev
, s
);
1374 void comedi_unmap(struct vm_area_struct
*area
)
1376 struct comedi_async
*async
;
1377 struct comedi_device
*dev
;
1379 async
= area
->vm_private_data
;
1380 dev
= async
->subdevice
->device
;
1382 mutex_lock(&dev
->mutex
);
1383 async
->mmap_count
--;
1384 mutex_unlock(&dev
->mutex
);
1387 static struct vm_operations_struct comedi_vm_ops
= {
1388 .close
= comedi_unmap
,
1391 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1393 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1394 struct comedi_device_file_info
*dev_file_info
=
1395 comedi_get_device_file_info(minor
);
1396 struct comedi_device
*dev
= dev_file_info
->device
;
1397 struct comedi_async
*async
= NULL
;
1398 unsigned long start
= vma
->vm_start
;
1403 struct comedi_subdevice
*s
;
1405 mutex_lock(&dev
->mutex
);
1406 if (!dev
->attached
) {
1407 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1411 if (vma
->vm_flags
& VM_WRITE
)
1412 s
= comedi_get_write_subdevice(dev_file_info
);
1414 s
= comedi_get_read_subdevice(dev_file_info
);
1421 if (async
== NULL
) {
1426 if (vma
->vm_pgoff
!= 0) {
1427 DPRINTK("comedi: mmap() offset must be 0.\n");
1432 size
= vma
->vm_end
- vma
->vm_start
;
1433 if (size
> async
->prealloc_bufsz
) {
1437 if (size
& (~PAGE_MASK
)) {
1442 n_pages
= size
>> PAGE_SHIFT
;
1443 for (i
= 0; i
< n_pages
; ++i
) {
1444 if (remap_pfn_range(vma
, start
,
1445 page_to_pfn(virt_to_page
1446 (async
->buf_page_list
1447 [i
].virt_addr
)), PAGE_SIZE
,
1455 vma
->vm_ops
= &comedi_vm_ops
;
1456 vma
->vm_private_data
= async
;
1458 async
->mmap_count
++;
1462 mutex_unlock(&dev
->mutex
);
1466 static unsigned int comedi_poll(struct file
*file
, poll_table
* wait
)
1468 unsigned int mask
= 0;
1469 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1470 struct comedi_device_file_info
*dev_file_info
=
1471 comedi_get_device_file_info(minor
);
1472 struct comedi_device
*dev
= dev_file_info
->device
;
1473 struct comedi_subdevice
*read_subdev
;
1474 struct comedi_subdevice
*write_subdev
;
1476 mutex_lock(&dev
->mutex
);
1477 if (!dev
->attached
) {
1478 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1479 mutex_unlock(&dev
->mutex
);
1484 read_subdev
= comedi_get_read_subdevice(dev_file_info
);
1486 poll_wait(file
, &read_subdev
->async
->wait_head
, wait
);
1487 if (!read_subdev
->busy
1488 || comedi_buf_read_n_available(read_subdev
->async
) > 0
1489 || !(comedi_get_subdevice_runflags(read_subdev
) &
1491 mask
|= POLLIN
| POLLRDNORM
;
1494 write_subdev
= comedi_get_write_subdevice(dev_file_info
);
1496 poll_wait(file
, &write_subdev
->async
->wait_head
, wait
);
1497 comedi_buf_write_alloc(write_subdev
->async
,
1498 write_subdev
->async
->prealloc_bufsz
);
1499 if (!write_subdev
->busy
1500 || !(comedi_get_subdevice_runflags(write_subdev
) &
1502 || comedi_buf_write_n_allocated(write_subdev
->async
) >=
1503 bytes_per_sample(write_subdev
->async
->subdevice
)) {
1504 mask
|= POLLOUT
| POLLWRNORM
;
1508 mutex_unlock(&dev
->mutex
);
1512 static ssize_t
comedi_write(struct file
*file
, const char *buf
, size_t nbytes
,
1515 struct comedi_subdevice
*s
;
1516 struct comedi_async
*async
;
1517 int n
, m
, count
= 0, retval
= 0;
1518 DECLARE_WAITQUEUE(wait
, current
);
1519 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1520 struct comedi_device_file_info
*dev_file_info
=
1521 comedi_get_device_file_info(minor
);
1522 struct comedi_device
*dev
= dev_file_info
->device
;
1524 if (!dev
->attached
) {
1525 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1530 s
= comedi_get_write_subdevice(dev_file_info
);
1545 if (s
->busy
!= file
) {
1549 add_wait_queue(&async
->wait_head
, &wait
);
1550 while (nbytes
> 0 && !retval
) {
1551 set_current_state(TASK_INTERRUPTIBLE
);
1556 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
1557 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
1558 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
1559 if (m
> comedi_buf_write_n_allocated(async
))
1560 m
= comedi_buf_write_n_allocated(async
);
1565 if (!(comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)) {
1566 if (comedi_get_subdevice_runflags(s
) &
1572 do_become_nonbusy(dev
, s
);
1575 if (file
->f_flags
& O_NONBLOCK
) {
1579 if (signal_pending(current
)) {
1580 retval
= -ERESTARTSYS
;
1586 if (s
->busy
!= file
) {
1593 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
1599 comedi_buf_write_free(async
, n
);
1605 break; /* makes device work like a pipe */
1607 set_current_state(TASK_RUNNING
);
1608 remove_wait_queue(&async
->wait_head
, &wait
);
1611 return count
? count
: retval
;
1614 static ssize_t
comedi_read(struct file
*file
, char *buf
, size_t nbytes
,
1617 struct comedi_subdevice
*s
;
1618 struct comedi_async
*async
;
1619 int n
, m
, count
= 0, retval
= 0;
1620 DECLARE_WAITQUEUE(wait
, current
);
1621 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1622 struct comedi_device_file_info
*dev_file_info
=
1623 comedi_get_device_file_info(minor
);
1624 struct comedi_device
*dev
= dev_file_info
->device
;
1626 if (!dev
->attached
) {
1627 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1632 s
= comedi_get_read_subdevice(dev_file_info
);
1646 if (s
->busy
!= file
) {
1651 add_wait_queue(&async
->wait_head
, &wait
);
1652 while (nbytes
> 0 && !retval
) {
1653 set_current_state(TASK_INTERRUPTIBLE
);
1657 m
= comedi_buf_read_n_available(async
);
1658 /* printk("%d available\n",m); */
1659 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
1660 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
1661 /* printk("%d contiguous\n",m); */
1666 if (!(comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)) {
1667 do_become_nonbusy(dev
, s
);
1668 if (comedi_get_subdevice_runflags(s
) &
1676 if (file
->f_flags
& O_NONBLOCK
) {
1680 if (signal_pending(current
)) {
1681 retval
= -ERESTARTSYS
;
1689 if (s
->busy
!= file
) {
1695 m
= copy_to_user(buf
, async
->prealloc_buf
+
1696 async
->buf_read_ptr
, n
);
1702 comedi_buf_read_alloc(async
, n
);
1703 comedi_buf_read_free(async
, n
);
1709 break; /* makes device work like a pipe */
1711 if (!(comedi_get_subdevice_runflags(s
) & (SRF_ERROR
| SRF_RUNNING
)) &&
1712 async
->buf_read_count
- async
->buf_write_count
== 0) {
1713 do_become_nonbusy(dev
, s
);
1715 set_current_state(TASK_RUNNING
);
1716 remove_wait_queue(&async
->wait_head
, &wait
);
1719 return count
? count
: retval
;
1723 This function restores a subdevice to an idle state.
1725 void do_become_nonbusy(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1727 struct comedi_async
*async
= s
->async
;
1729 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
1731 comedi_reset_async_buf(async
);
1732 async
->inttrig
= NULL
;
1735 "BUG: (?) do_become_nonbusy called with async=0\n");
1741 static int comedi_open(struct inode
*inode
, struct file
*file
)
1743 const unsigned minor
= iminor(inode
);
1744 struct comedi_device_file_info
*dev_file_info
=
1745 comedi_get_device_file_info(minor
);
1746 struct comedi_device
*dev
=
1747 dev_file_info
? dev_file_info
->device
: NULL
;
1750 DPRINTK("invalid minor number\n");
1754 /* This is slightly hacky, but we want module autoloading
1756 * case: user opens device, attached -> ok
1757 * case: user opens device, unattached, in_request_module=0 -> autoload
1758 * case: user opens device, unattached, in_request_module=1 -> fail
1759 * case: root opens device, attached -> ok
1760 * case: root opens device, unattached, in_request_module=1 -> ok
1761 * (typically called from modprobe)
1762 * case: root opens device, unattached, in_request_module=0 -> autoload
1764 * The last could be changed to "-> ok", which would deny root
1767 mutex_lock(&dev
->mutex
);
1770 if (!capable(CAP_NET_ADMIN
) && dev
->in_request_module
) {
1771 DPRINTK("in request module\n");
1772 mutex_unlock(&dev
->mutex
);
1775 if (capable(CAP_NET_ADMIN
) && dev
->in_request_module
)
1778 dev
->in_request_module
= 1;
1781 mutex_unlock(&dev
->mutex
);
1782 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
1783 mutex_lock(&dev
->mutex
);
1786 dev
->in_request_module
= 0;
1788 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
1789 DPRINTK("not attached and not CAP_NET_ADMIN\n");
1790 mutex_unlock(&dev
->mutex
);
1794 __module_get(THIS_MODULE
);
1796 if (dev
->attached
) {
1797 if (!try_module_get(dev
->driver
->module
)) {
1798 module_put(THIS_MODULE
);
1799 mutex_unlock(&dev
->mutex
);
1804 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
)
1809 mutex_unlock(&dev
->mutex
);
1814 static int comedi_close(struct inode
*inode
, struct file
*file
)
1816 const unsigned minor
= iminor(inode
);
1817 struct comedi_device_file_info
*dev_file_info
=
1818 comedi_get_device_file_info(minor
);
1819 struct comedi_device
*dev
= dev_file_info
->device
;
1820 struct comedi_subdevice
*s
= NULL
;
1823 mutex_lock(&dev
->mutex
);
1825 if (dev
->subdevices
) {
1826 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
1827 s
= dev
->subdevices
+ i
;
1829 if (s
->busy
== file
)
1831 if (s
->lock
== file
)
1835 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
1838 module_put(THIS_MODULE
);
1840 module_put(dev
->driver
->module
);
1844 mutex_unlock(&dev
->mutex
);
1846 if (file
->f_flags
& FASYNC
)
1847 comedi_fasync(-1, file
, 0);
1852 static int comedi_fasync(int fd
, struct file
*file
, int on
)
1854 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1855 struct comedi_device_file_info
*dev_file_info
=
1856 comedi_get_device_file_info(minor
);
1858 struct comedi_device
*dev
= dev_file_info
->device
;
1860 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
1863 const struct file_operations comedi_fops
= {
1864 .owner
= THIS_MODULE
,
1865 .unlocked_ioctl
= comedi_unlocked_ioctl
,
1866 .compat_ioctl
= comedi_compat_ioctl
,
1867 .open
= comedi_open
,
1868 .release
= comedi_close
,
1869 .read
= comedi_read
,
1870 .write
= comedi_write
,
1871 .mmap
= comedi_mmap
,
1872 .poll
= comedi_poll
,
1873 .fasync
= comedi_fasync
,
1876 struct class *comedi_class
;
1877 static struct cdev comedi_cdev
;
1879 static void comedi_cleanup_legacy_minors(void)
1883 for (i
= 0; i
< comedi_num_legacy_minors
; i
++)
1884 comedi_free_board_minor(i
);
1887 static int __init
comedi_init(void)
1892 printk(KERN_INFO
"comedi: version " COMEDI_RELEASE
1893 " - http://www.comedi.org\n");
1895 if (comedi_num_legacy_minors
< 0 ||
1896 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
1897 printk(KERN_ERR
"comedi: error: invalid value for module "
1898 "parameter \"comedi_num_legacy_minors\". Valid values "
1899 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS
);
1904 * comedi is unusable if both comedi_autoconfig and
1905 * comedi_num_legacy_minors are zero, so we might as well adjust the
1906 * defaults in that case
1908 if (comedi_autoconfig
== 0 && comedi_num_legacy_minors
== 0)
1909 comedi_num_legacy_minors
= 16;
1911 memset(comedi_file_info_table
, 0,
1912 sizeof(struct comedi_device_file_info
*) * COMEDI_NUM_MINORS
);
1914 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1915 COMEDI_NUM_MINORS
, "comedi");
1918 cdev_init(&comedi_cdev
, &comedi_fops
);
1919 comedi_cdev
.owner
= THIS_MODULE
;
1920 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
1921 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
1922 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1926 comedi_class
= class_create(THIS_MODULE
, "comedi");
1927 if (IS_ERR(comedi_class
)) {
1928 printk("comedi: failed to create class");
1929 cdev_del(&comedi_cdev
);
1930 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1932 return PTR_ERR(comedi_class
);
1935 /* XXX requires /proc interface */
1938 /* create devices files for legacy/manual use */
1939 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
1941 minor
= comedi_alloc_board_minor(NULL
);
1943 comedi_cleanup_legacy_minors();
1944 cdev_del(&comedi_cdev
);
1945 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1954 static void __exit
comedi_cleanup(void)
1958 comedi_cleanup_legacy_minors();
1959 for (i
= 0; i
< COMEDI_NUM_MINORS
; ++i
)
1960 BUG_ON(comedi_file_info_table
[i
]);
1962 class_destroy(comedi_class
);
1963 cdev_del(&comedi_cdev
);
1964 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
1966 comedi_proc_cleanup();
1969 module_init(comedi_init
);
1970 module_exit(comedi_cleanup
);
1972 void comedi_error(const struct comedi_device
*dev
, const char *s
)
1974 printk("comedi%d: %s: %s\n", dev
->minor
, dev
->driver
->driver_name
, s
);
1977 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1979 struct comedi_async
*async
= s
->async
;
1980 unsigned runflags
= 0;
1981 unsigned runflags_mask
= 0;
1983 /* DPRINTK("comedi_event 0x%x\n",mask); */
1985 if ((comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) == 0)
1989 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
1990 COMEDI_CB_OVERFLOW
)) {
1991 runflags_mask
|= SRF_RUNNING
;
1993 /* remember if an error event has occured, so an error
1994 * can be returned the next time the user does a read() */
1995 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
1996 runflags_mask
|= SRF_ERROR
;
1997 runflags
|= SRF_ERROR
;
1999 if (runflags_mask
) {
2000 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2001 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2004 if (async
->cb_mask
& s
->async
->events
) {
2005 if (comedi_get_subdevice_runflags(s
) & SRF_USER
) {
2006 wake_up_interruptible(&async
->wait_head
);
2007 if (s
->subdev_flags
& SDF_CMD_READ
) {
2008 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2010 if (s
->subdev_flags
& SDF_CMD_WRITE
) {
2011 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2015 async
->cb_func(s
->async
->events
, async
->cb_arg
);
2018 s
->async
->events
= 0;
2021 void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
, unsigned mask
,
2024 unsigned long flags
;
2026 spin_lock_irqsave(&s
->spin_lock
, flags
);
2027 s
->runflags
&= ~mask
;
2028 s
->runflags
|= (bits
& mask
);
2029 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2032 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
2034 unsigned long flags
;
2037 spin_lock_irqsave(&s
->spin_lock
, flags
);
2038 runflags
= s
->runflags
;
2039 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2043 static int is_device_busy(struct comedi_device
*dev
)
2045 struct comedi_subdevice
*s
;
2051 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2052 s
= dev
->subdevices
+ i
;
2055 if (s
->async
&& s
->async
->mmap_count
)
2062 void comedi_device_init(struct comedi_device
*dev
)
2064 memset(dev
, 0, sizeof(struct comedi_device
));
2065 spin_lock_init(&dev
->spinlock
);
2066 mutex_init(&dev
->mutex
);
2070 void comedi_device_cleanup(struct comedi_device
*dev
)
2074 mutex_lock(&dev
->mutex
);
2075 comedi_device_detach(dev
);
2076 mutex_unlock(&dev
->mutex
);
2077 mutex_destroy(&dev
->mutex
);
2080 int comedi_alloc_board_minor(struct device
*hardware_device
)
2082 unsigned long flags
;
2083 struct comedi_device_file_info
*info
;
2084 struct device
*csdev
;
2088 info
= kzalloc(sizeof(struct comedi_device_file_info
), GFP_KERNEL
);
2091 info
->device
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2092 if (info
->device
== NULL
) {
2096 comedi_device_init(info
->device
);
2097 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2098 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2099 if (comedi_file_info_table
[i
] == NULL
) {
2100 comedi_file_info_table
[i
] = info
;
2104 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2105 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2106 comedi_device_cleanup(info
->device
);
2107 kfree(info
->device
);
2110 "comedi: error: ran out of minor numbers for board device files.\n");
2113 info
->device
->minor
= i
;
2114 csdev
= COMEDI_DEVICE_CREATE(comedi_class
, NULL
,
2115 MKDEV(COMEDI_MAJOR
, i
), NULL
,
2116 hardware_device
, "comedi%i", i
);
2118 info
->device
->class_dev
= csdev
;
2119 dev_set_drvdata(csdev
, info
);
2120 retval
= device_create_file(csdev
, &dev_attr_max_read_buffer_kb
);
2123 "comedi: failed to create sysfs attribute file \"%s\".\n",
2124 dev_attr_max_read_buffer_kb
.attr
.name
);
2125 comedi_free_board_minor(i
);
2128 retval
= device_create_file(csdev
, &dev_attr_read_buffer_kb
);
2131 "comedi: failed to create sysfs attribute file \"%s\".\n",
2132 dev_attr_read_buffer_kb
.attr
.name
);
2133 comedi_free_board_minor(i
);
2136 retval
= device_create_file(csdev
, &dev_attr_max_write_buffer_kb
);
2139 "comedi: failed to create sysfs attribute file \"%s\".\n",
2140 dev_attr_max_write_buffer_kb
.attr
.name
);
2141 comedi_free_board_minor(i
);
2144 retval
= device_create_file(csdev
, &dev_attr_write_buffer_kb
);
2147 "comedi: failed to create sysfs attribute file \"%s\".\n",
2148 dev_attr_write_buffer_kb
.attr
.name
);
2149 comedi_free_board_minor(i
);
2155 void comedi_free_board_minor(unsigned minor
)
2157 unsigned long flags
;
2158 struct comedi_device_file_info
*info
;
2160 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2161 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2162 info
= comedi_file_info_table
[minor
];
2163 comedi_file_info_table
[minor
] = NULL
;
2164 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2167 struct comedi_device
*dev
= info
->device
;
2169 if (dev
->class_dev
) {
2170 device_destroy(comedi_class
,
2171 MKDEV(COMEDI_MAJOR
, dev
->minor
));
2173 comedi_device_cleanup(dev
);
2180 int comedi_alloc_subdevice_minor(struct comedi_device
*dev
,
2181 struct comedi_subdevice
*s
)
2183 unsigned long flags
;
2184 struct comedi_device_file_info
*info
;
2185 struct device
*csdev
;
2189 info
= kmalloc(sizeof(struct comedi_device_file_info
), GFP_KERNEL
);
2193 info
->read_subdevice
= s
;
2194 info
->write_subdevice
= s
;
2195 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2196 for (i
= COMEDI_FIRST_SUBDEVICE_MINOR
; i
< COMEDI_NUM_MINORS
; ++i
) {
2197 if (comedi_file_info_table
[i
] == NULL
) {
2198 comedi_file_info_table
[i
] = info
;
2202 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2203 if (i
== COMEDI_NUM_MINORS
) {
2206 "comedi: error: ran out of minor numbers for board device files.\n");
2210 csdev
= COMEDI_DEVICE_CREATE(comedi_class
, dev
->class_dev
,
2211 MKDEV(COMEDI_MAJOR
, i
), NULL
, NULL
,
2212 "comedi%i_subd%i", dev
->minor
,
2213 (int)(s
- dev
->subdevices
));
2215 s
->class_dev
= csdev
;
2216 dev_set_drvdata(csdev
, info
);
2217 retval
= device_create_file(csdev
, &dev_attr_max_read_buffer_kb
);
2220 "comedi: failed to create sysfs attribute file \"%s\".\n",
2221 dev_attr_max_read_buffer_kb
.attr
.name
);
2222 comedi_free_subdevice_minor(s
);
2225 retval
= device_create_file(csdev
, &dev_attr_read_buffer_kb
);
2228 "comedi: failed to create sysfs attribute file \"%s\".\n",
2229 dev_attr_read_buffer_kb
.attr
.name
);
2230 comedi_free_subdevice_minor(s
);
2233 retval
= device_create_file(csdev
, &dev_attr_max_write_buffer_kb
);
2236 "comedi: failed to create sysfs attribute file \"%s\".\n",
2237 dev_attr_max_write_buffer_kb
.attr
.name
);
2238 comedi_free_subdevice_minor(s
);
2241 retval
= device_create_file(csdev
, &dev_attr_write_buffer_kb
);
2244 "comedi: failed to create sysfs attribute file \"%s\".\n",
2245 dev_attr_write_buffer_kb
.attr
.name
);
2246 comedi_free_subdevice_minor(s
);
2252 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2254 unsigned long flags
;
2255 struct comedi_device_file_info
*info
;
2262 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2263 BUG_ON(s
->minor
< COMEDI_FIRST_SUBDEVICE_MINOR
);
2265 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2266 info
= comedi_file_info_table
[s
->minor
];
2267 comedi_file_info_table
[s
->minor
] = NULL
;
2268 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2271 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2272 s
->class_dev
= NULL
;
2277 struct comedi_device_file_info
*comedi_get_device_file_info(unsigned minor
)
2279 unsigned long flags
;
2280 struct comedi_device_file_info
*info
;
2282 BUG_ON(minor
>= COMEDI_NUM_MINORS
);
2283 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2284 info
= comedi_file_info_table
[minor
];
2285 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2289 static int resize_async_buffer(struct comedi_device
*dev
,
2290 struct comedi_subdevice
*s
,
2291 struct comedi_async
*async
, unsigned new_size
)
2295 if (new_size
> async
->max_bufsize
)
2299 DPRINTK("subdevice is busy, cannot resize buffer\n");
2302 if (async
->mmap_count
) {
2303 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2307 if (!async
->prealloc_buf
)
2310 /* make sure buffer is an integral number of pages
2312 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2314 retval
= comedi_buf_alloc(dev
, s
, new_size
);
2318 if (s
->buf_change
) {
2319 retval
= s
->buf_change(dev
, s
, new_size
);
2324 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2325 dev
->minor
, (int)(s
- dev
->subdevices
), async
->prealloc_bufsz
);
2329 /* sysfs attribute files */
2331 static const unsigned bytes_per_kibi
= 1024;
2333 static ssize_t
show_max_read_buffer_kb(struct device
*dev
,
2334 struct device_attribute
*attr
, char *buf
)
2337 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2338 unsigned max_buffer_size_kb
= 0;
2339 struct comedi_subdevice
*const read_subdevice
=
2340 comedi_get_read_subdevice(info
);
2342 mutex_lock(&info
->device
->mutex
);
2343 if (read_subdevice
&&
2344 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) &&
2345 read_subdevice
->async
) {
2346 max_buffer_size_kb
= read_subdevice
->async
->max_bufsize
/
2349 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", max_buffer_size_kb
);
2350 mutex_unlock(&info
->device
->mutex
);
2355 static ssize_t
store_max_read_buffer_kb(struct device
*dev
,
2356 struct device_attribute
*attr
,
2357 const char *buf
, size_t count
)
2359 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2360 unsigned long new_max_size_kb
;
2361 uint64_t new_max_size
;
2362 struct comedi_subdevice
*const read_subdevice
=
2363 comedi_get_read_subdevice(info
);
2365 if (strict_strtoul(buf
, 10, &new_max_size_kb
))
2367 if (new_max_size_kb
!= (uint32_t) new_max_size_kb
)
2369 new_max_size
= ((uint64_t) new_max_size_kb
) * bytes_per_kibi
;
2370 if (new_max_size
!= (uint32_t) new_max_size
)
2373 mutex_lock(&info
->device
->mutex
);
2374 if (read_subdevice
== NULL
||
2375 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) == 0 ||
2376 read_subdevice
->async
== NULL
) {
2377 mutex_unlock(&info
->device
->mutex
);
2380 read_subdevice
->async
->max_bufsize
= new_max_size
;
2381 mutex_unlock(&info
->device
->mutex
);
2386 static struct device_attribute dev_attr_max_read_buffer_kb
= {
2388 .name
= "max_read_buffer_kb",
2389 .mode
= S_IRUGO
| S_IWUSR
},
2390 .show
= &show_max_read_buffer_kb
,
2391 .store
= &store_max_read_buffer_kb
2394 static ssize_t
show_read_buffer_kb(struct device
*dev
,
2395 struct device_attribute
*attr
, char *buf
)
2398 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2399 unsigned buffer_size_kb
= 0;
2400 struct comedi_subdevice
*const read_subdevice
=
2401 comedi_get_read_subdevice(info
);
2403 mutex_lock(&info
->device
->mutex
);
2404 if (read_subdevice
&&
2405 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) &&
2406 read_subdevice
->async
) {
2407 buffer_size_kb
= read_subdevice
->async
->prealloc_bufsz
/
2410 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", buffer_size_kb
);
2411 mutex_unlock(&info
->device
->mutex
);
2416 static ssize_t
store_read_buffer_kb(struct device
*dev
,
2417 struct device_attribute
*attr
,
2418 const char *buf
, size_t count
)
2420 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2421 unsigned long new_size_kb
;
2424 struct comedi_subdevice
*const read_subdevice
=
2425 comedi_get_read_subdevice(info
);
2427 if (strict_strtoul(buf
, 10, &new_size_kb
))
2429 if (new_size_kb
!= (uint32_t) new_size_kb
)
2431 new_size
= ((uint64_t) new_size_kb
) * bytes_per_kibi
;
2432 if (new_size
!= (uint32_t) new_size
)
2435 mutex_lock(&info
->device
->mutex
);
2436 if (read_subdevice
== NULL
||
2437 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) == 0 ||
2438 read_subdevice
->async
== NULL
) {
2439 mutex_unlock(&info
->device
->mutex
);
2442 retval
= resize_async_buffer(info
->device
, read_subdevice
,
2443 read_subdevice
->async
, new_size
);
2444 mutex_unlock(&info
->device
->mutex
);
2451 static struct device_attribute dev_attr_read_buffer_kb
= {
2453 .name
= "read_buffer_kb",
2454 .mode
= S_IRUGO
| S_IWUSR
| S_IWGRP
},
2455 .show
= &show_read_buffer_kb
,
2456 .store
= &store_read_buffer_kb
2459 static ssize_t
show_max_write_buffer_kb(struct device
*dev
,
2460 struct device_attribute
*attr
,
2464 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2465 unsigned max_buffer_size_kb
= 0;
2466 struct comedi_subdevice
*const write_subdevice
=
2467 comedi_get_write_subdevice(info
);
2469 mutex_lock(&info
->device
->mutex
);
2470 if (write_subdevice
&&
2471 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) &&
2472 write_subdevice
->async
) {
2473 max_buffer_size_kb
= write_subdevice
->async
->max_bufsize
/
2476 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", max_buffer_size_kb
);
2477 mutex_unlock(&info
->device
->mutex
);
2482 static ssize_t
store_max_write_buffer_kb(struct device
*dev
,
2483 struct device_attribute
*attr
,
2484 const char *buf
, size_t count
)
2486 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2487 unsigned long new_max_size_kb
;
2488 uint64_t new_max_size
;
2489 struct comedi_subdevice
*const write_subdevice
=
2490 comedi_get_write_subdevice(info
);
2492 if (strict_strtoul(buf
, 10, &new_max_size_kb
))
2494 if (new_max_size_kb
!= (uint32_t) new_max_size_kb
)
2496 new_max_size
= ((uint64_t) new_max_size_kb
) * bytes_per_kibi
;
2497 if (new_max_size
!= (uint32_t) new_max_size
)
2500 mutex_lock(&info
->device
->mutex
);
2501 if (write_subdevice
== NULL
||
2502 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) == 0 ||
2503 write_subdevice
->async
== NULL
) {
2504 mutex_unlock(&info
->device
->mutex
);
2507 write_subdevice
->async
->max_bufsize
= new_max_size
;
2508 mutex_unlock(&info
->device
->mutex
);
2513 static struct device_attribute dev_attr_max_write_buffer_kb
= {
2515 .name
= "max_write_buffer_kb",
2516 .mode
= S_IRUGO
| S_IWUSR
},
2517 .show
= &show_max_write_buffer_kb
,
2518 .store
= &store_max_write_buffer_kb
2521 static ssize_t
show_write_buffer_kb(struct device
*dev
,
2522 struct device_attribute
*attr
, char *buf
)
2525 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2526 unsigned buffer_size_kb
= 0;
2527 struct comedi_subdevice
*const write_subdevice
=
2528 comedi_get_write_subdevice(info
);
2530 mutex_lock(&info
->device
->mutex
);
2531 if (write_subdevice
&&
2532 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) &&
2533 write_subdevice
->async
) {
2534 buffer_size_kb
= write_subdevice
->async
->prealloc_bufsz
/
2537 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", buffer_size_kb
);
2538 mutex_unlock(&info
->device
->mutex
);
2543 static ssize_t
store_write_buffer_kb(struct device
*dev
,
2544 struct device_attribute
*attr
,
2545 const char *buf
, size_t count
)
2547 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2548 unsigned long new_size_kb
;
2551 struct comedi_subdevice
*const write_subdevice
=
2552 comedi_get_write_subdevice(info
);
2554 if (strict_strtoul(buf
, 10, &new_size_kb
))
2556 if (new_size_kb
!= (uint32_t) new_size_kb
)
2558 new_size
= ((uint64_t) new_size_kb
) * bytes_per_kibi
;
2559 if (new_size
!= (uint32_t) new_size
)
2562 mutex_lock(&info
->device
->mutex
);
2563 if (write_subdevice
== NULL
||
2564 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) == 0 ||
2565 write_subdevice
->async
== NULL
) {
2566 mutex_unlock(&info
->device
->mutex
);
2569 retval
= resize_async_buffer(info
->device
, write_subdevice
,
2570 write_subdevice
->async
, new_size
);
2571 mutex_unlock(&info
->device
->mutex
);
2578 static struct device_attribute dev_attr_write_buffer_kb
= {
2580 .name
= "write_buffer_kb",
2581 .mode
= S_IRUGO
| S_IWUSR
| S_IWGRP
},
2582 .show
= &show_write_buffer_kb
,
2583 .store
= &store_write_buffer_kb