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