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_compat32.h"
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/fcntl.h>
34 #include <linux/delay.h>
35 #include <linux/ioport.h>
37 #include <linux/slab.h>
38 #include <linux/kmod.h>
39 #include <linux/poll.h>
40 #include <linux/init.h>
41 #include <linux/device.h>
42 #include <linux/vmalloc.h>
44 #include "comedidev.h"
45 #include <linux/cdev.h>
46 #include <linux/stat.h>
49 #include <linux/uaccess.h>
51 #include "comedi_internal.h"
53 MODULE_AUTHOR("http://www.comedi.org");
54 MODULE_DESCRIPTION("Comedi core module");
55 MODULE_LICENSE("GPL");
57 #ifdef CONFIG_COMEDI_DEBUG
59 EXPORT_SYMBOL(comedi_debug
);
60 module_param(comedi_debug
, int, S_IRUGO
| S_IWUSR
);
61 MODULE_PARM_DESC(comedi_debug
,
62 "enable comedi core and driver debugging if non-zero (default 0)"
66 bool comedi_autoconfig
= 1;
67 module_param(comedi_autoconfig
, bool, S_IRUGO
);
68 MODULE_PARM_DESC(comedi_autoconfig
,
69 "enable drivers to auto-configure comedi devices (default 1)");
71 static int comedi_num_legacy_minors
;
72 module_param(comedi_num_legacy_minors
, int, S_IRUGO
);
73 MODULE_PARM_DESC(comedi_num_legacy_minors
,
74 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
77 unsigned int comedi_default_buf_size_kb
= CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
;
78 module_param(comedi_default_buf_size_kb
, uint
, S_IRUGO
| S_IWUSR
);
79 MODULE_PARM_DESC(comedi_default_buf_size_kb
,
80 "default asynchronous buffer size in KiB (default "
81 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
) ")");
83 unsigned int comedi_default_buf_maxsize_kb
84 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
;
85 module_param(comedi_default_buf_maxsize_kb
, uint
, S_IRUGO
| S_IWUSR
);
86 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb
,
87 "default maximum size of asynchronous buffer in KiB (default "
88 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
) ")");
90 static DEFINE_SPINLOCK(comedi_file_info_table_lock
);
91 static struct comedi_device_file_info
92 *comedi_file_info_table
[COMEDI_NUM_MINORS
];
94 static void do_become_nonbusy(struct comedi_device
*dev
,
95 struct comedi_subdevice
*s
);
96 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
98 static int comedi_fasync(int fd
, struct file
*file
, int on
);
100 static int is_device_busy(struct comedi_device
*dev
);
102 static int resize_async_buffer(struct comedi_device
*dev
,
103 struct comedi_subdevice
*s
,
104 struct comedi_async
*async
, unsigned new_size
)
108 if (new_size
> async
->max_bufsize
)
112 DPRINTK("subdevice is busy, cannot resize buffer\n");
115 if (async
->mmap_count
) {
116 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
120 if (!async
->prealloc_buf
)
123 /* make sure buffer is an integral number of pages
125 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
127 retval
= comedi_buf_alloc(dev
, s
, new_size
);
132 retval
= s
->buf_change(dev
, s
, new_size
);
137 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
138 dev
->minor
, (int)(s
- dev
->subdevices
), async
->prealloc_bufsz
);
142 /* sysfs attribute files */
144 static ssize_t
show_max_read_buffer_kb(struct device
*dev
,
145 struct device_attribute
*attr
, char *buf
)
147 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
148 struct comedi_subdevice
*s
= comedi_get_read_subdevice(info
);
149 unsigned int size
= 0;
151 mutex_lock(&info
->device
->mutex
);
152 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
153 size
= s
->async
->max_bufsize
/ 1024;
154 mutex_unlock(&info
->device
->mutex
);
156 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
159 static ssize_t
store_max_read_buffer_kb(struct device
*dev
,
160 struct device_attribute
*attr
,
161 const char *buf
, size_t count
)
163 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
164 struct comedi_subdevice
*s
= comedi_get_read_subdevice(info
);
168 err
= kstrtouint(buf
, 10, &size
);
171 if (size
> (UINT_MAX
/ 1024))
175 mutex_lock(&info
->device
->mutex
);
176 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
177 s
->async
->max_bufsize
= size
;
180 mutex_unlock(&info
->device
->mutex
);
182 return err
? err
: count
;
185 static ssize_t
show_read_buffer_kb(struct device
*dev
,
186 struct device_attribute
*attr
, char *buf
)
188 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
189 struct comedi_subdevice
*s
= comedi_get_read_subdevice(info
);
190 unsigned int size
= 0;
192 mutex_lock(&info
->device
->mutex
);
193 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
194 size
= s
->async
->prealloc_bufsz
/ 1024;
195 mutex_unlock(&info
->device
->mutex
);
197 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
200 static ssize_t
store_read_buffer_kb(struct device
*dev
,
201 struct device_attribute
*attr
,
202 const char *buf
, size_t count
)
204 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
205 struct comedi_subdevice
*s
= comedi_get_read_subdevice(info
);
209 err
= kstrtouint(buf
, 10, &size
);
212 if (size
> (UINT_MAX
/ 1024))
216 mutex_lock(&info
->device
->mutex
);
217 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
218 err
= resize_async_buffer(info
->device
, s
, s
->async
, size
);
221 mutex_unlock(&info
->device
->mutex
);
223 return err
? err
: count
;
226 static ssize_t
show_max_write_buffer_kb(struct device
*dev
,
227 struct device_attribute
*attr
,
230 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
231 struct comedi_subdevice
*s
= comedi_get_write_subdevice(info
);
232 unsigned int size
= 0;
234 mutex_lock(&info
->device
->mutex
);
235 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
236 size
= s
->async
->max_bufsize
/ 1024;
237 mutex_unlock(&info
->device
->mutex
);
239 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
242 static ssize_t
store_max_write_buffer_kb(struct device
*dev
,
243 struct device_attribute
*attr
,
244 const char *buf
, size_t count
)
246 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
247 struct comedi_subdevice
*s
= comedi_get_write_subdevice(info
);
251 err
= kstrtouint(buf
, 10, &size
);
254 if (size
> (UINT_MAX
/ 1024))
258 mutex_lock(&info
->device
->mutex
);
259 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
260 s
->async
->max_bufsize
= size
;
263 mutex_unlock(&info
->device
->mutex
);
265 return err
? err
: count
;
268 static ssize_t
show_write_buffer_kb(struct device
*dev
,
269 struct device_attribute
*attr
, char *buf
)
271 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
272 struct comedi_subdevice
*s
= comedi_get_write_subdevice(info
);
273 unsigned int size
= 0;
275 mutex_lock(&info
->device
->mutex
);
276 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
277 size
= s
->async
->prealloc_bufsz
/ 1024;
278 mutex_unlock(&info
->device
->mutex
);
280 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
283 static ssize_t
store_write_buffer_kb(struct device
*dev
,
284 struct device_attribute
*attr
,
285 const char *buf
, size_t count
)
287 struct comedi_device_file_info
*info
= dev_get_drvdata(dev
);
288 struct comedi_subdevice
*s
= comedi_get_write_subdevice(info
);
292 err
= kstrtouint(buf
, 10, &size
);
295 if (size
> (UINT_MAX
/ 1024))
299 mutex_lock(&info
->device
->mutex
);
300 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
301 err
= resize_async_buffer(info
->device
, s
, s
->async
, size
);
304 mutex_unlock(&info
->device
->mutex
);
306 return err
? err
: count
;
309 static struct device_attribute comedi_dev_attrs
[] = {
310 __ATTR(max_read_buffer_kb
, S_IRUGO
| S_IWUSR
,
311 show_max_read_buffer_kb
, store_max_read_buffer_kb
),
312 __ATTR(read_buffer_kb
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
313 show_read_buffer_kb
, store_read_buffer_kb
),
314 __ATTR(max_write_buffer_kb
, S_IRUGO
| S_IWUSR
,
315 show_max_write_buffer_kb
, store_max_write_buffer_kb
),
316 __ATTR(write_buffer_kb
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
317 show_write_buffer_kb
, store_write_buffer_kb
),
326 pointer to devconfig structure
329 devconfig structure at arg
334 static int do_devconfig_ioctl(struct comedi_device
*dev
,
335 struct comedi_devconfig __user
*arg
)
337 struct comedi_devconfig it
;
339 unsigned char *aux_data
= NULL
;
342 if (!capable(CAP_SYS_ADMIN
))
346 if (is_device_busy(dev
))
349 struct module
*driver_module
= dev
->driver
->module
;
350 comedi_device_detach(dev
);
351 module_put(driver_module
);
356 if (copy_from_user(&it
, arg
, sizeof(struct comedi_devconfig
)))
359 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
361 if (comedi_aux_data(it
.options
, 0) &&
362 it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
364 aux_len
= it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
];
368 aux_data
= vmalloc(aux_len
);
372 if (copy_from_user(aux_data
,
373 (unsigned char __user
*
374 )comedi_aux_data(it
.options
, 0), aux_len
)) {
378 it
.options
[COMEDI_DEVCONF_AUX_DATA_LO
] =
379 (unsigned long)aux_data
;
380 if (sizeof(void *) > sizeof(int)) {
381 bit_shift
= sizeof(int) * 8;
382 it
.options
[COMEDI_DEVCONF_AUX_DATA_HI
] =
383 ((unsigned long)aux_data
) >> bit_shift
;
385 it
.options
[COMEDI_DEVCONF_AUX_DATA_HI
] = 0;
388 ret
= comedi_device_attach(dev
, &it
);
390 if (!try_module_get(dev
->driver
->module
)) {
391 comedi_device_detach(dev
);
404 buffer configuration ioctl
407 pointer to bufconfig structure
413 modified bufconfig at arg
416 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
417 struct comedi_bufconfig __user
*arg
)
419 struct comedi_bufconfig bc
;
420 struct comedi_async
*async
;
421 struct comedi_subdevice
*s
;
424 if (copy_from_user(&bc
, arg
, sizeof(struct comedi_bufconfig
)))
427 if (bc
.subdevice
>= dev
->n_subdevices
|| bc
.subdevice
< 0)
430 s
= &dev
->subdevices
[bc
.subdevice
];
434 DPRINTK("subdevice does not have async capability\n");
440 if (bc
.maximum_size
) {
441 if (!capable(CAP_SYS_ADMIN
))
444 async
->max_bufsize
= bc
.maximum_size
;
448 retval
= resize_async_buffer(dev
, s
, async
, bc
.size
);
453 bc
.size
= async
->prealloc_bufsz
;
454 bc
.maximum_size
= async
->max_bufsize
;
457 if (copy_to_user(arg
, &bc
, sizeof(struct comedi_bufconfig
)))
468 pointer to devinfo structure
477 static int do_devinfo_ioctl(struct comedi_device
*dev
,
478 struct comedi_devinfo __user
*arg
,
481 struct comedi_devinfo devinfo
;
482 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
483 struct comedi_device_file_info
*dev_file_info
=
484 comedi_get_device_file_info(minor
);
485 struct comedi_subdevice
*read_subdev
=
486 comedi_get_read_subdevice(dev_file_info
);
487 struct comedi_subdevice
*write_subdev
=
488 comedi_get_write_subdevice(dev_file_info
);
490 memset(&devinfo
, 0, sizeof(devinfo
));
492 /* fill devinfo structure */
493 devinfo
.version_code
= COMEDI_VERSION_CODE
;
494 devinfo
.n_subdevs
= dev
->n_subdevices
;
495 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
496 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
499 devinfo
.read_subdevice
= read_subdev
- dev
->subdevices
;
501 devinfo
.read_subdevice
= -1;
504 devinfo
.write_subdevice
= write_subdev
- dev
->subdevices
;
506 devinfo
.write_subdevice
= -1;
508 if (copy_to_user(arg
, &devinfo
, sizeof(struct comedi_devinfo
)))
519 pointer to array of subdevice info structures
525 array of subdevice info structures at arg
528 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
529 struct comedi_subdinfo __user
*arg
, void *file
)
532 struct comedi_subdinfo
*tmp
, *us
;
533 struct comedi_subdevice
*s
;
536 kcalloc(dev
->n_subdevices
, sizeof(struct comedi_subdinfo
),
541 /* fill subdinfo structs */
542 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
543 s
= &dev
->subdevices
[i
];
547 us
->n_chan
= s
->n_chan
;
548 us
->subd_flags
= s
->subdev_flags
;
549 if (comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)
550 us
->subd_flags
|= SDF_RUNNING
;
551 #define TIMER_nanosec 5 /* backwards compatibility */
552 us
->timer_type
= TIMER_nanosec
;
553 us
->len_chanlist
= s
->len_chanlist
;
554 us
->maxdata
= s
->maxdata
;
555 if (s
->range_table
) {
557 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
559 us
->range_type
= 0; /* XXX */
561 us
->flags
= s
->flags
;
564 us
->subd_flags
|= SDF_BUSY
;
566 us
->subd_flags
|= SDF_BUSY_OWNER
;
568 us
->subd_flags
|= SDF_LOCKED
;
570 us
->subd_flags
|= SDF_LOCK_OWNER
;
571 if (!s
->maxdata
&& s
->maxdata_list
)
572 us
->subd_flags
|= SDF_MAXDATA
;
574 us
->subd_flags
|= SDF_FLAGS
;
575 if (s
->range_table_list
)
576 us
->subd_flags
|= SDF_RANGETYPE
;
578 us
->subd_flags
|= SDF_CMD
;
580 if (s
->insn_bits
!= &insn_inval
)
581 us
->insn_bits_support
= COMEDI_SUPPORTED
;
583 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
585 us
->settling_time_0
= s
->settling_time_0
;
588 ret
= copy_to_user(arg
, tmp
,
589 dev
->n_subdevices
* sizeof(struct comedi_subdinfo
));
593 return ret
? -EFAULT
: 0;
601 pointer to chaninfo structure
604 chaninfo structure at arg
607 arrays at elements of chaninfo structure
610 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
611 struct comedi_chaninfo __user
*arg
)
613 struct comedi_subdevice
*s
;
614 struct comedi_chaninfo it
;
616 if (copy_from_user(&it
, arg
, sizeof(struct comedi_chaninfo
)))
619 if (it
.subdev
>= dev
->n_subdevices
)
621 s
= &dev
->subdevices
[it
.subdev
];
623 if (it
.maxdata_list
) {
624 if (s
->maxdata
|| !s
->maxdata_list
)
626 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
627 s
->n_chan
* sizeof(unsigned int)))
634 if (copy_to_user(it
.flaglist
, s
->flaglist
,
635 s
->n_chan
* sizeof(unsigned int)))
642 if (!s
->range_table_list
)
644 for (i
= 0; i
< s
->n_chan
; i
++) {
647 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
648 (s
->range_table_list
[i
]->length
);
649 if (put_user(x
, it
.rangelist
+ i
))
653 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
654 s
->n_chan
* sizeof(unsigned int)))
664 buffer information ioctl
667 pointer to bufinfo structure
673 modified bufinfo at arg
676 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
677 struct comedi_bufinfo __user
*arg
, void *file
)
679 struct comedi_bufinfo bi
;
680 struct comedi_subdevice
*s
;
681 struct comedi_async
*async
;
683 if (copy_from_user(&bi
, arg
, sizeof(struct comedi_bufinfo
)))
686 if (bi
.subdevice
>= dev
->n_subdevices
|| bi
.subdevice
< 0)
689 s
= &dev
->subdevices
[bi
.subdevice
];
691 if (s
->lock
&& s
->lock
!= file
)
697 DPRINTK("subdevice does not have async capability\n");
698 bi
.buf_write_ptr
= 0;
700 bi
.buf_write_count
= 0;
701 bi
.buf_read_count
= 0;
703 bi
.bytes_written
= 0;
708 bi
.bytes_written
= 0;
709 goto copyback_position
;
714 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
715 bi
.bytes_read
= comedi_buf_read_alloc(async
, bi
.bytes_read
);
716 comedi_buf_read_free(async
, bi
.bytes_read
);
718 if (!(comedi_get_subdevice_runflags(s
) & (SRF_ERROR
|
720 && async
->buf_write_count
== async
->buf_read_count
) {
721 do_become_nonbusy(dev
, s
);
725 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
727 comedi_buf_write_alloc(async
, bi
.bytes_written
);
728 comedi_buf_write_free(async
, bi
.bytes_written
);
732 bi
.buf_write_count
= async
->buf_write_count
;
733 bi
.buf_write_ptr
= async
->buf_write_ptr
;
734 bi
.buf_read_count
= async
->buf_read_count
;
735 bi
.buf_read_ptr
= async
->buf_read_ptr
;
738 if (copy_to_user(arg
, &bi
, sizeof(struct comedi_bufinfo
)))
744 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
745 unsigned int *data
, void *file
);
748 * synchronous instructions
751 * pointer to sync cmd structure
754 * sync cmd struct at arg
761 /* arbitrary limits */
762 #define MAX_SAMPLES 256
763 static int do_insnlist_ioctl(struct comedi_device
*dev
,
764 struct comedi_insnlist __user
*arg
, void *file
)
766 struct comedi_insnlist insnlist
;
767 struct comedi_insn
*insns
= NULL
;
768 unsigned int *data
= NULL
;
772 if (copy_from_user(&insnlist
, arg
, sizeof(struct comedi_insnlist
)))
775 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
777 DPRINTK("kmalloc failed\n");
783 kcalloc(insnlist
.n_insns
, sizeof(struct comedi_insn
), GFP_KERNEL
);
785 DPRINTK("kmalloc failed\n");
790 if (copy_from_user(insns
, insnlist
.insns
,
791 sizeof(struct comedi_insn
) * insnlist
.n_insns
)) {
792 DPRINTK("copy_from_user failed\n");
797 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
798 if (insns
[i
].n
> MAX_SAMPLES
) {
799 DPRINTK("number of samples too large\n");
803 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
804 if (copy_from_user(data
, insns
[i
].data
,
805 insns
[i
].n
* sizeof(unsigned int))) {
806 DPRINTK("copy_from_user failed\n");
811 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
814 if (insns
[i
].insn
& INSN_MASK_READ
) {
815 if (copy_to_user(insns
[i
].data
, data
,
816 insns
[i
].n
* sizeof(unsigned int))) {
817 DPRINTK("copy_to_user failed\n");
835 static int check_insn_config_length(struct comedi_insn
*insn
,
842 case INSN_CONFIG_DIO_OUTPUT
:
843 case INSN_CONFIG_DIO_INPUT
:
844 case INSN_CONFIG_DISARM
:
845 case INSN_CONFIG_RESET
:
849 case INSN_CONFIG_ARM
:
850 case INSN_CONFIG_DIO_QUERY
:
851 case INSN_CONFIG_BLOCK_SIZE
:
852 case INSN_CONFIG_FILTER
:
853 case INSN_CONFIG_SERIAL_CLOCK
:
854 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
855 case INSN_CONFIG_ALT_SOURCE
:
856 case INSN_CONFIG_SET_COUNTER_MODE
:
857 case INSN_CONFIG_8254_READ_STATUS
:
858 case INSN_CONFIG_SET_ROUTING
:
859 case INSN_CONFIG_GET_ROUTING
:
860 case INSN_CONFIG_GET_PWM_STATUS
:
861 case INSN_CONFIG_PWM_SET_PERIOD
:
862 case INSN_CONFIG_PWM_GET_PERIOD
:
866 case INSN_CONFIG_SET_GATE_SRC
:
867 case INSN_CONFIG_GET_GATE_SRC
:
868 case INSN_CONFIG_SET_CLOCK_SRC
:
869 case INSN_CONFIG_GET_CLOCK_SRC
:
870 case INSN_CONFIG_SET_OTHER_SRC
:
871 case INSN_CONFIG_GET_COUNTER_STATUS
:
872 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
873 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
874 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
878 case INSN_CONFIG_PWM_OUTPUT
:
879 case INSN_CONFIG_ANALOG_TRIG
:
883 /* by default we allow the insn since we don't have checks for
884 * all possible cases yet */
886 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
888 pr_warn("comedi: Add a check to %s in %s.\n",
890 pr_warn("comedi: Assuming n=%i is correct.\n", insn
->n
);
896 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
897 unsigned int *data
, void *file
)
899 struct comedi_subdevice
*s
;
903 if (insn
->insn
& INSN_MASK_SPECIAL
) {
904 /* a non-subdevice instruction */
906 switch (insn
->insn
) {
916 do_gettimeofday(&tv
);
918 data
[1] = tv
.tv_usec
;
924 if (insn
->n
!= 1 || data
[0] >= 100000) {
928 udelay(data
[0] / 1000);
936 if (insn
->subdev
>= dev
->n_subdevices
) {
937 DPRINTK("%d not usable subdevice\n",
942 s
= &dev
->subdevices
[insn
->subdev
];
944 DPRINTK("no async\n");
948 if (!s
->async
->inttrig
) {
949 DPRINTK("no inttrig\n");
953 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
958 DPRINTK("invalid insn\n");
963 /* a subdevice instruction */
964 unsigned int maxdata
;
966 if (insn
->subdev
>= dev
->n_subdevices
) {
967 DPRINTK("subdevice %d out of range\n", insn
->subdev
);
971 s
= &dev
->subdevices
[insn
->subdev
];
973 if (s
->type
== COMEDI_SUBD_UNUSED
) {
974 DPRINTK("%d not usable subdevice\n", insn
->subdev
);
979 /* are we locked? (ioctl lock) */
980 if (s
->lock
&& s
->lock
!= file
) {
981 DPRINTK("device locked\n");
986 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
989 DPRINTK("bad chanspec\n");
997 /* This looks arbitrary. It is. */
998 s
->busy
= &parse_insn
;
999 switch (insn
->insn
) {
1001 ret
= s
->insn_read(dev
, s
, insn
, data
);
1004 maxdata
= s
->maxdata_list
1005 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1007 for (i
= 0; i
< insn
->n
; ++i
) {
1008 if (data
[i
] > maxdata
) {
1010 DPRINTK("bad data value(s)\n");
1015 ret
= s
->insn_write(dev
, s
, insn
, data
);
1021 /* Most drivers ignore the base channel in
1022 * insn->chanspec. Fix this here if
1023 * the subdevice has <= 32 channels. */
1025 unsigned int orig_mask
;
1027 orig_mask
= data
[0];
1028 if (s
->n_chan
<= 32) {
1029 shift
= CR_CHAN(insn
->chanspec
);
1037 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1038 data
[0] = orig_mask
;
1044 ret
= check_insn_config_length(insn
, data
);
1047 ret
= s
->insn_config(dev
, s
, insn
, data
);
1063 * synchronous instructions
1069 * struct comedi_insn struct at arg
1075 static int do_insn_ioctl(struct comedi_device
*dev
,
1076 struct comedi_insn __user
*arg
, void *file
)
1078 struct comedi_insn insn
;
1079 unsigned int *data
= NULL
;
1082 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1088 if (copy_from_user(&insn
, arg
, sizeof(struct comedi_insn
))) {
1093 /* This is where the behavior of insn and insnlist deviate. */
1094 if (insn
.n
> MAX_SAMPLES
)
1095 insn
.n
= MAX_SAMPLES
;
1096 if (insn
.insn
& INSN_MASK_WRITE
) {
1097 if (copy_from_user(data
,
1099 insn
.n
* sizeof(unsigned int))) {
1104 ret
= parse_insn(dev
, &insn
, data
, file
);
1107 if (insn
.insn
& INSN_MASK_READ
) {
1108 if (copy_to_user(insn
.data
,
1110 insn
.n
* sizeof(unsigned int))) {
1123 static void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
1124 unsigned mask
, unsigned bits
)
1126 unsigned long flags
;
1128 spin_lock_irqsave(&s
->spin_lock
, flags
);
1129 s
->runflags
&= ~mask
;
1130 s
->runflags
|= (bits
& mask
);
1131 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1134 static int do_cmd_ioctl(struct comedi_device
*dev
,
1135 struct comedi_cmd __user
*arg
, void *file
)
1137 struct comedi_cmd cmd
;
1138 struct comedi_subdevice
*s
;
1139 struct comedi_async
*async
;
1141 unsigned int __user
*user_chanlist
;
1143 if (copy_from_user(&cmd
, arg
, sizeof(struct comedi_cmd
))) {
1144 DPRINTK("bad cmd address\n");
1147 /* save user's chanlist pointer so it can be restored later */
1148 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1150 if (cmd
.subdev
>= dev
->n_subdevices
) {
1151 DPRINTK("%d no such subdevice\n", cmd
.subdev
);
1155 s
= &dev
->subdevices
[cmd
.subdev
];
1158 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1159 DPRINTK("%d not valid subdevice\n", cmd
.subdev
);
1163 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1164 DPRINTK("subdevice %i does not support commands\n",
1169 /* are we locked? (ioctl lock) */
1170 if (s
->lock
&& s
->lock
!= file
) {
1171 DPRINTK("subdevice locked\n");
1177 DPRINTK("subdevice busy\n");
1182 /* make sure channel/gain list isn't too long */
1183 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1184 DPRINTK("channel/gain list too long %u > %d\n",
1185 cmd
.chanlist_len
, s
->len_chanlist
);
1190 /* make sure channel/gain list isn't too short */
1191 if (cmd
.chanlist_len
< 1) {
1192 DPRINTK("channel/gain list too short %u < 1\n",
1199 async
->cmd
.data
= NULL
;
1200 /* load channel/gain list */
1201 async
->cmd
.chanlist
=
1202 kmalloc(async
->cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1203 if (!async
->cmd
.chanlist
) {
1204 DPRINTK("allocation failed\n");
1209 if (copy_from_user(async
->cmd
.chanlist
, user_chanlist
,
1210 async
->cmd
.chanlist_len
* sizeof(int))) {
1211 DPRINTK("fault reading chanlist\n");
1216 /* make sure each element in channel/gain list is valid */
1217 ret
= comedi_check_chanlist(s
,
1218 async
->cmd
.chanlist_len
,
1219 async
->cmd
.chanlist
);
1221 DPRINTK("bad chanlist\n");
1225 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1227 if (async
->cmd
.flags
& TRIG_BOGUS
|| ret
) {
1228 DPRINTK("test returned %d\n", ret
);
1230 /* restore chanlist pointer before copying back */
1231 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1233 if (copy_to_user(arg
, &cmd
, sizeof(struct comedi_cmd
))) {
1234 DPRINTK("fault writing cmd\n");
1242 if (!async
->prealloc_bufsz
) {
1244 DPRINTK("no buffer (?)\n");
1248 comedi_reset_async_buf(async
);
1251 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1253 if (async
->cmd
.flags
& TRIG_WAKE_EOS
)
1254 async
->cb_mask
|= COMEDI_CB_EOS
;
1256 comedi_set_subdevice_runflags(s
, ~0, SRF_USER
| SRF_RUNNING
);
1258 ret
= s
->do_cmd(dev
, s
);
1263 do_become_nonbusy(dev
, s
);
1270 command testing ioctl
1273 pointer to cmd structure
1276 cmd structure at arg
1280 modified cmd structure at arg
1283 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1284 struct comedi_cmd __user
*arg
, void *file
)
1286 struct comedi_cmd cmd
;
1287 struct comedi_subdevice
*s
;
1289 unsigned int *chanlist
= NULL
;
1290 unsigned int __user
*user_chanlist
;
1292 if (copy_from_user(&cmd
, arg
, sizeof(struct comedi_cmd
))) {
1293 DPRINTK("bad cmd address\n");
1296 /* save user's chanlist pointer so it can be restored later */
1297 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1299 if (cmd
.subdev
>= dev
->n_subdevices
) {
1300 DPRINTK("%d no such subdevice\n", cmd
.subdev
);
1304 s
= &dev
->subdevices
[cmd
.subdev
];
1305 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1306 DPRINTK("%d not valid subdevice\n", cmd
.subdev
);
1310 if (!s
->do_cmd
|| !s
->do_cmdtest
) {
1311 DPRINTK("subdevice %i does not support commands\n",
1316 /* make sure channel/gain list isn't too long */
1317 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1318 DPRINTK("channel/gain list too long %d > %d\n",
1319 cmd
.chanlist_len
, s
->len_chanlist
);
1324 /* load channel/gain list */
1327 kmalloc(cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1329 DPRINTK("allocation failed\n");
1334 if (copy_from_user(chanlist
, user_chanlist
,
1335 cmd
.chanlist_len
* sizeof(int))) {
1336 DPRINTK("fault reading chanlist\n");
1341 /* make sure each element in channel/gain list is valid */
1342 ret
= comedi_check_chanlist(s
, cmd
.chanlist_len
, chanlist
);
1344 DPRINTK("bad chanlist\n");
1348 cmd
.chanlist
= chanlist
;
1351 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1353 /* restore chanlist pointer before copying back */
1354 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1356 if (copy_to_user(arg
, &cmd
, sizeof(struct comedi_cmd
))) {
1357 DPRINTK("bad cmd address\n");
1382 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1386 unsigned long flags
;
1387 struct comedi_subdevice
*s
;
1389 if (arg
>= dev
->n_subdevices
)
1391 s
= &dev
->subdevices
[arg
];
1393 spin_lock_irqsave(&s
->spin_lock
, flags
);
1394 if (s
->busy
|| s
->lock
)
1398 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1405 ret
= s
->lock_f(dev
, s
);
1424 This function isn't protected by the semaphore, since
1425 we already own the lock.
1427 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1430 struct comedi_subdevice
*s
;
1432 if (arg
>= dev
->n_subdevices
)
1434 s
= &dev
->subdevices
[arg
];
1439 if (s
->lock
&& s
->lock
!= file
)
1442 if (s
->lock
== file
) {
1456 cancel acquisition ioctl
1468 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1471 struct comedi_subdevice
*s
;
1473 if (arg
>= dev
->n_subdevices
)
1475 s
= &dev
->subdevices
[arg
];
1476 if (s
->async
== NULL
)
1479 if (s
->lock
&& s
->lock
!= file
)
1485 if (s
->busy
!= file
)
1488 return do_cancel(dev
, s
);
1493 instructs driver to synchronize buffers
1505 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1508 struct comedi_subdevice
*s
;
1510 if (arg
>= dev
->n_subdevices
)
1512 s
= &dev
->subdevices
[arg
];
1514 if (s
->lock
&& s
->lock
!= file
)
1520 if (s
->busy
!= file
)
1524 return s
->poll(dev
, s
);
1529 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
1532 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1533 struct comedi_device_file_info
*dev_file_info
=
1534 comedi_get_device_file_info(minor
);
1535 struct comedi_device
*dev
;
1538 if (dev_file_info
== NULL
|| dev_file_info
->device
== NULL
)
1540 dev
= dev_file_info
->device
;
1542 mutex_lock(&dev
->mutex
);
1544 /* Device config is special, because it must work on
1545 * an unconfigured device. */
1546 if (cmd
== COMEDI_DEVCONFIG
) {
1547 rc
= do_devconfig_ioctl(dev
,
1548 (struct comedi_devconfig __user
*)arg
);
1552 if (!dev
->attached
) {
1553 DPRINTK("no driver configured on /dev/comedi%i\n", dev
->minor
);
1559 case COMEDI_BUFCONFIG
:
1560 rc
= do_bufconfig_ioctl(dev
,
1561 (struct comedi_bufconfig __user
*)arg
);
1563 case COMEDI_DEVINFO
:
1564 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
1567 case COMEDI_SUBDINFO
:
1568 rc
= do_subdinfo_ioctl(dev
,
1569 (struct comedi_subdinfo __user
*)arg
,
1572 case COMEDI_CHANINFO
:
1573 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
1575 case COMEDI_RANGEINFO
:
1576 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
1578 case COMEDI_BUFINFO
:
1579 rc
= do_bufinfo_ioctl(dev
,
1580 (struct comedi_bufinfo __user
*)arg
,
1584 rc
= do_lock_ioctl(dev
, arg
, file
);
1587 rc
= do_unlock_ioctl(dev
, arg
, file
);
1590 rc
= do_cancel_ioctl(dev
, arg
, file
);
1593 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
1595 case COMEDI_CMDTEST
:
1596 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
1599 case COMEDI_INSNLIST
:
1600 rc
= do_insnlist_ioctl(dev
,
1601 (struct comedi_insnlist __user
*)arg
,
1605 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
1609 rc
= do_poll_ioctl(dev
, arg
, file
);
1617 mutex_unlock(&dev
->mutex
);
1621 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1625 if ((comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) && s
->cancel
)
1626 ret
= s
->cancel(dev
, s
);
1628 do_become_nonbusy(dev
, s
);
1634 static void comedi_vm_open(struct vm_area_struct
*area
)
1636 struct comedi_async
*async
;
1637 struct comedi_device
*dev
;
1639 async
= area
->vm_private_data
;
1640 dev
= async
->subdevice
->device
;
1642 mutex_lock(&dev
->mutex
);
1643 async
->mmap_count
++;
1644 mutex_unlock(&dev
->mutex
);
1647 static void comedi_vm_close(struct vm_area_struct
*area
)
1649 struct comedi_async
*async
;
1650 struct comedi_device
*dev
;
1652 async
= area
->vm_private_data
;
1653 dev
= async
->subdevice
->device
;
1655 mutex_lock(&dev
->mutex
);
1656 async
->mmap_count
--;
1657 mutex_unlock(&dev
->mutex
);
1660 static struct vm_operations_struct comedi_vm_ops
= {
1661 .open
= comedi_vm_open
,
1662 .close
= comedi_vm_close
,
1665 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1667 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1668 struct comedi_async
*async
= NULL
;
1669 unsigned long start
= vma
->vm_start
;
1674 struct comedi_subdevice
*s
;
1675 struct comedi_device_file_info
*dev_file_info
;
1676 struct comedi_device
*dev
;
1678 dev_file_info
= comedi_get_device_file_info(minor
);
1679 if (dev_file_info
== NULL
)
1681 dev
= dev_file_info
->device
;
1685 mutex_lock(&dev
->mutex
);
1686 if (!dev
->attached
) {
1687 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1691 if (vma
->vm_flags
& VM_WRITE
)
1692 s
= comedi_get_write_subdevice(dev_file_info
);
1694 s
= comedi_get_read_subdevice(dev_file_info
);
1701 if (async
== NULL
) {
1706 if (vma
->vm_pgoff
!= 0) {
1707 DPRINTK("comedi: mmap() offset must be 0.\n");
1712 size
= vma
->vm_end
- vma
->vm_start
;
1713 if (size
> async
->prealloc_bufsz
) {
1717 if (size
& (~PAGE_MASK
)) {
1722 n_pages
= size
>> PAGE_SHIFT
;
1723 for (i
= 0; i
< n_pages
; ++i
) {
1724 if (remap_pfn_range(vma
, start
,
1725 page_to_pfn(virt_to_page
1726 (async
->buf_page_list
1727 [i
].virt_addr
)), PAGE_SIZE
,
1735 vma
->vm_ops
= &comedi_vm_ops
;
1736 vma
->vm_private_data
= async
;
1738 async
->mmap_count
++;
1742 mutex_unlock(&dev
->mutex
);
1746 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
1748 unsigned int mask
= 0;
1749 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1750 struct comedi_subdevice
*read_subdev
;
1751 struct comedi_subdevice
*write_subdev
;
1752 struct comedi_device_file_info
*dev_file_info
;
1753 struct comedi_device
*dev
;
1754 dev_file_info
= comedi_get_device_file_info(minor
);
1756 if (dev_file_info
== NULL
)
1758 dev
= dev_file_info
->device
;
1762 mutex_lock(&dev
->mutex
);
1763 if (!dev
->attached
) {
1764 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1765 mutex_unlock(&dev
->mutex
);
1770 read_subdev
= comedi_get_read_subdevice(dev_file_info
);
1772 poll_wait(file
, &read_subdev
->async
->wait_head
, wait
);
1773 if (!read_subdev
->busy
1774 || comedi_buf_read_n_available(read_subdev
->async
) > 0
1775 || !(comedi_get_subdevice_runflags(read_subdev
) &
1777 mask
|= POLLIN
| POLLRDNORM
;
1780 write_subdev
= comedi_get_write_subdevice(dev_file_info
);
1782 poll_wait(file
, &write_subdev
->async
->wait_head
, wait
);
1783 comedi_buf_write_alloc(write_subdev
->async
,
1784 write_subdev
->async
->prealloc_bufsz
);
1785 if (!write_subdev
->busy
1786 || !(comedi_get_subdevice_runflags(write_subdev
) &
1788 || comedi_buf_write_n_allocated(write_subdev
->async
) >=
1789 bytes_per_sample(write_subdev
->async
->subdevice
)) {
1790 mask
|= POLLOUT
| POLLWRNORM
;
1794 mutex_unlock(&dev
->mutex
);
1798 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
1799 size_t nbytes
, loff_t
*offset
)
1801 struct comedi_subdevice
*s
;
1802 struct comedi_async
*async
;
1803 int n
, m
, count
= 0, retval
= 0;
1804 DECLARE_WAITQUEUE(wait
, current
);
1805 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1806 struct comedi_device_file_info
*dev_file_info
;
1807 struct comedi_device
*dev
;
1808 dev_file_info
= comedi_get_device_file_info(minor
);
1810 if (dev_file_info
== NULL
)
1812 dev
= dev_file_info
->device
;
1816 if (!dev
->attached
) {
1817 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1822 s
= comedi_get_write_subdevice(dev_file_info
);
1837 if (s
->busy
!= file
) {
1841 add_wait_queue(&async
->wait_head
, &wait
);
1842 while (nbytes
> 0 && !retval
) {
1843 set_current_state(TASK_INTERRUPTIBLE
);
1845 if (!(comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)) {
1847 if (comedi_get_subdevice_runflags(s
) &
1853 do_become_nonbusy(dev
, s
);
1861 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
1862 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
1863 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
1864 if (m
> comedi_buf_write_n_allocated(async
))
1865 m
= comedi_buf_write_n_allocated(async
);
1870 if (file
->f_flags
& O_NONBLOCK
) {
1875 if (signal_pending(current
)) {
1876 retval
= -ERESTARTSYS
;
1881 if (s
->busy
!= file
) {
1888 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
1894 comedi_buf_write_free(async
, n
);
1900 break; /* makes device work like a pipe */
1902 set_current_state(TASK_RUNNING
);
1903 remove_wait_queue(&async
->wait_head
, &wait
);
1906 return count
? count
: retval
;
1909 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
1912 struct comedi_subdevice
*s
;
1913 struct comedi_async
*async
;
1914 int n
, m
, count
= 0, retval
= 0;
1915 DECLARE_WAITQUEUE(wait
, current
);
1916 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1917 struct comedi_device_file_info
*dev_file_info
;
1918 struct comedi_device
*dev
;
1919 dev_file_info
= comedi_get_device_file_info(minor
);
1921 if (dev_file_info
== NULL
)
1923 dev
= dev_file_info
->device
;
1927 if (!dev
->attached
) {
1928 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1933 s
= comedi_get_read_subdevice(dev_file_info
);
1947 if (s
->busy
!= file
) {
1952 add_wait_queue(&async
->wait_head
, &wait
);
1953 while (nbytes
> 0 && !retval
) {
1954 set_current_state(TASK_INTERRUPTIBLE
);
1958 m
= comedi_buf_read_n_available(async
);
1959 /* printk("%d available\n",m); */
1960 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
1961 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
1962 /* printk("%d contiguous\n",m); */
1967 if (!(comedi_get_subdevice_runflags(s
) & SRF_RUNNING
)) {
1968 do_become_nonbusy(dev
, s
);
1969 if (comedi_get_subdevice_runflags(s
) &
1977 if (file
->f_flags
& O_NONBLOCK
) {
1982 if (signal_pending(current
)) {
1983 retval
= -ERESTARTSYS
;
1990 if (s
->busy
!= file
) {
1996 m
= copy_to_user(buf
, async
->prealloc_buf
+
1997 async
->buf_read_ptr
, n
);
2003 comedi_buf_read_alloc(async
, n
);
2004 comedi_buf_read_free(async
, n
);
2010 break; /* makes device work like a pipe */
2012 if (!(comedi_get_subdevice_runflags(s
) & (SRF_ERROR
| SRF_RUNNING
)) &&
2013 async
->buf_read_count
- async
->buf_write_count
== 0) {
2014 do_become_nonbusy(dev
, s
);
2016 set_current_state(TASK_RUNNING
);
2017 remove_wait_queue(&async
->wait_head
, &wait
);
2020 return count
? count
: retval
;
2024 This function restores a subdevice to an idle state.
2026 static void do_become_nonbusy(struct comedi_device
*dev
,
2027 struct comedi_subdevice
*s
)
2029 struct comedi_async
*async
= s
->async
;
2031 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
2033 comedi_reset_async_buf(async
);
2034 async
->inttrig
= NULL
;
2035 kfree(async
->cmd
.chanlist
);
2036 async
->cmd
.chanlist
= NULL
;
2038 dev_err(dev
->class_dev
,
2039 "BUG: (?) do_become_nonbusy called with async=NULL\n");
2045 static int comedi_open(struct inode
*inode
, struct file
*file
)
2047 const unsigned minor
= iminor(inode
);
2048 struct comedi_device_file_info
*dev_file_info
=
2049 comedi_get_device_file_info(minor
);
2050 struct comedi_device
*dev
=
2051 dev_file_info
? dev_file_info
->device
: NULL
;
2054 DPRINTK("invalid minor number\n");
2058 /* This is slightly hacky, but we want module autoloading
2060 * case: user opens device, attached -> ok
2061 * case: user opens device, unattached, in_request_module=0 -> autoload
2062 * case: user opens device, unattached, in_request_module=1 -> fail
2063 * case: root opens device, attached -> ok
2064 * case: root opens device, unattached, in_request_module=1 -> ok
2065 * (typically called from modprobe)
2066 * case: root opens device, unattached, in_request_module=0 -> autoload
2068 * The last could be changed to "-> ok", which would deny root
2071 mutex_lock(&dev
->mutex
);
2074 if (!capable(CAP_NET_ADMIN
) && dev
->in_request_module
) {
2075 DPRINTK("in request module\n");
2076 mutex_unlock(&dev
->mutex
);
2079 if (capable(CAP_NET_ADMIN
) && dev
->in_request_module
)
2082 dev
->in_request_module
= 1;
2085 mutex_unlock(&dev
->mutex
);
2086 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
2087 mutex_lock(&dev
->mutex
);
2090 dev
->in_request_module
= 0;
2092 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
2093 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2094 mutex_unlock(&dev
->mutex
);
2098 __module_get(THIS_MODULE
);
2100 if (dev
->attached
) {
2101 if (!try_module_get(dev
->driver
->module
)) {
2102 module_put(THIS_MODULE
);
2103 mutex_unlock(&dev
->mutex
);
2108 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
) {
2109 int rc
= dev
->open(dev
);
2111 module_put(dev
->driver
->module
);
2112 module_put(THIS_MODULE
);
2113 mutex_unlock(&dev
->mutex
);
2120 mutex_unlock(&dev
->mutex
);
2125 static int comedi_close(struct inode
*inode
, struct file
*file
)
2127 const unsigned minor
= iminor(inode
);
2128 struct comedi_subdevice
*s
= NULL
;
2130 struct comedi_device_file_info
*dev_file_info
;
2131 struct comedi_device
*dev
;
2132 dev_file_info
= comedi_get_device_file_info(minor
);
2134 if (dev_file_info
== NULL
)
2136 dev
= dev_file_info
->device
;
2140 mutex_lock(&dev
->mutex
);
2142 if (dev
->subdevices
) {
2143 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2144 s
= &dev
->subdevices
[i
];
2146 if (s
->busy
== file
)
2148 if (s
->lock
== file
)
2152 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
2155 module_put(THIS_MODULE
);
2157 module_put(dev
->driver
->module
);
2161 mutex_unlock(&dev
->mutex
);
2163 if (file
->f_flags
& FASYNC
)
2164 comedi_fasync(-1, file
, 0);
2169 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2171 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
2172 struct comedi_device_file_info
*dev_file_info
;
2173 struct comedi_device
*dev
;
2174 dev_file_info
= comedi_get_device_file_info(minor
);
2176 if (dev_file_info
== NULL
)
2178 dev
= dev_file_info
->device
;
2182 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2185 static const struct file_operations comedi_fops
= {
2186 .owner
= THIS_MODULE
,
2187 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2188 .compat_ioctl
= comedi_compat_ioctl
,
2189 .open
= comedi_open
,
2190 .release
= comedi_close
,
2191 .read
= comedi_read
,
2192 .write
= comedi_write
,
2193 .mmap
= comedi_mmap
,
2194 .poll
= comedi_poll
,
2195 .fasync
= comedi_fasync
,
2196 .llseek
= noop_llseek
,
2199 static struct class *comedi_class
;
2200 static struct cdev comedi_cdev
;
2202 static void comedi_cleanup_legacy_minors(void)
2206 for (i
= 0; i
< comedi_num_legacy_minors
; i
++)
2207 comedi_free_board_minor(i
);
2210 static int __init
comedi_init(void)
2215 pr_info("comedi: version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2217 if (comedi_num_legacy_minors
< 0 ||
2218 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2219 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2220 COMEDI_NUM_BOARD_MINORS
);
2225 * comedi is unusable if both comedi_autoconfig and
2226 * comedi_num_legacy_minors are zero, so we might as well adjust the
2227 * defaults in that case
2229 if (comedi_autoconfig
== 0 && comedi_num_legacy_minors
== 0)
2230 comedi_num_legacy_minors
= 16;
2232 memset(comedi_file_info_table
, 0,
2233 sizeof(struct comedi_device_file_info
*) * COMEDI_NUM_MINORS
);
2235 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2236 COMEDI_NUM_MINORS
, "comedi");
2239 cdev_init(&comedi_cdev
, &comedi_fops
);
2240 comedi_cdev
.owner
= THIS_MODULE
;
2241 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2242 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2243 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2247 comedi_class
= class_create(THIS_MODULE
, "comedi");
2248 if (IS_ERR(comedi_class
)) {
2249 pr_err("comedi: failed to create class\n");
2250 cdev_del(&comedi_cdev
);
2251 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2253 return PTR_ERR(comedi_class
);
2256 comedi_class
->dev_attrs
= comedi_dev_attrs
;
2258 /* XXX requires /proc interface */
2261 /* create devices files for legacy/manual use */
2262 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2264 minor
= comedi_alloc_board_minor(NULL
);
2266 comedi_cleanup_legacy_minors();
2267 cdev_del(&comedi_cdev
);
2268 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2277 static void __exit
comedi_cleanup(void)
2281 comedi_cleanup_legacy_minors();
2282 for (i
= 0; i
< COMEDI_NUM_MINORS
; ++i
)
2283 BUG_ON(comedi_file_info_table
[i
]);
2285 class_destroy(comedi_class
);
2286 cdev_del(&comedi_cdev
);
2287 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2289 comedi_proc_cleanup();
2292 module_init(comedi_init
);
2293 module_exit(comedi_cleanup
);
2295 void comedi_error(const struct comedi_device
*dev
, const char *s
)
2297 dev_err(dev
->class_dev
, "%s: %s\n", dev
->driver
->driver_name
, s
);
2299 EXPORT_SYMBOL(comedi_error
);
2301 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2303 struct comedi_async
*async
= s
->async
;
2304 unsigned runflags
= 0;
2305 unsigned runflags_mask
= 0;
2307 /* DPRINTK("comedi_event 0x%x\n",mask); */
2309 if ((comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) == 0)
2313 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
2314 COMEDI_CB_OVERFLOW
)) {
2315 runflags_mask
|= SRF_RUNNING
;
2317 /* remember if an error event has occurred, so an error
2318 * can be returned the next time the user does a read() */
2319 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2320 runflags_mask
|= SRF_ERROR
;
2321 runflags
|= SRF_ERROR
;
2323 if (runflags_mask
) {
2324 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2325 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2328 if (async
->cb_mask
& s
->async
->events
) {
2329 if (comedi_get_subdevice_runflags(s
) & SRF_USER
) {
2330 wake_up_interruptible(&async
->wait_head
);
2331 if (s
->subdev_flags
& SDF_CMD_READ
)
2332 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2333 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2334 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2337 async
->cb_func(s
->async
->events
, async
->cb_arg
);
2340 s
->async
->events
= 0;
2342 EXPORT_SYMBOL(comedi_event
);
2344 unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
2346 unsigned long flags
;
2349 spin_lock_irqsave(&s
->spin_lock
, flags
);
2350 runflags
= s
->runflags
;
2351 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2354 EXPORT_SYMBOL(comedi_get_subdevice_runflags
);
2356 static int is_device_busy(struct comedi_device
*dev
)
2358 struct comedi_subdevice
*s
;
2364 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2365 s
= &dev
->subdevices
[i
];
2368 if (s
->async
&& s
->async
->mmap_count
)
2375 static void comedi_device_init(struct comedi_device
*dev
)
2377 memset(dev
, 0, sizeof(struct comedi_device
));
2378 spin_lock_init(&dev
->spinlock
);
2379 mutex_init(&dev
->mutex
);
2383 static void comedi_device_cleanup(struct comedi_device
*dev
)
2387 mutex_lock(&dev
->mutex
);
2388 comedi_device_detach(dev
);
2389 mutex_unlock(&dev
->mutex
);
2390 mutex_destroy(&dev
->mutex
);
2393 int comedi_alloc_board_minor(struct device
*hardware_device
)
2395 struct comedi_device_file_info
*info
;
2396 struct device
*csdev
;
2399 info
= kzalloc(sizeof(struct comedi_device_file_info
), GFP_KERNEL
);
2402 info
->device
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2403 if (info
->device
== NULL
) {
2407 info
->hardware_device
= hardware_device
;
2408 comedi_device_init(info
->device
);
2409 spin_lock(&comedi_file_info_table_lock
);
2410 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2411 if (comedi_file_info_table
[i
] == NULL
) {
2412 comedi_file_info_table
[i
] = info
;
2416 spin_unlock(&comedi_file_info_table_lock
);
2417 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2418 comedi_device_cleanup(info
->device
);
2419 kfree(info
->device
);
2421 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2424 info
->device
->minor
= i
;
2425 csdev
= device_create(comedi_class
, hardware_device
,
2426 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2428 info
->device
->class_dev
= csdev
;
2429 dev_set_drvdata(csdev
, info
);
2434 void comedi_free_board_minor(unsigned minor
)
2436 struct comedi_device_file_info
*info
;
2438 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2439 spin_lock(&comedi_file_info_table_lock
);
2440 info
= comedi_file_info_table
[minor
];
2441 comedi_file_info_table
[minor
] = NULL
;
2442 spin_unlock(&comedi_file_info_table_lock
);
2445 struct comedi_device
*dev
= info
->device
;
2447 if (dev
->class_dev
) {
2448 device_destroy(comedi_class
,
2449 MKDEV(COMEDI_MAJOR
, dev
->minor
));
2451 comedi_device_cleanup(dev
);
2458 int comedi_find_board_minor(struct device
*hardware_device
)
2461 struct comedi_device_file_info
*info
;
2463 for (minor
= 0; minor
< COMEDI_NUM_BOARD_MINORS
; minor
++) {
2464 spin_lock(&comedi_file_info_table_lock
);
2465 info
= comedi_file_info_table
[minor
];
2466 if (info
&& info
->hardware_device
== hardware_device
) {
2467 spin_unlock(&comedi_file_info_table_lock
);
2470 spin_unlock(&comedi_file_info_table_lock
);
2475 int comedi_alloc_subdevice_minor(struct comedi_device
*dev
,
2476 struct comedi_subdevice
*s
)
2478 struct comedi_device_file_info
*info
;
2479 struct device
*csdev
;
2482 info
= kmalloc(sizeof(struct comedi_device_file_info
), GFP_KERNEL
);
2486 info
->read_subdevice
= s
;
2487 info
->write_subdevice
= s
;
2488 spin_lock(&comedi_file_info_table_lock
);
2489 for (i
= COMEDI_FIRST_SUBDEVICE_MINOR
; i
< COMEDI_NUM_MINORS
; ++i
) {
2490 if (comedi_file_info_table
[i
] == NULL
) {
2491 comedi_file_info_table
[i
] = info
;
2495 spin_unlock(&comedi_file_info_table_lock
);
2496 if (i
== COMEDI_NUM_MINORS
) {
2498 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2502 csdev
= device_create(comedi_class
, dev
->class_dev
,
2503 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2504 dev
->minor
, (int)(s
- dev
->subdevices
));
2506 s
->class_dev
= csdev
;
2507 dev_set_drvdata(csdev
, info
);
2512 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2514 struct comedi_device_file_info
*info
;
2521 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2522 BUG_ON(s
->minor
< COMEDI_FIRST_SUBDEVICE_MINOR
);
2524 spin_lock(&comedi_file_info_table_lock
);
2525 info
= comedi_file_info_table
[s
->minor
];
2526 comedi_file_info_table
[s
->minor
] = NULL
;
2527 spin_unlock(&comedi_file_info_table_lock
);
2530 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2531 s
->class_dev
= NULL
;
2536 struct comedi_device_file_info
*comedi_get_device_file_info(unsigned minor
)
2538 struct comedi_device_file_info
*info
;
2540 BUG_ON(minor
>= COMEDI_NUM_MINORS
);
2541 spin_lock(&comedi_file_info_table_lock
);
2542 info
= comedi_file_info_table
[minor
];
2543 spin_unlock(&comedi_file_info_table_lock
);
2546 EXPORT_SYMBOL_GPL(comedi_get_device_file_info
);