Home | History | Annotate | Line # | Download | only in kern
subr_autoconf.c revision 1.119.4.6
      1 /* $NetBSD: subr_autoconf.c,v 1.119.4.6 2007/11/14 19:04:43 joerg 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.119.4.6 2007/11/14 19:04:43 joerg Exp $");
     81 
     82 #include "opt_ddb.h"
     83 
     84 #include <sys/param.h>
     85 #include <sys/device.h>
     86 #include <sys/disklabel.h>
     87 #include <sys/conf.h>
     88 #include <sys/kauth.h>
     89 #include <sys/malloc.h>
     90 #include <sys/systm.h>
     91 #include <sys/kernel.h>
     92 #include <sys/errno.h>
     93 #include <sys/proc.h>
     94 #include <sys/reboot.h>
     95 
     96 #include <sys/buf.h>
     97 #include <sys/dirent.h>
     98 #include <sys/lock.h>
     99 #include <sys/vnode.h>
    100 #include <sys/mount.h>
    101 #include <sys/namei.h>
    102 #include <sys/unistd.h>
    103 #include <sys/fcntl.h>
    104 #include <sys/lockf.h>
    105 #include <sys/callout.h>
    106 
    107 #include <sys/disk.h>
    108 
    109 #include <machine/limits.h>
    110 
    111 #include "opt_userconf.h"
    112 #ifdef USERCONF
    113 #include <sys/userconf.h>
    114 #endif
    115 
    116 #ifdef __i386__
    117 #include "opt_splash.h"
    118 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
    119 #include <dev/splash/splash.h>
    120 extern struct splash_progress *splash_progress_state;
    121 #endif
    122 #endif
    123 
    124 /*
    125  * Autoconfiguration subroutines.
    126  */
    127 
    128 /*
    129  * ioconf.c exports exactly two names: cfdata and cfroots.  All system
    130  * devices and drivers are found via these tables.
    131  */
    132 extern struct cfdata cfdata[];
    133 extern const short cfroots[];
    134 
    135 /*
    136  * List of all cfdriver structures.  We use this to detect duplicates
    137  * when other cfdrivers are loaded.
    138  */
    139 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
    140 extern struct cfdriver * const cfdriver_list_initial[];
    141 
    142 /*
    143  * Initial list of cfattach's.
    144  */
    145 extern const struct cfattachinit cfattachinit[];
    146 
    147 /*
    148  * List of cfdata tables.  We always have one such list -- the one
    149  * built statically when the kernel was configured.
    150  */
    151 struct cftablelist allcftables = TAILQ_HEAD_INITIALIZER(allcftables);
    152 static struct cftable initcftable;
    153 
    154 #define	ROOT ((device_t)NULL)
    155 
    156 struct matchinfo {
    157 	cfsubmatch_t fn;
    158 	struct	device *parent;
    159 	const int *locs;
    160 	void	*aux;
    161 	struct	cfdata *match;
    162 	int	pri;
    163 };
    164 
    165 static char *number(char *, int);
    166 static void mapply(struct matchinfo *, cfdata_t);
    167 static device_t config_devalloc(const device_t, const cfdata_t, const int *);
    168 static void config_devdealloc(device_t);
    169 static void config_makeroom(int, struct cfdriver *);
    170 static void config_devlink(device_t);
    171 static void config_devunlink(device_t);
    172 
    173 struct deferred_config {
    174 	TAILQ_ENTRY(deferred_config) dc_queue;
    175 	device_t dc_dev;
    176 	void (*dc_func)(device_t);
    177 };
    178 
    179 TAILQ_HEAD(deferred_config_head, deferred_config);
    180 
    181 struct deferred_config_head deferred_config_queue =
    182 	TAILQ_HEAD_INITIALIZER(deferred_config_queue);
    183 struct deferred_config_head interrupt_config_queue =
    184 	TAILQ_HEAD_INITIALIZER(interrupt_config_queue);
    185 
    186 static void config_process_deferred(struct deferred_config_head *, device_t);
    187 
    188 /* Hooks to finalize configuration once all real devices have been found. */
    189 struct finalize_hook {
    190 	TAILQ_ENTRY(finalize_hook) f_list;
    191 	int (*f_func)(device_t);
    192 	device_t f_dev;
    193 };
    194 static TAILQ_HEAD(, finalize_hook) config_finalize_list =
    195 	TAILQ_HEAD_INITIALIZER(config_finalize_list);
    196 static int config_finalize_done;
    197 
    198 /* list of all devices */
    199 struct devicelist alldevs = TAILQ_HEAD_INITIALIZER(alldevs);
    200 
    201 volatile int config_pending;		/* semaphore for mountroot */
    202 
    203 #define	STREQ(s1, s2)			\
    204 	(*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
    205 
    206 static int config_initialized;		/* config_init() has been called. */
    207 
    208 static int config_do_twiddle;
    209 
    210 struct vnode *
    211 opendisk(struct device *dv)
    212 {
    213 	int bmajor, bminor;
    214 	struct vnode *tmpvn;
    215 	int error;
    216 	dev_t dev;
    217 
    218 	/*
    219 	 * Lookup major number for disk block device.
    220 	 */
    221 	bmajor = devsw_name2blk(device_xname(dv), NULL, 0);
    222 	if (bmajor == -1)
    223 		return NULL;
    224 
    225 	bminor = minor(device_unit(dv));
    226 	/*
    227 	 * Fake a temporary vnode for the disk, open it, and read
    228 	 * and hash the sectors.
    229 	 */
    230 	dev = device_is_a(dv, "dk") ? makedev(bmajor, bminor) :
    231 	    MAKEDISKDEV(bmajor, bminor, RAW_PART);
    232 	if (bdevvp(dev, &tmpvn))
    233 		panic("%s: can't alloc vnode for %s", __func__,
    234 		    device_xname(dv));
    235 	error = VOP_OPEN(tmpvn, FREAD, NOCRED, 0);
    236 	if (error) {
    237 #ifndef DEBUG
    238 		/*
    239 		 * Ignore errors caused by missing device, partition,
    240 		 * or medium.
    241 		 */
    242 		if (error != ENXIO && error != ENODEV)
    243 #endif
    244 			printf("%s: can't open dev %s (%d)\n",
    245 			    __func__, device_xname(dv), error);
    246 		vput(tmpvn);
    247 		return NULL;
    248 	}
    249 
    250 	return tmpvn;
    251 }
    252 
    253 int
    254 config_handle_wedges(struct device *dv, int par)
    255 {
    256 	struct dkwedge_list wl;
    257 	struct dkwedge_info *wi;
    258 	struct vnode *vn;
    259 	char diskname[16];
    260 	int i, error;
    261 
    262 	if ((vn = opendisk(dv)) == NULL)
    263 		return -1;
    264 
    265 	wl.dkwl_bufsize = sizeof(*wi) * 16;
    266 	wl.dkwl_buf = wi = malloc(wl.dkwl_bufsize, M_TEMP, M_WAITOK);
    267 
    268 	error = VOP_IOCTL(vn, DIOCLWEDGES, &wl, FREAD, NOCRED, 0);
    269 	VOP_CLOSE(vn, FREAD, NOCRED, 0);
    270 	vput(vn);
    271 	if (error) {
    272 #ifdef DEBUG_WEDGE
    273 		printf("%s: List wedges returned %d\n",
    274 		    device_xname(dv), error);
    275 #endif
    276 		free(wi, M_TEMP);
    277 		return -1;
    278 	}
    279 
    280 #ifdef DEBUG_WEDGE
    281 	printf("%s: Returned %u(%u) wedges\n", device_xname(dv),
    282 	    wl.dkwl_nwedges, wl.dkwl_ncopied);
    283 #endif
    284 	snprintf(diskname, sizeof(diskname), "%s%c", device_xname(dv),
    285 	    par + 'a');
    286 
    287 	for (i = 0; i < wl.dkwl_ncopied; i++) {
    288 #ifdef DEBUG_WEDGE
    289 		printf("%s: Looking for %s in %s\n",
    290 		    device_xname(dv), diskname, wi[i].dkw_wname);
    291 #endif
    292 		if (strcmp(wi[i].dkw_wname, diskname) == 0)
    293 			break;
    294 	}
    295 
    296 	if (i == wl.dkwl_ncopied) {
    297 #ifdef DEBUG_WEDGE
    298 		printf("%s: Cannot find wedge with parent %s\n",
    299 		    device_xname(dv), diskname);
    300 #endif
    301 		free(wi, M_TEMP);
    302 		return -1;
    303 	}
    304 
    305 #ifdef DEBUG_WEDGE
    306 	printf("%s: Setting boot wedge %s (%s) at %llu %llu\n",
    307 		device_xname(dv), wi[i].dkw_devname, wi[i].dkw_wname,
    308 		(unsigned long long)wi[i].dkw_offset,
    309 		(unsigned long long)wi[i].dkw_size);
    310 #endif
    311 	dkwedge_set_bootwedge(dv, wi[i].dkw_offset, wi[i].dkw_size);
    312 	free(wi, M_TEMP);
    313 	return 0;
    314 }
    315 
    316 /*
    317  * Initialize the autoconfiguration data structures.  Normally this
    318  * is done by configure(), but some platforms need to do this very
    319  * early (to e.g. initialize the console).
    320  */
    321 void
    322 config_init(void)
    323 {
    324 	const struct cfattachinit *cfai;
    325 	int i, j;
    326 
    327 	if (config_initialized)
    328 		return;
    329 
    330 	/* allcfdrivers is statically initialized. */
    331 	for (i = 0; cfdriver_list_initial[i] != NULL; i++) {
    332 		if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0)
    333 			panic("configure: duplicate `%s' drivers",
    334 			    cfdriver_list_initial[i]->cd_name);
    335 	}
    336 
    337 	for (cfai = &cfattachinit[0]; cfai->cfai_name != NULL; cfai++) {
    338 		for (j = 0; cfai->cfai_list[j] != NULL; j++) {
    339 			if (config_cfattach_attach(cfai->cfai_name,
    340 						   cfai->cfai_list[j]) != 0)
    341 				panic("configure: duplicate `%s' attachment "
    342 				    "of `%s' driver",
    343 				    cfai->cfai_list[j]->ca_name,
    344 				    cfai->cfai_name);
    345 		}
    346 	}
    347 
    348 	initcftable.ct_cfdata = cfdata;
    349 	TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
    350 
    351 	config_initialized = 1;
    352 }
    353 
    354 /*
    355  * Configure the system's hardware.
    356  */
    357 void
    358 configure(void)
    359 {
    360 	int errcnt;
    361 
    362 	/* Initialize data structures. */
    363 	config_init();
    364 	pnp_init();
    365 
    366 #ifdef USERCONF
    367 	if (boothowto & RB_USERCONF)
    368 		user_config();
    369 #endif
    370 
    371 	if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
    372 		config_do_twiddle = 1;
    373 		printf_nolog("Detecting hardware...");
    374 	}
    375 
    376 	/*
    377 	 * Do the machine-dependent portion of autoconfiguration.  This
    378 	 * sets the configuration machinery here in motion by "finding"
    379 	 * the root bus.  When this function returns, we expect interrupts
    380 	 * to be enabled.
    381 	 */
    382 	cpu_configure();
    383 
    384 	/* Initialize callouts, part 2. */
    385 	callout_startup2();
    386 
    387 	/*
    388 	 * Now that we've found all the hardware, start the real time
    389 	 * and statistics clocks.
    390 	 */
    391 	initclocks();
    392 
    393 	cold = 0;	/* clocks are running, we're warm now! */
    394 
    395 	/*
    396 	 * Now callback to finish configuration for devices which want
    397 	 * to do this once interrupts are enabled.
    398 	 */
    399 	config_process_deferred(&interrupt_config_queue, NULL);
    400 
    401 	errcnt = aprint_get_error_count();
    402 	if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
    403 	    (boothowto & AB_VERBOSE) == 0) {
    404 		if (config_do_twiddle) {
    405 			config_do_twiddle = 0;
    406 			printf_nolog("done.\n");
    407 		}
    408 		if (errcnt != 0) {
    409 			printf("WARNING: %d error%s while detecting hardware; "
    410 			    "check system log.\n", errcnt,
    411 			    errcnt == 1 ? "" : "s");
    412 		}
    413 	}
    414 }
    415 
    416 /*
    417  * Add a cfdriver to the system.
    418  */
    419 int
    420 config_cfdriver_attach(struct cfdriver *cd)
    421 {
    422 	struct cfdriver *lcd;
    423 
    424 	/* Make sure this driver isn't already in the system. */
    425 	LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
    426 		if (STREQ(lcd->cd_name, cd->cd_name))
    427 			return (EEXIST);
    428 	}
    429 
    430 	LIST_INIT(&cd->cd_attach);
    431 	LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
    432 
    433 	return (0);
    434 }
    435 
    436 /*
    437  * Remove a cfdriver from the system.
    438  */
    439 int
    440 config_cfdriver_detach(struct cfdriver *cd)
    441 {
    442 	int i;
    443 
    444 	/* Make sure there are no active instances. */
    445 	for (i = 0; i < cd->cd_ndevs; i++) {
    446 		if (cd->cd_devs[i] != NULL)
    447 			return (EBUSY);
    448 	}
    449 
    450 	/* ...and no attachments loaded. */
    451 	if (LIST_EMPTY(&cd->cd_attach) == 0)
    452 		return (EBUSY);
    453 
    454 	LIST_REMOVE(cd, cd_list);
    455 
    456 	KASSERT(cd->cd_devs == NULL);
    457 
    458 	return (0);
    459 }
    460 
    461 /*
    462  * Look up a cfdriver by name.
    463  */
    464 struct cfdriver *
    465 config_cfdriver_lookup(const char *name)
    466 {
    467 	struct cfdriver *cd;
    468 
    469 	LIST_FOREACH(cd, &allcfdrivers, cd_list) {
    470 		if (STREQ(cd->cd_name, name))
    471 			return (cd);
    472 	}
    473 
    474 	return (NULL);
    475 }
    476 
    477 /*
    478  * Add a cfattach to the specified driver.
    479  */
    480 int
    481 config_cfattach_attach(const char *driver, struct cfattach *ca)
    482 {
    483 	struct cfattach *lca;
    484 	struct cfdriver *cd;
    485 
    486 	cd = config_cfdriver_lookup(driver);
    487 	if (cd == NULL)
    488 		return (ESRCH);
    489 
    490 	/* Make sure this attachment isn't already on this driver. */
    491 	LIST_FOREACH(lca, &cd->cd_attach, ca_list) {
    492 		if (STREQ(lca->ca_name, ca->ca_name))
    493 			return (EEXIST);
    494 	}
    495 
    496 	LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list);
    497 
    498 	return (0);
    499 }
    500 
    501 /*
    502  * Remove a cfattach from the specified driver.
    503  */
    504 int
    505 config_cfattach_detach(const char *driver, struct cfattach *ca)
    506 {
    507 	struct cfdriver *cd;
    508 	device_t dev;
    509 	int i;
    510 
    511 	cd = config_cfdriver_lookup(driver);
    512 	if (cd == NULL)
    513 		return (ESRCH);
    514 
    515 	/* Make sure there are no active instances. */
    516 	for (i = 0; i < cd->cd_ndevs; i++) {
    517 		if ((dev = cd->cd_devs[i]) == NULL)
    518 			continue;
    519 		if (dev->dv_cfattach == ca)
    520 			return (EBUSY);
    521 	}
    522 
    523 	LIST_REMOVE(ca, ca_list);
    524 
    525 	return (0);
    526 }
    527 
    528 /*
    529  * Look up a cfattach by name.
    530  */
    531 static struct cfattach *
    532 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname)
    533 {
    534 	struct cfattach *ca;
    535 
    536 	LIST_FOREACH(ca, &cd->cd_attach, ca_list) {
    537 		if (STREQ(ca->ca_name, atname))
    538 			return (ca);
    539 	}
    540 
    541 	return (NULL);
    542 }
    543 
    544 /*
    545  * Look up a cfattach by driver/attachment name.
    546  */
    547 struct cfattach *
    548 config_cfattach_lookup(const char *name, const char *atname)
    549 {
    550 	struct cfdriver *cd;
    551 
    552 	cd = config_cfdriver_lookup(name);
    553 	if (cd == NULL)
    554 		return (NULL);
    555 
    556 	return (config_cfattach_lookup_cd(cd, atname));
    557 }
    558 
    559 /*
    560  * Apply the matching function and choose the best.  This is used
    561  * a few times and we want to keep the code small.
    562  */
    563 static void
    564 mapply(struct matchinfo *m, cfdata_t cf)
    565 {
    566 	int pri;
    567 
    568 	if (m->fn != NULL) {
    569 		pri = (*m->fn)(m->parent, cf, m->locs, m->aux);
    570 	} else {
    571 		pri = config_match(m->parent, cf, m->aux);
    572 	}
    573 	if (pri > m->pri) {
    574 		m->match = cf;
    575 		m->pri = pri;
    576 	}
    577 }
    578 
    579 int
    580 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    581 {
    582 	const struct cfiattrdata *ci;
    583 	const struct cflocdesc *cl;
    584 	int nlocs, i;
    585 
    586 	ci = cfiattr_lookup(cf->cf_pspec->cfp_iattr, parent->dv_cfdriver);
    587 	KASSERT(ci);
    588 	nlocs = ci->ci_loclen;
    589 	for (i = 0; i < nlocs; i++) {
    590 		cl = &ci->ci_locdesc[i];
    591 		/* !cld_defaultstr means no default value */
    592 		if ((!(cl->cld_defaultstr)
    593 		     || (cf->cf_loc[i] != cl->cld_default))
    594 		    && cf->cf_loc[i] != locs[i])
    595 			return (0);
    596 	}
    597 
    598 	return (config_match(parent, cf, aux));
    599 }
    600 
    601 /*
    602  * Helper function: check whether the driver supports the interface attribute
    603  * and return its descriptor structure.
    604  */
    605 static const struct cfiattrdata *
    606 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia)
    607 {
    608 	const struct cfiattrdata * const *cpp;
    609 
    610 	if (cd->cd_attrs == NULL)
    611 		return (0);
    612 
    613 	for (cpp = cd->cd_attrs; *cpp; cpp++) {
    614 		if (STREQ((*cpp)->ci_name, ia)) {
    615 			/* Match. */
    616 			return (*cpp);
    617 		}
    618 	}
    619 	return (0);
    620 }
    621 
    622 /*
    623  * Lookup an interface attribute description by name.
    624  * If the driver is given, consider only its supported attributes.
    625  */
    626 const struct cfiattrdata *
    627 cfiattr_lookup(const char *name, const struct cfdriver *cd)
    628 {
    629 	const struct cfdriver *d;
    630 	const struct cfiattrdata *ia;
    631 
    632 	if (cd)
    633 		return (cfdriver_get_iattr(cd, name));
    634 
    635 	LIST_FOREACH(d, &allcfdrivers, cd_list) {
    636 		ia = cfdriver_get_iattr(d, name);
    637 		if (ia)
    638 			return (ia);
    639 	}
    640 	return (0);
    641 }
    642 
    643 /*
    644  * Determine if `parent' is a potential parent for a device spec based
    645  * on `cfp'.
    646  */
    647 static int
    648 cfparent_match(const device_t parent, const struct cfparent *cfp)
    649 {
    650 	struct cfdriver *pcd;
    651 
    652 	/* We don't match root nodes here. */
    653 	if (cfp == NULL)
    654 		return (0);
    655 
    656 	pcd = parent->dv_cfdriver;
    657 	KASSERT(pcd != NULL);
    658 
    659 	/*
    660 	 * First, ensure this parent has the correct interface
    661 	 * attribute.
    662 	 */
    663 	if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr))
    664 		return (0);
    665 
    666 	/*
    667 	 * If no specific parent device instance was specified (i.e.
    668 	 * we're attaching to the attribute only), we're done!
    669 	 */
    670 	if (cfp->cfp_parent == NULL)
    671 		return (1);
    672 
    673 	/*
    674 	 * Check the parent device's name.
    675 	 */
    676 	if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
    677 		return (0);	/* not the same parent */
    678 
    679 	/*
    680 	 * Make sure the unit number matches.
    681 	 */
    682 	if (cfp->cfp_unit == DVUNIT_ANY ||	/* wildcard */
    683 	    cfp->cfp_unit == parent->dv_unit)
    684 		return (1);
    685 
    686 	/* Unit numbers don't match. */
    687 	return (0);
    688 }
    689 
    690 /*
    691  * Helper for config_cfdata_attach(): check all devices whether it could be
    692  * parent any attachment in the config data table passed, and rescan.
    693  */
    694 static void
    695 rescan_with_cfdata(const struct cfdata *cf)
    696 {
    697 	device_t d;
    698 	const struct cfdata *cf1;
    699 
    700 	/*
    701 	 * "alldevs" is likely longer than an LKM's cfdata, so make it
    702 	 * the outer loop.
    703 	 */
    704 	TAILQ_FOREACH(d, &alldevs, dv_list) {
    705 
    706 		if (!(d->dv_cfattach->ca_rescan))
    707 			continue;
    708 
    709 		for (cf1 = cf; cf1->cf_name; cf1++) {
    710 
    711 			if (!cfparent_match(d, cf1->cf_pspec))
    712 				continue;
    713 
    714 			(*d->dv_cfattach->ca_rescan)(d,
    715 				cf1->cf_pspec->cfp_iattr, cf1->cf_loc);
    716 		}
    717 	}
    718 }
    719 
    720 /*
    721  * Attach a supplemental config data table and rescan potential
    722  * parent devices if required.
    723  */
    724 int
    725 config_cfdata_attach(cfdata_t cf, int scannow)
    726 {
    727 	struct cftable *ct;
    728 
    729 	ct = malloc(sizeof(struct cftable), M_DEVBUF, M_WAITOK);
    730 	ct->ct_cfdata = cf;
    731 	TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
    732 
    733 	if (scannow)
    734 		rescan_with_cfdata(cf);
    735 
    736 	return (0);
    737 }
    738 
    739 /*
    740  * Helper for config_cfdata_detach: check whether a device is
    741  * found through any attachment in the config data table.
    742  */
    743 static int
    744 dev_in_cfdata(const struct device *d, const struct cfdata *cf)
    745 {
    746 	const struct cfdata *cf1;
    747 
    748 	for (cf1 = cf; cf1->cf_name; cf1++)
    749 		if (d->dv_cfdata == cf1)
    750 			return (1);
    751 
    752 	return (0);
    753 }
    754 
    755 /*
    756  * Detach a supplemental config data table. Detach all devices found
    757  * through that table (and thus keeping references to it) before.
    758  */
    759 int
    760 config_cfdata_detach(cfdata_t cf)
    761 {
    762 	device_t d;
    763 	int error;
    764 	struct cftable *ct;
    765 
    766 again:
    767 	TAILQ_FOREACH(d, &alldevs, dv_list) {
    768 		if (dev_in_cfdata(d, cf)) {
    769 			error = config_detach(d, 0);
    770 			if (error) {
    771 				aprint_error("%s: unable to detach instance\n",
    772 					d->dv_xname);
    773 				return (error);
    774 			}
    775 			goto again;
    776 		}
    777 	}
    778 
    779 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
    780 		if (ct->ct_cfdata == cf) {
    781 			TAILQ_REMOVE(&allcftables, ct, ct_list);
    782 			free(ct, M_DEVBUF);
    783 			return (0);
    784 		}
    785 	}
    786 
    787 	/* not found -- shouldn't happen */
    788 	return (EINVAL);
    789 }
    790 
    791 /*
    792  * Invoke the "match" routine for a cfdata entry on behalf of
    793  * an external caller, usually a "submatch" routine.
    794  */
    795 int
    796 config_match(device_t parent, cfdata_t cf, void *aux)
    797 {
    798 	struct cfattach *ca;
    799 
    800 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
    801 	if (ca == NULL) {
    802 		/* No attachment for this entry, oh well. */
    803 		return (0);
    804 	}
    805 
    806 	return ((*ca->ca_match)(parent, cf, aux));
    807 }
    808 
    809 /*
    810  * Iterate over all potential children of some device, calling the given
    811  * function (default being the child's match function) for each one.
    812  * Nonzero returns are matches; the highest value returned is considered
    813  * the best match.  Return the `found child' if we got a match, or NULL
    814  * otherwise.  The `aux' pointer is simply passed on through.
    815  *
    816  * Note that this function is designed so that it can be used to apply
    817  * an arbitrary function to all potential children (its return value
    818  * can be ignored).
    819  */
    820 cfdata_t
    821 config_search_loc(cfsubmatch_t fn, device_t parent,
    822 		  const char *ifattr, const int *locs, void *aux)
    823 {
    824 	struct cftable *ct;
    825 	cfdata_t cf;
    826 	struct matchinfo m;
    827 
    828 	KASSERT(config_initialized);
    829 	KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr));
    830 
    831 	m.fn = fn;
    832 	m.parent = parent;
    833 	m.locs = locs;
    834 	m.aux = aux;
    835 	m.match = NULL;
    836 	m.pri = 0;
    837 
    838 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
    839 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
    840 
    841 			/* We don't match root nodes here. */
    842 			if (!cf->cf_pspec)
    843 				continue;
    844 
    845 			/*
    846 			 * Skip cf if no longer eligible, otherwise scan
    847 			 * through parents for one matching `parent', and
    848 			 * try match function.
    849 			 */
    850 			if (cf->cf_fstate == FSTATE_FOUND)
    851 				continue;
    852 			if (cf->cf_fstate == FSTATE_DNOTFOUND ||
    853 			    cf->cf_fstate == FSTATE_DSTAR)
    854 				continue;
    855 
    856 			/*
    857 			 * If an interface attribute was specified,
    858 			 * consider only children which attach to
    859 			 * that attribute.
    860 			 */
    861 			if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr))
    862 				continue;
    863 
    864 			if (cfparent_match(parent, cf->cf_pspec))
    865 				mapply(&m, cf);
    866 		}
    867 	}
    868 	return (m.match);
    869 }
    870 
    871 cfdata_t
    872 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr,
    873     void *aux)
    874 {
    875 
    876 	return (config_search_loc(fn, parent, ifattr, NULL, aux));
    877 }
    878 
    879 /*
    880  * Find the given root device.
    881  * This is much like config_search, but there is no parent.
    882  * Don't bother with multiple cfdata tables; the root node
    883  * must always be in the initial table.
    884  */
    885 cfdata_t
    886 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux)
    887 {
    888 	cfdata_t cf;
    889 	const short *p;
    890 	struct matchinfo m;
    891 
    892 	m.fn = fn;
    893 	m.parent = ROOT;
    894 	m.aux = aux;
    895 	m.match = NULL;
    896 	m.pri = 0;
    897 	m.locs = 0;
    898 	/*
    899 	 * Look at root entries for matching name.  We do not bother
    900 	 * with found-state here since only one root should ever be
    901 	 * searched (and it must be done first).
    902 	 */
    903 	for (p = cfroots; *p >= 0; p++) {
    904 		cf = &cfdata[*p];
    905 		if (strcmp(cf->cf_name, rootname) == 0)
    906 			mapply(&m, cf);
    907 	}
    908 	return (m.match);
    909 }
    910 
    911 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
    912 
    913 /*
    914  * The given `aux' argument describes a device that has been found
    915  * on the given parent, but not necessarily configured.  Locate the
    916  * configuration data for that device (using the submatch function
    917  * provided, or using candidates' cd_match configuration driver
    918  * functions) and attach it, and return true.  If the device was
    919  * not configured, call the given `print' function and return 0.
    920  */
    921 device_t
    922 config_found_sm_loc(device_t parent,
    923 		const char *ifattr, const int *locs, void *aux,
    924 		cfprint_t print, cfsubmatch_t submatch)
    925 {
    926 	cfdata_t cf;
    927 
    928 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
    929 	if (splash_progress_state)
    930 		splash_progress_update(splash_progress_state);
    931 #endif
    932 
    933 	if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux)))
    934 		return(config_attach_loc(parent, cf, locs, aux, print));
    935 	if (print) {
    936 		if (config_do_twiddle)
    937 			twiddle();
    938 		aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]);
    939 	}
    940 
    941 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
    942 	if (splash_progress_state)
    943 		splash_progress_update(splash_progress_state);
    944 #endif
    945 
    946 	return (NULL);
    947 }
    948 
    949 device_t
    950 config_found_ia(device_t parent, const char *ifattr, void *aux,
    951     cfprint_t print)
    952 {
    953 
    954 	return (config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL));
    955 }
    956 
    957 device_t
    958 config_found(device_t parent, void *aux, cfprint_t print)
    959 {
    960 
    961 	return (config_found_sm_loc(parent, NULL, NULL, aux, print, NULL));
    962 }
    963 
    964 /*
    965  * As above, but for root devices.
    966  */
    967 device_t
    968 config_rootfound(const char *rootname, void *aux)
    969 {
    970 	cfdata_t cf;
    971 
    972 	if ((cf = config_rootsearch((cfsubmatch_t)NULL, rootname, aux)) != NULL)
    973 		return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
    974 	aprint_error("root device %s not configured\n", rootname);
    975 	return (NULL);
    976 }
    977 
    978 /* just like sprintf(buf, "%d") except that it works from the end */
    979 static char *
    980 number(char *ep, int n)
    981 {
    982 
    983 	*--ep = 0;
    984 	while (n >= 10) {
    985 		*--ep = (n % 10) + '0';
    986 		n /= 10;
    987 	}
    988 	*--ep = n + '0';
    989 	return (ep);
    990 }
    991 
    992 /*
    993  * Expand the size of the cd_devs array if necessary.
    994  */
    995 static void
    996 config_makeroom(int n, struct cfdriver *cd)
    997 {
    998 	int old, new;
    999 	void **nsp;
   1000 
   1001 	if (n < cd->cd_ndevs)
   1002 		return;
   1003 
   1004 	/*
   1005 	 * Need to expand the array.
   1006 	 */
   1007 	old = cd->cd_ndevs;
   1008 	if (old == 0)
   1009 		new = 4;
   1010 	else
   1011 		new = old * 2;
   1012 	while (new <= n)
   1013 		new *= 2;
   1014 	cd->cd_ndevs = new;
   1015 	nsp = malloc(new * sizeof(void *), M_DEVBUF,
   1016 	    cold ? M_NOWAIT : M_WAITOK);
   1017 	if (nsp == NULL)
   1018 		panic("config_attach: %sing dev array",
   1019 		    old != 0 ? "expand" : "creat");
   1020 	memset(nsp + old, 0, (new - old) * sizeof(void *));
   1021 	if (old != 0) {
   1022 		memcpy(nsp, cd->cd_devs, old * sizeof(void *));
   1023 		free(cd->cd_devs, M_DEVBUF);
   1024 	}
   1025 	cd->cd_devs = nsp;
   1026 }
   1027 
   1028 static void
   1029 config_devlink(device_t dev)
   1030 {
   1031 	struct cfdriver *cd = dev->dv_cfdriver;
   1032 
   1033 	/* put this device in the devices array */
   1034 	config_makeroom(dev->dv_unit, cd);
   1035 	if (cd->cd_devs[dev->dv_unit])
   1036 		panic("config_attach: duplicate %s", dev->dv_xname);
   1037 	cd->cd_devs[dev->dv_unit] = dev;
   1038 
   1039 	TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);	/* link up */
   1040 }
   1041 
   1042 static void
   1043 config_devunlink(device_t dev)
   1044 {
   1045 	struct cfdriver *cd = dev->dv_cfdriver;
   1046 	int i;
   1047 
   1048 	/* Unlink from device list. */
   1049 	TAILQ_REMOVE(&alldevs, dev, dv_list);
   1050 
   1051 	/* Remove from cfdriver's array. */
   1052 	cd->cd_devs[dev->dv_unit] = NULL;
   1053 
   1054 	/*
   1055 	 * If the device now has no units in use, deallocate its softc array.
   1056 	 */
   1057 	for (i = 0; i < cd->cd_ndevs; i++)
   1058 		if (cd->cd_devs[i] != NULL)
   1059 			break;
   1060 	if (i == cd->cd_ndevs) {		/* nothing found; deallocate */
   1061 		free(cd->cd_devs, M_DEVBUF);
   1062 		cd->cd_devs = NULL;
   1063 		cd->cd_ndevs = 0;
   1064 	}
   1065 }
   1066 
   1067 static device_t
   1068 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs)
   1069 {
   1070 	struct cfdriver *cd;
   1071 	struct cfattach *ca;
   1072 	size_t lname, lunit;
   1073 	const char *xunit;
   1074 	int myunit;
   1075 	char num[10];
   1076 	device_t dev;
   1077 	void *dev_private;
   1078 	const struct cfiattrdata *ia;
   1079 
   1080 	cd = config_cfdriver_lookup(cf->cf_name);
   1081 	if (cd == NULL)
   1082 		return (NULL);
   1083 
   1084 	ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
   1085 	if (ca == NULL)
   1086 		return (NULL);
   1087 
   1088 	if ((ca->ca_flags & DVF_PRIV_ALLOC) == 0 &&
   1089 	    ca->ca_devsize < sizeof(struct device))
   1090 		panic("config_devalloc");
   1091 
   1092 #ifndef __BROKEN_CONFIG_UNIT_USAGE
   1093 	if (cf->cf_fstate == FSTATE_STAR) {
   1094 		for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
   1095 			if (cd->cd_devs[myunit] == NULL)
   1096 				break;
   1097 		/*
   1098 		 * myunit is now the unit of the first NULL device pointer,
   1099 		 * or max(cd->cd_ndevs,cf->cf_unit).
   1100 		 */
   1101 	} else {
   1102 		myunit = cf->cf_unit;
   1103 		if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL)
   1104 			return (NULL);
   1105 	}
   1106 #else
   1107 	myunit = cf->cf_unit;
   1108 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
   1109 
   1110 	/* compute length of name and decimal expansion of unit number */
   1111 	lname = strlen(cd->cd_name);
   1112 	xunit = number(&num[sizeof(num)], myunit);
   1113 	lunit = &num[sizeof(num)] - xunit;
   1114 	if (lname + lunit > sizeof(dev->dv_xname))
   1115 		panic("config_devalloc: device name too long");
   1116 
   1117 	/* get memory for all device vars */
   1118 	dev_private = malloc(ca->ca_devsize, M_DEVBUF,
   1119 			     M_ZERO | (cold ? M_NOWAIT : M_WAITOK));
   1120 	if (dev_private == NULL)
   1121 		panic("config_devalloc: memory allocation for device softc failed");
   1122 
   1123 	if ((ca->ca_flags & DVF_PRIV_ALLOC) != 0) {
   1124 		dev = malloc(sizeof(struct device), M_DEVBUF,
   1125 			     M_ZERO | (cold ? M_NOWAIT : M_WAITOK));
   1126 	} else {
   1127 		dev = dev_private;
   1128 	}
   1129 	if (dev == NULL)
   1130 		panic("config_devalloc: memory allocation for device_t failed");
   1131 
   1132 	dev->dv_class = cd->cd_class;
   1133 	dev->dv_cfdata = cf;
   1134 	dev->dv_cfdriver = cd;
   1135 	dev->dv_cfattach = ca;
   1136 	dev->dv_unit = myunit;
   1137 	dev->dv_activity_count = 0;
   1138 	dev->dv_activity_handlers = NULL;
   1139 	dev->dv_private = dev_private;
   1140 	memcpy(dev->dv_xname, cd->cd_name, lname);
   1141 	memcpy(dev->dv_xname + lname, xunit, lunit);
   1142 	dev->dv_parent = parent;
   1143 	if (parent != NULL)
   1144 		dev->dv_depth = parent->dv_depth + 1;
   1145 	else
   1146 		dev->dv_depth = 0;
   1147 	dev->dv_flags = DVF_ACTIVE;	/* always initially active */
   1148 	dev->dv_flags |= ca->ca_flags;	/* inherit flags from class */
   1149 	if (locs) {
   1150 		KASSERT(parent); /* no locators at root */
   1151 		ia = cfiattr_lookup(cf->cf_pspec->cfp_iattr,
   1152 				    parent->dv_cfdriver);
   1153 		dev->dv_locators = malloc(ia->ci_loclen * sizeof(int),
   1154 					  M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
   1155 		memcpy(dev->dv_locators, locs, ia->ci_loclen * sizeof(int));
   1156 	}
   1157 	dev->dv_properties = prop_dictionary_create();
   1158 	KASSERT(dev->dv_properties != NULL);
   1159 
   1160 	return (dev);
   1161 }
   1162 
   1163 static void
   1164 config_devdealloc(device_t dev)
   1165 {
   1166 
   1167 	KASSERT(dev->dv_properties != NULL);
   1168 	prop_object_release(dev->dv_properties);
   1169 
   1170 	if (dev->dv_activity_handlers)
   1171 		panic("config_devdealloc with registered handlers");
   1172 
   1173 	if (dev->dv_locators)
   1174 		free(dev->dv_locators, M_DEVBUF);
   1175 
   1176 	if ((dev->dv_flags & DVF_PRIV_ALLOC) != 0)
   1177 		free(dev->dv_private, M_DEVBUF);
   1178 
   1179 	free(dev, M_DEVBUF);
   1180 }
   1181 
   1182 /*
   1183  * Attach a found device.
   1184  */
   1185 device_t
   1186 config_attach_loc(device_t parent, cfdata_t cf,
   1187 	const int *locs, void *aux, cfprint_t print)
   1188 {
   1189 	device_t dev;
   1190 	struct cftable *ct;
   1191 	const char *drvname;
   1192 
   1193 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
   1194 	if (splash_progress_state)
   1195 		splash_progress_update(splash_progress_state);
   1196 #endif
   1197 
   1198 	dev = config_devalloc(parent, cf, locs);
   1199 	if (!dev)
   1200 		panic("config_attach: allocation of device softc failed");
   1201 
   1202 	/* XXX redundant - see below? */
   1203 	if (cf->cf_fstate != FSTATE_STAR) {
   1204 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
   1205 		cf->cf_fstate = FSTATE_FOUND;
   1206 	}
   1207 #ifdef __BROKEN_CONFIG_UNIT_USAGE
   1208 	  else
   1209 		cf->cf_unit++;
   1210 #endif
   1211 
   1212 	config_devlink(dev);
   1213 
   1214 	if (config_do_twiddle)
   1215 		twiddle();
   1216 	else
   1217 		aprint_naive("Found ");
   1218 	/*
   1219 	 * We want the next two printfs for normal, verbose, and quiet,
   1220 	 * but not silent (in which case, we're twiddling, instead).
   1221 	 */
   1222 	if (parent == ROOT) {
   1223 		aprint_naive("%s (root)", dev->dv_xname);
   1224 		aprint_normal("%s (root)", dev->dv_xname);
   1225 	} else {
   1226 		aprint_naive("%s at %s", dev->dv_xname, parent->dv_xname);
   1227 		aprint_normal("%s at %s", dev->dv_xname, parent->dv_xname);
   1228 		if (print)
   1229 			(void) (*print)(aux, NULL);
   1230 	}
   1231 
   1232 	/*
   1233 	 * Before attaching, clobber any unfound devices that are
   1234 	 * otherwise identical.
   1235 	 * XXX code above is redundant?
   1236 	 */
   1237 	drvname = dev->dv_cfdriver->cd_name;
   1238 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
   1239 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
   1240 			if (STREQ(cf->cf_name, drvname) &&
   1241 			    cf->cf_unit == dev->dv_unit) {
   1242 				if (cf->cf_fstate == FSTATE_NOTFOUND)
   1243 					cf->cf_fstate = FSTATE_FOUND;
   1244 #ifdef __BROKEN_CONFIG_UNIT_USAGE
   1245 				/*
   1246 				 * Bump the unit number on all starred cfdata
   1247 				 * entries for this device.
   1248 				 */
   1249 				if (cf->cf_fstate == FSTATE_STAR)
   1250 					cf->cf_unit++;
   1251 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
   1252 			}
   1253 		}
   1254 	}
   1255 #ifdef __HAVE_DEVICE_REGISTER
   1256 	device_register(dev, aux);
   1257 #endif
   1258 
   1259 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
   1260 	if (splash_progress_state)
   1261 		splash_progress_update(splash_progress_state);
   1262 #endif
   1263 	(*dev->dv_cfattach->ca_attach)(parent, dev, aux);
   1264 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
   1265 	if (splash_progress_state)
   1266 		splash_progress_update(splash_progress_state);
   1267 #endif
   1268 
   1269 	if (!device_pnp_is_registered(dev))
   1270 		aprint_error_dev(dev, "WARNING: power management not supported\n");
   1271 
   1272 	config_process_deferred(&deferred_config_queue, dev);
   1273 	return (dev);
   1274 }
   1275 
   1276 device_t
   1277 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print)
   1278 {
   1279 
   1280 	return (config_attach_loc(parent, cf, NULL, aux, print));
   1281 }
   1282 
   1283 /*
   1284  * As above, but for pseudo-devices.  Pseudo-devices attached in this
   1285  * way are silently inserted into the device tree, and their children
   1286  * attached.
   1287  *
   1288  * Note that because pseudo-devices are attached silently, any information
   1289  * the attach routine wishes to print should be prefixed with the device
   1290  * name by the attach routine.
   1291  */
   1292 device_t
   1293 config_attach_pseudo(cfdata_t cf)
   1294 {
   1295 	device_t dev;
   1296 
   1297 	dev = config_devalloc(ROOT, cf, NULL);
   1298 	if (!dev)
   1299 		return (NULL);
   1300 
   1301 	/* XXX mark busy in cfdata */
   1302 
   1303 	config_devlink(dev);
   1304 
   1305 #if 0	/* XXXJRT not yet */
   1306 #ifdef __HAVE_DEVICE_REGISTER
   1307 	device_register(dev, NULL);	/* like a root node */
   1308 #endif
   1309 #endif
   1310 	(*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL);
   1311 	config_process_deferred(&deferred_config_queue, dev);
   1312 	return (dev);
   1313 }
   1314 
   1315 /*
   1316  * Detach a device.  Optionally forced (e.g. because of hardware
   1317  * removal) and quiet.  Returns zero if successful, non-zero
   1318  * (an error code) otherwise.
   1319  *
   1320  * Note that this code wants to be run from a process context, so
   1321  * that the detach can sleep to allow processes which have a device
   1322  * open to run and unwind their stacks.
   1323  */
   1324 int
   1325 config_detach(device_t dev, int flags)
   1326 {
   1327 	struct cftable *ct;
   1328 	cfdata_t cf;
   1329 	const struct cfattach *ca;
   1330 	struct cfdriver *cd;
   1331 #ifdef DIAGNOSTIC
   1332 	device_t d;
   1333 #endif
   1334 	int rv = 0;
   1335 
   1336 #ifdef DIAGNOSTIC
   1337 	if (dev->dv_cfdata != NULL &&
   1338 	    dev->dv_cfdata->cf_fstate != FSTATE_FOUND &&
   1339 	    dev->dv_cfdata->cf_fstate != FSTATE_STAR)
   1340 		panic("config_detach: bad device fstate");
   1341 #endif
   1342 	cd = dev->dv_cfdriver;
   1343 	KASSERT(cd != NULL);
   1344 
   1345 	ca = dev->dv_cfattach;
   1346 	KASSERT(ca != NULL);
   1347 
   1348 	/*
   1349 	 * Ensure the device is deactivated.  If the device doesn't
   1350 	 * have an activation entry point, we allow DVF_ACTIVE to
   1351 	 * remain set.  Otherwise, if DVF_ACTIVE is still set, the
   1352 	 * device is busy, and the detach fails.
   1353 	 */
   1354 	if (ca->ca_activate != NULL)
   1355 		rv = config_deactivate(dev);
   1356 
   1357 	/*
   1358 	 * Try to detach the device.  If that's not possible, then
   1359 	 * we either panic() (for the forced but failed case), or
   1360 	 * return an error.
   1361 	 */
   1362 	if (rv == 0) {
   1363 		if (ca->ca_detach != NULL)
   1364 			rv = (*ca->ca_detach)(dev, flags);
   1365 		else
   1366 			rv = EOPNOTSUPP;
   1367 	}
   1368 	if (rv != 0) {
   1369 		if ((flags & DETACH_FORCE) == 0)
   1370 			return (rv);
   1371 		else
   1372 			panic("config_detach: forced detach of %s failed (%d)",
   1373 			    dev->dv_xname, rv);
   1374 	}
   1375 
   1376 	/*
   1377 	 * The device has now been successfully detached.
   1378 	 */
   1379 
   1380 #ifdef DIAGNOSTIC
   1381 	/*
   1382 	 * Sanity: If you're successfully detached, you should have no
   1383 	 * children.  (Note that because children must be attached
   1384 	 * after parents, we only need to search the latter part of
   1385 	 * the list.)
   1386 	 */
   1387 	for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
   1388 	    d = TAILQ_NEXT(d, dv_list)) {
   1389 		if (d->dv_parent == dev) {
   1390 			printf("config_detach: detached device %s"
   1391 			    " has children %s\n", dev->dv_xname, d->dv_xname);
   1392 			panic("config_detach");
   1393 		}
   1394 	}
   1395 #endif
   1396 
   1397 	/* notify the parent that the child is gone */
   1398 	if (dev->dv_parent) {
   1399 		device_t p = dev->dv_parent;
   1400 		if (p->dv_cfattach->ca_childdetached)
   1401 			(*p->dv_cfattach->ca_childdetached)(p, dev);
   1402 	}
   1403 
   1404 	/*
   1405 	 * Mark cfdata to show that the unit can be reused, if possible.
   1406 	 */
   1407 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
   1408 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
   1409 			if (STREQ(cf->cf_name, cd->cd_name)) {
   1410 				if (cf->cf_fstate == FSTATE_FOUND &&
   1411 				    cf->cf_unit == dev->dv_unit)
   1412 					cf->cf_fstate = FSTATE_NOTFOUND;
   1413 #ifdef __BROKEN_CONFIG_UNIT_USAGE
   1414 				/*
   1415 				 * Note that we can only re-use a starred
   1416 				 * unit number if the unit being detached
   1417 				 * had the last assigned unit number.
   1418 				 */
   1419 				if (cf->cf_fstate == FSTATE_STAR &&
   1420 				    cf->cf_unit == dev->dv_unit + 1)
   1421 					cf->cf_unit--;
   1422 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
   1423 			}
   1424 		}
   1425 	}
   1426 
   1427 	config_devunlink(dev);
   1428 
   1429 	if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
   1430 		aprint_normal("%s detached\n", dev->dv_xname);
   1431 
   1432 	config_devdealloc(dev);
   1433 
   1434 	return (0);
   1435 }
   1436 
   1437 int
   1438 config_activate(device_t dev)
   1439 {
   1440 	const struct cfattach *ca = dev->dv_cfattach;
   1441 	int rv = 0, oflags = dev->dv_flags;
   1442 
   1443 	if (ca->ca_activate == NULL)
   1444 		return (EOPNOTSUPP);
   1445 
   1446 	if ((dev->dv_flags & DVF_ACTIVE) == 0) {
   1447 		dev->dv_flags |= DVF_ACTIVE;
   1448 		rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
   1449 		if (rv)
   1450 			dev->dv_flags = oflags;
   1451 	}
   1452 	return (rv);
   1453 }
   1454 
   1455 int
   1456 config_deactivate(device_t dev)
   1457 {
   1458 	const struct cfattach *ca = dev->dv_cfattach;
   1459 	int rv = 0, oflags = dev->dv_flags;
   1460 
   1461 	if (ca->ca_activate == NULL)
   1462 		return (EOPNOTSUPP);
   1463 
   1464 	if (dev->dv_flags & DVF_ACTIVE) {
   1465 		dev->dv_flags &= ~DVF_ACTIVE;
   1466 		rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
   1467 		if (rv)
   1468 			dev->dv_flags = oflags;
   1469 	}
   1470 	return (rv);
   1471 }
   1472 
   1473 /*
   1474  * Defer the configuration of the specified device until all
   1475  * of its parent's devices have been attached.
   1476  */
   1477 void
   1478 config_defer(device_t dev, void (*func)(device_t))
   1479 {
   1480 	struct deferred_config *dc;
   1481 
   1482 	if (dev->dv_parent == NULL)
   1483 		panic("config_defer: can't defer config of a root device");
   1484 
   1485 #ifdef DIAGNOSTIC
   1486 	for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
   1487 	     dc = TAILQ_NEXT(dc, dc_queue)) {
   1488 		if (dc->dc_dev == dev)
   1489 			panic("config_defer: deferred twice");
   1490 	}
   1491 #endif
   1492 
   1493 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
   1494 	if (dc == NULL)
   1495 		panic("config_defer: unable to allocate callback");
   1496 
   1497 	dc->dc_dev = dev;
   1498 	dc->dc_func = func;
   1499 	TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
   1500 	config_pending_incr();
   1501 }
   1502 
   1503 /*
   1504  * Defer some autoconfiguration for a device until after interrupts
   1505  * are enabled.
   1506  */
   1507 void
   1508 config_interrupts(device_t dev, void (*func)(device_t))
   1509 {
   1510 	struct deferred_config *dc;
   1511 
   1512 	/*
   1513 	 * If interrupts are enabled, callback now.
   1514 	 */
   1515 	if (cold == 0) {
   1516 		(*func)(dev);
   1517 		return;
   1518 	}
   1519 
   1520 #ifdef DIAGNOSTIC
   1521 	for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
   1522 	     dc = TAILQ_NEXT(dc, dc_queue)) {
   1523 		if (dc->dc_dev == dev)
   1524 			panic("config_interrupts: deferred twice");
   1525 	}
   1526 #endif
   1527 
   1528 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
   1529 	if (dc == NULL)
   1530 		panic("config_interrupts: unable to allocate callback");
   1531 
   1532 	dc->dc_dev = dev;
   1533 	dc->dc_func = func;
   1534 	TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
   1535 	config_pending_incr();
   1536 }
   1537 
   1538 /*
   1539  * Process a deferred configuration queue.
   1540  */
   1541 static void
   1542 config_process_deferred(struct deferred_config_head *queue,
   1543     device_t parent)
   1544 {
   1545 	struct deferred_config *dc, *ndc;
   1546 
   1547 	for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
   1548 		ndc = TAILQ_NEXT(dc, dc_queue);
   1549 		if (parent == NULL || dc->dc_dev->dv_parent == parent) {
   1550 			TAILQ_REMOVE(queue, dc, dc_queue);
   1551 			(*dc->dc_func)(dc->dc_dev);
   1552 			free(dc, M_DEVBUF);
   1553 			config_pending_decr();
   1554 		}
   1555 	}
   1556 }
   1557 
   1558 /*
   1559  * Manipulate the config_pending semaphore.
   1560  */
   1561 void
   1562 config_pending_incr(void)
   1563 {
   1564 
   1565 	config_pending++;
   1566 }
   1567 
   1568 void
   1569 config_pending_decr(void)
   1570 {
   1571 
   1572 #ifdef DIAGNOSTIC
   1573 	if (config_pending == 0)
   1574 		panic("config_pending_decr: config_pending == 0");
   1575 #endif
   1576 	config_pending--;
   1577 	if (config_pending == 0)
   1578 		wakeup(&config_pending);
   1579 }
   1580 
   1581 /*
   1582  * Register a "finalization" routine.  Finalization routines are
   1583  * called iteratively once all real devices have been found during
   1584  * autoconfiguration, for as long as any one finalizer has done
   1585  * any work.
   1586  */
   1587 int
   1588 config_finalize_register(device_t dev, int (*fn)(device_t))
   1589 {
   1590 	struct finalize_hook *f;
   1591 
   1592 	/*
   1593 	 * If finalization has already been done, invoke the
   1594 	 * callback function now.
   1595 	 */
   1596 	if (config_finalize_done) {
   1597 		while ((*fn)(dev) != 0)
   1598 			/* loop */ ;
   1599 	}
   1600 
   1601 	/* Ensure this isn't already on the list. */
   1602 	TAILQ_FOREACH(f, &config_finalize_list, f_list) {
   1603 		if (f->f_func == fn && f->f_dev == dev)
   1604 			return (EEXIST);
   1605 	}
   1606 
   1607 	f = malloc(sizeof(*f), M_TEMP, M_WAITOK);
   1608 	f->f_func = fn;
   1609 	f->f_dev = dev;
   1610 	TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
   1611 
   1612 	return (0);
   1613 }
   1614 
   1615 void
   1616 config_finalize(void)
   1617 {
   1618 	struct finalize_hook *f;
   1619 	int rv;
   1620 
   1621 	/* Run the hooks until none of them does any work. */
   1622 	do {
   1623 		rv = 0;
   1624 		TAILQ_FOREACH(f, &config_finalize_list, f_list)
   1625 			rv |= (*f->f_func)(f->f_dev);
   1626 	} while (rv != 0);
   1627 
   1628 	config_finalize_done = 1;
   1629 
   1630 	/* Now free all the hooks. */
   1631 	while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
   1632 		TAILQ_REMOVE(&config_finalize_list, f, f_list);
   1633 		free(f, M_TEMP);
   1634 	}
   1635 }
   1636 
   1637 /*
   1638  * device_lookup:
   1639  *
   1640  *	Look up a device instance for a given driver.
   1641  */
   1642 void *
   1643 device_lookup(cfdriver_t cd, int unit)
   1644 {
   1645 
   1646 	if (unit < 0 || unit >= cd->cd_ndevs)
   1647 		return (NULL);
   1648 
   1649 	return (cd->cd_devs[unit]);
   1650 }
   1651 
   1652 /*
   1653  * Accessor functions for the device_t type.
   1654  */
   1655 devclass_t
   1656 device_class(device_t dev)
   1657 {
   1658 
   1659 	return (dev->dv_class);
   1660 }
   1661 
   1662 cfdata_t
   1663 device_cfdata(device_t dev)
   1664 {
   1665 
   1666 	return (dev->dv_cfdata);
   1667 }
   1668 
   1669 cfdriver_t
   1670 device_cfdriver(device_t dev)
   1671 {
   1672 
   1673 	return (dev->dv_cfdriver);
   1674 }
   1675 
   1676 cfattach_t
   1677 device_cfattach(device_t dev)
   1678 {
   1679 
   1680 	return (dev->dv_cfattach);
   1681 }
   1682 
   1683 int
   1684 device_unit(device_t dev)
   1685 {
   1686 
   1687 	return (dev->dv_unit);
   1688 }
   1689 
   1690 const char *
   1691 device_xname(device_t dev)
   1692 {
   1693 
   1694 	return (dev->dv_xname);
   1695 }
   1696 
   1697 device_t
   1698 device_parent(device_t dev)
   1699 {
   1700 
   1701 	return (dev->dv_parent);
   1702 }
   1703 
   1704 bool
   1705 device_is_active(device_t dev)
   1706 {
   1707 	int active_flags;
   1708 
   1709 	active_flags = DVF_ACTIVE;
   1710 	active_flags |= DVF_CLASS_SUSPENDED;
   1711 	active_flags |= DVF_DRIVER_SUSPENDED;
   1712 	active_flags |= DVF_BUS_SUSPENDED;
   1713 
   1714 	return ((dev->dv_flags & active_flags) == DVF_ACTIVE);
   1715 }
   1716 
   1717 bool
   1718 device_is_enabled(device_t dev)
   1719 {
   1720 	return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
   1721 }
   1722 
   1723 bool
   1724 device_has_power(device_t dev)
   1725 {
   1726 	int active_flags;
   1727 
   1728 	active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
   1729 
   1730 	return ((dev->dv_flags & active_flags) == DVF_ACTIVE);
   1731 }
   1732 
   1733 int
   1734 device_locator(device_t dev, u_int locnum)
   1735 {
   1736 
   1737 	KASSERT(dev->dv_locators != NULL);
   1738 	return (dev->dv_locators[locnum]);
   1739 }
   1740 
   1741 void *
   1742 device_private(device_t dev)
   1743 {
   1744 
   1745 	return (dev->dv_private);
   1746 }
   1747 
   1748 prop_dictionary_t
   1749 device_properties(device_t dev)
   1750 {
   1751 
   1752 	return (dev->dv_properties);
   1753 }
   1754 
   1755 /*
   1756  * device_is_a:
   1757  *
   1758  *	Returns true if the device is an instance of the specified
   1759  *	driver.
   1760  */
   1761 bool
   1762 device_is_a(device_t dev, const char *dname)
   1763 {
   1764 
   1765 	return (strcmp(dev->dv_cfdriver->cd_name, dname) == 0);
   1766 }
   1767 
   1768 /*
   1769  * Power management related functions.
   1770  */
   1771 
   1772 bool
   1773 device_pnp_is_registered(device_t dev)
   1774 {
   1775 	return (dev->dv_flags & DVF_POWER_HANDLERS) != 0;
   1776 }
   1777 
   1778 bool
   1779 device_pnp_driver_suspend(device_t dev)
   1780 {
   1781 	if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
   1782 		return true;
   1783 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
   1784 		return false;
   1785 	if (*dev->dv_driver_suspend != NULL &&
   1786 	    !(*dev->dv_driver_suspend)(dev))
   1787 		return false;
   1788 
   1789 	dev->dv_flags |= DVF_DRIVER_SUSPENDED;
   1790 	return true;
   1791 }
   1792 
   1793 bool
   1794 device_pnp_driver_resume(device_t dev)
   1795 {
   1796 	if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
   1797 		return true;
   1798 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
   1799 		return false;
   1800 	if (*dev->dv_driver_resume != NULL &&
   1801 	    !(*dev->dv_driver_resume)(dev))
   1802 		return false;
   1803 
   1804 	dev->dv_flags &= ~DVF_DRIVER_SUSPENDED;
   1805 	return true;
   1806 }
   1807 
   1808 void
   1809 device_pnp_driver_register(device_t dev,
   1810     bool (*suspend)(device_t), bool (*resume)(device_t))
   1811 {
   1812 	dev->dv_driver_suspend = suspend;
   1813 	dev->dv_driver_resume = resume;
   1814 	dev->dv_flags |= DVF_POWER_HANDLERS;
   1815 }
   1816 
   1817 void
   1818 device_pnp_driver_deregister(device_t dev)
   1819 {
   1820 	dev->dv_driver_suspend = NULL;
   1821 	dev->dv_driver_resume = NULL;
   1822 	dev->dv_flags &= ~DVF_POWER_HANDLERS;
   1823 }
   1824 
   1825 bool
   1826 device_pnp_driver_child_register(device_t dev)
   1827 {
   1828 	device_t parent = device_parent(dev);
   1829 
   1830 	if (parent == NULL || parent->dv_driver_child_register == NULL)
   1831 		return true;
   1832 	return (*parent->dv_driver_child_register)(dev);
   1833 }
   1834 
   1835 void
   1836 device_pnp_driver_set_child_register(device_t dev,
   1837     bool (*child_register)(device_t))
   1838 {
   1839 	dev->dv_driver_child_register = child_register;
   1840 }
   1841 
   1842 void *
   1843 device_pnp_bus_private(device_t dev)
   1844 {
   1845 	return dev->dv_bus_private;
   1846 }
   1847 
   1848 bool
   1849 device_pnp_bus_suspend(device_t dev)
   1850 {
   1851 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
   1852 		return true;
   1853 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 ||
   1854 	    (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
   1855 		return false;
   1856 	if (*dev->dv_bus_suspend != NULL &&
   1857 	    !(*dev->dv_bus_suspend)(dev))
   1858 		return false;
   1859 
   1860 	dev->dv_flags |= DVF_BUS_SUSPENDED;
   1861 	return true;
   1862 }
   1863 
   1864 bool
   1865 device_pnp_bus_resume(device_t dev)
   1866 {
   1867 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0)
   1868 		return true;
   1869 	if (*dev->dv_bus_resume != NULL &&
   1870 	    !(*dev->dv_bus_resume)(dev))
   1871 		return false;
   1872 
   1873 	dev->dv_flags &= ~DVF_BUS_SUSPENDED;
   1874 	return true;
   1875 }
   1876 
   1877 void
   1878 device_pnp_bus_register(device_t dev, void *priv,
   1879     bool (*suspend)(device_t), bool (*resume)(device_t),
   1880     void (*deregister)(device_t))
   1881 {
   1882 	dev->dv_bus_private = priv;
   1883 	dev->dv_bus_resume = resume;
   1884 	dev->dv_bus_suspend = suspend;
   1885 	dev->dv_bus_deregister = deregister;
   1886 }
   1887 
   1888 void
   1889 device_pnp_bus_deregister(device_t dev)
   1890 {
   1891 	if (dev->dv_bus_deregister == NULL)
   1892 		return;
   1893 	(*dev->dv_bus_deregister)(dev);
   1894 	dev->dv_bus_private = NULL;
   1895 	dev->dv_bus_suspend = NULL;
   1896 	dev->dv_bus_resume = NULL;
   1897 	dev->dv_bus_deregister = NULL;
   1898 }
   1899 
   1900 void *
   1901 device_pnp_class_private(device_t dev)
   1902 {
   1903 	return dev->dv_class_private;
   1904 }
   1905 
   1906 bool
   1907 device_pnp_class_suspend(device_t dev)
   1908 {
   1909 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0)
   1910 		return true;
   1911 	if (*dev->dv_class_suspend != NULL &&
   1912 	    !(*dev->dv_class_suspend)(dev))
   1913 		return false;
   1914 
   1915 	dev->dv_flags |= DVF_CLASS_SUSPENDED;
   1916 	return true;
   1917 }
   1918 
   1919 bool
   1920 device_pnp_class_resume(device_t dev)
   1921 {
   1922 	if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
   1923 		return true;
   1924 	if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 ||
   1925 	    (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
   1926 		return false;
   1927 	if (*dev->dv_class_resume != NULL &&
   1928 	    !(*dev->dv_class_resume)(dev))
   1929 		return false;
   1930 
   1931 	dev->dv_flags &= ~DVF_CLASS_SUSPENDED;
   1932 	return true;
   1933 }
   1934 
   1935 void
   1936 device_pnp_class_register(device_t dev, void *priv,
   1937     bool (*suspend)(device_t), bool (*resume)(device_t),
   1938     void (*deregister)(device_t))
   1939 {
   1940 	dev->dv_class_private = priv;
   1941 	dev->dv_class_suspend = suspend;
   1942 	dev->dv_class_resume = resume;
   1943 	dev->dv_class_deregister = deregister;
   1944 }
   1945 
   1946 void
   1947 device_pnp_class_deregister(device_t dev)
   1948 {
   1949 	if (dev->dv_class_deregister == NULL)
   1950 		return;
   1951 	(*dev->dv_class_deregister)(dev);
   1952 	dev->dv_class_private = NULL;
   1953 	dev->dv_class_suspend = NULL;
   1954 	dev->dv_class_resume = NULL;
   1955 	dev->dv_class_deregister = NULL;
   1956 }
   1957 
   1958 bool
   1959 device_active(device_t dev, devactive_t type)
   1960 {
   1961 	size_t i;
   1962 
   1963 	if (dev->dv_activity_count == 0)
   1964 		return false;
   1965 
   1966 	for (i = 0; i < dev->dv_activity_count; ++i)
   1967 		(*dev->dv_activity_handlers[i])(dev, type);
   1968 
   1969 	return true;
   1970 }
   1971 
   1972 bool
   1973 device_active_register(device_t dev, void (*handler)(device_t, devactive_t))
   1974 {
   1975 	void (**new_handlers)(device_t, devactive_t);
   1976 	void (**old_handlers)(device_t, devactive_t);
   1977 	size_t i, new_size;
   1978 	int s;
   1979 
   1980 	old_handlers = dev->dv_activity_handlers;
   1981 
   1982 	for (i = 0; i < dev->dv_activity_count; ++i) {
   1983 		if (old_handlers[i] == handler)
   1984 			panic("Double registering of idle handlers");
   1985 	}
   1986 
   1987 	new_size = dev->dv_activity_count + 1;
   1988 	new_handlers = malloc(sizeof(void *) * new_size, M_DEVBUF, M_WAITOK);
   1989 
   1990 	memcpy(new_handlers, old_handlers,
   1991 	    sizeof(void *) * dev->dv_activity_count);
   1992 	new_handlers[new_size - 1] = handler;
   1993 
   1994 	s = splhigh();
   1995 	dev->dv_activity_count = new_size;
   1996 	dev->dv_activity_handlers = new_handlers;
   1997 	splx(s);
   1998 
   1999 	if (old_handlers != NULL)
   2000 		free(old_handlers, M_DEVBUF);
   2001 
   2002 	return true;
   2003 }
   2004 
   2005 void
   2006 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t))
   2007 {
   2008 	void (**new_handlers)(device_t, devactive_t);
   2009 	void (**old_handlers)(device_t, devactive_t);
   2010 	size_t i, new_size;
   2011 	int s;
   2012 
   2013 	old_handlers = dev->dv_activity_handlers;
   2014 
   2015 	for (i = 0; i < dev->dv_activity_count; ++i) {
   2016 		if (old_handlers[i] == handler)
   2017 			break;
   2018 	}
   2019 
   2020 	if (i == dev->dv_activity_count)
   2021 		return; /* XXX panic? */
   2022 
   2023 	new_size = dev->dv_activity_count - 1;
   2024 
   2025 	if (new_size == 0) {
   2026 		new_handlers = NULL;
   2027 	} else {
   2028 		new_handlers = malloc(sizeof(void *) * new_size, M_DEVBUF,
   2029 		    M_WAITOK);
   2030 		memcpy(new_handlers, old_handlers, sizeof(void *) * i);
   2031 		memcpy(new_handlers + i, old_handlers + i + 1,
   2032 		    sizeof(void *) * (new_size - i));
   2033 	}
   2034 
   2035 	s = splhigh();
   2036 	dev->dv_activity_count = new_size;
   2037 	dev->dv_activity_handlers = new_handlers;
   2038 	splx(s);
   2039 
   2040 	free(old_handlers, M_DEVBUF);
   2041 }
   2042