subr_autoconf.c revision 1541
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1992, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This software was developed by the Computer Systems Engineering group 61541Srgrimes * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 71541Srgrimes * contributed to Berkeley. 81541Srgrimes * 91541Srgrimes * All advertising materials mentioning features or use of this software 101541Srgrimes * must display the following acknowledgement: 111541Srgrimes * This product includes software developed by the University of 121541Srgrimes * California, Lawrence Berkeley Laboratories. 131541Srgrimes * 141541Srgrimes * Redistribution and use in source and binary forms, with or without 151541Srgrimes * modification, are permitted provided that the following conditions 161541Srgrimes * are met: 171541Srgrimes * 1. Redistributions of source code must retain the above copyright 181541Srgrimes * notice, this list of conditions and the following disclaimer. 191541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 201541Srgrimes * notice, this list of conditions and the following disclaimer in the 211541Srgrimes * documentation and/or other materials provided with the distribution. 221541Srgrimes * 3. All advertising materials mentioning features or use of this software 231541Srgrimes * must display the following acknowledgement: 241541Srgrimes * This product includes software developed by the University of 251541Srgrimes * California, Berkeley and its contributors. 261541Srgrimes * 4. Neither the name of the University nor the names of its contributors 271541Srgrimes * may be used to endorse or promote products derived from this software 281541Srgrimes * without specific prior written permission. 291541Srgrimes * 301541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 311541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 321541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 331541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 341541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 351541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 361541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 371541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 381541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 391541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 401541Srgrimes * SUCH DAMAGE. 411541Srgrimes * 421541Srgrimes * @(#)subr_autoconf.c 8.1 (Berkeley) 6/10/93 431541Srgrimes * 441541Srgrimes * from: $Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp $ (LBL) 451541Srgrimes */ 461541Srgrimes 471541Srgrimes#include <sys/param.h> 481541Srgrimes#include <sys/device.h> 491541Srgrimes#include <sys/malloc.h> 501541Srgrimes 511541Srgrimes/* 521541Srgrimes * Autoconfiguration subroutines. 531541Srgrimes */ 541541Srgrimes 551541Srgrimes/* 561541Srgrimes * ioconf.c exports exactly two names: cfdata and cfroots. All system 571541Srgrimes * devices and drivers are found via these tables. 581541Srgrimes */ 591541Srgrimesextern struct cfdata cfdata[]; 601541Srgrimesextern short cfroots[]; 611541Srgrimes 621541Srgrimes#define ROOT ((struct device *)NULL) 631541Srgrimes 641541Srgrimesstruct matchinfo { 651541Srgrimes cfmatch_t fn; 661541Srgrimes struct device *parent; 671541Srgrimes void *aux; 681541Srgrimes struct cfdata *match; 691541Srgrimes int pri; 701541Srgrimes}; 711541Srgrimes 721541Srgrimes/* 731541Srgrimes * Apply the matching function and choose the best. This is used 741541Srgrimes * a few times and we want to keep the code small. 751541Srgrimes */ 761541Srgrimesstatic void 771541Srgrimesmapply(m, cf) 781541Srgrimes register struct matchinfo *m; 791541Srgrimes register struct cfdata *cf; 801541Srgrimes{ 811541Srgrimes register int pri; 821541Srgrimes 831541Srgrimes if (m->fn != NULL) 841541Srgrimes pri = (*m->fn)(m->parent, cf, m->aux); 851541Srgrimes else 861541Srgrimes pri = (*cf->cf_driver->cd_match)(m->parent, cf, m->aux); 871541Srgrimes if (pri > m->pri) { 881541Srgrimes m->match = cf; 891541Srgrimes m->pri = pri; 901541Srgrimes } 911541Srgrimes} 921541Srgrimes 931541Srgrimes/* 941541Srgrimes * Iterate over all potential children of some device, calling the given 951541Srgrimes * function (default being the child's match function) for each one. 961541Srgrimes * Nonzero returns are matches; the highest value returned is considered 971541Srgrimes * the best match. Return the `found child' if we got a match, or NULL 981541Srgrimes * otherwise. The `aux' pointer is simply passed on through. 991541Srgrimes * 1001541Srgrimes * Note that this function is designed so that it can be used to apply 1011541Srgrimes * an arbitrary function to all potential children (its return value 1021541Srgrimes * can be ignored). 1031541Srgrimes */ 1041541Srgrimesstruct cfdata * 1051541Srgrimesconfig_search(fn, parent, aux) 1061541Srgrimes cfmatch_t fn; 1071541Srgrimes register struct device *parent; 1081541Srgrimes void *aux; 1091541Srgrimes{ 1101541Srgrimes register struct cfdata *cf; 1111541Srgrimes register short *p; 1121541Srgrimes struct matchinfo m; 1131541Srgrimes 1141541Srgrimes m.fn = fn; 1151541Srgrimes m.parent = parent; 1161541Srgrimes m.aux = aux; 1171541Srgrimes m.match = NULL; 1181541Srgrimes m.pri = 0; 1191541Srgrimes for (cf = cfdata; cf->cf_driver; cf++) { 1201541Srgrimes /* 1211541Srgrimes * Skip cf if no longer eligible, otherwise scan through 1221541Srgrimes * parents for one matching `parent', and try match function. 1231541Srgrimes */ 1241541Srgrimes if (cf->cf_fstate == FSTATE_FOUND) 1251541Srgrimes continue; 1261541Srgrimes for (p = cf->cf_parents; *p >= 0; p++) 1271541Srgrimes if (parent->dv_cfdata == &cfdata[*p]) 1281541Srgrimes mapply(&m, cf); 1291541Srgrimes } 1301541Srgrimes return (m.match); 1311541Srgrimes} 1321541Srgrimes 1331541Srgrimes/* 1341541Srgrimes * Find the given root device. 1351541Srgrimes * This is much like config_search, but there is no parent. 1361541Srgrimes */ 1371541Srgrimesstruct cfdata * 1381541Srgrimesconfig_rootsearch(fn, rootname, aux) 1391541Srgrimes register cfmatch_t fn; 1401541Srgrimes register char *rootname; 1411541Srgrimes register void *aux; 1421541Srgrimes{ 1431541Srgrimes register struct cfdata *cf; 1441541Srgrimes register short *p; 1451541Srgrimes struct matchinfo m; 1461541Srgrimes 1471541Srgrimes m.fn = fn; 1481541Srgrimes m.parent = ROOT; 1491541Srgrimes m.aux = aux; 1501541Srgrimes m.match = NULL; 1511541Srgrimes m.pri = 0; 1521541Srgrimes /* 1531541Srgrimes * Look at root entries for matching name. We do not bother 1541541Srgrimes * with found-state here since only one root should ever be 1551541Srgrimes * searched (and it must be done first). 1561541Srgrimes */ 1571541Srgrimes for (p = cfroots; *p >= 0; p++) { 1581541Srgrimes cf = &cfdata[*p]; 1591541Srgrimes if (strcmp(cf->cf_driver->cd_name, rootname) == 0) 1601541Srgrimes mapply(&m, cf); 1611541Srgrimes } 1621541Srgrimes return (m.match); 1631541Srgrimes} 1641541Srgrimes 1651541Srgrimesstatic char *msgs[3] = { "", " not configured\n", " unsupported\n" }; 1661541Srgrimes 1671541Srgrimes/* 1681541Srgrimes * The given `aux' argument describes a device that has been found 1691541Srgrimes * on the given parent, but not necessarily configured. Locate the 1701541Srgrimes * configuration data for that device (using the cd_match configuration 1711541Srgrimes * driver function) and attach it, and return true. If the device was 1721541Srgrimes * not configured, call the given `print' function and return 0. 1731541Srgrimes */ 1741541Srgrimesint 1751541Srgrimesconfig_found(parent, aux, print) 1761541Srgrimes struct device *parent; 1771541Srgrimes void *aux; 1781541Srgrimes cfprint_t print; 1791541Srgrimes{ 1801541Srgrimes struct cfdata *cf; 1811541Srgrimes 1821541Srgrimes if ((cf = config_search((cfmatch_t)NULL, parent, aux)) != NULL) { 1831541Srgrimes config_attach(parent, cf, aux, print); 1841541Srgrimes return (1); 1851541Srgrimes } 1861541Srgrimes printf(msgs[(*print)(aux, parent->dv_xname)]); 1871541Srgrimes return (0); 1881541Srgrimes} 1891541Srgrimes 1901541Srgrimes/* 1911541Srgrimes * As above, but for root devices. 1921541Srgrimes */ 1931541Srgrimesint 1941541Srgrimesconfig_rootfound(rootname, aux) 1951541Srgrimes char *rootname; 1961541Srgrimes void *aux; 1971541Srgrimes{ 1981541Srgrimes struct cfdata *cf; 1991541Srgrimes 2001541Srgrimes if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL) { 2011541Srgrimes config_attach(ROOT, cf, aux, (cfprint_t)NULL); 2021541Srgrimes return (1); 2031541Srgrimes } 2041541Srgrimes printf("root device %s not configured\n", rootname); 2051541Srgrimes return (0); 2061541Srgrimes} 2071541Srgrimes 2081541Srgrimes/* just like sprintf(buf, "%d") except that it works from the end */ 2091541Srgrimesstatic char * 2101541Srgrimesnumber(ep, n) 2111541Srgrimes register char *ep; 2121541Srgrimes register int n; 2131541Srgrimes{ 2141541Srgrimes 2151541Srgrimes *--ep = 0; 2161541Srgrimes while (n >= 10) { 2171541Srgrimes *--ep = (n % 10) + '0'; 2181541Srgrimes n /= 10; 2191541Srgrimes } 2201541Srgrimes *--ep = n + '0'; 2211541Srgrimes return (ep); 2221541Srgrimes} 2231541Srgrimes 2241541Srgrimes/* 2251541Srgrimes * Attach a found device. Allocates memory for device variables. 2261541Srgrimes */ 2271541Srgrimesvoid 2281541Srgrimesconfig_attach(parent, cf, aux, print) 2291541Srgrimes register struct device *parent; 2301541Srgrimes register struct cfdata *cf; 2311541Srgrimes register void *aux; 2321541Srgrimes cfprint_t print; 2331541Srgrimes{ 2341541Srgrimes register struct device *dev; 2351541Srgrimes register struct cfdriver *cd; 2361541Srgrimes register size_t lname, lunit; 2371541Srgrimes register char *xunit; 2381541Srgrimes int myunit; 2391541Srgrimes char num[10]; 2401541Srgrimes static struct device **nextp = &alldevs; 2411541Srgrimes 2421541Srgrimes cd = cf->cf_driver; 2431541Srgrimes if (cd->cd_devsize < sizeof(struct device)) 2441541Srgrimes panic("config_attach"); 2451541Srgrimes myunit = cf->cf_unit; 2461541Srgrimes if (cf->cf_fstate == FSTATE_NOTFOUND) 2471541Srgrimes cf->cf_fstate = FSTATE_FOUND; 2481541Srgrimes else 2491541Srgrimes cf->cf_unit++; 2501541Srgrimes 2511541Srgrimes /* compute length of name and decimal expansion of unit number */ 2521541Srgrimes lname = strlen(cd->cd_name); 2531541Srgrimes xunit = number(&num[sizeof num], myunit); 2541541Srgrimes lunit = &num[sizeof num] - xunit; 2551541Srgrimes if (lname + lunit >= sizeof(dev->dv_xname)) 2561541Srgrimes panic("config_attach: device name too long"); 2571541Srgrimes 2581541Srgrimes /* get memory for all device vars */ 2591541Srgrimes dev = (struct device *)malloc(cd->cd_devsize, M_DEVBUF, M_WAITOK); 2601541Srgrimes /* XXX cannot wait! */ 2611541Srgrimes bzero(dev, cd->cd_devsize); 2621541Srgrimes *nextp = dev; /* link up */ 2631541Srgrimes nextp = &dev->dv_next; 2641541Srgrimes dev->dv_class = cd->cd_class; 2651541Srgrimes dev->dv_cfdata = cf; 2661541Srgrimes dev->dv_unit = myunit; 2671541Srgrimes bcopy(cd->cd_name, dev->dv_xname, lname); 2681541Srgrimes bcopy(xunit, dev->dv_xname + lname, lunit); 2691541Srgrimes dev->dv_parent = parent; 2701541Srgrimes if (parent == ROOT) 2711541Srgrimes printf("%s (root)", dev->dv_xname); 2721541Srgrimes else { 2731541Srgrimes printf("%s at %s", dev->dv_xname, parent->dv_xname); 2741541Srgrimes (void) (*print)(aux, (char *)0); 2751541Srgrimes } 2761541Srgrimes 2771541Srgrimes /* put this device in the devices array */ 2781541Srgrimes if (dev->dv_unit >= cd->cd_ndevs) { 2791541Srgrimes /* 2801541Srgrimes * Need to expand the array. 2811541Srgrimes */ 2821541Srgrimes int old = cd->cd_ndevs, oldbytes, new, newbytes; 2831541Srgrimes void **nsp; 2841541Srgrimes 2851541Srgrimes if (old == 0) { 2861541Srgrimes nsp = malloc(MINALLOCSIZE, M_DEVBUF, M_WAITOK); /*XXX*/ 2871541Srgrimes bzero(nsp, MINALLOCSIZE); 2881541Srgrimes cd->cd_ndevs = MINALLOCSIZE / sizeof(void *); 2891541Srgrimes } else { 2901541Srgrimes new = cd->cd_ndevs; 2911541Srgrimes do { 2921541Srgrimes new *= 2; 2931541Srgrimes } while (new <= dev->dv_unit); 2941541Srgrimes cd->cd_ndevs = new; 2951541Srgrimes oldbytes = old * sizeof(void *); 2961541Srgrimes newbytes = new * sizeof(void *); 2971541Srgrimes nsp = malloc(newbytes, M_DEVBUF, M_WAITOK); /*XXX*/ 2981541Srgrimes bcopy(cd->cd_devs, nsp, oldbytes); 2991541Srgrimes bzero(&nsp[old], newbytes - oldbytes); 3001541Srgrimes free(cd->cd_devs, M_DEVBUF); 3011541Srgrimes } 3021541Srgrimes cd->cd_devs = nsp; 3031541Srgrimes } 3041541Srgrimes if (cd->cd_devs[dev->dv_unit]) 3051541Srgrimes panic("config_attach: duplicate %s", dev->dv_xname); 3061541Srgrimes cd->cd_devs[dev->dv_unit] = dev; 3071541Srgrimes 3081541Srgrimes /* 3091541Srgrimes * Before attaching, clobber any unfound devices that are 3101541Srgrimes * otherwise identical. 3111541Srgrimes */ 3121541Srgrimes for (cf = cfdata; cf->cf_driver; cf++) 3131541Srgrimes if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit && 3141541Srgrimes cf->cf_fstate == FSTATE_NOTFOUND) 3151541Srgrimes cf->cf_fstate = FSTATE_FOUND; 3161541Srgrimes (*cd->cd_attach)(parent, dev, aux); 3171541Srgrimes} 3181541Srgrimes 3191541Srgrimes/* 3201541Srgrimes * Attach an event. These must come from initially-zero space (see 3211541Srgrimes * commented-out assignments below), but that occurs naturally for 3221541Srgrimes * device instance variables. 3231541Srgrimes */ 3241541Srgrimesvoid 3251541Srgrimesevcnt_attach(dev, name, ev) 3261541Srgrimes struct device *dev; 3271541Srgrimes const char *name; 3281541Srgrimes struct evcnt *ev; 3291541Srgrimes{ 3301541Srgrimes static struct evcnt **nextp = &allevents; 3311541Srgrimes 3321541Srgrimes#ifdef DIAGNOSTIC 3331541Srgrimes if (strlen(name) >= sizeof(ev->ev_name)) 3341541Srgrimes panic("evcnt_attach"); 3351541Srgrimes#endif 3361541Srgrimes /* ev->ev_next = NULL; */ 3371541Srgrimes ev->ev_dev = dev; 3381541Srgrimes /* ev->ev_count = 0; */ 3391541Srgrimes strcpy(ev->ev_name, name); 3401541Srgrimes *nextp = ev; 3411541Srgrimes nextp = &ev->ev_next; 3421541Srgrimes} 343