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