xref: /freebsd/sys/kern/subr_bus.c (revision 6186fd1857626de0f7cb1a9e4dff19082f9ebb11)
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 #include "opt_random.h"
32 
33 #include <sys/param.h>
34 #include <sys/conf.h>
35 #include <sys/filio.h>
36 #include <sys/lock.h>
37 #include <sys/kernel.h>
38 #include <sys/kobj.h>
39 #include <sys/limits.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/poll.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 
58 #include <net/vnet.h>
59 
60 #include <machine/cpu.h>
61 #include <machine/stdarg.h>
62 
63 #include <vm/uma.h>
64 
65 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
66 SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
67 
68 /*
69  * Used to attach drivers to devclasses.
70  */
71 typedef struct driverlink *driverlink_t;
72 struct driverlink {
73 	kobj_class_t	driver;
74 	TAILQ_ENTRY(driverlink) link;	/* list of drivers in devclass */
75 	int		pass;
76 	TAILQ_ENTRY(driverlink) passlink;
77 };
78 
79 /*
80  * Forward declarations
81  */
82 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
83 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
84 typedef TAILQ_HEAD(device_list, device) device_list_t;
85 
86 struct devclass {
87 	TAILQ_ENTRY(devclass) link;
88 	devclass_t	parent;		/* parent in devclass hierarchy */
89 	driver_list_t	drivers;     /* bus devclasses store drivers for bus */
90 	char		*name;
91 	device_t	*devices;	/* array of devices indexed by unit */
92 	int		maxunit;	/* size of devices array */
93 	int		flags;
94 #define DC_HAS_CHILDREN		1
95 
96 	struct sysctl_ctx_list sysctl_ctx;
97 	struct sysctl_oid *sysctl_tree;
98 };
99 
100 /**
101  * @brief Implementation of device.
102  */
103 struct device {
104 	/*
105 	 * A device is a kernel object. The first field must be the
106 	 * current ops table for the object.
107 	 */
108 	KOBJ_FIELDS;
109 
110 	/*
111 	 * Device hierarchy.
112 	 */
113 	TAILQ_ENTRY(device)	link;	/**< list of devices in parent */
114 	TAILQ_ENTRY(device)	devlink; /**< global device list membership */
115 	device_t	parent;		/**< parent of this device  */
116 	device_list_t	children;	/**< list of child devices */
117 
118 	/*
119 	 * Details of this device.
120 	 */
121 	driver_t	*driver;	/**< current driver */
122 	devclass_t	devclass;	/**< current device class */
123 	int		unit;		/**< current unit number */
124 	char*		nameunit;	/**< name+unit e.g. foodev0 */
125 	char*		desc;		/**< driver specific description */
126 	int		busy;		/**< count of calls to device_busy() */
127 	device_state_t	state;		/**< current device state  */
128 	uint32_t	devflags;	/**< api level flags for device_get_flags() */
129 	u_int		flags;		/**< internal device flags  */
130 #define	DF_ENABLED	0x01		/* device should be probed/attached */
131 #define	DF_FIXEDCLASS	0x02		/* devclass specified at create time */
132 #define	DF_WILDCARD	0x04		/* unit was originally wildcard */
133 #define	DF_DESCMALLOCED	0x08		/* description was malloced */
134 #define	DF_QUIET	0x10		/* don't print verbose attach message */
135 #define	DF_DONENOMATCH	0x20		/* don't execute DEVICE_NOMATCH again */
136 #define	DF_EXTERNALSOFTC 0x40		/* softc not allocated by us */
137 #define	DF_REBID	0x80		/* Can rebid after attach */
138 #define	DF_SUSPENDED	0x100		/* Device is suspended. */
139 	u_int	order;			/**< order from device_add_child_ordered() */
140 	void	*ivars;			/**< instance variables  */
141 	void	*softc;			/**< current driver's variables  */
142 
143 	struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables  */
144 	struct sysctl_oid *sysctl_tree;	/**< state for sysctl variables */
145 };
146 
147 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
148 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
149 
150 #ifdef BUS_DEBUG
151 
152 static int bus_debug = 1;
153 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
154     "Bus debug level");
155 
156 #define PDEBUG(a)	if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
157 #define DEVICENAME(d)	((d)? device_get_name(d): "no device")
158 #define DRIVERNAME(d)	((d)? d->name : "no driver")
159 #define DEVCLANAME(d)	((d)? d->name : "no devclass")
160 
161 /**
162  * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
163  * prevent syslog from deleting initial spaces
164  */
165 #define indentprintf(p)	do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf("  "); printf p ; } while (0)
166 
167 static void print_device_short(device_t dev, int indent);
168 static void print_device(device_t dev, int indent);
169 void print_device_tree_short(device_t dev, int indent);
170 void print_device_tree(device_t dev, int indent);
171 static void print_driver_short(driver_t *driver, int indent);
172 static void print_driver(driver_t *driver, int indent);
173 static void print_driver_list(driver_list_t drivers, int indent);
174 static void print_devclass_short(devclass_t dc, int indent);
175 static void print_devclass(devclass_t dc, int indent);
176 void print_devclass_list_short(void);
177 void print_devclass_list(void);
178 
179 #else
180 /* Make the compiler ignore the function calls */
181 #define PDEBUG(a)			/* nop */
182 #define DEVICENAME(d)			/* nop */
183 #define DRIVERNAME(d)			/* nop */
184 #define DEVCLANAME(d)			/* nop */
185 
186 #define print_device_short(d,i)		/* nop */
187 #define print_device(d,i)		/* nop */
188 #define print_device_tree_short(d,i)	/* nop */
189 #define print_device_tree(d,i)		/* nop */
190 #define print_driver_short(d,i)		/* nop */
191 #define print_driver(d,i)		/* nop */
192 #define print_driver_list(d,i)		/* nop */
193 #define print_devclass_short(d,i)	/* nop */
194 #define print_devclass(d,i)		/* nop */
195 #define print_devclass_list_short()	/* nop */
196 #define print_devclass_list()		/* nop */
197 #endif
198 
199 /*
200  * dev sysctl tree
201  */
202 
203 enum {
204 	DEVCLASS_SYSCTL_PARENT,
205 };
206 
207 static int
208 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
209 {
210 	devclass_t dc = (devclass_t)arg1;
211 	const char *value;
212 
213 	switch (arg2) {
214 	case DEVCLASS_SYSCTL_PARENT:
215 		value = dc->parent ? dc->parent->name : "";
216 		break;
217 	default:
218 		return (EINVAL);
219 	}
220 	return (SYSCTL_OUT(req, value, strlen(value)));
221 }
222 
223 static void
224 devclass_sysctl_init(devclass_t dc)
225 {
226 
227 	if (dc->sysctl_tree != NULL)
228 		return;
229 	sysctl_ctx_init(&dc->sysctl_ctx);
230 	dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
231 	    SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
232 	    CTLFLAG_RD, NULL, "");
233 	SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
234 	    OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
235 	    dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
236 	    "parent class");
237 }
238 
239 enum {
240 	DEVICE_SYSCTL_DESC,
241 	DEVICE_SYSCTL_DRIVER,
242 	DEVICE_SYSCTL_LOCATION,
243 	DEVICE_SYSCTL_PNPINFO,
244 	DEVICE_SYSCTL_PARENT,
245 };
246 
247 static int
248 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
249 {
250 	device_t dev = (device_t)arg1;
251 	const char *value;
252 	char *buf;
253 	int error;
254 
255 	buf = NULL;
256 	switch (arg2) {
257 	case DEVICE_SYSCTL_DESC:
258 		value = dev->desc ? dev->desc : "";
259 		break;
260 	case DEVICE_SYSCTL_DRIVER:
261 		value = dev->driver ? dev->driver->name : "";
262 		break;
263 	case DEVICE_SYSCTL_LOCATION:
264 		value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
265 		bus_child_location_str(dev, buf, 1024);
266 		break;
267 	case DEVICE_SYSCTL_PNPINFO:
268 		value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
269 		bus_child_pnpinfo_str(dev, buf, 1024);
270 		break;
271 	case DEVICE_SYSCTL_PARENT:
272 		value = dev->parent ? dev->parent->nameunit : "";
273 		break;
274 	default:
275 		return (EINVAL);
276 	}
277 	error = SYSCTL_OUT(req, value, strlen(value));
278 	if (buf != NULL)
279 		free(buf, M_BUS);
280 	return (error);
281 }
282 
283 static void
284 device_sysctl_init(device_t dev)
285 {
286 	devclass_t dc = dev->devclass;
287 
288 	if (dev->sysctl_tree != NULL)
289 		return;
290 	devclass_sysctl_init(dc);
291 	sysctl_ctx_init(&dev->sysctl_ctx);
292 	dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
293 	    SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
294 	    dev->nameunit + strlen(dc->name),
295 	    CTLFLAG_RD, NULL, "");
296 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
297 	    OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
298 	    dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
299 	    "device description");
300 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
301 	    OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
302 	    dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
303 	    "device driver name");
304 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
305 	    OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
306 	    dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
307 	    "device location relative to parent");
308 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
309 	    OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
310 	    dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
311 	    "device identification");
312 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
313 	    OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
314 	    dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
315 	    "parent device");
316 }
317 
318 static void
319 device_sysctl_update(device_t dev)
320 {
321 	devclass_t dc = dev->devclass;
322 
323 	if (dev->sysctl_tree == NULL)
324 		return;
325 	sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
326 }
327 
328 static void
329 device_sysctl_fini(device_t dev)
330 {
331 	if (dev->sysctl_tree == NULL)
332 		return;
333 	sysctl_ctx_free(&dev->sysctl_ctx);
334 	dev->sysctl_tree = NULL;
335 }
336 
337 /*
338  * /dev/devctl implementation
339  */
340 
341 /*
342  * This design allows only one reader for /dev/devctl.  This is not desirable
343  * in the long run, but will get a lot of hair out of this implementation.
344  * Maybe we should make this device a clonable device.
345  *
346  * Also note: we specifically do not attach a device to the device_t tree
347  * to avoid potential chicken and egg problems.  One could argue that all
348  * of this belongs to the root node.  One could also further argue that the
349  * sysctl interface that we have not might more properly be an ioctl
350  * interface, but at this stage of the game, I'm not inclined to rock that
351  * boat.
352  *
353  * I'm also not sure that the SIGIO support is done correctly or not, as
354  * I copied it from a driver that had SIGIO support that likely hasn't been
355  * tested since 3.4 or 2.2.8!
356  */
357 
358 /* Deprecated way to adjust queue length */
359 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
360 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RWTUN |
361     CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_disable, "I",
362     "devctl disable -- deprecated");
363 
364 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
365 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
366 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
367 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN |
368     CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
369 
370 static d_open_t		devopen;
371 static d_close_t	devclose;
372 static d_read_t		devread;
373 static d_ioctl_t	devioctl;
374 static d_poll_t		devpoll;
375 static d_kqfilter_t	devkqfilter;
376 
377 static struct cdevsw dev_cdevsw = {
378 	.d_version =	D_VERSION,
379 	.d_open =	devopen,
380 	.d_close =	devclose,
381 	.d_read =	devread,
382 	.d_ioctl =	devioctl,
383 	.d_poll =	devpoll,
384 	.d_kqfilter =	devkqfilter,
385 	.d_name =	"devctl",
386 };
387 
388 struct dev_event_info
389 {
390 	char *dei_data;
391 	TAILQ_ENTRY(dev_event_info) dei_link;
392 };
393 
394 TAILQ_HEAD(devq, dev_event_info);
395 
396 static struct dev_softc
397 {
398 	int	inuse;
399 	int	nonblock;
400 	int	queued;
401 	int	async;
402 	struct mtx mtx;
403 	struct cv cv;
404 	struct selinfo sel;
405 	struct devq devq;
406 	struct sigio *sigio;
407 } devsoftc;
408 
409 static void	filt_devctl_detach(struct knote *kn);
410 static int	filt_devctl_read(struct knote *kn, long hint);
411 
412 struct filterops devctl_rfiltops = {
413 	.f_isfd = 1,
414 	.f_detach = filt_devctl_detach,
415 	.f_event = filt_devctl_read,
416 };
417 
418 static struct cdev *devctl_dev;
419 
420 static void
421 devinit(void)
422 {
423 	devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
424 	    UID_ROOT, GID_WHEEL, 0600, "devctl");
425 	mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
426 	cv_init(&devsoftc.cv, "dev cv");
427 	TAILQ_INIT(&devsoftc.devq);
428 	knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
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 			 * The driver returned an error so it
2116 			 * certainly doesn't match.
2117 			 */
2118 			if (result > 0) {
2119 				(void)device_set_driver(child, NULL);
2120 				continue;
2121 			}
2122 
2123 			/*
2124 			 * A priority lower than SUCCESS, remember the
2125 			 * best matching driver. Initialise the value
2126 			 * of pri for the first match.
2127 			 */
2128 			if (best == NULL || result > pri) {
2129 				/*
2130 				 * Probes that return BUS_PROBE_NOWILDCARD
2131 				 * or lower only match on devices whose
2132 				 * driver was explicitly specified.
2133 				 */
2134 				if (result <= BUS_PROBE_NOWILDCARD &&
2135 				    !(child->flags & DF_FIXEDCLASS))
2136 					continue;
2137 				best = dl;
2138 				pri = result;
2139 				continue;
2140 			}
2141 		}
2142 		/*
2143 		 * If we have an unambiguous match in this devclass,
2144 		 * don't look in the parent.
2145 		 */
2146 		if (best && pri == 0)
2147 			break;
2148 	}
2149 
2150 	/*
2151 	 * If we found a driver, change state and initialise the devclass.
2152 	 */
2153 	/* XXX What happens if we rebid and got no best? */
2154 	if (best) {
2155 		/*
2156 		 * If this device was attached, and we were asked to
2157 		 * rescan, and it is a different driver, then we have
2158 		 * to detach the old driver and reattach this new one.
2159 		 * Note, we don't have to check for DF_REBID here
2160 		 * because if the state is > DS_ALIVE, we know it must
2161 		 * be.
2162 		 *
2163 		 * This assumes that all DF_REBID drivers can have
2164 		 * their probe routine called at any time and that
2165 		 * they are idempotent as well as completely benign in
2166 		 * normal operations.
2167 		 *
2168 		 * We also have to make sure that the detach
2169 		 * succeeded, otherwise we fail the operation (or
2170 		 * maybe it should just fail silently?  I'm torn).
2171 		 */
2172 		if (child->state > DS_ALIVE && best->driver != child->driver)
2173 			if ((result = device_detach(dev)) != 0)
2174 				return (result);
2175 
2176 		/* Set the winning driver, devclass, and flags. */
2177 		if (!child->devclass) {
2178 			result = device_set_devclass(child, best->driver->name);
2179 			if (result != 0)
2180 				return (result);
2181 		}
2182 		result = device_set_driver(child, best->driver);
2183 		if (result != 0)
2184 			return (result);
2185 		resource_int_value(best->driver->name, child->unit,
2186 		    "flags", &child->devflags);
2187 
2188 		if (pri < 0) {
2189 			/*
2190 			 * A bit bogus. Call the probe method again to make
2191 			 * sure that we have the right description.
2192 			 */
2193 			DEVICE_PROBE(child);
2194 #if 0
2195 			child->flags |= DF_REBID;
2196 #endif
2197 		} else
2198 			child->flags &= ~DF_REBID;
2199 		child->state = DS_ALIVE;
2200 
2201 		bus_data_generation_update();
2202 		return (0);
2203 	}
2204 
2205 	return (ENXIO);
2206 }
2207 
2208 /**
2209  * @brief Return the parent of a device
2210  */
2211 device_t
2212 device_get_parent(device_t dev)
2213 {
2214 	return (dev->parent);
2215 }
2216 
2217 /**
2218  * @brief Get a list of children of a device
2219  *
2220  * An array containing a list of all the children of the given device
2221  * is allocated and returned in @p *devlistp. The number of devices
2222  * in the array is returned in @p *devcountp. The caller should free
2223  * the array using @c free(p, M_TEMP).
2224  *
2225  * @param dev		the device to examine
2226  * @param devlistp	points at location for array pointer return
2227  *			value
2228  * @param devcountp	points at location for array size return value
2229  *
2230  * @retval 0		success
2231  * @retval ENOMEM	the array allocation failed
2232  */
2233 int
2234 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2235 {
2236 	int count;
2237 	device_t child;
2238 	device_t *list;
2239 
2240 	count = 0;
2241 	TAILQ_FOREACH(child, &dev->children, link) {
2242 		count++;
2243 	}
2244 	if (count == 0) {
2245 		*devlistp = NULL;
2246 		*devcountp = 0;
2247 		return (0);
2248 	}
2249 
2250 	list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2251 	if (!list)
2252 		return (ENOMEM);
2253 
2254 	count = 0;
2255 	TAILQ_FOREACH(child, &dev->children, link) {
2256 		list[count] = child;
2257 		count++;
2258 	}
2259 
2260 	*devlistp = list;
2261 	*devcountp = count;
2262 
2263 	return (0);
2264 }
2265 
2266 /**
2267  * @brief Return the current driver for the device or @c NULL if there
2268  * is no driver currently attached
2269  */
2270 driver_t *
2271 device_get_driver(device_t dev)
2272 {
2273 	return (dev->driver);
2274 }
2275 
2276 /**
2277  * @brief Return the current devclass for the device or @c NULL if
2278  * there is none.
2279  */
2280 devclass_t
2281 device_get_devclass(device_t dev)
2282 {
2283 	return (dev->devclass);
2284 }
2285 
2286 /**
2287  * @brief Return the name of the device's devclass or @c NULL if there
2288  * is none.
2289  */
2290 const char *
2291 device_get_name(device_t dev)
2292 {
2293 	if (dev != NULL && dev->devclass)
2294 		return (devclass_get_name(dev->devclass));
2295 	return (NULL);
2296 }
2297 
2298 /**
2299  * @brief Return a string containing the device's devclass name
2300  * followed by an ascii representation of the device's unit number
2301  * (e.g. @c "foo2").
2302  */
2303 const char *
2304 device_get_nameunit(device_t dev)
2305 {
2306 	return (dev->nameunit);
2307 }
2308 
2309 /**
2310  * @brief Return the device's unit number.
2311  */
2312 int
2313 device_get_unit(device_t dev)
2314 {
2315 	return (dev->unit);
2316 }
2317 
2318 /**
2319  * @brief Return the device's description string
2320  */
2321 const char *
2322 device_get_desc(device_t dev)
2323 {
2324 	return (dev->desc);
2325 }
2326 
2327 /**
2328  * @brief Return the device's flags
2329  */
2330 uint32_t
2331 device_get_flags(device_t dev)
2332 {
2333 	return (dev->devflags);
2334 }
2335 
2336 struct sysctl_ctx_list *
2337 device_get_sysctl_ctx(device_t dev)
2338 {
2339 	return (&dev->sysctl_ctx);
2340 }
2341 
2342 struct sysctl_oid *
2343 device_get_sysctl_tree(device_t dev)
2344 {
2345 	return (dev->sysctl_tree);
2346 }
2347 
2348 /**
2349  * @brief Print the name of the device followed by a colon and a space
2350  *
2351  * @returns the number of characters printed
2352  */
2353 int
2354 device_print_prettyname(device_t dev)
2355 {
2356 	const char *name = device_get_name(dev);
2357 
2358 	if (name == NULL)
2359 		return (printf("unknown: "));
2360 	return (printf("%s%d: ", name, device_get_unit(dev)));
2361 }
2362 
2363 /**
2364  * @brief Print the name of the device followed by a colon, a space
2365  * and the result of calling vprintf() with the value of @p fmt and
2366  * the following arguments.
2367  *
2368  * @returns the number of characters printed
2369  */
2370 int
2371 device_printf(device_t dev, const char * fmt, ...)
2372 {
2373 	va_list ap;
2374 	int retval;
2375 
2376 	retval = device_print_prettyname(dev);
2377 	va_start(ap, fmt);
2378 	retval += vprintf(fmt, ap);
2379 	va_end(ap);
2380 	return (retval);
2381 }
2382 
2383 /**
2384  * @internal
2385  */
2386 static void
2387 device_set_desc_internal(device_t dev, const char* desc, int copy)
2388 {
2389 	if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2390 		free(dev->desc, M_BUS);
2391 		dev->flags &= ~DF_DESCMALLOCED;
2392 		dev->desc = NULL;
2393 	}
2394 
2395 	if (copy && desc) {
2396 		dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2397 		if (dev->desc) {
2398 			strcpy(dev->desc, desc);
2399 			dev->flags |= DF_DESCMALLOCED;
2400 		}
2401 	} else {
2402 		/* Avoid a -Wcast-qual warning */
2403 		dev->desc = (char *)(uintptr_t) desc;
2404 	}
2405 
2406 	bus_data_generation_update();
2407 }
2408 
2409 /**
2410  * @brief Set the device's description
2411  *
2412  * The value of @c desc should be a string constant that will not
2413  * change (at least until the description is changed in a subsequent
2414  * call to device_set_desc() or device_set_desc_copy()).
2415  */
2416 void
2417 device_set_desc(device_t dev, const char* desc)
2418 {
2419 	device_set_desc_internal(dev, desc, FALSE);
2420 }
2421 
2422 /**
2423  * @brief Set the device's description
2424  *
2425  * The string pointed to by @c desc is copied. Use this function if
2426  * the device description is generated, (e.g. with sprintf()).
2427  */
2428 void
2429 device_set_desc_copy(device_t dev, const char* desc)
2430 {
2431 	device_set_desc_internal(dev, desc, TRUE);
2432 }
2433 
2434 /**
2435  * @brief Set the device's flags
2436  */
2437 void
2438 device_set_flags(device_t dev, uint32_t flags)
2439 {
2440 	dev->devflags = flags;
2441 }
2442 
2443 /**
2444  * @brief Return the device's softc field
2445  *
2446  * The softc is allocated and zeroed when a driver is attached, based
2447  * on the size field of the driver.
2448  */
2449 void *
2450 device_get_softc(device_t dev)
2451 {
2452 	return (dev->softc);
2453 }
2454 
2455 /**
2456  * @brief Set the device's softc field
2457  *
2458  * Most drivers do not need to use this since the softc is allocated
2459  * automatically when the driver is attached.
2460  */
2461 void
2462 device_set_softc(device_t dev, void *softc)
2463 {
2464 	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2465 		free(dev->softc, M_BUS_SC);
2466 	dev->softc = softc;
2467 	if (dev->softc)
2468 		dev->flags |= DF_EXTERNALSOFTC;
2469 	else
2470 		dev->flags &= ~DF_EXTERNALSOFTC;
2471 }
2472 
2473 /**
2474  * @brief Free claimed softc
2475  *
2476  * Most drivers do not need to use this since the softc is freed
2477  * automatically when the driver is detached.
2478  */
2479 void
2480 device_free_softc(void *softc)
2481 {
2482 	free(softc, M_BUS_SC);
2483 }
2484 
2485 /**
2486  * @brief Claim softc
2487  *
2488  * This function can be used to let the driver free the automatically
2489  * allocated softc using "device_free_softc()". This function is
2490  * useful when the driver is refcounting the softc and the softc
2491  * cannot be freed when the "device_detach" method is called.
2492  */
2493 void
2494 device_claim_softc(device_t dev)
2495 {
2496 	if (dev->softc)
2497 		dev->flags |= DF_EXTERNALSOFTC;
2498 	else
2499 		dev->flags &= ~DF_EXTERNALSOFTC;
2500 }
2501 
2502 /**
2503  * @brief Get the device's ivars field
2504  *
2505  * The ivars field is used by the parent device to store per-device
2506  * state (e.g. the physical location of the device or a list of
2507  * resources).
2508  */
2509 void *
2510 device_get_ivars(device_t dev)
2511 {
2512 
2513 	KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2514 	return (dev->ivars);
2515 }
2516 
2517 /**
2518  * @brief Set the device's ivars field
2519  */
2520 void
2521 device_set_ivars(device_t dev, void * ivars)
2522 {
2523 
2524 	KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2525 	dev->ivars = ivars;
2526 }
2527 
2528 /**
2529  * @brief Return the device's state
2530  */
2531 device_state_t
2532 device_get_state(device_t dev)
2533 {
2534 	return (dev->state);
2535 }
2536 
2537 /**
2538  * @brief Set the DF_ENABLED flag for the device
2539  */
2540 void
2541 device_enable(device_t dev)
2542 {
2543 	dev->flags |= DF_ENABLED;
2544 }
2545 
2546 /**
2547  * @brief Clear the DF_ENABLED flag for the device
2548  */
2549 void
2550 device_disable(device_t dev)
2551 {
2552 	dev->flags &= ~DF_ENABLED;
2553 }
2554 
2555 /**
2556  * @brief Increment the busy counter for the device
2557  */
2558 void
2559 device_busy(device_t dev)
2560 {
2561 	if (dev->state < DS_ATTACHING)
2562 		panic("device_busy: called for unattached device");
2563 	if (dev->busy == 0 && dev->parent)
2564 		device_busy(dev->parent);
2565 	dev->busy++;
2566 	if (dev->state == DS_ATTACHED)
2567 		dev->state = DS_BUSY;
2568 }
2569 
2570 /**
2571  * @brief Decrement the busy counter for the device
2572  */
2573 void
2574 device_unbusy(device_t dev)
2575 {
2576 	if (dev->busy != 0 && dev->state != DS_BUSY &&
2577 	    dev->state != DS_ATTACHING)
2578 		panic("device_unbusy: called for non-busy device %s",
2579 		    device_get_nameunit(dev));
2580 	dev->busy--;
2581 	if (dev->busy == 0) {
2582 		if (dev->parent)
2583 			device_unbusy(dev->parent);
2584 		if (dev->state == DS_BUSY)
2585 			dev->state = DS_ATTACHED;
2586 	}
2587 }
2588 
2589 /**
2590  * @brief Set the DF_QUIET flag for the device
2591  */
2592 void
2593 device_quiet(device_t dev)
2594 {
2595 	dev->flags |= DF_QUIET;
2596 }
2597 
2598 /**
2599  * @brief Clear the DF_QUIET flag for the device
2600  */
2601 void
2602 device_verbose(device_t dev)
2603 {
2604 	dev->flags &= ~DF_QUIET;
2605 }
2606 
2607 /**
2608  * @brief Return non-zero if the DF_QUIET flag is set on the device
2609  */
2610 int
2611 device_is_quiet(device_t dev)
2612 {
2613 	return ((dev->flags & DF_QUIET) != 0);
2614 }
2615 
2616 /**
2617  * @brief Return non-zero if the DF_ENABLED flag is set on the device
2618  */
2619 int
2620 device_is_enabled(device_t dev)
2621 {
2622 	return ((dev->flags & DF_ENABLED) != 0);
2623 }
2624 
2625 /**
2626  * @brief Return non-zero if the device was successfully probed
2627  */
2628 int
2629 device_is_alive(device_t dev)
2630 {
2631 	return (dev->state >= DS_ALIVE);
2632 }
2633 
2634 /**
2635  * @brief Return non-zero if the device currently has a driver
2636  * attached to it
2637  */
2638 int
2639 device_is_attached(device_t dev)
2640 {
2641 	return (dev->state >= DS_ATTACHED);
2642 }
2643 
2644 /**
2645  * @brief Set the devclass of a device
2646  * @see devclass_add_device().
2647  */
2648 int
2649 device_set_devclass(device_t dev, const char *classname)
2650 {
2651 	devclass_t dc;
2652 	int error;
2653 
2654 	if (!classname) {
2655 		if (dev->devclass)
2656 			devclass_delete_device(dev->devclass, dev);
2657 		return (0);
2658 	}
2659 
2660 	if (dev->devclass) {
2661 		printf("device_set_devclass: device class already set\n");
2662 		return (EINVAL);
2663 	}
2664 
2665 	dc = devclass_find_internal(classname, NULL, TRUE);
2666 	if (!dc)
2667 		return (ENOMEM);
2668 
2669 	error = devclass_add_device(dc, dev);
2670 
2671 	bus_data_generation_update();
2672 	return (error);
2673 }
2674 
2675 /**
2676  * @brief Set the driver of a device
2677  *
2678  * @retval 0		success
2679  * @retval EBUSY	the device already has a driver attached
2680  * @retval ENOMEM	a memory allocation failure occurred
2681  */
2682 int
2683 device_set_driver(device_t dev, driver_t *driver)
2684 {
2685 	if (dev->state >= DS_ATTACHED)
2686 		return (EBUSY);
2687 
2688 	if (dev->driver == driver)
2689 		return (0);
2690 
2691 	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2692 		free(dev->softc, M_BUS_SC);
2693 		dev->softc = NULL;
2694 	}
2695 	device_set_desc(dev, NULL);
2696 	kobj_delete((kobj_t) dev, NULL);
2697 	dev->driver = driver;
2698 	if (driver) {
2699 		kobj_init((kobj_t) dev, (kobj_class_t) driver);
2700 		if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2701 			dev->softc = malloc(driver->size, M_BUS_SC,
2702 			    M_NOWAIT | M_ZERO);
2703 			if (!dev->softc) {
2704 				kobj_delete((kobj_t) dev, NULL);
2705 				kobj_init((kobj_t) dev, &null_class);
2706 				dev->driver = NULL;
2707 				return (ENOMEM);
2708 			}
2709 		}
2710 	} else {
2711 		kobj_init((kobj_t) dev, &null_class);
2712 	}
2713 
2714 	bus_data_generation_update();
2715 	return (0);
2716 }
2717 
2718 /**
2719  * @brief Probe a device, and return this status.
2720  *
2721  * This function is the core of the device autoconfiguration
2722  * system. Its purpose is to select a suitable driver for a device and
2723  * then call that driver to initialise the hardware appropriately. The
2724  * driver is selected by calling the DEVICE_PROBE() method of a set of
2725  * candidate drivers and then choosing the driver which returned the
2726  * best value. This driver is then attached to the device using
2727  * device_attach().
2728  *
2729  * The set of suitable drivers is taken from the list of drivers in
2730  * the parent device's devclass. If the device was originally created
2731  * with a specific class name (see device_add_child()), only drivers
2732  * with that name are probed, otherwise all drivers in the devclass
2733  * are probed. If no drivers return successful probe values in the
2734  * parent devclass, the search continues in the parent of that
2735  * devclass (see devclass_get_parent()) if any.
2736  *
2737  * @param dev		the device to initialise
2738  *
2739  * @retval 0		success
2740  * @retval ENXIO	no driver was found
2741  * @retval ENOMEM	memory allocation failure
2742  * @retval non-zero	some other unix error code
2743  * @retval -1		Device already attached
2744  */
2745 int
2746 device_probe(device_t dev)
2747 {
2748 	int error;
2749 
2750 	GIANT_REQUIRED;
2751 
2752 	if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2753 		return (-1);
2754 
2755 	if (!(dev->flags & DF_ENABLED)) {
2756 		if (bootverbose && device_get_name(dev) != NULL) {
2757 			device_print_prettyname(dev);
2758 			printf("not probed (disabled)\n");
2759 		}
2760 		return (-1);
2761 	}
2762 	if ((error = device_probe_child(dev->parent, dev)) != 0) {
2763 		if (bus_current_pass == BUS_PASS_DEFAULT &&
2764 		    !(dev->flags & DF_DONENOMATCH)) {
2765 			BUS_PROBE_NOMATCH(dev->parent, dev);
2766 			devnomatch(dev);
2767 			dev->flags |= DF_DONENOMATCH;
2768 		}
2769 		return (error);
2770 	}
2771 	return (0);
2772 }
2773 
2774 /**
2775  * @brief Probe a device and attach a driver if possible
2776  *
2777  * calls device_probe() and attaches if that was successful.
2778  */
2779 int
2780 device_probe_and_attach(device_t dev)
2781 {
2782 	int error;
2783 
2784 	GIANT_REQUIRED;
2785 
2786 	error = device_probe(dev);
2787 	if (error == -1)
2788 		return (0);
2789 	else if (error != 0)
2790 		return (error);
2791 
2792 	CURVNET_SET_QUIET(vnet0);
2793 	error = device_attach(dev);
2794 	CURVNET_RESTORE();
2795 	return error;
2796 }
2797 
2798 /**
2799  * @brief Attach a device driver to a device
2800  *
2801  * This function is a wrapper around the DEVICE_ATTACH() driver
2802  * method. In addition to calling DEVICE_ATTACH(), it initialises the
2803  * device's sysctl tree, optionally prints a description of the device
2804  * and queues a notification event for user-based device management
2805  * services.
2806  *
2807  * Normally this function is only called internally from
2808  * device_probe_and_attach().
2809  *
2810  * @param dev		the device to initialise
2811  *
2812  * @retval 0		success
2813  * @retval ENXIO	no driver was found
2814  * @retval ENOMEM	memory allocation failure
2815  * @retval non-zero	some other unix error code
2816  */
2817 int
2818 device_attach(device_t dev)
2819 {
2820 	uint64_t attachtime;
2821 	int error;
2822 
2823 	if (resource_disabled(dev->driver->name, dev->unit)) {
2824 		device_disable(dev);
2825 		if (bootverbose)
2826 			 device_printf(dev, "disabled via hints entry\n");
2827 		return (ENXIO);
2828 	}
2829 
2830 	device_sysctl_init(dev);
2831 	if (!device_is_quiet(dev))
2832 		device_print_child(dev->parent, dev);
2833 	attachtime = get_cyclecount();
2834 	dev->state = DS_ATTACHING;
2835 	if ((error = DEVICE_ATTACH(dev)) != 0) {
2836 		printf("device_attach: %s%d attach returned %d\n",
2837 		    dev->driver->name, dev->unit, error);
2838 		if (!(dev->flags & DF_FIXEDCLASS))
2839 			devclass_delete_device(dev->devclass, dev);
2840 		(void)device_set_driver(dev, NULL);
2841 		device_sysctl_fini(dev);
2842 		KASSERT(dev->busy == 0, ("attach failed but busy"));
2843 		dev->state = DS_NOTPRESENT;
2844 		return (error);
2845 	}
2846 	attachtime = get_cyclecount() - attachtime;
2847 	/*
2848 	 * 4 bits per device is a reasonable value for desktop and server
2849 	 * hardware with good get_cyclecount() implementations, but may
2850 	 * need to be adjusted on other platforms.
2851 	 */
2852 #ifdef RANDOM_DEBUG
2853 	printf("%s(): feeding %d bit(s) of entropy from %s%d\n",
2854 	    __func__, 4, dev->driver->name, dev->unit);
2855 #endif
2856 	random_harvest(&attachtime, sizeof(attachtime), 4, RANDOM_ATTACH);
2857 	device_sysctl_update(dev);
2858 	if (dev->busy)
2859 		dev->state = DS_BUSY;
2860 	else
2861 		dev->state = DS_ATTACHED;
2862 	dev->flags &= ~DF_DONENOMATCH;
2863 	devadded(dev);
2864 	return (0);
2865 }
2866 
2867 /**
2868  * @brief Detach a driver from a device
2869  *
2870  * This function is a wrapper around the DEVICE_DETACH() driver
2871  * method. If the call to DEVICE_DETACH() succeeds, it calls
2872  * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2873  * notification event for user-based device management services and
2874  * cleans up the device's sysctl tree.
2875  *
2876  * @param dev		the device to un-initialise
2877  *
2878  * @retval 0		success
2879  * @retval ENXIO	no driver was found
2880  * @retval ENOMEM	memory allocation failure
2881  * @retval non-zero	some other unix error code
2882  */
2883 int
2884 device_detach(device_t dev)
2885 {
2886 	int error;
2887 
2888 	GIANT_REQUIRED;
2889 
2890 	PDEBUG(("%s", DEVICENAME(dev)));
2891 	if (dev->state == DS_BUSY)
2892 		return (EBUSY);
2893 	if (dev->state != DS_ATTACHED)
2894 		return (0);
2895 
2896 	if ((error = DEVICE_DETACH(dev)) != 0)
2897 		return (error);
2898 	devremoved(dev);
2899 	if (!device_is_quiet(dev))
2900 		device_printf(dev, "detached\n");
2901 	if (dev->parent)
2902 		BUS_CHILD_DETACHED(dev->parent, dev);
2903 
2904 	if (!(dev->flags & DF_FIXEDCLASS))
2905 		devclass_delete_device(dev->devclass, dev);
2906 
2907 	dev->state = DS_NOTPRESENT;
2908 	(void)device_set_driver(dev, NULL);
2909 	device_sysctl_fini(dev);
2910 
2911 	return (0);
2912 }
2913 
2914 /**
2915  * @brief Tells a driver to quiesce itself.
2916  *
2917  * This function is a wrapper around the DEVICE_QUIESCE() driver
2918  * method. If the call to DEVICE_QUIESCE() succeeds.
2919  *
2920  * @param dev		the device to quiesce
2921  *
2922  * @retval 0		success
2923  * @retval ENXIO	no driver was found
2924  * @retval ENOMEM	memory allocation failure
2925  * @retval non-zero	some other unix error code
2926  */
2927 int
2928 device_quiesce(device_t dev)
2929 {
2930 
2931 	PDEBUG(("%s", DEVICENAME(dev)));
2932 	if (dev->state == DS_BUSY)
2933 		return (EBUSY);
2934 	if (dev->state != DS_ATTACHED)
2935 		return (0);
2936 
2937 	return (DEVICE_QUIESCE(dev));
2938 }
2939 
2940 /**
2941  * @brief Notify a device of system shutdown
2942  *
2943  * This function calls the DEVICE_SHUTDOWN() driver method if the
2944  * device currently has an attached driver.
2945  *
2946  * @returns the value returned by DEVICE_SHUTDOWN()
2947  */
2948 int
2949 device_shutdown(device_t dev)
2950 {
2951 	if (dev->state < DS_ATTACHED)
2952 		return (0);
2953 	return (DEVICE_SHUTDOWN(dev));
2954 }
2955 
2956 /**
2957  * @brief Set the unit number of a device
2958  *
2959  * This function can be used to override the unit number used for a
2960  * device (e.g. to wire a device to a pre-configured unit number).
2961  */
2962 int
2963 device_set_unit(device_t dev, int unit)
2964 {
2965 	devclass_t dc;
2966 	int err;
2967 
2968 	dc = device_get_devclass(dev);
2969 	if (unit < dc->maxunit && dc->devices[unit])
2970 		return (EBUSY);
2971 	err = devclass_delete_device(dc, dev);
2972 	if (err)
2973 		return (err);
2974 	dev->unit = unit;
2975 	err = devclass_add_device(dc, dev);
2976 	if (err)
2977 		return (err);
2978 
2979 	bus_data_generation_update();
2980 	return (0);
2981 }
2982 
2983 /*======================================*/
2984 /*
2985  * Some useful method implementations to make life easier for bus drivers.
2986  */
2987 
2988 /**
2989  * @brief Initialise a resource list.
2990  *
2991  * @param rl		the resource list to initialise
2992  */
2993 void
2994 resource_list_init(struct resource_list *rl)
2995 {
2996 	STAILQ_INIT(rl);
2997 }
2998 
2999 /**
3000  * @brief Reclaim memory used by a resource list.
3001  *
3002  * This function frees the memory for all resource entries on the list
3003  * (if any).
3004  *
3005  * @param rl		the resource list to free
3006  */
3007 void
3008 resource_list_free(struct resource_list *rl)
3009 {
3010 	struct resource_list_entry *rle;
3011 
3012 	while ((rle = STAILQ_FIRST(rl)) != NULL) {
3013 		if (rle->res)
3014 			panic("resource_list_free: resource entry is busy");
3015 		STAILQ_REMOVE_HEAD(rl, link);
3016 		free(rle, M_BUS);
3017 	}
3018 }
3019 
3020 /**
3021  * @brief Add a resource entry.
3022  *
3023  * This function adds a resource entry using the given @p type, @p
3024  * start, @p end and @p count values. A rid value is chosen by
3025  * searching sequentially for the first unused rid starting at zero.
3026  *
3027  * @param rl		the resource list to edit
3028  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3029  * @param start		the start address of the resource
3030  * @param end		the end address of the resource
3031  * @param count		XXX end-start+1
3032  */
3033 int
3034 resource_list_add_next(struct resource_list *rl, int type, u_long start,
3035     u_long end, u_long count)
3036 {
3037 	int rid;
3038 
3039 	rid = 0;
3040 	while (resource_list_find(rl, type, rid) != NULL)
3041 		rid++;
3042 	resource_list_add(rl, type, rid, start, end, count);
3043 	return (rid);
3044 }
3045 
3046 /**
3047  * @brief Add or modify a resource entry.
3048  *
3049  * If an existing entry exists with the same type and rid, it will be
3050  * modified using the given values of @p start, @p end and @p
3051  * count. If no entry exists, a new one will be created using the
3052  * given values.  The resource list entry that matches is then returned.
3053  *
3054  * @param rl		the resource list to edit
3055  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3056  * @param rid		the resource identifier
3057  * @param start		the start address of the resource
3058  * @param end		the end address of the resource
3059  * @param count		XXX end-start+1
3060  */
3061 struct resource_list_entry *
3062 resource_list_add(struct resource_list *rl, int type, int rid,
3063     u_long start, u_long end, u_long count)
3064 {
3065 	struct resource_list_entry *rle;
3066 
3067 	rle = resource_list_find(rl, type, rid);
3068 	if (!rle) {
3069 		rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3070 		    M_NOWAIT);
3071 		if (!rle)
3072 			panic("resource_list_add: can't record entry");
3073 		STAILQ_INSERT_TAIL(rl, rle, link);
3074 		rle->type = type;
3075 		rle->rid = rid;
3076 		rle->res = NULL;
3077 		rle->flags = 0;
3078 	}
3079 
3080 	if (rle->res)
3081 		panic("resource_list_add: resource entry is busy");
3082 
3083 	rle->start = start;
3084 	rle->end = end;
3085 	rle->count = count;
3086 	return (rle);
3087 }
3088 
3089 /**
3090  * @brief Determine if a resource entry is busy.
3091  *
3092  * Returns true if a resource entry is busy meaning that it has an
3093  * associated resource that is not an unallocated "reserved" resource.
3094  *
3095  * @param rl		the resource list to search
3096  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3097  * @param rid		the resource identifier
3098  *
3099  * @returns Non-zero if the entry is busy, zero otherwise.
3100  */
3101 int
3102 resource_list_busy(struct resource_list *rl, int type, int rid)
3103 {
3104 	struct resource_list_entry *rle;
3105 
3106 	rle = resource_list_find(rl, type, rid);
3107 	if (rle == NULL || rle->res == NULL)
3108 		return (0);
3109 	if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3110 		KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3111 		    ("reserved resource is active"));
3112 		return (0);
3113 	}
3114 	return (1);
3115 }
3116 
3117 /**
3118  * @brief Determine if a resource entry is reserved.
3119  *
3120  * Returns true if a resource entry is reserved meaning that it has an
3121  * associated "reserved" resource.  The resource can either be
3122  * allocated or unallocated.
3123  *
3124  * @param rl		the resource list to search
3125  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3126  * @param rid		the resource identifier
3127  *
3128  * @returns Non-zero if the entry is reserved, zero otherwise.
3129  */
3130 int
3131 resource_list_reserved(struct resource_list *rl, int type, int rid)
3132 {
3133 	struct resource_list_entry *rle;
3134 
3135 	rle = resource_list_find(rl, type, rid);
3136 	if (rle != NULL && rle->flags & RLE_RESERVED)
3137 		return (1);
3138 	return (0);
3139 }
3140 
3141 /**
3142  * @brief Find a resource entry by type and rid.
3143  *
3144  * @param rl		the resource list to search
3145  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3146  * @param rid		the resource identifier
3147  *
3148  * @returns the resource entry pointer or NULL if there is no such
3149  * entry.
3150  */
3151 struct resource_list_entry *
3152 resource_list_find(struct resource_list *rl, int type, int rid)
3153 {
3154 	struct resource_list_entry *rle;
3155 
3156 	STAILQ_FOREACH(rle, rl, link) {
3157 		if (rle->type == type && rle->rid == rid)
3158 			return (rle);
3159 	}
3160 	return (NULL);
3161 }
3162 
3163 /**
3164  * @brief Delete a resource entry.
3165  *
3166  * @param rl		the resource list to edit
3167  * @param type		the resource entry type (e.g. SYS_RES_MEMORY)
3168  * @param rid		the resource identifier
3169  */
3170 void
3171 resource_list_delete(struct resource_list *rl, int type, int rid)
3172 {
3173 	struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3174 
3175 	if (rle) {
3176 		if (rle->res != NULL)
3177 			panic("resource_list_delete: resource has not been released");
3178 		STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3179 		free(rle, M_BUS);
3180 	}
3181 }
3182 
3183 /**
3184  * @brief Allocate a reserved resource
3185  *
3186  * This can be used by busses to force the allocation of resources
3187  * that are always active in the system even if they are not allocated
3188  * by a driver (e.g. PCI BARs).  This function is usually called when
3189  * adding a new child to the bus.  The resource is allocated from the
3190  * parent bus when it is reserved.  The resource list entry is marked
3191  * with RLE_RESERVED to note that it is a reserved resource.
3192  *
3193  * Subsequent attempts to allocate the resource with
3194  * resource_list_alloc() will succeed the first time and will set
3195  * RLE_ALLOCATED to note that it has been allocated.  When a reserved
3196  * resource that has been allocated is released with
3197  * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3198  * the actual resource remains allocated.  The resource can be released to
3199  * the parent bus by calling resource_list_unreserve().
3200  *
3201  * @param rl		the resource list to allocate from
3202  * @param bus		the parent device of @p child
3203  * @param child		the device for which the resource is being reserved
3204  * @param type		the type of resource to allocate
3205  * @param rid		a pointer to the resource identifier
3206  * @param start		hint at the start of the resource range - pass
3207  *			@c 0UL for any start address
3208  * @param end		hint at the end of the resource range - pass
3209  *			@c ~0UL for any end address
3210  * @param count		hint at the size of range required - pass @c 1
3211  *			for any size
3212  * @param flags		any extra flags to control the resource
3213  *			allocation - see @c RF_XXX flags in
3214  *			<sys/rman.h> for details
3215  *
3216  * @returns		the resource which was allocated or @c NULL if no
3217  *			resource could be allocated
3218  */
3219 struct resource *
3220 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3221     int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3222 {
3223 	struct resource_list_entry *rle = NULL;
3224 	int passthrough = (device_get_parent(child) != bus);
3225 	struct resource *r;
3226 
3227 	if (passthrough)
3228 		panic(
3229     "resource_list_reserve() should only be called for direct children");
3230 	if (flags & RF_ACTIVE)
3231 		panic(
3232     "resource_list_reserve() should only reserve inactive resources");
3233 
3234 	r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3235 	    flags);
3236 	if (r != NULL) {
3237 		rle = resource_list_find(rl, type, *rid);
3238 		rle->flags |= RLE_RESERVED;
3239 	}
3240 	return (r);
3241 }
3242 
3243 /**
3244  * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3245  *
3246  * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3247  * and passing the allocation up to the parent of @p bus. This assumes
3248  * that the first entry of @c device_get_ivars(child) is a struct
3249  * resource_list. This also handles 'passthrough' allocations where a
3250  * child is a remote descendant of bus by passing the allocation up to
3251  * the parent of bus.
3252  *
3253  * Typically, a bus driver would store a list of child resources
3254  * somewhere in the child device's ivars (see device_get_ivars()) and
3255  * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3256  * then call resource_list_alloc() to perform the allocation.
3257  *
3258  * @param rl		the resource list to allocate from
3259  * @param bus		the parent device of @p child
3260  * @param child		the device which is requesting an allocation
3261  * @param type		the type of resource to allocate
3262  * @param rid		a pointer to the resource identifier
3263  * @param start		hint at the start of the resource range - pass
3264  *			@c 0UL for any start address
3265  * @param end		hint at the end of the resource range - pass
3266  *			@c ~0UL for any end address
3267  * @param count		hint at the size of range required - pass @c 1
3268  *			for any size
3269  * @param flags		any extra flags to control the resource
3270  *			allocation - see @c RF_XXX flags in
3271  *			<sys/rman.h> for details
3272  *
3273  * @returns		the resource which was allocated or @c NULL if no
3274  *			resource could be allocated
3275  */
3276 struct resource *
3277 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3278     int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3279 {
3280 	struct resource_list_entry *rle = NULL;
3281 	int passthrough = (device_get_parent(child) != bus);
3282 	int isdefault = (start == 0UL && end == ~0UL);
3283 
3284 	if (passthrough) {
3285 		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3286 		    type, rid, start, end, count, flags));
3287 	}
3288 
3289 	rle = resource_list_find(rl, type, *rid);
3290 
3291 	if (!rle)
3292 		return (NULL);		/* no resource of that type/rid */
3293 
3294 	if (rle->res) {
3295 		if (rle->flags & RLE_RESERVED) {
3296 			if (rle->flags & RLE_ALLOCATED)
3297 				return (NULL);
3298 			if ((flags & RF_ACTIVE) &&
3299 			    bus_activate_resource(child, type, *rid,
3300 			    rle->res) != 0)
3301 				return (NULL);
3302 			rle->flags |= RLE_ALLOCATED;
3303 			return (rle->res);
3304 		}
3305 		device_printf(bus,
3306 		    "resource entry %#x type %d for child %s is busy\n", *rid,
3307 		    type, device_get_nameunit(child));
3308 		return (NULL);
3309 	}
3310 
3311 	if (isdefault) {
3312 		start = rle->start;
3313 		count = ulmax(count, rle->count);
3314 		end = ulmax(rle->end, start + count - 1);
3315 	}
3316 
3317 	rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3318 	    type, rid, start, end, count, flags);
3319 
3320 	/*
3321 	 * Record the new range.
3322 	 */
3323 	if (rle->res) {
3324 		rle->start = rman_get_start(rle->res);
3325 		rle->end = rman_get_end(rle->res);
3326 		rle->count = count;
3327 	}
3328 
3329 	return (rle->res);
3330 }
3331 
3332 /**
3333  * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3334  *
3335  * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3336  * used with resource_list_alloc().
3337  *
3338  * @param rl		the resource list which was allocated from
3339  * @param bus		the parent device of @p child
3340  * @param child		the device which is requesting a release
3341  * @param type		the type of resource to release
3342  * @param rid		the resource identifier
3343  * @param res		the resource to release
3344  *
3345  * @retval 0		success
3346  * @retval non-zero	a standard unix error code indicating what
3347  *			error condition prevented the operation
3348  */
3349 int
3350 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3351     int type, int rid, struct resource *res)
3352 {
3353 	struct resource_list_entry *rle = NULL;
3354 	int passthrough = (device_get_parent(child) != bus);
3355 	int error;
3356 
3357 	if (passthrough) {
3358 		return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3359 		    type, rid, res));
3360 	}
3361 
3362 	rle = resource_list_find(rl, type, rid);
3363 
3364 	if (!rle)
3365 		panic("resource_list_release: can't find resource");
3366 	if (!rle->res)
3367 		panic("resource_list_release: resource entry is not busy");
3368 	if (rle->flags & RLE_RESERVED) {
3369 		if (rle->flags & RLE_ALLOCATED) {
3370 			if (rman_get_flags(res) & RF_ACTIVE) {
3371 				error = bus_deactivate_resource(child, type,
3372 				    rid, res);
3373 				if (error)
3374 					return (error);
3375 			}
3376 			rle->flags &= ~RLE_ALLOCATED;
3377 			return (0);
3378 		}
3379 		return (EINVAL);
3380 	}
3381 
3382 	error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3383 	    type, rid, res);
3384 	if (error)
3385 		return (error);
3386 
3387 	rle->res = NULL;
3388 	return (0);
3389 }
3390 
3391 /**
3392  * @brief Release all active resources of a given type
3393  *
3394  * Release all active resources of a specified type.  This is intended
3395  * to be used to cleanup resources leaked by a driver after detach or
3396  * a failed attach.
3397  *
3398  * @param rl		the resource list which was allocated from
3399  * @param bus		the parent device of @p child
3400  * @param child		the device whose active resources are being released
3401  * @param type		the type of resources to release
3402  *
3403  * @retval 0		success
3404  * @retval EBUSY	at least one resource was active
3405  */
3406 int
3407 resource_list_release_active(struct resource_list *rl, device_t bus,
3408     device_t child, int type)
3409 {
3410 	struct resource_list_entry *rle;
3411 	int error, retval;
3412 
3413 	retval = 0;
3414 	STAILQ_FOREACH(rle, rl, link) {
3415 		if (rle->type != type)
3416 			continue;
3417 		if (rle->res == NULL)
3418 			continue;
3419 		if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3420 		    RLE_RESERVED)
3421 			continue;
3422 		retval = EBUSY;
3423 		error = resource_list_release(rl, bus, child, type,
3424 		    rman_get_rid(rle->res), rle->res);
3425 		if (error != 0)
3426 			device_printf(bus,
3427 			    "Failed to release active resource: %d\n", error);
3428 	}
3429 	return (retval);
3430 }
3431 
3432 
3433 /**
3434  * @brief Fully release a reserved resource
3435  *
3436  * Fully releases a resource reserved via resource_list_reserve().
3437  *
3438  * @param rl		the resource list which was allocated from
3439  * @param bus		the parent device of @p child
3440  * @param child		the device whose reserved resource is being released
3441  * @param type		the type of resource to release
3442  * @param rid		the resource identifier
3443  * @param res		the resource to release
3444  *
3445  * @retval 0		success
3446  * @retval non-zero	a standard unix error code indicating what
3447  *			error condition prevented the operation
3448  */
3449 int
3450 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3451     int type, int rid)
3452 {
3453 	struct resource_list_entry *rle = NULL;
3454 	int passthrough = (device_get_parent(child) != bus);
3455 
3456 	if (passthrough)
3457 		panic(
3458     "resource_list_unreserve() should only be called for direct children");
3459 
3460 	rle = resource_list_find(rl, type, rid);
3461 
3462 	if (!rle)
3463 		panic("resource_list_unreserve: can't find resource");
3464 	if (!(rle->flags & RLE_RESERVED))
3465 		return (EINVAL);
3466 	if (rle->flags & RLE_ALLOCATED)
3467 		return (EBUSY);
3468 	rle->flags &= ~RLE_RESERVED;
3469 	return (resource_list_release(rl, bus, child, type, rid, rle->res));
3470 }
3471 
3472 /**
3473  * @brief Print a description of resources in a resource list
3474  *
3475  * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3476  * The name is printed if at least one resource of the given type is available.
3477  * The format is used to print resource start and end.
3478  *
3479  * @param rl		the resource list to print
3480  * @param name		the name of @p type, e.g. @c "memory"
3481  * @param type		type type of resource entry to print
3482  * @param format	printf(9) format string to print resource
3483  *			start and end values
3484  *
3485  * @returns		the number of characters printed
3486  */
3487 int
3488 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3489     const char *format)
3490 {
3491 	struct resource_list_entry *rle;
3492 	int printed, retval;
3493 
3494 	printed = 0;
3495 	retval = 0;
3496 	/* Yes, this is kinda cheating */
3497 	STAILQ_FOREACH(rle, rl, link) {
3498 		if (rle->type == type) {
3499 			if (printed == 0)
3500 				retval += printf(" %s ", name);
3501 			else
3502 				retval += printf(",");
3503 			printed++;
3504 			retval += printf(format, rle->start);
3505 			if (rle->count > 1) {
3506 				retval += printf("-");
3507 				retval += printf(format, rle->start +
3508 						 rle->count - 1);
3509 			}
3510 		}
3511 	}
3512 	return (retval);
3513 }
3514 
3515 /**
3516  * @brief Releases all the resources in a list.
3517  *
3518  * @param rl		The resource list to purge.
3519  *
3520  * @returns		nothing
3521  */
3522 void
3523 resource_list_purge(struct resource_list *rl)
3524 {
3525 	struct resource_list_entry *rle;
3526 
3527 	while ((rle = STAILQ_FIRST(rl)) != NULL) {
3528 		if (rle->res)
3529 			bus_release_resource(rman_get_device(rle->res),
3530 			    rle->type, rle->rid, rle->res);
3531 		STAILQ_REMOVE_HEAD(rl, link);
3532 		free(rle, M_BUS);
3533 	}
3534 }
3535 
3536 device_t
3537 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3538 {
3539 
3540 	return (device_add_child_ordered(dev, order, name, unit));
3541 }
3542 
3543 /**
3544  * @brief Helper function for implementing DEVICE_PROBE()
3545  *
3546  * This function can be used to help implement the DEVICE_PROBE() for
3547  * a bus (i.e. a device which has other devices attached to it). It
3548  * calls the DEVICE_IDENTIFY() method of each driver in the device's
3549  * devclass.
3550  */
3551 int
3552 bus_generic_probe(device_t dev)
3553 {
3554 	devclass_t dc = dev->devclass;
3555 	driverlink_t dl;
3556 
3557 	TAILQ_FOREACH(dl, &dc->drivers, link) {
3558 		/*
3559 		 * If this driver's pass is too high, then ignore it.
3560 		 * For most drivers in the default pass, this will
3561 		 * never be true.  For early-pass drivers they will
3562 		 * only call the identify routines of eligible drivers
3563 		 * when this routine is called.  Drivers for later
3564 		 * passes should have their identify routines called
3565 		 * on early-pass busses during BUS_NEW_PASS().
3566 		 */
3567 		if (dl->pass > bus_current_pass)
3568 			continue;
3569 		DEVICE_IDENTIFY(dl->driver, dev);
3570 	}
3571 
3572 	return (0);
3573 }
3574 
3575 /**
3576  * @brief Helper function for implementing DEVICE_ATTACH()
3577  *
3578  * This function can be used to help implement the DEVICE_ATTACH() for
3579  * a bus. It calls device_probe_and_attach() for each of the device's
3580  * children.
3581  */
3582 int
3583 bus_generic_attach(device_t dev)
3584 {
3585 	device_t child;
3586 
3587 	TAILQ_FOREACH(child, &dev->children, link) {
3588 		device_probe_and_attach(child);
3589 	}
3590 
3591 	return (0);
3592 }
3593 
3594 /**
3595  * @brief Helper function for implementing DEVICE_DETACH()
3596  *
3597  * This function can be used to help implement the DEVICE_DETACH() for
3598  * a bus. It calls device_detach() for each of the device's
3599  * children.
3600  */
3601 int
3602 bus_generic_detach(device_t dev)
3603 {
3604 	device_t child;
3605 	int error;
3606 
3607 	if (dev->state != DS_ATTACHED)
3608 		return (EBUSY);
3609 
3610 	TAILQ_FOREACH(child, &dev->children, link) {
3611 		if ((error = device_detach(child)) != 0)
3612 			return (error);
3613 	}
3614 
3615 	return (0);
3616 }
3617 
3618 /**
3619  * @brief Helper function for implementing DEVICE_SHUTDOWN()
3620  *
3621  * This function can be used to help implement the DEVICE_SHUTDOWN()
3622  * for a bus. It calls device_shutdown() for each of the device's
3623  * children.
3624  */
3625 int
3626 bus_generic_shutdown(device_t dev)
3627 {
3628 	device_t child;
3629 
3630 	TAILQ_FOREACH(child, &dev->children, link) {
3631 		device_shutdown(child);
3632 	}
3633 
3634 	return (0);
3635 }
3636 
3637 /**
3638  * @brief Default function for suspending a child device.
3639  *
3640  * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3641  */
3642 int
3643 bus_generic_suspend_child(device_t dev, device_t child)
3644 {
3645 	int	error;
3646 
3647 	error = DEVICE_SUSPEND(child);
3648 
3649 	if (error == 0)
3650 		dev->flags |= DF_SUSPENDED;
3651 
3652 	return (error);
3653 }
3654 
3655 /**
3656  * @brief Default function for resuming a child device.
3657  *
3658  * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3659  */
3660 int
3661 bus_generic_resume_child(device_t dev, device_t child)
3662 {
3663 
3664 	DEVICE_RESUME(child);
3665 	dev->flags &= ~DF_SUSPENDED;
3666 
3667 	return (0);
3668 }
3669 
3670 /**
3671  * @brief Helper function for implementing DEVICE_SUSPEND()
3672  *
3673  * This function can be used to help implement the DEVICE_SUSPEND()
3674  * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3675  * children. If any call to DEVICE_SUSPEND() fails, the suspend
3676  * operation is aborted and any devices which were suspended are
3677  * resumed immediately by calling their DEVICE_RESUME() methods.
3678  */
3679 int
3680 bus_generic_suspend(device_t dev)
3681 {
3682 	int		error;
3683 	device_t	child, child2;
3684 
3685 	TAILQ_FOREACH(child, &dev->children, link) {
3686 		error = BUS_SUSPEND_CHILD(dev, child);
3687 		if (error) {
3688 			for (child2 = TAILQ_FIRST(&dev->children);
3689 			     child2 && child2 != child;
3690 			     child2 = TAILQ_NEXT(child2, link))
3691 				BUS_RESUME_CHILD(dev, child2);
3692 			return (error);
3693 		}
3694 	}
3695 	return (0);
3696 }
3697 
3698 /**
3699  * @brief Helper function for implementing DEVICE_RESUME()
3700  *
3701  * This function can be used to help implement the DEVICE_RESUME() for
3702  * a bus. It calls DEVICE_RESUME() on each of the device's children.
3703  */
3704 int
3705 bus_generic_resume(device_t dev)
3706 {
3707 	device_t	child;
3708 
3709 	TAILQ_FOREACH(child, &dev->children, link) {
3710 		BUS_RESUME_CHILD(dev, child);
3711 		/* if resume fails, there's nothing we can usefully do... */
3712 	}
3713 	return (0);
3714 }
3715 
3716 /**
3717  * @brief Helper function for implementing BUS_PRINT_CHILD().
3718  *
3719  * This function prints the first part of the ascii representation of
3720  * @p child, including its name, unit and description (if any - see
3721  * device_set_desc()).
3722  *
3723  * @returns the number of characters printed
3724  */
3725 int
3726 bus_print_child_header(device_t dev, device_t child)
3727 {
3728 	int	retval = 0;
3729 
3730 	if (device_get_desc(child)) {
3731 		retval += device_printf(child, "<%s>", device_get_desc(child));
3732 	} else {
3733 		retval += printf("%s", device_get_nameunit(child));
3734 	}
3735 
3736 	return (retval);
3737 }
3738 
3739 /**
3740  * @brief Helper function for implementing BUS_PRINT_CHILD().
3741  *
3742  * This function prints the last part of the ascii representation of
3743  * @p child, which consists of the string @c " on " followed by the
3744  * name and unit of the @p dev.
3745  *
3746  * @returns the number of characters printed
3747  */
3748 int
3749 bus_print_child_footer(device_t dev, device_t child)
3750 {
3751 	return (printf(" on %s\n", device_get_nameunit(dev)));
3752 }
3753 
3754 /**
3755  * @brief Helper function for implementing BUS_PRINT_CHILD().
3756  *
3757  * This function simply calls bus_print_child_header() followed by
3758  * bus_print_child_footer().
3759  *
3760  * @returns the number of characters printed
3761  */
3762 int
3763 bus_generic_print_child(device_t dev, device_t child)
3764 {
3765 	int	retval = 0;
3766 
3767 	retval += bus_print_child_header(dev, child);
3768 	retval += bus_print_child_footer(dev, child);
3769 
3770 	return (retval);
3771 }
3772 
3773 /**
3774  * @brief Stub function for implementing BUS_READ_IVAR().
3775  *
3776  * @returns ENOENT
3777  */
3778 int
3779 bus_generic_read_ivar(device_t dev, device_t child, int index,
3780     uintptr_t * result)
3781 {
3782 	return (ENOENT);
3783 }
3784 
3785 /**
3786  * @brief Stub function for implementing BUS_WRITE_IVAR().
3787  *
3788  * @returns ENOENT
3789  */
3790 int
3791 bus_generic_write_ivar(device_t dev, device_t child, int index,
3792     uintptr_t value)
3793 {
3794 	return (ENOENT);
3795 }
3796 
3797 /**
3798  * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3799  *
3800  * @returns NULL
3801  */
3802 struct resource_list *
3803 bus_generic_get_resource_list(device_t dev, device_t child)
3804 {
3805 	return (NULL);
3806 }
3807 
3808 /**
3809  * @brief Helper function for implementing BUS_DRIVER_ADDED().
3810  *
3811  * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3812  * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3813  * and then calls device_probe_and_attach() for each unattached child.
3814  */
3815 void
3816 bus_generic_driver_added(device_t dev, driver_t *driver)
3817 {
3818 	device_t child;
3819 
3820 	DEVICE_IDENTIFY(driver, dev);
3821 	TAILQ_FOREACH(child, &dev->children, link) {
3822 		if (child->state == DS_NOTPRESENT ||
3823 		    (child->flags & DF_REBID))
3824 			device_probe_and_attach(child);
3825 	}
3826 }
3827 
3828 /**
3829  * @brief Helper function for implementing BUS_NEW_PASS().
3830  *
3831  * This implementing of BUS_NEW_PASS() first calls the identify
3832  * routines for any drivers that probe at the current pass.  Then it
3833  * walks the list of devices for this bus.  If a device is already
3834  * attached, then it calls BUS_NEW_PASS() on that device.  If the
3835  * device is not already attached, it attempts to attach a driver to
3836  * it.
3837  */
3838 void
3839 bus_generic_new_pass(device_t dev)
3840 {
3841 	driverlink_t dl;
3842 	devclass_t dc;
3843 	device_t child;
3844 
3845 	dc = dev->devclass;
3846 	TAILQ_FOREACH(dl, &dc->drivers, link) {
3847 		if (dl->pass == bus_current_pass)
3848 			DEVICE_IDENTIFY(dl->driver, dev);
3849 	}
3850 	TAILQ_FOREACH(child, &dev->children, link) {
3851 		if (child->state >= DS_ATTACHED)
3852 			BUS_NEW_PASS(child);
3853 		else if (child->state == DS_NOTPRESENT)
3854 			device_probe_and_attach(child);
3855 	}
3856 }
3857 
3858 /**
3859  * @brief Helper function for implementing BUS_SETUP_INTR().
3860  *
3861  * This simple implementation of BUS_SETUP_INTR() simply calls the
3862  * BUS_SETUP_INTR() method of the parent of @p dev.
3863  */
3864 int
3865 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3866     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3867     void **cookiep)
3868 {
3869 	/* Propagate up the bus hierarchy until someone handles it. */
3870 	if (dev->parent)
3871 		return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3872 		    filter, intr, arg, cookiep));
3873 	return (EINVAL);
3874 }
3875 
3876 /**
3877  * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3878  *
3879  * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3880  * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3881  */
3882 int
3883 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3884     void *cookie)
3885 {
3886 	/* Propagate up the bus hierarchy until someone handles it. */
3887 	if (dev->parent)
3888 		return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3889 	return (EINVAL);
3890 }
3891 
3892 /**
3893  * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3894  *
3895  * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3896  * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3897  */
3898 int
3899 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3900     struct resource *r, u_long start, u_long end)
3901 {
3902 	/* Propagate up the bus hierarchy until someone handles it. */
3903 	if (dev->parent)
3904 		return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3905 		    end));
3906 	return (EINVAL);
3907 }
3908 
3909 /**
3910  * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3911  *
3912  * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3913  * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3914  */
3915 struct resource *
3916 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3917     u_long start, u_long end, u_long count, u_int flags)
3918 {
3919 	/* Propagate up the bus hierarchy until someone handles it. */
3920 	if (dev->parent)
3921 		return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3922 		    start, end, count, flags));
3923 	return (NULL);
3924 }
3925 
3926 /**
3927  * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3928  *
3929  * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3930  * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3931  */
3932 int
3933 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3934     struct resource *r)
3935 {
3936 	/* Propagate up the bus hierarchy until someone handles it. */
3937 	if (dev->parent)
3938 		return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3939 		    r));
3940 	return (EINVAL);
3941 }
3942 
3943 /**
3944  * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3945  *
3946  * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3947  * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3948  */
3949 int
3950 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3951     struct resource *r)
3952 {
3953 	/* Propagate up the bus hierarchy until someone handles it. */
3954 	if (dev->parent)
3955 		return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3956 		    r));
3957 	return (EINVAL);
3958 }
3959 
3960 /**
3961  * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3962  *
3963  * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3964  * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3965  */
3966 int
3967 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3968     int rid, struct resource *r)
3969 {
3970 	/* Propagate up the bus hierarchy until someone handles it. */
3971 	if (dev->parent)
3972 		return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3973 		    r));
3974 	return (EINVAL);
3975 }
3976 
3977 /**
3978  * @brief Helper function for implementing BUS_BIND_INTR().
3979  *
3980  * This simple implementation of BUS_BIND_INTR() simply calls the
3981  * BUS_BIND_INTR() method of the parent of @p dev.
3982  */
3983 int
3984 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3985     int cpu)
3986 {
3987 
3988 	/* Propagate up the bus hierarchy until someone handles it. */
3989 	if (dev->parent)
3990 		return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3991 	return (EINVAL);
3992 }
3993 
3994 /**
3995  * @brief Helper function for implementing BUS_CONFIG_INTR().
3996  *
3997  * This simple implementation of BUS_CONFIG_INTR() simply calls the
3998  * BUS_CONFIG_INTR() method of the parent of @p dev.
3999  */
4000 int
4001 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
4002     enum intr_polarity pol)
4003 {
4004 
4005 	/* Propagate up the bus hierarchy until someone handles it. */
4006 	if (dev->parent)
4007 		return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
4008 	return (EINVAL);
4009 }
4010 
4011 /**
4012  * @brief Helper function for implementing BUS_DESCRIBE_INTR().
4013  *
4014  * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
4015  * BUS_DESCRIBE_INTR() method of the parent of @p dev.
4016  */
4017 int
4018 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
4019     void *cookie, const char *descr)
4020 {
4021 
4022 	/* Propagate up the bus hierarchy until someone handles it. */
4023 	if (dev->parent)
4024 		return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
4025 		    descr));
4026 	return (EINVAL);
4027 }
4028 
4029 /**
4030  * @brief Helper function for implementing BUS_GET_DMA_TAG().
4031  *
4032  * This simple implementation of BUS_GET_DMA_TAG() simply calls the
4033  * BUS_GET_DMA_TAG() method of the parent of @p dev.
4034  */
4035 bus_dma_tag_t
4036 bus_generic_get_dma_tag(device_t dev, device_t child)
4037 {
4038 
4039 	/* Propagate up the bus hierarchy until someone handles it. */
4040 	if (dev->parent != NULL)
4041 		return (BUS_GET_DMA_TAG(dev->parent, child));
4042 	return (NULL);
4043 }
4044 
4045 /**
4046  * @brief Helper function for implementing BUS_GET_RESOURCE().
4047  *
4048  * This implementation of BUS_GET_RESOURCE() uses the
4049  * resource_list_find() function to do most of the work. It calls
4050  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4051  * search.
4052  */
4053 int
4054 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4055     u_long *startp, u_long *countp)
4056 {
4057 	struct resource_list *		rl = NULL;
4058 	struct resource_list_entry *	rle = NULL;
4059 
4060 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4061 	if (!rl)
4062 		return (EINVAL);
4063 
4064 	rle = resource_list_find(rl, type, rid);
4065 	if (!rle)
4066 		return (ENOENT);
4067 
4068 	if (startp)
4069 		*startp = rle->start;
4070 	if (countp)
4071 		*countp = rle->count;
4072 
4073 	return (0);
4074 }
4075 
4076 /**
4077  * @brief Helper function for implementing BUS_SET_RESOURCE().
4078  *
4079  * This implementation of BUS_SET_RESOURCE() uses the
4080  * resource_list_add() function to do most of the work. It calls
4081  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4082  * edit.
4083  */
4084 int
4085 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4086     u_long start, u_long count)
4087 {
4088 	struct resource_list *		rl = NULL;
4089 
4090 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4091 	if (!rl)
4092 		return (EINVAL);
4093 
4094 	resource_list_add(rl, type, rid, start, (start + count - 1), count);
4095 
4096 	return (0);
4097 }
4098 
4099 /**
4100  * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4101  *
4102  * This implementation of BUS_DELETE_RESOURCE() uses the
4103  * resource_list_delete() function to do most of the work. It calls
4104  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4105  * edit.
4106  */
4107 void
4108 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4109 {
4110 	struct resource_list *		rl = NULL;
4111 
4112 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4113 	if (!rl)
4114 		return;
4115 
4116 	resource_list_delete(rl, type, rid);
4117 
4118 	return;
4119 }
4120 
4121 /**
4122  * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4123  *
4124  * This implementation of BUS_RELEASE_RESOURCE() uses the
4125  * resource_list_release() function to do most of the work. It calls
4126  * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4127  */
4128 int
4129 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4130     int rid, struct resource *r)
4131 {
4132 	struct resource_list *		rl = NULL;
4133 
4134 	if (device_get_parent(child) != dev)
4135 		return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4136 		    type, rid, r));
4137 
4138 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4139 	if (!rl)
4140 		return (EINVAL);
4141 
4142 	return (resource_list_release(rl, dev, child, type, rid, r));
4143 }
4144 
4145 /**
4146  * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4147  *
4148  * This implementation of BUS_ALLOC_RESOURCE() uses the
4149  * resource_list_alloc() function to do most of the work. It calls
4150  * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4151  */
4152 struct resource *
4153 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4154     int *rid, u_long start, u_long end, u_long count, u_int flags)
4155 {
4156 	struct resource_list *		rl = NULL;
4157 
4158 	if (device_get_parent(child) != dev)
4159 		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4160 		    type, rid, start, end, count, flags));
4161 
4162 	rl = BUS_GET_RESOURCE_LIST(dev, child);
4163 	if (!rl)
4164 		return (NULL);
4165 
4166 	return (resource_list_alloc(rl, dev, child, type, rid,
4167 	    start, end, count, flags));
4168 }
4169 
4170 /**
4171  * @brief Helper function for implementing BUS_CHILD_PRESENT().
4172  *
4173  * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4174  * BUS_CHILD_PRESENT() method of the parent of @p dev.
4175  */
4176 int
4177 bus_generic_child_present(device_t dev, device_t child)
4178 {
4179 	return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4180 }
4181 
4182 /*
4183  * Some convenience functions to make it easier for drivers to use the
4184  * resource-management functions.  All these really do is hide the
4185  * indirection through the parent's method table, making for slightly
4186  * less-wordy code.  In the future, it might make sense for this code
4187  * to maintain some sort of a list of resources allocated by each device.
4188  */
4189 
4190 int
4191 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4192     struct resource **res)
4193 {
4194 	int i;
4195 
4196 	for (i = 0; rs[i].type != -1; i++)
4197 		res[i] = NULL;
4198 	for (i = 0; rs[i].type != -1; i++) {
4199 		res[i] = bus_alloc_resource_any(dev,
4200 		    rs[i].type, &rs[i].rid, rs[i].flags);
4201 		if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4202 			bus_release_resources(dev, rs, res);
4203 			return (ENXIO);
4204 		}
4205 	}
4206 	return (0);
4207 }
4208 
4209 void
4210 bus_release_resources(device_t dev, const struct resource_spec *rs,
4211     struct resource **res)
4212 {
4213 	int i;
4214 
4215 	for (i = 0; rs[i].type != -1; i++)
4216 		if (res[i] != NULL) {
4217 			bus_release_resource(
4218 			    dev, rs[i].type, rs[i].rid, res[i]);
4219 			res[i] = NULL;
4220 		}
4221 }
4222 
4223 /**
4224  * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4225  *
4226  * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4227  * parent of @p dev.
4228  */
4229 struct resource *
4230 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
4231     u_long count, u_int flags)
4232 {
4233 	if (dev->parent == NULL)
4234 		return (NULL);
4235 	return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4236 	    count, flags));
4237 }
4238 
4239 /**
4240  * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4241  *
4242  * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4243  * parent of @p dev.
4244  */
4245 int
4246 bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start,
4247     u_long end)
4248 {
4249 	if (dev->parent == NULL)
4250 		return (EINVAL);
4251 	return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4252 }
4253 
4254 /**
4255  * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4256  *
4257  * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4258  * parent of @p dev.
4259  */
4260 int
4261 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4262 {
4263 	if (dev->parent == NULL)
4264 		return (EINVAL);
4265 	return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4266 }
4267 
4268 /**
4269  * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4270  *
4271  * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4272  * parent of @p dev.
4273  */
4274 int
4275 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4276 {
4277 	if (dev->parent == NULL)
4278 		return (EINVAL);
4279 	return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4280 }
4281 
4282 /**
4283  * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4284  *
4285  * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4286  * parent of @p dev.
4287  */
4288 int
4289 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4290 {
4291 	if (dev->parent == NULL)
4292 		return (EINVAL);
4293 	return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
4294 }
4295 
4296 /**
4297  * @brief Wrapper function for BUS_SETUP_INTR().
4298  *
4299  * This function simply calls the BUS_SETUP_INTR() method of the
4300  * parent of @p dev.
4301  */
4302 int
4303 bus_setup_intr(device_t dev, struct resource *r, int flags,
4304     driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4305 {
4306 	int error;
4307 
4308 	if (dev->parent == NULL)
4309 		return (EINVAL);
4310 	error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4311 	    arg, cookiep);
4312 	if (error != 0)
4313 		return (error);
4314 	if (handler != NULL && !(flags & INTR_MPSAFE))
4315 		device_printf(dev, "[GIANT-LOCKED]\n");
4316 	return (0);
4317 }
4318 
4319 /**
4320  * @brief Wrapper function for BUS_TEARDOWN_INTR().
4321  *
4322  * This function simply calls the BUS_TEARDOWN_INTR() method of the
4323  * parent of @p dev.
4324  */
4325 int
4326 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4327 {
4328 	if (dev->parent == NULL)
4329 		return (EINVAL);
4330 	return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4331 }
4332 
4333 /**
4334  * @brief Wrapper function for BUS_BIND_INTR().
4335  *
4336  * This function simply calls the BUS_BIND_INTR() method of the
4337  * parent of @p dev.
4338  */
4339 int
4340 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4341 {
4342 	if (dev->parent == NULL)
4343 		return (EINVAL);
4344 	return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4345 }
4346 
4347 /**
4348  * @brief Wrapper function for BUS_DESCRIBE_INTR().
4349  *
4350  * This function first formats the requested description into a
4351  * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4352  * the parent of @p dev.
4353  */
4354 int
4355 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4356     const char *fmt, ...)
4357 {
4358 	va_list ap;
4359 	char descr[MAXCOMLEN + 1];
4360 
4361 	if (dev->parent == NULL)
4362 		return (EINVAL);
4363 	va_start(ap, fmt);
4364 	vsnprintf(descr, sizeof(descr), fmt, ap);
4365 	va_end(ap);
4366 	return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4367 }
4368 
4369 /**
4370  * @brief Wrapper function for BUS_SET_RESOURCE().
4371  *
4372  * This function simply calls the BUS_SET_RESOURCE() method of the
4373  * parent of @p dev.
4374  */
4375 int
4376 bus_set_resource(device_t dev, int type, int rid,
4377     u_long start, u_long count)
4378 {
4379 	return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4380 	    start, count));
4381 }
4382 
4383 /**
4384  * @brief Wrapper function for BUS_GET_RESOURCE().
4385  *
4386  * This function simply calls the BUS_GET_RESOURCE() method of the
4387  * parent of @p dev.
4388  */
4389 int
4390 bus_get_resource(device_t dev, int type, int rid,
4391     u_long *startp, u_long *countp)
4392 {
4393 	return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4394 	    startp, countp));
4395 }
4396 
4397 /**
4398  * @brief Wrapper function for BUS_GET_RESOURCE().
4399  *
4400  * This function simply calls the BUS_GET_RESOURCE() method of the
4401  * parent of @p dev and returns the start value.
4402  */
4403 u_long
4404 bus_get_resource_start(device_t dev, int type, int rid)
4405 {
4406 	u_long start, count;
4407 	int error;
4408 
4409 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4410 	    &start, &count);
4411 	if (error)
4412 		return (0);
4413 	return (start);
4414 }
4415 
4416 /**
4417  * @brief Wrapper function for BUS_GET_RESOURCE().
4418  *
4419  * This function simply calls the BUS_GET_RESOURCE() method of the
4420  * parent of @p dev and returns the count value.
4421  */
4422 u_long
4423 bus_get_resource_count(device_t dev, int type, int rid)
4424 {
4425 	u_long start, count;
4426 	int error;
4427 
4428 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4429 	    &start, &count);
4430 	if (error)
4431 		return (0);
4432 	return (count);
4433 }
4434 
4435 /**
4436  * @brief Wrapper function for BUS_DELETE_RESOURCE().
4437  *
4438  * This function simply calls the BUS_DELETE_RESOURCE() method of the
4439  * parent of @p dev.
4440  */
4441 void
4442 bus_delete_resource(device_t dev, int type, int rid)
4443 {
4444 	BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4445 }
4446 
4447 /**
4448  * @brief Wrapper function for BUS_CHILD_PRESENT().
4449  *
4450  * This function simply calls the BUS_CHILD_PRESENT() method of the
4451  * parent of @p dev.
4452  */
4453 int
4454 bus_child_present(device_t child)
4455 {
4456 	return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4457 }
4458 
4459 /**
4460  * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4461  *
4462  * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4463  * parent of @p dev.
4464  */
4465 int
4466 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4467 {
4468 	device_t parent;
4469 
4470 	parent = device_get_parent(child);
4471 	if (parent == NULL) {
4472 		*buf = '\0';
4473 		return (0);
4474 	}
4475 	return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4476 }
4477 
4478 /**
4479  * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4480  *
4481  * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4482  * parent of @p dev.
4483  */
4484 int
4485 bus_child_location_str(device_t child, char *buf, size_t buflen)
4486 {
4487 	device_t parent;
4488 
4489 	parent = device_get_parent(child);
4490 	if (parent == NULL) {
4491 		*buf = '\0';
4492 		return (0);
4493 	}
4494 	return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4495 }
4496 
4497 /**
4498  * @brief Wrapper function for BUS_GET_DMA_TAG().
4499  *
4500  * This function simply calls the BUS_GET_DMA_TAG() method of the
4501  * parent of @p dev.
4502  */
4503 bus_dma_tag_t
4504 bus_get_dma_tag(device_t dev)
4505 {
4506 	device_t parent;
4507 
4508 	parent = device_get_parent(dev);
4509 	if (parent == NULL)
4510 		return (NULL);
4511 	return (BUS_GET_DMA_TAG(parent, dev));
4512 }
4513 
4514 /* Resume all devices and then notify userland that we're up again. */
4515 static int
4516 root_resume(device_t dev)
4517 {
4518 	int error;
4519 
4520 	error = bus_generic_resume(dev);
4521 	if (error == 0)
4522 		devctl_notify("kern", "power", "resume", NULL);
4523 	return (error);
4524 }
4525 
4526 static int
4527 root_print_child(device_t dev, device_t child)
4528 {
4529 	int	retval = 0;
4530 
4531 	retval += bus_print_child_header(dev, child);
4532 	retval += printf("\n");
4533 
4534 	return (retval);
4535 }
4536 
4537 static int
4538 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4539     driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4540 {
4541 	/*
4542 	 * If an interrupt mapping gets to here something bad has happened.
4543 	 */
4544 	panic("root_setup_intr");
4545 }
4546 
4547 /*
4548  * If we get here, assume that the device is permanant and really is
4549  * present in the system.  Removable bus drivers are expected to intercept
4550  * this call long before it gets here.  We return -1 so that drivers that
4551  * really care can check vs -1 or some ERRNO returned higher in the food
4552  * chain.
4553  */
4554 static int
4555 root_child_present(device_t dev, device_t child)
4556 {
4557 	return (-1);
4558 }
4559 
4560 static kobj_method_t root_methods[] = {
4561 	/* Device interface */
4562 	KOBJMETHOD(device_shutdown,	bus_generic_shutdown),
4563 	KOBJMETHOD(device_suspend,	bus_generic_suspend),
4564 	KOBJMETHOD(device_resume,	root_resume),
4565 
4566 	/* Bus interface */
4567 	KOBJMETHOD(bus_print_child,	root_print_child),
4568 	KOBJMETHOD(bus_read_ivar,	bus_generic_read_ivar),
4569 	KOBJMETHOD(bus_write_ivar,	bus_generic_write_ivar),
4570 	KOBJMETHOD(bus_setup_intr,	root_setup_intr),
4571 	KOBJMETHOD(bus_child_present,	root_child_present),
4572 
4573 	KOBJMETHOD_END
4574 };
4575 
4576 static driver_t root_driver = {
4577 	"root",
4578 	root_methods,
4579 	1,			/* no softc */
4580 };
4581 
4582 device_t	root_bus;
4583 devclass_t	root_devclass;
4584 
4585 static int
4586 root_bus_module_handler(module_t mod, int what, void* arg)
4587 {
4588 	switch (what) {
4589 	case MOD_LOAD:
4590 		TAILQ_INIT(&bus_data_devices);
4591 		kobj_class_compile((kobj_class_t) &root_driver);
4592 		root_bus = make_device(NULL, "root", 0);
4593 		root_bus->desc = "System root bus";
4594 		kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4595 		root_bus->driver = &root_driver;
4596 		root_bus->state = DS_ATTACHED;
4597 		root_devclass = devclass_find_internal("root", NULL, FALSE);
4598 		devinit();
4599 		return (0);
4600 
4601 	case MOD_SHUTDOWN:
4602 		device_shutdown(root_bus);
4603 		return (0);
4604 	default:
4605 		return (EOPNOTSUPP);
4606 	}
4607 
4608 	return (0);
4609 }
4610 
4611 static moduledata_t root_bus_mod = {
4612 	"rootbus",
4613 	root_bus_module_handler,
4614 	NULL
4615 };
4616 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4617 
4618 /**
4619  * @brief Automatically configure devices
4620  *
4621  * This function begins the autoconfiguration process by calling
4622  * device_probe_and_attach() for each child of the @c root0 device.
4623  */
4624 void
4625 root_bus_configure(void)
4626 {
4627 
4628 	PDEBUG(("."));
4629 
4630 	/* Eventually this will be split up, but this is sufficient for now. */
4631 	bus_set_pass(BUS_PASS_DEFAULT);
4632 }
4633 
4634 /**
4635  * @brief Module handler for registering device drivers
4636  *
4637  * This module handler is used to automatically register device
4638  * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4639  * devclass_add_driver() for the driver described by the
4640  * driver_module_data structure pointed to by @p arg
4641  */
4642 int
4643 driver_module_handler(module_t mod, int what, void *arg)
4644 {
4645 	struct driver_module_data *dmd;
4646 	devclass_t bus_devclass;
4647 	kobj_class_t driver;
4648 	int error, pass;
4649 
4650 	dmd = (struct driver_module_data *)arg;
4651 	bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4652 	error = 0;
4653 
4654 	switch (what) {
4655 	case MOD_LOAD:
4656 		if (dmd->dmd_chainevh)
4657 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4658 
4659 		pass = dmd->dmd_pass;
4660 		driver = dmd->dmd_driver;
4661 		PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4662 		    DRIVERNAME(driver), dmd->dmd_busname, pass));
4663 		error = devclass_add_driver(bus_devclass, driver, pass,
4664 		    dmd->dmd_devclass);
4665 		break;
4666 
4667 	case MOD_UNLOAD:
4668 		PDEBUG(("Unloading module: driver %s from bus %s",
4669 		    DRIVERNAME(dmd->dmd_driver),
4670 		    dmd->dmd_busname));
4671 		error = devclass_delete_driver(bus_devclass,
4672 		    dmd->dmd_driver);
4673 
4674 		if (!error && dmd->dmd_chainevh)
4675 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4676 		break;
4677 	case MOD_QUIESCE:
4678 		PDEBUG(("Quiesce module: driver %s from bus %s",
4679 		    DRIVERNAME(dmd->dmd_driver),
4680 		    dmd->dmd_busname));
4681 		error = devclass_quiesce_driver(bus_devclass,
4682 		    dmd->dmd_driver);
4683 
4684 		if (!error && dmd->dmd_chainevh)
4685 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4686 		break;
4687 	default:
4688 		error = EOPNOTSUPP;
4689 		break;
4690 	}
4691 
4692 	return (error);
4693 }
4694 
4695 /**
4696  * @brief Enumerate all hinted devices for this bus.
4697  *
4698  * Walks through the hints for this bus and calls the bus_hinted_child
4699  * routine for each one it fines.  It searches first for the specific
4700  * bus that's being probed for hinted children (eg isa0), and then for
4701  * generic children (eg isa).
4702  *
4703  * @param	dev	bus device to enumerate
4704  */
4705 void
4706 bus_enumerate_hinted_children(device_t bus)
4707 {
4708 	int i;
4709 	const char *dname, *busname;
4710 	int dunit;
4711 
4712 	/*
4713 	 * enumerate all devices on the specific bus
4714 	 */
4715 	busname = device_get_nameunit(bus);
4716 	i = 0;
4717 	while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4718 		BUS_HINTED_CHILD(bus, dname, dunit);
4719 
4720 	/*
4721 	 * and all the generic ones.
4722 	 */
4723 	busname = device_get_name(bus);
4724 	i = 0;
4725 	while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4726 		BUS_HINTED_CHILD(bus, dname, dunit);
4727 }
4728 
4729 #ifdef BUS_DEBUG
4730 
4731 /* the _short versions avoid iteration by not calling anything that prints
4732  * more than oneliners. I love oneliners.
4733  */
4734 
4735 static void
4736 print_device_short(device_t dev, int indent)
4737 {
4738 	if (!dev)
4739 		return;
4740 
4741 	indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4742 	    dev->unit, dev->desc,
4743 	    (dev->parent? "":"no "),
4744 	    (TAILQ_EMPTY(&dev->children)? "no ":""),
4745 	    (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4746 	    (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4747 	    (dev->flags&DF_WILDCARD? "wildcard,":""),
4748 	    (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4749 	    (dev->flags&DF_REBID? "rebiddable,":""),
4750 	    (dev->ivars? "":"no "),
4751 	    (dev->softc? "":"no "),
4752 	    dev->busy));
4753 }
4754 
4755 static void
4756 print_device(device_t dev, int indent)
4757 {
4758 	if (!dev)
4759 		return;
4760 
4761 	print_device_short(dev, indent);
4762 
4763 	indentprintf(("Parent:\n"));
4764 	print_device_short(dev->parent, indent+1);
4765 	indentprintf(("Driver:\n"));
4766 	print_driver_short(dev->driver, indent+1);
4767 	indentprintf(("Devclass:\n"));
4768 	print_devclass_short(dev->devclass, indent+1);
4769 }
4770 
4771 void
4772 print_device_tree_short(device_t dev, int indent)
4773 /* print the device and all its children (indented) */
4774 {
4775 	device_t child;
4776 
4777 	if (!dev)
4778 		return;
4779 
4780 	print_device_short(dev, indent);
4781 
4782 	TAILQ_FOREACH(child, &dev->children, link) {
4783 		print_device_tree_short(child, indent+1);
4784 	}
4785 }
4786 
4787 void
4788 print_device_tree(device_t dev, int indent)
4789 /* print the device and all its children (indented) */
4790 {
4791 	device_t child;
4792 
4793 	if (!dev)
4794 		return;
4795 
4796 	print_device(dev, indent);
4797 
4798 	TAILQ_FOREACH(child, &dev->children, link) {
4799 		print_device_tree(child, indent+1);
4800 	}
4801 }
4802 
4803 static void
4804 print_driver_short(driver_t *driver, int indent)
4805 {
4806 	if (!driver)
4807 		return;
4808 
4809 	indentprintf(("driver %s: softc size = %zd\n",
4810 	    driver->name, driver->size));
4811 }
4812 
4813 static void
4814 print_driver(driver_t *driver, int indent)
4815 {
4816 	if (!driver)
4817 		return;
4818 
4819 	print_driver_short(driver, indent);
4820 }
4821 
4822 static void
4823 print_driver_list(driver_list_t drivers, int indent)
4824 {
4825 	driverlink_t driver;
4826 
4827 	TAILQ_FOREACH(driver, &drivers, link) {
4828 		print_driver(driver->driver, indent);
4829 	}
4830 }
4831 
4832 static void
4833 print_devclass_short(devclass_t dc, int indent)
4834 {
4835 	if ( !dc )
4836 		return;
4837 
4838 	indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4839 }
4840 
4841 static void
4842 print_devclass(devclass_t dc, int indent)
4843 {
4844 	int i;
4845 
4846 	if ( !dc )
4847 		return;
4848 
4849 	print_devclass_short(dc, indent);
4850 	indentprintf(("Drivers:\n"));
4851 	print_driver_list(dc->drivers, indent+1);
4852 
4853 	indentprintf(("Devices:\n"));
4854 	for (i = 0; i < dc->maxunit; i++)
4855 		if (dc->devices[i])
4856 			print_device(dc->devices[i], indent+1);
4857 }
4858 
4859 void
4860 print_devclass_list_short(void)
4861 {
4862 	devclass_t dc;
4863 
4864 	printf("Short listing of devclasses, drivers & devices:\n");
4865 	TAILQ_FOREACH(dc, &devclasses, link) {
4866 		print_devclass_short(dc, 0);
4867 	}
4868 }
4869 
4870 void
4871 print_devclass_list(void)
4872 {
4873 	devclass_t dc;
4874 
4875 	printf("Full listing of devclasses, drivers & devices:\n");
4876 	TAILQ_FOREACH(dc, &devclasses, link) {
4877 		print_devclass(dc, 0);
4878 	}
4879 }
4880 
4881 #endif
4882 
4883 /*
4884  * User-space access to the device tree.
4885  *
4886  * We implement a small set of nodes:
4887  *
4888  * hw.bus			Single integer read method to obtain the
4889  *				current generation count.
4890  * hw.bus.devices		Reads the entire device tree in flat space.
4891  * hw.bus.rman			Resource manager interface
4892  *
4893  * We might like to add the ability to scan devclasses and/or drivers to
4894  * determine what else is currently loaded/available.
4895  */
4896 
4897 static int
4898 sysctl_bus(SYSCTL_HANDLER_ARGS)
4899 {
4900 	struct u_businfo	ubus;
4901 
4902 	ubus.ub_version = BUS_USER_VERSION;
4903 	ubus.ub_generation = bus_data_generation;
4904 
4905 	return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4906 }
4907 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4908     "bus-related data");
4909 
4910 static int
4911 sysctl_devices(SYSCTL_HANDLER_ARGS)
4912 {
4913 	int			*name = (int *)arg1;
4914 	u_int			namelen = arg2;
4915 	int			index;
4916 	struct device		*dev;
4917 	struct u_device		udev;	/* XXX this is a bit big */
4918 	int			error;
4919 
4920 	if (namelen != 2)
4921 		return (EINVAL);
4922 
4923 	if (bus_data_generation_check(name[0]))
4924 		return (EINVAL);
4925 
4926 	index = name[1];
4927 
4928 	/*
4929 	 * Scan the list of devices, looking for the requested index.
4930 	 */
4931 	TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4932 		if (index-- == 0)
4933 			break;
4934 	}
4935 	if (dev == NULL)
4936 		return (ENOENT);
4937 
4938 	/*
4939 	 * Populate the return array.
4940 	 */
4941 	bzero(&udev, sizeof(udev));
4942 	udev.dv_handle = (uintptr_t)dev;
4943 	udev.dv_parent = (uintptr_t)dev->parent;
4944 	if (dev->nameunit != NULL)
4945 		strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4946 	if (dev->desc != NULL)
4947 		strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4948 	if (dev->driver != NULL && dev->driver->name != NULL)
4949 		strlcpy(udev.dv_drivername, dev->driver->name,
4950 		    sizeof(udev.dv_drivername));
4951 	bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4952 	bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4953 	udev.dv_devflags = dev->devflags;
4954 	udev.dv_flags = dev->flags;
4955 	udev.dv_state = dev->state;
4956 	error = SYSCTL_OUT(req, &udev, sizeof(udev));
4957 	return (error);
4958 }
4959 
4960 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4961     "system device tree");
4962 
4963 int
4964 bus_data_generation_check(int generation)
4965 {
4966 	if (generation != bus_data_generation)
4967 		return (1);
4968 
4969 	/* XXX generate optimised lists here? */
4970 	return (0);
4971 }
4972 
4973 void
4974 bus_data_generation_update(void)
4975 {
4976 	bus_data_generation++;
4977 }
4978 
4979 int
4980 bus_free_resource(device_t dev, int type, struct resource *r)
4981 {
4982 	if (r == NULL)
4983 		return (0);
4984 	return (bus_release_resource(dev, type, rman_get_rid(r), r));
4985 }
4986