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