xref: /freebsd/sys/kern/subr_bus.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/poll.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/condvar.h>
46 #include <sys/queue.h>
47 #include <machine/bus.h>
48 #include <sys/random.h>
49 #include <sys/rman.h>
50 #include <sys/selinfo.h>
51 #include <sys/signalvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54 #include <sys/uio.h>
55 #include <sys/bus.h>
56 #include <sys/interrupt.h>
57 #include <sys/cpuset.h>
58 
59 #include <net/vnet.h>
60 
61 #include <machine/cpu.h>
62 #include <machine/stdarg.h>
63 
64 #include <vm/uma.h>
65 
66 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
67 SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
68 
69 /*
70  * Used to attach drivers to devclasses.
71  */
72 typedef struct driverlink *driverlink_t;
73 struct driverlink {
74 	kobj_class_t	driver;
75 	TAILQ_ENTRY(driverlink) link;	/* list of drivers in devclass */
76 	int		pass;
77 	TAILQ_ENTRY(driverlink) passlink;
78 };
79 
80 /*
81  * Forward declarations
82  */
83 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
84 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
85 typedef TAILQ_HEAD(device_list, device) device_list_t;
86 
87 struct devclass {
88 	TAILQ_ENTRY(devclass) link;
89 	devclass_t	parent;		/* parent in devclass hierarchy */
90 	driver_list_t	drivers;     /* bus devclasses store drivers for bus */
91 	char		*name;
92 	device_t	*devices;	/* array of devices indexed by unit */
93 	int		maxunit;	/* size of devices array */
94 	int		flags;
95 #define DC_HAS_CHILDREN		1
96 
97 	struct sysctl_ctx_list sysctl_ctx;
98 	struct sysctl_oid *sysctl_tree;
99 };
100 
101 /**
102  * @brief Implementation of device.
103  */
104 struct device {
105 	/*
106 	 * A device is a kernel object. The first field must be the
107 	 * current ops table for the object.
108 	 */
109 	KOBJ_FIELDS;
110 
111 	/*
112 	 * Device hierarchy.
113 	 */
114 	TAILQ_ENTRY(device)	link;	/**< list of devices in parent */
115 	TAILQ_ENTRY(device)	devlink; /**< global device list membership */
116 	device_t	parent;		/**< parent of this device  */
117 	device_list_t	children;	/**< list of child devices */
118 
119 	/*
120 	 * Details of this device.
121 	 */
122 	driver_t	*driver;	/**< current driver */
123 	devclass_t	devclass;	/**< current device class */
124 	int		unit;		/**< current unit number */
125 	char*		nameunit;	/**< name+unit e.g. foodev0 */
126 	char*		desc;		/**< driver specific description */
127 	int		busy;		/**< count of calls to device_busy() */
128 	device_state_t	state;		/**< current device state  */
129 	uint32_t	devflags;	/**< api level flags for device_get_flags() */
130 	u_int		flags;		/**< internal device flags  */
131 	u_int	order;			/**< order from device_add_child_ordered() */
132 	void	*ivars;			/**< instance variables  */
133 	void	*softc;			/**< current driver's variables  */
134 
135 	struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables  */
136 	struct sysctl_oid *sysctl_tree;	/**< state for sysctl variables */
137 };
138 
139 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
140 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
141 
142 static void devctl2_init(void);
143 
144 #ifdef BUS_DEBUG
145 
146 static int bus_debug = 1;
147 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
148     "Bus debug level");
149 
150 #define PDEBUG(a)	if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
151 #define DEVICENAME(d)	((d)? device_get_name(d): "no device")
152 #define DRIVERNAME(d)	((d)? d->name : "no driver")
153 #define DEVCLANAME(d)	((d)? d->name : "no devclass")
154 
155 /**
156  * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
157  * prevent syslog from deleting initial spaces
158  */
159 #define indentprintf(p)	do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf("  "); printf p ; } while (0)
160 
161 static void print_device_short(device_t dev, int indent);
162 static void print_device(device_t dev, int indent);
163 void print_device_tree_short(device_t dev, int indent);
164 void print_device_tree(device_t dev, int indent);
165 static void print_driver_short(driver_t *driver, int indent);
166 static void print_driver(driver_t *driver, int indent);
167 static void print_driver_list(driver_list_t drivers, int indent);
168 static void print_devclass_short(devclass_t dc, int indent);
169 static void print_devclass(devclass_t dc, int indent);
170 void print_devclass_list_short(void);
171 void print_devclass_list(void);
172 
173 #else
174 /* Make the compiler ignore the function calls */
175 #define PDEBUG(a)			/* nop */
176 #define DEVICENAME(d)			/* nop */
177 #define DRIVERNAME(d)			/* nop */
178 #define DEVCLANAME(d)			/* nop */
179 
180 #define print_device_short(d,i)		/* nop */
181 #define print_device(d,i)		/* nop */
182 #define print_device_tree_short(d,i)	/* nop */
183 #define print_device_tree(d,i)		/* nop */
184 #define print_driver_short(d,i)		/* nop */
185 #define print_driver(d,i)		/* nop */
186 #define print_driver_list(d,i)		/* nop */
187 #define print_devclass_short(d,i)	/* nop */
188 #define print_devclass(d,i)		/* nop */
189 #define print_devclass_list_short()	/* nop */
190 #define print_devclass_list()		/* nop */
191 #endif
192 
193 /*
194  * dev sysctl tree
195  */
196 
197 enum {
198 	DEVCLASS_SYSCTL_PARENT,
199 };
200 
201 static int
202 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
203 {
204 	devclass_t dc = (devclass_t)arg1;
205 	const char *value;
206 
207 	switch (arg2) {
208 	case DEVCLASS_SYSCTL_PARENT:
209 		value = dc->parent ? dc->parent->name : "";
210 		break;
211 	default:
212 		return (EINVAL);
213 	}
214 	return (SYSCTL_OUT_STR(req, value));
215 }
216 
217 static void
218 devclass_sysctl_init(devclass_t dc)
219 {
220 
221 	if (dc->sysctl_tree != NULL)
222 		return;
223 	sysctl_ctx_init(&dc->sysctl_ctx);
224 	dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
225 	    SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
226 	    CTLFLAG_RD, NULL, "");
227 	SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
228 	    OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
229 	    dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
230 	    "parent class");
231 }
232 
233 enum {
234 	DEVICE_SYSCTL_DESC,
235 	DEVICE_SYSCTL_DRIVER,
236 	DEVICE_SYSCTL_LOCATION,
237 	DEVICE_SYSCTL_PNPINFO,
238 	DEVICE_SYSCTL_PARENT,
239 };
240 
241 static int
242 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
243 {
244 	device_t dev = (device_t)arg1;
245 	const char *value;
246 	char *buf;
247 	int error;
248 
249 	buf = NULL;
250 	switch (arg2) {
251 	case DEVICE_SYSCTL_DESC:
252 		value = dev->desc ? dev->desc : "";
253 		break;
254 	case DEVICE_SYSCTL_DRIVER:
255 		value = dev->driver ? dev->driver->name : "";
256 		break;
257 	case DEVICE_SYSCTL_LOCATION:
258 		value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
259 		bus_child_location_str(dev, buf, 1024);
260 		break;
261 	case DEVICE_SYSCTL_PNPINFO:
262 		value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
263 		bus_child_pnpinfo_str(dev, buf, 1024);
264 		break;
265 	case DEVICE_SYSCTL_PARENT:
266 		value = dev->parent ? dev->parent->nameunit : "";
267 		break;
268 	default:
269 		return (EINVAL);
270 	}
271 	error = SYSCTL_OUT_STR(req, value);
272 	if (buf != NULL)
273 		free(buf, M_BUS);
274 	return (error);
275 }
276 
277 static void
278 device_sysctl_init(device_t dev)
279 {
280 	devclass_t dc = dev->devclass;
281 	int domain;
282 
283 	if (dev->sysctl_tree != NULL)
284 		return;
285 	devclass_sysctl_init(dc);
286 	sysctl_ctx_init(&dev->sysctl_ctx);
287 	dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
288 	    SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
289 	    dev->nameunit + strlen(dc->name),
290 	    CTLFLAG_RD, NULL, "");
291 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
292 	    OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
293 	    dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
294 	    "device description");
295 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
296 	    OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
297 	    dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
298 	    "device driver name");
299 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
300 	    OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
301 	    dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
302 	    "device location relative to parent");
303 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
304 	    OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
305 	    dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
306 	    "device identification");
307 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
308 	    OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
309 	    dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
310 	    "parent device");
311 	if (bus_get_domain(dev, &domain) == 0)
312 		SYSCTL_ADD_INT(&dev->sysctl_ctx,
313 		    SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain",
314 		    CTLFLAG_RD, NULL, domain, "NUMA domain");
315 }
316 
317 static void
318 device_sysctl_update(device_t dev)
319 {
320 	devclass_t dc = dev->devclass;
321 
322 	if (dev->sysctl_tree == NULL)
323 		return;
324 	sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
325 }
326 
327 static void
328 device_sysctl_fini(device_t dev)
329 {
330 	if (dev->sysctl_tree == NULL)
331 		return;
332 	sysctl_ctx_free(&dev->sysctl_ctx);
333 	dev->sysctl_tree = NULL;
334 }
335 
336 /*
337  * /dev/devctl implementation
338  */
339 
340 /*
341  * This design allows only one reader for /dev/devctl.  This is not desirable
342  * in the long run, but will get a lot of hair out of this implementation.
343  * Maybe we should make this device a clonable device.
344  *
345  * Also note: we specifically do not attach a device to the device_t tree
346  * to avoid potential chicken and egg problems.  One could argue that all
347  * of this belongs to the root node.  One could also further argue that the
348  * sysctl interface that we have not might more properly be an ioctl
349  * interface, but at this stage of the game, I'm not inclined to rock that
350  * boat.
351  *
352  * I'm also not sure that the SIGIO support is done correctly or not, as
353  * I copied it from a driver that had SIGIO support that likely hasn't been
354  * tested since 3.4 or 2.2.8!
355  */
356 
357 /* Deprecated way to adjust queue length */
358 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
359 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RWTUN |
360     CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_disable, "I",
361     "devctl disable -- deprecated");
362 
363 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
364 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
365 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
366 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN |
367     CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
368 
369 static d_open_t		devopen;
370 static d_close_t	devclose;
371 static d_read_t		devread;
372 static d_ioctl_t	devioctl;
373 static d_poll_t		devpoll;
374 static d_kqfilter_t	devkqfilter;
375 
376 static struct cdevsw dev_cdevsw = {
377 	.d_version =	D_VERSION,
378 	.d_open =	devopen,
379 	.d_close =	devclose,
380 	.d_read =	devread,
381 	.d_ioctl =	devioctl,
382 	.d_poll =	devpoll,
383 	.d_kqfilter =	devkqfilter,
384 	.d_name =	"devctl",
385 };
386 
387 struct dev_event_info
388 {
389 	char *dei_data;
390 	TAILQ_ENTRY(dev_event_info) dei_link;
391 };
392 
393 TAILQ_HEAD(devq, dev_event_info);
394 
395 static struct dev_softc
396 {
397 	int	inuse;
398 	int	nonblock;
399 	int	queued;
400 	int	async;
401 	struct mtx mtx;
402 	struct cv cv;
403 	struct selinfo sel;
404 	struct devq devq;
405 	struct sigio *sigio;
406 } devsoftc;
407 
408 static void	filt_devctl_detach(struct knote *kn);
409 static int	filt_devctl_read(struct knote *kn, long hint);
410 
411 struct filterops devctl_rfiltops = {
412 	.f_isfd = 1,
413 	.f_detach = filt_devctl_detach,
414 	.f_event = filt_devctl_read,
415 };
416 
417 static struct cdev *devctl_dev;
418 
419 static void
420 devinit(void)
421 {
422 	devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
423 	    UID_ROOT, GID_WHEEL, 0600, "devctl");
424 	mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
425 	cv_init(&devsoftc.cv, "dev cv");
426 	TAILQ_INIT(&devsoftc.devq);
427 	knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
428 	devctl2_init();
429 }
430 
431 static int
432 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
433 {
434 
435 	mtx_lock(&devsoftc.mtx);
436 	if (devsoftc.inuse) {
437 		mtx_unlock(&devsoftc.mtx);
438 		return (EBUSY);
439 	}
440 	/* move to init */
441 	devsoftc.inuse = 1;
442 	mtx_unlock(&devsoftc.mtx);
443 	return (0);
444 }
445 
446 static int
447 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
448 {
449 
450 	mtx_lock(&devsoftc.mtx);
451 	devsoftc.inuse = 0;
452 	devsoftc.nonblock = 0;
453 	devsoftc.async = 0;
454 	cv_broadcast(&devsoftc.cv);
455 	funsetown(&devsoftc.sigio);
456 	mtx_unlock(&devsoftc.mtx);
457 	return (0);
458 }
459 
460 /*
461  * The read channel for this device is used to report changes to
462  * userland in realtime.  We are required to free the data as well as
463  * the n1 object because we allocate them separately.  Also note that
464  * we return one record at a time.  If you try to read this device a
465  * character at a time, you will lose the rest of the data.  Listening
466  * programs are expected to cope.
467  */
468 static int
469 devread(struct cdev *dev, struct uio *uio, int ioflag)
470 {
471 	struct dev_event_info *n1;
472 	int rv;
473 
474 	mtx_lock(&devsoftc.mtx);
475 	while (TAILQ_EMPTY(&devsoftc.devq)) {
476 		if (devsoftc.nonblock) {
477 			mtx_unlock(&devsoftc.mtx);
478 			return (EAGAIN);
479 		}
480 		rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
481 		if (rv) {
482 			/*
483 			 * Need to translate ERESTART to EINTR here? -- jake
484 			 */
485 			mtx_unlock(&devsoftc.mtx);
486 			return (rv);
487 		}
488 	}
489 	n1 = TAILQ_FIRST(&devsoftc.devq);
490 	TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
491 	devsoftc.queued--;
492 	mtx_unlock(&devsoftc.mtx);
493 	rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
494 	free(n1->dei_data, M_BUS);
495 	free(n1, M_BUS);
496 	return (rv);
497 }
498 
499 static	int
500 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
501 {
502 	switch (cmd) {
503 
504 	case FIONBIO:
505 		if (*(int*)data)
506 			devsoftc.nonblock = 1;
507 		else
508 			devsoftc.nonblock = 0;
509 		return (0);
510 	case FIOASYNC:
511 		if (*(int*)data)
512 			devsoftc.async = 1;
513 		else
514 			devsoftc.async = 0;
515 		return (0);
516 	case FIOSETOWN:
517 		return fsetown(*(int *)data, &devsoftc.sigio);
518 	case FIOGETOWN:
519 		*(int *)data = fgetown(&devsoftc.sigio);
520 		return (0);
521 
522 		/* (un)Support for other fcntl() calls. */
523 	case FIOCLEX:
524 	case FIONCLEX:
525 	case FIONREAD:
526 	default:
527 		break;
528 	}
529 	return (ENOTTY);
530 }
531 
532 static	int
533 devpoll(struct cdev *dev, int events, struct thread *td)
534 {
535 	int	revents = 0;
536 
537 	mtx_lock(&devsoftc.mtx);
538 	if (events & (POLLIN | POLLRDNORM)) {
539 		if (!TAILQ_EMPTY(&devsoftc.devq))
540 			revents = events & (POLLIN | POLLRDNORM);
541 		else
542 			selrecord(td, &devsoftc.sel);
543 	}
544 	mtx_unlock(&devsoftc.mtx);
545 
546 	return (revents);
547 }
548 
549 static int
550 devkqfilter(struct cdev *dev, struct knote *kn)
551 {
552 	int error;
553 
554 	if (kn->kn_filter == EVFILT_READ) {
555 		kn->kn_fop = &devctl_rfiltops;
556 		knlist_add(&devsoftc.sel.si_note, kn, 0);
557 		error = 0;
558 	} else
559 		error = EINVAL;
560 	return (error);
561 }
562 
563 static void
564 filt_devctl_detach(struct knote *kn)
565 {
566 
567 	knlist_remove(&devsoftc.sel.si_note, kn, 0);
568 }
569 
570 static int
571 filt_devctl_read(struct knote *kn, long hint)
572 {
573 	kn->kn_data = devsoftc.queued;
574 	return (kn->kn_data != 0);
575 }
576 
577 /**
578  * @brief Return whether the userland process is running
579  */
580 boolean_t
581 devctl_process_running(void)
582 {
583 	return (devsoftc.inuse == 1);
584 }
585 
586 /**
587  * @brief Queue data to be read from the devctl device
588  *
589  * Generic interface to queue data to the devctl device.  It is
590  * assumed that @p data is properly formatted.  It is further assumed
591  * that @p data is allocated using the M_BUS malloc type.
592  */
593 void
594 devctl_queue_data_f(char *data, int flags)
595 {
596 	struct dev_event_info *n1 = NULL, *n2 = NULL;
597 
598 	if (strlen(data) == 0)
599 		goto out;
600 	if (devctl_queue_length == 0)
601 		goto out;
602 	n1 = malloc(sizeof(*n1), M_BUS, flags);
603 	if (n1 == NULL)
604 		goto out;
605 	n1->dei_data = data;
606 	mtx_lock(&devsoftc.mtx);
607 	if (devctl_queue_length == 0) {
608 		mtx_unlock(&devsoftc.mtx);
609 		free(n1->dei_data, M_BUS);
610 		free(n1, M_BUS);
611 		return;
612 	}
613 	/* Leave at least one spot in the queue... */
614 	while (devsoftc.queued > devctl_queue_length - 1) {
615 		n2 = TAILQ_FIRST(&devsoftc.devq);
616 		TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
617 		free(n2->dei_data, M_BUS);
618 		free(n2, M_BUS);
619 		devsoftc.queued--;
620 	}
621 	TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
622 	devsoftc.queued++;
623 	cv_broadcast(&devsoftc.cv);
624 	KNOTE_LOCKED(&devsoftc.sel.si_note, 0);
625 	mtx_unlock(&devsoftc.mtx);
626 	selwakeup(&devsoftc.sel);
627 	if (devsoftc.async && devsoftc.sigio != NULL)
628 		pgsigio(&devsoftc.sigio, SIGIO, 0);
629 	return;
630 out:
631 	/*
632 	 * We have to free data on all error paths since the caller
633 	 * assumes it will be free'd when this item is dequeued.
634 	 */
635 	free(data, M_BUS);
636 	return;
637 }
638 
639 void
640 devctl_queue_data(char *data)
641 {
642 
643 	devctl_queue_data_f(data, M_NOWAIT);
644 }
645 
646 /**
647  * @brief Send a 'notification' to userland, using standard ways
648  */
649 void
650 devctl_notify_f(const char *system, const char *subsystem, const char *type,
651     const char *data, int flags)
652 {
653 	int len = 0;
654 	char *msg;
655 
656 	if (system == NULL)
657 		return;		/* BOGUS!  Must specify system. */
658 	if (subsystem == NULL)
659 		return;		/* BOGUS!  Must specify subsystem. */
660 	if (type == NULL)
661 		return;		/* BOGUS!  Must specify type. */
662 	len += strlen(" system=") + strlen(system);
663 	len += strlen(" subsystem=") + strlen(subsystem);
664 	len += strlen(" type=") + strlen(type);
665 	/* add in the data message plus newline. */
666 	if (data != NULL)
667 		len += strlen(data);
668 	len += 3;	/* '!', '\n', and NUL */
669 	msg = malloc(len, M_BUS, flags);
670 	if (msg == NULL)
671 		return;		/* Drop it on the floor */
672 	if (data != NULL)
673 		snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
674 		    system, subsystem, type, data);
675 	else
676 		snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
677 		    system, subsystem, type);
678 	devctl_queue_data_f(msg, flags);
679 }
680 
681 void
682 devctl_notify(const char *system, const char *subsystem, const char *type,
683     const char *data)
684 {
685 
686 	devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
687 }
688 
689 /*
690  * Common routine that tries to make sending messages as easy as possible.
691  * We allocate memory for the data, copy strings into that, but do not
692  * free it unless there's an error.  The dequeue part of the driver should
693  * free the data.  We don't send data when the device is disabled.  We do
694  * send data, even when we have no listeners, because we wish to avoid
695  * races relating to startup and restart of listening applications.
696  *
697  * devaddq is designed to string together the type of event, with the
698  * object of that event, plus the plug and play info and location info
699  * for that event.  This is likely most useful for devices, but less
700  * useful for other consumers of this interface.  Those should use
701  * the devctl_queue_data() interface instead.
702  */
703 static void
704 devaddq(const char *type, const char *what, device_t dev)
705 {
706 	char *data = NULL;
707 	char *loc = NULL;
708 	char *pnp = NULL;
709 	const char *parstr;
710 
711 	if (!devctl_queue_length)/* Rare race, but lost races safely discard */
712 		return;
713 	data = malloc(1024, M_BUS, M_NOWAIT);
714 	if (data == NULL)
715 		goto bad;
716 
717 	/* get the bus specific location of this device */
718 	loc = malloc(1024, M_BUS, M_NOWAIT);
719 	if (loc == NULL)
720 		goto bad;
721 	*loc = '\0';
722 	bus_child_location_str(dev, loc, 1024);
723 
724 	/* Get the bus specific pnp info of this device */
725 	pnp = malloc(1024, M_BUS, M_NOWAIT);
726 	if (pnp == NULL)
727 		goto bad;
728 	*pnp = '\0';
729 	bus_child_pnpinfo_str(dev, pnp, 1024);
730 
731 	/* Get the parent of this device, or / if high enough in the tree. */
732 	if (device_get_parent(dev) == NULL)
733 		parstr = ".";	/* Or '/' ? */
734 	else
735 		parstr = device_get_nameunit(device_get_parent(dev));
736 	/* String it all together. */
737 	snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
738 	  parstr);
739 	free(loc, M_BUS);
740 	free(pnp, M_BUS);
741 	devctl_queue_data(data);
742 	return;
743 bad:
744 	free(pnp, M_BUS);
745 	free(loc, M_BUS);
746 	free(data, M_BUS);
747 	return;
748 }
749 
750 /*
751  * A device was added to the tree.  We are called just after it successfully
752  * attaches (that is, probe and attach success for this device).  No call
753  * is made if a device is merely parented into the tree.  See devnomatch
754  * if probe fails.  If attach fails, no notification is sent (but maybe
755  * we should have a different message for this).
756  */
757 static void
758 devadded(device_t dev)
759 {
760 	devaddq("+", device_get_nameunit(dev), dev);
761 }
762 
763 /*
764  * A device was removed from the tree.  We are called just before this
765  * happens.
766  */
767 static void
768 devremoved(device_t dev)
769 {
770 	devaddq("-", device_get_nameunit(dev), dev);
771 }
772 
773 /*
774  * Called when there's no match for this device.  This is only called
775  * the first time that no match happens, so we don't keep getting this
776  * message.  Should that prove to be undesirable, we can change it.
777  * This is called when all drivers that can attach to a given bus
778  * decline to accept this device.  Other errors may not be detected.
779  */
780 static void
781 devnomatch(device_t dev)
782 {
783 	devaddq("?", "", dev);
784 }
785 
786 static int
787 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
788 {
789 	struct dev_event_info *n1;
790 	int dis, error;
791 
792 	dis = (devctl_queue_length == 0);
793 	error = sysctl_handle_int(oidp, &dis, 0, req);
794 	if (error || !req->newptr)
795 		return (error);
796 	if (mtx_initialized(&devsoftc.mtx))
797 		mtx_lock(&devsoftc.mtx);
798 	if (dis) {
799 		while (!TAILQ_EMPTY(&devsoftc.devq)) {
800 			n1 = TAILQ_FIRST(&devsoftc.devq);
801 			TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
802 			free(n1->dei_data, M_BUS);
803 			free(n1, M_BUS);
804 		}
805 		devsoftc.queued = 0;
806 		devctl_queue_length = 0;
807 	} else {
808 		devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
809 	}
810 	if (mtx_initialized(&devsoftc.mtx))
811 		mtx_unlock(&devsoftc.mtx);
812 	return (0);
813 }
814 
815 static int
816 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
817 {
818 	struct dev_event_info *n1;
819 	int q, error;
820 
821 	q = devctl_queue_length;
822 	error = sysctl_handle_int(oidp, &q, 0, req);
823 	if (error || !req->newptr)
824 		return (error);
825 	if (q < 0)
826 		return (EINVAL);
827 	if (mtx_initialized(&devsoftc.mtx))
828 		mtx_lock(&devsoftc.mtx);
829 	devctl_queue_length = q;
830 	while (devsoftc.queued > devctl_queue_length) {
831 		n1 = TAILQ_FIRST(&devsoftc.devq);
832 		TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
833 		free(n1->dei_data, M_BUS);
834 		free(n1, M_BUS);
835 		devsoftc.queued--;
836 	}
837 	if (mtx_initialized(&devsoftc.mtx))
838 		mtx_unlock(&devsoftc.mtx);
839 	return (0);
840 }
841 
842 /* End of /dev/devctl code */
843 
844 static TAILQ_HEAD(,device)	bus_data_devices;
845 static int bus_data_generation = 1;
846 
847 static kobj_method_t null_methods[] = {
848 	KOBJMETHOD_END
849 };
850 
851 DEFINE_CLASS(null, null_methods, 0);
852 
853 /*
854  * Bus pass implementation
855  */
856 
857 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
858 int bus_current_pass = BUS_PASS_ROOT;
859 
860 /**
861  * @internal
862  * @brief Register the pass level of a new driver attachment
863  *
864  * Register a new driver attachment's pass level.  If no driver
865  * attachment with the same pass level has been added, then @p new
866  * will be added to the global passes list.
867  *
868  * @param new		the new driver attachment
869  */
870 static void
871 driver_register_pass(struct driverlink *new)
872 {
873 	struct driverlink *dl;
874 
875 	/* We only consider pass numbers during boot. */
876 	if (bus_current_pass == BUS_PASS_DEFAULT)
877 		return;
878 
879 	/*
880 	 * Walk the passes list.  If we already know about this pass
881 	 * then there is nothing to do.  If we don't, then insert this
882 	 * driver link into the list.
883 	 */
884 	TAILQ_FOREACH(dl, &passes, passlink) {
885 		if (dl->pass < new->pass)
886 			continue;
887 		if (dl->pass == new->pass)
888 			return;
889 		TAILQ_INSERT_BEFORE(dl, new, passlink);
890 		return;
891 	}
892 	TAILQ_INSERT_TAIL(&passes, new, passlink);
893 }
894 
895 /**
896  * @brief Raise the current bus pass
897  *
898  * Raise the current bus pass level to @p pass.  Call the BUS_NEW_PASS()
899  * method on the root bus to kick off a new device tree scan for each
900  * new pass level that has at least one driver.
901  */
902 void
903 bus_set_pass(int pass)
904 {
905 	struct driverlink *dl;
906 
907 	if (bus_current_pass > pass)
908 		panic("Attempt to lower bus pass level");
909 
910 	TAILQ_FOREACH(dl, &passes, passlink) {
911 		/* Skip pass values below the current pass level. */
912 		if (dl->pass <= bus_current_pass)
913 			continue;
914 
915 		/*
916 		 * Bail once we hit a driver with a pass level that is
917 		 * too high.
918 		 */
919 		if (dl->pass > pass)
920 			break;
921 
922 		/*
923 		 * Raise the pass level to the next level and rescan
924 		 * the tree.
925 		 */
926 		bus_current_pass = dl->pass;
927 		BUS_NEW_PASS(root_bus);
928 	}
929 
930 	/*
931 	 * If there isn't a driver registered for the requested pass,
932 	 * then bus_current_pass might still be less than 'pass'.  Set
933 	 * it to 'pass' in that case.
934 	 */
935 	if (bus_current_pass < pass)
936 		bus_current_pass = pass;
937 	KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
938 }
939 
940 /*
941  * Devclass implementation
942  */
943 
944 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
945 
946 /**
947  * @internal
948  * @brief Find or create a device class
949  *
950  * If a device class with the name @p classname exists, return it,
951  * otherwise if @p create is non-zero create and return a new device
952  * class.
953  *
954  * If @p parentname is non-NULL, the parent of the devclass is set to
955  * the devclass of that name.
956  *
957  * @param classname	the devclass name to find or create
958  * @param parentname	the parent devclass name or @c NULL
959  * @param create	non-zero to create a devclass
960  */
961 static devclass_t
962 devclass_find_internal(const char *classname, const char *parentname,
963 		       int create)
964 {
965 	devclass_t dc;
966 
967 	PDEBUG(("looking for %s", classname));
968 	if (!classname)
969 		return (NULL);
970 
971 	TAILQ_FOREACH(dc, &devclasses, link) {
972 		if (!strcmp(dc->name, classname))
973 			break;
974 	}
975 
976 	if (create && !dc) {
977 		PDEBUG(("creating %s", classname));
978 		dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
979 		    M_BUS, M_NOWAIT | M_ZERO);
980 		if (!dc)
981 			return (NULL);
982 		dc->parent = NULL;
983 		dc->name = (char*) (dc + 1);
984 		strcpy(dc->name, classname);
985 		TAILQ_INIT(&dc->drivers);
986 		TAILQ_INSERT_TAIL(&devclasses, dc, link);
987 
988 		bus_data_generation_update();
989 	}
990 
991 	/*
992 	 * If a parent class is specified, then set that as our parent so
993 	 * that this devclass will support drivers for the parent class as
994 	 * well.  If the parent class has the same name don't do this though
995 	 * as it creates a cycle that can trigger an infinite loop in
996 	 * device_probe_child() if a device exists for which there is no
997 	 * suitable driver.
998 	 */
999 	if (parentname && dc && !dc->parent &&
1000 	    strcmp(classname, parentname) != 0) {
1001 		dc->parent = devclass_find_internal(parentname, NULL, TRUE);
1002 		dc->parent->flags |= DC_HAS_CHILDREN;
1003 	}
1004 
1005 	return (dc);
1006 }
1007 
1008 /**
1009  * @brief Create a device class
1010  *
1011  * If a device class with the name @p classname exists, return it,
1012  * otherwise create and return a new device class.
1013  *
1014  * @param classname	the devclass name to find or create
1015  */
1016 devclass_t
1017 devclass_create(const char *classname)
1018 {
1019 	return (devclass_find_internal(classname, NULL, TRUE));
1020 }
1021 
1022 /**
1023  * @brief Find a device class
1024  *
1025  * If a device class with the name @p classname exists, return it,
1026  * otherwise return @c NULL.
1027  *
1028  * @param classname	the devclass name to find
1029  */
1030 devclass_t
1031 devclass_find(const char *classname)
1032 {
1033 	return (devclass_find_internal(classname, NULL, FALSE));
1034 }
1035 
1036 /**
1037  * @brief Register that a device driver has been added to a devclass
1038  *
1039  * Register that a device driver has been added to a devclass.  This
1040  * is called by devclass_add_driver to accomplish the recursive
1041  * notification of all the children classes of dc, as well as dc.
1042  * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1043  * the devclass.
1044  *
1045  * We do a full search here of the devclass list at each iteration
1046  * level to save storing children-lists in the devclass structure.  If
1047  * we ever move beyond a few dozen devices doing this, we may need to
1048  * reevaluate...
1049  *
1050  * @param dc		the devclass to edit
1051  * @param driver	the driver that was just added
1052  */
1053 static void
1054 devclass_driver_added(devclass_t dc, driver_t *driver)
1055 {
1056 	devclass_t parent;
1057 	int i;
1058 
1059 	/*
1060 	 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1061 	 */
1062 	for (i = 0; i < dc->maxunit; i++)
1063 		if (dc->devices[i] && device_is_attached(dc->devices[i]))
1064 			BUS_DRIVER_ADDED(dc->devices[i], driver);
1065 
1066 	/*
1067 	 * Walk through the children classes.  Since we only keep a
1068 	 * single parent pointer around, we walk the entire list of
1069 	 * devclasses looking for children.  We set the
1070 	 * DC_HAS_CHILDREN flag when a child devclass is created on
1071 	 * the parent, so we only walk the list for those devclasses
1072 	 * that have children.
1073 	 */
1074 	if (!(dc->flags & DC_HAS_CHILDREN))
1075 		return;
1076 	parent = dc;
1077 	TAILQ_FOREACH(dc, &devclasses, link) {
1078 		if (dc->parent == parent)
1079 			devclass_driver_added(dc, driver);
1080 	}
1081 }
1082 
1083 /**
1084  * @brief Add a device driver to a device class
1085  *
1086  * Add a device driver to a devclass. This is normally called
1087  * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1088  * all devices in the devclass will be called to allow them to attempt
1089  * to re-probe any unmatched children.
1090  *
1091  * @param dc		the devclass to edit
1092  * @param driver	the driver to register
1093  */
1094 int
1095 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1096 {
1097 	driverlink_t dl;
1098 	const char *parentname;
1099 
1100 	PDEBUG(("%s", DRIVERNAME(driver)));
1101 
1102 	/* Don't allow invalid pass values. */
1103 	if (pass <= BUS_PASS_ROOT)
1104 		return (EINVAL);
1105 
1106 	dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1107 	if (!dl)
1108 		return (ENOMEM);
1109 
1110 	/*
1111 	 * Compile the driver's methods. Also increase the reference count
1112 	 * so that the class doesn't get freed when the last instance
1113 	 * goes. This means we can safely use static methods and avoids a
1114 	 * double-free in devclass_delete_driver.
1115 	 */
1116 	kobj_class_compile((kobj_class_t) driver);
1117 
1118 	/*
1119 	 * If the driver has any base classes, make the
1120 	 * devclass inherit from the devclass of the driver's
1121 	 * first base class. This will allow the system to
1122 	 * search for drivers in both devclasses for children
1123 	 * of a device using this driver.
1124 	 */
1125 	if (driver->baseclasses)
1126 		parentname = driver->baseclasses[0]->name;
1127 	else
1128 		parentname = NULL;
1129 	*dcp = devclass_find_internal(driver->name, parentname, TRUE);
1130 
1131 	dl->driver = driver;
1132 	TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1133 	driver->refs++;		/* XXX: kobj_mtx */
1134 	dl->pass = pass;
1135 	driver_register_pass(dl);
1136 
1137 	devclass_driver_added(dc, driver);
1138 	bus_data_generation_update();
1139 	return (0);
1140 }
1141 
1142 /**
1143  * @brief Register that a device driver has been deleted from a devclass
1144  *
1145  * Register that a device driver has been removed from a devclass.
1146  * This is called by devclass_delete_driver to accomplish the
1147  * recursive notification of all the children classes of busclass, as
1148  * well as busclass.  Each layer will attempt to detach the driver
1149  * from any devices that are children of the bus's devclass.  The function
1150  * will return an error if a device fails to detach.
1151  *
1152  * We do a full search here of the devclass list at each iteration
1153  * level to save storing children-lists in the devclass structure.  If
1154  * we ever move beyond a few dozen devices doing this, we may need to
1155  * reevaluate...
1156  *
1157  * @param busclass	the devclass of the parent bus
1158  * @param dc		the devclass of the driver being deleted
1159  * @param driver	the driver being deleted
1160  */
1161 static int
1162 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1163 {
1164 	devclass_t parent;
1165 	device_t dev;
1166 	int error, i;
1167 
1168 	/*
1169 	 * Disassociate from any devices.  We iterate through all the
1170 	 * devices in the devclass of the driver and detach any which are
1171 	 * using the driver and which have a parent in the devclass which
1172 	 * we are deleting from.
1173 	 *
1174 	 * Note that since a driver can be in multiple devclasses, we
1175 	 * should not detach devices which are not children of devices in
1176 	 * the affected devclass.
1177 	 */
1178 	for (i = 0; i < dc->maxunit; i++) {
1179 		if (dc->devices[i]) {
1180 			dev = dc->devices[i];
1181 			if (dev->driver == driver && dev->parent &&
1182 			    dev->parent->devclass == busclass) {
1183 				if ((error = device_detach(dev)) != 0)
1184 					return (error);
1185 				BUS_PROBE_NOMATCH(dev->parent, dev);
1186 				devnomatch(dev);
1187 				dev->flags |= DF_DONENOMATCH;
1188 			}
1189 		}
1190 	}
1191 
1192 	/*
1193 	 * Walk through the children classes.  Since we only keep a
1194 	 * single parent pointer around, we walk the entire list of
1195 	 * devclasses looking for children.  We set the
1196 	 * DC_HAS_CHILDREN flag when a child devclass is created on
1197 	 * the parent, so we only walk the list for those devclasses
1198 	 * that have children.
1199 	 */
1200 	if (!(busclass->flags & DC_HAS_CHILDREN))
1201 		return (0);
1202 	parent = busclass;
1203 	TAILQ_FOREACH(busclass, &devclasses, link) {
1204 		if (busclass->parent == parent) {
1205 			error = devclass_driver_deleted(busclass, dc, driver);
1206 			if (error)
1207 				return (error);
1208 		}
1209 	}
1210 	return (0);
1211 }
1212 
1213 /**
1214  * @brief Delete a device driver from a device class
1215  *
1216  * Delete a device driver from a devclass. This is normally called
1217  * automatically by DRIVER_MODULE().
1218  *
1219  * If the driver is currently attached to any devices,
1220  * devclass_delete_driver() will first attempt to detach from each
1221  * device. If one of the detach calls fails, the driver will not be
1222  * deleted.
1223  *
1224  * @param dc		the devclass to edit
1225  * @param driver	the driver to unregister
1226  */
1227 int
1228 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1229 {
1230 	devclass_t dc = devclass_find(driver->name);
1231 	driverlink_t dl;
1232 	int error;
1233 
1234 	PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1235 
1236 	if (!dc)
1237 		return (0);
1238 
1239 	/*
1240 	 * Find the link structure in the bus' list of drivers.
1241 	 */
1242 	TAILQ_FOREACH(dl, &busclass->drivers, link) {
1243 		if (dl->driver == driver)
1244 			break;
1245 	}
1246 
1247 	if (!dl) {
1248 		PDEBUG(("%s not found in %s list", driver->name,
1249 		    busclass->name));
1250 		return (ENOENT);
1251 	}
1252 
1253 	error = devclass_driver_deleted(busclass, dc, driver);
1254 	if (error != 0)
1255 		return (error);
1256 
1257 	TAILQ_REMOVE(&busclass->drivers, dl, link);
1258 	free(dl, M_BUS);
1259 
1260 	/* XXX: kobj_mtx */
1261 	driver->refs--;
1262 	if (driver->refs == 0)
1263 		kobj_class_free((kobj_class_t) driver);
1264 
1265 	bus_data_generation_update();
1266 	return (0);
1267 }
1268 
1269 /**
1270  * @brief Quiesces a set of device drivers from a device class
1271  *
1272  * Quiesce a device driver from a devclass. This is normally called
1273  * automatically by DRIVER_MODULE().
1274  *
1275  * If the driver is currently attached to any devices,
1276  * devclass_quiesece_driver() will first attempt to quiesce each
1277  * device.
1278  *
1279  * @param dc		the devclass to edit
1280  * @param driver	the driver to unregister
1281  */
1282 static int
1283 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1284 {
1285 	devclass_t dc = devclass_find(driver->name);
1286 	driverlink_t dl;
1287 	device_t dev;
1288 	int i;
1289 	int error;
1290 
1291 	PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1292 
1293 	if (!dc)
1294 		return (0);
1295 
1296 	/*
1297 	 * Find the link structure in the bus' list of drivers.
1298 	 */
1299 	TAILQ_FOREACH(dl, &busclass->drivers, link) {
1300 		if (dl->driver == driver)
1301 			break;
1302 	}
1303 
1304 	if (!dl) {
1305 		PDEBUG(("%s not found in %s list", driver->name,
1306 		    busclass->name));
1307 		return (ENOENT);
1308 	}
1309 
1310 	/*
1311 	 * Quiesce all devices.  We iterate through all the devices in
1312 	 * the devclass of the driver and quiesce any which are using
1313 	 * the driver and which have a parent in the devclass which we
1314 	 * are quiescing.
1315 	 *
1316 	 * Note that since a driver can be in multiple devclasses, we
1317 	 * should not quiesce devices which are not children of
1318 	 * devices in the affected devclass.
1319 	 */
1320 	for (i = 0; i < dc->maxunit; i++) {
1321 		if (dc->devices[i]) {
1322 			dev = dc->devices[i];
1323 			if (dev->driver == driver && dev->parent &&
1324 			    dev->parent->devclass == busclass) {
1325 				if ((error = device_quiesce(dev)) != 0)
1326 					return (error);
1327 			}
1328 		}
1329 	}
1330 
1331 	return (0);
1332 }
1333 
1334 /**
1335  * @internal
1336  */
1337 static driverlink_t
1338 devclass_find_driver_internal(devclass_t dc, const char *classname)
1339 {
1340 	driverlink_t dl;
1341 
1342 	PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1343 
1344 	TAILQ_FOREACH(dl, &dc->drivers, link) {
1345 		if (!strcmp(dl->driver->name, classname))
1346 			return (dl);
1347 	}
1348 
1349 	PDEBUG(("not found"));
1350 	return (NULL);
1351 }
1352 
1353 /**
1354  * @brief Return the name of the devclass
1355  */
1356 const char *
1357 devclass_get_name(devclass_t dc)
1358 {
1359 	return (dc->name);
1360 }
1361 
1362 /**
1363  * @brief Find a device given a unit number
1364  *
1365  * @param dc		the devclass to search
1366  * @param unit		the unit number to search for
1367  *
1368  * @returns		the device with the given unit number or @c
1369  *			NULL if there is no such device
1370  */
1371 device_t
1372 devclass_get_device(devclass_t dc, int unit)
1373 {
1374 	if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1375 		return (NULL);
1376 	return (dc->devices[unit]);
1377 }
1378 
1379 /**
1380  * @brief Find the softc field of a device given a unit number
1381  *
1382  * @param dc		the devclass to search
1383  * @param unit		the unit number to search for
1384  *
1385  * @returns		the softc field of the device with the given
1386  *			unit number or @c NULL if there is no such
1387  *			device
1388  */
1389 void *
1390 devclass_get_softc(devclass_t dc, int unit)
1391 {
1392 	device_t dev;
1393 
1394 	dev = devclass_get_device(dc, unit);
1395 	if (!dev)
1396 		return (NULL);
1397 
1398 	return (device_get_softc(dev));
1399 }
1400 
1401 /**
1402  * @brief Get a list of devices in the devclass
1403  *
1404  * An array containing a list of all the devices in the given devclass
1405  * is allocated and returned in @p *devlistp. The number of devices
1406  * in the array is returned in @p *devcountp. The caller should free
1407  * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1408  *
1409  * @param dc		the devclass to examine
1410  * @param devlistp	points at location for array pointer return
1411  *			value
1412  * @param devcountp	points at location for array size return value
1413  *
1414  * @retval 0		success
1415  * @retval ENOMEM	the array allocation failed
1416  */
1417 int
1418 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1419 {
1420 	int count, i;
1421 	device_t *list;
1422 
1423 	count = devclass_get_count(dc);
1424 	list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1425 	if (!list)
1426 		return (ENOMEM);
1427 
1428 	count = 0;
1429 	for (i = 0; i < dc->maxunit; i++) {
1430 		if (dc->devices[i]) {
1431 			list[count] = dc->devices[i];
1432 			count++;
1433 		}
1434 	}
1435 
1436 	*devlistp = list;
1437 	*devcountp = count;
1438 
1439 	return (0);
1440 }
1441 
1442 /**
1443  * @brief Get a list of drivers in the devclass
1444  *
1445  * An array containing a list of pointers to all the drivers in the
1446  * given devclass is allocated and returned in @p *listp.  The number
1447  * of drivers in the array is returned in @p *countp. The caller should
1448  * free the array using @c free(p, M_TEMP).
1449  *
1450  * @param dc		the devclass to examine
1451  * @param listp		gives location for array pointer return value
1452  * @param countp	gives location for number of array elements
1453  *			return value
1454  *
1455  * @retval 0		success
1456  * @retval ENOMEM	the array allocation failed
1457  */
1458 int
1459 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1460 {
1461 	driverlink_t dl;
1462 	driver_t **list;
1463 	int count;
1464 
1465 	count = 0;
1466 	TAILQ_FOREACH(dl, &dc->drivers, link)
1467 		count++;
1468 	list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1469 	if (list == NULL)
1470 		return (ENOMEM);
1471 
1472 	count = 0;
1473 	TAILQ_FOREACH(dl, &dc->drivers, link) {
1474 		list[count] = dl->driver;
1475 		count++;
1476 	}
1477 	*listp = list;
1478 	*countp = count;
1479 
1480 	return (0);
1481 }
1482 
1483 /**
1484  * @brief Get the number of devices in a devclass
1485  *
1486  * @param dc		the devclass to examine
1487  */
1488 int
1489 devclass_get_count(devclass_t dc)
1490 {
1491 	int count, i;
1492 
1493 	count = 0;
1494 	for (i = 0; i < dc->maxunit; i++)
1495 		if (dc->devices[i])
1496 			count++;
1497 	return (count);
1498 }
1499 
1500 /**
1501  * @brief Get the maximum unit number used in a devclass
1502  *
1503  * Note that this is one greater than the highest currently-allocated
1504  * unit.  If a null devclass_t is passed in, -1 is returned to indicate
1505  * that not even the devclass has been allocated yet.
1506  *
1507  * @param dc		the devclass to examine
1508  */
1509 int
1510 devclass_get_maxunit(devclass_t dc)
1511 {
1512 	if (dc == NULL)
1513 		return (-1);
1514 	return (dc->maxunit);
1515 }
1516 
1517 /**
1518  * @brief Find a free unit number in a devclass
1519  *
1520  * This function searches for the first unused unit number greater
1521  * that or equal to @p unit.
1522  *
1523  * @param dc		the devclass to examine
1524  * @param unit		the first unit number to check
1525  */
1526 int
1527 devclass_find_free_unit(devclass_t dc, int unit)
1528 {
1529 	if (dc == NULL)
1530 		return (unit);
1531 	while (unit < dc->maxunit && dc->devices[unit] != NULL)
1532 		unit++;
1533 	return (unit);
1534 }
1535 
1536 /**
1537  * @brief Set the parent of a devclass
1538  *
1539  * The parent class is normally initialised automatically by
1540  * DRIVER_MODULE().
1541  *
1542  * @param dc		the devclass to edit
1543  * @param pdc		the new parent devclass
1544  */
1545 void
1546 devclass_set_parent(devclass_t dc, devclass_t pdc)
1547 {
1548 	dc->parent = pdc;
1549 }
1550 
1551 /**
1552  * @brief Get the parent of a devclass
1553  *
1554  * @param dc		the devclass to examine
1555  */
1556 devclass_t
1557 devclass_get_parent(devclass_t dc)
1558 {
1559 	return (dc->parent);
1560 }
1561 
1562 struct sysctl_ctx_list *
1563 devclass_get_sysctl_ctx(devclass_t dc)
1564 {
1565 	return (&dc->sysctl_ctx);
1566 }
1567 
1568 struct sysctl_oid *
1569 devclass_get_sysctl_tree(devclass_t dc)
1570 {
1571 	return (dc->sysctl_tree);
1572 }
1573 
1574 /**
1575  * @internal
1576  * @brief Allocate a unit number
1577  *
1578  * On entry, @p *unitp is the desired unit number (or @c -1 if any
1579  * will do). The allocated unit number is returned in @p *unitp.
1580 
1581  * @param dc		the devclass to allocate from
1582  * @param unitp		points at the location for the allocated unit
1583  *			number
1584  *
1585  * @retval 0		success
1586  * @retval EEXIST	the requested unit number is already allocated
1587  * @retval ENOMEM	memory allocation failure
1588  */
1589 static int
1590 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1591 {
1592 	const char *s;
1593 	int unit = *unitp;
1594 
1595 	PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1596 
1597 	/* Ask the parent bus if it wants to wire this device. */
1598 	if (unit == -1)
1599 		BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1600 		    &unit);
1601 
1602 	/* If we were given a wired unit number, check for existing device */
1603 	/* XXX imp XXX */
1604 	if (unit != -1) {
1605 		if (unit >= 0 && unit < dc->maxunit &&
1606 		    dc->devices[unit] != NULL) {
1607 			if (bootverbose)
1608 				printf("%s: %s%d already exists; skipping it\n",
1609 				    dc->name, dc->name, *unitp);
1610 			return (EEXIST);
1611 		}
1612 	} else {
1613 		/* Unwired device, find the next available slot for it */
1614 		unit = 0;
1615 		for (unit = 0;; unit++) {
1616 			/* If there is an "at" hint for a unit then skip it. */
1617 			if (resource_string_value(dc->name, unit, "at", &s) ==
1618 			    0)
1619 				continue;
1620 
1621 			/* If this device slot is already in use, skip it. */
1622 			if (unit < dc->maxunit && dc->devices[unit] != NULL)
1623 				continue;
1624 
1625 			break;
1626 		}
1627 	}
1628 
1629 	/*
1630 	 * We've selected a unit beyond the length of the table, so let's
1631 	 * extend the table to make room for all units up to and including
1632 	 * this one.
1633 	 */
1634 	if (unit >= dc->maxunit) {
1635 		device_t *newlist, *oldlist;
1636 		int newsize;
1637 
1638 		oldlist = dc->devices;
1639 		newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1640 		newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1641 		if (!newlist)
1642 			return (ENOMEM);
1643 		if (oldlist != NULL)
1644 			bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1645 		bzero(newlist + dc->maxunit,
1646 		    sizeof(device_t) * (newsize - dc->maxunit));
1647 		dc->devices = newlist;
1648 		dc->maxunit = newsize;
1649 		if (oldlist != NULL)
1650 			free(oldlist, M_BUS);
1651 	}
1652 	PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1653 
1654 	*unitp = unit;
1655 	return (0);
1656 }
1657 
1658 /**
1659  * @internal
1660  * @brief Add a device to a devclass
1661  *
1662  * A unit number is allocated for the device (using the device's
1663  * preferred unit number if any) and the device is registered in the
1664  * devclass. This allows the device to be looked up by its unit
1665  * number, e.g. by decoding a dev_t minor number.
1666  *
1667  * @param dc		the devclass to add to
1668  * @param dev		the device to add
1669  *
1670  * @retval 0		success
1671  * @retval EEXIST	the requested unit number is already allocated
1672  * @retval ENOMEM	memory allocation failure
1673  */
1674 static int
1675 devclass_add_device(devclass_t dc, device_t dev)
1676 {
1677 	int buflen, error;
1678 
1679 	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1680 
1681 	buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1682 	if (buflen < 0)
1683 		return (ENOMEM);
1684 	dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1685 	if (!dev->nameunit)
1686 		return (ENOMEM);
1687 
1688 	if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1689 		free(dev->nameunit, M_BUS);
1690 		dev->nameunit = NULL;
1691 		return (error);
1692 	}
1693 	dc->devices[dev->unit] = dev;
1694 	dev->devclass = dc;
1695 	snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1696 
1697 	return (0);
1698 }
1699 
1700 /**
1701  * @internal
1702  * @brief Delete a device from a devclass
1703  *
1704  * The device is removed from the devclass's device list and its unit
1705  * number is freed.
1706 
1707  * @param dc		the devclass to delete from
1708  * @param dev		the device to delete
1709  *
1710  * @retval 0		success
1711  */
1712 static int
1713 devclass_delete_device(devclass_t dc, device_t dev)
1714 {
1715 	if (!dc || !dev)
1716 		return (0);
1717 
1718 	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1719 
1720 	if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1721 		panic("devclass_delete_device: inconsistent device class");
1722 	dc->devices[dev->unit] = NULL;
1723 	if (dev->flags & DF_WILDCARD)
1724 		dev->unit = -1;
1725 	dev->devclass = NULL;
1726 	free(dev->nameunit, M_BUS);
1727 	dev->nameunit = NULL;
1728 
1729 	return (0);
1730 }
1731 
1732 /**
1733  * @internal
1734  * @brief Make a new device and add it as a child of @p parent
1735  *
1736  * @param parent	the parent of the new device
1737  * @param name		the devclass name of the new device or @c NULL
1738  *			to leave the devclass unspecified
1739  * @parem unit		the unit number of the new device of @c -1 to
1740  *			leave the unit number unspecified
1741  *
1742  * @returns the new device
1743  */
1744 static device_t
1745 make_device(device_t parent, const char *name, int unit)
1746 {
1747 	device_t dev;
1748 	devclass_t dc;
1749 
1750 	PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1751 
1752 	if (name) {
1753 		dc = devclass_find_internal(name, NULL, TRUE);
1754 		if (!dc) {
1755 			printf("make_device: can't find device class %s\n",
1756 			    name);
1757 			return (NULL);
1758 		}
1759 	} else {
1760 		dc = NULL;
1761 	}
1762 
1763 	dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1764 	if (!dev)
1765 		return (NULL);
1766 
1767 	dev->parent = parent;
1768 	TAILQ_INIT(&dev->children);
1769 	kobj_init((kobj_t) dev, &null_class);
1770 	dev->driver = NULL;
1771 	dev->devclass = NULL;
1772 	dev->unit = unit;
1773 	dev->nameunit = NULL;
1774 	dev->desc = NULL;
1775 	dev->busy = 0;
1776 	dev->devflags = 0;
1777 	dev->flags = DF_ENABLED;
1778 	dev->order = 0;
1779 	if (unit == -1)
1780 		dev->flags |= DF_WILDCARD;
1781 	if (name) {
1782 		dev->flags |= DF_FIXEDCLASS;
1783 		if (devclass_add_device(dc, dev)) {
1784 			kobj_delete((kobj_t) dev, M_BUS);
1785 			return (NULL);
1786 		}
1787 	}
1788 	dev->ivars = NULL;
1789 	dev->softc = NULL;
1790 
1791 	dev->state = DS_NOTPRESENT;
1792 
1793 	TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1794 	bus_data_generation_update();
1795 
1796 	return (dev);
1797 }
1798 
1799 /**
1800  * @internal
1801  * @brief Print a description of a device.
1802  */
1803 static int
1804 device_print_child(device_t dev, device_t child)
1805 {
1806 	int retval = 0;
1807 
1808 	if (device_is_alive(child))
1809 		retval += BUS_PRINT_CHILD(dev, child);
1810 	else
1811 		retval += device_printf(child, " not found\n");
1812 
1813 	return (retval);
1814 }
1815 
1816 /**
1817  * @brief Create a new device
1818  *
1819  * This creates a new device and adds it as a child of an existing
1820  * parent device. The new device will be added after the last existing
1821  * child with order zero.
1822  *
1823  * @param dev		the device which will be the parent of the
1824  *			new child device
1825  * @param name		devclass name for new device or @c NULL if not
1826  *			specified
1827  * @param unit		unit number for new device or @c -1 if not
1828  *			specified
1829  *
1830  * @returns		the new device
1831  */
1832 device_t
1833 device_add_child(device_t dev, const char *name, int unit)
1834 {
1835 	return (device_add_child_ordered(dev, 0, name, unit));
1836 }
1837 
1838 /**
1839  * @brief Create a new device
1840  *
1841  * This creates a new device and adds it as a child of an existing
1842  * parent device. The new device will be added after the last existing
1843  * child with the same order.
1844  *
1845  * @param dev		the device which will be the parent of the
1846  *			new child device
1847  * @param order		a value which is used to partially sort the
1848  *			children of @p dev - devices created using
1849  *			lower values of @p order appear first in @p
1850  *			dev's list of children
1851  * @param name		devclass name for new device or @c NULL if not
1852  *			specified
1853  * @param unit		unit number for new device or @c -1 if not
1854  *			specified
1855  *
1856  * @returns		the new device
1857  */
1858 device_t
1859 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1860 {
1861 	device_t child;
1862 	device_t place;
1863 
1864 	PDEBUG(("%s at %s with order %u as unit %d",
1865 	    name, DEVICENAME(dev), order, unit));
1866 	KASSERT(name != NULL || unit == -1,
1867 	    ("child device with wildcard name and specific unit number"));
1868 
1869 	child = make_device(dev, name, unit);
1870 	if (child == NULL)
1871 		return (child);
1872 	child->order = order;
1873 
1874 	TAILQ_FOREACH(place, &dev->children, link) {
1875 		if (place->order > order)
1876 			break;
1877 	}
1878 
1879 	if (place) {
1880 		/*
1881 		 * The device 'place' is the first device whose order is
1882 		 * greater than the new child.
1883 		 */
1884 		TAILQ_INSERT_BEFORE(place, child, link);
1885 	} else {
1886 		/*
1887 		 * The new child's order is greater or equal to the order of
1888 		 * any existing device. Add the child to the tail of the list.
1889 		 */
1890 		TAILQ_INSERT_TAIL(&dev->children, child, link);
1891 	}
1892 
1893 	bus_data_generation_update();
1894 	return (child);
1895 }
1896 
1897 /**
1898  * @brief Delete a device
1899  *
1900  * This function deletes a device along with all of its children. If
1901  * the device currently has a driver attached to it, the device is
1902  * detached first using device_detach().
1903  *
1904  * @param dev		the parent device
1905  * @param child		the device to delete
1906  *
1907  * @retval 0		success
1908  * @retval non-zero	a unit error code describing the error
1909  */
1910 int
1911 device_delete_child(device_t dev, device_t child)
1912 {
1913 	int error;
1914 	device_t grandchild;
1915 
1916 	PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1917 
1918 	/* remove children first */
1919 	while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1920 		error = device_delete_child(child, grandchild);
1921 		if (error)
1922 			return (error);
1923 	}
1924 
1925 	if ((error = device_detach(child)) != 0)
1926 		return (error);
1927 	if (child->devclass)
1928 		devclass_delete_device(child->devclass, child);
1929 	if (child->parent)
1930 		BUS_CHILD_DELETED(dev, child);
1931 	TAILQ_REMOVE(&dev->children, child, link);
1932 	TAILQ_REMOVE(&bus_data_devices, child, devlink);
1933 	kobj_delete((kobj_t) child, M_BUS);
1934 
1935 	bus_data_generation_update();
1936 	return (0);
1937 }
1938 
1939 /**
1940  * @brief Delete all children devices of the given device, if any.
1941  *
1942  * This function deletes all children devices of the given device, if
1943  * any, using the device_delete_child() function for each device it
1944  * finds. If a child device cannot be deleted, this function will
1945  * return an error code.
1946  *
1947  * @param dev		the parent device
1948  *
1949  * @retval 0		success
1950  * @retval non-zero	a device would not detach
1951  */
1952 int
1953 device_delete_children(device_t dev)
1954 {
1955 	device_t child;
1956 	int error;
1957 
1958 	PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1959 
1960 	error = 0;
1961 
1962 	while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1963 		error = device_delete_child(dev, child);
1964 		if (error) {
1965 			PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1966 			break;
1967 		}
1968 	}
1969 	return (error);
1970 }
1971 
1972 /**
1973  * @brief Find a device given a unit number
1974  *
1975  * This is similar to devclass_get_devices() but only searches for
1976  * devices which have @p dev as a parent.
1977  *
1978  * @param dev		the parent device to search
1979  * @param unit		the unit number to search for.  If the unit is -1,
1980  *			return the first child of @p dev which has name
1981  *			@p classname (that is, the one with the lowest unit.)
1982  *
1983  * @returns		the device with the given unit number or @c
1984  *			NULL if there is no such device
1985  */
1986 device_t
1987 device_find_child(device_t dev, const char *classname, int unit)
1988 {
1989 	devclass_t dc;
1990 	device_t child;
1991 
1992 	dc = devclass_find(classname);
1993 	if (!dc)
1994 		return (NULL);
1995 
1996 	if (unit != -1) {
1997 		child = devclass_get_device(dc, unit);
1998 		if (child && child->parent == dev)
1999 			return (child);
2000 	} else {
2001 		for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
2002 			child = devclass_get_device(dc, unit);
2003 			if (child && child->parent == dev)
2004 				return (child);
2005 		}
2006 	}
2007 	return (NULL);
2008 }
2009 
2010 /**
2011  * @internal
2012  */
2013 static driverlink_t
2014 first_matching_driver(devclass_t dc, device_t dev)
2015 {
2016 	if (dev->devclass)
2017 		return (devclass_find_driver_internal(dc, dev->devclass->name));
2018 	return (TAILQ_FIRST(&dc->drivers));
2019 }
2020 
2021 /**
2022  * @internal
2023  */
2024 static driverlink_t
2025 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
2026 {
2027 	if (dev->devclass) {
2028 		driverlink_t dl;
2029 		for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
2030 			if (!strcmp(dev->devclass->name, dl->driver->name))
2031 				return (dl);
2032 		return (NULL);
2033 	}
2034 	return (TAILQ_NEXT(last, link));
2035 }
2036 
2037 /**
2038  * @internal
2039  */
2040 int
2041 device_probe_child(device_t dev, device_t child)
2042 {
2043 	devclass_t dc;
2044 	driverlink_t best = NULL;
2045 	driverlink_t dl;
2046 	int result, pri = 0;
2047 	int hasclass = (child->devclass != NULL);
2048 
2049 	GIANT_REQUIRED;
2050 
2051 	dc = dev->devclass;
2052 	if (!dc)
2053 		panic("device_probe_child: parent device has no devclass");
2054 
2055 	/*
2056 	 * If the state is already probed, then return.  However, don't
2057 	 * return if we can rebid this object.
2058 	 */
2059 	if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2060 		return (0);
2061 
2062 	for (; dc; dc = dc->parent) {
2063 		for (dl = first_matching_driver(dc, child);
2064 		     dl;
2065 		     dl = next_matching_driver(dc, child, dl)) {
2066 			/* If this driver's pass is too high, then ignore it. */
2067 			if (dl->pass > bus_current_pass)
2068 				continue;
2069 
2070 			PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2071 			result = device_set_driver(child, dl->driver);
2072 			if (result == ENOMEM)
2073 				return (result);
2074 			else if (result != 0)
2075 				continue;
2076 			if (!hasclass) {
2077 				if (device_set_devclass(child,
2078 				    dl->driver->name) != 0) {
2079 					char const * devname =
2080 					    device_get_name(child);
2081 					if (devname == NULL)
2082 						devname = "(unknown)";
2083 					printf("driver bug: Unable to set "
2084 					    "devclass (class: %s "
2085 					    "devname: %s)\n",
2086 					    dl->driver->name,
2087 					    devname);
2088 					(void)device_set_driver(child, NULL);
2089 					continue;
2090 				}
2091 			}
2092 
2093 			/* Fetch any flags for the device before probing. */
2094 			resource_int_value(dl->driver->name, child->unit,
2095 			    "flags", &child->devflags);
2096 
2097 			result = DEVICE_PROBE(child);
2098 
2099 			/* Reset flags and devclass before the next probe. */
2100 			child->devflags = 0;
2101 			if (!hasclass)
2102 				(void)device_set_devclass(child, NULL);
2103 
2104 			/*
2105 			 * If the driver returns SUCCESS, there can be
2106 			 * no higher match for this device.
2107 			 */
2108 			if (result == 0) {
2109 				best = dl;
2110 				pri = 0;
2111 				break;
2112 			}
2113 
2114 			/*
2115 			 * Probes that return BUS_PROBE_NOWILDCARD or lower
2116 			 * only match on devices whose driver was explicitly
2117 			 * specified.
2118 			 */
2119 			if (result <= BUS_PROBE_NOWILDCARD &&
2120 			    !(child->flags & DF_FIXEDCLASS)) {
2121 				result = ENXIO;
2122 			}
2123 
2124 			/*
2125 			 * The driver returned an error so it
2126 			 * certainly doesn't match.
2127 			 */
2128 			if (result > 0) {
2129 				(void)device_set_driver(child, NULL);
2130 				continue;
2131 			}
2132 
2133 			/*
2134 			 * A priority lower than SUCCESS, remember the
2135 			 * best matching driver. Initialise the value
2136 			 * of pri for the first match.
2137 			 */
2138 			if (best == NULL || result > pri) {
2139 				best = dl;
2140 				pri = result;
2141 				continue;
2142 			}
2143 		}
2144 		/*
2145 		 * If we have an unambiguous match in this devclass,
2146 		 * don't look in the parent.
2147 		 */
2148 		if (best && pri == 0)
2149 			break;
2150 	}
2151 
2152 	/*
2153 	 * If we found a driver, change state and initialise the devclass.
2154 	 */
2155 	/* XXX What happens if we rebid and got no best? */
2156 	if (best) {
2157 		/*
2158 		 * If this device was attached, and we were asked to
2159 		 * rescan, and it is a different driver, then we have
2160 		 * to detach the old driver and reattach this new one.
2161 		 * Note, we don't have to check for DF_REBID here
2162 		 * because if the state is > DS_ALIVE, we know it must
2163 		 * be.
2164 		 *
2165 		 * This assumes that all DF_REBID drivers can have
2166 		 * their probe routine called at any time and that
2167 		 * they are idempotent as well as completely benign in
2168 		 * normal operations.
2169 		 *
2170 		 * We also have to make sure that the detach
2171 		 * succeeded, otherwise we fail the operation (or
2172 		 * maybe it should just fail silently?  I'm torn).
2173 		 */
2174 		if (child->state > DS_ALIVE && best->driver != child->driver)
2175 			if ((result = device_detach(dev)) != 0)
2176 				return (result);
2177 
2178 		/* Set the winning driver, devclass, and flags. */
2179 		if (!child->devclass) {
2180 			result = device_set_devclass(child, best->driver->name);
2181 			if (result != 0)
2182 				return (result);
2183 		}
2184 		result = device_set_driver(child, best->driver);
2185 		if (result != 0)
2186 			return (result);
2187 		resource_int_value(best->driver->name, child->unit,
2188 		    "flags", &child->devflags);
2189 
2190 		if (pri < 0) {
2191 			/*
2192 			 * A bit bogus. Call the probe method again to make
2193 			 * sure that we have the right description.
2194 			 */
2195 			DEVICE_PROBE(child);
2196 #if 0
2197 			child->flags |= DF_REBID;
2198 #endif
2199 		} else
2200 			child->flags &= ~DF_REBID;
2201 		child->state = DS_ALIVE;
2202 
2203 		bus_data_generation_update();
2204 		return (0);
2205 	}
2206 
2207 	return (ENXIO);
2208 }
2209 
2210 /**
2211  * @brief Return the parent of a device
2212  */
2213 device_t
2214 device_get_parent(device_t dev)
2215 {
2216 	return (dev->parent);
2217 }
2218 
2219 /**
2220  * @brief Get a list of children of a device
2221  *
2222  * An array containing a list of all the children of the given device
2223  * is allocated and returned in @p *devlistp. The number of devices
2224  * in the array is returned in @p *devcountp. The caller should free
2225  * the array using @c free(p, M_TEMP).
2226  *
2227  * @param dev		the device to examine
2228  * @param devlistp	points at location for array pointer return
2229  *			value
2230  * @param devcountp	points at location for array size return value
2231  *
2232  * @retval 0		success
2233  * @retval ENOMEM	the array allocation failed
2234  */
2235 int
2236 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2237 {
2238 	int count;
2239 	device_t child;
2240 	device_t *list;
2241 
2242 	count = 0;
2243 	TAILQ_FOREACH(child, &dev->children, link) {
2244 		count++;
2245 	}
2246 	if (count == 0) {
2247 		*devlistp = NULL;
2248 		*devcountp = 0;
2249 		return (0);
2250 	}
2251 
2252 	list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2253 	if (!list)
2254 		return (ENOMEM);
2255 
2256 	count = 0;
2257 	TAILQ_FOREACH(child, &dev->children, link) {
2258 		list[count] = child;
2259 		count++;
2260 	}
2261 
2262 	*devlistp = list;
2263 	*devcountp = count;
2264 
2265 	return (0);
2266 }
2267 
2268 /**
2269  * @brief Return the current driver for the device or @c NULL if there
2270  * is no driver currently attached
2271  */
2272 driver_t *
2273 device_get_driver(device_t dev)
2274 {
2275 	return (dev->driver);
2276 }
2277 
2278 /**
2279  * @brief Return the current devclass for the device or @c NULL if
2280  * there is none.
2281  */
2282 devclass_t
2283 device_get_devclass(device_t dev)
2284 {
2285 	return (dev->devclass);
2286 }
2287 
2288 /**
2289  * @brief Return the name of the device's devclass or @c NULL if there
2290  * is none.
2291  */
2292 const char *
2293 device_get_name(device_t dev)
2294 {
2295 	if (dev != NULL && dev->devclass)
2296 		return (devclass_get_name(dev->devclass));
2297 	return (NULL);
2298 }
2299 
2300 /**
2301  * @brief Return a string containing the device's devclass name
2302  * followed by an ascii representation of the device's unit number
2303  * (e.g. @c "foo2").
2304  */
2305 const char *
2306 device_get_nameunit(device_t dev)
2307 {
2308 	return (dev->nameunit);
2309 }
2310 
2311 /**
2312  * @brief Return the device's unit number.
2313  */
2314 int
2315 device_get_unit(device_t dev)
2316 {
2317 	return (dev->unit);
2318 }
2319 
2320 /**
2321  * @brief Return the device's description string
2322  */
2323 const char *
2324 device_get_desc(device_t dev)
2325 {
2326 	return (dev->desc);
2327 }
2328 
2329 /**
2330  * @brief Return the device's flags
2331  */
2332 uint32_t
2333 device_get_flags(device_t dev)
2334 {
2335 	return (dev->devflags);
2336 }
2337 
2338 struct sysctl_ctx_list *
2339 device_get_sysctl_ctx(device_t dev)
2340 {
2341 	return (&dev->sysctl_ctx);
2342 }
2343 
2344 struct sysctl_oid *
2345 device_get_sysctl_tree(device_t dev)
2346 {
2347 	return (dev->sysctl_tree);
2348 }
2349 
2350 /**
2351  * @brief Print the name of the device followed by a colon and a space
2352  *
2353  * @returns the number of characters printed
2354  */
2355 int
2356 device_print_prettyname(device_t dev)
2357 {
2358 	const char *name = device_get_name(dev);
2359 
2360 	if (name == NULL)
2361 		return (printf("unknown: "));
2362 	return (printf("%s%d: ", name, device_get_unit(dev)));
2363 }
2364 
2365 /**
2366  * @brief Print the name of the device followed by a colon, a space
2367  * and the result of calling vprintf() with the value of @p fmt and
2368  * the following arguments.
2369  *
2370  * @returns the number of characters printed
2371  */
2372 int
2373 device_printf(device_t dev, const char * fmt, ...)
2374 {
2375 	va_list ap;
2376 	int retval;
2377 
2378 	retval = device_print_prettyname(dev);
2379 	va_start(ap, fmt);
2380 	retval += vprintf(fmt, ap);
2381 	va_end(ap);
2382 	return (retval);
2383 }
2384 
2385 /**
2386  * @internal
2387  */
2388 static void
2389 device_set_desc_internal(device_t dev, const char* desc, int copy)
2390 {
2391 	if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2392 		free(dev->desc, M_BUS);
2393 		dev->flags &= ~DF_DESCMALLOCED;
2394 		dev->desc = NULL;
2395 	}
2396 
2397 	if (copy && desc) {
2398 		dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2399 		if (dev->desc) {
2400 			strcpy(dev->desc, desc);
2401 			dev->flags |= DF_DESCMALLOCED;
2402 		}
2403 	} else {
2404 		/* Avoid a -Wcast-qual warning */
2405 		dev->desc = (char *)(uintptr_t) desc;
2406 	}
2407 
2408 	bus_data_generation_update();
2409 }
2410 
2411 /**
2412  * @brief Set the device's description
2413  *
2414  * The value of @c desc should be a string constant that will not
2415  * change (at least until the description is changed in a subsequent
2416  * call to device_set_desc() or device_set_desc_copy()).
2417  */
2418 void
2419 device_set_desc(device_t dev, const char* desc)
2420 {
2421 	device_set_desc_internal(dev, desc, FALSE);
2422 }
2423 
2424 /**
2425  * @brief Set the device's description
2426  *
2427  * The string pointed to by @c desc is copied. Use this function if
2428  * the device description is generated, (e.g. with sprintf()).
2429  */
2430 void
2431 device_set_desc_copy(device_t dev, const char* desc)
2432 {
2433 	device_set_desc_internal(dev, desc, TRUE);
2434 }
2435 
2436 /**
2437  * @brief Set the device's flags
2438  */
2439 void
2440 device_set_flags(device_t dev, uint32_t flags)
2441 {
2442 	dev->devflags = flags;
2443 }
2444 
2445 /**
2446  * @brief Return the device's softc field
2447  *
2448  * The softc is allocated and zeroed when a driver is attached, based
2449  * on the size field of the driver.
2450  */
2451 void *
2452 device_get_softc(device_t dev)
2453 {
2454 	return (dev->softc);
2455 }
2456 
2457 /**
2458  * @brief Set the device's softc field
2459  *
2460  * Most drivers do not need to use this since the softc is allocated
2461  * automatically when the driver is attached.
2462  */
2463 void
2464 device_set_softc(device_t dev, void *softc)
2465 {
2466 	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2467 		free(dev->softc, M_BUS_SC);
2468 	dev->softc = softc;
2469 	if (dev->softc)
2470 		dev->flags |= DF_EXTERNALSOFTC;
2471 	else
2472 		dev->flags &= ~DF_EXTERNALSOFTC;
2473 }
2474 
2475 /**
2476  * @brief Free claimed softc
2477  *
2478  * Most drivers do not need to use this since the softc is freed
2479  * automatically when the driver is detached.
2480  */
2481 void
2482 device_free_softc(void *softc)
2483 {
2484 	free(softc, M_BUS_SC);
2485 }
2486 
2487 /**
2488  * @brief Claim softc
2489  *
2490  * This function can be used to let the driver free the automatically
2491  * allocated softc using "device_free_softc()". This function is
2492  * useful when the driver is refcounting the softc and the softc
2493  * cannot be freed when the "device_detach" method is called.
2494  */
2495 void
2496 device_claim_softc(device_t dev)
2497 {
2498 	if (dev->softc)
2499 		dev->flags |= DF_EXTERNALSOFTC;
2500 	else
2501 		dev->flags &= ~DF_EXTERNALSOFTC;
2502 }
2503 
2504 /**
2505  * @brief Get the device's ivars field
2506  *
2507  * The ivars field is used by the parent device to store per-device
2508  * state (e.g. the physical location of the device or a list of
2509  * resources).
2510  */
2511 void *
2512 device_get_ivars(device_t dev)
2513 {
2514 
2515 	KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2516 	return (dev->ivars);
2517 }
2518 
2519 /**
2520  * @brief Set the device's ivars field
2521  */
2522 void
2523 device_set_ivars(device_t dev, void * ivars)
2524 {
2525 
2526 	KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2527 	dev->ivars = ivars;
2528 }
2529 
2530 /**
2531  * @brief Return the device's state
2532  */
2533 device_state_t
2534 device_get_state(device_t dev)
2535 {
2536 	return (dev->state);
2537 }
2538 
2539 /**
2540  * @brief Set the DF_ENABLED flag for the device
2541  */
2542 void
2543 device_enable(device_t dev)
2544 {
2545 	dev->flags |= DF_ENABLED;
2546 }
2547 
2548 /**
2549  * @brief Clear the DF_ENABLED flag for the device
2550  */
2551 void
2552 device_disable(device_t dev)
2553 {
2554 	dev->flags &= ~DF_ENABLED;
2555 }
2556 
2557 /**
2558  * @brief Increment the busy counter for the device
2559  */
2560 void
2561 device_busy(device_t dev)
2562 {
2563 	if (dev->state < DS_ATTACHING)
2564 		panic("device_busy: called for unattached device");
2565 	if (dev->busy == 0 && dev->parent)
2566 		device_busy(dev->parent);
2567 	dev->busy++;
2568 	if (dev->state == DS_ATTACHED)
2569 		dev->state = DS_BUSY;
2570 }
2571 
2572 /**
2573  * @brief Decrement the busy counter for the device
2574  */
2575 void
2576 device_unbusy(device_t dev)
2577 {
2578 	if (dev->busy != 0 && dev->state != DS_BUSY &&
2579 	    dev->state != DS_ATTACHING)
2580 		panic("device_unbusy: called for non-busy device %s",
2581 		    device_get_nameunit(dev));
2582 	dev->busy--;
2583 	if (dev->busy == 0) {
2584 		if (dev->parent)
2585 			device_unbusy(dev->parent);
2586 		if (dev->state == DS_BUSY)
2587 			dev->state = DS_ATTACHED;
2588 	}
2589 }
2590 
2591 /**
2592  * @brief Set the DF_QUIET flag for the device
2593  */
2594 void
2595 device_quiet(device_t dev)
2596 {
2597 	dev->flags |= DF_QUIET;
2598 }
2599 
2600 /**
2601  * @brief Clear the DF_QUIET flag for the device
2602  */
2603 void
2604 device_verbose(device_t dev)
2605 {
2606 	dev->flags &= ~DF_QUIET;
2607 }
2608 
2609 /**
2610  * @brief Return non-zero if the DF_QUIET flag is set on the device
2611  */
2612 int
2613 device_is_quiet(device_t dev)
2614 {
2615 	return ((dev->flags & DF_QUIET) != 0);
2616 }
2617 
2618 /**
2619  * @brief Return non-zero if the DF_ENABLED flag is set on the device
2620  */
2621 int
2622 device_is_enabled(device_t dev)
2623 {
2624 	return ((dev->flags & DF_ENABLED) != 0);
2625 }
2626 
2627 /**
2628  * @brief Return non-zero if the device was successfully probed
2629  */
2630 int
2631 device_is_alive(device_t dev)
2632 {
2633 	return (dev->state >= DS_ALIVE);
2634 }
2635 
2636 /**
2637  * @brief Return non-zero if the device currently has a driver
2638  * attached to it
2639  */
2640 int
2641 device_is_attached(device_t dev)
2642 {
2643 	return (dev->state >= DS_ATTACHED);
2644 }
2645 
2646 /**
2647  * @brief Return non-zero if the device is currently suspended.
2648  */
2649 int
2650 device_is_suspended(device_t dev)
2651 {
2652 	return ((dev->flags & DF_SUSPENDED) != 0);
2653 }
2654 
2655 /**
2656  * @brief Set the devclass of a device
2657  * @see devclass_add_device().
2658  */
2659 int
2660 device_set_devclass(device_t dev, const char *classname)
2661 {
2662 	devclass_t dc;
2663 	int error;
2664 
2665 	if (!classname) {
2666 		if (dev->devclass)
2667 			devclass_delete_device(dev->devclass, dev);
2668 		return (0);
2669 	}
2670 
2671 	if (dev->devclass) {
2672 		printf("device_set_devclass: device class already set\n");
2673 		return (EINVAL);
2674 	}
2675 
2676 	dc = devclass_find_internal(classname, NULL, TRUE);
2677 	if (!dc)
2678 		return (ENOMEM);
2679 
2680 	error = devclass_add_device(dc, dev);
2681 
2682 	bus_data_generation_update();
2683 	return (error);
2684 }
2685 
2686 /**
2687  * @brief Set the devclass of a device and mark the devclass fixed.
2688  * @see device_set_devclass()
2689  */
2690 int
2691 device_set_devclass_fixed(device_t dev, const char *classname)
2692 {
2693 	int error;
2694 
2695 	if (classname == NULL)
2696 		return (EINVAL);
2697 
2698 	error = device_set_devclass(dev, classname);
2699 	if (error)
2700 		return (error);
2701 	dev->flags |= DF_FIXEDCLASS;
2702 	return (0);
2703 }
2704 
2705 /**
2706  * @brief Set the driver of a device
2707  *
2708  * @retval 0		success
2709  * @retval EBUSY	the device already has a driver attached
2710  * @retval ENOMEM	a memory allocation failure occurred
2711  */
2712 int
2713 device_set_driver(device_t dev, driver_t *driver)
2714 {
2715 	if (dev->state >= DS_ATTACHED)
2716 		return (EBUSY);
2717 
2718 	if (dev->driver == driver)
2719 		return (0);
2720 
2721 	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2722 		free(dev->softc, M_BUS_SC);
2723 		dev->softc = NULL;
2724 	}
2725 	device_set_desc(dev, NULL);
2726 	kobj_delete((kobj_t) dev, NULL);
2727 	dev->driver = driver;
2728 	if (driver) {
2729 		kobj_init((kobj_t) dev, (kobj_class_t) driver);
2730 		if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2731 			dev->softc = malloc(driver->size, M_BUS_SC,
2732 			    M_NOWAIT | M_ZERO);
2733 			if (!dev->softc) {
2734 				kobj_delete((kobj_t) dev, NULL);
2735 				kobj_init((kobj_t) dev, &null_class);
2736 				dev->driver = NULL;
2737 				return (ENOMEM);
2738 			}
2739 		}
2740 	} else {
2741 		kobj_init((kobj_t) dev, &null_class);
2742 	}
2743 
2744 	bus_data_generation_update();
2745 	return (0);
2746 }
2747 
2748 /**
2749  * @brief Probe a device, and return this status.
2750  *
2751  * This function is the core of the device autoconfiguration
2752  * system. Its purpose is to select a suitable driver for a device and
2753  * then call that driver to initialise the hardware appropriately. The
2754  * driver is selected by calling the DEVICE_PROBE() method of a set of
2755  * candidate drivers and then choosing the driver which returned the
2756  * best value. This driver is then attached to the device using
2757  * device_attach().
2758  *
2759  * The set of suitable drivers is taken from the list of drivers in
2760  * the parent device's devclass. If the device was originally created
2761  * with a specific class name (see device_add_child()), only drivers
2762  * with that name are probed, otherwise all drivers in the devclass
2763  * are probed. If no drivers return successful probe values in the
2764  * parent devclass, the search continues in the parent of that
2765  * devclass (see devclass_get_parent()) if any.
2766  *
2767  * @param dev		the device to initialise
2768  *
2769  * @retval 0		success
2770  * @retval ENXIO	no driver was found
2771  * @retval ENOMEM	memory allocation failure
2772  * @retval non-zero	some other unix error code
2773  * @retval -1		Device already attached
2774  */
2775 int
2776 device_probe(device_t dev)
2777 {
2778 	int error;
2779 
2780 	GIANT_REQUIRED;
2781 
2782 	if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2783 		return (-1);
2784 
2785 	if (!(dev->flags & DF_ENABLED)) {
2786 		if (bootverbose && device_get_name(dev) != NULL) {
2787 			device_print_prettyname(dev);
2788 			printf("not probed (disabled)\n");
2789 		}
2790 		return (-1);
2791 	}
2792 	if ((error = device_probe_child(dev->parent, dev)) != 0) {
2793 		if (bus_current_pass == BUS_PASS_DEFAULT &&
2794 		    !(dev->flags & DF_DONENOMATCH)) {
2795 			BUS_PROBE_NOMATCH(dev->parent, dev);
2796 			devnomatch(dev);
2797 			dev->flags |= DF_DONENOMATCH;
2798 		}
2799 		return (error);
2800 	}
2801 	return (0);
2802 }
2803 
2804 /**
2805  * @brief Probe a device and attach a driver if possible
2806  *
2807  * calls device_probe() and attaches if that was successful.
2808  */
2809 int
2810 device_probe_and_attach(device_t dev)
2811 {
2812 	int error;
2813 
2814 	GIANT_REQUIRED;
2815 
2816 	error = device_probe(dev);
2817 	if (error == -1)
2818 		return (0);
2819 	else if (error != 0)
2820 		return (error);
2821 
2822 	CURVNET_SET_QUIET(vnet0);
2823 	error = device_attach(dev);
2824 	CURVNET_RESTORE();
2825 	return error;
2826 }
2827 
2828 /**
2829  * @brief Attach a device driver to a device
2830  *
2831  * This function is a wrapper around the DEVICE_ATTACH() driver
2832  * method. In addition to calling DEVICE_ATTACH(), it initialises the
2833  * device's sysctl tree, optionally prints a description of the device
2834  * and queues a notification event for user-based device management
2835  * services.
2836  *
2837  * Normally this function is only called internally from
2838  * device_probe_and_attach().
2839  *
2840  * @param dev		the device to initialise
2841  *
2842  * @retval 0		success
2843  * @retval ENXIO	no driver was found
2844  * @retval ENOMEM	memory allocation failure
2845  * @retval non-zero	some other unix error code
2846  */
2847 int
2848 device_attach(device_t dev)
2849 {
2850 	uint64_t attachtime;
2851 	int error;
2852 
2853 	if (resource_disabled(dev->driver->name, dev->unit)) {
2854 		device_disable(dev);
2855 		if (bootverbose)
2856 			 device_printf(dev, "disabled via hints entry\n");
2857 		return (ENXIO);
2858 	}
2859 
2860 	device_sysctl_init(dev);
2861 	if (!device_is_quiet(dev))
2862 		device_print_child(dev->parent, dev);
2863 	attachtime = get_cyclecount();
2864 	dev->state = DS_ATTACHING;
2865 	if ((error = DEVICE_ATTACH(dev)) != 0) {
2866 		printf("device_attach: %s%d attach returned %d\n",
2867 		    dev->driver->name, dev->unit, error);
2868 		if (!(dev->flags & DF_FIXEDCLASS))
2869 			devclass_delete_device(dev->devclass, dev);
2870 		(void)device_set_driver(dev, NULL);
2871 		device_sysctl_fini(dev);
2872 		KASSERT(dev->busy == 0, ("attach failed but busy"));
2873 		dev->state = DS_NOTPRESENT;
2874 		return (error);
2875 	}
2876 	attachtime = get_cyclecount() - attachtime;
2877 	/*
2878 	 * 4 bits per device is a reasonable value for desktop and server
2879 	 * hardware with good get_cyclecount() implementations, but WILL
2880 	 * need to be adjusted on other platforms.
2881 	 */
2882 #define	RANDOM_PROBE_BIT_GUESS	4
2883 	if (bootverbose)
2884 		printf("random: harvesting attach, %zu bytes (%d bits) from %s%d\n",
2885 		    sizeof(attachtime), RANDOM_PROBE_BIT_GUESS,
2886 		    dev->driver->name, dev->unit);
2887 	random_harvest_direct(&attachtime, sizeof(attachtime),
2888 	    RANDOM_PROBE_BIT_GUESS, RANDOM_ATTACH);
2889 	device_sysctl_update(dev);
2890 	if (dev->busy)
2891 		dev->state = DS_BUSY;
2892 	else
2893 		dev->state = DS_ATTACHED;
2894 	dev->flags &= ~DF_DONENOMATCH;
2895 	devadded(dev);
2896 	return (0);
2897 }
2898 
2899 /**
2900  * @brief Detach a driver from a device
2901  *
2902  * This function is a wrapper around the DEVICE_DETACH() driver
2903  * method. If the call to DEVICE_DETACH() succeeds, it calls
2904  * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2905  * notification event for user-based device management services and
2906  * cleans up the device's sysctl tree.
2907  *
2908  * @param dev		the device to un-initialise
2909  *
2910  * @retval 0		success
2911  * @retval ENXIO	no driver was found
2912  * @retval ENOMEM	memory allocation failure
2913  * @retval non-zero	some other unix error code
2914  */
2915 int
2916 device_detach(device_t dev)
2917 {
2918 	int error;
2919 
2920 	GIANT_REQUIRED;
2921 
2922 	PDEBUG(("%s", DEVICENAME(dev)));
2923 	if (dev->state == DS_BUSY)
2924 		return (EBUSY);
2925 	if (dev->state != DS_ATTACHED)
2926 		return (0);
2927 
2928 	if ((error = DEVICE_DETACH(dev)) != 0)
2929 		return (error);
2930 	devremoved(dev);
2931 	if (!device_is_quiet(dev))
2932 		device_printf(dev, "detached\n");
2933 	if (dev->parent)
2934 		BUS_CHILD_DETACHED(dev->parent, dev);
2935 
2936 	if (!(dev->flags & DF_FIXEDCLASS))
2937 		devclass_delete_device(dev->devclass, dev);
2938 
2939 	dev->state = DS_NOTPRESENT;
2940 	(void)device_set_driver(dev, NULL);
2941 	device_sysctl_fini(dev);
2942 
2943 	return (0);
2944 }
2945 
2946 /**
2947  * @brief Tells a driver to quiesce itself.
2948  *
2949  * This function is a wrapper around the DEVICE_QUIESCE() driver
2950  * method. If the call to DEVICE_QUIESCE() succeeds.
2951  *
2952  * @param dev		the device to quiesce
2953  *
2954  * @retval 0		success
2955  * @retval ENXIO	no driver was found
2956  * @retval ENOMEM	memory allocation failure
2957  * @retval non-zero	some other unix error code
2958  */
2959 int
2960 device_quiesce(device_t dev)
2961 {
2962 
2963 	PDEBUG(("%s", DEVICENAME(dev)));
2964 	if (dev->state == DS_BUSY)
2965 		return (EBUSY);
2966 	if (dev->state != DS_ATTACHED)
2967 		return (0);
2968 
2969 	return (DEVICE_QUIESCE(dev));
2970 }
2971 
2972 /**
2973  * @brief Notify a device of system shutdown
2974  *
2975  * This function calls the DEVICE_SHUTDOWN() driver method if the
2976  * device currently has an attached driver.
2977  *
2978  * @returns the value returned by DEVICE_SHUTDOWN()
2979  */
2980 int
2981 device_shutdown(device_t dev)
2982 {
2983 	if (dev->state < DS_ATTACHED)
2984 		return (0);
2985 	return (DEVICE_SHUTDOWN(dev));
2986 }
2987 
2988 /**
2989  * @brief Set the unit number of a device
2990  *
2991  * This function can be used to override the unit number used for a
2992  * device (e.g. to wire a device to a pre-configured unit number).
2993  */
2994 int
2995 device_set_unit(device_t dev, int unit)
2996 {
2997 	devclass_t dc;
2998 	int err;
2999 
3000 	dc = device_get_devclass(dev);
3001 	if (unit < dc->maxunit && dc->devices[unit])
3002 		return (EBUSY);
3003 	err = devclass_delete_device(dc, dev);
3004 	if (err)
3005 		return (err);
3006 	dev->unit = unit;
3007 	err = devclass_add_device(dc, dev);
3008 	if (err)
3009 		return (err);
3010 
3011 	bus_data_generation_update();
3012 	return (0);
3013 }
3014 
3015 /*======================================*/
3016 /*
3017  * Some useful method implementations to make life easier for bus drivers.
3018  */
3019 
3020 /**
3021  * @brief Initialise a resource list.
3022  *
3023  * @param rl		the resource list to initialise
3024  */
3025 void
3026 resource_list_init(struct resource_list *rl)
3027 {
3028 	STAILQ_INIT(rl);
3029 }
3030 
3031 /**
3032  * @brief Reclaim memory used by a resource list.
3033  *
3034  * This function frees the memory for all resource entries on the list
3035  * (if any).
3036  *
3037  * @param rl		the resource list to free
3038  */
3039 void
3040 resource_list_free(struct resource_list *rl)
3041 {
3042 	struct resource_list_entry *rle;
3043 
3044 	while ((rle = STAILQ_FIRST(rl)) != NULL) {
3045 		if (rle->res)
3046 			panic("resource_list_free: resource entry is busy");
3047 		STAILQ_REMOVE_HEAD(rl, link);
3048 		free(rle, M_BUS);
3049 	}
3050 }
3051 
3052 /**
3053  * @brief Add a resource entry.
3054  *
3055  * This function adds a resource entry using the given @p type, @p
3056  * start, @p end and @p count values. A rid value is chosen by
3057  * searching sequentially for the first unused rid starting at zero.
3058  *
3059  * @param rl		the resource list to edit
3060  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3061  * @param start		the start address of the resource
3062  * @param end		the end address of the resource
3063  * @param count		XXX end-start+1
3064  */
3065 int
3066 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start,
3067     rman_res_t end, rman_res_t count)
3068 {
3069 	int rid;
3070 
3071 	rid = 0;
3072 	while (resource_list_find(rl, type, rid) != NULL)
3073 		rid++;
3074 	resource_list_add(rl, type, rid, start, end, count);
3075 	return (rid);
3076 }
3077 
3078 /**
3079  * @brief Add or modify a resource entry.
3080  *
3081  * If an existing entry exists with the same type and rid, it will be
3082  * modified using the given values of @p start, @p end and @p
3083  * count. If no entry exists, a new one will be created using the
3084  * given values.  The resource list entry that matches is then returned.
3085  *
3086  * @param rl		the resource list to edit
3087  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3088  * @param rid		the resource identifier
3089  * @param start		the start address of the resource
3090  * @param end		the end address of the resource
3091  * @param count		XXX end-start+1
3092  */
3093 struct resource_list_entry *
3094 resource_list_add(struct resource_list *rl, int type, int rid,
3095     rman_res_t start, rman_res_t end, rman_res_t count)
3096 {
3097 	struct resource_list_entry *rle;
3098 
3099 	rle = resource_list_find(rl, type, rid);
3100 	if (!rle) {
3101 		rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3102 		    M_NOWAIT);
3103 		if (!rle)
3104 			panic("resource_list_add: can't record entry");
3105 		STAILQ_INSERT_TAIL(rl, rle, link);
3106 		rle->type = type;
3107 		rle->rid = rid;
3108 		rle->res = NULL;
3109 		rle->flags = 0;
3110 	}
3111 
3112 	if (rle->res)
3113 		panic("resource_list_add: resource entry is busy");
3114 
3115 	rle->start = start;
3116 	rle->end = end;
3117 	rle->count = count;
3118 	return (rle);
3119 }
3120 
3121 /**
3122  * @brief Determine if a resource entry is busy.
3123  *
3124  * Returns true if a resource entry is busy meaning that it has an
3125  * associated resource that is not an unallocated "reserved" resource.
3126  *
3127  * @param rl		the resource list to search
3128  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3129  * @param rid		the resource identifier
3130  *
3131  * @returns Non-zero if the entry is busy, zero otherwise.
3132  */
3133 int
3134 resource_list_busy(struct resource_list *rl, int type, int rid)
3135 {
3136 	struct resource_list_entry *rle;
3137 
3138 	rle = resource_list_find(rl, type, rid);
3139 	if (rle == NULL || rle->res == NULL)
3140 		return (0);
3141 	if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3142 		KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3143 		    ("reserved resource is active"));
3144 		return (0);
3145 	}
3146 	return (1);
3147 }
3148 
3149 /**
3150  * @brief Determine if a resource entry is reserved.
3151  *
3152  * Returns true if a resource entry is reserved meaning that it has an
3153  * associated "reserved" resource.  The resource can either be
3154  * allocated or unallocated.
3155  *
3156  * @param rl		the resource list to search
3157  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3158  * @param rid		the resource identifier
3159  *
3160  * @returns Non-zero if the entry is reserved, zero otherwise.
3161  */
3162 int
3163 resource_list_reserved(struct resource_list *rl, int type, int rid)
3164 {
3165 	struct resource_list_entry *rle;
3166 
3167 	rle = resource_list_find(rl, type, rid);
3168 	if (rle != NULL && rle->flags & RLE_RESERVED)
3169 		return (1);
3170 	return (0);
3171 }
3172 
3173 /**
3174  * @brief Find a resource entry by type and rid.
3175  *
3176  * @param rl		the resource list to search
3177  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3178  * @param rid		the resource identifier
3179  *
3180  * @returns the resource entry pointer or NULL if there is no such
3181  * entry.
3182  */
3183 struct resource_list_entry *
3184 resource_list_find(struct resource_list *rl, int type, int rid)
3185 {
3186 	struct resource_list_entry *rle;
3187 
3188 	STAILQ_FOREACH(rle, rl, link) {
3189 		if (rle->type == type && rle->rid == rid)
3190 			return (rle);
3191 	}
3192 	return (NULL);
3193 }
3194 
3195 /**
3196  * @brief Delete a resource entry.
3197  *
3198  * @param rl		the resource list to edit
3199  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3200  * @param rid		the resource identifier
3201  */
3202 void
3203 resource_list_delete(struct resource_list *rl, int type, int rid)
3204 {
3205 	struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3206 
3207 	if (rle) {
3208 		if (rle->res != NULL)
3209 			panic("resource_list_delete: resource has not been released");
3210 		STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3211 		free(rle, M_BUS);
3212 	}
3213 }
3214 
3215 /**
3216  * @brief Allocate a reserved resource
3217  *
3218  * This can be used by busses to force the allocation of resources
3219  * that are always active in the system even if they are not allocated
3220  * by a driver (e.g. PCI BARs).  This function is usually called when
3221  * adding a new child to the bus.  The resource is allocated from the
3222  * parent bus when it is reserved.  The resource list entry is marked
3223  * with RLE_RESERVED to note that it is a reserved resource.
3224  *
3225  * Subsequent attempts to allocate the resource with
3226  * resource_list_alloc() will succeed the first time and will set
3227  * RLE_ALLOCATED to note that it has been allocated.  When a reserved
3228  * resource that has been allocated is released with
3229  * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3230  * the actual resource remains allocated.  The resource can be released to
3231  * the parent bus by calling resource_list_unreserve().
3232  *
3233  * @param rl		the resource list to allocate from
3234  * @param bus		the parent device of @p child
3235  * @param child		the device for which the resource is being reserved
3236  * @param type		the type of resource to allocate
3237  * @param rid		a pointer to the resource identifier
3238  * @param start		hint at the start of the resource range - pass
3239  *			@c 0UL for any start address
3240  * @param end		hint at the end of the resource range - pass
3241  *			@c ~0UL for any end address
3242  * @param count		hint at the size of range required - pass @c 1
3243  *			for any size
3244  * @param flags		any extra flags to control the resource
3245  *			allocation - see @c RF_XXX flags in
3246  *			<sys/rman.h> for details
3247  *
3248  * @returns		the resource which was allocated or @c NULL if no
3249  *			resource could be allocated
3250  */
3251 struct resource *
3252 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3253     int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3254 {
3255 	struct resource_list_entry *rle = NULL;
3256 	int passthrough = (device_get_parent(child) != bus);
3257 	struct resource *r;
3258 
3259 	if (passthrough)
3260 		panic(
3261     "resource_list_reserve() should only be called for direct children");
3262 	if (flags & RF_ACTIVE)
3263 		panic(
3264     "resource_list_reserve() should only reserve inactive resources");
3265 
3266 	r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3267 	    flags);
3268 	if (r != NULL) {
3269 		rle = resource_list_find(rl, type, *rid);
3270 		rle->flags |= RLE_RESERVED;
3271 	}
3272 	return (r);
3273 }
3274 
3275 /**
3276  * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3277  *
3278  * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3279  * and passing the allocation up to the parent of @p bus. This assumes
3280  * that the first entry of @c device_get_ivars(child) is a struct
3281  * resource_list. This also handles 'passthrough' allocations where a
3282  * child is a remote descendant of bus by passing the allocation up to
3283  * the parent of bus.
3284  *
3285  * Typically, a bus driver would store a list of child resources
3286  * somewhere in the child device's ivars (see device_get_ivars()) and
3287  * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3288  * then call resource_list_alloc() to perform the allocation.
3289  *
3290  * @param rl		the resource list to allocate from
3291  * @param bus		the parent device of @p child
3292  * @param child		the device which is requesting an allocation
3293  * @param type		the type of resource to allocate
3294  * @param rid		a pointer to the resource identifier
3295  * @param start		hint at the start of the resource range - pass
3296  *			@c 0UL for any start address
3297  * @param end		hint at the end of the resource range - pass
3298  *			@c ~0UL for any end address
3299  * @param count		hint at the size of range required - pass @c 1
3300  *			for any size
3301  * @param flags		any extra flags to control the resource
3302  *			allocation - see @c RF_XXX flags in
3303  *			<sys/rman.h> for details
3304  *
3305  * @returns		the resource which was allocated or @c NULL if no
3306  *			resource could be allocated
3307  */
3308 struct resource *
3309 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3310     int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3311 {
3312 	struct resource_list_entry *rle = NULL;
3313 	int passthrough = (device_get_parent(child) != bus);
3314 	int isdefault = (start == 0UL && end == ~0UL);
3315 
3316 	if (passthrough) {
3317 		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3318 		    type, rid, start, end, count, flags));
3319 	}
3320 
3321 	rle = resource_list_find(rl, type, *rid);
3322 
3323 	if (!rle)
3324 		return (NULL);		/* no resource of that type/rid */
3325 
3326 	if (rle->res) {
3327 		if (rle->flags & RLE_RESERVED) {
3328 			if (rle->flags & RLE_ALLOCATED)
3329 				return (NULL);
3330 			if ((flags & RF_ACTIVE) &&
3331 			    bus_activate_resource(child, type, *rid,
3332 			    rle->res) != 0)
3333 				return (NULL);
3334 			rle->flags |= RLE_ALLOCATED;
3335 			return (rle->res);
3336 		}
3337 		device_printf(bus,
3338 		    "resource entry %#x type %d for child %s is busy\n", *rid,
3339 		    type, device_get_nameunit(child));
3340 		return (NULL);
3341 	}
3342 
3343 	if (isdefault) {
3344 		start = rle->start;
3345 		count = ulmax(count, rle->count);
3346 		end = ulmax(rle->end, start + count - 1);
3347 	}
3348 
3349 	rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3350 	    type, rid, start, end, count, flags);
3351 
3352 	/*
3353 	 * Record the new range.
3354 	 */
3355 	if (rle->res) {
3356 		rle->start = rman_get_start(rle->res);
3357 		rle->end = rman_get_end(rle->res);
3358 		rle->count = count;
3359 	}
3360 
3361 	return (rle->res);
3362 }
3363 
3364 /**
3365  * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3366  *
3367  * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3368  * used with resource_list_alloc().
3369  *
3370  * @param rl		the resource list which was allocated from
3371  * @param bus		the parent device of @p child
3372  * @param child		the device which is requesting a release
3373  * @param type		the type of resource to release
3374  * @param rid		the resource identifier
3375  * @param res		the resource to release
3376  *
3377  * @retval 0		success
3378  * @retval non-zero	a standard unix error code indicating what
3379  *			error condition prevented the operation
3380  */
3381 int
3382 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3383     int type, int rid, struct resource *res)
3384 {
3385 	struct resource_list_entry *rle = NULL;
3386 	int passthrough = (device_get_parent(child) != bus);
3387 	int error;
3388 
3389 	if (passthrough) {
3390 		return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3391 		    type, rid, res));
3392 	}
3393 
3394 	rle = resource_list_find(rl, type, rid);
3395 
3396 	if (!rle)
3397 		panic("resource_list_release: can't find resource");
3398 	if (!rle->res)
3399 		panic("resource_list_release: resource entry is not busy");
3400 	if (rle->flags & RLE_RESERVED) {
3401 		if (rle->flags & RLE_ALLOCATED) {
3402 			if (rman_get_flags(res) & RF_ACTIVE) {
3403 				error = bus_deactivate_resource(child, type,
3404 				    rid, res);
3405 				if (error)
3406 					return (error);
3407 			}
3408 			rle->flags &= ~RLE_ALLOCATED;
3409 			return (0);
3410 		}
3411 		return (EINVAL);
3412 	}
3413 
3414 	error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3415 	    type, rid, res);
3416 	if (error)
3417 		return (error);
3418 
3419 	rle->res = NULL;
3420 	return (0);
3421 }
3422 
3423 /**
3424  * @brief Release all active resources of a given type
3425  *
3426  * Release all active resources of a specified type.  This is intended
3427  * to be used to cleanup resources leaked by a driver after detach or
3428  * a failed attach.
3429  *
3430  * @param rl		the resource list which was allocated from
3431  * @param bus		the parent device of @p child
3432  * @param child		the device whose active resources are being released
3433  * @param type		the type of resources to release
3434  *
3435  * @retval 0		success
3436  * @retval EBUSY	at least one resource was active
3437  */
3438 int
3439 resource_list_release_active(struct resource_list *rl, device_t bus,
3440     device_t child, int type)
3441 {
3442 	struct resource_list_entry *rle;
3443 	int error, retval;
3444 
3445 	retval = 0;
3446 	STAILQ_FOREACH(rle, rl, link) {
3447 		if (rle->type != type)
3448 			continue;
3449 		if (rle->res == NULL)
3450 			continue;
3451 		if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3452 		    RLE_RESERVED)
3453 			continue;
3454 		retval = EBUSY;
3455 		error = resource_list_release(rl, bus, child, type,
3456 		    rman_get_rid(rle->res), rle->res);
3457 		if (error != 0)
3458 			device_printf(bus,
3459 			    "Failed to release active resource: %d\n", error);
3460 	}
3461 	return (retval);
3462 }
3463 
3464 
3465 /**
3466  * @brief Fully release a reserved resource
3467  *
3468  * Fully releases a resource reserved via resource_list_reserve().
3469  *
3470  * @param rl		the resource list which was allocated from
3471  * @param bus		the parent device of @p child
3472  * @param child		the device whose reserved resource is being released
3473  * @param type		the type of resource to release
3474  * @param rid		the resource identifier
3475  * @param res		the resource to release
3476  *
3477  * @retval 0		success
3478  * @retval non-zero	a standard unix error code indicating what
3479  *			error condition prevented the operation
3480  */
3481 int
3482 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3483     int type, int rid)
3484 {
3485 	struct resource_list_entry *rle = NULL;
3486 	int passthrough = (device_get_parent(child) != bus);
3487 
3488 	if (passthrough)
3489 		panic(
3490     "resource_list_unreserve() should only be called for direct children");
3491 
3492 	rle = resource_list_find(rl, type, rid);
3493 
3494 	if (!rle)
3495 		panic("resource_list_unreserve: can't find resource");
3496 	if (!(rle->flags & RLE_RESERVED))
3497 		return (EINVAL);
3498 	if (rle->flags & RLE_ALLOCATED)
3499 		return (EBUSY);
3500 	rle->flags &= ~RLE_RESERVED;
3501 	return (resource_list_release(rl, bus, child, type, rid, rle->res));
3502 }
3503 
3504 /**
3505  * @brief Print a description of resources in a resource list
3506  *
3507  * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3508  * The name is printed if at least one resource of the given type is available.
3509  * The format is used to print resource start and end.
3510  *
3511  * @param rl		the resource list to print
3512  * @param name		the name of @p type, e.g. @c "memory"
3513  * @param type		type type of resource entry to print
3514  * @param format	printf(9) format string to print resource
3515  *			start and end values
3516  *
3517  * @returns		the number of characters printed
3518  */
3519 int
3520 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3521     const char *format)
3522 {
3523 	struct resource_list_entry *rle;
3524 	int printed, retval;
3525 
3526 	printed = 0;
3527 	retval = 0;
3528 	/* Yes, this is kinda cheating */
3529 	STAILQ_FOREACH(rle, rl, link) {
3530 		if (rle->type == type) {
3531 			if (printed == 0)
3532 				retval += printf(" %s ", name);
3533 			else
3534 				retval += printf(",");
3535 			printed++;
3536 			retval += printf(format, rle->start);
3537 			if (rle->count > 1) {
3538 				retval += printf("-");
3539 				retval += printf(format, rle->start +
3540 						 rle->count - 1);
3541 			}
3542 		}
3543 	}
3544 	return (retval);
3545 }
3546 
3547 /**
3548  * @brief Releases all the resources in a list.
3549  *
3550  * @param rl		The resource list to purge.
3551  *
3552  * @returns		nothing
3553  */
3554 void
3555 resource_list_purge(struct resource_list *rl)
3556 {
3557 	struct resource_list_entry *rle;
3558 
3559 	while ((rle = STAILQ_FIRST(rl)) != NULL) {
3560 		if (rle->res)
3561 			bus_release_resource(rman_get_device(rle->res),
3562 			    rle->type, rle->rid, rle->res);
3563 		STAILQ_REMOVE_HEAD(rl, link);
3564 		free(rle, M_BUS);
3565 	}
3566 }
3567 
3568 device_t
3569 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3570 {
3571 
3572 	return (device_add_child_ordered(dev, order, name, unit));
3573 }
3574 
3575 /**
3576  * @brief Helper function for implementing DEVICE_PROBE()
3577  *
3578  * This function can be used to help implement the DEVICE_PROBE() for
3579  * a bus (i.e. a device which has other devices attached to it). It
3580  * calls the DEVICE_IDENTIFY() method of each driver in the device's
3581  * devclass.
3582  */
3583 int
3584 bus_generic_probe(device_t dev)
3585 {
3586 	devclass_t dc = dev->devclass;
3587 	driverlink_t dl;
3588 
3589 	TAILQ_FOREACH(dl, &dc->drivers, link) {
3590 		/*
3591 		 * If this driver's pass is too high, then ignore it.
3592 		 * For most drivers in the default pass, this will
3593 		 * never be true.  For early-pass drivers they will
3594 		 * only call the identify routines of eligible drivers
3595 		 * when this routine is called.  Drivers for later
3596 		 * passes should have their identify routines called
3597 		 * on early-pass busses during BUS_NEW_PASS().
3598 		 */
3599 		if (dl->pass > bus_current_pass)
3600 			continue;
3601 		DEVICE_IDENTIFY(dl->driver, dev);
3602 	}
3603 
3604 	return (0);
3605 }
3606 
3607 /**
3608  * @brief Helper function for implementing DEVICE_ATTACH()
3609  *
3610  * This function can be used to help implement the DEVICE_ATTACH() for
3611  * a bus. It calls device_probe_and_attach() for each of the device's
3612  * children.
3613  */
3614 int
3615 bus_generic_attach(device_t dev)
3616 {
3617 	device_t child;
3618 
3619 	TAILQ_FOREACH(child, &dev->children, link) {
3620 		device_probe_and_attach(child);
3621 	}
3622 
3623 	return (0);
3624 }
3625 
3626 /**
3627  * @brief Helper function for implementing DEVICE_DETACH()
3628  *
3629  * This function can be used to help implement the DEVICE_DETACH() for
3630  * a bus. It calls device_detach() for each of the device's
3631  * children.
3632  */
3633 int
3634 bus_generic_detach(device_t dev)
3635 {
3636 	device_t child;
3637 	int error;
3638 
3639 	if (dev->state != DS_ATTACHED)
3640 		return (EBUSY);
3641 
3642 	TAILQ_FOREACH(child, &dev->children, link) {
3643 		if ((error = device_detach(child)) != 0)
3644 			return (error);
3645 	}
3646 
3647 	return (0);
3648 }
3649 
3650 /**
3651  * @brief Helper function for implementing DEVICE_SHUTDOWN()
3652  *
3653  * This function can be used to help implement the DEVICE_SHUTDOWN()
3654  * for a bus. It calls device_shutdown() for each of the device's
3655  * children.
3656  */
3657 int
3658 bus_generic_shutdown(device_t dev)
3659 {
3660 	device_t child;
3661 
3662 	TAILQ_FOREACH(child, &dev->children, link) {
3663 		device_shutdown(child);
3664 	}
3665 
3666 	return (0);
3667 }
3668 
3669 /**
3670  * @brief Default function for suspending a child device.
3671  *
3672  * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3673  */
3674 int
3675 bus_generic_suspend_child(device_t dev, device_t child)
3676 {
3677 	int	error;
3678 
3679 	error = DEVICE_SUSPEND(child);
3680 
3681 	if (error == 0)
3682 		child->flags |= DF_SUSPENDED;
3683 
3684 	return (error);
3685 }
3686 
3687 /**
3688  * @brief Default function for resuming a child device.
3689  *
3690  * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3691  */
3692 int
3693 bus_generic_resume_child(device_t dev, device_t child)
3694 {
3695 
3696 	DEVICE_RESUME(child);
3697 	child->flags &= ~DF_SUSPENDED;
3698 
3699 	return (0);
3700 }
3701 
3702 /**
3703  * @brief Helper function for implementing DEVICE_SUSPEND()
3704  *
3705  * This function can be used to help implement the DEVICE_SUSPEND()
3706  * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3707  * children. If any call to DEVICE_SUSPEND() fails, the suspend
3708  * operation is aborted and any devices which were suspended are
3709  * resumed immediately by calling their DEVICE_RESUME() methods.
3710  */
3711 int
3712 bus_generic_suspend(device_t dev)
3713 {
3714 	int		error;
3715 	device_t	child, child2;
3716 
3717 	TAILQ_FOREACH(child, &dev->children, link) {
3718 		error = BUS_SUSPEND_CHILD(dev, child);
3719 		if (error) {
3720 			for (child2 = TAILQ_FIRST(&dev->children);
3721 			     child2 && child2 != child;
3722 			     child2 = TAILQ_NEXT(child2, link))
3723 				BUS_RESUME_CHILD(dev, child2);
3724 			return (error);
3725 		}
3726 	}
3727 	return (0);
3728 }
3729 
3730 /**
3731  * @brief Helper function for implementing DEVICE_RESUME()
3732  *
3733  * This function can be used to help implement the DEVICE_RESUME() for
3734  * a bus. It calls DEVICE_RESUME() on each of the device's children.
3735  */
3736 int
3737 bus_generic_resume(device_t dev)
3738 {
3739 	device_t	child;
3740 
3741 	TAILQ_FOREACH(child, &dev->children, link) {
3742 		BUS_RESUME_CHILD(dev, child);
3743 		/* if resume fails, there's nothing we can usefully do... */
3744 	}
3745 	return (0);
3746 }
3747 
3748 /**
3749  * @brief Helper function for implementing BUS_PRINT_CHILD().
3750  *
3751  * This function prints the first part of the ascii representation of
3752  * @p child, including its name, unit and description (if any - see
3753  * device_set_desc()).
3754  *
3755  * @returns the number of characters printed
3756  */
3757 int
3758 bus_print_child_header(device_t dev, device_t child)
3759 {
3760 	int	retval = 0;
3761 
3762 	if (device_get_desc(child)) {
3763 		retval += device_printf(child, "<%s>", device_get_desc(child));
3764 	} else {
3765 		retval += printf("%s", device_get_nameunit(child));
3766 	}
3767 
3768 	return (retval);
3769 }
3770 
3771 /**
3772  * @brief Helper function for implementing BUS_PRINT_CHILD().
3773  *
3774  * This function prints the last part of the ascii representation of
3775  * @p child, which consists of the string @c " on " followed by the
3776  * name and unit of the @p dev.
3777  *
3778  * @returns the number of characters printed
3779  */
3780 int
3781 bus_print_child_footer(device_t dev, device_t child)
3782 {
3783 	return (printf(" on %s\n", device_get_nameunit(dev)));
3784 }
3785 
3786 /**
3787  * @brief Helper function for implementing BUS_PRINT_CHILD().
3788  *
3789  * This function prints out the VM domain for the given device.
3790  *
3791  * @returns the number of characters printed
3792  */
3793 int
3794 bus_print_child_domain(device_t dev, device_t child)
3795 {
3796 	int domain;
3797 
3798 	/* No domain? Don't print anything */
3799 	if (BUS_GET_DOMAIN(dev, child, &domain) != 0)
3800 		return (0);
3801 
3802 	return (printf(" numa-domain %d", domain));
3803 }
3804 
3805 /**
3806  * @brief Helper function for implementing BUS_PRINT_CHILD().
3807  *
3808  * This function simply calls bus_print_child_header() followed by
3809  * bus_print_child_footer().
3810  *
3811  * @returns the number of characters printed
3812  */
3813 int
3814 bus_generic_print_child(device_t dev, device_t child)
3815 {
3816 	int	retval = 0;
3817 
3818 	retval += bus_print_child_header(dev, child);
3819 	retval += bus_print_child_domain(dev, child);
3820 	retval += bus_print_child_footer(dev, child);
3821 
3822 	return (retval);
3823 }
3824 
3825 /**
3826  * @brief Stub function for implementing BUS_READ_IVAR().
3827  *
3828  * @returns ENOENT
3829  */
3830 int
3831 bus_generic_read_ivar(device_t dev, device_t child, int index,
3832     uintptr_t * result)
3833 {
3834 	return (ENOENT);
3835 }
3836 
3837 /**
3838  * @brief Stub function for implementing BUS_WRITE_IVAR().
3839  *
3840  * @returns ENOENT
3841  */
3842 int
3843 bus_generic_write_ivar(device_t dev, device_t child, int index,
3844     uintptr_t value)
3845 {
3846 	return (ENOENT);
3847 }
3848 
3849 /**
3850  * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3851  *
3852  * @returns NULL
3853  */
3854 struct resource_list *
3855 bus_generic_get_resource_list(device_t dev, device_t child)
3856 {
3857 	return (NULL);
3858 }
3859 
3860 /**
3861  * @brief Helper function for implementing BUS_DRIVER_ADDED().
3862  *
3863  * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3864  * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3865  * and then calls device_probe_and_attach() for each unattached child.
3866  */
3867 void
3868 bus_generic_driver_added(device_t dev, driver_t *driver)
3869 {
3870 	device_t child;
3871 
3872 	DEVICE_IDENTIFY(driver, dev);
3873 	TAILQ_FOREACH(child, &dev->children, link) {
3874 		if (child->state == DS_NOTPRESENT ||
3875 		    (child->flags & DF_REBID))
3876 			device_probe_and_attach(child);
3877 	}
3878 }
3879 
3880 /**
3881  * @brief Helper function for implementing BUS_NEW_PASS().
3882  *
3883  * This implementing of BUS_NEW_PASS() first calls the identify
3884  * routines for any drivers that probe at the current pass.  Then it
3885  * walks the list of devices for this bus.  If a device is already
3886  * attached, then it calls BUS_NEW_PASS() on that device.  If the
3887  * device is not already attached, it attempts to attach a driver to
3888  * it.
3889  */
3890 void
3891 bus_generic_new_pass(device_t dev)
3892 {
3893 	driverlink_t dl;
3894 	devclass_t dc;
3895 	device_t child;
3896 
3897 	dc = dev->devclass;
3898 	TAILQ_FOREACH(dl, &dc->drivers, link) {
3899 		if (dl->pass == bus_current_pass)
3900 			DEVICE_IDENTIFY(dl->driver, dev);
3901 	}
3902 	TAILQ_FOREACH(child, &dev->children, link) {
3903 		if (child->state >= DS_ATTACHED)
3904 			BUS_NEW_PASS(child);
3905 		else if (child->state == DS_NOTPRESENT)
3906 			device_probe_and_attach(child);
3907 	}
3908 }
3909 
3910 /**
3911  * @brief Helper function for implementing BUS_SETUP_INTR().
3912  *
3913  * This simple implementation of BUS_SETUP_INTR() simply calls the
3914  * BUS_SETUP_INTR() method of the parent of @p dev.
3915  */
3916 int
3917 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3918     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3919     void **cookiep)
3920 {
3921 	/* Propagate up the bus hierarchy until someone handles it. */
3922 	if (dev->parent)
3923 		return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3924 		    filter, intr, arg, cookiep));
3925 	return (EINVAL);
3926 }
3927 
3928 /**
3929  * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3930  *
3931  * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3932  * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3933  */
3934 int
3935 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3936     void *cookie)
3937 {
3938 	/* Propagate up the bus hierarchy until someone handles it. */
3939 	if (dev->parent)
3940 		return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3941 	return (EINVAL);
3942 }
3943 
3944 /**
3945  * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3946  *
3947  * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3948  * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3949  */
3950 int
3951 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3952     struct resource *r, rman_res_t start, rman_res_t end)
3953 {
3954 	/* Propagate up the bus hierarchy until someone handles it. */
3955 	if (dev->parent)
3956 		return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3957 		    end));
3958 	return (EINVAL);
3959 }
3960 
3961 /**
3962  * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3963  *
3964  * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3965  * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3966  */
3967 struct resource *
3968 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3969     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3970 {
3971 	/* Propagate up the bus hierarchy until someone handles it. */
3972 	if (dev->parent)
3973 		return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3974 		    start, end, count, flags));
3975 	return (NULL);
3976 }
3977 
3978 /**
3979  * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3980  *
3981  * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3982  * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3983  */
3984 int
3985 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3986     struct resource *r)
3987 {
3988 	/* Propagate up the bus hierarchy until someone handles it. */
3989 	if (dev->parent)
3990 		return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3991 		    r));
3992 	return (EINVAL);
3993 }
3994 
3995 /**
3996  * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3997  *
3998  * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3999  * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
4000  */
4001 int
4002 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
4003     struct resource *r)
4004 {
4005 	/* Propagate up the bus hierarchy until someone handles it. */
4006 	if (dev->parent)
4007 		return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
4008 		    r));
4009 	return (EINVAL);
4010 }
4011 
4012 /**
4013  * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
4014  *
4015  * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
4016  * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
4017  */
4018 int
4019 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
4020     int rid, struct resource *r)
4021 {
4022 	/* Propagate up the bus hierarchy until someone handles it. */
4023 	if (dev->parent)
4024 		return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
4025 		    r));
4026 	return (EINVAL);
4027 }
4028 
4029 /**
4030  * @brief Helper function for implementing BUS_BIND_INTR().
4031  *
4032  * This simple implementation of BUS_BIND_INTR() simply calls the
4033  * BUS_BIND_INTR() method of the parent of @p dev.
4034  */
4035 int
4036 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
4037     int cpu)
4038 {
4039 
4040 	/* Propagate up the bus hierarchy until someone handles it. */
4041 	if (dev->parent)
4042 		return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
4043 	return (EINVAL);
4044 }
4045 
4046 /**
4047  * @brief Helper function for implementing BUS_CONFIG_INTR().
4048  *
4049  * This simple implementation of BUS_CONFIG_INTR() simply calls the
4050  * BUS_CONFIG_INTR() method of the parent of @p dev.
4051  */
4052 int
4053 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
4054     enum intr_polarity pol)
4055 {
4056 
4057 	/* Propagate up the bus hierarchy until someone handles it. */
4058 	if (dev->parent)
4059 		return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
4060 	return (EINVAL);
4061 }
4062 
4063 /**
4064  * @brief Helper function for implementing BUS_DESCRIBE_INTR().
4065  *
4066  * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
4067  * BUS_DESCRIBE_INTR() method of the parent of @p dev.
4068  */
4069 int
4070 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
4071     void *cookie, const char *descr)
4072 {
4073 
4074 	/* Propagate up the bus hierarchy until someone handles it. */
4075 	if (dev->parent)
4076 		return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
4077 		    descr));
4078 	return (EINVAL);
4079 }
4080 
4081 /**
4082  * @brief Helper function for implementing BUS_GET_DMA_TAG().
4083  *
4084  * This simple implementation of BUS_GET_DMA_TAG() simply calls the
4085  * BUS_GET_DMA_TAG() method of the parent of @p dev.
4086  */
4087 bus_dma_tag_t
4088 bus_generic_get_dma_tag(device_t dev, device_t child)
4089 {
4090 
4091 	/* Propagate up the bus hierarchy until someone handles it. */
4092 	if (dev->parent != NULL)
4093 		return (BUS_GET_DMA_TAG(dev->parent, child));
4094 	return (NULL);
4095 }
4096 
4097 /**
4098  * @brief Helper function for implementing BUS_GET_RESOURCE().
4099  *
4100  * This implementation of BUS_GET_RESOURCE() uses the
4101  * resource_list_find() function to do most of the work. It calls
4102  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4103  * search.
4104  */
4105 int
4106 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4107     rman_res_t *startp, rman_res_t *countp)
4108 {
4109 	struct resource_list *		rl = NULL;
4110 	struct resource_list_entry *	rle = NULL;
4111 
4112 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4113 	if (!rl)
4114 		return (EINVAL);
4115 
4116 	rle = resource_list_find(rl, type, rid);
4117 	if (!rle)
4118 		return (ENOENT);
4119 
4120 	if (startp)
4121 		*startp = rle->start;
4122 	if (countp)
4123 		*countp = rle->count;
4124 
4125 	return (0);
4126 }
4127 
4128 /**
4129  * @brief Helper function for implementing BUS_SET_RESOURCE().
4130  *
4131  * This implementation of BUS_SET_RESOURCE() uses the
4132  * resource_list_add() function to do most of the work. It calls
4133  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4134  * edit.
4135  */
4136 int
4137 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4138     rman_res_t start, rman_res_t count)
4139 {
4140 	struct resource_list *		rl = NULL;
4141 
4142 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4143 	if (!rl)
4144 		return (EINVAL);
4145 
4146 	resource_list_add(rl, type, rid, start, (start + count - 1), count);
4147 
4148 	return (0);
4149 }
4150 
4151 /**
4152  * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4153  *
4154  * This implementation of BUS_DELETE_RESOURCE() uses the
4155  * resource_list_delete() function to do most of the work. It calls
4156  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4157  * edit.
4158  */
4159 void
4160 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4161 {
4162 	struct resource_list *		rl = NULL;
4163 
4164 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4165 	if (!rl)
4166 		return;
4167 
4168 	resource_list_delete(rl, type, rid);
4169 
4170 	return;
4171 }
4172 
4173 /**
4174  * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4175  *
4176  * This implementation of BUS_RELEASE_RESOURCE() uses the
4177  * resource_list_release() function to do most of the work. It calls
4178  * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4179  */
4180 int
4181 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4182     int rid, struct resource *r)
4183 {
4184 	struct resource_list *		rl = NULL;
4185 
4186 	if (device_get_parent(child) != dev)
4187 		return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4188 		    type, rid, r));
4189 
4190 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4191 	if (!rl)
4192 		return (EINVAL);
4193 
4194 	return (resource_list_release(rl, dev, child, type, rid, r));
4195 }
4196 
4197 /**
4198  * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4199  *
4200  * This implementation of BUS_ALLOC_RESOURCE() uses the
4201  * resource_list_alloc() function to do most of the work. It calls
4202  * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4203  */
4204 struct resource *
4205 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4206     int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4207 {
4208 	struct resource_list *		rl = NULL;
4209 
4210 	if (device_get_parent(child) != dev)
4211 		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4212 		    type, rid, start, end, count, flags));
4213 
4214 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4215 	if (!rl)
4216 		return (NULL);
4217 
4218 	return (resource_list_alloc(rl, dev, child, type, rid,
4219 	    start, end, count, flags));
4220 }
4221 
4222 /**
4223  * @brief Helper function for implementing BUS_CHILD_PRESENT().
4224  *
4225  * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4226  * BUS_CHILD_PRESENT() method of the parent of @p dev.
4227  */
4228 int
4229 bus_generic_child_present(device_t dev, device_t child)
4230 {
4231 	return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4232 }
4233 
4234 int
4235 bus_generic_get_domain(device_t dev, device_t child, int *domain)
4236 {
4237 
4238 	if (dev->parent)
4239 		return (BUS_GET_DOMAIN(dev->parent, dev, domain));
4240 
4241 	return (ENOENT);
4242 }
4243 
4244 /*
4245  * Some convenience functions to make it easier for drivers to use the
4246  * resource-management functions.  All these really do is hide the
4247  * indirection through the parent's method table, making for slightly
4248  * less-wordy code.  In the future, it might make sense for this code
4249  * to maintain some sort of a list of resources allocated by each device.
4250  */
4251 
4252 int
4253 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4254     struct resource **res)
4255 {
4256 	int i;
4257 
4258 	for (i = 0; rs[i].type != -1; i++)
4259 		res[i] = NULL;
4260 	for (i = 0; rs[i].type != -1; i++) {
4261 		res[i] = bus_alloc_resource_any(dev,
4262 		    rs[i].type, &rs[i].rid, rs[i].flags);
4263 		if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4264 			bus_release_resources(dev, rs, res);
4265 			return (ENXIO);
4266 		}
4267 	}
4268 	return (0);
4269 }
4270 
4271 void
4272 bus_release_resources(device_t dev, const struct resource_spec *rs,
4273     struct resource **res)
4274 {
4275 	int i;
4276 
4277 	for (i = 0; rs[i].type != -1; i++)
4278 		if (res[i] != NULL) {
4279 			bus_release_resource(
4280 			    dev, rs[i].type, rs[i].rid, res[i]);
4281 			res[i] = NULL;
4282 		}
4283 }
4284 
4285 /**
4286  * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4287  *
4288  * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4289  * parent of @p dev.
4290  */
4291 struct resource *
4292 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start, rman_res_t end,
4293     rman_res_t count, u_int flags)
4294 {
4295 	if (dev->parent == NULL)
4296 		return (NULL);
4297 	return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4298 	    count, flags));
4299 }
4300 
4301 /**
4302  * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4303  *
4304  * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4305  * parent of @p dev.
4306  */
4307 int
4308 bus_adjust_resource(device_t dev, int type, struct resource *r, rman_res_t start,
4309     rman_res_t end)
4310 {
4311 	if (dev->parent == NULL)
4312 		return (EINVAL);
4313 	return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4314 }
4315 
4316 /**
4317  * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4318  *
4319  * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4320  * parent of @p dev.
4321  */
4322 int
4323 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4324 {
4325 	if (dev->parent == NULL)
4326 		return (EINVAL);
4327 	return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4328 }
4329 
4330 /**
4331  * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4332  *
4333  * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4334  * parent of @p dev.
4335  */
4336 int
4337 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4338 {
4339 	if (dev->parent == NULL)
4340 		return (EINVAL);
4341 	return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4342 }
4343 
4344 /**
4345  * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4346  *
4347  * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4348  * parent of @p dev.
4349  */
4350 int
4351 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4352 {
4353 	if (dev->parent == NULL)
4354 		return (EINVAL);
4355 	return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
4356 }
4357 
4358 /**
4359  * @brief Wrapper function for BUS_SETUP_INTR().
4360  *
4361  * This function simply calls the BUS_SETUP_INTR() method of the
4362  * parent of @p dev.
4363  */
4364 int
4365 bus_setup_intr(device_t dev, struct resource *r, int flags,
4366     driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4367 {
4368 	int error;
4369 
4370 	if (dev->parent == NULL)
4371 		return (EINVAL);
4372 	error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4373 	    arg, cookiep);
4374 	if (error != 0)
4375 		return (error);
4376 	if (handler != NULL && !(flags & INTR_MPSAFE))
4377 		device_printf(dev, "[GIANT-LOCKED]\n");
4378 	return (0);
4379 }
4380 
4381 /**
4382  * @brief Wrapper function for BUS_TEARDOWN_INTR().
4383  *
4384  * This function simply calls the BUS_TEARDOWN_INTR() method of the
4385  * parent of @p dev.
4386  */
4387 int
4388 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4389 {
4390 	if (dev->parent == NULL)
4391 		return (EINVAL);
4392 	return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4393 }
4394 
4395 /**
4396  * @brief Wrapper function for BUS_BIND_INTR().
4397  *
4398  * This function simply calls the BUS_BIND_INTR() method of the
4399  * parent of @p dev.
4400  */
4401 int
4402 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4403 {
4404 	if (dev->parent == NULL)
4405 		return (EINVAL);
4406 	return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4407 }
4408 
4409 /**
4410  * @brief Wrapper function for BUS_DESCRIBE_INTR().
4411  *
4412  * This function first formats the requested description into a
4413  * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4414  * the parent of @p dev.
4415  */
4416 int
4417 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4418     const char *fmt, ...)
4419 {
4420 	va_list ap;
4421 	char descr[MAXCOMLEN + 1];
4422 
4423 	if (dev->parent == NULL)
4424 		return (EINVAL);
4425 	va_start(ap, fmt);
4426 	vsnprintf(descr, sizeof(descr), fmt, ap);
4427 	va_end(ap);
4428 	return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4429 }
4430 
4431 /**
4432  * @brief Wrapper function for BUS_SET_RESOURCE().
4433  *
4434  * This function simply calls the BUS_SET_RESOURCE() method of the
4435  * parent of @p dev.
4436  */
4437 int
4438 bus_set_resource(device_t dev, int type, int rid,
4439     rman_res_t start, rman_res_t count)
4440 {
4441 	return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4442 	    start, count));
4443 }
4444 
4445 /**
4446  * @brief Wrapper function for BUS_GET_RESOURCE().
4447  *
4448  * This function simply calls the BUS_GET_RESOURCE() method of the
4449  * parent of @p dev.
4450  */
4451 int
4452 bus_get_resource(device_t dev, int type, int rid,
4453     rman_res_t *startp, rman_res_t *countp)
4454 {
4455 	return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4456 	    startp, countp));
4457 }
4458 
4459 /**
4460  * @brief Wrapper function for BUS_GET_RESOURCE().
4461  *
4462  * This function simply calls the BUS_GET_RESOURCE() method of the
4463  * parent of @p dev and returns the start value.
4464  */
4465 rman_res_t
4466 bus_get_resource_start(device_t dev, int type, int rid)
4467 {
4468 	rman_res_t start;
4469 	rman_res_t count;
4470 	int error;
4471 
4472 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4473 	    &start, &count);
4474 	if (error)
4475 		return (0);
4476 	return (start);
4477 }
4478 
4479 /**
4480  * @brief Wrapper function for BUS_GET_RESOURCE().
4481  *
4482  * This function simply calls the BUS_GET_RESOURCE() method of the
4483  * parent of @p dev and returns the count value.
4484  */
4485 rman_res_t
4486 bus_get_resource_count(device_t dev, int type, int rid)
4487 {
4488 	rman_res_t start;
4489 	rman_res_t count;
4490 	int error;
4491 
4492 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4493 	    &start, &count);
4494 	if (error)
4495 		return (0);
4496 	return (count);
4497 }
4498 
4499 /**
4500  * @brief Wrapper function for BUS_DELETE_RESOURCE().
4501  *
4502  * This function simply calls the BUS_DELETE_RESOURCE() method of the
4503  * parent of @p dev.
4504  */
4505 void
4506 bus_delete_resource(device_t dev, int type, int rid)
4507 {
4508 	BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4509 }
4510 
4511 /**
4512  * @brief Wrapper function for BUS_CHILD_PRESENT().
4513  *
4514  * This function simply calls the BUS_CHILD_PRESENT() method of the
4515  * parent of @p dev.
4516  */
4517 int
4518 bus_child_present(device_t child)
4519 {
4520 	return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4521 }
4522 
4523 /**
4524  * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4525  *
4526  * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4527  * parent of @p dev.
4528  */
4529 int
4530 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4531 {
4532 	device_t parent;
4533 
4534 	parent = device_get_parent(child);
4535 	if (parent == NULL) {
4536 		*buf = '\0';
4537 		return (0);
4538 	}
4539 	return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4540 }
4541 
4542 /**
4543  * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4544  *
4545  * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4546  * parent of @p dev.
4547  */
4548 int
4549 bus_child_location_str(device_t child, char *buf, size_t buflen)
4550 {
4551 	device_t parent;
4552 
4553 	parent = device_get_parent(child);
4554 	if (parent == NULL) {
4555 		*buf = '\0';
4556 		return (0);
4557 	}
4558 	return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4559 }
4560 
4561 /**
4562  * @brief Wrapper function for BUS_GET_DMA_TAG().
4563  *
4564  * This function simply calls the BUS_GET_DMA_TAG() method of the
4565  * parent of @p dev.
4566  */
4567 bus_dma_tag_t
4568 bus_get_dma_tag(device_t dev)
4569 {
4570 	device_t parent;
4571 
4572 	parent = device_get_parent(dev);
4573 	if (parent == NULL)
4574 		return (NULL);
4575 	return (BUS_GET_DMA_TAG(parent, dev));
4576 }
4577 
4578 /**
4579  * @brief Wrapper function for BUS_GET_DOMAIN().
4580  *
4581  * This function simply calls the BUS_GET_DOMAIN() method of the
4582  * parent of @p dev.
4583  */
4584 int
4585 bus_get_domain(device_t dev, int *domain)
4586 {
4587 	return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain));
4588 }
4589 
4590 /* Resume all devices and then notify userland that we're up again. */
4591 static int
4592 root_resume(device_t dev)
4593 {
4594 	int error;
4595 
4596 	error = bus_generic_resume(dev);
4597 	if (error == 0)
4598 		devctl_notify("kern", "power", "resume", NULL);
4599 	return (error);
4600 }
4601 
4602 static int
4603 root_print_child(device_t dev, device_t child)
4604 {
4605 	int	retval = 0;
4606 
4607 	retval += bus_print_child_header(dev, child);
4608 	retval += printf("\n");
4609 
4610 	return (retval);
4611 }
4612 
4613 static int
4614 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4615     driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4616 {
4617 	/*
4618 	 * If an interrupt mapping gets to here something bad has happened.
4619 	 */
4620 	panic("root_setup_intr");
4621 }
4622 
4623 /*
4624  * If we get here, assume that the device is permanant and really is
4625  * present in the system.  Removable bus drivers are expected to intercept
4626  * this call long before it gets here.  We return -1 so that drivers that
4627  * really care can check vs -1 or some ERRNO returned higher in the food
4628  * chain.
4629  */
4630 static int
4631 root_child_present(device_t dev, device_t child)
4632 {
4633 	return (-1);
4634 }
4635 
4636 static kobj_method_t root_methods[] = {
4637 	/* Device interface */
4638 	KOBJMETHOD(device_shutdown,	bus_generic_shutdown),
4639 	KOBJMETHOD(device_suspend,	bus_generic_suspend),
4640 	KOBJMETHOD(device_resume,	root_resume),
4641 
4642 	/* Bus interface */
4643 	KOBJMETHOD(bus_print_child,	root_print_child),
4644 	KOBJMETHOD(bus_read_ivar,	bus_generic_read_ivar),
4645 	KOBJMETHOD(bus_write_ivar,	bus_generic_write_ivar),
4646 	KOBJMETHOD(bus_setup_intr,	root_setup_intr),
4647 	KOBJMETHOD(bus_child_present,	root_child_present),
4648 
4649 	KOBJMETHOD_END
4650 };
4651 
4652 static driver_t root_driver = {
4653 	"root",
4654 	root_methods,
4655 	1,			/* no softc */
4656 };
4657 
4658 device_t	root_bus;
4659 devclass_t	root_devclass;
4660 
4661 static int
4662 root_bus_module_handler(module_t mod, int what, void* arg)
4663 {
4664 	switch (what) {
4665 	case MOD_LOAD:
4666 		TAILQ_INIT(&bus_data_devices);
4667 		kobj_class_compile((kobj_class_t) &root_driver);
4668 		root_bus = make_device(NULL, "root", 0);
4669 		root_bus->desc = "System root bus";
4670 		kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4671 		root_bus->driver = &root_driver;
4672 		root_bus->state = DS_ATTACHED;
4673 		root_devclass = devclass_find_internal("root", NULL, FALSE);
4674 		devinit();
4675 		return (0);
4676 
4677 	case MOD_SHUTDOWN:
4678 		device_shutdown(root_bus);
4679 		return (0);
4680 	default:
4681 		return (EOPNOTSUPP);
4682 	}
4683 
4684 	return (0);
4685 }
4686 
4687 static moduledata_t root_bus_mod = {
4688 	"rootbus",
4689 	root_bus_module_handler,
4690 	NULL
4691 };
4692 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4693 
4694 /**
4695  * @brief Automatically configure devices
4696  *
4697  * This function begins the autoconfiguration process by calling
4698  * device_probe_and_attach() for each child of the @c root0 device.
4699  */
4700 void
4701 root_bus_configure(void)
4702 {
4703 
4704 	PDEBUG(("."));
4705 
4706 	/* Eventually this will be split up, but this is sufficient for now. */
4707 	bus_set_pass(BUS_PASS_DEFAULT);
4708 }
4709 
4710 /**
4711  * @brief Module handler for registering device drivers
4712  *
4713  * This module handler is used to automatically register device
4714  * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4715  * devclass_add_driver() for the driver described by the
4716  * driver_module_data structure pointed to by @p arg
4717  */
4718 int
4719 driver_module_handler(module_t mod, int what, void *arg)
4720 {
4721 	struct driver_module_data *dmd;
4722 	devclass_t bus_devclass;
4723 	kobj_class_t driver;
4724 	int error, pass;
4725 
4726 	dmd = (struct driver_module_data *)arg;
4727 	bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4728 	error = 0;
4729 
4730 	switch (what) {
4731 	case MOD_LOAD:
4732 		if (dmd->dmd_chainevh)
4733 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4734 
4735 		pass = dmd->dmd_pass;
4736 		driver = dmd->dmd_driver;
4737 		PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4738 		    DRIVERNAME(driver), dmd->dmd_busname, pass));
4739 		error = devclass_add_driver(bus_devclass, driver, pass,
4740 		    dmd->dmd_devclass);
4741 		break;
4742 
4743 	case MOD_UNLOAD:
4744 		PDEBUG(("Unloading module: driver %s from bus %s",
4745 		    DRIVERNAME(dmd->dmd_driver),
4746 		    dmd->dmd_busname));
4747 		error = devclass_delete_driver(bus_devclass,
4748 		    dmd->dmd_driver);
4749 
4750 		if (!error && dmd->dmd_chainevh)
4751 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4752 		break;
4753 	case MOD_QUIESCE:
4754 		PDEBUG(("Quiesce module: driver %s from bus %s",
4755 		    DRIVERNAME(dmd->dmd_driver),
4756 		    dmd->dmd_busname));
4757 		error = devclass_quiesce_driver(bus_devclass,
4758 		    dmd->dmd_driver);
4759 
4760 		if (!error && dmd->dmd_chainevh)
4761 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4762 		break;
4763 	default:
4764 		error = EOPNOTSUPP;
4765 		break;
4766 	}
4767 
4768 	return (error);
4769 }
4770 
4771 /**
4772  * @brief Enumerate all hinted devices for this bus.
4773  *
4774  * Walks through the hints for this bus and calls the bus_hinted_child
4775  * routine for each one it fines.  It searches first for the specific
4776  * bus that's being probed for hinted children (eg isa0), and then for
4777  * generic children (eg isa).
4778  *
4779  * @param	dev	bus device to enumerate
4780  */
4781 void
4782 bus_enumerate_hinted_children(device_t bus)
4783 {
4784 	int i;
4785 	const char *dname, *busname;
4786 	int dunit;
4787 
4788 	/*
4789 	 * enumerate all devices on the specific bus
4790 	 */
4791 	busname = device_get_nameunit(bus);
4792 	i = 0;
4793 	while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4794 		BUS_HINTED_CHILD(bus, dname, dunit);
4795 
4796 	/*
4797 	 * and all the generic ones.
4798 	 */
4799 	busname = device_get_name(bus);
4800 	i = 0;
4801 	while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4802 		BUS_HINTED_CHILD(bus, dname, dunit);
4803 }
4804 
4805 #ifdef BUS_DEBUG
4806 
4807 /* the _short versions avoid iteration by not calling anything that prints
4808  * more than oneliners. I love oneliners.
4809  */
4810 
4811 static void
4812 print_device_short(device_t dev, int indent)
4813 {
4814 	if (!dev)
4815 		return;
4816 
4817 	indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4818 	    dev->unit, dev->desc,
4819 	    (dev->parent? "":"no "),
4820 	    (TAILQ_EMPTY(&dev->children)? "no ":""),
4821 	    (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4822 	    (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4823 	    (dev->flags&DF_WILDCARD? "wildcard,":""),
4824 	    (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4825 	    (dev->flags&DF_REBID? "rebiddable,":""),
4826 	    (dev->ivars? "":"no "),
4827 	    (dev->softc? "":"no "),
4828 	    dev->busy));
4829 }
4830 
4831 static void
4832 print_device(device_t dev, int indent)
4833 {
4834 	if (!dev)
4835 		return;
4836 
4837 	print_device_short(dev, indent);
4838 
4839 	indentprintf(("Parent:\n"));
4840 	print_device_short(dev->parent, indent+1);
4841 	indentprintf(("Driver:\n"));
4842 	print_driver_short(dev->driver, indent+1);
4843 	indentprintf(("Devclass:\n"));
4844 	print_devclass_short(dev->devclass, indent+1);
4845 }
4846 
4847 void
4848 print_device_tree_short(device_t dev, int indent)
4849 /* print the device and all its children (indented) */
4850 {
4851 	device_t child;
4852 
4853 	if (!dev)
4854 		return;
4855 
4856 	print_device_short(dev, indent);
4857 
4858 	TAILQ_FOREACH(child, &dev->children, link) {
4859 		print_device_tree_short(child, indent+1);
4860 	}
4861 }
4862 
4863 void
4864 print_device_tree(device_t dev, int indent)
4865 /* print the device and all its children (indented) */
4866 {
4867 	device_t child;
4868 
4869 	if (!dev)
4870 		return;
4871 
4872 	print_device(dev, indent);
4873 
4874 	TAILQ_FOREACH(child, &dev->children, link) {
4875 		print_device_tree(child, indent+1);
4876 	}
4877 }
4878 
4879 static void
4880 print_driver_short(driver_t *driver, int indent)
4881 {
4882 	if (!driver)
4883 		return;
4884 
4885 	indentprintf(("driver %s: softc size = %zd\n",
4886 	    driver->name, driver->size));
4887 }
4888 
4889 static void
4890 print_driver(driver_t *driver, int indent)
4891 {
4892 	if (!driver)
4893 		return;
4894 
4895 	print_driver_short(driver, indent);
4896 }
4897 
4898 static void
4899 print_driver_list(driver_list_t drivers, int indent)
4900 {
4901 	driverlink_t driver;
4902 
4903 	TAILQ_FOREACH(driver, &drivers, link) {
4904 		print_driver(driver->driver, indent);
4905 	}
4906 }
4907 
4908 static void
4909 print_devclass_short(devclass_t dc, int indent)
4910 {
4911 	if ( !dc )
4912 		return;
4913 
4914 	indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4915 }
4916 
4917 static void
4918 print_devclass(devclass_t dc, int indent)
4919 {
4920 	int i;
4921 
4922 	if ( !dc )
4923 		return;
4924 
4925 	print_devclass_short(dc, indent);
4926 	indentprintf(("Drivers:\n"));
4927 	print_driver_list(dc->drivers, indent+1);
4928 
4929 	indentprintf(("Devices:\n"));
4930 	for (i = 0; i < dc->maxunit; i++)
4931 		if (dc->devices[i])
4932 			print_device(dc->devices[i], indent+1);
4933 }
4934 
4935 void
4936 print_devclass_list_short(void)
4937 {
4938 	devclass_t dc;
4939 
4940 	printf("Short listing of devclasses, drivers & devices:\n");
4941 	TAILQ_FOREACH(dc, &devclasses, link) {
4942 		print_devclass_short(dc, 0);
4943 	}
4944 }
4945 
4946 void
4947 print_devclass_list(void)
4948 {
4949 	devclass_t dc;
4950 
4951 	printf("Full listing of devclasses, drivers & devices:\n");
4952 	TAILQ_FOREACH(dc, &devclasses, link) {
4953 		print_devclass(dc, 0);
4954 	}
4955 }
4956 
4957 #endif
4958 
4959 /*
4960  * User-space access to the device tree.
4961  *
4962  * We implement a small set of nodes:
4963  *
4964  * hw.bus			Single integer read method to obtain the
4965  *				current generation count.
4966  * hw.bus.devices		Reads the entire device tree in flat space.
4967  * hw.bus.rman			Resource manager interface
4968  *
4969  * We might like to add the ability to scan devclasses and/or drivers to
4970  * determine what else is currently loaded/available.
4971  */
4972 
4973 static int
4974 sysctl_bus(SYSCTL_HANDLER_ARGS)
4975 {
4976 	struct u_businfo	ubus;
4977 
4978 	ubus.ub_version = BUS_USER_VERSION;
4979 	ubus.ub_generation = bus_data_generation;
4980 
4981 	return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4982 }
4983 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4984     "bus-related data");
4985 
4986 static int
4987 sysctl_devices(SYSCTL_HANDLER_ARGS)
4988 {
4989 	int			*name = (int *)arg1;
4990 	u_int			namelen = arg2;
4991 	int			index;
4992 	struct device		*dev;
4993 	struct u_device		udev;	/* XXX this is a bit big */
4994 	int			error;
4995 
4996 	if (namelen != 2)
4997 		return (EINVAL);
4998 
4999 	if (bus_data_generation_check(name[0]))
5000 		return (EINVAL);
5001 
5002 	index = name[1];
5003 
5004 	/*
5005 	 * Scan the list of devices, looking for the requested index.
5006 	 */
5007 	TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5008 		if (index-- == 0)
5009 			break;
5010 	}
5011 	if (dev == NULL)
5012 		return (ENOENT);
5013 
5014 	/*
5015 	 * Populate the return array.
5016 	 */
5017 	bzero(&udev, sizeof(udev));
5018 	udev.dv_handle = (uintptr_t)dev;
5019 	udev.dv_parent = (uintptr_t)dev->parent;
5020 	if (dev->nameunit != NULL)
5021 		strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
5022 	if (dev->desc != NULL)
5023 		strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
5024 	if (dev->driver != NULL && dev->driver->name != NULL)
5025 		strlcpy(udev.dv_drivername, dev->driver->name,
5026 		    sizeof(udev.dv_drivername));
5027 	bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
5028 	bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
5029 	udev.dv_devflags = dev->devflags;
5030 	udev.dv_flags = dev->flags;
5031 	udev.dv_state = dev->state;
5032 	error = SYSCTL_OUT(req, &udev, sizeof(udev));
5033 	return (error);
5034 }
5035 
5036 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
5037     "system device tree");
5038 
5039 int
5040 bus_data_generation_check(int generation)
5041 {
5042 	if (generation != bus_data_generation)
5043 		return (1);
5044 
5045 	/* XXX generate optimised lists here? */
5046 	return (0);
5047 }
5048 
5049 void
5050 bus_data_generation_update(void)
5051 {
5052 	bus_data_generation++;
5053 }
5054 
5055 int
5056 bus_free_resource(device_t dev, int type, struct resource *r)
5057 {
5058 	if (r == NULL)
5059 		return (0);
5060 	return (bus_release_resource(dev, type, rman_get_rid(r), r));
5061 }
5062 
5063 /*
5064  * /dev/devctl2 implementation.  The existing /dev/devctl device has
5065  * implicit semantics on open, so it could not be reused for this.
5066  * Another option would be to call this /dev/bus?
5067  */
5068 static int
5069 find_device(struct devreq *req, device_t *devp)
5070 {
5071 	device_t dev;
5072 
5073 	/*
5074 	 * First, ensure that the name is nul terminated.
5075 	 */
5076 	if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL)
5077 		return (EINVAL);
5078 
5079 	/*
5080 	 * Second, try to find an attached device whose name matches
5081 	 * 'name'.
5082 	 */
5083 	TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5084 		if (dev->nameunit != NULL &&
5085 		    strcmp(dev->nameunit, req->dr_name) == 0) {
5086 			*devp = dev;
5087 			return (0);
5088 		}
5089 	}
5090 
5091 	/* Finally, give device enumerators a chance. */
5092 	dev = NULL;
5093 	EVENTHANDLER_INVOKE(dev_lookup, req->dr_name, &dev);
5094 	if (dev == NULL)
5095 		return (ENOENT);
5096 	*devp = dev;
5097 	return (0);
5098 }
5099 
5100 static bool
5101 driver_exists(struct device *bus, const char *driver)
5102 {
5103 	devclass_t dc;
5104 
5105 	for (dc = bus->devclass; dc != NULL; dc = dc->parent) {
5106 		if (devclass_find_driver_internal(dc, driver) != NULL)
5107 			return (true);
5108 	}
5109 	return (false);
5110 }
5111 
5112 static int
5113 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
5114     struct thread *td)
5115 {
5116 	struct devreq *req;
5117 	device_t dev;
5118 	int error, old;
5119 
5120 	/* Locate the device to control. */
5121 	mtx_lock(&Giant);
5122 	req = (struct devreq *)data;
5123 	switch (cmd) {
5124 	case DEV_ATTACH:
5125 	case DEV_DETACH:
5126 	case DEV_ENABLE:
5127 	case DEV_DISABLE:
5128 	case DEV_SUSPEND:
5129 	case DEV_RESUME:
5130 	case DEV_SET_DRIVER:
5131 		error = priv_check(td, PRIV_DRIVER);
5132 		if (error == 0)
5133 			error = find_device(req, &dev);
5134 		break;
5135 	default:
5136 		error = ENOTTY;
5137 		break;
5138 	}
5139 	if (error) {
5140 		mtx_unlock(&Giant);
5141 		return (error);
5142 	}
5143 
5144 	/* Perform the requested operation. */
5145 	switch (cmd) {
5146 	case DEV_ATTACH:
5147 		if (device_is_attached(dev) && (dev->flags & DF_REBID) == 0)
5148 			error = EBUSY;
5149 		else if (!device_is_enabled(dev))
5150 			error = ENXIO;
5151 		else
5152 			error = device_probe_and_attach(dev);
5153 		break;
5154 	case DEV_DETACH:
5155 		if (!device_is_attached(dev)) {
5156 			error = ENXIO;
5157 			break;
5158 		}
5159 		if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5160 			error = device_quiesce(dev);
5161 			if (error)
5162 				break;
5163 		}
5164 		error = device_detach(dev);
5165 		break;
5166 	case DEV_ENABLE:
5167 		if (device_is_enabled(dev)) {
5168 			error = EBUSY;
5169 			break;
5170 		}
5171 
5172 		/*
5173 		 * If the device has been probed but not attached (e.g.
5174 		 * when it has been disabled by a loader hint), just
5175 		 * attach the device rather than doing a full probe.
5176 		 */
5177 		device_enable(dev);
5178 		if (device_is_alive(dev)) {
5179 			/*
5180 			 * If the device was disabled via a hint, clear
5181 			 * the hint.
5182 			 */
5183 			if (resource_disabled(dev->driver->name, dev->unit))
5184 				resource_unset_value(dev->driver->name,
5185 				    dev->unit, "disabled");
5186 			error = device_attach(dev);
5187 		} else
5188 			error = device_probe_and_attach(dev);
5189 		break;
5190 	case DEV_DISABLE:
5191 		if (!device_is_enabled(dev)) {
5192 			error = ENXIO;
5193 			break;
5194 		}
5195 
5196 		if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5197 			error = device_quiesce(dev);
5198 			if (error)
5199 				break;
5200 		}
5201 
5202 		/*
5203 		 * Force DF_FIXEDCLASS on around detach to preserve
5204 		 * the existing name.
5205 		 */
5206 		old = dev->flags;
5207 		dev->flags |= DF_FIXEDCLASS;
5208 		error = device_detach(dev);
5209 		if (!(old & DF_FIXEDCLASS))
5210 			dev->flags &= ~DF_FIXEDCLASS;
5211 		if (error == 0)
5212 			device_disable(dev);
5213 		break;
5214 	case DEV_SUSPEND:
5215 		if (device_is_suspended(dev)) {
5216 			error = EBUSY;
5217 			break;
5218 		}
5219 		if (device_get_parent(dev) == NULL) {
5220 			error = EINVAL;
5221 			break;
5222 		}
5223 		error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev);
5224 		break;
5225 	case DEV_RESUME:
5226 		if (!device_is_suspended(dev)) {
5227 			error = EINVAL;
5228 			break;
5229 		}
5230 		if (device_get_parent(dev) == NULL) {
5231 			error = EINVAL;
5232 			break;
5233 		}
5234 		error = BUS_RESUME_CHILD(device_get_parent(dev), dev);
5235 		break;
5236 	case DEV_SET_DRIVER: {
5237 		devclass_t dc;
5238 		char driver[128];
5239 
5240 		error = copyinstr(req->dr_data, driver, sizeof(driver), NULL);
5241 		if (error)
5242 			break;
5243 		if (driver[0] == '\0') {
5244 			error = EINVAL;
5245 			break;
5246 		}
5247 		if (dev->devclass != NULL &&
5248 		    strcmp(driver, dev->devclass->name) == 0)
5249 			/* XXX: Could possibly force DF_FIXEDCLASS on? */
5250 			break;
5251 
5252 		/*
5253 		 * Scan drivers for this device's bus looking for at
5254 		 * least one matching driver.
5255 		 */
5256 		if (dev->parent == NULL) {
5257 			error = EINVAL;
5258 			break;
5259 		}
5260 		if (!driver_exists(dev->parent, driver)) {
5261 			error = ENOENT;
5262 			break;
5263 		}
5264 		dc = devclass_create(driver);
5265 		if (dc == NULL) {
5266 			error = ENOMEM;
5267 			break;
5268 		}
5269 
5270 		/* Detach device if necessary. */
5271 		if (device_is_attached(dev)) {
5272 			if (req->dr_flags & DEVF_SET_DRIVER_DETACH)
5273 				error = device_detach(dev);
5274 			else
5275 				error = EBUSY;
5276 			if (error)
5277 				break;
5278 		}
5279 
5280 		/* Clear any previously-fixed device class and unit. */
5281 		if (dev->flags & DF_FIXEDCLASS)
5282 			devclass_delete_device(dev->devclass, dev);
5283 		dev->flags |= DF_WILDCARD;
5284 		dev->unit = -1;
5285 
5286 		/* Force the new device class. */
5287 		error = devclass_add_device(dc, dev);
5288 		if (error)
5289 			break;
5290 		dev->flags |= DF_FIXEDCLASS;
5291 		error = device_probe_and_attach(dev);
5292 		break;
5293 	}
5294 	}
5295 	mtx_unlock(&Giant);
5296 	return (error);
5297 }
5298 
5299 static struct cdevsw devctl2_cdevsw = {
5300 	.d_version =	D_VERSION,
5301 	.d_ioctl =	devctl2_ioctl,
5302 	.d_name =	"devctl2",
5303 };
5304 
5305 static void
5306 devctl2_init(void)
5307 {
5308 
5309 	make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL,
5310 	    UID_ROOT, GID_WHEEL, 0600, "devctl2");
5311 }
5312