Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / sys / kern / subr_bus.c
blobc623ce27b39299aad64d12e57083b93b596b96a5
1 /*-
2 * Copyright (c) 1997,1998,2003 Doug Rabson
3 * All rights reserved.
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_bus.h"
32 #include <sys/param.h>
33 #include <sys/conf.h>
34 #include <sys/filio.h>
35 #include <sys/lock.h>
36 #include <sys/kernel.h>
37 #include <sys/kobj.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/poll.h>
42 #include <sys/proc.h>
43 #include <sys/condvar.h>
44 #include <sys/queue.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <sys/selinfo.h>
48 #include <sys/signalvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/uio.h>
52 #include <sys/bus.h>
53 #include <sys/interrupt.h>
55 #include <machine/stdarg.h>
57 #include <vm/uma.h>
59 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
60 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
63 * Used to attach drivers to devclasses.
65 typedef struct driverlink *driverlink_t;
66 struct driverlink {
67 kobj_class_t driver;
68 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
72 * Forward declarations
74 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
75 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
76 typedef TAILQ_HEAD(device_list, device) device_list_t;
78 struct 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 */
86 struct sysctl_ctx_list sysctl_ctx;
87 struct sysctl_oid *sysctl_tree;
90 /**
91 * @brief Implementation of device.
93 struct device {
95 * A device is a kernel object. The first field must be the
96 * current ops table for the object.
98 KOBJ_FIELDS;
101 * Device hierarchy.
103 TAILQ_ENTRY(device) link; /**< list of devices in parent */
104 TAILQ_ENTRY(device) devlink; /**< global device list membership */
105 device_t parent; /**< parent of this device */
106 device_list_t children; /**< list of child devices */
109 * Details of this device.
111 driver_t *driver; /**< current driver */
112 devclass_t devclass; /**< current device class */
113 int unit; /**< current unit number */
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; /**< current device state */
118 u_int32_t devflags; /**< api level flags for device_get_flags() */
119 u_short flags; /**< internal device 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 #define DF_REBID 128 /* Can rebid after attach */
128 u_char order; /**< order from device_add_child_ordered() */
129 u_char pad;
130 void *ivars; /**< instance variables */
131 void *softc; /**< current driver's variables */
133 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
134 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
137 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
138 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
140 #ifdef BUS_DEBUG
142 static int bus_debug = 1;
143 TUNABLE_INT("bus.debug", &bus_debug);
144 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
145 "Debug bus code");
147 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
148 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
149 #define DRIVERNAME(d) ((d)? d->name : "no driver")
150 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
153 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
154 * prevent syslog from deleting initial spaces
156 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
158 static void print_device_short(device_t dev, int indent);
159 static void print_device(device_t dev, int indent);
160 void print_device_tree_short(device_t dev, int indent);
161 void print_device_tree(device_t dev, int indent);
162 static void print_driver_short(driver_t *driver, int indent);
163 static void print_driver(driver_t *driver, int indent);
164 static void print_driver_list(driver_list_t drivers, int indent);
165 static void print_devclass_short(devclass_t dc, int indent);
166 static void print_devclass(devclass_t dc, int indent);
167 void print_devclass_list_short(void);
168 void print_devclass_list(void);
170 #else
171 /* Make the compiler ignore the function calls */
172 #define PDEBUG(a) /* nop */
173 #define DEVICENAME(d) /* nop */
174 #define DRIVERNAME(d) /* nop */
175 #define DEVCLANAME(d) /* nop */
177 #define print_device_short(d,i) /* nop */
178 #define print_device(d,i) /* nop */
179 #define print_device_tree_short(d,i) /* nop */
180 #define print_device_tree(d,i) /* nop */
181 #define print_driver_short(d,i) /* nop */
182 #define print_driver(d,i) /* nop */
183 #define print_driver_list(d,i) /* nop */
184 #define print_devclass_short(d,i) /* nop */
185 #define print_devclass(d,i) /* nop */
186 #define print_devclass_list_short() /* nop */
187 #define print_devclass_list() /* nop */
188 #endif
191 * dev sysctl tree
194 enum {
195 DEVCLASS_SYSCTL_PARENT,
198 static int
199 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
201 devclass_t dc = (devclass_t)arg1;
202 const char *value;
204 switch (arg2) {
205 case DEVCLASS_SYSCTL_PARENT:
206 value = dc->parent ? dc->parent->name : "";
207 break;
208 default:
209 return (EINVAL);
211 return (SYSCTL_OUT(req, value, strlen(value)));
214 static void
215 devclass_sysctl_init(devclass_t dc)
218 if (dc->sysctl_tree != NULL)
219 return;
220 sysctl_ctx_init(&dc->sysctl_ctx);
221 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
222 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
223 CTLFLAG_RD, 0, "");
224 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
225 OID_AUTO, "%parent", CTLFLAG_RD,
226 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
227 "parent class");
230 enum {
231 DEVICE_SYSCTL_DESC,
232 DEVICE_SYSCTL_DRIVER,
233 DEVICE_SYSCTL_LOCATION,
234 DEVICE_SYSCTL_PNPINFO,
235 DEVICE_SYSCTL_PARENT,
238 static int
239 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
241 device_t dev = (device_t)arg1;
242 const char *value;
243 char *buf;
244 int error;
246 buf = NULL;
247 switch (arg2) {
248 case DEVICE_SYSCTL_DESC:
249 value = dev->desc ? dev->desc : "";
250 break;
251 case DEVICE_SYSCTL_DRIVER:
252 value = dev->driver ? dev->driver->name : "";
253 break;
254 case DEVICE_SYSCTL_LOCATION:
255 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
256 bus_child_location_str(dev, buf, 1024);
257 break;
258 case DEVICE_SYSCTL_PNPINFO:
259 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
260 bus_child_pnpinfo_str(dev, buf, 1024);
261 break;
262 case DEVICE_SYSCTL_PARENT:
263 value = dev->parent ? dev->parent->nameunit : "";
264 break;
265 default:
266 return (EINVAL);
268 error = SYSCTL_OUT(req, value, strlen(value));
269 if (buf != NULL)
270 free(buf, M_BUS);
271 return (error);
274 static void
275 device_sysctl_init(device_t dev)
277 devclass_t dc = dev->devclass;
279 if (dev->sysctl_tree != NULL)
280 return;
281 devclass_sysctl_init(dc);
282 sysctl_ctx_init(&dev->sysctl_ctx);
283 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
284 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
285 dev->nameunit + strlen(dc->name),
286 CTLFLAG_RD, 0, "");
287 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
288 OID_AUTO, "%desc", CTLFLAG_RD,
289 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
290 "device description");
291 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
292 OID_AUTO, "%driver", CTLFLAG_RD,
293 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
294 "device driver name");
295 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
296 OID_AUTO, "%location", CTLFLAG_RD,
297 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
298 "device location relative to parent");
299 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
300 OID_AUTO, "%pnpinfo", CTLFLAG_RD,
301 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
302 "device identification");
303 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
304 OID_AUTO, "%parent", CTLFLAG_RD,
305 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
306 "parent device");
309 static void
310 device_sysctl_update(device_t dev)
312 devclass_t dc = dev->devclass;
314 if (dev->sysctl_tree == NULL)
315 return;
316 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
319 static void
320 device_sysctl_fini(device_t dev)
322 if (dev->sysctl_tree == NULL)
323 return;
324 sysctl_ctx_free(&dev->sysctl_ctx);
325 dev->sysctl_tree = NULL;
329 * /dev/devctl implementation
333 * This design allows only one reader for /dev/devctl. This is not desirable
334 * in the long run, but will get a lot of hair out of this implementation.
335 * Maybe we should make this device a clonable device.
337 * Also note: we specifically do not attach a device to the device_t tree
338 * to avoid potential chicken and egg problems. One could argue that all
339 * of this belongs to the root node. One could also further argue that the
340 * sysctl interface that we have not might more properly be an ioctl
341 * interface, but at this stage of the game, I'm not inclined to rock that
342 * boat.
344 * I'm also not sure that the SIGIO support is done correctly or not, as
345 * I copied it from a driver that had SIGIO support that likely hasn't been
346 * tested since 3.4 or 2.2.8!
349 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
350 static int devctl_disable = 0;
351 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
352 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
353 sysctl_devctl_disable, "I", "devctl disable");
355 static d_open_t devopen;
356 static d_close_t devclose;
357 static d_read_t devread;
358 static d_ioctl_t devioctl;
359 static d_poll_t devpoll;
361 static struct cdevsw dev_cdevsw = {
362 .d_version = D_VERSION,
363 .d_flags = D_NEEDGIANT,
364 .d_open = devopen,
365 .d_close = devclose,
366 .d_read = devread,
367 .d_ioctl = devioctl,
368 .d_poll = devpoll,
369 .d_name = "devctl",
372 struct dev_event_info
374 char *dei_data;
375 TAILQ_ENTRY(dev_event_info) dei_link;
378 TAILQ_HEAD(devq, dev_event_info);
380 static struct dev_softc
382 int inuse;
383 int nonblock;
384 struct mtx mtx;
385 struct cv cv;
386 struct selinfo sel;
387 struct devq devq;
388 struct proc *async_proc;
389 } devsoftc;
391 static struct cdev *devctl_dev;
393 static void
394 devinit(void)
396 devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
397 "devctl");
398 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
399 cv_init(&devsoftc.cv, "dev cv");
400 TAILQ_INIT(&devsoftc.devq);
403 static int
404 devopen(struct cdev *dev, int oflags, int devtype, d_thread_t *td)
406 if (devsoftc.inuse)
407 return (EBUSY);
408 /* move to init */
409 devsoftc.inuse = 1;
410 devsoftc.nonblock = 0;
411 devsoftc.async_proc = NULL;
412 return (0);
415 static int
416 devclose(struct cdev *dev, int fflag, int devtype, d_thread_t *td)
418 devsoftc.inuse = 0;
419 mtx_lock(&devsoftc.mtx);
420 cv_broadcast(&devsoftc.cv);
421 mtx_unlock(&devsoftc.mtx);
423 return (0);
427 * The read channel for this device is used to report changes to
428 * userland in realtime. We are required to free the data as well as
429 * the n1 object because we allocate them separately. Also note that
430 * we return one record at a time. If you try to read this device a
431 * character at a time, you will lose the rest of the data. Listening
432 * programs are expected to cope.
434 static int
435 devread(struct cdev *dev, struct uio *uio, int ioflag)
437 struct dev_event_info *n1;
438 int rv;
440 mtx_lock(&devsoftc.mtx);
441 while (TAILQ_EMPTY(&devsoftc.devq)) {
442 if (devsoftc.nonblock) {
443 mtx_unlock(&devsoftc.mtx);
444 return (EAGAIN);
446 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
447 if (rv) {
449 * Need to translate ERESTART to EINTR here? -- jake
451 mtx_unlock(&devsoftc.mtx);
452 return (rv);
455 n1 = TAILQ_FIRST(&devsoftc.devq);
456 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
457 mtx_unlock(&devsoftc.mtx);
458 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
459 free(n1->dei_data, M_BUS);
460 free(n1, M_BUS);
461 return (rv);
464 static int
465 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
467 switch (cmd) {
469 case FIONBIO:
470 if (*(int*)data)
471 devsoftc.nonblock = 1;
472 else
473 devsoftc.nonblock = 0;
474 return (0);
475 case FIOASYNC:
476 if (*(int*)data)
477 devsoftc.async_proc = td->td_proc;
478 else
479 devsoftc.async_proc = NULL;
480 return (0);
482 /* (un)Support for other fcntl() calls. */
483 case FIOCLEX:
484 case FIONCLEX:
485 case FIONREAD:
486 case FIOSETOWN:
487 case FIOGETOWN:
488 default:
489 break;
491 return (ENOTTY);
494 static int
495 devpoll(struct cdev *dev, int events, d_thread_t *td)
497 int revents = 0;
499 mtx_lock(&devsoftc.mtx);
500 if (events & (POLLIN | POLLRDNORM)) {
501 if (!TAILQ_EMPTY(&devsoftc.devq))
502 revents = events & (POLLIN | POLLRDNORM);
503 else
504 selrecord(td, &devsoftc.sel);
506 mtx_unlock(&devsoftc.mtx);
508 return (revents);
512 * @brief Return whether the userland process is running
514 boolean_t
515 devctl_process_running(void)
517 return (devsoftc.inuse == 1);
521 * @brief Queue data to be read from the devctl device
523 * Generic interface to queue data to the devctl device. It is
524 * assumed that @p data is properly formatted. It is further assumed
525 * that @p data is allocated using the M_BUS malloc type.
527 void
528 devctl_queue_data(char *data)
530 struct dev_event_info *n1 = NULL;
531 struct proc *p;
533 n1 = malloc(sizeof(*n1), M_BUS, M_NOWAIT);
534 if (n1 == NULL)
535 return;
536 n1->dei_data = data;
537 mtx_lock(&devsoftc.mtx);
538 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
539 cv_broadcast(&devsoftc.cv);
540 mtx_unlock(&devsoftc.mtx);
541 selwakeup(&devsoftc.sel);
542 p = devsoftc.async_proc;
543 if (p != NULL) {
544 PROC_LOCK(p);
545 psignal(p, SIGIO);
546 PROC_UNLOCK(p);
551 * @brief Send a 'notification' to userland, using standard ways
553 void
554 devctl_notify(const char *system, const char *subsystem, const char *type,
555 const char *data)
557 int len = 0;
558 char *msg;
560 if (system == NULL)
561 return; /* BOGUS! Must specify system. */
562 if (subsystem == NULL)
563 return; /* BOGUS! Must specify subsystem. */
564 if (type == NULL)
565 return; /* BOGUS! Must specify type. */
566 len += strlen(" system=") + strlen(system);
567 len += strlen(" subsystem=") + strlen(subsystem);
568 len += strlen(" type=") + strlen(type);
569 /* add in the data message plus newline. */
570 if (data != NULL)
571 len += strlen(data);
572 len += 3; /* '!', '\n', and NUL */
573 msg = malloc(len, M_BUS, M_NOWAIT);
574 if (msg == NULL)
575 return; /* Drop it on the floor */
576 if (data != NULL)
577 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
578 system, subsystem, type, data);
579 else
580 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
581 system, subsystem, type);
582 devctl_queue_data(msg);
586 * Common routine that tries to make sending messages as easy as possible.
587 * We allocate memory for the data, copy strings into that, but do not
588 * free it unless there's an error. The dequeue part of the driver should
589 * free the data. We don't send data when the device is disabled. We do
590 * send data, even when we have no listeners, because we wish to avoid
591 * races relating to startup and restart of listening applications.
593 * devaddq is designed to string together the type of event, with the
594 * object of that event, plus the plug and play info and location info
595 * for that event. This is likely most useful for devices, but less
596 * useful for other consumers of this interface. Those should use
597 * the devctl_queue_data() interface instead.
599 static void
600 devaddq(const char *type, const char *what, device_t dev)
602 char *data = NULL;
603 char *loc = NULL;
604 char *pnp = NULL;
605 const char *parstr;
607 if (devctl_disable)
608 return;
609 data = malloc(1024, M_BUS, M_NOWAIT);
610 if (data == NULL)
611 goto bad;
613 /* get the bus specific location of this device */
614 loc = malloc(1024, M_BUS, M_NOWAIT);
615 if (loc == NULL)
616 goto bad;
617 *loc = '\0';
618 bus_child_location_str(dev, loc, 1024);
620 /* Get the bus specific pnp info of this device */
621 pnp = malloc(1024, M_BUS, M_NOWAIT);
622 if (pnp == NULL)
623 goto bad;
624 *pnp = '\0';
625 bus_child_pnpinfo_str(dev, pnp, 1024);
627 /* Get the parent of this device, or / if high enough in the tree. */
628 if (device_get_parent(dev) == NULL)
629 parstr = "."; /* Or '/' ? */
630 else
631 parstr = device_get_nameunit(device_get_parent(dev));
632 /* String it all together. */
633 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
634 parstr);
635 free(loc, M_BUS);
636 free(pnp, M_BUS);
637 devctl_queue_data(data);
638 return;
639 bad:
640 free(pnp, M_BUS);
641 free(loc, M_BUS);
642 free(data, M_BUS);
643 return;
647 * A device was added to the tree. We are called just after it successfully
648 * attaches (that is, probe and attach success for this device). No call
649 * is made if a device is merely parented into the tree. See devnomatch
650 * if probe fails. If attach fails, no notification is sent (but maybe
651 * we should have a different message for this).
653 static void
654 devadded(device_t dev)
656 char *pnp = NULL;
657 char *tmp = NULL;
659 pnp = malloc(1024, M_BUS, M_NOWAIT);
660 if (pnp == NULL)
661 goto fail;
662 tmp = malloc(1024, M_BUS, M_NOWAIT);
663 if (tmp == NULL)
664 goto fail;
665 *pnp = '\0';
666 bus_child_pnpinfo_str(dev, pnp, 1024);
667 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
668 devaddq("+", tmp, dev);
669 fail:
670 if (pnp != NULL)
671 free(pnp, M_BUS);
672 if (tmp != NULL)
673 free(tmp, M_BUS);
674 return;
678 * A device was removed from the tree. We are called just before this
679 * happens.
681 static void
682 devremoved(device_t dev)
684 char *pnp = NULL;
685 char *tmp = NULL;
687 pnp = malloc(1024, M_BUS, M_NOWAIT);
688 if (pnp == NULL)
689 goto fail;
690 tmp = malloc(1024, M_BUS, M_NOWAIT);
691 if (tmp == NULL)
692 goto fail;
693 *pnp = '\0';
694 bus_child_pnpinfo_str(dev, pnp, 1024);
695 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
696 devaddq("-", tmp, dev);
697 fail:
698 if (pnp != NULL)
699 free(pnp, M_BUS);
700 if (tmp != NULL)
701 free(tmp, M_BUS);
702 return;
706 * Called when there's no match for this device. This is only called
707 * the first time that no match happens, so we don't keep getitng this
708 * message. Should that prove to be undesirable, we can change it.
709 * This is called when all drivers that can attach to a given bus
710 * decline to accept this device. Other errrors may not be detected.
712 static void
713 devnomatch(device_t dev)
715 devaddq("?", "", dev);
718 static int
719 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
721 struct dev_event_info *n1;
722 int dis, error;
724 dis = devctl_disable;
725 error = sysctl_handle_int(oidp, &dis, 0, req);
726 if (error || !req->newptr)
727 return (error);
728 mtx_lock(&devsoftc.mtx);
729 devctl_disable = dis;
730 if (dis) {
731 while (!TAILQ_EMPTY(&devsoftc.devq)) {
732 n1 = TAILQ_FIRST(&devsoftc.devq);
733 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
734 free(n1->dei_data, M_BUS);
735 free(n1, M_BUS);
738 mtx_unlock(&devsoftc.mtx);
739 return (0);
742 /* End of /dev/devctl code */
744 TAILQ_HEAD(,device) bus_data_devices;
745 static int bus_data_generation = 1;
747 kobj_method_t null_methods[] = {
748 { 0, 0 }
751 DEFINE_CLASS(null, null_methods, 0);
754 * Devclass implementation
757 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
761 * @internal
762 * @brief Find or create a device class
764 * If a device class with the name @p classname exists, return it,
765 * otherwise if @p create is non-zero create and return a new device
766 * class.
768 * If @p parentname is non-NULL, the parent of the devclass is set to
769 * the devclass of that name.
771 * @param classname the devclass name to find or create
772 * @param parentname the parent devclass name or @c NULL
773 * @param create non-zero to create a devclass
775 static devclass_t
776 devclass_find_internal(const char *classname, const char *parentname,
777 int create)
779 devclass_t dc;
781 PDEBUG(("looking for %s", classname));
782 if (!classname)
783 return (NULL);
785 TAILQ_FOREACH(dc, &devclasses, link) {
786 if (!strcmp(dc->name, classname))
787 break;
790 if (create && !dc) {
791 PDEBUG(("creating %s", classname));
792 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
793 M_BUS, M_NOWAIT|M_ZERO);
794 if (!dc)
795 return (NULL);
796 dc->parent = NULL;
797 dc->name = (char*) (dc + 1);
798 strcpy(dc->name, classname);
799 TAILQ_INIT(&dc->drivers);
800 TAILQ_INSERT_TAIL(&devclasses, dc, link);
802 bus_data_generation_update();
806 * If a parent class is specified, then set that as our parent so
807 * that this devclass will support drivers for the parent class as
808 * well. If the parent class has the same name don't do this though
809 * as it creates a cycle that can trigger an infinite loop in
810 * device_probe_child() if a device exists for which there is no
811 * suitable driver.
813 if (parentname && dc && !dc->parent &&
814 strcmp(classname, parentname) != 0) {
815 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
818 return (dc);
822 * @brief Create a device class
824 * If a device class with the name @p classname exists, return it,
825 * otherwise create and return a new device class.
827 * @param classname the devclass name to find or create
829 devclass_t
830 devclass_create(const char *classname)
832 return (devclass_find_internal(classname, NULL, TRUE));
836 * @brief Find a device class
838 * If a device class with the name @p classname exists, return it,
839 * otherwise return @c NULL.
841 * @param classname the devclass name to find
843 devclass_t
844 devclass_find(const char *classname)
846 return (devclass_find_internal(classname, NULL, FALSE));
850 * @brief Add a device driver to a device class
852 * Add a device driver to a devclass. This is normally called
853 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
854 * all devices in the devclass will be called to allow them to attempt
855 * to re-probe any unmatched children.
857 * @param dc the devclass to edit
858 * @param driver the driver to register
861 devclass_add_driver(devclass_t dc, driver_t *driver)
863 driverlink_t dl;
864 int i;
866 PDEBUG(("%s", DRIVERNAME(driver)));
868 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
869 if (!dl)
870 return (ENOMEM);
873 * Compile the driver's methods. Also increase the reference count
874 * so that the class doesn't get freed when the last instance
875 * goes. This means we can safely use static methods and avoids a
876 * double-free in devclass_delete_driver.
878 kobj_class_compile((kobj_class_t) driver);
881 * Make sure the devclass which the driver is implementing exists.
883 devclass_find_internal(driver->name, NULL, TRUE);
885 dl->driver = driver;
886 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
887 driver->refs++; /* XXX: kobj_mtx */
890 * Call BUS_DRIVER_ADDED for any existing busses in this class.
892 for (i = 0; i < dc->maxunit; i++)
893 if (dc->devices[i])
894 BUS_DRIVER_ADDED(dc->devices[i], driver);
896 bus_data_generation_update();
897 return (0);
901 * @brief Delete a device driver from a device class
903 * Delete a device driver from a devclass. This is normally called
904 * automatically by DRIVER_MODULE().
906 * If the driver is currently attached to any devices,
907 * devclass_delete_driver() will first attempt to detach from each
908 * device. If one of the detach calls fails, the driver will not be
909 * deleted.
911 * @param dc the devclass to edit
912 * @param driver the driver to unregister
915 devclass_delete_driver(devclass_t busclass, driver_t *driver)
917 devclass_t dc = devclass_find(driver->name);
918 driverlink_t dl;
919 device_t dev;
920 int i;
921 int error;
923 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
925 if (!dc)
926 return (0);
929 * Find the link structure in the bus' list of drivers.
931 TAILQ_FOREACH(dl, &busclass->drivers, link) {
932 if (dl->driver == driver)
933 break;
936 if (!dl) {
937 PDEBUG(("%s not found in %s list", driver->name,
938 busclass->name));
939 return (ENOENT);
943 * Disassociate from any devices. We iterate through all the
944 * devices in the devclass of the driver and detach any which are
945 * using the driver and which have a parent in the devclass which
946 * we are deleting from.
948 * Note that since a driver can be in multiple devclasses, we
949 * should not detach devices which are not children of devices in
950 * the affected devclass.
952 for (i = 0; i < dc->maxunit; i++) {
953 if (dc->devices[i]) {
954 dev = dc->devices[i];
955 if (dev->driver == driver && dev->parent &&
956 dev->parent->devclass == busclass) {
957 if ((error = device_detach(dev)) != 0)
958 return (error);
959 device_set_driver(dev, NULL);
964 TAILQ_REMOVE(&busclass->drivers, dl, link);
965 free(dl, M_BUS);
967 /* XXX: kobj_mtx */
968 driver->refs--;
969 if (driver->refs == 0)
970 kobj_class_free((kobj_class_t) driver);
972 bus_data_generation_update();
973 return (0);
977 * @brief Quiesces a set of device drivers from a device class
979 * Quiesce a device driver from a devclass. This is normally called
980 * automatically by DRIVER_MODULE().
982 * If the driver is currently attached to any devices,
983 * devclass_quiesece_driver() will first attempt to quiesce each
984 * device.
986 * @param dc the devclass to edit
987 * @param driver the driver to unregister
990 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
992 devclass_t dc = devclass_find(driver->name);
993 driverlink_t dl;
994 device_t dev;
995 int i;
996 int error;
998 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1000 if (!dc)
1001 return (0);
1004 * Find the link structure in the bus' list of drivers.
1006 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1007 if (dl->driver == driver)
1008 break;
1011 if (!dl) {
1012 PDEBUG(("%s not found in %s list", driver->name,
1013 busclass->name));
1014 return (ENOENT);
1018 * Quiesce all devices. We iterate through all the devices in
1019 * the devclass of the driver and quiesce any which are using
1020 * the driver and which have a parent in the devclass which we
1021 * are quiescing.
1023 * Note that since a driver can be in multiple devclasses, we
1024 * should not quiesce devices which are not children of
1025 * devices in the affected devclass.
1027 for (i = 0; i < dc->maxunit; i++) {
1028 if (dc->devices[i]) {
1029 dev = dc->devices[i];
1030 if (dev->driver == driver && dev->parent &&
1031 dev->parent->devclass == busclass) {
1032 if ((error = device_quiesce(dev)) != 0)
1033 return (error);
1038 return (0);
1042 * @internal
1044 static driverlink_t
1045 devclass_find_driver_internal(devclass_t dc, const char *classname)
1047 driverlink_t dl;
1049 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1051 TAILQ_FOREACH(dl, &dc->drivers, link) {
1052 if (!strcmp(dl->driver->name, classname))
1053 return (dl);
1056 PDEBUG(("not found"));
1057 return (NULL);
1061 * @brief Search a devclass for a driver
1063 * This function searches the devclass's list of drivers and returns
1064 * the first driver whose name is @p classname or @c NULL if there is
1065 * no driver of that name.
1067 * @param dc the devclass to search
1068 * @param classname the driver name to search for
1070 kobj_class_t
1071 devclass_find_driver(devclass_t dc, const char *classname)
1073 driverlink_t dl;
1075 dl = devclass_find_driver_internal(dc, classname);
1076 if (dl)
1077 return (dl->driver);
1078 return (NULL);
1082 * @brief Return the name of the devclass
1084 const char *
1085 devclass_get_name(devclass_t dc)
1087 return (dc->name);
1091 * @brief Find a device given a unit number
1093 * @param dc the devclass to search
1094 * @param unit the unit number to search for
1096 * @returns the device with the given unit number or @c
1097 * NULL if there is no such device
1099 device_t
1100 devclass_get_device(devclass_t dc, int unit)
1102 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1103 return (NULL);
1104 return (dc->devices[unit]);
1108 * @brief Find the softc field of a device given a unit number
1110 * @param dc the devclass to search
1111 * @param unit the unit number to search for
1113 * @returns the softc field of the device with the given
1114 * unit number or @c NULL if there is no such
1115 * device
1117 void *
1118 devclass_get_softc(devclass_t dc, int unit)
1120 device_t dev;
1122 dev = devclass_get_device(dc, unit);
1123 if (!dev)
1124 return (NULL);
1126 return (device_get_softc(dev));
1130 * @brief Get a list of devices in the devclass
1132 * An array containing a list of all the devices in the given devclass
1133 * is allocated and returned in @p *devlistp. The number of devices
1134 * in the array is returned in @p *devcountp. The caller should free
1135 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1137 * @param dc the devclass to examine
1138 * @param devlistp points at location for array pointer return
1139 * value
1140 * @param devcountp points at location for array size return value
1142 * @retval 0 success
1143 * @retval ENOMEM the array allocation failed
1146 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1148 int count, i;
1149 device_t *list;
1151 count = devclass_get_count(dc);
1152 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1153 if (!list)
1154 return (ENOMEM);
1156 count = 0;
1157 for (i = 0; i < dc->maxunit; i++) {
1158 if (dc->devices[i]) {
1159 list[count] = dc->devices[i];
1160 count++;
1164 *devlistp = list;
1165 *devcountp = count;
1167 return (0);
1171 * @brief Get a list of drivers in the devclass
1173 * An array containing a list of pointers to all the drivers in the
1174 * given devclass is allocated and returned in @p *listp. The number
1175 * of drivers in the array is returned in @p *countp. The caller should
1176 * free the array using @c free(p, M_TEMP).
1178 * @param dc the devclass to examine
1179 * @param listp gives location for array pointer return value
1180 * @param countp gives location for number of array elements
1181 * return value
1183 * @retval 0 success
1184 * @retval ENOMEM the array allocation failed
1187 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1189 driverlink_t dl;
1190 driver_t **list;
1191 int count;
1193 count = 0;
1194 TAILQ_FOREACH(dl, &dc->drivers, link)
1195 count++;
1196 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1197 if (list == NULL)
1198 return (ENOMEM);
1200 count = 0;
1201 TAILQ_FOREACH(dl, &dc->drivers, link) {
1202 list[count] = dl->driver;
1203 count++;
1205 *listp = list;
1206 *countp = count;
1208 return (0);
1212 * @brief Get the number of devices in a devclass
1214 * @param dc the devclass to examine
1217 devclass_get_count(devclass_t dc)
1219 int count, i;
1221 count = 0;
1222 for (i = 0; i < dc->maxunit; i++)
1223 if (dc->devices[i])
1224 count++;
1225 return (count);
1229 * @brief Get the maximum unit number used in a devclass
1231 * Note that this is one greater than the highest currently-allocated
1232 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1233 * that not even the devclass has been allocated yet.
1235 * @param dc the devclass to examine
1238 devclass_get_maxunit(devclass_t dc)
1240 if (dc == NULL)
1241 return (-1);
1242 return (dc->maxunit);
1246 * @brief Find a free unit number in a devclass
1248 * This function searches for the first unused unit number greater
1249 * that or equal to @p unit.
1251 * @param dc the devclass to examine
1252 * @param unit the first unit number to check
1255 devclass_find_free_unit(devclass_t dc, int unit)
1257 if (dc == NULL)
1258 return (unit);
1259 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1260 unit++;
1261 return (unit);
1265 * @brief Set the parent of a devclass
1267 * The parent class is normally initialised automatically by
1268 * DRIVER_MODULE().
1270 * @param dc the devclass to edit
1271 * @param pdc the new parent devclass
1273 void
1274 devclass_set_parent(devclass_t dc, devclass_t pdc)
1276 dc->parent = pdc;
1280 * @brief Get the parent of a devclass
1282 * @param dc the devclass to examine
1284 devclass_t
1285 devclass_get_parent(devclass_t dc)
1287 return (dc->parent);
1290 struct sysctl_ctx_list *
1291 devclass_get_sysctl_ctx(devclass_t dc)
1293 return (&dc->sysctl_ctx);
1296 struct sysctl_oid *
1297 devclass_get_sysctl_tree(devclass_t dc)
1299 return (dc->sysctl_tree);
1303 * @internal
1304 * @brief Allocate a unit number
1306 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1307 * will do). The allocated unit number is returned in @p *unitp.
1309 * @param dc the devclass to allocate from
1310 * @param unitp points at the location for the allocated unit
1311 * number
1313 * @retval 0 success
1314 * @retval EEXIST the requested unit number is already allocated
1315 * @retval ENOMEM memory allocation failure
1317 static int
1318 devclass_alloc_unit(devclass_t dc, int *unitp)
1320 int unit = *unitp;
1322 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1324 /* If we were given a wired unit number, check for existing device */
1325 /* XXX imp XXX */
1326 if (unit != -1) {
1327 if (unit >= 0 && unit < dc->maxunit &&
1328 dc->devices[unit] != NULL) {
1329 if (bootverbose)
1330 printf("%s: %s%d already exists; skipping it\n",
1331 dc->name, dc->name, *unitp);
1332 return (EEXIST);
1334 } else {
1335 /* Unwired device, find the next available slot for it */
1336 unit = 0;
1337 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1338 unit++;
1342 * We've selected a unit beyond the length of the table, so let's
1343 * extend the table to make room for all units up to and including
1344 * this one.
1346 if (unit >= dc->maxunit) {
1347 device_t *newlist;
1348 int newsize;
1350 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1351 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1352 if (!newlist)
1353 return (ENOMEM);
1354 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
1355 bzero(newlist + dc->maxunit,
1356 sizeof(device_t) * (newsize - dc->maxunit));
1357 if (dc->devices)
1358 free(dc->devices, M_BUS);
1359 dc->devices = newlist;
1360 dc->maxunit = newsize;
1362 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1364 *unitp = unit;
1365 return (0);
1369 * @internal
1370 * @brief Add a device to a devclass
1372 * A unit number is allocated for the device (using the device's
1373 * preferred unit number if any) and the device is registered in the
1374 * devclass. This allows the device to be looked up by its unit
1375 * number, e.g. by decoding a dev_t minor number.
1377 * @param dc the devclass to add to
1378 * @param dev the device to add
1380 * @retval 0 success
1381 * @retval EEXIST the requested unit number is already allocated
1382 * @retval ENOMEM memory allocation failure
1384 static int
1385 devclass_add_device(devclass_t dc, device_t dev)
1387 int buflen, error;
1389 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1391 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit);
1392 if (buflen < 0)
1393 return (ENOMEM);
1394 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1395 if (!dev->nameunit)
1396 return (ENOMEM);
1398 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1399 free(dev->nameunit, M_BUS);
1400 dev->nameunit = NULL;
1401 return (error);
1403 dc->devices[dev->unit] = dev;
1404 dev->devclass = dc;
1405 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1407 return (0);
1411 * @internal
1412 * @brief Delete a device from a devclass
1414 * The device is removed from the devclass's device list and its unit
1415 * number is freed.
1417 * @param dc the devclass to delete from
1418 * @param dev the device to delete
1420 * @retval 0 success
1422 static int
1423 devclass_delete_device(devclass_t dc, device_t dev)
1425 if (!dc || !dev)
1426 return (0);
1428 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1430 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1431 panic("devclass_delete_device: inconsistent device class");
1432 dc->devices[dev->unit] = NULL;
1433 if (dev->flags & DF_WILDCARD)
1434 dev->unit = -1;
1435 dev->devclass = NULL;
1436 free(dev->nameunit, M_BUS);
1437 dev->nameunit = NULL;
1439 return (0);
1443 * @internal
1444 * @brief Make a new device and add it as a child of @p parent
1446 * @param parent the parent of the new device
1447 * @param name the devclass name of the new device or @c NULL
1448 * to leave the devclass unspecified
1449 * @parem unit the unit number of the new device of @c -1 to
1450 * leave the unit number unspecified
1452 * @returns the new device
1454 static device_t
1455 make_device(device_t parent, const char *name, int unit)
1457 device_t dev;
1458 devclass_t dc;
1460 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1462 if (name) {
1463 dc = devclass_find_internal(name, NULL, TRUE);
1464 if (!dc) {
1465 printf("make_device: can't find device class %s\n",
1466 name);
1467 return (NULL);
1469 } else {
1470 dc = NULL;
1473 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1474 if (!dev)
1475 return (NULL);
1477 dev->parent = parent;
1478 TAILQ_INIT(&dev->children);
1479 kobj_init((kobj_t) dev, &null_class);
1480 dev->driver = NULL;
1481 dev->devclass = NULL;
1482 dev->unit = unit;
1483 dev->nameunit = NULL;
1484 dev->desc = NULL;
1485 dev->busy = 0;
1486 dev->devflags = 0;
1487 dev->flags = DF_ENABLED;
1488 dev->order = 0;
1489 if (unit == -1)
1490 dev->flags |= DF_WILDCARD;
1491 if (name) {
1492 dev->flags |= DF_FIXEDCLASS;
1493 if (devclass_add_device(dc, dev)) {
1494 kobj_delete((kobj_t) dev, M_BUS);
1495 return (NULL);
1498 dev->ivars = NULL;
1499 dev->softc = NULL;
1501 dev->state = DS_NOTPRESENT;
1503 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1504 bus_data_generation_update();
1506 return (dev);
1510 * @internal
1511 * @brief Print a description of a device.
1513 static int
1514 device_print_child(device_t dev, device_t child)
1516 int retval = 0;
1518 if (device_is_alive(child))
1519 retval += BUS_PRINT_CHILD(dev, child);
1520 else
1521 retval += device_printf(child, " not found\n");
1523 return (retval);
1527 * @brief Create a new device
1529 * This creates a new device and adds it as a child of an existing
1530 * parent device. The new device will be added after the last existing
1531 * child with order zero.
1533 * @param dev the device which will be the parent of the
1534 * new child device
1535 * @param name devclass name for new device or @c NULL if not
1536 * specified
1537 * @param unit unit number for new device or @c -1 if not
1538 * specified
1540 * @returns the new device
1542 device_t
1543 device_add_child(device_t dev, const char *name, int unit)
1545 return (device_add_child_ordered(dev, 0, name, unit));
1549 * @brief Create a new device
1551 * This creates a new device and adds it as a child of an existing
1552 * parent device. The new device will be added after the last existing
1553 * child with the same order.
1555 * @param dev the device which will be the parent of the
1556 * new child device
1557 * @param order a value which is used to partially sort the
1558 * children of @p dev - devices created using
1559 * lower values of @p order appear first in @p
1560 * dev's list of children
1561 * @param name devclass name for new device or @c NULL if not
1562 * specified
1563 * @param unit unit number for new device or @c -1 if not
1564 * specified
1566 * @returns the new device
1568 device_t
1569 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1571 device_t child;
1572 device_t place;
1574 PDEBUG(("%s at %s with order %d as unit %d",
1575 name, DEVICENAME(dev), order, unit));
1577 child = make_device(dev, name, unit);
1578 if (child == NULL)
1579 return (child);
1580 child->order = order;
1582 TAILQ_FOREACH(place, &dev->children, link) {
1583 if (place->order > order)
1584 break;
1587 if (place) {
1589 * The device 'place' is the first device whose order is
1590 * greater than the new child.
1592 TAILQ_INSERT_BEFORE(place, child, link);
1593 } else {
1595 * The new child's order is greater or equal to the order of
1596 * any existing device. Add the child to the tail of the list.
1598 TAILQ_INSERT_TAIL(&dev->children, child, link);
1601 bus_data_generation_update();
1602 return (child);
1606 * @brief Delete a device
1608 * This function deletes a device along with all of its children. If
1609 * the device currently has a driver attached to it, the device is
1610 * detached first using device_detach().
1612 * @param dev the parent device
1613 * @param child the device to delete
1615 * @retval 0 success
1616 * @retval non-zero a unit error code describing the error
1619 device_delete_child(device_t dev, device_t child)
1621 int error;
1622 device_t grandchild;
1624 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1626 /* remove children first */
1627 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1628 error = device_delete_child(child, grandchild);
1629 if (error)
1630 return (error);
1633 if ((error = device_detach(child)) != 0)
1634 return (error);
1635 if (child->devclass)
1636 devclass_delete_device(child->devclass, child);
1637 TAILQ_REMOVE(&dev->children, child, link);
1638 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1639 kobj_delete((kobj_t) child, M_BUS);
1641 bus_data_generation_update();
1642 return (0);
1646 * @brief Find a device given a unit number
1648 * This is similar to devclass_get_devices() but only searches for
1649 * devices which have @p dev as a parent.
1651 * @param dev the parent device to search
1652 * @param unit the unit number to search for. If the unit is -1,
1653 * return the first child of @p dev which has name
1654 * @p classname (that is, the one with the lowest unit.)
1656 * @returns the device with the given unit number or @c
1657 * NULL if there is no such device
1659 device_t
1660 device_find_child(device_t dev, const char *classname, int unit)
1662 devclass_t dc;
1663 device_t child;
1665 dc = devclass_find(classname);
1666 if (!dc)
1667 return (NULL);
1669 if (unit != -1) {
1670 child = devclass_get_device(dc, unit);
1671 if (child && child->parent == dev)
1672 return (child);
1673 } else {
1674 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1675 child = devclass_get_device(dc, unit);
1676 if (child && child->parent == dev)
1677 return (child);
1680 return (NULL);
1684 * @internal
1686 static driverlink_t
1687 first_matching_driver(devclass_t dc, device_t dev)
1689 if (dev->devclass)
1690 return (devclass_find_driver_internal(dc, dev->devclass->name));
1691 return (TAILQ_FIRST(&dc->drivers));
1695 * @internal
1697 static driverlink_t
1698 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1700 if (dev->devclass) {
1701 driverlink_t dl;
1702 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1703 if (!strcmp(dev->devclass->name, dl->driver->name))
1704 return (dl);
1705 return (NULL);
1707 return (TAILQ_NEXT(last, link));
1711 * @internal
1714 device_probe_child(device_t dev, device_t child)
1716 devclass_t dc;
1717 driverlink_t best = NULL;
1718 driverlink_t dl;
1719 int result, pri = 0;
1720 int hasclass = (child->devclass != 0);
1722 GIANT_REQUIRED;
1724 dc = dev->devclass;
1725 if (!dc)
1726 panic("device_probe_child: parent device has no devclass");
1729 * If the state is already probed, then return. However, don't
1730 * return if we can rebid this object.
1732 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
1733 return (0);
1735 for (; dc; dc = dc->parent) {
1736 for (dl = first_matching_driver(dc, child);
1738 dl = next_matching_driver(dc, child, dl)) {
1739 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1740 device_set_driver(child, dl->driver);
1741 if (!hasclass)
1742 device_set_devclass(child, dl->driver->name);
1744 /* Fetch any flags for the device before probing. */
1745 resource_int_value(dl->driver->name, child->unit,
1746 "flags", &child->devflags);
1748 result = DEVICE_PROBE(child);
1750 /* Reset flags and devclass before the next probe. */
1751 child->devflags = 0;
1752 if (!hasclass)
1753 device_set_devclass(child, NULL);
1756 * If the driver returns SUCCESS, there can be
1757 * no higher match for this device.
1759 if (result == 0) {
1760 best = dl;
1761 pri = 0;
1762 break;
1766 * The driver returned an error so it
1767 * certainly doesn't match.
1769 if (result > 0) {
1770 device_set_driver(child, NULL);
1771 continue;
1775 * A priority lower than SUCCESS, remember the
1776 * best matching driver. Initialise the value
1777 * of pri for the first match.
1779 if (best == NULL || result > pri) {
1781 * Probes that return BUS_PROBE_NOWILDCARD
1782 * or lower only match when they are set
1783 * in stone by the parent bus.
1785 if (result <= BUS_PROBE_NOWILDCARD &&
1786 child->flags & DF_WILDCARD)
1787 continue;
1788 best = dl;
1789 pri = result;
1790 continue;
1794 * If we have an unambiguous match in this devclass,
1795 * don't look in the parent.
1797 if (best && pri == 0)
1798 break;
1802 * If we found a driver, change state and initialise the devclass.
1804 /* XXX What happens if we rebid and got no best? */
1805 if (best) {
1807 * If this device was atached, and we were asked to
1808 * rescan, and it is a different driver, then we have
1809 * to detach the old driver and reattach this new one.
1810 * Note, we don't have to check for DF_REBID here
1811 * because if the state is > DS_ALIVE, we know it must
1812 * be.
1814 * This assumes that all DF_REBID drivers can have
1815 * their probe routine called at any time and that
1816 * they are idempotent as well as completely benign in
1817 * normal operations.
1819 * We also have to make sure that the detach
1820 * succeeded, otherwise we fail the operation (or
1821 * maybe it should just fail silently? I'm torn).
1823 if (child->state > DS_ALIVE && best->driver != child->driver)
1824 if ((result = device_detach(dev)) != 0)
1825 return (result);
1827 /* Set the winning driver, devclass, and flags. */
1828 if (!child->devclass)
1829 device_set_devclass(child, best->driver->name);
1830 device_set_driver(child, best->driver);
1831 resource_int_value(best->driver->name, child->unit,
1832 "flags", &child->devflags);
1834 if (pri < 0) {
1836 * A bit bogus. Call the probe method again to make
1837 * sure that we have the right description.
1839 DEVICE_PROBE(child);
1840 #if 0
1841 child->flags |= DF_REBID;
1842 #endif
1843 } else
1844 child->flags &= ~DF_REBID;
1845 child->state = DS_ALIVE;
1847 bus_data_generation_update();
1848 return (0);
1851 return (ENXIO);
1855 * @brief Return the parent of a device
1857 device_t
1858 device_get_parent(device_t dev)
1860 return (dev->parent);
1864 * @brief Get a list of children of a device
1866 * An array containing a list of all the children of the given device
1867 * is allocated and returned in @p *devlistp. The number of devices
1868 * in the array is returned in @p *devcountp. The caller should free
1869 * the array using @c free(p, M_TEMP).
1871 * @param dev the device to examine
1872 * @param devlistp points at location for array pointer return
1873 * value
1874 * @param devcountp points at location for array size return value
1876 * @retval 0 success
1877 * @retval ENOMEM the array allocation failed
1880 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1882 int count;
1883 device_t child;
1884 device_t *list;
1886 count = 0;
1887 TAILQ_FOREACH(child, &dev->children, link) {
1888 count++;
1891 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1892 if (!list)
1893 return (ENOMEM);
1895 count = 0;
1896 TAILQ_FOREACH(child, &dev->children, link) {
1897 list[count] = child;
1898 count++;
1901 *devlistp = list;
1902 *devcountp = count;
1904 return (0);
1908 * @brief Return the current driver for the device or @c NULL if there
1909 * is no driver currently attached
1911 driver_t *
1912 device_get_driver(device_t dev)
1914 return (dev->driver);
1918 * @brief Return the current devclass for the device or @c NULL if
1919 * there is none.
1921 devclass_t
1922 device_get_devclass(device_t dev)
1924 return (dev->devclass);
1928 * @brief Return the name of the device's devclass or @c NULL if there
1929 * is none.
1931 const char *
1932 device_get_name(device_t dev)
1934 if (dev != NULL && dev->devclass)
1935 return (devclass_get_name(dev->devclass));
1936 return (NULL);
1940 * @brief Return a string containing the device's devclass name
1941 * followed by an ascii representation of the device's unit number
1942 * (e.g. @c "foo2").
1944 const char *
1945 device_get_nameunit(device_t dev)
1947 return (dev->nameunit);
1951 * @brief Return the device's unit number.
1954 device_get_unit(device_t dev)
1956 return (dev->unit);
1960 * @brief Return the device's description string
1962 const char *
1963 device_get_desc(device_t dev)
1965 return (dev->desc);
1969 * @brief Return the device's flags
1971 u_int32_t
1972 device_get_flags(device_t dev)
1974 return (dev->devflags);
1977 struct sysctl_ctx_list *
1978 device_get_sysctl_ctx(device_t dev)
1980 return (&dev->sysctl_ctx);
1983 struct sysctl_oid *
1984 device_get_sysctl_tree(device_t dev)
1986 return (dev->sysctl_tree);
1990 * @brief Print the name of the device followed by a colon and a space
1992 * @returns the number of characters printed
1995 device_print_prettyname(device_t dev)
1997 const char *name = device_get_name(dev);
1999 if (name == 0)
2000 return (printf("unknown: "));
2001 return (printf("%s%d: ", name, device_get_unit(dev)));
2005 * @brief Print the name of the device followed by a colon, a space
2006 * and the result of calling vprintf() with the value of @p fmt and
2007 * the following arguments.
2009 * @returns the number of characters printed
2012 device_printf(device_t dev, const char * fmt, ...)
2014 va_list ap;
2015 int retval;
2017 retval = device_print_prettyname(dev);
2018 va_start(ap, fmt);
2019 retval += vprintf(fmt, ap);
2020 va_end(ap);
2021 return (retval);
2025 * @internal
2027 static void
2028 device_set_desc_internal(device_t dev, const char* desc, int copy)
2030 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2031 free(dev->desc, M_BUS);
2032 dev->flags &= ~DF_DESCMALLOCED;
2033 dev->desc = NULL;
2036 if (copy && desc) {
2037 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2038 if (dev->desc) {
2039 strcpy(dev->desc, desc);
2040 dev->flags |= DF_DESCMALLOCED;
2042 } else {
2043 /* Avoid a -Wcast-qual warning */
2044 dev->desc = (char *)(uintptr_t) desc;
2047 bus_data_generation_update();
2051 * @brief Set the device's description
2053 * The value of @c desc should be a string constant that will not
2054 * change (at least until the description is changed in a subsequent
2055 * call to device_set_desc() or device_set_desc_copy()).
2057 void
2058 device_set_desc(device_t dev, const char* desc)
2060 device_set_desc_internal(dev, desc, FALSE);
2064 * @brief Set the device's description
2066 * The string pointed to by @c desc is copied. Use this function if
2067 * the device description is generated, (e.g. with sprintf()).
2069 void
2070 device_set_desc_copy(device_t dev, const char* desc)
2072 device_set_desc_internal(dev, desc, TRUE);
2076 * @brief Set the device's flags
2078 void
2079 device_set_flags(device_t dev, u_int32_t flags)
2081 dev->devflags = flags;
2085 * @brief Return the device's softc field
2087 * The softc is allocated and zeroed when a driver is attached, based
2088 * on the size field of the driver.
2090 void *
2091 device_get_softc(device_t dev)
2093 return (dev->softc);
2097 * @brief Set the device's softc field
2099 * Most drivers do not need to use this since the softc is allocated
2100 * automatically when the driver is attached.
2102 void
2103 device_set_softc(device_t dev, void *softc)
2105 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2106 free(dev->softc, M_BUS_SC);
2107 dev->softc = softc;
2108 if (dev->softc)
2109 dev->flags |= DF_EXTERNALSOFTC;
2110 else
2111 dev->flags &= ~DF_EXTERNALSOFTC;
2115 * @brief Get the device's ivars field
2117 * The ivars field is used by the parent device to store per-device
2118 * state (e.g. the physical location of the device or a list of
2119 * resources).
2121 void *
2122 device_get_ivars(device_t dev)
2125 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2126 return (dev->ivars);
2130 * @brief Set the device's ivars field
2132 void
2133 device_set_ivars(device_t dev, void * ivars)
2136 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2137 dev->ivars = ivars;
2141 * @brief Return the device's state
2143 device_state_t
2144 device_get_state(device_t dev)
2146 return (dev->state);
2150 * @brief Set the DF_ENABLED flag for the device
2152 void
2153 device_enable(device_t dev)
2155 dev->flags |= DF_ENABLED;
2159 * @brief Clear the DF_ENABLED flag for the device
2161 void
2162 device_disable(device_t dev)
2164 dev->flags &= ~DF_ENABLED;
2168 * @brief Increment the busy counter for the device
2170 void
2171 device_busy(device_t dev)
2173 if (dev->state < DS_ATTACHED)
2174 panic("device_busy: called for unattached device");
2175 if (dev->busy == 0 && dev->parent)
2176 device_busy(dev->parent);
2177 dev->busy++;
2178 dev->state = DS_BUSY;
2182 * @brief Decrement the busy counter for the device
2184 void
2185 device_unbusy(device_t dev)
2187 if (dev->state != DS_BUSY)
2188 panic("device_unbusy: called for non-busy device %s",
2189 device_get_nameunit(dev));
2190 dev->busy--;
2191 if (dev->busy == 0) {
2192 if (dev->parent)
2193 device_unbusy(dev->parent);
2194 dev->state = DS_ATTACHED;
2199 * @brief Set the DF_QUIET flag for the device
2201 void
2202 device_quiet(device_t dev)
2204 dev->flags |= DF_QUIET;
2208 * @brief Clear the DF_QUIET flag for the device
2210 void
2211 device_verbose(device_t dev)
2213 dev->flags &= ~DF_QUIET;
2217 * @brief Return non-zero if the DF_QUIET flag is set on the device
2220 device_is_quiet(device_t dev)
2222 return ((dev->flags & DF_QUIET) != 0);
2226 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2229 device_is_enabled(device_t dev)
2231 return ((dev->flags & DF_ENABLED) != 0);
2235 * @brief Return non-zero if the device was successfully probed
2238 device_is_alive(device_t dev)
2240 return (dev->state >= DS_ALIVE);
2244 * @brief Return non-zero if the device currently has a driver
2245 * attached to it
2248 device_is_attached(device_t dev)
2250 return (dev->state >= DS_ATTACHED);
2254 * @brief Set the devclass of a device
2255 * @see devclass_add_device().
2258 device_set_devclass(device_t dev, const char *classname)
2260 devclass_t dc;
2261 int error;
2263 if (!classname) {
2264 if (dev->devclass)
2265 devclass_delete_device(dev->devclass, dev);
2266 return (0);
2269 if (dev->devclass) {
2270 printf("device_set_devclass: device class already set\n");
2271 return (EINVAL);
2274 dc = devclass_find_internal(classname, NULL, TRUE);
2275 if (!dc)
2276 return (ENOMEM);
2278 error = devclass_add_device(dc, dev);
2280 bus_data_generation_update();
2281 return (error);
2285 * @brief Set the driver of a device
2287 * @retval 0 success
2288 * @retval EBUSY the device already has a driver attached
2289 * @retval ENOMEM a memory allocation failure occurred
2292 device_set_driver(device_t dev, driver_t *driver)
2294 if (dev->state >= DS_ATTACHED)
2295 return (EBUSY);
2297 if (dev->driver == driver)
2298 return (0);
2300 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2301 free(dev->softc, M_BUS_SC);
2302 dev->softc = NULL;
2304 kobj_delete((kobj_t) dev, NULL);
2305 dev->driver = driver;
2306 if (driver) {
2307 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2308 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2309 dev->softc = malloc(driver->size, M_BUS_SC,
2310 M_NOWAIT | M_ZERO);
2311 if (!dev->softc) {
2312 kobj_delete((kobj_t) dev, NULL);
2313 kobj_init((kobj_t) dev, &null_class);
2314 dev->driver = NULL;
2315 return (ENOMEM);
2318 } else {
2319 kobj_init((kobj_t) dev, &null_class);
2322 bus_data_generation_update();
2323 return (0);
2327 * @brief Probe a device, and return this status.
2329 * This function is the core of the device autoconfiguration
2330 * system. Its purpose is to select a suitable driver for a device and
2331 * then call that driver to initialise the hardware appropriately. The
2332 * driver is selected by calling the DEVICE_PROBE() method of a set of
2333 * candidate drivers and then choosing the driver which returned the
2334 * best value. This driver is then attached to the device using
2335 * device_attach().
2337 * The set of suitable drivers is taken from the list of drivers in
2338 * the parent device's devclass. If the device was originally created
2339 * with a specific class name (see device_add_child()), only drivers
2340 * with that name are probed, otherwise all drivers in the devclass
2341 * are probed. If no drivers return successful probe values in the
2342 * parent devclass, the search continues in the parent of that
2343 * devclass (see devclass_get_parent()) if any.
2345 * @param dev the device to initialise
2347 * @retval 0 success
2348 * @retval ENXIO no driver was found
2349 * @retval ENOMEM memory allocation failure
2350 * @retval non-zero some other unix error code
2351 * @retval -1 Device already attached
2354 device_probe(device_t dev)
2356 int error;
2358 GIANT_REQUIRED;
2360 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2361 return (-1);
2363 if (!(dev->flags & DF_ENABLED)) {
2364 if (bootverbose && device_get_name(dev) != NULL) {
2365 device_print_prettyname(dev);
2366 printf("not probed (disabled)\n");
2368 return (-1);
2370 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2371 if (!(dev->flags & DF_DONENOMATCH)) {
2372 BUS_PROBE_NOMATCH(dev->parent, dev);
2373 devnomatch(dev);
2374 dev->flags |= DF_DONENOMATCH;
2376 return (error);
2378 return (0);
2382 * @brief Probe a device and attach a driver if possible
2384 * calls device_probe() and attaches if that was successful.
2387 device_probe_and_attach(device_t dev)
2389 int error;
2391 GIANT_REQUIRED;
2393 error = device_probe(dev);
2394 if (error == -1)
2395 return (0);
2396 else if (error != 0)
2397 return (error);
2398 return (device_attach(dev));
2402 * @brief Attach a device driver to a device
2404 * This function is a wrapper around the DEVICE_ATTACH() driver
2405 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2406 * device's sysctl tree, optionally prints a description of the device
2407 * and queues a notification event for user-based device management
2408 * services.
2410 * Normally this function is only called internally from
2411 * device_probe_and_attach().
2413 * @param dev the device to initialise
2415 * @retval 0 success
2416 * @retval ENXIO no driver was found
2417 * @retval ENOMEM memory allocation failure
2418 * @retval non-zero some other unix error code
2421 device_attach(device_t dev)
2423 int error;
2425 device_sysctl_init(dev);
2426 if (!device_is_quiet(dev))
2427 device_print_child(dev->parent, dev);
2428 if ((error = DEVICE_ATTACH(dev)) != 0) {
2429 printf("device_attach: %s%d attach returned %d\n",
2430 dev->driver->name, dev->unit, error);
2431 /* Unset the class; set in device_probe_child */
2432 if (dev->devclass == NULL)
2433 device_set_devclass(dev, NULL);
2434 device_set_driver(dev, NULL);
2435 device_sysctl_fini(dev);
2436 dev->state = DS_NOTPRESENT;
2437 return (error);
2439 device_sysctl_update(dev);
2440 dev->state = DS_ATTACHED;
2441 devadded(dev);
2442 return (0);
2446 * @brief Detach a driver from a device
2448 * This function is a wrapper around the DEVICE_DETACH() driver
2449 * method. If the call to DEVICE_DETACH() succeeds, it calls
2450 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2451 * notification event for user-based device management services and
2452 * cleans up the device's sysctl tree.
2454 * @param dev the device to un-initialise
2456 * @retval 0 success
2457 * @retval ENXIO no driver was found
2458 * @retval ENOMEM memory allocation failure
2459 * @retval non-zero some other unix error code
2462 device_detach(device_t dev)
2464 int error;
2466 GIANT_REQUIRED;
2468 PDEBUG(("%s", DEVICENAME(dev)));
2469 if (dev->state == DS_BUSY)
2470 return (EBUSY);
2471 if (dev->state != DS_ATTACHED)
2472 return (0);
2474 if ((error = DEVICE_DETACH(dev)) != 0)
2475 return (error);
2476 devremoved(dev);
2477 device_printf(dev, "detached\n");
2478 if (dev->parent)
2479 BUS_CHILD_DETACHED(dev->parent, dev);
2481 if (!(dev->flags & DF_FIXEDCLASS))
2482 devclass_delete_device(dev->devclass, dev);
2484 dev->state = DS_NOTPRESENT;
2485 device_set_driver(dev, NULL);
2486 device_set_desc(dev, NULL);
2487 device_sysctl_fini(dev);
2489 return (0);
2493 * @brief Tells a driver to quiesce itself.
2495 * This function is a wrapper around the DEVICE_QUIESCE() driver
2496 * method. If the call to DEVICE_QUIESCE() succeeds.
2498 * @param dev the device to quiesce
2500 * @retval 0 success
2501 * @retval ENXIO no driver was found
2502 * @retval ENOMEM memory allocation failure
2503 * @retval non-zero some other unix error code
2506 device_quiesce(device_t dev)
2509 PDEBUG(("%s", DEVICENAME(dev)));
2510 if (dev->state == DS_BUSY)
2511 return (EBUSY);
2512 if (dev->state != DS_ATTACHED)
2513 return (0);
2515 return (DEVICE_QUIESCE(dev));
2519 * @brief Notify a device of system shutdown
2521 * This function calls the DEVICE_SHUTDOWN() driver method if the
2522 * device currently has an attached driver.
2524 * @returns the value returned by DEVICE_SHUTDOWN()
2527 device_shutdown(device_t dev)
2529 if (dev->state < DS_ATTACHED)
2530 return (0);
2531 return (DEVICE_SHUTDOWN(dev));
2535 * @brief Set the unit number of a device
2537 * This function can be used to override the unit number used for a
2538 * device (e.g. to wire a device to a pre-configured unit number).
2541 device_set_unit(device_t dev, int unit)
2543 devclass_t dc;
2544 int err;
2546 dc = device_get_devclass(dev);
2547 if (unit < dc->maxunit && dc->devices[unit])
2548 return (EBUSY);
2549 err = devclass_delete_device(dc, dev);
2550 if (err)
2551 return (err);
2552 dev->unit = unit;
2553 err = devclass_add_device(dc, dev);
2554 if (err)
2555 return (err);
2557 bus_data_generation_update();
2558 return (0);
2561 /*======================================*/
2563 * Some useful method implementations to make life easier for bus drivers.
2567 * @brief Initialise a resource list.
2569 * @param rl the resource list to initialise
2571 void
2572 resource_list_init(struct resource_list *rl)
2574 STAILQ_INIT(rl);
2578 * @brief Reclaim memory used by a resource list.
2580 * This function frees the memory for all resource entries on the list
2581 * (if any).
2583 * @param rl the resource list to free
2585 void
2586 resource_list_free(struct resource_list *rl)
2588 struct resource_list_entry *rle;
2590 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2591 if (rle->res)
2592 panic("resource_list_free: resource entry is busy");
2593 STAILQ_REMOVE_HEAD(rl, link);
2594 free(rle, M_BUS);
2599 * @brief Add a resource entry.
2601 * This function adds a resource entry using the given @p type, @p
2602 * start, @p end and @p count values. A rid value is chosen by
2603 * searching sequentially for the first unused rid starting at zero.
2605 * @param rl the resource list to edit
2606 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2607 * @param start the start address of the resource
2608 * @param end the end address of the resource
2609 * @param count XXX end-start+1
2612 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2613 u_long end, u_long count)
2615 int rid;
2617 rid = 0;
2618 while (resource_list_find(rl, type, rid) != NULL)
2619 rid++;
2620 resource_list_add(rl, type, rid, start, end, count);
2621 return (rid);
2625 * @brief Add or modify a resource entry.
2627 * If an existing entry exists with the same type and rid, it will be
2628 * modified using the given values of @p start, @p end and @p
2629 * count. If no entry exists, a new one will be created using the
2630 * given values. The resource list entry that matches is then returned.
2632 * @param rl the resource list to edit
2633 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2634 * @param rid the resource identifier
2635 * @param start the start address of the resource
2636 * @param end the end address of the resource
2637 * @param count XXX end-start+1
2639 struct resource_list_entry *
2640 resource_list_add(struct resource_list *rl, int type, int rid,
2641 u_long start, u_long end, u_long count)
2643 struct resource_list_entry *rle;
2645 rle = resource_list_find(rl, type, rid);
2646 if (!rle) {
2647 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2648 M_NOWAIT);
2649 if (!rle)
2650 panic("resource_list_add: can't record entry");
2651 STAILQ_INSERT_TAIL(rl, rle, link);
2652 rle->type = type;
2653 rle->rid = rid;
2654 rle->res = NULL;
2657 if (rle->res)
2658 panic("resource_list_add: resource entry is busy");
2660 rle->start = start;
2661 rle->end = end;
2662 rle->count = count;
2663 return (rle);
2667 * @brief Find a resource entry by type and rid.
2669 * @param rl the resource list to search
2670 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2671 * @param rid the resource identifier
2673 * @returns the resource entry pointer or NULL if there is no such
2674 * entry.
2676 struct resource_list_entry *
2677 resource_list_find(struct resource_list *rl, int type, int rid)
2679 struct resource_list_entry *rle;
2681 STAILQ_FOREACH(rle, rl, link) {
2682 if (rle->type == type && rle->rid == rid)
2683 return (rle);
2685 return (NULL);
2689 * @brief Delete a resource entry.
2691 * @param rl the resource list to edit
2692 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2693 * @param rid the resource identifier
2695 void
2696 resource_list_delete(struct resource_list *rl, int type, int rid)
2698 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2700 if (rle) {
2701 if (rle->res != NULL)
2702 panic("resource_list_delete: resource has not been released");
2703 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
2704 free(rle, M_BUS);
2709 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
2711 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
2712 * and passing the allocation up to the parent of @p bus. This assumes
2713 * that the first entry of @c device_get_ivars(child) is a struct
2714 * resource_list. This also handles 'passthrough' allocations where a
2715 * child is a remote descendant of bus by passing the allocation up to
2716 * the parent of bus.
2718 * Typically, a bus driver would store a list of child resources
2719 * somewhere in the child device's ivars (see device_get_ivars()) and
2720 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
2721 * then call resource_list_alloc() to perform the allocation.
2723 * @param rl the resource list to allocate from
2724 * @param bus the parent device of @p child
2725 * @param child the device which is requesting an allocation
2726 * @param type the type of resource to allocate
2727 * @param rid a pointer to the resource identifier
2728 * @param start hint at the start of the resource range - pass
2729 * @c 0UL for any start address
2730 * @param end hint at the end of the resource range - pass
2731 * @c ~0UL for any end address
2732 * @param count hint at the size of range required - pass @c 1
2733 * for any size
2734 * @param flags any extra flags to control the resource
2735 * allocation - see @c RF_XXX flags in
2736 * <sys/rman.h> for details
2738 * @returns the resource which was allocated or @c NULL if no
2739 * resource could be allocated
2741 struct resource *
2742 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
2743 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
2745 struct resource_list_entry *rle = NULL;
2746 int passthrough = (device_get_parent(child) != bus);
2747 int isdefault = (start == 0UL && end == ~0UL);
2749 if (passthrough) {
2750 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2751 type, rid, start, end, count, flags));
2754 rle = resource_list_find(rl, type, *rid);
2756 if (!rle)
2757 return (NULL); /* no resource of that type/rid */
2759 if (rle->res)
2760 panic("resource_list_alloc: resource entry is busy");
2762 if (isdefault) {
2763 start = rle->start;
2764 count = ulmax(count, rle->count);
2765 end = ulmax(rle->end, start + count - 1);
2768 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2769 type, rid, start, end, count, flags);
2772 * Record the new range.
2774 if (rle->res) {
2775 rle->start = rman_get_start(rle->res);
2776 rle->end = rman_get_end(rle->res);
2777 rle->count = count;
2780 return (rle->res);
2784 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
2786 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
2787 * used with resource_list_alloc().
2789 * @param rl the resource list which was allocated from
2790 * @param bus the parent device of @p child
2791 * @param child the device which is requesting a release
2792 * @param type the type of resource to allocate
2793 * @param rid the resource identifier
2794 * @param res the resource to release
2796 * @retval 0 success
2797 * @retval non-zero a standard unix error code indicating what
2798 * error condition prevented the operation
2801 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
2802 int type, int rid, struct resource *res)
2804 struct resource_list_entry *rle = NULL;
2805 int passthrough = (device_get_parent(child) != bus);
2806 int error;
2808 if (passthrough) {
2809 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2810 type, rid, res));
2813 rle = resource_list_find(rl, type, rid);
2815 if (!rle)
2816 panic("resource_list_release: can't find resource");
2817 if (!rle->res)
2818 panic("resource_list_release: resource entry is not busy");
2820 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2821 type, rid, res);
2822 if (error)
2823 return (error);
2825 rle->res = NULL;
2826 return (0);
2830 * @brief Print a description of resources in a resource list
2832 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
2833 * The name is printed if at least one resource of the given type is available.
2834 * The format is used to print resource start and end.
2836 * @param rl the resource list to print
2837 * @param name the name of @p type, e.g. @c "memory"
2838 * @param type type type of resource entry to print
2839 * @param format printf(9) format string to print resource
2840 * start and end values
2842 * @returns the number of characters printed
2845 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2846 const char *format)
2848 struct resource_list_entry *rle;
2849 int printed, retval;
2851 printed = 0;
2852 retval = 0;
2853 /* Yes, this is kinda cheating */
2854 STAILQ_FOREACH(rle, rl, link) {
2855 if (rle->type == type) {
2856 if (printed == 0)
2857 retval += printf(" %s ", name);
2858 else
2859 retval += printf(",");
2860 printed++;
2861 retval += printf(format, rle->start);
2862 if (rle->count > 1) {
2863 retval += printf("-");
2864 retval += printf(format, rle->start +
2865 rle->count - 1);
2869 return (retval);
2873 * @brief Releases all the resources in a list.
2875 * @param rl The resource list to purge.
2877 * @returns nothing
2879 void
2880 resource_list_purge(struct resource_list *rl)
2882 struct resource_list_entry *rle;
2884 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2885 if (rle->res)
2886 bus_release_resource(rman_get_device(rle->res),
2887 rle->type, rle->rid, rle->res);
2888 STAILQ_REMOVE_HEAD(rl, link);
2889 free(rle, M_BUS);
2893 device_t
2894 bus_generic_add_child(device_t dev, int order, const char *name, int unit)
2897 return (device_add_child_ordered(dev, order, name, unit));
2901 * @brief Helper function for implementing DEVICE_PROBE()
2903 * This function can be used to help implement the DEVICE_PROBE() for
2904 * a bus (i.e. a device which has other devices attached to it). It
2905 * calls the DEVICE_IDENTIFY() method of each driver in the device's
2906 * devclass.
2909 bus_generic_probe(device_t dev)
2911 devclass_t dc = dev->devclass;
2912 driverlink_t dl;
2914 TAILQ_FOREACH(dl, &dc->drivers, link) {
2915 DEVICE_IDENTIFY(dl->driver, dev);
2918 return (0);
2922 * @brief Helper function for implementing DEVICE_ATTACH()
2924 * This function can be used to help implement the DEVICE_ATTACH() for
2925 * a bus. It calls device_probe_and_attach() for each of the device's
2926 * children.
2929 bus_generic_attach(device_t dev)
2931 device_t child;
2933 TAILQ_FOREACH(child, &dev->children, link) {
2934 device_probe_and_attach(child);
2937 return (0);
2941 * @brief Helper function for implementing DEVICE_DETACH()
2943 * This function can be used to help implement the DEVICE_DETACH() for
2944 * a bus. It calls device_detach() for each of the device's
2945 * children.
2948 bus_generic_detach(device_t dev)
2950 device_t child;
2951 int error;
2953 if (dev->state != DS_ATTACHED)
2954 return (EBUSY);
2956 TAILQ_FOREACH(child, &dev->children, link) {
2957 if ((error = device_detach(child)) != 0)
2958 return (error);
2961 return (0);
2965 * @brief Helper function for implementing DEVICE_SHUTDOWN()
2967 * This function can be used to help implement the DEVICE_SHUTDOWN()
2968 * for a bus. It calls device_shutdown() for each of the device's
2969 * children.
2972 bus_generic_shutdown(device_t dev)
2974 device_t child;
2976 TAILQ_FOREACH(child, &dev->children, link) {
2977 device_shutdown(child);
2980 return (0);
2984 * @brief Helper function for implementing DEVICE_SUSPEND()
2986 * This function can be used to help implement the DEVICE_SUSPEND()
2987 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
2988 * children. If any call to DEVICE_SUSPEND() fails, the suspend
2989 * operation is aborted and any devices which were suspended are
2990 * resumed immediately by calling their DEVICE_RESUME() methods.
2993 bus_generic_suspend(device_t dev)
2995 int error;
2996 device_t child, child2;
2998 TAILQ_FOREACH(child, &dev->children, link) {
2999 error = DEVICE_SUSPEND(child);
3000 if (error) {
3001 for (child2 = TAILQ_FIRST(&dev->children);
3002 child2 && child2 != child;
3003 child2 = TAILQ_NEXT(child2, link))
3004 DEVICE_RESUME(child2);
3005 return (error);
3008 return (0);
3012 * @brief Helper function for implementing DEVICE_RESUME()
3014 * This function can be used to help implement the DEVICE_RESUME() for
3015 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3018 bus_generic_resume(device_t dev)
3020 device_t child;
3022 TAILQ_FOREACH(child, &dev->children, link) {
3023 DEVICE_RESUME(child);
3024 /* if resume fails, there's nothing we can usefully do... */
3026 return (0);
3030 * @brief Helper function for implementing BUS_PRINT_CHILD().
3032 * This function prints the first part of the ascii representation of
3033 * @p child, including its name, unit and description (if any - see
3034 * device_set_desc()).
3036 * @returns the number of characters printed
3039 bus_print_child_header(device_t dev, device_t child)
3041 int retval = 0;
3043 if (device_get_desc(child)) {
3044 retval += device_printf(child, "<%s>", device_get_desc(child));
3045 } else {
3046 retval += printf("%s", device_get_nameunit(child));
3049 return (retval);
3053 * @brief Helper function for implementing BUS_PRINT_CHILD().
3055 * This function prints the last part of the ascii representation of
3056 * @p child, which consists of the string @c " on " followed by the
3057 * name and unit of the @p dev.
3059 * @returns the number of characters printed
3062 bus_print_child_footer(device_t dev, device_t child)
3064 return (printf(" on %s\n", device_get_nameunit(dev)));
3068 * @brief Helper function for implementing BUS_PRINT_CHILD().
3070 * This function simply calls bus_print_child_header() followed by
3071 * bus_print_child_footer().
3073 * @returns the number of characters printed
3076 bus_generic_print_child(device_t dev, device_t child)
3078 int retval = 0;
3080 retval += bus_print_child_header(dev, child);
3081 retval += bus_print_child_footer(dev, child);
3083 return (retval);
3087 * @brief Stub function for implementing BUS_READ_IVAR().
3089 * @returns ENOENT
3092 bus_generic_read_ivar(device_t dev, device_t child, int index,
3093 uintptr_t * result)
3095 return (ENOENT);
3099 * @brief Stub function for implementing BUS_WRITE_IVAR().
3101 * @returns ENOENT
3104 bus_generic_write_ivar(device_t dev, device_t child, int index,
3105 uintptr_t value)
3107 return (ENOENT);
3111 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3113 * @returns NULL
3115 struct resource_list *
3116 bus_generic_get_resource_list(device_t dev, device_t child)
3118 return (NULL);
3122 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3124 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3125 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3126 * and then calls device_probe_and_attach() for each unattached child.
3128 void
3129 bus_generic_driver_added(device_t dev, driver_t *driver)
3131 device_t child;
3133 DEVICE_IDENTIFY(driver, dev);
3134 TAILQ_FOREACH(child, &dev->children, link) {
3135 if (child->state == DS_NOTPRESENT ||
3136 (child->flags & DF_REBID))
3137 device_probe_and_attach(child);
3142 * @brief Helper function for implementing BUS_SETUP_INTR().
3144 * This simple implementation of BUS_SETUP_INTR() simply calls the
3145 * BUS_SETUP_INTR() method of the parent of @p dev.
3148 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3149 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3150 void **cookiep)
3152 /* Propagate up the bus hierarchy until someone handles it. */
3153 if (dev->parent)
3154 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3155 filter, intr, arg, cookiep));
3156 return (EINVAL);
3160 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3162 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3163 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3166 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3167 void *cookie)
3169 /* Propagate up the bus hierarchy until someone handles it. */
3170 if (dev->parent)
3171 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3172 return (EINVAL);
3176 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3178 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3179 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3181 struct resource *
3182 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3183 u_long start, u_long end, u_long count, u_int flags)
3185 /* Propagate up the bus hierarchy until someone handles it. */
3186 if (dev->parent)
3187 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3188 start, end, count, flags));
3189 return (NULL);
3193 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3195 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3196 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3199 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3200 struct resource *r)
3202 /* Propagate up the bus hierarchy until someone handles it. */
3203 if (dev->parent)
3204 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3205 r));
3206 return (EINVAL);
3210 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3212 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3213 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3216 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3217 struct resource *r)
3219 /* Propagate up the bus hierarchy until someone handles it. */
3220 if (dev->parent)
3221 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3222 r));
3223 return (EINVAL);
3227 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3229 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3230 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3233 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3234 int rid, struct resource *r)
3236 /* Propagate up the bus hierarchy until someone handles it. */
3237 if (dev->parent)
3238 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3239 r));
3240 return (EINVAL);
3244 * @brief Helper function for implementing BUS_BIND_INTR().
3246 * This simple implementation of BUS_BIND_INTR() simply calls the
3247 * BUS_BIND_INTR() method of the parent of @p dev.
3250 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3251 int cpu)
3254 /* Propagate up the bus hierarchy until someone handles it. */
3255 if (dev->parent)
3256 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3257 return (EINVAL);
3261 * @brief Helper function for implementing BUS_CONFIG_INTR().
3263 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3264 * BUS_CONFIG_INTR() method of the parent of @p dev.
3267 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3268 enum intr_polarity pol)
3271 /* Propagate up the bus hierarchy until someone handles it. */
3272 if (dev->parent)
3273 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3274 return (EINVAL);
3278 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3280 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3281 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3283 bus_dma_tag_t
3284 bus_generic_get_dma_tag(device_t dev, device_t child)
3287 /* Propagate up the bus hierarchy until someone handles it. */
3288 if (dev->parent != NULL)
3289 return (BUS_GET_DMA_TAG(dev->parent, child));
3290 return (NULL);
3294 * @brief Helper function for implementing BUS_GET_RESOURCE().
3296 * This implementation of BUS_GET_RESOURCE() uses the
3297 * resource_list_find() function to do most of the work. It calls
3298 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3299 * search.
3302 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3303 u_long *startp, u_long *countp)
3305 struct resource_list * rl = NULL;
3306 struct resource_list_entry * rle = NULL;
3308 rl = BUS_GET_RESOURCE_LIST(dev, child);
3309 if (!rl)
3310 return (EINVAL);
3312 rle = resource_list_find(rl, type, rid);
3313 if (!rle)
3314 return (ENOENT);
3316 if (startp)
3317 *startp = rle->start;
3318 if (countp)
3319 *countp = rle->count;
3321 return (0);
3325 * @brief Helper function for implementing BUS_SET_RESOURCE().
3327 * This implementation of BUS_SET_RESOURCE() uses the
3328 * resource_list_add() function to do most of the work. It calls
3329 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3330 * edit.
3333 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3334 u_long start, u_long count)
3336 struct resource_list * rl = NULL;
3338 rl = BUS_GET_RESOURCE_LIST(dev, child);
3339 if (!rl)
3340 return (EINVAL);
3342 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3344 return (0);
3348 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3350 * This implementation of BUS_DELETE_RESOURCE() uses the
3351 * resource_list_delete() function to do most of the work. It calls
3352 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3353 * edit.
3355 void
3356 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3358 struct resource_list * rl = NULL;
3360 rl = BUS_GET_RESOURCE_LIST(dev, child);
3361 if (!rl)
3362 return;
3364 resource_list_delete(rl, type, rid);
3366 return;
3370 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3372 * This implementation of BUS_RELEASE_RESOURCE() uses the
3373 * resource_list_release() function to do most of the work. It calls
3374 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3377 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3378 int rid, struct resource *r)
3380 struct resource_list * rl = NULL;
3382 rl = BUS_GET_RESOURCE_LIST(dev, child);
3383 if (!rl)
3384 return (EINVAL);
3386 return (resource_list_release(rl, dev, child, type, rid, r));
3390 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3392 * This implementation of BUS_ALLOC_RESOURCE() uses the
3393 * resource_list_alloc() function to do most of the work. It calls
3394 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3396 struct resource *
3397 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3398 int *rid, u_long start, u_long end, u_long count, u_int flags)
3400 struct resource_list * rl = NULL;
3402 rl = BUS_GET_RESOURCE_LIST(dev, child);
3403 if (!rl)
3404 return (NULL);
3406 return (resource_list_alloc(rl, dev, child, type, rid,
3407 start, end, count, flags));
3411 * @brief Helper function for implementing BUS_CHILD_PRESENT().
3413 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
3414 * BUS_CHILD_PRESENT() method of the parent of @p dev.
3417 bus_generic_child_present(device_t dev, device_t child)
3419 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
3423 * Some convenience functions to make it easier for drivers to use the
3424 * resource-management functions. All these really do is hide the
3425 * indirection through the parent's method table, making for slightly
3426 * less-wordy code. In the future, it might make sense for this code
3427 * to maintain some sort of a list of resources allocated by each device.
3431 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3432 struct resource **res)
3434 int i;
3436 for (i = 0; rs[i].type != -1; i++)
3437 res[i] = NULL;
3438 for (i = 0; rs[i].type != -1; i++) {
3439 res[i] = bus_alloc_resource_any(dev,
3440 rs[i].type, &rs[i].rid, rs[i].flags);
3441 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
3442 bus_release_resources(dev, rs, res);
3443 return (ENXIO);
3446 return (0);
3449 void
3450 bus_release_resources(device_t dev, const struct resource_spec *rs,
3451 struct resource **res)
3453 int i;
3455 for (i = 0; rs[i].type != -1; i++)
3456 if (res[i] != NULL) {
3457 bus_release_resource(
3458 dev, rs[i].type, rs[i].rid, res[i]);
3459 res[i] = NULL;
3464 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
3466 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
3467 * parent of @p dev.
3469 struct resource *
3470 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3471 u_long count, u_int flags)
3473 if (dev->parent == NULL)
3474 return (NULL);
3475 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3476 count, flags));
3480 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
3482 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
3483 * parent of @p dev.
3486 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3488 if (dev->parent == NULL)
3489 return (EINVAL);
3490 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3494 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
3496 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
3497 * parent of @p dev.
3500 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3502 if (dev->parent == NULL)
3503 return (EINVAL);
3504 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3508 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
3510 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
3511 * parent of @p dev.
3514 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3516 if (dev->parent == NULL)
3517 return (EINVAL);
3518 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3522 * @brief Wrapper function for BUS_SETUP_INTR().
3524 * This function simply calls the BUS_SETUP_INTR() method of the
3525 * parent of @p dev.
3528 bus_setup_intr(device_t dev, struct resource *r, int flags,
3529 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
3531 int error;
3533 if (dev->parent == NULL)
3534 return (EINVAL);
3535 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
3536 arg, cookiep);
3537 if (error != 0)
3538 return (error);
3539 if (handler != NULL && !(flags & INTR_MPSAFE))
3540 device_printf(dev, "[GIANT-LOCKED]\n");
3541 if (bootverbose && (flags & INTR_MPSAFE))
3542 device_printf(dev, "[MPSAFE]\n");
3543 if (filter != NULL) {
3544 if (handler == NULL)
3545 device_printf(dev, "[FILTER]\n");
3546 else
3547 device_printf(dev, "[FILTER+ITHREAD]\n");
3548 } else
3549 device_printf(dev, "[ITHREAD]\n");
3550 return (0);
3554 * @brief Wrapper function for BUS_TEARDOWN_INTR().
3556 * This function simply calls the BUS_TEARDOWN_INTR() method of the
3557 * parent of @p dev.
3560 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3562 if (dev->parent == NULL)
3563 return (EINVAL);
3564 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3568 * @brief Wrapper function for BUS_BIND_INTR().
3570 * This function simply calls the BUS_BIND_INTR() method of the
3571 * parent of @p dev.
3574 bus_bind_intr(device_t dev, struct resource *r, int cpu)
3576 if (dev->parent == NULL)
3577 return (EINVAL);
3578 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
3582 * @brief Wrapper function for BUS_SET_RESOURCE().
3584 * This function simply calls the BUS_SET_RESOURCE() method of the
3585 * parent of @p dev.
3588 bus_set_resource(device_t dev, int type, int rid,
3589 u_long start, u_long count)
3591 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3592 start, count));
3596 * @brief Wrapper function for BUS_GET_RESOURCE().
3598 * This function simply calls the BUS_GET_RESOURCE() method of the
3599 * parent of @p dev.
3602 bus_get_resource(device_t dev, int type, int rid,
3603 u_long *startp, u_long *countp)
3605 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3606 startp, countp));
3610 * @brief Wrapper function for BUS_GET_RESOURCE().
3612 * This function simply calls the BUS_GET_RESOURCE() method of the
3613 * parent of @p dev and returns the start value.
3615 u_long
3616 bus_get_resource_start(device_t dev, int type, int rid)
3618 u_long start, count;
3619 int error;
3621 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3622 &start, &count);
3623 if (error)
3624 return (0);
3625 return (start);
3629 * @brief Wrapper function for BUS_GET_RESOURCE().
3631 * This function simply calls the BUS_GET_RESOURCE() method of the
3632 * parent of @p dev and returns the count value.
3634 u_long
3635 bus_get_resource_count(device_t dev, int type, int rid)
3637 u_long start, count;
3638 int error;
3640 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3641 &start, &count);
3642 if (error)
3643 return (0);
3644 return (count);
3648 * @brief Wrapper function for BUS_DELETE_RESOURCE().
3650 * This function simply calls the BUS_DELETE_RESOURCE() method of the
3651 * parent of @p dev.
3653 void
3654 bus_delete_resource(device_t dev, int type, int rid)
3656 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3660 * @brief Wrapper function for BUS_CHILD_PRESENT().
3662 * This function simply calls the BUS_CHILD_PRESENT() method of the
3663 * parent of @p dev.
3666 bus_child_present(device_t child)
3668 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3672 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
3674 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
3675 * parent of @p dev.
3678 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3680 device_t parent;
3682 parent = device_get_parent(child);
3683 if (parent == NULL) {
3684 *buf = '\0';
3685 return (0);
3687 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3691 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
3693 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
3694 * parent of @p dev.
3697 bus_child_location_str(device_t child, char *buf, size_t buflen)
3699 device_t parent;
3701 parent = device_get_parent(child);
3702 if (parent == NULL) {
3703 *buf = '\0';
3704 return (0);
3706 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3710 * @brief Wrapper function for BUS_GET_DMA_TAG().
3712 * This function simply calls the BUS_GET_DMA_TAG() method of the
3713 * parent of @p dev.
3715 bus_dma_tag_t
3716 bus_get_dma_tag(device_t dev)
3718 device_t parent;
3720 parent = device_get_parent(dev);
3721 if (parent == NULL)
3722 return (NULL);
3723 return (BUS_GET_DMA_TAG(parent, dev));
3726 /* Resume all devices and then notify userland that we're up again. */
3727 static int
3728 root_resume(device_t dev)
3730 int error;
3732 error = bus_generic_resume(dev);
3733 if (error == 0)
3734 devctl_notify("kern", "power", "resume", NULL);
3735 return (error);
3738 static int
3739 root_print_child(device_t dev, device_t child)
3741 int retval = 0;
3743 retval += bus_print_child_header(dev, child);
3744 retval += printf("\n");
3746 return (retval);
3749 static int
3750 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3751 void **cookiep)
3754 * If an interrupt mapping gets to here something bad has happened.
3756 panic("root_setup_intr");
3760 * If we get here, assume that the device is permanant and really is
3761 * present in the system. Removable bus drivers are expected to intercept
3762 * this call long before it gets here. We return -1 so that drivers that
3763 * really care can check vs -1 or some ERRNO returned higher in the food
3764 * chain.
3766 static int
3767 root_child_present(device_t dev, device_t child)
3769 return (-1);
3772 static kobj_method_t root_methods[] = {
3773 /* Device interface */
3774 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3775 KOBJMETHOD(device_suspend, bus_generic_suspend),
3776 KOBJMETHOD(device_resume, root_resume),
3778 /* Bus interface */
3779 KOBJMETHOD(bus_print_child, root_print_child),
3780 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3781 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3782 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3783 KOBJMETHOD(bus_child_present, root_child_present),
3785 { 0, 0 }
3788 static driver_t root_driver = {
3789 "root",
3790 root_methods,
3791 1, /* no softc */
3794 device_t root_bus;
3795 devclass_t root_devclass;
3797 static int
3798 root_bus_module_handler(module_t mod, int what, void* arg)
3800 switch (what) {
3801 case MOD_LOAD:
3802 TAILQ_INIT(&bus_data_devices);
3803 kobj_class_compile((kobj_class_t) &root_driver);
3804 root_bus = make_device(NULL, "root", 0);
3805 root_bus->desc = "System root bus";
3806 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3807 root_bus->driver = &root_driver;
3808 root_bus->state = DS_ATTACHED;
3809 root_devclass = devclass_find_internal("root", NULL, FALSE);
3810 devinit();
3811 return (0);
3813 case MOD_SHUTDOWN:
3814 device_shutdown(root_bus);
3815 return (0);
3816 default:
3817 return (EOPNOTSUPP);
3820 return (0);
3823 static moduledata_t root_bus_mod = {
3824 "rootbus",
3825 root_bus_module_handler,
3828 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3831 * @brief Automatically configure devices
3833 * This function begins the autoconfiguration process by calling
3834 * device_probe_and_attach() for each child of the @c root0 device.
3836 void
3837 root_bus_configure(void)
3839 device_t dev;
3841 PDEBUG(("."));
3843 TAILQ_FOREACH(dev, &root_bus->children, link) {
3844 device_probe_and_attach(dev);
3849 * @brief Module handler for registering device drivers
3851 * This module handler is used to automatically register device
3852 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
3853 * devclass_add_driver() for the driver described by the
3854 * driver_module_data structure pointed to by @p arg
3857 driver_module_handler(module_t mod, int what, void *arg)
3859 int error;
3860 struct driver_module_data *dmd;
3861 devclass_t bus_devclass;
3862 kobj_class_t driver;
3864 dmd = (struct driver_module_data *)arg;
3865 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3866 error = 0;
3868 switch (what) {
3869 case MOD_LOAD:
3870 if (dmd->dmd_chainevh)
3871 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3873 driver = dmd->dmd_driver;
3874 PDEBUG(("Loading module: driver %s on bus %s",
3875 DRIVERNAME(driver), dmd->dmd_busname));
3876 error = devclass_add_driver(bus_devclass, driver);
3877 if (error)
3878 break;
3881 * If the driver has any base classes, make the
3882 * devclass inherit from the devclass of the driver's
3883 * first base class. This will allow the system to
3884 * search for drivers in both devclasses for children
3885 * of a device using this driver.
3887 if (driver->baseclasses) {
3888 const char *parentname;
3889 parentname = driver->baseclasses[0]->name;
3890 *dmd->dmd_devclass =
3891 devclass_find_internal(driver->name,
3892 parentname, TRUE);
3893 } else {
3894 *dmd->dmd_devclass =
3895 devclass_find_internal(driver->name, NULL, TRUE);
3897 break;
3899 case MOD_UNLOAD:
3900 PDEBUG(("Unloading module: driver %s from bus %s",
3901 DRIVERNAME(dmd->dmd_driver),
3902 dmd->dmd_busname));
3903 error = devclass_delete_driver(bus_devclass,
3904 dmd->dmd_driver);
3906 if (!error && dmd->dmd_chainevh)
3907 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3908 break;
3909 case MOD_QUIESCE:
3910 PDEBUG(("Quiesce module: driver %s from bus %s",
3911 DRIVERNAME(dmd->dmd_driver),
3912 dmd->dmd_busname));
3913 error = devclass_quiesce_driver(bus_devclass,
3914 dmd->dmd_driver);
3916 if (!error && dmd->dmd_chainevh)
3917 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3918 break;
3919 default:
3920 error = EOPNOTSUPP;
3921 break;
3924 return (error);
3928 * @brief Enumerate all hinted devices for this bus.
3930 * Walks through the hints for this bus and calls the bus_hinted_child
3931 * routine for each one it fines. It searches first for the specific
3932 * bus that's being probed for hinted children (eg isa0), and then for
3933 * generic children (eg isa).
3935 * @param dev bus device to enumerate
3937 void
3938 bus_enumerate_hinted_children(device_t bus)
3940 int i;
3941 const char *dname, *busname;
3942 int dunit;
3945 * enumerate all devices on the specific bus
3947 busname = device_get_nameunit(bus);
3948 i = 0;
3949 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
3950 BUS_HINTED_CHILD(bus, dname, dunit);
3953 * and all the generic ones.
3955 busname = device_get_name(bus);
3956 i = 0;
3957 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
3958 BUS_HINTED_CHILD(bus, dname, dunit);
3961 #ifdef BUS_DEBUG
3963 /* the _short versions avoid iteration by not calling anything that prints
3964 * more than oneliners. I love oneliners.
3967 static void
3968 print_device_short(device_t dev, int indent)
3970 if (!dev)
3971 return;
3973 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3974 dev->unit, dev->desc,
3975 (dev->parent? "":"no "),
3976 (TAILQ_EMPTY(&dev->children)? "no ":""),
3977 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3978 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3979 (dev->flags&DF_WILDCARD? "wildcard,":""),
3980 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3981 (dev->flags&DF_REBID? "rebiddable,":""),
3982 (dev->ivars? "":"no "),
3983 (dev->softc? "":"no "),
3984 dev->busy));
3987 static void
3988 print_device(device_t dev, int indent)
3990 if (!dev)
3991 return;
3993 print_device_short(dev, indent);
3995 indentprintf(("Parent:\n"));
3996 print_device_short(dev->parent, indent+1);
3997 indentprintf(("Driver:\n"));
3998 print_driver_short(dev->driver, indent+1);
3999 indentprintf(("Devclass:\n"));
4000 print_devclass_short(dev->devclass, indent+1);
4003 void
4004 print_device_tree_short(device_t dev, int indent)
4005 /* print the device and all its children (indented) */
4007 device_t child;
4009 if (!dev)
4010 return;
4012 print_device_short(dev, indent);
4014 TAILQ_FOREACH(child, &dev->children, link) {
4015 print_device_tree_short(child, indent+1);
4019 void
4020 print_device_tree(device_t dev, int indent)
4021 /* print the device and all its children (indented) */
4023 device_t child;
4025 if (!dev)
4026 return;
4028 print_device(dev, indent);
4030 TAILQ_FOREACH(child, &dev->children, link) {
4031 print_device_tree(child, indent+1);
4035 static void
4036 print_driver_short(driver_t *driver, int indent)
4038 if (!driver)
4039 return;
4041 indentprintf(("driver %s: softc size = %zd\n",
4042 driver->name, driver->size));
4045 static void
4046 print_driver(driver_t *driver, int indent)
4048 if (!driver)
4049 return;
4051 print_driver_short(driver, indent);
4055 static void
4056 print_driver_list(driver_list_t drivers, int indent)
4058 driverlink_t driver;
4060 TAILQ_FOREACH(driver, &drivers, link) {
4061 print_driver(driver->driver, indent);
4065 static void
4066 print_devclass_short(devclass_t dc, int indent)
4068 if ( !dc )
4069 return;
4071 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4074 static void
4075 print_devclass(devclass_t dc, int indent)
4077 int i;
4079 if ( !dc )
4080 return;
4082 print_devclass_short(dc, indent);
4083 indentprintf(("Drivers:\n"));
4084 print_driver_list(dc->drivers, indent+1);
4086 indentprintf(("Devices:\n"));
4087 for (i = 0; i < dc->maxunit; i++)
4088 if (dc->devices[i])
4089 print_device(dc->devices[i], indent+1);
4092 void
4093 print_devclass_list_short(void)
4095 devclass_t dc;
4097 printf("Short listing of devclasses, drivers & devices:\n");
4098 TAILQ_FOREACH(dc, &devclasses, link) {
4099 print_devclass_short(dc, 0);
4103 void
4104 print_devclass_list(void)
4106 devclass_t dc;
4108 printf("Full listing of devclasses, drivers & devices:\n");
4109 TAILQ_FOREACH(dc, &devclasses, link) {
4110 print_devclass(dc, 0);
4114 #endif
4117 * User-space access to the device tree.
4119 * We implement a small set of nodes:
4121 * hw.bus Single integer read method to obtain the
4122 * current generation count.
4123 * hw.bus.devices Reads the entire device tree in flat space.
4124 * hw.bus.rman Resource manager interface
4126 * We might like to add the ability to scan devclasses and/or drivers to
4127 * determine what else is currently loaded/available.
4130 static int
4131 sysctl_bus(SYSCTL_HANDLER_ARGS)
4133 struct u_businfo ubus;
4135 ubus.ub_version = BUS_USER_VERSION;
4136 ubus.ub_generation = bus_data_generation;
4138 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4140 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4141 "bus-related data");
4143 static int
4144 sysctl_devices(SYSCTL_HANDLER_ARGS)
4146 int *name = (int *)arg1;
4147 u_int namelen = arg2;
4148 int index;
4149 struct device *dev;
4150 struct u_device udev; /* XXX this is a bit big */
4151 int error;
4153 if (namelen != 2)
4154 return (EINVAL);
4156 if (bus_data_generation_check(name[0]))
4157 return (EINVAL);
4159 index = name[1];
4162 * Scan the list of devices, looking for the requested index.
4164 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4165 if (index-- == 0)
4166 break;
4168 if (dev == NULL)
4169 return (ENOENT);
4172 * Populate the return array.
4174 bzero(&udev, sizeof(udev));
4175 udev.dv_handle = (uintptr_t)dev;
4176 udev.dv_parent = (uintptr_t)dev->parent;
4177 if (dev->nameunit != NULL)
4178 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4179 if (dev->desc != NULL)
4180 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4181 if (dev->driver != NULL && dev->driver->name != NULL)
4182 strlcpy(udev.dv_drivername, dev->driver->name,
4183 sizeof(udev.dv_drivername));
4184 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4185 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4186 udev.dv_devflags = dev->devflags;
4187 udev.dv_flags = dev->flags;
4188 udev.dv_state = dev->state;
4189 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4190 return (error);
4193 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4194 "system device tree");
4197 bus_data_generation_check(int generation)
4199 if (generation != bus_data_generation)
4200 return (1);
4202 /* XXX generate optimised lists here? */
4203 return (0);
4206 void
4207 bus_data_generation_update(void)
4209 bus_data_generation++;
4213 bus_free_resource(device_t dev, int type, struct resource *r)
4215 if (r == NULL)
4216 return (0);
4217 return (bus_release_resource(dev, type, rman_get_rid(r), r));