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