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