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