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