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