subr_bus.c revision 45107
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 * $Id: subr_bus.c,v 1.15 1999/01/27 21:49:57 dillon Exp $ 27 */ 28 29#include <sys/param.h> 30#include <sys/queue.h> 31#include <sys/malloc.h> 32#include <sys/kernel.h> 33#include <sys/module.h> 34#include <sys/sysctl.h> 35#include <sys/bus_private.h> 36#include <sys/systm.h> 37#include <machine/stdarg.h> /* for device_printf() */ 38 39#include "opt_bus.h" 40 41#ifdef BUS_DEBUG 42#define PDEBUG(a) (printf(__FUNCTION__ ":%d: ", __LINE__), printf a, printf("\n")) 43#define DEVICENAME(d) ((d)? device_get_name(d): "no device") 44#define DRIVERNAME(d) ((d)? d->name : "no driver") 45#define DEVCLANAME(d) ((d)? d->name : "no devclass") 46 47/* Produce the indenting, indent*2 spaces plus a '.' ahead of that to 48 * prevent syslog from deleting initial spaces 49 */ 50#define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while(0) 51 52static void print_method_list(device_method_t *m, int indent); 53static void print_device_ops(device_ops_t ops, int indent); 54static void print_device_short(device_t dev, int indent); 55static void print_device(device_t dev, int indent); 56void print_device_tree_short(device_t dev, int indent); 57void print_device_tree(device_t dev, int indent); 58static void print_driver_short(driver_t *driver, int indent); 59static void print_driver(driver_t *driver, int indent); 60static void print_driver_list(driver_list_t drivers, int indent); 61static void print_devclass_short(devclass_t dc, int indent); 62static void print_devclass(devclass_t dc, int indent); 63void print_devclass_list_short(void); 64void print_devclass_list(void); 65 66#else 67/* Make the compiler ignore the function calls */ 68#define PDEBUG(a) /* nop */ 69#define DEVICENAME(d) /* nop */ 70#define DRIVERNAME(d) /* nop */ 71#define DEVCLANAME(d) /* nop */ 72 73#define print_method_list(m,i) /* nop */ 74#define print_device_ops(o,i) /* nop */ 75#define print_device_short(d,i) /* nop */ 76#define print_device(d,i) /* nop */ 77#define print_device_tree_short(d,i) /* nop */ 78#define print_device_tree(d,i) /* nop */ 79#define print_driver_short(d,i) /* nop */ 80#define print_driver(d,i) /* nop */ 81#define print_driver_list(d,i) /* nop */ 82#define print_devclass_short(d,i) /* nop */ 83#define print_devclass(d,i) /* nop */ 84#define print_devclass_list_short() /* nop */ 85#define print_devclass_list() /* nop */ 86#endif 87 88#ifdef DEVICE_SYSCTLS 89static void device_register_oids(device_t dev); 90static void device_unregister_oids(device_t dev); 91#endif 92 93/* 94 * Method table handling 95 */ 96static int next_method_offset = 1; 97static int methods_count = 0; 98static int methods_size = 0; 99 100struct method { 101 int offset; 102 char* name; 103}; 104 105static struct method *methods = 0; 106 107static void 108register_method(struct device_op_desc *desc) 109{ 110 int i; 111 struct method* m; 112 113 for (i = 0; i < methods_count; i++) 114 if (!strcmp(methods[i].name, desc->name)) { 115 desc->offset = methods[i].offset; 116 PDEBUG(("methods[%d] has the same name, %s, with offset %d", 117 i, desc->name, desc->offset)); 118 return; 119 } 120 121 if (methods_count == methods_size) { 122 struct method* p; 123 124 methods_size += 10; 125 p = (struct method*) malloc(methods_size * sizeof(struct method), 126 M_DEVBUF, M_NOWAIT); 127 if (!p) 128 panic("register_method: out of memory"); 129 if (methods) { 130 bcopy(methods, p, methods_count * sizeof(struct method)); 131 free(methods, M_DEVBUF); 132 } 133 methods = p; 134 } 135 m = &methods[methods_count++]; 136 m->name = malloc(strlen(desc->name) + 1, M_DEVBUF, M_NOWAIT); 137 if (!m->name) 138 panic("register_method: out of memory"); 139 strcpy(m->name, desc->name); 140 desc->offset = m->offset = next_method_offset++; 141} 142 143static int error_method(void) 144{ 145 return ENXIO; 146} 147 148static struct device_ops null_ops = { 149 1, 150 { error_method } 151}; 152 153static void 154compile_methods(driver_t *driver) 155{ 156 device_ops_t ops; 157 struct device_method *m; 158 int i; 159 160 /* 161 * First register any methods which need it. 162 */ 163 for (i = 0, m = driver->methods; m->desc; i++, m++) 164 if (!m->desc->offset) 165 register_method(m->desc); 166 else 167 PDEBUG(("offset not equal to zero, method desc %d left as is", i)); 168 169 /* 170 * Then allocate the compiled op table. 171 */ 172 ops = malloc(sizeof(struct device_ops) + (next_method_offset-1) * sizeof(devop_t), 173 M_DEVBUF, M_NOWAIT); 174 if (!ops) 175 panic("compile_methods: out of memory"); 176 177 ops->maxoffset = next_method_offset; 178 for (i = 0; i < next_method_offset; i++) 179 ops->methods[i] = error_method; 180 for (i = 0, m = driver->methods; m->desc; i++, m++) 181 ops->methods[m->desc->offset] = m->func; 182 PDEBUG(("%s has %d method%s, wasting %d bytes", 183 DRIVERNAME(driver), i, (i==1?"":"s"), 184 (next_method_offset-i)*sizeof(devop_t))); 185 186 driver->ops = ops; 187} 188 189/* 190 * Devclass implementation 191 */ 192 193static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 194 195static devclass_t 196devclass_find_internal(const char *classname, int create) 197{ 198 devclass_t dc; 199 200 PDEBUG(("looking for %s", classname)); 201 if (!classname) 202 return NULL; 203 204 for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link)) 205 if (!strcmp(dc->name, classname)) 206 return dc; 207 208 PDEBUG(("%s not found%s", classname, (create? ", creating": ""))); 209 if (create) { 210 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 211 M_DEVBUF, M_NOWAIT); 212 if (!dc) 213 return NULL; 214 dc->name = (char*) (dc + 1); 215 strcpy(dc->name, classname); 216 dc->devices = NULL; 217 dc->maxunit = 0; 218 dc->nextunit = 0; 219 TAILQ_INIT(&dc->drivers); 220 TAILQ_INSERT_TAIL(&devclasses, dc, link); 221 } 222 223 return dc; 224} 225 226devclass_t 227devclass_find(const char *classname) 228{ 229 return devclass_find_internal(classname, FALSE); 230} 231 232int 233devclass_add_driver(devclass_t dc, driver_t *driver) 234{ 235 driverlink_t dl; 236 237 PDEBUG(("%s", DRIVERNAME(driver))); 238 239 dl = malloc(sizeof *dl, M_DEVBUF, M_NOWAIT); 240 if (!dl) 241 return ENOMEM; 242 243 /* 244 * Compile the drivers methods. 245 */ 246 compile_methods(driver); 247 248 /* 249 * Make sure the devclass which the driver is implementing exists. 250 */ 251 devclass_find_internal(driver->name, TRUE); 252 253 dl->driver = driver; 254 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 255 256 return 0; 257} 258 259int 260devclass_delete_driver(devclass_t busclass, driver_t *driver) 261{ 262 devclass_t dc = devclass_find(driver->name); 263 driverlink_t dl; 264 device_t dev; 265 int i; 266 int error; 267 268 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 269 270 if (!dc) 271 return 0; 272 273 /* 274 * Find the link structure in the bus' list of drivers. 275 */ 276 for (dl = TAILQ_FIRST(&busclass->drivers); dl; 277 dl = TAILQ_NEXT(dl, link)) { 278 if (dl->driver == driver) 279 break; 280 } 281 282 if (!dl) { 283 PDEBUG(("%s not found in %s list", driver->name, busclass->name)); 284 return ENOENT; 285 } 286 287 /* 288 * Disassociate from any devices. We iterate through all the 289 * devices in the devclass of the driver and detach any which are 290 * using the driver and which have a parent in the devclass which 291 * we are deleting from. 292 * 293 * Note that since a driver can be in multiple devclasses, we 294 * should not detach devices which are not children of devices in 295 * the affected devclass. 296 */ 297 for (i = 0; i < dc->maxunit; i++) { 298 if (dc->devices[i]) { 299 dev = dc->devices[i]; 300 if (dev->driver == driver 301 && dev->parent && dev->parent->devclass == busclass) { 302 if ((error = device_detach(dev)) != 0) 303 return error; 304 device_set_driver(dev, NULL); 305 } 306 } 307 } 308 309 TAILQ_REMOVE(&busclass->drivers, dl, link); 310 free(dl, M_DEVBUF); 311 312 return 0; 313} 314 315static driverlink_t 316devclass_find_driver_internal(devclass_t dc, const char *classname) 317{ 318 driverlink_t dl; 319 320 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 321 322 for (dl = TAILQ_FIRST(&dc->drivers); dl; dl = TAILQ_NEXT(dl, link)) { 323 if (!strcmp(dl->driver->name, classname)) 324 return dl; 325 } 326 327 PDEBUG(("not found")); 328 return NULL; 329} 330 331driver_t * 332devclass_find_driver(devclass_t dc, const char *classname) 333{ 334 driverlink_t dl; 335 336 dl = devclass_find_driver_internal(dc, classname); 337 if (dl) 338 return dl->driver; 339 else 340 return NULL; 341} 342 343const char * 344devclass_get_name(devclass_t dc) 345{ 346 return dc->name; 347} 348 349device_t 350devclass_get_device(devclass_t dc, int unit) 351{ 352 if (unit < 0 || unit >= dc->maxunit) 353 return NULL; 354 return dc->devices[unit]; 355} 356 357void * 358devclass_get_softc(devclass_t dc, int unit) 359{ 360 device_t dev; 361 362 if (unit < 0 || unit >= dc->maxunit) 363 return NULL; 364 dev = dc->devices[unit]; 365 if (!dev || dev->state < DS_ATTACHED) 366 return NULL; 367 return dev->softc; 368} 369 370int 371devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 372{ 373 int i; 374 int count; 375 device_t *list; 376 377 count = 0; 378 for (i = 0; i < dc->maxunit; i++) 379 if (dc->devices[i]) 380 count++; 381 382 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT); 383 if (!list) 384 return ENOMEM; 385 386 count = 0; 387 for (i = 0; i < dc->maxunit; i++) 388 if (dc->devices[i]) { 389 list[count] = dc->devices[i]; 390 count++; 391 } 392 393 *devlistp = list; 394 *devcountp = count; 395 396 return 0; 397} 398 399int 400devclass_get_maxunit(devclass_t dc) 401{ 402 return dc->maxunit; 403} 404 405static int 406devclass_alloc_unit(devclass_t dc, int *unitp) 407{ 408 int unit = *unitp; 409 410 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 411 412 /* 413 * If we have been given a wired unit number, check for existing 414 * device. 415 */ 416 if (unit != -1) { 417 device_t dev; 418 dev = devclass_get_device(dc, unit); 419 if (dev) { 420 printf("devclass_alloc_unit: %s%d already exists, using next available unit number\n", dc->name, unit); 421 unit = -1; 422 } 423 } 424 425 if (unit == -1) { 426 unit = dc->nextunit; 427 dc->nextunit++; 428 } else if (dc->nextunit <= unit) 429 dc->nextunit = unit + 1; 430 431 if (unit >= dc->maxunit) { 432 device_t *newlist; 433 int newsize; 434 435 newsize = (dc->maxunit ? 2 * dc->maxunit 436 : MINALLOCSIZE / sizeof(device_t)); 437 newlist = malloc(sizeof(device_t) * newsize, M_DEVBUF, M_NOWAIT); 438 if (!newlist) 439 return ENOMEM; 440 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 441 bzero(newlist + dc->maxunit, 442 sizeof(device_t) * (newsize - dc->maxunit)); 443 if (dc->devices) 444 free(dc->devices, M_DEVBUF); 445 dc->devices = newlist; 446 dc->maxunit = newsize; 447 } 448 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 449 450 *unitp = unit; 451 return 0; 452} 453 454static int 455devclass_add_device(devclass_t dc, device_t dev) 456{ 457 int buflen, error; 458 459 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 460 461 buflen = strlen(dc->name) + 5; 462 dev->nameunit = malloc(buflen, M_DEVBUF, M_NOWAIT); 463 if (!dev->nameunit) 464 return ENOMEM; 465 466 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { 467 free(dev->nameunit, M_DEVBUF); 468 dev->nameunit = NULL; 469 return error; 470 } 471 dc->devices[dev->unit] = dev; 472 dev->devclass = dc; 473 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 474 475#ifdef DEVICE_SYSCTLS 476 device_register_oids(dev); 477#endif 478 479 return 0; 480} 481 482static int 483devclass_delete_device(devclass_t dc, device_t dev) 484{ 485 if (!dc || !dev) 486 return 0; 487 488 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 489 490 if (dev->devclass != dc 491 || dc->devices[dev->unit] != dev) 492 panic("devclass_delete_device: inconsistent device class"); 493 dc->devices[dev->unit] = NULL; 494 if (dev->flags & DF_WILDCARD) 495 dev->unit = -1; 496 dev->devclass = NULL; 497 free(dev->nameunit, M_DEVBUF); 498 dev->nameunit = NULL; 499 while (dc->nextunit > 0 && dc->devices[dc->nextunit - 1] == NULL) 500 dc->nextunit--; 501 502#ifdef DEVICE_SYSCTLS 503 device_unregister_oids(dev); 504#endif 505 506 return 0; 507} 508 509static device_t 510make_device(device_t parent, const char *name, 511 int unit, void *ivars) 512{ 513 device_t dev; 514 devclass_t dc; 515 516 PDEBUG(("%s at %s as unit %d with%s ivars", 517 name, DEVICENAME(parent), unit, (ivars? "":"out"))); 518 519 if (name) { 520 dc = devclass_find_internal(name, TRUE); 521 if (!dc) { 522 printf("make_device: can't find device class %s\n", name); 523 return NULL; 524 } 525 } else 526 dc = NULL; 527 528 dev = malloc(sizeof(struct device), M_DEVBUF, M_NOWAIT); 529 if (!dev) 530 return 0; 531 532 dev->parent = parent; 533 TAILQ_INIT(&dev->children); 534 dev->ops = &null_ops; 535 dev->driver = NULL; 536 dev->devclass = NULL; 537 dev->unit = unit; 538 dev->nameunit = NULL; 539 dev->desc = NULL; 540 dev->busy = 0; 541 dev->flags = DF_ENABLED; 542 if (unit == -1) 543 dev->flags |= DF_WILDCARD; 544 if (name) { 545 dev->flags |= DF_FIXEDCLASS; 546 devclass_add_device(dc, dev); 547 } 548 dev->ivars = ivars; 549 dev->softc = NULL; 550 551 dev->state = DS_NOTPRESENT; 552 553 return dev; 554} 555 556static void 557device_print_child(device_t dev, device_t child) 558{ 559 printf("%s%d", device_get_name(child), device_get_unit(child)); 560 if (device_is_alive(child)) { 561 if (device_get_desc(child)) 562 printf(": <%s>", device_get_desc(child)); 563 BUS_PRINT_CHILD(dev, child); 564 } else 565 printf(" not found"); 566 printf("\n"); 567} 568 569device_t 570device_add_child(device_t dev, const char *name, int unit, void *ivars) 571{ 572 device_t child; 573 574 PDEBUG(("%s at %s as unit %d with%s ivars", 575 name, DEVICENAME(dev), unit, (ivars? "":"out"))); 576 577 child = make_device(dev, name, unit, ivars); 578 579 if (child) 580 TAILQ_INSERT_TAIL(&dev->children, child, link); 581 else 582 PDEBUG(("%s failed", name)); 583 584 return child; 585} 586 587device_t 588device_add_child_after(device_t dev, device_t place, const char *name, 589 int unit, void *ivars) 590{ 591 device_t child; 592 593 PDEBUG(("%s at %s after %s as unit %d with%s ivars", 594 name, DEVICENAME(dev), DEVICENAME(place), unit, (ivars? "":"out"))); 595 596 child = make_device(dev, name, unit, ivars); 597 598 if (place) { 599 TAILQ_INSERT_AFTER(&dev->children, place, dev, link); 600 } else { 601 TAILQ_INSERT_HEAD(&dev->children, dev, link); 602 } 603 604 return child; 605} 606 607int 608device_delete_child(device_t dev, device_t child) 609{ 610 int error; 611 device_t grandchild; 612 613 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 614 615 /* remove children first */ 616 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 617 error = device_delete_child(child, grandchild); 618 if (error) 619 return error; 620 } 621 622 if ((error = device_detach(child)) != 0) 623 return error; 624 if (child->devclass) 625 devclass_delete_device(child->devclass, child); 626 TAILQ_REMOVE(&dev->children, child, link); 627 device_set_desc(child, NULL); 628 free(child, M_DEVBUF); 629 630 return 0; 631} 632 633/* 634 * Find only devices attached to this bus. 635 */ 636device_t 637device_find_child(device_t dev, const char *classname, int unit) 638{ 639 devclass_t dc; 640 device_t child; 641 642 dc = devclass_find(classname); 643 if (!dc) 644 return NULL; 645 646 child = devclass_get_device(dc, unit); 647 if (child && child->parent == dev) 648 return child; 649 return NULL; 650} 651 652static driverlink_t 653first_matching_driver(devclass_t dc, device_t dev) 654{ 655 if (dev->devclass) 656 return devclass_find_driver_internal(dc, dev->devclass->name); 657 else 658 return TAILQ_FIRST(&dc->drivers); 659} 660 661static driverlink_t 662next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 663{ 664 if (dev->devclass) { 665 driverlink_t dl; 666 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 667 if (!strcmp(dev->devclass->name, dl->driver->name)) 668 return dl; 669 return NULL; 670 } else 671 return TAILQ_NEXT(last, link); 672} 673 674static int 675device_probe_child(device_t dev, device_t child) 676{ 677 devclass_t dc; 678 driverlink_t dl; 679 680 dc = dev->devclass; 681 if (dc == NULL) 682 panic("device_probe_child: parent device has no devclass"); 683 684 if (child->state == DS_ALIVE) 685 return 0; 686 687 for (dl = first_matching_driver(dc, child); 688 dl; 689 dl = next_matching_driver(dc, child, dl)) { 690 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 691 device_set_driver(child, dl->driver); 692 if (DEVICE_PROBE(child) == 0) { 693 if (!child->devclass) 694 device_set_devclass(child, dl->driver->name); 695 child->state = DS_ALIVE; 696 return 0; 697 } 698 } 699 700 return ENXIO; 701} 702 703device_t 704device_get_parent(device_t dev) 705{ 706 return dev->parent; 707} 708 709int 710device_get_children(device_t dev, device_t **devlistp, int *devcountp) 711{ 712 int count; 713 device_t child; 714 device_t *list; 715 716 count = 0; 717 for (child = TAILQ_FIRST(&dev->children); child; 718 child = TAILQ_NEXT(child, link)) 719 count++; 720 721 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT); 722 if (!list) 723 return ENOMEM; 724 725 count = 0; 726 for (child = TAILQ_FIRST(&dev->children); child; 727 child = TAILQ_NEXT(child, link)) { 728 list[count] = child; 729 count++; 730 } 731 732 *devlistp = list; 733 *devcountp = count; 734 735 return 0; 736} 737 738driver_t * 739device_get_driver(device_t dev) 740{ 741 return dev->driver; 742} 743 744devclass_t 745device_get_devclass(device_t dev) 746{ 747 return dev->devclass; 748} 749 750const char * 751device_get_name(device_t dev) 752{ 753 if (dev->devclass) 754 return devclass_get_name(dev->devclass); 755 return NULL; 756} 757 758const char * 759device_get_nameunit(device_t dev) 760{ 761 return dev->nameunit; 762} 763 764int 765device_get_unit(device_t dev) 766{ 767 return dev->unit; 768} 769 770const char * 771device_get_desc(device_t dev) 772{ 773 return dev->desc; 774} 775 776void 777device_print_prettyname(device_t dev) 778{ 779 const char *name = device_get_name(dev); 780 781 if (name == 0) 782 name = "(no driver assigned)"; 783 printf("%s%d: ", name, device_get_unit(dev)); 784} 785 786void 787device_printf(device_t dev, const char * fmt, ...) 788{ 789 va_list ap; 790 791 device_print_prettyname(dev); 792 va_start(ap, fmt); 793 vprintf(fmt, ap); 794 va_end(ap); 795} 796 797static void 798device_set_desc_internal(device_t dev, const char* desc, int copy) 799{ 800 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 801 free(dev->desc, M_DEVBUF); 802 dev->flags &= ~DF_DESCMALLOCED; 803 dev->desc = NULL; 804 } 805 806 if (copy && desc) { 807 dev->desc = malloc(strlen(desc) + 1, M_DEVBUF, M_NOWAIT); 808 if (dev->desc) { 809 strcpy(dev->desc, desc); 810 dev->flags |= DF_DESCMALLOCED; 811 } 812 } else 813 /* Avoid a -Wcast-qual warning */ 814 dev->desc = (char *)(uintptr_t) desc; 815 816#ifdef DEVICE_SYSCTLS 817 { 818 struct sysctl_oid *oid = &dev->oid[1]; 819 oid->oid_arg1 = dev->desc ? dev->desc : ""; 820 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0; 821 } 822#endif 823} 824 825void 826device_set_desc(device_t dev, const char* desc) 827{ 828 device_set_desc_internal(dev, desc, FALSE); 829} 830 831void 832device_set_desc_copy(device_t dev, const char* desc) 833{ 834 device_set_desc_internal(dev, desc, TRUE); 835} 836 837void * 838device_get_softc(device_t dev) 839{ 840 return dev->softc; 841} 842 843void * 844device_get_ivars(device_t dev) 845{ 846 return dev->ivars; 847} 848 849device_state_t 850device_get_state(device_t dev) 851{ 852 return dev->state; 853} 854 855void 856device_enable(device_t dev) 857{ 858 dev->flags |= DF_ENABLED; 859} 860 861void 862device_disable(device_t dev) 863{ 864 dev->flags &= ~DF_ENABLED; 865} 866 867void 868device_busy(device_t dev) 869{ 870 if (dev->state < DS_ATTACHED) 871 panic("device_busy: called for unattached device"); 872 if (dev->busy == 0 && dev->parent) 873 device_busy(dev->parent); 874 dev->busy++; 875 dev->state = DS_BUSY; 876} 877 878void 879device_unbusy(device_t dev) 880{ 881 if (dev->state != DS_BUSY) 882 panic("device_unbusy: called for non-busy device"); 883 dev->busy--; 884 if (dev->busy == 0) { 885 if (dev->parent) 886 device_unbusy(dev->parent); 887 dev->state = DS_ATTACHED; 888 } 889} 890 891void 892device_quiet(device_t dev) 893{ 894 dev->flags |= DF_QUIET; 895} 896 897void 898device_verbose(device_t dev) 899{ 900 dev->flags &= ~DF_QUIET; 901} 902 903int 904device_is_quiet(device_t dev) 905{ 906 return (dev->flags & DF_QUIET) != 0; 907} 908 909int 910device_is_enabled(device_t dev) 911{ 912 return (dev->flags & DF_ENABLED) != 0; 913} 914 915int 916device_is_alive(device_t dev) 917{ 918 return dev->state >= DS_ALIVE; 919} 920 921int 922device_set_devclass(device_t dev, const char *classname) 923{ 924 devclass_t dc; 925 926 if (dev->devclass) { 927 printf("device_set_devclass: device class already set\n"); 928 return EINVAL; 929 } 930 931 dc = devclass_find_internal(classname, TRUE); 932 if (!dc) 933 return ENOMEM; 934 935 return devclass_add_device(dc, dev); 936} 937 938int 939device_set_driver(device_t dev, driver_t *driver) 940{ 941 if (dev->state >= DS_ATTACHED) 942 return EBUSY; 943 944 if (dev->driver == driver) 945 return 0; 946 947 if (dev->softc) { 948 free(dev->softc, M_DEVBUF); 949 dev->softc = NULL; 950 } 951 dev->ops = &null_ops; 952 dev->driver = driver; 953 if (driver) { 954 dev->ops = driver->ops; 955 dev->softc = malloc(driver->softc, M_DEVBUF, M_NOWAIT); 956 if (!dev->softc) { 957 dev->ops = &null_ops; 958 dev->driver = NULL; 959 return ENOMEM; 960 } 961 bzero(dev->softc, driver->softc); 962 } 963 return 0; 964} 965 966int 967device_probe_and_attach(device_t dev) 968{ 969 device_t bus = dev->parent; 970 int error = 0; 971 972 if (dev->state >= DS_ALIVE) 973 return 0; 974 975 if (dev->flags & DF_ENABLED) { 976 error = device_probe_child(bus, dev); 977 if (!error) { 978 if (!device_is_quiet(dev)) 979 device_print_child(bus, dev); 980 error = DEVICE_ATTACH(dev); 981 if (!error) 982 dev->state = DS_ATTACHED; 983 else { 984 printf("device_probe_and_attach: %s%d attach returned %d\n", 985 dev->driver->name, dev->unit, error); 986 device_set_driver(dev, NULL); 987 dev->state = DS_NOTPRESENT; 988 } 989 } 990 } else { 991 device_print_prettyname(dev); 992 printf("not probed (disabled)\n"); 993 } 994 995 return error; 996} 997 998int 999device_detach(device_t dev) 1000{ 1001 int error; 1002 1003 PDEBUG(("%s", DEVICENAME(dev))); 1004 if (dev->state == DS_BUSY) 1005 return EBUSY; 1006 if (dev->state != DS_ATTACHED) 1007 return 0; 1008 1009 if ((error = DEVICE_DETACH(dev)) != 0) 1010 return error; 1011 if (dev->parent) 1012 BUS_CHILD_DETACHED(dev->parent, dev); 1013 1014 if (!(dev->flags & DF_FIXEDCLASS)) 1015 devclass_delete_device(dev->devclass, dev); 1016 1017 dev->state = DS_NOTPRESENT; 1018 device_set_driver(dev, NULL); 1019 1020 return 0; 1021} 1022 1023int 1024device_shutdown(device_t dev) 1025{ 1026 if (dev->state < DS_ATTACHED) 1027 return 0; 1028 return DEVICE_SHUTDOWN(dev); 1029} 1030 1031#ifdef DEVICE_SYSCTLS 1032 1033/* 1034 * Sysctl nodes for devices. 1035 */ 1036 1037SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices"); 1038 1039static int 1040sysctl_handle_children SYSCTL_HANDLER_ARGS 1041{ 1042 device_t dev = arg1; 1043 device_t child; 1044 int first = 1, error = 0; 1045 1046 for (child = TAILQ_FIRST(&dev->children); child; 1047 child = TAILQ_NEXT(child, link)) { 1048 if (child->nameunit) { 1049 if (!first) { 1050 error = SYSCTL_OUT(req, ",", 1); 1051 if (error) return error; 1052 } else { 1053 first = 0; 1054 } 1055 error = SYSCTL_OUT(req, child->nameunit, strlen(child->nameunit)); 1056 if (error) return error; 1057 } 1058 } 1059 1060 error = SYSCTL_OUT(req, "", 1); 1061 1062 return error; 1063} 1064 1065static int 1066sysctl_handle_state SYSCTL_HANDLER_ARGS 1067{ 1068 device_t dev = arg1; 1069 1070 switch (dev->state) { 1071 case DS_NOTPRESENT: 1072 return SYSCTL_OUT(req, "notpresent", sizeof("notpresent")); 1073 case DS_ALIVE: 1074 return SYSCTL_OUT(req, "alive", sizeof("alive")); 1075 case DS_ATTACHED: 1076 return SYSCTL_OUT(req, "attached", sizeof("attached")); 1077 case DS_BUSY: 1078 return SYSCTL_OUT(req, "busy", sizeof("busy")); 1079 } 1080 1081 return 0; 1082} 1083 1084static void 1085device_register_oids(device_t dev) 1086{ 1087 struct sysctl_oid* oid; 1088 1089 oid = &dev->oid[0]; 1090 bzero(oid, sizeof(*oid)); 1091 oid->oid_parent = &sysctl__hw_devices_children; 1092 oid->oid_number = OID_AUTO; 1093 oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW; 1094 oid->oid_arg1 = &dev->oidlist[0]; 1095 oid->oid_arg2 = 0; 1096 oid->oid_name = dev->nameunit; 1097 oid->oid_handler = 0; 1098 oid->oid_fmt = "N"; 1099 SLIST_INIT(&dev->oidlist[0]); 1100 sysctl_register_oid(oid); 1101 1102 oid = &dev->oid[1]; 1103 bzero(oid, sizeof(*oid)); 1104 oid->oid_parent = &dev->oidlist[0]; 1105 oid->oid_number = OID_AUTO; 1106 oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD; 1107 oid->oid_arg1 = dev->desc ? dev->desc : ""; 1108 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0; 1109 oid->oid_name = "desc"; 1110 oid->oid_handler = sysctl_handle_string; 1111 oid->oid_fmt = "A"; 1112 sysctl_register_oid(oid); 1113 1114 oid = &dev->oid[2]; 1115 bzero(oid, sizeof(*oid)); 1116 oid->oid_parent = &dev->oidlist[0]; 1117 oid->oid_number = OID_AUTO; 1118 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD; 1119 oid->oid_arg1 = dev; 1120 oid->oid_arg2 = 0; 1121 oid->oid_name = "children"; 1122 oid->oid_handler = sysctl_handle_children; 1123 oid->oid_fmt = "A"; 1124 sysctl_register_oid(oid); 1125 1126 oid = &dev->oid[3]; 1127 bzero(oid, sizeof(*oid)); 1128 oid->oid_parent = &dev->oidlist[0]; 1129 oid->oid_number = OID_AUTO; 1130 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD; 1131 oid->oid_arg1 = dev; 1132 oid->oid_arg2 = 0; 1133 oid->oid_name = "state"; 1134 oid->oid_handler = sysctl_handle_state; 1135 oid->oid_fmt = "A"; 1136 sysctl_register_oid(oid); 1137} 1138 1139static void 1140device_unregister_oids(device_t dev) 1141{ 1142 sysctl_unregister_oid(&dev->oid[0]); 1143 sysctl_unregister_oid(&dev->oid[1]); 1144 sysctl_unregister_oid(&dev->oid[2]); 1145} 1146 1147#endif 1148 1149/* 1150 * Access functions for device resources. 1151 */ 1152extern struct config_device devtab[]; 1153extern int devtab_count; 1154 1155static int 1156resource_match_string(int i, char *resname, char *value) 1157{ 1158 int j; 1159 struct config_resource *res; 1160 1161 for (j = 0, res = devtab[i].resources; 1162 j < devtab[i].resource_count; j++, res++) 1163 if (!strcmp(res->name, resname) 1164 && res->type == RES_STRING 1165 && !strcmp(res->u.stringval, value)) 1166 return TRUE; 1167 return FALSE; 1168} 1169 1170static int 1171resource_find(const char *name, int unit, char *resname, 1172 struct config_resource **result) 1173{ 1174 int i, j; 1175 struct config_resource *res; 1176 1177 /* 1178 * First check specific instances, then generic. 1179 */ 1180 for (i = 0; i < devtab_count; i++) { 1181 if (devtab[i].unit < 0) 1182 continue; 1183 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1184 res = devtab[i].resources; 1185 for (j = 0; j < devtab[i].resource_count; j++, res++) 1186 if (!strcmp(res->name, resname)) { 1187 *result = res; 1188 return 0; 1189 } 1190 } 1191 } 1192 for (i = 0; i < devtab_count; i++) { 1193 if (devtab[i].unit >= 0) 1194 continue; 1195 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1196 res = devtab[i].resources; 1197 for (j = 0; j < devtab[i].resource_count; j++, res++) 1198 if (!strcmp(res->name, resname)) { 1199 *result = res; 1200 return 0; 1201 } 1202 } 1203 } 1204 return ENOENT; 1205} 1206 1207int 1208resource_int_value(const char *name, int unit, char *resname, int *result) 1209{ 1210 int error; 1211 struct config_resource *res; 1212 if ((error = resource_find(name, unit, resname, &res)) != 0) 1213 return error; 1214 if (res->type != RES_INT) 1215 return EFTYPE; 1216 *result = res->u.intval; 1217 return 0; 1218} 1219 1220int 1221resource_long_value(const char *name, int unit, char *resname, long *result) 1222{ 1223 int error; 1224 struct config_resource *res; 1225 if ((error = resource_find(name, unit, resname, &res)) != 0) 1226 return error; 1227 if (res->type != RES_LONG) 1228 return EFTYPE; 1229 *result = res->u.longval; 1230 return 0; 1231} 1232 1233int 1234resource_string_value(const char *name, int unit, char *resname, char **result) 1235{ 1236 int error; 1237 struct config_resource *res; 1238 if ((error = resource_find(name, unit, resname, &res)) != 0) 1239 return error; 1240 if (res->type != RES_STRING) 1241 return EFTYPE; 1242 *result = res->u.stringval; 1243 return 0; 1244} 1245 1246int 1247resource_query_string(int i, char *resname, char *value) 1248{ 1249 if (i < 0) 1250 i = 0; 1251 else 1252 i = i + 1; 1253 for (; i < devtab_count; i++) 1254 if (resource_match_string(i, resname, value)) 1255 return i; 1256 return -1; 1257} 1258 1259char * 1260resource_query_name(int i) 1261{ 1262 return devtab[i].name; 1263} 1264 1265int 1266resource_query_unit(int i) 1267{ 1268 return devtab[i].unit; 1269} 1270 1271 1272/* 1273 * Some useful method implementations to make life easier for bus drivers. 1274 */ 1275int 1276bus_generic_attach(device_t dev) 1277{ 1278 device_t child; 1279 1280 for (child = TAILQ_FIRST(&dev->children); 1281 child; child = TAILQ_NEXT(child, link)) 1282 device_probe_and_attach(child); 1283 1284 return 0; 1285} 1286 1287int 1288bus_generic_detach(device_t dev) 1289{ 1290 device_t child; 1291 int error; 1292 1293 if (dev->state != DS_ATTACHED) 1294 return EBUSY; 1295 1296 for (child = TAILQ_FIRST(&dev->children); 1297 child; child = TAILQ_NEXT(child, link)) 1298 if ((error = device_detach(child)) != 0) 1299 return error; 1300 1301 return 0; 1302} 1303 1304int 1305bus_generic_shutdown(device_t dev) 1306{ 1307 device_t child; 1308 1309 for (child = TAILQ_FIRST(&dev->children); 1310 child; child = TAILQ_NEXT(child, link)) 1311 DEVICE_SHUTDOWN(child); 1312 1313 return 0; 1314} 1315 1316int 1317bus_generic_suspend(device_t dev) 1318{ 1319 int error; 1320 device_t child, child2; 1321 1322 for (child = TAILQ_FIRST(&dev->children); 1323 child; child = TAILQ_NEXT(child, link)) { 1324 error = DEVICE_SUSPEND(child); 1325 if (error) { 1326 for (child2 = TAILQ_FIRST(&dev->children); 1327 child2 && child2 != child; 1328 child2 = TAILQ_NEXT(child2, link)) 1329 DEVICE_RESUME(child2); 1330 return (error); 1331 } 1332 } 1333 return 0; 1334} 1335 1336int 1337bus_generic_resume(device_t dev) 1338{ 1339 device_t child; 1340 1341 for (child = TAILQ_FIRST(&dev->children); 1342 child; child = TAILQ_NEXT(child, link)) { 1343 DEVICE_RESUME(child); 1344 /* if resume fails, there's nothing we can usefully do... */ 1345 } 1346 return 0; 1347} 1348 1349void 1350bus_generic_print_child(device_t dev, device_t child) 1351{ 1352 printf(" on %s%d", device_get_name(dev), device_get_unit(dev)); 1353} 1354 1355int 1356bus_generic_read_ivar(device_t dev, device_t child, int index, 1357 uintptr_t * result) 1358{ 1359 return ENOENT; 1360} 1361 1362int 1363bus_generic_write_ivar(device_t dev, device_t child, int index, 1364 uintptr_t value) 1365{ 1366 return ENOENT; 1367} 1368 1369int 1370bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 1371 driver_intr_t *intr, void *arg, void **cookiep) 1372{ 1373 /* Propagate up the bus hierarchy until someone handles it. */ 1374 if (dev->parent) 1375 return (BUS_SETUP_INTR(dev->parent, child, irq, intr, arg, 1376 cookiep)); 1377 else 1378 return (EINVAL); 1379} 1380 1381int 1382bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 1383 void *cookie) 1384{ 1385 /* Propagate up the bus hierarchy until someone handles it. */ 1386 if (dev->parent) 1387 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 1388 else 1389 return (EINVAL); 1390} 1391 1392struct resource * 1393bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 1394 u_long start, u_long end, u_long count, u_int flags) 1395{ 1396 /* Propagate up the bus hierarchy until someone handles it. */ 1397 if (dev->parent) 1398 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 1399 start, end, count, flags)); 1400 else 1401 return (NULL); 1402} 1403 1404int 1405bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 1406 struct resource *r) 1407{ 1408 /* Propagate up the bus hierarchy until someone handles it. */ 1409 if (dev->parent) 1410 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 1411 r)); 1412 else 1413 return (EINVAL); 1414} 1415 1416int 1417bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 1418 struct resource *r) 1419{ 1420 /* Propagate up the bus hierarchy until someone handles it. */ 1421 if (dev->parent) 1422 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 1423 r)); 1424 else 1425 return (EINVAL); 1426} 1427 1428int 1429bus_generic_deactivate_resource(device_t dev, device_t child, int type, 1430 int rid, struct resource *r) 1431{ 1432 /* Propagate up the bus hierarchy until someone handles it. */ 1433 if (dev->parent) 1434 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 1435 r)); 1436 else 1437 return (EINVAL); 1438} 1439 1440/* 1441 * Some convenience functions to make it easier for drivers to use the 1442 * resource-management functions. All these really do is hide the 1443 * indirection through the parent's method table, making for slightly 1444 * less-wordy code. In the future, it might make sense for this code 1445 * to maintain some sort of a list of resources allocated by each device. 1446 */ 1447struct resource * 1448bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 1449 u_long count, u_int flags) 1450{ 1451 if (dev->parent == 0) 1452 return (0); 1453 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 1454 count, flags)); 1455} 1456 1457int 1458bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 1459{ 1460 if (dev->parent == 0) 1461 return (EINVAL); 1462 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 1463} 1464 1465int 1466bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 1467{ 1468 if (dev->parent == 0) 1469 return (EINVAL); 1470 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 1471} 1472 1473int 1474bus_release_resource(device_t dev, int type, int rid, struct resource *r) 1475{ 1476 if (dev->parent == 0) 1477 return (EINVAL); 1478 return (BUS_RELEASE_RESOURCE(dev->parent, dev, 1479 type, rid, r)); 1480} 1481 1482int 1483bus_setup_intr(device_t dev, struct resource *r, 1484 driver_intr_t handler, void *arg, void **cookiep) 1485{ 1486 if (dev->parent == 0) 1487 return (EINVAL); 1488 return (BUS_SETUP_INTR(dev->parent, dev, r, handler, arg, cookiep)); 1489} 1490 1491int 1492bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 1493{ 1494 if (dev->parent == 0) 1495 return (EINVAL); 1496 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 1497} 1498 1499static void 1500root_print_child(device_t dev, device_t child) 1501{ 1502} 1503 1504static int 1505root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 1506 void **cookiep) 1507{ 1508 /* 1509 * If an interrupt mapping gets to here something bad has happened. 1510 */ 1511 panic("root_setup_intr"); 1512} 1513 1514static device_method_t root_methods[] = { 1515 /* Device interface */ 1516 DEVMETHOD(device_suspend, bus_generic_suspend), 1517 DEVMETHOD(device_resume, bus_generic_resume), 1518 1519 /* Bus interface */ 1520 DEVMETHOD(bus_print_child, root_print_child), 1521 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar), 1522 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar), 1523 DEVMETHOD(bus_setup_intr, root_setup_intr), 1524 1525 { 0, 0 } 1526}; 1527 1528static driver_t root_driver = { 1529 "root", 1530 root_methods, 1531 DRIVER_TYPE_MISC, 1532 1, /* no softc */ 1533}; 1534 1535device_t root_bus; 1536devclass_t root_devclass; 1537 1538static int 1539root_bus_module_handler(module_t mod, int what, void* arg) 1540{ 1541 switch (what) { 1542 case MOD_LOAD: 1543 compile_methods(&root_driver); 1544 root_bus = make_device(NULL, "root", 0, NULL); 1545 root_bus->desc = "System root bus"; 1546 root_bus->ops = root_driver.ops; 1547 root_bus->driver = &root_driver; 1548 root_bus->state = DS_ATTACHED; 1549 root_devclass = devclass_find_internal("root", FALSE); 1550 return 0; 1551 } 1552 1553 return 0; 1554} 1555 1556static moduledata_t root_bus_mod = { 1557 "rootbus", 1558 root_bus_module_handler, 1559 0 1560}; 1561DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1562 1563void 1564root_bus_configure(void) 1565{ 1566 device_t dev; 1567 1568 PDEBUG((".")); 1569 1570 for (dev = TAILQ_FIRST(&root_bus->children); dev; 1571 dev = TAILQ_NEXT(dev, link)) { 1572 device_probe_and_attach(dev); 1573 } 1574} 1575 1576int 1577driver_module_handler(module_t mod, int what, void *arg) 1578{ 1579 int error, i; 1580 struct driver_module_data *dmd; 1581 devclass_t bus_devclass; 1582 1583 dmd = (struct driver_module_data *)arg; 1584 bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE); 1585 error = 0; 1586 1587 switch (what) { 1588 case MOD_LOAD: 1589 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) { 1590 PDEBUG(("Loading module: driver %s on bus %s", 1591 DRIVERNAME(dmd->dmd_drivers[i]), 1592 dmd->dmd_busname)); 1593 error = devclass_add_driver(bus_devclass, 1594 dmd->dmd_drivers[i]); 1595 } 1596 if (error) 1597 break; 1598 1599 /* 1600 * The drivers loaded in this way are assumed to all 1601 * implement the same devclass. 1602 */ 1603 *dmd->dmd_devclass = 1604 devclass_find_internal(dmd->dmd_drivers[0]->name, 1605 TRUE); 1606 break; 1607 1608 case MOD_UNLOAD: 1609 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) { 1610 PDEBUG(("Unloading module: driver %s from bus %s", 1611 DRIVERNAME(dmd->dmd_drivers[i]), 1612 dmd->dmd_busname)); 1613 error = devclass_delete_driver(bus_devclass, 1614 dmd->dmd_drivers[i]); 1615 } 1616 break; 1617 } 1618 1619 if (!error && dmd->dmd_chainevh) 1620 error = dmd->dmd_chainevh(mod, what, dmd->dmd_chainarg); 1621 return (error); 1622} 1623 1624#ifdef BUS_DEBUG 1625 1626/* the _short versions avoid iteration by not calling anything that prints 1627 * more than oneliners. I love oneliners. 1628 */ 1629 1630static void 1631print_method_list(device_method_t *m, int indent) 1632{ 1633 int i; 1634 1635 if (!m) 1636 return; 1637 1638 for (i = 0; m->desc; i++, m++) 1639 indentprintf(("method %d: %s, offset=%d\n", 1640 i, m->desc->name, m->desc->offset)); 1641} 1642 1643static void 1644print_device_ops(device_ops_t ops, int indent) 1645{ 1646 int i; 1647 int count = 0; 1648 1649 if (!ops) 1650 return; 1651 1652 /* we present a list of the methods that are pointing to the 1653 * error_method, but ignore the 0'th elements; it is always 1654 * error_method. 1655 */ 1656 for (i = 1; i < ops->maxoffset; i++) { 1657 if (ops->methods[i] == error_method) { 1658 if (count == 0) 1659 indentprintf(("error_method:")); 1660 printf(" %d", i); 1661 count++; 1662 } 1663 } 1664 if (count) 1665 printf("\n"); 1666 1667 indentprintf(("(%d method%s, %d valid, %d error_method%s)\n", 1668 ops->maxoffset-1, (ops->maxoffset-1 == 1? "":"s"), 1669 ops->maxoffset-1-count, 1670 count, (count == 1? "":"'s"))); 1671} 1672 1673static void 1674print_device_short(device_t dev, int indent) 1675{ 1676 if (!dev) 1677 return; 1678 1679 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%sivars,%ssoftc,busy=%d\n", 1680 dev->unit, dev->desc, 1681 (dev->parent? "":"no "), 1682 (TAILQ_EMPTY(&dev->children)? "no ":""), 1683 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 1684 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 1685 (dev->flags&DF_WILDCARD? "wildcard,":""), 1686 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 1687 (dev->ivars? "":"no "), 1688 (dev->softc? "":"no "), 1689 dev->busy)); 1690} 1691 1692static void 1693print_device(device_t dev, int indent) 1694{ 1695 if (!dev) 1696 return; 1697 1698 print_device_short(dev, indent); 1699 1700 indentprintf(("Parent:\n")); 1701 print_device_short(dev->parent, indent+1); 1702 indentprintf(("Methods:\n")); 1703 print_device_ops(dev->ops, indent+1); 1704 indentprintf(("Driver:\n")); 1705 print_driver_short(dev->driver, indent+1); 1706 indentprintf(("Devclass:\n")); 1707 print_devclass_short(dev->devclass, indent+1); 1708} 1709 1710void 1711print_device_tree_short(device_t dev, int indent) 1712/* print the device and all its children (indented) */ 1713{ 1714 device_t child; 1715 1716 if (!dev) 1717 return; 1718 1719 print_device_short(dev, indent); 1720 1721 for (child = TAILQ_FIRST(&dev->children); child; 1722 child = TAILQ_NEXT(child, link)) 1723 print_device_tree_short(child, indent+1); 1724} 1725 1726void 1727print_device_tree(device_t dev, int indent) 1728/* print the device and all its children (indented) */ 1729{ 1730 device_t child; 1731 1732 if (!dev) 1733 return; 1734 1735 print_device(dev, indent); 1736 1737 for (child = TAILQ_FIRST(&dev->children); child; 1738 child = TAILQ_NEXT(child, link)) 1739 print_device_tree(child, indent+1); 1740} 1741 1742static void 1743print_driver_short(driver_t *driver, int indent) 1744{ 1745 if (!driver) 1746 return; 1747 1748 indentprintf(("driver %s: type = %s%s%s%s, softc size = %d\n", 1749 driver->name, 1750 /* yes, I know this looks silly, but going to bed at 1751 * two o'clock and having to get up at 7:30 again is silly 1752 * as well. As is sticking your head in a bucket of water. 1753 */ 1754 (driver->type == DRIVER_TYPE_TTY? "tty":""), 1755 (driver->type == DRIVER_TYPE_BIO? "bio":""), 1756 (driver->type == DRIVER_TYPE_NET? "net":""), 1757 (driver->type == DRIVER_TYPE_MISC? "misc":""), 1758 driver->softc)); 1759} 1760 1761static void 1762print_driver(driver_t *driver, int indent) 1763{ 1764 if (!driver) 1765 return; 1766 1767 print_driver_short(driver, indent); 1768 indentprintf(("Methods:\n")); 1769 print_method_list(driver->methods, indent+1); 1770 indentprintf(("Operations:\n")); 1771 print_device_ops(driver->ops, indent+1); 1772} 1773 1774 1775static void 1776print_driver_list(driver_list_t drivers, int indent) 1777{ 1778 driver_t *driver; 1779 1780 for (driver = TAILQ_FIRST(&drivers); driver; 1781 driver = TAILQ_NEXT(driver, link)) 1782 print_driver(driver, indent); 1783} 1784 1785static void 1786print_devclass_short(devclass_t dc, int indent) 1787{ 1788 if ( !dc ) 1789 return; 1790 1791 indentprintf(("devclass %s: max units = %d, next unit = %d\n", 1792 dc->name, dc->maxunit, dc->nextunit)); 1793} 1794 1795static void 1796print_devclass(devclass_t dc, int indent) 1797{ 1798 int i; 1799 1800 if ( !dc ) 1801 return; 1802 1803 print_devclass_short(dc, indent); 1804 indentprintf(("Drivers:\n")); 1805 print_driver_list(dc->drivers, indent+1); 1806 1807 indentprintf(("Devices:\n")); 1808 for (i = 0; i < dc->maxunit; i++) 1809 if (dc->devices[i]) 1810 print_device(dc->devices[i], indent+1); 1811} 1812 1813void 1814print_devclass_list_short(void) 1815{ 1816 devclass_t dc; 1817 1818 printf("Short listing of devclasses, drivers & devices:\n"); 1819 for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link)) 1820 print_devclass_short(dc, 0); 1821} 1822 1823void 1824print_devclass_list(void) 1825{ 1826 devclass_t dc; 1827 1828 printf("Full listing of devclasses, drivers & devices:\n"); 1829 for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link)) 1830 print_devclass(dc, 0); 1831} 1832 1833#endif 1834