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