1 /* 2 * Copyright (c) 1997,1998 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/kern/subr_bus.c,v 1.54.2.9 2002/10/10 15:13:32 jhb Exp $ 27 * $DragonFly: src/sys/kern/subr_bus.c,v 1.41 2008/01/05 13:30:33 corecode Exp $ 28 */ 29 30 #include "opt_bus.h" 31 32 #include <sys/param.h> 33 #include <sys/queue.h> 34 #include <sys/malloc.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #ifdef DEVICE_SYSCTLS 38 #include <sys/sysctl.h> 39 #endif 40 #include <sys/kobj.h> 41 #include <sys/bus_private.h> 42 #include <sys/systm.h> 43 #include <sys/bus.h> 44 #include <sys/rman.h> 45 46 #include <machine/stdarg.h> /* for device_printf() */ 47 48 #include <sys/thread2.h> 49 50 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 51 52 #ifdef BUS_DEBUG 53 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n")) 54 #define DEVICENAME(d) ((d)? device_get_name(d): "no device") 55 #define DRIVERNAME(d) ((d)? d->name : "no driver") 56 #define DEVCLANAME(d) ((d)? d->name : "no devclass") 57 58 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to 59 * prevent syslog from deleting initial spaces 60 */ 61 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0) 62 63 static void print_device_short(device_t dev, int indent); 64 static void print_device(device_t dev, int indent); 65 void print_device_tree_short(device_t dev, int indent); 66 void print_device_tree(device_t dev, int indent); 67 static void print_driver_short(driver_t *driver, int indent); 68 static void print_driver(driver_t *driver, int indent); 69 static void print_driver_list(driver_list_t drivers, int indent); 70 static void print_devclass_short(devclass_t dc, int indent); 71 static void print_devclass(devclass_t dc, int indent); 72 void print_devclass_list_short(void); 73 void print_devclass_list(void); 74 75 #else 76 /* Make the compiler ignore the function calls */ 77 #define PDEBUG(a) /* nop */ 78 #define DEVICENAME(d) /* nop */ 79 #define DRIVERNAME(d) /* nop */ 80 #define DEVCLANAME(d) /* nop */ 81 82 #define print_device_short(d,i) /* nop */ 83 #define print_device(d,i) /* nop */ 84 #define print_device_tree_short(d,i) /* nop */ 85 #define print_device_tree(d,i) /* nop */ 86 #define print_driver_short(d,i) /* nop */ 87 #define print_driver(d,i) /* nop */ 88 #define print_driver_list(d,i) /* nop */ 89 #define print_devclass_short(d,i) /* nop */ 90 #define print_devclass(d,i) /* nop */ 91 #define print_devclass_list_short() /* nop */ 92 #define print_devclass_list() /* nop */ 93 #endif 94 95 #ifdef DEVICE_SYSCTLS 96 static void device_register_oids(device_t dev); 97 static void device_unregister_oids(device_t dev); 98 #endif 99 static void device_attach_async(device_t dev); 100 static void device_attach_thread(void *arg); 101 static int device_doattach(device_t dev); 102 103 static int do_async_attach = 0; 104 static int numasyncthreads; 105 TUNABLE_INT("kern.do_async_attach", &do_async_attach); 106 107 kobj_method_t null_methods[] = { 108 { 0, 0 } 109 }; 110 111 DEFINE_CLASS(null, null_methods, 0); 112 113 /* 114 * Devclass implementation 115 */ 116 117 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 118 119 static devclass_t 120 devclass_find_internal(const char *classname, const char *parentname, 121 int create) 122 { 123 devclass_t dc; 124 125 PDEBUG(("looking for %s", classname)); 126 if (classname == NULL) 127 return(NULL); 128 129 TAILQ_FOREACH(dc, &devclasses, link) 130 if (!strcmp(dc->name, classname)) 131 break; 132 133 if (create && !dc) { 134 PDEBUG(("creating %s", classname)); 135 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1, 136 M_BUS, M_INTWAIT | M_ZERO); 137 if (!dc) 138 return(NULL); 139 dc->parent = NULL; 140 dc->name = (char*) (dc + 1); 141 strcpy(dc->name, classname); 142 dc->devices = NULL; 143 dc->maxunit = 0; 144 TAILQ_INIT(&dc->drivers); 145 TAILQ_INSERT_TAIL(&devclasses, dc, link); 146 } 147 if (parentname && dc && !dc->parent) 148 dc->parent = devclass_find_internal(parentname, NULL, FALSE); 149 150 return(dc); 151 } 152 153 devclass_t 154 devclass_create(const char *classname) 155 { 156 return(devclass_find_internal(classname, NULL, TRUE)); 157 } 158 159 devclass_t 160 devclass_find(const char *classname) 161 { 162 return(devclass_find_internal(classname, NULL, FALSE)); 163 } 164 165 device_t 166 devclass_find_unit(const char *classname, int unit) 167 { 168 devclass_t dc; 169 170 if ((dc = devclass_find(classname)) != NULL) 171 return(devclass_get_device(dc, unit)); 172 return (NULL); 173 } 174 175 int 176 devclass_add_driver(devclass_t dc, driver_t *driver) 177 { 178 driverlink_t dl; 179 device_t dev; 180 int i; 181 182 PDEBUG(("%s", DRIVERNAME(driver))); 183 184 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO); 185 if (!dl) 186 return(ENOMEM); 187 188 /* 189 * Compile the driver's methods. Also increase the reference count 190 * so that the class doesn't get freed when the last instance 191 * goes. This means we can safely use static methods and avoids a 192 * double-free in devclass_delete_driver. 193 */ 194 kobj_class_instantiate(driver); 195 196 /* 197 * Make sure the devclass which the driver is implementing exists. 198 */ 199 devclass_find_internal(driver->name, NULL, TRUE); 200 201 dl->driver = driver; 202 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 203 204 /* 205 * Call BUS_DRIVER_ADDED for any existing busses in this class, 206 * but only if the bus has already been attached (otherwise we 207 * might probe too early). 208 * 209 * This is what will cause a newly loaded module to be associated 210 * with hardware. bus_generic_driver_added() is typically what ends 211 * up being called. 212 */ 213 for (i = 0; i < dc->maxunit; i++) { 214 if ((dev = dc->devices[i]) != NULL) { 215 if (dev->state >= DS_ATTACHED) 216 BUS_DRIVER_ADDED(dev, driver); 217 } 218 } 219 220 return(0); 221 } 222 223 int 224 devclass_delete_driver(devclass_t busclass, driver_t *driver) 225 { 226 devclass_t dc = devclass_find(driver->name); 227 driverlink_t dl; 228 device_t dev; 229 int i; 230 int error; 231 232 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 233 234 if (!dc) 235 return(0); 236 237 /* 238 * Find the link structure in the bus' list of drivers. 239 */ 240 TAILQ_FOREACH(dl, &busclass->drivers, link) 241 if (dl->driver == driver) 242 break; 243 244 if (!dl) { 245 PDEBUG(("%s not found in %s list", driver->name, busclass->name)); 246 return(ENOENT); 247 } 248 249 /* 250 * Disassociate from any devices. We iterate through all the 251 * devices in the devclass of the driver and detach any which are 252 * using the driver and which have a parent in the devclass which 253 * we are deleting from. 254 * 255 * Note that since a driver can be in multiple devclasses, we 256 * should not detach devices which are not children of devices in 257 * the affected devclass. 258 */ 259 for (i = 0; i < dc->maxunit; i++) 260 if (dc->devices[i]) { 261 dev = dc->devices[i]; 262 if (dev->driver == driver && dev->parent && 263 dev->parent->devclass == busclass) { 264 if ((error = device_detach(dev)) != 0) 265 return(error); 266 device_set_driver(dev, NULL); 267 } 268 } 269 270 TAILQ_REMOVE(&busclass->drivers, dl, link); 271 kfree(dl, M_BUS); 272 273 kobj_class_uninstantiate(driver); 274 275 return(0); 276 } 277 278 static driverlink_t 279 devclass_find_driver_internal(devclass_t dc, const char *classname) 280 { 281 driverlink_t dl; 282 283 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 284 285 TAILQ_FOREACH(dl, &dc->drivers, link) 286 if (!strcmp(dl->driver->name, classname)) 287 return(dl); 288 289 PDEBUG(("not found")); 290 return(NULL); 291 } 292 293 kobj_class_t 294 devclass_find_driver(devclass_t dc, const char *classname) 295 { 296 driverlink_t dl; 297 298 dl = devclass_find_driver_internal(dc, classname); 299 if (dl) 300 return(dl->driver); 301 else 302 return(NULL); 303 } 304 305 const char * 306 devclass_get_name(devclass_t dc) 307 { 308 return(dc->name); 309 } 310 311 device_t 312 devclass_get_device(devclass_t dc, int unit) 313 { 314 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 315 return(NULL); 316 return(dc->devices[unit]); 317 } 318 319 void * 320 devclass_get_softc(devclass_t dc, int unit) 321 { 322 device_t dev; 323 324 dev = devclass_get_device(dc, unit); 325 if (!dev) 326 return(NULL); 327 328 return(device_get_softc(dev)); 329 } 330 331 int 332 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 333 { 334 int i; 335 int count; 336 device_t *list; 337 338 count = 0; 339 for (i = 0; i < dc->maxunit; i++) 340 if (dc->devices[i]) 341 count++; 342 343 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO); 344 if (list == NULL) 345 return(ENOMEM); 346 347 count = 0; 348 for (i = 0; i < dc->maxunit; i++) 349 if (dc->devices[i]) { 350 list[count] = dc->devices[i]; 351 count++; 352 } 353 354 *devlistp = list; 355 *devcountp = count; 356 357 return(0); 358 } 359 360 int 361 devclass_get_maxunit(devclass_t dc) 362 { 363 return(dc->maxunit); 364 } 365 366 void 367 devclass_set_parent(devclass_t dc, devclass_t pdc) 368 { 369 dc->parent = pdc; 370 } 371 372 devclass_t 373 devclass_get_parent(devclass_t dc) 374 { 375 return(dc->parent); 376 } 377 378 static int 379 devclass_alloc_unit(devclass_t dc, int *unitp) 380 { 381 int unit = *unitp; 382 383 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 384 385 /* If we have been given a wired unit number, check for existing device */ 386 if (unit != -1) { 387 if (unit >= 0 && unit < dc->maxunit && 388 dc->devices[unit] != NULL) { 389 if (bootverbose) 390 kprintf("%s-: %s%d exists, using next available unit number\n", 391 dc->name, dc->name, unit); 392 /* find the next available slot */ 393 while (++unit < dc->maxunit && dc->devices[unit] != NULL) 394 ; 395 } 396 } else { 397 /* Unwired device, find the next available slot for it */ 398 unit = 0; 399 while (unit < dc->maxunit && dc->devices[unit] != NULL) 400 unit++; 401 } 402 403 /* 404 * We've selected a unit beyond the length of the table, so let's 405 * extend the table to make room for all units up to and including 406 * this one. 407 */ 408 if (unit >= dc->maxunit) { 409 device_t *newlist; 410 int newsize; 411 412 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 413 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS, 414 M_INTWAIT | M_ZERO); 415 if (newlist == NULL) 416 return(ENOMEM); 417 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 418 if (dc->devices) 419 kfree(dc->devices, M_BUS); 420 dc->devices = newlist; 421 dc->maxunit = newsize; 422 } 423 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 424 425 *unitp = unit; 426 return(0); 427 } 428 429 static int 430 devclass_add_device(devclass_t dc, device_t dev) 431 { 432 int buflen, error; 433 434 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 435 436 buflen = strlen(dc->name) + 5; 437 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO); 438 if (!dev->nameunit) 439 return(ENOMEM); 440 441 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { 442 kfree(dev->nameunit, M_BUS); 443 dev->nameunit = NULL; 444 return(error); 445 } 446 dc->devices[dev->unit] = dev; 447 dev->devclass = dc; 448 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 449 450 #ifdef DEVICE_SYSCTLS 451 device_register_oids(dev); 452 #endif 453 454 return(0); 455 } 456 457 static int 458 devclass_delete_device(devclass_t dc, device_t dev) 459 { 460 if (!dc || !dev) 461 return(0); 462 463 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 464 465 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 466 panic("devclass_delete_device: inconsistent device class"); 467 dc->devices[dev->unit] = NULL; 468 if (dev->flags & DF_WILDCARD) 469 dev->unit = -1; 470 dev->devclass = NULL; 471 kfree(dev->nameunit, M_BUS); 472 dev->nameunit = NULL; 473 474 #ifdef DEVICE_SYSCTLS 475 device_unregister_oids(dev); 476 #endif 477 478 return(0); 479 } 480 481 static device_t 482 make_device(device_t parent, const char *name, int unit) 483 { 484 device_t dev; 485 devclass_t dc; 486 487 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 488 489 if (name != NULL) { 490 dc = devclass_find_internal(name, NULL, TRUE); 491 if (!dc) { 492 kprintf("make_device: can't find device class %s\n", name); 493 return(NULL); 494 } 495 } else 496 dc = NULL; 497 498 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO); 499 if (!dev) 500 return(0); 501 502 dev->parent = parent; 503 TAILQ_INIT(&dev->children); 504 kobj_init((kobj_t) dev, &null_class); 505 dev->driver = NULL; 506 dev->devclass = NULL; 507 dev->unit = unit; 508 dev->nameunit = NULL; 509 dev->desc = NULL; 510 dev->busy = 0; 511 dev->devflags = 0; 512 dev->flags = DF_ENABLED; 513 dev->order = 0; 514 if (unit == -1) 515 dev->flags |= DF_WILDCARD; 516 if (name) { 517 dev->flags |= DF_FIXEDCLASS; 518 if (devclass_add_device(dc, dev) != 0) { 519 kobj_delete((kobj_t)dev, M_BUS); 520 return(NULL); 521 } 522 } 523 dev->ivars = NULL; 524 dev->softc = NULL; 525 526 dev->state = DS_NOTPRESENT; 527 528 return(dev); 529 } 530 531 static int 532 device_print_child(device_t dev, device_t child) 533 { 534 int retval = 0; 535 536 if (device_is_alive(child)) 537 retval += BUS_PRINT_CHILD(dev, child); 538 else 539 retval += device_printf(child, " not found\n"); 540 541 return(retval); 542 } 543 544 device_t 545 device_add_child(device_t dev, const char *name, int unit) 546 { 547 return device_add_child_ordered(dev, 0, name, unit); 548 } 549 550 device_t 551 device_add_child_ordered(device_t dev, int order, const char *name, int unit) 552 { 553 device_t child; 554 device_t place; 555 556 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev), 557 order, unit)); 558 559 child = make_device(dev, name, unit); 560 if (child == NULL) 561 return child; 562 child->order = order; 563 564 TAILQ_FOREACH(place, &dev->children, link) 565 if (place->order > order) 566 break; 567 568 if (place) { 569 /* 570 * The device 'place' is the first device whose order is 571 * greater than the new child. 572 */ 573 TAILQ_INSERT_BEFORE(place, child, link); 574 } else { 575 /* 576 * The new child's order is greater or equal to the order of 577 * any existing device. Add the child to the tail of the list. 578 */ 579 TAILQ_INSERT_TAIL(&dev->children, child, link); 580 } 581 582 return(child); 583 } 584 585 int 586 device_delete_child(device_t dev, device_t child) 587 { 588 int error; 589 device_t grandchild; 590 591 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 592 593 /* remove children first */ 594 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 595 error = device_delete_child(child, grandchild); 596 if (error) 597 return(error); 598 } 599 600 if ((error = device_detach(child)) != 0) 601 return(error); 602 if (child->devclass) 603 devclass_delete_device(child->devclass, child); 604 TAILQ_REMOVE(&dev->children, child, link); 605 device_set_desc(child, NULL); 606 kobj_delete((kobj_t)child, M_BUS); 607 608 return(0); 609 } 610 611 /* 612 * Find only devices attached to this bus. 613 */ 614 device_t 615 device_find_child(device_t dev, const char *classname, int unit) 616 { 617 devclass_t dc; 618 device_t child; 619 620 dc = devclass_find(classname); 621 if (!dc) 622 return(NULL); 623 624 child = devclass_get_device(dc, unit); 625 if (child && child->parent == dev) 626 return(child); 627 return(NULL); 628 } 629 630 static driverlink_t 631 first_matching_driver(devclass_t dc, device_t dev) 632 { 633 if (dev->devclass) 634 return(devclass_find_driver_internal(dc, dev->devclass->name)); 635 else 636 return(TAILQ_FIRST(&dc->drivers)); 637 } 638 639 static driverlink_t 640 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 641 { 642 if (dev->devclass) { 643 driverlink_t dl; 644 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 645 if (!strcmp(dev->devclass->name, dl->driver->name)) 646 return(dl); 647 return(NULL); 648 } else 649 return(TAILQ_NEXT(last, link)); 650 } 651 652 static int 653 device_probe_child(device_t dev, device_t child) 654 { 655 devclass_t dc; 656 driverlink_t best = 0; 657 driverlink_t dl; 658 int result, pri = 0; 659 int hasclass = (child->devclass != 0); 660 661 dc = dev->devclass; 662 if (!dc) 663 panic("device_probe_child: parent device has no devclass"); 664 665 if (child->state == DS_ALIVE) 666 return(0); 667 668 for (; dc; dc = dc->parent) { 669 for (dl = first_matching_driver(dc, child); dl; 670 dl = next_matching_driver(dc, child, dl)) { 671 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 672 device_set_driver(child, dl->driver); 673 if (!hasclass) 674 device_set_devclass(child, dl->driver->name); 675 result = DEVICE_PROBE(child); 676 if (!hasclass) 677 device_set_devclass(child, 0); 678 679 /* 680 * If the driver returns SUCCESS, there can be 681 * no higher match for this device. 682 */ 683 if (result == 0) { 684 best = dl; 685 pri = 0; 686 break; 687 } 688 689 /* 690 * The driver returned an error so it 691 * certainly doesn't match. 692 */ 693 if (result > 0) { 694 device_set_driver(child, 0); 695 continue; 696 } 697 698 /* 699 * A priority lower than SUCCESS, remember the 700 * best matching driver. Initialise the value 701 * of pri for the first match. 702 */ 703 if (best == 0 || result > pri) { 704 best = dl; 705 pri = result; 706 continue; 707 } 708 } 709 /* 710 * If we have unambiguous match in this devclass, 711 * don't look in the parent. 712 */ 713 if (best && pri == 0) 714 break; 715 } 716 717 /* 718 * If we found a driver, change state and initialise the devclass. 719 */ 720 if (best) { 721 if (!child->devclass) 722 device_set_devclass(child, best->driver->name); 723 device_set_driver(child, best->driver); 724 if (pri < 0) { 725 /* 726 * A bit bogus. Call the probe method again to make 727 * sure that we have the right description. 728 */ 729 DEVICE_PROBE(child); 730 } 731 child->state = DS_ALIVE; 732 return(0); 733 } 734 735 return(ENXIO); 736 } 737 738 device_t 739 device_get_parent(device_t dev) 740 { 741 return dev->parent; 742 } 743 744 int 745 device_get_children(device_t dev, device_t **devlistp, int *devcountp) 746 { 747 int count; 748 device_t child; 749 device_t *list; 750 751 count = 0; 752 TAILQ_FOREACH(child, &dev->children, link) 753 count++; 754 755 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO); 756 if (!list) 757 return(ENOMEM); 758 759 count = 0; 760 TAILQ_FOREACH(child, &dev->children, link) { 761 list[count] = child; 762 count++; 763 } 764 765 *devlistp = list; 766 *devcountp = count; 767 768 return(0); 769 } 770 771 driver_t * 772 device_get_driver(device_t dev) 773 { 774 return(dev->driver); 775 } 776 777 devclass_t 778 device_get_devclass(device_t dev) 779 { 780 return(dev->devclass); 781 } 782 783 const char * 784 device_get_name(device_t dev) 785 { 786 if (dev->devclass) 787 return devclass_get_name(dev->devclass); 788 return(NULL); 789 } 790 791 const char * 792 device_get_nameunit(device_t dev) 793 { 794 return(dev->nameunit); 795 } 796 797 int 798 device_get_unit(device_t dev) 799 { 800 return(dev->unit); 801 } 802 803 const char * 804 device_get_desc(device_t dev) 805 { 806 return(dev->desc); 807 } 808 809 uint32_t 810 device_get_flags(device_t dev) 811 { 812 return(dev->devflags); 813 } 814 815 int 816 device_print_prettyname(device_t dev) 817 { 818 const char *name = device_get_name(dev); 819 820 if (name == 0) 821 return kprintf("unknown: "); 822 else 823 return kprintf("%s%d: ", name, device_get_unit(dev)); 824 } 825 826 int 827 device_printf(device_t dev, const char * fmt, ...) 828 { 829 __va_list ap; 830 int retval; 831 832 retval = device_print_prettyname(dev); 833 __va_start(ap, fmt); 834 retval += kvprintf(fmt, ap); 835 __va_end(ap); 836 return retval; 837 } 838 839 static void 840 device_set_desc_internal(device_t dev, const char* desc, int copy) 841 { 842 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 843 kfree(dev->desc, M_BUS); 844 dev->flags &= ~DF_DESCMALLOCED; 845 dev->desc = NULL; 846 } 847 848 if (copy && desc) { 849 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT); 850 if (dev->desc) { 851 strcpy(dev->desc, desc); 852 dev->flags |= DF_DESCMALLOCED; 853 } 854 } else 855 /* Avoid a -Wcast-qual warning */ 856 dev->desc = (char *)(uintptr_t) desc; 857 858 #ifdef DEVICE_SYSCTLS 859 { 860 struct sysctl_oid *oid = &dev->oid[1]; 861 oid->oid_arg1 = dev->desc ? dev->desc : ""; 862 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0; 863 } 864 #endif 865 } 866 867 void 868 device_set_desc(device_t dev, const char* desc) 869 { 870 device_set_desc_internal(dev, desc, FALSE); 871 } 872 873 void 874 device_set_desc_copy(device_t dev, const char* desc) 875 { 876 device_set_desc_internal(dev, desc, TRUE); 877 } 878 879 void 880 device_set_flags(device_t dev, uint32_t flags) 881 { 882 dev->devflags = flags; 883 } 884 885 void * 886 device_get_softc(device_t dev) 887 { 888 return dev->softc; 889 } 890 891 void 892 device_set_softc(device_t dev, void *softc) 893 { 894 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 895 kfree(dev->softc, M_BUS); 896 dev->softc = softc; 897 if (dev->softc) 898 dev->flags |= DF_EXTERNALSOFTC; 899 else 900 dev->flags &= ~DF_EXTERNALSOFTC; 901 } 902 903 void 904 device_set_async_attach(device_t dev, int enable) 905 { 906 if (enable) 907 dev->flags |= DF_ASYNCPROBE; 908 else 909 dev->flags &= ~DF_ASYNCPROBE; 910 } 911 912 void * 913 device_get_ivars(device_t dev) 914 { 915 return dev->ivars; 916 } 917 918 void 919 device_set_ivars(device_t dev, void * ivars) 920 { 921 if (!dev) 922 return; 923 924 dev->ivars = ivars; 925 } 926 927 device_state_t 928 device_get_state(device_t dev) 929 { 930 return(dev->state); 931 } 932 933 void 934 device_enable(device_t dev) 935 { 936 dev->flags |= DF_ENABLED; 937 } 938 939 void 940 device_disable(device_t dev) 941 { 942 dev->flags &= ~DF_ENABLED; 943 } 944 945 /* 946 * YYY cannot block 947 */ 948 void 949 device_busy(device_t dev) 950 { 951 if (dev->state < DS_ATTACHED) 952 panic("device_busy: called for unattached device"); 953 if (dev->busy == 0 && dev->parent) 954 device_busy(dev->parent); 955 dev->busy++; 956 dev->state = DS_BUSY; 957 } 958 959 /* 960 * YYY cannot block 961 */ 962 void 963 device_unbusy(device_t dev) 964 { 965 if (dev->state != DS_BUSY) 966 panic("device_unbusy: called for non-busy device"); 967 dev->busy--; 968 if (dev->busy == 0) { 969 if (dev->parent) 970 device_unbusy(dev->parent); 971 dev->state = DS_ATTACHED; 972 } 973 } 974 975 void 976 device_quiet(device_t dev) 977 { 978 dev->flags |= DF_QUIET; 979 } 980 981 void 982 device_verbose(device_t dev) 983 { 984 dev->flags &= ~DF_QUIET; 985 } 986 987 int 988 device_is_quiet(device_t dev) 989 { 990 return((dev->flags & DF_QUIET) != 0); 991 } 992 993 int 994 device_is_enabled(device_t dev) 995 { 996 return((dev->flags & DF_ENABLED) != 0); 997 } 998 999 int 1000 device_is_alive(device_t dev) 1001 { 1002 return(dev->state >= DS_ALIVE); 1003 } 1004 1005 int 1006 device_is_attached(device_t dev) 1007 { 1008 return(dev->state >= DS_ATTACHED); 1009 } 1010 1011 int 1012 device_set_devclass(device_t dev, const char *classname) 1013 { 1014 devclass_t dc; 1015 1016 if (!classname) { 1017 if (dev->devclass) 1018 devclass_delete_device(dev->devclass, dev); 1019 return(0); 1020 } 1021 1022 if (dev->devclass) { 1023 kprintf("device_set_devclass: device class already set\n"); 1024 return(EINVAL); 1025 } 1026 1027 dc = devclass_find_internal(classname, NULL, TRUE); 1028 if (!dc) 1029 return(ENOMEM); 1030 1031 return(devclass_add_device(dc, dev)); 1032 } 1033 1034 int 1035 device_set_driver(device_t dev, driver_t *driver) 1036 { 1037 if (dev->state >= DS_ATTACHED) 1038 return(EBUSY); 1039 1040 if (dev->driver == driver) 1041 return(0); 1042 1043 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 1044 kfree(dev->softc, M_BUS); 1045 dev->softc = NULL; 1046 } 1047 kobj_delete((kobj_t) dev, 0); 1048 dev->driver = driver; 1049 if (driver) { 1050 kobj_init((kobj_t) dev, (kobj_class_t) driver); 1051 if (!(dev->flags & DF_EXTERNALSOFTC)) { 1052 dev->softc = kmalloc(driver->size, M_BUS, 1053 M_INTWAIT | M_ZERO); 1054 if (!dev->softc) { 1055 kobj_delete((kobj_t)dev, 0); 1056 kobj_init((kobj_t) dev, &null_class); 1057 dev->driver = NULL; 1058 return(ENOMEM); 1059 } 1060 } 1061 } else 1062 kobj_init((kobj_t) dev, &null_class); 1063 return(0); 1064 } 1065 1066 int 1067 device_probe_and_attach(device_t dev) 1068 { 1069 device_t bus = dev->parent; 1070 int error = 0; 1071 1072 if (dev->state >= DS_ALIVE) 1073 return(0); 1074 1075 if ((dev->flags & DF_ENABLED) == 0) { 1076 if (bootverbose) { 1077 device_print_prettyname(dev); 1078 kprintf("not probed (disabled)\n"); 1079 } 1080 return(0); 1081 } 1082 1083 error = device_probe_child(bus, dev); 1084 if (error) { 1085 if (!(dev->flags & DF_DONENOMATCH)) { 1086 BUS_PROBE_NOMATCH(bus, dev); 1087 dev->flags |= DF_DONENOMATCH; 1088 } 1089 return(error); 1090 } 1091 1092 /* 1093 * Output the exact device chain prior to the attach in case the 1094 * system locks up during attach, and generate the full info after 1095 * the attach so correct irq and other information is displayed. 1096 */ 1097 if (bootverbose && !device_is_quiet(dev)) { 1098 device_t tmp; 1099 1100 kprintf("%s", device_get_nameunit(dev)); 1101 for (tmp = dev->parent; tmp; tmp = tmp->parent) 1102 kprintf(".%s", device_get_nameunit(tmp)); 1103 kprintf("\n"); 1104 } 1105 if (!device_is_quiet(dev)) 1106 device_print_child(bus, dev); 1107 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) { 1108 kprintf("%s: probing asynchronously\n", 1109 device_get_nameunit(dev)); 1110 dev->state = DS_INPROGRESS; 1111 device_attach_async(dev); 1112 error = 0; 1113 } else { 1114 error = device_doattach(dev); 1115 } 1116 return(error); 1117 } 1118 1119 /* 1120 * Device is known to be alive, do the attach asynchronously. 1121 * 1122 * The MP lock is held by all threads. 1123 */ 1124 static void 1125 device_attach_async(device_t dev) 1126 { 1127 thread_t td; 1128 1129 atomic_add_int(&numasyncthreads, 1); 1130 lwkt_create(device_attach_thread, dev, &td, NULL, 1131 0, 0, (dev->desc ? dev->desc : "devattach")); 1132 } 1133 1134 static void 1135 device_attach_thread(void *arg) 1136 { 1137 device_t dev = arg; 1138 1139 (void)device_doattach(dev); 1140 atomic_subtract_int(&numasyncthreads, 1); 1141 wakeup(&numasyncthreads); 1142 } 1143 1144 /* 1145 * Device is known to be alive, do the attach (synchronous or asynchronous) 1146 */ 1147 static int 1148 device_doattach(device_t dev) 1149 { 1150 device_t bus = dev->parent; 1151 int hasclass = (dev->devclass != 0); 1152 int error; 1153 1154 error = DEVICE_ATTACH(dev); 1155 if (error == 0) { 1156 dev->state = DS_ATTACHED; 1157 if (bootverbose && !device_is_quiet(dev)) 1158 device_print_child(bus, dev); 1159 } else { 1160 kprintf("device_probe_and_attach: %s%d attach returned %d\n", 1161 dev->driver->name, dev->unit, error); 1162 /* Unset the class that was set in device_probe_child */ 1163 if (!hasclass) 1164 device_set_devclass(dev, 0); 1165 device_set_driver(dev, NULL); 1166 dev->state = DS_NOTPRESENT; 1167 } 1168 return(error); 1169 } 1170 1171 int 1172 device_detach(device_t dev) 1173 { 1174 int error; 1175 1176 PDEBUG(("%s", DEVICENAME(dev))); 1177 if (dev->state == DS_BUSY) 1178 return(EBUSY); 1179 if (dev->state != DS_ATTACHED) 1180 return(0); 1181 1182 if ((error = DEVICE_DETACH(dev)) != 0) 1183 return(error); 1184 device_printf(dev, "detached\n"); 1185 if (dev->parent) 1186 BUS_CHILD_DETACHED(dev->parent, dev); 1187 1188 if (!(dev->flags & DF_FIXEDCLASS)) 1189 devclass_delete_device(dev->devclass, dev); 1190 1191 dev->state = DS_NOTPRESENT; 1192 device_set_driver(dev, NULL); 1193 1194 return(0); 1195 } 1196 1197 int 1198 device_shutdown(device_t dev) 1199 { 1200 if (dev->state < DS_ATTACHED) 1201 return 0; 1202 PDEBUG(("%s", DEVICENAME(dev))); 1203 return DEVICE_SHUTDOWN(dev); 1204 } 1205 1206 int 1207 device_set_unit(device_t dev, int unit) 1208 { 1209 devclass_t dc; 1210 int err; 1211 1212 dc = device_get_devclass(dev); 1213 if (unit < dc->maxunit && dc->devices[unit]) 1214 return(EBUSY); 1215 err = devclass_delete_device(dc, dev); 1216 if (err) 1217 return(err); 1218 dev->unit = unit; 1219 err = devclass_add_device(dc, dev); 1220 return(err); 1221 } 1222 1223 #ifdef DEVICE_SYSCTLS 1224 1225 /* 1226 * Sysctl nodes for devices. 1227 */ 1228 1229 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices"); 1230 1231 static int 1232 sysctl_handle_children(SYSCTL_HANDLER_ARGS) 1233 { 1234 device_t dev = arg1; 1235 device_t child; 1236 int first = 1, error = 0; 1237 1238 TAILQ_FOREACH(child, &dev->children, link) 1239 if (child->nameunit) { 1240 if (!first) { 1241 error = SYSCTL_OUT(req, ",", 1); 1242 if (error) 1243 return error; 1244 } else 1245 first = 0; 1246 error = SYSCTL_OUT(req, child->nameunit, 1247 strlen(child->nameunit)); 1248 if (error) 1249 return(error); 1250 } 1251 1252 error = SYSCTL_OUT(req, "", 1); 1253 1254 return(error); 1255 } 1256 1257 static int 1258 sysctl_handle_state(SYSCTL_HANDLER_ARGS) 1259 { 1260 device_t dev = arg1; 1261 1262 switch (dev->state) { 1263 case DS_NOTPRESENT: 1264 return SYSCTL_OUT(req, "notpresent", sizeof("notpresent")); 1265 case DS_ALIVE: 1266 return SYSCTL_OUT(req, "alive", sizeof("alive")); 1267 case DS_INPROGRESS: 1268 return SYSCTL_OUT(req, "in-progress", sizeof("in-progress")); 1269 case DS_ATTACHED: 1270 return SYSCTL_OUT(req, "attached", sizeof("attached")); 1271 case DS_BUSY: 1272 return SYSCTL_OUT(req, "busy", sizeof("busy")); 1273 default: 1274 return (0); 1275 } 1276 } 1277 1278 static void 1279 device_register_oids(device_t dev) 1280 { 1281 struct sysctl_oid* oid; 1282 1283 oid = &dev->oid[0]; 1284 bzero(oid, sizeof(*oid)); 1285 oid->oid_parent = &sysctl__hw_devices_children; 1286 oid->oid_number = OID_AUTO; 1287 oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW; 1288 oid->oid_arg1 = &dev->oidlist[0]; 1289 oid->oid_arg2 = 0; 1290 oid->oid_name = dev->nameunit; 1291 oid->oid_handler = 0; 1292 oid->oid_fmt = "N"; 1293 SLIST_INIT(&dev->oidlist[0]); 1294 sysctl_register_oid(oid); 1295 1296 oid = &dev->oid[1]; 1297 bzero(oid, sizeof(*oid)); 1298 oid->oid_parent = &dev->oidlist[0]; 1299 oid->oid_number = OID_AUTO; 1300 oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD; 1301 oid->oid_arg1 = dev->desc ? dev->desc : ""; 1302 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0; 1303 oid->oid_name = "desc"; 1304 oid->oid_handler = sysctl_handle_string; 1305 oid->oid_fmt = "A"; 1306 sysctl_register_oid(oid); 1307 1308 oid = &dev->oid[2]; 1309 bzero(oid, sizeof(*oid)); 1310 oid->oid_parent = &dev->oidlist[0]; 1311 oid->oid_number = OID_AUTO; 1312 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD; 1313 oid->oid_arg1 = dev; 1314 oid->oid_arg2 = 0; 1315 oid->oid_name = "children"; 1316 oid->oid_handler = sysctl_handle_children; 1317 oid->oid_fmt = "A"; 1318 sysctl_register_oid(oid); 1319 1320 oid = &dev->oid[3]; 1321 bzero(oid, sizeof(*oid)); 1322 oid->oid_parent = &dev->oidlist[0]; 1323 oid->oid_number = OID_AUTO; 1324 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD; 1325 oid->oid_arg1 = dev; 1326 oid->oid_arg2 = 0; 1327 oid->oid_name = "state"; 1328 oid->oid_handler = sysctl_handle_state; 1329 oid->oid_fmt = "A"; 1330 sysctl_register_oid(oid); 1331 } 1332 1333 static void 1334 device_unregister_oids(device_t dev) 1335 { 1336 sysctl_unregister_oid(&dev->oid[0]); 1337 sysctl_unregister_oid(&dev->oid[1]); 1338 sysctl_unregister_oid(&dev->oid[2]); 1339 } 1340 1341 #endif 1342 1343 /*======================================*/ 1344 /* 1345 * Access functions for device resources. 1346 */ 1347 1348 /* Supplied by config(8) in ioconf.c */ 1349 extern struct config_device config_devtab[]; 1350 extern int devtab_count; 1351 1352 /* Runtime version */ 1353 struct config_device *devtab = config_devtab; 1354 1355 static int 1356 resource_new_name(const char *name, int unit) 1357 { 1358 struct config_device *new; 1359 1360 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP, 1361 M_INTWAIT | M_ZERO); 1362 if (new == NULL) 1363 return(-1); 1364 if (devtab && devtab_count > 0) 1365 bcopy(devtab, new, devtab_count * sizeof(*new)); 1366 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT); 1367 if (new[devtab_count].name == NULL) { 1368 kfree(new, M_TEMP); 1369 return(-1); 1370 } 1371 strcpy(new[devtab_count].name, name); 1372 new[devtab_count].unit = unit; 1373 new[devtab_count].resource_count = 0; 1374 new[devtab_count].resources = NULL; 1375 if (devtab && devtab != config_devtab) 1376 kfree(devtab, M_TEMP); 1377 devtab = new; 1378 return devtab_count++; 1379 } 1380 1381 static int 1382 resource_new_resname(int j, const char *resname, resource_type type) 1383 { 1384 struct config_resource *new; 1385 int i; 1386 1387 i = devtab[j].resource_count; 1388 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO); 1389 if (new == NULL) 1390 return(-1); 1391 if (devtab[j].resources && i > 0) 1392 bcopy(devtab[j].resources, new, i * sizeof(*new)); 1393 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT); 1394 if (new[i].name == NULL) { 1395 kfree(new, M_TEMP); 1396 return(-1); 1397 } 1398 strcpy(new[i].name, resname); 1399 new[i].type = type; 1400 if (devtab[j].resources) 1401 kfree(devtab[j].resources, M_TEMP); 1402 devtab[j].resources = new; 1403 devtab[j].resource_count = i + 1; 1404 return(i); 1405 } 1406 1407 static int 1408 resource_match_string(int i, const char *resname, const char *value) 1409 { 1410 int j; 1411 struct config_resource *res; 1412 1413 for (j = 0, res = devtab[i].resources; 1414 j < devtab[i].resource_count; j++, res++) 1415 if (!strcmp(res->name, resname) 1416 && res->type == RES_STRING 1417 && !strcmp(res->u.stringval, value)) 1418 return(j); 1419 return(-1); 1420 } 1421 1422 static int 1423 resource_find(const char *name, int unit, const char *resname, 1424 struct config_resource **result) 1425 { 1426 int i, j; 1427 struct config_resource *res; 1428 1429 /* 1430 * First check specific instances, then generic. 1431 */ 1432 for (i = 0; i < devtab_count; i++) { 1433 if (devtab[i].unit < 0) 1434 continue; 1435 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1436 res = devtab[i].resources; 1437 for (j = 0; j < devtab[i].resource_count; j++, res++) 1438 if (!strcmp(res->name, resname)) { 1439 *result = res; 1440 return(0); 1441 } 1442 } 1443 } 1444 for (i = 0; i < devtab_count; i++) { 1445 if (devtab[i].unit >= 0) 1446 continue; 1447 /* XXX should this `&& devtab[i].unit == unit' be here? */ 1448 /* XXX if so, then the generic match does nothing */ 1449 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1450 res = devtab[i].resources; 1451 for (j = 0; j < devtab[i].resource_count; j++, res++) 1452 if (!strcmp(res->name, resname)) { 1453 *result = res; 1454 return(0); 1455 } 1456 } 1457 } 1458 return(ENOENT); 1459 } 1460 1461 int 1462 resource_int_value(const char *name, int unit, const char *resname, int *result) 1463 { 1464 int error; 1465 struct config_resource *res; 1466 1467 if ((error = resource_find(name, unit, resname, &res)) != 0) 1468 return(error); 1469 if (res->type != RES_INT) 1470 return(EFTYPE); 1471 *result = res->u.intval; 1472 return(0); 1473 } 1474 1475 int 1476 resource_long_value(const char *name, int unit, const char *resname, 1477 long *result) 1478 { 1479 int error; 1480 struct config_resource *res; 1481 1482 if ((error = resource_find(name, unit, resname, &res)) != 0) 1483 return(error); 1484 if (res->type != RES_LONG) 1485 return(EFTYPE); 1486 *result = res->u.longval; 1487 return(0); 1488 } 1489 1490 int 1491 resource_string_value(const char *name, int unit, const char *resname, 1492 char **result) 1493 { 1494 int error; 1495 struct config_resource *res; 1496 1497 if ((error = resource_find(name, unit, resname, &res)) != 0) 1498 return(error); 1499 if (res->type != RES_STRING) 1500 return(EFTYPE); 1501 *result = res->u.stringval; 1502 return(0); 1503 } 1504 1505 int 1506 resource_query_string(int i, const char *resname, const char *value) 1507 { 1508 if (i < 0) 1509 i = 0; 1510 else 1511 i = i + 1; 1512 for (; i < devtab_count; i++) 1513 if (resource_match_string(i, resname, value) >= 0) 1514 return(i); 1515 return(-1); 1516 } 1517 1518 int 1519 resource_locate(int i, const char *resname) 1520 { 1521 if (i < 0) 1522 i = 0; 1523 else 1524 i = i + 1; 1525 for (; i < devtab_count; i++) 1526 if (!strcmp(devtab[i].name, resname)) 1527 return(i); 1528 return(-1); 1529 } 1530 1531 int 1532 resource_count(void) 1533 { 1534 return(devtab_count); 1535 } 1536 1537 char * 1538 resource_query_name(int i) 1539 { 1540 return(devtab[i].name); 1541 } 1542 1543 int 1544 resource_query_unit(int i) 1545 { 1546 return(devtab[i].unit); 1547 } 1548 1549 static int 1550 resource_create(const char *name, int unit, const char *resname, 1551 resource_type type, struct config_resource **result) 1552 { 1553 int i, j; 1554 struct config_resource *res = NULL; 1555 1556 for (i = 0; i < devtab_count; i++) 1557 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1558 res = devtab[i].resources; 1559 break; 1560 } 1561 if (res == NULL) { 1562 i = resource_new_name(name, unit); 1563 if (i < 0) 1564 return(ENOMEM); 1565 res = devtab[i].resources; 1566 } 1567 for (j = 0; j < devtab[i].resource_count; j++, res++) 1568 if (!strcmp(res->name, resname)) { 1569 *result = res; 1570 return(0); 1571 } 1572 j = resource_new_resname(i, resname, type); 1573 if (j < 0) 1574 return(ENOMEM); 1575 res = &devtab[i].resources[j]; 1576 *result = res; 1577 return(0); 1578 } 1579 1580 int 1581 resource_set_int(const char *name, int unit, const char *resname, int value) 1582 { 1583 int error; 1584 struct config_resource *res; 1585 1586 error = resource_create(name, unit, resname, RES_INT, &res); 1587 if (error) 1588 return(error); 1589 if (res->type != RES_INT) 1590 return(EFTYPE); 1591 res->u.intval = value; 1592 return(0); 1593 } 1594 1595 int 1596 resource_set_long(const char *name, int unit, const char *resname, long value) 1597 { 1598 int error; 1599 struct config_resource *res; 1600 1601 error = resource_create(name, unit, resname, RES_LONG, &res); 1602 if (error) 1603 return(error); 1604 if (res->type != RES_LONG) 1605 return(EFTYPE); 1606 res->u.longval = value; 1607 return(0); 1608 } 1609 1610 int 1611 resource_set_string(const char *name, int unit, const char *resname, 1612 const char *value) 1613 { 1614 int error; 1615 struct config_resource *res; 1616 1617 error = resource_create(name, unit, resname, RES_STRING, &res); 1618 if (error) 1619 return(error); 1620 if (res->type != RES_STRING) 1621 return(EFTYPE); 1622 if (res->u.stringval) 1623 kfree(res->u.stringval, M_TEMP); 1624 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT); 1625 if (res->u.stringval == NULL) 1626 return(ENOMEM); 1627 strcpy(res->u.stringval, value); 1628 return(0); 1629 } 1630 1631 static void 1632 resource_cfgload(void *dummy __unused) 1633 { 1634 struct config_resource *res, *cfgres; 1635 int i, j; 1636 int error; 1637 char *name, *resname; 1638 int unit; 1639 resource_type type; 1640 char *stringval; 1641 int config_devtab_count; 1642 1643 config_devtab_count = devtab_count; 1644 devtab = NULL; 1645 devtab_count = 0; 1646 1647 for (i = 0; i < config_devtab_count; i++) { 1648 name = config_devtab[i].name; 1649 unit = config_devtab[i].unit; 1650 1651 for (j = 0; j < config_devtab[i].resource_count; j++) { 1652 cfgres = config_devtab[i].resources; 1653 resname = cfgres[j].name; 1654 type = cfgres[j].type; 1655 error = resource_create(name, unit, resname, type, 1656 &res); 1657 if (error) { 1658 kprintf("create resource %s%d: error %d\n", 1659 name, unit, error); 1660 continue; 1661 } 1662 if (res->type != type) { 1663 kprintf("type mismatch %s%d: %d != %d\n", 1664 name, unit, res->type, type); 1665 continue; 1666 } 1667 switch (type) { 1668 case RES_INT: 1669 res->u.intval = cfgres[j].u.intval; 1670 break; 1671 case RES_LONG: 1672 res->u.longval = cfgres[j].u.longval; 1673 break; 1674 case RES_STRING: 1675 if (res->u.stringval) 1676 kfree(res->u.stringval, M_TEMP); 1677 stringval = cfgres[j].u.stringval; 1678 res->u.stringval = kmalloc(strlen(stringval) + 1, 1679 M_TEMP, M_INTWAIT); 1680 if (res->u.stringval == NULL) 1681 break; 1682 strcpy(res->u.stringval, stringval); 1683 break; 1684 default: 1685 panic("unknown resource type %d", type); 1686 } 1687 } 1688 } 1689 } 1690 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0) 1691 1692 1693 /*======================================*/ 1694 /* 1695 * Some useful method implementations to make life easier for bus drivers. 1696 */ 1697 1698 void 1699 resource_list_init(struct resource_list *rl) 1700 { 1701 SLIST_INIT(rl); 1702 } 1703 1704 void 1705 resource_list_free(struct resource_list *rl) 1706 { 1707 struct resource_list_entry *rle; 1708 1709 while ((rle = SLIST_FIRST(rl)) != NULL) { 1710 if (rle->res) 1711 panic("resource_list_free: resource entry is busy"); 1712 SLIST_REMOVE_HEAD(rl, link); 1713 kfree(rle, M_BUS); 1714 } 1715 } 1716 1717 void 1718 resource_list_add(struct resource_list *rl, 1719 int type, int rid, 1720 u_long start, u_long end, u_long count) 1721 { 1722 struct resource_list_entry *rle; 1723 1724 rle = resource_list_find(rl, type, rid); 1725 if (rle == NULL) { 1726 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS, 1727 M_INTWAIT); 1728 if (!rle) 1729 panic("resource_list_add: can't record entry"); 1730 SLIST_INSERT_HEAD(rl, rle, link); 1731 rle->type = type; 1732 rle->rid = rid; 1733 rle->res = NULL; 1734 } 1735 1736 if (rle->res) 1737 panic("resource_list_add: resource entry is busy"); 1738 1739 rle->start = start; 1740 rle->end = end; 1741 rle->count = count; 1742 } 1743 1744 struct resource_list_entry* 1745 resource_list_find(struct resource_list *rl, 1746 int type, int rid) 1747 { 1748 struct resource_list_entry *rle; 1749 1750 SLIST_FOREACH(rle, rl, link) 1751 if (rle->type == type && rle->rid == rid) 1752 return(rle); 1753 return(NULL); 1754 } 1755 1756 void 1757 resource_list_delete(struct resource_list *rl, 1758 int type, int rid) 1759 { 1760 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 1761 1762 if (rle) { 1763 SLIST_REMOVE(rl, rle, resource_list_entry, link); 1764 kfree(rle, M_BUS); 1765 } 1766 } 1767 1768 struct resource * 1769 resource_list_alloc(struct resource_list *rl, 1770 device_t bus, device_t child, 1771 int type, int *rid, 1772 u_long start, u_long end, 1773 u_long count, u_int flags) 1774 { 1775 struct resource_list_entry *rle = 0; 1776 int passthrough = (device_get_parent(child) != bus); 1777 int isdefault = (start == 0UL && end == ~0UL); 1778 1779 if (passthrough) { 1780 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1781 type, rid, 1782 start, end, count, flags)); 1783 } 1784 1785 rle = resource_list_find(rl, type, *rid); 1786 1787 if (!rle) 1788 return(0); /* no resource of that type/rid */ 1789 if (rle->res) 1790 panic("resource_list_alloc: resource entry is busy"); 1791 1792 if (isdefault) { 1793 start = rle->start; 1794 count = max(count, rle->count); 1795 end = max(rle->end, start + count - 1); 1796 } 1797 1798 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1799 type, rid, start, end, count, flags); 1800 1801 /* 1802 * Record the new range. 1803 */ 1804 if (rle->res) { 1805 rle->start = rman_get_start(rle->res); 1806 rle->end = rman_get_end(rle->res); 1807 rle->count = count; 1808 } 1809 1810 return(rle->res); 1811 } 1812 1813 int 1814 resource_list_release(struct resource_list *rl, 1815 device_t bus, device_t child, 1816 int type, int rid, struct resource *res) 1817 { 1818 struct resource_list_entry *rle = 0; 1819 int passthrough = (device_get_parent(child) != bus); 1820 int error; 1821 1822 if (passthrough) { 1823 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1824 type, rid, res)); 1825 } 1826 1827 rle = resource_list_find(rl, type, rid); 1828 1829 if (!rle) 1830 panic("resource_list_release: can't find resource"); 1831 if (!rle->res) 1832 panic("resource_list_release: resource entry is not busy"); 1833 1834 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1835 type, rid, res); 1836 if (error) 1837 return(error); 1838 1839 rle->res = NULL; 1840 return(0); 1841 } 1842 1843 int 1844 resource_list_print_type(struct resource_list *rl, const char *name, int type, 1845 const char *format) 1846 { 1847 struct resource_list_entry *rle; 1848 int printed, retval; 1849 1850 printed = 0; 1851 retval = 0; 1852 /* Yes, this is kinda cheating */ 1853 SLIST_FOREACH(rle, rl, link) { 1854 if (rle->type == type) { 1855 if (printed == 0) 1856 retval += kprintf(" %s ", name); 1857 else 1858 retval += kprintf(","); 1859 printed++; 1860 retval += kprintf(format, rle->start); 1861 if (rle->count > 1) { 1862 retval += kprintf("-"); 1863 retval += kprintf(format, rle->start + 1864 rle->count - 1); 1865 } 1866 } 1867 } 1868 return(retval); 1869 } 1870 1871 /* 1872 * Generic driver/device identify functions. These will install a device 1873 * rendezvous point under the parent using the same name as the driver 1874 * name, which will at a later time be probed and attached. 1875 * 1876 * These functions are used when the parent does not 'scan' its bus for 1877 * matching devices, or for the particular devices using these functions, 1878 * or when the device is a pseudo or synthesized device (such as can be 1879 * found under firewire and ppbus). 1880 */ 1881 int 1882 bus_generic_identify(driver_t *driver, device_t parent) 1883 { 1884 if (parent->state == DS_ATTACHED) 1885 return (0); 1886 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1); 1887 return (0); 1888 } 1889 1890 int 1891 bus_generic_identify_sameunit(driver_t *driver, device_t parent) 1892 { 1893 if (parent->state == DS_ATTACHED) 1894 return (0); 1895 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent)); 1896 return (0); 1897 } 1898 1899 /* 1900 * Call DEVICE_IDENTIFY for each driver. 1901 */ 1902 int 1903 bus_generic_probe(device_t dev) 1904 { 1905 devclass_t dc = dev->devclass; 1906 driverlink_t dl; 1907 1908 TAILQ_FOREACH(dl, &dc->drivers, link) { 1909 DEVICE_IDENTIFY(dl->driver, dev); 1910 } 1911 1912 return(0); 1913 } 1914 1915 /* 1916 * This is an aweful hack due to the isa bus and autoconf code not 1917 * probing the ISA devices until after everything else has configured. 1918 * The ISA bus did a dummy attach long ago so we have to set it back 1919 * to an earlier state so the probe thinks its the initial probe and 1920 * not a bus rescan. 1921 * 1922 * XXX remove by properly defering the ISA bus scan. 1923 */ 1924 int 1925 bus_generic_probe_hack(device_t dev) 1926 { 1927 if (dev->state == DS_ATTACHED) { 1928 dev->state = DS_ALIVE; 1929 bus_generic_probe(dev); 1930 dev->state = DS_ATTACHED; 1931 } 1932 return (0); 1933 } 1934 1935 int 1936 bus_generic_attach(device_t dev) 1937 { 1938 device_t child; 1939 1940 TAILQ_FOREACH(child, &dev->children, link) { 1941 device_probe_and_attach(child); 1942 } 1943 1944 return(0); 1945 } 1946 1947 int 1948 bus_generic_detach(device_t dev) 1949 { 1950 device_t child; 1951 int error; 1952 1953 if (dev->state != DS_ATTACHED) 1954 return(EBUSY); 1955 1956 TAILQ_FOREACH(child, &dev->children, link) 1957 if ((error = device_detach(child)) != 0) 1958 return(error); 1959 1960 return 0; 1961 } 1962 1963 int 1964 bus_generic_shutdown(device_t dev) 1965 { 1966 device_t child; 1967 1968 TAILQ_FOREACH(child, &dev->children, link) 1969 device_shutdown(child); 1970 1971 return(0); 1972 } 1973 1974 int 1975 bus_generic_suspend(device_t dev) 1976 { 1977 int error; 1978 device_t child, child2; 1979 1980 TAILQ_FOREACH(child, &dev->children, link) { 1981 error = DEVICE_SUSPEND(child); 1982 if (error) { 1983 for (child2 = TAILQ_FIRST(&dev->children); 1984 child2 && child2 != child; 1985 child2 = TAILQ_NEXT(child2, link)) 1986 DEVICE_RESUME(child2); 1987 return(error); 1988 } 1989 } 1990 return(0); 1991 } 1992 1993 int 1994 bus_generic_resume(device_t dev) 1995 { 1996 device_t child; 1997 1998 TAILQ_FOREACH(child, &dev->children, link) 1999 DEVICE_RESUME(child); 2000 /* if resume fails, there's nothing we can usefully do... */ 2001 2002 return(0); 2003 } 2004 2005 int 2006 bus_print_child_header(device_t dev, device_t child) 2007 { 2008 int retval = 0; 2009 2010 if (device_get_desc(child)) 2011 retval += device_printf(child, "<%s>", device_get_desc(child)); 2012 else 2013 retval += kprintf("%s", device_get_nameunit(child)); 2014 if (bootverbose) { 2015 if (child->state != DS_ATTACHED) 2016 kprintf(" [tentative]"); 2017 else 2018 kprintf(" [attached!]"); 2019 } 2020 return(retval); 2021 } 2022 2023 int 2024 bus_print_child_footer(device_t dev, device_t child) 2025 { 2026 return(kprintf(" on %s\n", device_get_nameunit(dev))); 2027 } 2028 2029 device_t 2030 bus_generic_add_child(device_t dev, device_t child, int order, 2031 const char *name, int unit) 2032 { 2033 if (dev->parent) 2034 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit); 2035 else 2036 dev = device_add_child_ordered(child, order, name, unit); 2037 return(dev); 2038 2039 } 2040 2041 int 2042 bus_generic_print_child(device_t dev, device_t child) 2043 { 2044 int retval = 0; 2045 2046 retval += bus_print_child_header(dev, child); 2047 retval += bus_print_child_footer(dev, child); 2048 2049 return(retval); 2050 } 2051 2052 int 2053 bus_generic_read_ivar(device_t dev, device_t child, int index, 2054 uintptr_t * result) 2055 { 2056 int error; 2057 2058 if (dev->parent) 2059 error = BUS_READ_IVAR(dev->parent, child, index, result); 2060 else 2061 error = ENOENT; 2062 return (error); 2063 } 2064 2065 int 2066 bus_generic_write_ivar(device_t dev, device_t child, int index, 2067 uintptr_t value) 2068 { 2069 int error; 2070 2071 if (dev->parent) 2072 error = BUS_WRITE_IVAR(dev->parent, child, index, value); 2073 else 2074 error = ENOENT; 2075 return (error); 2076 } 2077 2078 /* 2079 * Resource list are used for iterations, do not recurse. 2080 */ 2081 struct resource_list * 2082 bus_generic_get_resource_list(device_t dev, device_t child) 2083 { 2084 return (NULL); 2085 } 2086 2087 void 2088 bus_generic_driver_added(device_t dev, driver_t *driver) 2089 { 2090 device_t child; 2091 2092 DEVICE_IDENTIFY(driver, dev); 2093 TAILQ_FOREACH(child, &dev->children, link) { 2094 if (child->state == DS_NOTPRESENT) 2095 device_probe_and_attach(child); 2096 } 2097 } 2098 2099 int 2100 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 2101 int flags, driver_intr_t *intr, void *arg, 2102 void **cookiep, lwkt_serialize_t serializer) 2103 { 2104 /* Propagate up the bus hierarchy until someone handles it. */ 2105 if (dev->parent) 2106 return(BUS_SETUP_INTR(dev->parent, child, irq, flags, 2107 intr, arg, cookiep, serializer)); 2108 else 2109 return(EINVAL); 2110 } 2111 2112 int 2113 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 2114 void *cookie) 2115 { 2116 /* Propagate up the bus hierarchy until someone handles it. */ 2117 if (dev->parent) 2118 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 2119 else 2120 return(EINVAL); 2121 } 2122 2123 int 2124 bus_generic_disable_intr(device_t dev, device_t child, void *cookie) 2125 { 2126 if (dev->parent) 2127 return(BUS_DISABLE_INTR(dev->parent, child, cookie)); 2128 else 2129 return(0); 2130 } 2131 2132 void 2133 bus_generic_enable_intr(device_t dev, device_t child, void *cookie) 2134 { 2135 if (dev->parent) 2136 BUS_ENABLE_INTR(dev->parent, child, cookie); 2137 } 2138 2139 int 2140 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, 2141 enum intr_polarity pol) 2142 { 2143 /* Propagate up the bus hierarchy until someone handles it. */ 2144 if (dev->parent) 2145 return(BUS_CONFIG_INTR(dev->parent, irq, trig, pol)); 2146 else 2147 return(EINVAL); 2148 } 2149 2150 struct resource * 2151 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 2152 u_long start, u_long end, u_long count, u_int flags) 2153 { 2154 /* Propagate up the bus hierarchy until someone handles it. */ 2155 if (dev->parent) 2156 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 2157 start, end, count, flags)); 2158 else 2159 return(NULL); 2160 } 2161 2162 int 2163 bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 2164 struct resource *r) 2165 { 2166 /* Propagate up the bus hierarchy until someone handles it. */ 2167 if (dev->parent) 2168 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r)); 2169 else 2170 return(EINVAL); 2171 } 2172 2173 int 2174 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 2175 struct resource *r) 2176 { 2177 /* Propagate up the bus hierarchy until someone handles it. */ 2178 if (dev->parent) 2179 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r)); 2180 else 2181 return(EINVAL); 2182 } 2183 2184 int 2185 bus_generic_deactivate_resource(device_t dev, device_t child, int type, 2186 int rid, struct resource *r) 2187 { 2188 /* Propagate up the bus hierarchy until someone handles it. */ 2189 if (dev->parent) 2190 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 2191 r)); 2192 else 2193 return(EINVAL); 2194 } 2195 2196 int 2197 bus_generic_get_resource(device_t dev, device_t child, int type, int rid, 2198 u_long *startp, u_long *countp) 2199 { 2200 int error; 2201 2202 error = ENOENT; 2203 if (dev->parent) { 2204 error = BUS_GET_RESOURCE(dev->parent, child, type, rid, 2205 startp, countp); 2206 } 2207 return (error); 2208 } 2209 2210 int 2211 bus_generic_set_resource(device_t dev, device_t child, int type, int rid, 2212 u_long start, u_long count) 2213 { 2214 int error; 2215 2216 error = EINVAL; 2217 if (dev->parent) { 2218 error = BUS_SET_RESOURCE(dev->parent, child, type, rid, 2219 start, count); 2220 } 2221 return (error); 2222 } 2223 2224 void 2225 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid) 2226 { 2227 if (dev->parent) 2228 BUS_DELETE_RESOURCE(dev, child, type, rid); 2229 } 2230 2231 int 2232 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 2233 u_long *startp, u_long *countp) 2234 { 2235 struct resource_list *rl = NULL; 2236 struct resource_list_entry *rle = NULL; 2237 2238 rl = BUS_GET_RESOURCE_LIST(dev, child); 2239 if (!rl) 2240 return(EINVAL); 2241 2242 rle = resource_list_find(rl, type, rid); 2243 if (!rle) 2244 return(ENOENT); 2245 2246 if (startp) 2247 *startp = rle->start; 2248 if (countp) 2249 *countp = rle->count; 2250 2251 return(0); 2252 } 2253 2254 int 2255 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 2256 u_long start, u_long count) 2257 { 2258 struct resource_list *rl = NULL; 2259 2260 rl = BUS_GET_RESOURCE_LIST(dev, child); 2261 if (!rl) 2262 return(EINVAL); 2263 2264 resource_list_add(rl, type, rid, start, (start + count - 1), count); 2265 2266 return(0); 2267 } 2268 2269 void 2270 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 2271 { 2272 struct resource_list *rl = NULL; 2273 2274 rl = BUS_GET_RESOURCE_LIST(dev, child); 2275 if (!rl) 2276 return; 2277 2278 resource_list_delete(rl, type, rid); 2279 } 2280 2281 int 2282 bus_generic_rl_release_resource(device_t dev, device_t child, int type, 2283 int rid, struct resource *r) 2284 { 2285 struct resource_list *rl = NULL; 2286 2287 rl = BUS_GET_RESOURCE_LIST(dev, child); 2288 if (!rl) 2289 return(EINVAL); 2290 2291 return(resource_list_release(rl, dev, child, type, rid, r)); 2292 } 2293 2294 struct resource * 2295 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 2296 int *rid, u_long start, u_long end, u_long count, u_int flags) 2297 { 2298 struct resource_list *rl = NULL; 2299 2300 rl = BUS_GET_RESOURCE_LIST(dev, child); 2301 if (!rl) 2302 return(NULL); 2303 2304 return(resource_list_alloc(rl, dev, child, type, rid, 2305 start, end, count, flags)); 2306 } 2307 2308 int 2309 bus_generic_child_present(device_t bus, device_t child) 2310 { 2311 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus)); 2312 } 2313 2314 2315 /* 2316 * Some convenience functions to make it easier for drivers to use the 2317 * resource-management functions. All these really do is hide the 2318 * indirection through the parent's method table, making for slightly 2319 * less-wordy code. In the future, it might make sense for this code 2320 * to maintain some sort of a list of resources allocated by each device. 2321 */ 2322 int 2323 bus_alloc_resources(device_t dev, struct resource_spec *rs, 2324 struct resource **res) 2325 { 2326 int i; 2327 2328 for (i = 0; rs[i].type != -1; i++) 2329 res[i] = NULL; 2330 for (i = 0; rs[i].type != -1; i++) { 2331 res[i] = bus_alloc_resource_any(dev, 2332 rs[i].type, &rs[i].rid, rs[i].flags); 2333 if (res[i] == NULL) { 2334 bus_release_resources(dev, rs, res); 2335 return (ENXIO); 2336 } 2337 } 2338 return (0); 2339 } 2340 2341 void 2342 bus_release_resources(device_t dev, const struct resource_spec *rs, 2343 struct resource **res) 2344 { 2345 int i; 2346 2347 for (i = 0; rs[i].type != -1; i++) 2348 if (res[i] != NULL) { 2349 bus_release_resource( 2350 dev, rs[i].type, rs[i].rid, res[i]); 2351 res[i] = NULL; 2352 } 2353 } 2354 2355 struct resource * 2356 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 2357 u_long count, u_int flags) 2358 { 2359 if (dev->parent == 0) 2360 return(0); 2361 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 2362 count, flags)); 2363 } 2364 2365 int 2366 bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 2367 { 2368 if (dev->parent == 0) 2369 return(EINVAL); 2370 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2371 } 2372 2373 int 2374 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 2375 { 2376 if (dev->parent == 0) 2377 return(EINVAL); 2378 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2379 } 2380 2381 int 2382 bus_release_resource(device_t dev, int type, int rid, struct resource *r) 2383 { 2384 if (dev->parent == 0) 2385 return(EINVAL); 2386 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 2387 } 2388 2389 int 2390 bus_setup_intr(device_t dev, struct resource *r, int flags, 2391 driver_intr_t handler, void *arg, 2392 void **cookiep, lwkt_serialize_t serializer) 2393 { 2394 if (dev->parent == 0) 2395 return(EINVAL); 2396 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg, 2397 cookiep, serializer)); 2398 } 2399 2400 int 2401 bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 2402 { 2403 if (dev->parent == 0) 2404 return(EINVAL); 2405 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 2406 } 2407 2408 void 2409 bus_enable_intr(device_t dev, void *cookie) 2410 { 2411 if (dev->parent) 2412 BUS_ENABLE_INTR(dev->parent, dev, cookie); 2413 } 2414 2415 int 2416 bus_disable_intr(device_t dev, void *cookie) 2417 { 2418 if (dev->parent) 2419 return(BUS_DISABLE_INTR(dev->parent, dev, cookie)); 2420 else 2421 return(0); 2422 } 2423 2424 int 2425 bus_set_resource(device_t dev, int type, int rid, 2426 u_long start, u_long count) 2427 { 2428 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 2429 start, count)); 2430 } 2431 2432 int 2433 bus_get_resource(device_t dev, int type, int rid, 2434 u_long *startp, u_long *countp) 2435 { 2436 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2437 startp, countp)); 2438 } 2439 2440 u_long 2441 bus_get_resource_start(device_t dev, int type, int rid) 2442 { 2443 u_long start, count; 2444 int error; 2445 2446 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2447 &start, &count); 2448 if (error) 2449 return(0); 2450 return(start); 2451 } 2452 2453 u_long 2454 bus_get_resource_count(device_t dev, int type, int rid) 2455 { 2456 u_long start, count; 2457 int error; 2458 2459 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2460 &start, &count); 2461 if (error) 2462 return(0); 2463 return(count); 2464 } 2465 2466 void 2467 bus_delete_resource(device_t dev, int type, int rid) 2468 { 2469 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 2470 } 2471 2472 int 2473 bus_child_present(device_t child) 2474 { 2475 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 2476 } 2477 2478 int 2479 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 2480 { 2481 device_t parent; 2482 2483 parent = device_get_parent(child); 2484 if (parent == NULL) { 2485 *buf = '\0'; 2486 return (0); 2487 } 2488 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 2489 } 2490 2491 int 2492 bus_child_location_str(device_t child, char *buf, size_t buflen) 2493 { 2494 device_t parent; 2495 2496 parent = device_get_parent(child); 2497 if (parent == NULL) { 2498 *buf = '\0'; 2499 return (0); 2500 } 2501 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 2502 } 2503 2504 static int 2505 root_print_child(device_t dev, device_t child) 2506 { 2507 return(0); 2508 } 2509 2510 static int 2511 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 2512 void **cookiep, lwkt_serialize_t serializer) 2513 { 2514 /* 2515 * If an interrupt mapping gets to here something bad has happened. 2516 */ 2517 panic("root_setup_intr"); 2518 } 2519 2520 /* 2521 * If we get here, assume that the device is permanant and really is 2522 * present in the system. Removable bus drivers are expected to intercept 2523 * this call long before it gets here. We return -1 so that drivers that 2524 * really care can check vs -1 or some ERRNO returned higher in the food 2525 * chain. 2526 */ 2527 static int 2528 root_child_present(device_t dev, device_t child) 2529 { 2530 return(-1); 2531 } 2532 2533 /* 2534 * XXX NOTE! other defaults may be set in bus_if.m 2535 */ 2536 static kobj_method_t root_methods[] = { 2537 /* Device interface */ 2538 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 2539 KOBJMETHOD(device_suspend, bus_generic_suspend), 2540 KOBJMETHOD(device_resume, bus_generic_resume), 2541 2542 /* Bus interface */ 2543 KOBJMETHOD(bus_add_child, bus_generic_add_child), 2544 KOBJMETHOD(bus_print_child, root_print_child), 2545 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 2546 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 2547 KOBJMETHOD(bus_setup_intr, root_setup_intr), 2548 KOBJMETHOD(bus_child_present, root_child_present), 2549 2550 { 0, 0 } 2551 }; 2552 2553 static driver_t root_driver = { 2554 "root", 2555 root_methods, 2556 1, /* no softc */ 2557 }; 2558 2559 device_t root_bus; 2560 devclass_t root_devclass; 2561 2562 static int 2563 root_bus_module_handler(module_t mod, int what, void* arg) 2564 { 2565 switch (what) { 2566 case MOD_LOAD: 2567 root_bus = make_device(NULL, "root", 0); 2568 root_bus->desc = "System root bus"; 2569 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 2570 root_bus->driver = &root_driver; 2571 root_bus->state = DS_ALIVE; 2572 root_devclass = devclass_find_internal("root", NULL, FALSE); 2573 return(0); 2574 2575 case MOD_SHUTDOWN: 2576 device_shutdown(root_bus); 2577 return(0); 2578 default: 2579 return(0); 2580 } 2581 } 2582 2583 static moduledata_t root_bus_mod = { 2584 "rootbus", 2585 root_bus_module_handler, 2586 0 2587 }; 2588 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 2589 2590 void 2591 root_bus_configure(void) 2592 { 2593 int warncount; 2594 device_t dev; 2595 2596 PDEBUG((".")); 2597 2598 /* 2599 * handle device_identify based device attachments to the root_bus 2600 * (typically nexus). 2601 */ 2602 bus_generic_probe(root_bus); 2603 2604 /* 2605 * Probe and attach the devices under root_bus. 2606 */ 2607 TAILQ_FOREACH(dev, &root_bus->children, link) { 2608 device_probe_and_attach(dev); 2609 } 2610 2611 /* 2612 * Wait for all asynchronous attaches to complete. If we don't 2613 * our legacy ISA bus scan could steal device unit numbers or 2614 * even I/O ports. 2615 */ 2616 warncount = 10; 2617 if (numasyncthreads) 2618 kprintf("Waiting for async drivers to attach\n"); 2619 while (numasyncthreads > 0) { 2620 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK) 2621 --warncount; 2622 if (warncount == 0) { 2623 kprintf("Warning: Still waiting for %d " 2624 "drivers to attach\n", numasyncthreads); 2625 } else if (warncount == -30) { 2626 kprintf("Giving up on %d drivers\n", numasyncthreads); 2627 break; 2628 } 2629 } 2630 root_bus->state = DS_ATTACHED; 2631 } 2632 2633 int 2634 driver_module_handler(module_t mod, int what, void *arg) 2635 { 2636 int error; 2637 struct driver_module_data *dmd; 2638 devclass_t bus_devclass; 2639 kobj_class_t driver; 2640 const char *parentname; 2641 2642 dmd = (struct driver_module_data *)arg; 2643 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE); 2644 error = 0; 2645 2646 switch (what) { 2647 case MOD_LOAD: 2648 if (dmd->dmd_chainevh) 2649 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 2650 2651 driver = dmd->dmd_driver; 2652 PDEBUG(("Loading module: driver %s on bus %s", 2653 DRIVERNAME(driver), dmd->dmd_busname)); 2654 2655 /* 2656 * If the driver has any base classes, make the 2657 * devclass inherit from the devclass of the driver's 2658 * first base class. This will allow the system to 2659 * search for drivers in both devclasses for children 2660 * of a device using this driver. 2661 */ 2662 if (driver->baseclasses) 2663 parentname = driver->baseclasses[0]->name; 2664 else 2665 parentname = NULL; 2666 *dmd->dmd_devclass = devclass_find_internal(driver->name, 2667 parentname, TRUE); 2668 2669 error = devclass_add_driver(bus_devclass, driver); 2670 if (error) 2671 break; 2672 break; 2673 2674 case MOD_UNLOAD: 2675 PDEBUG(("Unloading module: driver %s from bus %s", 2676 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname)); 2677 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver); 2678 2679 if (!error && dmd->dmd_chainevh) 2680 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 2681 break; 2682 } 2683 2684 return (error); 2685 } 2686 2687 #ifdef BUS_DEBUG 2688 2689 /* 2690 * The _short versions avoid iteration by not calling anything that prints 2691 * more than oneliners. I love oneliners. 2692 */ 2693 2694 static void 2695 print_device_short(device_t dev, int indent) 2696 { 2697 if (!dev) 2698 return; 2699 2700 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 2701 dev->unit, dev->desc, 2702 (dev->parent? "":"no "), 2703 (TAILQ_EMPTY(&dev->children)? "no ":""), 2704 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 2705 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 2706 (dev->flags&DF_WILDCARD? "wildcard,":""), 2707 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 2708 (dev->ivars? "":"no "), 2709 (dev->softc? "":"no "), 2710 dev->busy)); 2711 } 2712 2713 static void 2714 print_device(device_t dev, int indent) 2715 { 2716 if (!dev) 2717 return; 2718 2719 print_device_short(dev, indent); 2720 2721 indentprintf(("Parent:\n")); 2722 print_device_short(dev->parent, indent+1); 2723 indentprintf(("Driver:\n")); 2724 print_driver_short(dev->driver, indent+1); 2725 indentprintf(("Devclass:\n")); 2726 print_devclass_short(dev->devclass, indent+1); 2727 } 2728 2729 /* 2730 * Print the device and all its children (indented). 2731 */ 2732 void 2733 print_device_tree_short(device_t dev, int indent) 2734 { 2735 device_t child; 2736 2737 if (!dev) 2738 return; 2739 2740 print_device_short(dev, indent); 2741 2742 TAILQ_FOREACH(child, &dev->children, link) 2743 print_device_tree_short(child, indent+1); 2744 } 2745 2746 /* 2747 * Print the device and all its children (indented). 2748 */ 2749 void 2750 print_device_tree(device_t dev, int indent) 2751 { 2752 device_t child; 2753 2754 if (!dev) 2755 return; 2756 2757 print_device(dev, indent); 2758 2759 TAILQ_FOREACH(child, &dev->children, link) 2760 print_device_tree(child, indent+1); 2761 } 2762 2763 static void 2764 print_driver_short(driver_t *driver, int indent) 2765 { 2766 if (!driver) 2767 return; 2768 2769 indentprintf(("driver %s: softc size = %d\n", 2770 driver->name, driver->size)); 2771 } 2772 2773 static void 2774 print_driver(driver_t *driver, int indent) 2775 { 2776 if (!driver) 2777 return; 2778 2779 print_driver_short(driver, indent); 2780 } 2781 2782 2783 static void 2784 print_driver_list(driver_list_t drivers, int indent) 2785 { 2786 driverlink_t driver; 2787 2788 TAILQ_FOREACH(driver, &drivers, link) 2789 print_driver(driver->driver, indent); 2790 } 2791 2792 static void 2793 print_devclass_short(devclass_t dc, int indent) 2794 { 2795 if (!dc) 2796 return; 2797 2798 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 2799 } 2800 2801 static void 2802 print_devclass(devclass_t dc, int indent) 2803 { 2804 int i; 2805 2806 if (!dc) 2807 return; 2808 2809 print_devclass_short(dc, indent); 2810 indentprintf(("Drivers:\n")); 2811 print_driver_list(dc->drivers, indent+1); 2812 2813 indentprintf(("Devices:\n")); 2814 for (i = 0; i < dc->maxunit; i++) 2815 if (dc->devices[i]) 2816 print_device(dc->devices[i], indent+1); 2817 } 2818 2819 void 2820 print_devclass_list_short(void) 2821 { 2822 devclass_t dc; 2823 2824 kprintf("Short listing of devclasses, drivers & devices:\n"); 2825 TAILQ_FOREACH(dc, &devclasses, link) { 2826 print_devclass_short(dc, 0); 2827 } 2828 } 2829 2830 void 2831 print_devclass_list(void) 2832 { 2833 devclass_t dc; 2834 2835 kprintf("Full listing of devclasses, drivers & devices:\n"); 2836 TAILQ_FOREACH(dc, &devclasses, link) { 2837 print_devclass(dc, 0); 2838 } 2839 } 2840 2841 #endif 2842 2843 /* 2844 * Check to see if a device is disabled via a disabled hint. 2845 */ 2846 int 2847 resource_disabled(const char *name, int unit) 2848 { 2849 int error, value; 2850 2851 error = resource_int_value(name, unit, "disabled", &value); 2852 if (error) 2853 return(0); 2854 return(value); 2855 } 2856