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