Home | History | Annotate | Line # | Download | only in kern
subr_autoconf.c revision 1.62.6.2
      1 /* $NetBSD: subr_autoconf.c,v 1.62.6.2 2002/03/26 17:09:53 eeh Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2001-2002 Eduardo E. Horvath
      5  * Copyright (c) 1996, 2000 Christopher G. Demetriou
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *          This product includes software developed for the
     19  *          NetBSD Project.  See http://www.netbsd.org/ for
     20  *          information about NetBSD.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  *
     35  * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
     36  */
     37 
     38 /*
     39  * Copyright (c) 1992, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * This software was developed by the Computer Systems Engineering group
     43  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     44  * contributed to Berkeley.
     45  *
     46  * All advertising materials mentioning features or use of this software
     47  * must display the following acknowledgement:
     48  *	This product includes software developed by the University of
     49  *	California, Lawrence Berkeley Laboratories.
     50  *
     51  * Redistribution and use in source and binary forms, with or without
     52  * modification, are permitted provided that the following conditions
     53  * are met:
     54  * 1. Redistributions of source code must retain the above copyright
     55  *    notice, this list of conditions and the following disclaimer.
     56  * 2. Redistributions in binary form must reproduce the above copyright
     57  *    notice, this list of conditions and the following disclaimer in the
     58  *    documentation and/or other materials provided with the distribution.
     59  * 3. All advertising materials mentioning features or use of this software
     60  *    must display the following acknowledgement:
     61  *	This product includes software developed by the University of
     62  *	California, Berkeley and its contributors.
     63  * 4. Neither the name of the University nor the names of its contributors
     64  *    may be used to endorse or promote products derived from this software
     65  *    without specific prior written permission.
     66  *
     67  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     68  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     69  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     70  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     71  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     72  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     73  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     74  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     75  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     76  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     77  * SUCH DAMAGE.
     78  *
     79  * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp  (LBL)
     80  *
     81  *	@(#)subr_autoconf.c	8.3 (Berkeley) 5/17/94
     82  */
     83 
     84 #include <sys/cdefs.h>
     85 __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.62.6.2 2002/03/26 17:09:53 eeh Exp $");
     86 
     87 #include "opt_ddb.h"
     88 
     89 #include <sys/param.h>
     90 #include <sys/device.h>
     91 #include <sys/malloc.h>
     92 #include <sys/systm.h>
     93 #include <sys/kernel.h>
     94 #include <sys/errno.h>
     95 #include <sys/proc.h>
     96 #include <sys/properties.h>
     97 #include <machine/limits.h>
     98 
     99 #include "opt_userconf.h"
    100 #ifdef USERCONF
    101 #include <sys/userconf.h>
    102 #include <sys/reboot.h>
    103 #endif
    104 
    105 /*
    106  * Autoconfiguration subroutines.
    107  */
    108 
    109 static struct device *dev_create(struct device *, ssize_t, int);
    110 
    111 /*
    112  * ioconf.c exports exactly two names: cfdata and cfroots.  All system
    113  * devices and drivers are found via these tables.
    114  */
    115 extern struct cfdata cfdata[];
    116 extern short cfroots[];
    117 
    118 #define	ROOT ((struct device *)NULL)
    119 
    120 struct matchinfo {
    121 	cfmatch_t fn;
    122 	struct	device *parent;
    123 	void	*aux;
    124 	struct	cfdata *match;
    125 	int	pri;
    126 };
    127 
    128 static char *number(char *, int);
    129 static void mapply(struct matchinfo *, struct cfdata *, struct device *);
    130 static void locset(struct device *, struct cfdata *);
    131 static void locrm(struct device *, struct cfdata *);
    132 
    133 struct deferred_config {
    134 	TAILQ_ENTRY(deferred_config) dc_queue;
    135 	struct device *dc_dev;
    136 	void (*dc_func)(struct device *);
    137 };
    138 
    139 TAILQ_HEAD(deferred_config_head, deferred_config);
    140 
    141 struct deferred_config_head deferred_config_queue;
    142 struct deferred_config_head interrupt_config_queue;
    143 
    144 static void config_process_deferred(struct deferred_config_head *,
    145 	struct device *);
    146 
    147 /* list of all devices */
    148 struct devicelist alldevs;
    149 
    150 /* list of all events */
    151 struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
    152 
    153 __volatile int config_pending;		/* semaphore for mountroot */
    154 
    155 /* device properties database */
    156 static propdb_t devpropdb;
    157 
    158 /*
    159  * Configure the system's hardware.
    160  */
    161 void
    162 configure(void)
    163 {
    164 
    165 	TAILQ_INIT(&deferred_config_queue);
    166 	TAILQ_INIT(&interrupt_config_queue);
    167 	TAILQ_INIT(&alldevs);
    168 
    169 	devpropdb = propdb_create("devprop");
    170 
    171 #ifdef USERCONF
    172 	if (boothowto & RB_USERCONF)
    173 		user_config();
    174 #endif
    175 
    176 	/*
    177 	 * Do the machine-dependent portion of autoconfiguration.  This
    178 	 * sets the configuration machinery here in motion by "finding"
    179 	 * the root bus.  When this function returns, we expect interrupts
    180 	 * to be enabled.
    181 	 */
    182 	cpu_configure();
    183 
    184 	/*
    185 	 * Now that we've found all the hardware, start the real time
    186 	 * and statistics clocks.
    187 	 */
    188 	initclocks();
    189 
    190 	cold = 0;	/* clocks are running, we're warm now! */
    191 
    192 	/*
    193 	 * Now callback to finish configuration for devices which want
    194 	 * to do this once interrupts are enabled.
    195 	 */
    196 	config_process_deferred(&interrupt_config_queue, NULL);
    197 }
    198 
    199 /*
    200  * Set locators as properties on device node.
    201  */
    202 static void
    203 locset(struct device *d, struct cfdata *cf)
    204 {
    205 	int i;
    206 	int type = PROP_CONST|PROP_INT;
    207 	char buf[32];
    208 
    209 	dev_setprop(d, "cd-name", cf->cf_driver->cd_name,
    210 		strlen(cf->cf_driver->cd_name) + 1, PROP_STRING, (!cold));
    211 
    212 	for (i=0; cf->cf_locnames[i]; i++) {
    213 		sprintf(buf, "loc-%s", cf->cf_locnames[i]);
    214 		dev_setprop(d, buf, &cf->cf_loc[i],
    215 			    sizeof(int), type, (!cold));
    216 	}
    217 }
    218 
    219 /*
    220  * Remove locator properties from device node.
    221  */
    222 static void
    223 locrm(struct device *d, struct cfdata *cf)
    224 {
    225 	int i;
    226 	char buf[32];
    227 
    228 	dev_delprop(d, "cd-name");
    229 	for (i=0; cf->cf_locnames[i]; i++) {
    230 		sprintf(buf, "loc-%s", cf->cf_locnames[i]);
    231 		dev_delprop(d, buf);
    232 	}
    233 }
    234 
    235 /*
    236  * Apply the matching function and choose the best.  This is used
    237  * a few times and we want to keep the code small.
    238  */
    239 static void
    240 mapply(struct matchinfo *m, struct cfdata *cf, struct device *child)
    241 {
    242 	int pri;
    243 	void *aux = m->aux;
    244 
    245 	if (m->fn != NULL)
    246 		/* Someday the submatch function should use properties too. */
    247 		pri = (*m->fn)(m->parent, cf, m->aux);
    248 	else {
    249 		if ((ssize_t)cf->cf_attach->ca_devsize < 0) {
    250 			/* New-style device driver */
    251 			if (!child)
    252 				panic("mapply: no dev for new-style driver\n");
    253 			locset(child, cf);
    254 			child->dv_private = aux;
    255 			aux = child;
    256 		}
    257 
    258 		if (cf->cf_attach->ca_match == NULL) {
    259 			panic("mapply: no match function for '%s' device\n",
    260 				cf->cf_driver->cd_name);
    261 		}
    262 		pri = (*cf->cf_attach->ca_match)(m->parent, cf, aux);
    263 	}
    264 	if (pri > m->pri) {
    265 		m->match = cf;
    266 		m->pri = pri;
    267 	}
    268 	if (child) {
    269 		locrm(child, cf);
    270 		child->dv_private = NULL;
    271 	}
    272 }
    273 
    274 /*
    275  * Iterate over all potential children of some device, calling the given
    276  * function (default being the child's match function) for each one.
    277  * Nonzero returns are matches; the highest value returned is considered
    278  * the best match.  Return the `found child' if we got a match, or NULL
    279  * otherwise.  The `aux' pointer is simply passed on through.
    280  *
    281  * Note that this function is designed so that it can be used to apply
    282  * an arbitrary function to all potential children (its return value
    283  * can be ignored).
    284  */
    285 struct cfdata *
    286 config_search_ad(cfmatch_t fn, struct device *parent, void *aux,
    287 		 struct device *child)
    288 {
    289 	struct cfdata *cf;
    290 	short *p;
    291 	struct matchinfo m;
    292 
    293 	m.fn = fn;
    294 	m.parent = parent;
    295 	m.aux = aux;
    296 	m.match = NULL;
    297 	m.pri = 0;
    298 
    299 	for (cf = cfdata; cf->cf_driver; cf++) {
    300 		/*
    301 		 * Skip cf if no longer eligible, otherwise scan through
    302 		 * parents for one matching `parent', and try match function.
    303 		 */
    304 		if (cf->cf_fstate == FSTATE_FOUND)
    305 			continue;
    306 		for (p = cf->cf_parents; *p >= 0; p++)
    307 			if (parent->dv_cfdata == &cfdata[*p])
    308 				mapply(&m, cf, child);
    309 	}
    310 	return (m.match);
    311 }
    312 
    313 /*
    314  * Find the given root device.
    315  * This is much like config_search, but there is no parent.
    316  */
    317 struct cfdata *
    318 config_rootsearch_ad(cfmatch_t fn, const char *rootname, void *aux,
    319 		     struct device *root)
    320 {
    321 	struct cfdata *cf;
    322 	short *p;
    323 	struct matchinfo m;
    324 
    325 	m.fn = fn;
    326 	m.parent = ROOT;
    327 	m.aux = aux;
    328 	m.match = NULL;
    329 	m.pri = 0;
    330 	/*
    331 	 * Look at root entries for matching name.  We do not bother
    332 	 * with found-state here since only one root should ever be
    333 	 * searched (and it must be done first).
    334 	 */
    335 	for (p = cfroots; *p >= 0; p++) {
    336 		cf = &cfdata[*p];
    337 		if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
    338 			mapply(&m, cf, root);
    339 	}
    340 	return (m.match);
    341 }
    342 
    343 static const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
    344 
    345 /*
    346  * The given `aux' argument describes a device that has been found
    347  * on the given parent, but not necessarily configured.  Locate the
    348  * configuration data for that device (using the submatch function
    349  * provided, or using candidates' cd_match configuration driver
    350  * functions) and attach it, and return true.  If the device was
    351  * not configured, call the given `print' function and return 0.
    352  */
    353 struct device *
    354 config_found_sad(struct device *parent, void *aux, cfprint_t print,
    355     cfmatch_t submatch, struct device *d)
    356 {
    357 	struct cfdata *cf;
    358 
    359 	/*
    360 	 * Creating the dummy device here is really optional.  But
    361 	 * it allows old-style bus drivers to attach new-style children.
    362 	 */
    363 	if (!d) d = dev_create(ROOT, 0, cold ? M_NOWAIT : M_WAITOK);
    364 	if ((cf = config_search_ad(submatch, parent, aux, d)) != NULL)
    365 		return (config_attach_ad(parent, cf, aux, print, d));
    366 	if (print)
    367 		printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
    368 	return (NULL);
    369 }
    370 
    371 /*
    372  * As above, but for root devices.
    373  */
    374 struct device *
    375 config_rootfound(const char *rootname, void *aux)
    376 {
    377 	struct cfdata *cf;
    378 	struct device *root;
    379 
    380 	root = dev_create(ROOT, 0, cold ? M_NOWAIT : M_WAITOK);
    381 	if ((cf = config_rootsearch_ad((cfmatch_t)NULL, rootname, aux, root)) != NULL)
    382 		return (config_attach_ad(ROOT, cf, aux, (cfprint_t)NULL, root));
    383 	printf("root device %s not configured\n", rootname);
    384 	return (NULL);
    385 }
    386 
    387 /* just like sprintf(buf, "%d") except that it works from the end */
    388 static char *
    389 number(char *ep, int n)
    390 {
    391 
    392 	*--ep = 0;
    393 	while (n >= 10) {
    394 		*--ep = (n % 10) + '0';
    395 		n /= 10;
    396 	}
    397 	*--ep = n + '0';
    398 	return (ep);
    399 }
    400 
    401 /*
    402  * Allocate an empty device node.  If `parent' is provided then the
    403  * new node is attached to the tree under `parent'.  If `size'
    404  * is provided, then the new device node is allocated to be that
    405  * size.
    406  */
    407 static struct device *
    408 dev_create(struct device *parent, ssize_t size, int wait) {
    409 	struct device * dev;
    410 
    411 	/* get memory for all device vars */
    412 	if (size == 0)
    413 		size = sizeof(struct device);
    414 	dev = (struct device *)malloc(size, M_DEVBUF, wait);
    415 	if (!dev)
    416 	    panic("dev_create: memory allocation for device failed");
    417 	memset(dev, 0, size);
    418 	TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);	/* link up */
    419 	dev->dv_class = DV_EMPTY;
    420 	if (parent)
    421 		dev->dv_parent = parent;
    422 	return (dev);
    423 }
    424 
    425 /*
    426  * Expand the size of the cd_devs array if necessary.
    427  */
    428 void
    429 config_makeroom(int n, struct cfdriver *cd)
    430 {
    431 	int old, new;
    432 	void **nsp;
    433 
    434 	if (n < cd->cd_ndevs)
    435 		return;
    436 
    437 	/*
    438 	 * Need to expand the array.
    439 	 */
    440 	old = cd->cd_ndevs;
    441 	if (old == 0)
    442 		new = MINALLOCSIZE / sizeof(void *);
    443 	else
    444 		new = old * 2;
    445 	while (new <= n)
    446 		new *= 2;
    447 	cd->cd_ndevs = new;
    448 	nsp = malloc(new * sizeof(void *), M_DEVBUF,
    449 	    cold ? M_NOWAIT : M_WAITOK);
    450 	if (nsp == NULL)
    451 		panic("config_attach: %sing dev array",
    452 		    old != 0 ? "expand" : "creat");
    453 	memset(nsp + old, 0, (new - old) * sizeof(void *));
    454 	if (old != 0) {
    455 		memcpy(nsp, cd->cd_devs, old * sizeof(void *));
    456 		free(cd->cd_devs, M_DEVBUF);
    457 	}
    458 	cd->cd_devs = nsp;
    459 }
    460 
    461 /*
    462  * Attach a found device.  Allocates memory for device variables.
    463  */
    464 struct device *
    465 config_attach_ad(struct device *parent, struct cfdata *cf, void *aux,
    466 	cfprint_t print, struct device *propdev)
    467 {
    468 	struct device *dev;
    469 	struct cfdriver *cd;
    470 	struct cfattach *ca;
    471 	size_t lname, lunit;
    472 	ssize_t softsize;
    473 	const char *xunit;
    474 	int myunit;
    475 	char num[10];
    476 
    477 	cd = cf->cf_driver;
    478 	ca = cf->cf_attach;
    479 	softsize = ca->ca_devsize;
    480 
    481 #ifndef __BROKEN_CONFIG_UNIT_USAGE
    482 	if (cf->cf_fstate == FSTATE_STAR) {
    483 		for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
    484 			if (cd->cd_devs[myunit] == NULL)
    485 				break;
    486 		/*
    487 		 * myunit is now the unit of the first NULL device pointer,
    488 		 * or max(cd->cd_ndevs,cf->cf_unit).
    489 		 */
    490 	} else {
    491 		myunit = cf->cf_unit;
    492 #else /* __BROKEN_CONFIG_UNIT_USAGE */
    493 	myunit = cf->cf_unit;
    494 	if (cf->cf_fstate == FSTATE_STAR)
    495 		cf->cf_unit++;
    496 	else {
    497 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    498 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
    499 		cf->cf_fstate = FSTATE_FOUND;
    500 	}
    501 
    502 	/* compute length of name and decimal expansion of unit number */
    503 	lname = strlen(cd->cd_name);
    504 	xunit = number(&num[sizeof(num)], myunit);
    505 	lunit = &num[sizeof(num)] - xunit;
    506 	if (lname + lunit >= sizeof(dev->dv_xname))
    507 		panic("config_attach: device name too long");
    508 
    509 	/*
    510 	 * XXXXX We will continue to use the old-style device+softc in one
    511 	 * until all drivers are fixed to use DEV_PRIVATE() and not to refer
    512 	 * to sc->sc_dev.
    513 	 */
    514 	if (softsize < 0) softsize = -softsize;
    515 
    516 	/* get memory for all device vars */
    517 	if (softsize < 0) {
    518 		/* New-style device */
    519 		if (propdev) {
    520 			/* Great, we can use the propdev. */
    521 			dev = propdev;
    522 		} else {
    523 			dev = dev_create(parent, 0, cold ? M_NOWAIT : M_WAITOK);
    524 			if (!dev)
    525 				panic("config_attach: memory allocation for device failed");
    526 		}
    527 		if (softsize < 0) softsize = -softsize;
    528 		if (softsize < sizeof(struct device)) panic("config_attach");
    529 		dev->dv_private = malloc(softsize, M_DEVBUF,
    530 					 cold ? M_NOWAIT : M_WAITOK);
    531 		if (!dev->dv_private)
    532 			panic("config_attach: memory allocation for device softc failed");
    533 		memset(dev->dv_private, 0, softsize);
    534 		dev->dv_flags |= DVF_SOFTC;
    535 	} else {
    536 		dev = (struct device *)malloc(softsize, M_DEVBUF,
    537 					      cold ? M_NOWAIT : M_WAITOK);
    538 		if (!dev)
    539 			panic("config_attach: memory allocation for device softc failed");
    540 		memset(dev, 0, softsize);
    541 		TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);	/* link up */
    542 		dev->dv_private = dev;		/* Point private to ourself... */
    543 	}
    544 	dev->dv_class = cd->cd_class;
    545 	dev->dv_cfdata = cf;
    546 	dev->dv_unit = myunit;
    547 	memcpy(dev->dv_xname, cd->cd_name, lname);
    548 	memcpy(dev->dv_xname + lname, xunit, lunit);
    549 	dev->dv_parent = parent;
    550 	dev->dv_flags |= DVF_ACTIVE;	/* always initially active */
    551 	/* Set the locators once again. */
    552 	locset(dev, cf);
    553 	if (propdev && propdev != dev) {
    554 		/* Inherit (steal) properties from dummy device */
    555 		dev_copyprops(propdev, dev, (!cold));
    556 		dev_delprop(propdev, NULL);
    557 		/* destroy propdev */
    558 		config_detach(propdev, 0);
    559 	}
    560 
    561 	if (parent == ROOT)
    562 		printf("%s (root)", dev->dv_xname);
    563 	else {
    564 		printf("%s at %s", dev->dv_xname, parent->dv_xname);
    565 		if (print)
    566 			(void) (*print)(aux, NULL);
    567 	}
    568 
    569 	/* put this device in the devices array */
    570 	config_makeroom(dev->dv_unit, cd);
    571 	if (cd->cd_devs[dev->dv_unit])
    572 		panic("config_attach: duplicate %s", dev->dv_xname);
    573 	cd->cd_devs[dev->dv_unit] = dev;
    574 
    575 	/*
    576 	 * Before attaching, clobber any unfound devices that are
    577 	 * otherwise identical.
    578 	 */
    579 #ifdef __BROKEN_CONFIG_UNIT_USAGE
    580 	/* bump the unit number on all starred cfdata for this device. */
    581 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    582 	for (cf = cfdata; cf->cf_driver; cf++)
    583 		if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) {
    584 			if (cf->cf_fstate == FSTATE_NOTFOUND)
    585 				cf->cf_fstate = FSTATE_FOUND;
    586 #ifdef __BROKEN_CONFIG_UNIT_USAGE
    587 			if (cf->cf_fstate == FSTATE_STAR)
    588 				cf->cf_unit++;
    589 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    590 		}
    591 #ifdef __HAVE_DEVICE_REGISTER
    592 	device_register(dev, aux);
    593 #endif
    594 	(*ca->ca_attach)(parent, dev, aux);
    595 	config_process_deferred(&deferred_config_queue, dev);
    596 	return (dev);
    597 }
    598 
    599 /*
    600  * Detach a device.  Optionally forced (e.g. because of hardware
    601  * removal) and quiet.  Returns zero if successful, non-zero
    602  * (an error code) otherwise.
    603  *
    604  * Note that this code wants to be run from a process context, so
    605  * that the detach can sleep to allow processes which have a device
    606  * open to run and unwind their stacks.
    607  */
    608 int
    609 config_detach(struct device *dev, int flags)
    610 {
    611 	struct cfdata *cf;
    612 	struct cfattach *ca;
    613 	struct cfdriver *cd;
    614 #ifdef DIAGNOSTIC
    615 	struct device *d;
    616 #endif
    617 	int rv = 0, i;
    618 
    619 	if (dev->dv_class == DV_EMPTY) {
    620 		/*
    621 		 * The device is a dummy that never fully attached.
    622 		 * Simply remove it from the global list and free it.
    623 		 */
    624 		TAILQ_REMOVE(&alldevs, dev, dv_list);
    625 		dev_delprop(dev, NULL);
    626 		if (dev->dv_flags & DVF_SOFTC)
    627 			/* Separately allocated softc */
    628 			free(dev->dv_private, M_DEVBUF);
    629 		free(dev, M_DEVBUF);
    630 		return (0);
    631 	}
    632 
    633 	cf = dev->dv_cfdata;
    634 #ifdef DIAGNOSTIC
    635 	if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
    636 		panic("config_detach: bad device fstate");
    637 #endif
    638 	ca = cf->cf_attach;
    639 	cd = cf->cf_driver;
    640 
    641 	/*
    642 	 * Ensure the device is deactivated.  If the device doesn't
    643 	 * have an activation entry point, we allow DVF_ACTIVE to
    644 	 * remain set.  Otherwise, if DVF_ACTIVE is still set, the
    645 	 * device is busy, and the detach fails.
    646 	 */
    647 	if (ca->ca_activate != NULL)
    648 		rv = config_deactivate(dev);
    649 
    650 	/*
    651 	 * Try to detach the device.  If that's not possible, then
    652 	 * we either panic() (for the forced but failed case), or
    653 	 * return an error.
    654 	 */
    655 	if (rv == 0) {
    656 		if (ca->ca_detach != NULL)
    657 			rv = (*ca->ca_detach)(dev, flags);
    658 		else
    659 			rv = EOPNOTSUPP;
    660 	}
    661 	if (rv != 0) {
    662 		if ((flags & DETACH_FORCE) == 0)
    663 			return (rv);
    664 		else
    665 			panic("config_detach: forced detach of %s failed (%d)",
    666 			      dev->dv_xname, rv);
    667 	}
    668 
    669 	/*
    670 	 * The device has now been successfully detached.
    671 	 */
    672 
    673 #ifdef DIAGNOSTIC
    674 	/*
    675 	 * Sanity: If you're successfully detached, you should have no
    676 	 * children.  (Note that because children must be attached
    677 	 * after parents, we only need to search the latter part of
    678 	 * the list.)
    679 	 */
    680 	for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
    681 	     d = TAILQ_NEXT(d, dv_list)) {
    682 		if (d->dv_parent == dev) {
    683 			printf("config_detach: detached device %s"
    684 			       " has children %s\n", dev->dv_xname, d->dv_xname);
    685 			panic("config_detach");
    686 		}
    687 	}
    688 #endif
    689 
    690 	/*
    691 	 * Mark cfdata to show that the unit can be reused, if possible.
    692 	 */
    693 #ifdef __BROKEN_CONFIG_UNIT_USAGE
    694 	/*
    695 	 * Note that we can only re-use a starred unit number if the unit
    696 	 * being detached had the last assigned unit number.
    697 	 */
    698 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    699 	for (cf = cfdata; cf->cf_driver; cf++) {
    700 		if (cf->cf_driver == cd) {
    701 			if (cf->cf_fstate == FSTATE_FOUND &&
    702 			    cf->cf_unit == dev->dv_unit)
    703 				cf->cf_fstate = FSTATE_NOTFOUND;
    704 #ifdef __BROKEN_CONFIG_UNIT_USAGE
    705 			if (cf->cf_fstate == FSTATE_STAR &&
    706 			    cf->cf_unit == dev->dv_unit + 1)
    707 				cf->cf_unit--;
    708 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    709 		}
    710 	}
    711 
    712 	/*
    713 	 * Unlink from device list.
    714 	 */
    715 	TAILQ_REMOVE(&alldevs, dev, dv_list);
    716 
    717 	/*
    718 	 * Remove from cfdriver's array, tell the world, and free softc.
    719 	 */
    720 	if (cd) {
    721 		cd->cd_devs[dev->dv_unit] = NULL;
    722 		if ((flags & DETACH_QUIET) == 0)
    723 			printf("%s detached\n", dev->dv_xname);
    724 	}
    725 	dev_delprop(dev, NULL);
    726 	if (dev->dv_flags & DVF_SOFTC)
    727 		/* Separately allocated softc */
    728 		free(dev->dv_private, M_DEVBUF);
    729 	free(dev, M_DEVBUF);
    730 
    731 	/*
    732 	 * If the device now has no units in use, deallocate its softc array.
    733 	 */
    734 	for (i = 0; i < cd->cd_ndevs; i++)
    735 		if (cd->cd_devs[i] != NULL)
    736 			break;
    737 	if (i == cd->cd_ndevs) {		/* nothing found; deallocate */
    738 		free(cd->cd_devs, M_DEVBUF);
    739 		cd->cd_devs = NULL;
    740 		cd->cd_ndevs = 0;
    741 	}
    742 
    743 	/*
    744 	 * Return success.
    745 	 */
    746 	return (0);
    747 }
    748 
    749 int
    750 config_activate(struct device *dev)
    751 {
    752 	struct cfattach *ca = dev->dv_cfdata->cf_attach;
    753 	int rv = 0, oflags = dev->dv_flags;
    754 
    755 	if (ca->ca_activate == NULL)
    756 		return (EOPNOTSUPP);
    757 
    758 	if ((dev->dv_flags & DVF_ACTIVE) == 0) {
    759 		dev->dv_flags |= DVF_ACTIVE;
    760 		rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
    761 		if (rv)
    762 			dev->dv_flags = oflags;
    763 	}
    764 	return (rv);
    765 }
    766 
    767 int
    768 config_deactivate(struct device *dev)
    769 {
    770 	struct cfattach *ca = dev->dv_cfdata->cf_attach;
    771 	int rv = 0, oflags = dev->dv_flags;
    772 
    773 	if (ca->ca_activate == NULL)
    774 		return (EOPNOTSUPP);
    775 
    776 	if (dev->dv_flags & DVF_ACTIVE) {
    777 		dev->dv_flags &= ~DVF_ACTIVE;
    778 		rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
    779 		if (rv)
    780 			dev->dv_flags = oflags;
    781 	}
    782 	return (rv);
    783 }
    784 
    785 /*
    786  * Defer the configuration of the specified device until all
    787  * of its parent's devices have been attached.
    788  */
    789 void
    790 config_defer(struct device *dev, void (*func)(struct device *))
    791 {
    792 	struct deferred_config *dc;
    793 
    794 	if (dev->dv_parent == NULL)
    795 		panic("config_defer: can't defer config of a root device");
    796 
    797 #ifdef DIAGNOSTIC
    798 	for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
    799 	     dc = TAILQ_NEXT(dc, dc_queue)) {
    800 		if (dc->dc_dev == dev)
    801 			panic("config_defer: deferred twice");
    802 	}
    803 #endif
    804 
    805 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
    806 	if (dc == NULL)
    807 		panic("config_defer: unable to allocate callback");
    808 
    809 	dc->dc_dev = dev;
    810 	dc->dc_func = func;
    811 	TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
    812 	config_pending_incr();
    813 }
    814 
    815 /*
    816  * Defer some autoconfiguration for a device until after interrupts
    817  * are enabled.
    818  */
    819 void
    820 config_interrupts(struct device *dev, void (*func)(struct device *))
    821 {
    822 	struct deferred_config *dc;
    823 
    824 	/*
    825 	 * If interrupts are enabled, callback now.
    826 	 */
    827 	if (cold == 0) {
    828 		(*func)(dev);
    829 		return;
    830 	}
    831 
    832 #ifdef DIAGNOSTIC
    833 	for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
    834 	     dc = TAILQ_NEXT(dc, dc_queue)) {
    835 		if (dc->dc_dev == dev)
    836 			panic("config_interrupts: deferred twice");
    837 	}
    838 #endif
    839 
    840 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
    841 	if (dc == NULL)
    842 		panic("config_interrupts: unable to allocate callback");
    843 
    844 	dc->dc_dev = dev;
    845 	dc->dc_func = func;
    846 	TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
    847 	config_pending_incr();
    848 }
    849 
    850 /*
    851  * Process a deferred configuration queue.
    852  */
    853 static void
    854 config_process_deferred(struct deferred_config_head *queue,
    855     struct device *parent)
    856 {
    857 	struct deferred_config *dc, *ndc;
    858 
    859 	for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
    860 		ndc = TAILQ_NEXT(dc, dc_queue);
    861 		if (parent == NULL || dc->dc_dev->dv_parent == parent) {
    862 			TAILQ_REMOVE(queue, dc, dc_queue);
    863 			(*dc->dc_func)(dc->dc_dev);
    864 			free(dc, M_DEVBUF);
    865 			config_pending_decr();
    866 		}
    867 	}
    868 }
    869 
    870 /*
    871  * Manipulate the config_pending semaphore.
    872  */
    873 void
    874 config_pending_incr(void)
    875 {
    876 
    877 	config_pending++;
    878 }
    879 
    880 void
    881 config_pending_decr(void)
    882 {
    883 
    884 #ifdef DIAGNOSTIC
    885 	if (config_pending == 0)
    886 		panic("config_pending_decr: config_pending == 0");
    887 #endif
    888 	config_pending--;
    889 	if (config_pending == 0)
    890 		wakeup((void *)&config_pending);
    891 }
    892 
    893 /*
    894  * Attach a statically-initialized event.  The type and string pointers
    895  * are already set up.
    896  */
    897 void
    898 evcnt_attach_static(struct evcnt *ev)
    899 {
    900 	int len;
    901 
    902 	len = strlen(ev->ev_group);
    903 #ifdef DIAGNOSTIC
    904 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
    905 		panic("evcnt_attach_static: group length (%s)", ev->ev_group);
    906 #endif
    907 	ev->ev_grouplen = len;
    908 
    909 	len = strlen(ev->ev_name);
    910 #ifdef DIAGNOSTIC
    911 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
    912 		panic("evcnt_attach_static: name length (%s)", ev->ev_name);
    913 #endif
    914 	ev->ev_namelen = len;
    915 
    916 	TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
    917 }
    918 
    919 /*
    920  * Attach a dynamically-initialized event.  Zero it, set up the type
    921  * and string pointers and then act like it was statically initialized.
    922  */
    923 void
    924 evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
    925     const char *group, const char *name)
    926 {
    927 
    928 	memset(ev, 0, sizeof *ev);
    929 	ev->ev_type = type;
    930 	ev->ev_parent = parent;
    931 	ev->ev_group = group;
    932 	ev->ev_name = name;
    933 	evcnt_attach_static(ev);
    934 }
    935 
    936 /*
    937  * Detach an event.
    938  */
    939 void
    940 evcnt_detach(struct evcnt *ev)
    941 {
    942 
    943 	TAILQ_REMOVE(&allevents, ev, ev_list);
    944 }
    945 
    946 /*
    947  * Device property management routines.
    948  */
    949 #ifdef DEBUG
    950 int devprop_debug = 0;
    951 #endif
    952 
    953 /*
    954  * Create a dummy device you can attach properties to.
    955  */
    956 struct device *
    957 dev_config_create(struct device *parent, int wait)
    958 {
    959 	struct device *dev;
    960 
    961 	dev = dev_create(parent, 0, wait ? M_WAITOK : M_NOWAIT);
    962 	return (dev);
    963 }
    964 
    965 
    966 int
    967 dev_setprop(struct device *dev, const char *name, void *val,
    968 			size_t len, int type, int wait)
    969 {
    970 #ifdef DEBUG
    971 	if (devprop_debug)
    972 		printf("dev_setprop(%p, %s, %p %s, %ld, %x, %d)\n",
    973 		       dev, name, val, (PROP_TYPE(type) == PROP_STRING) ?
    974 		       (char *)val : "", len, type, wait);
    975 #endif
    976 
    977 	return (prop_set(devpropdb, dev, name, val, len, type, wait));
    978 }
    979 
    980 size_t
    981 dev_getprop(struct device *dev, const char *name, void *val,
    982 			size_t len, int *type, int search)
    983 {
    984 	ssize_t rv;
    985 
    986 #ifdef DEBUG
    987 	if (devprop_debug) {
    988 		printf("dev_getprop(%p, %s, %p, %ld, %p, %d)\n",
    989 		       dev, name, val, len, type, search);
    990 	}
    991 #endif
    992 	rv = prop_get(devpropdb, dev, name, val, len, type);
    993 	if (rv == -1) {
    994 		/* Not found -- try md_getprop */
    995 		rv = dev_mdgetprop(dev, name, val,
    996 			len, type);
    997 	}
    998 	if ((rv == -1) && search) {
    999 		if (dev->dv_parent)
   1000 			/* Tail recursion -- there should be no stack growth. */
   1001 			return (dev_getprop(dev->dv_parent, name,
   1002 					    val, len, type, search));
   1003 	}
   1004 #ifdef DEBUG
   1005 	if (devprop_debug && (rv == -1)) {
   1006 		printf("%s no found\n", name);
   1007 	}
   1008 #endif
   1009 	return (rv);
   1010 }
   1011 
   1012 int
   1013 dev_delprop(struct device *dev, const char *name)
   1014 {
   1015 
   1016 #ifdef DEBUG_N
   1017 	if (devprop_debug)
   1018 		printf("dev_delprop(%p, %s, %x)\n", dev, name);
   1019 #endif
   1020 	return (prop_delete(devpropdb, dev, name));
   1021 }
   1022 
   1023 
   1024 int
   1025 dev_copyprops(struct device *src, struct device *dest, int wait)
   1026 {
   1027 	return (prop_copy(devpropdb, src, dest, wait));
   1028 }
   1029 
   1030 #ifdef DDB
   1031 void
   1032 event_print(int full, void (*pr)(const char *, ...))
   1033 {
   1034 	struct evcnt *evp;
   1035 
   1036 	TAILQ_FOREACH(evp, &allevents, ev_list) {
   1037 		if (evp->ev_count == 0 && !full)
   1038 			continue;
   1039 
   1040 		(*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type,
   1041 		    evp->ev_group, evp->ev_name, evp->ev_count);
   1042 	}
   1043 }
   1044 #endif /* DDB */
   1045