2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997,1998,2003 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/domainset.h>
38 #include <sys/eventhandler.h>
39 #include <sys/filio.h>
41 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/queue.h>
52 #include <machine/bus.h>
53 #include <sys/random.h>
56 #include <sys/selinfo.h>
57 #include <sys/signalvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
63 #include <sys/cpuset.h>
67 #include <machine/cpu.h>
68 #include <machine/stdarg.h>
75 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
77 SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
81 * Used to attach drivers to devclasses.
83 typedef struct driverlink *driverlink_t;
86 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
89 #define DL_DEFERRED_PROBE 1 /* Probe deferred on this */
90 TAILQ_ENTRY(driverlink) passlink;
94 * Forward declarations
96 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
97 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
98 typedef TAILQ_HEAD(device_list, device) device_list_t;
101 TAILQ_ENTRY(devclass) link;
102 devclass_t parent; /* parent in devclass hierarchy */
103 driver_list_t drivers; /* bus devclasses store drivers for bus */
105 device_t *devices; /* array of devices indexed by unit */
106 int maxunit; /* size of devices array */
108 #define DC_HAS_CHILDREN 1
110 struct sysctl_ctx_list sysctl_ctx;
111 struct sysctl_oid *sysctl_tree;
115 * @brief Implementation of device.
119 * A device is a kernel object. The first field must be the
120 * current ops table for the object.
127 TAILQ_ENTRY(device) link; /**< list of devices in parent */
128 TAILQ_ENTRY(device) devlink; /**< global device list membership */
129 device_t parent; /**< parent of this device */
130 device_list_t children; /**< list of child devices */
133 * Details of this device.
135 driver_t *driver; /**< current driver */
136 devclass_t devclass; /**< current device class */
137 int unit; /**< current unit number */
138 char* nameunit; /**< name+unit e.g. foodev0 */
139 char* desc; /**< driver specific description */
140 int busy; /**< count of calls to device_busy() */
141 device_state_t state; /**< current device state */
142 uint32_t devflags; /**< api level flags for device_get_flags() */
143 u_int flags; /**< internal device flags */
144 u_int order; /**< order from device_add_child_ordered() */
145 void *ivars; /**< instance variables */
146 void *softc; /**< current driver's variables */
148 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
149 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
152 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
153 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
155 EVENTHANDLER_LIST_DEFINE(device_attach);
156 EVENTHANDLER_LIST_DEFINE(device_detach);
157 EVENTHANDLER_LIST_DEFINE(dev_lookup);
159 static int bus_child_location_sb(device_t child, struct sbuf *sb);
160 static int bus_child_pnpinfo_sb(device_t child, struct sbuf *sb);
161 static void devctl2_init(void);
162 static bool device_frozen;
164 #define DRIVERNAME(d) ((d)? d->name : "no driver")
165 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
169 static int bus_debug = 1;
170 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
173 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
174 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
177 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
178 * prevent syslog from deleting initial spaces
180 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
182 static void print_device_short(device_t dev, int indent);
183 static void print_device(device_t dev, int indent);
184 void print_device_tree_short(device_t dev, int indent);
185 void print_device_tree(device_t dev, int indent);
186 static void print_driver_short(driver_t *driver, int indent);
187 static void print_driver(driver_t *driver, int indent);
188 static void print_driver_list(driver_list_t drivers, int indent);
189 static void print_devclass_short(devclass_t dc, int indent);
190 static void print_devclass(devclass_t dc, int indent);
191 void print_devclass_list_short(void);
192 void print_devclass_list(void);
195 /* Make the compiler ignore the function calls */
196 #define PDEBUG(a) /* nop */
197 #define DEVICENAME(d) /* nop */
199 #define print_device_short(d,i) /* nop */
200 #define print_device(d,i) /* nop */
201 #define print_device_tree_short(d,i) /* nop */
202 #define print_device_tree(d,i) /* nop */
203 #define print_driver_short(d,i) /* nop */
204 #define print_driver(d,i) /* nop */
205 #define print_driver_list(d,i) /* nop */
206 #define print_devclass_short(d,i) /* nop */
207 #define print_devclass(d,i) /* nop */
208 #define print_devclass_list_short() /* nop */
209 #define print_devclass_list() /* nop */
217 DEVCLASS_SYSCTL_PARENT,
221 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
223 devclass_t dc = (devclass_t)arg1;
227 case DEVCLASS_SYSCTL_PARENT:
228 value = dc->parent ? dc->parent->name : "";
233 return (SYSCTL_OUT_STR(req, value));
237 devclass_sysctl_init(devclass_t dc)
239 if (dc->sysctl_tree != NULL)
241 sysctl_ctx_init(&dc->sysctl_ctx);
242 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
243 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
244 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
245 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
247 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
248 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
254 DEVICE_SYSCTL_DRIVER,
255 DEVICE_SYSCTL_LOCATION,
256 DEVICE_SYSCTL_PNPINFO,
257 DEVICE_SYSCTL_PARENT,
261 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
263 device_t dev = (device_t)arg1;
270 case DEVICE_SYSCTL_DESC:
271 value = dev->desc ? dev->desc : "";
273 case DEVICE_SYSCTL_DRIVER:
274 value = dev->driver ? dev->driver->name : "";
276 case DEVICE_SYSCTL_LOCATION:
277 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
278 bus_child_location_str(dev, buf, 1024);
280 case DEVICE_SYSCTL_PNPINFO:
281 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
282 bus_child_pnpinfo_str(dev, buf, 1024);
284 case DEVICE_SYSCTL_PARENT:
285 value = dev->parent ? dev->parent->nameunit : "";
290 error = SYSCTL_OUT_STR(req, value);
297 device_sysctl_init(device_t dev)
299 devclass_t dc = dev->devclass;
302 if (dev->sysctl_tree != NULL)
304 devclass_sysctl_init(dc);
305 sysctl_ctx_init(&dev->sysctl_ctx);
306 dev->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&dev->sysctl_ctx,
307 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
308 dev->nameunit + strlen(dc->name),
309 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", "device_index");
310 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
311 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
312 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
313 "device description");
314 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
316 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
317 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
318 "device driver name");
319 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
320 OID_AUTO, "%location",
321 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
322 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
323 "device location relative to parent");
324 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
325 OID_AUTO, "%pnpinfo",
326 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
327 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
328 "device identification");
329 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
331 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
332 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
334 if (bus_get_domain(dev, &domain) == 0)
335 SYSCTL_ADD_INT(&dev->sysctl_ctx,
336 SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain",
337 CTLFLAG_RD, NULL, domain, "NUMA domain");
341 device_sysctl_update(device_t dev)
343 devclass_t dc = dev->devclass;
345 if (dev->sysctl_tree == NULL)
347 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
351 device_sysctl_fini(device_t dev)
353 if (dev->sysctl_tree == NULL)
355 sysctl_ctx_free(&dev->sysctl_ctx);
356 dev->sysctl_tree = NULL;
360 * /dev/devctl implementation
364 * This design allows only one reader for /dev/devctl. This is not desirable
365 * in the long run, but will get a lot of hair out of this implementation.
366 * Maybe we should make this device a clonable device.
368 * Also note: we specifically do not attach a device to the device_t tree
369 * to avoid potential chicken and egg problems. One could argue that all
370 * of this belongs to the root node.
373 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
374 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
375 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
376 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN |
377 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
379 static d_open_t devopen;
380 static d_close_t devclose;
381 static d_read_t devread;
382 static d_ioctl_t devioctl;
383 static d_poll_t devpoll;
384 static d_kqfilter_t devkqfilter;
386 static struct cdevsw dev_cdevsw = {
387 .d_version = D_VERSION,
393 .d_kqfilter = devkqfilter,
397 #define DEVCTL_BUFFER (1024 - sizeof(void *))
398 struct dev_event_info {
399 STAILQ_ENTRY(dev_event_info) dei_link;
400 char dei_data[DEVCTL_BUFFER];
403 STAILQ_HEAD(devq, dev_event_info);
405 static struct dev_softc {
418 static void filt_devctl_detach(struct knote *kn);
419 static int filt_devctl_read(struct knote *kn, long hint);
421 struct filterops devctl_rfiltops = {
423 .f_detach = filt_devctl_detach,
424 .f_event = filt_devctl_read,
427 static struct cdev *devctl_dev;
432 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
433 UID_ROOT, GID_WHEEL, 0600, "devctl");
434 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
435 cv_init(&devsoftc.cv, "dev cv");
436 STAILQ_INIT(&devsoftc.devq);
437 knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
438 if (devctl_queue_length > 0) {
439 devsoftc.zone = uma_zcreate("DEVCTL", sizeof(struct dev_event_info),
440 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
441 uma_prealloc(devsoftc.zone, devctl_queue_length);
447 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
449 mtx_lock(&devsoftc.mtx);
450 if (devsoftc.inuse) {
451 mtx_unlock(&devsoftc.mtx);
456 mtx_unlock(&devsoftc.mtx);
461 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
463 mtx_lock(&devsoftc.mtx);
465 devsoftc.nonblock = 0;
467 cv_broadcast(&devsoftc.cv);
468 funsetown(&devsoftc.sigio);
469 mtx_unlock(&devsoftc.mtx);
474 * The read channel for this device is used to report changes to
475 * userland in realtime. We are required to free the data as well as
476 * the n1 object because we allocate them separately. Also note that
477 * we return one record at a time. If you try to read this device a
478 * character at a time, you will lose the rest of the data. Listening
479 * programs are expected to cope.
482 devread(struct cdev *dev, struct uio *uio, int ioflag)
484 struct dev_event_info *n1;
487 mtx_lock(&devsoftc.mtx);
488 while (STAILQ_EMPTY(&devsoftc.devq)) {
489 if (devsoftc.nonblock) {
490 mtx_unlock(&devsoftc.mtx);
493 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
496 * Need to translate ERESTART to EINTR here? -- jake
498 mtx_unlock(&devsoftc.mtx);
502 n1 = STAILQ_FIRST(&devsoftc.devq);
503 STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
505 mtx_unlock(&devsoftc.mtx);
506 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
507 uma_zfree(devsoftc.zone, n1);
512 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
517 devsoftc.nonblock = 1;
519 devsoftc.nonblock = 0;
528 return fsetown(*(int *)data, &devsoftc.sigio);
530 *(int *)data = fgetown(&devsoftc.sigio);
533 /* (un)Support for other fcntl() calls. */
544 devpoll(struct cdev *dev, int events, struct thread *td)
548 mtx_lock(&devsoftc.mtx);
549 if (events & (POLLIN | POLLRDNORM)) {
550 if (!STAILQ_EMPTY(&devsoftc.devq))
551 revents = events & (POLLIN | POLLRDNORM);
553 selrecord(td, &devsoftc.sel);
555 mtx_unlock(&devsoftc.mtx);
561 devkqfilter(struct cdev *dev, struct knote *kn)
565 if (kn->kn_filter == EVFILT_READ) {
566 kn->kn_fop = &devctl_rfiltops;
567 knlist_add(&devsoftc.sel.si_note, kn, 0);
575 filt_devctl_detach(struct knote *kn)
577 knlist_remove(&devsoftc.sel.si_note, kn, 0);
581 filt_devctl_read(struct knote *kn, long hint)
583 kn->kn_data = devsoftc.queued;
584 return (kn->kn_data != 0);
588 * @brief Return whether the userland process is running
591 devctl_process_running(void)
593 return (devsoftc.inuse == 1);
596 static struct dev_event_info *
597 devctl_alloc_dei(void)
599 struct dev_event_info *dei = NULL;
601 mtx_lock(&devsoftc.mtx);
602 if (devctl_queue_length == 0)
604 if (devctl_queue_length == devsoftc.queued) {
605 dei = STAILQ_FIRST(&devsoftc.devq);
606 STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
609 /* dei can't be NULL -- we know we have at least one in the zone */
610 dei = uma_zalloc(devsoftc.zone, M_NOWAIT);
613 *dei->dei_data = '\0';
615 mtx_unlock(&devsoftc.mtx);
619 static struct dev_event_info *
620 devctl_alloc_dei_sb(struct sbuf *sb)
622 struct dev_event_info *dei;
624 dei = devctl_alloc_dei();
626 sbuf_new(sb, dei->dei_data, sizeof(dei->dei_data), SBUF_FIXEDLEN);
631 devctl_free_dei(struct dev_event_info *dei)
633 uma_zfree(devsoftc.zone, dei);
637 devctl_queue(struct dev_event_info *dei)
639 mtx_lock(&devsoftc.mtx);
640 STAILQ_INSERT_TAIL(&devsoftc.devq, dei, dei_link);
642 cv_broadcast(&devsoftc.cv);
643 KNOTE_LOCKED(&devsoftc.sel.si_note, 0);
644 mtx_unlock(&devsoftc.mtx);
645 selwakeup(&devsoftc.sel);
646 if (devsoftc.async && devsoftc.sigio != NULL)
647 pgsigio(&devsoftc.sigio, SIGIO, 0);
651 * @brief Send a 'notification' to userland, using standard ways
654 devctl_notify(const char *system, const char *subsystem, const char *type,
657 struct dev_event_info *dei;
660 if (system == NULL || subsystem == NULL || type == NULL)
662 dei = devctl_alloc_dei_sb(&sb);
665 sbuf_cpy(&sb, "!system=");
666 sbuf_cat(&sb, system);
667 sbuf_cat(&sb, " subsystem=");
668 sbuf_cat(&sb, subsystem);
669 sbuf_cat(&sb, " type=");
675 sbuf_putc(&sb, '\n');
676 if (sbuf_finish(&sb) != 0)
677 devctl_free_dei(dei); /* overflow -> drop it */
683 * Common routine that tries to make sending messages as easy as possible.
684 * We allocate memory for the data, copy strings into that, but do not
685 * free it unless there's an error. The dequeue part of the driver should
686 * free the data. We don't send data when the device is disabled. We do
687 * send data, even when we have no listeners, because we wish to avoid
688 * races relating to startup and restart of listening applications.
690 * devaddq is designed to string together the type of event, with the
691 * object of that event, plus the plug and play info and location info
692 * for that event. This is likely most useful for devices, but less
693 * useful for other consumers of this interface. Those should use
694 * the devctl_notify() interface instead.
697 * ${type}${what} at $(location dev) $(pnp-info dev) on $(parent dev)
700 devaddq(const char *type, const char *what, device_t dev)
702 struct dev_event_info *dei;
706 dei = devctl_alloc_dei_sb(&sb);
711 sbuf_cat(&sb, " at ");
713 /* Add in the location */
714 bus_child_location_sb(dev, &sb);
718 bus_child_pnpinfo_sb(dev, &sb);
720 /* Get the parent of this device, or / if high enough in the tree. */
721 if (device_get_parent(dev) == NULL)
722 parstr = "."; /* Or '/' ? */
724 parstr = device_get_nameunit(device_get_parent(dev));
725 sbuf_cat(&sb, " on ");
726 sbuf_cat(&sb, parstr);
727 sbuf_putc(&sb, '\n');
728 if (sbuf_finish(&sb) != 0)
733 devctl_free_dei(dei);
737 * A device was added to the tree. We are called just after it successfully
738 * attaches (that is, probe and attach success for this device). No call
739 * is made if a device is merely parented into the tree. See devnomatch
740 * if probe fails. If attach fails, no notification is sent (but maybe
741 * we should have a different message for this).
744 devadded(device_t dev)
746 devaddq("+", device_get_nameunit(dev), dev);
750 * A device was removed from the tree. We are called just before this
754 devremoved(device_t dev)
756 devaddq("-", device_get_nameunit(dev), dev);
760 * Called when there's no match for this device. This is only called
761 * the first time that no match happens, so we don't keep getting this
762 * message. Should that prove to be undesirable, we can change it.
763 * This is called when all drivers that can attach to a given bus
764 * decline to accept this device. Other errors may not be detected.
767 devnomatch(device_t dev)
769 devaddq("?", "", dev);
773 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
777 q = devctl_queue_length;
778 error = sysctl_handle_int(oidp, &q, 0, req);
779 if (error || !req->newptr)
785 * When set as a tunable, we've not yet initialized the mutex.
786 * It is safe to just assign to devctl_queue_length and return
787 * as we're racing no one. We'll use whatever value set in
790 if (!mtx_initialized(&devsoftc.mtx)) {
791 devctl_queue_length = q;
796 * XXX It's hard to grow or shrink the UMA zone. Only allow
797 * disabling the queue size for the moment until underlying
798 * UMA issues can be sorted out.
802 if (q == devctl_queue_length)
804 mtx_lock(&devsoftc.mtx);
805 devctl_queue_length = 0;
806 uma_zdestroy(devsoftc.zone);
808 mtx_unlock(&devsoftc.mtx);
813 * @brief safely quotes strings that might have double quotes in them.
815 * The devctl protocol relies on quoted strings having matching quotes.
816 * This routine quotes any internal quotes so the resulting string
817 * is safe to pass to snprintf to construct, for example pnp info strings.
819 * @param sb sbuf to place the characters into
820 * @param src Original buffer.
823 devctl_safe_quote_sb(struct sbuf *sb, const char *src)
825 while (*src != '\0') {
826 if (*src == '"' || *src == '\\')
828 sbuf_putc(sb, *src++);
832 /* End of /dev/devctl code */
834 static TAILQ_HEAD(,device) bus_data_devices;
835 static int bus_data_generation = 1;
837 static kobj_method_t null_methods[] = {
841 DEFINE_CLASS(null, null_methods, 0);
844 * Bus pass implementation
847 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
848 int bus_current_pass = BUS_PASS_ROOT;
852 * @brief Register the pass level of a new driver attachment
854 * Register a new driver attachment's pass level. If no driver
855 * attachment with the same pass level has been added, then @p new
856 * will be added to the global passes list.
858 * @param new the new driver attachment
861 driver_register_pass(struct driverlink *new)
863 struct driverlink *dl;
865 /* We only consider pass numbers during boot. */
866 if (bus_current_pass == BUS_PASS_DEFAULT)
870 * Walk the passes list. If we already know about this pass
871 * then there is nothing to do. If we don't, then insert this
872 * driver link into the list.
874 TAILQ_FOREACH(dl, &passes, passlink) {
875 if (dl->pass < new->pass)
877 if (dl->pass == new->pass)
879 TAILQ_INSERT_BEFORE(dl, new, passlink);
882 TAILQ_INSERT_TAIL(&passes, new, passlink);
886 * @brief Raise the current bus pass
888 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
889 * method on the root bus to kick off a new device tree scan for each
890 * new pass level that has at least one driver.
893 bus_set_pass(int pass)
895 struct driverlink *dl;
897 if (bus_current_pass > pass)
898 panic("Attempt to lower bus pass level");
900 TAILQ_FOREACH(dl, &passes, passlink) {
901 /* Skip pass values below the current pass level. */
902 if (dl->pass <= bus_current_pass)
906 * Bail once we hit a driver with a pass level that is
913 * Raise the pass level to the next level and rescan
916 bus_current_pass = dl->pass;
917 BUS_NEW_PASS(root_bus);
921 * If there isn't a driver registered for the requested pass,
922 * then bus_current_pass might still be less than 'pass'. Set
923 * it to 'pass' in that case.
925 if (bus_current_pass < pass)
926 bus_current_pass = pass;
927 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
931 * Devclass implementation
934 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
938 * @brief Find or create a device class
940 * If a device class with the name @p classname exists, return it,
941 * otherwise if @p create is non-zero create and return a new device
944 * If @p parentname is non-NULL, the parent of the devclass is set to
945 * the devclass of that name.
947 * @param classname the devclass name to find or create
948 * @param parentname the parent devclass name or @c NULL
949 * @param create non-zero to create a devclass
952 devclass_find_internal(const char *classname, const char *parentname,
957 PDEBUG(("looking for %s", classname));
961 TAILQ_FOREACH(dc, &devclasses, link) {
962 if (!strcmp(dc->name, classname))
967 PDEBUG(("creating %s", classname));
968 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
969 M_BUS, M_NOWAIT | M_ZERO);
973 dc->name = (char*) (dc + 1);
974 strcpy(dc->name, classname);
975 TAILQ_INIT(&dc->drivers);
976 TAILQ_INSERT_TAIL(&devclasses, dc, link);
978 bus_data_generation_update();
982 * If a parent class is specified, then set that as our parent so
983 * that this devclass will support drivers for the parent class as
984 * well. If the parent class has the same name don't do this though
985 * as it creates a cycle that can trigger an infinite loop in
986 * device_probe_child() if a device exists for which there is no
989 if (parentname && dc && !dc->parent &&
990 strcmp(classname, parentname) != 0) {
991 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
992 dc->parent->flags |= DC_HAS_CHILDREN;
999 * @brief Create a device class
1001 * If a device class with the name @p classname exists, return it,
1002 * otherwise create and return a new device class.
1004 * @param classname the devclass name to find or create
1007 devclass_create(const char *classname)
1009 return (devclass_find_internal(classname, NULL, TRUE));
1013 * @brief Find a device class
1015 * If a device class with the name @p classname exists, return it,
1016 * otherwise return @c NULL.
1018 * @param classname the devclass name to find
1021 devclass_find(const char *classname)
1023 return (devclass_find_internal(classname, NULL, FALSE));
1027 * @brief Register that a device driver has been added to a devclass
1029 * Register that a device driver has been added to a devclass. This
1030 * is called by devclass_add_driver to accomplish the recursive
1031 * notification of all the children classes of dc, as well as dc.
1032 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1035 * We do a full search here of the devclass list at each iteration
1036 * level to save storing children-lists in the devclass structure. If
1037 * we ever move beyond a few dozen devices doing this, we may need to
1040 * @param dc the devclass to edit
1041 * @param driver the driver that was just added
1044 devclass_driver_added(devclass_t dc, driver_t *driver)
1050 * Call BUS_DRIVER_ADDED for any existing buses in this class.
1052 for (i = 0; i < dc->maxunit; i++)
1053 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1054 BUS_DRIVER_ADDED(dc->devices[i], driver);
1057 * Walk through the children classes. Since we only keep a
1058 * single parent pointer around, we walk the entire list of
1059 * devclasses looking for children. We set the
1060 * DC_HAS_CHILDREN flag when a child devclass is created on
1061 * the parent, so we only walk the list for those devclasses
1062 * that have children.
1064 if (!(dc->flags & DC_HAS_CHILDREN))
1067 TAILQ_FOREACH(dc, &devclasses, link) {
1068 if (dc->parent == parent)
1069 devclass_driver_added(dc, driver);
1074 * @brief Add a device driver to a device class
1076 * Add a device driver to a devclass. This is normally called
1077 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1078 * all devices in the devclass will be called to allow them to attempt
1079 * to re-probe any unmatched children.
1081 * @param dc the devclass to edit
1082 * @param driver the driver to register
1085 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1088 const char *parentname;
1090 PDEBUG(("%s", DRIVERNAME(driver)));
1092 /* Don't allow invalid pass values. */
1093 if (pass <= BUS_PASS_ROOT)
1096 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1101 * Compile the driver's methods. Also increase the reference count
1102 * so that the class doesn't get freed when the last instance
1103 * goes. This means we can safely use static methods and avoids a
1104 * double-free in devclass_delete_driver.
1106 kobj_class_compile((kobj_class_t) driver);
1109 * If the driver has any base classes, make the
1110 * devclass inherit from the devclass of the driver's
1111 * first base class. This will allow the system to
1112 * search for drivers in both devclasses for children
1113 * of a device using this driver.
1115 if (driver->baseclasses)
1116 parentname = driver->baseclasses[0]->name;
1119 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1121 dl->driver = driver;
1122 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1123 driver->refs++; /* XXX: kobj_mtx */
1125 driver_register_pass(dl);
1127 if (device_frozen) {
1128 dl->flags |= DL_DEFERRED_PROBE;
1130 devclass_driver_added(dc, driver);
1132 bus_data_generation_update();
1137 * @brief Register that a device driver has been deleted from a devclass
1139 * Register that a device driver has been removed from a devclass.
1140 * This is called by devclass_delete_driver to accomplish the
1141 * recursive notification of all the children classes of busclass, as
1142 * well as busclass. Each layer will attempt to detach the driver
1143 * from any devices that are children of the bus's devclass. The function
1144 * will return an error if a device fails to detach.
1146 * We do a full search here of the devclass list at each iteration
1147 * level to save storing children-lists in the devclass structure. If
1148 * we ever move beyond a few dozen devices doing this, we may need to
1151 * @param busclass the devclass of the parent bus
1152 * @param dc the devclass of the driver being deleted
1153 * @param driver the driver being deleted
1156 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1163 * Disassociate from any devices. We iterate through all the
1164 * devices in the devclass of the driver and detach any which are
1165 * using the driver and which have a parent in the devclass which
1166 * we are deleting from.
1168 * Note that since a driver can be in multiple devclasses, we
1169 * should not detach devices which are not children of devices in
1170 * the affected devclass.
1172 * If we're frozen, we don't generate NOMATCH events. Mark to
1175 for (i = 0; i < dc->maxunit; i++) {
1176 if (dc->devices[i]) {
1177 dev = dc->devices[i];
1178 if (dev->driver == driver && dev->parent &&
1179 dev->parent->devclass == busclass) {
1180 if ((error = device_detach(dev)) != 0)
1182 if (device_frozen) {
1183 dev->flags &= ~DF_DONENOMATCH;
1184 dev->flags |= DF_NEEDNOMATCH;
1186 BUS_PROBE_NOMATCH(dev->parent, dev);
1188 dev->flags |= DF_DONENOMATCH;
1195 * Walk through the children classes. Since we only keep a
1196 * single parent pointer around, we walk the entire list of
1197 * devclasses looking for children. We set the
1198 * DC_HAS_CHILDREN flag when a child devclass is created on
1199 * the parent, so we only walk the list for those devclasses
1200 * that have children.
1202 if (!(busclass->flags & DC_HAS_CHILDREN))
1205 TAILQ_FOREACH(busclass, &devclasses, link) {
1206 if (busclass->parent == parent) {
1207 error = devclass_driver_deleted(busclass, dc, driver);
1216 * @brief Delete a device driver from a device class
1218 * Delete a device driver from a devclass. This is normally called
1219 * automatically by DRIVER_MODULE().
1221 * If the driver is currently attached to any devices,
1222 * devclass_delete_driver() will first attempt to detach from each
1223 * device. If one of the detach calls fails, the driver will not be
1226 * @param dc the devclass to edit
1227 * @param driver the driver to unregister
1230 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1232 devclass_t dc = devclass_find(driver->name);
1236 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1242 * Find the link structure in the bus' list of drivers.
1244 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1245 if (dl->driver == driver)
1250 PDEBUG(("%s not found in %s list", driver->name,
1255 error = devclass_driver_deleted(busclass, dc, driver);
1259 TAILQ_REMOVE(&busclass->drivers, dl, link);
1264 if (driver->refs == 0)
1265 kobj_class_free((kobj_class_t) driver);
1267 bus_data_generation_update();
1272 * @brief Quiesces a set of device drivers from a device class
1274 * Quiesce a device driver from a devclass. This is normally called
1275 * automatically by DRIVER_MODULE().
1277 * If the driver is currently attached to any devices,
1278 * devclass_quiesece_driver() will first attempt to quiesce each
1281 * @param dc the devclass to edit
1282 * @param driver the driver to unregister
1285 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1287 devclass_t dc = devclass_find(driver->name);
1293 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1299 * Find the link structure in the bus' list of drivers.
1301 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1302 if (dl->driver == driver)
1307 PDEBUG(("%s not found in %s list", driver->name,
1313 * Quiesce all devices. We iterate through all the devices in
1314 * the devclass of the driver and quiesce any which are using
1315 * the driver and which have a parent in the devclass which we
1318 * Note that since a driver can be in multiple devclasses, we
1319 * should not quiesce devices which are not children of
1320 * devices in the affected devclass.
1322 for (i = 0; i < dc->maxunit; i++) {
1323 if (dc->devices[i]) {
1324 dev = dc->devices[i];
1325 if (dev->driver == driver && dev->parent &&
1326 dev->parent->devclass == busclass) {
1327 if ((error = device_quiesce(dev)) != 0)
1340 devclass_find_driver_internal(devclass_t dc, const char *classname)
1344 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1346 TAILQ_FOREACH(dl, &dc->drivers, link) {
1347 if (!strcmp(dl->driver->name, classname))
1351 PDEBUG(("not found"));
1356 * @brief Return the name of the devclass
1359 devclass_get_name(devclass_t dc)
1365 * @brief Find a device given a unit number
1367 * @param dc the devclass to search
1368 * @param unit the unit number to search for
1370 * @returns the device with the given unit number or @c
1371 * NULL if there is no such device
1374 devclass_get_device(devclass_t dc, int unit)
1376 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1378 return (dc->devices[unit]);
1382 * @brief Find the softc field of a device given a unit number
1384 * @param dc the devclass to search
1385 * @param unit the unit number to search for
1387 * @returns the softc field of the device with the given
1388 * unit number or @c NULL if there is no such
1392 devclass_get_softc(devclass_t dc, int unit)
1396 dev = devclass_get_device(dc, unit);
1400 return (device_get_softc(dev));
1404 * @brief Get a list of devices in the devclass
1406 * An array containing a list of all the devices in the given devclass
1407 * is allocated and returned in @p *devlistp. The number of devices
1408 * in the array is returned in @p *devcountp. The caller should free
1409 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1411 * @param dc the devclass to examine
1412 * @param devlistp points at location for array pointer return
1414 * @param devcountp points at location for array size return value
1417 * @retval ENOMEM the array allocation failed
1420 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1425 count = devclass_get_count(dc);
1426 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1431 for (i = 0; i < dc->maxunit; i++) {
1432 if (dc->devices[i]) {
1433 list[count] = dc->devices[i];
1445 * @brief Get a list of drivers in the devclass
1447 * An array containing a list of pointers to all the drivers in the
1448 * given devclass is allocated and returned in @p *listp. The number
1449 * of drivers in the array is returned in @p *countp. The caller should
1450 * free the array using @c free(p, M_TEMP).
1452 * @param dc the devclass to examine
1453 * @param listp gives location for array pointer return value
1454 * @param countp gives location for number of array elements
1458 * @retval ENOMEM the array allocation failed
1461 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1468 TAILQ_FOREACH(dl, &dc->drivers, link)
1470 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1475 TAILQ_FOREACH(dl, &dc->drivers, link) {
1476 list[count] = dl->driver;
1486 * @brief Get the number of devices in a devclass
1488 * @param dc the devclass to examine
1491 devclass_get_count(devclass_t dc)
1496 for (i = 0; i < dc->maxunit; i++)
1503 * @brief Get the maximum unit number used in a devclass
1505 * Note that this is one greater than the highest currently-allocated
1506 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1507 * that not even the devclass has been allocated yet.
1509 * @param dc the devclass to examine
1512 devclass_get_maxunit(devclass_t dc)
1516 return (dc->maxunit);
1520 * @brief Find a free unit number in a devclass
1522 * This function searches for the first unused unit number greater
1523 * that or equal to @p unit.
1525 * @param dc the devclass to examine
1526 * @param unit the first unit number to check
1529 devclass_find_free_unit(devclass_t dc, int unit)
1533 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1539 * @brief Set the parent of a devclass
1541 * The parent class is normally initialised automatically by
1544 * @param dc the devclass to edit
1545 * @param pdc the new parent devclass
1548 devclass_set_parent(devclass_t dc, devclass_t pdc)
1554 * @brief Get the parent of a devclass
1556 * @param dc the devclass to examine
1559 devclass_get_parent(devclass_t dc)
1561 return (dc->parent);
1564 struct sysctl_ctx_list *
1565 devclass_get_sysctl_ctx(devclass_t dc)
1567 return (&dc->sysctl_ctx);
1571 devclass_get_sysctl_tree(devclass_t dc)
1573 return (dc->sysctl_tree);
1578 * @brief Allocate a unit number
1580 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1581 * will do). The allocated unit number is returned in @p *unitp.
1583 * @param dc the devclass to allocate from
1584 * @param unitp points at the location for the allocated unit
1588 * @retval EEXIST the requested unit number is already allocated
1589 * @retval ENOMEM memory allocation failure
1592 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1597 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1599 /* Ask the parent bus if it wants to wire this device. */
1601 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1604 /* If we were given a wired unit number, check for existing device */
1607 if (unit >= 0 && unit < dc->maxunit &&
1608 dc->devices[unit] != NULL) {
1610 printf("%s: %s%d already exists; skipping it\n",
1611 dc->name, dc->name, *unitp);
1615 /* Unwired device, find the next available slot for it */
1617 for (unit = 0;; unit++) {
1618 /* If there is an "at" hint for a unit then skip it. */
1619 if (resource_string_value(dc->name, unit, "at", &s) ==
1623 /* If this device slot is already in use, skip it. */
1624 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1632 * We've selected a unit beyond the length of the table, so let's
1633 * extend the table to make room for all units up to and including
1636 if (unit >= dc->maxunit) {
1637 device_t *newlist, *oldlist;
1640 oldlist = dc->devices;
1641 newsize = roundup((unit + 1),
1642 MAX(1, MINALLOCSIZE / sizeof(device_t)));
1643 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1646 if (oldlist != NULL)
1647 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1648 bzero(newlist + dc->maxunit,
1649 sizeof(device_t) * (newsize - dc->maxunit));
1650 dc->devices = newlist;
1651 dc->maxunit = newsize;
1652 if (oldlist != NULL)
1653 free(oldlist, M_BUS);
1655 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1663 * @brief Add a device to a devclass
1665 * A unit number is allocated for the device (using the device's
1666 * preferred unit number if any) and the device is registered in the
1667 * devclass. This allows the device to be looked up by its unit
1668 * number, e.g. by decoding a dev_t minor number.
1670 * @param dc the devclass to add to
1671 * @param dev the device to add
1674 * @retval EEXIST the requested unit number is already allocated
1675 * @retval ENOMEM memory allocation failure
1678 devclass_add_device(devclass_t dc, device_t dev)
1682 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1684 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1687 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1691 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1692 free(dev->nameunit, M_BUS);
1693 dev->nameunit = NULL;
1696 dc->devices[dev->unit] = dev;
1698 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1705 * @brief Delete a device from a devclass
1707 * The device is removed from the devclass's device list and its unit
1710 * @param dc the devclass to delete from
1711 * @param dev the device to delete
1716 devclass_delete_device(devclass_t dc, device_t dev)
1721 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1723 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1724 panic("devclass_delete_device: inconsistent device class");
1725 dc->devices[dev->unit] = NULL;
1726 if (dev->flags & DF_WILDCARD)
1728 dev->devclass = NULL;
1729 free(dev->nameunit, M_BUS);
1730 dev->nameunit = NULL;
1737 * @brief Make a new device and add it as a child of @p parent
1739 * @param parent the parent of the new device
1740 * @param name the devclass name of the new device or @c NULL
1741 * to leave the devclass unspecified
1742 * @parem unit the unit number of the new device of @c -1 to
1743 * leave the unit number unspecified
1745 * @returns the new device
1748 make_device(device_t parent, const char *name, int unit)
1753 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1756 dc = devclass_find_internal(name, NULL, TRUE);
1758 printf("make_device: can't find device class %s\n",
1766 dev = malloc(sizeof(*dev), M_BUS, M_NOWAIT|M_ZERO);
1770 dev->parent = parent;
1771 TAILQ_INIT(&dev->children);
1772 kobj_init((kobj_t) dev, &null_class);
1774 dev->devclass = NULL;
1776 dev->nameunit = NULL;
1780 dev->flags = DF_ENABLED;
1783 dev->flags |= DF_WILDCARD;
1785 dev->flags |= DF_FIXEDCLASS;
1786 if (devclass_add_device(dc, dev)) {
1787 kobj_delete((kobj_t) dev, M_BUS);
1791 if (parent != NULL && device_has_quiet_children(parent))
1792 dev->flags |= DF_QUIET | DF_QUIET_CHILDREN;
1796 dev->state = DS_NOTPRESENT;
1798 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1799 bus_data_generation_update();
1806 * @brief Print a description of a device.
1809 device_print_child(device_t dev, device_t child)
1813 if (device_is_alive(child))
1814 retval += BUS_PRINT_CHILD(dev, child);
1816 retval += device_printf(child, " not found\n");
1822 * @brief Create a new device
1824 * This creates a new device and adds it as a child of an existing
1825 * parent device. The new device will be added after the last existing
1826 * child with order zero.
1828 * @param dev the device which will be the parent of the
1830 * @param name devclass name for new device or @c NULL if not
1832 * @param unit unit number for new device or @c -1 if not
1835 * @returns the new device
1838 device_add_child(device_t dev, const char *name, int unit)
1840 return (device_add_child_ordered(dev, 0, name, unit));
1844 * @brief Create a new device
1846 * This creates a new device and adds it as a child of an existing
1847 * parent device. The new device will be added after the last existing
1848 * child with the same order.
1850 * @param dev the device which will be the parent of the
1852 * @param order a value which is used to partially sort the
1853 * children of @p dev - devices created using
1854 * lower values of @p order appear first in @p
1855 * dev's list of children
1856 * @param name devclass name for new device or @c NULL if not
1858 * @param unit unit number for new device or @c -1 if not
1861 * @returns the new device
1864 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1869 PDEBUG(("%s at %s with order %u as unit %d",
1870 name, DEVICENAME(dev), order, unit));
1871 KASSERT(name != NULL || unit == -1,
1872 ("child device with wildcard name and specific unit number"));
1874 child = make_device(dev, name, unit);
1877 child->order = order;
1879 TAILQ_FOREACH(place, &dev->children, link) {
1880 if (place->order > order)
1886 * The device 'place' is the first device whose order is
1887 * greater than the new child.
1889 TAILQ_INSERT_BEFORE(place, child, link);
1892 * The new child's order is greater or equal to the order of
1893 * any existing device. Add the child to the tail of the list.
1895 TAILQ_INSERT_TAIL(&dev->children, child, link);
1898 bus_data_generation_update();
1903 * @brief Delete a device
1905 * This function deletes a device along with all of its children. If
1906 * the device currently has a driver attached to it, the device is
1907 * detached first using device_detach().
1909 * @param dev the parent device
1910 * @param child the device to delete
1913 * @retval non-zero a unit error code describing the error
1916 device_delete_child(device_t dev, device_t child)
1919 device_t grandchild;
1921 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1923 /* detach parent before deleting children, if any */
1924 if ((error = device_detach(child)) != 0)
1927 /* remove children second */
1928 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1929 error = device_delete_child(child, grandchild);
1934 if (child->devclass)
1935 devclass_delete_device(child->devclass, child);
1937 BUS_CHILD_DELETED(dev, child);
1938 TAILQ_REMOVE(&dev->children, child, link);
1939 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1940 kobj_delete((kobj_t) child, M_BUS);
1942 bus_data_generation_update();
1947 * @brief Delete all children devices of the given device, if any.
1949 * This function deletes all children devices of the given device, if
1950 * any, using the device_delete_child() function for each device it
1951 * finds. If a child device cannot be deleted, this function will
1952 * return an error code.
1954 * @param dev the parent device
1957 * @retval non-zero a device would not detach
1960 device_delete_children(device_t dev)
1965 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1969 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1970 error = device_delete_child(dev, child);
1972 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1980 * @brief Find a device given a unit number
1982 * This is similar to devclass_get_devices() but only searches for
1983 * devices which have @p dev as a parent.
1985 * @param dev the parent device to search
1986 * @param unit the unit number to search for. If the unit is -1,
1987 * return the first child of @p dev which has name
1988 * @p classname (that is, the one with the lowest unit.)
1990 * @returns the device with the given unit number or @c
1991 * NULL if there is no such device
1994 device_find_child(device_t dev, const char *classname, int unit)
1999 dc = devclass_find(classname);
2004 child = devclass_get_device(dc, unit);
2005 if (child && child->parent == dev)
2008 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
2009 child = devclass_get_device(dc, unit);
2010 if (child && child->parent == dev)
2021 first_matching_driver(devclass_t dc, device_t dev)
2024 return (devclass_find_driver_internal(dc, dev->devclass->name));
2025 return (TAILQ_FIRST(&dc->drivers));
2032 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
2034 if (dev->devclass) {
2036 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
2037 if (!strcmp(dev->devclass->name, dl->driver->name))
2041 return (TAILQ_NEXT(last, link));
2048 device_probe_child(device_t dev, device_t child)
2051 driverlink_t best = NULL;
2053 int result, pri = 0;
2054 int hasclass = (child->devclass != NULL);
2060 panic("device_probe_child: parent device has no devclass");
2063 * If the state is already probed, then return. However, don't
2064 * return if we can rebid this object.
2066 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2069 for (; dc; dc = dc->parent) {
2070 for (dl = first_matching_driver(dc, child);
2072 dl = next_matching_driver(dc, child, dl)) {
2073 /* If this driver's pass is too high, then ignore it. */
2074 if (dl->pass > bus_current_pass)
2077 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2078 result = device_set_driver(child, dl->driver);
2079 if (result == ENOMEM)
2081 else if (result != 0)
2084 if (device_set_devclass(child,
2085 dl->driver->name) != 0) {
2086 char const * devname =
2087 device_get_name(child);
2088 if (devname == NULL)
2089 devname = "(unknown)";
2090 printf("driver bug: Unable to set "
2091 "devclass (class: %s "
2095 (void)device_set_driver(child, NULL);
2100 /* Fetch any flags for the device before probing. */
2101 resource_int_value(dl->driver->name, child->unit,
2102 "flags", &child->devflags);
2104 result = DEVICE_PROBE(child);
2106 /* Reset flags and devclass before the next probe. */
2107 child->devflags = 0;
2109 (void)device_set_devclass(child, NULL);
2112 * If the driver returns SUCCESS, there can be
2113 * no higher match for this device.
2122 * Reset DF_QUIET in case this driver doesn't
2123 * end up as the best driver.
2125 device_verbose(child);
2128 * Probes that return BUS_PROBE_NOWILDCARD or lower
2129 * only match on devices whose driver was explicitly
2132 if (result <= BUS_PROBE_NOWILDCARD &&
2133 !(child->flags & DF_FIXEDCLASS)) {
2138 * The driver returned an error so it
2139 * certainly doesn't match.
2142 (void)device_set_driver(child, NULL);
2147 * A priority lower than SUCCESS, remember the
2148 * best matching driver. Initialise the value
2149 * of pri for the first match.
2151 if (best == NULL || result > pri) {
2158 * If we have an unambiguous match in this devclass,
2159 * don't look in the parent.
2161 if (best && pri == 0)
2166 * If we found a driver, change state and initialise the devclass.
2168 /* XXX What happens if we rebid and got no best? */
2171 * If this device was attached, and we were asked to
2172 * rescan, and it is a different driver, then we have
2173 * to detach the old driver and reattach this new one.
2174 * Note, we don't have to check for DF_REBID here
2175 * because if the state is > DS_ALIVE, we know it must
2178 * This assumes that all DF_REBID drivers can have
2179 * their probe routine called at any time and that
2180 * they are idempotent as well as completely benign in
2181 * normal operations.
2183 * We also have to make sure that the detach
2184 * succeeded, otherwise we fail the operation (or
2185 * maybe it should just fail silently? I'm torn).
2187 if (child->state > DS_ALIVE && best->driver != child->driver)
2188 if ((result = device_detach(dev)) != 0)
2191 /* Set the winning driver, devclass, and flags. */
2192 if (!child->devclass) {
2193 result = device_set_devclass(child, best->driver->name);
2197 result = device_set_driver(child, best->driver);
2200 resource_int_value(best->driver->name, child->unit,
2201 "flags", &child->devflags);
2205 * A bit bogus. Call the probe method again to make
2206 * sure that we have the right description.
2208 DEVICE_PROBE(child);
2210 child->flags |= DF_REBID;
2213 child->flags &= ~DF_REBID;
2214 child->state = DS_ALIVE;
2216 bus_data_generation_update();
2224 * @brief Return the parent of a device
2227 device_get_parent(device_t dev)
2229 return (dev->parent);
2233 * @brief Get a list of children of a device
2235 * An array containing a list of all the children of the given device
2236 * is allocated and returned in @p *devlistp. The number of devices
2237 * in the array is returned in @p *devcountp. The caller should free
2238 * the array using @c free(p, M_TEMP).
2240 * @param dev the device to examine
2241 * @param devlistp points at location for array pointer return
2243 * @param devcountp points at location for array size return value
2246 * @retval ENOMEM the array allocation failed
2249 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2256 TAILQ_FOREACH(child, &dev->children, link) {
2265 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2270 TAILQ_FOREACH(child, &dev->children, link) {
2271 list[count] = child;
2282 * @brief Return the current driver for the device or @c NULL if there
2283 * is no driver currently attached
2286 device_get_driver(device_t dev)
2288 return (dev->driver);
2292 * @brief Return the current devclass for the device or @c NULL if
2296 device_get_devclass(device_t dev)
2298 return (dev->devclass);
2302 * @brief Return the name of the device's devclass or @c NULL if there
2306 device_get_name(device_t dev)
2308 if (dev != NULL && dev->devclass)
2309 return (devclass_get_name(dev->devclass));
2314 * @brief Return a string containing the device's devclass name
2315 * followed by an ascii representation of the device's unit number
2319 device_get_nameunit(device_t dev)
2321 return (dev->nameunit);
2325 * @brief Return the device's unit number.
2328 device_get_unit(device_t dev)
2334 * @brief Return the device's description string
2337 device_get_desc(device_t dev)
2343 * @brief Return the device's flags
2346 device_get_flags(device_t dev)
2348 return (dev->devflags);
2351 struct sysctl_ctx_list *
2352 device_get_sysctl_ctx(device_t dev)
2354 return (&dev->sysctl_ctx);
2358 device_get_sysctl_tree(device_t dev)
2360 return (dev->sysctl_tree);
2364 * @brief Print the name of the device followed by a colon and a space
2366 * @returns the number of characters printed
2369 device_print_prettyname(device_t dev)
2371 const char *name = device_get_name(dev);
2374 return (printf("unknown: "));
2375 return (printf("%s%d: ", name, device_get_unit(dev)));
2379 * @brief Print the name of the device followed by a colon, a space
2380 * and the result of calling vprintf() with the value of @p fmt and
2381 * the following arguments.
2383 * @returns the number of characters printed
2386 device_printf(device_t dev, const char * fmt, ...)
2396 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
2397 sbuf_set_drain(&sb, sbuf_printf_drain, &retval);
2399 name = device_get_name(dev);
2402 sbuf_cat(&sb, "unknown: ");
2404 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
2407 sbuf_vprintf(&sb, fmt, ap);
2420 device_set_desc_internal(device_t dev, const char* desc, int copy)
2422 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2423 free(dev->desc, M_BUS);
2424 dev->flags &= ~DF_DESCMALLOCED;
2429 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2431 strcpy(dev->desc, desc);
2432 dev->flags |= DF_DESCMALLOCED;
2435 /* Avoid a -Wcast-qual warning */
2436 dev->desc = (char *)(uintptr_t) desc;
2439 bus_data_generation_update();
2443 * @brief Set the device's description
2445 * The value of @c desc should be a string constant that will not
2446 * change (at least until the description is changed in a subsequent
2447 * call to device_set_desc() or device_set_desc_copy()).
2450 device_set_desc(device_t dev, const char* desc)
2452 device_set_desc_internal(dev, desc, FALSE);
2456 * @brief Set the device's description
2458 * The string pointed to by @c desc is copied. Use this function if
2459 * the device description is generated, (e.g. with sprintf()).
2462 device_set_desc_copy(device_t dev, const char* desc)
2464 device_set_desc_internal(dev, desc, TRUE);
2468 * @brief Set the device's flags
2471 device_set_flags(device_t dev, uint32_t flags)
2473 dev->devflags = flags;
2477 * @brief Return the device's softc field
2479 * The softc is allocated and zeroed when a driver is attached, based
2480 * on the size field of the driver.
2483 device_get_softc(device_t dev)
2485 return (dev->softc);
2489 * @brief Set the device's softc field
2491 * Most drivers do not need to use this since the softc is allocated
2492 * automatically when the driver is attached.
2495 device_set_softc(device_t dev, void *softc)
2497 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2498 free(dev->softc, M_BUS_SC);
2501 dev->flags |= DF_EXTERNALSOFTC;
2503 dev->flags &= ~DF_EXTERNALSOFTC;
2507 * @brief Free claimed softc
2509 * Most drivers do not need to use this since the softc is freed
2510 * automatically when the driver is detached.
2513 device_free_softc(void *softc)
2515 free(softc, M_BUS_SC);
2519 * @brief Claim softc
2521 * This function can be used to let the driver free the automatically
2522 * allocated softc using "device_free_softc()". This function is
2523 * useful when the driver is refcounting the softc and the softc
2524 * cannot be freed when the "device_detach" method is called.
2527 device_claim_softc(device_t dev)
2530 dev->flags |= DF_EXTERNALSOFTC;
2532 dev->flags &= ~DF_EXTERNALSOFTC;
2536 * @brief Get the device's ivars field
2538 * The ivars field is used by the parent device to store per-device
2539 * state (e.g. the physical location of the device or a list of
2543 device_get_ivars(device_t dev)
2545 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2546 return (dev->ivars);
2550 * @brief Set the device's ivars field
2553 device_set_ivars(device_t dev, void * ivars)
2555 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2560 * @brief Return the device's state
2563 device_get_state(device_t dev)
2565 return (dev->state);
2569 * @brief Set the DF_ENABLED flag for the device
2572 device_enable(device_t dev)
2574 dev->flags |= DF_ENABLED;
2578 * @brief Clear the DF_ENABLED flag for the device
2581 device_disable(device_t dev)
2583 dev->flags &= ~DF_ENABLED;
2587 * @brief Increment the busy counter for the device
2590 device_busy(device_t dev)
2592 if (dev->state < DS_ATTACHING)
2593 panic("device_busy: called for unattached device");
2594 if (dev->busy == 0 && dev->parent)
2595 device_busy(dev->parent);
2597 if (dev->state == DS_ATTACHED)
2598 dev->state = DS_BUSY;
2602 * @brief Decrement the busy counter for the device
2605 device_unbusy(device_t dev)
2607 if (dev->busy != 0 && dev->state != DS_BUSY &&
2608 dev->state != DS_ATTACHING)
2609 panic("device_unbusy: called for non-busy device %s",
2610 device_get_nameunit(dev));
2612 if (dev->busy == 0) {
2614 device_unbusy(dev->parent);
2615 if (dev->state == DS_BUSY)
2616 dev->state = DS_ATTACHED;
2621 * @brief Set the DF_QUIET flag for the device
2624 device_quiet(device_t dev)
2626 dev->flags |= DF_QUIET;
2630 * @brief Set the DF_QUIET_CHILDREN flag for the device
2633 device_quiet_children(device_t dev)
2635 dev->flags |= DF_QUIET_CHILDREN;
2639 * @brief Clear the DF_QUIET flag for the device
2642 device_verbose(device_t dev)
2644 dev->flags &= ~DF_QUIET;
2648 * @brief Return non-zero if the DF_QUIET_CHIDLREN flag is set on the device
2651 device_has_quiet_children(device_t dev)
2653 return ((dev->flags & DF_QUIET_CHILDREN) != 0);
2657 * @brief Return non-zero if the DF_QUIET flag is set on the device
2660 device_is_quiet(device_t dev)
2662 return ((dev->flags & DF_QUIET) != 0);
2666 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2669 device_is_enabled(device_t dev)
2671 return ((dev->flags & DF_ENABLED) != 0);
2675 * @brief Return non-zero if the device was successfully probed
2678 device_is_alive(device_t dev)
2680 return (dev->state >= DS_ALIVE);
2684 * @brief Return non-zero if the device currently has a driver
2688 device_is_attached(device_t dev)
2690 return (dev->state >= DS_ATTACHED);
2694 * @brief Return non-zero if the device is currently suspended.
2697 device_is_suspended(device_t dev)
2699 return ((dev->flags & DF_SUSPENDED) != 0);
2703 * @brief Set the devclass of a device
2704 * @see devclass_add_device().
2707 device_set_devclass(device_t dev, const char *classname)
2714 devclass_delete_device(dev->devclass, dev);
2718 if (dev->devclass) {
2719 printf("device_set_devclass: device class already set\n");
2723 dc = devclass_find_internal(classname, NULL, TRUE);
2727 error = devclass_add_device(dc, dev);
2729 bus_data_generation_update();
2734 * @brief Set the devclass of a device and mark the devclass fixed.
2735 * @see device_set_devclass()
2738 device_set_devclass_fixed(device_t dev, const char *classname)
2742 if (classname == NULL)
2745 error = device_set_devclass(dev, classname);
2748 dev->flags |= DF_FIXEDCLASS;
2753 * @brief Query the device to determine if it's of a fixed devclass
2754 * @see device_set_devclass_fixed()
2757 device_is_devclass_fixed(device_t dev)
2759 return ((dev->flags & DF_FIXEDCLASS) != 0);
2763 * @brief Set the driver of a device
2766 * @retval EBUSY the device already has a driver attached
2767 * @retval ENOMEM a memory allocation failure occurred
2770 device_set_driver(device_t dev, driver_t *driver)
2773 struct domainset *policy;
2775 if (dev->state >= DS_ATTACHED)
2778 if (dev->driver == driver)
2781 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2782 free(dev->softc, M_BUS_SC);
2785 device_set_desc(dev, NULL);
2786 kobj_delete((kobj_t) dev, NULL);
2787 dev->driver = driver;
2789 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2790 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2791 if (bus_get_domain(dev, &domain) == 0)
2792 policy = DOMAINSET_PREF(domain);
2794 policy = DOMAINSET_RR();
2795 dev->softc = malloc_domainset(driver->size, M_BUS_SC,
2796 policy, M_NOWAIT | M_ZERO);
2798 kobj_delete((kobj_t) dev, NULL);
2799 kobj_init((kobj_t) dev, &null_class);
2805 kobj_init((kobj_t) dev, &null_class);
2808 bus_data_generation_update();
2813 * @brief Probe a device, and return this status.
2815 * This function is the core of the device autoconfiguration
2816 * system. Its purpose is to select a suitable driver for a device and
2817 * then call that driver to initialise the hardware appropriately. The
2818 * driver is selected by calling the DEVICE_PROBE() method of a set of
2819 * candidate drivers and then choosing the driver which returned the
2820 * best value. This driver is then attached to the device using
2823 * The set of suitable drivers is taken from the list of drivers in
2824 * the parent device's devclass. If the device was originally created
2825 * with a specific class name (see device_add_child()), only drivers
2826 * with that name are probed, otherwise all drivers in the devclass
2827 * are probed. If no drivers return successful probe values in the
2828 * parent devclass, the search continues in the parent of that
2829 * devclass (see devclass_get_parent()) if any.
2831 * @param dev the device to initialise
2834 * @retval ENXIO no driver was found
2835 * @retval ENOMEM memory allocation failure
2836 * @retval non-zero some other unix error code
2837 * @retval -1 Device already attached
2840 device_probe(device_t dev)
2846 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2849 if (!(dev->flags & DF_ENABLED)) {
2850 if (bootverbose && device_get_name(dev) != NULL) {
2851 device_print_prettyname(dev);
2852 printf("not probed (disabled)\n");
2856 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2857 if (bus_current_pass == BUS_PASS_DEFAULT &&
2858 !(dev->flags & DF_DONENOMATCH)) {
2859 BUS_PROBE_NOMATCH(dev->parent, dev);
2861 dev->flags |= DF_DONENOMATCH;
2869 * @brief Probe a device and attach a driver if possible
2871 * calls device_probe() and attaches if that was successful.
2874 device_probe_and_attach(device_t dev)
2880 error = device_probe(dev);
2883 else if (error != 0)
2886 CURVNET_SET_QUIET(vnet0);
2887 error = device_attach(dev);
2893 * @brief Attach a device driver to a device
2895 * This function is a wrapper around the DEVICE_ATTACH() driver
2896 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2897 * device's sysctl tree, optionally prints a description of the device
2898 * and queues a notification event for user-based device management
2901 * Normally this function is only called internally from
2902 * device_probe_and_attach().
2904 * @param dev the device to initialise
2907 * @retval ENXIO no driver was found
2908 * @retval ENOMEM memory allocation failure
2909 * @retval non-zero some other unix error code
2912 device_attach(device_t dev)
2914 uint64_t attachtime;
2915 uint16_t attachentropy;
2918 if (resource_disabled(dev->driver->name, dev->unit)) {
2919 device_disable(dev);
2921 device_printf(dev, "disabled via hints entry\n");
2925 device_sysctl_init(dev);
2926 if (!device_is_quiet(dev))
2927 device_print_child(dev->parent, dev);
2928 attachtime = get_cyclecount();
2929 dev->state = DS_ATTACHING;
2930 if ((error = DEVICE_ATTACH(dev)) != 0) {
2931 printf("device_attach: %s%d attach returned %d\n",
2932 dev->driver->name, dev->unit, error);
2933 if (!(dev->flags & DF_FIXEDCLASS))
2934 devclass_delete_device(dev->devclass, dev);
2935 (void)device_set_driver(dev, NULL);
2936 device_sysctl_fini(dev);
2937 KASSERT(dev->busy == 0, ("attach failed but busy"));
2938 dev->state = DS_NOTPRESENT;
2941 dev->flags |= DF_ATTACHED_ONCE;
2942 /* We only need the low bits of this time, but ranges from tens to thousands
2943 * have been seen, so keep 2 bytes' worth.
2945 attachentropy = (uint16_t)(get_cyclecount() - attachtime);
2946 random_harvest_direct(&attachentropy, sizeof(attachentropy), RANDOM_ATTACH);
2947 device_sysctl_update(dev);
2949 dev->state = DS_BUSY;
2951 dev->state = DS_ATTACHED;
2952 dev->flags &= ~DF_DONENOMATCH;
2953 EVENTHANDLER_DIRECT_INVOKE(device_attach, dev);
2959 * @brief Detach a driver from a device
2961 * This function is a wrapper around the DEVICE_DETACH() driver
2962 * method. If the call to DEVICE_DETACH() succeeds, it calls
2963 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2964 * notification event for user-based device management services and
2965 * cleans up the device's sysctl tree.
2967 * @param dev the device to un-initialise
2970 * @retval ENXIO no driver was found
2971 * @retval ENOMEM memory allocation failure
2972 * @retval non-zero some other unix error code
2975 device_detach(device_t dev)
2981 PDEBUG(("%s", DEVICENAME(dev)));
2982 if (dev->state == DS_BUSY)
2984 if (dev->state == DS_ATTACHING) {
2985 device_printf(dev, "device in attaching state! Deferring detach.\n");
2988 if (dev->state != DS_ATTACHED)
2991 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, EVHDEV_DETACH_BEGIN);
2992 if ((error = DEVICE_DETACH(dev)) != 0) {
2993 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
2994 EVHDEV_DETACH_FAILED);
2997 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
2998 EVHDEV_DETACH_COMPLETE);
3001 if (!device_is_quiet(dev))
3002 device_printf(dev, "detached\n");
3004 BUS_CHILD_DETACHED(dev->parent, dev);
3006 if (!(dev->flags & DF_FIXEDCLASS))
3007 devclass_delete_device(dev->devclass, dev);
3009 device_verbose(dev);
3010 dev->state = DS_NOTPRESENT;
3011 (void)device_set_driver(dev, NULL);
3012 device_sysctl_fini(dev);
3018 * @brief Tells a driver to quiesce itself.
3020 * This function is a wrapper around the DEVICE_QUIESCE() driver
3021 * method. If the call to DEVICE_QUIESCE() succeeds.
3023 * @param dev the device to quiesce
3026 * @retval ENXIO no driver was found
3027 * @retval ENOMEM memory allocation failure
3028 * @retval non-zero some other unix error code
3031 device_quiesce(device_t dev)
3033 PDEBUG(("%s", DEVICENAME(dev)));
3034 if (dev->state == DS_BUSY)
3036 if (dev->state != DS_ATTACHED)
3039 return (DEVICE_QUIESCE(dev));
3043 * @brief Notify a device of system shutdown
3045 * This function calls the DEVICE_SHUTDOWN() driver method if the
3046 * device currently has an attached driver.
3048 * @returns the value returned by DEVICE_SHUTDOWN()
3051 device_shutdown(device_t dev)
3053 if (dev->state < DS_ATTACHED)
3055 return (DEVICE_SHUTDOWN(dev));
3059 * @brief Set the unit number of a device
3061 * This function can be used to override the unit number used for a
3062 * device (e.g. to wire a device to a pre-configured unit number).
3065 device_set_unit(device_t dev, int unit)
3070 dc = device_get_devclass(dev);
3071 if (unit < dc->maxunit && dc->devices[unit])
3073 err = devclass_delete_device(dc, dev);
3077 err = devclass_add_device(dc, dev);
3081 bus_data_generation_update();
3085 /*======================================*/
3087 * Some useful method implementations to make life easier for bus drivers.
3091 resource_init_map_request_impl(struct resource_map_request *args, size_t sz)
3095 args->memattr = VM_MEMATTR_UNCACHEABLE;
3099 * @brief Initialise a resource list.
3101 * @param rl the resource list to initialise
3104 resource_list_init(struct resource_list *rl)
3110 * @brief Reclaim memory used by a resource list.
3112 * This function frees the memory for all resource entries on the list
3115 * @param rl the resource list to free
3118 resource_list_free(struct resource_list *rl)
3120 struct resource_list_entry *rle;
3122 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3124 panic("resource_list_free: resource entry is busy");
3125 STAILQ_REMOVE_HEAD(rl, link);
3131 * @brief Add a resource entry.
3133 * This function adds a resource entry using the given @p type, @p
3134 * start, @p end and @p count values. A rid value is chosen by
3135 * searching sequentially for the first unused rid starting at zero.
3137 * @param rl the resource list to edit
3138 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3139 * @param start the start address of the resource
3140 * @param end the end address of the resource
3141 * @param count XXX end-start+1
3144 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start,
3145 rman_res_t end, rman_res_t count)
3150 while (resource_list_find(rl, type, rid) != NULL)
3152 resource_list_add(rl, type, rid, start, end, count);
3157 * @brief Add or modify a resource entry.
3159 * If an existing entry exists with the same type and rid, it will be
3160 * modified using the given values of @p start, @p end and @p
3161 * count. If no entry exists, a new one will be created using the
3162 * given values. The resource list entry that matches is then returned.
3164 * @param rl the resource list to edit
3165 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3166 * @param rid the resource identifier
3167 * @param start the start address of the resource
3168 * @param end the end address of the resource
3169 * @param count XXX end-start+1
3171 struct resource_list_entry *
3172 resource_list_add(struct resource_list *rl, int type, int rid,
3173 rman_res_t start, rman_res_t end, rman_res_t count)
3175 struct resource_list_entry *rle;
3177 rle = resource_list_find(rl, type, rid);
3179 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3182 panic("resource_list_add: can't record entry");
3183 STAILQ_INSERT_TAIL(rl, rle, link);
3191 panic("resource_list_add: resource entry is busy");
3200 * @brief Determine if a resource entry is busy.
3202 * Returns true if a resource entry is busy meaning that it has an
3203 * associated resource that is not an unallocated "reserved" resource.
3205 * @param rl the resource list to search
3206 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3207 * @param rid the resource identifier
3209 * @returns Non-zero if the entry is busy, zero otherwise.
3212 resource_list_busy(struct resource_list *rl, int type, int rid)
3214 struct resource_list_entry *rle;
3216 rle = resource_list_find(rl, type, rid);
3217 if (rle == NULL || rle->res == NULL)
3219 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3220 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3221 ("reserved resource is active"));
3228 * @brief Determine if a resource entry is reserved.
3230 * Returns true if a resource entry is reserved meaning that it has an
3231 * associated "reserved" resource. The resource can either be
3232 * allocated or unallocated.
3234 * @param rl the resource list to search
3235 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3236 * @param rid the resource identifier
3238 * @returns Non-zero if the entry is reserved, zero otherwise.
3241 resource_list_reserved(struct resource_list *rl, int type, int rid)
3243 struct resource_list_entry *rle;
3245 rle = resource_list_find(rl, type, rid);
3246 if (rle != NULL && rle->flags & RLE_RESERVED)
3252 * @brief Find a resource entry by type and rid.
3254 * @param rl the resource list to search
3255 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3256 * @param rid the resource identifier
3258 * @returns the resource entry pointer or NULL if there is no such
3261 struct resource_list_entry *
3262 resource_list_find(struct resource_list *rl, int type, int rid)
3264 struct resource_list_entry *rle;
3266 STAILQ_FOREACH(rle, rl, link) {
3267 if (rle->type == type && rle->rid == rid)
3274 * @brief Delete a resource entry.
3276 * @param rl the resource list to edit
3277 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3278 * @param rid the resource identifier
3281 resource_list_delete(struct resource_list *rl, int type, int rid)
3283 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3286 if (rle->res != NULL)
3287 panic("resource_list_delete: resource has not been released");
3288 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3294 * @brief Allocate a reserved resource
3296 * This can be used by buses to force the allocation of resources
3297 * that are always active in the system even if they are not allocated
3298 * by a driver (e.g. PCI BARs). This function is usually called when
3299 * adding a new child to the bus. The resource is allocated from the
3300 * parent bus when it is reserved. The resource list entry is marked
3301 * with RLE_RESERVED to note that it is a reserved resource.
3303 * Subsequent attempts to allocate the resource with
3304 * resource_list_alloc() will succeed the first time and will set
3305 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3306 * resource that has been allocated is released with
3307 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3308 * the actual resource remains allocated. The resource can be released to
3309 * the parent bus by calling resource_list_unreserve().
3311 * @param rl the resource list to allocate from
3312 * @param bus the parent device of @p child
3313 * @param child the device for which the resource is being reserved
3314 * @param type the type of resource to allocate
3315 * @param rid a pointer to the resource identifier
3316 * @param start hint at the start of the resource range - pass
3317 * @c 0 for any start address
3318 * @param end hint at the end of the resource range - pass
3319 * @c ~0 for any end address
3320 * @param count hint at the size of range required - pass @c 1
3322 * @param flags any extra flags to control the resource
3323 * allocation - see @c RF_XXX flags in
3324 * <sys/rman.h> for details
3326 * @returns the resource which was allocated or @c NULL if no
3327 * resource could be allocated
3330 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3331 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3333 struct resource_list_entry *rle = NULL;
3334 int passthrough = (device_get_parent(child) != bus);
3339 "resource_list_reserve() should only be called for direct children");
3340 if (flags & RF_ACTIVE)
3342 "resource_list_reserve() should only reserve inactive resources");
3344 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3347 rle = resource_list_find(rl, type, *rid);
3348 rle->flags |= RLE_RESERVED;
3354 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3356 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3357 * and passing the allocation up to the parent of @p bus. This assumes
3358 * that the first entry of @c device_get_ivars(child) is a struct
3359 * resource_list. This also handles 'passthrough' allocations where a
3360 * child is a remote descendant of bus by passing the allocation up to
3361 * the parent of bus.
3363 * Typically, a bus driver would store a list of child resources
3364 * somewhere in the child device's ivars (see device_get_ivars()) and
3365 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3366 * then call resource_list_alloc() to perform the allocation.
3368 * @param rl the resource list to allocate from
3369 * @param bus the parent device of @p child
3370 * @param child the device which is requesting an allocation
3371 * @param type the type of resource to allocate
3372 * @param rid a pointer to the resource identifier
3373 * @param start hint at the start of the resource range - pass
3374 * @c 0 for any start address
3375 * @param end hint at the end of the resource range - pass
3376 * @c ~0 for any end address
3377 * @param count hint at the size of range required - pass @c 1
3379 * @param flags any extra flags to control the resource
3380 * allocation - see @c RF_XXX flags in
3381 * <sys/rman.h> for details
3383 * @returns the resource which was allocated or @c NULL if no
3384 * resource could be allocated
3387 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3388 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3390 struct resource_list_entry *rle = NULL;
3391 int passthrough = (device_get_parent(child) != bus);
3392 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
3395 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3396 type, rid, start, end, count, flags));
3399 rle = resource_list_find(rl, type, *rid);
3402 return (NULL); /* no resource of that type/rid */
3405 if (rle->flags & RLE_RESERVED) {
3406 if (rle->flags & RLE_ALLOCATED)
3408 if ((flags & RF_ACTIVE) &&
3409 bus_activate_resource(child, type, *rid,
3412 rle->flags |= RLE_ALLOCATED;
3416 "resource entry %#x type %d for child %s is busy\n", *rid,
3417 type, device_get_nameunit(child));
3423 count = ulmax(count, rle->count);
3424 end = ulmax(rle->end, start + count - 1);
3427 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3428 type, rid, start, end, count, flags);
3431 * Record the new range.
3434 rle->start = rman_get_start(rle->res);
3435 rle->end = rman_get_end(rle->res);
3443 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3445 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3446 * used with resource_list_alloc().
3448 * @param rl the resource list which was allocated from
3449 * @param bus the parent device of @p child
3450 * @param child the device which is requesting a release
3451 * @param type the type of resource to release
3452 * @param rid the resource identifier
3453 * @param res the resource to release
3456 * @retval non-zero a standard unix error code indicating what
3457 * error condition prevented the operation
3460 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3461 int type, int rid, struct resource *res)
3463 struct resource_list_entry *rle = NULL;
3464 int passthrough = (device_get_parent(child) != bus);
3468 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3472 rle = resource_list_find(rl, type, rid);
3475 panic("resource_list_release: can't find resource");
3477 panic("resource_list_release: resource entry is not busy");
3478 if (rle->flags & RLE_RESERVED) {
3479 if (rle->flags & RLE_ALLOCATED) {
3480 if (rman_get_flags(res) & RF_ACTIVE) {
3481 error = bus_deactivate_resource(child, type,
3486 rle->flags &= ~RLE_ALLOCATED;
3492 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3502 * @brief Release all active resources of a given type
3504 * Release all active resources of a specified type. This is intended
3505 * to be used to cleanup resources leaked by a driver after detach or
3508 * @param rl the resource list which was allocated from
3509 * @param bus the parent device of @p child
3510 * @param child the device whose active resources are being released
3511 * @param type the type of resources to release
3514 * @retval EBUSY at least one resource was active
3517 resource_list_release_active(struct resource_list *rl, device_t bus,
3518 device_t child, int type)
3520 struct resource_list_entry *rle;
3524 STAILQ_FOREACH(rle, rl, link) {
3525 if (rle->type != type)
3527 if (rle->res == NULL)
3529 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3533 error = resource_list_release(rl, bus, child, type,
3534 rman_get_rid(rle->res), rle->res);
3537 "Failed to release active resource: %d\n", error);
3543 * @brief Fully release a reserved resource
3545 * Fully releases a resource reserved via resource_list_reserve().
3547 * @param rl the resource list which was allocated from
3548 * @param bus the parent device of @p child
3549 * @param child the device whose reserved resource is being released
3550 * @param type the type of resource to release
3551 * @param rid the resource identifier
3552 * @param res the resource to release
3555 * @retval non-zero a standard unix error code indicating what
3556 * error condition prevented the operation
3559 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3562 struct resource_list_entry *rle = NULL;
3563 int passthrough = (device_get_parent(child) != bus);
3567 "resource_list_unreserve() should only be called for direct children");
3569 rle = resource_list_find(rl, type, rid);
3572 panic("resource_list_unreserve: can't find resource");
3573 if (!(rle->flags & RLE_RESERVED))
3575 if (rle->flags & RLE_ALLOCATED)
3577 rle->flags &= ~RLE_RESERVED;
3578 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3582 * @brief Print a description of resources in a resource list
3584 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3585 * The name is printed if at least one resource of the given type is available.
3586 * The format is used to print resource start and end.
3588 * @param rl the resource list to print
3589 * @param name the name of @p type, e.g. @c "memory"
3590 * @param type type type of resource entry to print
3591 * @param format printf(9) format string to print resource
3592 * start and end values
3594 * @returns the number of characters printed
3597 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3600 struct resource_list_entry *rle;
3601 int printed, retval;
3605 /* Yes, this is kinda cheating */
3606 STAILQ_FOREACH(rle, rl, link) {
3607 if (rle->type == type) {
3609 retval += printf(" %s ", name);
3611 retval += printf(",");
3613 retval += printf(format, rle->start);
3614 if (rle->count > 1) {
3615 retval += printf("-");
3616 retval += printf(format, rle->start +
3625 * @brief Releases all the resources in a list.
3627 * @param rl The resource list to purge.
3632 resource_list_purge(struct resource_list *rl)
3634 struct resource_list_entry *rle;
3636 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3638 bus_release_resource(rman_get_device(rle->res),
3639 rle->type, rle->rid, rle->res);
3640 STAILQ_REMOVE_HEAD(rl, link);
3646 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3648 return (device_add_child_ordered(dev, order, name, unit));
3652 * @brief Helper function for implementing DEVICE_PROBE()
3654 * This function can be used to help implement the DEVICE_PROBE() for
3655 * a bus (i.e. a device which has other devices attached to it). It
3656 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3660 bus_generic_probe(device_t dev)
3662 devclass_t dc = dev->devclass;
3665 TAILQ_FOREACH(dl, &dc->drivers, link) {
3667 * If this driver's pass is too high, then ignore it.
3668 * For most drivers in the default pass, this will
3669 * never be true. For early-pass drivers they will
3670 * only call the identify routines of eligible drivers
3671 * when this routine is called. Drivers for later
3672 * passes should have their identify routines called
3673 * on early-pass buses during BUS_NEW_PASS().
3675 if (dl->pass > bus_current_pass)
3677 DEVICE_IDENTIFY(dl->driver, dev);
3684 * @brief Helper function for implementing DEVICE_ATTACH()
3686 * This function can be used to help implement the DEVICE_ATTACH() for
3687 * a bus. It calls device_probe_and_attach() for each of the device's
3691 bus_generic_attach(device_t dev)
3695 TAILQ_FOREACH(child, &dev->children, link) {
3696 device_probe_and_attach(child);
3703 * @brief Helper function for delaying attaching children
3705 * Many buses can't run transactions on the bus which children need to probe and
3706 * attach until after interrupts and/or timers are running. This function
3707 * delays their attach until interrupts and timers are enabled.
3710 bus_delayed_attach_children(device_t dev)
3712 /* Probe and attach the bus children when interrupts are available */
3713 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
3719 * @brief Helper function for implementing DEVICE_DETACH()
3721 * This function can be used to help implement the DEVICE_DETACH() for
3722 * a bus. It calls device_detach() for each of the device's
3726 bus_generic_detach(device_t dev)
3731 if (dev->state != DS_ATTACHED)
3735 * Detach children in the reverse order.
3736 * See bus_generic_suspend for details.
3738 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3739 if ((error = device_detach(child)) != 0)
3747 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3749 * This function can be used to help implement the DEVICE_SHUTDOWN()
3750 * for a bus. It calls device_shutdown() for each of the device's
3754 bus_generic_shutdown(device_t dev)
3759 * Shut down children in the reverse order.
3760 * See bus_generic_suspend for details.
3762 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3763 device_shutdown(child);
3770 * @brief Default function for suspending a child device.
3772 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3775 bus_generic_suspend_child(device_t dev, device_t child)
3779 error = DEVICE_SUSPEND(child);
3782 child->flags |= DF_SUSPENDED;
3788 * @brief Default function for resuming a child device.
3790 * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3793 bus_generic_resume_child(device_t dev, device_t child)
3795 DEVICE_RESUME(child);
3796 child->flags &= ~DF_SUSPENDED;
3802 * @brief Helper function for implementing DEVICE_SUSPEND()
3804 * This function can be used to help implement the DEVICE_SUSPEND()
3805 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3806 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3807 * operation is aborted and any devices which were suspended are
3808 * resumed immediately by calling their DEVICE_RESUME() methods.
3811 bus_generic_suspend(device_t dev)
3817 * Suspend children in the reverse order.
3818 * For most buses all children are equal, so the order does not matter.
3819 * Other buses, such as acpi, carefully order their child devices to
3820 * express implicit dependencies between them. For such buses it is
3821 * safer to bring down devices in the reverse order.
3823 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3824 error = BUS_SUSPEND_CHILD(dev, child);
3826 child = TAILQ_NEXT(child, link);
3827 if (child != NULL) {
3828 TAILQ_FOREACH_FROM(child, &dev->children, link)
3829 BUS_RESUME_CHILD(dev, child);
3838 * @brief Helper function for implementing DEVICE_RESUME()
3840 * This function can be used to help implement the DEVICE_RESUME() for
3841 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3844 bus_generic_resume(device_t dev)
3848 TAILQ_FOREACH(child, &dev->children, link) {
3849 BUS_RESUME_CHILD(dev, child);
3850 /* if resume fails, there's nothing we can usefully do... */
3856 * @brief Helper function for implementing BUS_RESET_POST
3858 * Bus can use this function to implement common operations of
3859 * re-attaching or resuming the children after the bus itself was
3860 * reset, and after restoring bus-unique state of children.
3862 * @param dev The bus
3863 * #param flags DEVF_RESET_*
3866 bus_helper_reset_post(device_t dev, int flags)
3872 TAILQ_FOREACH(child, &dev->children,link) {
3873 BUS_RESET_POST(dev, child);
3874 error1 = (flags & DEVF_RESET_DETACH) != 0 ?
3875 device_probe_and_attach(child) :
3876 BUS_RESUME_CHILD(dev, child);
3877 if (error == 0 && error1 != 0)
3884 bus_helper_reset_prepare_rollback(device_t dev, device_t child, int flags)
3886 child = TAILQ_NEXT(child, link);
3889 TAILQ_FOREACH_FROM(child, &dev->children,link) {
3890 BUS_RESET_POST(dev, child);
3891 if ((flags & DEVF_RESET_DETACH) != 0)
3892 device_probe_and_attach(child);
3894 BUS_RESUME_CHILD(dev, child);
3899 * @brief Helper function for implementing BUS_RESET_PREPARE
3901 * Bus can use this function to implement common operations of
3902 * detaching or suspending the children before the bus itself is
3903 * reset, and then save bus-unique state of children that must
3904 * persists around reset.
3906 * @param dev The bus
3907 * #param flags DEVF_RESET_*
3910 bus_helper_reset_prepare(device_t dev, int flags)
3915 if (dev->state != DS_ATTACHED)
3918 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3919 if ((flags & DEVF_RESET_DETACH) != 0) {
3920 error = device_get_state(child) == DS_ATTACHED ?
3921 device_detach(child) : 0;
3923 error = BUS_SUSPEND_CHILD(dev, child);
3926 error = BUS_RESET_PREPARE(dev, child);
3928 if ((flags & DEVF_RESET_DETACH) != 0)
3929 device_probe_and_attach(child);
3931 BUS_RESUME_CHILD(dev, child);
3935 bus_helper_reset_prepare_rollback(dev, child, flags);
3943 * @brief Helper function for implementing BUS_PRINT_CHILD().
3945 * This function prints the first part of the ascii representation of
3946 * @p child, including its name, unit and description (if any - see
3947 * device_set_desc()).
3949 * @returns the number of characters printed
3952 bus_print_child_header(device_t dev, device_t child)
3956 if (device_get_desc(child)) {
3957 retval += device_printf(child, "<%s>", device_get_desc(child));
3959 retval += printf("%s", device_get_nameunit(child));
3966 * @brief Helper function for implementing BUS_PRINT_CHILD().
3968 * This function prints the last part of the ascii representation of
3969 * @p child, which consists of the string @c " on " followed by the
3970 * name and unit of the @p dev.
3972 * @returns the number of characters printed
3975 bus_print_child_footer(device_t dev, device_t child)
3977 return (printf(" on %s\n", device_get_nameunit(dev)));
3981 * @brief Helper function for implementing BUS_PRINT_CHILD().
3983 * This function prints out the VM domain for the given device.
3985 * @returns the number of characters printed
3988 bus_print_child_domain(device_t dev, device_t child)
3992 /* No domain? Don't print anything */
3993 if (BUS_GET_DOMAIN(dev, child, &domain) != 0)
3996 return (printf(" numa-domain %d", domain));
4000 * @brief Helper function for implementing BUS_PRINT_CHILD().
4002 * This function simply calls bus_print_child_header() followed by
4003 * bus_print_child_footer().
4005 * @returns the number of characters printed
4008 bus_generic_print_child(device_t dev, device_t child)
4012 retval += bus_print_child_header(dev, child);
4013 retval += bus_print_child_domain(dev, child);
4014 retval += bus_print_child_footer(dev, child);
4020 * @brief Stub function for implementing BUS_READ_IVAR().
4025 bus_generic_read_ivar(device_t dev, device_t child, int index,
4032 * @brief Stub function for implementing BUS_WRITE_IVAR().
4037 bus_generic_write_ivar(device_t dev, device_t child, int index,
4044 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
4048 struct resource_list *
4049 bus_generic_get_resource_list(device_t dev, device_t child)
4055 * @brief Helper function for implementing BUS_DRIVER_ADDED().
4057 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
4058 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
4059 * and then calls device_probe_and_attach() for each unattached child.
4062 bus_generic_driver_added(device_t dev, driver_t *driver)
4066 DEVICE_IDENTIFY(driver, dev);
4067 TAILQ_FOREACH(child, &dev->children, link) {
4068 if (child->state == DS_NOTPRESENT ||
4069 (child->flags & DF_REBID))
4070 device_probe_and_attach(child);
4075 * @brief Helper function for implementing BUS_NEW_PASS().
4077 * This implementing of BUS_NEW_PASS() first calls the identify
4078 * routines for any drivers that probe at the current pass. Then it
4079 * walks the list of devices for this bus. If a device is already
4080 * attached, then it calls BUS_NEW_PASS() on that device. If the
4081 * device is not already attached, it attempts to attach a driver to
4085 bus_generic_new_pass(device_t dev)
4092 TAILQ_FOREACH(dl, &dc->drivers, link) {
4093 if (dl->pass == bus_current_pass)
4094 DEVICE_IDENTIFY(dl->driver, dev);
4096 TAILQ_FOREACH(child, &dev->children, link) {
4097 if (child->state >= DS_ATTACHED)
4098 BUS_NEW_PASS(child);
4099 else if (child->state == DS_NOTPRESENT)
4100 device_probe_and_attach(child);
4105 * @brief Helper function for implementing BUS_SETUP_INTR().
4107 * This simple implementation of BUS_SETUP_INTR() simply calls the
4108 * BUS_SETUP_INTR() method of the parent of @p dev.
4111 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
4112 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
4115 /* Propagate up the bus hierarchy until someone handles it. */
4117 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
4118 filter, intr, arg, cookiep));
4123 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
4125 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
4126 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
4129 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
4132 /* Propagate up the bus hierarchy until someone handles it. */
4134 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
4139 * @brief Helper function for implementing BUS_SUSPEND_INTR().
4141 * This simple implementation of BUS_SUSPEND_INTR() simply calls the
4142 * BUS_SUSPEND_INTR() method of the parent of @p dev.
4145 bus_generic_suspend_intr(device_t dev, device_t child, struct resource *irq)
4147 /* Propagate up the bus hierarchy until someone handles it. */
4149 return (BUS_SUSPEND_INTR(dev->parent, child, irq));
4154 * @brief Helper function for implementing BUS_RESUME_INTR().
4156 * This simple implementation of BUS_RESUME_INTR() simply calls the
4157 * BUS_RESUME_INTR() method of the parent of @p dev.
4160 bus_generic_resume_intr(device_t dev, device_t child, struct resource *irq)
4162 /* Propagate up the bus hierarchy until someone handles it. */
4164 return (BUS_RESUME_INTR(dev->parent, child, irq));
4169 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
4171 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
4172 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
4175 bus_generic_adjust_resource(device_t dev, device_t child, int type,
4176 struct resource *r, rman_res_t start, rman_res_t end)
4178 /* Propagate up the bus hierarchy until someone handles it. */
4180 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
4186 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4188 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
4189 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
4192 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
4193 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4195 /* Propagate up the bus hierarchy until someone handles it. */
4197 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
4198 start, end, count, flags));
4203 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4205 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
4206 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
4209 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
4212 /* Propagate up the bus hierarchy until someone handles it. */
4214 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
4220 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
4222 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
4223 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
4226 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
4229 /* Propagate up the bus hierarchy until someone handles it. */
4231 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
4237 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
4239 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
4240 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
4243 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
4244 int rid, struct resource *r)
4246 /* Propagate up the bus hierarchy until someone handles it. */
4248 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
4254 * @brief Helper function for implementing BUS_MAP_RESOURCE().
4256 * This simple implementation of BUS_MAP_RESOURCE() simply calls the
4257 * BUS_MAP_RESOURCE() method of the parent of @p dev.
4260 bus_generic_map_resource(device_t dev, device_t child, int type,
4261 struct resource *r, struct resource_map_request *args,
4262 struct resource_map *map)
4264 /* Propagate up the bus hierarchy until someone handles it. */
4266 return (BUS_MAP_RESOURCE(dev->parent, child, type, r, args,
4272 * @brief Helper function for implementing BUS_UNMAP_RESOURCE().
4274 * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the
4275 * BUS_UNMAP_RESOURCE() method of the parent of @p dev.
4278 bus_generic_unmap_resource(device_t dev, device_t child, int type,
4279 struct resource *r, struct resource_map *map)
4281 /* Propagate up the bus hierarchy until someone handles it. */
4283 return (BUS_UNMAP_RESOURCE(dev->parent, child, type, r, map));
4288 * @brief Helper function for implementing BUS_BIND_INTR().
4290 * This simple implementation of BUS_BIND_INTR() simply calls the
4291 * BUS_BIND_INTR() method of the parent of @p dev.
4294 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
4297 /* Propagate up the bus hierarchy until someone handles it. */
4299 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
4304 * @brief Helper function for implementing BUS_CONFIG_INTR().
4306 * This simple implementation of BUS_CONFIG_INTR() simply calls the
4307 * BUS_CONFIG_INTR() method of the parent of @p dev.
4310 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
4311 enum intr_polarity pol)
4313 /* Propagate up the bus hierarchy until someone handles it. */
4315 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
4320 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
4322 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
4323 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
4326 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
4327 void *cookie, const char *descr)
4329 /* Propagate up the bus hierarchy until someone handles it. */
4331 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
4337 * @brief Helper function for implementing BUS_GET_CPUS().
4339 * This simple implementation of BUS_GET_CPUS() simply calls the
4340 * BUS_GET_CPUS() method of the parent of @p dev.
4343 bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op,
4344 size_t setsize, cpuset_t *cpuset)
4346 /* Propagate up the bus hierarchy until someone handles it. */
4347 if (dev->parent != NULL)
4348 return (BUS_GET_CPUS(dev->parent, child, op, setsize, cpuset));
4353 * @brief Helper function for implementing BUS_GET_DMA_TAG().
4355 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
4356 * BUS_GET_DMA_TAG() method of the parent of @p dev.
4359 bus_generic_get_dma_tag(device_t dev, device_t child)
4361 /* Propagate up the bus hierarchy until someone handles it. */
4362 if (dev->parent != NULL)
4363 return (BUS_GET_DMA_TAG(dev->parent, child));
4368 * @brief Helper function for implementing BUS_GET_BUS_TAG().
4370 * This simple implementation of BUS_GET_BUS_TAG() simply calls the
4371 * BUS_GET_BUS_TAG() method of the parent of @p dev.
4374 bus_generic_get_bus_tag(device_t dev, device_t child)
4376 /* Propagate up the bus hierarchy until someone handles it. */
4377 if (dev->parent != NULL)
4378 return (BUS_GET_BUS_TAG(dev->parent, child));
4379 return ((bus_space_tag_t)0);
4383 * @brief Helper function for implementing BUS_GET_RESOURCE().
4385 * This implementation of BUS_GET_RESOURCE() uses the
4386 * resource_list_find() function to do most of the work. It calls
4387 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4391 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4392 rman_res_t *startp, rman_res_t *countp)
4394 struct resource_list * rl = NULL;
4395 struct resource_list_entry * rle = NULL;
4397 rl = BUS_GET_RESOURCE_LIST(dev, child);
4401 rle = resource_list_find(rl, type, rid);
4406 *startp = rle->start;
4408 *countp = rle->count;
4414 * @brief Helper function for implementing BUS_SET_RESOURCE().
4416 * This implementation of BUS_SET_RESOURCE() uses the
4417 * resource_list_add() function to do most of the work. It calls
4418 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4422 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4423 rman_res_t start, rman_res_t count)
4425 struct resource_list * rl = NULL;
4427 rl = BUS_GET_RESOURCE_LIST(dev, child);
4431 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4437 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4439 * This implementation of BUS_DELETE_RESOURCE() uses the
4440 * resource_list_delete() function to do most of the work. It calls
4441 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4445 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4447 struct resource_list * rl = NULL;
4449 rl = BUS_GET_RESOURCE_LIST(dev, child);
4453 resource_list_delete(rl, type, rid);
4459 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4461 * This implementation of BUS_RELEASE_RESOURCE() uses the
4462 * resource_list_release() function to do most of the work. It calls
4463 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4466 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4467 int rid, struct resource *r)
4469 struct resource_list * rl = NULL;
4471 if (device_get_parent(child) != dev)
4472 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4475 rl = BUS_GET_RESOURCE_LIST(dev, child);
4479 return (resource_list_release(rl, dev, child, type, rid, r));
4483 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4485 * This implementation of BUS_ALLOC_RESOURCE() uses the
4486 * resource_list_alloc() function to do most of the work. It calls
4487 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4490 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4491 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4493 struct resource_list * rl = NULL;
4495 if (device_get_parent(child) != dev)
4496 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4497 type, rid, start, end, count, flags));
4499 rl = BUS_GET_RESOURCE_LIST(dev, child);
4503 return (resource_list_alloc(rl, dev, child, type, rid,
4504 start, end, count, flags));
4508 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4510 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4511 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4514 bus_generic_child_present(device_t dev, device_t child)
4516 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4520 bus_generic_get_domain(device_t dev, device_t child, int *domain)
4523 return (BUS_GET_DOMAIN(dev->parent, dev, domain));
4529 * @brief Helper function for implementing BUS_RESCAN().
4531 * This null implementation of BUS_RESCAN() always fails to indicate
4532 * the bus does not support rescanning.
4535 bus_null_rescan(device_t dev)
4541 * Some convenience functions to make it easier for drivers to use the
4542 * resource-management functions. All these really do is hide the
4543 * indirection through the parent's method table, making for slightly
4544 * less-wordy code. In the future, it might make sense for this code
4545 * to maintain some sort of a list of resources allocated by each device.
4549 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4550 struct resource **res)
4554 for (i = 0; rs[i].type != -1; i++)
4556 for (i = 0; rs[i].type != -1; i++) {
4557 res[i] = bus_alloc_resource_any(dev,
4558 rs[i].type, &rs[i].rid, rs[i].flags);
4559 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4560 bus_release_resources(dev, rs, res);
4568 bus_release_resources(device_t dev, const struct resource_spec *rs,
4569 struct resource **res)
4573 for (i = 0; rs[i].type != -1; i++)
4574 if (res[i] != NULL) {
4575 bus_release_resource(
4576 dev, rs[i].type, rs[i].rid, res[i]);
4582 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4584 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4588 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
4589 rman_res_t end, rman_res_t count, u_int flags)
4591 struct resource *res;
4593 if (dev->parent == NULL)
4595 res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4601 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4603 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4607 bus_adjust_resource(device_t dev, int type, struct resource *r, rman_res_t start,
4610 if (dev->parent == NULL)
4612 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4616 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4618 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4622 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4624 if (dev->parent == NULL)
4626 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4630 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4632 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4636 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4638 if (dev->parent == NULL)
4640 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4644 * @brief Wrapper function for BUS_MAP_RESOURCE().
4646 * This function simply calls the BUS_MAP_RESOURCE() method of the
4650 bus_map_resource(device_t dev, int type, struct resource *r,
4651 struct resource_map_request *args, struct resource_map *map)
4653 if (dev->parent == NULL)
4655 return (BUS_MAP_RESOURCE(dev->parent, dev, type, r, args, map));
4659 * @brief Wrapper function for BUS_UNMAP_RESOURCE().
4661 * This function simply calls the BUS_UNMAP_RESOURCE() method of the
4665 bus_unmap_resource(device_t dev, int type, struct resource *r,
4666 struct resource_map *map)
4668 if (dev->parent == NULL)
4670 return (BUS_UNMAP_RESOURCE(dev->parent, dev, type, r, map));
4674 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4676 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4680 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4684 if (dev->parent == NULL)
4686 rv = BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r);
4691 * @brief Wrapper function for BUS_SETUP_INTR().
4693 * This function simply calls the BUS_SETUP_INTR() method of the
4697 bus_setup_intr(device_t dev, struct resource *r, int flags,
4698 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4702 if (dev->parent == NULL)
4704 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4708 if (handler != NULL && !(flags & INTR_MPSAFE))
4709 device_printf(dev, "[GIANT-LOCKED]\n");
4714 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4716 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4720 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4722 if (dev->parent == NULL)
4724 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4728 * @brief Wrapper function for BUS_SUSPEND_INTR().
4730 * This function simply calls the BUS_SUSPEND_INTR() method of the
4734 bus_suspend_intr(device_t dev, struct resource *r)
4736 if (dev->parent == NULL)
4738 return (BUS_SUSPEND_INTR(dev->parent, dev, r));
4742 * @brief Wrapper function for BUS_RESUME_INTR().
4744 * This function simply calls the BUS_RESUME_INTR() method of the
4748 bus_resume_intr(device_t dev, struct resource *r)
4750 if (dev->parent == NULL)
4752 return (BUS_RESUME_INTR(dev->parent, dev, r));
4756 * @brief Wrapper function for BUS_BIND_INTR().
4758 * This function simply calls the BUS_BIND_INTR() method of the
4762 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4764 if (dev->parent == NULL)
4766 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4770 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4772 * This function first formats the requested description into a
4773 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4774 * the parent of @p dev.
4777 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4778 const char *fmt, ...)
4781 char descr[MAXCOMLEN + 1];
4783 if (dev->parent == NULL)
4786 vsnprintf(descr, sizeof(descr), fmt, ap);
4788 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4792 * @brief Wrapper function for BUS_SET_RESOURCE().
4794 * This function simply calls the BUS_SET_RESOURCE() method of the
4798 bus_set_resource(device_t dev, int type, int rid,
4799 rman_res_t start, rman_res_t count)
4801 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4806 * @brief Wrapper function for BUS_GET_RESOURCE().
4808 * This function simply calls the BUS_GET_RESOURCE() method of the
4812 bus_get_resource(device_t dev, int type, int rid,
4813 rman_res_t *startp, rman_res_t *countp)
4815 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4820 * @brief Wrapper function for BUS_GET_RESOURCE().
4822 * This function simply calls the BUS_GET_RESOURCE() method of the
4823 * parent of @p dev and returns the start value.
4826 bus_get_resource_start(device_t dev, int type, int rid)
4832 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4840 * @brief Wrapper function for BUS_GET_RESOURCE().
4842 * This function simply calls the BUS_GET_RESOURCE() method of the
4843 * parent of @p dev and returns the count value.
4846 bus_get_resource_count(device_t dev, int type, int rid)
4852 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4860 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4862 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4866 bus_delete_resource(device_t dev, int type, int rid)
4868 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4872 * @brief Wrapper function for BUS_CHILD_PRESENT().
4874 * This function simply calls the BUS_CHILD_PRESENT() method of the
4878 bus_child_present(device_t child)
4880 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4884 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4886 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4890 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4894 parent = device_get_parent(child);
4895 if (parent == NULL) {
4899 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4903 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4905 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4909 bus_child_location_str(device_t child, char *buf, size_t buflen)
4913 parent = device_get_parent(child);
4914 if (parent == NULL) {
4918 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4922 * @brief Wrapper function for bus_child_pnpinfo_str using sbuf
4924 * A convenient wrapper frunction for bus_child_pnpinfo_str that allows
4925 * us to splat that into an sbuf. It uses unholy knowledge of sbuf to
4926 * accomplish this, however. It is an interim function until we can convert
4927 * this interface more fully.
4929 /* Note: we reach inside of sbuf because it's API isn't rich enough to do this */
4930 #define SPACE(s) ((s)->s_size - (s)->s_len)
4931 #define EOB(s) ((s)->s_buf + (s)->s_len)
4934 bus_child_pnpinfo_sb(device_t dev, struct sbuf *sb)
4939 MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0);
4940 if (sb->s_error != 0)
4943 *p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
4946 sb->s_error = ENOMEM;
4949 bus_child_pnpinfo_str(dev, p, space);
4950 sb->s_len += strlen(p);
4955 * @brief Wrapper function for bus_child_pnpinfo_str using sbuf
4957 * A convenient wrapper frunction for bus_child_pnpinfo_str that allows
4958 * us to splat that into an sbuf. It uses unholy knowledge of sbuf to
4959 * accomplish this, however. It is an interim function until we can convert
4960 * this interface more fully.
4963 bus_child_location_sb(device_t dev, struct sbuf *sb)
4968 MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0);
4969 if (sb->s_error != 0)
4972 *p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
4975 sb->s_error = ENOMEM;
4978 bus_child_location_str(dev, p, space);
4979 sb->s_len += strlen(p);
4986 * @brief Wrapper function for BUS_GET_CPUS().
4988 * This function simply calls the BUS_GET_CPUS() method of the
4992 bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, cpuset_t *cpuset)
4996 parent = device_get_parent(dev);
4999 return (BUS_GET_CPUS(parent, dev, op, setsize, cpuset));
5003 * @brief Wrapper function for BUS_GET_DMA_TAG().
5005 * This function simply calls the BUS_GET_DMA_TAG() method of the
5009 bus_get_dma_tag(device_t dev)
5013 parent = device_get_parent(dev);
5016 return (BUS_GET_DMA_TAG(parent, dev));
5020 * @brief Wrapper function for BUS_GET_BUS_TAG().
5022 * This function simply calls the BUS_GET_BUS_TAG() method of the
5026 bus_get_bus_tag(device_t dev)
5030 parent = device_get_parent(dev);
5032 return ((bus_space_tag_t)0);
5033 return (BUS_GET_BUS_TAG(parent, dev));
5037 * @brief Wrapper function for BUS_GET_DOMAIN().
5039 * This function simply calls the BUS_GET_DOMAIN() method of the
5043 bus_get_domain(device_t dev, int *domain)
5045 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain));
5048 /* Resume all devices and then notify userland that we're up again. */
5050 root_resume(device_t dev)
5054 error = bus_generic_resume(dev);
5056 devctl_notify("kern", "power", "resume", NULL); /* Deprecated gone in 14 */
5057 devctl_notify("kernel", "power", "resume", NULL);
5063 root_print_child(device_t dev, device_t child)
5067 retval += bus_print_child_header(dev, child);
5068 retval += printf("\n");
5074 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
5075 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
5078 * If an interrupt mapping gets to here something bad has happened.
5080 panic("root_setup_intr");
5084 * If we get here, assume that the device is permanent and really is
5085 * present in the system. Removable bus drivers are expected to intercept
5086 * this call long before it gets here. We return -1 so that drivers that
5087 * really care can check vs -1 or some ERRNO returned higher in the food
5091 root_child_present(device_t dev, device_t child)
5097 root_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
5102 /* Default to returning the set of all CPUs. */
5103 if (setsize != sizeof(cpuset_t))
5112 static kobj_method_t root_methods[] = {
5113 /* Device interface */
5114 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
5115 KOBJMETHOD(device_suspend, bus_generic_suspend),
5116 KOBJMETHOD(device_resume, root_resume),
5119 KOBJMETHOD(bus_print_child, root_print_child),
5120 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
5121 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
5122 KOBJMETHOD(bus_setup_intr, root_setup_intr),
5123 KOBJMETHOD(bus_child_present, root_child_present),
5124 KOBJMETHOD(bus_get_cpus, root_get_cpus),
5129 static driver_t root_driver = {
5136 devclass_t root_devclass;
5139 root_bus_module_handler(module_t mod, int what, void* arg)
5143 TAILQ_INIT(&bus_data_devices);
5144 kobj_class_compile((kobj_class_t) &root_driver);
5145 root_bus = make_device(NULL, "root", 0);
5146 root_bus->desc = "System root bus";
5147 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
5148 root_bus->driver = &root_driver;
5149 root_bus->state = DS_ATTACHED;
5150 root_devclass = devclass_find_internal("root", NULL, FALSE);
5155 device_shutdown(root_bus);
5158 return (EOPNOTSUPP);
5164 static moduledata_t root_bus_mod = {
5166 root_bus_module_handler,
5169 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
5172 * @brief Automatically configure devices
5174 * This function begins the autoconfiguration process by calling
5175 * device_probe_and_attach() for each child of the @c root0 device.
5178 root_bus_configure(void)
5182 /* Eventually this will be split up, but this is sufficient for now. */
5183 bus_set_pass(BUS_PASS_DEFAULT);
5187 * @brief Module handler for registering device drivers
5189 * This module handler is used to automatically register device
5190 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
5191 * devclass_add_driver() for the driver described by the
5192 * driver_module_data structure pointed to by @p arg
5195 driver_module_handler(module_t mod, int what, void *arg)
5197 struct driver_module_data *dmd;
5198 devclass_t bus_devclass;
5199 kobj_class_t driver;
5202 dmd = (struct driver_module_data *)arg;
5203 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
5208 if (dmd->dmd_chainevh)
5209 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5211 pass = dmd->dmd_pass;
5212 driver = dmd->dmd_driver;
5213 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
5214 DRIVERNAME(driver), dmd->dmd_busname, pass));
5215 error = devclass_add_driver(bus_devclass, driver, pass,
5220 PDEBUG(("Unloading module: driver %s from bus %s",
5221 DRIVERNAME(dmd->dmd_driver),
5223 error = devclass_delete_driver(bus_devclass,
5226 if (!error && dmd->dmd_chainevh)
5227 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5230 PDEBUG(("Quiesce module: driver %s from bus %s",
5231 DRIVERNAME(dmd->dmd_driver),
5233 error = devclass_quiesce_driver(bus_devclass,
5236 if (!error && dmd->dmd_chainevh)
5237 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5248 * @brief Enumerate all hinted devices for this bus.
5250 * Walks through the hints for this bus and calls the bus_hinted_child
5251 * routine for each one it fines. It searches first for the specific
5252 * bus that's being probed for hinted children (eg isa0), and then for
5253 * generic children (eg isa).
5255 * @param dev bus device to enumerate
5258 bus_enumerate_hinted_children(device_t bus)
5261 const char *dname, *busname;
5265 * enumerate all devices on the specific bus
5267 busname = device_get_nameunit(bus);
5269 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5270 BUS_HINTED_CHILD(bus, dname, dunit);
5273 * and all the generic ones.
5275 busname = device_get_name(bus);
5277 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5278 BUS_HINTED_CHILD(bus, dname, dunit);
5283 /* the _short versions avoid iteration by not calling anything that prints
5284 * more than oneliners. I love oneliners.
5288 print_device_short(device_t dev, int indent)
5293 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
5294 dev->unit, dev->desc,
5295 (dev->parent? "":"no "),
5296 (TAILQ_EMPTY(&dev->children)? "no ":""),
5297 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
5298 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
5299 (dev->flags&DF_WILDCARD? "wildcard,":""),
5300 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
5301 (dev->flags&DF_REBID? "rebiddable,":""),
5302 (dev->flags&DF_SUSPENDED? "suspended,":""),
5303 (dev->ivars? "":"no "),
5304 (dev->softc? "":"no "),
5309 print_device(device_t dev, int indent)
5314 print_device_short(dev, indent);
5316 indentprintf(("Parent:\n"));
5317 print_device_short(dev->parent, indent+1);
5318 indentprintf(("Driver:\n"));
5319 print_driver_short(dev->driver, indent+1);
5320 indentprintf(("Devclass:\n"));
5321 print_devclass_short(dev->devclass, indent+1);
5325 print_device_tree_short(device_t dev, int indent)
5326 /* print the device and all its children (indented) */
5333 print_device_short(dev, indent);
5335 TAILQ_FOREACH(child, &dev->children, link) {
5336 print_device_tree_short(child, indent+1);
5341 print_device_tree(device_t dev, int indent)
5342 /* print the device and all its children (indented) */
5349 print_device(dev, indent);
5351 TAILQ_FOREACH(child, &dev->children, link) {
5352 print_device_tree(child, indent+1);
5357 print_driver_short(driver_t *driver, int indent)
5362 indentprintf(("driver %s: softc size = %zd\n",
5363 driver->name, driver->size));
5367 print_driver(driver_t *driver, int indent)
5372 print_driver_short(driver, indent);
5376 print_driver_list(driver_list_t drivers, int indent)
5378 driverlink_t driver;
5380 TAILQ_FOREACH(driver, &drivers, link) {
5381 print_driver(driver->driver, indent);
5386 print_devclass_short(devclass_t dc, int indent)
5391 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
5395 print_devclass(devclass_t dc, int indent)
5402 print_devclass_short(dc, indent);
5403 indentprintf(("Drivers:\n"));
5404 print_driver_list(dc->drivers, indent+1);
5406 indentprintf(("Devices:\n"));
5407 for (i = 0; i < dc->maxunit; i++)
5409 print_device(dc->devices[i], indent+1);
5413 print_devclass_list_short(void)
5417 printf("Short listing of devclasses, drivers & devices:\n");
5418 TAILQ_FOREACH(dc, &devclasses, link) {
5419 print_devclass_short(dc, 0);
5424 print_devclass_list(void)
5428 printf("Full listing of devclasses, drivers & devices:\n");
5429 TAILQ_FOREACH(dc, &devclasses, link) {
5430 print_devclass(dc, 0);
5437 * User-space access to the device tree.
5439 * We implement a small set of nodes:
5441 * hw.bus Single integer read method to obtain the
5442 * current generation count.
5443 * hw.bus.devices Reads the entire device tree in flat space.
5444 * hw.bus.rman Resource manager interface
5446 * We might like to add the ability to scan devclasses and/or drivers to
5447 * determine what else is currently loaded/available.
5451 sysctl_bus_info(SYSCTL_HANDLER_ARGS)
5453 struct u_businfo ubus;
5455 ubus.ub_version = BUS_USER_VERSION;
5456 ubus.ub_generation = bus_data_generation;
5458 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
5460 SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD |
5461 CTLFLAG_MPSAFE, NULL, 0, sysctl_bus_info, "S,u_businfo",
5462 "bus-related data");
5465 sysctl_devices(SYSCTL_HANDLER_ARGS)
5467 int *name = (int *)arg1;
5468 u_int namelen = arg2;
5471 struct u_device *udev;
5478 if (bus_data_generation_check(name[0]))
5484 * Scan the list of devices, looking for the requested index.
5486 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5494 * Populate the return item, careful not to overflow the buffer.
5496 udev = malloc(sizeof(*udev), M_BUS, M_WAITOK | M_ZERO);
5499 udev->dv_handle = (uintptr_t)dev;
5500 udev->dv_parent = (uintptr_t)dev->parent;
5501 udev->dv_devflags = dev->devflags;
5502 udev->dv_flags = dev->flags;
5503 udev->dv_state = dev->state;
5504 walker = udev->dv_fields;
5505 ep = walker + sizeof(udev->dv_fields);
5507 if ((src) == NULL) \
5510 strlcpy(walker, (src), ep - walker); \
5511 walker += strlen(walker) + 1; \
5519 CP(dev->driver != NULL ? dev->driver->name : NULL);
5520 bus_child_pnpinfo_str(dev, walker, ep - walker);
5521 walker += strlen(walker) + 1;
5524 bus_child_location_str(dev, walker, ep - walker);
5525 walker += strlen(walker) + 1;
5531 error = SYSCTL_OUT(req, udev, sizeof(*udev));
5536 SYSCTL_NODE(_hw_bus, OID_AUTO, devices,
5537 CTLFLAG_RD | CTLFLAG_NEEDGIANT, sysctl_devices,
5538 "system device tree");
5541 bus_data_generation_check(int generation)
5543 if (generation != bus_data_generation)
5546 /* XXX generate optimised lists here? */
5551 bus_data_generation_update(void)
5553 atomic_add_int(&bus_data_generation, 1);
5557 bus_free_resource(device_t dev, int type, struct resource *r)
5561 return (bus_release_resource(dev, type, rman_get_rid(r), r));
5565 device_lookup_by_name(const char *name)
5569 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5570 if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0)
5577 * /dev/devctl2 implementation. The existing /dev/devctl device has
5578 * implicit semantics on open, so it could not be reused for this.
5579 * Another option would be to call this /dev/bus?
5582 find_device(struct devreq *req, device_t *devp)
5587 * First, ensure that the name is nul terminated.
5589 if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL)
5593 * Second, try to find an attached device whose name matches
5596 dev = device_lookup_by_name(req->dr_name);
5602 /* Finally, give device enumerators a chance. */
5604 EVENTHANDLER_DIRECT_INVOKE(dev_lookup, req->dr_name, &dev);
5612 driver_exists(device_t bus, const char *driver)
5616 for (dc = bus->devclass; dc != NULL; dc = dc->parent) {
5617 if (devclass_find_driver_internal(dc, driver) != NULL)
5624 device_gen_nomatch(device_t dev)
5628 if (dev->flags & DF_NEEDNOMATCH &&
5629 dev->state == DS_NOTPRESENT) {
5630 BUS_PROBE_NOMATCH(dev->parent, dev);
5632 dev->flags |= DF_DONENOMATCH;
5634 dev->flags &= ~DF_NEEDNOMATCH;
5635 TAILQ_FOREACH(child, &dev->children, link) {
5636 device_gen_nomatch(child);
5641 device_do_deferred_actions(void)
5647 * Walk through the devclasses to find all the drivers we've tagged as
5648 * deferred during the freeze and call the driver added routines. They
5649 * have already been added to the lists in the background, so the driver
5650 * added routines that trigger a probe will have all the right bidders
5651 * for the probe auction.
5653 TAILQ_FOREACH(dc, &devclasses, link) {
5654 TAILQ_FOREACH(dl, &dc->drivers, link) {
5655 if (dl->flags & DL_DEFERRED_PROBE) {
5656 devclass_driver_added(dc, dl->driver);
5657 dl->flags &= ~DL_DEFERRED_PROBE;
5663 * We also defer no-match events during a freeze. Walk the tree and
5664 * generate all the pent-up events that are still relevant.
5666 device_gen_nomatch(root_bus);
5667 bus_data_generation_update();
5671 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
5678 /* Locate the device to control. */
5680 req = (struct devreq *)data;
5688 case DEV_SET_DRIVER:
5689 case DEV_CLEAR_DRIVER:
5693 error = priv_check(td, PRIV_DRIVER);
5695 error = find_device(req, &dev);
5699 error = priv_check(td, PRIV_DRIVER);
5710 /* Perform the requested operation. */
5713 if (device_is_attached(dev) && (dev->flags & DF_REBID) == 0)
5715 else if (!device_is_enabled(dev))
5718 error = device_probe_and_attach(dev);
5721 if (!device_is_attached(dev)) {
5725 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5726 error = device_quiesce(dev);
5730 error = device_detach(dev);
5733 if (device_is_enabled(dev)) {
5739 * If the device has been probed but not attached (e.g.
5740 * when it has been disabled by a loader hint), just
5741 * attach the device rather than doing a full probe.
5744 if (device_is_alive(dev)) {
5746 * If the device was disabled via a hint, clear
5749 if (resource_disabled(dev->driver->name, dev->unit))
5750 resource_unset_value(dev->driver->name,
5751 dev->unit, "disabled");
5752 error = device_attach(dev);
5754 error = device_probe_and_attach(dev);
5757 if (!device_is_enabled(dev)) {
5762 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5763 error = device_quiesce(dev);
5769 * Force DF_FIXEDCLASS on around detach to preserve
5770 * the existing name.
5773 dev->flags |= DF_FIXEDCLASS;
5774 error = device_detach(dev);
5775 if (!(old & DF_FIXEDCLASS))
5776 dev->flags &= ~DF_FIXEDCLASS;
5778 device_disable(dev);
5781 if (device_is_suspended(dev)) {
5785 if (device_get_parent(dev) == NULL) {
5789 error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev);
5792 if (!device_is_suspended(dev)) {
5796 if (device_get_parent(dev) == NULL) {
5800 error = BUS_RESUME_CHILD(device_get_parent(dev), dev);
5802 case DEV_SET_DRIVER: {
5806 error = copyinstr(req->dr_data, driver, sizeof(driver), NULL);
5809 if (driver[0] == '\0') {
5813 if (dev->devclass != NULL &&
5814 strcmp(driver, dev->devclass->name) == 0)
5815 /* XXX: Could possibly force DF_FIXEDCLASS on? */
5819 * Scan drivers for this device's bus looking for at
5820 * least one matching driver.
5822 if (dev->parent == NULL) {
5826 if (!driver_exists(dev->parent, driver)) {
5830 dc = devclass_create(driver);
5836 /* Detach device if necessary. */
5837 if (device_is_attached(dev)) {
5838 if (req->dr_flags & DEVF_SET_DRIVER_DETACH)
5839 error = device_detach(dev);
5846 /* Clear any previously-fixed device class and unit. */
5847 if (dev->flags & DF_FIXEDCLASS)
5848 devclass_delete_device(dev->devclass, dev);
5849 dev->flags |= DF_WILDCARD;
5852 /* Force the new device class. */
5853 error = devclass_add_device(dc, dev);
5856 dev->flags |= DF_FIXEDCLASS;
5857 error = device_probe_and_attach(dev);
5860 case DEV_CLEAR_DRIVER:
5861 if (!(dev->flags & DF_FIXEDCLASS)) {
5865 if (device_is_attached(dev)) {
5866 if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH)
5867 error = device_detach(dev);
5874 dev->flags &= ~DF_FIXEDCLASS;
5875 dev->flags |= DF_WILDCARD;
5876 devclass_delete_device(dev->devclass, dev);
5877 error = device_probe_and_attach(dev);
5880 if (!device_is_attached(dev)) {
5884 error = BUS_RESCAN(dev);
5889 parent = device_get_parent(dev);
5890 if (parent == NULL) {
5894 if (!(req->dr_flags & DEVF_FORCE_DELETE)) {
5895 if (bus_child_present(dev) != 0) {
5901 error = device_delete_child(parent, dev);
5908 device_frozen = true;
5914 device_do_deferred_actions();
5915 device_frozen = false;
5919 if ((req->dr_flags & ~(DEVF_RESET_DETACH)) != 0) {
5923 error = BUS_RESET_CHILD(device_get_parent(dev), dev,
5931 static struct cdevsw devctl2_cdevsw = {
5932 .d_version = D_VERSION,
5933 .d_ioctl = devctl2_ioctl,
5934 .d_name = "devctl2",
5940 make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL,
5941 UID_ROOT, GID_WHEEL, 0600, "devctl2");
5945 * APIs to manage deprecation and obsolescence.
5947 static int obsolete_panic = 0;
5948 SYSCTL_INT(_debug, OID_AUTO, obsolete_panic, CTLFLAG_RWTUN, &obsolete_panic, 0,
5949 "Panic when obsolete features are used (0 = never, 1 = if osbolete, "
5950 "2 = if deprecated)");
5953 gone_panic(int major, int running, const char *msg)
5955 switch (obsolete_panic)
5960 if (running < major)
5969 _gone_in(int major, const char *msg)
5971 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
5972 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
5973 printf("Obsolete code will be removed soon: %s\n", msg);
5975 printf("Deprecated code (to be removed in FreeBSD %d): %s\n",
5980 _gone_in_dev(device_t dev, int major, const char *msg)
5982 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
5983 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
5985 "Obsolete code will be removed soon: %s\n", msg);
5988 "Deprecated code (to be removed in FreeBSD %d): %s\n",
5993 DB_SHOW_COMMAND(device, db_show_device)
6000 dev = (device_t)addr;
6002 db_printf("name: %s\n", device_get_nameunit(dev));
6003 db_printf(" driver: %s\n", DRIVERNAME(dev->driver));
6004 db_printf(" class: %s\n", DEVCLANAME(dev->devclass));
6005 db_printf(" addr: %p\n", dev);
6006 db_printf(" parent: %p\n", dev->parent);
6007 db_printf(" softc: %p\n", dev->softc);
6008 db_printf(" ivars: %p\n", dev->ivars);
6011 DB_SHOW_ALL_COMMAND(devices, db_show_all_devices)
6015 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
6016 db_show_device((db_expr_t)dev, true, count, modif);