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
->f_dentry
->d_inode
);
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
->f_dentry
->d_inode
);
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
);
1639 /* Evade comedi_auto_unconfig(). */
1640 info
->hardware_device
= NULL
;
1644 if (!dev
->attached
) {
1645 DPRINTK("no driver configured on /dev/comedi%i\n", dev
->minor
);
1651 case COMEDI_BUFCONFIG
:
1652 rc
= do_bufconfig_ioctl(dev
,
1653 (struct comedi_bufconfig __user
*)arg
);
1655 case COMEDI_DEVINFO
:
1656 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
1659 case COMEDI_SUBDINFO
:
1660 rc
= do_subdinfo_ioctl(dev
,
1661 (struct comedi_subdinfo __user
*)arg
,
1664 case COMEDI_CHANINFO
:
1665 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
1667 case COMEDI_RANGEINFO
:
1668 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
1670 case COMEDI_BUFINFO
:
1671 rc
= do_bufinfo_ioctl(dev
,
1672 (struct comedi_bufinfo __user
*)arg
,
1676 rc
= do_lock_ioctl(dev
, arg
, file
);
1679 rc
= do_unlock_ioctl(dev
, arg
, file
);
1682 rc
= do_cancel_ioctl(dev
, arg
, file
);
1685 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
1687 case COMEDI_CMDTEST
:
1688 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
1691 case COMEDI_INSNLIST
:
1692 rc
= do_insnlist_ioctl(dev
,
1693 (struct comedi_insnlist __user
*)arg
,
1697 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
1701 rc
= do_poll_ioctl(dev
, arg
, file
);
1709 mutex_unlock(&dev
->mutex
);
1713 static void comedi_vm_open(struct vm_area_struct
*area
)
1715 struct comedi_async
*async
;
1716 struct comedi_device
*dev
;
1718 async
= area
->vm_private_data
;
1719 dev
= async
->subdevice
->device
;
1721 mutex_lock(&dev
->mutex
);
1722 async
->mmap_count
++;
1723 mutex_unlock(&dev
->mutex
);
1726 static void comedi_vm_close(struct vm_area_struct
*area
)
1728 struct comedi_async
*async
;
1729 struct comedi_device
*dev
;
1731 async
= area
->vm_private_data
;
1732 dev
= async
->subdevice
->device
;
1734 mutex_lock(&dev
->mutex
);
1735 async
->mmap_count
--;
1736 mutex_unlock(&dev
->mutex
);
1739 static struct vm_operations_struct comedi_vm_ops
= {
1740 .open
= comedi_vm_open
,
1741 .close
= comedi_vm_close
,
1744 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1746 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1747 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1748 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1749 struct comedi_subdevice
*s
;
1750 struct comedi_async
*async
;
1751 unsigned long start
= vma
->vm_start
;
1760 mutex_lock(&dev
->mutex
);
1762 if (!dev
->attached
) {
1763 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1768 if (vma
->vm_flags
& VM_WRITE
)
1769 s
= comedi_write_subdevice(info
);
1771 s
= comedi_read_subdevice(info
);
1783 if (vma
->vm_pgoff
!= 0) {
1784 DPRINTK("comedi: mmap() offset must be 0.\n");
1789 size
= vma
->vm_end
- vma
->vm_start
;
1790 if (size
> async
->prealloc_bufsz
) {
1794 if (size
& (~PAGE_MASK
)) {
1799 n_pages
= size
>> PAGE_SHIFT
;
1800 for (i
= 0; i
< n_pages
; ++i
) {
1801 struct comedi_buf_page
*buf
= &async
->buf_page_list
[i
];
1803 if (remap_pfn_range(vma
, start
,
1804 page_to_pfn(virt_to_page(buf
->virt_addr
)),
1805 PAGE_SIZE
, PAGE_SHARED
)) {
1812 vma
->vm_ops
= &comedi_vm_ops
;
1813 vma
->vm_private_data
= async
;
1815 async
->mmap_count
++;
1819 mutex_unlock(&dev
->mutex
);
1823 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
1825 unsigned int mask
= 0;
1826 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1827 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1828 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1829 struct comedi_subdevice
*s
;
1834 mutex_lock(&dev
->mutex
);
1836 if (!dev
->attached
) {
1837 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1841 s
= comedi_read_subdevice(info
);
1842 if (s
&& s
->async
) {
1843 poll_wait(file
, &s
->async
->wait_head
, wait
);
1844 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
1845 comedi_buf_read_n_available(s
->async
) > 0)
1846 mask
|= POLLIN
| POLLRDNORM
;
1849 s
= comedi_write_subdevice(info
);
1850 if (s
&& s
->async
) {
1851 unsigned int bps
= bytes_per_sample(s
->async
->subdevice
);
1853 poll_wait(file
, &s
->async
->wait_head
, wait
);
1854 comedi_buf_write_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1855 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
1856 comedi_buf_write_n_allocated(s
->async
) >= bps
)
1857 mask
|= POLLOUT
| POLLWRNORM
;
1861 mutex_unlock(&dev
->mutex
);
1865 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
1866 size_t nbytes
, loff_t
*offset
)
1868 struct comedi_subdevice
*s
;
1869 struct comedi_async
*async
;
1870 int n
, m
, count
= 0, retval
= 0;
1871 DECLARE_WAITQUEUE(wait
, current
);
1872 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1873 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1874 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1879 if (!dev
->attached
) {
1880 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1884 s
= comedi_write_subdevice(info
);
1885 if (!s
|| !s
->async
)
1890 if (!s
->busy
|| !nbytes
)
1892 if (s
->busy
!= file
)
1895 add_wait_queue(&async
->wait_head
, &wait
);
1896 while (nbytes
> 0 && !retval
) {
1897 set_current_state(TASK_INTERRUPTIBLE
);
1899 if (!comedi_is_subdevice_running(s
)) {
1901 if (comedi_is_subdevice_in_error(s
))
1905 do_become_nonbusy(dev
, s
);
1913 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
1914 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
1915 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
1916 if (m
> comedi_buf_write_n_allocated(async
))
1917 m
= comedi_buf_write_n_allocated(async
);
1922 if (file
->f_flags
& O_NONBLOCK
) {
1927 if (signal_pending(current
)) {
1928 retval
= -ERESTARTSYS
;
1933 if (s
->busy
!= file
) {
1940 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
1946 comedi_buf_write_free(async
, n
);
1952 break; /* makes device work like a pipe */
1954 set_current_state(TASK_RUNNING
);
1955 remove_wait_queue(&async
->wait_head
, &wait
);
1957 return count
? count
: retval
;
1960 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
1963 struct comedi_subdevice
*s
;
1964 struct comedi_async
*async
;
1965 int n
, m
, count
= 0, retval
= 0;
1966 DECLARE_WAITQUEUE(wait
, current
);
1967 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
1968 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1969 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1974 if (!dev
->attached
) {
1975 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1979 s
= comedi_read_subdevice(info
);
1980 if (!s
|| !s
->async
)
1984 if (!s
->busy
|| !nbytes
)
1986 if (s
->busy
!= file
)
1989 add_wait_queue(&async
->wait_head
, &wait
);
1990 while (nbytes
> 0 && !retval
) {
1991 set_current_state(TASK_INTERRUPTIBLE
);
1995 m
= comedi_buf_read_n_available(async
);
1996 /* printk("%d available\n",m); */
1997 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
1998 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
1999 /* printk("%d contiguous\n",m); */
2004 if (!comedi_is_subdevice_running(s
)) {
2005 do_become_nonbusy(dev
, s
);
2006 if (comedi_is_subdevice_in_error(s
))
2012 if (file
->f_flags
& O_NONBLOCK
) {
2017 if (signal_pending(current
)) {
2018 retval
= -ERESTARTSYS
;
2025 if (s
->busy
!= file
) {
2031 m
= copy_to_user(buf
, async
->prealloc_buf
+
2032 async
->buf_read_ptr
, n
);
2038 comedi_buf_read_alloc(async
, n
);
2039 comedi_buf_read_free(async
, n
);
2045 break; /* makes device work like a pipe */
2047 if (comedi_is_subdevice_idle(s
) &&
2048 async
->buf_read_count
- async
->buf_write_count
== 0) {
2049 do_become_nonbusy(dev
, s
);
2051 set_current_state(TASK_RUNNING
);
2052 remove_wait_queue(&async
->wait_head
, &wait
);
2054 return count
? count
: retval
;
2057 static int comedi_open(struct inode
*inode
, struct file
*file
)
2059 const unsigned minor
= iminor(inode
);
2060 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2063 DPRINTK("invalid minor number\n");
2067 /* This is slightly hacky, but we want module autoloading
2069 * case: user opens device, attached -> ok
2070 * case: user opens device, unattached, in_request_module=0 -> autoload
2071 * case: user opens device, unattached, in_request_module=1 -> fail
2072 * case: root opens device, attached -> ok
2073 * case: root opens device, unattached, in_request_module=1 -> ok
2074 * (typically called from modprobe)
2075 * case: root opens device, unattached, in_request_module=0 -> autoload
2077 * The last could be changed to "-> ok", which would deny root
2080 mutex_lock(&dev
->mutex
);
2083 if (!capable(CAP_NET_ADMIN
) && dev
->in_request_module
) {
2084 DPRINTK("in request module\n");
2085 mutex_unlock(&dev
->mutex
);
2088 if (capable(CAP_NET_ADMIN
) && dev
->in_request_module
)
2091 dev
->in_request_module
= 1;
2094 mutex_unlock(&dev
->mutex
);
2095 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
2096 mutex_lock(&dev
->mutex
);
2099 dev
->in_request_module
= 0;
2101 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
2102 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2103 mutex_unlock(&dev
->mutex
);
2107 __module_get(THIS_MODULE
);
2109 if (dev
->attached
) {
2110 if (!try_module_get(dev
->driver
->module
)) {
2111 module_put(THIS_MODULE
);
2112 mutex_unlock(&dev
->mutex
);
2117 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
) {
2118 int rc
= dev
->open(dev
);
2120 module_put(dev
->driver
->module
);
2121 module_put(THIS_MODULE
);
2122 mutex_unlock(&dev
->mutex
);
2129 mutex_unlock(&dev
->mutex
);
2134 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2136 const unsigned minor
= iminor(file
->f_dentry
->d_inode
);
2137 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2142 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2145 static int comedi_close(struct inode
*inode
, struct file
*file
)
2147 const unsigned minor
= iminor(inode
);
2148 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2149 struct comedi_subdevice
*s
= NULL
;
2155 mutex_lock(&dev
->mutex
);
2157 if (dev
->subdevices
) {
2158 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2159 s
= &dev
->subdevices
[i
];
2161 if (s
->busy
== file
)
2163 if (s
->lock
== file
)
2167 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
2170 module_put(THIS_MODULE
);
2172 module_put(dev
->driver
->module
);
2176 mutex_unlock(&dev
->mutex
);
2178 if (file
->f_flags
& FASYNC
)
2179 comedi_fasync(-1, file
, 0);
2184 static const struct file_operations comedi_fops
= {
2185 .owner
= THIS_MODULE
,
2186 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2187 .compat_ioctl
= comedi_compat_ioctl
,
2188 .open
= comedi_open
,
2189 .release
= comedi_close
,
2190 .read
= comedi_read
,
2191 .write
= comedi_write
,
2192 .mmap
= comedi_mmap
,
2193 .poll
= comedi_poll
,
2194 .fasync
= comedi_fasync
,
2195 .llseek
= noop_llseek
,
2198 static struct class *comedi_class
;
2199 static struct cdev comedi_cdev
;
2201 void comedi_error(const struct comedi_device
*dev
, const char *s
)
2203 dev_err(dev
->class_dev
, "%s: %s\n", dev
->driver
->driver_name
, s
);
2205 EXPORT_SYMBOL(comedi_error
);
2207 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2209 struct comedi_async
*async
= s
->async
;
2210 unsigned runflags
= 0;
2211 unsigned runflags_mask
= 0;
2213 /* DPRINTK("comedi_event 0x%x\n",mask); */
2215 if (!comedi_is_subdevice_running(s
))
2219 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
2220 COMEDI_CB_OVERFLOW
)) {
2221 runflags_mask
|= SRF_RUNNING
;
2223 /* remember if an error event has occurred, so an error
2224 * can be returned the next time the user does a read() */
2225 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2226 runflags_mask
|= SRF_ERROR
;
2227 runflags
|= SRF_ERROR
;
2229 if (runflags_mask
) {
2230 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2231 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2234 if (async
->cb_mask
& s
->async
->events
) {
2235 if (comedi_get_subdevice_runflags(s
) & SRF_USER
) {
2236 wake_up_interruptible(&async
->wait_head
);
2237 if (s
->subdev_flags
& SDF_CMD_READ
)
2238 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2239 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2240 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2243 async
->cb_func(s
->async
->events
, async
->cb_arg
);
2246 s
->async
->events
= 0;
2248 EXPORT_SYMBOL(comedi_event
);
2250 static void comedi_device_init(struct comedi_device
*dev
)
2252 memset(dev
, 0, sizeof(*dev
));
2253 spin_lock_init(&dev
->spinlock
);
2254 mutex_init(&dev
->mutex
);
2258 static void comedi_device_cleanup(struct comedi_device
*dev
)
2262 mutex_lock(&dev
->mutex
);
2263 comedi_device_detach(dev
);
2264 mutex_unlock(&dev
->mutex
);
2265 mutex_destroy(&dev
->mutex
);
2268 int comedi_alloc_board_minor(struct device
*hardware_device
)
2270 struct comedi_file_info
*info
;
2271 struct device
*csdev
;
2274 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
2277 info
->device
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2278 if (info
->device
== NULL
) {
2282 info
->hardware_device
= hardware_device
;
2283 comedi_device_init(info
->device
);
2284 spin_lock(&comedi_file_info_table_lock
);
2285 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2286 if (comedi_file_info_table
[i
] == NULL
) {
2287 comedi_file_info_table
[i
] = info
;
2291 spin_unlock(&comedi_file_info_table_lock
);
2292 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2293 comedi_device_cleanup(info
->device
);
2294 kfree(info
->device
);
2296 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2299 info
->device
->minor
= i
;
2300 csdev
= device_create(comedi_class
, hardware_device
,
2301 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2303 info
->device
->class_dev
= csdev
;
2304 dev_set_drvdata(csdev
, info
);
2309 void comedi_free_board_minor(unsigned minor
)
2311 struct comedi_file_info
*info
;
2313 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2314 spin_lock(&comedi_file_info_table_lock
);
2315 info
= comedi_file_info_table
[minor
];
2316 comedi_file_info_table
[minor
] = NULL
;
2317 spin_unlock(&comedi_file_info_table_lock
);
2320 struct comedi_device
*dev
= info
->device
;
2322 if (dev
->class_dev
) {
2323 device_destroy(comedi_class
,
2324 MKDEV(COMEDI_MAJOR
, dev
->minor
));
2326 comedi_device_cleanup(dev
);
2333 int comedi_find_board_minor(struct device
*hardware_device
)
2336 struct comedi_file_info
*info
;
2338 for (minor
= 0; minor
< COMEDI_NUM_BOARD_MINORS
; minor
++) {
2339 spin_lock(&comedi_file_info_table_lock
);
2340 info
= comedi_file_info_table
[minor
];
2341 if (info
&& info
->hardware_device
== hardware_device
) {
2342 spin_unlock(&comedi_file_info_table_lock
);
2345 spin_unlock(&comedi_file_info_table_lock
);
2350 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2352 struct comedi_device
*dev
= s
->device
;
2353 struct comedi_file_info
*info
;
2354 struct device
*csdev
;
2357 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
2361 if (s
->subdev_flags
& SDF_CMD_READ
)
2362 info
->read_subdevice
= s
;
2363 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2364 info
->write_subdevice
= s
;
2365 spin_lock(&comedi_file_info_table_lock
);
2366 for (i
= COMEDI_FIRST_SUBDEVICE_MINOR
; i
< COMEDI_NUM_MINORS
; ++i
) {
2367 if (comedi_file_info_table
[i
] == NULL
) {
2368 comedi_file_info_table
[i
] = info
;
2372 spin_unlock(&comedi_file_info_table_lock
);
2373 if (i
== COMEDI_NUM_MINORS
) {
2375 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2379 csdev
= device_create(comedi_class
, dev
->class_dev
,
2380 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2381 dev
->minor
, s
->index
);
2383 s
->class_dev
= csdev
;
2384 dev_set_drvdata(csdev
, info
);
2389 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2391 struct comedi_file_info
*info
;
2398 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2399 BUG_ON(s
->minor
< COMEDI_FIRST_SUBDEVICE_MINOR
);
2401 spin_lock(&comedi_file_info_table_lock
);
2402 info
= comedi_file_info_table
[s
->minor
];
2403 comedi_file_info_table
[s
->minor
] = NULL
;
2404 spin_unlock(&comedi_file_info_table_lock
);
2407 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2408 s
->class_dev
= NULL
;
2413 static void comedi_cleanup_board_minors(void)
2417 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++)
2418 comedi_free_board_minor(i
);
2421 static int __init
comedi_init(void)
2426 pr_info("comedi: version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2428 if (comedi_num_legacy_minors
< 0 ||
2429 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2430 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2431 COMEDI_NUM_BOARD_MINORS
);
2435 memset(comedi_file_info_table
, 0,
2436 sizeof(struct comedi_file_info
*) * COMEDI_NUM_MINORS
);
2438 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2439 COMEDI_NUM_MINORS
, "comedi");
2442 cdev_init(&comedi_cdev
, &comedi_fops
);
2443 comedi_cdev
.owner
= THIS_MODULE
;
2444 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2445 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2446 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2450 comedi_class
= class_create(THIS_MODULE
, "comedi");
2451 if (IS_ERR(comedi_class
)) {
2452 pr_err("comedi: failed to create class\n");
2453 cdev_del(&comedi_cdev
);
2454 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2456 return PTR_ERR(comedi_class
);
2459 comedi_class
->dev_attrs
= comedi_dev_attrs
;
2461 /* XXX requires /proc interface */
2464 /* create devices files for legacy/manual use */
2465 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2467 minor
= comedi_alloc_board_minor(NULL
);
2469 comedi_cleanup_board_minors();
2470 cdev_del(&comedi_cdev
);
2471 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2479 module_init(comedi_init
);
2481 static void __exit
comedi_cleanup(void)
2485 comedi_cleanup_board_minors();
2486 for (i
= 0; i
< COMEDI_NUM_MINORS
; ++i
)
2487 BUG_ON(comedi_file_info_table
[i
]);
2489 class_destroy(comedi_class
);
2490 cdev_del(&comedi_cdev
);
2491 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2493 comedi_proc_cleanup();
2495 module_exit(comedi_cleanup
);
2497 MODULE_AUTHOR("http://www.comedi.org");
2498 MODULE_DESCRIPTION("Comedi core module");
2499 MODULE_LICENSE("GPL");