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