subr_autoconf.c revision 1.100 1 /* $NetBSD: subr_autoconf.c,v 1.100 2005/08/29 19:13:48 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.100 2005/08/29 19:13:48 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;
138 struct device *parent;
139 const int *locs;
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 != NULL) {
443 pri = (*m->fn)(m->parent, cf, m->locs, m->aux);
444 } else {
445 pri = config_match(m->parent, cf, m->aux);
446 }
447 if (pri > m->pri) {
448 m->match = cf;
449 m->pri = pri;
450 }
451 }
452
453 int
454 config_stdsubmatch(struct device *parent, struct cfdata *cf,
455 const int *locs, void *aux)
456 {
457 const struct cfiattrdata *ci;
458 const struct cflocdesc *cl;
459 int nlocs, i;
460
461 ci = cfiattr_lookup(cf->cf_pspec->cfp_iattr, parent->dv_cfdriver);
462 KASSERT(ci);
463 nlocs = ci->ci_loclen;
464 for (i = 0; i < nlocs; i++) {
465 cl = &ci->ci_locdesc[i];
466 /* !cld_defaultstr means no default value */
467 if ((!(cl->cld_defaultstr)
468 || (cf->cf_loc[i] != cl->cld_default))
469 && cf->cf_loc[i] != locs[i])
470 return (0);
471 }
472
473 return (config_match(parent, cf, aux));
474 }
475
476 /*
477 * Helper function: check whether the driver supports the interface attribute
478 * and return its descriptor structure.
479 */
480 static const struct cfiattrdata *
481 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia)
482 {
483 const struct cfiattrdata * const *cpp;
484
485 if (cd->cd_attrs == NULL)
486 return (0);
487
488 for (cpp = cd->cd_attrs; *cpp; cpp++) {
489 if (STREQ((*cpp)->ci_name, ia)) {
490 /* Match. */
491 return (*cpp);
492 }
493 }
494 return (0);
495 }
496
497 /*
498 * Lookup an interface attribute description by name.
499 * If the driver is given, consider only its supported attributes.
500 */
501 const struct cfiattrdata *
502 cfiattr_lookup(const char *name, const struct cfdriver *cd)
503 {
504 const struct cfdriver *d;
505 const struct cfiattrdata *ia;
506
507 if (cd)
508 return (cfdriver_get_iattr(cd, name));
509
510 LIST_FOREACH(d, &allcfdrivers, cd_list) {
511 ia = cfdriver_get_iattr(d, name);
512 if (ia)
513 return (ia);
514 }
515 return (0);
516 }
517
518 /*
519 * Determine if `parent' is a potential parent for a device spec based
520 * on `cfp'.
521 */
522 static int
523 cfparent_match(const struct device *parent, const struct cfparent *cfp)
524 {
525 struct cfdriver *pcd;
526
527 /* We don't match root nodes here. */
528 if (cfp == NULL)
529 return (0);
530
531 pcd = parent->dv_cfdriver;
532 KASSERT(pcd != NULL);
533
534 /*
535 * First, ensure this parent has the correct interface
536 * attribute.
537 */
538 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr))
539 return (0);
540
541 /*
542 * If no specific parent device instance was specified (i.e.
543 * we're attaching to the attribute only), we're done!
544 */
545 if (cfp->cfp_parent == NULL)
546 return (1);
547
548 /*
549 * Check the parent device's name.
550 */
551 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
552 return (0); /* not the same parent */
553
554 /*
555 * Make sure the unit number matches.
556 */
557 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */
558 cfp->cfp_unit == parent->dv_unit)
559 return (1);
560
561 /* Unit numbers don't match. */
562 return (0);
563 }
564
565 /*
566 * Helper for config_cfdata_attach(): check all devices whether it could be
567 * parent any attachment in the config data table passed, and rescan.
568 */
569 static void
570 rescan_with_cfdata(const struct cfdata *cf)
571 {
572 struct device *d;
573 const struct cfdata *cf1;
574
575 /*
576 * "alldevs" is likely longer than an LKM's cfdata, so make it
577 * the outer loop.
578 */
579 TAILQ_FOREACH(d, &alldevs, dv_list) {
580
581 if (!(d->dv_cfattach->ca_rescan))
582 continue;
583
584 for (cf1 = cf; cf1->cf_name; cf1++) {
585
586 if (!cfparent_match(d, cf1->cf_pspec))
587 continue;
588
589 (*d->dv_cfattach->ca_rescan)(d,
590 cf1->cf_pspec->cfp_iattr, cf1->cf_loc);
591 }
592 }
593 }
594
595 /*
596 * Attach a supplemental config data table and rescan potential
597 * parent devices if required.
598 */
599 int
600 config_cfdata_attach(struct cfdata *cf, int scannow)
601 {
602 struct cftable *ct;
603
604 ct = malloc(sizeof(struct cftable), M_DEVBUF, M_WAITOK);
605 ct->ct_cfdata = cf;
606 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
607
608 if (scannow)
609 rescan_with_cfdata(cf);
610
611 return (0);
612 }
613
614 /*
615 * Helper for config_cfdata_detach: check whether a device is
616 * found through any attachment in the config data table.
617 */
618 static int
619 dev_in_cfdata(const struct device *d, const struct cfdata *cf)
620 {
621 const struct cfdata *cf1;
622
623 for (cf1 = cf; cf1->cf_name; cf1++)
624 if (d->dv_cfdata == cf1)
625 return (1);
626
627 return (0);
628 }
629
630 /*
631 * Detach a supplemental config data table. Detach all devices found
632 * through that table (and thus keeping references to it) before.
633 */
634 int
635 config_cfdata_detach(struct cfdata *cf)
636 {
637 struct device *d;
638 int error;
639 struct cftable *ct;
640
641 again:
642 TAILQ_FOREACH(d, &alldevs, dv_list) {
643 if (dev_in_cfdata(d, cf)) {
644 error = config_detach(d, 0);
645 if (error) {
646 aprint_error("%s: unable to detach instance\n",
647 d->dv_xname);
648 return (error);
649 }
650 goto again;
651 }
652 }
653
654 TAILQ_FOREACH(ct, &allcftables, ct_list) {
655 if (ct->ct_cfdata == cf) {
656 TAILQ_REMOVE(&allcftables, ct, ct_list);
657 free(ct, M_DEVBUF);
658 return (0);
659 }
660 }
661
662 /* not found -- shouldn't happen */
663 return (EINVAL);
664 }
665
666 /*
667 * Invoke the "match" routine for a cfdata entry on behalf of
668 * an external caller, usually a "submatch" routine.
669 */
670 int
671 config_match(struct device *parent, struct cfdata *cf, void *aux)
672 {
673 struct cfattach *ca;
674
675 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
676 if (ca == NULL) {
677 /* No attachment for this entry, oh well. */
678 return (0);
679 }
680
681 return ((*ca->ca_match)(parent, cf, aux));
682 }
683
684 /*
685 * Iterate over all potential children of some device, calling the given
686 * function (default being the child's match function) for each one.
687 * Nonzero returns are matches; the highest value returned is considered
688 * the best match. Return the `found child' if we got a match, or NULL
689 * otherwise. The `aux' pointer is simply passed on through.
690 *
691 * Note that this function is designed so that it can be used to apply
692 * an arbitrary function to all potential children (its return value
693 * can be ignored).
694 */
695 struct cfdata *
696 config_search_loc(cfsubmatch_t fn, struct device *parent,
697 const char *ifattr, const int *locs, void *aux)
698 {
699 struct cftable *ct;
700 struct cfdata *cf;
701 struct matchinfo m;
702
703 KASSERT(config_initialized);
704 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr));
705
706 m.fn = fn;
707 m.parent = parent;
708 m.locs = locs;
709 m.aux = aux;
710 m.match = NULL;
711 m.pri = 0;
712
713 TAILQ_FOREACH(ct, &allcftables, ct_list) {
714 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
715
716 /* We don't match root nodes here. */
717 if (!cf->cf_pspec)
718 continue;
719
720 /*
721 * Skip cf if no longer eligible, otherwise scan
722 * through parents for one matching `parent', and
723 * try match function.
724 */
725 if (cf->cf_fstate == FSTATE_FOUND)
726 continue;
727 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
728 cf->cf_fstate == FSTATE_DSTAR)
729 continue;
730
731 /*
732 * If an interface attribute was specified,
733 * consider only children which attach to
734 * that attribute.
735 */
736 if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr))
737 continue;
738
739 if (cfparent_match(parent, cf->cf_pspec))
740 mapply(&m, cf);
741 }
742 }
743 return (m.match);
744 }
745
746 /*
747 * Find the given root device.
748 * This is much like config_search, but there is no parent.
749 * Don't bother with multiple cfdata tables; the root node
750 * must always be in the initial table.
751 */
752 struct cfdata *
753 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux)
754 {
755 struct cfdata *cf;
756 const short *p;
757 struct matchinfo m;
758
759 m.fn = fn;
760 m.parent = ROOT;
761 m.aux = aux;
762 m.match = NULL;
763 m.pri = 0;
764 /*
765 * Look at root entries for matching name. We do not bother
766 * with found-state here since only one root should ever be
767 * searched (and it must be done first).
768 */
769 for (p = cfroots; *p >= 0; p++) {
770 cf = &cfdata[*p];
771 if (strcmp(cf->cf_name, rootname) == 0)
772 mapply(&m, cf);
773 }
774 return (m.match);
775 }
776
777 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
778
779 /*
780 * The given `aux' argument describes a device that has been found
781 * on the given parent, but not necessarily configured. Locate the
782 * configuration data for that device (using the submatch function
783 * provided, or using candidates' cd_match configuration driver
784 * functions) and attach it, and return true. If the device was
785 * not configured, call the given `print' function and return 0.
786 */
787 struct device *
788 config_found_sm_loc(struct device *parent,
789 const char *ifattr, const int *locs, void *aux,
790 cfprint_t print, cfsubmatch_t submatch)
791 {
792 struct cfdata *cf;
793
794 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux)))
795 return(config_attach_loc(parent, cf, locs, aux, print));
796 if (print) {
797 if (config_do_twiddle)
798 twiddle();
799 aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]);
800 }
801 return (NULL);
802 }
803
804 /*
805 * As above, but for root devices.
806 */
807 struct device *
808 config_rootfound(const char *rootname, void *aux)
809 {
810 struct cfdata *cf;
811
812 if ((cf = config_rootsearch((cfsubmatch_t)NULL, rootname, aux)) != NULL)
813 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
814 aprint_error("root device %s not configured\n", rootname);
815 return (NULL);
816 }
817
818 /* just like sprintf(buf, "%d") except that it works from the end */
819 static char *
820 number(char *ep, int n)
821 {
822
823 *--ep = 0;
824 while (n >= 10) {
825 *--ep = (n % 10) + '0';
826 n /= 10;
827 }
828 *--ep = n + '0';
829 return (ep);
830 }
831
832 /*
833 * Expand the size of the cd_devs array if necessary.
834 */
835 void
836 config_makeroom(int n, struct cfdriver *cd)
837 {
838 int old, new;
839 void **nsp;
840
841 if (n < cd->cd_ndevs)
842 return;
843
844 /*
845 * Need to expand the array.
846 */
847 old = cd->cd_ndevs;
848 if (old == 0)
849 new = MINALLOCSIZE / sizeof(void *);
850 else
851 new = old * 2;
852 while (new <= n)
853 new *= 2;
854 cd->cd_ndevs = new;
855 nsp = malloc(new * sizeof(void *), M_DEVBUF,
856 cold ? M_NOWAIT : M_WAITOK);
857 if (nsp == NULL)
858 panic("config_attach: %sing dev array",
859 old != 0 ? "expand" : "creat");
860 memset(nsp + old, 0, (new - old) * sizeof(void *));
861 if (old != 0) {
862 memcpy(nsp, cd->cd_devs, old * sizeof(void *));
863 free(cd->cd_devs, M_DEVBUF);
864 }
865 cd->cd_devs = nsp;
866 }
867
868 /*
869 * Attach a found device. Allocates memory for device variables.
870 */
871 struct device *
872 config_attach_loc(struct device *parent, struct cfdata *cf,
873 const int *locs, void *aux, cfprint_t print)
874 {
875 struct device *dev;
876 struct cftable *ct;
877 struct cfdriver *cd;
878 struct cfattach *ca;
879 size_t lname, lunit;
880 const char *xunit;
881 int myunit;
882 char num[10];
883 const struct cfiattrdata *ia;
884
885 cd = config_cfdriver_lookup(cf->cf_name);
886 KASSERT(cd != NULL);
887
888 ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
889 KASSERT(ca != NULL);
890
891 if (ca->ca_devsize < sizeof(struct device))
892 panic("config_attach");
893
894 #ifndef __BROKEN_CONFIG_UNIT_USAGE
895 if (cf->cf_fstate == FSTATE_STAR) {
896 for (myunit = cf->cf_unit; myunit < cd->cd_ndevs; myunit++)
897 if (cd->cd_devs[myunit] == NULL)
898 break;
899 /*
900 * myunit is now the unit of the first NULL device pointer,
901 * or max(cd->cd_ndevs,cf->cf_unit).
902 */
903 } else {
904 myunit = cf->cf_unit;
905 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
906 cf->cf_fstate = FSTATE_FOUND;
907 }
908 #else
909 myunit = cf->cf_unit;
910 if (cf->cf_fstate == FSTATE_STAR)
911 cf->cf_unit++;
912 else {
913 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
914 cf->cf_fstate = FSTATE_FOUND;
915 }
916 #endif /* ! __BROKEN_CONFIG_UNIT_USAGE */
917
918 /* compute length of name and decimal expansion of unit number */
919 lname = strlen(cd->cd_name);
920 xunit = number(&num[sizeof(num)], myunit);
921 lunit = &num[sizeof(num)] - xunit;
922 if (lname + lunit > sizeof(dev->dv_xname))
923 panic("config_attach: device name too long");
924
925 /* get memory for all device vars */
926 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
927 cold ? M_NOWAIT : M_WAITOK);
928 if (!dev)
929 panic("config_attach: memory allocation for device softc failed");
930 memset(dev, 0, ca->ca_devsize);
931 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
932 dev->dv_class = cd->cd_class;
933 dev->dv_cfdata = cf;
934 dev->dv_cfdriver = cd;
935 dev->dv_cfattach = ca;
936 dev->dv_unit = myunit;
937 memcpy(dev->dv_xname, cd->cd_name, lname);
938 memcpy(dev->dv_xname + lname, xunit, lunit);
939 dev->dv_parent = parent;
940 dev->dv_flags = DVF_ACTIVE; /* always initially active */
941 if (locs) {
942 KASSERT(parent); /* no locators at root */
943 ia = cfiattr_lookup(cf->cf_pspec->cfp_iattr,
944 parent->dv_cfdriver);
945 dev->dv_locators = malloc(ia->ci_loclen * sizeof(int),
946 M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
947 memcpy(dev->dv_locators, locs, ia->ci_loclen * sizeof(int));
948 }
949
950 if (config_do_twiddle)
951 twiddle();
952 else
953 aprint_naive("Found ");
954 /*
955 * We want the next two printfs for normal, verbose, and quiet,
956 * but not silent (in which case, we're twiddling, instead).
957 */
958 if (parent == ROOT) {
959 aprint_naive("%s (root)", dev->dv_xname);
960 aprint_normal("%s (root)", dev->dv_xname);
961 } else {
962 aprint_naive("%s at %s", dev->dv_xname, parent->dv_xname);
963 aprint_normal("%s at %s", dev->dv_xname, parent->dv_xname);
964 if (print)
965 (void) (*print)(aux, NULL);
966 }
967
968 /* put this device in the devices array */
969 config_makeroom(dev->dv_unit, cd);
970 if (cd->cd_devs[dev->dv_unit])
971 panic("config_attach: duplicate %s", dev->dv_xname);
972 cd->cd_devs[dev->dv_unit] = dev;
973
974 /*
975 * Before attaching, clobber any unfound devices that are
976 * otherwise identical.
977 */
978 TAILQ_FOREACH(ct, &allcftables, ct_list) {
979 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
980 if (STREQ(cf->cf_name, cd->cd_name) &&
981 cf->cf_unit == dev->dv_unit) {
982 if (cf->cf_fstate == FSTATE_NOTFOUND)
983 cf->cf_fstate = FSTATE_FOUND;
984 #ifdef __BROKEN_CONFIG_UNIT_USAGE
985 /*
986 * Bump the unit number on all starred cfdata
987 * entries for this device.
988 */
989 if (cf->cf_fstate == FSTATE_STAR)
990 cf->cf_unit++;
991 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
992 }
993 }
994 }
995 #ifdef __HAVE_DEVICE_REGISTER
996 device_register(dev, aux);
997 #endif
998 (*ca->ca_attach)(parent, dev, aux);
999 config_process_deferred(&deferred_config_queue, dev);
1000 return (dev);
1001 }
1002
1003 /*
1004 * As above, but for pseudo-devices. Pseudo-devices attached in this
1005 * way are silently inserted into the device tree, and their children
1006 * attached.
1007 *
1008 * Note that because pseudo-devices are attached silently, any information
1009 * the attach routine wishes to print should be prefixed with the device
1010 * name by the attach routine.
1011 */
1012 struct device *
1013 config_attach_pseudo(struct cfdata *cf)
1014 {
1015 struct device *dev;
1016 struct cfdriver *cd;
1017 struct cfattach *ca;
1018 size_t lname, lunit;
1019 const char *xunit;
1020 int myunit;
1021 char num[10];
1022
1023 cd = config_cfdriver_lookup(cf->cf_name);
1024 if (cd == NULL)
1025 return (NULL);
1026
1027 ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
1028 if (ca == NULL)
1029 return (NULL);
1030
1031 if (ca->ca_devsize < sizeof(struct device))
1032 panic("config_attach_pseudo");
1033
1034 /*
1035 * We just ignore cf_fstate, instead doing everything with
1036 * cf_unit.
1037 *
1038 * XXX Should we change this and use FSTATE_NOTFOUND and
1039 * XXX FSTATE_STAR?
1040 */
1041
1042 if (cf->cf_unit == DVUNIT_ANY) {
1043 for (myunit = 0; myunit < cd->cd_ndevs; myunit++)
1044 if (cd->cd_devs[myunit] == NULL)
1045 break;
1046 /*
1047 * myunit is now the unit of the first NULL device pointer.
1048 */
1049 } else {
1050 myunit = cf->cf_unit;
1051 if (myunit < cd->cd_ndevs && cd->cd_devs[myunit] != NULL)
1052 return (NULL);
1053 }
1054
1055 /* compute length of name and decimal expansion of unit number */
1056 lname = strlen(cd->cd_name);
1057 xunit = number(&num[sizeof(num)], myunit);
1058 lunit = &num[sizeof(num)] - xunit;
1059 if (lname + lunit > sizeof(dev->dv_xname))
1060 panic("config_attach_pseudo: device name too long");
1061
1062 /* get memory for all device vars */
1063 dev = (struct device *)malloc(ca->ca_devsize, M_DEVBUF,
1064 cold ? M_NOWAIT : M_WAITOK);
1065 if (!dev)
1066 panic("config_attach_pseudo: memory allocation for device "
1067 "softc failed");
1068 memset(dev, 0, ca->ca_devsize);
1069 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
1070 dev->dv_class = cd->cd_class;
1071 dev->dv_cfdata = cf;
1072 dev->dv_cfdriver = cd;
1073 dev->dv_cfattach = ca;
1074 dev->dv_unit = myunit;
1075 memcpy(dev->dv_xname, cd->cd_name, lname);
1076 memcpy(dev->dv_xname + lname, xunit, lunit);
1077 dev->dv_parent = ROOT;
1078 dev->dv_flags = DVF_ACTIVE; /* always initially active */
1079
1080 /* put this device in the devices array */
1081 config_makeroom(dev->dv_unit, cd);
1082 if (cd->cd_devs[dev->dv_unit])
1083 panic("config_attach_pseudo: duplicate %s", dev->dv_xname);
1084 cd->cd_devs[dev->dv_unit] = dev;
1085
1086 #if 0 /* XXXJRT not yet */
1087 #ifdef __HAVE_DEVICE_REGISTER
1088 device_register(dev, NULL); /* like a root node */
1089 #endif
1090 #endif
1091 (*ca->ca_attach)(ROOT, dev, NULL);
1092 config_process_deferred(&deferred_config_queue, dev);
1093 return (dev);
1094 }
1095
1096 /*
1097 * Detach a device. Optionally forced (e.g. because of hardware
1098 * removal) and quiet. Returns zero if successful, non-zero
1099 * (an error code) otherwise.
1100 *
1101 * Note that this code wants to be run from a process context, so
1102 * that the detach can sleep to allow processes which have a device
1103 * open to run and unwind their stacks.
1104 */
1105 int
1106 config_detach(struct device *dev, int flags)
1107 {
1108 struct cftable *ct;
1109 struct cfdata *cf;
1110 const struct cfattach *ca;
1111 struct cfdriver *cd;
1112 #ifdef DIAGNOSTIC
1113 struct device *d;
1114 #endif
1115 int rv = 0, i;
1116
1117 #ifdef DIAGNOSTIC
1118 if (dev->dv_cfdata != NULL &&
1119 dev->dv_cfdata->cf_fstate != FSTATE_FOUND &&
1120 dev->dv_cfdata->cf_fstate != FSTATE_STAR)
1121 panic("config_detach: bad device fstate");
1122 #endif
1123 cd = dev->dv_cfdriver;
1124 KASSERT(cd != NULL);
1125
1126 ca = dev->dv_cfattach;
1127 KASSERT(ca != NULL);
1128
1129 /*
1130 * Ensure the device is deactivated. If the device doesn't
1131 * have an activation entry point, we allow DVF_ACTIVE to
1132 * remain set. Otherwise, if DVF_ACTIVE is still set, the
1133 * device is busy, and the detach fails.
1134 */
1135 if (ca->ca_activate != NULL)
1136 rv = config_deactivate(dev);
1137
1138 /*
1139 * Try to detach the device. If that's not possible, then
1140 * we either panic() (for the forced but failed case), or
1141 * return an error.
1142 */
1143 if (rv == 0) {
1144 if (ca->ca_detach != NULL)
1145 rv = (*ca->ca_detach)(dev, flags);
1146 else
1147 rv = EOPNOTSUPP;
1148 }
1149 if (rv != 0) {
1150 if ((flags & DETACH_FORCE) == 0)
1151 return (rv);
1152 else
1153 panic("config_detach: forced detach of %s failed (%d)",
1154 dev->dv_xname, rv);
1155 }
1156
1157 /*
1158 * The device has now been successfully detached.
1159 */
1160
1161 #ifdef DIAGNOSTIC
1162 /*
1163 * Sanity: If you're successfully detached, you should have no
1164 * children. (Note that because children must be attached
1165 * after parents, we only need to search the latter part of
1166 * the list.)
1167 */
1168 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
1169 d = TAILQ_NEXT(d, dv_list)) {
1170 if (d->dv_parent == dev) {
1171 printf("config_detach: detached device %s"
1172 " has children %s\n", dev->dv_xname, d->dv_xname);
1173 panic("config_detach");
1174 }
1175 }
1176 #endif
1177
1178 /* notify the parent that the child is gone */
1179 if (dev->dv_parent) {
1180 struct device *p = dev->dv_parent;
1181 if (p->dv_cfattach->ca_childdetached)
1182 (*p->dv_cfattach->ca_childdetached)(p, dev);
1183 }
1184
1185 /*
1186 * Mark cfdata to show that the unit can be reused, if possible.
1187 */
1188 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1189 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1190 if (STREQ(cf->cf_name, cd->cd_name)) {
1191 if (cf->cf_fstate == FSTATE_FOUND &&
1192 cf->cf_unit == dev->dv_unit)
1193 cf->cf_fstate = FSTATE_NOTFOUND;
1194 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1195 /*
1196 * Note that we can only re-use a starred
1197 * unit number if the unit being detached
1198 * had the last assigned unit number.
1199 */
1200 if (cf->cf_fstate == FSTATE_STAR &&
1201 cf->cf_unit == dev->dv_unit + 1)
1202 cf->cf_unit--;
1203 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
1204 }
1205 }
1206 }
1207
1208 /*
1209 * Unlink from device list.
1210 */
1211 TAILQ_REMOVE(&alldevs, dev, dv_list);
1212
1213 /*
1214 * Remove from cfdriver's array, tell the world (unless it was
1215 * a pseudo-device), and free softc.
1216 */
1217 cd->cd_devs[dev->dv_unit] = NULL;
1218 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
1219 aprint_normal("%s detached\n", dev->dv_xname);
1220 if (dev->dv_locators)
1221 free(dev->dv_locators, M_DEVBUF);
1222 free(dev, M_DEVBUF);
1223
1224 /*
1225 * If the device now has no units in use, deallocate its softc array.
1226 */
1227 for (i = 0; i < cd->cd_ndevs; i++)
1228 if (cd->cd_devs[i] != NULL)
1229 break;
1230 if (i == cd->cd_ndevs) { /* nothing found; deallocate */
1231 free(cd->cd_devs, M_DEVBUF);
1232 cd->cd_devs = NULL;
1233 cd->cd_ndevs = 0;
1234 }
1235
1236 /*
1237 * Return success.
1238 */
1239 return (0);
1240 }
1241
1242 int
1243 config_activate(struct device *dev)
1244 {
1245 const struct cfattach *ca = dev->dv_cfattach;
1246 int rv = 0, oflags = dev->dv_flags;
1247
1248 if (ca->ca_activate == NULL)
1249 return (EOPNOTSUPP);
1250
1251 if ((dev->dv_flags & DVF_ACTIVE) == 0) {
1252 dev->dv_flags |= DVF_ACTIVE;
1253 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
1254 if (rv)
1255 dev->dv_flags = oflags;
1256 }
1257 return (rv);
1258 }
1259
1260 int
1261 config_deactivate(struct device *dev)
1262 {
1263 const struct cfattach *ca = dev->dv_cfattach;
1264 int rv = 0, oflags = dev->dv_flags;
1265
1266 if (ca->ca_activate == NULL)
1267 return (EOPNOTSUPP);
1268
1269 if (dev->dv_flags & DVF_ACTIVE) {
1270 dev->dv_flags &= ~DVF_ACTIVE;
1271 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
1272 if (rv)
1273 dev->dv_flags = oflags;
1274 }
1275 return (rv);
1276 }
1277
1278 /*
1279 * Defer the configuration of the specified device until all
1280 * of its parent's devices have been attached.
1281 */
1282 void
1283 config_defer(struct device *dev, void (*func)(struct device *))
1284 {
1285 struct deferred_config *dc;
1286
1287 if (dev->dv_parent == NULL)
1288 panic("config_defer: can't defer config of a root device");
1289
1290 #ifdef DIAGNOSTIC
1291 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
1292 dc = TAILQ_NEXT(dc, dc_queue)) {
1293 if (dc->dc_dev == dev)
1294 panic("config_defer: deferred twice");
1295 }
1296 #endif
1297
1298 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1299 if (dc == NULL)
1300 panic("config_defer: unable to allocate callback");
1301
1302 dc->dc_dev = dev;
1303 dc->dc_func = func;
1304 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
1305 config_pending_incr();
1306 }
1307
1308 /*
1309 * Defer some autoconfiguration for a device until after interrupts
1310 * are enabled.
1311 */
1312 void
1313 config_interrupts(struct device *dev, void (*func)(struct device *))
1314 {
1315 struct deferred_config *dc;
1316
1317 /*
1318 * If interrupts are enabled, callback now.
1319 */
1320 if (cold == 0) {
1321 (*func)(dev);
1322 return;
1323 }
1324
1325 #ifdef DIAGNOSTIC
1326 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
1327 dc = TAILQ_NEXT(dc, dc_queue)) {
1328 if (dc->dc_dev == dev)
1329 panic("config_interrupts: deferred twice");
1330 }
1331 #endif
1332
1333 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1334 if (dc == NULL)
1335 panic("config_interrupts: unable to allocate callback");
1336
1337 dc->dc_dev = dev;
1338 dc->dc_func = func;
1339 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
1340 config_pending_incr();
1341 }
1342
1343 /*
1344 * Process a deferred configuration queue.
1345 */
1346 static void
1347 config_process_deferred(struct deferred_config_head *queue,
1348 struct device *parent)
1349 {
1350 struct deferred_config *dc, *ndc;
1351
1352 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
1353 ndc = TAILQ_NEXT(dc, dc_queue);
1354 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
1355 TAILQ_REMOVE(queue, dc, dc_queue);
1356 (*dc->dc_func)(dc->dc_dev);
1357 free(dc, M_DEVBUF);
1358 config_pending_decr();
1359 }
1360 }
1361 }
1362
1363 /*
1364 * Manipulate the config_pending semaphore.
1365 */
1366 void
1367 config_pending_incr(void)
1368 {
1369
1370 config_pending++;
1371 }
1372
1373 void
1374 config_pending_decr(void)
1375 {
1376
1377 #ifdef DIAGNOSTIC
1378 if (config_pending == 0)
1379 panic("config_pending_decr: config_pending == 0");
1380 #endif
1381 config_pending--;
1382 if (config_pending == 0)
1383 wakeup(&config_pending);
1384 }
1385
1386 /*
1387 * Register a "finalization" routine. Finalization routines are
1388 * called iteratively once all real devices have been found during
1389 * autoconfiguration, for as long as any one finalizer has done
1390 * any work.
1391 */
1392 int
1393 config_finalize_register(struct device *dev, int (*fn)(struct device *))
1394 {
1395 struct finalize_hook *f;
1396
1397 /*
1398 * If finalization has already been done, invoke the
1399 * callback function now.
1400 */
1401 if (config_finalize_done) {
1402 while ((*fn)(dev) != 0)
1403 /* loop */ ;
1404 }
1405
1406 /* Ensure this isn't already on the list. */
1407 TAILQ_FOREACH(f, &config_finalize_list, f_list) {
1408 if (f->f_func == fn && f->f_dev == dev)
1409 return (EEXIST);
1410 }
1411
1412 f = malloc(sizeof(*f), M_TEMP, M_WAITOK);
1413 f->f_func = fn;
1414 f->f_dev = dev;
1415 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
1416
1417 return (0);
1418 }
1419
1420 void
1421 config_finalize(void)
1422 {
1423 struct finalize_hook *f;
1424 int rv;
1425
1426 /* Run the hooks until none of them does any work. */
1427 do {
1428 rv = 0;
1429 TAILQ_FOREACH(f, &config_finalize_list, f_list)
1430 rv |= (*f->f_func)(f->f_dev);
1431 } while (rv != 0);
1432
1433 config_finalize_done = 1;
1434
1435 /* Now free all the hooks. */
1436 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
1437 TAILQ_REMOVE(&config_finalize_list, f, f_list);
1438 free(f, M_TEMP);
1439 }
1440 }
1441
1442