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 #include "comedi_compat32.h"
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/fcntl.h>
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/kmod.h>
38 #include <linux/poll.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/vmalloc.h>
43 #include "comedidev.h"
44 #include <linux/cdev.h>
45 #include <linux/stat.h>
48 #include <linux/uaccess.h>
50 #include "comedi_internal.h"
52 #ifdef CONFIG_COMEDI_DEBUG
54 EXPORT_SYMBOL(comedi_debug
);
55 module_param(comedi_debug
, int, S_IRUGO
| S_IWUSR
);
56 MODULE_PARM_DESC(comedi_debug
,
57 "enable comedi core and driver debugging if non-zero (default 0)"
61 static int comedi_num_legacy_minors
;
62 module_param(comedi_num_legacy_minors
, int, S_IRUGO
);
63 MODULE_PARM_DESC(comedi_num_legacy_minors
,
64 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
67 unsigned int comedi_default_buf_size_kb
= CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
;
68 module_param(comedi_default_buf_size_kb
, uint
, S_IRUGO
| S_IWUSR
);
69 MODULE_PARM_DESC(comedi_default_buf_size_kb
,
70 "default asynchronous buffer size in KiB (default "
71 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
) ")");
73 unsigned int comedi_default_buf_maxsize_kb
74 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
;
75 module_param(comedi_default_buf_maxsize_kb
, uint
, S_IRUGO
| S_IWUSR
);
76 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb
,
77 "default maximum size of asynchronous buffer in KiB (default "
78 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
) ")");
80 struct comedi_file_info
{
81 struct comedi_device
*device
;
82 struct comedi_subdevice
*read_subdevice
;
83 struct comedi_subdevice
*write_subdevice
;
84 struct device
*hardware_device
;
87 static DEFINE_SPINLOCK(comedi_file_info_table_lock
);
88 static struct comedi_file_info
*comedi_file_info_table
[COMEDI_NUM_MINORS
];
90 static struct comedi_file_info
*comedi_file_info_from_minor(unsigned minor
)
92 struct comedi_file_info
*info
;
94 BUG_ON(minor
>= COMEDI_NUM_MINORS
);
95 spin_lock(&comedi_file_info_table_lock
);
96 info
= comedi_file_info_table
[minor
];
97 spin_unlock(&comedi_file_info_table_lock
);
101 static struct comedi_device
*
102 comedi_dev_from_file_info(struct comedi_file_info
*info
)
104 return info
? info
->device
: NULL
;
107 struct comedi_device
*comedi_dev_from_minor(unsigned minor
)
109 return comedi_dev_from_file_info(comedi_file_info_from_minor(minor
));
111 EXPORT_SYMBOL_GPL(comedi_dev_from_minor
);
113 static struct comedi_subdevice
*
114 comedi_read_subdevice(const struct comedi_file_info
*info
)
116 if (info
->read_subdevice
)
117 return info
->read_subdevice
;
119 return info
->device
->read_subdev
;
123 static struct comedi_subdevice
*
124 comedi_write_subdevice(const struct comedi_file_info
*info
)
126 if (info
->write_subdevice
)
127 return info
->write_subdevice
;
129 return info
->device
->write_subdev
;
133 static int resize_async_buffer(struct comedi_device
*dev
,
134 struct comedi_subdevice
*s
,
135 struct comedi_async
*async
, unsigned new_size
)
139 if (new_size
> async
->max_bufsize
)
143 DPRINTK("subdevice is busy, cannot resize buffer\n");
146 if (async
->mmap_count
) {
147 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
151 if (!async
->prealloc_buf
)
154 /* make sure buffer is an integral number of pages
156 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
158 retval
= comedi_buf_alloc(dev
, s
, new_size
);
163 retval
= s
->buf_change(dev
, s
, new_size
);
168 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
169 dev
->minor
, s
->index
, async
->prealloc_bufsz
);
173 /* sysfs attribute files */
175 static ssize_t
show_max_read_buffer_kb(struct device
*dev
,
176 struct device_attribute
*attr
, char *buf
)
178 struct comedi_file_info
*info
= dev_get_drvdata(dev
);
179 struct comedi_subdevice
*s
= comedi_read_subdevice(info
);
180 unsigned int size
= 0;
182 mutex_lock(&info
->device
->mutex
);
183 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
184 size
= s
->async
->max_bufsize
/ 1024;
185 mutex_unlock(&info
->device
->mutex
);
187 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
190 static ssize_t
store_max_read_buffer_kb(struct device
*dev
,
191 struct device_attribute
*attr
,
192 const char *buf
, size_t count
)
194 struct comedi_file_info
*info
= dev_get_drvdata(dev
);
195 struct comedi_subdevice
*s
= comedi_read_subdevice(info
);
199 err
= kstrtouint(buf
, 10, &size
);
202 if (size
> (UINT_MAX
/ 1024))
206 mutex_lock(&info
->device
->mutex
);
207 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
208 s
->async
->max_bufsize
= size
;
211 mutex_unlock(&info
->device
->mutex
);
213 return err
? err
: count
;
216 static ssize_t
show_read_buffer_kb(struct device
*dev
,
217 struct device_attribute
*attr
, char *buf
)
219 struct comedi_file_info
*info
= dev_get_drvdata(dev
);
220 struct comedi_subdevice
*s
= comedi_read_subdevice(info
);
221 unsigned int size
= 0;
223 mutex_lock(&info
->device
->mutex
);
224 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
225 size
= s
->async
->prealloc_bufsz
/ 1024;
226 mutex_unlock(&info
->device
->mutex
);
228 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
231 static ssize_t
store_read_buffer_kb(struct device
*dev
,
232 struct device_attribute
*attr
,
233 const char *buf
, size_t count
)
235 struct comedi_file_info
*info
= dev_get_drvdata(dev
);
236 struct comedi_subdevice
*s
= comedi_read_subdevice(info
);
240 err
= kstrtouint(buf
, 10, &size
);
243 if (size
> (UINT_MAX
/ 1024))
247 mutex_lock(&info
->device
->mutex
);
248 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
249 err
= resize_async_buffer(info
->device
, s
, s
->async
, size
);
252 mutex_unlock(&info
->device
->mutex
);
254 return err
? err
: count
;
257 static ssize_t
show_max_write_buffer_kb(struct device
*dev
,
258 struct device_attribute
*attr
,
261 struct comedi_file_info
*info
= dev_get_drvdata(dev
);
262 struct comedi_subdevice
*s
= comedi_write_subdevice(info
);
263 unsigned int size
= 0;
265 mutex_lock(&info
->device
->mutex
);
266 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
267 size
= s
->async
->max_bufsize
/ 1024;
268 mutex_unlock(&info
->device
->mutex
);
270 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
273 static ssize_t
store_max_write_buffer_kb(struct device
*dev
,
274 struct device_attribute
*attr
,
275 const char *buf
, size_t count
)
277 struct comedi_file_info
*info
= dev_get_drvdata(dev
);
278 struct comedi_subdevice
*s
= comedi_write_subdevice(info
);
282 err
= kstrtouint(buf
, 10, &size
);
285 if (size
> (UINT_MAX
/ 1024))
289 mutex_lock(&info
->device
->mutex
);
290 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
291 s
->async
->max_bufsize
= size
;
294 mutex_unlock(&info
->device
->mutex
);
296 return err
? err
: count
;
299 static ssize_t
show_write_buffer_kb(struct device
*dev
,
300 struct device_attribute
*attr
, char *buf
)
302 struct comedi_file_info
*info
= dev_get_drvdata(dev
);
303 struct comedi_subdevice
*s
= comedi_write_subdevice(info
);
304 unsigned int size
= 0;
306 mutex_lock(&info
->device
->mutex
);
307 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
308 size
= s
->async
->prealloc_bufsz
/ 1024;
309 mutex_unlock(&info
->device
->mutex
);
311 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
314 static ssize_t
store_write_buffer_kb(struct device
*dev
,
315 struct device_attribute
*attr
,
316 const char *buf
, size_t count
)
318 struct comedi_file_info
*info
= dev_get_drvdata(dev
);
319 struct comedi_subdevice
*s
= comedi_write_subdevice(info
);
323 err
= kstrtouint(buf
, 10, &size
);
326 if (size
> (UINT_MAX
/ 1024))
330 mutex_lock(&info
->device
->mutex
);
331 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
332 err
= resize_async_buffer(info
->device
, s
, s
->async
, size
);
335 mutex_unlock(&info
->device
->mutex
);
337 return err
? err
: count
;
340 static struct device_attribute comedi_dev_attrs
[] = {
341 __ATTR(max_read_buffer_kb
, S_IRUGO
| S_IWUSR
,
342 show_max_read_buffer_kb
, store_max_read_buffer_kb
),
343 __ATTR(read_buffer_kb
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
344 show_read_buffer_kb
, store_read_buffer_kb
),
345 __ATTR(max_write_buffer_kb
, S_IRUGO
| S_IWUSR
,
346 show_max_write_buffer_kb
, store_max_write_buffer_kb
),
347 __ATTR(write_buffer_kb
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
348 show_write_buffer_kb
, store_write_buffer_kb
),
352 static void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
353 unsigned mask
, unsigned bits
)
357 spin_lock_irqsave(&s
->spin_lock
, flags
);
358 s
->runflags
&= ~mask
;
359 s
->runflags
|= (bits
& mask
);
360 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
363 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
368 spin_lock_irqsave(&s
->spin_lock
, flags
);
369 runflags
= s
->runflags
;
370 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
374 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
)
376 unsigned runflags
= comedi_get_subdevice_runflags(s
);
378 return (runflags
& SRF_RUNNING
) ? true : false;
380 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running
);
382 static bool comedi_is_subdevice_in_error(struct comedi_subdevice
*s
)
384 unsigned runflags
= comedi_get_subdevice_runflags(s
);
386 return (runflags
& SRF_ERROR
) ? true : false;
389 static bool comedi_is_subdevice_idle(struct comedi_subdevice
*s
)
391 unsigned runflags
= comedi_get_subdevice_runflags(s
);
393 return (runflags
& (SRF_ERROR
| SRF_RUNNING
)) ? false : true;
397 This function restores a subdevice to an idle state.
399 static void do_become_nonbusy(struct comedi_device
*dev
,
400 struct comedi_subdevice
*s
)
402 struct comedi_async
*async
= s
->async
;
404 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
406 comedi_buf_reset(async
);
407 async
->inttrig
= NULL
;
408 kfree(async
->cmd
.chanlist
);
409 async
->cmd
.chanlist
= NULL
;
411 dev_err(dev
->class_dev
,
412 "BUG: (?) do_become_nonbusy called with async=NULL\n");
418 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
422 if (comedi_is_subdevice_running(s
) && s
->cancel
)
423 ret
= s
->cancel(dev
, s
);
425 do_become_nonbusy(dev
, s
);
430 static int is_device_busy(struct comedi_device
*dev
)
432 struct comedi_subdevice
*s
;
438 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
439 s
= &dev
->subdevices
[i
];
442 if (s
->async
&& s
->async
->mmap_count
)
454 pointer to devconfig structure
457 devconfig structure at arg
462 static int do_devconfig_ioctl(struct comedi_device
*dev
,
463 struct comedi_devconfig __user
*arg
)
465 struct comedi_devconfig it
;
468 if (!capable(CAP_SYS_ADMIN
))
472 if (is_device_busy(dev
))
475 struct module
*driver_module
= dev
->driver
->module
;
476 comedi_device_detach(dev
);
477 module_put(driver_module
);
482 if (copy_from_user(&it
, arg
, sizeof(it
)))
485 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
487 if (it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
488 dev_warn(dev
->class_dev
,
489 "comedi_config --init_data is deprecated\n");
493 ret
= comedi_device_attach(dev
, &it
);
495 if (!try_module_get(dev
->driver
->module
)) {
496 comedi_device_detach(dev
);
506 buffer configuration ioctl
509 pointer to bufconfig structure
515 modified bufconfig at arg
518 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
519 struct comedi_bufconfig __user
*arg
)
521 struct comedi_bufconfig bc
;
522 struct comedi_async
*async
;
523 struct comedi_subdevice
*s
;
526 if (copy_from_user(&bc
, arg
, sizeof(bc
)))
529 if (bc
.subdevice
>= dev
->n_subdevices
|| bc
.subdevice
< 0)
532 s
= &dev
->subdevices
[bc
.subdevice
];
536 DPRINTK("subdevice does not have async capability\n");
542 if (bc
.maximum_size
) {
543 if (!capable(CAP_SYS_ADMIN
))
546 async
->max_bufsize
= bc
.maximum_size
;
550 retval
= resize_async_buffer(dev
, s
, async
, bc
.size
);
555 bc
.size
= async
->prealloc_bufsz
;
556 bc
.maximum_size
= async
->max_bufsize
;
559 if (copy_to_user(arg
, &bc
, sizeof(bc
)))
570 pointer to devinfo structure
579 static int do_devinfo_ioctl(struct comedi_device
*dev
,
580 struct comedi_devinfo __user
*arg
,
583 const unsigned minor
= iminor(file_inode(file
));
584 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
585 struct comedi_subdevice
*s
;
586 struct comedi_devinfo devinfo
;
588 memset(&devinfo
, 0, sizeof(devinfo
));
590 /* fill devinfo structure */
591 devinfo
.version_code
= COMEDI_VERSION_CODE
;
592 devinfo
.n_subdevs
= dev
->n_subdevices
;
593 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
594 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
596 s
= comedi_read_subdevice(info
);
598 devinfo
.read_subdevice
= s
->index
;
600 devinfo
.read_subdevice
= -1;
602 s
= comedi_write_subdevice(info
);
604 devinfo
.write_subdevice
= s
->index
;
606 devinfo
.write_subdevice
= -1;
608 if (copy_to_user(arg
, &devinfo
, sizeof(devinfo
)))
619 pointer to array of subdevice info structures
625 array of subdevice info structures at arg
628 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
629 struct comedi_subdinfo __user
*arg
, void *file
)
632 struct comedi_subdinfo
*tmp
, *us
;
633 struct comedi_subdevice
*s
;
635 tmp
= kcalloc(dev
->n_subdevices
, sizeof(*tmp
), GFP_KERNEL
);
639 /* fill subdinfo structs */
640 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
641 s
= &dev
->subdevices
[i
];
645 us
->n_chan
= s
->n_chan
;
646 us
->subd_flags
= s
->subdev_flags
;
647 if (comedi_is_subdevice_running(s
))
648 us
->subd_flags
|= SDF_RUNNING
;
649 #define TIMER_nanosec 5 /* backwards compatibility */
650 us
->timer_type
= TIMER_nanosec
;
651 us
->len_chanlist
= s
->len_chanlist
;
652 us
->maxdata
= s
->maxdata
;
653 if (s
->range_table
) {
655 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
657 us
->range_type
= 0; /* XXX */
659 us
->flags
= s
->flags
;
662 us
->subd_flags
|= SDF_BUSY
;
664 us
->subd_flags
|= SDF_BUSY_OWNER
;
666 us
->subd_flags
|= SDF_LOCKED
;
668 us
->subd_flags
|= SDF_LOCK_OWNER
;
669 if (!s
->maxdata
&& s
->maxdata_list
)
670 us
->subd_flags
|= SDF_MAXDATA
;
672 us
->subd_flags
|= SDF_FLAGS
;
673 if (s
->range_table_list
)
674 us
->subd_flags
|= SDF_RANGETYPE
;
676 us
->subd_flags
|= SDF_CMD
;
678 if (s
->insn_bits
!= &insn_inval
)
679 us
->insn_bits_support
= COMEDI_SUPPORTED
;
681 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
683 us
->settling_time_0
= s
->settling_time_0
;
686 ret
= copy_to_user(arg
, tmp
, dev
->n_subdevices
* sizeof(*tmp
));
690 return ret
? -EFAULT
: 0;
698 pointer to chaninfo structure
701 chaninfo structure at arg
704 arrays at elements of chaninfo structure
707 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
708 struct comedi_chaninfo __user
*arg
)
710 struct comedi_subdevice
*s
;
711 struct comedi_chaninfo it
;
713 if (copy_from_user(&it
, arg
, sizeof(it
)))
716 if (it
.subdev
>= dev
->n_subdevices
)
718 s
= &dev
->subdevices
[it
.subdev
];
720 if (it
.maxdata_list
) {
721 if (s
->maxdata
|| !s
->maxdata_list
)
723 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
724 s
->n_chan
* sizeof(unsigned int)))
731 if (copy_to_user(it
.flaglist
, s
->flaglist
,
732 s
->n_chan
* sizeof(unsigned int)))
739 if (!s
->range_table_list
)
741 for (i
= 0; i
< s
->n_chan
; i
++) {
744 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
745 (s
->range_table_list
[i
]->length
);
746 if (put_user(x
, it
.rangelist
+ i
))
750 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
751 s
->n_chan
* sizeof(unsigned int)))
761 buffer information ioctl
764 pointer to bufinfo structure
770 modified bufinfo at arg
773 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
774 struct comedi_bufinfo __user
*arg
, void *file
)
776 struct comedi_bufinfo bi
;
777 struct comedi_subdevice
*s
;
778 struct comedi_async
*async
;
780 if (copy_from_user(&bi
, arg
, sizeof(bi
)))
783 if (bi
.subdevice
>= dev
->n_subdevices
|| bi
.subdevice
< 0)
786 s
= &dev
->subdevices
[bi
.subdevice
];
788 if (s
->lock
&& s
->lock
!= file
)
794 DPRINTK("subdevice does not have async capability\n");
795 bi
.buf_write_ptr
= 0;
797 bi
.buf_write_count
= 0;
798 bi
.buf_read_count
= 0;
800 bi
.bytes_written
= 0;
805 bi
.bytes_written
= 0;
806 goto copyback_position
;
811 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
812 bi
.bytes_read
= comedi_buf_read_alloc(async
, bi
.bytes_read
);
813 comedi_buf_read_free(async
, bi
.bytes_read
);
815 if (comedi_is_subdevice_idle(s
) &&
816 async
->buf_write_count
== async
->buf_read_count
) {
817 do_become_nonbusy(dev
, s
);
821 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
823 comedi_buf_write_alloc(async
, bi
.bytes_written
);
824 comedi_buf_write_free(async
, bi
.bytes_written
);
828 bi
.buf_write_count
= async
->buf_write_count
;
829 bi
.buf_write_ptr
= async
->buf_write_ptr
;
830 bi
.buf_read_count
= async
->buf_read_count
;
831 bi
.buf_read_ptr
= async
->buf_read_ptr
;
834 if (copy_to_user(arg
, &bi
, sizeof(bi
)))
840 static int check_insn_config_length(struct comedi_insn
*insn
,
847 case INSN_CONFIG_DIO_OUTPUT
:
848 case INSN_CONFIG_DIO_INPUT
:
849 case INSN_CONFIG_DISARM
:
850 case INSN_CONFIG_RESET
:
854 case INSN_CONFIG_ARM
:
855 case INSN_CONFIG_DIO_QUERY
:
856 case INSN_CONFIG_BLOCK_SIZE
:
857 case INSN_CONFIG_FILTER
:
858 case INSN_CONFIG_SERIAL_CLOCK
:
859 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
860 case INSN_CONFIG_ALT_SOURCE
:
861 case INSN_CONFIG_SET_COUNTER_MODE
:
862 case INSN_CONFIG_8254_READ_STATUS
:
863 case INSN_CONFIG_SET_ROUTING
:
864 case INSN_CONFIG_GET_ROUTING
:
865 case INSN_CONFIG_GET_PWM_STATUS
:
866 case INSN_CONFIG_PWM_SET_PERIOD
:
867 case INSN_CONFIG_PWM_GET_PERIOD
:
871 case INSN_CONFIG_SET_GATE_SRC
:
872 case INSN_CONFIG_GET_GATE_SRC
:
873 case INSN_CONFIG_SET_CLOCK_SRC
:
874 case INSN_CONFIG_GET_CLOCK_SRC
:
875 case INSN_CONFIG_SET_OTHER_SRC
:
876 case INSN_CONFIG_GET_COUNTER_STATUS
:
877 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
878 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
879 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
883 case INSN_CONFIG_PWM_OUTPUT
:
884 case INSN_CONFIG_ANALOG_TRIG
:
888 case INSN_CONFIG_DIGITAL_TRIG
:
892 /* by default we allow the insn since we don't have checks for
893 * all possible cases yet */
895 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
897 pr_warn("comedi: Add a check to %s in %s.\n",
899 pr_warn("comedi: Assuming n=%i is correct.\n", insn
->n
);
905 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
906 unsigned int *data
, void *file
)
908 struct comedi_subdevice
*s
;
912 if (insn
->insn
& INSN_MASK_SPECIAL
) {
913 /* a non-subdevice instruction */
915 switch (insn
->insn
) {
925 do_gettimeofday(&tv
);
927 data
[1] = tv
.tv_usec
;
933 if (insn
->n
!= 1 || data
[0] >= 100000) {
937 udelay(data
[0] / 1000);
945 if (insn
->subdev
>= dev
->n_subdevices
) {
946 DPRINTK("%d not usable subdevice\n",
951 s
= &dev
->subdevices
[insn
->subdev
];
953 DPRINTK("no async\n");
957 if (!s
->async
->inttrig
) {
958 DPRINTK("no inttrig\n");
962 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
967 DPRINTK("invalid insn\n");
972 /* a subdevice instruction */
973 unsigned int maxdata
;
975 if (insn
->subdev
>= dev
->n_subdevices
) {
976 DPRINTK("subdevice %d out of range\n", insn
->subdev
);
980 s
= &dev
->subdevices
[insn
->subdev
];
982 if (s
->type
== COMEDI_SUBD_UNUSED
) {
983 DPRINTK("%d not usable subdevice\n", insn
->subdev
);
988 /* are we locked? (ioctl lock) */
989 if (s
->lock
&& s
->lock
!= file
) {
990 DPRINTK("device locked\n");
995 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
998 DPRINTK("bad chanspec\n");
1006 /* This looks arbitrary. It is. */
1007 s
->busy
= &parse_insn
;
1008 switch (insn
->insn
) {
1010 ret
= s
->insn_read(dev
, s
, insn
, data
);
1013 maxdata
= s
->maxdata_list
1014 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1016 for (i
= 0; i
< insn
->n
; ++i
) {
1017 if (data
[i
] > maxdata
) {
1019 DPRINTK("bad data value(s)\n");
1024 ret
= s
->insn_write(dev
, s
, insn
, data
);
1030 /* Most drivers ignore the base channel in
1031 * insn->chanspec. Fix this here if
1032 * the subdevice has <= 32 channels. */
1034 unsigned int orig_mask
;
1036 orig_mask
= data
[0];
1037 if (s
->n_chan
<= 32) {
1038 shift
= CR_CHAN(insn
->chanspec
);
1046 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1047 data
[0] = orig_mask
;
1053 ret
= check_insn_config_length(insn
, data
);
1056 ret
= s
->insn_config(dev
, s
, insn
, data
);
1072 * synchronous instructions
1075 * pointer to sync cmd structure
1078 * sync cmd struct at arg
1085 /* arbitrary limits */
1086 #define MAX_SAMPLES 256
1087 static int do_insnlist_ioctl(struct comedi_device
*dev
,
1088 struct comedi_insnlist __user
*arg
, void *file
)
1090 struct comedi_insnlist insnlist
;
1091 struct comedi_insn
*insns
= NULL
;
1092 unsigned int *data
= NULL
;
1096 if (copy_from_user(&insnlist
, arg
, sizeof(insnlist
)))
1099 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1101 DPRINTK("kmalloc failed\n");
1106 insns
= kcalloc(insnlist
.n_insns
, sizeof(*insns
), GFP_KERNEL
);
1108 DPRINTK("kmalloc failed\n");
1113 if (copy_from_user(insns
, insnlist
.insns
,
1114 sizeof(*insns
) * insnlist
.n_insns
)) {
1115 DPRINTK("copy_from_user failed\n");
1120 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
1121 if (insns
[i
].n
> MAX_SAMPLES
) {
1122 DPRINTK("number of samples too large\n");
1126 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
1127 if (copy_from_user(data
, insns
[i
].data
,
1128 insns
[i
].n
* sizeof(unsigned int))) {
1129 DPRINTK("copy_from_user failed\n");
1134 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
1137 if (insns
[i
].insn
& INSN_MASK_READ
) {
1138 if (copy_to_user(insns
[i
].data
, data
,
1139 insns
[i
].n
* sizeof(unsigned int))) {
1140 DPRINTK("copy_to_user failed\n");
1160 * synchronous instructions
1166 * struct comedi_insn struct at arg
1172 static int do_insn_ioctl(struct comedi_device
*dev
,
1173 struct comedi_insn __user
*arg
, void *file
)
1175 struct comedi_insn insn
;
1176 unsigned int *data
= NULL
;
1179 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1185 if (copy_from_user(&insn
, arg
, sizeof(insn
))) {
1190 /* This is where the behavior of insn and insnlist deviate. */
1191 if (insn
.n
> MAX_SAMPLES
)
1192 insn
.n
= MAX_SAMPLES
;
1193 if (insn
.insn
& INSN_MASK_WRITE
) {
1194 if (copy_from_user(data
,
1196 insn
.n
* sizeof(unsigned int))) {
1201 ret
= parse_insn(dev
, &insn
, data
, file
);
1204 if (insn
.insn
& INSN_MASK_READ
) {
1205 if (copy_to_user(insn
.data
,
1207 insn
.n
* sizeof(unsigned int))) {
1220 static int do_cmd_ioctl(struct comedi_device
*dev
,
1221 struct comedi_cmd __user
*arg
, void *file
)
1223 struct comedi_cmd cmd
;
1224 struct comedi_subdevice
*s
;
1225 struct comedi_async
*async
;
1227 unsigned int __user
*user_chanlist
;
1229 if (copy_from_user(&cmd
, arg
, sizeof(cmd
))) {
1230 DPRINTK("bad cmd address\n");
1233 /* save user's chanlist pointer so it can be restored later */
1234 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1236 if (cmd
.subdev
>= dev
->n_subdevices
) {
1237 DPRINTK("%d no such subdevice\n", cmd
.subdev
);
1241 s
= &dev
->subdevices
[cmd
.subdev
];
1244 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1245 DPRINTK("%d not valid subdevice\n", cmd
.subdev
);
1249 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1250 DPRINTK("subdevice %i does not support commands\n",
1255 /* are we locked? (ioctl lock) */
1256 if (s
->lock
&& s
->lock
!= file
) {
1257 DPRINTK("subdevice locked\n");
1263 DPRINTK("subdevice busy\n");
1268 /* make sure channel/gain list isn't too long */
1269 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1270 DPRINTK("channel/gain list too long %u > %d\n",
1271 cmd
.chanlist_len
, s
->len_chanlist
);
1276 /* make sure channel/gain list isn't too short */
1277 if (cmd
.chanlist_len
< 1) {
1278 DPRINTK("channel/gain list too short %u < 1\n",
1285 async
->cmd
.data
= NULL
;
1286 /* load channel/gain list */
1287 async
->cmd
.chanlist
=
1288 kmalloc(async
->cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1289 if (!async
->cmd
.chanlist
) {
1290 DPRINTK("allocation failed\n");
1295 if (copy_from_user(async
->cmd
.chanlist
, user_chanlist
,
1296 async
->cmd
.chanlist_len
* sizeof(int))) {
1297 DPRINTK("fault reading chanlist\n");
1302 /* make sure each element in channel/gain list is valid */
1303 ret
= comedi_check_chanlist(s
,
1304 async
->cmd
.chanlist_len
,
1305 async
->cmd
.chanlist
);
1307 DPRINTK("bad chanlist\n");
1311 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1313 if (async
->cmd
.flags
& TRIG_BOGUS
|| ret
) {
1314 DPRINTK("test returned %d\n", ret
);
1316 /* restore chanlist pointer before copying back */
1317 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1319 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1320 DPRINTK("fault writing cmd\n");
1328 if (!async
->prealloc_bufsz
) {
1330 DPRINTK("no buffer (?)\n");
1334 comedi_buf_reset(async
);
1337 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1339 if (async
->cmd
.flags
& TRIG_WAKE_EOS
)
1340 async
->cb_mask
|= COMEDI_CB_EOS
;
1342 comedi_set_subdevice_runflags(s
, ~0, SRF_USER
| SRF_RUNNING
);
1344 ret
= s
->do_cmd(dev
, s
);
1349 do_become_nonbusy(dev
, s
);
1356 command testing ioctl
1359 pointer to cmd structure
1362 cmd structure at arg
1366 modified cmd structure at arg
1369 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1370 struct comedi_cmd __user
*arg
, void *file
)
1372 struct comedi_cmd cmd
;
1373 struct comedi_subdevice
*s
;
1375 unsigned int *chanlist
= NULL
;
1376 unsigned int __user
*user_chanlist
;
1378 if (copy_from_user(&cmd
, arg
, sizeof(cmd
))) {
1379 DPRINTK("bad cmd address\n");
1382 /* save user's chanlist pointer so it can be restored later */
1383 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1385 if (cmd
.subdev
>= dev
->n_subdevices
) {
1386 DPRINTK("%d no such subdevice\n", cmd
.subdev
);
1390 s
= &dev
->subdevices
[cmd
.subdev
];
1391 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1392 DPRINTK("%d not valid subdevice\n", cmd
.subdev
);
1396 if (!s
->do_cmd
|| !s
->do_cmdtest
) {
1397 DPRINTK("subdevice %i does not support commands\n",
1402 /* make sure channel/gain list isn't too long */
1403 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1404 DPRINTK("channel/gain list too long %d > %d\n",
1405 cmd
.chanlist_len
, s
->len_chanlist
);
1410 /* load channel/gain list */
1413 kmalloc(cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1415 DPRINTK("allocation failed\n");
1420 if (copy_from_user(chanlist
, user_chanlist
,
1421 cmd
.chanlist_len
* sizeof(int))) {
1422 DPRINTK("fault reading chanlist\n");
1427 /* make sure each element in channel/gain list is valid */
1428 ret
= comedi_check_chanlist(s
, cmd
.chanlist_len
, chanlist
);
1430 DPRINTK("bad chanlist\n");
1434 cmd
.chanlist
= chanlist
;
1437 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1439 /* restore chanlist pointer before copying back */
1440 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1442 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1443 DPRINTK("bad cmd address\n");
1468 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1472 unsigned long flags
;
1473 struct comedi_subdevice
*s
;
1475 if (arg
>= dev
->n_subdevices
)
1477 s
= &dev
->subdevices
[arg
];
1479 spin_lock_irqsave(&s
->spin_lock
, flags
);
1480 if (s
->busy
|| s
->lock
)
1484 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1491 ret
= s
->lock_f(dev
, s
);
1510 This function isn't protected by the semaphore, since
1511 we already own the lock.
1513 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1516 struct comedi_subdevice
*s
;
1518 if (arg
>= dev
->n_subdevices
)
1520 s
= &dev
->subdevices
[arg
];
1525 if (s
->lock
&& s
->lock
!= file
)
1528 if (s
->lock
== file
) {
1542 cancel acquisition ioctl
1554 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1557 struct comedi_subdevice
*s
;
1559 if (arg
>= dev
->n_subdevices
)
1561 s
= &dev
->subdevices
[arg
];
1562 if (s
->async
== NULL
)
1565 if (s
->lock
&& s
->lock
!= file
)
1571 if (s
->busy
!= file
)
1574 return do_cancel(dev
, s
);
1579 instructs driver to synchronize buffers
1591 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1594 struct comedi_subdevice
*s
;
1596 if (arg
>= dev
->n_subdevices
)
1598 s
= &dev
->subdevices
[arg
];
1600 if (s
->lock
&& s
->lock
!= file
)
1606 if (s
->busy
!= file
)
1610 return s
->poll(dev
, s
);
1615 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
1618 const unsigned minor
= iminor(file_inode(file
));
1619 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1620 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1626 mutex_lock(&dev
->mutex
);
1628 /* Device config is special, because it must work on
1629 * an unconfigured device. */
1630 if (cmd
== COMEDI_DEVCONFIG
) {
1631 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
1632 /* Device config not appropriate on non-board minors. */
1636 rc
= do_devconfig_ioctl(dev
,
1637 (struct comedi_devconfig __user
*)arg
);
1641 if (!dev
->attached
) {
1642 DPRINTK("no driver configured on /dev/comedi%i\n", dev
->minor
);
1648 case COMEDI_BUFCONFIG
:
1649 rc
= do_bufconfig_ioctl(dev
,
1650 (struct comedi_bufconfig __user
*)arg
);
1652 case COMEDI_DEVINFO
:
1653 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
1656 case COMEDI_SUBDINFO
:
1657 rc
= do_subdinfo_ioctl(dev
,
1658 (struct comedi_subdinfo __user
*)arg
,
1661 case COMEDI_CHANINFO
:
1662 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
1664 case COMEDI_RANGEINFO
:
1665 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
1667 case COMEDI_BUFINFO
:
1668 rc
= do_bufinfo_ioctl(dev
,
1669 (struct comedi_bufinfo __user
*)arg
,
1673 rc
= do_lock_ioctl(dev
, arg
, file
);
1676 rc
= do_unlock_ioctl(dev
, arg
, file
);
1679 rc
= do_cancel_ioctl(dev
, arg
, file
);
1682 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
1684 case COMEDI_CMDTEST
:
1685 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
1688 case COMEDI_INSNLIST
:
1689 rc
= do_insnlist_ioctl(dev
,
1690 (struct comedi_insnlist __user
*)arg
,
1694 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
1698 rc
= do_poll_ioctl(dev
, arg
, file
);
1706 mutex_unlock(&dev
->mutex
);
1710 static void comedi_vm_open(struct vm_area_struct
*area
)
1712 struct comedi_async
*async
;
1713 struct comedi_device
*dev
;
1715 async
= area
->vm_private_data
;
1716 dev
= async
->subdevice
->device
;
1718 mutex_lock(&dev
->mutex
);
1719 async
->mmap_count
++;
1720 mutex_unlock(&dev
->mutex
);
1723 static void comedi_vm_close(struct vm_area_struct
*area
)
1725 struct comedi_async
*async
;
1726 struct comedi_device
*dev
;
1728 async
= area
->vm_private_data
;
1729 dev
= async
->subdevice
->device
;
1731 mutex_lock(&dev
->mutex
);
1732 async
->mmap_count
--;
1733 mutex_unlock(&dev
->mutex
);
1736 static struct vm_operations_struct comedi_vm_ops
= {
1737 .open
= comedi_vm_open
,
1738 .close
= comedi_vm_close
,
1741 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1743 const unsigned minor
= iminor(file_inode(file
));
1744 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1745 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1746 struct comedi_subdevice
*s
;
1747 struct comedi_async
*async
;
1748 unsigned long start
= vma
->vm_start
;
1757 mutex_lock(&dev
->mutex
);
1759 if (!dev
->attached
) {
1760 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1765 if (vma
->vm_flags
& VM_WRITE
)
1766 s
= comedi_write_subdevice(info
);
1768 s
= comedi_read_subdevice(info
);
1780 if (vma
->vm_pgoff
!= 0) {
1781 DPRINTK("comedi: mmap() offset must be 0.\n");
1786 size
= vma
->vm_end
- vma
->vm_start
;
1787 if (size
> async
->prealloc_bufsz
) {
1791 if (size
& (~PAGE_MASK
)) {
1796 n_pages
= size
>> PAGE_SHIFT
;
1797 for (i
= 0; i
< n_pages
; ++i
) {
1798 struct comedi_buf_page
*buf
= &async
->buf_page_list
[i
];
1800 if (remap_pfn_range(vma
, start
,
1801 page_to_pfn(virt_to_page(buf
->virt_addr
)),
1802 PAGE_SIZE
, PAGE_SHARED
)) {
1809 vma
->vm_ops
= &comedi_vm_ops
;
1810 vma
->vm_private_data
= async
;
1812 async
->mmap_count
++;
1816 mutex_unlock(&dev
->mutex
);
1820 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
1822 unsigned int mask
= 0;
1823 const unsigned minor
= iminor(file_inode(file
));
1824 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1825 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1826 struct comedi_subdevice
*s
;
1831 mutex_lock(&dev
->mutex
);
1833 if (!dev
->attached
) {
1834 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1838 s
= comedi_read_subdevice(info
);
1839 if (s
&& s
->async
) {
1840 poll_wait(file
, &s
->async
->wait_head
, wait
);
1841 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
1842 comedi_buf_read_n_available(s
->async
) > 0)
1843 mask
|= POLLIN
| POLLRDNORM
;
1846 s
= comedi_write_subdevice(info
);
1847 if (s
&& s
->async
) {
1848 unsigned int bps
= bytes_per_sample(s
->async
->subdevice
);
1850 poll_wait(file
, &s
->async
->wait_head
, wait
);
1851 comedi_buf_write_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1852 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
1853 comedi_buf_write_n_allocated(s
->async
) >= bps
)
1854 mask
|= POLLOUT
| POLLWRNORM
;
1858 mutex_unlock(&dev
->mutex
);
1862 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
1863 size_t nbytes
, loff_t
*offset
)
1865 struct comedi_subdevice
*s
;
1866 struct comedi_async
*async
;
1867 int n
, m
, count
= 0, retval
= 0;
1868 DECLARE_WAITQUEUE(wait
, current
);
1869 const unsigned minor
= iminor(file_inode(file
));
1870 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1871 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1876 if (!dev
->attached
) {
1877 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1881 s
= comedi_write_subdevice(info
);
1882 if (!s
|| !s
->async
)
1887 if (!s
->busy
|| !nbytes
)
1889 if (s
->busy
!= file
)
1892 add_wait_queue(&async
->wait_head
, &wait
);
1893 while (nbytes
> 0 && !retval
) {
1894 set_current_state(TASK_INTERRUPTIBLE
);
1896 if (!comedi_is_subdevice_running(s
)) {
1898 if (comedi_is_subdevice_in_error(s
))
1902 do_become_nonbusy(dev
, s
);
1910 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
1911 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
1912 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
1913 if (m
> comedi_buf_write_n_allocated(async
))
1914 m
= comedi_buf_write_n_allocated(async
);
1919 if (file
->f_flags
& O_NONBLOCK
) {
1924 if (signal_pending(current
)) {
1925 retval
= -ERESTARTSYS
;
1930 if (s
->busy
!= file
) {
1937 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
1943 comedi_buf_write_free(async
, n
);
1949 break; /* makes device work like a pipe */
1951 set_current_state(TASK_RUNNING
);
1952 remove_wait_queue(&async
->wait_head
, &wait
);
1954 return count
? count
: retval
;
1957 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
1960 struct comedi_subdevice
*s
;
1961 struct comedi_async
*async
;
1962 int n
, m
, count
= 0, retval
= 0;
1963 DECLARE_WAITQUEUE(wait
, current
);
1964 const unsigned minor
= iminor(file_inode(file
));
1965 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1966 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1971 if (!dev
->attached
) {
1972 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1976 s
= comedi_read_subdevice(info
);
1977 if (!s
|| !s
->async
)
1981 if (!s
->busy
|| !nbytes
)
1983 if (s
->busy
!= file
)
1986 add_wait_queue(&async
->wait_head
, &wait
);
1987 while (nbytes
> 0 && !retval
) {
1988 set_current_state(TASK_INTERRUPTIBLE
);
1992 m
= comedi_buf_read_n_available(async
);
1993 /* printk("%d available\n",m); */
1994 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
1995 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
1996 /* printk("%d contiguous\n",m); */
2001 if (!comedi_is_subdevice_running(s
)) {
2002 do_become_nonbusy(dev
, s
);
2003 if (comedi_is_subdevice_in_error(s
))
2009 if (file
->f_flags
& O_NONBLOCK
) {
2014 if (signal_pending(current
)) {
2015 retval
= -ERESTARTSYS
;
2022 if (s
->busy
!= file
) {
2028 m
= copy_to_user(buf
, async
->prealloc_buf
+
2029 async
->buf_read_ptr
, n
);
2035 comedi_buf_read_alloc(async
, n
);
2036 comedi_buf_read_free(async
, n
);
2042 break; /* makes device work like a pipe */
2044 if (comedi_is_subdevice_idle(s
) &&
2045 async
->buf_read_count
- async
->buf_write_count
== 0) {
2046 do_become_nonbusy(dev
, s
);
2048 set_current_state(TASK_RUNNING
);
2049 remove_wait_queue(&async
->wait_head
, &wait
);
2051 return count
? count
: retval
;
2054 static int comedi_open(struct inode
*inode
, struct file
*file
)
2056 const unsigned minor
= iminor(inode
);
2057 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2060 DPRINTK("invalid minor number\n");
2064 /* This is slightly hacky, but we want module autoloading
2066 * case: user opens device, attached -> ok
2067 * case: user opens device, unattached, !in_request_module -> autoload
2068 * case: user opens device, unattached, in_request_module -> fail
2069 * case: root opens device, attached -> ok
2070 * case: root opens device, unattached, in_request_module -> ok
2071 * (typically called from modprobe)
2072 * case: root opens device, unattached, !in_request_module -> autoload
2074 * The last could be changed to "-> ok", which would deny root
2077 mutex_lock(&dev
->mutex
);
2080 if (!capable(CAP_NET_ADMIN
) && dev
->in_request_module
) {
2081 DPRINTK("in request module\n");
2082 mutex_unlock(&dev
->mutex
);
2085 if (capable(CAP_NET_ADMIN
) && dev
->in_request_module
)
2088 dev
->in_request_module
= true;
2091 mutex_unlock(&dev
->mutex
);
2092 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
2093 mutex_lock(&dev
->mutex
);
2096 dev
->in_request_module
= false;
2098 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
2099 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2100 mutex_unlock(&dev
->mutex
);
2104 __module_get(THIS_MODULE
);
2106 if (dev
->attached
) {
2107 if (!try_module_get(dev
->driver
->module
)) {
2108 module_put(THIS_MODULE
);
2109 mutex_unlock(&dev
->mutex
);
2114 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
) {
2115 int rc
= dev
->open(dev
);
2117 module_put(dev
->driver
->module
);
2118 module_put(THIS_MODULE
);
2119 mutex_unlock(&dev
->mutex
);
2126 mutex_unlock(&dev
->mutex
);
2131 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2133 const unsigned minor
= iminor(file_inode(file
));
2134 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2139 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2142 static int comedi_close(struct inode
*inode
, struct file
*file
)
2144 const unsigned minor
= iminor(inode
);
2145 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2146 struct comedi_subdevice
*s
= NULL
;
2152 mutex_lock(&dev
->mutex
);
2154 if (dev
->subdevices
) {
2155 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2156 s
= &dev
->subdevices
[i
];
2158 if (s
->busy
== file
)
2160 if (s
->lock
== file
)
2164 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
2167 module_put(THIS_MODULE
);
2169 module_put(dev
->driver
->module
);
2173 mutex_unlock(&dev
->mutex
);
2175 if (file
->f_flags
& FASYNC
)
2176 comedi_fasync(-1, file
, 0);
2181 static const struct file_operations comedi_fops
= {
2182 .owner
= THIS_MODULE
,
2183 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2184 .compat_ioctl
= comedi_compat_ioctl
,
2185 .open
= comedi_open
,
2186 .release
= comedi_close
,
2187 .read
= comedi_read
,
2188 .write
= comedi_write
,
2189 .mmap
= comedi_mmap
,
2190 .poll
= comedi_poll
,
2191 .fasync
= comedi_fasync
,
2192 .llseek
= noop_llseek
,
2195 static struct class *comedi_class
;
2196 static struct cdev comedi_cdev
;
2198 void comedi_error(const struct comedi_device
*dev
, const char *s
)
2200 dev_err(dev
->class_dev
, "%s: %s\n", dev
->driver
->driver_name
, s
);
2202 EXPORT_SYMBOL(comedi_error
);
2204 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2206 struct comedi_async
*async
= s
->async
;
2207 unsigned runflags
= 0;
2208 unsigned runflags_mask
= 0;
2210 /* DPRINTK("comedi_event 0x%x\n",mask); */
2212 if (!comedi_is_subdevice_running(s
))
2216 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
2217 COMEDI_CB_OVERFLOW
)) {
2218 runflags_mask
|= SRF_RUNNING
;
2220 /* remember if an error event has occurred, so an error
2221 * can be returned the next time the user does a read() */
2222 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2223 runflags_mask
|= SRF_ERROR
;
2224 runflags
|= SRF_ERROR
;
2226 if (runflags_mask
) {
2227 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2228 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2231 if (async
->cb_mask
& s
->async
->events
) {
2232 if (comedi_get_subdevice_runflags(s
) & SRF_USER
) {
2233 wake_up_interruptible(&async
->wait_head
);
2234 if (s
->subdev_flags
& SDF_CMD_READ
)
2235 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2236 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2237 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2240 async
->cb_func(s
->async
->events
, async
->cb_arg
);
2243 s
->async
->events
= 0;
2245 EXPORT_SYMBOL(comedi_event
);
2247 static void comedi_device_init(struct comedi_device
*dev
)
2249 spin_lock_init(&dev
->spinlock
);
2250 mutex_init(&dev
->mutex
);
2254 static void comedi_device_cleanup(struct comedi_device
*dev
)
2256 struct module
*driver_module
= NULL
;
2260 mutex_lock(&dev
->mutex
);
2262 driver_module
= dev
->driver
->module
;
2263 comedi_device_detach(dev
);
2264 while (dev
->use_count
> 0) {
2266 module_put(driver_module
);
2267 module_put(THIS_MODULE
);
2270 mutex_unlock(&dev
->mutex
);
2271 mutex_destroy(&dev
->mutex
);
2274 /* Note: the ->mutex is pre-locked on successful return */
2275 struct comedi_device
*comedi_alloc_board_minor(struct device
*hardware_device
)
2277 struct comedi_file_info
*info
;
2278 struct comedi_device
*dev
;
2279 struct device
*csdev
;
2282 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
2284 return ERR_PTR(-ENOMEM
);
2285 dev
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2288 return ERR_PTR(-ENOMEM
);
2291 info
->hardware_device
= hardware_device
;
2292 comedi_device_init(dev
);
2293 mutex_lock(&dev
->mutex
);
2294 spin_lock(&comedi_file_info_table_lock
);
2295 for (i
= hardware_device
? comedi_num_legacy_minors
: 0;
2296 i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2297 if (comedi_file_info_table
[i
] == NULL
) {
2298 comedi_file_info_table
[i
] = info
;
2302 spin_unlock(&comedi_file_info_table_lock
);
2303 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2304 mutex_unlock(&dev
->mutex
);
2305 comedi_device_cleanup(dev
);
2308 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2309 return ERR_PTR(-EBUSY
);
2312 csdev
= device_create(comedi_class
, hardware_device
,
2313 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2315 dev
->class_dev
= csdev
;
2316 dev_set_drvdata(csdev
, info
);
2318 /* Note: dev->mutex needs to be unlocked by the caller. */
2322 static struct comedi_file_info
*comedi_clear_minor(unsigned minor
)
2324 struct comedi_file_info
*info
;
2326 spin_lock(&comedi_file_info_table_lock
);
2327 info
= comedi_file_info_table
[minor
];
2328 comedi_file_info_table
[minor
] = NULL
;
2329 spin_unlock(&comedi_file_info_table_lock
);
2333 static void comedi_free_board_file_info(struct comedi_file_info
*info
)
2336 struct comedi_device
*dev
= info
->device
;
2338 if (dev
->class_dev
) {
2339 device_destroy(comedi_class
,
2340 MKDEV(COMEDI_MAJOR
, dev
->minor
));
2342 comedi_device_cleanup(dev
);
2349 static void comedi_free_board_minor(unsigned minor
)
2351 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2352 comedi_free_board_file_info(comedi_clear_minor(minor
));
2355 void comedi_release_hardware_device(struct device
*hardware_device
)
2358 struct comedi_file_info
*info
;
2360 for (minor
= comedi_num_legacy_minors
; minor
< COMEDI_NUM_BOARD_MINORS
;
2362 spin_lock(&comedi_file_info_table_lock
);
2363 info
= comedi_file_info_table
[minor
];
2364 if (info
&& info
->hardware_device
== hardware_device
) {
2365 comedi_file_info_table
[minor
] = NULL
;
2366 spin_unlock(&comedi_file_info_table_lock
);
2367 comedi_free_board_file_info(info
);
2370 spin_unlock(&comedi_file_info_table_lock
);
2374 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2376 struct comedi_device
*dev
= s
->device
;
2377 struct comedi_file_info
*info
;
2378 struct device
*csdev
;
2381 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
2385 if (s
->subdev_flags
& SDF_CMD_READ
)
2386 info
->read_subdevice
= s
;
2387 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2388 info
->write_subdevice
= s
;
2389 spin_lock(&comedi_file_info_table_lock
);
2390 for (i
= COMEDI_FIRST_SUBDEVICE_MINOR
; i
< COMEDI_NUM_MINORS
; ++i
) {
2391 if (comedi_file_info_table
[i
] == NULL
) {
2392 comedi_file_info_table
[i
] = info
;
2396 spin_unlock(&comedi_file_info_table_lock
);
2397 if (i
== COMEDI_NUM_MINORS
) {
2399 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2403 csdev
= device_create(comedi_class
, dev
->class_dev
,
2404 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2405 dev
->minor
, s
->index
);
2407 s
->class_dev
= csdev
;
2408 dev_set_drvdata(csdev
, info
);
2413 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2415 struct comedi_file_info
*info
;
2422 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2423 BUG_ON(s
->minor
< COMEDI_FIRST_SUBDEVICE_MINOR
);
2425 info
= comedi_clear_minor(s
->minor
);
2427 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2428 s
->class_dev
= NULL
;
2433 static void comedi_cleanup_board_minors(void)
2437 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++)
2438 comedi_free_board_minor(i
);
2441 static int __init
comedi_init(void)
2446 pr_info("comedi: version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2448 if (comedi_num_legacy_minors
< 0 ||
2449 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2450 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2451 COMEDI_NUM_BOARD_MINORS
);
2455 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2456 COMEDI_NUM_MINORS
, "comedi");
2459 cdev_init(&comedi_cdev
, &comedi_fops
);
2460 comedi_cdev
.owner
= THIS_MODULE
;
2461 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2462 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2463 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2467 comedi_class
= class_create(THIS_MODULE
, "comedi");
2468 if (IS_ERR(comedi_class
)) {
2469 pr_err("comedi: failed to create class\n");
2470 cdev_del(&comedi_cdev
);
2471 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2473 return PTR_ERR(comedi_class
);
2476 comedi_class
->dev_attrs
= comedi_dev_attrs
;
2478 /* XXX requires /proc interface */
2481 /* create devices files for legacy/manual use */
2482 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2483 struct comedi_device
*dev
;
2484 dev
= comedi_alloc_board_minor(NULL
);
2486 comedi_cleanup_board_minors();
2487 cdev_del(&comedi_cdev
);
2488 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2490 return PTR_ERR(dev
);
2492 /* comedi_alloc_board_minor() locked the mutex */
2493 mutex_unlock(&dev
->mutex
);
2499 module_init(comedi_init
);
2501 static void __exit
comedi_cleanup(void)
2505 comedi_cleanup_board_minors();
2506 for (i
= 0; i
< COMEDI_NUM_MINORS
; ++i
)
2507 BUG_ON(comedi_file_info_table
[i
]);
2509 class_destroy(comedi_class
);
2510 cdev_del(&comedi_cdev
);
2511 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2513 comedi_proc_cleanup();
2515 module_exit(comedi_cleanup
);
2517 MODULE_AUTHOR("http://www.comedi.org");
2518 MODULE_DESCRIPTION("Comedi core module");
2519 MODULE_LICENSE("GPL");