xref: /dragonfly/sys/kern/subr_bus.c (revision c209d55a)
1 /*
2  * Copyright (c) 1997,1998 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/kern/subr_bus.c,v 1.54.2.9 2002/10/10 15:13:32 jhb Exp $
27  */
28 
29 #include "opt_bus.h"
30 
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/kobj.h>
37 #include <sys/bus_private.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h>
42 #include <sys/device.h>
43 #include <sys/lock.h>
44 #include <sys/conf.h>
45 #include <sys/uio.h>
46 #include <sys/filio.h>
47 #include <sys/event.h>
48 #include <sys/signalvar.h>
49 #include <sys/machintr.h>
50 #include <sys/vnode.h>
51 
52 #include <machine/stdarg.h>	/* for device_printf() */
53 
54 #include <sys/thread2.h>
55 
56 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
57 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
58 
59 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
60 
61 #ifdef BUS_DEBUG
62 #define PDEBUG(a)	(kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
63 #define DEVICENAME(d)	((d)? device_get_name(d): "no device")
64 #define DRIVERNAME(d)	((d)? d->name : "no driver")
65 #define DEVCLANAME(d)	((d)? d->name : "no devclass")
66 
67 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
68  * prevent syslog from deleting initial spaces
69  */
70 #define indentprintf(p)	do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf("  "); kprintf p ; } while(0)
71 
72 static void	print_device_short(device_t dev, int indent);
73 static void	print_device(device_t dev, int indent);
74 void		print_device_tree_short(device_t dev, int indent);
75 void		print_device_tree(device_t dev, int indent);
76 static void	print_driver_short(driver_t *driver, int indent);
77 static void	print_driver(driver_t *driver, int indent);
78 static void	print_driver_list(driver_list_t drivers, int indent);
79 static void	print_devclass_short(devclass_t dc, int indent);
80 static void	print_devclass(devclass_t dc, int indent);
81 void		print_devclass_list_short(void);
82 void		print_devclass_list(void);
83 
84 #else
85 /* Make the compiler ignore the function calls */
86 #define PDEBUG(a)			/* nop */
87 #define DEVICENAME(d)			/* nop */
88 #define DRIVERNAME(d)			/* nop */
89 #define DEVCLANAME(d)			/* nop */
90 
91 #define print_device_short(d,i)		/* nop */
92 #define print_device(d,i)		/* nop */
93 #define print_device_tree_short(d,i)	/* nop */
94 #define print_device_tree(d,i)		/* nop */
95 #define print_driver_short(d,i)		/* nop */
96 #define print_driver(d,i)		/* nop */
97 #define print_driver_list(d,i)		/* nop */
98 #define print_devclass_short(d,i)	/* nop */
99 #define print_devclass(d,i)		/* nop */
100 #define print_devclass_list_short()	/* nop */
101 #define print_devclass_list()		/* nop */
102 #endif
103 
104 /*
105  * dev sysctl tree
106  */
107 
108 enum {
109 	DEVCLASS_SYSCTL_PARENT,
110 };
111 
112 static int
113 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
114 {
115 	devclass_t dc = (devclass_t)arg1;
116 	const char *value;
117 
118 	switch (arg2) {
119 	case DEVCLASS_SYSCTL_PARENT:
120 		value = dc->parent ? dc->parent->name : "";
121 		break;
122 	default:
123 		return (EINVAL);
124 	}
125 	return (SYSCTL_OUT(req, value, strlen(value)));
126 }
127 
128 static void
129 devclass_sysctl_init(devclass_t dc)
130 {
131 
132 	if (dc->sysctl_tree != NULL)
133 		return;
134 	sysctl_ctx_init(&dc->sysctl_ctx);
135 	dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
136 	    SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
137 	    CTLFLAG_RD, NULL, "");
138 	SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
139 	    OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
140 	    dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
141 	    "parent class");
142 }
143 
144 enum {
145 	DEVICE_SYSCTL_DESC,
146 	DEVICE_SYSCTL_DRIVER,
147 	DEVICE_SYSCTL_LOCATION,
148 	DEVICE_SYSCTL_PNPINFO,
149 	DEVICE_SYSCTL_PARENT,
150 };
151 
152 static int
153 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
154 {
155 	device_t dev = (device_t)arg1;
156 	const char *value;
157 	char *buf;
158 	int error;
159 
160 	buf = NULL;
161 	switch (arg2) {
162 	case DEVICE_SYSCTL_DESC:
163 		value = dev->desc ? dev->desc : "";
164 		break;
165 	case DEVICE_SYSCTL_DRIVER:
166 		value = dev->driver ? dev->driver->name : "";
167 		break;
168 	case DEVICE_SYSCTL_LOCATION:
169 		value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
170 		bus_child_location_str(dev, buf, 1024);
171 		break;
172 	case DEVICE_SYSCTL_PNPINFO:
173 		value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
174 		bus_child_pnpinfo_str(dev, buf, 1024);
175 		break;
176 	case DEVICE_SYSCTL_PARENT:
177 		value = dev->parent ? dev->parent->nameunit : "";
178 		break;
179 	default:
180 		return (EINVAL);
181 	}
182 	error = SYSCTL_OUT(req, value, strlen(value));
183 	if (buf != NULL)
184 		kfree(buf, M_BUS);
185 	return (error);
186 }
187 
188 static void
189 device_sysctl_init(device_t dev)
190 {
191 	devclass_t dc = dev->devclass;
192 
193 	if (dev->sysctl_tree != NULL)
194 		return;
195 	devclass_sysctl_init(dc);
196 	sysctl_ctx_init(&dev->sysctl_ctx);
197 	dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
198 	    SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
199 	    dev->nameunit + strlen(dc->name),
200 	    CTLFLAG_RD, NULL, "");
201 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
202 	    OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
203 	    dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
204 	    "device description");
205 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
206 	    OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
207 	    dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
208 	    "device driver name");
209 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
210 	    OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
211 	    dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
212 	    "device location relative to parent");
213 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
214 	    OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
215 	    dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
216 	    "device identification");
217 	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
218 	    OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
219 	    dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
220 	    "parent device");
221 }
222 
223 static void
224 device_sysctl_update(device_t dev)
225 {
226 	devclass_t dc = dev->devclass;
227 
228 	if (dev->sysctl_tree == NULL)
229 		return;
230 	sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
231 }
232 
233 static void
234 device_sysctl_fini(device_t dev)
235 {
236 	if (dev->sysctl_tree == NULL)
237 		return;
238 	sysctl_ctx_free(&dev->sysctl_ctx);
239 	dev->sysctl_tree = NULL;
240 }
241 
242 static void	device_attach_async(device_t dev);
243 static void	device_attach_thread(void *arg);
244 static int	device_doattach(device_t dev);
245 
246 static int do_async_attach = 0;
247 static int numasyncthreads;
248 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
249 
250 /*
251  * /dev/devctl implementation
252  */
253 
254 /*
255  * This design allows only one reader for /dev/devctl.  This is not desirable
256  * in the long run, but will get a lot of hair out of this implementation.
257  * Maybe we should make this device a clonable device.
258  *
259  * Also note: we specifically do not attach a device to the device_t tree
260  * to avoid potential chicken and egg problems.  One could argue that all
261  * of this belongs to the root node.  One could also further argue that the
262  * sysctl interface that we have not might more properly be an ioctl
263  * interface, but at this stage of the game, I'm not inclined to rock that
264  * boat.
265  *
266  * I'm also not sure that the SIGIO support is done correctly or not, as
267  * I copied it from a driver that had SIGIO support that likely hasn't been
268  * tested since 3.4 or 2.2.8!
269  */
270 
271 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
272 static int devctl_disable = 0;
273 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
274 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
275     sysctl_devctl_disable, "I", "devctl disable");
276 
277 static d_open_t		devopen;
278 static d_close_t	devclose;
279 static d_read_t		devread;
280 static d_ioctl_t	devioctl;
281 static d_kqfilter_t	devkqfilter;
282 
283 static struct dev_ops devctl_ops = {
284 	{ "devctl", 0, D_MPSAFE },
285 	.d_open =	devopen,
286 	.d_close =	devclose,
287 	.d_read =	devread,
288 	.d_ioctl =	devioctl,
289 	.d_kqfilter =	devkqfilter
290 };
291 
292 struct dev_event_info
293 {
294 	char *dei_data;
295 	TAILQ_ENTRY(dev_event_info) dei_link;
296 };
297 
298 TAILQ_HEAD(devq, dev_event_info);
299 
300 static struct dev_softc
301 {
302 	int	inuse;
303 	struct lock lock;
304 	struct kqinfo kq;
305 	struct devq devq;
306 	struct proc *async_proc;
307 } devsoftc;
308 
309 /*
310  * Chicken-and-egg problem with devfs, get the queue operational early.
311  */
312 static void
313 predevinit(void)
314 {
315 	lockinit(&devsoftc.lock, "dev mtx", 0, 0);
316 	TAILQ_INIT(&devsoftc.devq);
317 }
318 SYSINIT(predevinit, SI_SUB_CREATE_INIT, SI_ORDER_ANY, predevinit, 0);
319 
320 static void
321 devinit(void)
322 {
323 	/*
324 	 * WARNING! make_dev() can call back into devctl_queue_data()
325 	 *	    immediately.
326 	 */
327 	make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
328 }
329 
330 static int
331 devopen(struct dev_open_args *ap)
332 {
333 	lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
334 	if (devsoftc.inuse) {
335 		lockmgr(&devsoftc.lock, LK_RELEASE);
336 		return (EBUSY);
337 	}
338 	/* move to init */
339 	devsoftc.inuse = 1;
340 	devsoftc.async_proc = NULL;
341 	lockmgr(&devsoftc.lock, LK_RELEASE);
342 
343 	return (0);
344 }
345 
346 static int
347 devclose(struct dev_close_args *ap)
348 {
349 	lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
350 	devsoftc.inuse = 0;
351 	wakeup(&devsoftc);
352 	lockmgr(&devsoftc.lock, LK_RELEASE);
353 
354 	return (0);
355 }
356 
357 /*
358  * The read channel for this device is used to report changes to
359  * userland in realtime.  We are required to free the data as well as
360  * the n1 object because we allocate them separately.  Also note that
361  * we return one record at a time.  If you try to read this device a
362  * character at a time, you will lose the rest of the data.  Listening
363  * programs are expected to cope.
364  */
365 static int
366 devread(struct dev_read_args *ap)
367 {
368 	struct uio *uio = ap->a_uio;
369 	struct dev_event_info *n1;
370 	int rv;
371 
372 	lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
373 	while (TAILQ_EMPTY(&devsoftc.devq)) {
374 		if (ap->a_ioflag & IO_NDELAY) {
375 			lockmgr(&devsoftc.lock, LK_RELEASE);
376 			return (EAGAIN);
377 		}
378 		tsleep_interlock(&devsoftc, PCATCH);
379 		lockmgr(&devsoftc.lock, LK_RELEASE);
380 		rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
381 		lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
382 		if (rv) {
383 			/*
384 			 * Need to translate ERESTART to EINTR here? -- jake
385 			 */
386 			lockmgr(&devsoftc.lock, LK_RELEASE);
387 			return (rv);
388 		}
389 	}
390 	n1 = TAILQ_FIRST(&devsoftc.devq);
391 	TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
392 	lockmgr(&devsoftc.lock, LK_RELEASE);
393 	rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
394 	kfree(n1->dei_data, M_BUS);
395 	kfree(n1, M_BUS);
396 	return (rv);
397 }
398 
399 static	int
400 devioctl(struct dev_ioctl_args *ap)
401 {
402 	switch (ap->a_cmd) {
403 
404 	case FIONBIO:
405 		return (0);
406 	case FIOASYNC:
407 		if (*(int*)ap->a_data)
408 			devsoftc.async_proc = curproc;
409 		else
410 			devsoftc.async_proc = NULL;
411 		return (0);
412 
413 		/* (un)Support for other fcntl() calls. */
414 	case FIOCLEX:
415 	case FIONCLEX:
416 	case FIONREAD:
417 	case FIOSETOWN:
418 	case FIOGETOWN:
419 	default:
420 		break;
421 	}
422 	return (ENOTTY);
423 }
424 
425 static void dev_filter_detach(struct knote *);
426 static int dev_filter_read(struct knote *, long);
427 
428 static struct filterops dev_filtops =
429 	{ FILTEROP_ISFD | FILTEROP_MPSAFE, NULL,
430 	  dev_filter_detach, dev_filter_read };
431 
432 static int
433 devkqfilter(struct dev_kqfilter_args *ap)
434 {
435 	struct knote *kn = ap->a_kn;
436 	struct klist *klist;
437 
438 	ap->a_result = 0;
439 	lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
440 
441 	switch (kn->kn_filter) {
442 	case EVFILT_READ:
443 		kn->kn_fop = &dev_filtops;
444 		break;
445 	default:
446 		ap->a_result = EOPNOTSUPP;
447 		lockmgr(&devsoftc.lock, LK_RELEASE);
448 		return (0);
449 	}
450 
451 	klist = &devsoftc.kq.ki_note;
452 	knote_insert(klist, kn);
453 
454 	lockmgr(&devsoftc.lock, LK_RELEASE);
455 
456 	return (0);
457 }
458 
459 static void
460 dev_filter_detach(struct knote *kn)
461 {
462 	struct klist *klist;
463 
464 	lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
465 	klist = &devsoftc.kq.ki_note;
466 	knote_remove(klist, kn);
467 	lockmgr(&devsoftc.lock, LK_RELEASE);
468 }
469 
470 static int
471 dev_filter_read(struct knote *kn, long hint)
472 {
473 	int ready = 0;
474 
475 	lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
476 	if (!TAILQ_EMPTY(&devsoftc.devq))
477 		ready = 1;
478 	lockmgr(&devsoftc.lock, LK_RELEASE);
479 
480 	return (ready);
481 }
482 
483 
484 /**
485  * @brief Return whether the userland process is running
486  */
487 boolean_t
488 devctl_process_running(void)
489 {
490 	return (devsoftc.inuse == 1);
491 }
492 
493 /**
494  * @brief Queue data to be read from the devctl device
495  *
496  * Generic interface to queue data to the devctl device.  It is
497  * assumed that @p data is properly formatted.  It is further assumed
498  * that @p data is allocated using the M_BUS malloc type.
499  */
500 void
501 devctl_queue_data(char *data)
502 {
503 	struct dev_event_info *n1 = NULL;
504 	struct proc *p;
505 
506 	n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
507 	if (n1 == NULL)
508 		return;
509 	n1->dei_data = data;
510 	lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
511 	TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
512 	wakeup(&devsoftc);
513 	lockmgr(&devsoftc.lock, LK_RELEASE);
514 	KNOTE(&devsoftc.kq.ki_note, 0);
515 	p = devsoftc.async_proc;
516 	if (p != NULL)
517 		ksignal(p, SIGIO);
518 }
519 
520 /**
521  * @brief Send a 'notification' to userland, using standard ways
522  */
523 void
524 devctl_notify(const char *system, const char *subsystem, const char *type,
525     const char *data)
526 {
527 	int len = 0;
528 	char *msg;
529 
530 	if (system == NULL)
531 		return;		/* BOGUS!  Must specify system. */
532 	if (subsystem == NULL)
533 		return;		/* BOGUS!  Must specify subsystem. */
534 	if (type == NULL)
535 		return;		/* BOGUS!  Must specify type. */
536 	len += strlen(" system=") + strlen(system);
537 	len += strlen(" subsystem=") + strlen(subsystem);
538 	len += strlen(" type=") + strlen(type);
539 	/* add in the data message plus newline. */
540 	if (data != NULL)
541 		len += strlen(data);
542 	len += 3;	/* '!', '\n', and NUL */
543 	msg = kmalloc(len, M_BUS, M_NOWAIT);
544 	if (msg == NULL)
545 		return;		/* Drop it on the floor */
546 	if (data != NULL)
547 		ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
548 		    system, subsystem, type, data);
549 	else
550 		ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
551 		    system, subsystem, type);
552 	devctl_queue_data(msg);
553 }
554 
555 /*
556  * Common routine that tries to make sending messages as easy as possible.
557  * We allocate memory for the data, copy strings into that, but do not
558  * free it unless there's an error.  The dequeue part of the driver should
559  * free the data.  We don't send data when the device is disabled.  We do
560  * send data, even when we have no listeners, because we wish to avoid
561  * races relating to startup and restart of listening applications.
562  *
563  * devaddq is designed to string together the type of event, with the
564  * object of that event, plus the plug and play info and location info
565  * for that event.  This is likely most useful for devices, but less
566  * useful for other consumers of this interface.  Those should use
567  * the devctl_queue_data() interface instead.
568  */
569 static void
570 devaddq(const char *type, const char *what, device_t dev)
571 {
572 	char *data = NULL;
573 	char *loc = NULL;
574 	char *pnp = NULL;
575 	const char *parstr;
576 
577 	if (devctl_disable)
578 		return;
579 	data = kmalloc(1024, M_BUS, M_NOWAIT);
580 	if (data == NULL)
581 		goto bad;
582 
583 	/* get the bus specific location of this device */
584 	loc = kmalloc(1024, M_BUS, M_NOWAIT);
585 	if (loc == NULL)
586 		goto bad;
587 	*loc = '\0';
588 	bus_child_location_str(dev, loc, 1024);
589 
590 	/* Get the bus specific pnp info of this device */
591 	pnp = kmalloc(1024, M_BUS, M_NOWAIT);
592 	if (pnp == NULL)
593 		goto bad;
594 	*pnp = '\0';
595 	bus_child_pnpinfo_str(dev, pnp, 1024);
596 
597 	/* Get the parent of this device, or / if high enough in the tree. */
598 	if (device_get_parent(dev) == NULL)
599 		parstr = ".";	/* Or '/' ? */
600 	else
601 		parstr = device_get_nameunit(device_get_parent(dev));
602 	/* String it all together. */
603 	ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
604 	  parstr);
605 	kfree(loc, M_BUS);
606 	kfree(pnp, M_BUS);
607 	devctl_queue_data(data);
608 	return;
609 bad:
610 	if (pnp != NULL)
611 		kfree(pnp, M_BUS);
612 	if (loc != NULL)
613 		kfree(loc, M_BUS);
614 	if (loc != NULL)
615 		kfree(data, M_BUS);
616 	return;
617 }
618 
619 /*
620  * A device was added to the tree.  We are called just after it successfully
621  * attaches (that is, probe and attach success for this device).  No call
622  * is made if a device is merely parented into the tree.  See devnomatch
623  * if probe fails.  If attach fails, no notification is sent (but maybe
624  * we should have a different message for this).
625  */
626 static void
627 devadded(device_t dev)
628 {
629 	char *pnp = NULL;
630 	char *tmp = NULL;
631 
632 	pnp = kmalloc(1024, M_BUS, M_NOWAIT);
633 	if (pnp == NULL)
634 		goto fail;
635 	tmp = kmalloc(1024, M_BUS, M_NOWAIT);
636 	if (tmp == NULL)
637 		goto fail;
638 	*pnp = '\0';
639 	bus_child_pnpinfo_str(dev, pnp, 1024);
640 	ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
641 	devaddq("+", tmp, dev);
642 fail:
643 	if (pnp != NULL)
644 		kfree(pnp, M_BUS);
645 	if (tmp != NULL)
646 		kfree(tmp, M_BUS);
647 	return;
648 }
649 
650 /*
651  * A device was removed from the tree.  We are called just before this
652  * happens.
653  */
654 static void
655 devremoved(device_t dev)
656 {
657 	char *pnp = NULL;
658 	char *tmp = NULL;
659 
660 	pnp = kmalloc(1024, M_BUS, M_NOWAIT);
661 	if (pnp == NULL)
662 		goto fail;
663 	tmp = kmalloc(1024, M_BUS, M_NOWAIT);
664 	if (tmp == NULL)
665 		goto fail;
666 	*pnp = '\0';
667 	bus_child_pnpinfo_str(dev, pnp, 1024);
668 	ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
669 	devaddq("-", tmp, dev);
670 fail:
671 	if (pnp != NULL)
672 		kfree(pnp, M_BUS);
673 	if (tmp != NULL)
674 		kfree(tmp, M_BUS);
675 	return;
676 }
677 
678 /*
679  * Called when there's no match for this device.  This is only called
680  * the first time that no match happens, so we don't keep getitng this
681  * message.  Should that prove to be undesirable, we can change it.
682  * This is called when all drivers that can attach to a given bus
683  * decline to accept this device.  Other errrors may not be detected.
684  */
685 static void
686 devnomatch(device_t dev)
687 {
688 	devaddq("?", "", dev);
689 }
690 
691 static int
692 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
693 {
694 	struct dev_event_info *n1;
695 	int dis, error;
696 
697 	dis = devctl_disable;
698 	error = sysctl_handle_int(oidp, &dis, 0, req);
699 	if (error || !req->newptr)
700 		return (error);
701 	lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
702 	devctl_disable = dis;
703 	if (dis) {
704 		while (!TAILQ_EMPTY(&devsoftc.devq)) {
705 			n1 = TAILQ_FIRST(&devsoftc.devq);
706 			TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
707 			kfree(n1->dei_data, M_BUS);
708 			kfree(n1, M_BUS);
709 		}
710 	}
711 	lockmgr(&devsoftc.lock, LK_RELEASE);
712 	return (0);
713 }
714 
715 /* End of /dev/devctl code */
716 
717 TAILQ_HEAD(,bsd_device)	bus_data_devices;
718 static int bus_data_generation = 1;
719 
720 kobj_method_t null_methods[] = {
721 	{ 0, 0 }
722 };
723 
724 DEFINE_CLASS(null, null_methods, 0);
725 
726 /*
727  * Devclass implementation
728  */
729 
730 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
731 
732 static devclass_t
733 devclass_find_internal(const char *classname, const char *parentname,
734 		       int create)
735 {
736 	devclass_t dc;
737 
738 	PDEBUG(("looking for %s", classname));
739 	if (classname == NULL)
740 		return(NULL);
741 
742 	TAILQ_FOREACH(dc, &devclasses, link)
743 		if (!strcmp(dc->name, classname))
744 			break;
745 
746 	if (create && !dc) {
747 		PDEBUG(("creating %s", classname));
748 		dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
749 			    M_BUS, M_INTWAIT | M_ZERO);
750 		dc->parent = NULL;
751 		dc->name = (char*) (dc + 1);
752 		strcpy(dc->name, classname);
753 		dc->devices = NULL;
754 		dc->maxunit = 0;
755 		TAILQ_INIT(&dc->drivers);
756 		TAILQ_INSERT_TAIL(&devclasses, dc, link);
757 
758 		bus_data_generation_update();
759 
760 	}
761 
762 	/*
763 	 * If a parent class is specified, then set that as our parent so
764 	 * that this devclass will support drivers for the parent class as
765 	 * well.  If the parent class has the same name don't do this though
766 	 * as it creates a cycle that can trigger an infinite loop in
767 	 * device_probe_child() if a device exists for which there is no
768 	 * suitable driver.
769 	 */
770 	if (parentname && dc && !dc->parent &&
771 	    strcmp(classname, parentname) != 0)
772 		dc->parent = devclass_find_internal(parentname, NULL, FALSE);
773 
774 	return(dc);
775 }
776 
777 devclass_t
778 devclass_create(const char *classname)
779 {
780 	return(devclass_find_internal(classname, NULL, TRUE));
781 }
782 
783 devclass_t
784 devclass_find(const char *classname)
785 {
786 	return(devclass_find_internal(classname, NULL, FALSE));
787 }
788 
789 device_t
790 devclass_find_unit(const char *classname, int unit)
791 {
792 	devclass_t dc;
793 
794 	if ((dc = devclass_find(classname)) != NULL)
795 	    return(devclass_get_device(dc, unit));
796 	return (NULL);
797 }
798 
799 int
800 devclass_add_driver(devclass_t dc, driver_t *driver)
801 {
802 	driverlink_t dl;
803 	device_t dev;
804 	int i;
805 
806 	PDEBUG(("%s", DRIVERNAME(driver)));
807 
808 	dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
809 
810 	/*
811 	 * Compile the driver's methods. Also increase the reference count
812 	 * so that the class doesn't get freed when the last instance
813 	 * goes. This means we can safely use static methods and avoids a
814 	 * double-free in devclass_delete_driver.
815 	 */
816 	kobj_class_instantiate(driver);
817 
818 	/*
819 	 * Make sure the devclass which the driver is implementing exists.
820 	 */
821 	devclass_find_internal(driver->name, NULL, TRUE);
822 
823 	dl->driver = driver;
824 	TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
825 
826 	/*
827 	 * Call BUS_DRIVER_ADDED for any existing busses in this class,
828 	 * but only if the bus has already been attached (otherwise we
829 	 * might probe too early).
830 	 *
831 	 * This is what will cause a newly loaded module to be associated
832 	 * with hardware.  bus_generic_driver_added() is typically what ends
833 	 * up being called.
834 	 */
835 	for (i = 0; i < dc->maxunit; i++) {
836 		if ((dev = dc->devices[i]) != NULL) {
837 			if (dev->state >= DS_ATTACHED)
838 				BUS_DRIVER_ADDED(dev, driver);
839 		}
840 	}
841 
842 	bus_data_generation_update();
843 	return(0);
844 }
845 
846 int
847 devclass_delete_driver(devclass_t busclass, driver_t *driver)
848 {
849 	devclass_t dc = devclass_find(driver->name);
850 	driverlink_t dl;
851 	device_t dev;
852 	int i;
853 	int error;
854 
855 	PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
856 
857 	if (!dc)
858 		return(0);
859 
860 	/*
861 	 * Find the link structure in the bus' list of drivers.
862 	 */
863 	TAILQ_FOREACH(dl, &busclass->drivers, link)
864 		if (dl->driver == driver)
865 			break;
866 
867 	if (!dl) {
868 		PDEBUG(("%s not found in %s list", driver->name, busclass->name));
869 		return(ENOENT);
870 	}
871 
872 	/*
873 	 * Disassociate from any devices.  We iterate through all the
874 	 * devices in the devclass of the driver and detach any which are
875 	 * using the driver and which have a parent in the devclass which
876 	 * we are deleting from.
877 	 *
878 	 * Note that since a driver can be in multiple devclasses, we
879 	 * should not detach devices which are not children of devices in
880 	 * the affected devclass.
881 	 */
882 	for (i = 0; i < dc->maxunit; i++)
883 		if (dc->devices[i]) {
884 			dev = dc->devices[i];
885 			if (dev->driver == driver && dev->parent &&
886 			    dev->parent->devclass == busclass) {
887 				if ((error = device_detach(dev)) != 0)
888 					return(error);
889 				device_set_driver(dev, NULL);
890 		    	}
891 		}
892 
893 	TAILQ_REMOVE(&busclass->drivers, dl, link);
894 	kfree(dl, M_BUS);
895 
896 	kobj_class_uninstantiate(driver);
897 
898 	bus_data_generation_update();
899 	return(0);
900 }
901 
902 static driverlink_t
903 devclass_find_driver_internal(devclass_t dc, const char *classname)
904 {
905 	driverlink_t dl;
906 
907 	PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
908 
909 	TAILQ_FOREACH(dl, &dc->drivers, link)
910 		if (!strcmp(dl->driver->name, classname))
911 			return(dl);
912 
913 	PDEBUG(("not found"));
914 	return(NULL);
915 }
916 
917 kobj_class_t
918 devclass_find_driver(devclass_t dc, const char *classname)
919 {
920 	driverlink_t dl;
921 
922 	dl = devclass_find_driver_internal(dc, classname);
923 	if (dl)
924 		return(dl->driver);
925 	else
926 		return(NULL);
927 }
928 
929 const char *
930 devclass_get_name(devclass_t dc)
931 {
932 	return(dc->name);
933 }
934 
935 device_t
936 devclass_get_device(devclass_t dc, int unit)
937 {
938 	if (dc == NULL || unit < 0 || unit >= dc->maxunit)
939 		return(NULL);
940 	return(dc->devices[unit]);
941 }
942 
943 void *
944 devclass_get_softc(devclass_t dc, int unit)
945 {
946 	device_t dev;
947 
948 	dev = devclass_get_device(dc, unit);
949 	if (!dev)
950 		return(NULL);
951 
952 	return(device_get_softc(dev));
953 }
954 
955 int
956 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
957 {
958 	int i;
959 	int count;
960 	device_t *list;
961 
962 	count = 0;
963 	for (i = 0; i < dc->maxunit; i++)
964 		if (dc->devices[i])
965 			count++;
966 
967 	list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
968 
969 	count = 0;
970 	for (i = 0; i < dc->maxunit; i++)
971 		if (dc->devices[i]) {
972 			list[count] = dc->devices[i];
973 			count++;
974 		}
975 
976 	*devlistp = list;
977 	*devcountp = count;
978 
979 	return(0);
980 }
981 
982 /**
983  * @brief Get a list of drivers in the devclass
984  *
985  * An array containing a list of pointers to all the drivers in the
986  * given devclass is allocated and returned in @p *listp.  The number
987  * of drivers in the array is returned in @p *countp. The caller should
988  * free the array using @c free(p, M_TEMP).
989  *
990  * @param dc            the devclass to examine
991  * @param listp         gives location for array pointer return value
992  * @param countp        gives location for number of array elements
993  *                      return value
994  *
995  * @retval 0            success
996  * @retval ENOMEM       the array allocation failed
997  */
998 int
999 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1000 {
1001         driverlink_t dl;
1002         driver_t **list;
1003         int count;
1004 
1005         count = 0;
1006         TAILQ_FOREACH(dl, &dc->drivers, link)
1007                 count++;
1008         list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1009         if (list == NULL)
1010                 return (ENOMEM);
1011 
1012         count = 0;
1013         TAILQ_FOREACH(dl, &dc->drivers, link) {
1014                 list[count] = dl->driver;
1015                 count++;
1016         }
1017         *listp = list;
1018         *countp = count;
1019 
1020         return (0);
1021 }
1022 
1023 /**
1024  * @brief Get the number of devices in a devclass
1025  *
1026  * @param dc		the devclass to examine
1027  */
1028 int
1029 devclass_get_count(devclass_t dc)
1030 {
1031 	int count, i;
1032 
1033 	count = 0;
1034 	for (i = 0; i < dc->maxunit; i++)
1035 		if (dc->devices[i])
1036 			count++;
1037 	return (count);
1038 }
1039 
1040 int
1041 devclass_get_maxunit(devclass_t dc)
1042 {
1043 	return(dc->maxunit);
1044 }
1045 
1046 void
1047 devclass_set_parent(devclass_t dc, devclass_t pdc)
1048 {
1049         dc->parent = pdc;
1050 }
1051 
1052 devclass_t
1053 devclass_get_parent(devclass_t dc)
1054 {
1055 	return(dc->parent);
1056 }
1057 
1058 static int
1059 devclass_alloc_unit(devclass_t dc, int *unitp)
1060 {
1061 	int unit = *unitp;
1062 
1063 	PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1064 
1065 	/* If we have been given a wired unit number, check for existing device */
1066 	if (unit != -1) {
1067 		if (unit >= 0 && unit < dc->maxunit &&
1068 		    dc->devices[unit] != NULL) {
1069 			if (bootverbose)
1070 				kprintf("%s-: %s%d exists, using next available unit number\n",
1071 				       dc->name, dc->name, unit);
1072 			/* find the next available slot */
1073 			while (++unit < dc->maxunit && dc->devices[unit] != NULL)
1074 				;
1075 		}
1076 	} else {
1077 		/* Unwired device, find the next available slot for it */
1078 		unit = 0;
1079 		while (unit < dc->maxunit && dc->devices[unit] != NULL)
1080 			unit++;
1081 	}
1082 
1083 	/*
1084 	 * We've selected a unit beyond the length of the table, so let's
1085 	 * extend the table to make room for all units up to and including
1086 	 * this one.
1087 	 */
1088 	if (unit >= dc->maxunit) {
1089 		device_t *newlist;
1090 		int newsize;
1091 
1092 		newsize = (unit + 1);
1093 		newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
1094 				 M_INTWAIT | M_ZERO);
1095 		if (newlist == NULL)
1096 			return(ENOMEM);
1097 		bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
1098 		if (dc->devices)
1099 			kfree(dc->devices, M_BUS);
1100 		dc->devices = newlist;
1101 		dc->maxunit = newsize;
1102 	}
1103 	PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1104 
1105 	*unitp = unit;
1106 	return(0);
1107 }
1108 
1109 static int
1110 devclass_add_device(devclass_t dc, device_t dev)
1111 {
1112 	int buflen, error;
1113 
1114 	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1115 
1116 	buflen = strlen(dc->name) + 5;
1117 	dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
1118 	if (dev->nameunit == NULL)
1119 		return(ENOMEM);
1120 
1121 	if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1122 		kfree(dev->nameunit, M_BUS);
1123 		dev->nameunit = NULL;
1124 		return(error);
1125 	}
1126 	dc->devices[dev->unit] = dev;
1127 	dev->devclass = dc;
1128 	ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1129 
1130 	return(0);
1131 }
1132 
1133 static int
1134 devclass_delete_device(devclass_t dc, device_t dev)
1135 {
1136 	if (!dc || !dev)
1137 		return(0);
1138 
1139 	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1140 
1141 	if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1142 		panic("devclass_delete_device: inconsistent device class");
1143 	dc->devices[dev->unit] = NULL;
1144 	if (dev->flags & DF_WILDCARD)
1145 		dev->unit = -1;
1146 	dev->devclass = NULL;
1147 	kfree(dev->nameunit, M_BUS);
1148 	dev->nameunit = NULL;
1149 
1150 	return(0);
1151 }
1152 
1153 static device_t
1154 make_device(device_t parent, const char *name, int unit)
1155 {
1156 	device_t dev;
1157 	devclass_t dc;
1158 
1159 	PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1160 
1161 	if (name != NULL) {
1162 		dc = devclass_find_internal(name, NULL, TRUE);
1163 		if (!dc) {
1164 			kprintf("make_device: can't find device class %s\n", name);
1165 			return(NULL);
1166 		}
1167 	} else
1168 		dc = NULL;
1169 
1170 	dev = kmalloc(sizeof(struct bsd_device), M_BUS, M_INTWAIT | M_ZERO);
1171 	if (!dev)
1172 		return(0);
1173 
1174 	dev->parent = parent;
1175 	TAILQ_INIT(&dev->children);
1176 	kobj_init((kobj_t) dev, &null_class);
1177 	dev->driver = NULL;
1178 	dev->devclass = NULL;
1179 	dev->unit = unit;
1180 	dev->nameunit = NULL;
1181 	dev->desc = NULL;
1182 	dev->busy = 0;
1183 	dev->devflags = 0;
1184 	dev->flags = DF_ENABLED;
1185 	dev->order = 0;
1186 	if (unit == -1)
1187 		dev->flags |= DF_WILDCARD;
1188 	if (name) {
1189 		dev->flags |= DF_FIXEDCLASS;
1190 		if (devclass_add_device(dc, dev) != 0) {
1191 			kobj_delete((kobj_t)dev, M_BUS);
1192 			return(NULL);
1193 		}
1194     	}
1195 	dev->ivars = NULL;
1196 	dev->softc = NULL;
1197 
1198 	dev->state = DS_NOTPRESENT;
1199 
1200 	TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1201 	bus_data_generation_update();
1202 
1203 	return(dev);
1204 }
1205 
1206 static int
1207 device_print_child(device_t dev, device_t child)
1208 {
1209 	int retval = 0;
1210 
1211 	if (device_is_alive(child))
1212 		retval += BUS_PRINT_CHILD(dev, child);
1213 	else
1214 		retval += device_printf(child, " not found\n");
1215 
1216 	return(retval);
1217 }
1218 
1219 device_t
1220 device_add_child(device_t dev, const char *name, int unit)
1221 {
1222 	return device_add_child_ordered(dev, 0, name, unit);
1223 }
1224 
1225 device_t
1226 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1227 {
1228 	device_t child;
1229 	device_t place;
1230 
1231 	PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1232 		order, unit));
1233 
1234 	child = make_device(dev, name, unit);
1235 	if (child == NULL)
1236 		return child;
1237 	child->order = order;
1238 
1239 	TAILQ_FOREACH(place, &dev->children, link) {
1240 		if (place->order > order)
1241 			break;
1242 	}
1243 
1244 	if (place) {
1245 		/*
1246 		 * The device 'place' is the first device whose order is
1247 		 * greater than the new child.
1248 		 */
1249 		TAILQ_INSERT_BEFORE(place, child, link);
1250 	} else {
1251 		/*
1252 		 * The new child's order is greater or equal to the order of
1253 		 * any existing device. Add the child to the tail of the list.
1254 		 */
1255 		TAILQ_INSERT_TAIL(&dev->children, child, link);
1256     	}
1257 
1258 	bus_data_generation_update();
1259 	return(child);
1260 }
1261 
1262 int
1263 device_delete_child(device_t dev, device_t child)
1264 {
1265 	int error;
1266 	device_t grandchild;
1267 
1268 	PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1269 
1270 	/* remove children first */
1271 	while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1272         	error = device_delete_child(child, grandchild);
1273 		if (error)
1274 			return(error);
1275 	}
1276 
1277 	if ((error = device_detach(child)) != 0)
1278 		return(error);
1279 	if (child->devclass)
1280 		devclass_delete_device(child->devclass, child);
1281 	TAILQ_REMOVE(&dev->children, child, link);
1282 	TAILQ_REMOVE(&bus_data_devices, child, devlink);
1283 	kobj_delete((kobj_t)child, M_BUS);
1284 
1285 	bus_data_generation_update();
1286 	return(0);
1287 }
1288 
1289 /**
1290  * @brief Delete all children devices of the given device, if any.
1291  *
1292  * This function deletes all children devices of the given device, if
1293  * any, using the device_delete_child() function for each device it
1294  * finds. If a child device cannot be deleted, this function will
1295  * return an error code.
1296  *
1297  * @param dev		the parent device
1298  *
1299  * @retval 0		success
1300  * @retval non-zero	a device would not detach
1301  */
1302 int
1303 device_delete_children(device_t dev)
1304 {
1305 	device_t child;
1306 	int error;
1307 
1308 	PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1309 
1310 	error = 0;
1311 
1312 	while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1313 		error = device_delete_child(dev, child);
1314 		if (error) {
1315 			PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1316 			break;
1317 		}
1318 	}
1319 	return (error);
1320 }
1321 
1322 /**
1323  * @brief Find a device given a unit number
1324  *
1325  * This is similar to devclass_get_devices() but only searches for
1326  * devices which have @p dev as a parent.
1327  *
1328  * @param dev		the parent device to search
1329  * @param unit		the unit number to search for.  If the unit is -1,
1330  *			return the first child of @p dev which has name
1331  *			@p classname (that is, the one with the lowest unit.)
1332  *
1333  * @returns		the device with the given unit number or @c
1334  *			NULL if there is no such device
1335  */
1336 device_t
1337 device_find_child(device_t dev, const char *classname, int unit)
1338 {
1339 	devclass_t dc;
1340 	device_t child;
1341 
1342 	dc = devclass_find(classname);
1343 	if (!dc)
1344 		return(NULL);
1345 
1346 	if (unit != -1) {
1347 		child = devclass_get_device(dc, unit);
1348 		if (child && child->parent == dev)
1349 			return (child);
1350 	} else {
1351 		for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1352 			child = devclass_get_device(dc, unit);
1353 			if (child && child->parent == dev)
1354 				return (child);
1355 		}
1356 	}
1357 	return(NULL);
1358 }
1359 
1360 static driverlink_t
1361 first_matching_driver(devclass_t dc, device_t dev)
1362 {
1363 	if (dev->devclass)
1364 		return(devclass_find_driver_internal(dc, dev->devclass->name));
1365 	else
1366 		return(TAILQ_FIRST(&dc->drivers));
1367 }
1368 
1369 static driverlink_t
1370 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1371 {
1372 	if (dev->devclass) {
1373 		driverlink_t dl;
1374 		for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1375 			if (!strcmp(dev->devclass->name, dl->driver->name))
1376 				return(dl);
1377 		return(NULL);
1378 	} else
1379 		return(TAILQ_NEXT(last, link));
1380 }
1381 
1382 int
1383 device_probe_child(device_t dev, device_t child)
1384 {
1385 	devclass_t dc;
1386 	driverlink_t best = NULL;
1387 	driverlink_t dl;
1388 	int result, pri = 0;
1389 	int hasclass = (child->devclass != NULL);
1390 
1391 	dc = dev->devclass;
1392 	if (!dc)
1393 		panic("device_probe_child: parent device has no devclass");
1394 
1395 	if (child->state == DS_ALIVE)
1396 		return(0);
1397 
1398 	for (; dc; dc = dc->parent) {
1399 		for (dl = first_matching_driver(dc, child); dl;
1400 		     dl = next_matching_driver(dc, child, dl)) {
1401 			PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1402 			device_set_driver(child, dl->driver);
1403 			if (!hasclass)
1404 				device_set_devclass(child, dl->driver->name);
1405 			result = DEVICE_PROBE(child);
1406 			if (!hasclass)
1407 				device_set_devclass(child, 0);
1408 
1409 			/*
1410 			 * If the driver returns SUCCESS, there can be
1411 			 * no higher match for this device.
1412 			 */
1413 			if (result == 0) {
1414 				best = dl;
1415 				pri = 0;
1416 				break;
1417 			}
1418 
1419 			/*
1420 			 * The driver returned an error so it
1421 			 * certainly doesn't match.
1422 			 */
1423 			if (result > 0) {
1424 				device_set_driver(child, NULL);
1425 				continue;
1426 			}
1427 
1428 			/*
1429 			 * A priority lower than SUCCESS, remember the
1430 			 * best matching driver. Initialise the value
1431 			 * of pri for the first match.
1432 			 */
1433 			if (best == NULL || result > pri) {
1434 				best = dl;
1435 				pri = result;
1436 				continue;
1437 			}
1438 		}
1439 		/*
1440 	         * If we have unambiguous match in this devclass,
1441 	         * don't look in the parent.
1442 	         */
1443 	        if (best && pri == 0)
1444 			break;
1445 	}
1446 
1447 	/*
1448 	 * If we found a driver, change state and initialise the devclass.
1449 	 */
1450 	if (best) {
1451 		if (!child->devclass)
1452 			device_set_devclass(child, best->driver->name);
1453 		device_set_driver(child, best->driver);
1454 		if (pri < 0) {
1455 			/*
1456 			 * A bit bogus. Call the probe method again to make
1457 			 * sure that we have the right description.
1458 			 */
1459 			DEVICE_PROBE(child);
1460 		}
1461 
1462 		bus_data_generation_update();
1463 		child->state = DS_ALIVE;
1464 		return(0);
1465 	}
1466 
1467 	return(ENXIO);
1468 }
1469 
1470 int
1471 device_probe_child_gpri(device_t dev, device_t child, u_int gpri)
1472 {
1473 	devclass_t dc;
1474 	driverlink_t best = NULL;
1475 	driverlink_t dl;
1476 	int result, pri = 0;
1477 	int hasclass = (child->devclass != NULL);
1478 
1479 	dc = dev->devclass;
1480 	if (!dc)
1481 		panic("device_probe_child: parent device has no devclass");
1482 
1483 	if (child->state == DS_ALIVE)
1484 		return(0);
1485 
1486 	for (; dc; dc = dc->parent) {
1487 		for (dl = first_matching_driver(dc, child); dl;
1488 			dl = next_matching_driver(dc, child, dl)) {
1489 			/*
1490 			 * GPRI handling, only probe drivers with the
1491 			 * specific GPRI.
1492 			 */
1493 			if (dl->driver->gpri != gpri)
1494 				continue;
1495 
1496 			PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1497 			device_set_driver(child, dl->driver);
1498 			if (!hasclass)
1499 				device_set_devclass(child, dl->driver->name);
1500 			result = DEVICE_PROBE(child);
1501 			if (!hasclass)
1502 				device_set_devclass(child, 0);
1503 
1504 			/*
1505 			 * If the driver returns SUCCESS, there can be
1506 			 * no higher match for this device.
1507 			 */
1508 			if (result == 0) {
1509 				best = dl;
1510 				pri = 0;
1511 				break;
1512 			}
1513 
1514 			/*
1515 			 * The driver returned an error so it
1516 			 * certainly doesn't match.
1517 			 */
1518 			if (result > 0) {
1519 				device_set_driver(child, NULL);
1520 				continue;
1521 			}
1522 
1523 			/*
1524 			 * A priority lower than SUCCESS, remember the
1525 			 * best matching driver. Initialise the value
1526 			 * of pri for the first match.
1527 			 */
1528 			if (best == NULL || result > pri) {
1529 				best = dl;
1530 				pri = result;
1531 				continue;
1532 			}
1533 	        }
1534 		/*
1535 	         * If we have unambiguous match in this devclass,
1536 	         * don't look in the parent.
1537 	         */
1538 	        if (best && pri == 0)
1539 			break;
1540 	}
1541 
1542 	/*
1543 	 * If we found a driver, change state and initialise the devclass.
1544 	 */
1545 	if (best) {
1546 		if (!child->devclass)
1547 			device_set_devclass(child, best->driver->name);
1548 		device_set_driver(child, best->driver);
1549 		if (pri < 0) {
1550 			/*
1551 			 * A bit bogus. Call the probe method again to make
1552 			 * sure that we have the right description.
1553 			 */
1554 			DEVICE_PROBE(child);
1555 		}
1556 
1557 		bus_data_generation_update();
1558 		child->state = DS_ALIVE;
1559 		return(0);
1560 	}
1561 
1562 	return(ENXIO);
1563 }
1564 
1565 device_t
1566 device_get_parent(device_t dev)
1567 {
1568 	return dev->parent;
1569 }
1570 
1571 int
1572 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1573 {
1574 	int count;
1575 	device_t child;
1576 	device_t *list;
1577 
1578 	count = 0;
1579 	TAILQ_FOREACH(child, &dev->children, link)
1580 		count++;
1581 
1582 	list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1583 
1584 	count = 0;
1585 	TAILQ_FOREACH(child, &dev->children, link) {
1586 		list[count] = child;
1587 		count++;
1588 	}
1589 
1590 	*devlistp = list;
1591 	*devcountp = count;
1592 
1593 	return(0);
1594 }
1595 
1596 driver_t *
1597 device_get_driver(device_t dev)
1598 {
1599 	return(dev->driver);
1600 }
1601 
1602 devclass_t
1603 device_get_devclass(device_t dev)
1604 {
1605 	return(dev->devclass);
1606 }
1607 
1608 const char *
1609 device_get_name(device_t dev)
1610 {
1611 	if (dev->devclass)
1612 		return devclass_get_name(dev->devclass);
1613 	return(NULL);
1614 }
1615 
1616 const char *
1617 device_get_nameunit(device_t dev)
1618 {
1619 	return(dev->nameunit);
1620 }
1621 
1622 int
1623 device_get_unit(device_t dev)
1624 {
1625 	return(dev->unit);
1626 }
1627 
1628 const char *
1629 device_get_desc(device_t dev)
1630 {
1631 	return(dev->desc);
1632 }
1633 
1634 uint32_t
1635 device_get_flags(device_t dev)
1636 {
1637 	return(dev->devflags);
1638 }
1639 
1640 struct sysctl_ctx_list *
1641 device_get_sysctl_ctx(device_t dev)
1642 {
1643 	return (&dev->sysctl_ctx);
1644 }
1645 
1646 struct sysctl_oid *
1647 device_get_sysctl_tree(device_t dev)
1648 {
1649 	return (dev->sysctl_tree);
1650 }
1651 
1652 int
1653 device_print_prettyname(device_t dev)
1654 {
1655 	const char *name = device_get_name(dev);
1656 
1657 	if (name == NULL)
1658 		return kprintf("unknown: ");
1659 	else
1660 		return kprintf("%s%d: ", name, device_get_unit(dev));
1661 }
1662 
1663 int
1664 device_printf(device_t dev, const char * fmt, ...)
1665 {
1666 	__va_list ap;
1667 	int retval;
1668 
1669 	retval = device_print_prettyname(dev);
1670 	__va_start(ap, fmt);
1671 	retval += kvprintf(fmt, ap);
1672 	__va_end(ap);
1673 	return retval;
1674 }
1675 
1676 static void
1677 device_set_desc_internal(device_t dev, const char* desc, int copy)
1678 {
1679 	if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1680 		kfree(dev->desc, M_BUS);
1681 		dev->flags &= ~DF_DESCMALLOCED;
1682 		dev->desc = NULL;
1683 	}
1684 
1685 	if (copy && desc) {
1686 		dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1687 		if (dev->desc) {
1688 			strcpy(dev->desc, desc);
1689 			dev->flags |= DF_DESCMALLOCED;
1690 		}
1691 	} else {
1692 		/* Avoid a -Wcast-qual warning */
1693 		dev->desc = (char *)(uintptr_t) desc;
1694 	}
1695 
1696 	bus_data_generation_update();
1697 }
1698 
1699 void
1700 device_set_desc(device_t dev, const char* desc)
1701 {
1702 	device_set_desc_internal(dev, desc, FALSE);
1703 }
1704 
1705 void
1706 device_set_desc_copy(device_t dev, const char* desc)
1707 {
1708 	device_set_desc_internal(dev, desc, TRUE);
1709 }
1710 
1711 void
1712 device_set_flags(device_t dev, uint32_t flags)
1713 {
1714 	dev->devflags = flags;
1715 }
1716 
1717 void *
1718 device_get_softc(device_t dev)
1719 {
1720 	return dev->softc;
1721 }
1722 
1723 void
1724 device_set_softc(device_t dev, void *softc)
1725 {
1726 	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1727 		kfree(dev->softc, M_BUS);
1728 	dev->softc = softc;
1729 	if (dev->softc)
1730 		dev->flags |= DF_EXTERNALSOFTC;
1731 	else
1732 		dev->flags &= ~DF_EXTERNALSOFTC;
1733 }
1734 
1735 void
1736 device_set_async_attach(device_t dev, int enable)
1737 {
1738 	if (enable)
1739 		dev->flags |= DF_ASYNCPROBE;
1740 	else
1741 		dev->flags &= ~DF_ASYNCPROBE;
1742 }
1743 
1744 void *
1745 device_get_ivars(device_t dev)
1746 {
1747 	return dev->ivars;
1748 }
1749 
1750 void
1751 device_set_ivars(device_t dev, void * ivars)
1752 {
1753 	if (!dev)
1754 		return;
1755 
1756 	dev->ivars = ivars;
1757 }
1758 
1759 device_state_t
1760 device_get_state(device_t dev)
1761 {
1762 	return(dev->state);
1763 }
1764 
1765 void
1766 device_enable(device_t dev)
1767 {
1768 	dev->flags |= DF_ENABLED;
1769 }
1770 
1771 void
1772 device_disable(device_t dev)
1773 {
1774 	dev->flags &= ~DF_ENABLED;
1775 }
1776 
1777 /*
1778  * YYY cannot block
1779  */
1780 void
1781 device_busy(device_t dev)
1782 {
1783 	if (dev->state < DS_ATTACHED)
1784 		panic("device_busy: called for unattached device");
1785 	if (dev->busy == 0 && dev->parent)
1786 		device_busy(dev->parent);
1787 	dev->busy++;
1788 	dev->state = DS_BUSY;
1789 }
1790 
1791 /*
1792  * YYY cannot block
1793  */
1794 void
1795 device_unbusy(device_t dev)
1796 {
1797 	if (dev->state != DS_BUSY)
1798 		panic("device_unbusy: called for non-busy device");
1799 	dev->busy--;
1800 	if (dev->busy == 0) {
1801 		if (dev->parent)
1802 			device_unbusy(dev->parent);
1803 		dev->state = DS_ATTACHED;
1804 	}
1805 }
1806 
1807 void
1808 device_quiet(device_t dev)
1809 {
1810 	dev->flags |= DF_QUIET;
1811 }
1812 
1813 void
1814 device_verbose(device_t dev)
1815 {
1816 	dev->flags &= ~DF_QUIET;
1817 }
1818 
1819 int
1820 device_is_quiet(device_t dev)
1821 {
1822 	return((dev->flags & DF_QUIET) != 0);
1823 }
1824 
1825 int
1826 device_is_enabled(device_t dev)
1827 {
1828 	return((dev->flags & DF_ENABLED) != 0);
1829 }
1830 
1831 int
1832 device_is_alive(device_t dev)
1833 {
1834 	return(dev->state >= DS_ALIVE);
1835 }
1836 
1837 int
1838 device_is_attached(device_t dev)
1839 {
1840 	return(dev->state >= DS_ATTACHED);
1841 }
1842 
1843 int
1844 device_set_devclass(device_t dev, const char *classname)
1845 {
1846 	devclass_t dc;
1847 	int error;
1848 
1849 	if (!classname) {
1850 		if (dev->devclass)
1851 			devclass_delete_device(dev->devclass, dev);
1852 		return(0);
1853 	}
1854 
1855 	if (dev->devclass) {
1856 		kprintf("device_set_devclass: device class already set\n");
1857 		return(EINVAL);
1858 	}
1859 
1860 	dc = devclass_find_internal(classname, NULL, TRUE);
1861 	if (!dc)
1862 		return(ENOMEM);
1863 
1864 	error = devclass_add_device(dc, dev);
1865 
1866 	bus_data_generation_update();
1867 	return(error);
1868 }
1869 
1870 int
1871 device_set_driver(device_t dev, driver_t *driver)
1872 {
1873 	if (dev->state >= DS_ATTACHED)
1874 		return(EBUSY);
1875 
1876 	if (dev->driver == driver)
1877 		return(0);
1878 
1879 	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1880 		kfree(dev->softc, M_BUS);
1881 		dev->softc = NULL;
1882 	}
1883 	device_set_desc(dev, NULL);
1884 	kobj_delete((kobj_t) dev, 0);
1885 	dev->driver = driver;
1886 	if (driver) {
1887 		kobj_init((kobj_t) dev, (kobj_class_t) driver);
1888 		if (!(dev->flags & DF_EXTERNALSOFTC))
1889 			dev->softc = kmalloc(driver->size, M_BUS,
1890 					    M_INTWAIT | M_ZERO);
1891 	} else {
1892 		kobj_init((kobj_t) dev, &null_class);
1893 	}
1894 
1895 	bus_data_generation_update();
1896 	return(0);
1897 }
1898 
1899 int
1900 device_probe_and_attach(device_t dev)
1901 {
1902 	device_t bus = dev->parent;
1903 	int error = 0;
1904 
1905 	if (dev->state >= DS_ALIVE)
1906 		return(0);
1907 
1908 	if ((dev->flags & DF_ENABLED) == 0) {
1909 		if (bootverbose) {
1910 			device_print_prettyname(dev);
1911 			kprintf("not probed (disabled)\n");
1912 		}
1913 		return(0);
1914 	}
1915 
1916 	error = device_probe_child(bus, dev);
1917 	if (error) {
1918 		if (!(dev->flags & DF_DONENOMATCH)) {
1919 			BUS_PROBE_NOMATCH(bus, dev);
1920 			devnomatch(dev);
1921 			dev->flags |= DF_DONENOMATCH;
1922 		}
1923 		return(error);
1924 	}
1925 
1926 	/*
1927 	 * Output the exact device chain prior to the attach in case the
1928 	 * system locks up during attach, and generate the full info after
1929 	 * the attach so correct irq and other information is displayed.
1930 	 */
1931 	if (bootverbose && !device_is_quiet(dev)) {
1932 		device_t tmp;
1933 
1934 		kprintf("%s", device_get_nameunit(dev));
1935 		for (tmp = dev->parent; tmp; tmp = tmp->parent)
1936 			kprintf(".%s", device_get_nameunit(tmp));
1937 		kprintf("\n");
1938 	}
1939 	if (!device_is_quiet(dev))
1940 		device_print_child(bus, dev);
1941 	if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1942 		kprintf("%s: probing asynchronously\n",
1943 			device_get_nameunit(dev));
1944 		dev->state = DS_INPROGRESS;
1945 		device_attach_async(dev);
1946 		error = 0;
1947 	} else {
1948 		error = device_doattach(dev);
1949 	}
1950 	return(error);
1951 }
1952 
1953 int
1954 device_probe_and_attach_gpri(device_t dev, u_int gpri)
1955 {
1956 	device_t bus = dev->parent;
1957 	int error = 0;
1958 
1959 	if (dev->state >= DS_ALIVE)
1960 		return(0);
1961 
1962 	if ((dev->flags & DF_ENABLED) == 0) {
1963 		if (bootverbose) {
1964 			device_print_prettyname(dev);
1965 			kprintf("not probed (disabled)\n");
1966 		}
1967 		return(0);
1968 	}
1969 
1970 	error = device_probe_child_gpri(bus, dev, gpri);
1971 	if (error) {
1972 #if 0
1973 		if (!(dev->flags & DF_DONENOMATCH)) {
1974 			BUS_PROBE_NOMATCH(bus, dev);
1975 			devnomatch(dev);
1976 			dev->flags |= DF_DONENOMATCH;
1977 		}
1978 #endif
1979 		return(error);
1980 	}
1981 
1982 	/*
1983 	 * Output the exact device chain prior to the attach in case the
1984 	 * system locks up during attach, and generate the full info after
1985 	 * the attach so correct irq and other information is displayed.
1986 	 */
1987 	if (bootverbose && !device_is_quiet(dev)) {
1988 		device_t tmp;
1989 
1990 		kprintf("%s", device_get_nameunit(dev));
1991 		for (tmp = dev->parent; tmp; tmp = tmp->parent)
1992 			kprintf(".%s", device_get_nameunit(tmp));
1993 		kprintf("\n");
1994 	}
1995 	if (!device_is_quiet(dev))
1996 		device_print_child(bus, dev);
1997 	if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1998 		kprintf("%s: probing asynchronously\n",
1999 			device_get_nameunit(dev));
2000 		dev->state = DS_INPROGRESS;
2001 		device_attach_async(dev);
2002 		error = 0;
2003 	} else {
2004 		error = device_doattach(dev);
2005 	}
2006 	return(error);
2007 }
2008 
2009 /*
2010  * Device is known to be alive, do the attach asynchronously.
2011  * However, serialize the attaches with the mp lock.
2012  */
2013 static void
2014 device_attach_async(device_t dev)
2015 {
2016 	thread_t td;
2017 
2018 	atomic_add_int(&numasyncthreads, 1);
2019 	lwkt_create(device_attach_thread, dev, &td, NULL,
2020 		    0, 0, "%s", (dev->desc ? dev->desc : "devattach"));
2021 }
2022 
2023 static void
2024 device_attach_thread(void *arg)
2025 {
2026 	device_t dev = arg;
2027 
2028 	(void)device_doattach(dev);
2029 	atomic_subtract_int(&numasyncthreads, 1);
2030 	wakeup(&numasyncthreads);
2031 }
2032 
2033 /*
2034  * Device is known to be alive, do the attach (synchronous or asynchronous)
2035  */
2036 static int
2037 device_doattach(device_t dev)
2038 {
2039 	device_t bus = dev->parent;
2040 	int hasclass = (dev->devclass != NULL);
2041 	int error;
2042 
2043 	device_sysctl_init(dev);
2044 	error = DEVICE_ATTACH(dev);
2045 	if (error == 0) {
2046 		dev->state = DS_ATTACHED;
2047 		if (bootverbose && !device_is_quiet(dev))
2048 			device_print_child(bus, dev);
2049 		device_sysctl_update(dev);
2050 		devadded(dev);
2051 	} else {
2052 		kprintf("device_probe_and_attach: %s%d attach returned %d\n",
2053 		       dev->driver->name, dev->unit, error);
2054 		/* Unset the class that was set in device_probe_child */
2055 		if (!hasclass)
2056 			device_set_devclass(dev, 0);
2057 		device_set_driver(dev, NULL);
2058 		dev->state = DS_NOTPRESENT;
2059 		device_sysctl_fini(dev);
2060 	}
2061 	return(error);
2062 }
2063 
2064 int
2065 device_detach(device_t dev)
2066 {
2067 	int error;
2068 
2069 	PDEBUG(("%s", DEVICENAME(dev)));
2070 	if (dev->state == DS_BUSY)
2071 		return(EBUSY);
2072 	if (dev->state != DS_ATTACHED)
2073 		return(0);
2074 
2075 	if ((error = DEVICE_DETACH(dev)) != 0)
2076 		return(error);
2077 	devremoved(dev);
2078 	device_printf(dev, "detached\n");
2079 	if (dev->parent)
2080 		BUS_CHILD_DETACHED(dev->parent, dev);
2081 
2082 	if (!(dev->flags & DF_FIXEDCLASS))
2083 		devclass_delete_device(dev->devclass, dev);
2084 
2085 	dev->state = DS_NOTPRESENT;
2086 	device_set_driver(dev, NULL);
2087 	device_sysctl_fini(dev);
2088 
2089 	return(0);
2090 }
2091 
2092 int
2093 device_shutdown(device_t dev)
2094 {
2095 	if (dev->state < DS_ATTACHED)
2096 		return 0;
2097 	PDEBUG(("%s", DEVICENAME(dev)));
2098 	return DEVICE_SHUTDOWN(dev);
2099 }
2100 
2101 int
2102 device_set_unit(device_t dev, int unit)
2103 {
2104 	devclass_t dc;
2105 	int err;
2106 
2107 	dc = device_get_devclass(dev);
2108 	if (unit < dc->maxunit && dc->devices[unit])
2109 		return(EBUSY);
2110 	err = devclass_delete_device(dc, dev);
2111 	if (err)
2112 		return(err);
2113 	dev->unit = unit;
2114 	err = devclass_add_device(dc, dev);
2115 	if (err)
2116 		return(err);
2117 
2118 	bus_data_generation_update();
2119 	return(0);
2120 }
2121 
2122 /*======================================*/
2123 /*
2124  * Access functions for device resources.
2125  */
2126 
2127 /* Supplied by config(8) in ioconf.c */
2128 extern struct config_device config_devtab[];
2129 extern int devtab_count;
2130 
2131 /* Runtime version */
2132 struct config_device *devtab = config_devtab;
2133 
2134 static int
2135 resource_new_name(const char *name, int unit)
2136 {
2137 	struct config_device *new;
2138 
2139 	new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
2140 		     M_INTWAIT | M_ZERO);
2141 	if (devtab && devtab_count > 0)
2142 		bcopy(devtab, new, devtab_count * sizeof(*new));
2143 	new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
2144 	if (new[devtab_count].name == NULL) {
2145 		kfree(new, M_TEMP);
2146 		return(-1);
2147 	}
2148 	strcpy(new[devtab_count].name, name);
2149 	new[devtab_count].unit = unit;
2150 	new[devtab_count].resource_count = 0;
2151 	new[devtab_count].resources = NULL;
2152 	if (devtab && devtab != config_devtab)
2153 		kfree(devtab, M_TEMP);
2154 	devtab = new;
2155 	return devtab_count++;
2156 }
2157 
2158 static int
2159 resource_new_resname(int j, const char *resname, resource_type type)
2160 {
2161 	struct config_resource *new;
2162 	int i;
2163 
2164 	i = devtab[j].resource_count;
2165 	new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
2166 	if (devtab[j].resources && i > 0)
2167 		bcopy(devtab[j].resources, new, i * sizeof(*new));
2168 	new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
2169 	if (new[i].name == NULL) {
2170 		kfree(new, M_TEMP);
2171 		return(-1);
2172 	}
2173 	strcpy(new[i].name, resname);
2174 	new[i].type = type;
2175 	if (devtab[j].resources)
2176 		kfree(devtab[j].resources, M_TEMP);
2177 	devtab[j].resources = new;
2178 	devtab[j].resource_count = i + 1;
2179 	return(i);
2180 }
2181 
2182 static int
2183 resource_match_string(int i, const char *resname, const char *value)
2184 {
2185 	int j;
2186 	struct config_resource *res;
2187 
2188 	for (j = 0, res = devtab[i].resources;
2189 	     j < devtab[i].resource_count; j++, res++)
2190 		if (!strcmp(res->name, resname)
2191 		    && res->type == RES_STRING
2192 		    && !strcmp(res->u.stringval, value))
2193 			return(j);
2194 	return(-1);
2195 }
2196 
2197 static int
2198 resource_find(const char *name, int unit, const char *resname,
2199 	      struct config_resource **result)
2200 {
2201 	int i, j;
2202 	struct config_resource *res;
2203 
2204 	/*
2205 	 * First check specific instances, then generic.
2206 	 */
2207 	for (i = 0; i < devtab_count; i++) {
2208 		if (devtab[i].unit < 0)
2209 			continue;
2210 		if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2211 			res = devtab[i].resources;
2212 			for (j = 0; j < devtab[i].resource_count; j++, res++)
2213 				if (!strcmp(res->name, resname)) {
2214 					*result = res;
2215 					return(0);
2216 				}
2217 		}
2218 	}
2219 	for (i = 0; i < devtab_count; i++) {
2220 		if (devtab[i].unit >= 0)
2221 			continue;
2222 		/* XXX should this `&& devtab[i].unit == unit' be here? */
2223 		/* XXX if so, then the generic match does nothing */
2224 		if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2225 			res = devtab[i].resources;
2226 			for (j = 0; j < devtab[i].resource_count; j++, res++)
2227 				if (!strcmp(res->name, resname)) {
2228 					*result = res;
2229 					return(0);
2230 				}
2231 		}
2232 	}
2233 	return(ENOENT);
2234 }
2235 
2236 static int
2237 resource_kenv(const char *name, int unit, const char *resname, long *result)
2238 {
2239 	const char *env;
2240 	char buf[64];
2241 
2242 	/*
2243 	 * DragonFly style loader.conf hinting
2244 	 */
2245 	ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2246 	if ((env = kgetenv(buf)) != NULL) {
2247 		*result = strtol(env, NULL, 0);
2248 		return(0);
2249 	}
2250 
2251 	/*
2252 	 * Also support FreeBSD style loader.conf hinting
2253 	 */
2254 	ksnprintf(buf, sizeof(buf), "hint.%s.%d.%s", name, unit, resname);
2255 	if ((env = kgetenv(buf)) != NULL) {
2256 		*result = strtol(env, NULL, 0);
2257 		return(0);
2258 	}
2259 
2260 	return (ENOENT);
2261 }
2262 
2263 int
2264 resource_int_value(const char *name, int unit, const char *resname, int *result)
2265 {
2266 	struct config_resource *res;
2267 	long kvalue = 0;
2268 	int error;
2269 
2270 	if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2271 		*result = (int)kvalue;
2272 		return 0;
2273 	}
2274 	if ((error = resource_find(name, unit, resname, &res)) != 0)
2275 		return(error);
2276 	if (res->type != RES_INT)
2277 		return(EFTYPE);
2278 	*result = res->u.intval;
2279 	return(0);
2280 }
2281 
2282 int
2283 resource_long_value(const char *name, int unit, const char *resname,
2284 		    long *result)
2285 {
2286 	struct config_resource *res;
2287 	long kvalue;
2288 	int error;
2289 
2290 	if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2291 		*result = kvalue;
2292 		return 0;
2293 	}
2294 	if ((error = resource_find(name, unit, resname, &res)) != 0)
2295 		return(error);
2296 	if (res->type != RES_LONG)
2297 		return(EFTYPE);
2298 	*result = res->u.longval;
2299 	return(0);
2300 }
2301 
2302 int
2303 resource_string_value(const char *name, int unit, const char *resname,
2304     const char **result)
2305 {
2306 	int error;
2307 	struct config_resource *res;
2308 	char buf[64];
2309 	const char *env;
2310 
2311 	/*
2312 	 * DragonFly style loader.conf hinting
2313 	 */
2314 	ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2315 	if ((env = kgetenv(buf)) != NULL) {
2316 		*result = env;
2317 		return 0;
2318 	}
2319 
2320 	/*
2321 	 * Also support FreeBSD style loader.conf hinting
2322 	 */
2323 	ksnprintf(buf, sizeof(buf), "hint.%s.%d.%s", name, unit, resname);
2324 	if ((env = kgetenv(buf)) != NULL) {
2325 		*result = env;
2326 		return 0;
2327 	}
2328 
2329 	if ((error = resource_find(name, unit, resname, &res)) != 0)
2330 		return(error);
2331 	if (res->type != RES_STRING)
2332 		return(EFTYPE);
2333 	*result = res->u.stringval;
2334 	return(0);
2335 }
2336 
2337 int
2338 resource_query_string(int i, const char *resname, const char *value)
2339 {
2340 	if (i < 0)
2341 		i = 0;
2342 	else
2343 		i = i + 1;
2344 	for (; i < devtab_count; i++)
2345 		if (resource_match_string(i, resname, value) >= 0)
2346 			return(i);
2347 	return(-1);
2348 }
2349 
2350 int
2351 resource_locate(int i, const char *resname)
2352 {
2353 	if (i < 0)
2354 		i = 0;
2355 	else
2356 		i = i + 1;
2357 	for (; i < devtab_count; i++)
2358 		if (!strcmp(devtab[i].name, resname))
2359 			return(i);
2360 	return(-1);
2361 }
2362 
2363 int
2364 resource_count(void)
2365 {
2366 	return(devtab_count);
2367 }
2368 
2369 char *
2370 resource_query_name(int i)
2371 {
2372 	return(devtab[i].name);
2373 }
2374 
2375 int
2376 resource_query_unit(int i)
2377 {
2378 	return(devtab[i].unit);
2379 }
2380 
2381 static int
2382 resource_create(const char *name, int unit, const char *resname,
2383 		resource_type type, struct config_resource **result)
2384 {
2385 	int i, j;
2386 	struct config_resource *res = NULL;
2387 
2388 	for (i = 0; i < devtab_count; i++)
2389 		if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2390 			res = devtab[i].resources;
2391 			break;
2392 		}
2393 	if (res == NULL) {
2394 		i = resource_new_name(name, unit);
2395 		if (i < 0)
2396 			return(ENOMEM);
2397 		res = devtab[i].resources;
2398 	}
2399 	for (j = 0; j < devtab[i].resource_count; j++, res++)
2400 		if (!strcmp(res->name, resname)) {
2401 			*result = res;
2402 			return(0);
2403 		}
2404 	j = resource_new_resname(i, resname, type);
2405 	if (j < 0)
2406 		return(ENOMEM);
2407 	res = &devtab[i].resources[j];
2408 	*result = res;
2409 	return(0);
2410 }
2411 
2412 int
2413 resource_set_int(const char *name, int unit, const char *resname, int value)
2414 {
2415 	int error;
2416 	struct config_resource *res;
2417 
2418 	error = resource_create(name, unit, resname, RES_INT, &res);
2419 	if (error)
2420 		return(error);
2421 	if (res->type != RES_INT)
2422 		return(EFTYPE);
2423 	res->u.intval = value;
2424 	return(0);
2425 }
2426 
2427 int
2428 resource_set_long(const char *name, int unit, const char *resname, long value)
2429 {
2430 	int error;
2431 	struct config_resource *res;
2432 
2433 	error = resource_create(name, unit, resname, RES_LONG, &res);
2434 	if (error)
2435 		return(error);
2436 	if (res->type != RES_LONG)
2437 		return(EFTYPE);
2438 	res->u.longval = value;
2439 	return(0);
2440 }
2441 
2442 int
2443 resource_set_string(const char *name, int unit, const char *resname,
2444 		    const char *value)
2445 {
2446 	int error;
2447 	struct config_resource *res;
2448 
2449 	error = resource_create(name, unit, resname, RES_STRING, &res);
2450 	if (error)
2451 		return(error);
2452 	if (res->type != RES_STRING)
2453 		return(EFTYPE);
2454 	if (res->u.stringval)
2455 		kfree(res->u.stringval, M_TEMP);
2456 	res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2457 	if (res->u.stringval == NULL)
2458 		return(ENOMEM);
2459 	strcpy(res->u.stringval, value);
2460 	return(0);
2461 }
2462 
2463 static void
2464 resource_cfgload(void *dummy __unused)
2465 {
2466 	struct config_resource *res, *cfgres;
2467 	int i, j;
2468 	int error;
2469 	char *name, *resname;
2470 	int unit;
2471 	resource_type type;
2472 	char *stringval;
2473 	int config_devtab_count;
2474 
2475 	config_devtab_count = devtab_count;
2476 	devtab = NULL;
2477 	devtab_count = 0;
2478 
2479 	for (i = 0; i < config_devtab_count; i++) {
2480 		name = config_devtab[i].name;
2481 		unit = config_devtab[i].unit;
2482 
2483 		for (j = 0; j < config_devtab[i].resource_count; j++) {
2484 			cfgres = config_devtab[i].resources;
2485 			resname = cfgres[j].name;
2486 			type = cfgres[j].type;
2487 			error = resource_create(name, unit, resname, type,
2488 						&res);
2489 			if (error) {
2490 				kprintf("create resource %s%d: error %d\n",
2491 					name, unit, error);
2492 				continue;
2493 			}
2494 			if (res->type != type) {
2495 				kprintf("type mismatch %s%d: %d != %d\n",
2496 					name, unit, res->type, type);
2497 				continue;
2498 			}
2499 			switch (type) {
2500 			case RES_INT:
2501 				res->u.intval = cfgres[j].u.intval;
2502 				break;
2503 			case RES_LONG:
2504 				res->u.longval = cfgres[j].u.longval;
2505 				break;
2506 			case RES_STRING:
2507 				if (res->u.stringval)
2508 					kfree(res->u.stringval, M_TEMP);
2509 				stringval = cfgres[j].u.stringval;
2510 				res->u.stringval = kmalloc(strlen(stringval) + 1,
2511 							  M_TEMP, M_INTWAIT);
2512 				if (res->u.stringval == NULL)
2513 					break;
2514 				strcpy(res->u.stringval, stringval);
2515 				break;
2516 			default:
2517 				panic("unknown resource type %d", type);
2518 			}
2519 		}
2520 	}
2521 }
2522 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0);
2523 
2524 
2525 /*======================================*/
2526 /*
2527  * Some useful method implementations to make life easier for bus drivers.
2528  */
2529 
2530 void
2531 resource_list_init(struct resource_list *rl)
2532 {
2533 	SLIST_INIT(rl);
2534 }
2535 
2536 void
2537 resource_list_free(struct resource_list *rl)
2538 {
2539 	struct resource_list_entry *rle;
2540 
2541 	while ((rle = SLIST_FIRST(rl)) != NULL) {
2542 		if (rle->res)
2543 			panic("resource_list_free: resource entry is busy");
2544 		SLIST_REMOVE_HEAD(rl, link);
2545 		kfree(rle, M_BUS);
2546 	}
2547 }
2548 
2549 void
2550 resource_list_add(struct resource_list *rl, int type, int rid,
2551     u_long start, u_long end, u_long count, int cpuid)
2552 {
2553 	struct resource_list_entry *rle;
2554 
2555 	rle = resource_list_find(rl, type, rid);
2556 	if (rle == NULL) {
2557 		rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2558 			     M_INTWAIT);
2559 		SLIST_INSERT_HEAD(rl, rle, link);
2560 		rle->type = type;
2561 		rle->rid = rid;
2562 		rle->res = NULL;
2563 		rle->cpuid = -1;
2564 	}
2565 
2566 	if (rle->res)
2567 		panic("resource_list_add: resource entry is busy");
2568 
2569 	rle->start = start;
2570 	rle->end = end;
2571 	rle->count = count;
2572 
2573 	if (cpuid != -1) {
2574 		if (rle->cpuid != -1 && rle->cpuid != cpuid) {
2575 			panic("resource_list_add: moving from cpu%d -> cpu%d",
2576 			    rle->cpuid, cpuid);
2577 		}
2578 		rle->cpuid = cpuid;
2579 	}
2580 }
2581 
2582 struct resource_list_entry*
2583 resource_list_find(struct resource_list *rl,
2584 		   int type, int rid)
2585 {
2586 	struct resource_list_entry *rle;
2587 
2588 	SLIST_FOREACH(rle, rl, link)
2589 		if (rle->type == type && rle->rid == rid)
2590 			return(rle);
2591 	return(NULL);
2592 }
2593 
2594 void
2595 resource_list_delete(struct resource_list *rl,
2596 		     int type, int rid)
2597 {
2598 	struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2599 
2600 	if (rle) {
2601 		if (rle->res != NULL)
2602 			panic("resource_list_delete: resource has not been released");
2603 		SLIST_REMOVE(rl, rle, resource_list_entry, link);
2604 		kfree(rle, M_BUS);
2605 	}
2606 }
2607 
2608 struct resource *
2609 resource_list_alloc(struct resource_list *rl,
2610 		    device_t bus, device_t child,
2611 		    int type, int *rid,
2612 		    u_long start, u_long end,
2613 		    u_long count, u_int flags, int cpuid)
2614 {
2615 	struct resource_list_entry *rle = NULL;
2616 	int passthrough = (device_get_parent(child) != bus);
2617 	int isdefault = (start == 0UL && end == ~0UL);
2618 
2619 	if (passthrough) {
2620 		return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2621 					  type, rid,
2622 					  start, end, count, flags, cpuid));
2623 	}
2624 
2625 	rle = resource_list_find(rl, type, *rid);
2626 
2627 	if (!rle)
2628 		return(0);		/* no resource of that type/rid */
2629 
2630 	if (rle->res)
2631 		panic("resource_list_alloc: resource entry is busy");
2632 
2633 	if (isdefault) {
2634 		start = rle->start;
2635 		count = max(count, rle->count);
2636 		end = max(rle->end, start + count - 1);
2637 	}
2638 	cpuid = rle->cpuid;
2639 
2640 	rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2641 				      type, rid, start, end, count,
2642 				      flags, cpuid);
2643 
2644 	/*
2645 	 * Record the new range.
2646 	 */
2647 	if (rle->res) {
2648 		rle->start = rman_get_start(rle->res);
2649 		rle->end = rman_get_end(rle->res);
2650 		rle->count = count;
2651 	}
2652 
2653 	return(rle->res);
2654 }
2655 
2656 int
2657 resource_list_release(struct resource_list *rl,
2658 		      device_t bus, device_t child,
2659 		      int type, int rid, struct resource *res)
2660 {
2661 	struct resource_list_entry *rle = NULL;
2662 	int passthrough = (device_get_parent(child) != bus);
2663 	int error;
2664 
2665 	if (passthrough) {
2666 		return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2667 					    type, rid, res));
2668 	}
2669 
2670 	rle = resource_list_find(rl, type, rid);
2671 
2672 	if (!rle)
2673 		panic("resource_list_release: can't find resource");
2674 	if (!rle->res)
2675 		panic("resource_list_release: resource entry is not busy");
2676 
2677 	error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2678 				     type, rid, res);
2679 	if (error)
2680 		return(error);
2681 
2682 	rle->res = NULL;
2683 	return(0);
2684 }
2685 
2686 int
2687 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2688 			 const char *format)
2689 {
2690 	struct resource_list_entry *rle;
2691 	int printed, retval;
2692 
2693 	printed = 0;
2694 	retval = 0;
2695 	/* Yes, this is kinda cheating */
2696 	SLIST_FOREACH(rle, rl, link) {
2697 		if (rle->type == type) {
2698 			if (printed == 0)
2699 				retval += kprintf(" %s ", name);
2700 			else
2701 				retval += kprintf(",");
2702 			printed++;
2703 			retval += kprintf(format, rle->start);
2704 			if (rle->count > 1) {
2705 				retval += kprintf("-");
2706 				retval += kprintf(format, rle->start +
2707 						 rle->count - 1);
2708 			}
2709 		}
2710 	}
2711 	return(retval);
2712 }
2713 
2714 /*
2715  * Generic driver/device identify functions.  These will install a device
2716  * rendezvous point under the parent using the same name as the driver
2717  * name, which will at a later time be probed and attached.
2718  *
2719  * These functions are used when the parent does not 'scan' its bus for
2720  * matching devices, or for the particular devices using these functions,
2721  * or when the device is a pseudo or synthesized device (such as can be
2722  * found under firewire and ppbus).
2723  */
2724 int
2725 bus_generic_identify(driver_t *driver, device_t parent)
2726 {
2727 	if (parent->state == DS_ATTACHED)
2728 		return (0);
2729 	BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2730 	return (0);
2731 }
2732 
2733 int
2734 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2735 {
2736 	if (parent->state == DS_ATTACHED)
2737 		return (0);
2738 	BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2739 	return (0);
2740 }
2741 
2742 /*
2743  * Call DEVICE_IDENTIFY for each driver.
2744  */
2745 int
2746 bus_generic_probe(device_t dev)
2747 {
2748 	devclass_t dc = dev->devclass;
2749 	driverlink_t dl;
2750 
2751 	TAILQ_FOREACH(dl, &dc->drivers, link) {
2752 		DEVICE_IDENTIFY(dl->driver, dev);
2753 	}
2754 
2755 	return(0);
2756 }
2757 
2758 /*
2759  * This is an aweful hack due to the isa bus and autoconf code not
2760  * probing the ISA devices until after everything else has configured.
2761  * The ISA bus did a dummy attach long ago so we have to set it back
2762  * to an earlier state so the probe thinks its the initial probe and
2763  * not a bus rescan.
2764  *
2765  * XXX remove by properly defering the ISA bus scan.
2766  */
2767 int
2768 bus_generic_probe_hack(device_t dev)
2769 {
2770 	if (dev->state == DS_ATTACHED) {
2771 		dev->state = DS_ALIVE;
2772 		bus_generic_probe(dev);
2773 		dev->state = DS_ATTACHED;
2774 	}
2775 	return (0);
2776 }
2777 
2778 int
2779 bus_generic_attach(device_t dev)
2780 {
2781 	device_t child;
2782 
2783 	TAILQ_FOREACH(child, &dev->children, link) {
2784 		device_probe_and_attach(child);
2785 	}
2786 
2787 	return(0);
2788 }
2789 
2790 int
2791 bus_generic_attach_gpri(device_t dev, u_int gpri)
2792 {
2793 	device_t child;
2794 
2795 	TAILQ_FOREACH(child, &dev->children, link) {
2796 		device_probe_and_attach_gpri(child, gpri);
2797 	}
2798 
2799 	return(0);
2800 }
2801 
2802 int
2803 bus_generic_detach(device_t dev)
2804 {
2805 	device_t child;
2806 	int error;
2807 
2808 	if (dev->state != DS_ATTACHED)
2809 		return(EBUSY);
2810 
2811 	TAILQ_FOREACH(child, &dev->children, link)
2812 		if ((error = device_detach(child)) != 0)
2813 			return(error);
2814 
2815 	return 0;
2816 }
2817 
2818 int
2819 bus_generic_shutdown(device_t dev)
2820 {
2821 	device_t child;
2822 
2823 	TAILQ_FOREACH(child, &dev->children, link)
2824 		device_shutdown(child);
2825 
2826 	return(0);
2827 }
2828 
2829 int
2830 bus_generic_suspend(device_t dev)
2831 {
2832 	int error;
2833 	device_t child, child2;
2834 
2835 	TAILQ_FOREACH(child, &dev->children, link) {
2836 		error = DEVICE_SUSPEND(child);
2837 		if (error) {
2838 			for (child2 = TAILQ_FIRST(&dev->children);
2839 			     child2 && child2 != child;
2840 			     child2 = TAILQ_NEXT(child2, link))
2841 				DEVICE_RESUME(child2);
2842 			return(error);
2843 		}
2844 	}
2845 	return(0);
2846 }
2847 
2848 int
2849 bus_generic_resume(device_t dev)
2850 {
2851 	device_t child;
2852 
2853 	TAILQ_FOREACH(child, &dev->children, link)
2854 		DEVICE_RESUME(child);
2855 		/* if resume fails, there's nothing we can usefully do... */
2856 
2857 	return(0);
2858 }
2859 
2860 int
2861 bus_print_child_header(device_t dev, device_t child)
2862 {
2863 	int retval = 0;
2864 
2865 	if (device_get_desc(child))
2866 		retval += device_printf(child, "<%s>", device_get_desc(child));
2867 	else
2868 		retval += kprintf("%s", device_get_nameunit(child));
2869 	if (bootverbose) {
2870 		if (child->state != DS_ATTACHED)
2871 			kprintf(" [tentative]");
2872 		else
2873 			kprintf(" [attached!]");
2874 	}
2875 	return(retval);
2876 }
2877 
2878 int
2879 bus_print_child_footer(device_t dev, device_t child)
2880 {
2881 	return(kprintf(" on %s\n", device_get_nameunit(dev)));
2882 }
2883 
2884 device_t
2885 bus_generic_add_child(device_t dev, device_t child, int order,
2886 		      const char *name, int unit)
2887 {
2888 	if (dev->parent)
2889 		dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2890 	else
2891 		dev = device_add_child_ordered(child, order, name, unit);
2892 	return(dev);
2893 
2894 }
2895 
2896 int
2897 bus_generic_print_child(device_t dev, device_t child)
2898 {
2899 	int retval = 0;
2900 
2901 	retval += bus_print_child_header(dev, child);
2902 	retval += bus_print_child_footer(dev, child);
2903 
2904 	return(retval);
2905 }
2906 
2907 int
2908 bus_generic_read_ivar(device_t dev, device_t child, int index,
2909 		      uintptr_t * result)
2910 {
2911 	int error;
2912 
2913 	if (dev->parent)
2914 		error = BUS_READ_IVAR(dev->parent, child, index, result);
2915 	else
2916 		error = ENOENT;
2917 	return (error);
2918 }
2919 
2920 int
2921 bus_generic_write_ivar(device_t dev, device_t child, int index,
2922 		       uintptr_t value)
2923 {
2924 	int error;
2925 
2926 	if (dev->parent)
2927 		error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2928 	else
2929 		error = ENOENT;
2930 	return (error);
2931 }
2932 
2933 /*
2934  * Resource list are used for iterations, do not recurse.
2935  */
2936 struct resource_list *
2937 bus_generic_get_resource_list(device_t dev, device_t child)
2938 {
2939 	return (NULL);
2940 }
2941 
2942 void
2943 bus_generic_driver_added(device_t dev, driver_t *driver)
2944 {
2945 	device_t child;
2946 
2947 	DEVICE_IDENTIFY(driver, dev);
2948 	TAILQ_FOREACH(child, &dev->children, link) {
2949 		if (child->state == DS_NOTPRESENT)
2950 			device_probe_and_attach(child);
2951 	}
2952 }
2953 
2954 int
2955 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2956     int flags, driver_intr_t *intr, void *arg, void **cookiep,
2957     lwkt_serialize_t serializer, const char *desc)
2958 {
2959 	/* Propagate up the bus hierarchy until someone handles it. */
2960 	if (dev->parent) {
2961 		return BUS_SETUP_INTR(dev->parent, child, irq, flags,
2962 		    intr, arg, cookiep, serializer, desc);
2963 	} else {
2964 		return EINVAL;
2965 	}
2966 }
2967 
2968 int
2969 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2970 			  void *cookie)
2971 {
2972 	/* Propagate up the bus hierarchy until someone handles it. */
2973 	if (dev->parent)
2974 		return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2975 	else
2976 		return(EINVAL);
2977 }
2978 
2979 int
2980 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2981 {
2982 	if (dev->parent)
2983 		return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2984 	else
2985 		return(0);
2986 }
2987 
2988 void
2989 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2990 {
2991 	if (dev->parent)
2992 		BUS_ENABLE_INTR(dev->parent, child, cookie);
2993 }
2994 
2995 int
2996 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
2997     enum intr_polarity pol)
2998 {
2999 	/* Propagate up the bus hierarchy until someone handles it. */
3000 	if (dev->parent)
3001 		return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
3002 	else
3003 		return(EINVAL);
3004 }
3005 
3006 struct resource *
3007 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3008     u_long start, u_long end, u_long count, u_int flags, int cpuid)
3009 {
3010 	/* Propagate up the bus hierarchy until someone handles it. */
3011 	if (dev->parent)
3012 		return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3013 					   start, end, count, flags, cpuid));
3014 	else
3015 		return(NULL);
3016 }
3017 
3018 int
3019 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3020 			     struct resource *r)
3021 {
3022 	/* Propagate up the bus hierarchy until someone handles it. */
3023 	if (dev->parent)
3024 		return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
3025 	else
3026 		return(EINVAL);
3027 }
3028 
3029 int
3030 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3031 			      struct resource *r)
3032 {
3033 	/* Propagate up the bus hierarchy until someone handles it. */
3034 	if (dev->parent)
3035 		return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
3036 	else
3037 		return(EINVAL);
3038 }
3039 
3040 int
3041 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3042 				int rid, struct resource *r)
3043 {
3044 	/* Propagate up the bus hierarchy until someone handles it. */
3045 	if (dev->parent)
3046 		return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3047 					       r));
3048 	else
3049 		return(EINVAL);
3050 }
3051 
3052 int
3053 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
3054 			 u_long *startp, u_long *countp)
3055 {
3056 	int error;
3057 
3058 	error = ENOENT;
3059 	if (dev->parent) {
3060 		error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
3061 					 startp, countp);
3062 	}
3063 	return (error);
3064 }
3065 
3066 int
3067 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
3068 			u_long start, u_long count, int cpuid)
3069 {
3070 	int error;
3071 
3072 	error = EINVAL;
3073 	if (dev->parent) {
3074 		error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
3075 					 start, count, cpuid);
3076 	}
3077 	return (error);
3078 }
3079 
3080 void
3081 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
3082 {
3083 	if (dev->parent)
3084 		BUS_DELETE_RESOURCE(dev, child, type, rid);
3085 }
3086 
3087 /**
3088  * @brief Helper function for implementing BUS_GET_DMA_TAG().
3089  *
3090  * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3091  * BUS_GET_DMA_TAG() method of the parent of @p dev.
3092  */
3093 bus_dma_tag_t
3094 bus_generic_get_dma_tag(device_t dev, device_t child)
3095 {
3096 
3097 	/* Propagate up the bus hierarchy until someone handles it. */
3098 	if (dev->parent != NULL)
3099 		return (BUS_GET_DMA_TAG(dev->parent, child));
3100 	return (NULL);
3101 }
3102 
3103 int
3104 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3105     u_long *startp, u_long *countp)
3106 {
3107 	struct resource_list *rl = NULL;
3108 	struct resource_list_entry *rle = NULL;
3109 
3110 	rl = BUS_GET_RESOURCE_LIST(dev, child);
3111 	if (!rl)
3112 		return(EINVAL);
3113 
3114 	rle = resource_list_find(rl, type, rid);
3115 	if (!rle)
3116 		return(ENOENT);
3117 
3118 	if (startp)
3119 		*startp = rle->start;
3120 	if (countp)
3121 		*countp = rle->count;
3122 
3123 	return(0);
3124 }
3125 
3126 int
3127 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3128     u_long start, u_long count, int cpuid)
3129 {
3130 	struct resource_list *rl = NULL;
3131 
3132 	rl = BUS_GET_RESOURCE_LIST(dev, child);
3133 	if (!rl)
3134 		return(EINVAL);
3135 
3136 	resource_list_add(rl, type, rid, start, (start + count - 1), count,
3137 	    cpuid);
3138 
3139 	return(0);
3140 }
3141 
3142 void
3143 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3144 {
3145 	struct resource_list *rl = NULL;
3146 
3147 	rl = BUS_GET_RESOURCE_LIST(dev, child);
3148 	if (!rl)
3149 		return;
3150 
3151 	resource_list_delete(rl, type, rid);
3152 }
3153 
3154 int
3155 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3156     int rid, struct resource *r)
3157 {
3158 	struct resource_list *rl = NULL;
3159 
3160 	rl = BUS_GET_RESOURCE_LIST(dev, child);
3161 	if (!rl)
3162 		return(EINVAL);
3163 
3164 	return(resource_list_release(rl, dev, child, type, rid, r));
3165 }
3166 
3167 struct resource *
3168 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3169     int *rid, u_long start, u_long end, u_long count, u_int flags, int cpuid)
3170 {
3171 	struct resource_list *rl = NULL;
3172 
3173 	rl = BUS_GET_RESOURCE_LIST(dev, child);
3174 	if (!rl)
3175 		return(NULL);
3176 
3177 	return(resource_list_alloc(rl, dev, child, type, rid,
3178 	    start, end, count, flags, cpuid));
3179 }
3180 
3181 int
3182 bus_generic_child_present(device_t bus, device_t child)
3183 {
3184 	return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
3185 }
3186 
3187 
3188 /*
3189  * Some convenience functions to make it easier for drivers to use the
3190  * resource-management functions.  All these really do is hide the
3191  * indirection through the parent's method table, making for slightly
3192  * less-wordy code.  In the future, it might make sense for this code
3193  * to maintain some sort of a list of resources allocated by each device.
3194  */
3195 int
3196 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3197     struct resource **res)
3198 {
3199 	int i;
3200 
3201 	for (i = 0; rs[i].type != -1; i++)
3202 	        res[i] = NULL;
3203 	for (i = 0; rs[i].type != -1; i++) {
3204 		res[i] = bus_alloc_resource_any(dev,
3205 		    rs[i].type, &rs[i].rid, rs[i].flags);
3206 		if (res[i] == NULL) {
3207 			bus_release_resources(dev, rs, res);
3208 			return (ENXIO);
3209 		}
3210 	}
3211 	return (0);
3212 }
3213 
3214 void
3215 bus_release_resources(device_t dev, const struct resource_spec *rs,
3216     struct resource **res)
3217 {
3218 	int i;
3219 
3220 	for (i = 0; rs[i].type != -1; i++)
3221 		if (res[i] != NULL) {
3222 			bus_release_resource(
3223 			    dev, rs[i].type, rs[i].rid, res[i]);
3224 			res[i] = NULL;
3225 		}
3226 }
3227 
3228 struct resource *
3229 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3230 		   u_long count, u_int flags)
3231 {
3232 	if (dev->parent == NULL)
3233 		return(0);
3234 	return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3235 				  count, flags, -1));
3236 }
3237 
3238 struct resource *
3239 bus_alloc_legacy_irq_resource(device_t dev, int *rid, u_long irq, u_int flags)
3240 {
3241 	if (dev->parent == NULL)
3242 		return(0);
3243 	return BUS_ALLOC_RESOURCE(dev->parent, dev, SYS_RES_IRQ, rid,
3244 	    irq, irq, 1, flags, machintr_legacy_intr_cpuid(irq));
3245 }
3246 
3247 int
3248 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3249 {
3250 	if (dev->parent == NULL)
3251 		return(EINVAL);
3252 	return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3253 }
3254 
3255 int
3256 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3257 {
3258 	if (dev->parent == NULL)
3259 		return(EINVAL);
3260 	return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3261 }
3262 
3263 int
3264 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3265 {
3266 	if (dev->parent == NULL)
3267 		return(EINVAL);
3268 	return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3269 }
3270 
3271 int
3272 bus_setup_intr_descr(device_t dev, struct resource *r, int flags,
3273     driver_intr_t handler, void *arg, void **cookiep,
3274     lwkt_serialize_t serializer, const char *desc)
3275 {
3276 	if (dev->parent == NULL)
3277 		return EINVAL;
3278 	return BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
3279 	    cookiep, serializer, desc);
3280 }
3281 
3282 int
3283 bus_setup_intr(device_t dev, struct resource *r, int flags,
3284     driver_intr_t handler, void *arg, void **cookiep,
3285     lwkt_serialize_t serializer)
3286 {
3287 	return bus_setup_intr_descr(dev, r, flags, handler, arg, cookiep,
3288 	    serializer, NULL);
3289 }
3290 
3291 int
3292 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3293 {
3294 	if (dev->parent == NULL)
3295 		return(EINVAL);
3296 	return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3297 }
3298 
3299 void
3300 bus_enable_intr(device_t dev, void *cookie)
3301 {
3302 	if (dev->parent)
3303 		BUS_ENABLE_INTR(dev->parent, dev, cookie);
3304 }
3305 
3306 int
3307 bus_disable_intr(device_t dev, void *cookie)
3308 {
3309 	if (dev->parent)
3310 		return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
3311 	else
3312 		return(0);
3313 }
3314 
3315 int
3316 bus_set_resource(device_t dev, int type, int rid,
3317 		 u_long start, u_long count, int cpuid)
3318 {
3319 	return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3320 				start, count, cpuid));
3321 }
3322 
3323 int
3324 bus_get_resource(device_t dev, int type, int rid,
3325 		 u_long *startp, u_long *countp)
3326 {
3327 	return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3328 				startp, countp));
3329 }
3330 
3331 u_long
3332 bus_get_resource_start(device_t dev, int type, int rid)
3333 {
3334 	u_long start, count;
3335 	int error;
3336 
3337 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3338 				 &start, &count);
3339 	if (error)
3340 		return(0);
3341 	return(start);
3342 }
3343 
3344 u_long
3345 bus_get_resource_count(device_t dev, int type, int rid)
3346 {
3347 	u_long start, count;
3348 	int error;
3349 
3350 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3351 				 &start, &count);
3352 	if (error)
3353 		return(0);
3354 	return(count);
3355 }
3356 
3357 void
3358 bus_delete_resource(device_t dev, int type, int rid)
3359 {
3360 	BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3361 }
3362 
3363 int
3364 bus_child_present(device_t child)
3365 {
3366 	return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3367 }
3368 
3369 int
3370 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3371 {
3372 	device_t parent;
3373 
3374 	parent = device_get_parent(child);
3375 	if (parent == NULL) {
3376 		*buf = '\0';
3377 		return (0);
3378 	}
3379 	return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3380 }
3381 
3382 int
3383 bus_child_location_str(device_t child, char *buf, size_t buflen)
3384 {
3385 	device_t parent;
3386 
3387 	parent = device_get_parent(child);
3388 	if (parent == NULL) {
3389 		*buf = '\0';
3390 		return (0);
3391 	}
3392 	return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3393 }
3394 
3395 /**
3396  * @brief Wrapper function for BUS_GET_DMA_TAG().
3397  *
3398  * This function simply calls the BUS_GET_DMA_TAG() method of the
3399  * parent of @p dev.
3400  */
3401 bus_dma_tag_t
3402 bus_get_dma_tag(device_t dev)
3403 {
3404 	device_t parent;
3405 
3406 	parent = device_get_parent(dev);
3407 	if (parent == NULL)
3408 		return (NULL);
3409 	return (BUS_GET_DMA_TAG(parent, dev));
3410 }
3411 
3412 static int
3413 root_print_child(device_t dev, device_t child)
3414 {
3415 	return(0);
3416 }
3417 
3418 static int
3419 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3420 		void **cookiep, lwkt_serialize_t serializer, const char *desc)
3421 {
3422 	/*
3423 	 * If an interrupt mapping gets to here something bad has happened.
3424 	 */
3425 	panic("root_setup_intr");
3426 }
3427 
3428 /*
3429  * If we get here, assume that the device is permanant and really is
3430  * present in the system.  Removable bus drivers are expected to intercept
3431  * this call long before it gets here.  We return -1 so that drivers that
3432  * really care can check vs -1 or some ERRNO returned higher in the food
3433  * chain.
3434  */
3435 static int
3436 root_child_present(device_t dev, device_t child)
3437 {
3438 	return(-1);
3439 }
3440 
3441 /*
3442  * XXX NOTE! other defaults may be set in bus_if.m
3443  */
3444 static kobj_method_t root_methods[] = {
3445 	/* Device interface */
3446 	KOBJMETHOD(device_shutdown,	bus_generic_shutdown),
3447 	KOBJMETHOD(device_suspend,	bus_generic_suspend),
3448 	KOBJMETHOD(device_resume,	bus_generic_resume),
3449 
3450 	/* Bus interface */
3451 	KOBJMETHOD(bus_add_child,	bus_generic_add_child),
3452 	KOBJMETHOD(bus_print_child,	root_print_child),
3453 	KOBJMETHOD(bus_read_ivar,	bus_generic_read_ivar),
3454 	KOBJMETHOD(bus_write_ivar,	bus_generic_write_ivar),
3455 	KOBJMETHOD(bus_setup_intr,	root_setup_intr),
3456 	KOBJMETHOD(bus_child_present,   root_child_present),
3457 
3458 	KOBJMETHOD_END
3459 };
3460 
3461 static driver_t root_driver = {
3462 	"root",
3463 	root_methods,
3464 	1,			/* no softc */
3465 };
3466 
3467 device_t	root_bus;
3468 devclass_t	root_devclass;
3469 
3470 static int
3471 root_bus_module_handler(module_t mod, int what, void* arg)
3472 {
3473 	switch (what) {
3474 	case MOD_LOAD:
3475 		TAILQ_INIT(&bus_data_devices);
3476 		root_bus = make_device(NULL, "root", 0);
3477 		root_bus->desc = "System root bus";
3478 		kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3479 		root_bus->driver = &root_driver;
3480 		root_bus->state = DS_ALIVE;
3481 		root_devclass = devclass_find_internal("root", NULL, FALSE);
3482 		devinit();
3483 		return(0);
3484 
3485 	case MOD_SHUTDOWN:
3486 		device_shutdown(root_bus);
3487 		return(0);
3488 	default:
3489 		return(0);
3490 	}
3491 }
3492 
3493 static moduledata_t root_bus_mod = {
3494 	"rootbus",
3495 	root_bus_module_handler,
3496 	0
3497 };
3498 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3499 
3500 void
3501 root_bus_configure(void)
3502 {
3503 	int warncount;
3504 	device_t dev;
3505 
3506 	PDEBUG(("."));
3507 
3508 	/*
3509 	 * handle device_identify based device attachments to the root_bus
3510 	 * (typically nexus).
3511 	 */
3512 	bus_generic_probe(root_bus);
3513 
3514 	/*
3515 	 * Probe and attach the devices under root_bus.
3516 	 */
3517 	TAILQ_FOREACH(dev, &root_bus->children, link) {
3518 		device_probe_and_attach(dev);
3519 	}
3520 
3521 	/*
3522 	 * Wait for all asynchronous attaches to complete.  If we don't
3523 	 * our legacy ISA bus scan could steal device unit numbers or
3524 	 * even I/O ports.
3525 	 */
3526 	warncount = 10;
3527 	if (numasyncthreads)
3528 		kprintf("Waiting for async drivers to attach\n");
3529 	while (numasyncthreads > 0) {
3530 		if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3531 			--warncount;
3532 		if (warncount == 0) {
3533 			kprintf("Warning: Still waiting for %d "
3534 				"drivers to attach\n", numasyncthreads);
3535 		} else if (warncount == -30) {
3536 			kprintf("Giving up on %d drivers\n", numasyncthreads);
3537 			break;
3538 		}
3539 	}
3540 	root_bus->state = DS_ATTACHED;
3541 }
3542 
3543 int
3544 driver_module_handler(module_t mod, int what, void *arg)
3545 {
3546 	int error;
3547 	struct driver_module_data *dmd;
3548 	devclass_t bus_devclass;
3549 	kobj_class_t driver;
3550         const char *parentname;
3551 
3552 	dmd = (struct driver_module_data *)arg;
3553 	bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3554 	error = 0;
3555 
3556 	switch (what) {
3557 	case MOD_LOAD:
3558 		if (dmd->dmd_chainevh)
3559 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3560 
3561 		driver = dmd->dmd_driver;
3562 		PDEBUG(("Loading module: driver %s on bus %s",
3563 		        DRIVERNAME(driver), dmd->dmd_busname));
3564 
3565 		/*
3566 		 * If the driver has any base classes, make the
3567 		 * devclass inherit from the devclass of the driver's
3568 		 * first base class. This will allow the system to
3569 		 * search for drivers in both devclasses for children
3570 		 * of a device using this driver.
3571 		 */
3572 		if (driver->baseclasses)
3573 			parentname = driver->baseclasses[0]->name;
3574 		else
3575 			parentname = NULL;
3576 		*dmd->dmd_devclass = devclass_find_internal(driver->name,
3577 							    parentname, TRUE);
3578 
3579 		error = devclass_add_driver(bus_devclass, driver);
3580 		if (error)
3581 			break;
3582 		break;
3583 
3584 	case MOD_UNLOAD:
3585 		PDEBUG(("Unloading module: driver %s from bus %s",
3586 			DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3587 		error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3588 
3589 		if (!error && dmd->dmd_chainevh)
3590 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3591 		break;
3592 	}
3593 
3594 	return (error);
3595 }
3596 
3597 #ifdef BUS_DEBUG
3598 
3599 /*
3600  * The _short versions avoid iteration by not calling anything that prints
3601  * more than oneliners. I love oneliners.
3602  */
3603 
3604 static void
3605 print_device_short(device_t dev, int indent)
3606 {
3607 	if (!dev)
3608 		return;
3609 
3610 	indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3611 		      dev->unit, dev->desc,
3612 		      (dev->parent? "":"no "),
3613 		      (TAILQ_EMPTY(&dev->children)? "no ":""),
3614 		      (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3615 		      (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3616 		      (dev->flags&DF_WILDCARD? "wildcard,":""),
3617 		      (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3618 		      (dev->ivars? "":"no "),
3619 		      (dev->softc? "":"no "),
3620 		      dev->busy));
3621 }
3622 
3623 static void
3624 print_device(device_t dev, int indent)
3625 {
3626 	if (!dev)
3627 		return;
3628 
3629 	print_device_short(dev, indent);
3630 
3631 	indentprintf(("Parent:\n"));
3632 	print_device_short(dev->parent, indent+1);
3633 	indentprintf(("Driver:\n"));
3634 	print_driver_short(dev->driver, indent+1);
3635 	indentprintf(("Devclass:\n"));
3636 	print_devclass_short(dev->devclass, indent+1);
3637 }
3638 
3639 /*
3640  * Print the device and all its children (indented).
3641  */
3642 void
3643 print_device_tree_short(device_t dev, int indent)
3644 {
3645 	device_t child;
3646 
3647 	if (!dev)
3648 		return;
3649 
3650 	print_device_short(dev, indent);
3651 
3652 	TAILQ_FOREACH(child, &dev->children, link)
3653 		print_device_tree_short(child, indent+1);
3654 }
3655 
3656 /*
3657  * Print the device and all its children (indented).
3658  */
3659 void
3660 print_device_tree(device_t dev, int indent)
3661 {
3662 	device_t child;
3663 
3664 	if (!dev)
3665 		return;
3666 
3667 	print_device(dev, indent);
3668 
3669 	TAILQ_FOREACH(child, &dev->children, link)
3670 		print_device_tree(child, indent+1);
3671 }
3672 
3673 static void
3674 print_driver_short(driver_t *driver, int indent)
3675 {
3676 	if (!driver)
3677 		return;
3678 
3679 	indentprintf(("driver %s: softc size = %zu\n",
3680 		      driver->name, driver->size));
3681 }
3682 
3683 static void
3684 print_driver(driver_t *driver, int indent)
3685 {
3686 	if (!driver)
3687 		return;
3688 
3689 	print_driver_short(driver, indent);
3690 }
3691 
3692 
3693 static void
3694 print_driver_list(driver_list_t drivers, int indent)
3695 {
3696 	driverlink_t driver;
3697 
3698 	TAILQ_FOREACH(driver, &drivers, link)
3699 		print_driver(driver->driver, indent);
3700 }
3701 
3702 static void
3703 print_devclass_short(devclass_t dc, int indent)
3704 {
3705 	if (!dc)
3706 		return;
3707 
3708 	indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3709 }
3710 
3711 static void
3712 print_devclass(devclass_t dc, int indent)
3713 {
3714 	int i;
3715 
3716 	if (!dc)
3717 		return;
3718 
3719 	print_devclass_short(dc, indent);
3720 	indentprintf(("Drivers:\n"));
3721 	print_driver_list(dc->drivers, indent+1);
3722 
3723 	indentprintf(("Devices:\n"));
3724 	for (i = 0; i < dc->maxunit; i++)
3725 		if (dc->devices[i])
3726 			print_device(dc->devices[i], indent+1);
3727 }
3728 
3729 void
3730 print_devclass_list_short(void)
3731 {
3732 	devclass_t dc;
3733 
3734 	kprintf("Short listing of devclasses, drivers & devices:\n");
3735 	TAILQ_FOREACH(dc, &devclasses, link) {
3736 		print_devclass_short(dc, 0);
3737 	}
3738 }
3739 
3740 void
3741 print_devclass_list(void)
3742 {
3743 	devclass_t dc;
3744 
3745 	kprintf("Full listing of devclasses, drivers & devices:\n");
3746 	TAILQ_FOREACH(dc, &devclasses, link) {
3747 		print_devclass(dc, 0);
3748 	}
3749 }
3750 
3751 #endif
3752 
3753 /*
3754  * Check to see if a device is disabled via a disabled hint.
3755  */
3756 int
3757 resource_disabled(const char *name, int unit)
3758 {
3759 	int error, value;
3760 
3761 	error = resource_int_value(name, unit, "disabled", &value);
3762 	if (error)
3763 	       return(0);
3764 	return(value);
3765 }
3766 
3767 /*
3768  * User-space access to the device tree.
3769  *
3770  * We implement a small set of nodes:
3771  *
3772  * hw.bus			Single integer read method to obtain the
3773  *				current generation count.
3774  * hw.bus.devices		Reads the entire device tree in flat space.
3775  * hw.bus.rman			Resource manager interface
3776  *
3777  * We might like to add the ability to scan devclasses and/or drivers to
3778  * determine what else is currently loaded/available.
3779  */
3780 
3781 static int
3782 sysctl_bus(SYSCTL_HANDLER_ARGS)
3783 {
3784 	struct u_businfo	ubus;
3785 
3786 	ubus.ub_version = BUS_USER_VERSION;
3787 	ubus.ub_generation = bus_data_generation;
3788 
3789 	return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3790 }
3791 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3792     "bus-related data");
3793 
3794 static int
3795 sysctl_devices(SYSCTL_HANDLER_ARGS)
3796 {
3797 	int			*name = (int *)arg1;
3798 	u_int			namelen = arg2;
3799 	int			index;
3800 	device_t		dev;
3801 	struct u_device		udev;	/* XXX this is a bit big */
3802 	int			error;
3803 
3804 	if (namelen != 2)
3805 		return (EINVAL);
3806 
3807 	if (bus_data_generation_check(name[0]))
3808 		return (EINVAL);
3809 
3810 	index = name[1];
3811 
3812 	/*
3813 	 * Scan the list of devices, looking for the requested index.
3814 	 */
3815 	TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3816 		if (index-- == 0)
3817 			break;
3818 	}
3819 	if (dev == NULL)
3820 		return (ENOENT);
3821 
3822 	/*
3823 	 * Populate the return array.
3824 	 */
3825 	bzero(&udev, sizeof(udev));
3826 	udev.dv_handle = (uintptr_t)dev;
3827 	udev.dv_parent = (uintptr_t)dev->parent;
3828 	if (dev->nameunit != NULL)
3829 		strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3830 	if (dev->desc != NULL)
3831 		strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3832 	if (dev->driver != NULL && dev->driver->name != NULL)
3833 		strlcpy(udev.dv_drivername, dev->driver->name,
3834 		    sizeof(udev.dv_drivername));
3835 	bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3836 	bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3837 	udev.dv_devflags = dev->devflags;
3838 	udev.dv_flags = dev->flags;
3839 	udev.dv_state = dev->state;
3840 	error = SYSCTL_OUT(req, &udev, sizeof(udev));
3841 	return (error);
3842 }
3843 
3844 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3845     "system device tree");
3846 
3847 int
3848 bus_data_generation_check(int generation)
3849 {
3850 	if (generation != bus_data_generation)
3851 		return (1);
3852 
3853 	/* XXX generate optimised lists here? */
3854 	return (0);
3855 }
3856 
3857 void
3858 bus_data_generation_update(void)
3859 {
3860 	bus_data_generation++;
3861 }
3862 
3863 const char *
3864 intr_str_polarity(enum intr_polarity pola)
3865 {
3866 	switch (pola) {
3867 	case INTR_POLARITY_LOW:
3868 		return "low";
3869 
3870 	case INTR_POLARITY_HIGH:
3871 		return "high";
3872 
3873 	case INTR_POLARITY_CONFORM:
3874 		return "conform";
3875 	}
3876 	return "unknown";
3877 }
3878 
3879 const char *
3880 intr_str_trigger(enum intr_trigger trig)
3881 {
3882 	switch (trig) {
3883 	case INTR_TRIGGER_EDGE:
3884 		return "edge";
3885 
3886 	case INTR_TRIGGER_LEVEL:
3887 		return "level";
3888 
3889 	case INTR_TRIGGER_CONFORM:
3890 		return "conform";
3891 	}
3892 	return "unknown";
3893 }
3894 
3895 int
3896 device_getenv_int(device_t dev, const char *knob, int def)
3897 {
3898 	char env[128];
3899 
3900 	/* Deprecated; for compat */
3901 	ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3902 	kgetenv_int(env, &def);
3903 
3904 	/* Prefer dev.driver.unit.knob */
3905 	ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3906 	    device_get_name(dev), device_get_unit(dev), knob);
3907 	kgetenv_int(env, &def);
3908 
3909 	return def;
3910 }
3911 
3912 void
3913 device_getenv_string(device_t dev, const char *knob, char * __restrict data,
3914     int dlen, const char * __restrict def)
3915 {
3916 	char env[128];
3917 
3918 	strlcpy(data, def, dlen);
3919 
3920 	/* Deprecated; for compat */
3921 	ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3922 	kgetenv_string(env, data, dlen);
3923 
3924 	/* Prefer dev.driver.unit.knob */
3925 	ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3926 	    device_get_name(dev), device_get_unit(dev), knob);
3927 	kgetenv_string(env, data, dlen);
3928 }
3929