David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | comedi/comedi_fops.c |
| 3 | comedi kernel module |
| 4 | |
| 5 | COMEDI - Linux Control and Measurement Device Interface |
| 6 | Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org> |
| 7 | |
| 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. |
| 12 | |
| 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. |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #undef DEBUG |
| 20 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 21 | #include "comedi_compat32.h" |
| 22 | |
| 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> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 29 | #include <linux/mm.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> |
| 36 | #include <linux/fs.h> |
| 37 | #include "comedidev.h" |
| 38 | #include <linux/cdev.h> |
Frank Mori Hess | 883db3d | 2009-04-14 11:21:41 -0400 | [diff] [blame] | 39 | #include <linux/stat.h> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 40 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 41 | #include <linux/io.h> |
| 42 | #include <linux/uaccess.h> |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 43 | |
Ian Abbott | 3a5fa27 | 2012-06-19 10:17:44 +0100 | [diff] [blame] | 44 | #include "comedi_internal.h" |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 45 | |
Ian Abbott | eda5682 | 2013-04-04 14:59:02 +0100 | [diff] [blame] | 46 | #define COMEDI_NUM_MINORS 0x100 |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 47 | #define COMEDI_NUM_SUBDEVICE_MINORS \ |
| 48 | (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS) |
Ian Abbott | eda5682 | 2013-04-04 14:59:02 +0100 | [diff] [blame] | 49 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 50 | #ifdef CONFIG_COMEDI_DEBUG |
| 51 | int comedi_debug; |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 52 | EXPORT_SYMBOL_GPL(comedi_debug); |
Ian Abbott | 4d7df82 | 2012-04-13 14:12:53 +0100 | [diff] [blame] | 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)" |
| 56 | ); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 57 | #endif |
| 58 | |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 59 | static int comedi_num_legacy_minors; |
Ian Abbott | 4d7df82 | 2012-04-13 14:12:53 +0100 | [diff] [blame] | 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)" |
| 63 | ); |
| 64 | |
Ian Abbott | 234bb3c | 2012-04-13 14:12:54 +0100 | [diff] [blame] | 65 | unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB; |
Ian Abbott | 4d7df82 | 2012-04-13 14:12:53 +0100 | [diff] [blame] | 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 " |
Ian Abbott | 234bb3c | 2012-04-13 14:12:54 +0100 | [diff] [blame] | 69 | __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")"); |
Ian Abbott | 4d7df82 | 2012-04-13 14:12:53 +0100 | [diff] [blame] | 70 | |
Ian Abbott | 234bb3c | 2012-04-13 14:12:54 +0100 | [diff] [blame] | 71 | unsigned int comedi_default_buf_maxsize_kb |
| 72 | = CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB; |
Ian Abbott | 4d7df82 | 2012-04-13 14:12:53 +0100 | [diff] [blame] | 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 " |
Ian Abbott | 234bb3c | 2012-04-13 14:12:54 +0100 | [diff] [blame] | 76 | __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")"); |
Bernd Porr | 1dd33ab | 2008-12-08 23:30:13 +0000 | [diff] [blame] | 77 | |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 78 | static DEFINE_MUTEX(comedi_board_minor_table_lock); |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 79 | static struct comedi_device |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 80 | *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS]; |
| 81 | |
| 82 | static DEFINE_MUTEX(comedi_subdevice_minor_table_lock); |
| 83 | /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */ |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 84 | static struct comedi_subdevice |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 85 | *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 86 | |
Ian Abbott | d9740a0 | 2013-04-04 14:58:55 +0100 | [diff] [blame] | 87 | static struct class *comedi_class; |
| 88 | static struct cdev comedi_cdev; |
| 89 | |
| 90 | static void comedi_device_init(struct comedi_device *dev) |
| 91 | { |
| 92 | spin_lock_init(&dev->spinlock); |
| 93 | mutex_init(&dev->mutex); |
| 94 | dev->minor = -1; |
| 95 | } |
| 96 | |
| 97 | static void comedi_device_cleanup(struct comedi_device *dev) |
| 98 | { |
| 99 | struct module *driver_module = NULL; |
| 100 | |
| 101 | if (dev == NULL) |
| 102 | return; |
| 103 | mutex_lock(&dev->mutex); |
| 104 | if (dev->attached) |
| 105 | driver_module = dev->driver->module; |
| 106 | comedi_device_detach(dev); |
| 107 | while (dev->use_count > 0) { |
| 108 | if (driver_module) |
| 109 | module_put(driver_module); |
| 110 | module_put(THIS_MODULE); |
| 111 | dev->use_count--; |
| 112 | } |
| 113 | mutex_unlock(&dev->mutex); |
| 114 | mutex_destroy(&dev->mutex); |
| 115 | } |
| 116 | |
Ian Abbott | db210da | 2013-04-04 14:59:18 +0100 | [diff] [blame] | 117 | static bool comedi_clear_board_dev(struct comedi_device *dev) |
| 118 | { |
| 119 | unsigned int i = dev->minor; |
| 120 | bool cleared = false; |
| 121 | |
| 122 | mutex_lock(&comedi_board_minor_table_lock); |
| 123 | if (dev == comedi_board_minor_table[i]) { |
| 124 | comedi_board_minor_table[i] = NULL; |
| 125 | cleared = true; |
| 126 | } |
| 127 | mutex_unlock(&comedi_board_minor_table_lock); |
| 128 | return cleared; |
| 129 | } |
| 130 | |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 131 | static struct comedi_device *comedi_clear_board_minor(unsigned minor) |
Ian Abbott | d9740a0 | 2013-04-04 14:58:55 +0100 | [diff] [blame] | 132 | { |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 133 | struct comedi_device *dev; |
Ian Abbott | d9740a0 | 2013-04-04 14:58:55 +0100 | [diff] [blame] | 134 | |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 135 | mutex_lock(&comedi_board_minor_table_lock); |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 136 | dev = comedi_board_minor_table[minor]; |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 137 | comedi_board_minor_table[minor] = NULL; |
| 138 | mutex_unlock(&comedi_board_minor_table_lock); |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 139 | return dev; |
Ian Abbott | d9740a0 | 2013-04-04 14:58:55 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 142 | static void comedi_free_board_dev(struct comedi_device *dev) |
Ian Abbott | d9740a0 | 2013-04-04 14:58:55 +0100 | [diff] [blame] | 143 | { |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 144 | if (dev) { |
| 145 | if (dev->class_dev) { |
| 146 | device_destroy(comedi_class, |
| 147 | MKDEV(COMEDI_MAJOR, dev->minor)); |
Ian Abbott | d9740a0 | 2013-04-04 14:58:55 +0100 | [diff] [blame] | 148 | } |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 149 | comedi_device_cleanup(dev); |
| 150 | kfree(dev); |
Ian Abbott | d9740a0 | 2013-04-04 14:58:55 +0100 | [diff] [blame] | 151 | } |
| 152 | } |
Ian Abbott | 8ab4ed6 | 2013-04-04 14:58:54 +0100 | [diff] [blame] | 153 | |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 154 | static struct comedi_subdevice |
| 155 | *comedi_subdevice_from_minor(unsigned minor) |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 156 | { |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 157 | struct comedi_subdevice *s; |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 158 | unsigned int i = minor - COMEDI_NUM_BOARD_MINORS; |
| 159 | |
| 160 | BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS); |
| 161 | mutex_lock(&comedi_subdevice_minor_table_lock); |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 162 | s = comedi_subdevice_minor_table[i]; |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 163 | mutex_unlock(&comedi_subdevice_minor_table_lock); |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 164 | return s; |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Ian Abbott | f3abc83 | 2013-04-04 14:59:12 +0100 | [diff] [blame] | 167 | static struct comedi_device *comedi_dev_from_board_minor(unsigned minor) |
| 168 | { |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 169 | struct comedi_device *dev; |
Ian Abbott | f3abc83 | 2013-04-04 14:59:12 +0100 | [diff] [blame] | 170 | |
Ian Abbott | dac59de | 2013-04-04 14:59:14 +0100 | [diff] [blame] | 171 | BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS); |
| 172 | mutex_lock(&comedi_board_minor_table_lock); |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 173 | dev = comedi_board_minor_table[minor]; |
Ian Abbott | dac59de | 2013-04-04 14:59:14 +0100 | [diff] [blame] | 174 | mutex_unlock(&comedi_board_minor_table_lock); |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 175 | return dev; |
Ian Abbott | f3abc83 | 2013-04-04 14:59:12 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static struct comedi_device *comedi_dev_from_subdevice_minor(unsigned minor) |
| 179 | { |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 180 | struct comedi_subdevice *s; |
Ian Abbott | f3abc83 | 2013-04-04 14:59:12 +0100 | [diff] [blame] | 181 | |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 182 | s = comedi_subdevice_from_minor(minor); |
| 183 | return s ? s->device : NULL; |
Ian Abbott | f3abc83 | 2013-04-04 14:59:12 +0100 | [diff] [blame] | 184 | } |
| 185 | |
H Hartley Sweeten | 85104e9 | 2012-12-19 15:34:40 -0700 | [diff] [blame] | 186 | struct comedi_device *comedi_dev_from_minor(unsigned minor) |
| 187 | { |
Ian Abbott | f3abc83 | 2013-04-04 14:59:12 +0100 | [diff] [blame] | 188 | if (minor < COMEDI_NUM_BOARD_MINORS) |
| 189 | return comedi_dev_from_board_minor(minor); |
| 190 | else |
| 191 | return comedi_dev_from_subdevice_minor(minor); |
H Hartley Sweeten | 85104e9 | 2012-12-19 15:34:40 -0700 | [diff] [blame] | 192 | } |
| 193 | EXPORT_SYMBOL_GPL(comedi_dev_from_minor); |
| 194 | |
H Hartley Sweeten | 43bd33f | 2012-12-19 15:33:29 -0700 | [diff] [blame] | 195 | static struct comedi_subdevice * |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 196 | comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor) |
H Hartley Sweeten | 43bd33f | 2012-12-19 15:33:29 -0700 | [diff] [blame] | 197 | { |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 198 | struct comedi_subdevice *s; |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 199 | |
| 200 | if (minor >= COMEDI_NUM_BOARD_MINORS) { |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 201 | s = comedi_subdevice_from_minor(minor); |
| 202 | if (!s || s->device != dev) |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 203 | return NULL; |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 204 | if (s->subdev_flags & SDF_CMD_READ) |
| 205 | return s; |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 206 | } |
| 207 | return dev->read_subdev; |
H Hartley Sweeten | 43bd33f | 2012-12-19 15:33:29 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static struct comedi_subdevice * |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 211 | comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor) |
H Hartley Sweeten | 43bd33f | 2012-12-19 15:33:29 -0700 | [diff] [blame] | 212 | { |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 213 | struct comedi_subdevice *s; |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 214 | |
| 215 | if (minor >= COMEDI_NUM_BOARD_MINORS) { |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 216 | s = comedi_subdevice_from_minor(minor); |
| 217 | if (!s || s->device != dev) |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 218 | return NULL; |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 219 | if (s->subdev_flags & SDF_CMD_WRITE) |
| 220 | return s; |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 221 | } |
| 222 | return dev->write_subdev; |
H Hartley Sweeten | 43bd33f | 2012-12-19 15:33:29 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Frank Mori Hess | 883db3d | 2009-04-14 11:21:41 -0400 | [diff] [blame] | 225 | static int resize_async_buffer(struct comedi_device *dev, |
| 226 | struct comedi_subdevice *s, |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 227 | struct comedi_async *async, unsigned new_size) |
| 228 | { |
| 229 | int retval; |
Frank Mori Hess | 883db3d | 2009-04-14 11:21:41 -0400 | [diff] [blame] | 230 | |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 231 | if (new_size > async->max_bufsize) |
| 232 | return -EPERM; |
| 233 | |
| 234 | if (s->busy) { |
| 235 | DPRINTK("subdevice is busy, cannot resize buffer\n"); |
| 236 | return -EBUSY; |
| 237 | } |
| 238 | if (async->mmap_count) { |
| 239 | DPRINTK("subdevice is mmapped, cannot resize buffer\n"); |
| 240 | return -EBUSY; |
| 241 | } |
| 242 | |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 243 | /* make sure buffer is an integral number of pages |
| 244 | * (we round up) */ |
| 245 | new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK; |
| 246 | |
| 247 | retval = comedi_buf_alloc(dev, s, new_size); |
| 248 | if (retval < 0) |
| 249 | return retval; |
| 250 | |
| 251 | if (s->buf_change) { |
| 252 | retval = s->buf_change(dev, s, new_size); |
| 253 | if (retval < 0) |
| 254 | return retval; |
| 255 | } |
| 256 | |
| 257 | DPRINTK("comedi%i subd %d buffer resized to %i bytes\n", |
H Hartley Sweeten | 90a35c1 | 2012-12-19 17:27:02 -0700 | [diff] [blame] | 258 | dev->minor, s->index, async->prealloc_bufsz); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* sysfs attribute files */ |
| 263 | |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 264 | static ssize_t max_read_buffer_kb_show(struct device *csdev, |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 265 | struct device_attribute *attr, char *buf) |
| 266 | { |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 267 | unsigned int minor = MINOR(csdev->devt); |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 268 | struct comedi_device *dev; |
| 269 | struct comedi_subdevice *s; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 270 | unsigned int size = 0; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 271 | |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 272 | dev = comedi_dev_from_minor(minor); |
| 273 | if (!dev) |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 274 | return -ENODEV; |
| 275 | |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 276 | mutex_lock(&dev->mutex); |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 277 | s = comedi_read_subdevice(dev, minor); |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 278 | if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) |
| 279 | size = s->async->max_bufsize / 1024; |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 280 | mutex_unlock(&dev->mutex); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 281 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 282 | return snprintf(buf, PAGE_SIZE, "%i\n", size); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 285 | static ssize_t max_read_buffer_kb_store(struct device *csdev, |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 286 | struct device_attribute *attr, |
| 287 | const char *buf, size_t count) |
| 288 | { |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 289 | unsigned int minor = MINOR(csdev->devt); |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 290 | struct comedi_device *dev; |
| 291 | struct comedi_subdevice *s; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 292 | unsigned int size; |
| 293 | int err; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 294 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 295 | err = kstrtouint(buf, 10, &size); |
| 296 | if (err) |
| 297 | return err; |
| 298 | if (size > (UINT_MAX / 1024)) |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 299 | return -EINVAL; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 300 | size *= 1024; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 301 | |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 302 | dev = comedi_dev_from_minor(minor); |
| 303 | if (!dev) |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 304 | return -ENODEV; |
| 305 | |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 306 | mutex_lock(&dev->mutex); |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 307 | s = comedi_read_subdevice(dev, minor); |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 308 | if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) |
| 309 | s->async->max_bufsize = size; |
| 310 | else |
| 311 | err = -EINVAL; |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 312 | mutex_unlock(&dev->mutex); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 313 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 314 | return err ? err : count; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 315 | } |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 316 | static DEVICE_ATTR_RW(max_read_buffer_kb); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 317 | |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 318 | static ssize_t read_buffer_kb_show(struct device *csdev, |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 319 | struct device_attribute *attr, char *buf) |
| 320 | { |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 321 | unsigned int minor = MINOR(csdev->devt); |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 322 | struct comedi_device *dev; |
| 323 | struct comedi_subdevice *s; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 324 | unsigned int size = 0; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 325 | |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 326 | dev = comedi_dev_from_minor(minor); |
| 327 | if (!dev) |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 328 | return -ENODEV; |
| 329 | |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 330 | mutex_lock(&dev->mutex); |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 331 | s = comedi_read_subdevice(dev, minor); |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 332 | if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) |
| 333 | size = s->async->prealloc_bufsz / 1024; |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 334 | mutex_unlock(&dev->mutex); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 335 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 336 | return snprintf(buf, PAGE_SIZE, "%i\n", size); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 339 | static ssize_t read_buffer_kb_store(struct device *csdev, |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 340 | struct device_attribute *attr, |
| 341 | const char *buf, size_t count) |
| 342 | { |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 343 | unsigned int minor = MINOR(csdev->devt); |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 344 | struct comedi_device *dev; |
| 345 | struct comedi_subdevice *s; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 346 | unsigned int size; |
| 347 | int err; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 348 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 349 | err = kstrtouint(buf, 10, &size); |
| 350 | if (err) |
| 351 | return err; |
| 352 | if (size > (UINT_MAX / 1024)) |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 353 | return -EINVAL; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 354 | size *= 1024; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 355 | |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 356 | dev = comedi_dev_from_minor(minor); |
| 357 | if (!dev) |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 358 | return -ENODEV; |
| 359 | |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 360 | mutex_lock(&dev->mutex); |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 361 | s = comedi_read_subdevice(dev, minor); |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 362 | if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 363 | err = resize_async_buffer(dev, s, s->async, size); |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 364 | else |
| 365 | err = -EINVAL; |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 366 | mutex_unlock(&dev->mutex); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 367 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 368 | return err ? err : count; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 369 | } |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 370 | static DEVICE_ATTR_RW(read_buffer_kb); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 371 | |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 372 | static ssize_t max_write_buffer_kb_show(struct device *csdev, |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 373 | struct device_attribute *attr, |
| 374 | char *buf) |
| 375 | { |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 376 | unsigned int minor = MINOR(csdev->devt); |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 377 | struct comedi_device *dev; |
| 378 | struct comedi_subdevice *s; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 379 | unsigned int size = 0; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 380 | |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 381 | dev = comedi_dev_from_minor(minor); |
| 382 | if (!dev) |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 383 | return -ENODEV; |
| 384 | |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 385 | mutex_lock(&dev->mutex); |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 386 | s = comedi_write_subdevice(dev, minor); |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 387 | if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) |
| 388 | size = s->async->max_bufsize / 1024; |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 389 | mutex_unlock(&dev->mutex); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 390 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 391 | return snprintf(buf, PAGE_SIZE, "%i\n", size); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 394 | static ssize_t max_write_buffer_kb_store(struct device *csdev, |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 395 | struct device_attribute *attr, |
| 396 | const char *buf, size_t count) |
| 397 | { |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 398 | unsigned int minor = MINOR(csdev->devt); |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 399 | struct comedi_device *dev; |
| 400 | struct comedi_subdevice *s; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 401 | unsigned int size; |
| 402 | int err; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 403 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 404 | err = kstrtouint(buf, 10, &size); |
| 405 | if (err) |
| 406 | return err; |
| 407 | if (size > (UINT_MAX / 1024)) |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 408 | return -EINVAL; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 409 | size *= 1024; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 410 | |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 411 | dev = comedi_dev_from_minor(minor); |
| 412 | if (!dev) |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 413 | return -ENODEV; |
| 414 | |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 415 | mutex_lock(&dev->mutex); |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 416 | s = comedi_write_subdevice(dev, minor); |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 417 | if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) |
| 418 | s->async->max_bufsize = size; |
| 419 | else |
| 420 | err = -EINVAL; |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 421 | mutex_unlock(&dev->mutex); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 422 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 423 | return err ? err : count; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 424 | } |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 425 | static DEVICE_ATTR_RW(max_write_buffer_kb); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 426 | |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 427 | static ssize_t write_buffer_kb_show(struct device *csdev, |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 428 | struct device_attribute *attr, char *buf) |
| 429 | { |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 430 | unsigned int minor = MINOR(csdev->devt); |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 431 | struct comedi_device *dev; |
| 432 | struct comedi_subdevice *s; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 433 | unsigned int size = 0; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 434 | |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 435 | dev = comedi_dev_from_minor(minor); |
| 436 | if (!dev) |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 437 | return -ENODEV; |
| 438 | |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 439 | mutex_lock(&dev->mutex); |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 440 | s = comedi_write_subdevice(dev, minor); |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 441 | if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) |
| 442 | size = s->async->prealloc_bufsz / 1024; |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 443 | mutex_unlock(&dev->mutex); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 444 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 445 | return snprintf(buf, PAGE_SIZE, "%i\n", size); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 448 | static ssize_t write_buffer_kb_store(struct device *csdev, |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 449 | struct device_attribute *attr, |
| 450 | const char *buf, size_t count) |
| 451 | { |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 452 | unsigned int minor = MINOR(csdev->devt); |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 453 | struct comedi_device *dev; |
| 454 | struct comedi_subdevice *s; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 455 | unsigned int size; |
| 456 | int err; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 457 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 458 | err = kstrtouint(buf, 10, &size); |
| 459 | if (err) |
| 460 | return err; |
| 461 | if (size > (UINT_MAX / 1024)) |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 462 | return -EINVAL; |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 463 | size *= 1024; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 464 | |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 465 | dev = comedi_dev_from_minor(minor); |
| 466 | if (!dev) |
Ian Abbott | c88db46 | 2013-04-04 14:59:09 +0100 | [diff] [blame] | 467 | return -ENODEV; |
| 468 | |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 469 | mutex_lock(&dev->mutex); |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 470 | s = comedi_write_subdevice(dev, minor); |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 471 | if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 472 | err = resize_async_buffer(dev, s, s->async, size); |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 473 | else |
| 474 | err = -EINVAL; |
Ian Abbott | 7f4656c | 2013-04-04 14:59:08 +0100 | [diff] [blame] | 475 | mutex_unlock(&dev->mutex); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 476 | |
H Hartley Sweeten | 72fd9fa | 2012-06-05 10:18:02 -0700 | [diff] [blame] | 477 | return err ? err : count; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 478 | } |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 479 | static DEVICE_ATTR_RW(write_buffer_kb); |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 480 | |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 481 | static struct attribute *comedi_dev_attrs[] = { |
| 482 | &dev_attr_max_read_buffer_kb.attr, |
| 483 | &dev_attr_read_buffer_kb.attr, |
| 484 | &dev_attr_max_write_buffer_kb.attr, |
| 485 | &dev_attr_write_buffer_kb.attr, |
| 486 | NULL, |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 487 | }; |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 488 | ATTRIBUTE_GROUPS(comedi_dev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 489 | |
H Hartley Sweeten | 2aae007 | 2012-12-19 15:31:57 -0700 | [diff] [blame] | 490 | static void comedi_set_subdevice_runflags(struct comedi_subdevice *s, |
| 491 | unsigned mask, unsigned bits) |
| 492 | { |
| 493 | unsigned long flags; |
| 494 | |
| 495 | spin_lock_irqsave(&s->spin_lock, flags); |
| 496 | s->runflags &= ~mask; |
| 497 | s->runflags |= (bits & mask); |
| 498 | spin_unlock_irqrestore(&s->spin_lock, flags); |
| 499 | } |
| 500 | |
H Hartley Sweeten | ade1764 | 2012-12-19 15:43:40 -0700 | [diff] [blame] | 501 | static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s) |
H Hartley Sweeten | 7412071 | 2012-12-19 15:42:26 -0700 | [diff] [blame] | 502 | { |
| 503 | unsigned long flags; |
| 504 | unsigned runflags; |
| 505 | |
| 506 | spin_lock_irqsave(&s->spin_lock, flags); |
| 507 | runflags = s->runflags; |
| 508 | spin_unlock_irqrestore(&s->spin_lock, flags); |
| 509 | return runflags; |
| 510 | } |
H Hartley Sweeten | 7412071 | 2012-12-19 15:42:26 -0700 | [diff] [blame] | 511 | |
H Hartley Sweeten | e0dac31 | 2012-12-19 15:42:47 -0700 | [diff] [blame] | 512 | bool comedi_is_subdevice_running(struct comedi_subdevice *s) |
| 513 | { |
| 514 | unsigned runflags = comedi_get_subdevice_runflags(s); |
| 515 | |
| 516 | return (runflags & SRF_RUNNING) ? true : false; |
| 517 | } |
| 518 | EXPORT_SYMBOL_GPL(comedi_is_subdevice_running); |
| 519 | |
H Hartley Sweeten | c098c21a | 2012-12-19 15:44:02 -0700 | [diff] [blame] | 520 | static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s) |
| 521 | { |
| 522 | unsigned runflags = comedi_get_subdevice_runflags(s); |
| 523 | |
| 524 | return (runflags & SRF_ERROR) ? true : false; |
| 525 | } |
| 526 | |
H Hartley Sweeten | 9682e28 | 2012-12-19 15:44:24 -0700 | [diff] [blame] | 527 | static bool comedi_is_subdevice_idle(struct comedi_subdevice *s) |
| 528 | { |
| 529 | unsigned runflags = comedi_get_subdevice_runflags(s); |
| 530 | |
| 531 | return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true; |
| 532 | } |
| 533 | |
H Hartley Sweeten | 588ba6d | 2013-06-11 11:32:29 -0700 | [diff] [blame] | 534 | /** |
H Hartley Sweeten | 0480bcb | 2013-06-19 15:24:36 -0700 | [diff] [blame] | 535 | * comedi_alloc_spriv() - Allocate memory for the subdevice private data. |
H Hartley Sweeten | 588ba6d | 2013-06-11 11:32:29 -0700 | [diff] [blame] | 536 | * @s: comedi_subdevice struct |
H Hartley Sweeten | 0480bcb | 2013-06-19 15:24:36 -0700 | [diff] [blame] | 537 | * @size: size of the memory to allocate |
H Hartley Sweeten | 588ba6d | 2013-06-11 11:32:29 -0700 | [diff] [blame] | 538 | * |
| 539 | * This also sets the subdevice runflags to allow the core to automatically |
| 540 | * free the private data during the detach. |
| 541 | */ |
H Hartley Sweeten | 0480bcb | 2013-06-19 15:24:36 -0700 | [diff] [blame] | 542 | void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size) |
H Hartley Sweeten | 588ba6d | 2013-06-11 11:32:29 -0700 | [diff] [blame] | 543 | { |
H Hartley Sweeten | 0480bcb | 2013-06-19 15:24:36 -0700 | [diff] [blame] | 544 | s->private = kzalloc(size, GFP_KERNEL); |
| 545 | if (s->private) |
Ian Abbott | 67aa4ac | 2013-10-07 15:51:58 +0100 | [diff] [blame] | 546 | s->runflags |= SRF_FREE_SPRIV; |
H Hartley Sweeten | 0480bcb | 2013-06-19 15:24:36 -0700 | [diff] [blame] | 547 | return s->private; |
H Hartley Sweeten | 588ba6d | 2013-06-11 11:32:29 -0700 | [diff] [blame] | 548 | } |
H Hartley Sweeten | 0480bcb | 2013-06-19 15:24:36 -0700 | [diff] [blame] | 549 | EXPORT_SYMBOL_GPL(comedi_alloc_spriv); |
H Hartley Sweeten | 588ba6d | 2013-06-11 11:32:29 -0700 | [diff] [blame] | 550 | |
H Hartley Sweeten | 2aae007 | 2012-12-19 15:31:57 -0700 | [diff] [blame] | 551 | /* |
| 552 | This function restores a subdevice to an idle state. |
| 553 | */ |
| 554 | static void do_become_nonbusy(struct comedi_device *dev, |
| 555 | struct comedi_subdevice *s) |
| 556 | { |
| 557 | struct comedi_async *async = s->async; |
| 558 | |
| 559 | comedi_set_subdevice_runflags(s, SRF_RUNNING, 0); |
| 560 | if (async) { |
H Hartley Sweeten | 61c9fb0 | 2013-01-09 13:27:07 -0700 | [diff] [blame] | 561 | comedi_buf_reset(async); |
H Hartley Sweeten | 2aae007 | 2012-12-19 15:31:57 -0700 | [diff] [blame] | 562 | async->inttrig = NULL; |
| 563 | kfree(async->cmd.chanlist); |
| 564 | async->cmd.chanlist = NULL; |
| 565 | } else { |
| 566 | dev_err(dev->class_dev, |
| 567 | "BUG: (?) do_become_nonbusy called with async=NULL\n"); |
| 568 | } |
| 569 | |
| 570 | s->busy = NULL; |
| 571 | } |
| 572 | |
| 573 | static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s) |
| 574 | { |
| 575 | int ret = 0; |
| 576 | |
H Hartley Sweeten | f012463 | 2012-12-19 15:43:18 -0700 | [diff] [blame] | 577 | if (comedi_is_subdevice_running(s) && s->cancel) |
H Hartley Sweeten | 2aae007 | 2012-12-19 15:31:57 -0700 | [diff] [blame] | 578 | ret = s->cancel(dev, s); |
| 579 | |
| 580 | do_become_nonbusy(dev, s); |
| 581 | |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | static int is_device_busy(struct comedi_device *dev) |
| 586 | { |
| 587 | struct comedi_subdevice *s; |
| 588 | int i; |
| 589 | |
| 590 | if (!dev->attached) |
| 591 | return 0; |
| 592 | |
| 593 | for (i = 0; i < dev->n_subdevices; i++) { |
| 594 | s = &dev->subdevices[i]; |
| 595 | if (s->busy) |
| 596 | return 1; |
| 597 | if (s->async && s->async->mmap_count) |
| 598 | return 1; |
| 599 | } |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 604 | /* |
| 605 | COMEDI_DEVCONFIG |
| 606 | device config ioctl |
| 607 | |
| 608 | arg: |
| 609 | pointer to devconfig structure |
| 610 | |
| 611 | reads: |
| 612 | devconfig structure at arg |
| 613 | |
| 614 | writes: |
| 615 | none |
| 616 | */ |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 617 | static int do_devconfig_ioctl(struct comedi_device *dev, |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 618 | struct comedi_devconfig __user *arg) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 619 | { |
Bill Pemberton | 0707bb0 | 2009-03-16 22:06:20 -0400 | [diff] [blame] | 620 | struct comedi_devconfig it; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 621 | |
| 622 | if (!capable(CAP_SYS_ADMIN)) |
| 623 | return -EPERM; |
| 624 | |
| 625 | if (arg == NULL) { |
| 626 | if (is_device_busy(dev)) |
| 627 | return -EBUSY; |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 628 | if (dev->attached) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 629 | struct module *driver_module = dev->driver->module; |
| 630 | comedi_device_detach(dev); |
| 631 | module_put(driver_module); |
| 632 | } |
| 633 | return 0; |
| 634 | } |
| 635 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 636 | if (copy_from_user(&it, arg, sizeof(it))) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 637 | return -EFAULT; |
| 638 | |
| 639 | it.board_name[COMEDI_NAMELEN - 1] = 0; |
| 640 | |
H Hartley Sweeten | d184313 | 2013-01-09 09:46:10 -0700 | [diff] [blame] | 641 | if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) { |
| 642 | dev_warn(dev->class_dev, |
| 643 | "comedi_config --init_data is deprecated\n"); |
| 644 | return -EINVAL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Ian Abbott | 8ab4ed6 | 2013-04-04 14:58:54 +0100 | [diff] [blame] | 647 | if (dev->minor >= comedi_num_legacy_minors) |
| 648 | /* don't re-use dynamically allocated comedi devices */ |
| 649 | return -EBUSY; |
| 650 | |
Ian Abbott | b2a644b | 2013-04-04 14:58:56 +0100 | [diff] [blame] | 651 | /* This increments the driver module count on success. */ |
| 652 | return comedi_device_attach(dev, &it); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /* |
| 656 | COMEDI_BUFCONFIG |
| 657 | buffer configuration ioctl |
| 658 | |
| 659 | arg: |
| 660 | pointer to bufconfig structure |
| 661 | |
| 662 | reads: |
| 663 | bufconfig at arg |
| 664 | |
| 665 | writes: |
| 666 | modified bufconfig at arg |
| 667 | |
| 668 | */ |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 669 | static int do_bufconfig_ioctl(struct comedi_device *dev, |
| 670 | struct comedi_bufconfig __user *arg) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 671 | { |
Bill Pemberton | be6aba4 | 2009-03-16 22:06:37 -0400 | [diff] [blame] | 672 | struct comedi_bufconfig bc; |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 673 | struct comedi_async *async; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 674 | struct comedi_subdevice *s; |
Frank Mori Hess | 883db3d | 2009-04-14 11:21:41 -0400 | [diff] [blame] | 675 | int retval = 0; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 676 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 677 | if (copy_from_user(&bc, arg, sizeof(bc))) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 678 | return -EFAULT; |
| 679 | |
Güngör Erseymen | 11f80dd | 2013-06-07 21:29:49 +0300 | [diff] [blame] | 680 | if (bc.subdevice >= dev->n_subdevices) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 681 | return -EINVAL; |
| 682 | |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 683 | s = &dev->subdevices[bc.subdevice]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 684 | async = s->async; |
| 685 | |
| 686 | if (!async) { |
| 687 | DPRINTK("subdevice does not have async capability\n"); |
| 688 | bc.size = 0; |
| 689 | bc.maximum_size = 0; |
| 690 | goto copyback; |
| 691 | } |
| 692 | |
| 693 | if (bc.maximum_size) { |
| 694 | if (!capable(CAP_SYS_ADMIN)) |
| 695 | return -EPERM; |
| 696 | |
| 697 | async->max_bufsize = bc.maximum_size; |
| 698 | } |
| 699 | |
| 700 | if (bc.size) { |
Frank Mori Hess | 883db3d | 2009-04-14 11:21:41 -0400 | [diff] [blame] | 701 | retval = resize_async_buffer(dev, s, async, bc.size); |
| 702 | if (retval < 0) |
| 703 | return retval; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | bc.size = async->prealloc_bufsz; |
| 707 | bc.maximum_size = async->max_bufsize; |
| 708 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 709 | copyback: |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 710 | if (copy_to_user(arg, &bc, sizeof(bc))) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 711 | return -EFAULT; |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | COMEDI_DEVINFO |
| 718 | device info ioctl |
| 719 | |
| 720 | arg: |
| 721 | pointer to devinfo structure |
| 722 | |
| 723 | reads: |
| 724 | none |
| 725 | |
| 726 | writes: |
| 727 | devinfo structure |
| 728 | |
| 729 | */ |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 730 | static int do_devinfo_ioctl(struct comedi_device *dev, |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 731 | struct comedi_devinfo __user *arg, |
| 732 | struct file *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 733 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 734 | const unsigned minor = iminor(file_inode(file)); |
H Hartley Sweeten | 0e70092 | 2012-12-19 15:39:18 -0700 | [diff] [blame] | 735 | struct comedi_subdevice *s; |
| 736 | struct comedi_devinfo devinfo; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 737 | |
| 738 | memset(&devinfo, 0, sizeof(devinfo)); |
| 739 | |
| 740 | /* fill devinfo structure */ |
| 741 | devinfo.version_code = COMEDI_VERSION_CODE; |
| 742 | devinfo.n_subdevs = dev->n_subdevices; |
Vasiliy Kulikov | 819cbb1 | 2011-06-26 12:56:22 +0400 | [diff] [blame] | 743 | strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN); |
| 744 | strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 745 | |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 746 | s = comedi_read_subdevice(dev, minor); |
H Hartley Sweeten | 0e70092 | 2012-12-19 15:39:18 -0700 | [diff] [blame] | 747 | if (s) |
H Hartley Sweeten | 90a35c1 | 2012-12-19 17:27:02 -0700 | [diff] [blame] | 748 | devinfo.read_subdevice = s->index; |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 749 | else |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 750 | devinfo.read_subdevice = -1; |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 751 | |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 752 | s = comedi_write_subdevice(dev, minor); |
H Hartley Sweeten | 0e70092 | 2012-12-19 15:39:18 -0700 | [diff] [blame] | 753 | if (s) |
H Hartley Sweeten | 90a35c1 | 2012-12-19 17:27:02 -0700 | [diff] [blame] | 754 | devinfo.write_subdevice = s->index; |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 755 | else |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 756 | devinfo.write_subdevice = -1; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 757 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 758 | if (copy_to_user(arg, &devinfo, sizeof(devinfo))) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 759 | return -EFAULT; |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | COMEDI_SUBDINFO |
| 766 | subdevice info ioctl |
| 767 | |
| 768 | arg: |
| 769 | pointer to array of subdevice info structures |
| 770 | |
| 771 | reads: |
| 772 | none |
| 773 | |
| 774 | writes: |
| 775 | array of subdevice info structures at arg |
| 776 | |
| 777 | */ |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 778 | static int do_subdinfo_ioctl(struct comedi_device *dev, |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 779 | struct comedi_subdinfo __user *arg, void *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 780 | { |
| 781 | int ret, i; |
Bill Pemberton | bd52efb | 2009-03-16 22:06:09 -0400 | [diff] [blame] | 782 | struct comedi_subdinfo *tmp, *us; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 783 | struct comedi_subdevice *s; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 784 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 785 | tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 786 | if (!tmp) |
| 787 | return -ENOMEM; |
| 788 | |
| 789 | /* fill subdinfo structs */ |
| 790 | for (i = 0; i < dev->n_subdevices; i++) { |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 791 | s = &dev->subdevices[i]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 792 | us = tmp + i; |
| 793 | |
| 794 | us->type = s->type; |
| 795 | us->n_chan = s->n_chan; |
| 796 | us->subd_flags = s->subdev_flags; |
H Hartley Sweeten | f012463 | 2012-12-19 15:43:18 -0700 | [diff] [blame] | 797 | if (comedi_is_subdevice_running(s)) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 798 | us->subd_flags |= SDF_RUNNING; |
| 799 | #define TIMER_nanosec 5 /* backwards compatibility */ |
| 800 | us->timer_type = TIMER_nanosec; |
| 801 | us->len_chanlist = s->len_chanlist; |
| 802 | us->maxdata = s->maxdata; |
| 803 | if (s->range_table) { |
| 804 | us->range_type = |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 805 | (i << 24) | (0 << 16) | (s->range_table->length); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 806 | } else { |
| 807 | us->range_type = 0; /* XXX */ |
| 808 | } |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 809 | |
| 810 | if (s->busy) |
| 811 | us->subd_flags |= SDF_BUSY; |
| 812 | if (s->busy == file) |
| 813 | us->subd_flags |= SDF_BUSY_OWNER; |
| 814 | if (s->lock) |
| 815 | us->subd_flags |= SDF_LOCKED; |
| 816 | if (s->lock == file) |
| 817 | us->subd_flags |= SDF_LOCK_OWNER; |
| 818 | if (!s->maxdata && s->maxdata_list) |
| 819 | us->subd_flags |= SDF_MAXDATA; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 820 | if (s->range_table_list) |
| 821 | us->subd_flags |= SDF_RANGETYPE; |
| 822 | if (s->do_cmd) |
| 823 | us->subd_flags |= SDF_CMD; |
| 824 | |
| 825 | if (s->insn_bits != &insn_inval) |
| 826 | us->insn_bits_support = COMEDI_SUPPORTED; |
| 827 | else |
| 828 | us->insn_bits_support = COMEDI_UNSUPPORTED; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 829 | } |
| 830 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 831 | ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp)); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 832 | |
| 833 | kfree(tmp); |
| 834 | |
| 835 | return ret ? -EFAULT : 0; |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | COMEDI_CHANINFO |
| 840 | subdevice info ioctl |
| 841 | |
| 842 | arg: |
| 843 | pointer to chaninfo structure |
| 844 | |
| 845 | reads: |
| 846 | chaninfo structure at arg |
| 847 | |
| 848 | writes: |
| 849 | arrays at elements of chaninfo structure |
| 850 | |
| 851 | */ |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 852 | static int do_chaninfo_ioctl(struct comedi_device *dev, |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 853 | struct comedi_chaninfo __user *arg) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 854 | { |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 855 | struct comedi_subdevice *s; |
Bill Pemberton | a18b416 | 2009-03-16 22:06:04 -0400 | [diff] [blame] | 856 | struct comedi_chaninfo it; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 857 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 858 | if (copy_from_user(&it, arg, sizeof(it))) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 859 | return -EFAULT; |
| 860 | |
| 861 | if (it.subdev >= dev->n_subdevices) |
| 862 | return -EINVAL; |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 863 | s = &dev->subdevices[it.subdev]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 864 | |
| 865 | if (it.maxdata_list) { |
| 866 | if (s->maxdata || !s->maxdata_list) |
| 867 | return -EINVAL; |
| 868 | if (copy_to_user(it.maxdata_list, s->maxdata_list, |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 869 | s->n_chan * sizeof(unsigned int))) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 870 | return -EFAULT; |
| 871 | } |
| 872 | |
Ian Abbott | 64d9b1d | 2013-10-07 16:50:05 +0100 | [diff] [blame] | 873 | if (it.flaglist) |
| 874 | return -EINVAL; /* flaglist not supported */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 875 | |
| 876 | if (it.rangelist) { |
| 877 | int i; |
| 878 | |
| 879 | if (!s->range_table_list) |
| 880 | return -EINVAL; |
| 881 | for (i = 0; i < s->n_chan; i++) { |
| 882 | int x; |
| 883 | |
| 884 | x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 885 | (s->range_table_list[i]->length); |
Vasiliy Kulikov | 81604d4 | 2010-09-05 22:32:33 +0400 | [diff] [blame] | 886 | if (put_user(x, it.rangelist + i)) |
| 887 | return -EFAULT; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 888 | } |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 889 | #if 0 |
| 890 | if (copy_to_user(it.rangelist, s->range_type_list, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 891 | s->n_chan * sizeof(unsigned int))) |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 892 | return -EFAULT; |
| 893 | #endif |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | COMEDI_BUFINFO |
| 901 | buffer information ioctl |
| 902 | |
| 903 | arg: |
| 904 | pointer to bufinfo structure |
| 905 | |
| 906 | reads: |
| 907 | bufinfo at arg |
| 908 | |
| 909 | writes: |
| 910 | modified bufinfo at arg |
| 911 | |
| 912 | */ |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 913 | static int do_bufinfo_ioctl(struct comedi_device *dev, |
Ian Abbott | 53fa827 | 2010-05-19 18:09:50 +0100 | [diff] [blame] | 914 | struct comedi_bufinfo __user *arg, void *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 915 | { |
Bill Pemberton | 9aa5339 | 2009-03-16 22:06:42 -0400 | [diff] [blame] | 916 | struct comedi_bufinfo bi; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 917 | struct comedi_subdevice *s; |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 918 | struct comedi_async *async; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 919 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 920 | if (copy_from_user(&bi, arg, sizeof(bi))) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 921 | return -EFAULT; |
| 922 | |
Güngör Erseymen | 7b1a5e2 | 2013-06-07 21:29:50 +0300 | [diff] [blame] | 923 | if (bi.subdevice >= dev->n_subdevices) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 924 | return -EINVAL; |
| 925 | |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 926 | s = &dev->subdevices[bi.subdevice]; |
Ian Abbott | 53fa827 | 2010-05-19 18:09:50 +0100 | [diff] [blame] | 927 | |
| 928 | if (s->lock && s->lock != file) |
| 929 | return -EACCES; |
| 930 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 931 | async = s->async; |
| 932 | |
| 933 | if (!async) { |
| 934 | DPRINTK("subdevice does not have async capability\n"); |
| 935 | bi.buf_write_ptr = 0; |
| 936 | bi.buf_read_ptr = 0; |
| 937 | bi.buf_write_count = 0; |
| 938 | bi.buf_read_count = 0; |
Ian Abbott | 4772c01 | 2010-05-19 18:09:49 +0100 | [diff] [blame] | 939 | bi.bytes_read = 0; |
| 940 | bi.bytes_written = 0; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 941 | goto copyback; |
| 942 | } |
Ian Abbott | 53fa827 | 2010-05-19 18:09:50 +0100 | [diff] [blame] | 943 | if (!s->busy) { |
| 944 | bi.bytes_read = 0; |
| 945 | bi.bytes_written = 0; |
| 946 | goto copyback_position; |
| 947 | } |
| 948 | if (s->busy != file) |
| 949 | return -EACCES; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 950 | |
| 951 | if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) { |
| 952 | bi.bytes_read = comedi_buf_read_alloc(async, bi.bytes_read); |
| 953 | comedi_buf_read_free(async, bi.bytes_read); |
| 954 | |
H Hartley Sweeten | 9682e28 | 2012-12-19 15:44:24 -0700 | [diff] [blame] | 955 | if (comedi_is_subdevice_idle(s) && |
| 956 | async->buf_write_count == async->buf_read_count) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 957 | do_become_nonbusy(dev, s); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) { |
| 962 | bi.bytes_written = |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 963 | comedi_buf_write_alloc(async, bi.bytes_written); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 964 | comedi_buf_write_free(async, bi.bytes_written); |
| 965 | } |
| 966 | |
Ian Abbott | 53fa827 | 2010-05-19 18:09:50 +0100 | [diff] [blame] | 967 | copyback_position: |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 968 | bi.buf_write_count = async->buf_write_count; |
| 969 | bi.buf_write_ptr = async->buf_write_ptr; |
| 970 | bi.buf_read_count = async->buf_read_count; |
| 971 | bi.buf_read_ptr = async->buf_read_ptr; |
| 972 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 973 | copyback: |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 974 | if (copy_to_user(arg, &bi, sizeof(bi))) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 975 | return -EFAULT; |
| 976 | |
| 977 | return 0; |
| 978 | } |
| 979 | |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 980 | static int check_insn_config_length(struct comedi_insn *insn, |
| 981 | unsigned int *data) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 982 | { |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 983 | if (insn->n < 1) |
| 984 | return -EINVAL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 985 | |
| 986 | switch (data[0]) { |
| 987 | case INSN_CONFIG_DIO_OUTPUT: |
| 988 | case INSN_CONFIG_DIO_INPUT: |
| 989 | case INSN_CONFIG_DISARM: |
| 990 | case INSN_CONFIG_RESET: |
| 991 | if (insn->n == 1) |
| 992 | return 0; |
| 993 | break; |
| 994 | case INSN_CONFIG_ARM: |
| 995 | case INSN_CONFIG_DIO_QUERY: |
| 996 | case INSN_CONFIG_BLOCK_SIZE: |
| 997 | case INSN_CONFIG_FILTER: |
| 998 | case INSN_CONFIG_SERIAL_CLOCK: |
| 999 | case INSN_CONFIG_BIDIRECTIONAL_DATA: |
| 1000 | case INSN_CONFIG_ALT_SOURCE: |
| 1001 | case INSN_CONFIG_SET_COUNTER_MODE: |
| 1002 | case INSN_CONFIG_8254_READ_STATUS: |
| 1003 | case INSN_CONFIG_SET_ROUTING: |
| 1004 | case INSN_CONFIG_GET_ROUTING: |
| 1005 | case INSN_CONFIG_GET_PWM_STATUS: |
| 1006 | case INSN_CONFIG_PWM_SET_PERIOD: |
| 1007 | case INSN_CONFIG_PWM_GET_PERIOD: |
| 1008 | if (insn->n == 2) |
| 1009 | return 0; |
| 1010 | break; |
| 1011 | case INSN_CONFIG_SET_GATE_SRC: |
| 1012 | case INSN_CONFIG_GET_GATE_SRC: |
| 1013 | case INSN_CONFIG_SET_CLOCK_SRC: |
| 1014 | case INSN_CONFIG_GET_CLOCK_SRC: |
| 1015 | case INSN_CONFIG_SET_OTHER_SRC: |
| 1016 | case INSN_CONFIG_GET_COUNTER_STATUS: |
| 1017 | case INSN_CONFIG_PWM_SET_H_BRIDGE: |
| 1018 | case INSN_CONFIG_PWM_GET_H_BRIDGE: |
| 1019 | case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE: |
| 1020 | if (insn->n == 3) |
| 1021 | return 0; |
| 1022 | break; |
| 1023 | case INSN_CONFIG_PWM_OUTPUT: |
| 1024 | case INSN_CONFIG_ANALOG_TRIG: |
| 1025 | if (insn->n == 5) |
| 1026 | return 0; |
| 1027 | break; |
Ian Abbott | b0a2b6d | 2012-11-14 11:22:57 +0000 | [diff] [blame] | 1028 | case INSN_CONFIG_DIGITAL_TRIG: |
| 1029 | if (insn->n == 6) |
| 1030 | return 0; |
| 1031 | break; |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 1032 | /* by default we allow the insn since we don't have checks for |
| 1033 | * all possible cases yet */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1034 | default: |
Ian Abbott | 4f870fe | 2012-08-16 14:38:05 +0100 | [diff] [blame] | 1035 | pr_warn("comedi: No check for data length of config insn id %i is implemented.\n", |
| 1036 | data[0]); |
| 1037 | pr_warn("comedi: Add a check to %s in %s.\n", |
| 1038 | __func__, __FILE__); |
| 1039 | pr_warn("comedi: Assuming n=%i is correct.\n", insn->n); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1040 | return 0; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1041 | } |
| 1042 | return -EINVAL; |
| 1043 | } |
| 1044 | |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 1045 | static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn, |
| 1046 | unsigned int *data, void *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1047 | { |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 1048 | struct comedi_subdevice *s; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1049 | int ret = 0; |
| 1050 | int i; |
| 1051 | |
| 1052 | if (insn->insn & INSN_MASK_SPECIAL) { |
| 1053 | /* a non-subdevice instruction */ |
| 1054 | |
| 1055 | switch (insn->insn) { |
| 1056 | case INSN_GTOD: |
| 1057 | { |
| 1058 | struct timeval tv; |
| 1059 | |
| 1060 | if (insn->n != 2) { |
| 1061 | ret = -EINVAL; |
| 1062 | break; |
| 1063 | } |
| 1064 | |
| 1065 | do_gettimeofday(&tv); |
| 1066 | data[0] = tv.tv_sec; |
| 1067 | data[1] = tv.tv_usec; |
| 1068 | ret = 2; |
| 1069 | |
| 1070 | break; |
| 1071 | } |
| 1072 | case INSN_WAIT: |
| 1073 | if (insn->n != 1 || data[0] >= 100000) { |
| 1074 | ret = -EINVAL; |
| 1075 | break; |
| 1076 | } |
| 1077 | udelay(data[0] / 1000); |
| 1078 | ret = 1; |
| 1079 | break; |
| 1080 | case INSN_INTTRIG: |
| 1081 | if (insn->n != 1) { |
| 1082 | ret = -EINVAL; |
| 1083 | break; |
| 1084 | } |
| 1085 | if (insn->subdev >= dev->n_subdevices) { |
| 1086 | DPRINTK("%d not usable subdevice\n", |
| 1087 | insn->subdev); |
| 1088 | ret = -EINVAL; |
| 1089 | break; |
| 1090 | } |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 1091 | s = &dev->subdevices[insn->subdev]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1092 | if (!s->async) { |
| 1093 | DPRINTK("no async\n"); |
| 1094 | ret = -EINVAL; |
| 1095 | break; |
| 1096 | } |
| 1097 | if (!s->async->inttrig) { |
| 1098 | DPRINTK("no inttrig\n"); |
| 1099 | ret = -EAGAIN; |
| 1100 | break; |
| 1101 | } |
Ian Abbott | 5d06e3d | 2012-09-18 19:46:58 +0100 | [diff] [blame] | 1102 | ret = s->async->inttrig(dev, s, data[0]); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1103 | if (ret >= 0) |
| 1104 | ret = 1; |
| 1105 | break; |
| 1106 | default: |
| 1107 | DPRINTK("invalid insn\n"); |
| 1108 | ret = -EINVAL; |
| 1109 | break; |
| 1110 | } |
| 1111 | } else { |
| 1112 | /* a subdevice instruction */ |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 1113 | unsigned int maxdata; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1114 | |
| 1115 | if (insn->subdev >= dev->n_subdevices) { |
| 1116 | DPRINTK("subdevice %d out of range\n", insn->subdev); |
| 1117 | ret = -EINVAL; |
| 1118 | goto out; |
| 1119 | } |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 1120 | s = &dev->subdevices[insn->subdev]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1121 | |
| 1122 | if (s->type == COMEDI_SUBD_UNUSED) { |
| 1123 | DPRINTK("%d not usable subdevice\n", insn->subdev); |
| 1124 | ret = -EIO; |
| 1125 | goto out; |
| 1126 | } |
| 1127 | |
| 1128 | /* are we locked? (ioctl lock) */ |
| 1129 | if (s->lock && s->lock != file) { |
| 1130 | DPRINTK("device locked\n"); |
| 1131 | ret = -EACCES; |
| 1132 | goto out; |
| 1133 | } |
| 1134 | |
Greg Kroah-Hartman | 0fd0ca7 | 2010-05-01 12:33:17 -0700 | [diff] [blame] | 1135 | ret = comedi_check_chanlist(s, 1, &insn->chanspec); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1136 | if (ret < 0) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1137 | ret = -EINVAL; |
| 1138 | DPRINTK("bad chanspec\n"); |
| 1139 | goto out; |
| 1140 | } |
| 1141 | |
| 1142 | if (s->busy) { |
| 1143 | ret = -EBUSY; |
| 1144 | goto out; |
| 1145 | } |
| 1146 | /* This looks arbitrary. It is. */ |
| 1147 | s->busy = &parse_insn; |
| 1148 | switch (insn->insn) { |
| 1149 | case INSN_READ: |
| 1150 | ret = s->insn_read(dev, s, insn, data); |
| 1151 | break; |
| 1152 | case INSN_WRITE: |
| 1153 | maxdata = s->maxdata_list |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1154 | ? s->maxdata_list[CR_CHAN(insn->chanspec)] |
| 1155 | : s->maxdata; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1156 | for (i = 0; i < insn->n; ++i) { |
| 1157 | if (data[i] > maxdata) { |
| 1158 | ret = -EINVAL; |
| 1159 | DPRINTK("bad data value(s)\n"); |
| 1160 | break; |
| 1161 | } |
| 1162 | } |
| 1163 | if (ret == 0) |
| 1164 | ret = s->insn_write(dev, s, insn, data); |
| 1165 | break; |
| 1166 | case INSN_BITS: |
| 1167 | if (insn->n != 2) { |
| 1168 | ret = -EINVAL; |
Ian Abbott | 2f644cc | 2011-01-18 17:44:33 +0000 | [diff] [blame] | 1169 | } else { |
| 1170 | /* Most drivers ignore the base channel in |
| 1171 | * insn->chanspec. Fix this here if |
| 1172 | * the subdevice has <= 32 channels. */ |
| 1173 | unsigned int shift; |
| 1174 | unsigned int orig_mask; |
| 1175 | |
| 1176 | orig_mask = data[0]; |
| 1177 | if (s->n_chan <= 32) { |
| 1178 | shift = CR_CHAN(insn->chanspec); |
| 1179 | if (shift > 0) { |
| 1180 | insn->chanspec = 0; |
| 1181 | data[0] <<= shift; |
| 1182 | data[1] <<= shift; |
| 1183 | } |
| 1184 | } else |
| 1185 | shift = 0; |
| 1186 | ret = s->insn_bits(dev, s, insn, data); |
| 1187 | data[0] = orig_mask; |
| 1188 | if (shift > 0) |
| 1189 | data[1] >>= shift; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1190 | } |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1191 | break; |
| 1192 | case INSN_CONFIG: |
| 1193 | ret = check_insn_config_length(insn, data); |
| 1194 | if (ret) |
| 1195 | break; |
| 1196 | ret = s->insn_config(dev, s, insn, data); |
| 1197 | break; |
| 1198 | default: |
| 1199 | ret = -EINVAL; |
| 1200 | break; |
| 1201 | } |
| 1202 | |
| 1203 | s->busy = NULL; |
| 1204 | } |
| 1205 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1206 | out: |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1207 | return ret; |
| 1208 | } |
| 1209 | |
| 1210 | /* |
H Hartley Sweeten | 5b6cbd8 | 2013-01-21 14:02:59 -0700 | [diff] [blame] | 1211 | * COMEDI_INSNLIST |
| 1212 | * synchronous instructions |
| 1213 | * |
| 1214 | * arg: |
| 1215 | * pointer to sync cmd structure |
| 1216 | * |
| 1217 | * reads: |
| 1218 | * sync cmd struct at arg |
| 1219 | * instruction list |
| 1220 | * data (for writes) |
| 1221 | * |
| 1222 | * writes: |
| 1223 | * data (for reads) |
| 1224 | */ |
| 1225 | /* arbitrary limits */ |
| 1226 | #define MAX_SAMPLES 256 |
| 1227 | static int do_insnlist_ioctl(struct comedi_device *dev, |
| 1228 | struct comedi_insnlist __user *arg, void *file) |
| 1229 | { |
| 1230 | struct comedi_insnlist insnlist; |
| 1231 | struct comedi_insn *insns = NULL; |
| 1232 | unsigned int *data = NULL; |
| 1233 | int i = 0; |
| 1234 | int ret = 0; |
| 1235 | |
| 1236 | if (copy_from_user(&insnlist, arg, sizeof(insnlist))) |
| 1237 | return -EFAULT; |
| 1238 | |
| 1239 | data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL); |
| 1240 | if (!data) { |
| 1241 | DPRINTK("kmalloc failed\n"); |
| 1242 | ret = -ENOMEM; |
| 1243 | goto error; |
| 1244 | } |
| 1245 | |
| 1246 | insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL); |
| 1247 | if (!insns) { |
| 1248 | DPRINTK("kmalloc failed\n"); |
| 1249 | ret = -ENOMEM; |
| 1250 | goto error; |
| 1251 | } |
| 1252 | |
| 1253 | if (copy_from_user(insns, insnlist.insns, |
| 1254 | sizeof(*insns) * insnlist.n_insns)) { |
| 1255 | DPRINTK("copy_from_user failed\n"); |
| 1256 | ret = -EFAULT; |
| 1257 | goto error; |
| 1258 | } |
| 1259 | |
| 1260 | for (i = 0; i < insnlist.n_insns; i++) { |
| 1261 | if (insns[i].n > MAX_SAMPLES) { |
| 1262 | DPRINTK("number of samples too large\n"); |
| 1263 | ret = -EINVAL; |
| 1264 | goto error; |
| 1265 | } |
| 1266 | if (insns[i].insn & INSN_MASK_WRITE) { |
| 1267 | if (copy_from_user(data, insns[i].data, |
| 1268 | insns[i].n * sizeof(unsigned int))) { |
| 1269 | DPRINTK("copy_from_user failed\n"); |
| 1270 | ret = -EFAULT; |
| 1271 | goto error; |
| 1272 | } |
| 1273 | } |
| 1274 | ret = parse_insn(dev, insns + i, data, file); |
| 1275 | if (ret < 0) |
| 1276 | goto error; |
| 1277 | if (insns[i].insn & INSN_MASK_READ) { |
| 1278 | if (copy_to_user(insns[i].data, data, |
| 1279 | insns[i].n * sizeof(unsigned int))) { |
| 1280 | DPRINTK("copy_to_user failed\n"); |
| 1281 | ret = -EFAULT; |
| 1282 | goto error; |
| 1283 | } |
| 1284 | } |
| 1285 | if (need_resched()) |
| 1286 | schedule(); |
| 1287 | } |
| 1288 | |
| 1289 | error: |
| 1290 | kfree(insns); |
| 1291 | kfree(data); |
| 1292 | |
| 1293 | if (ret < 0) |
| 1294 | return ret; |
| 1295 | return i; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
Pieter De Praetere | 20617f2 | 2010-03-10 09:47:44 +0100 | [diff] [blame] | 1299 | * COMEDI_INSN |
| 1300 | * synchronous instructions |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1301 | * |
Pieter De Praetere | 20617f2 | 2010-03-10 09:47:44 +0100 | [diff] [blame] | 1302 | * arg: |
| 1303 | * pointer to insn |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1304 | * |
Pieter De Praetere | 20617f2 | 2010-03-10 09:47:44 +0100 | [diff] [blame] | 1305 | * reads: |
| 1306 | * struct comedi_insn struct at arg |
| 1307 | * data (for writes) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1308 | * |
Pieter De Praetere | 20617f2 | 2010-03-10 09:47:44 +0100 | [diff] [blame] | 1309 | * writes: |
| 1310 | * data (for reads) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1311 | */ |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 1312 | static int do_insn_ioctl(struct comedi_device *dev, |
| 1313 | struct comedi_insn __user *arg, void *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1314 | { |
Bill Pemberton | 90035c0 | 2009-03-16 22:05:53 -0400 | [diff] [blame] | 1315 | struct comedi_insn insn; |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 1316 | unsigned int *data = NULL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1317 | int ret = 0; |
| 1318 | |
Bill Pemberton | 790c554 | 2009-03-16 22:05:02 -0400 | [diff] [blame] | 1319 | data = kmalloc(sizeof(unsigned int) * MAX_SAMPLES, GFP_KERNEL); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1320 | if (!data) { |
| 1321 | ret = -ENOMEM; |
| 1322 | goto error; |
| 1323 | } |
| 1324 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 1325 | if (copy_from_user(&insn, arg, sizeof(insn))) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1326 | ret = -EFAULT; |
| 1327 | goto error; |
| 1328 | } |
| 1329 | |
| 1330 | /* This is where the behavior of insn and insnlist deviate. */ |
| 1331 | if (insn.n > MAX_SAMPLES) |
| 1332 | insn.n = MAX_SAMPLES; |
| 1333 | if (insn.insn & INSN_MASK_WRITE) { |
Mark | 21fe2ee | 2010-05-13 17:44:39 +0800 | [diff] [blame] | 1334 | if (copy_from_user(data, |
| 1335 | insn.data, |
| 1336 | insn.n * sizeof(unsigned int))) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1337 | ret = -EFAULT; |
| 1338 | goto error; |
| 1339 | } |
| 1340 | } |
| 1341 | ret = parse_insn(dev, &insn, data, file); |
| 1342 | if (ret < 0) |
| 1343 | goto error; |
| 1344 | if (insn.insn & INSN_MASK_READ) { |
Mark | 21fe2ee | 2010-05-13 17:44:39 +0800 | [diff] [blame] | 1345 | if (copy_to_user(insn.data, |
| 1346 | data, |
| 1347 | insn.n * sizeof(unsigned int))) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1348 | ret = -EFAULT; |
| 1349 | goto error; |
| 1350 | } |
| 1351 | } |
| 1352 | ret = insn.n; |
| 1353 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1354 | error: |
| 1355 | kfree(data); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1356 | |
| 1357 | return ret; |
| 1358 | } |
| 1359 | |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 1360 | static int do_cmd_ioctl(struct comedi_device *dev, |
H Hartley Sweeten | cbe01f7 | 2012-09-18 11:41:54 -0700 | [diff] [blame] | 1361 | struct comedi_cmd __user *arg, void *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1362 | { |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1363 | struct comedi_cmd cmd; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 1364 | struct comedi_subdevice *s; |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 1365 | struct comedi_async *async; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1366 | int ret = 0; |
H Hartley Sweeten | 95bc359 | 2012-09-18 11:43:52 -0700 | [diff] [blame] | 1367 | unsigned int __user *user_chanlist; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1368 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 1369 | if (copy_from_user(&cmd, arg, sizeof(cmd))) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1370 | DPRINTK("bad cmd address\n"); |
| 1371 | return -EFAULT; |
| 1372 | } |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1373 | /* save user's chanlist pointer so it can be restored later */ |
H Hartley Sweeten | 95bc359 | 2012-09-18 11:43:52 -0700 | [diff] [blame] | 1374 | user_chanlist = (unsigned int __user *)cmd.chanlist; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1375 | |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1376 | if (cmd.subdev >= dev->n_subdevices) { |
| 1377 | DPRINTK("%d no such subdevice\n", cmd.subdev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1378 | return -ENODEV; |
| 1379 | } |
| 1380 | |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1381 | s = &dev->subdevices[cmd.subdev]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1382 | async = s->async; |
| 1383 | |
| 1384 | if (s->type == COMEDI_SUBD_UNUSED) { |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1385 | DPRINTK("%d not valid subdevice\n", cmd.subdev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1386 | return -EIO; |
| 1387 | } |
| 1388 | |
| 1389 | if (!s->do_cmd || !s->do_cmdtest || !s->async) { |
| 1390 | DPRINTK("subdevice %i does not support commands\n", |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1391 | cmd.subdev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1392 | return -EIO; |
| 1393 | } |
| 1394 | |
| 1395 | /* are we locked? (ioctl lock) */ |
| 1396 | if (s->lock && s->lock != file) { |
| 1397 | DPRINTK("subdevice locked\n"); |
| 1398 | return -EACCES; |
| 1399 | } |
| 1400 | |
| 1401 | /* are we busy? */ |
| 1402 | if (s->busy) { |
| 1403 | DPRINTK("subdevice busy\n"); |
| 1404 | return -EBUSY; |
| 1405 | } |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1406 | |
| 1407 | /* make sure channel/gain list isn't too long */ |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1408 | if (cmd.chanlist_len > s->len_chanlist) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1409 | DPRINTK("channel/gain list too long %u > %d\n", |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1410 | cmd.chanlist_len, s->len_chanlist); |
Ian Abbott | 4b18f08 | 2013-07-05 16:49:34 +0100 | [diff] [blame] | 1411 | return -EINVAL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | /* make sure channel/gain list isn't too short */ |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1415 | if (cmd.chanlist_len < 1) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1416 | DPRINTK("channel/gain list too short %u < 1\n", |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1417 | cmd.chanlist_len); |
Ian Abbott | 4b18f08 | 2013-07-05 16:49:34 +0100 | [diff] [blame] | 1418 | return -EINVAL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1419 | } |
| 1420 | |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1421 | async->cmd = cmd; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1422 | async->cmd.data = NULL; |
| 1423 | /* load channel/gain list */ |
Teodora Baluta | f0c80e4 | 2013-10-29 00:09:18 +0200 | [diff] [blame^] | 1424 | async->cmd.chanlist = memdup_user(user_chanlist, |
| 1425 | async->cmd.chanlist_len * sizeof(int)); |
| 1426 | if (IS_ERR(async->cmd.chanlist)) { |
| 1427 | ret = PTR_ERR(async->cmd.chanlist); |
| 1428 | DPRINTK("memdup_user failed with code %d\n", ret); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1429 | goto cleanup; |
| 1430 | } |
| 1431 | |
| 1432 | /* make sure each element in channel/gain list is valid */ |
Mark | 21fe2ee | 2010-05-13 17:44:39 +0800 | [diff] [blame] | 1433 | ret = comedi_check_chanlist(s, |
| 1434 | async->cmd.chanlist_len, |
| 1435 | async->cmd.chanlist); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1436 | if (ret < 0) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1437 | DPRINTK("bad chanlist\n"); |
| 1438 | goto cleanup; |
| 1439 | } |
| 1440 | |
| 1441 | ret = s->do_cmdtest(dev, s, &async->cmd); |
| 1442 | |
| 1443 | if (async->cmd.flags & TRIG_BOGUS || ret) { |
| 1444 | DPRINTK("test returned %d\n", ret); |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1445 | cmd = async->cmd; |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1446 | /* restore chanlist pointer before copying back */ |
H Hartley Sweeten | 95bc359 | 2012-09-18 11:43:52 -0700 | [diff] [blame] | 1447 | cmd.chanlist = (unsigned int __force *)user_chanlist; |
H Hartley Sweeten | 88bc057 | 2012-09-18 11:42:37 -0700 | [diff] [blame] | 1448 | cmd.data = NULL; |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 1449 | if (copy_to_user(arg, &cmd, sizeof(cmd))) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1450 | DPRINTK("fault writing cmd\n"); |
| 1451 | ret = -EFAULT; |
| 1452 | goto cleanup; |
| 1453 | } |
| 1454 | ret = -EAGAIN; |
| 1455 | goto cleanup; |
| 1456 | } |
| 1457 | |
| 1458 | if (!async->prealloc_bufsz) { |
| 1459 | ret = -ENOMEM; |
| 1460 | DPRINTK("no buffer (?)\n"); |
| 1461 | goto cleanup; |
| 1462 | } |
| 1463 | |
H Hartley Sweeten | 61c9fb0 | 2013-01-09 13:27:07 -0700 | [diff] [blame] | 1464 | comedi_buf_reset(async); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1465 | |
| 1466 | async->cb_mask = |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1467 | COMEDI_CB_EOA | COMEDI_CB_BLOCK | COMEDI_CB_ERROR | |
| 1468 | COMEDI_CB_OVERFLOW; |
| 1469 | if (async->cmd.flags & TRIG_WAKE_EOS) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1470 | async->cb_mask |= COMEDI_CB_EOS; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1471 | |
Ian Abbott | 67aa4ac | 2013-10-07 15:51:58 +0100 | [diff] [blame] | 1472 | comedi_set_subdevice_runflags(s, SRF_USER | SRF_ERROR | SRF_RUNNING, |
| 1473 | SRF_USER | SRF_RUNNING); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1474 | |
Ian Abbott | 4b18f08 | 2013-07-05 16:49:34 +0100 | [diff] [blame] | 1475 | /* set s->busy _after_ setting SRF_RUNNING flag to avoid race with |
| 1476 | * comedi_read() or comedi_write() */ |
| 1477 | s->busy = file; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1478 | ret = s->do_cmd(dev, s); |
| 1479 | if (ret == 0) |
| 1480 | return 0; |
| 1481 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1482 | cleanup: |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1483 | do_become_nonbusy(dev, s); |
| 1484 | |
| 1485 | return ret; |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | COMEDI_CMDTEST |
| 1490 | command testing ioctl |
| 1491 | |
| 1492 | arg: |
| 1493 | pointer to cmd structure |
| 1494 | |
| 1495 | reads: |
| 1496 | cmd structure at arg |
| 1497 | channel/range list |
| 1498 | |
| 1499 | writes: |
| 1500 | modified cmd structure at arg |
| 1501 | |
| 1502 | */ |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 1503 | static int do_cmdtest_ioctl(struct comedi_device *dev, |
| 1504 | struct comedi_cmd __user *arg, void *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1505 | { |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1506 | struct comedi_cmd cmd; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 1507 | struct comedi_subdevice *s; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1508 | int ret = 0; |
| 1509 | unsigned int *chanlist = NULL; |
H Hartley Sweeten | 95bc359 | 2012-09-18 11:43:52 -0700 | [diff] [blame] | 1510 | unsigned int __user *user_chanlist; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1511 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 1512 | if (copy_from_user(&cmd, arg, sizeof(cmd))) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1513 | DPRINTK("bad cmd address\n"); |
| 1514 | return -EFAULT; |
| 1515 | } |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1516 | /* save user's chanlist pointer so it can be restored later */ |
H Hartley Sweeten | 95bc359 | 2012-09-18 11:43:52 -0700 | [diff] [blame] | 1517 | user_chanlist = (unsigned int __user *)cmd.chanlist; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1518 | |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1519 | if (cmd.subdev >= dev->n_subdevices) { |
| 1520 | DPRINTK("%d no such subdevice\n", cmd.subdev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1521 | return -ENODEV; |
| 1522 | } |
| 1523 | |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1524 | s = &dev->subdevices[cmd.subdev]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1525 | if (s->type == COMEDI_SUBD_UNUSED) { |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1526 | DPRINTK("%d not valid subdevice\n", cmd.subdev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1527 | return -EIO; |
| 1528 | } |
| 1529 | |
| 1530 | if (!s->do_cmd || !s->do_cmdtest) { |
| 1531 | DPRINTK("subdevice %i does not support commands\n", |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1532 | cmd.subdev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1533 | return -EIO; |
| 1534 | } |
| 1535 | |
| 1536 | /* make sure channel/gain list isn't too long */ |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1537 | if (cmd.chanlist_len > s->len_chanlist) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1538 | DPRINTK("channel/gain list too long %d > %d\n", |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1539 | cmd.chanlist_len, s->len_chanlist); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1540 | ret = -EINVAL; |
| 1541 | goto cleanup; |
| 1542 | } |
| 1543 | |
| 1544 | /* load channel/gain list */ |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1545 | if (cmd.chanlist) { |
Teodora Baluta | f0c80e4 | 2013-10-29 00:09:18 +0200 | [diff] [blame^] | 1546 | chanlist = memdup_user(user_chanlist, |
| 1547 | cmd.chanlist_len * sizeof(int)); |
| 1548 | if (IS_ERR(chanlist)) { |
| 1549 | ret = PTR_ERR(chanlist); |
| 1550 | DPRINTK("memdup_user exited with code %d", ret); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1551 | goto cleanup; |
| 1552 | } |
| 1553 | |
| 1554 | /* make sure each element in channel/gain list is valid */ |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1555 | ret = comedi_check_chanlist(s, cmd.chanlist_len, chanlist); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1556 | if (ret < 0) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1557 | DPRINTK("bad chanlist\n"); |
| 1558 | goto cleanup; |
| 1559 | } |
| 1560 | |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1561 | cmd.chanlist = chanlist; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1562 | } |
| 1563 | |
H Hartley Sweeten | f834867 | 2012-09-18 11:43:13 -0700 | [diff] [blame] | 1564 | ret = s->do_cmdtest(dev, s, &cmd); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1565 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1566 | /* restore chanlist pointer before copying back */ |
H Hartley Sweeten | 95bc359 | 2012-09-18 11:43:52 -0700 | [diff] [blame] | 1567 | cmd.chanlist = (unsigned int __force *)user_chanlist; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1568 | |
H Hartley Sweeten | bc252fd | 2012-12-19 15:42:02 -0700 | [diff] [blame] | 1569 | if (copy_to_user(arg, &cmd, sizeof(cmd))) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1570 | DPRINTK("bad cmd address\n"); |
| 1571 | ret = -EFAULT; |
| 1572 | goto cleanup; |
| 1573 | } |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1574 | cleanup: |
| 1575 | kfree(chanlist); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1576 | |
| 1577 | return ret; |
| 1578 | } |
| 1579 | |
| 1580 | /* |
| 1581 | COMEDI_LOCK |
| 1582 | lock subdevice |
| 1583 | |
| 1584 | arg: |
| 1585 | subdevice number |
| 1586 | |
| 1587 | reads: |
| 1588 | none |
| 1589 | |
| 1590 | writes: |
| 1591 | none |
| 1592 | |
| 1593 | */ |
| 1594 | |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 1595 | static int do_lock_ioctl(struct comedi_device *dev, unsigned int arg, |
| 1596 | void *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1597 | { |
| 1598 | int ret = 0; |
| 1599 | unsigned long flags; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 1600 | struct comedi_subdevice *s; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1601 | |
| 1602 | if (arg >= dev->n_subdevices) |
| 1603 | return -EINVAL; |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 1604 | s = &dev->subdevices[arg]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1605 | |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 1606 | spin_lock_irqsave(&s->spin_lock, flags); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1607 | if (s->busy || s->lock) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1608 | ret = -EBUSY; |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1609 | else |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1610 | s->lock = file; |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 1611 | spin_unlock_irqrestore(&s->spin_lock, flags); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1612 | |
Greg Dietsche | c5274ab | 2011-06-13 13:11:47 -0500 | [diff] [blame] | 1613 | #if 0 |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1614 | if (ret < 0) |
| 1615 | return ret; |
| 1616 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1617 | if (s->lock_f) |
| 1618 | ret = s->lock_f(dev, s); |
| 1619 | #endif |
| 1620 | |
| 1621 | return ret; |
| 1622 | } |
| 1623 | |
| 1624 | /* |
| 1625 | COMEDI_UNLOCK |
| 1626 | unlock subdevice |
| 1627 | |
| 1628 | arg: |
| 1629 | subdevice number |
| 1630 | |
| 1631 | reads: |
| 1632 | none |
| 1633 | |
| 1634 | writes: |
| 1635 | none |
| 1636 | |
| 1637 | This function isn't protected by the semaphore, since |
| 1638 | we already own the lock. |
| 1639 | */ |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 1640 | static int do_unlock_ioctl(struct comedi_device *dev, unsigned int arg, |
| 1641 | void *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1642 | { |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 1643 | struct comedi_subdevice *s; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1644 | |
| 1645 | if (arg >= dev->n_subdevices) |
| 1646 | return -EINVAL; |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 1647 | s = &dev->subdevices[arg]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1648 | |
| 1649 | if (s->busy) |
| 1650 | return -EBUSY; |
| 1651 | |
| 1652 | if (s->lock && s->lock != file) |
| 1653 | return -EACCES; |
| 1654 | |
| 1655 | if (s->lock == file) { |
| 1656 | #if 0 |
| 1657 | if (s->unlock) |
| 1658 | s->unlock(dev, s); |
| 1659 | #endif |
| 1660 | |
| 1661 | s->lock = NULL; |
| 1662 | } |
| 1663 | |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | COMEDI_CANCEL |
| 1669 | cancel acquisition ioctl |
| 1670 | |
| 1671 | arg: |
| 1672 | subdevice number |
| 1673 | |
| 1674 | reads: |
| 1675 | nothing |
| 1676 | |
| 1677 | writes: |
| 1678 | nothing |
| 1679 | |
| 1680 | */ |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 1681 | static int do_cancel_ioctl(struct comedi_device *dev, unsigned int arg, |
| 1682 | void *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1683 | { |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 1684 | struct comedi_subdevice *s; |
Ian Abbott | 69acbaa | 2013-07-08 13:36:19 +0100 | [diff] [blame] | 1685 | int ret; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1686 | |
| 1687 | if (arg >= dev->n_subdevices) |
| 1688 | return -EINVAL; |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 1689 | s = &dev->subdevices[arg]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1690 | if (s->async == NULL) |
| 1691 | return -EINVAL; |
| 1692 | |
| 1693 | if (s->lock && s->lock != file) |
| 1694 | return -EACCES; |
| 1695 | |
| 1696 | if (!s->busy) |
| 1697 | return 0; |
| 1698 | |
| 1699 | if (s->busy != file) |
| 1700 | return -EBUSY; |
| 1701 | |
Ian Abbott | 69acbaa | 2013-07-08 13:36:19 +0100 | [diff] [blame] | 1702 | ret = do_cancel(dev, s); |
| 1703 | if (comedi_get_subdevice_runflags(s) & SRF_USER) |
| 1704 | wake_up_interruptible(&s->async->wait_head); |
| 1705 | |
| 1706 | return ret; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | /* |
| 1710 | COMEDI_POLL ioctl |
| 1711 | instructs driver to synchronize buffers |
| 1712 | |
| 1713 | arg: |
| 1714 | subdevice number |
| 1715 | |
| 1716 | reads: |
| 1717 | nothing |
| 1718 | |
| 1719 | writes: |
| 1720 | nothing |
| 1721 | |
| 1722 | */ |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 1723 | static int do_poll_ioctl(struct comedi_device *dev, unsigned int arg, |
| 1724 | void *file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1725 | { |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 1726 | struct comedi_subdevice *s; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1727 | |
| 1728 | if (arg >= dev->n_subdevices) |
| 1729 | return -EINVAL; |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 1730 | s = &dev->subdevices[arg]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1731 | |
| 1732 | if (s->lock && s->lock != file) |
| 1733 | return -EACCES; |
| 1734 | |
| 1735 | if (!s->busy) |
| 1736 | return 0; |
| 1737 | |
| 1738 | if (s->busy != file) |
| 1739 | return -EBUSY; |
| 1740 | |
| 1741 | if (s->poll) |
| 1742 | return s->poll(dev, s); |
| 1743 | |
| 1744 | return -EINVAL; |
| 1745 | } |
| 1746 | |
H Hartley Sweeten | 47db6d5 | 2012-06-07 17:05:27 -0700 | [diff] [blame] | 1747 | static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd, |
| 1748 | unsigned long arg) |
| 1749 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 1750 | const unsigned minor = iminor(file_inode(file)); |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 1751 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
H Hartley Sweeten | 47db6d5 | 2012-06-07 17:05:27 -0700 | [diff] [blame] | 1752 | int rc; |
| 1753 | |
H Hartley Sweeten | 4da5fa9 | 2012-12-19 15:35:23 -0700 | [diff] [blame] | 1754 | if (!dev) |
H Hartley Sweeten | 47db6d5 | 2012-06-07 17:05:27 -0700 | [diff] [blame] | 1755 | return -ENODEV; |
H Hartley Sweeten | 47db6d5 | 2012-06-07 17:05:27 -0700 | [diff] [blame] | 1756 | |
| 1757 | mutex_lock(&dev->mutex); |
| 1758 | |
| 1759 | /* Device config is special, because it must work on |
| 1760 | * an unconfigured device. */ |
| 1761 | if (cmd == COMEDI_DEVCONFIG) { |
Ian Abbott | 754ab5c | 2013-01-28 16:14:31 +0000 | [diff] [blame] | 1762 | if (minor >= COMEDI_NUM_BOARD_MINORS) { |
| 1763 | /* Device config not appropriate on non-board minors. */ |
| 1764 | rc = -ENOTTY; |
| 1765 | goto done; |
| 1766 | } |
H Hartley Sweeten | 47db6d5 | 2012-06-07 17:05:27 -0700 | [diff] [blame] | 1767 | rc = do_devconfig_ioctl(dev, |
| 1768 | (struct comedi_devconfig __user *)arg); |
Ian Abbott | 8ab4ed6 | 2013-04-04 14:58:54 +0100 | [diff] [blame] | 1769 | if (rc == 0) { |
| 1770 | if (arg == 0 && |
| 1771 | dev->minor >= comedi_num_legacy_minors) { |
| 1772 | /* Successfully unconfigured a dynamically |
| 1773 | * allocated device. Try and remove it. */ |
Ian Abbott | db210da | 2013-04-04 14:59:18 +0100 | [diff] [blame] | 1774 | if (comedi_clear_board_dev(dev)) { |
Ian Abbott | 8ab4ed6 | 2013-04-04 14:58:54 +0100 | [diff] [blame] | 1775 | mutex_unlock(&dev->mutex); |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 1776 | comedi_free_board_dev(dev); |
Ian Abbott | 8ab4ed6 | 2013-04-04 14:58:54 +0100 | [diff] [blame] | 1777 | return rc; |
| 1778 | } |
| 1779 | } |
| 1780 | } |
H Hartley Sweeten | 47db6d5 | 2012-06-07 17:05:27 -0700 | [diff] [blame] | 1781 | goto done; |
| 1782 | } |
| 1783 | |
| 1784 | if (!dev->attached) { |
| 1785 | DPRINTK("no driver configured on /dev/comedi%i\n", dev->minor); |
| 1786 | rc = -ENODEV; |
| 1787 | goto done; |
| 1788 | } |
| 1789 | |
| 1790 | switch (cmd) { |
| 1791 | case COMEDI_BUFCONFIG: |
| 1792 | rc = do_bufconfig_ioctl(dev, |
| 1793 | (struct comedi_bufconfig __user *)arg); |
| 1794 | break; |
| 1795 | case COMEDI_DEVINFO: |
| 1796 | rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg, |
| 1797 | file); |
| 1798 | break; |
| 1799 | case COMEDI_SUBDINFO: |
| 1800 | rc = do_subdinfo_ioctl(dev, |
| 1801 | (struct comedi_subdinfo __user *)arg, |
| 1802 | file); |
| 1803 | break; |
| 1804 | case COMEDI_CHANINFO: |
| 1805 | rc = do_chaninfo_ioctl(dev, (void __user *)arg); |
| 1806 | break; |
| 1807 | case COMEDI_RANGEINFO: |
| 1808 | rc = do_rangeinfo_ioctl(dev, (void __user *)arg); |
| 1809 | break; |
| 1810 | case COMEDI_BUFINFO: |
| 1811 | rc = do_bufinfo_ioctl(dev, |
| 1812 | (struct comedi_bufinfo __user *)arg, |
| 1813 | file); |
| 1814 | break; |
| 1815 | case COMEDI_LOCK: |
| 1816 | rc = do_lock_ioctl(dev, arg, file); |
| 1817 | break; |
| 1818 | case COMEDI_UNLOCK: |
| 1819 | rc = do_unlock_ioctl(dev, arg, file); |
| 1820 | break; |
| 1821 | case COMEDI_CANCEL: |
| 1822 | rc = do_cancel_ioctl(dev, arg, file); |
| 1823 | break; |
| 1824 | case COMEDI_CMD: |
| 1825 | rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file); |
| 1826 | break; |
| 1827 | case COMEDI_CMDTEST: |
| 1828 | rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg, |
| 1829 | file); |
| 1830 | break; |
| 1831 | case COMEDI_INSNLIST: |
| 1832 | rc = do_insnlist_ioctl(dev, |
| 1833 | (struct comedi_insnlist __user *)arg, |
| 1834 | file); |
| 1835 | break; |
| 1836 | case COMEDI_INSN: |
| 1837 | rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg, |
| 1838 | file); |
| 1839 | break; |
| 1840 | case COMEDI_POLL: |
| 1841 | rc = do_poll_ioctl(dev, arg, file); |
| 1842 | break; |
| 1843 | default: |
| 1844 | rc = -ENOTTY; |
| 1845 | break; |
| 1846 | } |
| 1847 | |
| 1848 | done: |
| 1849 | mutex_unlock(&dev->mutex); |
| 1850 | return rc; |
| 1851 | } |
| 1852 | |
Federico Vaga | df30b21 | 2011-10-29 09:45:39 +0200 | [diff] [blame] | 1853 | static void comedi_vm_open(struct vm_area_struct *area) |
| 1854 | { |
| 1855 | struct comedi_async *async; |
| 1856 | struct comedi_device *dev; |
| 1857 | |
| 1858 | async = area->vm_private_data; |
| 1859 | dev = async->subdevice->device; |
| 1860 | |
| 1861 | mutex_lock(&dev->mutex); |
| 1862 | async->mmap_count++; |
| 1863 | mutex_unlock(&dev->mutex); |
| 1864 | } |
| 1865 | |
| 1866 | static void comedi_vm_close(struct vm_area_struct *area) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1867 | { |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 1868 | struct comedi_async *async; |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 1869 | struct comedi_device *dev; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1870 | |
| 1871 | async = area->vm_private_data; |
| 1872 | dev = async->subdevice->device; |
| 1873 | |
| 1874 | mutex_lock(&dev->mutex); |
| 1875 | async->mmap_count--; |
| 1876 | mutex_unlock(&dev->mutex); |
| 1877 | } |
| 1878 | |
| 1879 | static struct vm_operations_struct comedi_vm_ops = { |
Federico Vaga | df30b21 | 2011-10-29 09:45:39 +0200 | [diff] [blame] | 1880 | .open = comedi_vm_open, |
| 1881 | .close = comedi_vm_close, |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1882 | }; |
| 1883 | |
| 1884 | static int comedi_mmap(struct file *file, struct vm_area_struct *vma) |
| 1885 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 1886 | const unsigned minor = iminor(file_inode(file)); |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 1887 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
H Hartley Sweeten | a52840a | 2012-12-19 15:45:12 -0700 | [diff] [blame] | 1888 | struct comedi_subdevice *s; |
| 1889 | struct comedi_async *async; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1890 | unsigned long start = vma->vm_start; |
| 1891 | unsigned long size; |
| 1892 | int n_pages; |
| 1893 | int i; |
| 1894 | int retval; |
Bernd Porr | 3ffab42 | 2011-11-08 21:23:03 +0000 | [diff] [blame] | 1895 | |
H Hartley Sweeten | a52840a | 2012-12-19 15:45:12 -0700 | [diff] [blame] | 1896 | if (!dev) |
Florian Schmaus | 70fe742 | 2011-12-14 14:23:24 +0100 | [diff] [blame] | 1897 | return -ENODEV; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1898 | |
| 1899 | mutex_lock(&dev->mutex); |
H Hartley Sweeten | a52840a | 2012-12-19 15:45:12 -0700 | [diff] [blame] | 1900 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1901 | if (!dev->attached) { |
| 1902 | DPRINTK("no driver configured on comedi%i\n", dev->minor); |
| 1903 | retval = -ENODEV; |
| 1904 | goto done; |
| 1905 | } |
H Hartley Sweeten | a52840a | 2012-12-19 15:45:12 -0700 | [diff] [blame] | 1906 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1907 | if (vma->vm_flags & VM_WRITE) |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 1908 | s = comedi_write_subdevice(dev, minor); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1909 | else |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 1910 | s = comedi_read_subdevice(dev, minor); |
H Hartley Sweeten | a52840a | 2012-12-19 15:45:12 -0700 | [diff] [blame] | 1911 | if (!s) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1912 | retval = -EINVAL; |
| 1913 | goto done; |
| 1914 | } |
H Hartley Sweeten | a52840a | 2012-12-19 15:45:12 -0700 | [diff] [blame] | 1915 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1916 | async = s->async; |
H Hartley Sweeten | a52840a | 2012-12-19 15:45:12 -0700 | [diff] [blame] | 1917 | if (!async) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1918 | retval = -EINVAL; |
| 1919 | goto done; |
| 1920 | } |
| 1921 | |
| 1922 | if (vma->vm_pgoff != 0) { |
| 1923 | DPRINTK("comedi: mmap() offset must be 0.\n"); |
| 1924 | retval = -EINVAL; |
| 1925 | goto done; |
| 1926 | } |
| 1927 | |
| 1928 | size = vma->vm_end - vma->vm_start; |
| 1929 | if (size > async->prealloc_bufsz) { |
| 1930 | retval = -EFAULT; |
| 1931 | goto done; |
| 1932 | } |
| 1933 | if (size & (~PAGE_MASK)) { |
| 1934 | retval = -EFAULT; |
| 1935 | goto done; |
| 1936 | } |
| 1937 | |
| 1938 | n_pages = size >> PAGE_SHIFT; |
| 1939 | for (i = 0; i < n_pages; ++i) { |
H Hartley Sweeten | a52840a | 2012-12-19 15:45:12 -0700 | [diff] [blame] | 1940 | struct comedi_buf_page *buf = &async->buf_page_list[i]; |
| 1941 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1942 | if (remap_pfn_range(vma, start, |
H Hartley Sweeten | a52840a | 2012-12-19 15:45:12 -0700 | [diff] [blame] | 1943 | page_to_pfn(virt_to_page(buf->virt_addr)), |
| 1944 | PAGE_SIZE, PAGE_SHARED)) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1945 | retval = -EAGAIN; |
| 1946 | goto done; |
| 1947 | } |
| 1948 | start += PAGE_SIZE; |
| 1949 | } |
| 1950 | |
| 1951 | vma->vm_ops = &comedi_vm_ops; |
| 1952 | vma->vm_private_data = async; |
| 1953 | |
| 1954 | async->mmap_count++; |
| 1955 | |
| 1956 | retval = 0; |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 1957 | done: |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1958 | mutex_unlock(&dev->mutex); |
| 1959 | return retval; |
| 1960 | } |
| 1961 | |
Benedikt Bergenthal | 1ae5062 | 2012-04-16 12:40:22 +0200 | [diff] [blame] | 1962 | static unsigned int comedi_poll(struct file *file, poll_table *wait) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1963 | { |
| 1964 | unsigned int mask = 0; |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 1965 | const unsigned minor = iminor(file_inode(file)); |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 1966 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
H Hartley Sweeten | ca081b1 | 2012-12-19 15:39:44 -0700 | [diff] [blame] | 1967 | struct comedi_subdevice *s; |
Bernd Porr | 3ffab42 | 2011-11-08 21:23:03 +0000 | [diff] [blame] | 1968 | |
H Hartley Sweeten | ca081b1 | 2012-12-19 15:39:44 -0700 | [diff] [blame] | 1969 | if (!dev) |
Florian Schmaus | 70fe742 | 2011-12-14 14:23:24 +0100 | [diff] [blame] | 1970 | return -ENODEV; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1971 | |
| 1972 | mutex_lock(&dev->mutex); |
H Hartley Sweeten | ca081b1 | 2012-12-19 15:39:44 -0700 | [diff] [blame] | 1973 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1974 | if (!dev->attached) { |
| 1975 | DPRINTK("no driver configured on comedi%i\n", dev->minor); |
H Hartley Sweeten | ca081b1 | 2012-12-19 15:39:44 -0700 | [diff] [blame] | 1976 | goto done; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1977 | } |
| 1978 | |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 1979 | s = comedi_read_subdevice(dev, minor); |
Ian Abbott | cc400e1 | 2013-02-05 12:50:40 +0000 | [diff] [blame] | 1980 | if (s && s->async) { |
H Hartley Sweeten | ca081b1 | 2012-12-19 15:39:44 -0700 | [diff] [blame] | 1981 | poll_wait(file, &s->async->wait_head, wait); |
H Hartley Sweeten | f012463 | 2012-12-19 15:43:18 -0700 | [diff] [blame] | 1982 | if (!s->busy || !comedi_is_subdevice_running(s) || |
H Hartley Sweeten | ca081b1 | 2012-12-19 15:39:44 -0700 | [diff] [blame] | 1983 | comedi_buf_read_n_available(s->async) > 0) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1984 | mask |= POLLIN | POLLRDNORM; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1985 | } |
| 1986 | |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 1987 | s = comedi_write_subdevice(dev, minor); |
Ian Abbott | cc400e1 | 2013-02-05 12:50:40 +0000 | [diff] [blame] | 1988 | if (s && s->async) { |
H Hartley Sweeten | ca081b1 | 2012-12-19 15:39:44 -0700 | [diff] [blame] | 1989 | unsigned int bps = bytes_per_sample(s->async->subdevice); |
| 1990 | |
| 1991 | poll_wait(file, &s->async->wait_head, wait); |
| 1992 | comedi_buf_write_alloc(s->async, s->async->prealloc_bufsz); |
H Hartley Sweeten | f012463 | 2012-12-19 15:43:18 -0700 | [diff] [blame] | 1993 | if (!s->busy || !comedi_is_subdevice_running(s) || |
H Hartley Sweeten | ca081b1 | 2012-12-19 15:39:44 -0700 | [diff] [blame] | 1994 | comedi_buf_write_n_allocated(s->async) >= bps) |
| 1995 | mask |= POLLOUT | POLLWRNORM; |
| 1996 | } |
| 1997 | |
| 1998 | done: |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 1999 | mutex_unlock(&dev->mutex); |
| 2000 | return mask; |
| 2001 | } |
| 2002 | |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 2003 | static ssize_t comedi_write(struct file *file, const char __user *buf, |
| 2004 | size_t nbytes, loff_t *offset) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2005 | { |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 2006 | struct comedi_subdevice *s; |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 2007 | struct comedi_async *async; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2008 | int n, m, count = 0, retval = 0; |
| 2009 | DECLARE_WAITQUEUE(wait, current); |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 2010 | const unsigned minor = iminor(file_inode(file)); |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 2011 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
Bernd Porr | 3ffab42 | 2011-11-08 21:23:03 +0000 | [diff] [blame] | 2012 | |
H Hartley Sweeten | 2714b01 | 2012-12-19 15:40:34 -0700 | [diff] [blame] | 2013 | if (!dev) |
Florian Schmaus | 70fe742 | 2011-12-14 14:23:24 +0100 | [diff] [blame] | 2014 | return -ENODEV; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2015 | |
| 2016 | if (!dev->attached) { |
| 2017 | DPRINTK("no driver configured on comedi%i\n", dev->minor); |
H Hartley Sweeten | 2714b01 | 2012-12-19 15:40:34 -0700 | [diff] [blame] | 2018 | return -ENODEV; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2019 | } |
| 2020 | |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 2021 | s = comedi_write_subdevice(dev, minor); |
Ian Abbott | cc400e1 | 2013-02-05 12:50:40 +0000 | [diff] [blame] | 2022 | if (!s || !s->async) |
H Hartley Sweeten | 2714b01 | 2012-12-19 15:40:34 -0700 | [diff] [blame] | 2023 | return -EIO; |
| 2024 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2025 | async = s->async; |
| 2026 | |
H Hartley Sweeten | 2714b01 | 2012-12-19 15:40:34 -0700 | [diff] [blame] | 2027 | if (!s->busy || !nbytes) |
| 2028 | return 0; |
| 2029 | if (s->busy != file) |
| 2030 | return -EACCES; |
| 2031 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2032 | add_wait_queue(&async->wait_head, &wait); |
| 2033 | while (nbytes > 0 && !retval) { |
| 2034 | set_current_state(TASK_INTERRUPTIBLE); |
| 2035 | |
H Hartley Sweeten | f012463 | 2012-12-19 15:43:18 -0700 | [diff] [blame] | 2036 | if (!comedi_is_subdevice_running(s)) { |
Ian Abbott | d261154 | 2010-05-19 17:22:41 +0100 | [diff] [blame] | 2037 | if (count == 0) { |
Ian Abbott | 4b18f08 | 2013-07-05 16:49:34 +0100 | [diff] [blame] | 2038 | mutex_lock(&dev->mutex); |
H Hartley Sweeten | c098c21a | 2012-12-19 15:44:02 -0700 | [diff] [blame] | 2039 | if (comedi_is_subdevice_in_error(s)) |
Ian Abbott | d261154 | 2010-05-19 17:22:41 +0100 | [diff] [blame] | 2040 | retval = -EPIPE; |
H Hartley Sweeten | c098c21a | 2012-12-19 15:44:02 -0700 | [diff] [blame] | 2041 | else |
Ian Abbott | d261154 | 2010-05-19 17:22:41 +0100 | [diff] [blame] | 2042 | retval = 0; |
Ian Abbott | d261154 | 2010-05-19 17:22:41 +0100 | [diff] [blame] | 2043 | do_become_nonbusy(dev, s); |
Ian Abbott | 4b18f08 | 2013-07-05 16:49:34 +0100 | [diff] [blame] | 2044 | mutex_unlock(&dev->mutex); |
Ian Abbott | d261154 | 2010-05-19 17:22:41 +0100 | [diff] [blame] | 2045 | } |
| 2046 | break; |
| 2047 | } |
| 2048 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2049 | n = nbytes; |
| 2050 | |
| 2051 | m = n; |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2052 | if (async->buf_write_ptr + m > async->prealloc_bufsz) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2053 | m = async->prealloc_bufsz - async->buf_write_ptr; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2054 | comedi_buf_write_alloc(async, async->prealloc_bufsz); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2055 | if (m > comedi_buf_write_n_allocated(async)) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2056 | m = comedi_buf_write_n_allocated(async); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2057 | if (m < n) |
| 2058 | n = m; |
| 2059 | |
| 2060 | if (n == 0) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2061 | if (file->f_flags & O_NONBLOCK) { |
| 2062 | retval = -EAGAIN; |
| 2063 | break; |
| 2064 | } |
Federico Vaga | 6a9ce6b | 2011-10-29 09:47:39 +0200 | [diff] [blame] | 2065 | schedule(); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2066 | if (signal_pending(current)) { |
| 2067 | retval = -ERESTARTSYS; |
| 2068 | break; |
| 2069 | } |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2070 | if (!s->busy) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2071 | break; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2072 | if (s->busy != file) { |
| 2073 | retval = -EACCES; |
| 2074 | break; |
| 2075 | } |
| 2076 | continue; |
| 2077 | } |
| 2078 | |
| 2079 | m = copy_from_user(async->prealloc_buf + async->buf_write_ptr, |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2080 | buf, n); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2081 | if (m) { |
| 2082 | n -= m; |
| 2083 | retval = -EFAULT; |
| 2084 | } |
| 2085 | comedi_buf_write_free(async, n); |
| 2086 | |
| 2087 | count += n; |
| 2088 | nbytes -= n; |
| 2089 | |
| 2090 | buf += n; |
| 2091 | break; /* makes device work like a pipe */ |
| 2092 | } |
| 2093 | set_current_state(TASK_RUNNING); |
| 2094 | remove_wait_queue(&async->wait_head, &wait); |
| 2095 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2096 | return count ? count : retval; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2097 | } |
| 2098 | |
Greg Kroah-Hartman | 92d0127 | 2010-05-03 16:32:28 -0700 | [diff] [blame] | 2099 | static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes, |
Andrea Gelmini | 6705b68 | 2010-02-26 10:14:55 +0100 | [diff] [blame] | 2100 | loff_t *offset) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2101 | { |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 2102 | struct comedi_subdevice *s; |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 2103 | struct comedi_async *async; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2104 | int n, m, count = 0, retval = 0; |
| 2105 | DECLARE_WAITQUEUE(wait, current); |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 2106 | const unsigned minor = iminor(file_inode(file)); |
Ian Abbott | 5e04c25 | 2013-04-04 14:59:11 +0100 | [diff] [blame] | 2107 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
Bernd Porr | 3ffab42 | 2011-11-08 21:23:03 +0000 | [diff] [blame] | 2108 | |
H Hartley Sweeten | 5c87fef | 2012-12-19 15:40:08 -0700 | [diff] [blame] | 2109 | if (!dev) |
Florian Schmaus | 70fe742 | 2011-12-14 14:23:24 +0100 | [diff] [blame] | 2110 | return -ENODEV; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2111 | |
| 2112 | if (!dev->attached) { |
| 2113 | DPRINTK("no driver configured on comedi%i\n", dev->minor); |
H Hartley Sweeten | 5c87fef | 2012-12-19 15:40:08 -0700 | [diff] [blame] | 2114 | return -ENODEV; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2115 | } |
| 2116 | |
Ian Abbott | da56fdc | 2013-04-04 14:59:10 +0100 | [diff] [blame] | 2117 | s = comedi_read_subdevice(dev, minor); |
Ian Abbott | cc400e1 | 2013-02-05 12:50:40 +0000 | [diff] [blame] | 2118 | if (!s || !s->async) |
H Hartley Sweeten | 5c87fef | 2012-12-19 15:40:08 -0700 | [diff] [blame] | 2119 | return -EIO; |
| 2120 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2121 | async = s->async; |
H Hartley Sweeten | 5c87fef | 2012-12-19 15:40:08 -0700 | [diff] [blame] | 2122 | if (!s->busy || !nbytes) |
| 2123 | return 0; |
| 2124 | if (s->busy != file) |
| 2125 | return -EACCES; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2126 | |
| 2127 | add_wait_queue(&async->wait_head, &wait); |
| 2128 | while (nbytes > 0 && !retval) { |
| 2129 | set_current_state(TASK_INTERRUPTIBLE); |
| 2130 | |
| 2131 | n = nbytes; |
| 2132 | |
| 2133 | m = comedi_buf_read_n_available(async); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2134 | /* printk("%d available\n",m); */ |
| 2135 | if (async->buf_read_ptr + m > async->prealloc_bufsz) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2136 | m = async->prealloc_bufsz - async->buf_read_ptr; |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2137 | /* printk("%d contiguous\n",m); */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2138 | if (m < n) |
| 2139 | n = m; |
| 2140 | |
| 2141 | if (n == 0) { |
H Hartley Sweeten | f012463 | 2012-12-19 15:43:18 -0700 | [diff] [blame] | 2142 | if (!comedi_is_subdevice_running(s)) { |
Ian Abbott | 4b18f08 | 2013-07-05 16:49:34 +0100 | [diff] [blame] | 2143 | mutex_lock(&dev->mutex); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2144 | do_become_nonbusy(dev, s); |
H Hartley Sweeten | c098c21a | 2012-12-19 15:44:02 -0700 | [diff] [blame] | 2145 | if (comedi_is_subdevice_in_error(s)) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2146 | retval = -EPIPE; |
H Hartley Sweeten | c098c21a | 2012-12-19 15:44:02 -0700 | [diff] [blame] | 2147 | else |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2148 | retval = 0; |
Ian Abbott | 4b18f08 | 2013-07-05 16:49:34 +0100 | [diff] [blame] | 2149 | mutex_unlock(&dev->mutex); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2150 | break; |
| 2151 | } |
| 2152 | if (file->f_flags & O_NONBLOCK) { |
| 2153 | retval = -EAGAIN; |
| 2154 | break; |
| 2155 | } |
Federico Vaga | 6a9ce6b | 2011-10-29 09:47:39 +0200 | [diff] [blame] | 2156 | schedule(); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2157 | if (signal_pending(current)) { |
| 2158 | retval = -ERESTARTSYS; |
| 2159 | break; |
| 2160 | } |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2161 | if (!s->busy) { |
| 2162 | retval = 0; |
| 2163 | break; |
| 2164 | } |
| 2165 | if (s->busy != file) { |
| 2166 | retval = -EACCES; |
| 2167 | break; |
| 2168 | } |
| 2169 | continue; |
| 2170 | } |
| 2171 | m = copy_to_user(buf, async->prealloc_buf + |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2172 | async->buf_read_ptr, n); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2173 | if (m) { |
| 2174 | n -= m; |
| 2175 | retval = -EFAULT; |
| 2176 | } |
| 2177 | |
| 2178 | comedi_buf_read_alloc(async, n); |
| 2179 | comedi_buf_read_free(async, n); |
| 2180 | |
| 2181 | count += n; |
| 2182 | nbytes -= n; |
| 2183 | |
| 2184 | buf += n; |
| 2185 | break; /* makes device work like a pipe */ |
| 2186 | } |
Ian Abbott | 4b18f08 | 2013-07-05 16:49:34 +0100 | [diff] [blame] | 2187 | if (comedi_is_subdevice_idle(s)) { |
| 2188 | mutex_lock(&dev->mutex); |
| 2189 | if (async->buf_read_count - async->buf_write_count == 0) |
| 2190 | do_become_nonbusy(dev, s); |
| 2191 | mutex_unlock(&dev->mutex); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2192 | } |
| 2193 | set_current_state(TASK_RUNNING); |
| 2194 | remove_wait_queue(&async->wait_head, &wait); |
| 2195 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2196 | return count ? count : retval; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2197 | } |
| 2198 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2199 | static int comedi_open(struct inode *inode, struct file *file) |
| 2200 | { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2201 | const unsigned minor = iminor(inode); |
H Hartley Sweeten | 4da5fa9 | 2012-12-19 15:35:23 -0700 | [diff] [blame] | 2202 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
Ian Abbott | 9792007 | 2009-02-09 16:51:38 +0000 | [diff] [blame] | 2203 | |
H Hartley Sweeten | 4da5fa9 | 2012-12-19 15:35:23 -0700 | [diff] [blame] | 2204 | if (!dev) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2205 | DPRINTK("invalid minor number\n"); |
| 2206 | return -ENODEV; |
| 2207 | } |
| 2208 | |
| 2209 | /* This is slightly hacky, but we want module autoloading |
| 2210 | * to work for root. |
| 2211 | * case: user opens device, attached -> ok |
Ian Abbott | 13f12b5 | 2013-03-15 13:15:34 +0000 | [diff] [blame] | 2212 | * case: user opens device, unattached, !in_request_module -> autoload |
| 2213 | * case: user opens device, unattached, in_request_module -> fail |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2214 | * case: root opens device, attached -> ok |
Ian Abbott | 13f12b5 | 2013-03-15 13:15:34 +0000 | [diff] [blame] | 2215 | * case: root opens device, unattached, in_request_module -> ok |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2216 | * (typically called from modprobe) |
Ian Abbott | 13f12b5 | 2013-03-15 13:15:34 +0000 | [diff] [blame] | 2217 | * case: root opens device, unattached, !in_request_module -> autoload |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2218 | * |
| 2219 | * The last could be changed to "-> ok", which would deny root |
| 2220 | * autoloading. |
| 2221 | */ |
| 2222 | mutex_lock(&dev->mutex); |
| 2223 | if (dev->attached) |
| 2224 | goto ok; |
Eric Paris | a8f80e8 | 2009-08-13 09:44:51 -0400 | [diff] [blame] | 2225 | if (!capable(CAP_NET_ADMIN) && dev->in_request_module) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2226 | DPRINTK("in request module\n"); |
| 2227 | mutex_unlock(&dev->mutex); |
| 2228 | return -ENODEV; |
| 2229 | } |
Eric Paris | a8f80e8 | 2009-08-13 09:44:51 -0400 | [diff] [blame] | 2230 | if (capable(CAP_NET_ADMIN) && dev->in_request_module) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2231 | goto ok; |
| 2232 | |
Ian Abbott | 13f12b5 | 2013-03-15 13:15:34 +0000 | [diff] [blame] | 2233 | dev->in_request_module = true; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2234 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2235 | #ifdef CONFIG_KMOD |
| 2236 | mutex_unlock(&dev->mutex); |
Ian Abbott | 56d92c6 | 2009-02-09 16:32:12 +0000 | [diff] [blame] | 2237 | request_module("char-major-%i-%i", COMEDI_MAJOR, dev->minor); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2238 | mutex_lock(&dev->mutex); |
| 2239 | #endif |
| 2240 | |
Ian Abbott | 13f12b5 | 2013-03-15 13:15:34 +0000 | [diff] [blame] | 2241 | dev->in_request_module = false; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2242 | |
Eric Paris | a8f80e8 | 2009-08-13 09:44:51 -0400 | [diff] [blame] | 2243 | if (!dev->attached && !capable(CAP_NET_ADMIN)) { |
| 2244 | DPRINTK("not attached and not CAP_NET_ADMIN\n"); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2245 | mutex_unlock(&dev->mutex); |
| 2246 | return -ENODEV; |
| 2247 | } |
| 2248 | ok: |
| 2249 | __module_get(THIS_MODULE); |
| 2250 | |
| 2251 | if (dev->attached) { |
| 2252 | if (!try_module_get(dev->driver->module)) { |
| 2253 | module_put(THIS_MODULE); |
| 2254 | mutex_unlock(&dev->mutex); |
| 2255 | return -ENOSYS; |
| 2256 | } |
| 2257 | } |
| 2258 | |
Ian Abbott | 3c17ba07 | 2010-05-19 14:10:00 +0100 | [diff] [blame] | 2259 | if (dev->attached && dev->use_count == 0 && dev->open) { |
| 2260 | int rc = dev->open(dev); |
| 2261 | if (rc < 0) { |
| 2262 | module_put(dev->driver->module); |
| 2263 | module_put(THIS_MODULE); |
| 2264 | mutex_unlock(&dev->mutex); |
| 2265 | return rc; |
| 2266 | } |
| 2267 | } |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2268 | |
| 2269 | dev->use_count++; |
| 2270 | |
| 2271 | mutex_unlock(&dev->mutex); |
| 2272 | |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
H Hartley Sweeten | 2aae007 | 2012-12-19 15:31:57 -0700 | [diff] [blame] | 2276 | static int comedi_fasync(int fd, struct file *file, int on) |
| 2277 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 2278 | const unsigned minor = iminor(file_inode(file)); |
H Hartley Sweeten | 4da5fa9 | 2012-12-19 15:35:23 -0700 | [diff] [blame] | 2279 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
H Hartley Sweeten | 2aae007 | 2012-12-19 15:31:57 -0700 | [diff] [blame] | 2280 | |
H Hartley Sweeten | 4da5fa9 | 2012-12-19 15:35:23 -0700 | [diff] [blame] | 2281 | if (!dev) |
H Hartley Sweeten | 2aae007 | 2012-12-19 15:31:57 -0700 | [diff] [blame] | 2282 | return -ENODEV; |
| 2283 | |
| 2284 | return fasync_helper(fd, file, on, &dev->async_queue); |
| 2285 | } |
| 2286 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2287 | static int comedi_close(struct inode *inode, struct file *file) |
| 2288 | { |
| 2289 | const unsigned minor = iminor(inode); |
H Hartley Sweeten | 4da5fa9 | 2012-12-19 15:35:23 -0700 | [diff] [blame] | 2290 | struct comedi_device *dev = comedi_dev_from_minor(minor); |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 2291 | struct comedi_subdevice *s = NULL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2292 | int i; |
Bernd Porr | 3ffab42 | 2011-11-08 21:23:03 +0000 | [diff] [blame] | 2293 | |
H Hartley Sweeten | 4da5fa9 | 2012-12-19 15:35:23 -0700 | [diff] [blame] | 2294 | if (!dev) |
Florian Schmaus | 70fe742 | 2011-12-14 14:23:24 +0100 | [diff] [blame] | 2295 | return -ENODEV; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2296 | |
| 2297 | mutex_lock(&dev->mutex); |
| 2298 | |
| 2299 | if (dev->subdevices) { |
| 2300 | for (i = 0; i < dev->n_subdevices; i++) { |
H Hartley Sweeten | b077f2c | 2012-09-05 18:20:58 -0700 | [diff] [blame] | 2301 | s = &dev->subdevices[i]; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2302 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2303 | if (s->busy == file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2304 | do_cancel(dev, s); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2305 | if (s->lock == file) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2306 | s->lock = NULL; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2307 | } |
| 2308 | } |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2309 | if (dev->attached && dev->use_count == 1 && dev->close) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2310 | dev->close(dev); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2311 | |
| 2312 | module_put(THIS_MODULE); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2313 | if (dev->attached) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2314 | module_put(dev->driver->module); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2315 | |
| 2316 | dev->use_count--; |
| 2317 | |
| 2318 | mutex_unlock(&dev->mutex); |
| 2319 | |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2320 | return 0; |
| 2321 | } |
| 2322 | |
Ian Abbott | 8cb8aad | 2012-06-19 10:17:43 +0100 | [diff] [blame] | 2323 | static const struct file_operations comedi_fops = { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 2324 | .owner = THIS_MODULE, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 2325 | .unlocked_ioctl = comedi_unlocked_ioctl, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 2326 | .compat_ioctl = comedi_compat_ioctl, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 2327 | .open = comedi_open, |
| 2328 | .release = comedi_close, |
| 2329 | .read = comedi_read, |
| 2330 | .write = comedi_write, |
| 2331 | .mmap = comedi_mmap, |
| 2332 | .poll = comedi_poll, |
| 2333 | .fasync = comedi_fasync, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 2334 | .llseek = noop_llseek, |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2335 | }; |
| 2336 | |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 2337 | void comedi_error(const struct comedi_device *dev, const char *s) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2338 | { |
Ian Abbott | 4f870fe | 2012-08-16 14:38:05 +0100 | [diff] [blame] | 2339 | dev_err(dev->class_dev, "%s: %s\n", dev->driver->driver_name, s); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2340 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 2341 | EXPORT_SYMBOL_GPL(comedi_error); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2342 | |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 2343 | void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2344 | { |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 2345 | struct comedi_async *async = s->async; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2346 | unsigned runflags = 0; |
| 2347 | unsigned runflags_mask = 0; |
| 2348 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2349 | /* DPRINTK("comedi_event 0x%x\n",mask); */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2350 | |
H Hartley Sweeten | f012463 | 2012-12-19 15:43:18 -0700 | [diff] [blame] | 2351 | if (!comedi_is_subdevice_running(s)) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2352 | return; |
| 2353 | |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 2354 | if (s-> |
| 2355 | async->events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | |
| 2356 | COMEDI_CB_OVERFLOW)) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2357 | runflags_mask |= SRF_RUNNING; |
| 2358 | } |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2359 | /* remember if an error event has occurred, so an error |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2360 | * can be returned the next time the user does a read() */ |
| 2361 | if (s->async->events & (COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) { |
| 2362 | runflags_mask |= SRF_ERROR; |
| 2363 | runflags |= SRF_ERROR; |
| 2364 | } |
| 2365 | if (runflags_mask) { |
| 2366 | /*sets SRF_ERROR and SRF_RUNNING together atomically */ |
| 2367 | comedi_set_subdevice_runflags(s, runflags_mask, runflags); |
| 2368 | } |
| 2369 | |
| 2370 | if (async->cb_mask & s->async->events) { |
| 2371 | if (comedi_get_subdevice_runflags(s) & SRF_USER) { |
Greg Kroah-Hartman | fcea115 | 2009-04-27 15:15:30 -0700 | [diff] [blame] | 2372 | wake_up_interruptible(&async->wait_head); |
Andrea Gelmini | 6705b68 | 2010-02-26 10:14:55 +0100 | [diff] [blame] | 2373 | if (s->subdev_flags & SDF_CMD_READ) |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 2374 | kill_fasync(&dev->async_queue, SIGIO, POLL_IN); |
Andrea Gelmini | 6705b68 | 2010-02-26 10:14:55 +0100 | [diff] [blame] | 2375 | if (s->subdev_flags & SDF_CMD_WRITE) |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 2376 | kill_fasync(&dev->async_queue, SIGIO, POLL_OUT); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2377 | } else { |
| 2378 | if (async->cb_func) |
| 2379 | async->cb_func(s->async->events, async->cb_arg); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2380 | } |
| 2381 | } |
| 2382 | s->async->events = 0; |
| 2383 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 2384 | EXPORT_SYMBOL_GPL(comedi_event); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2385 | |
Ian Abbott | 0777839 | 2013-04-04 14:58:51 +0100 | [diff] [blame] | 2386 | /* Note: the ->mutex is pre-locked on successful return */ |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2387 | struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2388 | { |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2389 | struct comedi_device *dev; |
Bill Pemberton | 0bfbbe8 | 2009-03-16 22:05:36 -0400 | [diff] [blame] | 2390 | struct device *csdev; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2391 | unsigned i; |
| 2392 | |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2393 | dev = kzalloc(sizeof(struct comedi_device), GFP_KERNEL); |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 2394 | if (dev == NULL) |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2395 | return ERR_PTR(-ENOMEM); |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2396 | comedi_device_init(dev); |
Ian Abbott | db2e348 | 2013-04-04 14:59:00 +0100 | [diff] [blame] | 2397 | comedi_set_hw_dev(dev, hardware_device); |
Ian Abbott | 0777839 | 2013-04-04 14:58:51 +0100 | [diff] [blame] | 2398 | mutex_lock(&dev->mutex); |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 2399 | mutex_lock(&comedi_board_minor_table_lock); |
Ian Abbott | 38b9722 | 2013-04-04 14:58:52 +0100 | [diff] [blame] | 2400 | for (i = hardware_device ? comedi_num_legacy_minors : 0; |
| 2401 | i < COMEDI_NUM_BOARD_MINORS; ++i) { |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 2402 | if (comedi_board_minor_table[i] == NULL) { |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 2403 | comedi_board_minor_table[i] = dev; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2404 | break; |
| 2405 | } |
| 2406 | } |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 2407 | mutex_unlock(&comedi_board_minor_table_lock); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2408 | if (i == COMEDI_NUM_BOARD_MINORS) { |
Ian Abbott | 0777839 | 2013-04-04 14:58:51 +0100 | [diff] [blame] | 2409 | mutex_unlock(&dev->mutex); |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2410 | comedi_device_cleanup(dev); |
| 2411 | kfree(dev); |
Ian Abbott | 4f870fe | 2012-08-16 14:38:05 +0100 | [diff] [blame] | 2412 | pr_err("comedi: error: ran out of minor numbers for board device files.\n"); |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2413 | return ERR_PTR(-EBUSY); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2414 | } |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2415 | dev->minor = i; |
Pavel Roskin | 0435f93 | 2011-07-06 10:15:44 -0400 | [diff] [blame] | 2416 | csdev = device_create(comedi_class, hardware_device, |
| 2417 | MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2418 | if (!IS_ERR(csdev)) |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2419 | dev->class_dev = csdev; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 2420 | |
Ian Abbott | 0777839 | 2013-04-04 14:58:51 +0100 | [diff] [blame] | 2421 | /* Note: dev->mutex needs to be unlocked by the caller. */ |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2422 | return dev; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2423 | } |
| 2424 | |
Ian Abbott | 70f30c3 | 2013-04-04 14:58:49 +0100 | [diff] [blame] | 2425 | static void comedi_free_board_minor(unsigned minor) |
Ian Abbott | 24fb134 | 2013-04-04 14:58:46 +0100 | [diff] [blame] | 2426 | { |
| 2427 | BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS); |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 2428 | comedi_free_board_dev(comedi_clear_board_minor(minor)); |
Ian Abbott | 24fb134 | 2013-04-04 14:58:46 +0100 | [diff] [blame] | 2429 | } |
| 2430 | |
Ian Abbott | 3346b79 | 2013-04-04 14:58:47 +0100 | [diff] [blame] | 2431 | void comedi_release_hardware_device(struct device *hardware_device) |
Ian Abbott | c43435d | 2012-03-30 17:14:58 +0100 | [diff] [blame] | 2432 | { |
| 2433 | int minor; |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 2434 | struct comedi_device *dev; |
Ian Abbott | c43435d | 2012-03-30 17:14:58 +0100 | [diff] [blame] | 2435 | |
Ian Abbott | 38b9722 | 2013-04-04 14:58:52 +0100 | [diff] [blame] | 2436 | for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS; |
| 2437 | minor++) { |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 2438 | mutex_lock(&comedi_board_minor_table_lock); |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 2439 | dev = comedi_board_minor_table[minor]; |
| 2440 | if (dev && dev->hw_dev == hardware_device) { |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 2441 | comedi_board_minor_table[minor] = NULL; |
| 2442 | mutex_unlock(&comedi_board_minor_table_lock); |
Ian Abbott | cb6b79d | 2013-04-04 14:59:16 +0100 | [diff] [blame] | 2443 | comedi_free_board_dev(dev); |
Ian Abbott | 3346b79 | 2013-04-04 14:58:47 +0100 | [diff] [blame] | 2444 | break; |
Ian Abbott | c43435d | 2012-03-30 17:14:58 +0100 | [diff] [blame] | 2445 | } |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 2446 | mutex_unlock(&comedi_board_minor_table_lock); |
Ian Abbott | c43435d | 2012-03-30 17:14:58 +0100 | [diff] [blame] | 2447 | } |
Ian Abbott | c43435d | 2012-03-30 17:14:58 +0100 | [diff] [blame] | 2448 | } |
| 2449 | |
Ian Abbott | f65cc54 | 2013-02-01 10:20:30 +0000 | [diff] [blame] | 2450 | int comedi_alloc_subdevice_minor(struct comedi_subdevice *s) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2451 | { |
Ian Abbott | f65cc54 | 2013-02-01 10:20:30 +0000 | [diff] [blame] | 2452 | struct comedi_device *dev = s->device; |
Bill Pemberton | 0bfbbe8 | 2009-03-16 22:05:36 -0400 | [diff] [blame] | 2453 | struct device *csdev; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2454 | unsigned i; |
| 2455 | |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 2456 | mutex_lock(&comedi_subdevice_minor_table_lock); |
| 2457 | for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) { |
| 2458 | if (comedi_subdevice_minor_table[i] == NULL) { |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 2459 | comedi_subdevice_minor_table[i] = s; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2460 | break; |
| 2461 | } |
| 2462 | } |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 2463 | mutex_unlock(&comedi_subdevice_minor_table_lock); |
| 2464 | if (i == COMEDI_NUM_SUBDEVICE_MINORS) { |
Ian Abbott | b12da2e | 2013-02-07 16:03:01 +0000 | [diff] [blame] | 2465 | pr_err("comedi: error: ran out of minor numbers for subdevice files.\n"); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2466 | return -EBUSY; |
| 2467 | } |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 2468 | i += COMEDI_NUM_BOARD_MINORS; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2469 | s->minor = i; |
Pavel Roskin | 0435f93 | 2011-07-06 10:15:44 -0400 | [diff] [blame] | 2470 | csdev = device_create(comedi_class, dev->class_dev, |
| 2471 | MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i", |
H Hartley Sweeten | 90a35c1 | 2012-12-19 17:27:02 -0700 | [diff] [blame] | 2472 | dev->minor, s->index); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2473 | if (!IS_ERR(csdev)) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2474 | s->class_dev = csdev; |
H Hartley Sweeten | a5011a2 | 2012-05-09 09:20:08 -0700 | [diff] [blame] | 2475 | |
Ian Abbott | da71854 | 2013-02-07 16:03:00 +0000 | [diff] [blame] | 2476 | return 0; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2477 | } |
| 2478 | |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 2479 | void comedi_free_subdevice_minor(struct comedi_subdevice *s) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2480 | { |
Ian Abbott | 0fcc9d4 | 2013-04-04 14:59:13 +0100 | [diff] [blame] | 2481 | unsigned int i; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2482 | |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2483 | if (s == NULL) |
| 2484 | return; |
| 2485 | if (s->minor < 0) |
| 2486 | return; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2487 | |
| 2488 | BUG_ON(s->minor >= COMEDI_NUM_MINORS); |
Ian Abbott | 8907cf6 | 2013-04-04 14:59:03 +0100 | [diff] [blame] | 2489 | BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2490 | |
Ian Abbott | 0fcc9d4 | 2013-04-04 14:59:13 +0100 | [diff] [blame] | 2491 | i = s->minor - COMEDI_NUM_BOARD_MINORS; |
| 2492 | mutex_lock(&comedi_subdevice_minor_table_lock); |
Ian Abbott | bd5b417 | 2013-04-04 14:59:15 +0100 | [diff] [blame] | 2493 | if (s == comedi_subdevice_minor_table[i]) |
| 2494 | comedi_subdevice_minor_table[i] = NULL; |
Ian Abbott | 0fcc9d4 | 2013-04-04 14:59:13 +0100 | [diff] [blame] | 2495 | mutex_unlock(&comedi_subdevice_minor_table_lock); |
Greg Kroah-Hartman | 476b847 | 2008-11-13 17:05:58 -0800 | [diff] [blame] | 2496 | if (s->class_dev) { |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2497 | device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor)); |
| 2498 | s->class_dev = NULL; |
| 2499 | } |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 2500 | } |
H Hartley Sweeten | a578782 | 2012-12-19 15:40:59 -0700 | [diff] [blame] | 2501 | |
Ian Abbott | 682b911 | 2013-01-28 17:07:39 +0000 | [diff] [blame] | 2502 | static void comedi_cleanup_board_minors(void) |
H Hartley Sweeten | 76cca89 | 2012-12-19 15:41:42 -0700 | [diff] [blame] | 2503 | { |
| 2504 | unsigned i; |
| 2505 | |
Ian Abbott | 682b911 | 2013-01-28 17:07:39 +0000 | [diff] [blame] | 2506 | for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) |
H Hartley Sweeten | 76cca89 | 2012-12-19 15:41:42 -0700 | [diff] [blame] | 2507 | comedi_free_board_minor(i); |
| 2508 | } |
| 2509 | |
H Hartley Sweeten | 91fa0b0 | 2012-12-19 15:41:19 -0700 | [diff] [blame] | 2510 | static int __init comedi_init(void) |
| 2511 | { |
| 2512 | int i; |
| 2513 | int retval; |
| 2514 | |
| 2515 | pr_info("comedi: version " COMEDI_RELEASE " - http://www.comedi.org\n"); |
| 2516 | |
| 2517 | if (comedi_num_legacy_minors < 0 || |
| 2518 | comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) { |
| 2519 | pr_err("comedi: error: invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n", |
| 2520 | COMEDI_NUM_BOARD_MINORS); |
| 2521 | return -EINVAL; |
| 2522 | } |
| 2523 | |
H Hartley Sweeten | 91fa0b0 | 2012-12-19 15:41:19 -0700 | [diff] [blame] | 2524 | retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0), |
| 2525 | COMEDI_NUM_MINORS, "comedi"); |
| 2526 | if (retval) |
| 2527 | return -EIO; |
| 2528 | cdev_init(&comedi_cdev, &comedi_fops); |
| 2529 | comedi_cdev.owner = THIS_MODULE; |
| 2530 | kobject_set_name(&comedi_cdev.kobj, "comedi"); |
| 2531 | if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) { |
| 2532 | unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), |
| 2533 | COMEDI_NUM_MINORS); |
| 2534 | return -EIO; |
| 2535 | } |
| 2536 | comedi_class = class_create(THIS_MODULE, "comedi"); |
| 2537 | if (IS_ERR(comedi_class)) { |
| 2538 | pr_err("comedi: failed to create class\n"); |
| 2539 | cdev_del(&comedi_cdev); |
| 2540 | unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), |
| 2541 | COMEDI_NUM_MINORS); |
| 2542 | return PTR_ERR(comedi_class); |
| 2543 | } |
| 2544 | |
Greg Kroah-Hartman | e56341a | 2013-07-24 15:05:25 -0700 | [diff] [blame] | 2545 | comedi_class->dev_groups = comedi_dev_groups; |
H Hartley Sweeten | 91fa0b0 | 2012-12-19 15:41:19 -0700 | [diff] [blame] | 2546 | |
| 2547 | /* XXX requires /proc interface */ |
| 2548 | comedi_proc_init(); |
| 2549 | |
| 2550 | /* create devices files for legacy/manual use */ |
| 2551 | for (i = 0; i < comedi_num_legacy_minors; i++) { |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2552 | struct comedi_device *dev; |
| 2553 | dev = comedi_alloc_board_minor(NULL); |
| 2554 | if (IS_ERR(dev)) { |
Ian Abbott | 682b911 | 2013-01-28 17:07:39 +0000 | [diff] [blame] | 2555 | comedi_cleanup_board_minors(); |
H Hartley Sweeten | 91fa0b0 | 2012-12-19 15:41:19 -0700 | [diff] [blame] | 2556 | cdev_del(&comedi_cdev); |
| 2557 | unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), |
| 2558 | COMEDI_NUM_MINORS); |
Ian Abbott | 7638ffc | 2013-04-04 14:58:50 +0100 | [diff] [blame] | 2559 | return PTR_ERR(dev); |
Ian Abbott | 0777839 | 2013-04-04 14:58:51 +0100 | [diff] [blame] | 2560 | } else { |
| 2561 | /* comedi_alloc_board_minor() locked the mutex */ |
| 2562 | mutex_unlock(&dev->mutex); |
H Hartley Sweeten | 91fa0b0 | 2012-12-19 15:41:19 -0700 | [diff] [blame] | 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | return 0; |
| 2567 | } |
| 2568 | module_init(comedi_init); |
| 2569 | |
| 2570 | static void __exit comedi_cleanup(void) |
| 2571 | { |
| 2572 | int i; |
| 2573 | |
Ian Abbott | 682b911 | 2013-01-28 17:07:39 +0000 | [diff] [blame] | 2574 | comedi_cleanup_board_minors(); |
Ian Abbott | 5b7dba1 | 2013-04-04 14:59:04 +0100 | [diff] [blame] | 2575 | for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i) |
| 2576 | BUG_ON(comedi_board_minor_table[i]); |
| 2577 | for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) |
| 2578 | BUG_ON(comedi_subdevice_minor_table[i]); |
H Hartley Sweeten | 91fa0b0 | 2012-12-19 15:41:19 -0700 | [diff] [blame] | 2579 | |
| 2580 | class_destroy(comedi_class); |
| 2581 | cdev_del(&comedi_cdev); |
| 2582 | unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS); |
| 2583 | |
| 2584 | comedi_proc_cleanup(); |
| 2585 | } |
| 2586 | module_exit(comedi_cleanup); |
| 2587 | |
H Hartley Sweeten | a578782 | 2012-12-19 15:40:59 -0700 | [diff] [blame] | 2588 | MODULE_AUTHOR("http://www.comedi.org"); |
| 2589 | MODULE_DESCRIPTION("Comedi core module"); |
| 2590 | MODULE_LICENSE("GPL"); |