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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "comedi_compat32.h"
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/fcntl.h>
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/kmod.h>
38 #include <linux/poll.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/vmalloc.h>
43 #include "comedidev.h"
44 #include <linux/cdev.h>
45 #include <linux/stat.h>
48 #include <linux/uaccess.h>
50 #include "comedi_internal.h"
52 #define COMEDI_NUM_MINORS 0x100
53 #define COMEDI_NUM_SUBDEVICE_MINORS \
54 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
56 #ifdef CONFIG_COMEDI_DEBUG
58 EXPORT_SYMBOL(comedi_debug
);
59 module_param(comedi_debug
, int, S_IRUGO
| S_IWUSR
);
60 MODULE_PARM_DESC(comedi_debug
,
61 "enable comedi core and driver debugging if non-zero (default 0)"
65 static int comedi_num_legacy_minors
;
66 module_param(comedi_num_legacy_minors
, int, S_IRUGO
);
67 MODULE_PARM_DESC(comedi_num_legacy_minors
,
68 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
71 unsigned int comedi_default_buf_size_kb
= CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
;
72 module_param(comedi_default_buf_size_kb
, uint
, S_IRUGO
| S_IWUSR
);
73 MODULE_PARM_DESC(comedi_default_buf_size_kb
,
74 "default asynchronous buffer size in KiB (default "
75 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB
) ")");
77 unsigned int comedi_default_buf_maxsize_kb
78 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
;
79 module_param(comedi_default_buf_maxsize_kb
, uint
, S_IRUGO
| S_IWUSR
);
80 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb
,
81 "default maximum size of asynchronous buffer in KiB (default "
82 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB
) ")");
84 struct comedi_file_info
{
85 struct comedi_device
*device
;
86 struct comedi_subdevice
*read_subdevice
;
87 struct comedi_subdevice
*write_subdevice
;
90 static DEFINE_MUTEX(comedi_board_minor_table_lock
);
91 static struct comedi_file_info
92 *comedi_board_minor_table
[COMEDI_NUM_BOARD_MINORS
];
94 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock
);
95 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
96 static struct comedi_file_info
97 *comedi_subdevice_minor_table
[COMEDI_NUM_SUBDEVICE_MINORS
];
99 static struct class *comedi_class
;
100 static struct cdev comedi_cdev
;
102 static void comedi_device_init(struct comedi_device
*dev
)
104 spin_lock_init(&dev
->spinlock
);
105 mutex_init(&dev
->mutex
);
109 static void comedi_device_cleanup(struct comedi_device
*dev
)
111 struct module
*driver_module
= NULL
;
115 mutex_lock(&dev
->mutex
);
117 driver_module
= dev
->driver
->module
;
118 comedi_device_detach(dev
);
119 while (dev
->use_count
> 0) {
121 module_put(driver_module
);
122 module_put(THIS_MODULE
);
125 mutex_unlock(&dev
->mutex
);
126 mutex_destroy(&dev
->mutex
);
129 static struct comedi_file_info
*comedi_clear_board_minor(unsigned minor
)
131 struct comedi_file_info
*info
;
133 mutex_lock(&comedi_board_minor_table_lock
);
134 info
= comedi_board_minor_table
[minor
];
135 comedi_board_minor_table
[minor
] = NULL
;
136 mutex_unlock(&comedi_board_minor_table_lock
);
140 static struct comedi_file_info
*comedi_clear_subdevice_minor(unsigned minor
)
142 struct comedi_file_info
*info
;
143 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
145 mutex_lock(&comedi_subdevice_minor_table_lock
);
146 info
= comedi_subdevice_minor_table
[i
];
147 comedi_subdevice_minor_table
[i
] = NULL
;
148 mutex_unlock(&comedi_subdevice_minor_table_lock
);
152 static void comedi_free_board_file_info(struct comedi_file_info
*info
)
155 struct comedi_device
*dev
= info
->device
;
157 if (dev
->class_dev
) {
158 device_destroy(comedi_class
,
159 MKDEV(COMEDI_MAJOR
, dev
->minor
));
161 comedi_device_cleanup(dev
);
168 static struct comedi_file_info
169 *comedi_file_info_from_board_minor(unsigned minor
)
171 struct comedi_file_info
*info
;
173 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
174 mutex_lock(&comedi_board_minor_table_lock
);
175 info
= comedi_board_minor_table
[minor
];
176 mutex_unlock(&comedi_board_minor_table_lock
);
180 static struct comedi_file_info
181 *comedi_file_info_from_subdevice_minor(unsigned minor
)
183 struct comedi_file_info
*info
;
184 unsigned int i
= minor
- COMEDI_NUM_BOARD_MINORS
;
186 BUG_ON(i
>= COMEDI_NUM_SUBDEVICE_MINORS
);
187 mutex_lock(&comedi_subdevice_minor_table_lock
);
188 info
= comedi_subdevice_minor_table
[i
];
189 mutex_unlock(&comedi_subdevice_minor_table_lock
);
193 static struct comedi_file_info
*comedi_file_info_from_minor(unsigned minor
)
195 if (minor
< COMEDI_NUM_BOARD_MINORS
)
196 return comedi_file_info_from_board_minor(minor
);
198 return comedi_file_info_from_subdevice_minor(minor
);
201 static struct comedi_device
*
202 comedi_dev_from_file_info(struct comedi_file_info
*info
)
204 return info
? info
->device
: NULL
;
207 struct comedi_device
*comedi_dev_from_minor(unsigned minor
)
209 return comedi_dev_from_file_info(comedi_file_info_from_minor(minor
));
211 EXPORT_SYMBOL_GPL(comedi_dev_from_minor
);
213 static struct comedi_subdevice
*
214 comedi_read_subdevice(const struct comedi_file_info
*info
)
216 if (info
->read_subdevice
)
217 return info
->read_subdevice
;
219 return info
->device
->read_subdev
;
223 static struct comedi_subdevice
*
224 comedi_write_subdevice(const struct comedi_file_info
*info
)
226 if (info
->write_subdevice
)
227 return info
->write_subdevice
;
229 return info
->device
->write_subdev
;
233 static int resize_async_buffer(struct comedi_device
*dev
,
234 struct comedi_subdevice
*s
,
235 struct comedi_async
*async
, unsigned new_size
)
239 if (new_size
> async
->max_bufsize
)
243 DPRINTK("subdevice is busy, cannot resize buffer\n");
246 if (async
->mmap_count
) {
247 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
251 if (!async
->prealloc_buf
)
254 /* make sure buffer is an integral number of pages
256 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
258 retval
= comedi_buf_alloc(dev
, s
, new_size
);
263 retval
= s
->buf_change(dev
, s
, new_size
);
268 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
269 dev
->minor
, s
->index
, async
->prealloc_bufsz
);
273 /* sysfs attribute files */
275 static ssize_t
show_max_read_buffer_kb(struct device
*csdev
,
276 struct device_attribute
*attr
, char *buf
)
278 struct comedi_file_info
*info
= dev_get_drvdata(csdev
);
279 struct comedi_device
*dev
;
280 struct comedi_subdevice
*s
;
281 unsigned int size
= 0;
284 mutex_lock(&dev
->mutex
);
285 s
= comedi_read_subdevice(info
);
286 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
287 size
= s
->async
->max_bufsize
/ 1024;
288 mutex_unlock(&dev
->mutex
);
290 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
293 static ssize_t
store_max_read_buffer_kb(struct device
*csdev
,
294 struct device_attribute
*attr
,
295 const char *buf
, size_t count
)
297 struct comedi_file_info
*info
= dev_get_drvdata(csdev
);
298 struct comedi_device
*dev
;
299 struct comedi_subdevice
*s
;
303 err
= kstrtouint(buf
, 10, &size
);
306 if (size
> (UINT_MAX
/ 1024))
311 mutex_lock(&dev
->mutex
);
312 s
= comedi_read_subdevice(info
);
313 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
314 s
->async
->max_bufsize
= size
;
317 mutex_unlock(&dev
->mutex
);
319 return err
? err
: count
;
322 static ssize_t
show_read_buffer_kb(struct device
*csdev
,
323 struct device_attribute
*attr
, char *buf
)
325 struct comedi_file_info
*info
= dev_get_drvdata(csdev
);
326 struct comedi_device
*dev
;
327 struct comedi_subdevice
*s
;
328 unsigned int size
= 0;
331 mutex_lock(&dev
->mutex
);
332 s
= comedi_read_subdevice(info
);
333 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
334 size
= s
->async
->prealloc_bufsz
/ 1024;
335 mutex_unlock(&dev
->mutex
);
337 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
340 static ssize_t
store_read_buffer_kb(struct device
*csdev
,
341 struct device_attribute
*attr
,
342 const char *buf
, size_t count
)
344 struct comedi_file_info
*info
= dev_get_drvdata(csdev
);
345 struct comedi_device
*dev
;
346 struct comedi_subdevice
*s
;
350 err
= kstrtouint(buf
, 10, &size
);
353 if (size
> (UINT_MAX
/ 1024))
358 mutex_lock(&dev
->mutex
);
359 s
= comedi_read_subdevice(info
);
360 if (s
&& (s
->subdev_flags
& SDF_CMD_READ
) && s
->async
)
361 err
= resize_async_buffer(dev
, s
, s
->async
, size
);
364 mutex_unlock(&dev
->mutex
);
366 return err
? err
: count
;
369 static ssize_t
show_max_write_buffer_kb(struct device
*csdev
,
370 struct device_attribute
*attr
,
373 struct comedi_file_info
*info
= dev_get_drvdata(csdev
);
374 struct comedi_device
*dev
;
375 struct comedi_subdevice
*s
;
376 unsigned int size
= 0;
379 mutex_lock(&dev
->mutex
);
380 s
= comedi_write_subdevice(info
);
381 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
382 size
= s
->async
->max_bufsize
/ 1024;
383 mutex_unlock(&dev
->mutex
);
385 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
388 static ssize_t
store_max_write_buffer_kb(struct device
*csdev
,
389 struct device_attribute
*attr
,
390 const char *buf
, size_t count
)
392 struct comedi_file_info
*info
= dev_get_drvdata(csdev
);
393 struct comedi_device
*dev
;
394 struct comedi_subdevice
*s
;
398 err
= kstrtouint(buf
, 10, &size
);
401 if (size
> (UINT_MAX
/ 1024))
406 mutex_lock(&dev
->mutex
);
407 s
= comedi_write_subdevice(info
);
408 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
409 s
->async
->max_bufsize
= size
;
412 mutex_unlock(&dev
->mutex
);
414 return err
? err
: count
;
417 static ssize_t
show_write_buffer_kb(struct device
*csdev
,
418 struct device_attribute
*attr
, char *buf
)
420 struct comedi_file_info
*info
= dev_get_drvdata(csdev
);
421 struct comedi_device
*dev
;
422 struct comedi_subdevice
*s
;
423 unsigned int size
= 0;
426 mutex_lock(&dev
->mutex
);
427 s
= comedi_write_subdevice(info
);
428 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
429 size
= s
->async
->prealloc_bufsz
/ 1024;
430 mutex_unlock(&dev
->mutex
);
432 return snprintf(buf
, PAGE_SIZE
, "%i\n", size
);
435 static ssize_t
store_write_buffer_kb(struct device
*csdev
,
436 struct device_attribute
*attr
,
437 const char *buf
, size_t count
)
439 struct comedi_file_info
*info
= dev_get_drvdata(csdev
);
440 struct comedi_device
*dev
;
441 struct comedi_subdevice
*s
;
445 err
= kstrtouint(buf
, 10, &size
);
448 if (size
> (UINT_MAX
/ 1024))
453 mutex_lock(&dev
->mutex
);
454 s
= comedi_write_subdevice(info
);
455 if (s
&& (s
->subdev_flags
& SDF_CMD_WRITE
) && s
->async
)
456 err
= resize_async_buffer(dev
, s
, s
->async
, size
);
459 mutex_unlock(&dev
->mutex
);
461 return err
? err
: count
;
464 static struct device_attribute comedi_dev_attrs
[] = {
465 __ATTR(max_read_buffer_kb
, S_IRUGO
| S_IWUSR
,
466 show_max_read_buffer_kb
, store_max_read_buffer_kb
),
467 __ATTR(read_buffer_kb
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
468 show_read_buffer_kb
, store_read_buffer_kb
),
469 __ATTR(max_write_buffer_kb
, S_IRUGO
| S_IWUSR
,
470 show_max_write_buffer_kb
, store_max_write_buffer_kb
),
471 __ATTR(write_buffer_kb
, S_IRUGO
| S_IWUSR
| S_IWGRP
,
472 show_write_buffer_kb
, store_write_buffer_kb
),
476 static void comedi_set_subdevice_runflags(struct comedi_subdevice
*s
,
477 unsigned mask
, unsigned bits
)
481 spin_lock_irqsave(&s
->spin_lock
, flags
);
482 s
->runflags
&= ~mask
;
483 s
->runflags
|= (bits
& mask
);
484 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
487 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice
*s
)
492 spin_lock_irqsave(&s
->spin_lock
, flags
);
493 runflags
= s
->runflags
;
494 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
498 bool comedi_is_subdevice_running(struct comedi_subdevice
*s
)
500 unsigned runflags
= comedi_get_subdevice_runflags(s
);
502 return (runflags
& SRF_RUNNING
) ? true : false;
504 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running
);
506 static bool comedi_is_subdevice_in_error(struct comedi_subdevice
*s
)
508 unsigned runflags
= comedi_get_subdevice_runflags(s
);
510 return (runflags
& SRF_ERROR
) ? true : false;
513 static bool comedi_is_subdevice_idle(struct comedi_subdevice
*s
)
515 unsigned runflags
= comedi_get_subdevice_runflags(s
);
517 return (runflags
& (SRF_ERROR
| SRF_RUNNING
)) ? false : true;
521 This function restores a subdevice to an idle state.
523 static void do_become_nonbusy(struct comedi_device
*dev
,
524 struct comedi_subdevice
*s
)
526 struct comedi_async
*async
= s
->async
;
528 comedi_set_subdevice_runflags(s
, SRF_RUNNING
, 0);
530 comedi_buf_reset(async
);
531 async
->inttrig
= NULL
;
532 kfree(async
->cmd
.chanlist
);
533 async
->cmd
.chanlist
= NULL
;
535 dev_err(dev
->class_dev
,
536 "BUG: (?) do_become_nonbusy called with async=NULL\n");
542 static int do_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
546 if (comedi_is_subdevice_running(s
) && s
->cancel
)
547 ret
= s
->cancel(dev
, s
);
549 do_become_nonbusy(dev
, s
);
554 static int is_device_busy(struct comedi_device
*dev
)
556 struct comedi_subdevice
*s
;
562 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
563 s
= &dev
->subdevices
[i
];
566 if (s
->async
&& s
->async
->mmap_count
)
578 pointer to devconfig structure
581 devconfig structure at arg
586 static int do_devconfig_ioctl(struct comedi_device
*dev
,
587 struct comedi_devconfig __user
*arg
)
589 struct comedi_devconfig it
;
591 if (!capable(CAP_SYS_ADMIN
))
595 if (is_device_busy(dev
))
598 struct module
*driver_module
= dev
->driver
->module
;
599 comedi_device_detach(dev
);
600 module_put(driver_module
);
605 if (copy_from_user(&it
, arg
, sizeof(it
)))
608 it
.board_name
[COMEDI_NAMELEN
- 1] = 0;
610 if (it
.options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
611 dev_warn(dev
->class_dev
,
612 "comedi_config --init_data is deprecated\n");
616 if (dev
->minor
>= comedi_num_legacy_minors
)
617 /* don't re-use dynamically allocated comedi devices */
620 /* This increments the driver module count on success. */
621 return comedi_device_attach(dev
, &it
);
626 buffer configuration ioctl
629 pointer to bufconfig structure
635 modified bufconfig at arg
638 static int do_bufconfig_ioctl(struct comedi_device
*dev
,
639 struct comedi_bufconfig __user
*arg
)
641 struct comedi_bufconfig bc
;
642 struct comedi_async
*async
;
643 struct comedi_subdevice
*s
;
646 if (copy_from_user(&bc
, arg
, sizeof(bc
)))
649 if (bc
.subdevice
>= dev
->n_subdevices
|| bc
.subdevice
< 0)
652 s
= &dev
->subdevices
[bc
.subdevice
];
656 DPRINTK("subdevice does not have async capability\n");
662 if (bc
.maximum_size
) {
663 if (!capable(CAP_SYS_ADMIN
))
666 async
->max_bufsize
= bc
.maximum_size
;
670 retval
= resize_async_buffer(dev
, s
, async
, bc
.size
);
675 bc
.size
= async
->prealloc_bufsz
;
676 bc
.maximum_size
= async
->max_bufsize
;
679 if (copy_to_user(arg
, &bc
, sizeof(bc
)))
690 pointer to devinfo structure
699 static int do_devinfo_ioctl(struct comedi_device
*dev
,
700 struct comedi_devinfo __user
*arg
,
703 const unsigned minor
= iminor(file_inode(file
));
704 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
705 struct comedi_subdevice
*s
;
706 struct comedi_devinfo devinfo
;
708 memset(&devinfo
, 0, sizeof(devinfo
));
710 /* fill devinfo structure */
711 devinfo
.version_code
= COMEDI_VERSION_CODE
;
712 devinfo
.n_subdevs
= dev
->n_subdevices
;
713 strlcpy(devinfo
.driver_name
, dev
->driver
->driver_name
, COMEDI_NAMELEN
);
714 strlcpy(devinfo
.board_name
, dev
->board_name
, COMEDI_NAMELEN
);
716 s
= comedi_read_subdevice(info
);
718 devinfo
.read_subdevice
= s
->index
;
720 devinfo
.read_subdevice
= -1;
722 s
= comedi_write_subdevice(info
);
724 devinfo
.write_subdevice
= s
->index
;
726 devinfo
.write_subdevice
= -1;
728 if (copy_to_user(arg
, &devinfo
, sizeof(devinfo
)))
739 pointer to array of subdevice info structures
745 array of subdevice info structures at arg
748 static int do_subdinfo_ioctl(struct comedi_device
*dev
,
749 struct comedi_subdinfo __user
*arg
, void *file
)
752 struct comedi_subdinfo
*tmp
, *us
;
753 struct comedi_subdevice
*s
;
755 tmp
= kcalloc(dev
->n_subdevices
, sizeof(*tmp
), GFP_KERNEL
);
759 /* fill subdinfo structs */
760 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
761 s
= &dev
->subdevices
[i
];
765 us
->n_chan
= s
->n_chan
;
766 us
->subd_flags
= s
->subdev_flags
;
767 if (comedi_is_subdevice_running(s
))
768 us
->subd_flags
|= SDF_RUNNING
;
769 #define TIMER_nanosec 5 /* backwards compatibility */
770 us
->timer_type
= TIMER_nanosec
;
771 us
->len_chanlist
= s
->len_chanlist
;
772 us
->maxdata
= s
->maxdata
;
773 if (s
->range_table
) {
775 (i
<< 24) | (0 << 16) | (s
->range_table
->length
);
777 us
->range_type
= 0; /* XXX */
779 us
->flags
= s
->flags
;
782 us
->subd_flags
|= SDF_BUSY
;
784 us
->subd_flags
|= SDF_BUSY_OWNER
;
786 us
->subd_flags
|= SDF_LOCKED
;
788 us
->subd_flags
|= SDF_LOCK_OWNER
;
789 if (!s
->maxdata
&& s
->maxdata_list
)
790 us
->subd_flags
|= SDF_MAXDATA
;
792 us
->subd_flags
|= SDF_FLAGS
;
793 if (s
->range_table_list
)
794 us
->subd_flags
|= SDF_RANGETYPE
;
796 us
->subd_flags
|= SDF_CMD
;
798 if (s
->insn_bits
!= &insn_inval
)
799 us
->insn_bits_support
= COMEDI_SUPPORTED
;
801 us
->insn_bits_support
= COMEDI_UNSUPPORTED
;
803 us
->settling_time_0
= s
->settling_time_0
;
806 ret
= copy_to_user(arg
, tmp
, dev
->n_subdevices
* sizeof(*tmp
));
810 return ret
? -EFAULT
: 0;
818 pointer to chaninfo structure
821 chaninfo structure at arg
824 arrays at elements of chaninfo structure
827 static int do_chaninfo_ioctl(struct comedi_device
*dev
,
828 struct comedi_chaninfo __user
*arg
)
830 struct comedi_subdevice
*s
;
831 struct comedi_chaninfo it
;
833 if (copy_from_user(&it
, arg
, sizeof(it
)))
836 if (it
.subdev
>= dev
->n_subdevices
)
838 s
= &dev
->subdevices
[it
.subdev
];
840 if (it
.maxdata_list
) {
841 if (s
->maxdata
|| !s
->maxdata_list
)
843 if (copy_to_user(it
.maxdata_list
, s
->maxdata_list
,
844 s
->n_chan
* sizeof(unsigned int)))
851 if (copy_to_user(it
.flaglist
, s
->flaglist
,
852 s
->n_chan
* sizeof(unsigned int)))
859 if (!s
->range_table_list
)
861 for (i
= 0; i
< s
->n_chan
; i
++) {
864 x
= (dev
->minor
<< 28) | (it
.subdev
<< 24) | (i
<< 16) |
865 (s
->range_table_list
[i
]->length
);
866 if (put_user(x
, it
.rangelist
+ i
))
870 if (copy_to_user(it
.rangelist
, s
->range_type_list
,
871 s
->n_chan
* sizeof(unsigned int)))
881 buffer information ioctl
884 pointer to bufinfo structure
890 modified bufinfo at arg
893 static int do_bufinfo_ioctl(struct comedi_device
*dev
,
894 struct comedi_bufinfo __user
*arg
, void *file
)
896 struct comedi_bufinfo bi
;
897 struct comedi_subdevice
*s
;
898 struct comedi_async
*async
;
900 if (copy_from_user(&bi
, arg
, sizeof(bi
)))
903 if (bi
.subdevice
>= dev
->n_subdevices
|| bi
.subdevice
< 0)
906 s
= &dev
->subdevices
[bi
.subdevice
];
908 if (s
->lock
&& s
->lock
!= file
)
914 DPRINTK("subdevice does not have async capability\n");
915 bi
.buf_write_ptr
= 0;
917 bi
.buf_write_count
= 0;
918 bi
.buf_read_count
= 0;
920 bi
.bytes_written
= 0;
925 bi
.bytes_written
= 0;
926 goto copyback_position
;
931 if (bi
.bytes_read
&& (s
->subdev_flags
& SDF_CMD_READ
)) {
932 bi
.bytes_read
= comedi_buf_read_alloc(async
, bi
.bytes_read
);
933 comedi_buf_read_free(async
, bi
.bytes_read
);
935 if (comedi_is_subdevice_idle(s
) &&
936 async
->buf_write_count
== async
->buf_read_count
) {
937 do_become_nonbusy(dev
, s
);
941 if (bi
.bytes_written
&& (s
->subdev_flags
& SDF_CMD_WRITE
)) {
943 comedi_buf_write_alloc(async
, bi
.bytes_written
);
944 comedi_buf_write_free(async
, bi
.bytes_written
);
948 bi
.buf_write_count
= async
->buf_write_count
;
949 bi
.buf_write_ptr
= async
->buf_write_ptr
;
950 bi
.buf_read_count
= async
->buf_read_count
;
951 bi
.buf_read_ptr
= async
->buf_read_ptr
;
954 if (copy_to_user(arg
, &bi
, sizeof(bi
)))
960 static int check_insn_config_length(struct comedi_insn
*insn
,
967 case INSN_CONFIG_DIO_OUTPUT
:
968 case INSN_CONFIG_DIO_INPUT
:
969 case INSN_CONFIG_DISARM
:
970 case INSN_CONFIG_RESET
:
974 case INSN_CONFIG_ARM
:
975 case INSN_CONFIG_DIO_QUERY
:
976 case INSN_CONFIG_BLOCK_SIZE
:
977 case INSN_CONFIG_FILTER
:
978 case INSN_CONFIG_SERIAL_CLOCK
:
979 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
980 case INSN_CONFIG_ALT_SOURCE
:
981 case INSN_CONFIG_SET_COUNTER_MODE
:
982 case INSN_CONFIG_8254_READ_STATUS
:
983 case INSN_CONFIG_SET_ROUTING
:
984 case INSN_CONFIG_GET_ROUTING
:
985 case INSN_CONFIG_GET_PWM_STATUS
:
986 case INSN_CONFIG_PWM_SET_PERIOD
:
987 case INSN_CONFIG_PWM_GET_PERIOD
:
991 case INSN_CONFIG_SET_GATE_SRC
:
992 case INSN_CONFIG_GET_GATE_SRC
:
993 case INSN_CONFIG_SET_CLOCK_SRC
:
994 case INSN_CONFIG_GET_CLOCK_SRC
:
995 case INSN_CONFIG_SET_OTHER_SRC
:
996 case INSN_CONFIG_GET_COUNTER_STATUS
:
997 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
998 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
999 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
1003 case INSN_CONFIG_PWM_OUTPUT
:
1004 case INSN_CONFIG_ANALOG_TRIG
:
1008 case INSN_CONFIG_DIGITAL_TRIG
:
1012 /* by default we allow the insn since we don't have checks for
1013 * all possible cases yet */
1015 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1017 pr_warn("comedi: Add a check to %s in %s.\n",
1018 __func__
, __FILE__
);
1019 pr_warn("comedi: Assuming n=%i is correct.\n", insn
->n
);
1025 static int parse_insn(struct comedi_device
*dev
, struct comedi_insn
*insn
,
1026 unsigned int *data
, void *file
)
1028 struct comedi_subdevice
*s
;
1032 if (insn
->insn
& INSN_MASK_SPECIAL
) {
1033 /* a non-subdevice instruction */
1035 switch (insn
->insn
) {
1045 do_gettimeofday(&tv
);
1046 data
[0] = tv
.tv_sec
;
1047 data
[1] = tv
.tv_usec
;
1053 if (insn
->n
!= 1 || data
[0] >= 100000) {
1057 udelay(data
[0] / 1000);
1065 if (insn
->subdev
>= dev
->n_subdevices
) {
1066 DPRINTK("%d not usable subdevice\n",
1071 s
= &dev
->subdevices
[insn
->subdev
];
1073 DPRINTK("no async\n");
1077 if (!s
->async
->inttrig
) {
1078 DPRINTK("no inttrig\n");
1082 ret
= s
->async
->inttrig(dev
, s
, data
[0]);
1087 DPRINTK("invalid insn\n");
1092 /* a subdevice instruction */
1093 unsigned int maxdata
;
1095 if (insn
->subdev
>= dev
->n_subdevices
) {
1096 DPRINTK("subdevice %d out of range\n", insn
->subdev
);
1100 s
= &dev
->subdevices
[insn
->subdev
];
1102 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1103 DPRINTK("%d not usable subdevice\n", insn
->subdev
);
1108 /* are we locked? (ioctl lock) */
1109 if (s
->lock
&& s
->lock
!= file
) {
1110 DPRINTK("device locked\n");
1115 ret
= comedi_check_chanlist(s
, 1, &insn
->chanspec
);
1118 DPRINTK("bad chanspec\n");
1126 /* This looks arbitrary. It is. */
1127 s
->busy
= &parse_insn
;
1128 switch (insn
->insn
) {
1130 ret
= s
->insn_read(dev
, s
, insn
, data
);
1133 maxdata
= s
->maxdata_list
1134 ? s
->maxdata_list
[CR_CHAN(insn
->chanspec
)]
1136 for (i
= 0; i
< insn
->n
; ++i
) {
1137 if (data
[i
] > maxdata
) {
1139 DPRINTK("bad data value(s)\n");
1144 ret
= s
->insn_write(dev
, s
, insn
, data
);
1150 /* Most drivers ignore the base channel in
1151 * insn->chanspec. Fix this here if
1152 * the subdevice has <= 32 channels. */
1154 unsigned int orig_mask
;
1156 orig_mask
= data
[0];
1157 if (s
->n_chan
<= 32) {
1158 shift
= CR_CHAN(insn
->chanspec
);
1166 ret
= s
->insn_bits(dev
, s
, insn
, data
);
1167 data
[0] = orig_mask
;
1173 ret
= check_insn_config_length(insn
, data
);
1176 ret
= s
->insn_config(dev
, s
, insn
, data
);
1192 * synchronous instructions
1195 * pointer to sync cmd structure
1198 * sync cmd struct at arg
1205 /* arbitrary limits */
1206 #define MAX_SAMPLES 256
1207 static int do_insnlist_ioctl(struct comedi_device
*dev
,
1208 struct comedi_insnlist __user
*arg
, void *file
)
1210 struct comedi_insnlist insnlist
;
1211 struct comedi_insn
*insns
= NULL
;
1212 unsigned int *data
= NULL
;
1216 if (copy_from_user(&insnlist
, arg
, sizeof(insnlist
)))
1219 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1221 DPRINTK("kmalloc failed\n");
1226 insns
= kcalloc(insnlist
.n_insns
, sizeof(*insns
), GFP_KERNEL
);
1228 DPRINTK("kmalloc failed\n");
1233 if (copy_from_user(insns
, insnlist
.insns
,
1234 sizeof(*insns
) * insnlist
.n_insns
)) {
1235 DPRINTK("copy_from_user failed\n");
1240 for (i
= 0; i
< insnlist
.n_insns
; i
++) {
1241 if (insns
[i
].n
> MAX_SAMPLES
) {
1242 DPRINTK("number of samples too large\n");
1246 if (insns
[i
].insn
& INSN_MASK_WRITE
) {
1247 if (copy_from_user(data
, insns
[i
].data
,
1248 insns
[i
].n
* sizeof(unsigned int))) {
1249 DPRINTK("copy_from_user failed\n");
1254 ret
= parse_insn(dev
, insns
+ i
, data
, file
);
1257 if (insns
[i
].insn
& INSN_MASK_READ
) {
1258 if (copy_to_user(insns
[i
].data
, data
,
1259 insns
[i
].n
* sizeof(unsigned int))) {
1260 DPRINTK("copy_to_user failed\n");
1280 * synchronous instructions
1286 * struct comedi_insn struct at arg
1292 static int do_insn_ioctl(struct comedi_device
*dev
,
1293 struct comedi_insn __user
*arg
, void *file
)
1295 struct comedi_insn insn
;
1296 unsigned int *data
= NULL
;
1299 data
= kmalloc(sizeof(unsigned int) * MAX_SAMPLES
, GFP_KERNEL
);
1305 if (copy_from_user(&insn
, arg
, sizeof(insn
))) {
1310 /* This is where the behavior of insn and insnlist deviate. */
1311 if (insn
.n
> MAX_SAMPLES
)
1312 insn
.n
= MAX_SAMPLES
;
1313 if (insn
.insn
& INSN_MASK_WRITE
) {
1314 if (copy_from_user(data
,
1316 insn
.n
* sizeof(unsigned int))) {
1321 ret
= parse_insn(dev
, &insn
, data
, file
);
1324 if (insn
.insn
& INSN_MASK_READ
) {
1325 if (copy_to_user(insn
.data
,
1327 insn
.n
* sizeof(unsigned int))) {
1340 static int do_cmd_ioctl(struct comedi_device
*dev
,
1341 struct comedi_cmd __user
*arg
, void *file
)
1343 struct comedi_cmd cmd
;
1344 struct comedi_subdevice
*s
;
1345 struct comedi_async
*async
;
1347 unsigned int __user
*user_chanlist
;
1349 if (copy_from_user(&cmd
, arg
, sizeof(cmd
))) {
1350 DPRINTK("bad cmd address\n");
1353 /* save user's chanlist pointer so it can be restored later */
1354 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1356 if (cmd
.subdev
>= dev
->n_subdevices
) {
1357 DPRINTK("%d no such subdevice\n", cmd
.subdev
);
1361 s
= &dev
->subdevices
[cmd
.subdev
];
1364 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1365 DPRINTK("%d not valid subdevice\n", cmd
.subdev
);
1369 if (!s
->do_cmd
|| !s
->do_cmdtest
|| !s
->async
) {
1370 DPRINTK("subdevice %i does not support commands\n",
1375 /* are we locked? (ioctl lock) */
1376 if (s
->lock
&& s
->lock
!= file
) {
1377 DPRINTK("subdevice locked\n");
1383 DPRINTK("subdevice busy\n");
1388 /* make sure channel/gain list isn't too long */
1389 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1390 DPRINTK("channel/gain list too long %u > %d\n",
1391 cmd
.chanlist_len
, s
->len_chanlist
);
1396 /* make sure channel/gain list isn't too short */
1397 if (cmd
.chanlist_len
< 1) {
1398 DPRINTK("channel/gain list too short %u < 1\n",
1405 async
->cmd
.data
= NULL
;
1406 /* load channel/gain list */
1407 async
->cmd
.chanlist
=
1408 kmalloc(async
->cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1409 if (!async
->cmd
.chanlist
) {
1410 DPRINTK("allocation failed\n");
1415 if (copy_from_user(async
->cmd
.chanlist
, user_chanlist
,
1416 async
->cmd
.chanlist_len
* sizeof(int))) {
1417 DPRINTK("fault reading chanlist\n");
1422 /* make sure each element in channel/gain list is valid */
1423 ret
= comedi_check_chanlist(s
,
1424 async
->cmd
.chanlist_len
,
1425 async
->cmd
.chanlist
);
1427 DPRINTK("bad chanlist\n");
1431 ret
= s
->do_cmdtest(dev
, s
, &async
->cmd
);
1433 if (async
->cmd
.flags
& TRIG_BOGUS
|| ret
) {
1434 DPRINTK("test returned %d\n", ret
);
1436 /* restore chanlist pointer before copying back */
1437 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1439 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1440 DPRINTK("fault writing cmd\n");
1448 if (!async
->prealloc_bufsz
) {
1450 DPRINTK("no buffer (?)\n");
1454 comedi_buf_reset(async
);
1457 COMEDI_CB_EOA
| COMEDI_CB_BLOCK
| COMEDI_CB_ERROR
|
1459 if (async
->cmd
.flags
& TRIG_WAKE_EOS
)
1460 async
->cb_mask
|= COMEDI_CB_EOS
;
1462 comedi_set_subdevice_runflags(s
, ~0, SRF_USER
| SRF_RUNNING
);
1464 ret
= s
->do_cmd(dev
, s
);
1469 do_become_nonbusy(dev
, s
);
1476 command testing ioctl
1479 pointer to cmd structure
1482 cmd structure at arg
1486 modified cmd structure at arg
1489 static int do_cmdtest_ioctl(struct comedi_device
*dev
,
1490 struct comedi_cmd __user
*arg
, void *file
)
1492 struct comedi_cmd cmd
;
1493 struct comedi_subdevice
*s
;
1495 unsigned int *chanlist
= NULL
;
1496 unsigned int __user
*user_chanlist
;
1498 if (copy_from_user(&cmd
, arg
, sizeof(cmd
))) {
1499 DPRINTK("bad cmd address\n");
1502 /* save user's chanlist pointer so it can be restored later */
1503 user_chanlist
= (unsigned int __user
*)cmd
.chanlist
;
1505 if (cmd
.subdev
>= dev
->n_subdevices
) {
1506 DPRINTK("%d no such subdevice\n", cmd
.subdev
);
1510 s
= &dev
->subdevices
[cmd
.subdev
];
1511 if (s
->type
== COMEDI_SUBD_UNUSED
) {
1512 DPRINTK("%d not valid subdevice\n", cmd
.subdev
);
1516 if (!s
->do_cmd
|| !s
->do_cmdtest
) {
1517 DPRINTK("subdevice %i does not support commands\n",
1522 /* make sure channel/gain list isn't too long */
1523 if (cmd
.chanlist_len
> s
->len_chanlist
) {
1524 DPRINTK("channel/gain list too long %d > %d\n",
1525 cmd
.chanlist_len
, s
->len_chanlist
);
1530 /* load channel/gain list */
1533 kmalloc(cmd
.chanlist_len
* sizeof(int), GFP_KERNEL
);
1535 DPRINTK("allocation failed\n");
1540 if (copy_from_user(chanlist
, user_chanlist
,
1541 cmd
.chanlist_len
* sizeof(int))) {
1542 DPRINTK("fault reading chanlist\n");
1547 /* make sure each element in channel/gain list is valid */
1548 ret
= comedi_check_chanlist(s
, cmd
.chanlist_len
, chanlist
);
1550 DPRINTK("bad chanlist\n");
1554 cmd
.chanlist
= chanlist
;
1557 ret
= s
->do_cmdtest(dev
, s
, &cmd
);
1559 /* restore chanlist pointer before copying back */
1560 cmd
.chanlist
= (unsigned int __force
*)user_chanlist
;
1562 if (copy_to_user(arg
, &cmd
, sizeof(cmd
))) {
1563 DPRINTK("bad cmd address\n");
1588 static int do_lock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1592 unsigned long flags
;
1593 struct comedi_subdevice
*s
;
1595 if (arg
>= dev
->n_subdevices
)
1597 s
= &dev
->subdevices
[arg
];
1599 spin_lock_irqsave(&s
->spin_lock
, flags
);
1600 if (s
->busy
|| s
->lock
)
1604 spin_unlock_irqrestore(&s
->spin_lock
, flags
);
1611 ret
= s
->lock_f(dev
, s
);
1630 This function isn't protected by the semaphore, since
1631 we already own the lock.
1633 static int do_unlock_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1636 struct comedi_subdevice
*s
;
1638 if (arg
>= dev
->n_subdevices
)
1640 s
= &dev
->subdevices
[arg
];
1645 if (s
->lock
&& s
->lock
!= file
)
1648 if (s
->lock
== file
) {
1662 cancel acquisition ioctl
1674 static int do_cancel_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1677 struct comedi_subdevice
*s
;
1679 if (arg
>= dev
->n_subdevices
)
1681 s
= &dev
->subdevices
[arg
];
1682 if (s
->async
== NULL
)
1685 if (s
->lock
&& s
->lock
!= file
)
1691 if (s
->busy
!= file
)
1694 return do_cancel(dev
, s
);
1699 instructs driver to synchronize buffers
1711 static int do_poll_ioctl(struct comedi_device
*dev
, unsigned int arg
,
1714 struct comedi_subdevice
*s
;
1716 if (arg
>= dev
->n_subdevices
)
1718 s
= &dev
->subdevices
[arg
];
1720 if (s
->lock
&& s
->lock
!= file
)
1726 if (s
->busy
!= file
)
1730 return s
->poll(dev
, s
);
1735 static long comedi_unlocked_ioctl(struct file
*file
, unsigned int cmd
,
1738 const unsigned minor
= iminor(file_inode(file
));
1739 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1740 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1746 mutex_lock(&dev
->mutex
);
1748 /* Device config is special, because it must work on
1749 * an unconfigured device. */
1750 if (cmd
== COMEDI_DEVCONFIG
) {
1751 if (minor
>= COMEDI_NUM_BOARD_MINORS
) {
1752 /* Device config not appropriate on non-board minors. */
1756 rc
= do_devconfig_ioctl(dev
,
1757 (struct comedi_devconfig __user
*)arg
);
1760 dev
->minor
>= comedi_num_legacy_minors
) {
1761 /* Successfully unconfigured a dynamically
1762 * allocated device. Try and remove it. */
1763 info
= comedi_clear_board_minor(dev
->minor
);
1765 mutex_unlock(&dev
->mutex
);
1766 comedi_free_board_file_info(info
);
1774 if (!dev
->attached
) {
1775 DPRINTK("no driver configured on /dev/comedi%i\n", dev
->minor
);
1781 case COMEDI_BUFCONFIG
:
1782 rc
= do_bufconfig_ioctl(dev
,
1783 (struct comedi_bufconfig __user
*)arg
);
1785 case COMEDI_DEVINFO
:
1786 rc
= do_devinfo_ioctl(dev
, (struct comedi_devinfo __user
*)arg
,
1789 case COMEDI_SUBDINFO
:
1790 rc
= do_subdinfo_ioctl(dev
,
1791 (struct comedi_subdinfo __user
*)arg
,
1794 case COMEDI_CHANINFO
:
1795 rc
= do_chaninfo_ioctl(dev
, (void __user
*)arg
);
1797 case COMEDI_RANGEINFO
:
1798 rc
= do_rangeinfo_ioctl(dev
, (void __user
*)arg
);
1800 case COMEDI_BUFINFO
:
1801 rc
= do_bufinfo_ioctl(dev
,
1802 (struct comedi_bufinfo __user
*)arg
,
1806 rc
= do_lock_ioctl(dev
, arg
, file
);
1809 rc
= do_unlock_ioctl(dev
, arg
, file
);
1812 rc
= do_cancel_ioctl(dev
, arg
, file
);
1815 rc
= do_cmd_ioctl(dev
, (struct comedi_cmd __user
*)arg
, file
);
1817 case COMEDI_CMDTEST
:
1818 rc
= do_cmdtest_ioctl(dev
, (struct comedi_cmd __user
*)arg
,
1821 case COMEDI_INSNLIST
:
1822 rc
= do_insnlist_ioctl(dev
,
1823 (struct comedi_insnlist __user
*)arg
,
1827 rc
= do_insn_ioctl(dev
, (struct comedi_insn __user
*)arg
,
1831 rc
= do_poll_ioctl(dev
, arg
, file
);
1839 mutex_unlock(&dev
->mutex
);
1843 static void comedi_vm_open(struct vm_area_struct
*area
)
1845 struct comedi_async
*async
;
1846 struct comedi_device
*dev
;
1848 async
= area
->vm_private_data
;
1849 dev
= async
->subdevice
->device
;
1851 mutex_lock(&dev
->mutex
);
1852 async
->mmap_count
++;
1853 mutex_unlock(&dev
->mutex
);
1856 static void comedi_vm_close(struct vm_area_struct
*area
)
1858 struct comedi_async
*async
;
1859 struct comedi_device
*dev
;
1861 async
= area
->vm_private_data
;
1862 dev
= async
->subdevice
->device
;
1864 mutex_lock(&dev
->mutex
);
1865 async
->mmap_count
--;
1866 mutex_unlock(&dev
->mutex
);
1869 static struct vm_operations_struct comedi_vm_ops
= {
1870 .open
= comedi_vm_open
,
1871 .close
= comedi_vm_close
,
1874 static int comedi_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1876 const unsigned minor
= iminor(file_inode(file
));
1877 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1878 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1879 struct comedi_subdevice
*s
;
1880 struct comedi_async
*async
;
1881 unsigned long start
= vma
->vm_start
;
1890 mutex_lock(&dev
->mutex
);
1892 if (!dev
->attached
) {
1893 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1898 if (vma
->vm_flags
& VM_WRITE
)
1899 s
= comedi_write_subdevice(info
);
1901 s
= comedi_read_subdevice(info
);
1913 if (vma
->vm_pgoff
!= 0) {
1914 DPRINTK("comedi: mmap() offset must be 0.\n");
1919 size
= vma
->vm_end
- vma
->vm_start
;
1920 if (size
> async
->prealloc_bufsz
) {
1924 if (size
& (~PAGE_MASK
)) {
1929 n_pages
= size
>> PAGE_SHIFT
;
1930 for (i
= 0; i
< n_pages
; ++i
) {
1931 struct comedi_buf_page
*buf
= &async
->buf_page_list
[i
];
1933 if (remap_pfn_range(vma
, start
,
1934 page_to_pfn(virt_to_page(buf
->virt_addr
)),
1935 PAGE_SIZE
, PAGE_SHARED
)) {
1942 vma
->vm_ops
= &comedi_vm_ops
;
1943 vma
->vm_private_data
= async
;
1945 async
->mmap_count
++;
1949 mutex_unlock(&dev
->mutex
);
1953 static unsigned int comedi_poll(struct file
*file
, poll_table
*wait
)
1955 unsigned int mask
= 0;
1956 const unsigned minor
= iminor(file_inode(file
));
1957 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
1958 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
1959 struct comedi_subdevice
*s
;
1964 mutex_lock(&dev
->mutex
);
1966 if (!dev
->attached
) {
1967 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
1971 s
= comedi_read_subdevice(info
);
1972 if (s
&& s
->async
) {
1973 poll_wait(file
, &s
->async
->wait_head
, wait
);
1974 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
1975 comedi_buf_read_n_available(s
->async
) > 0)
1976 mask
|= POLLIN
| POLLRDNORM
;
1979 s
= comedi_write_subdevice(info
);
1980 if (s
&& s
->async
) {
1981 unsigned int bps
= bytes_per_sample(s
->async
->subdevice
);
1983 poll_wait(file
, &s
->async
->wait_head
, wait
);
1984 comedi_buf_write_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1985 if (!s
->busy
|| !comedi_is_subdevice_running(s
) ||
1986 comedi_buf_write_n_allocated(s
->async
) >= bps
)
1987 mask
|= POLLOUT
| POLLWRNORM
;
1991 mutex_unlock(&dev
->mutex
);
1995 static ssize_t
comedi_write(struct file
*file
, const char __user
*buf
,
1996 size_t nbytes
, loff_t
*offset
)
1998 struct comedi_subdevice
*s
;
1999 struct comedi_async
*async
;
2000 int n
, m
, count
= 0, retval
= 0;
2001 DECLARE_WAITQUEUE(wait
, current
);
2002 const unsigned minor
= iminor(file_inode(file
));
2003 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
2004 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
2009 if (!dev
->attached
) {
2010 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
2014 s
= comedi_write_subdevice(info
);
2015 if (!s
|| !s
->async
)
2020 if (!s
->busy
|| !nbytes
)
2022 if (s
->busy
!= file
)
2025 add_wait_queue(&async
->wait_head
, &wait
);
2026 while (nbytes
> 0 && !retval
) {
2027 set_current_state(TASK_INTERRUPTIBLE
);
2029 if (!comedi_is_subdevice_running(s
)) {
2031 if (comedi_is_subdevice_in_error(s
))
2035 do_become_nonbusy(dev
, s
);
2043 if (async
->buf_write_ptr
+ m
> async
->prealloc_bufsz
)
2044 m
= async
->prealloc_bufsz
- async
->buf_write_ptr
;
2045 comedi_buf_write_alloc(async
, async
->prealloc_bufsz
);
2046 if (m
> comedi_buf_write_n_allocated(async
))
2047 m
= comedi_buf_write_n_allocated(async
);
2052 if (file
->f_flags
& O_NONBLOCK
) {
2057 if (signal_pending(current
)) {
2058 retval
= -ERESTARTSYS
;
2063 if (s
->busy
!= file
) {
2070 m
= copy_from_user(async
->prealloc_buf
+ async
->buf_write_ptr
,
2076 comedi_buf_write_free(async
, n
);
2082 break; /* makes device work like a pipe */
2084 set_current_state(TASK_RUNNING
);
2085 remove_wait_queue(&async
->wait_head
, &wait
);
2087 return count
? count
: retval
;
2090 static ssize_t
comedi_read(struct file
*file
, char __user
*buf
, size_t nbytes
,
2093 struct comedi_subdevice
*s
;
2094 struct comedi_async
*async
;
2095 int n
, m
, count
= 0, retval
= 0;
2096 DECLARE_WAITQUEUE(wait
, current
);
2097 const unsigned minor
= iminor(file_inode(file
));
2098 struct comedi_file_info
*info
= comedi_file_info_from_minor(minor
);
2099 struct comedi_device
*dev
= comedi_dev_from_file_info(info
);
2104 if (!dev
->attached
) {
2105 DPRINTK("no driver configured on comedi%i\n", dev
->minor
);
2109 s
= comedi_read_subdevice(info
);
2110 if (!s
|| !s
->async
)
2114 if (!s
->busy
|| !nbytes
)
2116 if (s
->busy
!= file
)
2119 add_wait_queue(&async
->wait_head
, &wait
);
2120 while (nbytes
> 0 && !retval
) {
2121 set_current_state(TASK_INTERRUPTIBLE
);
2125 m
= comedi_buf_read_n_available(async
);
2126 /* printk("%d available\n",m); */
2127 if (async
->buf_read_ptr
+ m
> async
->prealloc_bufsz
)
2128 m
= async
->prealloc_bufsz
- async
->buf_read_ptr
;
2129 /* printk("%d contiguous\n",m); */
2134 if (!comedi_is_subdevice_running(s
)) {
2135 do_become_nonbusy(dev
, s
);
2136 if (comedi_is_subdevice_in_error(s
))
2142 if (file
->f_flags
& O_NONBLOCK
) {
2147 if (signal_pending(current
)) {
2148 retval
= -ERESTARTSYS
;
2155 if (s
->busy
!= file
) {
2161 m
= copy_to_user(buf
, async
->prealloc_buf
+
2162 async
->buf_read_ptr
, n
);
2168 comedi_buf_read_alloc(async
, n
);
2169 comedi_buf_read_free(async
, n
);
2175 break; /* makes device work like a pipe */
2177 if (comedi_is_subdevice_idle(s
) &&
2178 async
->buf_read_count
- async
->buf_write_count
== 0) {
2179 do_become_nonbusy(dev
, s
);
2181 set_current_state(TASK_RUNNING
);
2182 remove_wait_queue(&async
->wait_head
, &wait
);
2184 return count
? count
: retval
;
2187 static int comedi_open(struct inode
*inode
, struct file
*file
)
2189 const unsigned minor
= iminor(inode
);
2190 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2193 DPRINTK("invalid minor number\n");
2197 /* This is slightly hacky, but we want module autoloading
2199 * case: user opens device, attached -> ok
2200 * case: user opens device, unattached, !in_request_module -> autoload
2201 * case: user opens device, unattached, in_request_module -> fail
2202 * case: root opens device, attached -> ok
2203 * case: root opens device, unattached, in_request_module -> ok
2204 * (typically called from modprobe)
2205 * case: root opens device, unattached, !in_request_module -> autoload
2207 * The last could be changed to "-> ok", which would deny root
2210 mutex_lock(&dev
->mutex
);
2213 if (!capable(CAP_NET_ADMIN
) && dev
->in_request_module
) {
2214 DPRINTK("in request module\n");
2215 mutex_unlock(&dev
->mutex
);
2218 if (capable(CAP_NET_ADMIN
) && dev
->in_request_module
)
2221 dev
->in_request_module
= true;
2224 mutex_unlock(&dev
->mutex
);
2225 request_module("char-major-%i-%i", COMEDI_MAJOR
, dev
->minor
);
2226 mutex_lock(&dev
->mutex
);
2229 dev
->in_request_module
= false;
2231 if (!dev
->attached
&& !capable(CAP_NET_ADMIN
)) {
2232 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2233 mutex_unlock(&dev
->mutex
);
2237 __module_get(THIS_MODULE
);
2239 if (dev
->attached
) {
2240 if (!try_module_get(dev
->driver
->module
)) {
2241 module_put(THIS_MODULE
);
2242 mutex_unlock(&dev
->mutex
);
2247 if (dev
->attached
&& dev
->use_count
== 0 && dev
->open
) {
2248 int rc
= dev
->open(dev
);
2250 module_put(dev
->driver
->module
);
2251 module_put(THIS_MODULE
);
2252 mutex_unlock(&dev
->mutex
);
2259 mutex_unlock(&dev
->mutex
);
2264 static int comedi_fasync(int fd
, struct file
*file
, int on
)
2266 const unsigned minor
= iminor(file_inode(file
));
2267 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2272 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
2275 static int comedi_close(struct inode
*inode
, struct file
*file
)
2277 const unsigned minor
= iminor(inode
);
2278 struct comedi_device
*dev
= comedi_dev_from_minor(minor
);
2279 struct comedi_subdevice
*s
= NULL
;
2285 mutex_lock(&dev
->mutex
);
2287 if (dev
->subdevices
) {
2288 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
2289 s
= &dev
->subdevices
[i
];
2291 if (s
->busy
== file
)
2293 if (s
->lock
== file
)
2297 if (dev
->attached
&& dev
->use_count
== 1 && dev
->close
)
2300 module_put(THIS_MODULE
);
2302 module_put(dev
->driver
->module
);
2306 mutex_unlock(&dev
->mutex
);
2308 if (file
->f_flags
& FASYNC
)
2309 comedi_fasync(-1, file
, 0);
2314 static const struct file_operations comedi_fops
= {
2315 .owner
= THIS_MODULE
,
2316 .unlocked_ioctl
= comedi_unlocked_ioctl
,
2317 .compat_ioctl
= comedi_compat_ioctl
,
2318 .open
= comedi_open
,
2319 .release
= comedi_close
,
2320 .read
= comedi_read
,
2321 .write
= comedi_write
,
2322 .mmap
= comedi_mmap
,
2323 .poll
= comedi_poll
,
2324 .fasync
= comedi_fasync
,
2325 .llseek
= noop_llseek
,
2328 void comedi_error(const struct comedi_device
*dev
, const char *s
)
2330 dev_err(dev
->class_dev
, "%s: %s\n", dev
->driver
->driver_name
, s
);
2332 EXPORT_SYMBOL(comedi_error
);
2334 void comedi_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2336 struct comedi_async
*async
= s
->async
;
2337 unsigned runflags
= 0;
2338 unsigned runflags_mask
= 0;
2340 /* DPRINTK("comedi_event 0x%x\n",mask); */
2342 if (!comedi_is_subdevice_running(s
))
2346 async
->events
& (COMEDI_CB_EOA
| COMEDI_CB_ERROR
|
2347 COMEDI_CB_OVERFLOW
)) {
2348 runflags_mask
|= SRF_RUNNING
;
2350 /* remember if an error event has occurred, so an error
2351 * can be returned the next time the user does a read() */
2352 if (s
->async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
)) {
2353 runflags_mask
|= SRF_ERROR
;
2354 runflags
|= SRF_ERROR
;
2356 if (runflags_mask
) {
2357 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2358 comedi_set_subdevice_runflags(s
, runflags_mask
, runflags
);
2361 if (async
->cb_mask
& s
->async
->events
) {
2362 if (comedi_get_subdevice_runflags(s
) & SRF_USER
) {
2363 wake_up_interruptible(&async
->wait_head
);
2364 if (s
->subdev_flags
& SDF_CMD_READ
)
2365 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
2366 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2367 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_OUT
);
2370 async
->cb_func(s
->async
->events
, async
->cb_arg
);
2373 s
->async
->events
= 0;
2375 EXPORT_SYMBOL(comedi_event
);
2377 /* Note: the ->mutex is pre-locked on successful return */
2378 struct comedi_device
*comedi_alloc_board_minor(struct device
*hardware_device
)
2380 struct comedi_file_info
*info
;
2381 struct comedi_device
*dev
;
2382 struct device
*csdev
;
2385 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
2387 return ERR_PTR(-ENOMEM
);
2388 dev
= kzalloc(sizeof(struct comedi_device
), GFP_KERNEL
);
2391 return ERR_PTR(-ENOMEM
);
2394 comedi_device_init(dev
);
2395 comedi_set_hw_dev(dev
, hardware_device
);
2396 mutex_lock(&dev
->mutex
);
2397 mutex_lock(&comedi_board_minor_table_lock
);
2398 for (i
= hardware_device
? comedi_num_legacy_minors
: 0;
2399 i
< COMEDI_NUM_BOARD_MINORS
; ++i
) {
2400 if (comedi_board_minor_table
[i
] == NULL
) {
2401 comedi_board_minor_table
[i
] = info
;
2405 mutex_unlock(&comedi_board_minor_table_lock
);
2406 if (i
== COMEDI_NUM_BOARD_MINORS
) {
2407 mutex_unlock(&dev
->mutex
);
2408 comedi_device_cleanup(dev
);
2411 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2412 return ERR_PTR(-EBUSY
);
2415 csdev
= device_create(comedi_class
, hardware_device
,
2416 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i", i
);
2418 dev
->class_dev
= csdev
;
2419 dev_set_drvdata(csdev
, info
);
2421 /* Note: dev->mutex needs to be unlocked by the caller. */
2425 static void comedi_free_board_minor(unsigned minor
)
2427 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
2428 comedi_free_board_file_info(comedi_clear_board_minor(minor
));
2431 void comedi_release_hardware_device(struct device
*hardware_device
)
2434 struct comedi_file_info
*info
;
2436 for (minor
= comedi_num_legacy_minors
; minor
< COMEDI_NUM_BOARD_MINORS
;
2438 mutex_lock(&comedi_board_minor_table_lock
);
2439 info
= comedi_board_minor_table
[minor
];
2440 if (info
&& info
->device
->hw_dev
== hardware_device
) {
2441 comedi_board_minor_table
[minor
] = NULL
;
2442 mutex_unlock(&comedi_board_minor_table_lock
);
2443 comedi_free_board_file_info(info
);
2446 mutex_unlock(&comedi_board_minor_table_lock
);
2450 int comedi_alloc_subdevice_minor(struct comedi_subdevice
*s
)
2452 struct comedi_device
*dev
= s
->device
;
2453 struct comedi_file_info
*info
;
2454 struct device
*csdev
;
2457 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
2461 if (s
->subdev_flags
& SDF_CMD_READ
)
2462 info
->read_subdevice
= s
;
2463 if (s
->subdev_flags
& SDF_CMD_WRITE
)
2464 info
->write_subdevice
= s
;
2465 mutex_lock(&comedi_subdevice_minor_table_lock
);
2466 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
) {
2467 if (comedi_subdevice_minor_table
[i
] == NULL
) {
2468 comedi_subdevice_minor_table
[i
] = info
;
2472 mutex_unlock(&comedi_subdevice_minor_table_lock
);
2473 if (i
== COMEDI_NUM_SUBDEVICE_MINORS
) {
2475 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2478 i
+= COMEDI_NUM_BOARD_MINORS
;
2480 csdev
= device_create(comedi_class
, dev
->class_dev
,
2481 MKDEV(COMEDI_MAJOR
, i
), NULL
, "comedi%i_subd%i",
2482 dev
->minor
, s
->index
);
2484 s
->class_dev
= csdev
;
2485 dev_set_drvdata(csdev
, info
);
2490 void comedi_free_subdevice_minor(struct comedi_subdevice
*s
)
2492 struct comedi_file_info
*info
;
2499 BUG_ON(s
->minor
>= COMEDI_NUM_MINORS
);
2500 BUG_ON(s
->minor
< COMEDI_NUM_BOARD_MINORS
);
2502 info
= comedi_clear_subdevice_minor(s
->minor
);
2504 device_destroy(comedi_class
, MKDEV(COMEDI_MAJOR
, s
->minor
));
2505 s
->class_dev
= NULL
;
2510 static void comedi_cleanup_board_minors(void)
2514 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++)
2515 comedi_free_board_minor(i
);
2518 static int __init
comedi_init(void)
2523 pr_info("comedi: version " COMEDI_RELEASE
" - http://www.comedi.org\n");
2525 if (comedi_num_legacy_minors
< 0 ||
2526 comedi_num_legacy_minors
> COMEDI_NUM_BOARD_MINORS
) {
2527 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2528 COMEDI_NUM_BOARD_MINORS
);
2532 retval
= register_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2533 COMEDI_NUM_MINORS
, "comedi");
2536 cdev_init(&comedi_cdev
, &comedi_fops
);
2537 comedi_cdev
.owner
= THIS_MODULE
;
2538 kobject_set_name(&comedi_cdev
.kobj
, "comedi");
2539 if (cdev_add(&comedi_cdev
, MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
)) {
2540 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2544 comedi_class
= class_create(THIS_MODULE
, "comedi");
2545 if (IS_ERR(comedi_class
)) {
2546 pr_err("comedi: failed to create class\n");
2547 cdev_del(&comedi_cdev
);
2548 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2550 return PTR_ERR(comedi_class
);
2553 comedi_class
->dev_attrs
= comedi_dev_attrs
;
2555 /* XXX requires /proc interface */
2558 /* create devices files for legacy/manual use */
2559 for (i
= 0; i
< comedi_num_legacy_minors
; i
++) {
2560 struct comedi_device
*dev
;
2561 dev
= comedi_alloc_board_minor(NULL
);
2563 comedi_cleanup_board_minors();
2564 cdev_del(&comedi_cdev
);
2565 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0),
2567 return PTR_ERR(dev
);
2569 /* comedi_alloc_board_minor() locked the mutex */
2570 mutex_unlock(&dev
->mutex
);
2576 module_init(comedi_init
);
2578 static void __exit
comedi_cleanup(void)
2582 comedi_cleanup_board_minors();
2583 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; ++i
)
2584 BUG_ON(comedi_board_minor_table
[i
]);
2585 for (i
= 0; i
< COMEDI_NUM_SUBDEVICE_MINORS
; ++i
)
2586 BUG_ON(comedi_subdevice_minor_table
[i
]);
2588 class_destroy(comedi_class
);
2589 cdev_del(&comedi_cdev
);
2590 unregister_chrdev_region(MKDEV(COMEDI_MAJOR
, 0), COMEDI_NUM_MINORS
);
2592 comedi_proc_cleanup();
2594 module_exit(comedi_cleanup
);
2596 MODULE_AUTHOR("http://www.comedi.org");
2597 MODULE_DESCRIPTION("Comedi core module");
2598 MODULE_LICENSE("GPL");