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