Home | History | Annotate | Line # | Download | only in kern
subr_autoconf.c revision 1.29
      1  1.29   thorpej /*	$NetBSD: subr_autoconf.c,v 1.29 1998/06/09 18:46:12 thorpej Exp $	*/
      2   1.9       cgd 
      3   1.1     glass /*
      4   1.7     glass  * Copyright (c) 1992, 1993
      5   1.7     glass  *	The Regents of the University of California.  All rights reserved.
      6   1.1     glass  *
      7   1.1     glass  * This software was developed by the Computer Systems Engineering group
      8   1.1     glass  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9   1.1     glass  * contributed to Berkeley.
     10   1.1     glass  *
     11   1.1     glass  * All advertising materials mentioning features or use of this software
     12   1.1     glass  * must display the following acknowledgement:
     13   1.1     glass  *	This product includes software developed by the University of
     14   1.1     glass  *	California, Lawrence Berkeley Laboratories.
     15   1.1     glass  *
     16   1.7     glass  * Redistribution and use in source and binary forms, with or without
     17   1.7     glass  * modification, are permitted provided that the following conditions
     18   1.7     glass  * are met:
     19   1.7     glass  * 1. Redistributions of source code must retain the above copyright
     20   1.7     glass  *    notice, this list of conditions and the following disclaimer.
     21   1.7     glass  * 2. Redistributions in binary form must reproduce the above copyright
     22   1.7     glass  *    notice, this list of conditions and the following disclaimer in the
     23   1.7     glass  *    documentation and/or other materials provided with the distribution.
     24   1.7     glass  * 3. All advertising materials mentioning features or use of this software
     25   1.7     glass  *    must display the following acknowledgement:
     26   1.7     glass  *	This product includes software developed by the University of
     27   1.7     glass  *	California, Berkeley and its contributors.
     28   1.7     glass  * 4. Neither the name of the University nor the names of its contributors
     29   1.7     glass  *    may be used to endorse or promote products derived from this software
     30   1.7     glass  *    without specific prior written permission.
     31   1.1     glass  *
     32   1.7     glass  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33   1.7     glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34   1.7     glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35   1.7     glass  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36   1.7     glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37   1.7     glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38   1.7     glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39   1.7     glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40   1.7     glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41   1.7     glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42   1.7     glass  * SUCH DAMAGE.
     43   1.1     glass  *
     44   1.8       cgd  * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp  (LBL)
     45   1.9       cgd  *
     46  1.28      fvdl  *	@(#)subr_autoconf.c	8.3 (Berkeley) 5/17/94
     47   1.1     glass  */
     48   1.1     glass 
     49   1.4   mycroft #include <sys/param.h>
     50   1.4   mycroft #include <sys/device.h>
     51   1.4   mycroft #include <sys/malloc.h>
     52  1.17  christos #include <sys/systm.h>
     53  1.16   mycroft #include <machine/limits.h>
     54   1.1     glass 
     55   1.1     glass /*
     56   1.1     glass  * Autoconfiguration subroutines.
     57   1.1     glass  */
     58   1.1     glass 
     59   1.1     glass /*
     60   1.1     glass  * ioconf.c exports exactly two names: cfdata and cfroots.  All system
     61   1.1     glass  * devices and drivers are found via these tables.
     62   1.1     glass  */
     63   1.1     glass extern struct cfdata cfdata[];
     64   1.1     glass extern short cfroots[];
     65   1.1     glass 
     66   1.1     glass #define	ROOT ((struct device *)NULL)
     67   1.1     glass 
     68  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
     69  1.13   mycroft struct device *config_make_softc __P((struct device *, struct cfdata *));
     70  1.25       cgd #endif
     71  1.12   mycroft 
     72  1.16   mycroft struct matchinfo {
     73  1.16   mycroft 	cfmatch_t fn;
     74  1.16   mycroft 	struct	device *parent;
     75  1.25       cgd 	void	*aux;
     76  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
     77  1.25       cgd 	void	*match;
     78  1.25       cgd 	int	indirect;
     79  1.25       cgd #else
     80  1.25       cgd 	struct	cfdata *match;
     81  1.25       cgd #endif
     82  1.25       cgd 	int	pri;
     83  1.16   mycroft };
     84  1.17  christos 
     85  1.17  christos static char *number __P((char *, int));
     86  1.17  christos static void mapply __P((struct matchinfo *, struct cfdata *));
     87  1.16   mycroft 
     88  1.29   thorpej struct deferred_config {
     89  1.29   thorpej 	TAILQ_ENTRY(deferred_config) dc_queue;
     90  1.29   thorpej 	struct device *dc_dev;
     91  1.29   thorpej 	void (*dc_func) __P((struct device *));
     92  1.29   thorpej };
     93  1.29   thorpej 
     94  1.29   thorpej TAILQ_HEAD(, deferred_config) deferred_config_queue;
     95  1.29   thorpej 
     96  1.29   thorpej static void config_process_deferred_children __P((struct device *));
     97  1.29   thorpej 
     98  1.20       cgd struct devicelist alldevs;		/* list of all devices */
     99  1.20       cgd struct evcntlist allevents;		/* list of all event counters */
    100  1.20       cgd 
    101  1.20       cgd /*
    102  1.20       cgd  * Initialize autoconfiguration data structures.
    103  1.20       cgd  */
    104  1.20       cgd void
    105  1.20       cgd config_init()
    106  1.20       cgd {
    107  1.20       cgd 
    108  1.29   thorpej 	TAILQ_INIT(&deferred_config_queue);
    109  1.20       cgd 	TAILQ_INIT(&alldevs);
    110  1.20       cgd 	TAILQ_INIT(&allevents);
    111  1.20       cgd }
    112  1.20       cgd 
    113   1.1     glass /*
    114   1.1     glass  * Apply the matching function and choose the best.  This is used
    115   1.1     glass  * a few times and we want to keep the code small.
    116   1.1     glass  */
    117  1.16   mycroft static void
    118  1.16   mycroft mapply(m, cf)
    119   1.1     glass 	register struct matchinfo *m;
    120   1.1     glass 	register struct cfdata *cf;
    121   1.1     glass {
    122   1.1     glass 	register int pri;
    123  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    124  1.12   mycroft 	void *match;
    125  1.12   mycroft 
    126  1.16   mycroft 	if (m->indirect)
    127  1.13   mycroft 		match = config_make_softc(m->parent, cf);
    128  1.16   mycroft 	else
    129  1.12   mycroft 		match = cf;
    130  1.25       cgd #endif
    131   1.1     glass 
    132   1.1     glass 	if (m->fn != NULL)
    133  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    134  1.12   mycroft 		pri = (*m->fn)(m->parent, match, m->aux);
    135  1.25       cgd #else
    136  1.25       cgd 		pri = (*m->fn)(m->parent, cf, m->aux);
    137  1.25       cgd #endif
    138   1.3     glass 	else {
    139  1.19   thorpej 	        if (cf->cf_attach->ca_match == NULL) {
    140  1.16   mycroft 			panic("mapply: no match function for '%s' device\n",
    141  1.12   mycroft 			    cf->cf_driver->cd_name);
    142   1.3     glass 		}
    143  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    144  1.19   thorpej 		pri = (*cf->cf_attach->ca_match)(m->parent, match, m->aux);
    145  1.25       cgd #else
    146  1.25       cgd 		pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux);
    147  1.25       cgd #endif
    148   1.3     glass 	}
    149   1.1     glass 	if (pri > m->pri) {
    150  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    151  1.14   mycroft 		if (m->indirect && m->match)
    152  1.12   mycroft 			free(m->match, M_DEVBUF);
    153  1.12   mycroft 		m->match = match;
    154  1.25       cgd #else
    155  1.25       cgd 		m->match = cf;
    156  1.25       cgd #endif
    157   1.1     glass 		m->pri = pri;
    158  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    159  1.12   mycroft 	} else {
    160  1.14   mycroft 		if (m->indirect)
    161  1.12   mycroft 			free(match, M_DEVBUF);
    162  1.25       cgd #endif
    163   1.1     glass 	}
    164   1.1     glass }
    165   1.1     glass 
    166   1.1     glass /*
    167   1.1     glass  * Iterate over all potential children of some device, calling the given
    168   1.1     glass  * function (default being the child's match function) for each one.
    169   1.1     glass  * Nonzero returns are matches; the highest value returned is considered
    170   1.1     glass  * the best match.  Return the `found child' if we got a match, or NULL
    171   1.1     glass  * otherwise.  The `aux' pointer is simply passed on through.
    172   1.1     glass  *
    173   1.1     glass  * Note that this function is designed so that it can be used to apply
    174   1.1     glass  * an arbitrary function to all potential children (its return value
    175   1.1     glass  * can be ignored).
    176   1.1     glass  */
    177  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    178  1.12   mycroft void *
    179  1.25       cgd #else
    180  1.25       cgd struct cfdata *
    181  1.25       cgd #endif
    182   1.1     glass config_search(fn, parent, aux)
    183   1.1     glass 	cfmatch_t fn;
    184   1.1     glass 	register struct device *parent;
    185   1.1     glass 	void *aux;
    186   1.1     glass {
    187   1.1     glass 	register struct cfdata *cf;
    188   1.1     glass 	register short *p;
    189   1.1     glass 	struct matchinfo m;
    190   1.1     glass 
    191   1.1     glass 	m.fn = fn;
    192   1.1     glass 	m.parent = parent;
    193  1.25       cgd 	m.aux = aux;
    194  1.14   mycroft 	m.match = NULL;
    195  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    196  1.14   mycroft 	m.indirect = parent && parent->dv_cfdata->cf_driver->cd_indirect;
    197  1.25       cgd #endif
    198   1.1     glass 	m.pri = 0;
    199   1.1     glass 	for (cf = cfdata; cf->cf_driver; cf++) {
    200   1.1     glass 		/*
    201   1.1     glass 		 * Skip cf if no longer eligible, otherwise scan through
    202   1.1     glass 		 * parents for one matching `parent', and try match function.
    203   1.1     glass 		 */
    204   1.1     glass 		if (cf->cf_fstate == FSTATE_FOUND)
    205   1.1     glass 			continue;
    206   1.1     glass 		for (p = cf->cf_parents; *p >= 0; p++)
    207   1.1     glass 			if (parent->dv_cfdata == &cfdata[*p])
    208  1.16   mycroft 				mapply(&m, cf);
    209   1.1     glass 	}
    210   1.1     glass 	return (m.match);
    211   1.1     glass }
    212   1.1     glass 
    213  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    214   1.1     glass /*
    215  1.16   mycroft  * Iterate over all potential children of some device, calling the given
    216  1.16   mycroft  * function for each one.
    217  1.16   mycroft  *
    218  1.16   mycroft  * Note that this function is designed so that it can be used to apply
    219  1.16   mycroft  * an arbitrary function to all potential children (its return value
    220  1.16   mycroft  * can be ignored).
    221  1.16   mycroft  */
    222  1.16   mycroft void
    223  1.16   mycroft config_scan(fn, parent)
    224  1.16   mycroft 	cfscan_t fn;
    225  1.16   mycroft 	register struct device *parent;
    226  1.16   mycroft {
    227  1.16   mycroft 	register struct cfdata *cf;
    228  1.16   mycroft 	register short *p;
    229  1.16   mycroft 	void *match;
    230  1.16   mycroft 	int indirect;
    231  1.16   mycroft 
    232  1.16   mycroft 	indirect = parent && parent->dv_cfdata->cf_driver->cd_indirect;
    233  1.16   mycroft 	for (cf = cfdata; cf->cf_driver; cf++) {
    234  1.16   mycroft 		/*
    235  1.16   mycroft 		 * Skip cf if no longer eligible, otherwise scan through
    236  1.16   mycroft 		 * parents for one matching `parent', and try match function.
    237  1.16   mycroft 		 */
    238  1.16   mycroft 		if (cf->cf_fstate == FSTATE_FOUND)
    239  1.16   mycroft 			continue;
    240  1.16   mycroft 		for (p = cf->cf_parents; *p >= 0; p++)
    241  1.16   mycroft 			if (parent->dv_cfdata == &cfdata[*p]) {
    242  1.16   mycroft 				if (indirect)
    243  1.16   mycroft 					match = config_make_softc(parent, cf);
    244  1.16   mycroft 				else
    245  1.16   mycroft 					match = cf;
    246  1.16   mycroft 				(*fn)(parent, match);
    247  1.16   mycroft 			}
    248  1.16   mycroft 	}
    249  1.16   mycroft }
    250  1.25       cgd #endif /* __BROKEN_INDIRECT_CONFIG */
    251  1.16   mycroft 
    252  1.16   mycroft /*
    253   1.1     glass  * Find the given root device.
    254   1.1     glass  * This is much like config_search, but there is no parent.
    255   1.1     glass  */
    256  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    257  1.12   mycroft void *
    258  1.25       cgd #else
    259  1.25       cgd struct cfdata *
    260  1.25       cgd #endif
    261   1.1     glass config_rootsearch(fn, rootname, aux)
    262   1.1     glass 	register cfmatch_t fn;
    263   1.1     glass 	register char *rootname;
    264   1.1     glass 	register void *aux;
    265   1.1     glass {
    266   1.1     glass 	register struct cfdata *cf;
    267   1.1     glass 	register short *p;
    268   1.1     glass 	struct matchinfo m;
    269   1.1     glass 
    270   1.1     glass 	m.fn = fn;
    271   1.1     glass 	m.parent = ROOT;
    272  1.25       cgd 	m.aux = aux;
    273  1.14   mycroft 	m.match = NULL;
    274  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    275  1.14   mycroft 	m.indirect = 0;
    276  1.25       cgd #endif
    277   1.1     glass 	m.pri = 0;
    278   1.1     glass 	/*
    279   1.1     glass 	 * Look at root entries for matching name.  We do not bother
    280   1.1     glass 	 * with found-state here since only one root should ever be
    281   1.1     glass 	 * searched (and it must be done first).
    282   1.1     glass 	 */
    283   1.1     glass 	for (p = cfroots; *p >= 0; p++) {
    284   1.1     glass 		cf = &cfdata[*p];
    285   1.1     glass 		if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
    286  1.16   mycroft 			mapply(&m, cf);
    287   1.1     glass 	}
    288   1.1     glass 	return (m.match);
    289   1.1     glass }
    290   1.1     glass 
    291   1.1     glass static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
    292   1.1     glass 
    293   1.1     glass /*
    294   1.1     glass  * The given `aux' argument describes a device that has been found
    295   1.1     glass  * on the given parent, but not necessarily configured.  Locate the
    296  1.18       cgd  * configuration data for that device (using the submatch function
    297  1.18       cgd  * provided, or using candidates' cd_match configuration driver
    298  1.18       cgd  * functions) and attach it, and return true.  If the device was
    299   1.1     glass  * not configured, call the given `print' function and return 0.
    300   1.1     glass  */
    301  1.21       cgd struct device *
    302  1.18       cgd config_found_sm(parent, aux, print, submatch)
    303   1.1     glass 	struct device *parent;
    304   1.1     glass 	void *aux;
    305   1.1     glass 	cfprint_t print;
    306  1.18       cgd 	cfmatch_t submatch;
    307   1.1     glass {
    308  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    309  1.12   mycroft 	void *match;
    310   1.1     glass 
    311  1.21       cgd 	if ((match = config_search(submatch, parent, aux)) != NULL)
    312  1.21       cgd 		return (config_attach(parent, match, aux, print));
    313  1.25       cgd #else
    314  1.25       cgd 	struct cfdata *cf;
    315  1.25       cgd 
    316  1.25       cgd 	if ((cf = config_search(submatch, parent, aux)) != NULL)
    317  1.25       cgd 		return (config_attach(parent, cf, aux, print));
    318  1.25       cgd #endif
    319   1.3     glass 	if (print)
    320  1.24  christos 		printf(msgs[(*print)(aux, parent->dv_xname)]);
    321  1.21       cgd 	return (NULL);
    322   1.1     glass }
    323   1.1     glass 
    324   1.1     glass /*
    325   1.1     glass  * As above, but for root devices.
    326   1.1     glass  */
    327  1.21       cgd struct device *
    328   1.1     glass config_rootfound(rootname, aux)
    329   1.1     glass 	char *rootname;
    330   1.1     glass 	void *aux;
    331   1.1     glass {
    332  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    333  1.12   mycroft 	void *match;
    334   1.1     glass 
    335  1.21       cgd 	if ((match = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
    336  1.21       cgd 		return (config_attach(ROOT, match, aux, (cfprint_t)NULL));
    337  1.25       cgd #else
    338  1.25       cgd 	struct cfdata *cf;
    339  1.25       cgd 
    340  1.25       cgd 	if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
    341  1.25       cgd 		return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
    342  1.25       cgd #endif
    343  1.24  christos 	printf("root device %s not configured\n", rootname);
    344  1.21       cgd 	return (NULL);
    345   1.1     glass }
    346   1.1     glass 
    347   1.1     glass /* just like sprintf(buf, "%d") except that it works from the end */
    348   1.1     glass static char *
    349   1.1     glass number(ep, n)
    350   1.1     glass 	register char *ep;
    351   1.1     glass 	register int n;
    352   1.1     glass {
    353   1.1     glass 
    354   1.1     glass 	*--ep = 0;
    355   1.1     glass 	while (n >= 10) {
    356   1.1     glass 		*--ep = (n % 10) + '0';
    357   1.1     glass 		n /= 10;
    358   1.1     glass 	}
    359   1.1     glass 	*--ep = n + '0';
    360   1.1     glass 	return (ep);
    361   1.1     glass }
    362   1.1     glass 
    363   1.1     glass /*
    364   1.1     glass  * Attach a found device.  Allocates memory for device variables.
    365   1.1     glass  */
    366  1.25       cgd #ifdef __BROKEN_INDIRECT_CONFIG
    367  1.21       cgd struct device *
    368  1.12   mycroft config_attach(parent, match, aux, print)
    369   1.1     glass 	register struct device *parent;
    370  1.12   mycroft 	void *match;
    371   1.1     glass 	register void *aux;
    372   1.1     glass 	cfprint_t print;
    373   1.1     glass {
    374  1.12   mycroft 	register struct cfdata *cf;
    375  1.12   mycroft 	register struct device *dev;
    376  1.13   mycroft 	register struct cfdriver *cd;
    377  1.19   thorpej 	register struct cfattach *ca;
    378  1.12   mycroft 
    379  1.13   mycroft 	if (parent && parent->dv_cfdata->cf_driver->cd_indirect) {
    380  1.12   mycroft 		dev = match;
    381  1.12   mycroft 		cf = dev->dv_cfdata;
    382  1.12   mycroft 	} else {
    383  1.12   mycroft 		cf = match;
    384  1.13   mycroft 		dev = config_make_softc(parent, cf);
    385  1.12   mycroft 	}
    386  1.12   mycroft 
    387  1.13   mycroft 	cd = cf->cf_driver;
    388  1.19   thorpej 	ca = cf->cf_attach;
    389  1.13   mycroft 	cd->cd_devs[cf->cf_unit] = dev;
    390  1.12   mycroft 
    391  1.12   mycroft 	if (cf->cf_fstate == FSTATE_STAR)
    392  1.12   mycroft 		cf->cf_unit++;
    393  1.25       cgd 	else {
    394  1.25       cgd 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
    395  1.12   mycroft 		cf->cf_fstate = FSTATE_FOUND;
    396  1.25       cgd 	}
    397  1.12   mycroft 
    398  1.20       cgd 	TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);
    399  1.12   mycroft 
    400  1.12   mycroft 	if (parent == ROOT)
    401  1.24  christos 		printf("%s (root)", dev->dv_xname);
    402  1.12   mycroft 	else {
    403  1.24  christos 		printf("%s at %s", dev->dv_xname, parent->dv_xname);
    404  1.12   mycroft 		if (print)
    405  1.12   mycroft 			(void) (*print)(aux, (char *)0);
    406  1.12   mycroft 	}
    407  1.12   mycroft 
    408  1.12   mycroft 	/*
    409  1.12   mycroft 	 * Before attaching, clobber any unfound devices that are
    410  1.19   thorpej 	 * otherwise identical, or bump the unit number on all starred
    411  1.19   thorpej 	 * cfdata for this device.
    412  1.12   mycroft 	 */
    413  1.12   mycroft 	for (cf = cfdata; cf->cf_driver; cf++)
    414  1.19   thorpej 		if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) {
    415  1.19   thorpej 			if (cf->cf_fstate == FSTATE_NOTFOUND)
    416  1.19   thorpej 				cf->cf_fstate = FSTATE_FOUND;
    417  1.19   thorpej 			if (cf->cf_fstate == FSTATE_STAR)
    418  1.19   thorpej 				cf->cf_unit++;
    419  1.19   thorpej 		}
    420  1.27  drochner #if defined(__alpha__) || defined(hp300) || defined(__i386__)
    421  1.22       cgd 	device_register(dev, aux);
    422  1.22       cgd #endif
    423  1.19   thorpej 	(*ca->ca_attach)(parent, dev, aux);
    424  1.29   thorpej 	config_process_deferred_children(dev);
    425  1.21       cgd 	return (dev);
    426  1.12   mycroft }
    427  1.12   mycroft 
    428  1.12   mycroft struct device *
    429  1.13   mycroft config_make_softc(parent, cf)
    430  1.13   mycroft 	struct device *parent;
    431  1.12   mycroft 	struct cfdata *cf;
    432  1.12   mycroft {
    433   1.1     glass 	register struct device *dev;
    434   1.1     glass 	register struct cfdriver *cd;
    435  1.19   thorpej 	register struct cfattach *ca;
    436   1.1     glass 	register size_t lname, lunit;
    437   1.1     glass 	register char *xunit;
    438   1.1     glass 	char num[10];
    439   1.1     glass 
    440   1.1     glass 	cd = cf->cf_driver;
    441  1.19   thorpej 	ca = cf->cf_attach;
    442  1.19   thorpej 	if (ca->ca_devsize < sizeof(struct device))
    443  1.12   mycroft 		panic("config_make_softc");
    444   1.1     glass 
    445   1.1     glass 	/* compute length of name and decimal expansion of unit number */
    446   1.1     glass 	lname = strlen(cd->cd_name);
    447  1.12   mycroft 	xunit = number(&num[sizeof num], cf->cf_unit);
    448   1.1     glass 	lunit = &num[sizeof num] - xunit;
    449   1.1     glass 	if (lname + lunit >= sizeof(dev->dv_xname))
    450   1.1     glass 		panic("config_attach: device name too long");
    451   1.1     glass 
    452   1.1     glass 	/* get memory for all device vars */
    453  1.19   thorpej 	dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF, M_NOWAIT);
    454   1.3     glass 	if (!dev)
    455   1.3     glass 	    panic("config_attach: memory allocation for device softc failed");
    456  1.19   thorpej 	bzero(dev, ca->ca_devsize);
    457   1.1     glass 	dev->dv_class = cd->cd_class;
    458   1.1     glass 	dev->dv_cfdata = cf;
    459  1.12   mycroft 	dev->dv_unit = cf->cf_unit;
    460   1.1     glass 	bcopy(cd->cd_name, dev->dv_xname, lname);
    461   1.1     glass 	bcopy(xunit, dev->dv_xname + lname, lunit);
    462   1.1     glass 	dev->dv_parent = parent;
    463   1.1     glass 
    464   1.1     glass 	/* put this device in the devices array */
    465   1.1     glass 	if (dev->dv_unit >= cd->cd_ndevs) {
    466   1.1     glass 		/*
    467   1.1     glass 		 * Need to expand the array.
    468   1.1     glass 		 */
    469  1.10   mycroft 		int old = cd->cd_ndevs, new;
    470   1.1     glass 		void **nsp;
    471   1.1     glass 
    472  1.10   mycroft 		if (old == 0)
    473  1.10   mycroft 			new = MINALLOCSIZE / sizeof(void *);
    474  1.10   mycroft 		else
    475  1.10   mycroft 			new = old * 2;
    476  1.10   mycroft 		while (new <= dev->dv_unit)
    477  1.10   mycroft 			new *= 2;
    478  1.10   mycroft 		cd->cd_ndevs = new;
    479  1.10   mycroft 		nsp = malloc(new * sizeof(void *), M_DEVBUF, M_NOWAIT);
    480  1.10   mycroft 		if (nsp == 0)
    481  1.10   mycroft 			panic("config_attach: %sing dev array",
    482  1.10   mycroft 			    old != 0 ? "expand" : "creat");
    483  1.10   mycroft 		bzero(nsp + old, (new - old) * sizeof(void *));
    484  1.10   mycroft 		if (old != 0) {
    485  1.10   mycroft 			bcopy(cd->cd_devs, nsp, old * sizeof(void *));
    486   1.1     glass 			free(cd->cd_devs, M_DEVBUF);
    487   1.1     glass 		}
    488   1.1     glass 		cd->cd_devs = nsp;
    489   1.1     glass 	}
    490   1.1     glass 	if (cd->cd_devs[dev->dv_unit])
    491   1.1     glass 		panic("config_attach: duplicate %s", dev->dv_xname);
    492   1.1     glass 
    493  1.12   mycroft 	return (dev);
    494   1.1     glass }
    495  1.25       cgd #else /* __BROKEN_INDIRECT_CONFIG */
    496  1.25       cgd struct device *
    497  1.25       cgd config_attach(parent, cf, aux, print)
    498  1.25       cgd 	register struct device *parent;
    499  1.25       cgd 	register struct cfdata *cf;
    500  1.25       cgd 	register void *aux;
    501  1.25       cgd 	cfprint_t print;
    502  1.25       cgd {
    503  1.25       cgd 	register struct device *dev;
    504  1.25       cgd 	register struct cfdriver *cd;
    505  1.25       cgd 	register struct cfattach *ca;
    506  1.25       cgd 	register size_t lname, lunit;
    507  1.25       cgd 	register char *xunit;
    508  1.25       cgd 	int myunit;
    509  1.25       cgd 	char num[10];
    510  1.25       cgd 
    511  1.25       cgd 	cd = cf->cf_driver;
    512  1.25       cgd 	ca = cf->cf_attach;
    513  1.25       cgd 	if (ca->ca_devsize < sizeof(struct device))
    514  1.25       cgd 		panic("config_attach");
    515  1.25       cgd 	myunit = cf->cf_unit;
    516  1.25       cgd 	if (cf->cf_fstate == FSTATE_STAR)
    517  1.25       cgd 		cf->cf_unit++;
    518  1.25       cgd 	else {
    519  1.25       cgd 		KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
    520  1.25       cgd 		cf->cf_fstate = FSTATE_FOUND;
    521  1.25       cgd 	}
    522  1.25       cgd 
    523  1.25       cgd 	/* compute length of name and decimal expansion of unit number */
    524  1.25       cgd 	lname = strlen(cd->cd_name);
    525  1.25       cgd 	xunit = number(&num[sizeof num], myunit);
    526  1.25       cgd 	lunit = &num[sizeof num] - xunit;
    527  1.25       cgd 	if (lname + lunit >= sizeof(dev->dv_xname))
    528  1.25       cgd 		panic("config_attach: device name too long");
    529  1.25       cgd 
    530  1.25       cgd 	/* get memory for all device vars */
    531  1.25       cgd 	dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF, M_NOWAIT);
    532  1.25       cgd 	if (!dev)
    533  1.25       cgd 	    panic("config_attach: memory allocation for device softc failed");
    534  1.25       cgd 	bzero(dev, ca->ca_devsize);
    535  1.25       cgd 	TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);	/* link up */
    536  1.25       cgd 	dev->dv_class = cd->cd_class;
    537  1.25       cgd 	dev->dv_cfdata = cf;
    538  1.25       cgd 	dev->dv_unit = myunit;
    539  1.25       cgd 	bcopy(cd->cd_name, dev->dv_xname, lname);
    540  1.25       cgd 	bcopy(xunit, dev->dv_xname + lname, lunit);
    541  1.25       cgd 	dev->dv_parent = parent;
    542  1.29   thorpej 
    543  1.25       cgd 	if (parent == ROOT)
    544  1.25       cgd 		printf("%s (root)", dev->dv_xname);
    545  1.25       cgd 	else {
    546  1.25       cgd 		printf("%s at %s", dev->dv_xname, parent->dv_xname);
    547  1.25       cgd 		if (print)
    548  1.25       cgd 			(void) (*print)(aux, (char *)0);
    549  1.25       cgd 	}
    550  1.25       cgd 
    551  1.25       cgd 	/* put this device in the devices array */
    552  1.25       cgd 	if (dev->dv_unit >= cd->cd_ndevs) {
    553  1.25       cgd 		/*
    554  1.25       cgd 		 * Need to expand the array.
    555  1.25       cgd 		 */
    556  1.25       cgd 		int old = cd->cd_ndevs, new;
    557  1.25       cgd 		void **nsp;
    558  1.25       cgd 
    559  1.25       cgd 		if (old == 0)
    560  1.25       cgd 			new = MINALLOCSIZE / sizeof(void *);
    561  1.25       cgd 		else
    562  1.25       cgd 			new = old * 2;
    563  1.25       cgd 		while (new <= dev->dv_unit)
    564  1.25       cgd 			new *= 2;
    565  1.25       cgd 		cd->cd_ndevs = new;
    566  1.25       cgd 		nsp = malloc(new * sizeof(void *), M_DEVBUF, M_NOWAIT);
    567  1.25       cgd 		if (nsp == 0)
    568  1.25       cgd 			panic("config_attach: %sing dev array",
    569  1.25       cgd 			    old != 0 ? "expand" : "creat");
    570  1.25       cgd 		bzero(nsp + old, (new - old) * sizeof(void *));
    571  1.25       cgd 		if (old != 0) {
    572  1.25       cgd 			bcopy(cd->cd_devs, nsp, old * sizeof(void *));
    573  1.25       cgd 			free(cd->cd_devs, M_DEVBUF);
    574  1.25       cgd 		}
    575  1.25       cgd 		cd->cd_devs = nsp;
    576  1.25       cgd 	}
    577  1.25       cgd 	if (cd->cd_devs[dev->dv_unit])
    578  1.25       cgd 		panic("config_attach: duplicate %s", dev->dv_xname);
    579  1.25       cgd 	cd->cd_devs[dev->dv_unit] = dev;
    580  1.25       cgd 
    581  1.25       cgd 	/*
    582  1.25       cgd 	 * Before attaching, clobber any unfound devices that are
    583  1.25       cgd 	 * otherwise identical, or bump the unit number on all starred
    584  1.25       cgd 	 * cfdata for this device.
    585  1.25       cgd 	 */
    586  1.25       cgd 	for (cf = cfdata; cf->cf_driver; cf++)
    587  1.25       cgd 		if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) {
    588  1.25       cgd 			if (cf->cf_fstate == FSTATE_NOTFOUND)
    589  1.25       cgd 				cf->cf_fstate = FSTATE_FOUND;
    590  1.25       cgd 			if (cf->cf_fstate == FSTATE_STAR)
    591  1.25       cgd 				cf->cf_unit++;
    592  1.25       cgd 		}
    593  1.27  drochner #if defined(__alpha__) || defined(hp300) || defined(__i386__)
    594  1.25       cgd 	device_register(dev, aux);
    595  1.25       cgd #endif
    596  1.25       cgd 	(*ca->ca_attach)(parent, dev, aux);
    597  1.29   thorpej 	config_process_deferred_children(dev);
    598  1.25       cgd 	return (dev);
    599  1.25       cgd }
    600  1.25       cgd #endif /* __BROKEN_INDIRECT_CONFIG */
    601  1.29   thorpej 
    602  1.29   thorpej /*
    603  1.29   thorpej  * Defer the configuration of the specified device until all
    604  1.29   thorpej  * of its parent's devices have been attached.
    605  1.29   thorpej  */
    606  1.29   thorpej void
    607  1.29   thorpej config_defer(dev, func)
    608  1.29   thorpej 	struct device *dev;
    609  1.29   thorpej 	void (*func) __P((struct device *));
    610  1.29   thorpej {
    611  1.29   thorpej 	struct deferred_config *dc;
    612  1.29   thorpej 
    613  1.29   thorpej 	if (dev->dv_parent == NULL)
    614  1.29   thorpej 		panic("config_defer: can't defer config of a root device");
    615  1.29   thorpej 
    616  1.29   thorpej #ifdef DIAGNOSTIC
    617  1.29   thorpej 	for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
    618  1.29   thorpej 	     dc = TAILQ_NEXT(dc, dc_queue)) {
    619  1.29   thorpej 		if (dc->dc_dev == dev)
    620  1.29   thorpej 			panic("config_defer: deferred twice");
    621  1.29   thorpej 	}
    622  1.29   thorpej #endif
    623  1.29   thorpej 
    624  1.29   thorpej 	if ((dc = malloc(sizeof(*dc), M_DEVBUF, M_NOWAIT)) == NULL)
    625  1.29   thorpej 		panic("config_defer: can't allocate defer structure");
    626  1.29   thorpej 
    627  1.29   thorpej 	dc->dc_dev = dev;
    628  1.29   thorpej 	dc->dc_func = func;
    629  1.29   thorpej 	TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
    630  1.29   thorpej }
    631  1.29   thorpej 
    632  1.29   thorpej /*
    633  1.29   thorpej  * Process the deferred configuration queue for a device.
    634  1.29   thorpej  */
    635  1.29   thorpej static void
    636  1.29   thorpej config_process_deferred_children(parent)
    637  1.29   thorpej 	struct device *parent;
    638  1.29   thorpej {
    639  1.29   thorpej 	struct deferred_config *dc, *ndc;
    640  1.29   thorpej 
    641  1.29   thorpej 	for (dc = TAILQ_FIRST(&deferred_config_queue);
    642  1.29   thorpej 	     dc != NULL; dc = ndc) {
    643  1.29   thorpej 		ndc = TAILQ_NEXT(dc, dc_queue);
    644  1.29   thorpej 		if (dc->dc_dev->dv_parent == parent) {
    645  1.29   thorpej 			TAILQ_REMOVE(&deferred_config_queue, dc, dc_queue);
    646  1.29   thorpej 			(*dc->dc_func)(dc->dc_dev);
    647  1.29   thorpej 			free(dc, M_DEVBUF);
    648  1.29   thorpej 		}
    649  1.29   thorpej 	}
    650  1.29   thorpej }
    651   1.1     glass 
    652   1.1     glass /*
    653   1.1     glass  * Attach an event.  These must come from initially-zero space (see
    654   1.1     glass  * commented-out assignments below), but that occurs naturally for
    655   1.1     glass  * device instance variables.
    656   1.1     glass  */
    657   1.1     glass void
    658   1.1     glass evcnt_attach(dev, name, ev)
    659   1.1     glass 	struct device *dev;
    660   1.1     glass 	const char *name;
    661   1.1     glass 	struct evcnt *ev;
    662   1.1     glass {
    663   1.1     glass 
    664   1.1     glass #ifdef DIAGNOSTIC
    665   1.1     glass 	if (strlen(name) >= sizeof(ev->ev_name))
    666   1.1     glass 		panic("evcnt_attach");
    667   1.1     glass #endif
    668   1.1     glass 	/* ev->ev_next = NULL; */
    669   1.1     glass 	ev->ev_dev = dev;
    670   1.1     glass 	/* ev->ev_count = 0; */
    671   1.1     glass 	strcpy(ev->ev_name, name);
    672  1.20       cgd 	TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
    673   1.1     glass }
    674