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