5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define __NO_VERSION__
27 #include "comedi_fops.h"
28 #include "comedi_compat32.h"
30 #include <linux/module.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/fcntl.h>
35 #include <linux/delay.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/kmod.h>
40 #include <linux/poll.h>
41 #include <linux/init.h>
42 #include <linux/device.h>
43 #include <linux/vmalloc.h>
45 #include "comedidev.h"
46 #include <linux/cdev.h>
47 #include <linux/stat.h>
50 #include <linux/uaccess.h>
54 MODULE_AUTHOR("http://www.comedi.org");
55 MODULE_DESCRIPTION("Comedi core module");
56 MODULE_LICENSE("GPL");
58 #ifdef CONFIG_COMEDI_DEBUG
60 EXPORT_SYMBOL(comedi_debug
);
61 module_param(comedi_debug
, int, 0644);
64 int comedi_autoconfig
= 1;
65 module_param(comedi_autoconfig
, bool, 0444);
67 static int comedi_num_legacy_minors
;
68 module_param(comedi_num_legacy_minors
, int, 0444);
70 static DEFINE_SPINLOCK(comedi_file_info_table_lock
);
71 static struct comedi_device_file_info
72 *comedi_file_info_table
[COMEDI_NUM_MINORS
];
74 static int do_devconfig_ioctl(struct comedi_device
*dev
,
75 struct comedi_devconfig __user
*arg
);
76 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
77 struct comedi_bufconfig __user
*arg
);
78 static int do_devinfo_ioctl(struct comedi_device
*dev
,
79 struct comedi_devinfo __user
*arg
,
81 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
82 struct comedi_subdinfo __user
*arg
, void *file
);
83 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
84 struct comedi_chaninfo __user
*arg
);
85 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
86 struct comedi_bufinfo __user
*arg
);
87 static int do_cmd_ioctl(struct comedi_device
*dev
,
88 struct comedi_cmd __user
*arg
, void *file
);
89 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
91 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
93 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
95 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
96 struct comedi_cmd __user
*arg
, void *file
);
97 static int do_insnlist_ioctl(struct comedi_device
*dev
,
98 struct comedi_insnlist __user
*arg
, void *file
);
99 static int do_insn_ioctl(struct comedi_device
*dev
,
100 struct comedi_insn __user
*arg
, void *file
);
101 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int subd
,
104 extern void do_become_nonbusy(struct comedi_device
*dev
,
105 struct comedi_subdevice
*s
);
106 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
108 static int comedi_fasync(int fd
, struct file
*file
, int on
);
110 static int is_device_busy(struct comedi_device
*dev
);
111 static int resize_async_buffer(struct comedi_device
*dev
,
112 struct comedi_subdevice
*s
,
113 struct comedi_async
*async
, unsigned new_size
);
115 /* declarations for sysfs attribute files */
116 static struct device_attribute dev_attr_max_read_buffer_kb
;
117 static struct device_attribute dev_attr_read_buffer_kb
;
118 static struct device_attribute dev_attr_max_write_buffer_kb
;
119 static struct device_attribute dev_attr_write_buffer_kb
;
121 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
124 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
125 struct comedi_device_file_info
*dev_file_info
=
126 comedi_get_device_file_info(minor
);
127 struct comedi_device
*dev
;
130 if (dev_file_info
== NULL
|| dev_file_info
->device
== NULL
)
132 dev
= dev_file_info
->device
;
134 mutex_lock(&dev
->mutex
);
136 /* Device config is special, because it must work on
137 * an unconfigured device. */
138 if (cmd
== COMEDI_DEVCONFIG
) {
139 rc
= do_devconfig_ioctl(dev
,
140 (struct comedi_devconfig __user
*)arg
);
144 if (!dev
->attached
) {
145 DPRINTK("no driver configured on /dev/comedi%i\n", dev
->minor
);
151 case COMEDI_BUFCONFIG
:
152 rc
= do_bufconfig_ioctl(dev
,
153 (struct comedi_bufconfig __user
*)arg
);
156 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
159 case COMEDI_SUBDINFO
:
160 rc
= do_subdinfo_ioctl(dev
,
161 (struct comedi_subdinfo __user
*)arg
,
164 case COMEDI_CHANINFO
:
165 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
167 case COMEDI_RANGEINFO
:
168 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
171 rc
= do_bufinfo_ioctl(dev
,
172 (struct comedi_bufinfo __user
*)arg
);
175 rc
= do_lock_ioctl(dev
, arg
, file
);
178 rc
= do_unlock_ioctl(dev
, arg
, file
);
181 rc
= do_cancel_ioctl(dev
, arg
, file
);
184 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
187 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
190 case COMEDI_INSNLIST
:
191 rc
= do_insnlist_ioctl(dev
,
192 (struct comedi_insnlist __user
*)arg
,
196 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
200 rc
= do_poll_ioctl(dev
, arg
, file
);
208 mutex_unlock(&dev
->mutex
);
217 pointer to devconfig structure
220 devconfig structure at arg
225 static int do_devconfig_ioctl(struct comedi_device
*dev
,
226 struct comedi_devconfig __user
*arg
)
228 struct comedi_devconfig it
;
230 unsigned char *aux_data
= NULL
;
233 if (!capable(CAP_SYS_ADMIN
))
237 if (is_device_busy(dev
))
240 struct module
*driver_module
= dev
->driver
->module
;
241 comedi_device_detach(dev
);
242 module_put(driver_module
);
247 if (copy_from_user(&it
, arg
, sizeof(struct comedi_devconfig
)))
250 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
252 if (comedi_aux_data(it
.options
, 0) &&
253 it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
255 aux_len
= it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
];
259 aux_data
= vmalloc(aux_len
);
263 if (copy_from_user(aux_data
,
264 comedi_aux_data(it
.options
, 0), aux_len
)) {
268 it
.options
[COMEDI_DEVCONF_AUX_DATA_LO
] =
269 (unsigned long)aux_data
;
270 if (sizeof(void *) > sizeof(int)) {
271 bit_shift
= sizeof(int) * 8;
272 it
.options
[COMEDI_DEVCONF_AUX_DATA_HI
] =
273 ((unsigned long)aux_data
) >> bit_shift
;
275 it
.options
[COMEDI_DEVCONF_AUX_DATA_HI
] = 0;
278 ret
= comedi_device_attach(dev
, &it
);
280 if (!try_module_get(dev
->driver
->module
)) {
281 comedi_device_detach(dev
);
294 buffer configuration ioctl
297 pointer to bufconfig structure
303 modified bufconfig at arg
306 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
307 struct comedi_bufconfig __user
*arg
)
309 struct comedi_bufconfig bc
;
310 struct comedi_async
*async
;
311 struct comedi_subdevice
*s
;
314 if (copy_from_user(&bc
, arg
, sizeof(struct comedi_bufconfig
)))
317 if (bc
.subdevice
>= dev
->n_subdevices
|| bc
.subdevice
< 0)
320 s
= dev
->subdevices
+ bc
.subdevice
;
324 DPRINTK("subdevice does not have async capability\n");
330 if (bc
.maximum_size
) {
331 if (!capable(CAP_SYS_ADMIN
))
334 async
->max_bufsize
= bc
.maximum_size
;
338 retval
= resize_async_buffer(dev
, s
, async
, bc
.size
);
343 bc
.size
= async
->prealloc_bufsz
;
344 bc
.maximum_size
= async
->max_bufsize
;
347 if (copy_to_user(arg
, &bc
, sizeof(struct comedi_bufconfig
)))
358 pointer to devinfo structure
367 static int do_devinfo_ioctl(struct comedi_device
*dev
,
368 struct comedi_devinfo __user
*arg
,
371 struct comedi_devinfo devinfo
;
372 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
373 struct comedi_device_file_info
*dev_file_info
=
374 comedi_get_device_file_info(minor
);
375 struct comedi_subdevice
*read_subdev
=
376 comedi_get_read_subdevice(dev_file_info
);
377 struct comedi_subdevice
*write_subdev
=
378 comedi_get_write_subdevice(dev_file_info
);
380 memset(&devinfo
, 0, sizeof(devinfo
));
382 /* fill devinfo structure */
383 devinfo
.version_code
= COMEDI_VERSION_CODE
;
384 devinfo
.n_subdevs
= dev
->n_subdevices
;
385 memcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
386 memcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
389 devinfo
.read_subdevice
= read_subdev
- dev
->subdevices
;
391 devinfo
.read_subdevice
= -1;
394 devinfo
.write_subdevice
= write_subdev
- dev
->subdevices
;
396 devinfo
.write_subdevice
= -1;
398 if (copy_to_user(arg
, &devinfo
, sizeof(struct comedi_devinfo
)))
409 pointer to array of subdevice info structures
415 array of subdevice info structures at arg
418 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
419 struct comedi_subdinfo __user
*arg
, void *file
)
422 struct comedi_subdinfo
*tmp
, *us
;
423 struct comedi_subdevice
*s
;
426 kcalloc(dev
->n_subdevices
, sizeof(struct comedi_subdinfo
),
431 /* fill subdinfo structs */
432 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
433 s
= dev
->subdevices
+ i
;
437 us
->n_chan
= s
->n_chan
;
438 us
->subd_flags
= s
->subdev_flags
;
439 if (comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)
440 us
->subd_flags
|= SDF_RUNNING
;
441 #define TIMER_nanosec 5 /* backwards compatibility */
442 us
->timer_type
= TIMER_nanosec
;
443 us
->len_chanlist
= s
->len_chanlist
;
444 us
->maxdata
= s
->maxdata
;
445 if (s
->range_table
) {
447 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
449 us
->range_type
= 0; /* XXX */
451 us
->flags
= s
->flags
;
454 us
->subd_flags
|= SDF_BUSY
;
456 us
->subd_flags
|= SDF_BUSY_OWNER
;
458 us
->subd_flags
|= SDF_LOCKED
;
460 us
->subd_flags
|= SDF_LOCK_OWNER
;
461 if (!s
->maxdata
&& s
->maxdata_list
)
462 us
->subd_flags
|= SDF_MAXDATA
;
464 us
->subd_flags
|= SDF_FLAGS
;
465 if (s
->range_table_list
)
466 us
->subd_flags
|= SDF_RANGETYPE
;
468 us
->subd_flags
|= SDF_CMD
;
470 if (s
->insn_bits
!= &insn_inval
)
471 us
->insn_bits_support
= COMEDI_SUPPORTED
;
473 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
475 us
->settling_time_0
= s
->settling_time_0
;
478 ret
= copy_to_user(arg
, tmp
,
479 dev
->n_subdevices
* sizeof(struct comedi_subdinfo
));
483 return ret
? -EFAULT
: 0;
491 pointer to chaninfo structure
494 chaninfo structure at arg
497 arrays at elements of chaninfo structure
500 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
501 struct comedi_chaninfo __user
*arg
)
503 struct comedi_subdevice
*s
;
504 struct comedi_chaninfo it
;
506 if (copy_from_user(&it
, arg
, sizeof(struct comedi_chaninfo
)))
509 if (it
.subdev
>= dev
->n_subdevices
)
511 s
= dev
->subdevices
+ it
.subdev
;
513 if (it
.maxdata_list
) {
514 if (s
->maxdata
|| !s
->maxdata_list
)
516 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
517 s
->n_chan
* sizeof(unsigned int)))
524 if (copy_to_user(it
.flaglist
, s
->flaglist
,
525 s
->n_chan
* sizeof(unsigned int)))
532 if (!s
->range_table_list
)
534 for (i
= 0; i
< s
->n_chan
; i
++) {
537 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
538 (s
->range_table_list
[i
]->length
);
539 put_user(x
, it
.rangelist
+ i
);
542 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
543 s
->n_chan
* sizeof(unsigned int)))
553 buffer information ioctl
556 pointer to bufinfo structure
562 modified bufinfo at arg
565 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
566 struct comedi_bufinfo __user
*arg
)
568 struct comedi_bufinfo bi
;
569 struct comedi_subdevice
*s
;
570 struct comedi_async
*async
;
572 if (copy_from_user(&bi
, arg
, sizeof(struct comedi_bufinfo
)))
575 if (bi
.subdevice
>= dev
->n_subdevices
|| bi
.subdevice
< 0)
578 s
= dev
->subdevices
+ bi
.subdevice
;
582 DPRINTK("subdevice does not have async capability\n");
583 bi
.buf_write_ptr
= 0;
585 bi
.buf_write_count
= 0;
586 bi
.buf_read_count
= 0;
590 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
591 bi
.bytes_read
= comedi_buf_read_alloc(async
, bi
.bytes_read
);
592 comedi_buf_read_free(async
, bi
.bytes_read
);
594 if (!(comedi_get_subdevice_runflags(s
) & (SRF_ERROR
|
596 && async
->buf_write_count
== async
->buf_read_count
) {
597 do_become_nonbusy(dev
, s
);
601 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
603 comedi_buf_write_alloc(async
, bi
.bytes_written
);
604 comedi_buf_write_free(async
, bi
.bytes_written
);
607 bi
.buf_write_count
= async
->buf_write_count
;
608 bi
.buf_write_ptr
= async
->buf_write_ptr
;
609 bi
.buf_read_count
= async
->buf_read_count
;
610 bi
.buf_read_ptr
= async
->buf_read_ptr
;
613 if (copy_to_user(arg
, &bi
, sizeof(struct comedi_bufinfo
)))
619 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
620 unsigned int *data
, void *file
);
623 * synchronous instructions
626 * pointer to sync cmd structure
629 * sync cmd struct at arg
636 /* arbitrary limits */
637 #define MAX_SAMPLES 256
638 static int do_insnlist_ioctl(struct comedi_device
*dev
,
639 struct comedi_insnlist __user
*arg
, void *file
)
641 struct comedi_insnlist insnlist
;
642 struct comedi_insn
*insns
= NULL
;
643 unsigned int *data
= NULL
;
647 if (copy_from_user(&insnlist
, arg
, sizeof(struct comedi_insnlist
)))
650 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
652 DPRINTK("kmalloc failed\n");
658 kmalloc(sizeof(struct comedi_insn
) * insnlist
.n_insns
, GFP_KERNEL
);
660 DPRINTK("kmalloc failed\n");
665 if (copy_from_user(insns
, insnlist
.insns
,
666 sizeof(struct comedi_insn
) * insnlist
.n_insns
)) {
667 DPRINTK("copy_from_user failed\n");
672 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
673 if (insns
[i
].n
> MAX_SAMPLES
) {
674 DPRINTK("number of samples too large\n");
678 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
679 if (copy_from_user(data
, insns
[i
].data
,
680 insns
[i
].n
* sizeof(unsigned int))) {
681 DPRINTK("copy_from_user failed\n");
686 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
689 if (insns
[i
].insn
& INSN_MASK_READ
) {
690 if (copy_to_user(insns
[i
].data
, data
,
691 insns
[i
].n
* sizeof(unsigned int))) {
692 DPRINTK("copy_to_user failed\n");
710 static int check_insn_config_length(struct comedi_insn
*insn
,
717 case INSN_CONFIG_DIO_OUTPUT
:
718 case INSN_CONFIG_DIO_INPUT
:
719 case INSN_CONFIG_DISARM
:
720 case INSN_CONFIG_RESET
:
724 case INSN_CONFIG_ARM
:
725 case INSN_CONFIG_DIO_QUERY
:
726 case INSN_CONFIG_BLOCK_SIZE
:
727 case INSN_CONFIG_FILTER
:
728 case INSN_CONFIG_SERIAL_CLOCK
:
729 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
730 case INSN_CONFIG_ALT_SOURCE
:
731 case INSN_CONFIG_SET_COUNTER_MODE
:
732 case INSN_CONFIG_8254_READ_STATUS
:
733 case INSN_CONFIG_SET_ROUTING
:
734 case INSN_CONFIG_GET_ROUTING
:
735 case INSN_CONFIG_GET_PWM_STATUS
:
736 case INSN_CONFIG_PWM_SET_PERIOD
:
737 case INSN_CONFIG_PWM_GET_PERIOD
:
741 case INSN_CONFIG_SET_GATE_SRC
:
742 case INSN_CONFIG_GET_GATE_SRC
:
743 case INSN_CONFIG_SET_CLOCK_SRC
:
744 case INSN_CONFIG_GET_CLOCK_SRC
:
745 case INSN_CONFIG_SET_OTHER_SRC
:
746 case INSN_CONFIG_GET_COUNTER_STATUS
:
747 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
748 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
749 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
753 case INSN_CONFIG_PWM_OUTPUT
:
754 case INSN_CONFIG_ANALOG_TRIG
:
758 /* by default we allow the insn since we don't have checks for
759 * all possible cases yet */
762 "comedi: no check for data length of config insn id "
763 "%i is implemented.\n"
764 " Add a check to %s in %s.\n"
765 " Assuming n=%i is correct.\n", data
[0], __func__
,
773 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
774 unsigned int *data
, void *file
)
776 struct comedi_subdevice
*s
;
780 if (insn
->insn
& INSN_MASK_SPECIAL
) {
781 /* a non-subdevice instruction */
783 switch (insn
->insn
) {
793 do_gettimeofday(&tv
);
795 data
[1] = tv
.tv_usec
;
801 if (insn
->n
!= 1 || data
[0] >= 100000) {
805 udelay(data
[0] / 1000);
813 if (insn
->subdev
>= dev
->n_subdevices
) {
814 DPRINTK("%d not usable subdevice\n",
819 s
= dev
->subdevices
+ insn
->subdev
;
821 DPRINTK("no async\n");
825 if (!s
->async
->inttrig
) {
826 DPRINTK("no inttrig\n");
830 ret
= s
->async
->inttrig(dev
, s
, insn
->data
[0]);
835 DPRINTK("invalid insn\n");
840 /* a subdevice instruction */
841 unsigned int maxdata
;
843 if (insn
->subdev
>= dev
->n_subdevices
) {
844 DPRINTK("subdevice %d out of range\n", insn
->subdev
);
848 s
= dev
->subdevices
+ insn
->subdev
;
850 if (s
->type
== COMEDI_SUBD_UNUSED
) {
851 DPRINTK("%d not usable subdevice\n", insn
->subdev
);
856 /* are we locked? (ioctl lock) */
857 if (s
->lock
&& s
->lock
!= file
) {
858 DPRINTK("device locked\n");
863 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
866 DPRINTK("bad chanspec\n");
874 /* This looks arbitrary. It is. */
875 s
->busy
= &parse_insn
;
876 switch (insn
->insn
) {
878 ret
= s
->insn_read(dev
, s
, insn
, data
);
881 maxdata
= s
->maxdata_list
882 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
884 for (i
= 0; i
< insn
->n
; ++i
) {
885 if (data
[i
] > maxdata
) {
887 DPRINTK("bad data value(s)\n");
892 ret
= s
->insn_write(dev
, s
, insn
, data
);
899 ret
= s
->insn_bits(dev
, s
, insn
, data
);
902 ret
= check_insn_config_length(insn
, data
);
905 ret
= s
->insn_config(dev
, s
, insn
, data
);
921 * synchronous instructions
927 * struct comedi_insn struct at arg
933 static int do_insn_ioctl(struct comedi_device
*dev
,
934 struct comedi_insn __user
*arg
, void *file
)
936 struct comedi_insn insn
;
937 unsigned int *data
= NULL
;
940 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
946 if (copy_from_user(&insn
, arg
, sizeof(struct comedi_insn
))) {
951 /* This is where the behavior of insn and insnlist deviate. */
952 if (insn
.n
> MAX_SAMPLES
)
953 insn
.n
= MAX_SAMPLES
;
954 if (insn
.insn
& INSN_MASK_WRITE
) {
955 if (copy_from_user(data
,
957 insn
.n
* sizeof(unsigned int))) {
962 ret
= parse_insn(dev
, &insn
, data
, file
);
965 if (insn
.insn
& INSN_MASK_READ
) {
966 if (copy_to_user(insn
.data
,
968 insn
.n
* sizeof(unsigned int))) {
981 static void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
982 unsigned mask
, unsigned bits
)
986 spin_lock_irqsave(&s
->spin_lock
, flags
);
987 s
->runflags
&= ~mask
;
988 s
->runflags
|= (bits
& mask
);
989 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
992 static int do_cmd_ioctl(struct comedi_device
*dev
,
993 struct comedi_cmd __user
*cmd
, void *file
)
995 struct comedi_cmd user_cmd
;
996 struct comedi_subdevice
*s
;
997 struct comedi_async
*async
;
999 unsigned int __user
*chanlist_saver
= NULL
;
1001 if (copy_from_user(&user_cmd
, cmd
, sizeof(struct comedi_cmd
))) {
1002 DPRINTK("bad cmd address\n");
1005 /* save user's chanlist pointer so it can be restored later */
1006 chanlist_saver
= user_cmd
.chanlist
;
1008 if (user_cmd
.subdev
>= dev
->n_subdevices
) {
1009 DPRINTK("%d no such subdevice\n", user_cmd
.subdev
);
1013 s
= dev
->subdevices
+ user_cmd
.subdev
;
1016 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1017 DPRINTK("%d not valid subdevice\n", user_cmd
.subdev
);
1021 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1022 DPRINTK("subdevice %i does not support commands\n",
1027 /* are we locked? (ioctl lock) */
1028 if (s
->lock
&& s
->lock
!= file
) {
1029 DPRINTK("subdevice locked\n");
1035 DPRINTK("subdevice busy\n");
1040 /* make sure channel/gain list isn't too long */
1041 if (user_cmd
.chanlist_len
> s
->len_chanlist
) {
1042 DPRINTK("channel/gain list too long %u > %d\n",
1043 user_cmd
.chanlist_len
, s
->len_chanlist
);
1048 /* make sure channel/gain list isn't too short */
1049 if (user_cmd
.chanlist_len
< 1) {
1050 DPRINTK("channel/gain list too short %u < 1\n",
1051 user_cmd
.chanlist_len
);
1056 kfree(async
->cmd
.chanlist
);
1057 async
->cmd
= user_cmd
;
1058 async
->cmd
.data
= NULL
;
1059 /* load channel/gain list */
1060 async
->cmd
.chanlist
=
1061 kmalloc(async
->cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1062 if (!async
->cmd
.chanlist
) {
1063 DPRINTK("allocation failed\n");
1068 if (copy_from_user(async
->cmd
.chanlist
, user_cmd
.chanlist
,
1069 async
->cmd
.chanlist_len
* sizeof(int))) {
1070 DPRINTK("fault reading chanlist\n");
1075 /* make sure each element in channel/gain list is valid */
1076 ret
= comedi_check_chanlist(s
,
1077 async
->cmd
.chanlist_len
,
1078 async
->cmd
.chanlist
);
1080 DPRINTK("bad chanlist\n");
1084 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1086 if (async
->cmd
.flags
& TRIG_BOGUS
|| ret
) {
1087 DPRINTK("test returned %d\n", ret
);
1088 user_cmd
= async
->cmd
;
1089 /* restore chanlist pointer before copying back */
1090 user_cmd
.chanlist
= chanlist_saver
;
1091 user_cmd
.data
= NULL
;
1092 if (copy_to_user(cmd
, &user_cmd
, sizeof(struct comedi_cmd
))) {
1093 DPRINTK("fault writing cmd\n");
1101 if (!async
->prealloc_bufsz
) {
1103 DPRINTK("no buffer (?)\n");
1107 comedi_reset_async_buf(async
);
1110 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1112 if (async
->cmd
.flags
& TRIG_WAKE_EOS
)
1113 async
->cb_mask
|= COMEDI_CB_EOS
;
1115 comedi_set_subdevice_runflags(s
, ~0, SRF_USER
| SRF_RUNNING
);
1117 ret
= s
->do_cmd(dev
, s
);
1122 do_become_nonbusy(dev
, s
);
1129 command testing ioctl
1132 pointer to cmd structure
1135 cmd structure at arg
1139 modified cmd structure at arg
1142 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1143 struct comedi_cmd __user
*arg
, void *file
)
1145 struct comedi_cmd user_cmd
;
1146 struct comedi_subdevice
*s
;
1148 unsigned int *chanlist
= NULL
;
1149 unsigned int __user
*chanlist_saver
= NULL
;
1151 if (copy_from_user(&user_cmd
, arg
, sizeof(struct comedi_cmd
))) {
1152 DPRINTK("bad cmd address\n");
1155 /* save user's chanlist pointer so it can be restored later */
1156 chanlist_saver
= user_cmd
.chanlist
;
1158 if (user_cmd
.subdev
>= dev
->n_subdevices
) {
1159 DPRINTK("%d no such subdevice\n", user_cmd
.subdev
);
1163 s
= dev
->subdevices
+ user_cmd
.subdev
;
1164 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1165 DPRINTK("%d not valid subdevice\n", user_cmd
.subdev
);
1169 if (!s
->do_cmd
|| !s
->do_cmdtest
) {
1170 DPRINTK("subdevice %i does not support commands\n",
1175 /* make sure channel/gain list isn't too long */
1176 if (user_cmd
.chanlist_len
> s
->len_chanlist
) {
1177 DPRINTK("channel/gain list too long %d > %d\n",
1178 user_cmd
.chanlist_len
, s
->len_chanlist
);
1183 /* load channel/gain list */
1184 if (user_cmd
.chanlist
) {
1186 kmalloc(user_cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1188 DPRINTK("allocation failed\n");
1193 if (copy_from_user(chanlist
, user_cmd
.chanlist
,
1194 user_cmd
.chanlist_len
* sizeof(int))) {
1195 DPRINTK("fault reading chanlist\n");
1200 /* make sure each element in channel/gain list is valid */
1201 ret
= comedi_check_chanlist(s
, user_cmd
.chanlist_len
, chanlist
);
1203 DPRINTK("bad chanlist\n");
1207 user_cmd
.chanlist
= chanlist
;
1210 ret
= s
->do_cmdtest(dev
, s
, &user_cmd
);
1212 /* restore chanlist pointer before copying back */
1213 user_cmd
.chanlist
= chanlist_saver
;
1215 if (copy_to_user(arg
, &user_cmd
, sizeof(struct comedi_cmd
))) {
1216 DPRINTK("bad cmd address\n");
1241 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1245 unsigned long flags
;
1246 struct comedi_subdevice
*s
;
1248 if (arg
>= dev
->n_subdevices
)
1250 s
= dev
->subdevices
+ arg
;
1252 spin_lock_irqsave(&s
->spin_lock
, flags
);
1253 if (s
->busy
|| s
->lock
)
1257 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1264 ret
= s
->lock_f(dev
, s
);
1283 This function isn't protected by the semaphore, since
1284 we already own the lock.
1286 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1289 struct comedi_subdevice
*s
;
1291 if (arg
>= dev
->n_subdevices
)
1293 s
= dev
->subdevices
+ arg
;
1298 if (s
->lock
&& s
->lock
!= file
)
1301 if (s
->lock
== file
) {
1315 cancel acquisition ioctl
1327 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1330 struct comedi_subdevice
*s
;
1332 if (arg
>= dev
->n_subdevices
)
1334 s
= dev
->subdevices
+ arg
;
1335 if (s
->async
== NULL
)
1338 if (s
->lock
&& s
->lock
!= file
)
1344 if (s
->busy
!= file
)
1347 return do_cancel(dev
, s
);
1352 instructs driver to synchronize buffers
1364 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1367 struct comedi_subdevice
*s
;
1369 if (arg
>= dev
->n_subdevices
)
1371 s
= dev
->subdevices
+ arg
;
1373 if (s
->lock
&& s
->lock
!= file
)
1379 if (s
->busy
!= file
)
1383 return s
->poll(dev
, s
);
1388 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1392 if ((comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) && s
->cancel
)
1393 ret
= s
->cancel(dev
, s
);
1395 do_become_nonbusy(dev
, s
);
1400 static void comedi_unmap(struct vm_area_struct
*area
)
1402 struct comedi_async
*async
;
1403 struct comedi_device
*dev
;
1405 async
= area
->vm_private_data
;
1406 dev
= async
->subdevice
->device
;
1408 mutex_lock(&dev
->mutex
);
1409 async
->mmap_count
--;
1410 mutex_unlock(&dev
->mutex
);
1413 static struct vm_operations_struct comedi_vm_ops
= {
1414 .close
= comedi_unmap
,
1417 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1419 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1420 struct comedi_device_file_info
*dev_file_info
=
1421 comedi_get_device_file_info(minor
);
1422 struct comedi_device
*dev
= dev_file_info
->device
;
1423 struct comedi_async
*async
= NULL
;
1424 unsigned long start
= vma
->vm_start
;
1429 struct comedi_subdevice
*s
;
1431 mutex_lock(&dev
->mutex
);
1432 if (!dev
->attached
) {
1433 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1437 if (vma
->vm_flags
& VM_WRITE
)
1438 s
= comedi_get_write_subdevice(dev_file_info
);
1440 s
= comedi_get_read_subdevice(dev_file_info
);
1447 if (async
== NULL
) {
1452 if (vma
->vm_pgoff
!= 0) {
1453 DPRINTK("comedi: mmap() offset must be 0.\n");
1458 size
= vma
->vm_end
- vma
->vm_start
;
1459 if (size
> async
->prealloc_bufsz
) {
1463 if (size
& (~PAGE_MASK
)) {
1468 n_pages
= size
>> PAGE_SHIFT
;
1469 for (i
= 0; i
< n_pages
; ++i
) {
1470 if (remap_pfn_range(vma
, start
,
1471 page_to_pfn(virt_to_page
1472 (async
->buf_page_list
1473 [i
].virt_addr
)), PAGE_SIZE
,
1481 vma
->vm_ops
= &comedi_vm_ops
;
1482 vma
->vm_private_data
= async
;
1484 async
->mmap_count
++;
1488 mutex_unlock(&dev
->mutex
);
1492 static unsigned int comedi_poll(struct file
*file
, poll_table
* wait
)
1494 unsigned int mask
= 0;
1495 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1496 struct comedi_device_file_info
*dev_file_info
=
1497 comedi_get_device_file_info(minor
);
1498 struct comedi_device
*dev
= dev_file_info
->device
;
1499 struct comedi_subdevice
*read_subdev
;
1500 struct comedi_subdevice
*write_subdev
;
1502 mutex_lock(&dev
->mutex
);
1503 if (!dev
->attached
) {
1504 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1505 mutex_unlock(&dev
->mutex
);
1510 read_subdev
= comedi_get_read_subdevice(dev_file_info
);
1512 poll_wait(file
, &read_subdev
->async
->wait_head
, wait
);
1513 if (!read_subdev
->busy
1514 || comedi_buf_read_n_available(read_subdev
->async
) > 0
1515 || !(comedi_get_subdevice_runflags(read_subdev
) &
1517 mask
|= POLLIN
| POLLRDNORM
;
1520 write_subdev
= comedi_get_write_subdevice(dev_file_info
);
1522 poll_wait(file
, &write_subdev
->async
->wait_head
, wait
);
1523 comedi_buf_write_alloc(write_subdev
->async
,
1524 write_subdev
->async
->prealloc_bufsz
);
1525 if (!write_subdev
->busy
1526 || !(comedi_get_subdevice_runflags(write_subdev
) &
1528 || comedi_buf_write_n_allocated(write_subdev
->async
) >=
1529 bytes_per_sample(write_subdev
->async
->subdevice
)) {
1530 mask
|= POLLOUT
| POLLWRNORM
;
1534 mutex_unlock(&dev
->mutex
);
1538 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
1539 size_t nbytes
, loff_t
*offset
)
1541 struct comedi_subdevice
*s
;
1542 struct comedi_async
*async
;
1543 int n
, m
, count
= 0, retval
= 0;
1544 DECLARE_WAITQUEUE(wait
, current
);
1545 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1546 struct comedi_device_file_info
*dev_file_info
=
1547 comedi_get_device_file_info(minor
);
1548 struct comedi_device
*dev
= dev_file_info
->device
;
1550 if (!dev
->attached
) {
1551 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1556 s
= comedi_get_write_subdevice(dev_file_info
);
1571 if (s
->busy
!= file
) {
1575 add_wait_queue(&async
->wait_head
, &wait
);
1576 while (nbytes
> 0 && !retval
) {
1577 set_current_state(TASK_INTERRUPTIBLE
);
1582 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
1583 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
1584 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
1585 if (m
> comedi_buf_write_n_allocated(async
))
1586 m
= comedi_buf_write_n_allocated(async
);
1591 if (!(comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)) {
1592 if (comedi_get_subdevice_runflags(s
) &
1598 do_become_nonbusy(dev
, s
);
1601 if (file
->f_flags
& O_NONBLOCK
) {
1605 if (signal_pending(current
)) {
1606 retval
= -ERESTARTSYS
;
1612 if (s
->busy
!= file
) {
1619 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
1625 comedi_buf_write_free(async
, n
);
1631 break; /* makes device work like a pipe */
1633 set_current_state(TASK_RUNNING
);
1634 remove_wait_queue(&async
->wait_head
, &wait
);
1637 return count
? count
: retval
;
1640 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
1643 struct comedi_subdevice
*s
;
1644 struct comedi_async
*async
;
1645 int n
, m
, count
= 0, retval
= 0;
1646 DECLARE_WAITQUEUE(wait
, current
);
1647 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1648 struct comedi_device_file_info
*dev_file_info
=
1649 comedi_get_device_file_info(minor
);
1650 struct comedi_device
*dev
= dev_file_info
->device
;
1652 if (!dev
->attached
) {
1653 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1658 s
= comedi_get_read_subdevice(dev_file_info
);
1672 if (s
->busy
!= file
) {
1677 add_wait_queue(&async
->wait_head
, &wait
);
1678 while (nbytes
> 0 && !retval
) {
1679 set_current_state(TASK_INTERRUPTIBLE
);
1683 m
= comedi_buf_read_n_available(async
);
1684 /* printk("%d available\n",m); */
1685 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
1686 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
1687 /* printk("%d contiguous\n",m); */
1692 if (!(comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)) {
1693 do_become_nonbusy(dev
, s
);
1694 if (comedi_get_subdevice_runflags(s
) &
1702 if (file
->f_flags
& O_NONBLOCK
) {
1706 if (signal_pending(current
)) {
1707 retval
= -ERESTARTSYS
;
1715 if (s
->busy
!= file
) {
1721 m
= copy_to_user(buf
, async
->prealloc_buf
+
1722 async
->buf_read_ptr
, n
);
1728 comedi_buf_read_alloc(async
, n
);
1729 comedi_buf_read_free(async
, n
);
1735 break; /* makes device work like a pipe */
1737 if (!(comedi_get_subdevice_runflags(s
) & (SRF_ERROR
| SRF_RUNNING
)) &&
1738 async
->buf_read_count
- async
->buf_write_count
== 0) {
1739 do_become_nonbusy(dev
, s
);
1741 set_current_state(TASK_RUNNING
);
1742 remove_wait_queue(&async
->wait_head
, &wait
);
1745 return count
? count
: retval
;
1749 This function restores a subdevice to an idle state.
1751 void do_become_nonbusy(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1753 struct comedi_async
*async
= s
->async
;
1755 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
1757 comedi_reset_async_buf(async
);
1758 async
->inttrig
= NULL
;
1761 "BUG: (?) do_become_nonbusy called with async=0\n");
1767 static int comedi_open(struct inode
*inode
, struct file
*file
)
1769 const unsigned minor
= iminor(inode
);
1770 struct comedi_device_file_info
*dev_file_info
=
1771 comedi_get_device_file_info(minor
);
1772 struct comedi_device
*dev
=
1773 dev_file_info
? dev_file_info
->device
: NULL
;
1776 DPRINTK("invalid minor number\n");
1780 /* This is slightly hacky, but we want module autoloading
1782 * case: user opens device, attached -> ok
1783 * case: user opens device, unattached, in_request_module=0 -> autoload
1784 * case: user opens device, unattached, in_request_module=1 -> fail
1785 * case: root opens device, attached -> ok
1786 * case: root opens device, unattached, in_request_module=1 -> ok
1787 * (typically called from modprobe)
1788 * case: root opens device, unattached, in_request_module=0 -> autoload
1790 * The last could be changed to "-> ok", which would deny root
1793 mutex_lock(&dev
->mutex
);
1796 if (!capable(CAP_NET_ADMIN
) && dev
->in_request_module
) {
1797 DPRINTK("in request module\n");
1798 mutex_unlock(&dev
->mutex
);
1801 if (capable(CAP_NET_ADMIN
) && dev
->in_request_module
)
1804 dev
->in_request_module
= 1;
1807 mutex_unlock(&dev
->mutex
);
1808 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
1809 mutex_lock(&dev
->mutex
);
1812 dev
->in_request_module
= 0;
1814 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
1815 DPRINTK("not attached and not CAP_NET_ADMIN\n");
1816 mutex_unlock(&dev
->mutex
);
1820 __module_get(THIS_MODULE
);
1822 if (dev
->attached
) {
1823 if (!try_module_get(dev
->driver
->module
)) {
1824 module_put(THIS_MODULE
);
1825 mutex_unlock(&dev
->mutex
);
1830 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
)
1835 mutex_unlock(&dev
->mutex
);
1840 static int comedi_close(struct inode
*inode
, struct file
*file
)
1842 const unsigned minor
= iminor(inode
);
1843 struct comedi_device_file_info
*dev_file_info
=
1844 comedi_get_device_file_info(minor
);
1845 struct comedi_device
*dev
= dev_file_info
->device
;
1846 struct comedi_subdevice
*s
= NULL
;
1849 mutex_lock(&dev
->mutex
);
1851 if (dev
->subdevices
) {
1852 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
1853 s
= dev
->subdevices
+ i
;
1855 if (s
->busy
== file
)
1857 if (s
->lock
== file
)
1861 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
1864 module_put(THIS_MODULE
);
1866 module_put(dev
->driver
->module
);
1870 mutex_unlock(&dev
->mutex
);
1872 if (file
->f_flags
& FASYNC
)
1873 comedi_fasync(-1, file
, 0);
1878 static int comedi_fasync(int fd
, struct file
*file
, int on
)
1880 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1881 struct comedi_device_file_info
*dev_file_info
=
1882 comedi_get_device_file_info(minor
);
1884 struct comedi_device
*dev
= dev_file_info
->device
;
1886 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
1889 const struct file_operations comedi_fops
= {
1890 .owner
= THIS_MODULE
,
1891 .unlocked_ioctl
= comedi_unlocked_ioctl
,
1892 .compat_ioctl
= comedi_compat_ioctl
,
1893 .open
= comedi_open
,
1894 .release
= comedi_close
,
1895 .read
= comedi_read
,
1896 .write
= comedi_write
,
1897 .mmap
= comedi_mmap
,
1898 .poll
= comedi_poll
,
1899 .fasync
= comedi_fasync
,
1902 struct class *comedi_class
;
1903 static struct cdev comedi_cdev
;
1905 static void comedi_cleanup_legacy_minors(void)
1909 for (i
= 0; i
< comedi_num_legacy_minors
; i
++)
1910 comedi_free_board_minor(i
);
1913 static int __init
comedi_init(void)
1918 printk(KERN_INFO
"comedi: version " COMEDI_RELEASE
1919 " - http://www.comedi.org\n");
1921 if (comedi_num_legacy_minors
< 0 ||
1922 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
1923 printk(KERN_ERR
"comedi: error: invalid value for module "
1924 "parameter \"comedi_num_legacy_minors\". Valid values "
1925 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS
);
1930 * comedi is unusable if both comedi_autoconfig and
1931 * comedi_num_legacy_minors are zero, so we might as well adjust the
1932 * defaults in that case
1934 if (comedi_autoconfig
== 0 && comedi_num_legacy_minors
== 0)
1935 comedi_num_legacy_minors
= 16;
1937 memset(comedi_file_info_table
, 0,
1938 sizeof(struct comedi_device_file_info
*) * COMEDI_NUM_MINORS
);
1940 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1941 COMEDI_NUM_MINORS
, "comedi");
1944 cdev_init(&comedi_cdev
, &comedi_fops
);
1945 comedi_cdev
.owner
= THIS_MODULE
;
1946 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
1947 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
1948 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1952 comedi_class
= class_create(THIS_MODULE
, "comedi");
1953 if (IS_ERR(comedi_class
)) {
1954 printk(KERN_ERR
"comedi: failed to create class");
1955 cdev_del(&comedi_cdev
);
1956 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1958 return PTR_ERR(comedi_class
);
1961 /* XXX requires /proc interface */
1964 /* create devices files for legacy/manual use */
1965 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
1967 minor
= comedi_alloc_board_minor(NULL
);
1969 comedi_cleanup_legacy_minors();
1970 cdev_del(&comedi_cdev
);
1971 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
1980 static void __exit
comedi_cleanup(void)
1984 comedi_cleanup_legacy_minors();
1985 for (i
= 0; i
< COMEDI_NUM_MINORS
; ++i
)
1986 BUG_ON(comedi_file_info_table
[i
]);
1988 class_destroy(comedi_class
);
1989 cdev_del(&comedi_cdev
);
1990 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
1992 comedi_proc_cleanup();
1995 module_init(comedi_init
);
1996 module_exit(comedi_cleanup
);
1998 void comedi_error(const struct comedi_device
*dev
, const char *s
)
2000 printk(KERN_ERR
"comedi%d: %s: %s\n", dev
->minor
,
2001 dev
->driver
->driver_name
, s
);
2003 EXPORT_SYMBOL(comedi_error
);
2005 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2007 struct comedi_async
*async
= s
->async
;
2008 unsigned runflags
= 0;
2009 unsigned runflags_mask
= 0;
2011 /* DPRINTK("comedi_event 0x%x\n",mask); */
2013 if ((comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) == 0)
2017 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
2018 COMEDI_CB_OVERFLOW
)) {
2019 runflags_mask
|= SRF_RUNNING
;
2021 /* remember if an error event has occured, so an error
2022 * can be returned the next time the user does a read() */
2023 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2024 runflags_mask
|= SRF_ERROR
;
2025 runflags
|= SRF_ERROR
;
2027 if (runflags_mask
) {
2028 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2029 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2032 if (async
->cb_mask
& s
->async
->events
) {
2033 if (comedi_get_subdevice_runflags(s
) & SRF_USER
) {
2034 wake_up_interruptible(&async
->wait_head
);
2035 if (s
->subdev_flags
& SDF_CMD_READ
)
2036 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2037 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2038 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2041 async
->cb_func(s
->async
->events
, async
->cb_arg
);
2044 s
->async
->events
= 0;
2046 EXPORT_SYMBOL(comedi_event
);
2048 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
2050 unsigned long flags
;
2053 spin_lock_irqsave(&s
->spin_lock
, flags
);
2054 runflags
= s
->runflags
;
2055 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2058 EXPORT_SYMBOL(comedi_get_subdevice_runflags
);
2060 static int is_device_busy(struct comedi_device
*dev
)
2062 struct comedi_subdevice
*s
;
2068 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2069 s
= dev
->subdevices
+ i
;
2072 if (s
->async
&& s
->async
->mmap_count
)
2079 static void comedi_device_init(struct comedi_device
*dev
)
2081 memset(dev
, 0, sizeof(struct comedi_device
));
2082 spin_lock_init(&dev
->spinlock
);
2083 mutex_init(&dev
->mutex
);
2087 static void comedi_device_cleanup(struct comedi_device
*dev
)
2091 mutex_lock(&dev
->mutex
);
2092 comedi_device_detach(dev
);
2093 mutex_unlock(&dev
->mutex
);
2094 mutex_destroy(&dev
->mutex
);
2097 int comedi_alloc_board_minor(struct device
*hardware_device
)
2099 unsigned long flags
;
2100 struct comedi_device_file_info
*info
;
2101 struct device
*csdev
;
2105 info
= kzalloc(sizeof(struct comedi_device_file_info
), GFP_KERNEL
);
2108 info
->device
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2109 if (info
->device
== NULL
) {
2113 comedi_device_init(info
->device
);
2114 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2115 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2116 if (comedi_file_info_table
[i
] == NULL
) {
2117 comedi_file_info_table
[i
] = info
;
2121 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2122 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2123 comedi_device_cleanup(info
->device
);
2124 kfree(info
->device
);
2128 "ran out of minor numbers for board device files.\n");
2131 info
->device
->minor
= i
;
2132 csdev
= COMEDI_DEVICE_CREATE(comedi_class
, NULL
,
2133 MKDEV(COMEDI_MAJOR
, i
), NULL
,
2134 hardware_device
, "comedi%i", i
);
2136 info
->device
->class_dev
= csdev
;
2137 dev_set_drvdata(csdev
, info
);
2138 retval
= device_create_file(csdev
, &dev_attr_max_read_buffer_kb
);
2142 "failed to create sysfs attribute file \"%s\".\n",
2143 dev_attr_max_read_buffer_kb
.attr
.name
);
2144 comedi_free_board_minor(i
);
2147 retval
= device_create_file(csdev
, &dev_attr_read_buffer_kb
);
2151 "failed to create sysfs attribute file \"%s\".\n",
2152 dev_attr_read_buffer_kb
.attr
.name
);
2153 comedi_free_board_minor(i
);
2156 retval
= device_create_file(csdev
, &dev_attr_max_write_buffer_kb
);
2160 "failed to create sysfs attribute file \"%s\".\n",
2161 dev_attr_max_write_buffer_kb
.attr
.name
);
2162 comedi_free_board_minor(i
);
2165 retval
= device_create_file(csdev
, &dev_attr_write_buffer_kb
);
2169 "failed to create sysfs attribute file \"%s\".\n",
2170 dev_attr_write_buffer_kb
.attr
.name
);
2171 comedi_free_board_minor(i
);
2177 void comedi_free_board_minor(unsigned minor
)
2179 unsigned long flags
;
2180 struct comedi_device_file_info
*info
;
2182 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2183 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2184 info
= comedi_file_info_table
[minor
];
2185 comedi_file_info_table
[minor
] = NULL
;
2186 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2189 struct comedi_device
*dev
= info
->device
;
2191 if (dev
->class_dev
) {
2192 device_destroy(comedi_class
,
2193 MKDEV(COMEDI_MAJOR
, dev
->minor
));
2195 comedi_device_cleanup(dev
);
2202 int comedi_alloc_subdevice_minor(struct comedi_device
*dev
,
2203 struct comedi_subdevice
*s
)
2205 unsigned long flags
;
2206 struct comedi_device_file_info
*info
;
2207 struct device
*csdev
;
2211 info
= kmalloc(sizeof(struct comedi_device_file_info
), GFP_KERNEL
);
2215 info
->read_subdevice
= s
;
2216 info
->write_subdevice
= s
;
2217 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2218 for (i
= COMEDI_FIRST_SUBDEVICE_MINOR
; i
< COMEDI_NUM_MINORS
; ++i
) {
2219 if (comedi_file_info_table
[i
] == NULL
) {
2220 comedi_file_info_table
[i
] = info
;
2224 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2225 if (i
== COMEDI_NUM_MINORS
) {
2229 "ran out of minor numbers for board device files.\n");
2233 csdev
= COMEDI_DEVICE_CREATE(comedi_class
, dev
->class_dev
,
2234 MKDEV(COMEDI_MAJOR
, i
), NULL
, NULL
,
2235 "comedi%i_subd%i", dev
->minor
,
2236 (int)(s
- dev
->subdevices
));
2238 s
->class_dev
= csdev
;
2239 dev_set_drvdata(csdev
, info
);
2240 retval
= device_create_file(csdev
, &dev_attr_max_read_buffer_kb
);
2244 "failed to create sysfs attribute file \"%s\".\n",
2245 dev_attr_max_read_buffer_kb
.attr
.name
);
2246 comedi_free_subdevice_minor(s
);
2249 retval
= device_create_file(csdev
, &dev_attr_read_buffer_kb
);
2253 "failed to create sysfs attribute file \"%s\".\n",
2254 dev_attr_read_buffer_kb
.attr
.name
);
2255 comedi_free_subdevice_minor(s
);
2258 retval
= device_create_file(csdev
, &dev_attr_max_write_buffer_kb
);
2262 "failed to create sysfs attribute file \"%s\".\n",
2263 dev_attr_max_write_buffer_kb
.attr
.name
);
2264 comedi_free_subdevice_minor(s
);
2267 retval
= device_create_file(csdev
, &dev_attr_write_buffer_kb
);
2271 "failed to create sysfs attribute file \"%s\".\n",
2272 dev_attr_write_buffer_kb
.attr
.name
);
2273 comedi_free_subdevice_minor(s
);
2279 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2281 unsigned long flags
;
2282 struct comedi_device_file_info
*info
;
2289 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2290 BUG_ON(s
->minor
< COMEDI_FIRST_SUBDEVICE_MINOR
);
2292 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2293 info
= comedi_file_info_table
[s
->minor
];
2294 comedi_file_info_table
[s
->minor
] = NULL
;
2295 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2298 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2299 s
->class_dev
= NULL
;
2304 struct comedi_device_file_info
*comedi_get_device_file_info(unsigned minor
)
2306 unsigned long flags
;
2307 struct comedi_device_file_info
*info
;
2309 BUG_ON(minor
>= COMEDI_NUM_MINORS
);
2310 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2311 info
= comedi_file_info_table
[minor
];
2312 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2315 EXPORT_SYMBOL_GPL(comedi_get_device_file_info
);
2317 static int resize_async_buffer(struct comedi_device
*dev
,
2318 struct comedi_subdevice
*s
,
2319 struct comedi_async
*async
, unsigned new_size
)
2323 if (new_size
> async
->max_bufsize
)
2327 DPRINTK("subdevice is busy, cannot resize buffer\n");
2330 if (async
->mmap_count
) {
2331 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2335 if (!async
->prealloc_buf
)
2338 /* make sure buffer is an integral number of pages
2340 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2342 retval
= comedi_buf_alloc(dev
, s
, new_size
);
2346 if (s
->buf_change
) {
2347 retval
= s
->buf_change(dev
, s
, new_size
);
2352 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2353 dev
->minor
, (int)(s
- dev
->subdevices
), async
->prealloc_bufsz
);
2357 /* sysfs attribute files */
2359 static const unsigned bytes_per_kibi
= 1024;
2361 static ssize_t
show_max_read_buffer_kb(struct device
*dev
,
2362 struct device_attribute
*attr
, char *buf
)
2365 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2366 unsigned max_buffer_size_kb
= 0;
2367 struct comedi_subdevice
*const read_subdevice
=
2368 comedi_get_read_subdevice(info
);
2370 mutex_lock(&info
->device
->mutex
);
2371 if (read_subdevice
&&
2372 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) &&
2373 read_subdevice
->async
) {
2374 max_buffer_size_kb
= read_subdevice
->async
->max_bufsize
/
2377 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", max_buffer_size_kb
);
2378 mutex_unlock(&info
->device
->mutex
);
2383 static ssize_t
store_max_read_buffer_kb(struct device
*dev
,
2384 struct device_attribute
*attr
,
2385 const char *buf
, size_t count
)
2387 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2388 unsigned long new_max_size_kb
;
2389 uint64_t new_max_size
;
2390 struct comedi_subdevice
*const read_subdevice
=
2391 comedi_get_read_subdevice(info
);
2393 if (strict_strtoul(buf
, 10, &new_max_size_kb
))
2395 if (new_max_size_kb
!= (uint32_t) new_max_size_kb
)
2397 new_max_size
= ((uint64_t) new_max_size_kb
) * bytes_per_kibi
;
2398 if (new_max_size
!= (uint32_t) new_max_size
)
2401 mutex_lock(&info
->device
->mutex
);
2402 if (read_subdevice
== NULL
||
2403 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) == 0 ||
2404 read_subdevice
->async
== NULL
) {
2405 mutex_unlock(&info
->device
->mutex
);
2408 read_subdevice
->async
->max_bufsize
= new_max_size
;
2409 mutex_unlock(&info
->device
->mutex
);
2414 static struct device_attribute dev_attr_max_read_buffer_kb
= {
2416 .name
= "max_read_buffer_kb",
2417 .mode
= S_IRUGO
| S_IWUSR
},
2418 .show
= &show_max_read_buffer_kb
,
2419 .store
= &store_max_read_buffer_kb
2422 static ssize_t
show_read_buffer_kb(struct device
*dev
,
2423 struct device_attribute
*attr
, char *buf
)
2426 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2427 unsigned buffer_size_kb
= 0;
2428 struct comedi_subdevice
*const read_subdevice
=
2429 comedi_get_read_subdevice(info
);
2431 mutex_lock(&info
->device
->mutex
);
2432 if (read_subdevice
&&
2433 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) &&
2434 read_subdevice
->async
) {
2435 buffer_size_kb
= read_subdevice
->async
->prealloc_bufsz
/
2438 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", buffer_size_kb
);
2439 mutex_unlock(&info
->device
->mutex
);
2444 static ssize_t
store_read_buffer_kb(struct device
*dev
,
2445 struct device_attribute
*attr
,
2446 const char *buf
, size_t count
)
2448 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2449 unsigned long new_size_kb
;
2452 struct comedi_subdevice
*const read_subdevice
=
2453 comedi_get_read_subdevice(info
);
2455 if (strict_strtoul(buf
, 10, &new_size_kb
))
2457 if (new_size_kb
!= (uint32_t) new_size_kb
)
2459 new_size
= ((uint64_t) new_size_kb
) * bytes_per_kibi
;
2460 if (new_size
!= (uint32_t) new_size
)
2463 mutex_lock(&info
->device
->mutex
);
2464 if (read_subdevice
== NULL
||
2465 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) == 0 ||
2466 read_subdevice
->async
== NULL
) {
2467 mutex_unlock(&info
->device
->mutex
);
2470 retval
= resize_async_buffer(info
->device
, read_subdevice
,
2471 read_subdevice
->async
, new_size
);
2472 mutex_unlock(&info
->device
->mutex
);
2479 static struct device_attribute dev_attr_read_buffer_kb
= {
2481 .name
= "read_buffer_kb",
2482 .mode
= S_IRUGO
| S_IWUSR
| S_IWGRP
},
2483 .show
= &show_read_buffer_kb
,
2484 .store
= &store_read_buffer_kb
2487 static ssize_t
show_max_write_buffer_kb(struct device
*dev
,
2488 struct device_attribute
*attr
,
2492 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2493 unsigned max_buffer_size_kb
= 0;
2494 struct comedi_subdevice
*const write_subdevice
=
2495 comedi_get_write_subdevice(info
);
2497 mutex_lock(&info
->device
->mutex
);
2498 if (write_subdevice
&&
2499 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) &&
2500 write_subdevice
->async
) {
2501 max_buffer_size_kb
= write_subdevice
->async
->max_bufsize
/
2504 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", max_buffer_size_kb
);
2505 mutex_unlock(&info
->device
->mutex
);
2510 static ssize_t
store_max_write_buffer_kb(struct device
*dev
,
2511 struct device_attribute
*attr
,
2512 const char *buf
, size_t count
)
2514 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2515 unsigned long new_max_size_kb
;
2516 uint64_t new_max_size
;
2517 struct comedi_subdevice
*const write_subdevice
=
2518 comedi_get_write_subdevice(info
);
2520 if (strict_strtoul(buf
, 10, &new_max_size_kb
))
2522 if (new_max_size_kb
!= (uint32_t) new_max_size_kb
)
2524 new_max_size
= ((uint64_t) new_max_size_kb
) * bytes_per_kibi
;
2525 if (new_max_size
!= (uint32_t) new_max_size
)
2528 mutex_lock(&info
->device
->mutex
);
2529 if (write_subdevice
== NULL
||
2530 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) == 0 ||
2531 write_subdevice
->async
== NULL
) {
2532 mutex_unlock(&info
->device
->mutex
);
2535 write_subdevice
->async
->max_bufsize
= new_max_size
;
2536 mutex_unlock(&info
->device
->mutex
);
2541 static struct device_attribute dev_attr_max_write_buffer_kb
= {
2543 .name
= "max_write_buffer_kb",
2544 .mode
= S_IRUGO
| S_IWUSR
},
2545 .show
= &show_max_write_buffer_kb
,
2546 .store
= &store_max_write_buffer_kb
2549 static ssize_t
show_write_buffer_kb(struct device
*dev
,
2550 struct device_attribute
*attr
, char *buf
)
2553 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2554 unsigned buffer_size_kb
= 0;
2555 struct comedi_subdevice
*const write_subdevice
=
2556 comedi_get_write_subdevice(info
);
2558 mutex_lock(&info
->device
->mutex
);
2559 if (write_subdevice
&&
2560 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) &&
2561 write_subdevice
->async
) {
2562 buffer_size_kb
= write_subdevice
->async
->prealloc_bufsz
/
2565 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", buffer_size_kb
);
2566 mutex_unlock(&info
->device
->mutex
);
2571 static ssize_t
store_write_buffer_kb(struct device
*dev
,
2572 struct device_attribute
*attr
,
2573 const char *buf
, size_t count
)
2575 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2576 unsigned long new_size_kb
;
2579 struct comedi_subdevice
*const write_subdevice
=
2580 comedi_get_write_subdevice(info
);
2582 if (strict_strtoul(buf
, 10, &new_size_kb
))
2584 if (new_size_kb
!= (uint32_t) new_size_kb
)
2586 new_size
= ((uint64_t) new_size_kb
) * bytes_per_kibi
;
2587 if (new_size
!= (uint32_t) new_size
)
2590 mutex_lock(&info
->device
->mutex
);
2591 if (write_subdevice
== NULL
||
2592 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) == 0 ||
2593 write_subdevice
->async
== NULL
) {
2594 mutex_unlock(&info
->device
->mutex
);
2597 retval
= resize_async_buffer(info
->device
, write_subdevice
,
2598 write_subdevice
->async
, new_size
);
2599 mutex_unlock(&info
->device
->mutex
);
2606 static struct device_attribute dev_attr_write_buffer_kb
= {
2608 .name
= "write_buffer_kb",
2609 .mode
= S_IRUGO
| S_IWUSR
| S_IWGRP
},
2610 .show
= &show_write_buffer_kb
,
2611 .store
= &store_write_buffer_kb