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