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