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