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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include "comedi_compat32.h"
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/fcntl.h>
28 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/kmod.h>
32 #include <linux/poll.h>
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/vmalloc.h>
37 #include "comedidev.h"
38 #include <linux/cdev.h>
39 #include <linux/stat.h>
42 #include <linux/uaccess.h>
44 #include "comedi_internal.h"
46 #define COMEDI_NUM_MINORS 0x100
47 #define COMEDI_NUM_SUBDEVICE_MINORS \
48 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
50 static int comedi_num_legacy_minors
;
51 module_param(comedi_num_legacy_minors
, int, S_IRUGO
);
52 MODULE_PARM_DESC(comedi_num_legacy_minors
,
53 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
56 unsigned int comedi_default_buf_size_kb
= CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
;
57 module_param(comedi_default_buf_size_kb
, uint
, S_IRUGO
| S_IWUSR
);
58 MODULE_PARM_DESC(comedi_default_buf_size_kb
,
59 "default asynchronous buffer size in KiB (default "
60 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
) ")");
62 unsigned int comedi_default_buf_maxsize_kb
63 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
;
64 module_param(comedi_default_buf_maxsize_kb
, uint
, S_IRUGO
| S_IWUSR
);
65 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb
,
66 "default maximum size of asynchronous buffer in KiB (default "
67 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
) ")");
69 static DEFINE_MUTEX(comedi_board_minor_table_lock
);
70 static struct comedi_device
71 *comedi_board_minor_table
[COMEDI_NUM_BOARD_MINORS
];
73 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock
);
74 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
75 static struct comedi_subdevice
76 *comedi_subdevice_minor_table
[COMEDI_NUM_SUBDEVICE_MINORS
];
78 static struct class *comedi_class
;
79 static struct cdev comedi_cdev
;
81 static void comedi_device_init(struct comedi_device
*dev
)
83 kref_init(&dev
->refcount
);
84 spin_lock_init(&dev
->spinlock
);
85 mutex_init(&dev
->mutex
);
86 init_rwsem(&dev
->attach_lock
);
90 static void comedi_dev_kref_release(struct kref
*kref
)
92 struct comedi_device
*dev
=
93 container_of(kref
, struct comedi_device
, refcount
);
95 mutex_destroy(&dev
->mutex
);
96 put_device(dev
->class_dev
);
100 int comedi_dev_put(struct comedi_device
*dev
)
103 return kref_put(&dev
->refcount
, comedi_dev_kref_release
);
106 EXPORT_SYMBOL_GPL(comedi_dev_put
);
108 static struct comedi_device
*comedi_dev_get(struct comedi_device
*dev
)
111 kref_get(&dev
->refcount
);
115 static void comedi_device_cleanup(struct comedi_device
*dev
)
117 struct module
*driver_module
= NULL
;
121 mutex_lock(&dev
->mutex
);
123 driver_module
= dev
->driver
->module
;
124 comedi_device_detach(dev
);
125 if (driver_module
&& dev
->use_count
)
126 module_put(driver_module
);
127 mutex_unlock(&dev
->mutex
);
130 static bool comedi_clear_board_dev(struct comedi_device
*dev
)
132 unsigned int i
= dev
->minor
;
133 bool cleared
= false;
135 mutex_lock(&comedi_board_minor_table_lock
);
136 if (dev
== comedi_board_minor_table
[i
]) {
137 comedi_board_minor_table
[i
] = NULL
;
140 mutex_unlock(&comedi_board_minor_table_lock
);
144 static struct comedi_device
*comedi_clear_board_minor(unsigned minor
)
146 struct comedi_device
*dev
;
148 mutex_lock(&comedi_board_minor_table_lock
);
149 dev
= comedi_board_minor_table
[minor
];
150 comedi_board_minor_table
[minor
] = NULL
;
151 mutex_unlock(&comedi_board_minor_table_lock
);
155 static void comedi_free_board_dev(struct comedi_device
*dev
)
158 comedi_device_cleanup(dev
);
159 if (dev
->class_dev
) {
160 device_destroy(comedi_class
,
161 MKDEV(COMEDI_MAJOR
, dev
->minor
));
167 static struct comedi_subdevice
168 *comedi_subdevice_from_minor(const struct comedi_device
*dev
, unsigned minor
)
170 struct comedi_subdevice
*s
;
171 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
173 BUG_ON(i
>= COMEDI_NUM_SUBDEVICE_MINORS
);
174 mutex_lock(&comedi_subdevice_minor_table_lock
);
175 s
= comedi_subdevice_minor_table
[i
];
176 if (s
&& s
->device
!= dev
)
178 mutex_unlock(&comedi_subdevice_minor_table_lock
);
182 static struct comedi_device
*comedi_dev_get_from_board_minor(unsigned minor
)
184 struct comedi_device
*dev
;
186 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
187 mutex_lock(&comedi_board_minor_table_lock
);
188 dev
= comedi_dev_get(comedi_board_minor_table
[minor
]);
189 mutex_unlock(&comedi_board_minor_table_lock
);
193 static struct comedi_device
*comedi_dev_get_from_subdevice_minor(unsigned minor
)
195 struct comedi_device
*dev
;
196 struct comedi_subdevice
*s
;
197 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
199 BUG_ON(i
>= COMEDI_NUM_SUBDEVICE_MINORS
);
200 mutex_lock(&comedi_subdevice_minor_table_lock
);
201 s
= comedi_subdevice_minor_table
[i
];
202 dev
= comedi_dev_get(s
? s
->device
: NULL
);
203 mutex_unlock(&comedi_subdevice_minor_table_lock
);
207 struct comedi_device
*comedi_dev_get_from_minor(unsigned minor
)
209 if (minor
< COMEDI_NUM_BOARD_MINORS
)
210 return comedi_dev_get_from_board_minor(minor
);
212 return comedi_dev_get_from_subdevice_minor(minor
);
214 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor
);
216 static struct comedi_subdevice
*
217 comedi_read_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(dev
, minor
);
223 if (s
== NULL
|| (s
->subdev_flags
& SDF_CMD_READ
))
226 return dev
->read_subdev
;
229 static struct comedi_subdevice
*
230 comedi_write_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
232 struct comedi_subdevice
*s
;
234 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
235 s
= comedi_subdevice_from_minor(dev
, minor
);
236 if (s
== NULL
|| (s
->subdev_flags
& SDF_CMD_WRITE
))
239 return dev
->write_subdev
;
242 static int resize_async_buffer(struct comedi_device
*dev
,
243 struct comedi_subdevice
*s
, unsigned new_size
)
245 struct comedi_async
*async
= s
->async
;
248 if (new_size
> async
->max_bufsize
)
252 dev_dbg(dev
->class_dev
,
253 "subdevice is busy, cannot resize buffer\n");
256 if (comedi_buf_is_mmapped(s
)) {
257 dev_dbg(dev
->class_dev
,
258 "subdevice is mmapped, cannot resize buffer\n");
262 /* make sure buffer is an integral number of pages
264 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
266 retval
= comedi_buf_alloc(dev
, s
, new_size
);
271 retval
= s
->buf_change(dev
, s
);
276 dev_dbg(dev
->class_dev
, "subd %d buffer resized to %i bytes\n",
277 s
->index
, async
->prealloc_bufsz
);
281 /* sysfs attribute files */
283 static ssize_t
max_read_buffer_kb_show(struct device
*csdev
,
284 struct device_attribute
*attr
, char *buf
)
286 unsigned int minor
= MINOR(csdev
->devt
);
287 struct comedi_device
*dev
;
288 struct comedi_subdevice
*s
;
289 unsigned int size
= 0;
291 dev
= comedi_dev_get_from_minor(minor
);
295 mutex_lock(&dev
->mutex
);
296 s
= comedi_read_subdevice(dev
, minor
);
297 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
298 size
= s
->async
->max_bufsize
/ 1024;
299 mutex_unlock(&dev
->mutex
);
302 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
305 static ssize_t
max_read_buffer_kb_store(struct device
*csdev
,
306 struct device_attribute
*attr
,
307 const char *buf
, size_t count
)
309 unsigned int minor
= MINOR(csdev
->devt
);
310 struct comedi_device
*dev
;
311 struct comedi_subdevice
*s
;
315 err
= kstrtouint(buf
, 10, &size
);
318 if (size
> (UINT_MAX
/ 1024))
322 dev
= comedi_dev_get_from_minor(minor
);
326 mutex_lock(&dev
->mutex
);
327 s
= comedi_read_subdevice(dev
, minor
);
328 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
329 s
->async
->max_bufsize
= size
;
332 mutex_unlock(&dev
->mutex
);
335 return err
? err
: count
;
337 static DEVICE_ATTR_RW(max_read_buffer_kb
);
339 static ssize_t
read_buffer_kb_show(struct device
*csdev
,
340 struct device_attribute
*attr
, char *buf
)
342 unsigned int minor
= MINOR(csdev
->devt
);
343 struct comedi_device
*dev
;
344 struct comedi_subdevice
*s
;
345 unsigned int size
= 0;
347 dev
= comedi_dev_get_from_minor(minor
);
351 mutex_lock(&dev
->mutex
);
352 s
= comedi_read_subdevice(dev
, minor
);
353 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
354 size
= s
->async
->prealloc_bufsz
/ 1024;
355 mutex_unlock(&dev
->mutex
);
358 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
361 static ssize_t
read_buffer_kb_store(struct device
*csdev
,
362 struct device_attribute
*attr
,
363 const char *buf
, size_t count
)
365 unsigned int minor
= MINOR(csdev
->devt
);
366 struct comedi_device
*dev
;
367 struct comedi_subdevice
*s
;
371 err
= kstrtouint(buf
, 10, &size
);
374 if (size
> (UINT_MAX
/ 1024))
378 dev
= comedi_dev_get_from_minor(minor
);
382 mutex_lock(&dev
->mutex
);
383 s
= comedi_read_subdevice(dev
, minor
);
384 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
385 err
= resize_async_buffer(dev
, s
, size
);
388 mutex_unlock(&dev
->mutex
);
391 return err
? err
: count
;
393 static DEVICE_ATTR_RW(read_buffer_kb
);
395 static ssize_t
max_write_buffer_kb_show(struct device
*csdev
,
396 struct device_attribute
*attr
,
399 unsigned int minor
= MINOR(csdev
->devt
);
400 struct comedi_device
*dev
;
401 struct comedi_subdevice
*s
;
402 unsigned int size
= 0;
404 dev
= comedi_dev_get_from_minor(minor
);
408 mutex_lock(&dev
->mutex
);
409 s
= comedi_write_subdevice(dev
, minor
);
410 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
411 size
= s
->async
->max_bufsize
/ 1024;
412 mutex_unlock(&dev
->mutex
);
415 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
418 static ssize_t
max_write_buffer_kb_store(struct device
*csdev
,
419 struct device_attribute
*attr
,
420 const char *buf
, size_t count
)
422 unsigned int minor
= MINOR(csdev
->devt
);
423 struct comedi_device
*dev
;
424 struct comedi_subdevice
*s
;
428 err
= kstrtouint(buf
, 10, &size
);
431 if (size
> (UINT_MAX
/ 1024))
435 dev
= comedi_dev_get_from_minor(minor
);
439 mutex_lock(&dev
->mutex
);
440 s
= comedi_write_subdevice(dev
, minor
);
441 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
442 s
->async
->max_bufsize
= size
;
445 mutex_unlock(&dev
->mutex
);
448 return err
? err
: count
;
450 static DEVICE_ATTR_RW(max_write_buffer_kb
);
452 static ssize_t
write_buffer_kb_show(struct device
*csdev
,
453 struct device_attribute
*attr
, char *buf
)
455 unsigned int minor
= MINOR(csdev
->devt
);
456 struct comedi_device
*dev
;
457 struct comedi_subdevice
*s
;
458 unsigned int size
= 0;
460 dev
= comedi_dev_get_from_minor(minor
);
464 mutex_lock(&dev
->mutex
);
465 s
= comedi_write_subdevice(dev
, minor
);
466 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
467 size
= s
->async
->prealloc_bufsz
/ 1024;
468 mutex_unlock(&dev
->mutex
);
471 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
474 static ssize_t
write_buffer_kb_store(struct device
*csdev
,
475 struct device_attribute
*attr
,
476 const char *buf
, size_t count
)
478 unsigned int minor
= MINOR(csdev
->devt
);
479 struct comedi_device
*dev
;
480 struct comedi_subdevice
*s
;
484 err
= kstrtouint(buf
, 10, &size
);
487 if (size
> (UINT_MAX
/ 1024))
491 dev
= comedi_dev_get_from_minor(minor
);
495 mutex_lock(&dev
->mutex
);
496 s
= comedi_write_subdevice(dev
, minor
);
497 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
498 err
= resize_async_buffer(dev
, s
, size
);
501 mutex_unlock(&dev
->mutex
);
504 return err
? err
: count
;
506 static DEVICE_ATTR_RW(write_buffer_kb
);
508 static struct attribute
*comedi_dev_attrs
[] = {
509 &dev_attr_max_read_buffer_kb
.attr
,
510 &dev_attr_read_buffer_kb
.attr
,
511 &dev_attr_max_write_buffer_kb
.attr
,
512 &dev_attr_write_buffer_kb
.attr
,
515 ATTRIBUTE_GROUPS(comedi_dev
);
517 static void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
518 unsigned mask
, unsigned bits
)
522 spin_lock_irqsave(&s
->spin_lock
, flags
);
523 s
->runflags
&= ~mask
;
524 s
->runflags
|= (bits
& mask
);
525 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
528 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
533 spin_lock_irqsave(&s
->spin_lock
, flags
);
534 runflags
= s
->runflags
;
535 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
539 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
)
541 unsigned runflags
= comedi_get_subdevice_runflags(s
);
543 return (runflags
& SRF_RUNNING
) ? true : false;
545 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running
);
547 static bool comedi_is_subdevice_in_error(struct comedi_subdevice
*s
)
549 unsigned runflags
= comedi_get_subdevice_runflags(s
);
551 return (runflags
& SRF_ERROR
) ? true : false;
554 static bool comedi_is_subdevice_idle(struct comedi_subdevice
*s
)
556 unsigned runflags
= comedi_get_subdevice_runflags(s
);
558 return (runflags
& (SRF_ERROR
| SRF_RUNNING
)) ? false : true;
562 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
563 * @s: comedi_subdevice struct
564 * @size: size of the memory to allocate
566 * This also sets the subdevice runflags to allow the core to automatically
567 * free the private data during the detach.
569 void *comedi_alloc_spriv(struct comedi_subdevice
*s
, size_t size
)
571 s
->private = kzalloc(size
, GFP_KERNEL
);
573 s
->runflags
|= SRF_FREE_SPRIV
;
576 EXPORT_SYMBOL_GPL(comedi_alloc_spriv
);
579 This function restores a subdevice to an idle state.
581 static void do_become_nonbusy(struct comedi_device
*dev
,
582 struct comedi_subdevice
*s
)
584 struct comedi_async
*async
= s
->async
;
586 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
589 async
->inttrig
= NULL
;
590 kfree(async
->cmd
.chanlist
);
591 async
->cmd
.chanlist
= NULL
;
593 wake_up_interruptible_all(&s
->async
->wait_head
);
595 dev_err(dev
->class_dev
,
596 "BUG: (?) do_become_nonbusy called with async=NULL\n");
601 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
605 if (comedi_is_subdevice_running(s
) && s
->cancel
)
606 ret
= s
->cancel(dev
, s
);
608 do_become_nonbusy(dev
, s
);
613 void comedi_device_cancel_all(struct comedi_device
*dev
)
615 struct comedi_subdevice
*s
;
621 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
622 s
= &dev
->subdevices
[i
];
628 static int is_device_busy(struct comedi_device
*dev
)
630 struct comedi_subdevice
*s
;
636 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
637 s
= &dev
->subdevices
[i
];
640 if (s
->async
&& comedi_buf_is_mmapped(s
))
652 pointer to devconfig structure
655 devconfig structure at arg
660 static int do_devconfig_ioctl(struct comedi_device
*dev
,
661 struct comedi_devconfig __user
*arg
)
663 struct comedi_devconfig it
;
665 if (!capable(CAP_SYS_ADMIN
))
669 if (is_device_busy(dev
))
672 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
, 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
];
976 dev_dbg(dev
->class_dev
,
977 "subdevice does not have async capability\n");
978 bi
.buf_write_ptr
= 0;
980 bi
.buf_write_count
= 0;
981 bi
.buf_read_count
= 0;
983 bi
.bytes_written
= 0;
988 bi
.bytes_written
= 0;
989 goto copyback_position
;
994 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
995 bi
.bytes_read
= comedi_buf_read_alloc(s
, bi
.bytes_read
);
996 comedi_buf_read_free(s
, bi
.bytes_read
);
998 if (comedi_is_subdevice_idle(s
) &&
999 comedi_buf_n_bytes_ready(s
) == 0) {
1000 do_become_nonbusy(dev
, s
);
1004 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
1006 comedi_buf_write_alloc(s
, bi
.bytes_written
);
1007 comedi_buf_write_free(s
, bi
.bytes_written
);
1011 bi
.buf_write_count
= async
->buf_write_count
;
1012 bi
.buf_write_ptr
= async
->buf_write_ptr
;
1013 bi
.buf_read_count
= async
->buf_read_count
;
1014 bi
.buf_read_ptr
= async
->buf_read_ptr
;
1017 if (copy_to_user(arg
, &bi
, sizeof(bi
)))
1023 static int check_insn_config_length(struct comedi_insn
*insn
,
1030 case INSN_CONFIG_DIO_OUTPUT
:
1031 case INSN_CONFIG_DIO_INPUT
:
1032 case INSN_CONFIG_DISARM
:
1033 case INSN_CONFIG_RESET
:
1037 case INSN_CONFIG_ARM
:
1038 case INSN_CONFIG_DIO_QUERY
:
1039 case INSN_CONFIG_BLOCK_SIZE
:
1040 case INSN_CONFIG_FILTER
:
1041 case INSN_CONFIG_SERIAL_CLOCK
:
1042 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
1043 case INSN_CONFIG_ALT_SOURCE
:
1044 case INSN_CONFIG_SET_COUNTER_MODE
:
1045 case INSN_CONFIG_8254_READ_STATUS
:
1046 case INSN_CONFIG_SET_ROUTING
:
1047 case INSN_CONFIG_GET_ROUTING
:
1048 case INSN_CONFIG_GET_PWM_STATUS
:
1049 case INSN_CONFIG_PWM_SET_PERIOD
:
1050 case INSN_CONFIG_PWM_GET_PERIOD
:
1054 case INSN_CONFIG_SET_GATE_SRC
:
1055 case INSN_CONFIG_GET_GATE_SRC
:
1056 case INSN_CONFIG_SET_CLOCK_SRC
:
1057 case INSN_CONFIG_GET_CLOCK_SRC
:
1058 case INSN_CONFIG_SET_OTHER_SRC
:
1059 case INSN_CONFIG_GET_COUNTER_STATUS
:
1060 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
1061 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
1062 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
1066 case INSN_CONFIG_PWM_OUTPUT
:
1067 case INSN_CONFIG_ANALOG_TRIG
:
1071 case INSN_CONFIG_DIGITAL_TRIG
:
1075 /* by default we allow the insn since we don't have checks for
1076 * all possible cases yet */
1078 pr_warn("No check for data length of config insn id %i is implemented\n",
1080 pr_warn("Add a check to %s in %s\n", __func__
, __FILE__
);
1081 pr_warn("Assuming n=%i is correct\n", insn
->n
);
1087 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
1088 unsigned int *data
, void *file
)
1090 struct comedi_subdevice
*s
;
1094 if (insn
->insn
& INSN_MASK_SPECIAL
) {
1095 /* a non-subdevice instruction */
1097 switch (insn
->insn
) {
1107 do_gettimeofday(&tv
);
1108 data
[0] = tv
.tv_sec
;
1109 data
[1] = tv
.tv_usec
;
1115 if (insn
->n
!= 1 || data
[0] >= 100000) {
1119 udelay(data
[0] / 1000);
1127 if (insn
->subdev
>= dev
->n_subdevices
) {
1128 dev_dbg(dev
->class_dev
,
1129 "%d not usable subdevice\n",
1134 s
= &dev
->subdevices
[insn
->subdev
];
1136 dev_dbg(dev
->class_dev
, "no async\n");
1140 if (!s
->async
->inttrig
) {
1141 dev_dbg(dev
->class_dev
, "no inttrig\n");
1145 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
1150 dev_dbg(dev
->class_dev
, "invalid insn\n");
1155 /* a subdevice instruction */
1156 unsigned int maxdata
;
1158 if (insn
->subdev
>= dev
->n_subdevices
) {
1159 dev_dbg(dev
->class_dev
, "subdevice %d out of range\n",
1164 s
= &dev
->subdevices
[insn
->subdev
];
1166 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1167 dev_dbg(dev
->class_dev
, "%d not usable subdevice\n",
1173 /* are we locked? (ioctl lock) */
1174 if (s
->lock
&& s
->lock
!= file
) {
1175 dev_dbg(dev
->class_dev
, "device locked\n");
1180 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
1183 dev_dbg(dev
->class_dev
, "bad chanspec\n");
1191 /* This looks arbitrary. It is. */
1192 s
->busy
= &parse_insn
;
1193 switch (insn
->insn
) {
1195 ret
= s
->insn_read(dev
, s
, insn
, data
);
1196 if (ret
== -ETIMEDOUT
) {
1197 dev_dbg(dev
->class_dev
,
1198 "subdevice %d read instruction timed out\n",
1203 maxdata
= s
->maxdata_list
1204 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1206 for (i
= 0; i
< insn
->n
; ++i
) {
1207 if (data
[i
] > maxdata
) {
1209 dev_dbg(dev
->class_dev
,
1210 "bad data value(s)\n");
1215 ret
= s
->insn_write(dev
, s
, insn
, data
);
1216 if (ret
== -ETIMEDOUT
) {
1217 dev_dbg(dev
->class_dev
,
1218 "subdevice %d write instruction timed out\n",
1227 /* Most drivers ignore the base channel in
1228 * insn->chanspec. Fix this here if
1229 * the subdevice has <= 32 channels. */
1230 unsigned int orig_mask
= data
[0];
1231 unsigned int shift
= 0;
1233 if (s
->n_chan
<= 32) {
1234 shift
= CR_CHAN(insn
->chanspec
);
1241 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1242 data
[0] = orig_mask
;
1248 ret
= check_insn_config_length(insn
, data
);
1251 ret
= s
->insn_config(dev
, s
, insn
, data
);
1267 * synchronous instructions
1270 * pointer to sync cmd structure
1273 * sync cmd struct at arg
1280 /* arbitrary limits */
1281 #define MAX_SAMPLES 256
1282 static int do_insnlist_ioctl(struct comedi_device
*dev
,
1283 struct comedi_insnlist __user
*arg
, void *file
)
1285 struct comedi_insnlist insnlist
;
1286 struct comedi_insn
*insns
= NULL
;
1287 unsigned int *data
= NULL
;
1291 if (copy_from_user(&insnlist
, arg
, sizeof(insnlist
)))
1294 data
= kmalloc_array(MAX_SAMPLES
, sizeof(unsigned int), GFP_KERNEL
);
1300 insns
= kcalloc(insnlist
.n_insns
, sizeof(*insns
), GFP_KERNEL
);
1306 if (copy_from_user(insns
, insnlist
.insns
,
1307 sizeof(*insns
) * insnlist
.n_insns
)) {
1308 dev_dbg(dev
->class_dev
, "copy_from_user failed\n");
1313 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
1314 if (insns
[i
].n
> MAX_SAMPLES
) {
1315 dev_dbg(dev
->class_dev
,
1316 "number of samples too large\n");
1320 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
1321 if (copy_from_user(data
, insns
[i
].data
,
1322 insns
[i
].n
* sizeof(unsigned int))) {
1323 dev_dbg(dev
->class_dev
,
1324 "copy_from_user failed\n");
1329 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
1332 if (insns
[i
].insn
& INSN_MASK_READ
) {
1333 if (copy_to_user(insns
[i
].data
, data
,
1334 insns
[i
].n
* sizeof(unsigned int))) {
1335 dev_dbg(dev
->class_dev
,
1336 "copy_to_user failed\n");
1356 * synchronous instructions
1362 * struct comedi_insn struct at arg
1368 static int do_insn_ioctl(struct comedi_device
*dev
,
1369 struct comedi_insn __user
*arg
, void *file
)
1371 struct comedi_insn insn
;
1372 unsigned int *data
= NULL
;
1375 data
= kmalloc_array(MAX_SAMPLES
, sizeof(unsigned int), GFP_KERNEL
);
1381 if (copy_from_user(&insn
, arg
, sizeof(insn
))) {
1386 /* This is where the behavior of insn and insnlist deviate. */
1387 if (insn
.n
> MAX_SAMPLES
)
1388 insn
.n
= MAX_SAMPLES
;
1389 if (insn
.insn
& INSN_MASK_WRITE
) {
1390 if (copy_from_user(data
,
1392 insn
.n
* sizeof(unsigned int))) {
1397 ret
= parse_insn(dev
, &insn
, data
, file
);
1400 if (insn
.insn
& INSN_MASK_READ
) {
1401 if (copy_to_user(insn
.data
,
1403 insn
.n
* sizeof(unsigned int))) {
1416 static int __comedi_get_user_cmd(struct comedi_device
*dev
,
1417 struct comedi_cmd __user
*arg
,
1418 struct comedi_cmd
*cmd
)
1420 struct comedi_subdevice
*s
;
1422 if (copy_from_user(cmd
, arg
, sizeof(*cmd
))) {
1423 dev_dbg(dev
->class_dev
, "bad cmd address\n");
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
];
1434 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1435 dev_dbg(dev
->class_dev
, "%d not valid subdevice\n",
1440 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1441 dev_dbg(dev
->class_dev
,
1442 "subdevice %d does not support commands\n",
1447 /* make sure channel/gain list isn't too long */
1448 if (cmd
->chanlist_len
> s
->len_chanlist
) {
1449 dev_dbg(dev
->class_dev
, "channel/gain list too long %d > %d\n",
1450 cmd
->chanlist_len
, s
->len_chanlist
);
1457 static int __comedi_get_user_chanlist(struct comedi_device
*dev
,
1458 struct comedi_subdevice
*s
,
1459 unsigned int __user
*user_chanlist
,
1460 struct comedi_cmd
*cmd
)
1462 unsigned int *chanlist
;
1465 cmd
->chanlist
= NULL
;
1466 chanlist
= memdup_user(user_chanlist
,
1467 cmd
->chanlist_len
* sizeof(unsigned int));
1468 if (IS_ERR(chanlist
))
1469 return PTR_ERR(chanlist
);
1471 /* make sure each element in channel/gain list is valid */
1472 ret
= comedi_check_chanlist(s
, cmd
->chanlist_len
, chanlist
);
1478 cmd
->chanlist
= chanlist
;
1483 static int do_cmd_ioctl(struct comedi_device
*dev
,
1484 struct comedi_cmd __user
*arg
, void *file
)
1486 struct comedi_cmd cmd
;
1487 struct comedi_subdevice
*s
;
1488 struct comedi_async
*async
;
1489 unsigned int __user
*user_chanlist
;
1492 /* get the user's cmd and do some simple validation */
1493 ret
= __comedi_get_user_cmd(dev
, arg
, &cmd
);
1497 /* save user's chanlist pointer so it can be restored later */
1498 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1500 s
= &dev
->subdevices
[cmd
.subdev
];
1503 /* are we locked? (ioctl lock) */
1504 if (s
->lock
&& s
->lock
!= file
) {
1505 dev_dbg(dev
->class_dev
, "subdevice locked\n");
1511 dev_dbg(dev
->class_dev
, "subdevice busy\n");
1515 /* make sure channel/gain list isn't too short */
1516 if (cmd
.chanlist_len
< 1) {
1517 dev_dbg(dev
->class_dev
, "channel/gain list too short %u < 1\n",
1523 async
->cmd
.data
= NULL
;
1525 /* load channel/gain list */
1526 ret
= __comedi_get_user_chanlist(dev
, s
, user_chanlist
, &async
->cmd
);
1530 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1532 if (async
->cmd
.flags
& CMDF_BOGUS
|| ret
) {
1533 dev_dbg(dev
->class_dev
, "test returned %d\n", ret
);
1535 /* restore chanlist pointer before copying back */
1536 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1538 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1539 dev_dbg(dev
->class_dev
, "fault writing cmd\n");
1547 if (!async
->prealloc_bufsz
) {
1549 dev_dbg(dev
->class_dev
, "no buffer (?)\n");
1553 comedi_buf_reset(s
);
1556 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1558 if (async
->cmd
.flags
& CMDF_WAKE_EOS
)
1559 async
->cb_mask
|= COMEDI_CB_EOS
;
1561 comedi_set_subdevice_runflags(s
, SRF_ERROR
| SRF_RUNNING
, SRF_RUNNING
);
1563 /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
1564 * comedi_read() or comedi_write() */
1566 ret
= s
->do_cmd(dev
, s
);
1571 do_become_nonbusy(dev
, s
);
1578 command testing ioctl
1581 pointer to cmd structure
1584 cmd structure at arg
1588 modified cmd structure at arg
1591 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1592 struct comedi_cmd __user
*arg
, void *file
)
1594 struct comedi_cmd cmd
;
1595 struct comedi_subdevice
*s
;
1596 unsigned int __user
*user_chanlist
;
1599 /* get the user's cmd and do some simple validation */
1600 ret
= __comedi_get_user_cmd(dev
, arg
, &cmd
);
1604 /* save user's chanlist pointer so it can be restored later */
1605 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1607 s
= &dev
->subdevices
[cmd
.subdev
];
1609 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1610 if (user_chanlist
) {
1611 /* load channel/gain list */
1612 ret
= __comedi_get_user_chanlist(dev
, s
, user_chanlist
, &cmd
);
1617 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1619 kfree(cmd
.chanlist
); /* free kernel copy of user chanlist */
1621 /* restore chanlist pointer before copying back */
1622 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1624 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1625 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1647 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned long 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
);
1681 This function isn't protected by the semaphore, since
1682 we already own the lock.
1684 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1687 struct comedi_subdevice
*s
;
1689 if (arg
>= dev
->n_subdevices
)
1691 s
= &dev
->subdevices
[arg
];
1696 if (s
->lock
&& s
->lock
!= file
)
1699 if (s
->lock
== file
)
1707 cancel acquisition ioctl
1719 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1722 struct comedi_subdevice
*s
;
1725 if (arg
>= dev
->n_subdevices
)
1727 s
= &dev
->subdevices
[arg
];
1728 if (s
->async
== NULL
)
1734 if (s
->busy
!= file
)
1737 ret
= do_cancel(dev
, s
);
1744 instructs driver to synchronize buffers
1756 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1759 struct comedi_subdevice
*s
;
1761 if (arg
>= dev
->n_subdevices
)
1763 s
= &dev
->subdevices
[arg
];
1768 if (s
->busy
!= file
)
1772 return s
->poll(dev
, s
);
1777 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
1780 const unsigned minor
= iminor(file_inode(file
));
1781 struct comedi_device
*dev
= file
->private_data
;
1784 mutex_lock(&dev
->mutex
);
1786 /* Device config is special, because it must work on
1787 * an unconfigured device. */
1788 if (cmd
== COMEDI_DEVCONFIG
) {
1789 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
1790 /* Device config not appropriate on non-board minors. */
1794 rc
= do_devconfig_ioctl(dev
,
1795 (struct comedi_devconfig __user
*)arg
);
1798 dev
->minor
>= comedi_num_legacy_minors
) {
1799 /* Successfully unconfigured a dynamically
1800 * allocated device. Try and remove it. */
1801 if (comedi_clear_board_dev(dev
)) {
1802 mutex_unlock(&dev
->mutex
);
1803 comedi_free_board_dev(dev
);
1811 if (!dev
->attached
) {
1812 dev_dbg(dev
->class_dev
, "no driver attached\n");
1818 case COMEDI_BUFCONFIG
:
1819 rc
= do_bufconfig_ioctl(dev
,
1820 (struct comedi_bufconfig __user
*)arg
);
1822 case COMEDI_DEVINFO
:
1823 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
1826 case COMEDI_SUBDINFO
:
1827 rc
= do_subdinfo_ioctl(dev
,
1828 (struct comedi_subdinfo __user
*)arg
,
1831 case COMEDI_CHANINFO
:
1832 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
1834 case COMEDI_RANGEINFO
:
1835 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
1837 case COMEDI_BUFINFO
:
1838 rc
= do_bufinfo_ioctl(dev
,
1839 (struct comedi_bufinfo __user
*)arg
,
1843 rc
= do_lock_ioctl(dev
, arg
, file
);
1846 rc
= do_unlock_ioctl(dev
, arg
, file
);
1849 rc
= do_cancel_ioctl(dev
, arg
, file
);
1852 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
1854 case COMEDI_CMDTEST
:
1855 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
1858 case COMEDI_INSNLIST
:
1859 rc
= do_insnlist_ioctl(dev
,
1860 (struct comedi_insnlist __user
*)arg
,
1864 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
1868 rc
= do_poll_ioctl(dev
, arg
, file
);
1876 mutex_unlock(&dev
->mutex
);
1880 static void comedi_vm_open(struct vm_area_struct
*area
)
1882 struct comedi_buf_map
*bm
;
1884 bm
= area
->vm_private_data
;
1885 comedi_buf_map_get(bm
);
1888 static void comedi_vm_close(struct vm_area_struct
*area
)
1890 struct comedi_buf_map
*bm
;
1892 bm
= area
->vm_private_data
;
1893 comedi_buf_map_put(bm
);
1896 static struct vm_operations_struct comedi_vm_ops
= {
1897 .open
= comedi_vm_open
,
1898 .close
= comedi_vm_close
,
1901 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1903 const unsigned minor
= iminor(file_inode(file
));
1904 struct comedi_device
*dev
= file
->private_data
;
1905 struct comedi_subdevice
*s
;
1906 struct comedi_async
*async
;
1907 struct comedi_buf_map
*bm
= NULL
;
1908 unsigned long start
= vma
->vm_start
;
1915 * 'trylock' avoids circular dependency with current->mm->mmap_sem
1916 * and down-reading &dev->attach_lock should normally succeed without
1917 * contention unless the device is in the process of being attached
1920 if (!down_read_trylock(&dev
->attach_lock
))
1923 if (!dev
->attached
) {
1924 dev_dbg(dev
->class_dev
, "no driver attached\n");
1929 if (vma
->vm_flags
& VM_WRITE
)
1930 s
= comedi_write_subdevice(dev
, minor
);
1932 s
= comedi_read_subdevice(dev
, minor
);
1944 if (vma
->vm_pgoff
!= 0) {
1945 dev_dbg(dev
->class_dev
, "mmap() offset must be 0.\n");
1950 size
= vma
->vm_end
- vma
->vm_start
;
1951 if (size
> async
->prealloc_bufsz
) {
1955 if (size
& (~PAGE_MASK
)) {
1960 n_pages
= size
>> PAGE_SHIFT
;
1962 /* get reference to current buf map (if any) */
1963 bm
= comedi_buf_map_from_subdev_get(s
);
1964 if (!bm
|| n_pages
> bm
->n_pages
) {
1968 for (i
= 0; i
< n_pages
; ++i
) {
1969 struct comedi_buf_page
*buf
= &bm
->page_list
[i
];
1971 if (remap_pfn_range(vma
, start
,
1972 page_to_pfn(virt_to_page(buf
->virt_addr
)),
1973 PAGE_SIZE
, PAGE_SHARED
)) {
1980 vma
->vm_ops
= &comedi_vm_ops
;
1981 vma
->vm_private_data
= bm
;
1983 vma
->vm_ops
->open(vma
);
1987 up_read(&dev
->attach_lock
);
1988 comedi_buf_map_put(bm
); /* put reference to buf map - okay if NULL */
1992 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
1994 unsigned int mask
= 0;
1995 const unsigned minor
= iminor(file_inode(file
));
1996 struct comedi_device
*dev
= file
->private_data
;
1997 struct comedi_subdevice
*s
;
1999 mutex_lock(&dev
->mutex
);
2001 if (!dev
->attached
) {
2002 dev_dbg(dev
->class_dev
, "no driver attached\n");
2006 s
= comedi_read_subdevice(dev
, minor
);
2007 if (s
&& s
->async
) {
2008 poll_wait(file
, &s
->async
->wait_head
, wait
);
2009 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
2010 comedi_buf_read_n_available(s
) > 0)
2011 mask
|= POLLIN
| POLLRDNORM
;
2014 s
= comedi_write_subdevice(dev
, minor
);
2015 if (s
&& s
->async
) {
2016 unsigned int bps
= bytes_per_sample(s
);
2018 poll_wait(file
, &s
->async
->wait_head
, wait
);
2019 comedi_buf_write_alloc(s
, s
->async
->prealloc_bufsz
);
2020 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
2021 comedi_buf_write_n_allocated(s
) >= bps
)
2022 mask
|= POLLOUT
| POLLWRNORM
;
2026 mutex_unlock(&dev
->mutex
);
2030 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
2031 size_t nbytes
, loff_t
*offset
)
2033 struct comedi_subdevice
*s
;
2034 struct comedi_async
*async
;
2035 int n
, m
, count
= 0, retval
= 0;
2036 DECLARE_WAITQUEUE(wait
, current
);
2037 const unsigned minor
= iminor(file_inode(file
));
2038 struct comedi_device
*dev
= file
->private_data
;
2039 bool on_wait_queue
= false;
2041 unsigned int old_detach_count
;
2043 /* Protect against device detachment during operation. */
2044 down_read(&dev
->attach_lock
);
2045 attach_locked
= true;
2046 old_detach_count
= dev
->detach_count
;
2048 if (!dev
->attached
) {
2049 dev_dbg(dev
->class_dev
, "no driver attached\n");
2054 s
= comedi_write_subdevice(dev
, minor
);
2055 if (!s
|| !s
->async
) {
2062 if (!s
->busy
|| !nbytes
)
2064 if (s
->busy
!= file
) {
2069 add_wait_queue(&async
->wait_head
, &wait
);
2070 on_wait_queue
= true;
2071 while (nbytes
> 0 && !retval
) {
2072 set_current_state(TASK_INTERRUPTIBLE
);
2074 if (!comedi_is_subdevice_running(s
)) {
2076 struct comedi_subdevice
*new_s
;
2078 if (comedi_is_subdevice_in_error(s
))
2083 * To avoid deadlock, cannot acquire dev->mutex
2084 * while dev->attach_lock is held. Need to
2085 * remove task from the async wait queue before
2086 * releasing dev->attach_lock, as it might not
2087 * be valid afterwards.
2089 remove_wait_queue(&async
->wait_head
, &wait
);
2090 on_wait_queue
= false;
2091 up_read(&dev
->attach_lock
);
2092 attach_locked
= false;
2093 mutex_lock(&dev
->mutex
);
2095 * Become non-busy unless things have changed
2096 * behind our back. Checking dev->detach_count
2097 * is unchanged ought to be sufficient (unless
2098 * there have been 2**32 detaches in the
2099 * meantime!), but check the subdevice pointer
2100 * as well just in case.
2102 new_s
= comedi_write_subdevice(dev
, minor
);
2103 if (dev
->attached
&&
2104 old_detach_count
== dev
->detach_count
&&
2105 s
== new_s
&& new_s
->async
== async
)
2106 do_become_nonbusy(dev
, s
);
2107 mutex_unlock(&dev
->mutex
);
2115 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
2116 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
2117 comedi_buf_write_alloc(s
, async
->prealloc_bufsz
);
2118 if (m
> comedi_buf_write_n_allocated(s
))
2119 m
= comedi_buf_write_n_allocated(s
);
2124 if (file
->f_flags
& O_NONBLOCK
) {
2129 if (signal_pending(current
)) {
2130 retval
= -ERESTARTSYS
;
2135 if (s
->busy
!= file
) {
2142 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
2148 comedi_buf_write_free(s
, n
);
2154 break; /* makes device work like a pipe */
2158 remove_wait_queue(&async
->wait_head
, &wait
);
2159 set_current_state(TASK_RUNNING
);
2161 up_read(&dev
->attach_lock
);
2163 return count
? count
: retval
;
2166 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
2169 struct comedi_subdevice
*s
;
2170 struct comedi_async
*async
;
2171 int n
, m
, count
= 0, retval
= 0;
2172 DECLARE_WAITQUEUE(wait
, current
);
2173 const unsigned minor
= iminor(file_inode(file
));
2174 struct comedi_device
*dev
= file
->private_data
;
2175 unsigned int old_detach_count
;
2176 bool become_nonbusy
= false;
2179 /* Protect against device detachment during operation. */
2180 down_read(&dev
->attach_lock
);
2181 attach_locked
= true;
2182 old_detach_count
= dev
->detach_count
;
2184 if (!dev
->attached
) {
2185 dev_dbg(dev
->class_dev
, "no driver attached\n");
2190 s
= comedi_read_subdevice(dev
, minor
);
2191 if (!s
|| !s
->async
) {
2197 if (!s
->busy
|| !nbytes
)
2199 if (s
->busy
!= file
) {
2204 add_wait_queue(&async
->wait_head
, &wait
);
2205 while (nbytes
> 0 && !retval
) {
2206 set_current_state(TASK_INTERRUPTIBLE
);
2210 m
= comedi_buf_read_n_available(s
);
2211 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
2212 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
2217 if (!comedi_is_subdevice_running(s
)) {
2218 if (comedi_is_subdevice_in_error(s
))
2222 become_nonbusy
= true;
2225 if (file
->f_flags
& O_NONBLOCK
) {
2230 if (signal_pending(current
)) {
2231 retval
= -ERESTARTSYS
;
2238 if (s
->busy
!= file
) {
2244 m
= copy_to_user(buf
, async
->prealloc_buf
+
2245 async
->buf_read_ptr
, n
);
2251 comedi_buf_read_alloc(s
, n
);
2252 comedi_buf_read_free(s
, n
);
2258 break; /* makes device work like a pipe */
2260 remove_wait_queue(&async
->wait_head
, &wait
);
2261 set_current_state(TASK_RUNNING
);
2262 if (become_nonbusy
|| comedi_is_subdevice_idle(s
)) {
2263 struct comedi_subdevice
*new_s
;
2266 * To avoid deadlock, cannot acquire dev->mutex
2267 * while dev->attach_lock is held.
2269 up_read(&dev
->attach_lock
);
2270 attach_locked
= false;
2271 mutex_lock(&dev
->mutex
);
2273 * Check device hasn't become detached behind our back.
2274 * Checking dev->detach_count is unchanged ought to be
2275 * sufficient (unless there have been 2**32 detaches in the
2276 * meantime!), but check the subdevice pointer as well just in
2279 new_s
= comedi_read_subdevice(dev
, minor
);
2280 if (dev
->attached
&& old_detach_count
== dev
->detach_count
&&
2281 s
== new_s
&& new_s
->async
== async
) {
2282 if (become_nonbusy
|| comedi_buf_n_bytes_ready(s
) == 0)
2283 do_become_nonbusy(dev
, s
);
2285 mutex_unlock(&dev
->mutex
);
2289 up_read(&dev
->attach_lock
);
2291 return count
? count
: retval
;
2294 static int comedi_open(struct inode
*inode
, struct file
*file
)
2296 const unsigned minor
= iminor(inode
);
2297 struct comedi_device
*dev
= comedi_dev_get_from_minor(minor
);
2301 pr_debug("invalid minor number\n");
2305 mutex_lock(&dev
->mutex
);
2306 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
2307 dev_dbg(dev
->class_dev
, "not attached and not CAP_NET_ADMIN\n");
2311 if (dev
->attached
&& dev
->use_count
== 0) {
2312 if (!try_module_get(dev
->driver
->module
)) {
2317 rc
= dev
->open(dev
);
2319 module_put(dev
->driver
->module
);
2326 file
->private_data
= dev
;
2330 mutex_unlock(&dev
->mutex
);
2332 comedi_dev_put(dev
);
2336 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2338 struct comedi_device
*dev
= file
->private_data
;
2340 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2343 static int comedi_close(struct inode
*inode
, struct file
*file
)
2345 struct comedi_device
*dev
= file
->private_data
;
2346 struct comedi_subdevice
*s
= NULL
;
2349 mutex_lock(&dev
->mutex
);
2351 if (dev
->subdevices
) {
2352 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2353 s
= &dev
->subdevices
[i
];
2355 if (s
->busy
== file
)
2357 if (s
->lock
== file
)
2361 if (dev
->attached
&& dev
->use_count
== 1) {
2364 module_put(dev
->driver
->module
);
2369 mutex_unlock(&dev
->mutex
);
2370 comedi_dev_put(dev
);
2375 static const struct file_operations comedi_fops
= {
2376 .owner
= THIS_MODULE
,
2377 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2378 .compat_ioctl
= comedi_compat_ioctl
,
2379 .open
= comedi_open
,
2380 .release
= comedi_close
,
2381 .read
= comedi_read
,
2382 .write
= comedi_write
,
2383 .mmap
= comedi_mmap
,
2384 .poll
= comedi_poll
,
2385 .fasync
= comedi_fasync
,
2386 .llseek
= noop_llseek
,
2389 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2391 struct comedi_async
*async
= s
->async
;
2392 unsigned runflags
= 0;
2393 unsigned runflags_mask
= 0;
2395 if (!comedi_is_subdevice_running(s
))
2399 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
2400 COMEDI_CB_OVERFLOW
)) {
2401 runflags_mask
|= SRF_RUNNING
;
2403 /* remember if an error event has occurred, so an error
2404 * can be returned the next time the user does a read() */
2405 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2406 runflags_mask
|= SRF_ERROR
;
2407 runflags
|= SRF_ERROR
;
2409 if (runflags_mask
) {
2410 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2411 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2414 if (async
->cb_mask
& s
->async
->events
) {
2415 wake_up_interruptible(&async
->wait_head
);
2416 if (s
->subdev_flags
& SDF_CMD_READ
)
2417 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2418 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2419 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2421 s
->async
->events
= 0;
2423 EXPORT_SYMBOL_GPL(comedi_event
);
2425 /* Note: the ->mutex is pre-locked on successful return */
2426 struct comedi_device
*comedi_alloc_board_minor(struct device
*hardware_device
)
2428 struct comedi_device
*dev
;
2429 struct device
*csdev
;
2432 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2434 return ERR_PTR(-ENOMEM
);
2435 comedi_device_init(dev
);
2436 comedi_set_hw_dev(dev
, hardware_device
);
2437 mutex_lock(&dev
->mutex
);
2438 mutex_lock(&comedi_board_minor_table_lock
);
2439 for (i
= hardware_device
? comedi_num_legacy_minors
: 0;
2440 i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2441 if (comedi_board_minor_table
[i
] == NULL
) {
2442 comedi_board_minor_table
[i
] = dev
;
2446 mutex_unlock(&comedi_board_minor_table_lock
);
2447 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2448 mutex_unlock(&dev
->mutex
);
2449 comedi_device_cleanup(dev
);
2450 comedi_dev_put(dev
);
2451 pr_err("ran out of minor numbers for board device files\n");
2452 return ERR_PTR(-EBUSY
);
2455 csdev
= device_create(comedi_class
, hardware_device
,
2456 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2458 dev
->class_dev
= get_device(csdev
);
2460 /* Note: dev->mutex needs to be unlocked by the caller. */
2464 static void comedi_free_board_minor(unsigned minor
)
2466 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2467 comedi_free_board_dev(comedi_clear_board_minor(minor
));
2470 void comedi_release_hardware_device(struct device
*hardware_device
)
2473 struct comedi_device
*dev
;
2475 for (minor
= comedi_num_legacy_minors
; minor
< COMEDI_NUM_BOARD_MINORS
;
2477 mutex_lock(&comedi_board_minor_table_lock
);
2478 dev
= comedi_board_minor_table
[minor
];
2479 if (dev
&& dev
->hw_dev
== hardware_device
) {
2480 comedi_board_minor_table
[minor
] = NULL
;
2481 mutex_unlock(&comedi_board_minor_table_lock
);
2482 comedi_free_board_dev(dev
);
2485 mutex_unlock(&comedi_board_minor_table_lock
);
2489 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2491 struct comedi_device
*dev
= s
->device
;
2492 struct device
*csdev
;
2495 mutex_lock(&comedi_subdevice_minor_table_lock
);
2496 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
) {
2497 if (comedi_subdevice_minor_table
[i
] == NULL
) {
2498 comedi_subdevice_minor_table
[i
] = s
;
2502 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2503 if (i
== COMEDI_NUM_SUBDEVICE_MINORS
) {
2504 pr_err("ran out of minor numbers for subdevice files\n");
2507 i
+= COMEDI_NUM_BOARD_MINORS
;
2509 csdev
= device_create(comedi_class
, dev
->class_dev
,
2510 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2511 dev
->minor
, s
->index
);
2513 s
->class_dev
= csdev
;
2518 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2527 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2528 BUG_ON(s
->minor
< COMEDI_NUM_BOARD_MINORS
);
2530 i
= s
->minor
- COMEDI_NUM_BOARD_MINORS
;
2531 mutex_lock(&comedi_subdevice_minor_table_lock
);
2532 if (s
== comedi_subdevice_minor_table
[i
])
2533 comedi_subdevice_minor_table
[i
] = NULL
;
2534 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2536 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2537 s
->class_dev
= NULL
;
2541 static void comedi_cleanup_board_minors(void)
2545 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++)
2546 comedi_free_board_minor(i
);
2549 static int __init
comedi_init(void)
2554 pr_info("version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2556 if (comedi_num_legacy_minors
< 0 ||
2557 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2558 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2559 COMEDI_NUM_BOARD_MINORS
);
2563 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2564 COMEDI_NUM_MINORS
, "comedi");
2567 cdev_init(&comedi_cdev
, &comedi_fops
);
2568 comedi_cdev
.owner
= THIS_MODULE
;
2570 retval
= kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2572 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2577 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2578 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2582 comedi_class
= class_create(THIS_MODULE
, "comedi");
2583 if (IS_ERR(comedi_class
)) {
2584 pr_err("failed to create class\n");
2585 cdev_del(&comedi_cdev
);
2586 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2588 return PTR_ERR(comedi_class
);
2591 comedi_class
->dev_groups
= comedi_dev_groups
;
2593 /* XXX requires /proc interface */
2596 /* create devices files for legacy/manual use */
2597 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2598 struct comedi_device
*dev
;
2600 dev
= comedi_alloc_board_minor(NULL
);
2602 comedi_cleanup_board_minors();
2603 cdev_del(&comedi_cdev
);
2604 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2606 return PTR_ERR(dev
);
2608 /* comedi_alloc_board_minor() locked the mutex */
2609 mutex_unlock(&dev
->mutex
);
2614 module_init(comedi_init
);
2616 static void __exit
comedi_cleanup(void)
2620 comedi_cleanup_board_minors();
2621 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
)
2622 BUG_ON(comedi_board_minor_table
[i
]);
2623 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
)
2624 BUG_ON(comedi_subdevice_minor_table
[i
]);
2626 class_destroy(comedi_class
);
2627 cdev_del(&comedi_cdev
);
2628 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2630 comedi_proc_cleanup();
2632 module_exit(comedi_cleanup
);
2634 MODULE_AUTHOR("http://www.comedi.org");
2635 MODULE_DESCRIPTION("Comedi core module");
2636 MODULE_LICENSE("GPL");