5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include "comedi_compat32.h"
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/fcntl.h>
28 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/kmod.h>
32 #include <linux/poll.h>
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/vmalloc.h>
37 #include "comedidev.h"
38 #include <linux/cdev.h>
39 #include <linux/stat.h>
42 #include <linux/uaccess.h>
44 #include "comedi_internal.h"
47 * comedi_subdevice "runflags"
48 * @COMEDI_SRF_RT: DEPRECATED: command is running real-time
49 * @COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred
50 * since the last command was started
51 * @COMEDI_SRF_RUNNING: command is running
52 * @COMEDI_SRF_FREE_SPRIV: free s->private on detach
54 * @COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy"
56 #define COMEDI_SRF_RT BIT(1)
57 #define COMEDI_SRF_ERROR BIT(2)
58 #define COMEDI_SRF_RUNNING BIT(27)
59 #define COMEDI_SRF_FREE_SPRIV BIT(31)
61 #define COMEDI_SRF_BUSY_MASK (COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING)
64 * struct comedi_file - per-file private data for comedi device
65 * @dev: comedi_device struct
66 * @read_subdev: current "read" subdevice
67 * @write_subdev: current "write" subdevice
68 * @last_detach_count: last known detach count
69 * @last_attached: last known attached/detached state
72 struct comedi_device
*dev
;
73 struct comedi_subdevice
*read_subdev
;
74 struct comedi_subdevice
*write_subdev
;
75 unsigned int last_detach_count
;
79 #define COMEDI_NUM_MINORS 0x100
80 #define COMEDI_NUM_SUBDEVICE_MINORS \
81 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
83 static int comedi_num_legacy_minors
;
84 module_param(comedi_num_legacy_minors
, int, S_IRUGO
);
85 MODULE_PARM_DESC(comedi_num_legacy_minors
,
86 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
89 unsigned int comedi_default_buf_size_kb
= CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
;
90 module_param(comedi_default_buf_size_kb
, uint
, S_IRUGO
| S_IWUSR
);
91 MODULE_PARM_DESC(comedi_default_buf_size_kb
,
92 "default asynchronous buffer size in KiB (default "
93 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
) ")");
95 unsigned int comedi_default_buf_maxsize_kb
96 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
;
97 module_param(comedi_default_buf_maxsize_kb
, uint
, S_IRUGO
| S_IWUSR
);
98 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb
,
99 "default maximum size of asynchronous buffer in KiB (default "
100 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
) ")");
102 static DEFINE_MUTEX(comedi_board_minor_table_lock
);
103 static struct comedi_device
104 *comedi_board_minor_table
[COMEDI_NUM_BOARD_MINORS
];
106 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock
);
107 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
108 static struct comedi_subdevice
109 *comedi_subdevice_minor_table
[COMEDI_NUM_SUBDEVICE_MINORS
];
111 static struct class *comedi_class
;
112 static struct cdev comedi_cdev
;
114 static void comedi_device_init(struct comedi_device
*dev
)
116 kref_init(&dev
->refcount
);
117 spin_lock_init(&dev
->spinlock
);
118 mutex_init(&dev
->mutex
);
119 init_rwsem(&dev
->attach_lock
);
123 static void comedi_dev_kref_release(struct kref
*kref
)
125 struct comedi_device
*dev
=
126 container_of(kref
, struct comedi_device
, refcount
);
128 mutex_destroy(&dev
->mutex
);
129 put_device(dev
->class_dev
);
134 * comedi_dev_put - release a use of a comedi device structure
135 * @dev: comedi_device struct
137 * Must be called when a user of a comedi device is finished with it.
138 * When the last user of the comedi device calls this function, the
139 * comedi device is destroyed.
141 * Return 1 if the comedi device is destroyed by this call or dev is
142 * NULL, otherwise return 0. Callers must not assume the comedi
143 * device is still valid if this function returns 0.
145 int comedi_dev_put(struct comedi_device
*dev
)
148 return kref_put(&dev
->refcount
, comedi_dev_kref_release
);
151 EXPORT_SYMBOL_GPL(comedi_dev_put
);
153 static struct comedi_device
*comedi_dev_get(struct comedi_device
*dev
)
156 kref_get(&dev
->refcount
);
160 static void comedi_device_cleanup(struct comedi_device
*dev
)
162 struct module
*driver_module
= NULL
;
166 mutex_lock(&dev
->mutex
);
168 driver_module
= dev
->driver
->module
;
169 comedi_device_detach(dev
);
170 if (driver_module
&& dev
->use_count
)
171 module_put(driver_module
);
172 mutex_unlock(&dev
->mutex
);
175 static bool comedi_clear_board_dev(struct comedi_device
*dev
)
177 unsigned int i
= dev
->minor
;
178 bool cleared
= false;
180 mutex_lock(&comedi_board_minor_table_lock
);
181 if (dev
== comedi_board_minor_table
[i
]) {
182 comedi_board_minor_table
[i
] = NULL
;
185 mutex_unlock(&comedi_board_minor_table_lock
);
189 static struct comedi_device
*comedi_clear_board_minor(unsigned minor
)
191 struct comedi_device
*dev
;
193 mutex_lock(&comedi_board_minor_table_lock
);
194 dev
= comedi_board_minor_table
[minor
];
195 comedi_board_minor_table
[minor
] = NULL
;
196 mutex_unlock(&comedi_board_minor_table_lock
);
200 static void comedi_free_board_dev(struct comedi_device
*dev
)
203 comedi_device_cleanup(dev
);
204 if (dev
->class_dev
) {
205 device_destroy(comedi_class
,
206 MKDEV(COMEDI_MAJOR
, dev
->minor
));
212 static struct comedi_subdevice
213 *comedi_subdevice_from_minor(const struct comedi_device
*dev
, unsigned minor
)
215 struct comedi_subdevice
*s
;
216 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
218 mutex_lock(&comedi_subdevice_minor_table_lock
);
219 s
= comedi_subdevice_minor_table
[i
];
220 if (s
&& s
->device
!= dev
)
222 mutex_unlock(&comedi_subdevice_minor_table_lock
);
226 static struct comedi_device
*comedi_dev_get_from_board_minor(unsigned minor
)
228 struct comedi_device
*dev
;
230 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
231 mutex_lock(&comedi_board_minor_table_lock
);
232 dev
= comedi_dev_get(comedi_board_minor_table
[minor
]);
233 mutex_unlock(&comedi_board_minor_table_lock
);
237 static struct comedi_device
*comedi_dev_get_from_subdevice_minor(unsigned minor
)
239 struct comedi_device
*dev
;
240 struct comedi_subdevice
*s
;
241 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
243 mutex_lock(&comedi_subdevice_minor_table_lock
);
244 s
= comedi_subdevice_minor_table
[i
];
245 dev
= comedi_dev_get(s
? s
->device
: NULL
);
246 mutex_unlock(&comedi_subdevice_minor_table_lock
);
251 * comedi_dev_get_from_minor - get comedi device by minor device number
252 * @minor: minor device number
254 * Finds the comedi device associated by the minor device number, if any,
255 * and increments its reference count. The comedi device is prevented from
256 * being freed until a matching call is made to comedi_dev_put().
258 * Return a pointer to the comedi device if it exists, with its usage
259 * reference incremented. Return NULL if no comedi device exists with the
260 * specified minor device number.
262 struct comedi_device
*comedi_dev_get_from_minor(unsigned minor
)
264 if (minor
< COMEDI_NUM_BOARD_MINORS
)
265 return comedi_dev_get_from_board_minor(minor
);
267 return comedi_dev_get_from_subdevice_minor(minor
);
269 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor
);
271 static struct comedi_subdevice
*
272 comedi_read_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
274 struct comedi_subdevice
*s
;
276 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
277 s
= comedi_subdevice_from_minor(dev
, minor
);
278 if (!s
|| (s
->subdev_flags
& SDF_CMD_READ
))
281 return dev
->read_subdev
;
284 static struct comedi_subdevice
*
285 comedi_write_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
287 struct comedi_subdevice
*s
;
289 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
290 s
= comedi_subdevice_from_minor(dev
, minor
);
291 if (!s
|| (s
->subdev_flags
& SDF_CMD_WRITE
))
294 return dev
->write_subdev
;
297 static void comedi_file_reset(struct file
*file
)
299 struct comedi_file
*cfp
= file
->private_data
;
300 struct comedi_device
*dev
= cfp
->dev
;
301 struct comedi_subdevice
*s
, *read_s
, *write_s
;
302 unsigned int minor
= iminor(file_inode(file
));
304 read_s
= dev
->read_subdev
;
305 write_s
= dev
->write_subdev
;
306 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
307 s
= comedi_subdevice_from_minor(dev
, minor
);
308 if (!s
|| s
->subdev_flags
& SDF_CMD_READ
)
310 if (!s
|| s
->subdev_flags
& SDF_CMD_WRITE
)
313 cfp
->last_attached
= dev
->attached
;
314 cfp
->last_detach_count
= dev
->detach_count
;
315 ACCESS_ONCE(cfp
->read_subdev
) = read_s
;
316 ACCESS_ONCE(cfp
->write_subdev
) = write_s
;
319 static void comedi_file_check(struct file
*file
)
321 struct comedi_file
*cfp
= file
->private_data
;
322 struct comedi_device
*dev
= cfp
->dev
;
324 if (cfp
->last_attached
!= dev
->attached
||
325 cfp
->last_detach_count
!= dev
->detach_count
)
326 comedi_file_reset(file
);
329 static struct comedi_subdevice
*comedi_file_read_subdevice(struct file
*file
)
331 struct comedi_file
*cfp
= file
->private_data
;
333 comedi_file_check(file
);
334 return ACCESS_ONCE(cfp
->read_subdev
);
337 static struct comedi_subdevice
*comedi_file_write_subdevice(struct file
*file
)
339 struct comedi_file
*cfp
= file
->private_data
;
341 comedi_file_check(file
);
342 return ACCESS_ONCE(cfp
->write_subdev
);
345 static int resize_async_buffer(struct comedi_device
*dev
,
346 struct comedi_subdevice
*s
, unsigned new_size
)
348 struct comedi_async
*async
= s
->async
;
351 if (new_size
> async
->max_bufsize
)
355 dev_dbg(dev
->class_dev
,
356 "subdevice is busy, cannot resize buffer\n");
359 if (comedi_buf_is_mmapped(s
)) {
360 dev_dbg(dev
->class_dev
,
361 "subdevice is mmapped, cannot resize buffer\n");
365 /* make sure buffer is an integral number of pages (we round up) */
366 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
368 retval
= comedi_buf_alloc(dev
, s
, new_size
);
373 retval
= s
->buf_change(dev
, s
);
378 dev_dbg(dev
->class_dev
, "subd %d buffer resized to %i bytes\n",
379 s
->index
, async
->prealloc_bufsz
);
383 /* sysfs attribute files */
385 static ssize_t
max_read_buffer_kb_show(struct device
*csdev
,
386 struct device_attribute
*attr
, char *buf
)
388 unsigned int minor
= MINOR(csdev
->devt
);
389 struct comedi_device
*dev
;
390 struct comedi_subdevice
*s
;
391 unsigned int size
= 0;
393 dev
= comedi_dev_get_from_minor(minor
);
397 mutex_lock(&dev
->mutex
);
398 s
= comedi_read_subdevice(dev
, minor
);
399 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
400 size
= s
->async
->max_bufsize
/ 1024;
401 mutex_unlock(&dev
->mutex
);
404 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
407 static ssize_t
max_read_buffer_kb_store(struct device
*csdev
,
408 struct device_attribute
*attr
,
409 const char *buf
, size_t count
)
411 unsigned int minor
= MINOR(csdev
->devt
);
412 struct comedi_device
*dev
;
413 struct comedi_subdevice
*s
;
417 err
= kstrtouint(buf
, 10, &size
);
420 if (size
> (UINT_MAX
/ 1024))
424 dev
= comedi_dev_get_from_minor(minor
);
428 mutex_lock(&dev
->mutex
);
429 s
= comedi_read_subdevice(dev
, minor
);
430 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
431 s
->async
->max_bufsize
= size
;
434 mutex_unlock(&dev
->mutex
);
437 return err
? err
: count
;
439 static DEVICE_ATTR_RW(max_read_buffer_kb
);
441 static ssize_t
read_buffer_kb_show(struct device
*csdev
,
442 struct device_attribute
*attr
, char *buf
)
444 unsigned int minor
= MINOR(csdev
->devt
);
445 struct comedi_device
*dev
;
446 struct comedi_subdevice
*s
;
447 unsigned int size
= 0;
449 dev
= comedi_dev_get_from_minor(minor
);
453 mutex_lock(&dev
->mutex
);
454 s
= comedi_read_subdevice(dev
, minor
);
455 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
456 size
= s
->async
->prealloc_bufsz
/ 1024;
457 mutex_unlock(&dev
->mutex
);
460 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
463 static ssize_t
read_buffer_kb_store(struct device
*csdev
,
464 struct device_attribute
*attr
,
465 const char *buf
, size_t count
)
467 unsigned int minor
= MINOR(csdev
->devt
);
468 struct comedi_device
*dev
;
469 struct comedi_subdevice
*s
;
473 err
= kstrtouint(buf
, 10, &size
);
476 if (size
> (UINT_MAX
/ 1024))
480 dev
= comedi_dev_get_from_minor(minor
);
484 mutex_lock(&dev
->mutex
);
485 s
= comedi_read_subdevice(dev
, minor
);
486 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
487 err
= resize_async_buffer(dev
, s
, size
);
490 mutex_unlock(&dev
->mutex
);
493 return err
? err
: count
;
495 static DEVICE_ATTR_RW(read_buffer_kb
);
497 static ssize_t
max_write_buffer_kb_show(struct device
*csdev
,
498 struct device_attribute
*attr
,
501 unsigned int minor
= MINOR(csdev
->devt
);
502 struct comedi_device
*dev
;
503 struct comedi_subdevice
*s
;
504 unsigned int size
= 0;
506 dev
= comedi_dev_get_from_minor(minor
);
510 mutex_lock(&dev
->mutex
);
511 s
= comedi_write_subdevice(dev
, minor
);
512 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
513 size
= s
->async
->max_bufsize
/ 1024;
514 mutex_unlock(&dev
->mutex
);
517 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
520 static ssize_t
max_write_buffer_kb_store(struct device
*csdev
,
521 struct device_attribute
*attr
,
522 const char *buf
, size_t count
)
524 unsigned int minor
= MINOR(csdev
->devt
);
525 struct comedi_device
*dev
;
526 struct comedi_subdevice
*s
;
530 err
= kstrtouint(buf
, 10, &size
);
533 if (size
> (UINT_MAX
/ 1024))
537 dev
= comedi_dev_get_from_minor(minor
);
541 mutex_lock(&dev
->mutex
);
542 s
= comedi_write_subdevice(dev
, minor
);
543 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
544 s
->async
->max_bufsize
= size
;
547 mutex_unlock(&dev
->mutex
);
550 return err
? err
: count
;
552 static DEVICE_ATTR_RW(max_write_buffer_kb
);
554 static ssize_t
write_buffer_kb_show(struct device
*csdev
,
555 struct device_attribute
*attr
, char *buf
)
557 unsigned int minor
= MINOR(csdev
->devt
);
558 struct comedi_device
*dev
;
559 struct comedi_subdevice
*s
;
560 unsigned int size
= 0;
562 dev
= comedi_dev_get_from_minor(minor
);
566 mutex_lock(&dev
->mutex
);
567 s
= comedi_write_subdevice(dev
, minor
);
568 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
569 size
= s
->async
->prealloc_bufsz
/ 1024;
570 mutex_unlock(&dev
->mutex
);
573 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
576 static ssize_t
write_buffer_kb_store(struct device
*csdev
,
577 struct device_attribute
*attr
,
578 const char *buf
, size_t count
)
580 unsigned int minor
= MINOR(csdev
->devt
);
581 struct comedi_device
*dev
;
582 struct comedi_subdevice
*s
;
586 err
= kstrtouint(buf
, 10, &size
);
589 if (size
> (UINT_MAX
/ 1024))
593 dev
= comedi_dev_get_from_minor(minor
);
597 mutex_lock(&dev
->mutex
);
598 s
= comedi_write_subdevice(dev
, minor
);
599 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
600 err
= resize_async_buffer(dev
, s
, size
);
603 mutex_unlock(&dev
->mutex
);
606 return err
? err
: count
;
608 static DEVICE_ATTR_RW(write_buffer_kb
);
610 static struct attribute
*comedi_dev_attrs
[] = {
611 &dev_attr_max_read_buffer_kb
.attr
,
612 &dev_attr_read_buffer_kb
.attr
,
613 &dev_attr_max_write_buffer_kb
.attr
,
614 &dev_attr_write_buffer_kb
.attr
,
617 ATTRIBUTE_GROUPS(comedi_dev
);
619 static void __comedi_clear_subdevice_runflags(struct comedi_subdevice
*s
,
622 s
->runflags
&= ~bits
;
625 static void __comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
631 static void comedi_update_subdevice_runflags(struct comedi_subdevice
*s
,
632 unsigned mask
, unsigned bits
)
636 spin_lock_irqsave(&s
->spin_lock
, flags
);
637 __comedi_clear_subdevice_runflags(s
, mask
);
638 __comedi_set_subdevice_runflags(s
, bits
& mask
);
639 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
642 static unsigned __comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
647 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
652 spin_lock_irqsave(&s
->spin_lock
, flags
);
653 runflags
= __comedi_get_subdevice_runflags(s
);
654 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
658 static bool comedi_is_runflags_running(unsigned runflags
)
660 return runflags
& COMEDI_SRF_RUNNING
;
663 static bool comedi_is_runflags_in_error(unsigned runflags
)
665 return runflags
& COMEDI_SRF_ERROR
;
669 * comedi_is_subdevice_running - check if async command running on subdevice
670 * @s: comedi_subdevice struct
672 * Return true if an asynchronous comedi command is active on the comedi
673 * subdevice, else return false.
675 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
)
677 unsigned runflags
= comedi_get_subdevice_runflags(s
);
679 return comedi_is_runflags_running(runflags
);
681 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running
);
683 static bool __comedi_is_subdevice_running(struct comedi_subdevice
*s
)
685 unsigned runflags
= __comedi_get_subdevice_runflags(s
);
687 return comedi_is_runflags_running(runflags
);
690 static bool comedi_is_subdevice_idle(struct comedi_subdevice
*s
)
692 unsigned runflags
= comedi_get_subdevice_runflags(s
);
694 return !(runflags
& COMEDI_SRF_BUSY_MASK
);
697 bool comedi_can_auto_free_spriv(struct comedi_subdevice
*s
)
699 unsigned runflags
= __comedi_get_subdevice_runflags(s
);
701 return runflags
& COMEDI_SRF_FREE_SPRIV
;
705 * comedi_set_spriv_auto_free - mark subdevice private data as freeable
706 * @s: comedi_subdevice struct
708 * Mark the subdevice as having a pointer to private data that can be
709 * automatically freed by the comedi core during the detach.
711 void comedi_set_spriv_auto_free(struct comedi_subdevice
*s
)
713 __comedi_set_subdevice_runflags(s
, COMEDI_SRF_FREE_SPRIV
);
715 EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free
);
718 * comedi_alloc_spriv - Allocate memory for the subdevice private data.
719 * @s: comedi_subdevice struct
720 * @size: size of the memory to allocate
722 * This also sets the subdevice runflags to allow the core to automatically
723 * free the private data during the detach.
725 void *comedi_alloc_spriv(struct comedi_subdevice
*s
, size_t size
)
727 s
->private = kzalloc(size
, GFP_KERNEL
);
729 comedi_set_spriv_auto_free(s
);
732 EXPORT_SYMBOL_GPL(comedi_alloc_spriv
);
735 * This function restores a subdevice to an idle state.
737 static void do_become_nonbusy(struct comedi_device
*dev
,
738 struct comedi_subdevice
*s
)
740 struct comedi_async
*async
= s
->async
;
742 comedi_update_subdevice_runflags(s
, COMEDI_SRF_RUNNING
, 0);
745 async
->inttrig
= NULL
;
746 kfree(async
->cmd
.chanlist
);
747 async
->cmd
.chanlist
= NULL
;
749 wake_up_interruptible_all(&async
->wait_head
);
751 dev_err(dev
->class_dev
,
752 "BUG: (?) do_become_nonbusy called with async=NULL\n");
757 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
761 if (comedi_is_subdevice_running(s
) && s
->cancel
)
762 ret
= s
->cancel(dev
, s
);
764 do_become_nonbusy(dev
, s
);
769 void comedi_device_cancel_all(struct comedi_device
*dev
)
771 struct comedi_subdevice
*s
;
777 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
778 s
= &dev
->subdevices
[i
];
784 static int is_device_busy(struct comedi_device
*dev
)
786 struct comedi_subdevice
*s
;
792 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
793 s
= &dev
->subdevices
[i
];
796 if (s
->async
&& comedi_buf_is_mmapped(s
))
804 * COMEDI_DEVCONFIG ioctl
805 * attaches (and configures) or detaches a legacy device
808 * pointer to comedi_devconfig structure (NULL if detaching)
811 * comedi_devconfig structure (if attaching)
816 static int do_devconfig_ioctl(struct comedi_device
*dev
,
817 struct comedi_devconfig __user
*arg
)
819 struct comedi_devconfig it
;
821 if (!capable(CAP_SYS_ADMIN
))
825 if (is_device_busy(dev
))
828 struct module
*driver_module
= dev
->driver
->module
;
830 comedi_device_detach(dev
);
831 module_put(driver_module
);
836 if (copy_from_user(&it
, arg
, sizeof(it
)))
839 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
841 if (it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
842 dev_warn(dev
->class_dev
,
843 "comedi_config --init_data is deprecated\n");
847 if (dev
->minor
>= comedi_num_legacy_minors
)
848 /* don't re-use dynamically allocated comedi devices */
851 /* This increments the driver module count on success. */
852 return comedi_device_attach(dev
, &it
);
856 * COMEDI_BUFCONFIG ioctl
857 * buffer configuration
860 * pointer to comedi_bufconfig structure
863 * comedi_bufconfig structure
866 * modified comedi_bufconfig structure
868 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
869 struct comedi_bufconfig __user
*arg
)
871 struct comedi_bufconfig bc
;
872 struct comedi_async
*async
;
873 struct comedi_subdevice
*s
;
876 if (copy_from_user(&bc
, arg
, sizeof(bc
)))
879 if (bc
.subdevice
>= dev
->n_subdevices
)
882 s
= &dev
->subdevices
[bc
.subdevice
];
886 dev_dbg(dev
->class_dev
,
887 "subdevice does not have async capability\n");
893 if (bc
.maximum_size
) {
894 if (!capable(CAP_SYS_ADMIN
))
897 async
->max_bufsize
= bc
.maximum_size
;
901 retval
= resize_async_buffer(dev
, s
, bc
.size
);
906 bc
.size
= async
->prealloc_bufsz
;
907 bc
.maximum_size
= async
->max_bufsize
;
910 if (copy_to_user(arg
, &bc
, sizeof(bc
)))
917 * COMEDI_DEVINFO ioctl
921 * pointer to comedi_devinfo structure
927 * comedi_devinfo structure
929 static int do_devinfo_ioctl(struct comedi_device
*dev
,
930 struct comedi_devinfo __user
*arg
,
933 struct comedi_subdevice
*s
;
934 struct comedi_devinfo devinfo
;
936 memset(&devinfo
, 0, sizeof(devinfo
));
938 /* fill devinfo structure */
939 devinfo
.version_code
= COMEDI_VERSION_CODE
;
940 devinfo
.n_subdevs
= dev
->n_subdevices
;
941 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
942 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
944 s
= comedi_file_read_subdevice(file
);
946 devinfo
.read_subdevice
= s
->index
;
948 devinfo
.read_subdevice
= -1;
950 s
= comedi_file_write_subdevice(file
);
952 devinfo
.write_subdevice
= s
->index
;
954 devinfo
.write_subdevice
= -1;
956 if (copy_to_user(arg
, &devinfo
, sizeof(devinfo
)))
963 * COMEDI_SUBDINFO ioctl
967 * pointer to array of comedi_subdinfo structures
973 * array of comedi_subdinfo structures
975 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
976 struct comedi_subdinfo __user
*arg
, void *file
)
979 struct comedi_subdinfo
*tmp
, *us
;
980 struct comedi_subdevice
*s
;
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 if (copy_from_user(&it
, arg
, sizeof(it
)))
1058 if (it
.subdev
>= dev
->n_subdevices
)
1060 s
= &dev
->subdevices
[it
.subdev
];
1062 if (it
.maxdata_list
) {
1063 if (s
->maxdata
|| !s
->maxdata_list
)
1065 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
1066 s
->n_chan
* sizeof(unsigned int)))
1071 return -EINVAL
; /* flaglist not supported */
1076 if (!s
->range_table_list
)
1078 for (i
= 0; i
< s
->n_chan
; i
++) {
1081 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
1082 (s
->range_table_list
[i
]->length
);
1083 if (put_user(x
, it
.rangelist
+ i
))
1092 * COMEDI_BUFINFO ioctl
1093 * buffer information
1096 * pointer to comedi_bufinfo structure
1099 * comedi_bufinfo structure
1102 * modified comedi_bufinfo structure
1104 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
1105 struct comedi_bufinfo __user
*arg
, void *file
)
1107 struct comedi_bufinfo bi
;
1108 struct comedi_subdevice
*s
;
1109 struct comedi_async
*async
;
1111 if (copy_from_user(&bi
, arg
, sizeof(bi
)))
1114 if (bi
.subdevice
>= dev
->n_subdevices
)
1117 s
= &dev
->subdevices
[bi
.subdevice
];
1122 dev_dbg(dev
->class_dev
,
1123 "subdevice does not have async capability\n");
1124 bi
.buf_write_ptr
= 0;
1125 bi
.buf_read_ptr
= 0;
1126 bi
.buf_write_count
= 0;
1127 bi
.buf_read_count
= 0;
1129 bi
.bytes_written
= 0;
1134 bi
.bytes_written
= 0;
1135 goto copyback_position
;
1137 if (s
->busy
!= file
)
1140 if (bi
.bytes_read
&& !(async
->cmd
.flags
& CMDF_WRITE
)) {
1141 bi
.bytes_read
= comedi_buf_read_alloc(s
, bi
.bytes_read
);
1142 comedi_buf_read_free(s
, bi
.bytes_read
);
1144 if (comedi_is_subdevice_idle(s
) &&
1145 comedi_buf_n_bytes_ready(s
) == 0) {
1146 do_become_nonbusy(dev
, s
);
1150 if (bi
.bytes_written
&& (async
->cmd
.flags
& CMDF_WRITE
)) {
1152 comedi_buf_write_alloc(s
, bi
.bytes_written
);
1153 comedi_buf_write_free(s
, bi
.bytes_written
);
1157 bi
.buf_write_count
= async
->buf_write_count
;
1158 bi
.buf_write_ptr
= async
->buf_write_ptr
;
1159 bi
.buf_read_count
= async
->buf_read_count
;
1160 bi
.buf_read_ptr
= async
->buf_read_ptr
;
1163 if (copy_to_user(arg
, &bi
, sizeof(bi
)))
1169 static int check_insn_config_length(struct comedi_insn
*insn
,
1176 case INSN_CONFIG_DIO_OUTPUT
:
1177 case INSN_CONFIG_DIO_INPUT
:
1178 case INSN_CONFIG_DISARM
:
1179 case INSN_CONFIG_RESET
:
1183 case INSN_CONFIG_ARM
:
1184 case INSN_CONFIG_DIO_QUERY
:
1185 case INSN_CONFIG_BLOCK_SIZE
:
1186 case INSN_CONFIG_FILTER
:
1187 case INSN_CONFIG_SERIAL_CLOCK
:
1188 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
1189 case INSN_CONFIG_ALT_SOURCE
:
1190 case INSN_CONFIG_SET_COUNTER_MODE
:
1191 case INSN_CONFIG_8254_READ_STATUS
:
1192 case INSN_CONFIG_SET_ROUTING
:
1193 case INSN_CONFIG_GET_ROUTING
:
1194 case INSN_CONFIG_GET_PWM_STATUS
:
1195 case INSN_CONFIG_PWM_SET_PERIOD
:
1196 case INSN_CONFIG_PWM_GET_PERIOD
:
1200 case INSN_CONFIG_SET_GATE_SRC
:
1201 case INSN_CONFIG_GET_GATE_SRC
:
1202 case INSN_CONFIG_SET_CLOCK_SRC
:
1203 case INSN_CONFIG_GET_CLOCK_SRC
:
1204 case INSN_CONFIG_SET_OTHER_SRC
:
1205 case INSN_CONFIG_GET_COUNTER_STATUS
:
1206 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
1207 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
1208 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
1212 case INSN_CONFIG_PWM_OUTPUT
:
1213 case INSN_CONFIG_ANALOG_TRIG
:
1217 case INSN_CONFIG_DIGITAL_TRIG
:
1222 * by default we allow the insn since we don't have checks for
1223 * all possible cases yet
1226 pr_warn("No check for data length of config insn id %i is implemented\n",
1228 pr_warn("Add a check to %s in %s\n", __func__
, __FILE__
);
1229 pr_warn("Assuming n=%i is correct\n", insn
->n
);
1235 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
1236 unsigned int *data
, void *file
)
1238 struct comedi_subdevice
*s
;
1242 if (insn
->insn
& INSN_MASK_SPECIAL
) {
1243 /* a non-subdevice instruction */
1245 switch (insn
->insn
) {
1255 do_gettimeofday(&tv
);
1256 data
[0] = tv
.tv_sec
;
1257 data
[1] = tv
.tv_usec
;
1263 if (insn
->n
!= 1 || data
[0] >= 100000) {
1267 udelay(data
[0] / 1000);
1275 if (insn
->subdev
>= dev
->n_subdevices
) {
1276 dev_dbg(dev
->class_dev
,
1277 "%d not usable subdevice\n",
1282 s
= &dev
->subdevices
[insn
->subdev
];
1284 dev_dbg(dev
->class_dev
, "no async\n");
1288 if (!s
->async
->inttrig
) {
1289 dev_dbg(dev
->class_dev
, "no inttrig\n");
1293 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
1298 dev_dbg(dev
->class_dev
, "invalid insn\n");
1303 /* a subdevice instruction */
1304 unsigned int maxdata
;
1306 if (insn
->subdev
>= dev
->n_subdevices
) {
1307 dev_dbg(dev
->class_dev
, "subdevice %d out of range\n",
1312 s
= &dev
->subdevices
[insn
->subdev
];
1314 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1315 dev_dbg(dev
->class_dev
, "%d not usable subdevice\n",
1321 /* are we locked? (ioctl lock) */
1322 if (s
->lock
&& s
->lock
!= file
) {
1323 dev_dbg(dev
->class_dev
, "device locked\n");
1328 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
1331 dev_dbg(dev
->class_dev
, "bad chanspec\n");
1339 /* This looks arbitrary. It is. */
1340 s
->busy
= &parse_insn
;
1341 switch (insn
->insn
) {
1343 ret
= s
->insn_read(dev
, s
, insn
, data
);
1344 if (ret
== -ETIMEDOUT
) {
1345 dev_dbg(dev
->class_dev
,
1346 "subdevice %d read instruction timed out\n",
1351 maxdata
= s
->maxdata_list
1352 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1354 for (i
= 0; i
< insn
->n
; ++i
) {
1355 if (data
[i
] > maxdata
) {
1357 dev_dbg(dev
->class_dev
,
1358 "bad data value(s)\n");
1363 ret
= s
->insn_write(dev
, s
, insn
, data
);
1364 if (ret
== -ETIMEDOUT
) {
1365 dev_dbg(dev
->class_dev
,
1366 "subdevice %d write instruction timed out\n",
1376 * Most drivers ignore the base channel in
1377 * insn->chanspec. Fix this here if
1378 * the subdevice has <= 32 channels.
1380 unsigned int orig_mask
= data
[0];
1381 unsigned int shift
= 0;
1383 if (s
->n_chan
<= 32) {
1384 shift
= CR_CHAN(insn
->chanspec
);
1391 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1392 data
[0] = orig_mask
;
1398 ret
= check_insn_config_length(insn
, data
);
1401 ret
= s
->insn_config(dev
, s
, insn
, data
);
1416 * COMEDI_INSNLIST ioctl
1417 * synchronous instruction list
1420 * pointer to comedi_insnlist structure
1423 * comedi_insnlist structure
1424 * array of comedi_insn structures from insnlist->insns pointer
1425 * data (for writes) from insns[].data pointers
1428 * data (for reads) to insns[].data pointers
1430 /* arbitrary limits */
1431 #define MAX_SAMPLES 256
1432 static int do_insnlist_ioctl(struct comedi_device
*dev
,
1433 struct comedi_insnlist __user
*arg
, void *file
)
1435 struct comedi_insnlist insnlist
;
1436 struct comedi_insn
*insns
= NULL
;
1437 unsigned int *data
= NULL
;
1441 if (copy_from_user(&insnlist
, arg
, sizeof(insnlist
)))
1444 data
= kmalloc_array(MAX_SAMPLES
, sizeof(unsigned int), GFP_KERNEL
);
1450 insns
= kcalloc(insnlist
.n_insns
, sizeof(*insns
), GFP_KERNEL
);
1456 if (copy_from_user(insns
, insnlist
.insns
,
1457 sizeof(*insns
) * insnlist
.n_insns
)) {
1458 dev_dbg(dev
->class_dev
, "copy_from_user failed\n");
1463 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
1464 if (insns
[i
].n
> MAX_SAMPLES
) {
1465 dev_dbg(dev
->class_dev
,
1466 "number of samples too large\n");
1470 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
1471 if (copy_from_user(data
, insns
[i
].data
,
1472 insns
[i
].n
* sizeof(unsigned int))) {
1473 dev_dbg(dev
->class_dev
,
1474 "copy_from_user failed\n");
1479 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
1482 if (insns
[i
].insn
& INSN_MASK_READ
) {
1483 if (copy_to_user(insns
[i
].data
, data
,
1484 insns
[i
].n
* sizeof(unsigned int))) {
1485 dev_dbg(dev
->class_dev
,
1486 "copy_to_user failed\n");
1506 * synchronous instruction
1509 * pointer to comedi_insn structure
1512 * comedi_insn structure
1513 * data (for writes) from insn->data pointer
1516 * data (for reads) to insn->data pointer
1518 static int do_insn_ioctl(struct comedi_device
*dev
,
1519 struct comedi_insn __user
*arg
, void *file
)
1521 struct comedi_insn insn
;
1522 unsigned int *data
= NULL
;
1525 data
= kmalloc_array(MAX_SAMPLES
, sizeof(unsigned int), GFP_KERNEL
);
1531 if (copy_from_user(&insn
, arg
, sizeof(insn
))) {
1536 /* This is where the behavior of insn and insnlist deviate. */
1537 if (insn
.n
> MAX_SAMPLES
)
1538 insn
.n
= MAX_SAMPLES
;
1539 if (insn
.insn
& INSN_MASK_WRITE
) {
1540 if (copy_from_user(data
,
1542 insn
.n
* sizeof(unsigned int))) {
1547 ret
= parse_insn(dev
, &insn
, data
, file
);
1550 if (insn
.insn
& INSN_MASK_READ
) {
1551 if (copy_to_user(insn
.data
,
1553 insn
.n
* sizeof(unsigned int))) {
1566 static int __comedi_get_user_cmd(struct comedi_device
*dev
,
1567 struct comedi_cmd __user
*arg
,
1568 struct comedi_cmd
*cmd
)
1570 struct comedi_subdevice
*s
;
1572 if (copy_from_user(cmd
, arg
, sizeof(*cmd
))) {
1573 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1577 if (cmd
->subdev
>= dev
->n_subdevices
) {
1578 dev_dbg(dev
->class_dev
, "%d no such subdevice\n", cmd
->subdev
);
1582 s
= &dev
->subdevices
[cmd
->subdev
];
1584 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1585 dev_dbg(dev
->class_dev
, "%d not valid subdevice\n",
1590 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1591 dev_dbg(dev
->class_dev
,
1592 "subdevice %d does not support commands\n",
1597 /* make sure channel/gain list isn't too long */
1598 if (cmd
->chanlist_len
> s
->len_chanlist
) {
1599 dev_dbg(dev
->class_dev
, "channel/gain list too long %d > %d\n",
1600 cmd
->chanlist_len
, s
->len_chanlist
);
1605 * Set the CMDF_WRITE flag to the correct state if the subdevice
1606 * supports only "read" commands or only "write" commands.
1608 switch (s
->subdev_flags
& (SDF_CMD_READ
| SDF_CMD_WRITE
)) {
1610 cmd
->flags
&= ~CMDF_WRITE
;
1613 cmd
->flags
|= CMDF_WRITE
;
1622 static int __comedi_get_user_chanlist(struct comedi_device
*dev
,
1623 struct comedi_subdevice
*s
,
1624 unsigned int __user
*user_chanlist
,
1625 struct comedi_cmd
*cmd
)
1627 unsigned int *chanlist
;
1630 cmd
->chanlist
= NULL
;
1631 chanlist
= memdup_user(user_chanlist
,
1632 cmd
->chanlist_len
* sizeof(unsigned int));
1633 if (IS_ERR(chanlist
))
1634 return PTR_ERR(chanlist
);
1636 /* make sure each element in channel/gain list is valid */
1637 ret
= comedi_check_chanlist(s
, cmd
->chanlist_len
, chanlist
);
1643 cmd
->chanlist
= chanlist
;
1650 * asynchronous acquisition command set-up
1653 * pointer to comedi_cmd structure
1656 * comedi_cmd structure
1657 * channel/range list from cmd->chanlist pointer
1660 * possibly modified comedi_cmd structure (when -EAGAIN returned)
1662 static int do_cmd_ioctl(struct comedi_device
*dev
,
1663 struct comedi_cmd __user
*arg
, void *file
)
1665 struct comedi_cmd cmd
;
1666 struct comedi_subdevice
*s
;
1667 struct comedi_async
*async
;
1668 unsigned int __user
*user_chanlist
;
1671 /* get the user's cmd and do some simple validation */
1672 ret
= __comedi_get_user_cmd(dev
, arg
, &cmd
);
1676 /* save user's chanlist pointer so it can be restored later */
1677 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1679 s
= &dev
->subdevices
[cmd
.subdev
];
1682 /* are we locked? (ioctl lock) */
1683 if (s
->lock
&& s
->lock
!= file
) {
1684 dev_dbg(dev
->class_dev
, "subdevice locked\n");
1690 dev_dbg(dev
->class_dev
, "subdevice busy\n");
1694 /* make sure channel/gain list isn't too short */
1695 if (cmd
.chanlist_len
< 1) {
1696 dev_dbg(dev
->class_dev
, "channel/gain list too short %u < 1\n",
1702 async
->cmd
.data
= NULL
;
1704 /* load channel/gain list */
1705 ret
= __comedi_get_user_chanlist(dev
, s
, user_chanlist
, &async
->cmd
);
1709 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1711 if (async
->cmd
.flags
& CMDF_BOGUS
|| ret
) {
1712 dev_dbg(dev
->class_dev
, "test returned %d\n", ret
);
1714 /* restore chanlist pointer before copying back */
1715 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1717 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1718 dev_dbg(dev
->class_dev
, "fault writing cmd\n");
1726 if (!async
->prealloc_bufsz
) {
1728 dev_dbg(dev
->class_dev
, "no buffer (?)\n");
1732 comedi_buf_reset(s
);
1734 async
->cb_mask
= COMEDI_CB_BLOCK
| COMEDI_CB_CANCEL_MASK
;
1735 if (async
->cmd
.flags
& CMDF_WAKE_EOS
)
1736 async
->cb_mask
|= COMEDI_CB_EOS
;
1738 comedi_update_subdevice_runflags(s
, COMEDI_SRF_BUSY_MASK
,
1739 COMEDI_SRF_RUNNING
);
1742 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1743 * race with comedi_read() or comedi_write().
1746 ret
= s
->do_cmd(dev
, s
);
1751 do_become_nonbusy(dev
, s
);
1757 * COMEDI_CMDTEST ioctl
1758 * asynchronous acquisition command testing
1761 * pointer to comedi_cmd structure
1764 * comedi_cmd structure
1765 * channel/range list from cmd->chanlist pointer
1768 * possibly modified comedi_cmd structure
1770 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1771 struct comedi_cmd __user
*arg
, void *file
)
1773 struct comedi_cmd cmd
;
1774 struct comedi_subdevice
*s
;
1775 unsigned int __user
*user_chanlist
;
1778 /* get the user's cmd and do some simple validation */
1779 ret
= __comedi_get_user_cmd(dev
, arg
, &cmd
);
1783 /* save user's chanlist pointer so it can be restored later */
1784 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1786 s
= &dev
->subdevices
[cmd
.subdev
];
1788 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1789 if (user_chanlist
) {
1790 /* load channel/gain list */
1791 ret
= __comedi_get_user_chanlist(dev
, s
, user_chanlist
, &cmd
);
1796 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1798 kfree(cmd
.chanlist
); /* free kernel copy of user chanlist */
1800 /* restore chanlist pointer before copying back */
1801 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1803 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1804 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1824 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1828 unsigned long flags
;
1829 struct comedi_subdevice
*s
;
1831 if (arg
>= dev
->n_subdevices
)
1833 s
= &dev
->subdevices
[arg
];
1835 spin_lock_irqsave(&s
->spin_lock
, flags
);
1836 if (s
->busy
|| s
->lock
)
1840 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1846 * COMEDI_UNLOCK ioctl
1858 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1861 struct comedi_subdevice
*s
;
1863 if (arg
>= dev
->n_subdevices
)
1865 s
= &dev
->subdevices
[arg
];
1870 if (s
->lock
&& s
->lock
!= file
)
1873 if (s
->lock
== file
)
1880 * COMEDI_CANCEL ioctl
1881 * cancel asynchronous acquisition
1892 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1895 struct comedi_subdevice
*s
;
1897 if (arg
>= dev
->n_subdevices
)
1899 s
= &dev
->subdevices
[arg
];
1906 if (s
->busy
!= file
)
1909 return do_cancel(dev
, s
);
1914 * instructs driver to synchronize buffers
1925 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1928 struct comedi_subdevice
*s
;
1930 if (arg
>= dev
->n_subdevices
)
1932 s
= &dev
->subdevices
[arg
];
1937 if (s
->busy
!= file
)
1941 return s
->poll(dev
, s
);
1947 * COMEDI_SETRSUBD ioctl
1948 * sets the current "read" subdevice on a per-file basis
1959 static int do_setrsubd_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1962 struct comedi_file
*cfp
= file
->private_data
;
1963 struct comedi_subdevice
*s_old
, *s_new
;
1965 if (arg
>= dev
->n_subdevices
)
1968 s_new
= &dev
->subdevices
[arg
];
1969 s_old
= comedi_file_read_subdevice(file
);
1971 return 0; /* no change */
1973 if (!(s_new
->subdev_flags
& SDF_CMD_READ
))
1977 * Check the file isn't still busy handling a "read" command on the
1978 * old subdevice (if any).
1980 if (s_old
&& s_old
->busy
== file
&& s_old
->async
&&
1981 !(s_old
->async
->cmd
.flags
& CMDF_WRITE
))
1984 ACCESS_ONCE(cfp
->read_subdev
) = s_new
;
1989 * COMEDI_SETWSUBD ioctl
1990 * sets the current "write" subdevice on a per-file basis
2001 static int do_setwsubd_ioctl(struct comedi_device
*dev
, unsigned long arg
,
2004 struct comedi_file
*cfp
= file
->private_data
;
2005 struct comedi_subdevice
*s_old
, *s_new
;
2007 if (arg
>= dev
->n_subdevices
)
2010 s_new
= &dev
->subdevices
[arg
];
2011 s_old
= comedi_file_write_subdevice(file
);
2013 return 0; /* no change */
2015 if (!(s_new
->subdev_flags
& SDF_CMD_WRITE
))
2019 * Check the file isn't still busy handling a "write" command on the
2020 * old subdevice (if any).
2022 if (s_old
&& s_old
->busy
== file
&& s_old
->async
&&
2023 (s_old
->async
->cmd
.flags
& CMDF_WRITE
))
2026 ACCESS_ONCE(cfp
->write_subdev
) = s_new
;
2030 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
2033 unsigned minor
= iminor(file_inode(file
));
2034 struct comedi_file
*cfp
= file
->private_data
;
2035 struct comedi_device
*dev
= cfp
->dev
;
2038 mutex_lock(&dev
->mutex
);
2041 * Device config is special, because it must work on
2042 * an unconfigured device.
2044 if (cmd
== COMEDI_DEVCONFIG
) {
2045 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
2046 /* Device config not appropriate on non-board minors. */
2050 rc
= do_devconfig_ioctl(dev
,
2051 (struct comedi_devconfig __user
*)arg
);
2054 dev
->minor
>= comedi_num_legacy_minors
) {
2056 * Successfully unconfigured a dynamically
2057 * allocated device. Try and remove it.
2059 if (comedi_clear_board_dev(dev
)) {
2060 mutex_unlock(&dev
->mutex
);
2061 comedi_free_board_dev(dev
);
2069 if (!dev
->attached
) {
2070 dev_dbg(dev
->class_dev
, "no driver attached\n");
2076 case COMEDI_BUFCONFIG
:
2077 rc
= do_bufconfig_ioctl(dev
,
2078 (struct comedi_bufconfig __user
*)arg
);
2080 case COMEDI_DEVINFO
:
2081 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
2084 case COMEDI_SUBDINFO
:
2085 rc
= do_subdinfo_ioctl(dev
,
2086 (struct comedi_subdinfo __user
*)arg
,
2089 case COMEDI_CHANINFO
:
2090 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
2092 case COMEDI_RANGEINFO
:
2093 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
2095 case COMEDI_BUFINFO
:
2096 rc
= do_bufinfo_ioctl(dev
,
2097 (struct comedi_bufinfo __user
*)arg
,
2101 rc
= do_lock_ioctl(dev
, arg
, file
);
2104 rc
= do_unlock_ioctl(dev
, arg
, file
);
2107 rc
= do_cancel_ioctl(dev
, arg
, file
);
2110 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
2112 case COMEDI_CMDTEST
:
2113 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
2116 case COMEDI_INSNLIST
:
2117 rc
= do_insnlist_ioctl(dev
,
2118 (struct comedi_insnlist __user
*)arg
,
2122 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
2126 rc
= do_poll_ioctl(dev
, arg
, file
);
2128 case COMEDI_SETRSUBD
:
2129 rc
= do_setrsubd_ioctl(dev
, arg
, file
);
2131 case COMEDI_SETWSUBD
:
2132 rc
= do_setwsubd_ioctl(dev
, arg
, file
);
2140 mutex_unlock(&dev
->mutex
);
2144 static void comedi_vm_open(struct vm_area_struct
*area
)
2146 struct comedi_buf_map
*bm
;
2148 bm
= area
->vm_private_data
;
2149 comedi_buf_map_get(bm
);
2152 static void comedi_vm_close(struct vm_area_struct
*area
)
2154 struct comedi_buf_map
*bm
;
2156 bm
= area
->vm_private_data
;
2157 comedi_buf_map_put(bm
);
2160 static struct vm_operations_struct comedi_vm_ops
= {
2161 .open
= comedi_vm_open
,
2162 .close
= comedi_vm_close
,
2165 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2167 struct comedi_file
*cfp
= file
->private_data
;
2168 struct comedi_device
*dev
= cfp
->dev
;
2169 struct comedi_subdevice
*s
;
2170 struct comedi_async
*async
;
2171 struct comedi_buf_map
*bm
= NULL
;
2172 unsigned long start
= vma
->vm_start
;
2179 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2180 * and down-reading &dev->attach_lock should normally succeed without
2181 * contention unless the device is in the process of being attached
2184 if (!down_read_trylock(&dev
->attach_lock
))
2187 if (!dev
->attached
) {
2188 dev_dbg(dev
->class_dev
, "no driver attached\n");
2193 if (vma
->vm_flags
& VM_WRITE
)
2194 s
= comedi_file_write_subdevice(file
);
2196 s
= comedi_file_read_subdevice(file
);
2208 if (vma
->vm_pgoff
!= 0) {
2209 dev_dbg(dev
->class_dev
, "mmap() offset must be 0.\n");
2214 size
= vma
->vm_end
- vma
->vm_start
;
2215 if (size
> async
->prealloc_bufsz
) {
2219 if (size
& (~PAGE_MASK
)) {
2224 n_pages
= size
>> PAGE_SHIFT
;
2226 /* get reference to current buf map (if any) */
2227 bm
= comedi_buf_map_from_subdev_get(s
);
2228 if (!bm
|| n_pages
> bm
->n_pages
) {
2232 for (i
= 0; i
< n_pages
; ++i
) {
2233 struct comedi_buf_page
*buf
= &bm
->page_list
[i
];
2235 if (remap_pfn_range(vma
, start
,
2236 page_to_pfn(virt_to_page(buf
->virt_addr
)),
2237 PAGE_SIZE
, PAGE_SHARED
)) {
2244 vma
->vm_ops
= &comedi_vm_ops
;
2245 vma
->vm_private_data
= bm
;
2247 vma
->vm_ops
->open(vma
);
2251 up_read(&dev
->attach_lock
);
2252 comedi_buf_map_put(bm
); /* put reference to buf map - okay if NULL */
2256 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
2258 unsigned int mask
= 0;
2259 struct comedi_file
*cfp
= file
->private_data
;
2260 struct comedi_device
*dev
= cfp
->dev
;
2261 struct comedi_subdevice
*s
;
2263 mutex_lock(&dev
->mutex
);
2265 if (!dev
->attached
) {
2266 dev_dbg(dev
->class_dev
, "no driver attached\n");
2270 s
= comedi_file_read_subdevice(file
);
2271 if (s
&& s
->async
) {
2272 poll_wait(file
, &s
->async
->wait_head
, wait
);
2273 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
2274 (s
->async
->cmd
.flags
& CMDF_WRITE
) ||
2275 comedi_buf_read_n_available(s
) > 0)
2276 mask
|= POLLIN
| POLLRDNORM
;
2279 s
= comedi_file_write_subdevice(file
);
2280 if (s
&& s
->async
) {
2281 unsigned int bps
= comedi_bytes_per_sample(s
);
2283 poll_wait(file
, &s
->async
->wait_head
, wait
);
2284 comedi_buf_write_alloc(s
, s
->async
->prealloc_bufsz
);
2285 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
2286 !(s
->async
->cmd
.flags
& CMDF_WRITE
) ||
2287 comedi_buf_write_n_allocated(s
) >= bps
)
2288 mask
|= POLLOUT
| POLLWRNORM
;
2292 mutex_unlock(&dev
->mutex
);
2296 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
2297 size_t nbytes
, loff_t
*offset
)
2299 struct comedi_subdevice
*s
;
2300 struct comedi_async
*async
;
2301 int n
, m
, count
= 0, retval
= 0;
2302 DECLARE_WAITQUEUE(wait
, current
);
2303 struct comedi_file
*cfp
= file
->private_data
;
2304 struct comedi_device
*dev
= cfp
->dev
;
2305 bool on_wait_queue
= false;
2307 unsigned int old_detach_count
;
2309 /* Protect against device detachment during operation. */
2310 down_read(&dev
->attach_lock
);
2311 attach_locked
= true;
2312 old_detach_count
= dev
->detach_count
;
2314 if (!dev
->attached
) {
2315 dev_dbg(dev
->class_dev
, "no driver attached\n");
2320 s
= comedi_file_write_subdevice(file
);
2321 if (!s
|| !s
->async
) {
2328 if (!s
->busy
|| !nbytes
)
2330 if (s
->busy
!= file
) {
2334 if (!(async
->cmd
.flags
& CMDF_WRITE
)) {
2339 add_wait_queue(&async
->wait_head
, &wait
);
2340 on_wait_queue
= true;
2341 while (nbytes
> 0 && !retval
) {
2344 set_current_state(TASK_INTERRUPTIBLE
);
2346 runflags
= comedi_get_subdevice_runflags(s
);
2347 if (!comedi_is_runflags_running(runflags
)) {
2349 struct comedi_subdevice
*new_s
;
2351 if (comedi_is_runflags_in_error(runflags
))
2356 * To avoid deadlock, cannot acquire dev->mutex
2357 * while dev->attach_lock is held. Need to
2358 * remove task from the async wait queue before
2359 * releasing dev->attach_lock, as it might not
2360 * be valid afterwards.
2362 remove_wait_queue(&async
->wait_head
, &wait
);
2363 on_wait_queue
= false;
2364 up_read(&dev
->attach_lock
);
2365 attach_locked
= false;
2366 mutex_lock(&dev
->mutex
);
2368 * Become non-busy unless things have changed
2369 * behind our back. Checking dev->detach_count
2370 * is unchanged ought to be sufficient (unless
2371 * there have been 2**32 detaches in the
2372 * meantime!), but check the subdevice pointer
2373 * as well just in case.
2375 new_s
= comedi_file_write_subdevice(file
);
2376 if (dev
->attached
&&
2377 old_detach_count
== dev
->detach_count
&&
2378 s
== new_s
&& new_s
->async
== async
)
2379 do_become_nonbusy(dev
, s
);
2380 mutex_unlock(&dev
->mutex
);
2388 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
2389 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
2390 comedi_buf_write_alloc(s
, async
->prealloc_bufsz
);
2391 if (m
> comedi_buf_write_n_allocated(s
))
2392 m
= comedi_buf_write_n_allocated(s
);
2397 if (file
->f_flags
& O_NONBLOCK
) {
2402 if (signal_pending(current
)) {
2403 retval
= -ERESTARTSYS
;
2408 if (s
->busy
!= file
) {
2412 if (!(async
->cmd
.flags
& CMDF_WRITE
)) {
2419 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
2425 comedi_buf_write_free(s
, n
);
2431 break; /* makes device work like a pipe */
2435 remove_wait_queue(&async
->wait_head
, &wait
);
2436 set_current_state(TASK_RUNNING
);
2438 up_read(&dev
->attach_lock
);
2440 return count
? count
: retval
;
2443 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
2446 struct comedi_subdevice
*s
;
2447 struct comedi_async
*async
;
2448 int n
, m
, count
= 0, retval
= 0;
2449 DECLARE_WAITQUEUE(wait
, current
);
2450 struct comedi_file
*cfp
= file
->private_data
;
2451 struct comedi_device
*dev
= cfp
->dev
;
2452 unsigned int old_detach_count
;
2453 bool become_nonbusy
= false;
2456 /* Protect against device detachment during operation. */
2457 down_read(&dev
->attach_lock
);
2458 attach_locked
= true;
2459 old_detach_count
= dev
->detach_count
;
2461 if (!dev
->attached
) {
2462 dev_dbg(dev
->class_dev
, "no driver attached\n");
2467 s
= comedi_file_read_subdevice(file
);
2468 if (!s
|| !s
->async
) {
2474 if (!s
->busy
|| !nbytes
)
2476 if (s
->busy
!= file
) {
2480 if (async
->cmd
.flags
& CMDF_WRITE
) {
2485 add_wait_queue(&async
->wait_head
, &wait
);
2486 while (nbytes
> 0 && !retval
) {
2487 set_current_state(TASK_INTERRUPTIBLE
);
2491 m
= comedi_buf_read_n_available(s
);
2492 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
2493 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
2498 unsigned runflags
= comedi_get_subdevice_runflags(s
);
2500 if (!comedi_is_runflags_running(runflags
)) {
2501 if (comedi_is_runflags_in_error(runflags
))
2505 become_nonbusy
= true;
2508 if (file
->f_flags
& O_NONBLOCK
) {
2513 if (signal_pending(current
)) {
2514 retval
= -ERESTARTSYS
;
2521 if (s
->busy
!= file
) {
2525 if (async
->cmd
.flags
& CMDF_WRITE
) {
2531 m
= copy_to_user(buf
, async
->prealloc_buf
+
2532 async
->buf_read_ptr
, n
);
2538 comedi_buf_read_alloc(s
, n
);
2539 comedi_buf_read_free(s
, n
);
2545 break; /* makes device work like a pipe */
2547 remove_wait_queue(&async
->wait_head
, &wait
);
2548 set_current_state(TASK_RUNNING
);
2549 if (become_nonbusy
|| comedi_is_subdevice_idle(s
)) {
2550 struct comedi_subdevice
*new_s
;
2553 * To avoid deadlock, cannot acquire dev->mutex
2554 * while dev->attach_lock is held.
2556 up_read(&dev
->attach_lock
);
2557 attach_locked
= false;
2558 mutex_lock(&dev
->mutex
);
2560 * Check device hasn't become detached behind our back.
2561 * Checking dev->detach_count is unchanged ought to be
2562 * sufficient (unless there have been 2**32 detaches in the
2563 * meantime!), but check the subdevice pointer as well just in
2566 new_s
= comedi_file_read_subdevice(file
);
2567 if (dev
->attached
&& old_detach_count
== dev
->detach_count
&&
2568 s
== new_s
&& new_s
->async
== async
) {
2569 if (become_nonbusy
|| comedi_buf_n_bytes_ready(s
) == 0)
2570 do_become_nonbusy(dev
, s
);
2572 mutex_unlock(&dev
->mutex
);
2576 up_read(&dev
->attach_lock
);
2578 return count
? count
: retval
;
2581 static int comedi_open(struct inode
*inode
, struct file
*file
)
2583 const unsigned minor
= iminor(inode
);
2584 struct comedi_file
*cfp
;
2585 struct comedi_device
*dev
= comedi_dev_get_from_minor(minor
);
2589 pr_debug("invalid minor number\n");
2593 cfp
= kzalloc(sizeof(*cfp
), GFP_KERNEL
);
2599 mutex_lock(&dev
->mutex
);
2600 if (!dev
->attached
&& !capable(CAP_SYS_ADMIN
)) {
2601 dev_dbg(dev
->class_dev
, "not attached and not CAP_SYS_ADMIN\n");
2605 if (dev
->attached
&& dev
->use_count
== 0) {
2606 if (!try_module_get(dev
->driver
->module
)) {
2611 rc
= dev
->open(dev
);
2613 module_put(dev
->driver
->module
);
2620 file
->private_data
= cfp
;
2621 comedi_file_reset(file
);
2625 mutex_unlock(&dev
->mutex
);
2627 comedi_dev_put(dev
);
2633 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2635 struct comedi_file
*cfp
= file
->private_data
;
2636 struct comedi_device
*dev
= cfp
->dev
;
2638 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2641 static int comedi_close(struct inode
*inode
, struct file
*file
)
2643 struct comedi_file
*cfp
= file
->private_data
;
2644 struct comedi_device
*dev
= cfp
->dev
;
2645 struct comedi_subdevice
*s
= NULL
;
2648 mutex_lock(&dev
->mutex
);
2650 if (dev
->subdevices
) {
2651 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2652 s
= &dev
->subdevices
[i
];
2654 if (s
->busy
== file
)
2656 if (s
->lock
== file
)
2660 if (dev
->attached
&& dev
->use_count
== 1) {
2663 module_put(dev
->driver
->module
);
2668 mutex_unlock(&dev
->mutex
);
2669 comedi_dev_put(dev
);
2675 static const struct file_operations comedi_fops
= {
2676 .owner
= THIS_MODULE
,
2677 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2678 .compat_ioctl
= comedi_compat_ioctl
,
2679 .open
= comedi_open
,
2680 .release
= comedi_close
,
2681 .read
= comedi_read
,
2682 .write
= comedi_write
,
2683 .mmap
= comedi_mmap
,
2684 .poll
= comedi_poll
,
2685 .fasync
= comedi_fasync
,
2686 .llseek
= noop_llseek
,
2690 * comedi_event - handle events for asynchronous comedi command
2691 * @dev: comedi_device struct
2692 * @s: comedi_subdevice struct associated with dev
2693 * Context: interrupt (usually), s->spin_lock spin-lock not held
2695 * If an asynchronous comedi command is active on the subdevice, process
2696 * any COMEDI_CB_... event flags that have been set, usually by an
2697 * interrupt handler. These may change the run state of the asynchronous
2698 * command, wake a task, and/or send a SIGIO signal.
2700 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2702 struct comedi_async
*async
= s
->async
;
2703 unsigned int events
;
2705 unsigned long flags
;
2707 spin_lock_irqsave(&s
->spin_lock
, flags
);
2709 events
= async
->events
;
2711 if (!__comedi_is_subdevice_running(s
)) {
2712 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2716 if (events
& COMEDI_CB_CANCEL_MASK
)
2717 __comedi_clear_subdevice_runflags(s
, COMEDI_SRF_RUNNING
);
2720 * Remember if an error event has occurred, so an error can be
2721 * returned the next time the user does a read() or write().
2723 if (events
& COMEDI_CB_ERROR_MASK
)
2724 __comedi_set_subdevice_runflags(s
, COMEDI_SRF_ERROR
);
2726 if (async
->cb_mask
& events
) {
2727 wake_up_interruptible(&async
->wait_head
);
2728 si_code
= async
->cmd
.flags
& CMDF_WRITE
? POLL_OUT
: POLL_IN
;
2731 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
2734 kill_fasync(&dev
->async_queue
, SIGIO
, si_code
);
2736 EXPORT_SYMBOL_GPL(comedi_event
);
2738 /* Note: the ->mutex is pre-locked on successful return */
2739 struct comedi_device
*comedi_alloc_board_minor(struct device
*hardware_device
)
2741 struct comedi_device
*dev
;
2742 struct device
*csdev
;
2745 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2747 return ERR_PTR(-ENOMEM
);
2748 comedi_device_init(dev
);
2749 comedi_set_hw_dev(dev
, hardware_device
);
2750 mutex_lock(&dev
->mutex
);
2751 mutex_lock(&comedi_board_minor_table_lock
);
2752 for (i
= hardware_device
? comedi_num_legacy_minors
: 0;
2753 i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2754 if (!comedi_board_minor_table
[i
]) {
2755 comedi_board_minor_table
[i
] = dev
;
2759 mutex_unlock(&comedi_board_minor_table_lock
);
2760 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2761 mutex_unlock(&dev
->mutex
);
2762 comedi_device_cleanup(dev
);
2763 comedi_dev_put(dev
);
2764 dev_err(hardware_device
,
2765 "ran out of minor numbers for board device files\n");
2766 return ERR_PTR(-EBUSY
);
2769 csdev
= device_create(comedi_class
, hardware_device
,
2770 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2772 dev
->class_dev
= get_device(csdev
);
2774 /* Note: dev->mutex needs to be unlocked by the caller. */
2778 static void comedi_free_board_minor(unsigned minor
)
2780 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2781 comedi_free_board_dev(comedi_clear_board_minor(minor
));
2784 void comedi_release_hardware_device(struct device
*hardware_device
)
2787 struct comedi_device
*dev
;
2789 for (minor
= comedi_num_legacy_minors
; minor
< COMEDI_NUM_BOARD_MINORS
;
2791 mutex_lock(&comedi_board_minor_table_lock
);
2792 dev
= comedi_board_minor_table
[minor
];
2793 if (dev
&& dev
->hw_dev
== hardware_device
) {
2794 comedi_board_minor_table
[minor
] = NULL
;
2795 mutex_unlock(&comedi_board_minor_table_lock
);
2796 comedi_free_board_dev(dev
);
2799 mutex_unlock(&comedi_board_minor_table_lock
);
2803 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2805 struct comedi_device
*dev
= s
->device
;
2806 struct device
*csdev
;
2809 mutex_lock(&comedi_subdevice_minor_table_lock
);
2810 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
) {
2811 if (!comedi_subdevice_minor_table
[i
]) {
2812 comedi_subdevice_minor_table
[i
] = s
;
2816 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2817 if (i
== COMEDI_NUM_SUBDEVICE_MINORS
) {
2818 dev_err(dev
->class_dev
,
2819 "ran out of minor numbers for subdevice files\n");
2822 i
+= COMEDI_NUM_BOARD_MINORS
;
2824 csdev
= device_create(comedi_class
, dev
->class_dev
,
2825 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2826 dev
->minor
, s
->index
);
2828 s
->class_dev
= csdev
;
2833 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2842 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2843 BUG_ON(s
->minor
< COMEDI_NUM_BOARD_MINORS
);
2845 i
= s
->minor
- COMEDI_NUM_BOARD_MINORS
;
2846 mutex_lock(&comedi_subdevice_minor_table_lock
);
2847 if (s
== comedi_subdevice_minor_table
[i
])
2848 comedi_subdevice_minor_table
[i
] = NULL
;
2849 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2851 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2852 s
->class_dev
= NULL
;
2856 static void comedi_cleanup_board_minors(void)
2860 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++)
2861 comedi_free_board_minor(i
);
2864 static int __init
comedi_init(void)
2869 pr_info("version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2871 if (comedi_num_legacy_minors
< 0 ||
2872 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2873 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2874 COMEDI_NUM_BOARD_MINORS
);
2878 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2879 COMEDI_NUM_MINORS
, "comedi");
2882 cdev_init(&comedi_cdev
, &comedi_fops
);
2883 comedi_cdev
.owner
= THIS_MODULE
;
2885 retval
= kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2887 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2892 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2893 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2897 comedi_class
= class_create(THIS_MODULE
, "comedi");
2898 if (IS_ERR(comedi_class
)) {
2899 pr_err("failed to create class\n");
2900 cdev_del(&comedi_cdev
);
2901 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2903 return PTR_ERR(comedi_class
);
2906 comedi_class
->dev_groups
= comedi_dev_groups
;
2908 /* XXX requires /proc interface */
2911 /* create devices files for legacy/manual use */
2912 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2913 struct comedi_device
*dev
;
2915 dev
= comedi_alloc_board_minor(NULL
);
2917 comedi_cleanup_board_minors();
2918 cdev_del(&comedi_cdev
);
2919 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2921 return PTR_ERR(dev
);
2923 /* comedi_alloc_board_minor() locked the mutex */
2924 mutex_unlock(&dev
->mutex
);
2929 module_init(comedi_init
);
2931 static void __exit
comedi_cleanup(void)
2935 comedi_cleanup_board_minors();
2936 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
)
2937 BUG_ON(comedi_board_minor_table
[i
]);
2938 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
)
2939 BUG_ON(comedi_subdevice_minor_table
[i
]);
2941 class_destroy(comedi_class
);
2942 cdev_del(&comedi_cdev
);
2943 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2945 comedi_proc_cleanup();
2947 module_exit(comedi_cleanup
);
2949 MODULE_AUTHOR("http://www.comedi.org");
2950 MODULE_DESCRIPTION("Comedi core module");
2951 MODULE_LICENSE("GPL");