xref: /dragonfly/sys/kern/subr_bus.c (revision e26d350b)
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 		/*
1098 		 * WARNING: Due to gcc builtin optimization,
1099 		 *	    calling bcopy causes gcc to assume
1100 		 *	    that the source and destination args
1101 		 *	    cannot be NULL and optimize-away later
1102 		 *	    conditional tests to determine if dc->devices
1103 		 *	    is NULL.  In this situation, in fact,
1104 		 *	    dc->devices CAN be NULL w/ maxunit == 0.
1105 		 */
1106 		if (dc->devices) {
1107 			bcopy(dc->devices,
1108 			      newlist,
1109 			      sizeof(device_t) * dc->maxunit);
1110 			kfree(dc->devices, M_BUS);
1111 		}
1112 		dc->devices = newlist;
1113 		dc->maxunit = newsize;
1114 	}
1115 	PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1116 
1117 	*unitp = unit;
1118 	return(0);
1119 }
1120 
1121 static int
1122 devclass_add_device(devclass_t dc, device_t dev)
1123 {
1124 	int buflen, error;
1125 
1126 	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1127 
1128 	buflen = strlen(dc->name) + 5;
1129 	dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
1130 	if (dev->nameunit == NULL)
1131 		return(ENOMEM);
1132 
1133 	if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1134 		kfree(dev->nameunit, M_BUS);
1135 		dev->nameunit = NULL;
1136 		return(error);
1137 	}
1138 	dc->devices[dev->unit] = dev;
1139 	dev->devclass = dc;
1140 	ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1141 
1142 	return(0);
1143 }
1144 
1145 static int
1146 devclass_delete_device(devclass_t dc, device_t dev)
1147 {
1148 	if (!dc || !dev)
1149 		return(0);
1150 
1151 	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1152 
1153 	if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1154 		panic("devclass_delete_device: inconsistent device class");
1155 	dc->devices[dev->unit] = NULL;
1156 	if (dev->flags & DF_WILDCARD)
1157 		dev->unit = -1;
1158 	dev->devclass = NULL;
1159 	kfree(dev->nameunit, M_BUS);
1160 	dev->nameunit = NULL;
1161 
1162 	return(0);
1163 }
1164 
1165 static device_t
1166 make_device(device_t parent, const char *name, int unit)
1167 {
1168 	device_t dev;
1169 	devclass_t dc;
1170 
1171 	PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1172 
1173 	if (name != NULL) {
1174 		dc = devclass_find_internal(name, NULL, TRUE);
1175 		if (!dc) {
1176 			kprintf("make_device: can't find device class %s\n", name);
1177 			return(NULL);
1178 		}
1179 	} else
1180 		dc = NULL;
1181 
1182 	dev = kmalloc(sizeof(struct bsd_device), M_BUS, M_INTWAIT | M_ZERO);
1183 	if (!dev)
1184 		return(0);
1185 
1186 	dev->parent = parent;
1187 	TAILQ_INIT(&dev->children);
1188 	kobj_init((kobj_t) dev, &null_class);
1189 	dev->driver = NULL;
1190 	dev->devclass = NULL;
1191 	dev->unit = unit;
1192 	dev->nameunit = NULL;
1193 	dev->desc = NULL;
1194 	dev->busy = 0;
1195 	dev->devflags = 0;
1196 	dev->flags = DF_ENABLED;
1197 	dev->order = 0;
1198 	if (unit == -1)
1199 		dev->flags |= DF_WILDCARD;
1200 	if (name) {
1201 		dev->flags |= DF_FIXEDCLASS;
1202 		if (devclass_add_device(dc, dev) != 0) {
1203 			kobj_delete((kobj_t)dev, M_BUS);
1204 			return(NULL);
1205 		}
1206     	}
1207 	dev->ivars = NULL;
1208 	dev->softc = NULL;
1209 
1210 	dev->state = DS_NOTPRESENT;
1211 
1212 	TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1213 	bus_data_generation_update();
1214 
1215 	return(dev);
1216 }
1217 
1218 static int
1219 device_print_child(device_t dev, device_t child)
1220 {
1221 	int retval = 0;
1222 
1223 	if (device_is_alive(child))
1224 		retval += BUS_PRINT_CHILD(dev, child);
1225 	else
1226 		retval += device_printf(child, " not found\n");
1227 
1228 	return(retval);
1229 }
1230 
1231 device_t
1232 device_add_child(device_t dev, const char *name, int unit)
1233 {
1234 	return device_add_child_ordered(dev, 0, name, unit);
1235 }
1236 
1237 device_t
1238 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1239 {
1240 	device_t child;
1241 	device_t place;
1242 
1243 	PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1244 		order, unit));
1245 
1246 	child = make_device(dev, name, unit);
1247 	if (child == NULL)
1248 		return child;
1249 	child->order = order;
1250 
1251 	TAILQ_FOREACH(place, &dev->children, link) {
1252 		if (place->order > order)
1253 			break;
1254 	}
1255 
1256 	if (place) {
1257 		/*
1258 		 * The device 'place' is the first device whose order is
1259 		 * greater than the new child.
1260 		 */
1261 		TAILQ_INSERT_BEFORE(place, child, link);
1262 	} else {
1263 		/*
1264 		 * The new child's order is greater or equal to the order of
1265 		 * any existing device. Add the child to the tail of the list.
1266 		 */
1267 		TAILQ_INSERT_TAIL(&dev->children, child, link);
1268     	}
1269 
1270 	bus_data_generation_update();
1271 	return(child);
1272 }
1273 
1274 int
1275 device_delete_child(device_t dev, device_t child)
1276 {
1277 	int error;
1278 	device_t grandchild;
1279 
1280 	PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1281 
1282 	/* remove children first */
1283 	while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1284         	error = device_delete_child(child, grandchild);
1285 		if (error)
1286 			return(error);
1287 	}
1288 
1289 	if ((error = device_detach(child)) != 0)
1290 		return(error);
1291 	if (child->devclass)
1292 		devclass_delete_device(child->devclass, child);
1293 	TAILQ_REMOVE(&dev->children, child, link);
1294 	TAILQ_REMOVE(&bus_data_devices, child, devlink);
1295 	kobj_delete((kobj_t)child, M_BUS);
1296 
1297 	bus_data_generation_update();
1298 	return(0);
1299 }
1300 
1301 /**
1302  * @brief Delete all children devices of the given device, if any.
1303  *
1304  * This function deletes all children devices of the given device, if
1305  * any, using the device_delete_child() function for each device it
1306  * finds. If a child device cannot be deleted, this function will
1307  * return an error code.
1308  *
1309  * @param dev		the parent device
1310  *
1311  * @retval 0		success
1312  * @retval non-zero	a device would not detach
1313  */
1314 int
1315 device_delete_children(device_t dev)
1316 {
1317 	device_t child;
1318 	int error;
1319 
1320 	PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1321 
1322 	error = 0;
1323 
1324 	while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1325 		error = device_delete_child(dev, child);
1326 		if (error) {
1327 			PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1328 			break;
1329 		}
1330 	}
1331 	return (error);
1332 }
1333 
1334 /**
1335  * @brief Find a device given a unit number
1336  *
1337  * This is similar to devclass_get_devices() but only searches for
1338  * devices which have @p dev as a parent.
1339  *
1340  * @param dev		the parent device to search
1341  * @param unit		the unit number to search for.  If the unit is -1,
1342  *			return the first child of @p dev which has name
1343  *			@p classname (that is, the one with the lowest unit.)
1344  *
1345  * @returns		the device with the given unit number or @c
1346  *			NULL if there is no such device
1347  */
1348 device_t
1349 device_find_child(device_t dev, const char *classname, int unit)
1350 {
1351 	devclass_t dc;
1352 	device_t child;
1353 
1354 	dc = devclass_find(classname);
1355 	if (!dc)
1356 		return(NULL);
1357 
1358 	if (unit != -1) {
1359 		child = devclass_get_device(dc, unit);
1360 		if (child && child->parent == dev)
1361 			return (child);
1362 	} else {
1363 		for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1364 			child = devclass_get_device(dc, unit);
1365 			if (child && child->parent == dev)
1366 				return (child);
1367 		}
1368 	}
1369 	return(NULL);
1370 }
1371 
1372 static driverlink_t
1373 first_matching_driver(devclass_t dc, device_t dev)
1374 {
1375 	if (dev->devclass)
1376 		return(devclass_find_driver_internal(dc, dev->devclass->name));
1377 	else
1378 		return(TAILQ_FIRST(&dc->drivers));
1379 }
1380 
1381 static driverlink_t
1382 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1383 {
1384 	if (dev->devclass) {
1385 		driverlink_t dl;
1386 		for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1387 			if (!strcmp(dev->devclass->name, dl->driver->name))
1388 				return(dl);
1389 		return(NULL);
1390 	} else
1391 		return(TAILQ_NEXT(last, link));
1392 }
1393 
1394 int
1395 device_probe_child(device_t dev, device_t child)
1396 {
1397 	devclass_t dc;
1398 	driverlink_t best = NULL;
1399 	driverlink_t dl;
1400 	int result, pri = 0;
1401 	int hasclass = (child->devclass != NULL);
1402 
1403 	dc = dev->devclass;
1404 	if (!dc)
1405 		panic("device_probe_child: parent device has no devclass");
1406 
1407 	if (child->state == DS_ALIVE)
1408 		return(0);
1409 
1410 	for (; dc; dc = dc->parent) {
1411 		for (dl = first_matching_driver(dc, child); dl;
1412 		     dl = next_matching_driver(dc, child, dl)) {
1413 			PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1414 			device_set_driver(child, dl->driver);
1415 			if (!hasclass)
1416 				device_set_devclass(child, dl->driver->name);
1417 			result = DEVICE_PROBE(child);
1418 			if (!hasclass)
1419 				device_set_devclass(child, 0);
1420 
1421 			/*
1422 			 * If the driver returns SUCCESS, there can be
1423 			 * no higher match for this device.
1424 			 */
1425 			if (result == 0) {
1426 				best = dl;
1427 				pri = 0;
1428 				break;
1429 			}
1430 
1431 			/*
1432 			 * The driver returned an error so it
1433 			 * certainly doesn't match.
1434 			 */
1435 			if (result > 0) {
1436 				device_set_driver(child, NULL);
1437 				continue;
1438 			}
1439 
1440 			/*
1441 			 * A priority lower than SUCCESS, remember the
1442 			 * best matching driver. Initialise the value
1443 			 * of pri for the first match.
1444 			 */
1445 			if (best == NULL || result > pri) {
1446 				best = dl;
1447 				pri = result;
1448 				continue;
1449 			}
1450 		}
1451 		/*
1452 	         * If we have unambiguous match in this devclass,
1453 	         * don't look in the parent.
1454 	         */
1455 	        if (best && pri == 0)
1456 			break;
1457 	}
1458 
1459 	/*
1460 	 * If we found a driver, change state and initialise the devclass.
1461 	 */
1462 	if (best) {
1463 		if (!child->devclass)
1464 			device_set_devclass(child, best->driver->name);
1465 		device_set_driver(child, best->driver);
1466 		if (pri < 0) {
1467 			/*
1468 			 * A bit bogus. Call the probe method again to make
1469 			 * sure that we have the right description.
1470 			 */
1471 			DEVICE_PROBE(child);
1472 		}
1473 
1474 		bus_data_generation_update();
1475 		child->state = DS_ALIVE;
1476 		return(0);
1477 	}
1478 
1479 	return(ENXIO);
1480 }
1481 
1482 int
1483 device_probe_child_gpri(device_t dev, device_t child, u_int gpri)
1484 {
1485 	devclass_t dc;
1486 	driverlink_t best = NULL;
1487 	driverlink_t dl;
1488 	int result, pri = 0;
1489 	int hasclass = (child->devclass != NULL);
1490 
1491 	dc = dev->devclass;
1492 	if (!dc)
1493 		panic("device_probe_child: parent device has no devclass");
1494 
1495 	if (child->state == DS_ALIVE)
1496 		return(0);
1497 
1498 	for (; dc; dc = dc->parent) {
1499 		for (dl = first_matching_driver(dc, child); dl;
1500 			dl = next_matching_driver(dc, child, dl)) {
1501 			/*
1502 			 * GPRI handling, only probe drivers with the
1503 			 * specific GPRI.
1504 			 */
1505 			if (dl->driver->gpri != gpri)
1506 				continue;
1507 
1508 			PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1509 			device_set_driver(child, dl->driver);
1510 			if (!hasclass)
1511 				device_set_devclass(child, dl->driver->name);
1512 			result = DEVICE_PROBE(child);
1513 			if (!hasclass)
1514 				device_set_devclass(child, 0);
1515 
1516 			/*
1517 			 * If the driver returns SUCCESS, there can be
1518 			 * no higher match for this device.
1519 			 */
1520 			if (result == 0) {
1521 				best = dl;
1522 				pri = 0;
1523 				break;
1524 			}
1525 
1526 			/*
1527 			 * The driver returned an error so it
1528 			 * certainly doesn't match.
1529 			 */
1530 			if (result > 0) {
1531 				device_set_driver(child, NULL);
1532 				continue;
1533 			}
1534 
1535 			/*
1536 			 * A priority lower than SUCCESS, remember the
1537 			 * best matching driver. Initialise the value
1538 			 * of pri for the first match.
1539 			 */
1540 			if (best == NULL || result > pri) {
1541 				best = dl;
1542 				pri = result;
1543 				continue;
1544 			}
1545 	        }
1546 		/*
1547 	         * If we have unambiguous match in this devclass,
1548 	         * don't look in the parent.
1549 	         */
1550 	        if (best && pri == 0)
1551 			break;
1552 	}
1553 
1554 	/*
1555 	 * If we found a driver, change state and initialise the devclass.
1556 	 */
1557 	if (best) {
1558 		if (!child->devclass)
1559 			device_set_devclass(child, best->driver->name);
1560 		device_set_driver(child, best->driver);
1561 		if (pri < 0) {
1562 			/*
1563 			 * A bit bogus. Call the probe method again to make
1564 			 * sure that we have the right description.
1565 			 */
1566 			DEVICE_PROBE(child);
1567 		}
1568 
1569 		bus_data_generation_update();
1570 		child->state = DS_ALIVE;
1571 		return(0);
1572 	}
1573 
1574 	return(ENXIO);
1575 }
1576 
1577 device_t
1578 device_get_parent(device_t dev)
1579 {
1580 	return dev->parent;
1581 }
1582 
1583 int
1584 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1585 {
1586 	int count;
1587 	device_t child;
1588 	device_t *list;
1589 
1590 	count = 0;
1591 	TAILQ_FOREACH(child, &dev->children, link)
1592 		count++;
1593 
1594 	list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1595 
1596 	count = 0;
1597 	TAILQ_FOREACH(child, &dev->children, link) {
1598 		list[count] = child;
1599 		count++;
1600 	}
1601 
1602 	*devlistp = list;
1603 	*devcountp = count;
1604 
1605 	return(0);
1606 }
1607 
1608 driver_t *
1609 device_get_driver(device_t dev)
1610 {
1611 	return(dev->driver);
1612 }
1613 
1614 devclass_t
1615 device_get_devclass(device_t dev)
1616 {
1617 	return(dev->devclass);
1618 }
1619 
1620 const char *
1621 device_get_name(device_t dev)
1622 {
1623 	if (dev->devclass)
1624 		return devclass_get_name(dev->devclass);
1625 	return(NULL);
1626 }
1627 
1628 const char *
1629 device_get_nameunit(device_t dev)
1630 {
1631 	return(dev->nameunit);
1632 }
1633 
1634 int
1635 device_get_unit(device_t dev)
1636 {
1637 	return(dev->unit);
1638 }
1639 
1640 const char *
1641 device_get_desc(device_t dev)
1642 {
1643 	return(dev->desc);
1644 }
1645 
1646 uint32_t
1647 device_get_flags(device_t dev)
1648 {
1649 	return(dev->devflags);
1650 }
1651 
1652 struct sysctl_ctx_list *
1653 device_get_sysctl_ctx(device_t dev)
1654 {
1655 	return (&dev->sysctl_ctx);
1656 }
1657 
1658 struct sysctl_oid *
1659 device_get_sysctl_tree(device_t dev)
1660 {
1661 	return (dev->sysctl_tree);
1662 }
1663 
1664 int
1665 device_print_prettyname(device_t dev)
1666 {
1667 	const char *name = device_get_name(dev);
1668 
1669 	if (name == NULL)
1670 		return kprintf("unknown: ");
1671 	else
1672 		return kprintf("%s%d: ", name, device_get_unit(dev));
1673 }
1674 
1675 int
1676 device_printf(device_t dev, const char * fmt, ...)
1677 {
1678 	__va_list ap;
1679 	int retval;
1680 
1681 	retval = device_print_prettyname(dev);
1682 	__va_start(ap, fmt);
1683 	retval += kvprintf(fmt, ap);
1684 	__va_end(ap);
1685 	return retval;
1686 }
1687 
1688 static void
1689 device_set_desc_internal(device_t dev, const char* desc, int copy)
1690 {
1691 	if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1692 		kfree(dev->desc, M_BUS);
1693 		dev->flags &= ~DF_DESCMALLOCED;
1694 		dev->desc = NULL;
1695 	}
1696 
1697 	if (copy && desc) {
1698 		dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1699 		if (dev->desc) {
1700 			strcpy(dev->desc, desc);
1701 			dev->flags |= DF_DESCMALLOCED;
1702 		}
1703 	} else {
1704 		/* Avoid a -Wcast-qual warning */
1705 		dev->desc = (char *)(uintptr_t) desc;
1706 	}
1707 
1708 	bus_data_generation_update();
1709 }
1710 
1711 void
1712 device_set_desc(device_t dev, const char* desc)
1713 {
1714 	device_set_desc_internal(dev, desc, FALSE);
1715 }
1716 
1717 void
1718 device_set_desc_copy(device_t dev, const char* desc)
1719 {
1720 	device_set_desc_internal(dev, desc, TRUE);
1721 }
1722 
1723 void
1724 device_set_flags(device_t dev, uint32_t flags)
1725 {
1726 	dev->devflags = flags;
1727 }
1728 
1729 void *
1730 device_get_softc(device_t dev)
1731 {
1732 	return dev->softc;
1733 }
1734 
1735 void
1736 device_set_softc(device_t dev, void *softc)
1737 {
1738 	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1739 		kfree(dev->softc, M_BUS);
1740 	dev->softc = softc;
1741 	if (dev->softc)
1742 		dev->flags |= DF_EXTERNALSOFTC;
1743 	else
1744 		dev->flags &= ~DF_EXTERNALSOFTC;
1745 }
1746 
1747 void
1748 device_set_async_attach(device_t dev, int enable)
1749 {
1750 	if (enable)
1751 		dev->flags |= DF_ASYNCPROBE;
1752 	else
1753 		dev->flags &= ~DF_ASYNCPROBE;
1754 }
1755 
1756 void *
1757 device_get_ivars(device_t dev)
1758 {
1759 	return dev->ivars;
1760 }
1761 
1762 void
1763 device_set_ivars(device_t dev, void * ivars)
1764 {
1765 	if (!dev)
1766 		return;
1767 
1768 	dev->ivars = ivars;
1769 }
1770 
1771 device_state_t
1772 device_get_state(device_t dev)
1773 {
1774 	return(dev->state);
1775 }
1776 
1777 void
1778 device_enable(device_t dev)
1779 {
1780 	dev->flags |= DF_ENABLED;
1781 }
1782 
1783 void
1784 device_disable(device_t dev)
1785 {
1786 	dev->flags &= ~DF_ENABLED;
1787 }
1788 
1789 /*
1790  * YYY cannot block
1791  */
1792 void
1793 device_busy(device_t dev)
1794 {
1795 	if (dev->state < DS_ATTACHED)
1796 		panic("device_busy: called for unattached device");
1797 	if (dev->busy == 0 && dev->parent)
1798 		device_busy(dev->parent);
1799 	dev->busy++;
1800 	dev->state = DS_BUSY;
1801 }
1802 
1803 /*
1804  * YYY cannot block
1805  */
1806 void
1807 device_unbusy(device_t dev)
1808 {
1809 	if (dev->state != DS_BUSY)
1810 		panic("device_unbusy: called for non-busy device");
1811 	dev->busy--;
1812 	if (dev->busy == 0) {
1813 		if (dev->parent)
1814 			device_unbusy(dev->parent);
1815 		dev->state = DS_ATTACHED;
1816 	}
1817 }
1818 
1819 void
1820 device_quiet(device_t dev)
1821 {
1822 	dev->flags |= DF_QUIET;
1823 }
1824 
1825 void
1826 device_verbose(device_t dev)
1827 {
1828 	dev->flags &= ~DF_QUIET;
1829 }
1830 
1831 int
1832 device_is_quiet(device_t dev)
1833 {
1834 	return((dev->flags & DF_QUIET) != 0);
1835 }
1836 
1837 int
1838 device_is_enabled(device_t dev)
1839 {
1840 	return((dev->flags & DF_ENABLED) != 0);
1841 }
1842 
1843 int
1844 device_is_alive(device_t dev)
1845 {
1846 	return(dev->state >= DS_ALIVE);
1847 }
1848 
1849 int
1850 device_is_attached(device_t dev)
1851 {
1852 	return(dev->state >= DS_ATTACHED);
1853 }
1854 
1855 int
1856 device_set_devclass(device_t dev, const char *classname)
1857 {
1858 	devclass_t dc;
1859 	int error;
1860 
1861 	if (!classname) {
1862 		if (dev->devclass)
1863 			devclass_delete_device(dev->devclass, dev);
1864 		return(0);
1865 	}
1866 
1867 	if (dev->devclass) {
1868 		kprintf("device_set_devclass: device class already set\n");
1869 		return(EINVAL);
1870 	}
1871 
1872 	dc = devclass_find_internal(classname, NULL, TRUE);
1873 	if (!dc)
1874 		return(ENOMEM);
1875 
1876 	error = devclass_add_device(dc, dev);
1877 
1878 	bus_data_generation_update();
1879 	return(error);
1880 }
1881 
1882 int
1883 device_set_driver(device_t dev, driver_t *driver)
1884 {
1885 	if (dev->state >= DS_ATTACHED)
1886 		return(EBUSY);
1887 
1888 	if (dev->driver == driver)
1889 		return(0);
1890 
1891 	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1892 		kfree(dev->softc, M_BUS);
1893 		dev->softc = NULL;
1894 	}
1895 	device_set_desc(dev, NULL);
1896 	kobj_delete((kobj_t) dev, 0);
1897 	dev->driver = driver;
1898 	if (driver) {
1899 		kobj_init((kobj_t) dev, (kobj_class_t) driver);
1900 		if (!(dev->flags & DF_EXTERNALSOFTC))
1901 			dev->softc = kmalloc(driver->size, M_BUS,
1902 					    M_INTWAIT | M_ZERO);
1903 	} else {
1904 		kobj_init((kobj_t) dev, &null_class);
1905 	}
1906 
1907 	bus_data_generation_update();
1908 	return(0);
1909 }
1910 
1911 int
1912 device_probe_and_attach(device_t dev)
1913 {
1914 	device_t bus = dev->parent;
1915 	int error = 0;
1916 
1917 	if (dev->state >= DS_ALIVE)
1918 		return(0);
1919 
1920 	if ((dev->flags & DF_ENABLED) == 0) {
1921 		if (bootverbose) {
1922 			device_print_prettyname(dev);
1923 			kprintf("not probed (disabled)\n");
1924 		}
1925 		return(0);
1926 	}
1927 
1928 	error = device_probe_child(bus, dev);
1929 	if (error) {
1930 		if (!(dev->flags & DF_DONENOMATCH)) {
1931 			BUS_PROBE_NOMATCH(bus, dev);
1932 			devnomatch(dev);
1933 			dev->flags |= DF_DONENOMATCH;
1934 		}
1935 		return(error);
1936 	}
1937 
1938 	/*
1939 	 * Output the exact device chain prior to the attach in case the
1940 	 * system locks up during attach, and generate the full info after
1941 	 * the attach so correct irq and other information is displayed.
1942 	 */
1943 	if (bootverbose && !device_is_quiet(dev)) {
1944 		device_t tmp;
1945 
1946 		kprintf("%s", device_get_nameunit(dev));
1947 		for (tmp = dev->parent; tmp; tmp = tmp->parent)
1948 			kprintf(".%s", device_get_nameunit(tmp));
1949 		kprintf("\n");
1950 	}
1951 	if (!device_is_quiet(dev))
1952 		device_print_child(bus, dev);
1953 	if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1954 		kprintf("%s: probing asynchronously\n",
1955 			device_get_nameunit(dev));
1956 		dev->state = DS_INPROGRESS;
1957 		device_attach_async(dev);
1958 		error = 0;
1959 	} else {
1960 		error = device_doattach(dev);
1961 	}
1962 	return(error);
1963 }
1964 
1965 int
1966 device_probe_and_attach_gpri(device_t dev, u_int gpri)
1967 {
1968 	device_t bus = dev->parent;
1969 	int error = 0;
1970 
1971 	if (dev->state >= DS_ALIVE)
1972 		return(0);
1973 
1974 	if ((dev->flags & DF_ENABLED) == 0) {
1975 		if (bootverbose) {
1976 			device_print_prettyname(dev);
1977 			kprintf("not probed (disabled)\n");
1978 		}
1979 		return(0);
1980 	}
1981 
1982 	error = device_probe_child_gpri(bus, dev, gpri);
1983 	if (error) {
1984 #if 0
1985 		if (!(dev->flags & DF_DONENOMATCH)) {
1986 			BUS_PROBE_NOMATCH(bus, dev);
1987 			devnomatch(dev);
1988 			dev->flags |= DF_DONENOMATCH;
1989 		}
1990 #endif
1991 		return(error);
1992 	}
1993 
1994 	/*
1995 	 * Output the exact device chain prior to the attach in case the
1996 	 * system locks up during attach, and generate the full info after
1997 	 * the attach so correct irq and other information is displayed.
1998 	 */
1999 	if (bootverbose && !device_is_quiet(dev)) {
2000 		device_t tmp;
2001 
2002 		kprintf("%s", device_get_nameunit(dev));
2003 		for (tmp = dev->parent; tmp; tmp = tmp->parent)
2004 			kprintf(".%s", device_get_nameunit(tmp));
2005 		kprintf("\n");
2006 	}
2007 	if (!device_is_quiet(dev))
2008 		device_print_child(bus, dev);
2009 	if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
2010 		kprintf("%s: probing asynchronously\n",
2011 			device_get_nameunit(dev));
2012 		dev->state = DS_INPROGRESS;
2013 		device_attach_async(dev);
2014 		error = 0;
2015 	} else {
2016 		error = device_doattach(dev);
2017 	}
2018 	return(error);
2019 }
2020 
2021 /*
2022  * Device is known to be alive, do the attach asynchronously.
2023  * However, serialize the attaches with the mp lock.
2024  */
2025 static void
2026 device_attach_async(device_t dev)
2027 {
2028 	thread_t td;
2029 
2030 	atomic_add_int(&numasyncthreads, 1);
2031 	lwkt_create(device_attach_thread, dev, &td, NULL,
2032 		    0, 0, "%s", (dev->desc ? dev->desc : "devattach"));
2033 }
2034 
2035 static void
2036 device_attach_thread(void *arg)
2037 {
2038 	device_t dev = arg;
2039 
2040 	(void)device_doattach(dev);
2041 	atomic_subtract_int(&numasyncthreads, 1);
2042 	wakeup(&numasyncthreads);
2043 }
2044 
2045 /*
2046  * Device is known to be alive, do the attach (synchronous or asynchronous)
2047  */
2048 static int
2049 device_doattach(device_t dev)
2050 {
2051 	device_t bus = dev->parent;
2052 	int hasclass = (dev->devclass != NULL);
2053 	int error;
2054 
2055 	device_sysctl_init(dev);
2056 	error = DEVICE_ATTACH(dev);
2057 	if (error == 0) {
2058 		dev->state = DS_ATTACHED;
2059 		if (bootverbose && !device_is_quiet(dev))
2060 			device_print_child(bus, dev);
2061 		device_sysctl_update(dev);
2062 		devadded(dev);
2063 	} else {
2064 		kprintf("device_probe_and_attach: %s%d attach returned %d\n",
2065 		       dev->driver->name, dev->unit, error);
2066 		/* Unset the class that was set in device_probe_child */
2067 		if (!hasclass)
2068 			device_set_devclass(dev, 0);
2069 		device_set_driver(dev, NULL);
2070 		dev->state = DS_NOTPRESENT;
2071 		device_sysctl_fini(dev);
2072 	}
2073 	return(error);
2074 }
2075 
2076 int
2077 device_detach(device_t dev)
2078 {
2079 	int error;
2080 
2081 	PDEBUG(("%s", DEVICENAME(dev)));
2082 	if (dev->state == DS_BUSY)
2083 		return(EBUSY);
2084 	if (dev->state != DS_ATTACHED)
2085 		return(0);
2086 
2087 	if ((error = DEVICE_DETACH(dev)) != 0)
2088 		return(error);
2089 	devremoved(dev);
2090 	device_printf(dev, "detached\n");
2091 	if (dev->parent)
2092 		BUS_CHILD_DETACHED(dev->parent, dev);
2093 
2094 	if (!(dev->flags & DF_FIXEDCLASS))
2095 		devclass_delete_device(dev->devclass, dev);
2096 
2097 	dev->state = DS_NOTPRESENT;
2098 	device_set_driver(dev, NULL);
2099 	device_sysctl_fini(dev);
2100 
2101 	return(0);
2102 }
2103 
2104 int
2105 device_shutdown(device_t dev)
2106 {
2107 	if (dev->state < DS_ATTACHED)
2108 		return 0;
2109 	PDEBUG(("%s", DEVICENAME(dev)));
2110 	return DEVICE_SHUTDOWN(dev);
2111 }
2112 
2113 int
2114 device_set_unit(device_t dev, int unit)
2115 {
2116 	devclass_t dc;
2117 	int err;
2118 
2119 	dc = device_get_devclass(dev);
2120 	if (unit < dc->maxunit && dc->devices[unit])
2121 		return(EBUSY);
2122 	err = devclass_delete_device(dc, dev);
2123 	if (err)
2124 		return(err);
2125 	dev->unit = unit;
2126 	err = devclass_add_device(dc, dev);
2127 	if (err)
2128 		return(err);
2129 
2130 	bus_data_generation_update();
2131 	return(0);
2132 }
2133 
2134 /*======================================*/
2135 /*
2136  * Access functions for device resources.
2137  */
2138 
2139 /* Supplied by config(8) in ioconf.c */
2140 extern struct config_device config_devtab[];
2141 extern int devtab_count;
2142 
2143 /* Runtime version */
2144 struct config_device *devtab = config_devtab;
2145 
2146 static int
2147 resource_new_name(const char *name, int unit)
2148 {
2149 	struct config_device *new;
2150 
2151 	new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
2152 		     M_INTWAIT | M_ZERO);
2153 	if (devtab && devtab_count > 0)
2154 		bcopy(devtab, new, devtab_count * sizeof(*new));
2155 	new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
2156 	if (new[devtab_count].name == NULL) {
2157 		kfree(new, M_TEMP);
2158 		return(-1);
2159 	}
2160 	strcpy(new[devtab_count].name, name);
2161 	new[devtab_count].unit = unit;
2162 	new[devtab_count].resource_count = 0;
2163 	new[devtab_count].resources = NULL;
2164 	if (devtab && devtab != config_devtab)
2165 		kfree(devtab, M_TEMP);
2166 	devtab = new;
2167 	return devtab_count++;
2168 }
2169 
2170 static int
2171 resource_new_resname(int j, const char *resname, resource_type type)
2172 {
2173 	struct config_resource *new;
2174 	int i;
2175 
2176 	i = devtab[j].resource_count;
2177 	new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
2178 	if (devtab[j].resources && i > 0)
2179 		bcopy(devtab[j].resources, new, i * sizeof(*new));
2180 	new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
2181 	if (new[i].name == NULL) {
2182 		kfree(new, M_TEMP);
2183 		return(-1);
2184 	}
2185 	strcpy(new[i].name, resname);
2186 	new[i].type = type;
2187 	if (devtab[j].resources)
2188 		kfree(devtab[j].resources, M_TEMP);
2189 	devtab[j].resources = new;
2190 	devtab[j].resource_count = i + 1;
2191 	return(i);
2192 }
2193 
2194 static int
2195 resource_match_string(int i, const char *resname, const char *value)
2196 {
2197 	int j;
2198 	struct config_resource *res;
2199 
2200 	for (j = 0, res = devtab[i].resources;
2201 	     j < devtab[i].resource_count; j++, res++)
2202 		if (!strcmp(res->name, resname)
2203 		    && res->type == RES_STRING
2204 		    && !strcmp(res->u.stringval, value))
2205 			return(j);
2206 	return(-1);
2207 }
2208 
2209 static int
2210 resource_find(const char *name, int unit, const char *resname,
2211 	      struct config_resource **result)
2212 {
2213 	int i, j;
2214 	struct config_resource *res;
2215 
2216 	/*
2217 	 * First check specific instances, then generic.
2218 	 */
2219 	for (i = 0; i < devtab_count; i++) {
2220 		if (devtab[i].unit < 0)
2221 			continue;
2222 		if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2223 			res = devtab[i].resources;
2224 			for (j = 0; j < devtab[i].resource_count; j++, res++)
2225 				if (!strcmp(res->name, resname)) {
2226 					*result = res;
2227 					return(0);
2228 				}
2229 		}
2230 	}
2231 	for (i = 0; i < devtab_count; i++) {
2232 		if (devtab[i].unit >= 0)
2233 			continue;
2234 		/* XXX should this `&& devtab[i].unit == unit' be here? */
2235 		/* XXX if so, then the generic match does nothing */
2236 		if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2237 			res = devtab[i].resources;
2238 			for (j = 0; j < devtab[i].resource_count; j++, res++)
2239 				if (!strcmp(res->name, resname)) {
2240 					*result = res;
2241 					return(0);
2242 				}
2243 		}
2244 	}
2245 	return(ENOENT);
2246 }
2247 
2248 static int
2249 resource_kenv(const char *name, int unit, const char *resname, long *result)
2250 {
2251 	const char *env;
2252 	char buf[64];
2253 
2254 	/*
2255 	 * DragonFly style loader.conf hinting
2256 	 */
2257 	ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2258 	if ((env = kgetenv(buf)) != NULL) {
2259 		*result = strtol(env, NULL, 0);
2260 		return(0);
2261 	}
2262 
2263 	/*
2264 	 * Also support FreeBSD style loader.conf hinting
2265 	 */
2266 	ksnprintf(buf, sizeof(buf), "hint.%s.%d.%s", name, unit, resname);
2267 	if ((env = kgetenv(buf)) != NULL) {
2268 		*result = strtol(env, NULL, 0);
2269 		return(0);
2270 	}
2271 
2272 	return (ENOENT);
2273 }
2274 
2275 int
2276 resource_int_value(const char *name, int unit, const char *resname, int *result)
2277 {
2278 	struct config_resource *res;
2279 	long kvalue = 0;
2280 	int error;
2281 
2282 	if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2283 		*result = (int)kvalue;
2284 		return 0;
2285 	}
2286 	if ((error = resource_find(name, unit, resname, &res)) != 0)
2287 		return(error);
2288 	if (res->type != RES_INT)
2289 		return(EFTYPE);
2290 	*result = res->u.intval;
2291 	return(0);
2292 }
2293 
2294 int
2295 resource_long_value(const char *name, int unit, const char *resname,
2296 		    long *result)
2297 {
2298 	struct config_resource *res;
2299 	long kvalue;
2300 	int error;
2301 
2302 	if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2303 		*result = kvalue;
2304 		return 0;
2305 	}
2306 	if ((error = resource_find(name, unit, resname, &res)) != 0)
2307 		return(error);
2308 	if (res->type != RES_LONG)
2309 		return(EFTYPE);
2310 	*result = res->u.longval;
2311 	return(0);
2312 }
2313 
2314 int
2315 resource_string_value(const char *name, int unit, const char *resname,
2316     const char **result)
2317 {
2318 	int error;
2319 	struct config_resource *res;
2320 	char buf[64];
2321 	const char *env;
2322 
2323 	/*
2324 	 * DragonFly style loader.conf hinting
2325 	 */
2326 	ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2327 	if ((env = kgetenv(buf)) != NULL) {
2328 		*result = env;
2329 		return 0;
2330 	}
2331 
2332 	/*
2333 	 * Also support FreeBSD style loader.conf hinting
2334 	 */
2335 	ksnprintf(buf, sizeof(buf), "hint.%s.%d.%s", name, unit, resname);
2336 	if ((env = kgetenv(buf)) != NULL) {
2337 		*result = env;
2338 		return 0;
2339 	}
2340 
2341 	if ((error = resource_find(name, unit, resname, &res)) != 0)
2342 		return(error);
2343 	if (res->type != RES_STRING)
2344 		return(EFTYPE);
2345 	*result = res->u.stringval;
2346 	return(0);
2347 }
2348 
2349 int
2350 resource_query_string(int i, const char *resname, const char *value)
2351 {
2352 	if (i < 0)
2353 		i = 0;
2354 	else
2355 		i = i + 1;
2356 	for (; i < devtab_count; i++)
2357 		if (resource_match_string(i, resname, value) >= 0)
2358 			return(i);
2359 	return(-1);
2360 }
2361 
2362 int
2363 resource_locate(int i, const char *resname)
2364 {
2365 	if (i < 0)
2366 		i = 0;
2367 	else
2368 		i = i + 1;
2369 	for (; i < devtab_count; i++)
2370 		if (!strcmp(devtab[i].name, resname))
2371 			return(i);
2372 	return(-1);
2373 }
2374 
2375 int
2376 resource_count(void)
2377 {
2378 	return(devtab_count);
2379 }
2380 
2381 char *
2382 resource_query_name(int i)
2383 {
2384 	return(devtab[i].name);
2385 }
2386 
2387 int
2388 resource_query_unit(int i)
2389 {
2390 	return(devtab[i].unit);
2391 }
2392 
2393 static int
2394 resource_create(const char *name, int unit, const char *resname,
2395 		resource_type type, struct config_resource **result)
2396 {
2397 	int i, j;
2398 	struct config_resource *res = NULL;
2399 
2400 	for (i = 0; i < devtab_count; i++)
2401 		if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2402 			res = devtab[i].resources;
2403 			break;
2404 		}
2405 	if (res == NULL) {
2406 		i = resource_new_name(name, unit);
2407 		if (i < 0)
2408 			return(ENOMEM);
2409 		res = devtab[i].resources;
2410 	}
2411 	for (j = 0; j < devtab[i].resource_count; j++, res++)
2412 		if (!strcmp(res->name, resname)) {
2413 			*result = res;
2414 			return(0);
2415 		}
2416 	j = resource_new_resname(i, resname, type);
2417 	if (j < 0)
2418 		return(ENOMEM);
2419 	res = &devtab[i].resources[j];
2420 	*result = res;
2421 	return(0);
2422 }
2423 
2424 int
2425 resource_set_int(const char *name, int unit, const char *resname, int value)
2426 {
2427 	int error;
2428 	struct config_resource *res;
2429 
2430 	error = resource_create(name, unit, resname, RES_INT, &res);
2431 	if (error)
2432 		return(error);
2433 	if (res->type != RES_INT)
2434 		return(EFTYPE);
2435 	res->u.intval = value;
2436 	return(0);
2437 }
2438 
2439 int
2440 resource_set_long(const char *name, int unit, const char *resname, long value)
2441 {
2442 	int error;
2443 	struct config_resource *res;
2444 
2445 	error = resource_create(name, unit, resname, RES_LONG, &res);
2446 	if (error)
2447 		return(error);
2448 	if (res->type != RES_LONG)
2449 		return(EFTYPE);
2450 	res->u.longval = value;
2451 	return(0);
2452 }
2453 
2454 int
2455 resource_set_string(const char *name, int unit, const char *resname,
2456 		    const char *value)
2457 {
2458 	int error;
2459 	struct config_resource *res;
2460 
2461 	error = resource_create(name, unit, resname, RES_STRING, &res);
2462 	if (error)
2463 		return(error);
2464 	if (res->type != RES_STRING)
2465 		return(EFTYPE);
2466 	if (res->u.stringval)
2467 		kfree(res->u.stringval, M_TEMP);
2468 	res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2469 	if (res->u.stringval == NULL)
2470 		return(ENOMEM);
2471 	strcpy(res->u.stringval, value);
2472 	return(0);
2473 }
2474 
2475 static void
2476 resource_cfgload(void *dummy __unused)
2477 {
2478 	struct config_resource *res, *cfgres;
2479 	int i, j;
2480 	int error;
2481 	char *name, *resname;
2482 	int unit;
2483 	resource_type type;
2484 	char *stringval;
2485 	int config_devtab_count;
2486 
2487 	config_devtab_count = devtab_count;
2488 	devtab = NULL;
2489 	devtab_count = 0;
2490 
2491 	for (i = 0; i < config_devtab_count; i++) {
2492 		name = config_devtab[i].name;
2493 		unit = config_devtab[i].unit;
2494 
2495 		for (j = 0; j < config_devtab[i].resource_count; j++) {
2496 			cfgres = config_devtab[i].resources;
2497 			resname = cfgres[j].name;
2498 			type = cfgres[j].type;
2499 			error = resource_create(name, unit, resname, type,
2500 						&res);
2501 			if (error) {
2502 				kprintf("create resource %s%d: error %d\n",
2503 					name, unit, error);
2504 				continue;
2505 			}
2506 			if (res->type != type) {
2507 				kprintf("type mismatch %s%d: %d != %d\n",
2508 					name, unit, res->type, type);
2509 				continue;
2510 			}
2511 			switch (type) {
2512 			case RES_INT:
2513 				res->u.intval = cfgres[j].u.intval;
2514 				break;
2515 			case RES_LONG:
2516 				res->u.longval = cfgres[j].u.longval;
2517 				break;
2518 			case RES_STRING:
2519 				if (res->u.stringval)
2520 					kfree(res->u.stringval, M_TEMP);
2521 				stringval = cfgres[j].u.stringval;
2522 				res->u.stringval = kmalloc(strlen(stringval) + 1,
2523 							  M_TEMP, M_INTWAIT);
2524 				if (res->u.stringval == NULL)
2525 					break;
2526 				strcpy(res->u.stringval, stringval);
2527 				break;
2528 			default:
2529 				panic("unknown resource type %d", type);
2530 			}
2531 		}
2532 	}
2533 }
2534 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0);
2535 
2536 
2537 /*======================================*/
2538 /*
2539  * Some useful method implementations to make life easier for bus drivers.
2540  */
2541 
2542 void
2543 resource_list_init(struct resource_list *rl)
2544 {
2545 	SLIST_INIT(rl);
2546 }
2547 
2548 void
2549 resource_list_free(struct resource_list *rl)
2550 {
2551 	struct resource_list_entry *rle;
2552 
2553 	while ((rle = SLIST_FIRST(rl)) != NULL) {
2554 		if (rle->res)
2555 			panic("resource_list_free: resource entry is busy");
2556 		SLIST_REMOVE_HEAD(rl, link);
2557 		kfree(rle, M_BUS);
2558 	}
2559 }
2560 
2561 void
2562 resource_list_add(struct resource_list *rl, int type, int rid,
2563     u_long start, u_long end, u_long count, int cpuid)
2564 {
2565 	struct resource_list_entry *rle;
2566 
2567 	rle = resource_list_find(rl, type, rid);
2568 	if (rle == NULL) {
2569 		rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2570 			     M_INTWAIT);
2571 		SLIST_INSERT_HEAD(rl, rle, link);
2572 		rle->type = type;
2573 		rle->rid = rid;
2574 		rle->res = NULL;
2575 		rle->cpuid = -1;
2576 	}
2577 
2578 	if (rle->res)
2579 		panic("resource_list_add: resource entry is busy");
2580 
2581 	rle->start = start;
2582 	rle->end = end;
2583 	rle->count = count;
2584 
2585 	if (cpuid != -1) {
2586 		if (rle->cpuid != -1 && rle->cpuid != cpuid) {
2587 			panic("resource_list_add: moving from cpu%d -> cpu%d",
2588 			    rle->cpuid, cpuid);
2589 		}
2590 		rle->cpuid = cpuid;
2591 	}
2592 }
2593 
2594 struct resource_list_entry*
2595 resource_list_find(struct resource_list *rl,
2596 		   int type, int rid)
2597 {
2598 	struct resource_list_entry *rle;
2599 
2600 	SLIST_FOREACH(rle, rl, link)
2601 		if (rle->type == type && rle->rid == rid)
2602 			return(rle);
2603 	return(NULL);
2604 }
2605 
2606 void
2607 resource_list_delete(struct resource_list *rl,
2608 		     int type, int rid)
2609 {
2610 	struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2611 
2612 	if (rle) {
2613 		if (rle->res != NULL)
2614 			panic("resource_list_delete: resource has not been released");
2615 		SLIST_REMOVE(rl, rle, resource_list_entry, link);
2616 		kfree(rle, M_BUS);
2617 	}
2618 }
2619 
2620 struct resource *
2621 resource_list_alloc(struct resource_list *rl,
2622 		    device_t bus, device_t child,
2623 		    int type, int *rid,
2624 		    u_long start, u_long end,
2625 		    u_long count, u_int flags, int cpuid)
2626 {
2627 	struct resource_list_entry *rle = NULL;
2628 	int passthrough = (device_get_parent(child) != bus);
2629 	int isdefault = (start == 0UL && end == ~0UL);
2630 
2631 	if (passthrough) {
2632 		return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2633 					  type, rid,
2634 					  start, end, count, flags, cpuid));
2635 	}
2636 
2637 	rle = resource_list_find(rl, type, *rid);
2638 
2639 	if (!rle)
2640 		return(0);		/* no resource of that type/rid */
2641 
2642 	if (rle->res)
2643 		panic("resource_list_alloc: resource entry is busy");
2644 
2645 	if (isdefault) {
2646 		start = rle->start;
2647 		count = max(count, rle->count);
2648 		end = max(rle->end, start + count - 1);
2649 	}
2650 	cpuid = rle->cpuid;
2651 
2652 	rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2653 				      type, rid, start, end, count,
2654 				      flags, cpuid);
2655 
2656 	/*
2657 	 * Record the new range.
2658 	 */
2659 	if (rle->res) {
2660 		rle->start = rman_get_start(rle->res);
2661 		rle->end = rman_get_end(rle->res);
2662 		rle->count = count;
2663 	}
2664 
2665 	return(rle->res);
2666 }
2667 
2668 int
2669 resource_list_release(struct resource_list *rl,
2670 		      device_t bus, device_t child,
2671 		      int type, int rid, struct resource *res)
2672 {
2673 	struct resource_list_entry *rle = NULL;
2674 	int passthrough = (device_get_parent(child) != bus);
2675 	int error;
2676 
2677 	if (passthrough) {
2678 		return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2679 					    type, rid, res));
2680 	}
2681 
2682 	rle = resource_list_find(rl, type, rid);
2683 
2684 	if (!rle)
2685 		panic("resource_list_release: can't find resource");
2686 	if (!rle->res)
2687 		panic("resource_list_release: resource entry is not busy");
2688 
2689 	error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2690 				     type, rid, res);
2691 	if (error)
2692 		return(error);
2693 
2694 	rle->res = NULL;
2695 	return(0);
2696 }
2697 
2698 int
2699 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2700 			 const char *format)
2701 {
2702 	struct resource_list_entry *rle;
2703 	int printed, retval;
2704 
2705 	printed = 0;
2706 	retval = 0;
2707 	/* Yes, this is kinda cheating */
2708 	SLIST_FOREACH(rle, rl, link) {
2709 		if (rle->type == type) {
2710 			if (printed == 0)
2711 				retval += kprintf(" %s ", name);
2712 			else
2713 				retval += kprintf(",");
2714 			printed++;
2715 			retval += kprintf(format, rle->start);
2716 			if (rle->count > 1) {
2717 				retval += kprintf("-");
2718 				retval += kprintf(format, rle->start +
2719 						 rle->count - 1);
2720 			}
2721 		}
2722 	}
2723 	return(retval);
2724 }
2725 
2726 /*
2727  * Generic driver/device identify functions.  These will install a device
2728  * rendezvous point under the parent using the same name as the driver
2729  * name, which will at a later time be probed and attached.
2730  *
2731  * These functions are used when the parent does not 'scan' its bus for
2732  * matching devices, or for the particular devices using these functions,
2733  * or when the device is a pseudo or synthesized device (such as can be
2734  * found under firewire and ppbus).
2735  */
2736 int
2737 bus_generic_identify(driver_t *driver, device_t parent)
2738 {
2739 	if (parent->state == DS_ATTACHED)
2740 		return (0);
2741 	BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2742 	return (0);
2743 }
2744 
2745 int
2746 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2747 {
2748 	if (parent->state == DS_ATTACHED)
2749 		return (0);
2750 	BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2751 	return (0);
2752 }
2753 
2754 /*
2755  * Call DEVICE_IDENTIFY for each driver.
2756  */
2757 int
2758 bus_generic_probe(device_t dev)
2759 {
2760 	devclass_t dc = dev->devclass;
2761 	driverlink_t dl;
2762 
2763 	TAILQ_FOREACH(dl, &dc->drivers, link) {
2764 		DEVICE_IDENTIFY(dl->driver, dev);
2765 	}
2766 
2767 	return(0);
2768 }
2769 
2770 /*
2771  * This is an aweful hack due to the isa bus and autoconf code not
2772  * probing the ISA devices until after everything else has configured.
2773  * The ISA bus did a dummy attach long ago so we have to set it back
2774  * to an earlier state so the probe thinks its the initial probe and
2775  * not a bus rescan.
2776  *
2777  * XXX remove by properly defering the ISA bus scan.
2778  */
2779 int
2780 bus_generic_probe_hack(device_t dev)
2781 {
2782 	if (dev->state == DS_ATTACHED) {
2783 		dev->state = DS_ALIVE;
2784 		bus_generic_probe(dev);
2785 		dev->state = DS_ATTACHED;
2786 	}
2787 	return (0);
2788 }
2789 
2790 int
2791 bus_generic_attach(device_t dev)
2792 {
2793 	device_t child;
2794 
2795 	TAILQ_FOREACH(child, &dev->children, link) {
2796 		device_probe_and_attach(child);
2797 	}
2798 
2799 	return(0);
2800 }
2801 
2802 int
2803 bus_generic_attach_gpri(device_t dev, u_int gpri)
2804 {
2805 	device_t child;
2806 
2807 	TAILQ_FOREACH(child, &dev->children, link) {
2808 		device_probe_and_attach_gpri(child, gpri);
2809 	}
2810 
2811 	return(0);
2812 }
2813 
2814 int
2815 bus_generic_detach(device_t dev)
2816 {
2817 	device_t child;
2818 	int error;
2819 
2820 	if (dev->state != DS_ATTACHED)
2821 		return(EBUSY);
2822 
2823 	TAILQ_FOREACH(child, &dev->children, link)
2824 		if ((error = device_detach(child)) != 0)
2825 			return(error);
2826 
2827 	return 0;
2828 }
2829 
2830 int
2831 bus_generic_shutdown(device_t dev)
2832 {
2833 	device_t child;
2834 
2835 	TAILQ_FOREACH(child, &dev->children, link)
2836 		device_shutdown(child);
2837 
2838 	return(0);
2839 }
2840 
2841 int
2842 bus_generic_suspend(device_t dev)
2843 {
2844 	int error;
2845 	device_t child, child2;
2846 
2847 	TAILQ_FOREACH(child, &dev->children, link) {
2848 		error = DEVICE_SUSPEND(child);
2849 		if (error) {
2850 			for (child2 = TAILQ_FIRST(&dev->children);
2851 			     child2 && child2 != child;
2852 			     child2 = TAILQ_NEXT(child2, link))
2853 				DEVICE_RESUME(child2);
2854 			return(error);
2855 		}
2856 	}
2857 	return(0);
2858 }
2859 
2860 int
2861 bus_generic_resume(device_t dev)
2862 {
2863 	device_t child;
2864 
2865 	TAILQ_FOREACH(child, &dev->children, link)
2866 		DEVICE_RESUME(child);
2867 		/* if resume fails, there's nothing we can usefully do... */
2868 
2869 	return(0);
2870 }
2871 
2872 int
2873 bus_print_child_header(device_t dev, device_t child)
2874 {
2875 	int retval = 0;
2876 
2877 	if (device_get_desc(child))
2878 		retval += device_printf(child, "<%s>", device_get_desc(child));
2879 	else
2880 		retval += kprintf("%s", device_get_nameunit(child));
2881 	if (bootverbose) {
2882 		if (child->state != DS_ATTACHED)
2883 			kprintf(" [tentative]");
2884 		else
2885 			kprintf(" [attached!]");
2886 	}
2887 	return(retval);
2888 }
2889 
2890 int
2891 bus_print_child_footer(device_t dev, device_t child)
2892 {
2893 	return(kprintf(" on %s\n", device_get_nameunit(dev)));
2894 }
2895 
2896 device_t
2897 bus_generic_add_child(device_t dev, device_t child, int order,
2898 		      const char *name, int unit)
2899 {
2900 	if (dev->parent)
2901 		dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2902 	else
2903 		dev = device_add_child_ordered(child, order, name, unit);
2904 	return(dev);
2905 
2906 }
2907 
2908 int
2909 bus_generic_print_child(device_t dev, device_t child)
2910 {
2911 	int retval = 0;
2912 
2913 	retval += bus_print_child_header(dev, child);
2914 	retval += bus_print_child_footer(dev, child);
2915 
2916 	return(retval);
2917 }
2918 
2919 int
2920 bus_generic_read_ivar(device_t dev, device_t child, int index,
2921 		      uintptr_t * result)
2922 {
2923 	int error;
2924 
2925 	if (dev->parent)
2926 		error = BUS_READ_IVAR(dev->parent, child, index, result);
2927 	else
2928 		error = ENOENT;
2929 	return (error);
2930 }
2931 
2932 int
2933 bus_generic_write_ivar(device_t dev, device_t child, int index,
2934 		       uintptr_t value)
2935 {
2936 	int error;
2937 
2938 	if (dev->parent)
2939 		error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2940 	else
2941 		error = ENOENT;
2942 	return (error);
2943 }
2944 
2945 /*
2946  * Resource list are used for iterations, do not recurse.
2947  */
2948 struct resource_list *
2949 bus_generic_get_resource_list(device_t dev, device_t child)
2950 {
2951 	return (NULL);
2952 }
2953 
2954 void
2955 bus_generic_driver_added(device_t dev, driver_t *driver)
2956 {
2957 	device_t child;
2958 
2959 	DEVICE_IDENTIFY(driver, dev);
2960 	TAILQ_FOREACH(child, &dev->children, link) {
2961 		if (child->state == DS_NOTPRESENT)
2962 			device_probe_and_attach(child);
2963 	}
2964 }
2965 
2966 int
2967 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2968     int flags, driver_intr_t *intr, void *arg, void **cookiep,
2969     lwkt_serialize_t serializer, const char *desc)
2970 {
2971 	/* Propagate up the bus hierarchy until someone handles it. */
2972 	if (dev->parent) {
2973 		return BUS_SETUP_INTR(dev->parent, child, irq, flags,
2974 		    intr, arg, cookiep, serializer, desc);
2975 	} else {
2976 		return EINVAL;
2977 	}
2978 }
2979 
2980 int
2981 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2982 			  void *cookie)
2983 {
2984 	/* Propagate up the bus hierarchy until someone handles it. */
2985 	if (dev->parent)
2986 		return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2987 	else
2988 		return(EINVAL);
2989 }
2990 
2991 int
2992 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2993 {
2994 	if (dev->parent)
2995 		return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2996 	else
2997 		return(0);
2998 }
2999 
3000 void
3001 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
3002 {
3003 	if (dev->parent)
3004 		BUS_ENABLE_INTR(dev->parent, child, cookie);
3005 }
3006 
3007 int
3008 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
3009     enum intr_polarity pol)
3010 {
3011 	/* Propagate up the bus hierarchy until someone handles it. */
3012 	if (dev->parent)
3013 		return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
3014 	else
3015 		return(EINVAL);
3016 }
3017 
3018 struct resource *
3019 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3020     u_long start, u_long end, u_long count, u_int flags, int cpuid)
3021 {
3022 	/* Propagate up the bus hierarchy until someone handles it. */
3023 	if (dev->parent)
3024 		return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3025 					   start, end, count, flags, cpuid));
3026 	else
3027 		return(NULL);
3028 }
3029 
3030 int
3031 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3032 			     struct resource *r)
3033 {
3034 	/* Propagate up the bus hierarchy until someone handles it. */
3035 	if (dev->parent)
3036 		return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
3037 	else
3038 		return(EINVAL);
3039 }
3040 
3041 int
3042 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3043 			      struct resource *r)
3044 {
3045 	/* Propagate up the bus hierarchy until someone handles it. */
3046 	if (dev->parent)
3047 		return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
3048 	else
3049 		return(EINVAL);
3050 }
3051 
3052 int
3053 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3054 				int rid, struct resource *r)
3055 {
3056 	/* Propagate up the bus hierarchy until someone handles it. */
3057 	if (dev->parent)
3058 		return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3059 					       r));
3060 	else
3061 		return(EINVAL);
3062 }
3063 
3064 int
3065 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
3066 			 u_long *startp, u_long *countp)
3067 {
3068 	int error;
3069 
3070 	error = ENOENT;
3071 	if (dev->parent) {
3072 		error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
3073 					 startp, countp);
3074 	}
3075 	return (error);
3076 }
3077 
3078 int
3079 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
3080 			u_long start, u_long count, int cpuid)
3081 {
3082 	int error;
3083 
3084 	error = EINVAL;
3085 	if (dev->parent) {
3086 		error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
3087 					 start, count, cpuid);
3088 	}
3089 	return (error);
3090 }
3091 
3092 void
3093 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
3094 {
3095 	if (dev->parent)
3096 		BUS_DELETE_RESOURCE(dev, child, type, rid);
3097 }
3098 
3099 /**
3100  * @brief Helper function for implementing BUS_GET_DMA_TAG().
3101  *
3102  * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3103  * BUS_GET_DMA_TAG() method of the parent of @p dev.
3104  */
3105 bus_dma_tag_t
3106 bus_generic_get_dma_tag(device_t dev, device_t child)
3107 {
3108 
3109 	/* Propagate up the bus hierarchy until someone handles it. */
3110 	if (dev->parent != NULL)
3111 		return (BUS_GET_DMA_TAG(dev->parent, child));
3112 	return (NULL);
3113 }
3114 
3115 int
3116 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3117     u_long *startp, u_long *countp)
3118 {
3119 	struct resource_list *rl = NULL;
3120 	struct resource_list_entry *rle = NULL;
3121 
3122 	rl = BUS_GET_RESOURCE_LIST(dev, child);
3123 	if (!rl)
3124 		return(EINVAL);
3125 
3126 	rle = resource_list_find(rl, type, rid);
3127 	if (!rle)
3128 		return(ENOENT);
3129 
3130 	if (startp)
3131 		*startp = rle->start;
3132 	if (countp)
3133 		*countp = rle->count;
3134 
3135 	return(0);
3136 }
3137 
3138 int
3139 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3140     u_long start, u_long count, int cpuid)
3141 {
3142 	struct resource_list *rl = NULL;
3143 
3144 	rl = BUS_GET_RESOURCE_LIST(dev, child);
3145 	if (!rl)
3146 		return(EINVAL);
3147 
3148 	resource_list_add(rl, type, rid, start, (start + count - 1), count,
3149 	    cpuid);
3150 
3151 	return(0);
3152 }
3153 
3154 void
3155 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3156 {
3157 	struct resource_list *rl = NULL;
3158 
3159 	rl = BUS_GET_RESOURCE_LIST(dev, child);
3160 	if (!rl)
3161 		return;
3162 
3163 	resource_list_delete(rl, type, rid);
3164 }
3165 
3166 int
3167 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3168     int rid, struct resource *r)
3169 {
3170 	struct resource_list *rl = NULL;
3171 
3172 	rl = BUS_GET_RESOURCE_LIST(dev, child);
3173 	if (!rl)
3174 		return(EINVAL);
3175 
3176 	return(resource_list_release(rl, dev, child, type, rid, r));
3177 }
3178 
3179 struct resource *
3180 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3181     int *rid, u_long start, u_long end, u_long count, u_int flags, int cpuid)
3182 {
3183 	struct resource_list *rl = NULL;
3184 
3185 	rl = BUS_GET_RESOURCE_LIST(dev, child);
3186 	if (!rl)
3187 		return(NULL);
3188 
3189 	return(resource_list_alloc(rl, dev, child, type, rid,
3190 	    start, end, count, flags, cpuid));
3191 }
3192 
3193 int
3194 bus_generic_child_present(device_t bus, device_t child)
3195 {
3196 	return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
3197 }
3198 
3199 
3200 /*
3201  * Some convenience functions to make it easier for drivers to use the
3202  * resource-management functions.  All these really do is hide the
3203  * indirection through the parent's method table, making for slightly
3204  * less-wordy code.  In the future, it might make sense for this code
3205  * to maintain some sort of a list of resources allocated by each device.
3206  */
3207 int
3208 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3209     struct resource **res)
3210 {
3211 	int i;
3212 
3213 	for (i = 0; rs[i].type != -1; i++)
3214 	        res[i] = NULL;
3215 	for (i = 0; rs[i].type != -1; i++) {
3216 		res[i] = bus_alloc_resource_any(dev,
3217 		    rs[i].type, &rs[i].rid, rs[i].flags);
3218 		if (res[i] == NULL) {
3219 			bus_release_resources(dev, rs, res);
3220 			return (ENXIO);
3221 		}
3222 	}
3223 	return (0);
3224 }
3225 
3226 void
3227 bus_release_resources(device_t dev, const struct resource_spec *rs,
3228     struct resource **res)
3229 {
3230 	int i;
3231 
3232 	for (i = 0; rs[i].type != -1; i++)
3233 		if (res[i] != NULL) {
3234 			bus_release_resource(
3235 			    dev, rs[i].type, rs[i].rid, res[i]);
3236 			res[i] = NULL;
3237 		}
3238 }
3239 
3240 struct resource *
3241 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3242 		   u_long count, u_int flags)
3243 {
3244 	if (dev->parent == NULL)
3245 		return(0);
3246 	return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3247 				  count, flags, -1));
3248 }
3249 
3250 struct resource *
3251 bus_alloc_legacy_irq_resource(device_t dev, int *rid, u_long irq, u_int flags)
3252 {
3253 	if (dev->parent == NULL)
3254 		return(0);
3255 	return BUS_ALLOC_RESOURCE(dev->parent, dev, SYS_RES_IRQ, rid,
3256 	    irq, irq, 1, flags, machintr_legacy_intr_cpuid(irq));
3257 }
3258 
3259 int
3260 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3261 {
3262 	if (dev->parent == NULL)
3263 		return(EINVAL);
3264 	return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3265 }
3266 
3267 int
3268 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3269 {
3270 	if (dev->parent == NULL)
3271 		return(EINVAL);
3272 	return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3273 }
3274 
3275 int
3276 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3277 {
3278 	if (dev->parent == NULL)
3279 		return(EINVAL);
3280 	return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3281 }
3282 
3283 int
3284 bus_setup_intr_descr(device_t dev, struct resource *r, int flags,
3285     driver_intr_t handler, void *arg, void **cookiep,
3286     lwkt_serialize_t serializer, const char *desc)
3287 {
3288 	if (dev->parent == NULL)
3289 		return EINVAL;
3290 	return BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
3291 	    cookiep, serializer, desc);
3292 }
3293 
3294 int
3295 bus_setup_intr(device_t dev, struct resource *r, int flags,
3296     driver_intr_t handler, void *arg, void **cookiep,
3297     lwkt_serialize_t serializer)
3298 {
3299 	return bus_setup_intr_descr(dev, r, flags, handler, arg, cookiep,
3300 	    serializer, NULL);
3301 }
3302 
3303 int
3304 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3305 {
3306 	if (dev->parent == NULL)
3307 		return(EINVAL);
3308 	return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3309 }
3310 
3311 void
3312 bus_enable_intr(device_t dev, void *cookie)
3313 {
3314 	if (dev->parent)
3315 		BUS_ENABLE_INTR(dev->parent, dev, cookie);
3316 }
3317 
3318 int
3319 bus_disable_intr(device_t dev, void *cookie)
3320 {
3321 	if (dev->parent)
3322 		return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
3323 	else
3324 		return(0);
3325 }
3326 
3327 int
3328 bus_set_resource(device_t dev, int type, int rid,
3329 		 u_long start, u_long count, int cpuid)
3330 {
3331 	return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3332 				start, count, cpuid));
3333 }
3334 
3335 int
3336 bus_get_resource(device_t dev, int type, int rid,
3337 		 u_long *startp, u_long *countp)
3338 {
3339 	return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3340 				startp, countp));
3341 }
3342 
3343 u_long
3344 bus_get_resource_start(device_t dev, int type, int rid)
3345 {
3346 	u_long start, count;
3347 	int error;
3348 
3349 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3350 				 &start, &count);
3351 	if (error)
3352 		return(0);
3353 	return(start);
3354 }
3355 
3356 u_long
3357 bus_get_resource_count(device_t dev, int type, int rid)
3358 {
3359 	u_long start, count;
3360 	int error;
3361 
3362 	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3363 				 &start, &count);
3364 	if (error)
3365 		return(0);
3366 	return(count);
3367 }
3368 
3369 void
3370 bus_delete_resource(device_t dev, int type, int rid)
3371 {
3372 	BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3373 }
3374 
3375 int
3376 bus_child_present(device_t child)
3377 {
3378 	return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3379 }
3380 
3381 int
3382 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3383 {
3384 	device_t parent;
3385 
3386 	parent = device_get_parent(child);
3387 	if (parent == NULL) {
3388 		*buf = '\0';
3389 		return (0);
3390 	}
3391 	return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3392 }
3393 
3394 int
3395 bus_child_location_str(device_t child, char *buf, size_t buflen)
3396 {
3397 	device_t parent;
3398 
3399 	parent = device_get_parent(child);
3400 	if (parent == NULL) {
3401 		*buf = '\0';
3402 		return (0);
3403 	}
3404 	return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3405 }
3406 
3407 /**
3408  * @brief Wrapper function for BUS_GET_DMA_TAG().
3409  *
3410  * This function simply calls the BUS_GET_DMA_TAG() method of the
3411  * parent of @p dev.
3412  */
3413 bus_dma_tag_t
3414 bus_get_dma_tag(device_t dev)
3415 {
3416 	device_t parent;
3417 
3418 	parent = device_get_parent(dev);
3419 	if (parent == NULL)
3420 		return (NULL);
3421 	return (BUS_GET_DMA_TAG(parent, dev));
3422 }
3423 
3424 static int
3425 root_print_child(device_t dev, device_t child)
3426 {
3427 	return(0);
3428 }
3429 
3430 static int
3431 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3432 		void **cookiep, lwkt_serialize_t serializer, const char *desc)
3433 {
3434 	/*
3435 	 * If an interrupt mapping gets to here something bad has happened.
3436 	 */
3437 	panic("root_setup_intr");
3438 }
3439 
3440 /*
3441  * If we get here, assume that the device is permanant and really is
3442  * present in the system.  Removable bus drivers are expected to intercept
3443  * this call long before it gets here.  We return -1 so that drivers that
3444  * really care can check vs -1 or some ERRNO returned higher in the food
3445  * chain.
3446  */
3447 static int
3448 root_child_present(device_t dev, device_t child)
3449 {
3450 	return(-1);
3451 }
3452 
3453 /*
3454  * XXX NOTE! other defaults may be set in bus_if.m
3455  */
3456 static kobj_method_t root_methods[] = {
3457 	/* Device interface */
3458 	KOBJMETHOD(device_shutdown,	bus_generic_shutdown),
3459 	KOBJMETHOD(device_suspend,	bus_generic_suspend),
3460 	KOBJMETHOD(device_resume,	bus_generic_resume),
3461 
3462 	/* Bus interface */
3463 	KOBJMETHOD(bus_add_child,	bus_generic_add_child),
3464 	KOBJMETHOD(bus_print_child,	root_print_child),
3465 	KOBJMETHOD(bus_read_ivar,	bus_generic_read_ivar),
3466 	KOBJMETHOD(bus_write_ivar,	bus_generic_write_ivar),
3467 	KOBJMETHOD(bus_setup_intr,	root_setup_intr),
3468 	KOBJMETHOD(bus_child_present,   root_child_present),
3469 
3470 	KOBJMETHOD_END
3471 };
3472 
3473 static driver_t root_driver = {
3474 	"root",
3475 	root_methods,
3476 	1,			/* no softc */
3477 };
3478 
3479 device_t	root_bus;
3480 devclass_t	root_devclass;
3481 
3482 static int
3483 root_bus_module_handler(module_t mod, int what, void* arg)
3484 {
3485 	switch (what) {
3486 	case MOD_LOAD:
3487 		TAILQ_INIT(&bus_data_devices);
3488 		root_bus = make_device(NULL, "root", 0);
3489 		root_bus->desc = "System root bus";
3490 		kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3491 		root_bus->driver = &root_driver;
3492 		root_bus->state = DS_ALIVE;
3493 		root_devclass = devclass_find_internal("root", NULL, FALSE);
3494 		devinit();
3495 		return(0);
3496 
3497 	case MOD_SHUTDOWN:
3498 		device_shutdown(root_bus);
3499 		return(0);
3500 	default:
3501 		return(0);
3502 	}
3503 }
3504 
3505 static moduledata_t root_bus_mod = {
3506 	"rootbus",
3507 	root_bus_module_handler,
3508 	0
3509 };
3510 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3511 
3512 void
3513 root_bus_configure(void)
3514 {
3515 	int warncount;
3516 	device_t dev;
3517 
3518 	PDEBUG(("."));
3519 
3520 	/*
3521 	 * handle device_identify based device attachments to the root_bus
3522 	 * (typically nexus).
3523 	 */
3524 	bus_generic_probe(root_bus);
3525 
3526 	/*
3527 	 * Probe and attach the devices under root_bus.
3528 	 */
3529 	TAILQ_FOREACH(dev, &root_bus->children, link) {
3530 		device_probe_and_attach(dev);
3531 	}
3532 
3533 	/*
3534 	 * Wait for all asynchronous attaches to complete.  If we don't
3535 	 * our legacy ISA bus scan could steal device unit numbers or
3536 	 * even I/O ports.
3537 	 */
3538 	warncount = 10;
3539 	if (numasyncthreads)
3540 		kprintf("Waiting for async drivers to attach\n");
3541 	while (numasyncthreads > 0) {
3542 		if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3543 			--warncount;
3544 		if (warncount == 0) {
3545 			kprintf("Warning: Still waiting for %d "
3546 				"drivers to attach\n", numasyncthreads);
3547 		} else if (warncount == -30) {
3548 			kprintf("Giving up on %d drivers\n", numasyncthreads);
3549 			break;
3550 		}
3551 	}
3552 	root_bus->state = DS_ATTACHED;
3553 }
3554 
3555 int
3556 driver_module_handler(module_t mod, int what, void *arg)
3557 {
3558 	int error;
3559 	struct driver_module_data *dmd;
3560 	devclass_t bus_devclass;
3561 	kobj_class_t driver;
3562         const char *parentname;
3563 
3564 	dmd = (struct driver_module_data *)arg;
3565 	bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3566 	error = 0;
3567 
3568 	switch (what) {
3569 	case MOD_LOAD:
3570 		if (dmd->dmd_chainevh)
3571 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3572 
3573 		driver = dmd->dmd_driver;
3574 		PDEBUG(("Loading module: driver %s on bus %s",
3575 		        DRIVERNAME(driver), dmd->dmd_busname));
3576 
3577 		/*
3578 		 * If the driver has any base classes, make the
3579 		 * devclass inherit from the devclass of the driver's
3580 		 * first base class. This will allow the system to
3581 		 * search for drivers in both devclasses for children
3582 		 * of a device using this driver.
3583 		 */
3584 		if (driver->baseclasses)
3585 			parentname = driver->baseclasses[0]->name;
3586 		else
3587 			parentname = NULL;
3588 		*dmd->dmd_devclass = devclass_find_internal(driver->name,
3589 							    parentname, TRUE);
3590 
3591 		error = devclass_add_driver(bus_devclass, driver);
3592 		if (error)
3593 			break;
3594 		break;
3595 
3596 	case MOD_UNLOAD:
3597 		PDEBUG(("Unloading module: driver %s from bus %s",
3598 			DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3599 		error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3600 
3601 		if (!error && dmd->dmd_chainevh)
3602 			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3603 		break;
3604 	}
3605 
3606 	return (error);
3607 }
3608 
3609 #ifdef BUS_DEBUG
3610 
3611 /*
3612  * The _short versions avoid iteration by not calling anything that prints
3613  * more than oneliners. I love oneliners.
3614  */
3615 
3616 static void
3617 print_device_short(device_t dev, int indent)
3618 {
3619 	if (!dev)
3620 		return;
3621 
3622 	indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3623 		      dev->unit, dev->desc,
3624 		      (dev->parent? "":"no "),
3625 		      (TAILQ_EMPTY(&dev->children)? "no ":""),
3626 		      (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3627 		      (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3628 		      (dev->flags&DF_WILDCARD? "wildcard,":""),
3629 		      (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3630 		      (dev->ivars? "":"no "),
3631 		      (dev->softc? "":"no "),
3632 		      dev->busy));
3633 }
3634 
3635 static void
3636 print_device(device_t dev, int indent)
3637 {
3638 	if (!dev)
3639 		return;
3640 
3641 	print_device_short(dev, indent);
3642 
3643 	indentprintf(("Parent:\n"));
3644 	print_device_short(dev->parent, indent+1);
3645 	indentprintf(("Driver:\n"));
3646 	print_driver_short(dev->driver, indent+1);
3647 	indentprintf(("Devclass:\n"));
3648 	print_devclass_short(dev->devclass, indent+1);
3649 }
3650 
3651 /*
3652  * Print the device and all its children (indented).
3653  */
3654 void
3655 print_device_tree_short(device_t dev, int indent)
3656 {
3657 	device_t child;
3658 
3659 	if (!dev)
3660 		return;
3661 
3662 	print_device_short(dev, indent);
3663 
3664 	TAILQ_FOREACH(child, &dev->children, link)
3665 		print_device_tree_short(child, indent+1);
3666 }
3667 
3668 /*
3669  * Print the device and all its children (indented).
3670  */
3671 void
3672 print_device_tree(device_t dev, int indent)
3673 {
3674 	device_t child;
3675 
3676 	if (!dev)
3677 		return;
3678 
3679 	print_device(dev, indent);
3680 
3681 	TAILQ_FOREACH(child, &dev->children, link)
3682 		print_device_tree(child, indent+1);
3683 }
3684 
3685 static void
3686 print_driver_short(driver_t *driver, int indent)
3687 {
3688 	if (!driver)
3689 		return;
3690 
3691 	indentprintf(("driver %s: softc size = %zu\n",
3692 		      driver->name, driver->size));
3693 }
3694 
3695 static void
3696 print_driver(driver_t *driver, int indent)
3697 {
3698 	if (!driver)
3699 		return;
3700 
3701 	print_driver_short(driver, indent);
3702 }
3703 
3704 
3705 static void
3706 print_driver_list(driver_list_t drivers, int indent)
3707 {
3708 	driverlink_t driver;
3709 
3710 	TAILQ_FOREACH(driver, &drivers, link)
3711 		print_driver(driver->driver, indent);
3712 }
3713 
3714 static void
3715 print_devclass_short(devclass_t dc, int indent)
3716 {
3717 	if (!dc)
3718 		return;
3719 
3720 	indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3721 }
3722 
3723 static void
3724 print_devclass(devclass_t dc, int indent)
3725 {
3726 	int i;
3727 
3728 	if (!dc)
3729 		return;
3730 
3731 	print_devclass_short(dc, indent);
3732 	indentprintf(("Drivers:\n"));
3733 	print_driver_list(dc->drivers, indent+1);
3734 
3735 	indentprintf(("Devices:\n"));
3736 	for (i = 0; i < dc->maxunit; i++)
3737 		if (dc->devices[i])
3738 			print_device(dc->devices[i], indent+1);
3739 }
3740 
3741 void
3742 print_devclass_list_short(void)
3743 {
3744 	devclass_t dc;
3745 
3746 	kprintf("Short listing of devclasses, drivers & devices:\n");
3747 	TAILQ_FOREACH(dc, &devclasses, link) {
3748 		print_devclass_short(dc, 0);
3749 	}
3750 }
3751 
3752 void
3753 print_devclass_list(void)
3754 {
3755 	devclass_t dc;
3756 
3757 	kprintf("Full listing of devclasses, drivers & devices:\n");
3758 	TAILQ_FOREACH(dc, &devclasses, link) {
3759 		print_devclass(dc, 0);
3760 	}
3761 }
3762 
3763 #endif
3764 
3765 /*
3766  * Check to see if a device is disabled via a disabled hint.
3767  */
3768 int
3769 resource_disabled(const char *name, int unit)
3770 {
3771 	int error, value;
3772 
3773 	error = resource_int_value(name, unit, "disabled", &value);
3774 	if (error)
3775 	       return(0);
3776 	return(value);
3777 }
3778 
3779 /*
3780  * User-space access to the device tree.
3781  *
3782  * We implement a small set of nodes:
3783  *
3784  * hw.bus			Single integer read method to obtain the
3785  *				current generation count.
3786  * hw.bus.devices		Reads the entire device tree in flat space.
3787  * hw.bus.rman			Resource manager interface
3788  *
3789  * We might like to add the ability to scan devclasses and/or drivers to
3790  * determine what else is currently loaded/available.
3791  */
3792 
3793 static int
3794 sysctl_bus(SYSCTL_HANDLER_ARGS)
3795 {
3796 	struct u_businfo	ubus;
3797 
3798 	ubus.ub_version = BUS_USER_VERSION;
3799 	ubus.ub_generation = bus_data_generation;
3800 
3801 	return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3802 }
3803 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3804     "bus-related data");
3805 
3806 static int
3807 sysctl_devices(SYSCTL_HANDLER_ARGS)
3808 {
3809 	int			*name = (int *)arg1;
3810 	u_int			namelen = arg2;
3811 	int			index;
3812 	device_t		dev;
3813 	struct u_device		udev;	/* XXX this is a bit big */
3814 	int			error;
3815 
3816 	if (namelen != 2)
3817 		return (EINVAL);
3818 
3819 	if (bus_data_generation_check(name[0]))
3820 		return (EINVAL);
3821 
3822 	index = name[1];
3823 
3824 	/*
3825 	 * Scan the list of devices, looking for the requested index.
3826 	 */
3827 	TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3828 		if (index-- == 0)
3829 			break;
3830 	}
3831 	if (dev == NULL)
3832 		return (ENOENT);
3833 
3834 	/*
3835 	 * Populate the return array.
3836 	 */
3837 	bzero(&udev, sizeof(udev));
3838 	udev.dv_handle = (uintptr_t)dev;
3839 	udev.dv_parent = (uintptr_t)dev->parent;
3840 	if (dev->nameunit != NULL)
3841 		strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3842 	if (dev->desc != NULL)
3843 		strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3844 	if (dev->driver != NULL && dev->driver->name != NULL)
3845 		strlcpy(udev.dv_drivername, dev->driver->name,
3846 		    sizeof(udev.dv_drivername));
3847 	bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3848 	bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3849 	udev.dv_devflags = dev->devflags;
3850 	udev.dv_flags = dev->flags;
3851 	udev.dv_state = dev->state;
3852 	error = SYSCTL_OUT(req, &udev, sizeof(udev));
3853 	return (error);
3854 }
3855 
3856 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3857     "system device tree");
3858 
3859 int
3860 bus_data_generation_check(int generation)
3861 {
3862 	if (generation != bus_data_generation)
3863 		return (1);
3864 
3865 	/* XXX generate optimised lists here? */
3866 	return (0);
3867 }
3868 
3869 void
3870 bus_data_generation_update(void)
3871 {
3872 	bus_data_generation++;
3873 }
3874 
3875 const char *
3876 intr_str_polarity(enum intr_polarity pola)
3877 {
3878 	switch (pola) {
3879 	case INTR_POLARITY_LOW:
3880 		return "low";
3881 
3882 	case INTR_POLARITY_HIGH:
3883 		return "high";
3884 
3885 	case INTR_POLARITY_CONFORM:
3886 		return "conform";
3887 	}
3888 	return "unknown";
3889 }
3890 
3891 const char *
3892 intr_str_trigger(enum intr_trigger trig)
3893 {
3894 	switch (trig) {
3895 	case INTR_TRIGGER_EDGE:
3896 		return "edge";
3897 
3898 	case INTR_TRIGGER_LEVEL:
3899 		return "level";
3900 
3901 	case INTR_TRIGGER_CONFORM:
3902 		return "conform";
3903 	}
3904 	return "unknown";
3905 }
3906 
3907 int
3908 device_getenv_int(device_t dev, const char *knob, int def)
3909 {
3910 	char env[128];
3911 
3912 	/* Deprecated; for compat */
3913 	ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3914 	kgetenv_int(env, &def);
3915 
3916 	/* Prefer dev.driver.unit.knob */
3917 	ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3918 	    device_get_name(dev), device_get_unit(dev), knob);
3919 	kgetenv_int(env, &def);
3920 
3921 	return def;
3922 }
3923 
3924 void
3925 device_getenv_string(device_t dev, const char *knob, char * __restrict data,
3926     int dlen, const char * __restrict def)
3927 {
3928 	char env[128];
3929 
3930 	strlcpy(data, def, dlen);
3931 
3932 	/* Deprecated; for compat */
3933 	ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3934 	kgetenv_string(env, data, dlen);
3935 
3936 	/* Prefer dev.driver.unit.knob */
3937 	ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3938 	    device_get_name(dev), device_get_unit(dev), knob);
3939 	kgetenv_string(env, data, dlen);
3940 }
3941