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