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