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 * struct comedi_file - per-file private data for comedi device
48 * @dev: comedi_device struct
49 * @read_subdev: current "read" subdevice
50 * @write_subdev: current "write" subdevice
51 * @last_detach_count: last known detach count
52 * @last_attached: last known attached/detached state
55 struct comedi_device
*dev
;
56 struct comedi_subdevice
*read_subdev
;
57 struct comedi_subdevice
*write_subdev
;
58 unsigned int last_detach_count
;
62 #define COMEDI_NUM_MINORS 0x100
63 #define COMEDI_NUM_SUBDEVICE_MINORS \
64 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
66 static int comedi_num_legacy_minors
;
67 module_param(comedi_num_legacy_minors
, int, S_IRUGO
);
68 MODULE_PARM_DESC(comedi_num_legacy_minors
,
69 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
72 unsigned int comedi_default_buf_size_kb
= CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
;
73 module_param(comedi_default_buf_size_kb
, uint
, S_IRUGO
| S_IWUSR
);
74 MODULE_PARM_DESC(comedi_default_buf_size_kb
,
75 "default asynchronous buffer size in KiB (default "
76 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
) ")");
78 unsigned int comedi_default_buf_maxsize_kb
79 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
;
80 module_param(comedi_default_buf_maxsize_kb
, uint
, S_IRUGO
| S_IWUSR
);
81 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb
,
82 "default maximum size of asynchronous buffer in KiB (default "
83 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
) ")");
85 static DEFINE_MUTEX(comedi_board_minor_table_lock
);
86 static struct comedi_device
87 *comedi_board_minor_table
[COMEDI_NUM_BOARD_MINORS
];
89 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock
);
90 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
91 static struct comedi_subdevice
92 *comedi_subdevice_minor_table
[COMEDI_NUM_SUBDEVICE_MINORS
];
94 static struct class *comedi_class
;
95 static struct cdev comedi_cdev
;
97 static void comedi_device_init(struct comedi_device
*dev
)
99 kref_init(&dev
->refcount
);
100 spin_lock_init(&dev
->spinlock
);
101 mutex_init(&dev
->mutex
);
102 init_rwsem(&dev
->attach_lock
);
106 static void comedi_dev_kref_release(struct kref
*kref
)
108 struct comedi_device
*dev
=
109 container_of(kref
, struct comedi_device
, refcount
);
111 mutex_destroy(&dev
->mutex
);
112 put_device(dev
->class_dev
);
117 * comedi_dev_put - release a use of a comedi device structure
118 * @dev: comedi_device struct
120 * Must be called when a user of a comedi device is finished with it.
121 * When the last user of the comedi device calls this function, the
122 * comedi device is destroyed.
124 * Return 1 if the comedi device is destroyed by this call or dev is
125 * NULL, otherwise return 0. Callers must not assume the comedi
126 * device is still valid if this function returns 0.
128 int comedi_dev_put(struct comedi_device
*dev
)
131 return kref_put(&dev
->refcount
, comedi_dev_kref_release
);
134 EXPORT_SYMBOL_GPL(comedi_dev_put
);
136 static struct comedi_device
*comedi_dev_get(struct comedi_device
*dev
)
139 kref_get(&dev
->refcount
);
143 static void comedi_device_cleanup(struct comedi_device
*dev
)
145 struct module
*driver_module
= NULL
;
149 mutex_lock(&dev
->mutex
);
151 driver_module
= dev
->driver
->module
;
152 comedi_device_detach(dev
);
153 if (driver_module
&& dev
->use_count
)
154 module_put(driver_module
);
155 mutex_unlock(&dev
->mutex
);
158 static bool comedi_clear_board_dev(struct comedi_device
*dev
)
160 unsigned int i
= dev
->minor
;
161 bool cleared
= false;
163 mutex_lock(&comedi_board_minor_table_lock
);
164 if (dev
== comedi_board_minor_table
[i
]) {
165 comedi_board_minor_table
[i
] = NULL
;
168 mutex_unlock(&comedi_board_minor_table_lock
);
172 static struct comedi_device
*comedi_clear_board_minor(unsigned minor
)
174 struct comedi_device
*dev
;
176 mutex_lock(&comedi_board_minor_table_lock
);
177 dev
= comedi_board_minor_table
[minor
];
178 comedi_board_minor_table
[minor
] = NULL
;
179 mutex_unlock(&comedi_board_minor_table_lock
);
183 static void comedi_free_board_dev(struct comedi_device
*dev
)
186 comedi_device_cleanup(dev
);
187 if (dev
->class_dev
) {
188 device_destroy(comedi_class
,
189 MKDEV(COMEDI_MAJOR
, dev
->minor
));
195 static struct comedi_subdevice
196 *comedi_subdevice_from_minor(const struct comedi_device
*dev
, unsigned minor
)
198 struct comedi_subdevice
*s
;
199 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
201 BUG_ON(i
>= COMEDI_NUM_SUBDEVICE_MINORS
);
202 mutex_lock(&comedi_subdevice_minor_table_lock
);
203 s
= comedi_subdevice_minor_table
[i
];
204 if (s
&& s
->device
!= dev
)
206 mutex_unlock(&comedi_subdevice_minor_table_lock
);
210 static struct comedi_device
*comedi_dev_get_from_board_minor(unsigned minor
)
212 struct comedi_device
*dev
;
214 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
215 mutex_lock(&comedi_board_minor_table_lock
);
216 dev
= comedi_dev_get(comedi_board_minor_table
[minor
]);
217 mutex_unlock(&comedi_board_minor_table_lock
);
221 static struct comedi_device
*comedi_dev_get_from_subdevice_minor(unsigned minor
)
223 struct comedi_device
*dev
;
224 struct comedi_subdevice
*s
;
225 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
227 BUG_ON(i
>= COMEDI_NUM_SUBDEVICE_MINORS
);
228 mutex_lock(&comedi_subdevice_minor_table_lock
);
229 s
= comedi_subdevice_minor_table
[i
];
230 dev
= comedi_dev_get(s
? s
->device
: NULL
);
231 mutex_unlock(&comedi_subdevice_minor_table_lock
);
236 * comedi_dev_get_from_minor - get comedi device by minor device number
237 * @minor: minor device number
239 * Finds the comedi device associated by the minor device number, if any,
240 * and increments its reference count. The comedi device is prevented from
241 * being freed until a matching call is made to comedi_dev_put().
243 * Return a pointer to the comedi device if it exists, with its usage
244 * reference incremented. Return NULL if no comedi device exists with the
245 * specified minor device number.
247 struct comedi_device
*comedi_dev_get_from_minor(unsigned minor
)
249 if (minor
< COMEDI_NUM_BOARD_MINORS
)
250 return comedi_dev_get_from_board_minor(minor
);
252 return comedi_dev_get_from_subdevice_minor(minor
);
254 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor
);
256 static struct comedi_subdevice
*
257 comedi_read_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
259 struct comedi_subdevice
*s
;
261 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
262 s
= comedi_subdevice_from_minor(dev
, minor
);
263 if (s
== NULL
|| (s
->subdev_flags
& SDF_CMD_READ
))
266 return dev
->read_subdev
;
269 static struct comedi_subdevice
*
270 comedi_write_subdevice(const struct comedi_device
*dev
, unsigned int minor
)
272 struct comedi_subdevice
*s
;
274 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
275 s
= comedi_subdevice_from_minor(dev
, minor
);
276 if (s
== NULL
|| (s
->subdev_flags
& SDF_CMD_WRITE
))
279 return dev
->write_subdev
;
282 static void comedi_file_reset(struct file
*file
)
284 struct comedi_file
*cfp
= file
->private_data
;
285 struct comedi_device
*dev
= cfp
->dev
;
286 struct comedi_subdevice
*s
, *read_s
, *write_s
;
287 unsigned int minor
= iminor(file_inode(file
));
289 read_s
= dev
->read_subdev
;
290 write_s
= dev
->write_subdev
;
291 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
292 s
= comedi_subdevice_from_minor(dev
, minor
);
293 if (s
== NULL
|| s
->subdev_flags
& SDF_CMD_READ
)
295 if (s
== NULL
|| s
->subdev_flags
& SDF_CMD_WRITE
)
298 cfp
->last_attached
= dev
->attached
;
299 cfp
->last_detach_count
= dev
->detach_count
;
300 ACCESS_ONCE(cfp
->read_subdev
) = read_s
;
301 ACCESS_ONCE(cfp
->write_subdev
) = write_s
;
304 static void comedi_file_check(struct file
*file
)
306 struct comedi_file
*cfp
= file
->private_data
;
307 struct comedi_device
*dev
= cfp
->dev
;
309 if (cfp
->last_attached
!= dev
->attached
||
310 cfp
->last_detach_count
!= dev
->detach_count
)
311 comedi_file_reset(file
);
314 static struct comedi_subdevice
*comedi_file_read_subdevice(struct file
*file
)
316 struct comedi_file
*cfp
= file
->private_data
;
318 comedi_file_check(file
);
319 return ACCESS_ONCE(cfp
->read_subdev
);
322 static struct comedi_subdevice
*comedi_file_write_subdevice(struct file
*file
)
324 struct comedi_file
*cfp
= file
->private_data
;
326 comedi_file_check(file
);
327 return ACCESS_ONCE(cfp
->write_subdev
);
330 static int resize_async_buffer(struct comedi_device
*dev
,
331 struct comedi_subdevice
*s
, unsigned new_size
)
333 struct comedi_async
*async
= s
->async
;
336 if (new_size
> async
->max_bufsize
)
340 dev_dbg(dev
->class_dev
,
341 "subdevice is busy, cannot resize buffer\n");
344 if (comedi_buf_is_mmapped(s
)) {
345 dev_dbg(dev
->class_dev
,
346 "subdevice is mmapped, cannot resize buffer\n");
350 /* make sure buffer is an integral number of pages
352 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
354 retval
= comedi_buf_alloc(dev
, s
, new_size
);
359 retval
= s
->buf_change(dev
, s
);
364 dev_dbg(dev
->class_dev
, "subd %d buffer resized to %i bytes\n",
365 s
->index
, async
->prealloc_bufsz
);
369 /* sysfs attribute files */
371 static ssize_t
max_read_buffer_kb_show(struct device
*csdev
,
372 struct device_attribute
*attr
, char *buf
)
374 unsigned int minor
= MINOR(csdev
->devt
);
375 struct comedi_device
*dev
;
376 struct comedi_subdevice
*s
;
377 unsigned int size
= 0;
379 dev
= comedi_dev_get_from_minor(minor
);
383 mutex_lock(&dev
->mutex
);
384 s
= comedi_read_subdevice(dev
, minor
);
385 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
386 size
= s
->async
->max_bufsize
/ 1024;
387 mutex_unlock(&dev
->mutex
);
390 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
393 static ssize_t
max_read_buffer_kb_store(struct device
*csdev
,
394 struct device_attribute
*attr
,
395 const char *buf
, size_t count
)
397 unsigned int minor
= MINOR(csdev
->devt
);
398 struct comedi_device
*dev
;
399 struct comedi_subdevice
*s
;
403 err
= kstrtouint(buf
, 10, &size
);
406 if (size
> (UINT_MAX
/ 1024))
410 dev
= comedi_dev_get_from_minor(minor
);
414 mutex_lock(&dev
->mutex
);
415 s
= comedi_read_subdevice(dev
, minor
);
416 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
417 s
->async
->max_bufsize
= size
;
420 mutex_unlock(&dev
->mutex
);
423 return err
? err
: count
;
425 static DEVICE_ATTR_RW(max_read_buffer_kb
);
427 static ssize_t
read_buffer_kb_show(struct device
*csdev
,
428 struct device_attribute
*attr
, char *buf
)
430 unsigned int minor
= MINOR(csdev
->devt
);
431 struct comedi_device
*dev
;
432 struct comedi_subdevice
*s
;
433 unsigned int size
= 0;
435 dev
= comedi_dev_get_from_minor(minor
);
439 mutex_lock(&dev
->mutex
);
440 s
= comedi_read_subdevice(dev
, minor
);
441 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
442 size
= s
->async
->prealloc_bufsz
/ 1024;
443 mutex_unlock(&dev
->mutex
);
446 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
449 static ssize_t
read_buffer_kb_store(struct device
*csdev
,
450 struct device_attribute
*attr
,
451 const char *buf
, size_t count
)
453 unsigned int minor
= MINOR(csdev
->devt
);
454 struct comedi_device
*dev
;
455 struct comedi_subdevice
*s
;
459 err
= kstrtouint(buf
, 10, &size
);
462 if (size
> (UINT_MAX
/ 1024))
466 dev
= comedi_dev_get_from_minor(minor
);
470 mutex_lock(&dev
->mutex
);
471 s
= comedi_read_subdevice(dev
, minor
);
472 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
473 err
= resize_async_buffer(dev
, s
, size
);
476 mutex_unlock(&dev
->mutex
);
479 return err
? err
: count
;
481 static DEVICE_ATTR_RW(read_buffer_kb
);
483 static ssize_t
max_write_buffer_kb_show(struct device
*csdev
,
484 struct device_attribute
*attr
,
487 unsigned int minor
= MINOR(csdev
->devt
);
488 struct comedi_device
*dev
;
489 struct comedi_subdevice
*s
;
490 unsigned int size
= 0;
492 dev
= comedi_dev_get_from_minor(minor
);
496 mutex_lock(&dev
->mutex
);
497 s
= comedi_write_subdevice(dev
, minor
);
498 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
499 size
= s
->async
->max_bufsize
/ 1024;
500 mutex_unlock(&dev
->mutex
);
503 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
506 static ssize_t
max_write_buffer_kb_store(struct device
*csdev
,
507 struct device_attribute
*attr
,
508 const char *buf
, size_t count
)
510 unsigned int minor
= MINOR(csdev
->devt
);
511 struct comedi_device
*dev
;
512 struct comedi_subdevice
*s
;
516 err
= kstrtouint(buf
, 10, &size
);
519 if (size
> (UINT_MAX
/ 1024))
523 dev
= comedi_dev_get_from_minor(minor
);
527 mutex_lock(&dev
->mutex
);
528 s
= comedi_write_subdevice(dev
, minor
);
529 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
530 s
->async
->max_bufsize
= size
;
533 mutex_unlock(&dev
->mutex
);
536 return err
? err
: count
;
538 static DEVICE_ATTR_RW(max_write_buffer_kb
);
540 static ssize_t
write_buffer_kb_show(struct device
*csdev
,
541 struct device_attribute
*attr
, char *buf
)
543 unsigned int minor
= MINOR(csdev
->devt
);
544 struct comedi_device
*dev
;
545 struct comedi_subdevice
*s
;
546 unsigned int size
= 0;
548 dev
= comedi_dev_get_from_minor(minor
);
552 mutex_lock(&dev
->mutex
);
553 s
= comedi_write_subdevice(dev
, minor
);
554 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
555 size
= s
->async
->prealloc_bufsz
/ 1024;
556 mutex_unlock(&dev
->mutex
);
559 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
562 static ssize_t
write_buffer_kb_store(struct device
*csdev
,
563 struct device_attribute
*attr
,
564 const char *buf
, size_t count
)
566 unsigned int minor
= MINOR(csdev
->devt
);
567 struct comedi_device
*dev
;
568 struct comedi_subdevice
*s
;
572 err
= kstrtouint(buf
, 10, &size
);
575 if (size
> (UINT_MAX
/ 1024))
579 dev
= comedi_dev_get_from_minor(minor
);
583 mutex_lock(&dev
->mutex
);
584 s
= comedi_write_subdevice(dev
, minor
);
585 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
586 err
= resize_async_buffer(dev
, s
, size
);
589 mutex_unlock(&dev
->mutex
);
592 return err
? err
: count
;
594 static DEVICE_ATTR_RW(write_buffer_kb
);
596 static struct attribute
*comedi_dev_attrs
[] = {
597 &dev_attr_max_read_buffer_kb
.attr
,
598 &dev_attr_read_buffer_kb
.attr
,
599 &dev_attr_max_write_buffer_kb
.attr
,
600 &dev_attr_write_buffer_kb
.attr
,
603 ATTRIBUTE_GROUPS(comedi_dev
);
605 static void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
606 unsigned mask
, unsigned bits
)
610 spin_lock_irqsave(&s
->spin_lock
, flags
);
611 s
->runflags
&= ~mask
;
612 s
->runflags
|= (bits
& mask
);
613 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
616 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
621 spin_lock_irqsave(&s
->spin_lock
, flags
);
622 runflags
= s
->runflags
;
623 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
628 * comedi_is_subdevice_running - check if async command running on subdevice
629 * @s: comedi_subdevice struct
631 * Return true if an asynchronous comedi command is active on the comedi
632 * subdevice, else return false.
634 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
)
636 unsigned runflags
= comedi_get_subdevice_runflags(s
);
638 return (runflags
& COMEDI_SRF_RUNNING
) ? true : false;
640 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running
);
642 static bool comedi_is_subdevice_in_error(struct comedi_subdevice
*s
)
644 unsigned runflags
= comedi_get_subdevice_runflags(s
);
646 return (runflags
& COMEDI_SRF_ERROR
) ? true : false;
649 static bool comedi_is_subdevice_idle(struct comedi_subdevice
*s
)
651 unsigned runflags
= comedi_get_subdevice_runflags(s
);
653 return (runflags
& COMEDI_SRF_BUSY_MASK
) ? false : true;
657 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
658 * @s: comedi_subdevice struct
659 * @size: size of the memory to allocate
661 * This also sets the subdevice runflags to allow the core to automatically
662 * free the private data during the detach.
664 void *comedi_alloc_spriv(struct comedi_subdevice
*s
, size_t size
)
666 s
->private = kzalloc(size
, GFP_KERNEL
);
668 s
->runflags
|= COMEDI_SRF_FREE_SPRIV
;
671 EXPORT_SYMBOL_GPL(comedi_alloc_spriv
);
674 This function restores a subdevice to an idle state.
676 static void do_become_nonbusy(struct comedi_device
*dev
,
677 struct comedi_subdevice
*s
)
679 struct comedi_async
*async
= s
->async
;
681 comedi_set_subdevice_runflags(s
, COMEDI_SRF_RUNNING
, 0);
684 async
->inttrig
= NULL
;
685 kfree(async
->cmd
.chanlist
);
686 async
->cmd
.chanlist
= NULL
;
688 wake_up_interruptible_all(&s
->async
->wait_head
);
690 dev_err(dev
->class_dev
,
691 "BUG: (?) do_become_nonbusy called with async=NULL\n");
696 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
700 if (comedi_is_subdevice_running(s
) && s
->cancel
)
701 ret
= s
->cancel(dev
, s
);
703 do_become_nonbusy(dev
, s
);
708 void comedi_device_cancel_all(struct comedi_device
*dev
)
710 struct comedi_subdevice
*s
;
716 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
717 s
= &dev
->subdevices
[i
];
723 static int is_device_busy(struct comedi_device
*dev
)
725 struct comedi_subdevice
*s
;
731 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
732 s
= &dev
->subdevices
[i
];
735 if (s
->async
&& comedi_buf_is_mmapped(s
))
747 pointer to devconfig structure
750 devconfig structure at arg
755 static int do_devconfig_ioctl(struct comedi_device
*dev
,
756 struct comedi_devconfig __user
*arg
)
758 struct comedi_devconfig it
;
760 if (!capable(CAP_SYS_ADMIN
))
764 if (is_device_busy(dev
))
767 struct module
*driver_module
= dev
->driver
->module
;
769 comedi_device_detach(dev
);
770 module_put(driver_module
);
775 if (copy_from_user(&it
, arg
, sizeof(it
)))
778 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
780 if (it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
781 dev_warn(dev
->class_dev
,
782 "comedi_config --init_data is deprecated\n");
786 if (dev
->minor
>= comedi_num_legacy_minors
)
787 /* don't re-use dynamically allocated comedi devices */
790 /* This increments the driver module count on success. */
791 return comedi_device_attach(dev
, &it
);
796 buffer configuration ioctl
799 pointer to bufconfig structure
805 modified bufconfig at arg
808 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
809 struct comedi_bufconfig __user
*arg
)
811 struct comedi_bufconfig bc
;
812 struct comedi_async
*async
;
813 struct comedi_subdevice
*s
;
816 if (copy_from_user(&bc
, arg
, sizeof(bc
)))
819 if (bc
.subdevice
>= dev
->n_subdevices
)
822 s
= &dev
->subdevices
[bc
.subdevice
];
826 dev_dbg(dev
->class_dev
,
827 "subdevice does not have async capability\n");
833 if (bc
.maximum_size
) {
834 if (!capable(CAP_SYS_ADMIN
))
837 async
->max_bufsize
= bc
.maximum_size
;
841 retval
= resize_async_buffer(dev
, s
, bc
.size
);
846 bc
.size
= async
->prealloc_bufsz
;
847 bc
.maximum_size
= async
->max_bufsize
;
850 if (copy_to_user(arg
, &bc
, sizeof(bc
)))
861 pointer to devinfo structure
870 static int do_devinfo_ioctl(struct comedi_device
*dev
,
871 struct comedi_devinfo __user
*arg
,
874 struct comedi_subdevice
*s
;
875 struct comedi_devinfo devinfo
;
877 memset(&devinfo
, 0, sizeof(devinfo
));
879 /* fill devinfo structure */
880 devinfo
.version_code
= COMEDI_VERSION_CODE
;
881 devinfo
.n_subdevs
= dev
->n_subdevices
;
882 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
883 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
885 s
= comedi_file_read_subdevice(file
);
887 devinfo
.read_subdevice
= s
->index
;
889 devinfo
.read_subdevice
= -1;
891 s
= comedi_file_write_subdevice(file
);
893 devinfo
.write_subdevice
= s
->index
;
895 devinfo
.write_subdevice
= -1;
897 if (copy_to_user(arg
, &devinfo
, sizeof(devinfo
)))
908 pointer to array of subdevice info structures
914 array of subdevice info structures at arg
917 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
918 struct comedi_subdinfo __user
*arg
, void *file
)
921 struct comedi_subdinfo
*tmp
, *us
;
922 struct comedi_subdevice
*s
;
924 tmp
= kcalloc(dev
->n_subdevices
, sizeof(*tmp
), GFP_KERNEL
);
928 /* fill subdinfo structs */
929 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
930 s
= &dev
->subdevices
[i
];
934 us
->n_chan
= s
->n_chan
;
935 us
->subd_flags
= s
->subdev_flags
;
936 if (comedi_is_subdevice_running(s
))
937 us
->subd_flags
|= SDF_RUNNING
;
938 #define TIMER_nanosec 5 /* backwards compatibility */
939 us
->timer_type
= TIMER_nanosec
;
940 us
->len_chanlist
= s
->len_chanlist
;
941 us
->maxdata
= s
->maxdata
;
942 if (s
->range_table
) {
944 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
946 us
->range_type
= 0; /* XXX */
950 us
->subd_flags
|= SDF_BUSY
;
952 us
->subd_flags
|= SDF_BUSY_OWNER
;
954 us
->subd_flags
|= SDF_LOCKED
;
956 us
->subd_flags
|= SDF_LOCK_OWNER
;
957 if (!s
->maxdata
&& s
->maxdata_list
)
958 us
->subd_flags
|= SDF_MAXDATA
;
959 if (s
->range_table_list
)
960 us
->subd_flags
|= SDF_RANGETYPE
;
962 us
->subd_flags
|= SDF_CMD
;
964 if (s
->insn_bits
!= &insn_inval
)
965 us
->insn_bits_support
= COMEDI_SUPPORTED
;
967 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
970 ret
= copy_to_user(arg
, tmp
, dev
->n_subdevices
* sizeof(*tmp
));
974 return ret
? -EFAULT
: 0;
982 pointer to chaninfo structure
985 chaninfo structure at arg
988 arrays at elements of chaninfo structure
991 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
992 struct comedi_chaninfo __user
*arg
)
994 struct comedi_subdevice
*s
;
995 struct comedi_chaninfo it
;
997 if (copy_from_user(&it
, arg
, sizeof(it
)))
1000 if (it
.subdev
>= dev
->n_subdevices
)
1002 s
= &dev
->subdevices
[it
.subdev
];
1004 if (it
.maxdata_list
) {
1005 if (s
->maxdata
|| !s
->maxdata_list
)
1007 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
1008 s
->n_chan
* sizeof(unsigned int)))
1013 return -EINVAL
; /* flaglist not supported */
1018 if (!s
->range_table_list
)
1020 for (i
= 0; i
< s
->n_chan
; i
++) {
1023 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
1024 (s
->range_table_list
[i
]->length
);
1025 if (put_user(x
, it
.rangelist
+ i
))
1029 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
1030 s
->n_chan
* sizeof(unsigned int)))
1040 buffer information ioctl
1043 pointer to bufinfo structure
1049 modified bufinfo at arg
1052 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
1053 struct comedi_bufinfo __user
*arg
, void *file
)
1055 struct comedi_bufinfo bi
;
1056 struct comedi_subdevice
*s
;
1057 struct comedi_async
*async
;
1059 if (copy_from_user(&bi
, arg
, sizeof(bi
)))
1062 if (bi
.subdevice
>= dev
->n_subdevices
)
1065 s
= &dev
->subdevices
[bi
.subdevice
];
1070 dev_dbg(dev
->class_dev
,
1071 "subdevice does not have async capability\n");
1072 bi
.buf_write_ptr
= 0;
1073 bi
.buf_read_ptr
= 0;
1074 bi
.buf_write_count
= 0;
1075 bi
.buf_read_count
= 0;
1077 bi
.bytes_written
= 0;
1082 bi
.bytes_written
= 0;
1083 goto copyback_position
;
1085 if (s
->busy
!= file
)
1088 if (bi
.bytes_read
&& !(async
->cmd
.flags
& CMDF_WRITE
)) {
1089 bi
.bytes_read
= comedi_buf_read_alloc(s
, bi
.bytes_read
);
1090 comedi_buf_read_free(s
, bi
.bytes_read
);
1092 if (comedi_is_subdevice_idle(s
) &&
1093 comedi_buf_n_bytes_ready(s
) == 0) {
1094 do_become_nonbusy(dev
, s
);
1098 if (bi
.bytes_written
&& (async
->cmd
.flags
& CMDF_WRITE
)) {
1100 comedi_buf_write_alloc(s
, bi
.bytes_written
);
1101 comedi_buf_write_free(s
, bi
.bytes_written
);
1105 bi
.buf_write_count
= async
->buf_write_count
;
1106 bi
.buf_write_ptr
= async
->buf_write_ptr
;
1107 bi
.buf_read_count
= async
->buf_read_count
;
1108 bi
.buf_read_ptr
= async
->buf_read_ptr
;
1111 if (copy_to_user(arg
, &bi
, sizeof(bi
)))
1117 static int check_insn_config_length(struct comedi_insn
*insn
,
1124 case INSN_CONFIG_DIO_OUTPUT
:
1125 case INSN_CONFIG_DIO_INPUT
:
1126 case INSN_CONFIG_DISARM
:
1127 case INSN_CONFIG_RESET
:
1131 case INSN_CONFIG_ARM
:
1132 case INSN_CONFIG_DIO_QUERY
:
1133 case INSN_CONFIG_BLOCK_SIZE
:
1134 case INSN_CONFIG_FILTER
:
1135 case INSN_CONFIG_SERIAL_CLOCK
:
1136 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
1137 case INSN_CONFIG_ALT_SOURCE
:
1138 case INSN_CONFIG_SET_COUNTER_MODE
:
1139 case INSN_CONFIG_8254_READ_STATUS
:
1140 case INSN_CONFIG_SET_ROUTING
:
1141 case INSN_CONFIG_GET_ROUTING
:
1142 case INSN_CONFIG_GET_PWM_STATUS
:
1143 case INSN_CONFIG_PWM_SET_PERIOD
:
1144 case INSN_CONFIG_PWM_GET_PERIOD
:
1148 case INSN_CONFIG_SET_GATE_SRC
:
1149 case INSN_CONFIG_GET_GATE_SRC
:
1150 case INSN_CONFIG_SET_CLOCK_SRC
:
1151 case INSN_CONFIG_GET_CLOCK_SRC
:
1152 case INSN_CONFIG_SET_OTHER_SRC
:
1153 case INSN_CONFIG_GET_COUNTER_STATUS
:
1154 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
1155 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
1156 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
1160 case INSN_CONFIG_PWM_OUTPUT
:
1161 case INSN_CONFIG_ANALOG_TRIG
:
1165 case INSN_CONFIG_DIGITAL_TRIG
:
1169 /* by default we allow the insn since we don't have checks for
1170 * all possible cases yet */
1172 pr_warn("No check for data length of config insn id %i is implemented\n",
1174 pr_warn("Add a check to %s in %s\n", __func__
, __FILE__
);
1175 pr_warn("Assuming n=%i is correct\n", insn
->n
);
1181 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
1182 unsigned int *data
, void *file
)
1184 struct comedi_subdevice
*s
;
1188 if (insn
->insn
& INSN_MASK_SPECIAL
) {
1189 /* a non-subdevice instruction */
1191 switch (insn
->insn
) {
1201 do_gettimeofday(&tv
);
1202 data
[0] = tv
.tv_sec
;
1203 data
[1] = tv
.tv_usec
;
1209 if (insn
->n
!= 1 || data
[0] >= 100000) {
1213 udelay(data
[0] / 1000);
1221 if (insn
->subdev
>= dev
->n_subdevices
) {
1222 dev_dbg(dev
->class_dev
,
1223 "%d not usable subdevice\n",
1228 s
= &dev
->subdevices
[insn
->subdev
];
1230 dev_dbg(dev
->class_dev
, "no async\n");
1234 if (!s
->async
->inttrig
) {
1235 dev_dbg(dev
->class_dev
, "no inttrig\n");
1239 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
1244 dev_dbg(dev
->class_dev
, "invalid insn\n");
1249 /* a subdevice instruction */
1250 unsigned int maxdata
;
1252 if (insn
->subdev
>= dev
->n_subdevices
) {
1253 dev_dbg(dev
->class_dev
, "subdevice %d out of range\n",
1258 s
= &dev
->subdevices
[insn
->subdev
];
1260 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1261 dev_dbg(dev
->class_dev
, "%d not usable subdevice\n",
1267 /* are we locked? (ioctl lock) */
1268 if (s
->lock
&& s
->lock
!= file
) {
1269 dev_dbg(dev
->class_dev
, "device locked\n");
1274 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
1277 dev_dbg(dev
->class_dev
, "bad chanspec\n");
1285 /* This looks arbitrary. It is. */
1286 s
->busy
= &parse_insn
;
1287 switch (insn
->insn
) {
1289 ret
= s
->insn_read(dev
, s
, insn
, data
);
1290 if (ret
== -ETIMEDOUT
) {
1291 dev_dbg(dev
->class_dev
,
1292 "subdevice %d read instruction timed out\n",
1297 maxdata
= s
->maxdata_list
1298 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1300 for (i
= 0; i
< insn
->n
; ++i
) {
1301 if (data
[i
] > maxdata
) {
1303 dev_dbg(dev
->class_dev
,
1304 "bad data value(s)\n");
1309 ret
= s
->insn_write(dev
, s
, insn
, data
);
1310 if (ret
== -ETIMEDOUT
) {
1311 dev_dbg(dev
->class_dev
,
1312 "subdevice %d write instruction timed out\n",
1321 /* Most drivers ignore the base channel in
1322 * insn->chanspec. Fix this here if
1323 * the subdevice has <= 32 channels. */
1324 unsigned int orig_mask
= data
[0];
1325 unsigned int shift
= 0;
1327 if (s
->n_chan
<= 32) {
1328 shift
= CR_CHAN(insn
->chanspec
);
1335 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1336 data
[0] = orig_mask
;
1342 ret
= check_insn_config_length(insn
, data
);
1345 ret
= s
->insn_config(dev
, s
, insn
, data
);
1361 * synchronous instructions
1364 * pointer to sync cmd structure
1367 * sync cmd struct at arg
1374 /* arbitrary limits */
1375 #define MAX_SAMPLES 256
1376 static int do_insnlist_ioctl(struct comedi_device
*dev
,
1377 struct comedi_insnlist __user
*arg
, void *file
)
1379 struct comedi_insnlist insnlist
;
1380 struct comedi_insn
*insns
= NULL
;
1381 unsigned int *data
= NULL
;
1385 if (copy_from_user(&insnlist
, arg
, sizeof(insnlist
)))
1388 data
= kmalloc_array(MAX_SAMPLES
, sizeof(unsigned int), GFP_KERNEL
);
1394 insns
= kcalloc(insnlist
.n_insns
, sizeof(*insns
), GFP_KERNEL
);
1400 if (copy_from_user(insns
, insnlist
.insns
,
1401 sizeof(*insns
) * insnlist
.n_insns
)) {
1402 dev_dbg(dev
->class_dev
, "copy_from_user failed\n");
1407 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
1408 if (insns
[i
].n
> MAX_SAMPLES
) {
1409 dev_dbg(dev
->class_dev
,
1410 "number of samples too large\n");
1414 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
1415 if (copy_from_user(data
, insns
[i
].data
,
1416 insns
[i
].n
* sizeof(unsigned int))) {
1417 dev_dbg(dev
->class_dev
,
1418 "copy_from_user failed\n");
1423 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
1426 if (insns
[i
].insn
& INSN_MASK_READ
) {
1427 if (copy_to_user(insns
[i
].data
, data
,
1428 insns
[i
].n
* sizeof(unsigned int))) {
1429 dev_dbg(dev
->class_dev
,
1430 "copy_to_user failed\n");
1450 * synchronous instructions
1456 * struct comedi_insn struct at arg
1462 static int do_insn_ioctl(struct comedi_device
*dev
,
1463 struct comedi_insn __user
*arg
, void *file
)
1465 struct comedi_insn insn
;
1466 unsigned int *data
= NULL
;
1469 data
= kmalloc_array(MAX_SAMPLES
, sizeof(unsigned int), GFP_KERNEL
);
1475 if (copy_from_user(&insn
, arg
, sizeof(insn
))) {
1480 /* This is where the behavior of insn and insnlist deviate. */
1481 if (insn
.n
> MAX_SAMPLES
)
1482 insn
.n
= MAX_SAMPLES
;
1483 if (insn
.insn
& INSN_MASK_WRITE
) {
1484 if (copy_from_user(data
,
1486 insn
.n
* sizeof(unsigned int))) {
1491 ret
= parse_insn(dev
, &insn
, data
, file
);
1494 if (insn
.insn
& INSN_MASK_READ
) {
1495 if (copy_to_user(insn
.data
,
1497 insn
.n
* sizeof(unsigned int))) {
1510 static int __comedi_get_user_cmd(struct comedi_device
*dev
,
1511 struct comedi_cmd __user
*arg
,
1512 struct comedi_cmd
*cmd
)
1514 struct comedi_subdevice
*s
;
1516 if (copy_from_user(cmd
, arg
, sizeof(*cmd
))) {
1517 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1521 if (cmd
->subdev
>= dev
->n_subdevices
) {
1522 dev_dbg(dev
->class_dev
, "%d no such subdevice\n", cmd
->subdev
);
1526 s
= &dev
->subdevices
[cmd
->subdev
];
1528 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1529 dev_dbg(dev
->class_dev
, "%d not valid subdevice\n",
1534 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1535 dev_dbg(dev
->class_dev
,
1536 "subdevice %d does not support commands\n",
1541 /* make sure channel/gain list isn't too long */
1542 if (cmd
->chanlist_len
> s
->len_chanlist
) {
1543 dev_dbg(dev
->class_dev
, "channel/gain list too long %d > %d\n",
1544 cmd
->chanlist_len
, s
->len_chanlist
);
1549 * Set the CMDF_WRITE flag to the correct state if the subdevice
1550 * supports only "read" commands or only "write" commands.
1552 switch (s
->subdev_flags
& (SDF_CMD_READ
| SDF_CMD_WRITE
)) {
1554 cmd
->flags
&= ~CMDF_WRITE
;
1557 cmd
->flags
|= CMDF_WRITE
;
1566 static int __comedi_get_user_chanlist(struct comedi_device
*dev
,
1567 struct comedi_subdevice
*s
,
1568 unsigned int __user
*user_chanlist
,
1569 struct comedi_cmd
*cmd
)
1571 unsigned int *chanlist
;
1574 cmd
->chanlist
= NULL
;
1575 chanlist
= memdup_user(user_chanlist
,
1576 cmd
->chanlist_len
* sizeof(unsigned int));
1577 if (IS_ERR(chanlist
))
1578 return PTR_ERR(chanlist
);
1580 /* make sure each element in channel/gain list is valid */
1581 ret
= comedi_check_chanlist(s
, cmd
->chanlist_len
, chanlist
);
1587 cmd
->chanlist
= chanlist
;
1592 static int do_cmd_ioctl(struct comedi_device
*dev
,
1593 struct comedi_cmd __user
*arg
, void *file
)
1595 struct comedi_cmd cmd
;
1596 struct comedi_subdevice
*s
;
1597 struct comedi_async
*async
;
1598 unsigned int __user
*user_chanlist
;
1601 /* get the user's cmd and do some simple validation */
1602 ret
= __comedi_get_user_cmd(dev
, arg
, &cmd
);
1606 /* save user's chanlist pointer so it can be restored later */
1607 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1609 s
= &dev
->subdevices
[cmd
.subdev
];
1612 /* are we locked? (ioctl lock) */
1613 if (s
->lock
&& s
->lock
!= file
) {
1614 dev_dbg(dev
->class_dev
, "subdevice locked\n");
1620 dev_dbg(dev
->class_dev
, "subdevice busy\n");
1624 /* make sure channel/gain list isn't too short */
1625 if (cmd
.chanlist_len
< 1) {
1626 dev_dbg(dev
->class_dev
, "channel/gain list too short %u < 1\n",
1632 async
->cmd
.data
= NULL
;
1634 /* load channel/gain list */
1635 ret
= __comedi_get_user_chanlist(dev
, s
, user_chanlist
, &async
->cmd
);
1639 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1641 if (async
->cmd
.flags
& CMDF_BOGUS
|| ret
) {
1642 dev_dbg(dev
->class_dev
, "test returned %d\n", ret
);
1644 /* restore chanlist pointer before copying back */
1645 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1647 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1648 dev_dbg(dev
->class_dev
, "fault writing cmd\n");
1656 if (!async
->prealloc_bufsz
) {
1658 dev_dbg(dev
->class_dev
, "no buffer (?)\n");
1662 comedi_buf_reset(s
);
1664 async
->cb_mask
= COMEDI_CB_BLOCK
| COMEDI_CB_CANCEL_MASK
;
1665 if (async
->cmd
.flags
& CMDF_WAKE_EOS
)
1666 async
->cb_mask
|= COMEDI_CB_EOS
;
1668 comedi_set_subdevice_runflags(s
, COMEDI_SRF_BUSY_MASK
,
1669 COMEDI_SRF_RUNNING
);
1672 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
1673 * race with comedi_read() or comedi_write().
1676 ret
= s
->do_cmd(dev
, s
);
1681 do_become_nonbusy(dev
, s
);
1688 command testing ioctl
1691 pointer to cmd structure
1694 cmd structure at arg
1698 modified cmd structure at arg
1701 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1702 struct comedi_cmd __user
*arg
, void *file
)
1704 struct comedi_cmd cmd
;
1705 struct comedi_subdevice
*s
;
1706 unsigned int __user
*user_chanlist
;
1709 /* get the user's cmd and do some simple validation */
1710 ret
= __comedi_get_user_cmd(dev
, arg
, &cmd
);
1714 /* save user's chanlist pointer so it can be restored later */
1715 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1717 s
= &dev
->subdevices
[cmd
.subdev
];
1719 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
1720 if (user_chanlist
) {
1721 /* load channel/gain list */
1722 ret
= __comedi_get_user_chanlist(dev
, s
, user_chanlist
, &cmd
);
1727 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1729 kfree(cmd
.chanlist
); /* free kernel copy of user chanlist */
1731 /* restore chanlist pointer before copying back */
1732 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1734 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1735 dev_dbg(dev
->class_dev
, "bad cmd address\n");
1757 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1761 unsigned long flags
;
1762 struct comedi_subdevice
*s
;
1764 if (arg
>= dev
->n_subdevices
)
1766 s
= &dev
->subdevices
[arg
];
1768 spin_lock_irqsave(&s
->spin_lock
, flags
);
1769 if (s
->busy
|| s
->lock
)
1773 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1791 This function isn't protected by the semaphore, since
1792 we already own the lock.
1794 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1797 struct comedi_subdevice
*s
;
1799 if (arg
>= dev
->n_subdevices
)
1801 s
= &dev
->subdevices
[arg
];
1806 if (s
->lock
&& s
->lock
!= file
)
1809 if (s
->lock
== file
)
1817 cancel acquisition ioctl
1829 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1832 struct comedi_subdevice
*s
;
1834 if (arg
>= dev
->n_subdevices
)
1836 s
= &dev
->subdevices
[arg
];
1837 if (s
->async
== NULL
)
1843 if (s
->busy
!= file
)
1846 return do_cancel(dev
, s
);
1851 instructs driver to synchronize buffers
1863 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1866 struct comedi_subdevice
*s
;
1868 if (arg
>= dev
->n_subdevices
)
1870 s
= &dev
->subdevices
[arg
];
1875 if (s
->busy
!= file
)
1879 return s
->poll(dev
, s
);
1885 * COMEDI_SETRSUBD ioctl
1886 * sets the current "read" subdevice on a per-file basis
1897 static int do_setrsubd_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1900 struct comedi_file
*cfp
= file
->private_data
;
1901 struct comedi_subdevice
*s_old
, *s_new
;
1903 if (arg
>= dev
->n_subdevices
)
1906 s_new
= &dev
->subdevices
[arg
];
1907 s_old
= comedi_file_read_subdevice(file
);
1909 return 0; /* no change */
1911 if (!(s_new
->subdev_flags
& SDF_CMD_READ
))
1915 * Check the file isn't still busy handling a "read" command on the
1916 * old subdevice (if any).
1918 if (s_old
&& s_old
->busy
== file
&& s_old
->async
&&
1919 !(s_old
->async
->cmd
.flags
& CMDF_WRITE
))
1922 ACCESS_ONCE(cfp
->read_subdev
) = s_new
;
1927 * COMEDI_SETWSUBD ioctl
1928 * sets the current "write" subdevice on a per-file basis
1939 static int do_setwsubd_ioctl(struct comedi_device
*dev
, unsigned long arg
,
1942 struct comedi_file
*cfp
= file
->private_data
;
1943 struct comedi_subdevice
*s_old
, *s_new
;
1945 if (arg
>= dev
->n_subdevices
)
1948 s_new
= &dev
->subdevices
[arg
];
1949 s_old
= comedi_file_write_subdevice(file
);
1951 return 0; /* no change */
1953 if (!(s_new
->subdev_flags
& SDF_CMD_WRITE
))
1957 * Check the file isn't still busy handling a "write" command on the
1958 * old subdevice (if any).
1960 if (s_old
&& s_old
->busy
== file
&& s_old
->async
&&
1961 (s_old
->async
->cmd
.flags
& CMDF_WRITE
))
1964 ACCESS_ONCE(cfp
->write_subdev
) = s_new
;
1968 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
1971 unsigned minor
= iminor(file_inode(file
));
1972 struct comedi_file
*cfp
= file
->private_data
;
1973 struct comedi_device
*dev
= cfp
->dev
;
1976 mutex_lock(&dev
->mutex
);
1978 /* Device config is special, because it must work on
1979 * an unconfigured device. */
1980 if (cmd
== COMEDI_DEVCONFIG
) {
1981 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
1982 /* Device config not appropriate on non-board minors. */
1986 rc
= do_devconfig_ioctl(dev
,
1987 (struct comedi_devconfig __user
*)arg
);
1990 dev
->minor
>= comedi_num_legacy_minors
) {
1991 /* Successfully unconfigured a dynamically
1992 * allocated device. Try and remove it. */
1993 if (comedi_clear_board_dev(dev
)) {
1994 mutex_unlock(&dev
->mutex
);
1995 comedi_free_board_dev(dev
);
2003 if (!dev
->attached
) {
2004 dev_dbg(dev
->class_dev
, "no driver attached\n");
2010 case COMEDI_BUFCONFIG
:
2011 rc
= do_bufconfig_ioctl(dev
,
2012 (struct comedi_bufconfig __user
*)arg
);
2014 case COMEDI_DEVINFO
:
2015 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
2018 case COMEDI_SUBDINFO
:
2019 rc
= do_subdinfo_ioctl(dev
,
2020 (struct comedi_subdinfo __user
*)arg
,
2023 case COMEDI_CHANINFO
:
2024 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
2026 case COMEDI_RANGEINFO
:
2027 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
2029 case COMEDI_BUFINFO
:
2030 rc
= do_bufinfo_ioctl(dev
,
2031 (struct comedi_bufinfo __user
*)arg
,
2035 rc
= do_lock_ioctl(dev
, arg
, file
);
2038 rc
= do_unlock_ioctl(dev
, arg
, file
);
2041 rc
= do_cancel_ioctl(dev
, arg
, file
);
2044 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
2046 case COMEDI_CMDTEST
:
2047 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
2050 case COMEDI_INSNLIST
:
2051 rc
= do_insnlist_ioctl(dev
,
2052 (struct comedi_insnlist __user
*)arg
,
2056 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
2060 rc
= do_poll_ioctl(dev
, arg
, file
);
2062 case COMEDI_SETRSUBD
:
2063 rc
= do_setrsubd_ioctl(dev
, arg
, file
);
2065 case COMEDI_SETWSUBD
:
2066 rc
= do_setwsubd_ioctl(dev
, arg
, file
);
2074 mutex_unlock(&dev
->mutex
);
2078 static void comedi_vm_open(struct vm_area_struct
*area
)
2080 struct comedi_buf_map
*bm
;
2082 bm
= area
->vm_private_data
;
2083 comedi_buf_map_get(bm
);
2086 static void comedi_vm_close(struct vm_area_struct
*area
)
2088 struct comedi_buf_map
*bm
;
2090 bm
= area
->vm_private_data
;
2091 comedi_buf_map_put(bm
);
2094 static struct vm_operations_struct comedi_vm_ops
= {
2095 .open
= comedi_vm_open
,
2096 .close
= comedi_vm_close
,
2099 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2101 struct comedi_file
*cfp
= file
->private_data
;
2102 struct comedi_device
*dev
= cfp
->dev
;
2103 struct comedi_subdevice
*s
;
2104 struct comedi_async
*async
;
2105 struct comedi_buf_map
*bm
= NULL
;
2106 unsigned long start
= vma
->vm_start
;
2113 * 'trylock' avoids circular dependency with current->mm->mmap_sem
2114 * and down-reading &dev->attach_lock should normally succeed without
2115 * contention unless the device is in the process of being attached
2118 if (!down_read_trylock(&dev
->attach_lock
))
2121 if (!dev
->attached
) {
2122 dev_dbg(dev
->class_dev
, "no driver attached\n");
2127 if (vma
->vm_flags
& VM_WRITE
)
2128 s
= comedi_file_write_subdevice(file
);
2130 s
= comedi_file_read_subdevice(file
);
2142 if (vma
->vm_pgoff
!= 0) {
2143 dev_dbg(dev
->class_dev
, "mmap() offset must be 0.\n");
2148 size
= vma
->vm_end
- vma
->vm_start
;
2149 if (size
> async
->prealloc_bufsz
) {
2153 if (size
& (~PAGE_MASK
)) {
2158 n_pages
= size
>> PAGE_SHIFT
;
2160 /* get reference to current buf map (if any) */
2161 bm
= comedi_buf_map_from_subdev_get(s
);
2162 if (!bm
|| n_pages
> bm
->n_pages
) {
2166 for (i
= 0; i
< n_pages
; ++i
) {
2167 struct comedi_buf_page
*buf
= &bm
->page_list
[i
];
2169 if (remap_pfn_range(vma
, start
,
2170 page_to_pfn(virt_to_page(buf
->virt_addr
)),
2171 PAGE_SIZE
, PAGE_SHARED
)) {
2178 vma
->vm_ops
= &comedi_vm_ops
;
2179 vma
->vm_private_data
= bm
;
2181 vma
->vm_ops
->open(vma
);
2185 up_read(&dev
->attach_lock
);
2186 comedi_buf_map_put(bm
); /* put reference to buf map - okay if NULL */
2190 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
2192 unsigned int mask
= 0;
2193 struct comedi_file
*cfp
= file
->private_data
;
2194 struct comedi_device
*dev
= cfp
->dev
;
2195 struct comedi_subdevice
*s
;
2197 mutex_lock(&dev
->mutex
);
2199 if (!dev
->attached
) {
2200 dev_dbg(dev
->class_dev
, "no driver attached\n");
2204 s
= comedi_file_read_subdevice(file
);
2205 if (s
&& s
->async
) {
2206 poll_wait(file
, &s
->async
->wait_head
, wait
);
2207 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
2208 (s
->async
->cmd
.flags
& CMDF_WRITE
) ||
2209 comedi_buf_read_n_available(s
) > 0)
2210 mask
|= POLLIN
| POLLRDNORM
;
2213 s
= comedi_file_write_subdevice(file
);
2214 if (s
&& s
->async
) {
2215 unsigned int bps
= comedi_bytes_per_sample(s
);
2217 poll_wait(file
, &s
->async
->wait_head
, wait
);
2218 comedi_buf_write_alloc(s
, s
->async
->prealloc_bufsz
);
2219 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
2220 !(s
->async
->cmd
.flags
& CMDF_WRITE
) ||
2221 comedi_buf_write_n_allocated(s
) >= bps
)
2222 mask
|= POLLOUT
| POLLWRNORM
;
2226 mutex_unlock(&dev
->mutex
);
2230 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
2231 size_t nbytes
, loff_t
*offset
)
2233 struct comedi_subdevice
*s
;
2234 struct comedi_async
*async
;
2235 int n
, m
, count
= 0, retval
= 0;
2236 DECLARE_WAITQUEUE(wait
, current
);
2237 struct comedi_file
*cfp
= file
->private_data
;
2238 struct comedi_device
*dev
= cfp
->dev
;
2239 bool on_wait_queue
= false;
2241 unsigned int old_detach_count
;
2243 /* Protect against device detachment during operation. */
2244 down_read(&dev
->attach_lock
);
2245 attach_locked
= true;
2246 old_detach_count
= dev
->detach_count
;
2248 if (!dev
->attached
) {
2249 dev_dbg(dev
->class_dev
, "no driver attached\n");
2254 s
= comedi_file_write_subdevice(file
);
2255 if (!s
|| !s
->async
) {
2262 if (!s
->busy
|| !nbytes
)
2264 if (s
->busy
!= file
) {
2268 if (!(async
->cmd
.flags
& CMDF_WRITE
)) {
2273 add_wait_queue(&async
->wait_head
, &wait
);
2274 on_wait_queue
= true;
2275 while (nbytes
> 0 && !retval
) {
2276 set_current_state(TASK_INTERRUPTIBLE
);
2278 if (!comedi_is_subdevice_running(s
)) {
2280 struct comedi_subdevice
*new_s
;
2282 if (comedi_is_subdevice_in_error(s
))
2287 * To avoid deadlock, cannot acquire dev->mutex
2288 * while dev->attach_lock is held. Need to
2289 * remove task from the async wait queue before
2290 * releasing dev->attach_lock, as it might not
2291 * be valid afterwards.
2293 remove_wait_queue(&async
->wait_head
, &wait
);
2294 on_wait_queue
= false;
2295 up_read(&dev
->attach_lock
);
2296 attach_locked
= false;
2297 mutex_lock(&dev
->mutex
);
2299 * Become non-busy unless things have changed
2300 * behind our back. Checking dev->detach_count
2301 * is unchanged ought to be sufficient (unless
2302 * there have been 2**32 detaches in the
2303 * meantime!), but check the subdevice pointer
2304 * as well just in case.
2306 new_s
= comedi_file_write_subdevice(file
);
2307 if (dev
->attached
&&
2308 old_detach_count
== dev
->detach_count
&&
2309 s
== new_s
&& new_s
->async
== async
)
2310 do_become_nonbusy(dev
, s
);
2311 mutex_unlock(&dev
->mutex
);
2319 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
2320 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
2321 comedi_buf_write_alloc(s
, async
->prealloc_bufsz
);
2322 if (m
> comedi_buf_write_n_allocated(s
))
2323 m
= comedi_buf_write_n_allocated(s
);
2328 if (file
->f_flags
& O_NONBLOCK
) {
2333 if (signal_pending(current
)) {
2334 retval
= -ERESTARTSYS
;
2339 if (s
->busy
!= file
) {
2343 if (!(async
->cmd
.flags
& CMDF_WRITE
)) {
2350 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
2356 comedi_buf_write_free(s
, n
);
2362 break; /* makes device work like a pipe */
2366 remove_wait_queue(&async
->wait_head
, &wait
);
2367 set_current_state(TASK_RUNNING
);
2369 up_read(&dev
->attach_lock
);
2371 return count
? count
: retval
;
2374 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
2377 struct comedi_subdevice
*s
;
2378 struct comedi_async
*async
;
2379 int n
, m
, count
= 0, retval
= 0;
2380 DECLARE_WAITQUEUE(wait
, current
);
2381 struct comedi_file
*cfp
= file
->private_data
;
2382 struct comedi_device
*dev
= cfp
->dev
;
2383 unsigned int old_detach_count
;
2384 bool become_nonbusy
= false;
2387 /* Protect against device detachment during operation. */
2388 down_read(&dev
->attach_lock
);
2389 attach_locked
= true;
2390 old_detach_count
= dev
->detach_count
;
2392 if (!dev
->attached
) {
2393 dev_dbg(dev
->class_dev
, "no driver attached\n");
2398 s
= comedi_file_read_subdevice(file
);
2399 if (!s
|| !s
->async
) {
2405 if (!s
->busy
|| !nbytes
)
2407 if (s
->busy
!= file
) {
2411 if (async
->cmd
.flags
& CMDF_WRITE
) {
2416 add_wait_queue(&async
->wait_head
, &wait
);
2417 while (nbytes
> 0 && !retval
) {
2418 set_current_state(TASK_INTERRUPTIBLE
);
2422 m
= comedi_buf_read_n_available(s
);
2423 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
2424 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
2429 if (!comedi_is_subdevice_running(s
)) {
2430 if (comedi_is_subdevice_in_error(s
))
2434 become_nonbusy
= true;
2437 if (file
->f_flags
& O_NONBLOCK
) {
2442 if (signal_pending(current
)) {
2443 retval
= -ERESTARTSYS
;
2450 if (s
->busy
!= file
) {
2454 if (async
->cmd
.flags
& CMDF_WRITE
) {
2460 m
= copy_to_user(buf
, async
->prealloc_buf
+
2461 async
->buf_read_ptr
, n
);
2467 comedi_buf_read_alloc(s
, n
);
2468 comedi_buf_read_free(s
, n
);
2474 break; /* makes device work like a pipe */
2476 remove_wait_queue(&async
->wait_head
, &wait
);
2477 set_current_state(TASK_RUNNING
);
2478 if (become_nonbusy
|| comedi_is_subdevice_idle(s
)) {
2479 struct comedi_subdevice
*new_s
;
2482 * To avoid deadlock, cannot acquire dev->mutex
2483 * while dev->attach_lock is held.
2485 up_read(&dev
->attach_lock
);
2486 attach_locked
= false;
2487 mutex_lock(&dev
->mutex
);
2489 * Check device hasn't become detached behind our back.
2490 * Checking dev->detach_count is unchanged ought to be
2491 * sufficient (unless there have been 2**32 detaches in the
2492 * meantime!), but check the subdevice pointer as well just in
2495 new_s
= comedi_file_read_subdevice(file
);
2496 if (dev
->attached
&& old_detach_count
== dev
->detach_count
&&
2497 s
== new_s
&& new_s
->async
== async
) {
2498 if (become_nonbusy
|| comedi_buf_n_bytes_ready(s
) == 0)
2499 do_become_nonbusy(dev
, s
);
2501 mutex_unlock(&dev
->mutex
);
2505 up_read(&dev
->attach_lock
);
2507 return count
? count
: retval
;
2510 static int comedi_open(struct inode
*inode
, struct file
*file
)
2512 const unsigned minor
= iminor(inode
);
2513 struct comedi_file
*cfp
;
2514 struct comedi_device
*dev
= comedi_dev_get_from_minor(minor
);
2518 pr_debug("invalid minor number\n");
2522 cfp
= kzalloc(sizeof(*cfp
), GFP_KERNEL
);
2528 mutex_lock(&dev
->mutex
);
2529 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
2530 dev_dbg(dev
->class_dev
, "not attached and not CAP_NET_ADMIN\n");
2534 if (dev
->attached
&& dev
->use_count
== 0) {
2535 if (!try_module_get(dev
->driver
->module
)) {
2540 rc
= dev
->open(dev
);
2542 module_put(dev
->driver
->module
);
2549 file
->private_data
= cfp
;
2550 comedi_file_reset(file
);
2554 mutex_unlock(&dev
->mutex
);
2556 comedi_dev_put(dev
);
2562 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2564 struct comedi_file
*cfp
= file
->private_data
;
2565 struct comedi_device
*dev
= cfp
->dev
;
2567 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2570 static int comedi_close(struct inode
*inode
, struct file
*file
)
2572 struct comedi_file
*cfp
= file
->private_data
;
2573 struct comedi_device
*dev
= cfp
->dev
;
2574 struct comedi_subdevice
*s
= NULL
;
2577 mutex_lock(&dev
->mutex
);
2579 if (dev
->subdevices
) {
2580 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2581 s
= &dev
->subdevices
[i
];
2583 if (s
->busy
== file
)
2585 if (s
->lock
== file
)
2589 if (dev
->attached
&& dev
->use_count
== 1) {
2592 module_put(dev
->driver
->module
);
2597 mutex_unlock(&dev
->mutex
);
2598 comedi_dev_put(dev
);
2604 static const struct file_operations comedi_fops
= {
2605 .owner
= THIS_MODULE
,
2606 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2607 .compat_ioctl
= comedi_compat_ioctl
,
2608 .open
= comedi_open
,
2609 .release
= comedi_close
,
2610 .read
= comedi_read
,
2611 .write
= comedi_write
,
2612 .mmap
= comedi_mmap
,
2613 .poll
= comedi_poll
,
2614 .fasync
= comedi_fasync
,
2615 .llseek
= noop_llseek
,
2619 * comedi_event - handle events for asynchronous comedi command
2620 * @dev: comedi_device struct
2621 * @s: comedi_subdevice struct associated with dev
2622 * Context: interrupt (usually), s->spin_lock spin-lock not held
2624 * If an asynchronous comedi command is active on the subdevice, process
2625 * any COMEDI_CB_... event flags that have been set, usually by an
2626 * interrupt handler. These may change the run state of the asynchronous
2627 * command, wake a task, and/or send a SIGIO signal.
2629 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2631 struct comedi_async
*async
= s
->async
;
2632 unsigned runflags
= 0;
2633 unsigned runflags_mask
= 0;
2635 if (!comedi_is_subdevice_running(s
))
2638 if (s
->async
->events
& COMEDI_CB_CANCEL_MASK
)
2639 runflags_mask
|= COMEDI_SRF_RUNNING
;
2642 * Remember if an error event has occurred, so an error
2643 * can be returned the next time the user does a read().
2645 if (s
->async
->events
& COMEDI_CB_ERROR_MASK
) {
2646 runflags_mask
|= COMEDI_SRF_ERROR
;
2647 runflags
|= COMEDI_SRF_ERROR
;
2649 if (runflags_mask
) {
2651 * Sets COMEDI_SRF_ERROR and COMEDI_SRF_RUNNING together
2654 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2657 if (async
->cb_mask
& s
->async
->events
) {
2658 wake_up_interruptible(&async
->wait_head
);
2659 if (s
->subdev_flags
& SDF_CMD_READ
)
2660 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2661 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2662 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2664 s
->async
->events
= 0;
2666 EXPORT_SYMBOL_GPL(comedi_event
);
2668 /* Note: the ->mutex is pre-locked on successful return */
2669 struct comedi_device
*comedi_alloc_board_minor(struct device
*hardware_device
)
2671 struct comedi_device
*dev
;
2672 struct device
*csdev
;
2675 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
2677 return ERR_PTR(-ENOMEM
);
2678 comedi_device_init(dev
);
2679 comedi_set_hw_dev(dev
, hardware_device
);
2680 mutex_lock(&dev
->mutex
);
2681 mutex_lock(&comedi_board_minor_table_lock
);
2682 for (i
= hardware_device
? comedi_num_legacy_minors
: 0;
2683 i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2684 if (comedi_board_minor_table
[i
] == NULL
) {
2685 comedi_board_minor_table
[i
] = dev
;
2689 mutex_unlock(&comedi_board_minor_table_lock
);
2690 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2691 mutex_unlock(&dev
->mutex
);
2692 comedi_device_cleanup(dev
);
2693 comedi_dev_put(dev
);
2694 pr_err("ran out of minor numbers for board device files\n");
2695 return ERR_PTR(-EBUSY
);
2698 csdev
= device_create(comedi_class
, hardware_device
,
2699 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2701 dev
->class_dev
= get_device(csdev
);
2703 /* Note: dev->mutex needs to be unlocked by the caller. */
2707 static void comedi_free_board_minor(unsigned minor
)
2709 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2710 comedi_free_board_dev(comedi_clear_board_minor(minor
));
2713 void comedi_release_hardware_device(struct device
*hardware_device
)
2716 struct comedi_device
*dev
;
2718 for (minor
= comedi_num_legacy_minors
; minor
< COMEDI_NUM_BOARD_MINORS
;
2720 mutex_lock(&comedi_board_minor_table_lock
);
2721 dev
= comedi_board_minor_table
[minor
];
2722 if (dev
&& dev
->hw_dev
== hardware_device
) {
2723 comedi_board_minor_table
[minor
] = NULL
;
2724 mutex_unlock(&comedi_board_minor_table_lock
);
2725 comedi_free_board_dev(dev
);
2728 mutex_unlock(&comedi_board_minor_table_lock
);
2732 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2734 struct comedi_device
*dev
= s
->device
;
2735 struct device
*csdev
;
2738 mutex_lock(&comedi_subdevice_minor_table_lock
);
2739 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
) {
2740 if (comedi_subdevice_minor_table
[i
] == NULL
) {
2741 comedi_subdevice_minor_table
[i
] = s
;
2745 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2746 if (i
== COMEDI_NUM_SUBDEVICE_MINORS
) {
2747 pr_err("ran out of minor numbers for subdevice files\n");
2750 i
+= COMEDI_NUM_BOARD_MINORS
;
2752 csdev
= device_create(comedi_class
, dev
->class_dev
,
2753 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2754 dev
->minor
, s
->index
);
2756 s
->class_dev
= csdev
;
2761 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2770 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2771 BUG_ON(s
->minor
< COMEDI_NUM_BOARD_MINORS
);
2773 i
= s
->minor
- COMEDI_NUM_BOARD_MINORS
;
2774 mutex_lock(&comedi_subdevice_minor_table_lock
);
2775 if (s
== comedi_subdevice_minor_table
[i
])
2776 comedi_subdevice_minor_table
[i
] = NULL
;
2777 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2779 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2780 s
->class_dev
= NULL
;
2784 static void comedi_cleanup_board_minors(void)
2788 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++)
2789 comedi_free_board_minor(i
);
2792 static int __init
comedi_init(void)
2797 pr_info("version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2799 if (comedi_num_legacy_minors
< 0 ||
2800 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2801 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2802 COMEDI_NUM_BOARD_MINORS
);
2806 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2807 COMEDI_NUM_MINORS
, "comedi");
2810 cdev_init(&comedi_cdev
, &comedi_fops
);
2811 comedi_cdev
.owner
= THIS_MODULE
;
2813 retval
= kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2815 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2820 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2821 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2825 comedi_class
= class_create(THIS_MODULE
, "comedi");
2826 if (IS_ERR(comedi_class
)) {
2827 pr_err("failed to create class\n");
2828 cdev_del(&comedi_cdev
);
2829 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2831 return PTR_ERR(comedi_class
);
2834 comedi_class
->dev_groups
= comedi_dev_groups
;
2836 /* XXX requires /proc interface */
2839 /* create devices files for legacy/manual use */
2840 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2841 struct comedi_device
*dev
;
2843 dev
= comedi_alloc_board_minor(NULL
);
2845 comedi_cleanup_board_minors();
2846 cdev_del(&comedi_cdev
);
2847 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2849 return PTR_ERR(dev
);
2851 /* comedi_alloc_board_minor() locked the mutex */
2852 mutex_unlock(&dev
->mutex
);
2857 module_init(comedi_init
);
2859 static void __exit
comedi_cleanup(void)
2863 comedi_cleanup_board_minors();
2864 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
)
2865 BUG_ON(comedi_board_minor_table
[i
]);
2866 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
)
2867 BUG_ON(comedi_subdevice_minor_table
[i
]);
2869 class_destroy(comedi_class
);
2870 cdev_del(&comedi_cdev
);
2871 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2873 comedi_proc_cleanup();
2875 module_exit(comedi_cleanup
);
2877 MODULE_AUTHOR("http://www.comedi.org");
2878 MODULE_DESCRIPTION("Comedi core module");
2879 MODULE_LICENSE("GPL");