subr_autoconf.c revision 1.14 1 /* $NetBSD: subr_autoconf.c,v 1.14 1994/11/04 03:07:17 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
54 /*
55 * Autoconfiguration subroutines.
56 */
57
58 /*
59 * ioconf.c exports exactly two names: cfdata and cfroots. All system
60 * devices and drivers are found via these tables.
61 */
62 extern struct cfdata cfdata[];
63 extern short cfroots[];
64
65 #define ROOT ((struct device *)NULL)
66
67 struct matchinfo {
68 cfmatch_t fn;
69 struct device *parent;
70 void *match, *aux;
71 int indirect, pri;
72 };
73
74 struct device *config_make_softc __P((struct device *, struct cfdata *));
75
76 /*
77 * Apply the matching function and choose the best. This is used
78 * a few times and we want to keep the code small.
79 */
80 static void
81 mapply(m, cf)
82 register struct matchinfo *m;
83 register struct cfdata *cf;
84 {
85 register int pri;
86 void *match;
87
88 if (m->indirect) {
89 match = config_make_softc(m->parent, cf);
90 cf->cf_driver->cd_devs[cf->cf_unit] = match;
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 (m->indirect)
105 cf->cf_driver->cd_devs[cf->cf_unit] = NULL;
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 * Find the given root device.
161 * This is much like config_search, but there is no parent.
162 */
163 void *
164 config_rootsearch(fn, rootname, aux)
165 register cfmatch_t fn;
166 register char *rootname;
167 register void *aux;
168 {
169 register struct cfdata *cf;
170 register short *p;
171 struct matchinfo m;
172
173 m.fn = fn;
174 m.parent = ROOT;
175 m.match = NULL;
176 m.aux = aux;
177 m.indirect = 0;
178 m.pri = 0;
179 /*
180 * Look at root entries for matching name. We do not bother
181 * with found-state here since only one root should ever be
182 * searched (and it must be done first).
183 */
184 for (p = cfroots; *p >= 0; p++) {
185 cf = &cfdata[*p];
186 if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
187 mapply(&m, cf);
188 }
189 return (m.match);
190 }
191
192 static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
193
194 /*
195 * The given `aux' argument describes a device that has been found
196 * on the given parent, but not necessarily configured. Locate the
197 * configuration data for that device (using the cd_match configuration
198 * driver function) and attach it, and return true. If the device was
199 * not configured, call the given `print' function and return 0.
200 */
201 int
202 config_found(parent, aux, print)
203 struct device *parent;
204 void *aux;
205 cfprint_t print;
206 {
207 void *match;
208
209 if ((match = config_search((cfmatch_t)NULL, parent, aux)) != NULL) {
210 config_attach(parent, match, aux, print);
211 return (1);
212 }
213 if (print)
214 printf(msgs[(*print)(aux, parent->dv_xname)]);
215 return (0);
216 }
217
218 /*
219 * As above, but for root devices.
220 */
221 int
222 config_rootfound(rootname, aux)
223 char *rootname;
224 void *aux;
225 {
226 void *match;
227
228 if ((match = config_rootsearch((cfmatch_t)NULL, rootname, aux))
229 != NULL) {
230 config_attach(ROOT, match, aux, (cfprint_t)NULL);
231 return (1);
232 }
233 printf("root device %s not configured\n", rootname);
234 return (0);
235 }
236
237 /* just like sprintf(buf, "%d") except that it works from the end */
238 static char *
239 number(ep, n)
240 register char *ep;
241 register int n;
242 {
243
244 *--ep = 0;
245 while (n >= 10) {
246 *--ep = (n % 10) + '0';
247 n /= 10;
248 }
249 *--ep = n + '0';
250 return (ep);
251 }
252
253 /*
254 * Attach a found device. Allocates memory for device variables.
255 */
256 void
257 config_attach(parent, match, aux, print)
258 register struct device *parent;
259 void *match;
260 register void *aux;
261 cfprint_t print;
262 {
263 register struct cfdata *cf;
264 register struct device *dev;
265 register struct cfdriver *cd;
266 static struct device **nextp = &alldevs;
267
268 if (parent && parent->dv_cfdata->cf_driver->cd_indirect) {
269 dev = match;
270 cf = dev->dv_cfdata;
271 } else {
272 cf = match;
273 dev = config_make_softc(parent, cf);
274 }
275
276 cd = cf->cf_driver;
277 cd->cd_devs[cf->cf_unit] = dev;
278
279 if (cf->cf_fstate == FSTATE_STAR)
280 cf->cf_unit++;
281 else
282 cf->cf_fstate = FSTATE_FOUND;
283
284 *nextp = dev; /* link up */
285 nextp = &dev->dv_next;
286
287 if (parent == ROOT)
288 printf("%s (root)", dev->dv_xname);
289 else {
290 printf("%s at %s", dev->dv_xname, parent->dv_xname);
291 if (print)
292 (void) (*print)(aux, (char *)0);
293 }
294
295 /*
296 * Before attaching, clobber any unfound devices that are
297 * otherwise identical.
298 */
299 for (cf = cfdata; cf->cf_driver; cf++)
300 if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit &&
301 cf->cf_fstate == FSTATE_NOTFOUND)
302 cf->cf_fstate = FSTATE_FOUND;
303 (*cd->cd_attach)(parent, dev, aux);
304 }
305
306 struct device *
307 config_make_softc(parent, cf)
308 struct device *parent;
309 struct cfdata *cf;
310 {
311 register struct device *dev;
312 register struct cfdriver *cd;
313 register size_t lname, lunit;
314 register char *xunit;
315 char num[10];
316
317 cd = cf->cf_driver;
318 if (cd->cd_devsize < sizeof(struct device))
319 panic("config_make_softc");
320
321 /* compute length of name and decimal expansion of unit number */
322 lname = strlen(cd->cd_name);
323 xunit = number(&num[sizeof num], cf->cf_unit);
324 lunit = &num[sizeof num] - xunit;
325 if (lname + lunit >= sizeof(dev->dv_xname))
326 panic("config_attach: device name too long");
327
328 /* get memory for all device vars */
329 dev = (struct device *)malloc(cd->cd_devsize, M_DEVBUF, M_NOWAIT);
330 if (!dev)
331 panic("config_attach: memory allocation for device softc failed");
332 bzero(dev, cd->cd_devsize);
333 dev->dv_class = cd->cd_class;
334 dev->dv_cfdata = cf;
335 dev->dv_unit = cf->cf_unit;
336 bcopy(cd->cd_name, dev->dv_xname, lname);
337 bcopy(xunit, dev->dv_xname + lname, lunit);
338 dev->dv_parent = parent;
339
340 /* put this device in the devices array */
341 if (dev->dv_unit >= cd->cd_ndevs) {
342 /*
343 * Need to expand the array.
344 */
345 int old = cd->cd_ndevs, new;
346 void **nsp;
347
348 if (old == 0)
349 new = MINALLOCSIZE / sizeof(void *);
350 else
351 new = old * 2;
352 while (new <= dev->dv_unit)
353 new *= 2;
354 cd->cd_ndevs = new;
355 nsp = malloc(new * sizeof(void *), M_DEVBUF, M_NOWAIT);
356 if (nsp == 0)
357 panic("config_attach: %sing dev array",
358 old != 0 ? "expand" : "creat");
359 bzero(nsp + old, (new - old) * sizeof(void *));
360 if (old != 0) {
361 bcopy(cd->cd_devs, nsp, old * sizeof(void *));
362 free(cd->cd_devs, M_DEVBUF);
363 }
364 cd->cd_devs = nsp;
365 }
366 if (cd->cd_devs[dev->dv_unit])
367 panic("config_attach: duplicate %s", dev->dv_xname);
368
369 return (dev);
370 }
371
372 /*
373 * Attach an event. These must come from initially-zero space (see
374 * commented-out assignments below), but that occurs naturally for
375 * device instance variables.
376 */
377 void
378 evcnt_attach(dev, name, ev)
379 struct device *dev;
380 const char *name;
381 struct evcnt *ev;
382 {
383 static struct evcnt **nextp = &allevents;
384
385 #ifdef DIAGNOSTIC
386 if (strlen(name) >= sizeof(ev->ev_name))
387 panic("evcnt_attach");
388 #endif
389 /* ev->ev_next = NULL; */
390 ev->ev_dev = dev;
391 /* ev->ev_count = 0; */
392 strcpy(ev->ev_name, name);
393 *nextp = ev;
394 nextp = &ev->ev_next;
395 }
396