subr_autoconf.c revision 1.32 1 1.32 cgd /* $NetBSD: subr_autoconf.c,v 1.32 1998/08/31 22:28:08 cgd Exp $ */
2 1.9 cgd
3 1.1 glass /*
4 1.7 glass * Copyright (c) 1992, 1993
5 1.7 glass * The Regents of the University of California. All rights reserved.
6 1.1 glass *
7 1.1 glass * This software was developed by the Computer Systems Engineering group
8 1.1 glass * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 glass * contributed to Berkeley.
10 1.1 glass *
11 1.1 glass * All advertising materials mentioning features or use of this software
12 1.1 glass * must display the following acknowledgement:
13 1.1 glass * This product includes software developed by the University of
14 1.1 glass * California, Lawrence Berkeley Laboratories.
15 1.1 glass *
16 1.7 glass * Redistribution and use in source and binary forms, with or without
17 1.7 glass * modification, are permitted provided that the following conditions
18 1.7 glass * are met:
19 1.7 glass * 1. Redistributions of source code must retain the above copyright
20 1.7 glass * notice, this list of conditions and the following disclaimer.
21 1.7 glass * 2. Redistributions in binary form must reproduce the above copyright
22 1.7 glass * notice, this list of conditions and the following disclaimer in the
23 1.7 glass * documentation and/or other materials provided with the distribution.
24 1.7 glass * 3. All advertising materials mentioning features or use of this software
25 1.7 glass * must display the following acknowledgement:
26 1.7 glass * This product includes software developed by the University of
27 1.7 glass * California, Berkeley and its contributors.
28 1.7 glass * 4. Neither the name of the University nor the names of its contributors
29 1.7 glass * may be used to endorse or promote products derived from this software
30 1.7 glass * without specific prior written permission.
31 1.1 glass *
32 1.7 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.7 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.7 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.7 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.7 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.7 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.7 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.7 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.7 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.7 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.7 glass * SUCH DAMAGE.
43 1.1 glass *
44 1.8 cgd * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL)
45 1.9 cgd *
46 1.28 fvdl * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94
47 1.1 glass */
48 1.1 glass
49 1.4 mycroft #include <sys/param.h>
50 1.4 mycroft #include <sys/device.h>
51 1.4 mycroft #include <sys/malloc.h>
52 1.17 christos #include <sys/systm.h>
53 1.16 mycroft #include <machine/limits.h>
54 1.1 glass
55 1.1 glass /*
56 1.1 glass * Autoconfiguration subroutines.
57 1.1 glass */
58 1.1 glass
59 1.1 glass /*
60 1.1 glass * ioconf.c exports exactly two names: cfdata and cfroots. All system
61 1.1 glass * devices and drivers are found via these tables.
62 1.1 glass */
63 1.1 glass extern struct cfdata cfdata[];
64 1.1 glass extern short cfroots[];
65 1.1 glass
66 1.1 glass #define ROOT ((struct device *)NULL)
67 1.1 glass
68 1.16 mycroft struct matchinfo {
69 1.16 mycroft cfmatch_t fn;
70 1.16 mycroft struct device *parent;
71 1.25 cgd void *aux;
72 1.25 cgd struct cfdata *match;
73 1.25 cgd int pri;
74 1.16 mycroft };
75 1.17 christos
76 1.17 christos static char *number __P((char *, int));
77 1.17 christos static void mapply __P((struct matchinfo *, struct cfdata *));
78 1.16 mycroft
79 1.29 thorpej struct deferred_config {
80 1.29 thorpej TAILQ_ENTRY(deferred_config) dc_queue;
81 1.29 thorpej struct device *dc_dev;
82 1.29 thorpej void (*dc_func) __P((struct device *));
83 1.29 thorpej };
84 1.29 thorpej
85 1.29 thorpej TAILQ_HEAD(, deferred_config) deferred_config_queue;
86 1.29 thorpej
87 1.29 thorpej static void config_process_deferred_children __P((struct device *));
88 1.29 thorpej
89 1.20 cgd struct devicelist alldevs; /* list of all devices */
90 1.20 cgd struct evcntlist allevents; /* list of all event counters */
91 1.20 cgd
92 1.20 cgd /*
93 1.20 cgd * Initialize autoconfiguration data structures.
94 1.20 cgd */
95 1.20 cgd void
96 1.20 cgd config_init()
97 1.20 cgd {
98 1.20 cgd
99 1.29 thorpej TAILQ_INIT(&deferred_config_queue);
100 1.20 cgd TAILQ_INIT(&alldevs);
101 1.20 cgd TAILQ_INIT(&allevents);
102 1.20 cgd }
103 1.20 cgd
104 1.1 glass /*
105 1.1 glass * Apply the matching function and choose the best. This is used
106 1.1 glass * a few times and we want to keep the code small.
107 1.1 glass */
108 1.16 mycroft static void
109 1.16 mycroft mapply(m, cf)
110 1.1 glass register struct matchinfo *m;
111 1.1 glass register struct cfdata *cf;
112 1.1 glass {
113 1.1 glass register int pri;
114 1.1 glass
115 1.1 glass if (m->fn != NULL)
116 1.25 cgd pri = (*m->fn)(m->parent, cf, m->aux);
117 1.3 glass else {
118 1.19 thorpej if (cf->cf_attach->ca_match == NULL) {
119 1.16 mycroft panic("mapply: no match function for '%s' device\n",
120 1.12 mycroft cf->cf_driver->cd_name);
121 1.3 glass }
122 1.25 cgd pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux);
123 1.3 glass }
124 1.1 glass if (pri > m->pri) {
125 1.25 cgd m->match = cf;
126 1.1 glass m->pri = pri;
127 1.1 glass }
128 1.1 glass }
129 1.1 glass
130 1.1 glass /*
131 1.1 glass * Iterate over all potential children of some device, calling the given
132 1.1 glass * function (default being the child's match function) for each one.
133 1.1 glass * Nonzero returns are matches; the highest value returned is considered
134 1.1 glass * the best match. Return the `found child' if we got a match, or NULL
135 1.1 glass * otherwise. The `aux' pointer is simply passed on through.
136 1.1 glass *
137 1.1 glass * Note that this function is designed so that it can be used to apply
138 1.1 glass * an arbitrary function to all potential children (its return value
139 1.1 glass * can be ignored).
140 1.1 glass */
141 1.25 cgd struct cfdata *
142 1.1 glass config_search(fn, parent, aux)
143 1.1 glass cfmatch_t fn;
144 1.1 glass register struct device *parent;
145 1.1 glass void *aux;
146 1.1 glass {
147 1.1 glass register struct cfdata *cf;
148 1.1 glass register short *p;
149 1.1 glass struct matchinfo m;
150 1.1 glass
151 1.1 glass m.fn = fn;
152 1.1 glass m.parent = parent;
153 1.25 cgd m.aux = aux;
154 1.14 mycroft m.match = NULL;
155 1.1 glass m.pri = 0;
156 1.1 glass for (cf = cfdata; cf->cf_driver; cf++) {
157 1.1 glass /*
158 1.1 glass * Skip cf if no longer eligible, otherwise scan through
159 1.1 glass * parents for one matching `parent', and try match function.
160 1.1 glass */
161 1.1 glass if (cf->cf_fstate == FSTATE_FOUND)
162 1.1 glass continue;
163 1.1 glass for (p = cf->cf_parents; *p >= 0; p++)
164 1.1 glass if (parent->dv_cfdata == &cfdata[*p])
165 1.16 mycroft mapply(&m, cf);
166 1.1 glass }
167 1.1 glass return (m.match);
168 1.1 glass }
169 1.1 glass
170 1.16 mycroft /*
171 1.1 glass * Find the given root device.
172 1.1 glass * This is much like config_search, but there is no parent.
173 1.1 glass */
174 1.25 cgd struct cfdata *
175 1.1 glass config_rootsearch(fn, rootname, aux)
176 1.1 glass register cfmatch_t fn;
177 1.1 glass register char *rootname;
178 1.1 glass register void *aux;
179 1.1 glass {
180 1.1 glass register struct cfdata *cf;
181 1.1 glass register short *p;
182 1.1 glass struct matchinfo m;
183 1.1 glass
184 1.1 glass m.fn = fn;
185 1.1 glass m.parent = ROOT;
186 1.25 cgd m.aux = aux;
187 1.14 mycroft m.match = NULL;
188 1.1 glass m.pri = 0;
189 1.1 glass /*
190 1.1 glass * Look at root entries for matching name. We do not bother
191 1.1 glass * with found-state here since only one root should ever be
192 1.1 glass * searched (and it must be done first).
193 1.1 glass */
194 1.1 glass for (p = cfroots; *p >= 0; p++) {
195 1.1 glass cf = &cfdata[*p];
196 1.1 glass if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
197 1.16 mycroft mapply(&m, cf);
198 1.1 glass }
199 1.1 glass return (m.match);
200 1.1 glass }
201 1.1 glass
202 1.1 glass static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
203 1.1 glass
204 1.1 glass /*
205 1.1 glass * The given `aux' argument describes a device that has been found
206 1.1 glass * on the given parent, but not necessarily configured. Locate the
207 1.18 cgd * configuration data for that device (using the submatch function
208 1.18 cgd * provided, or using candidates' cd_match configuration driver
209 1.18 cgd * functions) and attach it, and return true. If the device was
210 1.1 glass * not configured, call the given `print' function and return 0.
211 1.1 glass */
212 1.21 cgd struct device *
213 1.18 cgd config_found_sm(parent, aux, print, submatch)
214 1.1 glass struct device *parent;
215 1.1 glass void *aux;
216 1.1 glass cfprint_t print;
217 1.18 cgd cfmatch_t submatch;
218 1.1 glass {
219 1.25 cgd struct cfdata *cf;
220 1.25 cgd
221 1.25 cgd if ((cf = config_search(submatch, parent, aux)) != NULL)
222 1.25 cgd return (config_attach(parent, cf, aux, print));
223 1.3 glass if (print)
224 1.24 christos printf(msgs[(*print)(aux, parent->dv_xname)]);
225 1.21 cgd return (NULL);
226 1.1 glass }
227 1.1 glass
228 1.1 glass /*
229 1.1 glass * As above, but for root devices.
230 1.1 glass */
231 1.21 cgd struct device *
232 1.1 glass config_rootfound(rootname, aux)
233 1.1 glass char *rootname;
234 1.1 glass void *aux;
235 1.1 glass {
236 1.25 cgd struct cfdata *cf;
237 1.25 cgd
238 1.25 cgd if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
239 1.25 cgd return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
240 1.24 christos printf("root device %s not configured\n", rootname);
241 1.21 cgd return (NULL);
242 1.1 glass }
243 1.1 glass
244 1.1 glass /* just like sprintf(buf, "%d") except that it works from the end */
245 1.1 glass static char *
246 1.1 glass number(ep, n)
247 1.1 glass register char *ep;
248 1.1 glass register int n;
249 1.1 glass {
250 1.1 glass
251 1.1 glass *--ep = 0;
252 1.1 glass while (n >= 10) {
253 1.1 glass *--ep = (n % 10) + '0';
254 1.1 glass n /= 10;
255 1.1 glass }
256 1.1 glass *--ep = n + '0';
257 1.1 glass return (ep);
258 1.1 glass }
259 1.1 glass
260 1.1 glass /*
261 1.1 glass * Attach a found device. Allocates memory for device variables.
262 1.1 glass */
263 1.25 cgd struct device *
264 1.25 cgd config_attach(parent, cf, aux, print)
265 1.25 cgd register struct device *parent;
266 1.25 cgd register struct cfdata *cf;
267 1.25 cgd register void *aux;
268 1.25 cgd cfprint_t print;
269 1.25 cgd {
270 1.25 cgd register struct device *dev;
271 1.25 cgd register struct cfdriver *cd;
272 1.25 cgd register struct cfattach *ca;
273 1.25 cgd register size_t lname, lunit;
274 1.25 cgd register char *xunit;
275 1.25 cgd int myunit;
276 1.25 cgd char num[10];
277 1.25 cgd
278 1.25 cgd cd = cf->cf_driver;
279 1.25 cgd ca = cf->cf_attach;
280 1.25 cgd if (ca->ca_devsize < sizeof(struct device))
281 1.25 cgd panic("config_attach");
282 1.25 cgd myunit = cf->cf_unit;
283 1.25 cgd if (cf->cf_fstate == FSTATE_STAR)
284 1.25 cgd cf->cf_unit++;
285 1.25 cgd else {
286 1.25 cgd KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
287 1.25 cgd cf->cf_fstate = FSTATE_FOUND;
288 1.25 cgd }
289 1.25 cgd
290 1.25 cgd /* compute length of name and decimal expansion of unit number */
291 1.25 cgd lname = strlen(cd->cd_name);
292 1.30 perry xunit = number(&num[sizeof(num)], myunit);
293 1.30 perry lunit = &num[sizeof(num)] - xunit;
294 1.25 cgd if (lname + lunit >= sizeof(dev->dv_xname))
295 1.25 cgd panic("config_attach: device name too long");
296 1.25 cgd
297 1.25 cgd /* get memory for all device vars */
298 1.25 cgd dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF, M_NOWAIT);
299 1.25 cgd if (!dev)
300 1.25 cgd panic("config_attach: memory allocation for device softc failed");
301 1.31 perry memset(dev, 0, ca->ca_devsize);
302 1.25 cgd TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
303 1.25 cgd dev->dv_class = cd->cd_class;
304 1.25 cgd dev->dv_cfdata = cf;
305 1.25 cgd dev->dv_unit = myunit;
306 1.31 perry memcpy(dev->dv_xname, cd->cd_name, lname);
307 1.31 perry memcpy(dev->dv_xname + lname, xunit, lunit);
308 1.25 cgd dev->dv_parent = parent;
309 1.29 thorpej
310 1.25 cgd if (parent == ROOT)
311 1.25 cgd printf("%s (root)", dev->dv_xname);
312 1.25 cgd else {
313 1.25 cgd printf("%s at %s", dev->dv_xname, parent->dv_xname);
314 1.25 cgd if (print)
315 1.25 cgd (void) (*print)(aux, (char *)0);
316 1.25 cgd }
317 1.25 cgd
318 1.25 cgd /* put this device in the devices array */
319 1.25 cgd if (dev->dv_unit >= cd->cd_ndevs) {
320 1.25 cgd /*
321 1.25 cgd * Need to expand the array.
322 1.25 cgd */
323 1.25 cgd int old = cd->cd_ndevs, new;
324 1.25 cgd void **nsp;
325 1.25 cgd
326 1.25 cgd if (old == 0)
327 1.25 cgd new = MINALLOCSIZE / sizeof(void *);
328 1.25 cgd else
329 1.25 cgd new = old * 2;
330 1.25 cgd while (new <= dev->dv_unit)
331 1.25 cgd new *= 2;
332 1.25 cgd cd->cd_ndevs = new;
333 1.25 cgd nsp = malloc(new * sizeof(void *), M_DEVBUF, M_NOWAIT);
334 1.25 cgd if (nsp == 0)
335 1.25 cgd panic("config_attach: %sing dev array",
336 1.25 cgd old != 0 ? "expand" : "creat");
337 1.31 perry memset(nsp + old, 0, (new - old) * sizeof(void *));
338 1.25 cgd if (old != 0) {
339 1.31 perry memcpy(nsp, cd->cd_devs, old * sizeof(void *));
340 1.25 cgd free(cd->cd_devs, M_DEVBUF);
341 1.25 cgd }
342 1.25 cgd cd->cd_devs = nsp;
343 1.25 cgd }
344 1.25 cgd if (cd->cd_devs[dev->dv_unit])
345 1.25 cgd panic("config_attach: duplicate %s", dev->dv_xname);
346 1.25 cgd cd->cd_devs[dev->dv_unit] = dev;
347 1.25 cgd
348 1.25 cgd /*
349 1.25 cgd * Before attaching, clobber any unfound devices that are
350 1.25 cgd * otherwise identical, or bump the unit number on all starred
351 1.25 cgd * cfdata for this device.
352 1.25 cgd */
353 1.25 cgd for (cf = cfdata; cf->cf_driver; cf++)
354 1.25 cgd if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) {
355 1.25 cgd if (cf->cf_fstate == FSTATE_NOTFOUND)
356 1.25 cgd cf->cf_fstate = FSTATE_FOUND;
357 1.25 cgd if (cf->cf_fstate == FSTATE_STAR)
358 1.25 cgd cf->cf_unit++;
359 1.25 cgd }
360 1.27 drochner #if defined(__alpha__) || defined(hp300) || defined(__i386__)
361 1.25 cgd device_register(dev, aux);
362 1.25 cgd #endif
363 1.25 cgd (*ca->ca_attach)(parent, dev, aux);
364 1.29 thorpej config_process_deferred_children(dev);
365 1.25 cgd return (dev);
366 1.25 cgd }
367 1.29 thorpej
368 1.29 thorpej /*
369 1.29 thorpej * Defer the configuration of the specified device until all
370 1.29 thorpej * of its parent's devices have been attached.
371 1.29 thorpej */
372 1.29 thorpej void
373 1.29 thorpej config_defer(dev, func)
374 1.29 thorpej struct device *dev;
375 1.29 thorpej void (*func) __P((struct device *));
376 1.29 thorpej {
377 1.29 thorpej struct deferred_config *dc;
378 1.29 thorpej
379 1.29 thorpej if (dev->dv_parent == NULL)
380 1.29 thorpej panic("config_defer: can't defer config of a root device");
381 1.29 thorpej
382 1.29 thorpej #ifdef DIAGNOSTIC
383 1.29 thorpej for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
384 1.29 thorpej dc = TAILQ_NEXT(dc, dc_queue)) {
385 1.29 thorpej if (dc->dc_dev == dev)
386 1.29 thorpej panic("config_defer: deferred twice");
387 1.29 thorpej }
388 1.29 thorpej #endif
389 1.29 thorpej
390 1.29 thorpej if ((dc = malloc(sizeof(*dc), M_DEVBUF, M_NOWAIT)) == NULL)
391 1.29 thorpej panic("config_defer: can't allocate defer structure");
392 1.29 thorpej
393 1.29 thorpej dc->dc_dev = dev;
394 1.29 thorpej dc->dc_func = func;
395 1.29 thorpej TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
396 1.29 thorpej }
397 1.29 thorpej
398 1.29 thorpej /*
399 1.29 thorpej * Process the deferred configuration queue for a device.
400 1.29 thorpej */
401 1.29 thorpej static void
402 1.29 thorpej config_process_deferred_children(parent)
403 1.29 thorpej struct device *parent;
404 1.29 thorpej {
405 1.29 thorpej struct deferred_config *dc, *ndc;
406 1.29 thorpej
407 1.29 thorpej for (dc = TAILQ_FIRST(&deferred_config_queue);
408 1.29 thorpej dc != NULL; dc = ndc) {
409 1.29 thorpej ndc = TAILQ_NEXT(dc, dc_queue);
410 1.29 thorpej if (dc->dc_dev->dv_parent == parent) {
411 1.29 thorpej TAILQ_REMOVE(&deferred_config_queue, dc, dc_queue);
412 1.29 thorpej (*dc->dc_func)(dc->dc_dev);
413 1.29 thorpej free(dc, M_DEVBUF);
414 1.29 thorpej }
415 1.29 thorpej }
416 1.29 thorpej }
417 1.1 glass
418 1.1 glass /*
419 1.1 glass * Attach an event. These must come from initially-zero space (see
420 1.1 glass * commented-out assignments below), but that occurs naturally for
421 1.1 glass * device instance variables.
422 1.1 glass */
423 1.1 glass void
424 1.1 glass evcnt_attach(dev, name, ev)
425 1.1 glass struct device *dev;
426 1.1 glass const char *name;
427 1.1 glass struct evcnt *ev;
428 1.1 glass {
429 1.1 glass
430 1.1 glass #ifdef DIAGNOSTIC
431 1.1 glass if (strlen(name) >= sizeof(ev->ev_name))
432 1.1 glass panic("evcnt_attach");
433 1.1 glass #endif
434 1.1 glass /* ev->ev_next = NULL; */
435 1.1 glass ev->ev_dev = dev;
436 1.1 glass /* ev->ev_count = 0; */
437 1.1 glass strcpy(ev->ev_name, name);
438 1.20 cgd TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
439 1.1 glass }
440