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