subr_bus.c revision 132199
1/*-
2 * Copyright (c) 1997,1998,2003 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/subr_bus.c 132199 2004-07-15 08:26:07Z phk $");
29
30#include "opt_bus.h"
31
32#define __RMAN_RESOURCE_VISIBLE
33#include <sys/param.h>
34#include <sys/conf.h>
35#include <sys/filio.h>
36#include <sys/lock.h>
37#include <sys/kernel.h>
38#include <sys/kobj.h>
39#include <sys/malloc.h>
40#include <sys/module.h>
41#include <sys/mutex.h>
42#include <sys/poll.h>
43#include <sys/proc.h>
44#include <sys/condvar.h>
45#include <sys/queue.h>
46#include <machine/bus.h>
47#include <sys/rman.h>
48#include <sys/selinfo.h>
49#include <sys/signalvar.h>
50#include <sys/sysctl.h>
51#include <sys/systm.h>
52#include <sys/uio.h>
53#include <sys/bus.h>
54
55#include <machine/stdarg.h>
56
57#include <vm/uma.h>
58
59SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
60SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
61
62/*
63 * Used to attach drivers to devclasses.
64 */
65typedef struct driverlink *driverlink_t;
66struct driverlink {
67	kobj_class_t	driver;
68	TAILQ_ENTRY(driverlink) link;	/* list of drivers in devclass */
69};
70
71/*
72 * Forward declarations
73 */
74typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
75typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
76typedef TAILQ_HEAD(device_list, device) device_list_t;
77
78struct devclass {
79	TAILQ_ENTRY(devclass) link;
80	devclass_t	parent;		/* parent in devclass hierarchy */
81	driver_list_t	drivers;     /* bus devclasses store drivers for bus */
82	char		*name;
83	device_t	*devices;	/* array of devices indexed by unit */
84	int		maxunit;	/* size of devices array */
85
86	struct sysctl_ctx_list sysctl_ctx;
87	struct sysctl_oid *sysctl_tree;
88};
89
90/*
91 * Implementation of device.
92 */
93struct device {
94	/*
95	 * A device is a kernel object. The first field must be the
96	 * current ops table for the object.
97	 */
98	KOBJ_FIELDS;
99
100	/*
101	 * Device hierarchy.
102	 */
103	TAILQ_ENTRY(device)	link;		/* list of devices in parent */
104	TAILQ_ENTRY(device)	devlink;	/* global device list membership */
105	device_t	parent;
106	device_list_t	children;	/* list of subordinate devices */
107
108	/*
109	 * Details of this device.
110	 */
111	driver_t	*driver;
112	devclass_t	devclass;	/* device class which we are in */
113	int		unit;
114	char*		nameunit;	/* name+unit e.g. foodev0 */
115	char*		desc;		/* driver specific description */
116	int		busy;		/* count of calls to device_busy() */
117	device_state_t	state;
118	u_int32_t	devflags;  /* api level flags for device_get_flags() */
119	u_short		flags;
120#define	DF_ENABLED	1	/* device should be probed/attached */
121#define	DF_FIXEDCLASS	2	/* devclass specified at create time */
122#define	DF_WILDCARD	4	/* unit was originally wildcard */
123#define	DF_DESCMALLOCED	8	/* description was malloced */
124#define	DF_QUIET	16	/* don't print verbose attach message */
125#define	DF_DONENOMATCH	32	/* don't execute DEVICE_NOMATCH again */
126#define	DF_EXTERNALSOFTC 64	/* softc not allocated by us */
127	u_char	order;		/* order from device_add_child_ordered() */
128	u_char	pad;
129	void	*ivars;
130	void	*softc;
131
132	struct sysctl_ctx_list sysctl_ctx;
133	struct sysctl_oid *sysctl_tree;
134};
135
136struct device_op_desc {
137	unsigned int	offset;	/* offset in driver ops */
138	struct method*	method;	/* internal method implementation */
139	devop_t		deflt;	/* default implementation */
140	const char*	name;	/* unique name (for registration) */
141};
142
143static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
144static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
145
146#ifdef BUS_DEBUG
147
148static int bus_debug = 1;
149TUNABLE_INT("bus.debug", &bus_debug);
150SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
151    "Debug bus code");
152
153#define PDEBUG(a)	if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
154#define DEVICENAME(d)	((d)? device_get_name(d): "no device")
155#define DRIVERNAME(d)	((d)? d->name : "no driver")
156#define DEVCLANAME(d)	((d)? d->name : "no devclass")
157
158/* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
159 * prevent syslog from deleting initial spaces
160 */
161#define indentprintf(p)	do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf("  "); printf p ; } while (0)
162
163static void print_device_short(device_t dev, int indent);
164static void print_device(device_t dev, int indent);
165void print_device_tree_short(device_t dev, int indent);
166void print_device_tree(device_t dev, int indent);
167static void print_driver_short(driver_t *driver, int indent);
168static void print_driver(driver_t *driver, int indent);
169static void print_driver_list(driver_list_t drivers, int indent);
170static void print_devclass_short(devclass_t dc, int indent);
171static void print_devclass(devclass_t dc, int indent);
172void print_devclass_list_short(void);
173void print_devclass_list(void);
174
175#else
176/* Make the compiler ignore the function calls */
177#define PDEBUG(a)			/* nop */
178#define DEVICENAME(d)			/* nop */
179#define DRIVERNAME(d)			/* nop */
180#define DEVCLANAME(d)			/* nop */
181
182#define print_device_short(d,i)		/* nop */
183#define print_device(d,i)		/* nop */
184#define print_device_tree_short(d,i)	/* nop */
185#define print_device_tree(d,i)		/* nop */
186#define print_driver_short(d,i)		/* nop */
187#define print_driver(d,i)		/* nop */
188#define print_driver_list(d,i)		/* nop */
189#define print_devclass_short(d,i)	/* nop */
190#define print_devclass(d,i)		/* nop */
191#define print_devclass_list_short()	/* nop */
192#define print_devclass_list()		/* nop */
193#endif
194
195/*
196 * dev sysctl tree
197 */
198
199enum {
200	DEVCLASS_SYSCTL_PARENT,
201};
202
203static int
204devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
205{
206	devclass_t dc = (devclass_t)arg1;
207	const char *value;
208	char *buf;
209	int error;
210
211	buf = NULL;
212	switch (arg2) {
213	case DEVCLASS_SYSCTL_PARENT:
214		value = dc->parent ? dc->parent->name : "";
215		break;
216	default:
217		return (EINVAL);
218	}
219	error = SYSCTL_OUT(req, value, strlen(value));
220	if (buf != NULL)
221		free(buf, M_BUS);
222	return (error);
223}
224
225static void
226devclass_sysctl_init(devclass_t dc)
227{
228
229	if (dc->sysctl_tree != NULL)
230		return;
231	sysctl_ctx_init(&dc->sysctl_ctx);
232	dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
233	    SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
234	    CTLFLAG_RD, 0, "");
235	SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
236	    OID_AUTO, "%parent", CTLFLAG_RD,
237	    dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
238	    "parent class");
239}
240
241enum {
242	DEVICE_SYSCTL_DESC,
243	DEVICE_SYSCTL_DRIVER,
244	DEVICE_SYSCTL_LOCATION,
245	DEVICE_SYSCTL_PNPINFO,
246	DEVICE_SYSCTL_PARENT,
247};
248
249static int
250device_sysctl_handler(SYSCTL_HANDLER_ARGS)
251{
252	device_t dev = (device_t)arg1;
253	const char *value;
254	char *buf;
255	int error;
256
257	buf = NULL;
258	switch (arg2) {
259	case DEVICE_SYSCTL_DESC:
260		value = dev->desc ? dev->desc : "";
261		break;
262	case DEVICE_SYSCTL_DRIVER:
263		value = dev->driver ? dev->driver->name : "";
264		break;
265	case DEVICE_SYSCTL_LOCATION:
266		value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
267		bus_child_location_str(dev, buf, 1024);
268		break;
269	case DEVICE_SYSCTL_PNPINFO:
270		value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
271		bus_child_pnpinfo_str(dev, buf, 1024);
272		break;
273	case DEVICE_SYSCTL_PARENT:
274		value = dev->parent ? dev->parent->nameunit : "";
275		break;
276	default:
277		return (EINVAL);
278	}
279	error = SYSCTL_OUT(req, value, strlen(value));
280	if (buf != NULL)
281		free(buf, M_BUS);
282	return (error);
283}
284
285static void
286device_sysctl_init(device_t dev)
287{
288	devclass_t dc = dev->devclass;
289
290	if (dev->sysctl_tree != NULL)
291		return;
292	devclass_sysctl_init(dc);
293	sysctl_ctx_init(&dev->sysctl_ctx);
294	dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
295	    SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
296	    dev->nameunit + strlen(dc->name),
297	    CTLFLAG_RD, 0, "");
298	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
299	    OID_AUTO, "%desc", CTLFLAG_RD,
300	    dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
301	    "device description");
302	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
303	    OID_AUTO, "%driver", CTLFLAG_RD,
304	    dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
305	    "device driver name");
306	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
307	    OID_AUTO, "%location", CTLFLAG_RD,
308	    dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
309	    "device location relative to parent");
310	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
311	    OID_AUTO, "%pnpinfo", CTLFLAG_RD,
312	    dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
313	    "device identification");
314	SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
315	    OID_AUTO, "%parent", CTLFLAG_RD,
316	    dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
317	    "parent device");
318}
319
320static void
321device_sysctl_fini(device_t dev)
322{
323	if (dev->sysctl_tree == NULL)
324		return;
325	sysctl_ctx_free(&dev->sysctl_ctx);
326	dev->sysctl_tree = NULL;
327}
328
329/*
330 * /dev/devctl implementation
331 */
332
333/*
334 * This design allows only one reader for /dev/devctl.  This is not desirable
335 * in the long run, but will get a lot of hair out of this implementation.
336 * Maybe we should make this device a clonable device.
337 *
338 * Also note: we specifically do not attach a device to the device_t tree
339 * to avoid potential chicken and egg problems.  One could argue that all
340 * of this belongs to the root node.  One could also further argue that the
341 * sysctl interface that we have not might more properly be an ioctl
342 * interface, but at this stage of the game, I'm not inclined to rock that
343 * boat.
344 *
345 * I'm also not sure that the SIGIO support is done correctly or not, as
346 * I copied it from a driver that had SIGIO support that likely hasn't been
347 * tested since 3.4 or 2.2.8!
348 */
349
350static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
351static int devctl_disable = 0;
352TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
353SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
354    sysctl_devctl_disable, "I", "devctl disable");
355
356static d_open_t		devopen;
357static d_close_t	devclose;
358static d_read_t		devread;
359static d_ioctl_t	devioctl;
360static d_poll_t		devpoll;
361
362#define CDEV_MAJOR 173
363static struct cdevsw dev_cdevsw = {
364	.d_version =	D_VERSION,
365	.d_flags =	D_NEEDGIANT,
366	.d_open =	devopen,
367	.d_close =	devclose,
368	.d_read =	devread,
369	.d_ioctl =	devioctl,
370	.d_poll =	devpoll,
371	.d_name =	"devctl",
372	.d_maj =	CDEV_MAJOR,
373};
374
375struct dev_event_info
376{
377	char *dei_data;
378	TAILQ_ENTRY(dev_event_info) dei_link;
379};
380
381TAILQ_HEAD(devq, dev_event_info);
382
383static struct dev_softc
384{
385	int	inuse;
386	int	nonblock;
387	struct mtx mtx;
388	struct cv cv;
389	struct selinfo sel;
390	struct devq devq;
391	struct proc *async_proc;
392} devsoftc;
393
394static struct cdev *devctl_dev;
395
396static void
397devinit(void)
398{
399	devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
400	    "devctl");
401	mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
402	cv_init(&devsoftc.cv, "dev cv");
403	TAILQ_INIT(&devsoftc.devq);
404}
405
406static int
407devopen(struct cdev *dev, int oflags, int devtype, d_thread_t *td)
408{
409	if (devsoftc.inuse)
410		return (EBUSY);
411	/* move to init */
412	devsoftc.inuse = 1;
413	devsoftc.nonblock = 0;
414	devsoftc.async_proc = NULL;
415	return (0);
416}
417
418static int
419devclose(struct cdev *dev, int fflag, int devtype, d_thread_t *td)
420{
421	devsoftc.inuse = 0;
422	mtx_lock(&devsoftc.mtx);
423	cv_broadcast(&devsoftc.cv);
424	mtx_unlock(&devsoftc.mtx);
425
426	return (0);
427}
428
429/*
430 * The read channel for this device is used to report changes to
431 * userland in realtime.  We are required to free the data as well as
432 * the n1 object because we allocate them separately.  Also note that
433 * we return one record at a time.  If you try to read this device a
434 * character at a time, you will loose the rest of the data.  Listening
435 * programs are expected to cope.
436 */
437static int
438devread(struct cdev *dev, struct uio *uio, int ioflag)
439{
440	struct dev_event_info *n1;
441	int rv;
442
443	mtx_lock(&devsoftc.mtx);
444	while (TAILQ_EMPTY(&devsoftc.devq)) {
445		if (devsoftc.nonblock) {
446			mtx_unlock(&devsoftc.mtx);
447			return (EAGAIN);
448		}
449		rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
450		if (rv) {
451			/*
452			 * Need to translate ERESTART to EINTR here? -- jake
453			 */
454			mtx_unlock(&devsoftc.mtx);
455			return (rv);
456		}
457	}
458	n1 = TAILQ_FIRST(&devsoftc.devq);
459	TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
460	mtx_unlock(&devsoftc.mtx);
461	rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
462	free(n1->dei_data, M_BUS);
463	free(n1, M_BUS);
464	return (rv);
465}
466
467static	int
468devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
469{
470	switch (cmd) {
471
472	case FIONBIO:
473		if (*(int*)data)
474			devsoftc.nonblock = 1;
475		else
476			devsoftc.nonblock = 0;
477		return (0);
478	case FIOASYNC:
479		if (*(int*)data)
480			devsoftc.async_proc = td->td_proc;
481		else
482			devsoftc.async_proc = NULL;
483		return (0);
484
485		/* (un)Support for other fcntl() calls. */
486	case FIOCLEX:
487	case FIONCLEX:
488	case FIONREAD:
489	case FIOSETOWN:
490	case FIOGETOWN:
491	default:
492		break;
493	}
494	return (ENOTTY);
495}
496
497static	int
498devpoll(struct cdev *dev, int events, d_thread_t *td)
499{
500	int	revents = 0;
501
502	mtx_lock(&devsoftc.mtx);
503	if (events & (POLLIN | POLLRDNORM)) {
504		if (!TAILQ_EMPTY(&devsoftc.devq))
505			revents = events & (POLLIN | POLLRDNORM);
506		else
507			selrecord(td, &devsoftc.sel);
508	}
509	mtx_unlock(&devsoftc.mtx);
510
511	return (revents);
512}
513
514/*
515 * Generic interface to queue data to the devctl device.  It is
516 * assumed that data is properly formatted.  It is further assumed
517 * that data is allocated.
518 */
519void
520devctl_queue_data(char *data)
521{
522	struct dev_event_info *n1 = NULL;
523	struct proc *p;
524
525	n1 = malloc(sizeof(*n1), M_BUS, M_NOWAIT);
526	if (n1 == NULL)
527		return;
528	n1->dei_data = data;
529	mtx_lock(&devsoftc.mtx);
530	TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
531	cv_broadcast(&devsoftc.cv);
532	mtx_unlock(&devsoftc.mtx);
533	selwakeup(&devsoftc.sel);
534	p = devsoftc.async_proc;
535	if (p != NULL) {
536		PROC_LOCK(p);
537		psignal(p, SIGIO);
538		PROC_UNLOCK(p);
539	}
540}
541
542/*
543 * Send a 'notification' to userland, using standard ways
544 */
545void
546devctl_notify(const char *system, const char *subsystem, const char *type,
547    const char *data)
548{
549	int len = 0;
550	char *msg;
551
552	if (system == NULL)
553		return;		/* BOGUS!  Must specify system. */
554	if (subsystem == NULL)
555		return;		/* BOGUS!  Must specify subsystem. */
556	if (type == NULL)
557		return;		/* BOGUS!  Must specify type. */
558	len += strlen(" system=") + strlen(system);
559	len += strlen(" subsystem=") + strlen(subsystem);
560	len += strlen(" type=") + strlen(type);
561	/* add in the data message plus newline. */
562	if (data != NULL)
563		len += strlen(data);
564	len += 3;	/* '!', '\n', and NUL */
565	msg = malloc(len, M_BUS, M_NOWAIT);
566	if (msg == NULL)
567		return;		/* Drop it on the floor */
568	snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n", system,
569	    subsystem, type, data);
570	devctl_queue_data(msg);
571}
572
573/*
574 * Common routine that tries to make sending messages as easy as possible.
575 * We allocate memory for the data, copy strings into that, but do not
576 * free it unless there's an error.  The dequeue part of the driver should
577 * free the data.  We don't send data when the device is disabled.  We do
578 * send data, even when we have no listeners, because we wish to avoid
579 * races relating to startup and restart of listening applications.
580 */
581static void
582devaddq(const char *type, const char *what, device_t dev)
583{
584	char *data = NULL;
585	char *loc;
586	const char *parstr;
587
588	if (devctl_disable)
589		return;
590	data = malloc(1024, M_BUS, M_NOWAIT);
591	if (data == NULL)
592		goto bad;
593	loc = malloc(1024, M_BUS, M_NOWAIT);
594	if (loc == NULL)
595		goto bad;
596	*loc = '\0';
597	bus_child_location_str(dev, loc, 1024);
598	if (device_get_parent(dev) == NULL)
599		parstr = ".";	/* Or '/' ? */
600	else
601		parstr = device_get_nameunit(device_get_parent(dev));
602	snprintf(data, 1024, "%s%s at %s on %s\n", type, what, loc, parstr);
603	free(loc, M_BUS);
604	devctl_queue_data(data);
605	return;
606bad:
607	free(data, M_BUS);
608	return;
609}
610
611/*
612 * A device was added to the tree.  We are called just after it successfully
613 * attaches (that is, probe and attach success for this device).  No call
614 * is made if a device is merely parented into the tree.  See devnomatch
615 * if probe fails.  If attach fails, no notification is sent (but maybe
616 * we should have a different message for this).
617 */
618static void
619devadded(device_t dev)
620{
621	char *pnp = NULL;
622	char *tmp = NULL;
623
624	pnp = malloc(1024, M_BUS, M_NOWAIT);
625	if (pnp == NULL)
626		goto fail;
627	tmp = malloc(1024, M_BUS, M_NOWAIT);
628	if (tmp == NULL)
629		goto fail;
630	*pnp = '\0';
631	bus_child_pnpinfo_str(dev, pnp, 1024);
632	snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
633	devaddq("+", tmp, dev);
634fail:
635	if (pnp != NULL)
636		free(pnp, M_BUS);
637	if (tmp != NULL)
638		free(tmp, M_BUS);
639	return;
640}
641
642/*
643 * A device was removed from the tree.  We are called just before this
644 * happens.
645 */
646static void
647devremoved(device_t dev)
648{
649	char *pnp = NULL;
650	char *tmp = NULL;
651
652	pnp = malloc(1024, M_BUS, M_NOWAIT);
653	if (pnp == NULL)
654		goto fail;
655	tmp = malloc(1024, M_BUS, M_NOWAIT);
656	if (tmp == NULL)
657		goto fail;
658	*pnp = '\0';
659	bus_child_pnpinfo_str(dev, pnp, 1024);
660	snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
661	devaddq("-", tmp, dev);
662fail:
663	if (pnp != NULL)
664		free(pnp, M_BUS);
665	if (tmp != NULL)
666		free(tmp, M_BUS);
667	return;
668}
669
670/*
671 * Called when there's no match for this device.  This is only called
672 * the first time that no match happens, so we don't keep getitng this
673 * message.  Should that prove to be undesirable, we can change it.
674 * This is called when all drivers that can attach to a given bus
675 * decline to accept this device.  Other errrors may not be detected.
676 */
677static void
678devnomatch(device_t dev)
679{
680	char *pnp = NULL;
681
682	pnp = malloc(1024, M_BUS, M_NOWAIT);
683	if (pnp == NULL)
684		return;
685	*pnp = '\0';
686	bus_child_pnpinfo_str(dev, pnp, 1024);
687	devaddq("?", pnp, dev);
688	free(pnp, M_BUS);
689	return;
690}
691
692static int
693sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
694{
695	struct dev_event_info *n1;
696	int dis, error;
697
698	dis = devctl_disable;
699	error = sysctl_handle_int(oidp, &dis, 0, req);
700	if (error || !req->newptr)
701		return (error);
702	mtx_lock(&devsoftc.mtx);
703	devctl_disable = dis;
704	if (dis) {
705		while (!TAILQ_EMPTY(&devsoftc.devq)) {
706			n1 = TAILQ_FIRST(&devsoftc.devq);
707			TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
708			free(n1->dei_data, M_BUS);
709			free(n1, M_BUS);
710		}
711	}
712	mtx_unlock(&devsoftc.mtx);
713	return (0);
714}
715
716/* End of /dev/devctl code */
717
718TAILQ_HEAD(,device)	bus_data_devices;
719static int bus_data_generation = 1;
720
721kobj_method_t null_methods[] = {
722	{ 0, 0 }
723};
724
725DEFINE_CLASS(null, null_methods, 0);
726
727/*
728 * Devclass implementation
729 */
730
731static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
732
733static devclass_t
734devclass_find_internal(const char *classname, const char *parentname,
735		       int create)
736{
737	devclass_t dc;
738
739	PDEBUG(("looking for %s", classname));
740	if (!classname)
741		return (NULL);
742
743	TAILQ_FOREACH(dc, &devclasses, link) {
744		if (!strcmp(dc->name, classname))
745			break;
746	}
747
748	if (create && !dc) {
749		PDEBUG(("creating %s", classname));
750		dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
751		    M_BUS, M_NOWAIT|M_ZERO);
752		if (!dc)
753			return (NULL);
754		dc->parent = NULL;
755		dc->name = (char*) (dc + 1);
756		strcpy(dc->name, classname);
757		TAILQ_INIT(&dc->drivers);
758		TAILQ_INSERT_TAIL(&devclasses, dc, link);
759
760		bus_data_generation_update();
761	}
762	if (parentname && dc && !dc->parent) {
763		dc->parent = devclass_find_internal(parentname, 0, FALSE);
764	}
765
766	return (dc);
767}
768
769devclass_t
770devclass_create(const char *classname)
771{
772	return (devclass_find_internal(classname, 0, TRUE));
773}
774
775devclass_t
776devclass_find(const char *classname)
777{
778	return (devclass_find_internal(classname, 0, FALSE));
779}
780
781int
782devclass_add_driver(devclass_t dc, driver_t *driver)
783{
784	driverlink_t dl;
785	int i;
786
787	PDEBUG(("%s", DRIVERNAME(driver)));
788
789	dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
790	if (!dl)
791		return (ENOMEM);
792
793	/*
794	 * Compile the driver's methods. Also increase the reference count
795	 * so that the class doesn't get freed when the last instance
796	 * goes. This means we can safely use static methods and avoids a
797	 * double-free in devclass_delete_driver.
798	 */
799	kobj_class_compile((kobj_class_t) driver);
800
801	/*
802	 * Make sure the devclass which the driver is implementing exists.
803	 */
804	devclass_find_internal(driver->name, 0, TRUE);
805
806	dl->driver = driver;
807	TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
808	driver->refs++;
809
810	/*
811	 * Call BUS_DRIVER_ADDED for any existing busses in this class.
812	 */
813	for (i = 0; i < dc->maxunit; i++)
814		if (dc->devices[i])
815			BUS_DRIVER_ADDED(dc->devices[i], driver);
816
817	bus_data_generation_update();
818	return (0);
819}
820
821int
822devclass_delete_driver(devclass_t busclass, driver_t *driver)
823{
824	devclass_t dc = devclass_find(driver->name);
825	driverlink_t dl;
826	device_t dev;
827	int i;
828	int error;
829
830	PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
831
832	if (!dc)
833		return (0);
834
835	/*
836	 * Find the link structure in the bus' list of drivers.
837	 */
838	TAILQ_FOREACH(dl, &busclass->drivers, link) {
839		if (dl->driver == driver)
840			break;
841	}
842
843	if (!dl) {
844		PDEBUG(("%s not found in %s list", driver->name,
845		    busclass->name));
846		return (ENOENT);
847	}
848
849	/*
850	 * Disassociate from any devices.  We iterate through all the
851	 * devices in the devclass of the driver and detach any which are
852	 * using the driver and which have a parent in the devclass which
853	 * we are deleting from.
854	 *
855	 * Note that since a driver can be in multiple devclasses, we
856	 * should not detach devices which are not children of devices in
857	 * the affected devclass.
858	 */
859	for (i = 0; i < dc->maxunit; i++) {
860		if (dc->devices[i]) {
861			dev = dc->devices[i];
862			if (dev->driver == driver && dev->parent &&
863			    dev->parent->devclass == busclass) {
864				if ((error = device_detach(dev)) != 0)
865					return (error);
866				device_set_driver(dev, NULL);
867			}
868		}
869	}
870
871	TAILQ_REMOVE(&busclass->drivers, dl, link);
872	free(dl, M_BUS);
873
874	driver->refs--;
875	if (driver->refs == 0)
876		kobj_class_free((kobj_class_t) driver);
877
878	bus_data_generation_update();
879	return (0);
880}
881
882static driverlink_t
883devclass_find_driver_internal(devclass_t dc, const char *classname)
884{
885	driverlink_t dl;
886
887	PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
888
889	TAILQ_FOREACH(dl, &dc->drivers, link) {
890		if (!strcmp(dl->driver->name, classname))
891			return (dl);
892	}
893
894	PDEBUG(("not found"));
895	return (NULL);
896}
897
898kobj_class_t
899devclass_find_driver(devclass_t dc, const char *classname)
900{
901	driverlink_t dl;
902
903	dl = devclass_find_driver_internal(dc, classname);
904	if (dl)
905		return (dl->driver);
906	return (NULL);
907}
908
909const char *
910devclass_get_name(devclass_t dc)
911{
912	return (dc->name);
913}
914
915device_t
916devclass_get_device(devclass_t dc, int unit)
917{
918	if (dc == NULL || unit < 0 || unit >= dc->maxunit)
919		return (NULL);
920	return (dc->devices[unit]);
921}
922
923void *
924devclass_get_softc(devclass_t dc, int unit)
925{
926	device_t dev;
927
928	dev = devclass_get_device(dc, unit);
929	if (!dev)
930		return (NULL);
931
932	return (device_get_softc(dev));
933}
934
935int
936devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
937{
938	int i;
939	int count;
940	device_t *list;
941
942	count = 0;
943	for (i = 0; i < dc->maxunit; i++)
944		if (dc->devices[i])
945			count++;
946
947	list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
948	if (!list)
949		return (ENOMEM);
950
951	count = 0;
952	for (i = 0; i < dc->maxunit; i++) {
953		if (dc->devices[i]) {
954			list[count] = dc->devices[i];
955			count++;
956		}
957	}
958
959	*devlistp = list;
960	*devcountp = count;
961
962	return (0);
963}
964
965int
966devclass_get_maxunit(devclass_t dc)
967{
968	return (dc->maxunit);
969}
970
971int
972devclass_find_free_unit(devclass_t dc, int unit)
973{
974	if (dc == NULL)
975		return (unit);
976	while (unit < dc->maxunit && dc->devices[unit] != NULL)
977		unit++;
978	return (unit);
979}
980
981void
982devclass_set_parent(devclass_t dc, devclass_t pdc)
983{
984	dc->parent = pdc;
985}
986
987devclass_t
988devclass_get_parent(devclass_t dc)
989{
990	return (dc->parent);
991}
992
993struct sysctl_ctx_list *
994devclass_get_sysctl_ctx(devclass_t dc)
995{
996	return (&dc->sysctl_ctx);
997}
998
999struct sysctl_oid *
1000devclass_get_sysctl_tree(devclass_t dc)
1001{
1002	return (dc->sysctl_tree);
1003}
1004
1005static int
1006devclass_alloc_unit(devclass_t dc, int *unitp)
1007{
1008	int unit = *unitp;
1009
1010	PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1011
1012	/* If we were given a wired unit number, check for existing device */
1013	/* XXX imp XXX */
1014	if (unit != -1) {
1015		if (unit >= 0 && unit < dc->maxunit &&
1016		    dc->devices[unit] != NULL) {
1017			if (bootverbose)
1018				printf("%s: %s%d already exists; skipping it\n",
1019				    dc->name, dc->name, *unitp);
1020			return (EEXIST);
1021		}
1022	} else {
1023		/* Unwired device, find the next available slot for it */
1024		unit = 0;
1025		while (unit < dc->maxunit && dc->devices[unit] != NULL)
1026			unit++;
1027	}
1028
1029	/*
1030	 * We've selected a unit beyond the length of the table, so let's
1031	 * extend the table to make room for all units up to and including
1032	 * this one.
1033	 */
1034	if (unit >= dc->maxunit) {
1035		device_t *newlist;
1036		int newsize;
1037
1038		newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1039		newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1040		if (!newlist)
1041			return (ENOMEM);
1042		bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
1043		bzero(newlist + dc->maxunit,
1044		    sizeof(device_t) * (newsize - dc->maxunit));
1045		if (dc->devices)
1046			free(dc->devices, M_BUS);
1047		dc->devices = newlist;
1048		dc->maxunit = newsize;
1049	}
1050	PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1051
1052	*unitp = unit;
1053	return (0);
1054}
1055
1056static int
1057devclass_add_device(devclass_t dc, device_t dev)
1058{
1059	int buflen, error;
1060
1061	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1062
1063	buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit);
1064	if (buflen < 0)
1065		return (ENOMEM);
1066	dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1067	if (!dev->nameunit)
1068		return (ENOMEM);
1069
1070	if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1071		free(dev->nameunit, M_BUS);
1072		dev->nameunit = NULL;
1073		return (error);
1074	}
1075	dc->devices[dev->unit] = dev;
1076	dev->devclass = dc;
1077	snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1078
1079	return (0);
1080}
1081
1082static int
1083devclass_delete_device(devclass_t dc, device_t dev)
1084{
1085	if (!dc || !dev)
1086		return (0);
1087
1088	PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1089
1090	if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1091		panic("devclass_delete_device: inconsistent device class");
1092	dc->devices[dev->unit] = NULL;
1093	if (dev->flags & DF_WILDCARD)
1094		dev->unit = -1;
1095	dev->devclass = NULL;
1096	free(dev->nameunit, M_BUS);
1097	dev->nameunit = NULL;
1098
1099	return (0);
1100}
1101
1102static device_t
1103make_device(device_t parent, const char *name, int unit)
1104{
1105	device_t dev;
1106	devclass_t dc;
1107
1108	PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1109
1110	if (name) {
1111		dc = devclass_find_internal(name, 0, TRUE);
1112		if (!dc) {
1113			printf("make_device: can't find device class %s\n",
1114			    name);
1115			return (NULL);
1116		}
1117	} else {
1118		dc = NULL;
1119	}
1120
1121	dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1122	if (!dev)
1123		return (NULL);
1124
1125	dev->parent = parent;
1126	TAILQ_INIT(&dev->children);
1127	kobj_init((kobj_t) dev, &null_class);
1128	dev->driver = NULL;
1129	dev->devclass = NULL;
1130	dev->unit = unit;
1131	dev->nameunit = NULL;
1132	dev->desc = NULL;
1133	dev->busy = 0;
1134	dev->devflags = 0;
1135	dev->flags = DF_ENABLED;
1136	dev->order = 0;
1137	if (unit == -1)
1138		dev->flags |= DF_WILDCARD;
1139	if (name) {
1140		dev->flags |= DF_FIXEDCLASS;
1141		if (devclass_add_device(dc, dev)) {
1142			kobj_delete((kobj_t) dev, M_BUS);
1143			return (NULL);
1144		}
1145	}
1146	dev->ivars = NULL;
1147	dev->softc = NULL;
1148
1149	dev->state = DS_NOTPRESENT;
1150
1151	TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1152	bus_data_generation_update();
1153
1154	return (dev);
1155}
1156
1157static int
1158device_print_child(device_t dev, device_t child)
1159{
1160	int retval = 0;
1161
1162	if (device_is_alive(child))
1163		retval += BUS_PRINT_CHILD(dev, child);
1164	else
1165		retval += device_printf(child, " not found\n");
1166
1167	return (retval);
1168}
1169
1170device_t
1171device_add_child(device_t dev, const char *name, int unit)
1172{
1173	return (device_add_child_ordered(dev, 0, name, unit));
1174}
1175
1176device_t
1177device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1178{
1179	device_t child;
1180	device_t place;
1181
1182	PDEBUG(("%s at %s with order %d as unit %d",
1183	    name, DEVICENAME(dev), order, unit));
1184
1185	child = make_device(dev, name, unit);
1186	if (child == NULL)
1187		return (child);
1188	child->order = order;
1189
1190	TAILQ_FOREACH(place, &dev->children, link) {
1191		if (place->order > order)
1192			break;
1193	}
1194
1195	if (place) {
1196		/*
1197		 * The device 'place' is the first device whose order is
1198		 * greater than the new child.
1199		 */
1200		TAILQ_INSERT_BEFORE(place, child, link);
1201	} else {
1202		/*
1203		 * The new child's order is greater or equal to the order of
1204		 * any existing device. Add the child to the tail of the list.
1205		 */
1206		TAILQ_INSERT_TAIL(&dev->children, child, link);
1207	}
1208
1209	bus_data_generation_update();
1210	return (child);
1211}
1212
1213int
1214device_delete_child(device_t dev, device_t child)
1215{
1216	int error;
1217	device_t grandchild;
1218
1219	PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1220
1221	/* remove children first */
1222	while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1223		error = device_delete_child(child, grandchild);
1224		if (error)
1225			return (error);
1226	}
1227
1228	if ((error = device_detach(child)) != 0)
1229		return (error);
1230	if (child->devclass)
1231		devclass_delete_device(child->devclass, child);
1232	TAILQ_REMOVE(&dev->children, child, link);
1233	TAILQ_REMOVE(&bus_data_devices, child, devlink);
1234	device_set_desc(child, NULL);
1235	kobj_delete((kobj_t) child, M_BUS);
1236
1237	bus_data_generation_update();
1238	return (0);
1239}
1240
1241/*
1242 * Find only devices attached to this bus.
1243 */
1244device_t
1245device_find_child(device_t dev, const char *classname, int unit)
1246{
1247	devclass_t dc;
1248	device_t child;
1249
1250	dc = devclass_find(classname);
1251	if (!dc)
1252		return (NULL);
1253
1254	child = devclass_get_device(dc, unit);
1255	if (child && child->parent == dev)
1256		return (child);
1257	return (NULL);
1258}
1259
1260static driverlink_t
1261first_matching_driver(devclass_t dc, device_t dev)
1262{
1263	if (dev->devclass)
1264		return (devclass_find_driver_internal(dc, dev->devclass->name));
1265	return (TAILQ_FIRST(&dc->drivers));
1266}
1267
1268static driverlink_t
1269next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1270{
1271	if (dev->devclass) {
1272		driverlink_t dl;
1273		for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1274			if (!strcmp(dev->devclass->name, dl->driver->name))
1275				return (dl);
1276		return (NULL);
1277	}
1278	return (TAILQ_NEXT(last, link));
1279}
1280
1281static int
1282device_probe_child(device_t dev, device_t child)
1283{
1284	devclass_t dc;
1285	driverlink_t best = 0;
1286	driverlink_t dl;
1287	int result, pri = 0;
1288	int hasclass = (child->devclass != 0);
1289
1290	dc = dev->devclass;
1291	if (!dc)
1292		panic("device_probe_child: parent device has no devclass");
1293
1294	if (child->state == DS_ALIVE)
1295		return (0);
1296
1297	for (; dc; dc = dc->parent) {
1298		for (dl = first_matching_driver(dc, child);
1299		     dl;
1300		     dl = next_matching_driver(dc, child, dl)) {
1301			PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1302			device_set_driver(child, dl->driver);
1303			if (!hasclass)
1304				device_set_devclass(child, dl->driver->name);
1305			result = DEVICE_PROBE(child);
1306			if (!hasclass)
1307				device_set_devclass(child, 0);
1308
1309			/*
1310			 * If the driver returns SUCCESS, there can be
1311			 * no higher match for this device.
1312			 */
1313			if (result == 0) {
1314				best = dl;
1315				pri = 0;
1316				break;
1317			}
1318
1319			/*
1320			 * The driver returned an error so it
1321			 * certainly doesn't match.
1322			 */
1323			if (result > 0) {
1324				device_set_driver(child, 0);
1325				continue;
1326			}
1327
1328			/*
1329			 * A priority lower than SUCCESS, remember the
1330			 * best matching driver. Initialise the value
1331			 * of pri for the first match.
1332			 */
1333			if (best == 0 || result > pri) {
1334				best = dl;
1335				pri = result;
1336				continue;
1337			}
1338		}
1339		/*
1340		 * If we have an unambiguous match in this devclass,
1341		 * don't look in the parent.
1342		 */
1343		if (best && pri == 0)
1344			break;
1345	}
1346
1347	/*
1348	 * If we found a driver, change state and initialise the devclass.
1349	 */
1350	if (best) {
1351		if (!child->devclass)
1352			device_set_devclass(child, best->driver->name);
1353		device_set_driver(child, best->driver);
1354		if (pri < 0) {
1355			/*
1356			 * A bit bogus. Call the probe method again to make
1357			 * sure that we have the right description.
1358			 */
1359			DEVICE_PROBE(child);
1360		}
1361		child->state = DS_ALIVE;
1362
1363		bus_data_generation_update();
1364		return (0);
1365	}
1366
1367	return (ENXIO);
1368}
1369
1370device_t
1371device_get_parent(device_t dev)
1372{
1373	return (dev->parent);
1374}
1375
1376int
1377device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1378{
1379	int count;
1380	device_t child;
1381	device_t *list;
1382
1383	count = 0;
1384	TAILQ_FOREACH(child, &dev->children, link) {
1385		count++;
1386	}
1387
1388	list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1389	if (!list)
1390		return (ENOMEM);
1391
1392	count = 0;
1393	TAILQ_FOREACH(child, &dev->children, link) {
1394		list[count] = child;
1395		count++;
1396	}
1397
1398	*devlistp = list;
1399	*devcountp = count;
1400
1401	return (0);
1402}
1403
1404driver_t *
1405device_get_driver(device_t dev)
1406{
1407	return (dev->driver);
1408}
1409
1410devclass_t
1411device_get_devclass(device_t dev)
1412{
1413	return (dev->devclass);
1414}
1415
1416const char *
1417device_get_name(device_t dev)
1418{
1419	if (dev != NULL && dev->devclass)
1420		return (devclass_get_name(dev->devclass));
1421	return (NULL);
1422}
1423
1424const char *
1425device_get_nameunit(device_t dev)
1426{
1427	return (dev->nameunit);
1428}
1429
1430int
1431device_get_unit(device_t dev)
1432{
1433	return (dev->unit);
1434}
1435
1436const char *
1437device_get_desc(device_t dev)
1438{
1439	return (dev->desc);
1440}
1441
1442u_int32_t
1443device_get_flags(device_t dev)
1444{
1445	return (dev->devflags);
1446}
1447
1448struct sysctl_ctx_list *
1449device_get_sysctl_ctx(device_t dev)
1450{
1451	return (&dev->sysctl_ctx);
1452}
1453
1454struct sysctl_oid *
1455device_get_sysctl_tree(device_t dev)
1456{
1457	return (dev->sysctl_tree);
1458}
1459
1460int
1461device_print_prettyname(device_t dev)
1462{
1463	const char *name = device_get_name(dev);
1464
1465	if (name == 0)
1466		return (printf("unknown: "));
1467	return (printf("%s%d: ", name, device_get_unit(dev)));
1468}
1469
1470int
1471device_printf(device_t dev, const char * fmt, ...)
1472{
1473	va_list ap;
1474	int retval;
1475
1476	retval = device_print_prettyname(dev);
1477	va_start(ap, fmt);
1478	retval += vprintf(fmt, ap);
1479	va_end(ap);
1480	return (retval);
1481}
1482
1483static void
1484device_set_desc_internal(device_t dev, const char* desc, int copy)
1485{
1486	if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1487		free(dev->desc, M_BUS);
1488		dev->flags &= ~DF_DESCMALLOCED;
1489		dev->desc = NULL;
1490	}
1491
1492	if (copy && desc) {
1493		dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
1494		if (dev->desc) {
1495			strcpy(dev->desc, desc);
1496			dev->flags |= DF_DESCMALLOCED;
1497		}
1498	} else {
1499		/* Avoid a -Wcast-qual warning */
1500		dev->desc = (char *)(uintptr_t) desc;
1501	}
1502
1503	bus_data_generation_update();
1504}
1505
1506void
1507device_set_desc(device_t dev, const char* desc)
1508{
1509	device_set_desc_internal(dev, desc, FALSE);
1510}
1511
1512void
1513device_set_desc_copy(device_t dev, const char* desc)
1514{
1515	device_set_desc_internal(dev, desc, TRUE);
1516}
1517
1518void
1519device_set_flags(device_t dev, u_int32_t flags)
1520{
1521	dev->devflags = flags;
1522}
1523
1524void *
1525device_get_softc(device_t dev)
1526{
1527	return (dev->softc);
1528}
1529
1530void
1531device_set_softc(device_t dev, void *softc)
1532{
1533	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1534		free(dev->softc, M_BUS_SC);
1535	dev->softc = softc;
1536	if (dev->softc)
1537		dev->flags |= DF_EXTERNALSOFTC;
1538	else
1539		dev->flags &= ~DF_EXTERNALSOFTC;
1540}
1541
1542void *
1543device_get_ivars(device_t dev)
1544{
1545
1546	KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
1547	return (dev->ivars);
1548}
1549
1550void
1551device_set_ivars(device_t dev, void * ivars)
1552{
1553
1554	KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
1555	dev->ivars = ivars;
1556}
1557
1558device_state_t
1559device_get_state(device_t dev)
1560{
1561	return (dev->state);
1562}
1563
1564void
1565device_enable(device_t dev)
1566{
1567	dev->flags |= DF_ENABLED;
1568}
1569
1570void
1571device_disable(device_t dev)
1572{
1573	dev->flags &= ~DF_ENABLED;
1574}
1575
1576void
1577device_busy(device_t dev)
1578{
1579	if (dev->state < DS_ATTACHED)
1580		panic("device_busy: called for unattached device");
1581	if (dev->busy == 0 && dev->parent)
1582		device_busy(dev->parent);
1583	dev->busy++;
1584	dev->state = DS_BUSY;
1585}
1586
1587void
1588device_unbusy(device_t dev)
1589{
1590	if (dev->state != DS_BUSY)
1591		panic("device_unbusy: called for non-busy device");
1592	dev->busy--;
1593	if (dev->busy == 0) {
1594		if (dev->parent)
1595			device_unbusy(dev->parent);
1596		dev->state = DS_ATTACHED;
1597	}
1598}
1599
1600void
1601device_quiet(device_t dev)
1602{
1603	dev->flags |= DF_QUIET;
1604}
1605
1606void
1607device_verbose(device_t dev)
1608{
1609	dev->flags &= ~DF_QUIET;
1610}
1611
1612int
1613device_is_quiet(device_t dev)
1614{
1615	return ((dev->flags & DF_QUIET) != 0);
1616}
1617
1618int
1619device_is_enabled(device_t dev)
1620{
1621	return ((dev->flags & DF_ENABLED) != 0);
1622}
1623
1624int
1625device_is_alive(device_t dev)
1626{
1627	return (dev->state >= DS_ALIVE);
1628}
1629
1630int
1631device_is_attached(device_t dev)
1632{
1633	return (dev->state >= DS_ATTACHED);
1634}
1635
1636int
1637device_set_devclass(device_t dev, const char *classname)
1638{
1639	devclass_t dc;
1640	int error;
1641
1642	if (!classname) {
1643		if (dev->devclass)
1644			devclass_delete_device(dev->devclass, dev);
1645		return (0);
1646	}
1647
1648	if (dev->devclass) {
1649		printf("device_set_devclass: device class already set\n");
1650		return (EINVAL);
1651	}
1652
1653	dc = devclass_find_internal(classname, 0, TRUE);
1654	if (!dc)
1655		return (ENOMEM);
1656
1657	error = devclass_add_device(dc, dev);
1658
1659	bus_data_generation_update();
1660	return (error);
1661}
1662
1663int
1664device_set_driver(device_t dev, driver_t *driver)
1665{
1666	if (dev->state >= DS_ATTACHED)
1667		return (EBUSY);
1668
1669	if (dev->driver == driver)
1670		return (0);
1671
1672	if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1673		free(dev->softc, M_BUS_SC);
1674		dev->softc = NULL;
1675	}
1676	kobj_delete((kobj_t) dev, 0);
1677	dev->driver = driver;
1678	if (driver) {
1679		kobj_init((kobj_t) dev, (kobj_class_t) driver);
1680		if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
1681			dev->softc = malloc(driver->size, M_BUS_SC,
1682			    M_NOWAIT | M_ZERO);
1683			if (!dev->softc) {
1684				kobj_delete((kobj_t) dev, 0);
1685				kobj_init((kobj_t) dev, &null_class);
1686				dev->driver = NULL;
1687				return (ENOMEM);
1688			}
1689		}
1690	} else {
1691		kobj_init((kobj_t) dev, &null_class);
1692	}
1693
1694	bus_data_generation_update();
1695	return (0);
1696}
1697
1698int
1699device_probe_and_attach(device_t dev)
1700{
1701	int error;
1702
1703	if (dev->state >= DS_ALIVE)
1704		return (0);
1705
1706	if (!(dev->flags & DF_ENABLED)) {
1707		if (bootverbose) {
1708			device_print_prettyname(dev);
1709			printf("not probed (disabled)\n");
1710		}
1711		return (0);
1712	}
1713	if ((error = device_probe_child(dev->parent, dev)) != 0) {
1714		if (!(dev->flags & DF_DONENOMATCH)) {
1715			BUS_PROBE_NOMATCH(dev->parent, dev);
1716			devnomatch(dev);
1717			dev->flags |= DF_DONENOMATCH;
1718		}
1719		return (error);
1720	}
1721	error = device_attach(dev);
1722
1723	return (error);
1724}
1725
1726int
1727device_attach(device_t dev)
1728{
1729	int error;
1730
1731	device_sysctl_init(dev);
1732	if (!device_is_quiet(dev))
1733		device_print_child(dev->parent, dev);
1734	if ((error = DEVICE_ATTACH(dev)) != 0) {
1735		printf("device_attach: %s%d attach returned %d\n",
1736		    dev->driver->name, dev->unit, error);
1737		/* Unset the class; set in device_probe_child */
1738		if (dev->devclass == 0)
1739			device_set_devclass(dev, 0);
1740		device_set_driver(dev, NULL);
1741		device_sysctl_fini(dev);
1742		dev->state = DS_NOTPRESENT;
1743		return (error);
1744	}
1745	dev->state = DS_ATTACHED;
1746	devadded(dev);
1747	return (0);
1748}
1749
1750int
1751device_detach(device_t dev)
1752{
1753	int error;
1754
1755	PDEBUG(("%s", DEVICENAME(dev)));
1756	if (dev->state == DS_BUSY)
1757		return (EBUSY);
1758	if (dev->state != DS_ATTACHED)
1759		return (0);
1760
1761	if ((error = DEVICE_DETACH(dev)) != 0)
1762		return (error);
1763	devremoved(dev);
1764	device_printf(dev, "detached\n");
1765	if (dev->parent)
1766		BUS_CHILD_DETACHED(dev->parent, dev);
1767
1768	if (!(dev->flags & DF_FIXEDCLASS))
1769		devclass_delete_device(dev->devclass, dev);
1770
1771	dev->state = DS_NOTPRESENT;
1772	device_set_driver(dev, NULL);
1773	device_sysctl_fini(dev);
1774
1775	return (0);
1776}
1777
1778int
1779device_shutdown(device_t dev)
1780{
1781	if (dev->state < DS_ATTACHED)
1782		return (0);
1783	return (DEVICE_SHUTDOWN(dev));
1784}
1785
1786int
1787device_set_unit(device_t dev, int unit)
1788{
1789	devclass_t dc;
1790	int err;
1791
1792	dc = device_get_devclass(dev);
1793	if (unit < dc->maxunit && dc->devices[unit])
1794		return (EBUSY);
1795	err = devclass_delete_device(dc, dev);
1796	if (err)
1797		return (err);
1798	dev->unit = unit;
1799	err = devclass_add_device(dc, dev);
1800	if (err)
1801		return (err);
1802
1803	bus_data_generation_update();
1804	return (0);
1805}
1806
1807/*======================================*/
1808/*
1809 * Some useful method implementations to make life easier for bus drivers.
1810 */
1811
1812void
1813resource_list_init(struct resource_list *rl)
1814{
1815	SLIST_INIT(rl);
1816}
1817
1818void
1819resource_list_free(struct resource_list *rl)
1820{
1821	struct resource_list_entry *rle;
1822
1823	while ((rle = SLIST_FIRST(rl)) != NULL) {
1824		if (rle->res)
1825			panic("resource_list_free: resource entry is busy");
1826		SLIST_REMOVE_HEAD(rl, link);
1827		free(rle, M_BUS);
1828	}
1829}
1830
1831int
1832resource_list_add_next(struct resource_list *rl, int type, u_long start,
1833    u_long end, u_long count)
1834{
1835	int rid;
1836
1837	rid = 0;
1838	while (resource_list_find(rl, type, rid) != NULL)
1839		rid++;
1840	resource_list_add(rl, type, rid, start, end, count);
1841	return (rid);
1842}
1843
1844void
1845resource_list_add(struct resource_list *rl, int type, int rid,
1846    u_long start, u_long end, u_long count)
1847{
1848	struct resource_list_entry *rle;
1849
1850	rle = resource_list_find(rl, type, rid);
1851	if (!rle) {
1852		rle = malloc(sizeof(struct resource_list_entry), M_BUS,
1853		    M_NOWAIT);
1854		if (!rle)
1855			panic("resource_list_add: can't record entry");
1856		SLIST_INSERT_HEAD(rl, rle, link);
1857		rle->type = type;
1858		rle->rid = rid;
1859		rle->res = NULL;
1860	}
1861
1862	if (rle->res)
1863		panic("resource_list_add: resource entry is busy");
1864
1865	rle->start = start;
1866	rle->end = end;
1867	rle->count = count;
1868}
1869
1870struct resource_list_entry *
1871resource_list_find(struct resource_list *rl, int type, int rid)
1872{
1873	struct resource_list_entry *rle;
1874
1875	SLIST_FOREACH(rle, rl, link) {
1876		if (rle->type == type && rle->rid == rid)
1877			return (rle);
1878	}
1879	return (NULL);
1880}
1881
1882void
1883resource_list_delete(struct resource_list *rl, int type, int rid)
1884{
1885	struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1886
1887	if (rle) {
1888		if (rle->res != NULL)
1889			panic("resource_list_delete: resource has not been released");
1890		SLIST_REMOVE(rl, rle, resource_list_entry, link);
1891		free(rle, M_BUS);
1892	}
1893}
1894
1895struct resource *
1896resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
1897    int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
1898{
1899	struct resource_list_entry *rle = 0;
1900	int passthrough = (device_get_parent(child) != bus);
1901	int isdefault = (start == 0UL && end == ~0UL);
1902
1903	if (passthrough) {
1904		return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1905		    type, rid, start, end, count, flags));
1906	}
1907
1908	rle = resource_list_find(rl, type, *rid);
1909
1910	if (!rle)
1911		return (NULL);		/* no resource of that type/rid */
1912
1913	if (rle->res)
1914		panic("resource_list_alloc: resource entry is busy");
1915
1916	if (isdefault) {
1917		start = rle->start;
1918		count = ulmax(count, rle->count);
1919		end = ulmax(rle->end, start + count - 1);
1920	}
1921
1922	rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1923	    type, rid, start, end, count, flags);
1924
1925	/*
1926	 * Record the new range.
1927	 */
1928	if (rle->res) {
1929		rle->start = rman_get_start(rle->res);
1930		rle->end = rman_get_end(rle->res);
1931		rle->count = count;
1932	}
1933
1934	return (rle->res);
1935}
1936
1937int
1938resource_list_release(struct resource_list *rl, device_t bus, device_t child,
1939    int type, int rid, struct resource *res)
1940{
1941	struct resource_list_entry *rle = 0;
1942	int passthrough = (device_get_parent(child) != bus);
1943	int error;
1944
1945	if (passthrough) {
1946		return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1947		    type, rid, res));
1948	}
1949
1950	rle = resource_list_find(rl, type, rid);
1951
1952	if (!rle)
1953		panic("resource_list_release: can't find resource");
1954	if (!rle->res)
1955		panic("resource_list_release: resource entry is not busy");
1956
1957	error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1958	    type, rid, res);
1959	if (error)
1960		return (error);
1961
1962	rle->res = NULL;
1963	return (0);
1964}
1965
1966int
1967resource_list_print_type(struct resource_list *rl, const char *name, int type,
1968    const char *format)
1969{
1970	struct resource_list_entry *rle;
1971	int printed, retval;
1972
1973	printed = 0;
1974	retval = 0;
1975	/* Yes, this is kinda cheating */
1976	SLIST_FOREACH(rle, rl, link) {
1977		if (rle->type == type) {
1978			if (printed == 0)
1979				retval += printf(" %s ", name);
1980			else
1981				retval += printf(",");
1982			printed++;
1983			retval += printf(format, rle->start);
1984			if (rle->count > 1) {
1985				retval += printf("-");
1986				retval += printf(format, rle->start +
1987						 rle->count - 1);
1988			}
1989		}
1990	}
1991	return (retval);
1992}
1993
1994/*
1995 * Call DEVICE_IDENTIFY for each driver.
1996 */
1997int
1998bus_generic_probe(device_t dev)
1999{
2000	devclass_t dc = dev->devclass;
2001	driverlink_t dl;
2002
2003	TAILQ_FOREACH(dl, &dc->drivers, link) {
2004		DEVICE_IDENTIFY(dl->driver, dev);
2005	}
2006
2007	return (0);
2008}
2009
2010int
2011bus_generic_attach(device_t dev)
2012{
2013	device_t child;
2014
2015	TAILQ_FOREACH(child, &dev->children, link) {
2016		device_probe_and_attach(child);
2017	}
2018
2019	return (0);
2020}
2021
2022int
2023bus_generic_detach(device_t dev)
2024{
2025	device_t child;
2026	int error;
2027
2028	if (dev->state != DS_ATTACHED)
2029		return (EBUSY);
2030
2031	TAILQ_FOREACH(child, &dev->children, link) {
2032		if ((error = device_detach(child)) != 0)
2033			return (error);
2034	}
2035
2036	return (0);
2037}
2038
2039int
2040bus_generic_shutdown(device_t dev)
2041{
2042	device_t child;
2043
2044	TAILQ_FOREACH(child, &dev->children, link) {
2045		device_shutdown(child);
2046	}
2047
2048	return (0);
2049}
2050
2051int
2052bus_generic_suspend(device_t dev)
2053{
2054	int		error;
2055	device_t	child, child2;
2056
2057	TAILQ_FOREACH(child, &dev->children, link) {
2058		error = DEVICE_SUSPEND(child);
2059		if (error) {
2060			for (child2 = TAILQ_FIRST(&dev->children);
2061			     child2 && child2 != child;
2062			     child2 = TAILQ_NEXT(child2, link))
2063				DEVICE_RESUME(child2);
2064			return (error);
2065		}
2066	}
2067	return (0);
2068}
2069
2070int
2071bus_generic_resume(device_t dev)
2072{
2073	device_t	child;
2074
2075	TAILQ_FOREACH(child, &dev->children, link) {
2076		DEVICE_RESUME(child);
2077		/* if resume fails, there's nothing we can usefully do... */
2078	}
2079	return (0);
2080}
2081
2082int
2083bus_print_child_header(device_t dev, device_t child)
2084{
2085	int	retval = 0;
2086
2087	if (device_get_desc(child)) {
2088		retval += device_printf(child, "<%s>", device_get_desc(child));
2089	} else {
2090		retval += printf("%s", device_get_nameunit(child));
2091	}
2092
2093	return (retval);
2094}
2095
2096int
2097bus_print_child_footer(device_t dev, device_t child)
2098{
2099	return (printf(" on %s\n", device_get_nameunit(dev)));
2100}
2101
2102int
2103bus_generic_print_child(device_t dev, device_t child)
2104{
2105	int	retval = 0;
2106
2107	retval += bus_print_child_header(dev, child);
2108	retval += bus_print_child_footer(dev, child);
2109
2110	return (retval);
2111}
2112
2113int
2114bus_generic_read_ivar(device_t dev, device_t child, int index,
2115    uintptr_t * result)
2116{
2117	return (ENOENT);
2118}
2119
2120int
2121bus_generic_write_ivar(device_t dev, device_t child, int index,
2122    uintptr_t value)
2123{
2124	return (ENOENT);
2125}
2126
2127struct resource_list *
2128bus_generic_get_resource_list(device_t dev, device_t child)
2129{
2130	return (NULL);
2131}
2132
2133void
2134bus_generic_driver_added(device_t dev, driver_t *driver)
2135{
2136	device_t child;
2137
2138	DEVICE_IDENTIFY(driver, dev);
2139	TAILQ_FOREACH(child, &dev->children, link) {
2140		if (child->state == DS_NOTPRESENT)
2141			device_probe_and_attach(child);
2142	}
2143}
2144
2145int
2146bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2147    int flags, driver_intr_t *intr, void *arg, void **cookiep)
2148{
2149	/* Propagate up the bus hierarchy until someone handles it. */
2150	if (dev->parent)
2151		return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
2152		    intr, arg, cookiep));
2153	return (EINVAL);
2154}
2155
2156int
2157bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2158    void *cookie)
2159{
2160	/* Propagate up the bus hierarchy until someone handles it. */
2161	if (dev->parent)
2162		return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2163	return (EINVAL);
2164}
2165
2166struct resource *
2167bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2168    u_long start, u_long end, u_long count, u_int flags)
2169{
2170	/* Propagate up the bus hierarchy until someone handles it. */
2171	if (dev->parent)
2172		return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2173		    start, end, count, flags));
2174	return (NULL);
2175}
2176
2177int
2178bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2179    struct resource *r)
2180{
2181	/* Propagate up the bus hierarchy until someone handles it. */
2182	if (dev->parent)
2183		return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
2184		    r));
2185	return (EINVAL);
2186}
2187
2188int
2189bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2190    struct resource *r)
2191{
2192	/* Propagate up the bus hierarchy until someone handles it. */
2193	if (dev->parent)
2194		return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
2195		    r));
2196	return (EINVAL);
2197}
2198
2199int
2200bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2201    int rid, struct resource *r)
2202{
2203	/* Propagate up the bus hierarchy until someone handles it. */
2204	if (dev->parent)
2205		return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2206		    r));
2207	return (EINVAL);
2208}
2209
2210int
2211bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
2212    enum intr_polarity pol)
2213{
2214
2215	/* Propagate up the bus hierarchy until someone handles it. */
2216	if (dev->parent)
2217		return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
2218	return (EINVAL);
2219}
2220
2221int
2222bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2223    u_long *startp, u_long *countp)
2224{
2225	struct resource_list *		rl = NULL;
2226	struct resource_list_entry *	rle = NULL;
2227
2228	rl = BUS_GET_RESOURCE_LIST(dev, child);
2229	if (!rl)
2230		return (EINVAL);
2231
2232	rle = resource_list_find(rl, type, rid);
2233	if (!rle)
2234		return (ENOENT);
2235
2236	if (startp)
2237		*startp = rle->start;
2238	if (countp)
2239		*countp = rle->count;
2240
2241	return (0);
2242}
2243
2244int
2245bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2246    u_long start, u_long count)
2247{
2248	struct resource_list *		rl = NULL;
2249
2250	rl = BUS_GET_RESOURCE_LIST(dev, child);
2251	if (!rl)
2252		return (EINVAL);
2253
2254	resource_list_add(rl, type, rid, start, (start + count - 1), count);
2255
2256	return (0);
2257}
2258
2259void
2260bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2261{
2262	struct resource_list *		rl = NULL;
2263
2264	rl = BUS_GET_RESOURCE_LIST(dev, child);
2265	if (!rl)
2266		return;
2267
2268	resource_list_delete(rl, type, rid);
2269
2270	return;
2271}
2272
2273int
2274bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2275    int rid, struct resource *r)
2276{
2277	struct resource_list *		rl = NULL;
2278
2279	rl = BUS_GET_RESOURCE_LIST(dev, child);
2280	if (!rl)
2281		return (EINVAL);
2282
2283	return (resource_list_release(rl, dev, child, type, rid, r));
2284}
2285
2286struct resource *
2287bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2288    int *rid, u_long start, u_long end, u_long count, u_int flags)
2289{
2290	struct resource_list *		rl = NULL;
2291
2292	rl = BUS_GET_RESOURCE_LIST(dev, child);
2293	if (!rl)
2294		return (NULL);
2295
2296	return (resource_list_alloc(rl, dev, child, type, rid,
2297	    start, end, count, flags));
2298}
2299
2300int
2301bus_generic_child_present(device_t bus, device_t child)
2302{
2303	return (BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2304}
2305
2306/*
2307 * Some convenience functions to make it easier for drivers to use the
2308 * resource-management functions.  All these really do is hide the
2309 * indirection through the parent's method table, making for slightly
2310 * less-wordy code.  In the future, it might make sense for this code
2311 * to maintain some sort of a list of resources allocated by each device.
2312 */
2313struct resource *
2314bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2315    u_long count, u_int flags)
2316{
2317	if (dev->parent == 0)
2318		return (0);
2319	return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2320	    count, flags));
2321}
2322
2323int
2324bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2325{
2326	if (dev->parent == 0)
2327		return (EINVAL);
2328	return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2329}
2330
2331int
2332bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2333{
2334	if (dev->parent == 0)
2335		return (EINVAL);
2336	return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2337}
2338
2339int
2340bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2341{
2342	if (dev->parent == 0)
2343		return (EINVAL);
2344	return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2345}
2346
2347int
2348bus_setup_intr(device_t dev, struct resource *r, int flags,
2349    driver_intr_t handler, void *arg, void **cookiep)
2350{
2351	int error;
2352
2353	if (dev->parent != 0) {
2354		if ((flags &~ INTR_ENTROPY) == (INTR_TYPE_NET | INTR_MPSAFE) &&
2355		    !debug_mpsafenet)
2356			flags &= ~INTR_MPSAFE;
2357		error = BUS_SETUP_INTR(dev->parent, dev, r, flags,
2358		    handler, arg, cookiep);
2359		if (error == 0) {
2360			if (!(flags & (INTR_MPSAFE | INTR_FAST)))
2361				device_printf(dev, "[GIANT-LOCKED]\n");
2362			if (bootverbose && (flags & INTR_MPSAFE))
2363				device_printf(dev, "[MPSAFE]\n");
2364			if (flags & INTR_FAST)
2365				device_printf(dev, "[FAST]\n");
2366		}
2367	} else
2368		error = EINVAL;
2369	return (error);
2370}
2371
2372int
2373bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2374{
2375	if (dev->parent == 0)
2376		return (EINVAL);
2377	return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2378}
2379
2380int
2381bus_set_resource(device_t dev, int type, int rid,
2382    u_long start, u_long count)
2383{
2384	return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2385	    start, count));
2386}
2387
2388int
2389bus_get_resource(device_t dev, int type, int rid,
2390    u_long *startp, u_long *countp)
2391{
2392	return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2393	    startp, countp));
2394}
2395
2396u_long
2397bus_get_resource_start(device_t dev, int type, int rid)
2398{
2399	u_long start, count;
2400	int error;
2401
2402	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2403	    &start, &count);
2404	if (error)
2405		return (0);
2406	return (start);
2407}
2408
2409u_long
2410bus_get_resource_count(device_t dev, int type, int rid)
2411{
2412	u_long start, count;
2413	int error;
2414
2415	error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2416	    &start, &count);
2417	if (error)
2418		return (0);
2419	return (count);
2420}
2421
2422void
2423bus_delete_resource(device_t dev, int type, int rid)
2424{
2425	BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2426}
2427
2428int
2429bus_child_present(device_t child)
2430{
2431	return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2432}
2433
2434int
2435bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2436{
2437	device_t parent;
2438
2439	parent = device_get_parent(child);
2440	if (parent == NULL) {
2441		*buf = '\0';
2442		return (0);
2443	}
2444	return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2445}
2446
2447int
2448bus_child_location_str(device_t child, char *buf, size_t buflen)
2449{
2450	device_t parent;
2451
2452	parent = device_get_parent(child);
2453	if (parent == NULL) {
2454		*buf = '\0';
2455		return (0);
2456	}
2457	return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2458}
2459
2460static int
2461root_print_child(device_t dev, device_t child)
2462{
2463	int	retval = 0;
2464
2465	retval += bus_print_child_header(dev, child);
2466	retval += printf("\n");
2467
2468	return (retval);
2469}
2470
2471static int
2472root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2473    void **cookiep)
2474{
2475	/*
2476	 * If an interrupt mapping gets to here something bad has happened.
2477	 */
2478	panic("root_setup_intr");
2479}
2480
2481/*
2482 * If we get here, assume that the device is permanant and really is
2483 * present in the system.  Removable bus drivers are expected to intercept
2484 * this call long before it gets here.  We return -1 so that drivers that
2485 * really care can check vs -1 or some ERRNO returned higher in the food
2486 * chain.
2487 */
2488static int
2489root_child_present(device_t dev, device_t child)
2490{
2491	return (-1);
2492}
2493
2494static kobj_method_t root_methods[] = {
2495	/* Device interface */
2496	KOBJMETHOD(device_shutdown,	bus_generic_shutdown),
2497	KOBJMETHOD(device_suspend,	bus_generic_suspend),
2498	KOBJMETHOD(device_resume,	bus_generic_resume),
2499
2500	/* Bus interface */
2501	KOBJMETHOD(bus_print_child,	root_print_child),
2502	KOBJMETHOD(bus_read_ivar,	bus_generic_read_ivar),
2503	KOBJMETHOD(bus_write_ivar,	bus_generic_write_ivar),
2504	KOBJMETHOD(bus_setup_intr,	root_setup_intr),
2505	KOBJMETHOD(bus_child_present,	root_child_present),
2506
2507	{ 0, 0 }
2508};
2509
2510static driver_t root_driver = {
2511	"root",
2512	root_methods,
2513	1,			/* no softc */
2514};
2515
2516device_t	root_bus;
2517devclass_t	root_devclass;
2518
2519static int
2520root_bus_module_handler(module_t mod, int what, void* arg)
2521{
2522	switch (what) {
2523	case MOD_LOAD:
2524		TAILQ_INIT(&bus_data_devices);
2525		kobj_class_compile((kobj_class_t) &root_driver);
2526		root_bus = make_device(NULL, "root", 0);
2527		root_bus->desc = "System root bus";
2528		kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
2529		root_bus->driver = &root_driver;
2530		root_bus->state = DS_ATTACHED;
2531		root_devclass = devclass_find_internal("root", 0, FALSE);
2532		devinit();
2533		return (0);
2534
2535	case MOD_SHUTDOWN:
2536		device_shutdown(root_bus);
2537		return (0);
2538	default:
2539		return (EOPNOTSUPP);
2540	}
2541
2542	return (0);
2543}
2544
2545static moduledata_t root_bus_mod = {
2546	"rootbus",
2547	root_bus_module_handler,
2548	0
2549};
2550DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2551
2552void
2553root_bus_configure(void)
2554{
2555	device_t dev;
2556
2557	PDEBUG(("."));
2558
2559	TAILQ_FOREACH(dev, &root_bus->children, link) {
2560		device_probe_and_attach(dev);
2561	}
2562}
2563
2564int
2565driver_module_handler(module_t mod, int what, void *arg)
2566{
2567	int error;
2568	struct driver_module_data *dmd;
2569	devclass_t bus_devclass;
2570	kobj_class_t driver;
2571
2572	dmd = (struct driver_module_data *)arg;
2573	bus_devclass = devclass_find_internal(dmd->dmd_busname, 0, TRUE);
2574	error = 0;
2575
2576	switch (what) {
2577	case MOD_LOAD:
2578		if (dmd->dmd_chainevh)
2579			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2580
2581		driver = dmd->dmd_driver;
2582		PDEBUG(("Loading module: driver %s on bus %s",
2583		    DRIVERNAME(driver), dmd->dmd_busname));
2584		error = devclass_add_driver(bus_devclass, driver);
2585		if (error)
2586			break;
2587
2588		/*
2589		 * If the driver has any base classes, make the
2590		 * devclass inherit from the devclass of the driver's
2591		 * first base class. This will allow the system to
2592		 * search for drivers in both devclasses for children
2593		 * of a device using this driver.
2594		 */
2595		if (driver->baseclasses) {
2596			const char *parentname;
2597			parentname = driver->baseclasses[0]->name;
2598			*dmd->dmd_devclass =
2599				devclass_find_internal(driver->name,
2600				    parentname, TRUE);
2601		} else {
2602			*dmd->dmd_devclass =
2603				devclass_find_internal(driver->name, 0, TRUE);
2604		}
2605		break;
2606
2607	case MOD_UNLOAD:
2608		PDEBUG(("Unloading module: driver %s from bus %s",
2609		    DRIVERNAME(dmd->dmd_driver),
2610		    dmd->dmd_busname));
2611		error = devclass_delete_driver(bus_devclass,
2612		    dmd->dmd_driver);
2613
2614		if (!error && dmd->dmd_chainevh)
2615			error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2616		break;
2617	default:
2618		error = EOPNOTSUPP;
2619		break;
2620	}
2621
2622	return (error);
2623}
2624
2625#ifdef BUS_DEBUG
2626
2627/* the _short versions avoid iteration by not calling anything that prints
2628 * more than oneliners. I love oneliners.
2629 */
2630
2631static void
2632print_device_short(device_t dev, int indent)
2633{
2634	if (!dev)
2635		return;
2636
2637	indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2638	    dev->unit, dev->desc,
2639	    (dev->parent? "":"no "),
2640	    (TAILQ_EMPTY(&dev->children)? "no ":""),
2641	    (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2642	    (dev->flags&DF_FIXEDCLASS? "fixed,":""),
2643	    (dev->flags&DF_WILDCARD? "wildcard,":""),
2644	    (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2645	    (dev->ivars? "":"no "),
2646	    (dev->softc? "":"no "),
2647	    dev->busy));
2648}
2649
2650static void
2651print_device(device_t dev, int indent)
2652{
2653	if (!dev)
2654		return;
2655
2656	print_device_short(dev, indent);
2657
2658	indentprintf(("Parent:\n"));
2659	print_device_short(dev->parent, indent+1);
2660	indentprintf(("Driver:\n"));
2661	print_driver_short(dev->driver, indent+1);
2662	indentprintf(("Devclass:\n"));
2663	print_devclass_short(dev->devclass, indent+1);
2664}
2665
2666void
2667print_device_tree_short(device_t dev, int indent)
2668/* print the device and all its children (indented) */
2669{
2670	device_t child;
2671
2672	if (!dev)
2673		return;
2674
2675	print_device_short(dev, indent);
2676
2677	TAILQ_FOREACH(child, &dev->children, link) {
2678		print_device_tree_short(child, indent+1);
2679	}
2680}
2681
2682void
2683print_device_tree(device_t dev, int indent)
2684/* print the device and all its children (indented) */
2685{
2686	device_t child;
2687
2688	if (!dev)
2689		return;
2690
2691	print_device(dev, indent);
2692
2693	TAILQ_FOREACH(child, &dev->children, link) {
2694		print_device_tree(child, indent+1);
2695	}
2696}
2697
2698static void
2699print_driver_short(driver_t *driver, int indent)
2700{
2701	if (!driver)
2702		return;
2703
2704	indentprintf(("driver %s: softc size = %zd\n",
2705	    driver->name, driver->size));
2706}
2707
2708static void
2709print_driver(driver_t *driver, int indent)
2710{
2711	if (!driver)
2712		return;
2713
2714	print_driver_short(driver, indent);
2715}
2716
2717
2718static void
2719print_driver_list(driver_list_t drivers, int indent)
2720{
2721	driverlink_t driver;
2722
2723	TAILQ_FOREACH(driver, &drivers, link) {
2724		print_driver(driver->driver, indent);
2725	}
2726}
2727
2728static void
2729print_devclass_short(devclass_t dc, int indent)
2730{
2731	if ( !dc )
2732		return;
2733
2734	indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
2735}
2736
2737static void
2738print_devclass(devclass_t dc, int indent)
2739{
2740	int i;
2741
2742	if ( !dc )
2743		return;
2744
2745	print_devclass_short(dc, indent);
2746	indentprintf(("Drivers:\n"));
2747	print_driver_list(dc->drivers, indent+1);
2748
2749	indentprintf(("Devices:\n"));
2750	for (i = 0; i < dc->maxunit; i++)
2751		if (dc->devices[i])
2752			print_device(dc->devices[i], indent+1);
2753}
2754
2755void
2756print_devclass_list_short(void)
2757{
2758	devclass_t dc;
2759
2760	printf("Short listing of devclasses, drivers & devices:\n");
2761	TAILQ_FOREACH(dc, &devclasses, link) {
2762		print_devclass_short(dc, 0);
2763	}
2764}
2765
2766void
2767print_devclass_list(void)
2768{
2769	devclass_t dc;
2770
2771	printf("Full listing of devclasses, drivers & devices:\n");
2772	TAILQ_FOREACH(dc, &devclasses, link) {
2773		print_devclass(dc, 0);
2774	}
2775}
2776
2777#endif
2778
2779/*
2780 * User-space access to the device tree.
2781 *
2782 * We implement a small set of nodes:
2783 *
2784 * hw.bus			Single integer read method to obtain the
2785 *				current generation count.
2786 * hw.bus.devices		Reads the entire device tree in flat space.
2787 * hw.bus.rman			Resource manager interface
2788 *
2789 * We might like to add the ability to scan devclasses and/or drivers to
2790 * determine what else is currently loaded/available.
2791 */
2792
2793static int
2794sysctl_bus(SYSCTL_HANDLER_ARGS)
2795{
2796	struct u_businfo	ubus;
2797
2798	ubus.ub_version = BUS_USER_VERSION;
2799	ubus.ub_generation = bus_data_generation;
2800
2801	return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
2802}
2803SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
2804    "bus-related data");
2805
2806static int
2807sysctl_devices(SYSCTL_HANDLER_ARGS)
2808{
2809	int			*name = (int *)arg1;
2810	u_int			namelen = arg2;
2811	int			index;
2812	struct device		*dev;
2813	struct u_device		udev;	/* XXX this is a bit big */
2814	int			error;
2815
2816	if (namelen != 2)
2817		return (EINVAL);
2818
2819	if (bus_data_generation_check(name[0]))
2820		return (EINVAL);
2821
2822	index = name[1];
2823
2824	/*
2825	 * Scan the list of devices, looking for the requested index.
2826	 */
2827	TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
2828		if (index-- == 0)
2829			break;
2830	}
2831	if (dev == NULL)
2832		return (ENOENT);
2833
2834	/*
2835	 * Populate the return array.
2836	 */
2837	udev.dv_handle = (uintptr_t)dev;
2838	udev.dv_parent = (uintptr_t)dev->parent;
2839	if (dev->nameunit == NULL)
2840		udev.dv_name[0] = '\0';
2841	else
2842		strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
2843
2844	if (dev->desc == NULL)
2845		udev.dv_desc[0] = '\0';
2846	else
2847		strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
2848	if (dev->driver == NULL || dev->driver->name == NULL)
2849		udev.dv_drivername[0] = '\0';
2850	else
2851		strlcpy(udev.dv_drivername, dev->driver->name,
2852		    sizeof(udev.dv_drivername));
2853	udev.dv_pnpinfo[0] = '\0';
2854	udev.dv_location[0] = '\0';
2855	bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
2856	bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
2857	udev.dv_devflags = dev->devflags;
2858	udev.dv_flags = dev->flags;
2859	udev.dv_state = dev->state;
2860	error = SYSCTL_OUT(req, &udev, sizeof(udev));
2861	return (error);
2862}
2863
2864SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
2865    "system device tree");
2866
2867/*
2868 * Sysctl interface for scanning the resource lists.
2869 *
2870 * We take two input parameters; the index into the list of resource
2871 * managers, and the resource offset into the list.
2872 */
2873static int
2874sysctl_rman(SYSCTL_HANDLER_ARGS)
2875{
2876	int			*name = (int *)arg1;
2877	u_int			namelen = arg2;
2878	int			rman_idx, res_idx;
2879	struct rman		*rm;
2880	struct resource		*res;
2881	struct u_rman		urm;
2882	struct u_resource	ures;
2883	int			error;
2884
2885	if (namelen != 3)
2886		return (EINVAL);
2887
2888	if (bus_data_generation_check(name[0]))
2889		return (EINVAL);
2890	rman_idx = name[1];
2891	res_idx = name[2];
2892
2893	/*
2894	 * Find the indexed resource manager
2895	 */
2896	TAILQ_FOREACH(rm, &rman_head, rm_link) {
2897		if (rman_idx-- == 0)
2898			break;
2899	}
2900	if (rm == NULL)
2901		return (ENOENT);
2902
2903	/*
2904	 * If the resource index is -1, we want details on the
2905	 * resource manager.
2906	 */
2907	if (res_idx == -1) {
2908		urm.rm_handle = (uintptr_t)rm;
2909		strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
2910		urm.rm_start = rm->rm_start;
2911		urm.rm_size = rm->rm_end - rm->rm_start + 1;
2912		urm.rm_type = rm->rm_type;
2913
2914		error = SYSCTL_OUT(req, &urm, sizeof(urm));
2915		return (error);
2916	}
2917
2918	/*
2919	 * Find the indexed resource and return it.
2920	 */
2921	TAILQ_FOREACH(res, &rm->rm_list, r_link) {
2922		if (res_idx-- == 0) {
2923			ures.r_handle = (uintptr_t)res;
2924			ures.r_parent = (uintptr_t)res->r_rm;
2925			ures.r_device = (uintptr_t)res->r_dev;
2926			if (res->r_dev != NULL) {
2927				if (device_get_name(res->r_dev) != NULL) {
2928					snprintf(ures.r_devname, RM_TEXTLEN,
2929					    "%s%d",
2930					    device_get_name(res->r_dev),
2931					    device_get_unit(res->r_dev));
2932				} else {
2933					strlcpy(ures.r_devname, "nomatch",
2934					    RM_TEXTLEN);
2935				}
2936			} else {
2937				ures.r_devname[0] = '\0';
2938			}
2939			ures.r_start = res->r_start;
2940			ures.r_size = res->r_end - res->r_start + 1;
2941			ures.r_flags = res->r_flags;
2942
2943			error = SYSCTL_OUT(req, &ures, sizeof(ures));
2944			return (error);
2945		}
2946	}
2947	return (ENOENT);
2948}
2949
2950SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
2951    "kernel resource manager");
2952
2953int
2954bus_data_generation_check(int generation)
2955{
2956	if (generation != bus_data_generation)
2957		return (1);
2958
2959	/* XXX generate optimised lists here? */
2960	return (0);
2961}
2962
2963void
2964bus_data_generation_update(void)
2965{
2966	bus_data_generation++;
2967}
2968