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.
21 #include "comedi_compat32.h"
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/fcntl.h>
28 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/kmod.h>
32 #include <linux/poll.h>
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/vmalloc.h>
37 #include "comedidev.h"
38 #include <linux/cdev.h>
39 #include <linux/stat.h>
42 #include <linux/uaccess.h>
44 #include "comedi_internal.h"
46 #define COMEDI_NUM_MINORS 0x100
47 #define COMEDI_NUM_SUBDEVICE_MINORS \
48 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
50 #ifdef CONFIG_COMEDI_DEBUG
52 EXPORT_SYMBOL_GPL(comedi_debug);
53 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
54 MODULE_PARM_DESC(comedi_debug,
55 "enable comedi core and driver debugging if non-zero (default 0)"
59 static int comedi_num_legacy_minors;
60 module_param(comedi_num_legacy_minors, int, S_IRUGO);
61 MODULE_PARM_DESC(comedi_num_legacy_minors,
62 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
65 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
66 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
67 MODULE_PARM_DESC(comedi_default_buf_size_kb,
68 "default asynchronous buffer size in KiB (default "
69 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
71 unsigned int comedi_default_buf_maxsize_kb
72 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
73 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
74 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
75 "default maximum size of asynchronous buffer in KiB (default "
76 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
78 static DEFINE_MUTEX(comedi_board_minor_table_lock);
79 static struct comedi_device
80 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
82 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
83 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
84 static struct comedi_subdevice
85 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
87 static struct class *comedi_class;
88 static struct cdev comedi_cdev;
90 static void comedi_device_init(struct comedi_device *dev)
92 kref_init(&dev->refcount);
93 spin_lock_init(&dev->spinlock);
94 mutex_init(&dev->mutex);
95 init_rwsem(&dev->attach_lock);
99 static void comedi_dev_kref_release(struct kref *kref)
101 struct comedi_device *dev =
102 container_of(kref, struct comedi_device, refcount);
104 mutex_destroy(&dev->mutex);
108 int comedi_dev_put(struct comedi_device *dev)
111 return kref_put(&dev->refcount, comedi_dev_kref_release);
114 EXPORT_SYMBOL_GPL(comedi_dev_put);
116 static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
119 kref_get(&dev->refcount);
123 static void comedi_device_cleanup(struct comedi_device *dev)
125 struct module *driver_module = NULL;
129 mutex_lock(&dev->mutex);
131 driver_module = dev->driver->module;
132 comedi_device_detach(dev);
133 while (dev->use_count > 0) {
135 module_put(driver_module);
136 module_put(THIS_MODULE);
139 mutex_unlock(&dev->mutex);
142 static bool comedi_clear_board_dev(struct comedi_device *dev)
144 unsigned int i = dev->minor;
145 bool cleared = false;
147 mutex_lock(&comedi_board_minor_table_lock);
148 if (dev == comedi_board_minor_table[i]) {
149 comedi_board_minor_table[i] = NULL;
152 mutex_unlock(&comedi_board_minor_table_lock);
156 static struct comedi_device *comedi_clear_board_minor(unsigned minor)
158 struct comedi_device *dev;
160 mutex_lock(&comedi_board_minor_table_lock);
161 dev = comedi_board_minor_table[minor];
162 comedi_board_minor_table[minor] = NULL;
163 mutex_unlock(&comedi_board_minor_table_lock);
167 static void comedi_free_board_dev(struct comedi_device *dev)
170 if (dev->class_dev) {
171 device_destroy(comedi_class,
172 MKDEV(COMEDI_MAJOR, dev->minor));
174 comedi_device_cleanup(dev);
179 static struct comedi_subdevice
180 *comedi_subdevice_from_minor(unsigned minor)
182 struct comedi_subdevice *s;
183 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
185 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
186 mutex_lock(&comedi_subdevice_minor_table_lock);
187 s = comedi_subdevice_minor_table[i];
188 mutex_unlock(&comedi_subdevice_minor_table_lock);
192 static struct comedi_device *comedi_dev_get_from_board_minor(unsigned minor)
194 struct comedi_device *dev;
196 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
197 mutex_lock(&comedi_board_minor_table_lock);
198 dev = comedi_dev_get(comedi_board_minor_table[minor]);
199 mutex_unlock(&comedi_board_minor_table_lock);
203 static struct comedi_device *comedi_dev_get_from_subdevice_minor(unsigned minor)
205 struct comedi_device *dev;
206 struct comedi_subdevice *s;
207 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
209 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
210 mutex_lock(&comedi_subdevice_minor_table_lock);
211 s = comedi_subdevice_minor_table[i];
212 dev = comedi_dev_get(s ? s->device : NULL);
213 mutex_unlock(&comedi_subdevice_minor_table_lock);
217 struct comedi_device *comedi_dev_get_from_minor(unsigned minor)
219 if (minor < COMEDI_NUM_BOARD_MINORS)
220 return comedi_dev_get_from_board_minor(minor);
222 return comedi_dev_get_from_subdevice_minor(minor);
224 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
226 static struct comedi_subdevice *
227 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
229 struct comedi_subdevice *s;
231 if (minor >= COMEDI_NUM_BOARD_MINORS) {
232 s = comedi_subdevice_from_minor(minor);
233 if (!s || s->device != dev)
235 if (s->subdev_flags & SDF_CMD_READ)
238 return dev->read_subdev;
241 static struct comedi_subdevice *
242 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
244 struct comedi_subdevice *s;
246 if (minor >= COMEDI_NUM_BOARD_MINORS) {
247 s = comedi_subdevice_from_minor(minor);
248 if (!s || s->device != dev)
250 if (s->subdev_flags & SDF_CMD_WRITE)
253 return dev->write_subdev;
256 static int resize_async_buffer(struct comedi_device *dev,
257 struct comedi_subdevice *s,
258 struct comedi_async *async, unsigned new_size)
262 if (new_size > async->max_bufsize)
266 DPRINTK("subdevice is busy, cannot resize buffer\n");
269 if (async->mmap_count) {
270 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
274 /* make sure buffer is an integral number of pages
276 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
278 retval = comedi_buf_alloc(dev, s, new_size);
283 retval = s->buf_change(dev, s, new_size);
288 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
289 dev->minor, s->index, async->prealloc_bufsz);
293 /* sysfs attribute files */
295 static ssize_t max_read_buffer_kb_show(struct device *csdev,
296 struct device_attribute *attr, char *buf)
298 unsigned int minor = MINOR(csdev->devt);
299 struct comedi_device *dev;
300 struct comedi_subdevice *s;
301 unsigned int size = 0;
303 dev = comedi_dev_get_from_minor(minor);
307 mutex_lock(&dev->mutex);
308 s = comedi_read_subdevice(dev, minor);
309 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
310 size = s->async->max_bufsize / 1024;
311 mutex_unlock(&dev->mutex);
314 return snprintf(buf, PAGE_SIZE, "%i\n", size);
317 static ssize_t max_read_buffer_kb_store(struct device *csdev,
318 struct device_attribute *attr,
319 const char *buf, size_t count)
321 unsigned int minor = MINOR(csdev->devt);
322 struct comedi_device *dev;
323 struct comedi_subdevice *s;
327 err = kstrtouint(buf, 10, &size);
330 if (size > (UINT_MAX / 1024))
334 dev = comedi_dev_get_from_minor(minor);
338 mutex_lock(&dev->mutex);
339 s = comedi_read_subdevice(dev, minor);
340 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
341 s->async->max_bufsize = size;
344 mutex_unlock(&dev->mutex);
347 return err ? err : count;
349 static DEVICE_ATTR_RW(max_read_buffer_kb);
351 static ssize_t read_buffer_kb_show(struct device *csdev,
352 struct device_attribute *attr, char *buf)
354 unsigned int minor = MINOR(csdev->devt);
355 struct comedi_device *dev;
356 struct comedi_subdevice *s;
357 unsigned int size = 0;
359 dev = comedi_dev_get_from_minor(minor);
363 mutex_lock(&dev->mutex);
364 s = comedi_read_subdevice(dev, minor);
365 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
366 size = s->async->prealloc_bufsz / 1024;
367 mutex_unlock(&dev->mutex);
370 return snprintf(buf, PAGE_SIZE, "%i\n", size);
373 static ssize_t read_buffer_kb_store(struct device *csdev,
374 struct device_attribute *attr,
375 const char *buf, size_t count)
377 unsigned int minor = MINOR(csdev->devt);
378 struct comedi_device *dev;
379 struct comedi_subdevice *s;
383 err = kstrtouint(buf, 10, &size);
386 if (size > (UINT_MAX / 1024))
390 dev = comedi_dev_get_from_minor(minor);
394 mutex_lock(&dev->mutex);
395 s = comedi_read_subdevice(dev, minor);
396 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
397 err = resize_async_buffer(dev, s, s->async, size);
400 mutex_unlock(&dev->mutex);
403 return err ? err : count;
405 static DEVICE_ATTR_RW(read_buffer_kb);
407 static ssize_t max_write_buffer_kb_show(struct device *csdev,
408 struct device_attribute *attr,
411 unsigned int minor = MINOR(csdev->devt);
412 struct comedi_device *dev;
413 struct comedi_subdevice *s;
414 unsigned int size = 0;
416 dev = comedi_dev_get_from_minor(minor);
420 mutex_lock(&dev->mutex);
421 s = comedi_write_subdevice(dev, minor);
422 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
423 size = s->async->max_bufsize / 1024;
424 mutex_unlock(&dev->mutex);
427 return snprintf(buf, PAGE_SIZE, "%i\n", size);
430 static ssize_t max_write_buffer_kb_store(struct device *csdev,
431 struct device_attribute *attr,
432 const char *buf, size_t count)
434 unsigned int minor = MINOR(csdev->devt);
435 struct comedi_device *dev;
436 struct comedi_subdevice *s;
440 err = kstrtouint(buf, 10, &size);
443 if (size > (UINT_MAX / 1024))
447 dev = comedi_dev_get_from_minor(minor);
451 mutex_lock(&dev->mutex);
452 s = comedi_write_subdevice(dev, minor);
453 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
454 s->async->max_bufsize = size;
457 mutex_unlock(&dev->mutex);
460 return err ? err : count;
462 static DEVICE_ATTR_RW(max_write_buffer_kb);
464 static ssize_t write_buffer_kb_show(struct device *csdev,
465 struct device_attribute *attr, char *buf)
467 unsigned int minor = MINOR(csdev->devt);
468 struct comedi_device *dev;
469 struct comedi_subdevice *s;
470 unsigned int size = 0;
472 dev = comedi_dev_get_from_minor(minor);
476 mutex_lock(&dev->mutex);
477 s = comedi_write_subdevice(dev, minor);
478 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
479 size = s->async->prealloc_bufsz / 1024;
480 mutex_unlock(&dev->mutex);
483 return snprintf(buf, PAGE_SIZE, "%i\n", size);
486 static ssize_t write_buffer_kb_store(struct device *csdev,
487 struct device_attribute *attr,
488 const char *buf, size_t count)
490 unsigned int minor = MINOR(csdev->devt);
491 struct comedi_device *dev;
492 struct comedi_subdevice *s;
496 err = kstrtouint(buf, 10, &size);
499 if (size > (UINT_MAX / 1024))
503 dev = comedi_dev_get_from_minor(minor);
507 mutex_lock(&dev->mutex);
508 s = comedi_write_subdevice(dev, minor);
509 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
510 err = resize_async_buffer(dev, s, s->async, size);
513 mutex_unlock(&dev->mutex);
516 return err ? err : count;
518 static DEVICE_ATTR_RW(write_buffer_kb);
520 static struct attribute *comedi_dev_attrs[] = {
521 &dev_attr_max_read_buffer_kb.attr,
522 &dev_attr_read_buffer_kb.attr,
523 &dev_attr_max_write_buffer_kb.attr,
524 &dev_attr_write_buffer_kb.attr,
527 ATTRIBUTE_GROUPS(comedi_dev);
529 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
530 unsigned mask, unsigned bits)
534 spin_lock_irqsave(&s->spin_lock, flags);
535 s->runflags &= ~mask;
536 s->runflags |= (bits & mask);
537 spin_unlock_irqrestore(&s->spin_lock, flags);
540 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
545 spin_lock_irqsave(&s->spin_lock, flags);
546 runflags = s->runflags;
547 spin_unlock_irqrestore(&s->spin_lock, flags);
551 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
553 unsigned runflags = comedi_get_subdevice_runflags(s);
555 return (runflags & SRF_RUNNING) ? true : false;
557 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
559 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
561 unsigned runflags = comedi_get_subdevice_runflags(s);
563 return (runflags & SRF_ERROR) ? true : false;
566 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
568 unsigned runflags = comedi_get_subdevice_runflags(s);
570 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
574 * comedi_alloc_spriv() - Allocate memory for the subdevice private data.
575 * @s: comedi_subdevice struct
576 * @size: size of the memory to allocate
578 * This also sets the subdevice runflags to allow the core to automatically
579 * free the private data during the detach.
581 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
583 s->private = kzalloc(size, GFP_KERNEL);
585 s->runflags |= SRF_FREE_SPRIV;
588 EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
591 This function restores a subdevice to an idle state.
593 static void do_become_nonbusy(struct comedi_device *dev,
594 struct comedi_subdevice *s)
596 struct comedi_async *async = s->async;
598 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
600 comedi_buf_reset(async);
601 async->inttrig = NULL;
602 kfree(async->cmd.chanlist);
603 async->cmd.chanlist = NULL;
605 wake_up_interruptible_all(&s->async->wait_head);
607 dev_err(dev->class_dev,
608 "BUG: (?) do_become_nonbusy called with async=NULL\n");
613 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
617 if (comedi_is_subdevice_running(s) && s->cancel)
618 ret = s->cancel(dev, s);
620 do_become_nonbusy(dev, s);
625 void comedi_device_cancel_all(struct comedi_device *dev)
627 struct comedi_subdevice *s;
633 for (i = 0; i < dev->n_subdevices; i++) {
634 s = &dev->subdevices[i];
640 static int is_device_busy(struct comedi_device *dev)
642 struct comedi_subdevice *s;
648 for (i = 0; i < dev->n_subdevices; i++) {
649 s = &dev->subdevices[i];
652 if (s->async && s->async->mmap_count)
664 pointer to devconfig structure
667 devconfig structure at arg
672 static int do_devconfig_ioctl(struct comedi_device *dev,
673 struct comedi_devconfig __user *arg)
675 struct comedi_devconfig it;
677 if (!capable(CAP_SYS_ADMIN))
681 if (is_device_busy(dev))
684 struct module *driver_module = dev->driver->module;
685 comedi_device_detach(dev);
686 module_put(driver_module);
691 if (copy_from_user(&it, arg, sizeof(it)))
694 it.board_name[COMEDI_NAMELEN - 1] = 0;
696 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
697 dev_warn(dev->class_dev,
698 "comedi_config --init_data is deprecated\n");
702 if (dev->minor >= comedi_num_legacy_minors)
703 /* don't re-use dynamically allocated comedi devices */
706 /* This increments the driver module count on success. */
707 return comedi_device_attach(dev, &it);
712 buffer configuration ioctl
715 pointer to bufconfig structure
721 modified bufconfig at arg
724 static int do_bufconfig_ioctl(struct comedi_device *dev,
725 struct comedi_bufconfig __user *arg)
727 struct comedi_bufconfig bc;
728 struct comedi_async *async;
729 struct comedi_subdevice *s;
732 if (copy_from_user(&bc, arg, sizeof(bc)))
735 if (bc.subdevice >= dev->n_subdevices)
738 s = &dev->subdevices[bc.subdevice];
742 DPRINTK("subdevice does not have async capability\n");
748 if (bc.maximum_size) {
749 if (!capable(CAP_SYS_ADMIN))
752 async->max_bufsize = bc.maximum_size;
756 retval = resize_async_buffer(dev, s, async, bc.size);
761 bc.size = async->prealloc_bufsz;
762 bc.maximum_size = async->max_bufsize;
765 if (copy_to_user(arg, &bc, sizeof(bc)))
776 pointer to devinfo structure
785 static int do_devinfo_ioctl(struct comedi_device *dev,
786 struct comedi_devinfo __user *arg,
789 const unsigned minor = iminor(file_inode(file));
790 struct comedi_subdevice *s;
791 struct comedi_devinfo devinfo;
793 memset(&devinfo, 0, sizeof(devinfo));
795 /* fill devinfo structure */
796 devinfo.version_code = COMEDI_VERSION_CODE;
797 devinfo.n_subdevs = dev->n_subdevices;
798 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
799 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
801 s = comedi_read_subdevice(dev, minor);
803 devinfo.read_subdevice = s->index;
805 devinfo.read_subdevice = -1;
807 s = comedi_write_subdevice(dev, minor);
809 devinfo.write_subdevice = s->index;
811 devinfo.write_subdevice = -1;
813 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
824 pointer to array of subdevice info structures
830 array of subdevice info structures at arg
833 static int do_subdinfo_ioctl(struct comedi_device *dev,
834 struct comedi_subdinfo __user *arg, void *file)
837 struct comedi_subdinfo *tmp, *us;
838 struct comedi_subdevice *s;
840 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
844 /* fill subdinfo structs */
845 for (i = 0; i < dev->n_subdevices; i++) {
846 s = &dev->subdevices[i];
850 us->n_chan = s->n_chan;
851 us->subd_flags = s->subdev_flags;
852 if (comedi_is_subdevice_running(s))
853 us->subd_flags |= SDF_RUNNING;
854 #define TIMER_nanosec 5 /* backwards compatibility */
855 us->timer_type = TIMER_nanosec;
856 us->len_chanlist = s->len_chanlist;
857 us->maxdata = s->maxdata;
858 if (s->range_table) {
860 (i << 24) | (0 << 16) | (s->range_table->length);
862 us->range_type = 0; /* XXX */
866 us->subd_flags |= SDF_BUSY;
868 us->subd_flags |= SDF_BUSY_OWNER;
870 us->subd_flags |= SDF_LOCKED;
872 us->subd_flags |= SDF_LOCK_OWNER;
873 if (!s->maxdata && s->maxdata_list)
874 us->subd_flags |= SDF_MAXDATA;
875 if (s->range_table_list)
876 us->subd_flags |= SDF_RANGETYPE;
878 us->subd_flags |= SDF_CMD;
880 if (s->insn_bits != &insn_inval)
881 us->insn_bits_support = COMEDI_SUPPORTED;
883 us->insn_bits_support = COMEDI_UNSUPPORTED;
886 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
890 return ret ? -EFAULT : 0;
898 pointer to chaninfo structure
901 chaninfo structure at arg
904 arrays at elements of chaninfo structure
907 static int do_chaninfo_ioctl(struct comedi_device *dev,
908 struct comedi_chaninfo __user *arg)
910 struct comedi_subdevice *s;
911 struct comedi_chaninfo it;
913 if (copy_from_user(&it, arg, sizeof(it)))
916 if (it.subdev >= dev->n_subdevices)
918 s = &dev->subdevices[it.subdev];
920 if (it.maxdata_list) {
921 if (s->maxdata || !s->maxdata_list)
923 if (copy_to_user(it.maxdata_list, s->maxdata_list,
924 s->n_chan * sizeof(unsigned int)))
929 return -EINVAL; /* flaglist not supported */
934 if (!s->range_table_list)
936 for (i = 0; i < s->n_chan; i++) {
939 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
940 (s->range_table_list[i]->length);
941 if (put_user(x, it.rangelist + i))
945 if (copy_to_user(it.rangelist, s->range_type_list,
946 s->n_chan * sizeof(unsigned int)))
956 buffer information ioctl
959 pointer to bufinfo structure
965 modified bufinfo at arg
968 static int do_bufinfo_ioctl(struct comedi_device *dev,
969 struct comedi_bufinfo __user *arg, void *file)
971 struct comedi_bufinfo bi;
972 struct comedi_subdevice *s;
973 struct comedi_async *async;
975 if (copy_from_user(&bi, arg, sizeof(bi)))
978 if (bi.subdevice >= dev->n_subdevices)
981 s = &dev->subdevices[bi.subdevice];
983 if (s->lock && s->lock != file)
989 DPRINTK("subdevice does not have async capability\n");
990 bi.buf_write_ptr = 0;
992 bi.buf_write_count = 0;
993 bi.buf_read_count = 0;
995 bi.bytes_written = 0;
1000 bi.bytes_written = 0;
1001 goto copyback_position;
1003 if (s->busy != file)
1006 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
1007 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
1008 comedi_buf_read_free(async, bi.bytes_read);
1010 if (comedi_is_subdevice_idle(s) &&
1011 async->buf_write_count == async->buf_read_count) {
1012 do_become_nonbusy(dev, s);
1016 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
1018 comedi_buf_write_alloc(async, bi.bytes_written);
1019 comedi_buf_write_free(async, bi.bytes_written);
1023 bi.buf_write_count = async->buf_write_count;
1024 bi.buf_write_ptr = async->buf_write_ptr;
1025 bi.buf_read_count = async->buf_read_count;
1026 bi.buf_read_ptr = async->buf_read_ptr;
1029 if (copy_to_user(arg, &bi, sizeof(bi)))
1035 static int check_insn_config_length(struct comedi_insn *insn,
1042 case INSN_CONFIG_DIO_OUTPUT:
1043 case INSN_CONFIG_DIO_INPUT:
1044 case INSN_CONFIG_DISARM:
1045 case INSN_CONFIG_RESET:
1049 case INSN_CONFIG_ARM:
1050 case INSN_CONFIG_DIO_QUERY:
1051 case INSN_CONFIG_BLOCK_SIZE:
1052 case INSN_CONFIG_FILTER:
1053 case INSN_CONFIG_SERIAL_CLOCK:
1054 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1055 case INSN_CONFIG_ALT_SOURCE:
1056 case INSN_CONFIG_SET_COUNTER_MODE:
1057 case INSN_CONFIG_8254_READ_STATUS:
1058 case INSN_CONFIG_SET_ROUTING:
1059 case INSN_CONFIG_GET_ROUTING:
1060 case INSN_CONFIG_GET_PWM_STATUS:
1061 case INSN_CONFIG_PWM_SET_PERIOD:
1062 case INSN_CONFIG_PWM_GET_PERIOD:
1066 case INSN_CONFIG_SET_GATE_SRC:
1067 case INSN_CONFIG_GET_GATE_SRC:
1068 case INSN_CONFIG_SET_CLOCK_SRC:
1069 case INSN_CONFIG_GET_CLOCK_SRC:
1070 case INSN_CONFIG_SET_OTHER_SRC:
1071 case INSN_CONFIG_GET_COUNTER_STATUS:
1072 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1073 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1074 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1078 case INSN_CONFIG_PWM_OUTPUT:
1079 case INSN_CONFIG_ANALOG_TRIG:
1083 case INSN_CONFIG_DIGITAL_TRIG:
1087 /* by default we allow the insn since we don't have checks for
1088 * all possible cases yet */
1090 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1092 pr_warn("comedi: Add a check to %s in %s.\n",
1093 __func__, __FILE__);
1094 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1100 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1101 unsigned int *data, void *file)
1103 struct comedi_subdevice *s;
1107 if (insn->insn & INSN_MASK_SPECIAL) {
1108 /* a non-subdevice instruction */
1110 switch (insn->insn) {
1120 do_gettimeofday(&tv);
1121 data[0] = tv.tv_sec;
1122 data[1] = tv.tv_usec;
1128 if (insn->n != 1 || data[0] >= 100000) {
1132 udelay(data[0] / 1000);
1140 if (insn->subdev >= dev->n_subdevices) {
1141 DPRINTK("%d not usable subdevice\n",
1146 s = &dev->subdevices[insn->subdev];
1148 DPRINTK("no async\n");
1152 if (!s->async->inttrig) {
1153 DPRINTK("no inttrig\n");
1157 ret = s->async->inttrig(dev, s, data[0]);
1162 DPRINTK("invalid insn\n");
1167 /* a subdevice instruction */
1168 unsigned int maxdata;
1170 if (insn->subdev >= dev->n_subdevices) {
1171 DPRINTK("subdevice %d out of range\n", insn->subdev);
1175 s = &dev->subdevices[insn->subdev];
1177 if (s->type == COMEDI_SUBD_UNUSED) {
1178 DPRINTK("%d not usable subdevice\n", insn->subdev);
1183 /* are we locked? (ioctl lock) */
1184 if (s->lock && s->lock != file) {
1185 DPRINTK("device locked\n");
1190 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1193 DPRINTK("bad chanspec\n");
1201 /* This looks arbitrary. It is. */
1202 s->busy = &parse_insn;
1203 switch (insn->insn) {
1205 ret = s->insn_read(dev, s, insn, data);
1208 maxdata = s->maxdata_list
1209 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1211 for (i = 0; i < insn->n; ++i) {
1212 if (data[i] > maxdata) {
1214 DPRINTK("bad data value(s)\n");
1219 ret = s->insn_write(dev, s, insn, data);
1225 /* Most drivers ignore the base channel in
1226 * insn->chanspec. Fix this here if
1227 * the subdevice has <= 32 channels. */
1229 unsigned int orig_mask;
1231 orig_mask = data[0];
1232 if (s->n_chan <= 32) {
1233 shift = CR_CHAN(insn->chanspec);
1241 ret = s->insn_bits(dev, s, insn, data);
1242 data[0] = orig_mask;
1248 ret = check_insn_config_length(insn, data);
1251 ret = s->insn_config(dev, s, insn, data);
1267 * synchronous instructions
1270 * pointer to sync cmd structure
1273 * sync cmd struct at arg
1280 /* arbitrary limits */
1281 #define MAX_SAMPLES 256
1282 static int do_insnlist_ioctl(struct comedi_device *dev,
1283 struct comedi_insnlist __user *arg, void *file)
1285 struct comedi_insnlist insnlist;
1286 struct comedi_insn *insns = NULL;
1287 unsigned int *data = NULL;
1291 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1294 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1296 DPRINTK("kmalloc failed\n");
1301 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1303 DPRINTK("kmalloc failed\n");
1308 if (copy_from_user(insns, insnlist.insns,
1309 sizeof(*insns) * insnlist.n_insns)) {
1310 DPRINTK("copy_from_user failed\n");
1315 for (i = 0; i < insnlist.n_insns; i++) {
1316 if (insns[i].n > MAX_SAMPLES) {
1317 DPRINTK("number of samples too large\n");
1321 if (insns[i].insn & INSN_MASK_WRITE) {
1322 if (copy_from_user(data, insns[i].data,
1323 insns[i].n * sizeof(unsigned int))) {
1324 DPRINTK("copy_from_user failed\n");
1329 ret = parse_insn(dev, insns + i, data, file);
1332 if (insns[i].insn & INSN_MASK_READ) {
1333 if (copy_to_user(insns[i].data, data,
1334 insns[i].n * sizeof(unsigned int))) {
1335 DPRINTK("copy_to_user failed\n");
1355 * synchronous instructions
1361 * struct comedi_insn struct at arg
1367 static int do_insn_ioctl(struct comedi_device *dev,
1368 struct comedi_insn __user *arg, void *file)
1370 struct comedi_insn insn;
1371 unsigned int *data = NULL;
1374 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1380 if (copy_from_user(&insn, arg, sizeof(insn))) {
1385 /* This is where the behavior of insn and insnlist deviate. */
1386 if (insn.n > MAX_SAMPLES)
1387 insn.n = MAX_SAMPLES;
1388 if (insn.insn & INSN_MASK_WRITE) {
1389 if (copy_from_user(data,
1391 insn.n * sizeof(unsigned int))) {
1396 ret = parse_insn(dev, &insn, data, file);
1399 if (insn.insn & INSN_MASK_READ) {
1400 if (copy_to_user(insn.data,
1402 insn.n * sizeof(unsigned int))) {
1415 static int do_cmd_ioctl(struct comedi_device *dev,
1416 struct comedi_cmd __user *arg, void *file)
1418 struct comedi_cmd cmd;
1419 struct comedi_subdevice *s;
1420 struct comedi_async *async;
1422 unsigned int __user *user_chanlist;
1424 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1425 DPRINTK("bad cmd address\n");
1428 /* save user's chanlist pointer so it can be restored later */
1429 user_chanlist = (unsigned int __user *)cmd.chanlist;
1431 if (cmd.subdev >= dev->n_subdevices) {
1432 DPRINTK("%d no such subdevice\n", cmd.subdev);
1436 s = &dev->subdevices[cmd.subdev];
1439 if (s->type == COMEDI_SUBD_UNUSED) {
1440 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1444 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1445 DPRINTK("subdevice %i does not support commands\n",
1450 /* are we locked? (ioctl lock) */
1451 if (s->lock && s->lock != file) {
1452 DPRINTK("subdevice locked\n");
1458 DPRINTK("subdevice busy\n");
1462 /* make sure channel/gain list isn't too long */
1463 if (cmd.chanlist_len > s->len_chanlist) {
1464 DPRINTK("channel/gain list too long %u > %d\n",
1465 cmd.chanlist_len, s->len_chanlist);
1469 /* make sure channel/gain list isn't too short */
1470 if (cmd.chanlist_len < 1) {
1471 DPRINTK("channel/gain list too short %u < 1\n",
1477 async->cmd.data = NULL;
1478 /* load channel/gain list */
1479 async->cmd.chanlist = memdup_user(user_chanlist,
1480 async->cmd.chanlist_len * sizeof(int));
1481 if (IS_ERR(async->cmd.chanlist)) {
1482 ret = PTR_ERR(async->cmd.chanlist);
1483 DPRINTK("memdup_user failed with code %d\n", ret);
1487 /* make sure each element in channel/gain list is valid */
1488 ret = comedi_check_chanlist(s,
1489 async->cmd.chanlist_len,
1490 async->cmd.chanlist);
1492 DPRINTK("bad chanlist\n");
1496 ret = s->do_cmdtest(dev, s, &async->cmd);
1498 if (async->cmd.flags & TRIG_BOGUS || ret) {
1499 DPRINTK("test returned %d\n", ret);
1501 /* restore chanlist pointer before copying back */
1502 cmd.chanlist = (unsigned int __force *)user_chanlist;
1504 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1505 DPRINTK("fault writing cmd\n");
1513 if (!async->prealloc_bufsz) {
1515 DPRINTK("no buffer (?)\n");
1519 comedi_buf_reset(async);
1522 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1524 if (async->cmd.flags & TRIG_WAKE_EOS)
1525 async->cb_mask |= COMEDI_CB_EOS;
1527 comedi_set_subdevice_runflags(s, SRF_ERROR | SRF_RUNNING, SRF_RUNNING);
1529 /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
1530 * comedi_read() or comedi_write() */
1532 ret = s->do_cmd(dev, s);
1537 do_become_nonbusy(dev, s);
1544 command testing ioctl
1547 pointer to cmd structure
1550 cmd structure at arg
1554 modified cmd structure at arg
1557 static int do_cmdtest_ioctl(struct comedi_device *dev,
1558 struct comedi_cmd __user *arg, void *file)
1560 struct comedi_cmd cmd;
1561 struct comedi_subdevice *s;
1563 unsigned int *chanlist = NULL;
1564 unsigned int __user *user_chanlist;
1566 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1567 DPRINTK("bad cmd address\n");
1570 /* save user's chanlist pointer so it can be restored later */
1571 user_chanlist = (unsigned int __user *)cmd.chanlist;
1573 if (cmd.subdev >= dev->n_subdevices) {
1574 DPRINTK("%d no such subdevice\n", cmd.subdev);
1578 s = &dev->subdevices[cmd.subdev];
1579 if (s->type == COMEDI_SUBD_UNUSED) {
1580 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1584 if (!s->do_cmd || !s->do_cmdtest) {
1585 DPRINTK("subdevice %i does not support commands\n",
1590 /* make sure channel/gain list isn't too long */
1591 if (cmd.chanlist_len > s->len_chanlist) {
1592 DPRINTK("channel/gain list too long %d > %d\n",
1593 cmd.chanlist_len, s->len_chanlist);
1598 /* load channel/gain list */
1600 chanlist = memdup_user(user_chanlist,
1601 cmd.chanlist_len * sizeof(int));
1602 if (IS_ERR(chanlist)) {
1603 ret = PTR_ERR(chanlist);
1604 DPRINTK("memdup_user exited with code %d", ret);
1608 /* make sure each element in channel/gain list is valid */
1609 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1611 DPRINTK("bad chanlist\n");
1615 cmd.chanlist = chanlist;
1618 ret = s->do_cmdtest(dev, s, &cmd);
1620 /* restore chanlist pointer before copying back */
1621 cmd.chanlist = (unsigned int __force *)user_chanlist;
1623 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1624 DPRINTK("bad cmd address\n");
1649 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1653 unsigned long flags;
1654 struct comedi_subdevice *s;
1656 if (arg >= dev->n_subdevices)
1658 s = &dev->subdevices[arg];
1660 spin_lock_irqsave(&s->spin_lock, flags);
1661 if (s->busy || s->lock)
1665 spin_unlock_irqrestore(&s->spin_lock, flags);
1672 ret = s->lock_f(dev, s);
1691 This function isn't protected by the semaphore, since
1692 we already own the lock.
1694 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1697 struct comedi_subdevice *s;
1699 if (arg >= dev->n_subdevices)
1701 s = &dev->subdevices[arg];
1706 if (s->lock && s->lock != file)
1709 if (s->lock == file) {
1723 cancel acquisition ioctl
1735 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1738 struct comedi_subdevice *s;
1741 if (arg >= dev->n_subdevices)
1743 s = &dev->subdevices[arg];
1744 if (s->async == NULL)
1747 if (s->lock && s->lock != file)
1753 if (s->busy != file)
1756 ret = do_cancel(dev, s);
1763 instructs driver to synchronize buffers
1775 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1778 struct comedi_subdevice *s;
1780 if (arg >= dev->n_subdevices)
1782 s = &dev->subdevices[arg];
1784 if (s->lock && s->lock != file)
1790 if (s->busy != file)
1794 return s->poll(dev, s);
1799 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1802 const unsigned minor = iminor(file_inode(file));
1803 struct comedi_device *dev = file->private_data;
1806 mutex_lock(&dev->mutex);
1808 /* Device config is special, because it must work on
1809 * an unconfigured device. */
1810 if (cmd == COMEDI_DEVCONFIG) {
1811 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1812 /* Device config not appropriate on non-board minors. */
1816 rc = do_devconfig_ioctl(dev,
1817 (struct comedi_devconfig __user *)arg);
1820 dev->minor >= comedi_num_legacy_minors) {
1821 /* Successfully unconfigured a dynamically
1822 * allocated device. Try and remove it. */
1823 if (comedi_clear_board_dev(dev)) {
1824 mutex_unlock(&dev->mutex);
1825 comedi_free_board_dev(dev);
1833 if (!dev->attached) {
1834 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1840 case COMEDI_BUFCONFIG:
1841 rc = do_bufconfig_ioctl(dev,
1842 (struct comedi_bufconfig __user *)arg);
1844 case COMEDI_DEVINFO:
1845 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1848 case COMEDI_SUBDINFO:
1849 rc = do_subdinfo_ioctl(dev,
1850 (struct comedi_subdinfo __user *)arg,
1853 case COMEDI_CHANINFO:
1854 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1856 case COMEDI_RANGEINFO:
1857 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1859 case COMEDI_BUFINFO:
1860 rc = do_bufinfo_ioctl(dev,
1861 (struct comedi_bufinfo __user *)arg,
1865 rc = do_lock_ioctl(dev, arg, file);
1868 rc = do_unlock_ioctl(dev, arg, file);
1871 rc = do_cancel_ioctl(dev, arg, file);
1874 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1876 case COMEDI_CMDTEST:
1877 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1880 case COMEDI_INSNLIST:
1881 rc = do_insnlist_ioctl(dev,
1882 (struct comedi_insnlist __user *)arg,
1886 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1890 rc = do_poll_ioctl(dev, arg, file);
1898 mutex_unlock(&dev->mutex);
1902 static void comedi_vm_open(struct vm_area_struct *area)
1904 struct comedi_async *async;
1905 struct comedi_device *dev;
1907 async = area->vm_private_data;
1908 dev = async->subdevice->device;
1910 mutex_lock(&dev->mutex);
1911 async->mmap_count++;
1912 mutex_unlock(&dev->mutex);
1915 static void comedi_vm_close(struct vm_area_struct *area)
1917 struct comedi_async *async;
1918 struct comedi_device *dev;
1920 async = area->vm_private_data;
1921 dev = async->subdevice->device;
1923 mutex_lock(&dev->mutex);
1924 async->mmap_count--;
1925 mutex_unlock(&dev->mutex);
1928 static struct vm_operations_struct comedi_vm_ops = {
1929 .open = comedi_vm_open,
1930 .close = comedi_vm_close,
1933 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1935 const unsigned minor = iminor(file_inode(file));
1936 struct comedi_device *dev = file->private_data;
1937 struct comedi_subdevice *s;
1938 struct comedi_async *async;
1939 unsigned long start = vma->vm_start;
1945 mutex_lock(&dev->mutex);
1947 if (!dev->attached) {
1948 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1953 if (vma->vm_flags & VM_WRITE)
1954 s = comedi_write_subdevice(dev, minor);
1956 s = comedi_read_subdevice(dev, minor);
1968 if (vma->vm_pgoff != 0) {
1969 DPRINTK("comedi: mmap() offset must be 0.\n");
1974 size = vma->vm_end - vma->vm_start;
1975 if (size > async->prealloc_bufsz) {
1979 if (size & (~PAGE_MASK)) {
1984 n_pages = size >> PAGE_SHIFT;
1985 for (i = 0; i < n_pages; ++i) {
1986 struct comedi_buf_page *buf = &async->buf_page_list[i];
1988 if (remap_pfn_range(vma, start,
1989 page_to_pfn(virt_to_page(buf->virt_addr)),
1990 PAGE_SIZE, PAGE_SHARED)) {
1997 vma->vm_ops = &comedi_vm_ops;
1998 vma->vm_private_data = async;
2000 async->mmap_count++;
2004 mutex_unlock(&dev->mutex);
2008 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2010 unsigned int mask = 0;
2011 const unsigned minor = iminor(file_inode(file));
2012 struct comedi_device *dev = file->private_data;
2013 struct comedi_subdevice *s;
2015 mutex_lock(&dev->mutex);
2017 if (!dev->attached) {
2018 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2022 s = comedi_read_subdevice(dev, minor);
2023 if (s && s->async) {
2024 poll_wait(file, &s->async->wait_head, wait);
2025 if (!s->busy || !comedi_is_subdevice_running(s) ||
2026 comedi_buf_read_n_available(s->async) > 0)
2027 mask |= POLLIN | POLLRDNORM;
2030 s = comedi_write_subdevice(dev, minor);
2031 if (s && s->async) {
2032 unsigned int bps = bytes_per_sample(s->async->subdevice);
2034 poll_wait(file, &s->async->wait_head, wait);
2035 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2036 if (!s->busy || !comedi_is_subdevice_running(s) ||
2037 comedi_buf_write_n_allocated(s->async) >= bps)
2038 mask |= POLLOUT | POLLWRNORM;
2042 mutex_unlock(&dev->mutex);
2046 static ssize_t comedi_write(struct file *file, const char __user *buf,
2047 size_t nbytes, loff_t *offset)
2049 struct comedi_subdevice *s;
2050 struct comedi_async *async;
2051 int n, m, count = 0, retval = 0;
2052 DECLARE_WAITQUEUE(wait, current);
2053 const unsigned minor = iminor(file_inode(file));
2054 struct comedi_device *dev = file->private_data;
2055 bool on_wait_queue = false;
2057 unsigned int old_detach_count;
2059 /* Protect against device detachment during operation. */
2060 down_read(&dev->attach_lock);
2061 attach_locked = true;
2062 old_detach_count = dev->detach_count;
2064 if (!dev->attached) {
2065 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2070 s = comedi_write_subdevice(dev, minor);
2071 if (!s || !s->async) {
2078 if (!s->busy || !nbytes)
2080 if (s->busy != file) {
2085 add_wait_queue(&async->wait_head, &wait);
2086 on_wait_queue = true;
2087 while (nbytes > 0 && !retval) {
2088 set_current_state(TASK_INTERRUPTIBLE);
2090 if (!comedi_is_subdevice_running(s)) {
2092 struct comedi_subdevice *new_s;
2094 if (comedi_is_subdevice_in_error(s))
2099 * To avoid deadlock, cannot acquire dev->mutex
2100 * while dev->attach_lock is held. Need to
2101 * remove task from the async wait queue before
2102 * releasing dev->attach_lock, as it might not
2103 * be valid afterwards.
2105 remove_wait_queue(&async->wait_head, &wait);
2106 on_wait_queue = false;
2107 up_read(&dev->attach_lock);
2108 attach_locked = false;
2109 mutex_lock(&dev->mutex);
2111 * Become non-busy unless things have changed
2112 * behind our back. Checking dev->detach_count
2113 * is unchanged ought to be sufficient (unless
2114 * there have been 2**32 detaches in the
2115 * meantime!), but check the subdevice pointer
2116 * as well just in case.
2118 new_s = comedi_write_subdevice(dev, minor);
2119 if (dev->attached &&
2120 old_detach_count == dev->detach_count &&
2121 s == new_s && new_s->async == async)
2122 do_become_nonbusy(dev, s);
2123 mutex_unlock(&dev->mutex);
2131 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2132 m = async->prealloc_bufsz - async->buf_write_ptr;
2133 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2134 if (m > comedi_buf_write_n_allocated(async))
2135 m = comedi_buf_write_n_allocated(async);
2140 if (file->f_flags & O_NONBLOCK) {
2145 if (signal_pending(current)) {
2146 retval = -ERESTARTSYS;
2151 if (s->busy != file) {
2158 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2164 comedi_buf_write_free(async, n);
2170 break; /* makes device work like a pipe */
2174 remove_wait_queue(&async->wait_head, &wait);
2175 set_current_state(TASK_RUNNING);
2177 up_read(&dev->attach_lock);
2179 return count ? count : retval;
2182 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2185 struct comedi_subdevice *s;
2186 struct comedi_async *async;
2187 int n, m, count = 0, retval = 0;
2188 DECLARE_WAITQUEUE(wait, current);
2189 const unsigned minor = iminor(file_inode(file));
2190 struct comedi_device *dev = file->private_data;
2191 unsigned int old_detach_count;
2192 bool become_nonbusy = false;
2195 /* Protect against device detachment during operation. */
2196 down_read(&dev->attach_lock);
2197 attach_locked = true;
2198 old_detach_count = dev->detach_count;
2200 if (!dev->attached) {
2201 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2206 s = comedi_read_subdevice(dev, minor);
2207 if (!s || !s->async) {
2213 if (!s->busy || !nbytes)
2215 if (s->busy != file) {
2220 add_wait_queue(&async->wait_head, &wait);
2221 while (nbytes > 0 && !retval) {
2222 set_current_state(TASK_INTERRUPTIBLE);
2226 m = comedi_buf_read_n_available(async);
2227 /* printk("%d available\n",m); */
2228 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2229 m = async->prealloc_bufsz - async->buf_read_ptr;
2230 /* printk("%d contiguous\n",m); */
2235 if (!comedi_is_subdevice_running(s)) {
2236 if (comedi_is_subdevice_in_error(s))
2240 become_nonbusy = true;
2243 if (file->f_flags & O_NONBLOCK) {
2248 if (signal_pending(current)) {
2249 retval = -ERESTARTSYS;
2256 if (s->busy != file) {
2262 m = copy_to_user(buf, async->prealloc_buf +
2263 async->buf_read_ptr, n);
2269 comedi_buf_read_alloc(async, n);
2270 comedi_buf_read_free(async, n);
2276 break; /* makes device work like a pipe */
2278 remove_wait_queue(&async->wait_head, &wait);
2279 set_current_state(TASK_RUNNING);
2280 if (become_nonbusy || comedi_is_subdevice_idle(s)) {
2281 struct comedi_subdevice *new_s;
2284 * To avoid deadlock, cannot acquire dev->mutex
2285 * while dev->attach_lock is held.
2287 up_read(&dev->attach_lock);
2288 attach_locked = false;
2289 mutex_lock(&dev->mutex);
2291 * Check device hasn't become detached behind our back.
2292 * Checking dev->detach_count is unchanged ought to be
2293 * sufficient (unless there have been 2**32 detaches in the
2294 * meantime!), but check the subdevice pointer as well just in
2297 new_s = comedi_read_subdevice(dev, minor);
2298 if (dev->attached && old_detach_count == dev->detach_count &&
2299 s == new_s && new_s->async == async) {
2300 if (become_nonbusy ||
2301 async->buf_read_count - async->buf_write_count == 0)
2302 do_become_nonbusy(dev, s);
2304 mutex_unlock(&dev->mutex);
2308 up_read(&dev->attach_lock);
2310 return count ? count : retval;
2313 static int comedi_open(struct inode *inode, struct file *file)
2315 const unsigned minor = iminor(inode);
2316 struct comedi_device *dev = comedi_dev_get_from_minor(minor);
2320 DPRINTK("invalid minor number\n");
2324 /* This is slightly hacky, but we want module autoloading
2326 * case: user opens device, attached -> ok
2327 * case: user opens device, unattached, !in_request_module -> autoload
2328 * case: user opens device, unattached, in_request_module -> fail
2329 * case: root opens device, attached -> ok
2330 * case: root opens device, unattached, in_request_module -> ok
2331 * (typically called from modprobe)
2332 * case: root opens device, unattached, !in_request_module -> autoload
2334 * The last could be changed to "-> ok", which would deny root
2337 mutex_lock(&dev->mutex);
2340 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2341 DPRINTK("in request module\n");
2345 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2348 dev->in_request_module = true;
2351 mutex_unlock(&dev->mutex);
2352 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2353 mutex_lock(&dev->mutex);
2356 dev->in_request_module = false;
2358 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2359 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2364 __module_get(THIS_MODULE);
2366 if (dev->attached) {
2367 if (!try_module_get(dev->driver->module)) {
2368 module_put(THIS_MODULE);
2374 if (dev->attached && dev->use_count == 0 && dev->open) {
2375 rc = dev->open(dev);
2377 module_put(dev->driver->module);
2378 module_put(THIS_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 && dev->close)
2422 module_put(THIS_MODULE);
2424 module_put(dev->driver->module);
2428 mutex_unlock(&dev->mutex);
2429 comedi_dev_put(dev);
2434 static const struct file_operations comedi_fops = {
2435 .owner = THIS_MODULE,
2436 .unlocked_ioctl = comedi_unlocked_ioctl,
2437 .compat_ioctl = comedi_compat_ioctl,
2438 .open = comedi_open,
2439 .release = comedi_close,
2440 .read = comedi_read,
2441 .write = comedi_write,
2442 .mmap = comedi_mmap,
2443 .poll = comedi_poll,
2444 .fasync = comedi_fasync,
2445 .llseek = noop_llseek,
2448 void comedi_error(const struct comedi_device *dev, const char *s)
2450 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2452 EXPORT_SYMBOL_GPL(comedi_error);
2454 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2456 struct comedi_async *async = s->async;
2457 unsigned runflags = 0;
2458 unsigned runflags_mask = 0;
2460 /* DPRINTK("comedi_event 0x%x\n",mask); */
2462 if (!comedi_is_subdevice_running(s))
2466 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2467 COMEDI_CB_OVERFLOW)) {
2468 runflags_mask |= SRF_RUNNING;
2470 /* remember if an error event has occurred, so an error
2471 * can be returned the next time the user does a read() */
2472 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2473 runflags_mask |= SRF_ERROR;
2474 runflags |= SRF_ERROR;
2476 if (runflags_mask) {
2477 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2478 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2481 if (async->cb_mask & s->async->events) {
2482 wake_up_interruptible(&async->wait_head);
2483 if (s->subdev_flags & SDF_CMD_READ)
2484 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2485 if (s->subdev_flags & SDF_CMD_WRITE)
2486 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2488 s->async->events = 0;
2490 EXPORT_SYMBOL_GPL(comedi_event);
2492 /* Note: the ->mutex is pre-locked on successful return */
2493 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2495 struct comedi_device *dev;
2496 struct device *csdev;
2499 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2501 return ERR_PTR(-ENOMEM);
2502 comedi_device_init(dev);
2503 comedi_set_hw_dev(dev, hardware_device);
2504 mutex_lock(&dev->mutex);
2505 mutex_lock(&comedi_board_minor_table_lock);
2506 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2507 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2508 if (comedi_board_minor_table[i] == NULL) {
2509 comedi_board_minor_table[i] = dev;
2513 mutex_unlock(&comedi_board_minor_table_lock);
2514 if (i == COMEDI_NUM_BOARD_MINORS) {
2515 mutex_unlock(&dev->mutex);
2516 comedi_device_cleanup(dev);
2517 comedi_dev_put(dev);
2518 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2519 return ERR_PTR(-EBUSY);
2522 csdev = device_create(comedi_class, hardware_device,
2523 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2525 dev->class_dev = csdev;
2527 /* Note: dev->mutex needs to be unlocked by the caller. */
2531 static void comedi_free_board_minor(unsigned minor)
2533 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2534 comedi_free_board_dev(comedi_clear_board_minor(minor));
2537 void comedi_release_hardware_device(struct device *hardware_device)
2540 struct comedi_device *dev;
2542 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2544 mutex_lock(&comedi_board_minor_table_lock);
2545 dev = comedi_board_minor_table[minor];
2546 if (dev && dev->hw_dev == hardware_device) {
2547 comedi_board_minor_table[minor] = NULL;
2548 mutex_unlock(&comedi_board_minor_table_lock);
2549 comedi_free_board_dev(dev);
2552 mutex_unlock(&comedi_board_minor_table_lock);
2556 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2558 struct comedi_device *dev = s->device;
2559 struct device *csdev;
2562 mutex_lock(&comedi_subdevice_minor_table_lock);
2563 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2564 if (comedi_subdevice_minor_table[i] == NULL) {
2565 comedi_subdevice_minor_table[i] = s;
2569 mutex_unlock(&comedi_subdevice_minor_table_lock);
2570 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2571 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2574 i += COMEDI_NUM_BOARD_MINORS;
2576 csdev = device_create(comedi_class, dev->class_dev,
2577 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2578 dev->minor, s->index);
2580 s->class_dev = csdev;
2585 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2594 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2595 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2597 i = s->minor - COMEDI_NUM_BOARD_MINORS;
2598 mutex_lock(&comedi_subdevice_minor_table_lock);
2599 if (s == comedi_subdevice_minor_table[i])
2600 comedi_subdevice_minor_table[i] = NULL;
2601 mutex_unlock(&comedi_subdevice_minor_table_lock);
2603 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2604 s->class_dev = NULL;
2608 static void comedi_cleanup_board_minors(void)
2612 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2613 comedi_free_board_minor(i);
2616 static int __init comedi_init(void)
2621 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2623 if (comedi_num_legacy_minors < 0 ||
2624 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2625 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2626 COMEDI_NUM_BOARD_MINORS);
2630 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2631 COMEDI_NUM_MINORS, "comedi");
2634 cdev_init(&comedi_cdev, &comedi_fops);
2635 comedi_cdev.owner = THIS_MODULE;
2636 kobject_set_name(&comedi_cdev.kobj, "comedi");
2637 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2638 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2642 comedi_class = class_create(THIS_MODULE, "comedi");
2643 if (IS_ERR(comedi_class)) {
2644 pr_err("comedi: failed to create class\n");
2645 cdev_del(&comedi_cdev);
2646 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2648 return PTR_ERR(comedi_class);
2651 comedi_class->dev_groups = comedi_dev_groups;
2653 /* XXX requires /proc interface */
2656 /* create devices files for legacy/manual use */
2657 for (i = 0; i < comedi_num_legacy_minors; i++) {
2658 struct comedi_device *dev;
2659 dev = comedi_alloc_board_minor(NULL);
2661 comedi_cleanup_board_minors();
2662 cdev_del(&comedi_cdev);
2663 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2665 return PTR_ERR(dev);
2667 /* comedi_alloc_board_minor() locked the mutex */
2668 mutex_unlock(&dev->mutex);
2674 module_init(comedi_init);
2676 static void __exit comedi_cleanup(void)
2680 comedi_cleanup_board_minors();
2681 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2682 BUG_ON(comedi_board_minor_table[i]);
2683 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2684 BUG_ON(comedi_subdevice_minor_table[i]);
2686 class_destroy(comedi_class);
2687 cdev_del(&comedi_cdev);
2688 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2690 comedi_proc_cleanup();
2692 module_exit(comedi_cleanup);
2694 MODULE_AUTHOR("http://www.comedi.org");
2695 MODULE_DESCRIPTION("Comedi core module");
2696 MODULE_LICENSE("GPL");