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 #include "comedi_compat32.h"
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/fcntl.h>
26 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/kmod.h>
30 #include <linux/poll.h>
31 #include <linux/init.h>
32 #include <linux/device.h>
33 #include <linux/vmalloc.h>
35 #include "comedidev.h"
36 #include <linux/cdev.h>
37 #include <linux/stat.h>
40 #include <linux/uaccess.h>
42 #include "comedi_internal.h"
44 #define COMEDI_NUM_MINORS 0x100
45 #define COMEDI_NUM_SUBDEVICE_MINORS \
46 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
48 static int comedi_num_legacy_minors;
49 module_param(comedi_num_legacy_minors, int, S_IRUGO);
50 MODULE_PARM_DESC(comedi_num_legacy_minors,
51 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
54 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
55 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(comedi_default_buf_size_kb,
57 "default asynchronous buffer size in KiB (default "
58 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
60 unsigned int comedi_default_buf_maxsize_kb
61 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
62 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
63 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
64 "default maximum size of asynchronous buffer in KiB (default "
65 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
67 static DEFINE_MUTEX(comedi_board_minor_table_lock);
68 static struct comedi_device
69 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
71 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
72 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
73 static struct comedi_subdevice
74 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
76 static struct class *comedi_class;
77 static struct cdev comedi_cdev;
79 static void comedi_device_init(struct comedi_device *dev)
81 kref_init(&dev->refcount);
82 spin_lock_init(&dev->spinlock);
83 mutex_init(&dev->mutex);
84 init_rwsem(&dev->attach_lock);
88 static void comedi_dev_kref_release(struct kref *kref)
90 struct comedi_device *dev =
91 container_of(kref, struct comedi_device, refcount);
93 mutex_destroy(&dev->mutex);
94 put_device(dev->class_dev);
98 int comedi_dev_put(struct comedi_device *dev)
101 return kref_put(&dev->refcount, comedi_dev_kref_release);
104 EXPORT_SYMBOL_GPL(comedi_dev_put);
106 static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
109 kref_get(&dev->refcount);
113 static void comedi_device_cleanup(struct comedi_device *dev)
115 struct module *driver_module = NULL;
119 mutex_lock(&dev->mutex);
121 driver_module = dev->driver->module;
122 comedi_device_detach(dev);
123 if (driver_module && dev->use_count)
124 module_put(driver_module);
125 mutex_unlock(&dev->mutex);
128 static bool comedi_clear_board_dev(struct comedi_device *dev)
130 unsigned int i = dev->minor;
131 bool cleared = false;
133 mutex_lock(&comedi_board_minor_table_lock);
134 if (dev == comedi_board_minor_table[i]) {
135 comedi_board_minor_table[i] = NULL;
138 mutex_unlock(&comedi_board_minor_table_lock);
142 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
144 struct comedi_device *dev;
146 mutex_lock(&comedi_board_minor_table_lock);
147 dev = comedi_board_minor_table[minor];
148 comedi_board_minor_table[minor] = NULL;
149 mutex_unlock(&comedi_board_minor_table_lock);
153 static void comedi_free_board_dev(struct comedi_device *dev)
156 comedi_device_cleanup(dev);
157 if (dev->class_dev) {
158 device_destroy(comedi_class,
159 MKDEV(COMEDI_MAJOR, dev->minor));
165 static struct comedi_subdevice
166 *comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned minor)
168 struct comedi_subdevice *s;
169 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
171 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
172 mutex_lock(&comedi_subdevice_minor_table_lock);
173 s = comedi_subdevice_minor_table[i];
174 if (s && s->device != dev)
176 mutex_unlock(&comedi_subdevice_minor_table_lock);
180 static struct comedi_device *comedi_dev_get_from_board_minor(unsigned minor)
182 struct comedi_device *dev;
184 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
185 mutex_lock(&comedi_board_minor_table_lock);
186 dev = comedi_dev_get(comedi_board_minor_table[minor]);
187 mutex_unlock(&comedi_board_minor_table_lock);
191 static struct comedi_device *comedi_dev_get_from_subdevice_minor(unsigned minor)
193 struct comedi_device *dev;
194 struct comedi_subdevice *s;
195 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
197 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
198 mutex_lock(&comedi_subdevice_minor_table_lock);
199 s = comedi_subdevice_minor_table[i];
200 dev = comedi_dev_get(s ? s->device : NULL);
201 mutex_unlock(&comedi_subdevice_minor_table_lock);
205 struct comedi_device *comedi_dev_get_from_minor(unsigned minor)
207 if (minor < COMEDI_NUM_BOARD_MINORS)
208 return comedi_dev_get_from_board_minor(minor);
210 return comedi_dev_get_from_subdevice_minor(minor);
212 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
214 static struct comedi_subdevice *
215 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
217 struct comedi_subdevice *s;
219 if (minor >= COMEDI_NUM_BOARD_MINORS) {
220 s = comedi_subdevice_from_minor(dev, minor);
221 if (s == NULL || (s->subdev_flags & SDF_CMD_READ))
224 return dev->read_subdev;
227 static struct comedi_subdevice *
228 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
230 struct comedi_subdevice *s;
232 if (minor >= COMEDI_NUM_BOARD_MINORS) {
233 s = comedi_subdevice_from_minor(dev, minor);
234 if (s == NULL || (s->subdev_flags & SDF_CMD_WRITE))
237 return dev->write_subdev;
240 static int resize_async_buffer(struct comedi_device *dev,
241 struct comedi_subdevice *s,
242 struct comedi_async *async, unsigned new_size)
246 if (new_size > async->max_bufsize)
250 dev_dbg(dev->class_dev,
251 "subdevice is busy, cannot resize buffer\n");
254 if (comedi_buf_is_mmapped(async)) {
255 dev_dbg(dev->class_dev,
256 "subdevice is mmapped, cannot resize buffer\n");
260 /* make sure buffer is an integral number of pages
262 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
264 retval = comedi_buf_alloc(dev, s, new_size);
269 retval = s->buf_change(dev, s, new_size);
274 dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
275 s->index, async->prealloc_bufsz);
279 /* sysfs attribute files */
281 static ssize_t max_read_buffer_kb_show(struct device *csdev,
282 struct device_attribute *attr, char *buf)
284 unsigned int minor = MINOR(csdev->devt);
285 struct comedi_device *dev;
286 struct comedi_subdevice *s;
287 unsigned int size = 0;
289 dev = comedi_dev_get_from_minor(minor);
293 mutex_lock(&dev->mutex);
294 s = comedi_read_subdevice(dev, minor);
295 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
296 size = s->async->max_bufsize / 1024;
297 mutex_unlock(&dev->mutex);
300 return snprintf(buf, PAGE_SIZE, "%u\n", size);
303 static ssize_t max_read_buffer_kb_store(struct device *csdev,
304 struct device_attribute *attr,
305 const char *buf, size_t count)
307 unsigned int minor = MINOR(csdev->devt);
308 struct comedi_device *dev;
309 struct comedi_subdevice *s;
313 err = kstrtouint(buf, 10, &size);
316 if (size > (UINT_MAX / 1024))
320 dev = comedi_dev_get_from_minor(minor);
324 mutex_lock(&dev->mutex);
325 s = comedi_read_subdevice(dev, minor);
326 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
327 s->async->max_bufsize = size;
330 mutex_unlock(&dev->mutex);
333 return err ? err : count;
335 static DEVICE_ATTR_RW(max_read_buffer_kb);
337 static ssize_t read_buffer_kb_show(struct device *csdev,
338 struct device_attribute *attr, char *buf)
340 unsigned int minor = MINOR(csdev->devt);
341 struct comedi_device *dev;
342 struct comedi_subdevice *s;
343 unsigned int size = 0;
345 dev = comedi_dev_get_from_minor(minor);
349 mutex_lock(&dev->mutex);
350 s = comedi_read_subdevice(dev, minor);
351 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
352 size = s->async->prealloc_bufsz / 1024;
353 mutex_unlock(&dev->mutex);
356 return snprintf(buf, PAGE_SIZE, "%u\n", size);
359 static ssize_t read_buffer_kb_store(struct device *csdev,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
363 unsigned int minor = MINOR(csdev->devt);
364 struct comedi_device *dev;
365 struct comedi_subdevice *s;
369 err = kstrtouint(buf, 10, &size);
372 if (size > (UINT_MAX / 1024))
376 dev = comedi_dev_get_from_minor(minor);
380 mutex_lock(&dev->mutex);
381 s = comedi_read_subdevice(dev, minor);
382 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
383 err = resize_async_buffer(dev, s, s->async, size);
386 mutex_unlock(&dev->mutex);
389 return err ? err : count;
391 static DEVICE_ATTR_RW(read_buffer_kb);
393 static ssize_t max_write_buffer_kb_show(struct device *csdev,
394 struct device_attribute *attr,
397 unsigned int minor = MINOR(csdev->devt);
398 struct comedi_device *dev;
399 struct comedi_subdevice *s;
400 unsigned int size = 0;
402 dev = comedi_dev_get_from_minor(minor);
406 mutex_lock(&dev->mutex);
407 s = comedi_write_subdevice(dev, minor);
408 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
409 size = s->async->max_bufsize / 1024;
410 mutex_unlock(&dev->mutex);
413 return snprintf(buf, PAGE_SIZE, "%u\n", size);
416 static ssize_t max_write_buffer_kb_store(struct device *csdev,
417 struct device_attribute *attr,
418 const char *buf, size_t count)
420 unsigned int minor = MINOR(csdev->devt);
421 struct comedi_device *dev;
422 struct comedi_subdevice *s;
426 err = kstrtouint(buf, 10, &size);
429 if (size > (UINT_MAX / 1024))
433 dev = comedi_dev_get_from_minor(minor);
437 mutex_lock(&dev->mutex);
438 s = comedi_write_subdevice(dev, minor);
439 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
440 s->async->max_bufsize = size;
443 mutex_unlock(&dev->mutex);
446 return err ? err : count;
448 static DEVICE_ATTR_RW(max_write_buffer_kb);
450 static ssize_t write_buffer_kb_show(struct device *csdev,
451 struct device_attribute *attr, char *buf)
453 unsigned int minor = MINOR(csdev->devt);
454 struct comedi_device *dev;
455 struct comedi_subdevice *s;
456 unsigned int size = 0;
458 dev = comedi_dev_get_from_minor(minor);
462 mutex_lock(&dev->mutex);
463 s = comedi_write_subdevice(dev, minor);
464 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
465 size = s->async->prealloc_bufsz / 1024;
466 mutex_unlock(&dev->mutex);
469 return snprintf(buf, PAGE_SIZE, "%u\n", size);
472 static ssize_t write_buffer_kb_store(struct device *csdev,
473 struct device_attribute *attr,
474 const char *buf, size_t count)
476 unsigned int minor = MINOR(csdev->devt);
477 struct comedi_device *dev;
478 struct comedi_subdevice *s;
482 err = kstrtouint(buf, 10, &size);
485 if (size > (UINT_MAX / 1024))
489 dev = comedi_dev_get_from_minor(minor);
493 mutex_lock(&dev->mutex);
494 s = comedi_write_subdevice(dev, minor);
495 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
496 err = resize_async_buffer(dev, s, s->async, size);
499 mutex_unlock(&dev->mutex);
502 return err ? err : count;
504 static DEVICE_ATTR_RW(write_buffer_kb);
506 static struct attribute *comedi_dev_attrs[] = {
507 &dev_attr_max_read_buffer_kb.attr,
508 &dev_attr_read_buffer_kb.attr,
509 &dev_attr_max_write_buffer_kb.attr,
510 &dev_attr_write_buffer_kb.attr,
513 ATTRIBUTE_GROUPS(comedi_dev);
515 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
516 unsigned mask, unsigned bits)
520 spin_lock_irqsave(&s->spin_lock, flags);
521 s->runflags &= ~mask;
522 s->runflags |= (bits & mask);
523 spin_unlock_irqrestore(&s->spin_lock, flags);
526 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
531 spin_lock_irqsave(&s->spin_lock, flags);
532 runflags = s->runflags;
533 spin_unlock_irqrestore(&s->spin_lock, flags);
537 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
539 unsigned runflags = comedi_get_subdevice_runflags(s);
541 return (runflags & SRF_RUNNING) ? true : false;
543 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
545 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
547 unsigned runflags = comedi_get_subdevice_runflags(s);
549 return (runflags & SRF_ERROR) ? true : false;
552 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
554 unsigned runflags = comedi_get_subdevice_runflags(s);
556 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
560 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
561 * @s: comedi_subdevice struct
562 * @size: size of the memory to allocate
564 * This also sets the subdevice runflags to allow the core to automatically
565 * free the private data during the detach.
567 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
569 s->private = kzalloc(size, GFP_KERNEL);
571 s->runflags |= SRF_FREE_SPRIV;
574 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
577 This function restores a subdevice to an idle state.
579 static void do_become_nonbusy(struct comedi_device *dev,
580 struct comedi_subdevice *s)
582 struct comedi_async *async = s->async;
584 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
586 comedi_buf_reset(async);
587 async->inttrig = NULL;
588 kfree(async->cmd.chanlist);
589 async->cmd.chanlist = NULL;
591 wake_up_interruptible_all(&s->async->wait_head);
593 dev_err(dev->class_dev,
594 "BUG: (?) do_become_nonbusy called with async=NULL\n");
599 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
603 if (comedi_is_subdevice_running(s) && s->cancel)
604 ret = s->cancel(dev, s);
606 do_become_nonbusy(dev, s);
611 void comedi_device_cancel_all(struct comedi_device *dev)
613 struct comedi_subdevice *s;
619 for (i = 0; i < dev->n_subdevices; i++) {
620 s = &dev->subdevices[i];
626 static int is_device_busy(struct comedi_device *dev)
628 struct comedi_subdevice *s;
634 for (i = 0; i < dev->n_subdevices; i++) {
635 s = &dev->subdevices[i];
638 if (s->async && comedi_buf_is_mmapped(s->async))
650 pointer to devconfig structure
653 devconfig structure at arg
658 static int do_devconfig_ioctl(struct comedi_device *dev,
659 struct comedi_devconfig __user *arg)
661 struct comedi_devconfig it;
663 if (!capable(CAP_SYS_ADMIN))
667 if (is_device_busy(dev))
670 struct module *driver_module = dev->driver->module;
671 comedi_device_detach(dev);
672 module_put(driver_module);
677 if (copy_from_user(&it, arg, sizeof(it)))
680 it.board_name[COMEDI_NAMELEN - 1] = 0;
682 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
683 dev_warn(dev->class_dev,
684 "comedi_config --init_data is deprecated\n");
688 if (dev->minor >= comedi_num_legacy_minors)
689 /* don't re-use dynamically allocated comedi devices */
692 /* This increments the driver module count on success. */
693 return comedi_device_attach(dev, &it);
698 buffer configuration ioctl
701 pointer to bufconfig structure
707 modified bufconfig at arg
710 static int do_bufconfig_ioctl(struct comedi_device *dev,
711 struct comedi_bufconfig __user *arg)
713 struct comedi_bufconfig bc;
714 struct comedi_async *async;
715 struct comedi_subdevice *s;
718 if (copy_from_user(&bc, arg, sizeof(bc)))
721 if (bc.subdevice >= dev->n_subdevices)
724 s = &dev->subdevices[bc.subdevice];
728 dev_dbg(dev->class_dev,
729 "subdevice does not have async capability\n");
735 if (bc.maximum_size) {
736 if (!capable(CAP_SYS_ADMIN))
739 async->max_bufsize = bc.maximum_size;
743 retval = resize_async_buffer(dev, s, async, bc.size);
748 bc.size = async->prealloc_bufsz;
749 bc.maximum_size = async->max_bufsize;
752 if (copy_to_user(arg, &bc, sizeof(bc)))
763 pointer to devinfo structure
772 static int do_devinfo_ioctl(struct comedi_device *dev,
773 struct comedi_devinfo __user *arg,
776 const unsigned minor = iminor(file_inode(file));
777 struct comedi_subdevice *s;
778 struct comedi_devinfo devinfo;
780 memset(&devinfo, 0, sizeof(devinfo));
782 /* fill devinfo structure */
783 devinfo.version_code = COMEDI_VERSION_CODE;
784 devinfo.n_subdevs = dev->n_subdevices;
785 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
786 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
788 s = comedi_read_subdevice(dev, minor);
790 devinfo.read_subdevice = s->index;
792 devinfo.read_subdevice = -1;
794 s = comedi_write_subdevice(dev, minor);
796 devinfo.write_subdevice = s->index;
798 devinfo.write_subdevice = -1;
800 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
811 pointer to array of subdevice info structures
817 array of subdevice info structures at arg
820 static int do_subdinfo_ioctl(struct comedi_device *dev,
821 struct comedi_subdinfo __user *arg, void *file)
824 struct comedi_subdinfo *tmp, *us;
825 struct comedi_subdevice *s;
827 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
831 /* fill subdinfo structs */
832 for (i = 0; i < dev->n_subdevices; i++) {
833 s = &dev->subdevices[i];
837 us->n_chan = s->n_chan;
838 us->subd_flags = s->subdev_flags;
839 if (comedi_is_subdevice_running(s))
840 us->subd_flags |= SDF_RUNNING;
841 #define TIMER_nanosec 5 /* backwards compatibility */
842 us->timer_type = TIMER_nanosec;
843 us->len_chanlist = s->len_chanlist;
844 us->maxdata = s->maxdata;
845 if (s->range_table) {
847 (i << 24) | (0 << 16) | (s->range_table->length);
849 us->range_type = 0; /* XXX */
853 us->subd_flags |= SDF_BUSY;
855 us->subd_flags |= SDF_BUSY_OWNER;
857 us->subd_flags |= SDF_LOCKED;
859 us->subd_flags |= SDF_LOCK_OWNER;
860 if (!s->maxdata && s->maxdata_list)
861 us->subd_flags |= SDF_MAXDATA;
862 if (s->range_table_list)
863 us->subd_flags |= SDF_RANGETYPE;
865 us->subd_flags |= SDF_CMD;
867 if (s->insn_bits != &insn_inval)
868 us->insn_bits_support = COMEDI_SUPPORTED;
870 us->insn_bits_support = COMEDI_UNSUPPORTED;
873 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
877 return ret ? -EFAULT : 0;
885 pointer to chaninfo structure
888 chaninfo structure at arg
891 arrays at elements of chaninfo structure
894 static int do_chaninfo_ioctl(struct comedi_device *dev,
895 struct comedi_chaninfo __user *arg)
897 struct comedi_subdevice *s;
898 struct comedi_chaninfo it;
900 if (copy_from_user(&it, arg, sizeof(it)))
903 if (it.subdev >= dev->n_subdevices)
905 s = &dev->subdevices[it.subdev];
907 if (it.maxdata_list) {
908 if (s->maxdata || !s->maxdata_list)
910 if (copy_to_user(it.maxdata_list, s->maxdata_list,
911 s->n_chan * sizeof(unsigned int)))
916 return -EINVAL; /* flaglist not supported */
921 if (!s->range_table_list)
923 for (i = 0; i < s->n_chan; i++) {
926 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
927 (s->range_table_list[i]->length);
928 if (put_user(x, it.rangelist + i))
932 if (copy_to_user(it.rangelist, s->range_type_list,
933 s->n_chan * sizeof(unsigned int)))
943 buffer information ioctl
946 pointer to bufinfo structure
952 modified bufinfo at arg
955 static int do_bufinfo_ioctl(struct comedi_device *dev,
956 struct comedi_bufinfo __user *arg, void *file)
958 struct comedi_bufinfo bi;
959 struct comedi_subdevice *s;
960 struct comedi_async *async;
962 if (copy_from_user(&bi, arg, sizeof(bi)))
965 if (bi.subdevice >= dev->n_subdevices)
968 s = &dev->subdevices[bi.subdevice];
970 if (s->lock && s->lock != file)
976 dev_dbg(dev->class_dev,
977 "subdevice does not have async capability\n");
978 bi.buf_write_ptr = 0;
980 bi.buf_write_count = 0;
981 bi.buf_read_count = 0;
983 bi.bytes_written = 0;
988 bi.bytes_written = 0;
989 goto copyback_position;
994 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
995 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
996 comedi_buf_read_free(async, bi.bytes_read);
998 if (comedi_is_subdevice_idle(s) &&
999 async->buf_write_count == async->buf_read_count) {
1000 do_become_nonbusy(dev, s);
1004 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
1006 comedi_buf_write_alloc(async, bi.bytes_written);
1007 comedi_buf_write_free(async, bi.bytes_written);
1011 bi.buf_write_count = async->buf_write_count;
1012 bi.buf_write_ptr = async->buf_write_ptr;
1013 bi.buf_read_count = async->buf_read_count;
1014 bi.buf_read_ptr = async->buf_read_ptr;
1017 if (copy_to_user(arg, &bi, sizeof(bi)))
1023 static int check_insn_config_length(struct comedi_insn *insn,
1030 case INSN_CONFIG_DIO_OUTPUT:
1031 case INSN_CONFIG_DIO_INPUT:
1032 case INSN_CONFIG_DISARM:
1033 case INSN_CONFIG_RESET:
1037 case INSN_CONFIG_ARM:
1038 case INSN_CONFIG_DIO_QUERY:
1039 case INSN_CONFIG_BLOCK_SIZE:
1040 case INSN_CONFIG_FILTER:
1041 case INSN_CONFIG_SERIAL_CLOCK:
1042 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1043 case INSN_CONFIG_ALT_SOURCE:
1044 case INSN_CONFIG_SET_COUNTER_MODE:
1045 case INSN_CONFIG_8254_READ_STATUS:
1046 case INSN_CONFIG_SET_ROUTING:
1047 case INSN_CONFIG_GET_ROUTING:
1048 case INSN_CONFIG_GET_PWM_STATUS:
1049 case INSN_CONFIG_PWM_SET_PERIOD:
1050 case INSN_CONFIG_PWM_GET_PERIOD:
1054 case INSN_CONFIG_SET_GATE_SRC:
1055 case INSN_CONFIG_GET_GATE_SRC:
1056 case INSN_CONFIG_SET_CLOCK_SRC:
1057 case INSN_CONFIG_GET_CLOCK_SRC:
1058 case INSN_CONFIG_SET_OTHER_SRC:
1059 case INSN_CONFIG_GET_COUNTER_STATUS:
1060 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1061 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1062 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1066 case INSN_CONFIG_PWM_OUTPUT:
1067 case INSN_CONFIG_ANALOG_TRIG:
1071 case INSN_CONFIG_DIGITAL_TRIG:
1075 /* by default we allow the insn since we don't have checks for
1076 * all possible cases yet */
1078 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1080 pr_warn("comedi: Add a check to %s in %s.\n",
1081 __func__, __FILE__);
1082 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1088 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1089 unsigned int *data, void *file)
1091 struct comedi_subdevice *s;
1095 if (insn->insn & INSN_MASK_SPECIAL) {
1096 /* a non-subdevice instruction */
1098 switch (insn->insn) {
1108 do_gettimeofday(&tv);
1109 data[0] = tv.tv_sec;
1110 data[1] = tv.tv_usec;
1116 if (insn->n != 1 || data[0] >= 100000) {
1120 udelay(data[0] / 1000);
1128 if (insn->subdev >= dev->n_subdevices) {
1129 dev_dbg(dev->class_dev,
1130 "%d not usable subdevice\n",
1135 s = &dev->subdevices[insn->subdev];
1137 dev_dbg(dev->class_dev, "no async\n");
1141 if (!s->async->inttrig) {
1142 dev_dbg(dev->class_dev, "no inttrig\n");
1146 ret = s->async->inttrig(dev, s, data[0]);
1151 dev_dbg(dev->class_dev, "invalid insn\n");
1156 /* a subdevice instruction */
1157 unsigned int maxdata;
1159 if (insn->subdev >= dev->n_subdevices) {
1160 dev_dbg(dev->class_dev, "subdevice %d out of range\n",
1165 s = &dev->subdevices[insn->subdev];
1167 if (s->type == COMEDI_SUBD_UNUSED) {
1168 dev_dbg(dev->class_dev, "%d not usable subdevice\n",
1174 /* are we locked? (ioctl lock) */
1175 if (s->lock && s->lock != file) {
1176 dev_dbg(dev->class_dev, "device locked\n");
1181 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1184 dev_dbg(dev->class_dev, "bad chanspec\n");
1192 /* This looks arbitrary. It is. */
1193 s->busy = &parse_insn;
1194 switch (insn->insn) {
1196 ret = s->insn_read(dev, s, insn, data);
1197 if (ret == -ETIMEDOUT) {
1198 dev_dbg(dev->class_dev,
1199 "subdevice %d read instruction timed out\n",
1204 maxdata = s->maxdata_list
1205 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1207 for (i = 0; i < insn->n; ++i) {
1208 if (data[i] > maxdata) {
1210 dev_dbg(dev->class_dev,
1211 "bad data value(s)\n");
1216 ret = s->insn_write(dev, s, insn, data);
1217 if (ret == -ETIMEDOUT) {
1218 dev_dbg(dev->class_dev,
1219 "subdevice %d write instruction timed out\n",
1228 /* Most drivers ignore the base channel in
1229 * insn->chanspec. Fix this here if
1230 * the subdevice has <= 32 channels. */
1232 unsigned int orig_mask;
1234 orig_mask = data[0];
1235 if (s->n_chan <= 32) {
1236 shift = CR_CHAN(insn->chanspec);
1244 ret = s->insn_bits(dev, s, insn, data);
1245 data[0] = orig_mask;
1251 ret = check_insn_config_length(insn, data);
1254 ret = s->insn_config(dev, s, insn, data);
1270 * synchronous instructions
1273 * pointer to sync cmd structure
1276 * sync cmd struct at arg
1283 /* arbitrary limits */
1284 #define MAX_SAMPLES 256
1285 static int do_insnlist_ioctl(struct comedi_device *dev,
1286 struct comedi_insnlist __user *arg, void *file)
1288 struct comedi_insnlist insnlist;
1289 struct comedi_insn *insns = NULL;
1290 unsigned int *data = NULL;
1294 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1297 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1303 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1309 if (copy_from_user(insns, insnlist.insns,
1310 sizeof(*insns) * insnlist.n_insns)) {
1311 dev_dbg(dev->class_dev, "copy_from_user failed\n");
1316 for (i = 0; i < insnlist.n_insns; i++) {
1317 if (insns[i].n > MAX_SAMPLES) {
1318 dev_dbg(dev->class_dev,
1319 "number of samples too large\n");
1323 if (insns[i].insn & INSN_MASK_WRITE) {
1324 if (copy_from_user(data, insns[i].data,
1325 insns[i].n * sizeof(unsigned int))) {
1326 dev_dbg(dev->class_dev,
1327 "copy_from_user failed\n");
1332 ret = parse_insn(dev, insns + i, data, file);
1335 if (insns[i].insn & INSN_MASK_READ) {
1336 if (copy_to_user(insns[i].data, data,
1337 insns[i].n * sizeof(unsigned int))) {
1338 dev_dbg(dev->class_dev,
1339 "copy_to_user failed\n");
1359 * synchronous instructions
1365 * struct comedi_insn struct at arg
1371 static int do_insn_ioctl(struct comedi_device *dev,
1372 struct comedi_insn __user *arg, void *file)
1374 struct comedi_insn insn;
1375 unsigned int *data = NULL;
1378 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1384 if (copy_from_user(&insn, arg, sizeof(insn))) {
1389 /* This is where the behavior of insn and insnlist deviate. */
1390 if (insn.n > MAX_SAMPLES)
1391 insn.n = MAX_SAMPLES;
1392 if (insn.insn & INSN_MASK_WRITE) {
1393 if (copy_from_user(data,
1395 insn.n * sizeof(unsigned int))) {
1400 ret = parse_insn(dev, &insn, data, file);
1403 if (insn.insn & INSN_MASK_READ) {
1404 if (copy_to_user(insn.data,
1406 insn.n * sizeof(unsigned int))) {
1419 static int __comedi_get_user_cmd(struct comedi_device *dev,
1420 struct comedi_cmd __user *arg,
1421 struct comedi_cmd *cmd)
1423 struct comedi_subdevice *s;
1425 if (copy_from_user(cmd, arg, sizeof(*cmd))) {
1426 dev_dbg(dev->class_dev, "bad cmd address\n");
1430 if (cmd->subdev >= dev->n_subdevices) {
1431 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
1435 s = &dev->subdevices[cmd->subdev];
1437 if (s->type == COMEDI_SUBD_UNUSED) {
1438 dev_dbg(dev->class_dev, "%d not valid subdevice\n",
1443 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1444 dev_dbg(dev->class_dev,
1445 "subdevice %d does not support commands\n",
1450 /* make sure channel/gain list isn't too long */
1451 if (cmd->chanlist_len > s->len_chanlist) {
1452 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
1453 cmd->chanlist_len, s->len_chanlist);
1460 static int __comedi_get_user_chanlist(struct comedi_device *dev,
1461 struct comedi_subdevice *s,
1462 unsigned int __user *user_chanlist,
1463 struct comedi_cmd *cmd)
1465 unsigned int *chanlist;
1468 /* user_chanlist could be NULL for do_cmdtest ioctls */
1472 chanlist = memdup_user(user_chanlist,
1473 cmd->chanlist_len * sizeof(unsigned int));
1474 if (IS_ERR(chanlist))
1475 return PTR_ERR(chanlist);
1477 /* make sure each element in channel/gain list is valid */
1478 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
1484 cmd->chanlist = chanlist;
1489 static int do_cmd_ioctl(struct comedi_device *dev,
1490 struct comedi_cmd __user *arg, void *file)
1492 struct comedi_cmd cmd;
1493 struct comedi_subdevice *s;
1494 struct comedi_async *async;
1495 unsigned int __user *user_chanlist;
1498 /* get the user's cmd and do some simple validation */
1499 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1503 /* save user's chanlist pointer so it can be restored later */
1504 user_chanlist = (unsigned int __user *)cmd.chanlist;
1506 s = &dev->subdevices[cmd.subdev];
1509 /* are we locked? (ioctl lock) */
1510 if (s->lock && s->lock != file) {
1511 dev_dbg(dev->class_dev, "subdevice locked\n");
1517 dev_dbg(dev->class_dev, "subdevice busy\n");
1521 /* make sure channel/gain list isn't too short */
1522 if (cmd.chanlist_len < 1) {
1523 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
1529 async->cmd.data = NULL;
1531 /* load channel/gain list */
1532 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
1536 ret = s->do_cmdtest(dev, s, &async->cmd);
1538 if (async->cmd.flags & TRIG_BOGUS || ret) {
1539 dev_dbg(dev->class_dev, "test returned %d\n", ret);
1541 /* restore chanlist pointer before copying back */
1542 cmd.chanlist = (unsigned int __force *)user_chanlist;
1544 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1545 dev_dbg(dev->class_dev, "fault writing cmd\n");
1553 if (!async->prealloc_bufsz) {
1555 dev_dbg(dev->class_dev, "no buffer (?)\n");
1559 comedi_buf_reset(async);
1562 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1564 if (async->cmd.flags & TRIG_WAKE_EOS)
1565 async->cb_mask |= COMEDI_CB_EOS;
1567 comedi_set_subdevice_runflags(s, SRF_ERROR | SRF_RUNNING, SRF_RUNNING);
1569 /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
1570 * comedi_read() or comedi_write() */
1572 ret = s->do_cmd(dev, s);
1577 do_become_nonbusy(dev, s);
1584 command testing ioctl
1587 pointer to cmd structure
1590 cmd structure at arg
1594 modified cmd structure at arg
1597 static int do_cmdtest_ioctl(struct comedi_device *dev,
1598 struct comedi_cmd __user *arg, void *file)
1600 struct comedi_cmd cmd;
1601 struct comedi_subdevice *s;
1602 unsigned int __user *user_chanlist;
1605 /* get the user's cmd and do some simple validation */
1606 ret = __comedi_get_user_cmd(dev, arg, &cmd);
1610 /* save user's chanlist pointer so it can be restored later */
1611 user_chanlist = (unsigned int __user *)cmd.chanlist;
1613 s = &dev->subdevices[cmd.subdev];
1615 /* load channel/gain list */
1616 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
1620 ret = s->do_cmdtest(dev, s, &cmd);
1622 /* restore chanlist pointer before copying back */
1623 cmd.chanlist = (unsigned int __force *)user_chanlist;
1625 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1626 dev_dbg(dev->class_dev, "bad cmd address\n");
1648 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1652 unsigned long flags;
1653 struct comedi_subdevice *s;
1655 if (arg >= dev->n_subdevices)
1657 s = &dev->subdevices[arg];
1659 spin_lock_irqsave(&s->spin_lock, flags);
1660 if (s->busy || s->lock)
1664 spin_unlock_irqrestore(&s->spin_lock, flags);
1671 ret = s->lock_f(dev, s);
1690 This function isn't protected by the semaphore, since
1691 we already own the lock.
1693 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1696 struct comedi_subdevice *s;
1698 if (arg >= dev->n_subdevices)
1700 s = &dev->subdevices[arg];
1705 if (s->lock && s->lock != file)
1708 if (s->lock == file) {
1722 cancel acquisition ioctl
1734 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1737 struct comedi_subdevice *s;
1740 if (arg >= dev->n_subdevices)
1742 s = &dev->subdevices[arg];
1743 if (s->async == NULL)
1746 if (s->lock && s->lock != file)
1752 if (s->busy != file)
1755 ret = do_cancel(dev, s);
1762 instructs driver to synchronize buffers
1774 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1777 struct comedi_subdevice *s;
1779 if (arg >= dev->n_subdevices)
1781 s = &dev->subdevices[arg];
1783 if (s->lock && s->lock != file)
1789 if (s->busy != file)
1793 return s->poll(dev, s);
1798 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1801 const unsigned minor = iminor(file_inode(file));
1802 struct comedi_device *dev = file->private_data;
1805 mutex_lock(&dev->mutex);
1807 /* Device config is special, because it must work on
1808 * an unconfigured device. */
1809 if (cmd == COMEDI_DEVCONFIG) {
1810 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1811 /* Device config not appropriate on non-board minors. */
1815 rc = do_devconfig_ioctl(dev,
1816 (struct comedi_devconfig __user *)arg);
1819 dev->minor >= comedi_num_legacy_minors) {
1820 /* Successfully unconfigured a dynamically
1821 * allocated device. Try and remove it. */
1822 if (comedi_clear_board_dev(dev)) {
1823 mutex_unlock(&dev->mutex);
1824 comedi_free_board_dev(dev);
1832 if (!dev->attached) {
1833 dev_dbg(dev->class_dev, "no driver attached\n");
1839 case COMEDI_BUFCONFIG:
1840 rc = do_bufconfig_ioctl(dev,
1841 (struct comedi_bufconfig __user *)arg);
1843 case COMEDI_DEVINFO:
1844 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1847 case COMEDI_SUBDINFO:
1848 rc = do_subdinfo_ioctl(dev,
1849 (struct comedi_subdinfo __user *)arg,
1852 case COMEDI_CHANINFO:
1853 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1855 case COMEDI_RANGEINFO:
1856 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1858 case COMEDI_BUFINFO:
1859 rc = do_bufinfo_ioctl(dev,
1860 (struct comedi_bufinfo __user *)arg,
1864 rc = do_lock_ioctl(dev, arg, file);
1867 rc = do_unlock_ioctl(dev, arg, file);
1870 rc = do_cancel_ioctl(dev, arg, file);
1873 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1875 case COMEDI_CMDTEST:
1876 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1879 case COMEDI_INSNLIST:
1880 rc = do_insnlist_ioctl(dev,
1881 (struct comedi_insnlist __user *)arg,
1885 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1889 rc = do_poll_ioctl(dev, arg, file);
1897 mutex_unlock(&dev->mutex);
1901 static void comedi_vm_open(struct vm_area_struct *area)
1903 struct comedi_buf_map *bm;
1905 bm = area->vm_private_data;
1906 comedi_buf_map_get(bm);
1909 static void comedi_vm_close(struct vm_area_struct *area)
1911 struct comedi_buf_map *bm;
1913 bm = area->vm_private_data;
1914 comedi_buf_map_put(bm);
1917 static struct vm_operations_struct comedi_vm_ops = {
1918 .open = comedi_vm_open,
1919 .close = comedi_vm_close,
1922 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1924 const unsigned minor = iminor(file_inode(file));
1925 struct comedi_device *dev = file->private_data;
1926 struct comedi_subdevice *s;
1927 struct comedi_async *async;
1928 struct comedi_buf_map *bm = NULL;
1929 unsigned long start = vma->vm_start;
1936 * 'trylock' avoids circular dependency with current->mm->mmap_sem
1937 * and down-reading &dev->attach_lock should normally succeed without
1938 * contention unless the device is in the process of being attached
1941 if (!down_read_trylock(&dev->attach_lock))
1944 if (!dev->attached) {
1945 dev_dbg(dev->class_dev, "no driver attached\n");
1950 if (vma->vm_flags & VM_WRITE)
1951 s = comedi_write_subdevice(dev, minor);
1953 s = comedi_read_subdevice(dev, minor);
1965 if (vma->vm_pgoff != 0) {
1966 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
1971 size = vma->vm_end - vma->vm_start;
1972 if (size > async->prealloc_bufsz) {
1976 if (size & (~PAGE_MASK)) {
1981 n_pages = size >> PAGE_SHIFT;
1983 /* get reference to current buf map (if any) */
1984 bm = comedi_buf_map_from_subdev_get(s);
1985 if (!bm || n_pages > bm->n_pages) {
1989 for (i = 0; i < n_pages; ++i) {
1990 struct comedi_buf_page *buf = &bm->page_list[i];
1992 if (remap_pfn_range(vma, start,
1993 page_to_pfn(virt_to_page(buf->virt_addr)),
1994 PAGE_SIZE, PAGE_SHARED)) {
2001 vma->vm_ops = &comedi_vm_ops;
2002 vma->vm_private_data = bm;
2004 vma->vm_ops->open(vma);
2008 up_read(&dev->attach_lock);
2009 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
2013 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2015 unsigned int mask = 0;
2016 const unsigned minor = iminor(file_inode(file));
2017 struct comedi_device *dev = file->private_data;
2018 struct comedi_subdevice *s;
2020 mutex_lock(&dev->mutex);
2022 if (!dev->attached) {
2023 dev_dbg(dev->class_dev, "no driver attached\n");
2027 s = comedi_read_subdevice(dev, minor);
2028 if (s && s->async) {
2029 poll_wait(file, &s->async->wait_head, wait);
2030 if (!s->busy || !comedi_is_subdevice_running(s) ||
2031 comedi_buf_read_n_available(s->async) > 0)
2032 mask |= POLLIN | POLLRDNORM;
2035 s = comedi_write_subdevice(dev, minor);
2036 if (s && s->async) {
2037 unsigned int bps = bytes_per_sample(s->async->subdevice);
2039 poll_wait(file, &s->async->wait_head, wait);
2040 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2041 if (!s->busy || !comedi_is_subdevice_running(s) ||
2042 comedi_buf_write_n_allocated(s->async) >= bps)
2043 mask |= POLLOUT | POLLWRNORM;
2047 mutex_unlock(&dev->mutex);
2051 static ssize_t comedi_write(struct file *file, const char __user *buf,
2052 size_t nbytes, loff_t *offset)
2054 struct comedi_subdevice *s;
2055 struct comedi_async *async;
2056 int n, m, count = 0, retval = 0;
2057 DECLARE_WAITQUEUE(wait, current);
2058 const unsigned minor = iminor(file_inode(file));
2059 struct comedi_device *dev = file->private_data;
2060 bool on_wait_queue = false;
2062 unsigned int old_detach_count;
2064 /* Protect against device detachment during operation. */
2065 down_read(&dev->attach_lock);
2066 attach_locked = true;
2067 old_detach_count = dev->detach_count;
2069 if (!dev->attached) {
2070 dev_dbg(dev->class_dev, "no driver attached\n");
2075 s = comedi_write_subdevice(dev, minor);
2076 if (!s || !s->async) {
2083 if (!s->busy || !nbytes)
2085 if (s->busy != file) {
2090 add_wait_queue(&async->wait_head, &wait);
2091 on_wait_queue = true;
2092 while (nbytes > 0 && !retval) {
2093 set_current_state(TASK_INTERRUPTIBLE);
2095 if (!comedi_is_subdevice_running(s)) {
2097 struct comedi_subdevice *new_s;
2099 if (comedi_is_subdevice_in_error(s))
2104 * To avoid deadlock, cannot acquire dev->mutex
2105 * while dev->attach_lock is held. Need to
2106 * remove task from the async wait queue before
2107 * releasing dev->attach_lock, as it might not
2108 * be valid afterwards.
2110 remove_wait_queue(&async->wait_head, &wait);
2111 on_wait_queue = false;
2112 up_read(&dev->attach_lock);
2113 attach_locked = false;
2114 mutex_lock(&dev->mutex);
2116 * Become non-busy unless things have changed
2117 * behind our back. Checking dev->detach_count
2118 * is unchanged ought to be sufficient (unless
2119 * there have been 2**32 detaches in the
2120 * meantime!), but check the subdevice pointer
2121 * as well just in case.
2123 new_s = comedi_write_subdevice(dev, minor);
2124 if (dev->attached &&
2125 old_detach_count == dev->detach_count &&
2126 s == new_s && new_s->async == async)
2127 do_become_nonbusy(dev, s);
2128 mutex_unlock(&dev->mutex);
2136 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2137 m = async->prealloc_bufsz - async->buf_write_ptr;
2138 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2139 if (m > comedi_buf_write_n_allocated(async))
2140 m = comedi_buf_write_n_allocated(async);
2145 if (file->f_flags & O_NONBLOCK) {
2150 if (signal_pending(current)) {
2151 retval = -ERESTARTSYS;
2156 if (s->busy != file) {
2163 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2169 comedi_buf_write_free(async, n);
2175 break; /* makes device work like a pipe */
2179 remove_wait_queue(&async->wait_head, &wait);
2180 set_current_state(TASK_RUNNING);
2182 up_read(&dev->attach_lock);
2184 return count ? count : retval;
2187 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2190 struct comedi_subdevice *s;
2191 struct comedi_async *async;
2192 int n, m, count = 0, retval = 0;
2193 DECLARE_WAITQUEUE(wait, current);
2194 const unsigned minor = iminor(file_inode(file));
2195 struct comedi_device *dev = file->private_data;
2196 unsigned int old_detach_count;
2197 bool become_nonbusy = false;
2200 /* Protect against device detachment during operation. */
2201 down_read(&dev->attach_lock);
2202 attach_locked = true;
2203 old_detach_count = dev->detach_count;
2205 if (!dev->attached) {
2206 dev_dbg(dev->class_dev, "no driver attached\n");
2211 s = comedi_read_subdevice(dev, minor);
2212 if (!s || !s->async) {
2218 if (!s->busy || !nbytes)
2220 if (s->busy != file) {
2225 add_wait_queue(&async->wait_head, &wait);
2226 while (nbytes > 0 && !retval) {
2227 set_current_state(TASK_INTERRUPTIBLE);
2231 m = comedi_buf_read_n_available(async);
2232 /* printk("%d available\n",m); */
2233 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2234 m = async->prealloc_bufsz - async->buf_read_ptr;
2235 /* printk("%d contiguous\n",m); */
2240 if (!comedi_is_subdevice_running(s)) {
2241 if (comedi_is_subdevice_in_error(s))
2245 become_nonbusy = true;
2248 if (file->f_flags & O_NONBLOCK) {
2253 if (signal_pending(current)) {
2254 retval = -ERESTARTSYS;
2261 if (s->busy != file) {
2267 m = copy_to_user(buf, async->prealloc_buf +
2268 async->buf_read_ptr, n);
2274 comedi_buf_read_alloc(async, n);
2275 comedi_buf_read_free(async, n);
2281 break; /* makes device work like a pipe */
2283 remove_wait_queue(&async->wait_head, &wait);
2284 set_current_state(TASK_RUNNING);
2285 if (become_nonbusy || comedi_is_subdevice_idle(s)) {
2286 struct comedi_subdevice *new_s;
2289 * To avoid deadlock, cannot acquire dev->mutex
2290 * while dev->attach_lock is held.
2292 up_read(&dev->attach_lock);
2293 attach_locked = false;
2294 mutex_lock(&dev->mutex);
2296 * Check device hasn't become detached behind our back.
2297 * Checking dev->detach_count is unchanged ought to be
2298 * sufficient (unless there have been 2**32 detaches in the
2299 * meantime!), but check the subdevice pointer as well just in
2302 new_s = comedi_read_subdevice(dev, minor);
2303 if (dev->attached && old_detach_count == dev->detach_count &&
2304 s == new_s && new_s->async == async) {
2305 if (become_nonbusy ||
2306 async->buf_read_count - async->buf_write_count == 0)
2307 do_become_nonbusy(dev, s);
2309 mutex_unlock(&dev->mutex);
2313 up_read(&dev->attach_lock);
2315 return count ? count : retval;
2318 static int comedi_open(struct inode *inode, struct file *file)
2320 const unsigned minor = iminor(inode);
2321 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2325 pr_debug("invalid minor number\n");
2329 /* This is slightly hacky, but we want module autoloading
2331 * case: user opens device, attached -> ok
2332 * case: user opens device, unattached, !in_request_module -> autoload
2333 * case: user opens device, unattached, in_request_module -> fail
2334 * case: root opens device, attached -> ok
2335 * case: root opens device, unattached, in_request_module -> ok
2336 * (typically called from modprobe)
2337 * case: root opens device, unattached, !in_request_module -> autoload
2339 * The last could be changed to "-> ok", which would deny root
2342 mutex_lock(&dev->mutex);
2345 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2346 dev_dbg(dev->class_dev, "in request module\n");
2350 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2353 dev->in_request_module = true;
2356 mutex_unlock(&dev->mutex);
2357 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2358 mutex_lock(&dev->mutex);
2361 dev->in_request_module = false;
2363 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2364 dev_dbg(dev->class_dev, "not attached and not CAP_NET_ADMIN\n");
2369 if (dev->attached && dev->use_count == 0) {
2370 if (!try_module_get(dev->driver->module)) {
2375 rc = dev->open(dev);
2377 module_put(dev->driver->module);
2384 file->private_data = dev;
2388 mutex_unlock(&dev->mutex);
2390 comedi_dev_put(dev);
2394 static int comedi_fasync(int fd, struct file *file, int on)
2396 struct comedi_device *dev = file->private_data;
2398 return fasync_helper(fd, file, on, &dev->async_queue);
2401 static int comedi_close(struct inode *inode, struct file *file)
2403 struct comedi_device *dev = file->private_data;
2404 struct comedi_subdevice *s = NULL;
2407 mutex_lock(&dev->mutex);
2409 if (dev->subdevices) {
2410 for (i = 0; i < dev->n_subdevices; i++) {
2411 s = &dev->subdevices[i];
2413 if (s->busy == file)
2415 if (s->lock == file)
2419 if (dev->attached && dev->use_count == 1) {
2422 module_put(dev->driver->module);
2427 mutex_unlock(&dev->mutex);
2428 comedi_dev_put(dev);
2433 static const struct file_operations comedi_fops = {
2434 .owner = THIS_MODULE,
2435 .unlocked_ioctl = comedi_unlocked_ioctl,
2436 .compat_ioctl = comedi_compat_ioctl,
2437 .open = comedi_open,
2438 .release = comedi_close,
2439 .read = comedi_read,
2440 .write = comedi_write,
2441 .mmap = comedi_mmap,
2442 .poll = comedi_poll,
2443 .fasync = comedi_fasync,
2444 .llseek = noop_llseek,
2447 void comedi_error(const struct comedi_device *dev, const char *s)
2449 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2451 EXPORT_SYMBOL_GPL(comedi_error);
2453 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2455 struct comedi_async *async = s->async;
2456 unsigned runflags = 0;
2457 unsigned runflags_mask = 0;
2459 if (!comedi_is_subdevice_running(s))
2463 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2464 COMEDI_CB_OVERFLOW)) {
2465 runflags_mask |= SRF_RUNNING;
2467 /* remember if an error event has occurred, so an error
2468 * can be returned the next time the user does a read() */
2469 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2470 runflags_mask |= SRF_ERROR;
2471 runflags |= SRF_ERROR;
2473 if (runflags_mask) {
2474 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2475 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2478 if (async->cb_mask & s->async->events) {
2479 wake_up_interruptible(&async->wait_head);
2480 if (s->subdev_flags & SDF_CMD_READ)
2481 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2482 if (s->subdev_flags & SDF_CMD_WRITE)
2483 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2485 s->async->events = 0;
2487 EXPORT_SYMBOL_GPL(comedi_event);
2489 /* Note: the ->mutex is pre-locked on successful return */
2490 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2492 struct comedi_device *dev;
2493 struct device *csdev;
2496 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2498 return ERR_PTR(-ENOMEM);
2499 comedi_device_init(dev);
2500 comedi_set_hw_dev(dev, hardware_device);
2501 mutex_lock(&dev->mutex);
2502 mutex_lock(&comedi_board_minor_table_lock);
2503 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2504 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2505 if (comedi_board_minor_table[i] == NULL) {
2506 comedi_board_minor_table[i] = dev;
2510 mutex_unlock(&comedi_board_minor_table_lock);
2511 if (i == COMEDI_NUM_BOARD_MINORS) {
2512 mutex_unlock(&dev->mutex);
2513 comedi_device_cleanup(dev);
2514 comedi_dev_put(dev);
2515 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2516 return ERR_PTR(-EBUSY);
2519 csdev = device_create(comedi_class, hardware_device,
2520 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2522 dev->class_dev = get_device(csdev);
2524 /* Note: dev->mutex needs to be unlocked by the caller. */
2528 static void comedi_free_board_minor(unsigned minor)
2530 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2531 comedi_free_board_dev(comedi_clear_board_minor(minor));
2534 void comedi_release_hardware_device(struct device *hardware_device)
2537 struct comedi_device *dev;
2539 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2541 mutex_lock(&comedi_board_minor_table_lock);
2542 dev = comedi_board_minor_table[minor];
2543 if (dev && dev->hw_dev == hardware_device) {
2544 comedi_board_minor_table[minor] = NULL;
2545 mutex_unlock(&comedi_board_minor_table_lock);
2546 comedi_free_board_dev(dev);
2549 mutex_unlock(&comedi_board_minor_table_lock);
2553 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2555 struct comedi_device *dev = s->device;
2556 struct device *csdev;
2559 mutex_lock(&comedi_subdevice_minor_table_lock);
2560 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2561 if (comedi_subdevice_minor_table[i] == NULL) {
2562 comedi_subdevice_minor_table[i] = s;
2566 mutex_unlock(&comedi_subdevice_minor_table_lock);
2567 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2568 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2571 i += COMEDI_NUM_BOARD_MINORS;
2573 csdev = device_create(comedi_class, dev->class_dev,
2574 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2575 dev->minor, s->index);
2577 s->class_dev = csdev;
2582 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2591 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2592 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2594 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2595 mutex_lock(&comedi_subdevice_minor_table_lock);
2596 if (s == comedi_subdevice_minor_table[i])
2597 comedi_subdevice_minor_table[i] = NULL;
2598 mutex_unlock(&comedi_subdevice_minor_table_lock);
2600 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2601 s->class_dev = NULL;
2605 static void comedi_cleanup_board_minors(void)
2609 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2610 comedi_free_board_minor(i);
2613 static int __init comedi_init(void)
2618 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2620 if (comedi_num_legacy_minors < 0 ||
2621 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2622 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2623 COMEDI_NUM_BOARD_MINORS);
2627 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2628 COMEDI_NUM_MINORS, "comedi");
2631 cdev_init(&comedi_cdev, &comedi_fops);
2632 comedi_cdev.owner = THIS_MODULE;
2633 kobject_set_name(&comedi_cdev.kobj, "comedi");
2634 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2635 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2639 comedi_class = class_create(THIS_MODULE, "comedi");
2640 if (IS_ERR(comedi_class)) {
2641 pr_err("comedi: failed to create class\n");
2642 cdev_del(&comedi_cdev);
2643 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2645 return PTR_ERR(comedi_class);
2648 comedi_class->dev_groups = comedi_dev_groups;
2650 /* XXX requires /proc interface */
2653 /* create devices files for legacy/manual use */
2654 for (i = 0; i < comedi_num_legacy_minors; i++) {
2655 struct comedi_device *dev;
2656 dev = comedi_alloc_board_minor(NULL);
2658 comedi_cleanup_board_minors();
2659 cdev_del(&comedi_cdev);
2660 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2662 return PTR_ERR(dev);
2664 /* comedi_alloc_board_minor() locked the mutex */
2665 mutex_unlock(&dev->mutex);
2671 module_init(comedi_init);
2673 static void __exit comedi_cleanup(void)
2677 comedi_cleanup_board_minors();
2678 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2679 BUG_ON(comedi_board_minor_table[i]);
2680 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2681 BUG_ON(comedi_subdevice_minor_table[i]);
2683 class_destroy(comedi_class);
2684 cdev_del(&comedi_cdev);
2685 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2687 comedi_proc_cleanup();
2689 module_exit(comedi_cleanup);
2691 MODULE_AUTHOR("http://www.comedi.org");
2692 MODULE_DESCRIPTION("Comedi core module");
2693 MODULE_LICENSE("GPL");