subr_bus.c revision 42734
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 * $Id: subr_bus.c,v 1.13 1999/01/10 22:04:05 n_hibma Exp $ 27 */ 28 29#include <sys/param.h> 30#include <sys/queue.h> 31#include <sys/malloc.h> 32#include <sys/kernel.h> 33#include <sys/module.h> 34#include <sys/bus_private.h> 35#include <sys/systm.h> 36#include <machine/stdarg.h> /* for device_printf() */ 37 38#include "opt_bus.h" 39 40#ifdef BUS_DEBUG 41#define PDEBUG(a) (printf(__FUNCTION__ ":%d: ", __LINE__), printf a, printf("\n")) 42#define DEVICENAME(d) ((d)? device_get_name(d): "no device") 43#define DRIVERNAME(d) ((d)? d->name : "no driver") 44#define DEVCLANAME(d) ((d)? d->name : "no devclass") 45 46/* Produce the indenting, indent*2 spaces plus a '.' ahead of that to 47 * prevent syslog from deleting initial spaces 48 */ 49#define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while(0) 50 51static void print_method_list(device_method_t *m, int indent); 52static void print_device_ops(device_ops_t ops, int indent); 53static void print_device_short(device_t dev, int indent); 54static void print_device(device_t dev, int indent); 55void print_device_tree_short(device_t dev, int indent); 56void print_device_tree(device_t dev, int indent); 57static void print_driver_short(driver_t *driver, int indent); 58static void print_driver(driver_t *driver, int indent); 59static void print_driver_list(driver_list_t drivers, int indent); 60static void print_devclass_short(devclass_t dc, int indent); 61static void print_devclass(devclass_t dc, int indent); 62void print_devclass_list_short(void); 63void print_devclass_list(void); 64 65#else 66/* Make the compiler ignore the function calls */ 67#define PDEBUG(a) /* nop */ 68#define DEVICENAME(d) /* nop */ 69#define DRIVERNAME(d) /* nop */ 70#define DEVCLANAME(d) /* nop */ 71 72#define print_method_list(m,i) /* nop */ 73#define print_device_ops(o,i) /* nop */ 74#define print_device_short(d,i) /* nop */ 75#define print_device(d,i) /* nop */ 76#define print_device_tree_short(d,i) /* nop */ 77#define print_device_tree(d,i) /* nop */ 78#define print_driver_short(d,i) /* nop */ 79#define print_driver(d,i) /* nop */ 80#define print_driver_list(d,i) /* nop */ 81#define print_devclass_short(d,i) /* nop */ 82#define print_devclass(d,i) /* nop */ 83#define print_devclass_list_short() /* nop */ 84#define print_devclass_list() /* nop */ 85#endif 86 87 88/* 89 * Method table handling 90 */ 91static int next_method_offset = 1; 92static int methods_count = 0; 93static int methods_size = 0; 94 95struct method { 96 int offset; 97 char* name; 98}; 99 100static struct method *methods = 0; 101 102static void 103register_method(struct device_op_desc *desc) 104{ 105 int i; 106 struct method* m; 107 108 for (i = 0; i < methods_count; i++) 109 if (!strcmp(methods[i].name, desc->name)) { 110 desc->offset = methods[i].offset; 111 PDEBUG(("methods[%d] has the same name, %s, with offset %d", 112 i, desc->name, desc->offset)); 113 return; 114 } 115 116 if (methods_count == methods_size) { 117 struct method* p; 118 119 methods_size += 10; 120 p = (struct method*) malloc(methods_size * sizeof(struct method), 121 M_DEVBUF, M_NOWAIT); 122 if (!p) 123 panic("register_method: out of memory"); 124 if (methods) { 125 bcopy(methods, p, methods_count * sizeof(struct method)); 126 free(methods, M_DEVBUF); 127 } 128 methods = p; 129 } 130 m = &methods[methods_count++]; 131 m->name = malloc(strlen(desc->name) + 1, M_DEVBUF, M_NOWAIT); 132 if (!m->name) 133 panic("register_method: out of memory"); 134 strcpy(m->name, desc->name); 135 desc->offset = m->offset = next_method_offset++; 136} 137 138static int error_method(void) 139{ 140 return ENXIO; 141} 142 143static struct device_ops null_ops = { 144 1, 145 { error_method } 146}; 147 148static void 149compile_methods(driver_t *driver) 150{ 151 device_ops_t ops; 152 struct device_method *m; 153 int i; 154 155 /* 156 * First register any methods which need it. 157 */ 158 for (i = 0, m = driver->methods; m->desc; i++, m++) 159 if (!m->desc->offset) 160 register_method(m->desc); 161 else 162 PDEBUG(("offset not equal to zero, method desc %d left as is", i)); 163 164 /* 165 * Then allocate the compiled op table. 166 */ 167 ops = malloc(sizeof(struct device_ops) + (next_method_offset-1) * sizeof(devop_t), 168 M_DEVBUF, M_NOWAIT); 169 if (!ops) 170 panic("compile_methods: out of memory"); 171 172 ops->maxoffset = next_method_offset; 173 for (i = 0; i < next_method_offset; i++) 174 ops->methods[i] = error_method; 175 for (i = 0, m = driver->methods; m->desc; i++, m++) 176 ops->methods[m->desc->offset] = m->func; 177 PDEBUG(("%s has %d method%s, wasting %d bytes", 178 DRIVERNAME(driver), i, (i==1?"":"s"), 179 (next_method_offset-i)*sizeof(devop_t))); 180 181 driver->ops = ops; 182} 183 184/* 185 * Devclass implementation 186 */ 187 188static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 189 190static devclass_t 191devclass_find_internal(const char *classname, int create) 192{ 193 devclass_t dc; 194 195 PDEBUG(("looking for %s", classname)); 196 if (!classname) 197 return NULL; 198 199 for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link)) 200 if (!strcmp(dc->name, classname)) 201 return dc; 202 203 PDEBUG(("%s not found%s", classname, (create? ", creating": ""))); 204 if (create) { 205 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 206 M_DEVBUF, M_NOWAIT); 207 if (!dc) 208 return NULL; 209 dc->name = (char*) (dc + 1); 210 strcpy(dc->name, classname); 211 dc->devices = NULL; 212 dc->maxunit = 0; 213 dc->nextunit = 0; 214 TAILQ_INIT(&dc->drivers); 215 TAILQ_INSERT_TAIL(&devclasses, dc, link); 216 } 217 218 return dc; 219} 220 221devclass_t 222devclass_find(const char *classname) 223{ 224 return devclass_find_internal(classname, FALSE); 225} 226 227int 228devclass_add_driver(devclass_t dc, driver_t *driver) 229{ 230 PDEBUG(("%s", DRIVERNAME(driver))); 231 /* 232 * Compile the drivers methods. 233 */ 234 compile_methods(driver); 235 236 /* 237 * Make sure the devclass which the driver is implementing exists. 238 */ 239 devclass_find_internal(driver->name, TRUE); 240 241 TAILQ_INSERT_TAIL(&dc->drivers, driver, link); 242 243 return 0; 244} 245 246int 247devclass_delete_driver(devclass_t busclass, driver_t *driver) 248{ 249 devclass_t dc = devclass_find(driver->name); 250 device_t dev; 251 int i; 252 int error; 253 254 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 255 256 if (!dc) 257 return 0; 258 259 /* 260 * Disassociate from any devices. We iterate through all the 261 * devices in the devclass of the driver and detach any which are 262 * using the driver. 263 */ 264 for (i = 0; i < dc->maxunit; i++) { 265 if (dc->devices[i]) { 266 dev = dc->devices[i]; 267 if (dev->driver == driver) { 268 if (error = device_detach(dev)) 269 return error; 270 device_set_driver(dev, NULL); 271 } 272 } 273 } 274 275 TAILQ_REMOVE(&busclass->drivers, driver, link); 276 return 0; 277} 278 279driver_t * 280devclass_find_driver(devclass_t dc, const char *classname) 281{ 282 driver_t *driver; 283 284 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 285 286 for (driver = TAILQ_FIRST(&dc->drivers); driver; 287 driver = TAILQ_NEXT(driver, link)) { 288 if (!strcmp(driver->name, classname)) 289 return driver; 290 } 291 292 PDEBUG(("not found")); 293 return NULL; 294} 295 296const char * 297devclass_get_name(devclass_t dc) 298{ 299 return dc->name; 300} 301 302device_t 303devclass_get_device(devclass_t dc, int unit) 304{ 305 if (unit < 0 || unit >= dc->maxunit) 306 return NULL; 307 return dc->devices[unit]; 308} 309 310void * 311devclass_get_softc(devclass_t dc, int unit) 312{ 313 device_t dev; 314 315 if (unit < 0 || unit >= dc->maxunit) 316 return NULL; 317 dev = dc->devices[unit]; 318 if (!dev || dev->state < DS_ATTACHED) 319 return NULL; 320 return dev->softc; 321} 322 323int 324devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 325{ 326 int i; 327 int count; 328 device_t *list; 329 330 count = 0; 331 for (i = 0; i < dc->maxunit; i++) 332 if (dc->devices[i]) 333 count++; 334 335 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT); 336 if (!list) 337 return ENOMEM; 338 339 count = 0; 340 for (i = 0; i < dc->maxunit; i++) 341 if (dc->devices[i]) { 342 list[count] = dc->devices[i]; 343 count++; 344 } 345 346 *devlistp = list; 347 *devcountp = count; 348 349 return 0; 350} 351 352int 353devclass_get_maxunit(devclass_t dc) 354{ 355 return dc->maxunit; 356} 357 358static int 359devclass_alloc_unit(devclass_t dc, int *unitp) 360{ 361 int unit = *unitp; 362 363 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 364 365 /* 366 * If we have been given a wired unit number, check for existing 367 * device. 368 */ 369 if (unit != -1) { 370 device_t dev; 371 dev = devclass_get_device(dc, unit); 372 if (dev) { 373 printf("devclass_alloc_unit: %s%d already exists, using next available unit number\n", dc->name, unit); 374 unit = -1; 375 } 376 } 377 378 if (unit == -1) { 379 unit = dc->nextunit; 380 dc->nextunit++; 381 } else if (dc->nextunit <= unit) 382 dc->nextunit = unit + 1; 383 384 if (unit >= dc->maxunit) { 385 device_t *newlist; 386 int newsize; 387 388 newsize = (dc->maxunit ? 2 * dc->maxunit 389 : MINALLOCSIZE / sizeof(device_t)); 390 newlist = malloc(sizeof(device_t) * newsize, M_DEVBUF, M_NOWAIT); 391 if (!newlist) 392 return ENOMEM; 393 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 394 bzero(newlist + dc->maxunit, 395 sizeof(device_t) * (newsize - dc->maxunit)); 396 if (dc->devices) 397 free(dc->devices, M_DEVBUF); 398 dc->devices = newlist; 399 dc->maxunit = newsize; 400 } 401 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 402 403 *unitp = unit; 404 return 0; 405} 406 407static int 408devclass_add_device(devclass_t dc, device_t dev) 409{ 410 int error; 411 412 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 413 414 if (error = devclass_alloc_unit(dc, &dev->unit)) 415 return error; 416 dc->devices[dev->unit] = dev; 417 dev->devclass = dc; 418 return 0; 419} 420 421static int 422devclass_delete_device(devclass_t dc, device_t dev) 423{ 424 if (!dc || !dev) 425 return 0; 426 427 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 428 429 if (dev->devclass != dc 430 || dc->devices[dev->unit] != dev) 431 panic("devclass_delete_device: inconsistent device class"); 432 dc->devices[dev->unit] = NULL; 433 if (dev->flags & DF_WILDCARD) 434 dev->unit = -1; 435 dev->devclass = NULL; 436 while (dc->nextunit > 0 && dc->devices[dc->nextunit - 1] == NULL) 437 dc->nextunit--; 438 return 0; 439} 440 441static device_t 442make_device(device_t parent, const char *name, 443 int unit, void *ivars) 444{ 445 device_t dev; 446 devclass_t dc; 447 int error; 448 449 PDEBUG(("%s at %s as unit %d with%s ivars", 450 name, DEVICENAME(parent), unit, (ivars? "":"out"))); 451 452 if (name) { 453 dc = devclass_find_internal(name, TRUE); 454 if (!dc) { 455 printf("make_device: can't find device class %s\n", name); 456 return NULL; 457 } 458 459 if (error = devclass_alloc_unit(dc, &unit)) 460 return NULL; 461 } else 462 dc = NULL; 463 464 dev = malloc(sizeof(struct device), M_DEVBUF, M_NOWAIT); 465 if (!dev) 466 return 0; 467 468 dev->parent = parent; 469 TAILQ_INIT(&dev->children); 470 dev->ops = &null_ops; 471 dev->driver = NULL; 472 dev->devclass = dc; 473 dev->unit = unit; 474 dev->desc = NULL; 475 dev->busy = 0; 476 dev->flags = DF_ENABLED; 477 if (unit == -1) 478 dev->flags |= DF_WILDCARD; 479 if (name) 480 dev->flags |= DF_FIXEDCLASS; 481 dev->ivars = ivars; 482 dev->softc = NULL; 483 484 if (dc) 485 dc->devices[unit] = dev; 486 487 dev->state = DS_NOTPRESENT; 488 489 return dev; 490} 491 492static void 493device_print_child(device_t dev, device_t child) 494{ 495 printf("%s%d", device_get_name(child), device_get_unit(child)); 496 if (device_is_alive(child)) { 497 if (device_get_desc(child)) 498 printf(": <%s>", device_get_desc(child)); 499 BUS_PRINT_CHILD(dev, child); 500 } else 501 printf(" not found"); 502 printf("\n"); 503} 504 505device_t 506device_add_child(device_t dev, const char *name, int unit, void *ivars) 507{ 508 device_t child; 509 510 PDEBUG(("%s at %s as unit %d with%s ivars", 511 name, DEVICENAME(dev), unit, (ivars? "":"out"))); 512 513 child = make_device(dev, name, unit, ivars); 514 515 if (child) 516 TAILQ_INSERT_TAIL(&dev->children, child, link); 517 else 518 PDEBUG(("%s failed", name)); 519 520 return child; 521} 522 523device_t 524device_add_child_after(device_t dev, device_t place, const char *name, 525 int unit, void *ivars) 526{ 527 device_t child; 528 529 PDEBUG(("%s at %s after %s as unit %d with%s ivars", 530 name, DEVICENAME(dev), DEVICENAME(place), unit, (ivars? "":"out"))); 531 532 child = make_device(dev, name, unit, ivars); 533 534 if (place) { 535 TAILQ_INSERT_AFTER(&dev->children, place, dev, link); 536 } else { 537 TAILQ_INSERT_HEAD(&dev->children, dev, link); 538 } 539 540 return child; 541} 542 543int 544device_delete_child(device_t dev, device_t child) 545{ 546 int error; 547 device_t grandchild; 548 549 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 550 551 /* remove children first */ 552 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 553 error = device_delete_child(child, grandchild); 554 if (error) 555 return error; 556 } 557 558 if (error = device_detach(child)) 559 return error; 560 if (child->devclass) 561 devclass_delete_device(child->devclass, child); 562 TAILQ_REMOVE(&dev->children, child, link); 563 free(child, M_DEVBUF); 564 565 return 0; 566} 567 568/* 569 * Find only devices attached to this bus. 570 */ 571device_t 572device_find_child(device_t dev, const char *classname, int unit) 573{ 574 devclass_t dc; 575 device_t child; 576 577 dc = devclass_find(classname); 578 if (!dc) 579 return NULL; 580 581 child = devclass_get_device(dc, unit); 582 if (child && child->parent == dev) 583 return child; 584 return NULL; 585} 586 587static driver_t * 588first_matching_driver(devclass_t dc, device_t dev) 589{ 590 if (dev->devclass) 591 return devclass_find_driver(dc, dev->devclass->name); 592 else 593 return TAILQ_FIRST(&dc->drivers); 594} 595 596static driver_t * 597next_matching_driver(devclass_t dc, device_t dev, driver_t *last) 598{ 599 if (dev->devclass) { 600 driver_t *driver; 601 for (driver = TAILQ_NEXT(last, link); driver; 602 driver = TAILQ_NEXT(driver, link)) 603 if (!strcmp(dev->devclass->name, driver->name)) 604 return driver; 605 return NULL; 606 } else 607 return TAILQ_NEXT(last, link); 608} 609 610static int 611device_probe_child(device_t dev, device_t child) 612{ 613 devclass_t dc; 614 driver_t *driver; 615 616 dc = dev->devclass; 617 if (dc == NULL) 618 panic("device_probe_child: parent device has no devclass"); 619 620 if (child->state == DS_ALIVE) 621 return 0; 622 623 for (driver = first_matching_driver(dc, child); 624 driver; 625 driver = next_matching_driver(dc, child, driver)) { 626 PDEBUG(("Trying %s", DRIVERNAME(driver))); 627 device_set_driver(child, driver); 628 if (DEVICE_PROBE(child) == 0) { 629 if (!child->devclass) 630 device_set_devclass(child, driver->name); 631 child->state = DS_ALIVE; 632 return 0; 633 } 634 } 635 636 return ENXIO; 637} 638 639device_t 640device_get_parent(device_t dev) 641{ 642 return dev->parent; 643} 644 645int 646device_get_children(device_t dev, device_t **devlistp, int *devcountp) 647{ 648 int count; 649 device_t child; 650 device_t *list; 651 652 count = 0; 653 for (child = TAILQ_FIRST(&dev->children); child; 654 child = TAILQ_NEXT(child, link)) 655 count++; 656 657 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT); 658 if (!list) 659 return ENOMEM; 660 661 count = 0; 662 for (child = TAILQ_FIRST(&dev->children); child; 663 child = TAILQ_NEXT(child, link)) { 664 list[count] = child; 665 count++; 666 } 667 668 *devlistp = list; 669 *devcountp = count; 670 671 return 0; 672} 673 674driver_t * 675device_get_driver(device_t dev) 676{ 677 return dev->driver; 678} 679 680devclass_t 681device_get_devclass(device_t dev) 682{ 683 return dev->devclass; 684} 685 686const char * 687device_get_name(device_t dev) 688{ 689 if (dev->devclass) 690 return devclass_get_name(dev->devclass); 691 return NULL; 692} 693 694int 695device_get_unit(device_t dev) 696{ 697 return dev->unit; 698} 699 700const char * 701device_get_desc(device_t dev) 702{ 703 return dev->desc; 704} 705 706void 707device_print_prettyname(device_t dev) 708{ 709 const char *name = device_get_name(dev); 710 711 if (name == 0) 712 name = "(no driver assigned)"; 713 printf("%s%d: ", name, device_get_unit(dev)); 714} 715 716void 717device_printf(device_t dev, const char * fmt, ...) 718{ 719 va_list ap; 720 721 device_print_prettyname(dev); 722 va_start(ap, fmt); 723 vprintf(fmt, ap); 724 va_end(ap); 725} 726 727void 728device_set_desc(device_t dev, const char* desc) 729{ 730 dev->desc = desc; 731} 732 733void * 734device_get_softc(device_t dev) 735{ 736 return dev->softc; 737} 738 739void * 740device_get_ivars(device_t dev) 741{ 742 return dev->ivars; 743} 744 745device_state_t 746device_get_state(device_t dev) 747{ 748 return dev->state; 749} 750 751void 752device_enable(device_t dev) 753{ 754 dev->flags |= DF_ENABLED; 755} 756 757void 758device_disable(device_t dev) 759{ 760 dev->flags &= ~DF_ENABLED; 761} 762 763void 764device_busy(device_t dev) 765{ 766 if (dev->state < DS_ATTACHED) 767 panic("device_busy: called for unattached device"); 768 if (dev->busy == 0 && dev->parent) 769 device_busy(dev->parent); 770 dev->busy++; 771 dev->state = DS_BUSY; 772} 773 774void 775device_unbusy(device_t dev) 776{ 777 if (dev->state != DS_BUSY) 778 panic("device_unbusy: called for non-busy device"); 779 dev->busy--; 780 if (dev->busy == 0) { 781 if (dev->parent) 782 device_unbusy(dev->parent); 783 dev->state = DS_ATTACHED; 784 } 785} 786 787int 788device_is_enabled(device_t dev) 789{ 790 return (dev->flags & DF_ENABLED) != 0; 791} 792 793int 794device_is_alive(device_t dev) 795{ 796 return dev->state >= DS_ALIVE; 797} 798 799int 800device_set_devclass(device_t dev, const char *classname) 801{ 802 devclass_t dc; 803 804 if (dev->devclass) { 805 printf("device_set_devclass: device class already set\n"); 806 return EINVAL; 807 } 808 809 dc = devclass_find_internal(classname, TRUE); 810 if (!dc) 811 return ENOMEM; 812 813 return devclass_add_device(dc, dev); 814} 815 816int 817device_set_driver(device_t dev, driver_t *driver) 818{ 819 if (dev->state >= DS_ATTACHED) 820 return EBUSY; 821 822 if (dev->driver == driver) 823 return 0; 824 825 if (dev->softc) { 826 free(dev->softc, M_DEVBUF); 827 dev->softc = NULL; 828 } 829 dev->ops = &null_ops; 830 dev->driver = driver; 831 if (driver) { 832 dev->ops = driver->ops; 833 dev->softc = malloc(driver->softc, M_DEVBUF, M_NOWAIT); 834 if (!dev->softc) { 835 dev->ops = &null_ops; 836 dev->driver = NULL; 837 return ENOMEM; 838 } 839 bzero(dev->softc, driver->softc); 840 } 841 return 0; 842} 843 844int 845device_probe_and_attach(device_t dev) 846{ 847 device_t bus = dev->parent; 848 int error = 0; 849 850 if (dev->state >= DS_ALIVE) 851 return 0; 852 853 if (dev->flags & DF_ENABLED) { 854 error = device_probe_child(bus, dev); 855 if (!error) { 856 device_print_child(bus, dev); 857 error = DEVICE_ATTACH(dev); 858 if (!error) 859 dev->state = DS_ATTACHED; 860 else { 861 printf("device_probe_and_attach: %s%d attach returned %d\n", 862 dev->driver->name, dev->unit, error); 863 device_set_driver(dev, NULL); 864 dev->state = DS_NOTPRESENT; 865 } 866 } 867 } else { 868 device_print_prettyname(dev); 869 printf("not probed (disabled)\n"); 870 } 871 872 return error; 873} 874 875int 876device_detach(device_t dev) 877{ 878 int error; 879 880 PDEBUG(("%s", DEVICENAME(dev))); 881 if (dev->state == DS_BUSY) 882 return EBUSY; 883 if (dev->state != DS_ATTACHED) 884 return 0; 885 886 if (error = DEVICE_DETACH(dev)) 887 return error; 888 889 if (!(dev->flags & DF_FIXEDCLASS)) 890 devclass_delete_device(dev->devclass, dev); 891 892 dev->state = DS_NOTPRESENT; 893 device_set_driver(dev, NULL); 894 895 return 0; 896} 897 898int 899device_shutdown(device_t dev) 900{ 901 if (dev->state < DS_ATTACHED) 902 return 0; 903 return DEVICE_SHUTDOWN(dev); 904} 905 906/* 907 * Access functions for device resources. 908 */ 909extern struct config_device devtab[]; 910extern int devtab_count; 911 912static int 913resource_match_string(int i, char *resname, char *value) 914{ 915 int j; 916 struct config_resource *res; 917 918 for (j = 0, res = devtab[i].resources; 919 j < devtab[i].resource_count; j++, res++) 920 if (!strcmp(res->name, resname) 921 && res->type == RES_STRING 922 && !strcmp(res->u.stringval, value)) 923 return TRUE; 924 return FALSE; 925} 926 927static int 928resource_find(const char *name, int unit, char *resname, 929 struct config_resource **result) 930{ 931 int i, j; 932 struct config_resource *res; 933 934 /* 935 * First check specific instances, then generic. 936 */ 937 for (i = 0; i < devtab_count; i++) { 938 if (devtab[i].unit < 0) 939 continue; 940 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 941 res = devtab[i].resources; 942 for (j = 0; j < devtab[i].resource_count; j++, res++) 943 if (!strcmp(res->name, resname)) { 944 *result = res; 945 return 0; 946 } 947 } 948 } 949 for (i = 0; i < devtab_count; i++) { 950 if (devtab[i].unit >= 0) 951 continue; 952 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 953 res = devtab[i].resources; 954 for (j = 0; j < devtab[i].resource_count; j++, res++) 955 if (!strcmp(res->name, resname)) { 956 *result = res; 957 return 0; 958 } 959 } 960 } 961 return ENOENT; 962} 963 964int 965resource_int_value(const char *name, int unit, char *resname, int *result) 966{ 967 int error; 968 struct config_resource *res; 969 if ((error = resource_find(name, unit, resname, &res)) != 0) 970 return error; 971 if (res->type != RES_INT) 972 return EFTYPE; 973 *result = res->u.intval; 974 return 0; 975} 976 977int 978resource_long_value(const char *name, int unit, char *resname, long *result) 979{ 980 int error; 981 struct config_resource *res; 982 if ((error = resource_find(name, unit, resname, &res)) != 0) 983 return error; 984 if (res->type != RES_LONG) 985 return EFTYPE; 986 *result = res->u.longval; 987 return 0; 988} 989 990int 991resource_string_value(const char *name, int unit, char *resname, char **result) 992{ 993 int error; 994 struct config_resource *res; 995 if ((error = resource_find(name, unit, resname, &res)) != 0) 996 return error; 997 if (res->type != RES_STRING) 998 return EFTYPE; 999 *result = res->u.stringval; 1000 return 0; 1001} 1002 1003int 1004resource_query_string(int i, char *resname, char *value) 1005{ 1006 if (i < 0) 1007 i = 0; 1008 else 1009 i = i + 1; 1010 for (; i < devtab_count; i++) 1011 if (resource_match_string(i, resname, value)) 1012 return i; 1013 return -1; 1014} 1015 1016char * 1017resource_query_name(int i) 1018{ 1019 return devtab[i].name; 1020} 1021 1022int 1023resource_query_unit(int i) 1024{ 1025 return devtab[i].unit; 1026} 1027 1028 1029/* 1030 * Some useful method implementations to make life easier for bus drivers. 1031 */ 1032int 1033bus_generic_attach(device_t dev) 1034{ 1035 device_t child; 1036 1037 for (child = TAILQ_FIRST(&dev->children); 1038 child; child = TAILQ_NEXT(child, link)) 1039 device_probe_and_attach(child); 1040 1041 return 0; 1042} 1043 1044int 1045bus_generic_detach(device_t dev) 1046{ 1047 device_t child; 1048 int error; 1049 1050 if (dev->state != DS_ATTACHED) 1051 return EBUSY; 1052 1053 for (child = TAILQ_FIRST(&dev->children); 1054 child; child = TAILQ_NEXT(child, link)) 1055 if (error = device_detach(child)) 1056 return error; 1057 1058 return 0; 1059} 1060 1061int 1062bus_generic_shutdown(device_t dev) 1063{ 1064 device_t child; 1065 1066 for (child = TAILQ_FIRST(&dev->children); 1067 child; child = TAILQ_NEXT(child, link)) 1068 DEVICE_SHUTDOWN(child); 1069 1070 return 0; 1071} 1072 1073int 1074bus_generic_suspend(device_t dev) 1075{ 1076 int error; 1077 device_t child, child2; 1078 1079 for (child = TAILQ_FIRST(&dev->children); 1080 child; child = TAILQ_NEXT(child, link)) { 1081 error = DEVICE_SUSPEND(child); 1082 if (error) { 1083 for (child2 = TAILQ_FIRST(&dev->children); 1084 child2 && child2 != child; 1085 child2 = TAILQ_NEXT(child2, link)) 1086 DEVICE_RESUME(child2); 1087 return (error); 1088 } 1089 } 1090 return 0; 1091} 1092 1093int 1094bus_generic_resume(device_t dev) 1095{ 1096 device_t child; 1097 1098 for (child = TAILQ_FIRST(&dev->children); 1099 child; child = TAILQ_NEXT(child, link)) { 1100 DEVICE_RESUME(child); 1101 /* if resume fails, there's nothing we can usefully do... */ 1102 } 1103 return 0; 1104} 1105 1106void 1107bus_generic_print_child(device_t dev, device_t child) 1108{ 1109 printf(" on %s%d", device_get_name(dev), device_get_unit(dev)); 1110} 1111 1112int 1113bus_generic_read_ivar(device_t dev, device_t child, int index, 1114 uintptr_t * result) 1115{ 1116 return ENOENT; 1117} 1118 1119int 1120bus_generic_write_ivar(device_t dev, device_t child, int index, 1121 uintptr_t value) 1122{ 1123 return ENOENT; 1124} 1125 1126int 1127bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 1128 driver_intr_t *intr, void *arg, void **cookiep) 1129{ 1130 /* Propagate up the bus hierarchy until someone handles it. */ 1131 if (dev->parent) 1132 return (BUS_SETUP_INTR(dev->parent, child, irq, intr, arg, 1133 cookiep)); 1134 else 1135 return (EINVAL); 1136} 1137 1138int 1139bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 1140 void *cookie) 1141{ 1142 /* Propagate up the bus hierarchy until someone handles it. */ 1143 if (dev->parent) 1144 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 1145 else 1146 return (EINVAL); 1147} 1148 1149struct resource * 1150bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 1151 u_long start, u_long end, u_long count, u_int flags) 1152{ 1153 /* Propagate up the bus hierarchy until someone handles it. */ 1154 if (dev->parent) 1155 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 1156 start, end, count, flags)); 1157 else 1158 return (NULL); 1159} 1160 1161int 1162bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 1163 struct resource *r) 1164{ 1165 /* Propagate up the bus hierarchy until someone handles it. */ 1166 if (dev->parent) 1167 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 1168 r)); 1169 else 1170 return (EINVAL); 1171} 1172 1173int 1174bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 1175 struct resource *r) 1176{ 1177 /* Propagate up the bus hierarchy until someone handles it. */ 1178 if (dev->parent) 1179 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 1180 r)); 1181 else 1182 return (EINVAL); 1183} 1184 1185int 1186bus_generic_deactivate_resource(device_t dev, device_t child, int type, 1187 int rid, struct resource *r) 1188{ 1189 /* Propagate up the bus hierarchy until someone handles it. */ 1190 if (dev->parent) 1191 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 1192 r)); 1193 else 1194 return (EINVAL); 1195} 1196 1197/* 1198 * Some convenience functions to make it easier for drivers to use the 1199 * resource-management functions. All these really do is hide the 1200 * indirection through the parent's method table, making for slightly 1201 * less-wordy code. In the future, it might make sense for this code 1202 * to maintain some sort of a list of resources allocated by each device. 1203 */ 1204struct resource * 1205bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 1206 u_long count, u_int flags) 1207{ 1208 if (dev->parent == 0) 1209 return (0); 1210 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 1211 count, flags)); 1212} 1213 1214int 1215bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 1216{ 1217 if (dev->parent == 0) 1218 return (EINVAL); 1219 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 1220} 1221 1222int 1223bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 1224{ 1225 if (dev->parent == 0) 1226 return (EINVAL); 1227 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 1228} 1229 1230int 1231bus_release_resource(device_t dev, int type, int rid, struct resource *r) 1232{ 1233 if (dev->parent == 0) 1234 return (EINVAL); 1235 return (BUS_RELEASE_RESOURCE(dev->parent, dev, 1236 type, rid, r)); 1237} 1238 1239static void 1240root_print_child(device_t dev, device_t child) 1241{ 1242} 1243 1244static int 1245root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 1246 void **cookiep) 1247{ 1248 /* 1249 * If an interrupt mapping gets to here something bad has happened. 1250 */ 1251 panic("root_setup_intr"); 1252} 1253 1254static device_method_t root_methods[] = { 1255 /* Device interface */ 1256 DEVMETHOD(device_suspend, bus_generic_suspend), 1257 DEVMETHOD(device_resume, bus_generic_resume), 1258 1259 /* Bus interface */ 1260 DEVMETHOD(bus_print_child, root_print_child), 1261 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar), 1262 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar), 1263 DEVMETHOD(bus_setup_intr, root_setup_intr), 1264 1265 { 0, 0 } 1266}; 1267 1268static driver_t root_driver = { 1269 "root", 1270 root_methods, 1271 DRIVER_TYPE_MISC, 1272 1, /* no softc */ 1273}; 1274 1275device_t root_bus; 1276devclass_t root_devclass; 1277 1278static int 1279root_bus_module_handler(module_t mod, int what, void* arg) 1280{ 1281 switch (what) { 1282 case MOD_LOAD: 1283 compile_methods(&root_driver); 1284 root_bus = make_device(NULL, "root", 0, NULL); 1285 root_bus->desc = "System root bus"; 1286 root_bus->ops = root_driver.ops; 1287 root_bus->driver = &root_driver; 1288 root_bus->state = DS_ATTACHED; 1289 root_devclass = devclass_find_internal("root", FALSE); 1290 return 0; 1291 } 1292 1293 return 0; 1294} 1295 1296static moduledata_t root_bus_mod = { 1297 "rootbus", 1298 root_bus_module_handler, 1299 0 1300}; 1301DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 1302 1303void 1304root_bus_configure(void) 1305{ 1306 device_t dev; 1307 1308 PDEBUG((".")); 1309 1310 for (dev = TAILQ_FIRST(&root_bus->children); dev; 1311 dev = TAILQ_NEXT(dev, link)) { 1312 device_probe_and_attach(dev); 1313 } 1314} 1315 1316int 1317driver_module_handler(module_t mod, int what, void *arg) 1318{ 1319 int error, i; 1320 struct driver_module_data *dmd; 1321 devclass_t bus_devclass; 1322 1323 dmd = (struct driver_module_data *)arg; 1324 bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE); 1325 error = 0; 1326 1327 switch (what) { 1328 case MOD_LOAD: 1329 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) { 1330 PDEBUG(("Loading module: driver %s on bus %s", 1331 DRIVERNAME(dmd->dmd_drivers[i]), 1332 dmd->dmd_busname)); 1333 error = devclass_add_driver(bus_devclass, 1334 dmd->dmd_drivers[i]); 1335 } 1336 if (error) 1337 break; 1338 1339 /* 1340 * The drivers loaded in this way are assumed to all 1341 * implement the same devclass. 1342 */ 1343 *dmd->dmd_devclass = 1344 devclass_find_internal(dmd->dmd_drivers[0]->name, 1345 TRUE); 1346 break; 1347 1348 case MOD_UNLOAD: 1349 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) { 1350 PDEBUG(("Unloading module: driver %s from bus %s", 1351 DRIVERNAME(dmd->dmd_drivers[i]), 1352 dmd->dmd_busname)); 1353 error = devclass_delete_driver(bus_devclass, 1354 dmd->dmd_drivers[i]); 1355 } 1356 break; 1357 } 1358 1359 if (!error && dmd->dmd_chainevh) 1360 error = dmd->dmd_chainevh(mod, what, dmd->dmd_chainarg); 1361 return (error); 1362} 1363 1364#ifdef BUS_DEBUG 1365 1366/* the _short versions avoid iteration by not calling anything that prints 1367 * more than oneliners. I love oneliners. 1368 */ 1369 1370static void 1371print_method_list(device_method_t *m, int indent) 1372{ 1373 int i; 1374 1375 if (!m) 1376 return; 1377 1378 for (i = 0; m->desc; i++, m++) 1379 indentprintf(("method %d: %s, offset=%d\n", 1380 i, m->desc->name, m->desc->offset)); 1381} 1382 1383static void 1384print_device_ops(device_ops_t ops, int indent) 1385{ 1386 int i; 1387 int count = 0; 1388 1389 if (!ops) 1390 return; 1391 1392 /* we present a list of the methods that are pointing to the 1393 * error_method, but ignore the 0'th elements; it is always 1394 * error_method. 1395 */ 1396 for (i = 1; i < ops->maxoffset; i++) { 1397 if (ops->methods[i] == error_method) { 1398 if (count == 0) 1399 indentprintf(("error_method:")); 1400 printf(" %d", i); 1401 count++; 1402 } 1403 } 1404 if (count) 1405 printf("\n"); 1406 1407 indentprintf(("(%d method%s, %d valid, %d error_method%s)\n", 1408 ops->maxoffset-1, (ops->maxoffset-1 == 1? "":"s"), 1409 ops->maxoffset-1-count, 1410 count, (count == 1? "":"'s"))); 1411} 1412 1413static void 1414print_device_short(device_t dev, int indent) 1415{ 1416 if (!dev) 1417 return; 1418 1419 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%sivars,%ssoftc,busy=%d\n", 1420 dev->unit, dev->desc, 1421 (dev->parent? "":"no "), 1422 (TAILQ_EMPTY(&dev->children)? "no ":""), 1423 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 1424 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 1425 (dev->flags&DF_WILDCARD? "wildcard,":""), 1426 (dev->ivars? "":"no "), 1427 (dev->softc? "":"no "), 1428 dev->busy)); 1429} 1430 1431static void 1432print_device(device_t dev, int indent) 1433{ 1434 if (!dev) 1435 return; 1436 1437 print_device_short(dev, indent); 1438 1439 indentprintf(("Parent:\n")); 1440 print_device_short(dev->parent, indent+1); 1441 indentprintf(("Methods:\n")); 1442 print_device_ops(dev->ops, indent+1); 1443 indentprintf(("Driver:\n")); 1444 print_driver_short(dev->driver, indent+1); 1445 indentprintf(("Devclass:\n")); 1446 print_devclass_short(dev->devclass, indent+1); 1447} 1448 1449void 1450print_device_tree_short(device_t dev, int indent) 1451/* print the device and all its children (indented) */ 1452{ 1453 device_t child; 1454 1455 if (!dev) 1456 return; 1457 1458 print_device_short(dev, indent); 1459 1460 for (child = TAILQ_FIRST(&dev->children); child; 1461 child = TAILQ_NEXT(child, link)) 1462 print_device_tree_short(child, indent+1); 1463} 1464 1465void 1466print_device_tree(device_t dev, int indent) 1467/* print the device and all its children (indented) */ 1468{ 1469 device_t child; 1470 1471 if (!dev) 1472 return; 1473 1474 print_device(dev, indent); 1475 1476 for (child = TAILQ_FIRST(&dev->children); child; 1477 child = TAILQ_NEXT(child, link)) 1478 print_device_tree(child, indent+1); 1479} 1480 1481static void 1482print_driver_short(driver_t *driver, int indent) 1483{ 1484 if (!driver) 1485 return; 1486 1487 indentprintf(("driver %s: type = %s%s%s%s, softc size = %d\n", 1488 driver->name, 1489 /* yes, I know this looks silly, but going to bed at 1490 * two o'clock and having to get up at 7:30 again is silly 1491 * as well. As is sticking your head in a bucket of water. 1492 */ 1493 (driver->type == DRIVER_TYPE_TTY? "tty":""), 1494 (driver->type == DRIVER_TYPE_BIO? "bio":""), 1495 (driver->type == DRIVER_TYPE_NET? "net":""), 1496 (driver->type == DRIVER_TYPE_MISC? "misc":""), 1497 driver->softc)); 1498} 1499 1500static void 1501print_driver(driver_t *driver, int indent) 1502{ 1503 if (!driver) 1504 return; 1505 1506 print_driver_short(driver, indent); 1507 indentprintf(("Methods:\n")); 1508 print_method_list(driver->methods, indent+1); 1509 indentprintf(("Operations:\n")); 1510 print_device_ops(driver->ops, indent+1); 1511} 1512 1513 1514static void 1515print_driver_list(driver_list_t drivers, int indent) 1516{ 1517 driver_t *driver; 1518 1519 for (driver = TAILQ_FIRST(&drivers); driver; 1520 driver = TAILQ_NEXT(driver, link)) 1521 print_driver(driver, indent); 1522} 1523 1524static void 1525print_devclass_short(devclass_t dc, int indent) 1526{ 1527 if ( !dc ) 1528 return; 1529 1530 indentprintf(("devclass %s: max units = %d, next unit = %d\n", 1531 dc->name, dc->maxunit, dc->nextunit)); 1532} 1533 1534static void 1535print_devclass(devclass_t dc, int indent) 1536{ 1537 int i; 1538 1539 if ( !dc ) 1540 return; 1541 1542 print_devclass_short(dc, indent); 1543 indentprintf(("Drivers:\n")); 1544 print_driver_list(dc->drivers, indent+1); 1545 1546 indentprintf(("Devices:\n")); 1547 for (i = 0; i < dc->maxunit; i++) 1548 if (dc->devices[i]) 1549 print_device(dc->devices[i], indent+1); 1550} 1551 1552void 1553print_devclass_list_short(void) 1554{ 1555 devclass_t dc; 1556 1557 printf("Short listing of devclasses, drivers & devices:\n"); 1558 for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link)) 1559 print_devclass_short(dc, 0); 1560} 1561 1562void 1563print_devclass_list(void) 1564{ 1565 devclass_t dc; 1566 1567 printf("Full listing of devclasses, drivers & devices:\n"); 1568 for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link)) 1569 print_devclass(dc, 0); 1570} 1571 1572#endif 1573