drivers.c revision f146fe63416de7162090a48135d33a2b74a4efcc
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 330static int insn_rw_emulate_bits(struct comedi_device *dev, 331 struct comedi_subdevice *s, 332 struct comedi_insn *insn, unsigned int *data) 333{ 334 struct comedi_insn new_insn; 335 int ret; 336 static const unsigned channels_per_bitfield = 32; 337 338 unsigned chan = CR_CHAN(insn->chanspec); 339 const unsigned base_bitfield_channel = 340 (chan < channels_per_bitfield) ? 0 : chan; 341 unsigned int new_data[2]; 342 343 memset(new_data, 0, sizeof(new_data)); 344 memset(&new_insn, 0, sizeof(new_insn)); 345 new_insn.insn = INSN_BITS; 346 new_insn.chanspec = base_bitfield_channel; 347 new_insn.n = 2; 348 new_insn.subdev = insn->subdev; 349 350 if (insn->insn == INSN_WRITE) { 351 if (!(s->subdev_flags & SDF_WRITABLE)) 352 return -EINVAL; 353 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */ 354 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) 355 : 0; /* bits */ 356 } 357 358 ret = s->insn_bits(dev, s, &new_insn, new_data); 359 if (ret < 0) 360 return ret; 361 362 if (insn->insn == INSN_READ) 363 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1; 364 365 return 1; 366} 367 368static int __comedi_device_postconfig_async(struct comedi_device *dev, 369 struct comedi_subdevice *s) 370{ 371 struct comedi_async *async; 372 unsigned int buf_size; 373 int ret; 374 375 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) { 376 dev_warn(dev->class_dev, 377 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n"); 378 return -EINVAL; 379 } 380 if (!s->do_cmdtest) { 381 dev_warn(dev->class_dev, 382 "async subdevices must have a do_cmdtest() function\n"); 383 return -EINVAL; 384 } 385 386 async = kzalloc(sizeof(*async), GFP_KERNEL); 387 if (!async) 388 return -ENOMEM; 389 390 init_waitqueue_head(&async->wait_head); 391 s->async = async; 392 393 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024; 394 buf_size = comedi_default_buf_size_kb * 1024; 395 if (buf_size > async->max_bufsize) 396 buf_size = async->max_bufsize; 397 398 if (comedi_buf_alloc(dev, s, buf_size) < 0) { 399 dev_warn(dev->class_dev, "Buffer allocation failed\n"); 400 return -ENOMEM; 401 } 402 if (s->buf_change) { 403 ret = s->buf_change(dev, s); 404 if (ret < 0) 405 return ret; 406 } 407 408 comedi_alloc_subdevice_minor(s); 409 410 return 0; 411} 412 413static int __comedi_device_postconfig(struct comedi_device *dev) 414{ 415 struct comedi_subdevice *s; 416 int ret; 417 int i; 418 419 for (i = 0; i < dev->n_subdevices; i++) { 420 s = &dev->subdevices[i]; 421 422 if (s->type == COMEDI_SUBD_UNUSED) 423 continue; 424 425 if (s->type == COMEDI_SUBD_DO) { 426 if (s->n_chan < 32) 427 s->io_bits = (1 << s->n_chan) - 1; 428 else 429 s->io_bits = 0xffffffff; 430 } 431 432 if (s->len_chanlist == 0) 433 s->len_chanlist = 1; 434 435 if (s->do_cmd) { 436 ret = __comedi_device_postconfig_async(dev, s); 437 if (ret) 438 return ret; 439 } 440 441 if (!s->range_table && !s->range_table_list) 442 s->range_table = &range_unknown; 443 444 if (!s->insn_read && s->insn_bits) 445 s->insn_read = insn_rw_emulate_bits; 446 if (!s->insn_write && s->insn_bits) 447 s->insn_write = insn_rw_emulate_bits; 448 449 if (!s->insn_read) 450 s->insn_read = insn_inval; 451 if (!s->insn_write) 452 s->insn_write = insn_inval; 453 if (!s->insn_bits) 454 s->insn_bits = insn_inval; 455 if (!s->insn_config) 456 s->insn_config = insn_inval; 457 458 if (!s->poll) 459 s->poll = poll_invalid; 460 } 461 462 return 0; 463} 464 465/* do a little post-config cleanup */ 466static int comedi_device_postconfig(struct comedi_device *dev) 467{ 468 int ret; 469 470 ret = __comedi_device_postconfig(dev); 471 if (ret < 0) 472 return ret; 473 down_write(&dev->attach_lock); 474 dev->attached = true; 475 up_write(&dev->attach_lock); 476 return 0; 477} 478 479/* 480 * Generic recognize function for drivers that register their supported 481 * board names. 482 * 483 * 'driv->board_name' points to a 'const char *' member within the 484 * zeroth element of an array of some private board information 485 * structure, say 'struct foo_board' containing a member 'const char 486 * *board_name' that is initialized to point to a board name string that 487 * is one of the candidates matched against this function's 'name' 488 * parameter. 489 * 490 * 'driv->offset' is the size of the private board information 491 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is 492 * the length of the array of private board information structures. 493 * 494 * If one of the board names in the array of private board information 495 * structures matches the name supplied to this function, the function 496 * returns a pointer to the pointer to the board name, otherwise it 497 * returns NULL. The return value ends up in the 'board_ptr' member of 498 * a 'struct comedi_device' that the low-level comedi driver's 499 * 'attach()' hook can convert to a point to a particular element of its 500 * array of private board information structures by subtracting the 501 * offset of the member that points to the board name. (No subtraction 502 * is required if the board name pointer is the first member of the 503 * private board information structure, which is generally the case.) 504 */ 505static void *comedi_recognize(struct comedi_driver *driv, const char *name) 506{ 507 char **name_ptr = (char **)driv->board_name; 508 int i; 509 510 for (i = 0; i < driv->num_names; i++) { 511 if (strcmp(*name_ptr, name) == 0) 512 return name_ptr; 513 name_ptr = (void *)name_ptr + driv->offset; 514 } 515 516 return NULL; 517} 518 519static void comedi_report_boards(struct comedi_driver *driv) 520{ 521 unsigned int i; 522 const char *const *name_ptr; 523 524 pr_info("comedi: valid board names for %s driver are:\n", 525 driv->driver_name); 526 527 name_ptr = driv->board_name; 528 for (i = 0; i < driv->num_names; i++) { 529 pr_info(" %s\n", *name_ptr); 530 name_ptr = (const char **)((char *)name_ptr + driv->offset); 531 } 532 533 if (driv->num_names == 0) 534 pr_info(" %s\n", driv->driver_name); 535} 536 537/** 538 * comedi_load_firmware() - Request and load firmware for a device. 539 * @dev: comedi_device struct 540 * @hw_device: device struct for the comedi_device 541 * @name: the name of the firmware image 542 * @cb: callback to the upload the firmware image 543 * @context: private context from the driver 544 */ 545int comedi_load_firmware(struct comedi_device *dev, 546 struct device *device, 547 const char *name, 548 int (*cb)(struct comedi_device *dev, 549 const u8 *data, size_t size, 550 unsigned long context), 551 unsigned long context) 552{ 553 const struct firmware *fw; 554 int ret; 555 556 if (!cb) 557 return -EINVAL; 558 559 ret = request_firmware(&fw, name, device); 560 if (ret == 0) { 561 ret = cb(dev, fw->data, fw->size, context); 562 release_firmware(fw); 563 } 564 565 return ret < 0 ? ret : 0; 566} 567EXPORT_SYMBOL_GPL(comedi_load_firmware); 568 569/** 570 * __comedi_request_region() - Request an I/O reqion for a legacy driver. 571 * @dev: comedi_device struct 572 * @start: base address of the I/O reqion 573 * @len: length of the I/O region 574 */ 575int __comedi_request_region(struct comedi_device *dev, 576 unsigned long start, unsigned long len) 577{ 578 if (!start) { 579 dev_warn(dev->class_dev, 580 "%s: a I/O base address must be specified\n", 581 dev->board_name); 582 return -EINVAL; 583 } 584 585 if (!request_region(start, len, dev->board_name)) { 586 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n", 587 dev->board_name, start, len); 588 return -EIO; 589 } 590 591 return 0; 592} 593EXPORT_SYMBOL_GPL(__comedi_request_region); 594 595/** 596 * comedi_request_region() - Request an I/O reqion for a legacy driver. 597 * @dev: comedi_device struct 598 * @start: base address of the I/O reqion 599 * @len: length of the I/O region 600 */ 601int comedi_request_region(struct comedi_device *dev, 602 unsigned long start, unsigned long len) 603{ 604 int ret; 605 606 ret = __comedi_request_region(dev, start, len); 607 if (ret == 0) { 608 dev->iobase = start; 609 dev->iolen = len; 610 } 611 612 return ret; 613} 614EXPORT_SYMBOL_GPL(comedi_request_region); 615 616/** 617 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers. 618 * @dev: comedi_device struct 619 */ 620void comedi_legacy_detach(struct comedi_device *dev) 621{ 622 if (dev->irq) { 623 free_irq(dev->irq, dev); 624 dev->irq = 0; 625 } 626 if (dev->iobase && dev->iolen) { 627 release_region(dev->iobase, dev->iolen); 628 dev->iobase = 0; 629 dev->iolen = 0; 630 } 631} 632EXPORT_SYMBOL_GPL(comedi_legacy_detach); 633 634int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it) 635{ 636 struct comedi_driver *driv; 637 int ret; 638 639 if (dev->attached) 640 return -EBUSY; 641 642 mutex_lock(&comedi_drivers_list_lock); 643 for (driv = comedi_drivers; driv; driv = driv->next) { 644 if (!try_module_get(driv->module)) 645 continue; 646 if (driv->num_names) { 647 dev->board_ptr = comedi_recognize(driv, it->board_name); 648 if (dev->board_ptr) 649 break; 650 } else if (strcmp(driv->driver_name, it->board_name) == 0) { 651 break; 652 } 653 module_put(driv->module); 654 } 655 if (driv == NULL) { 656 /* recognize has failed if we get here */ 657 /* report valid board names before returning error */ 658 for (driv = comedi_drivers; driv; driv = driv->next) { 659 if (!try_module_get(driv->module)) 660 continue; 661 comedi_report_boards(driv); 662 module_put(driv->module); 663 } 664 ret = -EIO; 665 goto out; 666 } 667 if (driv->attach == NULL) { 668 /* driver does not support manual configuration */ 669 dev_warn(dev->class_dev, 670 "driver '%s' does not support attach using comedi_config\n", 671 driv->driver_name); 672 module_put(driv->module); 673 ret = -ENOSYS; 674 goto out; 675 } 676 dev->driver = driv; 677 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr 678 : dev->driver->driver_name; 679 ret = driv->attach(dev, it); 680 if (ret >= 0) 681 ret = comedi_device_postconfig(dev); 682 if (ret < 0) { 683 comedi_device_detach(dev); 684 module_put(driv->module); 685 } 686 /* On success, the driver module count has been incremented. */ 687out: 688 mutex_unlock(&comedi_drivers_list_lock); 689 return ret; 690} 691 692int comedi_auto_config(struct device *hardware_device, 693 struct comedi_driver *driver, unsigned long context) 694{ 695 struct comedi_device *dev; 696 int ret; 697 698 if (!hardware_device) { 699 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n"); 700 return -EINVAL; 701 } 702 if (!driver) { 703 dev_warn(hardware_device, 704 "BUG! comedi_auto_config called with NULL comedi driver\n"); 705 return -EINVAL; 706 } 707 708 if (!driver->auto_attach) { 709 dev_warn(hardware_device, 710 "BUG! comedi driver '%s' has no auto_attach handler\n", 711 driver->driver_name); 712 return -EINVAL; 713 } 714 715 dev = comedi_alloc_board_minor(hardware_device); 716 if (IS_ERR(dev)) { 717 dev_warn(hardware_device, 718 "driver '%s' could not create device.\n", 719 driver->driver_name); 720 return PTR_ERR(dev); 721 } 722 /* Note: comedi_alloc_board_minor() locked dev->mutex. */ 723 724 dev->driver = driver; 725 dev->board_name = dev->driver->driver_name; 726 ret = driver->auto_attach(dev, context); 727 if (ret >= 0) 728 ret = comedi_device_postconfig(dev); 729 mutex_unlock(&dev->mutex); 730 731 if (ret < 0) { 732 dev_warn(hardware_device, 733 "driver '%s' failed to auto-configure device.\n", 734 driver->driver_name); 735 comedi_release_hardware_device(hardware_device); 736 } else { 737 /* 738 * class_dev should be set properly here 739 * after a successful auto config 740 */ 741 dev_info(dev->class_dev, 742 "driver '%s' has successfully auto-configured '%s'.\n", 743 driver->driver_name, dev->board_name); 744 } 745 return ret; 746} 747EXPORT_SYMBOL_GPL(comedi_auto_config); 748 749void comedi_auto_unconfig(struct device *hardware_device) 750{ 751 if (hardware_device == NULL) 752 return; 753 comedi_release_hardware_device(hardware_device); 754} 755EXPORT_SYMBOL_GPL(comedi_auto_unconfig); 756 757int comedi_driver_register(struct comedi_driver *driver) 758{ 759 mutex_lock(&comedi_drivers_list_lock); 760 driver->next = comedi_drivers; 761 comedi_drivers = driver; 762 mutex_unlock(&comedi_drivers_list_lock); 763 764 return 0; 765} 766EXPORT_SYMBOL_GPL(comedi_driver_register); 767 768void comedi_driver_unregister(struct comedi_driver *driver) 769{ 770 struct comedi_driver *prev; 771 int i; 772 773 /* unlink the driver */ 774 mutex_lock(&comedi_drivers_list_lock); 775 if (comedi_drivers == driver) { 776 comedi_drivers = driver->next; 777 } else { 778 for (prev = comedi_drivers; prev->next; prev = prev->next) { 779 if (prev->next == driver) { 780 prev->next = driver->next; 781 break; 782 } 783 } 784 } 785 mutex_unlock(&comedi_drivers_list_lock); 786 787 /* check for devices using this driver */ 788 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) { 789 struct comedi_device *dev = comedi_dev_get_from_minor(i); 790 791 if (!dev) 792 continue; 793 794 mutex_lock(&dev->mutex); 795 if (dev->attached && dev->driver == driver) { 796 if (dev->use_count) 797 dev_warn(dev->class_dev, 798 "BUG! detaching device with use_count=%d\n", 799 dev->use_count); 800 comedi_device_detach(dev); 801 } 802 mutex_unlock(&dev->mutex); 803 comedi_dev_put(dev); 804 } 805} 806EXPORT_SYMBOL_GPL(comedi_driver_unregister); 807