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