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