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 bool 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
, void *file
);
87 static int do_cmd_ioctl(struct comedi_device
*dev
,
88 struct comedi_cmd __user
*arg
, void *file
);
89 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
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 static 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
,
176 rc
= do_lock_ioctl(dev
, arg
, file
);
179 rc
= do_unlock_ioctl(dev
, arg
, file
);
182 rc
= do_cancel_ioctl(dev
, arg
, file
);
185 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
188 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
191 case COMEDI_INSNLIST
:
192 rc
= do_insnlist_ioctl(dev
,
193 (struct comedi_insnlist __user
*)arg
,
197 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
201 rc
= do_poll_ioctl(dev
, arg
, file
);
209 mutex_unlock(&dev
->mutex
);
218 pointer to devconfig structure
221 devconfig structure at arg
226 static int do_devconfig_ioctl(struct comedi_device
*dev
,
227 struct comedi_devconfig __user
*arg
)
229 struct comedi_devconfig it
;
231 unsigned char *aux_data
= NULL
;
234 if (!capable(CAP_SYS_ADMIN
))
238 if (is_device_busy(dev
))
241 struct module
*driver_module
= dev
->driver
->module
;
242 comedi_device_detach(dev
);
243 module_put(driver_module
);
248 if (copy_from_user(&it
, arg
, sizeof(struct comedi_devconfig
)))
251 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
253 if (comedi_aux_data(it
.options
, 0) &&
254 it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
256 aux_len
= it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
];
260 aux_data
= vmalloc(aux_len
);
264 if (copy_from_user(aux_data
,
265 comedi_aux_data(it
.options
, 0), aux_len
)) {
269 it
.options
[COMEDI_DEVCONF_AUX_DATA_LO
] =
270 (unsigned long)aux_data
;
271 if (sizeof(void *) > sizeof(int)) {
272 bit_shift
= sizeof(int) * 8;
273 it
.options
[COMEDI_DEVCONF_AUX_DATA_HI
] =
274 ((unsigned long)aux_data
) >> bit_shift
;
276 it
.options
[COMEDI_DEVCONF_AUX_DATA_HI
] = 0;
279 ret
= comedi_device_attach(dev
, &it
);
281 if (!try_module_get(dev
->driver
->module
)) {
282 comedi_device_detach(dev
);
295 buffer configuration ioctl
298 pointer to bufconfig structure
304 modified bufconfig at arg
307 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
308 struct comedi_bufconfig __user
*arg
)
310 struct comedi_bufconfig bc
;
311 struct comedi_async
*async
;
312 struct comedi_subdevice
*s
;
315 if (copy_from_user(&bc
, arg
, sizeof(struct comedi_bufconfig
)))
318 if (bc
.subdevice
>= dev
->n_subdevices
|| bc
.subdevice
< 0)
321 s
= dev
->subdevices
+ bc
.subdevice
;
325 DPRINTK("subdevice does not have async capability\n");
331 if (bc
.maximum_size
) {
332 if (!capable(CAP_SYS_ADMIN
))
335 async
->max_bufsize
= bc
.maximum_size
;
339 retval
= resize_async_buffer(dev
, s
, async
, bc
.size
);
344 bc
.size
= async
->prealloc_bufsz
;
345 bc
.maximum_size
= async
->max_bufsize
;
348 if (copy_to_user(arg
, &bc
, sizeof(struct comedi_bufconfig
)))
359 pointer to devinfo structure
368 static int do_devinfo_ioctl(struct comedi_device
*dev
,
369 struct comedi_devinfo __user
*arg
,
372 struct comedi_devinfo devinfo
;
373 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
374 struct comedi_device_file_info
*dev_file_info
=
375 comedi_get_device_file_info(minor
);
376 struct comedi_subdevice
*read_subdev
=
377 comedi_get_read_subdevice(dev_file_info
);
378 struct comedi_subdevice
*write_subdev
=
379 comedi_get_write_subdevice(dev_file_info
);
381 memset(&devinfo
, 0, sizeof(devinfo
));
383 /* fill devinfo structure */
384 devinfo
.version_code
= COMEDI_VERSION_CODE
;
385 devinfo
.n_subdevs
= dev
->n_subdevices
;
386 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
387 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
390 devinfo
.read_subdevice
= read_subdev
- dev
->subdevices
;
392 devinfo
.read_subdevice
= -1;
395 devinfo
.write_subdevice
= write_subdev
- dev
->subdevices
;
397 devinfo
.write_subdevice
= -1;
399 if (copy_to_user(arg
, &devinfo
, sizeof(struct comedi_devinfo
)))
410 pointer to array of subdevice info structures
416 array of subdevice info structures at arg
419 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
420 struct comedi_subdinfo __user
*arg
, void *file
)
423 struct comedi_subdinfo
*tmp
, *us
;
424 struct comedi_subdevice
*s
;
427 kcalloc(dev
->n_subdevices
, sizeof(struct comedi_subdinfo
),
432 /* fill subdinfo structs */
433 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
434 s
= dev
->subdevices
+ i
;
438 us
->n_chan
= s
->n_chan
;
439 us
->subd_flags
= s
->subdev_flags
;
440 if (comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)
441 us
->subd_flags
|= SDF_RUNNING
;
442 #define TIMER_nanosec 5 /* backwards compatibility */
443 us
->timer_type
= TIMER_nanosec
;
444 us
->len_chanlist
= s
->len_chanlist
;
445 us
->maxdata
= s
->maxdata
;
446 if (s
->range_table
) {
448 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
450 us
->range_type
= 0; /* XXX */
452 us
->flags
= s
->flags
;
455 us
->subd_flags
|= SDF_BUSY
;
457 us
->subd_flags
|= SDF_BUSY_OWNER
;
459 us
->subd_flags
|= SDF_LOCKED
;
461 us
->subd_flags
|= SDF_LOCK_OWNER
;
462 if (!s
->maxdata
&& s
->maxdata_list
)
463 us
->subd_flags
|= SDF_MAXDATA
;
465 us
->subd_flags
|= SDF_FLAGS
;
466 if (s
->range_table_list
)
467 us
->subd_flags
|= SDF_RANGETYPE
;
469 us
->subd_flags
|= SDF_CMD
;
471 if (s
->insn_bits
!= &insn_inval
)
472 us
->insn_bits_support
= COMEDI_SUPPORTED
;
474 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
476 us
->settling_time_0
= s
->settling_time_0
;
479 ret
= copy_to_user(arg
, tmp
,
480 dev
->n_subdevices
* sizeof(struct comedi_subdinfo
));
484 return ret
? -EFAULT
: 0;
492 pointer to chaninfo structure
495 chaninfo structure at arg
498 arrays at elements of chaninfo structure
501 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
502 struct comedi_chaninfo __user
*arg
)
504 struct comedi_subdevice
*s
;
505 struct comedi_chaninfo it
;
507 if (copy_from_user(&it
, arg
, sizeof(struct comedi_chaninfo
)))
510 if (it
.subdev
>= dev
->n_subdevices
)
512 s
= dev
->subdevices
+ it
.subdev
;
514 if (it
.maxdata_list
) {
515 if (s
->maxdata
|| !s
->maxdata_list
)
517 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
518 s
->n_chan
* sizeof(unsigned int)))
525 if (copy_to_user(it
.flaglist
, s
->flaglist
,
526 s
->n_chan
* sizeof(unsigned int)))
533 if (!s
->range_table_list
)
535 for (i
= 0; i
< s
->n_chan
; i
++) {
538 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
539 (s
->range_table_list
[i
]->length
);
540 if (put_user(x
, it
.rangelist
+ i
))
544 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
545 s
->n_chan
* sizeof(unsigned int)))
555 buffer information ioctl
558 pointer to bufinfo structure
564 modified bufinfo at arg
567 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
568 struct comedi_bufinfo __user
*arg
, void *file
)
570 struct comedi_bufinfo bi
;
571 struct comedi_subdevice
*s
;
572 struct comedi_async
*async
;
574 if (copy_from_user(&bi
, arg
, sizeof(struct comedi_bufinfo
)))
577 if (bi
.subdevice
>= dev
->n_subdevices
|| bi
.subdevice
< 0)
580 s
= dev
->subdevices
+ bi
.subdevice
;
582 if (s
->lock
&& s
->lock
!= file
)
588 DPRINTK("subdevice does not have async capability\n");
589 bi
.buf_write_ptr
= 0;
591 bi
.buf_write_count
= 0;
592 bi
.buf_read_count
= 0;
594 bi
.bytes_written
= 0;
599 bi
.bytes_written
= 0;
600 goto copyback_position
;
605 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
606 bi
.bytes_read
= comedi_buf_read_alloc(async
, bi
.bytes_read
);
607 comedi_buf_read_free(async
, bi
.bytes_read
);
609 if (!(comedi_get_subdevice_runflags(s
) & (SRF_ERROR
|
611 && async
->buf_write_count
== async
->buf_read_count
) {
612 do_become_nonbusy(dev
, s
);
616 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
618 comedi_buf_write_alloc(async
, bi
.bytes_written
);
619 comedi_buf_write_free(async
, bi
.bytes_written
);
623 bi
.buf_write_count
= async
->buf_write_count
;
624 bi
.buf_write_ptr
= async
->buf_write_ptr
;
625 bi
.buf_read_count
= async
->buf_read_count
;
626 bi
.buf_read_ptr
= async
->buf_read_ptr
;
629 if (copy_to_user(arg
, &bi
, sizeof(struct comedi_bufinfo
)))
635 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
636 unsigned int *data
, void *file
);
639 * synchronous instructions
642 * pointer to sync cmd structure
645 * sync cmd struct at arg
652 /* arbitrary limits */
653 #define MAX_SAMPLES 256
654 static int do_insnlist_ioctl(struct comedi_device
*dev
,
655 struct comedi_insnlist __user
*arg
, void *file
)
657 struct comedi_insnlist insnlist
;
658 struct comedi_insn
*insns
= NULL
;
659 unsigned int *data
= NULL
;
663 if (copy_from_user(&insnlist
, arg
, sizeof(struct comedi_insnlist
)))
666 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
668 DPRINTK("kmalloc failed\n");
674 kcalloc(insnlist
.n_insns
, sizeof(struct comedi_insn
), GFP_KERNEL
);
676 DPRINTK("kmalloc failed\n");
681 if (copy_from_user(insns
, insnlist
.insns
,
682 sizeof(struct comedi_insn
) * insnlist
.n_insns
)) {
683 DPRINTK("copy_from_user failed\n");
688 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
689 if (insns
[i
].n
> MAX_SAMPLES
) {
690 DPRINTK("number of samples too large\n");
694 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
695 if (copy_from_user(data
, insns
[i
].data
,
696 insns
[i
].n
* sizeof(unsigned int))) {
697 DPRINTK("copy_from_user failed\n");
702 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
705 if (insns
[i
].insn
& INSN_MASK_READ
) {
706 if (copy_to_user(insns
[i
].data
, data
,
707 insns
[i
].n
* sizeof(unsigned int))) {
708 DPRINTK("copy_to_user failed\n");
726 static int check_insn_config_length(struct comedi_insn
*insn
,
733 case INSN_CONFIG_DIO_OUTPUT
:
734 case INSN_CONFIG_DIO_INPUT
:
735 case INSN_CONFIG_DISARM
:
736 case INSN_CONFIG_RESET
:
740 case INSN_CONFIG_ARM
:
741 case INSN_CONFIG_DIO_QUERY
:
742 case INSN_CONFIG_BLOCK_SIZE
:
743 case INSN_CONFIG_FILTER
:
744 case INSN_CONFIG_SERIAL_CLOCK
:
745 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
746 case INSN_CONFIG_ALT_SOURCE
:
747 case INSN_CONFIG_SET_COUNTER_MODE
:
748 case INSN_CONFIG_8254_READ_STATUS
:
749 case INSN_CONFIG_SET_ROUTING
:
750 case INSN_CONFIG_GET_ROUTING
:
751 case INSN_CONFIG_GET_PWM_STATUS
:
752 case INSN_CONFIG_PWM_SET_PERIOD
:
753 case INSN_CONFIG_PWM_GET_PERIOD
:
757 case INSN_CONFIG_SET_GATE_SRC
:
758 case INSN_CONFIG_GET_GATE_SRC
:
759 case INSN_CONFIG_SET_CLOCK_SRC
:
760 case INSN_CONFIG_GET_CLOCK_SRC
:
761 case INSN_CONFIG_SET_OTHER_SRC
:
762 case INSN_CONFIG_GET_COUNTER_STATUS
:
763 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
764 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
765 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
769 case INSN_CONFIG_PWM_OUTPUT
:
770 case INSN_CONFIG_ANALOG_TRIG
:
774 /* by default we allow the insn since we don't have checks for
775 * all possible cases yet */
778 "comedi: no check for data length of config insn id "
779 "%i is implemented.\n"
780 " Add a check to %s in %s.\n"
781 " Assuming n=%i is correct.\n", data
[0], __func__
,
789 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
790 unsigned int *data
, void *file
)
792 struct comedi_subdevice
*s
;
796 if (insn
->insn
& INSN_MASK_SPECIAL
) {
797 /* a non-subdevice instruction */
799 switch (insn
->insn
) {
809 do_gettimeofday(&tv
);
811 data
[1] = tv
.tv_usec
;
817 if (insn
->n
!= 1 || data
[0] >= 100000) {
821 udelay(data
[0] / 1000);
829 if (insn
->subdev
>= dev
->n_subdevices
) {
830 DPRINTK("%d not usable subdevice\n",
835 s
= dev
->subdevices
+ insn
->subdev
;
837 DPRINTK("no async\n");
841 if (!s
->async
->inttrig
) {
842 DPRINTK("no inttrig\n");
846 ret
= s
->async
->inttrig(dev
, s
, insn
->data
[0]);
851 DPRINTK("invalid insn\n");
856 /* a subdevice instruction */
857 unsigned int maxdata
;
859 if (insn
->subdev
>= dev
->n_subdevices
) {
860 DPRINTK("subdevice %d out of range\n", insn
->subdev
);
864 s
= dev
->subdevices
+ insn
->subdev
;
866 if (s
->type
== COMEDI_SUBD_UNUSED
) {
867 DPRINTK("%d not usable subdevice\n", insn
->subdev
);
872 /* are we locked? (ioctl lock) */
873 if (s
->lock
&& s
->lock
!= file
) {
874 DPRINTK("device locked\n");
879 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
882 DPRINTK("bad chanspec\n");
890 /* This looks arbitrary. It is. */
891 s
->busy
= &parse_insn
;
892 switch (insn
->insn
) {
894 ret
= s
->insn_read(dev
, s
, insn
, data
);
897 maxdata
= s
->maxdata_list
898 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
900 for (i
= 0; i
< insn
->n
; ++i
) {
901 if (data
[i
] > maxdata
) {
903 DPRINTK("bad data value(s)\n");
908 ret
= s
->insn_write(dev
, s
, insn
, data
);
914 /* Most drivers ignore the base channel in
915 * insn->chanspec. Fix this here if
916 * the subdevice has <= 32 channels. */
918 unsigned int orig_mask
;
921 if (s
->n_chan
<= 32) {
922 shift
= CR_CHAN(insn
->chanspec
);
930 ret
= s
->insn_bits(dev
, s
, insn
, data
);
937 ret
= check_insn_config_length(insn
, data
);
940 ret
= s
->insn_config(dev
, s
, insn
, data
);
956 * synchronous instructions
962 * struct comedi_insn struct at arg
968 static int do_insn_ioctl(struct comedi_device
*dev
,
969 struct comedi_insn __user
*arg
, void *file
)
971 struct comedi_insn insn
;
972 unsigned int *data
= NULL
;
975 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
981 if (copy_from_user(&insn
, arg
, sizeof(struct comedi_insn
))) {
986 /* This is where the behavior of insn and insnlist deviate. */
987 if (insn
.n
> MAX_SAMPLES
)
988 insn
.n
= MAX_SAMPLES
;
989 if (insn
.insn
& INSN_MASK_WRITE
) {
990 if (copy_from_user(data
,
992 insn
.n
* sizeof(unsigned int))) {
997 ret
= parse_insn(dev
, &insn
, data
, file
);
1000 if (insn
.insn
& INSN_MASK_READ
) {
1001 if (copy_to_user(insn
.data
,
1003 insn
.n
* sizeof(unsigned int))) {
1016 static void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
1017 unsigned mask
, unsigned bits
)
1019 unsigned long flags
;
1021 spin_lock_irqsave(&s
->spin_lock
, flags
);
1022 s
->runflags
&= ~mask
;
1023 s
->runflags
|= (bits
& mask
);
1024 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1027 static int do_cmd_ioctl(struct comedi_device
*dev
,
1028 struct comedi_cmd __user
*cmd
, void *file
)
1030 struct comedi_cmd user_cmd
;
1031 struct comedi_subdevice
*s
;
1032 struct comedi_async
*async
;
1034 unsigned int __user
*chanlist_saver
= NULL
;
1036 if (copy_from_user(&user_cmd
, cmd
, sizeof(struct comedi_cmd
))) {
1037 DPRINTK("bad cmd address\n");
1040 /* save user's chanlist pointer so it can be restored later */
1041 chanlist_saver
= user_cmd
.chanlist
;
1043 if (user_cmd
.subdev
>= dev
->n_subdevices
) {
1044 DPRINTK("%d no such subdevice\n", user_cmd
.subdev
);
1048 s
= dev
->subdevices
+ user_cmd
.subdev
;
1051 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1052 DPRINTK("%d not valid subdevice\n", user_cmd
.subdev
);
1056 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1057 DPRINTK("subdevice %i does not support commands\n",
1062 /* are we locked? (ioctl lock) */
1063 if (s
->lock
&& s
->lock
!= file
) {
1064 DPRINTK("subdevice locked\n");
1070 DPRINTK("subdevice busy\n");
1075 /* make sure channel/gain list isn't too long */
1076 if (user_cmd
.chanlist_len
> s
->len_chanlist
) {
1077 DPRINTK("channel/gain list too long %u > %d\n",
1078 user_cmd
.chanlist_len
, s
->len_chanlist
);
1083 /* make sure channel/gain list isn't too short */
1084 if (user_cmd
.chanlist_len
< 1) {
1085 DPRINTK("channel/gain list too short %u < 1\n",
1086 user_cmd
.chanlist_len
);
1091 kfree(async
->cmd
.chanlist
);
1092 async
->cmd
= user_cmd
;
1093 async
->cmd
.data
= NULL
;
1094 /* load channel/gain list */
1095 async
->cmd
.chanlist
=
1096 kmalloc(async
->cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1097 if (!async
->cmd
.chanlist
) {
1098 DPRINTK("allocation failed\n");
1103 if (copy_from_user(async
->cmd
.chanlist
, user_cmd
.chanlist
,
1104 async
->cmd
.chanlist_len
* sizeof(int))) {
1105 DPRINTK("fault reading chanlist\n");
1110 /* make sure each element in channel/gain list is valid */
1111 ret
= comedi_check_chanlist(s
,
1112 async
->cmd
.chanlist_len
,
1113 async
->cmd
.chanlist
);
1115 DPRINTK("bad chanlist\n");
1119 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1121 if (async
->cmd
.flags
& TRIG_BOGUS
|| ret
) {
1122 DPRINTK("test returned %d\n", ret
);
1123 user_cmd
= async
->cmd
;
1124 /* restore chanlist pointer before copying back */
1125 user_cmd
.chanlist
= chanlist_saver
;
1126 user_cmd
.data
= NULL
;
1127 if (copy_to_user(cmd
, &user_cmd
, sizeof(struct comedi_cmd
))) {
1128 DPRINTK("fault writing cmd\n");
1136 if (!async
->prealloc_bufsz
) {
1138 DPRINTK("no buffer (?)\n");
1142 comedi_reset_async_buf(async
);
1145 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1147 if (async
->cmd
.flags
& TRIG_WAKE_EOS
)
1148 async
->cb_mask
|= COMEDI_CB_EOS
;
1150 comedi_set_subdevice_runflags(s
, ~0, SRF_USER
| SRF_RUNNING
);
1152 ret
= s
->do_cmd(dev
, s
);
1157 do_become_nonbusy(dev
, s
);
1164 command testing ioctl
1167 pointer to cmd structure
1170 cmd structure at arg
1174 modified cmd structure at arg
1177 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1178 struct comedi_cmd __user
*arg
, void *file
)
1180 struct comedi_cmd user_cmd
;
1181 struct comedi_subdevice
*s
;
1183 unsigned int *chanlist
= NULL
;
1184 unsigned int __user
*chanlist_saver
= NULL
;
1186 if (copy_from_user(&user_cmd
, arg
, sizeof(struct comedi_cmd
))) {
1187 DPRINTK("bad cmd address\n");
1190 /* save user's chanlist pointer so it can be restored later */
1191 chanlist_saver
= user_cmd
.chanlist
;
1193 if (user_cmd
.subdev
>= dev
->n_subdevices
) {
1194 DPRINTK("%d no such subdevice\n", user_cmd
.subdev
);
1198 s
= dev
->subdevices
+ user_cmd
.subdev
;
1199 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1200 DPRINTK("%d not valid subdevice\n", user_cmd
.subdev
);
1204 if (!s
->do_cmd
|| !s
->do_cmdtest
) {
1205 DPRINTK("subdevice %i does not support commands\n",
1210 /* make sure channel/gain list isn't too long */
1211 if (user_cmd
.chanlist_len
> s
->len_chanlist
) {
1212 DPRINTK("channel/gain list too long %d > %d\n",
1213 user_cmd
.chanlist_len
, s
->len_chanlist
);
1218 /* load channel/gain list */
1219 if (user_cmd
.chanlist
) {
1221 kmalloc(user_cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1223 DPRINTK("allocation failed\n");
1228 if (copy_from_user(chanlist
, user_cmd
.chanlist
,
1229 user_cmd
.chanlist_len
* sizeof(int))) {
1230 DPRINTK("fault reading chanlist\n");
1235 /* make sure each element in channel/gain list is valid */
1236 ret
= comedi_check_chanlist(s
, user_cmd
.chanlist_len
, chanlist
);
1238 DPRINTK("bad chanlist\n");
1242 user_cmd
.chanlist
= chanlist
;
1245 ret
= s
->do_cmdtest(dev
, s
, &user_cmd
);
1247 /* restore chanlist pointer before copying back */
1248 user_cmd
.chanlist
= chanlist_saver
;
1250 if (copy_to_user(arg
, &user_cmd
, sizeof(struct comedi_cmd
))) {
1251 DPRINTK("bad cmd address\n");
1276 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1280 unsigned long flags
;
1281 struct comedi_subdevice
*s
;
1283 if (arg
>= dev
->n_subdevices
)
1285 s
= dev
->subdevices
+ arg
;
1287 spin_lock_irqsave(&s
->spin_lock
, flags
);
1288 if (s
->busy
|| s
->lock
)
1292 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1299 ret
= s
->lock_f(dev
, s
);
1318 This function isn't protected by the semaphore, since
1319 we already own the lock.
1321 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1324 struct comedi_subdevice
*s
;
1326 if (arg
>= dev
->n_subdevices
)
1328 s
= dev
->subdevices
+ arg
;
1333 if (s
->lock
&& s
->lock
!= file
)
1336 if (s
->lock
== file
) {
1350 cancel acquisition ioctl
1362 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1365 struct comedi_subdevice
*s
;
1367 if (arg
>= dev
->n_subdevices
)
1369 s
= dev
->subdevices
+ arg
;
1370 if (s
->async
== NULL
)
1373 if (s
->lock
&& s
->lock
!= file
)
1379 if (s
->busy
!= file
)
1382 return do_cancel(dev
, s
);
1387 instructs driver to synchronize buffers
1399 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1402 struct comedi_subdevice
*s
;
1404 if (arg
>= dev
->n_subdevices
)
1406 s
= dev
->subdevices
+ arg
;
1408 if (s
->lock
&& s
->lock
!= file
)
1414 if (s
->busy
!= file
)
1418 return s
->poll(dev
, s
);
1423 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1427 if ((comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) && s
->cancel
)
1428 ret
= s
->cancel(dev
, s
);
1430 do_become_nonbusy(dev
, s
);
1436 static void comedi_vm_open(struct vm_area_struct
*area
)
1438 struct comedi_async
*async
;
1439 struct comedi_device
*dev
;
1441 async
= area
->vm_private_data
;
1442 dev
= async
->subdevice
->device
;
1444 mutex_lock(&dev
->mutex
);
1445 async
->mmap_count
++;
1446 mutex_unlock(&dev
->mutex
);
1449 static void comedi_vm_close(struct vm_area_struct
*area
)
1451 struct comedi_async
*async
;
1452 struct comedi_device
*dev
;
1454 async
= area
->vm_private_data
;
1455 dev
= async
->subdevice
->device
;
1457 mutex_lock(&dev
->mutex
);
1458 async
->mmap_count
--;
1459 mutex_unlock(&dev
->mutex
);
1462 static struct vm_operations_struct comedi_vm_ops
= {
1463 .open
= comedi_vm_open
,
1464 .close
= comedi_vm_close
,
1467 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1469 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1470 struct comedi_async
*async
= NULL
;
1471 unsigned long start
= vma
->vm_start
;
1476 struct comedi_subdevice
*s
;
1477 struct comedi_device_file_info
*dev_file_info
;
1478 struct comedi_device
*dev
;
1480 dev_file_info
= comedi_get_device_file_info(minor
);
1481 if (dev_file_info
== NULL
)
1483 dev
= dev_file_info
->device
;
1487 mutex_lock(&dev
->mutex
);
1488 if (!dev
->attached
) {
1489 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1493 if (vma
->vm_flags
& VM_WRITE
)
1494 s
= comedi_get_write_subdevice(dev_file_info
);
1496 s
= comedi_get_read_subdevice(dev_file_info
);
1503 if (async
== NULL
) {
1508 if (vma
->vm_pgoff
!= 0) {
1509 DPRINTK("comedi: mmap() offset must be 0.\n");
1514 size
= vma
->vm_end
- vma
->vm_start
;
1515 if (size
> async
->prealloc_bufsz
) {
1519 if (size
& (~PAGE_MASK
)) {
1524 n_pages
= size
>> PAGE_SHIFT
;
1525 for (i
= 0; i
< n_pages
; ++i
) {
1526 if (remap_pfn_range(vma
, start
,
1527 page_to_pfn(virt_to_page
1528 (async
->buf_page_list
1529 [i
].virt_addr
)), PAGE_SIZE
,
1537 vma
->vm_ops
= &comedi_vm_ops
;
1538 vma
->vm_private_data
= async
;
1540 async
->mmap_count
++;
1544 mutex_unlock(&dev
->mutex
);
1548 static unsigned int comedi_poll(struct file
*file
, poll_table
* wait
)
1550 unsigned int mask
= 0;
1551 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1552 struct comedi_subdevice
*read_subdev
;
1553 struct comedi_subdevice
*write_subdev
;
1554 struct comedi_device_file_info
*dev_file_info
;
1555 struct comedi_device
*dev
;
1556 dev_file_info
= comedi_get_device_file_info(minor
);
1558 if (dev_file_info
== NULL
)
1560 dev
= dev_file_info
->device
;
1564 mutex_lock(&dev
->mutex
);
1565 if (!dev
->attached
) {
1566 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1567 mutex_unlock(&dev
->mutex
);
1572 read_subdev
= comedi_get_read_subdevice(dev_file_info
);
1574 poll_wait(file
, &read_subdev
->async
->wait_head
, wait
);
1575 if (!read_subdev
->busy
1576 || comedi_buf_read_n_available(read_subdev
->async
) > 0
1577 || !(comedi_get_subdevice_runflags(read_subdev
) &
1579 mask
|= POLLIN
| POLLRDNORM
;
1582 write_subdev
= comedi_get_write_subdevice(dev_file_info
);
1584 poll_wait(file
, &write_subdev
->async
->wait_head
, wait
);
1585 comedi_buf_write_alloc(write_subdev
->async
,
1586 write_subdev
->async
->prealloc_bufsz
);
1587 if (!write_subdev
->busy
1588 || !(comedi_get_subdevice_runflags(write_subdev
) &
1590 || comedi_buf_write_n_allocated(write_subdev
->async
) >=
1591 bytes_per_sample(write_subdev
->async
->subdevice
)) {
1592 mask
|= POLLOUT
| POLLWRNORM
;
1596 mutex_unlock(&dev
->mutex
);
1600 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
1601 size_t nbytes
, loff_t
*offset
)
1603 struct comedi_subdevice
*s
;
1604 struct comedi_async
*async
;
1605 int n
, m
, count
= 0, retval
= 0;
1606 DECLARE_WAITQUEUE(wait
, current
);
1607 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1608 struct comedi_device_file_info
*dev_file_info
;
1609 struct comedi_device
*dev
;
1610 dev_file_info
= comedi_get_device_file_info(minor
);
1612 if (dev_file_info
== NULL
)
1614 dev
= dev_file_info
->device
;
1618 if (!dev
->attached
) {
1619 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1624 s
= comedi_get_write_subdevice(dev_file_info
);
1639 if (s
->busy
!= file
) {
1643 add_wait_queue(&async
->wait_head
, &wait
);
1644 while (nbytes
> 0 && !retval
) {
1645 set_current_state(TASK_INTERRUPTIBLE
);
1647 if (!(comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)) {
1649 if (comedi_get_subdevice_runflags(s
) &
1655 do_become_nonbusy(dev
, s
);
1663 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
1664 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
1665 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
1666 if (m
> comedi_buf_write_n_allocated(async
))
1667 m
= comedi_buf_write_n_allocated(async
);
1672 if (file
->f_flags
& O_NONBLOCK
) {
1677 if (signal_pending(current
)) {
1678 retval
= -ERESTARTSYS
;
1683 if (s
->busy
!= file
) {
1690 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
1696 comedi_buf_write_free(async
, n
);
1702 break; /* makes device work like a pipe */
1704 set_current_state(TASK_RUNNING
);
1705 remove_wait_queue(&async
->wait_head
, &wait
);
1708 return count
? count
: retval
;
1711 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
1714 struct comedi_subdevice
*s
;
1715 struct comedi_async
*async
;
1716 int n
, m
, count
= 0, retval
= 0;
1717 DECLARE_WAITQUEUE(wait
, current
);
1718 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1719 struct comedi_device_file_info
*dev_file_info
;
1720 struct comedi_device
*dev
;
1721 dev_file_info
= comedi_get_device_file_info(minor
);
1723 if (dev_file_info
== NULL
)
1725 dev
= dev_file_info
->device
;
1729 if (!dev
->attached
) {
1730 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1735 s
= comedi_get_read_subdevice(dev_file_info
);
1749 if (s
->busy
!= file
) {
1754 add_wait_queue(&async
->wait_head
, &wait
);
1755 while (nbytes
> 0 && !retval
) {
1756 set_current_state(TASK_INTERRUPTIBLE
);
1760 m
= comedi_buf_read_n_available(async
);
1761 /* printk("%d available\n",m); */
1762 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
1763 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
1764 /* printk("%d contiguous\n",m); */
1769 if (!(comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)) {
1770 do_become_nonbusy(dev
, s
);
1771 if (comedi_get_subdevice_runflags(s
) &
1779 if (file
->f_flags
& O_NONBLOCK
) {
1784 if (signal_pending(current
)) {
1785 retval
= -ERESTARTSYS
;
1792 if (s
->busy
!= file
) {
1798 m
= copy_to_user(buf
, async
->prealloc_buf
+
1799 async
->buf_read_ptr
, n
);
1805 comedi_buf_read_alloc(async
, n
);
1806 comedi_buf_read_free(async
, n
);
1812 break; /* makes device work like a pipe */
1814 if (!(comedi_get_subdevice_runflags(s
) & (SRF_ERROR
| SRF_RUNNING
)) &&
1815 async
->buf_read_count
- async
->buf_write_count
== 0) {
1816 do_become_nonbusy(dev
, s
);
1818 set_current_state(TASK_RUNNING
);
1819 remove_wait_queue(&async
->wait_head
, &wait
);
1822 return count
? count
: retval
;
1826 This function restores a subdevice to an idle state.
1828 void do_become_nonbusy(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1830 struct comedi_async
*async
= s
->async
;
1832 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
1834 comedi_reset_async_buf(async
);
1835 async
->inttrig
= NULL
;
1838 "BUG: (?) do_become_nonbusy called with async=0\n");
1844 static int comedi_open(struct inode
*inode
, struct file
*file
)
1846 const unsigned minor
= iminor(inode
);
1847 struct comedi_device_file_info
*dev_file_info
=
1848 comedi_get_device_file_info(minor
);
1849 struct comedi_device
*dev
=
1850 dev_file_info
? dev_file_info
->device
: NULL
;
1853 DPRINTK("invalid minor number\n");
1857 /* This is slightly hacky, but we want module autoloading
1859 * case: user opens device, attached -> ok
1860 * case: user opens device, unattached, in_request_module=0 -> autoload
1861 * case: user opens device, unattached, in_request_module=1 -> fail
1862 * case: root opens device, attached -> ok
1863 * case: root opens device, unattached, in_request_module=1 -> ok
1864 * (typically called from modprobe)
1865 * case: root opens device, unattached, in_request_module=0 -> autoload
1867 * The last could be changed to "-> ok", which would deny root
1870 mutex_lock(&dev
->mutex
);
1873 if (!capable(CAP_NET_ADMIN
) && dev
->in_request_module
) {
1874 DPRINTK("in request module\n");
1875 mutex_unlock(&dev
->mutex
);
1878 if (capable(CAP_NET_ADMIN
) && dev
->in_request_module
)
1881 dev
->in_request_module
= 1;
1884 mutex_unlock(&dev
->mutex
);
1885 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
1886 mutex_lock(&dev
->mutex
);
1889 dev
->in_request_module
= 0;
1891 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
1892 DPRINTK("not attached and not CAP_NET_ADMIN\n");
1893 mutex_unlock(&dev
->mutex
);
1897 __module_get(THIS_MODULE
);
1899 if (dev
->attached
) {
1900 if (!try_module_get(dev
->driver
->module
)) {
1901 module_put(THIS_MODULE
);
1902 mutex_unlock(&dev
->mutex
);
1907 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
) {
1908 int rc
= dev
->open(dev
);
1910 module_put(dev
->driver
->module
);
1911 module_put(THIS_MODULE
);
1912 mutex_unlock(&dev
->mutex
);
1919 mutex_unlock(&dev
->mutex
);
1924 static int comedi_close(struct inode
*inode
, struct file
*file
)
1926 const unsigned minor
= iminor(inode
);
1927 struct comedi_subdevice
*s
= NULL
;
1929 struct comedi_device_file_info
*dev_file_info
;
1930 struct comedi_device
*dev
;
1931 dev_file_info
= comedi_get_device_file_info(minor
);
1933 if (dev_file_info
== NULL
)
1935 dev
= dev_file_info
->device
;
1939 mutex_lock(&dev
->mutex
);
1941 if (dev
->subdevices
) {
1942 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
1943 s
= dev
->subdevices
+ i
;
1945 if (s
->busy
== file
)
1947 if (s
->lock
== file
)
1951 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
1954 module_put(THIS_MODULE
);
1956 module_put(dev
->driver
->module
);
1960 mutex_unlock(&dev
->mutex
);
1962 if (file
->f_flags
& FASYNC
)
1963 comedi_fasync(-1, file
, 0);
1968 static int comedi_fasync(int fd
, struct file
*file
, int on
)
1970 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1971 struct comedi_device_file_info
*dev_file_info
;
1972 struct comedi_device
*dev
;
1973 dev_file_info
= comedi_get_device_file_info(minor
);
1975 if (dev_file_info
== NULL
)
1977 dev
= dev_file_info
->device
;
1981 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
1984 const struct file_operations comedi_fops
= {
1985 .owner
= THIS_MODULE
,
1986 .unlocked_ioctl
= comedi_unlocked_ioctl
,
1987 .compat_ioctl
= comedi_compat_ioctl
,
1988 .open
= comedi_open
,
1989 .release
= comedi_close
,
1990 .read
= comedi_read
,
1991 .write
= comedi_write
,
1992 .mmap
= comedi_mmap
,
1993 .poll
= comedi_poll
,
1994 .fasync
= comedi_fasync
,
1995 .llseek
= noop_llseek
,
1998 struct class *comedi_class
;
1999 static struct cdev comedi_cdev
;
2001 static void comedi_cleanup_legacy_minors(void)
2005 for (i
= 0; i
< comedi_num_legacy_minors
; i
++)
2006 comedi_free_board_minor(i
);
2009 static int __init
comedi_init(void)
2014 printk(KERN_INFO
"comedi: version " COMEDI_RELEASE
2015 " - http://www.comedi.org\n");
2017 if (comedi_num_legacy_minors
< 0 ||
2018 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2019 printk(KERN_ERR
"comedi: error: invalid value for module "
2020 "parameter \"comedi_num_legacy_minors\". Valid values "
2021 "are 0 through %i.\n", COMEDI_NUM_BOARD_MINORS
);
2026 * comedi is unusable if both comedi_autoconfig and
2027 * comedi_num_legacy_minors are zero, so we might as well adjust the
2028 * defaults in that case
2030 if (comedi_autoconfig
== 0 && comedi_num_legacy_minors
== 0)
2031 comedi_num_legacy_minors
= 16;
2033 memset(comedi_file_info_table
, 0,
2034 sizeof(struct comedi_device_file_info
*) * COMEDI_NUM_MINORS
);
2036 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2037 COMEDI_NUM_MINORS
, "comedi");
2040 cdev_init(&comedi_cdev
, &comedi_fops
);
2041 comedi_cdev
.owner
= THIS_MODULE
;
2042 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2043 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2044 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2048 comedi_class
= class_create(THIS_MODULE
, "comedi");
2049 if (IS_ERR(comedi_class
)) {
2050 printk(KERN_ERR
"comedi: failed to create class");
2051 cdev_del(&comedi_cdev
);
2052 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2054 return PTR_ERR(comedi_class
);
2057 /* XXX requires /proc interface */
2060 /* create devices files for legacy/manual use */
2061 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2063 minor
= comedi_alloc_board_minor(NULL
);
2065 comedi_cleanup_legacy_minors();
2066 cdev_del(&comedi_cdev
);
2067 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2076 static void __exit
comedi_cleanup(void)
2080 comedi_cleanup_legacy_minors();
2081 for (i
= 0; i
< COMEDI_NUM_MINORS
; ++i
)
2082 BUG_ON(comedi_file_info_table
[i
]);
2084 class_destroy(comedi_class
);
2085 cdev_del(&comedi_cdev
);
2086 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2088 comedi_proc_cleanup();
2091 module_init(comedi_init
);
2092 module_exit(comedi_cleanup
);
2094 void comedi_error(const struct comedi_device
*dev
, const char *s
)
2096 printk(KERN_ERR
"comedi%d: %s: %s\n", dev
->minor
,
2097 dev
->driver
->driver_name
, s
);
2099 EXPORT_SYMBOL(comedi_error
);
2101 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2103 struct comedi_async
*async
= s
->async
;
2104 unsigned runflags
= 0;
2105 unsigned runflags_mask
= 0;
2107 /* DPRINTK("comedi_event 0x%x\n",mask); */
2109 if ((comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) == 0)
2113 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
2114 COMEDI_CB_OVERFLOW
)) {
2115 runflags_mask
|= SRF_RUNNING
;
2117 /* remember if an error event has occurred, so an error
2118 * can be returned the next time the user does a read() */
2119 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2120 runflags_mask
|= SRF_ERROR
;
2121 runflags
|= SRF_ERROR
;
2123 if (runflags_mask
) {
2124 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2125 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2128 if (async
->cb_mask
& s
->async
->events
) {
2129 if (comedi_get_subdevice_runflags(s
) & SRF_USER
) {
2130 wake_up_interruptible(&async
->wait_head
);
2131 if (s
->subdev_flags
& SDF_CMD_READ
)
2132 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2133 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2134 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2137 async
->cb_func(s
->async
->events
, async
->cb_arg
);
2140 s
->async
->events
= 0;
2142 EXPORT_SYMBOL(comedi_event
);
2144 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
2146 unsigned long flags
;
2149 spin_lock_irqsave(&s
->spin_lock
, flags
);
2150 runflags
= s
->runflags
;
2151 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2154 EXPORT_SYMBOL(comedi_get_subdevice_runflags
);
2156 static int is_device_busy(struct comedi_device
*dev
)
2158 struct comedi_subdevice
*s
;
2164 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2165 s
= dev
->subdevices
+ i
;
2168 if (s
->async
&& s
->async
->mmap_count
)
2175 static void comedi_device_init(struct comedi_device
*dev
)
2177 memset(dev
, 0, sizeof(struct comedi_device
));
2178 spin_lock_init(&dev
->spinlock
);
2179 mutex_init(&dev
->mutex
);
2183 static void comedi_device_cleanup(struct comedi_device
*dev
)
2187 mutex_lock(&dev
->mutex
);
2188 comedi_device_detach(dev
);
2189 mutex_unlock(&dev
->mutex
);
2190 mutex_destroy(&dev
->mutex
);
2193 int comedi_alloc_board_minor(struct device
*hardware_device
)
2195 unsigned long flags
;
2196 struct comedi_device_file_info
*info
;
2197 struct device
*csdev
;
2201 info
= kzalloc(sizeof(struct comedi_device_file_info
), GFP_KERNEL
);
2204 info
->device
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2205 if (info
->device
== NULL
) {
2209 comedi_device_init(info
->device
);
2210 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2211 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2212 if (comedi_file_info_table
[i
] == NULL
) {
2213 comedi_file_info_table
[i
] = info
;
2217 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2218 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2219 comedi_device_cleanup(info
->device
);
2220 kfree(info
->device
);
2224 "ran out of minor numbers for board device files.\n");
2227 info
->device
->minor
= i
;
2228 csdev
= device_create(comedi_class
, hardware_device
,
2229 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2231 info
->device
->class_dev
= csdev
;
2232 dev_set_drvdata(csdev
, info
);
2233 retval
= device_create_file(csdev
, &dev_attr_max_read_buffer_kb
);
2237 "failed to create sysfs attribute file \"%s\".\n",
2238 dev_attr_max_read_buffer_kb
.attr
.name
);
2239 comedi_free_board_minor(i
);
2242 retval
= device_create_file(csdev
, &dev_attr_read_buffer_kb
);
2246 "failed to create sysfs attribute file \"%s\".\n",
2247 dev_attr_read_buffer_kb
.attr
.name
);
2248 comedi_free_board_minor(i
);
2251 retval
= device_create_file(csdev
, &dev_attr_max_write_buffer_kb
);
2255 "failed to create sysfs attribute file \"%s\".\n",
2256 dev_attr_max_write_buffer_kb
.attr
.name
);
2257 comedi_free_board_minor(i
);
2260 retval
= device_create_file(csdev
, &dev_attr_write_buffer_kb
);
2264 "failed to create sysfs attribute file \"%s\".\n",
2265 dev_attr_write_buffer_kb
.attr
.name
);
2266 comedi_free_board_minor(i
);
2272 void comedi_free_board_minor(unsigned minor
)
2274 unsigned long flags
;
2275 struct comedi_device_file_info
*info
;
2277 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2278 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2279 info
= comedi_file_info_table
[minor
];
2280 comedi_file_info_table
[minor
] = NULL
;
2281 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2284 struct comedi_device
*dev
= info
->device
;
2286 if (dev
->class_dev
) {
2287 device_destroy(comedi_class
,
2288 MKDEV(COMEDI_MAJOR
, dev
->minor
));
2290 comedi_device_cleanup(dev
);
2297 int comedi_alloc_subdevice_minor(struct comedi_device
*dev
,
2298 struct comedi_subdevice
*s
)
2300 unsigned long flags
;
2301 struct comedi_device_file_info
*info
;
2302 struct device
*csdev
;
2306 info
= kmalloc(sizeof(struct comedi_device_file_info
), GFP_KERNEL
);
2310 info
->read_subdevice
= s
;
2311 info
->write_subdevice
= s
;
2312 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2313 for (i
= COMEDI_FIRST_SUBDEVICE_MINOR
; i
< COMEDI_NUM_MINORS
; ++i
) {
2314 if (comedi_file_info_table
[i
] == NULL
) {
2315 comedi_file_info_table
[i
] = info
;
2319 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2320 if (i
== COMEDI_NUM_MINORS
) {
2324 "ran out of minor numbers for board device files.\n");
2328 csdev
= device_create(comedi_class
, dev
->class_dev
,
2329 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2330 dev
->minor
, (int)(s
- dev
->subdevices
));
2332 s
->class_dev
= csdev
;
2333 dev_set_drvdata(csdev
, info
);
2334 retval
= device_create_file(csdev
, &dev_attr_max_read_buffer_kb
);
2338 "failed to create sysfs attribute file \"%s\".\n",
2339 dev_attr_max_read_buffer_kb
.attr
.name
);
2340 comedi_free_subdevice_minor(s
);
2343 retval
= device_create_file(csdev
, &dev_attr_read_buffer_kb
);
2347 "failed to create sysfs attribute file \"%s\".\n",
2348 dev_attr_read_buffer_kb
.attr
.name
);
2349 comedi_free_subdevice_minor(s
);
2352 retval
= device_create_file(csdev
, &dev_attr_max_write_buffer_kb
);
2356 "failed to create sysfs attribute file \"%s\".\n",
2357 dev_attr_max_write_buffer_kb
.attr
.name
);
2358 comedi_free_subdevice_minor(s
);
2361 retval
= device_create_file(csdev
, &dev_attr_write_buffer_kb
);
2365 "failed to create sysfs attribute file \"%s\".\n",
2366 dev_attr_write_buffer_kb
.attr
.name
);
2367 comedi_free_subdevice_minor(s
);
2373 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2375 unsigned long flags
;
2376 struct comedi_device_file_info
*info
;
2383 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2384 BUG_ON(s
->minor
< COMEDI_FIRST_SUBDEVICE_MINOR
);
2386 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2387 info
= comedi_file_info_table
[s
->minor
];
2388 comedi_file_info_table
[s
->minor
] = NULL
;
2389 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2392 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2393 s
->class_dev
= NULL
;
2398 struct comedi_device_file_info
*comedi_get_device_file_info(unsigned minor
)
2400 unsigned long flags
;
2401 struct comedi_device_file_info
*info
;
2403 BUG_ON(minor
>= COMEDI_NUM_MINORS
);
2404 spin_lock_irqsave(&comedi_file_info_table_lock
, flags
);
2405 info
= comedi_file_info_table
[minor
];
2406 spin_unlock_irqrestore(&comedi_file_info_table_lock
, flags
);
2409 EXPORT_SYMBOL_GPL(comedi_get_device_file_info
);
2411 static int resize_async_buffer(struct comedi_device
*dev
,
2412 struct comedi_subdevice
*s
,
2413 struct comedi_async
*async
, unsigned new_size
)
2417 if (new_size
> async
->max_bufsize
)
2421 DPRINTK("subdevice is busy, cannot resize buffer\n");
2424 if (async
->mmap_count
) {
2425 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
2429 if (!async
->prealloc_buf
)
2432 /* make sure buffer is an integral number of pages
2434 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
2436 retval
= comedi_buf_alloc(dev
, s
, new_size
);
2440 if (s
->buf_change
) {
2441 retval
= s
->buf_change(dev
, s
, new_size
);
2446 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
2447 dev
->minor
, (int)(s
- dev
->subdevices
), async
->prealloc_bufsz
);
2451 /* sysfs attribute files */
2453 static const unsigned bytes_per_kibi
= 1024;
2455 static ssize_t
show_max_read_buffer_kb(struct device
*dev
,
2456 struct device_attribute
*attr
, char *buf
)
2459 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2460 unsigned max_buffer_size_kb
= 0;
2461 struct comedi_subdevice
*const read_subdevice
=
2462 comedi_get_read_subdevice(info
);
2464 mutex_lock(&info
->device
->mutex
);
2465 if (read_subdevice
&&
2466 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) &&
2467 read_subdevice
->async
) {
2468 max_buffer_size_kb
= read_subdevice
->async
->max_bufsize
/
2471 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", max_buffer_size_kb
);
2472 mutex_unlock(&info
->device
->mutex
);
2477 static ssize_t
store_max_read_buffer_kb(struct device
*dev
,
2478 struct device_attribute
*attr
,
2479 const char *buf
, size_t count
)
2481 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2482 unsigned int new_max_size_kb
;
2483 unsigned int new_max_size
;
2485 struct comedi_subdevice
*const read_subdevice
=
2486 comedi_get_read_subdevice(info
);
2488 ret
= kstrtouint(buf
, 10, &new_max_size_kb
);
2491 if (new_max_size_kb
> (UINT_MAX
/ bytes_per_kibi
))
2493 new_max_size
= new_max_size_kb
* bytes_per_kibi
;
2495 mutex_lock(&info
->device
->mutex
);
2496 if (read_subdevice
== NULL
||
2497 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) == 0 ||
2498 read_subdevice
->async
== NULL
) {
2499 mutex_unlock(&info
->device
->mutex
);
2502 read_subdevice
->async
->max_bufsize
= new_max_size
;
2503 mutex_unlock(&info
->device
->mutex
);
2508 static struct device_attribute dev_attr_max_read_buffer_kb
= {
2510 .name
= "max_read_buffer_kb",
2511 .mode
= S_IRUGO
| S_IWUSR
},
2512 .show
= &show_max_read_buffer_kb
,
2513 .store
= &store_max_read_buffer_kb
2516 static ssize_t
show_read_buffer_kb(struct device
*dev
,
2517 struct device_attribute
*attr
, char *buf
)
2520 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2521 unsigned buffer_size_kb
= 0;
2522 struct comedi_subdevice
*const read_subdevice
=
2523 comedi_get_read_subdevice(info
);
2525 mutex_lock(&info
->device
->mutex
);
2526 if (read_subdevice
&&
2527 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) &&
2528 read_subdevice
->async
) {
2529 buffer_size_kb
= read_subdevice
->async
->prealloc_bufsz
/
2532 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", buffer_size_kb
);
2533 mutex_unlock(&info
->device
->mutex
);
2538 static ssize_t
store_read_buffer_kb(struct device
*dev
,
2539 struct device_attribute
*attr
,
2540 const char *buf
, size_t count
)
2542 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2543 unsigned int new_size_kb
;
2544 unsigned int new_size
;
2547 struct comedi_subdevice
*const read_subdevice
=
2548 comedi_get_read_subdevice(info
);
2550 ret
= kstrtouint(buf
, 10, &new_size_kb
);
2553 if (new_size_kb
> (UINT_MAX
/ bytes_per_kibi
))
2555 new_size
= new_size_kb
* bytes_per_kibi
;
2557 mutex_lock(&info
->device
->mutex
);
2558 if (read_subdevice
== NULL
||
2559 (read_subdevice
->subdev_flags
& SDF_CMD_READ
) == 0 ||
2560 read_subdevice
->async
== NULL
) {
2561 mutex_unlock(&info
->device
->mutex
);
2564 retval
= resize_async_buffer(info
->device
, read_subdevice
,
2565 read_subdevice
->async
, new_size
);
2566 mutex_unlock(&info
->device
->mutex
);
2573 static struct device_attribute dev_attr_read_buffer_kb
= {
2575 .name
= "read_buffer_kb",
2576 .mode
= S_IRUGO
| S_IWUSR
| S_IWGRP
},
2577 .show
= &show_read_buffer_kb
,
2578 .store
= &store_read_buffer_kb
2581 static ssize_t
show_max_write_buffer_kb(struct device
*dev
,
2582 struct device_attribute
*attr
,
2586 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2587 unsigned max_buffer_size_kb
= 0;
2588 struct comedi_subdevice
*const write_subdevice
=
2589 comedi_get_write_subdevice(info
);
2591 mutex_lock(&info
->device
->mutex
);
2592 if (write_subdevice
&&
2593 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) &&
2594 write_subdevice
->async
) {
2595 max_buffer_size_kb
= write_subdevice
->async
->max_bufsize
/
2598 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", max_buffer_size_kb
);
2599 mutex_unlock(&info
->device
->mutex
);
2604 static ssize_t
store_max_write_buffer_kb(struct device
*dev
,
2605 struct device_attribute
*attr
,
2606 const char *buf
, size_t count
)
2608 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2609 unsigned int new_max_size_kb
;
2610 unsigned int new_max_size
;
2612 struct comedi_subdevice
*const write_subdevice
=
2613 comedi_get_write_subdevice(info
);
2615 ret
= kstrtouint(buf
, 10, &new_max_size_kb
);
2618 if (new_max_size_kb
> (UINT_MAX
/ bytes_per_kibi
))
2620 new_max_size
= new_max_size_kb
* bytes_per_kibi
;
2622 mutex_lock(&info
->device
->mutex
);
2623 if (write_subdevice
== NULL
||
2624 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) == 0 ||
2625 write_subdevice
->async
== NULL
) {
2626 mutex_unlock(&info
->device
->mutex
);
2629 write_subdevice
->async
->max_bufsize
= new_max_size
;
2630 mutex_unlock(&info
->device
->mutex
);
2635 static struct device_attribute dev_attr_max_write_buffer_kb
= {
2637 .name
= "max_write_buffer_kb",
2638 .mode
= S_IRUGO
| S_IWUSR
},
2639 .show
= &show_max_write_buffer_kb
,
2640 .store
= &store_max_write_buffer_kb
2643 static ssize_t
show_write_buffer_kb(struct device
*dev
,
2644 struct device_attribute
*attr
, char *buf
)
2647 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2648 unsigned buffer_size_kb
= 0;
2649 struct comedi_subdevice
*const write_subdevice
=
2650 comedi_get_write_subdevice(info
);
2652 mutex_lock(&info
->device
->mutex
);
2653 if (write_subdevice
&&
2654 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) &&
2655 write_subdevice
->async
) {
2656 buffer_size_kb
= write_subdevice
->async
->prealloc_bufsz
/
2659 retval
= snprintf(buf
, PAGE_SIZE
, "%i\n", buffer_size_kb
);
2660 mutex_unlock(&info
->device
->mutex
);
2665 static ssize_t
store_write_buffer_kb(struct device
*dev
,
2666 struct device_attribute
*attr
,
2667 const char *buf
, size_t count
)
2669 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
2670 unsigned int new_size_kb
;
2671 unsigned int new_size
;
2674 struct comedi_subdevice
*const write_subdevice
=
2675 comedi_get_write_subdevice(info
);
2677 ret
= kstrtouint(buf
, 10, &new_size_kb
);
2680 if (new_size_kb
> (UINT_MAX
/ bytes_per_kibi
))
2682 new_size
= ((uint64_t) new_size_kb
) * bytes_per_kibi
;
2684 mutex_lock(&info
->device
->mutex
);
2685 if (write_subdevice
== NULL
||
2686 (write_subdevice
->subdev_flags
& SDF_CMD_WRITE
) == 0 ||
2687 write_subdevice
->async
== NULL
) {
2688 mutex_unlock(&info
->device
->mutex
);
2691 retval
= resize_async_buffer(info
->device
, write_subdevice
,
2692 write_subdevice
->async
, new_size
);
2693 mutex_unlock(&info
->device
->mutex
);
2700 static struct device_attribute dev_attr_write_buffer_kb
= {
2702 .name
= "write_buffer_kb",
2703 .mode
= S_IRUGO
| S_IWUSR
| S_IWGRP
},
2704 .show
= &show_write_buffer_kb
,
2705 .store
= &store_write_buffer_kb