at v3.18 864 lines 22 kB view raw
1/* 2 module/drivers.c 3 functions for manipulating drivers 4 5 COMEDI - Linux Control and Measurement Device Interface 6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org> 7 Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net> 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18*/ 19 20#include <linux/device.h> 21#include <linux/module.h> 22#include <linux/errno.h> 23#include <linux/kconfig.h> 24#include <linux/kernel.h> 25#include <linux/sched.h> 26#include <linux/fcntl.h> 27#include <linux/ioport.h> 28#include <linux/mm.h> 29#include <linux/slab.h> 30#include <linux/highmem.h> /* for SuSE brokenness */ 31#include <linux/vmalloc.h> 32#include <linux/cdev.h> 33#include <linux/dma-mapping.h> 34#include <linux/io.h> 35#include <linux/interrupt.h> 36#include <linux/firmware.h> 37 38#include "comedidev.h" 39#include "comedi_internal.h" 40 41struct comedi_driver *comedi_drivers; 42/* protects access to comedi_drivers */ 43DEFINE_MUTEX(comedi_drivers_list_lock); 44 45int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev) 46{ 47 if (hw_dev == dev->hw_dev) 48 return 0; 49 if (dev->hw_dev != NULL) 50 return -EEXIST; 51 dev->hw_dev = get_device(hw_dev); 52 return 0; 53} 54EXPORT_SYMBOL_GPL(comedi_set_hw_dev); 55 56static void comedi_clear_hw_dev(struct comedi_device *dev) 57{ 58 put_device(dev->hw_dev); 59 dev->hw_dev = NULL; 60} 61 62/** 63 * comedi_alloc_devpriv() - Allocate memory for the device private data. 64 * @dev: comedi_device struct 65 * @size: size of the memory to allocate 66 */ 67void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size) 68{ 69 dev->private = kzalloc(size, GFP_KERNEL); 70 return dev->private; 71} 72EXPORT_SYMBOL_GPL(comedi_alloc_devpriv); 73 74int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices) 75{ 76 struct comedi_subdevice *s; 77 int i; 78 79 if (num_subdevices < 1) 80 return -EINVAL; 81 82 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL); 83 if (!s) 84 return -ENOMEM; 85 dev->subdevices = s; 86 dev->n_subdevices = num_subdevices; 87 88 for (i = 0; i < num_subdevices; ++i) { 89 s = &dev->subdevices[i]; 90 s->device = dev; 91 s->index = i; 92 s->async_dma_dir = DMA_NONE; 93 spin_lock_init(&s->spin_lock); 94 s->minor = -1; 95 } 96 return 0; 97} 98EXPORT_SYMBOL_GPL(comedi_alloc_subdevices); 99 100/** 101 * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback. 102 * @s: comedi_subdevice struct 103 */ 104int comedi_alloc_subdev_readback(struct comedi_subdevice *s) 105{ 106 if (!s->n_chan) 107 return -EINVAL; 108 109 s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL); 110 if (!s->readback) 111 return -ENOMEM; 112 return 0; 113} 114EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback); 115 116static void comedi_device_detach_cleanup(struct comedi_device *dev) 117{ 118 int i; 119 struct comedi_subdevice *s; 120 121 if (dev->subdevices) { 122 for (i = 0; i < dev->n_subdevices; i++) { 123 s = &dev->subdevices[i]; 124 if (s->runflags & SRF_FREE_SPRIV) 125 kfree(s->private); 126 comedi_free_subdevice_minor(s); 127 if (s->async) { 128 comedi_buf_alloc(dev, s, 0); 129 kfree(s->async); 130 } 131 kfree(s->readback); 132 } 133 kfree(dev->subdevices); 134 dev->subdevices = NULL; 135 dev->n_subdevices = 0; 136 } 137 kfree(dev->private); 138 dev->private = NULL; 139 dev->driver = NULL; 140 dev->board_name = NULL; 141 dev->board_ptr = NULL; 142 dev->mmio = NULL; 143 dev->iobase = 0; 144 dev->iolen = 0; 145 dev->ioenabled = false; 146 dev->irq = 0; 147 dev->read_subdev = NULL; 148 dev->write_subdev = NULL; 149 dev->open = NULL; 150 dev->close = NULL; 151 comedi_clear_hw_dev(dev); 152} 153 154void comedi_device_detach(struct comedi_device *dev) 155{ 156 comedi_device_cancel_all(dev); 157 down_write(&dev->attach_lock); 158 dev->attached = false; 159 dev->detach_count++; 160 if (dev->driver) 161 dev->driver->detach(dev); 162 comedi_device_detach_cleanup(dev); 163 up_write(&dev->attach_lock); 164} 165 166static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s) 167{ 168 return -EINVAL; 169} 170 171int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s, 172 struct comedi_insn *insn, unsigned int *data) 173{ 174 return -EINVAL; 175} 176 177/** 178 * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback. 179 * @dev: comedi_device struct 180 * @s: comedi_subdevice struct 181 * @insn: comedi_insn struct 182 * @data: pointer to return the readback data 183 */ 184int comedi_readback_insn_read(struct comedi_device *dev, 185 struct comedi_subdevice *s, 186 struct comedi_insn *insn, 187 unsigned int *data) 188{ 189 unsigned int chan = CR_CHAN(insn->chanspec); 190 int i; 191 192 if (!s->readback) 193 return -EINVAL; 194 195 for (i = 0; i < insn->n; i++) 196 data[i] = s->readback[chan]; 197 198 return insn->n; 199} 200EXPORT_SYMBOL_GPL(comedi_readback_insn_read); 201 202/** 203 * comedi_timeout() - busy-wait for a driver condition to occur. 204 * @dev: comedi_device struct 205 * @s: comedi_subdevice struct 206 * @insn: comedi_insn struct 207 * @cb: callback to check for the condition 208 * @context: private context from the driver 209 */ 210int comedi_timeout(struct comedi_device *dev, 211 struct comedi_subdevice *s, 212 struct comedi_insn *insn, 213 int (*cb)(struct comedi_device *dev, 214 struct comedi_subdevice *s, 215 struct comedi_insn *insn, 216 unsigned long context), 217 unsigned long context) 218{ 219 unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS); 220 int ret; 221 222 while (time_before(jiffies, timeout)) { 223 ret = cb(dev, s, insn, context); 224 if (ret != -EBUSY) 225 return ret; /* success (0) or non EBUSY errno */ 226 cpu_relax(); 227 } 228 return -ETIMEDOUT; 229} 230EXPORT_SYMBOL_GPL(comedi_timeout); 231 232/** 233 * comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices. 234 * @dev: comedi_device struct 235 * @s: comedi_subdevice struct 236 * @insn: comedi_insn struct 237 * @data: parameters for the @insn 238 * @mask: io_bits mask for grouped channels 239 */ 240int comedi_dio_insn_config(struct comedi_device *dev, 241 struct comedi_subdevice *s, 242 struct comedi_insn *insn, 243 unsigned int *data, 244 unsigned int mask) 245{ 246 unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec); 247 248 if (!mask) 249 mask = chan_mask; 250 251 switch (data[0]) { 252 case INSN_CONFIG_DIO_INPUT: 253 s->io_bits &= ~mask; 254 break; 255 256 case INSN_CONFIG_DIO_OUTPUT: 257 s->io_bits |= mask; 258 break; 259 260 case INSN_CONFIG_DIO_QUERY: 261 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT; 262 return insn->n; 263 264 default: 265 return -EINVAL; 266 } 267 268 return 0; 269} 270EXPORT_SYMBOL_GPL(comedi_dio_insn_config); 271 272/** 273 * comedi_dio_update_state() - update the internal state of DIO subdevices. 274 * @s: comedi_subdevice struct 275 * @data: the channel mask and bits to update 276 */ 277unsigned int comedi_dio_update_state(struct comedi_subdevice *s, 278 unsigned int *data) 279{ 280 unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1) 281 : 0xffffffff; 282 unsigned int mask = data[0] & chanmask; 283 unsigned int bits = data[1]; 284 285 if (mask) { 286 s->state &= ~mask; 287 s->state |= (bits & mask); 288 } 289 290 return mask; 291} 292EXPORT_SYMBOL_GPL(comedi_dio_update_state); 293 294/** 295 * comedi_bytes_per_scan - get length of asynchronous command "scan" in bytes 296 * @s: comedi_subdevice struct 297 * 298 * Determines the overall scan length according to the subdevice type and the 299 * number of channels in the scan. 300 * 301 * For digital input, output or input/output subdevices, samples for multiple 302 * channels are assumed to be packed into one or more unsigned short or 303 * unsigned int values according to the subdevice's SDF_LSAMPL flag. For other 304 * types of subdevice, samples are assumed to occupy a whole unsigned short or 305 * unsigned int according to the SDF_LSAMPL flag. 306 * 307 * Returns the overall scan length in bytes. 308 */ 309unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s) 310{ 311 struct comedi_cmd *cmd = &s->async->cmd; 312 unsigned int num_samples; 313 unsigned int bits_per_sample; 314 315 switch (s->type) { 316 case COMEDI_SUBD_DI: 317 case COMEDI_SUBD_DO: 318 case COMEDI_SUBD_DIO: 319 bits_per_sample = 8 * bytes_per_sample(s); 320 num_samples = (cmd->chanlist_len + bits_per_sample - 1) / 321 bits_per_sample; 322 break; 323 default: 324 num_samples = cmd->chanlist_len; 325 break; 326 } 327 return num_samples * bytes_per_sample(s); 328} 329EXPORT_SYMBOL_GPL(comedi_bytes_per_scan); 330 331/** 332 * comedi_inc_scan_progress - update scan progress in asynchronous command 333 * @s: comedi_subdevice struct 334 * @num_bytes: amount of data in bytes to increment scan progress 335 * 336 * Increments the scan progress by the number of bytes specified by num_bytes. 337 * If the scan progress reaches or exceeds the scan length in bytes, reduce 338 * it modulo the scan length in bytes and set the "end of scan" asynchronous 339 * event flag to be processed later. 340 */ 341void comedi_inc_scan_progress(struct comedi_subdevice *s, 342 unsigned int num_bytes) 343{ 344 struct comedi_async *async = s->async; 345 unsigned int scan_length = comedi_bytes_per_scan(s); 346 347 async->scan_progress += num_bytes; 348 if (async->scan_progress >= scan_length) { 349 async->scan_progress %= scan_length; 350 async->events |= COMEDI_CB_EOS; 351 } 352} 353EXPORT_SYMBOL_GPL(comedi_inc_scan_progress); 354 355/** 356 * comedi_handle_events - handle events and possibly stop acquisition 357 * @dev: comedi_device struct 358 * @s: comedi_subdevice struct 359 * 360 * Handles outstanding asynchronous acquisition event flags associated 361 * with the subdevice. Call the subdevice's "->cancel()" handler if the 362 * "end of acquisition", "error" or "overflow" event flags are set in order 363 * to stop the acquisition at the driver level. 364 * 365 * Calls comedi_event() to further process the event flags, which may mark 366 * the asynchronous command as no longer running, possibly terminated with 367 * an error, and may wake up tasks. 368 * 369 * Return a bit-mask of the handled events. 370 */ 371unsigned int comedi_handle_events(struct comedi_device *dev, 372 struct comedi_subdevice *s) 373{ 374 unsigned int events = s->async->events; 375 376 if (events == 0) 377 return events; 378 379 if (events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW)) 380 s->cancel(dev, s); 381 382 comedi_event(dev, s); 383 384 return events; 385} 386EXPORT_SYMBOL_GPL(comedi_handle_events); 387 388static int insn_rw_emulate_bits(struct comedi_device *dev, 389 struct comedi_subdevice *s, 390 struct comedi_insn *insn, unsigned int *data) 391{ 392 struct comedi_insn new_insn; 393 int ret; 394 static const unsigned channels_per_bitfield = 32; 395 396 unsigned chan = CR_CHAN(insn->chanspec); 397 const unsigned base_bitfield_channel = 398 (chan < channels_per_bitfield) ? 0 : chan; 399 unsigned int new_data[2]; 400 401 memset(new_data, 0, sizeof(new_data)); 402 memset(&new_insn, 0, sizeof(new_insn)); 403 new_insn.insn = INSN_BITS; 404 new_insn.chanspec = base_bitfield_channel; 405 new_insn.n = 2; 406 new_insn.subdev = insn->subdev; 407 408 if (insn->insn == INSN_WRITE) { 409 if (!(s->subdev_flags & SDF_WRITABLE)) 410 return -EINVAL; 411 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */ 412 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) 413 : 0; /* bits */ 414 } 415 416 ret = s->insn_bits(dev, s, &new_insn, new_data); 417 if (ret < 0) 418 return ret; 419 420 if (insn->insn == INSN_READ) 421 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1; 422 423 return 1; 424} 425 426static int __comedi_device_postconfig_async(struct comedi_device *dev, 427 struct comedi_subdevice *s) 428{ 429 struct comedi_async *async; 430 unsigned int buf_size; 431 int ret; 432 433 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) { 434 dev_warn(dev->class_dev, 435 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n"); 436 return -EINVAL; 437 } 438 if (!s->do_cmdtest) { 439 dev_warn(dev->class_dev, 440 "async subdevices must have a do_cmdtest() function\n"); 441 return -EINVAL; 442 } 443 444 async = kzalloc(sizeof(*async), GFP_KERNEL); 445 if (!async) 446 return -ENOMEM; 447 448 init_waitqueue_head(&async->wait_head); 449 s->async = async; 450 451 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024; 452 buf_size = comedi_default_buf_size_kb * 1024; 453 if (buf_size > async->max_bufsize) 454 buf_size = async->max_bufsize; 455 456 if (comedi_buf_alloc(dev, s, buf_size) < 0) { 457 dev_warn(dev->class_dev, "Buffer allocation failed\n"); 458 return -ENOMEM; 459 } 460 if (s->buf_change) { 461 ret = s->buf_change(dev, s); 462 if (ret < 0) 463 return ret; 464 } 465 466 comedi_alloc_subdevice_minor(s); 467 468 return 0; 469} 470 471static int __comedi_device_postconfig(struct comedi_device *dev) 472{ 473 struct comedi_subdevice *s; 474 int ret; 475 int i; 476 477 for (i = 0; i < dev->n_subdevices; i++) { 478 s = &dev->subdevices[i]; 479 480 if (s->type == COMEDI_SUBD_UNUSED) 481 continue; 482 483 if (s->type == COMEDI_SUBD_DO) { 484 if (s->n_chan < 32) 485 s->io_bits = (1 << s->n_chan) - 1; 486 else 487 s->io_bits = 0xffffffff; 488 } 489 490 if (s->len_chanlist == 0) 491 s->len_chanlist = 1; 492 493 if (s->do_cmd) { 494 ret = __comedi_device_postconfig_async(dev, s); 495 if (ret) 496 return ret; 497 } 498 499 if (!s->range_table && !s->range_table_list) 500 s->range_table = &range_unknown; 501 502 if (!s->insn_read && s->insn_bits) 503 s->insn_read = insn_rw_emulate_bits; 504 if (!s->insn_write && s->insn_bits) 505 s->insn_write = insn_rw_emulate_bits; 506 507 if (!s->insn_read) 508 s->insn_read = insn_inval; 509 if (!s->insn_write) 510 s->insn_write = insn_inval; 511 if (!s->insn_bits) 512 s->insn_bits = insn_inval; 513 if (!s->insn_config) 514 s->insn_config = insn_inval; 515 516 if (!s->poll) 517 s->poll = poll_invalid; 518 } 519 520 return 0; 521} 522 523/* do a little post-config cleanup */ 524static int comedi_device_postconfig(struct comedi_device *dev) 525{ 526 int ret; 527 528 ret = __comedi_device_postconfig(dev); 529 if (ret < 0) 530 return ret; 531 down_write(&dev->attach_lock); 532 dev->attached = true; 533 up_write(&dev->attach_lock); 534 return 0; 535} 536 537/* 538 * Generic recognize function for drivers that register their supported 539 * board names. 540 * 541 * 'driv->board_name' points to a 'const char *' member within the 542 * zeroth element of an array of some private board information 543 * structure, say 'struct foo_board' containing a member 'const char 544 * *board_name' that is initialized to point to a board name string that 545 * is one of the candidates matched against this function's 'name' 546 * parameter. 547 * 548 * 'driv->offset' is the size of the private board information 549 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is 550 * the length of the array of private board information structures. 551 * 552 * If one of the board names in the array of private board information 553 * structures matches the name supplied to this function, the function 554 * returns a pointer to the pointer to the board name, otherwise it 555 * returns NULL. The return value ends up in the 'board_ptr' member of 556 * a 'struct comedi_device' that the low-level comedi driver's 557 * 'attach()' hook can convert to a point to a particular element of its 558 * array of private board information structures by subtracting the 559 * offset of the member that points to the board name. (No subtraction 560 * is required if the board name pointer is the first member of the 561 * private board information structure, which is generally the case.) 562 */ 563static void *comedi_recognize(struct comedi_driver *driv, const char *name) 564{ 565 char **name_ptr = (char **)driv->board_name; 566 int i; 567 568 for (i = 0; i < driv->num_names; i++) { 569 if (strcmp(*name_ptr, name) == 0) 570 return name_ptr; 571 name_ptr = (void *)name_ptr + driv->offset; 572 } 573 574 return NULL; 575} 576 577static void comedi_report_boards(struct comedi_driver *driv) 578{ 579 unsigned int i; 580 const char *const *name_ptr; 581 582 pr_info("comedi: valid board names for %s driver are:\n", 583 driv->driver_name); 584 585 name_ptr = driv->board_name; 586 for (i = 0; i < driv->num_names; i++) { 587 pr_info(" %s\n", *name_ptr); 588 name_ptr = (const char **)((char *)name_ptr + driv->offset); 589 } 590 591 if (driv->num_names == 0) 592 pr_info(" %s\n", driv->driver_name); 593} 594 595/** 596 * comedi_load_firmware() - Request and load firmware for a device. 597 * @dev: comedi_device struct 598 * @hw_device: device struct for the comedi_device 599 * @name: the name of the firmware image 600 * @cb: callback to the upload the firmware image 601 * @context: private context from the driver 602 */ 603int comedi_load_firmware(struct comedi_device *dev, 604 struct device *device, 605 const char *name, 606 int (*cb)(struct comedi_device *dev, 607 const u8 *data, size_t size, 608 unsigned long context), 609 unsigned long context) 610{ 611 const struct firmware *fw; 612 int ret; 613 614 if (!cb) 615 return -EINVAL; 616 617 ret = request_firmware(&fw, name, device); 618 if (ret == 0) { 619 ret = cb(dev, fw->data, fw->size, context); 620 release_firmware(fw); 621 } 622 623 return ret < 0 ? ret : 0; 624} 625EXPORT_SYMBOL_GPL(comedi_load_firmware); 626 627/** 628 * __comedi_request_region() - Request an I/O reqion for a legacy driver. 629 * @dev: comedi_device struct 630 * @start: base address of the I/O reqion 631 * @len: length of the I/O region 632 */ 633int __comedi_request_region(struct comedi_device *dev, 634 unsigned long start, unsigned long len) 635{ 636 if (!start) { 637 dev_warn(dev->class_dev, 638 "%s: a I/O base address must be specified\n", 639 dev->board_name); 640 return -EINVAL; 641 } 642 643 if (!request_region(start, len, dev->board_name)) { 644 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n", 645 dev->board_name, start, len); 646 return -EIO; 647 } 648 649 return 0; 650} 651EXPORT_SYMBOL_GPL(__comedi_request_region); 652 653/** 654 * comedi_request_region() - Request an I/O reqion for a legacy driver. 655 * @dev: comedi_device struct 656 * @start: base address of the I/O reqion 657 * @len: length of the I/O region 658 */ 659int comedi_request_region(struct comedi_device *dev, 660 unsigned long start, unsigned long len) 661{ 662 int ret; 663 664 ret = __comedi_request_region(dev, start, len); 665 if (ret == 0) { 666 dev->iobase = start; 667 dev->iolen = len; 668 } 669 670 return ret; 671} 672EXPORT_SYMBOL_GPL(comedi_request_region); 673 674/** 675 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers. 676 * @dev: comedi_device struct 677 */ 678void comedi_legacy_detach(struct comedi_device *dev) 679{ 680 if (dev->irq) { 681 free_irq(dev->irq, dev); 682 dev->irq = 0; 683 } 684 if (dev->iobase && dev->iolen) { 685 release_region(dev->iobase, dev->iolen); 686 dev->iobase = 0; 687 dev->iolen = 0; 688 } 689} 690EXPORT_SYMBOL_GPL(comedi_legacy_detach); 691 692int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it) 693{ 694 struct comedi_driver *driv; 695 int ret; 696 697 if (dev->attached) 698 return -EBUSY; 699 700 mutex_lock(&comedi_drivers_list_lock); 701 for (driv = comedi_drivers; driv; driv = driv->next) { 702 if (!try_module_get(driv->module)) 703 continue; 704 if (driv->num_names) { 705 dev->board_ptr = comedi_recognize(driv, it->board_name); 706 if (dev->board_ptr) 707 break; 708 } else if (strcmp(driv->driver_name, it->board_name) == 0) { 709 break; 710 } 711 module_put(driv->module); 712 } 713 if (driv == NULL) { 714 /* recognize has failed if we get here */ 715 /* report valid board names before returning error */ 716 for (driv = comedi_drivers; driv; driv = driv->next) { 717 if (!try_module_get(driv->module)) 718 continue; 719 comedi_report_boards(driv); 720 module_put(driv->module); 721 } 722 ret = -EIO; 723 goto out; 724 } 725 if (driv->attach == NULL) { 726 /* driver does not support manual configuration */ 727 dev_warn(dev->class_dev, 728 "driver '%s' does not support attach using comedi_config\n", 729 driv->driver_name); 730 module_put(driv->module); 731 ret = -ENOSYS; 732 goto out; 733 } 734 dev->driver = driv; 735 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr 736 : dev->driver->driver_name; 737 ret = driv->attach(dev, it); 738 if (ret >= 0) 739 ret = comedi_device_postconfig(dev); 740 if (ret < 0) { 741 comedi_device_detach(dev); 742 module_put(driv->module); 743 } 744 /* On success, the driver module count has been incremented. */ 745out: 746 mutex_unlock(&comedi_drivers_list_lock); 747 return ret; 748} 749 750int comedi_auto_config(struct device *hardware_device, 751 struct comedi_driver *driver, unsigned long context) 752{ 753 struct comedi_device *dev; 754 int ret; 755 756 if (!hardware_device) { 757 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n"); 758 return -EINVAL; 759 } 760 if (!driver) { 761 dev_warn(hardware_device, 762 "BUG! comedi_auto_config called with NULL comedi driver\n"); 763 return -EINVAL; 764 } 765 766 if (!driver->auto_attach) { 767 dev_warn(hardware_device, 768 "BUG! comedi driver '%s' has no auto_attach handler\n", 769 driver->driver_name); 770 return -EINVAL; 771 } 772 773 dev = comedi_alloc_board_minor(hardware_device); 774 if (IS_ERR(dev)) { 775 dev_warn(hardware_device, 776 "driver '%s' could not create device.\n", 777 driver->driver_name); 778 return PTR_ERR(dev); 779 } 780 /* Note: comedi_alloc_board_minor() locked dev->mutex. */ 781 782 dev->driver = driver; 783 dev->board_name = dev->driver->driver_name; 784 ret = driver->auto_attach(dev, context); 785 if (ret >= 0) 786 ret = comedi_device_postconfig(dev); 787 mutex_unlock(&dev->mutex); 788 789 if (ret < 0) { 790 dev_warn(hardware_device, 791 "driver '%s' failed to auto-configure device.\n", 792 driver->driver_name); 793 comedi_release_hardware_device(hardware_device); 794 } else { 795 /* 796 * class_dev should be set properly here 797 * after a successful auto config 798 */ 799 dev_info(dev->class_dev, 800 "driver '%s' has successfully auto-configured '%s'.\n", 801 driver->driver_name, dev->board_name); 802 } 803 return ret; 804} 805EXPORT_SYMBOL_GPL(comedi_auto_config); 806 807void comedi_auto_unconfig(struct device *hardware_device) 808{ 809 if (hardware_device == NULL) 810 return; 811 comedi_release_hardware_device(hardware_device); 812} 813EXPORT_SYMBOL_GPL(comedi_auto_unconfig); 814 815int comedi_driver_register(struct comedi_driver *driver) 816{ 817 mutex_lock(&comedi_drivers_list_lock); 818 driver->next = comedi_drivers; 819 comedi_drivers = driver; 820 mutex_unlock(&comedi_drivers_list_lock); 821 822 return 0; 823} 824EXPORT_SYMBOL_GPL(comedi_driver_register); 825 826void comedi_driver_unregister(struct comedi_driver *driver) 827{ 828 struct comedi_driver *prev; 829 int i; 830 831 /* unlink the driver */ 832 mutex_lock(&comedi_drivers_list_lock); 833 if (comedi_drivers == driver) { 834 comedi_drivers = driver->next; 835 } else { 836 for (prev = comedi_drivers; prev->next; prev = prev->next) { 837 if (prev->next == driver) { 838 prev->next = driver->next; 839 break; 840 } 841 } 842 } 843 mutex_unlock(&comedi_drivers_list_lock); 844 845 /* check for devices using this driver */ 846 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) { 847 struct comedi_device *dev = comedi_dev_get_from_minor(i); 848 849 if (!dev) 850 continue; 851 852 mutex_lock(&dev->mutex); 853 if (dev->attached && dev->driver == driver) { 854 if (dev->use_count) 855 dev_warn(dev->class_dev, 856 "BUG! detaching device with use_count=%d\n", 857 dev->use_count); 858 comedi_device_detach(dev); 859 } 860 mutex_unlock(&dev->mutex); 861 comedi_dev_put(dev); 862 } 863} 864EXPORT_SYMBOL_GPL(comedi_driver_unregister);