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