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