2 * SPDX-License-Identifier: BSD-2-Clause
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>
33 #include <sys/param.h>
35 #include <sys/domainset.h>
36 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
44 #include <machine/bus.h>
45 #include <sys/random.h>
46 #include <sys/refcount.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
53 #include <sys/cpuset.h>
57 #include <machine/cpu.h>
58 #include <machine/stdarg.h>
65 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
67 SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
70 static bool disable_failed_devs = false;
71 SYSCTL_BOOL(_hw_bus, OID_AUTO, disable_failed_devices, CTLFLAG_RWTUN, &disable_failed_devs,
72 0, "Do not retry attaching devices that return an error from DEVICE_ATTACH the first time");
75 * Used to attach drivers to devclasses.
77 typedef struct driverlink *driverlink_t;
80 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
83 #define DL_DEFERRED_PROBE 1 /* Probe deferred on this */
84 TAILQ_ENTRY(driverlink) passlink;
88 * Forward declarations
90 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
91 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
92 typedef TAILQ_HEAD(device_list, _device) device_list_t;
95 TAILQ_ENTRY(devclass) link;
96 devclass_t parent; /* parent in devclass hierarchy */
97 driver_list_t drivers; /* bus devclasses store drivers for bus */
99 device_t *devices; /* array of devices indexed by unit */
100 int maxunit; /* size of devices array */
102 #define DC_HAS_CHILDREN 1
104 struct sysctl_ctx_list sysctl_ctx;
105 struct sysctl_oid *sysctl_tree;
109 * @brief Implementation of _device.
111 * The structure is named "_device" instead of "device" to avoid type confusion
112 * caused by other subsystems defining a (struct device).
116 * A device is a kernel object. The first field must be the
117 * current ops table for the object.
124 TAILQ_ENTRY(_device) link; /**< list of devices in parent */
125 TAILQ_ENTRY(_device) devlink; /**< global device list membership */
126 device_t parent; /**< parent of this device */
127 device_list_t children; /**< list of child devices */
130 * Details of this device.
132 driver_t *driver; /**< current driver */
133 devclass_t devclass; /**< current device class */
134 int unit; /**< current unit number */
135 char* nameunit; /**< name+unit e.g. foodev0 */
136 char* desc; /**< driver specific description */
137 u_int busy; /**< count of calls to device_busy() */
138 device_state_t state; /**< current device state */
139 uint32_t devflags; /**< api level flags for device_get_flags() */
140 u_int flags; /**< internal device flags */
141 u_int order; /**< order from device_add_child_ordered() */
142 void *ivars; /**< instance variables */
143 void *softc; /**< current driver's variables */
145 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
146 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
149 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
150 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
152 EVENTHANDLER_LIST_DEFINE(device_attach);
153 EVENTHANDLER_LIST_DEFINE(device_detach);
154 EVENTHANDLER_LIST_DEFINE(device_nomatch);
155 EVENTHANDLER_LIST_DEFINE(dev_lookup);
157 static void devctl2_init(void);
158 static bool device_frozen;
160 #define DRIVERNAME(d) ((d)? d->name : "no driver")
161 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
165 static int bus_debug = 1;
166 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
168 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
169 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
172 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
173 * prevent syslog from deleting initial spaces
175 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
177 static void print_device_short(device_t dev, int indent);
178 static void print_device(device_t dev, int indent);
179 void print_device_tree_short(device_t dev, int indent);
180 void print_device_tree(device_t dev, int indent);
181 static void print_driver_short(driver_t *driver, int indent);
182 static void print_driver(driver_t *driver, int indent);
183 static void print_driver_list(driver_list_t drivers, int indent);
184 static void print_devclass_short(devclass_t dc, int indent);
185 static void print_devclass(devclass_t dc, int indent);
186 void print_devclass_list_short(void);
187 void print_devclass_list(void);
190 /* Make the compiler ignore the function calls */
191 #define PDEBUG(a) /* nop */
192 #define DEVICENAME(d) /* nop */
194 #define print_device_short(d,i) /* nop */
195 #define print_device(d,i) /* nop */
196 #define print_device_tree_short(d,i) /* nop */
197 #define print_device_tree(d,i) /* nop */
198 #define print_driver_short(d,i) /* nop */
199 #define print_driver(d,i) /* nop */
200 #define print_driver_list(d,i) /* nop */
201 #define print_devclass_short(d,i) /* nop */
202 #define print_devclass(d,i) /* nop */
203 #define print_devclass_list_short() /* nop */
204 #define print_devclass_list() /* nop */
212 DEVCLASS_SYSCTL_PARENT,
216 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
218 devclass_t dc = (devclass_t)arg1;
222 case DEVCLASS_SYSCTL_PARENT:
223 value = dc->parent ? dc->parent->name : "";
228 return (SYSCTL_OUT_STR(req, value));
232 devclass_sysctl_init(devclass_t dc)
234 if (dc->sysctl_tree != NULL)
236 sysctl_ctx_init(&dc->sysctl_ctx);
237 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
238 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
239 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
240 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
242 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
243 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
249 DEVICE_SYSCTL_DRIVER,
250 DEVICE_SYSCTL_LOCATION,
251 DEVICE_SYSCTL_PNPINFO,
252 DEVICE_SYSCTL_PARENT,
256 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
259 device_t dev = (device_t)arg1;
262 sbuf_new_for_sysctl(&sb, NULL, 1024, req);
263 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
266 case DEVICE_SYSCTL_DESC:
267 sbuf_cat(&sb, dev->desc ? dev->desc : "");
269 case DEVICE_SYSCTL_DRIVER:
270 sbuf_cat(&sb, dev->driver ? dev->driver->name : "");
272 case DEVICE_SYSCTL_LOCATION:
273 bus_child_location(dev, &sb);
275 case DEVICE_SYSCTL_PNPINFO:
276 bus_child_pnpinfo(dev, &sb);
278 case DEVICE_SYSCTL_PARENT:
279 sbuf_cat(&sb, dev->parent ? dev->parent->nameunit : "");
285 error = sbuf_finish(&sb);
293 device_sysctl_init(device_t dev)
295 devclass_t dc = dev->devclass;
298 if (dev->sysctl_tree != NULL)
300 devclass_sysctl_init(dc);
301 sysctl_ctx_init(&dev->sysctl_ctx);
302 dev->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&dev->sysctl_ctx,
303 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
304 dev->nameunit + strlen(dc->name),
305 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", "device_index");
306 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
307 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
308 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
309 "device description");
310 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
312 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
313 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
314 "device driver name");
315 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
316 OID_AUTO, "%location",
317 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
318 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
319 "device location relative to parent");
320 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
321 OID_AUTO, "%pnpinfo",
322 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
323 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
324 "device identification");
325 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
327 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
328 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
330 if (bus_get_domain(dev, &domain) == 0)
331 SYSCTL_ADD_INT(&dev->sysctl_ctx,
332 SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain",
333 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, domain, "NUMA domain");
337 device_sysctl_update(device_t dev)
339 devclass_t dc = dev->devclass;
341 if (dev->sysctl_tree == NULL)
343 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
347 device_sysctl_fini(device_t dev)
349 if (dev->sysctl_tree == NULL)
351 sysctl_ctx_free(&dev->sysctl_ctx);
352 dev->sysctl_tree = NULL;
355 static struct device_list bus_data_devices;
356 static int bus_data_generation = 1;
358 static kobj_method_t null_methods[] = {
362 DEFINE_CLASS(null, null_methods, 0);
365 bus_topo_assert(void)
382 mtx_lock(bus_topo_mtx());
386 bus_topo_unlock(void)
389 mtx_unlock(bus_topo_mtx());
393 * Bus pass implementation
396 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
397 int bus_current_pass = BUS_PASS_ROOT;
401 * @brief Register the pass level of a new driver attachment
403 * Register a new driver attachment's pass level. If no driver
404 * attachment with the same pass level has been added, then @p new
405 * will be added to the global passes list.
407 * @param new the new driver attachment
410 driver_register_pass(struct driverlink *new)
412 struct driverlink *dl;
414 /* We only consider pass numbers during boot. */
415 if (bus_current_pass == BUS_PASS_DEFAULT)
419 * Walk the passes list. If we already know about this pass
420 * then there is nothing to do. If we don't, then insert this
421 * driver link into the list.
423 TAILQ_FOREACH(dl, &passes, passlink) {
424 if (dl->pass < new->pass)
426 if (dl->pass == new->pass)
428 TAILQ_INSERT_BEFORE(dl, new, passlink);
431 TAILQ_INSERT_TAIL(&passes, new, passlink);
435 * @brief Raise the current bus pass
437 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
438 * method on the root bus to kick off a new device tree scan for each
439 * new pass level that has at least one driver.
442 bus_set_pass(int pass)
444 struct driverlink *dl;
446 if (bus_current_pass > pass)
447 panic("Attempt to lower bus pass level");
449 TAILQ_FOREACH(dl, &passes, passlink) {
450 /* Skip pass values below the current pass level. */
451 if (dl->pass <= bus_current_pass)
455 * Bail once we hit a driver with a pass level that is
462 * Raise the pass level to the next level and rescan
465 bus_current_pass = dl->pass;
466 BUS_NEW_PASS(root_bus);
470 * If there isn't a driver registered for the requested pass,
471 * then bus_current_pass might still be less than 'pass'. Set
472 * it to 'pass' in that case.
474 if (bus_current_pass < pass)
475 bus_current_pass = pass;
476 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
480 * Devclass implementation
483 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
487 * @brief Find or create a device class
489 * If a device class with the name @p classname exists, return it,
490 * otherwise if @p create is non-zero create and return a new device
493 * If @p parentname is non-NULL, the parent of the devclass is set to
494 * the devclass of that name.
496 * @param classname the devclass name to find or create
497 * @param parentname the parent devclass name or @c NULL
498 * @param create non-zero to create a devclass
501 devclass_find_internal(const char *classname, const char *parentname,
506 PDEBUG(("looking for %s", classname));
510 TAILQ_FOREACH(dc, &devclasses, link) {
511 if (!strcmp(dc->name, classname))
516 PDEBUG(("creating %s", classname));
517 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
518 M_BUS, M_NOWAIT | M_ZERO);
522 dc->name = (char*) (dc + 1);
523 strcpy(dc->name, classname);
524 TAILQ_INIT(&dc->drivers);
525 TAILQ_INSERT_TAIL(&devclasses, dc, link);
527 bus_data_generation_update();
531 * If a parent class is specified, then set that as our parent so
532 * that this devclass will support drivers for the parent class as
533 * well. If the parent class has the same name don't do this though
534 * as it creates a cycle that can trigger an infinite loop in
535 * device_probe_child() if a device exists for which there is no
538 if (parentname && dc && !dc->parent &&
539 strcmp(classname, parentname) != 0) {
540 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
541 dc->parent->flags |= DC_HAS_CHILDREN;
548 * @brief Create a device class
550 * If a device class with the name @p classname exists, return it,
551 * otherwise create and return a new device class.
553 * @param classname the devclass name to find or create
556 devclass_create(const char *classname)
558 return (devclass_find_internal(classname, NULL, TRUE));
562 * @brief Find a device class
564 * If a device class with the name @p classname exists, return it,
565 * otherwise return @c NULL.
567 * @param classname the devclass name to find
570 devclass_find(const char *classname)
572 return (devclass_find_internal(classname, NULL, FALSE));
576 * @brief Register that a device driver has been added to a devclass
578 * Register that a device driver has been added to a devclass. This
579 * is called by devclass_add_driver to accomplish the recursive
580 * notification of all the children classes of dc, as well as dc.
581 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
584 * We do a full search here of the devclass list at each iteration
585 * level to save storing children-lists in the devclass structure. If
586 * we ever move beyond a few dozen devices doing this, we may need to
589 * @param dc the devclass to edit
590 * @param driver the driver that was just added
593 devclass_driver_added(devclass_t dc, driver_t *driver)
599 * Call BUS_DRIVER_ADDED for any existing buses in this class.
601 for (i = 0; i < dc->maxunit; i++)
602 if (dc->devices[i] && device_is_attached(dc->devices[i]))
603 BUS_DRIVER_ADDED(dc->devices[i], driver);
606 * Walk through the children classes. Since we only keep a
607 * single parent pointer around, we walk the entire list of
608 * devclasses looking for children. We set the
609 * DC_HAS_CHILDREN flag when a child devclass is created on
610 * the parent, so we only walk the list for those devclasses
611 * that have children.
613 if (!(dc->flags & DC_HAS_CHILDREN))
616 TAILQ_FOREACH(dc, &devclasses, link) {
617 if (dc->parent == parent)
618 devclass_driver_added(dc, driver);
623 device_handle_nomatch(device_t dev)
625 BUS_PROBE_NOMATCH(dev->parent, dev);
626 EVENTHANDLER_DIRECT_INVOKE(device_nomatch, dev);
627 dev->flags |= DF_DONENOMATCH;
631 * @brief Add a device driver to a device class
633 * Add a device driver to a devclass. This is normally called
634 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
635 * all devices in the devclass will be called to allow them to attempt
636 * to re-probe any unmatched children.
638 * @param dc the devclass to edit
639 * @param driver the driver to register
642 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
646 const char *parentname;
648 PDEBUG(("%s", DRIVERNAME(driver)));
650 /* Don't allow invalid pass values. */
651 if (pass <= BUS_PASS_ROOT)
654 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
659 * Compile the driver's methods. Also increase the reference count
660 * so that the class doesn't get freed when the last instance
661 * goes. This means we can safely use static methods and avoids a
662 * double-free in devclass_delete_driver.
664 kobj_class_compile((kobj_class_t) driver);
667 * If the driver has any base classes, make the
668 * devclass inherit from the devclass of the driver's
669 * first base class. This will allow the system to
670 * search for drivers in both devclasses for children
671 * of a device using this driver.
673 if (driver->baseclasses)
674 parentname = driver->baseclasses[0]->name;
677 child_dc = devclass_find_internal(driver->name, parentname, TRUE);
682 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
683 driver->refs++; /* XXX: kobj_mtx */
685 driver_register_pass(dl);
688 dl->flags |= DL_DEFERRED_PROBE;
690 devclass_driver_added(dc, driver);
692 bus_data_generation_update();
697 * @brief Register that a device driver has been deleted from a devclass
699 * Register that a device driver has been removed from a devclass.
700 * This is called by devclass_delete_driver to accomplish the
701 * recursive notification of all the children classes of busclass, as
702 * well as busclass. Each layer will attempt to detach the driver
703 * from any devices that are children of the bus's devclass. The function
704 * will return an error if a device fails to detach.
706 * We do a full search here of the devclass list at each iteration
707 * level to save storing children-lists in the devclass structure. If
708 * we ever move beyond a few dozen devices doing this, we may need to
711 * @param busclass the devclass of the parent bus
712 * @param dc the devclass of the driver being deleted
713 * @param driver the driver being deleted
716 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
723 * Disassociate from any devices. We iterate through all the
724 * devices in the devclass of the driver and detach any which are
725 * using the driver and which have a parent in the devclass which
726 * we are deleting from.
728 * Note that since a driver can be in multiple devclasses, we
729 * should not detach devices which are not children of devices in
730 * the affected devclass.
732 * If we're frozen, we don't generate NOMATCH events. Mark to
735 for (i = 0; i < dc->maxunit; i++) {
736 if (dc->devices[i]) {
737 dev = dc->devices[i];
738 if (dev->driver == driver && dev->parent &&
739 dev->parent->devclass == busclass) {
740 if ((error = device_detach(dev)) != 0)
743 dev->flags &= ~DF_DONENOMATCH;
744 dev->flags |= DF_NEEDNOMATCH;
746 device_handle_nomatch(dev);
753 * Walk through the children classes. Since we only keep a
754 * single parent pointer around, we walk the entire list of
755 * devclasses looking for children. We set the
756 * DC_HAS_CHILDREN flag when a child devclass is created on
757 * the parent, so we only walk the list for those devclasses
758 * that have children.
760 if (!(busclass->flags & DC_HAS_CHILDREN))
763 TAILQ_FOREACH(busclass, &devclasses, link) {
764 if (busclass->parent == parent) {
765 error = devclass_driver_deleted(busclass, dc, driver);
774 * @brief Delete a device driver from a device class
776 * Delete a device driver from a devclass. This is normally called
777 * automatically by DRIVER_MODULE().
779 * If the driver is currently attached to any devices,
780 * devclass_delete_driver() will first attempt to detach from each
781 * device. If one of the detach calls fails, the driver will not be
784 * @param dc the devclass to edit
785 * @param driver the driver to unregister
788 devclass_delete_driver(devclass_t busclass, driver_t *driver)
790 devclass_t dc = devclass_find(driver->name);
794 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
800 * Find the link structure in the bus' list of drivers.
802 TAILQ_FOREACH(dl, &busclass->drivers, link) {
803 if (dl->driver == driver)
808 PDEBUG(("%s not found in %s list", driver->name,
813 error = devclass_driver_deleted(busclass, dc, driver);
817 TAILQ_REMOVE(&busclass->drivers, dl, link);
822 if (driver->refs == 0)
823 kobj_class_free((kobj_class_t) driver);
825 bus_data_generation_update();
830 * @brief Quiesces a set of device drivers from a device class
832 * Quiesce a device driver from a devclass. This is normally called
833 * automatically by DRIVER_MODULE().
835 * If the driver is currently attached to any devices,
836 * devclass_quiesece_driver() will first attempt to quiesce each
839 * @param dc the devclass to edit
840 * @param driver the driver to unregister
843 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
845 devclass_t dc = devclass_find(driver->name);
851 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
857 * Find the link structure in the bus' list of drivers.
859 TAILQ_FOREACH(dl, &busclass->drivers, link) {
860 if (dl->driver == driver)
865 PDEBUG(("%s not found in %s list", driver->name,
871 * Quiesce all devices. We iterate through all the devices in
872 * the devclass of the driver and quiesce any which are using
873 * the driver and which have a parent in the devclass which we
876 * Note that since a driver can be in multiple devclasses, we
877 * should not quiesce devices which are not children of
878 * devices in the affected devclass.
880 for (i = 0; i < dc->maxunit; i++) {
881 if (dc->devices[i]) {
882 dev = dc->devices[i];
883 if (dev->driver == driver && dev->parent &&
884 dev->parent->devclass == busclass) {
885 if ((error = device_quiesce(dev)) != 0)
898 devclass_find_driver_internal(devclass_t dc, const char *classname)
902 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
904 TAILQ_FOREACH(dl, &dc->drivers, link) {
905 if (!strcmp(dl->driver->name, classname))
909 PDEBUG(("not found"));
914 * @brief Return the name of the devclass
917 devclass_get_name(devclass_t dc)
923 * @brief Find a device given a unit number
925 * @param dc the devclass to search
926 * @param unit the unit number to search for
928 * @returns the device with the given unit number or @c
929 * NULL if there is no such device
932 devclass_get_device(devclass_t dc, int unit)
934 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
936 return (dc->devices[unit]);
940 * @brief Find the softc field of a device given a unit number
942 * @param dc the devclass to search
943 * @param unit the unit number to search for
945 * @returns the softc field of the device with the given
946 * unit number or @c NULL if there is no such
950 devclass_get_softc(devclass_t dc, int unit)
954 dev = devclass_get_device(dc, unit);
958 return (device_get_softc(dev));
962 * @brief Get a list of devices in the devclass
964 * An array containing a list of all the devices in the given devclass
965 * is allocated and returned in @p *devlistp. The number of devices
966 * in the array is returned in @p *devcountp. The caller should free
967 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
969 * @param dc the devclass to examine
970 * @param devlistp points at location for array pointer return
972 * @param devcountp points at location for array size return value
975 * @retval ENOMEM the array allocation failed
978 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
983 count = devclass_get_count(dc);
984 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
989 for (i = 0; i < dc->maxunit; i++) {
990 if (dc->devices[i]) {
991 list[count] = dc->devices[i];
1003 * @brief Get a list of drivers in the devclass
1005 * An array containing a list of pointers to all the drivers in the
1006 * given devclass is allocated and returned in @p *listp. The number
1007 * of drivers in the array is returned in @p *countp. The caller should
1008 * free the array using @c free(p, M_TEMP).
1010 * @param dc the devclass to examine
1011 * @param listp gives location for array pointer return value
1012 * @param countp gives location for number of array elements
1016 * @retval ENOMEM the array allocation failed
1019 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1026 TAILQ_FOREACH(dl, &dc->drivers, link)
1028 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1033 TAILQ_FOREACH(dl, &dc->drivers, link) {
1034 list[count] = dl->driver;
1044 * @brief Get the number of devices in a devclass
1046 * @param dc the devclass to examine
1049 devclass_get_count(devclass_t dc)
1054 for (i = 0; i < dc->maxunit; i++)
1061 * @brief Get the maximum unit number used in a devclass
1063 * Note that this is one greater than the highest currently-allocated
1064 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1065 * that not even the devclass has been allocated yet.
1067 * @param dc the devclass to examine
1070 devclass_get_maxunit(devclass_t dc)
1074 return (dc->maxunit);
1078 * @brief Find a free unit number in a devclass
1080 * This function searches for the first unused unit number greater
1081 * that or equal to @p unit.
1083 * @param dc the devclass to examine
1084 * @param unit the first unit number to check
1087 devclass_find_free_unit(devclass_t dc, int unit)
1091 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1097 * @brief Set the parent of a devclass
1099 * The parent class is normally initialised automatically by
1102 * @param dc the devclass to edit
1103 * @param pdc the new parent devclass
1106 devclass_set_parent(devclass_t dc, devclass_t pdc)
1112 * @brief Get the parent of a devclass
1114 * @param dc the devclass to examine
1117 devclass_get_parent(devclass_t dc)
1119 return (dc->parent);
1122 struct sysctl_ctx_list *
1123 devclass_get_sysctl_ctx(devclass_t dc)
1125 return (&dc->sysctl_ctx);
1129 devclass_get_sysctl_tree(devclass_t dc)
1131 return (dc->sysctl_tree);
1136 * @brief Allocate a unit number
1138 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1139 * will do). The allocated unit number is returned in @p *unitp.
1141 * @param dc the devclass to allocate from
1142 * @param unitp points at the location for the allocated unit
1146 * @retval EEXIST the requested unit number is already allocated
1147 * @retval ENOMEM memory allocation failure
1150 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1155 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1157 /* Ask the parent bus if it wants to wire this device. */
1159 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1162 /* If we were given a wired unit number, check for existing device */
1165 if (unit >= 0 && unit < dc->maxunit &&
1166 dc->devices[unit] != NULL) {
1168 printf("%s: %s%d already exists; skipping it\n",
1169 dc->name, dc->name, *unitp);
1173 /* Unwired device, find the next available slot for it */
1175 for (unit = 0;; unit++) {
1176 /* If this device slot is already in use, skip it. */
1177 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1180 /* If there is an "at" hint for a unit then skip it. */
1181 if (resource_string_value(dc->name, unit, "at", &s) ==
1190 * We've selected a unit beyond the length of the table, so let's
1191 * extend the table to make room for all units up to and including
1194 if (unit >= dc->maxunit) {
1195 device_t *newlist, *oldlist;
1198 oldlist = dc->devices;
1199 newsize = roundup((unit + 1),
1200 MAX(1, MINALLOCSIZE / sizeof(device_t)));
1201 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1204 if (oldlist != NULL)
1205 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1206 bzero(newlist + dc->maxunit,
1207 sizeof(device_t) * (newsize - dc->maxunit));
1208 dc->devices = newlist;
1209 dc->maxunit = newsize;
1210 if (oldlist != NULL)
1211 free(oldlist, M_BUS);
1213 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1221 * @brief Add a device to a devclass
1223 * A unit number is allocated for the device (using the device's
1224 * preferred unit number if any) and the device is registered in the
1225 * devclass. This allows the device to be looked up by its unit
1226 * number, e.g. by decoding a dev_t minor number.
1228 * @param dc the devclass to add to
1229 * @param dev the device to add
1232 * @retval EEXIST the requested unit number is already allocated
1233 * @retval ENOMEM memory allocation failure
1236 devclass_add_device(devclass_t dc, device_t dev)
1240 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1242 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1245 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1249 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1250 free(dev->nameunit, M_BUS);
1251 dev->nameunit = NULL;
1254 dc->devices[dev->unit] = dev;
1256 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1263 * @brief Delete a device from a devclass
1265 * The device is removed from the devclass's device list and its unit
1268 * @param dc the devclass to delete from
1269 * @param dev the device to delete
1274 devclass_delete_device(devclass_t dc, device_t dev)
1279 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1281 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1282 panic("devclass_delete_device: inconsistent device class");
1283 dc->devices[dev->unit] = NULL;
1284 if (dev->flags & DF_WILDCARD)
1286 dev->devclass = NULL;
1287 free(dev->nameunit, M_BUS);
1288 dev->nameunit = NULL;
1295 * @brief Make a new device and add it as a child of @p parent
1297 * @param parent the parent of the new device
1298 * @param name the devclass name of the new device or @c NULL
1299 * to leave the devclass unspecified
1300 * @parem unit the unit number of the new device of @c -1 to
1301 * leave the unit number unspecified
1303 * @returns the new device
1306 make_device(device_t parent, const char *name, int unit)
1311 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1314 dc = devclass_find_internal(name, NULL, TRUE);
1316 printf("make_device: can't find device class %s\n",
1324 dev = malloc(sizeof(*dev), M_BUS, M_NOWAIT|M_ZERO);
1328 dev->parent = parent;
1329 TAILQ_INIT(&dev->children);
1330 kobj_init((kobj_t) dev, &null_class);
1332 dev->devclass = NULL;
1334 dev->nameunit = NULL;
1338 dev->flags = DF_ENABLED;
1341 dev->flags |= DF_WILDCARD;
1343 dev->flags |= DF_FIXEDCLASS;
1344 if (devclass_add_device(dc, dev)) {
1345 kobj_delete((kobj_t) dev, M_BUS);
1349 if (parent != NULL && device_has_quiet_children(parent))
1350 dev->flags |= DF_QUIET | DF_QUIET_CHILDREN;
1354 dev->state = DS_NOTPRESENT;
1356 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1357 bus_data_generation_update();
1364 * @brief Print a description of a device.
1367 device_print_child(device_t dev, device_t child)
1371 if (device_is_alive(child))
1372 retval += BUS_PRINT_CHILD(dev, child);
1374 retval += device_printf(child, " not found\n");
1380 * @brief Create a new device
1382 * This creates a new device and adds it as a child of an existing
1383 * parent device. The new device will be added after the last existing
1384 * child with order zero.
1386 * @param dev the device which will be the parent of the
1388 * @param name devclass name for new device or @c NULL if not
1390 * @param unit unit number for new device or @c -1 if not
1393 * @returns the new device
1396 device_add_child(device_t dev, const char *name, int unit)
1398 return (device_add_child_ordered(dev, 0, name, unit));
1402 * @brief Create a new device
1404 * This creates a new device and adds it as a child of an existing
1405 * parent device. The new device will be added after the last existing
1406 * child with the same order.
1408 * @param dev the device which will be the parent of the
1410 * @param order a value which is used to partially sort the
1411 * children of @p dev - devices created using
1412 * lower values of @p order appear first in @p
1413 * dev's list of children
1414 * @param name devclass name for new device or @c NULL if not
1416 * @param unit unit number for new device or @c -1 if not
1419 * @returns the new device
1422 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1427 PDEBUG(("%s at %s with order %u as unit %d",
1428 name, DEVICENAME(dev), order, unit));
1429 KASSERT(name != NULL || unit == -1,
1430 ("child device with wildcard name and specific unit number"));
1432 child = make_device(dev, name, unit);
1435 child->order = order;
1437 TAILQ_FOREACH(place, &dev->children, link) {
1438 if (place->order > order)
1444 * The device 'place' is the first device whose order is
1445 * greater than the new child.
1447 TAILQ_INSERT_BEFORE(place, child, link);
1450 * The new child's order is greater or equal to the order of
1451 * any existing device. Add the child to the tail of the list.
1453 TAILQ_INSERT_TAIL(&dev->children, child, link);
1456 bus_data_generation_update();
1461 * @brief Delete a device
1463 * This function deletes a device along with all of its children. If
1464 * the device currently has a driver attached to it, the device is
1465 * detached first using device_detach().
1467 * @param dev the parent device
1468 * @param child the device to delete
1471 * @retval non-zero a unit error code describing the error
1474 device_delete_child(device_t dev, device_t child)
1477 device_t grandchild;
1479 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1481 /* detach parent before deleting children, if any */
1482 if ((error = device_detach(child)) != 0)
1485 /* remove children second */
1486 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1487 error = device_delete_child(child, grandchild);
1492 if (child->devclass)
1493 devclass_delete_device(child->devclass, child);
1495 BUS_CHILD_DELETED(dev, child);
1496 TAILQ_REMOVE(&dev->children, child, link);
1497 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1498 kobj_delete((kobj_t) child, M_BUS);
1500 bus_data_generation_update();
1505 * @brief Delete all children devices of the given device, if any.
1507 * This function deletes all children devices of the given device, if
1508 * any, using the device_delete_child() function for each device it
1509 * finds. If a child device cannot be deleted, this function will
1510 * return an error code.
1512 * @param dev the parent device
1515 * @retval non-zero a device would not detach
1518 device_delete_children(device_t dev)
1523 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1527 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1528 error = device_delete_child(dev, child);
1530 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1538 * @brief Find a device given a unit number
1540 * This is similar to devclass_get_devices() but only searches for
1541 * devices which have @p dev as a parent.
1543 * @param dev the parent device to search
1544 * @param unit the unit number to search for. If the unit is -1,
1545 * return the first child of @p dev which has name
1546 * @p classname (that is, the one with the lowest unit.)
1548 * @returns the device with the given unit number or @c
1549 * NULL if there is no such device
1552 device_find_child(device_t dev, const char *classname, int unit)
1557 dc = devclass_find(classname);
1562 child = devclass_get_device(dc, unit);
1563 if (child && child->parent == dev)
1566 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1567 child = devclass_get_device(dc, unit);
1568 if (child && child->parent == dev)
1579 first_matching_driver(devclass_t dc, device_t dev)
1582 return (devclass_find_driver_internal(dc, dev->devclass->name));
1583 return (TAILQ_FIRST(&dc->drivers));
1590 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1592 if (dev->devclass) {
1594 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1595 if (!strcmp(dev->devclass->name, dl->driver->name))
1599 return (TAILQ_NEXT(last, link));
1606 device_probe_child(device_t dev, device_t child)
1609 driverlink_t best = NULL;
1611 int result, pri = 0;
1612 /* We should preserve the devclass (or lack of) set by the bus. */
1613 int hasclass = (child->devclass != NULL);
1619 panic("device_probe_child: parent device has no devclass");
1622 * If the state is already probed, then return.
1624 if (child->state == DS_ALIVE)
1627 for (; dc; dc = dc->parent) {
1628 for (dl = first_matching_driver(dc, child);
1630 dl = next_matching_driver(dc, child, dl)) {
1631 /* If this driver's pass is too high, then ignore it. */
1632 if (dl->pass > bus_current_pass)
1635 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1636 result = device_set_driver(child, dl->driver);
1637 if (result == ENOMEM)
1639 else if (result != 0)
1642 if (device_set_devclass(child,
1643 dl->driver->name) != 0) {
1644 char const * devname =
1645 device_get_name(child);
1646 if (devname == NULL)
1647 devname = "(unknown)";
1648 printf("driver bug: Unable to set "
1649 "devclass (class: %s "
1653 (void)device_set_driver(child, NULL);
1658 /* Fetch any flags for the device before probing. */
1659 resource_int_value(dl->driver->name, child->unit,
1660 "flags", &child->devflags);
1662 result = DEVICE_PROBE(child);
1665 * If the driver returns SUCCESS, there can be
1666 * no higher match for this device.
1674 /* Reset flags and devclass before the next probe. */
1675 child->devflags = 0;
1677 (void)device_set_devclass(child, NULL);
1680 * Reset DF_QUIET in case this driver doesn't
1681 * end up as the best driver.
1683 device_verbose(child);
1686 * Probes that return BUS_PROBE_NOWILDCARD or lower
1687 * only match on devices whose driver was explicitly
1690 if (result <= BUS_PROBE_NOWILDCARD &&
1691 !(child->flags & DF_FIXEDCLASS)) {
1696 * The driver returned an error so it
1697 * certainly doesn't match.
1700 (void)device_set_driver(child, NULL);
1705 * A priority lower than SUCCESS, remember the
1706 * best matching driver. Initialise the value
1707 * of pri for the first match.
1709 if (best == NULL || result > pri) {
1716 * If we have an unambiguous match in this devclass,
1717 * don't look in the parent.
1719 if (best && pri == 0)
1727 * If we found a driver, change state and initialise the devclass.
1730 /* Set the winning driver, devclass, and flags. */
1731 result = device_set_driver(child, best->driver);
1734 if (!child->devclass) {
1735 result = device_set_devclass(child, best->driver->name);
1737 (void)device_set_driver(child, NULL);
1741 resource_int_value(best->driver->name, child->unit,
1742 "flags", &child->devflags);
1745 * A bit bogus. Call the probe method again to make sure
1746 * that we have the right description.
1748 result = DEVICE_PROBE(child);
1751 (void)device_set_devclass(child, NULL);
1752 (void)device_set_driver(child, NULL);
1757 child->state = DS_ALIVE;
1758 bus_data_generation_update();
1763 * @brief Return the parent of a device
1766 device_get_parent(device_t dev)
1768 return (dev->parent);
1772 * @brief Get a list of children of a device
1774 * An array containing a list of all the children of the given device
1775 * is allocated and returned in @p *devlistp. The number of devices
1776 * in the array is returned in @p *devcountp. The caller should free
1777 * the array using @c free(p, M_TEMP).
1779 * @param dev the device to examine
1780 * @param devlistp points at location for array pointer return
1782 * @param devcountp points at location for array size return value
1785 * @retval ENOMEM the array allocation failed
1788 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1795 TAILQ_FOREACH(child, &dev->children, link) {
1804 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1809 TAILQ_FOREACH(child, &dev->children, link) {
1810 list[count] = child;
1821 * @brief Return the current driver for the device or @c NULL if there
1822 * is no driver currently attached
1825 device_get_driver(device_t dev)
1827 return (dev->driver);
1831 * @brief Return the current devclass for the device or @c NULL if
1835 device_get_devclass(device_t dev)
1837 return (dev->devclass);
1841 * @brief Return the name of the device's devclass or @c NULL if there
1845 device_get_name(device_t dev)
1847 if (dev != NULL && dev->devclass)
1848 return (devclass_get_name(dev->devclass));
1853 * @brief Return a string containing the device's devclass name
1854 * followed by an ascii representation of the device's unit number
1858 device_get_nameunit(device_t dev)
1860 return (dev->nameunit);
1864 * @brief Return the device's unit number.
1867 device_get_unit(device_t dev)
1873 * @brief Return the device's description string
1876 device_get_desc(device_t dev)
1882 * @brief Return the device's flags
1885 device_get_flags(device_t dev)
1887 return (dev->devflags);
1890 struct sysctl_ctx_list *
1891 device_get_sysctl_ctx(device_t dev)
1893 return (&dev->sysctl_ctx);
1897 device_get_sysctl_tree(device_t dev)
1899 return (dev->sysctl_tree);
1903 * @brief Print the name of the device followed by a colon and a space
1905 * @returns the number of characters printed
1908 device_print_prettyname(device_t dev)
1910 const char *name = device_get_name(dev);
1913 return (printf("unknown: "));
1914 return (printf("%s%d: ", name, device_get_unit(dev)));
1918 * @brief Print the name of the device followed by a colon, a space
1919 * and the result of calling vprintf() with the value of @p fmt and
1920 * the following arguments.
1922 * @returns the number of characters printed
1925 device_printf(device_t dev, const char * fmt, ...)
1935 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
1936 sbuf_set_drain(&sb, sbuf_printf_drain, &retval);
1938 name = device_get_name(dev);
1941 sbuf_cat(&sb, "unknown: ");
1943 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
1946 sbuf_vprintf(&sb, fmt, ap);
1956 * @brief Print the name of the device followed by a colon, a space
1957 * and the result of calling log() with the value of @p fmt and
1958 * the following arguments.
1960 * @returns the number of characters printed
1963 device_log(device_t dev, int pri, const char * fmt, ...)
1973 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
1975 name = device_get_name(dev);
1978 sbuf_cat(&sb, "unknown: ");
1980 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
1983 sbuf_vprintf(&sb, fmt, ap);
1988 log(pri, "%.*s", (int) sbuf_len(&sb), sbuf_data(&sb));
1989 retval = sbuf_len(&sb);
2000 device_set_desc_internal(device_t dev, const char* desc, int copy)
2002 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2003 free(dev->desc, M_BUS);
2004 dev->flags &= ~DF_DESCMALLOCED;
2009 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2011 strcpy(dev->desc, desc);
2012 dev->flags |= DF_DESCMALLOCED;
2015 /* Avoid a -Wcast-qual warning */
2016 dev->desc = (char *)(uintptr_t) desc;
2019 bus_data_generation_update();
2023 * @brief Set the device's description
2025 * The value of @c desc should be a string constant that will not
2026 * change (at least until the description is changed in a subsequent
2027 * call to device_set_desc() or device_set_desc_copy()).
2030 device_set_desc(device_t dev, const char* desc)
2032 device_set_desc_internal(dev, desc, FALSE);
2036 * @brief Set the device's description
2038 * The string pointed to by @c desc is copied. Use this function if
2039 * the device description is generated, (e.g. with sprintf()).
2042 device_set_desc_copy(device_t dev, const char* desc)
2044 device_set_desc_internal(dev, desc, TRUE);
2048 * @brief Set the device's flags
2051 device_set_flags(device_t dev, uint32_t flags)
2053 dev->devflags = flags;
2057 * @brief Return the device's softc field
2059 * The softc is allocated and zeroed when a driver is attached, based
2060 * on the size field of the driver.
2063 device_get_softc(device_t dev)
2065 return (dev->softc);
2069 * @brief Set the device's softc field
2071 * Most drivers do not need to use this since the softc is allocated
2072 * automatically when the driver is attached.
2075 device_set_softc(device_t dev, void *softc)
2077 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2078 free(dev->softc, M_BUS_SC);
2081 dev->flags |= DF_EXTERNALSOFTC;
2083 dev->flags &= ~DF_EXTERNALSOFTC;
2087 * @brief Free claimed softc
2089 * Most drivers do not need to use this since the softc is freed
2090 * automatically when the driver is detached.
2093 device_free_softc(void *softc)
2095 free(softc, M_BUS_SC);
2099 * @brief Claim softc
2101 * This function can be used to let the driver free the automatically
2102 * allocated softc using "device_free_softc()". This function is
2103 * useful when the driver is refcounting the softc and the softc
2104 * cannot be freed when the "device_detach" method is called.
2107 device_claim_softc(device_t dev)
2110 dev->flags |= DF_EXTERNALSOFTC;
2112 dev->flags &= ~DF_EXTERNALSOFTC;
2116 * @brief Get the device's ivars field
2118 * The ivars field is used by the parent device to store per-device
2119 * state (e.g. the physical location of the device or a list of
2123 device_get_ivars(device_t dev)
2125 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2126 return (dev->ivars);
2130 * @brief Set the device's ivars field
2133 device_set_ivars(device_t dev, void * ivars)
2135 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2140 * @brief Return the device's state
2143 device_get_state(device_t dev)
2145 return (dev->state);
2149 * @brief Set the DF_ENABLED flag for the device
2152 device_enable(device_t dev)
2154 dev->flags |= DF_ENABLED;
2158 * @brief Clear the DF_ENABLED flag for the device
2161 device_disable(device_t dev)
2163 dev->flags &= ~DF_ENABLED;
2167 * @brief Increment the busy counter for the device
2170 device_busy(device_t dev)
2174 * Mark the device as busy, recursively up the tree if this busy count
2177 if (refcount_acquire(&dev->busy) == 0 && dev->parent != NULL)
2178 device_busy(dev->parent);
2182 * @brief Decrement the busy counter for the device
2185 device_unbusy(device_t dev)
2189 * Mark the device as unbsy, recursively if this is the last busy count.
2191 if (refcount_release(&dev->busy) && dev->parent != NULL)
2192 device_unbusy(dev->parent);
2196 * @brief Set the DF_QUIET flag for the device
2199 device_quiet(device_t dev)
2201 dev->flags |= DF_QUIET;
2205 * @brief Set the DF_QUIET_CHILDREN flag for the device
2208 device_quiet_children(device_t dev)
2210 dev->flags |= DF_QUIET_CHILDREN;
2214 * @brief Clear the DF_QUIET flag for the device
2217 device_verbose(device_t dev)
2219 dev->flags &= ~DF_QUIET;
2223 device_get_property(device_t dev, const char *prop, void *val, size_t sz,
2224 device_property_type_t type)
2226 device_t bus = device_get_parent(dev);
2229 case DEVICE_PROP_ANY:
2230 case DEVICE_PROP_BUFFER:
2231 case DEVICE_PROP_HANDLE: /* Size checks done in implementation. */
2233 case DEVICE_PROP_UINT32:
2237 case DEVICE_PROP_UINT64:
2245 return (BUS_GET_PROPERTY(bus, dev, prop, val, sz, type));
2249 device_has_property(device_t dev, const char *prop)
2251 return (device_get_property(dev, prop, NULL, 0, DEVICE_PROP_ANY) >= 0);
2255 * @brief Return non-zero if the DF_QUIET_CHIDLREN flag is set on the device
2258 device_has_quiet_children(device_t dev)
2260 return ((dev->flags & DF_QUIET_CHILDREN) != 0);
2264 * @brief Return non-zero if the DF_QUIET flag is set on the device
2267 device_is_quiet(device_t dev)
2269 return ((dev->flags & DF_QUIET) != 0);
2273 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2276 device_is_enabled(device_t dev)
2278 return ((dev->flags & DF_ENABLED) != 0);
2282 * @brief Return non-zero if the device was successfully probed
2285 device_is_alive(device_t dev)
2287 return (dev->state >= DS_ALIVE);
2291 * @brief Return non-zero if the device currently has a driver
2295 device_is_attached(device_t dev)
2297 return (dev->state >= DS_ATTACHED);
2301 * @brief Return non-zero if the device is currently suspended.
2304 device_is_suspended(device_t dev)
2306 return ((dev->flags & DF_SUSPENDED) != 0);
2310 * @brief Set the devclass of a device
2311 * @see devclass_add_device().
2314 device_set_devclass(device_t dev, const char *classname)
2321 devclass_delete_device(dev->devclass, dev);
2325 if (dev->devclass) {
2326 printf("device_set_devclass: device class already set\n");
2330 dc = devclass_find_internal(classname, NULL, TRUE);
2334 error = devclass_add_device(dc, dev);
2336 bus_data_generation_update();
2341 * @brief Set the devclass of a device and mark the devclass fixed.
2342 * @see device_set_devclass()
2345 device_set_devclass_fixed(device_t dev, const char *classname)
2349 if (classname == NULL)
2352 error = device_set_devclass(dev, classname);
2355 dev->flags |= DF_FIXEDCLASS;
2360 * @brief Query the device to determine if it's of a fixed devclass
2361 * @see device_set_devclass_fixed()
2364 device_is_devclass_fixed(device_t dev)
2366 return ((dev->flags & DF_FIXEDCLASS) != 0);
2370 * @brief Set the driver of a device
2373 * @retval EBUSY the device already has a driver attached
2374 * @retval ENOMEM a memory allocation failure occurred
2377 device_set_driver(device_t dev, driver_t *driver)
2380 struct domainset *policy;
2382 if (dev->state >= DS_ATTACHED)
2385 if (dev->driver == driver)
2388 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2389 free(dev->softc, M_BUS_SC);
2392 device_set_desc(dev, NULL);
2393 kobj_delete((kobj_t) dev, NULL);
2394 dev->driver = driver;
2396 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2397 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2398 if (bus_get_domain(dev, &domain) == 0)
2399 policy = DOMAINSET_PREF(domain);
2401 policy = DOMAINSET_RR();
2402 dev->softc = malloc_domainset(driver->size, M_BUS_SC,
2403 policy, M_NOWAIT | M_ZERO);
2405 kobj_delete((kobj_t) dev, NULL);
2406 kobj_init((kobj_t) dev, &null_class);
2412 kobj_init((kobj_t) dev, &null_class);
2415 bus_data_generation_update();
2420 * @brief Probe a device, and return this status.
2422 * This function is the core of the device autoconfiguration
2423 * system. Its purpose is to select a suitable driver for a device and
2424 * then call that driver to initialise the hardware appropriately. The
2425 * driver is selected by calling the DEVICE_PROBE() method of a set of
2426 * candidate drivers and then choosing the driver which returned the
2427 * best value. This driver is then attached to the device using
2430 * The set of suitable drivers is taken from the list of drivers in
2431 * the parent device's devclass. If the device was originally created
2432 * with a specific class name (see device_add_child()), only drivers
2433 * with that name are probed, otherwise all drivers in the devclass
2434 * are probed. If no drivers return successful probe values in the
2435 * parent devclass, the search continues in the parent of that
2436 * devclass (see devclass_get_parent()) if any.
2438 * @param dev the device to initialise
2441 * @retval ENXIO no driver was found
2442 * @retval ENOMEM memory allocation failure
2443 * @retval non-zero some other unix error code
2444 * @retval -1 Device already attached
2447 device_probe(device_t dev)
2453 if (dev->state >= DS_ALIVE)
2456 if (!(dev->flags & DF_ENABLED)) {
2457 if (bootverbose && device_get_name(dev) != NULL) {
2458 device_print_prettyname(dev);
2459 printf("not probed (disabled)\n");
2463 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2464 if (bus_current_pass == BUS_PASS_DEFAULT &&
2465 !(dev->flags & DF_DONENOMATCH)) {
2466 device_handle_nomatch(dev);
2474 * @brief Probe a device and attach a driver if possible
2476 * calls device_probe() and attaches if that was successful.
2479 device_probe_and_attach(device_t dev)
2485 error = device_probe(dev);
2488 else if (error != 0)
2491 CURVNET_SET_QUIET(vnet0);
2492 error = device_attach(dev);
2498 * @brief Attach a device driver to a device
2500 * This function is a wrapper around the DEVICE_ATTACH() driver
2501 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2502 * device's sysctl tree, optionally prints a description of the device
2503 * and queues a notification event for user-based device management
2506 * Normally this function is only called internally from
2507 * device_probe_and_attach().
2509 * @param dev the device to initialise
2512 * @retval ENXIO no driver was found
2513 * @retval ENOMEM memory allocation failure
2514 * @retval non-zero some other unix error code
2517 device_attach(device_t dev)
2519 uint64_t attachtime;
2520 uint16_t attachentropy;
2523 if (resource_disabled(dev->driver->name, dev->unit)) {
2524 device_disable(dev);
2526 device_printf(dev, "disabled via hints entry\n");
2530 device_sysctl_init(dev);
2531 if (!device_is_quiet(dev))
2532 device_print_child(dev->parent, dev);
2533 attachtime = get_cyclecount();
2534 dev->state = DS_ATTACHING;
2535 if ((error = DEVICE_ATTACH(dev)) != 0) {
2536 printf("device_attach: %s%d attach returned %d\n",
2537 dev->driver->name, dev->unit, error);
2538 if (disable_failed_devs) {
2540 * When the user has asked to disable failed devices, we
2541 * directly disable the device, but leave it in the
2542 * attaching state. It will not try to probe/attach the
2543 * device further. This leaves the device numbering
2544 * intact for other similar devices in the system. It
2545 * can be removed from this state with devctl.
2547 device_disable(dev);
2550 * Otherwise, when attach fails, tear down the state
2551 * around that so we can retry when, for example, new
2552 * drivers are loaded.
2554 if (!(dev->flags & DF_FIXEDCLASS))
2555 devclass_delete_device(dev->devclass, dev);
2556 (void)device_set_driver(dev, NULL);
2557 device_sysctl_fini(dev);
2558 KASSERT(dev->busy == 0, ("attach failed but busy"));
2559 dev->state = DS_NOTPRESENT;
2563 dev->flags |= DF_ATTACHED_ONCE;
2565 * We only need the low bits of this time, but ranges from tens to thousands
2566 * have been seen, so keep 2 bytes' worth.
2568 attachentropy = (uint16_t)(get_cyclecount() - attachtime);
2569 random_harvest_direct(&attachentropy, sizeof(attachentropy), RANDOM_ATTACH);
2570 device_sysctl_update(dev);
2571 dev->state = DS_ATTACHED;
2572 dev->flags &= ~DF_DONENOMATCH;
2573 EVENTHANDLER_DIRECT_INVOKE(device_attach, dev);
2578 * @brief Detach a driver from a device
2580 * This function is a wrapper around the DEVICE_DETACH() driver
2581 * method. If the call to DEVICE_DETACH() succeeds, it calls
2582 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2583 * notification event for user-based device management services and
2584 * cleans up the device's sysctl tree.
2586 * @param dev the device to un-initialise
2589 * @retval ENXIO no driver was found
2590 * @retval ENOMEM memory allocation failure
2591 * @retval non-zero some other unix error code
2594 device_detach(device_t dev)
2600 PDEBUG(("%s", DEVICENAME(dev)));
2603 if (dev->state == DS_ATTACHING) {
2604 device_printf(dev, "device in attaching state! Deferring detach.\n");
2607 if (dev->state != DS_ATTACHED)
2610 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, EVHDEV_DETACH_BEGIN);
2611 if ((error = DEVICE_DETACH(dev)) != 0) {
2612 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
2613 EVHDEV_DETACH_FAILED);
2616 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
2617 EVHDEV_DETACH_COMPLETE);
2619 if (!device_is_quiet(dev))
2620 device_printf(dev, "detached\n");
2622 BUS_CHILD_DETACHED(dev->parent, dev);
2624 if (!(dev->flags & DF_FIXEDCLASS))
2625 devclass_delete_device(dev->devclass, dev);
2627 device_verbose(dev);
2628 dev->state = DS_NOTPRESENT;
2629 (void)device_set_driver(dev, NULL);
2630 device_sysctl_fini(dev);
2636 * @brief Tells a driver to quiesce itself.
2638 * This function is a wrapper around the DEVICE_QUIESCE() driver
2639 * method. If the call to DEVICE_QUIESCE() succeeds.
2641 * @param dev the device to quiesce
2644 * @retval ENXIO no driver was found
2645 * @retval ENOMEM memory allocation failure
2646 * @retval non-zero some other unix error code
2649 device_quiesce(device_t dev)
2651 PDEBUG(("%s", DEVICENAME(dev)));
2654 if (dev->state != DS_ATTACHED)
2657 return (DEVICE_QUIESCE(dev));
2661 * @brief Notify a device of system shutdown
2663 * This function calls the DEVICE_SHUTDOWN() driver method if the
2664 * device currently has an attached driver.
2666 * @returns the value returned by DEVICE_SHUTDOWN()
2669 device_shutdown(device_t dev)
2671 if (dev->state < DS_ATTACHED)
2673 return (DEVICE_SHUTDOWN(dev));
2677 * @brief Set the unit number of a device
2679 * This function can be used to override the unit number used for a
2680 * device (e.g. to wire a device to a pre-configured unit number).
2683 device_set_unit(device_t dev, int unit)
2688 if (unit == dev->unit)
2690 dc = device_get_devclass(dev);
2691 if (unit < dc->maxunit && dc->devices[unit])
2693 err = devclass_delete_device(dc, dev);
2697 err = devclass_add_device(dc, dev);
2701 bus_data_generation_update();
2705 /*======================================*/
2707 * Some useful method implementations to make life easier for bus drivers.
2711 * @brief Initialize a resource mapping request
2713 * This is the internal implementation of the public API
2714 * resource_init_map_request. Callers may be using a different layout
2715 * of struct resource_map_request than the kernel, so callers pass in
2716 * the size of the structure they are using to identify the structure
2720 resource_init_map_request_impl(struct resource_map_request *args, size_t sz)
2724 args->memattr = VM_MEMATTR_DEVICE;
2728 * @brief Validate a resource mapping request
2730 * Translate a device driver's mapping request (@p in) to a struct
2731 * resource_map_request using the current structure layout (@p out).
2732 * In addition, validate the offset and length from the mapping
2733 * request against the bounds of the resource @p r. If the offset or
2734 * length are invalid, fail with EINVAL. If the offset and length are
2735 * valid, the absolute starting address of the requested mapping is
2736 * returned in @p startp and the length of the requested mapping is
2737 * returned in @p lengthp.
2740 resource_validate_map_request(struct resource *r,
2741 struct resource_map_request *in, struct resource_map_request *out,
2742 rman_res_t *startp, rman_res_t *lengthp)
2744 rman_res_t end, length, start;
2747 * This assumes that any callers of this function are compiled
2748 * into the kernel and use the same version of the structure
2751 MPASS(out->size == sizeof(struct resource_map_request));
2754 bcopy(in, out, imin(in->size, out->size));
2755 start = rman_get_start(r) + out->offset;
2756 if (out->length == 0)
2757 length = rman_get_size(r);
2759 length = out->length;
2760 end = start + length - 1;
2761 if (start > rman_get_end(r) || start < rman_get_start(r))
2763 if (end > rman_get_end(r) || end < start)
2771 * @brief Initialise a resource list.
2773 * @param rl the resource list to initialise
2776 resource_list_init(struct resource_list *rl)
2782 * @brief Reclaim memory used by a resource list.
2784 * This function frees the memory for all resource entries on the list
2787 * @param rl the resource list to free
2790 resource_list_free(struct resource_list *rl)
2792 struct resource_list_entry *rle;
2794 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2796 panic("resource_list_free: resource entry is busy");
2797 STAILQ_REMOVE_HEAD(rl, link);
2803 * @brief Add a resource entry.
2805 * This function adds a resource entry using the given @p type, @p
2806 * start, @p end and @p count values. A rid value is chosen by
2807 * searching sequentially for the first unused rid starting at zero.
2809 * @param rl the resource list to edit
2810 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2811 * @param start the start address of the resource
2812 * @param end the end address of the resource
2813 * @param count XXX end-start+1
2816 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start,
2817 rman_res_t end, rman_res_t count)
2822 while (resource_list_find(rl, type, rid) != NULL)
2824 resource_list_add(rl, type, rid, start, end, count);
2829 * @brief Add or modify a resource entry.
2831 * If an existing entry exists with the same type and rid, it will be
2832 * modified using the given values of @p start, @p end and @p
2833 * count. If no entry exists, a new one will be created using the
2834 * given values. The resource list entry that matches is then returned.
2836 * @param rl the resource list to edit
2837 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2838 * @param rid the resource identifier
2839 * @param start the start address of the resource
2840 * @param end the end address of the resource
2841 * @param count XXX end-start+1
2843 struct resource_list_entry *
2844 resource_list_add(struct resource_list *rl, int type, int rid,
2845 rman_res_t start, rman_res_t end, rman_res_t count)
2847 struct resource_list_entry *rle;
2849 rle = resource_list_find(rl, type, rid);
2851 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2854 panic("resource_list_add: can't record entry");
2855 STAILQ_INSERT_TAIL(rl, rle, link);
2863 panic("resource_list_add: resource entry is busy");
2872 * @brief Determine if a resource entry is busy.
2874 * Returns true if a resource entry is busy meaning that it has an
2875 * associated resource that is not an unallocated "reserved" resource.
2877 * @param rl the resource list to search
2878 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2879 * @param rid the resource identifier
2881 * @returns Non-zero if the entry is busy, zero otherwise.
2884 resource_list_busy(struct resource_list *rl, int type, int rid)
2886 struct resource_list_entry *rle;
2888 rle = resource_list_find(rl, type, rid);
2889 if (rle == NULL || rle->res == NULL)
2891 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
2892 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
2893 ("reserved resource is active"));
2900 * @brief Determine if a resource entry is reserved.
2902 * Returns true if a resource entry is reserved meaning that it has an
2903 * associated "reserved" resource. The resource can either be
2904 * allocated or unallocated.
2906 * @param rl the resource list to search
2907 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2908 * @param rid the resource identifier
2910 * @returns Non-zero if the entry is reserved, zero otherwise.
2913 resource_list_reserved(struct resource_list *rl, int type, int rid)
2915 struct resource_list_entry *rle;
2917 rle = resource_list_find(rl, type, rid);
2918 if (rle != NULL && rle->flags & RLE_RESERVED)
2924 * @brief Find a resource entry by type and rid.
2926 * @param rl the resource list to search
2927 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2928 * @param rid the resource identifier
2930 * @returns the resource entry pointer or NULL if there is no such
2933 struct resource_list_entry *
2934 resource_list_find(struct resource_list *rl, int type, int rid)
2936 struct resource_list_entry *rle;
2938 STAILQ_FOREACH(rle, rl, link) {
2939 if (rle->type == type && rle->rid == rid)
2946 * @brief Delete a resource entry.
2948 * @param rl the resource list to edit
2949 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2950 * @param rid the resource identifier
2953 resource_list_delete(struct resource_list *rl, int type, int rid)
2955 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2958 if (rle->res != NULL)
2959 panic("resource_list_delete: resource has not been released");
2960 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
2966 * @brief Allocate a reserved resource
2968 * This can be used by buses to force the allocation of resources
2969 * that are always active in the system even if they are not allocated
2970 * by a driver (e.g. PCI BARs). This function is usually called when
2971 * adding a new child to the bus. The resource is allocated from the
2972 * parent bus when it is reserved. The resource list entry is marked
2973 * with RLE_RESERVED to note that it is a reserved resource.
2975 * Subsequent attempts to allocate the resource with
2976 * resource_list_alloc() will succeed the first time and will set
2977 * RLE_ALLOCATED to note that it has been allocated. When a reserved
2978 * resource that has been allocated is released with
2979 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
2980 * the actual resource remains allocated. The resource can be released to
2981 * the parent bus by calling resource_list_unreserve().
2983 * @param rl the resource list to allocate from
2984 * @param bus the parent device of @p child
2985 * @param child the device for which the resource is being reserved
2986 * @param type the type of resource to allocate
2987 * @param rid a pointer to the resource identifier
2988 * @param start hint at the start of the resource range - pass
2989 * @c 0 for any start address
2990 * @param end hint at the end of the resource range - pass
2991 * @c ~0 for any end address
2992 * @param count hint at the size of range required - pass @c 1
2994 * @param flags any extra flags to control the resource
2995 * allocation - see @c RF_XXX flags in
2996 * <sys/rman.h> for details
2998 * @returns the resource which was allocated or @c NULL if no
2999 * resource could be allocated
3002 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3003 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3005 struct resource_list_entry *rle = NULL;
3006 int passthrough = (device_get_parent(child) != bus);
3011 "resource_list_reserve() should only be called for direct children");
3012 if (flags & RF_ACTIVE)
3014 "resource_list_reserve() should only reserve inactive resources");
3016 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3019 rle = resource_list_find(rl, type, *rid);
3020 rle->flags |= RLE_RESERVED;
3026 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3028 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3029 * and passing the allocation up to the parent of @p bus. This assumes
3030 * that the first entry of @c device_get_ivars(child) is a struct
3031 * resource_list. This also handles 'passthrough' allocations where a
3032 * child is a remote descendant of bus by passing the allocation up to
3033 * the parent of bus.
3035 * Typically, a bus driver would store a list of child resources
3036 * somewhere in the child device's ivars (see device_get_ivars()) and
3037 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3038 * then call resource_list_alloc() to perform the allocation.
3040 * @param rl the resource list to allocate from
3041 * @param bus the parent device of @p child
3042 * @param child the device which is requesting an allocation
3043 * @param type the type of resource to allocate
3044 * @param rid a pointer to the resource identifier
3045 * @param start hint at the start of the resource range - pass
3046 * @c 0 for any start address
3047 * @param end hint at the end of the resource range - pass
3048 * @c ~0 for any end address
3049 * @param count hint at the size of range required - pass @c 1
3051 * @param flags any extra flags to control the resource
3052 * allocation - see @c RF_XXX flags in
3053 * <sys/rman.h> for details
3055 * @returns the resource which was allocated or @c NULL if no
3056 * resource could be allocated
3059 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3060 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3062 struct resource_list_entry *rle = NULL;
3063 int passthrough = (device_get_parent(child) != bus);
3064 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
3067 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3068 type, rid, start, end, count, flags));
3071 rle = resource_list_find(rl, type, *rid);
3074 return (NULL); /* no resource of that type/rid */
3077 if (rle->flags & RLE_RESERVED) {
3078 if (rle->flags & RLE_ALLOCATED)
3080 if ((flags & RF_ACTIVE) &&
3081 bus_activate_resource(child, type, *rid,
3084 rle->flags |= RLE_ALLOCATED;
3088 "resource entry %#x type %d for child %s is busy\n", *rid,
3089 type, device_get_nameunit(child));
3095 count = ulmax(count, rle->count);
3096 end = ulmax(rle->end, start + count - 1);
3099 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3100 type, rid, start, end, count, flags);
3103 * Record the new range.
3106 rle->start = rman_get_start(rle->res);
3107 rle->end = rman_get_end(rle->res);
3115 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3117 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3118 * used with resource_list_alloc().
3120 * @param rl the resource list which was allocated from
3121 * @param bus the parent device of @p child
3122 * @param child the device which is requesting a release
3123 * @param type the type of resource to release
3124 * @param rid the resource identifier
3125 * @param res the resource to release
3128 * @retval non-zero a standard unix error code indicating what
3129 * error condition prevented the operation
3132 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3133 int type, int rid, struct resource *res)
3135 struct resource_list_entry *rle = NULL;
3136 int passthrough = (device_get_parent(child) != bus);
3140 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3144 rle = resource_list_find(rl, type, rid);
3147 panic("resource_list_release: can't find resource");
3149 panic("resource_list_release: resource entry is not busy");
3150 if (rle->flags & RLE_RESERVED) {
3151 if (rle->flags & RLE_ALLOCATED) {
3152 if (rman_get_flags(res) & RF_ACTIVE) {
3153 error = bus_deactivate_resource(child, type,
3158 rle->flags &= ~RLE_ALLOCATED;
3164 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3174 * @brief Release all active resources of a given type
3176 * Release all active resources of a specified type. This is intended
3177 * to be used to cleanup resources leaked by a driver after detach or
3180 * @param rl the resource list which was allocated from
3181 * @param bus the parent device of @p child
3182 * @param child the device whose active resources are being released
3183 * @param type the type of resources to release
3186 * @retval EBUSY at least one resource was active
3189 resource_list_release_active(struct resource_list *rl, device_t bus,
3190 device_t child, int type)
3192 struct resource_list_entry *rle;
3196 STAILQ_FOREACH(rle, rl, link) {
3197 if (rle->type != type)
3199 if (rle->res == NULL)
3201 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3205 error = resource_list_release(rl, bus, child, type,
3206 rman_get_rid(rle->res), rle->res);
3209 "Failed to release active resource: %d\n", error);
3215 * @brief Fully release a reserved resource
3217 * Fully releases a resource reserved via resource_list_reserve().
3219 * @param rl the resource list which was allocated from
3220 * @param bus the parent device of @p child
3221 * @param child the device whose reserved resource is being released
3222 * @param type the type of resource to release
3223 * @param rid the resource identifier
3224 * @param res the resource to release
3227 * @retval non-zero a standard unix error code indicating what
3228 * error condition prevented the operation
3231 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3234 struct resource_list_entry *rle = NULL;
3235 int passthrough = (device_get_parent(child) != bus);
3239 "resource_list_unreserve() should only be called for direct children");
3241 rle = resource_list_find(rl, type, rid);
3244 panic("resource_list_unreserve: can't find resource");
3245 if (!(rle->flags & RLE_RESERVED))
3247 if (rle->flags & RLE_ALLOCATED)
3249 rle->flags &= ~RLE_RESERVED;
3250 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3254 * @brief Print a description of resources in a resource list
3256 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3257 * The name is printed if at least one resource of the given type is available.
3258 * The format is used to print resource start and end.
3260 * @param rl the resource list to print
3261 * @param name the name of @p type, e.g. @c "memory"
3262 * @param type type type of resource entry to print
3263 * @param format printf(9) format string to print resource
3264 * start and end values
3266 * @returns the number of characters printed
3269 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3272 struct resource_list_entry *rle;
3273 int printed, retval;
3277 /* Yes, this is kinda cheating */
3278 STAILQ_FOREACH(rle, rl, link) {
3279 if (rle->type == type) {
3281 retval += printf(" %s ", name);
3283 retval += printf(",");
3285 retval += printf(format, rle->start);
3286 if (rle->count > 1) {
3287 retval += printf("-");
3288 retval += printf(format, rle->start +
3297 * @brief Releases all the resources in a list.
3299 * @param rl The resource list to purge.
3304 resource_list_purge(struct resource_list *rl)
3306 struct resource_list_entry *rle;
3308 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3310 bus_release_resource(rman_get_device(rle->res),
3311 rle->type, rle->rid, rle->res);
3312 STAILQ_REMOVE_HEAD(rl, link);
3318 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3320 return (device_add_child_ordered(dev, order, name, unit));
3324 * @brief Helper function for implementing DEVICE_PROBE()
3326 * This function can be used to help implement the DEVICE_PROBE() for
3327 * a bus (i.e. a device which has other devices attached to it). It
3328 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3332 bus_generic_probe(device_t dev)
3334 devclass_t dc = dev->devclass;
3337 TAILQ_FOREACH(dl, &dc->drivers, link) {
3339 * If this driver's pass is too high, then ignore it.
3340 * For most drivers in the default pass, this will
3341 * never be true. For early-pass drivers they will
3342 * only call the identify routines of eligible drivers
3343 * when this routine is called. Drivers for later
3344 * passes should have their identify routines called
3345 * on early-pass buses during BUS_NEW_PASS().
3347 if (dl->pass > bus_current_pass)
3349 DEVICE_IDENTIFY(dl->driver, dev);
3356 * @brief Helper function for implementing DEVICE_ATTACH()
3358 * This function can be used to help implement the DEVICE_ATTACH() for
3359 * a bus. It calls device_probe_and_attach() for each of the device's
3363 bus_generic_attach(device_t dev)
3367 TAILQ_FOREACH(child, &dev->children, link) {
3368 device_probe_and_attach(child);
3375 * @brief Helper function for delaying attaching children
3377 * Many buses can't run transactions on the bus which children need to probe and
3378 * attach until after interrupts and/or timers are running. This function
3379 * delays their attach until interrupts and timers are enabled.
3382 bus_delayed_attach_children(device_t dev)
3384 /* Probe and attach the bus children when interrupts are available */
3385 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
3391 * @brief Helper function for implementing DEVICE_DETACH()
3393 * This function can be used to help implement the DEVICE_DETACH() for
3394 * a bus. It calls device_detach() for each of the device's
3398 bus_generic_detach(device_t dev)
3403 if (dev->state != DS_ATTACHED)
3407 * Detach children in the reverse order.
3408 * See bus_generic_suspend for details.
3410 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3411 if ((error = device_detach(child)) != 0)
3419 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3421 * This function can be used to help implement the DEVICE_SHUTDOWN()
3422 * for a bus. It calls device_shutdown() for each of the device's
3426 bus_generic_shutdown(device_t dev)
3431 * Shut down children in the reverse order.
3432 * See bus_generic_suspend for details.
3434 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3435 device_shutdown(child);
3442 * @brief Default function for suspending a child device.
3444 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3447 bus_generic_suspend_child(device_t dev, device_t child)
3451 error = DEVICE_SUSPEND(child);
3454 child->flags |= DF_SUSPENDED;
3460 * @brief Default function for resuming a child device.
3462 * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3465 bus_generic_resume_child(device_t dev, device_t child)
3467 DEVICE_RESUME(child);
3468 child->flags &= ~DF_SUSPENDED;
3474 * @brief Helper function for implementing DEVICE_SUSPEND()
3476 * This function can be used to help implement the DEVICE_SUSPEND()
3477 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3478 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3479 * operation is aborted and any devices which were suspended are
3480 * resumed immediately by calling their DEVICE_RESUME() methods.
3483 bus_generic_suspend(device_t dev)
3489 * Suspend children in the reverse order.
3490 * For most buses all children are equal, so the order does not matter.
3491 * Other buses, such as acpi, carefully order their child devices to
3492 * express implicit dependencies between them. For such buses it is
3493 * safer to bring down devices in the reverse order.
3495 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3496 error = BUS_SUSPEND_CHILD(dev, child);
3498 child = TAILQ_NEXT(child, link);
3499 if (child != NULL) {
3500 TAILQ_FOREACH_FROM(child, &dev->children, link)
3501 BUS_RESUME_CHILD(dev, child);
3510 * @brief Helper function for implementing DEVICE_RESUME()
3512 * This function can be used to help implement the DEVICE_RESUME() for
3513 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3516 bus_generic_resume(device_t dev)
3520 TAILQ_FOREACH(child, &dev->children, link) {
3521 BUS_RESUME_CHILD(dev, child);
3522 /* if resume fails, there's nothing we can usefully do... */
3528 * @brief Helper function for implementing BUS_RESET_POST
3530 * Bus can use this function to implement common operations of
3531 * re-attaching or resuming the children after the bus itself was
3532 * reset, and after restoring bus-unique state of children.
3534 * @param dev The bus
3535 * #param flags DEVF_RESET_*
3538 bus_helper_reset_post(device_t dev, int flags)
3544 TAILQ_FOREACH(child, &dev->children,link) {
3545 BUS_RESET_POST(dev, child);
3546 error1 = (flags & DEVF_RESET_DETACH) != 0 ?
3547 device_probe_and_attach(child) :
3548 BUS_RESUME_CHILD(dev, child);
3549 if (error == 0 && error1 != 0)
3556 bus_helper_reset_prepare_rollback(device_t dev, device_t child, int flags)
3558 child = TAILQ_NEXT(child, link);
3561 TAILQ_FOREACH_FROM(child, &dev->children,link) {
3562 BUS_RESET_POST(dev, child);
3563 if ((flags & DEVF_RESET_DETACH) != 0)
3564 device_probe_and_attach(child);
3566 BUS_RESUME_CHILD(dev, child);
3571 * @brief Helper function for implementing BUS_RESET_PREPARE
3573 * Bus can use this function to implement common operations of
3574 * detaching or suspending the children before the bus itself is
3575 * reset, and then save bus-unique state of children that must
3576 * persists around reset.
3578 * @param dev The bus
3579 * #param flags DEVF_RESET_*
3582 bus_helper_reset_prepare(device_t dev, int flags)
3587 if (dev->state != DS_ATTACHED)
3590 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3591 if ((flags & DEVF_RESET_DETACH) != 0) {
3592 error = device_get_state(child) == DS_ATTACHED ?
3593 device_detach(child) : 0;
3595 error = BUS_SUSPEND_CHILD(dev, child);
3598 error = BUS_RESET_PREPARE(dev, child);
3600 if ((flags & DEVF_RESET_DETACH) != 0)
3601 device_probe_and_attach(child);
3603 BUS_RESUME_CHILD(dev, child);
3607 bus_helper_reset_prepare_rollback(dev, child, flags);
3615 * @brief Helper function for implementing BUS_PRINT_CHILD().
3617 * This function prints the first part of the ascii representation of
3618 * @p child, including its name, unit and description (if any - see
3619 * device_set_desc()).
3621 * @returns the number of characters printed
3624 bus_print_child_header(device_t dev, device_t child)
3628 if (device_get_desc(child)) {
3629 retval += device_printf(child, "<%s>", device_get_desc(child));
3631 retval += printf("%s", device_get_nameunit(child));
3638 * @brief Helper function for implementing BUS_PRINT_CHILD().
3640 * This function prints the last part of the ascii representation of
3641 * @p child, which consists of the string @c " on " followed by the
3642 * name and unit of the @p dev.
3644 * @returns the number of characters printed
3647 bus_print_child_footer(device_t dev, device_t child)
3649 return (printf(" on %s\n", device_get_nameunit(dev)));
3653 * @brief Helper function for implementing BUS_PRINT_CHILD().
3655 * This function prints out the VM domain for the given device.
3657 * @returns the number of characters printed
3660 bus_print_child_domain(device_t dev, device_t child)
3664 /* No domain? Don't print anything */
3665 if (BUS_GET_DOMAIN(dev, child, &domain) != 0)
3668 return (printf(" numa-domain %d", domain));
3672 * @brief Helper function for implementing BUS_PRINT_CHILD().
3674 * This function simply calls bus_print_child_header() followed by
3675 * bus_print_child_footer().
3677 * @returns the number of characters printed
3680 bus_generic_print_child(device_t dev, device_t child)
3684 retval += bus_print_child_header(dev, child);
3685 retval += bus_print_child_domain(dev, child);
3686 retval += bus_print_child_footer(dev, child);
3692 * @brief Stub function for implementing BUS_READ_IVAR().
3697 bus_generic_read_ivar(device_t dev, device_t child, int index,
3704 * @brief Stub function for implementing BUS_WRITE_IVAR().
3709 bus_generic_write_ivar(device_t dev, device_t child, int index,
3716 * @brief Helper function for implementing BUS_GET_PROPERTY().
3718 * This simply calls the BUS_GET_PROPERTY of the parent of dev,
3719 * until a non-default implementation is found.
3722 bus_generic_get_property(device_t dev, device_t child, const char *propname,
3723 void *propvalue, size_t size, device_property_type_t type)
3725 if (device_get_parent(dev) != NULL)
3726 return (BUS_GET_PROPERTY(device_get_parent(dev), child,
3727 propname, propvalue, size, type));
3733 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3737 struct resource_list *
3738 bus_generic_get_resource_list(device_t dev, device_t child)
3744 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3746 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3747 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3748 * and then calls device_probe_and_attach() for each unattached child.
3751 bus_generic_driver_added(device_t dev, driver_t *driver)
3755 DEVICE_IDENTIFY(driver, dev);
3756 TAILQ_FOREACH(child, &dev->children, link) {
3757 if (child->state == DS_NOTPRESENT)
3758 device_probe_and_attach(child);
3763 * @brief Helper function for implementing BUS_NEW_PASS().
3765 * This implementing of BUS_NEW_PASS() first calls the identify
3766 * routines for any drivers that probe at the current pass. Then it
3767 * walks the list of devices for this bus. If a device is already
3768 * attached, then it calls BUS_NEW_PASS() on that device. If the
3769 * device is not already attached, it attempts to attach a driver to
3773 bus_generic_new_pass(device_t dev)
3780 TAILQ_FOREACH(dl, &dc->drivers, link) {
3781 if (dl->pass == bus_current_pass)
3782 DEVICE_IDENTIFY(dl->driver, dev);
3784 TAILQ_FOREACH(child, &dev->children, link) {
3785 if (child->state >= DS_ATTACHED)
3786 BUS_NEW_PASS(child);
3787 else if (child->state == DS_NOTPRESENT)
3788 device_probe_and_attach(child);
3793 * @brief Helper function for implementing BUS_SETUP_INTR().
3795 * This simple implementation of BUS_SETUP_INTR() simply calls the
3796 * BUS_SETUP_INTR() method of the parent of @p dev.
3799 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3800 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3803 /* Propagate up the bus hierarchy until someone handles it. */
3805 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3806 filter, intr, arg, cookiep));
3811 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3813 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3814 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3817 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3820 /* Propagate up the bus hierarchy until someone handles it. */
3822 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3827 * @brief Helper function for implementing BUS_SUSPEND_INTR().
3829 * This simple implementation of BUS_SUSPEND_INTR() simply calls the
3830 * BUS_SUSPEND_INTR() method of the parent of @p dev.
3833 bus_generic_suspend_intr(device_t dev, device_t child, struct resource *irq)
3835 /* Propagate up the bus hierarchy until someone handles it. */
3837 return (BUS_SUSPEND_INTR(dev->parent, child, irq));
3842 * @brief Helper function for implementing BUS_RESUME_INTR().
3844 * This simple implementation of BUS_RESUME_INTR() simply calls the
3845 * BUS_RESUME_INTR() method of the parent of @p dev.
3848 bus_generic_resume_intr(device_t dev, device_t child, struct resource *irq)
3850 /* Propagate up the bus hierarchy until someone handles it. */
3852 return (BUS_RESUME_INTR(dev->parent, child, irq));
3857 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3859 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3860 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3863 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3864 struct resource *r, rman_res_t start, rman_res_t end)
3866 /* Propagate up the bus hierarchy until someone handles it. */
3868 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3874 * @brief Helper function for implementing BUS_TRANSLATE_RESOURCE().
3876 * This simple implementation of BUS_TRANSLATE_RESOURCE() simply calls the
3877 * BUS_TRANSLATE_RESOURCE() method of the parent of @p dev. If there is no
3878 * parent, no translation happens.
3881 bus_generic_translate_resource(device_t dev, int type, rman_res_t start,
3882 rman_res_t *newstart)
3885 return (BUS_TRANSLATE_RESOURCE(dev->parent, type, start,
3892 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3894 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3895 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3898 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3899 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3901 /* Propagate up the bus hierarchy until someone handles it. */
3903 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3904 start, end, count, flags));
3909 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3911 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3912 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3915 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3918 /* Propagate up the bus hierarchy until someone handles it. */
3920 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3926 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3928 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3929 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3932 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3935 /* Propagate up the bus hierarchy until someone handles it. */
3937 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3943 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3945 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3946 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3949 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3950 int rid, struct resource *r)
3952 /* Propagate up the bus hierarchy until someone handles it. */
3954 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3960 * @brief Helper function for implementing BUS_MAP_RESOURCE().
3962 * This simple implementation of BUS_MAP_RESOURCE() simply calls the
3963 * BUS_MAP_RESOURCE() method of the parent of @p dev.
3966 bus_generic_map_resource(device_t dev, device_t child, int type,
3967 struct resource *r, struct resource_map_request *args,
3968 struct resource_map *map)
3970 /* Propagate up the bus hierarchy until someone handles it. */
3972 return (BUS_MAP_RESOURCE(dev->parent, child, type, r, args,
3978 * @brief Helper function for implementing BUS_UNMAP_RESOURCE().
3980 * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the
3981 * BUS_UNMAP_RESOURCE() method of the parent of @p dev.
3984 bus_generic_unmap_resource(device_t dev, device_t child, int type,
3985 struct resource *r, struct resource_map *map)
3987 /* Propagate up the bus hierarchy until someone handles it. */
3989 return (BUS_UNMAP_RESOURCE(dev->parent, child, type, r, map));
3994 * @brief Helper function for implementing BUS_BIND_INTR().
3996 * This simple implementation of BUS_BIND_INTR() simply calls the
3997 * BUS_BIND_INTR() method of the parent of @p dev.
4000 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
4003 /* Propagate up the bus hierarchy until someone handles it. */
4005 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
4010 * @brief Helper function for implementing BUS_CONFIG_INTR().
4012 * This simple implementation of BUS_CONFIG_INTR() simply calls the
4013 * BUS_CONFIG_INTR() method of the parent of @p dev.
4016 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
4017 enum intr_polarity pol)
4019 /* Propagate up the bus hierarchy until someone handles it. */
4021 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
4026 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
4028 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
4029 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
4032 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
4033 void *cookie, const char *descr)
4035 /* Propagate up the bus hierarchy until someone handles it. */
4037 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
4043 * @brief Helper function for implementing BUS_GET_CPUS().
4045 * This simple implementation of BUS_GET_CPUS() simply calls the
4046 * BUS_GET_CPUS() method of the parent of @p dev.
4049 bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op,
4050 size_t setsize, cpuset_t *cpuset)
4052 /* Propagate up the bus hierarchy until someone handles it. */
4053 if (dev->parent != NULL)
4054 return (BUS_GET_CPUS(dev->parent, child, op, setsize, cpuset));
4059 * @brief Helper function for implementing BUS_GET_DMA_TAG().
4061 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
4062 * BUS_GET_DMA_TAG() method of the parent of @p dev.
4065 bus_generic_get_dma_tag(device_t dev, device_t child)
4067 /* Propagate up the bus hierarchy until someone handles it. */
4068 if (dev->parent != NULL)
4069 return (BUS_GET_DMA_TAG(dev->parent, child));
4074 * @brief Helper function for implementing BUS_GET_BUS_TAG().
4076 * This simple implementation of BUS_GET_BUS_TAG() simply calls the
4077 * BUS_GET_BUS_TAG() method of the parent of @p dev.
4080 bus_generic_get_bus_tag(device_t dev, device_t child)
4082 /* Propagate up the bus hierarchy until someone handles it. */
4083 if (dev->parent != NULL)
4084 return (BUS_GET_BUS_TAG(dev->parent, child));
4085 return ((bus_space_tag_t)0);
4089 * @brief Helper function for implementing BUS_GET_RESOURCE().
4091 * This implementation of BUS_GET_RESOURCE() uses the
4092 * resource_list_find() function to do most of the work. It calls
4093 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4097 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4098 rman_res_t *startp, rman_res_t *countp)
4100 struct resource_list * rl = NULL;
4101 struct resource_list_entry * rle = NULL;
4103 rl = BUS_GET_RESOURCE_LIST(dev, child);
4107 rle = resource_list_find(rl, type, rid);
4112 *startp = rle->start;
4114 *countp = rle->count;
4120 * @brief Helper function for implementing BUS_SET_RESOURCE().
4122 * This implementation of BUS_SET_RESOURCE() uses the
4123 * resource_list_add() function to do most of the work. It calls
4124 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4128 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4129 rman_res_t start, rman_res_t count)
4131 struct resource_list * rl = NULL;
4133 rl = BUS_GET_RESOURCE_LIST(dev, child);
4137 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4143 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4145 * This implementation of BUS_DELETE_RESOURCE() uses the
4146 * resource_list_delete() function to do most of the work. It calls
4147 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4151 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4153 struct resource_list * rl = NULL;
4155 rl = BUS_GET_RESOURCE_LIST(dev, child);
4159 resource_list_delete(rl, type, rid);
4165 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4167 * This implementation of BUS_RELEASE_RESOURCE() uses the
4168 * resource_list_release() function to do most of the work. It calls
4169 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4172 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4173 int rid, struct resource *r)
4175 struct resource_list * rl = NULL;
4177 if (device_get_parent(child) != dev)
4178 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4181 rl = BUS_GET_RESOURCE_LIST(dev, child);
4185 return (resource_list_release(rl, dev, child, type, rid, r));
4189 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4191 * This implementation of BUS_ALLOC_RESOURCE() uses the
4192 * resource_list_alloc() function to do most of the work. It calls
4193 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4196 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4197 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4199 struct resource_list * rl = NULL;
4201 if (device_get_parent(child) != dev)
4202 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4203 type, rid, start, end, count, flags));
4205 rl = BUS_GET_RESOURCE_LIST(dev, child);
4209 return (resource_list_alloc(rl, dev, child, type, rid,
4210 start, end, count, flags));
4214 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4216 * This implementation of BUS_ALLOC_RESOURCE() allocates a
4217 * resource from a resource manager. It uses BUS_GET_RMAN()
4218 * to obtain the resource manager.
4221 bus_generic_rman_alloc_resource(device_t dev, device_t child, int type,
4222 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4227 rm = BUS_GET_RMAN(dev, type, flags);
4231 r = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
4235 rman_set_rid(r, *rid);
4237 if (flags & RF_ACTIVE) {
4238 if (bus_activate_resource(child, type, *rid, r) != 0) {
4239 rman_release_resource(r);
4248 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
4250 * This implementation of BUS_ADJUST_RESOURCE() adjusts resources only
4251 * if they were allocated from the resource manager returned by
4255 bus_generic_rman_adjust_resource(device_t dev, device_t child, int type,
4256 struct resource *r, rman_res_t start, rman_res_t end)
4260 rm = BUS_GET_RMAN(dev, type, rman_get_flags(r));
4263 if (!rman_is_region_manager(r, rm))
4265 return (rman_adjust_resource(r, start, end));
4269 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4271 * This implementation of BUS_RELEASE_RESOURCE() releases resources
4272 * allocated by bus_generic_rman_alloc_resource.
4275 bus_generic_rman_release_resource(device_t dev, device_t child, int type,
4276 int rid, struct resource *r)
4284 rm = BUS_GET_RMAN(dev, type, rman_get_flags(r));
4285 KASSERT(rman_is_region_manager(r, rm),
4286 ("%s: rman %p doesn't match for resource %p", __func__, rm, r));
4289 if (rman_get_flags(r) & RF_ACTIVE) {
4290 error = bus_deactivate_resource(child, type, rid, r);
4294 return (rman_release_resource(r));
4298 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
4300 * This implementation of BUS_ACTIVATE_RESOURCE() activates resources
4301 * allocated by bus_generic_rman_alloc_resource.
4304 bus_generic_rman_activate_resource(device_t dev, device_t child, int type,
4305 int rid, struct resource *r)
4307 struct resource_map map;
4308 #ifdef INVARIANTS_XXX
4313 #ifdef INVARIANTS_XXX
4314 rm = BUS_GET_RMAN(dev, type, rman_get_flags(r));
4315 KASSERT(rman_is_region_manager(r, rm),
4316 ("%s: rman %p doesn't match for resource %p", __func__, rm, r));
4319 error = rman_activate_resource(r);
4323 if ((rman_get_flags(r) & RF_UNMAPPED) == 0 &&
4324 (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) {
4325 error = BUS_MAP_RESOURCE(dev, child, type, r, NULL, &map);
4327 rman_deactivate_resource(r);
4331 rman_set_mapping(r, &map);
4337 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
4339 * This implementation of BUS_DEACTIVATE_RESOURCE() deactivates
4340 * resources allocated by bus_generic_rman_alloc_resource.
4343 bus_generic_rman_deactivate_resource(device_t dev, device_t child, int type,
4344 int rid, struct resource *r)
4346 struct resource_map map;
4347 #ifdef INVARIANTS_XXX
4352 #ifdef INVARIANTS_XXX
4353 rm = BUS_GET_RMAN(dev, type, rman_get_flags(r));
4354 KASSERT(rman_is_region_manager(r, rm),
4355 ("%s: rman %p doesn't match for resource %p", __func__, rm, r));
4358 error = rman_deactivate_resource(r);
4362 if ((rman_get_flags(r) & RF_UNMAPPED) == 0 &&
4363 (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) {
4364 rman_get_mapping(r, &map);
4365 BUS_UNMAP_RESOURCE(dev, child, type, r, &map);
4371 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4373 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4374 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4377 bus_generic_child_present(device_t dev, device_t child)
4379 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4383 * @brief Helper function for implementing BUS_GET_DOMAIN().
4385 * This simple implementation of BUS_GET_DOMAIN() calls the
4386 * BUS_GET_DOMAIN() method of the parent of @p dev. If @p dev
4387 * does not have a parent, the function fails with ENOENT.
4390 bus_generic_get_domain(device_t dev, device_t child, int *domain)
4393 return (BUS_GET_DOMAIN(dev->parent, dev, domain));
4399 * @brief Helper function to implement normal BUS_GET_DEVICE_PATH()
4401 * This function knows how to (a) pass the request up the tree if there's
4402 * a parent and (b) Knows how to supply a FreeBSD locator.
4404 * @param bus bus in the walk up the tree
4405 * @param child leaf node to print information about
4406 * @param locator BUS_LOCATOR_xxx string for locator
4407 * @param sb Buffer to print information into
4410 bus_generic_get_device_path(device_t bus, device_t child, const char *locator,
4417 * We don't recurse on ACPI since either we know the handle for the
4418 * device or we don't. And if we're in the generic routine, we don't
4419 * have a ACPI override. All other locators build up a path by having
4420 * their parents create a path and then adding the path element for this
4421 * node. That's why we recurse with parent, bus rather than the typical
4422 * parent, child: each spot in the tree is independent of what our child
4423 * will do with this path.
4425 parent = device_get_parent(bus);
4426 if (parent != NULL && strcmp(locator, BUS_LOCATOR_ACPI) != 0) {
4427 rv = BUS_GET_DEVICE_PATH(parent, bus, locator, sb);
4429 if (strcmp(locator, BUS_LOCATOR_FREEBSD) == 0) {
4431 sbuf_printf(sb, "/%s", device_get_nameunit(child));
4436 * Don't know what to do. So assume we do nothing. Not sure that's
4437 * the right thing, but keeps us from having a big list here.
4444 * @brief Helper function for implementing BUS_RESCAN().
4446 * This null implementation of BUS_RESCAN() always fails to indicate
4447 * the bus does not support rescanning.
4450 bus_null_rescan(device_t dev)
4456 * Some convenience functions to make it easier for drivers to use the
4457 * resource-management functions. All these really do is hide the
4458 * indirection through the parent's method table, making for slightly
4459 * less-wordy code. In the future, it might make sense for this code
4460 * to maintain some sort of a list of resources allocated by each device.
4464 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4465 struct resource **res)
4469 for (i = 0; rs[i].type != -1; i++)
4471 for (i = 0; rs[i].type != -1; i++) {
4472 res[i] = bus_alloc_resource_any(dev,
4473 rs[i].type, &rs[i].rid, rs[i].flags);
4474 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4475 bus_release_resources(dev, rs, res);
4483 bus_release_resources(device_t dev, const struct resource_spec *rs,
4484 struct resource **res)
4488 for (i = 0; rs[i].type != -1; i++)
4489 if (res[i] != NULL) {
4490 bus_release_resource(
4491 dev, rs[i].type, rs[i].rid, res[i]);
4497 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4499 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4503 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
4504 rman_res_t end, rman_res_t count, u_int flags)
4506 struct resource *res;
4508 if (dev->parent == NULL)
4510 res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4516 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4518 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4522 bus_adjust_resource(device_t dev, int type, struct resource *r, rman_res_t start,
4525 if (dev->parent == NULL)
4527 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4531 * @brief Wrapper function for BUS_TRANSLATE_RESOURCE().
4533 * This function simply calls the BUS_TRANSLATE_RESOURCE() method of the
4537 bus_translate_resource(device_t dev, int type, rman_res_t start,
4538 rman_res_t *newstart)
4540 if (dev->parent == NULL)
4542 return (BUS_TRANSLATE_RESOURCE(dev->parent, type, start, newstart));
4546 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4548 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4552 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4554 if (dev->parent == NULL)
4556 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4560 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4562 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4566 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4568 if (dev->parent == NULL)
4570 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4574 * @brief Wrapper function for BUS_MAP_RESOURCE().
4576 * This function simply calls the BUS_MAP_RESOURCE() method of the
4580 bus_map_resource(device_t dev, int type, struct resource *r,
4581 struct resource_map_request *args, struct resource_map *map)
4583 if (dev->parent == NULL)
4585 return (BUS_MAP_RESOURCE(dev->parent, dev, type, r, args, map));
4589 * @brief Wrapper function for BUS_UNMAP_RESOURCE().
4591 * This function simply calls the BUS_UNMAP_RESOURCE() method of the
4595 bus_unmap_resource(device_t dev, int type, struct resource *r,
4596 struct resource_map *map)
4598 if (dev->parent == NULL)
4600 return (BUS_UNMAP_RESOURCE(dev->parent, dev, type, r, map));
4604 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4606 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4610 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4614 if (dev->parent == NULL)
4616 rv = BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r);
4621 * @brief Wrapper function for BUS_SETUP_INTR().
4623 * This function simply calls the BUS_SETUP_INTR() method of the
4627 bus_setup_intr(device_t dev, struct resource *r, int flags,
4628 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4632 if (dev->parent == NULL)
4634 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4638 if (handler != NULL && !(flags & INTR_MPSAFE))
4639 device_printf(dev, "[GIANT-LOCKED]\n");
4644 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4646 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4650 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4652 if (dev->parent == NULL)
4654 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4658 * @brief Wrapper function for BUS_SUSPEND_INTR().
4660 * This function simply calls the BUS_SUSPEND_INTR() method of the
4664 bus_suspend_intr(device_t dev, struct resource *r)
4666 if (dev->parent == NULL)
4668 return (BUS_SUSPEND_INTR(dev->parent, dev, r));
4672 * @brief Wrapper function for BUS_RESUME_INTR().
4674 * This function simply calls the BUS_RESUME_INTR() method of the
4678 bus_resume_intr(device_t dev, struct resource *r)
4680 if (dev->parent == NULL)
4682 return (BUS_RESUME_INTR(dev->parent, dev, r));
4686 * @brief Wrapper function for BUS_BIND_INTR().
4688 * This function simply calls the BUS_BIND_INTR() method of the
4692 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4694 if (dev->parent == NULL)
4696 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4700 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4702 * This function first formats the requested description into a
4703 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4704 * the parent of @p dev.
4707 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4708 const char *fmt, ...)
4711 char descr[MAXCOMLEN + 1];
4713 if (dev->parent == NULL)
4716 vsnprintf(descr, sizeof(descr), fmt, ap);
4718 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4722 * @brief Wrapper function for BUS_SET_RESOURCE().
4724 * This function simply calls the BUS_SET_RESOURCE() method of the
4728 bus_set_resource(device_t dev, int type, int rid,
4729 rman_res_t start, rman_res_t count)
4731 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4736 * @brief Wrapper function for BUS_GET_RESOURCE().
4738 * This function simply calls the BUS_GET_RESOURCE() method of the
4742 bus_get_resource(device_t dev, int type, int rid,
4743 rman_res_t *startp, rman_res_t *countp)
4745 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4750 * @brief Wrapper function for BUS_GET_RESOURCE().
4752 * This function simply calls the BUS_GET_RESOURCE() method of the
4753 * parent of @p dev and returns the start value.
4756 bus_get_resource_start(device_t dev, int type, int rid)
4762 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4770 * @brief Wrapper function for BUS_GET_RESOURCE().
4772 * This function simply calls the BUS_GET_RESOURCE() method of the
4773 * parent of @p dev and returns the count value.
4776 bus_get_resource_count(device_t dev, int type, int rid)
4782 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4790 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4792 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4796 bus_delete_resource(device_t dev, int type, int rid)
4798 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4802 * @brief Wrapper function for BUS_CHILD_PRESENT().
4804 * This function simply calls the BUS_CHILD_PRESENT() method of the
4808 bus_child_present(device_t child)
4810 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4814 * @brief Wrapper function for BUS_CHILD_PNPINFO().
4816 * This function simply calls the BUS_CHILD_PNPINFO() method of the parent of @p
4820 bus_child_pnpinfo(device_t child, struct sbuf *sb)
4824 parent = device_get_parent(child);
4827 return (BUS_CHILD_PNPINFO(parent, child, sb));
4831 * @brief Generic implementation that does nothing for bus_child_pnpinfo
4833 * This function has the right signature and returns 0 since the sbuf is passed
4834 * to us to append to.
4837 bus_generic_child_pnpinfo(device_t dev, device_t child, struct sbuf *sb)
4843 * @brief Wrapper function for BUS_CHILD_LOCATION().
4845 * This function simply calls the BUS_CHILD_LOCATION() method of the parent of
4849 bus_child_location(device_t child, struct sbuf *sb)
4853 parent = device_get_parent(child);
4856 return (BUS_CHILD_LOCATION(parent, child, sb));
4860 * @brief Generic implementation that does nothing for bus_child_location
4862 * This function has the right signature and returns 0 since the sbuf is passed
4863 * to us to append to.
4866 bus_generic_child_location(device_t dev, device_t child, struct sbuf *sb)
4872 * @brief Wrapper function for BUS_GET_CPUS().
4874 * This function simply calls the BUS_GET_CPUS() method of the
4878 bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, cpuset_t *cpuset)
4882 parent = device_get_parent(dev);
4885 return (BUS_GET_CPUS(parent, dev, op, setsize, cpuset));
4889 * @brief Wrapper function for BUS_GET_DMA_TAG().
4891 * This function simply calls the BUS_GET_DMA_TAG() method of the
4895 bus_get_dma_tag(device_t dev)
4899 parent = device_get_parent(dev);
4902 return (BUS_GET_DMA_TAG(parent, dev));
4906 * @brief Wrapper function for BUS_GET_BUS_TAG().
4908 * This function simply calls the BUS_GET_BUS_TAG() method of the
4912 bus_get_bus_tag(device_t dev)
4916 parent = device_get_parent(dev);
4918 return ((bus_space_tag_t)0);
4919 return (BUS_GET_BUS_TAG(parent, dev));
4923 * @brief Wrapper function for BUS_GET_DOMAIN().
4925 * This function simply calls the BUS_GET_DOMAIN() method of the
4929 bus_get_domain(device_t dev, int *domain)
4931 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain));
4934 /* Resume all devices and then notify userland that we're up again. */
4936 root_resume(device_t dev)
4940 error = bus_generic_resume(dev);
4942 devctl_notify("kernel", "power", "resume", NULL);
4948 root_print_child(device_t dev, device_t child)
4952 retval += bus_print_child_header(dev, child);
4953 retval += printf("\n");
4959 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4960 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4963 * If an interrupt mapping gets to here something bad has happened.
4965 panic("root_setup_intr");
4969 * If we get here, assume that the device is permanent and really is
4970 * present in the system. Removable bus drivers are expected to intercept
4971 * this call long before it gets here. We return -1 so that drivers that
4972 * really care can check vs -1 or some ERRNO returned higher in the food
4976 root_child_present(device_t dev, device_t child)
4982 root_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
4987 /* Default to returning the set of all CPUs. */
4988 if (setsize != sizeof(cpuset_t))
4997 static kobj_method_t root_methods[] = {
4998 /* Device interface */
4999 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
5000 KOBJMETHOD(device_suspend, bus_generic_suspend),
5001 KOBJMETHOD(device_resume, root_resume),
5004 KOBJMETHOD(bus_print_child, root_print_child),
5005 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
5006 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
5007 KOBJMETHOD(bus_setup_intr, root_setup_intr),
5008 KOBJMETHOD(bus_child_present, root_child_present),
5009 KOBJMETHOD(bus_get_cpus, root_get_cpus),
5014 static driver_t root_driver = {
5021 devclass_t root_devclass;
5024 root_bus_module_handler(module_t mod, int what, void* arg)
5028 TAILQ_INIT(&bus_data_devices);
5029 kobj_class_compile((kobj_class_t) &root_driver);
5030 root_bus = make_device(NULL, "root", 0);
5031 root_bus->desc = "System root bus";
5032 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
5033 root_bus->driver = &root_driver;
5034 root_bus->state = DS_ATTACHED;
5035 root_devclass = devclass_find_internal("root", NULL, FALSE);
5040 device_shutdown(root_bus);
5043 return (EOPNOTSUPP);
5049 static moduledata_t root_bus_mod = {
5051 root_bus_module_handler,
5054 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
5057 * @brief Automatically configure devices
5059 * This function begins the autoconfiguration process by calling
5060 * device_probe_and_attach() for each child of the @c root0 device.
5063 root_bus_configure(void)
5067 /* Eventually this will be split up, but this is sufficient for now. */
5068 bus_set_pass(BUS_PASS_DEFAULT);
5072 * @brief Module handler for registering device drivers
5074 * This module handler is used to automatically register device
5075 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
5076 * devclass_add_driver() for the driver described by the
5077 * driver_module_data structure pointed to by @p arg
5080 driver_module_handler(module_t mod, int what, void *arg)
5082 struct driver_module_data *dmd;
5083 devclass_t bus_devclass;
5084 kobj_class_t driver;
5087 dmd = (struct driver_module_data *)arg;
5088 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
5093 if (dmd->dmd_chainevh)
5094 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5096 pass = dmd->dmd_pass;
5097 driver = dmd->dmd_driver;
5098 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
5099 DRIVERNAME(driver), dmd->dmd_busname, pass));
5100 error = devclass_add_driver(bus_devclass, driver, pass,
5105 PDEBUG(("Unloading module: driver %s from bus %s",
5106 DRIVERNAME(dmd->dmd_driver),
5108 error = devclass_delete_driver(bus_devclass,
5111 if (!error && dmd->dmd_chainevh)
5112 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5115 PDEBUG(("Quiesce module: driver %s from bus %s",
5116 DRIVERNAME(dmd->dmd_driver),
5118 error = devclass_quiesce_driver(bus_devclass,
5121 if (!error && dmd->dmd_chainevh)
5122 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
5133 * @brief Enumerate all hinted devices for this bus.
5135 * Walks through the hints for this bus and calls the bus_hinted_child
5136 * routine for each one it fines. It searches first for the specific
5137 * bus that's being probed for hinted children (eg isa0), and then for
5138 * generic children (eg isa).
5140 * @param dev bus device to enumerate
5143 bus_enumerate_hinted_children(device_t bus)
5146 const char *dname, *busname;
5150 * enumerate all devices on the specific bus
5152 busname = device_get_nameunit(bus);
5154 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5155 BUS_HINTED_CHILD(bus, dname, dunit);
5158 * and all the generic ones.
5160 busname = device_get_name(bus);
5162 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
5163 BUS_HINTED_CHILD(bus, dname, dunit);
5168 /* the _short versions avoid iteration by not calling anything that prints
5169 * more than oneliners. I love oneliners.
5173 print_device_short(device_t dev, int indent)
5178 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
5179 dev->unit, dev->desc,
5180 (dev->parent? "":"no "),
5181 (TAILQ_EMPTY(&dev->children)? "no ":""),
5182 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
5183 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
5184 (dev->flags&DF_WILDCARD? "wildcard,":""),
5185 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
5186 (dev->flags&DF_SUSPENDED? "suspended,":""),
5187 (dev->ivars? "":"no "),
5188 (dev->softc? "":"no "),
5193 print_device(device_t dev, int indent)
5198 print_device_short(dev, indent);
5200 indentprintf(("Parent:\n"));
5201 print_device_short(dev->parent, indent+1);
5202 indentprintf(("Driver:\n"));
5203 print_driver_short(dev->driver, indent+1);
5204 indentprintf(("Devclass:\n"));
5205 print_devclass_short(dev->devclass, indent+1);
5209 print_device_tree_short(device_t dev, int indent)
5210 /* print the device and all its children (indented) */
5217 print_device_short(dev, indent);
5219 TAILQ_FOREACH(child, &dev->children, link) {
5220 print_device_tree_short(child, indent+1);
5225 print_device_tree(device_t dev, int indent)
5226 /* print the device and all its children (indented) */
5233 print_device(dev, indent);
5235 TAILQ_FOREACH(child, &dev->children, link) {
5236 print_device_tree(child, indent+1);
5241 print_driver_short(driver_t *driver, int indent)
5246 indentprintf(("driver %s: softc size = %zd\n",
5247 driver->name, driver->size));
5251 print_driver(driver_t *driver, int indent)
5256 print_driver_short(driver, indent);
5260 print_driver_list(driver_list_t drivers, int indent)
5262 driverlink_t driver;
5264 TAILQ_FOREACH(driver, &drivers, link) {
5265 print_driver(driver->driver, indent);
5270 print_devclass_short(devclass_t dc, int indent)
5275 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
5279 print_devclass(devclass_t dc, int indent)
5286 print_devclass_short(dc, indent);
5287 indentprintf(("Drivers:\n"));
5288 print_driver_list(dc->drivers, indent+1);
5290 indentprintf(("Devices:\n"));
5291 for (i = 0; i < dc->maxunit; i++)
5293 print_device(dc->devices[i], indent+1);
5297 print_devclass_list_short(void)
5301 printf("Short listing of devclasses, drivers & devices:\n");
5302 TAILQ_FOREACH(dc, &devclasses, link) {
5303 print_devclass_short(dc, 0);
5308 print_devclass_list(void)
5312 printf("Full listing of devclasses, drivers & devices:\n");
5313 TAILQ_FOREACH(dc, &devclasses, link) {
5314 print_devclass(dc, 0);
5321 * User-space access to the device tree.
5323 * We implement a small set of nodes:
5325 * hw.bus Single integer read method to obtain the
5326 * current generation count.
5327 * hw.bus.devices Reads the entire device tree in flat space.
5328 * hw.bus.rman Resource manager interface
5330 * We might like to add the ability to scan devclasses and/or drivers to
5331 * determine what else is currently loaded/available.
5335 sysctl_bus_info(SYSCTL_HANDLER_ARGS)
5337 struct u_businfo ubus;
5339 ubus.ub_version = BUS_USER_VERSION;
5340 ubus.ub_generation = bus_data_generation;
5342 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
5344 SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD |
5345 CTLFLAG_MPSAFE, NULL, 0, sysctl_bus_info, "S,u_businfo",
5346 "bus-related data");
5349 sysctl_devices(SYSCTL_HANDLER_ARGS)
5352 int *name = (int *)arg1;
5353 u_int namelen = arg2;
5356 struct u_device *udev;
5362 if (bus_data_generation_check(name[0]))
5368 * Scan the list of devices, looking for the requested index.
5370 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5378 * Populate the return item, careful not to overflow the buffer.
5380 udev = malloc(sizeof(*udev), M_BUS, M_WAITOK | M_ZERO);
5383 udev->dv_handle = (uintptr_t)dev;
5384 udev->dv_parent = (uintptr_t)dev->parent;
5385 udev->dv_devflags = dev->devflags;
5386 udev->dv_flags = dev->flags;
5387 udev->dv_state = dev->state;
5388 sbuf_new(&sb, udev->dv_fields, sizeof(udev->dv_fields), SBUF_FIXEDLEN);
5389 if (dev->nameunit != NULL)
5390 sbuf_cat(&sb, dev->nameunit);
5391 sbuf_putc(&sb, '\0');
5392 if (dev->desc != NULL)
5393 sbuf_cat(&sb, dev->desc);
5394 sbuf_putc(&sb, '\0');
5395 if (dev->driver != NULL)
5396 sbuf_cat(&sb, dev->driver->name);
5397 sbuf_putc(&sb, '\0');
5398 bus_child_pnpinfo(dev, &sb);
5399 sbuf_putc(&sb, '\0');
5400 bus_child_location(dev, &sb);
5401 sbuf_putc(&sb, '\0');
5402 error = sbuf_finish(&sb);
5404 error = SYSCTL_OUT(req, udev, sizeof(*udev));
5410 SYSCTL_NODE(_hw_bus, OID_AUTO, devices,
5411 CTLFLAG_RD | CTLFLAG_NEEDGIANT, sysctl_devices,
5412 "system device tree");
5415 bus_data_generation_check(int generation)
5417 if (generation != bus_data_generation)
5420 /* XXX generate optimised lists here? */
5425 bus_data_generation_update(void)
5427 atomic_add_int(&bus_data_generation, 1);
5431 bus_free_resource(device_t dev, int type, struct resource *r)
5435 return (bus_release_resource(dev, type, rman_get_rid(r), r));
5439 device_lookup_by_name(const char *name)
5443 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5444 if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0)
5451 * /dev/devctl2 implementation. The existing /dev/devctl device has
5452 * implicit semantics on open, so it could not be reused for this.
5453 * Another option would be to call this /dev/bus?
5456 find_device(struct devreq *req, device_t *devp)
5461 * First, ensure that the name is nul terminated.
5463 if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL)
5467 * Second, try to find an attached device whose name matches
5470 dev = device_lookup_by_name(req->dr_name);
5476 /* Finally, give device enumerators a chance. */
5478 EVENTHANDLER_DIRECT_INVOKE(dev_lookup, req->dr_name, &dev);
5486 driver_exists(device_t bus, const char *driver)
5490 for (dc = bus->devclass; dc != NULL; dc = dc->parent) {
5491 if (devclass_find_driver_internal(dc, driver) != NULL)
5498 device_gen_nomatch(device_t dev)
5502 if (dev->flags & DF_NEEDNOMATCH &&
5503 dev->state == DS_NOTPRESENT) {
5504 device_handle_nomatch(dev);
5506 dev->flags &= ~DF_NEEDNOMATCH;
5507 TAILQ_FOREACH(child, &dev->children, link) {
5508 device_gen_nomatch(child);
5513 device_do_deferred_actions(void)
5519 * Walk through the devclasses to find all the drivers we've tagged as
5520 * deferred during the freeze and call the driver added routines. They
5521 * have already been added to the lists in the background, so the driver
5522 * added routines that trigger a probe will have all the right bidders
5523 * for the probe auction.
5525 TAILQ_FOREACH(dc, &devclasses, link) {
5526 TAILQ_FOREACH(dl, &dc->drivers, link) {
5527 if (dl->flags & DL_DEFERRED_PROBE) {
5528 devclass_driver_added(dc, dl->driver);
5529 dl->flags &= ~DL_DEFERRED_PROBE;
5535 * We also defer no-match events during a freeze. Walk the tree and
5536 * generate all the pent-up events that are still relevant.
5538 device_gen_nomatch(root_bus);
5539 bus_data_generation_update();
5543 device_get_path(device_t dev, const char *locator, struct sbuf *sb)
5548 KASSERT(sb != NULL, ("sb is NULL"));
5549 parent = device_get_parent(dev);
5550 if (parent == NULL) {
5551 error = sbuf_putc(sb, '/');
5553 error = BUS_GET_DEVICE_PATH(parent, dev, locator, sb);
5555 error = sbuf_error(sb);
5556 if (error == 0 && sbuf_len(sb) <= 1)
5565 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
5572 /* Locate the device to control. */
5574 req = (struct devreq *)data;
5582 case DEV_SET_DRIVER:
5583 case DEV_CLEAR_DRIVER:
5587 error = priv_check(td, PRIV_DRIVER);
5589 error = find_device(req, &dev);
5593 error = priv_check(td, PRIV_DRIVER);
5596 error = find_device(req, &dev);
5607 /* Perform the requested operation. */
5610 if (device_is_attached(dev))
5612 else if (!device_is_enabled(dev))
5615 error = device_probe_and_attach(dev);
5618 if (!device_is_attached(dev)) {
5622 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5623 error = device_quiesce(dev);
5627 error = device_detach(dev);
5630 if (device_is_enabled(dev)) {
5636 * If the device has been probed but not attached (e.g.
5637 * when it has been disabled by a loader hint), just
5638 * attach the device rather than doing a full probe.
5641 if (device_is_alive(dev)) {
5643 * If the device was disabled via a hint, clear
5646 if (resource_disabled(dev->driver->name, dev->unit))
5647 resource_unset_value(dev->driver->name,
5648 dev->unit, "disabled");
5649 error = device_attach(dev);
5651 error = device_probe_and_attach(dev);
5654 if (!device_is_enabled(dev)) {
5659 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5660 error = device_quiesce(dev);
5666 * Force DF_FIXEDCLASS on around detach to preserve
5667 * the existing name.
5670 dev->flags |= DF_FIXEDCLASS;
5671 error = device_detach(dev);
5672 if (!(old & DF_FIXEDCLASS))
5673 dev->flags &= ~DF_FIXEDCLASS;
5675 device_disable(dev);
5678 if (device_is_suspended(dev)) {
5682 if (device_get_parent(dev) == NULL) {
5686 error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev);
5689 if (!device_is_suspended(dev)) {
5693 if (device_get_parent(dev) == NULL) {
5697 error = BUS_RESUME_CHILD(device_get_parent(dev), dev);
5699 case DEV_SET_DRIVER: {
5703 error = copyinstr(req->dr_data, driver, sizeof(driver), NULL);
5706 if (driver[0] == '\0') {
5710 if (dev->devclass != NULL &&
5711 strcmp(driver, dev->devclass->name) == 0)
5712 /* XXX: Could possibly force DF_FIXEDCLASS on? */
5716 * Scan drivers for this device's bus looking for at
5717 * least one matching driver.
5719 if (dev->parent == NULL) {
5723 if (!driver_exists(dev->parent, driver)) {
5727 dc = devclass_create(driver);
5733 /* Detach device if necessary. */
5734 if (device_is_attached(dev)) {
5735 if (req->dr_flags & DEVF_SET_DRIVER_DETACH)
5736 error = device_detach(dev);
5743 /* Clear any previously-fixed device class and unit. */
5744 if (dev->flags & DF_FIXEDCLASS)
5745 devclass_delete_device(dev->devclass, dev);
5746 dev->flags |= DF_WILDCARD;
5749 /* Force the new device class. */
5750 error = devclass_add_device(dc, dev);
5753 dev->flags |= DF_FIXEDCLASS;
5754 error = device_probe_and_attach(dev);
5757 case DEV_CLEAR_DRIVER:
5758 if (!(dev->flags & DF_FIXEDCLASS)) {
5762 if (device_is_attached(dev)) {
5763 if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH)
5764 error = device_detach(dev);
5771 dev->flags &= ~DF_FIXEDCLASS;
5772 dev->flags |= DF_WILDCARD;
5773 devclass_delete_device(dev->devclass, dev);
5774 error = device_probe_and_attach(dev);
5777 if (!device_is_attached(dev)) {
5781 error = BUS_RESCAN(dev);
5786 parent = device_get_parent(dev);
5787 if (parent == NULL) {
5791 if (!(req->dr_flags & DEVF_FORCE_DELETE)) {
5792 if (bus_child_present(dev) != 0) {
5798 error = device_delete_child(parent, dev);
5805 device_frozen = true;
5811 device_do_deferred_actions();
5812 device_frozen = false;
5816 if ((req->dr_flags & ~(DEVF_RESET_DETACH)) != 0) {
5820 error = BUS_RESET_CHILD(device_get_parent(dev), dev,
5823 case DEV_GET_PATH: {
5828 error = copyinstr(req->dr_buffer.buffer, locator,
5829 sizeof(locator), NULL);
5832 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND |
5833 SBUF_INCLUDENUL /* | SBUF_WAITOK */);
5834 error = device_get_path(dev, locator, sb);
5837 if (req->dr_buffer.length < len) {
5838 error = ENAMETOOLONG;
5840 error = copyout(sbuf_data(sb),
5841 req->dr_buffer.buffer, len);
5843 req->dr_buffer.length = len;
5853 static struct cdevsw devctl2_cdevsw = {
5854 .d_version = D_VERSION,
5855 .d_ioctl = devctl2_ioctl,
5856 .d_name = "devctl2",
5862 make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL,
5863 UID_ROOT, GID_WHEEL, 0644, "devctl2");
5867 * For maintaining device 'at' location info to avoid recomputing it
5869 struct device_location_node {
5870 const char *dln_locator;
5871 const char *dln_path;
5872 TAILQ_ENTRY(device_location_node) dln_link;
5874 typedef TAILQ_HEAD(device_location_list, device_location_node) device_location_list_t;
5876 struct device_location_cache {
5877 device_location_list_t dlc_list;
5882 * Location cache for wired devices.
5884 device_location_cache_t *
5885 dev_wired_cache_init(void)
5887 device_location_cache_t *dcp;
5889 dcp = malloc(sizeof(*dcp), M_BUS, M_WAITOK | M_ZERO);
5890 TAILQ_INIT(&dcp->dlc_list);
5896 dev_wired_cache_fini(device_location_cache_t *dcp)
5898 struct device_location_node *dln, *tdln;
5900 TAILQ_FOREACH_SAFE(dln, &dcp->dlc_list, dln_link, tdln) {
5906 static struct device_location_node *
5907 dev_wired_cache_lookup(device_location_cache_t *dcp, const char *locator)
5909 struct device_location_node *dln;
5911 TAILQ_FOREACH(dln, &dcp->dlc_list, dln_link) {
5912 if (strcmp(locator, dln->dln_locator) == 0)
5919 static struct device_location_node *
5920 dev_wired_cache_add(device_location_cache_t *dcp, const char *locator, const char *path)
5922 struct device_location_node *dln;
5923 size_t loclen, pathlen;
5925 loclen = strlen(locator) + 1;
5926 pathlen = strlen(path) + 1;
5927 dln = malloc(sizeof(*dln) + loclen + pathlen, M_BUS, M_WAITOK | M_ZERO);
5928 dln->dln_locator = (char *)(dln + 1);
5929 memcpy(__DECONST(char *, dln->dln_locator), locator, loclen);
5930 dln->dln_path = dln->dln_locator + loclen;
5931 memcpy(__DECONST(char *, dln->dln_path), path, pathlen);
5932 TAILQ_INSERT_HEAD(&dcp->dlc_list, dln, dln_link);
5938 dev_wired_cache_match(device_location_cache_t *dcp, device_t dev,
5945 struct device_location_node *res;
5947 cp = strchr(at, ':');
5951 if (len > sizeof(locator) - 1) /* Skip too long locator */
5953 memcpy(locator, at, len);
5954 locator[len] = '\0';
5958 /* maybe cache this inside device_t and look that up, but not yet */
5959 res = dev_wired_cache_lookup(dcp, locator);
5961 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND |
5962 SBUF_INCLUDENUL | SBUF_NOWAIT);
5964 error = device_get_path(dev, locator, sb);
5966 res = dev_wired_cache_add(dcp, locator,
5972 if (error != 0 || res == NULL || res->dln_path == NULL)
5975 return (strcmp(res->dln_path, cp) == 0);
5979 * APIs to manage deprecation and obsolescence.
5981 static int obsolete_panic = 0;
5982 SYSCTL_INT(_debug, OID_AUTO, obsolete_panic, CTLFLAG_RWTUN, &obsolete_panic, 0,
5983 "Panic when obsolete features are used (0 = never, 1 = if obsolete, "
5984 "2 = if deprecated)");
5987 gone_panic(int major, int running, const char *msg)
5989 switch (obsolete_panic)
5994 if (running < major)
6003 _gone_in(int major, const char *msg)
6005 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
6006 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
6007 printf("Obsolete code will be removed soon: %s\n", msg);
6009 printf("Deprecated code (to be removed in FreeBSD %d): %s\n",
6014 _gone_in_dev(device_t dev, int major, const char *msg)
6016 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
6017 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
6019 "Obsolete code will be removed soon: %s\n", msg);
6022 "Deprecated code (to be removed in FreeBSD %d): %s\n",
6027 DB_SHOW_COMMAND(device, db_show_device)
6034 dev = (device_t)addr;
6036 db_printf("name: %s\n", device_get_nameunit(dev));
6037 db_printf(" driver: %s\n", DRIVERNAME(dev->driver));
6038 db_printf(" class: %s\n", DEVCLANAME(dev->devclass));
6039 db_printf(" addr: %p\n", dev);
6040 db_printf(" parent: %p\n", dev->parent);
6041 db_printf(" softc: %p\n", dev->softc);
6042 db_printf(" ivars: %p\n", dev->ivars);
6045 DB_SHOW_ALL_COMMAND(devices, db_show_all_devices)
6049 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
6050 db_show_device((db_expr_t)dev, true, count, modif);