Home | History | Annotate | Line # | Download | only in kern
subr_autoconf.c revision 1.184
      1 /* $NetBSD: subr_autoconf.c,v 1.184 2009/09/16 22:45:24 dyoung 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.184 2009/09/16 22:45:24 dyoung Exp $");
     81 
     82 #ifdef _KERNEL_OPT
     83 #include "opt_ddb.h"
     84 #include "drvctl.h"
     85 #endif
     86 
     87 #include <sys/param.h>
     88 #include <sys/device.h>
     89 #include <sys/disklabel.h>
     90 #include <sys/conf.h>
     91 #include <sys/kauth.h>
     92 #include <sys/malloc.h>
     93 #include <sys/kmem.h>
     94 #include <sys/systm.h>
     95 #include <sys/kernel.h>
     96 #include <sys/errno.h>
     97 #include <sys/proc.h>
     98 #include <sys/reboot.h>
     99 #include <sys/kthread.h>
    100 #include <sys/buf.h>
    101 #include <sys/dirent.h>
    102 #include <sys/vnode.h>
    103 #include <sys/mount.h>
    104 #include <sys/namei.h>
    105 #include <sys/unistd.h>
    106 #include <sys/fcntl.h>
    107 #include <sys/lockf.h>
    108 #include <sys/callout.h>
    109 #include <sys/devmon.h>
    110 #include <sys/cpu.h>
    111 #include <sys/sysctl.h>
    112 
    113 #include <sys/disk.h>
    114 
    115 #include <machine/limits.h>
    116 
    117 #if defined(__i386__) && defined(_KERNEL_OPT)
    118 #include "opt_splash.h"
    119 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
    120 #include <dev/splash/splash.h>
    121 extern struct splash_progress *splash_progress_state;
    122 #endif
    123 #endif
    124 
    125 /*
    126  * Autoconfiguration subroutines.
    127  */
    128 
    129 /*
    130  * ioconf.c exports exactly two names: cfdata and cfroots.  All system
    131  * devices and drivers are found via these tables.
    132  */
    133 extern struct cfdata cfdata[];
    134 extern const short cfroots[];
    135 
    136 /*
    137  * List of all cfdriver structures.  We use this to detect duplicates
    138  * when other cfdrivers are loaded.
    139  */
    140 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
    141 extern struct cfdriver * const cfdriver_list_initial[];
    142 
    143 /*
    144  * Initial list of cfattach's.
    145  */
    146 extern const struct cfattachinit cfattachinit[];
    147 
    148 /*
    149  * List of cfdata tables.  We always have one such list -- the one
    150  * built statically when the kernel was configured.
    151  */
    152 struct cftablelist allcftables = TAILQ_HEAD_INITIALIZER(allcftables);
    153 static struct cftable initcftable;
    154 
    155 #define	ROOT ((device_t)NULL)
    156 
    157 struct matchinfo {
    158 	cfsubmatch_t fn;
    159 	struct	device *parent;
    160 	const int *locs;
    161 	void	*aux;
    162 	struct	cfdata *match;
    163 	int	pri;
    164 };
    165 
    166 static char *number(char *, int);
    167 static void mapply(struct matchinfo *, cfdata_t);
    168 static device_t config_devalloc(const device_t, const cfdata_t, const int *);
    169 static void config_devdealloc(device_t);
    170 static void config_makeroom(int, struct cfdriver *);
    171 static void config_devlink(device_t);
    172 static void config_devunlink(device_t);
    173 
    174 static void pmflock_debug(device_t, const char *, int);
    175 
    176 static device_t deviter_next1(deviter_t *);
    177 static void deviter_reinit(deviter_t *);
    178 
    179 struct deferred_config {
    180 	TAILQ_ENTRY(deferred_config) dc_queue;
    181 	device_t dc_dev;
    182 	void (*dc_func)(device_t);
    183 };
    184 
    185 TAILQ_HEAD(deferred_config_head, deferred_config);
    186 
    187 struct deferred_config_head deferred_config_queue =
    188 	TAILQ_HEAD_INITIALIZER(deferred_config_queue);
    189 struct deferred_config_head interrupt_config_queue =
    190 	TAILQ_HEAD_INITIALIZER(interrupt_config_queue);
    191 int interrupt_config_threads = 8;
    192 
    193 static void config_process_deferred(struct deferred_config_head *, device_t);
    194 
    195 /* Hooks to finalize configuration once all real devices have been found. */
    196 struct finalize_hook {
    197 	TAILQ_ENTRY(finalize_hook) f_list;
    198 	int (*f_func)(device_t);
    199 	device_t f_dev;
    200 };
    201 static TAILQ_HEAD(, finalize_hook) config_finalize_list =
    202 	TAILQ_HEAD_INITIALIZER(config_finalize_list);
    203 static int config_finalize_done;
    204 
    205 /* list of all devices */
    206 struct devicelist alldevs = TAILQ_HEAD_INITIALIZER(alldevs);
    207 kcondvar_t alldevs_cv;
    208 kmutex_t alldevs_mtx;
    209 static int alldevs_nread = 0;
    210 static int alldevs_nwrite = 0;
    211 static lwp_t *alldevs_writer = NULL;
    212 
    213 static int config_pending;		/* semaphore for mountroot */
    214 static kmutex_t config_misc_lock;
    215 static kcondvar_t config_misc_cv;
    216 
    217 static int detachall = 0;
    218 
    219 #define	STREQ(s1, s2)			\
    220 	(*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
    221 
    222 static int config_initialized;		/* config_init() has been called. */
    223 
    224 static int config_do_twiddle;
    225 static callout_t config_twiddle_ch;
    226 
    227 static void sysctl_detach_setup(struct sysctllog **);
    228 
    229 /*
    230  * Initialize the autoconfiguration data structures.  Normally this
    231  * is done by configure(), but some platforms need to do this very
    232  * early (to e.g. initialize the console).
    233  */
    234 void
    235 config_init(void)
    236 {
    237 	const struct cfattachinit *cfai;
    238 	int i, j;
    239 
    240 	if (config_initialized)
    241 		return;
    242 
    243 	mutex_init(&alldevs_mtx, MUTEX_DEFAULT, IPL_NONE);
    244 	cv_init(&alldevs_cv, "alldevs");
    245 
    246 	mutex_init(&config_misc_lock, MUTEX_DEFAULT, IPL_NONE);
    247 	cv_init(&config_misc_cv, "cfgmisc");
    248 
    249 	callout_init(&config_twiddle_ch, CALLOUT_MPSAFE);
    250 
    251 	/* allcfdrivers is statically initialized. */
    252 	for (i = 0; cfdriver_list_initial[i] != NULL; i++) {
    253 		if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0)
    254 			panic("configure: duplicate `%s' drivers",
    255 			    cfdriver_list_initial[i]->cd_name);
    256 	}
    257 
    258 	for (cfai = &cfattachinit[0]; cfai->cfai_name != NULL; cfai++) {
    259 		for (j = 0; cfai->cfai_list[j] != NULL; j++) {
    260 			if (config_cfattach_attach(cfai->cfai_name,
    261 						   cfai->cfai_list[j]) != 0)
    262 				panic("configure: duplicate `%s' attachment "
    263 				    "of `%s' driver",
    264 				    cfai->cfai_list[j]->ca_name,
    265 				    cfai->cfai_name);
    266 		}
    267 	}
    268 
    269 	initcftable.ct_cfdata = cfdata;
    270 	TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
    271 	sysctl_detach_setup(NULL);
    272 
    273 	config_initialized = 1;
    274 }
    275 
    276 void
    277 config_deferred(device_t dev)
    278 {
    279 	config_process_deferred(&deferred_config_queue, dev);
    280 	config_process_deferred(&interrupt_config_queue, dev);
    281 }
    282 
    283 static void
    284 config_interrupts_thread(void *cookie)
    285 {
    286 	struct deferred_config *dc;
    287 
    288 	while ((dc = TAILQ_FIRST(&interrupt_config_queue)) != NULL) {
    289 		TAILQ_REMOVE(&interrupt_config_queue, dc, dc_queue);
    290 		(*dc->dc_func)(dc->dc_dev);
    291 		kmem_free(dc, sizeof(*dc));
    292 		config_pending_decr();
    293 	}
    294 	kthread_exit(0);
    295 }
    296 
    297 void
    298 config_create_interruptthreads()
    299 {
    300 	int i;
    301 
    302 	for (i = 0; i < interrupt_config_threads; i++) {
    303 		(void)kthread_create(PRI_NONE, 0, NULL,
    304 		    config_interrupts_thread, NULL, NULL, "config");
    305 	}
    306 }
    307 
    308 /*
    309  * Announce device attach/detach to userland listeners.
    310  */
    311 static void
    312 devmon_report_device(device_t dev, bool isattach)
    313 {
    314 #if NDRVCTL > 0
    315 	prop_dictionary_t ev;
    316 	const char *parent;
    317 	const char *what;
    318 	device_t pdev = device_parent(dev);
    319 
    320 	ev = prop_dictionary_create();
    321 	if (ev == NULL)
    322 		return;
    323 
    324 	what = (isattach ? "device-attach" : "device-detach");
    325 	parent = (pdev == NULL ? "root" : device_xname(pdev));
    326 	if (!prop_dictionary_set_cstring(ev, "device", device_xname(dev)) ||
    327 	    !prop_dictionary_set_cstring(ev, "parent", parent)) {
    328 		prop_object_release(ev);
    329 		return;
    330 	}
    331 
    332 	devmon_insert(what, ev);
    333 #endif
    334 }
    335 
    336 /*
    337  * Add a cfdriver to the system.
    338  */
    339 int
    340 config_cfdriver_attach(struct cfdriver *cd)
    341 {
    342 	struct cfdriver *lcd;
    343 
    344 	/* Make sure this driver isn't already in the system. */
    345 	LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
    346 		if (STREQ(lcd->cd_name, cd->cd_name))
    347 			return EEXIST;
    348 	}
    349 
    350 	LIST_INIT(&cd->cd_attach);
    351 	LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
    352 
    353 	return 0;
    354 }
    355 
    356 /*
    357  * Remove a cfdriver from the system.
    358  */
    359 int
    360 config_cfdriver_detach(struct cfdriver *cd)
    361 {
    362 	int i;
    363 
    364 	/* Make sure there are no active instances. */
    365 	for (i = 0; i < cd->cd_ndevs; i++) {
    366 		if (cd->cd_devs[i] != NULL)
    367 			return EBUSY;
    368 	}
    369 
    370 	/* ...and no attachments loaded. */
    371 	if (LIST_EMPTY(&cd->cd_attach) == 0)
    372 		return EBUSY;
    373 
    374 	LIST_REMOVE(cd, cd_list);
    375 
    376 	KASSERT(cd->cd_devs == NULL);
    377 
    378 	return 0;
    379 }
    380 
    381 /*
    382  * Look up a cfdriver by name.
    383  */
    384 struct cfdriver *
    385 config_cfdriver_lookup(const char *name)
    386 {
    387 	struct cfdriver *cd;
    388 
    389 	LIST_FOREACH(cd, &allcfdrivers, cd_list) {
    390 		if (STREQ(cd->cd_name, name))
    391 			return cd;
    392 	}
    393 
    394 	return NULL;
    395 }
    396 
    397 /*
    398  * Add a cfattach to the specified driver.
    399  */
    400 int
    401 config_cfattach_attach(const char *driver, struct cfattach *ca)
    402 {
    403 	struct cfattach *lca;
    404 	struct cfdriver *cd;
    405 
    406 	cd = config_cfdriver_lookup(driver);
    407 	if (cd == NULL)
    408 		return ESRCH;
    409 
    410 	/* Make sure this attachment isn't already on this driver. */
    411 	LIST_FOREACH(lca, &cd->cd_attach, ca_list) {
    412 		if (STREQ(lca->ca_name, ca->ca_name))
    413 			return EEXIST;
    414 	}
    415 
    416 	LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list);
    417 
    418 	return 0;
    419 }
    420 
    421 /*
    422  * Remove a cfattach from the specified driver.
    423  */
    424 int
    425 config_cfattach_detach(const char *driver, struct cfattach *ca)
    426 {
    427 	struct cfdriver *cd;
    428 	device_t dev;
    429 	int i;
    430 
    431 	cd = config_cfdriver_lookup(driver);
    432 	if (cd == NULL)
    433 		return ESRCH;
    434 
    435 	/* Make sure there are no active instances. */
    436 	for (i = 0; i < cd->cd_ndevs; i++) {
    437 		if ((dev = cd->cd_devs[i]) == NULL)
    438 			continue;
    439 		if (dev->dv_cfattach == ca)
    440 			return EBUSY;
    441 	}
    442 
    443 	LIST_REMOVE(ca, ca_list);
    444 
    445 	return 0;
    446 }
    447 
    448 /*
    449  * Look up a cfattach by name.
    450  */
    451 static struct cfattach *
    452 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname)
    453 {
    454 	struct cfattach *ca;
    455 
    456 	LIST_FOREACH(ca, &cd->cd_attach, ca_list) {
    457 		if (STREQ(ca->ca_name, atname))
    458 			return ca;
    459 	}
    460 
    461 	return NULL;
    462 }
    463 
    464 /*
    465  * Look up a cfattach by driver/attachment name.
    466  */
    467 struct cfattach *
    468 config_cfattach_lookup(const char *name, const char *atname)
    469 {
    470 	struct cfdriver *cd;
    471 
    472 	cd = config_cfdriver_lookup(name);
    473 	if (cd == NULL)
    474 		return NULL;
    475 
    476 	return config_cfattach_lookup_cd(cd, atname);
    477 }
    478 
    479 /*
    480  * Apply the matching function and choose the best.  This is used
    481  * a few times and we want to keep the code small.
    482  */
    483 static void
    484 mapply(struct matchinfo *m, cfdata_t cf)
    485 {
    486 	int pri;
    487 
    488 	if (m->fn != NULL) {
    489 		pri = (*m->fn)(m->parent, cf, m->locs, m->aux);
    490 	} else {
    491 		pri = config_match(m->parent, cf, m->aux);
    492 	}
    493 	if (pri > m->pri) {
    494 		m->match = cf;
    495 		m->pri = pri;
    496 	}
    497 }
    498 
    499 int
    500 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    501 {
    502 	const struct cfiattrdata *ci;
    503 	const struct cflocdesc *cl;
    504 	int nlocs, i;
    505 
    506 	ci = cfiattr_lookup(cf->cf_pspec->cfp_iattr, parent->dv_cfdriver);
    507 	KASSERT(ci);
    508 	nlocs = ci->ci_loclen;
    509 	KASSERT(!nlocs || locs);
    510 	for (i = 0; i < nlocs; i++) {
    511 		cl = &ci->ci_locdesc[i];
    512 		/* !cld_defaultstr means no default value */
    513 		if ((!(cl->cld_defaultstr)
    514 		     || (cf->cf_loc[i] != cl->cld_default))
    515 		    && cf->cf_loc[i] != locs[i])
    516 			return 0;
    517 	}
    518 
    519 	return config_match(parent, cf, aux);
    520 }
    521 
    522 /*
    523  * Helper function: check whether the driver supports the interface attribute
    524  * and return its descriptor structure.
    525  */
    526 static const struct cfiattrdata *
    527 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia)
    528 {
    529 	const struct cfiattrdata * const *cpp;
    530 
    531 	if (cd->cd_attrs == NULL)
    532 		return 0;
    533 
    534 	for (cpp = cd->cd_attrs; *cpp; cpp++) {
    535 		if (STREQ((*cpp)->ci_name, ia)) {
    536 			/* Match. */
    537 			return *cpp;
    538 		}
    539 	}
    540 	return 0;
    541 }
    542 
    543 /*
    544  * Lookup an interface attribute description by name.
    545  * If the driver is given, consider only its supported attributes.
    546  */
    547 const struct cfiattrdata *
    548 cfiattr_lookup(const char *name, const struct cfdriver *cd)
    549 {
    550 	const struct cfdriver *d;
    551 	const struct cfiattrdata *ia;
    552 
    553 	if (cd)
    554 		return cfdriver_get_iattr(cd, name);
    555 
    556 	LIST_FOREACH(d, &allcfdrivers, cd_list) {
    557 		ia = cfdriver_get_iattr(d, name);
    558 		if (ia)
    559 			return ia;
    560 	}
    561 	return 0;
    562 }
    563 
    564 /*
    565  * Determine if `parent' is a potential parent for a device spec based
    566  * on `cfp'.
    567  */
    568 static int
    569 cfparent_match(const device_t parent, const struct cfparent *cfp)
    570 {
    571 	struct cfdriver *pcd;
    572 
    573 	/* We don't match root nodes here. */
    574 	if (cfp == NULL)
    575 		return 0;
    576 
    577 	pcd = parent->dv_cfdriver;
    578 	KASSERT(pcd != NULL);
    579 
    580 	/*
    581 	 * First, ensure this parent has the correct interface
    582 	 * attribute.
    583 	 */
    584 	if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr))
    585 		return 0;
    586 
    587 	/*
    588 	 * If no specific parent device instance was specified (i.e.
    589 	 * we're attaching to the attribute only), we're done!
    590 	 */
    591 	if (cfp->cfp_parent == NULL)
    592 		return 1;
    593 
    594 	/*
    595 	 * Check the parent device's name.
    596 	 */
    597 	if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
    598 		return 0;	/* not the same parent */
    599 
    600 	/*
    601 	 * Make sure the unit number matches.
    602 	 */
    603 	if (cfp->cfp_unit == DVUNIT_ANY ||	/* wildcard */
    604 	    cfp->cfp_unit == parent->dv_unit)
    605 		return 1;
    606 
    607 	/* Unit numbers don't match. */
    608 	return 0;
    609 }
    610 
    611 /*
    612  * Helper for config_cfdata_attach(): check all devices whether it could be
    613  * parent any attachment in the config data table passed, and rescan.
    614  */
    615 static void
    616 rescan_with_cfdata(const struct cfdata *cf)
    617 {
    618 	device_t d;
    619 	const struct cfdata *cf1;
    620 	deviter_t di;
    621 
    622 
    623 	/*
    624 	 * "alldevs" is likely longer than a modules's cfdata, so make it
    625 	 * the outer loop.
    626 	 */
    627 	for (d = deviter_first(&di, 0); d != NULL; d = deviter_next(&di)) {
    628 
    629 		if (!(d->dv_cfattach->ca_rescan))
    630 			continue;
    631 
    632 		for (cf1 = cf; cf1->cf_name; cf1++) {
    633 
    634 			if (!cfparent_match(d, cf1->cf_pspec))
    635 				continue;
    636 
    637 			(*d->dv_cfattach->ca_rescan)(d,
    638 				cf1->cf_pspec->cfp_iattr, cf1->cf_loc);
    639 		}
    640 	}
    641 	deviter_release(&di);
    642 }
    643 
    644 /*
    645  * Attach a supplemental config data table and rescan potential
    646  * parent devices if required.
    647  */
    648 int
    649 config_cfdata_attach(cfdata_t cf, int scannow)
    650 {
    651 	struct cftable *ct;
    652 
    653 	ct = kmem_alloc(sizeof(*ct), KM_SLEEP);
    654 	ct->ct_cfdata = cf;
    655 	TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
    656 
    657 	if (scannow)
    658 		rescan_with_cfdata(cf);
    659 
    660 	return 0;
    661 }
    662 
    663 /*
    664  * Helper for config_cfdata_detach: check whether a device is
    665  * found through any attachment in the config data table.
    666  */
    667 static int
    668 dev_in_cfdata(const struct device *d, const struct cfdata *cf)
    669 {
    670 	const struct cfdata *cf1;
    671 
    672 	for (cf1 = cf; cf1->cf_name; cf1++)
    673 		if (d->dv_cfdata == cf1)
    674 			return 1;
    675 
    676 	return 0;
    677 }
    678 
    679 /*
    680  * Detach a supplemental config data table. Detach all devices found
    681  * through that table (and thus keeping references to it) before.
    682  */
    683 int
    684 config_cfdata_detach(cfdata_t cf)
    685 {
    686 	device_t d;
    687 	int error = 0;
    688 	struct cftable *ct;
    689 	deviter_t di;
    690 
    691 	for (d = deviter_first(&di, DEVITER_F_RW); d != NULL;
    692 	     d = deviter_next(&di)) {
    693 		if (!dev_in_cfdata(d, cf))
    694 			continue;
    695 		if ((error = config_detach(d, 0)) != 0)
    696 			break;
    697 	}
    698 	deviter_release(&di);
    699 	if (error) {
    700 		aprint_error_dev(d, "unable to detach instance\n");
    701 		return error;
    702 	}
    703 
    704 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
    705 		if (ct->ct_cfdata == cf) {
    706 			TAILQ_REMOVE(&allcftables, ct, ct_list);
    707 			kmem_free(ct, sizeof(*ct));
    708 			return 0;
    709 		}
    710 	}
    711 
    712 	/* not found -- shouldn't happen */
    713 	return EINVAL;
    714 }
    715 
    716 /*
    717  * Invoke the "match" routine for a cfdata entry on behalf of
    718  * an external caller, usually a "submatch" routine.
    719  */
    720 int
    721 config_match(device_t parent, cfdata_t cf, void *aux)
    722 {
    723 	struct cfattach *ca;
    724 
    725 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
    726 	if (ca == NULL) {
    727 		/* No attachment for this entry, oh well. */
    728 		return 0;
    729 	}
    730 
    731 	return (*ca->ca_match)(parent, cf, aux);
    732 }
    733 
    734 /*
    735  * Iterate over all potential children of some device, calling the given
    736  * function (default being the child's match function) for each one.
    737  * Nonzero returns are matches; the highest value returned is considered
    738  * the best match.  Return the `found child' if we got a match, or NULL
    739  * otherwise.  The `aux' pointer is simply passed on through.
    740  *
    741  * Note that this function is designed so that it can be used to apply
    742  * an arbitrary function to all potential children (its return value
    743  * can be ignored).
    744  */
    745 cfdata_t
    746 config_search_loc(cfsubmatch_t fn, device_t parent,
    747 		  const char *ifattr, const int *locs, void *aux)
    748 {
    749 	struct cftable *ct;
    750 	cfdata_t cf;
    751 	struct matchinfo m;
    752 
    753 	KASSERT(config_initialized);
    754 	KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr));
    755 
    756 	m.fn = fn;
    757 	m.parent = parent;
    758 	m.locs = locs;
    759 	m.aux = aux;
    760 	m.match = NULL;
    761 	m.pri = 0;
    762 
    763 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
    764 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
    765 
    766 			/* We don't match root nodes here. */
    767 			if (!cf->cf_pspec)
    768 				continue;
    769 
    770 			/*
    771 			 * Skip cf if no longer eligible, otherwise scan
    772 			 * through parents for one matching `parent', and
    773 			 * try match function.
    774 			 */
    775 			if (cf->cf_fstate == FSTATE_FOUND)
    776 				continue;
    777 			if (cf->cf_fstate == FSTATE_DNOTFOUND ||
    778 			    cf->cf_fstate == FSTATE_DSTAR)
    779 				continue;
    780 
    781 			/*
    782 			 * If an interface attribute was specified,
    783 			 * consider only children which attach to
    784 			 * that attribute.
    785 			 */
    786 			if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr))
    787 				continue;
    788 
    789 			if (cfparent_match(parent, cf->cf_pspec))
    790 				mapply(&m, cf);
    791 		}
    792 	}
    793 	return m.match;
    794 }
    795 
    796 cfdata_t
    797 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr,
    798     void *aux)
    799 {
    800 
    801 	return config_search_loc(fn, parent, ifattr, NULL, aux);
    802 }
    803 
    804 /*
    805  * Find the given root device.
    806  * This is much like config_search, but there is no parent.
    807  * Don't bother with multiple cfdata tables; the root node
    808  * must always be in the initial table.
    809  */
    810 cfdata_t
    811 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux)
    812 {
    813 	cfdata_t cf;
    814 	const short *p;
    815 	struct matchinfo m;
    816 
    817 	m.fn = fn;
    818 	m.parent = ROOT;
    819 	m.aux = aux;
    820 	m.match = NULL;
    821 	m.pri = 0;
    822 	m.locs = 0;
    823 	/*
    824 	 * Look at root entries for matching name.  We do not bother
    825 	 * with found-state here since only one root should ever be
    826 	 * searched (and it must be done first).
    827 	 */
    828 	for (p = cfroots; *p >= 0; p++) {
    829 		cf = &cfdata[*p];
    830 		if (strcmp(cf->cf_name, rootname) == 0)
    831 			mapply(&m, cf);
    832 	}
    833 	return m.match;
    834 }
    835 
    836 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
    837 
    838 /*
    839  * The given `aux' argument describes a device that has been found
    840  * on the given parent, but not necessarily configured.  Locate the
    841  * configuration data for that device (using the submatch function
    842  * provided, or using candidates' cd_match configuration driver
    843  * functions) and attach it, and return true.  If the device was
    844  * not configured, call the given `print' function and return 0.
    845  */
    846 device_t
    847 config_found_sm_loc(device_t parent,
    848 		const char *ifattr, const int *locs, void *aux,
    849 		cfprint_t print, cfsubmatch_t submatch)
    850 {
    851 	cfdata_t cf;
    852 
    853 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
    854 	if (splash_progress_state)
    855 		splash_progress_update(splash_progress_state);
    856 #endif
    857 
    858 	if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux)))
    859 		return(config_attach_loc(parent, cf, locs, aux, print));
    860 	if (print) {
    861 		if (config_do_twiddle && cold)
    862 			twiddle();
    863 		aprint_normal("%s", msgs[(*print)(aux, device_xname(parent))]);
    864 	}
    865 
    866 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
    867 	if (splash_progress_state)
    868 		splash_progress_update(splash_progress_state);
    869 #endif
    870 
    871 	return NULL;
    872 }
    873 
    874 device_t
    875 config_found_ia(device_t parent, const char *ifattr, void *aux,
    876     cfprint_t print)
    877 {
    878 
    879 	return config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL);
    880 }
    881 
    882 device_t
    883 config_found(device_t parent, void *aux, cfprint_t print)
    884 {
    885 
    886 	return config_found_sm_loc(parent, NULL, NULL, aux, print, NULL);
    887 }
    888 
    889 /*
    890  * As above, but for root devices.
    891  */
    892 device_t
    893 config_rootfound(const char *rootname, void *aux)
    894 {
    895 	cfdata_t cf;
    896 
    897 	if ((cf = config_rootsearch((cfsubmatch_t)NULL, rootname, aux)) != NULL)
    898 		return config_attach(ROOT, cf, aux, (cfprint_t)NULL);
    899 	aprint_error("root device %s not configured\n", rootname);
    900 	return NULL;
    901 }
    902 
    903 /* just like sprintf(buf, "%d") except that it works from the end */
    904 static char *
    905 number(char *ep, int n)
    906 {
    907 
    908 	*--ep = 0;
    909 	while (n >= 10) {
    910 		*--ep = (n % 10) + '0';
    911 		n /= 10;
    912 	}
    913 	*--ep = n + '0';
    914 	return ep;
    915 }
    916 
    917 /*
    918  * Expand the size of the cd_devs array if necessary.
    919  */
    920 static void
    921 config_makeroom(int n, struct cfdriver *cd)
    922 {
    923 	int old, new;
    924 	device_t *nsp;
    925 
    926 	if (n < cd->cd_ndevs)
    927 		return;
    928 
    929 	/*
    930 	 * Need to expand the array.
    931 	 */
    932 	old = cd->cd_ndevs;
    933 	if (old == 0)
    934 		new = 4;
    935 	else
    936 		new = old * 2;
    937 	while (new <= n)
    938 		new *= 2;
    939 	cd->cd_ndevs = new;
    940 	nsp = kmem_alloc(sizeof(device_t [new]), KM_SLEEP);
    941 	if (nsp == NULL)
    942 		panic("config_attach: %sing dev array",
    943 		    old != 0 ? "expand" : "creat");
    944 	memset(nsp + old, 0, sizeof(device_t [new - old]));
    945 	if (old != 0) {
    946 		memcpy(nsp, cd->cd_devs, sizeof(device_t [old]));
    947 		kmem_free(cd->cd_devs, sizeof(device_t [old]));
    948 	}
    949 	cd->cd_devs = nsp;
    950 }
    951 
    952 static void
    953 config_devlink(device_t dev)
    954 {
    955 	struct cfdriver *cd = dev->dv_cfdriver;
    956 
    957 	/* put this device in the devices array */
    958 	config_makeroom(dev->dv_unit, cd);
    959 	if (cd->cd_devs[dev->dv_unit])
    960 		panic("config_attach: duplicate %s", device_xname(dev));
    961 	cd->cd_devs[dev->dv_unit] = dev;
    962 
    963 	/* It is safe to add a device to the tail of the list while
    964 	 * readers are in the list, but not while a writer is in
    965 	 * the list.  Wait for any writer to complete.
    966 	 */
    967 	mutex_enter(&alldevs_mtx);
    968 	while (alldevs_nwrite != 0 && alldevs_writer != curlwp)
    969 		cv_wait(&alldevs_cv, &alldevs_mtx);
    970 	TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);	/* link up */
    971 	cv_signal(&alldevs_cv);
    972 	mutex_exit(&alldevs_mtx);
    973 }
    974 
    975 static void
    976 config_devunlink(device_t dev)
    977 {
    978 	struct cfdriver *cd = dev->dv_cfdriver;
    979 	int i;
    980 
    981 	/* Unlink from device list. */
    982 	TAILQ_REMOVE(&alldevs, dev, dv_list);
    983 
    984 	/* Remove from cfdriver's array. */
    985 	cd->cd_devs[dev->dv_unit] = NULL;
    986 
    987 	/*
    988 	 * If the device now has no units in use, deallocate its softc array.
    989 	 */
    990 	for (i = 0; i < cd->cd_ndevs; i++) {
    991 		if (cd->cd_devs[i] != NULL)
    992 			return;
    993 	}
    994 	/* nothing found; deallocate */
    995 	kmem_free(cd->cd_devs, sizeof(device_t [cd->cd_ndevs]));
    996 	cd->cd_devs = NULL;
    997 	cd->cd_ndevs = 0;
    998 }
    999 
   1000 static device_t
   1001 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs)
   1002 {
   1003 	struct cfdriver *cd;
   1004 	struct cfattach *ca;
   1005 	size_t lname, lunit;
   1006 	const char *xunit;
   1007 	int myunit;
   1008 	char num[10];
   1009 	device_t dev;
   1010 	void *dev_private;
   1011 	const struct cfiattrdata *ia;
   1012 	device_lock_t dvl;
   1013 
   1014 	cd = config_cfdriver_lookup(cf->cf_name);
   1015 	if (cd == NULL)
   1016 		return NULL;
   1017 
   1018 	ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
   1019 	if (ca == NULL)
   1020 		return NULL;
   1021 
   1022 	if ((ca->ca_flags & DVF_PRIV_ALLOC) == 0 &&
   1023 	    ca->ca_devsize < sizeof(struct device))
   1024 		panic("config_devalloc: %s", cf->cf_atname);
   1025 
   1026 #ifndef __BROKEN_CONFIG_UNIT_USAGE
   1027 	if (cf->cf_fstate == FSTATE_STAR) {
   1028 		for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
   1029 			if (cd->cd_devs[myunit] == NULL)
   1030 				break;
   1031 		/*
   1032 		 * myunit is now the unit of the first NULL device pointer,
   1033 		 * or max(cd->cd_ndevs,cf->cf_unit).
   1034 		 */
   1035 	} else {
   1036 		myunit = cf->cf_unit;
   1037 		if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL)
   1038 			return NULL;
   1039 	}
   1040 #else
   1041 	myunit = cf->cf_unit;
   1042 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
   1043 
   1044 	/* compute length of name and decimal expansion of unit number */
   1045 	lname = strlen(cd->cd_name);
   1046 	xunit = number(&num[sizeof(num)], myunit);
   1047 	lunit = &num[sizeof(num)] - xunit;
   1048 	if (lname + lunit > sizeof(dev->dv_xname))
   1049 		panic("config_devalloc: device name too long");
   1050 
   1051 	/* get memory for all device vars */
   1052 	KASSERT((ca->ca_flags & DVF_PRIV_ALLOC) || ca->ca_devsize >= sizeof(struct device));
   1053 	if (ca->ca_devsize > 0) {
   1054 		dev_private = kmem_zalloc(ca->ca_devsize, KM_SLEEP);
   1055 		if (dev_private == NULL)
   1056 			panic("config_devalloc: memory allocation for device softc failed");
   1057 	} else {
   1058 		KASSERT(ca->ca_flags & DVF_PRIV_ALLOC);
   1059 		dev_private = NULL;
   1060 	}
   1061 
   1062 	if ((ca->ca_flags & DVF_PRIV_ALLOC) != 0) {
   1063 		dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
   1064 	} else {
   1065 		dev = dev_private;
   1066 	}
   1067 	if (dev == NULL)
   1068 		panic("config_devalloc: memory allocation for device_t failed");
   1069 
   1070 	dvl = device_getlock(dev);
   1071 
   1072 	mutex_init(&dvl->dvl_mtx, MUTEX_DEFAULT, IPL_NONE);
   1073 	cv_init(&dvl->dvl_cv, "pmfsusp");
   1074 
   1075 	dev->dv_class = cd->cd_class;
   1076 	dev->dv_cfdata = cf;
   1077 	dev->dv_cfdriver = cd;
   1078 	dev->dv_cfattach = ca;
   1079 	dev->dv_unit = myunit;
   1080 	dev->dv_activity_count = 0;
   1081 	dev->dv_activity_handlers = NULL;
   1082 	dev->dv_private = dev_private;
   1083 	memcpy(dev->dv_xname, cd->cd_name, lname);
   1084 	memcpy(dev->dv_xname + lname, xunit, lunit);
   1085 	dev->dv_parent = parent;
   1086 	if (parent != NULL)
   1087 		dev->dv_depth = parent->dv_depth + 1;
   1088 	else
   1089 		dev->dv_depth = 0;
   1090 	dev->dv_flags = DVF_ACTIVE;	/* always initially active */
   1091 	dev->dv_flags |= ca->ca_flags;	/* inherit flags from class */
   1092 	if (locs) {
   1093 		KASSERT(parent); /* no locators at root */
   1094 		ia = cfiattr_lookup(cf->cf_pspec->cfp_iattr,
   1095 				    parent->dv_cfdriver);
   1096 		dev->dv_locators =
   1097 		    kmem_alloc(sizeof(int [ia->ci_loclen + 1]), KM_SLEEP);
   1098 		*dev->dv_locators++ = sizeof(int [ia->ci_loclen + 1]);
   1099 		memcpy(dev->dv_locators, locs, sizeof(int [ia->ci_loclen]));
   1100 	}
   1101 	dev->dv_properties = prop_dictionary_create();
   1102 	KASSERT(dev->dv_properties != NULL);
   1103 
   1104 	prop_dictionary_set_cstring_nocopy(dev->dv_properties,
   1105 	    "device-driver", dev->dv_cfdriver->cd_name);
   1106 	prop_dictionary_set_uint16(dev->dv_properties,
   1107 	    "device-unit", dev->dv_unit);
   1108 
   1109 	return dev;
   1110 }
   1111 
   1112 static void
   1113 config_devdealloc(device_t dev)
   1114 {
   1115 	device_lock_t dvl = device_getlock(dev);
   1116 	int priv = (dev->dv_flags & DVF_PRIV_ALLOC);
   1117 
   1118 	cv_destroy(&dvl->dvl_cv);
   1119 	mutex_destroy(&dvl->dvl_mtx);
   1120 
   1121 	KASSERT(dev->dv_properties != NULL);
   1122 	prop_object_release(dev->dv_properties);
   1123 
   1124 	if (dev->dv_activity_handlers)
   1125 		panic("config_devdealloc with registered handlers");
   1126 
   1127 	if (dev->dv_locators) {
   1128 		size_t amount = *--dev->dv_locators;
   1129 		kmem_free(dev->dv_locators, amount);
   1130 	}
   1131 
   1132 	if (dev->dv_cfattach->ca_devsize > 0)
   1133 		kmem_free(dev->dv_private, dev->dv_cfattach->ca_devsize);
   1134 	if (priv)
   1135 		kmem_free(dev, sizeof(*dev));
   1136 }
   1137 
   1138 /*
   1139  * Attach a found device.
   1140  */
   1141 device_t
   1142 config_attach_loc(device_t parent, cfdata_t cf,
   1143 	const int *locs, void *aux, cfprint_t print)
   1144 {
   1145 	device_t dev;
   1146 	struct cftable *ct;
   1147 	const char *drvname;
   1148 
   1149 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
   1150 	if (splash_progress_state)
   1151 		splash_progress_update(splash_progress_state);
   1152 #endif
   1153 
   1154 	dev = config_devalloc(parent, cf, locs);
   1155 	if (!dev)
   1156 		panic("config_attach: allocation of device softc failed");
   1157 
   1158 	/* XXX redundant - see below? */
   1159 	if (cf->cf_fstate != FSTATE_STAR) {
   1160 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
   1161 		cf->cf_fstate = FSTATE_FOUND;
   1162 	}
   1163 #ifdef __BROKEN_CONFIG_UNIT_USAGE
   1164 	  else
   1165 		cf->cf_unit++;
   1166 #endif
   1167 
   1168 	config_devlink(dev);
   1169 
   1170 	if (config_do_twiddle && cold)
   1171 		twiddle();
   1172 	else
   1173 		aprint_naive("Found ");
   1174 	/*
   1175 	 * We want the next two printfs for normal, verbose, and quiet,
   1176 	 * but not silent (in which case, we're twiddling, instead).
   1177 	 */
   1178 	if (parent == ROOT) {
   1179 		aprint_naive("%s (root)", device_xname(dev));
   1180 		aprint_normal("%s (root)", device_xname(dev));
   1181 	} else {
   1182 		aprint_naive("%s at %s", device_xname(dev), device_xname(parent));
   1183 		aprint_normal("%s at %s", device_xname(dev), device_xname(parent));
   1184 		if (print)
   1185 			(void) (*print)(aux, NULL);
   1186 	}
   1187 
   1188 	/*
   1189 	 * Before attaching, clobber any unfound devices that are
   1190 	 * otherwise identical.
   1191 	 * XXX code above is redundant?
   1192 	 */
   1193 	drvname = dev->dv_cfdriver->cd_name;
   1194 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
   1195 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
   1196 			if (STREQ(cf->cf_name, drvname) &&
   1197 			    cf->cf_unit == dev->dv_unit) {
   1198 				if (cf->cf_fstate == FSTATE_NOTFOUND)
   1199 					cf->cf_fstate = FSTATE_FOUND;
   1200 #ifdef __BROKEN_CONFIG_UNIT_USAGE
   1201 				/*
   1202 				 * Bump the unit number on all starred cfdata
   1203 				 * entries for this device.
   1204 				 */
   1205 				if (cf->cf_fstate == FSTATE_STAR)
   1206 					cf->cf_unit++;
   1207 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
   1208 			}
   1209 		}
   1210 	}
   1211 #ifdef __HAVE_DEVICE_REGISTER
   1212 	device_register(dev, aux);
   1213 #endif
   1214 
   1215 	/* Let userland know */
   1216 	devmon_report_device(dev, true);
   1217 
   1218 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
   1219 	if (splash_progress_state)
   1220 		splash_progress_update(splash_progress_state);
   1221 #endif
   1222 	(*dev->dv_cfattach->ca_attach)(parent, dev, aux);
   1223 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
   1224 	if (splash_progress_state)
   1225 		splash_progress_update(splash_progress_state);
   1226 #endif
   1227 
   1228 	if (!device_pmf_is_registered(dev))
   1229 		aprint_debug_dev(dev, "WARNING: power management not supported\n");
   1230 
   1231 	config_process_deferred(&deferred_config_queue, dev);
   1232 	return dev;
   1233 }
   1234 
   1235 device_t
   1236 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print)
   1237 {
   1238 
   1239 	return config_attach_loc(parent, cf, NULL, aux, print);
   1240 }
   1241 
   1242 /*
   1243  * As above, but for pseudo-devices.  Pseudo-devices attached in this
   1244  * way are silently inserted into the device tree, and their children
   1245  * attached.
   1246  *
   1247  * Note that because pseudo-devices are attached silently, any information
   1248  * the attach routine wishes to print should be prefixed with the device
   1249  * name by the attach routine.
   1250  */
   1251 device_t
   1252 config_attach_pseudo(cfdata_t cf)
   1253 {
   1254 	device_t dev;
   1255 
   1256 	dev = config_devalloc(ROOT, cf, NULL);
   1257 	if (!dev)
   1258 		return NULL;
   1259 
   1260 	/* XXX mark busy in cfdata */
   1261 
   1262 	if (cf->cf_fstate != FSTATE_STAR) {
   1263 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
   1264 		cf->cf_fstate = FSTATE_FOUND;
   1265 	}
   1266 
   1267 	config_devlink(dev);
   1268 
   1269 #if 0	/* XXXJRT not yet */
   1270 #ifdef __HAVE_DEVICE_REGISTER
   1271 	device_register(dev, NULL);	/* like a root node */
   1272 #endif
   1273 #endif
   1274 	(*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL);
   1275 	config_process_deferred(&deferred_config_queue, dev);
   1276 	return dev;
   1277 }
   1278 
   1279 /*
   1280  * Detach a device.  Optionally forced (e.g. because of hardware
   1281  * removal) and quiet.  Returns zero if successful, non-zero
   1282  * (an error code) otherwise.
   1283  *
   1284  * Note that this code wants to be run from a process context, so
   1285  * that the detach can sleep to allow processes which have a device
   1286  * open to run and unwind their stacks.
   1287  */
   1288 int
   1289 config_detach(device_t dev, int flags)
   1290 {
   1291 	struct cftable *ct;
   1292 	cfdata_t cf;
   1293 	const struct cfattach *ca;
   1294 	struct cfdriver *cd;
   1295 #ifdef DIAGNOSTIC
   1296 	device_t d;
   1297 #endif
   1298 	int rv = 0;
   1299 
   1300 #ifdef DIAGNOSTIC
   1301 	cf = dev->dv_cfdata;
   1302 	if (cf != NULL && cf->cf_fstate != FSTATE_FOUND &&
   1303 	    cf->cf_fstate != FSTATE_STAR)
   1304 		panic("config_detach: %s: bad device fstate %d",
   1305 		    device_xname(dev), cf ? cf->cf_fstate : -1);
   1306 #endif
   1307 	cd = dev->dv_cfdriver;
   1308 	KASSERT(cd != NULL);
   1309 
   1310 	ca = dev->dv_cfattach;
   1311 	KASSERT(ca != NULL);
   1312 
   1313 	KASSERT(curlwp != NULL);
   1314 	mutex_enter(&alldevs_mtx);
   1315 	if (alldevs_nwrite > 0 && alldevs_writer == NULL)
   1316 		;
   1317 	else while (alldevs_nread != 0 ||
   1318 	       (alldevs_nwrite != 0 && alldevs_writer != curlwp))
   1319 		cv_wait(&alldevs_cv, &alldevs_mtx);
   1320 	if (alldevs_nwrite++ == 0)
   1321 		alldevs_writer = curlwp;
   1322 	mutex_exit(&alldevs_mtx);
   1323 
   1324 	/*
   1325 	 * Ensure the device is deactivated.  If the device doesn't
   1326 	 * have an activation entry point, we allow DVF_ACTIVE to
   1327 	 * remain set.  Otherwise, if DVF_ACTIVE is still set, the
   1328 	 * device is busy, and the detach fails.
   1329 	 */
   1330 	if (!detachall &&
   1331 	    (flags & (DETACH_SHUTDOWN|DETACH_FORCE)) == DETACH_SHUTDOWN &&
   1332 	    (dev->dv_flags & DVF_DETACH_SHUTDOWN) == 0) {
   1333 		rv = EOPNOTSUPP;
   1334 	} else if ((rv = config_deactivate(dev)) == EOPNOTSUPP)
   1335 		rv = 0;	/* Do not treat EOPNOTSUPP as an error */
   1336 
   1337 	/*
   1338 	 * Try to detach the device.  If that's not possible, then
   1339 	 * we either panic() (for the forced but failed case), or
   1340 	 * return an error.
   1341 	 */
   1342 	if (rv == 0) {
   1343 		if (ca->ca_detach != NULL)
   1344 			rv = (*ca->ca_detach)(dev, flags);
   1345 		else
   1346 			rv = EOPNOTSUPP;
   1347 	}
   1348 	if (rv != 0) {
   1349 		if ((flags & DETACH_FORCE) == 0)
   1350 			goto out;
   1351 		else
   1352 			panic("config_detach: forced detach of %s failed (%d)",
   1353 			    device_xname(dev), rv);
   1354 	}
   1355 
   1356 	dev->dv_flags &= ~DVF_ACTIVE;
   1357 
   1358 	/*
   1359 	 * The device has now been successfully detached.
   1360 	 */
   1361 
   1362 	/* Let userland know */
   1363 	devmon_report_device(dev, false);
   1364 
   1365 #ifdef DIAGNOSTIC
   1366 	/*
   1367 	 * Sanity: If you're successfully detached, you should have no
   1368 	 * children.  (Note that because children must be attached
   1369 	 * after parents, we only need to search the latter part of
   1370 	 * the list.)
   1371 	 */
   1372 	for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
   1373 	    d = TAILQ_NEXT(d, dv_list)) {
   1374 		if (d->dv_parent == dev) {
   1375 			printf("config_detach: detached device %s"
   1376 			    " has children %s\n", device_xname(dev), device_xname(d));
   1377 			panic("config_detach");
   1378 		}
   1379 	}
   1380 #endif
   1381 
   1382 	/* notify the parent that the child is gone */
   1383 	if (dev->dv_parent) {
   1384 		device_t p = dev->dv_parent;
   1385 		if (p->dv_cfattach->ca_childdetached)
   1386 			(*p->dv_cfattach->ca_childdetached)(p, dev);
   1387 	}
   1388 
   1389 	/*
   1390 	 * Mark cfdata to show that the unit can be reused, if possible.
   1391 	 */
   1392 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
   1393 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
   1394 			if (STREQ(cf->cf_name, cd->cd_name)) {
   1395 				if (cf->cf_fstate == FSTATE_FOUND &&
   1396 				    cf->cf_unit == dev->dv_unit)
   1397 					cf->cf_fstate = FSTATE_NOTFOUND;
   1398 #ifdef __BROKEN_CONFIG_UNIT_USAGE
   1399 				/*
   1400 				 * Note that we can only re-use a starred
   1401 				 * unit number if the unit being detached
   1402 				 * had the last assigned unit number.
   1403 				 */
   1404 				if (cf->cf_fstate == FSTATE_STAR &&
   1405 				    cf->cf_unit == dev->dv_unit + 1)
   1406 					cf->cf_unit--;
   1407 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
   1408 			}
   1409 		}
   1410 	}
   1411 
   1412 	config_devunlink(dev);
   1413 
   1414 	if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
   1415 		aprint_normal_dev(dev, "detached\n");
   1416 
   1417 	config_devdealloc(dev);
   1418 
   1419 out:
   1420 	mutex_enter(&alldevs_mtx);
   1421 	KASSERT(alldevs_nwrite != 0);
   1422 	if (--alldevs_nwrite == 0)
   1423 		alldevs_writer = NULL;
   1424 	cv_signal(&alldevs_cv);
   1425 	mutex_exit(&alldevs_mtx);
   1426 	return rv;
   1427 }
   1428 
   1429 int
   1430 config_detach_children(device_t parent, int flags)
   1431 {
   1432 	device_t dv;
   1433 	deviter_t di;
   1434 	int error = 0;
   1435 
   1436 	for (dv = deviter_first(&di, DEVITER_F_RW); dv != NULL;
   1437 	     dv = deviter_next(&di)) {
   1438 		if (device_parent(dv) != parent)
   1439 			continue;
   1440 		if ((error = config_detach(dv, flags)) != 0)
   1441 			break;
   1442 	}
   1443 	deviter_release(&di);
   1444 	return error;
   1445 }
   1446 
   1447 device_t
   1448 shutdown_first(struct shutdown_state *s)
   1449 {
   1450 	if (!s->initialized) {
   1451 		deviter_init(&s->di, DEVITER_F_SHUTDOWN|DEVITER_F_LEAVES_FIRST);
   1452 		s->initialized = true;
   1453 	}
   1454 	return shutdown_next(s);
   1455 }
   1456 
   1457 device_t
   1458 shutdown_next(struct shutdown_state *s)
   1459 {
   1460 	device_t dv;
   1461 
   1462 	while ((dv = deviter_next(&s->di)) != NULL && !device_is_active(dv))
   1463 		;
   1464 
   1465 	if (dv == NULL)
   1466 		s->initialized = false;
   1467 
   1468 	return dv;
   1469 }
   1470 
   1471 bool
   1472 config_detach_all(int how)
   1473 {
   1474 	static struct shutdown_state s;
   1475 	device_t curdev;
   1476 	bool progress = false;
   1477 
   1478 	if ((how & RB_NOSYNC) != 0)
   1479 		return false;
   1480 
   1481 	for (curdev = shutdown_first(&s); curdev != NULL;
   1482 	     curdev = shutdown_next(&s)) {
   1483 		aprint_debug(" detaching %s, ", device_xname(curdev));
   1484 		if (config_detach(curdev, DETACH_SHUTDOWN) == 0) {
   1485 			progress = true;
   1486 			aprint_debug("success.");
   1487 		} else
   1488 			aprint_debug("failed.");
   1489 	}
   1490 	return progress;
   1491 }
   1492 
   1493 int
   1494 config_deactivate(device_t dev)
   1495 {
   1496 	const struct cfattach *ca = dev->dv_cfattach;
   1497 	int rv = 0, oflags = dev->dv_flags;
   1498 
   1499 	if (ca->ca_activate == NULL)
   1500 		return EOPNOTSUPP;
   1501 
   1502 	if (dev->dv_flags & DVF_ACTIVE) {
   1503 		dev->dv_flags &= ~DVF_ACTIVE;
   1504 		rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
   1505 		if (rv)
   1506 			dev->dv_flags = oflags;
   1507 	}
   1508 	return rv;
   1509 }
   1510 
   1511 /*
   1512  * Defer the configuration of the specified device until all
   1513  * of its parent's devices have been attached.
   1514  */
   1515 void
   1516 config_defer(device_t dev, void (*func)(device_t))
   1517 {
   1518 	struct deferred_config *dc;
   1519 
   1520 	if (dev->dv_parent == NULL)
   1521 		panic("config_defer: can't defer config of a root device");
   1522 
   1523 #ifdef DIAGNOSTIC
   1524 	for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
   1525 	     dc = TAILQ_NEXT(dc, dc_queue)) {
   1526 		if (dc->dc_dev == dev)
   1527 			panic("config_defer: deferred twice");
   1528 	}
   1529 #endif
   1530 
   1531 	dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
   1532 	if (dc == NULL)
   1533 		panic("config_defer: unable to allocate callback");
   1534 
   1535 	dc->dc_dev = dev;
   1536 	dc->dc_func = func;
   1537 	TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
   1538 	config_pending_incr();
   1539 }
   1540 
   1541 /*
   1542  * Defer some autoconfiguration for a device until after interrupts
   1543  * are enabled.
   1544  */
   1545 void
   1546 config_interrupts(device_t dev, void (*func)(device_t))
   1547 {
   1548 	struct deferred_config *dc;
   1549 
   1550 	/*
   1551 	 * If interrupts are enabled, callback now.
   1552 	 */
   1553 	if (cold == 0) {
   1554 		(*func)(dev);
   1555 		return;
   1556 	}
   1557 
   1558 #ifdef DIAGNOSTIC
   1559 	for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
   1560 	     dc = TAILQ_NEXT(dc, dc_queue)) {
   1561 		if (dc->dc_dev == dev)
   1562 			panic("config_interrupts: deferred twice");
   1563 	}
   1564 #endif
   1565 
   1566 	dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
   1567 	if (dc == NULL)
   1568 		panic("config_interrupts: unable to allocate callback");
   1569 
   1570 	dc->dc_dev = dev;
   1571 	dc->dc_func = func;
   1572 	TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
   1573 	config_pending_incr();
   1574 }
   1575 
   1576 /*
   1577  * Process a deferred configuration queue.
   1578  */
   1579 static void
   1580 config_process_deferred(struct deferred_config_head *queue,
   1581     device_t parent)
   1582 {
   1583 	struct deferred_config *dc, *ndc;
   1584 
   1585 	for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
   1586 		ndc = TAILQ_NEXT(dc, dc_queue);
   1587 		if (parent == NULL || dc->dc_dev->dv_parent == parent) {
   1588 			TAILQ_REMOVE(queue, dc, dc_queue);
   1589 			(*dc->dc_func)(dc->dc_dev);
   1590 			kmem_free(dc, sizeof(*dc));
   1591 			config_pending_decr();
   1592 		}
   1593 	}
   1594 }
   1595 
   1596 /*
   1597  * Manipulate the config_pending semaphore.
   1598  */
   1599 void
   1600 config_pending_incr(void)
   1601 {
   1602 
   1603 	mutex_enter(&config_misc_lock);
   1604 	config_pending++;
   1605 	mutex_exit(&config_misc_lock);
   1606 }
   1607 
   1608 void
   1609 config_pending_decr(void)
   1610 {
   1611 
   1612 #ifdef DIAGNOSTIC
   1613 	if (config_pending == 0)
   1614 		panic("config_pending_decr: config_pending == 0");
   1615 #endif
   1616 	mutex_enter(&config_misc_lock);
   1617 	config_pending--;
   1618 	if (config_pending == 0)
   1619 		cv_broadcast(&config_misc_cv);
   1620 	mutex_exit(&config_misc_lock);
   1621 }
   1622 
   1623 /*
   1624  * Register a "finalization" routine.  Finalization routines are
   1625  * called iteratively once all real devices have been found during
   1626  * autoconfiguration, for as long as any one finalizer has done
   1627  * any work.
   1628  */
   1629 int
   1630 config_finalize_register(device_t dev, int (*fn)(device_t))
   1631 {
   1632 	struct finalize_hook *f;
   1633 
   1634 	/*
   1635 	 * If finalization has already been done, invoke the
   1636 	 * callback function now.
   1637 	 */
   1638 	if (config_finalize_done) {
   1639 		while ((*fn)(dev) != 0)
   1640 			/* loop */ ;
   1641 	}
   1642 
   1643 	/* Ensure this isn't already on the list. */
   1644 	TAILQ_FOREACH(f, &config_finalize_list, f_list) {
   1645 		if (f->f_func == fn && f->f_dev == dev)
   1646 			return EEXIST;
   1647 	}
   1648 
   1649 	f = kmem_alloc(sizeof(*f), KM_SLEEP);
   1650 	f->f_func = fn;
   1651 	f->f_dev = dev;
   1652 	TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
   1653 
   1654 	return 0;
   1655 }
   1656 
   1657 void
   1658 config_finalize(void)
   1659 {
   1660 	struct finalize_hook *f;
   1661 	struct pdevinit *pdev;
   1662 	extern struct pdevinit pdevinit[];
   1663 	int errcnt, rv;
   1664 
   1665 	/*
   1666 	 * Now that device driver threads have been created, wait for
   1667 	 * them to finish any deferred autoconfiguration.
   1668 	 */
   1669 	mutex_enter(&config_misc_lock);
   1670 	while (config_pending != 0)
   1671 		cv_wait(&config_misc_cv, &config_misc_lock);
   1672 	mutex_exit(&config_misc_lock);
   1673 
   1674 	KERNEL_LOCK(1, NULL);
   1675 
   1676 	/* Attach pseudo-devices. */
   1677 	for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
   1678 		(*pdev->pdev_attach)(pdev->pdev_count);
   1679 
   1680 	/* Run the hooks until none of them does any work. */
   1681 	do {
   1682 		rv = 0;
   1683 		TAILQ_FOREACH(f, &config_finalize_list, f_list)
   1684 			rv |= (*f->f_func)(f->f_dev);
   1685 	} while (rv != 0);
   1686 
   1687 	config_finalize_done = 1;
   1688 
   1689 	/* Now free all the hooks. */
   1690 	while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
   1691 		TAILQ_REMOVE(&config_finalize_list, f, f_list);
   1692 		kmem_free(f, sizeof(*f));
   1693 	}
   1694 
   1695 	KERNEL_UNLOCK_ONE(NULL);
   1696 
   1697 	errcnt = aprint_get_error_count();
   1698 	if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
   1699 	    (boothowto & AB_VERBOSE) == 0) {
   1700 		mutex_enter(&config_misc_lock);
   1701 		if (config_do_twiddle) {
   1702 			config_do_twiddle = 0;
   1703 			printf_nolog(" done.\n");
   1704 		}
   1705 		mutex_exit(&config_misc_lock);
   1706 		if (errcnt != 0) {
   1707 			printf("WARNING: %d error%s while detecting hardware; "
   1708 			    "check system log.\n", errcnt,
   1709 			    errcnt == 1 ? "" : "s");
   1710 		}
   1711 	}
   1712 }
   1713 
   1714 void
   1715 config_twiddle_init()
   1716 {
   1717 
   1718 	if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
   1719 		config_do_twiddle = 1;
   1720 	}
   1721 	callout_setfunc(&config_twiddle_ch, config_twiddle_fn, NULL);
   1722 }
   1723 
   1724 void
   1725 config_twiddle_fn(void *cookie)
   1726 {
   1727 
   1728 	mutex_enter(&config_misc_lock);
   1729 	if (config_do_twiddle) {
   1730 		twiddle();
   1731 		callout_schedule(&config_twiddle_ch, mstohz(100));
   1732 	}
   1733 	mutex_exit(&config_misc_lock);
   1734 }
   1735 
   1736 /*
   1737  * device_lookup:
   1738  *
   1739  *	Look up a device instance for a given driver.
   1740  */
   1741 device_t
   1742 device_lookup(cfdriver_t cd, int unit)
   1743 {
   1744 
   1745 	if (unit < 0 || unit >= cd->cd_ndevs)
   1746 		return NULL;
   1747 
   1748 	return cd->cd_devs[unit];
   1749 }
   1750 
   1751 /*
   1752  * device_lookup:
   1753  *
   1754  *	Look up a device instance for a given driver.
   1755  */
   1756 void *
   1757 device_lookup_private(cfdriver_t cd, int unit)
   1758 {
   1759 	device_t dv;
   1760 
   1761 	if (unit < 0 || unit >= cd->cd_ndevs)
   1762 		return NULL;
   1763 
   1764 	if ((dv = cd->cd_devs[unit]) == NULL)
   1765 		return NULL;
   1766 
   1767 	return dv->dv_private;
   1768 }
   1769 
   1770 /*
   1771  * Accessor functions for the device_t type.
   1772  */
   1773 devclass_t
   1774 device_class(device_t dev)
   1775 {
   1776 
   1777 	return dev->dv_class;
   1778 }
   1779 
   1780 cfdata_t
   1781 device_cfdata(device_t dev)
   1782 {
   1783 
   1784 	return dev->dv_cfdata;
   1785 }
   1786 
   1787 cfdriver_t
   1788 device_cfdriver(device_t dev)
   1789 {
   1790 
   1791 	return dev->dv_cfdriver;
   1792 }
   1793 
   1794 cfattach_t
   1795 device_cfattach(device_t dev)
   1796 {
   1797 
   1798 	return dev->dv_cfattach;
   1799 }
   1800 
   1801 int
   1802 device_unit(device_t dev)
   1803 {
   1804 
   1805 	return dev->dv_unit;
   1806 }
   1807 
   1808 const char *
   1809 device_xname(device_t dev)
   1810 {
   1811 
   1812 	return dev->dv_xname;
   1813 }
   1814 
   1815 device_t
   1816 device_parent(device_t dev)
   1817 {
   1818 
   1819 	return dev->dv_parent;
   1820 }
   1821 
   1822 bool
   1823 device_activation(device_t dev, devact_level_t level)
   1824 {
   1825 	int active_flags;
   1826 
   1827 	active_flags = DVF_ACTIVE;
   1828 	switch (level) {
   1829 	case DEVACT_LEVEL_FULL:
   1830 		active_flags |= DVF_CLASS_SUSPENDED;
   1831 		/*FALLTHROUGH*/
   1832 	case DEVACT_LEVEL_DRIVER:
   1833 		active_flags |= DVF_DRIVER_SUSPENDED;
   1834 		/*FALLTHROUGH*/
   1835 	case DEVACT_LEVEL_BUS:
   1836 		active_flags |= DVF_BUS_SUSPENDED;
   1837 		break;
   1838 	}
   1839 
   1840 	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
   1841 }
   1842 
   1843 bool
   1844 device_is_active(device_t dev)
   1845 {
   1846 	int active_flags;
   1847 
   1848 	active_flags = DVF_ACTIVE;
   1849 	active_flags |= DVF_CLASS_SUSPENDED;
   1850 	active_flags |= DVF_DRIVER_SUSPENDED;
   1851 	active_flags |= DVF_BUS_SUSPENDED;
   1852 
   1853 	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
   1854 }
   1855 
   1856 bool
   1857 device_is_enabled(device_t dev)
   1858 {
   1859 	return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
   1860 }
   1861 
   1862 bool
   1863 device_has_power(device_t dev)
   1864 {
   1865 	int active_flags;
   1866 
   1867 	active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
   1868 
   1869 	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
   1870 }
   1871 
   1872 int
   1873 device_locator(device_t dev, u_int locnum)
   1874 {
   1875 
   1876 	KASSERT(dev->dv_locators != NULL);
   1877 	return dev->dv_locators[locnum];
   1878 }
   1879 
   1880 void *
   1881 device_private(device_t dev)
   1882 {
   1883 
   1884 	/*
   1885 	 * The reason why device_private(NULL) is allowed is to simplify the
   1886 	 * work of a lot of userspace request handlers (i.e., c/bdev
   1887 	 * handlers) which grab cfdriver_t->cd_units[n].
   1888 	 * It avoids having them test for it to be NULL and only then calling
   1889 	 * device_private.
   1890 	 */
   1891 	return dev == NULL ? NULL : dev->dv_private;
   1892 }
   1893 
   1894 prop_dictionary_t
   1895 device_properties(device_t dev)
   1896 {
   1897 
   1898 	return dev->dv_properties;
   1899 }
   1900 
   1901 /*
   1902  * device_is_a:
   1903  *
   1904  *	Returns true if the device is an instance of the specified
   1905  *	driver.
   1906  */
   1907 bool
   1908 device_is_a(device_t dev, const char *dname)
   1909 {
   1910 
   1911 	return strcmp(dev->dv_cfdriver->cd_name, dname) == 0;
   1912 }
   1913 
   1914 /*
   1915  * device_find_by_xname:
   1916  *
   1917  *	Returns the device of the given name or NULL if it doesn't exist.
   1918  */
   1919 device_t
   1920 device_find_by_xname(const char *name)
   1921 {
   1922 	device_t dv;
   1923 	deviter_t di;
   1924 
   1925 	for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) {
   1926 		if (strcmp(device_xname(dv), name) == 0)
   1927 			break;
   1928 	}
   1929 	deviter_release(&di);
   1930 
   1931 	return dv;
   1932 }
   1933 
   1934 /*
   1935  * device_find_by_driver_unit:
   1936  *
   1937  *	Returns the device of the given driver name and unit or
   1938  *	NULL if it doesn't exist.
   1939  */
   1940 device_t
   1941 device_find_by_driver_unit(const char *name, int unit)
   1942 {
   1943 	struct cfdriver *cd;
   1944 
   1945 	if ((cd = config_cfdriver_lookup(name)) == NULL)
   1946 		return NULL;
   1947 	return device_lookup(cd, unit);
   1948 }
   1949 
   1950 /*
   1951  * Power management related functions.
   1952  */
   1953 
   1954 bool
   1955 device_pmf_is_registered(device_t dev)
   1956 {
   1957 	return (dev->dv_flags & DVF_POWER_HANDLERS) != 0;
   1958 }
   1959 
   1960 bool
   1961 device_pmf_driver_suspend(device_t dev PMF_FN_ARGS)
   1962 {
   1963 	if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
   1964 		return true;
   1965 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
   1966 		return false;
   1967 	if (pmf_qual_depth(PMF_FN_CALL1) <= DEVACT_LEVEL_DRIVER &&
   1968 	    dev->dv_driver_suspend != NULL &&
   1969 	    !(*dev->dv_driver_suspend)(dev PMF_FN_CALL))
   1970 		return false;
   1971 
   1972 	dev->dv_flags |= DVF_DRIVER_SUSPENDED;
   1973 	return true;
   1974 }
   1975 
   1976 bool
   1977 device_pmf_driver_resume(device_t dev PMF_FN_ARGS)
   1978 {
   1979 	if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
   1980 		return true;
   1981 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
   1982 		return false;
   1983 	if (pmf_qual_depth(PMF_FN_CALL1) <= DEVACT_LEVEL_DRIVER &&
   1984 	    dev->dv_driver_resume != NULL &&
   1985 	    !(*dev->dv_driver_resume)(dev PMF_FN_CALL))
   1986 		return false;
   1987 
   1988 	dev->dv_flags &= ~DVF_DRIVER_SUSPENDED;
   1989 	return true;
   1990 }
   1991 
   1992 bool
   1993 device_pmf_driver_shutdown(device_t dev, int how)
   1994 {
   1995 
   1996 	if (*dev->dv_driver_shutdown != NULL &&
   1997 	    !(*dev->dv_driver_shutdown)(dev, how))
   1998 		return false;
   1999 	return true;
   2000 }
   2001 
   2002 bool
   2003 device_pmf_driver_register(device_t dev,
   2004     bool (*suspend)(device_t PMF_FN_PROTO),
   2005     bool (*resume)(device_t PMF_FN_PROTO),
   2006     bool (*shutdown)(device_t, int))
   2007 {
   2008 	dev->dv_driver_suspend = suspend;
   2009 	dev->dv_driver_resume = resume;
   2010 	dev->dv_driver_shutdown = shutdown;
   2011 	dev->dv_flags |= DVF_POWER_HANDLERS;
   2012 	return true;
   2013 }
   2014 
   2015 static const char *
   2016 curlwp_name(void)
   2017 {
   2018 	if (curlwp->l_name != NULL)
   2019 		return curlwp->l_name;
   2020 	else
   2021 		return curlwp->l_proc->p_comm;
   2022 }
   2023 
   2024 void
   2025 device_pmf_driver_deregister(device_t dev)
   2026 {
   2027 	device_lock_t dvl = device_getlock(dev);
   2028 
   2029 	dev->dv_driver_suspend = NULL;
   2030 	dev->dv_driver_resume = NULL;
   2031 
   2032 	mutex_enter(&dvl->dvl_mtx);
   2033 	dev->dv_flags &= ~DVF_POWER_HANDLERS;
   2034 	while (dvl->dvl_nlock > 0 || dvl->dvl_nwait > 0) {
   2035 		/* Wake a thread that waits for the lock.  That
   2036 		 * thread will fail to acquire the lock, and then
   2037 		 * it will wake the next thread that waits for the
   2038 		 * lock, or else it will wake us.
   2039 		 */
   2040 		cv_signal(&dvl->dvl_cv);
   2041 		pmflock_debug(dev, __func__, __LINE__);
   2042 		cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
   2043 		pmflock_debug(dev, __func__, __LINE__);
   2044 	}
   2045 	mutex_exit(&dvl->dvl_mtx);
   2046 }
   2047 
   2048 bool
   2049 device_pmf_driver_child_register(device_t dev)
   2050 {
   2051 	device_t parent = device_parent(dev);
   2052 
   2053 	if (parent == NULL || parent->dv_driver_child_register == NULL)
   2054 		return true;
   2055 	return (*parent->dv_driver_child_register)(dev);
   2056 }
   2057 
   2058 void
   2059 device_pmf_driver_set_child_register(device_t dev,
   2060     bool (*child_register)(device_t))
   2061 {
   2062 	dev->dv_driver_child_register = child_register;
   2063 }
   2064 
   2065 static void
   2066 pmflock_debug(device_t dev, const char *func, int line)
   2067 {
   2068 	device_lock_t dvl = device_getlock(dev);
   2069 
   2070 	aprint_debug_dev(dev, "%s.%d, %s dvl_nlock %d dvl_nwait %d dv_flags %x\n",
   2071 	    func, line, curlwp_name(), dvl->dvl_nlock, dvl->dvl_nwait,
   2072 	    dev->dv_flags);
   2073 }
   2074 
   2075 static bool
   2076 device_pmf_lock1(device_t dev)
   2077 {
   2078 	device_lock_t dvl = device_getlock(dev);
   2079 
   2080 	while (device_pmf_is_registered(dev) &&
   2081 	    dvl->dvl_nlock > 0 && dvl->dvl_holder != curlwp) {
   2082 		dvl->dvl_nwait++;
   2083 		pmflock_debug(dev, __func__, __LINE__);
   2084 		cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
   2085 		pmflock_debug(dev, __func__, __LINE__);
   2086 		dvl->dvl_nwait--;
   2087 	}
   2088 	if (!device_pmf_is_registered(dev)) {
   2089 		pmflock_debug(dev, __func__, __LINE__);
   2090 		/* We could not acquire the lock, but some other thread may
   2091 		 * wait for it, also.  Wake that thread.
   2092 		 */
   2093 		cv_signal(&dvl->dvl_cv);
   2094 		return false;
   2095 	}
   2096 	dvl->dvl_nlock++;
   2097 	dvl->dvl_holder = curlwp;
   2098 	pmflock_debug(dev, __func__, __LINE__);
   2099 	return true;
   2100 }
   2101 
   2102 bool
   2103 device_pmf_lock(device_t dev)
   2104 {
   2105 	bool rc;
   2106 	device_lock_t dvl = device_getlock(dev);
   2107 
   2108 	mutex_enter(&dvl->dvl_mtx);
   2109 	rc = device_pmf_lock1(dev);
   2110 	mutex_exit(&dvl->dvl_mtx);
   2111 
   2112 	return rc;
   2113 }
   2114 
   2115 void
   2116 device_pmf_unlock(device_t dev)
   2117 {
   2118 	device_lock_t dvl = device_getlock(dev);
   2119 
   2120 	KASSERT(dvl->dvl_nlock > 0);
   2121 	mutex_enter(&dvl->dvl_mtx);
   2122 	if (--dvl->dvl_nlock == 0)
   2123 		dvl->dvl_holder = NULL;
   2124 	cv_signal(&dvl->dvl_cv);
   2125 	pmflock_debug(dev, __func__, __LINE__);
   2126 	mutex_exit(&dvl->dvl_mtx);
   2127 }
   2128 
   2129 device_lock_t
   2130 device_getlock(device_t dev)
   2131 {
   2132 	return &dev->dv_lock;
   2133 }
   2134 
   2135 void *
   2136 device_pmf_bus_private(device_t dev)
   2137 {
   2138 	return dev->dv_bus_private;
   2139 }
   2140 
   2141 bool
   2142 device_pmf_bus_suspend(device_t dev PMF_FN_ARGS)
   2143 {
   2144 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
   2145 		return true;
   2146 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 ||
   2147 	    (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
   2148 		return false;
   2149 	if (pmf_qual_depth(PMF_FN_CALL1) <= DEVACT_LEVEL_BUS &&
   2150 	    dev->dv_bus_suspend != NULL &&
   2151 	    !(*dev->dv_bus_suspend)(dev PMF_FN_CALL))
   2152 		return false;
   2153 
   2154 	dev->dv_flags |= DVF_BUS_SUSPENDED;
   2155 	return true;
   2156 }
   2157 
   2158 bool
   2159 device_pmf_bus_resume(device_t dev PMF_FN_ARGS)
   2160 {
   2161 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0)
   2162 		return true;
   2163 	if (pmf_qual_depth(PMF_FN_CALL1) <= DEVACT_LEVEL_BUS &&
   2164 	    dev->dv_bus_resume != NULL &&
   2165 	    !(*dev->dv_bus_resume)(dev PMF_FN_CALL))
   2166 		return false;
   2167 
   2168 	dev->dv_flags &= ~DVF_BUS_SUSPENDED;
   2169 	return true;
   2170 }
   2171 
   2172 bool
   2173 device_pmf_bus_shutdown(device_t dev, int how)
   2174 {
   2175 
   2176 	if (*dev->dv_bus_shutdown != NULL &&
   2177 	    !(*dev->dv_bus_shutdown)(dev, how))
   2178 		return false;
   2179 	return true;
   2180 }
   2181 
   2182 void
   2183 device_pmf_bus_register(device_t dev, void *priv,
   2184     bool (*suspend)(device_t PMF_FN_PROTO),
   2185     bool (*resume)(device_t PMF_FN_PROTO),
   2186     bool (*shutdown)(device_t, int), void (*deregister)(device_t))
   2187 {
   2188 	dev->dv_bus_private = priv;
   2189 	dev->dv_bus_resume = resume;
   2190 	dev->dv_bus_suspend = suspend;
   2191 	dev->dv_bus_shutdown = shutdown;
   2192 	dev->dv_bus_deregister = deregister;
   2193 }
   2194 
   2195 void
   2196 device_pmf_bus_deregister(device_t dev)
   2197 {
   2198 	if (dev->dv_bus_deregister == NULL)
   2199 		return;
   2200 	(*dev->dv_bus_deregister)(dev);
   2201 	dev->dv_bus_private = NULL;
   2202 	dev->dv_bus_suspend = NULL;
   2203 	dev->dv_bus_resume = NULL;
   2204 	dev->dv_bus_deregister = NULL;
   2205 }
   2206 
   2207 void *
   2208 device_pmf_class_private(device_t dev)
   2209 {
   2210 	return dev->dv_class_private;
   2211 }
   2212 
   2213 bool
   2214 device_pmf_class_suspend(device_t dev PMF_FN_ARGS)
   2215 {
   2216 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0)
   2217 		return true;
   2218 	if (pmf_qual_depth(PMF_FN_CALL1) <= DEVACT_LEVEL_CLASS &&
   2219 	    dev->dv_class_suspend != NULL &&
   2220 	    !(*dev->dv_class_suspend)(dev PMF_FN_CALL))
   2221 		return false;
   2222 
   2223 	dev->dv_flags |= DVF_CLASS_SUSPENDED;
   2224 	return true;
   2225 }
   2226 
   2227 bool
   2228 device_pmf_class_resume(device_t dev PMF_FN_ARGS)
   2229 {
   2230 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
   2231 		return true;
   2232 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 ||
   2233 	    (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
   2234 		return false;
   2235 	if (pmf_qual_depth(PMF_FN_CALL1) <= DEVACT_LEVEL_CLASS &&
   2236 	    dev->dv_class_resume != NULL &&
   2237 	    !(*dev->dv_class_resume)(dev PMF_FN_CALL))
   2238 		return false;
   2239 
   2240 	dev->dv_flags &= ~DVF_CLASS_SUSPENDED;
   2241 	return true;
   2242 }
   2243 
   2244 void
   2245 device_pmf_class_register(device_t dev, void *priv,
   2246     bool (*suspend)(device_t PMF_FN_PROTO),
   2247     bool (*resume)(device_t PMF_FN_PROTO),
   2248     void (*deregister)(device_t))
   2249 {
   2250 	dev->dv_class_private = priv;
   2251 	dev->dv_class_suspend = suspend;
   2252 	dev->dv_class_resume = resume;
   2253 	dev->dv_class_deregister = deregister;
   2254 }
   2255 
   2256 void
   2257 device_pmf_class_deregister(device_t dev)
   2258 {
   2259 	if (dev->dv_class_deregister == NULL)
   2260 		return;
   2261 	(*dev->dv_class_deregister)(dev);
   2262 	dev->dv_class_private = NULL;
   2263 	dev->dv_class_suspend = NULL;
   2264 	dev->dv_class_resume = NULL;
   2265 	dev->dv_class_deregister = NULL;
   2266 }
   2267 
   2268 bool
   2269 device_active(device_t dev, devactive_t type)
   2270 {
   2271 	size_t i;
   2272 
   2273 	if (dev->dv_activity_count == 0)
   2274 		return false;
   2275 
   2276 	for (i = 0; i < dev->dv_activity_count; ++i) {
   2277 		if (dev->dv_activity_handlers[i] == NULL)
   2278 			break;
   2279 		(*dev->dv_activity_handlers[i])(dev, type);
   2280 	}
   2281 
   2282 	return true;
   2283 }
   2284 
   2285 bool
   2286 device_active_register(device_t dev, void (*handler)(device_t, devactive_t))
   2287 {
   2288 	void (**new_handlers)(device_t, devactive_t);
   2289 	void (**old_handlers)(device_t, devactive_t);
   2290 	size_t i, old_size, new_size;
   2291 	int s;
   2292 
   2293 	old_handlers = dev->dv_activity_handlers;
   2294 	old_size = dev->dv_activity_count;
   2295 
   2296 	for (i = 0; i < old_size; ++i) {
   2297 		KASSERT(old_handlers[i] != handler);
   2298 		if (old_handlers[i] == NULL) {
   2299 			old_handlers[i] = handler;
   2300 			return true;
   2301 		}
   2302 	}
   2303 
   2304 	new_size = old_size + 4;
   2305 	new_handlers = kmem_alloc(sizeof(void *[new_size]), KM_SLEEP);
   2306 
   2307 	memcpy(new_handlers, old_handlers, sizeof(void *[old_size]));
   2308 	new_handlers[old_size] = handler;
   2309 	memset(new_handlers + old_size + 1, 0,
   2310 	    sizeof(int [new_size - (old_size+1)]));
   2311 
   2312 	s = splhigh();
   2313 	dev->dv_activity_count = new_size;
   2314 	dev->dv_activity_handlers = new_handlers;
   2315 	splx(s);
   2316 
   2317 	if (old_handlers != NULL)
   2318 		kmem_free(old_handlers, sizeof(void * [old_size]));
   2319 
   2320 	return true;
   2321 }
   2322 
   2323 void
   2324 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t))
   2325 {
   2326 	void (**old_handlers)(device_t, devactive_t);
   2327 	size_t i, old_size;
   2328 	int s;
   2329 
   2330 	old_handlers = dev->dv_activity_handlers;
   2331 	old_size = dev->dv_activity_count;
   2332 
   2333 	for (i = 0; i < old_size; ++i) {
   2334 		if (old_handlers[i] == handler)
   2335 			break;
   2336 		if (old_handlers[i] == NULL)
   2337 			return; /* XXX panic? */
   2338 	}
   2339 
   2340 	if (i == old_size)
   2341 		return; /* XXX panic? */
   2342 
   2343 	for (; i < old_size - 1; ++i) {
   2344 		if ((old_handlers[i] = old_handlers[i + 1]) != NULL)
   2345 			continue;
   2346 
   2347 		if (i == 0) {
   2348 			s = splhigh();
   2349 			dev->dv_activity_count = 0;
   2350 			dev->dv_activity_handlers = NULL;
   2351 			splx(s);
   2352 			kmem_free(old_handlers, sizeof(void *[old_size]));
   2353 		}
   2354 		return;
   2355 	}
   2356 	old_handlers[i] = NULL;
   2357 }
   2358 
   2359 /*
   2360  * Device Iteration
   2361  *
   2362  * deviter_t: a device iterator.  Holds state for a "walk" visiting
   2363  *     each device_t's in the device tree.
   2364  *
   2365  * deviter_init(di, flags): initialize the device iterator `di'
   2366  *     to "walk" the device tree.  deviter_next(di) will return
   2367  *     the first device_t in the device tree, or NULL if there are
   2368  *     no devices.
   2369  *
   2370  *     `flags' is one or more of DEVITER_F_RW, indicating that the
   2371  *     caller intends to modify the device tree by calling
   2372  *     config_detach(9) on devices in the order that the iterator
   2373  *     returns them; DEVITER_F_ROOT_FIRST, asking for the devices
   2374  *     nearest the "root" of the device tree to be returned, first;
   2375  *     DEVITER_F_LEAVES_FIRST, asking for the devices furthest from
   2376  *     the root of the device tree, first; and DEVITER_F_SHUTDOWN,
   2377  *     indicating both that deviter_init() should not respect any
   2378  *     locks on the device tree, and that deviter_next(di) may run
   2379  *     in more than one LWP before the walk has finished.
   2380  *
   2381  *     Only one DEVITER_F_RW iterator may be in the device tree at
   2382  *     once.
   2383  *
   2384  *     DEVITER_F_SHUTDOWN implies DEVITER_F_RW.
   2385  *
   2386  *     Results are undefined if the flags DEVITER_F_ROOT_FIRST and
   2387  *     DEVITER_F_LEAVES_FIRST are used in combination.
   2388  *
   2389  * deviter_first(di, flags): initialize the device iterator `di'
   2390  *     and return the first device_t in the device tree, or NULL
   2391  *     if there are no devices.  The statement
   2392  *
   2393  *         dv = deviter_first(di);
   2394  *
   2395  *     is shorthand for
   2396  *
   2397  *         deviter_init(di);
   2398  *         dv = deviter_next(di);
   2399  *
   2400  * deviter_next(di): return the next device_t in the device tree,
   2401  *     or NULL if there are no more devices.  deviter_next(di)
   2402  *     is undefined if `di' was not initialized with deviter_init() or
   2403  *     deviter_first().
   2404  *
   2405  * deviter_release(di): stops iteration (subsequent calls to
   2406  *     deviter_next() will return NULL), releases any locks and
   2407  *     resources held by the device iterator.
   2408  *
   2409  * Device iteration does not return device_t's in any particular
   2410  * order.  An iterator will never return the same device_t twice.
   2411  * Device iteration is guaranteed to complete---i.e., if deviter_next(di)
   2412  * is called repeatedly on the same `di', it will eventually return
   2413  * NULL.  It is ok to attach/detach devices during device iteration.
   2414  */
   2415 void
   2416 deviter_init(deviter_t *di, deviter_flags_t flags)
   2417 {
   2418 	device_t dv;
   2419 	bool rw;
   2420 
   2421 	mutex_enter(&alldevs_mtx);
   2422 	if ((flags & DEVITER_F_SHUTDOWN) != 0) {
   2423 		flags |= DEVITER_F_RW;
   2424 		alldevs_nwrite++;
   2425 		alldevs_writer = NULL;
   2426 		alldevs_nread = 0;
   2427 	} else {
   2428 		rw = (flags & DEVITER_F_RW) != 0;
   2429 
   2430 		if (alldevs_nwrite > 0 && alldevs_writer == NULL)
   2431 			;
   2432 		else while ((alldevs_nwrite != 0 && alldevs_writer != curlwp) ||
   2433 		       (rw && alldevs_nread != 0))
   2434 			cv_wait(&alldevs_cv, &alldevs_mtx);
   2435 
   2436 		if (rw) {
   2437 			if (alldevs_nwrite++ == 0)
   2438 				alldevs_writer = curlwp;
   2439 		} else
   2440 			alldevs_nread++;
   2441 	}
   2442 	mutex_exit(&alldevs_mtx);
   2443 
   2444 	memset(di, 0, sizeof(*di));
   2445 
   2446 	di->di_flags = flags;
   2447 
   2448 	switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
   2449 	case DEVITER_F_LEAVES_FIRST:
   2450 		TAILQ_FOREACH(dv, &alldevs, dv_list)
   2451 			di->di_curdepth = MAX(di->di_curdepth, dv->dv_depth);
   2452 		break;
   2453 	case DEVITER_F_ROOT_FIRST:
   2454 		TAILQ_FOREACH(dv, &alldevs, dv_list)
   2455 			di->di_maxdepth = MAX(di->di_maxdepth, dv->dv_depth);
   2456 		break;
   2457 	default:
   2458 		break;
   2459 	}
   2460 
   2461 	deviter_reinit(di);
   2462 }
   2463 
   2464 static void
   2465 deviter_reinit(deviter_t *di)
   2466 {
   2467 	if ((di->di_flags & DEVITER_F_RW) != 0)
   2468 		di->di_prev = TAILQ_LAST(&alldevs, devicelist);
   2469 	else
   2470 		di->di_prev = TAILQ_FIRST(&alldevs);
   2471 }
   2472 
   2473 device_t
   2474 deviter_first(deviter_t *di, deviter_flags_t flags)
   2475 {
   2476 	deviter_init(di, flags);
   2477 	return deviter_next(di);
   2478 }
   2479 
   2480 static device_t
   2481 deviter_next1(deviter_t *di)
   2482 {
   2483 	device_t dv;
   2484 
   2485 	dv = di->di_prev;
   2486 
   2487 	if (dv == NULL)
   2488 		;
   2489 	else if ((di->di_flags & DEVITER_F_RW) != 0)
   2490 		di->di_prev = TAILQ_PREV(dv, devicelist, dv_list);
   2491 	else
   2492 		di->di_prev = TAILQ_NEXT(dv, dv_list);
   2493 
   2494 	return dv;
   2495 }
   2496 
   2497 device_t
   2498 deviter_next(deviter_t *di)
   2499 {
   2500 	device_t dv = NULL;
   2501 
   2502 	switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
   2503 	case 0:
   2504 		return deviter_next1(di);
   2505 	case DEVITER_F_LEAVES_FIRST:
   2506 		while (di->di_curdepth >= 0) {
   2507 			if ((dv = deviter_next1(di)) == NULL) {
   2508 				di->di_curdepth--;
   2509 				deviter_reinit(di);
   2510 			} else if (dv->dv_depth == di->di_curdepth)
   2511 				break;
   2512 		}
   2513 		return dv;
   2514 	case DEVITER_F_ROOT_FIRST:
   2515 		while (di->di_curdepth <= di->di_maxdepth) {
   2516 			if ((dv = deviter_next1(di)) == NULL) {
   2517 				di->di_curdepth++;
   2518 				deviter_reinit(di);
   2519 			} else if (dv->dv_depth == di->di_curdepth)
   2520 				break;
   2521 		}
   2522 		return dv;
   2523 	default:
   2524 		return NULL;
   2525 	}
   2526 }
   2527 
   2528 void
   2529 deviter_release(deviter_t *di)
   2530 {
   2531 	bool rw = (di->di_flags & DEVITER_F_RW) != 0;
   2532 
   2533 	mutex_enter(&alldevs_mtx);
   2534 	if (!rw) {
   2535 		--alldevs_nread;
   2536 		cv_signal(&alldevs_cv);
   2537 	} else if (alldevs_nwrite > 0 && alldevs_writer == NULL) {
   2538 		--alldevs_nwrite;	/* shutting down: do not signal */
   2539 	} else {
   2540 		KASSERT(alldevs_nwrite != 0);
   2541 		if (--alldevs_nwrite == 0)
   2542 			alldevs_writer = NULL;
   2543 		cv_signal(&alldevs_cv);
   2544 	}
   2545 	mutex_exit(&alldevs_mtx);
   2546 }
   2547 
   2548 static void
   2549 sysctl_detach_setup(struct sysctllog **clog)
   2550 {
   2551 	const struct sysctlnode *node = NULL;
   2552 
   2553 	sysctl_createv(clog, 0, NULL, &node,
   2554 		CTLFLAG_PERMANENT,
   2555 		CTLTYPE_NODE, "kern", NULL,
   2556 		NULL, 0, NULL, 0,
   2557 		CTL_KERN, CTL_EOL);
   2558 
   2559 	if (node == NULL)
   2560 		return;
   2561 
   2562 	sysctl_createv(clog, 0, &node, NULL,
   2563 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   2564 		CTLTYPE_INT, "detachall",
   2565 		SYSCTL_DESCR("Detach all devices at shutdown"),
   2566 		NULL, 0, &detachall, 0,
   2567 		CTL_CREATE, CTL_EOL);
   2568 }
   2569