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