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_f(const char *system, const char *subsystem, const char *type,
655 const char *data, int flags __unused)
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 devctl_notify(const char *system, const char *subsystem, const char *type,
686 devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
690 * Common routine that tries to make sending messages as easy as possible.
691 * We allocate memory for the data, copy strings into that, but do not
692 * free it unless there's an error. The dequeue part of the driver should
693 * free the data. We don't send data when the device is disabled. We do
694 * send data, even when we have no listeners, because we wish to avoid
695 * races relating to startup and restart of listening applications.
697 * devaddq is designed to string together the type of event, with the
698 * object of that event, plus the plug and play info and location info
699 * for that event. This is likely most useful for devices, but less
700 * useful for other consumers of this interface. Those should use
701 * the devctl_notify() interface instead.
704 * ${type}${what} at $(location dev) $(pnp-info dev) on $(parent dev)
707 devaddq(const char *type, const char *what, device_t dev)
709 struct dev_event_info *dei;
713 dei = devctl_alloc_dei_sb(&sb);
718 sbuf_cat(&sb, " at ");
720 /* Add in the location */
721 bus_child_location_sb(dev, &sb);
725 bus_child_pnpinfo_sb(dev, &sb);
727 /* Get the parent of this device, or / if high enough in the tree. */
728 if (device_get_parent(dev) == NULL)
729 parstr = "."; /* Or '/' ? */
731 parstr = device_get_nameunit(device_get_parent(dev));
732 sbuf_cat(&sb, " on ");
733 sbuf_cat(&sb, parstr);
734 sbuf_putc(&sb, '\n');
735 if (sbuf_finish(&sb) != 0)
740 devctl_free_dei(dei);
744 * A device was added to the tree. We are called just after it successfully
745 * attaches (that is, probe and attach success for this device). No call
746 * is made if a device is merely parented into the tree. See devnomatch
747 * if probe fails. If attach fails, no notification is sent (but maybe
748 * we should have a different message for this).
751 devadded(device_t dev)
753 devaddq("+", device_get_nameunit(dev), dev);
757 * A device was removed from the tree. We are called just before this
761 devremoved(device_t dev)
763 devaddq("-", device_get_nameunit(dev), dev);
767 * Called when there's no match for this device. This is only called
768 * the first time that no match happens, so we don't keep getting this
769 * message. Should that prove to be undesirable, we can change it.
770 * This is called when all drivers that can attach to a given bus
771 * decline to accept this device. Other errors may not be detected.
774 devnomatch(device_t dev)
776 devaddq("?", "", dev);
780 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
784 q = devctl_queue_length;
785 error = sysctl_handle_int(oidp, &q, 0, req);
786 if (error || !req->newptr)
792 * When set as a tunable, we've not yet initialized the mutex.
793 * It is safe to just assign to devctl_queue_length and return
794 * as we're racing no one. We'll use whatever value set in
797 if (!mtx_initialized(&devsoftc.mtx)) {
798 devctl_queue_length = q;
803 * XXX It's hard to grow or shrink the UMA zone. Only allow
804 * disabling the queue size for the moment until underlying
805 * UMA issues can be sorted out.
809 if (q == devctl_queue_length)
811 mtx_lock(&devsoftc.mtx);
812 devctl_queue_length = 0;
813 uma_zdestroy(devsoftc.zone);
815 mtx_unlock(&devsoftc.mtx);
820 * @brief safely quotes strings that might have double quotes in them.
822 * The devctl protocol relies on quoted strings having matching quotes.
823 * This routine quotes any internal quotes so the resulting string
824 * is safe to pass to snprintf to construct, for example pnp info strings.
826 * @param sb sbuf to place the characters into
827 * @param src Original buffer.
830 devctl_safe_quote_sb(struct sbuf *sb, const char *src)
832 while (*src != '\0') {
833 if (*src == '"' || *src == '\\')
835 sbuf_putc(sb, *src++);
839 /* End of /dev/devctl code */
841 static TAILQ_HEAD(,device) bus_data_devices;
842 static int bus_data_generation = 1;
844 static kobj_method_t null_methods[] = {
848 DEFINE_CLASS(null, null_methods, 0);
851 * Bus pass implementation
854 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
855 int bus_current_pass = BUS_PASS_ROOT;
859 * @brief Register the pass level of a new driver attachment
861 * Register a new driver attachment's pass level. If no driver
862 * attachment with the same pass level has been added, then @p new
863 * will be added to the global passes list.
865 * @param new the new driver attachment
868 driver_register_pass(struct driverlink *new)
870 struct driverlink *dl;
872 /* We only consider pass numbers during boot. */
873 if (bus_current_pass == BUS_PASS_DEFAULT)
877 * Walk the passes list. If we already know about this pass
878 * then there is nothing to do. If we don't, then insert this
879 * driver link into the list.
881 TAILQ_FOREACH(dl, &passes, passlink) {
882 if (dl->pass < new->pass)
884 if (dl->pass == new->pass)
886 TAILQ_INSERT_BEFORE(dl, new, passlink);
889 TAILQ_INSERT_TAIL(&passes, new, passlink);
893 * @brief Raise the current bus pass
895 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
896 * method on the root bus to kick off a new device tree scan for each
897 * new pass level that has at least one driver.
900 bus_set_pass(int pass)
902 struct driverlink *dl;
904 if (bus_current_pass > pass)
905 panic("Attempt to lower bus pass level");
907 TAILQ_FOREACH(dl, &passes, passlink) {
908 /* Skip pass values below the current pass level. */
909 if (dl->pass <= bus_current_pass)
913 * Bail once we hit a driver with a pass level that is
920 * Raise the pass level to the next level and rescan
923 bus_current_pass = dl->pass;
924 BUS_NEW_PASS(root_bus);
928 * If there isn't a driver registered for the requested pass,
929 * then bus_current_pass might still be less than 'pass'. Set
930 * it to 'pass' in that case.
932 if (bus_current_pass < pass)
933 bus_current_pass = pass;
934 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
938 * Devclass implementation
941 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
945 * @brief Find or create a device class
947 * If a device class with the name @p classname exists, return it,
948 * otherwise if @p create is non-zero create and return a new device
951 * If @p parentname is non-NULL, the parent of the devclass is set to
952 * the devclass of that name.
954 * @param classname the devclass name to find or create
955 * @param parentname the parent devclass name or @c NULL
956 * @param create non-zero to create a devclass
959 devclass_find_internal(const char *classname, const char *parentname,
964 PDEBUG(("looking for %s", classname));
968 TAILQ_FOREACH(dc, &devclasses, link) {
969 if (!strcmp(dc->name, classname))
974 PDEBUG(("creating %s", classname));
975 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
976 M_BUS, M_NOWAIT | M_ZERO);
980 dc->name = (char*) (dc + 1);
981 strcpy(dc->name, classname);
982 TAILQ_INIT(&dc->drivers);
983 TAILQ_INSERT_TAIL(&devclasses, dc, link);
985 bus_data_generation_update();
989 * If a parent class is specified, then set that as our parent so
990 * that this devclass will support drivers for the parent class as
991 * well. If the parent class has the same name don't do this though
992 * as it creates a cycle that can trigger an infinite loop in
993 * device_probe_child() if a device exists for which there is no
996 if (parentname && dc && !dc->parent &&
997 strcmp(classname, parentname) != 0) {
998 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
999 dc->parent->flags |= DC_HAS_CHILDREN;
1006 * @brief Create a device class
1008 * If a device class with the name @p classname exists, return it,
1009 * otherwise create and return a new device class.
1011 * @param classname the devclass name to find or create
1014 devclass_create(const char *classname)
1016 return (devclass_find_internal(classname, NULL, TRUE));
1020 * @brief Find a device class
1022 * If a device class with the name @p classname exists, return it,
1023 * otherwise return @c NULL.
1025 * @param classname the devclass name to find
1028 devclass_find(const char *classname)
1030 return (devclass_find_internal(classname, NULL, FALSE));
1034 * @brief Register that a device driver has been added to a devclass
1036 * Register that a device driver has been added to a devclass. This
1037 * is called by devclass_add_driver to accomplish the recursive
1038 * notification of all the children classes of dc, as well as dc.
1039 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1042 * We do a full search here of the devclass list at each iteration
1043 * level to save storing children-lists in the devclass structure. If
1044 * we ever move beyond a few dozen devices doing this, we may need to
1047 * @param dc the devclass to edit
1048 * @param driver the driver that was just added
1051 devclass_driver_added(devclass_t dc, driver_t *driver)
1057 * Call BUS_DRIVER_ADDED for any existing buses in this class.
1059 for (i = 0; i < dc->maxunit; i++)
1060 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1061 BUS_DRIVER_ADDED(dc->devices[i], driver);
1064 * Walk through the children classes. Since we only keep a
1065 * single parent pointer around, we walk the entire list of
1066 * devclasses looking for children. We set the
1067 * DC_HAS_CHILDREN flag when a child devclass is created on
1068 * the parent, so we only walk the list for those devclasses
1069 * that have children.
1071 if (!(dc->flags & DC_HAS_CHILDREN))
1074 TAILQ_FOREACH(dc, &devclasses, link) {
1075 if (dc->parent == parent)
1076 devclass_driver_added(dc, driver);
1081 * @brief Add a device driver to a device class
1083 * Add a device driver to a devclass. This is normally called
1084 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1085 * all devices in the devclass will be called to allow them to attempt
1086 * to re-probe any unmatched children.
1088 * @param dc the devclass to edit
1089 * @param driver the driver to register
1092 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1095 const char *parentname;
1097 PDEBUG(("%s", DRIVERNAME(driver)));
1099 /* Don't allow invalid pass values. */
1100 if (pass <= BUS_PASS_ROOT)
1103 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1108 * Compile the driver's methods. Also increase the reference count
1109 * so that the class doesn't get freed when the last instance
1110 * goes. This means we can safely use static methods and avoids a
1111 * double-free in devclass_delete_driver.
1113 kobj_class_compile((kobj_class_t) driver);
1116 * If the driver has any base classes, make the
1117 * devclass inherit from the devclass of the driver's
1118 * first base class. This will allow the system to
1119 * search for drivers in both devclasses for children
1120 * of a device using this driver.
1122 if (driver->baseclasses)
1123 parentname = driver->baseclasses[0]->name;
1126 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1128 dl->driver = driver;
1129 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1130 driver->refs++; /* XXX: kobj_mtx */
1132 driver_register_pass(dl);
1134 if (device_frozen) {
1135 dl->flags |= DL_DEFERRED_PROBE;
1137 devclass_driver_added(dc, driver);
1139 bus_data_generation_update();
1144 * @brief Register that a device driver has been deleted from a devclass
1146 * Register that a device driver has been removed from a devclass.
1147 * This is called by devclass_delete_driver to accomplish the
1148 * recursive notification of all the children classes of busclass, as
1149 * well as busclass. Each layer will attempt to detach the driver
1150 * from any devices that are children of the bus's devclass. The function
1151 * will return an error if a device fails to detach.
1153 * We do a full search here of the devclass list at each iteration
1154 * level to save storing children-lists in the devclass structure. If
1155 * we ever move beyond a few dozen devices doing this, we may need to
1158 * @param busclass the devclass of the parent bus
1159 * @param dc the devclass of the driver being deleted
1160 * @param driver the driver being deleted
1163 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1170 * Disassociate from any devices. We iterate through all the
1171 * devices in the devclass of the driver and detach any which are
1172 * using the driver and which have a parent in the devclass which
1173 * we are deleting from.
1175 * Note that since a driver can be in multiple devclasses, we
1176 * should not detach devices which are not children of devices in
1177 * the affected devclass.
1179 * If we're frozen, we don't generate NOMATCH events. Mark to
1182 for (i = 0; i < dc->maxunit; i++) {
1183 if (dc->devices[i]) {
1184 dev = dc->devices[i];
1185 if (dev->driver == driver && dev->parent &&
1186 dev->parent->devclass == busclass) {
1187 if ((error = device_detach(dev)) != 0)
1189 if (device_frozen) {
1190 dev->flags &= ~DF_DONENOMATCH;
1191 dev->flags |= DF_NEEDNOMATCH;
1193 BUS_PROBE_NOMATCH(dev->parent, dev);
1195 dev->flags |= DF_DONENOMATCH;
1202 * Walk through the children classes. Since we only keep a
1203 * single parent pointer around, we walk the entire list of
1204 * devclasses looking for children. We set the
1205 * DC_HAS_CHILDREN flag when a child devclass is created on
1206 * the parent, so we only walk the list for those devclasses
1207 * that have children.
1209 if (!(busclass->flags & DC_HAS_CHILDREN))
1212 TAILQ_FOREACH(busclass, &devclasses, link) {
1213 if (busclass->parent == parent) {
1214 error = devclass_driver_deleted(busclass, dc, driver);
1223 * @brief Delete a device driver from a device class
1225 * Delete a device driver from a devclass. This is normally called
1226 * automatically by DRIVER_MODULE().
1228 * If the driver is currently attached to any devices,
1229 * devclass_delete_driver() will first attempt to detach from each
1230 * device. If one of the detach calls fails, the driver will not be
1233 * @param dc the devclass to edit
1234 * @param driver the driver to unregister
1237 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1239 devclass_t dc = devclass_find(driver->name);
1243 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1249 * Find the link structure in the bus' list of drivers.
1251 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1252 if (dl->driver == driver)
1257 PDEBUG(("%s not found in %s list", driver->name,
1262 error = devclass_driver_deleted(busclass, dc, driver);
1266 TAILQ_REMOVE(&busclass->drivers, dl, link);
1271 if (driver->refs == 0)
1272 kobj_class_free((kobj_class_t) driver);
1274 bus_data_generation_update();
1279 * @brief Quiesces a set of device drivers from a device class
1281 * Quiesce a device driver from a devclass. This is normally called
1282 * automatically by DRIVER_MODULE().
1284 * If the driver is currently attached to any devices,
1285 * devclass_quiesece_driver() will first attempt to quiesce each
1288 * @param dc the devclass to edit
1289 * @param driver the driver to unregister
1292 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1294 devclass_t dc = devclass_find(driver->name);
1300 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1306 * Find the link structure in the bus' list of drivers.
1308 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1309 if (dl->driver == driver)
1314 PDEBUG(("%s not found in %s list", driver->name,
1320 * Quiesce all devices. We iterate through all the devices in
1321 * the devclass of the driver and quiesce any which are using
1322 * the driver and which have a parent in the devclass which we
1325 * Note that since a driver can be in multiple devclasses, we
1326 * should not quiesce devices which are not children of
1327 * devices in the affected devclass.
1329 for (i = 0; i < dc->maxunit; i++) {
1330 if (dc->devices[i]) {
1331 dev = dc->devices[i];
1332 if (dev->driver == driver && dev->parent &&
1333 dev->parent->devclass == busclass) {
1334 if ((error = device_quiesce(dev)) != 0)
1347 devclass_find_driver_internal(devclass_t dc, const char *classname)
1351 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1353 TAILQ_FOREACH(dl, &dc->drivers, link) {
1354 if (!strcmp(dl->driver->name, classname))
1358 PDEBUG(("not found"));
1363 * @brief Return the name of the devclass
1366 devclass_get_name(devclass_t dc)
1372 * @brief Find a device given a unit number
1374 * @param dc the devclass to search
1375 * @param unit the unit number to search for
1377 * @returns the device with the given unit number or @c
1378 * NULL if there is no such device
1381 devclass_get_device(devclass_t dc, int unit)
1383 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1385 return (dc->devices[unit]);
1389 * @brief Find the softc field of a device given a unit number
1391 * @param dc the devclass to search
1392 * @param unit the unit number to search for
1394 * @returns the softc field of the device with the given
1395 * unit number or @c NULL if there is no such
1399 devclass_get_softc(devclass_t dc, int unit)
1403 dev = devclass_get_device(dc, unit);
1407 return (device_get_softc(dev));
1411 * @brief Get a list of devices in the devclass
1413 * An array containing a list of all the devices in the given devclass
1414 * is allocated and returned in @p *devlistp. The number of devices
1415 * in the array is returned in @p *devcountp. The caller should free
1416 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1418 * @param dc the devclass to examine
1419 * @param devlistp points at location for array pointer return
1421 * @param devcountp points at location for array size return value
1424 * @retval ENOMEM the array allocation failed
1427 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1432 count = devclass_get_count(dc);
1433 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1438 for (i = 0; i < dc->maxunit; i++) {
1439 if (dc->devices[i]) {
1440 list[count] = dc->devices[i];
1452 * @brief Get a list of drivers in the devclass
1454 * An array containing a list of pointers to all the drivers in the
1455 * given devclass is allocated and returned in @p *listp. The number
1456 * of drivers in the array is returned in @p *countp. The caller should
1457 * free the array using @c free(p, M_TEMP).
1459 * @param dc the devclass to examine
1460 * @param listp gives location for array pointer return value
1461 * @param countp gives location for number of array elements
1465 * @retval ENOMEM the array allocation failed
1468 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1475 TAILQ_FOREACH(dl, &dc->drivers, link)
1477 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1482 TAILQ_FOREACH(dl, &dc->drivers, link) {
1483 list[count] = dl->driver;
1493 * @brief Get the number of devices in a devclass
1495 * @param dc the devclass to examine
1498 devclass_get_count(devclass_t dc)
1503 for (i = 0; i < dc->maxunit; i++)
1510 * @brief Get the maximum unit number used in a devclass
1512 * Note that this is one greater than the highest currently-allocated
1513 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1514 * that not even the devclass has been allocated yet.
1516 * @param dc the devclass to examine
1519 devclass_get_maxunit(devclass_t dc)
1523 return (dc->maxunit);
1527 * @brief Find a free unit number in a devclass
1529 * This function searches for the first unused unit number greater
1530 * that or equal to @p unit.
1532 * @param dc the devclass to examine
1533 * @param unit the first unit number to check
1536 devclass_find_free_unit(devclass_t dc, int unit)
1540 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1546 * @brief Set the parent of a devclass
1548 * The parent class is normally initialised automatically by
1551 * @param dc the devclass to edit
1552 * @param pdc the new parent devclass
1555 devclass_set_parent(devclass_t dc, devclass_t pdc)
1561 * @brief Get the parent of a devclass
1563 * @param dc the devclass to examine
1566 devclass_get_parent(devclass_t dc)
1568 return (dc->parent);
1571 struct sysctl_ctx_list *
1572 devclass_get_sysctl_ctx(devclass_t dc)
1574 return (&dc->sysctl_ctx);
1578 devclass_get_sysctl_tree(devclass_t dc)
1580 return (dc->sysctl_tree);
1585 * @brief Allocate a unit number
1587 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1588 * will do). The allocated unit number is returned in @p *unitp.
1590 * @param dc the devclass to allocate from
1591 * @param unitp points at the location for the allocated unit
1595 * @retval EEXIST the requested unit number is already allocated
1596 * @retval ENOMEM memory allocation failure
1599 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1604 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1606 /* Ask the parent bus if it wants to wire this device. */
1608 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1611 /* If we were given a wired unit number, check for existing device */
1614 if (unit >= 0 && unit < dc->maxunit &&
1615 dc->devices[unit] != NULL) {
1617 printf("%s: %s%d already exists; skipping it\n",
1618 dc->name, dc->name, *unitp);
1622 /* Unwired device, find the next available slot for it */
1624 for (unit = 0;; unit++) {
1625 /* If there is an "at" hint for a unit then skip it. */
1626 if (resource_string_value(dc->name, unit, "at", &s) ==
1630 /* If this device slot is already in use, skip it. */
1631 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1639 * We've selected a unit beyond the length of the table, so let's
1640 * extend the table to make room for all units up to and including
1643 if (unit >= dc->maxunit) {
1644 device_t *newlist, *oldlist;
1647 oldlist = dc->devices;
1648 newsize = roundup((unit + 1),
1649 MAX(1, MINALLOCSIZE / sizeof(device_t)));
1650 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1653 if (oldlist != NULL)
1654 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1655 bzero(newlist + dc->maxunit,
1656 sizeof(device_t) * (newsize - dc->maxunit));
1657 dc->devices = newlist;
1658 dc->maxunit = newsize;
1659 if (oldlist != NULL)
1660 free(oldlist, M_BUS);
1662 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1670 * @brief Add a device to a devclass
1672 * A unit number is allocated for the device (using the device's
1673 * preferred unit number if any) and the device is registered in the
1674 * devclass. This allows the device to be looked up by its unit
1675 * number, e.g. by decoding a dev_t minor number.
1677 * @param dc the devclass to add to
1678 * @param dev the device to add
1681 * @retval EEXIST the requested unit number is already allocated
1682 * @retval ENOMEM memory allocation failure
1685 devclass_add_device(devclass_t dc, device_t dev)
1689 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1691 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1694 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1698 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1699 free(dev->nameunit, M_BUS);
1700 dev->nameunit = NULL;
1703 dc->devices[dev->unit] = dev;
1705 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1712 * @brief Delete a device from a devclass
1714 * The device is removed from the devclass's device list and its unit
1717 * @param dc the devclass to delete from
1718 * @param dev the device to delete
1723 devclass_delete_device(devclass_t dc, device_t dev)
1728 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1730 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1731 panic("devclass_delete_device: inconsistent device class");
1732 dc->devices[dev->unit] = NULL;
1733 if (dev->flags & DF_WILDCARD)
1735 dev->devclass = NULL;
1736 free(dev->nameunit, M_BUS);
1737 dev->nameunit = NULL;
1744 * @brief Make a new device and add it as a child of @p parent
1746 * @param parent the parent of the new device
1747 * @param name the devclass name of the new device or @c NULL
1748 * to leave the devclass unspecified
1749 * @parem unit the unit number of the new device of @c -1 to
1750 * leave the unit number unspecified
1752 * @returns the new device
1755 make_device(device_t parent, const char *name, int unit)
1760 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1763 dc = devclass_find_internal(name, NULL, TRUE);
1765 printf("make_device: can't find device class %s\n",
1773 dev = malloc(sizeof(*dev), M_BUS, M_NOWAIT|M_ZERO);
1777 dev->parent = parent;
1778 TAILQ_INIT(&dev->children);
1779 kobj_init((kobj_t) dev, &null_class);
1781 dev->devclass = NULL;
1783 dev->nameunit = NULL;
1787 dev->flags = DF_ENABLED;
1790 dev->flags |= DF_WILDCARD;
1792 dev->flags |= DF_FIXEDCLASS;
1793 if (devclass_add_device(dc, dev)) {
1794 kobj_delete((kobj_t) dev, M_BUS);
1798 if (parent != NULL && device_has_quiet_children(parent))
1799 dev->flags |= DF_QUIET | DF_QUIET_CHILDREN;
1803 dev->state = DS_NOTPRESENT;
1805 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1806 bus_data_generation_update();
1813 * @brief Print a description of a device.
1816 device_print_child(device_t dev, device_t child)
1820 if (device_is_alive(child))
1821 retval += BUS_PRINT_CHILD(dev, child);
1823 retval += device_printf(child, " not found\n");
1829 * @brief Create a new device
1831 * This creates a new device and adds it as a child of an existing
1832 * parent device. The new device will be added after the last existing
1833 * child with order zero.
1835 * @param dev the device which will be the parent of the
1837 * @param name devclass name for new device or @c NULL if not
1839 * @param unit unit number for new device or @c -1 if not
1842 * @returns the new device
1845 device_add_child(device_t dev, const char *name, int unit)
1847 return (device_add_child_ordered(dev, 0, name, unit));
1851 * @brief Create a new device
1853 * This creates a new device and adds it as a child of an existing
1854 * parent device. The new device will be added after the last existing
1855 * child with the same order.
1857 * @param dev the device which will be the parent of the
1859 * @param order a value which is used to partially sort the
1860 * children of @p dev - devices created using
1861 * lower values of @p order appear first in @p
1862 * dev's list of children
1863 * @param name devclass name for new device or @c NULL if not
1865 * @param unit unit number for new device or @c -1 if not
1868 * @returns the new device
1871 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1876 PDEBUG(("%s at %s with order %u as unit %d",
1877 name, DEVICENAME(dev), order, unit));
1878 KASSERT(name != NULL || unit == -1,
1879 ("child device with wildcard name and specific unit number"));
1881 child = make_device(dev, name, unit);
1884 child->order = order;
1886 TAILQ_FOREACH(place, &dev->children, link) {
1887 if (place->order > order)
1893 * The device 'place' is the first device whose order is
1894 * greater than the new child.
1896 TAILQ_INSERT_BEFORE(place, child, link);
1899 * The new child's order is greater or equal to the order of
1900 * any existing device. Add the child to the tail of the list.
1902 TAILQ_INSERT_TAIL(&dev->children, child, link);
1905 bus_data_generation_update();
1910 * @brief Delete a device
1912 * This function deletes a device along with all of its children. If
1913 * the device currently has a driver attached to it, the device is
1914 * detached first using device_detach().
1916 * @param dev the parent device
1917 * @param child the device to delete
1920 * @retval non-zero a unit error code describing the error
1923 device_delete_child(device_t dev, device_t child)
1926 device_t grandchild;
1928 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1930 /* detach parent before deleting children, if any */
1931 if ((error = device_detach(child)) != 0)
1934 /* remove children second */
1935 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1936 error = device_delete_child(child, grandchild);
1941 if (child->devclass)
1942 devclass_delete_device(child->devclass, child);
1944 BUS_CHILD_DELETED(dev, child);
1945 TAILQ_REMOVE(&dev->children, child, link);
1946 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1947 kobj_delete((kobj_t) child, M_BUS);
1949 bus_data_generation_update();
1954 * @brief Delete all children devices of the given device, if any.
1956 * This function deletes all children devices of the given device, if
1957 * any, using the device_delete_child() function for each device it
1958 * finds. If a child device cannot be deleted, this function will
1959 * return an error code.
1961 * @param dev the parent device
1964 * @retval non-zero a device would not detach
1967 device_delete_children(device_t dev)
1972 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1976 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1977 error = device_delete_child(dev, child);
1979 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1987 * @brief Find a device given a unit number
1989 * This is similar to devclass_get_devices() but only searches for
1990 * devices which have @p dev as a parent.
1992 * @param dev the parent device to search
1993 * @param unit the unit number to search for. If the unit is -1,
1994 * return the first child of @p dev which has name
1995 * @p classname (that is, the one with the lowest unit.)
1997 * @returns the device with the given unit number or @c
1998 * NULL if there is no such device
2001 device_find_child(device_t dev, const char *classname, int unit)
2006 dc = devclass_find(classname);
2011 child = devclass_get_device(dc, unit);
2012 if (child && child->parent == dev)
2015 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
2016 child = devclass_get_device(dc, unit);
2017 if (child && child->parent == dev)
2028 first_matching_driver(devclass_t dc, device_t dev)
2031 return (devclass_find_driver_internal(dc, dev->devclass->name));
2032 return (TAILQ_FIRST(&dc->drivers));
2039 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
2041 if (dev->devclass) {
2043 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
2044 if (!strcmp(dev->devclass->name, dl->driver->name))
2048 return (TAILQ_NEXT(last, link));
2055 device_probe_child(device_t dev, device_t child)
2058 driverlink_t best = NULL;
2060 int result, pri = 0;
2061 int hasclass = (child->devclass != NULL);
2067 panic("device_probe_child: parent device has no devclass");
2070 * If the state is already probed, then return. However, don't
2071 * return if we can rebid this object.
2073 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2076 for (; dc; dc = dc->parent) {
2077 for (dl = first_matching_driver(dc, child);
2079 dl = next_matching_driver(dc, child, dl)) {
2080 /* If this driver's pass is too high, then ignore it. */
2081 if (dl->pass > bus_current_pass)
2084 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2085 result = device_set_driver(child, dl->driver);
2086 if (result == ENOMEM)
2088 else if (result != 0)
2091 if (device_set_devclass(child,
2092 dl->driver->name) != 0) {
2093 char const * devname =
2094 device_get_name(child);
2095 if (devname == NULL)
2096 devname = "(unknown)";
2097 printf("driver bug: Unable to set "
2098 "devclass (class: %s "
2102 (void)device_set_driver(child, NULL);
2107 /* Fetch any flags for the device before probing. */
2108 resource_int_value(dl->driver->name, child->unit,
2109 "flags", &child->devflags);
2111 result = DEVICE_PROBE(child);
2113 /* Reset flags and devclass before the next probe. */
2114 child->devflags = 0;
2116 (void)device_set_devclass(child, NULL);
2119 * If the driver returns SUCCESS, there can be
2120 * no higher match for this device.
2129 * Reset DF_QUIET in case this driver doesn't
2130 * end up as the best driver.
2132 device_verbose(child);
2135 * Probes that return BUS_PROBE_NOWILDCARD or lower
2136 * only match on devices whose driver was explicitly
2139 if (result <= BUS_PROBE_NOWILDCARD &&
2140 !(child->flags & DF_FIXEDCLASS)) {
2145 * The driver returned an error so it
2146 * certainly doesn't match.
2149 (void)device_set_driver(child, NULL);
2154 * A priority lower than SUCCESS, remember the
2155 * best matching driver. Initialise the value
2156 * of pri for the first match.
2158 if (best == NULL || result > pri) {
2165 * If we have an unambiguous match in this devclass,
2166 * don't look in the parent.
2168 if (best && pri == 0)
2173 * If we found a driver, change state and initialise the devclass.
2175 /* XXX What happens if we rebid and got no best? */
2178 * If this device was attached, and we were asked to
2179 * rescan, and it is a different driver, then we have
2180 * to detach the old driver and reattach this new one.
2181 * Note, we don't have to check for DF_REBID here
2182 * because if the state is > DS_ALIVE, we know it must
2185 * This assumes that all DF_REBID drivers can have
2186 * their probe routine called at any time and that
2187 * they are idempotent as well as completely benign in
2188 * normal operations.
2190 * We also have to make sure that the detach
2191 * succeeded, otherwise we fail the operation (or
2192 * maybe it should just fail silently? I'm torn).
2194 if (child->state > DS_ALIVE && best->driver != child->driver)
2195 if ((result = device_detach(dev)) != 0)
2198 /* Set the winning driver, devclass, and flags. */
2199 if (!child->devclass) {
2200 result = device_set_devclass(child, best->driver->name);
2204 result = device_set_driver(child, best->driver);
2207 resource_int_value(best->driver->name, child->unit,
2208 "flags", &child->devflags);
2212 * A bit bogus. Call the probe method again to make
2213 * sure that we have the right description.
2215 DEVICE_PROBE(child);
2217 child->flags |= DF_REBID;
2220 child->flags &= ~DF_REBID;
2221 child->state = DS_ALIVE;
2223 bus_data_generation_update();
2231 * @brief Return the parent of a device
2234 device_get_parent(device_t dev)
2236 return (dev->parent);
2240 * @brief Get a list of children of a device
2242 * An array containing a list of all the children of the given device
2243 * is allocated and returned in @p *devlistp. The number of devices
2244 * in the array is returned in @p *devcountp. The caller should free
2245 * the array using @c free(p, M_TEMP).
2247 * @param dev the device to examine
2248 * @param devlistp points at location for array pointer return
2250 * @param devcountp points at location for array size return value
2253 * @retval ENOMEM the array allocation failed
2256 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2263 TAILQ_FOREACH(child, &dev->children, link) {
2272 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2277 TAILQ_FOREACH(child, &dev->children, link) {
2278 list[count] = child;
2289 * @brief Return the current driver for the device or @c NULL if there
2290 * is no driver currently attached
2293 device_get_driver(device_t dev)
2295 return (dev->driver);
2299 * @brief Return the current devclass for the device or @c NULL if
2303 device_get_devclass(device_t dev)
2305 return (dev->devclass);
2309 * @brief Return the name of the device's devclass or @c NULL if there
2313 device_get_name(device_t dev)
2315 if (dev != NULL && dev->devclass)
2316 return (devclass_get_name(dev->devclass));
2321 * @brief Return a string containing the device's devclass name
2322 * followed by an ascii representation of the device's unit number
2326 device_get_nameunit(device_t dev)
2328 return (dev->nameunit);
2332 * @brief Return the device's unit number.
2335 device_get_unit(device_t dev)
2341 * @brief Return the device's description string
2344 device_get_desc(device_t dev)
2350 * @brief Return the device's flags
2353 device_get_flags(device_t dev)
2355 return (dev->devflags);
2358 struct sysctl_ctx_list *
2359 device_get_sysctl_ctx(device_t dev)
2361 return (&dev->sysctl_ctx);
2365 device_get_sysctl_tree(device_t dev)
2367 return (dev->sysctl_tree);
2371 * @brief Print the name of the device followed by a colon and a space
2373 * @returns the number of characters printed
2376 device_print_prettyname(device_t dev)
2378 const char *name = device_get_name(dev);
2381 return (printf("unknown: "));
2382 return (printf("%s%d: ", name, device_get_unit(dev)));
2386 * @brief Print the name of the device followed by a colon, a space
2387 * and the result of calling vprintf() with the value of @p fmt and
2388 * the following arguments.
2390 * @returns the number of characters printed
2393 device_printf(device_t dev, const char * fmt, ...)
2403 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
2404 sbuf_set_drain(&sb, sbuf_printf_drain, &retval);
2406 name = device_get_name(dev);
2409 sbuf_cat(&sb, "unknown: ");
2411 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
2414 sbuf_vprintf(&sb, fmt, ap);
2427 device_set_desc_internal(device_t dev, const char* desc, int copy)
2429 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2430 free(dev->desc, M_BUS);
2431 dev->flags &= ~DF_DESCMALLOCED;
2436 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2438 strcpy(dev->desc, desc);
2439 dev->flags |= DF_DESCMALLOCED;
2442 /* Avoid a -Wcast-qual warning */
2443 dev->desc = (char *)(uintptr_t) desc;
2446 bus_data_generation_update();
2450 * @brief Set the device's description
2452 * The value of @c desc should be a string constant that will not
2453 * change (at least until the description is changed in a subsequent
2454 * call to device_set_desc() or device_set_desc_copy()).
2457 device_set_desc(device_t dev, const char* desc)
2459 device_set_desc_internal(dev, desc, FALSE);
2463 * @brief Set the device's description
2465 * The string pointed to by @c desc is copied. Use this function if
2466 * the device description is generated, (e.g. with sprintf()).
2469 device_set_desc_copy(device_t dev, const char* desc)
2471 device_set_desc_internal(dev, desc, TRUE);
2475 * @brief Set the device's flags
2478 device_set_flags(device_t dev, uint32_t flags)
2480 dev->devflags = flags;
2484 * @brief Return the device's softc field
2486 * The softc is allocated and zeroed when a driver is attached, based
2487 * on the size field of the driver.
2490 device_get_softc(device_t dev)
2492 return (dev->softc);
2496 * @brief Set the device's softc field
2498 * Most drivers do not need to use this since the softc is allocated
2499 * automatically when the driver is attached.
2502 device_set_softc(device_t dev, void *softc)
2504 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2505 free(dev->softc, M_BUS_SC);
2508 dev->flags |= DF_EXTERNALSOFTC;
2510 dev->flags &= ~DF_EXTERNALSOFTC;
2514 * @brief Free claimed softc
2516 * Most drivers do not need to use this since the softc is freed
2517 * automatically when the driver is detached.
2520 device_free_softc(void *softc)
2522 free(softc, M_BUS_SC);
2526 * @brief Claim softc
2528 * This function can be used to let the driver free the automatically
2529 * allocated softc using "device_free_softc()". This function is
2530 * useful when the driver is refcounting the softc and the softc
2531 * cannot be freed when the "device_detach" method is called.
2534 device_claim_softc(device_t dev)
2537 dev->flags |= DF_EXTERNALSOFTC;
2539 dev->flags &= ~DF_EXTERNALSOFTC;
2543 * @brief Get the device's ivars field
2545 * The ivars field is used by the parent device to store per-device
2546 * state (e.g. the physical location of the device or a list of
2550 device_get_ivars(device_t dev)
2552 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2553 return (dev->ivars);
2557 * @brief Set the device's ivars field
2560 device_set_ivars(device_t dev, void * ivars)
2562 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2567 * @brief Return the device's state
2570 device_get_state(device_t dev)
2572 return (dev->state);
2576 * @brief Set the DF_ENABLED flag for the device
2579 device_enable(device_t dev)
2581 dev->flags |= DF_ENABLED;
2585 * @brief Clear the DF_ENABLED flag for the device
2588 device_disable(device_t dev)
2590 dev->flags &= ~DF_ENABLED;
2594 * @brief Increment the busy counter for the device
2597 device_busy(device_t dev)
2599 if (dev->state < DS_ATTACHING)
2600 panic("device_busy: called for unattached device");
2601 if (dev->busy == 0 && dev->parent)
2602 device_busy(dev->parent);
2604 if (dev->state == DS_ATTACHED)
2605 dev->state = DS_BUSY;
2609 * @brief Decrement the busy counter for the device
2612 device_unbusy(device_t dev)
2614 if (dev->busy != 0 && dev->state != DS_BUSY &&
2615 dev->state != DS_ATTACHING)
2616 panic("device_unbusy: called for non-busy device %s",
2617 device_get_nameunit(dev));
2619 if (dev->busy == 0) {
2621 device_unbusy(dev->parent);
2622 if (dev->state == DS_BUSY)
2623 dev->state = DS_ATTACHED;
2628 * @brief Set the DF_QUIET flag for the device
2631 device_quiet(device_t dev)
2633 dev->flags |= DF_QUIET;
2637 * @brief Set the DF_QUIET_CHILDREN flag for the device
2640 device_quiet_children(device_t dev)
2642 dev->flags |= DF_QUIET_CHILDREN;
2646 * @brief Clear the DF_QUIET flag for the device
2649 device_verbose(device_t dev)
2651 dev->flags &= ~DF_QUIET;
2655 * @brief Return non-zero if the DF_QUIET_CHIDLREN flag is set on the device
2658 device_has_quiet_children(device_t dev)
2660 return ((dev->flags & DF_QUIET_CHILDREN) != 0);
2664 * @brief Return non-zero if the DF_QUIET flag is set on the device
2667 device_is_quiet(device_t dev)
2669 return ((dev->flags & DF_QUIET) != 0);
2673 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2676 device_is_enabled(device_t dev)
2678 return ((dev->flags & DF_ENABLED) != 0);
2682 * @brief Return non-zero if the device was successfully probed
2685 device_is_alive(device_t dev)
2687 return (dev->state >= DS_ALIVE);
2691 * @brief Return non-zero if the device currently has a driver
2695 device_is_attached(device_t dev)
2697 return (dev->state >= DS_ATTACHED);
2701 * @brief Return non-zero if the device is currently suspended.
2704 device_is_suspended(device_t dev)
2706 return ((dev->flags & DF_SUSPENDED) != 0);
2710 * @brief Set the devclass of a device
2711 * @see devclass_add_device().
2714 device_set_devclass(device_t dev, const char *classname)
2721 devclass_delete_device(dev->devclass, dev);
2725 if (dev->devclass) {
2726 printf("device_set_devclass: device class already set\n");
2730 dc = devclass_find_internal(classname, NULL, TRUE);
2734 error = devclass_add_device(dc, dev);
2736 bus_data_generation_update();
2741 * @brief Set the devclass of a device and mark the devclass fixed.
2742 * @see device_set_devclass()
2745 device_set_devclass_fixed(device_t dev, const char *classname)
2749 if (classname == NULL)
2752 error = device_set_devclass(dev, classname);
2755 dev->flags |= DF_FIXEDCLASS;
2760 * @brief Query the device to determine if it's of a fixed devclass
2761 * @see device_set_devclass_fixed()
2764 device_is_devclass_fixed(device_t dev)
2766 return ((dev->flags & DF_FIXEDCLASS) != 0);
2770 * @brief Set the driver of a device
2773 * @retval EBUSY the device already has a driver attached
2774 * @retval ENOMEM a memory allocation failure occurred
2777 device_set_driver(device_t dev, driver_t *driver)
2780 struct domainset *policy;
2782 if (dev->state >= DS_ATTACHED)
2785 if (dev->driver == driver)
2788 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2789 free(dev->softc, M_BUS_SC);
2792 device_set_desc(dev, NULL);
2793 kobj_delete((kobj_t) dev, NULL);
2794 dev->driver = driver;
2796 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2797 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2798 if (bus_get_domain(dev, &domain) == 0)
2799 policy = DOMAINSET_PREF(domain);
2801 policy = DOMAINSET_RR();
2802 dev->softc = malloc_domainset(driver->size, M_BUS_SC,
2803 policy, M_NOWAIT | M_ZERO);
2805 kobj_delete((kobj_t) dev, NULL);
2806 kobj_init((kobj_t) dev, &null_class);
2812 kobj_init((kobj_t) dev, &null_class);
2815 bus_data_generation_update();
2820 * @brief Probe a device, and return this status.
2822 * This function is the core of the device autoconfiguration
2823 * system. Its purpose is to select a suitable driver for a device and
2824 * then call that driver to initialise the hardware appropriately. The
2825 * driver is selected by calling the DEVICE_PROBE() method of a set of
2826 * candidate drivers and then choosing the driver which returned the
2827 * best value. This driver is then attached to the device using
2830 * The set of suitable drivers is taken from the list of drivers in
2831 * the parent device's devclass. If the device was originally created
2832 * with a specific class name (see device_add_child()), only drivers
2833 * with that name are probed, otherwise all drivers in the devclass
2834 * are probed. If no drivers return successful probe values in the
2835 * parent devclass, the search continues in the parent of that
2836 * devclass (see devclass_get_parent()) if any.
2838 * @param dev the device to initialise
2841 * @retval ENXIO no driver was found
2842 * @retval ENOMEM memory allocation failure
2843 * @retval non-zero some other unix error code
2844 * @retval -1 Device already attached
2847 device_probe(device_t dev)
2853 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2856 if (!(dev->flags & DF_ENABLED)) {
2857 if (bootverbose && device_get_name(dev) != NULL) {
2858 device_print_prettyname(dev);
2859 printf("not probed (disabled)\n");
2863 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2864 if (bus_current_pass == BUS_PASS_DEFAULT &&
2865 !(dev->flags & DF_DONENOMATCH)) {
2866 BUS_PROBE_NOMATCH(dev->parent, dev);
2868 dev->flags |= DF_DONENOMATCH;
2876 * @brief Probe a device and attach a driver if possible
2878 * calls device_probe() and attaches if that was successful.
2881 device_probe_and_attach(device_t dev)
2887 error = device_probe(dev);
2890 else if (error != 0)
2893 CURVNET_SET_QUIET(vnet0);
2894 error = device_attach(dev);
2900 * @brief Attach a device driver to a device
2902 * This function is a wrapper around the DEVICE_ATTACH() driver
2903 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2904 * device's sysctl tree, optionally prints a description of the device
2905 * and queues a notification event for user-based device management
2908 * Normally this function is only called internally from
2909 * device_probe_and_attach().
2911 * @param dev the device to initialise
2914 * @retval ENXIO no driver was found
2915 * @retval ENOMEM memory allocation failure
2916 * @retval non-zero some other unix error code
2919 device_attach(device_t dev)
2921 uint64_t attachtime;
2922 uint16_t attachentropy;
2925 if (resource_disabled(dev->driver->name, dev->unit)) {
2926 device_disable(dev);
2928 device_printf(dev, "disabled via hints entry\n");
2932 device_sysctl_init(dev);
2933 if (!device_is_quiet(dev))
2934 device_print_child(dev->parent, dev);
2935 attachtime = get_cyclecount();
2936 dev->state = DS_ATTACHING;
2937 if ((error = DEVICE_ATTACH(dev)) != 0) {
2938 printf("device_attach: %s%d attach returned %d\n",
2939 dev->driver->name, dev->unit, error);
2940 if (!(dev->flags & DF_FIXEDCLASS))
2941 devclass_delete_device(dev->devclass, dev);
2942 (void)device_set_driver(dev, NULL);
2943 device_sysctl_fini(dev);
2944 KASSERT(dev->busy == 0, ("attach failed but busy"));
2945 dev->state = DS_NOTPRESENT;
2948 dev->flags |= DF_ATTACHED_ONCE;
2949 /* We only need the low bits of this time, but ranges from tens to thousands
2950 * have been seen, so keep 2 bytes' worth.
2952 attachentropy = (uint16_t)(get_cyclecount() - attachtime);
2953 random_harvest_direct(&attachentropy, sizeof(attachentropy), RANDOM_ATTACH);
2954 device_sysctl_update(dev);
2956 dev->state = DS_BUSY;
2958 dev->state = DS_ATTACHED;
2959 dev->flags &= ~DF_DONENOMATCH;
2960 EVENTHANDLER_DIRECT_INVOKE(device_attach, dev);
2966 * @brief Detach a driver from a device
2968 * This function is a wrapper around the DEVICE_DETACH() driver
2969 * method. If the call to DEVICE_DETACH() succeeds, it calls
2970 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2971 * notification event for user-based device management services and
2972 * cleans up the device's sysctl tree.
2974 * @param dev the device to un-initialise
2977 * @retval ENXIO no driver was found
2978 * @retval ENOMEM memory allocation failure
2979 * @retval non-zero some other unix error code
2982 device_detach(device_t dev)
2988 PDEBUG(("%s", DEVICENAME(dev)));
2989 if (dev->state == DS_BUSY)
2991 if (dev->state == DS_ATTACHING) {
2992 device_printf(dev, "device in attaching state! Deferring detach.\n");
2995 if (dev->state != DS_ATTACHED)
2998 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, EVHDEV_DETACH_BEGIN);
2999 if ((error = DEVICE_DETACH(dev)) != 0) {
3000 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
3001 EVHDEV_DETACH_FAILED);
3004 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
3005 EVHDEV_DETACH_COMPLETE);
3008 if (!device_is_quiet(dev))
3009 device_printf(dev, "detached\n");
3011 BUS_CHILD_DETACHED(dev->parent, dev);
3013 if (!(dev->flags & DF_FIXEDCLASS))
3014 devclass_delete_device(dev->devclass, dev);
3016 device_verbose(dev);
3017 dev->state = DS_NOTPRESENT;
3018 (void)device_set_driver(dev, NULL);
3019 device_sysctl_fini(dev);
3025 * @brief Tells a driver to quiesce itself.
3027 * This function is a wrapper around the DEVICE_QUIESCE() driver
3028 * method. If the call to DEVICE_QUIESCE() succeeds.
3030 * @param dev the device to quiesce
3033 * @retval ENXIO no driver was found
3034 * @retval ENOMEM memory allocation failure
3035 * @retval non-zero some other unix error code
3038 device_quiesce(device_t dev)
3040 PDEBUG(("%s", DEVICENAME(dev)));
3041 if (dev->state == DS_BUSY)
3043 if (dev->state != DS_ATTACHED)
3046 return (DEVICE_QUIESCE(dev));
3050 * @brief Notify a device of system shutdown
3052 * This function calls the DEVICE_SHUTDOWN() driver method if the
3053 * device currently has an attached driver.
3055 * @returns the value returned by DEVICE_SHUTDOWN()
3058 device_shutdown(device_t dev)
3060 if (dev->state < DS_ATTACHED)
3062 return (DEVICE_SHUTDOWN(dev));
3066 * @brief Set the unit number of a device
3068 * This function can be used to override the unit number used for a
3069 * device (e.g. to wire a device to a pre-configured unit number).
3072 device_set_unit(device_t dev, int unit)
3077 dc = device_get_devclass(dev);
3078 if (unit < dc->maxunit && dc->devices[unit])
3080 err = devclass_delete_device(dc, dev);
3084 err = devclass_add_device(dc, dev);
3088 bus_data_generation_update();
3092 /*======================================*/
3094 * Some useful method implementations to make life easier for bus drivers.
3098 resource_init_map_request_impl(struct resource_map_request *args, size_t sz)
3102 args->memattr = VM_MEMATTR_UNCACHEABLE;
3106 * @brief Initialise a resource list.
3108 * @param rl the resource list to initialise
3111 resource_list_init(struct resource_list *rl)
3117 * @brief Reclaim memory used by a resource list.
3119 * This function frees the memory for all resource entries on the list
3122 * @param rl the resource list to free
3125 resource_list_free(struct resource_list *rl)
3127 struct resource_list_entry *rle;
3129 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3131 panic("resource_list_free: resource entry is busy");
3132 STAILQ_REMOVE_HEAD(rl, link);
3138 * @brief Add a resource entry.
3140 * This function adds a resource entry using the given @p type, @p
3141 * start, @p end and @p count values. A rid value is chosen by
3142 * searching sequentially for the first unused rid starting at zero.
3144 * @param rl the resource list to edit
3145 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3146 * @param start the start address of the resource
3147 * @param end the end address of the resource
3148 * @param count XXX end-start+1
3151 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start,
3152 rman_res_t end, rman_res_t count)
3157 while (resource_list_find(rl, type, rid) != NULL)
3159 resource_list_add(rl, type, rid, start, end, count);
3164 * @brief Add or modify a resource entry.
3166 * If an existing entry exists with the same type and rid, it will be
3167 * modified using the given values of @p start, @p end and @p
3168 * count. If no entry exists, a new one will be created using the
3169 * given values. The resource list entry that matches is then returned.
3171 * @param rl the resource list to edit
3172 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3173 * @param rid the resource identifier
3174 * @param start the start address of the resource
3175 * @param end the end address of the resource
3176 * @param count XXX end-start+1
3178 struct resource_list_entry *
3179 resource_list_add(struct resource_list *rl, int type, int rid,
3180 rman_res_t start, rman_res_t end, rman_res_t count)
3182 struct resource_list_entry *rle;
3184 rle = resource_list_find(rl, type, rid);
3186 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3189 panic("resource_list_add: can't record entry");
3190 STAILQ_INSERT_TAIL(rl, rle, link);
3198 panic("resource_list_add: resource entry is busy");
3207 * @brief Determine if a resource entry is busy.
3209 * Returns true if a resource entry is busy meaning that it has an
3210 * associated resource that is not an unallocated "reserved" resource.
3212 * @param rl the resource list to search
3213 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3214 * @param rid the resource identifier
3216 * @returns Non-zero if the entry is busy, zero otherwise.
3219 resource_list_busy(struct resource_list *rl, int type, int rid)
3221 struct resource_list_entry *rle;
3223 rle = resource_list_find(rl, type, rid);
3224 if (rle == NULL || rle->res == NULL)
3226 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3227 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3228 ("reserved resource is active"));
3235 * @brief Determine if a resource entry is reserved.
3237 * Returns true if a resource entry is reserved meaning that it has an
3238 * associated "reserved" resource. The resource can either be
3239 * allocated or unallocated.
3241 * @param rl the resource list to search
3242 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3243 * @param rid the resource identifier
3245 * @returns Non-zero if the entry is reserved, zero otherwise.
3248 resource_list_reserved(struct resource_list *rl, int type, int rid)
3250 struct resource_list_entry *rle;
3252 rle = resource_list_find(rl, type, rid);
3253 if (rle != NULL && rle->flags & RLE_RESERVED)
3259 * @brief Find a resource entry by type and rid.
3261 * @param rl the resource list to search
3262 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3263 * @param rid the resource identifier
3265 * @returns the resource entry pointer or NULL if there is no such
3268 struct resource_list_entry *
3269 resource_list_find(struct resource_list *rl, int type, int rid)
3271 struct resource_list_entry *rle;
3273 STAILQ_FOREACH(rle, rl, link) {
3274 if (rle->type == type && rle->rid == rid)
3281 * @brief Delete a resource entry.
3283 * @param rl the resource list to edit
3284 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3285 * @param rid the resource identifier
3288 resource_list_delete(struct resource_list *rl, int type, int rid)
3290 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3293 if (rle->res != NULL)
3294 panic("resource_list_delete: resource has not been released");
3295 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3301 * @brief Allocate a reserved resource
3303 * This can be used by buses to force the allocation of resources
3304 * that are always active in the system even if they are not allocated
3305 * by a driver (e.g. PCI BARs). This function is usually called when
3306 * adding a new child to the bus. The resource is allocated from the
3307 * parent bus when it is reserved. The resource list entry is marked
3308 * with RLE_RESERVED to note that it is a reserved resource.
3310 * Subsequent attempts to allocate the resource with
3311 * resource_list_alloc() will succeed the first time and will set
3312 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3313 * resource that has been allocated is released with
3314 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3315 * the actual resource remains allocated. The resource can be released to
3316 * the parent bus by calling resource_list_unreserve().
3318 * @param rl the resource list to allocate from
3319 * @param bus the parent device of @p child
3320 * @param child the device for which the resource is being reserved
3321 * @param type the type of resource to allocate
3322 * @param rid a pointer to the resource identifier
3323 * @param start hint at the start of the resource range - pass
3324 * @c 0 for any start address
3325 * @param end hint at the end of the resource range - pass
3326 * @c ~0 for any end address
3327 * @param count hint at the size of range required - pass @c 1
3329 * @param flags any extra flags to control the resource
3330 * allocation - see @c RF_XXX flags in
3331 * <sys/rman.h> for details
3333 * @returns the resource which was allocated or @c NULL if no
3334 * resource could be allocated
3337 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3338 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3340 struct resource_list_entry *rle = NULL;
3341 int passthrough = (device_get_parent(child) != bus);
3346 "resource_list_reserve() should only be called for direct children");
3347 if (flags & RF_ACTIVE)
3349 "resource_list_reserve() should only reserve inactive resources");
3351 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3354 rle = resource_list_find(rl, type, *rid);
3355 rle->flags |= RLE_RESERVED;
3361 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3363 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3364 * and passing the allocation up to the parent of @p bus. This assumes
3365 * that the first entry of @c device_get_ivars(child) is a struct
3366 * resource_list. This also handles 'passthrough' allocations where a
3367 * child is a remote descendant of bus by passing the allocation up to
3368 * the parent of bus.
3370 * Typically, a bus driver would store a list of child resources
3371 * somewhere in the child device's ivars (see device_get_ivars()) and
3372 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3373 * then call resource_list_alloc() to perform the allocation.
3375 * @param rl the resource list to allocate from
3376 * @param bus the parent device of @p child
3377 * @param child the device which is requesting an allocation
3378 * @param type the type of resource to allocate
3379 * @param rid a pointer to the resource identifier
3380 * @param start hint at the start of the resource range - pass
3381 * @c 0 for any start address
3382 * @param end hint at the end of the resource range - pass
3383 * @c ~0 for any end address
3384 * @param count hint at the size of range required - pass @c 1
3386 * @param flags any extra flags to control the resource
3387 * allocation - see @c RF_XXX flags in
3388 * <sys/rman.h> for details
3390 * @returns the resource which was allocated or @c NULL if no
3391 * resource could be allocated
3394 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3395 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3397 struct resource_list_entry *rle = NULL;
3398 int passthrough = (device_get_parent(child) != bus);
3399 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
3402 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3403 type, rid, start, end, count, flags));
3406 rle = resource_list_find(rl, type, *rid);
3409 return (NULL); /* no resource of that type/rid */
3412 if (rle->flags & RLE_RESERVED) {
3413 if (rle->flags & RLE_ALLOCATED)
3415 if ((flags & RF_ACTIVE) &&
3416 bus_activate_resource(child, type, *rid,
3419 rle->flags |= RLE_ALLOCATED;
3423 "resource entry %#x type %d for child %s is busy\n", *rid,
3424 type, device_get_nameunit(child));
3430 count = ulmax(count, rle->count);
3431 end = ulmax(rle->end, start + count - 1);
3434 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3435 type, rid, start, end, count, flags);
3438 * Record the new range.
3441 rle->start = rman_get_start(rle->res);
3442 rle->end = rman_get_end(rle->res);
3450 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3452 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3453 * used with resource_list_alloc().
3455 * @param rl the resource list which was allocated from
3456 * @param bus the parent device of @p child
3457 * @param child the device which is requesting a release
3458 * @param type the type of resource to release
3459 * @param rid the resource identifier
3460 * @param res the resource to release
3463 * @retval non-zero a standard unix error code indicating what
3464 * error condition prevented the operation
3467 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3468 int type, int rid, struct resource *res)
3470 struct resource_list_entry *rle = NULL;
3471 int passthrough = (device_get_parent(child) != bus);
3475 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3479 rle = resource_list_find(rl, type, rid);
3482 panic("resource_list_release: can't find resource");
3484 panic("resource_list_release: resource entry is not busy");
3485 if (rle->flags & RLE_RESERVED) {
3486 if (rle->flags & RLE_ALLOCATED) {
3487 if (rman_get_flags(res) & RF_ACTIVE) {
3488 error = bus_deactivate_resource(child, type,
3493 rle->flags &= ~RLE_ALLOCATED;
3499 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3509 * @brief Release all active resources of a given type
3511 * Release all active resources of a specified type. This is intended
3512 * to be used to cleanup resources leaked by a driver after detach or
3515 * @param rl the resource list which was allocated from
3516 * @param bus the parent device of @p child
3517 * @param child the device whose active resources are being released
3518 * @param type the type of resources to release
3521 * @retval EBUSY at least one resource was active
3524 resource_list_release_active(struct resource_list *rl, device_t bus,
3525 device_t child, int type)
3527 struct resource_list_entry *rle;
3531 STAILQ_FOREACH(rle, rl, link) {
3532 if (rle->type != type)
3534 if (rle->res == NULL)
3536 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3540 error = resource_list_release(rl, bus, child, type,
3541 rman_get_rid(rle->res), rle->res);
3544 "Failed to release active resource: %d\n", error);
3550 * @brief Fully release a reserved resource
3552 * Fully releases a resource reserved via resource_list_reserve().
3554 * @param rl the resource list which was allocated from
3555 * @param bus the parent device of @p child
3556 * @param child the device whose reserved resource is being released
3557 * @param type the type of resource to release
3558 * @param rid the resource identifier
3559 * @param res the resource to release
3562 * @retval non-zero a standard unix error code indicating what
3563 * error condition prevented the operation
3566 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3569 struct resource_list_entry *rle = NULL;
3570 int passthrough = (device_get_parent(child) != bus);
3574 "resource_list_unreserve() should only be called for direct children");
3576 rle = resource_list_find(rl, type, rid);
3579 panic("resource_list_unreserve: can't find resource");
3580 if (!(rle->flags & RLE_RESERVED))
3582 if (rle->flags & RLE_ALLOCATED)
3584 rle->flags &= ~RLE_RESERVED;
3585 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3589 * @brief Print a description of resources in a resource list
3591 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3592 * The name is printed if at least one resource of the given type is available.
3593 * The format is used to print resource start and end.
3595 * @param rl the resource list to print
3596 * @param name the name of @p type, e.g. @c "memory"
3597 * @param type type type of resource entry to print
3598 * @param format printf(9) format string to print resource
3599 * start and end values
3601 * @returns the number of characters printed
3604 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3607 struct resource_list_entry *rle;
3608 int printed, retval;
3612 /* Yes, this is kinda cheating */
3613 STAILQ_FOREACH(rle, rl, link) {
3614 if (rle->type == type) {
3616 retval += printf(" %s ", name);
3618 retval += printf(",");
3620 retval += printf(format, rle->start);
3621 if (rle->count > 1) {
3622 retval += printf("-");
3623 retval += printf(format, rle->start +
3632 * @brief Releases all the resources in a list.
3634 * @param rl The resource list to purge.
3639 resource_list_purge(struct resource_list *rl)
3641 struct resource_list_entry *rle;
3643 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3645 bus_release_resource(rman_get_device(rle->res),
3646 rle->type, rle->rid, rle->res);
3647 STAILQ_REMOVE_HEAD(rl, link);
3653 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3655 return (device_add_child_ordered(dev, order, name, unit));
3659 * @brief Helper function for implementing DEVICE_PROBE()
3661 * This function can be used to help implement the DEVICE_PROBE() for
3662 * a bus (i.e. a device which has other devices attached to it). It
3663 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3667 bus_generic_probe(device_t dev)
3669 devclass_t dc = dev->devclass;
3672 TAILQ_FOREACH(dl, &dc->drivers, link) {
3674 * If this driver's pass is too high, then ignore it.
3675 * For most drivers in the default pass, this will
3676 * never be true. For early-pass drivers they will
3677 * only call the identify routines of eligible drivers
3678 * when this routine is called. Drivers for later
3679 * passes should have their identify routines called
3680 * on early-pass buses during BUS_NEW_PASS().
3682 if (dl->pass > bus_current_pass)
3684 DEVICE_IDENTIFY(dl->driver, dev);
3691 * @brief Helper function for implementing DEVICE_ATTACH()
3693 * This function can be used to help implement the DEVICE_ATTACH() for
3694 * a bus. It calls device_probe_and_attach() for each of the device's
3698 bus_generic_attach(device_t dev)
3702 TAILQ_FOREACH(child, &dev->children, link) {
3703 device_probe_and_attach(child);
3710 * @brief Helper function for delaying attaching children
3712 * Many buses can't run transactions on the bus which children need to probe and
3713 * attach until after interrupts and/or timers are running. This function
3714 * delays their attach until interrupts and timers are enabled.
3717 bus_delayed_attach_children(device_t dev)
3719 /* Probe and attach the bus children when interrupts are available */
3720 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
3726 * @brief Helper function for implementing DEVICE_DETACH()
3728 * This function can be used to help implement the DEVICE_DETACH() for
3729 * a bus. It calls device_detach() for each of the device's
3733 bus_generic_detach(device_t dev)
3738 if (dev->state != DS_ATTACHED)
3742 * Detach children in the reverse order.
3743 * See bus_generic_suspend for details.
3745 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3746 if ((error = device_detach(child)) != 0)
3754 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3756 * This function can be used to help implement the DEVICE_SHUTDOWN()
3757 * for a bus. It calls device_shutdown() for each of the device's
3761 bus_generic_shutdown(device_t dev)
3766 * Shut down children in the reverse order.
3767 * See bus_generic_suspend for details.
3769 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3770 device_shutdown(child);
3777 * @brief Default function for suspending a child device.
3779 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3782 bus_generic_suspend_child(device_t dev, device_t child)
3786 error = DEVICE_SUSPEND(child);
3789 child->flags |= DF_SUSPENDED;
3795 * @brief Default function for resuming a child device.
3797 * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3800 bus_generic_resume_child(device_t dev, device_t child)
3802 DEVICE_RESUME(child);
3803 child->flags &= ~DF_SUSPENDED;
3809 * @brief Helper function for implementing DEVICE_SUSPEND()
3811 * This function can be used to help implement the DEVICE_SUSPEND()
3812 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3813 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3814 * operation is aborted and any devices which were suspended are
3815 * resumed immediately by calling their DEVICE_RESUME() methods.
3818 bus_generic_suspend(device_t dev)
3824 * Suspend children in the reverse order.
3825 * For most buses all children are equal, so the order does not matter.
3826 * Other buses, such as acpi, carefully order their child devices to
3827 * express implicit dependencies between them. For such buses it is
3828 * safer to bring down devices in the reverse order.
3830 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3831 error = BUS_SUSPEND_CHILD(dev, child);
3833 child = TAILQ_NEXT(child, link);
3834 if (child != NULL) {
3835 TAILQ_FOREACH_FROM(child, &dev->children, link)
3836 BUS_RESUME_CHILD(dev, child);
3845 * @brief Helper function for implementing DEVICE_RESUME()
3847 * This function can be used to help implement the DEVICE_RESUME() for
3848 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3851 bus_generic_resume(device_t dev)
3855 TAILQ_FOREACH(child, &dev->children, link) {
3856 BUS_RESUME_CHILD(dev, child);
3857 /* if resume fails, there's nothing we can usefully do... */
3863 * @brief Helper function for implementing BUS_RESET_POST
3865 * Bus can use this function to implement common operations of
3866 * re-attaching or resuming the children after the bus itself was
3867 * reset, and after restoring bus-unique state of children.
3869 * @param dev The bus
3870 * #param flags DEVF_RESET_*
3873 bus_helper_reset_post(device_t dev, int flags)
3879 TAILQ_FOREACH(child, &dev->children,link) {
3880 BUS_RESET_POST(dev, child);
3881 error1 = (flags & DEVF_RESET_DETACH) != 0 ?
3882 device_probe_and_attach(child) :
3883 BUS_RESUME_CHILD(dev, child);
3884 if (error == 0 && error1 != 0)
3891 bus_helper_reset_prepare_rollback(device_t dev, device_t child, int flags)
3893 child = TAILQ_NEXT(child, link);
3896 TAILQ_FOREACH_FROM(child, &dev->children,link) {
3897 BUS_RESET_POST(dev, child);
3898 if ((flags & DEVF_RESET_DETACH) != 0)
3899 device_probe_and_attach(child);
3901 BUS_RESUME_CHILD(dev, child);
3906 * @brief Helper function for implementing BUS_RESET_PREPARE
3908 * Bus can use this function to implement common operations of
3909 * detaching or suspending the children before the bus itself is
3910 * reset, and then save bus-unique state of children that must
3911 * persists around reset.
3913 * @param dev The bus
3914 * #param flags DEVF_RESET_*
3917 bus_helper_reset_prepare(device_t dev, int flags)
3922 if (dev->state != DS_ATTACHED)
3925 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3926 if ((flags & DEVF_RESET_DETACH) != 0) {
3927 error = device_get_state(child) == DS_ATTACHED ?
3928 device_detach(child) : 0;
3930 error = BUS_SUSPEND_CHILD(dev, child);
3933 error = BUS_RESET_PREPARE(dev, child);
3935 if ((flags & DEVF_RESET_DETACH) != 0)
3936 device_probe_and_attach(child);
3938 BUS_RESUME_CHILD(dev, child);
3942 bus_helper_reset_prepare_rollback(dev, child, flags);
3950 * @brief Helper function for implementing BUS_PRINT_CHILD().
3952 * This function prints the first part of the ascii representation of
3953 * @p child, including its name, unit and description (if any - see
3954 * device_set_desc()).
3956 * @returns the number of characters printed
3959 bus_print_child_header(device_t dev, device_t child)
3963 if (device_get_desc(child)) {
3964 retval += device_printf(child, "<%s>", device_get_desc(child));
3966 retval += printf("%s", device_get_nameunit(child));
3973 * @brief Helper function for implementing BUS_PRINT_CHILD().
3975 * This function prints the last part of the ascii representation of
3976 * @p child, which consists of the string @c " on " followed by the
3977 * name and unit of the @p dev.
3979 * @returns the number of characters printed
3982 bus_print_child_footer(device_t dev, device_t child)
3984 return (printf(" on %s\n", device_get_nameunit(dev)));
3988 * @brief Helper function for implementing BUS_PRINT_CHILD().
3990 * This function prints out the VM domain for the given device.
3992 * @returns the number of characters printed
3995 bus_print_child_domain(device_t dev, device_t child)
3999 /* No domain? Don't print anything */
4000 if (BUS_GET_DOMAIN(dev, child, &domain) != 0)
4003 return (printf(" numa-domain %d", domain));
4007 * @brief Helper function for implementing BUS_PRINT_CHILD().
4009 * This function simply calls bus_print_child_header() followed by
4010 * bus_print_child_footer().
4012 * @returns the number of characters printed
4015 bus_generic_print_child(device_t dev, device_t child)
4019 retval += bus_print_child_header(dev, child);
4020 retval += bus_print_child_domain(dev, child);
4021 retval += bus_print_child_footer(dev, child);
4027 * @brief Stub function for implementing BUS_READ_IVAR().
4032 bus_generic_read_ivar(device_t dev, device_t child, int index,
4039 * @brief Stub function for implementing BUS_WRITE_IVAR().
4044 bus_generic_write_ivar(device_t dev, device_t child, int index,
4051 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
4055 struct resource_list *
4056 bus_generic_get_resource_list(device_t dev, device_t child)
4062 * @brief Helper function for implementing BUS_DRIVER_ADDED().
4064 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
4065 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
4066 * and then calls device_probe_and_attach() for each unattached child.
4069 bus_generic_driver_added(device_t dev, driver_t *driver)
4073 DEVICE_IDENTIFY(driver, dev);
4074 TAILQ_FOREACH(child, &dev->children, link) {
4075 if (child->state == DS_NOTPRESENT ||
4076 (child->flags & DF_REBID))
4077 device_probe_and_attach(child);
4082 * @brief Helper function for implementing BUS_NEW_PASS().
4084 * This implementing of BUS_NEW_PASS() first calls the identify
4085 * routines for any drivers that probe at the current pass. Then it
4086 * walks the list of devices for this bus. If a device is already
4087 * attached, then it calls BUS_NEW_PASS() on that device. If the
4088 * device is not already attached, it attempts to attach a driver to
4092 bus_generic_new_pass(device_t dev)
4099 TAILQ_FOREACH(dl, &dc->drivers, link) {
4100 if (dl->pass == bus_current_pass)
4101 DEVICE_IDENTIFY(dl->driver, dev);
4103 TAILQ_FOREACH(child, &dev->children, link) {
4104 if (child->state >= DS_ATTACHED)
4105 BUS_NEW_PASS(child);
4106 else if (child->state == DS_NOTPRESENT)
4107 device_probe_and_attach(child);
4112 * @brief Helper function for implementing BUS_SETUP_INTR().
4114 * This simple implementation of BUS_SETUP_INTR() simply calls the
4115 * BUS_SETUP_INTR() method of the parent of @p dev.
4118 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
4119 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
4122 /* Propagate up the bus hierarchy until someone handles it. */
4124 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
4125 filter, intr, arg, cookiep));
4130 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
4132 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
4133 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
4136 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
4139 /* Propagate up the bus hierarchy until someone handles it. */
4141 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
4146 * @brief Helper function for implementing BUS_SUSPEND_INTR().
4148 * This simple implementation of BUS_SUSPEND_INTR() simply calls the
4149 * BUS_SUSPEND_INTR() method of the parent of @p dev.
4152 bus_generic_suspend_intr(device_t dev, device_t child, struct resource *irq)
4154 /* Propagate up the bus hierarchy until someone handles it. */
4156 return (BUS_SUSPEND_INTR(dev->parent, child, irq));
4161 * @brief Helper function for implementing BUS_RESUME_INTR().
4163 * This simple implementation of BUS_RESUME_INTR() simply calls the
4164 * BUS_RESUME_INTR() method of the parent of @p dev.
4167 bus_generic_resume_intr(device_t dev, device_t child, struct resource *irq)
4169 /* Propagate up the bus hierarchy until someone handles it. */
4171 return (BUS_RESUME_INTR(dev->parent, child, irq));
4176 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
4178 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
4179 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
4182 bus_generic_adjust_resource(device_t dev, device_t child, int type,
4183 struct resource *r, rman_res_t start, rman_res_t end)
4185 /* Propagate up the bus hierarchy until someone handles it. */
4187 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
4193 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4195 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
4196 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
4199 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
4200 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4202 /* Propagate up the bus hierarchy until someone handles it. */
4204 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
4205 start, end, count, flags));
4210 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4212 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
4213 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
4216 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
4219 /* Propagate up the bus hierarchy until someone handles it. */
4221 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
4227 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
4229 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
4230 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
4233 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
4236 /* Propagate up the bus hierarchy until someone handles it. */
4238 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
4244 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
4246 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
4247 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
4250 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
4251 int rid, struct resource *r)
4253 /* Propagate up the bus hierarchy until someone handles it. */
4255 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
4261 * @brief Helper function for implementing BUS_MAP_RESOURCE().
4263 * This simple implementation of BUS_MAP_RESOURCE() simply calls the
4264 * BUS_MAP_RESOURCE() method of the parent of @p dev.
4267 bus_generic_map_resource(device_t dev, device_t child, int type,
4268 struct resource *r, struct resource_map_request *args,
4269 struct resource_map *map)
4271 /* Propagate up the bus hierarchy until someone handles it. */
4273 return (BUS_MAP_RESOURCE(dev->parent, child, type, r, args,
4279 * @brief Helper function for implementing BUS_UNMAP_RESOURCE().
4281 * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the
4282 * BUS_UNMAP_RESOURCE() method of the parent of @p dev.
4285 bus_generic_unmap_resource(device_t dev, device_t child, int type,
4286 struct resource *r, struct resource_map *map)
4288 /* Propagate up the bus hierarchy until someone handles it. */
4290 return (BUS_UNMAP_RESOURCE(dev->parent, child, type, r, map));
4295 * @brief Helper function for implementing BUS_BIND_INTR().
4297 * This simple implementation of BUS_BIND_INTR() simply calls the
4298 * BUS_BIND_INTR() method of the parent of @p dev.
4301 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
4304 /* Propagate up the bus hierarchy until someone handles it. */
4306 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
4311 * @brief Helper function for implementing BUS_CONFIG_INTR().
4313 * This simple implementation of BUS_CONFIG_INTR() simply calls the
4314 * BUS_CONFIG_INTR() method of the parent of @p dev.
4317 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
4318 enum intr_polarity pol)
4320 /* Propagate up the bus hierarchy until someone handles it. */
4322 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
4327 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
4329 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
4330 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
4333 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
4334 void *cookie, const char *descr)
4336 /* Propagate up the bus hierarchy until someone handles it. */
4338 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
4344 * @brief Helper function for implementing BUS_GET_CPUS().
4346 * This simple implementation of BUS_GET_CPUS() simply calls the
4347 * BUS_GET_CPUS() method of the parent of @p dev.
4350 bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op,
4351 size_t setsize, cpuset_t *cpuset)
4353 /* Propagate up the bus hierarchy until someone handles it. */
4354 if (dev->parent != NULL)
4355 return (BUS_GET_CPUS(dev->parent, child, op, setsize, cpuset));
4360 * @brief Helper function for implementing BUS_GET_DMA_TAG().
4362 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
4363 * BUS_GET_DMA_TAG() method of the parent of @p dev.
4366 bus_generic_get_dma_tag(device_t dev, device_t child)
4368 /* Propagate up the bus hierarchy until someone handles it. */
4369 if (dev->parent != NULL)
4370 return (BUS_GET_DMA_TAG(dev->parent, child));
4375 * @brief Helper function for implementing BUS_GET_BUS_TAG().
4377 * This simple implementation of BUS_GET_BUS_TAG() simply calls the
4378 * BUS_GET_BUS_TAG() method of the parent of @p dev.
4381 bus_generic_get_bus_tag(device_t dev, device_t child)
4383 /* Propagate up the bus hierarchy until someone handles it. */
4384 if (dev->parent != NULL)
4385 return (BUS_GET_BUS_TAG(dev->parent, child));
4386 return ((bus_space_tag_t)0);
4390 * @brief Helper function for implementing BUS_GET_RESOURCE().
4392 * This implementation of BUS_GET_RESOURCE() uses the
4393 * resource_list_find() function to do most of the work. It calls
4394 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4398 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4399 rman_res_t *startp, rman_res_t *countp)
4401 struct resource_list * rl = NULL;
4402 struct resource_list_entry * rle = NULL;
4404 rl = BUS_GET_RESOURCE_LIST(dev, child);
4408 rle = resource_list_find(rl, type, rid);
4413 *startp = rle->start;
4415 *countp = rle->count;
4421 * @brief Helper function for implementing BUS_SET_RESOURCE().
4423 * This implementation of BUS_SET_RESOURCE() uses the
4424 * resource_list_add() function to do most of the work. It calls
4425 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4429 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4430 rman_res_t start, rman_res_t count)
4432 struct resource_list * rl = NULL;
4434 rl = BUS_GET_RESOURCE_LIST(dev, child);
4438 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4444 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4446 * This implementation of BUS_DELETE_RESOURCE() uses the
4447 * resource_list_delete() function to do most of the work. It calls
4448 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4452 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4454 struct resource_list * rl = NULL;
4456 rl = BUS_GET_RESOURCE_LIST(dev, child);
4460 resource_list_delete(rl, type, rid);
4466 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4468 * This implementation of BUS_RELEASE_RESOURCE() uses the
4469 * resource_list_release() function to do most of the work. It calls
4470 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4473 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4474 int rid, struct resource *r)
4476 struct resource_list * rl = NULL;
4478 if (device_get_parent(child) != dev)
4479 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4482 rl = BUS_GET_RESOURCE_LIST(dev, child);
4486 return (resource_list_release(rl, dev, child, type, rid, r));
4490 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4492 * This implementation of BUS_ALLOC_RESOURCE() uses the
4493 * resource_list_alloc() function to do most of the work. It calls
4494 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4497 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4498 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4500 struct resource_list * rl = NULL;
4502 if (device_get_parent(child) != dev)
4503 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4504 type, rid, start, end, count, flags));
4506 rl = BUS_GET_RESOURCE_LIST(dev, child);
4510 return (resource_list_alloc(rl, dev, child, type, rid,
4511 start, end, count, flags));
4515 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4517 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4518 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4521 bus_generic_child_present(device_t dev, device_t child)
4523 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4527 bus_generic_get_domain(device_t dev, device_t child, int *domain)
4530 return (BUS_GET_DOMAIN(dev->parent, dev, domain));
4536 * @brief Helper function for implementing BUS_RESCAN().
4538 * This null implementation of BUS_RESCAN() always fails to indicate
4539 * the bus does not support rescanning.
4542 bus_null_rescan(device_t dev)
4548 * Some convenience functions to make it easier for drivers to use the
4549 * resource-management functions. All these really do is hide the
4550 * indirection through the parent's method table, making for slightly
4551 * less-wordy code. In the future, it might make sense for this code
4552 * to maintain some sort of a list of resources allocated by each device.
4556 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4557 struct resource **res)
4561 for (i = 0; rs[i].type != -1; i++)
4563 for (i = 0; rs[i].type != -1; i++) {
4564 res[i] = bus_alloc_resource_any(dev,
4565 rs[i].type, &rs[i].rid, rs[i].flags);
4566 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4567 bus_release_resources(dev, rs, res);
4575 bus_release_resources(device_t dev, const struct resource_spec *rs,
4576 struct resource **res)
4580 for (i = 0; rs[i].type != -1; i++)
4581 if (res[i] != NULL) {
4582 bus_release_resource(
4583 dev, rs[i].type, rs[i].rid, res[i]);
4589 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4591 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4595 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
4596 rman_res_t end, rman_res_t count, u_int flags)
4598 struct resource *res;
4600 if (dev->parent == NULL)
4602 res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4608 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4610 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4614 bus_adjust_resource(device_t dev, int type, struct resource *r, rman_res_t start,
4617 if (dev->parent == NULL)
4619 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4623 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4625 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4629 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4631 if (dev->parent == NULL)
4633 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4637 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4639 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4643 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4645 if (dev->parent == NULL)
4647 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4651 * @brief Wrapper function for BUS_MAP_RESOURCE().
4653 * This function simply calls the BUS_MAP_RESOURCE() method of the
4657 bus_map_resource(device_t dev, int type, struct resource *r,
4658 struct resource_map_request *args, struct resource_map *map)
4660 if (dev->parent == NULL)
4662 return (BUS_MAP_RESOURCE(dev->parent, dev, type, r, args, map));
4666 * @brief Wrapper function for BUS_UNMAP_RESOURCE().
4668 * This function simply calls the BUS_UNMAP_RESOURCE() method of the
4672 bus_unmap_resource(device_t dev, int type, struct resource *r,
4673 struct resource_map *map)
4675 if (dev->parent == NULL)
4677 return (BUS_UNMAP_RESOURCE(dev->parent, dev, type, r, map));
4681 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4683 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4687 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4691 if (dev->parent == NULL)
4693 rv = BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r);
4698 * @brief Wrapper function for BUS_SETUP_INTR().
4700 * This function simply calls the BUS_SETUP_INTR() method of the
4704 bus_setup_intr(device_t dev, struct resource *r, int flags,
4705 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4709 if (dev->parent == NULL)
4711 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4715 if (handler != NULL && !(flags & INTR_MPSAFE))
4716 device_printf(dev, "[GIANT-LOCKED]\n");
4721 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4723 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4727 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4729 if (dev->parent == NULL)
4731 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4735 * @brief Wrapper function for BUS_SUSPEND_INTR().
4737 * This function simply calls the BUS_SUSPEND_INTR() method of the
4741 bus_suspend_intr(device_t dev, struct resource *r)
4743 if (dev->parent == NULL)
4745 return (BUS_SUSPEND_INTR(dev->parent, dev, r));
4749 * @brief Wrapper function for BUS_RESUME_INTR().
4751 * This function simply calls the BUS_RESUME_INTR() method of the
4755 bus_resume_intr(device_t dev, struct resource *r)
4757 if (dev->parent == NULL)
4759 return (BUS_RESUME_INTR(dev->parent, dev, r));
4763 * @brief Wrapper function for BUS_BIND_INTR().
4765 * This function simply calls the BUS_BIND_INTR() method of the
4769 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4771 if (dev->parent == NULL)
4773 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4777 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4779 * This function first formats the requested description into a
4780 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4781 * the parent of @p dev.
4784 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4785 const char *fmt, ...)
4788 char descr[MAXCOMLEN + 1];
4790 if (dev->parent == NULL)
4793 vsnprintf(descr, sizeof(descr), fmt, ap);
4795 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4799 * @brief Wrapper function for BUS_SET_RESOURCE().
4801 * This function simply calls the BUS_SET_RESOURCE() method of the
4805 bus_set_resource(device_t dev, int type, int rid,
4806 rman_res_t start, rman_res_t count)
4808 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4813 * @brief Wrapper function for BUS_GET_RESOURCE().
4815 * This function simply calls the BUS_GET_RESOURCE() method of the
4819 bus_get_resource(device_t dev, int type, int rid,
4820 rman_res_t *startp, rman_res_t *countp)
4822 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4827 * @brief Wrapper function for BUS_GET_RESOURCE().
4829 * This function simply calls the BUS_GET_RESOURCE() method of the
4830 * parent of @p dev and returns the start value.
4833 bus_get_resource_start(device_t dev, int type, int rid)
4839 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4847 * @brief Wrapper function for BUS_GET_RESOURCE().
4849 * This function simply calls the BUS_GET_RESOURCE() method of the
4850 * parent of @p dev and returns the count value.
4853 bus_get_resource_count(device_t dev, int type, int rid)
4859 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4867 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4869 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4873 bus_delete_resource(device_t dev, int type, int rid)
4875 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4879 * @brief Wrapper function for BUS_CHILD_PRESENT().
4881 * This function simply calls the BUS_CHILD_PRESENT() method of the
4885 bus_child_present(device_t child)
4887 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4891 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4893 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4897 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4901 parent = device_get_parent(child);
4902 if (parent == NULL) {
4906 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4910 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4912 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4916 bus_child_location_str(device_t child, char *buf, size_t buflen)
4920 parent = device_get_parent(child);
4921 if (parent == NULL) {
4925 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4929 * @brief Wrapper function for bus_child_pnpinfo_str using sbuf
4931 * A convenient wrapper frunction for bus_child_pnpinfo_str that allows
4932 * us to splat that into an sbuf. It uses unholy knowledge of sbuf to
4933 * accomplish this, however. It is an interim function until we can convert
4934 * this interface more fully.
4936 /* Note: we reach inside of sbuf because it's API isn't rich enough to do this */
4937 #define SPACE(s) ((s)->s_size - (s)->s_len)
4938 #define EOB(s) ((s)->s_buf + (s)->s_len)
4941 bus_child_pnpinfo_sb(device_t dev, struct sbuf *sb)
4946 MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0);
4947 if (sb->s_error != 0)
4950 *p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
4953 sb->s_error = ENOMEM;
4956 bus_child_pnpinfo_str(dev, p, space);
4957 sb->s_len += strlen(p);
4962 * @brief Wrapper function for bus_child_pnpinfo_str using sbuf
4964 * A convenient wrapper frunction for bus_child_pnpinfo_str that allows
4965 * us to splat that into an sbuf. It uses unholy knowledge of sbuf to
4966 * accomplish this, however. It is an interim function until we can convert
4967 * this interface more fully.
4970 bus_child_location_sb(device_t dev, struct sbuf *sb)
4975 MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0);
4976 if (sb->s_error != 0)
4979 *p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
4982 sb->s_error = ENOMEM;
4985 bus_child_location_str(dev, p, space);
4986 sb->s_len += strlen(p);
4993 * @brief Wrapper function for BUS_GET_CPUS().
4995 * This function simply calls the BUS_GET_CPUS() method of the
4999 bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, cpuset_t *cpuset)
5003 parent = device_get_parent(dev);
5006 return (BUS_GET_CPUS(parent, dev, op, setsize, cpuset));
5010 * @brief Wrapper function for BUS_GET_DMA_TAG().
5012 * This function simply calls the BUS_GET_DMA_TAG() method of the
5016 bus_get_dma_tag(device_t dev)
5020 parent = device_get_parent(dev);
5023 return (BUS_GET_DMA_TAG(parent, dev));
5027 * @brief Wrapper function for BUS_GET_BUS_TAG().
5029 * This function simply calls the BUS_GET_BUS_TAG() method of the
5033 bus_get_bus_tag(device_t dev)
5037 parent = device_get_parent(dev);
5039 return ((bus_space_tag_t)0);
5040 return (BUS_GET_BUS_TAG(parent, dev));
5044 * @brief Wrapper function for BUS_GET_DOMAIN().
5046 * This function simply calls the BUS_GET_DOMAIN() method of the
5050 bus_get_domain(device_t dev, int *domain)
5052 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain));
5055 /* Resume all devices and then notify userland that we're up again. */
5057 root_resume(device_t dev)
5061 error = bus_generic_resume(dev);
5063 devctl_notify("kern", "power", "resume", NULL); /* Deprecated gone in 14 */
5064 devctl_notify("kernel", "power", "resume", NULL);
5070 root_print_child(device_t dev, device_t child)
5074 retval += bus_print_child_header(dev, child);
5075 retval += printf("\n");
5081 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
5082 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
5085 * If an interrupt mapping gets to here something bad has happened.
5087 panic("root_setup_intr");
5091 * If we get here, assume that the device is permanent and really is
5092 * present in the system. Removable bus drivers are expected to intercept
5093 * this call long before it gets here. We return -1 so that drivers that
5094 * really care can check vs -1 or some ERRNO returned higher in the food
5098 root_child_present(device_t dev, device_t child)
5104 root_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
5109 /* Default to returning the set of all CPUs. */
5110 if (setsize != sizeof(cpuset_t))
5119 static kobj_method_t root_methods[] = {
5120 /* Device interface */
5121 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
5122 KOBJMETHOD(device_suspend, bus_generic_suspend),
5123 KOBJMETHOD(device_resume, root_resume),
5126 KOBJMETHOD(bus_print_child, root_print_child),
5127 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
5128 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
5129 KOBJMETHOD(bus_setup_intr, root_setup_intr),
5130 KOBJMETHOD(bus_child_present, root_child_present),
5131 KOBJMETHOD(bus_get_cpus, root_get_cpus),
5136 static driver_t root_driver = {
5143 devclass_t root_devclass;
5146 root_bus_module_handler(module_t mod, int what, void* arg)
5150 TAILQ_INIT(&bus_data_devices);
5151 kobj_class_compile((kobj_class_t) &root_driver);
5152 root_bus = make_device(NULL, "root", 0);
5153 root_bus->desc = "System root bus";
5154 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
5155 root_bus->driver = &root_driver;
5156 root_bus->state = DS_ATTACHED;
5157 root_devclass = devclass_find_internal("root", NULL, FALSE);
5162 device_shutdown(root_bus);
5165 return (EOPNOTSUPP);
5171 static moduledata_t root_bus_mod = {
5173 root_bus_module_handler,
5176 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
5179 * @brief Automatically configure devices
5181 * This function begins the autoconfiguration process by calling
5182 * device_probe_and_attach() for each child of the @c root0 device.
5185 root_bus_configure(void)
5189 /* Eventually this will be split up, but this is sufficient for now. */
5190 bus_set_pass(BUS_PASS_DEFAULT);
5194 * @brief Module handler for registering device drivers
5196 * This module handler is used to automatically register device
5197 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
5198 * devclass_add_driver() for the driver described by the
5199 * driver_module_data structure pointed to by @p arg
5202 driver_module_handler(module_t mod, int what, void *arg)
5204 struct driver_module_data *dmd;
5205 devclass_t bus_devclass;
5206 kobj_class_t driver;
5209 dmd = (struct driver_module_data *)arg;
5210 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
5215 if (dmd->dmd_chainevh)
5216 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5218 pass = dmd->dmd_pass;
5219 driver = dmd->dmd_driver;
5220 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
5221 DRIVERNAME(driver), dmd->dmd_busname, pass));
5222 error = devclass_add_driver(bus_devclass, driver, pass,
5227 PDEBUG(("Unloading module: driver %s from bus %s",
5228 DRIVERNAME(dmd->dmd_driver),
5230 error = devclass_delete_driver(bus_devclass,
5233 if (!error && dmd->dmd_chainevh)
5234 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5237 PDEBUG(("Quiesce module: driver %s from bus %s",
5238 DRIVERNAME(dmd->dmd_driver),
5240 error = devclass_quiesce_driver(bus_devclass,
5243 if (!error && dmd->dmd_chainevh)
5244 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5255 * @brief Enumerate all hinted devices for this bus.
5257 * Walks through the hints for this bus and calls the bus_hinted_child
5258 * routine for each one it fines. It searches first for the specific
5259 * bus that's being probed for hinted children (eg isa0), and then for
5260 * generic children (eg isa).
5262 * @param dev bus device to enumerate
5265 bus_enumerate_hinted_children(device_t bus)
5268 const char *dname, *busname;
5272 * enumerate all devices on the specific bus
5274 busname = device_get_nameunit(bus);
5276 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5277 BUS_HINTED_CHILD(bus, dname, dunit);
5280 * and all the generic ones.
5282 busname = device_get_name(bus);
5284 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5285 BUS_HINTED_CHILD(bus, dname, dunit);
5290 /* the _short versions avoid iteration by not calling anything that prints
5291 * more than oneliners. I love oneliners.
5295 print_device_short(device_t dev, int indent)
5300 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
5301 dev->unit, dev->desc,
5302 (dev->parent? "":"no "),
5303 (TAILQ_EMPTY(&dev->children)? "no ":""),
5304 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
5305 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
5306 (dev->flags&DF_WILDCARD? "wildcard,":""),
5307 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
5308 (dev->flags&DF_REBID? "rebiddable,":""),
5309 (dev->flags&DF_SUSPENDED? "suspended,":""),
5310 (dev->ivars? "":"no "),
5311 (dev->softc? "":"no "),
5316 print_device(device_t dev, int indent)
5321 print_device_short(dev, indent);
5323 indentprintf(("Parent:\n"));
5324 print_device_short(dev->parent, indent+1);
5325 indentprintf(("Driver:\n"));
5326 print_driver_short(dev->driver, indent+1);
5327 indentprintf(("Devclass:\n"));
5328 print_devclass_short(dev->devclass, indent+1);
5332 print_device_tree_short(device_t dev, int indent)
5333 /* print the device and all its children (indented) */
5340 print_device_short(dev, indent);
5342 TAILQ_FOREACH(child, &dev->children, link) {
5343 print_device_tree_short(child, indent+1);
5348 print_device_tree(device_t dev, int indent)
5349 /* print the device and all its children (indented) */
5356 print_device(dev, indent);
5358 TAILQ_FOREACH(child, &dev->children, link) {
5359 print_device_tree(child, indent+1);
5364 print_driver_short(driver_t *driver, int indent)
5369 indentprintf(("driver %s: softc size = %zd\n",
5370 driver->name, driver->size));
5374 print_driver(driver_t *driver, int indent)
5379 print_driver_short(driver, indent);
5383 print_driver_list(driver_list_t drivers, int indent)
5385 driverlink_t driver;
5387 TAILQ_FOREACH(driver, &drivers, link) {
5388 print_driver(driver->driver, indent);
5393 print_devclass_short(devclass_t dc, int indent)
5398 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
5402 print_devclass(devclass_t dc, int indent)
5409 print_devclass_short(dc, indent);
5410 indentprintf(("Drivers:\n"));
5411 print_driver_list(dc->drivers, indent+1);
5413 indentprintf(("Devices:\n"));
5414 for (i = 0; i < dc->maxunit; i++)
5416 print_device(dc->devices[i], indent+1);
5420 print_devclass_list_short(void)
5424 printf("Short listing of devclasses, drivers & devices:\n");
5425 TAILQ_FOREACH(dc, &devclasses, link) {
5426 print_devclass_short(dc, 0);
5431 print_devclass_list(void)
5435 printf("Full listing of devclasses, drivers & devices:\n");
5436 TAILQ_FOREACH(dc, &devclasses, link) {
5437 print_devclass(dc, 0);
5444 * User-space access to the device tree.
5446 * We implement a small set of nodes:
5448 * hw.bus Single integer read method to obtain the
5449 * current generation count.
5450 * hw.bus.devices Reads the entire device tree in flat space.
5451 * hw.bus.rman Resource manager interface
5453 * We might like to add the ability to scan devclasses and/or drivers to
5454 * determine what else is currently loaded/available.
5458 sysctl_bus_info(SYSCTL_HANDLER_ARGS)
5460 struct u_businfo ubus;
5462 ubus.ub_version = BUS_USER_VERSION;
5463 ubus.ub_generation = bus_data_generation;
5465 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
5467 SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD |
5468 CTLFLAG_MPSAFE, NULL, 0, sysctl_bus_info, "S,u_businfo",
5469 "bus-related data");
5472 sysctl_devices(SYSCTL_HANDLER_ARGS)
5474 int *name = (int *)arg1;
5475 u_int namelen = arg2;
5478 struct u_device *udev;
5485 if (bus_data_generation_check(name[0]))
5491 * Scan the list of devices, looking for the requested index.
5493 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5501 * Populate the return item, careful not to overflow the buffer.
5503 udev = malloc(sizeof(*udev), M_BUS, M_WAITOK | M_ZERO);
5506 udev->dv_handle = (uintptr_t)dev;
5507 udev->dv_parent = (uintptr_t)dev->parent;
5508 udev->dv_devflags = dev->devflags;
5509 udev->dv_flags = dev->flags;
5510 udev->dv_state = dev->state;
5511 walker = udev->dv_fields;
5512 ep = walker + sizeof(udev->dv_fields);
5514 if ((src) == NULL) \
5517 strlcpy(walker, (src), ep - walker); \
5518 walker += strlen(walker) + 1; \
5526 CP(dev->driver != NULL ? dev->driver->name : NULL);
5527 bus_child_pnpinfo_str(dev, walker, ep - walker);
5528 walker += strlen(walker) + 1;
5531 bus_child_location_str(dev, walker, ep - walker);
5532 walker += strlen(walker) + 1;
5538 error = SYSCTL_OUT(req, udev, sizeof(*udev));
5543 SYSCTL_NODE(_hw_bus, OID_AUTO, devices,
5544 CTLFLAG_RD | CTLFLAG_NEEDGIANT, sysctl_devices,
5545 "system device tree");
5548 bus_data_generation_check(int generation)
5550 if (generation != bus_data_generation)
5553 /* XXX generate optimised lists here? */
5558 bus_data_generation_update(void)
5560 atomic_add_int(&bus_data_generation, 1);
5564 bus_free_resource(device_t dev, int type, struct resource *r)
5568 return (bus_release_resource(dev, type, rman_get_rid(r), r));
5572 device_lookup_by_name(const char *name)
5576 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5577 if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0)
5584 * /dev/devctl2 implementation. The existing /dev/devctl device has
5585 * implicit semantics on open, so it could not be reused for this.
5586 * Another option would be to call this /dev/bus?
5589 find_device(struct devreq *req, device_t *devp)
5594 * First, ensure that the name is nul terminated.
5596 if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL)
5600 * Second, try to find an attached device whose name matches
5603 dev = device_lookup_by_name(req->dr_name);
5609 /* Finally, give device enumerators a chance. */
5611 EVENTHANDLER_DIRECT_INVOKE(dev_lookup, req->dr_name, &dev);
5619 driver_exists(device_t bus, const char *driver)
5623 for (dc = bus->devclass; dc != NULL; dc = dc->parent) {
5624 if (devclass_find_driver_internal(dc, driver) != NULL)
5631 device_gen_nomatch(device_t dev)
5635 if (dev->flags & DF_NEEDNOMATCH &&
5636 dev->state == DS_NOTPRESENT) {
5637 BUS_PROBE_NOMATCH(dev->parent, dev);
5639 dev->flags |= DF_DONENOMATCH;
5641 dev->flags &= ~DF_NEEDNOMATCH;
5642 TAILQ_FOREACH(child, &dev->children, link) {
5643 device_gen_nomatch(child);
5648 device_do_deferred_actions(void)
5654 * Walk through the devclasses to find all the drivers we've tagged as
5655 * deferred during the freeze and call the driver added routines. They
5656 * have already been added to the lists in the background, so the driver
5657 * added routines that trigger a probe will have all the right bidders
5658 * for the probe auction.
5660 TAILQ_FOREACH(dc, &devclasses, link) {
5661 TAILQ_FOREACH(dl, &dc->drivers, link) {
5662 if (dl->flags & DL_DEFERRED_PROBE) {
5663 devclass_driver_added(dc, dl->driver);
5664 dl->flags &= ~DL_DEFERRED_PROBE;
5670 * We also defer no-match events during a freeze. Walk the tree and
5671 * generate all the pent-up events that are still relevant.
5673 device_gen_nomatch(root_bus);
5674 bus_data_generation_update();
5678 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
5685 /* Locate the device to control. */
5687 req = (struct devreq *)data;
5695 case DEV_SET_DRIVER:
5696 case DEV_CLEAR_DRIVER:
5700 error = priv_check(td, PRIV_DRIVER);
5702 error = find_device(req, &dev);
5706 error = priv_check(td, PRIV_DRIVER);
5717 /* Perform the requested operation. */
5720 if (device_is_attached(dev) && (dev->flags & DF_REBID) == 0)
5722 else if (!device_is_enabled(dev))
5725 error = device_probe_and_attach(dev);
5728 if (!device_is_attached(dev)) {
5732 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5733 error = device_quiesce(dev);
5737 error = device_detach(dev);
5740 if (device_is_enabled(dev)) {
5746 * If the device has been probed but not attached (e.g.
5747 * when it has been disabled by a loader hint), just
5748 * attach the device rather than doing a full probe.
5751 if (device_is_alive(dev)) {
5753 * If the device was disabled via a hint, clear
5756 if (resource_disabled(dev->driver->name, dev->unit))
5757 resource_unset_value(dev->driver->name,
5758 dev->unit, "disabled");
5759 error = device_attach(dev);
5761 error = device_probe_and_attach(dev);
5764 if (!device_is_enabled(dev)) {
5769 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5770 error = device_quiesce(dev);
5776 * Force DF_FIXEDCLASS on around detach to preserve
5777 * the existing name.
5780 dev->flags |= DF_FIXEDCLASS;
5781 error = device_detach(dev);
5782 if (!(old & DF_FIXEDCLASS))
5783 dev->flags &= ~DF_FIXEDCLASS;
5785 device_disable(dev);
5788 if (device_is_suspended(dev)) {
5792 if (device_get_parent(dev) == NULL) {
5796 error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev);
5799 if (!device_is_suspended(dev)) {
5803 if (device_get_parent(dev) == NULL) {
5807 error = BUS_RESUME_CHILD(device_get_parent(dev), dev);
5809 case DEV_SET_DRIVER: {
5813 error = copyinstr(req->dr_data, driver, sizeof(driver), NULL);
5816 if (driver[0] == '\0') {
5820 if (dev->devclass != NULL &&
5821 strcmp(driver, dev->devclass->name) == 0)
5822 /* XXX: Could possibly force DF_FIXEDCLASS on? */
5826 * Scan drivers for this device's bus looking for at
5827 * least one matching driver.
5829 if (dev->parent == NULL) {
5833 if (!driver_exists(dev->parent, driver)) {
5837 dc = devclass_create(driver);
5843 /* Detach device if necessary. */
5844 if (device_is_attached(dev)) {
5845 if (req->dr_flags & DEVF_SET_DRIVER_DETACH)
5846 error = device_detach(dev);
5853 /* Clear any previously-fixed device class and unit. */
5854 if (dev->flags & DF_FIXEDCLASS)
5855 devclass_delete_device(dev->devclass, dev);
5856 dev->flags |= DF_WILDCARD;
5859 /* Force the new device class. */
5860 error = devclass_add_device(dc, dev);
5863 dev->flags |= DF_FIXEDCLASS;
5864 error = device_probe_and_attach(dev);
5867 case DEV_CLEAR_DRIVER:
5868 if (!(dev->flags & DF_FIXEDCLASS)) {
5872 if (device_is_attached(dev)) {
5873 if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH)
5874 error = device_detach(dev);
5881 dev->flags &= ~DF_FIXEDCLASS;
5882 dev->flags |= DF_WILDCARD;
5883 devclass_delete_device(dev->devclass, dev);
5884 error = device_probe_and_attach(dev);
5887 if (!device_is_attached(dev)) {
5891 error = BUS_RESCAN(dev);
5896 parent = device_get_parent(dev);
5897 if (parent == NULL) {
5901 if (!(req->dr_flags & DEVF_FORCE_DELETE)) {
5902 if (bus_child_present(dev) != 0) {
5908 error = device_delete_child(parent, dev);
5915 device_frozen = true;
5921 device_do_deferred_actions();
5922 device_frozen = false;
5926 if ((req->dr_flags & ~(DEVF_RESET_DETACH)) != 0) {
5930 error = BUS_RESET_CHILD(device_get_parent(dev), dev,
5938 static struct cdevsw devctl2_cdevsw = {
5939 .d_version = D_VERSION,
5940 .d_ioctl = devctl2_ioctl,
5941 .d_name = "devctl2",
5947 make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL,
5948 UID_ROOT, GID_WHEEL, 0600, "devctl2");
5952 * APIs to manage deprecation and obsolescence.
5954 static int obsolete_panic = 0;
5955 SYSCTL_INT(_debug, OID_AUTO, obsolete_panic, CTLFLAG_RWTUN, &obsolete_panic, 0,
5956 "Panic when obsolete features are used (0 = never, 1 = if osbolete, "
5957 "2 = if deprecated)");
5960 gone_panic(int major, int running, const char *msg)
5962 switch (obsolete_panic)
5967 if (running < major)
5976 _gone_in(int major, const char *msg)
5978 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
5979 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
5980 printf("Obsolete code will be removed soon: %s\n", msg);
5982 printf("Deprecated code (to be removed in FreeBSD %d): %s\n",
5987 _gone_in_dev(device_t dev, int major, const char *msg)
5989 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
5990 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
5992 "Obsolete code will be removed soon: %s\n", msg);
5995 "Deprecated code (to be removed in FreeBSD %d): %s\n",
6000 DB_SHOW_COMMAND(device, db_show_device)
6007 dev = (device_t)addr;
6009 db_printf("name: %s\n", device_get_nameunit(dev));
6010 db_printf(" driver: %s\n", DRIVERNAME(dev->driver));
6011 db_printf(" class: %s\n", DEVCLANAME(dev->devclass));
6012 db_printf(" addr: %p\n", dev);
6013 db_printf(" parent: %p\n", dev->parent);
6014 db_printf(" softc: %p\n", dev->softc);
6015 db_printf(" ivars: %p\n", dev->ivars);
6018 DB_SHOW_ALL_COMMAND(devices, db_show_all_devices)
6022 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
6023 db_show_device((db_expr_t)dev, true, count, modif);