Home | History | Annotate | Line # | Download | only in kern
subr_autoconf.c revision 1.70
      1  1.70   thorpej /* $NetBSD: subr_autoconf.c,v 1.70 2002/09/27 06:12:55 thorpej 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.70   thorpej __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.70 2002/09/27 06:12:55 thorpej 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.16   mycroft #include <machine/limits.h>
     96   1.1     glass 
     97  1.57  gmcgarry #include "opt_userconf.h"
     98  1.57  gmcgarry #ifdef USERCONF
     99  1.57  gmcgarry #include <sys/userconf.h>
    100  1.57  gmcgarry #include <sys/reboot.h>
    101  1.57  gmcgarry #endif
    102  1.57  gmcgarry 
    103   1.1     glass /*
    104   1.1     glass  * Autoconfiguration subroutines.
    105   1.1     glass  */
    106   1.1     glass 
    107   1.1     glass /*
    108   1.1     glass  * ioconf.c exports exactly two names: cfdata and cfroots.  All system
    109   1.1     glass  * devices and drivers are found via these tables.
    110   1.1     glass  */
    111   1.1     glass extern struct cfdata cfdata[];
    112   1.1     glass extern short cfroots[];
    113   1.1     glass 
    114  1.65   thorpej /*
    115  1.67   thorpej  * List of all cfdriver structures.  We use this to detect duplicates
    116  1.67   thorpej  * when other cfdrivers are loaded.
    117  1.67   thorpej  */
    118  1.69   thorpej struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
    119  1.69   thorpej extern struct cfdriver * const cfdriver_list_initial[];
    120  1.67   thorpej 
    121  1.67   thorpej /*
    122  1.65   thorpej  * List of cfdata tables.  We always have one such list -- the one
    123  1.65   thorpej  * built statically when the kernel was configured.
    124  1.65   thorpej  */
    125  1.65   thorpej struct cftablelist allcftables;
    126  1.65   thorpej static struct cftable initcftable;
    127  1.65   thorpej 
    128   1.1     glass #define	ROOT ((struct device *)NULL)
    129   1.1     glass 
    130  1.16   mycroft struct matchinfo {
    131  1.16   mycroft 	cfmatch_t fn;
    132  1.16   mycroft 	struct	device *parent;
    133  1.25       cgd 	void	*aux;
    134  1.25       cgd 	struct	cfdata *match;
    135  1.25       cgd 	int	pri;
    136  1.16   mycroft };
    137  1.17  christos 
    138  1.51       cgd static char *number(char *, int);
    139  1.51       cgd static void mapply(struct matchinfo *, struct cfdata *);
    140  1.16   mycroft 
    141  1.29   thorpej struct deferred_config {
    142  1.29   thorpej 	TAILQ_ENTRY(deferred_config) dc_queue;
    143  1.29   thorpej 	struct device *dc_dev;
    144  1.51       cgd 	void (*dc_func)(struct device *);
    145  1.29   thorpej };
    146  1.29   thorpej 
    147  1.42   thorpej TAILQ_HEAD(deferred_config_head, deferred_config);
    148  1.29   thorpej 
    149  1.42   thorpej struct deferred_config_head deferred_config_queue;
    150  1.42   thorpej struct deferred_config_head interrupt_config_queue;
    151  1.42   thorpej 
    152  1.51       cgd static void config_process_deferred(struct deferred_config_head *,
    153  1.51       cgd 	struct device *);
    154  1.29   thorpej 
    155  1.56   thorpej /* list of all devices */
    156  1.56   thorpej struct devicelist alldevs;
    157  1.56   thorpej 
    158  1.56   thorpej /* list of all events */
    159  1.56   thorpej struct evcntlist allevents = TAILQ_HEAD_INITIALIZER(allevents);
    160  1.20       cgd 
    161  1.47   thorpej __volatile int config_pending;		/* semaphore for mountroot */
    162  1.47   thorpej 
    163  1.67   thorpej #define	STREQ(s1, s2)			\
    164  1.70   thorpej 	(*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
    165  1.67   thorpej 
    166  1.20       cgd /*
    167  1.41   thorpej  * Configure the system's hardware.
    168  1.20       cgd  */
    169  1.20       cgd void
    170  1.51       cgd configure(void)
    171  1.20       cgd {
    172  1.67   thorpej 	int i;
    173  1.67   thorpej 
    174  1.69   thorpej 	/* allcfdrivers is statically initialized. */
    175  1.67   thorpej 	for (i = 0; cfdriver_list_initial[i] != NULL; i++)
    176  1.67   thorpej 		if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0)
    177  1.67   thorpej 			panic("configure: duplicate `%s' drivers",
    178  1.67   thorpej 			    cfdriver_list_initial[i]->cd_name);
    179  1.20       cgd 
    180  1.65   thorpej 	TAILQ_INIT(&allcftables);
    181  1.65   thorpej 	initcftable.ct_cfdata = cfdata;
    182  1.65   thorpej 	TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
    183  1.65   thorpej 
    184  1.29   thorpej 	TAILQ_INIT(&deferred_config_queue);
    185  1.42   thorpej 	TAILQ_INIT(&interrupt_config_queue);
    186  1.41   thorpej 	TAILQ_INIT(&alldevs);
    187  1.57  gmcgarry 
    188  1.57  gmcgarry #ifdef USERCONF
    189  1.57  gmcgarry 	if (boothowto & RB_USERCONF)
    190  1.57  gmcgarry 		user_config();
    191  1.57  gmcgarry #endif
    192  1.41   thorpej 
    193  1.41   thorpej 	/*
    194  1.41   thorpej 	 * Do the machine-dependent portion of autoconfiguration.  This
    195  1.41   thorpej 	 * sets the configuration machinery here in motion by "finding"
    196  1.41   thorpej 	 * the root bus.  When this function returns, we expect interrupts
    197  1.41   thorpej 	 * to be enabled.
    198  1.41   thorpej 	 */
    199  1.41   thorpej 	cpu_configure();
    200  1.43   thorpej 
    201  1.43   thorpej 	/*
    202  1.43   thorpej 	 * Now that we've found all the hardware, start the real time
    203  1.43   thorpej 	 * and statistics clocks.
    204  1.43   thorpej 	 */
    205  1.43   thorpej 	initclocks();
    206  1.43   thorpej 
    207  1.43   thorpej 	cold = 0;	/* clocks are running, we're warm now! */
    208  1.42   thorpej 
    209  1.42   thorpej 	/*
    210  1.42   thorpej 	 * Now callback to finish configuration for devices which want
    211  1.42   thorpej 	 * to do this once interrupts are enabled.
    212  1.42   thorpej 	 */
    213  1.42   thorpej 	config_process_deferred(&interrupt_config_queue, NULL);
    214  1.20       cgd }
    215  1.20       cgd 
    216   1.1     glass /*
    217  1.67   thorpej  * Add a cfdriver to the system.
    218  1.67   thorpej  */
    219  1.67   thorpej int
    220  1.67   thorpej config_cfdriver_attach(struct cfdriver *cd)
    221  1.67   thorpej {
    222  1.67   thorpej 	struct cfdriver *lcd;
    223  1.67   thorpej 
    224  1.67   thorpej 	/* Make sure this driver isn't already in the system. */
    225  1.67   thorpej 	LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
    226  1.67   thorpej 		if (STREQ(lcd->cd_name, cd->cd_name))
    227  1.67   thorpej 			return (EEXIST);
    228  1.67   thorpej 	}
    229  1.67   thorpej 
    230  1.67   thorpej 	LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
    231  1.67   thorpej 
    232  1.67   thorpej 	return (0);
    233  1.67   thorpej }
    234  1.67   thorpej 
    235  1.67   thorpej /*
    236  1.67   thorpej  * Remove a cfdriver from the system.
    237  1.67   thorpej  */
    238  1.67   thorpej int
    239  1.67   thorpej config_cfdriver_detach(struct cfdriver *cd)
    240  1.67   thorpej {
    241  1.67   thorpej 	int i;
    242  1.67   thorpej 
    243  1.67   thorpej 	/* Make sure there are no active instances. */
    244  1.67   thorpej 	for (i = 0; i < cd->cd_ndevs; i++) {
    245  1.67   thorpej 		if (cd->cd_devs[i] != NULL)
    246  1.67   thorpej 			return (EBUSY);
    247  1.67   thorpej 	}
    248  1.67   thorpej 
    249  1.67   thorpej 	LIST_REMOVE(cd, cd_list);
    250  1.67   thorpej 
    251  1.67   thorpej 	KASSERT(cd->cd_devs == NULL);
    252  1.67   thorpej 
    253  1.67   thorpej 	return (0);
    254  1.67   thorpej }
    255  1.67   thorpej 
    256  1.67   thorpej /*
    257  1.67   thorpej  * Look up a cfdriver by name.
    258  1.67   thorpej  */
    259  1.67   thorpej static struct cfdriver *
    260  1.67   thorpej config_cfdriver_lookup(const char *name)
    261  1.67   thorpej {
    262  1.67   thorpej 	struct cfdriver *cd;
    263  1.69   thorpej 
    264  1.69   thorpej 	/*
    265  1.69   thorpej 	 * It is sometimes necessary to use the autoconfiguration
    266  1.69   thorpej 	 * framework quite early (e.g. to initialize the console).
    267  1.69   thorpej 	 * We support this by noticing an empty cfdriver list and
    268  1.69   thorpej 	 * searching the initial static list instead.
    269  1.69   thorpej 	 */
    270  1.69   thorpej 	if (LIST_EMPTY(&allcfdrivers)) {
    271  1.69   thorpej 		int i;
    272  1.69   thorpej 
    273  1.69   thorpej 		for (i = 0; cfdriver_list_initial[i] != NULL; i++) {
    274  1.69   thorpej 			if (STREQ(cfdriver_list_initial[i]->cd_name, name))
    275  1.69   thorpej 				return (cfdriver_list_initial[i]);
    276  1.69   thorpej 		}
    277  1.69   thorpej 	}
    278  1.67   thorpej 
    279  1.67   thorpej 	LIST_FOREACH(cd, &allcfdrivers, cd_list) {
    280  1.67   thorpej 		if (STREQ(cd->cd_name, name))
    281  1.67   thorpej 			return (cd);
    282  1.67   thorpej 	}
    283  1.67   thorpej 
    284  1.67   thorpej 	return (NULL);
    285  1.67   thorpej }
    286  1.67   thorpej 
    287  1.67   thorpej /*
    288   1.1     glass  * Apply the matching function and choose the best.  This is used
    289   1.1     glass  * a few times and we want to keep the code small.
    290   1.1     glass  */
    291  1.16   mycroft static void
    292  1.51       cgd mapply(struct matchinfo *m, struct cfdata *cf)
    293   1.1     glass {
    294  1.50  augustss 	int pri;
    295   1.1     glass 
    296   1.1     glass 	if (m->fn != NULL)
    297  1.25       cgd 		pri = (*m->fn)(m->parent, cf, m->aux);
    298   1.3     glass 	else {
    299  1.19   thorpej 	        if (cf->cf_attach->ca_match == NULL) {
    300  1.16   mycroft 			panic("mapply: no match function for '%s' device\n",
    301  1.67   thorpej 			    cf->cf_name);
    302   1.3     glass 		}
    303  1.25       cgd 		pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux);
    304   1.3     glass 	}
    305   1.1     glass 	if (pri > m->pri) {
    306  1.25       cgd 		m->match = cf;
    307   1.1     glass 		m->pri = pri;
    308   1.1     glass 	}
    309   1.1     glass }
    310   1.1     glass 
    311   1.1     glass /*
    312  1.66   thorpej  * Determine if `parent' is a potential parent for a device spec based
    313  1.66   thorpej  * on `cfp'.
    314  1.66   thorpej  */
    315  1.66   thorpej static int
    316  1.66   thorpej cfparent_match(struct device *parent, const struct cfparent *cfp)
    317  1.66   thorpej {
    318  1.67   thorpej 	struct cfdriver *pcd;
    319  1.66   thorpej 	const char * const *cpp;
    320  1.66   thorpej 	const char *cp;
    321  1.70   thorpej 
    322  1.70   thorpej 	/* We don't match root nodes here. */
    323  1.70   thorpej 	if (cfp == NULL)
    324  1.70   thorpej 		return (0);
    325  1.66   thorpej 
    326  1.67   thorpej 	pcd = config_cfdriver_lookup(parent->dv_cfdata->cf_name);
    327  1.67   thorpej 	KASSERT(pcd != NULL);
    328  1.67   thorpej 
    329  1.66   thorpej 	/*
    330  1.66   thorpej 	 * First, ensure this parent has the correct interface
    331  1.66   thorpej 	 * attribute.
    332  1.66   thorpej 	 */
    333  1.66   thorpej 	if (pcd->cd_attrs == NULL)
    334  1.66   thorpej 		return (0);	/* no interface attributes -> no children */
    335  1.66   thorpej 	for (cpp = pcd->cd_attrs; (cp = *cpp) != NULL; cpp++) {
    336  1.67   thorpej 		if (STREQ(cp, cfp->cfp_iattr)) {
    337  1.66   thorpej 			/* Match. */
    338  1.66   thorpej 			break;
    339  1.66   thorpej 		}
    340  1.66   thorpej 	}
    341  1.66   thorpej 	if (cp == NULL)
    342  1.66   thorpej 		return (0);	/* doesn't carry the req'd attribute */
    343  1.66   thorpej 
    344  1.66   thorpej 	/*
    345  1.66   thorpej 	 * If no specific parent device instance was specified (i.e.
    346  1.66   thorpej 	 * we're attaching to the attribute only), we're done!
    347  1.66   thorpej 	 */
    348  1.66   thorpej 	if (cfp->cfp_parent == NULL)
    349  1.66   thorpej 		return (1);
    350  1.66   thorpej 
    351  1.66   thorpej 	/*
    352  1.66   thorpej 	 * Check the parent device's name.
    353  1.66   thorpej 	 */
    354  1.66   thorpej 	if (pcd->cd_name[0] != cfp->cfp_parent[0] ||
    355  1.66   thorpej 	    strcmp(pcd->cd_name, cfp->cfp_parent) != 0)
    356  1.66   thorpej 		return (0);	/* not the same parent */
    357  1.66   thorpej 
    358  1.66   thorpej 	/*
    359  1.66   thorpej 	 * Make sure the unit number matches.
    360  1.66   thorpej 	 */
    361  1.66   thorpej 	if (cfp->cfp_unit == -1 ||	/* wildcard */
    362  1.66   thorpej 	    cfp->cfp_unit == parent->dv_unit)
    363  1.66   thorpej 		return (1);
    364  1.66   thorpej 
    365  1.66   thorpej 	/* Unit numbers don't match. */
    366  1.66   thorpej 	return (0);
    367  1.68   thorpej }
    368  1.68   thorpej 
    369  1.68   thorpej /*
    370  1.68   thorpej  * Invoke the "match" routine for a cfdata entry on behalf of
    371  1.68   thorpej  * an external caller, usually a "submatch" routine.
    372  1.68   thorpej  */
    373  1.68   thorpej int
    374  1.68   thorpej config_match(struct device *parent, struct cfdata *cf, void *aux)
    375  1.68   thorpej {
    376  1.68   thorpej 
    377  1.68   thorpej 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    378  1.66   thorpej }
    379  1.66   thorpej 
    380  1.66   thorpej /*
    381   1.1     glass  * Iterate over all potential children of some device, calling the given
    382   1.1     glass  * function (default being the child's match function) for each one.
    383   1.1     glass  * Nonzero returns are matches; the highest value returned is considered
    384   1.1     glass  * the best match.  Return the `found child' if we got a match, or NULL
    385   1.1     glass  * otherwise.  The `aux' pointer is simply passed on through.
    386   1.1     glass  *
    387   1.1     glass  * Note that this function is designed so that it can be used to apply
    388   1.1     glass  * an arbitrary function to all potential children (its return value
    389   1.1     glass  * can be ignored).
    390   1.1     glass  */
    391  1.25       cgd struct cfdata *
    392  1.51       cgd config_search(cfmatch_t fn, struct device *parent, void *aux)
    393   1.1     glass {
    394  1.65   thorpej 	struct cftable *ct;
    395  1.50  augustss 	struct cfdata *cf;
    396   1.1     glass 	struct matchinfo m;
    397   1.1     glass 
    398   1.1     glass 	m.fn = fn;
    399   1.1     glass 	m.parent = parent;
    400  1.25       cgd 	m.aux = aux;
    401  1.14   mycroft 	m.match = NULL;
    402   1.1     glass 	m.pri = 0;
    403  1.65   thorpej 
    404  1.65   thorpej 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
    405  1.67   thorpej 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
    406  1.65   thorpej 			/*
    407  1.65   thorpej 			 * Skip cf if no longer eligible, otherwise scan
    408  1.65   thorpej 			 * through parents for one matching `parent', and
    409  1.65   thorpej 			 * try match function.
    410  1.65   thorpej 			 */
    411  1.65   thorpej 			if (cf->cf_fstate == FSTATE_FOUND)
    412  1.65   thorpej 				continue;
    413  1.65   thorpej 			if (cf->cf_fstate == FSTATE_DNOTFOUND ||
    414  1.65   thorpej 			    cf->cf_fstate == FSTATE_DSTAR)
    415  1.65   thorpej 				continue;
    416  1.66   thorpej 			if (cfparent_match(parent, cf->cf_pspec))
    417  1.66   thorpej 				mapply(&m, cf);
    418  1.65   thorpej 		}
    419   1.1     glass 	}
    420   1.1     glass 	return (m.match);
    421   1.1     glass }
    422   1.1     glass 
    423  1.16   mycroft /*
    424   1.1     glass  * Find the given root device.
    425   1.1     glass  * This is much like config_search, but there is no parent.
    426  1.65   thorpej  * Don't bother with multiple cfdata tables; the root node
    427  1.65   thorpej  * must always be in the initial table.
    428   1.1     glass  */
    429  1.25       cgd struct cfdata *
    430  1.52       cgd config_rootsearch(cfmatch_t fn, const char *rootname, void *aux)
    431   1.1     glass {
    432  1.50  augustss 	struct cfdata *cf;
    433  1.50  augustss 	short *p;
    434   1.1     glass 	struct matchinfo m;
    435   1.1     glass 
    436   1.1     glass 	m.fn = fn;
    437   1.1     glass 	m.parent = ROOT;
    438  1.25       cgd 	m.aux = aux;
    439  1.14   mycroft 	m.match = NULL;
    440   1.1     glass 	m.pri = 0;
    441   1.1     glass 	/*
    442   1.1     glass 	 * Look at root entries for matching name.  We do not bother
    443   1.1     glass 	 * with found-state here since only one root should ever be
    444   1.1     glass 	 * searched (and it must be done first).
    445   1.1     glass 	 */
    446   1.1     glass 	for (p = cfroots; *p >= 0; p++) {
    447   1.1     glass 		cf = &cfdata[*p];
    448  1.67   thorpej 		if (strcmp(cf->cf_name, rootname) == 0)
    449  1.16   mycroft 			mapply(&m, cf);
    450   1.1     glass 	}
    451   1.1     glass 	return (m.match);
    452   1.1     glass }
    453   1.1     glass 
    454  1.52       cgd static const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
    455   1.1     glass 
    456   1.1     glass /*
    457   1.1     glass  * The given `aux' argument describes a device that has been found
    458   1.1     glass  * on the given parent, but not necessarily configured.  Locate the
    459  1.18       cgd  * configuration data for that device (using the submatch function
    460  1.18       cgd  * provided, or using candidates' cd_match configuration driver
    461  1.18       cgd  * functions) and attach it, and return true.  If the device was
    462   1.1     glass  * not configured, call the given `print' function and return 0.
    463   1.1     glass  */
    464  1.21       cgd struct device *
    465  1.51       cgd config_found_sm(struct device *parent, void *aux, cfprint_t print,
    466  1.51       cgd     cfmatch_t submatch)
    467   1.1     glass {
    468  1.25       cgd 	struct cfdata *cf;
    469  1.25       cgd 
    470  1.25       cgd 	if ((cf = config_search(submatch, parent, aux)) != NULL)
    471  1.25       cgd 		return (config_attach(parent, cf, aux, print));
    472   1.3     glass 	if (print)
    473  1.55  sommerfe 		printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
    474  1.21       cgd 	return (NULL);
    475   1.1     glass }
    476   1.1     glass 
    477   1.1     glass /*
    478   1.1     glass  * As above, but for root devices.
    479   1.1     glass  */
    480  1.21       cgd struct device *
    481  1.52       cgd config_rootfound(const char *rootname, void *aux)
    482   1.1     glass {
    483  1.25       cgd 	struct cfdata *cf;
    484  1.25       cgd 
    485  1.25       cgd 	if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
    486  1.25       cgd 		return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
    487  1.24  christos 	printf("root device %s not configured\n", rootname);
    488  1.21       cgd 	return (NULL);
    489   1.1     glass }
    490   1.1     glass 
    491   1.1     glass /* just like sprintf(buf, "%d") except that it works from the end */
    492   1.1     glass static char *
    493  1.51       cgd number(char *ep, int n)
    494   1.1     glass {
    495   1.1     glass 
    496   1.1     glass 	*--ep = 0;
    497   1.1     glass 	while (n >= 10) {
    498   1.1     glass 		*--ep = (n % 10) + '0';
    499   1.1     glass 		n /= 10;
    500   1.1     glass 	}
    501   1.1     glass 	*--ep = n + '0';
    502   1.1     glass 	return (ep);
    503   1.1     glass }
    504   1.1     glass 
    505   1.1     glass /*
    506  1.59  augustss  * Expand the size of the cd_devs array if necessary.
    507  1.59  augustss  */
    508  1.59  augustss void
    509  1.59  augustss config_makeroom(int n, struct cfdriver *cd)
    510  1.59  augustss {
    511  1.59  augustss 	int old, new;
    512  1.59  augustss 	void **nsp;
    513  1.59  augustss 
    514  1.59  augustss 	if (n < cd->cd_ndevs)
    515  1.59  augustss 		return;
    516  1.59  augustss 
    517  1.59  augustss 	/*
    518  1.59  augustss 	 * Need to expand the array.
    519  1.59  augustss 	 */
    520  1.59  augustss 	old = cd->cd_ndevs;
    521  1.61   thorpej 	if (old == 0)
    522  1.59  augustss 		new = MINALLOCSIZE / sizeof(void *);
    523  1.59  augustss 	else
    524  1.59  augustss 		new = old * 2;
    525  1.59  augustss 	while (new <= n)
    526  1.59  augustss 		new *= 2;
    527  1.59  augustss 	cd->cd_ndevs = new;
    528  1.59  augustss 	nsp = malloc(new * sizeof(void *), M_DEVBUF,
    529  1.59  augustss 	    cold ? M_NOWAIT : M_WAITOK);
    530  1.60  augustss 	if (nsp == NULL)
    531  1.59  augustss 		panic("config_attach: %sing dev array",
    532  1.59  augustss 		    old != 0 ? "expand" : "creat");
    533  1.59  augustss 	memset(nsp + old, 0, (new - old) * sizeof(void *));
    534  1.61   thorpej 	if (old != 0) {
    535  1.59  augustss 		memcpy(nsp, cd->cd_devs, old * sizeof(void *));
    536  1.59  augustss 		free(cd->cd_devs, M_DEVBUF);
    537  1.59  augustss 	}
    538  1.59  augustss 	cd->cd_devs = nsp;
    539  1.59  augustss }
    540  1.59  augustss 
    541  1.59  augustss /*
    542   1.1     glass  * Attach a found device.  Allocates memory for device variables.
    543   1.1     glass  */
    544  1.25       cgd struct device *
    545  1.51       cgd config_attach(struct device *parent, struct cfdata *cf, void *aux,
    546  1.51       cgd 	cfprint_t print)
    547  1.25       cgd {
    548  1.50  augustss 	struct device *dev;
    549  1.65   thorpej 	struct cftable *ct;
    550  1.50  augustss 	struct cfdriver *cd;
    551  1.50  augustss 	struct cfattach *ca;
    552  1.50  augustss 	size_t lname, lunit;
    553  1.52       cgd 	const char *xunit;
    554  1.25       cgd 	int myunit;
    555  1.25       cgd 	char num[10];
    556  1.25       cgd 
    557  1.67   thorpej 	cd = config_cfdriver_lookup(cf->cf_name);
    558  1.67   thorpej 	KASSERT(cd != NULL);
    559  1.25       cgd 	ca = cf->cf_attach;
    560  1.25       cgd 	if (ca->ca_devsize < sizeof(struct device))
    561  1.25       cgd 		panic("config_attach");
    562  1.66   thorpej 
    563  1.46       cgd #ifndef __BROKEN_CONFIG_UNIT_USAGE
    564  1.45       cgd 	if (cf->cf_fstate == FSTATE_STAR) {
    565  1.45       cgd 		for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
    566  1.45       cgd 			if (cd->cd_devs[myunit] == NULL)
    567  1.45       cgd 				break;
    568  1.45       cgd 		/*
    569  1.45       cgd 		 * myunit is now the unit of the first NULL device pointer,
    570  1.45       cgd 		 * or max(cd->cd_ndevs,cf->cf_unit).
    571  1.45       cgd 		 */
    572  1.45       cgd 	} else {
    573  1.45       cgd 		myunit = cf->cf_unit;
    574  1.66   thorpej 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
    575  1.66   thorpej 		cf->cf_fstate = FSTATE_FOUND;
    576  1.66   thorpej 	}
    577  1.66   thorpej #else
    578  1.46       cgd 	myunit = cf->cf_unit;
    579  1.46       cgd 	if (cf->cf_fstate == FSTATE_STAR)
    580  1.46       cgd 		cf->cf_unit++;
    581  1.46       cgd 	else {
    582  1.25       cgd 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
    583  1.25       cgd 		cf->cf_fstate = FSTATE_FOUND;
    584  1.25       cgd 	}
    585  1.66   thorpej #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
    586  1.25       cgd 
    587  1.25       cgd 	/* compute length of name and decimal expansion of unit number */
    588  1.25       cgd 	lname = strlen(cd->cd_name);
    589  1.30     perry 	xunit = number(&num[sizeof(num)], myunit);
    590  1.30     perry 	lunit = &num[sizeof(num)] - xunit;
    591  1.64  drochner 	if (lname + lunit > sizeof(dev->dv_xname))
    592  1.25       cgd 		panic("config_attach: device name too long");
    593  1.25       cgd 
    594  1.25       cgd 	/* get memory for all device vars */
    595  1.43   thorpej 	dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
    596  1.43   thorpej 	    cold ? M_NOWAIT : M_WAITOK);
    597  1.25       cgd 	if (!dev)
    598  1.25       cgd 	    panic("config_attach: memory allocation for device softc failed");
    599  1.31     perry 	memset(dev, 0, ca->ca_devsize);
    600  1.25       cgd 	TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);	/* link up */
    601  1.25       cgd 	dev->dv_class = cd->cd_class;
    602  1.25       cgd 	dev->dv_cfdata = cf;
    603  1.25       cgd 	dev->dv_unit = myunit;
    604  1.31     perry 	memcpy(dev->dv_xname, cd->cd_name, lname);
    605  1.31     perry 	memcpy(dev->dv_xname + lname, xunit, lunit);
    606  1.25       cgd 	dev->dv_parent = parent;
    607  1.33   thorpej 	dev->dv_flags = DVF_ACTIVE;	/* always initially active */
    608  1.29   thorpej 
    609  1.25       cgd 	if (parent == ROOT)
    610  1.25       cgd 		printf("%s (root)", dev->dv_xname);
    611  1.25       cgd 	else {
    612  1.25       cgd 		printf("%s at %s", dev->dv_xname, parent->dv_xname);
    613  1.25       cgd 		if (print)
    614  1.52       cgd 			(void) (*print)(aux, NULL);
    615  1.25       cgd 	}
    616  1.25       cgd 
    617  1.25       cgd 	/* put this device in the devices array */
    618  1.59  augustss 	config_makeroom(dev->dv_unit, cd);
    619  1.25       cgd 	if (cd->cd_devs[dev->dv_unit])
    620  1.25       cgd 		panic("config_attach: duplicate %s", dev->dv_xname);
    621  1.25       cgd 	cd->cd_devs[dev->dv_unit] = dev;
    622  1.25       cgd 
    623  1.25       cgd 	/*
    624  1.25       cgd 	 * Before attaching, clobber any unfound devices that are
    625  1.45       cgd 	 * otherwise identical.
    626  1.25       cgd 	 */
    627  1.65   thorpej 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
    628  1.67   thorpej 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
    629  1.67   thorpej 			if (STREQ(cf->cf_name, cd->cd_name) &&
    630  1.65   thorpej 			    cf->cf_unit == dev->dv_unit) {
    631  1.65   thorpej 				if (cf->cf_fstate == FSTATE_NOTFOUND)
    632  1.65   thorpej 					cf->cf_fstate = FSTATE_FOUND;
    633  1.46       cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
    634  1.66   thorpej 				/*
    635  1.66   thorpej 				 * Bump the unit number on all starred cfdata
    636  1.66   thorpej 				 * entries for this device.
    637  1.66   thorpej 				 */
    638  1.65   thorpej 				if (cf->cf_fstate == FSTATE_STAR)
    639  1.65   thorpej 					cf->cf_unit++;
    640  1.46       cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    641  1.65   thorpej 			}
    642  1.25       cgd 		}
    643  1.65   thorpej 	}
    644  1.49      danw #ifdef __HAVE_DEVICE_REGISTER
    645  1.25       cgd 	device_register(dev, aux);
    646  1.25       cgd #endif
    647  1.25       cgd 	(*ca->ca_attach)(parent, dev, aux);
    648  1.42   thorpej 	config_process_deferred(&deferred_config_queue, dev);
    649  1.25       cgd 	return (dev);
    650  1.25       cgd }
    651  1.29   thorpej 
    652  1.29   thorpej /*
    653  1.33   thorpej  * Detach a device.  Optionally forced (e.g. because of hardware
    654  1.33   thorpej  * removal) and quiet.  Returns zero if successful, non-zero
    655  1.33   thorpej  * (an error code) otherwise.
    656  1.33   thorpej  *
    657  1.33   thorpej  * Note that this code wants to be run from a process context, so
    658  1.33   thorpej  * that the detach can sleep to allow processes which have a device
    659  1.33   thorpej  * open to run and unwind their stacks.
    660  1.33   thorpej  */
    661  1.33   thorpej int
    662  1.51       cgd config_detach(struct device *dev, int flags)
    663  1.33   thorpej {
    664  1.65   thorpej 	struct cftable *ct;
    665  1.33   thorpej 	struct cfdata *cf;
    666  1.33   thorpej 	struct cfattach *ca;
    667  1.33   thorpej 	struct cfdriver *cd;
    668  1.33   thorpej #ifdef DIAGNOSTIC
    669  1.33   thorpej 	struct device *d;
    670  1.33   thorpej #endif
    671  1.36   thorpej 	int rv = 0, i;
    672  1.33   thorpej 
    673  1.33   thorpej 	cf = dev->dv_cfdata;
    674  1.33   thorpej #ifdef DIAGNOSTIC
    675  1.33   thorpej 	if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
    676  1.33   thorpej 		panic("config_detach: bad device fstate");
    677  1.33   thorpej #endif
    678  1.67   thorpej 	cd = config_cfdriver_lookup(cf->cf_name);
    679  1.67   thorpej 	KASSERT(cd != NULL);
    680  1.33   thorpej 	ca = cf->cf_attach;
    681  1.33   thorpej 
    682  1.33   thorpej 	/*
    683  1.33   thorpej 	 * Ensure the device is deactivated.  If the device doesn't
    684  1.33   thorpej 	 * have an activation entry point, we allow DVF_ACTIVE to
    685  1.33   thorpej 	 * remain set.  Otherwise, if DVF_ACTIVE is still set, the
    686  1.33   thorpej 	 * device is busy, and the detach fails.
    687  1.33   thorpej 	 */
    688  1.35   thorpej 	if (ca->ca_activate != NULL)
    689  1.35   thorpej 		rv = config_deactivate(dev);
    690  1.33   thorpej 
    691  1.33   thorpej 	/*
    692  1.33   thorpej 	 * Try to detach the device.  If that's not possible, then
    693  1.33   thorpej 	 * we either panic() (for the forced but failed case), or
    694  1.33   thorpej 	 * return an error.
    695  1.33   thorpej 	 */
    696  1.33   thorpej 	if (rv == 0) {
    697  1.33   thorpej 		if (ca->ca_detach != NULL)
    698  1.33   thorpej 			rv = (*ca->ca_detach)(dev, flags);
    699  1.33   thorpej 		else
    700  1.33   thorpej 			rv = EOPNOTSUPP;
    701  1.33   thorpej 	}
    702  1.33   thorpej 	if (rv != 0) {
    703  1.33   thorpej 		if ((flags & DETACH_FORCE) == 0)
    704  1.33   thorpej 			return (rv);
    705  1.33   thorpej 		else
    706  1.33   thorpej 			panic("config_detach: forced detach of %s failed (%d)",
    707  1.33   thorpej 			    dev->dv_xname, rv);
    708  1.33   thorpej 	}
    709  1.33   thorpej 
    710  1.33   thorpej 	/*
    711  1.33   thorpej 	 * The device has now been successfully detached.
    712  1.33   thorpej 	 */
    713  1.33   thorpej 
    714  1.33   thorpej #ifdef DIAGNOSTIC
    715  1.33   thorpej 	/*
    716  1.33   thorpej 	 * Sanity: If you're successfully detached, you should have no
    717  1.33   thorpej 	 * children.  (Note that because children must be attached
    718  1.33   thorpej 	 * after parents, we only need to search the latter part of
    719  1.33   thorpej 	 * the list.)
    720  1.33   thorpej 	 */
    721  1.33   thorpej 	for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
    722  1.48     enami 	    d = TAILQ_NEXT(d, dv_list)) {
    723  1.48     enami 		if (d->dv_parent == dev) {
    724  1.48     enami 			printf("config_detach: detached device %s"
    725  1.48     enami 			    " has children %s\n", dev->dv_xname, d->dv_xname);
    726  1.48     enami 			panic("config_detach");
    727  1.48     enami 		}
    728  1.33   thorpej 	}
    729  1.33   thorpej #endif
    730  1.33   thorpej 
    731  1.33   thorpej 	/*
    732  1.33   thorpej 	 * Mark cfdata to show that the unit can be reused, if possible.
    733  1.33   thorpej 	 */
    734  1.65   thorpej 	TAILQ_FOREACH(ct, &allcftables, ct_list) {
    735  1.67   thorpej 		for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
    736  1.67   thorpej 			if (STREQ(cf->cf_name, cd->cd_name)) {
    737  1.65   thorpej 				if (cf->cf_fstate == FSTATE_FOUND &&
    738  1.65   thorpej 				    cf->cf_unit == dev->dv_unit)
    739  1.65   thorpej 					cf->cf_fstate = FSTATE_NOTFOUND;
    740  1.46       cgd #ifdef __BROKEN_CONFIG_UNIT_USAGE
    741  1.66   thorpej 				/*
    742  1.66   thorpej 				 * Note that we can only re-use a starred
    743  1.66   thorpej 				 * unit number if the unit being detached
    744  1.66   thorpej 				 * had the last assigned unit number.
    745  1.66   thorpej 				 */
    746  1.65   thorpej 				if (cf->cf_fstate == FSTATE_STAR &&
    747  1.65   thorpej 				    cf->cf_unit == dev->dv_unit + 1)
    748  1.65   thorpej 					cf->cf_unit--;
    749  1.46       cgd #endif /* __BROKEN_CONFIG_UNIT_USAGE */
    750  1.65   thorpej 			}
    751  1.33   thorpej 		}
    752  1.33   thorpej 	}
    753  1.33   thorpej 
    754  1.33   thorpej 	/*
    755  1.33   thorpej 	 * Unlink from device list.
    756  1.33   thorpej 	 */
    757  1.33   thorpej 	TAILQ_REMOVE(&alldevs, dev, dv_list);
    758  1.33   thorpej 
    759  1.33   thorpej 	/*
    760  1.33   thorpej 	 * Remove from cfdriver's array, tell the world, and free softc.
    761  1.33   thorpej 	 */
    762  1.33   thorpej 	cd->cd_devs[dev->dv_unit] = NULL;
    763  1.33   thorpej 	if ((flags & DETACH_QUIET) == 0)
    764  1.33   thorpej 		printf("%s detached\n", dev->dv_xname);
    765  1.33   thorpej 	free(dev, M_DEVBUF);
    766  1.33   thorpej 
    767  1.33   thorpej 	/*
    768  1.33   thorpej 	 * If the device now has no units in use, deallocate its softc array.
    769  1.33   thorpej 	 */
    770  1.33   thorpej 	for (i = 0; i < cd->cd_ndevs; i++)
    771  1.33   thorpej 		if (cd->cd_devs[i] != NULL)
    772  1.33   thorpej 			break;
    773  1.33   thorpej 	if (i == cd->cd_ndevs) {		/* nothing found; deallocate */
    774  1.33   thorpej 		free(cd->cd_devs, M_DEVBUF);
    775  1.33   thorpej 		cd->cd_devs = NULL;
    776  1.33   thorpej 		cd->cd_ndevs = 0;
    777  1.33   thorpej 	}
    778  1.33   thorpej 
    779  1.33   thorpej 	/*
    780  1.33   thorpej 	 * Return success.
    781  1.33   thorpej 	 */
    782  1.33   thorpej 	return (0);
    783  1.33   thorpej }
    784  1.33   thorpej 
    785  1.33   thorpej int
    786  1.51       cgd config_activate(struct device *dev)
    787  1.33   thorpej {
    788  1.33   thorpej 	struct cfattach *ca = dev->dv_cfdata->cf_attach;
    789  1.34   thorpej 	int rv = 0, oflags = dev->dv_flags;
    790  1.33   thorpej 
    791  1.33   thorpej 	if (ca->ca_activate == NULL)
    792  1.33   thorpej 		return (EOPNOTSUPP);
    793  1.33   thorpej 
    794  1.33   thorpej 	if ((dev->dv_flags & DVF_ACTIVE) == 0) {
    795  1.33   thorpej 		dev->dv_flags |= DVF_ACTIVE;
    796  1.33   thorpej 		rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
    797  1.34   thorpej 		if (rv)
    798  1.34   thorpej 			dev->dv_flags = oflags;
    799  1.33   thorpej 	}
    800  1.33   thorpej 	return (rv);
    801  1.33   thorpej }
    802  1.33   thorpej 
    803  1.33   thorpej int
    804  1.51       cgd config_deactivate(struct device *dev)
    805  1.33   thorpej {
    806  1.33   thorpej 	struct cfattach *ca = dev->dv_cfdata->cf_attach;
    807  1.34   thorpej 	int rv = 0, oflags = dev->dv_flags;
    808  1.33   thorpej 
    809  1.33   thorpej 	if (ca->ca_activate == NULL)
    810  1.33   thorpej 		return (EOPNOTSUPP);
    811  1.33   thorpej 
    812  1.33   thorpej 	if (dev->dv_flags & DVF_ACTIVE) {
    813  1.33   thorpej 		dev->dv_flags &= ~DVF_ACTIVE;
    814  1.33   thorpej 		rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
    815  1.34   thorpej 		if (rv)
    816  1.34   thorpej 			dev->dv_flags = oflags;
    817  1.33   thorpej 	}
    818  1.33   thorpej 	return (rv);
    819  1.33   thorpej }
    820  1.33   thorpej 
    821  1.33   thorpej /*
    822  1.29   thorpej  * Defer the configuration of the specified device until all
    823  1.29   thorpej  * of its parent's devices have been attached.
    824  1.29   thorpej  */
    825  1.29   thorpej void
    826  1.51       cgd config_defer(struct device *dev, void (*func)(struct device *))
    827  1.29   thorpej {
    828  1.29   thorpej 	struct deferred_config *dc;
    829  1.29   thorpej 
    830  1.29   thorpej 	if (dev->dv_parent == NULL)
    831  1.29   thorpej 		panic("config_defer: can't defer config of a root device");
    832  1.29   thorpej 
    833  1.29   thorpej #ifdef DIAGNOSTIC
    834  1.29   thorpej 	for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
    835  1.29   thorpej 	     dc = TAILQ_NEXT(dc, dc_queue)) {
    836  1.29   thorpej 		if (dc->dc_dev == dev)
    837  1.29   thorpej 			panic("config_defer: deferred twice");
    838  1.29   thorpej 	}
    839  1.29   thorpej #endif
    840  1.29   thorpej 
    841  1.43   thorpej 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
    842  1.43   thorpej 	if (dc == NULL)
    843  1.43   thorpej 		panic("config_defer: unable to allocate callback");
    844  1.29   thorpej 
    845  1.29   thorpej 	dc->dc_dev = dev;
    846  1.29   thorpej 	dc->dc_func = func;
    847  1.29   thorpej 	TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
    848  1.47   thorpej 	config_pending_incr();
    849  1.29   thorpej }
    850  1.29   thorpej 
    851  1.29   thorpej /*
    852  1.42   thorpej  * Defer some autoconfiguration for a device until after interrupts
    853  1.42   thorpej  * are enabled.
    854  1.42   thorpej  */
    855  1.42   thorpej void
    856  1.51       cgd config_interrupts(struct device *dev, void (*func)(struct device *))
    857  1.42   thorpej {
    858  1.42   thorpej 	struct deferred_config *dc;
    859  1.42   thorpej 
    860  1.42   thorpej 	/*
    861  1.42   thorpej 	 * If interrupts are enabled, callback now.
    862  1.42   thorpej 	 */
    863  1.43   thorpej 	if (cold == 0) {
    864  1.42   thorpej 		(*func)(dev);
    865  1.42   thorpej 		return;
    866  1.42   thorpej 	}
    867  1.42   thorpej 
    868  1.42   thorpej #ifdef DIAGNOSTIC
    869  1.42   thorpej 	for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
    870  1.42   thorpej 	     dc = TAILQ_NEXT(dc, dc_queue)) {
    871  1.42   thorpej 		if (dc->dc_dev == dev)
    872  1.42   thorpej 			panic("config_interrupts: deferred twice");
    873  1.42   thorpej 	}
    874  1.42   thorpej #endif
    875  1.42   thorpej 
    876  1.43   thorpej 	dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
    877  1.43   thorpej 	if (dc == NULL)
    878  1.43   thorpej 		panic("config_interrupts: unable to allocate callback");
    879  1.42   thorpej 
    880  1.42   thorpej 	dc->dc_dev = dev;
    881  1.42   thorpej 	dc->dc_func = func;
    882  1.42   thorpej 	TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
    883  1.47   thorpej 	config_pending_incr();
    884  1.42   thorpej }
    885  1.42   thorpej 
    886  1.42   thorpej /*
    887  1.42   thorpej  * Process a deferred configuration queue.
    888  1.29   thorpej  */
    889  1.29   thorpej static void
    890  1.51       cgd config_process_deferred(struct deferred_config_head *queue,
    891  1.51       cgd     struct device *parent)
    892  1.29   thorpej {
    893  1.29   thorpej 	struct deferred_config *dc, *ndc;
    894  1.29   thorpej 
    895  1.42   thorpej 	for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
    896  1.29   thorpej 		ndc = TAILQ_NEXT(dc, dc_queue);
    897  1.42   thorpej 		if (parent == NULL || dc->dc_dev->dv_parent == parent) {
    898  1.42   thorpej 			TAILQ_REMOVE(queue, dc, dc_queue);
    899  1.29   thorpej 			(*dc->dc_func)(dc->dc_dev);
    900  1.29   thorpej 			free(dc, M_DEVBUF);
    901  1.47   thorpej 			config_pending_decr();
    902  1.29   thorpej 		}
    903  1.29   thorpej 	}
    904  1.47   thorpej }
    905  1.47   thorpej 
    906  1.47   thorpej /*
    907  1.47   thorpej  * Manipulate the config_pending semaphore.
    908  1.47   thorpej  */
    909  1.47   thorpej void
    910  1.51       cgd config_pending_incr(void)
    911  1.47   thorpej {
    912  1.47   thorpej 
    913  1.47   thorpej 	config_pending++;
    914  1.47   thorpej }
    915  1.47   thorpej 
    916  1.47   thorpej void
    917  1.51       cgd config_pending_decr(void)
    918  1.47   thorpej {
    919  1.47   thorpej 
    920  1.47   thorpej #ifdef DIAGNOSTIC
    921  1.47   thorpej 	if (config_pending == 0)
    922  1.47   thorpej 		panic("config_pending_decr: config_pending == 0");
    923  1.47   thorpej #endif
    924  1.47   thorpej 	config_pending--;
    925  1.47   thorpej 	if (config_pending == 0)
    926  1.47   thorpej 		wakeup((void *)&config_pending);
    927  1.29   thorpej }
    928   1.1     glass 
    929   1.1     glass /*
    930  1.53       cgd  * Attach a statically-initialized event.  The type and string pointers
    931  1.53       cgd  * are already set up.
    932   1.1     glass  */
    933   1.1     glass void
    934  1.53       cgd evcnt_attach_static(struct evcnt *ev)
    935   1.1     glass {
    936  1.53       cgd 	int len;
    937   1.1     glass 
    938  1.53       cgd 	len = strlen(ev->ev_group);
    939   1.1     glass #ifdef DIAGNOSTIC
    940  1.53       cgd 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
    941  1.53       cgd 		panic("evcnt_attach_static: group length (%s)", ev->ev_group);
    942   1.1     glass #endif
    943  1.53       cgd 	ev->ev_grouplen = len;
    944  1.53       cgd 
    945  1.53       cgd 	len = strlen(ev->ev_name);
    946  1.53       cgd #ifdef DIAGNOSTIC
    947  1.53       cgd 	if (len >= EVCNT_STRING_MAX)		/* ..._MAX includes NUL */
    948  1.53       cgd 		panic("evcnt_attach_static: name length (%s)", ev->ev_name);
    949  1.53       cgd #endif
    950  1.53       cgd 	ev->ev_namelen = len;
    951  1.53       cgd 
    952  1.20       cgd 	TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
    953  1.53       cgd }
    954  1.53       cgd 
    955  1.53       cgd /*
    956  1.53       cgd  * Attach a dynamically-initialized event.  Zero it, set up the type
    957  1.53       cgd  * and string pointers and then act like it was statically initialized.
    958  1.53       cgd  */
    959  1.53       cgd void
    960  1.53       cgd evcnt_attach_dynamic(struct evcnt *ev, int type, const struct evcnt *parent,
    961  1.53       cgd     const char *group, const char *name)
    962  1.53       cgd {
    963  1.53       cgd 
    964  1.53       cgd 	memset(ev, 0, sizeof *ev);
    965  1.53       cgd 	ev->ev_type = type;
    966  1.53       cgd 	ev->ev_parent = parent;
    967  1.53       cgd 	ev->ev_group = group;
    968  1.53       cgd 	ev->ev_name = name;
    969  1.53       cgd 	evcnt_attach_static(ev);
    970  1.33   thorpej }
    971  1.33   thorpej 
    972  1.33   thorpej /*
    973  1.33   thorpej  * Detach an event.
    974  1.33   thorpej  */
    975  1.33   thorpej void
    976  1.51       cgd evcnt_detach(struct evcnt *ev)
    977  1.33   thorpej {
    978  1.33   thorpej 
    979  1.33   thorpej 	TAILQ_REMOVE(&allevents, ev, ev_list);
    980   1.1     glass }
    981  1.62    simonb 
    982  1.62    simonb #ifdef DDB
    983  1.62    simonb void
    984  1.62    simonb event_print(int full, void (*pr)(const char *, ...))
    985  1.62    simonb {
    986  1.62    simonb 	struct evcnt *evp;
    987  1.62    simonb 
    988  1.62    simonb 	TAILQ_FOREACH(evp, &allevents, ev_list) {
    989  1.62    simonb 		if (evp->ev_count == 0 && !full)
    990  1.62    simonb 			continue;
    991  1.62    simonb 
    992  1.62    simonb 		(*pr)("evcnt type %d: %s %s = %lld\n", evp->ev_type,
    993  1.62    simonb 		    evp->ev_group, evp->ev_name, evp->ev_count);
    994  1.62    simonb 	}
    995  1.62    simonb }
    996  1.62    simonb #endif /* DDB */
    997