1 /* $NetBSD: subr_autoconf.c,v 1.108 2006/02/23 05:48:12 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 2000 Christopher G. Demetriou 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the 18 * NetBSD Project. See http://www.NetBSD.org/ for 19 * information about NetBSD. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )-- 35 */ 36 37 /* 38 * Copyright (c) 1992, 1993 39 * The Regents of the University of California. All rights reserved. 40 * 41 * This software was developed by the Computer Systems Engineering group 42 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 43 * contributed to Berkeley. 44 * 45 * All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Lawrence Berkeley Laboratories. 49 * 50 * Redistribution and use in source and binary forms, with or without 51 * modification, are permitted provided that the following conditions 52 * are met: 53 * 1. Redistributions of source code must retain the above copyright 54 * notice, this list of conditions and the following disclaimer. 55 * 2. Redistributions in binary form must reproduce the above copyright 56 * notice, this list of conditions and the following disclaimer in the 57 * documentation and/or other materials provided with the distribution. 58 * 3. Neither the name of the University nor the names of its contributors 59 * may be used to endorse or promote products derived from this software 60 * without specific prior written permission. 61 * 62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 72 * SUCH DAMAGE. 73 * 74 * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL) 75 * 76 * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94 77 */ 78 79 #include <sys/cdefs.h> 80 __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.108 2006/02/23 05:48:12 thorpej Exp $"); 81 82 #include "opt_ddb.h" 83 84 #include <sys/param.h> 85 #include <sys/device.h> 86 #include <sys/malloc.h> 87 #include <sys/systm.h> 88 #include <sys/kernel.h> 89 #include <sys/errno.h> 90 #include <sys/proc.h> 91 #include <sys/reboot.h> 92 #include <sys/properties.h> 93 #include <machine/limits.h> 94 95 #include "opt_userconf.h" 96 #ifdef USERCONF 97 #include <sys/userconf.h> 98 #endif 99 100 #ifdef __i386__ 101 #include "opt_splash.h" 102 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 103 #include <dev/splash/splash.h> 104 extern struct splash_progress *splash_progress_state; 105 #endif 106 #endif 107 108 /* 109 * Autoconfiguration subroutines. 110 */ 111 112 /* 113 * ioconf.c exports exactly two names: cfdata and cfroots. All system 114 * devices and drivers are found via these tables. 115 */ 116 extern struct cfdata cfdata[]; 117 extern const short cfroots[]; 118 119 /* 120 * List of all cfdriver structures. We use this to detect duplicates 121 * when other cfdrivers are loaded. 122 */ 123 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers); 124 extern struct cfdriver * const cfdriver_list_initial[]; 125 126 /* 127 * Initial list of cfattach's. 128 */ 129 extern const struct cfattachinit cfattachinit[]; 130 131 /* 132 * List of cfdata tables. We always have one such list -- the one 133 * built statically when the kernel was configured. 134 */ 135 struct cftablelist allcftables; 136 static struct cftable initcftable; 137 138 /* 139 * Database of device properties. 140 */ 141 static propdb_t dev_propdb; 142 143 #define ROOT ((device_t)NULL) 144 145 struct matchinfo { 146 cfsubmatch_t fn; 147 struct device *parent; 148 const int *locs; 149 void *aux; 150 struct cfdata *match; 151 int pri; 152 }; 153 154 static char *number(char *, int); 155 static void mapply(struct matchinfo *, cfdata_t); 156 157 struct deferred_config { 158 TAILQ_ENTRY(deferred_config) dc_queue; 159 device_t dc_dev; 160 void (*dc_func)(device_t); 161 }; 162 163 TAILQ_HEAD(deferred_config_head, deferred_config); 164 165 struct deferred_config_head deferred_config_queue; 166 struct deferred_config_head interrupt_config_queue; 167 168 static void config_process_deferred(struct deferred_config_head *, device_t); 169 170 /* Hooks to finalize configuration once all real devices have been found. */ 171 struct finalize_hook { 172 TAILQ_ENTRY(finalize_hook) f_list; 173 int (*f_func)(device_t); 174 device_t f_dev; 175 }; 176 static TAILQ_HEAD(, finalize_hook) config_finalize_list; 177 static int config_finalize_done; 178 179 /* list of all devices */ 180 struct devicelist alldevs; 181 182 volatile int config_pending; /* semaphore for mountroot */ 183 184 #define STREQ(s1, s2) \ 185 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0) 186 187 static int config_initialized; /* config_init() has been called. */ 188 189 static int config_do_twiddle; 190 191 /* 192 * Initialize the autoconfiguration data structures. Normally this 193 * is done by configure(), but some platforms need to do this very 194 * early (to e.g. initialize the console). 195 */ 196 void 197 config_init(void) 198 { 199 const struct cfattachinit *cfai; 200 int i, j; 201 202 if (config_initialized) 203 return; 204 205 /* allcfdrivers is statically initialized. */ 206 for (i = 0; cfdriver_list_initial[i] != NULL; i++) { 207 if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0) 208 panic("configure: duplicate `%s' drivers", 209 cfdriver_list_initial[i]->cd_name); 210 } 211 212 for (cfai = &cfattachinit[0]; cfai->cfai_name != NULL; cfai++) { 213 for (j = 0; cfai->cfai_list[j] != NULL; j++) { 214 if (config_cfattach_attach(cfai->cfai_name, 215 cfai->cfai_list[j]) != 0) 216 panic("configure: duplicate `%s' attachment " 217 "of `%s' driver", 218 cfai->cfai_list[j]->ca_name, 219 cfai->cfai_name); 220 } 221 } 222 223 TAILQ_INIT(&allcftables); 224 initcftable.ct_cfdata = cfdata; 225 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list); 226 227 TAILQ_INIT(&deferred_config_queue); 228 TAILQ_INIT(&interrupt_config_queue); 229 TAILQ_INIT(&config_finalize_list); 230 TAILQ_INIT(&alldevs); 231 232 config_initialized = 1; 233 } 234 235 /* 236 * Configure the system's hardware. 237 */ 238 void 239 configure(void) 240 { 241 int errcnt; 242 243 /* Initialize data structures. */ 244 config_init(); 245 246 /* Initialize the device property database. */ 247 dev_propdb = propdb_create("device properties"); 248 if (dev_propdb == NULL) 249 panic("unable to create device property database"); 250 251 #ifdef USERCONF 252 if (boothowto & RB_USERCONF) 253 user_config(); 254 #endif 255 256 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) { 257 config_do_twiddle = 1; 258 printf_nolog("Detecting hardware..."); 259 } 260 261 /* 262 * Do the machine-dependent portion of autoconfiguration. This 263 * sets the configuration machinery here in motion by "finding" 264 * the root bus. When this function returns, we expect interrupts 265 * to be enabled. 266 */ 267 cpu_configure(); 268 269 /* 270 * Now that we've found all the hardware, start the real time 271 * and statistics clocks. 272 */ 273 initclocks(); 274 275 cold = 0; /* clocks are running, we're warm now! */ 276 277 /* 278 * Now callback to finish configuration for devices which want 279 * to do this once interrupts are enabled. 280 */ 281 config_process_deferred(&interrupt_config_queue, NULL); 282 283 errcnt = aprint_get_error_count(); 284 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 && 285 (boothowto & AB_VERBOSE) == 0) { 286 if (config_do_twiddle) { 287 config_do_twiddle = 0; 288 printf_nolog("done.\n"); 289 } 290 if (errcnt != 0) { 291 printf("WARNING: %d error%s while detecting hardware; " 292 "check system log.\n", errcnt, 293 errcnt == 1 ? "" : "s"); 294 } 295 } 296 } 297 298 /* 299 * Add a cfdriver to the system. 300 */ 301 int 302 config_cfdriver_attach(struct cfdriver *cd) 303 { 304 struct cfdriver *lcd; 305 306 /* Make sure this driver isn't already in the system. */ 307 LIST_FOREACH(lcd, &allcfdrivers, cd_list) { 308 if (STREQ(lcd->cd_name, cd->cd_name)) 309 return (EEXIST); 310 } 311 312 LIST_INIT(&cd->cd_attach); 313 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list); 314 315 return (0); 316 } 317 318 /* 319 * Remove a cfdriver from the system. 320 */ 321 int 322 config_cfdriver_detach(struct cfdriver *cd) 323 { 324 int i; 325 326 /* Make sure there are no active instances. */ 327 for (i = 0; i < cd->cd_ndevs; i++) { 328 if (cd->cd_devs[i] != NULL) 329 return (EBUSY); 330 } 331 332 /* ...and no attachments loaded. */ 333 if (LIST_EMPTY(&cd->cd_attach) == 0) 334 return (EBUSY); 335 336 LIST_REMOVE(cd, cd_list); 337 338 KASSERT(cd->cd_devs == NULL); 339 340 return (0); 341 } 342 343 /* 344 * Look up a cfdriver by name. 345 */ 346 struct cfdriver * 347 config_cfdriver_lookup(const char *name) 348 { 349 struct cfdriver *cd; 350 351 LIST_FOREACH(cd, &allcfdrivers, cd_list) { 352 if (STREQ(cd->cd_name, name)) 353 return (cd); 354 } 355 356 return (NULL); 357 } 358 359 /* 360 * Add a cfattach to the specified driver. 361 */ 362 int 363 config_cfattach_attach(const char *driver, struct cfattach *ca) 364 { 365 struct cfattach *lca; 366 struct cfdriver *cd; 367 368 cd = config_cfdriver_lookup(driver); 369 if (cd == NULL) 370 return (ESRCH); 371 372 /* Make sure this attachment isn't already on this driver. */ 373 LIST_FOREACH(lca, &cd->cd_attach, ca_list) { 374 if (STREQ(lca->ca_name, ca->ca_name)) 375 return (EEXIST); 376 } 377 378 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list); 379 380 return (0); 381 } 382 383 /* 384 * Remove a cfattach from the specified driver. 385 */ 386 int 387 config_cfattach_detach(const char *driver, struct cfattach *ca) 388 { 389 struct cfdriver *cd; 390 device_t dev; 391 int i; 392 393 cd = config_cfdriver_lookup(driver); 394 if (cd == NULL) 395 return (ESRCH); 396 397 /* Make sure there are no active instances. */ 398 for (i = 0; i < cd->cd_ndevs; i++) { 399 if ((dev = cd->cd_devs[i]) == NULL) 400 continue; 401 if (dev->dv_cfattach == ca) 402 return (EBUSY); 403 } 404 405 LIST_REMOVE(ca, ca_list); 406 407 return (0); 408 } 409 410 /* 411 * Look up a cfattach by name. 412 */ 413 static struct cfattach * 414 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname) 415 { 416 struct cfattach *ca; 417 418 LIST_FOREACH(ca, &cd->cd_attach, ca_list) { 419 if (STREQ(ca->ca_name, atname)) 420 return (ca); 421 } 422 423 return (NULL); 424 } 425 426 /* 427 * Look up a cfattach by driver/attachment name. 428 */ 429 struct cfattach * 430 config_cfattach_lookup(const char *name, const char *atname) 431 { 432 struct cfdriver *cd; 433 434 cd = config_cfdriver_lookup(name); 435 if (cd == NULL) 436 return (NULL); 437 438 return (config_cfattach_lookup_cd(cd, atname)); 439 } 440 441 /* 442 * Apply the matching function and choose the best. This is used 443 * a few times and we want to keep the code small. 444 */ 445 static void 446 mapply(struct matchinfo *m, cfdata_t cf) 447 { 448 int pri; 449 450 if (m->fn != NULL) { 451 pri = (*m->fn)(m->parent, cf, m->locs, m->aux); 452 } else { 453 pri = config_match(m->parent, cf, m->aux); 454 } 455 if (pri > m->pri) { 456 m->match = cf; 457 m->pri = pri; 458 } 459 } 460 461 int 462 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux) 463 { 464 const struct cfiattrdata *ci; 465 const struct cflocdesc *cl; 466 int nlocs, i; 467 468 ci = cfiattr_lookup(cf->cf_pspec->cfp_iattr, parent->dv_cfdriver); 469 KASSERT(ci); 470 nlocs = ci->ci_loclen; 471 for (i = 0; i < nlocs; i++) { 472 cl = &ci->ci_locdesc[i]; 473 /* !cld_defaultstr means no default value */ 474 if ((!(cl->cld_defaultstr) 475 || (cf->cf_loc[i] != cl->cld_default)) 476 && cf->cf_loc[i] != locs[i]) 477 return (0); 478 } 479 480 return (config_match(parent, cf, aux)); 481 } 482 483 /* 484 * Helper function: check whether the driver supports the interface attribute 485 * and return its descriptor structure. 486 */ 487 static const struct cfiattrdata * 488 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia) 489 { 490 const struct cfiattrdata * const *cpp; 491 492 if (cd->cd_attrs == NULL) 493 return (0); 494 495 for (cpp = cd->cd_attrs; *cpp; cpp++) { 496 if (STREQ((*cpp)->ci_name, ia)) { 497 /* Match. */ 498 return (*cpp); 499 } 500 } 501 return (0); 502 } 503 504 /* 505 * Lookup an interface attribute description by name. 506 * If the driver is given, consider only its supported attributes. 507 */ 508 const struct cfiattrdata * 509 cfiattr_lookup(const char *name, const struct cfdriver *cd) 510 { 511 const struct cfdriver *d; 512 const struct cfiattrdata *ia; 513 514 if (cd) 515 return (cfdriver_get_iattr(cd, name)); 516 517 LIST_FOREACH(d, &allcfdrivers, cd_list) { 518 ia = cfdriver_get_iattr(d, name); 519 if (ia) 520 return (ia); 521 } 522 return (0); 523 } 524 525 /* 526 * Determine if `parent' is a potential parent for a device spec based 527 * on `cfp'. 528 */ 529 static int 530 cfparent_match(const device_t parent, const struct cfparent *cfp) 531 { 532 struct cfdriver *pcd; 533 534 /* We don't match root nodes here. */ 535 if (cfp == NULL) 536 return (0); 537 538 pcd = parent->dv_cfdriver; 539 KASSERT(pcd != NULL); 540 541 /* 542 * First, ensure this parent has the correct interface 543 * attribute. 544 */ 545 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr)) 546 return (0); 547 548 /* 549 * If no specific parent device instance was specified (i.e. 550 * we're attaching to the attribute only), we're done! 551 */ 552 if (cfp->cfp_parent == NULL) 553 return (1); 554 555 /* 556 * Check the parent device's name. 557 */ 558 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0) 559 return (0); /* not the same parent */ 560 561 /* 562 * Make sure the unit number matches. 563 */ 564 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */ 565 cfp->cfp_unit == parent->dv_unit) 566 return (1); 567 568 /* Unit numbers don't match. */ 569 return (0); 570 } 571 572 /* 573 * Helper for config_cfdata_attach(): check all devices whether it could be 574 * parent any attachment in the config data table passed, and rescan. 575 */ 576 static void 577 rescan_with_cfdata(const struct cfdata *cf) 578 { 579 device_t d; 580 const struct cfdata *cf1; 581 582 /* 583 * "alldevs" is likely longer than an LKM's cfdata, so make it 584 * the outer loop. 585 */ 586 TAILQ_FOREACH(d, &alldevs, dv_list) { 587 588 if (!(d->dv_cfattach->ca_rescan)) 589 continue; 590 591 for (cf1 = cf; cf1->cf_name; cf1++) { 592 593 if (!cfparent_match(d, cf1->cf_pspec)) 594 continue; 595 596 (*d->dv_cfattach->ca_rescan)(d, 597 cf1->cf_pspec->cfp_iattr, cf1->cf_loc); 598 } 599 } 600 } 601 602 /* 603 * Attach a supplemental config data table and rescan potential 604 * parent devices if required. 605 */ 606 int 607 config_cfdata_attach(cfdata_t cf, int scannow) 608 { 609 struct cftable *ct; 610 611 ct = malloc(sizeof(struct cftable), M_DEVBUF, M_WAITOK); 612 ct->ct_cfdata = cf; 613 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list); 614 615 if (scannow) 616 rescan_with_cfdata(cf); 617 618 return (0); 619 } 620 621 /* 622 * Helper for config_cfdata_detach: check whether a device is 623 * found through any attachment in the config data table. 624 */ 625 static int 626 dev_in_cfdata(const struct device *d, const struct cfdata *cf) 627 { 628 const struct cfdata *cf1; 629 630 for (cf1 = cf; cf1->cf_name; cf1++) 631 if (d->dv_cfdata == cf1) 632 return (1); 633 634 return (0); 635 } 636 637 /* 638 * Detach a supplemental config data table. Detach all devices found 639 * through that table (and thus keeping references to it) before. 640 */ 641 int 642 config_cfdata_detach(cfdata_t cf) 643 { 644 device_t d; 645 int error; 646 struct cftable *ct; 647 648 again: 649 TAILQ_FOREACH(d, &alldevs, dv_list) { 650 if (dev_in_cfdata(d, cf)) { 651 error = config_detach(d, 0); 652 if (error) { 653 aprint_error("%s: unable to detach instance\n", 654 d->dv_xname); 655 return (error); 656 } 657 goto again; 658 } 659 } 660 661 TAILQ_FOREACH(ct, &allcftables, ct_list) { 662 if (ct->ct_cfdata == cf) { 663 TAILQ_REMOVE(&allcftables, ct, ct_list); 664 free(ct, M_DEVBUF); 665 return (0); 666 } 667 } 668 669 /* not found -- shouldn't happen */ 670 return (EINVAL); 671 } 672 673 /* 674 * Invoke the "match" routine for a cfdata entry on behalf of 675 * an external caller, usually a "submatch" routine. 676 */ 677 int 678 config_match(device_t parent, cfdata_t cf, void *aux) 679 { 680 struct cfattach *ca; 681 682 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname); 683 if (ca == NULL) { 684 /* No attachment for this entry, oh well. */ 685 return (0); 686 } 687 688 return ((*ca->ca_match)(parent, cf, aux)); 689 } 690 691 /* 692 * Iterate over all potential children of some device, calling the given 693 * function (default being the child's match function) for each one. 694 * Nonzero returns are matches; the highest value returned is considered 695 * the best match. Return the `found child' if we got a match, or NULL 696 * otherwise. The `aux' pointer is simply passed on through. 697 * 698 * Note that this function is designed so that it can be used to apply 699 * an arbitrary function to all potential children (its return value 700 * can be ignored). 701 */ 702 cfdata_t 703 config_search_loc(cfsubmatch_t fn, device_t parent, 704 const char *ifattr, const int *locs, void *aux) 705 { 706 struct cftable *ct; 707 cfdata_t cf; 708 struct matchinfo m; 709 710 KASSERT(config_initialized); 711 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr)); 712 713 m.fn = fn; 714 m.parent = parent; 715 m.locs = locs; 716 m.aux = aux; 717 m.match = NULL; 718 m.pri = 0; 719 720 TAILQ_FOREACH(ct, &allcftables, ct_list) { 721 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 722 723 /* We don't match root nodes here. */ 724 if (!cf->cf_pspec) 725 continue; 726 727 /* 728 * Skip cf if no longer eligible, otherwise scan 729 * through parents for one matching `parent', and 730 * try match function. 731 */ 732 if (cf->cf_fstate == FSTATE_FOUND) 733 continue; 734 if (cf->cf_fstate == FSTATE_DNOTFOUND || 735 cf->cf_fstate == FSTATE_DSTAR) 736 continue; 737 738 /* 739 * If an interface attribute was specified, 740 * consider only children which attach to 741 * that attribute. 742 */ 743 if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr)) 744 continue; 745 746 if (cfparent_match(parent, cf->cf_pspec)) 747 mapply(&m, cf); 748 } 749 } 750 return (m.match); 751 } 752 753 cfdata_t 754 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr, 755 void *aux) 756 { 757 758 return (config_search_loc(fn, parent, ifattr, NULL, aux)); 759 } 760 761 /* 762 * Find the given root device. 763 * This is much like config_search, but there is no parent. 764 * Don't bother with multiple cfdata tables; the root node 765 * must always be in the initial table. 766 */ 767 cfdata_t 768 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux) 769 { 770 cfdata_t cf; 771 const short *p; 772 struct matchinfo m; 773 774 m.fn = fn; 775 m.parent = ROOT; 776 m.aux = aux; 777 m.match = NULL; 778 m.pri = 0; 779 /* 780 * Look at root entries for matching name. We do not bother 781 * with found-state here since only one root should ever be 782 * searched (and it must be done first). 783 */ 784 for (p = cfroots; *p >= 0; p++) { 785 cf = &cfdata[*p]; 786 if (strcmp(cf->cf_name, rootname) == 0) 787 mapply(&m, cf); 788 } 789 return (m.match); 790 } 791 792 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" }; 793 794 /* 795 * The given `aux' argument describes a device that has been found 796 * on the given parent, but not necessarily configured. Locate the 797 * configuration data for that device (using the submatch function 798 * provided, or using candidates' cd_match configuration driver 799 * functions) and attach it, and return true. If the device was 800 * not configured, call the given `print' function and return 0. 801 */ 802 device_t 803 config_found_sm_loc(device_t parent, 804 const char *ifattr, const int *locs, void *aux, 805 cfprint_t print, cfsubmatch_t submatch) 806 { 807 cfdata_t cf; 808 809 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 810 if (splash_progress_state) 811 splash_progress_update(splash_progress_state); 812 #endif 813 814 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux))) 815 return(config_attach_loc(parent, cf, locs, aux, print)); 816 if (print) { 817 if (config_do_twiddle) 818 twiddle(); 819 aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]); 820 } 821 822 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 823 if (splash_progress_state) 824 splash_progress_update(splash_progress_state); 825 #endif 826 827 return (NULL); 828 } 829 830 device_t 831 config_found_ia(device_t parent, const char *ifattr, void *aux, 832 cfprint_t print) 833 { 834 835 return (config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL)); 836 } 837 838 device_t 839 config_found(device_t parent, void *aux, cfprint_t print) 840 { 841 842 return (config_found_sm_loc(parent, NULL, NULL, aux, print, NULL)); 843 } 844 845 /* 846 * As above, but for root devices. 847 */ 848 device_t 849 config_rootfound(const char *rootname, void *aux) 850 { 851 cfdata_t cf; 852 853 if ((cf = config_rootsearch((cfsubmatch_t)NULL, rootname, aux)) != NULL) 854 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL)); 855 aprint_error("root device %s not configured\n", rootname); 856 return (NULL); 857 } 858 859 /* just like sprintf(buf, "%d") except that it works from the end */ 860 static char * 861 number(char *ep, int n) 862 { 863 864 *--ep = 0; 865 while (n >= 10) { 866 *--ep = (n % 10) + '0'; 867 n /= 10; 868 } 869 *--ep = n + '0'; 870 return (ep); 871 } 872 873 /* 874 * Expand the size of the cd_devs array if necessary. 875 */ 876 void 877 config_makeroom(int n, struct cfdriver *cd) 878 { 879 int old, new; 880 void **nsp; 881 882 if (n < cd->cd_ndevs) 883 return; 884 885 /* 886 * Need to expand the array. 887 */ 888 old = cd->cd_ndevs; 889 if (old == 0) 890 new = MINALLOCSIZE / sizeof(void *); 891 else 892 new = old * 2; 893 while (new <= n) 894 new *= 2; 895 cd->cd_ndevs = new; 896 nsp = malloc(new * sizeof(void *), M_DEVBUF, 897 cold ? M_NOWAIT : M_WAITOK); 898 if (nsp == NULL) 899 panic("config_attach: %sing dev array", 900 old != 0 ? "expand" : "creat"); 901 memset(nsp + old, 0, (new - old) * sizeof(void *)); 902 if (old != 0) { 903 memcpy(nsp, cd->cd_devs, old * sizeof(void *)); 904 free(cd->cd_devs, M_DEVBUF); 905 } 906 cd->cd_devs = nsp; 907 } 908 909 /* 910 * Attach a found device. Allocates memory for device variables. 911 */ 912 device_t 913 config_attach_loc(device_t parent, cfdata_t cf, 914 const int *locs, void *aux, cfprint_t print) 915 { 916 device_t dev; 917 struct cftable *ct; 918 struct cfdriver *cd; 919 struct cfattach *ca; 920 size_t lname, lunit; 921 const char *xunit; 922 int myunit; 923 char num[10]; 924 const struct cfiattrdata *ia; 925 926 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 927 if (splash_progress_state) 928 splash_progress_update(splash_progress_state); 929 #endif 930 931 cd = config_cfdriver_lookup(cf->cf_name); 932 KASSERT(cd != NULL); 933 934 ca = config_cfattach_lookup_cd(cd, cf->cf_atname); 935 KASSERT(ca != NULL); 936 937 if (ca->ca_devsize < sizeof(struct device)) 938 panic("config_attach"); 939 940 #ifndef __BROKEN_CONFIG_UNIT_USAGE 941 if (cf->cf_fstate == FSTATE_STAR) { 942 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++) 943 if (cd->cd_devs[myunit] == NULL) 944 break; 945 /* 946 * myunit is now the unit of the first NULL device pointer, 947 * or max(cd->cd_ndevs,cf->cf_unit). 948 */ 949 } else { 950 myunit = cf->cf_unit; 951 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 952 cf->cf_fstate = FSTATE_FOUND; 953 } 954 #else 955 myunit = cf->cf_unit; 956 if (cf->cf_fstate == FSTATE_STAR) 957 cf->cf_unit++; 958 else { 959 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND); 960 cf->cf_fstate = FSTATE_FOUND; 961 } 962 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */ 963 964 /* compute length of name and decimal expansion of unit number */ 965 lname = strlen(cd->cd_name); 966 xunit = number(&num[sizeof(num)], myunit); 967 lunit = &num[sizeof(num)] - xunit; 968 if (lname + lunit > sizeof(dev->dv_xname)) 969 panic("config_attach: device name too long"); 970 971 /* get memory for all device vars */ 972 dev = (device_t)malloc(ca->ca_devsize, M_DEVBUF, 973 cold ? M_NOWAIT : M_WAITOK); 974 if (!dev) 975 panic("config_attach: memory allocation for device softc failed"); 976 memset(dev, 0, ca->ca_devsize); 977 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */ 978 dev->dv_class = cd->cd_class; 979 dev->dv_cfdata = cf; 980 dev->dv_cfdriver = cd; 981 dev->dv_cfattach = ca; 982 dev->dv_unit = myunit; 983 memcpy(dev->dv_xname, cd->cd_name, lname); 984 memcpy(dev->dv_xname + lname, xunit, lunit); 985 dev->dv_parent = parent; 986 dev->dv_flags = DVF_ACTIVE; /* always initially active */ 987 if (locs) { 988 KASSERT(parent); /* no locators at root */ 989 ia = cfiattr_lookup(cf->cf_pspec->cfp_iattr, 990 parent->dv_cfdriver); 991 dev->dv_locators = malloc(ia->ci_loclen * sizeof(int), 992 M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 993 memcpy(dev->dv_locators, locs, ia->ci_loclen * sizeof(int)); 994 } 995 996 if (config_do_twiddle) 997 twiddle(); 998 else 999 aprint_naive("Found "); 1000 /* 1001 * We want the next two printfs for normal, verbose, and quiet, 1002 * but not silent (in which case, we're twiddling, instead). 1003 */ 1004 if (parent == ROOT) { 1005 aprint_naive("%s (root)", dev->dv_xname); 1006 aprint_normal("%s (root)", dev->dv_xname); 1007 } else { 1008 aprint_naive("%s at %s", dev->dv_xname, parent->dv_xname); 1009 aprint_normal("%s at %s", dev->dv_xname, parent->dv_xname); 1010 if (print) 1011 (void) (*print)(aux, NULL); 1012 } 1013 1014 /* put this device in the devices array */ 1015 config_makeroom(dev->dv_unit, cd); 1016 if (cd->cd_devs[dev->dv_unit]) 1017 panic("config_attach: duplicate %s", dev->dv_xname); 1018 cd->cd_devs[dev->dv_unit] = dev; 1019 1020 /* 1021 * Before attaching, clobber any unfound devices that are 1022 * otherwise identical. 1023 */ 1024 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1025 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1026 if (STREQ(cf->cf_name, cd->cd_name) && 1027 cf->cf_unit == dev->dv_unit) { 1028 if (cf->cf_fstate == FSTATE_NOTFOUND) 1029 cf->cf_fstate = FSTATE_FOUND; 1030 #ifdef __BROKEN_CONFIG_UNIT_USAGE 1031 /* 1032 * Bump the unit number on all starred cfdata 1033 * entries for this device. 1034 */ 1035 if (cf->cf_fstate == FSTATE_STAR) 1036 cf->cf_unit++; 1037 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 1038 } 1039 } 1040 } 1041 #ifdef __HAVE_DEVICE_REGISTER 1042 device_register(dev, aux); 1043 #endif 1044 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1045 if (splash_progress_state) 1046 splash_progress_update(splash_progress_state); 1047 #endif 1048 (*ca->ca_attach)(parent, dev, aux); 1049 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS) 1050 if (splash_progress_state) 1051 splash_progress_update(splash_progress_state); 1052 #endif 1053 config_process_deferred(&deferred_config_queue, dev); 1054 return (dev); 1055 } 1056 1057 device_t 1058 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print) 1059 { 1060 1061 return (config_attach_loc(parent, cf, NULL, aux, print)); 1062 } 1063 1064 /* 1065 * As above, but for pseudo-devices. Pseudo-devices attached in this 1066 * way are silently inserted into the device tree, and their children 1067 * attached. 1068 * 1069 * Note that because pseudo-devices are attached silently, any information 1070 * the attach routine wishes to print should be prefixed with the device 1071 * name by the attach routine. 1072 */ 1073 device_t 1074 config_attach_pseudo(cfdata_t cf) 1075 { 1076 device_t dev; 1077 struct cfdriver *cd; 1078 struct cfattach *ca; 1079 size_t lname, lunit; 1080 const char *xunit; 1081 int myunit; 1082 char num[10]; 1083 1084 cd = config_cfdriver_lookup(cf->cf_name); 1085 if (cd == NULL) 1086 return (NULL); 1087 1088 ca = config_cfattach_lookup_cd(cd, cf->cf_atname); 1089 if (ca == NULL) 1090 return (NULL); 1091 1092 if (ca->ca_devsize < sizeof(struct device)) 1093 panic("config_attach_pseudo"); 1094 1095 /* 1096 * We just ignore cf_fstate, instead doing everything with 1097 * cf_unit. 1098 * 1099 * XXX Should we change this and use FSTATE_NOTFOUND and 1100 * XXX FSTATE_STAR? 1101 */ 1102 1103 if (cf->cf_unit == DVUNIT_ANY) { 1104 for (myunit = 0; myunit < cd->cd_ndevs; myunit++) 1105 if (cd->cd_devs[myunit] == NULL) 1106 break; 1107 /* 1108 * myunit is now the unit of the first NULL device pointer. 1109 */ 1110 } else { 1111 myunit = cf->cf_unit; 1112 if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL) 1113 return (NULL); 1114 } 1115 1116 /* compute length of name and decimal expansion of unit number */ 1117 lname = strlen(cd->cd_name); 1118 xunit = number(&num[sizeof(num)], myunit); 1119 lunit = &num[sizeof(num)] - xunit; 1120 if (lname + lunit > sizeof(dev->dv_xname)) 1121 panic("config_attach_pseudo: device name too long"); 1122 1123 /* get memory for all device vars */ 1124 dev = (device_t)malloc(ca->ca_devsize, M_DEVBUF, 1125 cold ? M_NOWAIT : M_WAITOK); 1126 if (!dev) 1127 panic("config_attach_pseudo: memory allocation for device " 1128 "softc failed"); 1129 memset(dev, 0, ca->ca_devsize); 1130 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */ 1131 dev->dv_class = cd->cd_class; 1132 dev->dv_cfdata = cf; 1133 dev->dv_cfdriver = cd; 1134 dev->dv_cfattach = ca; 1135 dev->dv_unit = myunit; 1136 memcpy(dev->dv_xname, cd->cd_name, lname); 1137 memcpy(dev->dv_xname + lname, xunit, lunit); 1138 dev->dv_parent = ROOT; 1139 dev->dv_flags = DVF_ACTIVE; /* always initially active */ 1140 1141 /* put this device in the devices array */ 1142 config_makeroom(dev->dv_unit, cd); 1143 if (cd->cd_devs[dev->dv_unit]) 1144 panic("config_attach_pseudo: duplicate %s", dev->dv_xname); 1145 cd->cd_devs[dev->dv_unit] = dev; 1146 1147 #if 0 /* XXXJRT not yet */ 1148 #ifdef __HAVE_DEVICE_REGISTER 1149 device_register(dev, NULL); /* like a root node */ 1150 #endif 1151 #endif 1152 (*ca->ca_attach)(ROOT, dev, NULL); 1153 config_process_deferred(&deferred_config_queue, dev); 1154 return (dev); 1155 } 1156 1157 /* 1158 * Detach a device. Optionally forced (e.g. because of hardware 1159 * removal) and quiet. Returns zero if successful, non-zero 1160 * (an error code) otherwise. 1161 * 1162 * Note that this code wants to be run from a process context, so 1163 * that the detach can sleep to allow processes which have a device 1164 * open to run and unwind their stacks. 1165 */ 1166 int 1167 config_detach(device_t dev, int flags) 1168 { 1169 struct cftable *ct; 1170 cfdata_t cf; 1171 const struct cfattach *ca; 1172 struct cfdriver *cd; 1173 #ifdef DIAGNOSTIC 1174 device_t d; 1175 #endif 1176 int rv = 0, i; 1177 1178 #ifdef DIAGNOSTIC 1179 if (dev->dv_cfdata != NULL && 1180 dev->dv_cfdata->cf_fstate != FSTATE_FOUND && 1181 dev->dv_cfdata->cf_fstate != FSTATE_STAR) 1182 panic("config_detach: bad device fstate"); 1183 #endif 1184 cd = dev->dv_cfdriver; 1185 KASSERT(cd != NULL); 1186 1187 ca = dev->dv_cfattach; 1188 KASSERT(ca != NULL); 1189 1190 /* 1191 * Ensure the device is deactivated. If the device doesn't 1192 * have an activation entry point, we allow DVF_ACTIVE to 1193 * remain set. Otherwise, if DVF_ACTIVE is still set, the 1194 * device is busy, and the detach fails. 1195 */ 1196 if (ca->ca_activate != NULL) 1197 rv = config_deactivate(dev); 1198 1199 /* 1200 * Try to detach the device. If that's not possible, then 1201 * we either panic() (for the forced but failed case), or 1202 * return an error. 1203 */ 1204 if (rv == 0) { 1205 if (ca->ca_detach != NULL) 1206 rv = (*ca->ca_detach)(dev, flags); 1207 else 1208 rv = EOPNOTSUPP; 1209 } 1210 if (rv != 0) { 1211 if ((flags & DETACH_FORCE) == 0) 1212 return (rv); 1213 else 1214 panic("config_detach: forced detach of %s failed (%d)", 1215 dev->dv_xname, rv); 1216 } 1217 1218 /* 1219 * The device has now been successfully detached. 1220 */ 1221 1222 #ifdef DIAGNOSTIC 1223 /* 1224 * Sanity: If you're successfully detached, you should have no 1225 * children. (Note that because children must be attached 1226 * after parents, we only need to search the latter part of 1227 * the list.) 1228 */ 1229 for (d = TAILQ_NEXT(dev, dv_list); d != NULL; 1230 d = TAILQ_NEXT(d, dv_list)) { 1231 if (d->dv_parent == dev) { 1232 printf("config_detach: detached device %s" 1233 " has children %s\n", dev->dv_xname, d->dv_xname); 1234 panic("config_detach"); 1235 } 1236 } 1237 #endif 1238 1239 /* notify the parent that the child is gone */ 1240 if (dev->dv_parent) { 1241 device_t p = dev->dv_parent; 1242 if (p->dv_cfattach->ca_childdetached) 1243 (*p->dv_cfattach->ca_childdetached)(p, dev); 1244 } 1245 1246 /* 1247 * Mark cfdata to show that the unit can be reused, if possible. 1248 */ 1249 TAILQ_FOREACH(ct, &allcftables, ct_list) { 1250 for (cf = ct->ct_cfdata; cf->cf_name; cf++) { 1251 if (STREQ(cf->cf_name, cd->cd_name)) { 1252 if (cf->cf_fstate == FSTATE_FOUND && 1253 cf->cf_unit == dev->dv_unit) 1254 cf->cf_fstate = FSTATE_NOTFOUND; 1255 #ifdef __BROKEN_CONFIG_UNIT_USAGE 1256 /* 1257 * Note that we can only re-use a starred 1258 * unit number if the unit being detached 1259 * had the last assigned unit number. 1260 */ 1261 if (cf->cf_fstate == FSTATE_STAR && 1262 cf->cf_unit == dev->dv_unit + 1) 1263 cf->cf_unit--; 1264 #endif /* __BROKEN_CONFIG_UNIT_USAGE */ 1265 } 1266 } 1267 } 1268 1269 /* 1270 * Unlink from device list. 1271 */ 1272 TAILQ_REMOVE(&alldevs, dev, dv_list); 1273 1274 /* 1275 * Remove from cfdriver's array, tell the world (unless it was 1276 * a pseudo-device), and free softc. 1277 */ 1278 cd->cd_devs[dev->dv_unit] = NULL; 1279 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0) 1280 aprint_normal("%s detached\n", dev->dv_xname); 1281 if (dev->dv_locators) 1282 free(dev->dv_locators, M_DEVBUF); 1283 free(dev, M_DEVBUF); 1284 1285 /* 1286 * If the device now has no units in use, deallocate its softc array. 1287 */ 1288 for (i = 0; i < cd->cd_ndevs; i++) 1289 if (cd->cd_devs[i] != NULL) 1290 break; 1291 if (i == cd->cd_ndevs) { /* nothing found; deallocate */ 1292 free(cd->cd_devs, M_DEVBUF); 1293 cd->cd_devs = NULL; 1294 cd->cd_ndevs = 0; 1295 } 1296 1297 /* 1298 * Return success. 1299 */ 1300 return (0); 1301 } 1302 1303 int 1304 config_activate(device_t dev) 1305 { 1306 const struct cfattach *ca = dev->dv_cfattach; 1307 int rv = 0, oflags = dev->dv_flags; 1308 1309 if (ca->ca_activate == NULL) 1310 return (EOPNOTSUPP); 1311 1312 if ((dev->dv_flags & DVF_ACTIVE) == 0) { 1313 dev->dv_flags |= DVF_ACTIVE; 1314 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE); 1315 if (rv) 1316 dev->dv_flags = oflags; 1317 } 1318 return (rv); 1319 } 1320 1321 int 1322 config_deactivate(device_t dev) 1323 { 1324 const struct cfattach *ca = dev->dv_cfattach; 1325 int rv = 0, oflags = dev->dv_flags; 1326 1327 if (ca->ca_activate == NULL) 1328 return (EOPNOTSUPP); 1329 1330 if (dev->dv_flags & DVF_ACTIVE) { 1331 dev->dv_flags &= ~DVF_ACTIVE; 1332 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE); 1333 if (rv) 1334 dev->dv_flags = oflags; 1335 } 1336 return (rv); 1337 } 1338 1339 /* 1340 * Defer the configuration of the specified device until all 1341 * of its parent's devices have been attached. 1342 */ 1343 void 1344 config_defer(device_t dev, void (*func)(device_t)) 1345 { 1346 struct deferred_config *dc; 1347 1348 if (dev->dv_parent == NULL) 1349 panic("config_defer: can't defer config of a root device"); 1350 1351 #ifdef DIAGNOSTIC 1352 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL; 1353 dc = TAILQ_NEXT(dc, dc_queue)) { 1354 if (dc->dc_dev == dev) 1355 panic("config_defer: deferred twice"); 1356 } 1357 #endif 1358 1359 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 1360 if (dc == NULL) 1361 panic("config_defer: unable to allocate callback"); 1362 1363 dc->dc_dev = dev; 1364 dc->dc_func = func; 1365 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue); 1366 config_pending_incr(); 1367 } 1368 1369 /* 1370 * Defer some autoconfiguration for a device until after interrupts 1371 * are enabled. 1372 */ 1373 void 1374 config_interrupts(device_t dev, void (*func)(device_t)) 1375 { 1376 struct deferred_config *dc; 1377 1378 /* 1379 * If interrupts are enabled, callback now. 1380 */ 1381 if (cold == 0) { 1382 (*func)(dev); 1383 return; 1384 } 1385 1386 #ifdef DIAGNOSTIC 1387 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL; 1388 dc = TAILQ_NEXT(dc, dc_queue)) { 1389 if (dc->dc_dev == dev) 1390 panic("config_interrupts: deferred twice"); 1391 } 1392 #endif 1393 1394 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 1395 if (dc == NULL) 1396 panic("config_interrupts: unable to allocate callback"); 1397 1398 dc->dc_dev = dev; 1399 dc->dc_func = func; 1400 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue); 1401 config_pending_incr(); 1402 } 1403 1404 /* 1405 * Process a deferred configuration queue. 1406 */ 1407 static void 1408 config_process_deferred(struct deferred_config_head *queue, 1409 device_t parent) 1410 { 1411 struct deferred_config *dc, *ndc; 1412 1413 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) { 1414 ndc = TAILQ_NEXT(dc, dc_queue); 1415 if (parent == NULL || dc->dc_dev->dv_parent == parent) { 1416 TAILQ_REMOVE(queue, dc, dc_queue); 1417 (*dc->dc_func)(dc->dc_dev); 1418 free(dc, M_DEVBUF); 1419 config_pending_decr(); 1420 } 1421 } 1422 } 1423 1424 /* 1425 * Manipulate the config_pending semaphore. 1426 */ 1427 void 1428 config_pending_incr(void) 1429 { 1430 1431 config_pending++; 1432 } 1433 1434 void 1435 config_pending_decr(void) 1436 { 1437 1438 #ifdef DIAGNOSTIC 1439 if (config_pending == 0) 1440 panic("config_pending_decr: config_pending == 0"); 1441 #endif 1442 config_pending--; 1443 if (config_pending == 0) 1444 wakeup(&config_pending); 1445 } 1446 1447 /* 1448 * Register a "finalization" routine. Finalization routines are 1449 * called iteratively once all real devices have been found during 1450 * autoconfiguration, for as long as any one finalizer has done 1451 * any work. 1452 */ 1453 int 1454 config_finalize_register(device_t dev, int (*fn)(device_t)) 1455 { 1456 struct finalize_hook *f; 1457 1458 /* 1459 * If finalization has already been done, invoke the 1460 * callback function now. 1461 */ 1462 if (config_finalize_done) { 1463 while ((*fn)(dev) != 0) 1464 /* loop */ ; 1465 } 1466 1467 /* Ensure this isn't already on the list. */ 1468 TAILQ_FOREACH(f, &config_finalize_list, f_list) { 1469 if (f->f_func == fn && f->f_dev == dev) 1470 return (EEXIST); 1471 } 1472 1473 f = malloc(sizeof(*f), M_TEMP, M_WAITOK); 1474 f->f_func = fn; 1475 f->f_dev = dev; 1476 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list); 1477 1478 return (0); 1479 } 1480 1481 void 1482 config_finalize(void) 1483 { 1484 struct finalize_hook *f; 1485 int rv; 1486 1487 /* Run the hooks until none of them does any work. */ 1488 do { 1489 rv = 0; 1490 TAILQ_FOREACH(f, &config_finalize_list, f_list) 1491 rv |= (*f->f_func)(f->f_dev); 1492 } while (rv != 0); 1493 1494 config_finalize_done = 1; 1495 1496 /* Now free all the hooks. */ 1497 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) { 1498 TAILQ_REMOVE(&config_finalize_list, f, f_list); 1499 free(f, M_TEMP); 1500 } 1501 } 1502 1503 /* 1504 * Wrappers around prop_*() for handling device properties. 1505 */ 1506 int 1507 devprop_set(device_t dev, const char *name, void *val, size_t len, 1508 int type, int wait) 1509 { 1510 1511 return (prop_set(dev_propdb, dev, name, val, len, type, wait)); 1512 } 1513 1514 size_t 1515 devprop_list(device_t dev, char *names, size_t len) 1516 { 1517 1518 return (prop_list(dev_propdb, dev, names, len)); 1519 } 1520 1521 size_t 1522 devprop_get(device_t dev, const char *name, void *val, size_t len, int *typep) 1523 { 1524 1525 return (prop_get(dev_propdb, dev, name, val, len, typep)); 1526 } 1527 1528 int 1529 devprop_delete(device_t dev, const char *name) 1530 { 1531 1532 return (prop_delete(dev_propdb, dev, name)); 1533 } 1534 1535 int 1536 devprop_copy(device_t from, device_t to, int wait) 1537 { 1538 1539 return (prop_copy(dev_propdb, from, to, wait)); 1540 } 1541 1542 /* 1543 * device_lookup: 1544 * 1545 * Look up a device instance for a given driver. 1546 */ 1547 void * 1548 device_lookup(cfdriver_t cd, int unit) 1549 { 1550 1551 if (unit < 0 || unit >= cd->cd_ndevs) 1552 return (NULL); 1553 1554 return (cd->cd_devs[unit]); 1555 } 1556 1557 /* 1558 * Accessor functions for the device_t type. 1559 */ 1560 devclass_t 1561 device_class(device_t dev) 1562 { 1563 1564 return (dev->dv_class); 1565 } 1566 1567 cfdata_t 1568 device_cfdata(device_t dev) 1569 { 1570 1571 return (dev->dv_cfdata); 1572 } 1573 1574 cfdriver_t 1575 device_cfdriver(device_t dev) 1576 { 1577 1578 return (dev->dv_cfdriver); 1579 } 1580 1581 cfattach_t 1582 device_cfattach(device_t dev) 1583 { 1584 1585 return (dev->dv_cfattach); 1586 } 1587 1588 int 1589 device_unit(device_t dev) 1590 { 1591 1592 return (dev->dv_unit); 1593 } 1594 1595 const char * 1596 device_xname(device_t dev) 1597 { 1598 1599 return (dev->dv_xname); 1600 } 1601 1602 device_t 1603 device_parent(device_t dev) 1604 { 1605 1606 return (dev->dv_parent); 1607 } 1608 1609 boolean_t 1610 device_is_active(device_t dev) 1611 { 1612 1613 return ((dev->dv_flags & DVF_ACTIVE) != 0); 1614 } 1615 1616 int * 1617 device_locators(device_t dev) 1618 { 1619 1620 return (dev->dv_locators); 1621 } 1622 1623 /* 1624 * device_is_a: 1625 * 1626 * Returns true if the device is an instance of the specified 1627 * driver. 1628 */ 1629 boolean_t 1630 device_is_a(device_t dev, const char *dname) 1631 { 1632 1633 return (strcmp(dev->dv_cfdriver->cd_name, dname) == 0); 1634 } 1635