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