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