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