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