Home | History | Annotate | Line # | Download | only in kern
subr_autoconf.c revision 1.17
      1 /*	$NetBSD: subr_autoconf.c,v 1.17 1996/02/04 02:16:35 christos 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.1 (Berkeley) 6/10/93
     47  */
     48 
     49 #include <sys/param.h>
     50 #include <sys/device.h>
     51 #include <sys/malloc.h>
     52 #include <sys/systm.h>
     53 #include <machine/limits.h>
     54 
     55 /*
     56  * Autoconfiguration subroutines.
     57  */
     58 
     59 /*
     60  * ioconf.c exports exactly two names: cfdata and cfroots.  All system
     61  * devices and drivers are found via these tables.
     62  */
     63 extern struct cfdata cfdata[];
     64 extern short cfroots[];
     65 
     66 #define	ROOT ((struct device *)NULL)
     67 
     68 struct device *config_make_softc __P((struct device *, struct cfdata *));
     69 
     70 struct matchinfo {
     71 	cfmatch_t fn;
     72 	struct	device *parent;
     73 	void	*match, *aux;
     74 	int	indirect, pri;
     75 };
     76 
     77 static char *number __P((char *, int));
     78 static void mapply __P((struct matchinfo *, struct cfdata *));
     79 
     80 /*
     81  * Apply the matching function and choose the best.  This is used
     82  * a few times and we want to keep the code small.
     83  */
     84 static void
     85 mapply(m, cf)
     86 	register struct matchinfo *m;
     87 	register struct cfdata *cf;
     88 {
     89 	register int pri;
     90 	void *match;
     91 
     92 	if (m->indirect)
     93 		match = config_make_softc(m->parent, cf);
     94 	else
     95 		match = cf;
     96 
     97 	if (m->fn != NULL)
     98 		pri = (*m->fn)(m->parent, match, m->aux);
     99 	else {
    100 	        if (cf->cf_driver->cd_match == NULL) {
    101 			panic("mapply: no match function for '%s' device\n",
    102 			    cf->cf_driver->cd_name);
    103 		}
    104 		pri = (*cf->cf_driver->cd_match)(m->parent, match, m->aux);
    105 	}
    106 
    107 	if (pri > m->pri) {
    108 		if (m->indirect && m->match)
    109 			free(m->match, M_DEVBUF);
    110 		m->match = match;
    111 		m->pri = pri;
    112 	} else {
    113 		if (m->indirect)
    114 			free(match, M_DEVBUF);
    115 	}
    116 }
    117 
    118 /*
    119  * Iterate over all potential children of some device, calling the given
    120  * function (default being the child's match function) for each one.
    121  * Nonzero returns are matches; the highest value returned is considered
    122  * the best match.  Return the `found child' if we got a match, or NULL
    123  * otherwise.  The `aux' pointer is simply passed on through.
    124  *
    125  * Note that this function is designed so that it can be used to apply
    126  * an arbitrary function to all potential children (its return value
    127  * can be ignored).
    128  */
    129 void *
    130 config_search(fn, parent, aux)
    131 	cfmatch_t fn;
    132 	register struct device *parent;
    133 	void *aux;
    134 {
    135 	register struct cfdata *cf;
    136 	register short *p;
    137 	struct matchinfo m;
    138 
    139 	m.fn = fn;
    140 	m.parent = parent;
    141 	m.match = NULL;
    142 	m.aux = aux;
    143 	m.indirect = parent && parent->dv_cfdata->cf_driver->cd_indirect;
    144 	m.pri = 0;
    145 	for (cf = cfdata; cf->cf_driver; cf++) {
    146 		/*
    147 		 * Skip cf if no longer eligible, otherwise scan through
    148 		 * parents for one matching `parent', and try match function.
    149 		 */
    150 		if (cf->cf_fstate == FSTATE_FOUND)
    151 			continue;
    152 		for (p = cf->cf_parents; *p >= 0; p++)
    153 			if (parent->dv_cfdata == &cfdata[*p])
    154 				mapply(&m, cf);
    155 	}
    156 	return (m.match);
    157 }
    158 
    159 /*
    160  * Iterate over all potential children of some device, calling the given
    161  * function for each one.
    162  *
    163  * Note that this function is designed so that it can be used to apply
    164  * an arbitrary function to all potential children (its return value
    165  * can be ignored).
    166  */
    167 void
    168 config_scan(fn, parent)
    169 	cfscan_t fn;
    170 	register struct device *parent;
    171 {
    172 	register struct cfdata *cf;
    173 	register short *p;
    174 	void *match;
    175 	int indirect;
    176 
    177 	indirect = parent && parent->dv_cfdata->cf_driver->cd_indirect;
    178 	for (cf = cfdata; cf->cf_driver; cf++) {
    179 		/*
    180 		 * Skip cf if no longer eligible, otherwise scan through
    181 		 * parents for one matching `parent', and try match function.
    182 		 */
    183 		if (cf->cf_fstate == FSTATE_FOUND)
    184 			continue;
    185 		for (p = cf->cf_parents; *p >= 0; p++)
    186 			if (parent->dv_cfdata == &cfdata[*p]) {
    187 				if (indirect)
    188 					match = config_make_softc(parent, cf);
    189 				else
    190 					match = cf;
    191 				(*fn)(parent, match);
    192 			}
    193 	}
    194 }
    195 
    196 /*
    197  * Find the given root device.
    198  * This is much like config_search, but there is no parent.
    199  */
    200 void *
    201 config_rootsearch(fn, rootname, aux)
    202 	register cfmatch_t fn;
    203 	register char *rootname;
    204 	register void *aux;
    205 {
    206 	register struct cfdata *cf;
    207 	register short *p;
    208 	struct matchinfo m;
    209 
    210 	m.fn = fn;
    211 	m.parent = ROOT;
    212 	m.match = NULL;
    213 	m.aux = aux;
    214 	m.indirect = 0;
    215 	m.pri = 0;
    216 	/*
    217 	 * Look at root entries for matching name.  We do not bother
    218 	 * with found-state here since only one root should ever be
    219 	 * searched (and it must be done first).
    220 	 */
    221 	for (p = cfroots; *p >= 0; p++) {
    222 		cf = &cfdata[*p];
    223 		if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
    224 			mapply(&m, cf);
    225 	}
    226 	return (m.match);
    227 }
    228 
    229 static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
    230 
    231 /*
    232  * The given `aux' argument describes a device that has been found
    233  * on the given parent, but not necessarily configured.  Locate the
    234  * configuration data for that device (using the cd_match configuration
    235  * driver function) and attach it, and return true.  If the device was
    236  * not configured, call the given `print' function and return 0.
    237  */
    238 int
    239 config_found(parent, aux, print)
    240 	struct device *parent;
    241 	void *aux;
    242 	cfprint_t print;
    243 {
    244 	void *match;
    245 
    246 	if ((match = config_search((cfmatch_t)NULL, parent, aux)) != NULL) {
    247 		config_attach(parent, match, aux, print);
    248 		return (1);
    249 	}
    250 	if (print)
    251 		printf(msgs[(*print)(aux, parent->dv_xname)]);
    252 	return (0);
    253 }
    254 
    255 /*
    256  * As above, but for root devices.
    257  */
    258 int
    259 config_rootfound(rootname, aux)
    260 	char *rootname;
    261 	void *aux;
    262 {
    263 	void *match;
    264 
    265 	if ((match = config_rootsearch((cfmatch_t)NULL, rootname, aux))
    266 	    != NULL) {
    267 		config_attach(ROOT, match, aux, (cfprint_t)NULL);
    268 		return (1);
    269 	}
    270 	printf("root device %s not configured\n", rootname);
    271 	return (0);
    272 }
    273 
    274 /* just like sprintf(buf, "%d") except that it works from the end */
    275 static char *
    276 number(ep, n)
    277 	register char *ep;
    278 	register int n;
    279 {
    280 
    281 	*--ep = 0;
    282 	while (n >= 10) {
    283 		*--ep = (n % 10) + '0';
    284 		n /= 10;
    285 	}
    286 	*--ep = n + '0';
    287 	return (ep);
    288 }
    289 
    290 /*
    291  * Attach a found device.  Allocates memory for device variables.
    292  */
    293 void
    294 config_attach(parent, match, aux, print)
    295 	register struct device *parent;
    296 	void *match;
    297 	register void *aux;
    298 	cfprint_t print;
    299 {
    300 	register struct cfdata *cf;
    301 	register struct device *dev;
    302 	register struct cfdriver *cd;
    303 	static struct device **nextp = &alldevs;
    304 
    305 	if (parent && parent->dv_cfdata->cf_driver->cd_indirect) {
    306 		dev = match;
    307 		cf = dev->dv_cfdata;
    308 	} else {
    309 		cf = match;
    310 		dev = config_make_softc(parent, cf);
    311 	}
    312 
    313 	cd = cf->cf_driver;
    314 	cd->cd_devs[cf->cf_unit] = dev;
    315 
    316 	if (cf->cf_fstate == FSTATE_STAR)
    317 		cf->cf_unit++;
    318 	else
    319 		cf->cf_fstate = FSTATE_FOUND;
    320 
    321 	*nextp = dev;			/* link up */
    322 	nextp = &dev->dv_next;
    323 
    324 	if (parent == ROOT)
    325 		printf("%s (root)", dev->dv_xname);
    326 	else {
    327 		printf("%s at %s", dev->dv_xname, parent->dv_xname);
    328 		if (print)
    329 			(void) (*print)(aux, (char *)0);
    330 	}
    331 
    332 	/*
    333 	 * Before attaching, clobber any unfound devices that are
    334 	 * otherwise identical.
    335 	 */
    336 	for (cf = cfdata; cf->cf_driver; cf++)
    337 		if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit &&
    338 		    cf->cf_fstate == FSTATE_NOTFOUND)
    339 			cf->cf_fstate = FSTATE_FOUND;
    340 	(*cd->cd_attach)(parent, dev, aux);
    341 }
    342 
    343 struct device *
    344 config_make_softc(parent, cf)
    345 	struct device *parent;
    346 	struct cfdata *cf;
    347 {
    348 	register struct device *dev;
    349 	register struct cfdriver *cd;
    350 	register size_t lname, lunit;
    351 	register char *xunit;
    352 	char num[10];
    353 
    354 	cd = cf->cf_driver;
    355 	if (cd->cd_devsize < sizeof(struct device))
    356 		panic("config_make_softc");
    357 
    358 	/* compute length of name and decimal expansion of unit number */
    359 	lname = strlen(cd->cd_name);
    360 	xunit = number(&num[sizeof num], cf->cf_unit);
    361 	lunit = &num[sizeof num] - xunit;
    362 	if (lname + lunit >= sizeof(dev->dv_xname))
    363 		panic("config_attach: device name too long");
    364 
    365 	/* get memory for all device vars */
    366 	dev = (struct device *)malloc(cd->cd_devsize, M_DEVBUF, M_NOWAIT);
    367 	if (!dev)
    368 	    panic("config_attach: memory allocation for device softc failed");
    369 	bzero(dev, cd->cd_devsize);
    370 	dev->dv_class = cd->cd_class;
    371 	dev->dv_cfdata = cf;
    372 	dev->dv_unit = cf->cf_unit;
    373 	bcopy(cd->cd_name, dev->dv_xname, lname);
    374 	bcopy(xunit, dev->dv_xname + lname, lunit);
    375 	dev->dv_parent = parent;
    376 
    377 	/* put this device in the devices array */
    378 	if (dev->dv_unit >= cd->cd_ndevs) {
    379 		/*
    380 		 * Need to expand the array.
    381 		 */
    382 		int old = cd->cd_ndevs, new;
    383 		void **nsp;
    384 
    385 		if (old == 0)
    386 			new = MINALLOCSIZE / sizeof(void *);
    387 		else
    388 			new = old * 2;
    389 		while (new <= dev->dv_unit)
    390 			new *= 2;
    391 		cd->cd_ndevs = new;
    392 		nsp = malloc(new * sizeof(void *), M_DEVBUF, M_NOWAIT);
    393 		if (nsp == 0)
    394 			panic("config_attach: %sing dev array",
    395 			    old != 0 ? "expand" : "creat");
    396 		bzero(nsp + old, (new - old) * sizeof(void *));
    397 		if (old != 0) {
    398 			bcopy(cd->cd_devs, nsp, old * sizeof(void *));
    399 			free(cd->cd_devs, M_DEVBUF);
    400 		}
    401 		cd->cd_devs = nsp;
    402 	}
    403 	if (cd->cd_devs[dev->dv_unit])
    404 		panic("config_attach: duplicate %s", dev->dv_xname);
    405 
    406 	return (dev);
    407 }
    408 
    409 /*
    410  * Attach an event.  These must come from initially-zero space (see
    411  * commented-out assignments below), but that occurs naturally for
    412  * device instance variables.
    413  */
    414 void
    415 evcnt_attach(dev, name, ev)
    416 	struct device *dev;
    417 	const char *name;
    418 	struct evcnt *ev;
    419 {
    420 	static struct evcnt **nextp = &allevents;
    421 
    422 #ifdef DIAGNOSTIC
    423 	if (strlen(name) >= sizeof(ev->ev_name))
    424 		panic("evcnt_attach");
    425 #endif
    426 	/* ev->ev_next = NULL; */
    427 	ev->ev_dev = dev;
    428 	/* ev->ev_count = 0; */
    429 	strcpy(ev->ev_name, name);
    430 	*nextp = ev;
    431 	nextp = &ev->ev_next;
    432 }
    433