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