Home | History | Annotate | Line # | Download | only in kern
subr_autoconf.c revision 1.60
      1 /* $NetBSD: subr_autoconf.c,v 1.60 2001/12/05 16:12:42 augustss 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. All advertising materials mentioning features or use of this software
     59  *    must display the following acknowledgement:
     60  *	This product includes software developed by the University of
     61  *	California, Berkeley and its contributors.
     62  * 4. Neither the name of the University nor the names of its contributors
     63  *    may be used to endorse or promote products derived from this software
     64  *    without specific prior written permission.
     65  *
     66  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     67  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     68  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     69  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     70  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     71  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     72  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     73  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     74  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     75  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     76  * SUCH DAMAGE.
     77  *
     78  * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp  (LBL)
     79  *
     80  *	@(#)subr_autoconf.c	8.3 (Berkeley) 5/17/94
     81  */
     82 
     83 #include <sys/cdefs.h>
     84 __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.60 2001/12/05 16:12:42 augustss Exp $");
     85 
     86 #include <sys/param.h>
     87 #include <sys/device.h>
     88 #include <sys/malloc.h>
     89 #include <sys/systm.h>
     90 #include <sys/kernel.h>
     91 #include <sys/errno.h>
     92 #include <sys/proc.h>
     93 #include <machine/limits.h>
     94 
     95 #include "opt_userconf.h"
     96 #ifdef USERCONF
     97 #include <sys/userconf.h>
     98 #include <sys/reboot.h>
     99 #endif
    100 
    101 /*
    102  * Autoconfiguration subroutines.
    103  */
    104 
    105 /*
    106  * ioconf.c exports exactly two names: cfdata and cfroots.  All system
    107  * devices and drivers are found via these tables.
    108  */
    109 extern struct cfdata cfdata[];
    110 extern short cfroots[];
    111 
    112 #define	ROOT ((struct device *)NULL)
    113 
    114 struct matchinfo {
    115 	cfmatch_t fn;
    116 	struct	device *parent;
    117 	void	*aux;
    118 	struct	cfdata *match;
    119 	int	pri;
    120 };
    121 
    122 static char *number(char *, int);
    123 static void mapply(struct matchinfo *, struct cfdata *);
    124 
    125 struct deferred_config {
    126 	TAILQ_ENTRY(deferred_config) dc_queue;
    127 	struct device *dc_dev;
    128 	void (*dc_func)(struct device *);
    129 };
    130 
    131 TAILQ_HEAD(deferred_config_head, deferred_config);
    132 
    133 struct deferred_config_head deferred_config_queue;
    134 struct deferred_config_head interrupt_config_queue;
    135 
    136 static void config_process_deferred(struct deferred_config_head *,
    137 	struct device *);
    138 
    139 /* list of all devices */
    140 struct devicelist alldevs;
    141 
    142 /* list of all events */
    143 struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
    144 
    145 __volatile int config_pending;		/* semaphore for mountroot */
    146 
    147 /*
    148  * Configure the system's hardware.
    149  */
    150 void
    151 configure(void)
    152 {
    153 
    154 	TAILQ_INIT(&deferred_config_queue);
    155 	TAILQ_INIT(&interrupt_config_queue);
    156 	TAILQ_INIT(&alldevs);
    157 
    158 #ifdef USERCONF
    159 	if (boothowto & RB_USERCONF)
    160 		user_config();
    161 #endif
    162 
    163 	/*
    164 	 * Do the machine-dependent portion of autoconfiguration.  This
    165 	 * sets the configuration machinery here in motion by "finding"
    166 	 * the root bus.  When this function returns, we expect interrupts
    167 	 * to be enabled.
    168 	 */
    169 	cpu_configure();
    170 
    171 	/*
    172 	 * Now that we've found all the hardware, start the real time
    173 	 * and statistics clocks.
    174 	 */
    175 	initclocks();
    176 
    177 	cold = 0;	/* clocks are running, we're warm now! */
    178 
    179 	/*
    180 	 * Now callback to finish configuration for devices which want
    181 	 * to do this once interrupts are enabled.
    182 	 */
    183 	config_process_deferred(&interrupt_config_queue, NULL);
    184 }
    185 
    186 /*
    187  * Apply the matching function and choose the best.  This is used
    188  * a few times and we want to keep the code small.
    189  */
    190 static void
    191 mapply(struct matchinfo *m, struct cfdata *cf)
    192 {
    193 	int pri;
    194 
    195 	if (m->fn != NULL)
    196 		pri = (*m->fn)(m->parent, cf, m->aux);
    197 	else {
    198 	        if (cf->cf_attach->ca_match == NULL) {
    199 			panic("mapply: no match function for '%s' device\n",
    200 			    cf->cf_driver->cd_name);
    201 		}
    202 		pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux);
    203 	}
    204 	if (pri > m->pri) {
    205 		m->match = cf;
    206 		m->pri = pri;
    207 	}
    208 }
    209 
    210 /*
    211  * Iterate over all potential children of some device, calling the given
    212  * function (default being the child's match function) for each one.
    213  * Nonzero returns are matches; the highest value returned is considered
    214  * the best match.  Return the `found child' if we got a match, or NULL
    215  * otherwise.  The `aux' pointer is simply passed on through.
    216  *
    217  * Note that this function is designed so that it can be used to apply
    218  * an arbitrary function to all potential children (its return value
    219  * can be ignored).
    220  */
    221 struct cfdata *
    222 config_search(cfmatch_t fn, struct device *parent, void *aux)
    223 {
    224 	struct cfdata *cf;
    225 	short *p;
    226 	struct matchinfo m;
    227 
    228 	m.fn = fn;
    229 	m.parent = parent;
    230 	m.aux = aux;
    231 	m.match = NULL;
    232 	m.pri = 0;
    233 	for (cf = cfdata; cf->cf_driver; cf++) {
    234 		/*
    235 		 * Skip cf if no longer eligible, otherwise scan through
    236 		 * parents for one matching `parent', and try match function.
    237 		 */
    238 		if (cf->cf_fstate == FSTATE_FOUND)
    239 			continue;
    240 		for (p = cf->cf_parents; *p >= 0; p++)
    241 			if (parent->dv_cfdata == &cfdata[*p])
    242 				mapply(&m, cf);
    243 	}
    244 	return (m.match);
    245 }
    246 
    247 /*
    248  * Find the given root device.
    249  * This is much like config_search, but there is no parent.
    250  */
    251 struct cfdata *
    252 config_rootsearch(cfmatch_t fn, const char *rootname, void *aux)
    253 {
    254 	struct cfdata *cf;
    255 	short *p;
    256 	struct matchinfo m;
    257 
    258 	m.fn = fn;
    259 	m.parent = ROOT;
    260 	m.aux = aux;
    261 	m.match = NULL;
    262 	m.pri = 0;
    263 	/*
    264 	 * Look at root entries for matching name.  We do not bother
    265 	 * with found-state here since only one root should ever be
    266 	 * searched (and it must be done first).
    267 	 */
    268 	for (p = cfroots; *p >= 0; p++) {
    269 		cf = &cfdata[*p];
    270 		if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
    271 			mapply(&m, cf);
    272 	}
    273 	return (m.match);
    274 }
    275 
    276 static const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
    277 
    278 /*
    279  * The given `aux' argument describes a device that has been found
    280  * on the given parent, but not necessarily configured.  Locate the
    281  * configuration data for that device (using the submatch function
    282  * provided, or using candidates' cd_match configuration driver
    283  * functions) and attach it, and return true.  If the device was
    284  * not configured, call the given `print' function and return 0.
    285  */
    286 struct device *
    287 config_found_sm(struct device *parent, void *aux, cfprint_t print,
    288     cfmatch_t submatch)
    289 {
    290 	struct cfdata *cf;
    291 
    292 	if ((cf = config_search(submatch, parent, aux)) != NULL)
    293 		return (config_attach(parent, cf, aux, print));
    294 	if (print)
    295 		printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
    296 	return (NULL);
    297 }
    298 
    299 /*
    300  * As above, but for root devices.
    301  */
    302 struct device *
    303 config_rootfound(const char *rootname, void *aux)
    304 {
    305 	struct cfdata *cf;
    306 
    307 	if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
    308 		return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
    309 	printf("root device %s not configured\n", rootname);
    310 	return (NULL);
    311 }
    312 
    313 /* just like sprintf(buf, "%d") except that it works from the end */
    314 static char *
    315 number(char *ep, int n)
    316 {
    317 
    318 	*--ep = 0;
    319 	while (n >= 10) {
    320 		*--ep = (n % 10) + '0';
    321 		n /= 10;
    322 	}
    323 	*--ep = n + '0';
    324 	return (ep);
    325 }
    326 
    327 /*
    328  * Expand the size of the cd_devs array if necessary.
    329  */
    330 void
    331 config_makeroom(int n, struct cfdriver *cd)
    332 {
    333 	int old, new;
    334 	void **nsp;
    335 
    336 	if (n < cd->cd_ndevs)
    337 		return;
    338 
    339 	/*
    340 	 * Need to expand the array.
    341 	 */
    342 	old = cd->cd_ndevs;
    343 	if (old == NULL)
    344 		new = MINALLOCSIZE / sizeof(void *);
    345 	else
    346 		new = old * 2;
    347 	while (new <= n)
    348 		new *= 2;
    349 	cd->cd_ndevs = new;
    350 	nsp = malloc(new * sizeof(void *), M_DEVBUF,
    351 	    cold ? M_NOWAIT : M_WAITOK);
    352 	if (nsp == NULL)
    353 		panic("config_attach: %sing dev array",
    354 		    old != 0 ? "expand" : "creat");
    355 	memset(nsp + old, 0, (new - old) * sizeof(void *));
    356 	if (old != NULL) {
    357 		memcpy(nsp, cd->cd_devs, old * sizeof(void *));
    358 		free(cd->cd_devs, M_DEVBUF);
    359 	}
    360 	cd->cd_devs = nsp;
    361 }
    362 
    363 /*
    364  * Attach a found device.  Allocates memory for device variables.
    365  */
    366 struct device *
    367 config_attach(struct device *parent, struct cfdata *cf, void *aux,
    368 	cfprint_t print)
    369 {
    370 	struct device *dev;
    371 	struct cfdriver *cd;
    372 	struct cfattach *ca;
    373 	size_t lname, lunit;
    374 	const char *xunit;
    375 	int myunit;
    376 	char num[10];
    377 
    378 	cd = cf->cf_driver;
    379 	ca = cf->cf_attach;
    380 	if (ca->ca_devsize < sizeof(struct device))
    381 		panic("config_attach");
    382 #ifndef __BROKEN_CONFIG_UNIT_USAGE
    383 	if (cf->cf_fstate == FSTATE_STAR) {
    384 		for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
    385 			if (cd->cd_devs[myunit] == NULL)
    386 				break;
    387 		/*
    388 		 * myunit is now the unit of the first NULL device pointer,
    389 		 * or max(cd->cd_ndevs,cf->cf_unit).
    390 		 */
    391 	} else {
    392 		myunit = cf->cf_unit;
    393 #else /* __BROKEN_CONFIG_UNIT_USAGE */
    394 	myunit = cf->cf_unit;
    395 	if (cf->cf_fstate == FSTATE_STAR)
    396 		cf->cf_unit++;
    397 	else {
    398 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    399 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
    400 		cf->cf_fstate = FSTATE_FOUND;
    401 	}
    402 
    403 	/* compute length of name and decimal expansion of unit number */
    404 	lname = strlen(cd->cd_name);
    405 	xunit = number(&num[sizeof(num)], myunit);
    406 	lunit = &num[sizeof(num)] - xunit;
    407 	if (lname + lunit >= sizeof(dev->dv_xname))
    408 		panic("config_attach: device name too long");
    409 
    410 	/* get memory for all device vars */
    411 	dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
    412 	    cold ? M_NOWAIT : M_WAITOK);
    413 	if (!dev)
    414 	    panic("config_attach: memory allocation for device softc failed");
    415 	memset(dev, 0, ca->ca_devsize);
    416 	TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);	/* link up */
    417 	dev->dv_class = cd->cd_class;
    418 	dev->dv_cfdata = cf;
    419 	dev->dv_unit = myunit;
    420 	memcpy(dev->dv_xname, cd->cd_name, lname);
    421 	memcpy(dev->dv_xname + lname, xunit, lunit);
    422 	dev->dv_parent = parent;
    423 	dev->dv_flags = DVF_ACTIVE;	/* always initially active */
    424 
    425 	if (parent == ROOT)
    426 		printf("%s (root)", dev->dv_xname);
    427 	else {
    428 		printf("%s at %s", dev->dv_xname, parent->dv_xname);
    429 		if (print)
    430 			(void) (*print)(aux, NULL);
    431 	}
    432 
    433 	/* put this device in the devices array */
    434 	config_makeroom(dev->dv_unit, cd);
    435 	if (cd->cd_devs[dev->dv_unit])
    436 		panic("config_attach: duplicate %s", dev->dv_xname);
    437 	cd->cd_devs[dev->dv_unit] = dev;
    438 
    439 	/*
    440 	 * Before attaching, clobber any unfound devices that are
    441 	 * otherwise identical.
    442 	 */
    443 #ifdef __BROKEN_CONFIG_UNIT_USAGE
    444 	/* bump the unit number on all starred cfdata for this device. */
    445 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    446 	for (cf = cfdata; cf->cf_driver; cf++)
    447 		if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) {
    448 			if (cf->cf_fstate == FSTATE_NOTFOUND)
    449 				cf->cf_fstate = FSTATE_FOUND;
    450 #ifdef __BROKEN_CONFIG_UNIT_USAGE
    451 			if (cf->cf_fstate == FSTATE_STAR)
    452 				cf->cf_unit++;
    453 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    454 		}
    455 #ifdef __HAVE_DEVICE_REGISTER
    456 	device_register(dev, aux);
    457 #endif
    458 	(*ca->ca_attach)(parent, dev, aux);
    459 	config_process_deferred(&deferred_config_queue, dev);
    460 	return (dev);
    461 }
    462 
    463 /*
    464  * Detach a device.  Optionally forced (e.g. because of hardware
    465  * removal) and quiet.  Returns zero if successful, non-zero
    466  * (an error code) otherwise.
    467  *
    468  * Note that this code wants to be run from a process context, so
    469  * that the detach can sleep to allow processes which have a device
    470  * open to run and unwind their stacks.
    471  */
    472 int
    473 config_detach(struct device *dev, int flags)
    474 {
    475 	struct cfdata *cf;
    476 	struct cfattach *ca;
    477 	struct cfdriver *cd;
    478 #ifdef DIAGNOSTIC
    479 	struct device *d;
    480 #endif
    481 	int rv = 0, i;
    482 
    483 	cf = dev->dv_cfdata;
    484 #ifdef DIAGNOSTIC
    485 	if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
    486 		panic("config_detach: bad device fstate");
    487 #endif
    488 	ca = cf->cf_attach;
    489 	cd = cf->cf_driver;
    490 
    491 	/*
    492 	 * Ensure the device is deactivated.  If the device doesn't
    493 	 * have an activation entry point, we allow DVF_ACTIVE to
    494 	 * remain set.  Otherwise, if DVF_ACTIVE is still set, the
    495 	 * device is busy, and the detach fails.
    496 	 */
    497 	if (ca->ca_activate != NULL)
    498 		rv = config_deactivate(dev);
    499 
    500 	/*
    501 	 * Try to detach the device.  If that's not possible, then
    502 	 * we either panic() (for the forced but failed case), or
    503 	 * return an error.
    504 	 */
    505 	if (rv == 0) {
    506 		if (ca->ca_detach != NULL)
    507 			rv = (*ca->ca_detach)(dev, flags);
    508 		else
    509 			rv = EOPNOTSUPP;
    510 	}
    511 	if (rv != 0) {
    512 		if ((flags & DETACH_FORCE) == 0)
    513 			return (rv);
    514 		else
    515 			panic("config_detach: forced detach of %s failed (%d)",
    516 			    dev->dv_xname, rv);
    517 	}
    518 
    519 	/*
    520 	 * The device has now been successfully detached.
    521 	 */
    522 
    523 #ifdef DIAGNOSTIC
    524 	/*
    525 	 * Sanity: If you're successfully detached, you should have no
    526 	 * children.  (Note that because children must be attached
    527 	 * after parents, we only need to search the latter part of
    528 	 * the list.)
    529 	 */
    530 	for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
    531 	    d = TAILQ_NEXT(d, dv_list)) {
    532 		if (d->dv_parent == dev) {
    533 			printf("config_detach: detached device %s"
    534 			    " has children %s\n", dev->dv_xname, d->dv_xname);
    535 			panic("config_detach");
    536 		}
    537 	}
    538 #endif
    539 
    540 	/*
    541 	 * Mark cfdata to show that the unit can be reused, if possible.
    542 	 */
    543 #ifdef __BROKEN_CONFIG_UNIT_USAGE
    544 	/*
    545 	 * Note that we can only re-use a starred unit number if the unit
    546 	 * being detached had the last assigned unit number.
    547 	 */
    548 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    549 	for (cf = cfdata; cf->cf_driver; cf++) {
    550 		if (cf->cf_driver == cd) {
    551 			if (cf->cf_fstate == FSTATE_FOUND &&
    552 			    cf->cf_unit == dev->dv_unit)
    553 				cf->cf_fstate = FSTATE_NOTFOUND;
    554 #ifdef __BROKEN_CONFIG_UNIT_USAGE
    555 			if (cf->cf_fstate == FSTATE_STAR &&
    556 			    cf->cf_unit == dev->dv_unit + 1)
    557 				cf->cf_unit--;
    558 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    559 		}
    560 	}
    561 
    562 	/*
    563 	 * Unlink from device list.
    564 	 */
    565 	TAILQ_REMOVE(&alldevs, dev, dv_list);
    566 
    567 	/*
    568 	 * Remove from cfdriver's array, tell the world, and free softc.
    569 	 */
    570 	cd->cd_devs[dev->dv_unit] = NULL;
    571 	if ((flags & DETACH_QUIET) == 0)
    572 		printf("%s detached\n", dev->dv_xname);
    573 	free(dev, M_DEVBUF);
    574 
    575 	/*
    576 	 * If the device now has no units in use, deallocate its softc array.
    577 	 */
    578 	for (i = 0; i < cd->cd_ndevs; i++)
    579 		if (cd->cd_devs[i] != NULL)
    580 			break;
    581 	if (i == cd->cd_ndevs) {		/* nothing found; deallocate */
    582 		free(cd->cd_devs, M_DEVBUF);
    583 		cd->cd_devs = NULL;
    584 		cd->cd_ndevs = 0;
    585 	}
    586 
    587 	/*
    588 	 * Return success.
    589 	 */
    590 	return (0);
    591 }
    592 
    593 int
    594 config_activate(struct device *dev)
    595 {
    596 	struct cfattach *ca = dev->dv_cfdata->cf_attach;
    597 	int rv = 0, oflags = dev->dv_flags;
    598 
    599 	if (ca->ca_activate == NULL)
    600 		return (EOPNOTSUPP);
    601 
    602 	if ((dev->dv_flags & DVF_ACTIVE) == 0) {
    603 		dev->dv_flags |= DVF_ACTIVE;
    604 		rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
    605 		if (rv)
    606 			dev->dv_flags = oflags;
    607 	}
    608 	return (rv);
    609 }
    610 
    611 int
    612 config_deactivate(struct device *dev)
    613 {
    614 	struct cfattach *ca = dev->dv_cfdata->cf_attach;
    615 	int rv = 0, oflags = dev->dv_flags;
    616 
    617 	if (ca->ca_activate == NULL)
    618 		return (EOPNOTSUPP);
    619 
    620 	if (dev->dv_flags & DVF_ACTIVE) {
    621 		dev->dv_flags &= ~DVF_ACTIVE;
    622 		rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
    623 		if (rv)
    624 			dev->dv_flags = oflags;
    625 	}
    626 	return (rv);
    627 }
    628 
    629 /*
    630  * Defer the configuration of the specified device until all
    631  * of its parent's devices have been attached.
    632  */
    633 void
    634 config_defer(struct device *dev, void (*func)(struct device *))
    635 {
    636 	struct deferred_config *dc;
    637 
    638 	if (dev->dv_parent == NULL)
    639 		panic("config_defer: can't defer config of a root device");
    640 
    641 #ifdef DIAGNOSTIC
    642 	for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
    643 	     dc = TAILQ_NEXT(dc, dc_queue)) {
    644 		if (dc->dc_dev == dev)
    645 			panic("config_defer: deferred twice");
    646 	}
    647 #endif
    648 
    649 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
    650 	if (dc == NULL)
    651 		panic("config_defer: unable to allocate callback");
    652 
    653 	dc->dc_dev = dev;
    654 	dc->dc_func = func;
    655 	TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
    656 	config_pending_incr();
    657 }
    658 
    659 /*
    660  * Defer some autoconfiguration for a device until after interrupts
    661  * are enabled.
    662  */
    663 void
    664 config_interrupts(struct device *dev, void (*func)(struct device *))
    665 {
    666 	struct deferred_config *dc;
    667 
    668 	/*
    669 	 * If interrupts are enabled, callback now.
    670 	 */
    671 	if (cold == 0) {
    672 		(*func)(dev);
    673 		return;
    674 	}
    675 
    676 #ifdef DIAGNOSTIC
    677 	for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
    678 	     dc = TAILQ_NEXT(dc, dc_queue)) {
    679 		if (dc->dc_dev == dev)
    680 			panic("config_interrupts: deferred twice");
    681 	}
    682 #endif
    683 
    684 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
    685 	if (dc == NULL)
    686 		panic("config_interrupts: unable to allocate callback");
    687 
    688 	dc->dc_dev = dev;
    689 	dc->dc_func = func;
    690 	TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
    691 	config_pending_incr();
    692 }
    693 
    694 /*
    695  * Process a deferred configuration queue.
    696  */
    697 static void
    698 config_process_deferred(struct deferred_config_head *queue,
    699     struct device *parent)
    700 {
    701 	struct deferred_config *dc, *ndc;
    702 
    703 	for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
    704 		ndc = TAILQ_NEXT(dc, dc_queue);
    705 		if (parent == NULL || dc->dc_dev->dv_parent == parent) {
    706 			TAILQ_REMOVE(queue, dc, dc_queue);
    707 			(*dc->dc_func)(dc->dc_dev);
    708 			free(dc, M_DEVBUF);
    709 			config_pending_decr();
    710 		}
    711 	}
    712 }
    713 
    714 /*
    715  * Manipulate the config_pending semaphore.
    716  */
    717 void
    718 config_pending_incr(void)
    719 {
    720 
    721 	config_pending++;
    722 }
    723 
    724 void
    725 config_pending_decr(void)
    726 {
    727 
    728 #ifdef DIAGNOSTIC
    729 	if (config_pending == 0)
    730 		panic("config_pending_decr: config_pending == 0");
    731 #endif
    732 	config_pending--;
    733 	if (config_pending == 0)
    734 		wakeup((void *)&config_pending);
    735 }
    736 
    737 /*
    738  * Attach a statically-initialized event.  The type and string pointers
    739  * are already set up.
    740  */
    741 void
    742 evcnt_attach_static(struct evcnt *ev)
    743 {
    744 	int len;
    745 
    746 	len = strlen(ev->ev_group);
    747 #ifdef DIAGNOSTIC
    748 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
    749 		panic("evcnt_attach_static: group length (%s)", ev->ev_group);
    750 #endif
    751 	ev->ev_grouplen = len;
    752 
    753 	len = strlen(ev->ev_name);
    754 #ifdef DIAGNOSTIC
    755 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
    756 		panic("evcnt_attach_static: name length (%s)", ev->ev_name);
    757 #endif
    758 	ev->ev_namelen = len;
    759 
    760 	TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
    761 }
    762 
    763 /*
    764  * Attach a dynamically-initialized event.  Zero it, set up the type
    765  * and string pointers and then act like it was statically initialized.
    766  */
    767 void
    768 evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
    769     const char *group, const char *name)
    770 {
    771 
    772 	memset(ev, 0, sizeof *ev);
    773 	ev->ev_type = type;
    774 	ev->ev_parent = parent;
    775 	ev->ev_group = group;
    776 	ev->ev_name = name;
    777 	evcnt_attach_static(ev);
    778 }
    779 
    780 /*
    781  * Detach an event.
    782  */
    783 void
    784 evcnt_detach(struct evcnt *ev)
    785 {
    786 
    787 	TAILQ_REMOVE(&allevents, ev, ev_list);
    788 }
    789