5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "comedi_compat32.h"
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/fcntl.h>
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/kmod.h>
38 #include <linux/poll.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/vmalloc.h>
43 #include "comedidev.h"
44 #include <linux/cdev.h>
45 #include <linux/stat.h>
48 #include <linux/uaccess.h>
50 #include "comedi_internal.h"
52 #define COMEDI_NUM_MINORS 0x100
53 #define COMEDI_NUM_SUBDEVICE_MINORS \
54 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
56 #ifdef CONFIG_COMEDI_DEBUG
58 EXPORT_SYMBOL(comedi_debug);
59 module_param(comedi_debug, int, S_IRUGO | S_IWUSR);
60 MODULE_PARM_DESC(comedi_debug,
61 "enable comedi core and driver debugging if non-zero (default 0)"
65 static int comedi_num_legacy_minors;
66 module_param(comedi_num_legacy_minors, int, S_IRUGO);
67 MODULE_PARM_DESC(comedi_num_legacy_minors,
68 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
71 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
72 module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
73 MODULE_PARM_DESC(comedi_default_buf_size_kb,
74 "default asynchronous buffer size in KiB (default "
75 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
77 unsigned int comedi_default_buf_maxsize_kb
78 = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
79 module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
80 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
81 "default maximum size of asynchronous buffer in KiB (default "
82 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
84 struct comedi_file_info {
85 struct comedi_device *device;
86 struct comedi_subdevice *read_subdevice;
87 struct comedi_subdevice *write_subdevice;
90 static DEFINE_MUTEX(comedi_board_minor_table_lock);
91 static struct comedi_file_info
92 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
94 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
95 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
96 static struct comedi_file_info
97 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
99 static struct class *comedi_class;
100 static struct cdev comedi_cdev;
102 static void comedi_device_init(struct comedi_device *dev)
104 spin_lock_init(&dev->spinlock);
105 mutex_init(&dev->mutex);
109 static void comedi_device_cleanup(struct comedi_device *dev)
111 struct module *driver_module = NULL;
115 mutex_lock(&dev->mutex);
117 driver_module = dev->driver->module;
118 comedi_device_detach(dev);
119 while (dev->use_count > 0) {
121 module_put(driver_module);
122 module_put(THIS_MODULE);
125 mutex_unlock(&dev->mutex);
126 mutex_destroy(&dev->mutex);
129 static struct comedi_file_info *comedi_clear_board_minor(unsigned minor)
131 struct comedi_file_info *info;
133 mutex_lock(&comedi_board_minor_table_lock);
134 info = comedi_board_minor_table[minor];
135 comedi_board_minor_table[minor] = NULL;
136 mutex_unlock(&comedi_board_minor_table_lock);
140 static struct comedi_file_info *comedi_clear_subdevice_minor(unsigned minor)
142 struct comedi_file_info *info;
143 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
145 mutex_lock(&comedi_subdevice_minor_table_lock);
146 info = comedi_subdevice_minor_table[i];
147 comedi_subdevice_minor_table[i] = NULL;
148 mutex_unlock(&comedi_subdevice_minor_table_lock);
152 static void comedi_free_board_file_info(struct comedi_file_info *info)
155 struct comedi_device *dev = info->device;
157 if (dev->class_dev) {
158 device_destroy(comedi_class,
159 MKDEV(COMEDI_MAJOR, dev->minor));
161 comedi_device_cleanup(dev);
168 static struct comedi_file_info
169 *comedi_file_info_from_board_minor(unsigned minor)
171 struct comedi_file_info *info;
173 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
174 mutex_lock(&comedi_board_minor_table_lock);
175 info = comedi_board_minor_table[minor];
176 mutex_unlock(&comedi_board_minor_table_lock);
180 static struct comedi_file_info
181 *comedi_file_info_from_subdevice_minor(unsigned minor)
183 struct comedi_file_info *info;
184 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
186 BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
187 mutex_lock(&comedi_subdevice_minor_table_lock);
188 info = comedi_subdevice_minor_table[i];
189 mutex_unlock(&comedi_subdevice_minor_table_lock);
193 static struct comedi_file_info *comedi_file_info_from_minor(unsigned minor)
195 if (minor < COMEDI_NUM_BOARD_MINORS)
196 return comedi_file_info_from_board_minor(minor);
198 return comedi_file_info_from_subdevice_minor(minor);
201 static struct comedi_device *
202 comedi_dev_from_file_info(struct comedi_file_info *info)
204 return info ? info->device : NULL;
207 struct comedi_device *comedi_dev_from_minor(unsigned minor)
209 return comedi_dev_from_file_info(comedi_file_info_from_minor(minor));
211 EXPORT_SYMBOL_GPL(comedi_dev_from_minor);
213 static struct comedi_subdevice *
214 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
216 struct comedi_file_info *info;
218 if (minor >= COMEDI_NUM_BOARD_MINORS) {
219 info = comedi_file_info_from_subdevice_minor(minor);
220 if (!info || info->device != dev)
222 if (info->read_subdevice)
223 return info->read_subdevice;
225 return dev->read_subdev;
228 static struct comedi_subdevice *
229 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
231 struct comedi_file_info *info;
233 if (minor >= COMEDI_NUM_BOARD_MINORS) {
234 info = comedi_file_info_from_subdevice_minor(minor);
235 if (!info || info->device != dev)
237 if (info->write_subdevice)
238 return info->write_subdevice;
240 return dev->write_subdev;
243 static int resize_async_buffer(struct comedi_device *dev,
244 struct comedi_subdevice *s,
245 struct comedi_async *async, unsigned new_size)
249 if (new_size > async->max_bufsize)
253 DPRINTK("subdevice is busy, cannot resize buffer\n");
256 if (async->mmap_count) {
257 DPRINTK("subdevice is mmapped, cannot resize buffer\n");
261 if (!async->prealloc_buf)
264 /* make sure buffer is an integral number of pages
266 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
268 retval = comedi_buf_alloc(dev, s, new_size);
273 retval = s->buf_change(dev, s, new_size);
278 DPRINTK("comedi%i subd %d buffer resized to %i bytes\n",
279 dev->minor, s->index, async->prealloc_bufsz);
283 /* sysfs attribute files */
285 static ssize_t show_max_read_buffer_kb(struct device *csdev,
286 struct device_attribute *attr, char *buf)
288 unsigned int minor = MINOR(csdev->devt);
289 struct comedi_file_info *info;
290 struct comedi_device *dev;
291 struct comedi_subdevice *s;
292 unsigned int size = 0;
294 info = comedi_file_info_from_minor(minor);
299 mutex_lock(&dev->mutex);
300 s = comedi_read_subdevice(dev, minor);
301 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
302 size = s->async->max_bufsize / 1024;
303 mutex_unlock(&dev->mutex);
305 return snprintf(buf, PAGE_SIZE, "%i\n", size);
308 static ssize_t store_max_read_buffer_kb(struct device *csdev,
309 struct device_attribute *attr,
310 const char *buf, size_t count)
312 unsigned int minor = MINOR(csdev->devt);
313 struct comedi_file_info *info;
314 struct comedi_device *dev;
315 struct comedi_subdevice *s;
319 err = kstrtouint(buf, 10, &size);
322 if (size > (UINT_MAX / 1024))
326 info = comedi_file_info_from_minor(minor);
331 mutex_lock(&dev->mutex);
332 s = comedi_read_subdevice(dev, minor);
333 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
334 s->async->max_bufsize = size;
337 mutex_unlock(&dev->mutex);
339 return err ? err : count;
342 static ssize_t show_read_buffer_kb(struct device *csdev,
343 struct device_attribute *attr, char *buf)
345 unsigned int minor = MINOR(csdev->devt);
346 struct comedi_file_info *info;
347 struct comedi_device *dev;
348 struct comedi_subdevice *s;
349 unsigned int size = 0;
351 info = comedi_file_info_from_minor(minor);
356 mutex_lock(&dev->mutex);
357 s = comedi_read_subdevice(dev, minor);
358 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
359 size = s->async->prealloc_bufsz / 1024;
360 mutex_unlock(&dev->mutex);
362 return snprintf(buf, PAGE_SIZE, "%i\n", size);
365 static ssize_t store_read_buffer_kb(struct device *csdev,
366 struct device_attribute *attr,
367 const char *buf, size_t count)
369 unsigned int minor = MINOR(csdev->devt);
370 struct comedi_file_info *info;
371 struct comedi_device *dev;
372 struct comedi_subdevice *s;
376 err = kstrtouint(buf, 10, &size);
379 if (size > (UINT_MAX / 1024))
383 info = comedi_file_info_from_minor(minor);
388 mutex_lock(&dev->mutex);
389 s = comedi_read_subdevice(dev, minor);
390 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
391 err = resize_async_buffer(dev, s, s->async, size);
394 mutex_unlock(&dev->mutex);
396 return err ? err : count;
399 static ssize_t show_max_write_buffer_kb(struct device *csdev,
400 struct device_attribute *attr,
403 unsigned int minor = MINOR(csdev->devt);
404 struct comedi_file_info *info;
405 struct comedi_device *dev;
406 struct comedi_subdevice *s;
407 unsigned int size = 0;
409 info = comedi_file_info_from_minor(minor);
414 mutex_lock(&dev->mutex);
415 s = comedi_write_subdevice(dev, minor);
416 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
417 size = s->async->max_bufsize / 1024;
418 mutex_unlock(&dev->mutex);
420 return snprintf(buf, PAGE_SIZE, "%i\n", size);
423 static ssize_t store_max_write_buffer_kb(struct device *csdev,
424 struct device_attribute *attr,
425 const char *buf, size_t count)
427 unsigned int minor = MINOR(csdev->devt);
428 struct comedi_file_info *info;
429 struct comedi_device *dev;
430 struct comedi_subdevice *s;
434 err = kstrtouint(buf, 10, &size);
437 if (size > (UINT_MAX / 1024))
441 info = comedi_file_info_from_minor(minor);
446 mutex_lock(&dev->mutex);
447 s = comedi_write_subdevice(dev, minor);
448 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
449 s->async->max_bufsize = size;
452 mutex_unlock(&dev->mutex);
454 return err ? err : count;
457 static ssize_t show_write_buffer_kb(struct device *csdev,
458 struct device_attribute *attr, char *buf)
460 unsigned int minor = MINOR(csdev->devt);
461 struct comedi_file_info *info;
462 struct comedi_device *dev;
463 struct comedi_subdevice *s;
464 unsigned int size = 0;
466 info = comedi_file_info_from_minor(minor);
471 mutex_lock(&dev->mutex);
472 s = comedi_write_subdevice(dev, minor);
473 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
474 size = s->async->prealloc_bufsz / 1024;
475 mutex_unlock(&dev->mutex);
477 return snprintf(buf, PAGE_SIZE, "%i\n", size);
480 static ssize_t store_write_buffer_kb(struct device *csdev,
481 struct device_attribute *attr,
482 const char *buf, size_t count)
484 unsigned int minor = MINOR(csdev->devt);
485 struct comedi_file_info *info;
486 struct comedi_device *dev;
487 struct comedi_subdevice *s;
491 err = kstrtouint(buf, 10, &size);
494 if (size > (UINT_MAX / 1024))
498 info = comedi_file_info_from_minor(minor);
503 mutex_lock(&dev->mutex);
504 s = comedi_write_subdevice(dev, minor);
505 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
506 err = resize_async_buffer(dev, s, s->async, size);
509 mutex_unlock(&dev->mutex);
511 return err ? err : count;
514 static struct device_attribute comedi_dev_attrs[] = {
515 __ATTR(max_read_buffer_kb, S_IRUGO | S_IWUSR,
516 show_max_read_buffer_kb, store_max_read_buffer_kb),
517 __ATTR(read_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
518 show_read_buffer_kb, store_read_buffer_kb),
519 __ATTR(max_write_buffer_kb, S_IRUGO | S_IWUSR,
520 show_max_write_buffer_kb, store_max_write_buffer_kb),
521 __ATTR(write_buffer_kb, S_IRUGO | S_IWUSR | S_IWGRP,
522 show_write_buffer_kb, store_write_buffer_kb),
526 static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
527 unsigned mask, unsigned bits)
531 spin_lock_irqsave(&s->spin_lock, flags);
532 s->runflags &= ~mask;
533 s->runflags |= (bits & mask);
534 spin_unlock_irqrestore(&s->spin_lock, flags);
537 static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
542 spin_lock_irqsave(&s->spin_lock, flags);
543 runflags = s->runflags;
544 spin_unlock_irqrestore(&s->spin_lock, flags);
548 bool comedi_is_subdevice_running(struct comedi_subdevice *s)
550 unsigned runflags = comedi_get_subdevice_runflags(s);
552 return (runflags & SRF_RUNNING) ? true : false;
554 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
556 static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
558 unsigned runflags = comedi_get_subdevice_runflags(s);
560 return (runflags & SRF_ERROR) ? true : false;
563 static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
565 unsigned runflags = comedi_get_subdevice_runflags(s);
567 return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
571 This function restores a subdevice to an idle state.
573 static void do_become_nonbusy(struct comedi_device *dev,
574 struct comedi_subdevice *s)
576 struct comedi_async *async = s->async;
578 comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
580 comedi_buf_reset(async);
581 async->inttrig = NULL;
582 kfree(async->cmd.chanlist);
583 async->cmd.chanlist = NULL;
585 dev_err(dev->class_dev,
586 "BUG: (?) do_become_nonbusy called with async=NULL\n");
592 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
596 if (comedi_is_subdevice_running(s) && s->cancel)
597 ret = s->cancel(dev, s);
599 do_become_nonbusy(dev, s);
604 static int is_device_busy(struct comedi_device *dev)
606 struct comedi_subdevice *s;
612 for (i = 0; i < dev->n_subdevices; i++) {
613 s = &dev->subdevices[i];
616 if (s->async && s->async->mmap_count)
628 pointer to devconfig structure
631 devconfig structure at arg
636 static int do_devconfig_ioctl(struct comedi_device *dev,
637 struct comedi_devconfig __user *arg)
639 struct comedi_devconfig it;
641 if (!capable(CAP_SYS_ADMIN))
645 if (is_device_busy(dev))
648 struct module *driver_module = dev->driver->module;
649 comedi_device_detach(dev);
650 module_put(driver_module);
655 if (copy_from_user(&it, arg, sizeof(it)))
658 it.board_name[COMEDI_NAMELEN - 1] = 0;
660 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
661 dev_warn(dev->class_dev,
662 "comedi_config --init_data is deprecated\n");
666 if (dev->minor >= comedi_num_legacy_minors)
667 /* don't re-use dynamically allocated comedi devices */
670 /* This increments the driver module count on success. */
671 return comedi_device_attach(dev, &it);
676 buffer configuration ioctl
679 pointer to bufconfig structure
685 modified bufconfig at arg
688 static int do_bufconfig_ioctl(struct comedi_device *dev,
689 struct comedi_bufconfig __user *arg)
691 struct comedi_bufconfig bc;
692 struct comedi_async *async;
693 struct comedi_subdevice *s;
696 if (copy_from_user(&bc, arg, sizeof(bc)))
699 if (bc.subdevice >= dev->n_subdevices || bc.subdevice < 0)
702 s = &dev->subdevices[bc.subdevice];
706 DPRINTK("subdevice does not have async capability\n");
712 if (bc.maximum_size) {
713 if (!capable(CAP_SYS_ADMIN))
716 async->max_bufsize = bc.maximum_size;
720 retval = resize_async_buffer(dev, s, async, bc.size);
725 bc.size = async->prealloc_bufsz;
726 bc.maximum_size = async->max_bufsize;
729 if (copy_to_user(arg, &bc, sizeof(bc)))
740 pointer to devinfo structure
749 static int do_devinfo_ioctl(struct comedi_device *dev,
750 struct comedi_devinfo __user *arg,
753 const unsigned minor = iminor(file_inode(file));
754 struct comedi_subdevice *s;
755 struct comedi_devinfo devinfo;
757 memset(&devinfo, 0, sizeof(devinfo));
759 /* fill devinfo structure */
760 devinfo.version_code = COMEDI_VERSION_CODE;
761 devinfo.n_subdevs = dev->n_subdevices;
762 strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
763 strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
765 s = comedi_read_subdevice(dev, minor);
767 devinfo.read_subdevice = s->index;
769 devinfo.read_subdevice = -1;
771 s = comedi_write_subdevice(dev, minor);
773 devinfo.write_subdevice = s->index;
775 devinfo.write_subdevice = -1;
777 if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
788 pointer to array of subdevice info structures
794 array of subdevice info structures at arg
797 static int do_subdinfo_ioctl(struct comedi_device *dev,
798 struct comedi_subdinfo __user *arg, void *file)
801 struct comedi_subdinfo *tmp, *us;
802 struct comedi_subdevice *s;
804 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
808 /* fill subdinfo structs */
809 for (i = 0; i < dev->n_subdevices; i++) {
810 s = &dev->subdevices[i];
814 us->n_chan = s->n_chan;
815 us->subd_flags = s->subdev_flags;
816 if (comedi_is_subdevice_running(s))
817 us->subd_flags |= SDF_RUNNING;
818 #define TIMER_nanosec 5 /* backwards compatibility */
819 us->timer_type = TIMER_nanosec;
820 us->len_chanlist = s->len_chanlist;
821 us->maxdata = s->maxdata;
822 if (s->range_table) {
824 (i << 24) | (0 << 16) | (s->range_table->length);
826 us->range_type = 0; /* XXX */
828 us->flags = s->flags;
831 us->subd_flags |= SDF_BUSY;
833 us->subd_flags |= SDF_BUSY_OWNER;
835 us->subd_flags |= SDF_LOCKED;
837 us->subd_flags |= SDF_LOCK_OWNER;
838 if (!s->maxdata && s->maxdata_list)
839 us->subd_flags |= SDF_MAXDATA;
841 us->subd_flags |= SDF_FLAGS;
842 if (s->range_table_list)
843 us->subd_flags |= SDF_RANGETYPE;
845 us->subd_flags |= SDF_CMD;
847 if (s->insn_bits != &insn_inval)
848 us->insn_bits_support = COMEDI_SUPPORTED;
850 us->insn_bits_support = COMEDI_UNSUPPORTED;
852 us->settling_time_0 = s->settling_time_0;
855 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
859 return ret ? -EFAULT : 0;
867 pointer to chaninfo structure
870 chaninfo structure at arg
873 arrays at elements of chaninfo structure
876 static int do_chaninfo_ioctl(struct comedi_device *dev,
877 struct comedi_chaninfo __user *arg)
879 struct comedi_subdevice *s;
880 struct comedi_chaninfo it;
882 if (copy_from_user(&it, arg, sizeof(it)))
885 if (it.subdev >= dev->n_subdevices)
887 s = &dev->subdevices[it.subdev];
889 if (it.maxdata_list) {
890 if (s->maxdata || !s->maxdata_list)
892 if (copy_to_user(it.maxdata_list, s->maxdata_list,
893 s->n_chan * sizeof(unsigned int)))
900 if (copy_to_user(it.flaglist, s->flaglist,
901 s->n_chan * sizeof(unsigned int)))
908 if (!s->range_table_list)
910 for (i = 0; i < s->n_chan; i++) {
913 x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
914 (s->range_table_list[i]->length);
915 if (put_user(x, it.rangelist + i))
919 if (copy_to_user(it.rangelist, s->range_type_list,
920 s->n_chan * sizeof(unsigned int)))
930 buffer information ioctl
933 pointer to bufinfo structure
939 modified bufinfo at arg
942 static int do_bufinfo_ioctl(struct comedi_device *dev,
943 struct comedi_bufinfo __user *arg, void *file)
945 struct comedi_bufinfo bi;
946 struct comedi_subdevice *s;
947 struct comedi_async *async;
949 if (copy_from_user(&bi, arg, sizeof(bi)))
952 if (bi.subdevice >= dev->n_subdevices || bi.subdevice < 0)
955 s = &dev->subdevices[bi.subdevice];
957 if (s->lock && s->lock != file)
963 DPRINTK("subdevice does not have async capability\n");
964 bi.buf_write_ptr = 0;
966 bi.buf_write_count = 0;
967 bi.buf_read_count = 0;
969 bi.bytes_written = 0;
974 bi.bytes_written = 0;
975 goto copyback_position;
980 if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
981 bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read);
982 comedi_buf_read_free(async, bi.bytes_read);
984 if (comedi_is_subdevice_idle(s) &&
985 async->buf_write_count == async->buf_read_count) {
986 do_become_nonbusy(dev, s);
990 if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
992 comedi_buf_write_alloc(async, bi.bytes_written);
993 comedi_buf_write_free(async, bi.bytes_written);
997 bi.buf_write_count = async->buf_write_count;
998 bi.buf_write_ptr = async->buf_write_ptr;
999 bi.buf_read_count = async->buf_read_count;
1000 bi.buf_read_ptr = async->buf_read_ptr;
1003 if (copy_to_user(arg, &bi, sizeof(bi)))
1009 static int check_insn_config_length(struct comedi_insn *insn,
1016 case INSN_CONFIG_DIO_OUTPUT:
1017 case INSN_CONFIG_DIO_INPUT:
1018 case INSN_CONFIG_DISARM:
1019 case INSN_CONFIG_RESET:
1023 case INSN_CONFIG_ARM:
1024 case INSN_CONFIG_DIO_QUERY:
1025 case INSN_CONFIG_BLOCK_SIZE:
1026 case INSN_CONFIG_FILTER:
1027 case INSN_CONFIG_SERIAL_CLOCK:
1028 case INSN_CONFIG_BIDIRECTIONAL_DATA:
1029 case INSN_CONFIG_ALT_SOURCE:
1030 case INSN_CONFIG_SET_COUNTER_MODE:
1031 case INSN_CONFIG_8254_READ_STATUS:
1032 case INSN_CONFIG_SET_ROUTING:
1033 case INSN_CONFIG_GET_ROUTING:
1034 case INSN_CONFIG_GET_PWM_STATUS:
1035 case INSN_CONFIG_PWM_SET_PERIOD:
1036 case INSN_CONFIG_PWM_GET_PERIOD:
1040 case INSN_CONFIG_SET_GATE_SRC:
1041 case INSN_CONFIG_GET_GATE_SRC:
1042 case INSN_CONFIG_SET_CLOCK_SRC:
1043 case INSN_CONFIG_GET_CLOCK_SRC:
1044 case INSN_CONFIG_SET_OTHER_SRC:
1045 case INSN_CONFIG_GET_COUNTER_STATUS:
1046 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1047 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1048 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
1052 case INSN_CONFIG_PWM_OUTPUT:
1053 case INSN_CONFIG_ANALOG_TRIG:
1057 case INSN_CONFIG_DIGITAL_TRIG:
1061 /* by default we allow the insn since we don't have checks for
1062 * all possible cases yet */
1064 pr_warn("comedi: No check for data length of config insn id %i is implemented.\n",
1066 pr_warn("comedi: Add a check to %s in %s.\n",
1067 __func__, __FILE__);
1068 pr_warn("comedi: Assuming n=%i is correct.\n", insn->n);
1074 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
1075 unsigned int *data, void *file)
1077 struct comedi_subdevice *s;
1081 if (insn->insn & INSN_MASK_SPECIAL) {
1082 /* a non-subdevice instruction */
1084 switch (insn->insn) {
1094 do_gettimeofday(&tv);
1095 data[0] = tv.tv_sec;
1096 data[1] = tv.tv_usec;
1102 if (insn->n != 1 || data[0] >= 100000) {
1106 udelay(data[0] / 1000);
1114 if (insn->subdev >= dev->n_subdevices) {
1115 DPRINTK("%d not usable subdevice\n",
1120 s = &dev->subdevices[insn->subdev];
1122 DPRINTK("no async\n");
1126 if (!s->async->inttrig) {
1127 DPRINTK("no inttrig\n");
1131 ret = s->async->inttrig(dev, s, data[0]);
1136 DPRINTK("invalid insn\n");
1141 /* a subdevice instruction */
1142 unsigned int maxdata;
1144 if (insn->subdev >= dev->n_subdevices) {
1145 DPRINTK("subdevice %d out of range\n", insn->subdev);
1149 s = &dev->subdevices[insn->subdev];
1151 if (s->type == COMEDI_SUBD_UNUSED) {
1152 DPRINTK("%d not usable subdevice\n", insn->subdev);
1157 /* are we locked? (ioctl lock) */
1158 if (s->lock && s->lock != file) {
1159 DPRINTK("device locked\n");
1164 ret = comedi_check_chanlist(s, 1, &insn->chanspec);
1167 DPRINTK("bad chanspec\n");
1175 /* This looks arbitrary. It is. */
1176 s->busy = &parse_insn;
1177 switch (insn->insn) {
1179 ret = s->insn_read(dev, s, insn, data);
1182 maxdata = s->maxdata_list
1183 ? s->maxdata_list[CR_CHAN(insn->chanspec)]
1185 for (i = 0; i < insn->n; ++i) {
1186 if (data[i] > maxdata) {
1188 DPRINTK("bad data value(s)\n");
1193 ret = s->insn_write(dev, s, insn, data);
1199 /* Most drivers ignore the base channel in
1200 * insn->chanspec. Fix this here if
1201 * the subdevice has <= 32 channels. */
1203 unsigned int orig_mask;
1205 orig_mask = data[0];
1206 if (s->n_chan <= 32) {
1207 shift = CR_CHAN(insn->chanspec);
1215 ret = s->insn_bits(dev, s, insn, data);
1216 data[0] = orig_mask;
1222 ret = check_insn_config_length(insn, data);
1225 ret = s->insn_config(dev, s, insn, data);
1241 * synchronous instructions
1244 * pointer to sync cmd structure
1247 * sync cmd struct at arg
1254 /* arbitrary limits */
1255 #define MAX_SAMPLES 256
1256 static int do_insnlist_ioctl(struct comedi_device *dev,
1257 struct comedi_insnlist __user *arg, void *file)
1259 struct comedi_insnlist insnlist;
1260 struct comedi_insn *insns = NULL;
1261 unsigned int *data = NULL;
1265 if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
1268 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1270 DPRINTK("kmalloc failed\n");
1275 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
1277 DPRINTK("kmalloc failed\n");
1282 if (copy_from_user(insns, insnlist.insns,
1283 sizeof(*insns) * insnlist.n_insns)) {
1284 DPRINTK("copy_from_user failed\n");
1289 for (i = 0; i < insnlist.n_insns; i++) {
1290 if (insns[i].n > MAX_SAMPLES) {
1291 DPRINTK("number of samples too large\n");
1295 if (insns[i].insn & INSN_MASK_WRITE) {
1296 if (copy_from_user(data, insns[i].data,
1297 insns[i].n * sizeof(unsigned int))) {
1298 DPRINTK("copy_from_user failed\n");
1303 ret = parse_insn(dev, insns + i, data, file);
1306 if (insns[i].insn & INSN_MASK_READ) {
1307 if (copy_to_user(insns[i].data, data,
1308 insns[i].n * sizeof(unsigned int))) {
1309 DPRINTK("copy_to_user failed\n");
1329 * synchronous instructions
1335 * struct comedi_insn struct at arg
1341 static int do_insn_ioctl(struct comedi_device *dev,
1342 struct comedi_insn __user *arg, void *file)
1344 struct comedi_insn insn;
1345 unsigned int *data = NULL;
1348 data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL);
1354 if (copy_from_user(&insn, arg, sizeof(insn))) {
1359 /* This is where the behavior of insn and insnlist deviate. */
1360 if (insn.n > MAX_SAMPLES)
1361 insn.n = MAX_SAMPLES;
1362 if (insn.insn & INSN_MASK_WRITE) {
1363 if (copy_from_user(data,
1365 insn.n * sizeof(unsigned int))) {
1370 ret = parse_insn(dev, &insn, data, file);
1373 if (insn.insn & INSN_MASK_READ) {
1374 if (copy_to_user(insn.data,
1376 insn.n * sizeof(unsigned int))) {
1389 static int do_cmd_ioctl(struct comedi_device *dev,
1390 struct comedi_cmd __user *arg, void *file)
1392 struct comedi_cmd cmd;
1393 struct comedi_subdevice *s;
1394 struct comedi_async *async;
1396 unsigned int __user *user_chanlist;
1398 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1399 DPRINTK("bad cmd address\n");
1402 /* save user's chanlist pointer so it can be restored later */
1403 user_chanlist = (unsigned int __user *)cmd.chanlist;
1405 if (cmd.subdev >= dev->n_subdevices) {
1406 DPRINTK("%d no such subdevice\n", cmd.subdev);
1410 s = &dev->subdevices[cmd.subdev];
1413 if (s->type == COMEDI_SUBD_UNUSED) {
1414 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1418 if (!s->do_cmd || !s->do_cmdtest || !s->async) {
1419 DPRINTK("subdevice %i does not support commands\n",
1424 /* are we locked? (ioctl lock) */
1425 if (s->lock && s->lock != file) {
1426 DPRINTK("subdevice locked\n");
1432 DPRINTK("subdevice busy\n");
1437 /* make sure channel/gain list isn't too long */
1438 if (cmd.chanlist_len > s->len_chanlist) {
1439 DPRINTK("channel/gain list too long %u > %d\n",
1440 cmd.chanlist_len, s->len_chanlist);
1445 /* make sure channel/gain list isn't too short */
1446 if (cmd.chanlist_len < 1) {
1447 DPRINTK("channel/gain list too short %u < 1\n",
1454 async->cmd.data = NULL;
1455 /* load channel/gain list */
1456 async->cmd.chanlist =
1457 kmalloc(async->cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1458 if (!async->cmd.chanlist) {
1459 DPRINTK("allocation failed\n");
1464 if (copy_from_user(async->cmd.chanlist, user_chanlist,
1465 async->cmd.chanlist_len * sizeof(int))) {
1466 DPRINTK("fault reading chanlist\n");
1471 /* make sure each element in channel/gain list is valid */
1472 ret = comedi_check_chanlist(s,
1473 async->cmd.chanlist_len,
1474 async->cmd.chanlist);
1476 DPRINTK("bad chanlist\n");
1480 ret = s->do_cmdtest(dev, s, &async->cmd);
1482 if (async->cmd.flags & TRIG_BOGUS || ret) {
1483 DPRINTK("test returned %d\n", ret);
1485 /* restore chanlist pointer before copying back */
1486 cmd.chanlist = (unsigned int __force *)user_chanlist;
1488 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1489 DPRINTK("fault writing cmd\n");
1497 if (!async->prealloc_bufsz) {
1499 DPRINTK("no buffer (?)\n");
1503 comedi_buf_reset(async);
1506 COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR |
1508 if (async->cmd.flags & TRIG_WAKE_EOS)
1509 async->cb_mask |= COMEDI_CB_EOS;
1511 comedi_set_subdevice_runflags(s, ~0, SRF_USER | SRF_RUNNING);
1513 ret = s->do_cmd(dev, s);
1518 do_become_nonbusy(dev, s);
1525 command testing ioctl
1528 pointer to cmd structure
1531 cmd structure at arg
1535 modified cmd structure at arg
1538 static int do_cmdtest_ioctl(struct comedi_device *dev,
1539 struct comedi_cmd __user *arg, void *file)
1541 struct comedi_cmd cmd;
1542 struct comedi_subdevice *s;
1544 unsigned int *chanlist = NULL;
1545 unsigned int __user *user_chanlist;
1547 if (copy_from_user(&cmd, arg, sizeof(cmd))) {
1548 DPRINTK("bad cmd address\n");
1551 /* save user's chanlist pointer so it can be restored later */
1552 user_chanlist = (unsigned int __user *)cmd.chanlist;
1554 if (cmd.subdev >= dev->n_subdevices) {
1555 DPRINTK("%d no such subdevice\n", cmd.subdev);
1559 s = &dev->subdevices[cmd.subdev];
1560 if (s->type == COMEDI_SUBD_UNUSED) {
1561 DPRINTK("%d not valid subdevice\n", cmd.subdev);
1565 if (!s->do_cmd || !s->do_cmdtest) {
1566 DPRINTK("subdevice %i does not support commands\n",
1571 /* make sure channel/gain list isn't too long */
1572 if (cmd.chanlist_len > s->len_chanlist) {
1573 DPRINTK("channel/gain list too long %d > %d\n",
1574 cmd.chanlist_len, s->len_chanlist);
1579 /* load channel/gain list */
1582 kmalloc(cmd.chanlist_len * sizeof(int), GFP_KERNEL);
1584 DPRINTK("allocation failed\n");
1589 if (copy_from_user(chanlist, user_chanlist,
1590 cmd.chanlist_len * sizeof(int))) {
1591 DPRINTK("fault reading chanlist\n");
1596 /* make sure each element in channel/gain list is valid */
1597 ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist);
1599 DPRINTK("bad chanlist\n");
1603 cmd.chanlist = chanlist;
1606 ret = s->do_cmdtest(dev, s, &cmd);
1608 /* restore chanlist pointer before copying back */
1609 cmd.chanlist = (unsigned int __force *)user_chanlist;
1611 if (copy_to_user(arg, &cmd, sizeof(cmd))) {
1612 DPRINTK("bad cmd address\n");
1637 static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg,
1641 unsigned long flags;
1642 struct comedi_subdevice *s;
1644 if (arg >= dev->n_subdevices)
1646 s = &dev->subdevices[arg];
1648 spin_lock_irqsave(&s->spin_lock, flags);
1649 if (s->busy || s->lock)
1653 spin_unlock_irqrestore(&s->spin_lock, flags);
1660 ret = s->lock_f(dev, s);
1679 This function isn't protected by the semaphore, since
1680 we already own the lock.
1682 static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg,
1685 struct comedi_subdevice *s;
1687 if (arg >= dev->n_subdevices)
1689 s = &dev->subdevices[arg];
1694 if (s->lock && s->lock != file)
1697 if (s->lock == file) {
1711 cancel acquisition ioctl
1723 static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg,
1726 struct comedi_subdevice *s;
1728 if (arg >= dev->n_subdevices)
1730 s = &dev->subdevices[arg];
1731 if (s->async == NULL)
1734 if (s->lock && s->lock != file)
1740 if (s->busy != file)
1743 return do_cancel(dev, s);
1748 instructs driver to synchronize buffers
1760 static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg,
1763 struct comedi_subdevice *s;
1765 if (arg >= dev->n_subdevices)
1767 s = &dev->subdevices[arg];
1769 if (s->lock && s->lock != file)
1775 if (s->busy != file)
1779 return s->poll(dev, s);
1784 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
1787 const unsigned minor = iminor(file_inode(file));
1788 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1789 struct comedi_device *dev = comedi_dev_from_file_info(info);
1795 mutex_lock(&dev->mutex);
1797 /* Device config is special, because it must work on
1798 * an unconfigured device. */
1799 if (cmd == COMEDI_DEVCONFIG) {
1800 if (minor >= COMEDI_NUM_BOARD_MINORS) {
1801 /* Device config not appropriate on non-board minors. */
1805 rc = do_devconfig_ioctl(dev,
1806 (struct comedi_devconfig __user *)arg);
1809 dev->minor >= comedi_num_legacy_minors) {
1810 /* Successfully unconfigured a dynamically
1811 * allocated device. Try and remove it. */
1812 info = comedi_clear_board_minor(dev->minor);
1814 mutex_unlock(&dev->mutex);
1815 comedi_free_board_file_info(info);
1823 if (!dev->attached) {
1824 DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor);
1830 case COMEDI_BUFCONFIG:
1831 rc = do_bufconfig_ioctl(dev,
1832 (struct comedi_bufconfig __user *)arg);
1834 case COMEDI_DEVINFO:
1835 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
1838 case COMEDI_SUBDINFO:
1839 rc = do_subdinfo_ioctl(dev,
1840 (struct comedi_subdinfo __user *)arg,
1843 case COMEDI_CHANINFO:
1844 rc = do_chaninfo_ioctl(dev, (void __user *)arg);
1846 case COMEDI_RANGEINFO:
1847 rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
1849 case COMEDI_BUFINFO:
1850 rc = do_bufinfo_ioctl(dev,
1851 (struct comedi_bufinfo __user *)arg,
1855 rc = do_lock_ioctl(dev, arg, file);
1858 rc = do_unlock_ioctl(dev, arg, file);
1861 rc = do_cancel_ioctl(dev, arg, file);
1864 rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
1866 case COMEDI_CMDTEST:
1867 rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
1870 case COMEDI_INSNLIST:
1871 rc = do_insnlist_ioctl(dev,
1872 (struct comedi_insnlist __user *)arg,
1876 rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
1880 rc = do_poll_ioctl(dev, arg, file);
1888 mutex_unlock(&dev->mutex);
1892 static void comedi_vm_open(struct vm_area_struct *area)
1894 struct comedi_async *async;
1895 struct comedi_device *dev;
1897 async = area->vm_private_data;
1898 dev = async->subdevice->device;
1900 mutex_lock(&dev->mutex);
1901 async->mmap_count++;
1902 mutex_unlock(&dev->mutex);
1905 static void comedi_vm_close(struct vm_area_struct *area)
1907 struct comedi_async *async;
1908 struct comedi_device *dev;
1910 async = area->vm_private_data;
1911 dev = async->subdevice->device;
1913 mutex_lock(&dev->mutex);
1914 async->mmap_count--;
1915 mutex_unlock(&dev->mutex);
1918 static struct vm_operations_struct comedi_vm_ops = {
1919 .open = comedi_vm_open,
1920 .close = comedi_vm_close,
1923 static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
1925 const unsigned minor = iminor(file_inode(file));
1926 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
1927 struct comedi_device *dev = comedi_dev_from_file_info(info);
1928 struct comedi_subdevice *s;
1929 struct comedi_async *async;
1930 unsigned long start = vma->vm_start;
1939 mutex_lock(&dev->mutex);
1941 if (!dev->attached) {
1942 DPRINTK("no driver configured on comedi%i\n", dev->minor);
1947 if (vma->vm_flags & VM_WRITE)
1948 s = comedi_write_subdevice(dev, minor);
1950 s = comedi_read_subdevice(dev, minor);
1962 if (vma->vm_pgoff != 0) {
1963 DPRINTK("comedi: mmap() offset must be 0.\n");
1968 size = vma->vm_end - vma->vm_start;
1969 if (size > async->prealloc_bufsz) {
1973 if (size & (~PAGE_MASK)) {
1978 n_pages = size >> PAGE_SHIFT;
1979 for (i = 0; i < n_pages; ++i) {
1980 struct comedi_buf_page *buf = &async->buf_page_list[i];
1982 if (remap_pfn_range(vma, start,
1983 page_to_pfn(virt_to_page(buf->virt_addr)),
1984 PAGE_SIZE, PAGE_SHARED)) {
1991 vma->vm_ops = &comedi_vm_ops;
1992 vma->vm_private_data = async;
1994 async->mmap_count++;
1998 mutex_unlock(&dev->mutex);
2002 static unsigned int comedi_poll(struct file *file, poll_table *wait)
2004 unsigned int mask = 0;
2005 const unsigned minor = iminor(file_inode(file));
2006 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
2007 struct comedi_device *dev = comedi_dev_from_file_info(info);
2008 struct comedi_subdevice *s;
2013 mutex_lock(&dev->mutex);
2015 if (!dev->attached) {
2016 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2020 s = comedi_read_subdevice(dev, minor);
2021 if (s && s->async) {
2022 poll_wait(file, &s->async->wait_head, wait);
2023 if (!s->busy || !comedi_is_subdevice_running(s) ||
2024 comedi_buf_read_n_available(s->async) > 0)
2025 mask |= POLLIN | POLLRDNORM;
2028 s = comedi_write_subdevice(dev, minor);
2029 if (s && s->async) {
2030 unsigned int bps = bytes_per_sample(s->async->subdevice);
2032 poll_wait(file, &s->async->wait_head, wait);
2033 comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz);
2034 if (!s->busy || !comedi_is_subdevice_running(s) ||
2035 comedi_buf_write_n_allocated(s->async) >= bps)
2036 mask |= POLLOUT | POLLWRNORM;
2040 mutex_unlock(&dev->mutex);
2044 static ssize_t comedi_write(struct file *file, const char __user *buf,
2045 size_t nbytes, loff_t *offset)
2047 struct comedi_subdevice *s;
2048 struct comedi_async *async;
2049 int n, m, count = 0, retval = 0;
2050 DECLARE_WAITQUEUE(wait, current);
2051 const unsigned minor = iminor(file_inode(file));
2052 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
2053 struct comedi_device *dev = comedi_dev_from_file_info(info);
2058 if (!dev->attached) {
2059 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2063 s = comedi_write_subdevice(dev, minor);
2064 if (!s || !s->async)
2069 if (!s->busy || !nbytes)
2071 if (s->busy != file)
2074 add_wait_queue(&async->wait_head, &wait);
2075 while (nbytes > 0 && !retval) {
2076 set_current_state(TASK_INTERRUPTIBLE);
2078 if (!comedi_is_subdevice_running(s)) {
2080 if (comedi_is_subdevice_in_error(s))
2084 do_become_nonbusy(dev, s);
2092 if (async->buf_write_ptr + m > async->prealloc_bufsz)
2093 m = async->prealloc_bufsz - async->buf_write_ptr;
2094 comedi_buf_write_alloc(async, async->prealloc_bufsz);
2095 if (m > comedi_buf_write_n_allocated(async))
2096 m = comedi_buf_write_n_allocated(async);
2101 if (file->f_flags & O_NONBLOCK) {
2106 if (signal_pending(current)) {
2107 retval = -ERESTARTSYS;
2112 if (s->busy != file) {
2119 m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
2125 comedi_buf_write_free(async, n);
2131 break; /* makes device work like a pipe */
2133 set_current_state(TASK_RUNNING);
2134 remove_wait_queue(&async->wait_head, &wait);
2136 return count ? count : retval;
2139 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
2142 struct comedi_subdevice *s;
2143 struct comedi_async *async;
2144 int n, m, count = 0, retval = 0;
2145 DECLARE_WAITQUEUE(wait, current);
2146 const unsigned minor = iminor(file_inode(file));
2147 struct comedi_file_info *info = comedi_file_info_from_minor(minor);
2148 struct comedi_device *dev = comedi_dev_from_file_info(info);
2153 if (!dev->attached) {
2154 DPRINTK("no driver configured on comedi%i\n", dev->minor);
2158 s = comedi_read_subdevice(dev, minor);
2159 if (!s || !s->async)
2163 if (!s->busy || !nbytes)
2165 if (s->busy != file)
2168 add_wait_queue(&async->wait_head, &wait);
2169 while (nbytes > 0 && !retval) {
2170 set_current_state(TASK_INTERRUPTIBLE);
2174 m = comedi_buf_read_n_available(async);
2175 /* printk("%d available\n",m); */
2176 if (async->buf_read_ptr + m > async->prealloc_bufsz)
2177 m = async->prealloc_bufsz - async->buf_read_ptr;
2178 /* printk("%d contiguous\n",m); */
2183 if (!comedi_is_subdevice_running(s)) {
2184 do_become_nonbusy(dev, s);
2185 if (comedi_is_subdevice_in_error(s))
2191 if (file->f_flags & O_NONBLOCK) {
2196 if (signal_pending(current)) {
2197 retval = -ERESTARTSYS;
2204 if (s->busy != file) {
2210 m = copy_to_user(buf, async->prealloc_buf +
2211 async->buf_read_ptr, n);
2217 comedi_buf_read_alloc(async, n);
2218 comedi_buf_read_free(async, n);
2224 break; /* makes device work like a pipe */
2226 if (comedi_is_subdevice_idle(s) &&
2227 async->buf_read_count - async->buf_write_count == 0) {
2228 do_become_nonbusy(dev, s);
2230 set_current_state(TASK_RUNNING);
2231 remove_wait_queue(&async->wait_head, &wait);
2233 return count ? count : retval;
2236 static int comedi_open(struct inode *inode, struct file *file)
2238 const unsigned minor = iminor(inode);
2239 struct comedi_device *dev = comedi_dev_from_minor(minor);
2242 DPRINTK("invalid minor number\n");
2246 /* This is slightly hacky, but we want module autoloading
2248 * case: user opens device, attached -> ok
2249 * case: user opens device, unattached, !in_request_module -> autoload
2250 * case: user opens device, unattached, in_request_module -> fail
2251 * case: root opens device, attached -> ok
2252 * case: root opens device, unattached, in_request_module -> ok
2253 * (typically called from modprobe)
2254 * case: root opens device, unattached, !in_request_module -> autoload
2256 * The last could be changed to "-> ok", which would deny root
2259 mutex_lock(&dev->mutex);
2262 if (!capable(CAP_NET_ADMIN) && dev->in_request_module) {
2263 DPRINTK("in request module\n");
2264 mutex_unlock(&dev->mutex);
2267 if (capable(CAP_NET_ADMIN) && dev->in_request_module)
2270 dev->in_request_module = true;
2273 mutex_unlock(&dev->mutex);
2274 request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor);
2275 mutex_lock(&dev->mutex);
2278 dev->in_request_module = false;
2280 if (!dev->attached && !capable(CAP_NET_ADMIN)) {
2281 DPRINTK("not attached and not CAP_NET_ADMIN\n");
2282 mutex_unlock(&dev->mutex);
2286 __module_get(THIS_MODULE);
2288 if (dev->attached) {
2289 if (!try_module_get(dev->driver->module)) {
2290 module_put(THIS_MODULE);
2291 mutex_unlock(&dev->mutex);
2296 if (dev->attached && dev->use_count == 0 && dev->open) {
2297 int rc = dev->open(dev);
2299 module_put(dev->driver->module);
2300 module_put(THIS_MODULE);
2301 mutex_unlock(&dev->mutex);
2308 mutex_unlock(&dev->mutex);
2313 static int comedi_fasync(int fd, struct file *file, int on)
2315 const unsigned minor = iminor(file_inode(file));
2316 struct comedi_device *dev = comedi_dev_from_minor(minor);
2321 return fasync_helper(fd, file, on, &dev->async_queue);
2324 static int comedi_close(struct inode *inode, struct file *file)
2326 const unsigned minor = iminor(inode);
2327 struct comedi_device *dev = comedi_dev_from_minor(minor);
2328 struct comedi_subdevice *s = NULL;
2334 mutex_lock(&dev->mutex);
2336 if (dev->subdevices) {
2337 for (i = 0; i < dev->n_subdevices; i++) {
2338 s = &dev->subdevices[i];
2340 if (s->busy == file)
2342 if (s->lock == file)
2346 if (dev->attached && dev->use_count == 1 && dev->close)
2349 module_put(THIS_MODULE);
2351 module_put(dev->driver->module);
2355 mutex_unlock(&dev->mutex);
2357 if (file->f_flags & FASYNC)
2358 comedi_fasync(-1, file, 0);
2363 static const struct file_operations comedi_fops = {
2364 .owner = THIS_MODULE,
2365 .unlocked_ioctl = comedi_unlocked_ioctl,
2366 .compat_ioctl = comedi_compat_ioctl,
2367 .open = comedi_open,
2368 .release = comedi_close,
2369 .read = comedi_read,
2370 .write = comedi_write,
2371 .mmap = comedi_mmap,
2372 .poll = comedi_poll,
2373 .fasync = comedi_fasync,
2374 .llseek = noop_llseek,
2377 void comedi_error(const struct comedi_device *dev, const char *s)
2379 dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s);
2381 EXPORT_SYMBOL(comedi_error);
2383 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
2385 struct comedi_async *async = s->async;
2386 unsigned runflags = 0;
2387 unsigned runflags_mask = 0;
2389 /* DPRINTK("comedi_event 0x%x\n",mask); */
2391 if (!comedi_is_subdevice_running(s))
2395 async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR |
2396 COMEDI_CB_OVERFLOW)) {
2397 runflags_mask |= SRF_RUNNING;
2399 /* remember if an error event has occurred, so an error
2400 * can be returned the next time the user does a read() */
2401 if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) {
2402 runflags_mask |= SRF_ERROR;
2403 runflags |= SRF_ERROR;
2405 if (runflags_mask) {
2406 /*sets SRF_ERROR and SRF_RUNNING together atomically */
2407 comedi_set_subdevice_runflags(s, runflags_mask, runflags);
2410 if (async->cb_mask & s->async->events) {
2411 if (comedi_get_subdevice_runflags(s) & SRF_USER) {
2412 wake_up_interruptible(&async->wait_head);
2413 if (s->subdev_flags & SDF_CMD_READ)
2414 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
2415 if (s->subdev_flags & SDF_CMD_WRITE)
2416 kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
2419 async->cb_func(s->async->events, async->cb_arg);
2422 s->async->events = 0;
2424 EXPORT_SYMBOL(comedi_event);
2426 /* Note: the ->mutex is pre-locked on successful return */
2427 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
2429 struct comedi_file_info *info;
2430 struct comedi_device *dev;
2431 struct device *csdev;
2434 info = kzalloc(sizeof(*info), GFP_KERNEL);
2436 return ERR_PTR(-ENOMEM);
2437 dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL);
2440 return ERR_PTR(-ENOMEM);
2443 comedi_device_init(dev);
2444 comedi_set_hw_dev(dev, hardware_device);
2445 mutex_lock(&dev->mutex);
2446 mutex_lock(&comedi_board_minor_table_lock);
2447 for (i = hardware_device ? comedi_num_legacy_minors : 0;
2448 i < COMEDI_NUM_BOARD_MINORS; ++i) {
2449 if (comedi_board_minor_table[i] == NULL) {
2450 comedi_board_minor_table[i] = info;
2454 mutex_unlock(&comedi_board_minor_table_lock);
2455 if (i == COMEDI_NUM_BOARD_MINORS) {
2456 mutex_unlock(&dev->mutex);
2457 comedi_device_cleanup(dev);
2460 pr_err("comedi: error: ran out of minor numbers for board device files.\n");
2461 return ERR_PTR(-EBUSY);
2464 csdev = device_create(comedi_class, hardware_device,
2465 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
2467 dev->class_dev = csdev;
2469 /* Note: dev->mutex needs to be unlocked by the caller. */
2473 static void comedi_free_board_minor(unsigned minor)
2475 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
2476 comedi_free_board_file_info(comedi_clear_board_minor(minor));
2479 void comedi_release_hardware_device(struct device *hardware_device)
2482 struct comedi_file_info *info;
2484 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
2486 mutex_lock(&comedi_board_minor_table_lock);
2487 info = comedi_board_minor_table[minor];
2488 if (info && info->device->hw_dev == hardware_device) {
2489 comedi_board_minor_table[minor] = NULL;
2490 mutex_unlock(&comedi_board_minor_table_lock);
2491 comedi_free_board_file_info(info);
2494 mutex_unlock(&comedi_board_minor_table_lock);
2498 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
2500 struct comedi_device *dev = s->device;
2501 struct comedi_file_info *info;
2502 struct device *csdev;
2505 info = kzalloc(sizeof(*info), GFP_KERNEL);
2509 if (s->subdev_flags & SDF_CMD_READ)
2510 info->read_subdevice = s;
2511 if (s->subdev_flags & SDF_CMD_WRITE)
2512 info->write_subdevice = s;
2513 mutex_lock(&comedi_subdevice_minor_table_lock);
2514 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
2515 if (comedi_subdevice_minor_table[i] == NULL) {
2516 comedi_subdevice_minor_table[i] = info;
2520 mutex_unlock(&comedi_subdevice_minor_table_lock);
2521 if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
2523 pr_err("comedi: error: ran out of minor numbers for subdevice files.\n");
2526 i += COMEDI_NUM_BOARD_MINORS;
2528 csdev = device_create(comedi_class, dev->class_dev,
2529 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
2530 dev->minor, s->index);
2532 s->class_dev = csdev;
2537 void comedi_free_subdevice_minor(struct comedi_subdevice *s)
2539 struct comedi_file_info *info;
2546 BUG_ON(s->minor >= COMEDI_NUM_MINORS);
2547 BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
2549 info = comedi_clear_subdevice_minor(s->minor);
2551 device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
2552 s->class_dev = NULL;
2557 static void comedi_cleanup_board_minors(void)
2561 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
2562 comedi_free_board_minor(i);
2565 static int __init comedi_init(void)
2570 pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n");
2572 if (comedi_num_legacy_minors < 0 ||
2573 comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
2574 pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
2575 COMEDI_NUM_BOARD_MINORS);
2579 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2580 COMEDI_NUM_MINORS, "comedi");
2583 cdev_init(&comedi_cdev, &comedi_fops);
2584 comedi_cdev.owner = THIS_MODULE;
2585 kobject_set_name(&comedi_cdev.kobj, "comedi");
2586 if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
2587 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2591 comedi_class = class_create(THIS_MODULE, "comedi");
2592 if (IS_ERR(comedi_class)) {
2593 pr_err("comedi: failed to create class\n");
2594 cdev_del(&comedi_cdev);
2595 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2597 return PTR_ERR(comedi_class);
2600 comedi_class->dev_attrs = comedi_dev_attrs;
2602 /* XXX requires /proc interface */
2605 /* create devices files for legacy/manual use */
2606 for (i = 0; i < comedi_num_legacy_minors; i++) {
2607 struct comedi_device *dev;
2608 dev = comedi_alloc_board_minor(NULL);
2610 comedi_cleanup_board_minors();
2611 cdev_del(&comedi_cdev);
2612 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
2614 return PTR_ERR(dev);
2616 /* comedi_alloc_board_minor() locked the mutex */
2617 mutex_unlock(&dev->mutex);
2623 module_init(comedi_init);
2625 static void __exit comedi_cleanup(void)
2629 comedi_cleanup_board_minors();
2630 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
2631 BUG_ON(comedi_board_minor_table[i]);
2632 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
2633 BUG_ON(comedi_subdevice_minor_table[i]);
2635 class_destroy(comedi_class);
2636 cdev_del(&comedi_cdev);
2637 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
2639 comedi_proc_cleanup();
2641 module_exit(comedi_cleanup);
2643 MODULE_AUTHOR("http://www.comedi.org");
2644 MODULE_DESCRIPTION("Comedi core module");
2645 MODULE_LICENSE("GPL");