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