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