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