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