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