xref: /freebsd/sys/kern/subr_bus.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
1 /*-
2  * Copyright (c) 1997,1998 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include "opt_bus.h"
30 
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #ifdef DEVICE_SYSCTLS
37 #include <sys/sysctl.h>
38 #endif
39 #include <sys/kobj.h>
40 #include <sys/bus_private.h>
41 #include <sys/systm.h>
42 #include <machine/bus.h>
43 #include <sys/rman.h>
44 #include <machine/stdarg.h>	/* for device_printf() */
45 
46 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
47 
48 #ifdef BUS_DEBUG
49 #include <sys/sysctl.h>
50 
51 static int bus_debug = 1;
52 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0, "Debug bus code");
53 
54 #define PDEBUG(a)	if (bus_debug) {printf(__FUNCTION__ ":%d: ", __LINE__), printf a, printf("\n");}
55 #define DEVICENAME(d)	((d)? device_get_name(d): "no device")
56 #define DRIVERNAME(d)	((d)? d->name : "no driver")
57 #define DEVCLANAME(d)	((d)? d->name : "no devclass")
58 
59 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
60  * prevent syslog from deleting initial spaces
61  */
62 #define indentprintf(p)	do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf("  "); printf p ; } while(0)
63 
64 static void print_device_short(device_t dev, int indent);
65 static void print_device(device_t dev, int indent);
66 void print_device_tree_short(device_t dev, int indent);
67 void print_device_tree(device_t dev, int indent);
68 static void print_driver_short(driver_t *driver, int indent);
69 static void print_driver(driver_t *driver, int indent);
70 static void print_driver_list(driver_list_t drivers, int indent);
71 static void print_devclass_short(devclass_t dc, int indent);
72 static void print_devclass(devclass_t dc, int indent);
73 void print_devclass_list_short(void);
74 void print_devclass_list(void);
75 
76 #else
77 /* Make the compiler ignore the function calls */
78 #define PDEBUG(a)			/* nop */
79 #define DEVICENAME(d)			/* nop */
80 #define DRIVERNAME(d)			/* nop */
81 #define DEVCLANAME(d)			/* nop */
82 
83 #define print_device_short(d,i)		/* nop */
84 #define print_device(d,i)		/* nop */
85 #define print_device_tree_short(d,i)	/* nop */
86 #define print_device_tree(d,i)		/* nop */
87 #define print_driver_short(d,i)		/* nop */
88 #define print_driver(d,i)		/* nop */
89 #define print_driver_list(d,i)		/* nop */
90 #define print_devclass_short(d,i)	/* nop */
91 #define print_devclass(d,i)		/* nop */
92 #define print_devclass_list_short()	/* nop */
93 #define print_devclass_list()		/* nop */
94 #endif
95 
96 #ifdef DEVICE_SYSCTLS
97 static void device_register_oids(device_t dev);
98 static void device_unregister_oids(device_t dev);
99 #endif
100 
101 extern char static_hints[];
102 static int hints_loaded;
103 
104 kobj_method_t null_methods[] = {
105     { 0, 0 }
106 };
107 
108 DEFINE_CLASS(null, null_methods, 0);
109 
110 /*
111  * Devclass implementation
112  */
113 
114 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
115 
116 static devclass_t
117 devclass_find_internal(const char *classname, int create)
118 {
119     devclass_t dc;
120 
121     PDEBUG(("looking for %s", classname));
122     if (!classname)
123 	return NULL;
124 
125     for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
126 	if (!strcmp(dc->name, classname))
127 	    return dc;
128 
129     PDEBUG(("%s not found%s", classname, (create? ", creating": "")));
130     if (create) {
131 	dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
132 		    M_BUS, M_NOWAIT);
133 	if (!dc)
134 	    return NULL;
135 	bzero(dc, sizeof(struct devclass) + strlen(classname) + 1);
136 	dc->name = (char*) (dc + 1);
137 	strcpy(dc->name, classname);
138 	dc->devices = NULL;
139 	dc->maxunit = 0;
140 	TAILQ_INIT(&dc->drivers);
141 	TAILQ_INSERT_TAIL(&devclasses, dc, link);
142     }
143 
144     return dc;
145 }
146 
147 devclass_t
148 devclass_create(const char *classname)
149 {
150     return devclass_find_internal(classname, TRUE);
151 }
152 
153 devclass_t
154 devclass_find(const char *classname)
155 {
156     return devclass_find_internal(classname, FALSE);
157 }
158 
159 int
160 devclass_add_driver(devclass_t dc, driver_t *driver)
161 {
162     driverlink_t dl;
163     int i;
164 
165     PDEBUG(("%s", DRIVERNAME(driver)));
166 
167     dl = malloc(sizeof *dl, M_BUS, M_NOWAIT);
168     if (!dl)
169 	return ENOMEM;
170     bzero(dl, sizeof *dl);
171 
172     /*
173      * Compile the driver's methods. Also increase the reference count
174      * so that the class doesn't get freed when the last instance
175      * goes. This means we can safely use static methods and avoids a
176      * double-free in devclass_delete_driver.
177      */
178     kobj_class_compile((kobj_class_t) driver);
179 
180     /*
181      * Make sure the devclass which the driver is implementing exists.
182      */
183     devclass_find_internal(driver->name, TRUE);
184 
185     dl->driver = driver;
186     TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
187     driver->refs++;
188 
189     /*
190      * Call BUS_DRIVER_ADDED for any existing busses in this class.
191      */
192     for (i = 0; i < dc->maxunit; i++)
193 	if (dc->devices[i])
194 	    BUS_DRIVER_ADDED(dc->devices[i], driver);
195 
196     return 0;
197 }
198 
199 int
200 devclass_delete_driver(devclass_t busclass, driver_t *driver)
201 {
202     devclass_t dc = devclass_find(driver->name);
203     driverlink_t dl;
204     device_t dev;
205     int i;
206     int error;
207 
208     PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
209 
210     if (!dc)
211 	return 0;
212 
213     /*
214      * Find the link structure in the bus' list of drivers.
215      */
216     for (dl = TAILQ_FIRST(&busclass->drivers); dl;
217 	 dl = TAILQ_NEXT(dl, link)) {
218 	if (dl->driver == driver)
219 	    break;
220     }
221 
222     if (!dl) {
223 	PDEBUG(("%s not found in %s list", driver->name, busclass->name));
224 	return ENOENT;
225     }
226 
227     /*
228      * Disassociate from any devices.  We iterate through all the
229      * devices in the devclass of the driver and detach any which are
230      * using the driver and which have a parent in the devclass which
231      * we are deleting from.
232      *
233      * Note that since a driver can be in multiple devclasses, we
234      * should not detach devices which are not children of devices in
235      * the affected devclass.
236      */
237     for (i = 0; i < dc->maxunit; i++) {
238 	if (dc->devices[i]) {
239 	    dev = dc->devices[i];
240 	    if (dev->driver == driver
241 		&& dev->parent && dev->parent->devclass == busclass) {
242 		if ((error = device_detach(dev)) != 0)
243 		    return error;
244 		device_set_driver(dev, NULL);
245 	    }
246 	}
247     }
248 
249     TAILQ_REMOVE(&busclass->drivers, dl, link);
250     free(dl, M_BUS);
251 
252     driver->refs--;
253     if (driver->refs == 0)
254 	kobj_class_free((kobj_class_t) driver);
255 
256     return 0;
257 }
258 
259 static driverlink_t
260 devclass_find_driver_internal(devclass_t dc, const char *classname)
261 {
262     driverlink_t dl;
263 
264     PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
265 
266     for (dl = TAILQ_FIRST(&dc->drivers); dl; dl = TAILQ_NEXT(dl, link)) {
267 	if (!strcmp(dl->driver->name, classname))
268 	    return dl;
269     }
270 
271     PDEBUG(("not found"));
272     return NULL;
273 }
274 
275 driver_t *
276 devclass_find_driver(devclass_t dc, const char *classname)
277 {
278     driverlink_t dl;
279 
280     dl = devclass_find_driver_internal(dc, classname);
281     if (dl)
282 	return dl->driver;
283     else
284 	return NULL;
285 }
286 
287 const char *
288 devclass_get_name(devclass_t dc)
289 {
290     return dc->name;
291 }
292 
293 device_t
294 devclass_get_device(devclass_t dc, int unit)
295 {
296     if (dc == NULL || unit < 0 || unit >= dc->maxunit)
297 	return NULL;
298     return dc->devices[unit];
299 }
300 
301 void *
302 devclass_get_softc(devclass_t dc, int unit)
303 {
304     device_t dev;
305 
306     dev = devclass_get_device(dc, unit);
307     if (!dev)
308 	return (NULL);
309 
310     return (device_get_softc(dev));
311 }
312 
313 int
314 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
315 {
316     int i;
317     int count;
318     device_t *list;
319 
320     count = 0;
321     for (i = 0; i < dc->maxunit; i++)
322 	if (dc->devices[i])
323 	    count++;
324 
325     list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT);
326     if (!list)
327 	return ENOMEM;
328     bzero(list, count * sizeof(device_t));
329 
330     count = 0;
331     for (i = 0; i < dc->maxunit; i++)
332 	if (dc->devices[i]) {
333 	    list[count] = dc->devices[i];
334 	    count++;
335 	}
336 
337     *devlistp = list;
338     *devcountp = count;
339 
340     return 0;
341 }
342 
343 int
344 devclass_get_maxunit(devclass_t dc)
345 {
346     return dc->maxunit;
347 }
348 
349 static int
350 devclass_alloc_unit(devclass_t dc, int *unitp)
351 {
352     int unit = *unitp;
353 
354     PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
355 
356     /* If we have been given a wired unit number, check for existing device */
357     if (unit != -1) {
358 	if (unit >= 0 && unit < dc->maxunit && dc->devices[unit] != NULL) {
359 	    if (bootverbose)
360 		printf("%s-: %s%d exists, using next available unit number\n",
361 		       dc->name, dc->name, unit);
362 	    /* find the next available slot */
363 	    while (++unit < dc->maxunit && dc->devices[unit] != NULL)
364 		;
365 	}
366     }
367     else {
368 	/* Unwired device, find the next available slot for it */
369     	unit = 0;
370 	while (unit < dc->maxunit && dc->devices[unit] != NULL)
371 	    unit++;
372     }
373 
374     /*
375      * We've selected a unit beyond the length of the table, so let's extend
376      * the table to make room for all units up to and including this one.
377      */
378     if (unit >= dc->maxunit) {
379 	device_t *newlist;
380 	int newsize;
381 
382 	newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
383 	newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
384 	if (!newlist)
385 	    return ENOMEM;
386 	bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
387 	bzero(newlist + dc->maxunit,
388 	      sizeof(device_t) * (newsize - dc->maxunit));
389 	if (dc->devices)
390 	    free(dc->devices, M_BUS);
391 	dc->devices = newlist;
392 	dc->maxunit = newsize;
393     }
394     PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
395 
396     *unitp = unit;
397     return 0;
398 }
399 
400 static int
401 devclass_add_device(devclass_t dc, device_t dev)
402 {
403     int buflen, error;
404 
405     PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
406 
407     buflen = strlen(dc->name) + 5;
408     dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT);
409     if (!dev->nameunit)
410 	return ENOMEM;
411     bzero(dev->nameunit, buflen);
412 
413     if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
414 	free(dev->nameunit, M_BUS);
415 	dev->nameunit = NULL;
416 	return error;
417     }
418     dc->devices[dev->unit] = dev;
419     dev->devclass = dc;
420     snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
421 
422 #ifdef DEVICE_SYSCTLS
423     device_register_oids(dev);
424 #endif
425 
426     return 0;
427 }
428 
429 static int
430 devclass_delete_device(devclass_t dc, device_t dev)
431 {
432     if (!dc || !dev)
433 	return 0;
434 
435     PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
436 
437     if (dev->devclass != dc
438 	|| dc->devices[dev->unit] != dev)
439 	panic("devclass_delete_device: inconsistent device class");
440     dc->devices[dev->unit] = NULL;
441     if (dev->flags & DF_WILDCARD)
442 	dev->unit = -1;
443     dev->devclass = NULL;
444     free(dev->nameunit, M_BUS);
445     dev->nameunit = NULL;
446 
447 #ifdef DEVICE_SYSCTLS
448     device_unregister_oids(dev);
449 #endif
450 
451     return 0;
452 }
453 
454 static device_t
455 make_device(device_t parent, const char *name, int unit)
456 {
457     device_t dev;
458     devclass_t dc;
459 
460     PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
461 
462     if (name) {
463 	dc = devclass_find_internal(name, TRUE);
464 	if (!dc) {
465 	    printf("make_device: can't find device class %s\n", name);
466 	    return NULL;
467 	}
468     } else
469 	dc = NULL;
470 
471     dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT);
472     if (!dev)
473 	return 0;
474     bzero(dev, sizeof(struct device));
475 
476     dev->parent = parent;
477     TAILQ_INIT(&dev->children);
478     kobj_init((kobj_t) dev, &null_class);
479     dev->driver = NULL;
480     dev->devclass = NULL;
481     dev->unit = unit;
482     dev->nameunit = NULL;
483     dev->desc = NULL;
484     dev->busy = 0;
485     dev->devflags = 0;
486     dev->flags = DF_ENABLED;
487     dev->order = 0;
488     if (unit == -1)
489 	dev->flags |= DF_WILDCARD;
490     if (name) {
491 	dev->flags |= DF_FIXEDCLASS;
492 	devclass_add_device(dc, dev);
493     }
494     dev->ivars = NULL;
495     dev->softc = NULL;
496 
497     dev->state = DS_NOTPRESENT;
498 
499     kobj_init((kobj_t) dev, &null_class);
500 
501     return dev;
502 }
503 
504 static int
505 device_print_child(device_t dev, device_t child)
506 {
507     int retval = 0;
508 
509     if (device_is_alive(child)) {
510 	retval += BUS_PRINT_CHILD(dev, child);
511     } else
512 	retval += device_printf(child, " not found\n");
513 
514     return (retval);
515 }
516 
517 device_t
518 device_add_child(device_t dev, const char *name, int unit)
519 {
520     return device_add_child_ordered(dev, 0, name, unit);
521 }
522 
523 device_t
524 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
525 {
526     device_t child;
527     device_t place;
528 
529     PDEBUG(("%s at %s with order %d as unit %d",
530 	    name, DEVICENAME(dev), order, unit));
531 
532     child = make_device(dev, name, unit);
533     if (child == NULL)
534 	return child;
535     child->order = order;
536 
537     TAILQ_FOREACH(place, &dev->children, link)
538 	if (place->order > order)
539 	    break;
540 
541     if (place) {
542 	/*
543 	 * The device 'place' is the first device whose order is
544 	 * greater than the new child.
545 	 */
546 	TAILQ_INSERT_BEFORE(place, child, link);
547     } else {
548 	/*
549 	 * The new child's order is greater or equal to the order of
550 	 * any existing device. Add the child to the tail of the list.
551 	 */
552 	TAILQ_INSERT_TAIL(&dev->children, child, link);
553     }
554 
555     return child;
556 }
557 
558 int
559 device_delete_child(device_t dev, device_t child)
560 {
561     int error;
562     device_t grandchild;
563 
564     PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
565 
566     /* remove children first */
567     while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
568         error = device_delete_child(child, grandchild);
569 	if (error)
570 	    return error;
571     }
572 
573     if ((error = device_detach(child)) != 0)
574 	return error;
575     if (child->devclass)
576 	devclass_delete_device(child->devclass, child);
577     TAILQ_REMOVE(&dev->children, child, link);
578     device_set_desc(child, NULL);
579     free(child, M_BUS);
580 
581     return 0;
582 }
583 
584 /*
585  * Find only devices attached to this bus.
586  */
587 device_t
588 device_find_child(device_t dev, const char *classname, int unit)
589 {
590     devclass_t dc;
591     device_t child;
592 
593     dc = devclass_find(classname);
594     if (!dc)
595 	return NULL;
596 
597     child = devclass_get_device(dc, unit);
598     if (child && child->parent == dev)
599 	return child;
600     return NULL;
601 }
602 
603 static driverlink_t
604 first_matching_driver(devclass_t dc, device_t dev)
605 {
606     if (dev->devclass)
607 	return devclass_find_driver_internal(dc, dev->devclass->name);
608     else
609 	return TAILQ_FIRST(&dc->drivers);
610 }
611 
612 static driverlink_t
613 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
614 {
615     if (dev->devclass) {
616 	driverlink_t dl;
617 	for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
618 	    if (!strcmp(dev->devclass->name, dl->driver->name))
619 		return dl;
620 	return NULL;
621     } else
622 	return TAILQ_NEXT(last, link);
623 }
624 
625 static int
626 device_probe_child(device_t dev, device_t child)
627 {
628     devclass_t dc;
629     driverlink_t best = 0;
630     driverlink_t dl;
631     int result, pri = 0;
632     int hasclass = (child->devclass != 0);
633 
634     dc = dev->devclass;
635     if (!dc)
636 	panic("device_probe_child: parent device has no devclass");
637 
638     if (child->state == DS_ALIVE)
639 	return 0;
640 
641     for (dl = first_matching_driver(dc, child);
642 	 dl;
643 	 dl = next_matching_driver(dc, child, dl)) {
644 	PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
645 	device_set_driver(child, dl->driver);
646 	if (!hasclass)
647 	    device_set_devclass(child, dl->driver->name);
648 	result = DEVICE_PROBE(child);
649 	if (!hasclass)
650 	    device_set_devclass(child, 0);
651 
652 	/*
653 	 * If the driver returns SUCCESS, there can be no higher match
654 	 * for this device.
655 	 */
656 	if (result == 0) {
657 	    best = dl;
658 	    pri = 0;
659 	    break;
660 	}
661 
662 	/*
663 	 * The driver returned an error so it certainly doesn't match.
664 	 */
665 	if (result > 0) {
666 	    device_set_driver(child, 0);
667 	    continue;
668 	}
669 
670 	/*
671 	 * A priority lower than SUCCESS, remember the best matching
672 	 * driver. Initialise the value of pri for the first match.
673 	 */
674 	if (best == 0 || result > pri) {
675 	    best = dl;
676 	    pri = result;
677 	    continue;
678 	}
679     }
680 
681     /*
682      * If we found a driver, change state and initialise the devclass.
683      */
684     if (best) {
685 	if (!child->devclass)
686 	    device_set_devclass(child, best->driver->name);
687 	device_set_driver(child, best->driver);
688 	if (pri < 0) {
689 	    /*
690 	     * A bit bogus. Call the probe method again to make sure
691 	     * that we have the right description.
692 	     */
693 	    DEVICE_PROBE(child);
694 	}
695 	child->state = DS_ALIVE;
696 	return 0;
697     }
698 
699     return ENXIO;
700 }
701 
702 device_t
703 device_get_parent(device_t dev)
704 {
705     return dev->parent;
706 }
707 
708 int
709 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
710 {
711     int count;
712     device_t child;
713     device_t *list;
714 
715     count = 0;
716     for (child = TAILQ_FIRST(&dev->children); child;
717 	 child = TAILQ_NEXT(child, link))
718 	count++;
719 
720     list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT);
721     if (!list)
722 	return ENOMEM;
723     bzero(list, count * sizeof(device_t));
724 
725     count = 0;
726     for (child = TAILQ_FIRST(&dev->children); child;
727 	 child = TAILQ_NEXT(child, link)) {
728 	list[count] = child;
729 	count++;
730     }
731 
732     *devlistp = list;
733     *devcountp = count;
734 
735     return 0;
736 }
737 
738 driver_t *
739 device_get_driver(device_t dev)
740 {
741     return dev->driver;
742 }
743 
744 devclass_t
745 device_get_devclass(device_t dev)
746 {
747     return dev->devclass;
748 }
749 
750 const char *
751 device_get_name(device_t dev)
752 {
753     if (dev->devclass)
754 	return devclass_get_name(dev->devclass);
755     return NULL;
756 }
757 
758 const char *
759 device_get_nameunit(device_t dev)
760 {
761     return dev->nameunit;
762 }
763 
764 int
765 device_get_unit(device_t dev)
766 {
767     return dev->unit;
768 }
769 
770 const char *
771 device_get_desc(device_t dev)
772 {
773     return dev->desc;
774 }
775 
776 u_int32_t
777 device_get_flags(device_t dev)
778 {
779     return dev->devflags;
780 }
781 
782 int
783 device_print_prettyname(device_t dev)
784 {
785     const char *name = device_get_name(dev);
786 
787     if (name == 0)
788 	return printf("unknown: ");
789     else
790 	return printf("%s%d: ", name, device_get_unit(dev));
791 }
792 
793 int
794 device_printf(device_t dev, const char * fmt, ...)
795 {
796     va_list ap;
797     int retval;
798 
799     retval = device_print_prettyname(dev);
800     va_start(ap, fmt);
801     retval += vprintf(fmt, ap);
802     va_end(ap);
803     return retval;
804 }
805 
806 static void
807 device_set_desc_internal(device_t dev, const char* desc, int copy)
808 {
809     if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
810 	free(dev->desc, M_BUS);
811 	dev->flags &= ~DF_DESCMALLOCED;
812 	dev->desc = NULL;
813     }
814 
815     if (copy && desc) {
816 	dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
817 	if (dev->desc) {
818 	    strcpy(dev->desc, desc);
819 	    dev->flags |= DF_DESCMALLOCED;
820 	}
821     } else
822 	/* Avoid a -Wcast-qual warning */
823 	dev->desc = (char *)(uintptr_t) desc;
824 
825 #ifdef DEVICE_SYSCTLS
826     {
827 	struct sysctl_oid *oid = &dev->oid[1];
828 	oid->oid_arg1 = dev->desc ? dev->desc : "";
829 	oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
830     }
831 #endif
832 }
833 
834 void
835 device_set_desc(device_t dev, const char* desc)
836 {
837     device_set_desc_internal(dev, desc, FALSE);
838 }
839 
840 void
841 device_set_desc_copy(device_t dev, const char* desc)
842 {
843     device_set_desc_internal(dev, desc, TRUE);
844 }
845 
846 void
847 device_set_flags(device_t dev, u_int32_t flags)
848 {
849     dev->devflags = flags;
850 }
851 
852 void *
853 device_get_softc(device_t dev)
854 {
855     return dev->softc;
856 }
857 
858 void
859 device_set_softc(device_t dev, void *softc)
860 {
861     if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
862 	free(dev->softc, M_BUS);
863     dev->softc = softc;
864     if (dev->softc)
865         dev->flags |= DF_EXTERNALSOFTC;
866     else
867         dev->flags &= ~DF_EXTERNALSOFTC;
868 }
869 
870 void *
871 device_get_ivars(device_t dev)
872 {
873     return dev->ivars;
874 }
875 
876 void
877 device_set_ivars(device_t dev, void * ivars)
878 {
879     if (!dev)
880 	return;
881 
882     dev->ivars = ivars;
883 
884     return;
885 }
886 
887 device_state_t
888 device_get_state(device_t dev)
889 {
890     return dev->state;
891 }
892 
893 void
894 device_enable(device_t dev)
895 {
896     dev->flags |= DF_ENABLED;
897 }
898 
899 void
900 device_disable(device_t dev)
901 {
902     dev->flags &= ~DF_ENABLED;
903 }
904 
905 void
906 device_busy(device_t dev)
907 {
908     if (dev->state < DS_ATTACHED)
909 	panic("device_busy: called for unattached device");
910     if (dev->busy == 0 && dev->parent)
911 	device_busy(dev->parent);
912     dev->busy++;
913     dev->state = DS_BUSY;
914 }
915 
916 void
917 device_unbusy(device_t dev)
918 {
919     if (dev->state != DS_BUSY)
920 	panic("device_unbusy: called for non-busy device");
921     dev->busy--;
922     if (dev->busy == 0) {
923 	if (dev->parent)
924 	    device_unbusy(dev->parent);
925 	dev->state = DS_ATTACHED;
926     }
927 }
928 
929 void
930 device_quiet(device_t dev)
931 {
932     dev->flags |= DF_QUIET;
933 }
934 
935 void
936 device_verbose(device_t dev)
937 {
938     dev->flags &= ~DF_QUIET;
939 }
940 
941 int
942 device_is_quiet(device_t dev)
943 {
944     return (dev->flags & DF_QUIET) != 0;
945 }
946 
947 int
948 device_is_enabled(device_t dev)
949 {
950     return (dev->flags & DF_ENABLED) != 0;
951 }
952 
953 int
954 device_is_alive(device_t dev)
955 {
956     return dev->state >= DS_ALIVE;
957 }
958 
959 int
960 device_set_devclass(device_t dev, const char *classname)
961 {
962     devclass_t dc;
963 
964     if (!classname) {
965 	if (dev->devclass)
966 	    devclass_delete_device(dev->devclass, dev);
967 	return 0;
968     }
969 
970     if (dev->devclass) {
971 	printf("device_set_devclass: device class already set\n");
972 	return EINVAL;
973     }
974 
975     dc = devclass_find_internal(classname, TRUE);
976     if (!dc)
977 	return ENOMEM;
978 
979     return devclass_add_device(dc, dev);
980 }
981 
982 int
983 device_set_driver(device_t dev, driver_t *driver)
984 {
985     if (dev->state >= DS_ATTACHED)
986 	return EBUSY;
987 
988     if (dev->driver == driver)
989 	return 0;
990 
991     if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
992 	free(dev->softc, M_BUS);
993 	dev->softc = NULL;
994     }
995     kobj_delete((kobj_t) dev, 0);
996     dev->driver = driver;
997     if (driver) {
998 	kobj_init((kobj_t) dev, (kobj_class_t) driver);
999 	if (!(dev->flags & DF_EXTERNALSOFTC)) {
1000 	    dev->softc = malloc(driver->size, M_BUS, M_NOWAIT);
1001 	    if (!dev->softc) {
1002 	        kobj_init((kobj_t) dev, &null_class);
1003 	        dev->driver = NULL;
1004 	        return ENOMEM;
1005 	    }
1006 	    bzero(dev->softc, driver->size);
1007 	}
1008     } else
1009 	kobj_init((kobj_t) dev, &null_class);
1010     return 0;
1011 }
1012 
1013 int
1014 device_probe_and_attach(device_t dev)
1015 {
1016     device_t bus = dev->parent;
1017     int error = 0;
1018 
1019     if (dev->state >= DS_ALIVE)
1020 	return 0;
1021 
1022     if (dev->flags & DF_ENABLED) {
1023 	error = device_probe_child(bus, dev);
1024 	if (!error) {
1025 	    if (!device_is_quiet(dev))
1026 		device_print_child(bus, dev);
1027 	    error = DEVICE_ATTACH(dev);
1028 	    if (!error)
1029 		dev->state = DS_ATTACHED;
1030 	    else {
1031 		printf("device_probe_and_attach: %s%d attach returned %d\n",
1032 		       dev->driver->name, dev->unit, error);
1033 		device_set_driver(dev, NULL);
1034 		dev->state = DS_NOTPRESENT;
1035 	    }
1036 	} else {
1037 	    if (!(dev->flags & DF_DONENOMATCH)) {
1038 		BUS_PROBE_NOMATCH(bus, dev);
1039 		dev->flags |= DF_DONENOMATCH;
1040 	    }
1041 	}
1042     } else {
1043 	if (bootverbose) {
1044 	    device_print_prettyname(dev);
1045 	    printf("not probed (disabled)\n");
1046 	}
1047     }
1048 
1049     return error;
1050 }
1051 
1052 int
1053 device_detach(device_t dev)
1054 {
1055     int error;
1056 
1057     PDEBUG(("%s", DEVICENAME(dev)));
1058     if (dev->state == DS_BUSY)
1059 	return EBUSY;
1060     if (dev->state != DS_ATTACHED)
1061 	return 0;
1062 
1063     if ((error = DEVICE_DETACH(dev)) != 0)
1064 	return error;
1065     device_printf(dev, "detached\n");
1066     if (dev->parent)
1067 	BUS_CHILD_DETACHED(dev->parent, dev);
1068 
1069     if (!(dev->flags & DF_FIXEDCLASS))
1070 	devclass_delete_device(dev->devclass, dev);
1071 
1072     dev->state = DS_NOTPRESENT;
1073     device_set_driver(dev, NULL);
1074 
1075     return 0;
1076 }
1077 
1078 int
1079 device_shutdown(device_t dev)
1080 {
1081     if (dev->state < DS_ATTACHED)
1082 	return 0;
1083     return DEVICE_SHUTDOWN(dev);
1084 }
1085 
1086 int
1087 device_set_unit(device_t dev, int unit)
1088 {
1089     devclass_t dc;
1090     int err;
1091 
1092     dc = device_get_devclass(dev);
1093     if (unit < dc->maxunit && dc->devices[unit])
1094 	return EBUSY;
1095     err = devclass_delete_device(dc, dev);
1096     if (err)
1097 	return err;
1098     dev->unit = unit;
1099     err = devclass_add_device(dc, dev);
1100     if (err)
1101 	return err;
1102     return 0;
1103 }
1104 
1105 #ifdef DEVICE_SYSCTLS
1106 
1107 /*
1108  * Sysctl nodes for devices.
1109  */
1110 
1111 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
1112 
1113 static int
1114 sysctl_handle_children(SYSCTL_HANDLER_ARGS)
1115 {
1116     device_t dev = arg1;
1117     device_t child;
1118     int first = 1, error = 0;
1119 
1120     for (child = TAILQ_FIRST(&dev->children); child;
1121 	 child = TAILQ_NEXT(child, link)) {
1122 	if (child->nameunit) {
1123 	    if (!first) {
1124 		error = SYSCTL_OUT(req, ",", 1);
1125 		if (error) return error;
1126 	    } else {
1127 		first = 0;
1128 	    }
1129 	    error = SYSCTL_OUT(req, child->nameunit, strlen(child->nameunit));
1130 	    if (error) return error;
1131 	}
1132     }
1133 
1134     error = SYSCTL_OUT(req, "", 1);
1135 
1136     return error;
1137 }
1138 
1139 static int
1140 sysctl_handle_state(SYSCTL_HANDLER_ARGS)
1141 {
1142     device_t dev = arg1;
1143 
1144     switch (dev->state) {
1145     case DS_NOTPRESENT:
1146 	return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
1147     case DS_ALIVE:
1148 	return SYSCTL_OUT(req, "alive", sizeof("alive"));
1149     case DS_ATTACHED:
1150 	return SYSCTL_OUT(req, "attached", sizeof("attached"));
1151     case DS_BUSY:
1152 	return SYSCTL_OUT(req, "busy", sizeof("busy"));
1153     }
1154 
1155     return 0;
1156 }
1157 
1158 static void
1159 device_register_oids(device_t dev)
1160 {
1161     struct sysctl_oid* oid;
1162 
1163     oid = &dev->oid[0];
1164     bzero(oid, sizeof(*oid));
1165     oid->oid_parent = &sysctl__hw_devices_children;
1166     oid->oid_number = OID_AUTO;
1167     oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
1168     oid->oid_arg1 = &dev->oidlist[0];
1169     oid->oid_arg2 = 0;
1170     oid->oid_name = dev->nameunit;
1171     oid->oid_handler = 0;
1172     oid->oid_fmt = "N";
1173     SLIST_INIT(&dev->oidlist[0]);
1174     sysctl_register_oid(oid);
1175 
1176     oid = &dev->oid[1];
1177     bzero(oid, sizeof(*oid));
1178     oid->oid_parent = &dev->oidlist[0];
1179     oid->oid_number = OID_AUTO;
1180     oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
1181     oid->oid_arg1 = dev->desc ? dev->desc : "";
1182     oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
1183     oid->oid_name = "desc";
1184     oid->oid_handler = sysctl_handle_string;
1185     oid->oid_fmt = "A";
1186     sysctl_register_oid(oid);
1187 
1188     oid = &dev->oid[2];
1189     bzero(oid, sizeof(*oid));
1190     oid->oid_parent = &dev->oidlist[0];
1191     oid->oid_number = OID_AUTO;
1192     oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1193     oid->oid_arg1 = dev;
1194     oid->oid_arg2 = 0;
1195     oid->oid_name = "children";
1196     oid->oid_handler = sysctl_handle_children;
1197     oid->oid_fmt = "A";
1198     sysctl_register_oid(oid);
1199 
1200     oid = &dev->oid[3];
1201     bzero(oid, sizeof(*oid));
1202     oid->oid_parent = &dev->oidlist[0];
1203     oid->oid_number = OID_AUTO;
1204     oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1205     oid->oid_arg1 = dev;
1206     oid->oid_arg2 = 0;
1207     oid->oid_name = "state";
1208     oid->oid_handler = sysctl_handle_state;
1209     oid->oid_fmt = "A";
1210     sysctl_register_oid(oid);
1211 }
1212 
1213 static void
1214 device_unregister_oids(device_t dev)
1215 {
1216     sysctl_unregister_oid(&dev->oid[0]);
1217     sysctl_unregister_oid(&dev->oid[1]);
1218     sysctl_unregister_oid(&dev->oid[2]);
1219 }
1220 
1221 #endif
1222 
1223 /*======================================*/
1224 /*
1225  * Access functions for device resources.
1226  */
1227 
1228 /* Runtime version */
1229 static struct config_device *devtab;
1230 static int devtab_count = 0;
1231 
1232 static int
1233 resource_new_name(const char *name, int unit)
1234 {
1235 	struct config_device *new;
1236 
1237 	new = malloc((devtab_count + 1) * sizeof(*new), M_TEMP, M_NOWAIT);
1238 	if (new == NULL)
1239 		return -1;
1240 	if (devtab && devtab_count > 0)
1241 		bcopy(devtab, new, devtab_count * sizeof(*new));
1242 	bzero(&new[devtab_count], sizeof(*new));
1243 	new[devtab_count].name = malloc(strlen(name) + 1, M_TEMP, M_NOWAIT);
1244 	if (new[devtab_count].name == NULL) {
1245 		free(new, M_TEMP);
1246 		return -1;
1247 	}
1248 	strcpy(new[devtab_count].name, name);
1249 	new[devtab_count].unit = unit;
1250 	new[devtab_count].resource_count = 0;
1251 	new[devtab_count].resources = NULL;
1252 	devtab = new;
1253 	return devtab_count++;
1254 }
1255 
1256 static int
1257 resource_new_resname(int j, const char *resname, resource_type type)
1258 {
1259 	struct config_resource *new;
1260 	int i;
1261 
1262 	i = devtab[j].resource_count;
1263 	new = malloc((i + 1) * sizeof(*new), M_TEMP, M_NOWAIT);
1264 	if (new == NULL)
1265 		return -1;
1266 	if (devtab[j].resources && i > 0)
1267 		bcopy(devtab[j].resources, new, i * sizeof(*new));
1268 	bzero(&new[i], sizeof(*new));
1269 	new[i].name = malloc(strlen(resname) + 1, M_TEMP, M_NOWAIT);
1270 	if (new[i].name == NULL) {
1271 		free(new, M_TEMP);
1272 		return -1;
1273 	}
1274 	strcpy(new[i].name, resname);
1275 	new[i].type = type;
1276 	if (devtab[j].resources)
1277 		free(devtab[j].resources, M_TEMP);
1278 	devtab[j].resources = new;
1279 	devtab[j].resource_count = i + 1;
1280 	return i;
1281 }
1282 
1283 static int
1284 resource_match_string(int i, const char *resname, const char *value)
1285 {
1286 	int j;
1287 	struct config_resource *res;
1288 
1289 	for (j = 0, res = devtab[i].resources;
1290 	     j < devtab[i].resource_count; j++, res++)
1291 		if (!strcmp(res->name, resname)
1292 		    && res->type == RES_STRING
1293 		    && !strcmp(res->u.stringval, value))
1294 			return j;
1295 	return -1;
1296 }
1297 
1298 static int
1299 resource_find_hard(const char *cp, const char *name, int unit,
1300 		   const char *resname, struct config_resource **result)
1301 {
1302 	char match[256];
1303 	int matchlen;
1304 	char *op;
1305 	long val;
1306 
1307 	snprintf(match, sizeof(match), "hint.%s.%d.%s=", name, unit, resname);
1308 	matchlen = strlen(match);
1309 	while (cp) {
1310 		if (strncmp(match, cp, matchlen) == 0)
1311 			break;
1312 		while (*cp != '\0')
1313 			cp++;
1314 		cp++;
1315 		if (*cp == '\0') {
1316 			cp = NULL;
1317 			break;
1318 		}
1319 	}
1320 	if (cp)
1321 		cp += matchlen;		/* skip over name and '=' */
1322 	else
1323 		return ENOENT;
1324 	val = strtoul(cp, &op, 0);
1325 	if (*cp != '\0' && *op == '\0') {
1326 		(*result)->type = RES_INT;
1327 		(*result)->u.intval = val;
1328 	} else {
1329 		(*result)->type = RES_STRING;
1330 		(*result)->u.stringval = cp;
1331 	}
1332 	return 0;
1333 }
1334 
1335 static int
1336 resource_find(const char *name, int unit, const char *resname,
1337 	      struct config_resource **result)
1338 {
1339 	int i, j;
1340 	struct config_resource *res;
1341 
1342 	if (!hints_loaded) {
1343 		/* First specific, then generic. Dynamic over static. */
1344 		i = resource_find_hard(kern_envp, name, unit, resname, result);
1345 		if (i == 0)
1346 			return 0;
1347 		i = resource_find_hard(static_hints, name, unit, resname,
1348 				       result);
1349 		if (i == 0)
1350 			return 0;
1351 		i = resource_find_hard(kern_envp, name, -1, resname, result);
1352 		if (i == 0)
1353 			return 0;
1354 		i = resource_find_hard(static_hints, name, -1, resname, result);
1355 		return i;
1356 	}
1357 
1358 	/*
1359 	 * First check specific instances, then generic.
1360 	 */
1361 	for (i = 0; i < devtab_count; i++) {
1362 		if (devtab[i].unit < 0)
1363 			continue;
1364 		if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1365 			res = devtab[i].resources;
1366 			for (j = 0; j < devtab[i].resource_count; j++, res++)
1367 				if (!strcmp(res->name, resname)) {
1368 					*result = res;
1369 					return 0;
1370 				}
1371 		}
1372 	}
1373 	for (i = 0; i < devtab_count; i++) {
1374 		if (devtab[i].unit >= 0)
1375 			continue;
1376 		if (!strcmp(devtab[i].name, name)) {
1377 			res = devtab[i].resources;
1378 			for (j = 0; j < devtab[i].resource_count; j++, res++)
1379 				if (!strcmp(res->name, resname)) {
1380 					*result = res;
1381 					return 0;
1382 				}
1383 		}
1384 	}
1385 	return ENOENT;
1386 }
1387 
1388 int
1389 resource_int_value(const char *name, int unit, const char *resname, int *result)
1390 {
1391 	struct config_resource tmpres;
1392 	struct config_resource *res;
1393 	int error;
1394 
1395 	res = &tmpres;
1396 	if ((error = resource_find(name, unit, resname, &res)) != 0)
1397 		return error;
1398 	if (res->type != RES_INT)
1399 		return EFTYPE;
1400 	*result = res->u.intval;
1401 	return 0;
1402 }
1403 
1404 int
1405 resource_long_value(const char *name, int unit, const char *resname,
1406 		    long *result)
1407 {
1408 	struct config_resource tmpres;
1409 	struct config_resource *res;
1410 	int error;
1411 
1412 	res = &tmpres;
1413 	if ((error = resource_find(name, unit, resname, &res)) != 0)
1414 		return error;
1415 	if (res->type != RES_LONG)
1416 		return EFTYPE;
1417 	*result = res->u.longval;
1418 	return 0;
1419 }
1420 
1421 int
1422 resource_string_value(const char *name, int unit, const char *resname,
1423 		      char **result)
1424 {
1425 	struct config_resource tmpres;
1426 	struct config_resource *res;
1427 	int error;
1428 
1429 	res = &tmpres;
1430 	if ((error = resource_find(name, unit, resname, &res)) != 0)
1431 		return error;
1432 	if (res->type != RES_STRING)
1433 		return EFTYPE;
1434 	*result = res->u.stringval;
1435 	return 0;
1436 }
1437 
1438 int
1439 resource_query_string(int i, const char *resname, const char *value)
1440 {
1441 	if (i < 0)
1442 		i = 0;
1443 	else
1444 		i = i + 1;
1445 	for (; i < devtab_count; i++)
1446 		if (resource_match_string(i, resname, value) >= 0)
1447 			return i;
1448 	return -1;
1449 }
1450 
1451 int
1452 resource_locate(int i, const char *resname)
1453 {
1454 	if (i < 0)
1455 		i = 0;
1456 	else
1457 		i = i + 1;
1458 	for (; i < devtab_count; i++)
1459 		if (!strcmp(devtab[i].name, resname))
1460 			return i;
1461 	return -1;
1462 }
1463 
1464 int
1465 resource_count(void)
1466 {
1467 	return devtab_count;
1468 }
1469 
1470 char *
1471 resource_query_name(int i)
1472 {
1473 	return devtab[i].name;
1474 }
1475 
1476 int
1477 resource_query_unit(int i)
1478 {
1479 	return devtab[i].unit;
1480 }
1481 
1482 static int
1483 resource_create(const char *name, int unit, const char *resname,
1484 		resource_type type, struct config_resource **result)
1485 {
1486 	int i, j;
1487 	struct config_resource *res = NULL;
1488 
1489 	for (i = 0; i < devtab_count; i++) {
1490 		if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1491 			res = devtab[i].resources;
1492 			break;
1493 		}
1494 	}
1495 	if (res == NULL) {
1496 		i = resource_new_name(name, unit);
1497 		if (i < 0)
1498 			return ENOMEM;
1499 		res = devtab[i].resources;
1500 	}
1501 	for (j = 0; j < devtab[i].resource_count; j++, res++) {
1502 		if (!strcmp(res->name, resname)) {
1503 			*result = res;
1504 			return 0;
1505 		}
1506 	}
1507 	j = resource_new_resname(i, resname, type);
1508 	if (j < 0)
1509 		return ENOMEM;
1510 	res = &devtab[i].resources[j];
1511 	*result = res;
1512 	return 0;
1513 }
1514 
1515 int
1516 resource_set_int(const char *name, int unit, const char *resname, int value)
1517 {
1518 	int error;
1519 	struct config_resource *res;
1520 
1521 	error = resource_create(name, unit, resname, RES_INT, &res);
1522 	if (error)
1523 		return error;
1524 	if (res->type != RES_INT)
1525 		return EFTYPE;
1526 	res->u.intval = value;
1527 	return 0;
1528 }
1529 
1530 int
1531 resource_set_long(const char *name, int unit, const char *resname, long value)
1532 {
1533 	int error;
1534 	struct config_resource *res;
1535 
1536 	error = resource_create(name, unit, resname, RES_LONG, &res);
1537 	if (error)
1538 		return error;
1539 	if (res->type != RES_LONG)
1540 		return EFTYPE;
1541 	res->u.longval = value;
1542 	return 0;
1543 }
1544 
1545 int
1546 resource_set_string(const char *name, int unit, const char *resname,
1547 		    const char *value)
1548 {
1549 	int error;
1550 	struct config_resource *res;
1551 
1552 	error = resource_create(name, unit, resname, RES_STRING, &res);
1553 	if (error)
1554 		return error;
1555 	if (res->type != RES_STRING)
1556 		return EFTYPE;
1557 	if (res->u.stringval)
1558 		free(res->u.stringval, M_TEMP);
1559 	res->u.stringval = malloc(strlen(value) + 1, M_TEMP, M_NOWAIT);
1560 	if (res->u.stringval == NULL)
1561 		return ENOMEM;
1562 	strcpy(res->u.stringval, value);
1563 	return 0;
1564 }
1565 
1566 /*
1567  * We use the identify routine to get the hints for all the other devices.
1568  * Strings that are all digits or begin with 0x are integers.
1569  *
1570  * hint.aha.0.bus_speedup=1
1571  * hint.aha.1.irq=10
1572  * hint.wl.0.netid=PLUG
1573  * hint.wl.1.netid=XYZZY
1574  */
1575 static void
1576 hint_load(char *cp)
1577 {
1578 	char	*ep, *op, *walker;
1579 	int	len;
1580 	int	val;
1581 	char	name[20];
1582 	int	unit;
1583 	char	resname[255];
1584 
1585 	for (ep = cp; *ep != '=' && *ep != '\0'; ep++)
1586 		;
1587 	len = ep - cp;
1588 	if (*ep == '=')
1589 		ep++;
1590 	if (strncmp(cp, "hint.", 5) != 0)
1591 		return;
1592 	walker = cp;
1593 	walker += 5;
1594 	op = walker;
1595 	while (*walker && *walker != '.')
1596 		walker++;
1597 	if (*walker != '.')
1598 		return;
1599 	if (walker - op > sizeof(name))
1600 		return;
1601 	strncpy(name, op, walker - op);
1602 	name[walker - op] = '\0';
1603 	walker++;
1604 	op = walker;
1605 	while (*walker && *walker != '.')
1606 		walker++;
1607 	if (*walker != '.')
1608 		return;
1609 	unit = strtoul(op, &walker, 0);
1610 	if (*walker != '.')
1611 		return;
1612 	walker++;
1613 	op = walker;
1614 	while (*walker && *walker != '=')
1615 		walker++;
1616 	if (*walker != '=')
1617 		return;
1618 	if (walker - op > sizeof(resname))
1619 		return;
1620 	strncpy(resname, op, walker - op);
1621 	resname[walker - op] = '\0';
1622 	walker++;
1623 	if (walker != ep)
1624 		return;
1625 	if (bootverbose)
1626 		printf("Setting %s %d %s to ", name, unit, resname);
1627 	val = strtoul(ep, &op, 0);
1628 	if (*ep != '\0' && *op == '\0') {
1629 		resource_set_int(name, unit, resname, val);
1630 		if (bootverbose)
1631 			printf("%d (int)\n", val);
1632 	} else {
1633 		resource_set_string(name, unit, resname, ep);
1634 		if (bootverbose)
1635 			printf("%s (string)\n", ep);
1636 	}
1637 }
1638 
1639 
1640 static void
1641 hints_load(void *dummy __unused)
1642 {
1643 	char	*cp;
1644 
1645 	cp = static_hints;
1646 	while (cp) {
1647 		hint_load(cp);
1648 		while (*cp != '\0')
1649 			cp++;
1650 		cp++;
1651 		if (*cp == '\0')
1652 			break;
1653 	}
1654 	cp = kern_envp;
1655 	while (cp) {
1656 		hint_load(cp);
1657 		while (*cp != '\0')
1658 			cp++;
1659 		cp++;
1660 		if (*cp == '\0')
1661 			break;
1662 	}
1663 	hints_loaded++;
1664 }
1665 SYSINIT(cfghints, SI_SUB_KMEM, SI_ORDER_ANY + 60, hints_load, 0)
1666 
1667 /*======================================*/
1668 /*
1669  * Some useful method implementations to make life easier for bus drivers.
1670  */
1671 
1672 void
1673 resource_list_init(struct resource_list *rl)
1674 {
1675 	SLIST_INIT(rl);
1676 }
1677 
1678 void
1679 resource_list_free(struct resource_list *rl)
1680 {
1681     struct resource_list_entry *rle;
1682 
1683     while ((rle = SLIST_FIRST(rl)) != NULL) {
1684 	if (rle->res)
1685 	    panic("resource_list_free: resource entry is busy");
1686 	SLIST_REMOVE_HEAD(rl, link);
1687 	free(rle, M_BUS);
1688     }
1689 }
1690 
1691 void
1692 resource_list_add(struct resource_list *rl,
1693 		  int type, int rid,
1694 		  u_long start, u_long end, u_long count)
1695 {
1696     struct resource_list_entry *rle;
1697 
1698     rle = resource_list_find(rl, type, rid);
1699     if (!rle) {
1700 	rle = malloc(sizeof(struct resource_list_entry), M_BUS, M_NOWAIT);
1701 	if (!rle)
1702 	    panic("resource_list_add: can't record entry");
1703 	SLIST_INSERT_HEAD(rl, rle, link);
1704 	rle->type = type;
1705 	rle->rid = rid;
1706 	rle->res = NULL;
1707     }
1708 
1709     if (rle->res)
1710 	panic("resource_list_add: resource entry is busy");
1711 
1712     rle->start = start;
1713     rle->end = end;
1714     rle->count = count;
1715 }
1716 
1717 struct resource_list_entry*
1718 resource_list_find(struct resource_list *rl,
1719 		   int type, int rid)
1720 {
1721     struct resource_list_entry *rle;
1722 
1723     SLIST_FOREACH(rle, rl, link)
1724 	if (rle->type == type && rle->rid == rid)
1725 	    return rle;
1726     return NULL;
1727 }
1728 
1729 void
1730 resource_list_delete(struct resource_list *rl,
1731 		     int type, int rid)
1732 {
1733     struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1734 
1735     if (rle) {
1736 	SLIST_REMOVE(rl, rle, resource_list_entry, link);
1737 	free(rle, M_BUS);
1738     }
1739 }
1740 
1741 struct resource *
1742 resource_list_alloc(struct resource_list *rl,
1743 		    device_t bus, device_t child,
1744 		    int type, int *rid,
1745 		    u_long start, u_long end,
1746 		    u_long count, u_int flags)
1747 {
1748     struct resource_list_entry *rle = 0;
1749     int passthrough = (device_get_parent(child) != bus);
1750     int isdefault = (start == 0UL && end == ~0UL);
1751 
1752     if (passthrough) {
1753 	return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1754 				  type, rid,
1755 				  start, end, count, flags);
1756     }
1757 
1758     rle = resource_list_find(rl, type, *rid);
1759 
1760     if (!rle)
1761 	return 0;		/* no resource of that type/rid */
1762     if (rle->res)
1763 	panic("resource_list_alloc: resource entry is busy");
1764 
1765     if (isdefault) {
1766 	start = rle->start;
1767 	count = max(count, rle->count);
1768 	end = max(rle->end, start + count - 1);
1769     }
1770 
1771     rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1772 				  type, rid, start, end, count, flags);
1773 
1774     /*
1775      * Record the new range.
1776      */
1777     if (rle->res) {
1778 	    rle->start = rman_get_start(rle->res);
1779 	    rle->end = rman_get_end(rle->res);
1780 	    rle->count = count;
1781     }
1782 
1783     return rle->res;
1784 }
1785 
1786 int
1787 resource_list_release(struct resource_list *rl,
1788 		      device_t bus, device_t child,
1789 		      int type, int rid, struct resource *res)
1790 {
1791     struct resource_list_entry *rle = 0;
1792     int passthrough = (device_get_parent(child) != bus);
1793     int error;
1794 
1795     if (passthrough) {
1796 	return BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1797 				    type, rid, res);
1798     }
1799 
1800     rle = resource_list_find(rl, type, rid);
1801 
1802     if (!rle)
1803 	panic("resource_list_release: can't find resource");
1804     if (!rle->res)
1805 	panic("resource_list_release: resource entry is not busy");
1806 
1807     error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1808 				 type, rid, res);
1809     if (error)
1810 	return error;
1811 
1812     rle->res = NULL;
1813     return 0;
1814 }
1815 
1816 /*
1817  * Call DEVICE_IDENTIFY for each driver.
1818  */
1819 int
1820 bus_generic_probe(device_t dev)
1821 {
1822     devclass_t dc = dev->devclass;
1823     driverlink_t dl;
1824 
1825     for (dl = TAILQ_FIRST(&dc->drivers); dl; dl = TAILQ_NEXT(dl, link))
1826 	DEVICE_IDENTIFY(dl->driver, dev);
1827 
1828     return 0;
1829 }
1830 
1831 int
1832 bus_generic_attach(device_t dev)
1833 {
1834     device_t child;
1835 
1836     for (child = TAILQ_FIRST(&dev->children);
1837 	 child; child = TAILQ_NEXT(child, link))
1838 	device_probe_and_attach(child);
1839 
1840     return 0;
1841 }
1842 
1843 int
1844 bus_generic_detach(device_t dev)
1845 {
1846     device_t child;
1847     int error;
1848 
1849     if (dev->state != DS_ATTACHED)
1850 	return EBUSY;
1851 
1852     for (child = TAILQ_FIRST(&dev->children);
1853 	 child; child = TAILQ_NEXT(child, link))
1854 	if ((error = device_detach(child)) != 0)
1855 	    return error;
1856 
1857     return 0;
1858 }
1859 
1860 int
1861 bus_generic_shutdown(device_t dev)
1862 {
1863     device_t child;
1864 
1865     for (child = TAILQ_FIRST(&dev->children);
1866 	 child; child = TAILQ_NEXT(child, link))
1867 	device_shutdown(child);
1868 
1869     return 0;
1870 }
1871 
1872 int
1873 bus_generic_suspend(device_t dev)
1874 {
1875 	int		error;
1876 	device_t	child, child2;
1877 
1878 	for (child = TAILQ_FIRST(&dev->children);
1879 	     child; child = TAILQ_NEXT(child, link)) {
1880 		error = DEVICE_SUSPEND(child);
1881 		if (error) {
1882 			for (child2 = TAILQ_FIRST(&dev->children);
1883 			     child2 && child2 != child;
1884 			     child2 = TAILQ_NEXT(child2, link))
1885 				DEVICE_RESUME(child2);
1886 			return (error);
1887 		}
1888 	}
1889 	return 0;
1890 }
1891 
1892 int
1893 bus_generic_resume(device_t dev)
1894 {
1895 	device_t	child;
1896 
1897 	for (child = TAILQ_FIRST(&dev->children);
1898 	     child; child = TAILQ_NEXT(child, link)) {
1899 		DEVICE_RESUME(child);
1900 		/* if resume fails, there's nothing we can usefully do... */
1901 	}
1902 	return 0;
1903 }
1904 
1905 int
1906 bus_print_child_header (device_t dev, device_t child)
1907 {
1908 	int	retval = 0;
1909 
1910 	if (device_get_desc(child)) {
1911 		retval += device_printf(child, "<%s>",
1912 				       device_get_desc(child));
1913 	} else {
1914 		retval += printf("%s", device_get_nameunit(child));
1915 	}
1916 
1917 	return (retval);
1918 }
1919 
1920 int
1921 bus_print_child_footer (device_t dev, device_t child)
1922 {
1923 	return(printf(" on %s\n", device_get_nameunit(dev)));
1924 }
1925 
1926 int
1927 bus_generic_print_child(device_t dev, device_t child)
1928 {
1929 	int	retval = 0;
1930 
1931 	retval += bus_print_child_header(dev, child);
1932 	retval += bus_print_child_footer(dev, child);
1933 
1934 	return (retval);
1935 }
1936 
1937 int
1938 bus_generic_read_ivar(device_t dev, device_t child, int index,
1939 		      uintptr_t * result)
1940 {
1941     return ENOENT;
1942 }
1943 
1944 int
1945 bus_generic_write_ivar(device_t dev, device_t child, int index,
1946 		       uintptr_t value)
1947 {
1948     return ENOENT;
1949 }
1950 
1951 void
1952 bus_generic_driver_added(device_t dev, driver_t *driver)
1953 {
1954     device_t child;
1955 
1956     DEVICE_IDENTIFY(driver, dev);
1957     for (child = TAILQ_FIRST(&dev->children);
1958 	 child; child = TAILQ_NEXT(child, link))
1959 	if (child->state == DS_NOTPRESENT)
1960 	    device_probe_and_attach(child);
1961 }
1962 
1963 int
1964 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
1965 		       int flags, driver_intr_t *intr, void *arg,
1966 		       void **cookiep)
1967 {
1968 	/* Propagate up the bus hierarchy until someone handles it. */
1969 	if (dev->parent)
1970 		return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
1971 				       intr, arg, cookiep));
1972 	else
1973 		return (EINVAL);
1974 }
1975 
1976 int
1977 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
1978 			  void *cookie)
1979 {
1980 	/* Propagate up the bus hierarchy until someone handles it. */
1981 	if (dev->parent)
1982 		return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
1983 	else
1984 		return (EINVAL);
1985 }
1986 
1987 struct resource *
1988 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
1989 			   u_long start, u_long end, u_long count, u_int flags)
1990 {
1991 	/* Propagate up the bus hierarchy until someone handles it. */
1992 	if (dev->parent)
1993 		return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
1994 					   start, end, count, flags));
1995 	else
1996 		return (NULL);
1997 }
1998 
1999 int
2000 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2001 			     struct resource *r)
2002 {
2003 	/* Propagate up the bus hierarchy until someone handles it. */
2004 	if (dev->parent)
2005 		return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
2006 					     r));
2007 	else
2008 		return (EINVAL);
2009 }
2010 
2011 int
2012 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2013 			      struct resource *r)
2014 {
2015 	/* Propagate up the bus hierarchy until someone handles it. */
2016 	if (dev->parent)
2017 		return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
2018 					      r));
2019 	else
2020 		return (EINVAL);
2021 }
2022 
2023 int
2024 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2025 				int rid, struct resource *r)
2026 {
2027 	/* Propagate up the bus hierarchy until someone handles it. */
2028 	if (dev->parent)
2029 		return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2030 						r));
2031 	else
2032 		return (EINVAL);
2033 }
2034 
2035 /*
2036  * Some convenience functions to make it easier for drivers to use the
2037  * resource-management functions.  All these really do is hide the
2038  * indirection through the parent's method table, making for slightly
2039  * less-wordy code.  In the future, it might make sense for this code
2040  * to maintain some sort of a list of resources allocated by each device.
2041  */
2042 struct resource *
2043 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2044 		   u_long count, u_int flags)
2045 {
2046 	if (dev->parent == 0)
2047 		return (0);
2048 	return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2049 				   count, flags));
2050 }
2051 
2052 int
2053 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2054 {
2055 	if (dev->parent == 0)
2056 		return (EINVAL);
2057 	return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2058 }
2059 
2060 int
2061 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2062 {
2063 	if (dev->parent == 0)
2064 		return (EINVAL);
2065 	return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2066 }
2067 
2068 int
2069 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2070 {
2071 	if (dev->parent == 0)
2072 		return (EINVAL);
2073 	return (BUS_RELEASE_RESOURCE(dev->parent, dev,
2074 				     type, rid, r));
2075 }
2076 
2077 int
2078 bus_setup_intr(device_t dev, struct resource *r, int flags,
2079 	       driver_intr_t handler, void *arg, void **cookiep)
2080 {
2081 	if (dev->parent == 0)
2082 		return (EINVAL);
2083 	return (BUS_SETUP_INTR(dev->parent, dev, r, flags,
2084 			       handler, arg, cookiep));
2085 }
2086 
2087 int
2088 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2089 {
2090 	if (dev->parent == 0)
2091 		return (EINVAL);
2092 	return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2093 }
2094 
2095 int
2096 bus_set_resource(device_t dev, int type, int rid,
2097 		 u_long start, u_long count)
2098 {
2099 	return BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2100 				start, count);
2101 }
2102 
2103 int
2104 bus_get_resource(device_t dev, int type, int rid,
2105 		 u_long *startp, u_long *countp)
2106 {
2107 	return BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2108 				startp, countp);
2109 }
2110 
2111 u_long
2112 bus_get_resource_start(device_t dev, int type, int rid)
2113 {
2114 	u_long start, count;
2115 	int error;
2116 
2117 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2118 				 &start, &count);
2119 	if (error)
2120 		return 0;
2121 	return start;
2122 }
2123 
2124 u_long
2125 bus_get_resource_count(device_t dev, int type, int rid)
2126 {
2127 	u_long start, count;
2128 	int error;
2129 
2130 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2131 				 &start, &count);
2132 	if (error)
2133 		return 0;
2134 	return count;
2135 }
2136 
2137 void
2138 bus_delete_resource(device_t dev, int type, int rid)
2139 {
2140 	BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2141 }
2142 
2143 static int
2144 root_print_child(device_t dev, device_t child)
2145 {
2146 	return (0);
2147 }
2148 
2149 static int
2150 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2151 		void **cookiep)
2152 {
2153 	/*
2154 	 * If an interrupt mapping gets to here something bad has happened.
2155 	 */
2156 	panic("root_setup_intr");
2157 }
2158 
2159 static kobj_method_t root_methods[] = {
2160 	/* Device interface */
2161 	KOBJMETHOD(device_shutdown,	bus_generic_shutdown),
2162 	KOBJMETHOD(device_suspend,	bus_generic_suspend),
2163 	KOBJMETHOD(device_resume,	bus_generic_resume),
2164 
2165 	/* Bus interface */
2166 	KOBJMETHOD(bus_print_child,	root_print_child),
2167 	KOBJMETHOD(bus_read_ivar,	bus_generic_read_ivar),
2168 	KOBJMETHOD(bus_write_ivar,	bus_generic_write_ivar),
2169 	KOBJMETHOD(bus_setup_intr,	root_setup_intr),
2170 
2171 	{ 0, 0 }
2172 };
2173 
2174 static driver_t root_driver = {
2175 	"root",
2176 	root_methods,
2177 	1,			/* no softc */
2178 };
2179 
2180 device_t	root_bus;
2181 devclass_t	root_devclass;
2182 
2183 static int
2184 root_bus_module_handler(module_t mod, int what, void* arg)
2185 {
2186     switch (what) {
2187     case MOD_LOAD:
2188 	kobj_class_compile((kobj_class_t) &root_driver);
2189 	root_bus = make_device(NULL, "root", 0);
2190 	root_bus->desc = "System root bus";
2191 	kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
2192 	root_bus->driver = &root_driver;
2193 	root_bus->state = DS_ATTACHED;
2194 	root_devclass = devclass_find_internal("root", FALSE);
2195 	return 0;
2196 
2197     case MOD_SHUTDOWN:
2198 	device_shutdown(root_bus);
2199 	return 0;
2200     }
2201 
2202     return 0;
2203 }
2204 
2205 static moduledata_t root_bus_mod = {
2206 	"rootbus",
2207 	root_bus_module_handler,
2208 	0
2209 };
2210 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2211 
2212 void
2213 root_bus_configure(void)
2214 {
2215     device_t dev;
2216 
2217     PDEBUG(("."));
2218 
2219     for (dev = TAILQ_FIRST(&root_bus->children); dev;
2220 	 dev = TAILQ_NEXT(dev, link)) {
2221 	device_probe_and_attach(dev);
2222     }
2223 }
2224 
2225 int
2226 driver_module_handler(module_t mod, int what, void *arg)
2227 {
2228 	int error, i;
2229 	struct driver_module_data *dmd;
2230 	devclass_t bus_devclass;
2231 
2232 	dmd = (struct driver_module_data *)arg;
2233 	bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE);
2234 	error = 0;
2235 
2236 	switch (what) {
2237 	case MOD_LOAD:
2238 		if (dmd->dmd_chainevh)
2239 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2240 
2241 		for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
2242 			PDEBUG(("Loading module: driver %s on bus %s",
2243 				DRIVERNAME(dmd->dmd_drivers[i]),
2244 				dmd->dmd_busname));
2245 			error = devclass_add_driver(bus_devclass,
2246 						    dmd->dmd_drivers[i]);
2247 		}
2248 		if (error)
2249 			break;
2250 
2251 		/*
2252 		 * The drivers loaded in this way are assumed to all
2253 		 * implement the same devclass.
2254 		 */
2255 		*dmd->dmd_devclass =
2256 			devclass_find_internal(dmd->dmd_drivers[0]->name,
2257 					       TRUE);
2258 		break;
2259 
2260 	case MOD_UNLOAD:
2261 		for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
2262 			PDEBUG(("Unloading module: driver %s from bus %s",
2263 				DRIVERNAME(dmd->dmd_drivers[i]),
2264 				dmd->dmd_busname));
2265 			error = devclass_delete_driver(bus_devclass,
2266 						       dmd->dmd_drivers[i]);
2267 		}
2268 
2269 		if (!error && dmd->dmd_chainevh)
2270 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2271 		break;
2272 	}
2273 
2274 	return (error);
2275 }
2276 
2277 #ifdef BUS_DEBUG
2278 
2279 /* the _short versions avoid iteration by not calling anything that prints
2280  * more than oneliners. I love oneliners.
2281  */
2282 
2283 static void
2284 print_device_short(device_t dev, int indent)
2285 {
2286 	if (!dev)
2287 		return;
2288 
2289 	indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2290 		dev->unit, dev->desc,
2291 		(dev->parent? "":"no "),
2292 		(TAILQ_EMPTY(&dev->children)? "no ":""),
2293 		(dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2294 		(dev->flags&DF_FIXEDCLASS? "fixed,":""),
2295 		(dev->flags&DF_WILDCARD? "wildcard,":""),
2296 		(dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2297 		(dev->ivars? "":"no "),
2298 		(dev->softc? "":"no "),
2299 		dev->busy));
2300 }
2301 
2302 static void
2303 print_device(device_t dev, int indent)
2304 {
2305 	if (!dev)
2306 		return;
2307 
2308 	print_device_short(dev, indent);
2309 
2310 	indentprintf(("Parent:\n"));
2311 	print_device_short(dev->parent, indent+1);
2312 	indentprintf(("Driver:\n"));
2313 	print_driver_short(dev->driver, indent+1);
2314 	indentprintf(("Devclass:\n"));
2315 	print_devclass_short(dev->devclass, indent+1);
2316 }
2317 
2318 void
2319 print_device_tree_short(device_t dev, int indent)
2320 /* print the device and all its children (indented) */
2321 {
2322 	device_t child;
2323 
2324 	if (!dev)
2325 		return;
2326 
2327 	print_device_short(dev, indent);
2328 
2329 	for (child = TAILQ_FIRST(&dev->children); child;
2330 		 child = TAILQ_NEXT(child, link))
2331 		print_device_tree_short(child, indent+1);
2332 }
2333 
2334 void
2335 print_device_tree(device_t dev, int indent)
2336 /* print the device and all its children (indented) */
2337 {
2338 	device_t child;
2339 
2340 	if (!dev)
2341 		return;
2342 
2343 	print_device(dev, indent);
2344 
2345 	for (child = TAILQ_FIRST(&dev->children); child;
2346 		 child = TAILQ_NEXT(child, link))
2347 		print_device_tree(child, indent+1);
2348 }
2349 
2350 static void
2351 print_driver_short(driver_t *driver, int indent)
2352 {
2353 	if (!driver)
2354 		return;
2355 
2356 	indentprintf(("driver %s: softc size = %d\n",
2357 		driver->name, driver->size));
2358 }
2359 
2360 static void
2361 print_driver(driver_t *driver, int indent)
2362 {
2363 	if (!driver)
2364 		return;
2365 
2366 	print_driver_short(driver, indent);
2367 }
2368 
2369 
2370 static void
2371 print_driver_list(driver_list_t drivers, int indent)
2372 {
2373 	driverlink_t driver;
2374 
2375 	for (driver = TAILQ_FIRST(&drivers); driver;
2376 	     driver = TAILQ_NEXT(driver, link))
2377 		print_driver(driver->driver, indent);
2378 }
2379 
2380 static void
2381 print_devclass_short(devclass_t dc, int indent)
2382 {
2383 	if ( !dc )
2384 		return;
2385 
2386 	indentprintf(("devclass %s: max units = %d\n",
2387 		dc->name, dc->maxunit));
2388 }
2389 
2390 static void
2391 print_devclass(devclass_t dc, int indent)
2392 {
2393 	int i;
2394 
2395 	if ( !dc )
2396 		return;
2397 
2398 	print_devclass_short(dc, indent);
2399 	indentprintf(("Drivers:\n"));
2400 	print_driver_list(dc->drivers, indent+1);
2401 
2402 	indentprintf(("Devices:\n"));
2403 	for (i = 0; i < dc->maxunit; i++)
2404 		if (dc->devices[i])
2405 			print_device(dc->devices[i], indent+1);
2406 }
2407 
2408 void
2409 print_devclass_list_short(void)
2410 {
2411 	devclass_t dc;
2412 
2413 	printf("Short listing of devclasses, drivers & devices:\n");
2414 	for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
2415 		print_devclass_short(dc, 0);
2416 }
2417 
2418 void
2419 print_devclass_list(void)
2420 {
2421 	devclass_t dc;
2422 
2423 	printf("Full listing of devclasses, drivers & devices:\n");
2424 	for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
2425 		print_devclass(dc, 0);
2426 }
2427 
2428 #endif
2429