subr_bus.c revision 85887
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: head/sys/kern/subr_bus.c 85887 2001-11-02 17:33:06Z imp $ 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#include <sys/kobj.h> 37#include <sys/bus_private.h> 38#include <sys/sysctl.h> 39#include <sys/systm.h> 40#include <machine/bus.h> 41#include <sys/rman.h> 42#include <machine/stdarg.h> /* for device_printf() */ 43 44static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 45 46#ifdef BUS_DEBUG 47 48static int bus_debug = 1; 49SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0, 50 "Debug bus code"); 51 52#define PDEBUG(a) if (bus_debug) {printf(__FUNCTION__ ":%d: ", __LINE__), printf a, printf("\n");} 53#define DEVICENAME(d) ((d)? device_get_name(d): "no device") 54#define DRIVERNAME(d) ((d)? d->name : "no driver") 55#define DEVCLANAME(d) ((d)? d->name : "no devclass") 56 57/* Produce the indenting, indent*2 spaces plus a '.' ahead of that to 58 * prevent syslog from deleting initial spaces 59 */ 60#define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0) 61 62static void print_device_short(device_t dev, int indent); 63static void print_device(device_t dev, int indent); 64void print_device_tree_short(device_t dev, int indent); 65void print_device_tree(device_t dev, int indent); 66static void print_driver_short(driver_t *driver, int indent); 67static void print_driver(driver_t *driver, int indent); 68static void print_driver_list(driver_list_t drivers, int indent); 69static void print_devclass_short(devclass_t dc, int indent); 70static void print_devclass(devclass_t dc, int indent); 71void print_devclass_list_short(void); 72void print_devclass_list(void); 73 74#else 75/* Make the compiler ignore the function calls */ 76#define PDEBUG(a) /* nop */ 77#define DEVICENAME(d) /* nop */ 78#define DRIVERNAME(d) /* nop */ 79#define DEVCLANAME(d) /* nop */ 80 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 94TAILQ_HEAD(,device) bus_data_devices; 95static int bus_data_generation = 1; 96 97kobj_method_t null_methods[] = { 98 { 0, 0 } 99}; 100 101DEFINE_CLASS(null, null_methods, 0); 102 103/* 104 * Devclass implementation 105 */ 106 107static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 108 109static devclass_t 110devclass_find_internal(const char *classname, int create) 111{ 112 devclass_t dc; 113 114 PDEBUG(("looking for %s", classname)); 115 if (!classname) 116 return (NULL); 117 118 TAILQ_FOREACH(dc, &devclasses, link) { 119 if (!strcmp(dc->name, classname)) 120 return (dc); 121 } 122 123 PDEBUG(("%s not found%s", classname, (create? ", creating": ""))); 124 if (create) { 125 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 126 M_BUS, M_NOWAIT|M_ZERO); 127 if (!dc) 128 return (NULL); 129 dc->name = (char*) (dc + 1); 130 strcpy(dc->name, classname); 131 dc->devices = NULL; 132 dc->maxunit = 0; 133 TAILQ_INIT(&dc->drivers); 134 TAILQ_INSERT_TAIL(&devclasses, dc, link); 135 136 bus_data_generation_update(); 137 } 138 139 return (dc); 140} 141 142devclass_t 143devclass_create(const char *classname) 144{ 145 return (devclass_find_internal(classname, TRUE)); 146} 147 148devclass_t 149devclass_find(const char *classname) 150{ 151 return (devclass_find_internal(classname, FALSE)); 152} 153 154int 155devclass_add_driver(devclass_t dc, driver_t *driver) 156{ 157 driverlink_t dl; 158 int i; 159 160 PDEBUG(("%s", DRIVERNAME(driver))); 161 162 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO); 163 if (!dl) 164 return (ENOMEM); 165 166 /* 167 * Compile the driver's methods. Also increase the reference count 168 * so that the class doesn't get freed when the last instance 169 * goes. This means we can safely use static methods and avoids a 170 * double-free in devclass_delete_driver. 171 */ 172 kobj_class_compile((kobj_class_t) driver); 173 174 /* 175 * Make sure the devclass which the driver is implementing exists. 176 */ 177 devclass_find_internal(driver->name, TRUE); 178 179 dl->driver = driver; 180 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 181 driver->refs++; 182 183 /* 184 * Call BUS_DRIVER_ADDED for any existing busses in this class. 185 */ 186 for (i = 0; i < dc->maxunit; i++) 187 if (dc->devices[i]) 188 BUS_DRIVER_ADDED(dc->devices[i], driver); 189 190 bus_data_generation_update(); 191 return (0); 192} 193 194int 195devclass_delete_driver(devclass_t busclass, driver_t *driver) 196{ 197 devclass_t dc = devclass_find(driver->name); 198 driverlink_t dl; 199 device_t dev; 200 int i; 201 int error; 202 203 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 204 205 if (!dc) 206 return (0); 207 208 /* 209 * Find the link structure in the bus' list of drivers. 210 */ 211 TAILQ_FOREACH(dl, &busclass->drivers, link) { 212 if (dl->driver == driver) 213 break; 214 } 215 216 if (!dl) { 217 PDEBUG(("%s not found in %s list", driver->name, 218 busclass->name)); 219 return (ENOENT); 220 } 221 222 /* 223 * Disassociate from any devices. We iterate through all the 224 * devices in the devclass of the driver and detach any which are 225 * using the driver and which have a parent in the devclass which 226 * we are deleting from. 227 * 228 * Note that since a driver can be in multiple devclasses, we 229 * should not detach devices which are not children of devices in 230 * the affected devclass. 231 */ 232 for (i = 0; i < dc->maxunit; i++) { 233 if (dc->devices[i]) { 234 dev = dc->devices[i]; 235 if (dev->driver == driver && dev->parent && 236 dev->parent->devclass == busclass) { 237 if ((error = device_detach(dev)) != 0) 238 return (error); 239 device_set_driver(dev, NULL); 240 } 241 } 242 } 243 244 TAILQ_REMOVE(&busclass->drivers, dl, link); 245 free(dl, M_BUS); 246 247 driver->refs--; 248 if (driver->refs == 0) 249 kobj_class_free((kobj_class_t) driver); 250 251 bus_data_generation_update(); 252 return (0); 253} 254 255static driverlink_t 256devclass_find_driver_internal(devclass_t dc, const char *classname) 257{ 258 driverlink_t dl; 259 260 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 261 262 TAILQ_FOREACH(dl, &dc->drivers, link) { 263 if (!strcmp(dl->driver->name, classname)) 264 return (dl); 265 } 266 267 PDEBUG(("not found")); 268 return (NULL); 269} 270 271driver_t * 272devclass_find_driver(devclass_t dc, const char *classname) 273{ 274 driverlink_t dl; 275 276 dl = devclass_find_driver_internal(dc, classname); 277 if (dl) 278 return (dl->driver); 279 return (NULL); 280} 281 282const char * 283devclass_get_name(devclass_t dc) 284{ 285 return (dc->name); 286} 287 288device_t 289devclass_get_device(devclass_t dc, int unit) 290{ 291 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 292 return (NULL); 293 return (dc->devices[unit]); 294} 295 296void * 297devclass_get_softc(devclass_t dc, int unit) 298{ 299 device_t dev; 300 301 dev = devclass_get_device(dc, unit); 302 if (!dev) 303 return (NULL); 304 305 return (device_get_softc(dev)); 306} 307 308int 309devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 310{ 311 int i; 312 int count; 313 device_t *list; 314 315 count = 0; 316 for (i = 0; i < dc->maxunit; i++) 317 if (dc->devices[i]) 318 count++; 319 320 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 321 if (!list) 322 return (ENOMEM); 323 324 count = 0; 325 for (i = 0; i < dc->maxunit; i++) { 326 if (dc->devices[i]) { 327 list[count] = dc->devices[i]; 328 count++; 329 } 330 } 331 332 *devlistp = list; 333 *devcountp = count; 334 335 return (0); 336} 337 338int 339devclass_get_maxunit(devclass_t dc) 340{ 341 return (dc->maxunit); 342} 343 344int 345devclass_find_free_unit(devclass_t dc, int unit) 346{ 347 if (dc == NULL) 348 return (unit); 349 while (unit < dc->maxunit && dc->devices[unit] != NULL) 350 unit++; 351 return (unit); 352} 353 354static int 355devclass_alloc_unit(devclass_t dc, int *unitp) 356{ 357 int unit = *unitp; 358 359 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 360 361 /* If we were given a wired unit number, check for existing device */ 362 /* XXX imp XXX */ 363 if (unit != -1) { 364 if (unit >= 0 && unit < dc->maxunit && 365 dc->devices[unit] != NULL) { 366 printf("%s-: %s%d already exists, skipping it\n", 367 dc->name, dc->name, *unitp); 368 return (EEXIST); 369 } 370 } else { 371 /* Unwired device, find the next available slot for it */ 372 unit = 0; 373 while (unit < dc->maxunit && dc->devices[unit] != NULL) 374 unit++; 375 } 376 377 /* 378 * We've selected a unit beyond the length of the table, so let's 379 * extend the table to make room for all units up to and including 380 * this one. 381 */ 382 if (unit >= dc->maxunit) { 383 device_t *newlist; 384 int newsize; 385 386 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 387 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 388 if (!newlist) 389 return (ENOMEM); 390 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 391 bzero(newlist + dc->maxunit, 392 sizeof(device_t) * (newsize - dc->maxunit)); 393 if (dc->devices) 394 free(dc->devices, M_BUS); 395 dc->devices = newlist; 396 dc->maxunit = newsize; 397 } 398 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 399 400 *unitp = unit; 401 return (0); 402} 403 404static int 405devclass_add_device(devclass_t dc, device_t dev) 406{ 407 int buflen, error; 408 409 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 410 411 buflen = strlen(dc->name) + 5; 412 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 413 if (!dev->nameunit) 414 return (ENOMEM); 415 416 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { 417 free(dev->nameunit, M_BUS); 418 dev->nameunit = NULL; 419 return (error); 420 } 421 dc->devices[dev->unit] = dev; 422 dev->devclass = dc; 423 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 424 425 return (0); 426} 427 428static int 429devclass_delete_device(devclass_t dc, device_t dev) 430{ 431 if (!dc || !dev) 432 return (0); 433 434 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 435 436 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 437 panic("devclass_delete_device: inconsistent device class"); 438 dc->devices[dev->unit] = NULL; 439 if (dev->flags & DF_WILDCARD) 440 dev->unit = -1; 441 dev->devclass = NULL; 442 free(dev->nameunit, M_BUS); 443 dev->nameunit = NULL; 444 445 return (0); 446} 447 448static device_t 449make_device(device_t parent, const char *name, int unit) 450{ 451 device_t dev; 452 devclass_t dc; 453 454 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 455 456 if (name) { 457 dc = devclass_find_internal(name, TRUE); 458 if (!dc) { 459 printf("make_device: can't find device class %s\n", 460 name); 461 return (NULL); 462 } 463 } else { 464 dc = NULL; 465 } 466 467 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO); 468 if (!dev) 469 return (NULL); 470 471 dev->parent = parent; 472 TAILQ_INIT(&dev->children); 473 kobj_init((kobj_t) dev, &null_class); 474 dev->driver = NULL; 475 dev->devclass = NULL; 476 dev->unit = unit; 477 dev->nameunit = NULL; 478 dev->desc = NULL; 479 dev->busy = 0; 480 dev->devflags = 0; 481 dev->flags = DF_ENABLED; 482 dev->order = 0; 483 if (unit == -1) 484 dev->flags |= DF_WILDCARD; 485 if (name) { 486 dev->flags |= DF_FIXEDCLASS; 487 if (devclass_add_device(dc, dev)) { 488 kobj_delete((kobj_t) dev, M_BUS); 489 return (NULL); 490 } 491 } 492 dev->ivars = NULL; 493 dev->softc = NULL; 494 495 dev->state = DS_NOTPRESENT; 496 497 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 498 bus_data_generation_update(); 499 500 return (dev); 501} 502 503static int 504device_print_child(device_t dev, device_t child) 505{ 506 int retval = 0; 507 508 if (device_is_alive(child)) 509 retval += BUS_PRINT_CHILD(dev, child); 510 else 511 retval += device_printf(child, " not found\n"); 512 513 return (retval); 514} 515 516device_t 517device_add_child(device_t dev, const char *name, int unit) 518{ 519 return (device_add_child_ordered(dev, 0, name, unit)); 520} 521 522device_t 523device_add_child_ordered(device_t dev, int order, const char *name, int unit) 524{ 525 device_t child; 526 device_t place; 527 528 PDEBUG(("%s at %s with order %d as unit %d", 529 name, DEVICENAME(dev), order, unit)); 530 531 child = make_device(dev, name, unit); 532 if (child == NULL) 533 return (child); 534 child->order = order; 535 536 TAILQ_FOREACH(place, &dev->children, link) { 537 if (place->order > order) 538 break; 539 } 540 541 if (place) { 542 /* 543 * The device 'place' is the first device whose order is 544 * greater than the new child. 545 */ 546 TAILQ_INSERT_BEFORE(place, child, link); 547 } else { 548 /* 549 * The new child's order is greater or equal to the order of 550 * any existing device. Add the child to the tail of the list. 551 */ 552 TAILQ_INSERT_TAIL(&dev->children, child, link); 553 } 554 555 bus_data_generation_update(); 556 return (child); 557} 558 559int 560device_delete_child(device_t dev, device_t child) 561{ 562 int error; 563 device_t grandchild; 564 565 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 566 567 /* remove children first */ 568 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 569 error = device_delete_child(child, grandchild); 570 if (error) 571 return (error); 572 } 573 574 if ((error = device_detach(child)) != 0) 575 return (error); 576 if (child->devclass) 577 devclass_delete_device(child->devclass, child); 578 TAILQ_REMOVE(&dev->children, child, link); 579 TAILQ_REMOVE(&bus_data_devices, child, devlink); 580 device_set_desc(child, NULL); 581 free(child, M_BUS); 582 583 bus_data_generation_update(); 584 return (0); 585} 586 587/* 588 * Find only devices attached to this bus. 589 */ 590device_t 591device_find_child(device_t dev, const char *classname, int unit) 592{ 593 devclass_t dc; 594 device_t child; 595 596 dc = devclass_find(classname); 597 if (!dc) 598 return (NULL); 599 600 child = devclass_get_device(dc, unit); 601 if (child && child->parent == dev) 602 return (child); 603 return (NULL); 604} 605 606static driverlink_t 607first_matching_driver(devclass_t dc, device_t dev) 608{ 609 if (dev->devclass) 610 return (devclass_find_driver_internal(dc, dev->devclass->name)); 611 return (TAILQ_FIRST(&dc->drivers)); 612} 613 614static driverlink_t 615next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 616{ 617 if (dev->devclass) { 618 driverlink_t dl; 619 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 620 if (!strcmp(dev->devclass->name, dl->driver->name)) 621 return (dl); 622 return (NULL); 623 } 624 return (TAILQ_NEXT(last, link)); 625} 626 627static int 628device_probe_child(device_t dev, device_t child) 629{ 630 devclass_t dc; 631 driverlink_t best = 0; 632 driverlink_t dl; 633 int result, pri = 0; 634 int hasclass = (child->devclass != 0); 635 636 dc = dev->devclass; 637 if (!dc) 638 panic("device_probe_child: parent device has no devclass"); 639 640 if (child->state == DS_ALIVE) 641 return (0); 642 643 for (dl = first_matching_driver(dc, child); 644 dl; 645 dl = next_matching_driver(dc, child, dl)) { 646 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 647 device_set_driver(child, dl->driver); 648 if (!hasclass) 649 device_set_devclass(child, dl->driver->name); 650 result = DEVICE_PROBE(child); 651 if (!hasclass) 652 device_set_devclass(child, 0); 653 654 /* 655 * If the driver returns SUCCESS, there can be no higher match 656 * for this device. 657 */ 658 if (result == 0) { 659 best = dl; 660 pri = 0; 661 break; 662 } 663 664 /* 665 * The driver returned an error so it certainly doesn't match. 666 */ 667 if (result > 0) { 668 device_set_driver(child, 0); 669 continue; 670 } 671 672 /* 673 * A priority lower than SUCCESS, remember the best matching 674 * driver. Initialise the value of pri for the first match. 675 */ 676 if (best == 0 || result > pri) { 677 best = dl; 678 pri = result; 679 continue; 680 } 681 } 682 683 /* 684 * If we found a driver, change state and initialise the devclass. 685 */ 686 if (best) { 687 if (!child->devclass) 688 device_set_devclass(child, best->driver->name); 689 device_set_driver(child, best->driver); 690 if (pri < 0) { 691 /* 692 * A bit bogus. Call the probe method again to make 693 * sure that we have the right description. 694 */ 695 DEVICE_PROBE(child); 696 } 697 child->state = DS_ALIVE; 698 699 bus_data_generation_update(); 700 return (0); 701 } 702 703 return (ENXIO); 704} 705 706device_t 707device_get_parent(device_t dev) 708{ 709 return (dev->parent); 710} 711 712int 713device_get_children(device_t dev, device_t **devlistp, int *devcountp) 714{ 715 int count; 716 device_t child; 717 device_t *list; 718 719 count = 0; 720 TAILQ_FOREACH(child, &dev->children, link) { 721 count++; 722 } 723 724 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 725 if (!list) 726 return (ENOMEM); 727 728 count = 0; 729 TAILQ_FOREACH(child, &dev->children, link) { 730 list[count] = child; 731 count++; 732 } 733 734 *devlistp = list; 735 *devcountp = count; 736 737 return (0); 738} 739 740driver_t * 741device_get_driver(device_t dev) 742{ 743 return (dev->driver); 744} 745 746devclass_t 747device_get_devclass(device_t dev) 748{ 749 return (dev->devclass); 750} 751 752const char * 753device_get_name(device_t dev) 754{ 755 if (dev->devclass) 756 return (devclass_get_name(dev->devclass)); 757 return (NULL); 758} 759 760const char * 761device_get_nameunit(device_t dev) 762{ 763 return (dev->nameunit); 764} 765 766int 767device_get_unit(device_t dev) 768{ 769 return (dev->unit); 770} 771 772const char * 773device_get_desc(device_t dev) 774{ 775 return (dev->desc); 776} 777 778u_int32_t 779device_get_flags(device_t dev) 780{ 781 return (dev->devflags); 782} 783 784int 785device_print_prettyname(device_t dev) 786{ 787 const char *name = device_get_name(dev); 788 789 if (name == 0) 790 return (printf("unknown: ")); 791 return (printf("%s%d: ", name, device_get_unit(dev))); 792} 793 794int 795device_printf(device_t dev, const char * fmt, ...) 796{ 797 va_list ap; 798 int retval; 799 800 retval = device_print_prettyname(dev); 801 va_start(ap, fmt); 802 retval += vprintf(fmt, ap); 803 va_end(ap); 804 return (retval); 805} 806 807static void 808device_set_desc_internal(device_t dev, const char* desc, int copy) 809{ 810 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 811 free(dev->desc, M_BUS); 812 dev->flags &= ~DF_DESCMALLOCED; 813 dev->desc = NULL; 814 } 815 816 if (copy && desc) { 817 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 818 if (dev->desc) { 819 strcpy(dev->desc, desc); 820 dev->flags |= DF_DESCMALLOCED; 821 } 822 } else { 823 /* Avoid a -Wcast-qual warning */ 824 dev->desc = (char *)(uintptr_t) desc; 825 } 826 827 bus_data_generation_update(); 828} 829 830void 831device_set_desc(device_t dev, const char* desc) 832{ 833 device_set_desc_internal(dev, desc, FALSE); 834} 835 836void 837device_set_desc_copy(device_t dev, const char* desc) 838{ 839 device_set_desc_internal(dev, desc, TRUE); 840} 841 842void 843device_set_flags(device_t dev, u_int32_t flags) 844{ 845 dev->devflags = flags; 846} 847 848void * 849device_get_softc(device_t dev) 850{ 851 return (dev->softc); 852} 853 854void 855device_set_softc(device_t dev, void *softc) 856{ 857 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 858 free(dev->softc, M_BUS); 859 dev->softc = softc; 860 if (dev->softc) 861 dev->flags |= DF_EXTERNALSOFTC; 862 else 863 dev->flags &= ~DF_EXTERNALSOFTC; 864} 865 866void * 867device_get_ivars(device_t dev) 868{ 869 return (dev->ivars); 870} 871 872void 873device_set_ivars(device_t dev, void * ivars) 874{ 875 if (!dev) 876 return; 877 878 dev->ivars = ivars; 879 880 return; 881} 882 883device_state_t 884device_get_state(device_t dev) 885{ 886 return (dev->state); 887} 888 889void 890device_enable(device_t dev) 891{ 892 dev->flags |= DF_ENABLED; 893} 894 895void 896device_disable(device_t dev) 897{ 898 dev->flags &= ~DF_ENABLED; 899} 900 901void 902device_busy(device_t dev) 903{ 904 if (dev->state < DS_ATTACHED) 905 panic("device_busy: called for unattached device"); 906 if (dev->busy == 0 && dev->parent) 907 device_busy(dev->parent); 908 dev->busy++; 909 dev->state = DS_BUSY; 910} 911 912void 913device_unbusy(device_t dev) 914{ 915 if (dev->state != DS_BUSY) 916 panic("device_unbusy: called for non-busy device"); 917 dev->busy--; 918 if (dev->busy == 0) { 919 if (dev->parent) 920 device_unbusy(dev->parent); 921 dev->state = DS_ATTACHED; 922 } 923} 924 925void 926device_quiet(device_t dev) 927{ 928 dev->flags |= DF_QUIET; 929} 930 931void 932device_verbose(device_t dev) 933{ 934 dev->flags &= ~DF_QUIET; 935} 936 937int 938device_is_quiet(device_t dev) 939{ 940 return ((dev->flags & DF_QUIET) != 0); 941} 942 943int 944device_is_enabled(device_t dev) 945{ 946 return ((dev->flags & DF_ENABLED) != 0); 947} 948 949int 950device_is_alive(device_t dev) 951{ 952 return (dev->state >= DS_ALIVE); 953} 954 955int 956device_set_devclass(device_t dev, const char *classname) 957{ 958 devclass_t dc; 959 int error; 960 961 if (!classname) { 962 if (dev->devclass) 963 devclass_delete_device(dev->devclass, dev); 964 return (0); 965 } 966 967 if (dev->devclass) { 968 printf("device_set_devclass: device class already set\n"); 969 return (EINVAL); 970 } 971 972 dc = devclass_find_internal(classname, TRUE); 973 if (!dc) 974 return (ENOMEM); 975 976 error = devclass_add_device(dc, dev); 977 978 bus_data_generation_update(); 979 return (error); 980} 981 982int 983device_set_driver(device_t dev, driver_t *driver) 984{ 985 if (dev->state >= DS_ATTACHED) 986 return (EBUSY); 987 988 if (dev->driver == driver) 989 return (0); 990 991 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 992 free(dev->softc, M_BUS); 993 dev->softc = NULL; 994 } 995 kobj_delete((kobj_t) dev, 0); 996 dev->driver = driver; 997 if (driver) { 998 kobj_init((kobj_t) dev, (kobj_class_t) driver); 999 if (!(dev->flags & DF_EXTERNALSOFTC)) { 1000 dev->softc = malloc(driver->size, M_BUS, 1001 M_NOWAIT | M_ZERO); 1002 if (!dev->softc) { 1003 kobj_init((kobj_t) dev, &null_class); 1004 dev->driver = NULL; 1005 return (ENOMEM); 1006 } 1007 } 1008 } else { 1009 kobj_init((kobj_t) dev, &null_class); 1010 } 1011 1012 bus_data_generation_update(); 1013 return (0); 1014} 1015 1016int 1017device_probe_and_attach(device_t dev) 1018{ 1019 device_t bus = dev->parent; 1020 int error = 0; 1021 int hasclass = (dev->devclass != 0); 1022 1023 if (dev->state >= DS_ALIVE) 1024 return (0); 1025 1026 if (dev->flags & DF_ENABLED) { 1027 error = device_probe_child(bus, dev); 1028 if (!error) { 1029 if (!device_is_quiet(dev)) 1030 device_print_child(bus, dev); 1031 error = DEVICE_ATTACH(dev); 1032 if (!error) 1033 dev->state = DS_ATTACHED; 1034 else { 1035 printf("device_probe_and_attach: %s%d attach returned %d\n", 1036 dev->driver->name, dev->unit, error); 1037 /* Unset the class; set in device_probe_child */ 1038 if (!hasclass) 1039 device_set_devclass(dev, 0); 1040 device_set_driver(dev, NULL); 1041 dev->state = DS_NOTPRESENT; 1042 } 1043 } else { 1044 if (!(dev->flags & DF_DONENOMATCH)) { 1045 BUS_PROBE_NOMATCH(bus, dev); 1046 dev->flags |= DF_DONENOMATCH; 1047 } 1048 } 1049 } else { 1050 if (bootverbose) { 1051 device_print_prettyname(dev); 1052 printf("not probed (disabled)\n"); 1053 } 1054 } 1055 1056 return (error); 1057} 1058 1059int 1060device_detach(device_t dev) 1061{ 1062 int error; 1063 1064 PDEBUG(("%s", DEVICENAME(dev))); 1065 if (dev->state == DS_BUSY) 1066 return (EBUSY); 1067 if (dev->state != DS_ATTACHED) 1068 return (0); 1069 1070 if ((error = DEVICE_DETACH(dev)) != 0) 1071 return (error); 1072 device_printf(dev, "detached\n"); 1073 if (dev->parent) 1074 BUS_CHILD_DETACHED(dev->parent, dev); 1075 1076 if (!(dev->flags & DF_FIXEDCLASS)) 1077 devclass_delete_device(dev->devclass, dev); 1078 1079 dev->state = DS_NOTPRESENT; 1080 device_set_driver(dev, NULL); 1081 1082 return (0); 1083} 1084 1085int 1086device_shutdown(device_t dev) 1087{ 1088 if (dev->state < DS_ATTACHED) 1089 return (0); 1090 return (DEVICE_SHUTDOWN(dev)); 1091} 1092 1093int 1094device_set_unit(device_t dev, int unit) 1095{ 1096 devclass_t dc; 1097 int err; 1098 1099 dc = device_get_devclass(dev); 1100 if (unit < dc->maxunit && dc->devices[unit]) 1101 return (EBUSY); 1102 err = devclass_delete_device(dc, dev); 1103 if (err) 1104 return (err); 1105 dev->unit = unit; 1106 err = devclass_add_device(dc, dev); 1107 if (err) 1108 return (err); 1109 1110 bus_data_generation_update(); 1111 return (0); 1112} 1113 1114/*======================================*/ 1115/* 1116 * Some useful method implementations to make life easier for bus drivers. 1117 */ 1118 1119void 1120resource_list_init(struct resource_list *rl) 1121{ 1122 SLIST_INIT(rl); 1123} 1124 1125void 1126resource_list_free(struct resource_list *rl) 1127{ 1128 struct resource_list_entry *rle; 1129 1130 while ((rle = SLIST_FIRST(rl)) != NULL) { 1131 if (rle->res) 1132 panic("resource_list_free: resource entry is busy"); 1133 SLIST_REMOVE_HEAD(rl, link); 1134 free(rle, M_BUS); 1135 } 1136} 1137 1138void 1139resource_list_add(struct resource_list *rl, int type, int rid, 1140 u_long start, u_long end, u_long count) 1141{ 1142 struct resource_list_entry *rle; 1143 1144 rle = resource_list_find(rl, type, rid); 1145 if (!rle) { 1146 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 1147 M_NOWAIT); 1148 if (!rle) 1149 panic("resource_list_add: can't record entry"); 1150 SLIST_INSERT_HEAD(rl, rle, link); 1151 rle->type = type; 1152 rle->rid = rid; 1153 rle->res = NULL; 1154 } 1155 1156 if (rle->res) 1157 panic("resource_list_add: resource entry is busy"); 1158 1159 rle->start = start; 1160 rle->end = end; 1161 rle->count = count; 1162} 1163 1164struct resource_list_entry * 1165resource_list_find(struct resource_list *rl, int type, int rid) 1166{ 1167 struct resource_list_entry *rle; 1168 1169 SLIST_FOREACH(rle, rl, link) { 1170 if (rle->type == type && rle->rid == rid) 1171 return (rle); 1172 } 1173 return (NULL); 1174} 1175 1176void 1177resource_list_delete(struct resource_list *rl, int type, int rid) 1178{ 1179 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 1180 1181 if (rle) { 1182 if (rle->res != NULL) 1183 panic("resource_list_delete: resource has not been released"); 1184 SLIST_REMOVE(rl, rle, resource_list_entry, link); 1185 free(rle, M_BUS); 1186 } 1187} 1188 1189struct resource * 1190resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 1191 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 1192{ 1193 struct resource_list_entry *rle = 0; 1194 int passthrough = (device_get_parent(child) != bus); 1195 int isdefault = (start == 0UL && end == ~0UL); 1196 1197 if (passthrough) { 1198 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1199 type, rid, start, end, count, flags)); 1200 } 1201 1202 rle = resource_list_find(rl, type, *rid); 1203 1204 if (!rle) 1205 return (NULL); /* no resource of that type/rid */ 1206 1207 if (rle->res) 1208 panic("resource_list_alloc: resource entry is busy"); 1209 1210 if (isdefault) { 1211 start = rle->start; 1212 count = max(count, rle->count); 1213 end = max(rle->end, start + count - 1); 1214 } 1215 1216 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1217 type, rid, start, end, count, flags); 1218 1219 /* 1220 * Record the new range. 1221 */ 1222 if (rle->res) { 1223 rle->start = rman_get_start(rle->res); 1224 rle->end = rman_get_end(rle->res); 1225 rle->count = count; 1226 } 1227 1228 return (rle->res); 1229} 1230 1231int 1232resource_list_release(struct resource_list *rl, device_t bus, device_t child, 1233 int type, int rid, struct resource *res) 1234{ 1235 struct resource_list_entry *rle = 0; 1236 int passthrough = (device_get_parent(child) != bus); 1237 int error; 1238 1239 if (passthrough) { 1240 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1241 type, rid, res)); 1242 } 1243 1244 rle = resource_list_find(rl, type, rid); 1245 1246 if (!rle) 1247 panic("resource_list_release: can't find resource"); 1248 if (!rle->res) 1249 panic("resource_list_release: resource entry is not busy"); 1250 1251 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1252 type, rid, res); 1253 if (error) 1254 return (error); 1255 1256 rle->res = NULL; 1257 return (0); 1258} 1259 1260/* 1261 * Call DEVICE_IDENTIFY for each driver. 1262 */ 1263int 1264bus_generic_probe(device_t dev) 1265{ 1266 devclass_t dc = dev->devclass; 1267 driverlink_t dl; 1268 1269 TAILQ_FOREACH(dl, &dc->drivers, link) { 1270 DEVICE_IDENTIFY(dl->driver, dev); 1271 } 1272 1273 return (0); 1274} 1275 1276int 1277bus_generic_attach(device_t dev) 1278{ 1279 device_t child; 1280 1281 TAILQ_FOREACH(child, &dev->children, link) { 1282 device_probe_and_attach(child); 1283 } 1284 1285 return (0); 1286} 1287 1288int 1289bus_generic_detach(device_t dev) 1290{ 1291 device_t child; 1292 int error; 1293 1294 if (dev->state != DS_ATTACHED) 1295 return (EBUSY); 1296 1297 TAILQ_FOREACH(child, &dev->children, link) { 1298 if ((error = device_detach(child)) != 0) 1299 return (error); 1300 } 1301 1302 return (0); 1303} 1304 1305int 1306bus_generic_shutdown(device_t dev) 1307{ 1308 device_t child; 1309 1310 TAILQ_FOREACH(child, &dev->children, link) { 1311 device_shutdown(child); 1312 } 1313 1314 return (0); 1315} 1316 1317int 1318bus_generic_suspend(device_t dev) 1319{ 1320 int error; 1321 device_t child, child2; 1322 1323 TAILQ_FOREACH(child, &dev->children, 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 TAILQ_FOREACH(child, &dev->children, link) { 1342 DEVICE_RESUME(child); 1343 /* if resume fails, there's nothing we can usefully do... */ 1344 } 1345 return (0); 1346} 1347 1348int 1349bus_print_child_header (device_t dev, device_t child) 1350{ 1351 int retval = 0; 1352 1353 if (device_get_desc(child)) { 1354 retval += device_printf(child, "<%s>", device_get_desc(child)); 1355 } else { 1356 retval += printf("%s", device_get_nameunit(child)); 1357 } 1358 1359 return (retval); 1360} 1361 1362int 1363bus_print_child_footer (device_t dev, device_t child) 1364{ 1365 return (printf(" on %s\n", device_get_nameunit(dev))); 1366} 1367 1368int 1369bus_generic_print_child(device_t dev, device_t child) 1370{ 1371 int retval = 0; 1372 1373 retval += bus_print_child_header(dev, child); 1374 retval += bus_print_child_footer(dev, child); 1375 1376 return (retval); 1377} 1378 1379int 1380bus_generic_read_ivar(device_t dev, device_t child, int index, 1381 uintptr_t * result) 1382{ 1383 return (ENOENT); 1384} 1385 1386int 1387bus_generic_write_ivar(device_t dev, device_t child, int index, 1388 uintptr_t value) 1389{ 1390 return (ENOENT); 1391} 1392 1393struct resource_list * 1394bus_generic_get_resource_list (device_t dev, device_t child) 1395{ 1396 return (NULL); 1397} 1398 1399void 1400bus_generic_driver_added(device_t dev, driver_t *driver) 1401{ 1402 device_t child; 1403 1404 DEVICE_IDENTIFY(driver, dev); 1405 TAILQ_FOREACH(child, &dev->children, link) { 1406 if (child->state == DS_NOTPRESENT) 1407 device_probe_and_attach(child); 1408 } 1409} 1410 1411int 1412bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 1413 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1414{ 1415 /* Propagate up the bus hierarchy until someone handles it. */ 1416 if (dev->parent) 1417 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 1418 intr, arg, cookiep)); 1419 return (EINVAL); 1420} 1421 1422int 1423bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 1424 void *cookie) 1425{ 1426 /* Propagate up the bus hierarchy until someone handles it. */ 1427 if (dev->parent) 1428 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 1429 return (EINVAL); 1430} 1431 1432struct resource * 1433bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 1434 u_long start, u_long end, u_long count, u_int flags) 1435{ 1436 /* Propagate up the bus hierarchy until someone handles it. */ 1437 if (dev->parent) 1438 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 1439 start, end, count, flags)); 1440 return (NULL); 1441} 1442 1443int 1444bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 1445 struct resource *r) 1446{ 1447 /* Propagate up the bus hierarchy until someone handles it. */ 1448 if (dev->parent) 1449 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 1450 r)); 1451 return (EINVAL); 1452} 1453 1454int 1455bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 1456 struct resource *r) 1457{ 1458 /* Propagate up the bus hierarchy until someone handles it. */ 1459 if (dev->parent) 1460 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 1461 r)); 1462 return (EINVAL); 1463} 1464 1465int 1466bus_generic_deactivate_resource(device_t dev, device_t child, int type, 1467 int rid, struct resource *r) 1468{ 1469 /* Propagate up the bus hierarchy until someone handles it. */ 1470 if (dev->parent) 1471 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 1472 r)); 1473 return (EINVAL); 1474} 1475 1476int 1477bus_generic_rl_get_resource (device_t dev, device_t child, int type, int rid, 1478 u_long *startp, u_long *countp) 1479{ 1480 struct resource_list * rl = NULL; 1481 struct resource_list_entry * rle = NULL; 1482 1483 rl = BUS_GET_RESOURCE_LIST(dev, child); 1484 if (!rl) 1485 return (EINVAL); 1486 1487 rle = resource_list_find(rl, type, rid); 1488 if (!rle) 1489 return (ENOENT); 1490 1491 if (startp) 1492 *startp = rle->start; 1493 if (countp) 1494 *countp = rle->count; 1495 1496 return (0); 1497} 1498 1499int 1500bus_generic_rl_set_resource (device_t dev, device_t child, int type, int rid, 1501 u_long start, u_long count) 1502{ 1503 struct resource_list * rl = NULL; 1504 1505 rl = BUS_GET_RESOURCE_LIST(dev, child); 1506 if (!rl) 1507 return (EINVAL); 1508 1509 resource_list_add(rl, type, rid, start, (start + count - 1), count); 1510 1511 return (0); 1512} 1513 1514void 1515bus_generic_rl_delete_resource (device_t dev, device_t child, int type, int rid) 1516{ 1517 struct resource_list * rl = NULL; 1518 1519 rl = BUS_GET_RESOURCE_LIST(dev, child); 1520 if (!rl) 1521 return; 1522 1523 resource_list_delete(rl, type, rid); 1524 1525 return; 1526} 1527 1528int 1529bus_generic_rl_release_resource (device_t dev, device_t child, int type, 1530 int rid, struct resource *r) 1531{ 1532 struct resource_list * rl = NULL; 1533 1534 rl = BUS_GET_RESOURCE_LIST(dev, child); 1535 if (!rl) 1536 return (EINVAL); 1537 1538 return (resource_list_release(rl, dev, child, type, rid, r)); 1539} 1540 1541struct resource * 1542bus_generic_rl_alloc_resource (device_t dev, device_t child, int type, 1543 int *rid, u_long start, u_long end, u_long count, u_int flags) 1544{ 1545 struct resource_list * rl = NULL; 1546 1547 rl = BUS_GET_RESOURCE_LIST(dev, child); 1548 if (!rl) 1549 return (NULL); 1550 1551 return (resource_list_alloc(rl, dev, child, type, rid, 1552 start, end, count, flags)); 1553} 1554 1555/* 1556 * Some convenience functions to make it easier for drivers to use the 1557 * resource-management functions. All these really do is hide the 1558 * indirection through the parent's method table, making for slightly 1559 * less-wordy code. In the future, it might make sense for this code 1560 * to maintain some sort of a list of resources allocated by each device. 1561 */ 1562struct resource * 1563bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 1564 u_long count, u_int flags) 1565{ 1566 if (dev->parent == 0) 1567 return (0); 1568 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 1569 count, flags)); 1570} 1571 1572int 1573bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 1574{ 1575 if (dev->parent == 0) 1576 return (EINVAL); 1577 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 1578} 1579 1580int 1581bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 1582{ 1583 if (dev->parent == 0) 1584 return (EINVAL); 1585 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 1586} 1587 1588int 1589bus_release_resource(device_t dev, int type, int rid, struct resource *r) 1590{ 1591 if (dev->parent == 0) 1592 return (EINVAL); 1593 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 1594} 1595 1596int 1597bus_setup_intr(device_t dev, struct resource *r, int flags, 1598 driver_intr_t handler, void *arg, void **cookiep) 1599{ 1600 if (dev->parent == 0) 1601 return (EINVAL); 1602 return (BUS_SETUP_INTR(dev->parent, dev, r, flags, 1603 handler, arg, cookiep)); 1604} 1605 1606int 1607bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 1608{ 1609 if (dev->parent == 0) 1610 return (EINVAL); 1611 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 1612} 1613 1614int 1615bus_set_resource(device_t dev, int type, int rid, 1616 u_long start, u_long count) 1617{ 1618 return BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 1619 start, count); 1620} 1621 1622int 1623bus_get_resource(device_t dev, int type, int rid, 1624 u_long *startp, u_long *countp) 1625{ 1626 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 1627 startp, countp)); 1628} 1629 1630u_long 1631bus_get_resource_start(device_t dev, int type, int rid) 1632{ 1633 u_long start, count; 1634 int error; 1635 1636 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 1637 &start, &count); 1638 if (error) 1639 return (0); 1640 return (start); 1641} 1642 1643u_long 1644bus_get_resource_count(device_t dev, int type, int rid) 1645{ 1646 u_long start, count; 1647 int error; 1648 1649 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 1650 &start, &count); 1651 if (error) 1652 return (0); 1653 return (count); 1654} 1655 1656void 1657bus_delete_resource(device_t dev, int type, int rid) 1658{ 1659 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 1660} 1661 1662static int 1663root_print_child(device_t dev, device_t child) 1664{ 1665 int retval = 0; 1666 1667 retval += bus_print_child_header(dev, child); 1668 retval += printf("\n"); 1669 1670 return (retval); 1671} 1672 1673static int 1674root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 1675 void **cookiep) 1676{ 1677 /* 1678 * If an interrupt mapping gets to here something bad has happened. 1679 */ 1680 panic("root_setup_intr"); 1681} 1682 1683static kobj_method_t root_methods[] = { 1684 /* Device interface */ 1685 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 1686 KOBJMETHOD(device_suspend, bus_generic_suspend), 1687 KOBJMETHOD(device_resume, bus_generic_resume), 1688 1689 /* Bus interface */ 1690 KOBJMETHOD(bus_print_child, root_print_child), 1691 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 1692 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 1693 KOBJMETHOD(bus_setup_intr, root_setup_intr), 1694 1695 { 0, 0 } 1696}; 1697 1698static driver_t root_driver = { 1699 "root", 1700 root_methods, 1701 1, /* no softc */ 1702}; 1703 1704device_t root_bus; 1705devclass_t root_devclass; 1706 1707static int 1708root_bus_module_handler(module_t mod, int what, void* arg) 1709{ 1710 switch (what) { 1711 case MOD_LOAD: 1712 TAILQ_INIT(&bus_data_devices); 1713 kobj_class_compile((kobj_class_t) &root_driver); 1714 root_bus = make_device(NULL, "root", 0); 1715 root_bus->desc = "System root bus"; 1716 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 1717 root_bus->driver = &root_driver; 1718 root_bus->state = DS_ATTACHED; 1719 root_devclass = devclass_find_internal("root", FALSE); 1720 return (0); 1721 1722 case MOD_SHUTDOWN: 1723 device_shutdown(root_bus); 1724 return (0); 1725 } 1726 1727 return (0); 1728} 1729 1730static moduledata_t root_bus_mod = { 1731 "rootbus", 1732 root_bus_module_handler, 1733 0 1734}; 1735DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1736 1737void 1738root_bus_configure(void) 1739{ 1740 device_t dev; 1741 1742 PDEBUG((".")); 1743 1744 TAILQ_FOREACH(dev, &root_bus->children, link) { 1745 device_probe_and_attach(dev); 1746 } 1747} 1748 1749int 1750driver_module_handler(module_t mod, int what, void *arg) 1751{ 1752 int error, i; 1753 struct driver_module_data *dmd; 1754 devclass_t bus_devclass; 1755 1756 dmd = (struct driver_module_data *)arg; 1757 bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE); 1758 error = 0; 1759 1760 switch (what) { 1761 case MOD_LOAD: 1762 if (dmd->dmd_chainevh) 1763 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 1764 1765 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) { 1766 PDEBUG(("Loading module: driver %s on bus %s", 1767 DRIVERNAME(dmd->dmd_drivers[i]), dmd->dmd_busname)); 1768 error = devclass_add_driver(bus_devclass, 1769 dmd->dmd_drivers[i]); 1770 } 1771 if (error) 1772 break; 1773 1774 /* 1775 * The drivers loaded in this way are assumed to all 1776 * implement the same devclass. 1777 */ 1778 *dmd->dmd_devclass = 1779 devclass_find_internal(dmd->dmd_drivers[0]->name, TRUE); 1780 break; 1781 1782 case MOD_UNLOAD: 1783 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) { 1784 PDEBUG(("Unloading module: driver %s from bus %s", 1785 DRIVERNAME(dmd->dmd_drivers[i]), 1786 dmd->dmd_busname)); 1787 error = devclass_delete_driver(bus_devclass, 1788 dmd->dmd_drivers[i]); 1789 } 1790 1791 if (!error && dmd->dmd_chainevh) 1792 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 1793 break; 1794 } 1795 1796 return (error); 1797} 1798 1799#ifdef BUS_DEBUG 1800 1801/* the _short versions avoid iteration by not calling anything that prints 1802 * more than oneliners. I love oneliners. 1803 */ 1804 1805static void 1806print_device_short(device_t dev, int indent) 1807{ 1808 if (!dev) 1809 return; 1810 1811 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 1812 dev->unit, dev->desc, 1813 (dev->parent? "":"no "), 1814 (TAILQ_EMPTY(&dev->children)? "no ":""), 1815 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 1816 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 1817 (dev->flags&DF_WILDCARD? "wildcard,":""), 1818 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 1819 (dev->ivars? "":"no "), 1820 (dev->softc? "":"no "), 1821 dev->busy)); 1822} 1823 1824static void 1825print_device(device_t dev, int indent) 1826{ 1827 if (!dev) 1828 return; 1829 1830 print_device_short(dev, indent); 1831 1832 indentprintf(("Parent:\n")); 1833 print_device_short(dev->parent, indent+1); 1834 indentprintf(("Driver:\n")); 1835 print_driver_short(dev->driver, indent+1); 1836 indentprintf(("Devclass:\n")); 1837 print_devclass_short(dev->devclass, indent+1); 1838} 1839 1840void 1841print_device_tree_short(device_t dev, int indent) 1842/* print the device and all its children (indented) */ 1843{ 1844 device_t child; 1845 1846 if (!dev) 1847 return; 1848 1849 print_device_short(dev, indent); 1850 1851 TAILQ_FOREACH(child, &dev->children, link) { 1852 print_device_tree_short(child, indent+1); 1853 } 1854} 1855 1856void 1857print_device_tree(device_t dev, int indent) 1858/* print the device and all its children (indented) */ 1859{ 1860 device_t child; 1861 1862 if (!dev) 1863 return; 1864 1865 print_device(dev, indent); 1866 1867 TAILQ_FOREACH(child, &dev->children, link) { 1868 print_device_tree(child, indent+1); 1869 } 1870} 1871 1872static void 1873print_driver_short(driver_t *driver, int indent) 1874{ 1875 if (!driver) 1876 return; 1877 1878 indentprintf(("driver %s: softc size = %d\n", 1879 driver->name, driver->size)); 1880} 1881 1882static void 1883print_driver(driver_t *driver, int indent) 1884{ 1885 if (!driver) 1886 return; 1887 1888 print_driver_short(driver, indent); 1889} 1890 1891 1892static void 1893print_driver_list(driver_list_t drivers, int indent) 1894{ 1895 driverlink_t driver; 1896 1897 TAILQ_FOREACH(driver, &drivers, link) { 1898 print_driver(driver->driver, indent); 1899 } 1900} 1901 1902static void 1903print_devclass_short(devclass_t dc, int indent) 1904{ 1905 if ( !dc ) 1906 return; 1907 1908 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 1909} 1910 1911static void 1912print_devclass(devclass_t dc, int indent) 1913{ 1914 int i; 1915 1916 if ( !dc ) 1917 return; 1918 1919 print_devclass_short(dc, indent); 1920 indentprintf(("Drivers:\n")); 1921 print_driver_list(dc->drivers, indent+1); 1922 1923 indentprintf(("Devices:\n")); 1924 for (i = 0; i < dc->maxunit; i++) 1925 if (dc->devices[i]) 1926 print_device(dc->devices[i], indent+1); 1927} 1928 1929void 1930print_devclass_list_short(void) 1931{ 1932 devclass_t dc; 1933 1934 printf("Short listing of devclasses, drivers & devices:\n"); 1935 TAILQ_FOREACH(dc, &devclasses, link) { 1936 print_devclass_short(dc, 0); 1937 } 1938} 1939 1940void 1941print_devclass_list(void) 1942{ 1943 devclass_t dc; 1944 1945 printf("Full listing of devclasses, drivers & devices:\n"); 1946 TAILQ_FOREACH(dc, &devclasses, link) { 1947 print_devclass(dc, 0); 1948 } 1949} 1950 1951#endif 1952 1953/* 1954 * User-space access to the device tree. 1955 * 1956 * We implement a small set of nodes: 1957 * 1958 * hw.bus Single integer read method to obtain the 1959 * current generation count. 1960 * hw.bus.devices Reads the entire device tree in flat space. 1961 * hw.bus.rman Resource manager interface 1962 * 1963 * We might like to add the ability to scan devclasses and/or drivers to 1964 * determine what else is currently loaded/available. 1965 */ 1966SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL); 1967 1968static int 1969sysctl_bus(SYSCTL_HANDLER_ARGS) 1970{ 1971 struct u_businfo ubus; 1972 1973 ubus.ub_version = BUS_USER_VERSION; 1974 ubus.ub_generation = bus_data_generation; 1975 1976 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 1977} 1978SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 1979 "bus-related data"); 1980 1981static int 1982sysctl_devices(SYSCTL_HANDLER_ARGS) 1983{ 1984 int *name = (int *)arg1; 1985 u_int namelen = arg2; 1986 int index; 1987 struct device *dev; 1988 struct u_device udev; /* XXX this is a bit big */ 1989 int error; 1990 1991 if (namelen != 2) 1992 return (EINVAL); 1993 1994 if (bus_data_generation_check(name[0])) 1995 return (EINVAL); 1996 1997 index = name[1]; 1998 1999 /* 2000 * Scan the list of devices, looking for the requested index. 2001 */ 2002 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 2003 if (index-- == 0) 2004 break; 2005 } 2006 if (dev == NULL) 2007 return (ENOENT); 2008 2009 /* 2010 * Populate the return array. 2011 */ 2012 udev.dv_handle = (uintptr_t)dev; 2013 udev.dv_parent = (uintptr_t)dev->parent; 2014 if (dev->nameunit == NULL) { 2015 udev.dv_name[0] = 0; 2016 } else { 2017 snprintf(udev.dv_name, 32, "%s", dev->nameunit); 2018 } 2019 if (dev->desc == NULL) { 2020 udev.dv_desc[0] = 0; 2021 } else { 2022 snprintf(udev.dv_desc, 32, "%s", dev->desc); 2023 } 2024 if ((dev->driver == NULL) || (dev->driver->name == NULL)) { 2025 udev.dv_drivername[0] = 0; 2026 } else { 2027 snprintf(udev.dv_drivername, 32, "%s", dev->driver->name); 2028 } 2029 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 2030 return (error); 2031} 2032 2033SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 2034 "system device tree"); 2035 2036/* 2037 * Sysctl interface for scanning the resource lists. 2038 * 2039 * We take two input parameters; the index into the list of resource 2040 * managers, and the resource offset into the list. 2041 */ 2042static int 2043sysctl_rman(SYSCTL_HANDLER_ARGS) 2044{ 2045 int *name = (int *)arg1; 2046 u_int namelen = arg2; 2047 int rman_idx, res_idx; 2048 struct rman *rm; 2049 struct resource *res; 2050 struct u_rman urm; 2051 struct u_resource ures; 2052 int error; 2053 2054 if (namelen != 3) 2055 return (EINVAL); 2056 2057 if (bus_data_generation_check(name[0])) 2058 return (EINVAL); 2059 rman_idx = name[1]; 2060 res_idx = name[2]; 2061 2062 /* 2063 * Find the indexed resource manager 2064 */ 2065 TAILQ_FOREACH(rm, &rman_head, rm_link) { 2066 if (rman_idx-- == 0) 2067 break; 2068 } 2069 if (rm == NULL) 2070 return (ENOENT); 2071 2072 /* 2073 * If the resource index is -1, we want details on the 2074 * resource manager. 2075 */ 2076 if (res_idx == -1) { 2077 urm.rm_handle = (uintptr_t)rm; 2078 snprintf(urm.rm_descr, RM_TEXTLEN, "%s", rm->rm_descr); 2079 urm.rm_descr[RM_TEXTLEN - 1] = '\0'; 2080 urm.rm_start = rm->rm_start; 2081 urm.rm_size = rm->rm_end - rm->rm_start + 1; 2082 urm.rm_type = rm->rm_type; 2083 2084 error = SYSCTL_OUT(req, &urm, sizeof(urm)); 2085 return (error); 2086 } 2087 2088 /* 2089 * Find the indexed resource and return it. 2090 */ 2091 TAILQ_FOREACH(res, &rm->rm_list, r_link) { 2092 if (res_idx-- == 0) { 2093 ures.r_handle = (uintptr_t)res; 2094 ures.r_parent = (uintptr_t)res->r_rm; 2095 ures.r_device = (uintptr_t)res->r_dev; 2096 if (res->r_dev != NULL) { 2097 if (device_get_name(res->r_dev) != NULL) { 2098 snprintf(ures.r_devname, RM_TEXTLEN, 2099 "%s%d", 2100 device_get_name(res->r_dev), 2101 device_get_unit(res->r_dev)); 2102 } else { 2103 snprintf(ures.r_devname, RM_TEXTLEN, 2104 "nomatch"); 2105 } 2106 } else { 2107 ures.r_devname[0] = 0; 2108 } 2109 ures.r_start = res->r_start; 2110 ures.r_size = res->r_end - res->r_start + 1; 2111 ures.r_flags = res->r_flags; 2112 2113 error = SYSCTL_OUT(req, &ures, sizeof(ures)); 2114 return (error); 2115 } 2116 } 2117 return (ENOENT); 2118} 2119 2120SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman, 2121 "kernel resource manager"); 2122 2123int 2124bus_data_generation_check(int generation) 2125{ 2126 if (generation != bus_data_generation) 2127 return (1); 2128 2129 /* XXX generate optimised lists here? */ 2130 return (0); 2131} 2132 2133void 2134bus_data_generation_update(void) 2135{ 2136 bus_data_generation++; 2137} 2138