subr_autoconf.c revision 1.119.4.5 1 /* $NetBSD: subr_autoconf.c,v 1.119.4.5 2007/11/07 01:14:15 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.5 2007/11/07 01:14:15 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 pnp_init();
368
369 #ifdef USERCONF
370 if (boothowto & RB_USERCONF)
371 user_config();
372 #endif
373
374 if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
375 config_do_twiddle = 1;
376 printf_nolog("Detecting hardware...");
377 }
378
379 /*
380 * Do the machine-dependent portion of autoconfiguration. This
381 * sets the configuration machinery here in motion by "finding"
382 * the root bus. When this function returns, we expect interrupts
383 * to be enabled.
384 */
385 cpu_configure();
386
387 /* Initialize callouts, part 2. */
388 callout_startup2();
389
390 /*
391 * Now that we've found all the hardware, start the real time
392 * and statistics clocks.
393 */
394 initclocks();
395
396 cold = 0; /* clocks are running, we're warm now! */
397
398 /*
399 * Now callback to finish configuration for devices which want
400 * to do this once interrupts are enabled.
401 */
402 config_process_deferred(&interrupt_config_queue, NULL);
403
404 errcnt = aprint_get_error_count();
405 if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
406 (boothowto & AB_VERBOSE) == 0) {
407 if (config_do_twiddle) {
408 config_do_twiddle = 0;
409 printf_nolog("done.\n");
410 }
411 if (errcnt != 0) {
412 printf("WARNING: %d error%s while detecting hardware; "
413 "check system log.\n", errcnt,
414 errcnt == 1 ? "" : "s");
415 }
416 }
417 }
418
419 /*
420 * Add a cfdriver to the system.
421 */
422 int
423 config_cfdriver_attach(struct cfdriver *cd)
424 {
425 struct cfdriver *lcd;
426
427 /* Make sure this driver isn't already in the system. */
428 LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
429 if (STREQ(lcd->cd_name, cd->cd_name))
430 return (EEXIST);
431 }
432
433 LIST_INIT(&cd->cd_attach);
434 LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
435
436 return (0);
437 }
438
439 /*
440 * Remove a cfdriver from the system.
441 */
442 int
443 config_cfdriver_detach(struct cfdriver *cd)
444 {
445 int i;
446
447 /* Make sure there are no active instances. */
448 for (i = 0; i < cd->cd_ndevs; i++) {
449 if (cd->cd_devs[i] != NULL)
450 return (EBUSY);
451 }
452
453 /* ...and no attachments loaded. */
454 if (LIST_EMPTY(&cd->cd_attach) == 0)
455 return (EBUSY);
456
457 LIST_REMOVE(cd, cd_list);
458
459 KASSERT(cd->cd_devs == NULL);
460
461 return (0);
462 }
463
464 /*
465 * Look up a cfdriver by name.
466 */
467 struct cfdriver *
468 config_cfdriver_lookup(const char *name)
469 {
470 struct cfdriver *cd;
471
472 LIST_FOREACH(cd, &allcfdrivers, cd_list) {
473 if (STREQ(cd->cd_name, name))
474 return (cd);
475 }
476
477 return (NULL);
478 }
479
480 /*
481 * Add a cfattach to the specified driver.
482 */
483 int
484 config_cfattach_attach(const char *driver, struct cfattach *ca)
485 {
486 struct cfattach *lca;
487 struct cfdriver *cd;
488
489 cd = config_cfdriver_lookup(driver);
490 if (cd == NULL)
491 return (ESRCH);
492
493 /* Make sure this attachment isn't already on this driver. */
494 LIST_FOREACH(lca, &cd->cd_attach, ca_list) {
495 if (STREQ(lca->ca_name, ca->ca_name))
496 return (EEXIST);
497 }
498
499 LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list);
500
501 return (0);
502 }
503
504 /*
505 * Remove a cfattach from the specified driver.
506 */
507 int
508 config_cfattach_detach(const char *driver, struct cfattach *ca)
509 {
510 struct cfdriver *cd;
511 device_t dev;
512 int i;
513
514 cd = config_cfdriver_lookup(driver);
515 if (cd == NULL)
516 return (ESRCH);
517
518 /* Make sure there are no active instances. */
519 for (i = 0; i < cd->cd_ndevs; i++) {
520 if ((dev = cd->cd_devs[i]) == NULL)
521 continue;
522 if (dev->dv_cfattach == ca)
523 return (EBUSY);
524 }
525
526 LIST_REMOVE(ca, ca_list);
527
528 return (0);
529 }
530
531 /*
532 * Look up a cfattach by name.
533 */
534 static struct cfattach *
535 config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname)
536 {
537 struct cfattach *ca;
538
539 LIST_FOREACH(ca, &cd->cd_attach, ca_list) {
540 if (STREQ(ca->ca_name, atname))
541 return (ca);
542 }
543
544 return (NULL);
545 }
546
547 /*
548 * Look up a cfattach by driver/attachment name.
549 */
550 struct cfattach *
551 config_cfattach_lookup(const char *name, const char *atname)
552 {
553 struct cfdriver *cd;
554
555 cd = config_cfdriver_lookup(name);
556 if (cd == NULL)
557 return (NULL);
558
559 return (config_cfattach_lookup_cd(cd, atname));
560 }
561
562 /*
563 * Apply the matching function and choose the best. This is used
564 * a few times and we want to keep the code small.
565 */
566 static void
567 mapply(struct matchinfo *m, cfdata_t cf)
568 {
569 int pri;
570
571 if (m->fn != NULL) {
572 pri = (*m->fn)(m->parent, cf, m->locs, m->aux);
573 } else {
574 pri = config_match(m->parent, cf, m->aux);
575 }
576 if (pri > m->pri) {
577 m->match = cf;
578 m->pri = pri;
579 }
580 }
581
582 int
583 config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
584 {
585 const struct cfiattrdata *ci;
586 const struct cflocdesc *cl;
587 int nlocs, i;
588
589 ci = cfiattr_lookup(cf->cf_pspec->cfp_iattr, parent->dv_cfdriver);
590 KASSERT(ci);
591 nlocs = ci->ci_loclen;
592 for (i = 0; i < nlocs; i++) {
593 cl = &ci->ci_locdesc[i];
594 /* !cld_defaultstr means no default value */
595 if ((!(cl->cld_defaultstr)
596 || (cf->cf_loc[i] != cl->cld_default))
597 && cf->cf_loc[i] != locs[i])
598 return (0);
599 }
600
601 return (config_match(parent, cf, aux));
602 }
603
604 /*
605 * Helper function: check whether the driver supports the interface attribute
606 * and return its descriptor structure.
607 */
608 static const struct cfiattrdata *
609 cfdriver_get_iattr(const struct cfdriver *cd, const char *ia)
610 {
611 const struct cfiattrdata * const *cpp;
612
613 if (cd->cd_attrs == NULL)
614 return (0);
615
616 for (cpp = cd->cd_attrs; *cpp; cpp++) {
617 if (STREQ((*cpp)->ci_name, ia)) {
618 /* Match. */
619 return (*cpp);
620 }
621 }
622 return (0);
623 }
624
625 /*
626 * Lookup an interface attribute description by name.
627 * If the driver is given, consider only its supported attributes.
628 */
629 const struct cfiattrdata *
630 cfiattr_lookup(const char *name, const struct cfdriver *cd)
631 {
632 const struct cfdriver *d;
633 const struct cfiattrdata *ia;
634
635 if (cd)
636 return (cfdriver_get_iattr(cd, name));
637
638 LIST_FOREACH(d, &allcfdrivers, cd_list) {
639 ia = cfdriver_get_iattr(d, name);
640 if (ia)
641 return (ia);
642 }
643 return (0);
644 }
645
646 /*
647 * Determine if `parent' is a potential parent for a device spec based
648 * on `cfp'.
649 */
650 static int
651 cfparent_match(const device_t parent, const struct cfparent *cfp)
652 {
653 struct cfdriver *pcd;
654
655 /* We don't match root nodes here. */
656 if (cfp == NULL)
657 return (0);
658
659 pcd = parent->dv_cfdriver;
660 KASSERT(pcd != NULL);
661
662 /*
663 * First, ensure this parent has the correct interface
664 * attribute.
665 */
666 if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr))
667 return (0);
668
669 /*
670 * If no specific parent device instance was specified (i.e.
671 * we're attaching to the attribute only), we're done!
672 */
673 if (cfp->cfp_parent == NULL)
674 return (1);
675
676 /*
677 * Check the parent device's name.
678 */
679 if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
680 return (0); /* not the same parent */
681
682 /*
683 * Make sure the unit number matches.
684 */
685 if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */
686 cfp->cfp_unit == parent->dv_unit)
687 return (1);
688
689 /* Unit numbers don't match. */
690 return (0);
691 }
692
693 /*
694 * Helper for config_cfdata_attach(): check all devices whether it could be
695 * parent any attachment in the config data table passed, and rescan.
696 */
697 static void
698 rescan_with_cfdata(const struct cfdata *cf)
699 {
700 device_t d;
701 const struct cfdata *cf1;
702
703 /*
704 * "alldevs" is likely longer than an LKM's cfdata, so make it
705 * the outer loop.
706 */
707 TAILQ_FOREACH(d, &alldevs, dv_list) {
708
709 if (!(d->dv_cfattach->ca_rescan))
710 continue;
711
712 for (cf1 = cf; cf1->cf_name; cf1++) {
713
714 if (!cfparent_match(d, cf1->cf_pspec))
715 continue;
716
717 (*d->dv_cfattach->ca_rescan)(d,
718 cf1->cf_pspec->cfp_iattr, cf1->cf_loc);
719 }
720 }
721 }
722
723 /*
724 * Attach a supplemental config data table and rescan potential
725 * parent devices if required.
726 */
727 int
728 config_cfdata_attach(cfdata_t cf, int scannow)
729 {
730 struct cftable *ct;
731
732 ct = malloc(sizeof(struct cftable), M_DEVBUF, M_WAITOK);
733 ct->ct_cfdata = cf;
734 TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
735
736 if (scannow)
737 rescan_with_cfdata(cf);
738
739 return (0);
740 }
741
742 /*
743 * Helper for config_cfdata_detach: check whether a device is
744 * found through any attachment in the config data table.
745 */
746 static int
747 dev_in_cfdata(const struct device *d, const struct cfdata *cf)
748 {
749 const struct cfdata *cf1;
750
751 for (cf1 = cf; cf1->cf_name; cf1++)
752 if (d->dv_cfdata == cf1)
753 return (1);
754
755 return (0);
756 }
757
758 /*
759 * Detach a supplemental config data table. Detach all devices found
760 * through that table (and thus keeping references to it) before.
761 */
762 int
763 config_cfdata_detach(cfdata_t cf)
764 {
765 device_t d;
766 int error;
767 struct cftable *ct;
768
769 again:
770 TAILQ_FOREACH(d, &alldevs, dv_list) {
771 if (dev_in_cfdata(d, cf)) {
772 error = config_detach(d, 0);
773 if (error) {
774 aprint_error("%s: unable to detach instance\n",
775 d->dv_xname);
776 return (error);
777 }
778 goto again;
779 }
780 }
781
782 TAILQ_FOREACH(ct, &allcftables, ct_list) {
783 if (ct->ct_cfdata == cf) {
784 TAILQ_REMOVE(&allcftables, ct, ct_list);
785 free(ct, M_DEVBUF);
786 return (0);
787 }
788 }
789
790 /* not found -- shouldn't happen */
791 return (EINVAL);
792 }
793
794 /*
795 * Invoke the "match" routine for a cfdata entry on behalf of
796 * an external caller, usually a "submatch" routine.
797 */
798 int
799 config_match(device_t parent, cfdata_t cf, void *aux)
800 {
801 struct cfattach *ca;
802
803 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
804 if (ca == NULL) {
805 /* No attachment for this entry, oh well. */
806 return (0);
807 }
808
809 return ((*ca->ca_match)(parent, cf, aux));
810 }
811
812 /*
813 * Iterate over all potential children of some device, calling the given
814 * function (default being the child's match function) for each one.
815 * Nonzero returns are matches; the highest value returned is considered
816 * the best match. Return the `found child' if we got a match, or NULL
817 * otherwise. The `aux' pointer is simply passed on through.
818 *
819 * Note that this function is designed so that it can be used to apply
820 * an arbitrary function to all potential children (its return value
821 * can be ignored).
822 */
823 cfdata_t
824 config_search_loc(cfsubmatch_t fn, device_t parent,
825 const char *ifattr, const int *locs, void *aux)
826 {
827 struct cftable *ct;
828 cfdata_t cf;
829 struct matchinfo m;
830
831 KASSERT(config_initialized);
832 KASSERT(!ifattr || cfdriver_get_iattr(parent->dv_cfdriver, ifattr));
833
834 m.fn = fn;
835 m.parent = parent;
836 m.locs = locs;
837 m.aux = aux;
838 m.match = NULL;
839 m.pri = 0;
840
841 TAILQ_FOREACH(ct, &allcftables, ct_list) {
842 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
843
844 /* We don't match root nodes here. */
845 if (!cf->cf_pspec)
846 continue;
847
848 /*
849 * Skip cf if no longer eligible, otherwise scan
850 * through parents for one matching `parent', and
851 * try match function.
852 */
853 if (cf->cf_fstate == FSTATE_FOUND)
854 continue;
855 if (cf->cf_fstate == FSTATE_DNOTFOUND ||
856 cf->cf_fstate == FSTATE_DSTAR)
857 continue;
858
859 /*
860 * If an interface attribute was specified,
861 * consider only children which attach to
862 * that attribute.
863 */
864 if (ifattr && !STREQ(ifattr, cf->cf_pspec->cfp_iattr))
865 continue;
866
867 if (cfparent_match(parent, cf->cf_pspec))
868 mapply(&m, cf);
869 }
870 }
871 return (m.match);
872 }
873
874 cfdata_t
875 config_search_ia(cfsubmatch_t fn, device_t parent, const char *ifattr,
876 void *aux)
877 {
878
879 return (config_search_loc(fn, parent, ifattr, NULL, aux));
880 }
881
882 /*
883 * Find the given root device.
884 * This is much like config_search, but there is no parent.
885 * Don't bother with multiple cfdata tables; the root node
886 * must always be in the initial table.
887 */
888 cfdata_t
889 config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux)
890 {
891 cfdata_t cf;
892 const short *p;
893 struct matchinfo m;
894
895 m.fn = fn;
896 m.parent = ROOT;
897 m.aux = aux;
898 m.match = NULL;
899 m.pri = 0;
900 m.locs = 0;
901 /*
902 * Look at root entries for matching name. We do not bother
903 * with found-state here since only one root should ever be
904 * searched (and it must be done first).
905 */
906 for (p = cfroots; *p >= 0; p++) {
907 cf = &cfdata[*p];
908 if (strcmp(cf->cf_name, rootname) == 0)
909 mapply(&m, cf);
910 }
911 return (m.match);
912 }
913
914 static const char * const msgs[3] = { "", " not configured\n", " unsupported\n" };
915
916 /*
917 * The given `aux' argument describes a device that has been found
918 * on the given parent, but not necessarily configured. Locate the
919 * configuration data for that device (using the submatch function
920 * provided, or using candidates' cd_match configuration driver
921 * functions) and attach it, and return true. If the device was
922 * not configured, call the given `print' function and return 0.
923 */
924 device_t
925 config_found_sm_loc(device_t parent,
926 const char *ifattr, const int *locs, void *aux,
927 cfprint_t print, cfsubmatch_t submatch)
928 {
929 cfdata_t cf;
930
931 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
932 if (splash_progress_state)
933 splash_progress_update(splash_progress_state);
934 #endif
935
936 if ((cf = config_search_loc(submatch, parent, ifattr, locs, aux)))
937 return(config_attach_loc(parent, cf, locs, aux, print));
938 if (print) {
939 if (config_do_twiddle)
940 twiddle();
941 aprint_normal("%s", msgs[(*print)(aux, parent->dv_xname)]);
942 }
943
944 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
945 if (splash_progress_state)
946 splash_progress_update(splash_progress_state);
947 #endif
948
949 return (NULL);
950 }
951
952 device_t
953 config_found_ia(device_t parent, const char *ifattr, void *aux,
954 cfprint_t print)
955 {
956
957 return (config_found_sm_loc(parent, ifattr, NULL, aux, print, NULL));
958 }
959
960 device_t
961 config_found(device_t parent, void *aux, cfprint_t print)
962 {
963
964 return (config_found_sm_loc(parent, NULL, NULL, aux, print, NULL));
965 }
966
967 /*
968 * As above, but for root devices.
969 */
970 device_t
971 config_rootfound(const char *rootname, void *aux)
972 {
973 cfdata_t cf;
974
975 if ((cf = config_rootsearch((cfsubmatch_t)NULL, rootname, aux)) != NULL)
976 return (config_attach(ROOT, cf, aux, (cfprint_t)NULL));
977 aprint_error("root device %s not configured\n", rootname);
978 return (NULL);
979 }
980
981 /* just like sprintf(buf, "%d") except that it works from the end */
982 static char *
983 number(char *ep, int n)
984 {
985
986 *--ep = 0;
987 while (n >= 10) {
988 *--ep = (n % 10) + '0';
989 n /= 10;
990 }
991 *--ep = n + '0';
992 return (ep);
993 }
994
995 /*
996 * Expand the size of the cd_devs array if necessary.
997 */
998 static void
999 config_makeroom(int n, struct cfdriver *cd)
1000 {
1001 int old, new;
1002 void **nsp;
1003
1004 if (n < cd->cd_ndevs)
1005 return;
1006
1007 /*
1008 * Need to expand the array.
1009 */
1010 old = cd->cd_ndevs;
1011 if (old == 0)
1012 new = 4;
1013 else
1014 new = old * 2;
1015 while (new <= n)
1016 new *= 2;
1017 cd->cd_ndevs = new;
1018 nsp = malloc(new * sizeof(void *), M_DEVBUF,
1019 cold ? M_NOWAIT : M_WAITOK);
1020 if (nsp == NULL)
1021 panic("config_attach: %sing dev array",
1022 old != 0 ? "expand" : "creat");
1023 memset(nsp + old, 0, (new - old) * sizeof(void *));
1024 if (old != 0) {
1025 memcpy(nsp, cd->cd_devs, old * sizeof(void *));
1026 free(cd->cd_devs, M_DEVBUF);
1027 }
1028 cd->cd_devs = nsp;
1029 }
1030
1031 static void
1032 config_devlink(device_t dev)
1033 {
1034 struct cfdriver *cd = dev->dv_cfdriver;
1035
1036 /* put this device in the devices array */
1037 config_makeroom(dev->dv_unit, cd);
1038 if (cd->cd_devs[dev->dv_unit])
1039 panic("config_attach: duplicate %s", dev->dv_xname);
1040 cd->cd_devs[dev->dv_unit] = dev;
1041
1042 TAILQ_INSERT_TAIL(&alldevs, dev, dv_list); /* link up */
1043 }
1044
1045 static void
1046 config_devunlink(device_t dev)
1047 {
1048 struct cfdriver *cd = dev->dv_cfdriver;
1049 int i;
1050
1051 /* Unlink from device list. */
1052 TAILQ_REMOVE(&alldevs, dev, dv_list);
1053
1054 /* Remove from cfdriver's array. */
1055 cd->cd_devs[dev->dv_unit] = NULL;
1056
1057 /*
1058 * If the device now has no units in use, deallocate its softc array.
1059 */
1060 for (i = 0; i < cd->cd_ndevs; i++)
1061 if (cd->cd_devs[i] != NULL)
1062 break;
1063 if (i == cd->cd_ndevs) { /* nothing found; deallocate */
1064 free(cd->cd_devs, M_DEVBUF);
1065 cd->cd_devs = NULL;
1066 cd->cd_ndevs = 0;
1067 }
1068 }
1069
1070 static device_t
1071 config_devalloc(const device_t parent, const cfdata_t cf, const int *locs)
1072 {
1073 struct cfdriver *cd;
1074 struct cfattach *ca;
1075 size_t lname, lunit;
1076 const char *xunit;
1077 int myunit;
1078 char num[10];
1079 device_t dev;
1080 void *dev_private;
1081 const struct cfiattrdata *ia;
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 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_activity_count = 0;
1141 dev->dv_activity_handlers = NULL;
1142 dev->dv_private = dev_private;
1143 memcpy(dev->dv_xname, cd->cd_name, lname);
1144 memcpy(dev->dv_xname + lname, xunit, lunit);
1145 dev->dv_parent = parent;
1146 if (parent != NULL)
1147 dev->dv_depth = parent->dv_depth + 1;
1148 else
1149 dev->dv_depth = 0;
1150 dev->dv_flags = DVF_ACTIVE; /* always initially active */
1151 dev->dv_flags |= ca->ca_flags; /* inherit flags from class */
1152 if (locs) {
1153 KASSERT(parent); /* no locators at root */
1154 ia = cfiattr_lookup(cf->cf_pspec->cfp_iattr,
1155 parent->dv_cfdriver);
1156 dev->dv_locators = malloc(ia->ci_loclen * sizeof(int),
1157 M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1158 memcpy(dev->dv_locators, locs, ia->ci_loclen * sizeof(int));
1159 }
1160 dev->dv_properties = prop_dictionary_create();
1161 KASSERT(dev->dv_properties != NULL);
1162
1163 return (dev);
1164 }
1165
1166 static void
1167 config_devdealloc(device_t dev)
1168 {
1169
1170 KASSERT(dev->dv_properties != NULL);
1171 prop_object_release(dev->dv_properties);
1172
1173 if (dev->dv_activity_handlers)
1174 panic("config_devdealloc with registered handlers");
1175
1176 if (dev->dv_locators)
1177 free(dev->dv_locators, M_DEVBUF);
1178
1179 if ((dev->dv_flags & DVF_PRIV_ALLOC) != 0)
1180 free(dev->dv_private, M_DEVBUF);
1181
1182 free(dev, M_DEVBUF);
1183 }
1184
1185 /*
1186 * Attach a found device.
1187 */
1188 device_t
1189 config_attach_loc(device_t parent, cfdata_t cf,
1190 const int *locs, void *aux, cfprint_t print)
1191 {
1192 device_t dev;
1193 struct cftable *ct;
1194 const char *drvname;
1195
1196 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
1197 if (splash_progress_state)
1198 splash_progress_update(splash_progress_state);
1199 #endif
1200
1201 dev = config_devalloc(parent, cf, locs);
1202 if (!dev)
1203 panic("config_attach: allocation of device softc failed");
1204
1205 /* XXX redundant - see below? */
1206 if (cf->cf_fstate != FSTATE_STAR) {
1207 KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
1208 cf->cf_fstate = FSTATE_FOUND;
1209 }
1210 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1211 else
1212 cf->cf_unit++;
1213 #endif
1214
1215 config_devlink(dev);
1216
1217 if (config_do_twiddle)
1218 twiddle();
1219 else
1220 aprint_naive("Found ");
1221 /*
1222 * We want the next two printfs for normal, verbose, and quiet,
1223 * but not silent (in which case, we're twiddling, instead).
1224 */
1225 if (parent == ROOT) {
1226 aprint_naive("%s (root)", dev->dv_xname);
1227 aprint_normal("%s (root)", dev->dv_xname);
1228 } else {
1229 aprint_naive("%s at %s", dev->dv_xname, parent->dv_xname);
1230 aprint_normal("%s at %s", dev->dv_xname, parent->dv_xname);
1231 if (print)
1232 (void) (*print)(aux, NULL);
1233 }
1234
1235 /*
1236 * Before attaching, clobber any unfound devices that are
1237 * otherwise identical.
1238 * XXX code above is redundant?
1239 */
1240 drvname = dev->dv_cfdriver->cd_name;
1241 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1242 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1243 if (STREQ(cf->cf_name, drvname) &&
1244 cf->cf_unit == dev->dv_unit) {
1245 if (cf->cf_fstate == FSTATE_NOTFOUND)
1246 cf->cf_fstate = FSTATE_FOUND;
1247 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1248 /*
1249 * Bump the unit number on all starred cfdata
1250 * entries for this device.
1251 */
1252 if (cf->cf_fstate == FSTATE_STAR)
1253 cf->cf_unit++;
1254 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
1255 }
1256 }
1257 }
1258 #ifdef __HAVE_DEVICE_REGISTER
1259 device_register(dev, aux);
1260 #endif
1261
1262 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
1263 if (splash_progress_state)
1264 splash_progress_update(splash_progress_state);
1265 #endif
1266 (*dev->dv_cfattach->ca_attach)(parent, dev, aux);
1267 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
1268 if (splash_progress_state)
1269 splash_progress_update(splash_progress_state);
1270 #endif
1271
1272 if (!device_pnp_is_registered(dev))
1273 aprint_error_dev(dev, "WARNING: power management not supported\n");
1274
1275 config_process_deferred(&deferred_config_queue, dev);
1276 return (dev);
1277 }
1278
1279 device_t
1280 config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print)
1281 {
1282
1283 return (config_attach_loc(parent, cf, NULL, aux, print));
1284 }
1285
1286 /*
1287 * As above, but for pseudo-devices. Pseudo-devices attached in this
1288 * way are silently inserted into the device tree, and their children
1289 * attached.
1290 *
1291 * Note that because pseudo-devices are attached silently, any information
1292 * the attach routine wishes to print should be prefixed with the device
1293 * name by the attach routine.
1294 */
1295 device_t
1296 config_attach_pseudo(cfdata_t cf)
1297 {
1298 device_t dev;
1299
1300 dev = config_devalloc(ROOT, cf, NULL);
1301 if (!dev)
1302 return (NULL);
1303
1304 /* XXX mark busy in cfdata */
1305
1306 config_devlink(dev);
1307
1308 #if 0 /* XXXJRT not yet */
1309 #ifdef __HAVE_DEVICE_REGISTER
1310 device_register(dev, NULL); /* like a root node */
1311 #endif
1312 #endif
1313 (*dev->dv_cfattach->ca_attach)(ROOT, dev, NULL);
1314 config_process_deferred(&deferred_config_queue, dev);
1315 return (dev);
1316 }
1317
1318 /*
1319 * Detach a device. Optionally forced (e.g. because of hardware
1320 * removal) and quiet. Returns zero if successful, non-zero
1321 * (an error code) otherwise.
1322 *
1323 * Note that this code wants to be run from a process context, so
1324 * that the detach can sleep to allow processes which have a device
1325 * open to run and unwind their stacks.
1326 */
1327 int
1328 config_detach(device_t dev, int flags)
1329 {
1330 struct cftable *ct;
1331 cfdata_t cf;
1332 const struct cfattach *ca;
1333 struct cfdriver *cd;
1334 #ifdef DIAGNOSTIC
1335 device_t d;
1336 #endif
1337 int rv = 0;
1338
1339 #ifdef DIAGNOSTIC
1340 if (dev->dv_cfdata != NULL &&
1341 dev->dv_cfdata->cf_fstate != FSTATE_FOUND &&
1342 dev->dv_cfdata->cf_fstate != FSTATE_STAR)
1343 panic("config_detach: bad device fstate");
1344 #endif
1345 cd = dev->dv_cfdriver;
1346 KASSERT(cd != NULL);
1347
1348 ca = dev->dv_cfattach;
1349 KASSERT(ca != NULL);
1350
1351 /*
1352 * Ensure the device is deactivated. If the device doesn't
1353 * have an activation entry point, we allow DVF_ACTIVE to
1354 * remain set. Otherwise, if DVF_ACTIVE is still set, the
1355 * device is busy, and the detach fails.
1356 */
1357 if (ca->ca_activate != NULL)
1358 rv = config_deactivate(dev);
1359
1360 /*
1361 * Try to detach the device. If that's not possible, then
1362 * we either panic() (for the forced but failed case), or
1363 * return an error.
1364 */
1365 if (rv == 0) {
1366 if (ca->ca_detach != NULL)
1367 rv = (*ca->ca_detach)(dev, flags);
1368 else
1369 rv = EOPNOTSUPP;
1370 }
1371 if (rv != 0) {
1372 if ((flags & DETACH_FORCE) == 0)
1373 return (rv);
1374 else
1375 panic("config_detach: forced detach of %s failed (%d)",
1376 dev->dv_xname, rv);
1377 }
1378
1379 /*
1380 * The device has now been successfully detached.
1381 */
1382
1383 #ifdef DIAGNOSTIC
1384 /*
1385 * Sanity: If you're successfully detached, you should have no
1386 * children. (Note that because children must be attached
1387 * after parents, we only need to search the latter part of
1388 * the list.)
1389 */
1390 for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
1391 d = TAILQ_NEXT(d, dv_list)) {
1392 if (d->dv_parent == dev) {
1393 printf("config_detach: detached device %s"
1394 " has children %s\n", dev->dv_xname, d->dv_xname);
1395 panic("config_detach");
1396 }
1397 }
1398 #endif
1399
1400 /* notify the parent that the child is gone */
1401 if (dev->dv_parent) {
1402 device_t p = dev->dv_parent;
1403 if (p->dv_cfattach->ca_childdetached)
1404 (*p->dv_cfattach->ca_childdetached)(p, dev);
1405 }
1406
1407 /*
1408 * Mark cfdata to show that the unit can be reused, if possible.
1409 */
1410 TAILQ_FOREACH(ct, &allcftables, ct_list) {
1411 for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
1412 if (STREQ(cf->cf_name, cd->cd_name)) {
1413 if (cf->cf_fstate == FSTATE_FOUND &&
1414 cf->cf_unit == dev->dv_unit)
1415 cf->cf_fstate = FSTATE_NOTFOUND;
1416 #ifdef __BROKEN_CONFIG_UNIT_USAGE
1417 /*
1418 * Note that we can only re-use a starred
1419 * unit number if the unit being detached
1420 * had the last assigned unit number.
1421 */
1422 if (cf->cf_fstate == FSTATE_STAR &&
1423 cf->cf_unit == dev->dv_unit + 1)
1424 cf->cf_unit--;
1425 #endif /* __BROKEN_CONFIG_UNIT_USAGE */
1426 }
1427 }
1428 }
1429
1430 config_devunlink(dev);
1431
1432 if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
1433 aprint_normal("%s detached\n", dev->dv_xname);
1434
1435 config_devdealloc(dev);
1436
1437 return (0);
1438 }
1439
1440 int
1441 config_activate(device_t dev)
1442 {
1443 const struct cfattach *ca = dev->dv_cfattach;
1444 int rv = 0, oflags = dev->dv_flags;
1445
1446 if (ca->ca_activate == NULL)
1447 return (EOPNOTSUPP);
1448
1449 if ((dev->dv_flags & DVF_ACTIVE) == 0) {
1450 dev->dv_flags |= DVF_ACTIVE;
1451 rv = (*ca->ca_activate)(dev, DVACT_ACTIVATE);
1452 if (rv)
1453 dev->dv_flags = oflags;
1454 }
1455 return (rv);
1456 }
1457
1458 int
1459 config_deactivate(device_t dev)
1460 {
1461 const struct cfattach *ca = dev->dv_cfattach;
1462 int rv = 0, oflags = dev->dv_flags;
1463
1464 if (ca->ca_activate == NULL)
1465 return (EOPNOTSUPP);
1466
1467 if (dev->dv_flags & DVF_ACTIVE) {
1468 dev->dv_flags &= ~DVF_ACTIVE;
1469 rv = (*ca->ca_activate)(dev, DVACT_DEACTIVATE);
1470 if (rv)
1471 dev->dv_flags = oflags;
1472 }
1473 return (rv);
1474 }
1475
1476 /*
1477 * Defer the configuration of the specified device until all
1478 * of its parent's devices have been attached.
1479 */
1480 void
1481 config_defer(device_t dev, void (*func)(device_t))
1482 {
1483 struct deferred_config *dc;
1484
1485 if (dev->dv_parent == NULL)
1486 panic("config_defer: can't defer config of a root device");
1487
1488 #ifdef DIAGNOSTIC
1489 for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
1490 dc = TAILQ_NEXT(dc, dc_queue)) {
1491 if (dc->dc_dev == dev)
1492 panic("config_defer: deferred twice");
1493 }
1494 #endif
1495
1496 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1497 if (dc == NULL)
1498 panic("config_defer: unable to allocate callback");
1499
1500 dc->dc_dev = dev;
1501 dc->dc_func = func;
1502 TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
1503 config_pending_incr();
1504 }
1505
1506 /*
1507 * Defer some autoconfiguration for a device until after interrupts
1508 * are enabled.
1509 */
1510 void
1511 config_interrupts(device_t dev, void (*func)(device_t))
1512 {
1513 struct deferred_config *dc;
1514
1515 /*
1516 * If interrupts are enabled, callback now.
1517 */
1518 if (cold == 0) {
1519 (*func)(dev);
1520 return;
1521 }
1522
1523 #ifdef DIAGNOSTIC
1524 for (dc = TAILQ_FIRST(&interrupt_config_queue); dc != NULL;
1525 dc = TAILQ_NEXT(dc, dc_queue)) {
1526 if (dc->dc_dev == dev)
1527 panic("config_interrupts: deferred twice");
1528 }
1529 #endif
1530
1531 dc = malloc(sizeof(*dc), M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
1532 if (dc == NULL)
1533 panic("config_interrupts: unable to allocate callback");
1534
1535 dc->dc_dev = dev;
1536 dc->dc_func = func;
1537 TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
1538 config_pending_incr();
1539 }
1540
1541 /*
1542 * Process a deferred configuration queue.
1543 */
1544 static void
1545 config_process_deferred(struct deferred_config_head *queue,
1546 device_t parent)
1547 {
1548 struct deferred_config *dc, *ndc;
1549
1550 for (dc = TAILQ_FIRST(queue); dc != NULL; dc = ndc) {
1551 ndc = TAILQ_NEXT(dc, dc_queue);
1552 if (parent == NULL || dc->dc_dev->dv_parent == parent) {
1553 TAILQ_REMOVE(queue, dc, dc_queue);
1554 (*dc->dc_func)(dc->dc_dev);
1555 free(dc, M_DEVBUF);
1556 config_pending_decr();
1557 }
1558 }
1559 }
1560
1561 /*
1562 * Manipulate the config_pending semaphore.
1563 */
1564 void
1565 config_pending_incr(void)
1566 {
1567
1568 config_pending++;
1569 }
1570
1571 void
1572 config_pending_decr(void)
1573 {
1574
1575 #ifdef DIAGNOSTIC
1576 if (config_pending == 0)
1577 panic("config_pending_decr: config_pending == 0");
1578 #endif
1579 config_pending--;
1580 if (config_pending == 0)
1581 wakeup(&config_pending);
1582 }
1583
1584 /*
1585 * Register a "finalization" routine. Finalization routines are
1586 * called iteratively once all real devices have been found during
1587 * autoconfiguration, for as long as any one finalizer has done
1588 * any work.
1589 */
1590 int
1591 config_finalize_register(device_t dev, int (*fn)(device_t))
1592 {
1593 struct finalize_hook *f;
1594
1595 /*
1596 * If finalization has already been done, invoke the
1597 * callback function now.
1598 */
1599 if (config_finalize_done) {
1600 while ((*fn)(dev) != 0)
1601 /* loop */ ;
1602 }
1603
1604 /* Ensure this isn't already on the list. */
1605 TAILQ_FOREACH(f, &config_finalize_list, f_list) {
1606 if (f->f_func == fn && f->f_dev == dev)
1607 return (EEXIST);
1608 }
1609
1610 f = malloc(sizeof(*f), M_TEMP, M_WAITOK);
1611 f->f_func = fn;
1612 f->f_dev = dev;
1613 TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
1614
1615 return (0);
1616 }
1617
1618 void
1619 config_finalize(void)
1620 {
1621 struct finalize_hook *f;
1622 int rv;
1623
1624 /* Run the hooks until none of them does any work. */
1625 do {
1626 rv = 0;
1627 TAILQ_FOREACH(f, &config_finalize_list, f_list)
1628 rv |= (*f->f_func)(f->f_dev);
1629 } while (rv != 0);
1630
1631 config_finalize_done = 1;
1632
1633 /* Now free all the hooks. */
1634 while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
1635 TAILQ_REMOVE(&config_finalize_list, f, f_list);
1636 free(f, M_TEMP);
1637 }
1638 }
1639
1640 /*
1641 * device_lookup:
1642 *
1643 * Look up a device instance for a given driver.
1644 */
1645 void *
1646 device_lookup(cfdriver_t cd, int unit)
1647 {
1648
1649 if (unit < 0 || unit >= cd->cd_ndevs)
1650 return (NULL);
1651
1652 return (cd->cd_devs[unit]);
1653 }
1654
1655 /*
1656 * Accessor functions for the device_t type.
1657 */
1658 devclass_t
1659 device_class(device_t dev)
1660 {
1661
1662 return (dev->dv_class);
1663 }
1664
1665 cfdata_t
1666 device_cfdata(device_t dev)
1667 {
1668
1669 return (dev->dv_cfdata);
1670 }
1671
1672 cfdriver_t
1673 device_cfdriver(device_t dev)
1674 {
1675
1676 return (dev->dv_cfdriver);
1677 }
1678
1679 cfattach_t
1680 device_cfattach(device_t dev)
1681 {
1682
1683 return (dev->dv_cfattach);
1684 }
1685
1686 int
1687 device_unit(device_t dev)
1688 {
1689
1690 return (dev->dv_unit);
1691 }
1692
1693 const char *
1694 device_xname(device_t dev)
1695 {
1696
1697 return (dev->dv_xname);
1698 }
1699
1700 device_t
1701 device_parent(device_t dev)
1702 {
1703
1704 return (dev->dv_parent);
1705 }
1706
1707 bool
1708 device_is_active(device_t dev)
1709 {
1710 int active_flags;
1711
1712 active_flags = DVF_ACTIVE;
1713 active_flags |= DVF_CLASS_SUSPENDED;
1714 active_flags |= DVF_DRIVER_SUSPENDED;
1715 active_flags |= DVF_BUS_SUSPENDED;
1716
1717 return ((dev->dv_flags & active_flags) == DVF_ACTIVE);
1718 }
1719
1720 bool
1721 device_is_enabled(device_t dev)
1722 {
1723 return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
1724 }
1725
1726 bool
1727 device_has_power(device_t dev)
1728 {
1729 int active_flags;
1730
1731 active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
1732
1733 return ((dev->dv_flags & active_flags) == DVF_ACTIVE);
1734 }
1735
1736 int
1737 device_locator(device_t dev, u_int locnum)
1738 {
1739
1740 KASSERT(dev->dv_locators != NULL);
1741 return (dev->dv_locators[locnum]);
1742 }
1743
1744 void *
1745 device_private(device_t dev)
1746 {
1747
1748 return (dev->dv_private);
1749 }
1750
1751 prop_dictionary_t
1752 device_properties(device_t dev)
1753 {
1754
1755 return (dev->dv_properties);
1756 }
1757
1758 /*
1759 * device_is_a:
1760 *
1761 * Returns true if the device is an instance of the specified
1762 * driver.
1763 */
1764 bool
1765 device_is_a(device_t dev, const char *dname)
1766 {
1767
1768 return (strcmp(dev->dv_cfdriver->cd_name, dname) == 0);
1769 }
1770
1771 /*
1772 * Power management related functions.
1773 */
1774
1775 bool
1776 device_pnp_is_registered(device_t dev)
1777 {
1778 return (dev->dv_flags & DVF_POWER_HANDLERS) != 0;
1779 }
1780
1781 bool
1782 device_pnp_driver_suspend(device_t dev)
1783 {
1784 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
1785 return true;
1786 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
1787 return false;
1788 if (*dev->dv_driver_suspend != NULL &&
1789 !(*dev->dv_driver_suspend)(dev))
1790 return false;
1791
1792 dev->dv_flags |= DVF_DRIVER_SUSPENDED;
1793 return true;
1794 }
1795
1796 bool
1797 device_pnp_driver_resume(device_t dev)
1798 {
1799 if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
1800 return true;
1801 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
1802 return false;
1803 if (*dev->dv_driver_resume != NULL &&
1804 !(*dev->dv_driver_resume)(dev))
1805 return false;
1806
1807 dev->dv_flags &= ~DVF_DRIVER_SUSPENDED;
1808 return true;
1809 }
1810
1811 void
1812 device_pnp_driver_register(device_t dev,
1813 bool (*suspend)(device_t), bool (*resume)(device_t))
1814 {
1815 dev->dv_driver_suspend = suspend;
1816 dev->dv_driver_resume = resume;
1817 dev->dv_flags |= DVF_POWER_HANDLERS;
1818 }
1819
1820 void
1821 device_pnp_driver_deregister(device_t dev)
1822 {
1823 dev->dv_driver_suspend = NULL;
1824 dev->dv_driver_resume = NULL;
1825 dev->dv_flags &= ~DVF_POWER_HANDLERS;
1826 }
1827
1828 bool
1829 device_pnp_driver_child_register(device_t dev)
1830 {
1831 device_t parent = device_parent(dev);
1832
1833 if (parent == NULL || parent->dv_driver_child_register == NULL)
1834 return true;
1835 return (*parent->dv_driver_child_register)(dev);
1836 }
1837
1838 void
1839 device_pnp_driver_set_child_register(device_t dev,
1840 bool (*child_register)(device_t))
1841 {
1842 dev->dv_driver_child_register = child_register;
1843 }
1844
1845 void *
1846 device_pnp_bus_private(device_t dev)
1847 {
1848 return dev->dv_bus_private;
1849 }
1850
1851 bool
1852 device_pnp_bus_suspend(device_t dev)
1853 {
1854 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
1855 return true;
1856 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 ||
1857 (dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
1858 return false;
1859 if (*dev->dv_bus_suspend != NULL &&
1860 !(*dev->dv_bus_suspend)(dev))
1861 return false;
1862
1863 dev->dv_flags |= DVF_BUS_SUSPENDED;
1864 return true;
1865 }
1866
1867 bool
1868 device_pnp_bus_resume(device_t dev)
1869 {
1870 if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0)
1871 return true;
1872 if (*dev->dv_bus_resume != NULL &&
1873 !(*dev->dv_bus_resume)(dev))
1874 return false;
1875
1876 dev->dv_flags &= ~DVF_BUS_SUSPENDED;
1877 return true;
1878 }
1879
1880 void
1881 device_pnp_bus_register(device_t dev, void *priv,
1882 bool (*suspend)(device_t), bool (*resume)(device_t),
1883 void (*deregister)(device_t))
1884 {
1885 dev->dv_bus_private = priv;
1886 dev->dv_bus_resume = resume;
1887 dev->dv_bus_suspend = suspend;
1888 dev->dv_bus_deregister = deregister;
1889 }
1890
1891 void
1892 device_pnp_bus_deregister(device_t dev)
1893 {
1894 if (dev->dv_bus_deregister == NULL)
1895 return;
1896 (*dev->dv_bus_deregister)(dev);
1897 dev->dv_bus_private = NULL;
1898 dev->dv_bus_suspend = NULL;
1899 dev->dv_bus_resume = NULL;
1900 dev->dv_bus_deregister = NULL;
1901 }
1902
1903 void *
1904 device_pnp_class_private(device_t dev)
1905 {
1906 return dev->dv_class_private;
1907 }
1908
1909 bool
1910 device_pnp_class_suspend(device_t dev)
1911 {
1912 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0)
1913 return true;
1914 if (*dev->dv_class_suspend != NULL &&
1915 !(*dev->dv_class_suspend)(dev))
1916 return false;
1917
1918 dev->dv_flags |= DVF_CLASS_SUSPENDED;
1919 return true;
1920 }
1921
1922 bool
1923 device_pnp_class_resume(device_t dev)
1924 {
1925 if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
1926 return true;
1927 if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 ||
1928 (dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
1929 return false;
1930 if (*dev->dv_class_resume != NULL &&
1931 !(*dev->dv_class_resume)(dev))
1932 return false;
1933
1934 dev->dv_flags &= ~DVF_CLASS_SUSPENDED;
1935 return true;
1936 }
1937
1938 void
1939 device_pnp_class_register(device_t dev, void *priv,
1940 bool (*suspend)(device_t), bool (*resume)(device_t),
1941 void (*deregister)(device_t))
1942 {
1943 dev->dv_class_private = priv;
1944 dev->dv_class_suspend = suspend;
1945 dev->dv_class_resume = resume;
1946 dev->dv_class_deregister = deregister;
1947 }
1948
1949 void
1950 device_pnp_class_deregister(device_t dev)
1951 {
1952 if (dev->dv_class_deregister == NULL)
1953 return;
1954 (*dev->dv_class_deregister)(dev);
1955 dev->dv_class_private = NULL;
1956 dev->dv_class_suspend = NULL;
1957 dev->dv_class_resume = NULL;
1958 dev->dv_class_deregister = NULL;
1959 }
1960
1961 bool
1962 device_active(device_t dev, devactive_t type)
1963 {
1964 size_t i;
1965
1966 if (dev->dv_activity_count == 0)
1967 return false;
1968
1969 for (i = 0; i < dev->dv_activity_count; ++i)
1970 (*dev->dv_activity_handlers[i])(dev, type);
1971
1972 return true;
1973 }
1974
1975 bool
1976 device_active_register(device_t dev, void (*handler)(device_t, devactive_t))
1977 {
1978 void (**new_handlers)(device_t, devactive_t);
1979 void (**old_handlers)(device_t, devactive_t);
1980 size_t i, new_size;
1981 int s;
1982
1983 old_handlers = dev->dv_activity_handlers;
1984
1985 for (i = 0; i < dev->dv_activity_count; ++i) {
1986 if (old_handlers[i] == handler)
1987 panic("Double registering of idle handlers");
1988 }
1989
1990 new_size = dev->dv_activity_count + 1;
1991 new_handlers = malloc(sizeof(void *) * new_size, M_DEVBUF, M_WAITOK);
1992
1993 memcpy(new_handlers, old_handlers,
1994 sizeof(void *) * dev->dv_activity_count);
1995 new_handlers[new_size - 1] = handler;
1996
1997 s = splhigh();
1998 dev->dv_activity_count = new_size;
1999 dev->dv_activity_handlers = new_handlers;
2000 splx(s);
2001
2002 if (old_handlers != NULL)
2003 free(old_handlers, M_DEVBUF);
2004
2005 return true;
2006 }
2007
2008 void
2009 device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t))
2010 {
2011 void (**new_handlers)(device_t, devactive_t);
2012 void (**old_handlers)(device_t, devactive_t);
2013 size_t i, new_size;
2014 int s;
2015
2016 old_handlers = dev->dv_activity_handlers;
2017
2018 for (i = 0; i < dev->dv_activity_count; ++i) {
2019 if (old_handlers[i] == handler)
2020 break;
2021 }
2022
2023 if (i == dev->dv_activity_count)
2024 return; /* XXX panic? */
2025
2026 new_size = dev->dv_activity_count - 1;
2027
2028 if (new_size == 0) {
2029 new_handlers = NULL;
2030 } else {
2031 new_handlers = malloc(sizeof(void *) * new_size, M_DEVBUF,
2032 M_WAITOK);
2033 memcpy(new_handlers, old_handlers, sizeof(void *) * i);
2034 memcpy(new_handlers + i, old_handlers + i + 1,
2035 sizeof(void *) * (new_size - i));
2036 }
2037
2038 s = splhigh();
2039 dev->dv_activity_count = new_size;
2040 dev->dv_activity_handlers = new_handlers;
2041 splx(s);
2042
2043 free(old_handlers, M_DEVBUF);
2044 }
2045