at v3.12 647 lines 16 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 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; 41DEFINE_MUTEX(comedi_drivers_list_lock); 42 43int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev) 44{ 45 if (hw_dev == dev->hw_dev) 46 return 0; 47 if (dev->hw_dev != NULL) 48 return -EEXIST; 49 dev->hw_dev = get_device(hw_dev); 50 return 0; 51} 52EXPORT_SYMBOL_GPL(comedi_set_hw_dev); 53 54static void comedi_clear_hw_dev(struct comedi_device *dev) 55{ 56 put_device(dev->hw_dev); 57 dev->hw_dev = NULL; 58} 59 60/** 61 * comedi_alloc_devpriv() - Allocate memory for the device private data. 62 * @dev: comedi_device struct 63 * @size: size of the memory to allocate 64 */ 65void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size) 66{ 67 dev->private = kzalloc(size, GFP_KERNEL); 68 return dev->private; 69} 70EXPORT_SYMBOL_GPL(comedi_alloc_devpriv); 71 72int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices) 73{ 74 struct comedi_subdevice *s; 75 int i; 76 77 if (num_subdevices < 1) 78 return -EINVAL; 79 80 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL); 81 if (!s) 82 return -ENOMEM; 83 dev->subdevices = s; 84 dev->n_subdevices = num_subdevices; 85 86 for (i = 0; i < num_subdevices; ++i) { 87 s = &dev->subdevices[i]; 88 s->device = dev; 89 s->index = i; 90 s->async_dma_dir = DMA_NONE; 91 spin_lock_init(&s->spin_lock); 92 s->minor = -1; 93 } 94 return 0; 95} 96EXPORT_SYMBOL_GPL(comedi_alloc_subdevices); 97 98static void cleanup_device(struct comedi_device *dev) 99{ 100 int i; 101 struct comedi_subdevice *s; 102 103 if (dev->subdevices) { 104 for (i = 0; i < dev->n_subdevices; i++) { 105 s = &dev->subdevices[i]; 106 if (s->runflags & SRF_FREE_SPRIV) 107 kfree(s->private); 108 comedi_free_subdevice_minor(s); 109 if (s->async) { 110 comedi_buf_alloc(dev, s, 0); 111 kfree(s->async); 112 } 113 } 114 kfree(dev->subdevices); 115 dev->subdevices = NULL; 116 dev->n_subdevices = 0; 117 } 118 kfree(dev->private); 119 dev->private = NULL; 120 dev->driver = NULL; 121 dev->board_name = NULL; 122 dev->board_ptr = NULL; 123 dev->iobase = 0; 124 dev->iolen = 0; 125 dev->ioenabled = false; 126 dev->irq = 0; 127 dev->read_subdev = NULL; 128 dev->write_subdev = NULL; 129 dev->open = NULL; 130 dev->close = NULL; 131 comedi_clear_hw_dev(dev); 132} 133 134void comedi_device_detach(struct comedi_device *dev) 135{ 136 dev->attached = false; 137 if (dev->driver) 138 dev->driver->detach(dev); 139 cleanup_device(dev); 140} 141 142static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s) 143{ 144 return -EINVAL; 145} 146 147int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s, 148 struct comedi_insn *insn, unsigned int *data) 149{ 150 return -EINVAL; 151} 152 153/** 154 * comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices. 155 * @dev: comedi_device struct 156 * @s: comedi_subdevice struct 157 * @insn: comedi_insn struct 158 * @data: parameters for the @insn 159 * @mask: io_bits mask for grouped channels 160 */ 161int comedi_dio_insn_config(struct comedi_device *dev, 162 struct comedi_subdevice *s, 163 struct comedi_insn *insn, 164 unsigned int *data, 165 unsigned int mask) 166{ 167 unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec); 168 169 if (!mask) 170 mask = chan_mask; 171 172 switch (data[0]) { 173 case INSN_CONFIG_DIO_INPUT: 174 s->io_bits &= ~mask; 175 break; 176 177 case INSN_CONFIG_DIO_OUTPUT: 178 s->io_bits |= mask; 179 break; 180 181 case INSN_CONFIG_DIO_QUERY: 182 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT; 183 return insn->n; 184 185 default: 186 return -EINVAL; 187 } 188 189 return 0; 190} 191EXPORT_SYMBOL_GPL(comedi_dio_insn_config); 192 193static int insn_rw_emulate_bits(struct comedi_device *dev, 194 struct comedi_subdevice *s, 195 struct comedi_insn *insn, unsigned int *data) 196{ 197 struct comedi_insn new_insn; 198 int ret; 199 static const unsigned channels_per_bitfield = 32; 200 201 unsigned chan = CR_CHAN(insn->chanspec); 202 const unsigned base_bitfield_channel = 203 (chan < channels_per_bitfield) ? 0 : chan; 204 unsigned int new_data[2]; 205 memset(new_data, 0, sizeof(new_data)); 206 memset(&new_insn, 0, sizeof(new_insn)); 207 new_insn.insn = INSN_BITS; 208 new_insn.chanspec = base_bitfield_channel; 209 new_insn.n = 2; 210 new_insn.subdev = insn->subdev; 211 212 if (insn->insn == INSN_WRITE) { 213 if (!(s->subdev_flags & SDF_WRITABLE)) 214 return -EINVAL; 215 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */ 216 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) 217 : 0; /* bits */ 218 } 219 220 ret = s->insn_bits(dev, s, &new_insn, new_data); 221 if (ret < 0) 222 return ret; 223 224 if (insn->insn == INSN_READ) 225 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1; 226 227 return 1; 228} 229 230static int __comedi_device_postconfig_async(struct comedi_device *dev, 231 struct comedi_subdevice *s) 232{ 233 struct comedi_async *async; 234 unsigned int buf_size; 235 int ret; 236 237 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) { 238 dev_warn(dev->class_dev, 239 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n"); 240 return -EINVAL; 241 } 242 if (!s->do_cmdtest) { 243 dev_warn(dev->class_dev, 244 "async subdevices must have a do_cmdtest() function\n"); 245 return -EINVAL; 246 } 247 248 async = kzalloc(sizeof(*async), GFP_KERNEL); 249 if (!async) 250 return -ENOMEM; 251 252 init_waitqueue_head(&async->wait_head); 253 async->subdevice = s; 254 s->async = async; 255 256 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024; 257 buf_size = comedi_default_buf_size_kb * 1024; 258 if (buf_size > async->max_bufsize) 259 buf_size = async->max_bufsize; 260 261 if (comedi_buf_alloc(dev, s, buf_size) < 0) { 262 dev_warn(dev->class_dev, "Buffer allocation failed\n"); 263 return -ENOMEM; 264 } 265 if (s->buf_change) { 266 ret = s->buf_change(dev, s, buf_size); 267 if (ret < 0) 268 return ret; 269 } 270 271 comedi_alloc_subdevice_minor(s); 272 273 return 0; 274} 275 276static int __comedi_device_postconfig(struct comedi_device *dev) 277{ 278 struct comedi_subdevice *s; 279 int ret; 280 int i; 281 282 for (i = 0; i < dev->n_subdevices; i++) { 283 s = &dev->subdevices[i]; 284 285 if (s->type == COMEDI_SUBD_UNUSED) 286 continue; 287 288 if (s->len_chanlist == 0) 289 s->len_chanlist = 1; 290 291 if (s->do_cmd) { 292 ret = __comedi_device_postconfig_async(dev, s); 293 if (ret) 294 return ret; 295 } 296 297 if (!s->range_table && !s->range_table_list) 298 s->range_table = &range_unknown; 299 300 if (!s->insn_read && s->insn_bits) 301 s->insn_read = insn_rw_emulate_bits; 302 if (!s->insn_write && s->insn_bits) 303 s->insn_write = insn_rw_emulate_bits; 304 305 if (!s->insn_read) 306 s->insn_read = insn_inval; 307 if (!s->insn_write) 308 s->insn_write = insn_inval; 309 if (!s->insn_bits) 310 s->insn_bits = insn_inval; 311 if (!s->insn_config) 312 s->insn_config = insn_inval; 313 314 if (!s->poll) 315 s->poll = poll_invalid; 316 } 317 318 return 0; 319} 320 321/* do a little post-config cleanup */ 322static int comedi_device_postconfig(struct comedi_device *dev) 323{ 324 int ret; 325 326 ret = __comedi_device_postconfig(dev); 327 if (ret < 0) 328 return ret; 329 smp_wmb(); 330 dev->attached = true; 331 return 0; 332} 333 334/* 335 * Generic recognize function for drivers that register their supported 336 * board names. 337 * 338 * 'driv->board_name' points to a 'const char *' member within the 339 * zeroth element of an array of some private board information 340 * structure, say 'struct foo_board' containing a member 'const char 341 * *board_name' that is initialized to point to a board name string that 342 * is one of the candidates matched against this function's 'name' 343 * parameter. 344 * 345 * 'driv->offset' is the size of the private board information 346 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is 347 * the length of the array of private board information structures. 348 * 349 * If one of the board names in the array of private board information 350 * structures matches the name supplied to this function, the function 351 * returns a pointer to the pointer to the board name, otherwise it 352 * returns NULL. The return value ends up in the 'board_ptr' member of 353 * a 'struct comedi_device' that the low-level comedi driver's 354 * 'attach()' hook can convert to a point to a particular element of its 355 * array of private board information structures by subtracting the 356 * offset of the member that points to the board name. (No subtraction 357 * is required if the board name pointer is the first member of the 358 * private board information structure, which is generally the case.) 359 */ 360static void *comedi_recognize(struct comedi_driver *driv, const char *name) 361{ 362 char **name_ptr = (char **)driv->board_name; 363 int i; 364 365 for (i = 0; i < driv->num_names; i++) { 366 if (strcmp(*name_ptr, name) == 0) 367 return name_ptr; 368 name_ptr = (void *)name_ptr + driv->offset; 369 } 370 371 return NULL; 372} 373 374static void comedi_report_boards(struct comedi_driver *driv) 375{ 376 unsigned int i; 377 const char *const *name_ptr; 378 379 pr_info("comedi: valid board names for %s driver are:\n", 380 driv->driver_name); 381 382 name_ptr = driv->board_name; 383 for (i = 0; i < driv->num_names; i++) { 384 pr_info(" %s\n", *name_ptr); 385 name_ptr = (const char **)((char *)name_ptr + driv->offset); 386 } 387 388 if (driv->num_names == 0) 389 pr_info(" %s\n", driv->driver_name); 390} 391 392/** 393 * comedi_load_firmware() - Request and load firmware for a device. 394 * @dev: comedi_device struct 395 * @hw_device: device struct for the comedi_device 396 * @name: the name of the firmware image 397 * @cb: callback to the upload the firmware image 398 * @context: private context from the driver 399 */ 400int comedi_load_firmware(struct comedi_device *dev, 401 struct device *device, 402 const char *name, 403 int (*cb)(struct comedi_device *dev, 404 const u8 *data, size_t size, 405 unsigned long context), 406 unsigned long context) 407{ 408 const struct firmware *fw; 409 int ret; 410 411 if (!cb) 412 return -EINVAL; 413 414 ret = request_firmware(&fw, name, device); 415 if (ret == 0) { 416 ret = cb(dev, fw->data, fw->size, context); 417 release_firmware(fw); 418 } 419 420 return ret; 421} 422EXPORT_SYMBOL_GPL(comedi_load_firmware); 423 424/** 425 * __comedi_request_region() - Request an I/O reqion for a legacy driver. 426 * @dev: comedi_device struct 427 * @start: base address of the I/O reqion 428 * @len: length of the I/O region 429 */ 430int __comedi_request_region(struct comedi_device *dev, 431 unsigned long start, unsigned long len) 432{ 433 if (!start) { 434 dev_warn(dev->class_dev, 435 "%s: a I/O base address must be specified\n", 436 dev->board_name); 437 return -EINVAL; 438 } 439 440 if (!request_region(start, len, dev->board_name)) { 441 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n", 442 dev->board_name, start, len); 443 return -EIO; 444 } 445 446 return 0; 447} 448EXPORT_SYMBOL_GPL(__comedi_request_region); 449 450/** 451 * comedi_request_region() - Request an I/O reqion for a legacy driver. 452 * @dev: comedi_device struct 453 * @start: base address of the I/O reqion 454 * @len: length of the I/O region 455 */ 456int comedi_request_region(struct comedi_device *dev, 457 unsigned long start, unsigned long len) 458{ 459 int ret; 460 461 ret = __comedi_request_region(dev, start, len); 462 if (ret == 0) { 463 dev->iobase = start; 464 dev->iolen = len; 465 } 466 467 return ret; 468} 469EXPORT_SYMBOL_GPL(comedi_request_region); 470 471/** 472 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers. 473 * @dev: comedi_device struct 474 */ 475void comedi_legacy_detach(struct comedi_device *dev) 476{ 477 if (dev->irq) { 478 free_irq(dev->irq, dev); 479 dev->irq = 0; 480 } 481 if (dev->iobase && dev->iolen) { 482 release_region(dev->iobase, dev->iolen); 483 dev->iobase = 0; 484 dev->iolen = 0; 485 } 486} 487EXPORT_SYMBOL_GPL(comedi_legacy_detach); 488 489int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it) 490{ 491 struct comedi_driver *driv; 492 int ret; 493 494 if (dev->attached) 495 return -EBUSY; 496 497 mutex_lock(&comedi_drivers_list_lock); 498 for (driv = comedi_drivers; driv; driv = driv->next) { 499 if (!try_module_get(driv->module)) 500 continue; 501 if (driv->num_names) { 502 dev->board_ptr = comedi_recognize(driv, it->board_name); 503 if (dev->board_ptr) 504 break; 505 } else if (strcmp(driv->driver_name, it->board_name) == 0) 506 break; 507 module_put(driv->module); 508 } 509 if (driv == NULL) { 510 /* recognize has failed if we get here */ 511 /* report valid board names before returning error */ 512 for (driv = comedi_drivers; driv; driv = driv->next) { 513 if (!try_module_get(driv->module)) 514 continue; 515 comedi_report_boards(driv); 516 module_put(driv->module); 517 } 518 ret = -EIO; 519 goto out; 520 } 521 if (driv->attach == NULL) { 522 /* driver does not support manual configuration */ 523 dev_warn(dev->class_dev, 524 "driver '%s' does not support attach using comedi_config\n", 525 driv->driver_name); 526 module_put(driv->module); 527 ret = -ENOSYS; 528 goto out; 529 } 530 /* initialize dev->driver here so 531 * comedi_error() can be called from attach */ 532 dev->driver = driv; 533 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr 534 : dev->driver->driver_name; 535 ret = driv->attach(dev, it); 536 if (ret >= 0) 537 ret = comedi_device_postconfig(dev); 538 if (ret < 0) { 539 comedi_device_detach(dev); 540 module_put(driv->module); 541 } 542 /* On success, the driver module count has been incremented. */ 543out: 544 mutex_unlock(&comedi_drivers_list_lock); 545 return ret; 546} 547 548int comedi_auto_config(struct device *hardware_device, 549 struct comedi_driver *driver, unsigned long context) 550{ 551 struct comedi_device *dev; 552 int ret; 553 554 if (!hardware_device) { 555 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n"); 556 return -EINVAL; 557 } 558 if (!driver) { 559 dev_warn(hardware_device, 560 "BUG! comedi_auto_config called with NULL comedi driver\n"); 561 return -EINVAL; 562 } 563 564 if (!driver->auto_attach) { 565 dev_warn(hardware_device, 566 "BUG! comedi driver '%s' has no auto_attach handler\n", 567 driver->driver_name); 568 return -EINVAL; 569 } 570 571 dev = comedi_alloc_board_minor(hardware_device); 572 if (IS_ERR(dev)) 573 return PTR_ERR(dev); 574 /* Note: comedi_alloc_board_minor() locked dev->mutex. */ 575 576 dev->driver = driver; 577 dev->board_name = dev->driver->driver_name; 578 ret = driver->auto_attach(dev, context); 579 if (ret >= 0) 580 ret = comedi_device_postconfig(dev); 581 if (ret < 0) 582 comedi_device_detach(dev); 583 mutex_unlock(&dev->mutex); 584 585 if (ret < 0) 586 comedi_release_hardware_device(hardware_device); 587 return ret; 588} 589EXPORT_SYMBOL_GPL(comedi_auto_config); 590 591void comedi_auto_unconfig(struct device *hardware_device) 592{ 593 if (hardware_device == NULL) 594 return; 595 comedi_release_hardware_device(hardware_device); 596} 597EXPORT_SYMBOL_GPL(comedi_auto_unconfig); 598 599int comedi_driver_register(struct comedi_driver *driver) 600{ 601 mutex_lock(&comedi_drivers_list_lock); 602 driver->next = comedi_drivers; 603 comedi_drivers = driver; 604 mutex_unlock(&comedi_drivers_list_lock); 605 606 return 0; 607} 608EXPORT_SYMBOL_GPL(comedi_driver_register); 609 610void comedi_driver_unregister(struct comedi_driver *driver) 611{ 612 struct comedi_driver *prev; 613 int i; 614 615 /* unlink the driver */ 616 mutex_lock(&comedi_drivers_list_lock); 617 if (comedi_drivers == driver) { 618 comedi_drivers = driver->next; 619 } else { 620 for (prev = comedi_drivers; prev->next; prev = prev->next) { 621 if (prev->next == driver) { 622 prev->next = driver->next; 623 break; 624 } 625 } 626 } 627 mutex_unlock(&comedi_drivers_list_lock); 628 629 /* check for devices using this driver */ 630 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) { 631 struct comedi_device *dev = comedi_dev_from_minor(i); 632 633 if (!dev) 634 continue; 635 636 mutex_lock(&dev->mutex); 637 if (dev->attached && dev->driver == driver) { 638 if (dev->use_count) 639 dev_warn(dev->class_dev, 640 "BUG! detaching device with use_count=%d\n", 641 dev->use_count); 642 comedi_device_detach(dev); 643 } 644 mutex_unlock(&dev->mutex); 645 } 646} 647EXPORT_SYMBOL_GPL(comedi_driver_unregister);