1 // SPDX-License-Identifier: GPL-2.0+
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include "comedi_compat32.h"
14 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/sched/signal.h>
18 #include <linux/fcntl.h>
19 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/poll.h>
23 #include <linux/device.h>
25 #include "comedidev.h"
26 #include <linux/cdev.h>
29 #include <linux/uaccess.h>
31 #include "comedi_internal.h"
34 * comedi_subdevice "runflags"
35 * COMEDI_SRF_RT: DEPRECATED: command is running real-time
36 * COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred
37 * since the last command was started
38 * COMEDI_SRF_RUNNING: command is running
39 * COMEDI_SRF_FREE_SPRIV: free s->private on detach
41 * COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy"
43 #define COMEDI_SRF_RT BIT(1)
44 #define COMEDI_SRF_ERROR BIT(2)
45 #define COMEDI_SRF_RUNNING BIT(27)
46 #define COMEDI_SRF_FREE_SPRIV BIT(31)
48 #define COMEDI_SRF_BUSY_MASK (COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING)
51 * struct comedi_file - Per-file private data for COMEDI device
52 * @dev: COMEDI device.
53 * @read_subdev: Current "read" subdevice.
54 * @write_subdev: Current "write" subdevice.
55 * @last_detach_count: Last known detach count.
56 * @last_attached: Last known attached/detached state.
59 struct comedi_device
*dev
;
60 struct comedi_subdevice
*read_subdev
;
61 struct comedi_subdevice
*write_subdev
;
62 unsigned int last_detach_count
;
63 unsigned int last_attached
:1;
66 #define COMEDI_NUM_MINORS 0x100
67 #define COMEDI_NUM_SUBDEVICE_MINORS \
68 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
70 static unsigned short comedi_num_legacy_minors
;
71 module_param(comedi_num_legacy_minors
, ushort
, 0444);
72 MODULE_PARM_DESC(comedi_num_legacy_minors
,
73 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
76 unsigned int comedi_default_buf_size_kb
= CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
;
77 module_param(comedi_default_buf_size_kb
, uint
, 0644);
78 MODULE_PARM_DESC(comedi_default_buf_size_kb
,
79 "default asynchronous buffer size in KiB (default "
80 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
) ")");
82 unsigned int comedi_default_buf_maxsize_kb
=
83 CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
;
84 module_param(comedi_default_buf_maxsize_kb
, uint
, 0644);
85 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb
,
86 "default maximum size of asynchronous buffer in KiB (default "
87 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
) ")");
89 static DEFINE_MUTEX(comedi_board_minor_table_lock
);
90 static struct comedi_device
91 *comedi_board_minor_table
[COMEDI_NUM_BOARD_MINORS
];
93 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock
);
94 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
95 static struct comedi_subdevice
96 *comedi_subdevice_minor_table
[COMEDI_NUM_SUBDEVICE_MINORS
];
98 static struct class *comedi_class
;
99 static struct cdev comedi_cdev
;
101 static void comedi_device_init(struct comedi_device
*dev
)
103 kref_init(&dev
->refcount
);
104 spin_lock_init(&dev
->spinlock
);
105 mutex_init(&dev
->mutex
);
106 init_rwsem(&dev
->attach_lock
);
110 static void comedi_dev_kref_release(struct kref
*kref
)
112 struct comedi_device
*dev
=
113 container_of(kref
, struct comedi_device
, refcount
);
115 mutex_destroy(&dev
->mutex
);
116 put_device(dev
->class_dev
);
121 * comedi_dev_put() - Release a use of a COMEDI device
122 * @dev: COMEDI device.
124 * Must be called when a user of a COMEDI device is finished with it.
125 * When the last user of the COMEDI device calls this function, the
126 * COMEDI device is destroyed.
128 * Return: 1 if the COMEDI device is destroyed by this call or @dev is
129 * NULL, otherwise return 0. Callers must not assume the COMEDI
130 * device is still valid if this function returns 0.
132 int comedi_dev_put(struct comedi_device
*dev
)
135 return kref_put(&dev
->refcount
, comedi_dev_kref_release
);
138 EXPORT_SYMBOL_GPL(comedi_dev_put
);
140 static struct comedi_device
*comedi_dev_get(struct comedi_device
*dev
)
143 kref_get(&dev
->refcount
);
147 static void comedi_device_cleanup(struct comedi_device
*dev
)
149 struct module
*driver_module
= NULL
;
153 mutex_lock(&dev
->mutex
);
155 driver_module
= dev
->driver
->module
;
156 comedi_device_detach(dev
);
157 if (driver_module
&& dev
->use_count
)
158 module_put(driver_module
);
159 mutex_unlock(&dev
->mutex
);
162 static bool comedi_clear_board_dev(struct comedi_device
*dev
)
164 unsigned int i
= dev
->minor
;
165 bool cleared
= false;
167 lockdep_assert_held(&dev
->mutex
);
168 mutex_lock(&comedi_board_minor_table_lock
);
169 if (dev
== comedi_board_minor_table
[i
]) {
170 comedi_board_minor_table
[i
] = NULL
;
173 mutex_unlock(&comedi_board_minor_table_lock
);
177 static struct comedi_device
*comedi_clear_board_minor(unsigned int minor
)
179 struct comedi_device
*dev
;
181 mutex_lock(&comedi_board_minor_table_lock
);
182 dev
= comedi_board_minor_table
[minor
];
183 comedi_board_minor_table
[minor
] = NULL
;
184 mutex_unlock(&comedi_board_minor_table_lock
);
188 static void comedi_free_board_dev(struct comedi_device
*dev
)
191 comedi_device_cleanup(dev
);
192 if (dev
->class_dev
) {
193 device_destroy(comedi_class
,
194 MKDEV(COMEDI_MAJOR
, dev
->minor
));
200 static struct comedi_subdevice
*
201 comedi_subdevice_from_minor(const struct comedi_device
*dev
, unsigned int minor
)
203 struct comedi_subdevice
*s
;
204 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
206 mutex_lock(&comedi_subdevice_minor_table_lock
);
207 s
= comedi_subdevice_minor_table
[i
];
208 if (s
&& s
->device
!= dev
)
210 mutex_unlock(&comedi_subdevice_minor_table_lock
);
214 static struct comedi_device
*comedi_dev_get_from_board_minor(unsigned int minor
)
216 struct comedi_device
*dev
;
218 mutex_lock(&comedi_board_minor_table_lock
);
219 dev
= comedi_dev_get(comedi_board_minor_table
[minor
]);
220 mutex_unlock(&comedi_board_minor_table_lock
);
224 static struct comedi_device
*
225 comedi_dev_get_from_subdevice_minor(unsigned int minor
)
227 struct comedi_device
*dev
;
228 struct comedi_subdevice
*s
;
229 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
231 mutex_lock(&comedi_subdevice_minor_table_lock
);
232 s
= comedi_subdevice_minor_table
[i
];
233 dev
= comedi_dev_get(s
? s
->device
: NULL
);
234 mutex_unlock(&comedi_subdevice_minor_table_lock
);
239 * comedi_dev_get_from_minor() - Get COMEDI device by minor device number
240 * @minor: Minor device number.
242 * Finds the COMEDI device associated with the minor device number, if any,
243 * and increments its reference count. The COMEDI device is prevented from
244 * being freed until a matching call is made to comedi_dev_put().
246 * Return: A pointer to the COMEDI device if it exists, with its usage
247 * reference incremented. Return NULL if no COMEDI device exists with the
248 * specified minor device number.
250 struct comedi_device
*comedi_dev_get_from_minor(unsigned int minor
)
252 if (minor
< COMEDI_NUM_BOARD_MINORS
)
253 return comedi_dev_get_from_board_minor(minor
);
255 return comedi_dev_get_from_subdevice_minor(minor
);
257 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor
);
259 static struct comedi_subdevice
*
260 comedi_read_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
262 struct comedi_subdevice
*s
;
264 lockdep_assert_held(&dev
->mutex
);
265 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
266 s
= comedi_subdevice_from_minor(dev
, minor
);
267 if (!s
|| (s
->subdev_flags
& SDF_CMD_READ
))
270 return dev
->read_subdev
;
273 static struct comedi_subdevice
*
274 comedi_write_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
276 struct comedi_subdevice
*s
;
278 lockdep_assert_held(&dev
->mutex
);
279 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
280 s
= comedi_subdevice_from_minor(dev
, minor
);
281 if (!s
|| (s
->subdev_flags
& SDF_CMD_WRITE
))
284 return dev
->write_subdev
;
287 static void comedi_file_reset(struct file
*file
)
289 struct comedi_file
*cfp
= file
->private_data
;
290 struct comedi_device
*dev
= cfp
->dev
;
291 struct comedi_subdevice
*s
, *read_s
, *write_s
;
292 unsigned int minor
= iminor(file_inode(file
));
294 read_s
= dev
->read_subdev
;
295 write_s
= dev
->write_subdev
;
296 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
297 s
= comedi_subdevice_from_minor(dev
, minor
);
298 if (!s
|| s
->subdev_flags
& SDF_CMD_READ
)
300 if (!s
|| s
->subdev_flags
& SDF_CMD_WRITE
)
303 cfp
->last_attached
= dev
->attached
;
304 cfp
->last_detach_count
= dev
->detach_count
;
305 WRITE_ONCE(cfp
->read_subdev
, read_s
);
306 WRITE_ONCE(cfp
->write_subdev
, write_s
);
309 static void comedi_file_check(struct file
*file
)
311 struct comedi_file
*cfp
= file
->private_data
;
312 struct comedi_device
*dev
= cfp
->dev
;
314 if (cfp
->last_attached
!= dev
->attached
||
315 cfp
->last_detach_count
!= dev
->detach_count
)
316 comedi_file_reset(file
);
319 static struct comedi_subdevice
*comedi_file_read_subdevice(struct file
*file
)
321 struct comedi_file
*cfp
= file
->private_data
;
323 comedi_file_check(file
);
324 return READ_ONCE(cfp
->read_subdev
);
327 static struct comedi_subdevice
*comedi_file_write_subdevice(struct file
*file
)
329 struct comedi_file
*cfp
= file
->private_data
;
331 comedi_file_check(file
);
332 return READ_ONCE(cfp
->write_subdev
);
335 static int resize_async_buffer(struct comedi_device
*dev
,
336 struct comedi_subdevice
*s
,
337 unsigned int new_size
)
339 struct comedi_async
*async
= s
->async
;
342 lockdep_assert_held(&dev
->mutex
);
344 if (new_size
> async
->max_bufsize
)
348 dev_dbg(dev
->class_dev
,
349 "subdevice is busy, cannot resize buffer\n");
352 if (comedi_buf_is_mmapped(s
)) {
353 dev_dbg(dev
->class_dev
,
354 "subdevice is mmapped, cannot resize buffer\n");
358 /* make sure buffer is an integral number of pages (we round up) */
359 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
361 retval
= comedi_buf_alloc(dev
, s
, new_size
);
366 retval
= s
->buf_change(dev
, s
);
371 dev_dbg(dev
->class_dev
, "subd %d buffer resized to %i bytes\n",
372 s
->index
, async
->prealloc_bufsz
);
376 /* sysfs attribute files */
378 static ssize_t
max_read_buffer_kb_show(struct device
*csdev
,
379 struct device_attribute
*attr
, char *buf
)
381 unsigned int minor
= MINOR(csdev
->devt
);
382 struct comedi_device
*dev
;
383 struct comedi_subdevice
*s
;
384 unsigned int size
= 0;
386 dev
= comedi_dev_get_from_minor(minor
);
390 mutex_lock(&dev
->mutex
);
391 s
= comedi_read_subdevice(dev
, minor
);
392 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
393 size
= s
->async
->max_bufsize
/ 1024;
394 mutex_unlock(&dev
->mutex
);
397 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
400 static ssize_t
max_read_buffer_kb_store(struct device
*csdev
,
401 struct device_attribute
*attr
,
402 const char *buf
, size_t count
)
404 unsigned int minor
= MINOR(csdev
->devt
);
405 struct comedi_device
*dev
;
406 struct comedi_subdevice
*s
;
410 err
= kstrtouint(buf
, 10, &size
);
413 if (size
> (UINT_MAX
/ 1024))
417 dev
= comedi_dev_get_from_minor(minor
);
421 mutex_lock(&dev
->mutex
);
422 s
= comedi_read_subdevice(dev
, minor
);
423 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
424 s
->async
->max_bufsize
= size
;
427 mutex_unlock(&dev
->mutex
);
430 return err
? err
: count
;
432 static DEVICE_ATTR_RW(max_read_buffer_kb
);
434 static ssize_t
read_buffer_kb_show(struct device
*csdev
,
435 struct device_attribute
*attr
, char *buf
)
437 unsigned int minor
= MINOR(csdev
->devt
);
438 struct comedi_device
*dev
;
439 struct comedi_subdevice
*s
;
440 unsigned int size
= 0;
442 dev
= comedi_dev_get_from_minor(minor
);
446 mutex_lock(&dev
->mutex
);
447 s
= comedi_read_subdevice(dev
, minor
);
448 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
449 size
= s
->async
->prealloc_bufsz
/ 1024;
450 mutex_unlock(&dev
->mutex
);
453 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
456 static ssize_t
read_buffer_kb_store(struct device
*csdev
,
457 struct device_attribute
*attr
,
458 const char *buf
, size_t count
)
460 unsigned int minor
= MINOR(csdev
->devt
);
461 struct comedi_device
*dev
;
462 struct comedi_subdevice
*s
;
466 err
= kstrtouint(buf
, 10, &size
);
469 if (size
> (UINT_MAX
/ 1024))
473 dev
= comedi_dev_get_from_minor(minor
);
477 mutex_lock(&dev
->mutex
);
478 s
= comedi_read_subdevice(dev
, minor
);
479 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
480 err
= resize_async_buffer(dev
, s
, size
);
483 mutex_unlock(&dev
->mutex
);
486 return err
? err
: count
;
488 static DEVICE_ATTR_RW(read_buffer_kb
);
490 static ssize_t
max_write_buffer_kb_show(struct device
*csdev
,
491 struct device_attribute
*attr
,
494 unsigned int minor
= MINOR(csdev
->devt
);
495 struct comedi_device
*dev
;
496 struct comedi_subdevice
*s
;
497 unsigned int size
= 0;
499 dev
= comedi_dev_get_from_minor(minor
);
503 mutex_lock(&dev
->mutex
);
504 s
= comedi_write_subdevice(dev
, minor
);
505 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
506 size
= s
->async
->max_bufsize
/ 1024;
507 mutex_unlock(&dev
->mutex
);
510 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
513 static ssize_t
max_write_buffer_kb_store(struct device
*csdev
,
514 struct device_attribute
*attr
,
515 const char *buf
, size_t count
)
517 unsigned int minor
= MINOR(csdev
->devt
);
518 struct comedi_device
*dev
;
519 struct comedi_subdevice
*s
;
523 err
= kstrtouint(buf
, 10, &size
);
526 if (size
> (UINT_MAX
/ 1024))
530 dev
= comedi_dev_get_from_minor(minor
);
534 mutex_lock(&dev
->mutex
);
535 s
= comedi_write_subdevice(dev
, minor
);
536 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
537 s
->async
->max_bufsize
= size
;
540 mutex_unlock(&dev
->mutex
);
543 return err
? err
: count
;
545 static DEVICE_ATTR_RW(max_write_buffer_kb
);
547 static ssize_t
write_buffer_kb_show(struct device
*csdev
,
548 struct device_attribute
*attr
, char *buf
)
550 unsigned int minor
= MINOR(csdev
->devt
);
551 struct comedi_device
*dev
;
552 struct comedi_subdevice
*s
;
553 unsigned int size
= 0;
555 dev
= comedi_dev_get_from_minor(minor
);
559 mutex_lock(&dev
->mutex
);
560 s
= comedi_write_subdevice(dev
, minor
);
561 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
562 size
= s
->async
->prealloc_bufsz
/ 1024;
563 mutex_unlock(&dev
->mutex
);
566 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
569 static ssize_t
write_buffer_kb_store(struct device
*csdev
,
570 struct device_attribute
*attr
,
571 const char *buf
, size_t count
)
573 unsigned int minor
= MINOR(csdev
->devt
);
574 struct comedi_device
*dev
;
575 struct comedi_subdevice
*s
;
579 err
= kstrtouint(buf
, 10, &size
);
582 if (size
> (UINT_MAX
/ 1024))
586 dev
= comedi_dev_get_from_minor(minor
);
590 mutex_lock(&dev
->mutex
);
591 s
= comedi_write_subdevice(dev
, minor
);
592 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
593 err
= resize_async_buffer(dev
, s
, size
);
596 mutex_unlock(&dev
->mutex
);
599 return err
? err
: count
;
601 static DEVICE_ATTR_RW(write_buffer_kb
);
603 static struct attribute
*comedi_dev_attrs
[] = {
604 &dev_attr_max_read_buffer_kb
.attr
,
605 &dev_attr_read_buffer_kb
.attr
,
606 &dev_attr_max_write_buffer_kb
.attr
,
607 &dev_attr_write_buffer_kb
.attr
,
610 ATTRIBUTE_GROUPS(comedi_dev
);
612 static void __comedi_clear_subdevice_runflags(struct comedi_subdevice
*s
,
615 s
->runflags
&= ~bits
;
618 static void __comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
624 static void comedi_update_subdevice_runflags(struct comedi_subdevice
*s
,
630 spin_lock_irqsave(&s
->spin_lock
, flags
);
631 __comedi_clear_subdevice_runflags(s
, mask
);
632 __comedi_set_subdevice_runflags(s
, bits
& mask
);
633 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
636 static unsigned int __comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
641 static unsigned int comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
644 unsigned int runflags
;
646 spin_lock_irqsave(&s
->spin_lock
, flags
);
647 runflags
= __comedi_get_subdevice_runflags(s
);
648 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
652 static bool comedi_is_runflags_running(unsigned int runflags
)
654 return runflags
& COMEDI_SRF_RUNNING
;
657 static bool comedi_is_runflags_in_error(unsigned int runflags
)
659 return runflags
& COMEDI_SRF_ERROR
;
663 * comedi_is_subdevice_running() - Check if async command running on subdevice
664 * @s: COMEDI subdevice.
666 * Return: %true if an asynchronous COMEDI command is active on the
667 * subdevice, else %false.
669 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
)
671 unsigned int runflags
= comedi_get_subdevice_runflags(s
);
673 return comedi_is_runflags_running(runflags
);
675 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running
);
677 static bool __comedi_is_subdevice_running(struct comedi_subdevice
*s
)
679 unsigned int runflags
= __comedi_get_subdevice_runflags(s
);
681 return comedi_is_runflags_running(runflags
);
684 bool comedi_can_auto_free_spriv(struct comedi_subdevice
*s
)
686 unsigned int runflags
= __comedi_get_subdevice_runflags(s
);
688 return runflags
& COMEDI_SRF_FREE_SPRIV
;
692 * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
693 * @s: COMEDI subdevice.
695 * Mark the subdevice as having a pointer to private data that can be
696 * automatically freed when the COMEDI device is detached from the low-level
699 void comedi_set_spriv_auto_free(struct comedi_subdevice
*s
)
701 __comedi_set_subdevice_runflags(s
, COMEDI_SRF_FREE_SPRIV
);
703 EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free
);
706 * comedi_alloc_spriv - Allocate memory for the subdevice private data
707 * @s: COMEDI subdevice.
708 * @size: Size of the memory to allocate.
710 * Allocate memory for the subdevice private data and point @s->private
711 * to it. The memory will be freed automatically when the COMEDI device
712 * is detached from the low-level driver.
714 * Return: A pointer to the allocated memory @s->private on success.
715 * Return NULL on failure.
717 void *comedi_alloc_spriv(struct comedi_subdevice
*s
, size_t size
)
719 s
->private = kzalloc(size
, GFP_KERNEL
);
721 comedi_set_spriv_auto_free(s
);
724 EXPORT_SYMBOL_GPL(comedi_alloc_spriv
);
727 * This function restores a subdevice to an idle state.
729 static void do_become_nonbusy(struct comedi_device
*dev
,
730 struct comedi_subdevice
*s
)
732 struct comedi_async
*async
= s
->async
;
734 lockdep_assert_held(&dev
->mutex
);
735 comedi_update_subdevice_runflags(s
, COMEDI_SRF_RUNNING
, 0);
738 async
->inttrig
= NULL
;
739 kfree(async
->cmd
.chanlist
);
740 async
->cmd
.chanlist
= NULL
;
742 wake_up_interruptible_all(&async
->wait_head
);
744 dev_err(dev
->class_dev
,
745 "BUG: (?) %s called with async=NULL\n", __func__
);
750 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
754 lockdep_assert_held(&dev
->mutex
);
755 if (comedi_is_subdevice_running(s
) && s
->cancel
)
756 ret
= s
->cancel(dev
, s
);
758 do_become_nonbusy(dev
, s
);
763 void comedi_device_cancel_all(struct comedi_device
*dev
)
765 struct comedi_subdevice
*s
;
768 lockdep_assert_held(&dev
->mutex
);
772 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
773 s
= &dev
->subdevices
[i
];
779 static int is_device_busy(struct comedi_device
*dev
)
781 struct comedi_subdevice
*s
;
784 lockdep_assert_held(&dev
->mutex
);
788 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
789 s
= &dev
->subdevices
[i
];
792 if (s
->async
&& comedi_buf_is_mmapped(s
))
800 * COMEDI_DEVCONFIG ioctl
801 * attaches (and configures) or detaches a legacy device
804 * pointer to comedi_devconfig structure (NULL if detaching)
807 * comedi_devconfig structure (if attaching)
812 static int do_devconfig_ioctl(struct comedi_device
*dev
,
813 struct comedi_devconfig __user
*arg
)
815 struct comedi_devconfig it
;
817 lockdep_assert_held(&dev
->mutex
);
818 if (!capable(CAP_SYS_ADMIN
))
822 if (is_device_busy(dev
))
825 struct module
*driver_module
= dev
->driver
->module
;
827 comedi_device_detach(dev
);
828 module_put(driver_module
);
833 if (copy_from_user(&it
, arg
, sizeof(it
)))
836 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
838 if (it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
839 dev_warn(dev
->class_dev
,
840 "comedi_config --init_data is deprecated\n");
844 if (dev
->minor
>= comedi_num_legacy_minors
)
845 /* don't re-use dynamically allocated comedi devices */
848 /* This increments the driver module count on success. */
849 return comedi_device_attach(dev
, &it
);
853 * COMEDI_BUFCONFIG ioctl
854 * buffer configuration
857 * pointer to comedi_bufconfig structure
860 * comedi_bufconfig structure
863 * modified comedi_bufconfig structure
865 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
866 struct comedi_bufconfig __user
*arg
)
868 struct comedi_bufconfig bc
;
869 struct comedi_async
*async
;
870 struct comedi_subdevice
*s
;
873 lockdep_assert_held(&dev
->mutex
);
874 if (copy_from_user(&bc
, arg
, sizeof(bc
)))
877 if (bc
.subdevice
>= dev
->n_subdevices
)
880 s
= &dev
->subdevices
[bc
.subdevice
];
884 dev_dbg(dev
->class_dev
,
885 "subdevice does not have async capability\n");
891 if (bc
.maximum_size
) {
892 if (!capable(CAP_SYS_ADMIN
))
895 async
->max_bufsize
= bc
.maximum_size
;
899 retval
= resize_async_buffer(dev
, s
, bc
.size
);
904 bc
.size
= async
->prealloc_bufsz
;
905 bc
.maximum_size
= async
->max_bufsize
;
908 if (copy_to_user(arg
, &bc
, sizeof(bc
)))
915 * COMEDI_DEVINFO ioctl
919 * pointer to comedi_devinfo structure
925 * comedi_devinfo structure
927 static int do_devinfo_ioctl(struct comedi_device
*dev
,
928 struct comedi_devinfo __user
*arg
,
931 struct comedi_subdevice
*s
;
932 struct comedi_devinfo devinfo
;
934 lockdep_assert_held(&dev
->mutex
);
935 memset(&devinfo
, 0, sizeof(devinfo
));
937 /* fill devinfo structure */
938 devinfo
.version_code
= COMEDI_VERSION_CODE
;
939 devinfo
.n_subdevs
= dev
->n_subdevices
;
940 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
941 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
943 s
= comedi_file_read_subdevice(file
);
945 devinfo
.read_subdevice
= s
->index
;
947 devinfo
.read_subdevice
= -1;
949 s
= comedi_file_write_subdevice(file
);
951 devinfo
.write_subdevice
= s
->index
;
953 devinfo
.write_subdevice
= -1;
955 if (copy_to_user(arg
, &devinfo
, sizeof(devinfo
)))
962 * COMEDI_SUBDINFO ioctl
966 * pointer to array of comedi_subdinfo structures
972 * array of comedi_subdinfo structures
974 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
975 struct comedi_subdinfo __user
*arg
, void *file
)
978 struct comedi_subdinfo
*tmp
, *us
;
979 struct comedi_subdevice
*s
;
981 lockdep_assert_held(&dev
->mutex
);
982 tmp
= kcalloc(dev
->n_subdevices
, sizeof(*tmp
), GFP_KERNEL
);
986 /* fill subdinfo structs */
987 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
988 s
= &dev
->subdevices
[i
];
992 us
->n_chan
= s
->n_chan
;
993 us
->subd_flags
= s
->subdev_flags
;
994 if (comedi_is_subdevice_running(s
))
995 us
->subd_flags
|= SDF_RUNNING
;
996 #define TIMER_nanosec 5 /* backwards compatibility */
997 us
->timer_type
= TIMER_nanosec
;
998 us
->len_chanlist
= s
->len_chanlist
;
999 us
->maxdata
= s
->maxdata
;
1000 if (s
->range_table
) {
1002 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
1004 us
->range_type
= 0; /* XXX */
1008 us
->subd_flags
|= SDF_BUSY
;
1009 if (s
->busy
== file
)
1010 us
->subd_flags
|= SDF_BUSY_OWNER
;
1012 us
->subd_flags
|= SDF_LOCKED
;
1013 if (s
->lock
== file
)
1014 us
->subd_flags
|= SDF_LOCK_OWNER
;
1015 if (!s
->maxdata
&& s
->maxdata_list
)
1016 us
->subd_flags
|= SDF_MAXDATA
;
1017 if (s
->range_table_list
)
1018 us
->subd_flags
|= SDF_RANGETYPE
;
1020 us
->subd_flags
|= SDF_CMD
;
1022 if (s
->insn_bits
!= &insn_inval
)
1023 us
->insn_bits_support
= COMEDI_SUPPORTED
;
1025 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
1028 ret
= copy_to_user(arg
, tmp
, dev
->n_subdevices
* sizeof(*tmp
));
1032 return ret
? -EFAULT
: 0;
1036 * COMEDI_CHANINFO ioctl
1037 * subdevice channel info
1040 * pointer to comedi_chaninfo structure
1043 * comedi_chaninfo structure
1046 * array of maxdata values to chaninfo->maxdata_list if requested
1047 * array of range table lengths to chaninfo->range_table_list if requested
1049 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
1050 struct comedi_chaninfo __user
*arg
)
1052 struct comedi_subdevice
*s
;
1053 struct comedi_chaninfo it
;
1055 lockdep_assert_held(&dev
->mutex
);
1056 if (copy_from_user(&it
, arg
, sizeof(it
)))
1059 if (it
.subdev
>= dev
->n_subdevices
)
1061 s
= &dev
->subdevices
[it
.subdev
];
1063 if (it
.maxdata_list
) {
1064 if (s
->maxdata
|| !s
->maxdata_list
)
1066 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
1067 s
->n_chan
* sizeof(unsigned int)))
1072 return -EINVAL
; /* flaglist not supported */
1077 if (!s
->range_table_list
)
1079 for (i
= 0; i
< s
->n_chan
; i
++) {
1082 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
1083 (s
->range_table_list
[i
]->length
);
1084 if (put_user(x
, it
.rangelist
+ i
))
1093 * COMEDI_BUFINFO ioctl
1094 * buffer information
1097 * pointer to comedi_bufinfo structure
1100 * comedi_bufinfo structure
1103 * modified comedi_bufinfo structure
1105 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
1106 struct comedi_bufinfo __user
*arg
, void *file
)
1108 struct comedi_bufinfo bi
;
1109 struct comedi_subdevice
*s
;
1110 struct comedi_async
*async
;
1111 unsigned int runflags
;
1113 bool become_nonbusy
= false;
1115 lockdep_assert_held(&dev
->mutex
);
1116 if (copy_from_user(&bi
, arg
, sizeof(bi
)))
1119 if (bi
.subdevice
>= dev
->n_subdevices
)
1122 s
= &dev
->subdevices
[bi
.subdevice
];
1126 if (!async
|| s
->busy
!= file
)
1129 runflags
= comedi_get_subdevice_runflags(s
);
1130 if (!(async
->cmd
.flags
& CMDF_WRITE
)) {
1131 /* command was set up in "read" direction */
1132 if (bi
.bytes_read
) {
1133 comedi_buf_read_alloc(s
, bi
.bytes_read
);
1134 bi
.bytes_read
= comedi_buf_read_free(s
, bi
.bytes_read
);
1137 * If nothing left to read, and command has stopped, and
1138 * {"read" position not updated or command stopped normally},
1139 * then become non-busy.
1141 if (comedi_buf_read_n_available(s
) == 0 &&
1142 !comedi_is_runflags_running(runflags
) &&
1143 (bi
.bytes_read
== 0 ||
1144 !comedi_is_runflags_in_error(runflags
))) {
1145 become_nonbusy
= true;
1146 if (comedi_is_runflags_in_error(runflags
))
1149 bi
.bytes_written
= 0;
1151 /* command was set up in "write" direction */
1152 if (!comedi_is_runflags_running(runflags
)) {
1153 bi
.bytes_written
= 0;
1154 become_nonbusy
= true;
1155 if (comedi_is_runflags_in_error(runflags
))
1157 } else if (bi
.bytes_written
) {
1158 comedi_buf_write_alloc(s
, bi
.bytes_written
);
1160 comedi_buf_write_free(s
, bi
.bytes_written
);
1165 bi
.buf_write_count
= async
->buf_write_count
;
1166 bi
.buf_write_ptr
= async
->buf_write_ptr
;
1167 bi
.buf_read_count
= async
->buf_read_count
;
1168 bi
.buf_read_ptr
= async
->buf_read_ptr
;
1171 do_become_nonbusy(dev
, s
);
1176 if (copy_to_user(arg
, &bi
, sizeof(bi
)))
1182 static int check_insn_config_length(struct comedi_insn
*insn
,
1189 case INSN_CONFIG_DIO_OUTPUT
:
1190 case INSN_CONFIG_DIO_INPUT
:
1191 case INSN_CONFIG_DISARM
:
1192 case INSN_CONFIG_RESET
:
1196 case INSN_CONFIG_ARM
:
1197 case INSN_CONFIG_DIO_QUERY
:
1198 case INSN_CONFIG_BLOCK_SIZE
:
1199 case INSN_CONFIG_FILTER
:
1200 case INSN_CONFIG_SERIAL_CLOCK
:
1201 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
1202 case INSN_CONFIG_ALT_SOURCE
:
1203 case INSN_CONFIG_SET_COUNTER_MODE
:
1204 case INSN_CONFIG_8254_READ_STATUS
:
1205 case INSN_CONFIG_SET_ROUTING
:
1206 case INSN_CONFIG_GET_ROUTING
:
1207 case INSN_CONFIG_GET_PWM_STATUS
:
1208 case INSN_CONFIG_PWM_SET_PERIOD
:
1209 case INSN_CONFIG_PWM_GET_PERIOD
:
1213 case INSN_CONFIG_SET_GATE_SRC
:
1214 case INSN_CONFIG_GET_GATE_SRC
:
1215 case INSN_CONFIG_SET_CLOCK_SRC
:
1216 case INSN_CONFIG_GET_CLOCK_SRC
:
1217 case INSN_CONFIG_SET_OTHER_SRC
:
1218 case INSN_CONFIG_GET_COUNTER_STATUS
:
1219 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
1220 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
1221 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
1225 case INSN_CONFIG_PWM_OUTPUT
:
1226 case INSN_CONFIG_ANALOG_TRIG
:
1227 case INSN_CONFIG_TIMER_1
:
1231 case INSN_CONFIG_DIGITAL_TRIG
:
1235 case INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS
:
1240 * by default we allow the insn since we don't have checks for
1241 * all possible cases yet
1244 pr_warn("No check for data length of config insn id %i is implemented\n",
1246 pr_warn("Add a check to %s in %s\n", __func__
, __FILE__
);
1247 pr_warn("Assuming n=%i is correct\n", insn
->n
);
1253 static int check_insn_device_config_length(struct comedi_insn
*insn
,
1260 case INSN_DEVICE_CONFIG_TEST_ROUTE
:
1261 case INSN_DEVICE_CONFIG_CONNECT_ROUTE
:
1262 case INSN_DEVICE_CONFIG_DISCONNECT_ROUTE
:
1266 case INSN_DEVICE_CONFIG_GET_ROUTES
:
1268 * Big enough for config_id and the length of the userland
1269 * memory buffer. Additional length should be in factors of 2
1270 * to communicate any returned route pairs (source,destination).
1280 * get_valid_routes() - Calls low-level driver get_valid_routes function to
1281 * either return a count of valid routes to user, or copy
1282 * of list of all valid device routes to buffer in
1284 * @dev: comedi device pointer
1285 * @data: data from user insn call. The length of the data must be >= 2.
1286 * data[0] must contain the INSN_DEVICE_CONFIG config_id.
1287 * data[1](input) contains the number of _pairs_ for which memory is
1288 * allotted from the user. If the user specifies '0', then only
1289 * the number of pairs available is returned.
1290 * data[1](output) returns either the number of pairs available (if none
1291 * where requested) or the number of _pairs_ that are copied back
1293 * data[2::2] returns each (source, destination) pair.
1295 * Return: -EINVAL if low-level driver does not allocate and return routes as
1296 * expected. Returns 0 otherwise.
1298 static int get_valid_routes(struct comedi_device
*dev
, unsigned int *data
)
1300 lockdep_assert_held(&dev
->mutex
);
1301 data
[1] = dev
->get_valid_routes(dev
, data
[1], data
+ 2);
1305 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
1306 unsigned int *data
, void *file
)
1308 struct comedi_subdevice
*s
;
1312 lockdep_assert_held(&dev
->mutex
);
1313 if (insn
->insn
& INSN_MASK_SPECIAL
) {
1314 /* a non-subdevice instruction */
1316 switch (insn
->insn
) {
1319 struct timespec64 tv
;
1326 ktime_get_real_ts64(&tv
);
1327 /* unsigned data safe until 2106 */
1328 data
[0] = (unsigned int)tv
.tv_sec
;
1329 data
[1] = tv
.tv_nsec
/ NSEC_PER_USEC
;
1335 if (insn
->n
!= 1 || data
[0] >= 100000) {
1339 udelay(data
[0] / 1000);
1347 if (insn
->subdev
>= dev
->n_subdevices
) {
1348 dev_dbg(dev
->class_dev
,
1349 "%d not usable subdevice\n",
1354 s
= &dev
->subdevices
[insn
->subdev
];
1356 dev_dbg(dev
->class_dev
, "no async\n");
1360 if (!s
->async
->inttrig
) {
1361 dev_dbg(dev
->class_dev
, "no inttrig\n");
1365 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
1369 case INSN_DEVICE_CONFIG
:
1370 ret
= check_insn_device_config_length(insn
, data
);
1374 if (data
[0] == INSN_DEVICE_CONFIG_GET_ROUTES
) {
1376 * data[1] should be the number of _pairs_ that
1377 * the memory can hold.
1379 data
[1] = (insn
->n
- 2) / 2;
1380 ret
= get_valid_routes(dev
, data
);
1384 /* other global device config instructions. */
1385 ret
= dev
->insn_device_config(dev
, insn
, data
);
1388 dev_dbg(dev
->class_dev
, "invalid insn\n");
1393 /* a subdevice instruction */
1394 unsigned int maxdata
;
1396 if (insn
->subdev
>= dev
->n_subdevices
) {
1397 dev_dbg(dev
->class_dev
, "subdevice %d out of range\n",
1402 s
= &dev
->subdevices
[insn
->subdev
];
1404 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1405 dev_dbg(dev
->class_dev
, "%d not usable subdevice\n",
1411 /* are we locked? (ioctl lock) */
1412 if (s
->lock
&& s
->lock
!= file
) {
1413 dev_dbg(dev
->class_dev
, "device locked\n");
1418 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
1421 dev_dbg(dev
->class_dev
, "bad chanspec\n");
1429 /* This looks arbitrary. It is. */
1430 s
->busy
= parse_insn
;
1431 switch (insn
->insn
) {
1433 ret
= s
->insn_read(dev
, s
, insn
, data
);
1434 if (ret
== -ETIMEDOUT
) {
1435 dev_dbg(dev
->class_dev
,
1436 "subdevice %d read instruction timed out\n",
1441 maxdata
= s
->maxdata_list
1442 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1444 for (i
= 0; i
< insn
->n
; ++i
) {
1445 if (data
[i
] > maxdata
) {
1447 dev_dbg(dev
->class_dev
,
1448 "bad data value(s)\n");
1453 ret
= s
->insn_write(dev
, s
, insn
, data
);
1454 if (ret
== -ETIMEDOUT
) {
1455 dev_dbg(dev
->class_dev
,
1456 "subdevice %d write instruction timed out\n",
1466 * Most drivers ignore the base channel in
1467 * insn->chanspec. Fix this here if
1468 * the subdevice has <= 32 channels.
1470 unsigned int orig_mask
= data
[0];
1471 unsigned int shift
= 0;
1473 if (s
->n_chan
<= 32) {
1474 shift
= CR_CHAN(insn
->chanspec
);
1481 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1482 data
[0] = orig_mask
;
1488 ret
= check_insn_config_length(insn
, data
);
1491 ret
= s
->insn_config(dev
, s
, insn
, data
);
1506 * COMEDI_INSNLIST ioctl
1507 * synchronous instruction list
1510 * pointer to comedi_insnlist structure
1513 * comedi_insnlist structure
1514 * array of comedi_insn structures from insnlist->insns pointer
1515 * data (for writes) from insns[].data pointers
1518 * data (for reads) to insns[].data pointers
1520 /* arbitrary limits */
1521 #define MIN_SAMPLES 16
1522 #define MAX_SAMPLES 65536
1523 static int do_insnlist_ioctl(struct comedi_device
*dev
,
1524 struct comedi_insnlist __user
*arg
, void *file
)
1526 struct comedi_insnlist insnlist
;
1527 struct comedi_insn
*insns
= NULL
;
1528 unsigned int *data
= NULL
;
1529 unsigned int max_n_data_required
= MIN_SAMPLES
;
1533 lockdep_assert_held(&dev
->mutex
);
1534 if (copy_from_user(&insnlist
, arg
, sizeof(insnlist
)))
1537 insns
= kcalloc(insnlist
.n_insns
, sizeof(*insns
), GFP_KERNEL
);
1543 if (copy_from_user(insns
, insnlist
.insns
,
1544 sizeof(*insns
) * insnlist
.n_insns
)) {
1545 dev_dbg(dev
->class_dev
, "copy_from_user failed\n");
1550 /* Determine maximum memory needed for all instructions. */
1551 for (i
= 0; i
< insnlist
.n_insns
; ++i
) {
1552 if (insns
[i
].n
> MAX_SAMPLES
) {
1553 dev_dbg(dev
->class_dev
,
1554 "number of samples too large\n");
1558 max_n_data_required
= max(max_n_data_required
, insns
[i
].n
);
1561 /* Allocate scratch space for all instruction data. */
1562 data
= kmalloc_array(max_n_data_required
, sizeof(unsigned int),
1569 for (i
= 0; i
< insnlist
.n_insns
; ++i
) {
1570 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
1571 if (copy_from_user(data
, insns
[i
].data
,
1572 insns
[i
].n
* sizeof(unsigned int))) {
1573 dev_dbg(dev
->class_dev
,
1574 "copy_from_user failed\n");
1579 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
1582 if (insns
[i
].insn
& INSN_MASK_READ
) {
1583 if (copy_to_user(insns
[i
].data
, data
,
1584 insns
[i
].n
* sizeof(unsigned int))) {
1585 dev_dbg(dev
->class_dev
,
1586 "copy_to_user failed\n");
1606 * synchronous instruction
1609 * pointer to comedi_insn structure
1612 * comedi_insn structure
1613 * data (for writes) from insn->data pointer
1616 * data (for reads) to insn->data pointer
1618 static int do_insn_ioctl(struct comedi_device
*dev
,
1619 struct comedi_insn __user
*arg
, void *file
)
1621 struct comedi_insn insn
;
1622 unsigned int *data
= NULL
;
1623 unsigned int n_data
= MIN_SAMPLES
;
1626 lockdep_assert_held(&dev
->mutex
);
1627 if (copy_from_user(&insn
, arg
, sizeof(insn
)))
1630 n_data
= max(n_data
, insn
.n
);
1632 /* This is where the behavior of insn and insnlist deviate. */
1633 if (insn
.n
> MAX_SAMPLES
) {
1634 insn
.n
= MAX_SAMPLES
;
1635 n_data
= MAX_SAMPLES
;
1638 data
= kmalloc_array(n_data
, sizeof(unsigned int), GFP_KERNEL
);
1644 if (insn
.insn
& INSN_MASK_WRITE
) {
1645 if (copy_from_user(data
,
1647 insn
.n
* sizeof(unsigned int))) {
1652 ret
= parse_insn(dev
, &insn
, data
, file
);
1655 if (insn
.insn
& INSN_MASK_READ
) {
1656 if (copy_to_user(insn
.data
,
1658 insn
.n
* sizeof(unsigned int))) {
1671 static int __comedi_get_user_cmd(struct comedi_device
*dev
,
1672 struct comedi_cmd __user
*arg
,
1673 struct comedi_cmd
*cmd
)
1675 struct comedi_subdevice
*s
;
1677 lockdep_assert_held(&dev
->mutex
);
1678 if (copy_from_user(cmd
, arg
, sizeof(*cmd
))) {
1679 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1683 if (cmd
->subdev
>= dev
->n_subdevices
) {
1684 dev_dbg(dev
->class_dev
, "%d no such subdevice\n", cmd
->subdev
);
1688 s
= &dev
->subdevices
[cmd
->subdev
];
1690 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1691 dev_dbg(dev
->class_dev
, "%d not valid subdevice\n",
1696 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1697 dev_dbg(dev
->class_dev
,
1698 "subdevice %d does not support commands\n",
1703 /* make sure channel/gain list isn't too long */
1704 if (cmd
->chanlist_len
> s
->len_chanlist
) {
1705 dev_dbg(dev
->class_dev
, "channel/gain list too long %d > %d\n",
1706 cmd
->chanlist_len
, s
->len_chanlist
);
1711 * Set the CMDF_WRITE flag to the correct state if the subdevice
1712 * supports only "read" commands or only "write" commands.
1714 switch (s
->subdev_flags
& (SDF_CMD_READ
| SDF_CMD_WRITE
)) {
1716 cmd
->flags
&= ~CMDF_WRITE
;
1719 cmd
->flags
|= CMDF_WRITE
;
1728 static int __comedi_get_user_chanlist(struct comedi_device
*dev
,
1729 struct comedi_subdevice
*s
,
1730 unsigned int __user
*user_chanlist
,
1731 struct comedi_cmd
*cmd
)
1733 unsigned int *chanlist
;
1736 lockdep_assert_held(&dev
->mutex
);
1737 cmd
->chanlist
= NULL
;
1738 chanlist
= memdup_user(user_chanlist
,
1739 cmd
->chanlist_len
* sizeof(unsigned int));
1740 if (IS_ERR(chanlist
))
1741 return PTR_ERR(chanlist
);
1743 /* make sure each element in channel/gain list is valid */
1744 ret
= comedi_check_chanlist(s
, cmd
->chanlist_len
, chanlist
);
1750 cmd
->chanlist
= chanlist
;
1757 * asynchronous acquisition command set-up
1760 * pointer to comedi_cmd structure
1763 * comedi_cmd structure
1764 * channel/range list from cmd->chanlist pointer
1767 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1769 static int do_cmd_ioctl(struct comedi_device
*dev
,
1770 struct comedi_cmd __user
*arg
, void *file
)
1772 struct comedi_cmd cmd
;
1773 struct comedi_subdevice
*s
;
1774 struct comedi_async
*async
;
1775 unsigned int __user
*user_chanlist
;
1778 lockdep_assert_held(&dev
->mutex
);
1780 /* get the user's cmd and do some simple validation */
1781 ret
= __comedi_get_user_cmd(dev
, arg
, &cmd
);
1785 /* save user's chanlist pointer so it can be restored later */
1786 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1788 s
= &dev
->subdevices
[cmd
.subdev
];
1791 /* are we locked? (ioctl lock) */
1792 if (s
->lock
&& s
->lock
!= file
) {
1793 dev_dbg(dev
->class_dev
, "subdevice locked\n");
1799 dev_dbg(dev
->class_dev
, "subdevice busy\n");
1803 /* make sure channel/gain list isn't too short */
1804 if (cmd
.chanlist_len
< 1) {
1805 dev_dbg(dev
->class_dev
, "channel/gain list too short %u < 1\n",
1811 async
->cmd
.data
= NULL
;
1813 /* load channel/gain list */
1814 ret
= __comedi_get_user_chanlist(dev
, s
, user_chanlist
, &async
->cmd
);
1818 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1820 if (async
->cmd
.flags
& CMDF_BOGUS
|| ret
) {
1821 dev_dbg(dev
->class_dev
, "test returned %d\n", ret
);
1823 /* restore chanlist pointer before copying back */
1824 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1826 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1827 dev_dbg(dev
->class_dev
, "fault writing cmd\n");
1835 if (!async
->prealloc_bufsz
) {
1837 dev_dbg(dev
->class_dev
, "no buffer (?)\n");
1841 comedi_buf_reset(s
);
1843 async
->cb_mask
= COMEDI_CB_BLOCK
| COMEDI_CB_CANCEL_MASK
;
1844 if (async
->cmd
.flags
& CMDF_WAKE_EOS
)
1845 async
->cb_mask
|= COMEDI_CB_EOS
;
1847 comedi_update_subdevice_runflags(s
, COMEDI_SRF_BUSY_MASK
,
1848 COMEDI_SRF_RUNNING
);
1851 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1852 * race with comedi_read() or comedi_write().
1855 ret
= s
->do_cmd(dev
, s
);
1860 do_become_nonbusy(dev
, s
);
1866 * COMEDI_CMDTEST ioctl
1867 * asynchronous acquisition command testing
1870 * pointer to comedi_cmd structure
1873 * comedi_cmd structure
1874 * channel/range list from cmd->chanlist pointer
1877 * possibly modified comedi_cmd structure
1879 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1880 struct comedi_cmd __user
*arg
, void *file
)
1882 struct comedi_cmd cmd
;
1883 struct comedi_subdevice
*s
;
1884 unsigned int __user
*user_chanlist
;
1887 lockdep_assert_held(&dev
->mutex
);
1889 /* get the user's cmd and do some simple validation */
1890 ret
= __comedi_get_user_cmd(dev
, arg
, &cmd
);
1894 /* save user's chanlist pointer so it can be restored later */
1895 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1897 s
= &dev
->subdevices
[cmd
.subdev
];
1899 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1900 if (user_chanlist
) {
1901 /* load channel/gain list */
1902 ret
= __comedi_get_user_chanlist(dev
, s
, user_chanlist
, &cmd
);
1907 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1909 kfree(cmd
.chanlist
); /* free kernel copy of user chanlist */
1911 /* restore chanlist pointer before copying back */
1912 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1914 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1915 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1935 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1939 unsigned long flags
;
1940 struct comedi_subdevice
*s
;
1942 lockdep_assert_held(&dev
->mutex
);
1943 if (arg
>= dev
->n_subdevices
)
1945 s
= &dev
->subdevices
[arg
];
1947 spin_lock_irqsave(&s
->spin_lock
, flags
);
1948 if (s
->busy
|| s
->lock
)
1952 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1958 * COMEDI_UNLOCK ioctl
1970 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1973 struct comedi_subdevice
*s
;
1975 lockdep_assert_held(&dev
->mutex
);
1976 if (arg
>= dev
->n_subdevices
)
1978 s
= &dev
->subdevices
[arg
];
1983 if (s
->lock
&& s
->lock
!= file
)
1986 if (s
->lock
== file
)
1993 * COMEDI_CANCEL ioctl
1994 * cancel asynchronous acquisition
2005 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned long arg
,
2008 struct comedi_subdevice
*s
;
2010 lockdep_assert_held(&dev
->mutex
);
2011 if (arg
>= dev
->n_subdevices
)
2013 s
= &dev
->subdevices
[arg
];
2020 if (s
->busy
!= file
)
2023 return do_cancel(dev
, s
);
2028 * instructs driver to synchronize buffers
2039 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned long arg
,
2042 struct comedi_subdevice
*s
;
2044 lockdep_assert_held(&dev
->mutex
);
2045 if (arg
>= dev
->n_subdevices
)
2047 s
= &dev
->subdevices
[arg
];
2052 if (s
->busy
!= file
)
2056 return s
->poll(dev
, s
);
2062 * COMEDI_SETRSUBD ioctl
2063 * sets the current "read" subdevice on a per-file basis
2074 static int do_setrsubd_ioctl(struct comedi_device
*dev
, unsigned long arg
,
2077 struct comedi_file
*cfp
= file
->private_data
;
2078 struct comedi_subdevice
*s_old
, *s_new
;
2080 lockdep_assert_held(&dev
->mutex
);
2081 if (arg
>= dev
->n_subdevices
)
2084 s_new
= &dev
->subdevices
[arg
];
2085 s_old
= comedi_file_read_subdevice(file
);
2087 return 0; /* no change */
2089 if (!(s_new
->subdev_flags
& SDF_CMD_READ
))
2093 * Check the file isn't still busy handling a "read" command on the
2094 * old subdevice (if any).
2096 if (s_old
&& s_old
->busy
== file
&& s_old
->async
&&
2097 !(s_old
->async
->cmd
.flags
& CMDF_WRITE
))
2100 WRITE_ONCE(cfp
->read_subdev
, s_new
);
2105 * COMEDI_SETWSUBD ioctl
2106 * sets the current "write" subdevice on a per-file basis
2117 static int do_setwsubd_ioctl(struct comedi_device
*dev
, unsigned long arg
,
2120 struct comedi_file
*cfp
= file
->private_data
;
2121 struct comedi_subdevice
*s_old
, *s_new
;
2123 lockdep_assert_held(&dev
->mutex
);
2124 if (arg
>= dev
->n_subdevices
)
2127 s_new
= &dev
->subdevices
[arg
];
2128 s_old
= comedi_file_write_subdevice(file
);
2130 return 0; /* no change */
2132 if (!(s_new
->subdev_flags
& SDF_CMD_WRITE
))
2136 * Check the file isn't still busy handling a "write" command on the
2137 * old subdevice (if any).
2139 if (s_old
&& s_old
->busy
== file
&& s_old
->async
&&
2140 (s_old
->async
->cmd
.flags
& CMDF_WRITE
))
2143 WRITE_ONCE(cfp
->write_subdev
, s_new
);
2147 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
2150 unsigned int minor
= iminor(file_inode(file
));
2151 struct comedi_file
*cfp
= file
->private_data
;
2152 struct comedi_device
*dev
= cfp
->dev
;
2155 mutex_lock(&dev
->mutex
);
2158 * Device config is special, because it must work on
2159 * an unconfigured device.
2161 if (cmd
== COMEDI_DEVCONFIG
) {
2162 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
2163 /* Device config not appropriate on non-board minors. */
2167 rc
= do_devconfig_ioctl(dev
,
2168 (struct comedi_devconfig __user
*)arg
);
2171 dev
->minor
>= comedi_num_legacy_minors
) {
2173 * Successfully unconfigured a dynamically
2174 * allocated device. Try and remove it.
2176 if (comedi_clear_board_dev(dev
)) {
2177 mutex_unlock(&dev
->mutex
);
2178 comedi_free_board_dev(dev
);
2186 if (!dev
->attached
) {
2187 dev_dbg(dev
->class_dev
, "no driver attached\n");
2193 case COMEDI_BUFCONFIG
:
2194 rc
= do_bufconfig_ioctl(dev
,
2195 (struct comedi_bufconfig __user
*)arg
);
2197 case COMEDI_DEVINFO
:
2198 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
2201 case COMEDI_SUBDINFO
:
2202 rc
= do_subdinfo_ioctl(dev
,
2203 (struct comedi_subdinfo __user
*)arg
,
2206 case COMEDI_CHANINFO
:
2207 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
2209 case COMEDI_RANGEINFO
:
2210 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
2212 case COMEDI_BUFINFO
:
2213 rc
= do_bufinfo_ioctl(dev
,
2214 (struct comedi_bufinfo __user
*)arg
,
2218 rc
= do_lock_ioctl(dev
, arg
, file
);
2221 rc
= do_unlock_ioctl(dev
, arg
, file
);
2224 rc
= do_cancel_ioctl(dev
, arg
, file
);
2227 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
2229 case COMEDI_CMDTEST
:
2230 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
2233 case COMEDI_INSNLIST
:
2234 rc
= do_insnlist_ioctl(dev
,
2235 (struct comedi_insnlist __user
*)arg
,
2239 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
2243 rc
= do_poll_ioctl(dev
, arg
, file
);
2245 case COMEDI_SETRSUBD
:
2246 rc
= do_setrsubd_ioctl(dev
, arg
, file
);
2248 case COMEDI_SETWSUBD
:
2249 rc
= do_setwsubd_ioctl(dev
, arg
, file
);
2257 mutex_unlock(&dev
->mutex
);
2261 static void comedi_vm_open(struct vm_area_struct
*area
)
2263 struct comedi_buf_map
*bm
;
2265 bm
= area
->vm_private_data
;
2266 comedi_buf_map_get(bm
);
2269 static void comedi_vm_close(struct vm_area_struct
*area
)
2271 struct comedi_buf_map
*bm
;
2273 bm
= area
->vm_private_data
;
2274 comedi_buf_map_put(bm
);
2277 static int comedi_vm_access(struct vm_area_struct
*vma
, unsigned long addr
,
2278 void *buf
, int len
, int write
)
2280 struct comedi_buf_map
*bm
= vma
->vm_private_data
;
2281 unsigned long offset
=
2282 addr
- vma
->vm_start
+ (vma
->vm_pgoff
<< PAGE_SHIFT
);
2286 if (len
> vma
->vm_end
- addr
)
2287 len
= vma
->vm_end
- addr
;
2288 return comedi_buf_map_access(bm
, offset
, buf
, len
, write
);
2291 static const struct vm_operations_struct comedi_vm_ops
= {
2292 .open
= comedi_vm_open
,
2293 .close
= comedi_vm_close
,
2294 .access
= comedi_vm_access
,
2297 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2299 struct comedi_file
*cfp
= file
->private_data
;
2300 struct comedi_device
*dev
= cfp
->dev
;
2301 struct comedi_subdevice
*s
;
2302 struct comedi_async
*async
;
2303 struct comedi_buf_map
*bm
= NULL
;
2304 struct comedi_buf_page
*buf
;
2305 unsigned long start
= vma
->vm_start
;
2312 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2313 * and down-reading &dev->attach_lock should normally succeed without
2314 * contention unless the device is in the process of being attached
2317 if (!down_read_trylock(&dev
->attach_lock
))
2320 if (!dev
->attached
) {
2321 dev_dbg(dev
->class_dev
, "no driver attached\n");
2326 if (vma
->vm_flags
& VM_WRITE
)
2327 s
= comedi_file_write_subdevice(file
);
2329 s
= comedi_file_read_subdevice(file
);
2341 if (vma
->vm_pgoff
!= 0) {
2342 dev_dbg(dev
->class_dev
, "mmap() offset must be 0.\n");
2347 size
= vma
->vm_end
- vma
->vm_start
;
2348 if (size
> async
->prealloc_bufsz
) {
2352 if (offset_in_page(size
)) {
2357 n_pages
= vma_pages(vma
);
2359 /* get reference to current buf map (if any) */
2360 bm
= comedi_buf_map_from_subdev_get(s
);
2361 if (!bm
|| n_pages
> bm
->n_pages
) {
2365 if (bm
->dma_dir
!= DMA_NONE
) {
2367 * DMA buffer was allocated as a single block.
2368 * Address is in page_list[0].
2370 buf
= &bm
->page_list
[0];
2371 retval
= dma_mmap_coherent(bm
->dma_hw_dev
, vma
, buf
->virt_addr
,
2372 buf
->dma_addr
, n_pages
* PAGE_SIZE
);
2374 for (i
= 0; i
< n_pages
; ++i
) {
2377 buf
= &bm
->page_list
[i
];
2378 pfn
= page_to_pfn(virt_to_page(buf
->virt_addr
));
2379 retval
= remap_pfn_range(vma
, start
, pfn
, PAGE_SIZE
,
2389 vma
->vm_ops
= &comedi_vm_ops
;
2390 vma
->vm_private_data
= bm
;
2392 vma
->vm_ops
->open(vma
);
2396 up_read(&dev
->attach_lock
);
2397 comedi_buf_map_put(bm
); /* put reference to buf map - okay if NULL */
2401 static __poll_t
comedi_poll(struct file
*file
, poll_table
*wait
)
2404 struct comedi_file
*cfp
= file
->private_data
;
2405 struct comedi_device
*dev
= cfp
->dev
;
2406 struct comedi_subdevice
*s
, *s_read
;
2408 down_read(&dev
->attach_lock
);
2410 if (!dev
->attached
) {
2411 dev_dbg(dev
->class_dev
, "no driver attached\n");
2415 s
= comedi_file_read_subdevice(file
);
2417 if (s
&& s
->async
) {
2418 poll_wait(file
, &s
->async
->wait_head
, wait
);
2419 if (s
->busy
!= file
|| !comedi_is_subdevice_running(s
) ||
2420 (s
->async
->cmd
.flags
& CMDF_WRITE
) ||
2421 comedi_buf_read_n_available(s
) > 0)
2422 mask
|= EPOLLIN
| EPOLLRDNORM
;
2425 s
= comedi_file_write_subdevice(file
);
2426 if (s
&& s
->async
) {
2427 unsigned int bps
= comedi_bytes_per_sample(s
);
2430 poll_wait(file
, &s
->async
->wait_head
, wait
);
2431 if (s
->busy
!= file
|| !comedi_is_subdevice_running(s
) ||
2432 !(s
->async
->cmd
.flags
& CMDF_WRITE
) ||
2433 comedi_buf_write_n_available(s
) >= bps
)
2434 mask
|= EPOLLOUT
| EPOLLWRNORM
;
2438 up_read(&dev
->attach_lock
);
2442 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
2443 size_t nbytes
, loff_t
*offset
)
2445 struct comedi_subdevice
*s
;
2446 struct comedi_async
*async
;
2450 DECLARE_WAITQUEUE(wait
, current
);
2451 struct comedi_file
*cfp
= file
->private_data
;
2452 struct comedi_device
*dev
= cfp
->dev
;
2453 bool become_nonbusy
= false;
2455 unsigned int old_detach_count
;
2457 /* Protect against device detachment during operation. */
2458 down_read(&dev
->attach_lock
);
2459 attach_locked
= true;
2460 old_detach_count
= dev
->detach_count
;
2462 if (!dev
->attached
) {
2463 dev_dbg(dev
->class_dev
, "no driver attached\n");
2468 s
= comedi_file_write_subdevice(file
);
2469 if (!s
|| !s
->async
) {
2475 if (s
->busy
!= file
|| !(async
->cmd
.flags
& CMDF_WRITE
)) {
2480 add_wait_queue(&async
->wait_head
, &wait
);
2481 while (count
== 0 && !retval
) {
2482 unsigned int runflags
;
2483 unsigned int wp
, n1
, n2
;
2485 set_current_state(TASK_INTERRUPTIBLE
);
2487 runflags
= comedi_get_subdevice_runflags(s
);
2488 if (!comedi_is_runflags_running(runflags
)) {
2489 if (comedi_is_runflags_in_error(runflags
))
2491 if (retval
|| nbytes
)
2492 become_nonbusy
= true;
2498 /* Allocate all free buffer space. */
2499 comedi_buf_write_alloc(s
, async
->prealloc_bufsz
);
2500 m
= comedi_buf_write_n_allocated(s
);
2501 n
= min_t(size_t, m
, nbytes
);
2504 if (file
->f_flags
& O_NONBLOCK
) {
2509 if (signal_pending(current
)) {
2510 retval
= -ERESTARTSYS
;
2513 if (s
->busy
!= file
||
2514 !(async
->cmd
.flags
& CMDF_WRITE
)) {
2521 set_current_state(TASK_RUNNING
);
2522 wp
= async
->buf_write_ptr
;
2523 n1
= min(n
, async
->prealloc_bufsz
- wp
);
2525 m
= copy_from_user(async
->prealloc_buf
+ wp
, buf
, n1
);
2529 m
= copy_from_user(async
->prealloc_buf
, buf
+ n1
, n2
);
2534 comedi_buf_write_free(s
, n
);
2541 remove_wait_queue(&async
->wait_head
, &wait
);
2542 set_current_state(TASK_RUNNING
);
2543 if (become_nonbusy
&& count
== 0) {
2544 struct comedi_subdevice
*new_s
;
2547 * To avoid deadlock, cannot acquire dev->mutex
2548 * while dev->attach_lock is held.
2550 up_read(&dev
->attach_lock
);
2551 attach_locked
= false;
2552 mutex_lock(&dev
->mutex
);
2554 * Check device hasn't become detached behind our back.
2555 * Checking dev->detach_count is unchanged ought to be
2556 * sufficient (unless there have been 2**32 detaches in the
2557 * meantime!), but check the subdevice pointer as well just in
2560 * Also check the subdevice is still in a suitable state to
2561 * become non-busy in case it changed behind our back.
2563 new_s
= comedi_file_write_subdevice(file
);
2564 if (dev
->attached
&& old_detach_count
== dev
->detach_count
&&
2565 s
== new_s
&& new_s
->async
== async
&& s
->busy
== file
&&
2566 (async
->cmd
.flags
& CMDF_WRITE
) &&
2567 !comedi_is_subdevice_running(s
))
2568 do_become_nonbusy(dev
, s
);
2569 mutex_unlock(&dev
->mutex
);
2573 up_read(&dev
->attach_lock
);
2575 return count
? count
: retval
;
2578 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
2581 struct comedi_subdevice
*s
;
2582 struct comedi_async
*async
;
2586 DECLARE_WAITQUEUE(wait
, current
);
2587 struct comedi_file
*cfp
= file
->private_data
;
2588 struct comedi_device
*dev
= cfp
->dev
;
2589 unsigned int old_detach_count
;
2590 bool become_nonbusy
= false;
2593 /* Protect against device detachment during operation. */
2594 down_read(&dev
->attach_lock
);
2595 attach_locked
= true;
2596 old_detach_count
= dev
->detach_count
;
2598 if (!dev
->attached
) {
2599 dev_dbg(dev
->class_dev
, "no driver attached\n");
2604 s
= comedi_file_read_subdevice(file
);
2605 if (!s
|| !s
->async
) {
2611 if (s
->busy
!= file
|| (async
->cmd
.flags
& CMDF_WRITE
)) {
2616 add_wait_queue(&async
->wait_head
, &wait
);
2617 while (count
== 0 && !retval
) {
2618 unsigned int rp
, n1
, n2
;
2620 set_current_state(TASK_INTERRUPTIBLE
);
2622 m
= comedi_buf_read_n_available(s
);
2623 n
= min_t(size_t, m
, nbytes
);
2626 unsigned int runflags
=
2627 comedi_get_subdevice_runflags(s
);
2629 if (!comedi_is_runflags_running(runflags
)) {
2630 if (comedi_is_runflags_in_error(runflags
))
2632 if (retval
|| nbytes
)
2633 become_nonbusy
= true;
2638 if (file
->f_flags
& O_NONBLOCK
) {
2643 if (signal_pending(current
)) {
2644 retval
= -ERESTARTSYS
;
2647 if (s
->busy
!= file
||
2648 (async
->cmd
.flags
& CMDF_WRITE
)) {
2655 set_current_state(TASK_RUNNING
);
2656 rp
= async
->buf_read_ptr
;
2657 n1
= min(n
, async
->prealloc_bufsz
- rp
);
2659 m
= copy_to_user(buf
, async
->prealloc_buf
+ rp
, n1
);
2663 m
= copy_to_user(buf
+ n1
, async
->prealloc_buf
, n2
);
2669 comedi_buf_read_alloc(s
, n
);
2670 comedi_buf_read_free(s
, n
);
2677 remove_wait_queue(&async
->wait_head
, &wait
);
2678 set_current_state(TASK_RUNNING
);
2679 if (become_nonbusy
&& count
== 0) {
2680 struct comedi_subdevice
*new_s
;
2683 * To avoid deadlock, cannot acquire dev->mutex
2684 * while dev->attach_lock is held.
2686 up_read(&dev
->attach_lock
);
2687 attach_locked
= false;
2688 mutex_lock(&dev
->mutex
);
2690 * Check device hasn't become detached behind our back.
2691 * Checking dev->detach_count is unchanged ought to be
2692 * sufficient (unless there have been 2**32 detaches in the
2693 * meantime!), but check the subdevice pointer as well just in
2696 * Also check the subdevice is still in a suitable state to
2697 * become non-busy in case it changed behind our back.
2699 new_s
= comedi_file_read_subdevice(file
);
2700 if (dev
->attached
&& old_detach_count
== dev
->detach_count
&&
2701 s
== new_s
&& new_s
->async
== async
&& s
->busy
== file
&&
2702 !(async
->cmd
.flags
& CMDF_WRITE
) &&
2703 !comedi_is_subdevice_running(s
) &&
2704 comedi_buf_read_n_available(s
) == 0)
2705 do_become_nonbusy(dev
, s
);
2706 mutex_unlock(&dev
->mutex
);
2710 up_read(&dev
->attach_lock
);
2712 return count
? count
: retval
;
2715 static int comedi_open(struct inode
*inode
, struct file
*file
)
2717 const unsigned int minor
= iminor(inode
);
2718 struct comedi_file
*cfp
;
2719 struct comedi_device
*dev
= comedi_dev_get_from_minor(minor
);
2723 pr_debug("invalid minor number\n");
2727 cfp
= kzalloc(sizeof(*cfp
), GFP_KERNEL
);
2729 comedi_dev_put(dev
);
2735 mutex_lock(&dev
->mutex
);
2736 if (!dev
->attached
&& !capable(CAP_SYS_ADMIN
)) {
2737 dev_dbg(dev
->class_dev
, "not attached and not CAP_SYS_ADMIN\n");
2741 if (dev
->attached
&& dev
->use_count
== 0) {
2742 if (!try_module_get(dev
->driver
->module
)) {
2747 rc
= dev
->open(dev
);
2749 module_put(dev
->driver
->module
);
2756 file
->private_data
= cfp
;
2757 comedi_file_reset(file
);
2761 mutex_unlock(&dev
->mutex
);
2763 comedi_dev_put(dev
);
2769 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2771 struct comedi_file
*cfp
= file
->private_data
;
2772 struct comedi_device
*dev
= cfp
->dev
;
2774 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2777 static int comedi_close(struct inode
*inode
, struct file
*file
)
2779 struct comedi_file
*cfp
= file
->private_data
;
2780 struct comedi_device
*dev
= cfp
->dev
;
2781 struct comedi_subdevice
*s
= NULL
;
2784 mutex_lock(&dev
->mutex
);
2786 if (dev
->subdevices
) {
2787 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2788 s
= &dev
->subdevices
[i
];
2790 if (s
->busy
== file
)
2792 if (s
->lock
== file
)
2796 if (dev
->attached
&& dev
->use_count
== 1) {
2799 module_put(dev
->driver
->module
);
2804 mutex_unlock(&dev
->mutex
);
2805 comedi_dev_put(dev
);
2811 static const struct file_operations comedi_fops
= {
2812 .owner
= THIS_MODULE
,
2813 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2814 .compat_ioctl
= comedi_compat_ioctl
,
2815 .open
= comedi_open
,
2816 .release
= comedi_close
,
2817 .read
= comedi_read
,
2818 .write
= comedi_write
,
2819 .mmap
= comedi_mmap
,
2820 .poll
= comedi_poll
,
2821 .fasync
= comedi_fasync
,
2822 .llseek
= noop_llseek
,
2826 * comedi_event() - Handle events for asynchronous COMEDI command
2827 * @dev: COMEDI device.
2828 * @s: COMEDI subdevice.
2829 * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
2831 * If an asynchronous COMEDI command is active on the subdevice, process
2832 * any %COMEDI_CB_... event flags that have been set, usually by an
2833 * interrupt handler. These may change the run state of the asynchronous
2834 * command, wake a task, and/or send a %SIGIO signal.
2836 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2838 struct comedi_async
*async
= s
->async
;
2839 unsigned int events
;
2841 unsigned long flags
;
2843 spin_lock_irqsave(&s
->spin_lock
, flags
);
2845 events
= async
->events
;
2847 if (!__comedi_is_subdevice_running(s
)) {
2848 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2852 if (events
& COMEDI_CB_CANCEL_MASK
)
2853 __comedi_clear_subdevice_runflags(s
, COMEDI_SRF_RUNNING
);
2856 * Remember if an error event has occurred, so an error can be
2857 * returned the next time the user does a read() or write().
2859 if (events
& COMEDI_CB_ERROR_MASK
)
2860 __comedi_set_subdevice_runflags(s
, COMEDI_SRF_ERROR
);
2862 if (async
->cb_mask
& events
) {
2863 wake_up_interruptible(&async
->wait_head
);
2864 si_code
= async
->cmd
.flags
& CMDF_WRITE
? POLL_OUT
: POLL_IN
;
2867 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2870 kill_fasync(&dev
->async_queue
, SIGIO
, si_code
);
2872 EXPORT_SYMBOL_GPL(comedi_event
);
2874 /* Note: the ->mutex is pre-locked on successful return */
2875 struct comedi_device
*comedi_alloc_board_minor(struct device
*hardware_device
)
2877 struct comedi_device
*dev
;
2878 struct device
*csdev
;
2881 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2883 return ERR_PTR(-ENOMEM
);
2884 comedi_device_init(dev
);
2885 comedi_set_hw_dev(dev
, hardware_device
);
2886 mutex_lock(&dev
->mutex
);
2887 mutex_lock(&comedi_board_minor_table_lock
);
2888 for (i
= hardware_device
? comedi_num_legacy_minors
: 0;
2889 i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2890 if (!comedi_board_minor_table
[i
]) {
2891 comedi_board_minor_table
[i
] = dev
;
2895 mutex_unlock(&comedi_board_minor_table_lock
);
2896 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2897 mutex_unlock(&dev
->mutex
);
2898 comedi_device_cleanup(dev
);
2899 comedi_dev_put(dev
);
2900 dev_err(hardware_device
,
2901 "ran out of minor numbers for board device files\n");
2902 return ERR_PTR(-EBUSY
);
2905 csdev
= device_create(comedi_class
, hardware_device
,
2906 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2908 dev
->class_dev
= get_device(csdev
);
2910 /* Note: dev->mutex needs to be unlocked by the caller. */
2914 void comedi_release_hardware_device(struct device
*hardware_device
)
2917 struct comedi_device
*dev
;
2919 for (minor
= comedi_num_legacy_minors
; minor
< COMEDI_NUM_BOARD_MINORS
;
2921 mutex_lock(&comedi_board_minor_table_lock
);
2922 dev
= comedi_board_minor_table
[minor
];
2923 if (dev
&& dev
->hw_dev
== hardware_device
) {
2924 comedi_board_minor_table
[minor
] = NULL
;
2925 mutex_unlock(&comedi_board_minor_table_lock
);
2926 comedi_free_board_dev(dev
);
2929 mutex_unlock(&comedi_board_minor_table_lock
);
2933 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2935 struct comedi_device
*dev
= s
->device
;
2936 struct device
*csdev
;
2939 mutex_lock(&comedi_subdevice_minor_table_lock
);
2940 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
) {
2941 if (!comedi_subdevice_minor_table
[i
]) {
2942 comedi_subdevice_minor_table
[i
] = s
;
2946 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2947 if (i
== COMEDI_NUM_SUBDEVICE_MINORS
) {
2948 dev_err(dev
->class_dev
,
2949 "ran out of minor numbers for subdevice files\n");
2952 i
+= COMEDI_NUM_BOARD_MINORS
;
2954 csdev
= device_create(comedi_class
, dev
->class_dev
,
2955 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2956 dev
->minor
, s
->index
);
2958 s
->class_dev
= csdev
;
2963 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2969 if (s
->minor
< COMEDI_NUM_BOARD_MINORS
||
2970 s
->minor
>= COMEDI_NUM_MINORS
)
2973 i
= s
->minor
- COMEDI_NUM_BOARD_MINORS
;
2974 mutex_lock(&comedi_subdevice_minor_table_lock
);
2975 if (s
== comedi_subdevice_minor_table
[i
])
2976 comedi_subdevice_minor_table
[i
] = NULL
;
2977 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2979 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2980 s
->class_dev
= NULL
;
2984 static void comedi_cleanup_board_minors(void)
2986 struct comedi_device
*dev
;
2989 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++) {
2990 dev
= comedi_clear_board_minor(i
);
2991 comedi_free_board_dev(dev
);
2995 static int __init
comedi_init(void)
3000 pr_info("version " COMEDI_RELEASE
" - http://www.comedi.org\n");
3002 if (comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
3003 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
3004 COMEDI_NUM_BOARD_MINORS
);
3008 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
3009 COMEDI_NUM_MINORS
, "comedi");
3013 cdev_init(&comedi_cdev
, &comedi_fops
);
3014 comedi_cdev
.owner
= THIS_MODULE
;
3016 retval
= kobject_set_name(&comedi_cdev
.kobj
, "comedi");
3018 goto out_unregister_chrdev_region
;
3020 retval
= cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0),
3023 goto out_unregister_chrdev_region
;
3025 comedi_class
= class_create(THIS_MODULE
, "comedi");
3026 if (IS_ERR(comedi_class
)) {
3027 retval
= PTR_ERR(comedi_class
);
3028 pr_err("failed to create class\n");
3032 comedi_class
->dev_groups
= comedi_dev_groups
;
3034 /* create devices files for legacy/manual use */
3035 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
3036 struct comedi_device
*dev
;
3038 dev
= comedi_alloc_board_minor(NULL
);
3040 retval
= PTR_ERR(dev
);
3041 goto out_cleanup_board_minors
;
3043 /* comedi_alloc_board_minor() locked the mutex */
3044 lockdep_assert_held(&dev
->mutex
);
3045 mutex_unlock(&dev
->mutex
);
3048 /* XXX requires /proc interface */
3053 out_cleanup_board_minors
:
3054 comedi_cleanup_board_minors();
3055 class_destroy(comedi_class
);
3057 cdev_del(&comedi_cdev
);
3058 out_unregister_chrdev_region
:
3059 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
3062 module_init(comedi_init
);
3064 static void __exit
comedi_cleanup(void)
3066 comedi_cleanup_board_minors();
3067 class_destroy(comedi_class
);
3068 cdev_del(&comedi_cdev
);
3069 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
3071 comedi_proc_cleanup();
3073 module_exit(comedi_cleanup
);
3075 MODULE_AUTHOR("http://www.comedi.org");
3076 MODULE_DESCRIPTION("Comedi core module");
3077 MODULE_LICENSE("GPL");