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