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