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