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