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)
264 device_t dev = (device_t)arg1;
267 sbuf_new_for_sysctl(&sb, NULL, 1024, req);
269 case DEVICE_SYSCTL_DESC:
270 sbuf_cat(&sb, dev->desc ? dev->desc : "");
272 case DEVICE_SYSCTL_DRIVER:
273 sbuf_cat(&sb, dev->driver ? dev->driver->name : "");
275 case DEVICE_SYSCTL_LOCATION:
276 bus_child_location_sb(dev, &sb);
278 case DEVICE_SYSCTL_PNPINFO:
279 bus_child_pnpinfo_sb(dev, &sb);
281 case DEVICE_SYSCTL_PARENT:
282 sbuf_cat(&sb, dev->parent ? dev->parent->nameunit : "");
288 error = sbuf_finish(&sb);
294 device_sysctl_init(device_t dev)
296 devclass_t dc = dev->devclass;
299 if (dev->sysctl_tree != NULL)
301 devclass_sysctl_init(dc);
302 sysctl_ctx_init(&dev->sysctl_ctx);
303 dev->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&dev->sysctl_ctx,
304 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
305 dev->nameunit + strlen(dc->name),
306 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", "device_index");
307 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
308 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
309 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
310 "device description");
311 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
313 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
314 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
315 "device driver name");
316 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
317 OID_AUTO, "%location",
318 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
319 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
320 "device location relative to parent");
321 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
322 OID_AUTO, "%pnpinfo",
323 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
324 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
325 "device identification");
326 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
328 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
329 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
331 if (bus_get_domain(dev, &domain) == 0)
332 SYSCTL_ADD_INT(&dev->sysctl_ctx,
333 SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain",
334 CTLFLAG_RD, NULL, domain, "NUMA domain");
338 device_sysctl_update(device_t dev)
340 devclass_t dc = dev->devclass;
342 if (dev->sysctl_tree == NULL)
344 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
348 device_sysctl_fini(device_t dev)
350 if (dev->sysctl_tree == NULL)
352 sysctl_ctx_free(&dev->sysctl_ctx);
353 dev->sysctl_tree = NULL;
357 * /dev/devctl implementation
361 * This design allows only one reader for /dev/devctl. This is not desirable
362 * in the long run, but will get a lot of hair out of this implementation.
363 * Maybe we should make this device a clonable device.
365 * Also note: we specifically do not attach a device to the device_t tree
366 * to avoid potential chicken and egg problems. One could argue that all
367 * of this belongs to the root node.
370 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
371 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
372 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
373 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN |
374 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
376 static d_open_t devopen;
377 static d_close_t devclose;
378 static d_read_t devread;
379 static d_ioctl_t devioctl;
380 static d_poll_t devpoll;
381 static d_kqfilter_t devkqfilter;
383 static struct cdevsw dev_cdevsw = {
384 .d_version = D_VERSION,
390 .d_kqfilter = devkqfilter,
394 #define DEVCTL_BUFFER (1024 - sizeof(void *))
395 struct dev_event_info {
396 STAILQ_ENTRY(dev_event_info) dei_link;
397 char dei_data[DEVCTL_BUFFER];
400 STAILQ_HEAD(devq, dev_event_info);
402 static struct dev_softc {
415 static void filt_devctl_detach(struct knote *kn);
416 static int filt_devctl_read(struct knote *kn, long hint);
418 struct filterops devctl_rfiltops = {
420 .f_detach = filt_devctl_detach,
421 .f_event = filt_devctl_read,
424 static struct cdev *devctl_dev;
429 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
430 UID_ROOT, GID_WHEEL, 0600, "devctl");
431 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
432 cv_init(&devsoftc.cv, "dev cv");
433 STAILQ_INIT(&devsoftc.devq);
434 knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
435 if (devctl_queue_length > 0) {
436 devsoftc.zone = uma_zcreate("DEVCTL", sizeof(struct dev_event_info),
437 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
438 uma_prealloc(devsoftc.zone, devctl_queue_length);
444 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
446 mtx_lock(&devsoftc.mtx);
447 if (devsoftc.inuse) {
448 mtx_unlock(&devsoftc.mtx);
453 mtx_unlock(&devsoftc.mtx);
458 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
460 mtx_lock(&devsoftc.mtx);
462 devsoftc.nonblock = 0;
464 cv_broadcast(&devsoftc.cv);
465 funsetown(&devsoftc.sigio);
466 mtx_unlock(&devsoftc.mtx);
471 * The read channel for this device is used to report changes to
472 * userland in realtime. We are required to free the data as well as
473 * the n1 object because we allocate them separately. Also note that
474 * we return one record at a time. If you try to read this device a
475 * character at a time, you will lose the rest of the data. Listening
476 * programs are expected to cope.
479 devread(struct cdev *dev, struct uio *uio, int ioflag)
481 struct dev_event_info *n1;
484 mtx_lock(&devsoftc.mtx);
485 while (STAILQ_EMPTY(&devsoftc.devq)) {
486 if (devsoftc.nonblock) {
487 mtx_unlock(&devsoftc.mtx);
490 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
493 * Need to translate ERESTART to EINTR here? -- jake
495 mtx_unlock(&devsoftc.mtx);
499 n1 = STAILQ_FIRST(&devsoftc.devq);
500 STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
502 mtx_unlock(&devsoftc.mtx);
503 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
504 uma_zfree(devsoftc.zone, n1);
509 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
514 devsoftc.nonblock = 1;
516 devsoftc.nonblock = 0;
525 return fsetown(*(int *)data, &devsoftc.sigio);
527 *(int *)data = fgetown(&devsoftc.sigio);
530 /* (un)Support for other fcntl() calls. */
541 devpoll(struct cdev *dev, int events, struct thread *td)
545 mtx_lock(&devsoftc.mtx);
546 if (events & (POLLIN | POLLRDNORM)) {
547 if (!STAILQ_EMPTY(&devsoftc.devq))
548 revents = events & (POLLIN | POLLRDNORM);
550 selrecord(td, &devsoftc.sel);
552 mtx_unlock(&devsoftc.mtx);
558 devkqfilter(struct cdev *dev, struct knote *kn)
562 if (kn->kn_filter == EVFILT_READ) {
563 kn->kn_fop = &devctl_rfiltops;
564 knlist_add(&devsoftc.sel.si_note, kn, 0);
572 filt_devctl_detach(struct knote *kn)
574 knlist_remove(&devsoftc.sel.si_note, kn, 0);
578 filt_devctl_read(struct knote *kn, long hint)
580 kn->kn_data = devsoftc.queued;
581 return (kn->kn_data != 0);
585 * @brief Return whether the userland process is running
588 devctl_process_running(void)
590 return (devsoftc.inuse == 1);
593 static struct dev_event_info *
594 devctl_alloc_dei(void)
596 struct dev_event_info *dei = NULL;
598 mtx_lock(&devsoftc.mtx);
599 if (devctl_queue_length == 0)
601 if (devctl_queue_length == devsoftc.queued) {
602 dei = STAILQ_FIRST(&devsoftc.devq);
603 STAILQ_REMOVE_HEAD(&devsoftc.devq, dei_link);
606 /* dei can't be NULL -- we know we have at least one in the zone */
607 dei = uma_zalloc(devsoftc.zone, M_NOWAIT);
610 *dei->dei_data = '\0';
612 mtx_unlock(&devsoftc.mtx);
616 static struct dev_event_info *
617 devctl_alloc_dei_sb(struct sbuf *sb)
619 struct dev_event_info *dei;
621 dei = devctl_alloc_dei();
623 sbuf_new(sb, dei->dei_data, sizeof(dei->dei_data), SBUF_FIXEDLEN);
628 devctl_free_dei(struct dev_event_info *dei)
630 uma_zfree(devsoftc.zone, dei);
634 devctl_queue(struct dev_event_info *dei)
636 mtx_lock(&devsoftc.mtx);
637 STAILQ_INSERT_TAIL(&devsoftc.devq, dei, dei_link);
639 cv_broadcast(&devsoftc.cv);
640 KNOTE_LOCKED(&devsoftc.sel.si_note, 0);
641 mtx_unlock(&devsoftc.mtx);
642 selwakeup(&devsoftc.sel);
643 if (devsoftc.async && devsoftc.sigio != NULL)
644 pgsigio(&devsoftc.sigio, SIGIO, 0);
648 * @brief Send a 'notification' to userland, using standard ways
651 devctl_notify(const char *system, const char *subsystem, const char *type,
654 struct dev_event_info *dei;
657 if (system == NULL || subsystem == NULL || type == NULL)
659 dei = devctl_alloc_dei_sb(&sb);
662 sbuf_cpy(&sb, "!system=");
663 sbuf_cat(&sb, system);
664 sbuf_cat(&sb, " subsystem=");
665 sbuf_cat(&sb, subsystem);
666 sbuf_cat(&sb, " type=");
672 sbuf_putc(&sb, '\n');
673 if (sbuf_finish(&sb) != 0)
674 devctl_free_dei(dei); /* overflow -> drop it */
680 * Common routine that tries to make sending messages as easy as possible.
681 * We allocate memory for the data, copy strings into that, but do not
682 * free it unless there's an error. The dequeue part of the driver should
683 * free the data. We don't send data when the device is disabled. We do
684 * send data, even when we have no listeners, because we wish to avoid
685 * races relating to startup and restart of listening applications.
687 * devaddq is designed to string together the type of event, with the
688 * object of that event, plus the plug and play info and location info
689 * for that event. This is likely most useful for devices, but less
690 * useful for other consumers of this interface. Those should use
691 * the devctl_notify() interface instead.
694 * ${type}${what} at $(location dev) $(pnp-info dev) on $(parent dev)
697 devaddq(const char *type, const char *what, device_t dev)
699 struct dev_event_info *dei;
703 dei = devctl_alloc_dei_sb(&sb);
708 sbuf_cat(&sb, " at ");
710 /* Add in the location */
711 bus_child_location_sb(dev, &sb);
715 bus_child_pnpinfo_sb(dev, &sb);
717 /* Get the parent of this device, or / if high enough in the tree. */
718 if (device_get_parent(dev) == NULL)
719 parstr = "."; /* Or '/' ? */
721 parstr = device_get_nameunit(device_get_parent(dev));
722 sbuf_cat(&sb, " on ");
723 sbuf_cat(&sb, parstr);
724 sbuf_putc(&sb, '\n');
725 if (sbuf_finish(&sb) != 0)
730 devctl_free_dei(dei);
734 * A device was added to the tree. We are called just after it successfully
735 * attaches (that is, probe and attach success for this device). No call
736 * is made if a device is merely parented into the tree. See devnomatch
737 * if probe fails. If attach fails, no notification is sent (but maybe
738 * we should have a different message for this).
741 devadded(device_t dev)
743 devaddq("+", device_get_nameunit(dev), dev);
747 * A device was removed from the tree. We are called just before this
751 devremoved(device_t dev)
753 devaddq("-", device_get_nameunit(dev), dev);
757 * Called when there's no match for this device. This is only called
758 * the first time that no match happens, so we don't keep getting this
759 * message. Should that prove to be undesirable, we can change it.
760 * This is called when all drivers that can attach to a given bus
761 * decline to accept this device. Other errors may not be detected.
764 devnomatch(device_t dev)
766 devaddq("?", "", dev);
770 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
774 q = devctl_queue_length;
775 error = sysctl_handle_int(oidp, &q, 0, req);
776 if (error || !req->newptr)
782 * When set as a tunable, we've not yet initialized the mutex.
783 * It is safe to just assign to devctl_queue_length and return
784 * as we're racing no one. We'll use whatever value set in
787 if (!mtx_initialized(&devsoftc.mtx)) {
788 devctl_queue_length = q;
793 * XXX It's hard to grow or shrink the UMA zone. Only allow
794 * disabling the queue size for the moment until underlying
795 * UMA issues can be sorted out.
799 if (q == devctl_queue_length)
801 mtx_lock(&devsoftc.mtx);
802 devctl_queue_length = 0;
803 uma_zdestroy(devsoftc.zone);
805 mtx_unlock(&devsoftc.mtx);
810 * @brief safely quotes strings that might have double quotes in them.
812 * The devctl protocol relies on quoted strings having matching quotes.
813 * This routine quotes any internal quotes so the resulting string
814 * is safe to pass to snprintf to construct, for example pnp info strings.
816 * @param sb sbuf to place the characters into
817 * @param src Original buffer.
820 devctl_safe_quote_sb(struct sbuf *sb, const char *src)
822 while (*src != '\0') {
823 if (*src == '"' || *src == '\\')
825 sbuf_putc(sb, *src++);
829 /* End of /dev/devctl code */
831 static TAILQ_HEAD(,device) bus_data_devices;
832 static int bus_data_generation = 1;
834 static kobj_method_t null_methods[] = {
838 DEFINE_CLASS(null, null_methods, 0);
841 * Bus pass implementation
844 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
845 int bus_current_pass = BUS_PASS_ROOT;
849 * @brief Register the pass level of a new driver attachment
851 * Register a new driver attachment's pass level. If no driver
852 * attachment with the same pass level has been added, then @p new
853 * will be added to the global passes list.
855 * @param new the new driver attachment
858 driver_register_pass(struct driverlink *new)
860 struct driverlink *dl;
862 /* We only consider pass numbers during boot. */
863 if (bus_current_pass == BUS_PASS_DEFAULT)
867 * Walk the passes list. If we already know about this pass
868 * then there is nothing to do. If we don't, then insert this
869 * driver link into the list.
871 TAILQ_FOREACH(dl, &passes, passlink) {
872 if (dl->pass < new->pass)
874 if (dl->pass == new->pass)
876 TAILQ_INSERT_BEFORE(dl, new, passlink);
879 TAILQ_INSERT_TAIL(&passes, new, passlink);
883 * @brief Raise the current bus pass
885 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
886 * method on the root bus to kick off a new device tree scan for each
887 * new pass level that has at least one driver.
890 bus_set_pass(int pass)
892 struct driverlink *dl;
894 if (bus_current_pass > pass)
895 panic("Attempt to lower bus pass level");
897 TAILQ_FOREACH(dl, &passes, passlink) {
898 /* Skip pass values below the current pass level. */
899 if (dl->pass <= bus_current_pass)
903 * Bail once we hit a driver with a pass level that is
910 * Raise the pass level to the next level and rescan
913 bus_current_pass = dl->pass;
914 BUS_NEW_PASS(root_bus);
918 * If there isn't a driver registered for the requested pass,
919 * then bus_current_pass might still be less than 'pass'. Set
920 * it to 'pass' in that case.
922 if (bus_current_pass < pass)
923 bus_current_pass = pass;
924 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
928 * Devclass implementation
931 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
935 * @brief Find or create a device class
937 * If a device class with the name @p classname exists, return it,
938 * otherwise if @p create is non-zero create and return a new device
941 * If @p parentname is non-NULL, the parent of the devclass is set to
942 * the devclass of that name.
944 * @param classname the devclass name to find or create
945 * @param parentname the parent devclass name or @c NULL
946 * @param create non-zero to create a devclass
949 devclass_find_internal(const char *classname, const char *parentname,
954 PDEBUG(("looking for %s", classname));
958 TAILQ_FOREACH(dc, &devclasses, link) {
959 if (!strcmp(dc->name, classname))
964 PDEBUG(("creating %s", classname));
965 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
966 M_BUS, M_NOWAIT | M_ZERO);
970 dc->name = (char*) (dc + 1);
971 strcpy(dc->name, classname);
972 TAILQ_INIT(&dc->drivers);
973 TAILQ_INSERT_TAIL(&devclasses, dc, link);
975 bus_data_generation_update();
979 * If a parent class is specified, then set that as our parent so
980 * that this devclass will support drivers for the parent class as
981 * well. If the parent class has the same name don't do this though
982 * as it creates a cycle that can trigger an infinite loop in
983 * device_probe_child() if a device exists for which there is no
986 if (parentname && dc && !dc->parent &&
987 strcmp(classname, parentname) != 0) {
988 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
989 dc->parent->flags |= DC_HAS_CHILDREN;
996 * @brief Create a device class
998 * If a device class with the name @p classname exists, return it,
999 * otherwise create and return a new device class.
1001 * @param classname the devclass name to find or create
1004 devclass_create(const char *classname)
1006 return (devclass_find_internal(classname, NULL, TRUE));
1010 * @brief Find a device class
1012 * If a device class with the name @p classname exists, return it,
1013 * otherwise return @c NULL.
1015 * @param classname the devclass name to find
1018 devclass_find(const char *classname)
1020 return (devclass_find_internal(classname, NULL, FALSE));
1024 * @brief Register that a device driver has been added to a devclass
1026 * Register that a device driver has been added to a devclass. This
1027 * is called by devclass_add_driver to accomplish the recursive
1028 * notification of all the children classes of dc, as well as dc.
1029 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1032 * We do a full search here of the devclass list at each iteration
1033 * level to save storing children-lists in the devclass structure. If
1034 * we ever move beyond a few dozen devices doing this, we may need to
1037 * @param dc the devclass to edit
1038 * @param driver the driver that was just added
1041 devclass_driver_added(devclass_t dc, driver_t *driver)
1047 * Call BUS_DRIVER_ADDED for any existing buses in this class.
1049 for (i = 0; i < dc->maxunit; i++)
1050 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1051 BUS_DRIVER_ADDED(dc->devices[i], driver);
1054 * Walk through the children classes. Since we only keep a
1055 * single parent pointer around, we walk the entire list of
1056 * devclasses looking for children. We set the
1057 * DC_HAS_CHILDREN flag when a child devclass is created on
1058 * the parent, so we only walk the list for those devclasses
1059 * that have children.
1061 if (!(dc->flags & DC_HAS_CHILDREN))
1064 TAILQ_FOREACH(dc, &devclasses, link) {
1065 if (dc->parent == parent)
1066 devclass_driver_added(dc, driver);
1071 * @brief Add a device driver to a device class
1073 * Add a device driver to a devclass. This is normally called
1074 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1075 * all devices in the devclass will be called to allow them to attempt
1076 * to re-probe any unmatched children.
1078 * @param dc the devclass to edit
1079 * @param driver the driver to register
1082 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1085 const char *parentname;
1087 PDEBUG(("%s", DRIVERNAME(driver)));
1089 /* Don't allow invalid pass values. */
1090 if (pass <= BUS_PASS_ROOT)
1093 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1098 * Compile the driver's methods. Also increase the reference count
1099 * so that the class doesn't get freed when the last instance
1100 * goes. This means we can safely use static methods and avoids a
1101 * double-free in devclass_delete_driver.
1103 kobj_class_compile((kobj_class_t) driver);
1106 * If the driver has any base classes, make the
1107 * devclass inherit from the devclass of the driver's
1108 * first base class. This will allow the system to
1109 * search for drivers in both devclasses for children
1110 * of a device using this driver.
1112 if (driver->baseclasses)
1113 parentname = driver->baseclasses[0]->name;
1116 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1118 dl->driver = driver;
1119 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1120 driver->refs++; /* XXX: kobj_mtx */
1122 driver_register_pass(dl);
1124 if (device_frozen) {
1125 dl->flags |= DL_DEFERRED_PROBE;
1127 devclass_driver_added(dc, driver);
1129 bus_data_generation_update();
1134 * @brief Register that a device driver has been deleted from a devclass
1136 * Register that a device driver has been removed from a devclass.
1137 * This is called by devclass_delete_driver to accomplish the
1138 * recursive notification of all the children classes of busclass, as
1139 * well as busclass. Each layer will attempt to detach the driver
1140 * from any devices that are children of the bus's devclass. The function
1141 * will return an error if a device fails to detach.
1143 * We do a full search here of the devclass list at each iteration
1144 * level to save storing children-lists in the devclass structure. If
1145 * we ever move beyond a few dozen devices doing this, we may need to
1148 * @param busclass the devclass of the parent bus
1149 * @param dc the devclass of the driver being deleted
1150 * @param driver the driver being deleted
1153 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1160 * Disassociate from any devices. We iterate through all the
1161 * devices in the devclass of the driver and detach any which are
1162 * using the driver and which have a parent in the devclass which
1163 * we are deleting from.
1165 * Note that since a driver can be in multiple devclasses, we
1166 * should not detach devices which are not children of devices in
1167 * the affected devclass.
1169 * If we're frozen, we don't generate NOMATCH events. Mark to
1172 for (i = 0; i < dc->maxunit; i++) {
1173 if (dc->devices[i]) {
1174 dev = dc->devices[i];
1175 if (dev->driver == driver && dev->parent &&
1176 dev->parent->devclass == busclass) {
1177 if ((error = device_detach(dev)) != 0)
1179 if (device_frozen) {
1180 dev->flags &= ~DF_DONENOMATCH;
1181 dev->flags |= DF_NEEDNOMATCH;
1183 BUS_PROBE_NOMATCH(dev->parent, dev);
1185 dev->flags |= DF_DONENOMATCH;
1192 * Walk through the children classes. Since we only keep a
1193 * single parent pointer around, we walk the entire list of
1194 * devclasses looking for children. We set the
1195 * DC_HAS_CHILDREN flag when a child devclass is created on
1196 * the parent, so we only walk the list for those devclasses
1197 * that have children.
1199 if (!(busclass->flags & DC_HAS_CHILDREN))
1202 TAILQ_FOREACH(busclass, &devclasses, link) {
1203 if (busclass->parent == parent) {
1204 error = devclass_driver_deleted(busclass, dc, driver);
1213 * @brief Delete a device driver from a device class
1215 * Delete a device driver from a devclass. This is normally called
1216 * automatically by DRIVER_MODULE().
1218 * If the driver is currently attached to any devices,
1219 * devclass_delete_driver() will first attempt to detach from each
1220 * device. If one of the detach calls fails, the driver will not be
1223 * @param dc the devclass to edit
1224 * @param driver the driver to unregister
1227 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1229 devclass_t dc = devclass_find(driver->name);
1233 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1239 * Find the link structure in the bus' list of drivers.
1241 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1242 if (dl->driver == driver)
1247 PDEBUG(("%s not found in %s list", driver->name,
1252 error = devclass_driver_deleted(busclass, dc, driver);
1256 TAILQ_REMOVE(&busclass->drivers, dl, link);
1261 if (driver->refs == 0)
1262 kobj_class_free((kobj_class_t) driver);
1264 bus_data_generation_update();
1269 * @brief Quiesces a set of device drivers from a device class
1271 * Quiesce a device driver from a devclass. This is normally called
1272 * automatically by DRIVER_MODULE().
1274 * If the driver is currently attached to any devices,
1275 * devclass_quiesece_driver() will first attempt to quiesce each
1278 * @param dc the devclass to edit
1279 * @param driver the driver to unregister
1282 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1284 devclass_t dc = devclass_find(driver->name);
1290 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1296 * Find the link structure in the bus' list of drivers.
1298 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1299 if (dl->driver == driver)
1304 PDEBUG(("%s not found in %s list", driver->name,
1310 * Quiesce all devices. We iterate through all the devices in
1311 * the devclass of the driver and quiesce any which are using
1312 * the driver and which have a parent in the devclass which we
1315 * Note that since a driver can be in multiple devclasses, we
1316 * should not quiesce devices which are not children of
1317 * devices in the affected devclass.
1319 for (i = 0; i < dc->maxunit; i++) {
1320 if (dc->devices[i]) {
1321 dev = dc->devices[i];
1322 if (dev->driver == driver && dev->parent &&
1323 dev->parent->devclass == busclass) {
1324 if ((error = device_quiesce(dev)) != 0)
1337 devclass_find_driver_internal(devclass_t dc, const char *classname)
1341 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1343 TAILQ_FOREACH(dl, &dc->drivers, link) {
1344 if (!strcmp(dl->driver->name, classname))
1348 PDEBUG(("not found"));
1353 * @brief Return the name of the devclass
1356 devclass_get_name(devclass_t dc)
1362 * @brief Find a device given a unit number
1364 * @param dc the devclass to search
1365 * @param unit the unit number to search for
1367 * @returns the device with the given unit number or @c
1368 * NULL if there is no such device
1371 devclass_get_device(devclass_t dc, int unit)
1373 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1375 return (dc->devices[unit]);
1379 * @brief Find the softc field of a device given a unit number
1381 * @param dc the devclass to search
1382 * @param unit the unit number to search for
1384 * @returns the softc field of the device with the given
1385 * unit number or @c NULL if there is no such
1389 devclass_get_softc(devclass_t dc, int unit)
1393 dev = devclass_get_device(dc, unit);
1397 return (device_get_softc(dev));
1401 * @brief Get a list of devices in the devclass
1403 * An array containing a list of all the devices in the given devclass
1404 * is allocated and returned in @p *devlistp. The number of devices
1405 * in the array is returned in @p *devcountp. The caller should free
1406 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1408 * @param dc the devclass to examine
1409 * @param devlistp points at location for array pointer return
1411 * @param devcountp points at location for array size return value
1414 * @retval ENOMEM the array allocation failed
1417 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1422 count = devclass_get_count(dc);
1423 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1428 for (i = 0; i < dc->maxunit; i++) {
1429 if (dc->devices[i]) {
1430 list[count] = dc->devices[i];
1442 * @brief Get a list of drivers in the devclass
1444 * An array containing a list of pointers to all the drivers in the
1445 * given devclass is allocated and returned in @p *listp. The number
1446 * of drivers in the array is returned in @p *countp. The caller should
1447 * free the array using @c free(p, M_TEMP).
1449 * @param dc the devclass to examine
1450 * @param listp gives location for array pointer return value
1451 * @param countp gives location for number of array elements
1455 * @retval ENOMEM the array allocation failed
1458 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1465 TAILQ_FOREACH(dl, &dc->drivers, link)
1467 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1472 TAILQ_FOREACH(dl, &dc->drivers, link) {
1473 list[count] = dl->driver;
1483 * @brief Get the number of devices in a devclass
1485 * @param dc the devclass to examine
1488 devclass_get_count(devclass_t dc)
1493 for (i = 0; i < dc->maxunit; i++)
1500 * @brief Get the maximum unit number used in a devclass
1502 * Note that this is one greater than the highest currently-allocated
1503 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1504 * that not even the devclass has been allocated yet.
1506 * @param dc the devclass to examine
1509 devclass_get_maxunit(devclass_t dc)
1513 return (dc->maxunit);
1517 * @brief Find a free unit number in a devclass
1519 * This function searches for the first unused unit number greater
1520 * that or equal to @p unit.
1522 * @param dc the devclass to examine
1523 * @param unit the first unit number to check
1526 devclass_find_free_unit(devclass_t dc, int unit)
1530 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1536 * @brief Set the parent of a devclass
1538 * The parent class is normally initialised automatically by
1541 * @param dc the devclass to edit
1542 * @param pdc the new parent devclass
1545 devclass_set_parent(devclass_t dc, devclass_t pdc)
1551 * @brief Get the parent of a devclass
1553 * @param dc the devclass to examine
1556 devclass_get_parent(devclass_t dc)
1558 return (dc->parent);
1561 struct sysctl_ctx_list *
1562 devclass_get_sysctl_ctx(devclass_t dc)
1564 return (&dc->sysctl_ctx);
1568 devclass_get_sysctl_tree(devclass_t dc)
1570 return (dc->sysctl_tree);
1575 * @brief Allocate a unit number
1577 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1578 * will do). The allocated unit number is returned in @p *unitp.
1580 * @param dc the devclass to allocate from
1581 * @param unitp points at the location for the allocated unit
1585 * @retval EEXIST the requested unit number is already allocated
1586 * @retval ENOMEM memory allocation failure
1589 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1594 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1596 /* Ask the parent bus if it wants to wire this device. */
1598 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1601 /* If we were given a wired unit number, check for existing device */
1604 if (unit >= 0 && unit < dc->maxunit &&
1605 dc->devices[unit] != NULL) {
1607 printf("%s: %s%d already exists; skipping it\n",
1608 dc->name, dc->name, *unitp);
1612 /* Unwired device, find the next available slot for it */
1614 for (unit = 0;; unit++) {
1615 /* If there is an "at" hint for a unit then skip it. */
1616 if (resource_string_value(dc->name, unit, "at", &s) ==
1620 /* If this device slot is already in use, skip it. */
1621 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1629 * We've selected a unit beyond the length of the table, so let's
1630 * extend the table to make room for all units up to and including
1633 if (unit >= dc->maxunit) {
1634 device_t *newlist, *oldlist;
1637 oldlist = dc->devices;
1638 newsize = roundup((unit + 1),
1639 MAX(1, MINALLOCSIZE / sizeof(device_t)));
1640 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1643 if (oldlist != NULL)
1644 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1645 bzero(newlist + dc->maxunit,
1646 sizeof(device_t) * (newsize - dc->maxunit));
1647 dc->devices = newlist;
1648 dc->maxunit = newsize;
1649 if (oldlist != NULL)
1650 free(oldlist, M_BUS);
1652 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1660 * @brief Add a device to a devclass
1662 * A unit number is allocated for the device (using the device's
1663 * preferred unit number if any) and the device is registered in the
1664 * devclass. This allows the device to be looked up by its unit
1665 * number, e.g. by decoding a dev_t minor number.
1667 * @param dc the devclass to add to
1668 * @param dev the device to add
1671 * @retval EEXIST the requested unit number is already allocated
1672 * @retval ENOMEM memory allocation failure
1675 devclass_add_device(devclass_t dc, device_t dev)
1679 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1681 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1684 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1688 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1689 free(dev->nameunit, M_BUS);
1690 dev->nameunit = NULL;
1693 dc->devices[dev->unit] = dev;
1695 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1702 * @brief Delete a device from a devclass
1704 * The device is removed from the devclass's device list and its unit
1707 * @param dc the devclass to delete from
1708 * @param dev the device to delete
1713 devclass_delete_device(devclass_t dc, device_t dev)
1718 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1720 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1721 panic("devclass_delete_device: inconsistent device class");
1722 dc->devices[dev->unit] = NULL;
1723 if (dev->flags & DF_WILDCARD)
1725 dev->devclass = NULL;
1726 free(dev->nameunit, M_BUS);
1727 dev->nameunit = NULL;
1734 * @brief Make a new device and add it as a child of @p parent
1736 * @param parent the parent of the new device
1737 * @param name the devclass name of the new device or @c NULL
1738 * to leave the devclass unspecified
1739 * @parem unit the unit number of the new device of @c -1 to
1740 * leave the unit number unspecified
1742 * @returns the new device
1745 make_device(device_t parent, const char *name, int unit)
1750 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1753 dc = devclass_find_internal(name, NULL, TRUE);
1755 printf("make_device: can't find device class %s\n",
1763 dev = malloc(sizeof(*dev), M_BUS, M_NOWAIT|M_ZERO);
1767 dev->parent = parent;
1768 TAILQ_INIT(&dev->children);
1769 kobj_init((kobj_t) dev, &null_class);
1771 dev->devclass = NULL;
1773 dev->nameunit = NULL;
1777 dev->flags = DF_ENABLED;
1780 dev->flags |= DF_WILDCARD;
1782 dev->flags |= DF_FIXEDCLASS;
1783 if (devclass_add_device(dc, dev)) {
1784 kobj_delete((kobj_t) dev, M_BUS);
1788 if (parent != NULL && device_has_quiet_children(parent))
1789 dev->flags |= DF_QUIET | DF_QUIET_CHILDREN;
1793 dev->state = DS_NOTPRESENT;
1795 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1796 bus_data_generation_update();
1803 * @brief Print a description of a device.
1806 device_print_child(device_t dev, device_t child)
1810 if (device_is_alive(child))
1811 retval += BUS_PRINT_CHILD(dev, child);
1813 retval += device_printf(child, " not found\n");
1819 * @brief Create a new device
1821 * This creates a new device and adds it as a child of an existing
1822 * parent device. The new device will be added after the last existing
1823 * child with order zero.
1825 * @param dev the device which will be the parent of the
1827 * @param name devclass name for new device or @c NULL if not
1829 * @param unit unit number for new device or @c -1 if not
1832 * @returns the new device
1835 device_add_child(device_t dev, const char *name, int unit)
1837 return (device_add_child_ordered(dev, 0, name, unit));
1841 * @brief Create a new device
1843 * This creates a new device and adds it as a child of an existing
1844 * parent device. The new device will be added after the last existing
1845 * child with the same order.
1847 * @param dev the device which will be the parent of the
1849 * @param order a value which is used to partially sort the
1850 * children of @p dev - devices created using
1851 * lower values of @p order appear first in @p
1852 * dev's list of children
1853 * @param name devclass name for new device or @c NULL if not
1855 * @param unit unit number for new device or @c -1 if not
1858 * @returns the new device
1861 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1866 PDEBUG(("%s at %s with order %u as unit %d",
1867 name, DEVICENAME(dev), order, unit));
1868 KASSERT(name != NULL || unit == -1,
1869 ("child device with wildcard name and specific unit number"));
1871 child = make_device(dev, name, unit);
1874 child->order = order;
1876 TAILQ_FOREACH(place, &dev->children, link) {
1877 if (place->order > order)
1883 * The device 'place' is the first device whose order is
1884 * greater than the new child.
1886 TAILQ_INSERT_BEFORE(place, child, link);
1889 * The new child's order is greater or equal to the order of
1890 * any existing device. Add the child to the tail of the list.
1892 TAILQ_INSERT_TAIL(&dev->children, child, link);
1895 bus_data_generation_update();
1900 * @brief Delete a device
1902 * This function deletes a device along with all of its children. If
1903 * the device currently has a driver attached to it, the device is
1904 * detached first using device_detach().
1906 * @param dev the parent device
1907 * @param child the device to delete
1910 * @retval non-zero a unit error code describing the error
1913 device_delete_child(device_t dev, device_t child)
1916 device_t grandchild;
1918 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1920 /* detach parent before deleting children, if any */
1921 if ((error = device_detach(child)) != 0)
1924 /* remove children second */
1925 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1926 error = device_delete_child(child, grandchild);
1931 if (child->devclass)
1932 devclass_delete_device(child->devclass, child);
1934 BUS_CHILD_DELETED(dev, child);
1935 TAILQ_REMOVE(&dev->children, child, link);
1936 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1937 kobj_delete((kobj_t) child, M_BUS);
1939 bus_data_generation_update();
1944 * @brief Delete all children devices of the given device, if any.
1946 * This function deletes all children devices of the given device, if
1947 * any, using the device_delete_child() function for each device it
1948 * finds. If a child device cannot be deleted, this function will
1949 * return an error code.
1951 * @param dev the parent device
1954 * @retval non-zero a device would not detach
1957 device_delete_children(device_t dev)
1962 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1966 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1967 error = device_delete_child(dev, child);
1969 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1977 * @brief Find a device given a unit number
1979 * This is similar to devclass_get_devices() but only searches for
1980 * devices which have @p dev as a parent.
1982 * @param dev the parent device to search
1983 * @param unit the unit number to search for. If the unit is -1,
1984 * return the first child of @p dev which has name
1985 * @p classname (that is, the one with the lowest unit.)
1987 * @returns the device with the given unit number or @c
1988 * NULL if there is no such device
1991 device_find_child(device_t dev, const char *classname, int unit)
1996 dc = devclass_find(classname);
2001 child = devclass_get_device(dc, unit);
2002 if (child && child->parent == dev)
2005 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
2006 child = devclass_get_device(dc, unit);
2007 if (child && child->parent == dev)
2018 first_matching_driver(devclass_t dc, device_t dev)
2021 return (devclass_find_driver_internal(dc, dev->devclass->name));
2022 return (TAILQ_FIRST(&dc->drivers));
2029 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
2031 if (dev->devclass) {
2033 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
2034 if (!strcmp(dev->devclass->name, dl->driver->name))
2038 return (TAILQ_NEXT(last, link));
2045 device_probe_child(device_t dev, device_t child)
2048 driverlink_t best = NULL;
2050 int result, pri = 0;
2051 int hasclass = (child->devclass != NULL);
2057 panic("device_probe_child: parent device has no devclass");
2060 * If the state is already probed, then return. However, don't
2061 * return if we can rebid this object.
2063 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2066 for (; dc; dc = dc->parent) {
2067 for (dl = first_matching_driver(dc, child);
2069 dl = next_matching_driver(dc, child, dl)) {
2070 /* If this driver's pass is too high, then ignore it. */
2071 if (dl->pass > bus_current_pass)
2074 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2075 result = device_set_driver(child, dl->driver);
2076 if (result == ENOMEM)
2078 else if (result != 0)
2081 if (device_set_devclass(child,
2082 dl->driver->name) != 0) {
2083 char const * devname =
2084 device_get_name(child);
2085 if (devname == NULL)
2086 devname = "(unknown)";
2087 printf("driver bug: Unable to set "
2088 "devclass (class: %s "
2092 (void)device_set_driver(child, NULL);
2097 /* Fetch any flags for the device before probing. */
2098 resource_int_value(dl->driver->name, child->unit,
2099 "flags", &child->devflags);
2101 result = DEVICE_PROBE(child);
2103 /* Reset flags and devclass before the next probe. */
2104 child->devflags = 0;
2106 (void)device_set_devclass(child, NULL);
2109 * If the driver returns SUCCESS, there can be
2110 * no higher match for this device.
2119 * Reset DF_QUIET in case this driver doesn't
2120 * end up as the best driver.
2122 device_verbose(child);
2125 * Probes that return BUS_PROBE_NOWILDCARD or lower
2126 * only match on devices whose driver was explicitly
2129 if (result <= BUS_PROBE_NOWILDCARD &&
2130 !(child->flags & DF_FIXEDCLASS)) {
2135 * The driver returned an error so it
2136 * certainly doesn't match.
2139 (void)device_set_driver(child, NULL);
2144 * A priority lower than SUCCESS, remember the
2145 * best matching driver. Initialise the value
2146 * of pri for the first match.
2148 if (best == NULL || result > pri) {
2155 * If we have an unambiguous match in this devclass,
2156 * don't look in the parent.
2158 if (best && pri == 0)
2163 * If we found a driver, change state and initialise the devclass.
2165 /* XXX What happens if we rebid and got no best? */
2168 * If this device was attached, and we were asked to
2169 * rescan, and it is a different driver, then we have
2170 * to detach the old driver and reattach this new one.
2171 * Note, we don't have to check for DF_REBID here
2172 * because if the state is > DS_ALIVE, we know it must
2175 * This assumes that all DF_REBID drivers can have
2176 * their probe routine called at any time and that
2177 * they are idempotent as well as completely benign in
2178 * normal operations.
2180 * We also have to make sure that the detach
2181 * succeeded, otherwise we fail the operation (or
2182 * maybe it should just fail silently? I'm torn).
2184 if (child->state > DS_ALIVE && best->driver != child->driver)
2185 if ((result = device_detach(dev)) != 0)
2188 /* Set the winning driver, devclass, and flags. */
2189 if (!child->devclass) {
2190 result = device_set_devclass(child, best->driver->name);
2194 result = device_set_driver(child, best->driver);
2197 resource_int_value(best->driver->name, child->unit,
2198 "flags", &child->devflags);
2202 * A bit bogus. Call the probe method again to make
2203 * sure that we have the right description.
2205 DEVICE_PROBE(child);
2207 child->flags |= DF_REBID;
2210 child->flags &= ~DF_REBID;
2211 child->state = DS_ALIVE;
2213 bus_data_generation_update();
2221 * @brief Return the parent of a device
2224 device_get_parent(device_t dev)
2226 return (dev->parent);
2230 * @brief Get a list of children of a device
2232 * An array containing a list of all the children of the given device
2233 * is allocated and returned in @p *devlistp. The number of devices
2234 * in the array is returned in @p *devcountp. The caller should free
2235 * the array using @c free(p, M_TEMP).
2237 * @param dev the device to examine
2238 * @param devlistp points at location for array pointer return
2240 * @param devcountp points at location for array size return value
2243 * @retval ENOMEM the array allocation failed
2246 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2253 TAILQ_FOREACH(child, &dev->children, link) {
2262 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2267 TAILQ_FOREACH(child, &dev->children, link) {
2268 list[count] = child;
2279 * @brief Return the current driver for the device or @c NULL if there
2280 * is no driver currently attached
2283 device_get_driver(device_t dev)
2285 return (dev->driver);
2289 * @brief Return the current devclass for the device or @c NULL if
2293 device_get_devclass(device_t dev)
2295 return (dev->devclass);
2299 * @brief Return the name of the device's devclass or @c NULL if there
2303 device_get_name(device_t dev)
2305 if (dev != NULL && dev->devclass)
2306 return (devclass_get_name(dev->devclass));
2311 * @brief Return a string containing the device's devclass name
2312 * followed by an ascii representation of the device's unit number
2316 device_get_nameunit(device_t dev)
2318 return (dev->nameunit);
2322 * @brief Return the device's unit number.
2325 device_get_unit(device_t dev)
2331 * @brief Return the device's description string
2334 device_get_desc(device_t dev)
2340 * @brief Return the device's flags
2343 device_get_flags(device_t dev)
2345 return (dev->devflags);
2348 struct sysctl_ctx_list *
2349 device_get_sysctl_ctx(device_t dev)
2351 return (&dev->sysctl_ctx);
2355 device_get_sysctl_tree(device_t dev)
2357 return (dev->sysctl_tree);
2361 * @brief Print the name of the device followed by a colon and a space
2363 * @returns the number of characters printed
2366 device_print_prettyname(device_t dev)
2368 const char *name = device_get_name(dev);
2371 return (printf("unknown: "));
2372 return (printf("%s%d: ", name, device_get_unit(dev)));
2376 * @brief Print the name of the device followed by a colon, a space
2377 * and the result of calling vprintf() with the value of @p fmt and
2378 * the following arguments.
2380 * @returns the number of characters printed
2383 device_printf(device_t dev, const char * fmt, ...)
2393 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
2394 sbuf_set_drain(&sb, sbuf_printf_drain, &retval);
2396 name = device_get_name(dev);
2399 sbuf_cat(&sb, "unknown: ");
2401 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
2404 sbuf_vprintf(&sb, fmt, ap);
2417 device_set_desc_internal(device_t dev, const char* desc, int copy)
2419 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2420 free(dev->desc, M_BUS);
2421 dev->flags &= ~DF_DESCMALLOCED;
2426 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2428 strcpy(dev->desc, desc);
2429 dev->flags |= DF_DESCMALLOCED;
2432 /* Avoid a -Wcast-qual warning */
2433 dev->desc = (char *)(uintptr_t) desc;
2436 bus_data_generation_update();
2440 * @brief Set the device's description
2442 * The value of @c desc should be a string constant that will not
2443 * change (at least until the description is changed in a subsequent
2444 * call to device_set_desc() or device_set_desc_copy()).
2447 device_set_desc(device_t dev, const char* desc)
2449 device_set_desc_internal(dev, desc, FALSE);
2453 * @brief Set the device's description
2455 * The string pointed to by @c desc is copied. Use this function if
2456 * the device description is generated, (e.g. with sprintf()).
2459 device_set_desc_copy(device_t dev, const char* desc)
2461 device_set_desc_internal(dev, desc, TRUE);
2465 * @brief Set the device's flags
2468 device_set_flags(device_t dev, uint32_t flags)
2470 dev->devflags = flags;
2474 * @brief Return the device's softc field
2476 * The softc is allocated and zeroed when a driver is attached, based
2477 * on the size field of the driver.
2480 device_get_softc(device_t dev)
2482 return (dev->softc);
2486 * @brief Set the device's softc field
2488 * Most drivers do not need to use this since the softc is allocated
2489 * automatically when the driver is attached.
2492 device_set_softc(device_t dev, void *softc)
2494 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2495 free(dev->softc, M_BUS_SC);
2498 dev->flags |= DF_EXTERNALSOFTC;
2500 dev->flags &= ~DF_EXTERNALSOFTC;
2504 * @brief Free claimed softc
2506 * Most drivers do not need to use this since the softc is freed
2507 * automatically when the driver is detached.
2510 device_free_softc(void *softc)
2512 free(softc, M_BUS_SC);
2516 * @brief Claim softc
2518 * This function can be used to let the driver free the automatically
2519 * allocated softc using "device_free_softc()". This function is
2520 * useful when the driver is refcounting the softc and the softc
2521 * cannot be freed when the "device_detach" method is called.
2524 device_claim_softc(device_t dev)
2527 dev->flags |= DF_EXTERNALSOFTC;
2529 dev->flags &= ~DF_EXTERNALSOFTC;
2533 * @brief Get the device's ivars field
2535 * The ivars field is used by the parent device to store per-device
2536 * state (e.g. the physical location of the device or a list of
2540 device_get_ivars(device_t dev)
2542 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2543 return (dev->ivars);
2547 * @brief Set the device's ivars field
2550 device_set_ivars(device_t dev, void * ivars)
2552 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2557 * @brief Return the device's state
2560 device_get_state(device_t dev)
2562 return (dev->state);
2566 * @brief Set the DF_ENABLED flag for the device
2569 device_enable(device_t dev)
2571 dev->flags |= DF_ENABLED;
2575 * @brief Clear the DF_ENABLED flag for the device
2578 device_disable(device_t dev)
2580 dev->flags &= ~DF_ENABLED;
2584 * @brief Increment the busy counter for the device
2587 device_busy(device_t dev)
2589 if (dev->state < DS_ATTACHING)
2590 panic("device_busy: called for unattached device");
2591 if (dev->busy == 0 && dev->parent)
2592 device_busy(dev->parent);
2594 if (dev->state == DS_ATTACHED)
2595 dev->state = DS_BUSY;
2599 * @brief Decrement the busy counter for the device
2602 device_unbusy(device_t dev)
2604 if (dev->busy != 0 && dev->state != DS_BUSY &&
2605 dev->state != DS_ATTACHING)
2606 panic("device_unbusy: called for non-busy device %s",
2607 device_get_nameunit(dev));
2609 if (dev->busy == 0) {
2611 device_unbusy(dev->parent);
2612 if (dev->state == DS_BUSY)
2613 dev->state = DS_ATTACHED;
2618 * @brief Set the DF_QUIET flag for the device
2621 device_quiet(device_t dev)
2623 dev->flags |= DF_QUIET;
2627 * @brief Set the DF_QUIET_CHILDREN flag for the device
2630 device_quiet_children(device_t dev)
2632 dev->flags |= DF_QUIET_CHILDREN;
2636 * @brief Clear the DF_QUIET flag for the device
2639 device_verbose(device_t dev)
2641 dev->flags &= ~DF_QUIET;
2645 * @brief Return non-zero if the DF_QUIET_CHIDLREN flag is set on the device
2648 device_has_quiet_children(device_t dev)
2650 return ((dev->flags & DF_QUIET_CHILDREN) != 0);
2654 * @brief Return non-zero if the DF_QUIET flag is set on the device
2657 device_is_quiet(device_t dev)
2659 return ((dev->flags & DF_QUIET) != 0);
2663 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2666 device_is_enabled(device_t dev)
2668 return ((dev->flags & DF_ENABLED) != 0);
2672 * @brief Return non-zero if the device was successfully probed
2675 device_is_alive(device_t dev)
2677 return (dev->state >= DS_ALIVE);
2681 * @brief Return non-zero if the device currently has a driver
2685 device_is_attached(device_t dev)
2687 return (dev->state >= DS_ATTACHED);
2691 * @brief Return non-zero if the device is currently suspended.
2694 device_is_suspended(device_t dev)
2696 return ((dev->flags & DF_SUSPENDED) != 0);
2700 * @brief Set the devclass of a device
2701 * @see devclass_add_device().
2704 device_set_devclass(device_t dev, const char *classname)
2711 devclass_delete_device(dev->devclass, dev);
2715 if (dev->devclass) {
2716 printf("device_set_devclass: device class already set\n");
2720 dc = devclass_find_internal(classname, NULL, TRUE);
2724 error = devclass_add_device(dc, dev);
2726 bus_data_generation_update();
2731 * @brief Set the devclass of a device and mark the devclass fixed.
2732 * @see device_set_devclass()
2735 device_set_devclass_fixed(device_t dev, const char *classname)
2739 if (classname == NULL)
2742 error = device_set_devclass(dev, classname);
2745 dev->flags |= DF_FIXEDCLASS;
2750 * @brief Query the device to determine if it's of a fixed devclass
2751 * @see device_set_devclass_fixed()
2754 device_is_devclass_fixed(device_t dev)
2756 return ((dev->flags & DF_FIXEDCLASS) != 0);
2760 * @brief Set the driver of a device
2763 * @retval EBUSY the device already has a driver attached
2764 * @retval ENOMEM a memory allocation failure occurred
2767 device_set_driver(device_t dev, driver_t *driver)
2770 struct domainset *policy;
2772 if (dev->state >= DS_ATTACHED)
2775 if (dev->driver == driver)
2778 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2779 free(dev->softc, M_BUS_SC);
2782 device_set_desc(dev, NULL);
2783 kobj_delete((kobj_t) dev, NULL);
2784 dev->driver = driver;
2786 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2787 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2788 if (bus_get_domain(dev, &domain) == 0)
2789 policy = DOMAINSET_PREF(domain);
2791 policy = DOMAINSET_RR();
2792 dev->softc = malloc_domainset(driver->size, M_BUS_SC,
2793 policy, M_NOWAIT | M_ZERO);
2795 kobj_delete((kobj_t) dev, NULL);
2796 kobj_init((kobj_t) dev, &null_class);
2802 kobj_init((kobj_t) dev, &null_class);
2805 bus_data_generation_update();
2810 * @brief Probe a device, and return this status.
2812 * This function is the core of the device autoconfiguration
2813 * system. Its purpose is to select a suitable driver for a device and
2814 * then call that driver to initialise the hardware appropriately. The
2815 * driver is selected by calling the DEVICE_PROBE() method of a set of
2816 * candidate drivers and then choosing the driver which returned the
2817 * best value. This driver is then attached to the device using
2820 * The set of suitable drivers is taken from the list of drivers in
2821 * the parent device's devclass. If the device was originally created
2822 * with a specific class name (see device_add_child()), only drivers
2823 * with that name are probed, otherwise all drivers in the devclass
2824 * are probed. If no drivers return successful probe values in the
2825 * parent devclass, the search continues in the parent of that
2826 * devclass (see devclass_get_parent()) if any.
2828 * @param dev the device to initialise
2831 * @retval ENXIO no driver was found
2832 * @retval ENOMEM memory allocation failure
2833 * @retval non-zero some other unix error code
2834 * @retval -1 Device already attached
2837 device_probe(device_t dev)
2843 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2846 if (!(dev->flags & DF_ENABLED)) {
2847 if (bootverbose && device_get_name(dev) != NULL) {
2848 device_print_prettyname(dev);
2849 printf("not probed (disabled)\n");
2853 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2854 if (bus_current_pass == BUS_PASS_DEFAULT &&
2855 !(dev->flags & DF_DONENOMATCH)) {
2856 BUS_PROBE_NOMATCH(dev->parent, dev);
2858 dev->flags |= DF_DONENOMATCH;
2866 * @brief Probe a device and attach a driver if possible
2868 * calls device_probe() and attaches if that was successful.
2871 device_probe_and_attach(device_t dev)
2877 error = device_probe(dev);
2880 else if (error != 0)
2883 CURVNET_SET_QUIET(vnet0);
2884 error = device_attach(dev);
2890 * @brief Attach a device driver to a device
2892 * This function is a wrapper around the DEVICE_ATTACH() driver
2893 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2894 * device's sysctl tree, optionally prints a description of the device
2895 * and queues a notification event for user-based device management
2898 * Normally this function is only called internally from
2899 * device_probe_and_attach().
2901 * @param dev the device to initialise
2904 * @retval ENXIO no driver was found
2905 * @retval ENOMEM memory allocation failure
2906 * @retval non-zero some other unix error code
2909 device_attach(device_t dev)
2911 uint64_t attachtime;
2912 uint16_t attachentropy;
2915 if (resource_disabled(dev->driver->name, dev->unit)) {
2916 device_disable(dev);
2918 device_printf(dev, "disabled via hints entry\n");
2922 device_sysctl_init(dev);
2923 if (!device_is_quiet(dev))
2924 device_print_child(dev->parent, dev);
2925 attachtime = get_cyclecount();
2926 dev->state = DS_ATTACHING;
2927 if ((error = DEVICE_ATTACH(dev)) != 0) {
2928 printf("device_attach: %s%d attach returned %d\n",
2929 dev->driver->name, dev->unit, error);
2930 if (!(dev->flags & DF_FIXEDCLASS))
2931 devclass_delete_device(dev->devclass, dev);
2932 (void)device_set_driver(dev, NULL);
2933 device_sysctl_fini(dev);
2934 KASSERT(dev->busy == 0, ("attach failed but busy"));
2935 dev->state = DS_NOTPRESENT;
2938 dev->flags |= DF_ATTACHED_ONCE;
2939 /* We only need the low bits of this time, but ranges from tens to thousands
2940 * have been seen, so keep 2 bytes' worth.
2942 attachentropy = (uint16_t)(get_cyclecount() - attachtime);
2943 random_harvest_direct(&attachentropy, sizeof(attachentropy), RANDOM_ATTACH);
2944 device_sysctl_update(dev);
2946 dev->state = DS_BUSY;
2948 dev->state = DS_ATTACHED;
2949 dev->flags &= ~DF_DONENOMATCH;
2950 EVENTHANDLER_DIRECT_INVOKE(device_attach, dev);
2956 * @brief Detach a driver from a device
2958 * This function is a wrapper around the DEVICE_DETACH() driver
2959 * method. If the call to DEVICE_DETACH() succeeds, it calls
2960 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2961 * notification event for user-based device management services and
2962 * cleans up the device's sysctl tree.
2964 * @param dev the device to un-initialise
2967 * @retval ENXIO no driver was found
2968 * @retval ENOMEM memory allocation failure
2969 * @retval non-zero some other unix error code
2972 device_detach(device_t dev)
2978 PDEBUG(("%s", DEVICENAME(dev)));
2979 if (dev->state == DS_BUSY)
2981 if (dev->state == DS_ATTACHING) {
2982 device_printf(dev, "device in attaching state! Deferring detach.\n");
2985 if (dev->state != DS_ATTACHED)
2988 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, EVHDEV_DETACH_BEGIN);
2989 if ((error = DEVICE_DETACH(dev)) != 0) {
2990 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
2991 EVHDEV_DETACH_FAILED);
2994 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
2995 EVHDEV_DETACH_COMPLETE);
2998 if (!device_is_quiet(dev))
2999 device_printf(dev, "detached\n");
3001 BUS_CHILD_DETACHED(dev->parent, dev);
3003 if (!(dev->flags & DF_FIXEDCLASS))
3004 devclass_delete_device(dev->devclass, dev);
3006 device_verbose(dev);
3007 dev->state = DS_NOTPRESENT;
3008 (void)device_set_driver(dev, NULL);
3009 device_sysctl_fini(dev);
3015 * @brief Tells a driver to quiesce itself.
3017 * This function is a wrapper around the DEVICE_QUIESCE() driver
3018 * method. If the call to DEVICE_QUIESCE() succeeds.
3020 * @param dev the device to quiesce
3023 * @retval ENXIO no driver was found
3024 * @retval ENOMEM memory allocation failure
3025 * @retval non-zero some other unix error code
3028 device_quiesce(device_t dev)
3030 PDEBUG(("%s", DEVICENAME(dev)));
3031 if (dev->state == DS_BUSY)
3033 if (dev->state != DS_ATTACHED)
3036 return (DEVICE_QUIESCE(dev));
3040 * @brief Notify a device of system shutdown
3042 * This function calls the DEVICE_SHUTDOWN() driver method if the
3043 * device currently has an attached driver.
3045 * @returns the value returned by DEVICE_SHUTDOWN()
3048 device_shutdown(device_t dev)
3050 if (dev->state < DS_ATTACHED)
3052 return (DEVICE_SHUTDOWN(dev));
3056 * @brief Set the unit number of a device
3058 * This function can be used to override the unit number used for a
3059 * device (e.g. to wire a device to a pre-configured unit number).
3062 device_set_unit(device_t dev, int unit)
3067 dc = device_get_devclass(dev);
3068 if (unit < dc->maxunit && dc->devices[unit])
3070 err = devclass_delete_device(dc, dev);
3074 err = devclass_add_device(dc, dev);
3078 bus_data_generation_update();
3082 /*======================================*/
3084 * Some useful method implementations to make life easier for bus drivers.
3088 resource_init_map_request_impl(struct resource_map_request *args, size_t sz)
3092 args->memattr = VM_MEMATTR_UNCACHEABLE;
3096 * @brief Initialise a resource list.
3098 * @param rl the resource list to initialise
3101 resource_list_init(struct resource_list *rl)
3107 * @brief Reclaim memory used by a resource list.
3109 * This function frees the memory for all resource entries on the list
3112 * @param rl the resource list to free
3115 resource_list_free(struct resource_list *rl)
3117 struct resource_list_entry *rle;
3119 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3121 panic("resource_list_free: resource entry is busy");
3122 STAILQ_REMOVE_HEAD(rl, link);
3128 * @brief Add a resource entry.
3130 * This function adds a resource entry using the given @p type, @p
3131 * start, @p end and @p count values. A rid value is chosen by
3132 * searching sequentially for the first unused rid starting at zero.
3134 * @param rl the resource list to edit
3135 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3136 * @param start the start address of the resource
3137 * @param end the end address of the resource
3138 * @param count XXX end-start+1
3141 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start,
3142 rman_res_t end, rman_res_t count)
3147 while (resource_list_find(rl, type, rid) != NULL)
3149 resource_list_add(rl, type, rid, start, end, count);
3154 * @brief Add or modify a resource entry.
3156 * If an existing entry exists with the same type and rid, it will be
3157 * modified using the given values of @p start, @p end and @p
3158 * count. If no entry exists, a new one will be created using the
3159 * given values. The resource list entry that matches is then returned.
3161 * @param rl the resource list to edit
3162 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3163 * @param rid the resource identifier
3164 * @param start the start address of the resource
3165 * @param end the end address of the resource
3166 * @param count XXX end-start+1
3168 struct resource_list_entry *
3169 resource_list_add(struct resource_list *rl, int type, int rid,
3170 rman_res_t start, rman_res_t end, rman_res_t count)
3172 struct resource_list_entry *rle;
3174 rle = resource_list_find(rl, type, rid);
3176 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3179 panic("resource_list_add: can't record entry");
3180 STAILQ_INSERT_TAIL(rl, rle, link);
3188 panic("resource_list_add: resource entry is busy");
3197 * @brief Determine if a resource entry is busy.
3199 * Returns true if a resource entry is busy meaning that it has an
3200 * associated resource that is not an unallocated "reserved" resource.
3202 * @param rl the resource list to search
3203 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3204 * @param rid the resource identifier
3206 * @returns Non-zero if the entry is busy, zero otherwise.
3209 resource_list_busy(struct resource_list *rl, int type, int rid)
3211 struct resource_list_entry *rle;
3213 rle = resource_list_find(rl, type, rid);
3214 if (rle == NULL || rle->res == NULL)
3216 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3217 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3218 ("reserved resource is active"));
3225 * @brief Determine if a resource entry is reserved.
3227 * Returns true if a resource entry is reserved meaning that it has an
3228 * associated "reserved" resource. The resource can either be
3229 * allocated or unallocated.
3231 * @param rl the resource list to search
3232 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3233 * @param rid the resource identifier
3235 * @returns Non-zero if the entry is reserved, zero otherwise.
3238 resource_list_reserved(struct resource_list *rl, int type, int rid)
3240 struct resource_list_entry *rle;
3242 rle = resource_list_find(rl, type, rid);
3243 if (rle != NULL && rle->flags & RLE_RESERVED)
3249 * @brief Find a resource entry by type and rid.
3251 * @param rl the resource list to search
3252 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3253 * @param rid the resource identifier
3255 * @returns the resource entry pointer or NULL if there is no such
3258 struct resource_list_entry *
3259 resource_list_find(struct resource_list *rl, int type, int rid)
3261 struct resource_list_entry *rle;
3263 STAILQ_FOREACH(rle, rl, link) {
3264 if (rle->type == type && rle->rid == rid)
3271 * @brief Delete a resource entry.
3273 * @param rl the resource list to edit
3274 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3275 * @param rid the resource identifier
3278 resource_list_delete(struct resource_list *rl, int type, int rid)
3280 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3283 if (rle->res != NULL)
3284 panic("resource_list_delete: resource has not been released");
3285 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3291 * @brief Allocate a reserved resource
3293 * This can be used by buses to force the allocation of resources
3294 * that are always active in the system even if they are not allocated
3295 * by a driver (e.g. PCI BARs). This function is usually called when
3296 * adding a new child to the bus. The resource is allocated from the
3297 * parent bus when it is reserved. The resource list entry is marked
3298 * with RLE_RESERVED to note that it is a reserved resource.
3300 * Subsequent attempts to allocate the resource with
3301 * resource_list_alloc() will succeed the first time and will set
3302 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3303 * resource that has been allocated is released with
3304 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3305 * the actual resource remains allocated. The resource can be released to
3306 * the parent bus by calling resource_list_unreserve().
3308 * @param rl the resource list to allocate from
3309 * @param bus the parent device of @p child
3310 * @param child the device for which the resource is being reserved
3311 * @param type the type of resource to allocate
3312 * @param rid a pointer to the resource identifier
3313 * @param start hint at the start of the resource range - pass
3314 * @c 0 for any start address
3315 * @param end hint at the end of the resource range - pass
3316 * @c ~0 for any end address
3317 * @param count hint at the size of range required - pass @c 1
3319 * @param flags any extra flags to control the resource
3320 * allocation - see @c RF_XXX flags in
3321 * <sys/rman.h> for details
3323 * @returns the resource which was allocated or @c NULL if no
3324 * resource could be allocated
3327 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3328 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3330 struct resource_list_entry *rle = NULL;
3331 int passthrough = (device_get_parent(child) != bus);
3336 "resource_list_reserve() should only be called for direct children");
3337 if (flags & RF_ACTIVE)
3339 "resource_list_reserve() should only reserve inactive resources");
3341 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3344 rle = resource_list_find(rl, type, *rid);
3345 rle->flags |= RLE_RESERVED;
3351 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3353 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3354 * and passing the allocation up to the parent of @p bus. This assumes
3355 * that the first entry of @c device_get_ivars(child) is a struct
3356 * resource_list. This also handles 'passthrough' allocations where a
3357 * child is a remote descendant of bus by passing the allocation up to
3358 * the parent of bus.
3360 * Typically, a bus driver would store a list of child resources
3361 * somewhere in the child device's ivars (see device_get_ivars()) and
3362 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3363 * then call resource_list_alloc() to perform the allocation.
3365 * @param rl the resource list to allocate from
3366 * @param bus the parent device of @p child
3367 * @param child the device which is requesting an allocation
3368 * @param type the type of resource to allocate
3369 * @param rid a pointer to the resource identifier
3370 * @param start hint at the start of the resource range - pass
3371 * @c 0 for any start address
3372 * @param end hint at the end of the resource range - pass
3373 * @c ~0 for any end address
3374 * @param count hint at the size of range required - pass @c 1
3376 * @param flags any extra flags to control the resource
3377 * allocation - see @c RF_XXX flags in
3378 * <sys/rman.h> for details
3380 * @returns the resource which was allocated or @c NULL if no
3381 * resource could be allocated
3384 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3385 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3387 struct resource_list_entry *rle = NULL;
3388 int passthrough = (device_get_parent(child) != bus);
3389 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
3392 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3393 type, rid, start, end, count, flags));
3396 rle = resource_list_find(rl, type, *rid);
3399 return (NULL); /* no resource of that type/rid */
3402 if (rle->flags & RLE_RESERVED) {
3403 if (rle->flags & RLE_ALLOCATED)
3405 if ((flags & RF_ACTIVE) &&
3406 bus_activate_resource(child, type, *rid,
3409 rle->flags |= RLE_ALLOCATED;
3413 "resource entry %#x type %d for child %s is busy\n", *rid,
3414 type, device_get_nameunit(child));
3420 count = ulmax(count, rle->count);
3421 end = ulmax(rle->end, start + count - 1);
3424 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3425 type, rid, start, end, count, flags);
3428 * Record the new range.
3431 rle->start = rman_get_start(rle->res);
3432 rle->end = rman_get_end(rle->res);
3440 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3442 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3443 * used with resource_list_alloc().
3445 * @param rl the resource list which was allocated from
3446 * @param bus the parent device of @p child
3447 * @param child the device which is requesting a release
3448 * @param type the type of resource to release
3449 * @param rid the resource identifier
3450 * @param res the resource to release
3453 * @retval non-zero a standard unix error code indicating what
3454 * error condition prevented the operation
3457 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3458 int type, int rid, struct resource *res)
3460 struct resource_list_entry *rle = NULL;
3461 int passthrough = (device_get_parent(child) != bus);
3465 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3469 rle = resource_list_find(rl, type, rid);
3472 panic("resource_list_release: can't find resource");
3474 panic("resource_list_release: resource entry is not busy");
3475 if (rle->flags & RLE_RESERVED) {
3476 if (rle->flags & RLE_ALLOCATED) {
3477 if (rman_get_flags(res) & RF_ACTIVE) {
3478 error = bus_deactivate_resource(child, type,
3483 rle->flags &= ~RLE_ALLOCATED;
3489 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3499 * @brief Release all active resources of a given type
3501 * Release all active resources of a specified type. This is intended
3502 * to be used to cleanup resources leaked by a driver after detach or
3505 * @param rl the resource list which was allocated from
3506 * @param bus the parent device of @p child
3507 * @param child the device whose active resources are being released
3508 * @param type the type of resources to release
3511 * @retval EBUSY at least one resource was active
3514 resource_list_release_active(struct resource_list *rl, device_t bus,
3515 device_t child, int type)
3517 struct resource_list_entry *rle;
3521 STAILQ_FOREACH(rle, rl, link) {
3522 if (rle->type != type)
3524 if (rle->res == NULL)
3526 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3530 error = resource_list_release(rl, bus, child, type,
3531 rman_get_rid(rle->res), rle->res);
3534 "Failed to release active resource: %d\n", error);
3540 * @brief Fully release a reserved resource
3542 * Fully releases a resource reserved via resource_list_reserve().
3544 * @param rl the resource list which was allocated from
3545 * @param bus the parent device of @p child
3546 * @param child the device whose reserved resource is being released
3547 * @param type the type of resource to release
3548 * @param rid the resource identifier
3549 * @param res the resource to release
3552 * @retval non-zero a standard unix error code indicating what
3553 * error condition prevented the operation
3556 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3559 struct resource_list_entry *rle = NULL;
3560 int passthrough = (device_get_parent(child) != bus);
3564 "resource_list_unreserve() should only be called for direct children");
3566 rle = resource_list_find(rl, type, rid);
3569 panic("resource_list_unreserve: can't find resource");
3570 if (!(rle->flags & RLE_RESERVED))
3572 if (rle->flags & RLE_ALLOCATED)
3574 rle->flags &= ~RLE_RESERVED;
3575 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3579 * @brief Print a description of resources in a resource list
3581 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3582 * The name is printed if at least one resource of the given type is available.
3583 * The format is used to print resource start and end.
3585 * @param rl the resource list to print
3586 * @param name the name of @p type, e.g. @c "memory"
3587 * @param type type type of resource entry to print
3588 * @param format printf(9) format string to print resource
3589 * start and end values
3591 * @returns the number of characters printed
3594 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3597 struct resource_list_entry *rle;
3598 int printed, retval;
3602 /* Yes, this is kinda cheating */
3603 STAILQ_FOREACH(rle, rl, link) {
3604 if (rle->type == type) {
3606 retval += printf(" %s ", name);
3608 retval += printf(",");
3610 retval += printf(format, rle->start);
3611 if (rle->count > 1) {
3612 retval += printf("-");
3613 retval += printf(format, rle->start +
3622 * @brief Releases all the resources in a list.
3624 * @param rl The resource list to purge.
3629 resource_list_purge(struct resource_list *rl)
3631 struct resource_list_entry *rle;
3633 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3635 bus_release_resource(rman_get_device(rle->res),
3636 rle->type, rle->rid, rle->res);
3637 STAILQ_REMOVE_HEAD(rl, link);
3643 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3645 return (device_add_child_ordered(dev, order, name, unit));
3649 * @brief Helper function for implementing DEVICE_PROBE()
3651 * This function can be used to help implement the DEVICE_PROBE() for
3652 * a bus (i.e. a device which has other devices attached to it). It
3653 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3657 bus_generic_probe(device_t dev)
3659 devclass_t dc = dev->devclass;
3662 TAILQ_FOREACH(dl, &dc->drivers, link) {
3664 * If this driver's pass is too high, then ignore it.
3665 * For most drivers in the default pass, this will
3666 * never be true. For early-pass drivers they will
3667 * only call the identify routines of eligible drivers
3668 * when this routine is called. Drivers for later
3669 * passes should have their identify routines called
3670 * on early-pass buses during BUS_NEW_PASS().
3672 if (dl->pass > bus_current_pass)
3674 DEVICE_IDENTIFY(dl->driver, dev);
3681 * @brief Helper function for implementing DEVICE_ATTACH()
3683 * This function can be used to help implement the DEVICE_ATTACH() for
3684 * a bus. It calls device_probe_and_attach() for each of the device's
3688 bus_generic_attach(device_t dev)
3692 TAILQ_FOREACH(child, &dev->children, link) {
3693 device_probe_and_attach(child);
3700 * @brief Helper function for delaying attaching children
3702 * Many buses can't run transactions on the bus which children need to probe and
3703 * attach until after interrupts and/or timers are running. This function
3704 * delays their attach until interrupts and timers are enabled.
3707 bus_delayed_attach_children(device_t dev)
3709 /* Probe and attach the bus children when interrupts are available */
3710 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
3716 * @brief Helper function for implementing DEVICE_DETACH()
3718 * This function can be used to help implement the DEVICE_DETACH() for
3719 * a bus. It calls device_detach() for each of the device's
3723 bus_generic_detach(device_t dev)
3728 if (dev->state != DS_ATTACHED)
3732 * Detach children in the reverse order.
3733 * See bus_generic_suspend for details.
3735 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3736 if ((error = device_detach(child)) != 0)
3744 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3746 * This function can be used to help implement the DEVICE_SHUTDOWN()
3747 * for a bus. It calls device_shutdown() for each of the device's
3751 bus_generic_shutdown(device_t dev)
3756 * Shut down children in the reverse order.
3757 * See bus_generic_suspend for details.
3759 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3760 device_shutdown(child);
3767 * @brief Default function for suspending a child device.
3769 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3772 bus_generic_suspend_child(device_t dev, device_t child)
3776 error = DEVICE_SUSPEND(child);
3779 child->flags |= DF_SUSPENDED;
3785 * @brief Default function for resuming a child device.
3787 * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3790 bus_generic_resume_child(device_t dev, device_t child)
3792 DEVICE_RESUME(child);
3793 child->flags &= ~DF_SUSPENDED;
3799 * @brief Helper function for implementing DEVICE_SUSPEND()
3801 * This function can be used to help implement the DEVICE_SUSPEND()
3802 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3803 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3804 * operation is aborted and any devices which were suspended are
3805 * resumed immediately by calling their DEVICE_RESUME() methods.
3808 bus_generic_suspend(device_t dev)
3814 * Suspend children in the reverse order.
3815 * For most buses all children are equal, so the order does not matter.
3816 * Other buses, such as acpi, carefully order their child devices to
3817 * express implicit dependencies between them. For such buses it is
3818 * safer to bring down devices in the reverse order.
3820 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3821 error = BUS_SUSPEND_CHILD(dev, child);
3823 child = TAILQ_NEXT(child, link);
3824 if (child != NULL) {
3825 TAILQ_FOREACH_FROM(child, &dev->children, link)
3826 BUS_RESUME_CHILD(dev, child);
3835 * @brief Helper function for implementing DEVICE_RESUME()
3837 * This function can be used to help implement the DEVICE_RESUME() for
3838 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3841 bus_generic_resume(device_t dev)
3845 TAILQ_FOREACH(child, &dev->children, link) {
3846 BUS_RESUME_CHILD(dev, child);
3847 /* if resume fails, there's nothing we can usefully do... */
3853 * @brief Helper function for implementing BUS_RESET_POST
3855 * Bus can use this function to implement common operations of
3856 * re-attaching or resuming the children after the bus itself was
3857 * reset, and after restoring bus-unique state of children.
3859 * @param dev The bus
3860 * #param flags DEVF_RESET_*
3863 bus_helper_reset_post(device_t dev, int flags)
3869 TAILQ_FOREACH(child, &dev->children,link) {
3870 BUS_RESET_POST(dev, child);
3871 error1 = (flags & DEVF_RESET_DETACH) != 0 ?
3872 device_probe_and_attach(child) :
3873 BUS_RESUME_CHILD(dev, child);
3874 if (error == 0 && error1 != 0)
3881 bus_helper_reset_prepare_rollback(device_t dev, device_t child, int flags)
3883 child = TAILQ_NEXT(child, link);
3886 TAILQ_FOREACH_FROM(child, &dev->children,link) {
3887 BUS_RESET_POST(dev, child);
3888 if ((flags & DEVF_RESET_DETACH) != 0)
3889 device_probe_and_attach(child);
3891 BUS_RESUME_CHILD(dev, child);
3896 * @brief Helper function for implementing BUS_RESET_PREPARE
3898 * Bus can use this function to implement common operations of
3899 * detaching or suspending the children before the bus itself is
3900 * reset, and then save bus-unique state of children that must
3901 * persists around reset.
3903 * @param dev The bus
3904 * #param flags DEVF_RESET_*
3907 bus_helper_reset_prepare(device_t dev, int flags)
3912 if (dev->state != DS_ATTACHED)
3915 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3916 if ((flags & DEVF_RESET_DETACH) != 0) {
3917 error = device_get_state(child) == DS_ATTACHED ?
3918 device_detach(child) : 0;
3920 error = BUS_SUSPEND_CHILD(dev, child);
3923 error = BUS_RESET_PREPARE(dev, child);
3925 if ((flags & DEVF_RESET_DETACH) != 0)
3926 device_probe_and_attach(child);
3928 BUS_RESUME_CHILD(dev, child);
3932 bus_helper_reset_prepare_rollback(dev, child, flags);
3940 * @brief Helper function for implementing BUS_PRINT_CHILD().
3942 * This function prints the first part of the ascii representation of
3943 * @p child, including its name, unit and description (if any - see
3944 * device_set_desc()).
3946 * @returns the number of characters printed
3949 bus_print_child_header(device_t dev, device_t child)
3953 if (device_get_desc(child)) {
3954 retval += device_printf(child, "<%s>", device_get_desc(child));
3956 retval += printf("%s", device_get_nameunit(child));
3963 * @brief Helper function for implementing BUS_PRINT_CHILD().
3965 * This function prints the last part of the ascii representation of
3966 * @p child, which consists of the string @c " on " followed by the
3967 * name and unit of the @p dev.
3969 * @returns the number of characters printed
3972 bus_print_child_footer(device_t dev, device_t child)
3974 return (printf(" on %s\n", device_get_nameunit(dev)));
3978 * @brief Helper function for implementing BUS_PRINT_CHILD().
3980 * This function prints out the VM domain for the given device.
3982 * @returns the number of characters printed
3985 bus_print_child_domain(device_t dev, device_t child)
3989 /* No domain? Don't print anything */
3990 if (BUS_GET_DOMAIN(dev, child, &domain) != 0)
3993 return (printf(" numa-domain %d", domain));
3997 * @brief Helper function for implementing BUS_PRINT_CHILD().
3999 * This function simply calls bus_print_child_header() followed by
4000 * bus_print_child_footer().
4002 * @returns the number of characters printed
4005 bus_generic_print_child(device_t dev, device_t child)
4009 retval += bus_print_child_header(dev, child);
4010 retval += bus_print_child_domain(dev, child);
4011 retval += bus_print_child_footer(dev, child);
4017 * @brief Stub function for implementing BUS_READ_IVAR().
4022 bus_generic_read_ivar(device_t dev, device_t child, int index,
4029 * @brief Stub function for implementing BUS_WRITE_IVAR().
4034 bus_generic_write_ivar(device_t dev, device_t child, int index,
4041 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
4045 struct resource_list *
4046 bus_generic_get_resource_list(device_t dev, device_t child)
4052 * @brief Helper function for implementing BUS_DRIVER_ADDED().
4054 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
4055 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
4056 * and then calls device_probe_and_attach() for each unattached child.
4059 bus_generic_driver_added(device_t dev, driver_t *driver)
4063 DEVICE_IDENTIFY(driver, dev);
4064 TAILQ_FOREACH(child, &dev->children, link) {
4065 if (child->state == DS_NOTPRESENT ||
4066 (child->flags & DF_REBID))
4067 device_probe_and_attach(child);
4072 * @brief Helper function for implementing BUS_NEW_PASS().
4074 * This implementing of BUS_NEW_PASS() first calls the identify
4075 * routines for any drivers that probe at the current pass. Then it
4076 * walks the list of devices for this bus. If a device is already
4077 * attached, then it calls BUS_NEW_PASS() on that device. If the
4078 * device is not already attached, it attempts to attach a driver to
4082 bus_generic_new_pass(device_t dev)
4089 TAILQ_FOREACH(dl, &dc->drivers, link) {
4090 if (dl->pass == bus_current_pass)
4091 DEVICE_IDENTIFY(dl->driver, dev);
4093 TAILQ_FOREACH(child, &dev->children, link) {
4094 if (child->state >= DS_ATTACHED)
4095 BUS_NEW_PASS(child);
4096 else if (child->state == DS_NOTPRESENT)
4097 device_probe_and_attach(child);
4102 * @brief Helper function for implementing BUS_SETUP_INTR().
4104 * This simple implementation of BUS_SETUP_INTR() simply calls the
4105 * BUS_SETUP_INTR() method of the parent of @p dev.
4108 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
4109 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
4112 /* Propagate up the bus hierarchy until someone handles it. */
4114 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
4115 filter, intr, arg, cookiep));
4120 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
4122 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
4123 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
4126 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
4129 /* Propagate up the bus hierarchy until someone handles it. */
4131 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
4136 * @brief Helper function for implementing BUS_SUSPEND_INTR().
4138 * This simple implementation of BUS_SUSPEND_INTR() simply calls the
4139 * BUS_SUSPEND_INTR() method of the parent of @p dev.
4142 bus_generic_suspend_intr(device_t dev, device_t child, struct resource *irq)
4144 /* Propagate up the bus hierarchy until someone handles it. */
4146 return (BUS_SUSPEND_INTR(dev->parent, child, irq));
4151 * @brief Helper function for implementing BUS_RESUME_INTR().
4153 * This simple implementation of BUS_RESUME_INTR() simply calls the
4154 * BUS_RESUME_INTR() method of the parent of @p dev.
4157 bus_generic_resume_intr(device_t dev, device_t child, struct resource *irq)
4159 /* Propagate up the bus hierarchy until someone handles it. */
4161 return (BUS_RESUME_INTR(dev->parent, child, irq));
4166 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
4168 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
4169 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
4172 bus_generic_adjust_resource(device_t dev, device_t child, int type,
4173 struct resource *r, rman_res_t start, rman_res_t end)
4175 /* Propagate up the bus hierarchy until someone handles it. */
4177 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
4183 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4185 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
4186 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
4189 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
4190 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4192 /* Propagate up the bus hierarchy until someone handles it. */
4194 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
4195 start, end, count, flags));
4200 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4202 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
4203 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
4206 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
4209 /* Propagate up the bus hierarchy until someone handles it. */
4211 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
4217 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
4219 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
4220 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
4223 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
4226 /* Propagate up the bus hierarchy until someone handles it. */
4228 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
4234 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
4236 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
4237 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
4240 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
4241 int rid, struct resource *r)
4243 /* Propagate up the bus hierarchy until someone handles it. */
4245 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
4251 * @brief Helper function for implementing BUS_MAP_RESOURCE().
4253 * This simple implementation of BUS_MAP_RESOURCE() simply calls the
4254 * BUS_MAP_RESOURCE() method of the parent of @p dev.
4257 bus_generic_map_resource(device_t dev, device_t child, int type,
4258 struct resource *r, struct resource_map_request *args,
4259 struct resource_map *map)
4261 /* Propagate up the bus hierarchy until someone handles it. */
4263 return (BUS_MAP_RESOURCE(dev->parent, child, type, r, args,
4269 * @brief Helper function for implementing BUS_UNMAP_RESOURCE().
4271 * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the
4272 * BUS_UNMAP_RESOURCE() method of the parent of @p dev.
4275 bus_generic_unmap_resource(device_t dev, device_t child, int type,
4276 struct resource *r, struct resource_map *map)
4278 /* Propagate up the bus hierarchy until someone handles it. */
4280 return (BUS_UNMAP_RESOURCE(dev->parent, child, type, r, map));
4285 * @brief Helper function for implementing BUS_BIND_INTR().
4287 * This simple implementation of BUS_BIND_INTR() simply calls the
4288 * BUS_BIND_INTR() method of the parent of @p dev.
4291 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
4294 /* Propagate up the bus hierarchy until someone handles it. */
4296 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
4301 * @brief Helper function for implementing BUS_CONFIG_INTR().
4303 * This simple implementation of BUS_CONFIG_INTR() simply calls the
4304 * BUS_CONFIG_INTR() method of the parent of @p dev.
4307 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
4308 enum intr_polarity pol)
4310 /* Propagate up the bus hierarchy until someone handles it. */
4312 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
4317 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
4319 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
4320 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
4323 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
4324 void *cookie, const char *descr)
4326 /* Propagate up the bus hierarchy until someone handles it. */
4328 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
4334 * @brief Helper function for implementing BUS_GET_CPUS().
4336 * This simple implementation of BUS_GET_CPUS() simply calls the
4337 * BUS_GET_CPUS() method of the parent of @p dev.
4340 bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op,
4341 size_t setsize, cpuset_t *cpuset)
4343 /* Propagate up the bus hierarchy until someone handles it. */
4344 if (dev->parent != NULL)
4345 return (BUS_GET_CPUS(dev->parent, child, op, setsize, cpuset));
4350 * @brief Helper function for implementing BUS_GET_DMA_TAG().
4352 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
4353 * BUS_GET_DMA_TAG() method of the parent of @p dev.
4356 bus_generic_get_dma_tag(device_t dev, device_t child)
4358 /* Propagate up the bus hierarchy until someone handles it. */
4359 if (dev->parent != NULL)
4360 return (BUS_GET_DMA_TAG(dev->parent, child));
4365 * @brief Helper function for implementing BUS_GET_BUS_TAG().
4367 * This simple implementation of BUS_GET_BUS_TAG() simply calls the
4368 * BUS_GET_BUS_TAG() method of the parent of @p dev.
4371 bus_generic_get_bus_tag(device_t dev, device_t child)
4373 /* Propagate up the bus hierarchy until someone handles it. */
4374 if (dev->parent != NULL)
4375 return (BUS_GET_BUS_TAG(dev->parent, child));
4376 return ((bus_space_tag_t)0);
4380 * @brief Helper function for implementing BUS_GET_RESOURCE().
4382 * This implementation of BUS_GET_RESOURCE() uses the
4383 * resource_list_find() function to do most of the work. It calls
4384 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4388 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4389 rman_res_t *startp, rman_res_t *countp)
4391 struct resource_list * rl = NULL;
4392 struct resource_list_entry * rle = NULL;
4394 rl = BUS_GET_RESOURCE_LIST(dev, child);
4398 rle = resource_list_find(rl, type, rid);
4403 *startp = rle->start;
4405 *countp = rle->count;
4411 * @brief Helper function for implementing BUS_SET_RESOURCE().
4413 * This implementation of BUS_SET_RESOURCE() uses the
4414 * resource_list_add() function to do most of the work. It calls
4415 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4419 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4420 rman_res_t start, rman_res_t count)
4422 struct resource_list * rl = NULL;
4424 rl = BUS_GET_RESOURCE_LIST(dev, child);
4428 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4434 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4436 * This implementation of BUS_DELETE_RESOURCE() uses the
4437 * resource_list_delete() function to do most of the work. It calls
4438 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4442 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4444 struct resource_list * rl = NULL;
4446 rl = BUS_GET_RESOURCE_LIST(dev, child);
4450 resource_list_delete(rl, type, rid);
4456 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4458 * This implementation of BUS_RELEASE_RESOURCE() uses the
4459 * resource_list_release() function to do most of the work. It calls
4460 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4463 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4464 int rid, struct resource *r)
4466 struct resource_list * rl = NULL;
4468 if (device_get_parent(child) != dev)
4469 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4472 rl = BUS_GET_RESOURCE_LIST(dev, child);
4476 return (resource_list_release(rl, dev, child, type, rid, r));
4480 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4482 * This implementation of BUS_ALLOC_RESOURCE() uses the
4483 * resource_list_alloc() function to do most of the work. It calls
4484 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4487 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4488 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4490 struct resource_list * rl = NULL;
4492 if (device_get_parent(child) != dev)
4493 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4494 type, rid, start, end, count, flags));
4496 rl = BUS_GET_RESOURCE_LIST(dev, child);
4500 return (resource_list_alloc(rl, dev, child, type, rid,
4501 start, end, count, flags));
4505 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4507 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4508 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4511 bus_generic_child_present(device_t dev, device_t child)
4513 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4517 bus_generic_get_domain(device_t dev, device_t child, int *domain)
4520 return (BUS_GET_DOMAIN(dev->parent, dev, domain));
4526 * @brief Helper function for implementing BUS_RESCAN().
4528 * This null implementation of BUS_RESCAN() always fails to indicate
4529 * the bus does not support rescanning.
4532 bus_null_rescan(device_t dev)
4538 * Some convenience functions to make it easier for drivers to use the
4539 * resource-management functions. All these really do is hide the
4540 * indirection through the parent's method table, making for slightly
4541 * less-wordy code. In the future, it might make sense for this code
4542 * to maintain some sort of a list of resources allocated by each device.
4546 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4547 struct resource **res)
4551 for (i = 0; rs[i].type != -1; i++)
4553 for (i = 0; rs[i].type != -1; i++) {
4554 res[i] = bus_alloc_resource_any(dev,
4555 rs[i].type, &rs[i].rid, rs[i].flags);
4556 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4557 bus_release_resources(dev, rs, res);
4565 bus_release_resources(device_t dev, const struct resource_spec *rs,
4566 struct resource **res)
4570 for (i = 0; rs[i].type != -1; i++)
4571 if (res[i] != NULL) {
4572 bus_release_resource(
4573 dev, rs[i].type, rs[i].rid, res[i]);
4579 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4581 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4585 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
4586 rman_res_t end, rman_res_t count, u_int flags)
4588 struct resource *res;
4590 if (dev->parent == NULL)
4592 res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4598 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4600 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4604 bus_adjust_resource(device_t dev, int type, struct resource *r, rman_res_t start,
4607 if (dev->parent == NULL)
4609 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4613 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4615 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4619 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4621 if (dev->parent == NULL)
4623 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4627 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4629 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4633 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4635 if (dev->parent == NULL)
4637 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4641 * @brief Wrapper function for BUS_MAP_RESOURCE().
4643 * This function simply calls the BUS_MAP_RESOURCE() method of the
4647 bus_map_resource(device_t dev, int type, struct resource *r,
4648 struct resource_map_request *args, struct resource_map *map)
4650 if (dev->parent == NULL)
4652 return (BUS_MAP_RESOURCE(dev->parent, dev, type, r, args, map));
4656 * @brief Wrapper function for BUS_UNMAP_RESOURCE().
4658 * This function simply calls the BUS_UNMAP_RESOURCE() method of the
4662 bus_unmap_resource(device_t dev, int type, struct resource *r,
4663 struct resource_map *map)
4665 if (dev->parent == NULL)
4667 return (BUS_UNMAP_RESOURCE(dev->parent, dev, type, r, map));
4671 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4673 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4677 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4681 if (dev->parent == NULL)
4683 rv = BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r);
4688 * @brief Wrapper function for BUS_SETUP_INTR().
4690 * This function simply calls the BUS_SETUP_INTR() method of the
4694 bus_setup_intr(device_t dev, struct resource *r, int flags,
4695 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4699 if (dev->parent == NULL)
4701 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4705 if (handler != NULL && !(flags & INTR_MPSAFE))
4706 device_printf(dev, "[GIANT-LOCKED]\n");
4711 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4713 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4717 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4719 if (dev->parent == NULL)
4721 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4725 * @brief Wrapper function for BUS_SUSPEND_INTR().
4727 * This function simply calls the BUS_SUSPEND_INTR() method of the
4731 bus_suspend_intr(device_t dev, struct resource *r)
4733 if (dev->parent == NULL)
4735 return (BUS_SUSPEND_INTR(dev->parent, dev, r));
4739 * @brief Wrapper function for BUS_RESUME_INTR().
4741 * This function simply calls the BUS_RESUME_INTR() method of the
4745 bus_resume_intr(device_t dev, struct resource *r)
4747 if (dev->parent == NULL)
4749 return (BUS_RESUME_INTR(dev->parent, dev, r));
4753 * @brief Wrapper function for BUS_BIND_INTR().
4755 * This function simply calls the BUS_BIND_INTR() method of the
4759 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4761 if (dev->parent == NULL)
4763 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4767 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4769 * This function first formats the requested description into a
4770 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4771 * the parent of @p dev.
4774 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4775 const char *fmt, ...)
4778 char descr[MAXCOMLEN + 1];
4780 if (dev->parent == NULL)
4783 vsnprintf(descr, sizeof(descr), fmt, ap);
4785 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4789 * @brief Wrapper function for BUS_SET_RESOURCE().
4791 * This function simply calls the BUS_SET_RESOURCE() method of the
4795 bus_set_resource(device_t dev, int type, int rid,
4796 rman_res_t start, rman_res_t count)
4798 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4803 * @brief Wrapper function for BUS_GET_RESOURCE().
4805 * This function simply calls the BUS_GET_RESOURCE() method of the
4809 bus_get_resource(device_t dev, int type, int rid,
4810 rman_res_t *startp, rman_res_t *countp)
4812 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4817 * @brief Wrapper function for BUS_GET_RESOURCE().
4819 * This function simply calls the BUS_GET_RESOURCE() method of the
4820 * parent of @p dev and returns the start value.
4823 bus_get_resource_start(device_t dev, int type, int rid)
4829 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4837 * @brief Wrapper function for BUS_GET_RESOURCE().
4839 * This function simply calls the BUS_GET_RESOURCE() method of the
4840 * parent of @p dev and returns the count value.
4843 bus_get_resource_count(device_t dev, int type, int rid)
4849 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4857 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4859 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4863 bus_delete_resource(device_t dev, int type, int rid)
4865 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4869 * @brief Wrapper function for BUS_CHILD_PRESENT().
4871 * This function simply calls the BUS_CHILD_PRESENT() method of the
4875 bus_child_present(device_t child)
4877 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4881 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4883 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4887 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4891 parent = device_get_parent(child);
4892 if (parent == NULL) {
4896 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4900 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4902 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4906 bus_child_location_str(device_t child, char *buf, size_t buflen)
4910 parent = device_get_parent(child);
4911 if (parent == NULL) {
4915 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4919 * @brief Wrapper function for bus_child_pnpinfo_str using sbuf
4921 * A convenient wrapper frunction for bus_child_pnpinfo_str that allows
4922 * us to splat that into an sbuf. It uses unholy knowledge of sbuf to
4923 * accomplish this, however. It is an interim function until we can convert
4924 * this interface more fully.
4926 /* Note: we reach inside of sbuf because it's API isn't rich enough to do this */
4927 #define SPACE(s) ((s)->s_size - (s)->s_len)
4928 #define EOB(s) ((s)->s_buf + (s)->s_len)
4931 bus_child_pnpinfo_sb(device_t dev, struct sbuf *sb)
4936 MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0);
4937 if (sb->s_error != 0)
4940 *p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
4943 sb->s_error = ENOMEM;
4946 bus_child_pnpinfo_str(dev, p, space);
4947 sb->s_len += strlen(p);
4952 * @brief Wrapper function for bus_child_pnpinfo_str using sbuf
4954 * A convenient wrapper frunction for bus_child_pnpinfo_str that allows
4955 * us to splat that into an sbuf. It uses unholy knowledge of sbuf to
4956 * accomplish this, however. It is an interim function until we can convert
4957 * this interface more fully.
4960 bus_child_location_sb(device_t dev, struct sbuf *sb)
4965 MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0);
4966 if (sb->s_error != 0)
4969 *p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
4972 sb->s_error = ENOMEM;
4975 bus_child_location_str(dev, p, space);
4976 sb->s_len += strlen(p);
4983 * @brief Wrapper function for BUS_GET_CPUS().
4985 * This function simply calls the BUS_GET_CPUS() method of the
4989 bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, cpuset_t *cpuset)
4993 parent = device_get_parent(dev);
4996 return (BUS_GET_CPUS(parent, dev, op, setsize, cpuset));
5000 * @brief Wrapper function for BUS_GET_DMA_TAG().
5002 * This function simply calls the BUS_GET_DMA_TAG() method of the
5006 bus_get_dma_tag(device_t dev)
5010 parent = device_get_parent(dev);
5013 return (BUS_GET_DMA_TAG(parent, dev));
5017 * @brief Wrapper function for BUS_GET_BUS_TAG().
5019 * This function simply calls the BUS_GET_BUS_TAG() method of the
5023 bus_get_bus_tag(device_t dev)
5027 parent = device_get_parent(dev);
5029 return ((bus_space_tag_t)0);
5030 return (BUS_GET_BUS_TAG(parent, dev));
5034 * @brief Wrapper function for BUS_GET_DOMAIN().
5036 * This function simply calls the BUS_GET_DOMAIN() method of the
5040 bus_get_domain(device_t dev, int *domain)
5042 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain));
5045 /* Resume all devices and then notify userland that we're up again. */
5047 root_resume(device_t dev)
5051 error = bus_generic_resume(dev);
5053 devctl_notify("kern", "power", "resume", NULL); /* Deprecated gone in 14 */
5054 devctl_notify("kernel", "power", "resume", NULL);
5060 root_print_child(device_t dev, device_t child)
5064 retval += bus_print_child_header(dev, child);
5065 retval += printf("\n");
5071 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
5072 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
5075 * If an interrupt mapping gets to here something bad has happened.
5077 panic("root_setup_intr");
5081 * If we get here, assume that the device is permanent and really is
5082 * present in the system. Removable bus drivers are expected to intercept
5083 * this call long before it gets here. We return -1 so that drivers that
5084 * really care can check vs -1 or some ERRNO returned higher in the food
5088 root_child_present(device_t dev, device_t child)
5094 root_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
5099 /* Default to returning the set of all CPUs. */
5100 if (setsize != sizeof(cpuset_t))
5109 static kobj_method_t root_methods[] = {
5110 /* Device interface */
5111 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
5112 KOBJMETHOD(device_suspend, bus_generic_suspend),
5113 KOBJMETHOD(device_resume, root_resume),
5116 KOBJMETHOD(bus_print_child, root_print_child),
5117 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
5118 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
5119 KOBJMETHOD(bus_setup_intr, root_setup_intr),
5120 KOBJMETHOD(bus_child_present, root_child_present),
5121 KOBJMETHOD(bus_get_cpus, root_get_cpus),
5126 static driver_t root_driver = {
5133 devclass_t root_devclass;
5136 root_bus_module_handler(module_t mod, int what, void* arg)
5140 TAILQ_INIT(&bus_data_devices);
5141 kobj_class_compile((kobj_class_t) &root_driver);
5142 root_bus = make_device(NULL, "root", 0);
5143 root_bus->desc = "System root bus";
5144 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
5145 root_bus->driver = &root_driver;
5146 root_bus->state = DS_ATTACHED;
5147 root_devclass = devclass_find_internal("root", NULL, FALSE);
5152 device_shutdown(root_bus);
5155 return (EOPNOTSUPP);
5161 static moduledata_t root_bus_mod = {
5163 root_bus_module_handler,
5166 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
5169 * @brief Automatically configure devices
5171 * This function begins the autoconfiguration process by calling
5172 * device_probe_and_attach() for each child of the @c root0 device.
5175 root_bus_configure(void)
5179 /* Eventually this will be split up, but this is sufficient for now. */
5180 bus_set_pass(BUS_PASS_DEFAULT);
5184 * @brief Module handler for registering device drivers
5186 * This module handler is used to automatically register device
5187 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
5188 * devclass_add_driver() for the driver described by the
5189 * driver_module_data structure pointed to by @p arg
5192 driver_module_handler(module_t mod, int what, void *arg)
5194 struct driver_module_data *dmd;
5195 devclass_t bus_devclass;
5196 kobj_class_t driver;
5199 dmd = (struct driver_module_data *)arg;
5200 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
5205 if (dmd->dmd_chainevh)
5206 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5208 pass = dmd->dmd_pass;
5209 driver = dmd->dmd_driver;
5210 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
5211 DRIVERNAME(driver), dmd->dmd_busname, pass));
5212 error = devclass_add_driver(bus_devclass, driver, pass,
5217 PDEBUG(("Unloading module: driver %s from bus %s",
5218 DRIVERNAME(dmd->dmd_driver),
5220 error = devclass_delete_driver(bus_devclass,
5223 if (!error && dmd->dmd_chainevh)
5224 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5227 PDEBUG(("Quiesce module: driver %s from bus %s",
5228 DRIVERNAME(dmd->dmd_driver),
5230 error = devclass_quiesce_driver(bus_devclass,
5233 if (!error && dmd->dmd_chainevh)
5234 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5245 * @brief Enumerate all hinted devices for this bus.
5247 * Walks through the hints for this bus and calls the bus_hinted_child
5248 * routine for each one it fines. It searches first for the specific
5249 * bus that's being probed for hinted children (eg isa0), and then for
5250 * generic children (eg isa).
5252 * @param dev bus device to enumerate
5255 bus_enumerate_hinted_children(device_t bus)
5258 const char *dname, *busname;
5262 * enumerate all devices on the specific bus
5264 busname = device_get_nameunit(bus);
5266 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5267 BUS_HINTED_CHILD(bus, dname, dunit);
5270 * and all the generic ones.
5272 busname = device_get_name(bus);
5274 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5275 BUS_HINTED_CHILD(bus, dname, dunit);
5280 /* the _short versions avoid iteration by not calling anything that prints
5281 * more than oneliners. I love oneliners.
5285 print_device_short(device_t dev, int indent)
5290 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
5291 dev->unit, dev->desc,
5292 (dev->parent? "":"no "),
5293 (TAILQ_EMPTY(&dev->children)? "no ":""),
5294 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
5295 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
5296 (dev->flags&DF_WILDCARD? "wildcard,":""),
5297 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
5298 (dev->flags&DF_REBID? "rebiddable,":""),
5299 (dev->flags&DF_SUSPENDED? "suspended,":""),
5300 (dev->ivars? "":"no "),
5301 (dev->softc? "":"no "),
5306 print_device(device_t dev, int indent)
5311 print_device_short(dev, indent);
5313 indentprintf(("Parent:\n"));
5314 print_device_short(dev->parent, indent+1);
5315 indentprintf(("Driver:\n"));
5316 print_driver_short(dev->driver, indent+1);
5317 indentprintf(("Devclass:\n"));
5318 print_devclass_short(dev->devclass, indent+1);
5322 print_device_tree_short(device_t dev, int indent)
5323 /* print the device and all its children (indented) */
5330 print_device_short(dev, indent);
5332 TAILQ_FOREACH(child, &dev->children, link) {
5333 print_device_tree_short(child, indent+1);
5338 print_device_tree(device_t dev, int indent)
5339 /* print the device and all its children (indented) */
5346 print_device(dev, indent);
5348 TAILQ_FOREACH(child, &dev->children, link) {
5349 print_device_tree(child, indent+1);
5354 print_driver_short(driver_t *driver, int indent)
5359 indentprintf(("driver %s: softc size = %zd\n",
5360 driver->name, driver->size));
5364 print_driver(driver_t *driver, int indent)
5369 print_driver_short(driver, indent);
5373 print_driver_list(driver_list_t drivers, int indent)
5375 driverlink_t driver;
5377 TAILQ_FOREACH(driver, &drivers, link) {
5378 print_driver(driver->driver, indent);
5383 print_devclass_short(devclass_t dc, int indent)
5388 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
5392 print_devclass(devclass_t dc, int indent)
5399 print_devclass_short(dc, indent);
5400 indentprintf(("Drivers:\n"));
5401 print_driver_list(dc->drivers, indent+1);
5403 indentprintf(("Devices:\n"));
5404 for (i = 0; i < dc->maxunit; i++)
5406 print_device(dc->devices[i], indent+1);
5410 print_devclass_list_short(void)
5414 printf("Short listing of devclasses, drivers & devices:\n");
5415 TAILQ_FOREACH(dc, &devclasses, link) {
5416 print_devclass_short(dc, 0);
5421 print_devclass_list(void)
5425 printf("Full listing of devclasses, drivers & devices:\n");
5426 TAILQ_FOREACH(dc, &devclasses, link) {
5427 print_devclass(dc, 0);
5434 * User-space access to the device tree.
5436 * We implement a small set of nodes:
5438 * hw.bus Single integer read method to obtain the
5439 * current generation count.
5440 * hw.bus.devices Reads the entire device tree in flat space.
5441 * hw.bus.rman Resource manager interface
5443 * We might like to add the ability to scan devclasses and/or drivers to
5444 * determine what else is currently loaded/available.
5448 sysctl_bus_info(SYSCTL_HANDLER_ARGS)
5450 struct u_businfo ubus;
5452 ubus.ub_version = BUS_USER_VERSION;
5453 ubus.ub_generation = bus_data_generation;
5455 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
5457 SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD |
5458 CTLFLAG_MPSAFE, NULL, 0, sysctl_bus_info, "S,u_businfo",
5459 "bus-related data");
5462 sysctl_devices(SYSCTL_HANDLER_ARGS)
5465 int *name = (int *)arg1;
5466 u_int namelen = arg2;
5469 struct u_device *udev;
5475 if (bus_data_generation_check(name[0]))
5481 * Scan the list of devices, looking for the requested index.
5483 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5491 * Populate the return item, careful not to overflow the buffer.
5493 udev = malloc(sizeof(*udev), M_BUS, M_WAITOK | M_ZERO);
5496 udev->dv_handle = (uintptr_t)dev;
5497 udev->dv_parent = (uintptr_t)dev->parent;
5498 udev->dv_devflags = dev->devflags;
5499 udev->dv_flags = dev->flags;
5500 udev->dv_state = dev->state;
5501 sbuf_new(&sb, udev->dv_fields, sizeof(udev->dv_fields), SBUF_FIXEDLEN);
5502 if (dev->nameunit != NULL)
5503 sbuf_cat(&sb, dev->nameunit);
5505 sbuf_putc(&sb, '\0');
5506 sbuf_putc(&sb, '\0');
5507 if (dev->desc != NULL)
5508 sbuf_cat(&sb, dev->desc);
5510 sbuf_putc(&sb, '\0');
5511 sbuf_putc(&sb, '\0');
5512 if (dev->driver != NULL)
5513 sbuf_cat(&sb, dev->driver->name);
5515 sbuf_putc(&sb, '\0');
5516 sbuf_putc(&sb, '\0');
5517 bus_child_pnpinfo_sb(dev, &sb);
5518 sbuf_putc(&sb, '\0');
5519 bus_child_location_sb(dev, &sb);
5520 sbuf_putc(&sb, '\0');
5521 error = sbuf_finish(&sb);
5523 error = SYSCTL_OUT(req, udev, sizeof(*udev));
5529 SYSCTL_NODE(_hw_bus, OID_AUTO, devices,
5530 CTLFLAG_RD | CTLFLAG_NEEDGIANT, sysctl_devices,
5531 "system device tree");
5534 bus_data_generation_check(int generation)
5536 if (generation != bus_data_generation)
5539 /* XXX generate optimised lists here? */
5544 bus_data_generation_update(void)
5546 atomic_add_int(&bus_data_generation, 1);
5550 bus_free_resource(device_t dev, int type, struct resource *r)
5554 return (bus_release_resource(dev, type, rman_get_rid(r), r));
5558 device_lookup_by_name(const char *name)
5562 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5563 if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0)
5570 * /dev/devctl2 implementation. The existing /dev/devctl device has
5571 * implicit semantics on open, so it could not be reused for this.
5572 * Another option would be to call this /dev/bus?
5575 find_device(struct devreq *req, device_t *devp)
5580 * First, ensure that the name is nul terminated.
5582 if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL)
5586 * Second, try to find an attached device whose name matches
5589 dev = device_lookup_by_name(req->dr_name);
5595 /* Finally, give device enumerators a chance. */
5597 EVENTHANDLER_DIRECT_INVOKE(dev_lookup, req->dr_name, &dev);
5605 driver_exists(device_t bus, const char *driver)
5609 for (dc = bus->devclass; dc != NULL; dc = dc->parent) {
5610 if (devclass_find_driver_internal(dc, driver) != NULL)
5617 device_gen_nomatch(device_t dev)
5621 if (dev->flags & DF_NEEDNOMATCH &&
5622 dev->state == DS_NOTPRESENT) {
5623 BUS_PROBE_NOMATCH(dev->parent, dev);
5625 dev->flags |= DF_DONENOMATCH;
5627 dev->flags &= ~DF_NEEDNOMATCH;
5628 TAILQ_FOREACH(child, &dev->children, link) {
5629 device_gen_nomatch(child);
5634 device_do_deferred_actions(void)
5640 * Walk through the devclasses to find all the drivers we've tagged as
5641 * deferred during the freeze and call the driver added routines. They
5642 * have already been added to the lists in the background, so the driver
5643 * added routines that trigger a probe will have all the right bidders
5644 * for the probe auction.
5646 TAILQ_FOREACH(dc, &devclasses, link) {
5647 TAILQ_FOREACH(dl, &dc->drivers, link) {
5648 if (dl->flags & DL_DEFERRED_PROBE) {
5649 devclass_driver_added(dc, dl->driver);
5650 dl->flags &= ~DL_DEFERRED_PROBE;
5656 * We also defer no-match events during a freeze. Walk the tree and
5657 * generate all the pent-up events that are still relevant.
5659 device_gen_nomatch(root_bus);
5660 bus_data_generation_update();
5664 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
5671 /* Locate the device to control. */
5673 req = (struct devreq *)data;
5681 case DEV_SET_DRIVER:
5682 case DEV_CLEAR_DRIVER:
5686 error = priv_check(td, PRIV_DRIVER);
5688 error = find_device(req, &dev);
5692 error = priv_check(td, PRIV_DRIVER);
5703 /* Perform the requested operation. */
5706 if (device_is_attached(dev) && (dev->flags & DF_REBID) == 0)
5708 else if (!device_is_enabled(dev))
5711 error = device_probe_and_attach(dev);
5714 if (!device_is_attached(dev)) {
5718 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5719 error = device_quiesce(dev);
5723 error = device_detach(dev);
5726 if (device_is_enabled(dev)) {
5732 * If the device has been probed but not attached (e.g.
5733 * when it has been disabled by a loader hint), just
5734 * attach the device rather than doing a full probe.
5737 if (device_is_alive(dev)) {
5739 * If the device was disabled via a hint, clear
5742 if (resource_disabled(dev->driver->name, dev->unit))
5743 resource_unset_value(dev->driver->name,
5744 dev->unit, "disabled");
5745 error = device_attach(dev);
5747 error = device_probe_and_attach(dev);
5750 if (!device_is_enabled(dev)) {
5755 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5756 error = device_quiesce(dev);
5762 * Force DF_FIXEDCLASS on around detach to preserve
5763 * the existing name.
5766 dev->flags |= DF_FIXEDCLASS;
5767 error = device_detach(dev);
5768 if (!(old & DF_FIXEDCLASS))
5769 dev->flags &= ~DF_FIXEDCLASS;
5771 device_disable(dev);
5774 if (device_is_suspended(dev)) {
5778 if (device_get_parent(dev) == NULL) {
5782 error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev);
5785 if (!device_is_suspended(dev)) {
5789 if (device_get_parent(dev) == NULL) {
5793 error = BUS_RESUME_CHILD(device_get_parent(dev), dev);
5795 case DEV_SET_DRIVER: {
5799 error = copyinstr(req->dr_data, driver, sizeof(driver), NULL);
5802 if (driver[0] == '\0') {
5806 if (dev->devclass != NULL &&
5807 strcmp(driver, dev->devclass->name) == 0)
5808 /* XXX: Could possibly force DF_FIXEDCLASS on? */
5812 * Scan drivers for this device's bus looking for at
5813 * least one matching driver.
5815 if (dev->parent == NULL) {
5819 if (!driver_exists(dev->parent, driver)) {
5823 dc = devclass_create(driver);
5829 /* Detach device if necessary. */
5830 if (device_is_attached(dev)) {
5831 if (req->dr_flags & DEVF_SET_DRIVER_DETACH)
5832 error = device_detach(dev);
5839 /* Clear any previously-fixed device class and unit. */
5840 if (dev->flags & DF_FIXEDCLASS)
5841 devclass_delete_device(dev->devclass, dev);
5842 dev->flags |= DF_WILDCARD;
5845 /* Force the new device class. */
5846 error = devclass_add_device(dc, dev);
5849 dev->flags |= DF_FIXEDCLASS;
5850 error = device_probe_and_attach(dev);
5853 case DEV_CLEAR_DRIVER:
5854 if (!(dev->flags & DF_FIXEDCLASS)) {
5858 if (device_is_attached(dev)) {
5859 if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH)
5860 error = device_detach(dev);
5867 dev->flags &= ~DF_FIXEDCLASS;
5868 dev->flags |= DF_WILDCARD;
5869 devclass_delete_device(dev->devclass, dev);
5870 error = device_probe_and_attach(dev);
5873 if (!device_is_attached(dev)) {
5877 error = BUS_RESCAN(dev);
5882 parent = device_get_parent(dev);
5883 if (parent == NULL) {
5887 if (!(req->dr_flags & DEVF_FORCE_DELETE)) {
5888 if (bus_child_present(dev) != 0) {
5894 error = device_delete_child(parent, dev);
5901 device_frozen = true;
5907 device_do_deferred_actions();
5908 device_frozen = false;
5912 if ((req->dr_flags & ~(DEVF_RESET_DETACH)) != 0) {
5916 error = BUS_RESET_CHILD(device_get_parent(dev), dev,
5924 static struct cdevsw devctl2_cdevsw = {
5925 .d_version = D_VERSION,
5926 .d_ioctl = devctl2_ioctl,
5927 .d_name = "devctl2",
5933 make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL,
5934 UID_ROOT, GID_WHEEL, 0600, "devctl2");
5938 * APIs to manage deprecation and obsolescence.
5940 static int obsolete_panic = 0;
5941 SYSCTL_INT(_debug, OID_AUTO, obsolete_panic, CTLFLAG_RWTUN, &obsolete_panic, 0,
5942 "Panic when obsolete features are used (0 = never, 1 = if osbolete, "
5943 "2 = if deprecated)");
5946 gone_panic(int major, int running, const char *msg)
5948 switch (obsolete_panic)
5953 if (running < major)
5962 _gone_in(int major, const char *msg)
5964 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
5965 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
5966 printf("Obsolete code will be removed soon: %s\n", msg);
5968 printf("Deprecated code (to be removed in FreeBSD %d): %s\n",
5973 _gone_in_dev(device_t dev, int major, const char *msg)
5975 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
5976 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
5978 "Obsolete code will be removed soon: %s\n", msg);
5981 "Deprecated code (to be removed in FreeBSD %d): %s\n",
5986 DB_SHOW_COMMAND(device, db_show_device)
5993 dev = (device_t)addr;
5995 db_printf("name: %s\n", device_get_nameunit(dev));
5996 db_printf(" driver: %s\n", DRIVERNAME(dev->driver));
5997 db_printf(" class: %s\n", DEVCLANAME(dev->devclass));
5998 db_printf(" addr: %p\n", dev);
5999 db_printf(" parent: %p\n", dev->parent);
6000 db_printf(" softc: %p\n", dev->softc);
6001 db_printf(" ivars: %p\n", dev->ivars);
6004 DB_SHOW_ALL_COMMAND(devices, db_show_all_devices)
6008 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
6009 db_show_device((db_expr_t)dev, true, count, modif);