subr_bus.c revision 79696
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 79696 2001-07-14 00:23:10Z peter $ 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 344static int 345devclass_alloc_unit(devclass_t dc, int *unitp) 346{ 347 int unit = *unitp; 348 349 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 350 351 /* If we were given a wired unit number, check for existing device */ 352 /* XXX imp XXX */ 353 if (unit != -1) { 354 if (unit >= 0 && unit < dc->maxunit && 355 dc->devices[unit] != NULL) { 356 /* find the next available slot */ 357 while (++unit < dc->maxunit && 358 dc->devices[unit] != NULL) 359 continue; 360 if (bootverbose) 361 printf("%s-: %s%d already exists, using %s%d instead\n", 362 dc->name, dc->name, *unitp, dc->name, unit); 363 } 364 } else { 365 /* Unwired device, find the next available slot for it */ 366 unit = 0; 367 while (unit < dc->maxunit && dc->devices[unit] != NULL) 368 unit++; 369 } 370 371 /* 372 * We've selected a unit beyond the length of the table, so let's 373 * extend the table to make room for all units up to and including 374 * this one. 375 */ 376 if (unit >= dc->maxunit) { 377 device_t *newlist; 378 int newsize; 379 380 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 381 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 382 if (!newlist) 383 return (ENOMEM); 384 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 385 bzero(newlist + dc->maxunit, 386 sizeof(device_t) * (newsize - dc->maxunit)); 387 if (dc->devices) 388 free(dc->devices, M_BUS); 389 dc->devices = newlist; 390 dc->maxunit = newsize; 391 } 392 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 393 394 *unitp = unit; 395 return (0); 396} 397 398static int 399devclass_add_device(devclass_t dc, device_t dev) 400{ 401 int buflen, error; 402 403 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 404 405 buflen = strlen(dc->name) + 5; 406 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 407 if (!dev->nameunit) 408 return (ENOMEM); 409 410 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { 411 free(dev->nameunit, M_BUS); 412 dev->nameunit = NULL; 413 return (error); 414 } 415 dc->devices[dev->unit] = dev; 416 dev->devclass = dc; 417 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 418 419 return (0); 420} 421 422static int 423devclass_delete_device(devclass_t dc, device_t dev) 424{ 425 if (!dc || !dev) 426 return (0); 427 428 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 429 430 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 431 panic("devclass_delete_device: inconsistent device class"); 432 dc->devices[dev->unit] = NULL; 433 if (dev->flags & DF_WILDCARD) 434 dev->unit = -1; 435 dev->devclass = NULL; 436 free(dev->nameunit, M_BUS); 437 dev->nameunit = NULL; 438 439 return (0); 440} 441 442static device_t 443make_device(device_t parent, const char *name, int unit) 444{ 445 device_t dev; 446 devclass_t dc; 447 448 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 449 450 if (name) { 451 dc = devclass_find_internal(name, TRUE); 452 if (!dc) { 453 printf("make_device: can't find device class %s\n", 454 name); 455 return (NULL); 456 } 457 } else { 458 dc = NULL; 459 } 460 461 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO); 462 if (!dev) 463 return (0); 464 465 dev->parent = parent; 466 TAILQ_INIT(&dev->children); 467 kobj_init((kobj_t) dev, &null_class); 468 dev->driver = NULL; 469 dev->devclass = NULL; 470 dev->unit = unit; 471 dev->nameunit = NULL; 472 dev->desc = NULL; 473 dev->busy = 0; 474 dev->devflags = 0; 475 dev->flags = DF_ENABLED; 476 dev->order = 0; 477 if (unit == -1) 478 dev->flags |= DF_WILDCARD; 479 if (name) { 480 dev->flags |= DF_FIXEDCLASS; 481 devclass_add_device(dc, dev); 482 } 483 dev->ivars = NULL; 484 dev->softc = NULL; 485 486 dev->state = DS_NOTPRESENT; 487 488 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 489 bus_data_generation_update(); 490 491 return (dev); 492} 493 494static int 495device_print_child(device_t dev, device_t child) 496{ 497 int retval = 0; 498 499 if (device_is_alive(child)) 500 retval += BUS_PRINT_CHILD(dev, child); 501 else 502 retval += device_printf(child, " not found\n"); 503 504 return (retval); 505} 506 507device_t 508device_add_child(device_t dev, const char *name, int unit) 509{ 510 return (device_add_child_ordered(dev, 0, name, unit)); 511} 512 513device_t 514device_add_child_ordered(device_t dev, int order, const char *name, int unit) 515{ 516 device_t child; 517 device_t place; 518 519 PDEBUG(("%s at %s with order %d as unit %d", 520 name, DEVICENAME(dev), order, unit)); 521 522 child = make_device(dev, name, unit); 523 if (child == NULL) 524 return (child); 525 child->order = order; 526 527 TAILQ_FOREACH(place, &dev->children, link) { 528 if (place->order > order) 529 break; 530 } 531 532 if (place) { 533 /* 534 * The device 'place' is the first device whose order is 535 * greater than the new child. 536 */ 537 TAILQ_INSERT_BEFORE(place, child, link); 538 } else { 539 /* 540 * The new child's order is greater or equal to the order of 541 * any existing device. Add the child to the tail of the list. 542 */ 543 TAILQ_INSERT_TAIL(&dev->children, child, link); 544 } 545 546 bus_data_generation_update(); 547 return (child); 548} 549 550int 551device_delete_child(device_t dev, device_t child) 552{ 553 int error; 554 device_t grandchild; 555 556 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 557 558 /* remove children first */ 559 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 560 error = device_delete_child(child, grandchild); 561 if (error) 562 return (error); 563 } 564 565 if ((error = device_detach(child)) != 0) 566 return (error); 567 if (child->devclass) 568 devclass_delete_device(child->devclass, child); 569 TAILQ_REMOVE(&dev->children, child, link); 570 TAILQ_REMOVE(&bus_data_devices, child, devlink); 571 device_set_desc(child, NULL); 572 free(child, M_BUS); 573 574 bus_data_generation_update(); 575 return (0); 576} 577 578/* 579 * Find only devices attached to this bus. 580 */ 581device_t 582device_find_child(device_t dev, const char *classname, int unit) 583{ 584 devclass_t dc; 585 device_t child; 586 587 dc = devclass_find(classname); 588 if (!dc) 589 return (NULL); 590 591 child = devclass_get_device(dc, unit); 592 if (child && child->parent == dev) 593 return (child); 594 return (NULL); 595} 596 597static driverlink_t 598first_matching_driver(devclass_t dc, device_t dev) 599{ 600 if (dev->devclass) 601 return (devclass_find_driver_internal(dc, dev->devclass->name)); 602 return (TAILQ_FIRST(&dc->drivers)); 603} 604 605static driverlink_t 606next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 607{ 608 if (dev->devclass) { 609 driverlink_t dl; 610 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 611 if (!strcmp(dev->devclass->name, dl->driver->name)) 612 return (dl); 613 return (NULL); 614 } 615 return (TAILQ_NEXT(last, link)); 616} 617 618static int 619device_probe_child(device_t dev, device_t child) 620{ 621 devclass_t dc; 622 driverlink_t best = 0; 623 driverlink_t dl; 624 int result, pri = 0; 625 int hasclass = (child->devclass != 0); 626 627 dc = dev->devclass; 628 if (!dc) 629 panic("device_probe_child: parent device has no devclass"); 630 631 if (child->state == DS_ALIVE) 632 return (0); 633 634 for (dl = first_matching_driver(dc, child); 635 dl; 636 dl = next_matching_driver(dc, child, dl)) { 637 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 638 device_set_driver(child, dl->driver); 639 if (!hasclass) 640 device_set_devclass(child, dl->driver->name); 641 result = DEVICE_PROBE(child); 642 if (!hasclass) 643 device_set_devclass(child, 0); 644 645 /* 646 * If the driver returns SUCCESS, there can be no higher match 647 * for this device. 648 */ 649 if (result == 0) { 650 best = dl; 651 pri = 0; 652 break; 653 } 654 655 /* 656 * The driver returned an error so it certainly doesn't match. 657 */ 658 if (result > 0) { 659 device_set_driver(child, 0); 660 continue; 661 } 662 663 /* 664 * A priority lower than SUCCESS, remember the best matching 665 * driver. Initialise the value of pri for the first match. 666 */ 667 if (best == 0 || result > pri) { 668 best = dl; 669 pri = result; 670 continue; 671 } 672 } 673 674 /* 675 * If we found a driver, change state and initialise the devclass. 676 */ 677 if (best) { 678 if (!child->devclass) 679 device_set_devclass(child, best->driver->name); 680 device_set_driver(child, best->driver); 681 if (pri < 0) { 682 /* 683 * A bit bogus. Call the probe method again to make 684 * sure that we have the right description. 685 */ 686 DEVICE_PROBE(child); 687 } 688 child->state = DS_ALIVE; 689 690 bus_data_generation_update(); 691 return (0); 692 } 693 694 return (ENXIO); 695} 696 697device_t 698device_get_parent(device_t dev) 699{ 700 return (dev->parent); 701} 702 703int 704device_get_children(device_t dev, device_t **devlistp, int *devcountp) 705{ 706 int count; 707 device_t child; 708 device_t *list; 709 710 count = 0; 711 TAILQ_FOREACH(child, &dev->children, link) { 712 count++; 713 } 714 715 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 716 if (!list) 717 return (ENOMEM); 718 719 count = 0; 720 TAILQ_FOREACH(child, &dev->children, link) { 721 list[count] = child; 722 count++; 723 } 724 725 *devlistp = list; 726 *devcountp = count; 727 728 return (0); 729} 730 731driver_t * 732device_get_driver(device_t dev) 733{ 734 return (dev->driver); 735} 736 737devclass_t 738device_get_devclass(device_t dev) 739{ 740 return (dev->devclass); 741} 742 743const char * 744device_get_name(device_t dev) 745{ 746 if (dev->devclass) 747 return (devclass_get_name(dev->devclass)); 748 return (NULL); 749} 750 751const char * 752device_get_nameunit(device_t dev) 753{ 754 return (dev->nameunit); 755} 756 757int 758device_get_unit(device_t dev) 759{ 760 return (dev->unit); 761} 762 763const char * 764device_get_desc(device_t dev) 765{ 766 return (dev->desc); 767} 768 769u_int32_t 770device_get_flags(device_t dev) 771{ 772 return (dev->devflags); 773} 774 775int 776device_print_prettyname(device_t dev) 777{ 778 const char *name = device_get_name(dev); 779 780 if (name == 0) 781 return (printf("unknown: ")); 782 return (printf("%s%d: ", name, device_get_unit(dev))); 783} 784 785int 786device_printf(device_t dev, const char * fmt, ...) 787{ 788 va_list ap; 789 int retval; 790 791 retval = device_print_prettyname(dev); 792 va_start(ap, fmt); 793 retval += vprintf(fmt, ap); 794 va_end(ap); 795 return (retval); 796} 797 798static void 799device_set_desc_internal(device_t dev, const char* desc, int copy) 800{ 801 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 802 free(dev->desc, M_BUS); 803 dev->flags &= ~DF_DESCMALLOCED; 804 dev->desc = NULL; 805 } 806 807 if (copy && desc) { 808 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 809 if (dev->desc) { 810 strcpy(dev->desc, desc); 811 dev->flags |= DF_DESCMALLOCED; 812 } 813 } else { 814 /* Avoid a -Wcast-qual warning */ 815 dev->desc = (char *)(uintptr_t) desc; 816 } 817 818 bus_data_generation_update(); 819} 820 821void 822device_set_desc(device_t dev, const char* desc) 823{ 824 device_set_desc_internal(dev, desc, FALSE); 825} 826 827void 828device_set_desc_copy(device_t dev, const char* desc) 829{ 830 device_set_desc_internal(dev, desc, TRUE); 831} 832 833void 834device_set_flags(device_t dev, u_int32_t flags) 835{ 836 dev->devflags = flags; 837} 838 839void * 840device_get_softc(device_t dev) 841{ 842 return (dev->softc); 843} 844 845void 846device_set_softc(device_t dev, void *softc) 847{ 848 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 849 free(dev->softc, M_BUS); 850 dev->softc = softc; 851 if (dev->softc) 852 dev->flags |= DF_EXTERNALSOFTC; 853 else 854 dev->flags &= ~DF_EXTERNALSOFTC; 855} 856 857void * 858device_get_ivars(device_t dev) 859{ 860 return (dev->ivars); 861} 862 863void 864device_set_ivars(device_t dev, void * ivars) 865{ 866 if (!dev) 867 return; 868 869 dev->ivars = ivars; 870 871 return; 872} 873 874device_state_t 875device_get_state(device_t dev) 876{ 877 return (dev->state); 878} 879 880void 881device_enable(device_t dev) 882{ 883 dev->flags |= DF_ENABLED; 884} 885 886void 887device_disable(device_t dev) 888{ 889 dev->flags &= ~DF_ENABLED; 890} 891 892void 893device_busy(device_t dev) 894{ 895 if (dev->state < DS_ATTACHED) 896 panic("device_busy: called for unattached device"); 897 if (dev->busy == 0 && dev->parent) 898 device_busy(dev->parent); 899 dev->busy++; 900 dev->state = DS_BUSY; 901} 902 903void 904device_unbusy(device_t dev) 905{ 906 if (dev->state != DS_BUSY) 907 panic("device_unbusy: called for non-busy device"); 908 dev->busy--; 909 if (dev->busy == 0) { 910 if (dev->parent) 911 device_unbusy(dev->parent); 912 dev->state = DS_ATTACHED; 913 } 914} 915 916void 917device_quiet(device_t dev) 918{ 919 dev->flags |= DF_QUIET; 920} 921 922void 923device_verbose(device_t dev) 924{ 925 dev->flags &= ~DF_QUIET; 926} 927 928int 929device_is_quiet(device_t dev) 930{ 931 return ((dev->flags & DF_QUIET) != 0); 932} 933 934int 935device_is_enabled(device_t dev) 936{ 937 return ((dev->flags & DF_ENABLED) != 0); 938} 939 940int 941device_is_alive(device_t dev) 942{ 943 return (dev->state >= DS_ALIVE); 944} 945 946int 947device_set_devclass(device_t dev, const char *classname) 948{ 949 devclass_t dc; 950 int error; 951 952 if (!classname) { 953 if (dev->devclass) 954 devclass_delete_device(dev->devclass, dev); 955 return (0); 956 } 957 958 if (dev->devclass) { 959 printf("device_set_devclass: device class already set\n"); 960 return (EINVAL); 961 } 962 963 dc = devclass_find_internal(classname, TRUE); 964 if (!dc) 965 return (ENOMEM); 966 967 error = devclass_add_device(dc, dev); 968 969 bus_data_generation_update(); 970 return (error); 971} 972 973int 974device_set_driver(device_t dev, driver_t *driver) 975{ 976 if (dev->state >= DS_ATTACHED) 977 return (EBUSY); 978 979 if (dev->driver == driver) 980 return (0); 981 982 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 983 free(dev->softc, M_BUS); 984 dev->softc = NULL; 985 } 986 kobj_delete((kobj_t) dev, 0); 987 dev->driver = driver; 988 if (driver) { 989 kobj_init((kobj_t) dev, (kobj_class_t) driver); 990 if (!(dev->flags & DF_EXTERNALSOFTC)) { 991 dev->softc = malloc(driver->size, M_BUS, 992 M_NOWAIT | M_ZERO); 993 if (!dev->softc) { 994 kobj_init((kobj_t) dev, &null_class); 995 dev->driver = NULL; 996 return (ENOMEM); 997 } 998 } 999 } else { 1000 kobj_init((kobj_t) dev, &null_class); 1001 } 1002 1003 bus_data_generation_update(); 1004 return (0); 1005} 1006 1007int 1008device_probe_and_attach(device_t dev) 1009{ 1010 device_t bus = dev->parent; 1011 int error = 0; 1012 int hasclass = (dev->devclass != 0); 1013 1014 if (dev->state >= DS_ALIVE) 1015 return (0); 1016 1017 if (dev->flags & DF_ENABLED) { 1018 error = device_probe_child(bus, dev); 1019 if (!error) { 1020 if (!device_is_quiet(dev)) 1021 device_print_child(bus, dev); 1022 error = DEVICE_ATTACH(dev); 1023 if (!error) 1024 dev->state = DS_ATTACHED; 1025 else { 1026 printf("device_probe_and_attach: %s%d attach returned %d\n", 1027 dev->driver->name, dev->unit, error); 1028 /* Unset the class; set in device_probe_child */ 1029 if (!hasclass) 1030 device_set_devclass(dev, 0); 1031 device_set_driver(dev, NULL); 1032 dev->state = DS_NOTPRESENT; 1033 } 1034 } else { 1035 if (!(dev->flags & DF_DONENOMATCH)) { 1036 BUS_PROBE_NOMATCH(bus, dev); 1037 dev->flags |= DF_DONENOMATCH; 1038 } 1039 } 1040 } else { 1041 if (bootverbose) { 1042 device_print_prettyname(dev); 1043 printf("not probed (disabled)\n"); 1044 } 1045 } 1046 1047 return (error); 1048} 1049 1050int 1051device_detach(device_t dev) 1052{ 1053 int error; 1054 1055 PDEBUG(("%s", DEVICENAME(dev))); 1056 if (dev->state == DS_BUSY) 1057 return (EBUSY); 1058 if (dev->state != DS_ATTACHED) 1059 return (0); 1060 1061 if ((error = DEVICE_DETACH(dev)) != 0) 1062 return (error); 1063 device_printf(dev, "detached\n"); 1064 if (dev->parent) 1065 BUS_CHILD_DETACHED(dev->parent, dev); 1066 1067 if (!(dev->flags & DF_FIXEDCLASS)) 1068 devclass_delete_device(dev->devclass, dev); 1069 1070 dev->state = DS_NOTPRESENT; 1071 device_set_driver(dev, NULL); 1072 1073 return (0); 1074} 1075 1076int 1077device_shutdown(device_t dev) 1078{ 1079 if (dev->state < DS_ATTACHED) 1080 return (0); 1081 return (DEVICE_SHUTDOWN(dev)); 1082} 1083 1084int 1085device_set_unit(device_t dev, int unit) 1086{ 1087 devclass_t dc; 1088 int err; 1089 1090 dc = device_get_devclass(dev); 1091 if (unit < dc->maxunit && dc->devices[unit]) 1092 return (EBUSY); 1093 err = devclass_delete_device(dc, dev); 1094 if (err) 1095 return (err); 1096 dev->unit = unit; 1097 err = devclass_add_device(dc, dev); 1098 if (err) 1099 return (err); 1100 1101 bus_data_generation_update(); 1102 return (0); 1103} 1104 1105/*======================================*/ 1106/* 1107 * Some useful method implementations to make life easier for bus drivers. 1108 */ 1109 1110void 1111resource_list_init(struct resource_list *rl) 1112{ 1113 SLIST_INIT(rl); 1114} 1115 1116void 1117resource_list_free(struct resource_list *rl) 1118{ 1119 struct resource_list_entry *rle; 1120 1121 while ((rle = SLIST_FIRST(rl)) != NULL) { 1122 if (rle->res) 1123 panic("resource_list_free: resource entry is busy"); 1124 SLIST_REMOVE_HEAD(rl, link); 1125 free(rle, M_BUS); 1126 } 1127} 1128 1129void 1130resource_list_add(struct resource_list *rl, int type, int rid, 1131 u_long start, u_long end, u_long count) 1132{ 1133 struct resource_list_entry *rle; 1134 1135 rle = resource_list_find(rl, type, rid); 1136 if (!rle) { 1137 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 1138 M_NOWAIT); 1139 if (!rle) 1140 panic("resource_list_add: can't record entry"); 1141 SLIST_INSERT_HEAD(rl, rle, link); 1142 rle->type = type; 1143 rle->rid = rid; 1144 rle->res = NULL; 1145 } 1146 1147 if (rle->res) 1148 panic("resource_list_add: resource entry is busy"); 1149 1150 rle->start = start; 1151 rle->end = end; 1152 rle->count = count; 1153} 1154 1155struct resource_list_entry * 1156resource_list_find(struct resource_list *rl, int type, int rid) 1157{ 1158 struct resource_list_entry *rle; 1159 1160 SLIST_FOREACH(rle, rl, link) { 1161 if (rle->type == type && rle->rid == rid) 1162 return (rle); 1163 } 1164 return (NULL); 1165} 1166 1167void 1168resource_list_delete(struct resource_list *rl, int type, int rid) 1169{ 1170 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 1171 1172 if (rle) { 1173 if (rle->res != NULL) 1174 panic("resource_list_delete: resource has not been released"); 1175 SLIST_REMOVE(rl, rle, resource_list_entry, link); 1176 free(rle, M_BUS); 1177 } 1178} 1179 1180struct resource * 1181resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 1182 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 1183{ 1184 struct resource_list_entry *rle = 0; 1185 int passthrough = (device_get_parent(child) != bus); 1186 int isdefault = (start == 0UL && end == ~0UL); 1187 1188 if (passthrough) { 1189 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1190 type, rid, start, end, count, flags)); 1191 } 1192 1193 rle = resource_list_find(rl, type, *rid); 1194 1195 if (!rle) 1196 return (NULL); /* no resource of that type/rid */ 1197 1198 if (rle->res) 1199 panic("resource_list_alloc: resource entry is busy"); 1200 1201 if (isdefault) { 1202 start = rle->start; 1203 count = max(count, rle->count); 1204 end = max(rle->end, start + count - 1); 1205 } 1206 1207 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1208 type, rid, start, end, count, flags); 1209 1210 /* 1211 * Record the new range. 1212 */ 1213 if (rle->res) { 1214 rle->start = rman_get_start(rle->res); 1215 rle->end = rman_get_end(rle->res); 1216 rle->count = count; 1217 } 1218 1219 return (rle->res); 1220} 1221 1222int 1223resource_list_release(struct resource_list *rl, device_t bus, device_t child, 1224 int type, int rid, struct resource *res) 1225{ 1226 struct resource_list_entry *rle = 0; 1227 int passthrough = (device_get_parent(child) != bus); 1228 int error; 1229 1230 if (passthrough) { 1231 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1232 type, rid, res)); 1233 } 1234 1235 rle = resource_list_find(rl, type, rid); 1236 1237 if (!rle) 1238 panic("resource_list_release: can't find resource"); 1239 if (!rle->res) 1240 panic("resource_list_release: resource entry is not busy"); 1241 1242 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1243 type, rid, res); 1244 if (error) 1245 return (error); 1246 1247 rle->res = NULL; 1248 return (0); 1249} 1250 1251/* 1252 * Call DEVICE_IDENTIFY for each driver. 1253 */ 1254int 1255bus_generic_probe(device_t dev) 1256{ 1257 devclass_t dc = dev->devclass; 1258 driverlink_t dl; 1259 1260 TAILQ_FOREACH(dl, &dc->drivers, link) { 1261 DEVICE_IDENTIFY(dl->driver, dev); 1262 } 1263 1264 return (0); 1265} 1266 1267int 1268bus_generic_attach(device_t dev) 1269{ 1270 device_t child; 1271 1272 TAILQ_FOREACH(child, &dev->children, link) { 1273 device_probe_and_attach(child); 1274 } 1275 1276 return (0); 1277} 1278 1279int 1280bus_generic_detach(device_t dev) 1281{ 1282 device_t child; 1283 int error; 1284 1285 if (dev->state != DS_ATTACHED) 1286 return (EBUSY); 1287 1288 TAILQ_FOREACH(child, &dev->children, link) { 1289 if ((error = device_detach(child)) != 0) 1290 return (error); 1291 } 1292 1293 return (0); 1294} 1295 1296int 1297bus_generic_shutdown(device_t dev) 1298{ 1299 device_t child; 1300 1301 TAILQ_FOREACH(child, &dev->children, link) { 1302 device_shutdown(child); 1303 } 1304 1305 return (0); 1306} 1307 1308int 1309bus_generic_suspend(device_t dev) 1310{ 1311 int error; 1312 device_t child, child2; 1313 1314 TAILQ_FOREACH(child, &dev->children, link) { 1315 error = DEVICE_SUSPEND(child); 1316 if (error) { 1317 for (child2 = TAILQ_FIRST(&dev->children); 1318 child2 && child2 != child; 1319 child2 = TAILQ_NEXT(child2, link)) 1320 DEVICE_RESUME(child2); 1321 return (error); 1322 } 1323 } 1324 return (0); 1325} 1326 1327int 1328bus_generic_resume(device_t dev) 1329{ 1330 device_t child; 1331 1332 TAILQ_FOREACH(child, &dev->children, link) { 1333 DEVICE_RESUME(child); 1334 /* if resume fails, there's nothing we can usefully do... */ 1335 } 1336 return (0); 1337} 1338 1339int 1340bus_print_child_header (device_t dev, device_t child) 1341{ 1342 int retval = 0; 1343 1344 if (device_get_desc(child)) { 1345 retval += device_printf(child, "<%s>", device_get_desc(child)); 1346 } else { 1347 retval += printf("%s", device_get_nameunit(child)); 1348 } 1349 1350 return (retval); 1351} 1352 1353int 1354bus_print_child_footer (device_t dev, device_t child) 1355{ 1356 return (printf(" on %s\n", device_get_nameunit(dev))); 1357} 1358 1359int 1360bus_generic_print_child(device_t dev, device_t child) 1361{ 1362 int retval = 0; 1363 1364 retval += bus_print_child_header(dev, child); 1365 retval += bus_print_child_footer(dev, child); 1366 1367 return (retval); 1368} 1369 1370int 1371bus_generic_read_ivar(device_t dev, device_t child, int index, 1372 uintptr_t * result) 1373{ 1374 return (ENOENT); 1375} 1376 1377int 1378bus_generic_write_ivar(device_t dev, device_t child, int index, 1379 uintptr_t value) 1380{ 1381 return (ENOENT); 1382} 1383 1384struct resource_list * 1385bus_generic_get_resource_list (device_t dev, device_t child) 1386{ 1387 return (NULL); 1388} 1389 1390void 1391bus_generic_driver_added(device_t dev, driver_t *driver) 1392{ 1393 device_t child; 1394 1395 DEVICE_IDENTIFY(driver, dev); 1396 TAILQ_FOREACH(child, &dev->children, link) { 1397 if (child->state == DS_NOTPRESENT) 1398 device_probe_and_attach(child); 1399 } 1400} 1401 1402int 1403bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 1404 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1405{ 1406 /* Propagate up the bus hierarchy until someone handles it. */ 1407 if (dev->parent) 1408 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 1409 intr, arg, cookiep)); 1410 return (EINVAL); 1411} 1412 1413int 1414bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 1415 void *cookie) 1416{ 1417 /* Propagate up the bus hierarchy until someone handles it. */ 1418 if (dev->parent) 1419 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 1420 return (EINVAL); 1421} 1422 1423struct resource * 1424bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 1425 u_long start, u_long end, u_long count, u_int flags) 1426{ 1427 /* Propagate up the bus hierarchy until someone handles it. */ 1428 if (dev->parent) 1429 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 1430 start, end, count, flags)); 1431 return (NULL); 1432} 1433 1434int 1435bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 1436 struct resource *r) 1437{ 1438 /* Propagate up the bus hierarchy until someone handles it. */ 1439 if (dev->parent) 1440 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 1441 r)); 1442 return (EINVAL); 1443} 1444 1445int 1446bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 1447 struct resource *r) 1448{ 1449 /* Propagate up the bus hierarchy until someone handles it. */ 1450 if (dev->parent) 1451 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 1452 r)); 1453 return (EINVAL); 1454} 1455 1456int 1457bus_generic_deactivate_resource(device_t dev, device_t child, int type, 1458 int rid, struct resource *r) 1459{ 1460 /* Propagate up the bus hierarchy until someone handles it. */ 1461 if (dev->parent) 1462 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 1463 r)); 1464 return (EINVAL); 1465} 1466 1467int 1468bus_generic_rl_get_resource (device_t dev, device_t child, int type, int rid, 1469 u_long *startp, u_long *countp) 1470{ 1471 struct resource_list * rl = NULL; 1472 struct resource_list_entry * rle = NULL; 1473 1474 rl = BUS_GET_RESOURCE_LIST(dev, child); 1475 if (!rl) 1476 return (EINVAL); 1477 1478 rle = resource_list_find(rl, type, rid); 1479 if (!rle) 1480 return (ENOENT); 1481 1482 if (startp) 1483 *startp = rle->start; 1484 if (countp) 1485 *countp = rle->count; 1486 1487 return (0); 1488} 1489 1490int 1491bus_generic_rl_set_resource (device_t dev, device_t child, int type, int rid, 1492 u_long start, u_long count) 1493{ 1494 struct resource_list * rl = NULL; 1495 1496 rl = BUS_GET_RESOURCE_LIST(dev, child); 1497 if (!rl) 1498 return (EINVAL); 1499 1500 resource_list_add(rl, type, rid, start, (start + count - 1), count); 1501 1502 return (0); 1503} 1504 1505void 1506bus_generic_rl_delete_resource (device_t dev, device_t child, int type, int rid) 1507{ 1508 struct resource_list * rl = NULL; 1509 1510 rl = BUS_GET_RESOURCE_LIST(dev, child); 1511 if (!rl) 1512 return; 1513 1514 resource_list_delete(rl, type, rid); 1515 1516 return; 1517} 1518 1519int 1520bus_generic_rl_release_resource (device_t dev, device_t child, int type, 1521 int rid, struct resource *r) 1522{ 1523 struct resource_list * rl = NULL; 1524 1525 rl = BUS_GET_RESOURCE_LIST(dev, child); 1526 if (!rl) 1527 return (EINVAL); 1528 1529 return (resource_list_release(rl, dev, child, type, rid, r)); 1530} 1531 1532struct resource * 1533bus_generic_rl_alloc_resource (device_t dev, device_t child, int type, 1534 int *rid, u_long start, u_long end, u_long count, u_int flags) 1535{ 1536 struct resource_list * rl = NULL; 1537 1538 rl = BUS_GET_RESOURCE_LIST(dev, child); 1539 if (!rl) 1540 return (NULL); 1541 1542 return (resource_list_alloc(rl, dev, child, type, rid, 1543 start, end, count, flags)); 1544} 1545 1546/* 1547 * Some convenience functions to make it easier for drivers to use the 1548 * resource-management functions. All these really do is hide the 1549 * indirection through the parent's method table, making for slightly 1550 * less-wordy code. In the future, it might make sense for this code 1551 * to maintain some sort of a list of resources allocated by each device. 1552 */ 1553struct resource * 1554bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 1555 u_long count, u_int flags) 1556{ 1557 if (dev->parent == 0) 1558 return (0); 1559 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 1560 count, flags)); 1561} 1562 1563int 1564bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 1565{ 1566 if (dev->parent == 0) 1567 return (EINVAL); 1568 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 1569} 1570 1571int 1572bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 1573{ 1574 if (dev->parent == 0) 1575 return (EINVAL); 1576 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 1577} 1578 1579int 1580bus_release_resource(device_t dev, int type, int rid, struct resource *r) 1581{ 1582 if (dev->parent == 0) 1583 return (EINVAL); 1584 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 1585} 1586 1587int 1588bus_setup_intr(device_t dev, struct resource *r, int flags, 1589 driver_intr_t handler, void *arg, void **cookiep) 1590{ 1591 if (dev->parent == 0) 1592 return (EINVAL); 1593 return (BUS_SETUP_INTR(dev->parent, dev, r, flags, 1594 handler, arg, cookiep)); 1595} 1596 1597int 1598bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 1599{ 1600 if (dev->parent == 0) 1601 return (EINVAL); 1602 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 1603} 1604 1605int 1606bus_set_resource(device_t dev, int type, int rid, 1607 u_long start, u_long count) 1608{ 1609 return BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 1610 start, count); 1611} 1612 1613int 1614bus_get_resource(device_t dev, int type, int rid, 1615 u_long *startp, u_long *countp) 1616{ 1617 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 1618 startp, countp)); 1619} 1620 1621u_long 1622bus_get_resource_start(device_t dev, int type, int rid) 1623{ 1624 u_long start, count; 1625 int error; 1626 1627 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 1628 &start, &count); 1629 if (error) 1630 return (0); 1631 return (start); 1632} 1633 1634u_long 1635bus_get_resource_count(device_t dev, int type, int rid) 1636{ 1637 u_long start, count; 1638 int error; 1639 1640 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 1641 &start, &count); 1642 if (error) 1643 return (0); 1644 return (count); 1645} 1646 1647void 1648bus_delete_resource(device_t dev, int type, int rid) 1649{ 1650 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 1651} 1652 1653static int 1654root_print_child(device_t dev, device_t child) 1655{ 1656 int retval = 0; 1657 1658 retval += bus_print_child_header(dev, child); 1659 retval += printf("\n"); 1660 1661 return (retval); 1662} 1663 1664static int 1665root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 1666 void **cookiep) 1667{ 1668 /* 1669 * If an interrupt mapping gets to here something bad has happened. 1670 */ 1671 panic("root_setup_intr"); 1672} 1673 1674static kobj_method_t root_methods[] = { 1675 /* Device interface */ 1676 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 1677 KOBJMETHOD(device_suspend, bus_generic_suspend), 1678 KOBJMETHOD(device_resume, bus_generic_resume), 1679 1680 /* Bus interface */ 1681 KOBJMETHOD(bus_print_child, root_print_child), 1682 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 1683 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 1684 KOBJMETHOD(bus_setup_intr, root_setup_intr), 1685 1686 { 0, 0 } 1687}; 1688 1689static driver_t root_driver = { 1690 "root", 1691 root_methods, 1692 1, /* no softc */ 1693}; 1694 1695device_t root_bus; 1696devclass_t root_devclass; 1697 1698static int 1699root_bus_module_handler(module_t mod, int what, void* arg) 1700{ 1701 switch (what) { 1702 case MOD_LOAD: 1703 TAILQ_INIT(&bus_data_devices); 1704 kobj_class_compile((kobj_class_t) &root_driver); 1705 root_bus = make_device(NULL, "root", 0); 1706 root_bus->desc = "System root bus"; 1707 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 1708 root_bus->driver = &root_driver; 1709 root_bus->state = DS_ATTACHED; 1710 root_devclass = devclass_find_internal("root", FALSE); 1711 return (0); 1712 1713 case MOD_SHUTDOWN: 1714 device_shutdown(root_bus); 1715 return (0); 1716 } 1717 1718 return (0); 1719} 1720 1721static moduledata_t root_bus_mod = { 1722 "rootbus", 1723 root_bus_module_handler, 1724 0 1725}; 1726DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1727 1728void 1729root_bus_configure(void) 1730{ 1731 device_t dev; 1732 1733 PDEBUG((".")); 1734 1735 TAILQ_FOREACH(dev, &root_bus->children, link) { 1736 device_probe_and_attach(dev); 1737 } 1738} 1739 1740int 1741driver_module_handler(module_t mod, int what, void *arg) 1742{ 1743 int error, i; 1744 struct driver_module_data *dmd; 1745 devclass_t bus_devclass; 1746 1747 dmd = (struct driver_module_data *)arg; 1748 bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE); 1749 error = 0; 1750 1751 switch (what) { 1752 case MOD_LOAD: 1753 if (dmd->dmd_chainevh) 1754 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 1755 1756 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) { 1757 PDEBUG(("Loading module: driver %s on bus %s", 1758 DRIVERNAME(dmd->dmd_drivers[i]), dmd->dmd_busname)); 1759 error = devclass_add_driver(bus_devclass, 1760 dmd->dmd_drivers[i]); 1761 } 1762 if (error) 1763 break; 1764 1765 /* 1766 * The drivers loaded in this way are assumed to all 1767 * implement the same devclass. 1768 */ 1769 *dmd->dmd_devclass = 1770 devclass_find_internal(dmd->dmd_drivers[0]->name, TRUE); 1771 break; 1772 1773 case MOD_UNLOAD: 1774 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) { 1775 PDEBUG(("Unloading module: driver %s from bus %s", 1776 DRIVERNAME(dmd->dmd_drivers[i]), 1777 dmd->dmd_busname)); 1778 error = devclass_delete_driver(bus_devclass, 1779 dmd->dmd_drivers[i]); 1780 } 1781 1782 if (!error && dmd->dmd_chainevh) 1783 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 1784 break; 1785 } 1786 1787 return (error); 1788} 1789 1790#ifdef BUS_DEBUG 1791 1792/* the _short versions avoid iteration by not calling anything that prints 1793 * more than oneliners. I love oneliners. 1794 */ 1795 1796static void 1797print_device_short(device_t dev, int indent) 1798{ 1799 if (!dev) 1800 return; 1801 1802 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 1803 dev->unit, dev->desc, 1804 (dev->parent? "":"no "), 1805 (TAILQ_EMPTY(&dev->children)? "no ":""), 1806 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 1807 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 1808 (dev->flags&DF_WILDCARD? "wildcard,":""), 1809 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 1810 (dev->ivars? "":"no "), 1811 (dev->softc? "":"no "), 1812 dev->busy)); 1813} 1814 1815static void 1816print_device(device_t dev, int indent) 1817{ 1818 if (!dev) 1819 return; 1820 1821 print_device_short(dev, indent); 1822 1823 indentprintf(("Parent:\n")); 1824 print_device_short(dev->parent, indent+1); 1825 indentprintf(("Driver:\n")); 1826 print_driver_short(dev->driver, indent+1); 1827 indentprintf(("Devclass:\n")); 1828 print_devclass_short(dev->devclass, indent+1); 1829} 1830 1831void 1832print_device_tree_short(device_t dev, int indent) 1833/* print the device and all its children (indented) */ 1834{ 1835 device_t child; 1836 1837 if (!dev) 1838 return; 1839 1840 print_device_short(dev, indent); 1841 1842 TAILQ_FOREACH(child, &dev->children, link) { 1843 print_device_tree_short(child, indent+1); 1844 } 1845} 1846 1847void 1848print_device_tree(device_t dev, int indent) 1849/* print the device and all its children (indented) */ 1850{ 1851 device_t child; 1852 1853 if (!dev) 1854 return; 1855 1856 print_device(dev, indent); 1857 1858 TAILQ_FOREACH(child, &dev->children, link) { 1859 print_device_tree(child, indent+1); 1860 } 1861} 1862 1863static void 1864print_driver_short(driver_t *driver, int indent) 1865{ 1866 if (!driver) 1867 return; 1868 1869 indentprintf(("driver %s: softc size = %d\n", 1870 driver->name, driver->size)); 1871} 1872 1873static void 1874print_driver(driver_t *driver, int indent) 1875{ 1876 if (!driver) 1877 return; 1878 1879 print_driver_short(driver, indent); 1880} 1881 1882 1883static void 1884print_driver_list(driver_list_t drivers, int indent) 1885{ 1886 driverlink_t driver; 1887 1888 TAILQ_FOREACH(driver, &drivers, link) { 1889 print_driver(driver->driver, indent); 1890 } 1891} 1892 1893static void 1894print_devclass_short(devclass_t dc, int indent) 1895{ 1896 if ( !dc ) 1897 return; 1898 1899 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 1900} 1901 1902static void 1903print_devclass(devclass_t dc, int indent) 1904{ 1905 int i; 1906 1907 if ( !dc ) 1908 return; 1909 1910 print_devclass_short(dc, indent); 1911 indentprintf(("Drivers:\n")); 1912 print_driver_list(dc->drivers, indent+1); 1913 1914 indentprintf(("Devices:\n")); 1915 for (i = 0; i < dc->maxunit; i++) 1916 if (dc->devices[i]) 1917 print_device(dc->devices[i], indent+1); 1918} 1919 1920void 1921print_devclass_list_short(void) 1922{ 1923 devclass_t dc; 1924 1925 printf("Short listing of devclasses, drivers & devices:\n"); 1926 TAILQ_FOREACH(dc, &devclasses, link) { 1927 print_devclass_short(dc, 0); 1928 } 1929} 1930 1931void 1932print_devclass_list(void) 1933{ 1934 devclass_t dc; 1935 1936 printf("Full listing of devclasses, drivers & devices:\n"); 1937 TAILQ_FOREACH(dc, &devclasses, link) { 1938 print_devclass(dc, 0); 1939 } 1940} 1941 1942#endif 1943 1944/* 1945 * User-space access to the device tree. 1946 * 1947 * We implement a small set of nodes: 1948 * 1949 * hw.bus Single integer read method to obtain the 1950 * current generation count. 1951 * hw.bus.devices Reads the entire device tree in flat space. 1952 * hw.bus.rman Resource manager interface 1953 * 1954 * We might like to add the ability to scan devclasses and/or drivers to 1955 * determine what else is currently loaded/available. 1956 */ 1957SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL); 1958 1959static int 1960sysctl_bus(SYSCTL_HANDLER_ARGS) 1961{ 1962 struct u_businfo ubus; 1963 1964 ubus.ub_version = BUS_USER_VERSION; 1965 ubus.ub_generation = bus_data_generation; 1966 1967 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 1968} 1969SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 1970 "bus-related data"); 1971 1972static int 1973sysctl_devices(SYSCTL_HANDLER_ARGS) 1974{ 1975 int *name = (int *)arg1; 1976 u_int namelen = arg2; 1977 int index; 1978 struct device *dev; 1979 struct u_device udev; /* XXX this is a bit big */ 1980 int error; 1981 1982 if (namelen != 2) 1983 return (EINVAL); 1984 1985 if (bus_data_generation_check(name[0])) 1986 return (EINVAL); 1987 1988 index = name[1]; 1989 1990 /* 1991 * Scan the list of devices, looking for the requested index. 1992 */ 1993 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 1994 if (index-- == 0) 1995 break; 1996 } 1997 if (dev == NULL) 1998 return (ENOENT); 1999 2000 /* 2001 * Populate the return array. 2002 */ 2003 udev.dv_handle = (uintptr_t)dev; 2004 udev.dv_parent = (uintptr_t)dev->parent; 2005 if (dev->nameunit == NULL) { 2006 udev.dv_name[0] = 0; 2007 } else { 2008 snprintf(udev.dv_name, 32, "%s", dev->nameunit); 2009 } 2010 if (dev->desc == NULL) { 2011 udev.dv_desc[0] = 0; 2012 } else { 2013 snprintf(udev.dv_desc, 32, "%s", dev->desc); 2014 } 2015 if ((dev->driver == NULL) || (dev->driver->name == NULL)) { 2016 udev.dv_drivername[0] = 0; 2017 } else { 2018 snprintf(udev.dv_drivername, 32, "%s", dev->driver->name); 2019 } 2020 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 2021 return (error); 2022} 2023 2024SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 2025 "system device tree"); 2026 2027/* 2028 * Sysctl interface for scanning the resource lists. 2029 * 2030 * We take two input parameters; the index into the list of resource 2031 * managers, and the resource offset into the list. 2032 */ 2033static int 2034sysctl_rman(SYSCTL_HANDLER_ARGS) 2035{ 2036 int *name = (int *)arg1; 2037 u_int namelen = arg2; 2038 int rman_idx, res_idx; 2039 struct rman *rm; 2040 struct resource *res; 2041 struct u_rman urm; 2042 struct u_resource ures; 2043 int error; 2044 2045 if (namelen != 3) 2046 return (EINVAL); 2047 2048 if (bus_data_generation_check(name[0])) 2049 return (EINVAL); 2050 rman_idx = name[1]; 2051 res_idx = name[2]; 2052 2053 /* 2054 * Find the indexed resource manager 2055 */ 2056 TAILQ_FOREACH(rm, &rman_head, rm_link) { 2057 if (rman_idx-- == 0) 2058 break; 2059 } 2060 if (rm == NULL) 2061 return (ENOENT); 2062 2063 /* 2064 * If the resource index is -1, we want details on the 2065 * resource manager. 2066 */ 2067 if (res_idx == -1) { 2068 urm.rm_handle = (uintptr_t)rm; 2069 snprintf(urm.rm_descr, RM_TEXTLEN, "%s", rm->rm_descr); 2070 urm.rm_descr[RM_TEXTLEN - 1] = '\0'; 2071 urm.rm_start = rm->rm_start; 2072 urm.rm_size = rm->rm_end - rm->rm_start + 1; 2073 urm.rm_type = rm->rm_type; 2074 2075 error = SYSCTL_OUT(req, &urm, sizeof(urm)); 2076 return (error); 2077 } 2078 2079 /* 2080 * Find the indexed resource and return it. 2081 */ 2082 TAILQ_FOREACH(res, &rm->rm_list, r_link) { 2083 if (res_idx-- == 0) { 2084 ures.r_handle = (uintptr_t)res; 2085 ures.r_parent = (uintptr_t)res->r_rm; 2086 ures.r_device = (uintptr_t)res->r_dev; 2087 if (res->r_dev != NULL) { 2088 if (device_get_name(res->r_dev) != NULL) { 2089 snprintf(ures.r_devname, RM_TEXTLEN, 2090 "%s%d", 2091 device_get_name(res->r_dev), 2092 device_get_unit(res->r_dev)); 2093 } else { 2094 snprintf(ures.r_devname, RM_TEXTLEN, 2095 "nomatch"); 2096 } 2097 } else { 2098 ures.r_devname[0] = 0; 2099 } 2100 ures.r_start = res->r_start; 2101 ures.r_size = res->r_end - res->r_start + 1; 2102 ures.r_flags = res->r_flags; 2103 2104 error = SYSCTL_OUT(req, &ures, sizeof(ures)); 2105 return (error); 2106 } 2107 } 2108 return (ENOENT); 2109} 2110 2111SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman, 2112 "kernel resource manager"); 2113 2114int 2115bus_data_generation_check(int generation) 2116{ 2117 if (generation != bus_data_generation) 2118 return (1); 2119 2120 /* XXX generate optimised lists here? */ 2121 return (0); 2122} 2123 2124void 2125bus_data_generation_update(void) 2126{ 2127 bus_data_generation++; 2128} 2129 2130/*======================================*/ 2131/* 2132 * Access functions for device resources. 2133 */ 2134 2135extern char static_hints[]; /* by config for now */ 2136extern int hintmode; /* 0 = off. 1 = config, 2 = fallback */ 2137static char *hintp; 2138 2139/* 2140 * Evil wildcarding resource string lookup. 2141 * This walks the supplied env string table and returns a match. 2142 * The start point can be remembered for incremental searches. 2143 */ 2144static int 2145res_find(int *line, int *startln, 2146 const char *name, int *unit, const char *resname, const char *value, 2147 const char **ret_name, int *ret_namelen, int *ret_unit, 2148 const char **ret_resname, int *ret_resnamelen, const char **ret_value) 2149{ 2150 int n = 0, hit; 2151 char r_name[32]; 2152 int r_unit; 2153 char r_resname[32]; 2154 char r_value[128]; 2155 const char *s, *cp; 2156 char *p; 2157 2158 if (hintp == NULL) { 2159 switch (hintmode) { 2160 case 0: /* config supplied nothing */ 2161 hintp = kern_envp; 2162 break; 2163 case 1: /* static hints only */ 2164 hintp = static_hints; 2165 break; 2166 case 2: /* fallback mode */ 2167 cp = kern_envp; 2168 while (cp) { 2169 if (strncmp(cp, "hint.", 5) == 0) { 2170 cp = NULL; 2171 hintp = kern_envp; 2172 break; 2173 } 2174 while (*cp != '\0') 2175 cp++; 2176 cp++; 2177 if (*cp == '\0') { 2178 cp = NULL; 2179 hintp = static_hints; 2180 break; 2181 } 2182 } 2183 break; 2184 default: 2185 break; 2186 } 2187 if (hintp == NULL) 2188 hintp = kern_envp; 2189 } 2190 2191 cp = hintp; 2192 while (cp) { 2193 hit = 1; 2194 (*line)++; 2195 if (strncmp(cp, "hint.", 5) != 0) 2196 hit = 0; 2197 else 2198 n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%128s", 2199 r_name, &r_unit, r_resname, r_value); 2200 if (hit && n != 4) { 2201 printf("CONFIG: invalid hint '%s'\n", cp); 2202 /* XXX: abuse bogus index() declaration */ 2203 p = index(cp, 'h'); 2204 *p = 'H'; 2205 hit = 0; 2206 } 2207 if (hit && startln && *startln >= 0 && *line < *startln) 2208 hit = 0; 2209 if (hit && name && strcmp(name, r_name) != 0) 2210 hit = 0; 2211 if (hit && unit && *unit != r_unit) 2212 hit = 0; 2213 if (hit && resname && strcmp(resname, r_resname) != 0) 2214 hit = 0; 2215 if (hit && value && strcmp(value, r_value) != 0) 2216 hit = 0; 2217 if (hit) 2218 break; 2219 while (*cp != '\0') 2220 cp++; 2221 cp++; 2222 if (*cp == '\0') { 2223 cp = NULL; 2224 break; 2225 } 2226 } 2227 if (cp == NULL) 2228 return ENOENT; 2229 2230 s = cp; 2231 /* This is a bit of a hack, but at least is reentrant */ 2232 /* Note that it returns some !unterminated! strings. */ 2233 s = index(s, '.') + 1; /* start of device */ 2234 if (ret_name) 2235 *ret_name = s; 2236 s = index(s, '.') + 1; /* start of unit */ 2237 if (ret_namelen) 2238 *ret_namelen = s - *ret_name - 1; /* device length */ 2239 if (ret_unit) 2240 *ret_unit = r_unit; 2241 s = index(s, '.') + 1; /* start of resname */ 2242 if (ret_resname) 2243 *ret_resname = s; 2244 s = index(s, '=') + 1; /* start of value */ 2245 if (ret_resnamelen) 2246 *ret_resnamelen = s - *ret_resname - 1; /* value len */ 2247 if (ret_value) 2248 *ret_value = s; 2249 if (startln) /* line number for anchor */ 2250 *startln = *line + 1; 2251 return 0; 2252} 2253 2254/* 2255 * Search all the data sources for matches to our query. We look for 2256 * dynamic hints first as overrides for static or fallback hints. 2257 */ 2258static int 2259resource_find(int *line, int *startln, 2260 const char *name, int *unit, const char *resname, const char *value, 2261 const char **ret_name, int *ret_namelen, int *ret_unit, 2262 const char **ret_resname, int *ret_resnamelen, const char **ret_value) 2263{ 2264 int i; 2265 int un; 2266 2267 *line = 0; 2268 2269 /* Search for exact unit matches first */ 2270 i = res_find(line, startln, name, unit, resname, value, 2271 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen, 2272 ret_value); 2273 if (i == 0) 2274 return 0; 2275 if (unit == NULL) 2276 return ENOENT; 2277 /* If we are still here, search for wildcard matches */ 2278 un = -1; 2279 i = res_find(line, startln, name, &un, resname, value, 2280 ret_name, ret_namelen, ret_unit, ret_resname, ret_resnamelen, 2281 ret_value); 2282 if (i == 0) 2283 return 0; 2284 return ENOENT; 2285} 2286 2287int 2288resource_int_value(const char *name, int unit, const char *resname, int *result) 2289{ 2290 int error; 2291 const char *str; 2292 char *op; 2293 unsigned long val; 2294 int line; 2295 2296 line = 0; 2297 error = resource_find(&line, NULL, name, &unit, resname, NULL, 2298 NULL, NULL, NULL, NULL, NULL, &str); 2299 if (error) 2300 return error; 2301 if (*str == '\0') 2302 return EFTYPE; 2303 val = strtoul(str, &op, 0); 2304 if (*op != '\0') 2305 return EFTYPE; 2306 *result = val; 2307 return 0; 2308} 2309 2310int 2311resource_long_value(const char *name, int unit, const char *resname, 2312 long *result) 2313{ 2314 int error; 2315 const char *str; 2316 char *op; 2317 unsigned long val; 2318 int line; 2319 2320 line = 0; 2321 error = resource_find(&line, NULL, name, &unit, resname, NULL, 2322 NULL, NULL, NULL, NULL, NULL, &str); 2323 if (error) 2324 return error; 2325 if (*str == '\0') 2326 return EFTYPE; 2327 val = strtoul(str, &op, 0); 2328 if (*op != '\0') 2329 return EFTYPE; 2330 *result = val; 2331 return 0; 2332} 2333 2334int 2335resource_string_value(const char *name, int unit, const char *resname, 2336 const char **result) 2337{ 2338 int error; 2339 const char *str; 2340 int line; 2341 2342 line = 0; 2343 error = resource_find(&line, NULL, name, &unit, resname, NULL, 2344 NULL, NULL, NULL, NULL, NULL, &str); 2345 if (error) 2346 return error; 2347 *result = str; 2348 return 0; 2349} 2350 2351/* 2352 * This is a bit nasty, but allows us to not modify the env strings. 2353 */ 2354static const char * 2355resource_string_copy(const char *s, int len) 2356{ 2357 static char stringbuf[256]; 2358 static int offset = 0; 2359 const char *ret; 2360 2361 if (len == 0) 2362 len = strlen(s); 2363 if (len > 255) 2364 return NULL; 2365 if ((offset + len + 1) > 255) 2366 offset = 0; 2367 bcopy(s, &stringbuf[offset], len); 2368 stringbuf[offset + len] = '\0'; 2369 ret = &stringbuf[offset]; 2370 offset += len + 1; 2371 return ret; 2372} 2373 2374/* 2375 * err = resource_find_at(&anchor, &name, &unit, resname, value) 2376 * Iteratively fetch a list of devices wired "at" something 2377 * res and value are restrictions. eg: "at", "scbus0". 2378 * For practical purposes, res = required, value = optional. 2379 * *name and *unit are set. 2380 * set *anchor to zero before starting. 2381 */ 2382int 2383resource_find_match(int *anchor, const char **name, int *unit, 2384 const char *resname, const char *value) 2385{ 2386 const char *found_name; 2387 int found_namelen; 2388 int found_unit; 2389 int ret; 2390 int newln; 2391 2392 newln = *anchor; 2393 ret = resource_find(anchor, &newln, NULL, NULL, resname, value, 2394 &found_name, &found_namelen, &found_unit, NULL, NULL, NULL); 2395 if (ret == 0) { 2396 *name = resource_string_copy(found_name, found_namelen); 2397 *unit = found_unit; 2398 } 2399 *anchor = newln; 2400 return ret; 2401} 2402 2403 2404/* 2405 * err = resource_find_dev(&anchor, name, &unit, res, value); 2406 * Iterate through a list of devices, returning their unit numbers. 2407 * res and value are optional restrictions. eg: "at", "scbus0". 2408 * *unit is set to the value. 2409 * set *anchor to zero before starting. 2410 */ 2411int 2412resource_find_dev(int *anchor, const char *name, int *unit, 2413 const char *resname, const char *value) 2414{ 2415 int found_unit; 2416 int newln; 2417 int ret; 2418 2419 newln = *anchor; 2420 ret = resource_find(anchor, &newln, name, NULL, resname, value, 2421 NULL, NULL, &found_unit, NULL, NULL, NULL); 2422 if (ret == 0) { 2423 *unit = found_unit; 2424 } 2425 *anchor = newln; 2426 return ret; 2427} 2428