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