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