David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | kcomedilib/kcomedilib.c |
| 3 | a comedlib interface for kernel modules |
| 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 | #define __NO_VERSION__ |
| 25 | #include <linux/module.h> |
| 26 | |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/sched.h> |
| 30 | #include <linux/fcntl.h> |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/ioport.h> |
| 33 | #include <linux/mm.h> |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 34 | #include <asm/io.h> |
| 35 | |
| 36 | #include "../comedi.h" |
| 37 | #include "../comedilib.h" |
| 38 | #include "../comedidev.h" |
| 39 | |
| 40 | MODULE_AUTHOR("David Schleef <ds@schleef.org>"); |
| 41 | MODULE_DESCRIPTION("Comedi kernel library"); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 44 | void *comedi_open(const char *filename) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 45 | { |
| 46 | struct comedi_device_file_info *dev_file_info; |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 47 | struct comedi_device *dev; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 48 | unsigned int minor; |
| 49 | |
| 50 | if (strncmp(filename, "/dev/comedi", 11) != 0) |
| 51 | return NULL; |
| 52 | |
| 53 | minor = simple_strtoul(filename + 11, NULL, 0); |
| 54 | |
| 55 | if (minor >= COMEDI_NUM_BOARD_MINORS) |
| 56 | return NULL; |
| 57 | |
| 58 | dev_file_info = comedi_get_device_file_info(minor); |
Bill Pemberton | 6a98d36 | 2009-03-16 22:03:45 -0400 | [diff] [blame] | 59 | if (dev_file_info == NULL) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 60 | return NULL; |
| 61 | dev = dev_file_info->device; |
| 62 | |
Bill Pemberton | 6a98d36 | 2009-03-16 22:03:45 -0400 | [diff] [blame] | 63 | if (dev == NULL || !dev->attached) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 64 | return NULL; |
| 65 | |
| 66 | if (!try_module_get(dev->driver->module)) |
| 67 | return NULL; |
| 68 | |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 69 | return (void *)dev; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 72 | void *comedi_open_old(unsigned int minor) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 73 | { |
| 74 | struct comedi_device_file_info *dev_file_info; |
Bill Pemberton | 71b5f4f | 2009-03-16 22:05:08 -0400 | [diff] [blame] | 75 | struct comedi_device *dev; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 76 | |
| 77 | if (minor >= COMEDI_NUM_MINORS) |
| 78 | return NULL; |
| 79 | |
| 80 | dev_file_info = comedi_get_device_file_info(minor); |
Bill Pemberton | 6a98d36 | 2009-03-16 22:03:45 -0400 | [diff] [blame] | 81 | if (dev_file_info == NULL) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 82 | return NULL; |
| 83 | dev = dev_file_info->device; |
| 84 | |
Bill Pemberton | 6a98d36 | 2009-03-16 22:03:45 -0400 | [diff] [blame] | 85 | if (dev == NULL || !dev->attached) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 86 | return NULL; |
| 87 | |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 88 | return (void *)dev; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 91 | int comedi_close(void *d) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 92 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 93 | struct comedi_device *dev = (struct comedi_device *)d; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 94 | |
| 95 | module_put(dev->driver->module); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int comedi_loglevel(int newlevel) |
| 101 | { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | void comedi_perror(const char *message) |
| 106 | { |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 107 | printk("%s: unknown error\n", message); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | char *comedi_strerror(int err) |
| 111 | { |
| 112 | return "unknown error"; |
| 113 | } |
| 114 | |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 115 | int comedi_fileno(void *d) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 116 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 117 | struct comedi_device *dev = (struct comedi_device *)d; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 118 | |
| 119 | /* return something random */ |
| 120 | return dev->minor; |
| 121 | } |
| 122 | |
Bill Pemberton | ea6d0d4 | 2009-03-16 22:05:47 -0400 | [diff] [blame] | 123 | int comedi_command(void *d, struct comedi_cmd *cmd) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 124 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 125 | struct comedi_device *dev = (struct comedi_device *)d; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 126 | struct comedi_subdevice *s; |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 127 | struct comedi_async *async; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 128 | unsigned runflags; |
| 129 | |
| 130 | if (cmd->subdev >= dev->n_subdevices) |
| 131 | return -ENODEV; |
| 132 | |
| 133 | s = dev->subdevices + cmd->subdev; |
| 134 | if (s->type == COMEDI_SUBD_UNUSED) |
| 135 | return -EIO; |
| 136 | |
| 137 | async = s->async; |
| 138 | if (async == NULL) |
| 139 | return -ENODEV; |
| 140 | |
| 141 | if (s->busy) |
| 142 | return -EBUSY; |
| 143 | s->busy = d; |
| 144 | |
| 145 | if (async->cb_mask & COMEDI_CB_EOS) |
| 146 | cmd->flags |= TRIG_WAKE_EOS; |
| 147 | |
| 148 | async->cmd = *cmd; |
| 149 | |
| 150 | runflags = SRF_RUNNING; |
| 151 | |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 152 | comedi_set_subdevice_runflags(s, ~0, runflags); |
| 153 | |
| 154 | comedi_reset_async_buf(async); |
| 155 | |
| 156 | return s->do_cmd(dev, s); |
| 157 | } |
| 158 | |
Bill Pemberton | ea6d0d4 | 2009-03-16 22:05:47 -0400 | [diff] [blame] | 159 | int comedi_command_test(void *d, struct comedi_cmd *cmd) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 160 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 161 | struct comedi_device *dev = (struct comedi_device *)d; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 162 | struct comedi_subdevice *s; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 163 | |
| 164 | if (cmd->subdev >= dev->n_subdevices) |
| 165 | return -ENODEV; |
| 166 | |
| 167 | s = dev->subdevices + cmd->subdev; |
| 168 | if (s->type == COMEDI_SUBD_UNUSED) |
| 169 | return -EIO; |
| 170 | |
| 171 | if (s->async == NULL) |
| 172 | return -ENODEV; |
| 173 | |
| 174 | return s->do_cmdtest(dev, s, cmd); |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * COMEDI_INSN |
| 179 | * perform an instruction |
| 180 | */ |
Bill Pemberton | 90035c0 | 2009-03-16 22:05:53 -0400 | [diff] [blame] | 181 | int comedi_do_insn(void *d, struct comedi_insn *insn) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 182 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 183 | struct comedi_device *dev = (struct comedi_device *)d; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 184 | struct comedi_subdevice *s; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 185 | int ret = 0; |
| 186 | |
| 187 | if (insn->insn & INSN_MASK_SPECIAL) { |
| 188 | switch (insn->insn) { |
| 189 | case INSN_GTOD: |
| 190 | { |
| 191 | struct timeval tv; |
| 192 | |
| 193 | do_gettimeofday(&tv); |
| 194 | insn->data[0] = tv.tv_sec; |
| 195 | insn->data[1] = tv.tv_usec; |
| 196 | ret = 2; |
| 197 | |
| 198 | break; |
| 199 | } |
| 200 | case INSN_WAIT: |
| 201 | /* XXX isn't the value supposed to be nanosecs? */ |
| 202 | if (insn->n != 1 || insn->data[0] >= 100) { |
| 203 | ret = -EINVAL; |
| 204 | break; |
| 205 | } |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 206 | udelay(insn->data[0]); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 207 | ret = 1; |
| 208 | break; |
| 209 | case INSN_INTTRIG: |
| 210 | if (insn->n != 1) { |
| 211 | ret = -EINVAL; |
| 212 | break; |
| 213 | } |
| 214 | if (insn->subdev >= dev->n_subdevices) { |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 215 | printk("%d not usable subdevice\n", |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 216 | insn->subdev); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 217 | ret = -EINVAL; |
| 218 | break; |
| 219 | } |
| 220 | s = dev->subdevices + insn->subdev; |
| 221 | if (!s->async) { |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 222 | printk("no async\n"); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 223 | ret = -EINVAL; |
| 224 | break; |
| 225 | } |
| 226 | if (!s->async->inttrig) { |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 227 | printk("no inttrig\n"); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 228 | ret = -EAGAIN; |
| 229 | break; |
| 230 | } |
| 231 | ret = s->async->inttrig(dev, s, insn->data[0]); |
| 232 | if (ret >= 0) |
| 233 | ret = 1; |
| 234 | break; |
| 235 | default: |
| 236 | ret = -EINVAL; |
| 237 | } |
| 238 | } else { |
| 239 | /* a subdevice instruction */ |
| 240 | if (insn->subdev >= dev->n_subdevices) { |
| 241 | ret = -EINVAL; |
| 242 | goto error; |
| 243 | } |
| 244 | s = dev->subdevices + insn->subdev; |
| 245 | |
| 246 | if (s->type == COMEDI_SUBD_UNUSED) { |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 247 | printk("%d not useable subdevice\n", insn->subdev); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 248 | ret = -EIO; |
| 249 | goto error; |
| 250 | } |
| 251 | |
| 252 | /* XXX check lock */ |
| 253 | |
Bill Pemberton | 197c82b | 2009-03-16 22:03:51 -0400 | [diff] [blame] | 254 | ret = check_chanlist(s, 1, &insn->chanspec); |
| 255 | if (ret < 0) { |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 256 | printk("bad chanspec\n"); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 257 | ret = -EINVAL; |
| 258 | goto error; |
| 259 | } |
| 260 | |
| 261 | if (s->busy) { |
| 262 | ret = -EBUSY; |
| 263 | goto error; |
| 264 | } |
| 265 | s->busy = d; |
| 266 | |
| 267 | switch (insn->insn) { |
| 268 | case INSN_READ: |
| 269 | ret = s->insn_read(dev, s, insn, insn->data); |
| 270 | break; |
| 271 | case INSN_WRITE: |
| 272 | ret = s->insn_write(dev, s, insn, insn->data); |
| 273 | break; |
| 274 | case INSN_BITS: |
| 275 | ret = s->insn_bits(dev, s, insn, insn->data); |
| 276 | break; |
| 277 | case INSN_CONFIG: |
| 278 | /* XXX should check instruction length */ |
| 279 | ret = s->insn_config(dev, s, insn, insn->data); |
| 280 | break; |
| 281 | default: |
| 282 | ret = -EINVAL; |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | s->busy = NULL; |
| 287 | } |
| 288 | if (ret < 0) |
| 289 | goto error; |
| 290 | #if 0 |
| 291 | /* XXX do we want this? -- abbotti #if'ed it out for now. */ |
| 292 | if (ret != insn->n) { |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 293 | printk("BUG: result of insn != insn.n\n"); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 294 | ret = -EINVAL; |
| 295 | goto error; |
| 296 | } |
| 297 | #endif |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 298 | error: |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 299 | |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | COMEDI_LOCK |
| 305 | lock subdevice |
| 306 | |
| 307 | arg: |
| 308 | subdevice number |
| 309 | |
| 310 | reads: |
| 311 | none |
| 312 | |
| 313 | writes: |
| 314 | none |
| 315 | |
| 316 | necessary locking: |
| 317 | - ioctl/rt lock (this type) |
| 318 | - lock while subdevice busy |
| 319 | - lock while subdevice being programmed |
| 320 | |
| 321 | */ |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 322 | int comedi_lock(void *d, unsigned int subdevice) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 323 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 324 | struct comedi_device *dev = (struct comedi_device *)d; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 325 | struct comedi_subdevice *s; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 326 | unsigned long flags; |
| 327 | int ret = 0; |
| 328 | |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 329 | if (subdevice >= dev->n_subdevices) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 330 | return -EINVAL; |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 331 | |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 332 | s = dev->subdevices + subdevice; |
| 333 | |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 334 | spin_lock_irqsave(&s->spin_lock, flags); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 335 | |
| 336 | if (s->busy) { |
| 337 | ret = -EBUSY; |
| 338 | } else { |
| 339 | if (s->lock) { |
| 340 | ret = -EBUSY; |
| 341 | } else { |
| 342 | s->lock = d; |
| 343 | } |
| 344 | } |
| 345 | |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 346 | spin_unlock_irqrestore(&s->spin_lock, flags); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 347 | |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | COMEDI_UNLOCK |
| 353 | unlock subdevice |
| 354 | |
| 355 | arg: |
| 356 | subdevice number |
| 357 | |
| 358 | reads: |
| 359 | none |
| 360 | |
| 361 | writes: |
| 362 | none |
| 363 | |
| 364 | */ |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 365 | int comedi_unlock(void *d, unsigned int subdevice) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 366 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 367 | struct comedi_device *dev = (struct comedi_device *)d; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 368 | struct comedi_subdevice *s; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 369 | unsigned long flags; |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 370 | struct comedi_async *async; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 371 | int ret; |
| 372 | |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 373 | if (subdevice >= dev->n_subdevices) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 374 | return -EINVAL; |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 375 | |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 376 | s = dev->subdevices + subdevice; |
| 377 | |
| 378 | async = s->async; |
| 379 | |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 380 | spin_lock_irqsave(&s->spin_lock, flags); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 381 | |
| 382 | if (s->busy) { |
| 383 | ret = -EBUSY; |
| 384 | } else if (s->lock && s->lock != (void *)d) { |
| 385 | ret = -EACCES; |
| 386 | } else { |
| 387 | s->lock = NULL; |
| 388 | |
| 389 | if (async) { |
| 390 | async->cb_mask = 0; |
| 391 | async->cb_func = NULL; |
| 392 | async->cb_arg = NULL; |
| 393 | } |
| 394 | |
| 395 | ret = 0; |
| 396 | } |
| 397 | |
Greg Kroah-Hartman | 5f74ea1 | 2009-04-27 14:44:31 -0700 | [diff] [blame] | 398 | spin_unlock_irqrestore(&s->spin_lock, flags); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 399 | |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | COMEDI_CANCEL |
| 405 | cancel acquisition ioctl |
| 406 | |
| 407 | arg: |
| 408 | subdevice number |
| 409 | |
| 410 | reads: |
| 411 | nothing |
| 412 | |
| 413 | writes: |
| 414 | nothing |
| 415 | |
| 416 | */ |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 417 | int comedi_cancel(void *d, unsigned int subdevice) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 418 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 419 | struct comedi_device *dev = (struct comedi_device *)d; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 420 | struct comedi_subdevice *s; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 421 | int ret = 0; |
| 422 | |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 423 | if (subdevice >= dev->n_subdevices) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 424 | return -EINVAL; |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 425 | |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 426 | s = dev->subdevices + subdevice; |
| 427 | |
| 428 | if (s->lock && s->lock != d) |
| 429 | return -EACCES; |
| 430 | |
| 431 | #if 0 |
| 432 | if (!s->busy) |
| 433 | return 0; |
| 434 | |
| 435 | if (s->busy != d) |
| 436 | return -EBUSY; |
| 437 | #endif |
| 438 | |
| 439 | if (!s->cancel || !s->async) |
| 440 | return -EINVAL; |
| 441 | |
Bill Pemberton | 197c82b | 2009-03-16 22:03:51 -0400 | [diff] [blame] | 442 | ret = s->cancel(dev, s); |
| 443 | |
| 444 | if (ret) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 445 | return ret; |
| 446 | |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 447 | comedi_set_subdevice_runflags(s, SRF_RUNNING | SRF_RT, 0); |
| 448 | s->async->inttrig = NULL; |
| 449 | s->busy = NULL; |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | registration of callback functions |
| 456 | */ |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 457 | int comedi_register_callback(void *d, unsigned int subdevice, |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 458 | unsigned int mask, int (*cb) (unsigned int, |
| 459 | void *), void *arg) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 460 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 461 | struct comedi_device *dev = (struct comedi_device *)d; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 462 | struct comedi_subdevice *s; |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 463 | struct comedi_async *async; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 464 | |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 465 | if (subdevice >= dev->n_subdevices) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 466 | return -EINVAL; |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 467 | |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 468 | s = dev->subdevices + subdevice; |
| 469 | |
| 470 | async = s->async; |
| 471 | if (s->type == COMEDI_SUBD_UNUSED || !async) |
| 472 | return -EIO; |
| 473 | |
| 474 | /* are we locked? (ioctl lock) */ |
| 475 | if (s->lock && s->lock != d) |
| 476 | return -EACCES; |
| 477 | |
| 478 | /* are we busy? */ |
| 479 | if (s->busy) |
| 480 | return -EBUSY; |
| 481 | |
| 482 | if (!mask) { |
| 483 | async->cb_mask = 0; |
| 484 | async->cb_func = NULL; |
| 485 | async->cb_arg = NULL; |
| 486 | } else { |
| 487 | async->cb_mask = mask; |
| 488 | async->cb_func = cb; |
| 489 | async->cb_arg = arg; |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 495 | int comedi_poll(void *d, unsigned int subdevice) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 496 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 497 | struct comedi_device *dev = (struct comedi_device *)d; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 498 | struct comedi_subdevice *s = dev->subdevices; |
Bill Pemberton | d163679 | 2009-03-16 22:05:20 -0400 | [diff] [blame] | 499 | struct comedi_async *async; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 500 | |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 501 | if (subdevice >= dev->n_subdevices) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 502 | return -EINVAL; |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 503 | |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 504 | s = dev->subdevices + subdevice; |
| 505 | |
| 506 | async = s->async; |
| 507 | if (s->type == COMEDI_SUBD_UNUSED || !async) |
| 508 | return -EIO; |
| 509 | |
| 510 | /* are we locked? (ioctl lock) */ |
| 511 | if (s->lock && s->lock != d) |
| 512 | return -EACCES; |
| 513 | |
| 514 | /* are we running? XXX wrong? */ |
| 515 | if (!s->busy) |
| 516 | return -EIO; |
| 517 | |
| 518 | return s->poll(dev, s); |
| 519 | } |
| 520 | |
| 521 | /* WARNING: not portable */ |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 522 | int comedi_map(void *d, unsigned int subdevice, void *ptr) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 523 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 524 | struct comedi_device *dev = (struct comedi_device *)d; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 525 | struct comedi_subdevice *s; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 526 | |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 527 | if (subdevice >= dev->n_subdevices) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 528 | return -EINVAL; |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 529 | |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 530 | s = dev->subdevices + subdevice; |
| 531 | |
| 532 | if (!s->async) |
| 533 | return -EINVAL; |
| 534 | |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 535 | if (ptr) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 536 | *((void **)ptr) = s->async->prealloc_buf; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 537 | |
| 538 | /* XXX no reference counting */ |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | /* WARNING: not portable */ |
Bill Pemberton | 0b3fb27 | 2009-03-16 22:04:56 -0400 | [diff] [blame] | 544 | int comedi_unmap(void *d, unsigned int subdevice) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 545 | { |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 546 | struct comedi_device *dev = (struct comedi_device *)d; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 547 | struct comedi_subdevice *s; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 548 | |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 549 | if (subdevice >= dev->n_subdevices) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 550 | return -EINVAL; |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 551 | |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 552 | s = dev->subdevices + subdevice; |
| 553 | |
| 554 | if (!s->async) |
| 555 | return -EINVAL; |
| 556 | |
| 557 | /* XXX no reference counting */ |
| 558 | |
| 559 | return 0; |
| 560 | } |