1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * module/drivers.c 4 * functions for manipulating drivers 5 * 6 * COMEDI - Linux Control and Measurement Device Interface 7 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org> 8 * Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net> 9 */ 10 11 #include <linux/device.h> 12 #include <linux/module.h> 13 #include <linux/errno.h> 14 #include <linux/kernel.h> 15 #include <linux/ioport.h> 16 #include <linux/slab.h> 17 #include <linux/dma-direction.h> 18 #include <linux/interrupt.h> 19 #include <linux/firmware.h> 20 #include <linux/comedi/comedidev.h> 21 #include "comedi_internal.h" 22 23 struct comedi_driver *comedi_drivers; 24 /* protects access to comedi_drivers */ 25 DEFINE_MUTEX(comedi_drivers_list_lock); 26 27 /** 28 * comedi_set_hw_dev() - Set hardware device associated with COMEDI device 29 * @dev: COMEDI device. 30 * @hw_dev: Hardware device. 31 * 32 * For automatically configured COMEDI devices (resulting from a call to 33 * comedi_auto_config() or one of its wrappers from the low-level COMEDI 34 * driver), comedi_set_hw_dev() is called automatically by the COMEDI core 35 * to associate the COMEDI device with the hardware device. It can also be 36 * called directly by "legacy" low-level COMEDI drivers that rely on the 37 * %COMEDI_DEVCONFIG ioctl to configure the hardware as long as the hardware 38 * has a &struct device. 39 * 40 * If @dev->hw_dev is NULL, it gets a reference to @hw_dev and sets 41 * @dev->hw_dev, otherwise, it does nothing. Calling it multiple times 42 * with the same hardware device is not considered an error. If it gets 43 * a reference to the hardware device, it will be automatically 'put' when 44 * the device is detached from COMEDI. 45 * 46 * Returns 0 if @dev->hw_dev was NULL or the same as @hw_dev, otherwise 47 * returns -EEXIST. 48 */ 49 int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev) 50 { 51 if (hw_dev == dev->hw_dev) 52 return 0; 53 if (dev->hw_dev) 54 return -EEXIST; 55 dev->hw_dev = get_device(hw_dev); 56 return 0; 57 } 58 EXPORT_SYMBOL_GPL(comedi_set_hw_dev); 59 60 static void comedi_clear_hw_dev(struct comedi_device *dev) 61 { 62 put_device(dev->hw_dev); 63 dev->hw_dev = NULL; 64 } 65 66 /** 67 * comedi_alloc_devpriv() - Allocate memory for the device private data 68 * @dev: COMEDI device. 69 * @size: Size of the memory to allocate. 70 * 71 * The allocated memory is zero-filled. @dev->private points to it on 72 * return. The memory will be automatically freed when the COMEDI device is 73 * "detached". 74 * 75 * Returns a pointer to the allocated memory, or NULL on failure. 76 */ 77 void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size) 78 { 79 dev->private = kzalloc(size, GFP_KERNEL); 80 return dev->private; 81 } 82 EXPORT_SYMBOL_GPL(comedi_alloc_devpriv); 83 84 /** 85 * comedi_alloc_subdevices() - Allocate subdevices for COMEDI device 86 * @dev: COMEDI device. 87 * @num_subdevices: Number of subdevices to allocate. 88 * 89 * Allocates and initializes an array of &struct comedi_subdevice for the 90 * COMEDI device. If successful, sets @dev->subdevices to point to the 91 * first one and @dev->n_subdevices to the number. 92 * 93 * Returns 0 on success, -EINVAL if @num_subdevices is < 1, or -ENOMEM if 94 * failed to allocate the memory. 95 */ 96 int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices) 97 { 98 struct comedi_subdevice *s; 99 int i; 100 101 if (num_subdevices < 1) 102 return -EINVAL; 103 104 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL); 105 if (!s) 106 return -ENOMEM; 107 dev->subdevices = s; 108 dev->n_subdevices = num_subdevices; 109 110 for (i = 0; i < num_subdevices; ++i) { 111 s = &dev->subdevices[i]; 112 s->device = dev; 113 s->index = i; 114 s->async_dma_dir = DMA_NONE; 115 spin_lock_init(&s->spin_lock); 116 s->minor = -1; 117 } 118 return 0; 119 } 120 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices); 121 122 /** 123 * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback 124 * @s: COMEDI subdevice. 125 * 126 * This is called by low-level COMEDI drivers to allocate an array to record 127 * the last values written to a subdevice's analog output channels (at least 128 * by the %INSN_WRITE instruction), to allow them to be read back by an 129 * %INSN_READ instruction. It also provides a default handler for the 130 * %INSN_READ instruction unless one has already been set. 131 * 132 * On success, @s->readback points to the first element of the array, which 133 * is zero-filled. The low-level driver is responsible for updating its 134 * contents. @s->insn_read will be set to comedi_readback_insn_read() 135 * unless it is already non-NULL. 136 * 137 * Returns 0 on success, -EINVAL if the subdevice has no channels, or 138 * -ENOMEM on allocation failure. 139 */ 140 int comedi_alloc_subdev_readback(struct comedi_subdevice *s) 141 { 142 if (!s->n_chan) 143 return -EINVAL; 144 145 s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL); 146 if (!s->readback) 147 return -ENOMEM; 148 149 if (!s->insn_read) 150 s->insn_read = comedi_readback_insn_read; 151 152 return 0; 153 } 154 EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback); 155 156 static void comedi_device_detach_cleanup(struct comedi_device *dev) 157 { 158 int i; 159 struct comedi_subdevice *s; 160 161 lockdep_assert_held(&dev->attach_lock); 162 lockdep_assert_held(&dev->mutex); 163 if (dev->subdevices) { 164 for (i = 0; i < dev->n_subdevices; i++) { 165 s = &dev->subdevices[i]; 166 if (comedi_can_auto_free_spriv(s)) 167 kfree(s->private); 168 comedi_free_subdevice_minor(s); 169 if (s->async) { 170 comedi_buf_alloc(dev, s, 0); 171 kfree(s->async); 172 } 173 kfree(s->readback); 174 } 175 kfree(dev->subdevices); 176 dev->subdevices = NULL; 177 dev->n_subdevices = 0; 178 } 179 kfree(dev->private); 180 if (!IS_ERR(dev->pacer)) 181 kfree(dev->pacer); 182 dev->private = NULL; 183 dev->pacer = NULL; 184 dev->driver = NULL; 185 dev->board_name = NULL; 186 dev->board_ptr = NULL; 187 dev->mmio = NULL; 188 dev->iobase = 0; 189 dev->iolen = 0; 190 dev->ioenabled = false; 191 dev->irq = 0; 192 dev->read_subdev = NULL; 193 dev->write_subdev = NULL; 194 dev->open = NULL; 195 dev->close = NULL; 196 comedi_clear_hw_dev(dev); 197 } 198 199 void comedi_device_detach(struct comedi_device *dev) 200 { 201 lockdep_assert_held(&dev->mutex); 202 comedi_device_cancel_all(dev); 203 down_write(&dev->attach_lock); 204 dev->attached = false; 205 dev->detach_count++; 206 if (dev->driver) 207 dev->driver->detach(dev); 208 comedi_device_detach_cleanup(dev); 209 up_write(&dev->attach_lock); 210 } 211 212 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s) 213 { 214 return -EINVAL; 215 } 216 217 static int insn_device_inval(struct comedi_device *dev, 218 struct comedi_insn *insn, unsigned int *data) 219 { 220 return -EINVAL; 221 } 222 223 static unsigned int get_zero_valid_routes(struct comedi_device *dev, 224 unsigned int n_pairs, 225 unsigned int *pair_data) 226 { 227 return 0; 228 } 229 230 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s, 231 struct comedi_insn *insn, unsigned int *data) 232 { 233 return -EINVAL; 234 } 235 236 /** 237 * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback. 238 * @dev: COMEDI device. 239 * @s: COMEDI subdevice. 240 * @insn: COMEDI instruction. 241 * @data: Pointer to return the readback data. 242 * 243 * Handles the %INSN_READ instruction for subdevices that use the readback 244 * array allocated by comedi_alloc_subdev_readback(). It may be used 245 * directly as the subdevice's handler (@s->insn_read) or called via a 246 * wrapper. 247 * 248 * @insn->n is normally 1, which will read a single value. If higher, the 249 * same element of the readback array will be read multiple times. 250 * 251 * Returns @insn->n on success, or -EINVAL if @s->readback is NULL. 252 */ 253 int comedi_readback_insn_read(struct comedi_device *dev, 254 struct comedi_subdevice *s, 255 struct comedi_insn *insn, 256 unsigned int *data) 257 { 258 unsigned int chan = CR_CHAN(insn->chanspec); 259 int i; 260 261 if (!s->readback) 262 return -EINVAL; 263 264 for (i = 0; i < insn->n; i++) 265 data[i] = s->readback[chan]; 266 267 return insn->n; 268 } 269 EXPORT_SYMBOL_GPL(comedi_readback_insn_read); 270 271 /** 272 * comedi_timeout() - Busy-wait for a driver condition to occur 273 * @dev: COMEDI device. 274 * @s: COMEDI subdevice. 275 * @insn: COMEDI instruction. 276 * @cb: Callback to check for the condition. 277 * @context: Private context from the driver. 278 * 279 * Busy-waits for up to a second (%COMEDI_TIMEOUT_MS) for the condition or 280 * some error (other than -EBUSY) to occur. The parameters @dev, @s, @insn, 281 * and @context are passed to the callback function, which returns -EBUSY to 282 * continue waiting or some other value to stop waiting (generally 0 if the 283 * condition occurred, or some error value). 284 * 285 * Returns -ETIMEDOUT if timed out, otherwise the return value from the 286 * callback function. 287 */ 288 int comedi_timeout(struct comedi_device *dev, 289 struct comedi_subdevice *s, 290 struct comedi_insn *insn, 291 int (*cb)(struct comedi_device *dev, 292 struct comedi_subdevice *s, 293 struct comedi_insn *insn, 294 unsigned long context), 295 unsigned long context) 296 { 297 unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS); 298 int ret; 299 300 while (time_before(jiffies, timeout)) { 301 ret = cb(dev, s, insn, context); 302 if (ret != -EBUSY) 303 return ret; /* success (0) or non EBUSY errno */ 304 cpu_relax(); 305 } 306 return -ETIMEDOUT; 307 } 308 EXPORT_SYMBOL_GPL(comedi_timeout); 309 310 /** 311 * comedi_dio_insn_config() - Boilerplate (*insn_config) for DIO subdevices 312 * @dev: COMEDI device. 313 * @s: COMEDI subdevice. 314 * @insn: COMEDI instruction. 315 * @data: Instruction parameters and return data. 316 * @mask: io_bits mask for grouped channels, or 0 for single channel. 317 * 318 * If @mask is 0, it is replaced with a single-bit mask corresponding to the 319 * channel number specified by @insn->chanspec. Otherwise, @mask 320 * corresponds to a group of channels (which should include the specified 321 * channel) that are always configured together as inputs or outputs. 322 * 323 * Partially handles the %INSN_CONFIG_DIO_INPUT, %INSN_CONFIG_DIO_OUTPUTS, 324 * and %INSN_CONFIG_DIO_QUERY instructions. The first two update 325 * @s->io_bits to record the directions of the masked channels. The last 326 * one sets @data[1] to the current direction of the group of channels 327 * (%COMEDI_INPUT) or %COMEDI_OUTPUT) as recorded in @s->io_bits. 328 * 329 * The caller is responsible for updating the DIO direction in the hardware 330 * registers if this function returns 0. 331 * 332 * Returns 0 for a %INSN_CONFIG_DIO_INPUT or %INSN_CONFIG_DIO_OUTPUT 333 * instruction, @insn->n (> 0) for a %INSN_CONFIG_DIO_QUERY instruction, or 334 * -EINVAL for some other instruction. 335 */ 336 int comedi_dio_insn_config(struct comedi_device *dev, 337 struct comedi_subdevice *s, 338 struct comedi_insn *insn, 339 unsigned int *data, 340 unsigned int mask) 341 { 342 unsigned int chan = CR_CHAN(insn->chanspec); 343 344 if (!mask && chan < 32) 345 mask = 1U << chan; 346 347 switch (data[0]) { 348 case INSN_CONFIG_DIO_INPUT: 349 s->io_bits &= ~mask; 350 break; 351 352 case INSN_CONFIG_DIO_OUTPUT: 353 s->io_bits |= mask; 354 break; 355 356 case INSN_CONFIG_DIO_QUERY: 357 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT; 358 return insn->n; 359 360 default: 361 return -EINVAL; 362 } 363 364 return 0; 365 } 366 EXPORT_SYMBOL_GPL(comedi_dio_insn_config); 367 368 /** 369 * comedi_dio_update_state() - Update the internal state of DIO subdevices 370 * @s: COMEDI subdevice. 371 * @data: The channel mask and bits to update. 372 * 373 * Updates @s->state which holds the internal state of the outputs for DIO 374 * or DO subdevices (up to 32 channels). @data[0] contains a bit-mask of 375 * the channels to be updated. @data[1] contains a bit-mask of those 376 * channels to be set to '1'. The caller is responsible for updating the 377 * outputs in hardware according to @s->state. As a minimum, the channels 378 * in the returned bit-mask need to be updated. 379 * 380 * Returns @mask with non-existent channels removed. 381 */ 382 unsigned int comedi_dio_update_state(struct comedi_subdevice *s, 383 unsigned int *data) 384 { 385 unsigned int chanmask = (s->n_chan < 32) ? ((1U << s->n_chan) - 1) 386 : 0xffffffff; 387 unsigned int mask = data[0] & chanmask; 388 unsigned int bits = data[1]; 389 390 if (mask) { 391 s->state &= ~mask; 392 s->state |= (bits & mask); 393 } 394 395 return mask; 396 } 397 EXPORT_SYMBOL_GPL(comedi_dio_update_state); 398 399 /** 400 * comedi_bytes_per_scan_cmd() - Get length of asynchronous command "scan" in 401 * bytes 402 * @s: COMEDI subdevice. 403 * @cmd: COMEDI command. 404 * 405 * Determines the overall scan length according to the subdevice type and the 406 * number of channels in the scan for the specified command. 407 * 408 * For digital input, output or input/output subdevices, samples for 409 * multiple channels are assumed to be packed into one or more unsigned 410 * short or unsigned int values according to the subdevice's %SDF_LSAMPL 411 * flag. For other types of subdevice, samples are assumed to occupy a 412 * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag. 413 * 414 * Returns the overall scan length in bytes. 415 */ 416 unsigned int comedi_bytes_per_scan_cmd(struct comedi_subdevice *s, 417 struct comedi_cmd *cmd) 418 { 419 unsigned int num_samples; 420 unsigned int bits_per_sample; 421 422 switch (s->type) { 423 case COMEDI_SUBD_DI: 424 case COMEDI_SUBD_DO: 425 case COMEDI_SUBD_DIO: 426 bits_per_sample = 8 * comedi_bytes_per_sample(s); 427 num_samples = DIV_ROUND_UP(cmd->scan_end_arg, bits_per_sample); 428 break; 429 default: 430 num_samples = cmd->scan_end_arg; 431 break; 432 } 433 return comedi_samples_to_bytes(s, num_samples); 434 } 435 EXPORT_SYMBOL_GPL(comedi_bytes_per_scan_cmd); 436 437 /** 438 * comedi_bytes_per_scan() - Get length of asynchronous command "scan" in bytes 439 * @s: COMEDI subdevice. 440 * 441 * Determines the overall scan length according to the subdevice type and the 442 * number of channels in the scan for the current command. 443 * 444 * For digital input, output or input/output subdevices, samples for 445 * multiple channels are assumed to be packed into one or more unsigned 446 * short or unsigned int values according to the subdevice's %SDF_LSAMPL 447 * flag. For other types of subdevice, samples are assumed to occupy a 448 * whole unsigned short or unsigned int according to the %SDF_LSAMPL flag. 449 * 450 * Returns the overall scan length in bytes. 451 */ 452 unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s) 453 { 454 struct comedi_cmd *cmd = &s->async->cmd; 455 456 return comedi_bytes_per_scan_cmd(s, cmd); 457 } 458 EXPORT_SYMBOL_GPL(comedi_bytes_per_scan); 459 460 static unsigned int __comedi_nscans_left(struct comedi_subdevice *s, 461 unsigned int nscans) 462 { 463 struct comedi_async *async = s->async; 464 struct comedi_cmd *cmd = &async->cmd; 465 466 if (cmd->stop_src == TRIG_COUNT) { 467 unsigned int scans_left = 0; 468 469 if (async->scans_done < cmd->stop_arg) 470 scans_left = cmd->stop_arg - async->scans_done; 471 472 if (nscans > scans_left) 473 nscans = scans_left; 474 } 475 return nscans; 476 } 477 478 /** 479 * comedi_nscans_left() - Return the number of scans left in the command 480 * @s: COMEDI subdevice. 481 * @nscans: The expected number of scans or 0 for all available scans. 482 * 483 * If @nscans is 0, it is set to the number of scans available in the 484 * async buffer. 485 * 486 * If the async command has a stop_src of %TRIG_COUNT, the @nscans will be 487 * checked against the number of scans remaining to complete the command. 488 * 489 * The return value will then be either the expected number of scans or the 490 * number of scans remaining to complete the command, whichever is fewer. 491 */ 492 unsigned int comedi_nscans_left(struct comedi_subdevice *s, 493 unsigned int nscans) 494 { 495 if (nscans == 0) { 496 unsigned int nbytes = comedi_buf_read_n_available(s); 497 498 nscans = nbytes / comedi_bytes_per_scan(s); 499 } 500 return __comedi_nscans_left(s, nscans); 501 } 502 EXPORT_SYMBOL_GPL(comedi_nscans_left); 503 504 /** 505 * comedi_nsamples_left() - Return the number of samples left in the command 506 * @s: COMEDI subdevice. 507 * @nsamples: The expected number of samples. 508 * 509 * Returns the number of samples remaining to complete the command, or the 510 * specified expected number of samples (@nsamples), whichever is fewer. 511 */ 512 unsigned int comedi_nsamples_left(struct comedi_subdevice *s, 513 unsigned int nsamples) 514 { 515 struct comedi_async *async = s->async; 516 struct comedi_cmd *cmd = &async->cmd; 517 unsigned long long scans_left; 518 unsigned long long samples_left; 519 520 if (cmd->stop_src != TRIG_COUNT) 521 return nsamples; 522 523 scans_left = __comedi_nscans_left(s, cmd->stop_arg); 524 if (!scans_left) 525 return 0; 526 527 samples_left = scans_left * cmd->scan_end_arg - 528 comedi_bytes_to_samples(s, async->scan_progress); 529 530 if (samples_left < nsamples) 531 return samples_left; 532 return nsamples; 533 } 534 EXPORT_SYMBOL_GPL(comedi_nsamples_left); 535 536 /** 537 * comedi_inc_scan_progress() - Update scan progress in asynchronous command 538 * @s: COMEDI subdevice. 539 * @num_bytes: Amount of data in bytes to increment scan progress. 540 * 541 * Increments the scan progress by the number of bytes specified by @num_bytes. 542 * If the scan progress reaches or exceeds the scan length in bytes, reduce 543 * it modulo the scan length in bytes and set the "end of scan" asynchronous 544 * event flag (%COMEDI_CB_EOS) to be processed later. 545 */ 546 void comedi_inc_scan_progress(struct comedi_subdevice *s, 547 unsigned int num_bytes) 548 { 549 struct comedi_async *async = s->async; 550 struct comedi_cmd *cmd = &async->cmd; 551 unsigned int scan_length = comedi_bytes_per_scan(s); 552 553 /* track the 'cur_chan' for non-SDF_PACKED subdevices */ 554 if (!(s->subdev_flags & SDF_PACKED)) { 555 async->cur_chan += comedi_bytes_to_samples(s, num_bytes); 556 async->cur_chan %= cmd->chanlist_len; 557 } 558 559 async->scan_progress += num_bytes; 560 if (async->scan_progress >= scan_length) { 561 unsigned int nscans = async->scan_progress / scan_length; 562 563 if (async->scans_done < (UINT_MAX - nscans)) 564 async->scans_done += nscans; 565 else 566 async->scans_done = UINT_MAX; 567 568 async->scan_progress %= scan_length; 569 async->events |= COMEDI_CB_EOS; 570 } 571 } 572 EXPORT_SYMBOL_GPL(comedi_inc_scan_progress); 573 574 /** 575 * comedi_handle_events() - Handle events and possibly stop acquisition 576 * @dev: COMEDI device. 577 * @s: COMEDI subdevice. 578 * 579 * Handles outstanding asynchronous acquisition event flags associated 580 * with the subdevice. Call the subdevice's @s->cancel() handler if the 581 * "end of acquisition", "error" or "overflow" event flags are set in order 582 * to stop the acquisition at the driver level. 583 * 584 * Calls comedi_event() to further process the event flags, which may mark 585 * the asynchronous command as no longer running, possibly terminated with 586 * an error, and may wake up tasks. 587 * 588 * Return a bit-mask of the handled events. 589 */ 590 unsigned int comedi_handle_events(struct comedi_device *dev, 591 struct comedi_subdevice *s) 592 { 593 unsigned int events = s->async->events; 594 595 if (events == 0) 596 return events; 597 598 if ((events & COMEDI_CB_CANCEL_MASK) && s->cancel) 599 s->cancel(dev, s); 600 601 comedi_event(dev, s); 602 603 return events; 604 } 605 EXPORT_SYMBOL_GPL(comedi_handle_events); 606 607 static int insn_rw_emulate_bits(struct comedi_device *dev, 608 struct comedi_subdevice *s, 609 struct comedi_insn *insn, 610 unsigned int *data) 611 { 612 struct comedi_insn _insn; 613 unsigned int chan = CR_CHAN(insn->chanspec); 614 unsigned int base_chan = (chan < 32) ? 0 : chan; 615 unsigned int _data[2]; 616 int ret; 617 618 if (insn->n == 0) 619 return 0; 620 621 memset(_data, 0, sizeof(_data)); 622 memset(&_insn, 0, sizeof(_insn)); 623 _insn.insn = INSN_BITS; 624 _insn.chanspec = base_chan; 625 _insn.n = 2; 626 _insn.subdev = insn->subdev; 627 628 if (insn->insn == INSN_WRITE) { 629 if (!(s->subdev_flags & SDF_WRITABLE)) 630 return -EINVAL; 631 _data[0] = 1U << (chan - base_chan); /* mask */ 632 _data[1] = data[0] ? (1U << (chan - base_chan)) : 0; /* bits */ 633 } 634 635 ret = s->insn_bits(dev, s, &_insn, _data); 636 if (ret < 0) 637 return ret; 638 639 if (insn->insn == INSN_READ) 640 data[0] = (_data[1] >> (chan - base_chan)) & 1; 641 642 return 1; 643 } 644 645 static int __comedi_device_postconfig_async(struct comedi_device *dev, 646 struct comedi_subdevice *s) 647 { 648 struct comedi_async *async; 649 unsigned int buf_size; 650 int ret; 651 652 lockdep_assert_held(&dev->mutex); 653 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) { 654 dev_warn(dev->class_dev, 655 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n"); 656 return -EINVAL; 657 } 658 if (!s->do_cmdtest) { 659 dev_warn(dev->class_dev, 660 "async subdevices must have a do_cmdtest() function\n"); 661 return -EINVAL; 662 } 663 if (!s->cancel) 664 dev_warn(dev->class_dev, 665 "async subdevices should have a cancel() function\n"); 666 667 async = kzalloc(sizeof(*async), GFP_KERNEL); 668 if (!async) 669 return -ENOMEM; 670 671 init_waitqueue_head(&async->wait_head); 672 s->async = async; 673 674 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024; 675 buf_size = comedi_default_buf_size_kb * 1024; 676 if (buf_size > async->max_bufsize) 677 buf_size = async->max_bufsize; 678 679 if (comedi_buf_alloc(dev, s, buf_size) < 0) { 680 dev_warn(dev->class_dev, "Buffer allocation failed\n"); 681 return -ENOMEM; 682 } 683 if (s->buf_change) { 684 ret = s->buf_change(dev, s); 685 if (ret < 0) 686 return ret; 687 } 688 689 comedi_alloc_subdevice_minor(s); 690 691 return 0; 692 } 693 694 static int __comedi_device_postconfig(struct comedi_device *dev) 695 { 696 struct comedi_subdevice *s; 697 int ret; 698 int i; 699 700 lockdep_assert_held(&dev->mutex); 701 if (!dev->insn_device_config) 702 dev->insn_device_config = insn_device_inval; 703 704 if (!dev->get_valid_routes) 705 dev->get_valid_routes = get_zero_valid_routes; 706 707 for (i = 0; i < dev->n_subdevices; i++) { 708 s = &dev->subdevices[i]; 709 710 if (s->type == COMEDI_SUBD_UNUSED) 711 continue; 712 713 if (s->type == COMEDI_SUBD_DO) { 714 if (s->n_chan < 32) 715 s->io_bits = (1U << s->n_chan) - 1; 716 else 717 s->io_bits = 0xffffffff; 718 } 719 720 if (s->len_chanlist == 0) 721 s->len_chanlist = 1; 722 723 if (s->do_cmd) { 724 ret = __comedi_device_postconfig_async(dev, s); 725 if (ret) 726 return ret; 727 } 728 729 if (!s->range_table && !s->range_table_list) 730 s->range_table = &range_unknown; 731 732 if (!s->insn_read && s->insn_bits) 733 s->insn_read = insn_rw_emulate_bits; 734 if (!s->insn_write && s->insn_bits) 735 s->insn_write = insn_rw_emulate_bits; 736 737 if (!s->insn_read) 738 s->insn_read = insn_inval; 739 if (!s->insn_write) 740 s->insn_write = insn_inval; 741 if (!s->insn_bits) 742 s->insn_bits = insn_inval; 743 if (!s->insn_config) 744 s->insn_config = insn_inval; 745 746 if (!s->poll) 747 s->poll = poll_invalid; 748 } 749 750 return 0; 751 } 752 753 /* do a little post-config cleanup */ 754 static int comedi_device_postconfig(struct comedi_device *dev) 755 { 756 int ret; 757 758 lockdep_assert_held(&dev->mutex); 759 ret = __comedi_device_postconfig(dev); 760 if (ret < 0) 761 return ret; 762 down_write(&dev->attach_lock); 763 dev->attached = true; 764 up_write(&dev->attach_lock); 765 return 0; 766 } 767 768 /* 769 * Generic recognize function for drivers that register their supported 770 * board names. 771 * 772 * 'driv->board_name' points to a 'const char *' member within the 773 * zeroth element of an array of some private board information 774 * structure, say 'struct foo_board' containing a member 'const char 775 * *board_name' that is initialized to point to a board name string that 776 * is one of the candidates matched against this function's 'name' 777 * parameter. 778 * 779 * 'driv->offset' is the size of the private board information 780 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is 781 * the length of the array of private board information structures. 782 * 783 * If one of the board names in the array of private board information 784 * structures matches the name supplied to this function, the function 785 * returns a pointer to the pointer to the board name, otherwise it 786 * returns NULL. The return value ends up in the 'board_ptr' member of 787 * a 'struct comedi_device' that the low-level comedi driver's 788 * 'attach()' hook can convert to a point to a particular element of its 789 * array of private board information structures by subtracting the 790 * offset of the member that points to the board name. (No subtraction 791 * is required if the board name pointer is the first member of the 792 * private board information structure, which is generally the case.) 793 */ 794 static void *comedi_recognize(struct comedi_driver *driv, const char *name) 795 { 796 char **name_ptr = (char **)driv->board_name; 797 int i; 798 799 for (i = 0; i < driv->num_names; i++) { 800 if (strcmp(*name_ptr, name) == 0) 801 return name_ptr; 802 name_ptr = (void *)name_ptr + driv->offset; 803 } 804 805 return NULL; 806 } 807 808 static void comedi_report_boards(struct comedi_driver *driv) 809 { 810 unsigned int i; 811 const char *const *name_ptr; 812 813 pr_info("comedi: valid board names for %s driver are:\n", 814 driv->driver_name); 815 816 name_ptr = driv->board_name; 817 for (i = 0; i < driv->num_names; i++) { 818 pr_info(" %s\n", *name_ptr); 819 name_ptr = (const char **)((char *)name_ptr + driv->offset); 820 } 821 822 if (driv->num_names == 0) 823 pr_info(" %s\n", driv->driver_name); 824 } 825 826 /** 827 * comedi_load_firmware() - Request and load firmware for a device 828 * @dev: COMEDI device. 829 * @device: Hardware device. 830 * @name: The name of the firmware image. 831 * @cb: Callback to the upload the firmware image. 832 * @context: Private context from the driver. 833 * 834 * Sends a firmware request for the hardware device and waits for it. Calls 835 * the callback function to upload the firmware to the device, them releases 836 * the firmware. 837 * 838 * Returns 0 on success, -EINVAL if @cb is NULL, or a negative error number 839 * from the firmware request or the callback function. 840 */ 841 int comedi_load_firmware(struct comedi_device *dev, 842 struct device *device, 843 const char *name, 844 int (*cb)(struct comedi_device *dev, 845 const u8 *data, size_t size, 846 unsigned long context), 847 unsigned long context) 848 { 849 const struct firmware *fw; 850 int ret; 851 852 if (!cb) 853 return -EINVAL; 854 855 ret = request_firmware(&fw, name, device); 856 if (ret == 0) { 857 ret = cb(dev, fw->data, fw->size, context); 858 release_firmware(fw); 859 } 860 861 return min(ret, 0); 862 } 863 EXPORT_SYMBOL_GPL(comedi_load_firmware); 864 865 /** 866 * __comedi_request_region() - Request an I/O region for a legacy driver 867 * @dev: COMEDI device. 868 * @start: Base address of the I/O region. 869 * @len: Length of the I/O region. 870 * 871 * Requests the specified I/O port region which must start at a non-zero 872 * address. 873 * 874 * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request 875 * fails. 876 */ 877 int __comedi_request_region(struct comedi_device *dev, 878 unsigned long start, unsigned long len) 879 { 880 if (!start) { 881 dev_warn(dev->class_dev, 882 "%s: a I/O base address must be specified\n", 883 dev->board_name); 884 return -EINVAL; 885 } 886 887 if (!request_region(start, len, dev->board_name)) { 888 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n", 889 dev->board_name, start, len); 890 return -EIO; 891 } 892 893 return 0; 894 } 895 EXPORT_SYMBOL_GPL(__comedi_request_region); 896 897 /** 898 * comedi_request_region() - Request an I/O region for a legacy driver 899 * @dev: COMEDI device. 900 * @start: Base address of the I/O region. 901 * @len: Length of the I/O region. 902 * 903 * Requests the specified I/O port region which must start at a non-zero 904 * address. 905 * 906 * On success, @dev->iobase is set to the base address of the region and 907 * @dev->iolen is set to its length. 908 * 909 * Returns 0 on success, -EINVAL if @start is 0, or -EIO if the request 910 * fails. 911 */ 912 int comedi_request_region(struct comedi_device *dev, 913 unsigned long start, unsigned long len) 914 { 915 int ret; 916 917 ret = __comedi_request_region(dev, start, len); 918 if (ret == 0) { 919 dev->iobase = start; 920 dev->iolen = len; 921 } 922 923 return ret; 924 } 925 EXPORT_SYMBOL_GPL(comedi_request_region); 926 927 /** 928 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers 929 * @dev: COMEDI device. 930 * 931 * This is a simple, generic 'detach' handler for legacy COMEDI devices that 932 * just use a single I/O port region and possibly an IRQ and that don't need 933 * any special clean-up for their private device or subdevice storage. It 934 * can also be called by a driver-specific 'detach' handler. 935 * 936 * If @dev->irq is non-zero, the IRQ will be freed. If @dev->iobase and 937 * @dev->iolen are both non-zero, the I/O port region will be released. 938 */ 939 void comedi_legacy_detach(struct comedi_device *dev) 940 { 941 if (dev->irq) { 942 free_irq(dev->irq, dev); 943 dev->irq = 0; 944 } 945 if (dev->iobase && dev->iolen) { 946 release_region(dev->iobase, dev->iolen); 947 dev->iobase = 0; 948 dev->iolen = 0; 949 } 950 } 951 EXPORT_SYMBOL_GPL(comedi_legacy_detach); 952 953 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it) 954 { 955 struct comedi_driver *driv; 956 int ret; 957 958 lockdep_assert_held(&dev->mutex); 959 if (dev->attached) 960 return -EBUSY; 961 962 mutex_lock(&comedi_drivers_list_lock); 963 for (driv = comedi_drivers; driv; driv = driv->next) { 964 if (!try_module_get(driv->module)) 965 continue; 966 if (driv->num_names) { 967 dev->board_ptr = comedi_recognize(driv, it->board_name); 968 if (dev->board_ptr) 969 break; 970 } else if (strcmp(driv->driver_name, it->board_name) == 0) { 971 break; 972 } 973 module_put(driv->module); 974 } 975 if (!driv) { 976 /* recognize has failed if we get here */ 977 /* report valid board names before returning error */ 978 for (driv = comedi_drivers; driv; driv = driv->next) { 979 if (!try_module_get(driv->module)) 980 continue; 981 comedi_report_boards(driv); 982 module_put(driv->module); 983 } 984 ret = -EIO; 985 goto out; 986 } 987 if (!driv->attach) { 988 /* driver does not support manual configuration */ 989 dev_warn(dev->class_dev, 990 "driver '%s' does not support attach using comedi_config\n", 991 driv->driver_name); 992 module_put(driv->module); 993 ret = -EIO; 994 goto out; 995 } 996 dev->driver = driv; 997 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr 998 : dev->driver->driver_name; 999 ret = driv->attach(dev, it); 1000 if (ret >= 0) 1001 ret = comedi_device_postconfig(dev); 1002 if (ret < 0) { 1003 comedi_device_detach(dev); 1004 module_put(driv->module); 1005 } 1006 /* On success, the driver module count has been incremented. */ 1007 out: 1008 mutex_unlock(&comedi_drivers_list_lock); 1009 return ret; 1010 } 1011 1012 /** 1013 * comedi_auto_config() - Create a COMEDI device for a hardware device 1014 * @hardware_device: Hardware device. 1015 * @driver: COMEDI low-level driver for the hardware device. 1016 * @context: Driver context for the auto_attach handler. 1017 * 1018 * Allocates a new COMEDI device for the hardware device and calls the 1019 * low-level driver's 'auto_attach' handler to set-up the hardware and 1020 * allocate the COMEDI subdevices. Additional "post-configuration" setting 1021 * up is performed on successful return from the 'auto_attach' handler. 1022 * If the 'auto_attach' handler fails, the low-level driver's 'detach' 1023 * handler will be called as part of the clean-up. 1024 * 1025 * This is usually called from a wrapper function in a bus-specific COMEDI 1026 * module, which in turn is usually called from a bus device 'probe' 1027 * function in the low-level driver. 1028 * 1029 * Returns 0 on success, -EINVAL if the parameters are invalid or the 1030 * post-configuration determines the driver has set the COMEDI device up 1031 * incorrectly, -ENOMEM if failed to allocate memory, -EBUSY if run out of 1032 * COMEDI minor device numbers, or some negative error number returned by 1033 * the driver's 'auto_attach' handler. 1034 */ 1035 int comedi_auto_config(struct device *hardware_device, 1036 struct comedi_driver *driver, unsigned long context) 1037 { 1038 struct comedi_device *dev; 1039 int ret; 1040 1041 if (!hardware_device) { 1042 pr_warn("BUG! %s called with NULL hardware_device\n", __func__); 1043 return -EINVAL; 1044 } 1045 if (!driver) { 1046 dev_warn(hardware_device, 1047 "BUG! %s called with NULL comedi driver\n", __func__); 1048 return -EINVAL; 1049 } 1050 1051 if (!driver->auto_attach) { 1052 dev_warn(hardware_device, 1053 "BUG! comedi driver '%s' has no auto_attach handler\n", 1054 driver->driver_name); 1055 return -EINVAL; 1056 } 1057 1058 dev = comedi_alloc_board_minor(hardware_device); 1059 if (IS_ERR(dev)) { 1060 dev_warn(hardware_device, 1061 "driver '%s' could not create device.\n", 1062 driver->driver_name); 1063 return PTR_ERR(dev); 1064 } 1065 /* Note: comedi_alloc_board_minor() locked dev->mutex. */ 1066 lockdep_assert_held(&dev->mutex); 1067 1068 dev->driver = driver; 1069 dev->board_name = dev->driver->driver_name; 1070 ret = driver->auto_attach(dev, context); 1071 if (ret >= 0) 1072 ret = comedi_device_postconfig(dev); 1073 1074 if (ret < 0) { 1075 dev_warn(hardware_device, 1076 "driver '%s' failed to auto-configure device.\n", 1077 driver->driver_name); 1078 mutex_unlock(&dev->mutex); 1079 comedi_release_hardware_device(hardware_device); 1080 } else { 1081 /* 1082 * class_dev should be set properly here 1083 * after a successful auto config 1084 */ 1085 dev_info(dev->class_dev, 1086 "driver '%s' has successfully auto-configured '%s'.\n", 1087 driver->driver_name, dev->board_name); 1088 mutex_unlock(&dev->mutex); 1089 } 1090 return ret; 1091 } 1092 EXPORT_SYMBOL_GPL(comedi_auto_config); 1093 1094 /** 1095 * comedi_auto_unconfig() - Unconfigure auto-allocated COMEDI device 1096 * @hardware_device: Hardware device previously passed to 1097 * comedi_auto_config(). 1098 * 1099 * Cleans up and eventually destroys the COMEDI device allocated by 1100 * comedi_auto_config() for the same hardware device. As part of this 1101 * clean-up, the low-level COMEDI driver's 'detach' handler will be called. 1102 * (The COMEDI device itself will persist in an unattached state if it is 1103 * still open, until it is released, and any mmapped buffers will persist 1104 * until they are munmapped.) 1105 * 1106 * This is usually called from a wrapper module in a bus-specific COMEDI 1107 * module, which in turn is usually set as the bus device 'remove' function 1108 * in the low-level COMEDI driver. 1109 */ 1110 void comedi_auto_unconfig(struct device *hardware_device) 1111 { 1112 if (!hardware_device) 1113 return; 1114 comedi_release_hardware_device(hardware_device); 1115 } 1116 EXPORT_SYMBOL_GPL(comedi_auto_unconfig); 1117 1118 /** 1119 * comedi_driver_register() - Register a low-level COMEDI driver 1120 * @driver: Low-level COMEDI driver. 1121 * 1122 * The low-level COMEDI driver is added to the list of registered COMEDI 1123 * drivers. This is used by the handler for the "/proc/comedi" file and is 1124 * also used by the handler for the %COMEDI_DEVCONFIG ioctl to configure 1125 * "legacy" COMEDI devices (for those low-level drivers that support it). 1126 * 1127 * Returns 0. 1128 */ 1129 int comedi_driver_register(struct comedi_driver *driver) 1130 { 1131 mutex_lock(&comedi_drivers_list_lock); 1132 driver->next = comedi_drivers; 1133 comedi_drivers = driver; 1134 mutex_unlock(&comedi_drivers_list_lock); 1135 1136 return 0; 1137 } 1138 EXPORT_SYMBOL_GPL(comedi_driver_register); 1139 1140 /** 1141 * comedi_driver_unregister() - Unregister a low-level COMEDI driver 1142 * @driver: Low-level COMEDI driver. 1143 * 1144 * The low-level COMEDI driver is removed from the list of registered COMEDI 1145 * drivers. Detaches any COMEDI devices attached to the driver, which will 1146 * result in the low-level driver's 'detach' handler being called for those 1147 * devices before this function returns. 1148 */ 1149 void comedi_driver_unregister(struct comedi_driver *driver) 1150 { 1151 struct comedi_driver *prev; 1152 int i; 1153 1154 /* unlink the driver */ 1155 mutex_lock(&comedi_drivers_list_lock); 1156 if (comedi_drivers == driver) { 1157 comedi_drivers = driver->next; 1158 } else { 1159 for (prev = comedi_drivers; prev->next; prev = prev->next) { 1160 if (prev->next == driver) { 1161 prev->next = driver->next; 1162 break; 1163 } 1164 } 1165 } 1166 mutex_unlock(&comedi_drivers_list_lock); 1167 1168 /* check for devices using this driver */ 1169 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) { 1170 struct comedi_device *dev = comedi_dev_get_from_minor(i); 1171 1172 if (!dev) 1173 continue; 1174 1175 mutex_lock(&dev->mutex); 1176 if (dev->attached && dev->driver == driver) { 1177 if (dev->use_count) 1178 dev_warn(dev->class_dev, 1179 "BUG! detaching device with use_count=%d\n", 1180 dev->use_count); 1181 comedi_device_detach(dev); 1182 } 1183 mutex_unlock(&dev->mutex); 1184 comedi_dev_put(dev); 1185 } 1186 } 1187 EXPORT_SYMBOL_GPL(comedi_driver_unregister); 1188