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