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.
19 #include "comedi_compat32.h"
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/fcntl.h>
26 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/kmod.h>
30 #include <linux/poll.h>
31 #include <linux/init.h>
32 #include <linux/device.h>
33 #include <linux/vmalloc.h>
35 #include "comedidev.h"
36 #include <linux/cdev.h>
37 #include <linux/stat.h>
40 #include <linux/uaccess.h>
42 #include "comedi_internal.h"
44 #define COMEDI_NUM_MINORS 0x100
45 #define COMEDI_NUM_SUBDEVICE_MINORS \
46 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
48 static int comedi_num_legacy_minors
;
49 module_param(comedi_num_legacy_minors
, int, S_IRUGO
);
50 MODULE_PARM_DESC(comedi_num_legacy_minors
,
51 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
54 unsigned int comedi_default_buf_size_kb
= CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
;
55 module_param(comedi_default_buf_size_kb
, uint
, S_IRUGO
| S_IWUSR
);
56 MODULE_PARM_DESC(comedi_default_buf_size_kb
,
57 "default asynchronous buffer size in KiB (default "
58 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
) ")");
60 unsigned int comedi_default_buf_maxsize_kb
61 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
;
62 module_param(comedi_default_buf_maxsize_kb
, uint
, S_IRUGO
| S_IWUSR
);
63 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb
,
64 "default maximum size of asynchronous buffer in KiB (default "
65 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
) ")");
67 static DEFINE_MUTEX(comedi_board_minor_table_lock
);
68 static struct comedi_device
69 *comedi_board_minor_table
[COMEDI_NUM_BOARD_MINORS
];
71 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock
);
72 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
73 static struct comedi_subdevice
74 *comedi_subdevice_minor_table
[COMEDI_NUM_SUBDEVICE_MINORS
];
76 static struct class *comedi_class
;
77 static struct cdev comedi_cdev
;
79 static void comedi_device_init(struct comedi_device
*dev
)
81 kref_init(&dev
->refcount
);
82 spin_lock_init(&dev
->spinlock
);
83 mutex_init(&dev
->mutex
);
84 init_rwsem(&dev
->attach_lock
);
88 static void comedi_dev_kref_release(struct kref
*kref
)
90 struct comedi_device
*dev
=
91 container_of(kref
, struct comedi_device
, refcount
);
93 mutex_destroy(&dev
->mutex
);
97 int comedi_dev_put(struct comedi_device
*dev
)
100 return kref_put(&dev
->refcount
, comedi_dev_kref_release
);
103 EXPORT_SYMBOL_GPL(comedi_dev_put
);
105 static struct comedi_device
*comedi_dev_get(struct comedi_device
*dev
)
108 kref_get(&dev
->refcount
);
112 static void comedi_device_cleanup(struct comedi_device
*dev
)
114 struct module
*driver_module
= NULL
;
118 mutex_lock(&dev
->mutex
);
120 driver_module
= dev
->driver
->module
;
121 comedi_device_detach(dev
);
122 while (dev
->use_count
> 0) {
124 module_put(driver_module
);
125 module_put(THIS_MODULE
);
128 mutex_unlock(&dev
->mutex
);
131 static bool comedi_clear_board_dev(struct comedi_device
*dev
)
133 unsigned int i
= dev
->minor
;
134 bool cleared
= false;
136 mutex_lock(&comedi_board_minor_table_lock
);
137 if (dev
== comedi_board_minor_table
[i
]) {
138 comedi_board_minor_table
[i
] = NULL
;
141 mutex_unlock(&comedi_board_minor_table_lock
);
145 static struct comedi_device
*comedi_clear_board_minor(unsigned minor
)
147 struct comedi_device
*dev
;
149 mutex_lock(&comedi_board_minor_table_lock
);
150 dev
= comedi_board_minor_table
[minor
];
151 comedi_board_minor_table
[minor
] = NULL
;
152 mutex_unlock(&comedi_board_minor_table_lock
);
156 static void comedi_free_board_dev(struct comedi_device
*dev
)
159 if (dev
->class_dev
) {
160 device_destroy(comedi_class
,
161 MKDEV(COMEDI_MAJOR
, dev
->minor
));
163 comedi_device_cleanup(dev
);
168 static struct comedi_subdevice
169 *comedi_subdevice_from_minor(const struct comedi_device
*dev
, unsigned minor
)
171 struct comedi_subdevice
*s
;
172 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
174 BUG_ON(i
>= COMEDI_NUM_SUBDEVICE_MINORS
);
175 mutex_lock(&comedi_subdevice_minor_table_lock
);
176 s
= comedi_subdevice_minor_table
[i
];
177 if (s
&& s
->device
!= dev
)
179 mutex_unlock(&comedi_subdevice_minor_table_lock
);
183 static struct comedi_device
*comedi_dev_get_from_board_minor(unsigned minor
)
185 struct comedi_device
*dev
;
187 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
188 mutex_lock(&comedi_board_minor_table_lock
);
189 dev
= comedi_dev_get(comedi_board_minor_table
[minor
]);
190 mutex_unlock(&comedi_board_minor_table_lock
);
194 static struct comedi_device
*comedi_dev_get_from_subdevice_minor(unsigned minor
)
196 struct comedi_device
*dev
;
197 struct comedi_subdevice
*s
;
198 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
200 BUG_ON(i
>= COMEDI_NUM_SUBDEVICE_MINORS
);
201 mutex_lock(&comedi_subdevice_minor_table_lock
);
202 s
= comedi_subdevice_minor_table
[i
];
203 dev
= comedi_dev_get(s
? s
->device
: NULL
);
204 mutex_unlock(&comedi_subdevice_minor_table_lock
);
208 struct comedi_device
*comedi_dev_get_from_minor(unsigned minor
)
210 if (minor
< COMEDI_NUM_BOARD_MINORS
)
211 return comedi_dev_get_from_board_minor(minor
);
213 return comedi_dev_get_from_subdevice_minor(minor
);
215 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor
);
217 static struct comedi_subdevice
*
218 comedi_read_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
220 struct comedi_subdevice
*s
;
222 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
223 s
= comedi_subdevice_from_minor(dev
, minor
);
224 if (s
== NULL
|| (s
->subdev_flags
& SDF_CMD_READ
))
227 return dev
->read_subdev
;
230 static struct comedi_subdevice
*
231 comedi_write_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
233 struct comedi_subdevice
*s
;
235 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
236 s
= comedi_subdevice_from_minor(dev
, minor
);
237 if (s
== NULL
|| (s
->subdev_flags
& SDF_CMD_WRITE
))
240 return dev
->write_subdev
;
243 static int resize_async_buffer(struct comedi_device
*dev
,
244 struct comedi_subdevice
*s
,
245 struct comedi_async
*async
, unsigned new_size
)
249 if (new_size
> async
->max_bufsize
)
253 dev_dbg(dev
->class_dev
,
254 "subdevice is busy, cannot resize buffer\n");
257 if (comedi_buf_is_mmapped(async
)) {
258 dev_dbg(dev
->class_dev
,
259 "subdevice is mmapped, cannot resize buffer\n");
263 /* make sure buffer is an integral number of pages
265 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
267 retval
= comedi_buf_alloc(dev
, s
, new_size
);
272 retval
= s
->buf_change(dev
, s
, new_size
);
277 dev_dbg(dev
->class_dev
, "subd %d buffer resized to %i bytes\n",
278 s
->index
, async
->prealloc_bufsz
);
282 /* sysfs attribute files */
284 static ssize_t
max_read_buffer_kb_show(struct device
*csdev
,
285 struct device_attribute
*attr
, char *buf
)
287 unsigned int minor
= MINOR(csdev
->devt
);
288 struct comedi_device
*dev
;
289 struct comedi_subdevice
*s
;
290 unsigned int size
= 0;
292 dev
= comedi_dev_get_from_minor(minor
);
296 mutex_lock(&dev
->mutex
);
297 s
= comedi_read_subdevice(dev
, minor
);
298 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
299 size
= s
->async
->max_bufsize
/ 1024;
300 mutex_unlock(&dev
->mutex
);
303 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
306 static ssize_t
max_read_buffer_kb_store(struct device
*csdev
,
307 struct device_attribute
*attr
,
308 const char *buf
, size_t count
)
310 unsigned int minor
= MINOR(csdev
->devt
);
311 struct comedi_device
*dev
;
312 struct comedi_subdevice
*s
;
316 err
= kstrtouint(buf
, 10, &size
);
319 if (size
> (UINT_MAX
/ 1024))
323 dev
= comedi_dev_get_from_minor(minor
);
327 mutex_lock(&dev
->mutex
);
328 s
= comedi_read_subdevice(dev
, minor
);
329 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
330 s
->async
->max_bufsize
= size
;
333 mutex_unlock(&dev
->mutex
);
336 return err
? err
: count
;
338 static DEVICE_ATTR_RW(max_read_buffer_kb
);
340 static ssize_t
read_buffer_kb_show(struct device
*csdev
,
341 struct device_attribute
*attr
, char *buf
)
343 unsigned int minor
= MINOR(csdev
->devt
);
344 struct comedi_device
*dev
;
345 struct comedi_subdevice
*s
;
346 unsigned int size
= 0;
348 dev
= comedi_dev_get_from_minor(minor
);
352 mutex_lock(&dev
->mutex
);
353 s
= comedi_read_subdevice(dev
, minor
);
354 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
355 size
= s
->async
->prealloc_bufsz
/ 1024;
356 mutex_unlock(&dev
->mutex
);
359 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
362 static ssize_t
read_buffer_kb_store(struct device
*csdev
,
363 struct device_attribute
*attr
,
364 const char *buf
, size_t count
)
366 unsigned int minor
= MINOR(csdev
->devt
);
367 struct comedi_device
*dev
;
368 struct comedi_subdevice
*s
;
372 err
= kstrtouint(buf
, 10, &size
);
375 if (size
> (UINT_MAX
/ 1024))
379 dev
= comedi_dev_get_from_minor(minor
);
383 mutex_lock(&dev
->mutex
);
384 s
= comedi_read_subdevice(dev
, minor
);
385 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
386 err
= resize_async_buffer(dev
, s
, s
->async
, size
);
389 mutex_unlock(&dev
->mutex
);
392 return err
? err
: count
;
394 static DEVICE_ATTR_RW(read_buffer_kb
);
396 static ssize_t
max_write_buffer_kb_show(struct device
*csdev
,
397 struct device_attribute
*attr
,
400 unsigned int minor
= MINOR(csdev
->devt
);
401 struct comedi_device
*dev
;
402 struct comedi_subdevice
*s
;
403 unsigned int size
= 0;
405 dev
= comedi_dev_get_from_minor(minor
);
409 mutex_lock(&dev
->mutex
);
410 s
= comedi_write_subdevice(dev
, minor
);
411 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
412 size
= s
->async
->max_bufsize
/ 1024;
413 mutex_unlock(&dev
->mutex
);
416 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
419 static ssize_t
max_write_buffer_kb_store(struct device
*csdev
,
420 struct device_attribute
*attr
,
421 const char *buf
, size_t count
)
423 unsigned int minor
= MINOR(csdev
->devt
);
424 struct comedi_device
*dev
;
425 struct comedi_subdevice
*s
;
429 err
= kstrtouint(buf
, 10, &size
);
432 if (size
> (UINT_MAX
/ 1024))
436 dev
= comedi_dev_get_from_minor(minor
);
440 mutex_lock(&dev
->mutex
);
441 s
= comedi_write_subdevice(dev
, minor
);
442 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
443 s
->async
->max_bufsize
= size
;
446 mutex_unlock(&dev
->mutex
);
449 return err
? err
: count
;
451 static DEVICE_ATTR_RW(max_write_buffer_kb
);
453 static ssize_t
write_buffer_kb_show(struct device
*csdev
,
454 struct device_attribute
*attr
, char *buf
)
456 unsigned int minor
= MINOR(csdev
->devt
);
457 struct comedi_device
*dev
;
458 struct comedi_subdevice
*s
;
459 unsigned int size
= 0;
461 dev
= comedi_dev_get_from_minor(minor
);
465 mutex_lock(&dev
->mutex
);
466 s
= comedi_write_subdevice(dev
, minor
);
467 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
468 size
= s
->async
->prealloc_bufsz
/ 1024;
469 mutex_unlock(&dev
->mutex
);
472 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
475 static ssize_t
write_buffer_kb_store(struct device
*csdev
,
476 struct device_attribute
*attr
,
477 const char *buf
, size_t count
)
479 unsigned int minor
= MINOR(csdev
->devt
);
480 struct comedi_device
*dev
;
481 struct comedi_subdevice
*s
;
485 err
= kstrtouint(buf
, 10, &size
);
488 if (size
> (UINT_MAX
/ 1024))
492 dev
= comedi_dev_get_from_minor(minor
);
496 mutex_lock(&dev
->mutex
);
497 s
= comedi_write_subdevice(dev
, minor
);
498 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
499 err
= resize_async_buffer(dev
, s
, s
->async
, size
);
502 mutex_unlock(&dev
->mutex
);
505 return err
? err
: count
;
507 static DEVICE_ATTR_RW(write_buffer_kb
);
509 static struct attribute
*comedi_dev_attrs
[] = {
510 &dev_attr_max_read_buffer_kb
.attr
,
511 &dev_attr_read_buffer_kb
.attr
,
512 &dev_attr_max_write_buffer_kb
.attr
,
513 &dev_attr_write_buffer_kb
.attr
,
516 ATTRIBUTE_GROUPS(comedi_dev
);
518 static void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
519 unsigned mask
, unsigned bits
)
523 spin_lock_irqsave(&s
->spin_lock
, flags
);
524 s
->runflags
&= ~mask
;
525 s
->runflags
|= (bits
& mask
);
526 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
529 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
534 spin_lock_irqsave(&s
->spin_lock
, flags
);
535 runflags
= s
->runflags
;
536 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
540 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
)
542 unsigned runflags
= comedi_get_subdevice_runflags(s
);
544 return (runflags
& SRF_RUNNING
) ? true : false;
546 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running
);
548 static bool comedi_is_subdevice_in_error(struct comedi_subdevice
*s
)
550 unsigned runflags
= comedi_get_subdevice_runflags(s
);
552 return (runflags
& SRF_ERROR
) ? true : false;
555 static bool comedi_is_subdevice_idle(struct comedi_subdevice
*s
)
557 unsigned runflags
= comedi_get_subdevice_runflags(s
);
559 return (runflags
& (SRF_ERROR
| SRF_RUNNING
)) ? false : true;
563 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
564 * @s: comedi_subdevice struct
565 * @size: size of the memory to allocate
567 * This also sets the subdevice runflags to allow the core to automatically
568 * free the private data during the detach.
570 void *comedi_alloc_spriv(struct comedi_subdevice
*s
, size_t size
)
572 s
->private = kzalloc(size
, GFP_KERNEL
);
574 s
->runflags
|= SRF_FREE_SPRIV
;
577 EXPORT_SYMBOL_GPL(comedi_alloc_spriv
);
580 This function restores a subdevice to an idle state.
582 static void do_become_nonbusy(struct comedi_device
*dev
,
583 struct comedi_subdevice
*s
)
585 struct comedi_async
*async
= s
->async
;
587 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
589 comedi_buf_reset(async
);
590 async
->inttrig
= NULL
;
591 kfree(async
->cmd
.chanlist
);
592 async
->cmd
.chanlist
= NULL
;
594 wake_up_interruptible_all(&s
->async
->wait_head
);
596 dev_err(dev
->class_dev
,
597 "BUG: (?) do_become_nonbusy called with async=NULL\n");
602 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
606 if (comedi_is_subdevice_running(s
) && s
->cancel
)
607 ret
= s
->cancel(dev
, s
);
609 do_become_nonbusy(dev
, s
);
614 void comedi_device_cancel_all(struct comedi_device
*dev
)
616 struct comedi_subdevice
*s
;
622 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
623 s
= &dev
->subdevices
[i
];
629 static int is_device_busy(struct comedi_device
*dev
)
631 struct comedi_subdevice
*s
;
637 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
638 s
= &dev
->subdevices
[i
];
641 if (s
->async
&& comedi_buf_is_mmapped(s
->async
))
653 pointer to devconfig structure
656 devconfig structure at arg
661 static int do_devconfig_ioctl(struct comedi_device
*dev
,
662 struct comedi_devconfig __user
*arg
)
664 struct comedi_devconfig it
;
666 if (!capable(CAP_SYS_ADMIN
))
670 if (is_device_busy(dev
))
673 struct module
*driver_module
= dev
->driver
->module
;
674 comedi_device_detach(dev
);
675 module_put(driver_module
);
680 if (copy_from_user(&it
, arg
, sizeof(it
)))
683 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
685 if (it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
686 dev_warn(dev
->class_dev
,
687 "comedi_config --init_data is deprecated\n");
691 if (dev
->minor
>= comedi_num_legacy_minors
)
692 /* don't re-use dynamically allocated comedi devices */
695 /* This increments the driver module count on success. */
696 return comedi_device_attach(dev
, &it
);
701 buffer configuration ioctl
704 pointer to bufconfig structure
710 modified bufconfig at arg
713 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
714 struct comedi_bufconfig __user
*arg
)
716 struct comedi_bufconfig bc
;
717 struct comedi_async
*async
;
718 struct comedi_subdevice
*s
;
721 if (copy_from_user(&bc
, arg
, sizeof(bc
)))
724 if (bc
.subdevice
>= dev
->n_subdevices
)
727 s
= &dev
->subdevices
[bc
.subdevice
];
731 dev_dbg(dev
->class_dev
,
732 "subdevice does not have async capability\n");
738 if (bc
.maximum_size
) {
739 if (!capable(CAP_SYS_ADMIN
))
742 async
->max_bufsize
= bc
.maximum_size
;
746 retval
= resize_async_buffer(dev
, s
, async
, bc
.size
);
751 bc
.size
= async
->prealloc_bufsz
;
752 bc
.maximum_size
= async
->max_bufsize
;
755 if (copy_to_user(arg
, &bc
, sizeof(bc
)))
766 pointer to devinfo structure
775 static int do_devinfo_ioctl(struct comedi_device
*dev
,
776 struct comedi_devinfo __user
*arg
,
779 const unsigned minor
= iminor(file_inode(file
));
780 struct comedi_subdevice
*s
;
781 struct comedi_devinfo devinfo
;
783 memset(&devinfo
, 0, sizeof(devinfo
));
785 /* fill devinfo structure */
786 devinfo
.version_code
= COMEDI_VERSION_CODE
;
787 devinfo
.n_subdevs
= dev
->n_subdevices
;
788 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
789 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
791 s
= comedi_read_subdevice(dev
, minor
);
793 devinfo
.read_subdevice
= s
->index
;
795 devinfo
.read_subdevice
= -1;
797 s
= comedi_write_subdevice(dev
, minor
);
799 devinfo
.write_subdevice
= s
->index
;
801 devinfo
.write_subdevice
= -1;
803 if (copy_to_user(arg
, &devinfo
, sizeof(devinfo
)))
814 pointer to array of subdevice info structures
820 array of subdevice info structures at arg
823 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
824 struct comedi_subdinfo __user
*arg
, void *file
)
827 struct comedi_subdinfo
*tmp
, *us
;
828 struct comedi_subdevice
*s
;
830 tmp
= kcalloc(dev
->n_subdevices
, sizeof(*tmp
), GFP_KERNEL
);
834 /* fill subdinfo structs */
835 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
836 s
= &dev
->subdevices
[i
];
840 us
->n_chan
= s
->n_chan
;
841 us
->subd_flags
= s
->subdev_flags
;
842 if (comedi_is_subdevice_running(s
))
843 us
->subd_flags
|= SDF_RUNNING
;
844 #define TIMER_nanosec 5 /* backwards compatibility */
845 us
->timer_type
= TIMER_nanosec
;
846 us
->len_chanlist
= s
->len_chanlist
;
847 us
->maxdata
= s
->maxdata
;
848 if (s
->range_table
) {
850 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
852 us
->range_type
= 0; /* XXX */
856 us
->subd_flags
|= SDF_BUSY
;
858 us
->subd_flags
|= SDF_BUSY_OWNER
;
860 us
->subd_flags
|= SDF_LOCKED
;
862 us
->subd_flags
|= SDF_LOCK_OWNER
;
863 if (!s
->maxdata
&& s
->maxdata_list
)
864 us
->subd_flags
|= SDF_MAXDATA
;
865 if (s
->range_table_list
)
866 us
->subd_flags
|= SDF_RANGETYPE
;
868 us
->subd_flags
|= SDF_CMD
;
870 if (s
->insn_bits
!= &insn_inval
)
871 us
->insn_bits_support
= COMEDI_SUPPORTED
;
873 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
876 ret
= copy_to_user(arg
, tmp
, dev
->n_subdevices
* sizeof(*tmp
));
880 return ret
? -EFAULT
: 0;
888 pointer to chaninfo structure
891 chaninfo structure at arg
894 arrays at elements of chaninfo structure
897 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
898 struct comedi_chaninfo __user
*arg
)
900 struct comedi_subdevice
*s
;
901 struct comedi_chaninfo it
;
903 if (copy_from_user(&it
, arg
, sizeof(it
)))
906 if (it
.subdev
>= dev
->n_subdevices
)
908 s
= &dev
->subdevices
[it
.subdev
];
910 if (it
.maxdata_list
) {
911 if (s
->maxdata
|| !s
->maxdata_list
)
913 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
914 s
->n_chan
* sizeof(unsigned int)))
919 return -EINVAL
; /* flaglist not supported */
924 if (!s
->range_table_list
)
926 for (i
= 0; i
< s
->n_chan
; i
++) {
929 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
930 (s
->range_table_list
[i
]->length
);
931 if (put_user(x
, it
.rangelist
+ i
))
935 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
936 s
->n_chan
* sizeof(unsigned int)))
946 buffer information ioctl
949 pointer to bufinfo structure
955 modified bufinfo at arg
958 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
959 struct comedi_bufinfo __user
*arg
, void *file
)
961 struct comedi_bufinfo bi
;
962 struct comedi_subdevice
*s
;
963 struct comedi_async
*async
;
965 if (copy_from_user(&bi
, arg
, sizeof(bi
)))
968 if (bi
.subdevice
>= dev
->n_subdevices
)
971 s
= &dev
->subdevices
[bi
.subdevice
];
973 if (s
->lock
&& s
->lock
!= file
)
979 dev_dbg(dev
->class_dev
,
980 "subdevice does not have async capability\n");
981 bi
.buf_write_ptr
= 0;
983 bi
.buf_write_count
= 0;
984 bi
.buf_read_count
= 0;
986 bi
.bytes_written
= 0;
991 bi
.bytes_written
= 0;
992 goto copyback_position
;
997 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
998 bi
.bytes_read
= comedi_buf_read_alloc(async
, bi
.bytes_read
);
999 comedi_buf_read_free(async
, bi
.bytes_read
);
1001 if (comedi_is_subdevice_idle(s
) &&
1002 async
->buf_write_count
== async
->buf_read_count
) {
1003 do_become_nonbusy(dev
, s
);
1007 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
1009 comedi_buf_write_alloc(async
, bi
.bytes_written
);
1010 comedi_buf_write_free(async
, bi
.bytes_written
);
1014 bi
.buf_write_count
= async
->buf_write_count
;
1015 bi
.buf_write_ptr
= async
->buf_write_ptr
;
1016 bi
.buf_read_count
= async
->buf_read_count
;
1017 bi
.buf_read_ptr
= async
->buf_read_ptr
;
1020 if (copy_to_user(arg
, &bi
, sizeof(bi
)))
1026 static int check_insn_config_length(struct comedi_insn
*insn
,
1033 case INSN_CONFIG_DIO_OUTPUT
:
1034 case INSN_CONFIG_DIO_INPUT
:
1035 case INSN_CONFIG_DISARM
:
1036 case INSN_CONFIG_RESET
:
1040 case INSN_CONFIG_ARM
:
1041 case INSN_CONFIG_DIO_QUERY
:
1042 case INSN_CONFIG_BLOCK_SIZE
:
1043 case INSN_CONFIG_FILTER
:
1044 case INSN_CONFIG_SERIAL_CLOCK
:
1045 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
1046 case INSN_CONFIG_ALT_SOURCE
:
1047 case INSN_CONFIG_SET_COUNTER_MODE
:
1048 case INSN_CONFIG_8254_READ_STATUS
:
1049 case INSN_CONFIG_SET_ROUTING
:
1050 case INSN_CONFIG_GET_ROUTING
:
1051 case INSN_CONFIG_GET_PWM_STATUS
:
1052 case INSN_CONFIG_PWM_SET_PERIOD
:
1053 case INSN_CONFIG_PWM_GET_PERIOD
:
1057 case INSN_CONFIG_SET_GATE_SRC
:
1058 case INSN_CONFIG_GET_GATE_SRC
:
1059 case INSN_CONFIG_SET_CLOCK_SRC
:
1060 case INSN_CONFIG_GET_CLOCK_SRC
:
1061 case INSN_CONFIG_SET_OTHER_SRC
:
1062 case INSN_CONFIG_GET_COUNTER_STATUS
:
1063 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
1064 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
1065 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
1069 case INSN_CONFIG_PWM_OUTPUT
:
1070 case INSN_CONFIG_ANALOG_TRIG
:
1074 case INSN_CONFIG_DIGITAL_TRIG
:
1078 /* by default we allow the insn since we don't have checks for
1079 * all possible cases yet */
1081 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1083 pr_warn("comedi: Add a check to %s in %s.\n",
1084 __func__
, __FILE__
);
1085 pr_warn("comedi: Assuming n=%i is correct.\n", insn
->n
);
1091 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
1092 unsigned int *data
, void *file
)
1094 struct comedi_subdevice
*s
;
1098 if (insn
->insn
& INSN_MASK_SPECIAL
) {
1099 /* a non-subdevice instruction */
1101 switch (insn
->insn
) {
1111 do_gettimeofday(&tv
);
1112 data
[0] = tv
.tv_sec
;
1113 data
[1] = tv
.tv_usec
;
1119 if (insn
->n
!= 1 || data
[0] >= 100000) {
1123 udelay(data
[0] / 1000);
1131 if (insn
->subdev
>= dev
->n_subdevices
) {
1132 dev_dbg(dev
->class_dev
,
1133 "%d not usable subdevice\n",
1138 s
= &dev
->subdevices
[insn
->subdev
];
1140 dev_dbg(dev
->class_dev
, "no async\n");
1144 if (!s
->async
->inttrig
) {
1145 dev_dbg(dev
->class_dev
, "no inttrig\n");
1149 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
1154 dev_dbg(dev
->class_dev
, "invalid insn\n");
1159 /* a subdevice instruction */
1160 unsigned int maxdata
;
1162 if (insn
->subdev
>= dev
->n_subdevices
) {
1163 dev_dbg(dev
->class_dev
, "subdevice %d out of range\n",
1168 s
= &dev
->subdevices
[insn
->subdev
];
1170 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1171 dev_dbg(dev
->class_dev
, "%d not usable subdevice\n",
1177 /* are we locked? (ioctl lock) */
1178 if (s
->lock
&& s
->lock
!= file
) {
1179 dev_dbg(dev
->class_dev
, "device locked\n");
1184 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
1187 dev_dbg(dev
->class_dev
, "bad chanspec\n");
1195 /* This looks arbitrary. It is. */
1196 s
->busy
= &parse_insn
;
1197 switch (insn
->insn
) {
1199 ret
= s
->insn_read(dev
, s
, insn
, data
);
1202 maxdata
= s
->maxdata_list
1203 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1205 for (i
= 0; i
< insn
->n
; ++i
) {
1206 if (data
[i
] > maxdata
) {
1208 dev_dbg(dev
->class_dev
,
1209 "bad data value(s)\n");
1214 ret
= s
->insn_write(dev
, s
, insn
, data
);
1220 /* Most drivers ignore the base channel in
1221 * insn->chanspec. Fix this here if
1222 * the subdevice has <= 32 channels. */
1224 unsigned int orig_mask
;
1226 orig_mask
= data
[0];
1227 if (s
->n_chan
<= 32) {
1228 shift
= CR_CHAN(insn
->chanspec
);
1236 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1237 data
[0] = orig_mask
;
1243 ret
= check_insn_config_length(insn
, data
);
1246 ret
= s
->insn_config(dev
, s
, insn
, data
);
1262 * synchronous instructions
1265 * pointer to sync cmd structure
1268 * sync cmd struct at arg
1275 /* arbitrary limits */
1276 #define MAX_SAMPLES 256
1277 static int do_insnlist_ioctl(struct comedi_device
*dev
,
1278 struct comedi_insnlist __user
*arg
, void *file
)
1280 struct comedi_insnlist insnlist
;
1281 struct comedi_insn
*insns
= NULL
;
1282 unsigned int *data
= NULL
;
1286 if (copy_from_user(&insnlist
, arg
, sizeof(insnlist
)))
1289 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1295 insns
= kcalloc(insnlist
.n_insns
, sizeof(*insns
), GFP_KERNEL
);
1301 if (copy_from_user(insns
, insnlist
.insns
,
1302 sizeof(*insns
) * insnlist
.n_insns
)) {
1303 dev_dbg(dev
->class_dev
, "copy_from_user failed\n");
1308 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
1309 if (insns
[i
].n
> MAX_SAMPLES
) {
1310 dev_dbg(dev
->class_dev
,
1311 "number of samples too large\n");
1315 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
1316 if (copy_from_user(data
, insns
[i
].data
,
1317 insns
[i
].n
* sizeof(unsigned int))) {
1318 dev_dbg(dev
->class_dev
,
1319 "copy_from_user failed\n");
1324 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
1327 if (insns
[i
].insn
& INSN_MASK_READ
) {
1328 if (copy_to_user(insns
[i
].data
, data
,
1329 insns
[i
].n
* sizeof(unsigned int))) {
1330 dev_dbg(dev
->class_dev
,
1331 "copy_to_user failed\n");
1351 * synchronous instructions
1357 * struct comedi_insn struct at arg
1363 static int do_insn_ioctl(struct comedi_device
*dev
,
1364 struct comedi_insn __user
*arg
, void *file
)
1366 struct comedi_insn insn
;
1367 unsigned int *data
= NULL
;
1370 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1376 if (copy_from_user(&insn
, arg
, sizeof(insn
))) {
1381 /* This is where the behavior of insn and insnlist deviate. */
1382 if (insn
.n
> MAX_SAMPLES
)
1383 insn
.n
= MAX_SAMPLES
;
1384 if (insn
.insn
& INSN_MASK_WRITE
) {
1385 if (copy_from_user(data
,
1387 insn
.n
* sizeof(unsigned int))) {
1392 ret
= parse_insn(dev
, &insn
, data
, file
);
1395 if (insn
.insn
& INSN_MASK_READ
) {
1396 if (copy_to_user(insn
.data
,
1398 insn
.n
* sizeof(unsigned int))) {
1411 static int do_cmd_ioctl(struct comedi_device
*dev
,
1412 struct comedi_cmd __user
*arg
, void *file
)
1414 struct comedi_cmd cmd
;
1415 struct comedi_subdevice
*s
;
1416 struct comedi_async
*async
;
1418 unsigned int __user
*user_chanlist
;
1420 if (copy_from_user(&cmd
, arg
, sizeof(cmd
))) {
1421 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1424 /* save user's chanlist pointer so it can be restored later */
1425 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1427 if (cmd
.subdev
>= dev
->n_subdevices
) {
1428 dev_dbg(dev
->class_dev
, "%d no such subdevice\n", cmd
.subdev
);
1432 s
= &dev
->subdevices
[cmd
.subdev
];
1435 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1436 dev_dbg(dev
->class_dev
, "%d not valid subdevice\n", cmd
.subdev
);
1440 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1441 dev_dbg(dev
->class_dev
,
1442 "subdevice %i does not support commands\n", cmd
.subdev
);
1446 /* are we locked? (ioctl lock) */
1447 if (s
->lock
&& s
->lock
!= file
) {
1448 dev_dbg(dev
->class_dev
, "subdevice locked\n");
1454 dev_dbg(dev
->class_dev
, "subdevice busy\n");
1458 /* make sure channel/gain list isn't too long */
1459 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1460 dev_dbg(dev
->class_dev
, "channel/gain list too long %u > %d\n",
1461 cmd
.chanlist_len
, s
->len_chanlist
);
1465 /* make sure channel/gain list isn't too short */
1466 if (cmd
.chanlist_len
< 1) {
1467 dev_dbg(dev
->class_dev
, "channel/gain list too short %u < 1\n",
1473 async
->cmd
.data
= NULL
;
1474 /* load channel/gain list */
1475 async
->cmd
.chanlist
= memdup_user(user_chanlist
,
1476 async
->cmd
.chanlist_len
* sizeof(int));
1477 if (IS_ERR(async
->cmd
.chanlist
)) {
1478 ret
= PTR_ERR(async
->cmd
.chanlist
);
1479 dev_dbg(dev
->class_dev
, "memdup_user failed with code %d\n",
1484 /* make sure each element in channel/gain list is valid */
1485 ret
= comedi_check_chanlist(s
,
1486 async
->cmd
.chanlist_len
,
1487 async
->cmd
.chanlist
);
1489 dev_dbg(dev
->class_dev
, "bad chanlist\n");
1493 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1495 if (async
->cmd
.flags
& TRIG_BOGUS
|| ret
) {
1496 dev_dbg(dev
->class_dev
, "test returned %d\n", ret
);
1498 /* restore chanlist pointer before copying back */
1499 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1501 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1502 dev_dbg(dev
->class_dev
, "fault writing cmd\n");
1510 if (!async
->prealloc_bufsz
) {
1512 dev_dbg(dev
->class_dev
, "no buffer (?)\n");
1516 comedi_buf_reset(async
);
1519 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1521 if (async
->cmd
.flags
& TRIG_WAKE_EOS
)
1522 async
->cb_mask
|= COMEDI_CB_EOS
;
1524 comedi_set_subdevice_runflags(s
, SRF_ERROR
| SRF_RUNNING
, SRF_RUNNING
);
1526 /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
1527 * comedi_read() or comedi_write() */
1529 ret
= s
->do_cmd(dev
, s
);
1534 do_become_nonbusy(dev
, s
);
1541 command testing ioctl
1544 pointer to cmd structure
1547 cmd structure at arg
1551 modified cmd structure at arg
1554 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1555 struct comedi_cmd __user
*arg
, void *file
)
1557 struct comedi_cmd cmd
;
1558 struct comedi_subdevice
*s
;
1560 unsigned int *chanlist
= NULL
;
1561 unsigned int __user
*user_chanlist
;
1563 if (copy_from_user(&cmd
, arg
, sizeof(cmd
))) {
1564 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1567 /* save user's chanlist pointer so it can be restored later */
1568 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1570 if (cmd
.subdev
>= dev
->n_subdevices
) {
1571 dev_dbg(dev
->class_dev
, "%d no such subdevice\n", cmd
.subdev
);
1575 s
= &dev
->subdevices
[cmd
.subdev
];
1576 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1577 dev_dbg(dev
->class_dev
, "%d not valid subdevice\n", cmd
.subdev
);
1581 if (!s
->do_cmd
|| !s
->do_cmdtest
) {
1582 dev_dbg(dev
->class_dev
,
1583 "subdevice %i does not support commands\n", cmd
.subdev
);
1587 /* make sure channel/gain list isn't too long */
1588 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1589 dev_dbg(dev
->class_dev
, "channel/gain list too long %d > %d\n",
1590 cmd
.chanlist_len
, s
->len_chanlist
);
1595 /* load channel/gain list */
1597 chanlist
= memdup_user(user_chanlist
,
1598 cmd
.chanlist_len
* sizeof(int));
1599 if (IS_ERR(chanlist
)) {
1600 ret
= PTR_ERR(chanlist
);
1601 dev_dbg(dev
->class_dev
,
1602 "memdup_user exited with code %d", ret
);
1606 /* make sure each element in channel/gain list is valid */
1607 ret
= comedi_check_chanlist(s
, cmd
.chanlist_len
, chanlist
);
1609 dev_dbg(dev
->class_dev
, "bad chanlist\n");
1613 cmd
.chanlist
= chanlist
;
1616 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1618 /* restore chanlist pointer before copying back */
1619 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1621 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1622 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1647 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1651 unsigned long flags
;
1652 struct comedi_subdevice
*s
;
1654 if (arg
>= dev
->n_subdevices
)
1656 s
= &dev
->subdevices
[arg
];
1658 spin_lock_irqsave(&s
->spin_lock
, flags
);
1659 if (s
->busy
|| s
->lock
)
1663 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1670 ret
= s
->lock_f(dev
, s
);
1689 This function isn't protected by the semaphore, since
1690 we already own the lock.
1692 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1695 struct comedi_subdevice
*s
;
1697 if (arg
>= dev
->n_subdevices
)
1699 s
= &dev
->subdevices
[arg
];
1704 if (s
->lock
&& s
->lock
!= file
)
1707 if (s
->lock
== file
) {
1721 cancel acquisition ioctl
1733 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1736 struct comedi_subdevice
*s
;
1739 if (arg
>= dev
->n_subdevices
)
1741 s
= &dev
->subdevices
[arg
];
1742 if (s
->async
== NULL
)
1745 if (s
->lock
&& s
->lock
!= file
)
1751 if (s
->busy
!= file
)
1754 ret
= do_cancel(dev
, s
);
1761 instructs driver to synchronize buffers
1773 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1776 struct comedi_subdevice
*s
;
1778 if (arg
>= dev
->n_subdevices
)
1780 s
= &dev
->subdevices
[arg
];
1782 if (s
->lock
&& s
->lock
!= file
)
1788 if (s
->busy
!= file
)
1792 return s
->poll(dev
, s
);
1797 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
1800 const unsigned minor
= iminor(file_inode(file
));
1801 struct comedi_device
*dev
= file
->private_data
;
1804 mutex_lock(&dev
->mutex
);
1806 /* Device config is special, because it must work on
1807 * an unconfigured device. */
1808 if (cmd
== COMEDI_DEVCONFIG
) {
1809 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
1810 /* Device config not appropriate on non-board minors. */
1814 rc
= do_devconfig_ioctl(dev
,
1815 (struct comedi_devconfig __user
*)arg
);
1818 dev
->minor
>= comedi_num_legacy_minors
) {
1819 /* Successfully unconfigured a dynamically
1820 * allocated device. Try and remove it. */
1821 if (comedi_clear_board_dev(dev
)) {
1822 mutex_unlock(&dev
->mutex
);
1823 comedi_free_board_dev(dev
);
1831 if (!dev
->attached
) {
1832 dev_dbg(dev
->class_dev
, "no driver attached\n");
1838 case COMEDI_BUFCONFIG
:
1839 rc
= do_bufconfig_ioctl(dev
,
1840 (struct comedi_bufconfig __user
*)arg
);
1842 case COMEDI_DEVINFO
:
1843 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
1846 case COMEDI_SUBDINFO
:
1847 rc
= do_subdinfo_ioctl(dev
,
1848 (struct comedi_subdinfo __user
*)arg
,
1851 case COMEDI_CHANINFO
:
1852 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
1854 case COMEDI_RANGEINFO
:
1855 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
1857 case COMEDI_BUFINFO
:
1858 rc
= do_bufinfo_ioctl(dev
,
1859 (struct comedi_bufinfo __user
*)arg
,
1863 rc
= do_lock_ioctl(dev
, arg
, file
);
1866 rc
= do_unlock_ioctl(dev
, arg
, file
);
1869 rc
= do_cancel_ioctl(dev
, arg
, file
);
1872 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
1874 case COMEDI_CMDTEST
:
1875 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
1878 case COMEDI_INSNLIST
:
1879 rc
= do_insnlist_ioctl(dev
,
1880 (struct comedi_insnlist __user
*)arg
,
1884 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
1888 rc
= do_poll_ioctl(dev
, arg
, file
);
1896 mutex_unlock(&dev
->mutex
);
1900 static void comedi_vm_open(struct vm_area_struct
*area
)
1902 struct comedi_buf_map
*bm
;
1904 bm
= area
->vm_private_data
;
1905 comedi_buf_map_get(bm
);
1908 static void comedi_vm_close(struct vm_area_struct
*area
)
1910 struct comedi_buf_map
*bm
;
1912 bm
= area
->vm_private_data
;
1913 comedi_buf_map_put(bm
);
1916 static struct vm_operations_struct comedi_vm_ops
= {
1917 .open
= comedi_vm_open
,
1918 .close
= comedi_vm_close
,
1921 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1923 const unsigned minor
= iminor(file_inode(file
));
1924 struct comedi_device
*dev
= file
->private_data
;
1925 struct comedi_subdevice
*s
;
1926 struct comedi_async
*async
;
1927 struct comedi_buf_map
*bm
;
1928 unsigned long start
= vma
->vm_start
;
1934 mutex_lock(&dev
->mutex
);
1936 if (!dev
->attached
) {
1937 dev_dbg(dev
->class_dev
, "no driver attached\n");
1942 if (vma
->vm_flags
& VM_WRITE
)
1943 s
= comedi_write_subdevice(dev
, minor
);
1945 s
= comedi_read_subdevice(dev
, minor
);
1957 if (vma
->vm_pgoff
!= 0) {
1958 dev_dbg(dev
->class_dev
, "mmap() offset must be 0.\n");
1963 size
= vma
->vm_end
- vma
->vm_start
;
1964 if (size
> async
->prealloc_bufsz
) {
1968 if (size
& (~PAGE_MASK
)) {
1973 n_pages
= size
>> PAGE_SHIFT
;
1974 bm
= async
->buf_map
;
1975 if (!bm
|| n_pages
> bm
->n_pages
) {
1979 for (i
= 0; i
< n_pages
; ++i
) {
1980 struct comedi_buf_page
*buf
= &bm
->page_list
[i
];
1982 if (remap_pfn_range(vma
, start
,
1983 page_to_pfn(virt_to_page(buf
->virt_addr
)),
1984 PAGE_SIZE
, PAGE_SHARED
)) {
1991 vma
->vm_ops
= &comedi_vm_ops
;
1992 vma
->vm_private_data
= bm
;
1994 vma
->vm_ops
->open(vma
);
1998 mutex_unlock(&dev
->mutex
);
2002 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
2004 unsigned int mask
= 0;
2005 const unsigned minor
= iminor(file_inode(file
));
2006 struct comedi_device
*dev
= file
->private_data
;
2007 struct comedi_subdevice
*s
;
2009 mutex_lock(&dev
->mutex
);
2011 if (!dev
->attached
) {
2012 dev_dbg(dev
->class_dev
, "no driver attached\n");
2016 s
= comedi_read_subdevice(dev
, minor
);
2017 if (s
&& s
->async
) {
2018 poll_wait(file
, &s
->async
->wait_head
, wait
);
2019 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
2020 comedi_buf_read_n_available(s
->async
) > 0)
2021 mask
|= POLLIN
| POLLRDNORM
;
2024 s
= comedi_write_subdevice(dev
, minor
);
2025 if (s
&& s
->async
) {
2026 unsigned int bps
= bytes_per_sample(s
->async
->subdevice
);
2028 poll_wait(file
, &s
->async
->wait_head
, wait
);
2029 comedi_buf_write_alloc(s
->async
, s
->async
->prealloc_bufsz
);
2030 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
2031 comedi_buf_write_n_allocated(s
->async
) >= bps
)
2032 mask
|= POLLOUT
| POLLWRNORM
;
2036 mutex_unlock(&dev
->mutex
);
2040 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
2041 size_t nbytes
, loff_t
*offset
)
2043 struct comedi_subdevice
*s
;
2044 struct comedi_async
*async
;
2045 int n
, m
, count
= 0, retval
= 0;
2046 DECLARE_WAITQUEUE(wait
, current
);
2047 const unsigned minor
= iminor(file_inode(file
));
2048 struct comedi_device
*dev
= file
->private_data
;
2049 bool on_wait_queue
= false;
2051 unsigned int old_detach_count
;
2053 /* Protect against device detachment during operation. */
2054 down_read(&dev
->attach_lock
);
2055 attach_locked
= true;
2056 old_detach_count
= dev
->detach_count
;
2058 if (!dev
->attached
) {
2059 dev_dbg(dev
->class_dev
, "no driver attached\n");
2064 s
= comedi_write_subdevice(dev
, minor
);
2065 if (!s
|| !s
->async
) {
2072 if (!s
->busy
|| !nbytes
)
2074 if (s
->busy
!= file
) {
2079 add_wait_queue(&async
->wait_head
, &wait
);
2080 on_wait_queue
= true;
2081 while (nbytes
> 0 && !retval
) {
2082 set_current_state(TASK_INTERRUPTIBLE
);
2084 if (!comedi_is_subdevice_running(s
)) {
2086 struct comedi_subdevice
*new_s
;
2088 if (comedi_is_subdevice_in_error(s
))
2093 * To avoid deadlock, cannot acquire dev->mutex
2094 * while dev->attach_lock is held. Need to
2095 * remove task from the async wait queue before
2096 * releasing dev->attach_lock, as it might not
2097 * be valid afterwards.
2099 remove_wait_queue(&async
->wait_head
, &wait
);
2100 on_wait_queue
= false;
2101 up_read(&dev
->attach_lock
);
2102 attach_locked
= false;
2103 mutex_lock(&dev
->mutex
);
2105 * Become non-busy unless things have changed
2106 * behind our back. Checking dev->detach_count
2107 * is unchanged ought to be sufficient (unless
2108 * there have been 2**32 detaches in the
2109 * meantime!), but check the subdevice pointer
2110 * as well just in case.
2112 new_s
= comedi_write_subdevice(dev
, minor
);
2113 if (dev
->attached
&&
2114 old_detach_count
== dev
->detach_count
&&
2115 s
== new_s
&& new_s
->async
== async
)
2116 do_become_nonbusy(dev
, s
);
2117 mutex_unlock(&dev
->mutex
);
2125 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
2126 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
2127 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
2128 if (m
> comedi_buf_write_n_allocated(async
))
2129 m
= comedi_buf_write_n_allocated(async
);
2134 if (file
->f_flags
& O_NONBLOCK
) {
2139 if (signal_pending(current
)) {
2140 retval
= -ERESTARTSYS
;
2145 if (s
->busy
!= file
) {
2152 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
2158 comedi_buf_write_free(async
, n
);
2164 break; /* makes device work like a pipe */
2168 remove_wait_queue(&async
->wait_head
, &wait
);
2169 set_current_state(TASK_RUNNING
);
2171 up_read(&dev
->attach_lock
);
2173 return count
? count
: retval
;
2176 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
2179 struct comedi_subdevice
*s
;
2180 struct comedi_async
*async
;
2181 int n
, m
, count
= 0, retval
= 0;
2182 DECLARE_WAITQUEUE(wait
, current
);
2183 const unsigned minor
= iminor(file_inode(file
));
2184 struct comedi_device
*dev
= file
->private_data
;
2185 unsigned int old_detach_count
;
2186 bool become_nonbusy
= false;
2189 /* Protect against device detachment during operation. */
2190 down_read(&dev
->attach_lock
);
2191 attach_locked
= true;
2192 old_detach_count
= dev
->detach_count
;
2194 if (!dev
->attached
) {
2195 dev_dbg(dev
->class_dev
, "no driver attached\n");
2200 s
= comedi_read_subdevice(dev
, minor
);
2201 if (!s
|| !s
->async
) {
2207 if (!s
->busy
|| !nbytes
)
2209 if (s
->busy
!= file
) {
2214 add_wait_queue(&async
->wait_head
, &wait
);
2215 while (nbytes
> 0 && !retval
) {
2216 set_current_state(TASK_INTERRUPTIBLE
);
2220 m
= comedi_buf_read_n_available(async
);
2221 /* printk("%d available\n",m); */
2222 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
2223 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
2224 /* printk("%d contiguous\n",m); */
2229 if (!comedi_is_subdevice_running(s
)) {
2230 if (comedi_is_subdevice_in_error(s
))
2234 become_nonbusy
= true;
2237 if (file
->f_flags
& O_NONBLOCK
) {
2242 if (signal_pending(current
)) {
2243 retval
= -ERESTARTSYS
;
2250 if (s
->busy
!= file
) {
2256 m
= copy_to_user(buf
, async
->prealloc_buf
+
2257 async
->buf_read_ptr
, n
);
2263 comedi_buf_read_alloc(async
, n
);
2264 comedi_buf_read_free(async
, n
);
2270 break; /* makes device work like a pipe */
2272 remove_wait_queue(&async
->wait_head
, &wait
);
2273 set_current_state(TASK_RUNNING
);
2274 if (become_nonbusy
|| comedi_is_subdevice_idle(s
)) {
2275 struct comedi_subdevice
*new_s
;
2278 * To avoid deadlock, cannot acquire dev->mutex
2279 * while dev->attach_lock is held.
2281 up_read(&dev
->attach_lock
);
2282 attach_locked
= false;
2283 mutex_lock(&dev
->mutex
);
2285 * Check device hasn't become detached behind our back.
2286 * Checking dev->detach_count is unchanged ought to be
2287 * sufficient (unless there have been 2**32 detaches in the
2288 * meantime!), but check the subdevice pointer as well just in
2291 new_s
= comedi_read_subdevice(dev
, minor
);
2292 if (dev
->attached
&& old_detach_count
== dev
->detach_count
&&
2293 s
== new_s
&& new_s
->async
== async
) {
2294 if (become_nonbusy
||
2295 async
->buf_read_count
- async
->buf_write_count
== 0)
2296 do_become_nonbusy(dev
, s
);
2298 mutex_unlock(&dev
->mutex
);
2302 up_read(&dev
->attach_lock
);
2304 return count
? count
: retval
;
2307 static int comedi_open(struct inode
*inode
, struct file
*file
)
2309 const unsigned minor
= iminor(inode
);
2310 struct comedi_device
*dev
= comedi_dev_get_from_minor(minor
);
2314 pr_debug("invalid minor number\n");
2318 /* This is slightly hacky, but we want module autoloading
2320 * case: user opens device, attached -> ok
2321 * case: user opens device, unattached, !in_request_module -> autoload
2322 * case: user opens device, unattached, in_request_module -> fail
2323 * case: root opens device, attached -> ok
2324 * case: root opens device, unattached, in_request_module -> ok
2325 * (typically called from modprobe)
2326 * case: root opens device, unattached, !in_request_module -> autoload
2328 * The last could be changed to "-> ok", which would deny root
2331 mutex_lock(&dev
->mutex
);
2334 if (!capable(CAP_NET_ADMIN
) && dev
->in_request_module
) {
2335 dev_dbg(dev
->class_dev
, "in request module\n");
2339 if (capable(CAP_NET_ADMIN
) && dev
->in_request_module
)
2342 dev
->in_request_module
= true;
2345 mutex_unlock(&dev
->mutex
);
2346 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
2347 mutex_lock(&dev
->mutex
);
2350 dev
->in_request_module
= false;
2352 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
2353 dev_dbg(dev
->class_dev
, "not attached and not CAP_NET_ADMIN\n");
2358 __module_get(THIS_MODULE
);
2360 if (dev
->attached
) {
2361 if (!try_module_get(dev
->driver
->module
)) {
2362 module_put(THIS_MODULE
);
2368 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
) {
2369 rc
= dev
->open(dev
);
2371 module_put(dev
->driver
->module
);
2372 module_put(THIS_MODULE
);
2378 file
->private_data
= dev
;
2382 mutex_unlock(&dev
->mutex
);
2384 comedi_dev_put(dev
);
2388 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2390 struct comedi_device
*dev
= file
->private_data
;
2392 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2395 static int comedi_close(struct inode
*inode
, struct file
*file
)
2397 struct comedi_device
*dev
= file
->private_data
;
2398 struct comedi_subdevice
*s
= NULL
;
2401 mutex_lock(&dev
->mutex
);
2403 if (dev
->subdevices
) {
2404 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2405 s
= &dev
->subdevices
[i
];
2407 if (s
->busy
== file
)
2409 if (s
->lock
== file
)
2413 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
2416 module_put(THIS_MODULE
);
2418 module_put(dev
->driver
->module
);
2422 mutex_unlock(&dev
->mutex
);
2423 comedi_dev_put(dev
);
2428 static const struct file_operations comedi_fops
= {
2429 .owner
= THIS_MODULE
,
2430 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2431 .compat_ioctl
= comedi_compat_ioctl
,
2432 .open
= comedi_open
,
2433 .release
= comedi_close
,
2434 .read
= comedi_read
,
2435 .write
= comedi_write
,
2436 .mmap
= comedi_mmap
,
2437 .poll
= comedi_poll
,
2438 .fasync
= comedi_fasync
,
2439 .llseek
= noop_llseek
,
2442 void comedi_error(const struct comedi_device
*dev
, const char *s
)
2444 dev_err(dev
->class_dev
, "%s: %s\n", dev
->driver
->driver_name
, s
);
2446 EXPORT_SYMBOL_GPL(comedi_error
);
2448 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2450 struct comedi_async
*async
= s
->async
;
2451 unsigned runflags
= 0;
2452 unsigned runflags_mask
= 0;
2454 if (!comedi_is_subdevice_running(s
))
2458 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
2459 COMEDI_CB_OVERFLOW
)) {
2460 runflags_mask
|= SRF_RUNNING
;
2462 /* remember if an error event has occurred, so an error
2463 * can be returned the next time the user does a read() */
2464 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2465 runflags_mask
|= SRF_ERROR
;
2466 runflags
|= SRF_ERROR
;
2468 if (runflags_mask
) {
2469 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2470 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2473 if (async
->cb_mask
& s
->async
->events
) {
2474 wake_up_interruptible(&async
->wait_head
);
2475 if (s
->subdev_flags
& SDF_CMD_READ
)
2476 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2477 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2478 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2480 s
->async
->events
= 0;
2482 EXPORT_SYMBOL_GPL(comedi_event
);
2484 /* Note: the ->mutex is pre-locked on successful return */
2485 struct comedi_device
*comedi_alloc_board_minor(struct device
*hardware_device
)
2487 struct comedi_device
*dev
;
2488 struct device
*csdev
;
2491 dev
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2493 return ERR_PTR(-ENOMEM
);
2494 comedi_device_init(dev
);
2495 comedi_set_hw_dev(dev
, hardware_device
);
2496 mutex_lock(&dev
->mutex
);
2497 mutex_lock(&comedi_board_minor_table_lock
);
2498 for (i
= hardware_device
? comedi_num_legacy_minors
: 0;
2499 i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2500 if (comedi_board_minor_table
[i
] == NULL
) {
2501 comedi_board_minor_table
[i
] = dev
;
2505 mutex_unlock(&comedi_board_minor_table_lock
);
2506 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2507 mutex_unlock(&dev
->mutex
);
2508 comedi_device_cleanup(dev
);
2509 comedi_dev_put(dev
);
2510 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2511 return ERR_PTR(-EBUSY
);
2514 csdev
= device_create(comedi_class
, hardware_device
,
2515 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2517 dev
->class_dev
= csdev
;
2519 /* Note: dev->mutex needs to be unlocked by the caller. */
2523 static void comedi_free_board_minor(unsigned minor
)
2525 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2526 comedi_free_board_dev(comedi_clear_board_minor(minor
));
2529 void comedi_release_hardware_device(struct device
*hardware_device
)
2532 struct comedi_device
*dev
;
2534 for (minor
= comedi_num_legacy_minors
; minor
< COMEDI_NUM_BOARD_MINORS
;
2536 mutex_lock(&comedi_board_minor_table_lock
);
2537 dev
= comedi_board_minor_table
[minor
];
2538 if (dev
&& dev
->hw_dev
== hardware_device
) {
2539 comedi_board_minor_table
[minor
] = NULL
;
2540 mutex_unlock(&comedi_board_minor_table_lock
);
2541 comedi_free_board_dev(dev
);
2544 mutex_unlock(&comedi_board_minor_table_lock
);
2548 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2550 struct comedi_device
*dev
= s
->device
;
2551 struct device
*csdev
;
2554 mutex_lock(&comedi_subdevice_minor_table_lock
);
2555 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
) {
2556 if (comedi_subdevice_minor_table
[i
] == NULL
) {
2557 comedi_subdevice_minor_table
[i
] = s
;
2561 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2562 if (i
== COMEDI_NUM_SUBDEVICE_MINORS
) {
2563 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2566 i
+= COMEDI_NUM_BOARD_MINORS
;
2568 csdev
= device_create(comedi_class
, dev
->class_dev
,
2569 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2570 dev
->minor
, s
->index
);
2572 s
->class_dev
= csdev
;
2577 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2586 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2587 BUG_ON(s
->minor
< COMEDI_NUM_BOARD_MINORS
);
2589 i
= s
->minor
- COMEDI_NUM_BOARD_MINORS
;
2590 mutex_lock(&comedi_subdevice_minor_table_lock
);
2591 if (s
== comedi_subdevice_minor_table
[i
])
2592 comedi_subdevice_minor_table
[i
] = NULL
;
2593 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2595 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2596 s
->class_dev
= NULL
;
2600 static void comedi_cleanup_board_minors(void)
2604 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++)
2605 comedi_free_board_minor(i
);
2608 static int __init
comedi_init(void)
2613 pr_info("comedi: version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2615 if (comedi_num_legacy_minors
< 0 ||
2616 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2617 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2618 COMEDI_NUM_BOARD_MINORS
);
2622 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2623 COMEDI_NUM_MINORS
, "comedi");
2626 cdev_init(&comedi_cdev
, &comedi_fops
);
2627 comedi_cdev
.owner
= THIS_MODULE
;
2628 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2629 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2630 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2634 comedi_class
= class_create(THIS_MODULE
, "comedi");
2635 if (IS_ERR(comedi_class
)) {
2636 pr_err("comedi: failed to create class\n");
2637 cdev_del(&comedi_cdev
);
2638 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2640 return PTR_ERR(comedi_class
);
2643 comedi_class
->dev_groups
= comedi_dev_groups
;
2645 /* XXX requires /proc interface */
2648 /* create devices files for legacy/manual use */
2649 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2650 struct comedi_device
*dev
;
2651 dev
= comedi_alloc_board_minor(NULL
);
2653 comedi_cleanup_board_minors();
2654 cdev_del(&comedi_cdev
);
2655 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2657 return PTR_ERR(dev
);
2659 /* comedi_alloc_board_minor() locked the mutex */
2660 mutex_unlock(&dev
->mutex
);
2666 module_init(comedi_init
);
2668 static void __exit
comedi_cleanup(void)
2672 comedi_cleanup_board_minors();
2673 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
)
2674 BUG_ON(comedi_board_minor_table
[i
]);
2675 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
)
2676 BUG_ON(comedi_subdevice_minor_table
[i
]);
2678 class_destroy(comedi_class
);
2679 cdev_del(&comedi_cdev
);
2680 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2682 comedi_proc_cleanup();
2684 module_exit(comedi_cleanup
);
2686 MODULE_AUTHOR("http://www.comedi.org");
2687 MODULE_DESCRIPTION("Comedi core module");
2688 MODULE_LICENSE("GPL");