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