Home | History | Annotate | Line # | Download | only in kern
subr_autoconf.c revision 1.1.1.1
      1 /*
      2  * Copyright (c) 1992, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This software was developed by the Computer Systems Engineering group
      6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      7  * contributed to Berkeley.
      8  *
      9  * All advertising materials mentioning features or use of this software
     10  * must display the following acknowledgement:
     11  *	This product includes software developed by the University of
     12  *	California, Lawrence Berkeley Laboratories.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. All advertising materials mentioning features or use of this software
     23  *    must display the following acknowledgement:
     24  *	This product includes software developed by the University of
     25  *	California, Berkeley and its contributors.
     26  * 4. Neither the name of the University nor the names of its contributors
     27  *    may be used to endorse or promote products derived from this software
     28  *    without specific prior written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     40  * SUCH DAMAGE.
     41  *
     42  *	@(#)subr_autoconf.c	8.1 (Berkeley) 6/10/93
     43  *
     44  * from: $Header: /tank/opengrok/rsync2/NetBSD/src/sys/kern/subr_autoconf.c,v 1.1.1.1 1998/03/01 02:09:44 fvdl Exp $ (LBL)
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/device.h>
     49 #include <sys/malloc.h>
     50 
     51 /*
     52  * Autoconfiguration subroutines.
     53  */
     54 
     55 /*
     56  * ioconf.c exports exactly two names: cfdata and cfroots.  All system
     57  * devices and drivers are found via these tables.
     58  */
     59 extern struct cfdata cfdata[];
     60 extern short cfroots[];
     61 
     62 #define	ROOT ((struct device *)NULL)
     63 
     64 struct matchinfo {
     65 	cfmatch_t fn;
     66 	struct	device *parent;
     67 	void	*aux;
     68 	struct	cfdata *match;
     69 	int	pri;
     70 };
     71 
     72 /*
     73  * Apply the matching function and choose the best.  This is used
     74  * a few times and we want to keep the code small.
     75  */
     76 static void
     77 mapply(m, cf)
     78 	register struct matchinfo *m;
     79 	register struct cfdata *cf;
     80 {
     81 	register int pri;
     82 
     83 	if (m->fn != NULL)
     84 		pri = (*m->fn)(m->parent, cf, m->aux);
     85 	else
     86 		pri = (*cf->cf_driver->cd_match)(m->parent, cf, m->aux);
     87 	if (pri > m->pri) {
     88 		m->match = cf;
     89 		m->pri = pri;
     90 	}
     91 }
     92 
     93 /*
     94  * Iterate over all potential children of some device, calling the given
     95  * function (default being the child's match function) for each one.
     96  * Nonzero returns are matches; the highest value returned is considered
     97  * the best match.  Return the `found child' if we got a match, or NULL
     98  * otherwise.  The `aux' pointer is simply passed on through.
     99  *
    100  * Note that this function is designed so that it can be used to apply
    101  * an arbitrary function to all potential children (its return value
    102  * can be ignored).
    103  */
    104 struct cfdata *
    105 config_search(fn, parent, aux)
    106 	cfmatch_t fn;
    107 	register struct device *parent;
    108 	void *aux;
    109 {
    110 	register struct cfdata *cf;
    111 	register short *p;
    112 	struct matchinfo m;
    113 
    114 	m.fn = fn;
    115 	m.parent = parent;
    116 	m.aux = aux;
    117 	m.match = NULL;
    118 	m.pri = 0;
    119 	for (cf = cfdata; cf->cf_driver; cf++) {
    120 		/*
    121 		 * Skip cf if no longer eligible, otherwise scan through
    122 		 * parents for one matching `parent', and try match function.
    123 		 */
    124 		if (cf->cf_fstate == FSTATE_FOUND)
    125 			continue;
    126 		for (p = cf->cf_parents; *p >= 0; p++)
    127 			if (parent->dv_cfdata == &cfdata[*p])
    128 				mapply(&m, cf);
    129 	}
    130 	return (m.match);
    131 }
    132 
    133 /*
    134  * Find the given root device.
    135  * This is much like config_search, but there is no parent.
    136  */
    137 struct cfdata *
    138 config_rootsearch(fn, rootname, aux)
    139 	register cfmatch_t fn;
    140 	register char *rootname;
    141 	register void *aux;
    142 {
    143 	register struct cfdata *cf;
    144 	register short *p;
    145 	struct matchinfo m;
    146 
    147 	m.fn = fn;
    148 	m.parent = ROOT;
    149 	m.aux = aux;
    150 	m.match = NULL;
    151 	m.pri = 0;
    152 	/*
    153 	 * Look at root entries for matching name.  We do not bother
    154 	 * with found-state here since only one root should ever be
    155 	 * searched (and it must be done first).
    156 	 */
    157 	for (p = cfroots; *p >= 0; p++) {
    158 		cf = &cfdata[*p];
    159 		if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
    160 			mapply(&m, cf);
    161 	}
    162 	return (m.match);
    163 }
    164 
    165 static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
    166 
    167 /*
    168  * The given `aux' argument describes a device that has been found
    169  * on the given parent, but not necessarily configured.  Locate the
    170  * configuration data for that device (using the cd_match configuration
    171  * driver function) and attach it, and return true.  If the device was
    172  * not configured, call the given `print' function and return 0.
    173  */
    174 int
    175 config_found(parent, aux, print)
    176 	struct device *parent;
    177 	void *aux;
    178 	cfprint_t print;
    179 {
    180 	struct cfdata *cf;
    181 
    182 	if ((cf = config_search((cfmatch_t)NULL, parent, aux)) != NULL) {
    183 		config_attach(parent, cf, aux, print);
    184 		return (1);
    185 	}
    186 	printf(msgs[(*print)(aux, parent->dv_xname)]);
    187 	return (0);
    188 }
    189 
    190 /*
    191  * As above, but for root devices.
    192  */
    193 int
    194 config_rootfound(rootname, aux)
    195 	char *rootname;
    196 	void *aux;
    197 {
    198 	struct cfdata *cf;
    199 
    200 	if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL) {
    201 		config_attach(ROOT, cf, aux, (cfprint_t)NULL);
    202 		return (1);
    203 	}
    204 	printf("root device %s not configured\n", rootname);
    205 	return (0);
    206 }
    207 
    208 /* just like sprintf(buf, "%d") except that it works from the end */
    209 static char *
    210 number(ep, n)
    211 	register char *ep;
    212 	register int n;
    213 {
    214 
    215 	*--ep = 0;
    216 	while (n >= 10) {
    217 		*--ep = (n % 10) + '0';
    218 		n /= 10;
    219 	}
    220 	*--ep = n + '0';
    221 	return (ep);
    222 }
    223 
    224 /*
    225  * Attach a found device.  Allocates memory for device variables.
    226  */
    227 void
    228 config_attach(parent, cf, aux, print)
    229 	register struct device *parent;
    230 	register struct cfdata *cf;
    231 	register void *aux;
    232 	cfprint_t print;
    233 {
    234 	register struct device *dev;
    235 	register struct cfdriver *cd;
    236 	register size_t lname, lunit;
    237 	register char *xunit;
    238 	int myunit;
    239 	char num[10];
    240 	static struct device **nextp = &alldevs;
    241 
    242 	cd = cf->cf_driver;
    243 	if (cd->cd_devsize < sizeof(struct device))
    244 		panic("config_attach");
    245 	myunit = cf->cf_unit;
    246 	if (cf->cf_fstate == FSTATE_NOTFOUND)
    247 		cf->cf_fstate = FSTATE_FOUND;
    248 	else
    249 		cf->cf_unit++;
    250 
    251 	/* compute length of name and decimal expansion of unit number */
    252 	lname = strlen(cd->cd_name);
    253 	xunit = number(&num[sizeof num], myunit);
    254 	lunit = &num[sizeof num] - xunit;
    255 	if (lname + lunit >= sizeof(dev->dv_xname))
    256 		panic("config_attach: device name too long");
    257 
    258 	/* get memory for all device vars */
    259 	dev = (struct device *)malloc(cd->cd_devsize, M_DEVBUF, M_WAITOK);
    260 					/* XXX cannot wait! */
    261 	bzero(dev, cd->cd_devsize);
    262 	*nextp = dev;			/* link up */
    263 	nextp = &dev->dv_next;
    264 	dev->dv_class = cd->cd_class;
    265 	dev->dv_cfdata = cf;
    266 	dev->dv_unit = myunit;
    267 	bcopy(cd->cd_name, dev->dv_xname, lname);
    268 	bcopy(xunit, dev->dv_xname + lname, lunit);
    269 	dev->dv_parent = parent;
    270 	if (parent == ROOT)
    271 		printf("%s (root)", dev->dv_xname);
    272 	else {
    273 		printf("%s at %s", dev->dv_xname, parent->dv_xname);
    274 		(void) (*print)(aux, (char *)0);
    275 	}
    276 
    277 	/* put this device in the devices array */
    278 	if (dev->dv_unit >= cd->cd_ndevs) {
    279 		/*
    280 		 * Need to expand the array.
    281 		 */
    282 		int old = cd->cd_ndevs, oldbytes, new, newbytes;
    283 		void **nsp;
    284 
    285 		if (old == 0) {
    286 			nsp = malloc(MINALLOCSIZE, M_DEVBUF, M_WAITOK);	/*XXX*/
    287 			bzero(nsp, MINALLOCSIZE);
    288 			cd->cd_ndevs = MINALLOCSIZE / sizeof(void *);
    289 		} else {
    290 			new = cd->cd_ndevs;
    291 			do {
    292 				new *= 2;
    293 			} while (new <= dev->dv_unit);
    294 			cd->cd_ndevs = new;
    295 			oldbytes = old * sizeof(void *);
    296 			newbytes = new * sizeof(void *);
    297 			nsp = malloc(newbytes, M_DEVBUF, M_WAITOK);	/*XXX*/
    298 			bcopy(cd->cd_devs, nsp, oldbytes);
    299 			bzero(&nsp[old], newbytes - oldbytes);
    300 			free(cd->cd_devs, M_DEVBUF);
    301 		}
    302 		cd->cd_devs = nsp;
    303 	}
    304 	if (cd->cd_devs[dev->dv_unit])
    305 		panic("config_attach: duplicate %s", dev->dv_xname);
    306 	cd->cd_devs[dev->dv_unit] = dev;
    307 
    308 	/*
    309 	 * Before attaching, clobber any unfound devices that are
    310 	 * otherwise identical.
    311 	 */
    312 	for (cf = cfdata; cf->cf_driver; cf++)
    313 		if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit &&
    314 		    cf->cf_fstate == FSTATE_NOTFOUND)
    315 			cf->cf_fstate = FSTATE_FOUND;
    316 	(*cd->cd_attach)(parent, dev, aux);
    317 }
    318 
    319 /*
    320  * Attach an event.  These must come from initially-zero space (see
    321  * commented-out assignments below), but that occurs naturally for
    322  * device instance variables.
    323  */
    324 void
    325 evcnt_attach(dev, name, ev)
    326 	struct device *dev;
    327 	const char *name;
    328 	struct evcnt *ev;
    329 {
    330 	static struct evcnt **nextp = &allevents;
    331 
    332 #ifdef DIAGNOSTIC
    333 	if (strlen(name) >= sizeof(ev->ev_name))
    334 		panic("evcnt_attach");
    335 #endif
    336 	/* ev->ev_next = NULL; */
    337 	ev->ev_dev = dev;
    338 	/* ev->ev_count = 0; */
    339 	strcpy(ev->ev_name, name);
    340 	*nextp = ev;
    341 	nextp = &ev->ev_next;
    342 }
    343