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 TAILQ_HEAD(,device) bus_data_devices; 95 static int bus_data_generation = 1; 96 97 kobj_method_t null_methods[] = { 98 { 0, 0 } 99 }; 100 101 DEFINE_CLASS(null, null_methods, 0); 102 103 /* 104 * Devclass implementation 105 */ 106 107 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 108 109 static devclass_t 110 devclass_find_internal(const char *classname, int create) 111 { 112 devclass_t dc; 113 114 PDEBUG(("looking for %s", classname)); 115 if (!classname) 116 return (NULL); 117 118 TAILQ_FOREACH(dc, &devclasses, link) { 119 if (!strcmp(dc->name, classname)) 120 return (dc); 121 } 122 123 PDEBUG(("%s not found%s", classname, (create? ", creating": ""))); 124 if (create) { 125 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 126 M_BUS, M_NOWAIT|M_ZERO); 127 if (!dc) 128 return (NULL); 129 dc->name = (char*) (dc + 1); 130 strcpy(dc->name, classname); 131 TAILQ_INIT(&dc->drivers); 132 TAILQ_INSERT_TAIL(&devclasses, dc, link); 133 134 bus_data_generation_update(); 135 } 136 137 return (dc); 138 } 139 140 devclass_t 141 devclass_create(const char *classname) 142 { 143 return (devclass_find_internal(classname, TRUE)); 144 } 145 146 devclass_t 147 devclass_find(const char *classname) 148 { 149 return (devclass_find_internal(classname, FALSE)); 150 } 151 152 int 153 devclass_add_driver(devclass_t dc, driver_t *driver) 154 { 155 driverlink_t dl; 156 int i; 157 158 PDEBUG(("%s", DRIVERNAME(driver))); 159 160 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO); 161 if (!dl) 162 return (ENOMEM); 163 164 /* 165 * Compile the driver's methods. Also increase the reference count 166 * so that the class doesn't get freed when the last instance 167 * goes. This means we can safely use static methods and avoids a 168 * double-free in devclass_delete_driver. 169 */ 170 kobj_class_compile((kobj_class_t) driver); 171 172 /* 173 * Make sure the devclass which the driver is implementing exists. 174 */ 175 devclass_find_internal(driver->name, TRUE); 176 177 dl->driver = driver; 178 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 179 driver->refs++; 180 181 /* 182 * Call BUS_DRIVER_ADDED for any existing busses in this class. 183 */ 184 for (i = 0; i < dc->maxunit; i++) 185 if (dc->devices[i]) 186 BUS_DRIVER_ADDED(dc->devices[i], driver); 187 188 bus_data_generation_update(); 189 return (0); 190 } 191 192 int 193 devclass_delete_driver(devclass_t busclass, driver_t *driver) 194 { 195 devclass_t dc = devclass_find(driver->name); 196 driverlink_t dl; 197 device_t dev; 198 int i; 199 int error; 200 201 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 202 203 if (!dc) 204 return (0); 205 206 /* 207 * Find the link structure in the bus' list of drivers. 208 */ 209 TAILQ_FOREACH(dl, &busclass->drivers, link) { 210 if (dl->driver == driver) 211 break; 212 } 213 214 if (!dl) { 215 PDEBUG(("%s not found in %s list", driver->name, 216 busclass->name)); 217 return (ENOENT); 218 } 219 220 /* 221 * Disassociate from any devices. We iterate through all the 222 * devices in the devclass of the driver and detach any which are 223 * using the driver and which have a parent in the devclass which 224 * we are deleting from. 225 * 226 * Note that since a driver can be in multiple devclasses, we 227 * should not detach devices which are not children of devices in 228 * the affected devclass. 229 */ 230 for (i = 0; i < dc->maxunit; i++) { 231 if (dc->devices[i]) { 232 dev = dc->devices[i]; 233 if (dev->driver == driver && dev->parent && 234 dev->parent->devclass == busclass) { 235 if ((error = device_detach(dev)) != 0) 236 return (error); 237 device_set_driver(dev, NULL); 238 } 239 } 240 } 241 242 TAILQ_REMOVE(&busclass->drivers, dl, link); 243 free(dl, M_BUS); 244 245 driver->refs--; 246 if (driver->refs == 0) 247 kobj_class_free((kobj_class_t) driver); 248 249 bus_data_generation_update(); 250 return (0); 251 } 252 253 static driverlink_t 254 devclass_find_driver_internal(devclass_t dc, const char *classname) 255 { 256 driverlink_t dl; 257 258 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 259 260 TAILQ_FOREACH(dl, &dc->drivers, link) { 261 if (!strcmp(dl->driver->name, classname)) 262 return (dl); 263 } 264 265 PDEBUG(("not found")); 266 return (NULL); 267 } 268 269 driver_t * 270 devclass_find_driver(devclass_t dc, const char *classname) 271 { 272 driverlink_t dl; 273 274 dl = devclass_find_driver_internal(dc, classname); 275 if (dl) 276 return (dl->driver); 277 return (NULL); 278 } 279 280 const char * 281 devclass_get_name(devclass_t dc) 282 { 283 return (dc->name); 284 } 285 286 device_t 287 devclass_get_device(devclass_t dc, int unit) 288 { 289 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 290 return (NULL); 291 return (dc->devices[unit]); 292 } 293 294 void * 295 devclass_get_softc(devclass_t dc, int unit) 296 { 297 device_t dev; 298 299 dev = devclass_get_device(dc, unit); 300 if (!dev) 301 return (NULL); 302 303 return (device_get_softc(dev)); 304 } 305 306 int 307 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 308 { 309 int i; 310 int count; 311 device_t *list; 312 313 count = 0; 314 for (i = 0; i < dc->maxunit; i++) 315 if (dc->devices[i]) 316 count++; 317 318 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 319 if (!list) 320 return (ENOMEM); 321 322 count = 0; 323 for (i = 0; i < dc->maxunit; i++) { 324 if (dc->devices[i]) { 325 list[count] = dc->devices[i]; 326 count++; 327 } 328 } 329 330 *devlistp = list; 331 *devcountp = count; 332 333 return (0); 334 } 335 336 int 337 devclass_get_maxunit(devclass_t dc) 338 { 339 return (dc->maxunit); 340 } 341 342 int 343 devclass_find_free_unit(devclass_t dc, int unit) 344 { 345 if (dc == NULL) 346 return (unit); 347 while (unit < dc->maxunit && dc->devices[unit] != NULL) 348 unit++; 349 return (unit); 350 } 351 352 static int 353 devclass_alloc_unit(devclass_t dc, int *unitp) 354 { 355 int unit = *unitp; 356 357 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 358 359 /* If we were given a wired unit number, check for existing device */ 360 /* XXX imp XXX */ 361 if (unit != -1) { 362 if (unit >= 0 && unit < dc->maxunit && 363 dc->devices[unit] != NULL) { 364 printf("%s-: %s%d already exists, skipping it\n", 365 dc->name, dc->name, *unitp); 366 return (EEXIST); 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 (NULL); 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 if (devclass_add_device(dc, dev)) { 486 kobj_delete((kobj_t) dev, M_BUS); 487 return (NULL); 488 } 489 } 490 dev->ivars = NULL; 491 dev->softc = NULL; 492 493 dev->state = DS_NOTPRESENT; 494 495 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 496 bus_data_generation_update(); 497 498 return (dev); 499 } 500 501 static int 502 device_print_child(device_t dev, device_t child) 503 { 504 int retval = 0; 505 506 if (device_is_alive(child)) 507 retval += BUS_PRINT_CHILD(dev, child); 508 else 509 retval += device_printf(child, " not found\n"); 510 511 return (retval); 512 } 513 514 device_t 515 device_add_child(device_t dev, const char *name, int unit) 516 { 517 return (device_add_child_ordered(dev, 0, name, unit)); 518 } 519 520 device_t 521 device_add_child_ordered(device_t dev, int order, const char *name, int unit) 522 { 523 device_t child; 524 device_t place; 525 526 PDEBUG(("%s at %s with order %d as unit %d", 527 name, DEVICENAME(dev), order, unit)); 528 529 child = make_device(dev, name, unit); 530 if (child == NULL) 531 return (child); 532 child->order = order; 533 534 TAILQ_FOREACH(place, &dev->children, link) { 535 if (place->order > order) 536 break; 537 } 538 539 if (place) { 540 /* 541 * The device 'place' is the first device whose order is 542 * greater than the new child. 543 */ 544 TAILQ_INSERT_BEFORE(place, child, link); 545 } else { 546 /* 547 * The new child's order is greater or equal to the order of 548 * any existing device. Add the child to the tail of the list. 549 */ 550 TAILQ_INSERT_TAIL(&dev->children, child, link); 551 } 552 553 bus_data_generation_update(); 554 return (child); 555 } 556 557 int 558 device_delete_child(device_t dev, device_t child) 559 { 560 int error; 561 device_t grandchild; 562 563 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 564 565 /* remove children first */ 566 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 567 error = device_delete_child(child, grandchild); 568 if (error) 569 return (error); 570 } 571 572 if ((error = device_detach(child)) != 0) 573 return (error); 574 if (child->devclass) 575 devclass_delete_device(child->devclass, child); 576 TAILQ_REMOVE(&dev->children, child, link); 577 TAILQ_REMOVE(&bus_data_devices, child, devlink); 578 device_set_desc(child, NULL); 579 free(child, M_BUS); 580 581 bus_data_generation_update(); 582 return (0); 583 } 584 585 /* 586 * Find only devices attached to this bus. 587 */ 588 device_t 589 device_find_child(device_t dev, const char *classname, int unit) 590 { 591 devclass_t dc; 592 device_t child; 593 594 dc = devclass_find(classname); 595 if (!dc) 596 return (NULL); 597 598 child = devclass_get_device(dc, unit); 599 if (child && child->parent == dev) 600 return (child); 601 return (NULL); 602 } 603 604 static driverlink_t 605 first_matching_driver(devclass_t dc, device_t dev) 606 { 607 if (dev->devclass) 608 return (devclass_find_driver_internal(dc, dev->devclass->name)); 609 return (TAILQ_FIRST(&dc->drivers)); 610 } 611 612 static driverlink_t 613 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 614 { 615 if (dev->devclass) { 616 driverlink_t dl; 617 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 618 if (!strcmp(dev->devclass->name, dl->driver->name)) 619 return (dl); 620 return (NULL); 621 } 622 return (TAILQ_NEXT(last, link)); 623 } 624 625 static int 626 device_probe_child(device_t dev, device_t child) 627 { 628 devclass_t dc; 629 driverlink_t best = 0; 630 driverlink_t dl; 631 int result, pri = 0; 632 int hasclass = (child->devclass != 0); 633 634 dc = dev->devclass; 635 if (!dc) 636 panic("device_probe_child: parent device has no devclass"); 637 638 if (child->state == DS_ALIVE) 639 return (0); 640 641 for (dl = first_matching_driver(dc, child); 642 dl; 643 dl = next_matching_driver(dc, child, dl)) { 644 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 645 device_set_driver(child, dl->driver); 646 if (!hasclass) 647 device_set_devclass(child, dl->driver->name); 648 result = DEVICE_PROBE(child); 649 if (!hasclass) 650 device_set_devclass(child, 0); 651 652 /* 653 * If the driver returns SUCCESS, there can be no higher match 654 * for this device. 655 */ 656 if (result == 0) { 657 best = dl; 658 pri = 0; 659 break; 660 } 661 662 /* 663 * The driver returned an error so it certainly doesn't match. 664 */ 665 if (result > 0) { 666 device_set_driver(child, 0); 667 continue; 668 } 669 670 /* 671 * A priority lower than SUCCESS, remember the best matching 672 * driver. Initialise the value of pri for the first match. 673 */ 674 if (best == 0 || result > pri) { 675 best = dl; 676 pri = result; 677 continue; 678 } 679 } 680 681 /* 682 * If we found a driver, change state and initialise the devclass. 683 */ 684 if (best) { 685 if (!child->devclass) 686 device_set_devclass(child, best->driver->name); 687 device_set_driver(child, best->driver); 688 if (pri < 0) { 689 /* 690 * A bit bogus. Call the probe method again to make 691 * sure that we have the right description. 692 */ 693 DEVICE_PROBE(child); 694 } 695 child->state = DS_ALIVE; 696 697 bus_data_generation_update(); 698 return (0); 699 } 700 701 return (ENXIO); 702 } 703 704 device_t 705 device_get_parent(device_t dev) 706 { 707 return (dev->parent); 708 } 709 710 int 711 device_get_children(device_t dev, device_t **devlistp, int *devcountp) 712 { 713 int count; 714 device_t child; 715 device_t *list; 716 717 count = 0; 718 TAILQ_FOREACH(child, &dev->children, link) { 719 count++; 720 } 721 722 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 723 if (!list) 724 return (ENOMEM); 725 726 count = 0; 727 TAILQ_FOREACH(child, &dev->children, link) { 728 list[count] = child; 729 count++; 730 } 731 732 *devlistp = list; 733 *devcountp = count; 734 735 return (0); 736 } 737 738 driver_t * 739 device_get_driver(device_t dev) 740 { 741 return (dev->driver); 742 } 743 744 devclass_t 745 device_get_devclass(device_t dev) 746 { 747 return (dev->devclass); 748 } 749 750 const char * 751 device_get_name(device_t dev) 752 { 753 if (dev->devclass) 754 return (devclass_get_name(dev->devclass)); 755 return (NULL); 756 } 757 758 const char * 759 device_get_nameunit(device_t dev) 760 { 761 return (dev->nameunit); 762 } 763 764 int 765 device_get_unit(device_t dev) 766 { 767 return (dev->unit); 768 } 769 770 const char * 771 device_get_desc(device_t dev) 772 { 773 return (dev->desc); 774 } 775 776 u_int32_t 777 device_get_flags(device_t dev) 778 { 779 return (dev->devflags); 780 } 781 782 int 783 device_print_prettyname(device_t dev) 784 { 785 const char *name = device_get_name(dev); 786 787 if (name == 0) 788 return (printf("unknown: ")); 789 return (printf("%s%d: ", name, device_get_unit(dev))); 790 } 791 792 int 793 device_printf(device_t dev, const char * fmt, ...) 794 { 795 va_list ap; 796 int retval; 797 798 retval = device_print_prettyname(dev); 799 va_start(ap, fmt); 800 retval += vprintf(fmt, ap); 801 va_end(ap); 802 return (retval); 803 } 804 805 static void 806 device_set_desc_internal(device_t dev, const char* desc, int copy) 807 { 808 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 809 free(dev->desc, M_BUS); 810 dev->flags &= ~DF_DESCMALLOCED; 811 dev->desc = NULL; 812 } 813 814 if (copy && desc) { 815 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 816 if (dev->desc) { 817 strcpy(dev->desc, desc); 818 dev->flags |= DF_DESCMALLOCED; 819 } 820 } else { 821 /* Avoid a -Wcast-qual warning */ 822 dev->desc = (char *)(uintptr_t) desc; 823 } 824 825 bus_data_generation_update(); 826 } 827 828 void 829 device_set_desc(device_t dev, const char* desc) 830 { 831 device_set_desc_internal(dev, desc, FALSE); 832 } 833 834 void 835 device_set_desc_copy(device_t dev, const char* desc) 836 { 837 device_set_desc_internal(dev, desc, TRUE); 838 } 839 840 void 841 device_set_flags(device_t dev, u_int32_t flags) 842 { 843 dev->devflags = flags; 844 } 845 846 void * 847 device_get_softc(device_t dev) 848 { 849 return (dev->softc); 850 } 851 852 void 853 device_set_softc(device_t dev, void *softc) 854 { 855 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 856 free(dev->softc, M_BUS); 857 dev->softc = softc; 858 if (dev->softc) 859 dev->flags |= DF_EXTERNALSOFTC; 860 else 861 dev->flags &= ~DF_EXTERNALSOFTC; 862 } 863 864 void * 865 device_get_ivars(device_t dev) 866 { 867 return (dev->ivars); 868 } 869 870 void 871 device_set_ivars(device_t dev, void * ivars) 872 { 873 if (!dev) 874 return; 875 876 dev->ivars = ivars; 877 878 return; 879 } 880 881 device_state_t 882 device_get_state(device_t dev) 883 { 884 return (dev->state); 885 } 886 887 void 888 device_enable(device_t dev) 889 { 890 dev->flags |= DF_ENABLED; 891 } 892 893 void 894 device_disable(device_t dev) 895 { 896 dev->flags &= ~DF_ENABLED; 897 } 898 899 void 900 device_busy(device_t dev) 901 { 902 if (dev->state < DS_ATTACHED) 903 panic("device_busy: called for unattached device"); 904 if (dev->busy == 0 && dev->parent) 905 device_busy(dev->parent); 906 dev->busy++; 907 dev->state = DS_BUSY; 908 } 909 910 void 911 device_unbusy(device_t dev) 912 { 913 if (dev->state != DS_BUSY) 914 panic("device_unbusy: called for non-busy device"); 915 dev->busy--; 916 if (dev->busy == 0) { 917 if (dev->parent) 918 device_unbusy(dev->parent); 919 dev->state = DS_ATTACHED; 920 } 921 } 922 923 void 924 device_quiet(device_t dev) 925 { 926 dev->flags |= DF_QUIET; 927 } 928 929 void 930 device_verbose(device_t dev) 931 { 932 dev->flags &= ~DF_QUIET; 933 } 934 935 int 936 device_is_quiet(device_t dev) 937 { 938 return ((dev->flags & DF_QUIET) != 0); 939 } 940 941 int 942 device_is_enabled(device_t dev) 943 { 944 return ((dev->flags & DF_ENABLED) != 0); 945 } 946 947 int 948 device_is_alive(device_t dev) 949 { 950 return (dev->state >= DS_ALIVE); 951 } 952 953 int 954 device_set_devclass(device_t dev, const char *classname) 955 { 956 devclass_t dc; 957 int error; 958 959 if (!classname) { 960 if (dev->devclass) 961 devclass_delete_device(dev->devclass, dev); 962 return (0); 963 } 964 965 if (dev->devclass) { 966 printf("device_set_devclass: device class already set\n"); 967 return (EINVAL); 968 } 969 970 dc = devclass_find_internal(classname, TRUE); 971 if (!dc) 972 return (ENOMEM); 973 974 error = devclass_add_device(dc, dev); 975 976 bus_data_generation_update(); 977 return (error); 978 } 979 980 int 981 device_set_driver(device_t dev, driver_t *driver) 982 { 983 if (dev->state >= DS_ATTACHED) 984 return (EBUSY); 985 986 if (dev->driver == driver) 987 return (0); 988 989 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 990 free(dev->softc, M_BUS); 991 dev->softc = NULL; 992 } 993 kobj_delete((kobj_t) dev, 0); 994 dev->driver = driver; 995 if (driver) { 996 kobj_init((kobj_t) dev, (kobj_class_t) driver); 997 if (!(dev->flags & DF_EXTERNALSOFTC)) { 998 dev->softc = malloc(driver->size, M_BUS, 999 M_NOWAIT | M_ZERO); 1000 if (!dev->softc) { 1001 kobj_init((kobj_t) dev, &null_class); 1002 dev->driver = NULL; 1003 return (ENOMEM); 1004 } 1005 } 1006 } else { 1007 kobj_init((kobj_t) dev, &null_class); 1008 } 1009 1010 bus_data_generation_update(); 1011 return (0); 1012 } 1013 1014 int 1015 device_probe_and_attach(device_t dev) 1016 { 1017 device_t bus = dev->parent; 1018 int error = 0; 1019 int hasclass = (dev->devclass != 0); 1020 1021 if (dev->state >= DS_ALIVE) 1022 return (0); 1023 1024 if (dev->flags & DF_ENABLED) { 1025 error = device_probe_child(bus, dev); 1026 if (!error) { 1027 if (!device_is_quiet(dev)) 1028 device_print_child(bus, dev); 1029 error = DEVICE_ATTACH(dev); 1030 if (!error) 1031 dev->state = DS_ATTACHED; 1032 else { 1033 printf("device_probe_and_attach: %s%d attach returned %d\n", 1034 dev->driver->name, dev->unit, error); 1035 /* Unset the class; set in device_probe_child */ 1036 if (!hasclass) 1037 device_set_devclass(dev, 0); 1038 device_set_driver(dev, NULL); 1039 dev->state = DS_NOTPRESENT; 1040 } 1041 } else { 1042 if (!(dev->flags & DF_DONENOMATCH)) { 1043 BUS_PROBE_NOMATCH(bus, dev); 1044 dev->flags |= DF_DONENOMATCH; 1045 } 1046 } 1047 } else { 1048 if (bootverbose) { 1049 device_print_prettyname(dev); 1050 printf("not probed (disabled)\n"); 1051 } 1052 } 1053 1054 return (error); 1055 } 1056 1057 int 1058 device_detach(device_t dev) 1059 { 1060 int error; 1061 1062 PDEBUG(("%s", DEVICENAME(dev))); 1063 if (dev->state == DS_BUSY) 1064 return (EBUSY); 1065 if (dev->state != DS_ATTACHED) 1066 return (0); 1067 1068 if ((error = DEVICE_DETACH(dev)) != 0) 1069 return (error); 1070 device_printf(dev, "detached\n"); 1071 if (dev->parent) 1072 BUS_CHILD_DETACHED(dev->parent, dev); 1073 1074 if (!(dev->flags & DF_FIXEDCLASS)) 1075 devclass_delete_device(dev->devclass, dev); 1076 1077 dev->state = DS_NOTPRESENT; 1078 device_set_driver(dev, NULL); 1079 1080 return (0); 1081 } 1082 1083 int 1084 device_shutdown(device_t dev) 1085 { 1086 if (dev->state < DS_ATTACHED) 1087 return (0); 1088 return (DEVICE_SHUTDOWN(dev)); 1089 } 1090 1091 int 1092 device_set_unit(device_t dev, int unit) 1093 { 1094 devclass_t dc; 1095 int err; 1096 1097 dc = device_get_devclass(dev); 1098 if (unit < dc->maxunit && dc->devices[unit]) 1099 return (EBUSY); 1100 err = devclass_delete_device(dc, dev); 1101 if (err) 1102 return (err); 1103 dev->unit = unit; 1104 err = devclass_add_device(dc, dev); 1105 if (err) 1106 return (err); 1107 1108 bus_data_generation_update(); 1109 return (0); 1110 } 1111 1112 /*======================================*/ 1113 /* 1114 * Some useful method implementations to make life easier for bus drivers. 1115 */ 1116 1117 void 1118 resource_list_init(struct resource_list *rl) 1119 { 1120 SLIST_INIT(rl); 1121 } 1122 1123 void 1124 resource_list_free(struct resource_list *rl) 1125 { 1126 struct resource_list_entry *rle; 1127 1128 while ((rle = SLIST_FIRST(rl)) != NULL) { 1129 if (rle->res) 1130 panic("resource_list_free: resource entry is busy"); 1131 SLIST_REMOVE_HEAD(rl, link); 1132 free(rle, M_BUS); 1133 } 1134 } 1135 1136 void 1137 resource_list_add(struct resource_list *rl, int type, int rid, 1138 u_long start, u_long end, u_long count) 1139 { 1140 struct resource_list_entry *rle; 1141 1142 rle = resource_list_find(rl, type, rid); 1143 if (!rle) { 1144 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 1145 M_NOWAIT); 1146 if (!rle) 1147 panic("resource_list_add: can't record entry"); 1148 SLIST_INSERT_HEAD(rl, rle, link); 1149 rle->type = type; 1150 rle->rid = rid; 1151 rle->res = NULL; 1152 } 1153 1154 if (rle->res) 1155 panic("resource_list_add: resource entry is busy"); 1156 1157 rle->start = start; 1158 rle->end = end; 1159 rle->count = count; 1160 } 1161 1162 struct resource_list_entry * 1163 resource_list_find(struct resource_list *rl, int type, int rid) 1164 { 1165 struct resource_list_entry *rle; 1166 1167 SLIST_FOREACH(rle, rl, link) { 1168 if (rle->type == type && rle->rid == rid) 1169 return (rle); 1170 } 1171 return (NULL); 1172 } 1173 1174 void 1175 resource_list_delete(struct resource_list *rl, int type, int rid) 1176 { 1177 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 1178 1179 if (rle) { 1180 if (rle->res != NULL) 1181 panic("resource_list_delete: resource has not been released"); 1182 SLIST_REMOVE(rl, rle, resource_list_entry, link); 1183 free(rle, M_BUS); 1184 } 1185 } 1186 1187 struct resource * 1188 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 1189 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 1190 { 1191 struct resource_list_entry *rle = 0; 1192 int passthrough = (device_get_parent(child) != bus); 1193 int isdefault = (start == 0UL && end == ~0UL); 1194 1195 if (passthrough) { 1196 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1197 type, rid, start, end, count, flags)); 1198 } 1199 1200 rle = resource_list_find(rl, type, *rid); 1201 1202 if (!rle) 1203 return (NULL); /* no resource of that type/rid */ 1204 1205 if (rle->res) 1206 panic("resource_list_alloc: resource entry is busy"); 1207 1208 if (isdefault) { 1209 start = rle->start; 1210 count = max(count, rle->count); 1211 end = max(rle->end, start + count - 1); 1212 } 1213 1214 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 1215 type, rid, start, end, count, flags); 1216 1217 /* 1218 * Record the new range. 1219 */ 1220 if (rle->res) { 1221 rle->start = rman_get_start(rle->res); 1222 rle->end = rman_get_end(rle->res); 1223 rle->count = count; 1224 } 1225 1226 return (rle->res); 1227 } 1228 1229 int 1230 resource_list_release(struct resource_list *rl, device_t bus, device_t child, 1231 int type, int rid, struct resource *res) 1232 { 1233 struct resource_list_entry *rle = 0; 1234 int passthrough = (device_get_parent(child) != bus); 1235 int error; 1236 1237 if (passthrough) { 1238 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1239 type, rid, res)); 1240 } 1241 1242 rle = resource_list_find(rl, type, rid); 1243 1244 if (!rle) 1245 panic("resource_list_release: can't find resource"); 1246 if (!rle->res) 1247 panic("resource_list_release: resource entry is not busy"); 1248 1249 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 1250 type, rid, res); 1251 if (error) 1252 return (error); 1253 1254 rle->res = NULL; 1255 return (0); 1256 } 1257 1258 /* 1259 * Call DEVICE_IDENTIFY for each driver. 1260 */ 1261 int 1262 bus_generic_probe(device_t dev) 1263 { 1264 devclass_t dc = dev->devclass; 1265 driverlink_t dl; 1266 1267 TAILQ_FOREACH(dl, &dc->drivers, link) { 1268 DEVICE_IDENTIFY(dl->driver, dev); 1269 } 1270 1271 return (0); 1272 } 1273 1274 int 1275 bus_generic_attach(device_t dev) 1276 { 1277 device_t child; 1278 1279 TAILQ_FOREACH(child, &dev->children, link) { 1280 device_probe_and_attach(child); 1281 } 1282 1283 return (0); 1284 } 1285 1286 int 1287 bus_generic_detach(device_t dev) 1288 { 1289 device_t child; 1290 int error; 1291 1292 if (dev->state != DS_ATTACHED) 1293 return (EBUSY); 1294 1295 TAILQ_FOREACH(child, &dev->children, link) { 1296 if ((error = device_detach(child)) != 0) 1297 return (error); 1298 } 1299 1300 return (0); 1301 } 1302 1303 int 1304 bus_generic_shutdown(device_t dev) 1305 { 1306 device_t child; 1307 1308 TAILQ_FOREACH(child, &dev->children, link) { 1309 device_shutdown(child); 1310 } 1311 1312 return (0); 1313 } 1314 1315 int 1316 bus_generic_suspend(device_t dev) 1317 { 1318 int error; 1319 device_t child, child2; 1320 1321 TAILQ_FOREACH(child, &dev->children, link) { 1322 error = DEVICE_SUSPEND(child); 1323 if (error) { 1324 for (child2 = TAILQ_FIRST(&dev->children); 1325 child2 && child2 != child; 1326 child2 = TAILQ_NEXT(child2, link)) 1327 DEVICE_RESUME(child2); 1328 return (error); 1329 } 1330 } 1331 return (0); 1332 } 1333 1334 int 1335 bus_generic_resume(device_t dev) 1336 { 1337 device_t child; 1338 1339 TAILQ_FOREACH(child, &dev->children, link) { 1340 DEVICE_RESUME(child); 1341 /* if resume fails, there's nothing we can usefully do... */ 1342 } 1343 return (0); 1344 } 1345 1346 int 1347 bus_print_child_header (device_t dev, device_t child) 1348 { 1349 int retval = 0; 1350 1351 if (device_get_desc(child)) { 1352 retval += device_printf(child, "<%s>", device_get_desc(child)); 1353 } else { 1354 retval += printf("%s", device_get_nameunit(child)); 1355 } 1356 1357 return (retval); 1358 } 1359 1360 int 1361 bus_print_child_footer (device_t dev, device_t child) 1362 { 1363 return (printf(" on %s\n", device_get_nameunit(dev))); 1364 } 1365 1366 int 1367 bus_generic_print_child(device_t dev, device_t child) 1368 { 1369 int retval = 0; 1370 1371 retval += bus_print_child_header(dev, child); 1372 retval += bus_print_child_footer(dev, child); 1373 1374 return (retval); 1375 } 1376 1377 int 1378 bus_generic_read_ivar(device_t dev, device_t child, int index, 1379 uintptr_t * result) 1380 { 1381 return (ENOENT); 1382 } 1383 1384 int 1385 bus_generic_write_ivar(device_t dev, device_t child, int index, 1386 uintptr_t value) 1387 { 1388 return (ENOENT); 1389 } 1390 1391 struct resource_list * 1392 bus_generic_get_resource_list (device_t dev, device_t child) 1393 { 1394 return (NULL); 1395 } 1396 1397 void 1398 bus_generic_driver_added(device_t dev, driver_t *driver) 1399 { 1400 device_t child; 1401 1402 DEVICE_IDENTIFY(driver, dev); 1403 TAILQ_FOREACH(child, &dev->children, link) { 1404 if (child->state == DS_NOTPRESENT) 1405 device_probe_and_attach(child); 1406 } 1407 } 1408 1409 int 1410 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 1411 int flags, driver_intr_t *intr, void *arg, void **cookiep) 1412 { 1413 /* Propagate up the bus hierarchy until someone handles it. */ 1414 if (dev->parent) 1415 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 1416 intr, arg, cookiep)); 1417 return (EINVAL); 1418 } 1419 1420 int 1421 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 1422 void *cookie) 1423 { 1424 /* Propagate up the bus hierarchy until someone handles it. */ 1425 if (dev->parent) 1426 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 1427 return (EINVAL); 1428 } 1429 1430 struct resource * 1431 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 1432 u_long start, u_long end, u_long count, u_int flags) 1433 { 1434 /* Propagate up the bus hierarchy until someone handles it. */ 1435 if (dev->parent) 1436 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 1437 start, end, count, flags)); 1438 return (NULL); 1439 } 1440 1441 int 1442 bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 1443 struct resource *r) 1444 { 1445 /* Propagate up the bus hierarchy until someone handles it. */ 1446 if (dev->parent) 1447 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 1448 r)); 1449 return (EINVAL); 1450 } 1451 1452 int 1453 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 1454 struct resource *r) 1455 { 1456 /* Propagate up the bus hierarchy until someone handles it. */ 1457 if (dev->parent) 1458 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 1459 r)); 1460 return (EINVAL); 1461 } 1462 1463 int 1464 bus_generic_deactivate_resource(device_t dev, device_t child, int type, 1465 int rid, struct resource *r) 1466 { 1467 /* Propagate up the bus hierarchy until someone handles it. */ 1468 if (dev->parent) 1469 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 1470 r)); 1471 return (EINVAL); 1472 } 1473 1474 int 1475 bus_generic_rl_get_resource (device_t dev, device_t child, int type, int rid, 1476 u_long *startp, u_long *countp) 1477 { 1478 struct resource_list * rl = NULL; 1479 struct resource_list_entry * rle = NULL; 1480 1481 rl = BUS_GET_RESOURCE_LIST(dev, child); 1482 if (!rl) 1483 return (EINVAL); 1484 1485 rle = resource_list_find(rl, type, rid); 1486 if (!rle) 1487 return (ENOENT); 1488 1489 if (startp) 1490 *startp = rle->start; 1491 if (countp) 1492 *countp = rle->count; 1493 1494 return (0); 1495 } 1496 1497 int 1498 bus_generic_rl_set_resource (device_t dev, device_t child, int type, int rid, 1499 u_long start, u_long count) 1500 { 1501 struct resource_list * rl = NULL; 1502 1503 rl = BUS_GET_RESOURCE_LIST(dev, child); 1504 if (!rl) 1505 return (EINVAL); 1506 1507 resource_list_add(rl, type, rid, start, (start + count - 1), count); 1508 1509 return (0); 1510 } 1511 1512 void 1513 bus_generic_rl_delete_resource (device_t dev, device_t child, int type, int rid) 1514 { 1515 struct resource_list * rl = NULL; 1516 1517 rl = BUS_GET_RESOURCE_LIST(dev, child); 1518 if (!rl) 1519 return; 1520 1521 resource_list_delete(rl, type, rid); 1522 1523 return; 1524 } 1525 1526 int 1527 bus_generic_rl_release_resource (device_t dev, device_t child, int type, 1528 int rid, struct resource *r) 1529 { 1530 struct resource_list * rl = NULL; 1531 1532 rl = BUS_GET_RESOURCE_LIST(dev, child); 1533 if (!rl) 1534 return (EINVAL); 1535 1536 return (resource_list_release(rl, dev, child, type, rid, r)); 1537 } 1538 1539 struct resource * 1540 bus_generic_rl_alloc_resource (device_t dev, device_t child, int type, 1541 int *rid, u_long start, u_long end, u_long count, u_int flags) 1542 { 1543 struct resource_list * rl = NULL; 1544 1545 rl = BUS_GET_RESOURCE_LIST(dev, child); 1546 if (!rl) 1547 return (NULL); 1548 1549 return (resource_list_alloc(rl, dev, child, type, rid, 1550 start, end, count, flags)); 1551 } 1552 1553 /* 1554 * Some convenience functions to make it easier for drivers to use the 1555 * resource-management functions. All these really do is hide the 1556 * indirection through the parent's method table, making for slightly 1557 * less-wordy code. In the future, it might make sense for this code 1558 * to maintain some sort of a list of resources allocated by each device. 1559 */ 1560 struct resource * 1561 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 1562 u_long count, u_int flags) 1563 { 1564 if (dev->parent == 0) 1565 return (0); 1566 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 1567 count, flags)); 1568 } 1569 1570 int 1571 bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 1572 { 1573 if (dev->parent == 0) 1574 return (EINVAL); 1575 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 1576 } 1577 1578 int 1579 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 1580 { 1581 if (dev->parent == 0) 1582 return (EINVAL); 1583 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 1584 } 1585 1586 int 1587 bus_release_resource(device_t dev, int type, int rid, struct resource *r) 1588 { 1589 if (dev->parent == 0) 1590 return (EINVAL); 1591 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 1592 } 1593 1594 int 1595 bus_setup_intr(device_t dev, struct resource *r, int flags, 1596 driver_intr_t handler, void *arg, void **cookiep) 1597 { 1598 if (dev->parent == 0) 1599 return (EINVAL); 1600 return (BUS_SETUP_INTR(dev->parent, dev, r, flags, 1601 handler, arg, cookiep)); 1602 } 1603 1604 int 1605 bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 1606 { 1607 if (dev->parent == 0) 1608 return (EINVAL); 1609 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 1610 } 1611 1612 int 1613 bus_set_resource(device_t dev, int type, int rid, 1614 u_long start, u_long count) 1615 { 1616 return BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 1617 start, count); 1618 } 1619 1620 int 1621 bus_get_resource(device_t dev, int type, int rid, 1622 u_long *startp, u_long *countp) 1623 { 1624 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 1625 startp, countp)); 1626 } 1627 1628 u_long 1629 bus_get_resource_start(device_t dev, int type, int rid) 1630 { 1631 u_long start, count; 1632 int error; 1633 1634 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 1635 &start, &count); 1636 if (error) 1637 return (0); 1638 return (start); 1639 } 1640 1641 u_long 1642 bus_get_resource_count(device_t dev, int type, int rid) 1643 { 1644 u_long start, count; 1645 int error; 1646 1647 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 1648 &start, &count); 1649 if (error) 1650 return (0); 1651 return (count); 1652 } 1653 1654 void 1655 bus_delete_resource(device_t dev, int type, int rid) 1656 { 1657 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 1658 } 1659 1660 static int 1661 root_print_child(device_t dev, device_t child) 1662 { 1663 int retval = 0; 1664 1665 retval += bus_print_child_header(dev, child); 1666 retval += printf("\n"); 1667 1668 return (retval); 1669 } 1670 1671 static int 1672 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 1673 void **cookiep) 1674 { 1675 /* 1676 * If an interrupt mapping gets to here something bad has happened. 1677 */ 1678 panic("root_setup_intr"); 1679 } 1680 1681 static kobj_method_t root_methods[] = { 1682 /* Device interface */ 1683 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 1684 KOBJMETHOD(device_suspend, bus_generic_suspend), 1685 KOBJMETHOD(device_resume, bus_generic_resume), 1686 1687 /* Bus interface */ 1688 KOBJMETHOD(bus_print_child, root_print_child), 1689 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 1690 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 1691 KOBJMETHOD(bus_setup_intr, root_setup_intr), 1692 1693 { 0, 0 } 1694 }; 1695 1696 static driver_t root_driver = { 1697 "root", 1698 root_methods, 1699 1, /* no softc */ 1700 }; 1701 1702 device_t root_bus; 1703 devclass_t root_devclass; 1704 1705 static int 1706 root_bus_module_handler(module_t mod, int what, void* arg) 1707 { 1708 switch (what) { 1709 case MOD_LOAD: 1710 TAILQ_INIT(&bus_data_devices); 1711 kobj_class_compile((kobj_class_t) &root_driver); 1712 root_bus = make_device(NULL, "root", 0); 1713 root_bus->desc = "System root bus"; 1714 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 1715 root_bus->driver = &root_driver; 1716 root_bus->state = DS_ATTACHED; 1717 root_devclass = devclass_find_internal("root", FALSE); 1718 return (0); 1719 1720 case MOD_SHUTDOWN: 1721 device_shutdown(root_bus); 1722 return (0); 1723 } 1724 1725 return (0); 1726 } 1727 1728 static moduledata_t root_bus_mod = { 1729 "rootbus", 1730 root_bus_module_handler, 1731 0 1732 }; 1733 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1734 1735 void 1736 root_bus_configure(void) 1737 { 1738 device_t dev; 1739 1740 PDEBUG((".")); 1741 1742 TAILQ_FOREACH(dev, &root_bus->children, link) { 1743 device_probe_and_attach(dev); 1744 } 1745 } 1746 1747 int 1748 driver_module_handler(module_t mod, int what, void *arg) 1749 { 1750 int error, i; 1751 struct driver_module_data *dmd; 1752 devclass_t bus_devclass; 1753 1754 dmd = (struct driver_module_data *)arg; 1755 bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE); 1756 error = 0; 1757 1758 switch (what) { 1759 case MOD_LOAD: 1760 if (dmd->dmd_chainevh) 1761 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 1762 1763 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) { 1764 PDEBUG(("Loading module: driver %s on bus %s", 1765 DRIVERNAME(dmd->dmd_drivers[i]), dmd->dmd_busname)); 1766 error = devclass_add_driver(bus_devclass, 1767 dmd->dmd_drivers[i]); 1768 } 1769 if (error) 1770 break; 1771 1772 /* 1773 * The drivers loaded in this way are assumed to all 1774 * implement the same devclass. 1775 */ 1776 *dmd->dmd_devclass = 1777 devclass_find_internal(dmd->dmd_drivers[0]->name, TRUE); 1778 break; 1779 1780 case MOD_UNLOAD: 1781 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) { 1782 PDEBUG(("Unloading module: driver %s from bus %s", 1783 DRIVERNAME(dmd->dmd_drivers[i]), 1784 dmd->dmd_busname)); 1785 error = devclass_delete_driver(bus_devclass, 1786 dmd->dmd_drivers[i]); 1787 } 1788 1789 if (!error && dmd->dmd_chainevh) 1790 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 1791 break; 1792 } 1793 1794 return (error); 1795 } 1796 1797 #ifdef BUS_DEBUG 1798 1799 /* the _short versions avoid iteration by not calling anything that prints 1800 * more than oneliners. I love oneliners. 1801 */ 1802 1803 static void 1804 print_device_short(device_t dev, int indent) 1805 { 1806 if (!dev) 1807 return; 1808 1809 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 1810 dev->unit, dev->desc, 1811 (dev->parent? "":"no "), 1812 (TAILQ_EMPTY(&dev->children)? "no ":""), 1813 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 1814 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 1815 (dev->flags&DF_WILDCARD? "wildcard,":""), 1816 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 1817 (dev->ivars? "":"no "), 1818 (dev->softc? "":"no "), 1819 dev->busy)); 1820 } 1821 1822 static void 1823 print_device(device_t dev, int indent) 1824 { 1825 if (!dev) 1826 return; 1827 1828 print_device_short(dev, indent); 1829 1830 indentprintf(("Parent:\n")); 1831 print_device_short(dev->parent, indent+1); 1832 indentprintf(("Driver:\n")); 1833 print_driver_short(dev->driver, indent+1); 1834 indentprintf(("Devclass:\n")); 1835 print_devclass_short(dev->devclass, indent+1); 1836 } 1837 1838 void 1839 print_device_tree_short(device_t dev, int indent) 1840 /* print the device and all its children (indented) */ 1841 { 1842 device_t child; 1843 1844 if (!dev) 1845 return; 1846 1847 print_device_short(dev, indent); 1848 1849 TAILQ_FOREACH(child, &dev->children, link) { 1850 print_device_tree_short(child, indent+1); 1851 } 1852 } 1853 1854 void 1855 print_device_tree(device_t dev, int indent) 1856 /* print the device and all its children (indented) */ 1857 { 1858 device_t child; 1859 1860 if (!dev) 1861 return; 1862 1863 print_device(dev, indent); 1864 1865 TAILQ_FOREACH(child, &dev->children, link) { 1866 print_device_tree(child, indent+1); 1867 } 1868 } 1869 1870 static void 1871 print_driver_short(driver_t *driver, int indent) 1872 { 1873 if (!driver) 1874 return; 1875 1876 indentprintf(("driver %s: softc size = %d\n", 1877 driver->name, driver->size)); 1878 } 1879 1880 static void 1881 print_driver(driver_t *driver, int indent) 1882 { 1883 if (!driver) 1884 return; 1885 1886 print_driver_short(driver, indent); 1887 } 1888 1889 1890 static void 1891 print_driver_list(driver_list_t drivers, int indent) 1892 { 1893 driverlink_t driver; 1894 1895 TAILQ_FOREACH(driver, &drivers, link) { 1896 print_driver(driver->driver, indent); 1897 } 1898 } 1899 1900 static void 1901 print_devclass_short(devclass_t dc, int indent) 1902 { 1903 if ( !dc ) 1904 return; 1905 1906 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 1907 } 1908 1909 static void 1910 print_devclass(devclass_t dc, int indent) 1911 { 1912 int i; 1913 1914 if ( !dc ) 1915 return; 1916 1917 print_devclass_short(dc, indent); 1918 indentprintf(("Drivers:\n")); 1919 print_driver_list(dc->drivers, indent+1); 1920 1921 indentprintf(("Devices:\n")); 1922 for (i = 0; i < dc->maxunit; i++) 1923 if (dc->devices[i]) 1924 print_device(dc->devices[i], indent+1); 1925 } 1926 1927 void 1928 print_devclass_list_short(void) 1929 { 1930 devclass_t dc; 1931 1932 printf("Short listing of devclasses, drivers & devices:\n"); 1933 TAILQ_FOREACH(dc, &devclasses, link) { 1934 print_devclass_short(dc, 0); 1935 } 1936 } 1937 1938 void 1939 print_devclass_list(void) 1940 { 1941 devclass_t dc; 1942 1943 printf("Full listing of devclasses, drivers & devices:\n"); 1944 TAILQ_FOREACH(dc, &devclasses, link) { 1945 print_devclass(dc, 0); 1946 } 1947 } 1948 1949 #endif 1950 1951 /* 1952 * User-space access to the device tree. 1953 * 1954 * We implement a small set of nodes: 1955 * 1956 * hw.bus Single integer read method to obtain the 1957 * current generation count. 1958 * hw.bus.devices Reads the entire device tree in flat space. 1959 * hw.bus.rman Resource manager interface 1960 * 1961 * We might like to add the ability to scan devclasses and/or drivers to 1962 * determine what else is currently loaded/available. 1963 */ 1964 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL); 1965 1966 static int 1967 sysctl_bus(SYSCTL_HANDLER_ARGS) 1968 { 1969 struct u_businfo ubus; 1970 1971 ubus.ub_version = BUS_USER_VERSION; 1972 ubus.ub_generation = bus_data_generation; 1973 1974 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 1975 } 1976 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 1977 "bus-related data"); 1978 1979 static int 1980 sysctl_devices(SYSCTL_HANDLER_ARGS) 1981 { 1982 int *name = (int *)arg1; 1983 u_int namelen = arg2; 1984 int index; 1985 struct device *dev; 1986 struct u_device udev; /* XXX this is a bit big */ 1987 int error; 1988 1989 if (namelen != 2) 1990 return (EINVAL); 1991 1992 if (bus_data_generation_check(name[0])) 1993 return (EINVAL); 1994 1995 index = name[1]; 1996 1997 /* 1998 * Scan the list of devices, looking for the requested index. 1999 */ 2000 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 2001 if (index-- == 0) 2002 break; 2003 } 2004 if (dev == NULL) 2005 return (ENOENT); 2006 2007 /* 2008 * Populate the return array. 2009 */ 2010 udev.dv_handle = (uintptr_t)dev; 2011 udev.dv_parent = (uintptr_t)dev->parent; 2012 if (dev->nameunit == NULL) { 2013 udev.dv_name[0] = 0; 2014 } else { 2015 snprintf(udev.dv_name, 32, "%s", dev->nameunit); 2016 } 2017 if (dev->desc == NULL) { 2018 udev.dv_desc[0] = 0; 2019 } else { 2020 snprintf(udev.dv_desc, 32, "%s", dev->desc); 2021 } 2022 if ((dev->driver == NULL) || (dev->driver->name == NULL)) { 2023 udev.dv_drivername[0] = 0; 2024 } else { 2025 snprintf(udev.dv_drivername, 32, "%s", dev->driver->name); 2026 } 2027 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 2028 return (error); 2029 } 2030 2031 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 2032 "system device tree"); 2033 2034 /* 2035 * Sysctl interface for scanning the resource lists. 2036 * 2037 * We take two input parameters; the index into the list of resource 2038 * managers, and the resource offset into the list. 2039 */ 2040 static int 2041 sysctl_rman(SYSCTL_HANDLER_ARGS) 2042 { 2043 int *name = (int *)arg1; 2044 u_int namelen = arg2; 2045 int rman_idx, res_idx; 2046 struct rman *rm; 2047 struct resource *res; 2048 struct u_rman urm; 2049 struct u_resource ures; 2050 int error; 2051 2052 if (namelen != 3) 2053 return (EINVAL); 2054 2055 if (bus_data_generation_check(name[0])) 2056 return (EINVAL); 2057 rman_idx = name[1]; 2058 res_idx = name[2]; 2059 2060 /* 2061 * Find the indexed resource manager 2062 */ 2063 TAILQ_FOREACH(rm, &rman_head, rm_link) { 2064 if (rman_idx-- == 0) 2065 break; 2066 } 2067 if (rm == NULL) 2068 return (ENOENT); 2069 2070 /* 2071 * If the resource index is -1, we want details on the 2072 * resource manager. 2073 */ 2074 if (res_idx == -1) { 2075 urm.rm_handle = (uintptr_t)rm; 2076 snprintf(urm.rm_descr, RM_TEXTLEN, "%s", rm->rm_descr); 2077 urm.rm_descr[RM_TEXTLEN - 1] = '\0'; 2078 urm.rm_start = rm->rm_start; 2079 urm.rm_size = rm->rm_end - rm->rm_start + 1; 2080 urm.rm_type = rm->rm_type; 2081 2082 error = SYSCTL_OUT(req, &urm, sizeof(urm)); 2083 return (error); 2084 } 2085 2086 /* 2087 * Find the indexed resource and return it. 2088 */ 2089 TAILQ_FOREACH(res, &rm->rm_list, r_link) { 2090 if (res_idx-- == 0) { 2091 ures.r_handle = (uintptr_t)res; 2092 ures.r_parent = (uintptr_t)res->r_rm; 2093 ures.r_device = (uintptr_t)res->r_dev; 2094 if (res->r_dev != NULL) { 2095 if (device_get_name(res->r_dev) != NULL) { 2096 snprintf(ures.r_devname, RM_TEXTLEN, 2097 "%s%d", 2098 device_get_name(res->r_dev), 2099 device_get_unit(res->r_dev)); 2100 } else { 2101 snprintf(ures.r_devname, RM_TEXTLEN, 2102 "nomatch"); 2103 } 2104 } else { 2105 ures.r_devname[0] = 0; 2106 } 2107 ures.r_start = res->r_start; 2108 ures.r_size = res->r_end - res->r_start + 1; 2109 ures.r_flags = res->r_flags; 2110 2111 error = SYSCTL_OUT(req, &ures, sizeof(ures)); 2112 return (error); 2113 } 2114 } 2115 return (ENOENT); 2116 } 2117 2118 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman, 2119 "kernel resource manager"); 2120 2121 int 2122 bus_data_generation_check(int generation) 2123 { 2124 if (generation != bus_data_generation) 2125 return (1); 2126 2127 /* XXX generate optimised lists here? */ 2128 return (0); 2129 } 2130 2131 void 2132 bus_data_generation_update(void) 2133 { 2134 bus_data_generation++; 2135 } 2136