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