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_GPL(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 bool comedi_clear_board_dev(struct comedi_device
*dev
)
125 unsigned int i
= dev
->minor
;
126 bool cleared
= false;
128 mutex_lock(&comedi_board_minor_table_lock
);
129 if (dev
== comedi_board_minor_table
[i
]) {
130 comedi_board_minor_table
[i
] = NULL
;
133 mutex_unlock(&comedi_board_minor_table_lock
);
137 static struct comedi_device
*comedi_clear_board_minor(unsigned minor
)
139 struct comedi_device
*dev
;
141 mutex_lock(&comedi_board_minor_table_lock
);
142 dev
= comedi_board_minor_table
[minor
];
143 comedi_board_minor_table
[minor
] = NULL
;
144 mutex_unlock(&comedi_board_minor_table_lock
);
148 static void comedi_free_board_dev(struct comedi_device
*dev
)
151 if (dev
->class_dev
) {
152 device_destroy(comedi_class
,
153 MKDEV(COMEDI_MAJOR
, dev
->minor
));
155 comedi_device_cleanup(dev
);
160 static struct comedi_subdevice
161 *comedi_subdevice_from_minor(unsigned minor
)
163 struct comedi_subdevice
*s
;
164 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
166 BUG_ON(i
>= COMEDI_NUM_SUBDEVICE_MINORS
);
167 mutex_lock(&comedi_subdevice_minor_table_lock
);
168 s
= comedi_subdevice_minor_table
[i
];
169 mutex_unlock(&comedi_subdevice_minor_table_lock
);
173 static struct comedi_device
*comedi_dev_from_board_minor(unsigned minor
)
175 struct comedi_device
*dev
;
177 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
178 mutex_lock(&comedi_board_minor_table_lock
);
179 dev
= comedi_board_minor_table
[minor
];
180 mutex_unlock(&comedi_board_minor_table_lock
);
184 static struct comedi_device
*comedi_dev_from_subdevice_minor(unsigned minor
)
186 struct comedi_subdevice
*s
;
188 s
= comedi_subdevice_from_minor(minor
);
189 return s
? s
->device
: NULL
;
192 struct comedi_device
*comedi_dev_from_minor(unsigned minor
)
194 if (minor
< COMEDI_NUM_BOARD_MINORS
)
195 return comedi_dev_from_board_minor(minor
);
197 return comedi_dev_from_subdevice_minor(minor
);
199 EXPORT_SYMBOL_GPL(comedi_dev_from_minor
);
201 static struct comedi_subdevice
*
202 comedi_read_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
204 struct comedi_subdevice
*s
;
206 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
207 s
= comedi_subdevice_from_minor(minor
);
208 if (!s
|| s
->device
!= dev
)
210 if (s
->subdev_flags
& SDF_CMD_READ
)
213 return dev
->read_subdev
;
216 static struct comedi_subdevice
*
217 comedi_write_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
219 struct comedi_subdevice
*s
;
221 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
222 s
= comedi_subdevice_from_minor(minor
);
223 if (!s
|| s
->device
!= dev
)
225 if (s
->subdev_flags
& SDF_CMD_WRITE
)
228 return dev
->write_subdev
;
231 static int resize_async_buffer(struct comedi_device
*dev
,
232 struct comedi_subdevice
*s
,
233 struct comedi_async
*async
, unsigned new_size
)
237 if (new_size
> async
->max_bufsize
)
241 DPRINTK("subdevice is busy, cannot resize buffer\n");
244 if (async
->mmap_count
) {
245 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
249 /* make sure buffer is an integral number of pages
251 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
253 retval
= comedi_buf_alloc(dev
, s
, new_size
);
258 retval
= s
->buf_change(dev
, s
, new_size
);
263 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
264 dev
->minor
, s
->index
, async
->prealloc_bufsz
);
268 /* sysfs attribute files */
270 static ssize_t
show_max_read_buffer_kb(struct device
*csdev
,
271 struct device_attribute
*attr
, char *buf
)
273 unsigned int minor
= MINOR(csdev
->devt
);
274 struct comedi_device
*dev
;
275 struct comedi_subdevice
*s
;
276 unsigned int size
= 0;
278 dev
= comedi_dev_from_minor(minor
);
282 mutex_lock(&dev
->mutex
);
283 s
= comedi_read_subdevice(dev
, minor
);
284 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
285 size
= s
->async
->max_bufsize
/ 1024;
286 mutex_unlock(&dev
->mutex
);
288 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
291 static ssize_t
store_max_read_buffer_kb(struct device
*csdev
,
292 struct device_attribute
*attr
,
293 const char *buf
, size_t count
)
295 unsigned int minor
= MINOR(csdev
->devt
);
296 struct comedi_device
*dev
;
297 struct comedi_subdevice
*s
;
301 err
= kstrtouint(buf
, 10, &size
);
304 if (size
> (UINT_MAX
/ 1024))
308 dev
= comedi_dev_from_minor(minor
);
312 mutex_lock(&dev
->mutex
);
313 s
= comedi_read_subdevice(dev
, minor
);
314 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
315 s
->async
->max_bufsize
= size
;
318 mutex_unlock(&dev
->mutex
);
320 return err
? err
: count
;
323 static ssize_t
show_read_buffer_kb(struct device
*csdev
,
324 struct device_attribute
*attr
, char *buf
)
326 unsigned int minor
= MINOR(csdev
->devt
);
327 struct comedi_device
*dev
;
328 struct comedi_subdevice
*s
;
329 unsigned int size
= 0;
331 dev
= comedi_dev_from_minor(minor
);
335 mutex_lock(&dev
->mutex
);
336 s
= comedi_read_subdevice(dev
, minor
);
337 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
338 size
= s
->async
->prealloc_bufsz
/ 1024;
339 mutex_unlock(&dev
->mutex
);
341 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
344 static ssize_t
store_read_buffer_kb(struct device
*csdev
,
345 struct device_attribute
*attr
,
346 const char *buf
, size_t count
)
348 unsigned int minor
= MINOR(csdev
->devt
);
349 struct comedi_device
*dev
;
350 struct comedi_subdevice
*s
;
354 err
= kstrtouint(buf
, 10, &size
);
357 if (size
> (UINT_MAX
/ 1024))
361 dev
= comedi_dev_from_minor(minor
);
365 mutex_lock(&dev
->mutex
);
366 s
= comedi_read_subdevice(dev
, minor
);
367 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
368 err
= resize_async_buffer(dev
, s
, s
->async
, size
);
371 mutex_unlock(&dev
->mutex
);
373 return err
? err
: count
;
376 static ssize_t
show_max_write_buffer_kb(struct device
*csdev
,
377 struct device_attribute
*attr
,
380 unsigned int minor
= MINOR(csdev
->devt
);
381 struct comedi_device
*dev
;
382 struct comedi_subdevice
*s
;
383 unsigned int size
= 0;
385 dev
= comedi_dev_from_minor(minor
);
389 mutex_lock(&dev
->mutex
);
390 s
= comedi_write_subdevice(dev
, minor
);
391 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
392 size
= s
->async
->max_bufsize
/ 1024;
393 mutex_unlock(&dev
->mutex
);
395 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
398 static ssize_t
store_max_write_buffer_kb(struct device
*csdev
,
399 struct device_attribute
*attr
,
400 const char *buf
, size_t count
)
402 unsigned int minor
= MINOR(csdev
->devt
);
403 struct comedi_device
*dev
;
404 struct comedi_subdevice
*s
;
408 err
= kstrtouint(buf
, 10, &size
);
411 if (size
> (UINT_MAX
/ 1024))
415 dev
= comedi_dev_from_minor(minor
);
419 mutex_lock(&dev
->mutex
);
420 s
= comedi_write_subdevice(dev
, minor
);
421 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
422 s
->async
->max_bufsize
= size
;
425 mutex_unlock(&dev
->mutex
);
427 return err
? err
: count
;
430 static ssize_t
show_write_buffer_kb(struct device
*csdev
,
431 struct device_attribute
*attr
, char *buf
)
433 unsigned int minor
= MINOR(csdev
->devt
);
434 struct comedi_device
*dev
;
435 struct comedi_subdevice
*s
;
436 unsigned int size
= 0;
438 dev
= comedi_dev_from_minor(minor
);
442 mutex_lock(&dev
->mutex
);
443 s
= comedi_write_subdevice(dev
, minor
);
444 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
445 size
= s
->async
->prealloc_bufsz
/ 1024;
446 mutex_unlock(&dev
->mutex
);
448 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
451 static ssize_t
store_write_buffer_kb(struct device
*csdev
,
452 struct device_attribute
*attr
,
453 const char *buf
, size_t count
)
455 unsigned int minor
= MINOR(csdev
->devt
);
456 struct comedi_device
*dev
;
457 struct comedi_subdevice
*s
;
461 err
= kstrtouint(buf
, 10, &size
);
464 if (size
> (UINT_MAX
/ 1024))
468 dev
= comedi_dev_from_minor(minor
);
472 mutex_lock(&dev
->mutex
);
473 s
= comedi_write_subdevice(dev
, minor
);
474 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
475 err
= resize_async_buffer(dev
, s
, s
->async
, size
);
478 mutex_unlock(&dev
->mutex
);
480 return err
? err
: count
;
483 static struct device_attribute comedi_dev_attrs
[] = {
484 __ATTR(max_read_buffer_kb
, S_IRUGO
| S_IWUSR
,
485 show_max_read_buffer_kb
, store_max_read_buffer_kb
),
486 __ATTR(read_buffer_kb
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
487 show_read_buffer_kb
, store_read_buffer_kb
),
488 __ATTR(max_write_buffer_kb
, S_IRUGO
| S_IWUSR
,
489 show_max_write_buffer_kb
, store_max_write_buffer_kb
),
490 __ATTR(write_buffer_kb
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
491 show_write_buffer_kb
, store_write_buffer_kb
),
495 static void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
496 unsigned mask
, unsigned bits
)
500 spin_lock_irqsave(&s
->spin_lock
, flags
);
501 s
->runflags
&= ~mask
;
502 s
->runflags
|= (bits
& mask
);
503 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
506 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
511 spin_lock_irqsave(&s
->spin_lock
, flags
);
512 runflags
= s
->runflags
;
513 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
517 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
)
519 unsigned runflags
= comedi_get_subdevice_runflags(s
);
521 return (runflags
& SRF_RUNNING
) ? true : false;
523 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running
);
525 static bool comedi_is_subdevice_in_error(struct comedi_subdevice
*s
)
527 unsigned runflags
= comedi_get_subdevice_runflags(s
);
529 return (runflags
& SRF_ERROR
) ? true : false;
532 static bool comedi_is_subdevice_idle(struct comedi_subdevice
*s
)
534 unsigned runflags
= comedi_get_subdevice_runflags(s
);
536 return (runflags
& (SRF_ERROR
| SRF_RUNNING
)) ? false : true;
540 This function restores a subdevice to an idle state.
542 static void do_become_nonbusy(struct comedi_device
*dev
,
543 struct comedi_subdevice
*s
)
545 struct comedi_async
*async
= s
->async
;
547 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
549 comedi_buf_reset(async
);
550 async
->inttrig
= NULL
;
551 kfree(async
->cmd
.chanlist
);
552 async
->cmd
.chanlist
= NULL
;
554 dev_err(dev
->class_dev
,
555 "BUG: (?) do_become_nonbusy called with async=NULL\n");
561 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
565 if (comedi_is_subdevice_running(s
) && s
->cancel
)
566 ret
= s
->cancel(dev
, s
);
568 do_become_nonbusy(dev
, s
);
573 static int is_device_busy(struct comedi_device
*dev
)
575 struct comedi_subdevice
*s
;
581 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
582 s
= &dev
->subdevices
[i
];
585 if (s
->async
&& s
->async
->mmap_count
)
597 pointer to devconfig structure
600 devconfig structure at arg
605 static int do_devconfig_ioctl(struct comedi_device
*dev
,
606 struct comedi_devconfig __user
*arg
)
608 struct comedi_devconfig it
;
610 if (!capable(CAP_SYS_ADMIN
))
614 if (is_device_busy(dev
))
617 struct module
*driver_module
= dev
->driver
->module
;
618 comedi_device_detach(dev
);
619 module_put(driver_module
);
624 if (copy_from_user(&it
, arg
, sizeof(it
)))
627 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
629 if (it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
630 dev_warn(dev
->class_dev
,
631 "comedi_config --init_data is deprecated\n");
635 if (dev
->minor
>= comedi_num_legacy_minors
)
636 /* don't re-use dynamically allocated comedi devices */
639 /* This increments the driver module count on success. */
640 return comedi_device_attach(dev
, &it
);
645 buffer configuration ioctl
648 pointer to bufconfig structure
654 modified bufconfig at arg
657 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
658 struct comedi_bufconfig __user
*arg
)
660 struct comedi_bufconfig bc
;
661 struct comedi_async
*async
;
662 struct comedi_subdevice
*s
;
665 if (copy_from_user(&bc
, arg
, sizeof(bc
)))
668 if (bc
.subdevice
>= dev
->n_subdevices
|| bc
.subdevice
< 0)
671 s
= &dev
->subdevices
[bc
.subdevice
];
675 DPRINTK("subdevice does not have async capability\n");
681 if (bc
.maximum_size
) {
682 if (!capable(CAP_SYS_ADMIN
))
685 async
->max_bufsize
= bc
.maximum_size
;
689 retval
= resize_async_buffer(dev
, s
, async
, bc
.size
);
694 bc
.size
= async
->prealloc_bufsz
;
695 bc
.maximum_size
= async
->max_bufsize
;
698 if (copy_to_user(arg
, &bc
, sizeof(bc
)))
709 pointer to devinfo structure
718 static int do_devinfo_ioctl(struct comedi_device
*dev
,
719 struct comedi_devinfo __user
*arg
,
722 const unsigned minor
= iminor(file_inode(file
));
723 struct comedi_subdevice
*s
;
724 struct comedi_devinfo devinfo
;
726 memset(&devinfo
, 0, sizeof(devinfo
));
728 /* fill devinfo structure */
729 devinfo
.version_code
= COMEDI_VERSION_CODE
;
730 devinfo
.n_subdevs
= dev
->n_subdevices
;
731 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
732 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
734 s
= comedi_read_subdevice(dev
, minor
);
736 devinfo
.read_subdevice
= s
->index
;
738 devinfo
.read_subdevice
= -1;
740 s
= comedi_write_subdevice(dev
, minor
);
742 devinfo
.write_subdevice
= s
->index
;
744 devinfo
.write_subdevice
= -1;
746 if (copy_to_user(arg
, &devinfo
, sizeof(devinfo
)))
757 pointer to array of subdevice info structures
763 array of subdevice info structures at arg
766 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
767 struct comedi_subdinfo __user
*arg
, void *file
)
770 struct comedi_subdinfo
*tmp
, *us
;
771 struct comedi_subdevice
*s
;
773 tmp
= kcalloc(dev
->n_subdevices
, sizeof(*tmp
), GFP_KERNEL
);
777 /* fill subdinfo structs */
778 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
779 s
= &dev
->subdevices
[i
];
783 us
->n_chan
= s
->n_chan
;
784 us
->subd_flags
= s
->subdev_flags
;
785 if (comedi_is_subdevice_running(s
))
786 us
->subd_flags
|= SDF_RUNNING
;
787 #define TIMER_nanosec 5 /* backwards compatibility */
788 us
->timer_type
= TIMER_nanosec
;
789 us
->len_chanlist
= s
->len_chanlist
;
790 us
->maxdata
= s
->maxdata
;
791 if (s
->range_table
) {
793 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
795 us
->range_type
= 0; /* XXX */
797 us
->flags
= s
->flags
;
800 us
->subd_flags
|= SDF_BUSY
;
802 us
->subd_flags
|= SDF_BUSY_OWNER
;
804 us
->subd_flags
|= SDF_LOCKED
;
806 us
->subd_flags
|= SDF_LOCK_OWNER
;
807 if (!s
->maxdata
&& s
->maxdata_list
)
808 us
->subd_flags
|= SDF_MAXDATA
;
810 us
->subd_flags
|= SDF_FLAGS
;
811 if (s
->range_table_list
)
812 us
->subd_flags
|= SDF_RANGETYPE
;
814 us
->subd_flags
|= SDF_CMD
;
816 if (s
->insn_bits
!= &insn_inval
)
817 us
->insn_bits_support
= COMEDI_SUPPORTED
;
819 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
821 us
->settling_time_0
= s
->settling_time_0
;
824 ret
= copy_to_user(arg
, tmp
, dev
->n_subdevices
* sizeof(*tmp
));
828 return ret
? -EFAULT
: 0;
836 pointer to chaninfo structure
839 chaninfo structure at arg
842 arrays at elements of chaninfo structure
845 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
846 struct comedi_chaninfo __user
*arg
)
848 struct comedi_subdevice
*s
;
849 struct comedi_chaninfo it
;
851 if (copy_from_user(&it
, arg
, sizeof(it
)))
854 if (it
.subdev
>= dev
->n_subdevices
)
856 s
= &dev
->subdevices
[it
.subdev
];
858 if (it
.maxdata_list
) {
859 if (s
->maxdata
|| !s
->maxdata_list
)
861 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
862 s
->n_chan
* sizeof(unsigned int)))
869 if (copy_to_user(it
.flaglist
, s
->flaglist
,
870 s
->n_chan
* sizeof(unsigned int)))
877 if (!s
->range_table_list
)
879 for (i
= 0; i
< s
->n_chan
; i
++) {
882 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
883 (s
->range_table_list
[i
]->length
);
884 if (put_user(x
, it
.rangelist
+ i
))
888 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
889 s
->n_chan
* sizeof(unsigned int)))
899 buffer information ioctl
902 pointer to bufinfo structure
908 modified bufinfo at arg
911 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
912 struct comedi_bufinfo __user
*arg
, void *file
)
914 struct comedi_bufinfo bi
;
915 struct comedi_subdevice
*s
;
916 struct comedi_async
*async
;
918 if (copy_from_user(&bi
, arg
, sizeof(bi
)))
921 if (bi
.subdevice
>= dev
->n_subdevices
|| bi
.subdevice
< 0)
924 s
= &dev
->subdevices
[bi
.subdevice
];
926 if (s
->lock
&& s
->lock
!= file
)
932 DPRINTK("subdevice does not have async capability\n");
933 bi
.buf_write_ptr
= 0;
935 bi
.buf_write_count
= 0;
936 bi
.buf_read_count
= 0;
938 bi
.bytes_written
= 0;
943 bi
.bytes_written
= 0;
944 goto copyback_position
;
949 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
950 bi
.bytes_read
= comedi_buf_read_alloc(async
, bi
.bytes_read
);
951 comedi_buf_read_free(async
, bi
.bytes_read
);
953 if (comedi_is_subdevice_idle(s
) &&
954 async
->buf_write_count
== async
->buf_read_count
) {
955 do_become_nonbusy(dev
, s
);
959 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
961 comedi_buf_write_alloc(async
, bi
.bytes_written
);
962 comedi_buf_write_free(async
, bi
.bytes_written
);
966 bi
.buf_write_count
= async
->buf_write_count
;
967 bi
.buf_write_ptr
= async
->buf_write_ptr
;
968 bi
.buf_read_count
= async
->buf_read_count
;
969 bi
.buf_read_ptr
= async
->buf_read_ptr
;
972 if (copy_to_user(arg
, &bi
, sizeof(bi
)))
978 static int check_insn_config_length(struct comedi_insn
*insn
,
985 case INSN_CONFIG_DIO_OUTPUT
:
986 case INSN_CONFIG_DIO_INPUT
:
987 case INSN_CONFIG_DISARM
:
988 case INSN_CONFIG_RESET
:
992 case INSN_CONFIG_ARM
:
993 case INSN_CONFIG_DIO_QUERY
:
994 case INSN_CONFIG_BLOCK_SIZE
:
995 case INSN_CONFIG_FILTER
:
996 case INSN_CONFIG_SERIAL_CLOCK
:
997 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
998 case INSN_CONFIG_ALT_SOURCE
:
999 case INSN_CONFIG_SET_COUNTER_MODE
:
1000 case INSN_CONFIG_8254_READ_STATUS
:
1001 case INSN_CONFIG_SET_ROUTING
:
1002 case INSN_CONFIG_GET_ROUTING
:
1003 case INSN_CONFIG_GET_PWM_STATUS
:
1004 case INSN_CONFIG_PWM_SET_PERIOD
:
1005 case INSN_CONFIG_PWM_GET_PERIOD
:
1009 case INSN_CONFIG_SET_GATE_SRC
:
1010 case INSN_CONFIG_GET_GATE_SRC
:
1011 case INSN_CONFIG_SET_CLOCK_SRC
:
1012 case INSN_CONFIG_GET_CLOCK_SRC
:
1013 case INSN_CONFIG_SET_OTHER_SRC
:
1014 case INSN_CONFIG_GET_COUNTER_STATUS
:
1015 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
1016 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
1017 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
1021 case INSN_CONFIG_PWM_OUTPUT
:
1022 case INSN_CONFIG_ANALOG_TRIG
:
1026 case INSN_CONFIG_DIGITAL_TRIG
:
1030 /* by default we allow the insn since we don't have checks for
1031 * all possible cases yet */
1033 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1035 pr_warn("comedi: Add a check to %s in %s.\n",
1036 __func__
, __FILE__
);
1037 pr_warn("comedi: Assuming n=%i is correct.\n", insn
->n
);
1043 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
1044 unsigned int *data
, void *file
)
1046 struct comedi_subdevice
*s
;
1050 if (insn
->insn
& INSN_MASK_SPECIAL
) {
1051 /* a non-subdevice instruction */
1053 switch (insn
->insn
) {
1063 do_gettimeofday(&tv
);
1064 data
[0] = tv
.tv_sec
;
1065 data
[1] = tv
.tv_usec
;
1071 if (insn
->n
!= 1 || data
[0] >= 100000) {
1075 udelay(data
[0] / 1000);
1083 if (insn
->subdev
>= dev
->n_subdevices
) {
1084 DPRINTK("%d not usable subdevice\n",
1089 s
= &dev
->subdevices
[insn
->subdev
];
1091 DPRINTK("no async\n");
1095 if (!s
->async
->inttrig
) {
1096 DPRINTK("no inttrig\n");
1100 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
1105 DPRINTK("invalid insn\n");
1110 /* a subdevice instruction */
1111 unsigned int maxdata
;
1113 if (insn
->subdev
>= dev
->n_subdevices
) {
1114 DPRINTK("subdevice %d out of range\n", insn
->subdev
);
1118 s
= &dev
->subdevices
[insn
->subdev
];
1120 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1121 DPRINTK("%d not usable subdevice\n", insn
->subdev
);
1126 /* are we locked? (ioctl lock) */
1127 if (s
->lock
&& s
->lock
!= file
) {
1128 DPRINTK("device locked\n");
1133 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
1136 DPRINTK("bad chanspec\n");
1144 /* This looks arbitrary. It is. */
1145 s
->busy
= &parse_insn
;
1146 switch (insn
->insn
) {
1148 ret
= s
->insn_read(dev
, s
, insn
, data
);
1151 maxdata
= s
->maxdata_list
1152 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1154 for (i
= 0; i
< insn
->n
; ++i
) {
1155 if (data
[i
] > maxdata
) {
1157 DPRINTK("bad data value(s)\n");
1162 ret
= s
->insn_write(dev
, s
, insn
, data
);
1168 /* Most drivers ignore the base channel in
1169 * insn->chanspec. Fix this here if
1170 * the subdevice has <= 32 channels. */
1172 unsigned int orig_mask
;
1174 orig_mask
= data
[0];
1175 if (s
->n_chan
<= 32) {
1176 shift
= CR_CHAN(insn
->chanspec
);
1184 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1185 data
[0] = orig_mask
;
1191 ret
= check_insn_config_length(insn
, data
);
1194 ret
= s
->insn_config(dev
, s
, insn
, data
);
1210 * synchronous instructions
1213 * pointer to sync cmd structure
1216 * sync cmd struct at arg
1223 /* arbitrary limits */
1224 #define MAX_SAMPLES 256
1225 static int do_insnlist_ioctl(struct comedi_device
*dev
,
1226 struct comedi_insnlist __user
*arg
, void *file
)
1228 struct comedi_insnlist insnlist
;
1229 struct comedi_insn
*insns
= NULL
;
1230 unsigned int *data
= NULL
;
1234 if (copy_from_user(&insnlist
, arg
, sizeof(insnlist
)))
1237 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1239 DPRINTK("kmalloc failed\n");
1244 insns
= kcalloc(insnlist
.n_insns
, sizeof(*insns
), GFP_KERNEL
);
1246 DPRINTK("kmalloc failed\n");
1251 if (copy_from_user(insns
, insnlist
.insns
,
1252 sizeof(*insns
) * insnlist
.n_insns
)) {
1253 DPRINTK("copy_from_user failed\n");
1258 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
1259 if (insns
[i
].n
> MAX_SAMPLES
) {
1260 DPRINTK("number of samples too large\n");
1264 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
1265 if (copy_from_user(data
, insns
[i
].data
,
1266 insns
[i
].n
* sizeof(unsigned int))) {
1267 DPRINTK("copy_from_user failed\n");
1272 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
1275 if (insns
[i
].insn
& INSN_MASK_READ
) {
1276 if (copy_to_user(insns
[i
].data
, data
,
1277 insns
[i
].n
* sizeof(unsigned int))) {
1278 DPRINTK("copy_to_user failed\n");
1298 * synchronous instructions
1304 * struct comedi_insn struct at arg
1310 static int do_insn_ioctl(struct comedi_device
*dev
,
1311 struct comedi_insn __user
*arg
, void *file
)
1313 struct comedi_insn insn
;
1314 unsigned int *data
= NULL
;
1317 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1323 if (copy_from_user(&insn
, arg
, sizeof(insn
))) {
1328 /* This is where the behavior of insn and insnlist deviate. */
1329 if (insn
.n
> MAX_SAMPLES
)
1330 insn
.n
= MAX_SAMPLES
;
1331 if (insn
.insn
& INSN_MASK_WRITE
) {
1332 if (copy_from_user(data
,
1334 insn
.n
* sizeof(unsigned int))) {
1339 ret
= parse_insn(dev
, &insn
, data
, file
);
1342 if (insn
.insn
& INSN_MASK_READ
) {
1343 if (copy_to_user(insn
.data
,
1345 insn
.n
* sizeof(unsigned int))) {
1358 static int do_cmd_ioctl(struct comedi_device
*dev
,
1359 struct comedi_cmd __user
*arg
, void *file
)
1361 struct comedi_cmd cmd
;
1362 struct comedi_subdevice
*s
;
1363 struct comedi_async
*async
;
1365 unsigned int __user
*user_chanlist
;
1367 if (copy_from_user(&cmd
, arg
, sizeof(cmd
))) {
1368 DPRINTK("bad cmd address\n");
1371 /* save user's chanlist pointer so it can be restored later */
1372 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1374 if (cmd
.subdev
>= dev
->n_subdevices
) {
1375 DPRINTK("%d no such subdevice\n", cmd
.subdev
);
1379 s
= &dev
->subdevices
[cmd
.subdev
];
1382 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1383 DPRINTK("%d not valid subdevice\n", cmd
.subdev
);
1387 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1388 DPRINTK("subdevice %i does not support commands\n",
1393 /* are we locked? (ioctl lock) */
1394 if (s
->lock
&& s
->lock
!= file
) {
1395 DPRINTK("subdevice locked\n");
1401 DPRINTK("subdevice busy\n");
1406 /* make sure channel/gain list isn't too long */
1407 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1408 DPRINTK("channel/gain list too long %u > %d\n",
1409 cmd
.chanlist_len
, s
->len_chanlist
);
1414 /* make sure channel/gain list isn't too short */
1415 if (cmd
.chanlist_len
< 1) {
1416 DPRINTK("channel/gain list too short %u < 1\n",
1423 async
->cmd
.data
= NULL
;
1424 /* load channel/gain list */
1425 async
->cmd
.chanlist
=
1426 kmalloc(async
->cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1427 if (!async
->cmd
.chanlist
) {
1428 DPRINTK("allocation failed\n");
1433 if (copy_from_user(async
->cmd
.chanlist
, user_chanlist
,
1434 async
->cmd
.chanlist_len
* sizeof(int))) {
1435 DPRINTK("fault reading chanlist\n");
1440 /* make sure each element in channel/gain list is valid */
1441 ret
= comedi_check_chanlist(s
,
1442 async
->cmd
.chanlist_len
,
1443 async
->cmd
.chanlist
);
1445 DPRINTK("bad chanlist\n");
1449 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1451 if (async
->cmd
.flags
& TRIG_BOGUS
|| ret
) {
1452 DPRINTK("test returned %d\n", ret
);
1454 /* restore chanlist pointer before copying back */
1455 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1457 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1458 DPRINTK("fault writing cmd\n");
1466 if (!async
->prealloc_bufsz
) {
1468 DPRINTK("no buffer (?)\n");
1472 comedi_buf_reset(async
);
1475 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1477 if (async
->cmd
.flags
& TRIG_WAKE_EOS
)
1478 async
->cb_mask
|= COMEDI_CB_EOS
;
1480 comedi_set_subdevice_runflags(s
, ~0, SRF_USER
| SRF_RUNNING
);
1482 ret
= s
->do_cmd(dev
, s
);
1487 do_become_nonbusy(dev
, s
);
1494 command testing ioctl
1497 pointer to cmd structure
1500 cmd structure at arg
1504 modified cmd structure at arg
1507 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1508 struct comedi_cmd __user
*arg
, void *file
)
1510 struct comedi_cmd cmd
;
1511 struct comedi_subdevice
*s
;
1513 unsigned int *chanlist
= NULL
;
1514 unsigned int __user
*user_chanlist
;
1516 if (copy_from_user(&cmd
, arg
, sizeof(cmd
))) {
1517 DPRINTK("bad cmd address\n");
1520 /* save user's chanlist pointer so it can be restored later */
1521 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1523 if (cmd
.subdev
>= dev
->n_subdevices
) {
1524 DPRINTK("%d no such subdevice\n", cmd
.subdev
);
1528 s
= &dev
->subdevices
[cmd
.subdev
];
1529 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1530 DPRINTK("%d not valid subdevice\n", cmd
.subdev
);
1534 if (!s
->do_cmd
|| !s
->do_cmdtest
) {
1535 DPRINTK("subdevice %i does not support commands\n",
1540 /* make sure channel/gain list isn't too long */
1541 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1542 DPRINTK("channel/gain list too long %d > %d\n",
1543 cmd
.chanlist_len
, s
->len_chanlist
);
1548 /* load channel/gain list */
1551 kmalloc(cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1553 DPRINTK("allocation failed\n");
1558 if (copy_from_user(chanlist
, user_chanlist
,
1559 cmd
.chanlist_len
* sizeof(int))) {
1560 DPRINTK("fault reading chanlist\n");
1565 /* make sure each element in channel/gain list is valid */
1566 ret
= comedi_check_chanlist(s
, cmd
.chanlist_len
, chanlist
);
1568 DPRINTK("bad chanlist\n");
1572 cmd
.chanlist
= chanlist
;
1575 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1577 /* restore chanlist pointer before copying back */
1578 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1580 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1581 DPRINTK("bad cmd address\n");
1606 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1610 unsigned long flags
;
1611 struct comedi_subdevice
*s
;
1613 if (arg
>= dev
->n_subdevices
)
1615 s
= &dev
->subdevices
[arg
];
1617 spin_lock_irqsave(&s
->spin_lock
, flags
);
1618 if (s
->busy
|| s
->lock
)
1622 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1629 ret
= s
->lock_f(dev
, s
);
1648 This function isn't protected by the semaphore, since
1649 we already own the lock.
1651 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1654 struct comedi_subdevice
*s
;
1656 if (arg
>= dev
->n_subdevices
)
1658 s
= &dev
->subdevices
[arg
];
1663 if (s
->lock
&& s
->lock
!= file
)
1666 if (s
->lock
== file
) {
1680 cancel acquisition ioctl
1692 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1695 struct comedi_subdevice
*s
;
1697 if (arg
>= dev
->n_subdevices
)
1699 s
= &dev
->subdevices
[arg
];
1700 if (s
->async
== NULL
)
1703 if (s
->lock
&& s
->lock
!= file
)
1709 if (s
->busy
!= file
)
1712 return do_cancel(dev
, s
);
1717 instructs driver to synchronize buffers
1729 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1732 struct comedi_subdevice
*s
;
1734 if (arg
>= dev
->n_subdevices
)
1736 s
= &dev
->subdevices
[arg
];
1738 if (s
->lock
&& s
->lock
!= file
)
1744 if (s
->busy
!= file
)
1748 return s
->poll(dev
, s
);
1753 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
1756 const unsigned minor
= iminor(file_inode(file
));
1757 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
1763 mutex_lock(&dev
->mutex
);
1765 /* Device config is special, because it must work on
1766 * an unconfigured device. */
1767 if (cmd
== COMEDI_DEVCONFIG
) {
1768 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
1769 /* Device config not appropriate on non-board minors. */
1773 rc
= do_devconfig_ioctl(dev
,
1774 (struct comedi_devconfig __user
*)arg
);
1777 dev
->minor
>= comedi_num_legacy_minors
) {
1778 /* Successfully unconfigured a dynamically
1779 * allocated device. Try and remove it. */
1780 if (comedi_clear_board_dev(dev
)) {
1781 mutex_unlock(&dev
->mutex
);
1782 comedi_free_board_dev(dev
);
1790 if (!dev
->attached
) {
1791 DPRINTK("no driver configured on /dev/comedi%i\n", dev
->minor
);
1797 case COMEDI_BUFCONFIG
:
1798 rc
= do_bufconfig_ioctl(dev
,
1799 (struct comedi_bufconfig __user
*)arg
);
1801 case COMEDI_DEVINFO
:
1802 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
1805 case COMEDI_SUBDINFO
:
1806 rc
= do_subdinfo_ioctl(dev
,
1807 (struct comedi_subdinfo __user
*)arg
,
1810 case COMEDI_CHANINFO
:
1811 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
1813 case COMEDI_RANGEINFO
:
1814 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
1816 case COMEDI_BUFINFO
:
1817 rc
= do_bufinfo_ioctl(dev
,
1818 (struct comedi_bufinfo __user
*)arg
,
1822 rc
= do_lock_ioctl(dev
, arg
, file
);
1825 rc
= do_unlock_ioctl(dev
, arg
, file
);
1828 rc
= do_cancel_ioctl(dev
, arg
, file
);
1831 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
1833 case COMEDI_CMDTEST
:
1834 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
1837 case COMEDI_INSNLIST
:
1838 rc
= do_insnlist_ioctl(dev
,
1839 (struct comedi_insnlist __user
*)arg
,
1843 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
1847 rc
= do_poll_ioctl(dev
, arg
, file
);
1855 mutex_unlock(&dev
->mutex
);
1859 static void comedi_vm_open(struct vm_area_struct
*area
)
1861 struct comedi_async
*async
;
1862 struct comedi_device
*dev
;
1864 async
= area
->vm_private_data
;
1865 dev
= async
->subdevice
->device
;
1867 mutex_lock(&dev
->mutex
);
1868 async
->mmap_count
++;
1869 mutex_unlock(&dev
->mutex
);
1872 static void comedi_vm_close(struct vm_area_struct
*area
)
1874 struct comedi_async
*async
;
1875 struct comedi_device
*dev
;
1877 async
= area
->vm_private_data
;
1878 dev
= async
->subdevice
->device
;
1880 mutex_lock(&dev
->mutex
);
1881 async
->mmap_count
--;
1882 mutex_unlock(&dev
->mutex
);
1885 static struct vm_operations_struct comedi_vm_ops
= {
1886 .open
= comedi_vm_open
,
1887 .close
= comedi_vm_close
,
1890 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1892 const unsigned minor
= iminor(file_inode(file
));
1893 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
1894 struct comedi_subdevice
*s
;
1895 struct comedi_async
*async
;
1896 unsigned long start
= vma
->vm_start
;
1905 mutex_lock(&dev
->mutex
);
1907 if (!dev
->attached
) {
1908 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1913 if (vma
->vm_flags
& VM_WRITE
)
1914 s
= comedi_write_subdevice(dev
, minor
);
1916 s
= comedi_read_subdevice(dev
, minor
);
1928 if (vma
->vm_pgoff
!= 0) {
1929 DPRINTK("comedi: mmap() offset must be 0.\n");
1934 size
= vma
->vm_end
- vma
->vm_start
;
1935 if (size
> async
->prealloc_bufsz
) {
1939 if (size
& (~PAGE_MASK
)) {
1944 n_pages
= size
>> PAGE_SHIFT
;
1945 for (i
= 0; i
< n_pages
; ++i
) {
1946 struct comedi_buf_page
*buf
= &async
->buf_page_list
[i
];
1948 if (remap_pfn_range(vma
, start
,
1949 page_to_pfn(virt_to_page(buf
->virt_addr
)),
1950 PAGE_SIZE
, PAGE_SHARED
)) {
1957 vma
->vm_ops
= &comedi_vm_ops
;
1958 vma
->vm_private_data
= async
;
1960 async
->mmap_count
++;
1964 mutex_unlock(&dev
->mutex
);
1968 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
1970 unsigned int mask
= 0;
1971 const unsigned minor
= iminor(file_inode(file
));
1972 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
1973 struct comedi_subdevice
*s
;
1978 mutex_lock(&dev
->mutex
);
1980 if (!dev
->attached
) {
1981 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1985 s
= comedi_read_subdevice(dev
, minor
);
1986 if (s
&& s
->async
) {
1987 poll_wait(file
, &s
->async
->wait_head
, wait
);
1988 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
1989 comedi_buf_read_n_available(s
->async
) > 0)
1990 mask
|= POLLIN
| POLLRDNORM
;
1993 s
= comedi_write_subdevice(dev
, minor
);
1994 if (s
&& s
->async
) {
1995 unsigned int bps
= bytes_per_sample(s
->async
->subdevice
);
1997 poll_wait(file
, &s
->async
->wait_head
, wait
);
1998 comedi_buf_write_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1999 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
2000 comedi_buf_write_n_allocated(s
->async
) >= bps
)
2001 mask
|= POLLOUT
| POLLWRNORM
;
2005 mutex_unlock(&dev
->mutex
);
2009 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
2010 size_t nbytes
, loff_t
*offset
)
2012 struct comedi_subdevice
*s
;
2013 struct comedi_async
*async
;
2014 int n
, m
, count
= 0, retval
= 0;
2015 DECLARE_WAITQUEUE(wait
, current
);
2016 const unsigned minor
= iminor(file_inode(file
));
2017 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2022 if (!dev
->attached
) {
2023 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
2027 s
= comedi_write_subdevice(dev
, minor
);
2028 if (!s
|| !s
->async
)
2033 if (!s
->busy
|| !nbytes
)
2035 if (s
->busy
!= file
)
2038 add_wait_queue(&async
->wait_head
, &wait
);
2039 while (nbytes
> 0 && !retval
) {
2040 set_current_state(TASK_INTERRUPTIBLE
);
2042 if (!comedi_is_subdevice_running(s
)) {
2044 if (comedi_is_subdevice_in_error(s
))
2048 do_become_nonbusy(dev
, s
);
2056 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
2057 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
2058 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
2059 if (m
> comedi_buf_write_n_allocated(async
))
2060 m
= comedi_buf_write_n_allocated(async
);
2065 if (file
->f_flags
& O_NONBLOCK
) {
2070 if (signal_pending(current
)) {
2071 retval
= -ERESTARTSYS
;
2076 if (s
->busy
!= file
) {
2083 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
2089 comedi_buf_write_free(async
, n
);
2095 break; /* makes device work like a pipe */
2097 set_current_state(TASK_RUNNING
);
2098 remove_wait_queue(&async
->wait_head
, &wait
);
2100 return count
? count
: retval
;
2103 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
2106 struct comedi_subdevice
*s
;
2107 struct comedi_async
*async
;
2108 int n
, m
, count
= 0, retval
= 0;
2109 DECLARE_WAITQUEUE(wait
, current
);
2110 const unsigned minor
= iminor(file_inode(file
));
2111 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2116 if (!dev
->attached
) {
2117 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
2121 s
= comedi_read_subdevice(dev
, minor
);
2122 if (!s
|| !s
->async
)
2126 if (!s
->busy
|| !nbytes
)
2128 if (s
->busy
!= file
)
2131 add_wait_queue(&async
->wait_head
, &wait
);
2132 while (nbytes
> 0 && !retval
) {
2133 set_current_state(TASK_INTERRUPTIBLE
);
2137 m
= comedi_buf_read_n_available(async
);
2138 /* printk("%d available\n",m); */
2139 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
2140 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
2141 /* printk("%d contiguous\n",m); */
2146 if (!comedi_is_subdevice_running(s
)) {
2147 do_become_nonbusy(dev
, s
);
2148 if (comedi_is_subdevice_in_error(s
))
2154 if (file
->f_flags
& O_NONBLOCK
) {
2159 if (signal_pending(current
)) {
2160 retval
= -ERESTARTSYS
;
2167 if (s
->busy
!= file
) {
2173 m
= copy_to_user(buf
, async
->prealloc_buf
+
2174 async
->buf_read_ptr
, n
);
2180 comedi_buf_read_alloc(async
, n
);
2181 comedi_buf_read_free(async
, n
);
2187 break; /* makes device work like a pipe */
2189 if (comedi_is_subdevice_idle(s
) &&
2190 async
->buf_read_count
- async
->buf_write_count
== 0) {
2191 do_become_nonbusy(dev
, s
);
2193 set_current_state(TASK_RUNNING
);
2194 remove_wait_queue(&async
->wait_head
, &wait
);
2196 return count
? count
: retval
;
2199 static int comedi_open(struct inode
*inode
, struct file
*file
)
2201 const unsigned minor
= iminor(inode
);
2202 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2205 DPRINTK("invalid minor number\n");
2209 /* This is slightly hacky, but we want module autoloading
2211 * case: user opens device, attached -> ok
2212 * case: user opens device, unattached, !in_request_module -> autoload
2213 * case: user opens device, unattached, in_request_module -> fail
2214 * case: root opens device, attached -> ok
2215 * case: root opens device, unattached, in_request_module -> ok
2216 * (typically called from modprobe)
2217 * case: root opens device, unattached, !in_request_module -> autoload
2219 * The last could be changed to "-> ok", which would deny root
2222 mutex_lock(&dev
->mutex
);
2225 if (!capable(CAP_NET_ADMIN
) && dev
->in_request_module
) {
2226 DPRINTK("in request module\n");
2227 mutex_unlock(&dev
->mutex
);
2230 if (capable(CAP_NET_ADMIN
) && dev
->in_request_module
)
2233 dev
->in_request_module
= true;
2236 mutex_unlock(&dev
->mutex
);
2237 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
2238 mutex_lock(&dev
->mutex
);
2241 dev
->in_request_module
= false;
2243 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
2244 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2245 mutex_unlock(&dev
->mutex
);
2249 __module_get(THIS_MODULE
);
2251 if (dev
->attached
) {
2252 if (!try_module_get(dev
->driver
->module
)) {
2253 module_put(THIS_MODULE
);
2254 mutex_unlock(&dev
->mutex
);
2259 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
) {
2260 int rc
= dev
->open(dev
);
2262 module_put(dev
->driver
->module
);
2263 module_put(THIS_MODULE
);
2264 mutex_unlock(&dev
->mutex
);
2271 mutex_unlock(&dev
->mutex
);
2276 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2278 const unsigned minor
= iminor(file_inode(file
));
2279 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2284 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2287 static int comedi_close(struct inode
*inode
, struct file
*file
)
2289 const unsigned minor
= iminor(inode
);
2290 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2291 struct comedi_subdevice
*s
= NULL
;
2297 mutex_lock(&dev
->mutex
);
2299 if (dev
->subdevices
) {
2300 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2301 s
= &dev
->subdevices
[i
];
2303 if (s
->busy
== file
)
2305 if (s
->lock
== file
)
2309 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
2312 module_put(THIS_MODULE
);
2314 module_put(dev
->driver
->module
);
2318 mutex_unlock(&dev
->mutex
);
2320 if (file
->f_flags
& FASYNC
)
2321 comedi_fasync(-1, file
, 0);
2326 static const struct file_operations comedi_fops
= {
2327 .owner
= THIS_MODULE
,
2328 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2329 .compat_ioctl
= comedi_compat_ioctl
,
2330 .open
= comedi_open
,
2331 .release
= comedi_close
,
2332 .read
= comedi_read
,
2333 .write
= comedi_write
,
2334 .mmap
= comedi_mmap
,
2335 .poll
= comedi_poll
,
2336 .fasync
= comedi_fasync
,
2337 .llseek
= noop_llseek
,
2340 void comedi_error(const struct comedi_device
*dev
, const char *s
)
2342 dev_err(dev
->class_dev
, "%s: %s\n", dev
->driver
->driver_name
, s
);
2344 EXPORT_SYMBOL_GPL(comedi_error
);
2346 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2348 struct comedi_async
*async
= s
->async
;
2349 unsigned runflags
= 0;
2350 unsigned runflags_mask
= 0;
2352 /* DPRINTK("comedi_event 0x%x\n",mask); */
2354 if (!comedi_is_subdevice_running(s
))
2358 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
2359 COMEDI_CB_OVERFLOW
)) {
2360 runflags_mask
|= SRF_RUNNING
;
2362 /* remember if an error event has occurred, so an error
2363 * can be returned the next time the user does a read() */
2364 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2365 runflags_mask
|= SRF_ERROR
;
2366 runflags
|= SRF_ERROR
;
2368 if (runflags_mask
) {
2369 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2370 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2373 if (async
->cb_mask
& s
->async
->events
) {
2374 if (comedi_get_subdevice_runflags(s
) & SRF_USER
) {
2375 wake_up_interruptible(&async
->wait_head
);
2376 if (s
->subdev_flags
& SDF_CMD_READ
)
2377 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2378 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2379 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2382 async
->cb_func(s
->async
->events
, async
->cb_arg
);
2385 s
->async
->events
= 0;
2387 EXPORT_SYMBOL_GPL(comedi_event
);
2389 /* Note: the ->mutex is pre-locked on successful return */
2390 struct comedi_device
*comedi_alloc_board_minor(struct device
*hardware_device
)
2392 struct comedi_device
*dev
;
2393 struct device
*csdev
;
2396 dev
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2398 return ERR_PTR(-ENOMEM
);
2399 comedi_device_init(dev
);
2400 comedi_set_hw_dev(dev
, hardware_device
);
2401 mutex_lock(&dev
->mutex
);
2402 mutex_lock(&comedi_board_minor_table_lock
);
2403 for (i
= hardware_device
? comedi_num_legacy_minors
: 0;
2404 i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2405 if (comedi_board_minor_table
[i
] == NULL
) {
2406 comedi_board_minor_table
[i
] = dev
;
2410 mutex_unlock(&comedi_board_minor_table_lock
);
2411 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2412 mutex_unlock(&dev
->mutex
);
2413 comedi_device_cleanup(dev
);
2415 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2416 return ERR_PTR(-EBUSY
);
2419 csdev
= device_create(comedi_class
, hardware_device
,
2420 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2422 dev
->class_dev
= csdev
;
2424 /* Note: dev->mutex needs to be unlocked by the caller. */
2428 static void comedi_free_board_minor(unsigned minor
)
2430 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2431 comedi_free_board_dev(comedi_clear_board_minor(minor
));
2434 void comedi_release_hardware_device(struct device
*hardware_device
)
2437 struct comedi_device
*dev
;
2439 for (minor
= comedi_num_legacy_minors
; minor
< COMEDI_NUM_BOARD_MINORS
;
2441 mutex_lock(&comedi_board_minor_table_lock
);
2442 dev
= comedi_board_minor_table
[minor
];
2443 if (dev
&& dev
->hw_dev
== hardware_device
) {
2444 comedi_board_minor_table
[minor
] = NULL
;
2445 mutex_unlock(&comedi_board_minor_table_lock
);
2446 comedi_free_board_dev(dev
);
2449 mutex_unlock(&comedi_board_minor_table_lock
);
2453 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2455 struct comedi_device
*dev
= s
->device
;
2456 struct device
*csdev
;
2459 mutex_lock(&comedi_subdevice_minor_table_lock
);
2460 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
) {
2461 if (comedi_subdevice_minor_table
[i
] == NULL
) {
2462 comedi_subdevice_minor_table
[i
] = s
;
2466 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2467 if (i
== COMEDI_NUM_SUBDEVICE_MINORS
) {
2468 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2471 i
+= COMEDI_NUM_BOARD_MINORS
;
2473 csdev
= device_create(comedi_class
, dev
->class_dev
,
2474 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2475 dev
->minor
, s
->index
);
2477 s
->class_dev
= csdev
;
2482 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2491 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2492 BUG_ON(s
->minor
< COMEDI_NUM_BOARD_MINORS
);
2494 i
= s
->minor
- COMEDI_NUM_BOARD_MINORS
;
2495 mutex_lock(&comedi_subdevice_minor_table_lock
);
2496 if (s
== comedi_subdevice_minor_table
[i
])
2497 comedi_subdevice_minor_table
[i
] = NULL
;
2498 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2500 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2501 s
->class_dev
= NULL
;
2505 static void comedi_cleanup_board_minors(void)
2509 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++)
2510 comedi_free_board_minor(i
);
2513 static int __init
comedi_init(void)
2518 pr_info("comedi: version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2520 if (comedi_num_legacy_minors
< 0 ||
2521 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2522 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2523 COMEDI_NUM_BOARD_MINORS
);
2527 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2528 COMEDI_NUM_MINORS
, "comedi");
2531 cdev_init(&comedi_cdev
, &comedi_fops
);
2532 comedi_cdev
.owner
= THIS_MODULE
;
2533 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2534 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2535 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2539 comedi_class
= class_create(THIS_MODULE
, "comedi");
2540 if (IS_ERR(comedi_class
)) {
2541 pr_err("comedi: failed to create class\n");
2542 cdev_del(&comedi_cdev
);
2543 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2545 return PTR_ERR(comedi_class
);
2548 comedi_class
->dev_attrs
= comedi_dev_attrs
;
2550 /* XXX requires /proc interface */
2553 /* create devices files for legacy/manual use */
2554 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2555 struct comedi_device
*dev
;
2556 dev
= comedi_alloc_board_minor(NULL
);
2558 comedi_cleanup_board_minors();
2559 cdev_del(&comedi_cdev
);
2560 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2562 return PTR_ERR(dev
);
2564 /* comedi_alloc_board_minor() locked the mutex */
2565 mutex_unlock(&dev
->mutex
);
2571 module_init(comedi_init
);
2573 static void __exit
comedi_cleanup(void)
2577 comedi_cleanup_board_minors();
2578 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
)
2579 BUG_ON(comedi_board_minor_table
[i
]);
2580 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
)
2581 BUG_ON(comedi_subdevice_minor_table
[i
]);
2583 class_destroy(comedi_class
);
2584 cdev_del(&comedi_cdev
);
2585 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2587 comedi_proc_cleanup();
2589 module_exit(comedi_cleanup
);
2591 MODULE_AUTHOR("http://www.comedi.org");
2592 MODULE_DESCRIPTION("Comedi core module");
2593 MODULE_LICENSE("GPL");