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