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