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
, struct comedi_devconfig
*arg
);
74 static int do_bufconfig_ioctl(struct comedi_device
*dev
, void *arg
);
75 static int do_devinfo_ioctl(struct comedi_device
*dev
, struct comedi_devinfo
*arg
,
77 static int do_subdinfo_ioctl(struct comedi_device
*dev
, struct comedi_subdinfo
*arg
,
79 static int do_chaninfo_ioctl(struct comedi_device
*dev
, struct comedi_chaninfo
*arg
);
80 static int do_bufinfo_ioctl(struct comedi_device
*dev
, void *arg
);
81 static int do_cmd_ioctl(struct comedi_device
*dev
, void *arg
, void *file
);
82 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
, void *file
);
83 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
, void *file
);
84 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
, void *file
);
85 static int do_cmdtest_ioctl(struct comedi_device
*dev
, void *arg
, void *file
);
86 static int do_insnlist_ioctl(struct comedi_device
*dev
, void *arg
, void *file
);
87 static int do_insn_ioctl(struct comedi_device
*dev
, void *arg
, void *file
);
88 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int subd
, void *file
);
90 extern void do_become_nonbusy(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
91 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
93 static int comedi_fasync(int fd
, struct file
*file
, int on
);
95 static int is_device_busy(struct comedi_device
*dev
);
96 static int resize_async_buffer(struct comedi_device
*dev
,
97 struct comedi_subdevice
*s
,
98 struct comedi_async
*async
, unsigned new_size
);
100 /* declarations for sysfs attribute files */
101 static struct device_attribute dev_attr_max_read_buffer_kb
;
102 static struct device_attribute dev_attr_read_buffer_kb
;
103 static struct device_attribute dev_attr_max_write_buffer_kb
;
104 static struct device_attribute dev_attr_write_buffer_kb
;
106 #ifdef HAVE_UNLOCKED_IOCTL
107 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
110 static int comedi_ioctl(struct inode
*inode
, struct file
*file
,
111 unsigned int cmd
, unsigned long arg
)
114 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
115 struct comedi_device_file_info
*dev_file_info
=
116 comedi_get_device_file_info(minor
);
117 struct comedi_device
*dev
;
120 if (dev_file_info
== NULL
|| dev_file_info
->device
== NULL
)
122 dev
= dev_file_info
->device
;
124 mutex_lock(&dev
->mutex
);
126 /* Device config is special, because it must work on
127 * an unconfigured device. */
128 if (cmd
== COMEDI_DEVCONFIG
) {
129 rc
= do_devconfig_ioctl(dev
, (void *)arg
);
133 if (!dev
->attached
) {
134 DPRINTK("no driver configured on /dev/comedi%i\n", dev
->minor
);
140 case COMEDI_BUFCONFIG
:
141 rc
= do_bufconfig_ioctl(dev
, (void *)arg
);
144 rc
= do_devinfo_ioctl(dev
, (void *)arg
, file
);
146 case COMEDI_SUBDINFO
:
147 rc
= do_subdinfo_ioctl(dev
, (void *)arg
, file
);
149 case COMEDI_CHANINFO
:
150 rc
= do_chaninfo_ioctl(dev
, (void *)arg
);
152 case COMEDI_RANGEINFO
:
153 rc
= do_rangeinfo_ioctl(dev
, (void *)arg
);
156 rc
= do_bufinfo_ioctl(dev
, (void *)arg
);
159 rc
= do_lock_ioctl(dev
, arg
, file
);
162 rc
= do_unlock_ioctl(dev
, arg
, file
);
165 rc
= do_cancel_ioctl(dev
, arg
, file
);
168 rc
= do_cmd_ioctl(dev
, (void *)arg
, file
);
171 rc
= do_cmdtest_ioctl(dev
, (void *)arg
, file
);
173 case COMEDI_INSNLIST
:
174 rc
= do_insnlist_ioctl(dev
, (void *)arg
, file
);
177 rc
= do_insn_ioctl(dev
, (void *)arg
, file
);
180 rc
= do_poll_ioctl(dev
, arg
, file
);
188 mutex_unlock(&dev
->mutex
);
197 pointer to devconfig structure
200 devconfig structure at arg
205 static int do_devconfig_ioctl(struct comedi_device
*dev
, struct comedi_devconfig
*arg
)
207 struct comedi_devconfig it
;
209 unsigned char *aux_data
= NULL
;
212 if (!capable(CAP_SYS_ADMIN
))
216 if (is_device_busy(dev
))
219 struct module
*driver_module
= dev
->driver
->module
;
220 comedi_device_detach(dev
);
221 module_put(driver_module
);
226 if (copy_from_user(&it
, arg
, sizeof(struct comedi_devconfig
)))
229 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
231 if (comedi_aux_data(it
.options
, 0) &&
232 it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
234 aux_len
= it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
];
238 aux_data
= vmalloc(aux_len
);
242 if (copy_from_user(aux_data
,
243 comedi_aux_data(it
.options
, 0), aux_len
)) {
247 it
.options
[COMEDI_DEVCONF_AUX_DATA_LO
] =
248 (unsigned long)aux_data
;
249 if (sizeof(void *) > sizeof(int)) {
250 bit_shift
= sizeof(int) * 8;
251 it
.options
[COMEDI_DEVCONF_AUX_DATA_HI
] =
252 ((unsigned long)aux_data
) >> bit_shift
;
254 it
.options
[COMEDI_DEVCONF_AUX_DATA_HI
] = 0;
257 ret
= comedi_device_attach(dev
, &it
);
259 if (!try_module_get(dev
->driver
->module
)) {
260 comedi_device_detach(dev
);
273 buffer configuration ioctl
276 pointer to bufconfig structure
282 modified bufconfig at arg
285 static int do_bufconfig_ioctl(struct comedi_device
*dev
, void *arg
)
287 struct comedi_bufconfig bc
;
288 struct comedi_async
*async
;
289 struct comedi_subdevice
*s
;
292 if (copy_from_user(&bc
, arg
, sizeof(struct comedi_bufconfig
)))
295 if (bc
.subdevice
>= dev
->n_subdevices
|| bc
.subdevice
< 0)
298 s
= dev
->subdevices
+ bc
.subdevice
;
302 DPRINTK("subdevice does not have async capability\n");
308 if (bc
.maximum_size
) {
309 if (!capable(CAP_SYS_ADMIN
))
312 async
->max_bufsize
= bc
.maximum_size
;
316 retval
= resize_async_buffer(dev
, s
, async
, bc
.size
);
321 bc
.size
= async
->prealloc_bufsz
;
322 bc
.maximum_size
= async
->max_bufsize
;
325 if (copy_to_user(arg
, &bc
, sizeof(struct comedi_bufconfig
)))
336 pointer to devinfo structure
345 static int do_devinfo_ioctl(struct comedi_device
*dev
, struct comedi_devinfo
*arg
,
348 struct comedi_devinfo devinfo
;
349 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
350 struct comedi_device_file_info
*dev_file_info
=
351 comedi_get_device_file_info(minor
);
352 struct comedi_subdevice
*read_subdev
=
353 comedi_get_read_subdevice(dev_file_info
);
354 struct comedi_subdevice
*write_subdev
=
355 comedi_get_write_subdevice(dev_file_info
);
357 memset(&devinfo
, 0, sizeof(devinfo
));
359 /* fill devinfo structure */
360 devinfo
.version_code
= COMEDI_VERSION_CODE
;
361 devinfo
.n_subdevs
= dev
->n_subdevices
;
362 memcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
363 memcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
366 devinfo
.read_subdevice
= read_subdev
- dev
->subdevices
;
368 devinfo
.read_subdevice
= -1;
371 devinfo
.write_subdevice
= write_subdev
- dev
->subdevices
;
373 devinfo
.write_subdevice
= -1;
375 if (copy_to_user(arg
, &devinfo
, sizeof(struct comedi_devinfo
)))
386 pointer to array of subdevice info structures
392 array of subdevice info structures at arg
395 static int do_subdinfo_ioctl(struct comedi_device
*dev
, struct comedi_subdinfo
*arg
,
399 struct comedi_subdinfo
*tmp
, *us
;
400 struct comedi_subdevice
*s
;
402 tmp
= kcalloc(dev
->n_subdevices
, sizeof(struct comedi_subdinfo
), GFP_KERNEL
);
406 /* fill subdinfo structs */
407 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
408 s
= dev
->subdevices
+ i
;
412 us
->n_chan
= s
->n_chan
;
413 us
->subd_flags
= s
->subdev_flags
;
414 if (comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)
415 us
->subd_flags
|= SDF_RUNNING
;
416 #define TIMER_nanosec 5 /* backwards compatibility */
417 us
->timer_type
= TIMER_nanosec
;
418 us
->len_chanlist
= s
->len_chanlist
;
419 us
->maxdata
= s
->maxdata
;
420 if (s
->range_table
) {
422 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
424 us
->range_type
= 0; /* XXX */
426 us
->flags
= s
->flags
;
429 us
->subd_flags
|= SDF_BUSY
;
431 us
->subd_flags
|= SDF_BUSY_OWNER
;
433 us
->subd_flags
|= SDF_LOCKED
;
435 us
->subd_flags
|= SDF_LOCK_OWNER
;
436 if (!s
->maxdata
&& s
->maxdata_list
)
437 us
->subd_flags
|= SDF_MAXDATA
;
439 us
->subd_flags
|= SDF_FLAGS
;
440 if (s
->range_table_list
)
441 us
->subd_flags
|= SDF_RANGETYPE
;
443 us
->subd_flags
|= SDF_CMD
;
445 if (s
->insn_bits
!= &insn_inval
)
446 us
->insn_bits_support
= COMEDI_SUPPORTED
;
448 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
450 us
->settling_time_0
= s
->settling_time_0
;
453 ret
= copy_to_user(arg
, tmp
,
454 dev
->n_subdevices
* sizeof(struct comedi_subdinfo
));
458 return ret
? -EFAULT
: 0;
466 pointer to chaninfo structure
469 chaninfo structure at arg
472 arrays at elements of chaninfo structure
475 static int do_chaninfo_ioctl(struct comedi_device
*dev
, struct comedi_chaninfo
*arg
)
477 struct comedi_subdevice
*s
;
478 struct comedi_chaninfo it
;
480 if (copy_from_user(&it
, arg
, sizeof(struct comedi_chaninfo
)))
483 if (it
.subdev
>= dev
->n_subdevices
)
485 s
= dev
->subdevices
+ it
.subdev
;
487 if (it
.maxdata_list
) {
488 if (s
->maxdata
|| !s
->maxdata_list
)
490 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
491 s
->n_chan
* sizeof(unsigned int)))
498 if (copy_to_user(it
.flaglist
, s
->flaglist
,
499 s
->n_chan
* sizeof(unsigned int)))
506 if (!s
->range_table_list
)
508 for (i
= 0; i
< s
->n_chan
; i
++) {
511 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
512 (s
->range_table_list
[i
]->length
);
513 put_user(x
, it
.rangelist
+ i
);
516 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
517 s
->n_chan
*sizeof(unsigned int)))
527 buffer information ioctl
530 pointer to bufinfo structure
536 modified bufinfo at arg
539 static int do_bufinfo_ioctl(struct comedi_device
*dev
, void *arg
)
541 struct comedi_bufinfo bi
;
542 struct comedi_subdevice
*s
;
543 struct comedi_async
*async
;
545 if (copy_from_user(&bi
, arg
, sizeof(struct comedi_bufinfo
)))
548 if (bi
.subdevice
>= dev
->n_subdevices
|| bi
.subdevice
< 0)
551 s
= dev
->subdevices
+ bi
.subdevice
;
555 DPRINTK("subdevice does not have async capability\n");
556 bi
.buf_write_ptr
= 0;
558 bi
.buf_write_count
= 0;
559 bi
.buf_read_count
= 0;
563 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
564 bi
.bytes_read
= comedi_buf_read_alloc(async
, bi
.bytes_read
);
565 comedi_buf_read_free(async
, bi
.bytes_read
);
567 if (!(comedi_get_subdevice_runflags(s
) & (SRF_ERROR
|
569 && async
->buf_write_count
== async
->buf_read_count
) {
570 do_become_nonbusy(dev
, s
);
574 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
576 comedi_buf_write_alloc(async
, bi
.bytes_written
);
577 comedi_buf_write_free(async
, bi
.bytes_written
);
580 bi
.buf_write_count
= async
->buf_write_count
;
581 bi
.buf_write_ptr
= async
->buf_write_ptr
;
582 bi
.buf_read_count
= async
->buf_read_count
;
583 bi
.buf_read_ptr
= async
->buf_read_ptr
;
586 if (copy_to_user(arg
, &bi
, sizeof(struct comedi_bufinfo
)))
592 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
, unsigned int *data
,
596 * synchronous instructions
599 * pointer to sync cmd structure
602 * sync cmd struct at arg
609 /* arbitrary limits */
610 #define MAX_SAMPLES 256
611 static int do_insnlist_ioctl(struct comedi_device
*dev
, void *arg
, void *file
)
613 struct comedi_insnlist insnlist
;
614 struct comedi_insn
*insns
= NULL
;
615 unsigned int *data
= NULL
;
619 if (copy_from_user(&insnlist
, arg
, sizeof(struct comedi_insnlist
)))
622 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
624 DPRINTK("kmalloc failed\n");
629 insns
= kmalloc(sizeof(struct comedi_insn
) * insnlist
.n_insns
, GFP_KERNEL
);
631 DPRINTK("kmalloc failed\n");
636 if (copy_from_user(insns
, insnlist
.insns
,
637 sizeof(struct comedi_insn
) * insnlist
.n_insns
)) {
638 DPRINTK("copy_from_user failed\n");
643 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
644 if (insns
[i
].n
> MAX_SAMPLES
) {
645 DPRINTK("number of samples too large\n");
649 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
650 if (copy_from_user(data
, insns
[i
].data
,
651 insns
[i
].n
* sizeof(unsigned int))) {
652 DPRINTK("copy_from_user failed\n");
657 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
660 if (insns
[i
].insn
& INSN_MASK_READ
) {
661 if (copy_to_user(insns
[i
].data
, data
,
662 insns
[i
].n
* sizeof(unsigned int))) {
663 DPRINTK("copy_to_user failed\n");
681 static int check_insn_config_length(struct comedi_insn
*insn
, unsigned int *data
)
687 case INSN_CONFIG_DIO_OUTPUT
:
688 case INSN_CONFIG_DIO_INPUT
:
689 case INSN_CONFIG_DISARM
:
690 case INSN_CONFIG_RESET
:
694 case INSN_CONFIG_ARM
:
695 case INSN_CONFIG_DIO_QUERY
:
696 case INSN_CONFIG_BLOCK_SIZE
:
697 case INSN_CONFIG_FILTER
:
698 case INSN_CONFIG_SERIAL_CLOCK
:
699 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
700 case INSN_CONFIG_ALT_SOURCE
:
701 case INSN_CONFIG_SET_COUNTER_MODE
:
702 case INSN_CONFIG_8254_READ_STATUS
:
703 case INSN_CONFIG_SET_ROUTING
:
704 case INSN_CONFIG_GET_ROUTING
:
705 case INSN_CONFIG_GET_PWM_STATUS
:
706 case INSN_CONFIG_PWM_SET_PERIOD
:
707 case INSN_CONFIG_PWM_GET_PERIOD
:
711 case INSN_CONFIG_SET_GATE_SRC
:
712 case INSN_CONFIG_GET_GATE_SRC
:
713 case INSN_CONFIG_SET_CLOCK_SRC
:
714 case INSN_CONFIG_GET_CLOCK_SRC
:
715 case INSN_CONFIG_SET_OTHER_SRC
:
716 case INSN_CONFIG_GET_COUNTER_STATUS
:
717 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
718 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
719 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
723 case INSN_CONFIG_PWM_OUTPUT
:
724 case INSN_CONFIG_ANALOG_TRIG
:
728 /* by default we allow the insn since we don't have checks for
729 * all possible cases yet */
731 printk("comedi: no check for data length of config insn id "
732 "%i is implemented.\n"
733 " Add a check to %s in %s.\n"
734 " Assuming n=%i is correct.\n", data
[0], __func__
,
742 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
, unsigned int *data
,
745 struct comedi_subdevice
*s
;
749 if (insn
->insn
& INSN_MASK_SPECIAL
) {
750 /* a non-subdevice instruction */
752 switch (insn
->insn
) {
762 do_gettimeofday(&tv
);
764 data
[1] = tv
.tv_usec
;
770 if (insn
->n
!= 1 || data
[0] >= 100000) {
774 udelay(data
[0] / 1000);
782 if (insn
->subdev
>= dev
->n_subdevices
) {
783 DPRINTK("%d not usable subdevice\n",
788 s
= dev
->subdevices
+ insn
->subdev
;
790 DPRINTK("no async\n");
794 if (!s
->async
->inttrig
) {
795 DPRINTK("no inttrig\n");
799 ret
= s
->async
->inttrig(dev
, s
, insn
->data
[0]);
804 DPRINTK("invalid insn\n");
809 /* a subdevice instruction */
810 unsigned int maxdata
;
812 if (insn
->subdev
>= dev
->n_subdevices
) {
813 DPRINTK("subdevice %d out of range\n", insn
->subdev
);
817 s
= dev
->subdevices
+ insn
->subdev
;
819 if (s
->type
== COMEDI_SUBD_UNUSED
) {
820 DPRINTK("%d not usable subdevice\n", insn
->subdev
);
825 /* are we locked? (ioctl lock) */
826 if (s
->lock
&& s
->lock
!= file
) {
827 DPRINTK("device locked\n");
832 ret
= check_chanlist(s
, 1, &insn
->chanspec
);
835 DPRINTK("bad chanspec\n");
843 /* This looks arbitrary. It is. */
844 s
->busy
= &parse_insn
;
845 switch (insn
->insn
) {
847 ret
= s
->insn_read(dev
, s
, insn
, data
);
850 maxdata
= s
->maxdata_list
851 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
853 for (i
= 0; i
< insn
->n
; ++i
) {
854 if (data
[i
] > maxdata
) {
856 DPRINTK("bad data value(s)\n");
861 ret
= s
->insn_write(dev
, s
, insn
, data
);
868 ret
= s
->insn_bits(dev
, s
, insn
, data
);
871 ret
= check_insn_config_length(insn
, data
);
874 ret
= s
->insn_config(dev
, s
, insn
, data
);
890 * synchronous instructions
896 * struct comedi_insn struct at arg
902 static int do_insn_ioctl(struct comedi_device
*dev
, void *arg
, void *file
)
904 struct comedi_insn insn
;
905 unsigned int *data
= NULL
;
908 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
914 if (copy_from_user(&insn
, arg
, sizeof(struct comedi_insn
))) {
919 /* This is where the behavior of insn and insnlist deviate. */
920 if (insn
.n
> MAX_SAMPLES
)
921 insn
.n
= MAX_SAMPLES
;
922 if (insn
.insn
& INSN_MASK_WRITE
) {
923 if (copy_from_user(data
, insn
.data
, insn
.n
* sizeof(unsigned int))) {
928 ret
= parse_insn(dev
, &insn
, data
, file
);
931 if (insn
.insn
& INSN_MASK_READ
) {
932 if (copy_to_user(insn
.data
, data
, insn
.n
* sizeof(unsigned int))) {
950 pointer to cmd structure
957 modified cmd structure at arg
960 static int do_cmd_ioctl(struct comedi_device
*dev
, void *arg
, void *file
)
962 struct comedi_cmd user_cmd
;
963 struct comedi_subdevice
*s
;
964 struct comedi_async
*async
;
966 unsigned int *chanlist_saver
= NULL
;
968 if (copy_from_user(&user_cmd
, arg
, sizeof(struct comedi_cmd
))) {
969 DPRINTK("bad cmd address\n");
972 /* save user's chanlist pointer so it can be restored later */
973 chanlist_saver
= user_cmd
.chanlist
;
975 if (user_cmd
.subdev
>= dev
->n_subdevices
) {
976 DPRINTK("%d no such subdevice\n", user_cmd
.subdev
);
980 s
= dev
->subdevices
+ user_cmd
.subdev
;
983 if (s
->type
== COMEDI_SUBD_UNUSED
) {
984 DPRINTK("%d not valid subdevice\n", user_cmd
.subdev
);
988 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
989 DPRINTK("subdevice %i does not support commands\n",
994 /* are we locked? (ioctl lock) */
995 if (s
->lock
&& s
->lock
!= file
) {
996 DPRINTK("subdevice locked\n");
1002 DPRINTK("subdevice busy\n");
1007 /* make sure channel/gain list isn't too long */
1008 if (user_cmd
.chanlist_len
> s
->len_chanlist
) {
1009 DPRINTK("channel/gain list too long %u > %d\n",
1010 user_cmd
.chanlist_len
, s
->len_chanlist
);
1015 /* make sure channel/gain list isn't too short */
1016 if (user_cmd
.chanlist_len
< 1) {
1017 DPRINTK("channel/gain list too short %u < 1\n",
1018 user_cmd
.chanlist_len
);
1023 kfree(async
->cmd
.chanlist
);
1024 async
->cmd
= user_cmd
;
1025 async
->cmd
.data
= NULL
;
1026 /* load channel/gain list */
1027 async
->cmd
.chanlist
=
1028 kmalloc(async
->cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1029 if (!async
->cmd
.chanlist
) {
1030 DPRINTK("allocation failed\n");
1035 if (copy_from_user(async
->cmd
.chanlist
, user_cmd
.chanlist
,
1036 async
->cmd
.chanlist_len
* sizeof(int))) {
1037 DPRINTK("fault reading chanlist\n");
1042 /* make sure each element in channel/gain list is valid */
1043 ret
= check_chanlist(s
, async
->cmd
.chanlist_len
, async
->cmd
.chanlist
);
1045 DPRINTK("bad chanlist\n");
1049 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1051 if (async
->cmd
.flags
& TRIG_BOGUS
|| ret
) {
1052 DPRINTK("test returned %d\n", ret
);
1053 user_cmd
= async
->cmd
;
1054 /* restore chanlist pointer before copying back */
1055 user_cmd
.chanlist
= chanlist_saver
;
1056 user_cmd
.data
= NULL
;
1057 if (copy_to_user(arg
, &user_cmd
, sizeof(struct comedi_cmd
))) {
1058 DPRINTK("fault writing cmd\n");
1066 if (!async
->prealloc_bufsz
) {
1068 DPRINTK("no buffer (?)\n");
1072 comedi_reset_async_buf(async
);
1075 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1077 if (async
->cmd
.flags
& TRIG_WAKE_EOS
)
1078 async
->cb_mask
|= COMEDI_CB_EOS
;
1080 comedi_set_subdevice_runflags(s
, ~0, SRF_USER
| SRF_RUNNING
);
1082 #ifdef CONFIG_COMEDI_RT
1083 if (async
->cmd
.flags
& TRIG_RT
) {
1084 if (comedi_switch_to_rt(dev
) == 0)
1085 comedi_set_subdevice_runflags(s
, SRF_RT
, SRF_RT
);
1089 ret
= s
->do_cmd(dev
, s
);
1094 do_become_nonbusy(dev
, s
);
1101 command testing ioctl
1104 pointer to cmd structure
1107 cmd structure at arg
1111 modified cmd structure at arg
1114 static int do_cmdtest_ioctl(struct comedi_device
*dev
, void *arg
, void *file
)
1116 struct comedi_cmd user_cmd
;
1117 struct comedi_subdevice
*s
;
1119 unsigned int *chanlist
= NULL
;
1120 unsigned int *chanlist_saver
= NULL
;
1122 if (copy_from_user(&user_cmd
, arg
, sizeof(struct comedi_cmd
))) {
1123 DPRINTK("bad cmd address\n");
1126 /* save user's chanlist pointer so it can be restored later */
1127 chanlist_saver
= user_cmd
.chanlist
;
1129 if (user_cmd
.subdev
>= dev
->n_subdevices
) {
1130 DPRINTK("%d no such subdevice\n", user_cmd
.subdev
);
1134 s
= dev
->subdevices
+ user_cmd
.subdev
;
1135 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1136 DPRINTK("%d not valid subdevice\n", user_cmd
.subdev
);
1140 if (!s
->do_cmd
|| !s
->do_cmdtest
) {
1141 DPRINTK("subdevice %i does not support commands\n",
1146 /* make sure channel/gain list isn't too long */
1147 if (user_cmd
.chanlist_len
> s
->len_chanlist
) {
1148 DPRINTK("channel/gain list too long %d > %d\n",
1149 user_cmd
.chanlist_len
, s
->len_chanlist
);
1154 /* load channel/gain list */
1155 if (user_cmd
.chanlist
) {
1157 kmalloc(user_cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1159 DPRINTK("allocation failed\n");
1164 if (copy_from_user(chanlist
, user_cmd
.chanlist
,
1165 user_cmd
.chanlist_len
* sizeof(int))) {
1166 DPRINTK("fault reading chanlist\n");
1171 /* make sure each element in channel/gain list is valid */
1172 ret
= check_chanlist(s
, user_cmd
.chanlist_len
, chanlist
);
1174 DPRINTK("bad chanlist\n");
1178 user_cmd
.chanlist
= chanlist
;
1181 ret
= s
->do_cmdtest(dev
, s
, &user_cmd
);
1183 /* restore chanlist pointer before copying back */
1184 user_cmd
.chanlist
= chanlist_saver
;
1186 if (copy_to_user(arg
, &user_cmd
, sizeof(struct comedi_cmd
))) {
1187 DPRINTK("bad cmd address\n");
1212 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
, void *file
)
1215 unsigned long flags
;
1216 struct comedi_subdevice
*s
;
1218 if (arg
>= dev
->n_subdevices
)
1220 s
= dev
->subdevices
+ arg
;
1222 spin_lock_irqsave(&s
->spin_lock
, flags
);
1223 if (s
->busy
|| s
->lock
)
1227 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1234 ret
= s
->lock_f(dev
, s
);
1253 This function isn't protected by the semaphore, since
1254 we already own the lock.
1256 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
, void *file
)
1258 struct comedi_subdevice
*s
;
1260 if (arg
>= dev
->n_subdevices
)
1262 s
= dev
->subdevices
+ arg
;
1267 if (s
->lock
&& s
->lock
!= file
)
1270 if (s
->lock
== file
) {
1284 cancel acquisition ioctl
1296 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
, void *file
)
1298 struct comedi_subdevice
*s
;
1300 if (arg
>= dev
->n_subdevices
)
1302 s
= dev
->subdevices
+ arg
;
1303 if (s
->async
== NULL
)
1306 if (s
->lock
&& s
->lock
!= file
)
1312 if (s
->busy
!= file
)
1315 return do_cancel(dev
, s
);
1320 instructs driver to synchronize buffers
1332 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int arg
, void *file
)
1334 struct comedi_subdevice
*s
;
1336 if (arg
>= dev
->n_subdevices
)
1338 s
= dev
->subdevices
+ arg
;
1340 if (s
->lock
&& s
->lock
!= file
)
1346 if (s
->busy
!= file
)
1350 return s
->poll(dev
, s
);
1355 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1359 if ((comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) && s
->cancel
)
1360 ret
= s
->cancel(dev
, s
);
1362 do_become_nonbusy(dev
, s
);
1367 void comedi_unmap(struct vm_area_struct
*area
)
1369 struct comedi_async
*async
;
1370 struct comedi_device
*dev
;
1372 async
= area
->vm_private_data
;
1373 dev
= async
->subdevice
->device
;
1375 mutex_lock(&dev
->mutex
);
1376 async
->mmap_count
--;
1377 mutex_unlock(&dev
->mutex
);
1380 static struct vm_operations_struct comedi_vm_ops
= {
1381 .close
= comedi_unmap
,
1384 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1386 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1387 struct comedi_device_file_info
*dev_file_info
=
1388 comedi_get_device_file_info(minor
);
1389 struct comedi_device
*dev
= dev_file_info
->device
;
1390 struct comedi_async
*async
= NULL
;
1391 unsigned long start
= vma
->vm_start
;
1396 struct comedi_subdevice
*s
;
1398 mutex_lock(&dev
->mutex
);
1399 if (!dev
->attached
) {
1400 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1404 if (vma
->vm_flags
& VM_WRITE
)
1405 s
= comedi_get_write_subdevice(dev_file_info
);
1407 s
= comedi_get_read_subdevice(dev_file_info
);
1414 if (async
== NULL
) {
1419 if (vma
->vm_pgoff
!= 0) {
1420 DPRINTK("comedi: mmap() offset must be 0.\n");
1425 size
= vma
->vm_end
- vma
->vm_start
;
1426 if (size
> async
->prealloc_bufsz
) {
1430 if (size
& (~PAGE_MASK
)) {
1435 n_pages
= size
>> PAGE_SHIFT
;
1436 for (i
= 0; i
< n_pages
; ++i
) {
1437 if (remap_pfn_range(vma
, start
,
1438 page_to_pfn(virt_to_page(async
->
1441 PAGE_SIZE
, PAGE_SHARED
)) {
1448 vma
->vm_ops
= &comedi_vm_ops
;
1449 vma
->vm_private_data
= async
;
1451 async
->mmap_count
++;
1455 mutex_unlock(&dev
->mutex
);
1459 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
1461 unsigned int mask
= 0;
1462 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1463 struct comedi_device_file_info
*dev_file_info
=
1464 comedi_get_device_file_info(minor
);
1465 struct comedi_device
*dev
= dev_file_info
->device
;
1466 struct comedi_subdevice
*read_subdev
;
1467 struct comedi_subdevice
*write_subdev
;
1469 mutex_lock(&dev
->mutex
);
1470 if (!dev
->attached
) {
1471 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1472 mutex_unlock(&dev
->mutex
);
1477 read_subdev
= comedi_get_read_subdevice(dev_file_info
);
1479 poll_wait(file
, &read_subdev
->async
->wait_head
, wait
);
1480 if (!read_subdev
->busy
1481 || comedi_buf_read_n_available(read_subdev
->async
) > 0
1482 || !(comedi_get_subdevice_runflags(read_subdev
) &
1484 mask
|= POLLIN
| POLLRDNORM
;
1487 write_subdev
= comedi_get_write_subdevice(dev_file_info
);
1489 poll_wait(file
, &write_subdev
->async
->wait_head
, wait
);
1490 comedi_buf_write_alloc(write_subdev
->async
,
1491 write_subdev
->async
->prealloc_bufsz
);
1492 if (!write_subdev
->busy
1493 || !(comedi_get_subdevice_runflags(write_subdev
) &
1495 || comedi_buf_write_n_allocated(write_subdev
->async
) >=
1496 bytes_per_sample(write_subdev
->async
->subdevice
)) {
1497 mask
|= POLLOUT
| POLLWRNORM
;
1501 mutex_unlock(&dev
->mutex
);
1505 static ssize_t
comedi_write(struct file
*file
, const char *buf
, size_t nbytes
,
1508 struct comedi_subdevice
*s
;
1509 struct comedi_async
*async
;
1510 int n
, m
, count
= 0, retval
= 0;
1511 DECLARE_WAITQUEUE(wait
, current
);
1512 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1513 struct comedi_device_file_info
*dev_file_info
=
1514 comedi_get_device_file_info(minor
);
1515 struct comedi_device
*dev
= dev_file_info
->device
;
1517 if (!dev
->attached
) {
1518 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1523 s
= comedi_get_write_subdevice(dev_file_info
);
1538 if (s
->busy
!= file
) {
1542 add_wait_queue(&async
->wait_head
, &wait
);
1543 while (nbytes
> 0 && !retval
) {
1544 set_current_state(TASK_INTERRUPTIBLE
);
1549 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
1550 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
1551 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
1552 if (m
> comedi_buf_write_n_allocated(async
))
1553 m
= comedi_buf_write_n_allocated(async
);
1558 if (!(comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)) {
1559 if (comedi_get_subdevice_runflags(s
) &
1565 do_become_nonbusy(dev
, s
);
1568 if (file
->f_flags
& O_NONBLOCK
) {
1572 if (signal_pending(current
)) {
1573 retval
= -ERESTARTSYS
;
1579 if (s
->busy
!= file
) {
1586 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
1592 comedi_buf_write_free(async
, n
);
1598 break; /* makes device work like a pipe */
1600 set_current_state(TASK_RUNNING
);
1601 remove_wait_queue(&async
->wait_head
, &wait
);
1604 return count
? count
: retval
;
1607 static ssize_t
comedi_read(struct file
*file
, char *buf
, size_t nbytes
,
1610 struct comedi_subdevice
*s
;
1611 struct comedi_async
*async
;
1612 int n
, m
, count
= 0, retval
= 0;
1613 DECLARE_WAITQUEUE(wait
, current
);
1614 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1615 struct comedi_device_file_info
*dev_file_info
=
1616 comedi_get_device_file_info(minor
);
1617 struct comedi_device
*dev
= dev_file_info
->device
;
1619 if (!dev
->attached
) {
1620 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1625 s
= comedi_get_read_subdevice(dev_file_info
);
1639 if (s
->busy
!= file
) {
1644 add_wait_queue(&async
->wait_head
, &wait
);
1645 while (nbytes
> 0 && !retval
) {
1646 set_current_state(TASK_INTERRUPTIBLE
);
1650 m
= comedi_buf_read_n_available(async
);
1651 /* printk("%d available\n",m); */
1652 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
1653 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
1654 /* printk("%d contiguous\n",m); */
1659 if (!(comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)) {
1660 do_become_nonbusy(dev
, s
);
1661 if (comedi_get_subdevice_runflags(s
) &
1669 if (file
->f_flags
& O_NONBLOCK
) {
1673 if (signal_pending(current
)) {
1674 retval
= -ERESTARTSYS
;
1682 if (s
->busy
!= file
) {
1688 m
= copy_to_user(buf
, async
->prealloc_buf
+
1689 async
->buf_read_ptr
, n
);
1695 comedi_buf_read_alloc(async
, n
);
1696 comedi_buf_read_free(async
, n
);
1702 break; /* makes device work like a pipe */
1704 if (!(comedi_get_subdevice_runflags(s
) & (SRF_ERROR
| SRF_RUNNING
)) &&
1705 async
->buf_read_count
- async
->buf_write_count
== 0) {
1706 do_become_nonbusy(dev
, s
);
1708 set_current_state(TASK_RUNNING
);
1709 remove_wait_queue(&async
->wait_head
, &wait
);
1712 return count
? count
: retval
;
1716 This function restores a subdevice to an idle state.
1718 void do_become_nonbusy(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1720 struct comedi_async
*async
= s
->async
;
1722 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
1723 #ifdef CONFIG_COMEDI_RT
1724 if (comedi_get_subdevice_runflags(s
) & SRF_RT
) {
1725 comedi_switch_to_non_rt(dev
);
1726 comedi_set_subdevice_runflags(s
, SRF_RT
, 0);
1730 comedi_reset_async_buf(async
);
1731 async
->inttrig
= NULL
;
1734 "BUG: (?) do_become_nonbusy called with async=0\n");
1740 static int comedi_open(struct inode
*inode
, struct file
*file
)
1742 const unsigned minor
= iminor(inode
);
1743 struct comedi_device_file_info
*dev_file_info
=
1744 comedi_get_device_file_info(minor
);
1745 struct comedi_device
*dev
= dev_file_info
? dev_file_info
->device
: NULL
;
1748 DPRINTK("invalid minor number\n");
1752 /* This is slightly hacky, but we want module autoloading
1754 * case: user opens device, attached -> ok
1755 * case: user opens device, unattached, in_request_module=0 -> autoload
1756 * case: user opens device, unattached, in_request_module=1 -> fail
1757 * case: root opens device, attached -> ok
1758 * case: root opens device, unattached, in_request_module=1 -> ok
1759 * (typically called from modprobe)
1760 * case: root opens device, unattached, in_request_module=0 -> autoload
1762 * The last could be changed to "-> ok", which would deny root
1765 mutex_lock(&dev
->mutex
);
1768 if (!capable(CAP_SYS_MODULE
) && dev
->in_request_module
) {
1769 DPRINTK("in request module\n");
1770 mutex_unlock(&dev
->mutex
);
1773 if (capable(CAP_SYS_MODULE
) && dev
->in_request_module
)
1776 dev
->in_request_module
= 1;
1779 mutex_unlock(&dev
->mutex
);
1780 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
1781 mutex_lock(&dev
->mutex
);
1784 dev
->in_request_module
= 0;
1786 if (!dev
->attached
&& !capable(CAP_SYS_MODULE
)) {
1787 DPRINTK("not attached and not CAP_SYS_MODULE\n");
1788 mutex_unlock(&dev
->mutex
);
1792 __module_get(THIS_MODULE
);
1794 if (dev
->attached
) {
1795 if (!try_module_get(dev
->driver
->module
)) {
1796 module_put(THIS_MODULE
);
1797 mutex_unlock(&dev
->mutex
);
1802 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
)
1807 mutex_unlock(&dev
->mutex
);
1812 static int comedi_close(struct inode
*inode
, struct file
*file
)
1814 const unsigned minor
= iminor(inode
);
1815 struct comedi_device_file_info
*dev_file_info
=
1816 comedi_get_device_file_info(minor
);
1817 struct comedi_device
*dev
= dev_file_info
->device
;
1818 struct comedi_subdevice
*s
= NULL
;
1821 mutex_lock(&dev
->mutex
);
1823 if (dev
->subdevices
) {
1824 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
1825 s
= dev
->subdevices
+ i
;
1827 if (s
->busy
== file
)
1829 if (s
->lock
== file
)
1833 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
1836 module_put(THIS_MODULE
);
1838 module_put(dev
->driver
->module
);
1842 mutex_unlock(&dev
->mutex
);
1844 if (file
->f_flags
& FASYNC
)
1845 comedi_fasync(-1, file
, 0);
1850 static int comedi_fasync(int fd
, struct file
*file
, int on
)
1852 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1853 struct comedi_device_file_info
*dev_file_info
=
1854 comedi_get_device_file_info(minor
);
1856 struct comedi_device
*dev
= dev_file_info
->device
;
1858 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
1861 const struct file_operations comedi_fops
= {
1862 .owner
= THIS_MODULE
,
1863 #ifdef HAVE_UNLOCKED_IOCTL
1864 .unlocked_ioctl
= comedi_unlocked_ioctl
,
1866 .ioctl
= comedi_ioctl
,
1868 #ifdef HAVE_COMPAT_IOCTL
1869 .compat_ioctl
= comedi_compat_ioctl
,
1871 .open
= comedi_open
,
1872 .release
= comedi_close
,
1873 .read
= comedi_read
,
1874 .write
= comedi_write
,
1875 .mmap
= comedi_mmap
,
1876 .poll
= comedi_poll
,
1877 .fasync
= comedi_fasync
,
1880 struct class *comedi_class
;
1881 static struct cdev comedi_cdev
;
1883 static void comedi_cleanup_legacy_minors(void)
1887 for (i
= 0; i
< comedi_num_legacy_minors
; i
++)
1888 comedi_free_board_minor(i
);
1891 static int __init
comedi_init(void)
1896 printk(KERN_INFO
"comedi: version " COMEDI_RELEASE
1897 " - http://www.comedi.org\n");
1899 if (comedi_num_legacy_minors
< 0 ||
1900 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
1901 printk(KERN_ERR
"comedi: error: invalid value for module "
1902 "parameter \"comedi_num_legacy_minors\". Valid values "
1903 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS
);
1908 * comedi is unusable if both comedi_autoconfig and
1909 * comedi_num_legacy_minors are zero, so we might as well adjust the
1910 * defaults in that case
1912 if (comedi_autoconfig
== 0 && comedi_num_legacy_minors
== 0)
1913 comedi_num_legacy_minors
= 16;
1915 memset(comedi_file_info_table
, 0,
1916 sizeof(struct comedi_device_file_info
*) * COMEDI_NUM_MINORS
);
1918 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1919 COMEDI_NUM_MINORS
, "comedi");
1922 cdev_init(&comedi_cdev
, &comedi_fops
);
1923 comedi_cdev
.owner
= THIS_MODULE
;
1924 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
1925 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
1926 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1930 comedi_class
= class_create(THIS_MODULE
, "comedi");
1931 if (IS_ERR(comedi_class
)) {
1932 printk("comedi: failed to create class");
1933 cdev_del(&comedi_cdev
);
1934 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1936 return PTR_ERR(comedi_class
);
1939 /* XXX requires /proc interface */
1942 /* create devices files for legacy/manual use */
1943 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
1945 minor
= comedi_alloc_board_minor(NULL
);
1947 comedi_cleanup_legacy_minors();
1948 cdev_del(&comedi_cdev
);
1949 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1957 comedi_register_ioctl32();
1962 static void __exit
comedi_cleanup(void)
1966 comedi_cleanup_legacy_minors();
1967 for (i
= 0; i
< COMEDI_NUM_MINORS
; ++i
)
1968 BUG_ON(comedi_file_info_table
[i
]);
1971 class_destroy(comedi_class
);
1972 cdev_del(&comedi_cdev
);
1973 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
1975 comedi_proc_cleanup();
1977 comedi_rt_cleanup();
1979 comedi_unregister_ioctl32();
1982 module_init(comedi_init
);
1983 module_exit(comedi_cleanup
);
1985 void comedi_error(const struct comedi_device
*dev
, const char *s
)
1987 printk("comedi%d: %s: %s\n", dev
->minor
, dev
->driver
->driver_name
, s
);
1990 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1992 struct comedi_async
*async
= s
->async
;
1993 unsigned runflags
= 0;
1994 unsigned runflags_mask
= 0;
1996 /* DPRINTK("comedi_event 0x%x\n",mask); */
1998 if ((comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) == 0)
2002 events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2003 runflags_mask
|= SRF_RUNNING
;
2005 /* remember if an error event has occured, so an error
2006 * can be returned the next time the user does a read() */
2007 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2008 runflags_mask
|= SRF_ERROR
;
2009 runflags
|= SRF_ERROR
;
2011 if (runflags_mask
) {
2012 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2013 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2016 if (async
->cb_mask
& s
->async
->events
) {
2017 if (comedi_get_subdevice_runflags(s
) & SRF_USER
) {
2020 #ifdef CONFIG_COMEDI_RT
2022 comedi_rt_pend_wakeup(&async
->wait_head
);
2025 ("BUG: comedi_event() code unreachable\n");
2028 wake_up_interruptible(&async
->wait_head
);
2029 if (s
->subdev_flags
& SDF_CMD_READ
) {
2030 kill_fasync(&dev
->async_queue
, SIGIO
,
2033 if (s
->subdev_flags
& SDF_CMD_WRITE
) {
2034 kill_fasync(&dev
->async_queue
, SIGIO
,
2040 async
->cb_func(s
->async
->events
, async
->cb_arg
);
2041 /* XXX bug here. If subdevice A is rt, and
2042 * subdevice B tries to callback to a normal
2043 * linux kernel function, it will be at the
2044 * wrong priority. Since this isn't very
2045 * common, I'm not going to worry about it. */
2048 s
->async
->events
= 0;
2051 void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
, unsigned mask
,
2054 unsigned long flags
;
2056 spin_lock_irqsave(&s
->spin_lock
, flags
);
2057 s
->runflags
&= ~mask
;
2058 s
->runflags
|= (bits
& mask
);
2059 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2062 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
2064 unsigned long flags
;
2067 spin_lock_irqsave(&s
->spin_lock
, flags
);
2068 runflags
= s
->runflags
;
2069 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2073 static int is_device_busy(struct comedi_device
*dev
)
2075 struct comedi_subdevice
*s
;
2081 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2082 s
= dev
->subdevices
+ i
;
2085 if (s
->async
&& s
->async
->mmap_count
)
2092 void comedi_device_init(struct comedi_device
*dev
)
2094 memset(dev
, 0, sizeof(struct comedi_device
));
2095 spin_lock_init(&dev
->spinlock
);
2096 mutex_init(&dev
->mutex
);
2100 void comedi_device_cleanup(struct comedi_device
*dev
)
2104 mutex_lock(&dev
->mutex
);
2105 comedi_device_detach(dev
);
2106 mutex_unlock(&dev
->mutex
);
2107 mutex_destroy(&dev
->mutex
);
2110 int comedi_alloc_board_minor(struct device
*hardware_device
)
2112 unsigned long flags
;
2113 struct comedi_device_file_info
*info
;
2114 struct device
*csdev
;
2118 info
= kzalloc(sizeof(struct comedi_device_file_info
), GFP_KERNEL
);
2121 info
->device
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2122 if (info
->device
== NULL
) {
2126 comedi_device_init(info
->device
);
2127 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2128 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2129 if (comedi_file_info_table
[i
] == NULL
) {
2130 comedi_file_info_table
[i
] = info
;
2134 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2135 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2136 comedi_device_cleanup(info
->device
);
2137 kfree(info
->device
);
2139 printk(KERN_ERR
"comedi: error: ran out of minor numbers for board device files.\n");
2142 info
->device
->minor
= i
;
2143 csdev
= COMEDI_DEVICE_CREATE(comedi_class
, NULL
,
2144 MKDEV(COMEDI_MAJOR
, i
), NULL
,
2145 hardware_device
, "comedi%i", i
);
2147 info
->device
->class_dev
= csdev
;
2148 dev_set_drvdata(csdev
, info
);
2149 retval
= device_create_file(csdev
, &dev_attr_max_read_buffer_kb
);
2151 printk(KERN_ERR
"comedi: failed to create sysfs attribute file \"%s\".\n",
2152 dev_attr_max_read_buffer_kb
.attr
.name
);
2153 comedi_free_board_minor(i
);
2156 retval
= device_create_file(csdev
, &dev_attr_read_buffer_kb
);
2158 printk(KERN_ERR
"comedi: failed to create sysfs attribute file \"%s\".\n",
2159 dev_attr_read_buffer_kb
.attr
.name
);
2160 comedi_free_board_minor(i
);
2163 retval
= device_create_file(csdev
, &dev_attr_max_write_buffer_kb
);
2165 printk(KERN_ERR
"comedi: failed to create sysfs attribute file \"%s\".\n",
2166 dev_attr_max_write_buffer_kb
.attr
.name
);
2167 comedi_free_board_minor(i
);
2170 retval
= device_create_file(csdev
, &dev_attr_write_buffer_kb
);
2172 printk(KERN_ERR
"comedi: failed to create sysfs attribute file \"%s\".\n",
2173 dev_attr_write_buffer_kb
.attr
.name
);
2174 comedi_free_board_minor(i
);
2180 void comedi_free_board_minor(unsigned minor
)
2182 unsigned long flags
;
2183 struct comedi_device_file_info
*info
;
2185 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2186 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2187 info
= comedi_file_info_table
[minor
];
2188 comedi_file_info_table
[minor
] = NULL
;
2189 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2192 struct comedi_device
*dev
= info
->device
;
2194 if (dev
->class_dev
) {
2195 device_destroy(comedi_class
,
2196 MKDEV(COMEDI_MAJOR
, dev
->minor
));
2198 comedi_device_cleanup(dev
);
2205 int comedi_alloc_subdevice_minor(struct comedi_device
*dev
,
2206 struct comedi_subdevice
*s
)
2208 unsigned long flags
;
2209 struct comedi_device_file_info
*info
;
2210 struct device
*csdev
;
2214 info
= kmalloc(sizeof(struct comedi_device_file_info
), GFP_KERNEL
);
2218 info
->read_subdevice
= s
;
2219 info
->write_subdevice
= s
;
2220 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2221 for (i
= COMEDI_FIRST_SUBDEVICE_MINOR
; i
< COMEDI_NUM_MINORS
; ++i
) {
2222 if (comedi_file_info_table
[i
] == NULL
) {
2223 comedi_file_info_table
[i
] = info
;
2227 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2228 if (i
== COMEDI_NUM_MINORS
) {
2230 printk(KERN_ERR
"comedi: error: ran out of minor numbers for board device files.\n");
2234 csdev
= COMEDI_DEVICE_CREATE(comedi_class
, dev
->class_dev
,
2235 MKDEV(COMEDI_MAJOR
, i
), NULL
, NULL
,
2236 "comedi%i_subd%i", dev
->minor
,
2237 (int)(s
- dev
->subdevices
));
2239 s
->class_dev
= csdev
;
2240 dev_set_drvdata(csdev
, info
);
2241 retval
= device_create_file(csdev
, &dev_attr_max_read_buffer_kb
);
2243 printk(KERN_ERR
"comedi: failed to create sysfs attribute file \"%s\".\n",
2244 dev_attr_max_read_buffer_kb
.attr
.name
);
2245 comedi_free_subdevice_minor(s
);
2248 retval
= device_create_file(csdev
, &dev_attr_read_buffer_kb
);
2250 printk(KERN_ERR
"comedi: failed to create sysfs attribute file \"%s\".\n",
2251 dev_attr_read_buffer_kb
.attr
.name
);
2252 comedi_free_subdevice_minor(s
);
2255 retval
= device_create_file(csdev
, &dev_attr_max_write_buffer_kb
);
2257 printk(KERN_ERR
"comedi: failed to create sysfs attribute file \"%s\".\n",
2258 dev_attr_max_write_buffer_kb
.attr
.name
);
2259 comedi_free_subdevice_minor(s
);
2262 retval
= device_create_file(csdev
, &dev_attr_write_buffer_kb
);
2264 printk(KERN_ERR
"comedi: failed to create sysfs attribute file \"%s\".\n",
2265 dev_attr_write_buffer_kb
.attr
.name
);
2266 comedi_free_subdevice_minor(s
);
2272 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2274 unsigned long flags
;
2275 struct comedi_device_file_info
*info
;
2282 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2283 BUG_ON(s
->minor
< COMEDI_FIRST_SUBDEVICE_MINOR
);
2285 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2286 info
= comedi_file_info_table
[s
->minor
];
2287 comedi_file_info_table
[s
->minor
] = NULL
;
2288 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2291 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2292 s
->class_dev
= NULL
;
2297 struct comedi_device_file_info
*comedi_get_device_file_info(unsigned minor
)
2299 unsigned long flags
;
2300 struct comedi_device_file_info
*info
;
2302 BUG_ON(minor
>= COMEDI_NUM_MINORS
);
2303 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2304 info
= comedi_file_info_table
[minor
];
2305 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2309 static int resize_async_buffer(struct comedi_device
*dev
,
2310 struct comedi_subdevice
*s
,
2311 struct comedi_async
*async
, unsigned new_size
)
2315 if (new_size
> async
->max_bufsize
)
2319 DPRINTK("subdevice is busy, cannot resize buffer\n");
2322 if (async
->mmap_count
) {
2323 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2327 if (!async
->prealloc_buf
)
2330 /* make sure buffer is an integral number of pages
2332 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2334 retval
= comedi_buf_alloc(dev
, s
, new_size
);
2338 if (s
->buf_change
) {
2339 retval
= s
->buf_change(dev
, s
, new_size
);
2344 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2345 dev
->minor
, s
- dev
->subdevices
, async
->prealloc_bufsz
);
2349 /* sysfs attribute files */
2351 static const unsigned bytes_per_kibi
= 1024;
2353 static ssize_t
show_max_read_buffer_kb(struct device
*dev
,
2354 struct device_attribute
*attr
, char *buf
)
2357 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2358 unsigned max_buffer_size_kb
= 0;
2359 struct comedi_subdevice
*const read_subdevice
=
2360 comedi_get_read_subdevice(info
);
2362 mutex_lock(&info
->device
->mutex
);
2363 if (read_subdevice
&&
2364 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) &&
2365 read_subdevice
->async
) {
2366 max_buffer_size_kb
= read_subdevice
->async
->max_bufsize
/
2369 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", max_buffer_size_kb
);
2370 mutex_unlock(&info
->device
->mutex
);
2375 static ssize_t
store_max_read_buffer_kb(struct device
*dev
,
2376 struct device_attribute
*attr
,
2377 const char *buf
, size_t count
)
2379 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2380 unsigned long new_max_size_kb
;
2381 uint64_t new_max_size
;
2382 struct comedi_subdevice
*const read_subdevice
=
2383 comedi_get_read_subdevice(info
);
2385 if (strict_strtoul(buf
, 10, &new_max_size_kb
))
2387 if (new_max_size_kb
!= (uint32_t)new_max_size_kb
)
2389 new_max_size
= ((uint64_t)new_max_size_kb
) * bytes_per_kibi
;
2390 if (new_max_size
!= (uint32_t)new_max_size
)
2393 mutex_lock(&info
->device
->mutex
);
2394 if (read_subdevice
== NULL
||
2395 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) == 0 ||
2396 read_subdevice
->async
== NULL
) {
2397 mutex_unlock(&info
->device
->mutex
);
2400 read_subdevice
->async
->max_bufsize
= new_max_size
;
2401 mutex_unlock(&info
->device
->mutex
);
2406 static struct device_attribute dev_attr_max_read_buffer_kb
= {
2408 .name
= "max_read_buffer_kb",
2409 .mode
= S_IRUGO
| S_IWUSR
2411 .show
= &show_max_read_buffer_kb
,
2412 .store
= &store_max_read_buffer_kb
2415 static ssize_t
show_read_buffer_kb(struct device
*dev
,
2416 struct device_attribute
*attr
, char *buf
)
2419 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2420 unsigned buffer_size_kb
= 0;
2421 struct comedi_subdevice
*const read_subdevice
=
2422 comedi_get_read_subdevice(info
);
2424 mutex_lock(&info
->device
->mutex
);
2425 if (read_subdevice
&&
2426 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) &&
2427 read_subdevice
->async
) {
2428 buffer_size_kb
= read_subdevice
->async
->prealloc_bufsz
/
2431 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", buffer_size_kb
);
2432 mutex_unlock(&info
->device
->mutex
);
2437 static ssize_t
store_read_buffer_kb(struct device
*dev
,
2438 struct device_attribute
*attr
,
2439 const char *buf
, size_t count
)
2441 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2442 unsigned long new_size_kb
;
2445 struct comedi_subdevice
*const read_subdevice
=
2446 comedi_get_read_subdevice(info
);
2448 if (strict_strtoul(buf
, 10, &new_size_kb
))
2450 if (new_size_kb
!= (uint32_t)new_size_kb
)
2452 new_size
= ((uint64_t)new_size_kb
) * bytes_per_kibi
;
2453 if (new_size
!= (uint32_t)new_size
)
2456 mutex_lock(&info
->device
->mutex
);
2457 if (read_subdevice
== NULL
||
2458 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) == 0 ||
2459 read_subdevice
->async
== NULL
) {
2460 mutex_unlock(&info
->device
->mutex
);
2463 retval
= resize_async_buffer(info
->device
, read_subdevice
,
2464 read_subdevice
->async
, new_size
);
2465 mutex_unlock(&info
->device
->mutex
);
2472 static struct device_attribute dev_attr_read_buffer_kb
= {
2474 .name
= "read_buffer_kb",
2475 .mode
= S_IRUGO
| S_IWUSR
| S_IWGRP
2477 .show
= &show_read_buffer_kb
,
2478 .store
= &store_read_buffer_kb
2481 static ssize_t
show_max_write_buffer_kb(struct device
*dev
,
2482 struct device_attribute
*attr
,
2486 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2487 unsigned max_buffer_size_kb
= 0;
2488 struct comedi_subdevice
*const write_subdevice
=
2489 comedi_get_write_subdevice(info
);
2491 mutex_lock(&info
->device
->mutex
);
2492 if (write_subdevice
&&
2493 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) &&
2494 write_subdevice
->async
) {
2495 max_buffer_size_kb
= write_subdevice
->async
->max_bufsize
/
2498 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", max_buffer_size_kb
);
2499 mutex_unlock(&info
->device
->mutex
);
2504 static ssize_t
store_max_write_buffer_kb(struct device
*dev
,
2505 struct device_attribute
*attr
,
2506 const char *buf
, size_t count
)
2508 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2509 unsigned long new_max_size_kb
;
2510 uint64_t new_max_size
;
2511 struct comedi_subdevice
*const write_subdevice
=
2512 comedi_get_write_subdevice(info
);
2514 if (strict_strtoul(buf
, 10, &new_max_size_kb
))
2516 if (new_max_size_kb
!= (uint32_t)new_max_size_kb
)
2518 new_max_size
= ((uint64_t)new_max_size_kb
) * bytes_per_kibi
;
2519 if (new_max_size
!= (uint32_t)new_max_size
)
2522 mutex_lock(&info
->device
->mutex
);
2523 if (write_subdevice
== NULL
||
2524 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) == 0 ||
2525 write_subdevice
->async
== NULL
) {
2526 mutex_unlock(&info
->device
->mutex
);
2529 write_subdevice
->async
->max_bufsize
= new_max_size
;
2530 mutex_unlock(&info
->device
->mutex
);
2535 static struct device_attribute dev_attr_max_write_buffer_kb
= {
2537 .name
= "max_write_buffer_kb",
2538 .mode
= S_IRUGO
| S_IWUSR
2540 .show
= &show_max_write_buffer_kb
,
2541 .store
= &store_max_write_buffer_kb
2544 static ssize_t
show_write_buffer_kb(struct device
*dev
,
2545 struct device_attribute
*attr
, char *buf
)
2548 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2549 unsigned buffer_size_kb
= 0;
2550 struct comedi_subdevice
*const write_subdevice
=
2551 comedi_get_write_subdevice(info
);
2553 mutex_lock(&info
->device
->mutex
);
2554 if (write_subdevice
&&
2555 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) &&
2556 write_subdevice
->async
) {
2557 buffer_size_kb
= write_subdevice
->async
->prealloc_bufsz
/
2560 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", buffer_size_kb
);
2561 mutex_unlock(&info
->device
->mutex
);
2566 static ssize_t
store_write_buffer_kb(struct device
*dev
,
2567 struct device_attribute
*attr
,
2568 const char *buf
, size_t count
)
2570 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2571 unsigned long new_size_kb
;
2574 struct comedi_subdevice
*const write_subdevice
=
2575 comedi_get_write_subdevice(info
);
2577 if (strict_strtoul(buf
, 10, &new_size_kb
))
2579 if (new_size_kb
!= (uint32_t)new_size_kb
)
2581 new_size
= ((uint64_t)new_size_kb
) * bytes_per_kibi
;
2582 if (new_size
!= (uint32_t)new_size
)
2585 mutex_lock(&info
->device
->mutex
);
2586 if (write_subdevice
== NULL
||
2587 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) == 0 ||
2588 write_subdevice
->async
== NULL
) {
2589 mutex_unlock(&info
->device
->mutex
);
2592 retval
= resize_async_buffer(info
->device
, write_subdevice
,
2593 write_subdevice
->async
, new_size
);
2594 mutex_unlock(&info
->device
->mutex
);
2601 static struct device_attribute dev_attr_write_buffer_kb
= {
2603 .name
= "write_buffer_kb",
2604 .mode
= S_IRUGO
| S_IWUSR
| S_IWGRP
2606 .show
= &show_write_buffer_kb
,
2607 .store
= &store_write_buffer_kb