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