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 #define COMEDI_NUM_MINORS 0x100
53 #define COMEDI_NUM_SUBDEVICE_MINORS \
54 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
56 #ifdef CONFIG_COMEDI_DEBUG
58 EXPORT_SYMBOL(comedi_debug
);
59 module_param(comedi_debug
, int, S_IRUGO
| S_IWUSR
);
60 MODULE_PARM_DESC(comedi_debug
,
61 "enable comedi core and driver debugging if non-zero (default 0)"
65 static int comedi_num_legacy_minors
;
66 module_param(comedi_num_legacy_minors
, int, S_IRUGO
);
67 MODULE_PARM_DESC(comedi_num_legacy_minors
,
68 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
71 unsigned int comedi_default_buf_size_kb
= CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
;
72 module_param(comedi_default_buf_size_kb
, uint
, S_IRUGO
| S_IWUSR
);
73 MODULE_PARM_DESC(comedi_default_buf_size_kb
,
74 "default asynchronous buffer size in KiB (default "
75 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
) ")");
77 unsigned int comedi_default_buf_maxsize_kb
78 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
;
79 module_param(comedi_default_buf_maxsize_kb
, uint
, S_IRUGO
| S_IWUSR
);
80 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb
,
81 "default maximum size of asynchronous buffer in KiB (default "
82 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
) ")");
84 static DEFINE_MUTEX(comedi_board_minor_table_lock
);
85 static struct comedi_device
86 *comedi_board_minor_table
[COMEDI_NUM_BOARD_MINORS
];
88 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock
);
89 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
90 static struct comedi_subdevice
91 *comedi_subdevice_minor_table
[COMEDI_NUM_SUBDEVICE_MINORS
];
93 static struct class *comedi_class
;
94 static struct cdev comedi_cdev
;
96 static void comedi_device_init(struct comedi_device
*dev
)
98 spin_lock_init(&dev
->spinlock
);
99 mutex_init(&dev
->mutex
);
103 static void comedi_device_cleanup(struct comedi_device
*dev
)
105 struct module
*driver_module
= NULL
;
109 mutex_lock(&dev
->mutex
);
111 driver_module
= dev
->driver
->module
;
112 comedi_device_detach(dev
);
113 while (dev
->use_count
> 0) {
115 module_put(driver_module
);
116 module_put(THIS_MODULE
);
119 mutex_unlock(&dev
->mutex
);
120 mutex_destroy(&dev
->mutex
);
123 static struct comedi_device
*comedi_clear_board_minor(unsigned minor
)
125 struct comedi_device
*dev
;
127 mutex_lock(&comedi_board_minor_table_lock
);
128 dev
= comedi_board_minor_table
[minor
];
129 comedi_board_minor_table
[minor
] = NULL
;
130 mutex_unlock(&comedi_board_minor_table_lock
);
134 static void comedi_free_board_dev(struct comedi_device
*dev
)
137 if (dev
->class_dev
) {
138 device_destroy(comedi_class
,
139 MKDEV(COMEDI_MAJOR
, dev
->minor
));
141 comedi_device_cleanup(dev
);
146 static struct comedi_subdevice
147 *comedi_subdevice_from_minor(unsigned minor
)
149 struct comedi_subdevice
*s
;
150 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
152 BUG_ON(i
>= COMEDI_NUM_SUBDEVICE_MINORS
);
153 mutex_lock(&comedi_subdevice_minor_table_lock
);
154 s
= comedi_subdevice_minor_table
[i
];
155 mutex_unlock(&comedi_subdevice_minor_table_lock
);
159 static struct comedi_device
*comedi_dev_from_board_minor(unsigned minor
)
161 struct comedi_device
*dev
;
163 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
164 mutex_lock(&comedi_board_minor_table_lock
);
165 dev
= comedi_board_minor_table
[minor
];
166 mutex_unlock(&comedi_board_minor_table_lock
);
170 static struct comedi_device
*comedi_dev_from_subdevice_minor(unsigned minor
)
172 struct comedi_subdevice
*s
;
174 s
= comedi_subdevice_from_minor(minor
);
175 return s
? s
->device
: NULL
;
178 struct comedi_device
*comedi_dev_from_minor(unsigned minor
)
180 if (minor
< COMEDI_NUM_BOARD_MINORS
)
181 return comedi_dev_from_board_minor(minor
);
183 return comedi_dev_from_subdevice_minor(minor
);
185 EXPORT_SYMBOL_GPL(comedi_dev_from_minor
);
187 static struct comedi_subdevice
*
188 comedi_read_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
190 struct comedi_subdevice
*s
;
192 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
193 s
= comedi_subdevice_from_minor(minor
);
194 if (!s
|| s
->device
!= dev
)
196 if (s
->subdev_flags
& SDF_CMD_READ
)
199 return dev
->read_subdev
;
202 static struct comedi_subdevice
*
203 comedi_write_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
205 struct comedi_subdevice
*s
;
207 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
208 s
= comedi_subdevice_from_minor(minor
);
209 if (!s
|| s
->device
!= dev
)
211 if (s
->subdev_flags
& SDF_CMD_WRITE
)
214 return dev
->write_subdev
;
217 static int resize_async_buffer(struct comedi_device
*dev
,
218 struct comedi_subdevice
*s
,
219 struct comedi_async
*async
, unsigned new_size
)
223 if (new_size
> async
->max_bufsize
)
227 DPRINTK("subdevice is busy, cannot resize buffer\n");
230 if (async
->mmap_count
) {
231 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
235 if (!async
->prealloc_buf
)
238 /* make sure buffer is an integral number of pages
240 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
242 retval
= comedi_buf_alloc(dev
, s
, new_size
);
247 retval
= s
->buf_change(dev
, s
, new_size
);
252 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
253 dev
->minor
, s
->index
, async
->prealloc_bufsz
);
257 /* sysfs attribute files */
259 static ssize_t
show_max_read_buffer_kb(struct device
*csdev
,
260 struct device_attribute
*attr
, char *buf
)
262 unsigned int minor
= MINOR(csdev
->devt
);
263 struct comedi_device
*dev
;
264 struct comedi_subdevice
*s
;
265 unsigned int size
= 0;
267 dev
= comedi_dev_from_minor(minor
);
271 mutex_lock(&dev
->mutex
);
272 s
= comedi_read_subdevice(dev
, minor
);
273 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
274 size
= s
->async
->max_bufsize
/ 1024;
275 mutex_unlock(&dev
->mutex
);
277 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
280 static ssize_t
store_max_read_buffer_kb(struct device
*csdev
,
281 struct device_attribute
*attr
,
282 const char *buf
, size_t count
)
284 unsigned int minor
= MINOR(csdev
->devt
);
285 struct comedi_device
*dev
;
286 struct comedi_subdevice
*s
;
290 err
= kstrtouint(buf
, 10, &size
);
293 if (size
> (UINT_MAX
/ 1024))
297 dev
= comedi_dev_from_minor(minor
);
301 mutex_lock(&dev
->mutex
);
302 s
= comedi_read_subdevice(dev
, minor
);
303 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
304 s
->async
->max_bufsize
= size
;
307 mutex_unlock(&dev
->mutex
);
309 return err
? err
: count
;
312 static ssize_t
show_read_buffer_kb(struct device
*csdev
,
313 struct device_attribute
*attr
, char *buf
)
315 unsigned int minor
= MINOR(csdev
->devt
);
316 struct comedi_device
*dev
;
317 struct comedi_subdevice
*s
;
318 unsigned int size
= 0;
320 dev
= comedi_dev_from_minor(minor
);
324 mutex_lock(&dev
->mutex
);
325 s
= comedi_read_subdevice(dev
, minor
);
326 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
327 size
= s
->async
->prealloc_bufsz
/ 1024;
328 mutex_unlock(&dev
->mutex
);
330 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
333 static ssize_t
store_read_buffer_kb(struct device
*csdev
,
334 struct device_attribute
*attr
,
335 const char *buf
, size_t count
)
337 unsigned int minor
= MINOR(csdev
->devt
);
338 struct comedi_device
*dev
;
339 struct comedi_subdevice
*s
;
343 err
= kstrtouint(buf
, 10, &size
);
346 if (size
> (UINT_MAX
/ 1024))
350 dev
= comedi_dev_from_minor(minor
);
354 mutex_lock(&dev
->mutex
);
355 s
= comedi_read_subdevice(dev
, minor
);
356 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
357 err
= resize_async_buffer(dev
, s
, s
->async
, size
);
360 mutex_unlock(&dev
->mutex
);
362 return err
? err
: count
;
365 static ssize_t
show_max_write_buffer_kb(struct device
*csdev
,
366 struct device_attribute
*attr
,
369 unsigned int minor
= MINOR(csdev
->devt
);
370 struct comedi_device
*dev
;
371 struct comedi_subdevice
*s
;
372 unsigned int size
= 0;
374 dev
= comedi_dev_from_minor(minor
);
378 mutex_lock(&dev
->mutex
);
379 s
= comedi_write_subdevice(dev
, minor
);
380 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
381 size
= s
->async
->max_bufsize
/ 1024;
382 mutex_unlock(&dev
->mutex
);
384 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
387 static ssize_t
store_max_write_buffer_kb(struct device
*csdev
,
388 struct device_attribute
*attr
,
389 const char *buf
, size_t count
)
391 unsigned int minor
= MINOR(csdev
->devt
);
392 struct comedi_device
*dev
;
393 struct comedi_subdevice
*s
;
397 err
= kstrtouint(buf
, 10, &size
);
400 if (size
> (UINT_MAX
/ 1024))
404 dev
= comedi_dev_from_minor(minor
);
408 mutex_lock(&dev
->mutex
);
409 s
= comedi_write_subdevice(dev
, minor
);
410 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
411 s
->async
->max_bufsize
= size
;
414 mutex_unlock(&dev
->mutex
);
416 return err
? err
: count
;
419 static ssize_t
show_write_buffer_kb(struct device
*csdev
,
420 struct device_attribute
*attr
, char *buf
)
422 unsigned int minor
= MINOR(csdev
->devt
);
423 struct comedi_device
*dev
;
424 struct comedi_subdevice
*s
;
425 unsigned int size
= 0;
427 dev
= comedi_dev_from_minor(minor
);
431 mutex_lock(&dev
->mutex
);
432 s
= comedi_write_subdevice(dev
, minor
);
433 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
434 size
= s
->async
->prealloc_bufsz
/ 1024;
435 mutex_unlock(&dev
->mutex
);
437 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
440 static ssize_t
store_write_buffer_kb(struct device
*csdev
,
441 struct device_attribute
*attr
,
442 const char *buf
, size_t count
)
444 unsigned int minor
= MINOR(csdev
->devt
);
445 struct comedi_device
*dev
;
446 struct comedi_subdevice
*s
;
450 err
= kstrtouint(buf
, 10, &size
);
453 if (size
> (UINT_MAX
/ 1024))
457 dev
= comedi_dev_from_minor(minor
);
461 mutex_lock(&dev
->mutex
);
462 s
= comedi_write_subdevice(dev
, minor
);
463 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
464 err
= resize_async_buffer(dev
, s
, s
->async
, size
);
467 mutex_unlock(&dev
->mutex
);
469 return err
? err
: count
;
472 static struct device_attribute comedi_dev_attrs
[] = {
473 __ATTR(max_read_buffer_kb
, S_IRUGO
| S_IWUSR
,
474 show_max_read_buffer_kb
, store_max_read_buffer_kb
),
475 __ATTR(read_buffer_kb
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
476 show_read_buffer_kb
, store_read_buffer_kb
),
477 __ATTR(max_write_buffer_kb
, S_IRUGO
| S_IWUSR
,
478 show_max_write_buffer_kb
, store_max_write_buffer_kb
),
479 __ATTR(write_buffer_kb
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
480 show_write_buffer_kb
, store_write_buffer_kb
),
484 static void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
485 unsigned mask
, unsigned bits
)
489 spin_lock_irqsave(&s
->spin_lock
, flags
);
490 s
->runflags
&= ~mask
;
491 s
->runflags
|= (bits
& mask
);
492 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
495 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
500 spin_lock_irqsave(&s
->spin_lock
, flags
);
501 runflags
= s
->runflags
;
502 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
506 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
)
508 unsigned runflags
= comedi_get_subdevice_runflags(s
);
510 return (runflags
& SRF_RUNNING
) ? true : false;
512 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running
);
514 static bool comedi_is_subdevice_in_error(struct comedi_subdevice
*s
)
516 unsigned runflags
= comedi_get_subdevice_runflags(s
);
518 return (runflags
& SRF_ERROR
) ? true : false;
521 static bool comedi_is_subdevice_idle(struct comedi_subdevice
*s
)
523 unsigned runflags
= comedi_get_subdevice_runflags(s
);
525 return (runflags
& (SRF_ERROR
| SRF_RUNNING
)) ? false : true;
529 This function restores a subdevice to an idle state.
531 static void do_become_nonbusy(struct comedi_device
*dev
,
532 struct comedi_subdevice
*s
)
534 struct comedi_async
*async
= s
->async
;
536 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
538 comedi_buf_reset(async
);
539 async
->inttrig
= NULL
;
540 kfree(async
->cmd
.chanlist
);
541 async
->cmd
.chanlist
= NULL
;
543 dev_err(dev
->class_dev
,
544 "BUG: (?) do_become_nonbusy called with async=NULL\n");
550 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
554 if (comedi_is_subdevice_running(s
) && s
->cancel
)
555 ret
= s
->cancel(dev
, s
);
557 do_become_nonbusy(dev
, s
);
562 static int is_device_busy(struct comedi_device
*dev
)
564 struct comedi_subdevice
*s
;
570 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
571 s
= &dev
->subdevices
[i
];
574 if (s
->async
&& s
->async
->mmap_count
)
586 pointer to devconfig structure
589 devconfig structure at arg
594 static int do_devconfig_ioctl(struct comedi_device
*dev
,
595 struct comedi_devconfig __user
*arg
)
597 struct comedi_devconfig it
;
599 if (!capable(CAP_SYS_ADMIN
))
603 if (is_device_busy(dev
))
606 struct module
*driver_module
= dev
->driver
->module
;
607 comedi_device_detach(dev
);
608 module_put(driver_module
);
613 if (copy_from_user(&it
, arg
, sizeof(it
)))
616 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
618 if (it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
619 dev_warn(dev
->class_dev
,
620 "comedi_config --init_data is deprecated\n");
624 if (dev
->minor
>= comedi_num_legacy_minors
)
625 /* don't re-use dynamically allocated comedi devices */
628 /* This increments the driver module count on success. */
629 return comedi_device_attach(dev
, &it
);
634 buffer configuration ioctl
637 pointer to bufconfig structure
643 modified bufconfig at arg
646 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
647 struct comedi_bufconfig __user
*arg
)
649 struct comedi_bufconfig bc
;
650 struct comedi_async
*async
;
651 struct comedi_subdevice
*s
;
654 if (copy_from_user(&bc
, arg
, sizeof(bc
)))
657 if (bc
.subdevice
>= dev
->n_subdevices
|| bc
.subdevice
< 0)
660 s
= &dev
->subdevices
[bc
.subdevice
];
664 DPRINTK("subdevice does not have async capability\n");
670 if (bc
.maximum_size
) {
671 if (!capable(CAP_SYS_ADMIN
))
674 async
->max_bufsize
= bc
.maximum_size
;
678 retval
= resize_async_buffer(dev
, s
, async
, bc
.size
);
683 bc
.size
= async
->prealloc_bufsz
;
684 bc
.maximum_size
= async
->max_bufsize
;
687 if (copy_to_user(arg
, &bc
, sizeof(bc
)))
698 pointer to devinfo structure
707 static int do_devinfo_ioctl(struct comedi_device
*dev
,
708 struct comedi_devinfo __user
*arg
,
711 const unsigned minor
= iminor(file_inode(file
));
712 struct comedi_subdevice
*s
;
713 struct comedi_devinfo devinfo
;
715 memset(&devinfo
, 0, sizeof(devinfo
));
717 /* fill devinfo structure */
718 devinfo
.version_code
= COMEDI_VERSION_CODE
;
719 devinfo
.n_subdevs
= dev
->n_subdevices
;
720 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
721 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
723 s
= comedi_read_subdevice(dev
, minor
);
725 devinfo
.read_subdevice
= s
->index
;
727 devinfo
.read_subdevice
= -1;
729 s
= comedi_write_subdevice(dev
, minor
);
731 devinfo
.write_subdevice
= s
->index
;
733 devinfo
.write_subdevice
= -1;
735 if (copy_to_user(arg
, &devinfo
, sizeof(devinfo
)))
746 pointer to array of subdevice info structures
752 array of subdevice info structures at arg
755 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
756 struct comedi_subdinfo __user
*arg
, void *file
)
759 struct comedi_subdinfo
*tmp
, *us
;
760 struct comedi_subdevice
*s
;
762 tmp
= kcalloc(dev
->n_subdevices
, sizeof(*tmp
), GFP_KERNEL
);
766 /* fill subdinfo structs */
767 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
768 s
= &dev
->subdevices
[i
];
772 us
->n_chan
= s
->n_chan
;
773 us
->subd_flags
= s
->subdev_flags
;
774 if (comedi_is_subdevice_running(s
))
775 us
->subd_flags
|= SDF_RUNNING
;
776 #define TIMER_nanosec 5 /* backwards compatibility */
777 us
->timer_type
= TIMER_nanosec
;
778 us
->len_chanlist
= s
->len_chanlist
;
779 us
->maxdata
= s
->maxdata
;
780 if (s
->range_table
) {
782 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
784 us
->range_type
= 0; /* XXX */
786 us
->flags
= s
->flags
;
789 us
->subd_flags
|= SDF_BUSY
;
791 us
->subd_flags
|= SDF_BUSY_OWNER
;
793 us
->subd_flags
|= SDF_LOCKED
;
795 us
->subd_flags
|= SDF_LOCK_OWNER
;
796 if (!s
->maxdata
&& s
->maxdata_list
)
797 us
->subd_flags
|= SDF_MAXDATA
;
799 us
->subd_flags
|= SDF_FLAGS
;
800 if (s
->range_table_list
)
801 us
->subd_flags
|= SDF_RANGETYPE
;
803 us
->subd_flags
|= SDF_CMD
;
805 if (s
->insn_bits
!= &insn_inval
)
806 us
->insn_bits_support
= COMEDI_SUPPORTED
;
808 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
810 us
->settling_time_0
= s
->settling_time_0
;
813 ret
= copy_to_user(arg
, tmp
, dev
->n_subdevices
* sizeof(*tmp
));
817 return ret
? -EFAULT
: 0;
825 pointer to chaninfo structure
828 chaninfo structure at arg
831 arrays at elements of chaninfo structure
834 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
835 struct comedi_chaninfo __user
*arg
)
837 struct comedi_subdevice
*s
;
838 struct comedi_chaninfo it
;
840 if (copy_from_user(&it
, arg
, sizeof(it
)))
843 if (it
.subdev
>= dev
->n_subdevices
)
845 s
= &dev
->subdevices
[it
.subdev
];
847 if (it
.maxdata_list
) {
848 if (s
->maxdata
|| !s
->maxdata_list
)
850 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
851 s
->n_chan
* sizeof(unsigned int)))
858 if (copy_to_user(it
.flaglist
, s
->flaglist
,
859 s
->n_chan
* sizeof(unsigned int)))
866 if (!s
->range_table_list
)
868 for (i
= 0; i
< s
->n_chan
; i
++) {
871 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
872 (s
->range_table_list
[i
]->length
);
873 if (put_user(x
, it
.rangelist
+ i
))
877 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
878 s
->n_chan
* sizeof(unsigned int)))
888 buffer information ioctl
891 pointer to bufinfo structure
897 modified bufinfo at arg
900 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
901 struct comedi_bufinfo __user
*arg
, void *file
)
903 struct comedi_bufinfo bi
;
904 struct comedi_subdevice
*s
;
905 struct comedi_async
*async
;
907 if (copy_from_user(&bi
, arg
, sizeof(bi
)))
910 if (bi
.subdevice
>= dev
->n_subdevices
|| bi
.subdevice
< 0)
913 s
= &dev
->subdevices
[bi
.subdevice
];
915 if (s
->lock
&& s
->lock
!= file
)
921 DPRINTK("subdevice does not have async capability\n");
922 bi
.buf_write_ptr
= 0;
924 bi
.buf_write_count
= 0;
925 bi
.buf_read_count
= 0;
927 bi
.bytes_written
= 0;
932 bi
.bytes_written
= 0;
933 goto copyback_position
;
938 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
939 bi
.bytes_read
= comedi_buf_read_alloc(async
, bi
.bytes_read
);
940 comedi_buf_read_free(async
, bi
.bytes_read
);
942 if (comedi_is_subdevice_idle(s
) &&
943 async
->buf_write_count
== async
->buf_read_count
) {
944 do_become_nonbusy(dev
, s
);
948 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
950 comedi_buf_write_alloc(async
, bi
.bytes_written
);
951 comedi_buf_write_free(async
, bi
.bytes_written
);
955 bi
.buf_write_count
= async
->buf_write_count
;
956 bi
.buf_write_ptr
= async
->buf_write_ptr
;
957 bi
.buf_read_count
= async
->buf_read_count
;
958 bi
.buf_read_ptr
= async
->buf_read_ptr
;
961 if (copy_to_user(arg
, &bi
, sizeof(bi
)))
967 static int check_insn_config_length(struct comedi_insn
*insn
,
974 case INSN_CONFIG_DIO_OUTPUT
:
975 case INSN_CONFIG_DIO_INPUT
:
976 case INSN_CONFIG_DISARM
:
977 case INSN_CONFIG_RESET
:
981 case INSN_CONFIG_ARM
:
982 case INSN_CONFIG_DIO_QUERY
:
983 case INSN_CONFIG_BLOCK_SIZE
:
984 case INSN_CONFIG_FILTER
:
985 case INSN_CONFIG_SERIAL_CLOCK
:
986 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
987 case INSN_CONFIG_ALT_SOURCE
:
988 case INSN_CONFIG_SET_COUNTER_MODE
:
989 case INSN_CONFIG_8254_READ_STATUS
:
990 case INSN_CONFIG_SET_ROUTING
:
991 case INSN_CONFIG_GET_ROUTING
:
992 case INSN_CONFIG_GET_PWM_STATUS
:
993 case INSN_CONFIG_PWM_SET_PERIOD
:
994 case INSN_CONFIG_PWM_GET_PERIOD
:
998 case INSN_CONFIG_SET_GATE_SRC
:
999 case INSN_CONFIG_GET_GATE_SRC
:
1000 case INSN_CONFIG_SET_CLOCK_SRC
:
1001 case INSN_CONFIG_GET_CLOCK_SRC
:
1002 case INSN_CONFIG_SET_OTHER_SRC
:
1003 case INSN_CONFIG_GET_COUNTER_STATUS
:
1004 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
1005 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
1006 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
1010 case INSN_CONFIG_PWM_OUTPUT
:
1011 case INSN_CONFIG_ANALOG_TRIG
:
1015 case INSN_CONFIG_DIGITAL_TRIG
:
1019 /* by default we allow the insn since we don't have checks for
1020 * all possible cases yet */
1022 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1024 pr_warn("comedi: Add a check to %s in %s.\n",
1025 __func__
, __FILE__
);
1026 pr_warn("comedi: Assuming n=%i is correct.\n", insn
->n
);
1032 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
1033 unsigned int *data
, void *file
)
1035 struct comedi_subdevice
*s
;
1039 if (insn
->insn
& INSN_MASK_SPECIAL
) {
1040 /* a non-subdevice instruction */
1042 switch (insn
->insn
) {
1052 do_gettimeofday(&tv
);
1053 data
[0] = tv
.tv_sec
;
1054 data
[1] = tv
.tv_usec
;
1060 if (insn
->n
!= 1 || data
[0] >= 100000) {
1064 udelay(data
[0] / 1000);
1072 if (insn
->subdev
>= dev
->n_subdevices
) {
1073 DPRINTK("%d not usable subdevice\n",
1078 s
= &dev
->subdevices
[insn
->subdev
];
1080 DPRINTK("no async\n");
1084 if (!s
->async
->inttrig
) {
1085 DPRINTK("no inttrig\n");
1089 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
1094 DPRINTK("invalid insn\n");
1099 /* a subdevice instruction */
1100 unsigned int maxdata
;
1102 if (insn
->subdev
>= dev
->n_subdevices
) {
1103 DPRINTK("subdevice %d out of range\n", insn
->subdev
);
1107 s
= &dev
->subdevices
[insn
->subdev
];
1109 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1110 DPRINTK("%d not usable subdevice\n", insn
->subdev
);
1115 /* are we locked? (ioctl lock) */
1116 if (s
->lock
&& s
->lock
!= file
) {
1117 DPRINTK("device locked\n");
1122 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
1125 DPRINTK("bad chanspec\n");
1133 /* This looks arbitrary. It is. */
1134 s
->busy
= &parse_insn
;
1135 switch (insn
->insn
) {
1137 ret
= s
->insn_read(dev
, s
, insn
, data
);
1140 maxdata
= s
->maxdata_list
1141 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1143 for (i
= 0; i
< insn
->n
; ++i
) {
1144 if (data
[i
] > maxdata
) {
1146 DPRINTK("bad data value(s)\n");
1151 ret
= s
->insn_write(dev
, s
, insn
, data
);
1157 /* Most drivers ignore the base channel in
1158 * insn->chanspec. Fix this here if
1159 * the subdevice has <= 32 channels. */
1161 unsigned int orig_mask
;
1163 orig_mask
= data
[0];
1164 if (s
->n_chan
<= 32) {
1165 shift
= CR_CHAN(insn
->chanspec
);
1173 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1174 data
[0] = orig_mask
;
1180 ret
= check_insn_config_length(insn
, data
);
1183 ret
= s
->insn_config(dev
, s
, insn
, data
);
1199 * synchronous instructions
1202 * pointer to sync cmd structure
1205 * sync cmd struct at arg
1212 /* arbitrary limits */
1213 #define MAX_SAMPLES 256
1214 static int do_insnlist_ioctl(struct comedi_device
*dev
,
1215 struct comedi_insnlist __user
*arg
, void *file
)
1217 struct comedi_insnlist insnlist
;
1218 struct comedi_insn
*insns
= NULL
;
1219 unsigned int *data
= NULL
;
1223 if (copy_from_user(&insnlist
, arg
, sizeof(insnlist
)))
1226 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1228 DPRINTK("kmalloc failed\n");
1233 insns
= kcalloc(insnlist
.n_insns
, sizeof(*insns
), GFP_KERNEL
);
1235 DPRINTK("kmalloc failed\n");
1240 if (copy_from_user(insns
, insnlist
.insns
,
1241 sizeof(*insns
) * insnlist
.n_insns
)) {
1242 DPRINTK("copy_from_user failed\n");
1247 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
1248 if (insns
[i
].n
> MAX_SAMPLES
) {
1249 DPRINTK("number of samples too large\n");
1253 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
1254 if (copy_from_user(data
, insns
[i
].data
,
1255 insns
[i
].n
* sizeof(unsigned int))) {
1256 DPRINTK("copy_from_user failed\n");
1261 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
1264 if (insns
[i
].insn
& INSN_MASK_READ
) {
1265 if (copy_to_user(insns
[i
].data
, data
,
1266 insns
[i
].n
* sizeof(unsigned int))) {
1267 DPRINTK("copy_to_user failed\n");
1287 * synchronous instructions
1293 * struct comedi_insn struct at arg
1299 static int do_insn_ioctl(struct comedi_device
*dev
,
1300 struct comedi_insn __user
*arg
, void *file
)
1302 struct comedi_insn insn
;
1303 unsigned int *data
= NULL
;
1306 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1312 if (copy_from_user(&insn
, arg
, sizeof(insn
))) {
1317 /* This is where the behavior of insn and insnlist deviate. */
1318 if (insn
.n
> MAX_SAMPLES
)
1319 insn
.n
= MAX_SAMPLES
;
1320 if (insn
.insn
& INSN_MASK_WRITE
) {
1321 if (copy_from_user(data
,
1323 insn
.n
* sizeof(unsigned int))) {
1328 ret
= parse_insn(dev
, &insn
, data
, file
);
1331 if (insn
.insn
& INSN_MASK_READ
) {
1332 if (copy_to_user(insn
.data
,
1334 insn
.n
* sizeof(unsigned int))) {
1347 static int do_cmd_ioctl(struct comedi_device
*dev
,
1348 struct comedi_cmd __user
*arg
, void *file
)
1350 struct comedi_cmd cmd
;
1351 struct comedi_subdevice
*s
;
1352 struct comedi_async
*async
;
1354 unsigned int __user
*user_chanlist
;
1356 if (copy_from_user(&cmd
, arg
, sizeof(cmd
))) {
1357 DPRINTK("bad cmd address\n");
1360 /* save user's chanlist pointer so it can be restored later */
1361 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1363 if (cmd
.subdev
>= dev
->n_subdevices
) {
1364 DPRINTK("%d no such subdevice\n", cmd
.subdev
);
1368 s
= &dev
->subdevices
[cmd
.subdev
];
1371 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1372 DPRINTK("%d not valid subdevice\n", cmd
.subdev
);
1376 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1377 DPRINTK("subdevice %i does not support commands\n",
1382 /* are we locked? (ioctl lock) */
1383 if (s
->lock
&& s
->lock
!= file
) {
1384 DPRINTK("subdevice locked\n");
1390 DPRINTK("subdevice busy\n");
1395 /* make sure channel/gain list isn't too long */
1396 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1397 DPRINTK("channel/gain list too long %u > %d\n",
1398 cmd
.chanlist_len
, s
->len_chanlist
);
1403 /* make sure channel/gain list isn't too short */
1404 if (cmd
.chanlist_len
< 1) {
1405 DPRINTK("channel/gain list too short %u < 1\n",
1412 async
->cmd
.data
= NULL
;
1413 /* load channel/gain list */
1414 async
->cmd
.chanlist
=
1415 kmalloc(async
->cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1416 if (!async
->cmd
.chanlist
) {
1417 DPRINTK("allocation failed\n");
1422 if (copy_from_user(async
->cmd
.chanlist
, user_chanlist
,
1423 async
->cmd
.chanlist_len
* sizeof(int))) {
1424 DPRINTK("fault reading chanlist\n");
1429 /* make sure each element in channel/gain list is valid */
1430 ret
= comedi_check_chanlist(s
,
1431 async
->cmd
.chanlist_len
,
1432 async
->cmd
.chanlist
);
1434 DPRINTK("bad chanlist\n");
1438 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1440 if (async
->cmd
.flags
& TRIG_BOGUS
|| ret
) {
1441 DPRINTK("test returned %d\n", ret
);
1443 /* restore chanlist pointer before copying back */
1444 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1446 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1447 DPRINTK("fault writing cmd\n");
1455 if (!async
->prealloc_bufsz
) {
1457 DPRINTK("no buffer (?)\n");
1461 comedi_buf_reset(async
);
1464 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1466 if (async
->cmd
.flags
& TRIG_WAKE_EOS
)
1467 async
->cb_mask
|= COMEDI_CB_EOS
;
1469 comedi_set_subdevice_runflags(s
, ~0, SRF_USER
| SRF_RUNNING
);
1471 ret
= s
->do_cmd(dev
, s
);
1476 do_become_nonbusy(dev
, s
);
1483 command testing ioctl
1486 pointer to cmd structure
1489 cmd structure at arg
1493 modified cmd structure at arg
1496 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1497 struct comedi_cmd __user
*arg
, void *file
)
1499 struct comedi_cmd cmd
;
1500 struct comedi_subdevice
*s
;
1502 unsigned int *chanlist
= NULL
;
1503 unsigned int __user
*user_chanlist
;
1505 if (copy_from_user(&cmd
, arg
, sizeof(cmd
))) {
1506 DPRINTK("bad cmd address\n");
1509 /* save user's chanlist pointer so it can be restored later */
1510 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1512 if (cmd
.subdev
>= dev
->n_subdevices
) {
1513 DPRINTK("%d no such subdevice\n", cmd
.subdev
);
1517 s
= &dev
->subdevices
[cmd
.subdev
];
1518 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1519 DPRINTK("%d not valid subdevice\n", cmd
.subdev
);
1523 if (!s
->do_cmd
|| !s
->do_cmdtest
) {
1524 DPRINTK("subdevice %i does not support commands\n",
1529 /* make sure channel/gain list isn't too long */
1530 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1531 DPRINTK("channel/gain list too long %d > %d\n",
1532 cmd
.chanlist_len
, s
->len_chanlist
);
1537 /* load channel/gain list */
1540 kmalloc(cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1542 DPRINTK("allocation failed\n");
1547 if (copy_from_user(chanlist
, user_chanlist
,
1548 cmd
.chanlist_len
* sizeof(int))) {
1549 DPRINTK("fault reading chanlist\n");
1554 /* make sure each element in channel/gain list is valid */
1555 ret
= comedi_check_chanlist(s
, cmd
.chanlist_len
, chanlist
);
1557 DPRINTK("bad chanlist\n");
1561 cmd
.chanlist
= chanlist
;
1564 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1566 /* restore chanlist pointer before copying back */
1567 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1569 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1570 DPRINTK("bad cmd address\n");
1595 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1599 unsigned long flags
;
1600 struct comedi_subdevice
*s
;
1602 if (arg
>= dev
->n_subdevices
)
1604 s
= &dev
->subdevices
[arg
];
1606 spin_lock_irqsave(&s
->spin_lock
, flags
);
1607 if (s
->busy
|| s
->lock
)
1611 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1618 ret
= s
->lock_f(dev
, s
);
1637 This function isn't protected by the semaphore, since
1638 we already own the lock.
1640 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1643 struct comedi_subdevice
*s
;
1645 if (arg
>= dev
->n_subdevices
)
1647 s
= &dev
->subdevices
[arg
];
1652 if (s
->lock
&& s
->lock
!= file
)
1655 if (s
->lock
== file
) {
1669 cancel acquisition ioctl
1681 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1684 struct comedi_subdevice
*s
;
1686 if (arg
>= dev
->n_subdevices
)
1688 s
= &dev
->subdevices
[arg
];
1689 if (s
->async
== NULL
)
1692 if (s
->lock
&& s
->lock
!= file
)
1698 if (s
->busy
!= file
)
1701 return do_cancel(dev
, s
);
1706 instructs driver to synchronize buffers
1718 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1721 struct comedi_subdevice
*s
;
1723 if (arg
>= dev
->n_subdevices
)
1725 s
= &dev
->subdevices
[arg
];
1727 if (s
->lock
&& s
->lock
!= file
)
1733 if (s
->busy
!= file
)
1737 return s
->poll(dev
, s
);
1742 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
1745 const unsigned minor
= iminor(file_inode(file
));
1746 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
1752 mutex_lock(&dev
->mutex
);
1754 /* Device config is special, because it must work on
1755 * an unconfigured device. */
1756 if (cmd
== COMEDI_DEVCONFIG
) {
1757 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
1758 /* Device config not appropriate on non-board minors. */
1762 rc
= do_devconfig_ioctl(dev
,
1763 (struct comedi_devconfig __user
*)arg
);
1766 dev
->minor
>= comedi_num_legacy_minors
) {
1767 /* Successfully unconfigured a dynamically
1768 * allocated device. Try and remove it. */
1769 struct comedi_device
*devr
;
1770 devr
= comedi_clear_board_minor(dev
->minor
);
1772 mutex_unlock(&dev
->mutex
);
1773 comedi_free_board_dev(dev
);
1781 if (!dev
->attached
) {
1782 DPRINTK("no driver configured on /dev/comedi%i\n", dev
->minor
);
1788 case COMEDI_BUFCONFIG
:
1789 rc
= do_bufconfig_ioctl(dev
,
1790 (struct comedi_bufconfig __user
*)arg
);
1792 case COMEDI_DEVINFO
:
1793 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
1796 case COMEDI_SUBDINFO
:
1797 rc
= do_subdinfo_ioctl(dev
,
1798 (struct comedi_subdinfo __user
*)arg
,
1801 case COMEDI_CHANINFO
:
1802 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
1804 case COMEDI_RANGEINFO
:
1805 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
1807 case COMEDI_BUFINFO
:
1808 rc
= do_bufinfo_ioctl(dev
,
1809 (struct comedi_bufinfo __user
*)arg
,
1813 rc
= do_lock_ioctl(dev
, arg
, file
);
1816 rc
= do_unlock_ioctl(dev
, arg
, file
);
1819 rc
= do_cancel_ioctl(dev
, arg
, file
);
1822 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
1824 case COMEDI_CMDTEST
:
1825 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
1828 case COMEDI_INSNLIST
:
1829 rc
= do_insnlist_ioctl(dev
,
1830 (struct comedi_insnlist __user
*)arg
,
1834 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
1838 rc
= do_poll_ioctl(dev
, arg
, file
);
1846 mutex_unlock(&dev
->mutex
);
1850 static void comedi_vm_open(struct vm_area_struct
*area
)
1852 struct comedi_async
*async
;
1853 struct comedi_device
*dev
;
1855 async
= area
->vm_private_data
;
1856 dev
= async
->subdevice
->device
;
1858 mutex_lock(&dev
->mutex
);
1859 async
->mmap_count
++;
1860 mutex_unlock(&dev
->mutex
);
1863 static void comedi_vm_close(struct vm_area_struct
*area
)
1865 struct comedi_async
*async
;
1866 struct comedi_device
*dev
;
1868 async
= area
->vm_private_data
;
1869 dev
= async
->subdevice
->device
;
1871 mutex_lock(&dev
->mutex
);
1872 async
->mmap_count
--;
1873 mutex_unlock(&dev
->mutex
);
1876 static struct vm_operations_struct comedi_vm_ops
= {
1877 .open
= comedi_vm_open
,
1878 .close
= comedi_vm_close
,
1881 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1883 const unsigned minor
= iminor(file_inode(file
));
1884 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
1885 struct comedi_subdevice
*s
;
1886 struct comedi_async
*async
;
1887 unsigned long start
= vma
->vm_start
;
1896 mutex_lock(&dev
->mutex
);
1898 if (!dev
->attached
) {
1899 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1904 if (vma
->vm_flags
& VM_WRITE
)
1905 s
= comedi_write_subdevice(dev
, minor
);
1907 s
= comedi_read_subdevice(dev
, minor
);
1919 if (vma
->vm_pgoff
!= 0) {
1920 DPRINTK("comedi: mmap() offset must be 0.\n");
1925 size
= vma
->vm_end
- vma
->vm_start
;
1926 if (size
> async
->prealloc_bufsz
) {
1930 if (size
& (~PAGE_MASK
)) {
1935 n_pages
= size
>> PAGE_SHIFT
;
1936 for (i
= 0; i
< n_pages
; ++i
) {
1937 struct comedi_buf_page
*buf
= &async
->buf_page_list
[i
];
1939 if (remap_pfn_range(vma
, start
,
1940 page_to_pfn(virt_to_page(buf
->virt_addr
)),
1941 PAGE_SIZE
, PAGE_SHARED
)) {
1948 vma
->vm_ops
= &comedi_vm_ops
;
1949 vma
->vm_private_data
= async
;
1951 async
->mmap_count
++;
1955 mutex_unlock(&dev
->mutex
);
1959 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
1961 unsigned int mask
= 0;
1962 const unsigned minor
= iminor(file_inode(file
));
1963 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
1964 struct comedi_subdevice
*s
;
1969 mutex_lock(&dev
->mutex
);
1971 if (!dev
->attached
) {
1972 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1976 s
= comedi_read_subdevice(dev
, minor
);
1977 if (s
&& s
->async
) {
1978 poll_wait(file
, &s
->async
->wait_head
, wait
);
1979 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
1980 comedi_buf_read_n_available(s
->async
) > 0)
1981 mask
|= POLLIN
| POLLRDNORM
;
1984 s
= comedi_write_subdevice(dev
, minor
);
1985 if (s
&& s
->async
) {
1986 unsigned int bps
= bytes_per_sample(s
->async
->subdevice
);
1988 poll_wait(file
, &s
->async
->wait_head
, wait
);
1989 comedi_buf_write_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1990 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
1991 comedi_buf_write_n_allocated(s
->async
) >= bps
)
1992 mask
|= POLLOUT
| POLLWRNORM
;
1996 mutex_unlock(&dev
->mutex
);
2000 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
2001 size_t nbytes
, loff_t
*offset
)
2003 struct comedi_subdevice
*s
;
2004 struct comedi_async
*async
;
2005 int n
, m
, count
= 0, retval
= 0;
2006 DECLARE_WAITQUEUE(wait
, current
);
2007 const unsigned minor
= iminor(file_inode(file
));
2008 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2013 if (!dev
->attached
) {
2014 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
2018 s
= comedi_write_subdevice(dev
, minor
);
2019 if (!s
|| !s
->async
)
2024 if (!s
->busy
|| !nbytes
)
2026 if (s
->busy
!= file
)
2029 add_wait_queue(&async
->wait_head
, &wait
);
2030 while (nbytes
> 0 && !retval
) {
2031 set_current_state(TASK_INTERRUPTIBLE
);
2033 if (!comedi_is_subdevice_running(s
)) {
2035 if (comedi_is_subdevice_in_error(s
))
2039 do_become_nonbusy(dev
, s
);
2047 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
2048 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
2049 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
2050 if (m
> comedi_buf_write_n_allocated(async
))
2051 m
= comedi_buf_write_n_allocated(async
);
2056 if (file
->f_flags
& O_NONBLOCK
) {
2061 if (signal_pending(current
)) {
2062 retval
= -ERESTARTSYS
;
2067 if (s
->busy
!= file
) {
2074 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
2080 comedi_buf_write_free(async
, n
);
2086 break; /* makes device work like a pipe */
2088 set_current_state(TASK_RUNNING
);
2089 remove_wait_queue(&async
->wait_head
, &wait
);
2091 return count
? count
: retval
;
2094 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
2097 struct comedi_subdevice
*s
;
2098 struct comedi_async
*async
;
2099 int n
, m
, count
= 0, retval
= 0;
2100 DECLARE_WAITQUEUE(wait
, current
);
2101 const unsigned minor
= iminor(file_inode(file
));
2102 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2107 if (!dev
->attached
) {
2108 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
2112 s
= comedi_read_subdevice(dev
, minor
);
2113 if (!s
|| !s
->async
)
2117 if (!s
->busy
|| !nbytes
)
2119 if (s
->busy
!= file
)
2122 add_wait_queue(&async
->wait_head
, &wait
);
2123 while (nbytes
> 0 && !retval
) {
2124 set_current_state(TASK_INTERRUPTIBLE
);
2128 m
= comedi_buf_read_n_available(async
);
2129 /* printk("%d available\n",m); */
2130 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
2131 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
2132 /* printk("%d contiguous\n",m); */
2137 if (!comedi_is_subdevice_running(s
)) {
2138 do_become_nonbusy(dev
, s
);
2139 if (comedi_is_subdevice_in_error(s
))
2145 if (file
->f_flags
& O_NONBLOCK
) {
2150 if (signal_pending(current
)) {
2151 retval
= -ERESTARTSYS
;
2158 if (s
->busy
!= file
) {
2164 m
= copy_to_user(buf
, async
->prealloc_buf
+
2165 async
->buf_read_ptr
, n
);
2171 comedi_buf_read_alloc(async
, n
);
2172 comedi_buf_read_free(async
, n
);
2178 break; /* makes device work like a pipe */
2180 if (comedi_is_subdevice_idle(s
) &&
2181 async
->buf_read_count
- async
->buf_write_count
== 0) {
2182 do_become_nonbusy(dev
, s
);
2184 set_current_state(TASK_RUNNING
);
2185 remove_wait_queue(&async
->wait_head
, &wait
);
2187 return count
? count
: retval
;
2190 static int comedi_open(struct inode
*inode
, struct file
*file
)
2192 const unsigned minor
= iminor(inode
);
2193 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2196 DPRINTK("invalid minor number\n");
2200 /* This is slightly hacky, but we want module autoloading
2202 * case: user opens device, attached -> ok
2203 * case: user opens device, unattached, !in_request_module -> autoload
2204 * case: user opens device, unattached, in_request_module -> fail
2205 * case: root opens device, attached -> ok
2206 * case: root opens device, unattached, in_request_module -> ok
2207 * (typically called from modprobe)
2208 * case: root opens device, unattached, !in_request_module -> autoload
2210 * The last could be changed to "-> ok", which would deny root
2213 mutex_lock(&dev
->mutex
);
2216 if (!capable(CAP_NET_ADMIN
) && dev
->in_request_module
) {
2217 DPRINTK("in request module\n");
2218 mutex_unlock(&dev
->mutex
);
2221 if (capable(CAP_NET_ADMIN
) && dev
->in_request_module
)
2224 dev
->in_request_module
= true;
2227 mutex_unlock(&dev
->mutex
);
2228 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
2229 mutex_lock(&dev
->mutex
);
2232 dev
->in_request_module
= false;
2234 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
2235 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2236 mutex_unlock(&dev
->mutex
);
2240 __module_get(THIS_MODULE
);
2242 if (dev
->attached
) {
2243 if (!try_module_get(dev
->driver
->module
)) {
2244 module_put(THIS_MODULE
);
2245 mutex_unlock(&dev
->mutex
);
2250 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
) {
2251 int rc
= dev
->open(dev
);
2253 module_put(dev
->driver
->module
);
2254 module_put(THIS_MODULE
);
2255 mutex_unlock(&dev
->mutex
);
2262 mutex_unlock(&dev
->mutex
);
2267 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2269 const unsigned minor
= iminor(file_inode(file
));
2270 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2275 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2278 static int comedi_close(struct inode
*inode
, struct file
*file
)
2280 const unsigned minor
= iminor(inode
);
2281 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2282 struct comedi_subdevice
*s
= NULL
;
2288 mutex_lock(&dev
->mutex
);
2290 if (dev
->subdevices
) {
2291 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2292 s
= &dev
->subdevices
[i
];
2294 if (s
->busy
== file
)
2296 if (s
->lock
== file
)
2300 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
2303 module_put(THIS_MODULE
);
2305 module_put(dev
->driver
->module
);
2309 mutex_unlock(&dev
->mutex
);
2311 if (file
->f_flags
& FASYNC
)
2312 comedi_fasync(-1, file
, 0);
2317 static const struct file_operations comedi_fops
= {
2318 .owner
= THIS_MODULE
,
2319 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2320 .compat_ioctl
= comedi_compat_ioctl
,
2321 .open
= comedi_open
,
2322 .release
= comedi_close
,
2323 .read
= comedi_read
,
2324 .write
= comedi_write
,
2325 .mmap
= comedi_mmap
,
2326 .poll
= comedi_poll
,
2327 .fasync
= comedi_fasync
,
2328 .llseek
= noop_llseek
,
2331 void comedi_error(const struct comedi_device
*dev
, const char *s
)
2333 dev_err(dev
->class_dev
, "%s: %s\n", dev
->driver
->driver_name
, s
);
2335 EXPORT_SYMBOL(comedi_error
);
2337 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2339 struct comedi_async
*async
= s
->async
;
2340 unsigned runflags
= 0;
2341 unsigned runflags_mask
= 0;
2343 /* DPRINTK("comedi_event 0x%x\n",mask); */
2345 if (!comedi_is_subdevice_running(s
))
2349 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
2350 COMEDI_CB_OVERFLOW
)) {
2351 runflags_mask
|= SRF_RUNNING
;
2353 /* remember if an error event has occurred, so an error
2354 * can be returned the next time the user does a read() */
2355 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2356 runflags_mask
|= SRF_ERROR
;
2357 runflags
|= SRF_ERROR
;
2359 if (runflags_mask
) {
2360 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2361 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2364 if (async
->cb_mask
& s
->async
->events
) {
2365 if (comedi_get_subdevice_runflags(s
) & SRF_USER
) {
2366 wake_up_interruptible(&async
->wait_head
);
2367 if (s
->subdev_flags
& SDF_CMD_READ
)
2368 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2369 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2370 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2373 async
->cb_func(s
->async
->events
, async
->cb_arg
);
2376 s
->async
->events
= 0;
2378 EXPORT_SYMBOL(comedi_event
);
2380 /* Note: the ->mutex is pre-locked on successful return */
2381 struct comedi_device
*comedi_alloc_board_minor(struct device
*hardware_device
)
2383 struct comedi_device
*dev
;
2384 struct device
*csdev
;
2387 dev
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2389 return ERR_PTR(-ENOMEM
);
2390 comedi_device_init(dev
);
2391 comedi_set_hw_dev(dev
, hardware_device
);
2392 mutex_lock(&dev
->mutex
);
2393 mutex_lock(&comedi_board_minor_table_lock
);
2394 for (i
= hardware_device
? comedi_num_legacy_minors
: 0;
2395 i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2396 if (comedi_board_minor_table
[i
] == NULL
) {
2397 comedi_board_minor_table
[i
] = dev
;
2401 mutex_unlock(&comedi_board_minor_table_lock
);
2402 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2403 mutex_unlock(&dev
->mutex
);
2404 comedi_device_cleanup(dev
);
2406 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2407 return ERR_PTR(-EBUSY
);
2410 csdev
= device_create(comedi_class
, hardware_device
,
2411 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2413 dev
->class_dev
= csdev
;
2415 /* Note: dev->mutex needs to be unlocked by the caller. */
2419 static void comedi_free_board_minor(unsigned minor
)
2421 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2422 comedi_free_board_dev(comedi_clear_board_minor(minor
));
2425 void comedi_release_hardware_device(struct device
*hardware_device
)
2428 struct comedi_device
*dev
;
2430 for (minor
= comedi_num_legacy_minors
; minor
< COMEDI_NUM_BOARD_MINORS
;
2432 mutex_lock(&comedi_board_minor_table_lock
);
2433 dev
= comedi_board_minor_table
[minor
];
2434 if (dev
&& dev
->hw_dev
== hardware_device
) {
2435 comedi_board_minor_table
[minor
] = NULL
;
2436 mutex_unlock(&comedi_board_minor_table_lock
);
2437 comedi_free_board_dev(dev
);
2440 mutex_unlock(&comedi_board_minor_table_lock
);
2444 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2446 struct comedi_device
*dev
= s
->device
;
2447 struct device
*csdev
;
2450 mutex_lock(&comedi_subdevice_minor_table_lock
);
2451 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
) {
2452 if (comedi_subdevice_minor_table
[i
] == NULL
) {
2453 comedi_subdevice_minor_table
[i
] = s
;
2457 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2458 if (i
== COMEDI_NUM_SUBDEVICE_MINORS
) {
2459 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2462 i
+= COMEDI_NUM_BOARD_MINORS
;
2464 csdev
= device_create(comedi_class
, dev
->class_dev
,
2465 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2466 dev
->minor
, s
->index
);
2468 s
->class_dev
= csdev
;
2473 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2482 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2483 BUG_ON(s
->minor
< COMEDI_NUM_BOARD_MINORS
);
2485 i
= s
->minor
- COMEDI_NUM_BOARD_MINORS
;
2486 mutex_lock(&comedi_subdevice_minor_table_lock
);
2487 if (s
== comedi_subdevice_minor_table
[i
])
2488 comedi_subdevice_minor_table
[i
] = NULL
;
2489 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2491 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2492 s
->class_dev
= NULL
;
2496 static void comedi_cleanup_board_minors(void)
2500 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++)
2501 comedi_free_board_minor(i
);
2504 static int __init
comedi_init(void)
2509 pr_info("comedi: version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2511 if (comedi_num_legacy_minors
< 0 ||
2512 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2513 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2514 COMEDI_NUM_BOARD_MINORS
);
2518 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2519 COMEDI_NUM_MINORS
, "comedi");
2522 cdev_init(&comedi_cdev
, &comedi_fops
);
2523 comedi_cdev
.owner
= THIS_MODULE
;
2524 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2525 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2526 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2530 comedi_class
= class_create(THIS_MODULE
, "comedi");
2531 if (IS_ERR(comedi_class
)) {
2532 pr_err("comedi: failed to create class\n");
2533 cdev_del(&comedi_cdev
);
2534 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2536 return PTR_ERR(comedi_class
);
2539 comedi_class
->dev_attrs
= comedi_dev_attrs
;
2541 /* XXX requires /proc interface */
2544 /* create devices files for legacy/manual use */
2545 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2546 struct comedi_device
*dev
;
2547 dev
= comedi_alloc_board_minor(NULL
);
2549 comedi_cleanup_board_minors();
2550 cdev_del(&comedi_cdev
);
2551 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2553 return PTR_ERR(dev
);
2555 /* comedi_alloc_board_minor() locked the mutex */
2556 mutex_unlock(&dev
->mutex
);
2562 module_init(comedi_init
);
2564 static void __exit
comedi_cleanup(void)
2568 comedi_cleanup_board_minors();
2569 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
)
2570 BUG_ON(comedi_board_minor_table
[i
]);
2571 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
)
2572 BUG_ON(comedi_subdevice_minor_table
[i
]);
2574 class_destroy(comedi_class
);
2575 cdev_del(&comedi_cdev
);
2576 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2578 comedi_proc_cleanup();
2580 module_exit(comedi_cleanup
);
2582 MODULE_AUTHOR("http://www.comedi.org");
2583 MODULE_DESCRIPTION("Comedi core module");
2584 MODULE_LICENSE("GPL");