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