1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * comedi/comedi_fops.c 4 * comedi kernel module 5 * 6 * COMEDI - Linux Control and Measurement Device Interface 7 * Copyright (C) 1997-2007 David A. Schleef <ds@schleef.org> 8 * compat ioctls: 9 * Author: Ian Abbott, MEV Ltd. <abbotti@mev.co.uk> 10 * Copyright (C) 2007 MEV Ltd. <http://www.mev.co.uk/> 11 */ 12 13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 15 #include <linux/module.h> 16 #include <linux/errno.h> 17 #include <linux/kernel.h> 18 #include <linux/sched/signal.h> 19 #include <linux/fcntl.h> 20 #include <linux/delay.h> 21 #include <linux/mm.h> 22 #include <linux/slab.h> 23 #include <linux/poll.h> 24 #include <linux/device.h> 25 #include <linux/fs.h> 26 #include <linux/comedi/comedidev.h> 27 #include <linux/cdev.h> 28 29 #include <linux/io.h> 30 #include <linux/uaccess.h> 31 #include <linux/compat.h> 32 33 #include "comedi_internal.h" 34 35 /* 36 * comedi_subdevice "runflags" 37 * COMEDI_SRF_RT: DEPRECATED: command is running real-time 38 * COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred 39 * since the last command was started 40 * COMEDI_SRF_RUNNING: command is running 41 * COMEDI_SRF_FREE_SPRIV: free s->private on detach 42 * 43 * COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy" 44 */ 45 #define COMEDI_SRF_RT BIT(1) 46 #define COMEDI_SRF_ERROR BIT(2) 47 #define COMEDI_SRF_RUNNING BIT(27) 48 #define COMEDI_SRF_FREE_SPRIV BIT(31) 49 50 #define COMEDI_SRF_BUSY_MASK (COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING) 51 52 /** 53 * struct comedi_file - Per-file private data for COMEDI device 54 * @dev: COMEDI device. 55 * @read_subdev: Current "read" subdevice. 56 * @write_subdev: Current "write" subdevice. 57 * @last_detach_count: Last known detach count. 58 * @last_attached: Last known attached/detached state. 59 */ 60 struct comedi_file { 61 struct comedi_device *dev; 62 struct comedi_subdevice *read_subdev; 63 struct comedi_subdevice *write_subdev; 64 unsigned int last_detach_count; 65 unsigned int last_attached:1; 66 }; 67 68 #define COMEDI_NUM_MINORS 0x100 69 #define COMEDI_NUM_SUBDEVICE_MINORS \ 70 (COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS) 71 72 static unsigned short comedi_num_legacy_minors; 73 module_param(comedi_num_legacy_minors, ushort, 0444); 74 MODULE_PARM_DESC(comedi_num_legacy_minors, 75 "number of comedi minor devices to reserve for non-auto-configured devices (default 0)" 76 ); 77 78 unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB; 79 module_param(comedi_default_buf_size_kb, uint, 0644); 80 MODULE_PARM_DESC(comedi_default_buf_size_kb, 81 "default asynchronous buffer size in KiB (default " 82 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")"); 83 84 unsigned int comedi_default_buf_maxsize_kb = 85 CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB; 86 module_param(comedi_default_buf_maxsize_kb, uint, 0644); 87 MODULE_PARM_DESC(comedi_default_buf_maxsize_kb, 88 "default maximum size of asynchronous buffer in KiB (default " 89 __MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")"); 90 91 static DEFINE_MUTEX(comedi_board_minor_table_lock); 92 static struct comedi_device 93 *comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS]; 94 95 static DEFINE_MUTEX(comedi_subdevice_minor_table_lock); 96 /* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */ 97 static struct comedi_subdevice 98 *comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS]; 99 100 static struct cdev comedi_cdev; 101 102 static void comedi_device_init(struct comedi_device *dev) 103 { 104 kref_init(&dev->refcount); 105 spin_lock_init(&dev->spinlock); 106 mutex_init(&dev->mutex); 107 init_rwsem(&dev->attach_lock); 108 dev->minor = -1; 109 } 110 111 static void comedi_dev_kref_release(struct kref *kref) 112 { 113 struct comedi_device *dev = 114 container_of(kref, struct comedi_device, refcount); 115 116 mutex_destroy(&dev->mutex); 117 put_device(dev->class_dev); 118 kfree(dev); 119 } 120 121 /** 122 * comedi_dev_put() - Release a use of a COMEDI device 123 * @dev: COMEDI device. 124 * 125 * Must be called when a user of a COMEDI device is finished with it. 126 * When the last user of the COMEDI device calls this function, the 127 * COMEDI device is destroyed. 128 * 129 * Return: 1 if the COMEDI device is destroyed by this call or @dev is 130 * NULL, otherwise return 0. Callers must not assume the COMEDI 131 * device is still valid if this function returns 0. 132 */ 133 int comedi_dev_put(struct comedi_device *dev) 134 { 135 if (dev) 136 return kref_put(&dev->refcount, comedi_dev_kref_release); 137 return 1; 138 } 139 EXPORT_SYMBOL_GPL(comedi_dev_put); 140 141 static struct comedi_device *comedi_dev_get(struct comedi_device *dev) 142 { 143 if (dev) 144 kref_get(&dev->refcount); 145 return dev; 146 } 147 148 static void comedi_device_cleanup(struct comedi_device *dev) 149 { 150 struct module *driver_module = NULL; 151 152 if (!dev) 153 return; 154 mutex_lock(&dev->mutex); 155 if (dev->attached) 156 driver_module = dev->driver->module; 157 comedi_device_detach(dev); 158 if (driver_module && dev->use_count) 159 module_put(driver_module); 160 mutex_unlock(&dev->mutex); 161 } 162 163 static bool comedi_clear_board_dev(struct comedi_device *dev) 164 { 165 unsigned int i = dev->minor; 166 bool cleared = false; 167 168 lockdep_assert_held(&dev->mutex); 169 mutex_lock(&comedi_board_minor_table_lock); 170 if (dev == comedi_board_minor_table[i]) { 171 comedi_board_minor_table[i] = NULL; 172 cleared = true; 173 } 174 mutex_unlock(&comedi_board_minor_table_lock); 175 return cleared; 176 } 177 178 static struct comedi_device *comedi_clear_board_minor(unsigned int minor) 179 { 180 struct comedi_device *dev; 181 182 mutex_lock(&comedi_board_minor_table_lock); 183 dev = comedi_board_minor_table[minor]; 184 comedi_board_minor_table[minor] = NULL; 185 mutex_unlock(&comedi_board_minor_table_lock); 186 return dev; 187 } 188 189 static struct comedi_subdevice * 190 comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned int minor) 191 { 192 struct comedi_subdevice *s; 193 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS; 194 195 mutex_lock(&comedi_subdevice_minor_table_lock); 196 s = comedi_subdevice_minor_table[i]; 197 if (s && s->device != dev) 198 s = NULL; 199 mutex_unlock(&comedi_subdevice_minor_table_lock); 200 return s; 201 } 202 203 static struct comedi_device *comedi_dev_get_from_board_minor(unsigned int minor) 204 { 205 struct comedi_device *dev; 206 207 mutex_lock(&comedi_board_minor_table_lock); 208 dev = comedi_dev_get(comedi_board_minor_table[minor]); 209 mutex_unlock(&comedi_board_minor_table_lock); 210 return dev; 211 } 212 213 static struct comedi_device * 214 comedi_dev_get_from_subdevice_minor(unsigned int minor) 215 { 216 struct comedi_device *dev; 217 struct comedi_subdevice *s; 218 unsigned int i = minor - COMEDI_NUM_BOARD_MINORS; 219 220 mutex_lock(&comedi_subdevice_minor_table_lock); 221 s = comedi_subdevice_minor_table[i]; 222 dev = comedi_dev_get(s ? s->device : NULL); 223 mutex_unlock(&comedi_subdevice_minor_table_lock); 224 return dev; 225 } 226 227 /** 228 * comedi_dev_get_from_minor() - Get COMEDI device by minor device number 229 * @minor: Minor device number. 230 * 231 * Finds the COMEDI device associated with the minor device number, if any, 232 * and increments its reference count. The COMEDI device is prevented from 233 * being freed until a matching call is made to comedi_dev_put(). 234 * 235 * Return: A pointer to the COMEDI device if it exists, with its usage 236 * reference incremented. Return NULL if no COMEDI device exists with the 237 * specified minor device number. 238 */ 239 struct comedi_device *comedi_dev_get_from_minor(unsigned int minor) 240 { 241 if (minor < COMEDI_NUM_BOARD_MINORS) 242 return comedi_dev_get_from_board_minor(minor); 243 244 return comedi_dev_get_from_subdevice_minor(minor); 245 } 246 EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor); 247 248 static struct comedi_subdevice * 249 comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor) 250 { 251 struct comedi_subdevice *s; 252 253 lockdep_assert_held(&dev->mutex); 254 if (minor >= COMEDI_NUM_BOARD_MINORS) { 255 s = comedi_subdevice_from_minor(dev, minor); 256 if (!s || (s->subdev_flags & SDF_CMD_READ)) 257 return s; 258 } 259 return dev->read_subdev; 260 } 261 262 static struct comedi_subdevice * 263 comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor) 264 { 265 struct comedi_subdevice *s; 266 267 lockdep_assert_held(&dev->mutex); 268 if (minor >= COMEDI_NUM_BOARD_MINORS) { 269 s = comedi_subdevice_from_minor(dev, minor); 270 if (!s || (s->subdev_flags & SDF_CMD_WRITE)) 271 return s; 272 } 273 return dev->write_subdev; 274 } 275 276 static void comedi_file_reset(struct file *file) 277 { 278 struct comedi_file *cfp = file->private_data; 279 struct comedi_device *dev = cfp->dev; 280 struct comedi_subdevice *s, *read_s, *write_s; 281 unsigned int minor = iminor(file_inode(file)); 282 283 read_s = dev->read_subdev; 284 write_s = dev->write_subdev; 285 if (minor >= COMEDI_NUM_BOARD_MINORS) { 286 s = comedi_subdevice_from_minor(dev, minor); 287 if (!s || s->subdev_flags & SDF_CMD_READ) 288 read_s = s; 289 if (!s || s->subdev_flags & SDF_CMD_WRITE) 290 write_s = s; 291 } 292 cfp->last_attached = dev->attached; 293 cfp->last_detach_count = dev->detach_count; 294 WRITE_ONCE(cfp->read_subdev, read_s); 295 WRITE_ONCE(cfp->write_subdev, write_s); 296 } 297 298 static void comedi_file_check(struct file *file) 299 { 300 struct comedi_file *cfp = file->private_data; 301 struct comedi_device *dev = cfp->dev; 302 303 if (cfp->last_attached != dev->attached || 304 cfp->last_detach_count != dev->detach_count) 305 comedi_file_reset(file); 306 } 307 308 static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file) 309 { 310 struct comedi_file *cfp = file->private_data; 311 312 comedi_file_check(file); 313 return READ_ONCE(cfp->read_subdev); 314 } 315 316 static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file) 317 { 318 struct comedi_file *cfp = file->private_data; 319 320 comedi_file_check(file); 321 return READ_ONCE(cfp->write_subdev); 322 } 323 324 static int resize_async_buffer(struct comedi_device *dev, 325 struct comedi_subdevice *s, 326 unsigned int new_size) 327 { 328 struct comedi_async *async = s->async; 329 int retval; 330 331 lockdep_assert_held(&dev->mutex); 332 333 if (new_size > async->max_bufsize) 334 return -EPERM; 335 336 if (s->busy) { 337 dev_dbg(dev->class_dev, 338 "subdevice is busy, cannot resize buffer\n"); 339 return -EBUSY; 340 } 341 if (comedi_buf_is_mmapped(s)) { 342 dev_dbg(dev->class_dev, 343 "subdevice is mmapped, cannot resize buffer\n"); 344 return -EBUSY; 345 } 346 347 /* make sure buffer is an integral number of pages (we round up) */ 348 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK; 349 350 retval = comedi_buf_alloc(dev, s, new_size); 351 if (retval < 0) 352 return retval; 353 354 if (s->buf_change) { 355 retval = s->buf_change(dev, s); 356 if (retval < 0) 357 return retval; 358 } 359 360 dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n", 361 s->index, async->prealloc_bufsz); 362 return 0; 363 } 364 365 /* sysfs attribute files */ 366 367 static ssize_t max_read_buffer_kb_show(struct device *csdev, 368 struct device_attribute *attr, char *buf) 369 { 370 unsigned int minor = MINOR(csdev->devt); 371 struct comedi_device *dev; 372 struct comedi_subdevice *s; 373 unsigned int size = 0; 374 375 dev = comedi_dev_get_from_minor(minor); 376 if (!dev) 377 return -ENODEV; 378 379 mutex_lock(&dev->mutex); 380 s = comedi_read_subdevice(dev, minor); 381 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) 382 size = s->async->max_bufsize / 1024; 383 mutex_unlock(&dev->mutex); 384 385 comedi_dev_put(dev); 386 return sysfs_emit(buf, "%u\n", size); 387 } 388 389 static ssize_t max_read_buffer_kb_store(struct device *csdev, 390 struct device_attribute *attr, 391 const char *buf, size_t count) 392 { 393 unsigned int minor = MINOR(csdev->devt); 394 struct comedi_device *dev; 395 struct comedi_subdevice *s; 396 unsigned int size; 397 int err; 398 399 err = kstrtouint(buf, 10, &size); 400 if (err) 401 return err; 402 if (size > (UINT_MAX / 1024)) 403 return -EINVAL; 404 size *= 1024; 405 406 dev = comedi_dev_get_from_minor(minor); 407 if (!dev) 408 return -ENODEV; 409 410 mutex_lock(&dev->mutex); 411 s = comedi_read_subdevice(dev, minor); 412 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) 413 s->async->max_bufsize = size; 414 else 415 err = -EINVAL; 416 mutex_unlock(&dev->mutex); 417 418 comedi_dev_put(dev); 419 return err ? err : count; 420 } 421 static DEVICE_ATTR_RW(max_read_buffer_kb); 422 423 static ssize_t read_buffer_kb_show(struct device *csdev, 424 struct device_attribute *attr, char *buf) 425 { 426 unsigned int minor = MINOR(csdev->devt); 427 struct comedi_device *dev; 428 struct comedi_subdevice *s; 429 unsigned int size = 0; 430 431 dev = comedi_dev_get_from_minor(minor); 432 if (!dev) 433 return -ENODEV; 434 435 mutex_lock(&dev->mutex); 436 s = comedi_read_subdevice(dev, minor); 437 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) 438 size = s->async->prealloc_bufsz / 1024; 439 mutex_unlock(&dev->mutex); 440 441 comedi_dev_put(dev); 442 return sysfs_emit(buf, "%u\n", size); 443 } 444 445 static ssize_t read_buffer_kb_store(struct device *csdev, 446 struct device_attribute *attr, 447 const char *buf, size_t count) 448 { 449 unsigned int minor = MINOR(csdev->devt); 450 struct comedi_device *dev; 451 struct comedi_subdevice *s; 452 unsigned int size; 453 int err; 454 455 err = kstrtouint(buf, 10, &size); 456 if (err) 457 return err; 458 if (size > (UINT_MAX / 1024)) 459 return -EINVAL; 460 size *= 1024; 461 462 dev = comedi_dev_get_from_minor(minor); 463 if (!dev) 464 return -ENODEV; 465 466 mutex_lock(&dev->mutex); 467 s = comedi_read_subdevice(dev, minor); 468 if (s && (s->subdev_flags & SDF_CMD_READ) && s->async) 469 err = resize_async_buffer(dev, s, size); 470 else 471 err = -EINVAL; 472 mutex_unlock(&dev->mutex); 473 474 comedi_dev_put(dev); 475 return err ? err : count; 476 } 477 static DEVICE_ATTR_RW(read_buffer_kb); 478 479 static ssize_t max_write_buffer_kb_show(struct device *csdev, 480 struct device_attribute *attr, 481 char *buf) 482 { 483 unsigned int minor = MINOR(csdev->devt); 484 struct comedi_device *dev; 485 struct comedi_subdevice *s; 486 unsigned int size = 0; 487 488 dev = comedi_dev_get_from_minor(minor); 489 if (!dev) 490 return -ENODEV; 491 492 mutex_lock(&dev->mutex); 493 s = comedi_write_subdevice(dev, minor); 494 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) 495 size = s->async->max_bufsize / 1024; 496 mutex_unlock(&dev->mutex); 497 498 comedi_dev_put(dev); 499 return sysfs_emit(buf, "%u\n", size); 500 } 501 502 static ssize_t max_write_buffer_kb_store(struct device *csdev, 503 struct device_attribute *attr, 504 const char *buf, size_t count) 505 { 506 unsigned int minor = MINOR(csdev->devt); 507 struct comedi_device *dev; 508 struct comedi_subdevice *s; 509 unsigned int size; 510 int err; 511 512 err = kstrtouint(buf, 10, &size); 513 if (err) 514 return err; 515 if (size > (UINT_MAX / 1024)) 516 return -EINVAL; 517 size *= 1024; 518 519 dev = comedi_dev_get_from_minor(minor); 520 if (!dev) 521 return -ENODEV; 522 523 mutex_lock(&dev->mutex); 524 s = comedi_write_subdevice(dev, minor); 525 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) 526 s->async->max_bufsize = size; 527 else 528 err = -EINVAL; 529 mutex_unlock(&dev->mutex); 530 531 comedi_dev_put(dev); 532 return err ? err : count; 533 } 534 static DEVICE_ATTR_RW(max_write_buffer_kb); 535 536 static ssize_t write_buffer_kb_show(struct device *csdev, 537 struct device_attribute *attr, char *buf) 538 { 539 unsigned int minor = MINOR(csdev->devt); 540 struct comedi_device *dev; 541 struct comedi_subdevice *s; 542 unsigned int size = 0; 543 544 dev = comedi_dev_get_from_minor(minor); 545 if (!dev) 546 return -ENODEV; 547 548 mutex_lock(&dev->mutex); 549 s = comedi_write_subdevice(dev, minor); 550 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) 551 size = s->async->prealloc_bufsz / 1024; 552 mutex_unlock(&dev->mutex); 553 554 comedi_dev_put(dev); 555 return sysfs_emit(buf, "%u\n", size); 556 } 557 558 static ssize_t write_buffer_kb_store(struct device *csdev, 559 struct device_attribute *attr, 560 const char *buf, size_t count) 561 { 562 unsigned int minor = MINOR(csdev->devt); 563 struct comedi_device *dev; 564 struct comedi_subdevice *s; 565 unsigned int size; 566 int err; 567 568 err = kstrtouint(buf, 10, &size); 569 if (err) 570 return err; 571 if (size > (UINT_MAX / 1024)) 572 return -EINVAL; 573 size *= 1024; 574 575 dev = comedi_dev_get_from_minor(minor); 576 if (!dev) 577 return -ENODEV; 578 579 mutex_lock(&dev->mutex); 580 s = comedi_write_subdevice(dev, minor); 581 if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async) 582 err = resize_async_buffer(dev, s, size); 583 else 584 err = -EINVAL; 585 mutex_unlock(&dev->mutex); 586 587 comedi_dev_put(dev); 588 return err ? err : count; 589 } 590 static DEVICE_ATTR_RW(write_buffer_kb); 591 592 static struct attribute *comedi_dev_attrs[] = { 593 &dev_attr_max_read_buffer_kb.attr, 594 &dev_attr_read_buffer_kb.attr, 595 &dev_attr_max_write_buffer_kb.attr, 596 &dev_attr_write_buffer_kb.attr, 597 NULL, 598 }; 599 ATTRIBUTE_GROUPS(comedi_dev); 600 601 static const struct class comedi_class = { 602 .name = "comedi", 603 .dev_groups = comedi_dev_groups, 604 }; 605 606 static void comedi_free_board_dev(struct comedi_device *dev) 607 { 608 if (dev) { 609 comedi_device_cleanup(dev); 610 if (dev->class_dev) { 611 device_destroy(&comedi_class, 612 MKDEV(COMEDI_MAJOR, dev->minor)); 613 } 614 comedi_dev_put(dev); 615 } 616 } 617 618 static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s, 619 unsigned int bits) 620 { 621 s->runflags &= ~bits; 622 } 623 624 static void __comedi_set_subdevice_runflags(struct comedi_subdevice *s, 625 unsigned int bits) 626 { 627 s->runflags |= bits; 628 } 629 630 static void comedi_update_subdevice_runflags(struct comedi_subdevice *s, 631 unsigned int mask, 632 unsigned int bits) 633 { 634 unsigned long flags; 635 636 spin_lock_irqsave(&s->spin_lock, flags); 637 __comedi_clear_subdevice_runflags(s, mask); 638 __comedi_set_subdevice_runflags(s, bits & mask); 639 spin_unlock_irqrestore(&s->spin_lock, flags); 640 } 641 642 static unsigned int __comedi_get_subdevice_runflags(struct comedi_subdevice *s) 643 { 644 return s->runflags; 645 } 646 647 static unsigned int comedi_get_subdevice_runflags(struct comedi_subdevice *s) 648 { 649 unsigned long flags; 650 unsigned int runflags; 651 652 spin_lock_irqsave(&s->spin_lock, flags); 653 runflags = __comedi_get_subdevice_runflags(s); 654 spin_unlock_irqrestore(&s->spin_lock, flags); 655 return runflags; 656 } 657 658 static bool comedi_is_runflags_running(unsigned int runflags) 659 { 660 return runflags & COMEDI_SRF_RUNNING; 661 } 662 663 static bool comedi_is_runflags_in_error(unsigned int runflags) 664 { 665 return runflags & COMEDI_SRF_ERROR; 666 } 667 668 /** 669 * comedi_is_subdevice_running() - Check if async command running on subdevice 670 * @s: COMEDI subdevice. 671 * 672 * Return: %true if an asynchronous COMEDI command is active on the 673 * subdevice, else %false. 674 */ 675 bool comedi_is_subdevice_running(struct comedi_subdevice *s) 676 { 677 unsigned int runflags = comedi_get_subdevice_runflags(s); 678 679 return comedi_is_runflags_running(runflags); 680 } 681 EXPORT_SYMBOL_GPL(comedi_is_subdevice_running); 682 683 static bool __comedi_is_subdevice_running(struct comedi_subdevice *s) 684 { 685 unsigned int runflags = __comedi_get_subdevice_runflags(s); 686 687 return comedi_is_runflags_running(runflags); 688 } 689 690 bool comedi_can_auto_free_spriv(struct comedi_subdevice *s) 691 { 692 unsigned int runflags = __comedi_get_subdevice_runflags(s); 693 694 return runflags & COMEDI_SRF_FREE_SPRIV; 695 } 696 697 /** 698 * comedi_set_spriv_auto_free() - Mark subdevice private data as freeable 699 * @s: COMEDI subdevice. 700 * 701 * Mark the subdevice as having a pointer to private data that can be 702 * automatically freed when the COMEDI device is detached from the low-level 703 * driver. 704 */ 705 void comedi_set_spriv_auto_free(struct comedi_subdevice *s) 706 { 707 __comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV); 708 } 709 EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free); 710 711 /** 712 * comedi_alloc_spriv - Allocate memory for the subdevice private data 713 * @s: COMEDI subdevice. 714 * @size: Size of the memory to allocate. 715 * 716 * Allocate memory for the subdevice private data and point @s->private 717 * to it. The memory will be freed automatically when the COMEDI device 718 * is detached from the low-level driver. 719 * 720 * Return: A pointer to the allocated memory @s->private on success. 721 * Return NULL on failure. 722 */ 723 void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size) 724 { 725 s->private = kzalloc(size, GFP_KERNEL); 726 if (s->private) 727 comedi_set_spriv_auto_free(s); 728 return s->private; 729 } 730 EXPORT_SYMBOL_GPL(comedi_alloc_spriv); 731 732 /* 733 * This function restores a subdevice to an idle state. 734 */ 735 static void do_become_nonbusy(struct comedi_device *dev, 736 struct comedi_subdevice *s) 737 { 738 struct comedi_async *async = s->async; 739 740 lockdep_assert_held(&dev->mutex); 741 comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0); 742 if (async) { 743 comedi_buf_reset(s); 744 async->inttrig = NULL; 745 kfree(async->cmd.chanlist); 746 async->cmd.chanlist = NULL; 747 s->busy = NULL; 748 wake_up_interruptible_all(&async->wait_head); 749 } else { 750 dev_err(dev->class_dev, 751 "BUG: (?) %s called with async=NULL\n", __func__); 752 s->busy = NULL; 753 } 754 } 755 756 static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s) 757 { 758 int ret = 0; 759 760 lockdep_assert_held(&dev->mutex); 761 if (comedi_is_subdevice_running(s) && s->cancel) 762 ret = s->cancel(dev, s); 763 764 do_become_nonbusy(dev, s); 765 766 return ret; 767 } 768 769 void comedi_device_cancel_all(struct comedi_device *dev) 770 { 771 struct comedi_subdevice *s; 772 int i; 773 774 lockdep_assert_held(&dev->mutex); 775 if (!dev->attached) 776 return; 777 778 for (i = 0; i < dev->n_subdevices; i++) { 779 s = &dev->subdevices[i]; 780 if (s->async) 781 do_cancel(dev, s); 782 } 783 } 784 785 static int is_device_busy(struct comedi_device *dev) 786 { 787 struct comedi_subdevice *s; 788 int i; 789 790 lockdep_assert_held_write(&dev->attach_lock); 791 lockdep_assert_held(&dev->mutex); 792 if (!dev->attached) 793 return 0; 794 795 for (i = 0; i < dev->n_subdevices; i++) { 796 s = &dev->subdevices[i]; 797 if (s->busy) 798 return 1; 799 if (!s->async) 800 continue; 801 if (comedi_buf_is_mmapped(s)) 802 return 1; 803 /* 804 * There may be tasks still waiting on the subdevice's wait 805 * queue, although they should already be about to be removed 806 * from it since the subdevice has no active async command. 807 */ 808 if (wq_has_sleeper(&s->async->wait_head)) 809 return 1; 810 } 811 812 return 0; 813 } 814 815 /* 816 * COMEDI_DEVCONFIG ioctl 817 * attaches (and configures) or detaches a legacy device 818 * 819 * arg: 820 * pointer to comedi_devconfig structure (NULL if detaching) 821 * 822 * reads: 823 * comedi_devconfig structure (if attaching) 824 * 825 * writes: 826 * nothing 827 */ 828 static int do_devconfig_ioctl(struct comedi_device *dev, 829 struct comedi_devconfig __user *arg) 830 { 831 struct comedi_devconfig it; 832 833 lockdep_assert_held(&dev->mutex); 834 if (!capable(CAP_SYS_ADMIN)) 835 return -EPERM; 836 837 if (!arg) { 838 int rc = 0; 839 840 if (dev->attached) { 841 down_write(&dev->attach_lock); 842 if (is_device_busy(dev)) { 843 rc = -EBUSY; 844 } else { 845 struct module *driver_module = 846 dev->driver->module; 847 848 comedi_device_detach_locked(dev); 849 module_put(driver_module); 850 } 851 up_write(&dev->attach_lock); 852 } 853 return rc; 854 } 855 856 if (copy_from_user(&it, arg, sizeof(it))) 857 return -EFAULT; 858 859 it.board_name[COMEDI_NAMELEN - 1] = 0; 860 861 if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) { 862 dev_warn(dev->class_dev, 863 "comedi_config --init_data is deprecated\n"); 864 return -EINVAL; 865 } 866 867 if (dev->minor >= comedi_num_legacy_minors) 868 /* don't re-use dynamically allocated comedi devices */ 869 return -EBUSY; 870 871 /* This increments the driver module count on success. */ 872 return comedi_device_attach(dev, &it); 873 } 874 875 /* 876 * COMEDI_BUFCONFIG ioctl 877 * buffer configuration 878 * 879 * arg: 880 * pointer to comedi_bufconfig structure 881 * 882 * reads: 883 * comedi_bufconfig structure 884 * 885 * writes: 886 * modified comedi_bufconfig structure 887 */ 888 static int do_bufconfig_ioctl(struct comedi_device *dev, 889 struct comedi_bufconfig __user *arg) 890 { 891 struct comedi_bufconfig bc; 892 struct comedi_async *async; 893 struct comedi_subdevice *s; 894 int retval = 0; 895 896 lockdep_assert_held(&dev->mutex); 897 if (copy_from_user(&bc, arg, sizeof(bc))) 898 return -EFAULT; 899 900 if (bc.subdevice >= dev->n_subdevices) 901 return -EINVAL; 902 903 s = &dev->subdevices[bc.subdevice]; 904 async = s->async; 905 906 if (!async) { 907 dev_dbg(dev->class_dev, 908 "subdevice does not have async capability\n"); 909 bc.size = 0; 910 bc.maximum_size = 0; 911 goto copyback; 912 } 913 914 if (bc.maximum_size) { 915 if (!capable(CAP_SYS_ADMIN)) 916 return -EPERM; 917 918 async->max_bufsize = bc.maximum_size; 919 } 920 921 if (bc.size) { 922 retval = resize_async_buffer(dev, s, bc.size); 923 if (retval < 0) 924 return retval; 925 } 926 927 bc.size = async->prealloc_bufsz; 928 bc.maximum_size = async->max_bufsize; 929 930 copyback: 931 if (copy_to_user(arg, &bc, sizeof(bc))) 932 return -EFAULT; 933 934 return 0; 935 } 936 937 /* 938 * COMEDI_DEVINFO ioctl 939 * device info 940 * 941 * arg: 942 * pointer to comedi_devinfo structure 943 * 944 * reads: 945 * nothing 946 * 947 * writes: 948 * comedi_devinfo structure 949 */ 950 static int do_devinfo_ioctl(struct comedi_device *dev, 951 struct comedi_devinfo __user *arg, 952 struct file *file) 953 { 954 struct comedi_subdevice *s; 955 struct comedi_devinfo devinfo; 956 957 lockdep_assert_held(&dev->mutex); 958 memset(&devinfo, 0, sizeof(devinfo)); 959 960 /* fill devinfo structure */ 961 devinfo.version_code = COMEDI_VERSION_CODE; 962 devinfo.n_subdevs = dev->n_subdevices; 963 strscpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN); 964 strscpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN); 965 966 s = comedi_file_read_subdevice(file); 967 if (s) 968 devinfo.read_subdevice = s->index; 969 else 970 devinfo.read_subdevice = -1; 971 972 s = comedi_file_write_subdevice(file); 973 if (s) 974 devinfo.write_subdevice = s->index; 975 else 976 devinfo.write_subdevice = -1; 977 978 if (copy_to_user(arg, &devinfo, sizeof(devinfo))) 979 return -EFAULT; 980 981 return 0; 982 } 983 984 /* 985 * COMEDI_SUBDINFO ioctl 986 * subdevices info 987 * 988 * arg: 989 * pointer to array of comedi_subdinfo structures 990 * 991 * reads: 992 * nothing 993 * 994 * writes: 995 * array of comedi_subdinfo structures 996 */ 997 static int do_subdinfo_ioctl(struct comedi_device *dev, 998 struct comedi_subdinfo __user *arg, void *file) 999 { 1000 int ret, i; 1001 struct comedi_subdinfo *tmp, *us; 1002 struct comedi_subdevice *s; 1003 1004 lockdep_assert_held(&dev->mutex); 1005 tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL); 1006 if (!tmp) 1007 return -ENOMEM; 1008 1009 /* fill subdinfo structs */ 1010 for (i = 0; i < dev->n_subdevices; i++) { 1011 s = &dev->subdevices[i]; 1012 us = tmp + i; 1013 1014 us->type = s->type; 1015 us->n_chan = s->n_chan; 1016 us->subd_flags = s->subdev_flags; 1017 if (comedi_is_subdevice_running(s)) 1018 us->subd_flags |= SDF_RUNNING; 1019 #define TIMER_nanosec 5 /* backwards compatibility */ 1020 us->timer_type = TIMER_nanosec; 1021 us->len_chanlist = s->len_chanlist; 1022 us->maxdata = s->maxdata; 1023 if (s->range_table) { 1024 us->range_type = 1025 (i << 24) | (0 << 16) | (s->range_table->length); 1026 } else { 1027 us->range_type = 0; /* XXX */ 1028 } 1029 1030 if (s->busy) 1031 us->subd_flags |= SDF_BUSY; 1032 if (s->busy == file) 1033 us->subd_flags |= SDF_BUSY_OWNER; 1034 if (s->lock) 1035 us->subd_flags |= SDF_LOCKED; 1036 if (s->lock == file) 1037 us->subd_flags |= SDF_LOCK_OWNER; 1038 if (!s->maxdata && s->maxdata_list) 1039 us->subd_flags |= SDF_MAXDATA; 1040 if (s->range_table_list) 1041 us->subd_flags |= SDF_RANGETYPE; 1042 if (s->do_cmd) 1043 us->subd_flags |= SDF_CMD; 1044 1045 if (s->insn_bits != &insn_inval) 1046 us->insn_bits_support = COMEDI_SUPPORTED; 1047 else 1048 us->insn_bits_support = COMEDI_UNSUPPORTED; 1049 } 1050 1051 ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp)); 1052 1053 kfree(tmp); 1054 1055 return ret ? -EFAULT : 0; 1056 } 1057 1058 /* 1059 * COMEDI_CHANINFO ioctl 1060 * subdevice channel info 1061 * 1062 * arg: 1063 * pointer to comedi_chaninfo structure 1064 * 1065 * reads: 1066 * comedi_chaninfo structure 1067 * 1068 * writes: 1069 * array of maxdata values to chaninfo->maxdata_list if requested 1070 * array of range table lengths to chaninfo->range_table_list if requested 1071 */ 1072 static int do_chaninfo_ioctl(struct comedi_device *dev, 1073 struct comedi_chaninfo *it) 1074 { 1075 struct comedi_subdevice *s; 1076 1077 lockdep_assert_held(&dev->mutex); 1078 1079 if (it->subdev >= dev->n_subdevices) 1080 return -EINVAL; 1081 s = &dev->subdevices[it->subdev]; 1082 1083 if (it->maxdata_list) { 1084 if (s->maxdata || !s->maxdata_list) 1085 return -EINVAL; 1086 if (copy_to_user(it->maxdata_list, s->maxdata_list, 1087 s->n_chan * sizeof(unsigned int))) 1088 return -EFAULT; 1089 } 1090 1091 if (it->flaglist) 1092 return -EINVAL; /* flaglist not supported */ 1093 1094 if (it->rangelist) { 1095 int i; 1096 1097 if (!s->range_table_list) 1098 return -EINVAL; 1099 for (i = 0; i < s->n_chan; i++) { 1100 int x; 1101 1102 x = (dev->minor << 28) | (it->subdev << 24) | (i << 16) | 1103 (s->range_table_list[i]->length); 1104 if (put_user(x, it->rangelist + i)) 1105 return -EFAULT; 1106 } 1107 } 1108 1109 return 0; 1110 } 1111 1112 /* 1113 * COMEDI_BUFINFO ioctl 1114 * buffer information 1115 * 1116 * arg: 1117 * pointer to comedi_bufinfo structure 1118 * 1119 * reads: 1120 * comedi_bufinfo structure 1121 * 1122 * writes: 1123 * modified comedi_bufinfo structure 1124 */ 1125 static int do_bufinfo_ioctl(struct comedi_device *dev, 1126 struct comedi_bufinfo __user *arg, void *file) 1127 { 1128 struct comedi_bufinfo bi; 1129 struct comedi_subdevice *s; 1130 struct comedi_async *async; 1131 unsigned int runflags; 1132 int retval = 0; 1133 bool become_nonbusy = false; 1134 1135 lockdep_assert_held(&dev->mutex); 1136 if (copy_from_user(&bi, arg, sizeof(bi))) 1137 return -EFAULT; 1138 1139 if (bi.subdevice >= dev->n_subdevices) 1140 return -EINVAL; 1141 1142 s = &dev->subdevices[bi.subdevice]; 1143 1144 async = s->async; 1145 1146 if (!async || s->busy != file) 1147 return -EINVAL; 1148 1149 runflags = comedi_get_subdevice_runflags(s); 1150 if (!(async->cmd.flags & CMDF_WRITE)) { 1151 /* command was set up in "read" direction */ 1152 if (bi.bytes_read) { 1153 comedi_buf_read_alloc(s, bi.bytes_read); 1154 bi.bytes_read = comedi_buf_read_free(s, bi.bytes_read); 1155 } 1156 /* 1157 * If nothing left to read, and command has stopped, and 1158 * {"read" position not updated or command stopped normally}, 1159 * then become non-busy. 1160 */ 1161 if (comedi_buf_read_n_available(s) == 0 && 1162 !comedi_is_runflags_running(runflags) && 1163 (bi.bytes_read == 0 || 1164 !comedi_is_runflags_in_error(runflags))) { 1165 become_nonbusy = true; 1166 if (comedi_is_runflags_in_error(runflags)) 1167 retval = -EPIPE; 1168 } 1169 bi.bytes_written = 0; 1170 } else { 1171 /* command was set up in "write" direction */ 1172 if (!comedi_is_runflags_running(runflags)) { 1173 bi.bytes_written = 0; 1174 become_nonbusy = true; 1175 if (comedi_is_runflags_in_error(runflags)) 1176 retval = -EPIPE; 1177 } else if (bi.bytes_written) { 1178 comedi_buf_write_alloc(s, bi.bytes_written); 1179 bi.bytes_written = 1180 comedi_buf_write_free(s, bi.bytes_written); 1181 } 1182 bi.bytes_read = 0; 1183 } 1184 1185 bi.buf_write_count = async->buf_write_count; 1186 bi.buf_write_ptr = async->buf_write_ptr; 1187 bi.buf_read_count = async->buf_read_count; 1188 bi.buf_read_ptr = async->buf_read_ptr; 1189 1190 if (become_nonbusy) 1191 do_become_nonbusy(dev, s); 1192 1193 if (retval) 1194 return retval; 1195 1196 if (copy_to_user(arg, &bi, sizeof(bi))) 1197 return -EFAULT; 1198 1199 return 0; 1200 } 1201 1202 static int check_insn_config_length(struct comedi_insn *insn, 1203 unsigned int *data) 1204 { 1205 if (insn->n < 1) 1206 return -EINVAL; 1207 1208 switch (data[0]) { 1209 case INSN_CONFIG_DIO_OUTPUT: 1210 case INSN_CONFIG_DIO_INPUT: 1211 case INSN_CONFIG_DISARM: 1212 case INSN_CONFIG_RESET: 1213 if (insn->n == 1) 1214 return 0; 1215 break; 1216 case INSN_CONFIG_ARM: 1217 case INSN_CONFIG_DIO_QUERY: 1218 case INSN_CONFIG_BLOCK_SIZE: 1219 case INSN_CONFIG_FILTER: 1220 case INSN_CONFIG_SERIAL_CLOCK: 1221 case INSN_CONFIG_BIDIRECTIONAL_DATA: 1222 case INSN_CONFIG_ALT_SOURCE: 1223 case INSN_CONFIG_SET_COUNTER_MODE: 1224 case INSN_CONFIG_8254_READ_STATUS: 1225 case INSN_CONFIG_SET_ROUTING: 1226 case INSN_CONFIG_GET_ROUTING: 1227 case INSN_CONFIG_GET_PWM_STATUS: 1228 case INSN_CONFIG_PWM_SET_PERIOD: 1229 case INSN_CONFIG_PWM_GET_PERIOD: 1230 if (insn->n == 2) 1231 return 0; 1232 break; 1233 case INSN_CONFIG_SET_GATE_SRC: 1234 case INSN_CONFIG_GET_GATE_SRC: 1235 case INSN_CONFIG_SET_CLOCK_SRC: 1236 case INSN_CONFIG_GET_CLOCK_SRC: 1237 case INSN_CONFIG_SET_OTHER_SRC: 1238 case INSN_CONFIG_GET_COUNTER_STATUS: 1239 case INSN_CONFIG_GET_PWM_OUTPUT: 1240 case INSN_CONFIG_PWM_SET_H_BRIDGE: 1241 case INSN_CONFIG_PWM_GET_H_BRIDGE: 1242 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE: 1243 if (insn->n == 3) 1244 return 0; 1245 break; 1246 case INSN_CONFIG_PWM_OUTPUT: 1247 case INSN_CONFIG_ANALOG_TRIG: 1248 case INSN_CONFIG_TIMER_1: 1249 if (insn->n == 5) 1250 return 0; 1251 break; 1252 case INSN_CONFIG_DIGITAL_TRIG: 1253 if (insn->n == 6) 1254 return 0; 1255 break; 1256 case INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS: 1257 if (insn->n >= 4) 1258 return 0; 1259 break; 1260 /* 1261 * by default we allow the insn since we don't have checks for 1262 * all possible cases yet 1263 */ 1264 default: 1265 pr_warn("No check for data length of config insn id %i is implemented\n", 1266 data[0]); 1267 pr_warn("Add a check to %s in %s\n", __func__, __FILE__); 1268 pr_warn("Assuming n=%i is correct\n", insn->n); 1269 return 0; 1270 } 1271 return -EINVAL; 1272 } 1273 1274 static int check_insn_device_config_length(struct comedi_insn *insn, 1275 unsigned int *data) 1276 { 1277 if (insn->n < 1) 1278 return -EINVAL; 1279 1280 switch (data[0]) { 1281 case INSN_DEVICE_CONFIG_TEST_ROUTE: 1282 case INSN_DEVICE_CONFIG_CONNECT_ROUTE: 1283 case INSN_DEVICE_CONFIG_DISCONNECT_ROUTE: 1284 if (insn->n == 3) 1285 return 0; 1286 break; 1287 case INSN_DEVICE_CONFIG_GET_ROUTES: 1288 /* 1289 * Big enough for config_id and the length of the userland 1290 * memory buffer. Additional length should be in factors of 2 1291 * to communicate any returned route pairs (source,destination). 1292 */ 1293 if (insn->n >= 2) 1294 return 0; 1295 break; 1296 } 1297 return -EINVAL; 1298 } 1299 1300 /** 1301 * get_valid_routes() - Calls low-level driver get_valid_routes function to 1302 * either return a count of valid routes to user, or copy 1303 * of list of all valid device routes to buffer in 1304 * userspace. 1305 * @dev: comedi device pointer 1306 * @data: data from user insn call. The length of the data must be >= 2. 1307 * data[0] must contain the INSN_DEVICE_CONFIG config_id. 1308 * data[1](input) contains the number of _pairs_ for which memory is 1309 * allotted from the user. If the user specifies '0', then only 1310 * the number of pairs available is returned. 1311 * data[1](output) returns either the number of pairs available (if none 1312 * where requested) or the number of _pairs_ that are copied back 1313 * to the user. 1314 * data[2::2] returns each (source, destination) pair. 1315 * 1316 * Return: -EINVAL if low-level driver does not allocate and return routes as 1317 * expected. Returns 0 otherwise. 1318 */ 1319 static int get_valid_routes(struct comedi_device *dev, unsigned int *data) 1320 { 1321 lockdep_assert_held(&dev->mutex); 1322 data[1] = dev->get_valid_routes(dev, data[1], data + 2); 1323 return 0; 1324 } 1325 1326 static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn, 1327 unsigned int *data, void *file) 1328 { 1329 struct comedi_subdevice *s; 1330 int ret = 0; 1331 int i; 1332 1333 lockdep_assert_held(&dev->mutex); 1334 if (insn->insn & INSN_MASK_SPECIAL) { 1335 /* a non-subdevice instruction */ 1336 1337 switch (insn->insn) { 1338 case INSN_GTOD: 1339 { 1340 struct timespec64 tv; 1341 1342 if (insn->n != 2) { 1343 ret = -EINVAL; 1344 break; 1345 } 1346 1347 ktime_get_real_ts64(&tv); 1348 /* unsigned data safe until 2106 */ 1349 data[0] = (unsigned int)tv.tv_sec; 1350 data[1] = tv.tv_nsec / NSEC_PER_USEC; 1351 ret = 2; 1352 1353 break; 1354 } 1355 case INSN_WAIT: 1356 if (insn->n != 1 || data[0] >= 100000) { 1357 ret = -EINVAL; 1358 break; 1359 } 1360 udelay(data[0] / 1000); 1361 ret = 1; 1362 break; 1363 case INSN_INTTRIG: 1364 if (insn->n != 1) { 1365 ret = -EINVAL; 1366 break; 1367 } 1368 if (insn->subdev >= dev->n_subdevices) { 1369 dev_dbg(dev->class_dev, 1370 "%d not usable subdevice\n", 1371 insn->subdev); 1372 ret = -EINVAL; 1373 break; 1374 } 1375 s = &dev->subdevices[insn->subdev]; 1376 if (!s->async) { 1377 dev_dbg(dev->class_dev, "no async\n"); 1378 ret = -EINVAL; 1379 break; 1380 } 1381 if (!s->async->inttrig) { 1382 dev_dbg(dev->class_dev, "no inttrig\n"); 1383 ret = -EAGAIN; 1384 break; 1385 } 1386 ret = s->async->inttrig(dev, s, data[0]); 1387 if (ret >= 0) 1388 ret = 1; 1389 break; 1390 case INSN_DEVICE_CONFIG: 1391 ret = check_insn_device_config_length(insn, data); 1392 if (ret) 1393 break; 1394 1395 if (data[0] == INSN_DEVICE_CONFIG_GET_ROUTES) { 1396 /* 1397 * data[1] should be the number of _pairs_ that 1398 * the memory can hold. 1399 */ 1400 data[1] = (insn->n - 2) / 2; 1401 ret = get_valid_routes(dev, data); 1402 break; 1403 } 1404 1405 /* other global device config instructions. */ 1406 ret = dev->insn_device_config(dev, insn, data); 1407 break; 1408 default: 1409 dev_dbg(dev->class_dev, "invalid insn\n"); 1410 ret = -EINVAL; 1411 break; 1412 } 1413 } else { 1414 /* a subdevice instruction */ 1415 unsigned int maxdata; 1416 1417 if (insn->subdev >= dev->n_subdevices) { 1418 dev_dbg(dev->class_dev, "subdevice %d out of range\n", 1419 insn->subdev); 1420 ret = -EINVAL; 1421 goto out; 1422 } 1423 s = &dev->subdevices[insn->subdev]; 1424 1425 if (s->type == COMEDI_SUBD_UNUSED) { 1426 dev_dbg(dev->class_dev, "%d not usable subdevice\n", 1427 insn->subdev); 1428 ret = -EIO; 1429 goto out; 1430 } 1431 1432 /* are we locked? (ioctl lock) */ 1433 if (s->lock && s->lock != file) { 1434 dev_dbg(dev->class_dev, "device locked\n"); 1435 ret = -EACCES; 1436 goto out; 1437 } 1438 1439 ret = comedi_check_chanlist(s, 1, &insn->chanspec); 1440 if (ret < 0) { 1441 ret = -EINVAL; 1442 dev_dbg(dev->class_dev, "bad chanspec\n"); 1443 goto out; 1444 } 1445 1446 if (s->busy) { 1447 ret = -EBUSY; 1448 goto out; 1449 } 1450 /* This looks arbitrary. It is. */ 1451 s->busy = parse_insn; 1452 switch (insn->insn) { 1453 case INSN_READ: 1454 ret = s->insn_read(dev, s, insn, data); 1455 if (ret == -ETIMEDOUT) { 1456 dev_dbg(dev->class_dev, 1457 "subdevice %d read instruction timed out\n", 1458 s->index); 1459 } 1460 break; 1461 case INSN_WRITE: 1462 maxdata = s->maxdata_list 1463 ? s->maxdata_list[CR_CHAN(insn->chanspec)] 1464 : s->maxdata; 1465 for (i = 0; i < insn->n; ++i) { 1466 if (data[i] > maxdata) { 1467 ret = -EINVAL; 1468 dev_dbg(dev->class_dev, 1469 "bad data value(s)\n"); 1470 break; 1471 } 1472 } 1473 if (ret == 0) { 1474 ret = s->insn_write(dev, s, insn, data); 1475 if (ret == -ETIMEDOUT) { 1476 dev_dbg(dev->class_dev, 1477 "subdevice %d write instruction timed out\n", 1478 s->index); 1479 } 1480 } 1481 break; 1482 case INSN_BITS: 1483 if (insn->n != 2) { 1484 ret = -EINVAL; 1485 } else { 1486 /* 1487 * Most drivers ignore the base channel in 1488 * insn->chanspec. Fix this here if 1489 * the subdevice has <= 32 channels. 1490 */ 1491 unsigned int orig_mask = data[0]; 1492 unsigned int shift = 0; 1493 1494 if (s->n_chan <= 32) { 1495 shift = CR_CHAN(insn->chanspec); 1496 if (shift > 0) { 1497 insn->chanspec = 0; 1498 data[0] <<= shift; 1499 data[1] <<= shift; 1500 } 1501 } 1502 ret = s->insn_bits(dev, s, insn, data); 1503 data[0] = orig_mask; 1504 if (shift > 0) 1505 data[1] >>= shift; 1506 } 1507 break; 1508 case INSN_CONFIG: 1509 ret = check_insn_config_length(insn, data); 1510 if (ret) 1511 break; 1512 ret = s->insn_config(dev, s, insn, data); 1513 break; 1514 default: 1515 ret = -EINVAL; 1516 break; 1517 } 1518 1519 s->busy = NULL; 1520 } 1521 1522 out: 1523 return ret; 1524 } 1525 1526 /* 1527 * COMEDI_INSNLIST ioctl 1528 * synchronous instruction list 1529 * 1530 * arg: 1531 * pointer to comedi_insnlist structure 1532 * 1533 * reads: 1534 * comedi_insnlist structure 1535 * array of comedi_insn structures from insnlist->insns pointer 1536 * data (for writes) from insns[].data pointers 1537 * 1538 * writes: 1539 * data (for reads) to insns[].data pointers 1540 */ 1541 /* arbitrary limits */ 1542 #define MIN_SAMPLES 16 1543 #define MAX_SAMPLES 65536 1544 static int do_insnlist_ioctl(struct comedi_device *dev, 1545 struct comedi_insn *insns, 1546 unsigned int n_insns, 1547 void *file) 1548 { 1549 unsigned int *data = NULL; 1550 unsigned int max_n_data_required = MIN_SAMPLES; 1551 int i = 0; 1552 int ret = 0; 1553 1554 lockdep_assert_held(&dev->mutex); 1555 1556 /* Determine maximum memory needed for all instructions. */ 1557 for (i = 0; i < n_insns; ++i) { 1558 if (insns[i].n > MAX_SAMPLES) { 1559 dev_dbg(dev->class_dev, 1560 "number of samples too large\n"); 1561 ret = -EINVAL; 1562 goto error; 1563 } 1564 max_n_data_required = max(max_n_data_required, insns[i].n); 1565 } 1566 1567 /* Allocate scratch space for all instruction data. */ 1568 data = kmalloc_array(max_n_data_required, sizeof(unsigned int), 1569 GFP_KERNEL); 1570 if (!data) { 1571 ret = -ENOMEM; 1572 goto error; 1573 } 1574 1575 for (i = 0; i < n_insns; ++i) { 1576 unsigned int n = insns[i].n; 1577 1578 if (insns[i].insn & INSN_MASK_WRITE) { 1579 if (copy_from_user(data, insns[i].data, 1580 n * sizeof(unsigned int))) { 1581 dev_dbg(dev->class_dev, 1582 "copy_from_user failed\n"); 1583 ret = -EFAULT; 1584 goto error; 1585 } 1586 if (n < MIN_SAMPLES) { 1587 memset(&data[n], 0, (MIN_SAMPLES - n) * 1588 sizeof(unsigned int)); 1589 } 1590 } 1591 ret = parse_insn(dev, insns + i, data, file); 1592 if (ret < 0) 1593 goto error; 1594 if (insns[i].insn & INSN_MASK_READ) { 1595 if (copy_to_user(insns[i].data, data, 1596 n * sizeof(unsigned int))) { 1597 dev_dbg(dev->class_dev, 1598 "copy_to_user failed\n"); 1599 ret = -EFAULT; 1600 goto error; 1601 } 1602 } 1603 if (need_resched()) 1604 schedule(); 1605 } 1606 1607 error: 1608 kfree(data); 1609 1610 if (ret < 0) 1611 return ret; 1612 return i; 1613 } 1614 1615 #define MAX_INSNS MAX_SAMPLES 1616 static int check_insnlist_len(struct comedi_device *dev, unsigned int n_insns) 1617 { 1618 if (n_insns > MAX_INSNS) { 1619 dev_dbg(dev->class_dev, "insnlist length too large\n"); 1620 return -EINVAL; 1621 } 1622 return 0; 1623 } 1624 1625 /* 1626 * COMEDI_INSN ioctl 1627 * synchronous instruction 1628 * 1629 * arg: 1630 * pointer to comedi_insn structure 1631 * 1632 * reads: 1633 * comedi_insn structure 1634 * data (for writes) from insn->data pointer 1635 * 1636 * writes: 1637 * data (for reads) to insn->data pointer 1638 */ 1639 static int do_insn_ioctl(struct comedi_device *dev, 1640 struct comedi_insn *insn, void *file) 1641 { 1642 unsigned int *data = NULL; 1643 unsigned int n_data = MIN_SAMPLES; 1644 int ret = 0; 1645 1646 lockdep_assert_held(&dev->mutex); 1647 1648 n_data = max(n_data, insn->n); 1649 1650 /* This is where the behavior of insn and insnlist deviate. */ 1651 if (insn->n > MAX_SAMPLES) { 1652 insn->n = MAX_SAMPLES; 1653 n_data = MAX_SAMPLES; 1654 } 1655 1656 data = kmalloc_array(n_data, sizeof(unsigned int), GFP_KERNEL); 1657 if (!data) { 1658 ret = -ENOMEM; 1659 goto error; 1660 } 1661 1662 if (insn->insn & INSN_MASK_WRITE) { 1663 if (copy_from_user(data, 1664 insn->data, 1665 insn->n * sizeof(unsigned int))) { 1666 ret = -EFAULT; 1667 goto error; 1668 } 1669 if (insn->n < MIN_SAMPLES) { 1670 memset(&data[insn->n], 0, 1671 (MIN_SAMPLES - insn->n) * sizeof(unsigned int)); 1672 } 1673 } 1674 ret = parse_insn(dev, insn, data, file); 1675 if (ret < 0) 1676 goto error; 1677 if (insn->insn & INSN_MASK_READ) { 1678 if (copy_to_user(insn->data, 1679 data, 1680 insn->n * sizeof(unsigned int))) { 1681 ret = -EFAULT; 1682 goto error; 1683 } 1684 } 1685 ret = insn->n; 1686 1687 error: 1688 kfree(data); 1689 1690 return ret; 1691 } 1692 1693 static int __comedi_get_user_cmd(struct comedi_device *dev, 1694 struct comedi_cmd *cmd) 1695 { 1696 struct comedi_subdevice *s; 1697 1698 lockdep_assert_held(&dev->mutex); 1699 if (cmd->subdev >= dev->n_subdevices) { 1700 dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev); 1701 return -ENODEV; 1702 } 1703 1704 s = &dev->subdevices[cmd->subdev]; 1705 1706 if (s->type == COMEDI_SUBD_UNUSED) { 1707 dev_dbg(dev->class_dev, "%d not valid subdevice\n", 1708 cmd->subdev); 1709 return -EIO; 1710 } 1711 1712 if (!s->do_cmd || !s->do_cmdtest || !s->async) { 1713 dev_dbg(dev->class_dev, 1714 "subdevice %d does not support commands\n", 1715 cmd->subdev); 1716 return -EIO; 1717 } 1718 1719 /* make sure channel/gain list isn't too long */ 1720 if (cmd->chanlist_len > s->len_chanlist) { 1721 dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n", 1722 cmd->chanlist_len, s->len_chanlist); 1723 return -EINVAL; 1724 } 1725 1726 /* 1727 * Set the CMDF_WRITE flag to the correct state if the subdevice 1728 * supports only "read" commands or only "write" commands. 1729 */ 1730 switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) { 1731 case SDF_CMD_READ: 1732 cmd->flags &= ~CMDF_WRITE; 1733 break; 1734 case SDF_CMD_WRITE: 1735 cmd->flags |= CMDF_WRITE; 1736 break; 1737 default: 1738 break; 1739 } 1740 1741 return 0; 1742 } 1743 1744 static int __comedi_get_user_chanlist(struct comedi_device *dev, 1745 struct comedi_subdevice *s, 1746 unsigned int __user *user_chanlist, 1747 struct comedi_cmd *cmd) 1748 { 1749 unsigned int *chanlist; 1750 int ret; 1751 1752 lockdep_assert_held(&dev->mutex); 1753 cmd->chanlist = NULL; 1754 chanlist = memdup_array_user(user_chanlist, 1755 cmd->chanlist_len, sizeof(unsigned int)); 1756 if (IS_ERR(chanlist)) 1757 return PTR_ERR(chanlist); 1758 1759 /* make sure each element in channel/gain list is valid */ 1760 ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist); 1761 if (ret < 0) { 1762 kfree(chanlist); 1763 return ret; 1764 } 1765 1766 cmd->chanlist = chanlist; 1767 1768 return 0; 1769 } 1770 1771 /* 1772 * COMEDI_CMD ioctl 1773 * asynchronous acquisition command set-up 1774 * 1775 * arg: 1776 * pointer to comedi_cmd structure 1777 * 1778 * reads: 1779 * comedi_cmd structure 1780 * channel/range list from cmd->chanlist pointer 1781 * 1782 * writes: 1783 * possibly modified comedi_cmd structure (when -EAGAIN returned) 1784 */ 1785 static int do_cmd_ioctl(struct comedi_device *dev, 1786 struct comedi_cmd *cmd, bool *copy, void *file) 1787 { 1788 struct comedi_subdevice *s; 1789 struct comedi_async *async; 1790 unsigned int __user *user_chanlist; 1791 int ret; 1792 1793 lockdep_assert_held(&dev->mutex); 1794 1795 /* do some simple cmd validation */ 1796 ret = __comedi_get_user_cmd(dev, cmd); 1797 if (ret) 1798 return ret; 1799 1800 /* save user's chanlist pointer so it can be restored later */ 1801 user_chanlist = (unsigned int __user *)cmd->chanlist; 1802 1803 s = &dev->subdevices[cmd->subdev]; 1804 async = s->async; 1805 1806 /* are we locked? (ioctl lock) */ 1807 if (s->lock && s->lock != file) { 1808 dev_dbg(dev->class_dev, "subdevice locked\n"); 1809 return -EACCES; 1810 } 1811 1812 /* are we busy? */ 1813 if (s->busy) { 1814 dev_dbg(dev->class_dev, "subdevice busy\n"); 1815 return -EBUSY; 1816 } 1817 1818 /* make sure channel/gain list isn't too short */ 1819 if (cmd->chanlist_len < 1) { 1820 dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n", 1821 cmd->chanlist_len); 1822 return -EINVAL; 1823 } 1824 1825 async->cmd = *cmd; 1826 async->cmd.data = NULL; 1827 1828 /* load channel/gain list */ 1829 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd); 1830 if (ret) 1831 goto cleanup; 1832 1833 ret = s->do_cmdtest(dev, s, &async->cmd); 1834 1835 if (async->cmd.flags & CMDF_BOGUS || ret) { 1836 dev_dbg(dev->class_dev, "test returned %d\n", ret); 1837 *cmd = async->cmd; 1838 /* restore chanlist pointer before copying back */ 1839 cmd->chanlist = (unsigned int __force *)user_chanlist; 1840 cmd->data = NULL; 1841 *copy = true; 1842 ret = -EAGAIN; 1843 goto cleanup; 1844 } 1845 1846 if (!async->prealloc_bufsz) { 1847 ret = -ENOMEM; 1848 dev_dbg(dev->class_dev, "no buffer (?)\n"); 1849 goto cleanup; 1850 } 1851 1852 comedi_buf_reset(s); 1853 1854 async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK; 1855 if (async->cmd.flags & CMDF_WAKE_EOS) 1856 async->cb_mask |= COMEDI_CB_EOS; 1857 1858 comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK, 1859 COMEDI_SRF_RUNNING); 1860 1861 /* 1862 * Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid 1863 * race with comedi_read() or comedi_write(). 1864 */ 1865 s->busy = file; 1866 ret = s->do_cmd(dev, s); 1867 if (ret == 0) 1868 return 0; 1869 1870 cleanup: 1871 do_become_nonbusy(dev, s); 1872 1873 return ret; 1874 } 1875 1876 /* 1877 * COMEDI_CMDTEST ioctl 1878 * asynchronous acquisition command testing 1879 * 1880 * arg: 1881 * pointer to comedi_cmd structure 1882 * 1883 * reads: 1884 * comedi_cmd structure 1885 * channel/range list from cmd->chanlist pointer 1886 * 1887 * writes: 1888 * possibly modified comedi_cmd structure 1889 */ 1890 static int do_cmdtest_ioctl(struct comedi_device *dev, 1891 struct comedi_cmd *cmd, bool *copy, void *file) 1892 { 1893 struct comedi_subdevice *s; 1894 unsigned int __user *user_chanlist; 1895 int ret; 1896 1897 lockdep_assert_held(&dev->mutex); 1898 1899 /* do some simple cmd validation */ 1900 ret = __comedi_get_user_cmd(dev, cmd); 1901 if (ret) 1902 return ret; 1903 1904 /* save user's chanlist pointer so it can be restored later */ 1905 user_chanlist = (unsigned int __user *)cmd->chanlist; 1906 1907 s = &dev->subdevices[cmd->subdev]; 1908 1909 /* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */ 1910 if (user_chanlist) { 1911 /* load channel/gain list */ 1912 ret = __comedi_get_user_chanlist(dev, s, user_chanlist, cmd); 1913 if (ret) 1914 return ret; 1915 } 1916 1917 ret = s->do_cmdtest(dev, s, cmd); 1918 1919 kfree(cmd->chanlist); /* free kernel copy of user chanlist */ 1920 1921 /* restore chanlist pointer before copying back */ 1922 cmd->chanlist = (unsigned int __force *)user_chanlist; 1923 *copy = true; 1924 1925 return ret; 1926 } 1927 1928 /* 1929 * COMEDI_LOCK ioctl 1930 * lock subdevice 1931 * 1932 * arg: 1933 * subdevice number 1934 * 1935 * reads: 1936 * nothing 1937 * 1938 * writes: 1939 * nothing 1940 */ 1941 static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg, 1942 void *file) 1943 { 1944 int ret = 0; 1945 unsigned long flags; 1946 struct comedi_subdevice *s; 1947 1948 lockdep_assert_held(&dev->mutex); 1949 if (arg >= dev->n_subdevices) 1950 return -EINVAL; 1951 s = &dev->subdevices[arg]; 1952 1953 spin_lock_irqsave(&s->spin_lock, flags); 1954 if (s->busy || s->lock) 1955 ret = -EBUSY; 1956 else 1957 s->lock = file; 1958 spin_unlock_irqrestore(&s->spin_lock, flags); 1959 1960 return ret; 1961 } 1962 1963 /* 1964 * COMEDI_UNLOCK ioctl 1965 * unlock subdevice 1966 * 1967 * arg: 1968 * subdevice number 1969 * 1970 * reads: 1971 * nothing 1972 * 1973 * writes: 1974 * nothing 1975 */ 1976 static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg, 1977 void *file) 1978 { 1979 struct comedi_subdevice *s; 1980 1981 lockdep_assert_held(&dev->mutex); 1982 if (arg >= dev->n_subdevices) 1983 return -EINVAL; 1984 s = &dev->subdevices[arg]; 1985 1986 if (s->busy) 1987 return -EBUSY; 1988 1989 if (s->lock && s->lock != file) 1990 return -EACCES; 1991 1992 if (s->lock == file) 1993 s->lock = NULL; 1994 1995 return 0; 1996 } 1997 1998 /* 1999 * COMEDI_CANCEL ioctl 2000 * cancel asynchronous acquisition 2001 * 2002 * arg: 2003 * subdevice number 2004 * 2005 * reads: 2006 * nothing 2007 * 2008 * writes: 2009 * nothing 2010 */ 2011 static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg, 2012 void *file) 2013 { 2014 struct comedi_subdevice *s; 2015 2016 lockdep_assert_held(&dev->mutex); 2017 if (arg >= dev->n_subdevices) 2018 return -EINVAL; 2019 s = &dev->subdevices[arg]; 2020 if (!s->async) 2021 return -EINVAL; 2022 2023 if (!s->busy) 2024 return 0; 2025 2026 if (s->busy != file) 2027 return -EBUSY; 2028 2029 return do_cancel(dev, s); 2030 } 2031 2032 /* 2033 * COMEDI_POLL ioctl 2034 * instructs driver to synchronize buffers 2035 * 2036 * arg: 2037 * subdevice number 2038 * 2039 * reads: 2040 * nothing 2041 * 2042 * writes: 2043 * nothing 2044 */ 2045 static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg, 2046 void *file) 2047 { 2048 struct comedi_subdevice *s; 2049 2050 lockdep_assert_held(&dev->mutex); 2051 if (arg >= dev->n_subdevices) 2052 return -EINVAL; 2053 s = &dev->subdevices[arg]; 2054 2055 if (!s->busy) 2056 return 0; 2057 2058 if (s->busy != file) 2059 return -EBUSY; 2060 2061 if (s->poll) 2062 return s->poll(dev, s); 2063 2064 return -EINVAL; 2065 } 2066 2067 /* 2068 * COMEDI_SETRSUBD ioctl 2069 * sets the current "read" subdevice on a per-file basis 2070 * 2071 * arg: 2072 * subdevice number 2073 * 2074 * reads: 2075 * nothing 2076 * 2077 * writes: 2078 * nothing 2079 */ 2080 static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg, 2081 struct file *file) 2082 { 2083 struct comedi_file *cfp = file->private_data; 2084 struct comedi_subdevice *s_old, *s_new; 2085 2086 lockdep_assert_held(&dev->mutex); 2087 if (arg >= dev->n_subdevices) 2088 return -EINVAL; 2089 2090 s_new = &dev->subdevices[arg]; 2091 s_old = comedi_file_read_subdevice(file); 2092 if (s_old == s_new) 2093 return 0; /* no change */ 2094 2095 if (!(s_new->subdev_flags & SDF_CMD_READ)) 2096 return -EINVAL; 2097 2098 /* 2099 * Check the file isn't still busy handling a "read" command on the 2100 * old subdevice (if any). 2101 */ 2102 if (s_old && s_old->busy == file && s_old->async && 2103 !(s_old->async->cmd.flags & CMDF_WRITE)) 2104 return -EBUSY; 2105 2106 WRITE_ONCE(cfp->read_subdev, s_new); 2107 return 0; 2108 } 2109 2110 /* 2111 * COMEDI_SETWSUBD ioctl 2112 * sets the current "write" subdevice on a per-file basis 2113 * 2114 * arg: 2115 * subdevice number 2116 * 2117 * reads: 2118 * nothing 2119 * 2120 * writes: 2121 * nothing 2122 */ 2123 static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg, 2124 struct file *file) 2125 { 2126 struct comedi_file *cfp = file->private_data; 2127 struct comedi_subdevice *s_old, *s_new; 2128 2129 lockdep_assert_held(&dev->mutex); 2130 if (arg >= dev->n_subdevices) 2131 return -EINVAL; 2132 2133 s_new = &dev->subdevices[arg]; 2134 s_old = comedi_file_write_subdevice(file); 2135 if (s_old == s_new) 2136 return 0; /* no change */ 2137 2138 if (!(s_new->subdev_flags & SDF_CMD_WRITE)) 2139 return -EINVAL; 2140 2141 /* 2142 * Check the file isn't still busy handling a "write" command on the 2143 * old subdevice (if any). 2144 */ 2145 if (s_old && s_old->busy == file && s_old->async && 2146 (s_old->async->cmd.flags & CMDF_WRITE)) 2147 return -EBUSY; 2148 2149 WRITE_ONCE(cfp->write_subdev, s_new); 2150 return 0; 2151 } 2152 2153 static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd, 2154 unsigned long arg) 2155 { 2156 unsigned int minor = iminor(file_inode(file)); 2157 struct comedi_file *cfp = file->private_data; 2158 struct comedi_device *dev = cfp->dev; 2159 int rc; 2160 2161 mutex_lock(&dev->mutex); 2162 2163 /* 2164 * Device config is special, because it must work on 2165 * an unconfigured device. 2166 */ 2167 if (cmd == COMEDI_DEVCONFIG) { 2168 if (minor >= COMEDI_NUM_BOARD_MINORS) { 2169 /* Device config not appropriate on non-board minors. */ 2170 rc = -ENOTTY; 2171 goto done; 2172 } 2173 rc = do_devconfig_ioctl(dev, 2174 (struct comedi_devconfig __user *)arg); 2175 if (rc == 0) { 2176 if (arg == 0 && 2177 dev->minor >= comedi_num_legacy_minors) { 2178 /* 2179 * Successfully unconfigured a dynamically 2180 * allocated device. Try and remove it. 2181 */ 2182 if (comedi_clear_board_dev(dev)) { 2183 mutex_unlock(&dev->mutex); 2184 comedi_free_board_dev(dev); 2185 return rc; 2186 } 2187 } 2188 } 2189 goto done; 2190 } 2191 2192 if (!dev->attached) { 2193 dev_dbg(dev->class_dev, "no driver attached\n"); 2194 rc = -ENODEV; 2195 goto done; 2196 } 2197 2198 switch (cmd) { 2199 case COMEDI_BUFCONFIG: 2200 rc = do_bufconfig_ioctl(dev, 2201 (struct comedi_bufconfig __user *)arg); 2202 break; 2203 case COMEDI_DEVINFO: 2204 rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg, 2205 file); 2206 break; 2207 case COMEDI_SUBDINFO: 2208 rc = do_subdinfo_ioctl(dev, 2209 (struct comedi_subdinfo __user *)arg, 2210 file); 2211 break; 2212 case COMEDI_CHANINFO: { 2213 struct comedi_chaninfo it; 2214 2215 if (copy_from_user(&it, (void __user *)arg, sizeof(it))) 2216 rc = -EFAULT; 2217 else 2218 rc = do_chaninfo_ioctl(dev, &it); 2219 break; 2220 } 2221 case COMEDI_RANGEINFO: { 2222 struct comedi_rangeinfo it; 2223 2224 if (copy_from_user(&it, (void __user *)arg, sizeof(it))) 2225 rc = -EFAULT; 2226 else 2227 rc = do_rangeinfo_ioctl(dev, &it); 2228 break; 2229 } 2230 case COMEDI_BUFINFO: 2231 rc = do_bufinfo_ioctl(dev, 2232 (struct comedi_bufinfo __user *)arg, 2233 file); 2234 break; 2235 case COMEDI_LOCK: 2236 rc = do_lock_ioctl(dev, arg, file); 2237 break; 2238 case COMEDI_UNLOCK: 2239 rc = do_unlock_ioctl(dev, arg, file); 2240 break; 2241 case COMEDI_CANCEL: 2242 rc = do_cancel_ioctl(dev, arg, file); 2243 break; 2244 case COMEDI_CMD: { 2245 struct comedi_cmd cmd; 2246 bool copy = false; 2247 2248 if (copy_from_user(&cmd, (void __user *)arg, sizeof(cmd))) { 2249 rc = -EFAULT; 2250 break; 2251 } 2252 rc = do_cmd_ioctl(dev, &cmd, ©, file); 2253 if (copy && copy_to_user((void __user *)arg, &cmd, sizeof(cmd))) 2254 rc = -EFAULT; 2255 break; 2256 } 2257 case COMEDI_CMDTEST: { 2258 struct comedi_cmd cmd; 2259 bool copy = false; 2260 2261 if (copy_from_user(&cmd, (void __user *)arg, sizeof(cmd))) { 2262 rc = -EFAULT; 2263 break; 2264 } 2265 rc = do_cmdtest_ioctl(dev, &cmd, ©, file); 2266 if (copy && copy_to_user((void __user *)arg, &cmd, sizeof(cmd))) 2267 rc = -EFAULT; 2268 break; 2269 } 2270 case COMEDI_INSNLIST: { 2271 struct comedi_insnlist insnlist; 2272 struct comedi_insn *insns = NULL; 2273 2274 if (copy_from_user(&insnlist, (void __user *)arg, 2275 sizeof(insnlist))) { 2276 rc = -EFAULT; 2277 break; 2278 } 2279 rc = check_insnlist_len(dev, insnlist.n_insns); 2280 if (rc) 2281 break; 2282 insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL); 2283 if (!insns) { 2284 rc = -ENOMEM; 2285 break; 2286 } 2287 if (copy_from_user(insns, insnlist.insns, 2288 sizeof(*insns) * insnlist.n_insns)) { 2289 rc = -EFAULT; 2290 kfree(insns); 2291 break; 2292 } 2293 rc = do_insnlist_ioctl(dev, insns, insnlist.n_insns, file); 2294 kfree(insns); 2295 break; 2296 } 2297 case COMEDI_INSN: { 2298 struct comedi_insn insn; 2299 2300 if (copy_from_user(&insn, (void __user *)arg, sizeof(insn))) 2301 rc = -EFAULT; 2302 else 2303 rc = do_insn_ioctl(dev, &insn, file); 2304 break; 2305 } 2306 case COMEDI_POLL: 2307 rc = do_poll_ioctl(dev, arg, file); 2308 break; 2309 case COMEDI_SETRSUBD: 2310 rc = do_setrsubd_ioctl(dev, arg, file); 2311 break; 2312 case COMEDI_SETWSUBD: 2313 rc = do_setwsubd_ioctl(dev, arg, file); 2314 break; 2315 default: 2316 rc = -ENOTTY; 2317 break; 2318 } 2319 2320 done: 2321 mutex_unlock(&dev->mutex); 2322 return rc; 2323 } 2324 2325 static void comedi_vm_open(struct vm_area_struct *area) 2326 { 2327 struct comedi_buf_map *bm; 2328 2329 bm = area->vm_private_data; 2330 comedi_buf_map_get(bm); 2331 } 2332 2333 static void comedi_vm_close(struct vm_area_struct *area) 2334 { 2335 struct comedi_buf_map *bm; 2336 2337 bm = area->vm_private_data; 2338 comedi_buf_map_put(bm); 2339 } 2340 2341 static int comedi_vm_access(struct vm_area_struct *vma, unsigned long addr, 2342 void *buf, int len, int write) 2343 { 2344 struct comedi_buf_map *bm = vma->vm_private_data; 2345 unsigned long offset = 2346 addr - vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT); 2347 2348 if (len < 0) 2349 return -EINVAL; 2350 if (len > vma->vm_end - addr) 2351 len = vma->vm_end - addr; 2352 return comedi_buf_map_access(bm, offset, buf, len, write); 2353 } 2354 2355 static const struct vm_operations_struct comedi_vm_ops = { 2356 .open = comedi_vm_open, 2357 .close = comedi_vm_close, 2358 .access = comedi_vm_access, 2359 }; 2360 2361 static int comedi_mmap(struct file *file, struct vm_area_struct *vma) 2362 { 2363 struct comedi_file *cfp = file->private_data; 2364 struct comedi_device *dev = cfp->dev; 2365 struct comedi_subdevice *s; 2366 struct comedi_async *async; 2367 struct comedi_buf_map *bm = NULL; 2368 struct comedi_buf_page *buf; 2369 unsigned long start = vma->vm_start; 2370 unsigned long size; 2371 int n_pages; 2372 int i; 2373 int retval = 0; 2374 2375 /* 2376 * 'trylock' avoids circular dependency with current->mm->mmap_lock 2377 * and down-reading &dev->attach_lock should normally succeed without 2378 * contention unless the device is in the process of being attached 2379 * or detached. 2380 */ 2381 if (!down_read_trylock(&dev->attach_lock)) 2382 return -EAGAIN; 2383 2384 if (!dev->attached) { 2385 dev_dbg(dev->class_dev, "no driver attached\n"); 2386 retval = -ENODEV; 2387 goto done; 2388 } 2389 2390 if (vma->vm_flags & VM_WRITE) 2391 s = comedi_file_write_subdevice(file); 2392 else 2393 s = comedi_file_read_subdevice(file); 2394 if (!s) { 2395 retval = -EINVAL; 2396 goto done; 2397 } 2398 2399 async = s->async; 2400 if (!async) { 2401 retval = -EINVAL; 2402 goto done; 2403 } 2404 2405 if (vma->vm_pgoff != 0) { 2406 dev_dbg(dev->class_dev, "mmap() offset must be 0.\n"); 2407 retval = -EINVAL; 2408 goto done; 2409 } 2410 2411 size = vma->vm_end - vma->vm_start; 2412 if (size > async->prealloc_bufsz) { 2413 retval = -EFAULT; 2414 goto done; 2415 } 2416 if (offset_in_page(size)) { 2417 retval = -EFAULT; 2418 goto done; 2419 } 2420 2421 n_pages = vma_pages(vma); 2422 2423 /* get reference to current buf map (if any) */ 2424 bm = comedi_buf_map_from_subdev_get(s); 2425 if (!bm || n_pages > bm->n_pages) { 2426 retval = -EINVAL; 2427 goto done; 2428 } 2429 if (bm->dma_dir != DMA_NONE) { 2430 unsigned long vm_start = vma->vm_start; 2431 unsigned long vm_end = vma->vm_end; 2432 2433 /* 2434 * Buffer pages are not contiguous, so temporarily modify VMA 2435 * start and end addresses for each buffer page. 2436 */ 2437 for (i = 0; i < n_pages; ++i) { 2438 buf = &bm->page_list[i]; 2439 vma->vm_start = start; 2440 vma->vm_end = start + PAGE_SIZE; 2441 retval = dma_mmap_coherent(bm->dma_hw_dev, vma, 2442 buf->virt_addr, 2443 buf->dma_addr, PAGE_SIZE); 2444 if (retval) 2445 break; 2446 2447 start += PAGE_SIZE; 2448 } 2449 vma->vm_start = vm_start; 2450 vma->vm_end = vm_end; 2451 } else { 2452 for (i = 0; i < n_pages; ++i) { 2453 unsigned long pfn; 2454 2455 buf = &bm->page_list[i]; 2456 pfn = page_to_pfn(virt_to_page(buf->virt_addr)); 2457 retval = remap_pfn_range(vma, start, pfn, PAGE_SIZE, 2458 PAGE_SHARED); 2459 if (retval) 2460 break; 2461 2462 start += PAGE_SIZE; 2463 } 2464 } 2465 2466 #ifdef CONFIG_MMU 2467 /* 2468 * Leaving behind a partial mapping of a buffer we're about to drop is 2469 * unsafe, see remap_pfn_range_notrack(). We need to zap the range 2470 * here ourselves instead of relying on the automatic zapping in 2471 * remap_pfn_range() because we call remap_pfn_range() in a loop. 2472 */ 2473 if (retval) 2474 zap_vma_ptes(vma, vma->vm_start, size); 2475 #endif 2476 2477 if (retval == 0) { 2478 vma->vm_ops = &comedi_vm_ops; 2479 vma->vm_private_data = bm; 2480 2481 vma->vm_ops->open(vma); 2482 } 2483 2484 done: 2485 up_read(&dev->attach_lock); 2486 comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */ 2487 return retval; 2488 } 2489 2490 static __poll_t comedi_poll(struct file *file, poll_table *wait) 2491 { 2492 __poll_t mask = 0; 2493 struct comedi_file *cfp = file->private_data; 2494 struct comedi_device *dev = cfp->dev; 2495 struct comedi_subdevice *s, *s_read; 2496 2497 down_read(&dev->attach_lock); 2498 2499 if (!dev->attached) { 2500 dev_dbg(dev->class_dev, "no driver attached\n"); 2501 goto done; 2502 } 2503 2504 s = comedi_file_read_subdevice(file); 2505 s_read = s; 2506 if (s && s->async) { 2507 poll_wait(file, &s->async->wait_head, wait); 2508 if (s->busy != file || !comedi_is_subdevice_running(s) || 2509 (s->async->cmd.flags & CMDF_WRITE) || 2510 comedi_buf_read_n_available(s) > 0) 2511 mask |= EPOLLIN | EPOLLRDNORM; 2512 } 2513 2514 s = comedi_file_write_subdevice(file); 2515 if (s && s->async) { 2516 unsigned int bps = comedi_bytes_per_sample(s); 2517 2518 if (s != s_read) 2519 poll_wait(file, &s->async->wait_head, wait); 2520 if (s->busy != file || !comedi_is_subdevice_running(s) || 2521 !(s->async->cmd.flags & CMDF_WRITE) || 2522 comedi_buf_write_n_available(s) >= bps) 2523 mask |= EPOLLOUT | EPOLLWRNORM; 2524 } 2525 2526 done: 2527 up_read(&dev->attach_lock); 2528 return mask; 2529 } 2530 2531 static unsigned int comedi_buf_copy_to_user(struct comedi_subdevice *s, 2532 void __user *dest, unsigned int src_offset, unsigned int n) 2533 { 2534 struct comedi_buf_map *bm = s->async->buf_map; 2535 struct comedi_buf_page *buf_page_list = bm->page_list; 2536 unsigned int page = src_offset >> PAGE_SHIFT; 2537 unsigned int offset = offset_in_page(src_offset); 2538 2539 while (n) { 2540 unsigned int copy_amount = min(n, PAGE_SIZE - offset); 2541 unsigned int uncopied; 2542 2543 uncopied = copy_to_user(dest, buf_page_list[page].virt_addr + 2544 offset, copy_amount); 2545 copy_amount -= uncopied; 2546 n -= copy_amount; 2547 if (uncopied) 2548 break; 2549 2550 dest += copy_amount; 2551 page++; 2552 if (page == bm->n_pages) 2553 page = 0; /* buffer wraparound */ 2554 offset = 0; 2555 } 2556 return n; 2557 } 2558 2559 static unsigned int comedi_buf_copy_from_user(struct comedi_subdevice *s, 2560 unsigned int dst_offset, const void __user *src, unsigned int n) 2561 { 2562 struct comedi_buf_map *bm = s->async->buf_map; 2563 struct comedi_buf_page *buf_page_list = bm->page_list; 2564 unsigned int page = dst_offset >> PAGE_SHIFT; 2565 unsigned int offset = offset_in_page(dst_offset); 2566 2567 while (n) { 2568 unsigned int copy_amount = min(n, PAGE_SIZE - offset); 2569 unsigned int uncopied; 2570 2571 uncopied = copy_from_user(buf_page_list[page].virt_addr + 2572 offset, src, copy_amount); 2573 copy_amount -= uncopied; 2574 n -= copy_amount; 2575 if (uncopied) 2576 break; 2577 2578 src += copy_amount; 2579 page++; 2580 if (page == bm->n_pages) 2581 page = 0; /* buffer wraparound */ 2582 offset = 0; 2583 } 2584 return n; 2585 } 2586 2587 static ssize_t comedi_write(struct file *file, const char __user *buf, 2588 size_t nbytes, loff_t *offset) 2589 { 2590 struct comedi_subdevice *s; 2591 struct comedi_async *async; 2592 unsigned int n, m; 2593 ssize_t count = 0; 2594 int retval = 0; 2595 DECLARE_WAITQUEUE(wait, current); 2596 struct comedi_file *cfp = file->private_data; 2597 struct comedi_device *dev = cfp->dev; 2598 bool become_nonbusy = false; 2599 bool attach_locked; 2600 unsigned int old_detach_count; 2601 2602 /* Protect against device detachment during operation. */ 2603 down_read(&dev->attach_lock); 2604 attach_locked = true; 2605 old_detach_count = dev->detach_count; 2606 2607 if (!dev->attached) { 2608 dev_dbg(dev->class_dev, "no driver attached\n"); 2609 retval = -ENODEV; 2610 goto out; 2611 } 2612 2613 s = comedi_file_write_subdevice(file); 2614 if (!s || !s->async) { 2615 retval = -EIO; 2616 goto out; 2617 } 2618 2619 async = s->async; 2620 if (s->busy != file || !(async->cmd.flags & CMDF_WRITE)) { 2621 retval = -EINVAL; 2622 goto out; 2623 } 2624 2625 add_wait_queue(&async->wait_head, &wait); 2626 while (count == 0 && !retval) { 2627 unsigned int runflags; 2628 2629 set_current_state(TASK_INTERRUPTIBLE); 2630 2631 runflags = comedi_get_subdevice_runflags(s); 2632 if (!comedi_is_runflags_running(runflags)) { 2633 if (comedi_is_runflags_in_error(runflags)) 2634 retval = -EPIPE; 2635 if (retval || nbytes) 2636 become_nonbusy = true; 2637 break; 2638 } 2639 if (nbytes == 0) 2640 break; 2641 2642 /* Allocate all free buffer space. */ 2643 comedi_buf_write_alloc(s, async->prealloc_bufsz); 2644 m = comedi_buf_write_n_allocated(s); 2645 n = min_t(size_t, m, nbytes); 2646 2647 if (n == 0) { 2648 if (file->f_flags & O_NONBLOCK) { 2649 retval = -EAGAIN; 2650 break; 2651 } 2652 schedule(); 2653 if (signal_pending(current)) { 2654 retval = -ERESTARTSYS; 2655 break; 2656 } 2657 if (s->busy != file || 2658 !(async->cmd.flags & CMDF_WRITE)) { 2659 retval = -EINVAL; 2660 break; 2661 } 2662 continue; 2663 } 2664 2665 set_current_state(TASK_RUNNING); 2666 m = comedi_buf_copy_from_user(s, async->buf_write_ptr, buf, n); 2667 if (m) { 2668 n -= m; 2669 retval = -EFAULT; 2670 } 2671 comedi_buf_write_free(s, n); 2672 2673 count += n; 2674 nbytes -= n; 2675 2676 buf += n; 2677 } 2678 remove_wait_queue(&async->wait_head, &wait); 2679 set_current_state(TASK_RUNNING); 2680 if (become_nonbusy && count == 0) { 2681 struct comedi_subdevice *new_s; 2682 2683 /* 2684 * To avoid deadlock, cannot acquire dev->mutex 2685 * while dev->attach_lock is held. 2686 */ 2687 up_read(&dev->attach_lock); 2688 attach_locked = false; 2689 mutex_lock(&dev->mutex); 2690 /* 2691 * Check device hasn't become detached behind our back. 2692 * Checking dev->detach_count is unchanged ought to be 2693 * sufficient (unless there have been 2**32 detaches in the 2694 * meantime!), but check the subdevice pointer as well just in 2695 * case. 2696 * 2697 * Also check the subdevice is still in a suitable state to 2698 * become non-busy in case it changed behind our back. 2699 */ 2700 new_s = comedi_file_write_subdevice(file); 2701 if (dev->attached && old_detach_count == dev->detach_count && 2702 s == new_s && new_s->async == async && s->busy == file && 2703 (async->cmd.flags & CMDF_WRITE) && 2704 !comedi_is_subdevice_running(s)) 2705 do_become_nonbusy(dev, s); 2706 mutex_unlock(&dev->mutex); 2707 } 2708 out: 2709 if (attach_locked) 2710 up_read(&dev->attach_lock); 2711 2712 return count ? count : retval; 2713 } 2714 2715 static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes, 2716 loff_t *offset) 2717 { 2718 struct comedi_subdevice *s; 2719 struct comedi_async *async; 2720 unsigned int n, m; 2721 ssize_t count = 0; 2722 int retval = 0; 2723 DECLARE_WAITQUEUE(wait, current); 2724 struct comedi_file *cfp = file->private_data; 2725 struct comedi_device *dev = cfp->dev; 2726 unsigned int old_detach_count; 2727 bool become_nonbusy = false; 2728 bool attach_locked; 2729 2730 /* Protect against device detachment during operation. */ 2731 down_read(&dev->attach_lock); 2732 attach_locked = true; 2733 old_detach_count = dev->detach_count; 2734 2735 if (!dev->attached) { 2736 dev_dbg(dev->class_dev, "no driver attached\n"); 2737 retval = -ENODEV; 2738 goto out; 2739 } 2740 2741 s = comedi_file_read_subdevice(file); 2742 if (!s || !s->async) { 2743 retval = -EIO; 2744 goto out; 2745 } 2746 2747 async = s->async; 2748 if (s->busy != file || (async->cmd.flags & CMDF_WRITE)) { 2749 retval = -EINVAL; 2750 goto out; 2751 } 2752 2753 add_wait_queue(&async->wait_head, &wait); 2754 while (count == 0 && !retval) { 2755 set_current_state(TASK_INTERRUPTIBLE); 2756 2757 m = comedi_buf_read_n_available(s); 2758 n = min_t(size_t, m, nbytes); 2759 2760 if (n == 0) { 2761 unsigned int runflags = 2762 comedi_get_subdevice_runflags(s); 2763 2764 if (!comedi_is_runflags_running(runflags)) { 2765 if (comedi_is_runflags_in_error(runflags)) 2766 retval = -EPIPE; 2767 if (retval || nbytes) 2768 become_nonbusy = true; 2769 break; 2770 } 2771 if (nbytes == 0) 2772 break; 2773 if (file->f_flags & O_NONBLOCK) { 2774 retval = -EAGAIN; 2775 break; 2776 } 2777 schedule(); 2778 if (signal_pending(current)) { 2779 retval = -ERESTARTSYS; 2780 break; 2781 } 2782 if (s->busy != file || 2783 (async->cmd.flags & CMDF_WRITE)) { 2784 retval = -EINVAL; 2785 break; 2786 } 2787 continue; 2788 } 2789 2790 set_current_state(TASK_RUNNING); 2791 m = comedi_buf_copy_to_user(s, buf, async->buf_read_ptr, n); 2792 if (m) { 2793 n -= m; 2794 retval = -EFAULT; 2795 } 2796 2797 comedi_buf_read_alloc(s, n); 2798 comedi_buf_read_free(s, n); 2799 2800 count += n; 2801 nbytes -= n; 2802 2803 buf += n; 2804 } 2805 remove_wait_queue(&async->wait_head, &wait); 2806 set_current_state(TASK_RUNNING); 2807 if (become_nonbusy && count == 0) { 2808 struct comedi_subdevice *new_s; 2809 2810 /* 2811 * To avoid deadlock, cannot acquire dev->mutex 2812 * while dev->attach_lock is held. 2813 */ 2814 up_read(&dev->attach_lock); 2815 attach_locked = false; 2816 mutex_lock(&dev->mutex); 2817 /* 2818 * Check device hasn't become detached behind our back. 2819 * Checking dev->detach_count is unchanged ought to be 2820 * sufficient (unless there have been 2**32 detaches in the 2821 * meantime!), but check the subdevice pointer as well just in 2822 * case. 2823 * 2824 * Also check the subdevice is still in a suitable state to 2825 * become non-busy in case it changed behind our back. 2826 */ 2827 new_s = comedi_file_read_subdevice(file); 2828 if (dev->attached && old_detach_count == dev->detach_count && 2829 s == new_s && new_s->async == async && s->busy == file && 2830 !(async->cmd.flags & CMDF_WRITE) && 2831 !comedi_is_subdevice_running(s) && 2832 comedi_buf_read_n_available(s) == 0) 2833 do_become_nonbusy(dev, s); 2834 mutex_unlock(&dev->mutex); 2835 } 2836 out: 2837 if (attach_locked) 2838 up_read(&dev->attach_lock); 2839 2840 return count ? count : retval; 2841 } 2842 2843 static int comedi_open(struct inode *inode, struct file *file) 2844 { 2845 const unsigned int minor = iminor(inode); 2846 struct comedi_file *cfp; 2847 struct comedi_device *dev = comedi_dev_get_from_minor(minor); 2848 int rc; 2849 2850 if (!dev) { 2851 pr_debug("invalid minor number\n"); 2852 return -ENODEV; 2853 } 2854 2855 cfp = kzalloc(sizeof(*cfp), GFP_KERNEL); 2856 if (!cfp) { 2857 comedi_dev_put(dev); 2858 return -ENOMEM; 2859 } 2860 2861 cfp->dev = dev; 2862 2863 mutex_lock(&dev->mutex); 2864 if (!dev->attached && !capable(CAP_SYS_ADMIN)) { 2865 dev_dbg(dev->class_dev, "not attached and not CAP_SYS_ADMIN\n"); 2866 rc = -ENODEV; 2867 goto out; 2868 } 2869 if (dev->attached && dev->use_count == 0) { 2870 if (!try_module_get(dev->driver->module)) { 2871 rc = -ENXIO; 2872 goto out; 2873 } 2874 if (dev->open) { 2875 rc = dev->open(dev); 2876 if (rc < 0) { 2877 module_put(dev->driver->module); 2878 goto out; 2879 } 2880 } 2881 } 2882 2883 dev->use_count++; 2884 file->private_data = cfp; 2885 comedi_file_reset(file); 2886 rc = 0; 2887 2888 out: 2889 mutex_unlock(&dev->mutex); 2890 if (rc) { 2891 comedi_dev_put(dev); 2892 kfree(cfp); 2893 } 2894 return rc; 2895 } 2896 2897 static int comedi_fasync(int fd, struct file *file, int on) 2898 { 2899 struct comedi_file *cfp = file->private_data; 2900 struct comedi_device *dev = cfp->dev; 2901 2902 return fasync_helper(fd, file, on, &dev->async_queue); 2903 } 2904 2905 static int comedi_close(struct inode *inode, struct file *file) 2906 { 2907 struct comedi_file *cfp = file->private_data; 2908 struct comedi_device *dev = cfp->dev; 2909 struct comedi_subdevice *s = NULL; 2910 int i; 2911 2912 mutex_lock(&dev->mutex); 2913 2914 if (dev->subdevices) { 2915 for (i = 0; i < dev->n_subdevices; i++) { 2916 s = &dev->subdevices[i]; 2917 2918 if (s->busy == file) 2919 do_cancel(dev, s); 2920 if (s->lock == file) 2921 s->lock = NULL; 2922 } 2923 } 2924 if (dev->attached && dev->use_count == 1) { 2925 if (dev->close) 2926 dev->close(dev); 2927 module_put(dev->driver->module); 2928 } 2929 2930 dev->use_count--; 2931 2932 mutex_unlock(&dev->mutex); 2933 comedi_dev_put(dev); 2934 kfree(cfp); 2935 2936 return 0; 2937 } 2938 2939 #ifdef CONFIG_COMPAT 2940 2941 #define COMEDI32_CHANINFO _IOR(CIO, 3, struct comedi32_chaninfo_struct) 2942 #define COMEDI32_RANGEINFO _IOR(CIO, 8, struct comedi32_rangeinfo_struct) 2943 /* 2944 * N.B. COMEDI32_CMD and COMEDI_CMD ought to use _IOWR, not _IOR. 2945 * It's too late to change it now, but it only affects the command number. 2946 */ 2947 #define COMEDI32_CMD _IOR(CIO, 9, struct comedi32_cmd_struct) 2948 /* 2949 * N.B. COMEDI32_CMDTEST and COMEDI_CMDTEST ought to use _IOWR, not _IOR. 2950 * It's too late to change it now, but it only affects the command number. 2951 */ 2952 #define COMEDI32_CMDTEST _IOR(CIO, 10, struct comedi32_cmd_struct) 2953 #define COMEDI32_INSNLIST _IOR(CIO, 11, struct comedi32_insnlist_struct) 2954 #define COMEDI32_INSN _IOR(CIO, 12, struct comedi32_insn_struct) 2955 2956 struct comedi32_chaninfo_struct { 2957 unsigned int subdev; 2958 compat_uptr_t maxdata_list; /* 32-bit 'unsigned int *' */ 2959 compat_uptr_t flaglist; /* 32-bit 'unsigned int *' */ 2960 compat_uptr_t rangelist; /* 32-bit 'unsigned int *' */ 2961 unsigned int unused[4]; 2962 }; 2963 2964 struct comedi32_rangeinfo_struct { 2965 unsigned int range_type; 2966 compat_uptr_t range_ptr; /* 32-bit 'void *' */ 2967 }; 2968 2969 struct comedi32_cmd_struct { 2970 unsigned int subdev; 2971 unsigned int flags; 2972 unsigned int start_src; 2973 unsigned int start_arg; 2974 unsigned int scan_begin_src; 2975 unsigned int scan_begin_arg; 2976 unsigned int convert_src; 2977 unsigned int convert_arg; 2978 unsigned int scan_end_src; 2979 unsigned int scan_end_arg; 2980 unsigned int stop_src; 2981 unsigned int stop_arg; 2982 compat_uptr_t chanlist; /* 32-bit 'unsigned int *' */ 2983 unsigned int chanlist_len; 2984 compat_uptr_t data; /* 32-bit 'short *' */ 2985 unsigned int data_len; 2986 }; 2987 2988 struct comedi32_insn_struct { 2989 unsigned int insn; 2990 unsigned int n; 2991 compat_uptr_t data; /* 32-bit 'unsigned int *' */ 2992 unsigned int subdev; 2993 unsigned int chanspec; 2994 unsigned int unused[3]; 2995 }; 2996 2997 struct comedi32_insnlist_struct { 2998 unsigned int n_insns; 2999 compat_uptr_t insns; /* 32-bit 'struct comedi_insn *' */ 3000 }; 3001 3002 /* Handle 32-bit COMEDI_CHANINFO ioctl. */ 3003 static int compat_chaninfo(struct file *file, unsigned long arg) 3004 { 3005 struct comedi_file *cfp = file->private_data; 3006 struct comedi_device *dev = cfp->dev; 3007 struct comedi32_chaninfo_struct chaninfo32; 3008 struct comedi_chaninfo chaninfo; 3009 int err; 3010 3011 if (copy_from_user(&chaninfo32, compat_ptr(arg), sizeof(chaninfo32))) 3012 return -EFAULT; 3013 3014 memset(&chaninfo, 0, sizeof(chaninfo)); 3015 chaninfo.subdev = chaninfo32.subdev; 3016 chaninfo.maxdata_list = compat_ptr(chaninfo32.maxdata_list); 3017 chaninfo.flaglist = compat_ptr(chaninfo32.flaglist); 3018 chaninfo.rangelist = compat_ptr(chaninfo32.rangelist); 3019 3020 mutex_lock(&dev->mutex); 3021 err = do_chaninfo_ioctl(dev, &chaninfo); 3022 mutex_unlock(&dev->mutex); 3023 return err; 3024 } 3025 3026 /* Handle 32-bit COMEDI_RANGEINFO ioctl. */ 3027 static int compat_rangeinfo(struct file *file, unsigned long arg) 3028 { 3029 struct comedi_file *cfp = file->private_data; 3030 struct comedi_device *dev = cfp->dev; 3031 struct comedi32_rangeinfo_struct rangeinfo32; 3032 struct comedi_rangeinfo rangeinfo; 3033 int err; 3034 3035 if (copy_from_user(&rangeinfo32, compat_ptr(arg), sizeof(rangeinfo32))) 3036 return -EFAULT; 3037 memset(&rangeinfo, 0, sizeof(rangeinfo)); 3038 rangeinfo.range_type = rangeinfo32.range_type; 3039 rangeinfo.range_ptr = compat_ptr(rangeinfo32.range_ptr); 3040 3041 mutex_lock(&dev->mutex); 3042 err = do_rangeinfo_ioctl(dev, &rangeinfo); 3043 mutex_unlock(&dev->mutex); 3044 return err; 3045 } 3046 3047 /* Copy 32-bit cmd structure to native cmd structure. */ 3048 static int get_compat_cmd(struct comedi_cmd *cmd, 3049 struct comedi32_cmd_struct __user *cmd32) 3050 { 3051 struct comedi32_cmd_struct v32; 3052 3053 if (copy_from_user(&v32, cmd32, sizeof(v32))) 3054 return -EFAULT; 3055 3056 cmd->subdev = v32.subdev; 3057 cmd->flags = v32.flags; 3058 cmd->start_src = v32.start_src; 3059 cmd->start_arg = v32.start_arg; 3060 cmd->scan_begin_src = v32.scan_begin_src; 3061 cmd->scan_begin_arg = v32.scan_begin_arg; 3062 cmd->convert_src = v32.convert_src; 3063 cmd->convert_arg = v32.convert_arg; 3064 cmd->scan_end_src = v32.scan_end_src; 3065 cmd->scan_end_arg = v32.scan_end_arg; 3066 cmd->stop_src = v32.stop_src; 3067 cmd->stop_arg = v32.stop_arg; 3068 cmd->chanlist = (unsigned int __force *)compat_ptr(v32.chanlist); 3069 cmd->chanlist_len = v32.chanlist_len; 3070 cmd->data = compat_ptr(v32.data); 3071 cmd->data_len = v32.data_len; 3072 return 0; 3073 } 3074 3075 /* Copy native cmd structure to 32-bit cmd structure. */ 3076 static int put_compat_cmd(struct comedi32_cmd_struct __user *cmd32, 3077 struct comedi_cmd *cmd) 3078 { 3079 struct comedi32_cmd_struct v32; 3080 3081 memset(&v32, 0, sizeof(v32)); 3082 v32.subdev = cmd->subdev; 3083 v32.flags = cmd->flags; 3084 v32.start_src = cmd->start_src; 3085 v32.start_arg = cmd->start_arg; 3086 v32.scan_begin_src = cmd->scan_begin_src; 3087 v32.scan_begin_arg = cmd->scan_begin_arg; 3088 v32.convert_src = cmd->convert_src; 3089 v32.convert_arg = cmd->convert_arg; 3090 v32.scan_end_src = cmd->scan_end_src; 3091 v32.scan_end_arg = cmd->scan_end_arg; 3092 v32.stop_src = cmd->stop_src; 3093 v32.stop_arg = cmd->stop_arg; 3094 /* Assume chanlist pointer is unchanged. */ 3095 v32.chanlist = ptr_to_compat((unsigned int __user *)cmd->chanlist); 3096 v32.chanlist_len = cmd->chanlist_len; 3097 v32.data = ptr_to_compat(cmd->data); 3098 v32.data_len = cmd->data_len; 3099 if (copy_to_user(cmd32, &v32, sizeof(v32))) 3100 return -EFAULT; 3101 return 0; 3102 } 3103 3104 /* Handle 32-bit COMEDI_CMD ioctl. */ 3105 static int compat_cmd(struct file *file, unsigned long arg) 3106 { 3107 struct comedi_file *cfp = file->private_data; 3108 struct comedi_device *dev = cfp->dev; 3109 struct comedi_cmd cmd; 3110 bool copy = false; 3111 int rc, err; 3112 3113 rc = get_compat_cmd(&cmd, compat_ptr(arg)); 3114 if (rc) 3115 return rc; 3116 3117 mutex_lock(&dev->mutex); 3118 rc = do_cmd_ioctl(dev, &cmd, ©, file); 3119 mutex_unlock(&dev->mutex); 3120 if (copy) { 3121 /* Special case: copy cmd back to user. */ 3122 err = put_compat_cmd(compat_ptr(arg), &cmd); 3123 if (err) 3124 rc = err; 3125 } 3126 return rc; 3127 } 3128 3129 /* Handle 32-bit COMEDI_CMDTEST ioctl. */ 3130 static int compat_cmdtest(struct file *file, unsigned long arg) 3131 { 3132 struct comedi_file *cfp = file->private_data; 3133 struct comedi_device *dev = cfp->dev; 3134 struct comedi_cmd cmd; 3135 bool copy = false; 3136 int rc, err; 3137 3138 rc = get_compat_cmd(&cmd, compat_ptr(arg)); 3139 if (rc) 3140 return rc; 3141 3142 mutex_lock(&dev->mutex); 3143 rc = do_cmdtest_ioctl(dev, &cmd, ©, file); 3144 mutex_unlock(&dev->mutex); 3145 if (copy) { 3146 err = put_compat_cmd(compat_ptr(arg), &cmd); 3147 if (err) 3148 rc = err; 3149 } 3150 return rc; 3151 } 3152 3153 /* Copy 32-bit insn structure to native insn structure. */ 3154 static int get_compat_insn(struct comedi_insn *insn, 3155 struct comedi32_insn_struct __user *insn32) 3156 { 3157 struct comedi32_insn_struct v32; 3158 3159 /* Copy insn structure. Ignore the unused members. */ 3160 if (copy_from_user(&v32, insn32, sizeof(v32))) 3161 return -EFAULT; 3162 memset(insn, 0, sizeof(*insn)); 3163 insn->insn = v32.insn; 3164 insn->n = v32.n; 3165 insn->data = compat_ptr(v32.data); 3166 insn->subdev = v32.subdev; 3167 insn->chanspec = v32.chanspec; 3168 return 0; 3169 } 3170 3171 /* Handle 32-bit COMEDI_INSNLIST ioctl. */ 3172 static int compat_insnlist(struct file *file, unsigned long arg) 3173 { 3174 struct comedi_file *cfp = file->private_data; 3175 struct comedi_device *dev = cfp->dev; 3176 struct comedi32_insnlist_struct insnlist32; 3177 struct comedi32_insn_struct __user *insn32; 3178 struct comedi_insn *insns; 3179 unsigned int n; 3180 int rc; 3181 3182 if (copy_from_user(&insnlist32, compat_ptr(arg), sizeof(insnlist32))) 3183 return -EFAULT; 3184 3185 rc = check_insnlist_len(dev, insnlist32.n_insns); 3186 if (rc) 3187 return rc; 3188 insns = kcalloc(insnlist32.n_insns, sizeof(*insns), GFP_KERNEL); 3189 if (!insns) 3190 return -ENOMEM; 3191 3192 /* Copy insn structures. */ 3193 insn32 = compat_ptr(insnlist32.insns); 3194 for (n = 0; n < insnlist32.n_insns; n++) { 3195 rc = get_compat_insn(insns + n, insn32 + n); 3196 if (rc) { 3197 kfree(insns); 3198 return rc; 3199 } 3200 } 3201 3202 mutex_lock(&dev->mutex); 3203 rc = do_insnlist_ioctl(dev, insns, insnlist32.n_insns, file); 3204 mutex_unlock(&dev->mutex); 3205 kfree(insns); 3206 return rc; 3207 } 3208 3209 /* Handle 32-bit COMEDI_INSN ioctl. */ 3210 static int compat_insn(struct file *file, unsigned long arg) 3211 { 3212 struct comedi_file *cfp = file->private_data; 3213 struct comedi_device *dev = cfp->dev; 3214 struct comedi_insn insn; 3215 int rc; 3216 3217 rc = get_compat_insn(&insn, (void __user *)arg); 3218 if (rc) 3219 return rc; 3220 3221 mutex_lock(&dev->mutex); 3222 rc = do_insn_ioctl(dev, &insn, file); 3223 mutex_unlock(&dev->mutex); 3224 return rc; 3225 } 3226 3227 /* 3228 * compat_ioctl file operation. 3229 * 3230 * Returns -ENOIOCTLCMD for unrecognised ioctl codes. 3231 */ 3232 static long comedi_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 3233 { 3234 int rc; 3235 3236 switch (cmd) { 3237 case COMEDI_DEVCONFIG: 3238 case COMEDI_DEVINFO: 3239 case COMEDI_SUBDINFO: 3240 case COMEDI_BUFCONFIG: 3241 case COMEDI_BUFINFO: 3242 /* Just need to translate the pointer argument. */ 3243 arg = (unsigned long)compat_ptr(arg); 3244 rc = comedi_unlocked_ioctl(file, cmd, arg); 3245 break; 3246 case COMEDI_LOCK: 3247 case COMEDI_UNLOCK: 3248 case COMEDI_CANCEL: 3249 case COMEDI_POLL: 3250 case COMEDI_SETRSUBD: 3251 case COMEDI_SETWSUBD: 3252 /* No translation needed. */ 3253 rc = comedi_unlocked_ioctl(file, cmd, arg); 3254 break; 3255 case COMEDI32_CHANINFO: 3256 rc = compat_chaninfo(file, arg); 3257 break; 3258 case COMEDI32_RANGEINFO: 3259 rc = compat_rangeinfo(file, arg); 3260 break; 3261 case COMEDI32_CMD: 3262 rc = compat_cmd(file, arg); 3263 break; 3264 case COMEDI32_CMDTEST: 3265 rc = compat_cmdtest(file, arg); 3266 break; 3267 case COMEDI32_INSNLIST: 3268 rc = compat_insnlist(file, arg); 3269 break; 3270 case COMEDI32_INSN: 3271 rc = compat_insn(file, arg); 3272 break; 3273 default: 3274 rc = -ENOIOCTLCMD; 3275 break; 3276 } 3277 return rc; 3278 } 3279 #else 3280 #define comedi_compat_ioctl NULL 3281 #endif 3282 3283 static const struct file_operations comedi_fops = { 3284 .owner = THIS_MODULE, 3285 .unlocked_ioctl = comedi_unlocked_ioctl, 3286 .compat_ioctl = comedi_compat_ioctl, 3287 .open = comedi_open, 3288 .release = comedi_close, 3289 .read = comedi_read, 3290 .write = comedi_write, 3291 .mmap = comedi_mmap, 3292 .poll = comedi_poll, 3293 .fasync = comedi_fasync, 3294 .llseek = noop_llseek, 3295 }; 3296 3297 /** 3298 * comedi_event() - Handle events for asynchronous COMEDI command 3299 * @dev: COMEDI device. 3300 * @s: COMEDI subdevice. 3301 * Context: in_interrupt() (usually), @s->spin_lock spin-lock not held. 3302 * 3303 * If an asynchronous COMEDI command is active on the subdevice, process 3304 * any %COMEDI_CB_... event flags that have been set, usually by an 3305 * interrupt handler. These may change the run state of the asynchronous 3306 * command, wake a task, and/or send a %SIGIO signal. 3307 */ 3308 void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s) 3309 { 3310 struct comedi_async *async = s->async; 3311 unsigned int events; 3312 int si_code = 0; 3313 unsigned long flags; 3314 3315 spin_lock_irqsave(&s->spin_lock, flags); 3316 3317 events = async->events; 3318 async->events = 0; 3319 if (!__comedi_is_subdevice_running(s)) { 3320 spin_unlock_irqrestore(&s->spin_lock, flags); 3321 return; 3322 } 3323 3324 if (events & COMEDI_CB_CANCEL_MASK) 3325 __comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING); 3326 3327 /* 3328 * Remember if an error event has occurred, so an error can be 3329 * returned the next time the user does a read() or write(). 3330 */ 3331 if (events & COMEDI_CB_ERROR_MASK) 3332 __comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR); 3333 3334 if (async->cb_mask & events) { 3335 wake_up_interruptible(&async->wait_head); 3336 si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN; 3337 } 3338 3339 spin_unlock_irqrestore(&s->spin_lock, flags); 3340 3341 if (si_code) 3342 kill_fasync(&dev->async_queue, SIGIO, si_code); 3343 } 3344 EXPORT_SYMBOL_GPL(comedi_event); 3345 3346 /* Note: the ->mutex is pre-locked on successful return */ 3347 struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device) 3348 { 3349 struct comedi_device *dev; 3350 struct device *csdev; 3351 unsigned int i; 3352 3353 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 3354 if (!dev) 3355 return ERR_PTR(-ENOMEM); 3356 comedi_device_init(dev); 3357 comedi_set_hw_dev(dev, hardware_device); 3358 mutex_lock(&dev->mutex); 3359 mutex_lock(&comedi_board_minor_table_lock); 3360 for (i = hardware_device ? comedi_num_legacy_minors : 0; 3361 i < COMEDI_NUM_BOARD_MINORS; ++i) { 3362 if (!comedi_board_minor_table[i]) { 3363 comedi_board_minor_table[i] = dev; 3364 break; 3365 } 3366 } 3367 mutex_unlock(&comedi_board_minor_table_lock); 3368 if (i == COMEDI_NUM_BOARD_MINORS) { 3369 mutex_unlock(&dev->mutex); 3370 comedi_device_cleanup(dev); 3371 comedi_dev_put(dev); 3372 dev_err(hardware_device, 3373 "ran out of minor numbers for board device files\n"); 3374 return ERR_PTR(-EBUSY); 3375 } 3376 dev->minor = i; 3377 csdev = device_create(&comedi_class, hardware_device, 3378 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i); 3379 if (!IS_ERR(csdev)) 3380 dev->class_dev = get_device(csdev); 3381 3382 /* Note: dev->mutex needs to be unlocked by the caller. */ 3383 return dev; 3384 } 3385 3386 void comedi_release_hardware_device(struct device *hardware_device) 3387 { 3388 int minor; 3389 struct comedi_device *dev; 3390 3391 for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS; 3392 minor++) { 3393 mutex_lock(&comedi_board_minor_table_lock); 3394 dev = comedi_board_minor_table[minor]; 3395 if (dev && dev->hw_dev == hardware_device) { 3396 comedi_board_minor_table[minor] = NULL; 3397 mutex_unlock(&comedi_board_minor_table_lock); 3398 comedi_free_board_dev(dev); 3399 break; 3400 } 3401 mutex_unlock(&comedi_board_minor_table_lock); 3402 } 3403 } 3404 3405 int comedi_alloc_subdevice_minor(struct comedi_subdevice *s) 3406 { 3407 struct comedi_device *dev = s->device; 3408 struct device *csdev; 3409 unsigned int i; 3410 3411 mutex_lock(&comedi_subdevice_minor_table_lock); 3412 for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) { 3413 if (!comedi_subdevice_minor_table[i]) { 3414 comedi_subdevice_minor_table[i] = s; 3415 break; 3416 } 3417 } 3418 mutex_unlock(&comedi_subdevice_minor_table_lock); 3419 if (i == COMEDI_NUM_SUBDEVICE_MINORS) { 3420 dev_err(dev->class_dev, 3421 "ran out of minor numbers for subdevice files\n"); 3422 return -EBUSY; 3423 } 3424 i += COMEDI_NUM_BOARD_MINORS; 3425 s->minor = i; 3426 csdev = device_create(&comedi_class, dev->class_dev, 3427 MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i", 3428 dev->minor, s->index); 3429 if (!IS_ERR(csdev)) 3430 s->class_dev = csdev; 3431 3432 return 0; 3433 } 3434 3435 void comedi_free_subdevice_minor(struct comedi_subdevice *s) 3436 { 3437 unsigned int i; 3438 3439 if (!s) 3440 return; 3441 if (s->minor < COMEDI_NUM_BOARD_MINORS || 3442 s->minor >= COMEDI_NUM_MINORS) 3443 return; 3444 3445 i = s->minor - COMEDI_NUM_BOARD_MINORS; 3446 mutex_lock(&comedi_subdevice_minor_table_lock); 3447 if (s == comedi_subdevice_minor_table[i]) 3448 comedi_subdevice_minor_table[i] = NULL; 3449 mutex_unlock(&comedi_subdevice_minor_table_lock); 3450 if (s->class_dev) { 3451 device_destroy(&comedi_class, MKDEV(COMEDI_MAJOR, s->minor)); 3452 s->class_dev = NULL; 3453 } 3454 } 3455 3456 static void comedi_cleanup_board_minors(void) 3457 { 3458 struct comedi_device *dev; 3459 unsigned int i; 3460 3461 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) { 3462 dev = comedi_clear_board_minor(i); 3463 comedi_free_board_dev(dev); 3464 } 3465 } 3466 3467 static int __init comedi_init(void) 3468 { 3469 int i; 3470 int retval; 3471 3472 pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n"); 3473 3474 if (comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) { 3475 pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n", 3476 COMEDI_NUM_BOARD_MINORS); 3477 return -EINVAL; 3478 } 3479 3480 retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0), 3481 COMEDI_NUM_MINORS, "comedi"); 3482 if (retval) 3483 return retval; 3484 3485 cdev_init(&comedi_cdev, &comedi_fops); 3486 comedi_cdev.owner = THIS_MODULE; 3487 3488 retval = kobject_set_name(&comedi_cdev.kobj, "comedi"); 3489 if (retval) 3490 goto out_unregister_chrdev_region; 3491 3492 retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), 3493 COMEDI_NUM_MINORS); 3494 if (retval) 3495 goto out_unregister_chrdev_region; 3496 3497 retval = class_register(&comedi_class); 3498 if (retval) { 3499 pr_err("failed to create class\n"); 3500 goto out_cdev_del; 3501 } 3502 3503 /* create devices files for legacy/manual use */ 3504 for (i = 0; i < comedi_num_legacy_minors; i++) { 3505 struct comedi_device *dev; 3506 3507 dev = comedi_alloc_board_minor(NULL); 3508 if (IS_ERR(dev)) { 3509 retval = PTR_ERR(dev); 3510 goto out_cleanup_board_minors; 3511 } 3512 /* comedi_alloc_board_minor() locked the mutex */ 3513 lockdep_assert_held(&dev->mutex); 3514 mutex_unlock(&dev->mutex); 3515 } 3516 3517 /* XXX requires /proc interface */ 3518 comedi_proc_init(); 3519 3520 return 0; 3521 3522 out_cleanup_board_minors: 3523 comedi_cleanup_board_minors(); 3524 class_unregister(&comedi_class); 3525 out_cdev_del: 3526 cdev_del(&comedi_cdev); 3527 out_unregister_chrdev_region: 3528 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS); 3529 return retval; 3530 } 3531 module_init(comedi_init); 3532 3533 static void __exit comedi_cleanup(void) 3534 { 3535 comedi_cleanup_board_minors(); 3536 class_unregister(&comedi_class); 3537 cdev_del(&comedi_cdev); 3538 unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS); 3539 3540 comedi_proc_cleanup(); 3541 } 3542 module_exit(comedi_cleanup); 3543 3544 MODULE_AUTHOR("https://www.comedi.org"); 3545 MODULE_DESCRIPTION("Comedi core module"); 3546 MODULE_LICENSE("GPL"); 3547