subr_autoconf.c revision 1.90 1 /* $NetBSD: subr_autoconf.c,v 1.90 2004/08/17 22:13:18 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1996, 2000 Christopher G. Demetriou
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the
18 * NetBSD Project. See http://www.NetBSD.org/ for
19 * information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
35 */
36
37 /*
38 * Copyright (c) 1992, 1993
39 * The Regents of the University of California. All rights reserved.
40 *
41 * This software was developed by the Computer Systems Engineering group
42 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
43 * contributed to Berkeley.
44 *
45 * All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Lawrence Berkeley Laboratories.
49 *
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
58 * 3. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 *
74 * from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL)
75 *
76 * @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94
77 */
78
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.90 2004/08/17 22:13:18 drochner Exp $");
81
82 #include "opt_ddb.h"
83
84 #include <sys/param.h>
85 #include <sys/device.h>
86 #include <sys/malloc.h>
87 #include <sys/systm.h>
88 #include <sys/kernel.h>
89 #include <sys/errno.h>
90 #include <sys/proc.h>
91 #include <sys/reboot.h>
92 #include <machine/limits.h>
93
94 #include "opt_userconf.h"
95 #ifdef USERCONF
96 #include <sys/userconf.h>
97 #endif
98
99 /*
100 * Autoconfiguration subroutines.
101 */
102
103 /*
104 * ioconf.c exports exactly two names: cfdata and cfroots. All system
105 * devices and drivers are found via these tables.
106 */
107 extern struct cfdata cfdata[];
108 extern const short cfroots[];
109
110 /*
111 * List of all cfdriver structures. We use this to detect duplicates
112 * when other cfdrivers are loaded.
113 */
114 struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
115 extern struct cfdriver * const cfdriver_list_initial[];
116
117 /*
118 * Initial list of cfattach's.
119 */
120 extern const struct cfattachinit cfattachinit[];
121
122 /*
123 * List of cfdata tables. We always have one such list -- the one
124 * built statically when the kernel was configured.
125 */
126 struct cftablelist allcftables;
127 static struct cftable initcftable;
128
129 /*
130 * Database of device properties.
131 */
132 propdb_t dev_propdb;
133
134 #define ROOT ((struct device *)NULL)
135
136 struct matchinfo {
137 cfmatch_t fn;
138 cfmatch_loc_t fn_loc;
139 struct device *parent;
140 const locdesc_t *ldesc;
141 void *aux;
142 struct cfdata *match;
143 int pri;
144 };
145
146 static char *number(char *, int);
147 static void mapply(struct matchinfo *, struct cfdata *);
148
149 struct deferred_config {
150 TAILQ_ENTRY(deferred_config) dc_queue;
151 struct device *dc_dev;
152 void (*dc_func)(struct device *);
153 };
154
155 TAILQ_HEAD(deferred_config_head, deferred_config);
156
157 struct deferred_config_head deferred_config_queue;
158 struct deferred_config_head interrupt_config_queue;
159
160 static void config_process_deferred(struct deferred_config_head *,
161 struct device *);
162
163 /* Hooks to finalize configuration once all real devices have been found. */
164 struct finalize_hook {
165 TAILQ_ENTRY(finalize_hook) f_list;
166 int (*f_func)(struct device *);
167 struct device *f_dev;
168 };
169 static TAILQ_HEAD(, finalize_hook) config_finalize_list;
170 static int config_finalize_done;
171
172 /* list of all devices */
173 struct devicelist alldevs;
174
175 __volatile int config_pending; /* semaphore for mountroot */
176
177 #define STREQ(s1, s2) \
178 (*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
179
180 static int config_initialized; /* config_init() has been called. */
181
182 static int config_do_twiddle;
183
184 /*
185 * Initialize the autoconfiguration data structures. Normally this
186 * is done by configure(), but some platforms need to do this very
187 * early (to e.g. initialize the console).
188 */
189 void
190 config_init(void)
191 {
192 const struct cfattachinit *cfai;
193 int i, j;
194
195 if (config_initialized)
196 return;
197
198 /* allcfdrivers is statically initialized. */
199 for (i = 0; cfdriver_list_initial[i] != NULL; i++) {
200 if (config_cfdriver_attach(cfdriver_list_initial[i]) != 0)
201 panic("configure: duplicate `%s' drivers",
202 cfdriver_list_initial[i]->cd_name);
203 }
204
205 for (cfai = &cfattachinit[0]; cfai->cfai_name != NULL; cfai++) {
206 for (j = 0; cfai->cfai_list[j] != NULL; j++) {
207 if (config_cfattach_attach(cfai->cfai_name,
208 cfai->cfai_list[j]) != 0)
209 panic("configure: duplicate `%s' attachment "
210 "of `%s' driver",
211 cfai->cfai_list[j]->ca_name,
212 cfai->cfai_name);
213 }
214 }
215
216 TAILQ_INIT(&allcftables);
217 initcftable.ct_cfdata = cfdata;
218 TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
219
220 TAILQ_INIT(&deferred_config_queue);
221 TAILQ_INIT(&interrupt_config_queue);
222 TAILQ_INIT(&config_finalize_list);
223 TAILQ_INIT(&alldevs);
224
225 config_initialized = 1;
226 }
227
228 /*
229 * Configure the system's hardware.
230 */
231 void
232 configure(void)
233 {
234 int errcnt;
235
236 /* Initialize data structures. */
237 config_init();
238
239 /* Initialize the device property database. */
240 dev_propdb = propdb_create("device properties");
241 if (dev_propdb == NULL)
242 panic("unable to create device property database");
243
244 #ifdef USERCONF
245 if (boothowto & RB_USERCONF)
246 user_config();
247 #endif
248
249 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
250 config_do_twiddle = 1;
251 printf_nolog("Detecting hardware...");
252 }
253
254 /*
255 * Do the machine-dependent portion of autoconfiguration. This
256 * sets the configuration machinery here in motion by "finding"
257 * the root bus. When this function returns, we expect interrupts
258 * to be enabled.
259 */
260 cpu_configure();
261
262 /*
263 * Now that we've found all the hardware, start the real time
264 * and statistics clocks.
265 */
266 initclocks();
267
268 cold = 0; /* clocks are running, we're warm now! */
269
270 /*
271 * Now callback to finish configuration for devices which want
272 * to do this once interrupts are enabled.
273 */
274 config_process_deferred(&interrupt_config_queue, NULL);
275
276 errcnt = aprint_get_error_count();
277 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
278 (boothowto & AB_VERBOSE) == 0) {
279 if (config_do_twiddle) {
280 config_do_twiddle = 0;
281 printf_nolog("done.\n");
282 }
283 if (errcnt != 0) {
284 printf("WARNING: %d error%s while detecting hardware; "
285 "check system log.\n", errcnt,
286 errcnt == 1 ? "" : "s");
287 }
288 }
289 }
290
291 /*
292 * Add a cfdriver to the system.
293 */
294 int
295 config_cfdriver_attach(struct cfdriver *cd)
296 {
297 struct cfdriver *lcd;
298
299 /* Make sure this driver isn't already in the system. */
300 LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
301 if (STREQ(lcd->cd_name, cd->cd_name))
302 return (EEXIST);
303 }
304
305 LIST_INIT(&cd->cd_attach);
306 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
307
308 return (0);
309 }
310
311 /*
312 * Remove a cfdriver from the system.
313 */
314 int
315 config_cfdriver_detach(struct cfdriver *cd)
316 {
317 int i;
318
319 /* Make sure there are no active instances. */
320 for (i = 0; i < cd->cd_ndevs; i++) {
321 if (cd->cd_devs[i] != NULL)
322 return (EBUSY);
323 }
324
325 /* ...and no attachments loaded. */
326 if (LIST_EMPTY(&cd->cd_attach) == 0)
327 return (EBUSY);
328
329 LIST_REMOVE(cd, cd_list);
330
331 KASSERT(cd->cd_devs == NULL);
332
333 return (0);
334 }
335
336 /*
337 * Look up a cfdriver by name.
338 */
339 struct cfdriver *
340 config_cfdriver_lookup(const char *name)
341 {
342 struct cfdriver *cd;
343
344 LIST_FOREACH(cd, &allcfdrivers, cd_list) {
345 if (STREQ(cd->cd_name, name))
346 return (cd);
347 }
348
349 return (NULL);
350 }
351
352 /*
353 * Add a cfattach to the specified driver.
354 */
355 int
356 config_cfattach_attach(const char *driver, struct cfattach *ca)
357 {
358 struct cfattach *lca;
359 struct cfdriver *cd;
360
361 cd = config_cfdriver_lookup(driver);
362 if (cd == NULL)
363 return (ESRCH);
364
365 /* Make sure this attachment isn't already on this driver. */
366 LIST_FOREACH(lca, &cd->cd_attach, ca_list) {
367 if (STREQ(lca->ca_name, ca->ca_name))
368 return (EEXIST);
369 }
370
371 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list);
372
373 return (0);
374 }
375
376 /*
377 * Remove a cfattach from the specified driver.
378 */
379 int
380 config_cfattach_detach(const char *driver, struct cfattach *ca)
381 {
382 struct cfdriver *cd;
383 struct device *dev;
384 int i;
385
386 cd = config_cfdriver_lookup(driver);
387 if (cd == NULL)
388 return (ESRCH);
389
390 /* Make sure there are no active instances. */
391 for (i = 0; i < cd->cd_ndevs; i++) {
392 if ((dev = cd->cd_devs[i]) == NULL)
393 continue;
394 if (dev->dv_cfattach == ca)
395 return (EBUSY);
396 }
397
398 LIST_REMOVE(ca, ca_list);
399
400 return (0);
401 }
402
403 /*
404 * Look up a cfattach by name.
405 */
406 static struct cfattach *
407 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname)
408 {
409 struct cfattach *ca;
410
411 LIST_FOREACH(ca, &cd->cd_attach, ca_list) {
412 if (STREQ(ca->ca_name, atname))
413 return (ca);
414 }
415
416 return (NULL);
417 }
418
419 /*
420 * Look up a cfattach by driver/attachment name.
421 */
422 struct cfattach *
423 config_cfattach_lookup(const char *name, const char *atname)
424 {
425 struct cfdriver *cd;
426
427 cd = config_cfdriver_lookup(name);
428 if (cd == NULL)
429 return (NULL);
430
431 return (config_cfattach_lookup_cd(cd, atname));
432 }
433
434 /*
435 * Apply the matching function and choose the best. This is used
436 * a few times and we want to keep the code small.
437 */
438 static void
439 mapply(struct matchinfo *m, struct cfdata *cf)
440 {
441 int pri;
442
443 if (m->fn != NULL) {
444 KASSERT(m->fn_loc == NULL);
445 pri = (*m->fn)(m->parent, cf, m->aux);
446 } else if (m->fn_loc != NULL) {
447 pri = (*m->fn_loc)(m->parent, cf, m->ldesc, m->aux);
448 } else {
449 struct cfattach *ca;
450
451 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
452 if (ca == NULL) {
453 /* No attachment for this entry, oh well. */
454 return;
455 }
456 if (ca->ca_match == NULL) {
457 panic("mapply: no match function for '%s' attachment "
458 "of '%s'", cf->cf_atname, cf->cf_name);
459 }
460 pri = (*ca->ca_match)(m->parent, cf, m->aux);
461 }
462 if (pri > m->pri) {
463 m->match = cf;
464 m->pri = pri;
465 }
466 }
467
468 /*
469 * Determine if `parent' is a potential parent for a device spec based
470 * on `cfp'.
471 */
472 static int
473 cfparent_match(const struct device *parent, const struct cfparent *cfp)
474 {
475 struct cfdriver *pcd;
476 const char * const *cpp;
477 const char *cp;
478
479 /* We don't match root nodes here. */
480 if (cfp == NULL)
481 return (0);
482
483 pcd = parent->dv_cfdriver;
484 KASSERT(pcd != NULL);
485
486 /*
487 * First, ensure this parent has the correct interface
488 * attribute.
489 */
490 if (pcd->cd_attrs == NULL)
491 return (0); /* no interface attributes -> no children */
492 for (cpp = pcd->cd_attrs; (cp = *cpp) != NULL; cpp++) {
493 if (STREQ(cp, cfp->cfp_iattr)) {
494 /* Match. */
495 break;
496 }
497 }
498 if (cp == NULL)
499 return (0); /* doesn't carry the req'd attribute */
500
501 /*
502 * If no specific parent device instance was specified (i.e.
503 * we're attaching to the attribute only), we're done!
504 */
505 if (cfp->cfp_parent == NULL)
506 return (1);
507
508 /*
509 * Check the parent device's name.
510 */
511 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
512 return (0); /* not the same parent */
513
514 /*
515 * Make sure the unit number matches.
516 */
517 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */
518 cfp->cfp_unit == parent->dv_unit)
519 return (1);
520
521 /* Unit numbers don't match. */
522 return (0);
523 }
524
525 /*
526 * Helper for config_cfdata_attach(): check all devices whether it could be
527 * parent any attachment in the config data table passed, and rescan.
528 */
529 static void
530 rescan_with_cfdata(const struct cfdata *cf)
531 {
532 struct device *d;
533 const struct cfdata *cf1;
534
535 /*
536 * "alldevs" is likely longer than an LKM's cfdata, so make it
537 * the outer loop.
538 */
539 TAILQ_FOREACH(d, &alldevs, dv_list) {
540
541 if (!(d->dv_cfattach->ca_rescan))
542 continue;
543
544 for (cf1 = cf; cf1->cf_name; cf1++) {
545
546 if (!cfparent_match(d, cf1->cf_pspec))
547 continue;
548
549 (*d->dv_cfattach->ca_rescan)(d,
550 cf1->cf_pspec->cfp_iattr, cf1->cf_loc);
551 }
552 }
553 }
554
555 /*
556 * Attach a supplemental config data table and rescan potential
557 * parent devices if required.
558 */
559 int
560 config_cfdata_attach(struct cfdata *cf, int scannow)
561 {
562 struct cftable *ct;
563
564 ct = malloc(sizeof(struct cftable), M_DEVBUF, M_WAITOK);
565 ct->ct_cfdata = cf;
566 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
567
568 if (scannow)
569 rescan_with_cfdata(cf);
570
571 return (0);
572 }
573
574 /*
575 * Helper for config_cfdata_detach: check whether a device is
576 * found through any attachment in the config data table.
577 */
578 static int
579 dev_in_cfdata(const struct device *d, const struct cfdata *cf)
580 {
581 const struct cfdata *cf1;
582
583 for (cf1 = cf; cf1->cf_name; cf1++)
584 if (d->dv_cfdata == cf1)
585 return (1);
586
587 return (0);
588 }
589
590 /*
591 * Detach a supplemental config data table. Detach all devices found
592 * through that table (and thus keeping references to it) before.
593 */
594 int
595 config_cfdata_detach(struct cfdata *cf)
596 {
597 struct device *d;
598 int error;
599 struct cftable *ct;
600
601 again:
602 TAILQ_FOREACH(d, &alldevs, dv_list) {
603 if (dev_in_cfdata(d, cf)) {
604 error = config_detach(d, 0);
605 if (error) {
606 aprint_error("%s: unable to detach instance\n",
607 d->dv_xname);
608 return (error);
609 }
610 goto again;
611 }
612 }
613
614 TAILQ_FOREACH(ct, &allcftables, ct_list) {
615 if (ct->ct_cfdata == cf) {
616 TAILQ_REMOVE(&allcftables, ct, ct_list);
617 free(ct, M_DEVBUF);
618 return (0);
619 }
620 }
621
622 /* not found -- shouldn't happen */
623 return (EINVAL);
624 }
625
626 /*
627 * Invoke the "match" routine for a cfdata entry on behalf of
628 * an external caller, usually a "submatch" routine.
629 */
630 int
631 config_match(struct device *parent, struct cfdata *cf, void *aux)
632 {
633 struct cfattach *ca;
634
635 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
636 if (ca == NULL) {
637 /* No attachment for this entry, oh well. */
638 return (0);
639 }
640
641 return ((*ca->ca_match)(parent, cf, aux));
642 }
643
644 /*
645 * Iterate over all potential children of some device, calling the given
646 * function (default being the child's match function) for each one.
647 * Nonzero returns are matches; the highest value returned is considered
648 * the best match. Return the `found child' if we got a match, or NULL
649 * otherwise. The `aux' pointer is simply passed on through.
650 *
651 * Note that this function is designed so that it can be used to apply
652 * an arbitrary function to all potential children (its return value
653 * can be ignored).
654 */
655 struct cfdata *
656 config_search(cfmatch_t fn, struct device *parent, void *aux)
657 {
658 struct cftable *ct;
659 struct cfdata *cf;
660 struct matchinfo m;
661
662 KASSERT(config_initialized);
663
664 m.fn = fn;
665 m.fn_loc = NULL;
666 m.parent = parent;
667 m.aux = aux;
668 m.match = NULL;
669 m.pri = 0;
670
671 TAILQ_FOREACH(ct, &allcftables, ct_list) {
672 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
673 /*
674 * Skip cf if no longer eligible, otherwise scan
675 * through parents for one matching `parent', and
676 * try match function.
677 */
678 if (cf->cf_fstate == FSTATE_FOUND)
679 continue;
680 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
681 cf->cf_fstate == FSTATE_DSTAR)
682 continue;
683 if (cfparent_match(parent, cf->cf_pspec))
684 mapply(&m, cf);
685 }
686 }
687 return (m.match);
688 }
689
690 /* same as above, with real locators passed */
691 struct cfdata *
692 config_search_loc(cfmatch_loc_t fn, struct device *parent,
693 const char *ifattr, const locdesc_t *ldesc, void *aux)
694 {
695 struct cftable *ct;
696 struct cfdata *cf;
697 struct matchinfo m;
698
699 KASSERT(config_initialized);
700
701 m.fn = NULL;
702 m.fn_loc = fn;
703 m.parent = parent;
704 m.ldesc = ldesc;
705 m.aux = aux;
706 m.match = NULL;
707 m.pri = 0;
708
709 TAILQ_FOREACH(ct, &allcftables, ct_list) {
710 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
711
712 /* We don't match root nodes here. */
713 if (!cf->cf_pspec)
714 continue;
715
716 /*
717 * Skip cf if no longer eligible, otherwise scan
718 * through parents for one matching `parent', and
719 * try match function.
720 */
721 if (cf->cf_fstate == FSTATE_FOUND)
722 continue;
723 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
724 cf->cf_fstate == FSTATE_DSTAR)
725 continue;
726
727 /*
728 * If an interface attribute was specified,
729 * consider only children which attach to
730 * that attribute.
731 */
732 if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr))
733 continue;
734
735 if (cfparent_match(parent, cf->cf_pspec))
736 mapply(&m, cf);
737 }
738 }
739 return (m.match);
740 }
741
742 /*
743 * Find the given root device.
744 * This is much like config_search, but there is no parent.
745 * Don't bother with multiple cfdata tables; the root node
746 * must always be in the initial table.
747 */
748 struct cfdata *
749 config_rootsearch(cfmatch_t fn, const char *rootname, void *aux)
750 {
751 struct cfdata *cf;
752 const short *p;
753 struct matchinfo m;
754
755 m.fn = fn;
756 m.fn_loc = NULL;
757 m.parent = ROOT;
758 m.aux = aux;
759 m.match = NULL;
760 m.pri = 0;
761 /*
762 * Look at root entries for matching name. We do not bother
763 * with found-state here since only one root should ever be
764 * searched (and it must be done first).
765 */
766 for (p = cfroots; *p >= 0; p++) {
767 cf = &cfdata[*p];
768 if (strcmp(cf->cf_name, rootname) == 0)
769 mapply(&m, cf);
770 }
771 return (m.match);
772 }
773
774 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
775
776 /*
777 * The given `aux' argument describes a device that has been found
778 * on the given parent, but not necessarily configured. Locate the
779 * configuration data for that device (using the submatch function
780 * provided, or using candidates' cd_match configuration driver
781 * functions) and attach it, and return true. If the device was
782 * not configured, call the given `print' function and return 0.
783 */
784 struct device *
785 config_found_sm(struct device *parent, void *aux, cfprint_t print,
786 cfmatch_t submatch)
787 {
788 struct cfdata *cf;
789
790 if ((cf = config_search(submatch, parent, aux)) != NULL)
791 return (config_attach(parent, cf, aux, print));
792 if (print) {
793 if (config_do_twiddle)
794 twiddle();
795 aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]);
796 }
797 return (NULL);
798 }
799
800 /* same as above, with real locators passed */
801 struct device *
802 config_found_sm_loc(struct device *parent,
803 const char *ifattr, const locdesc_t *ldesc, void *aux,
804 cfprint_t print, cfmatch_loc_t submatch)
805 {
806 struct cfdata *cf;
807
808 if ((cf = config_search_loc(submatch, parent, ifattr, ldesc, aux)))
809 return(config_attach_loc(parent, cf, ldesc, aux, print));
810 if (print) {
811 if (config_do_twiddle)
812 twiddle();
813 aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]);
814 }
815 return (NULL);
816 }
817
818 /*
819 * As above, but for root devices.
820 */
821 struct device *
822 config_rootfound(const char *rootname, void *aux)
823 {
824 struct cfdata *cf;
825
826 if ((cf = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
827 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
828 aprint_error("root device %s not configured\n", rootname);
829 return (NULL);
830 }
831
832 /* just like sprintf(buf, "%d") except that it works from the end */
833 static char *
834 number(char *ep, int n)
835 {
836
837 *--ep = 0;
838 while (n >= 10) {
839 *--ep = (n % 10) + '0';
840 n /= 10;
841 }
842 *--ep = n + '0';
843 return (ep);
844 }
845
846 /*
847 * Expand the size of the cd_devs array if necessary.
848 */
849 void
850 config_makeroom(int n, struct cfdriver *cd)
851 {
852 int old, new;
853 void **nsp;
854
855 if (n < cd->cd_ndevs)
856 return;
857
858 /*
859 * Need to expand the array.
860 */
861 old = cd->cd_ndevs;
862 if (old == 0)
863 new = MINALLOCSIZE / sizeof(void *);
864 else
865 new = old * 2;
866 while (new <= n)
867 new *= 2;
868 cd->cd_ndevs = new;
869 nsp = malloc(new * sizeof(void *), M_DEVBUF,
870 cold ? M_NOWAIT : M_WAITOK);
871 if (nsp == NULL)
872 panic("config_attach: %sing dev array",
873 old != 0 ? "expand" : "creat");
874 memset(nsp + old, 0, (new - old) * sizeof(void *));
875 if (old != 0) {
876 memcpy(nsp, cd->cd_devs, old * sizeof(void *));
877 free(cd->cd_devs, M_DEVBUF);
878 }
879 cd->cd_devs = nsp;
880 }
881
882 /*
883 * Attach a found device. Allocates memory for device variables.
884 */
885 struct device *
886 config_attach_loc(struct device *parent, struct cfdata *cf,
887 const locdesc_t *ldesc, void *aux, cfprint_t print)
888 {
889 struct device *dev;
890 struct cftable *ct;
891 struct cfdriver *cd;
892 struct cfattach *ca;
893 size_t lname, lunit;
894 const char *xunit;
895 int myunit;
896 char num[10];
897
898 cd = config_cfdriver_lookup(cf->cf_name);
899 KASSERT(cd != NULL);
900
901 ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
902 KASSERT(ca != NULL);
903
904 if (ca->ca_devsize < sizeof(struct device))
905 panic("config_attach");
906
907 #ifndef __BROKEN_CONFIG_UNIT_USAGE
908 if (cf->cf_fstate == FSTATE_STAR) {
909 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
910 if (cd->cd_devs[myunit] == NULL)
911 break;
912 /*
913 * myunit is now the unit of the first NULL device pointer,
914 * or max(cd->cd_ndevs,cf->cf_unit).
915 */
916 } else {
917 myunit = cf->cf_unit;
918 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
919 cf->cf_fstate = FSTATE_FOUND;
920 }
921 #else
922 myunit = cf->cf_unit;
923 if (cf->cf_fstate == FSTATE_STAR)
924 cf->cf_unit++;
925 else {
926 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
927 cf->cf_fstate = FSTATE_FOUND;
928 }
929 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
930
931 /* compute length of name and decimal expansion of unit number */
932 lname = strlen(cd->cd_name);
933 xunit = number(&num[sizeof(num)], myunit);
934 lunit = &num[sizeof(num)] - xunit;
935 if (lname + lunit > sizeof(dev->dv_xname))
936 panic("config_attach: device name too long");
937
938 /* get memory for all device vars */
939 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
940 cold ? M_NOWAIT : M_WAITOK);
941 if (!dev)
942 panic("config_attach: memory allocation for device softc failed");
943 memset(dev, 0, ca->ca_devsize);
944 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
945 dev->dv_class = cd->cd_class;
946 dev->dv_cfdata = cf;
947 dev->dv_cfdriver = cd;
948 dev->dv_cfattach = ca;
949 dev->dv_unit = myunit;
950 memcpy(dev->dv_xname, cd->cd_name, lname);
951 memcpy(dev->dv_xname + lname, xunit, lunit);
952 dev->dv_parent = parent;
953 dev->dv_flags = DVF_ACTIVE; /* always initially active */
954 if (ldesc) {
955 dev->dv_locators = malloc(ldesc->len * sizeof(int),
956 M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
957 memcpy(dev->dv_locators, ldesc->locs, ldesc->len * sizeof(int));
958 }
959
960 if (config_do_twiddle)
961 twiddle();
962 else
963 aprint_naive("Found ");
964 /*
965 * We want the next two printfs for normal, verbose, and quiet,
966 * but not silent (in which case, we're twiddling, instead).
967 */
968 if (parent == ROOT) {
969 aprint_naive("%s (root)", dev->dv_xname);
970 aprint_normal("%s (root)", dev->dv_xname);
971 } else {
972 aprint_naive("%s at %s", dev->dv_xname, parent->dv_xname);
973 aprint_normal("%s at %s", dev->dv_xname, parent->dv_xname);
974 if (print)
975 (void) (*print)(aux, NULL);
976 }
977
978 /* put this device in the devices array */
979 config_makeroom(dev->dv_unit, cd);
980 if (cd->cd_devs[dev->dv_unit])
981 panic("config_attach: duplicate %s", dev->dv_xname);
982 cd->cd_devs[dev->dv_unit] = dev;
983
984 /*
985 * Before attaching, clobber any unfound devices that are
986 * otherwise identical.
987 */
988 TAILQ_FOREACH(ct, &allcftables, ct_list) {
989 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
990 if (STREQ(cf->cf_name, cd->cd_name) &&
991 cf->cf_unit == dev->dv_unit) {
992 if (cf->cf_fstate == FSTATE_NOTFOUND)
993 cf->cf_fstate = FSTATE_FOUND;
994 #ifdef __BROKEN_CONFIG_UNIT_USAGE
995 /*
996 * Bump the unit number on all starred cfdata
997 * entries for this device.
998 */
999 if (cf->cf_fstate == FSTATE_STAR)
1000 cf->cf_unit++;
1001 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
1002 }
1003 }
1004 }
1005 #ifdef __HAVE_DEVICE_REGISTER
1006 device_register(dev, aux);
1007 #endif
1008 (*ca->ca_attach)(parent, dev, aux);
1009 config_process_deferred(&deferred_config_queue, dev);
1010 return (dev);
1011 }
1012
1013 /*
1014 * As above, but for pseudo-devices. Pseudo-devices attached in this
1015 * way are silently inserted into the device tree, and their children
1016 * attached.
1017 *
1018 * Note that because pseudo-devices are attached silently, any information
1019 * the attach routine wishes to print should be prefixed with the device
1020 * name by the attach routine.
1021 */
1022 struct device *
1023 config_attach_pseudo(const char *name, int unit)
1024 {
1025 struct device *dev;
1026 struct cfdriver *cd;
1027 struct cfattach *ca;
1028 size_t lname, lunit;
1029 const char *xunit;
1030 int myunit;
1031 char num[10];
1032
1033 cd = config_cfdriver_lookup(name);
1034 if (cd == NULL)
1035 return (NULL);
1036
1037 ca = config_cfattach_lookup_cd(cd, name);
1038 if (ca == NULL)
1039 return (NULL);
1040
1041 if (ca->ca_devsize < sizeof(struct device))
1042 panic("config_attach_pseudo");
1043
1044 if (unit == DVUNIT_ANY) {
1045 for (myunit = 0; myunit < cd->cd_ndevs; myunit++)
1046 if (cd->cd_devs[myunit] == NULL)
1047 break;
1048 /*
1049 * myunit is now the unit of the first NULL device pointer.
1050 */
1051 } else {
1052 myunit = unit;
1053 if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL)
1054 return (NULL);
1055 }
1056
1057 /* compute length of name and decimal expansion of unit number */
1058 lname = strlen(cd->cd_name);
1059 xunit = number(&num[sizeof(num)], myunit);
1060 lunit = &num[sizeof(num)] - xunit;
1061 if (lname + lunit > sizeof(dev->dv_xname))
1062 panic("config_attach_pseudo: device name too long");
1063
1064 /* get memory for all device vars */
1065 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
1066 cold ? M_NOWAIT : M_WAITOK);
1067 if (!dev)
1068 panic("config_attach_pseudo: memory allocation for device "
1069 "softc failed");
1070 memset(dev, 0, ca->ca_devsize);
1071 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
1072 dev->dv_class = cd->cd_class;
1073 dev->dv_cfdata = NULL;
1074 dev->dv_cfdriver = cd;
1075 dev->dv_cfattach = ca;
1076 dev->dv_unit = myunit;
1077 memcpy(dev->dv_xname, cd->cd_name, lname);
1078 memcpy(dev->dv_xname + lname, xunit, lunit);
1079 dev->dv_parent = ROOT;
1080 dev->dv_flags = DVF_ACTIVE; /* always initially active */
1081
1082 /* put this device in the devices array */
1083 config_makeroom(dev->dv_unit, cd);
1084 if (cd->cd_devs[dev->dv_unit])
1085 panic("config_attach_pseudo: duplicate %s", dev->dv_xname);
1086 cd->cd_devs[dev->dv_unit] = dev;
1087
1088 #if 0 /* XXXJRT not yet */
1089 #ifdef __HAVE_DEVICE_REGISTER
1090 device_register(dev, NULL); /* like a root node */
1091 #endif
1092 #endif
1093 (*ca->ca_attach)(ROOT, dev, NULL);
1094 config_process_deferred(&deferred_config_queue, dev);
1095 return (dev);
1096 }
1097
1098 /*
1099 * Detach a device. Optionally forced (e.g. because of hardware
1100 * removal) and quiet. Returns zero if successful, non-zero
1101 * (an error code) otherwise.
1102 *
1103 * Note that this code wants to be run from a process context, so
1104 * that the detach can sleep to allow processes which have a device
1105 * open to run and unwind their stacks.
1106 */
1107 int
1108 config_detach(struct device *dev, int flags)
1109 {
1110 struct cftable *ct;
1111 struct cfdata *cf;
1112 const struct cfattach *ca;
1113 struct cfdriver *cd;
1114 #ifdef DIAGNOSTIC
1115 struct device *d;
1116 #endif
1117 int rv = 0, i;
1118
1119 #ifdef DIAGNOSTIC
1120 if (dev->dv_cfdata != NULL &&
1121 dev->dv_cfdata->cf_fstate != FSTATE_FOUND &&
1122 dev->dv_cfdata->cf_fstate != FSTATE_STAR)
1123 panic("config_detach: bad device fstate");
1124 #endif
1125 cd = dev->dv_cfdriver;
1126 KASSERT(cd != NULL);
1127
1128 ca = dev->dv_cfattach;
1129 KASSERT(ca != NULL);
1130
1131 /*
1132 * Ensure the device is deactivated. If the device doesn't
1133 * have an activation entry point, we allow DVF_ACTIVE to
1134 * remain set. Otherwise, if DVF_ACTIVE is still set, the
1135 * device is busy, and the detach fails.
1136 */
1137 if (ca->ca_activate != NULL)
1138 rv = config_deactivate(dev);
1139
1140 /*
1141 * Try to detach the device. If that's not possible, then
1142 * we either panic() (for the forced but failed case), or
1143 * return an error.
1144 */
1145 if (rv == 0) {
1146 if (ca->ca_detach != NULL)
1147 rv = (*ca->ca_detach)(dev, flags);
1148 else
1149 rv = EOPNOTSUPP;
1150 }
1151 if (rv != 0) {
1152 if ((flags & DETACH_FORCE) == 0)
1153 return (rv);
1154 else
1155 panic("config_detach: forced detach of %s failed (%d)",
1156 dev->dv_xname, rv);
1157 }
1158
1159 /*
1160 * The device has now been successfully detached.
1161 */
1162
1163 #ifdef DIAGNOSTIC
1164 /*
1165 * Sanity: If you're successfully detached, you should have no
1166 * children. (Note that because children must be attached
1167 * after parents, we only need to search the latter part of
1168 * the list.)
1169 */
1170 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
1171 d = TAILQ_NEXT(d, dv_list)) {
1172 if (d->dv_parent == dev) {
1173 printf("config_detach: detached device %s"
1174 " has children %s\n", dev->dv_xname, d->dv_xname);
1175 panic("config_detach");
1176 }
1177 }
1178 #endif
1179
1180 /* notify the parent that the child is gone */
1181 if (dev->dv_parent) {
1182 struct device *p = dev->dv_parent;
1183 if (p->dv_cfattach->ca_childdetached)
1184 (*p->dv_cfattach->ca_childdetached)(p, dev);
1185 }
1186
1187 /*
1188 * Mark cfdata to show that the unit can be reused, if possible.
1189 */
1190 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1191 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1192 if (STREQ(cf->cf_name, cd->cd_name)) {
1193 if (cf->cf_fstate == FSTATE_FOUND &&
1194 cf->cf_unit == dev->dv_unit)
1195 cf->cf_fstate = FSTATE_NOTFOUND;
1196 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1197 /*
1198 * Note that we can only re-use a starred
1199 * unit number if the unit being detached
1200 * had the last assigned unit number.
1201 */
1202 if (cf->cf_fstate == FSTATE_STAR &&
1203 cf->cf_unit == dev->dv_unit + 1)
1204 cf->cf_unit--;
1205 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
1206 }
1207 }
1208 }
1209
1210 /*
1211 * Unlink from device list.
1212 */
1213 TAILQ_REMOVE(&alldevs, dev, dv_list);
1214
1215 /*
1216 * Remove from cfdriver's array, tell the world (unless it was
1217 * a pseudo-device), and free softc.
1218 */
1219 cd->cd_devs[dev->dv_unit] = NULL;
1220 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
1221 aprint_normal("%s detached\n", dev->dv_xname);
1222 if (dev->dv_locators)
1223 free(dev->dv_locators, M_DEVBUF);
1224 free(dev, M_DEVBUF);
1225
1226 /*
1227 * If the device now has no units in use, deallocate its softc array.
1228 */
1229 for (i = 0; i < cd->cd_ndevs; i++)
1230 if (cd->cd_devs[i] != NULL)
1231 break;
1232 if (i == cd->cd_ndevs) { /* nothing found; deallocate */
1233 free(cd->cd_devs, M_DEVBUF);
1234 cd->cd_devs = NULL;
1235 cd->cd_ndevs = 0;
1236 }
1237
1238 /*
1239 * Return success.
1240 */
1241 return (0);
1242 }
1243
1244 int
1245 config_activate(struct device *dev)
1246 {
1247 const struct cfattach *ca = dev->dv_cfattach;
1248 int rv = 0, oflags = dev->dv_flags;
1249
1250 if (ca->ca_activate == NULL)
1251 return (EOPNOTSUPP);
1252
1253 if ((dev->dv_flags & DVF_ACTIVE) == 0) {
1254 dev->dv_flags |= DVF_ACTIVE;
1255 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
1256 if (rv)
1257 dev->dv_flags = oflags;
1258 }
1259 return (rv);
1260 }
1261
1262 int
1263 config_deactivate(struct device *dev)
1264 {
1265 const struct cfattach *ca = dev->dv_cfattach;
1266 int rv = 0, oflags = dev->dv_flags;
1267
1268 if (ca->ca_activate == NULL)
1269 return (EOPNOTSUPP);
1270
1271 if (dev->dv_flags & DVF_ACTIVE) {
1272 dev->dv_flags &= ~DVF_ACTIVE;
1273 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
1274 if (rv)
1275 dev->dv_flags = oflags;
1276 }
1277 return (rv);
1278 }
1279
1280 /*
1281 * Defer the configuration of the specified device until all
1282 * of its parent's devices have been attached.
1283 */
1284 void
1285 config_defer(struct device *dev, void (*func)(struct device *))
1286 {
1287 struct deferred_config *dc;
1288
1289 if (dev->dv_parent == NULL)
1290 panic("config_defer: can't defer config of a root device");
1291
1292 #ifdef DIAGNOSTIC
1293 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
1294 dc = TAILQ_NEXT(dc, dc_queue)) {
1295 if (dc->dc_dev == dev)
1296 panic("config_defer: deferred twice");
1297 }
1298 #endif
1299
1300 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1301 if (dc == NULL)
1302 panic("config_defer: unable to allocate callback");
1303
1304 dc->dc_dev = dev;
1305 dc->dc_func = func;
1306 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
1307 config_pending_incr();
1308 }
1309
1310 /*
1311 * Defer some autoconfiguration for a device until after interrupts
1312 * are enabled.
1313 */
1314 void
1315 config_interrupts(struct device *dev, void (*func)(struct device *))
1316 {
1317 struct deferred_config *dc;
1318
1319 /*
1320 * If interrupts are enabled, callback now.
1321 */
1322 if (cold == 0) {
1323 (*func)(dev);
1324 return;
1325 }
1326
1327 #ifdef DIAGNOSTIC
1328 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
1329 dc = TAILQ_NEXT(dc, dc_queue)) {
1330 if (dc->dc_dev == dev)
1331 panic("config_interrupts: deferred twice");
1332 }
1333 #endif
1334
1335 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1336 if (dc == NULL)
1337 panic("config_interrupts: unable to allocate callback");
1338
1339 dc->dc_dev = dev;
1340 dc->dc_func = func;
1341 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
1342 config_pending_incr();
1343 }
1344
1345 /*
1346 * Process a deferred configuration queue.
1347 */
1348 static void
1349 config_process_deferred(struct deferred_config_head *queue,
1350 struct device *parent)
1351 {
1352 struct deferred_config *dc, *ndc;
1353
1354 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
1355 ndc = TAILQ_NEXT(dc, dc_queue);
1356 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
1357 TAILQ_REMOVE(queue, dc, dc_queue);
1358 (*dc->dc_func)(dc->dc_dev);
1359 free(dc, M_DEVBUF);
1360 config_pending_decr();
1361 }
1362 }
1363 }
1364
1365 /*
1366 * Manipulate the config_pending semaphore.
1367 */
1368 void
1369 config_pending_incr(void)
1370 {
1371
1372 config_pending++;
1373 }
1374
1375 void
1376 config_pending_decr(void)
1377 {
1378
1379 #ifdef DIAGNOSTIC
1380 if (config_pending == 0)
1381 panic("config_pending_decr: config_pending == 0");
1382 #endif
1383 config_pending--;
1384 if (config_pending == 0)
1385 wakeup((void *)&config_pending);
1386 }
1387
1388 /*
1389 * Register a "finalization" routine. Finalization routines are
1390 * called iteratively once all real devices have been found during
1391 * autoconfiguration, for as long as any one finalizer has done
1392 * any work.
1393 */
1394 int
1395 config_finalize_register(struct device *dev, int (*fn)(struct device *))
1396 {
1397 struct finalize_hook *f;
1398
1399 /*
1400 * If finalization has already been done, invoke the
1401 * callback function now.
1402 */
1403 if (config_finalize_done) {
1404 while ((*fn)(dev) != 0)
1405 /* loop */ ;
1406 }
1407
1408 /* Ensure this isn't already on the list. */
1409 TAILQ_FOREACH(f, &config_finalize_list, f_list) {
1410 if (f->f_func == fn && f->f_dev == dev)
1411 return (EEXIST);
1412 }
1413
1414 f = malloc(sizeof(*f), M_TEMP, M_WAITOK);
1415 f->f_func = fn;
1416 f->f_dev = dev;
1417 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
1418
1419 return (0);
1420 }
1421
1422 void
1423 config_finalize(void)
1424 {
1425 struct finalize_hook *f;
1426 int rv;
1427
1428 /* Run the hooks until none of them does any work. */
1429 do {
1430 rv = 0;
1431 TAILQ_FOREACH(f, &config_finalize_list, f_list)
1432 rv |= (*f->f_func)(f->f_dev);
1433 } while (rv != 0);
1434
1435 config_finalize_done = 1;
1436
1437 /* Now free all the hooks. */
1438 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
1439 TAILQ_REMOVE(&config_finalize_list, f, f_list);
1440 free(f, M_TEMP);
1441 }
1442 }
1443
1444