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