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