subr_autoconf.c revision 1.27 1 1.27 drochner /* $NetBSD: subr_autoconf.c,v 1.27 1997/09/20 14:16:43 drochner 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.9 cgd * @(#)subr_autoconf.c 8.1 (Berkeley) 6/10/93
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.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
69 1.13 mycroft struct device *config_make_softc __P((struct device *, struct cfdata *));
70 1.25 cgd #endif
71 1.12 mycroft
72 1.16 mycroft struct matchinfo {
73 1.16 mycroft cfmatch_t fn;
74 1.16 mycroft struct device *parent;
75 1.25 cgd void *aux;
76 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
77 1.25 cgd void *match;
78 1.25 cgd int indirect;
79 1.25 cgd #else
80 1.25 cgd struct cfdata *match;
81 1.25 cgd #endif
82 1.25 cgd int pri;
83 1.16 mycroft };
84 1.17 christos
85 1.17 christos static char *number __P((char *, int));
86 1.17 christos static void mapply __P((struct matchinfo *, struct cfdata *));
87 1.16 mycroft
88 1.20 cgd struct devicelist alldevs; /* list of all devices */
89 1.20 cgd struct evcntlist allevents; /* list of all event counters */
90 1.20 cgd
91 1.20 cgd /*
92 1.20 cgd * Initialize autoconfiguration data structures.
93 1.20 cgd */
94 1.20 cgd void
95 1.20 cgd config_init()
96 1.20 cgd {
97 1.20 cgd
98 1.20 cgd TAILQ_INIT(&alldevs);
99 1.20 cgd TAILQ_INIT(&allevents);
100 1.20 cgd }
101 1.20 cgd
102 1.1 glass /*
103 1.1 glass * Apply the matching function and choose the best. This is used
104 1.1 glass * a few times and we want to keep the code small.
105 1.1 glass */
106 1.16 mycroft static void
107 1.16 mycroft mapply(m, cf)
108 1.1 glass register struct matchinfo *m;
109 1.1 glass register struct cfdata *cf;
110 1.1 glass {
111 1.1 glass register int pri;
112 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
113 1.12 mycroft void *match;
114 1.12 mycroft
115 1.16 mycroft if (m->indirect)
116 1.13 mycroft match = config_make_softc(m->parent, cf);
117 1.16 mycroft else
118 1.12 mycroft match = cf;
119 1.25 cgd #endif
120 1.1 glass
121 1.1 glass if (m->fn != NULL)
122 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
123 1.12 mycroft pri = (*m->fn)(m->parent, match, m->aux);
124 1.25 cgd #else
125 1.25 cgd pri = (*m->fn)(m->parent, cf, m->aux);
126 1.25 cgd #endif
127 1.3 glass else {
128 1.19 thorpej if (cf->cf_attach->ca_match == NULL) {
129 1.16 mycroft panic("mapply: no match function for '%s' device\n",
130 1.12 mycroft cf->cf_driver->cd_name);
131 1.3 glass }
132 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
133 1.19 thorpej pri = (*cf->cf_attach->ca_match)(m->parent, match, m->aux);
134 1.25 cgd #else
135 1.25 cgd pri = (*cf->cf_attach->ca_match)(m->parent, cf, m->aux);
136 1.25 cgd #endif
137 1.3 glass }
138 1.1 glass if (pri > m->pri) {
139 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
140 1.14 mycroft if (m->indirect && m->match)
141 1.12 mycroft free(m->match, M_DEVBUF);
142 1.12 mycroft m->match = match;
143 1.25 cgd #else
144 1.25 cgd m->match = cf;
145 1.25 cgd #endif
146 1.1 glass m->pri = pri;
147 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
148 1.12 mycroft } else {
149 1.14 mycroft if (m->indirect)
150 1.12 mycroft free(match, M_DEVBUF);
151 1.25 cgd #endif
152 1.1 glass }
153 1.1 glass }
154 1.1 glass
155 1.1 glass /*
156 1.1 glass * Iterate over all potential children of some device, calling the given
157 1.1 glass * function (default being the child's match function) for each one.
158 1.1 glass * Nonzero returns are matches; the highest value returned is considered
159 1.1 glass * the best match. Return the `found child' if we got a match, or NULL
160 1.1 glass * otherwise. The `aux' pointer is simply passed on through.
161 1.1 glass *
162 1.1 glass * Note that this function is designed so that it can be used to apply
163 1.1 glass * an arbitrary function to all potential children (its return value
164 1.1 glass * can be ignored).
165 1.1 glass */
166 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
167 1.12 mycroft void *
168 1.25 cgd #else
169 1.25 cgd struct cfdata *
170 1.25 cgd #endif
171 1.1 glass config_search(fn, parent, aux)
172 1.1 glass cfmatch_t fn;
173 1.1 glass register struct device *parent;
174 1.1 glass void *aux;
175 1.1 glass {
176 1.1 glass register struct cfdata *cf;
177 1.1 glass register short *p;
178 1.1 glass struct matchinfo m;
179 1.1 glass
180 1.1 glass m.fn = fn;
181 1.1 glass m.parent = parent;
182 1.25 cgd m.aux = aux;
183 1.14 mycroft m.match = NULL;
184 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
185 1.14 mycroft m.indirect = parent && parent->dv_cfdata->cf_driver->cd_indirect;
186 1.25 cgd #endif
187 1.1 glass m.pri = 0;
188 1.1 glass for (cf = cfdata; cf->cf_driver; cf++) {
189 1.1 glass /*
190 1.1 glass * Skip cf if no longer eligible, otherwise scan through
191 1.1 glass * parents for one matching `parent', and try match function.
192 1.1 glass */
193 1.1 glass if (cf->cf_fstate == FSTATE_FOUND)
194 1.1 glass continue;
195 1.1 glass for (p = cf->cf_parents; *p >= 0; p++)
196 1.1 glass if (parent->dv_cfdata == &cfdata[*p])
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.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
203 1.1 glass /*
204 1.16 mycroft * Iterate over all potential children of some device, calling the given
205 1.16 mycroft * function for each one.
206 1.16 mycroft *
207 1.16 mycroft * Note that this function is designed so that it can be used to apply
208 1.16 mycroft * an arbitrary function to all potential children (its return value
209 1.16 mycroft * can be ignored).
210 1.16 mycroft */
211 1.16 mycroft void
212 1.16 mycroft config_scan(fn, parent)
213 1.16 mycroft cfscan_t fn;
214 1.16 mycroft register struct device *parent;
215 1.16 mycroft {
216 1.16 mycroft register struct cfdata *cf;
217 1.16 mycroft register short *p;
218 1.16 mycroft void *match;
219 1.16 mycroft int indirect;
220 1.16 mycroft
221 1.16 mycroft indirect = parent && parent->dv_cfdata->cf_driver->cd_indirect;
222 1.16 mycroft for (cf = cfdata; cf->cf_driver; cf++) {
223 1.16 mycroft /*
224 1.16 mycroft * Skip cf if no longer eligible, otherwise scan through
225 1.16 mycroft * parents for one matching `parent', and try match function.
226 1.16 mycroft */
227 1.16 mycroft if (cf->cf_fstate == FSTATE_FOUND)
228 1.16 mycroft continue;
229 1.16 mycroft for (p = cf->cf_parents; *p >= 0; p++)
230 1.16 mycroft if (parent->dv_cfdata == &cfdata[*p]) {
231 1.16 mycroft if (indirect)
232 1.16 mycroft match = config_make_softc(parent, cf);
233 1.16 mycroft else
234 1.16 mycroft match = cf;
235 1.16 mycroft (*fn)(parent, match);
236 1.16 mycroft }
237 1.16 mycroft }
238 1.16 mycroft }
239 1.25 cgd #endif /* __BROKEN_INDIRECT_CONFIG */
240 1.16 mycroft
241 1.16 mycroft /*
242 1.1 glass * Find the given root device.
243 1.1 glass * This is much like config_search, but there is no parent.
244 1.1 glass */
245 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
246 1.12 mycroft void *
247 1.25 cgd #else
248 1.25 cgd struct cfdata *
249 1.25 cgd #endif
250 1.1 glass config_rootsearch(fn, rootname, aux)
251 1.1 glass register cfmatch_t fn;
252 1.1 glass register char *rootname;
253 1.1 glass register void *aux;
254 1.1 glass {
255 1.1 glass register struct cfdata *cf;
256 1.1 glass register short *p;
257 1.1 glass struct matchinfo m;
258 1.1 glass
259 1.1 glass m.fn = fn;
260 1.1 glass m.parent = ROOT;
261 1.25 cgd m.aux = aux;
262 1.14 mycroft m.match = NULL;
263 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
264 1.14 mycroft m.indirect = 0;
265 1.25 cgd #endif
266 1.1 glass m.pri = 0;
267 1.1 glass /*
268 1.1 glass * Look at root entries for matching name. We do not bother
269 1.1 glass * with found-state here since only one root should ever be
270 1.1 glass * searched (and it must be done first).
271 1.1 glass */
272 1.1 glass for (p = cfroots; *p >= 0; p++) {
273 1.1 glass cf = &cfdata[*p];
274 1.1 glass if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
275 1.16 mycroft mapply(&m, cf);
276 1.1 glass }
277 1.1 glass return (m.match);
278 1.1 glass }
279 1.1 glass
280 1.1 glass static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
281 1.1 glass
282 1.1 glass /*
283 1.1 glass * The given `aux' argument describes a device that has been found
284 1.1 glass * on the given parent, but not necessarily configured. Locate the
285 1.18 cgd * configuration data for that device (using the submatch function
286 1.18 cgd * provided, or using candidates' cd_match configuration driver
287 1.18 cgd * functions) and attach it, and return true. If the device was
288 1.1 glass * not configured, call the given `print' function and return 0.
289 1.1 glass */
290 1.21 cgd struct device *
291 1.18 cgd config_found_sm(parent, aux, print, submatch)
292 1.1 glass struct device *parent;
293 1.1 glass void *aux;
294 1.1 glass cfprint_t print;
295 1.18 cgd cfmatch_t submatch;
296 1.1 glass {
297 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
298 1.12 mycroft void *match;
299 1.1 glass
300 1.21 cgd if ((match = config_search(submatch, parent, aux)) != NULL)
301 1.21 cgd return (config_attach(parent, match, aux, print));
302 1.25 cgd #else
303 1.25 cgd struct cfdata *cf;
304 1.25 cgd
305 1.25 cgd if ((cf = config_search(submatch, parent, aux)) != NULL)
306 1.25 cgd return (config_attach(parent, cf, aux, print));
307 1.25 cgd #endif
308 1.3 glass if (print)
309 1.24 christos printf(msgs[(*print)(aux, parent->dv_xname)]);
310 1.21 cgd return (NULL);
311 1.1 glass }
312 1.1 glass
313 1.1 glass /*
314 1.1 glass * As above, but for root devices.
315 1.1 glass */
316 1.21 cgd struct device *
317 1.1 glass config_rootfound(rootname, aux)
318 1.1 glass char *rootname;
319 1.1 glass void *aux;
320 1.1 glass {
321 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
322 1.12 mycroft void *match;
323 1.1 glass
324 1.21 cgd if ((match = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
325 1.21 cgd return (config_attach(ROOT, match, aux, (cfprint_t)NULL));
326 1.25 cgd #else
327 1.25 cgd struct cfdata *cf;
328 1.25 cgd
329 1.25 cgd if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
330 1.25 cgd return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
331 1.25 cgd #endif
332 1.24 christos printf("root device %s not configured\n", rootname);
333 1.21 cgd return (NULL);
334 1.1 glass }
335 1.1 glass
336 1.1 glass /* just like sprintf(buf, "%d") except that it works from the end */
337 1.1 glass static char *
338 1.1 glass number(ep, n)
339 1.1 glass register char *ep;
340 1.1 glass register int n;
341 1.1 glass {
342 1.1 glass
343 1.1 glass *--ep = 0;
344 1.1 glass while (n >= 10) {
345 1.1 glass *--ep = (n % 10) + '0';
346 1.1 glass n /= 10;
347 1.1 glass }
348 1.1 glass *--ep = n + '0';
349 1.1 glass return (ep);
350 1.1 glass }
351 1.1 glass
352 1.1 glass /*
353 1.1 glass * Attach a found device. Allocates memory for device variables.
354 1.1 glass */
355 1.25 cgd #ifdef __BROKEN_INDIRECT_CONFIG
356 1.21 cgd struct device *
357 1.12 mycroft config_attach(parent, match, aux, print)
358 1.1 glass register struct device *parent;
359 1.12 mycroft void *match;
360 1.1 glass register void *aux;
361 1.1 glass cfprint_t print;
362 1.1 glass {
363 1.12 mycroft register struct cfdata *cf;
364 1.12 mycroft register struct device *dev;
365 1.13 mycroft register struct cfdriver *cd;
366 1.19 thorpej register struct cfattach *ca;
367 1.12 mycroft
368 1.13 mycroft if (parent && parent->dv_cfdata->cf_driver->cd_indirect) {
369 1.12 mycroft dev = match;
370 1.12 mycroft cf = dev->dv_cfdata;
371 1.12 mycroft } else {
372 1.12 mycroft cf = match;
373 1.13 mycroft dev = config_make_softc(parent, cf);
374 1.12 mycroft }
375 1.12 mycroft
376 1.13 mycroft cd = cf->cf_driver;
377 1.19 thorpej ca = cf->cf_attach;
378 1.13 mycroft cd->cd_devs[cf->cf_unit] = dev;
379 1.12 mycroft
380 1.12 mycroft if (cf->cf_fstate == FSTATE_STAR)
381 1.12 mycroft cf->cf_unit++;
382 1.25 cgd else {
383 1.25 cgd KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
384 1.12 mycroft cf->cf_fstate = FSTATE_FOUND;
385 1.25 cgd }
386 1.12 mycroft
387 1.20 cgd TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);
388 1.12 mycroft
389 1.12 mycroft if (parent == ROOT)
390 1.24 christos printf("%s (root)", dev->dv_xname);
391 1.12 mycroft else {
392 1.24 christos printf("%s at %s", dev->dv_xname, parent->dv_xname);
393 1.12 mycroft if (print)
394 1.12 mycroft (void) (*print)(aux, (char *)0);
395 1.12 mycroft }
396 1.12 mycroft
397 1.12 mycroft /*
398 1.12 mycroft * Before attaching, clobber any unfound devices that are
399 1.19 thorpej * otherwise identical, or bump the unit number on all starred
400 1.19 thorpej * cfdata for this device.
401 1.12 mycroft */
402 1.12 mycroft for (cf = cfdata; cf->cf_driver; cf++)
403 1.19 thorpej if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) {
404 1.19 thorpej if (cf->cf_fstate == FSTATE_NOTFOUND)
405 1.19 thorpej cf->cf_fstate = FSTATE_FOUND;
406 1.19 thorpej if (cf->cf_fstate == FSTATE_STAR)
407 1.19 thorpej cf->cf_unit++;
408 1.19 thorpej }
409 1.27 drochner #if defined(__alpha__) || defined(hp300) || defined(__i386__)
410 1.22 cgd device_register(dev, aux);
411 1.22 cgd #endif
412 1.19 thorpej (*ca->ca_attach)(parent, dev, aux);
413 1.21 cgd return (dev);
414 1.12 mycroft }
415 1.12 mycroft
416 1.12 mycroft struct device *
417 1.13 mycroft config_make_softc(parent, cf)
418 1.13 mycroft struct device *parent;
419 1.12 mycroft struct cfdata *cf;
420 1.12 mycroft {
421 1.1 glass register struct device *dev;
422 1.1 glass register struct cfdriver *cd;
423 1.19 thorpej register struct cfattach *ca;
424 1.1 glass register size_t lname, lunit;
425 1.1 glass register char *xunit;
426 1.1 glass char num[10];
427 1.1 glass
428 1.1 glass cd = cf->cf_driver;
429 1.19 thorpej ca = cf->cf_attach;
430 1.19 thorpej if (ca->ca_devsize < sizeof(struct device))
431 1.12 mycroft panic("config_make_softc");
432 1.1 glass
433 1.1 glass /* compute length of name and decimal expansion of unit number */
434 1.1 glass lname = strlen(cd->cd_name);
435 1.12 mycroft xunit = number(&num[sizeof num], cf->cf_unit);
436 1.1 glass lunit = &num[sizeof num] - xunit;
437 1.1 glass if (lname + lunit >= sizeof(dev->dv_xname))
438 1.1 glass panic("config_attach: device name too long");
439 1.1 glass
440 1.1 glass /* get memory for all device vars */
441 1.19 thorpej dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF, M_NOWAIT);
442 1.3 glass if (!dev)
443 1.3 glass panic("config_attach: memory allocation for device softc failed");
444 1.19 thorpej bzero(dev, ca->ca_devsize);
445 1.1 glass dev->dv_class = cd->cd_class;
446 1.1 glass dev->dv_cfdata = cf;
447 1.12 mycroft dev->dv_unit = cf->cf_unit;
448 1.1 glass bcopy(cd->cd_name, dev->dv_xname, lname);
449 1.1 glass bcopy(xunit, dev->dv_xname + lname, lunit);
450 1.1 glass dev->dv_parent = parent;
451 1.1 glass
452 1.1 glass /* put this device in the devices array */
453 1.1 glass if (dev->dv_unit >= cd->cd_ndevs) {
454 1.1 glass /*
455 1.1 glass * Need to expand the array.
456 1.1 glass */
457 1.10 mycroft int old = cd->cd_ndevs, new;
458 1.1 glass void **nsp;
459 1.1 glass
460 1.10 mycroft if (old == 0)
461 1.10 mycroft new = MINALLOCSIZE / sizeof(void *);
462 1.10 mycroft else
463 1.10 mycroft new = old * 2;
464 1.10 mycroft while (new <= dev->dv_unit)
465 1.10 mycroft new *= 2;
466 1.10 mycroft cd->cd_ndevs = new;
467 1.10 mycroft nsp = malloc(new * sizeof(void *), M_DEVBUF, M_NOWAIT);
468 1.10 mycroft if (nsp == 0)
469 1.10 mycroft panic("config_attach: %sing dev array",
470 1.10 mycroft old != 0 ? "expand" : "creat");
471 1.10 mycroft bzero(nsp + old, (new - old) * sizeof(void *));
472 1.10 mycroft if (old != 0) {
473 1.10 mycroft bcopy(cd->cd_devs, nsp, old * sizeof(void *));
474 1.1 glass free(cd->cd_devs, M_DEVBUF);
475 1.1 glass }
476 1.1 glass cd->cd_devs = nsp;
477 1.1 glass }
478 1.1 glass if (cd->cd_devs[dev->dv_unit])
479 1.1 glass panic("config_attach: duplicate %s", dev->dv_xname);
480 1.1 glass
481 1.12 mycroft return (dev);
482 1.1 glass }
483 1.25 cgd #else /* __BROKEN_INDIRECT_CONFIG */
484 1.25 cgd struct device *
485 1.25 cgd config_attach(parent, cf, aux, print)
486 1.25 cgd register struct device *parent;
487 1.25 cgd register struct cfdata *cf;
488 1.25 cgd register void *aux;
489 1.25 cgd cfprint_t print;
490 1.25 cgd {
491 1.25 cgd register struct device *dev;
492 1.25 cgd register struct cfdriver *cd;
493 1.25 cgd register struct cfattach *ca;
494 1.25 cgd register size_t lname, lunit;
495 1.25 cgd register char *xunit;
496 1.25 cgd int myunit;
497 1.25 cgd char num[10];
498 1.25 cgd
499 1.25 cgd cd = cf->cf_driver;
500 1.25 cgd ca = cf->cf_attach;
501 1.25 cgd if (ca->ca_devsize < sizeof(struct device))
502 1.25 cgd panic("config_attach");
503 1.25 cgd myunit = cf->cf_unit;
504 1.25 cgd if (cf->cf_fstate == FSTATE_STAR)
505 1.25 cgd cf->cf_unit++;
506 1.25 cgd else {
507 1.25 cgd KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
508 1.25 cgd cf->cf_fstate = FSTATE_FOUND;
509 1.25 cgd }
510 1.25 cgd
511 1.25 cgd /* compute length of name and decimal expansion of unit number */
512 1.25 cgd lname = strlen(cd->cd_name);
513 1.25 cgd xunit = number(&num[sizeof num], myunit);
514 1.25 cgd lunit = &num[sizeof num] - xunit;
515 1.25 cgd if (lname + lunit >= sizeof(dev->dv_xname))
516 1.25 cgd panic("config_attach: device name too long");
517 1.25 cgd
518 1.25 cgd /* get memory for all device vars */
519 1.25 cgd dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF, M_NOWAIT);
520 1.25 cgd if (!dev)
521 1.25 cgd panic("config_attach: memory allocation for device softc failed");
522 1.25 cgd bzero(dev, ca->ca_devsize);
523 1.25 cgd TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
524 1.25 cgd dev->dv_class = cd->cd_class;
525 1.25 cgd dev->dv_cfdata = cf;
526 1.25 cgd dev->dv_unit = myunit;
527 1.25 cgd bcopy(cd->cd_name, dev->dv_xname, lname);
528 1.25 cgd bcopy(xunit, dev->dv_xname + lname, lunit);
529 1.25 cgd dev->dv_parent = parent;
530 1.25 cgd if (parent == ROOT)
531 1.25 cgd printf("%s (root)", dev->dv_xname);
532 1.25 cgd else {
533 1.25 cgd printf("%s at %s", dev->dv_xname, parent->dv_xname);
534 1.25 cgd if (print)
535 1.25 cgd (void) (*print)(aux, (char *)0);
536 1.25 cgd }
537 1.25 cgd
538 1.25 cgd /* put this device in the devices array */
539 1.25 cgd if (dev->dv_unit >= cd->cd_ndevs) {
540 1.25 cgd /*
541 1.25 cgd * Need to expand the array.
542 1.25 cgd */
543 1.25 cgd int old = cd->cd_ndevs, new;
544 1.25 cgd void **nsp;
545 1.25 cgd
546 1.25 cgd if (old == 0)
547 1.25 cgd new = MINALLOCSIZE / sizeof(void *);
548 1.25 cgd else
549 1.25 cgd new = old * 2;
550 1.25 cgd while (new <= dev->dv_unit)
551 1.25 cgd new *= 2;
552 1.25 cgd cd->cd_ndevs = new;
553 1.25 cgd nsp = malloc(new * sizeof(void *), M_DEVBUF, M_NOWAIT);
554 1.25 cgd if (nsp == 0)
555 1.25 cgd panic("config_attach: %sing dev array",
556 1.25 cgd old != 0 ? "expand" : "creat");
557 1.25 cgd bzero(nsp + old, (new - old) * sizeof(void *));
558 1.25 cgd if (old != 0) {
559 1.25 cgd bcopy(cd->cd_devs, nsp, old * sizeof(void *));
560 1.25 cgd free(cd->cd_devs, M_DEVBUF);
561 1.25 cgd }
562 1.25 cgd cd->cd_devs = nsp;
563 1.25 cgd }
564 1.25 cgd if (cd->cd_devs[dev->dv_unit])
565 1.25 cgd panic("config_attach: duplicate %s", dev->dv_xname);
566 1.25 cgd cd->cd_devs[dev->dv_unit] = dev;
567 1.25 cgd
568 1.25 cgd /*
569 1.25 cgd * Before attaching, clobber any unfound devices that are
570 1.25 cgd * otherwise identical, or bump the unit number on all starred
571 1.25 cgd * cfdata for this device.
572 1.25 cgd */
573 1.25 cgd for (cf = cfdata; cf->cf_driver; cf++)
574 1.25 cgd if (cf->cf_driver == cd && cf->cf_unit == dev->dv_unit) {
575 1.25 cgd if (cf->cf_fstate == FSTATE_NOTFOUND)
576 1.25 cgd cf->cf_fstate = FSTATE_FOUND;
577 1.25 cgd if (cf->cf_fstate == FSTATE_STAR)
578 1.25 cgd cf->cf_unit++;
579 1.25 cgd }
580 1.27 drochner #if defined(__alpha__) || defined(hp300) || defined(__i386__)
581 1.25 cgd device_register(dev, aux);
582 1.25 cgd #endif
583 1.25 cgd (*ca->ca_attach)(parent, dev, aux);
584 1.25 cgd return (dev);
585 1.25 cgd }
586 1.25 cgd #endif /* __BROKEN_INDIRECT_CONFIG */
587 1.1 glass
588 1.1 glass /*
589 1.1 glass * Attach an event. These must come from initially-zero space (see
590 1.1 glass * commented-out assignments below), but that occurs naturally for
591 1.1 glass * device instance variables.
592 1.1 glass */
593 1.1 glass void
594 1.1 glass evcnt_attach(dev, name, ev)
595 1.1 glass struct device *dev;
596 1.1 glass const char *name;
597 1.1 glass struct evcnt *ev;
598 1.1 glass {
599 1.1 glass
600 1.1 glass #ifdef DIAGNOSTIC
601 1.1 glass if (strlen(name) >= sizeof(ev->ev_name))
602 1.1 glass panic("evcnt_attach");
603 1.1 glass #endif
604 1.1 glass /* ev->ev_next = NULL; */
605 1.1 glass ev->ev_dev = dev;
606 1.1 glass /* ev->ev_count = 0; */
607 1.1 glass strcpy(ev->ev_name, name);
608 1.20 cgd TAILQ_INSERT_TAIL(&allevents, ev, ev_list);
609 1.1 glass }
610