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