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/signal.h>
27 #include <linux/fcntl.h>
28 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/poll.h>
32 #include <linux/device.h>
34 #include "comedidev.h"
35 #include <linux/cdev.h>
38 #include <linux/uaccess.h>
40 #include "comedi_internal.h"
43 * comedi_subdevice "runflags"
44 * COMEDI_SRF_RT: DEPRECATED: command is running real-time
45 * COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred
46 * since the last command was started
47 * COMEDI_SRF_RUNNING: command is running
48 * COMEDI_SRF_FREE_SPRIV: free s->private on detach
50 * COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy"
52 #define COMEDI_SRF_RT BIT(1)
53 #define COMEDI_SRF_ERROR BIT(2)
54 #define COMEDI_SRF_RUNNING BIT(27)
55 #define COMEDI_SRF_FREE_SPRIV BIT(31)
57 #define COMEDI_SRF_BUSY_MASK (COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING)
60 * struct comedi_file - Per-file private data for COMEDI device
61 * @dev: COMEDI device.
62 * @read_subdev: Current "read" subdevice.
63 * @write_subdev: Current "write" subdevice.
64 * @last_detach_count: Last known detach count.
65 * @last_attached: Last known attached/detached state.
68 struct comedi_device
*dev
;
69 struct comedi_subdevice
*read_subdev
;
70 struct comedi_subdevice
*write_subdev
;
71 unsigned int last_detach_count
;
75 #define COMEDI_NUM_MINORS 0x100
76 #define COMEDI_NUM_SUBDEVICE_MINORS \
77 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
79 static unsigned short comedi_num_legacy_minors
;
80 module_param(comedi_num_legacy_minors
, ushort
, 0444);
81 MODULE_PARM_DESC(comedi_num_legacy_minors
,
82 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
85 unsigned int comedi_default_buf_size_kb
= CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
;
86 module_param(comedi_default_buf_size_kb
, uint
, 0644);
87 MODULE_PARM_DESC(comedi_default_buf_size_kb
,
88 "default asynchronous buffer size in KiB (default "
89 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
) ")");
91 unsigned int comedi_default_buf_maxsize_kb
92 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
;
93 module_param(comedi_default_buf_maxsize_kb
, uint
, 0644);
94 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb
,
95 "default maximum size of asynchronous buffer in KiB (default "
96 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
) ")");
98 static DEFINE_MUTEX(comedi_board_minor_table_lock
);
99 static struct comedi_device
100 *comedi_board_minor_table
[COMEDI_NUM_BOARD_MINORS
];
102 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock
);
103 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
104 static struct comedi_subdevice
105 *comedi_subdevice_minor_table
[COMEDI_NUM_SUBDEVICE_MINORS
];
107 static struct class *comedi_class
;
108 static struct cdev comedi_cdev
;
110 static void comedi_device_init(struct comedi_device
*dev
)
112 kref_init(&dev
->refcount
);
113 spin_lock_init(&dev
->spinlock
);
114 mutex_init(&dev
->mutex
);
115 init_rwsem(&dev
->attach_lock
);
119 static void comedi_dev_kref_release(struct kref
*kref
)
121 struct comedi_device
*dev
=
122 container_of(kref
, struct comedi_device
, refcount
);
124 mutex_destroy(&dev
->mutex
);
125 put_device(dev
->class_dev
);
130 * comedi_dev_put() - Release a use of a COMEDI device
131 * @dev: COMEDI device.
133 * Must be called when a user of a COMEDI device is finished with it.
134 * When the last user of the COMEDI device calls this function, the
135 * COMEDI device is destroyed.
137 * Return: 1 if the COMEDI device is destroyed by this call or @dev is
138 * NULL, otherwise return 0. Callers must not assume the COMEDI
139 * device is still valid if this function returns 0.
141 int comedi_dev_put(struct comedi_device
*dev
)
144 return kref_put(&dev
->refcount
, comedi_dev_kref_release
);
147 EXPORT_SYMBOL_GPL(comedi_dev_put
);
149 static struct comedi_device
*comedi_dev_get(struct comedi_device
*dev
)
152 kref_get(&dev
->refcount
);
156 static void comedi_device_cleanup(struct comedi_device
*dev
)
158 struct module
*driver_module
= NULL
;
162 mutex_lock(&dev
->mutex
);
164 driver_module
= dev
->driver
->module
;
165 comedi_device_detach(dev
);
166 if (driver_module
&& dev
->use_count
)
167 module_put(driver_module
);
168 mutex_unlock(&dev
->mutex
);
171 static bool comedi_clear_board_dev(struct comedi_device
*dev
)
173 unsigned int i
= dev
->minor
;
174 bool cleared
= false;
176 mutex_lock(&comedi_board_minor_table_lock
);
177 if (dev
== comedi_board_minor_table
[i
]) {
178 comedi_board_minor_table
[i
] = NULL
;
181 mutex_unlock(&comedi_board_minor_table_lock
);
185 static struct comedi_device
*comedi_clear_board_minor(unsigned int minor
)
187 struct comedi_device
*dev
;
189 mutex_lock(&comedi_board_minor_table_lock
);
190 dev
= comedi_board_minor_table
[minor
];
191 comedi_board_minor_table
[minor
] = NULL
;
192 mutex_unlock(&comedi_board_minor_table_lock
);
196 static void comedi_free_board_dev(struct comedi_device
*dev
)
199 comedi_device_cleanup(dev
);
200 if (dev
->class_dev
) {
201 device_destroy(comedi_class
,
202 MKDEV(COMEDI_MAJOR
, dev
->minor
));
208 static struct comedi_subdevice
*
209 comedi_subdevice_from_minor(const struct comedi_device
*dev
, unsigned int minor
)
211 struct comedi_subdevice
*s
;
212 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
214 mutex_lock(&comedi_subdevice_minor_table_lock
);
215 s
= comedi_subdevice_minor_table
[i
];
216 if (s
&& s
->device
!= dev
)
218 mutex_unlock(&comedi_subdevice_minor_table_lock
);
222 static struct comedi_device
*comedi_dev_get_from_board_minor(unsigned int minor
)
224 struct comedi_device
*dev
;
226 mutex_lock(&comedi_board_minor_table_lock
);
227 dev
= comedi_dev_get(comedi_board_minor_table
[minor
]);
228 mutex_unlock(&comedi_board_minor_table_lock
);
232 static struct comedi_device
*
233 comedi_dev_get_from_subdevice_minor(unsigned int minor
)
235 struct comedi_device
*dev
;
236 struct comedi_subdevice
*s
;
237 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
239 mutex_lock(&comedi_subdevice_minor_table_lock
);
240 s
= comedi_subdevice_minor_table
[i
];
241 dev
= comedi_dev_get(s
? s
->device
: NULL
);
242 mutex_unlock(&comedi_subdevice_minor_table_lock
);
247 * comedi_dev_get_from_minor() - Get COMEDI device by minor device number
248 * @minor: Minor device number.
250 * Finds the COMEDI device associated with the minor device number, if any,
251 * and increments its reference count. The COMEDI device is prevented from
252 * being freed until a matching call is made to comedi_dev_put().
254 * Return: A pointer to the COMEDI device if it exists, with its usage
255 * reference incremented. Return NULL if no COMEDI device exists with the
256 * specified minor device number.
258 struct comedi_device
*comedi_dev_get_from_minor(unsigned int minor
)
260 if (minor
< COMEDI_NUM_BOARD_MINORS
)
261 return comedi_dev_get_from_board_minor(minor
);
263 return comedi_dev_get_from_subdevice_minor(minor
);
265 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor
);
267 static struct comedi_subdevice
*
268 comedi_read_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
270 struct comedi_subdevice
*s
;
272 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
273 s
= comedi_subdevice_from_minor(dev
, minor
);
274 if (!s
|| (s
->subdev_flags
& SDF_CMD_READ
))
277 return dev
->read_subdev
;
280 static struct comedi_subdevice
*
281 comedi_write_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
283 struct comedi_subdevice
*s
;
285 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
286 s
= comedi_subdevice_from_minor(dev
, minor
);
287 if (!s
|| (s
->subdev_flags
& SDF_CMD_WRITE
))
290 return dev
->write_subdev
;
293 static void comedi_file_reset(struct file
*file
)
295 struct comedi_file
*cfp
= file
->private_data
;
296 struct comedi_device
*dev
= cfp
->dev
;
297 struct comedi_subdevice
*s
, *read_s
, *write_s
;
298 unsigned int minor
= iminor(file_inode(file
));
300 read_s
= dev
->read_subdev
;
301 write_s
= dev
->write_subdev
;
302 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
303 s
= comedi_subdevice_from_minor(dev
, minor
);
304 if (!s
|| s
->subdev_flags
& SDF_CMD_READ
)
306 if (!s
|| s
->subdev_flags
& SDF_CMD_WRITE
)
309 cfp
->last_attached
= dev
->attached
;
310 cfp
->last_detach_count
= dev
->detach_count
;
311 WRITE_ONCE(cfp
->read_subdev
, read_s
);
312 WRITE_ONCE(cfp
->write_subdev
, write_s
);
315 static void comedi_file_check(struct file
*file
)
317 struct comedi_file
*cfp
= file
->private_data
;
318 struct comedi_device
*dev
= cfp
->dev
;
320 if (cfp
->last_attached
!= dev
->attached
||
321 cfp
->last_detach_count
!= dev
->detach_count
)
322 comedi_file_reset(file
);
325 static struct comedi_subdevice
*comedi_file_read_subdevice(struct file
*file
)
327 struct comedi_file
*cfp
= file
->private_data
;
329 comedi_file_check(file
);
330 return READ_ONCE(cfp
->read_subdev
);
333 static struct comedi_subdevice
*comedi_file_write_subdevice(struct file
*file
)
335 struct comedi_file
*cfp
= file
->private_data
;
337 comedi_file_check(file
);
338 return READ_ONCE(cfp
->write_subdev
);
341 static int resize_async_buffer(struct comedi_device
*dev
,
342 struct comedi_subdevice
*s
,
343 unsigned int new_size
)
345 struct comedi_async
*async
= s
->async
;
348 if (new_size
> async
->max_bufsize
)
352 dev_dbg(dev
->class_dev
,
353 "subdevice is busy, cannot resize buffer\n");
356 if (comedi_buf_is_mmapped(s
)) {
357 dev_dbg(dev
->class_dev
,
358 "subdevice is mmapped, cannot resize buffer\n");
362 /* make sure buffer is an integral number of pages (we round up) */
363 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
365 retval
= comedi_buf_alloc(dev
, s
, new_size
);
370 retval
= s
->buf_change(dev
, s
);
375 dev_dbg(dev
->class_dev
, "subd %d buffer resized to %i bytes\n",
376 s
->index
, async
->prealloc_bufsz
);
380 /* sysfs attribute files */
382 static ssize_t
max_read_buffer_kb_show(struct device
*csdev
,
383 struct device_attribute
*attr
, char *buf
)
385 unsigned int minor
= MINOR(csdev
->devt
);
386 struct comedi_device
*dev
;
387 struct comedi_subdevice
*s
;
388 unsigned int size
= 0;
390 dev
= comedi_dev_get_from_minor(minor
);
394 mutex_lock(&dev
->mutex
);
395 s
= comedi_read_subdevice(dev
, minor
);
396 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
397 size
= s
->async
->max_bufsize
/ 1024;
398 mutex_unlock(&dev
->mutex
);
401 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
404 static ssize_t
max_read_buffer_kb_store(struct device
*csdev
,
405 struct device_attribute
*attr
,
406 const char *buf
, size_t count
)
408 unsigned int minor
= MINOR(csdev
->devt
);
409 struct comedi_device
*dev
;
410 struct comedi_subdevice
*s
;
414 err
= kstrtouint(buf
, 10, &size
);
417 if (size
> (UINT_MAX
/ 1024))
421 dev
= comedi_dev_get_from_minor(minor
);
425 mutex_lock(&dev
->mutex
);
426 s
= comedi_read_subdevice(dev
, minor
);
427 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
428 s
->async
->max_bufsize
= size
;
431 mutex_unlock(&dev
->mutex
);
434 return err
? err
: count
;
436 static DEVICE_ATTR_RW(max_read_buffer_kb
);
438 static ssize_t
read_buffer_kb_show(struct device
*csdev
,
439 struct device_attribute
*attr
, char *buf
)
441 unsigned int minor
= MINOR(csdev
->devt
);
442 struct comedi_device
*dev
;
443 struct comedi_subdevice
*s
;
444 unsigned int size
= 0;
446 dev
= comedi_dev_get_from_minor(minor
);
450 mutex_lock(&dev
->mutex
);
451 s
= comedi_read_subdevice(dev
, minor
);
452 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
453 size
= s
->async
->prealloc_bufsz
/ 1024;
454 mutex_unlock(&dev
->mutex
);
457 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
460 static ssize_t
read_buffer_kb_store(struct device
*csdev
,
461 struct device_attribute
*attr
,
462 const char *buf
, size_t count
)
464 unsigned int minor
= MINOR(csdev
->devt
);
465 struct comedi_device
*dev
;
466 struct comedi_subdevice
*s
;
470 err
= kstrtouint(buf
, 10, &size
);
473 if (size
> (UINT_MAX
/ 1024))
477 dev
= comedi_dev_get_from_minor(minor
);
481 mutex_lock(&dev
->mutex
);
482 s
= comedi_read_subdevice(dev
, minor
);
483 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
484 err
= resize_async_buffer(dev
, s
, size
);
487 mutex_unlock(&dev
->mutex
);
490 return err
? err
: count
;
492 static DEVICE_ATTR_RW(read_buffer_kb
);
494 static ssize_t
max_write_buffer_kb_show(struct device
*csdev
,
495 struct device_attribute
*attr
,
498 unsigned int minor
= MINOR(csdev
->devt
);
499 struct comedi_device
*dev
;
500 struct comedi_subdevice
*s
;
501 unsigned int size
= 0;
503 dev
= comedi_dev_get_from_minor(minor
);
507 mutex_lock(&dev
->mutex
);
508 s
= comedi_write_subdevice(dev
, minor
);
509 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
510 size
= s
->async
->max_bufsize
/ 1024;
511 mutex_unlock(&dev
->mutex
);
514 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
517 static ssize_t
max_write_buffer_kb_store(struct device
*csdev
,
518 struct device_attribute
*attr
,
519 const char *buf
, size_t count
)
521 unsigned int minor
= MINOR(csdev
->devt
);
522 struct comedi_device
*dev
;
523 struct comedi_subdevice
*s
;
527 err
= kstrtouint(buf
, 10, &size
);
530 if (size
> (UINT_MAX
/ 1024))
534 dev
= comedi_dev_get_from_minor(minor
);
538 mutex_lock(&dev
->mutex
);
539 s
= comedi_write_subdevice(dev
, minor
);
540 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
541 s
->async
->max_bufsize
= size
;
544 mutex_unlock(&dev
->mutex
);
547 return err
? err
: count
;
549 static DEVICE_ATTR_RW(max_write_buffer_kb
);
551 static ssize_t
write_buffer_kb_show(struct device
*csdev
,
552 struct device_attribute
*attr
, char *buf
)
554 unsigned int minor
= MINOR(csdev
->devt
);
555 struct comedi_device
*dev
;
556 struct comedi_subdevice
*s
;
557 unsigned int size
= 0;
559 dev
= comedi_dev_get_from_minor(minor
);
563 mutex_lock(&dev
->mutex
);
564 s
= comedi_write_subdevice(dev
, minor
);
565 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
566 size
= s
->async
->prealloc_bufsz
/ 1024;
567 mutex_unlock(&dev
->mutex
);
570 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
573 static ssize_t
write_buffer_kb_store(struct device
*csdev
,
574 struct device_attribute
*attr
,
575 const char *buf
, size_t count
)
577 unsigned int minor
= MINOR(csdev
->devt
);
578 struct comedi_device
*dev
;
579 struct comedi_subdevice
*s
;
583 err
= kstrtouint(buf
, 10, &size
);
586 if (size
> (UINT_MAX
/ 1024))
590 dev
= comedi_dev_get_from_minor(minor
);
594 mutex_lock(&dev
->mutex
);
595 s
= comedi_write_subdevice(dev
, minor
);
596 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
597 err
= resize_async_buffer(dev
, s
, size
);
600 mutex_unlock(&dev
->mutex
);
603 return err
? err
: count
;
605 static DEVICE_ATTR_RW(write_buffer_kb
);
607 static struct attribute
*comedi_dev_attrs
[] = {
608 &dev_attr_max_read_buffer_kb
.attr
,
609 &dev_attr_read_buffer_kb
.attr
,
610 &dev_attr_max_write_buffer_kb
.attr
,
611 &dev_attr_write_buffer_kb
.attr
,
614 ATTRIBUTE_GROUPS(comedi_dev
);
616 static void __comedi_clear_subdevice_runflags(struct comedi_subdevice
*s
,
619 s
->runflags
&= ~bits
;
622 static void __comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
628 static void comedi_update_subdevice_runflags(struct comedi_subdevice
*s
,
634 spin_lock_irqsave(&s
->spin_lock
, flags
);
635 __comedi_clear_subdevice_runflags(s
, mask
);
636 __comedi_set_subdevice_runflags(s
, bits
& mask
);
637 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
640 static unsigned int __comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
645 static unsigned int comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
648 unsigned int runflags
;
650 spin_lock_irqsave(&s
->spin_lock
, flags
);
651 runflags
= __comedi_get_subdevice_runflags(s
);
652 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
656 static bool comedi_is_runflags_running(unsigned int runflags
)
658 return runflags
& COMEDI_SRF_RUNNING
;
661 static bool comedi_is_runflags_in_error(unsigned int runflags
)
663 return runflags
& COMEDI_SRF_ERROR
;
667 * comedi_is_subdevice_running() - Check if async command running on subdevice
668 * @s: COMEDI subdevice.
670 * Return: %true if an asynchronous COMEDI command is active on the
671 * subdevice, else %false.
673 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
)
675 unsigned int runflags
= comedi_get_subdevice_runflags(s
);
677 return comedi_is_runflags_running(runflags
);
679 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running
);
681 static bool __comedi_is_subdevice_running(struct comedi_subdevice
*s
)
683 unsigned int runflags
= __comedi_get_subdevice_runflags(s
);
685 return comedi_is_runflags_running(runflags
);
688 bool comedi_can_auto_free_spriv(struct comedi_subdevice
*s
)
690 unsigned int runflags
= __comedi_get_subdevice_runflags(s
);
692 return runflags
& COMEDI_SRF_FREE_SPRIV
;
696 * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
697 * @s: COMEDI subdevice.
699 * Mark the subdevice as having a pointer to private data that can be
700 * automatically freed when the COMEDI device is detached from the low-level
703 void comedi_set_spriv_auto_free(struct comedi_subdevice
*s
)
705 __comedi_set_subdevice_runflags(s
, COMEDI_SRF_FREE_SPRIV
);
707 EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free
);
710 * comedi_alloc_spriv - Allocate memory for the subdevice private data
711 * @s: COMEDI subdevice.
712 * @size: Size of the memory to allocate.
714 * Allocate memory for the subdevice private data and point @s->private
715 * to it. The memory will be freed automatically when the COMEDI device
716 * is detached from the low-level driver.
718 * Return: A pointer to the allocated memory @s->private on success.
719 * Return NULL on failure.
721 void *comedi_alloc_spriv(struct comedi_subdevice
*s
, size_t size
)
723 s
->private = kzalloc(size
, GFP_KERNEL
);
725 comedi_set_spriv_auto_free(s
);
728 EXPORT_SYMBOL_GPL(comedi_alloc_spriv
);
731 * This function restores a subdevice to an idle state.
733 static void do_become_nonbusy(struct comedi_device
*dev
,
734 struct comedi_subdevice
*s
)
736 struct comedi_async
*async
= s
->async
;
738 comedi_update_subdevice_runflags(s
, COMEDI_SRF_RUNNING
, 0);
741 async
->inttrig
= NULL
;
742 kfree(async
->cmd
.chanlist
);
743 async
->cmd
.chanlist
= NULL
;
745 wake_up_interruptible_all(&async
->wait_head
);
747 dev_err(dev
->class_dev
,
748 "BUG: (?) do_become_nonbusy called with async=NULL\n");
753 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
757 if (comedi_is_subdevice_running(s
) && s
->cancel
)
758 ret
= s
->cancel(dev
, s
);
760 do_become_nonbusy(dev
, s
);
765 void comedi_device_cancel_all(struct comedi_device
*dev
)
767 struct comedi_subdevice
*s
;
773 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
774 s
= &dev
->subdevices
[i
];
780 static int is_device_busy(struct comedi_device
*dev
)
782 struct comedi_subdevice
*s
;
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 if (!capable(CAP_SYS_ADMIN
))
821 if (is_device_busy(dev
))
824 struct module
*driver_module
= dev
->driver
->module
;
826 comedi_device_detach(dev
);
827 module_put(driver_module
);
832 if (copy_from_user(&it
, arg
, sizeof(it
)))
835 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
837 if (it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
838 dev_warn(dev
->class_dev
,
839 "comedi_config --init_data is deprecated\n");
843 if (dev
->minor
>= comedi_num_legacy_minors
)
844 /* don't re-use dynamically allocated comedi devices */
847 /* This increments the driver module count on success. */
848 return comedi_device_attach(dev
, &it
);
852 * COMEDI_BUFCONFIG ioctl
853 * buffer configuration
856 * pointer to comedi_bufconfig structure
859 * comedi_bufconfig structure
862 * modified comedi_bufconfig structure
864 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
865 struct comedi_bufconfig __user
*arg
)
867 struct comedi_bufconfig bc
;
868 struct comedi_async
*async
;
869 struct comedi_subdevice
*s
;
872 if (copy_from_user(&bc
, arg
, sizeof(bc
)))
875 if (bc
.subdevice
>= dev
->n_subdevices
)
878 s
= &dev
->subdevices
[bc
.subdevice
];
882 dev_dbg(dev
->class_dev
,
883 "subdevice does not have async capability\n");
889 if (bc
.maximum_size
) {
890 if (!capable(CAP_SYS_ADMIN
))
893 async
->max_bufsize
= bc
.maximum_size
;
897 retval
= resize_async_buffer(dev
, s
, bc
.size
);
902 bc
.size
= async
->prealloc_bufsz
;
903 bc
.maximum_size
= async
->max_bufsize
;
906 if (copy_to_user(arg
, &bc
, sizeof(bc
)))
913 * COMEDI_DEVINFO ioctl
917 * pointer to comedi_devinfo structure
923 * comedi_devinfo structure
925 static int do_devinfo_ioctl(struct comedi_device
*dev
,
926 struct comedi_devinfo __user
*arg
,
929 struct comedi_subdevice
*s
;
930 struct comedi_devinfo devinfo
;
932 memset(&devinfo
, 0, sizeof(devinfo
));
934 /* fill devinfo structure */
935 devinfo
.version_code
= COMEDI_VERSION_CODE
;
936 devinfo
.n_subdevs
= dev
->n_subdevices
;
937 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
938 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
940 s
= comedi_file_read_subdevice(file
);
942 devinfo
.read_subdevice
= s
->index
;
944 devinfo
.read_subdevice
= -1;
946 s
= comedi_file_write_subdevice(file
);
948 devinfo
.write_subdevice
= s
->index
;
950 devinfo
.write_subdevice
= -1;
952 if (copy_to_user(arg
, &devinfo
, sizeof(devinfo
)))
959 * COMEDI_SUBDINFO ioctl
963 * pointer to array of comedi_subdinfo structures
969 * array of comedi_subdinfo structures
971 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
972 struct comedi_subdinfo __user
*arg
, void *file
)
975 struct comedi_subdinfo
*tmp
, *us
;
976 struct comedi_subdevice
*s
;
978 tmp
= kcalloc(dev
->n_subdevices
, sizeof(*tmp
), GFP_KERNEL
);
982 /* fill subdinfo structs */
983 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
984 s
= &dev
->subdevices
[i
];
988 us
->n_chan
= s
->n_chan
;
989 us
->subd_flags
= s
->subdev_flags
;
990 if (comedi_is_subdevice_running(s
))
991 us
->subd_flags
|= SDF_RUNNING
;
992 #define TIMER_nanosec 5 /* backwards compatibility */
993 us
->timer_type
= TIMER_nanosec
;
994 us
->len_chanlist
= s
->len_chanlist
;
995 us
->maxdata
= s
->maxdata
;
996 if (s
->range_table
) {
998 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
1000 us
->range_type
= 0; /* XXX */
1004 us
->subd_flags
|= SDF_BUSY
;
1005 if (s
->busy
== file
)
1006 us
->subd_flags
|= SDF_BUSY_OWNER
;
1008 us
->subd_flags
|= SDF_LOCKED
;
1009 if (s
->lock
== file
)
1010 us
->subd_flags
|= SDF_LOCK_OWNER
;
1011 if (!s
->maxdata
&& s
->maxdata_list
)
1012 us
->subd_flags
|= SDF_MAXDATA
;
1013 if (s
->range_table_list
)
1014 us
->subd_flags
|= SDF_RANGETYPE
;
1016 us
->subd_flags
|= SDF_CMD
;
1018 if (s
->insn_bits
!= &insn_inval
)
1019 us
->insn_bits_support
= COMEDI_SUPPORTED
;
1021 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
1024 ret
= copy_to_user(arg
, tmp
, dev
->n_subdevices
* sizeof(*tmp
));
1028 return ret
? -EFAULT
: 0;
1032 * COMEDI_CHANINFO ioctl
1033 * subdevice channel info
1036 * pointer to comedi_chaninfo structure
1039 * comedi_chaninfo structure
1042 * array of maxdata values to chaninfo->maxdata_list if requested
1043 * array of range table lengths to chaninfo->range_table_list if requested
1045 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
1046 struct comedi_chaninfo __user
*arg
)
1048 struct comedi_subdevice
*s
;
1049 struct comedi_chaninfo it
;
1051 if (copy_from_user(&it
, arg
, sizeof(it
)))
1054 if (it
.subdev
>= dev
->n_subdevices
)
1056 s
= &dev
->subdevices
[it
.subdev
];
1058 if (it
.maxdata_list
) {
1059 if (s
->maxdata
|| !s
->maxdata_list
)
1061 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
1062 s
->n_chan
* sizeof(unsigned int)))
1067 return -EINVAL
; /* flaglist not supported */
1072 if (!s
->range_table_list
)
1074 for (i
= 0; i
< s
->n_chan
; i
++) {
1077 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
1078 (s
->range_table_list
[i
]->length
);
1079 if (put_user(x
, it
.rangelist
+ i
))
1088 * COMEDI_BUFINFO ioctl
1089 * buffer information
1092 * pointer to comedi_bufinfo structure
1095 * comedi_bufinfo structure
1098 * modified comedi_bufinfo structure
1100 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
1101 struct comedi_bufinfo __user
*arg
, void *file
)
1103 struct comedi_bufinfo bi
;
1104 struct comedi_subdevice
*s
;
1105 struct comedi_async
*async
;
1106 unsigned int runflags
;
1108 bool become_nonbusy
= false;
1110 if (copy_from_user(&bi
, arg
, sizeof(bi
)))
1113 if (bi
.subdevice
>= dev
->n_subdevices
)
1116 s
= &dev
->subdevices
[bi
.subdevice
];
1120 if (!async
|| s
->busy
!= file
)
1123 runflags
= comedi_get_subdevice_runflags(s
);
1124 if (!(async
->cmd
.flags
& CMDF_WRITE
)) {
1125 /* command was set up in "read" direction */
1126 if (bi
.bytes_read
) {
1127 comedi_buf_read_alloc(s
, bi
.bytes_read
);
1128 bi
.bytes_read
= comedi_buf_read_free(s
, bi
.bytes_read
);
1131 * If nothing left to read, and command has stopped, and
1132 * {"read" position not updated or command stopped normally},
1133 * then become non-busy.
1135 if (comedi_buf_read_n_available(s
) == 0 &&
1136 !comedi_is_runflags_running(runflags
) &&
1137 (bi
.bytes_read
== 0 ||
1138 !comedi_is_runflags_in_error(runflags
))) {
1139 become_nonbusy
= true;
1140 if (comedi_is_runflags_in_error(runflags
))
1143 bi
.bytes_written
= 0;
1145 /* command was set up in "write" direction */
1146 if (!comedi_is_runflags_running(runflags
)) {
1147 bi
.bytes_written
= 0;
1148 become_nonbusy
= true;
1149 if (comedi_is_runflags_in_error(runflags
))
1151 } else if (bi
.bytes_written
) {
1152 comedi_buf_write_alloc(s
, bi
.bytes_written
);
1154 comedi_buf_write_free(s
, bi
.bytes_written
);
1159 bi
.buf_write_count
= async
->buf_write_count
;
1160 bi
.buf_write_ptr
= async
->buf_write_ptr
;
1161 bi
.buf_read_count
= async
->buf_read_count
;
1162 bi
.buf_read_ptr
= async
->buf_read_ptr
;
1165 do_become_nonbusy(dev
, s
);
1170 if (copy_to_user(arg
, &bi
, sizeof(bi
)))
1176 static int check_insn_config_length(struct comedi_insn
*insn
,
1183 case INSN_CONFIG_DIO_OUTPUT
:
1184 case INSN_CONFIG_DIO_INPUT
:
1185 case INSN_CONFIG_DISARM
:
1186 case INSN_CONFIG_RESET
:
1190 case INSN_CONFIG_ARM
:
1191 case INSN_CONFIG_DIO_QUERY
:
1192 case INSN_CONFIG_BLOCK_SIZE
:
1193 case INSN_CONFIG_FILTER
:
1194 case INSN_CONFIG_SERIAL_CLOCK
:
1195 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
1196 case INSN_CONFIG_ALT_SOURCE
:
1197 case INSN_CONFIG_SET_COUNTER_MODE
:
1198 case INSN_CONFIG_8254_READ_STATUS
:
1199 case INSN_CONFIG_SET_ROUTING
:
1200 case INSN_CONFIG_GET_ROUTING
:
1201 case INSN_CONFIG_GET_PWM_STATUS
:
1202 case INSN_CONFIG_PWM_SET_PERIOD
:
1203 case INSN_CONFIG_PWM_GET_PERIOD
:
1207 case INSN_CONFIG_SET_GATE_SRC
:
1208 case INSN_CONFIG_GET_GATE_SRC
:
1209 case INSN_CONFIG_SET_CLOCK_SRC
:
1210 case INSN_CONFIG_GET_CLOCK_SRC
:
1211 case INSN_CONFIG_SET_OTHER_SRC
:
1212 case INSN_CONFIG_GET_COUNTER_STATUS
:
1213 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
1214 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
1215 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
1219 case INSN_CONFIG_PWM_OUTPUT
:
1220 case INSN_CONFIG_ANALOG_TRIG
:
1224 case INSN_CONFIG_DIGITAL_TRIG
:
1229 * by default we allow the insn since we don't have checks for
1230 * all possible cases yet
1233 pr_warn("No check for data length of config insn id %i is implemented\n",
1235 pr_warn("Add a check to %s in %s\n", __func__
, __FILE__
);
1236 pr_warn("Assuming n=%i is correct\n", insn
->n
);
1242 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
1243 unsigned int *data
, void *file
)
1245 struct comedi_subdevice
*s
;
1249 if (insn
->insn
& INSN_MASK_SPECIAL
) {
1250 /* a non-subdevice instruction */
1252 switch (insn
->insn
) {
1255 struct timespec64 tv
;
1262 ktime_get_real_ts64(&tv
);
1263 /* unsigned data safe until 2106 */
1264 data
[0] = (unsigned int)tv
.tv_sec
;
1265 data
[1] = tv
.tv_nsec
/ NSEC_PER_USEC
;
1271 if (insn
->n
!= 1 || data
[0] >= 100000) {
1275 udelay(data
[0] / 1000);
1283 if (insn
->subdev
>= dev
->n_subdevices
) {
1284 dev_dbg(dev
->class_dev
,
1285 "%d not usable subdevice\n",
1290 s
= &dev
->subdevices
[insn
->subdev
];
1292 dev_dbg(dev
->class_dev
, "no async\n");
1296 if (!s
->async
->inttrig
) {
1297 dev_dbg(dev
->class_dev
, "no inttrig\n");
1301 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
1306 dev_dbg(dev
->class_dev
, "invalid insn\n");
1311 /* a subdevice instruction */
1312 unsigned int maxdata
;
1314 if (insn
->subdev
>= dev
->n_subdevices
) {
1315 dev_dbg(dev
->class_dev
, "subdevice %d out of range\n",
1320 s
= &dev
->subdevices
[insn
->subdev
];
1322 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1323 dev_dbg(dev
->class_dev
, "%d not usable subdevice\n",
1329 /* are we locked? (ioctl lock) */
1330 if (s
->lock
&& s
->lock
!= file
) {
1331 dev_dbg(dev
->class_dev
, "device locked\n");
1336 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
1339 dev_dbg(dev
->class_dev
, "bad chanspec\n");
1347 /* This looks arbitrary. It is. */
1348 s
->busy
= parse_insn
;
1349 switch (insn
->insn
) {
1351 ret
= s
->insn_read(dev
, s
, insn
, data
);
1352 if (ret
== -ETIMEDOUT
) {
1353 dev_dbg(dev
->class_dev
,
1354 "subdevice %d read instruction timed out\n",
1359 maxdata
= s
->maxdata_list
1360 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1362 for (i
= 0; i
< insn
->n
; ++i
) {
1363 if (data
[i
] > maxdata
) {
1365 dev_dbg(dev
->class_dev
,
1366 "bad data value(s)\n");
1371 ret
= s
->insn_write(dev
, s
, insn
, data
);
1372 if (ret
== -ETIMEDOUT
) {
1373 dev_dbg(dev
->class_dev
,
1374 "subdevice %d write instruction timed out\n",
1384 * Most drivers ignore the base channel in
1385 * insn->chanspec. Fix this here if
1386 * the subdevice has <= 32 channels.
1388 unsigned int orig_mask
= data
[0];
1389 unsigned int shift
= 0;
1391 if (s
->n_chan
<= 32) {
1392 shift
= CR_CHAN(insn
->chanspec
);
1399 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1400 data
[0] = orig_mask
;
1406 ret
= check_insn_config_length(insn
, data
);
1409 ret
= s
->insn_config(dev
, s
, insn
, data
);
1424 * COMEDI_INSNLIST ioctl
1425 * synchronous instruction list
1428 * pointer to comedi_insnlist structure
1431 * comedi_insnlist structure
1432 * array of comedi_insn structures from insnlist->insns pointer
1433 * data (for writes) from insns[].data pointers
1436 * data (for reads) to insns[].data pointers
1438 /* arbitrary limits */
1439 #define MAX_SAMPLES 256
1440 static int do_insnlist_ioctl(struct comedi_device
*dev
,
1441 struct comedi_insnlist __user
*arg
, void *file
)
1443 struct comedi_insnlist insnlist
;
1444 struct comedi_insn
*insns
= NULL
;
1445 unsigned int *data
= NULL
;
1449 if (copy_from_user(&insnlist
, arg
, sizeof(insnlist
)))
1452 data
= kmalloc_array(MAX_SAMPLES
, sizeof(unsigned int), GFP_KERNEL
);
1458 insns
= kcalloc(insnlist
.n_insns
, sizeof(*insns
), GFP_KERNEL
);
1464 if (copy_from_user(insns
, insnlist
.insns
,
1465 sizeof(*insns
) * insnlist
.n_insns
)) {
1466 dev_dbg(dev
->class_dev
, "copy_from_user failed\n");
1471 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
1472 if (insns
[i
].n
> MAX_SAMPLES
) {
1473 dev_dbg(dev
->class_dev
,
1474 "number of samples too large\n");
1478 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
1479 if (copy_from_user(data
, insns
[i
].data
,
1480 insns
[i
].n
* sizeof(unsigned int))) {
1481 dev_dbg(dev
->class_dev
,
1482 "copy_from_user failed\n");
1487 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
1490 if (insns
[i
].insn
& INSN_MASK_READ
) {
1491 if (copy_to_user(insns
[i
].data
, data
,
1492 insns
[i
].n
* sizeof(unsigned int))) {
1493 dev_dbg(dev
->class_dev
,
1494 "copy_to_user failed\n");
1514 * synchronous instruction
1517 * pointer to comedi_insn structure
1520 * comedi_insn structure
1521 * data (for writes) from insn->data pointer
1524 * data (for reads) to insn->data pointer
1526 static int do_insn_ioctl(struct comedi_device
*dev
,
1527 struct comedi_insn __user
*arg
, void *file
)
1529 struct comedi_insn insn
;
1530 unsigned int *data
= NULL
;
1533 data
= kmalloc_array(MAX_SAMPLES
, sizeof(unsigned int), GFP_KERNEL
);
1539 if (copy_from_user(&insn
, arg
, sizeof(insn
))) {
1544 /* This is where the behavior of insn and insnlist deviate. */
1545 if (insn
.n
> MAX_SAMPLES
)
1546 insn
.n
= MAX_SAMPLES
;
1547 if (insn
.insn
& INSN_MASK_WRITE
) {
1548 if (copy_from_user(data
,
1550 insn
.n
* sizeof(unsigned int))) {
1555 ret
= parse_insn(dev
, &insn
, data
, file
);
1558 if (insn
.insn
& INSN_MASK_READ
) {
1559 if (copy_to_user(insn
.data
,
1561 insn
.n
* sizeof(unsigned int))) {
1574 static int __comedi_get_user_cmd(struct comedi_device
*dev
,
1575 struct comedi_cmd __user
*arg
,
1576 struct comedi_cmd
*cmd
)
1578 struct comedi_subdevice
*s
;
1580 if (copy_from_user(cmd
, arg
, sizeof(*cmd
))) {
1581 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1585 if (cmd
->subdev
>= dev
->n_subdevices
) {
1586 dev_dbg(dev
->class_dev
, "%d no such subdevice\n", cmd
->subdev
);
1590 s
= &dev
->subdevices
[cmd
->subdev
];
1592 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1593 dev_dbg(dev
->class_dev
, "%d not valid subdevice\n",
1598 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1599 dev_dbg(dev
->class_dev
,
1600 "subdevice %d does not support commands\n",
1605 /* make sure channel/gain list isn't too long */
1606 if (cmd
->chanlist_len
> s
->len_chanlist
) {
1607 dev_dbg(dev
->class_dev
, "channel/gain list too long %d > %d\n",
1608 cmd
->chanlist_len
, s
->len_chanlist
);
1613 * Set the CMDF_WRITE flag to the correct state if the subdevice
1614 * supports only "read" commands or only "write" commands.
1616 switch (s
->subdev_flags
& (SDF_CMD_READ
| SDF_CMD_WRITE
)) {
1618 cmd
->flags
&= ~CMDF_WRITE
;
1621 cmd
->flags
|= CMDF_WRITE
;
1630 static int __comedi_get_user_chanlist(struct comedi_device
*dev
,
1631 struct comedi_subdevice
*s
,
1632 unsigned int __user
*user_chanlist
,
1633 struct comedi_cmd
*cmd
)
1635 unsigned int *chanlist
;
1638 cmd
->chanlist
= NULL
;
1639 chanlist
= memdup_user(user_chanlist
,
1640 cmd
->chanlist_len
* sizeof(unsigned int));
1641 if (IS_ERR(chanlist
))
1642 return PTR_ERR(chanlist
);
1644 /* make sure each element in channel/gain list is valid */
1645 ret
= comedi_check_chanlist(s
, cmd
->chanlist_len
, chanlist
);
1651 cmd
->chanlist
= chanlist
;
1658 * asynchronous acquisition command set-up
1661 * pointer to comedi_cmd structure
1664 * comedi_cmd structure
1665 * channel/range list from cmd->chanlist pointer
1668 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1670 static int do_cmd_ioctl(struct comedi_device
*dev
,
1671 struct comedi_cmd __user
*arg
, void *file
)
1673 struct comedi_cmd cmd
;
1674 struct comedi_subdevice
*s
;
1675 struct comedi_async
*async
;
1676 unsigned int __user
*user_chanlist
;
1679 /* get the user's cmd and do some simple validation */
1680 ret
= __comedi_get_user_cmd(dev
, arg
, &cmd
);
1684 /* save user's chanlist pointer so it can be restored later */
1685 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1687 s
= &dev
->subdevices
[cmd
.subdev
];
1690 /* are we locked? (ioctl lock) */
1691 if (s
->lock
&& s
->lock
!= file
) {
1692 dev_dbg(dev
->class_dev
, "subdevice locked\n");
1698 dev_dbg(dev
->class_dev
, "subdevice busy\n");
1702 /* make sure channel/gain list isn't too short */
1703 if (cmd
.chanlist_len
< 1) {
1704 dev_dbg(dev
->class_dev
, "channel/gain list too short %u < 1\n",
1710 async
->cmd
.data
= NULL
;
1712 /* load channel/gain list */
1713 ret
= __comedi_get_user_chanlist(dev
, s
, user_chanlist
, &async
->cmd
);
1717 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1719 if (async
->cmd
.flags
& CMDF_BOGUS
|| ret
) {
1720 dev_dbg(dev
->class_dev
, "test returned %d\n", ret
);
1722 /* restore chanlist pointer before copying back */
1723 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1725 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1726 dev_dbg(dev
->class_dev
, "fault writing cmd\n");
1734 if (!async
->prealloc_bufsz
) {
1736 dev_dbg(dev
->class_dev
, "no buffer (?)\n");
1740 comedi_buf_reset(s
);
1742 async
->cb_mask
= COMEDI_CB_BLOCK
| COMEDI_CB_CANCEL_MASK
;
1743 if (async
->cmd
.flags
& CMDF_WAKE_EOS
)
1744 async
->cb_mask
|= COMEDI_CB_EOS
;
1746 comedi_update_subdevice_runflags(s
, COMEDI_SRF_BUSY_MASK
,
1747 COMEDI_SRF_RUNNING
);
1750 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1751 * race with comedi_read() or comedi_write().
1754 ret
= s
->do_cmd(dev
, s
);
1759 do_become_nonbusy(dev
, s
);
1765 * COMEDI_CMDTEST ioctl
1766 * asynchronous acquisition command testing
1769 * pointer to comedi_cmd structure
1772 * comedi_cmd structure
1773 * channel/range list from cmd->chanlist pointer
1776 * possibly modified comedi_cmd structure
1778 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1779 struct comedi_cmd __user
*arg
, void *file
)
1781 struct comedi_cmd cmd
;
1782 struct comedi_subdevice
*s
;
1783 unsigned int __user
*user_chanlist
;
1786 /* get the user's cmd and do some simple validation */
1787 ret
= __comedi_get_user_cmd(dev
, arg
, &cmd
);
1791 /* save user's chanlist pointer so it can be restored later */
1792 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1794 s
= &dev
->subdevices
[cmd
.subdev
];
1796 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1797 if (user_chanlist
) {
1798 /* load channel/gain list */
1799 ret
= __comedi_get_user_chanlist(dev
, s
, user_chanlist
, &cmd
);
1804 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1806 kfree(cmd
.chanlist
); /* free kernel copy of user chanlist */
1808 /* restore chanlist pointer before copying back */
1809 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1811 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1812 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1832 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1836 unsigned long flags
;
1837 struct comedi_subdevice
*s
;
1839 if (arg
>= dev
->n_subdevices
)
1841 s
= &dev
->subdevices
[arg
];
1843 spin_lock_irqsave(&s
->spin_lock
, flags
);
1844 if (s
->busy
|| s
->lock
)
1848 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1854 * COMEDI_UNLOCK ioctl
1866 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1869 struct comedi_subdevice
*s
;
1871 if (arg
>= dev
->n_subdevices
)
1873 s
= &dev
->subdevices
[arg
];
1878 if (s
->lock
&& s
->lock
!= file
)
1881 if (s
->lock
== file
)
1888 * COMEDI_CANCEL ioctl
1889 * cancel asynchronous acquisition
1900 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1903 struct comedi_subdevice
*s
;
1905 if (arg
>= dev
->n_subdevices
)
1907 s
= &dev
->subdevices
[arg
];
1914 if (s
->busy
!= file
)
1917 return do_cancel(dev
, s
);
1922 * instructs driver to synchronize buffers
1933 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1936 struct comedi_subdevice
*s
;
1938 if (arg
>= dev
->n_subdevices
)
1940 s
= &dev
->subdevices
[arg
];
1945 if (s
->busy
!= file
)
1949 return s
->poll(dev
, s
);
1955 * COMEDI_SETRSUBD ioctl
1956 * sets the current "read" subdevice on a per-file basis
1967 static int do_setrsubd_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1970 struct comedi_file
*cfp
= file
->private_data
;
1971 struct comedi_subdevice
*s_old
, *s_new
;
1973 if (arg
>= dev
->n_subdevices
)
1976 s_new
= &dev
->subdevices
[arg
];
1977 s_old
= comedi_file_read_subdevice(file
);
1979 return 0; /* no change */
1981 if (!(s_new
->subdev_flags
& SDF_CMD_READ
))
1985 * Check the file isn't still busy handling a "read" command on the
1986 * old subdevice (if any).
1988 if (s_old
&& s_old
->busy
== file
&& s_old
->async
&&
1989 !(s_old
->async
->cmd
.flags
& CMDF_WRITE
))
1992 WRITE_ONCE(cfp
->read_subdev
, s_new
);
1997 * COMEDI_SETWSUBD ioctl
1998 * sets the current "write" subdevice on a per-file basis
2009 static int do_setwsubd_ioctl(struct comedi_device
*dev
, unsigned long arg
,
2012 struct comedi_file
*cfp
= file
->private_data
;
2013 struct comedi_subdevice
*s_old
, *s_new
;
2015 if (arg
>= dev
->n_subdevices
)
2018 s_new
= &dev
->subdevices
[arg
];
2019 s_old
= comedi_file_write_subdevice(file
);
2021 return 0; /* no change */
2023 if (!(s_new
->subdev_flags
& SDF_CMD_WRITE
))
2027 * Check the file isn't still busy handling a "write" command on the
2028 * old subdevice (if any).
2030 if (s_old
&& s_old
->busy
== file
&& s_old
->async
&&
2031 (s_old
->async
->cmd
.flags
& CMDF_WRITE
))
2034 WRITE_ONCE(cfp
->write_subdev
, s_new
);
2038 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
2041 unsigned int minor
= iminor(file_inode(file
));
2042 struct comedi_file
*cfp
= file
->private_data
;
2043 struct comedi_device
*dev
= cfp
->dev
;
2046 mutex_lock(&dev
->mutex
);
2049 * Device config is special, because it must work on
2050 * an unconfigured device.
2052 if (cmd
== COMEDI_DEVCONFIG
) {
2053 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
2054 /* Device config not appropriate on non-board minors. */
2058 rc
= do_devconfig_ioctl(dev
,
2059 (struct comedi_devconfig __user
*)arg
);
2062 dev
->minor
>= comedi_num_legacy_minors
) {
2064 * Successfully unconfigured a dynamically
2065 * allocated device. Try and remove it.
2067 if (comedi_clear_board_dev(dev
)) {
2068 mutex_unlock(&dev
->mutex
);
2069 comedi_free_board_dev(dev
);
2077 if (!dev
->attached
) {
2078 dev_dbg(dev
->class_dev
, "no driver attached\n");
2084 case COMEDI_BUFCONFIG
:
2085 rc
= do_bufconfig_ioctl(dev
,
2086 (struct comedi_bufconfig __user
*)arg
);
2088 case COMEDI_DEVINFO
:
2089 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
2092 case COMEDI_SUBDINFO
:
2093 rc
= do_subdinfo_ioctl(dev
,
2094 (struct comedi_subdinfo __user
*)arg
,
2097 case COMEDI_CHANINFO
:
2098 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
2100 case COMEDI_RANGEINFO
:
2101 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
2103 case COMEDI_BUFINFO
:
2104 rc
= do_bufinfo_ioctl(dev
,
2105 (struct comedi_bufinfo __user
*)arg
,
2109 rc
= do_lock_ioctl(dev
, arg
, file
);
2112 rc
= do_unlock_ioctl(dev
, arg
, file
);
2115 rc
= do_cancel_ioctl(dev
, arg
, file
);
2118 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
2120 case COMEDI_CMDTEST
:
2121 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
2124 case COMEDI_INSNLIST
:
2125 rc
= do_insnlist_ioctl(dev
,
2126 (struct comedi_insnlist __user
*)arg
,
2130 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
2134 rc
= do_poll_ioctl(dev
, arg
, file
);
2136 case COMEDI_SETRSUBD
:
2137 rc
= do_setrsubd_ioctl(dev
, arg
, file
);
2139 case COMEDI_SETWSUBD
:
2140 rc
= do_setwsubd_ioctl(dev
, arg
, file
);
2148 mutex_unlock(&dev
->mutex
);
2152 static void comedi_vm_open(struct vm_area_struct
*area
)
2154 struct comedi_buf_map
*bm
;
2156 bm
= area
->vm_private_data
;
2157 comedi_buf_map_get(bm
);
2160 static void comedi_vm_close(struct vm_area_struct
*area
)
2162 struct comedi_buf_map
*bm
;
2164 bm
= area
->vm_private_data
;
2165 comedi_buf_map_put(bm
);
2168 static int comedi_vm_access(struct vm_area_struct
*vma
, unsigned long addr
,
2169 void *buf
, int len
, int write
)
2171 struct comedi_buf_map
*bm
= vma
->vm_private_data
;
2172 unsigned long offset
=
2173 addr
- vma
->vm_start
+ (vma
->vm_pgoff
<< PAGE_SHIFT
);
2177 if (len
> vma
->vm_end
- addr
)
2178 len
= vma
->vm_end
- addr
;
2179 return comedi_buf_map_access(bm
, offset
, buf
, len
, write
);
2182 static const struct vm_operations_struct comedi_vm_ops
= {
2183 .open
= comedi_vm_open
,
2184 .close
= comedi_vm_close
,
2185 .access
= comedi_vm_access
,
2188 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2190 struct comedi_file
*cfp
= file
->private_data
;
2191 struct comedi_device
*dev
= cfp
->dev
;
2192 struct comedi_subdevice
*s
;
2193 struct comedi_async
*async
;
2194 struct comedi_buf_map
*bm
= NULL
;
2195 unsigned long start
= vma
->vm_start
;
2202 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2203 * and down-reading &dev->attach_lock should normally succeed without
2204 * contention unless the device is in the process of being attached
2207 if (!down_read_trylock(&dev
->attach_lock
))
2210 if (!dev
->attached
) {
2211 dev_dbg(dev
->class_dev
, "no driver attached\n");
2216 if (vma
->vm_flags
& VM_WRITE
)
2217 s
= comedi_file_write_subdevice(file
);
2219 s
= comedi_file_read_subdevice(file
);
2231 if (vma
->vm_pgoff
!= 0) {
2232 dev_dbg(dev
->class_dev
, "mmap() offset must be 0.\n");
2237 size
= vma
->vm_end
- vma
->vm_start
;
2238 if (size
> async
->prealloc_bufsz
) {
2242 if (offset_in_page(size
)) {
2247 n_pages
= vma_pages(vma
);
2249 /* get reference to current buf map (if any) */
2250 bm
= comedi_buf_map_from_subdev_get(s
);
2251 if (!bm
|| n_pages
> bm
->n_pages
) {
2255 for (i
= 0; i
< n_pages
; ++i
) {
2256 struct comedi_buf_page
*buf
= &bm
->page_list
[i
];
2258 if (remap_pfn_range(vma
, start
,
2259 page_to_pfn(virt_to_page(buf
->virt_addr
)),
2260 PAGE_SIZE
, PAGE_SHARED
)) {
2267 vma
->vm_ops
= &comedi_vm_ops
;
2268 vma
->vm_private_data
= bm
;
2270 vma
->vm_ops
->open(vma
);
2274 up_read(&dev
->attach_lock
);
2275 comedi_buf_map_put(bm
); /* put reference to buf map - okay if NULL */
2279 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
2281 unsigned int mask
= 0;
2282 struct comedi_file
*cfp
= file
->private_data
;
2283 struct comedi_device
*dev
= cfp
->dev
;
2284 struct comedi_subdevice
*s
, *s_read
;
2286 down_read(&dev
->attach_lock
);
2288 if (!dev
->attached
) {
2289 dev_dbg(dev
->class_dev
, "no driver attached\n");
2293 s
= comedi_file_read_subdevice(file
);
2295 if (s
&& s
->async
) {
2296 poll_wait(file
, &s
->async
->wait_head
, wait
);
2297 if (s
->busy
!= file
|| !comedi_is_subdevice_running(s
) ||
2298 (s
->async
->cmd
.flags
& CMDF_WRITE
) ||
2299 comedi_buf_read_n_available(s
) > 0)
2300 mask
|= POLLIN
| POLLRDNORM
;
2303 s
= comedi_file_write_subdevice(file
);
2304 if (s
&& s
->async
) {
2305 unsigned int bps
= comedi_bytes_per_sample(s
);
2308 poll_wait(file
, &s
->async
->wait_head
, wait
);
2309 if (s
->busy
!= file
|| !comedi_is_subdevice_running(s
) ||
2310 !(s
->async
->cmd
.flags
& CMDF_WRITE
) ||
2311 comedi_buf_write_n_available(s
) >= bps
)
2312 mask
|= POLLOUT
| POLLWRNORM
;
2316 up_read(&dev
->attach_lock
);
2320 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
2321 size_t nbytes
, loff_t
*offset
)
2323 struct comedi_subdevice
*s
;
2324 struct comedi_async
*async
;
2328 DECLARE_WAITQUEUE(wait
, current
);
2329 struct comedi_file
*cfp
= file
->private_data
;
2330 struct comedi_device
*dev
= cfp
->dev
;
2331 bool become_nonbusy
= false;
2333 unsigned int old_detach_count
;
2335 /* Protect against device detachment during operation. */
2336 down_read(&dev
->attach_lock
);
2337 attach_locked
= true;
2338 old_detach_count
= dev
->detach_count
;
2340 if (!dev
->attached
) {
2341 dev_dbg(dev
->class_dev
, "no driver attached\n");
2346 s
= comedi_file_write_subdevice(file
);
2347 if (!s
|| !s
->async
) {
2353 if (s
->busy
!= file
|| !(async
->cmd
.flags
& CMDF_WRITE
)) {
2358 add_wait_queue(&async
->wait_head
, &wait
);
2359 while (count
== 0 && !retval
) {
2360 unsigned int runflags
;
2361 unsigned int wp
, n1
, n2
;
2363 set_current_state(TASK_INTERRUPTIBLE
);
2365 runflags
= comedi_get_subdevice_runflags(s
);
2366 if (!comedi_is_runflags_running(runflags
)) {
2367 if (comedi_is_runflags_in_error(runflags
))
2369 if (retval
|| nbytes
)
2370 become_nonbusy
= true;
2376 /* Allocate all free buffer space. */
2377 comedi_buf_write_alloc(s
, async
->prealloc_bufsz
);
2378 m
= comedi_buf_write_n_allocated(s
);
2379 n
= min_t(size_t, m
, nbytes
);
2382 if (file
->f_flags
& O_NONBLOCK
) {
2387 if (signal_pending(current
)) {
2388 retval
= -ERESTARTSYS
;
2391 if (s
->busy
!= file
||
2392 !(async
->cmd
.flags
& CMDF_WRITE
)) {
2399 set_current_state(TASK_RUNNING
);
2400 wp
= async
->buf_write_ptr
;
2401 n1
= min(n
, async
->prealloc_bufsz
- wp
);
2403 m
= copy_from_user(async
->prealloc_buf
+ wp
, buf
, n1
);
2407 m
= copy_from_user(async
->prealloc_buf
, buf
+ n1
, n2
);
2412 comedi_buf_write_free(s
, n
);
2419 remove_wait_queue(&async
->wait_head
, &wait
);
2420 set_current_state(TASK_RUNNING
);
2421 if (become_nonbusy
&& count
== 0) {
2422 struct comedi_subdevice
*new_s
;
2425 * To avoid deadlock, cannot acquire dev->mutex
2426 * while dev->attach_lock is held.
2428 up_read(&dev
->attach_lock
);
2429 attach_locked
= false;
2430 mutex_lock(&dev
->mutex
);
2432 * Check device hasn't become detached behind our back.
2433 * Checking dev->detach_count is unchanged ought to be
2434 * sufficient (unless there have been 2**32 detaches in the
2435 * meantime!), but check the subdevice pointer as well just in
2438 * Also check the subdevice is still in a suitable state to
2439 * become non-busy in case it changed behind our back.
2441 new_s
= comedi_file_write_subdevice(file
);
2442 if (dev
->attached
&& old_detach_count
== dev
->detach_count
&&
2443 s
== new_s
&& new_s
->async
== async
&& s
->busy
== file
&&
2444 (async
->cmd
.flags
& CMDF_WRITE
) &&
2445 !comedi_is_subdevice_running(s
))
2446 do_become_nonbusy(dev
, s
);
2447 mutex_unlock(&dev
->mutex
);
2451 up_read(&dev
->attach_lock
);
2453 return count
? count
: retval
;
2456 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
2459 struct comedi_subdevice
*s
;
2460 struct comedi_async
*async
;
2464 DECLARE_WAITQUEUE(wait
, current
);
2465 struct comedi_file
*cfp
= file
->private_data
;
2466 struct comedi_device
*dev
= cfp
->dev
;
2467 unsigned int old_detach_count
;
2468 bool become_nonbusy
= false;
2471 /* Protect against device detachment during operation. */
2472 down_read(&dev
->attach_lock
);
2473 attach_locked
= true;
2474 old_detach_count
= dev
->detach_count
;
2476 if (!dev
->attached
) {
2477 dev_dbg(dev
->class_dev
, "no driver attached\n");
2482 s
= comedi_file_read_subdevice(file
);
2483 if (!s
|| !s
->async
) {
2489 if (s
->busy
!= file
|| (async
->cmd
.flags
& CMDF_WRITE
)) {
2494 add_wait_queue(&async
->wait_head
, &wait
);
2495 while (count
== 0 && !retval
) {
2496 unsigned int rp
, n1
, n2
;
2498 set_current_state(TASK_INTERRUPTIBLE
);
2500 m
= comedi_buf_read_n_available(s
);
2501 n
= min_t(size_t, m
, nbytes
);
2504 unsigned int runflags
=
2505 comedi_get_subdevice_runflags(s
);
2507 if (!comedi_is_runflags_running(runflags
)) {
2508 if (comedi_is_runflags_in_error(runflags
))
2510 if (retval
|| nbytes
)
2511 become_nonbusy
= true;
2516 if (file
->f_flags
& O_NONBLOCK
) {
2521 if (signal_pending(current
)) {
2522 retval
= -ERESTARTSYS
;
2525 if (s
->busy
!= file
||
2526 (async
->cmd
.flags
& CMDF_WRITE
)) {
2533 set_current_state(TASK_RUNNING
);
2534 rp
= async
->buf_read_ptr
;
2535 n1
= min(n
, async
->prealloc_bufsz
- rp
);
2537 m
= copy_to_user(buf
, async
->prealloc_buf
+ rp
, n1
);
2541 m
= copy_to_user(buf
+ n1
, async
->prealloc_buf
, n2
);
2547 comedi_buf_read_alloc(s
, n
);
2548 comedi_buf_read_free(s
, n
);
2555 remove_wait_queue(&async
->wait_head
, &wait
);
2556 set_current_state(TASK_RUNNING
);
2557 if (become_nonbusy
&& count
== 0) {
2558 struct comedi_subdevice
*new_s
;
2561 * To avoid deadlock, cannot acquire dev->mutex
2562 * while dev->attach_lock is held.
2564 up_read(&dev
->attach_lock
);
2565 attach_locked
= false;
2566 mutex_lock(&dev
->mutex
);
2568 * Check device hasn't become detached behind our back.
2569 * Checking dev->detach_count is unchanged ought to be
2570 * sufficient (unless there have been 2**32 detaches in the
2571 * meantime!), but check the subdevice pointer as well just in
2574 * Also check the subdevice is still in a suitable state to
2575 * become non-busy in case it changed behind our back.
2577 new_s
= comedi_file_read_subdevice(file
);
2578 if (dev
->attached
&& old_detach_count
== dev
->detach_count
&&
2579 s
== new_s
&& new_s
->async
== async
&& s
->busy
== file
&&
2580 !(async
->cmd
.flags
& CMDF_WRITE
) &&
2581 !comedi_is_subdevice_running(s
) &&
2582 comedi_buf_read_n_available(s
) == 0)
2583 do_become_nonbusy(dev
, s
);
2584 mutex_unlock(&dev
->mutex
);
2588 up_read(&dev
->attach_lock
);
2590 return count
? count
: retval
;
2593 static int comedi_open(struct inode
*inode
, struct file
*file
)
2595 const unsigned int minor
= iminor(inode
);
2596 struct comedi_file
*cfp
;
2597 struct comedi_device
*dev
= comedi_dev_get_from_minor(minor
);
2601 pr_debug("invalid minor number\n");
2605 cfp
= kzalloc(sizeof(*cfp
), GFP_KERNEL
);
2611 mutex_lock(&dev
->mutex
);
2612 if (!dev
->attached
&& !capable(CAP_SYS_ADMIN
)) {
2613 dev_dbg(dev
->class_dev
, "not attached and not CAP_SYS_ADMIN\n");
2617 if (dev
->attached
&& dev
->use_count
== 0) {
2618 if (!try_module_get(dev
->driver
->module
)) {
2623 rc
= dev
->open(dev
);
2625 module_put(dev
->driver
->module
);
2632 file
->private_data
= cfp
;
2633 comedi_file_reset(file
);
2637 mutex_unlock(&dev
->mutex
);
2639 comedi_dev_put(dev
);
2645 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2647 struct comedi_file
*cfp
= file
->private_data
;
2648 struct comedi_device
*dev
= cfp
->dev
;
2650 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2653 static int comedi_close(struct inode
*inode
, struct file
*file
)
2655 struct comedi_file
*cfp
= file
->private_data
;
2656 struct comedi_device
*dev
= cfp
->dev
;
2657 struct comedi_subdevice
*s
= NULL
;
2660 mutex_lock(&dev
->mutex
);
2662 if (dev
->subdevices
) {
2663 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2664 s
= &dev
->subdevices
[i
];
2666 if (s
->busy
== file
)
2668 if (s
->lock
== file
)
2672 if (dev
->attached
&& dev
->use_count
== 1) {
2675 module_put(dev
->driver
->module
);
2680 mutex_unlock(&dev
->mutex
);
2681 comedi_dev_put(dev
);
2687 static const struct file_operations comedi_fops
= {
2688 .owner
= THIS_MODULE
,
2689 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2690 .compat_ioctl
= comedi_compat_ioctl
,
2691 .open
= comedi_open
,
2692 .release
= comedi_close
,
2693 .read
= comedi_read
,
2694 .write
= comedi_write
,
2695 .mmap
= comedi_mmap
,
2696 .poll
= comedi_poll
,
2697 .fasync
= comedi_fasync
,
2698 .llseek
= noop_llseek
,
2702 * comedi_event() - Handle events for asynchronous COMEDI command
2703 * @dev: COMEDI device.
2704 * @s: COMEDI subdevice.
2705 * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
2707 * If an asynchronous COMEDI command is active on the subdevice, process
2708 * any %COMEDI_CB_... event flags that have been set, usually by an
2709 * interrupt handler. These may change the run state of the asynchronous
2710 * command, wake a task, and/or send a %SIGIO signal.
2712 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2714 struct comedi_async
*async
= s
->async
;
2715 unsigned int events
;
2717 unsigned long flags
;
2719 spin_lock_irqsave(&s
->spin_lock
, flags
);
2721 events
= async
->events
;
2723 if (!__comedi_is_subdevice_running(s
)) {
2724 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2728 if (events
& COMEDI_CB_CANCEL_MASK
)
2729 __comedi_clear_subdevice_runflags(s
, COMEDI_SRF_RUNNING
);
2732 * Remember if an error event has occurred, so an error can be
2733 * returned the next time the user does a read() or write().
2735 if (events
& COMEDI_CB_ERROR_MASK
)
2736 __comedi_set_subdevice_runflags(s
, COMEDI_SRF_ERROR
);
2738 if (async
->cb_mask
& events
) {
2739 wake_up_interruptible(&async
->wait_head
);
2740 si_code
= async
->cmd
.flags
& CMDF_WRITE
? POLL_OUT
: POLL_IN
;
2743 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2746 kill_fasync(&dev
->async_queue
, SIGIO
, si_code
);
2748 EXPORT_SYMBOL_GPL(comedi_event
);
2750 /* Note: the ->mutex is pre-locked on successful return */
2751 struct comedi_device
*comedi_alloc_board_minor(struct device
*hardware_device
)
2753 struct comedi_device
*dev
;
2754 struct device
*csdev
;
2757 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2759 return ERR_PTR(-ENOMEM
);
2760 comedi_device_init(dev
);
2761 comedi_set_hw_dev(dev
, hardware_device
);
2762 mutex_lock(&dev
->mutex
);
2763 mutex_lock(&comedi_board_minor_table_lock
);
2764 for (i
= hardware_device
? comedi_num_legacy_minors
: 0;
2765 i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2766 if (!comedi_board_minor_table
[i
]) {
2767 comedi_board_minor_table
[i
] = dev
;
2771 mutex_unlock(&comedi_board_minor_table_lock
);
2772 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2773 mutex_unlock(&dev
->mutex
);
2774 comedi_device_cleanup(dev
);
2775 comedi_dev_put(dev
);
2776 dev_err(hardware_device
,
2777 "ran out of minor numbers for board device files\n");
2778 return ERR_PTR(-EBUSY
);
2781 csdev
= device_create(comedi_class
, hardware_device
,
2782 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2784 dev
->class_dev
= get_device(csdev
);
2786 /* Note: dev->mutex needs to be unlocked by the caller. */
2790 void comedi_release_hardware_device(struct device
*hardware_device
)
2793 struct comedi_device
*dev
;
2795 for (minor
= comedi_num_legacy_minors
; minor
< COMEDI_NUM_BOARD_MINORS
;
2797 mutex_lock(&comedi_board_minor_table_lock
);
2798 dev
= comedi_board_minor_table
[minor
];
2799 if (dev
&& dev
->hw_dev
== hardware_device
) {
2800 comedi_board_minor_table
[minor
] = NULL
;
2801 mutex_unlock(&comedi_board_minor_table_lock
);
2802 comedi_free_board_dev(dev
);
2805 mutex_unlock(&comedi_board_minor_table_lock
);
2809 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2811 struct comedi_device
*dev
= s
->device
;
2812 struct device
*csdev
;
2815 mutex_lock(&comedi_subdevice_minor_table_lock
);
2816 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
) {
2817 if (!comedi_subdevice_minor_table
[i
]) {
2818 comedi_subdevice_minor_table
[i
] = s
;
2822 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2823 if (i
== COMEDI_NUM_SUBDEVICE_MINORS
) {
2824 dev_err(dev
->class_dev
,
2825 "ran out of minor numbers for subdevice files\n");
2828 i
+= COMEDI_NUM_BOARD_MINORS
;
2830 csdev
= device_create(comedi_class
, dev
->class_dev
,
2831 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2832 dev
->minor
, s
->index
);
2834 s
->class_dev
= csdev
;
2839 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2845 if (s
->minor
< COMEDI_NUM_BOARD_MINORS
||
2846 s
->minor
>= COMEDI_NUM_MINORS
)
2849 i
= s
->minor
- COMEDI_NUM_BOARD_MINORS
;
2850 mutex_lock(&comedi_subdevice_minor_table_lock
);
2851 if (s
== comedi_subdevice_minor_table
[i
])
2852 comedi_subdevice_minor_table
[i
] = NULL
;
2853 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2855 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2856 s
->class_dev
= NULL
;
2860 static void comedi_cleanup_board_minors(void)
2862 struct comedi_device
*dev
;
2865 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++) {
2866 dev
= comedi_clear_board_minor(i
);
2867 comedi_free_board_dev(dev
);
2871 static int __init
comedi_init(void)
2876 pr_info("version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2878 if (comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2879 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2880 COMEDI_NUM_BOARD_MINORS
);
2884 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2885 COMEDI_NUM_MINORS
, "comedi");
2889 cdev_init(&comedi_cdev
, &comedi_fops
);
2890 comedi_cdev
.owner
= THIS_MODULE
;
2892 retval
= kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2894 goto out_unregister_chrdev_region
;
2896 retval
= cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0),
2899 goto out_unregister_chrdev_region
;
2901 comedi_class
= class_create(THIS_MODULE
, "comedi");
2902 if (IS_ERR(comedi_class
)) {
2903 retval
= PTR_ERR(comedi_class
);
2904 pr_err("failed to create class\n");
2908 comedi_class
->dev_groups
= comedi_dev_groups
;
2910 /* create devices files for legacy/manual use */
2911 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2912 struct comedi_device
*dev
;
2914 dev
= comedi_alloc_board_minor(NULL
);
2916 retval
= PTR_ERR(dev
);
2917 goto out_cleanup_board_minors
;
2919 /* comedi_alloc_board_minor() locked the mutex */
2920 mutex_unlock(&dev
->mutex
);
2923 /* XXX requires /proc interface */
2928 out_cleanup_board_minors
:
2929 comedi_cleanup_board_minors();
2930 class_destroy(comedi_class
);
2932 cdev_del(&comedi_cdev
);
2933 out_unregister_chrdev_region
:
2934 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2937 module_init(comedi_init
);
2939 static void __exit
comedi_cleanup(void)
2941 comedi_cleanup_board_minors();
2942 class_destroy(comedi_class
);
2943 cdev_del(&comedi_cdev
);
2944 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2946 comedi_proc_cleanup();
2948 module_exit(comedi_cleanup
);
2950 MODULE_AUTHOR("http://www.comedi.org");
2951 MODULE_DESCRIPTION("Comedi core module");
2952 MODULE_LICENSE("GPL");