2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997,1998,2003 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/domainset.h>
38 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
46 #include <machine/bus.h>
47 #include <sys/random.h>
48 #include <sys/refcount.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
55 #include <sys/cpuset.h>
59 #include <machine/cpu.h>
60 #include <machine/stdarg.h>
67 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
69 SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
73 * Used to attach drivers to devclasses.
75 typedef struct driverlink *driverlink_t;
78 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
81 #define DL_DEFERRED_PROBE 1 /* Probe deferred on this */
82 TAILQ_ENTRY(driverlink) passlink;
86 * Forward declarations
88 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
89 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
90 typedef TAILQ_HEAD(device_list, _device) device_list_t;
93 TAILQ_ENTRY(devclass) link;
94 devclass_t parent; /* parent in devclass hierarchy */
95 driver_list_t drivers; /* bus devclasses store drivers for bus */
97 device_t *devices; /* array of devices indexed by unit */
98 int maxunit; /* size of devices array */
100 #define DC_HAS_CHILDREN 1
102 struct sysctl_ctx_list sysctl_ctx;
103 struct sysctl_oid *sysctl_tree;
107 * @brief Implementation of _device.
109 * The structure is named "_device" instead of "device" to avoid type confusion
110 * caused by other subsystems defining a (struct device).
114 * A device is a kernel object. The first field must be the
115 * current ops table for the object.
122 TAILQ_ENTRY(_device) link; /**< list of devices in parent */
123 TAILQ_ENTRY(_device) devlink; /**< global device list membership */
124 device_t parent; /**< parent of this device */
125 device_list_t children; /**< list of child devices */
128 * Details of this device.
130 driver_t *driver; /**< current driver */
131 devclass_t devclass; /**< current device class */
132 int unit; /**< current unit number */
133 char* nameunit; /**< name+unit e.g. foodev0 */
134 char* desc; /**< driver specific description */
135 u_int busy; /**< count of calls to device_busy() */
136 device_state_t state; /**< current device state */
137 uint32_t devflags; /**< api level flags for device_get_flags() */
138 u_int flags; /**< internal device flags */
139 u_int order; /**< order from device_add_child_ordered() */
140 void *ivars; /**< instance variables */
141 void *softc; /**< current driver's variables */
143 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
144 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
147 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
148 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
150 EVENTHANDLER_LIST_DEFINE(device_attach);
151 EVENTHANDLER_LIST_DEFINE(device_detach);
152 EVENTHANDLER_LIST_DEFINE(device_nomatch);
153 EVENTHANDLER_LIST_DEFINE(dev_lookup);
155 static void devctl2_init(void);
156 static bool device_frozen;
158 #define DRIVERNAME(d) ((d)? d->name : "no driver")
159 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
163 static int bus_debug = 1;
164 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
166 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
167 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
170 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
171 * prevent syslog from deleting initial spaces
173 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
175 static void print_device_short(device_t dev, int indent);
176 static void print_device(device_t dev, int indent);
177 void print_device_tree_short(device_t dev, int indent);
178 void print_device_tree(device_t dev, int indent);
179 static void print_driver_short(driver_t *driver, int indent);
180 static void print_driver(driver_t *driver, int indent);
181 static void print_driver_list(driver_list_t drivers, int indent);
182 static void print_devclass_short(devclass_t dc, int indent);
183 static void print_devclass(devclass_t dc, int indent);
184 void print_devclass_list_short(void);
185 void print_devclass_list(void);
188 /* Make the compiler ignore the function calls */
189 #define PDEBUG(a) /* nop */
190 #define DEVICENAME(d) /* nop */
192 #define print_device_short(d,i) /* nop */
193 #define print_device(d,i) /* nop */
194 #define print_device_tree_short(d,i) /* nop */
195 #define print_device_tree(d,i) /* nop */
196 #define print_driver_short(d,i) /* nop */
197 #define print_driver(d,i) /* nop */
198 #define print_driver_list(d,i) /* nop */
199 #define print_devclass_short(d,i) /* nop */
200 #define print_devclass(d,i) /* nop */
201 #define print_devclass_list_short() /* nop */
202 #define print_devclass_list() /* nop */
210 DEVCLASS_SYSCTL_PARENT,
214 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
216 devclass_t dc = (devclass_t)arg1;
220 case DEVCLASS_SYSCTL_PARENT:
221 value = dc->parent ? dc->parent->name : "";
226 return (SYSCTL_OUT_STR(req, value));
230 devclass_sysctl_init(devclass_t dc)
232 if (dc->sysctl_tree != NULL)
234 sysctl_ctx_init(&dc->sysctl_ctx);
235 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
236 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
237 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
238 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
240 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
241 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
247 DEVICE_SYSCTL_DRIVER,
248 DEVICE_SYSCTL_LOCATION,
249 DEVICE_SYSCTL_PNPINFO,
250 DEVICE_SYSCTL_PARENT,
254 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
257 device_t dev = (device_t)arg1;
260 sbuf_new_for_sysctl(&sb, NULL, 1024, req);
261 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
264 case DEVICE_SYSCTL_DESC:
265 sbuf_cat(&sb, dev->desc ? dev->desc : "");
267 case DEVICE_SYSCTL_DRIVER:
268 sbuf_cat(&sb, dev->driver ? dev->driver->name : "");
270 case DEVICE_SYSCTL_LOCATION:
271 bus_child_location(dev, &sb);
273 case DEVICE_SYSCTL_PNPINFO:
274 bus_child_pnpinfo(dev, &sb);
276 case DEVICE_SYSCTL_PARENT:
277 sbuf_cat(&sb, dev->parent ? dev->parent->nameunit : "");
283 error = sbuf_finish(&sb);
291 device_sysctl_init(device_t dev)
293 devclass_t dc = dev->devclass;
296 if (dev->sysctl_tree != NULL)
298 devclass_sysctl_init(dc);
299 sysctl_ctx_init(&dev->sysctl_ctx);
300 dev->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&dev->sysctl_ctx,
301 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
302 dev->nameunit + strlen(dc->name),
303 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", "device_index");
304 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
305 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
306 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
307 "device description");
308 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
310 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
311 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
312 "device driver name");
313 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
314 OID_AUTO, "%location",
315 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
316 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
317 "device location relative to parent");
318 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
319 OID_AUTO, "%pnpinfo",
320 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
321 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
322 "device identification");
323 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
325 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
326 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
328 if (bus_get_domain(dev, &domain) == 0)
329 SYSCTL_ADD_INT(&dev->sysctl_ctx,
330 SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain",
331 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, domain, "NUMA domain");
335 device_sysctl_update(device_t dev)
337 devclass_t dc = dev->devclass;
339 if (dev->sysctl_tree == NULL)
341 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
345 device_sysctl_fini(device_t dev)
347 if (dev->sysctl_tree == NULL)
349 sysctl_ctx_free(&dev->sysctl_ctx);
350 dev->sysctl_tree = NULL;
353 static struct device_list bus_data_devices;
354 static int bus_data_generation = 1;
356 static kobj_method_t null_methods[] = {
360 DEFINE_CLASS(null, null_methods, 0);
363 bus_topo_assert(void)
380 mtx_lock(bus_topo_mtx());
384 bus_topo_unlock(void)
387 mtx_unlock(bus_topo_mtx());
391 * Bus pass implementation
394 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
395 int bus_current_pass = BUS_PASS_ROOT;
399 * @brief Register the pass level of a new driver attachment
401 * Register a new driver attachment's pass level. If no driver
402 * attachment with the same pass level has been added, then @p new
403 * will be added to the global passes list.
405 * @param new the new driver attachment
408 driver_register_pass(struct driverlink *new)
410 struct driverlink *dl;
412 /* We only consider pass numbers during boot. */
413 if (bus_current_pass == BUS_PASS_DEFAULT)
417 * Walk the passes list. If we already know about this pass
418 * then there is nothing to do. If we don't, then insert this
419 * driver link into the list.
421 TAILQ_FOREACH(dl, &passes, passlink) {
422 if (dl->pass < new->pass)
424 if (dl->pass == new->pass)
426 TAILQ_INSERT_BEFORE(dl, new, passlink);
429 TAILQ_INSERT_TAIL(&passes, new, passlink);
433 * @brief Raise the current bus pass
435 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
436 * method on the root bus to kick off a new device tree scan for each
437 * new pass level that has at least one driver.
440 bus_set_pass(int pass)
442 struct driverlink *dl;
444 if (bus_current_pass > pass)
445 panic("Attempt to lower bus pass level");
447 TAILQ_FOREACH(dl, &passes, passlink) {
448 /* Skip pass values below the current pass level. */
449 if (dl->pass <= bus_current_pass)
453 * Bail once we hit a driver with a pass level that is
460 * Raise the pass level to the next level and rescan
463 bus_current_pass = dl->pass;
464 BUS_NEW_PASS(root_bus);
468 * If there isn't a driver registered for the requested pass,
469 * then bus_current_pass might still be less than 'pass'. Set
470 * it to 'pass' in that case.
472 if (bus_current_pass < pass)
473 bus_current_pass = pass;
474 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
478 * Devclass implementation
481 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
485 * @brief Find or create a device class
487 * If a device class with the name @p classname exists, return it,
488 * otherwise if @p create is non-zero create and return a new device
491 * If @p parentname is non-NULL, the parent of the devclass is set to
492 * the devclass of that name.
494 * @param classname the devclass name to find or create
495 * @param parentname the parent devclass name or @c NULL
496 * @param create non-zero to create a devclass
499 devclass_find_internal(const char *classname, const char *parentname,
504 PDEBUG(("looking for %s", classname));
508 TAILQ_FOREACH(dc, &devclasses, link) {
509 if (!strcmp(dc->name, classname))
514 PDEBUG(("creating %s", classname));
515 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
516 M_BUS, M_NOWAIT | M_ZERO);
520 dc->name = (char*) (dc + 1);
521 strcpy(dc->name, classname);
522 TAILQ_INIT(&dc->drivers);
523 TAILQ_INSERT_TAIL(&devclasses, dc, link);
525 bus_data_generation_update();
529 * If a parent class is specified, then set that as our parent so
530 * that this devclass will support drivers for the parent class as
531 * well. If the parent class has the same name don't do this though
532 * as it creates a cycle that can trigger an infinite loop in
533 * device_probe_child() if a device exists for which there is no
536 if (parentname && dc && !dc->parent &&
537 strcmp(classname, parentname) != 0) {
538 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
539 dc->parent->flags |= DC_HAS_CHILDREN;
546 * @brief Create a device class
548 * If a device class with the name @p classname exists, return it,
549 * otherwise create and return a new device class.
551 * @param classname the devclass name to find or create
554 devclass_create(const char *classname)
556 return (devclass_find_internal(classname, NULL, TRUE));
560 * @brief Find a device class
562 * If a device class with the name @p classname exists, return it,
563 * otherwise return @c NULL.
565 * @param classname the devclass name to find
568 devclass_find(const char *classname)
570 return (devclass_find_internal(classname, NULL, FALSE));
574 * @brief Register that a device driver has been added to a devclass
576 * Register that a device driver has been added to a devclass. This
577 * is called by devclass_add_driver to accomplish the recursive
578 * notification of all the children classes of dc, as well as dc.
579 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
582 * We do a full search here of the devclass list at each iteration
583 * level to save storing children-lists in the devclass structure. If
584 * we ever move beyond a few dozen devices doing this, we may need to
587 * @param dc the devclass to edit
588 * @param driver the driver that was just added
591 devclass_driver_added(devclass_t dc, driver_t *driver)
597 * Call BUS_DRIVER_ADDED for any existing buses in this class.
599 for (i = 0; i < dc->maxunit; i++)
600 if (dc->devices[i] && device_is_attached(dc->devices[i]))
601 BUS_DRIVER_ADDED(dc->devices[i], driver);
604 * Walk through the children classes. Since we only keep a
605 * single parent pointer around, we walk the entire list of
606 * devclasses looking for children. We set the
607 * DC_HAS_CHILDREN flag when a child devclass is created on
608 * the parent, so we only walk the list for those devclasses
609 * that have children.
611 if (!(dc->flags & DC_HAS_CHILDREN))
614 TAILQ_FOREACH(dc, &devclasses, link) {
615 if (dc->parent == parent)
616 devclass_driver_added(dc, driver);
621 device_handle_nomatch(device_t dev)
623 BUS_PROBE_NOMATCH(dev->parent, dev);
624 EVENTHANDLER_DIRECT_INVOKE(device_nomatch, dev);
625 dev->flags |= DF_DONENOMATCH;
629 * @brief Add a device driver to a device class
631 * Add a device driver to a devclass. This is normally called
632 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
633 * all devices in the devclass will be called to allow them to attempt
634 * to re-probe any unmatched children.
636 * @param dc the devclass to edit
637 * @param driver the driver to register
640 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
644 const char *parentname;
646 PDEBUG(("%s", DRIVERNAME(driver)));
648 /* Don't allow invalid pass values. */
649 if (pass <= BUS_PASS_ROOT)
652 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
657 * Compile the driver's methods. Also increase the reference count
658 * so that the class doesn't get freed when the last instance
659 * goes. This means we can safely use static methods and avoids a
660 * double-free in devclass_delete_driver.
662 kobj_class_compile((kobj_class_t) driver);
665 * If the driver has any base classes, make the
666 * devclass inherit from the devclass of the driver's
667 * first base class. This will allow the system to
668 * search for drivers in both devclasses for children
669 * of a device using this driver.
671 if (driver->baseclasses)
672 parentname = driver->baseclasses[0]->name;
675 child_dc = devclass_find_internal(driver->name, parentname, TRUE);
680 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
681 driver->refs++; /* XXX: kobj_mtx */
683 driver_register_pass(dl);
686 dl->flags |= DL_DEFERRED_PROBE;
688 devclass_driver_added(dc, driver);
690 bus_data_generation_update();
695 * @brief Register that a device driver has been deleted from a devclass
697 * Register that a device driver has been removed from a devclass.
698 * This is called by devclass_delete_driver to accomplish the
699 * recursive notification of all the children classes of busclass, as
700 * well as busclass. Each layer will attempt to detach the driver
701 * from any devices that are children of the bus's devclass. The function
702 * will return an error if a device fails to detach.
704 * We do a full search here of the devclass list at each iteration
705 * level to save storing children-lists in the devclass structure. If
706 * we ever move beyond a few dozen devices doing this, we may need to
709 * @param busclass the devclass of the parent bus
710 * @param dc the devclass of the driver being deleted
711 * @param driver the driver being deleted
714 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
721 * Disassociate from any devices. We iterate through all the
722 * devices in the devclass of the driver and detach any which are
723 * using the driver and which have a parent in the devclass which
724 * we are deleting from.
726 * Note that since a driver can be in multiple devclasses, we
727 * should not detach devices which are not children of devices in
728 * the affected devclass.
730 * If we're frozen, we don't generate NOMATCH events. Mark to
733 for (i = 0; i < dc->maxunit; i++) {
734 if (dc->devices[i]) {
735 dev = dc->devices[i];
736 if (dev->driver == driver && dev->parent &&
737 dev->parent->devclass == busclass) {
738 if ((error = device_detach(dev)) != 0)
741 dev->flags &= ~DF_DONENOMATCH;
742 dev->flags |= DF_NEEDNOMATCH;
744 device_handle_nomatch(dev);
751 * Walk through the children classes. Since we only keep a
752 * single parent pointer around, we walk the entire list of
753 * devclasses looking for children. We set the
754 * DC_HAS_CHILDREN flag when a child devclass is created on
755 * the parent, so we only walk the list for those devclasses
756 * that have children.
758 if (!(busclass->flags & DC_HAS_CHILDREN))
761 TAILQ_FOREACH(busclass, &devclasses, link) {
762 if (busclass->parent == parent) {
763 error = devclass_driver_deleted(busclass, dc, driver);
772 * @brief Delete a device driver from a device class
774 * Delete a device driver from a devclass. This is normally called
775 * automatically by DRIVER_MODULE().
777 * If the driver is currently attached to any devices,
778 * devclass_delete_driver() will first attempt to detach from each
779 * device. If one of the detach calls fails, the driver will not be
782 * @param dc the devclass to edit
783 * @param driver the driver to unregister
786 devclass_delete_driver(devclass_t busclass, driver_t *driver)
788 devclass_t dc = devclass_find(driver->name);
792 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
798 * Find the link structure in the bus' list of drivers.
800 TAILQ_FOREACH(dl, &busclass->drivers, link) {
801 if (dl->driver == driver)
806 PDEBUG(("%s not found in %s list", driver->name,
811 error = devclass_driver_deleted(busclass, dc, driver);
815 TAILQ_REMOVE(&busclass->drivers, dl, link);
820 if (driver->refs == 0)
821 kobj_class_free((kobj_class_t) driver);
823 bus_data_generation_update();
828 * @brief Quiesces a set of device drivers from a device class
830 * Quiesce a device driver from a devclass. This is normally called
831 * automatically by DRIVER_MODULE().
833 * If the driver is currently attached to any devices,
834 * devclass_quiesece_driver() will first attempt to quiesce each
837 * @param dc the devclass to edit
838 * @param driver the driver to unregister
841 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
843 devclass_t dc = devclass_find(driver->name);
849 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
855 * Find the link structure in the bus' list of drivers.
857 TAILQ_FOREACH(dl, &busclass->drivers, link) {
858 if (dl->driver == driver)
863 PDEBUG(("%s not found in %s list", driver->name,
869 * Quiesce all devices. We iterate through all the devices in
870 * the devclass of the driver and quiesce any which are using
871 * the driver and which have a parent in the devclass which we
874 * Note that since a driver can be in multiple devclasses, we
875 * should not quiesce devices which are not children of
876 * devices in the affected devclass.
878 for (i = 0; i < dc->maxunit; i++) {
879 if (dc->devices[i]) {
880 dev = dc->devices[i];
881 if (dev->driver == driver && dev->parent &&
882 dev->parent->devclass == busclass) {
883 if ((error = device_quiesce(dev)) != 0)
896 devclass_find_driver_internal(devclass_t dc, const char *classname)
900 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
902 TAILQ_FOREACH(dl, &dc->drivers, link) {
903 if (!strcmp(dl->driver->name, classname))
907 PDEBUG(("not found"));
912 * @brief Return the name of the devclass
915 devclass_get_name(devclass_t dc)
921 * @brief Find a device given a unit number
923 * @param dc the devclass to search
924 * @param unit the unit number to search for
926 * @returns the device with the given unit number or @c
927 * NULL if there is no such device
930 devclass_get_device(devclass_t dc, int unit)
932 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
934 return (dc->devices[unit]);
938 * @brief Find the softc field of a device given a unit number
940 * @param dc the devclass to search
941 * @param unit the unit number to search for
943 * @returns the softc field of the device with the given
944 * unit number or @c NULL if there is no such
948 devclass_get_softc(devclass_t dc, int unit)
952 dev = devclass_get_device(dc, unit);
956 return (device_get_softc(dev));
960 * @brief Get a list of devices in the devclass
962 * An array containing a list of all the devices in the given devclass
963 * is allocated and returned in @p *devlistp. The number of devices
964 * in the array is returned in @p *devcountp. The caller should free
965 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
967 * @param dc the devclass to examine
968 * @param devlistp points at location for array pointer return
970 * @param devcountp points at location for array size return value
973 * @retval ENOMEM the array allocation failed
976 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
981 count = devclass_get_count(dc);
982 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
987 for (i = 0; i < dc->maxunit; i++) {
988 if (dc->devices[i]) {
989 list[count] = dc->devices[i];
1001 * @brief Get a list of drivers in the devclass
1003 * An array containing a list of pointers to all the drivers in the
1004 * given devclass is allocated and returned in @p *listp. The number
1005 * of drivers in the array is returned in @p *countp. The caller should
1006 * free the array using @c free(p, M_TEMP).
1008 * @param dc the devclass to examine
1009 * @param listp gives location for array pointer return value
1010 * @param countp gives location for number of array elements
1014 * @retval ENOMEM the array allocation failed
1017 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1024 TAILQ_FOREACH(dl, &dc->drivers, link)
1026 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1031 TAILQ_FOREACH(dl, &dc->drivers, link) {
1032 list[count] = dl->driver;
1042 * @brief Get the number of devices in a devclass
1044 * @param dc the devclass to examine
1047 devclass_get_count(devclass_t dc)
1052 for (i = 0; i < dc->maxunit; i++)
1059 * @brief Get the maximum unit number used in a devclass
1061 * Note that this is one greater than the highest currently-allocated
1062 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1063 * that not even the devclass has been allocated yet.
1065 * @param dc the devclass to examine
1068 devclass_get_maxunit(devclass_t dc)
1072 return (dc->maxunit);
1076 * @brief Find a free unit number in a devclass
1078 * This function searches for the first unused unit number greater
1079 * that or equal to @p unit.
1081 * @param dc the devclass to examine
1082 * @param unit the first unit number to check
1085 devclass_find_free_unit(devclass_t dc, int unit)
1089 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1095 * @brief Set the parent of a devclass
1097 * The parent class is normally initialised automatically by
1100 * @param dc the devclass to edit
1101 * @param pdc the new parent devclass
1104 devclass_set_parent(devclass_t dc, devclass_t pdc)
1110 * @brief Get the parent of a devclass
1112 * @param dc the devclass to examine
1115 devclass_get_parent(devclass_t dc)
1117 return (dc->parent);
1120 struct sysctl_ctx_list *
1121 devclass_get_sysctl_ctx(devclass_t dc)
1123 return (&dc->sysctl_ctx);
1127 devclass_get_sysctl_tree(devclass_t dc)
1129 return (dc->sysctl_tree);
1134 * @brief Allocate a unit number
1136 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1137 * will do). The allocated unit number is returned in @p *unitp.
1139 * @param dc the devclass to allocate from
1140 * @param unitp points at the location for the allocated unit
1144 * @retval EEXIST the requested unit number is already allocated
1145 * @retval ENOMEM memory allocation failure
1148 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1153 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1155 /* Ask the parent bus if it wants to wire this device. */
1157 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1160 /* If we were given a wired unit number, check for existing device */
1163 if (unit >= 0 && unit < dc->maxunit &&
1164 dc->devices[unit] != NULL) {
1166 printf("%s: %s%d already exists; skipping it\n",
1167 dc->name, dc->name, *unitp);
1171 /* Unwired device, find the next available slot for it */
1173 for (unit = 0;; unit++) {
1174 /* If this device slot is already in use, skip it. */
1175 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1178 /* If there is an "at" hint for a unit then skip it. */
1179 if (resource_string_value(dc->name, unit, "at", &s) ==
1188 * We've selected a unit beyond the length of the table, so let's
1189 * extend the table to make room for all units up to and including
1192 if (unit >= dc->maxunit) {
1193 device_t *newlist, *oldlist;
1196 oldlist = dc->devices;
1197 newsize = roundup((unit + 1),
1198 MAX(1, MINALLOCSIZE / sizeof(device_t)));
1199 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1202 if (oldlist != NULL)
1203 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1204 bzero(newlist + dc->maxunit,
1205 sizeof(device_t) * (newsize - dc->maxunit));
1206 dc->devices = newlist;
1207 dc->maxunit = newsize;
1208 if (oldlist != NULL)
1209 free(oldlist, M_BUS);
1211 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1219 * @brief Add a device to a devclass
1221 * A unit number is allocated for the device (using the device's
1222 * preferred unit number if any) and the device is registered in the
1223 * devclass. This allows the device to be looked up by its unit
1224 * number, e.g. by decoding a dev_t minor number.
1226 * @param dc the devclass to add to
1227 * @param dev the device to add
1230 * @retval EEXIST the requested unit number is already allocated
1231 * @retval ENOMEM memory allocation failure
1234 devclass_add_device(devclass_t dc, device_t dev)
1238 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1240 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1243 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1247 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1248 free(dev->nameunit, M_BUS);
1249 dev->nameunit = NULL;
1252 dc->devices[dev->unit] = dev;
1254 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1261 * @brief Delete a device from a devclass
1263 * The device is removed from the devclass's device list and its unit
1266 * @param dc the devclass to delete from
1267 * @param dev the device to delete
1272 devclass_delete_device(devclass_t dc, device_t dev)
1277 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1279 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1280 panic("devclass_delete_device: inconsistent device class");
1281 dc->devices[dev->unit] = NULL;
1282 if (dev->flags & DF_WILDCARD)
1284 dev->devclass = NULL;
1285 free(dev->nameunit, M_BUS);
1286 dev->nameunit = NULL;
1293 * @brief Make a new device and add it as a child of @p parent
1295 * @param parent the parent of the new device
1296 * @param name the devclass name of the new device or @c NULL
1297 * to leave the devclass unspecified
1298 * @parem unit the unit number of the new device of @c -1 to
1299 * leave the unit number unspecified
1301 * @returns the new device
1304 make_device(device_t parent, const char *name, int unit)
1309 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1312 dc = devclass_find_internal(name, NULL, TRUE);
1314 printf("make_device: can't find device class %s\n",
1322 dev = malloc(sizeof(*dev), M_BUS, M_NOWAIT|M_ZERO);
1326 dev->parent = parent;
1327 TAILQ_INIT(&dev->children);
1328 kobj_init((kobj_t) dev, &null_class);
1330 dev->devclass = NULL;
1332 dev->nameunit = NULL;
1336 dev->flags = DF_ENABLED;
1339 dev->flags |= DF_WILDCARD;
1341 dev->flags |= DF_FIXEDCLASS;
1342 if (devclass_add_device(dc, dev)) {
1343 kobj_delete((kobj_t) dev, M_BUS);
1347 if (parent != NULL && device_has_quiet_children(parent))
1348 dev->flags |= DF_QUIET | DF_QUIET_CHILDREN;
1352 dev->state = DS_NOTPRESENT;
1354 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1355 bus_data_generation_update();
1362 * @brief Print a description of a device.
1365 device_print_child(device_t dev, device_t child)
1369 if (device_is_alive(child))
1370 retval += BUS_PRINT_CHILD(dev, child);
1372 retval += device_printf(child, " not found\n");
1378 * @brief Create a new device
1380 * This creates a new device and adds it as a child of an existing
1381 * parent device. The new device will be added after the last existing
1382 * child with order zero.
1384 * @param dev the device which will be the parent of the
1386 * @param name devclass name for new device or @c NULL if not
1388 * @param unit unit number for new device or @c -1 if not
1391 * @returns the new device
1394 device_add_child(device_t dev, const char *name, int unit)
1396 return (device_add_child_ordered(dev, 0, name, unit));
1400 * @brief Create a new device
1402 * This creates a new device and adds it as a child of an existing
1403 * parent device. The new device will be added after the last existing
1404 * child with the same order.
1406 * @param dev the device which will be the parent of the
1408 * @param order a value which is used to partially sort the
1409 * children of @p dev - devices created using
1410 * lower values of @p order appear first in @p
1411 * dev's list of children
1412 * @param name devclass name for new device or @c NULL if not
1414 * @param unit unit number for new device or @c -1 if not
1417 * @returns the new device
1420 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1425 PDEBUG(("%s at %s with order %u as unit %d",
1426 name, DEVICENAME(dev), order, unit));
1427 KASSERT(name != NULL || unit == -1,
1428 ("child device with wildcard name and specific unit number"));
1430 child = make_device(dev, name, unit);
1433 child->order = order;
1435 TAILQ_FOREACH(place, &dev->children, link) {
1436 if (place->order > order)
1442 * The device 'place' is the first device whose order is
1443 * greater than the new child.
1445 TAILQ_INSERT_BEFORE(place, child, link);
1448 * The new child's order is greater or equal to the order of
1449 * any existing device. Add the child to the tail of the list.
1451 TAILQ_INSERT_TAIL(&dev->children, child, link);
1454 bus_data_generation_update();
1459 * @brief Delete a device
1461 * This function deletes a device along with all of its children. If
1462 * the device currently has a driver attached to it, the device is
1463 * detached first using device_detach().
1465 * @param dev the parent device
1466 * @param child the device to delete
1469 * @retval non-zero a unit error code describing the error
1472 device_delete_child(device_t dev, device_t child)
1475 device_t grandchild;
1477 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1479 /* detach parent before deleting children, if any */
1480 if ((error = device_detach(child)) != 0)
1483 /* remove children second */
1484 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1485 error = device_delete_child(child, grandchild);
1490 if (child->devclass)
1491 devclass_delete_device(child->devclass, child);
1493 BUS_CHILD_DELETED(dev, child);
1494 TAILQ_REMOVE(&dev->children, child, link);
1495 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1496 kobj_delete((kobj_t) child, M_BUS);
1498 bus_data_generation_update();
1503 * @brief Delete all children devices of the given device, if any.
1505 * This function deletes all children devices of the given device, if
1506 * any, using the device_delete_child() function for each device it
1507 * finds. If a child device cannot be deleted, this function will
1508 * return an error code.
1510 * @param dev the parent device
1513 * @retval non-zero a device would not detach
1516 device_delete_children(device_t dev)
1521 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1525 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1526 error = device_delete_child(dev, child);
1528 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1536 * @brief Find a device given a unit number
1538 * This is similar to devclass_get_devices() but only searches for
1539 * devices which have @p dev as a parent.
1541 * @param dev the parent device to search
1542 * @param unit the unit number to search for. If the unit is -1,
1543 * return the first child of @p dev which has name
1544 * @p classname (that is, the one with the lowest unit.)
1546 * @returns the device with the given unit number or @c
1547 * NULL if there is no such device
1550 device_find_child(device_t dev, const char *classname, int unit)
1555 dc = devclass_find(classname);
1560 child = devclass_get_device(dc, unit);
1561 if (child && child->parent == dev)
1564 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1565 child = devclass_get_device(dc, unit);
1566 if (child && child->parent == dev)
1577 first_matching_driver(devclass_t dc, device_t dev)
1580 return (devclass_find_driver_internal(dc, dev->devclass->name));
1581 return (TAILQ_FIRST(&dc->drivers));
1588 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1590 if (dev->devclass) {
1592 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1593 if (!strcmp(dev->devclass->name, dl->driver->name))
1597 return (TAILQ_NEXT(last, link));
1604 device_probe_child(device_t dev, device_t child)
1607 driverlink_t best = NULL;
1609 int result, pri = 0;
1610 /* We should preserve the devclass (or lack of) set by the bus. */
1611 int hasclass = (child->devclass != NULL);
1617 panic("device_probe_child: parent device has no devclass");
1620 * If the state is already probed, then return.
1622 if (child->state == DS_ALIVE)
1625 for (; dc; dc = dc->parent) {
1626 for (dl = first_matching_driver(dc, child);
1628 dl = next_matching_driver(dc, child, dl)) {
1629 /* If this driver's pass is too high, then ignore it. */
1630 if (dl->pass > bus_current_pass)
1633 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1634 result = device_set_driver(child, dl->driver);
1635 if (result == ENOMEM)
1637 else if (result != 0)
1640 if (device_set_devclass(child,
1641 dl->driver->name) != 0) {
1642 char const * devname =
1643 device_get_name(child);
1644 if (devname == NULL)
1645 devname = "(unknown)";
1646 printf("driver bug: Unable to set "
1647 "devclass (class: %s "
1651 (void)device_set_driver(child, NULL);
1656 /* Fetch any flags for the device before probing. */
1657 resource_int_value(dl->driver->name, child->unit,
1658 "flags", &child->devflags);
1660 result = DEVICE_PROBE(child);
1663 * If the driver returns SUCCESS, there can be
1664 * no higher match for this device.
1672 /* Reset flags and devclass before the next probe. */
1673 child->devflags = 0;
1675 (void)device_set_devclass(child, NULL);
1678 * Reset DF_QUIET in case this driver doesn't
1679 * end up as the best driver.
1681 device_verbose(child);
1684 * Probes that return BUS_PROBE_NOWILDCARD or lower
1685 * only match on devices whose driver was explicitly
1688 if (result <= BUS_PROBE_NOWILDCARD &&
1689 !(child->flags & DF_FIXEDCLASS)) {
1694 * The driver returned an error so it
1695 * certainly doesn't match.
1698 (void)device_set_driver(child, NULL);
1703 * A priority lower than SUCCESS, remember the
1704 * best matching driver. Initialise the value
1705 * of pri for the first match.
1707 if (best == NULL || result > pri) {
1714 * If we have an unambiguous match in this devclass,
1715 * don't look in the parent.
1717 if (best && pri == 0)
1725 * If we found a driver, change state and initialise the devclass.
1728 /* Set the winning driver, devclass, and flags. */
1729 result = device_set_driver(child, best->driver);
1732 if (!child->devclass) {
1733 result = device_set_devclass(child, best->driver->name);
1735 (void)device_set_driver(child, NULL);
1739 resource_int_value(best->driver->name, child->unit,
1740 "flags", &child->devflags);
1743 * A bit bogus. Call the probe method again to make sure
1744 * that we have the right description.
1746 result = DEVICE_PROBE(child);
1749 (void)device_set_devclass(child, NULL);
1750 (void)device_set_driver(child, NULL);
1755 child->state = DS_ALIVE;
1756 bus_data_generation_update();
1761 * @brief Return the parent of a device
1764 device_get_parent(device_t dev)
1766 return (dev->parent);
1770 * @brief Get a list of children of a device
1772 * An array containing a list of all the children of the given device
1773 * is allocated and returned in @p *devlistp. The number of devices
1774 * in the array is returned in @p *devcountp. The caller should free
1775 * the array using @c free(p, M_TEMP).
1777 * @param dev the device to examine
1778 * @param devlistp points at location for array pointer return
1780 * @param devcountp points at location for array size return value
1783 * @retval ENOMEM the array allocation failed
1786 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1793 TAILQ_FOREACH(child, &dev->children, link) {
1802 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1807 TAILQ_FOREACH(child, &dev->children, link) {
1808 list[count] = child;
1819 * @brief Return the current driver for the device or @c NULL if there
1820 * is no driver currently attached
1823 device_get_driver(device_t dev)
1825 return (dev->driver);
1829 * @brief Return the current devclass for the device or @c NULL if
1833 device_get_devclass(device_t dev)
1835 return (dev->devclass);
1839 * @brief Return the name of the device's devclass or @c NULL if there
1843 device_get_name(device_t dev)
1845 if (dev != NULL && dev->devclass)
1846 return (devclass_get_name(dev->devclass));
1851 * @brief Return a string containing the device's devclass name
1852 * followed by an ascii representation of the device's unit number
1856 device_get_nameunit(device_t dev)
1858 return (dev->nameunit);
1862 * @brief Return the device's unit number.
1865 device_get_unit(device_t dev)
1871 * @brief Return the device's description string
1874 device_get_desc(device_t dev)
1880 * @brief Return the device's flags
1883 device_get_flags(device_t dev)
1885 return (dev->devflags);
1888 struct sysctl_ctx_list *
1889 device_get_sysctl_ctx(device_t dev)
1891 return (&dev->sysctl_ctx);
1895 device_get_sysctl_tree(device_t dev)
1897 return (dev->sysctl_tree);
1901 * @brief Print the name of the device followed by a colon and a space
1903 * @returns the number of characters printed
1906 device_print_prettyname(device_t dev)
1908 const char *name = device_get_name(dev);
1911 return (printf("unknown: "));
1912 return (printf("%s%d: ", name, device_get_unit(dev)));
1916 * @brief Print the name of the device followed by a colon, a space
1917 * and the result of calling vprintf() with the value of @p fmt and
1918 * the following arguments.
1920 * @returns the number of characters printed
1923 device_printf(device_t dev, const char * fmt, ...)
1933 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
1934 sbuf_set_drain(&sb, sbuf_printf_drain, &retval);
1936 name = device_get_name(dev);
1939 sbuf_cat(&sb, "unknown: ");
1941 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
1944 sbuf_vprintf(&sb, fmt, ap);
1954 * @brief Print the name of the device followed by a colon, a space
1955 * and the result of calling log() with the value of @p fmt and
1956 * the following arguments.
1958 * @returns the number of characters printed
1961 device_log(device_t dev, int pri, const char * fmt, ...)
1971 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
1973 name = device_get_name(dev);
1976 sbuf_cat(&sb, "unknown: ");
1978 sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
1981 sbuf_vprintf(&sb, fmt, ap);
1986 log(pri, "%.*s", (int) sbuf_len(&sb), sbuf_data(&sb));
1987 retval = sbuf_len(&sb);
1998 device_set_desc_internal(device_t dev, const char* desc, int copy)
2000 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2001 free(dev->desc, M_BUS);
2002 dev->flags &= ~DF_DESCMALLOCED;
2007 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2009 strcpy(dev->desc, desc);
2010 dev->flags |= DF_DESCMALLOCED;
2013 /* Avoid a -Wcast-qual warning */
2014 dev->desc = (char *)(uintptr_t) desc;
2017 bus_data_generation_update();
2021 * @brief Set the device's description
2023 * The value of @c desc should be a string constant that will not
2024 * change (at least until the description is changed in a subsequent
2025 * call to device_set_desc() or device_set_desc_copy()).
2028 device_set_desc(device_t dev, const char* desc)
2030 device_set_desc_internal(dev, desc, FALSE);
2034 * @brief Set the device's description
2036 * The string pointed to by @c desc is copied. Use this function if
2037 * the device description is generated, (e.g. with sprintf()).
2040 device_set_desc_copy(device_t dev, const char* desc)
2042 device_set_desc_internal(dev, desc, TRUE);
2046 * @brief Set the device's flags
2049 device_set_flags(device_t dev, uint32_t flags)
2051 dev->devflags = flags;
2055 * @brief Return the device's softc field
2057 * The softc is allocated and zeroed when a driver is attached, based
2058 * on the size field of the driver.
2061 device_get_softc(device_t dev)
2063 return (dev->softc);
2067 * @brief Set the device's softc field
2069 * Most drivers do not need to use this since the softc is allocated
2070 * automatically when the driver is attached.
2073 device_set_softc(device_t dev, void *softc)
2075 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2076 free(dev->softc, M_BUS_SC);
2079 dev->flags |= DF_EXTERNALSOFTC;
2081 dev->flags &= ~DF_EXTERNALSOFTC;
2085 * @brief Free claimed softc
2087 * Most drivers do not need to use this since the softc is freed
2088 * automatically when the driver is detached.
2091 device_free_softc(void *softc)
2093 free(softc, M_BUS_SC);
2097 * @brief Claim softc
2099 * This function can be used to let the driver free the automatically
2100 * allocated softc using "device_free_softc()". This function is
2101 * useful when the driver is refcounting the softc and the softc
2102 * cannot be freed when the "device_detach" method is called.
2105 device_claim_softc(device_t dev)
2108 dev->flags |= DF_EXTERNALSOFTC;
2110 dev->flags &= ~DF_EXTERNALSOFTC;
2114 * @brief Get the device's ivars field
2116 * The ivars field is used by the parent device to store per-device
2117 * state (e.g. the physical location of the device or a list of
2121 device_get_ivars(device_t dev)
2123 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2124 return (dev->ivars);
2128 * @brief Set the device's ivars field
2131 device_set_ivars(device_t dev, void * ivars)
2133 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2138 * @brief Return the device's state
2141 device_get_state(device_t dev)
2143 return (dev->state);
2147 * @brief Set the DF_ENABLED flag for the device
2150 device_enable(device_t dev)
2152 dev->flags |= DF_ENABLED;
2156 * @brief Clear the DF_ENABLED flag for the device
2159 device_disable(device_t dev)
2161 dev->flags &= ~DF_ENABLED;
2165 * @brief Increment the busy counter for the device
2168 device_busy(device_t dev)
2172 * Mark the device as busy, recursively up the tree if this busy count
2175 if (refcount_acquire(&dev->busy) == 0 && dev->parent != NULL)
2176 device_busy(dev->parent);
2180 * @brief Decrement the busy counter for the device
2183 device_unbusy(device_t dev)
2187 * Mark the device as unbsy, recursively if this is the last busy count.
2189 if (refcount_release(&dev->busy) && dev->parent != NULL)
2190 device_unbusy(dev->parent);
2194 * @brief Set the DF_QUIET flag for the device
2197 device_quiet(device_t dev)
2199 dev->flags |= DF_QUIET;
2203 * @brief Set the DF_QUIET_CHILDREN flag for the device
2206 device_quiet_children(device_t dev)
2208 dev->flags |= DF_QUIET_CHILDREN;
2212 * @brief Clear the DF_QUIET flag for the device
2215 device_verbose(device_t dev)
2217 dev->flags &= ~DF_QUIET;
2221 device_get_property(device_t dev, const char *prop, void *val, size_t sz,
2222 device_property_type_t type)
2224 device_t bus = device_get_parent(dev);
2227 case DEVICE_PROP_ANY:
2228 case DEVICE_PROP_BUFFER:
2229 case DEVICE_PROP_HANDLE: /* Size checks done in implementation. */
2231 case DEVICE_PROP_UINT32:
2235 case DEVICE_PROP_UINT64:
2243 return (BUS_GET_PROPERTY(bus, dev, prop, val, sz, type));
2247 device_has_property(device_t dev, const char *prop)
2249 return (device_get_property(dev, prop, NULL, 0, DEVICE_PROP_ANY) >= 0);
2253 * @brief Return non-zero if the DF_QUIET_CHIDLREN flag is set on the device
2256 device_has_quiet_children(device_t dev)
2258 return ((dev->flags & DF_QUIET_CHILDREN) != 0);
2262 * @brief Return non-zero if the DF_QUIET flag is set on the device
2265 device_is_quiet(device_t dev)
2267 return ((dev->flags & DF_QUIET) != 0);
2271 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2274 device_is_enabled(device_t dev)
2276 return ((dev->flags & DF_ENABLED) != 0);
2280 * @brief Return non-zero if the device was successfully probed
2283 device_is_alive(device_t dev)
2285 return (dev->state >= DS_ALIVE);
2289 * @brief Return non-zero if the device currently has a driver
2293 device_is_attached(device_t dev)
2295 return (dev->state >= DS_ATTACHED);
2299 * @brief Return non-zero if the device is currently suspended.
2302 device_is_suspended(device_t dev)
2304 return ((dev->flags & DF_SUSPENDED) != 0);
2308 * @brief Set the devclass of a device
2309 * @see devclass_add_device().
2312 device_set_devclass(device_t dev, const char *classname)
2319 devclass_delete_device(dev->devclass, dev);
2323 if (dev->devclass) {
2324 printf("device_set_devclass: device class already set\n");
2328 dc = devclass_find_internal(classname, NULL, TRUE);
2332 error = devclass_add_device(dc, dev);
2334 bus_data_generation_update();
2339 * @brief Set the devclass of a device and mark the devclass fixed.
2340 * @see device_set_devclass()
2343 device_set_devclass_fixed(device_t dev, const char *classname)
2347 if (classname == NULL)
2350 error = device_set_devclass(dev, classname);
2353 dev->flags |= DF_FIXEDCLASS;
2358 * @brief Query the device to determine if it's of a fixed devclass
2359 * @see device_set_devclass_fixed()
2362 device_is_devclass_fixed(device_t dev)
2364 return ((dev->flags & DF_FIXEDCLASS) != 0);
2368 * @brief Set the driver of a device
2371 * @retval EBUSY the device already has a driver attached
2372 * @retval ENOMEM a memory allocation failure occurred
2375 device_set_driver(device_t dev, driver_t *driver)
2378 struct domainset *policy;
2380 if (dev->state >= DS_ATTACHED)
2383 if (dev->driver == driver)
2386 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2387 free(dev->softc, M_BUS_SC);
2390 device_set_desc(dev, NULL);
2391 kobj_delete((kobj_t) dev, NULL);
2392 dev->driver = driver;
2394 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2395 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2396 if (bus_get_domain(dev, &domain) == 0)
2397 policy = DOMAINSET_PREF(domain);
2399 policy = DOMAINSET_RR();
2400 dev->softc = malloc_domainset(driver->size, M_BUS_SC,
2401 policy, M_NOWAIT | M_ZERO);
2403 kobj_delete((kobj_t) dev, NULL);
2404 kobj_init((kobj_t) dev, &null_class);
2410 kobj_init((kobj_t) dev, &null_class);
2413 bus_data_generation_update();
2418 * @brief Probe a device, and return this status.
2420 * This function is the core of the device autoconfiguration
2421 * system. Its purpose is to select a suitable driver for a device and
2422 * then call that driver to initialise the hardware appropriately. The
2423 * driver is selected by calling the DEVICE_PROBE() method of a set of
2424 * candidate drivers and then choosing the driver which returned the
2425 * best value. This driver is then attached to the device using
2428 * The set of suitable drivers is taken from the list of drivers in
2429 * the parent device's devclass. If the device was originally created
2430 * with a specific class name (see device_add_child()), only drivers
2431 * with that name are probed, otherwise all drivers in the devclass
2432 * are probed. If no drivers return successful probe values in the
2433 * parent devclass, the search continues in the parent of that
2434 * devclass (see devclass_get_parent()) if any.
2436 * @param dev the device to initialise
2439 * @retval ENXIO no driver was found
2440 * @retval ENOMEM memory allocation failure
2441 * @retval non-zero some other unix error code
2442 * @retval -1 Device already attached
2445 device_probe(device_t dev)
2451 if (dev->state >= DS_ALIVE)
2454 if (!(dev->flags & DF_ENABLED)) {
2455 if (bootverbose && device_get_name(dev) != NULL) {
2456 device_print_prettyname(dev);
2457 printf("not probed (disabled)\n");
2461 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2462 if (bus_current_pass == BUS_PASS_DEFAULT &&
2463 !(dev->flags & DF_DONENOMATCH)) {
2464 device_handle_nomatch(dev);
2472 * @brief Probe a device and attach a driver if possible
2474 * calls device_probe() and attaches if that was successful.
2477 device_probe_and_attach(device_t dev)
2483 error = device_probe(dev);
2486 else if (error != 0)
2489 CURVNET_SET_QUIET(vnet0);
2490 error = device_attach(dev);
2496 * @brief Attach a device driver to a device
2498 * This function is a wrapper around the DEVICE_ATTACH() driver
2499 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2500 * device's sysctl tree, optionally prints a description of the device
2501 * and queues a notification event for user-based device management
2504 * Normally this function is only called internally from
2505 * device_probe_and_attach().
2507 * @param dev the device to initialise
2510 * @retval ENXIO no driver was found
2511 * @retval ENOMEM memory allocation failure
2512 * @retval non-zero some other unix error code
2515 device_attach(device_t dev)
2517 uint64_t attachtime;
2518 uint16_t attachentropy;
2521 if (resource_disabled(dev->driver->name, dev->unit)) {
2522 device_disable(dev);
2524 device_printf(dev, "disabled via hints entry\n");
2528 device_sysctl_init(dev);
2529 if (!device_is_quiet(dev))
2530 device_print_child(dev->parent, dev);
2531 attachtime = get_cyclecount();
2532 dev->state = DS_ATTACHING;
2533 if ((error = DEVICE_ATTACH(dev)) != 0) {
2534 printf("device_attach: %s%d attach returned %d\n",
2535 dev->driver->name, dev->unit, error);
2536 if (!(dev->flags & DF_FIXEDCLASS))
2537 devclass_delete_device(dev->devclass, dev);
2538 (void)device_set_driver(dev, NULL);
2539 device_sysctl_fini(dev);
2540 KASSERT(dev->busy == 0, ("attach failed but busy"));
2541 dev->state = DS_NOTPRESENT;
2544 dev->flags |= DF_ATTACHED_ONCE;
2545 /* We only need the low bits of this time, but ranges from tens to thousands
2546 * have been seen, so keep 2 bytes' worth.
2548 attachentropy = (uint16_t)(get_cyclecount() - attachtime);
2549 random_harvest_direct(&attachentropy, sizeof(attachentropy), RANDOM_ATTACH);
2550 device_sysctl_update(dev);
2551 dev->state = DS_ATTACHED;
2552 dev->flags &= ~DF_DONENOMATCH;
2553 EVENTHANDLER_DIRECT_INVOKE(device_attach, dev);
2558 * @brief Detach a driver from a device
2560 * This function is a wrapper around the DEVICE_DETACH() driver
2561 * method. If the call to DEVICE_DETACH() succeeds, it calls
2562 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2563 * notification event for user-based device management services and
2564 * cleans up the device's sysctl tree.
2566 * @param dev the device to un-initialise
2569 * @retval ENXIO no driver was found
2570 * @retval ENOMEM memory allocation failure
2571 * @retval non-zero some other unix error code
2574 device_detach(device_t dev)
2580 PDEBUG(("%s", DEVICENAME(dev)));
2583 if (dev->state == DS_ATTACHING) {
2584 device_printf(dev, "device in attaching state! Deferring detach.\n");
2587 if (dev->state != DS_ATTACHED)
2590 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev, EVHDEV_DETACH_BEGIN);
2591 if ((error = DEVICE_DETACH(dev)) != 0) {
2592 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
2593 EVHDEV_DETACH_FAILED);
2596 EVENTHANDLER_DIRECT_INVOKE(device_detach, dev,
2597 EVHDEV_DETACH_COMPLETE);
2599 if (!device_is_quiet(dev))
2600 device_printf(dev, "detached\n");
2602 BUS_CHILD_DETACHED(dev->parent, dev);
2604 if (!(dev->flags & DF_FIXEDCLASS))
2605 devclass_delete_device(dev->devclass, dev);
2607 device_verbose(dev);
2608 dev->state = DS_NOTPRESENT;
2609 (void)device_set_driver(dev, NULL);
2610 device_sysctl_fini(dev);
2616 * @brief Tells a driver to quiesce itself.
2618 * This function is a wrapper around the DEVICE_QUIESCE() driver
2619 * method. If the call to DEVICE_QUIESCE() succeeds.
2621 * @param dev the device to quiesce
2624 * @retval ENXIO no driver was found
2625 * @retval ENOMEM memory allocation failure
2626 * @retval non-zero some other unix error code
2629 device_quiesce(device_t dev)
2631 PDEBUG(("%s", DEVICENAME(dev)));
2634 if (dev->state != DS_ATTACHED)
2637 return (DEVICE_QUIESCE(dev));
2641 * @brief Notify a device of system shutdown
2643 * This function calls the DEVICE_SHUTDOWN() driver method if the
2644 * device currently has an attached driver.
2646 * @returns the value returned by DEVICE_SHUTDOWN()
2649 device_shutdown(device_t dev)
2651 if (dev->state < DS_ATTACHED)
2653 return (DEVICE_SHUTDOWN(dev));
2657 * @brief Set the unit number of a device
2659 * This function can be used to override the unit number used for a
2660 * device (e.g. to wire a device to a pre-configured unit number).
2663 device_set_unit(device_t dev, int unit)
2668 if (unit == dev->unit)
2670 dc = device_get_devclass(dev);
2671 if (unit < dc->maxunit && dc->devices[unit])
2673 err = devclass_delete_device(dc, dev);
2677 err = devclass_add_device(dc, dev);
2681 bus_data_generation_update();
2685 /*======================================*/
2687 * Some useful method implementations to make life easier for bus drivers.
2691 resource_init_map_request_impl(struct resource_map_request *args, size_t sz)
2695 args->memattr = VM_MEMATTR_DEVICE;
2699 * @brief Initialise a resource list.
2701 * @param rl the resource list to initialise
2704 resource_list_init(struct resource_list *rl)
2710 * @brief Reclaim memory used by a resource list.
2712 * This function frees the memory for all resource entries on the list
2715 * @param rl the resource list to free
2718 resource_list_free(struct resource_list *rl)
2720 struct resource_list_entry *rle;
2722 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2724 panic("resource_list_free: resource entry is busy");
2725 STAILQ_REMOVE_HEAD(rl, link);
2731 * @brief Add a resource entry.
2733 * This function adds a resource entry using the given @p type, @p
2734 * start, @p end and @p count values. A rid value is chosen by
2735 * searching sequentially for the first unused rid starting at zero.
2737 * @param rl the resource list to edit
2738 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2739 * @param start the start address of the resource
2740 * @param end the end address of the resource
2741 * @param count XXX end-start+1
2744 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start,
2745 rman_res_t end, rman_res_t count)
2750 while (resource_list_find(rl, type, rid) != NULL)
2752 resource_list_add(rl, type, rid, start, end, count);
2757 * @brief Add or modify a resource entry.
2759 * If an existing entry exists with the same type and rid, it will be
2760 * modified using the given values of @p start, @p end and @p
2761 * count. If no entry exists, a new one will be created using the
2762 * given values. The resource list entry that matches is then returned.
2764 * @param rl the resource list to edit
2765 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2766 * @param rid the resource identifier
2767 * @param start the start address of the resource
2768 * @param end the end address of the resource
2769 * @param count XXX end-start+1
2771 struct resource_list_entry *
2772 resource_list_add(struct resource_list *rl, int type, int rid,
2773 rman_res_t start, rman_res_t end, rman_res_t count)
2775 struct resource_list_entry *rle;
2777 rle = resource_list_find(rl, type, rid);
2779 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2782 panic("resource_list_add: can't record entry");
2783 STAILQ_INSERT_TAIL(rl, rle, link);
2791 panic("resource_list_add: resource entry is busy");
2800 * @brief Determine if a resource entry is busy.
2802 * Returns true if a resource entry is busy meaning that it has an
2803 * associated resource that is not an unallocated "reserved" resource.
2805 * @param rl the resource list to search
2806 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2807 * @param rid the resource identifier
2809 * @returns Non-zero if the entry is busy, zero otherwise.
2812 resource_list_busy(struct resource_list *rl, int type, int rid)
2814 struct resource_list_entry *rle;
2816 rle = resource_list_find(rl, type, rid);
2817 if (rle == NULL || rle->res == NULL)
2819 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
2820 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
2821 ("reserved resource is active"));
2828 * @brief Determine if a resource entry is reserved.
2830 * Returns true if a resource entry is reserved meaning that it has an
2831 * associated "reserved" resource. The resource can either be
2832 * allocated or unallocated.
2834 * @param rl the resource list to search
2835 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2836 * @param rid the resource identifier
2838 * @returns Non-zero if the entry is reserved, zero otherwise.
2841 resource_list_reserved(struct resource_list *rl, int type, int rid)
2843 struct resource_list_entry *rle;
2845 rle = resource_list_find(rl, type, rid);
2846 if (rle != NULL && rle->flags & RLE_RESERVED)
2852 * @brief Find a resource entry by type and rid.
2854 * @param rl the resource list to search
2855 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2856 * @param rid the resource identifier
2858 * @returns the resource entry pointer or NULL if there is no such
2861 struct resource_list_entry *
2862 resource_list_find(struct resource_list *rl, int type, int rid)
2864 struct resource_list_entry *rle;
2866 STAILQ_FOREACH(rle, rl, link) {
2867 if (rle->type == type && rle->rid == rid)
2874 * @brief Delete a resource entry.
2876 * @param rl the resource list to edit
2877 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2878 * @param rid the resource identifier
2881 resource_list_delete(struct resource_list *rl, int type, int rid)
2883 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2886 if (rle->res != NULL)
2887 panic("resource_list_delete: resource has not been released");
2888 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
2894 * @brief Allocate a reserved resource
2896 * This can be used by buses to force the allocation of resources
2897 * that are always active in the system even if they are not allocated
2898 * by a driver (e.g. PCI BARs). This function is usually called when
2899 * adding a new child to the bus. The resource is allocated from the
2900 * parent bus when it is reserved. The resource list entry is marked
2901 * with RLE_RESERVED to note that it is a reserved resource.
2903 * Subsequent attempts to allocate the resource with
2904 * resource_list_alloc() will succeed the first time and will set
2905 * RLE_ALLOCATED to note that it has been allocated. When a reserved
2906 * resource that has been allocated is released with
2907 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
2908 * the actual resource remains allocated. The resource can be released to
2909 * the parent bus by calling resource_list_unreserve().
2911 * @param rl the resource list to allocate from
2912 * @param bus the parent device of @p child
2913 * @param child the device for which the resource is being reserved
2914 * @param type the type of resource to allocate
2915 * @param rid a pointer to the resource identifier
2916 * @param start hint at the start of the resource range - pass
2917 * @c 0 for any start address
2918 * @param end hint at the end of the resource range - pass
2919 * @c ~0 for any end address
2920 * @param count hint at the size of range required - pass @c 1
2922 * @param flags any extra flags to control the resource
2923 * allocation - see @c RF_XXX flags in
2924 * <sys/rman.h> for details
2926 * @returns the resource which was allocated or @c NULL if no
2927 * resource could be allocated
2930 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
2931 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
2933 struct resource_list_entry *rle = NULL;
2934 int passthrough = (device_get_parent(child) != bus);
2939 "resource_list_reserve() should only be called for direct children");
2940 if (flags & RF_ACTIVE)
2942 "resource_list_reserve() should only reserve inactive resources");
2944 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
2947 rle = resource_list_find(rl, type, *rid);
2948 rle->flags |= RLE_RESERVED;
2954 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
2956 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
2957 * and passing the allocation up to the parent of @p bus. This assumes
2958 * that the first entry of @c device_get_ivars(child) is a struct
2959 * resource_list. This also handles 'passthrough' allocations where a
2960 * child is a remote descendant of bus by passing the allocation up to
2961 * the parent of bus.
2963 * Typically, a bus driver would store a list of child resources
2964 * somewhere in the child device's ivars (see device_get_ivars()) and
2965 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
2966 * then call resource_list_alloc() to perform the allocation.
2968 * @param rl the resource list to allocate from
2969 * @param bus the parent device of @p child
2970 * @param child the device which is requesting an allocation
2971 * @param type the type of resource to allocate
2972 * @param rid a pointer to the resource identifier
2973 * @param start hint at the start of the resource range - pass
2974 * @c 0 for any start address
2975 * @param end hint at the end of the resource range - pass
2976 * @c ~0 for any end address
2977 * @param count hint at the size of range required - pass @c 1
2979 * @param flags any extra flags to control the resource
2980 * allocation - see @c RF_XXX flags in
2981 * <sys/rman.h> for details
2983 * @returns the resource which was allocated or @c NULL if no
2984 * resource could be allocated
2987 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
2988 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
2990 struct resource_list_entry *rle = NULL;
2991 int passthrough = (device_get_parent(child) != bus);
2992 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
2995 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2996 type, rid, start, end, count, flags));
2999 rle = resource_list_find(rl, type, *rid);
3002 return (NULL); /* no resource of that type/rid */
3005 if (rle->flags & RLE_RESERVED) {
3006 if (rle->flags & RLE_ALLOCATED)
3008 if ((flags & RF_ACTIVE) &&
3009 bus_activate_resource(child, type, *rid,
3012 rle->flags |= RLE_ALLOCATED;
3016 "resource entry %#x type %d for child %s is busy\n", *rid,
3017 type, device_get_nameunit(child));
3023 count = ulmax(count, rle->count);
3024 end = ulmax(rle->end, start + count - 1);
3027 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3028 type, rid, start, end, count, flags);
3031 * Record the new range.
3034 rle->start = rman_get_start(rle->res);
3035 rle->end = rman_get_end(rle->res);
3043 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3045 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3046 * used with resource_list_alloc().
3048 * @param rl the resource list which was allocated from
3049 * @param bus the parent device of @p child
3050 * @param child the device which is requesting a release
3051 * @param type the type of resource to release
3052 * @param rid the resource identifier
3053 * @param res the resource to release
3056 * @retval non-zero a standard unix error code indicating what
3057 * error condition prevented the operation
3060 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3061 int type, int rid, struct resource *res)
3063 struct resource_list_entry *rle = NULL;
3064 int passthrough = (device_get_parent(child) != bus);
3068 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3072 rle = resource_list_find(rl, type, rid);
3075 panic("resource_list_release: can't find resource");
3077 panic("resource_list_release: resource entry is not busy");
3078 if (rle->flags & RLE_RESERVED) {
3079 if (rle->flags & RLE_ALLOCATED) {
3080 if (rman_get_flags(res) & RF_ACTIVE) {
3081 error = bus_deactivate_resource(child, type,
3086 rle->flags &= ~RLE_ALLOCATED;
3092 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3102 * @brief Release all active resources of a given type
3104 * Release all active resources of a specified type. This is intended
3105 * to be used to cleanup resources leaked by a driver after detach or
3108 * @param rl the resource list which was allocated from
3109 * @param bus the parent device of @p child
3110 * @param child the device whose active resources are being released
3111 * @param type the type of resources to release
3114 * @retval EBUSY at least one resource was active
3117 resource_list_release_active(struct resource_list *rl, device_t bus,
3118 device_t child, int type)
3120 struct resource_list_entry *rle;
3124 STAILQ_FOREACH(rle, rl, link) {
3125 if (rle->type != type)
3127 if (rle->res == NULL)
3129 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3133 error = resource_list_release(rl, bus, child, type,
3134 rman_get_rid(rle->res), rle->res);
3137 "Failed to release active resource: %d\n", error);
3143 * @brief Fully release a reserved resource
3145 * Fully releases a resource reserved via resource_list_reserve().
3147 * @param rl the resource list which was allocated from
3148 * @param bus the parent device of @p child
3149 * @param child the device whose reserved resource is being released
3150 * @param type the type of resource to release
3151 * @param rid the resource identifier
3152 * @param res the resource to release
3155 * @retval non-zero a standard unix error code indicating what
3156 * error condition prevented the operation
3159 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3162 struct resource_list_entry *rle = NULL;
3163 int passthrough = (device_get_parent(child) != bus);
3167 "resource_list_unreserve() should only be called for direct children");
3169 rle = resource_list_find(rl, type, rid);
3172 panic("resource_list_unreserve: can't find resource");
3173 if (!(rle->flags & RLE_RESERVED))
3175 if (rle->flags & RLE_ALLOCATED)
3177 rle->flags &= ~RLE_RESERVED;
3178 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3182 * @brief Print a description of resources in a resource list
3184 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3185 * The name is printed if at least one resource of the given type is available.
3186 * The format is used to print resource start and end.
3188 * @param rl the resource list to print
3189 * @param name the name of @p type, e.g. @c "memory"
3190 * @param type type type of resource entry to print
3191 * @param format printf(9) format string to print resource
3192 * start and end values
3194 * @returns the number of characters printed
3197 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3200 struct resource_list_entry *rle;
3201 int printed, retval;
3205 /* Yes, this is kinda cheating */
3206 STAILQ_FOREACH(rle, rl, link) {
3207 if (rle->type == type) {
3209 retval += printf(" %s ", name);
3211 retval += printf(",");
3213 retval += printf(format, rle->start);
3214 if (rle->count > 1) {
3215 retval += printf("-");
3216 retval += printf(format, rle->start +
3225 * @brief Releases all the resources in a list.
3227 * @param rl The resource list to purge.
3232 resource_list_purge(struct resource_list *rl)
3234 struct resource_list_entry *rle;
3236 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3238 bus_release_resource(rman_get_device(rle->res),
3239 rle->type, rle->rid, rle->res);
3240 STAILQ_REMOVE_HEAD(rl, link);
3246 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3248 return (device_add_child_ordered(dev, order, name, unit));
3252 * @brief Helper function for implementing DEVICE_PROBE()
3254 * This function can be used to help implement the DEVICE_PROBE() for
3255 * a bus (i.e. a device which has other devices attached to it). It
3256 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3260 bus_generic_probe(device_t dev)
3262 devclass_t dc = dev->devclass;
3265 TAILQ_FOREACH(dl, &dc->drivers, link) {
3267 * If this driver's pass is too high, then ignore it.
3268 * For most drivers in the default pass, this will
3269 * never be true. For early-pass drivers they will
3270 * only call the identify routines of eligible drivers
3271 * when this routine is called. Drivers for later
3272 * passes should have their identify routines called
3273 * on early-pass buses during BUS_NEW_PASS().
3275 if (dl->pass > bus_current_pass)
3277 DEVICE_IDENTIFY(dl->driver, dev);
3284 * @brief Helper function for implementing DEVICE_ATTACH()
3286 * This function can be used to help implement the DEVICE_ATTACH() for
3287 * a bus. It calls device_probe_and_attach() for each of the device's
3291 bus_generic_attach(device_t dev)
3295 TAILQ_FOREACH(child, &dev->children, link) {
3296 device_probe_and_attach(child);
3303 * @brief Helper function for delaying attaching children
3305 * Many buses can't run transactions on the bus which children need to probe and
3306 * attach until after interrupts and/or timers are running. This function
3307 * delays their attach until interrupts and timers are enabled.
3310 bus_delayed_attach_children(device_t dev)
3312 /* Probe and attach the bus children when interrupts are available */
3313 config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
3319 * @brief Helper function for implementing DEVICE_DETACH()
3321 * This function can be used to help implement the DEVICE_DETACH() for
3322 * a bus. It calls device_detach() for each of the device's
3326 bus_generic_detach(device_t dev)
3331 if (dev->state != DS_ATTACHED)
3335 * Detach children in the reverse order.
3336 * See bus_generic_suspend for details.
3338 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3339 if ((error = device_detach(child)) != 0)
3347 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3349 * This function can be used to help implement the DEVICE_SHUTDOWN()
3350 * for a bus. It calls device_shutdown() for each of the device's
3354 bus_generic_shutdown(device_t dev)
3359 * Shut down children in the reverse order.
3360 * See bus_generic_suspend for details.
3362 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3363 device_shutdown(child);
3370 * @brief Default function for suspending a child device.
3372 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3375 bus_generic_suspend_child(device_t dev, device_t child)
3379 error = DEVICE_SUSPEND(child);
3382 child->flags |= DF_SUSPENDED;
3388 * @brief Default function for resuming a child device.
3390 * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3393 bus_generic_resume_child(device_t dev, device_t child)
3395 DEVICE_RESUME(child);
3396 child->flags &= ~DF_SUSPENDED;
3402 * @brief Helper function for implementing DEVICE_SUSPEND()
3404 * This function can be used to help implement the DEVICE_SUSPEND()
3405 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3406 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3407 * operation is aborted and any devices which were suspended are
3408 * resumed immediately by calling their DEVICE_RESUME() methods.
3411 bus_generic_suspend(device_t dev)
3417 * Suspend children in the reverse order.
3418 * For most buses all children are equal, so the order does not matter.
3419 * Other buses, such as acpi, carefully order their child devices to
3420 * express implicit dependencies between them. For such buses it is
3421 * safer to bring down devices in the reverse order.
3423 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3424 error = BUS_SUSPEND_CHILD(dev, child);
3426 child = TAILQ_NEXT(child, link);
3427 if (child != NULL) {
3428 TAILQ_FOREACH_FROM(child, &dev->children, link)
3429 BUS_RESUME_CHILD(dev, child);
3438 * @brief Helper function for implementing DEVICE_RESUME()
3440 * This function can be used to help implement the DEVICE_RESUME() for
3441 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3444 bus_generic_resume(device_t dev)
3448 TAILQ_FOREACH(child, &dev->children, link) {
3449 BUS_RESUME_CHILD(dev, child);
3450 /* if resume fails, there's nothing we can usefully do... */
3456 * @brief Helper function for implementing BUS_RESET_POST
3458 * Bus can use this function to implement common operations of
3459 * re-attaching or resuming the children after the bus itself was
3460 * reset, and after restoring bus-unique state of children.
3462 * @param dev The bus
3463 * #param flags DEVF_RESET_*
3466 bus_helper_reset_post(device_t dev, int flags)
3472 TAILQ_FOREACH(child, &dev->children,link) {
3473 BUS_RESET_POST(dev, child);
3474 error1 = (flags & DEVF_RESET_DETACH) != 0 ?
3475 device_probe_and_attach(child) :
3476 BUS_RESUME_CHILD(dev, child);
3477 if (error == 0 && error1 != 0)
3484 bus_helper_reset_prepare_rollback(device_t dev, device_t child, int flags)
3486 child = TAILQ_NEXT(child, link);
3489 TAILQ_FOREACH_FROM(child, &dev->children,link) {
3490 BUS_RESET_POST(dev, child);
3491 if ((flags & DEVF_RESET_DETACH) != 0)
3492 device_probe_and_attach(child);
3494 BUS_RESUME_CHILD(dev, child);
3499 * @brief Helper function for implementing BUS_RESET_PREPARE
3501 * Bus can use this function to implement common operations of
3502 * detaching or suspending the children before the bus itself is
3503 * reset, and then save bus-unique state of children that must
3504 * persists around reset.
3506 * @param dev The bus
3507 * #param flags DEVF_RESET_*
3510 bus_helper_reset_prepare(device_t dev, int flags)
3515 if (dev->state != DS_ATTACHED)
3518 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) {
3519 if ((flags & DEVF_RESET_DETACH) != 0) {
3520 error = device_get_state(child) == DS_ATTACHED ?
3521 device_detach(child) : 0;
3523 error = BUS_SUSPEND_CHILD(dev, child);
3526 error = BUS_RESET_PREPARE(dev, child);
3528 if ((flags & DEVF_RESET_DETACH) != 0)
3529 device_probe_and_attach(child);
3531 BUS_RESUME_CHILD(dev, child);
3535 bus_helper_reset_prepare_rollback(dev, child, flags);
3543 * @brief Helper function for implementing BUS_PRINT_CHILD().
3545 * This function prints the first part of the ascii representation of
3546 * @p child, including its name, unit and description (if any - see
3547 * device_set_desc()).
3549 * @returns the number of characters printed
3552 bus_print_child_header(device_t dev, device_t child)
3556 if (device_get_desc(child)) {
3557 retval += device_printf(child, "<%s>", device_get_desc(child));
3559 retval += printf("%s", device_get_nameunit(child));
3566 * @brief Helper function for implementing BUS_PRINT_CHILD().
3568 * This function prints the last part of the ascii representation of
3569 * @p child, which consists of the string @c " on " followed by the
3570 * name and unit of the @p dev.
3572 * @returns the number of characters printed
3575 bus_print_child_footer(device_t dev, device_t child)
3577 return (printf(" on %s\n", device_get_nameunit(dev)));
3581 * @brief Helper function for implementing BUS_PRINT_CHILD().
3583 * This function prints out the VM domain for the given device.
3585 * @returns the number of characters printed
3588 bus_print_child_domain(device_t dev, device_t child)
3592 /* No domain? Don't print anything */
3593 if (BUS_GET_DOMAIN(dev, child, &domain) != 0)
3596 return (printf(" numa-domain %d", domain));
3600 * @brief Helper function for implementing BUS_PRINT_CHILD().
3602 * This function simply calls bus_print_child_header() followed by
3603 * bus_print_child_footer().
3605 * @returns the number of characters printed
3608 bus_generic_print_child(device_t dev, device_t child)
3612 retval += bus_print_child_header(dev, child);
3613 retval += bus_print_child_domain(dev, child);
3614 retval += bus_print_child_footer(dev, child);
3620 * @brief Stub function for implementing BUS_READ_IVAR().
3625 bus_generic_read_ivar(device_t dev, device_t child, int index,
3632 * @brief Stub function for implementing BUS_WRITE_IVAR().
3637 bus_generic_write_ivar(device_t dev, device_t child, int index,
3644 * @brief Helper function for implementing BUS_GET_PROPERTY().
3646 * This simply calls the BUS_GET_PROPERTY of the parent of dev,
3647 * until a non-default implementation is found.
3650 bus_generic_get_property(device_t dev, device_t child, const char *propname,
3651 void *propvalue, size_t size, device_property_type_t type)
3653 if (device_get_parent(dev) != NULL)
3654 return (BUS_GET_PROPERTY(device_get_parent(dev), child,
3655 propname, propvalue, size, type));
3661 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3665 struct resource_list *
3666 bus_generic_get_resource_list(device_t dev, device_t child)
3672 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3674 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3675 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3676 * and then calls device_probe_and_attach() for each unattached child.
3679 bus_generic_driver_added(device_t dev, driver_t *driver)
3683 DEVICE_IDENTIFY(driver, dev);
3684 TAILQ_FOREACH(child, &dev->children, link) {
3685 if (child->state == DS_NOTPRESENT)
3686 device_probe_and_attach(child);
3691 * @brief Helper function for implementing BUS_NEW_PASS().
3693 * This implementing of BUS_NEW_PASS() first calls the identify
3694 * routines for any drivers that probe at the current pass. Then it
3695 * walks the list of devices for this bus. If a device is already
3696 * attached, then it calls BUS_NEW_PASS() on that device. If the
3697 * device is not already attached, it attempts to attach a driver to
3701 bus_generic_new_pass(device_t dev)
3708 TAILQ_FOREACH(dl, &dc->drivers, link) {
3709 if (dl->pass == bus_current_pass)
3710 DEVICE_IDENTIFY(dl->driver, dev);
3712 TAILQ_FOREACH(child, &dev->children, link) {
3713 if (child->state >= DS_ATTACHED)
3714 BUS_NEW_PASS(child);
3715 else if (child->state == DS_NOTPRESENT)
3716 device_probe_and_attach(child);
3721 * @brief Helper function for implementing BUS_SETUP_INTR().
3723 * This simple implementation of BUS_SETUP_INTR() simply calls the
3724 * BUS_SETUP_INTR() method of the parent of @p dev.
3727 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3728 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3731 /* Propagate up the bus hierarchy until someone handles it. */
3733 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3734 filter, intr, arg, cookiep));
3739 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3741 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3742 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3745 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3748 /* Propagate up the bus hierarchy until someone handles it. */
3750 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3755 * @brief Helper function for implementing BUS_SUSPEND_INTR().
3757 * This simple implementation of BUS_SUSPEND_INTR() simply calls the
3758 * BUS_SUSPEND_INTR() method of the parent of @p dev.
3761 bus_generic_suspend_intr(device_t dev, device_t child, struct resource *irq)
3763 /* Propagate up the bus hierarchy until someone handles it. */
3765 return (BUS_SUSPEND_INTR(dev->parent, child, irq));
3770 * @brief Helper function for implementing BUS_RESUME_INTR().
3772 * This simple implementation of BUS_RESUME_INTR() simply calls the
3773 * BUS_RESUME_INTR() method of the parent of @p dev.
3776 bus_generic_resume_intr(device_t dev, device_t child, struct resource *irq)
3778 /* Propagate up the bus hierarchy until someone handles it. */
3780 return (BUS_RESUME_INTR(dev->parent, child, irq));
3785 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3787 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3788 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3791 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3792 struct resource *r, rman_res_t start, rman_res_t end)
3794 /* Propagate up the bus hierarchy until someone handles it. */
3796 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3802 * @brief Helper function for implementing BUS_TRANSLATE_RESOURCE().
3804 * This simple implementation of BUS_TRANSLATE_RESOURCE() simply calls the
3805 * BUS_TRANSLATE_RESOURCE() method of the parent of @p dev. If there is no
3806 * parent, no translation happens.
3809 bus_generic_translate_resource(device_t dev, int type, rman_res_t start,
3810 rman_res_t *newstart)
3813 return (BUS_TRANSLATE_RESOURCE(dev->parent, type, start,
3820 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3822 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3823 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3826 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3827 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3829 /* Propagate up the bus hierarchy until someone handles it. */
3831 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3832 start, end, count, flags));
3837 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3839 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3840 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3843 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3846 /* Propagate up the bus hierarchy until someone handles it. */
3848 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3854 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3856 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3857 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3860 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3863 /* Propagate up the bus hierarchy until someone handles it. */
3865 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3871 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3873 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3874 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3877 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3878 int rid, struct resource *r)
3880 /* Propagate up the bus hierarchy until someone handles it. */
3882 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3888 * @brief Helper function for implementing BUS_MAP_RESOURCE().
3890 * This simple implementation of BUS_MAP_RESOURCE() simply calls the
3891 * BUS_MAP_RESOURCE() method of the parent of @p dev.
3894 bus_generic_map_resource(device_t dev, device_t child, int type,
3895 struct resource *r, struct resource_map_request *args,
3896 struct resource_map *map)
3898 /* Propagate up the bus hierarchy until someone handles it. */
3900 return (BUS_MAP_RESOURCE(dev->parent, child, type, r, args,
3906 * @brief Helper function for implementing BUS_UNMAP_RESOURCE().
3908 * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the
3909 * BUS_UNMAP_RESOURCE() method of the parent of @p dev.
3912 bus_generic_unmap_resource(device_t dev, device_t child, int type,
3913 struct resource *r, struct resource_map *map)
3915 /* Propagate up the bus hierarchy until someone handles it. */
3917 return (BUS_UNMAP_RESOURCE(dev->parent, child, type, r, map));
3922 * @brief Helper function for implementing BUS_BIND_INTR().
3924 * This simple implementation of BUS_BIND_INTR() simply calls the
3925 * BUS_BIND_INTR() method of the parent of @p dev.
3928 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3931 /* Propagate up the bus hierarchy until someone handles it. */
3933 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3938 * @brief Helper function for implementing BUS_CONFIG_INTR().
3940 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3941 * BUS_CONFIG_INTR() method of the parent of @p dev.
3944 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3945 enum intr_polarity pol)
3947 /* Propagate up the bus hierarchy until someone handles it. */
3949 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3954 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
3956 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
3957 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
3960 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
3961 void *cookie, const char *descr)
3963 /* Propagate up the bus hierarchy until someone handles it. */
3965 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
3971 * @brief Helper function for implementing BUS_GET_CPUS().
3973 * This simple implementation of BUS_GET_CPUS() simply calls the
3974 * BUS_GET_CPUS() method of the parent of @p dev.
3977 bus_generic_get_cpus(device_t dev, device_t child, enum cpu_sets op,
3978 size_t setsize, cpuset_t *cpuset)
3980 /* Propagate up the bus hierarchy until someone handles it. */
3981 if (dev->parent != NULL)
3982 return (BUS_GET_CPUS(dev->parent, child, op, setsize, cpuset));
3987 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3989 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3990 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3993 bus_generic_get_dma_tag(device_t dev, device_t child)
3995 /* Propagate up the bus hierarchy until someone handles it. */
3996 if (dev->parent != NULL)
3997 return (BUS_GET_DMA_TAG(dev->parent, child));
4002 * @brief Helper function for implementing BUS_GET_BUS_TAG().
4004 * This simple implementation of BUS_GET_BUS_TAG() simply calls the
4005 * BUS_GET_BUS_TAG() method of the parent of @p dev.
4008 bus_generic_get_bus_tag(device_t dev, device_t child)
4010 /* Propagate up the bus hierarchy until someone handles it. */
4011 if (dev->parent != NULL)
4012 return (BUS_GET_BUS_TAG(dev->parent, child));
4013 return ((bus_space_tag_t)0);
4017 * @brief Helper function for implementing BUS_GET_RESOURCE().
4019 * This implementation of BUS_GET_RESOURCE() uses the
4020 * resource_list_find() function to do most of the work. It calls
4021 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4025 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4026 rman_res_t *startp, rman_res_t *countp)
4028 struct resource_list * rl = NULL;
4029 struct resource_list_entry * rle = NULL;
4031 rl = BUS_GET_RESOURCE_LIST(dev, child);
4035 rle = resource_list_find(rl, type, rid);
4040 *startp = rle->start;
4042 *countp = rle->count;
4048 * @brief Helper function for implementing BUS_SET_RESOURCE().
4050 * This implementation of BUS_SET_RESOURCE() uses the
4051 * resource_list_add() function to do most of the work. It calls
4052 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4056 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4057 rman_res_t start, rman_res_t count)
4059 struct resource_list * rl = NULL;
4061 rl = BUS_GET_RESOURCE_LIST(dev, child);
4065 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4071 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4073 * This implementation of BUS_DELETE_RESOURCE() uses the
4074 * resource_list_delete() function to do most of the work. It calls
4075 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4079 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4081 struct resource_list * rl = NULL;
4083 rl = BUS_GET_RESOURCE_LIST(dev, child);
4087 resource_list_delete(rl, type, rid);
4093 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4095 * This implementation of BUS_RELEASE_RESOURCE() uses the
4096 * resource_list_release() function to do most of the work. It calls
4097 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4100 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4101 int rid, struct resource *r)
4103 struct resource_list * rl = NULL;
4105 if (device_get_parent(child) != dev)
4106 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4109 rl = BUS_GET_RESOURCE_LIST(dev, child);
4113 return (resource_list_release(rl, dev, child, type, rid, r));
4117 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4119 * This implementation of BUS_ALLOC_RESOURCE() uses the
4120 * resource_list_alloc() function to do most of the work. It calls
4121 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4124 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4125 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4127 struct resource_list * rl = NULL;
4129 if (device_get_parent(child) != dev)
4130 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4131 type, rid, start, end, count, flags));
4133 rl = BUS_GET_RESOURCE_LIST(dev, child);
4137 return (resource_list_alloc(rl, dev, child, type, rid,
4138 start, end, count, flags));
4142 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4144 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4145 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4148 bus_generic_child_present(device_t dev, device_t child)
4150 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4154 bus_generic_get_domain(device_t dev, device_t child, int *domain)
4157 return (BUS_GET_DOMAIN(dev->parent, dev, domain));
4163 * @brief Helper function to implement normal BUS_GET_DEVICE_PATH()
4165 * This function knows how to (a) pass the request up the tree if there's
4166 * a parent and (b) Knows how to supply a FreeBSD locator.
4168 * @param bus bus in the walk up the tree
4169 * @param child leaf node to print information about
4170 * @param locator BUS_LOCATOR_xxx string for locator
4171 * @param sb Buffer to print information into
4174 bus_generic_get_device_path(device_t bus, device_t child, const char *locator,
4181 * We don't recurse on ACPI since either we know the handle for the
4182 * device or we don't. And if we're in the generic routine, we don't
4183 * have a ACPI override. All other locators build up a path by having
4184 * their parents create a path and then adding the path element for this
4185 * node. That's why we recurse with parent, bus rather than the typical
4186 * parent, child: each spot in the tree is independent of what our child
4187 * will do with this path.
4189 parent = device_get_parent(bus);
4190 if (parent != NULL && strcmp(locator, BUS_LOCATOR_ACPI) != 0) {
4191 rv = BUS_GET_DEVICE_PATH(parent, bus, locator, sb);
4193 if (strcmp(locator, BUS_LOCATOR_FREEBSD) == 0) {
4195 sbuf_printf(sb, "/%s", device_get_nameunit(child));
4200 * Don't know what to do. So assume we do nothing. Not sure that's
4201 * the right thing, but keeps us from having a big list here.
4208 * @brief Helper function for implementing BUS_RESCAN().
4210 * This null implementation of BUS_RESCAN() always fails to indicate
4211 * the bus does not support rescanning.
4214 bus_null_rescan(device_t dev)
4220 * Some convenience functions to make it easier for drivers to use the
4221 * resource-management functions. All these really do is hide the
4222 * indirection through the parent's method table, making for slightly
4223 * less-wordy code. In the future, it might make sense for this code
4224 * to maintain some sort of a list of resources allocated by each device.
4228 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4229 struct resource **res)
4233 for (i = 0; rs[i].type != -1; i++)
4235 for (i = 0; rs[i].type != -1; i++) {
4236 res[i] = bus_alloc_resource_any(dev,
4237 rs[i].type, &rs[i].rid, rs[i].flags);
4238 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4239 bus_release_resources(dev, rs, res);
4247 bus_release_resources(device_t dev, const struct resource_spec *rs,
4248 struct resource **res)
4252 for (i = 0; rs[i].type != -1; i++)
4253 if (res[i] != NULL) {
4254 bus_release_resource(
4255 dev, rs[i].type, rs[i].rid, res[i]);
4261 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4263 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4267 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
4268 rman_res_t end, rman_res_t count, u_int flags)
4270 struct resource *res;
4272 if (dev->parent == NULL)
4274 res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4280 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4282 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4286 bus_adjust_resource(device_t dev, int type, struct resource *r, rman_res_t start,
4289 if (dev->parent == NULL)
4291 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4295 * @brief Wrapper function for BUS_TRANSLATE_RESOURCE().
4297 * This function simply calls the BUS_TRANSLATE_RESOURCE() method of the
4301 bus_translate_resource(device_t dev, int type, rman_res_t start,
4302 rman_res_t *newstart)
4304 if (dev->parent == NULL)
4306 return (BUS_TRANSLATE_RESOURCE(dev->parent, type, start, newstart));
4310 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4312 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4316 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4318 if (dev->parent == NULL)
4320 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4324 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4326 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4330 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4332 if (dev->parent == NULL)
4334 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4338 * @brief Wrapper function for BUS_MAP_RESOURCE().
4340 * This function simply calls the BUS_MAP_RESOURCE() method of the
4344 bus_map_resource(device_t dev, int type, struct resource *r,
4345 struct resource_map_request *args, struct resource_map *map)
4347 if (dev->parent == NULL)
4349 return (BUS_MAP_RESOURCE(dev->parent, dev, type, r, args, map));
4353 * @brief Wrapper function for BUS_UNMAP_RESOURCE().
4355 * This function simply calls the BUS_UNMAP_RESOURCE() method of the
4359 bus_unmap_resource(device_t dev, int type, struct resource *r,
4360 struct resource_map *map)
4362 if (dev->parent == NULL)
4364 return (BUS_UNMAP_RESOURCE(dev->parent, dev, type, r, map));
4368 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4370 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4374 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4378 if (dev->parent == NULL)
4380 rv = BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r);
4385 * @brief Wrapper function for BUS_SETUP_INTR().
4387 * This function simply calls the BUS_SETUP_INTR() method of the
4391 bus_setup_intr(device_t dev, struct resource *r, int flags,
4392 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4396 if (dev->parent == NULL)
4398 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4402 if (handler != NULL && !(flags & INTR_MPSAFE))
4403 device_printf(dev, "[GIANT-LOCKED]\n");
4408 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4410 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4414 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4416 if (dev->parent == NULL)
4418 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4422 * @brief Wrapper function for BUS_SUSPEND_INTR().
4424 * This function simply calls the BUS_SUSPEND_INTR() method of the
4428 bus_suspend_intr(device_t dev, struct resource *r)
4430 if (dev->parent == NULL)
4432 return (BUS_SUSPEND_INTR(dev->parent, dev, r));
4436 * @brief Wrapper function for BUS_RESUME_INTR().
4438 * This function simply calls the BUS_RESUME_INTR() method of the
4442 bus_resume_intr(device_t dev, struct resource *r)
4444 if (dev->parent == NULL)
4446 return (BUS_RESUME_INTR(dev->parent, dev, r));
4450 * @brief Wrapper function for BUS_BIND_INTR().
4452 * This function simply calls the BUS_BIND_INTR() method of the
4456 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4458 if (dev->parent == NULL)
4460 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4464 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4466 * This function first formats the requested description into a
4467 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4468 * the parent of @p dev.
4471 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4472 const char *fmt, ...)
4475 char descr[MAXCOMLEN + 1];
4477 if (dev->parent == NULL)
4480 vsnprintf(descr, sizeof(descr), fmt, ap);
4482 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4486 * @brief Wrapper function for BUS_SET_RESOURCE().
4488 * This function simply calls the BUS_SET_RESOURCE() method of the
4492 bus_set_resource(device_t dev, int type, int rid,
4493 rman_res_t start, rman_res_t count)
4495 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4500 * @brief Wrapper function for BUS_GET_RESOURCE().
4502 * This function simply calls the BUS_GET_RESOURCE() method of the
4506 bus_get_resource(device_t dev, int type, int rid,
4507 rman_res_t *startp, rman_res_t *countp)
4509 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4514 * @brief Wrapper function for BUS_GET_RESOURCE().
4516 * This function simply calls the BUS_GET_RESOURCE() method of the
4517 * parent of @p dev and returns the start value.
4520 bus_get_resource_start(device_t dev, int type, int rid)
4526 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4534 * @brief Wrapper function for BUS_GET_RESOURCE().
4536 * This function simply calls the BUS_GET_RESOURCE() method of the
4537 * parent of @p dev and returns the count value.
4540 bus_get_resource_count(device_t dev, int type, int rid)
4546 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4554 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4556 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4560 bus_delete_resource(device_t dev, int type, int rid)
4562 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4566 * @brief Wrapper function for BUS_CHILD_PRESENT().
4568 * This function simply calls the BUS_CHILD_PRESENT() method of the
4572 bus_child_present(device_t child)
4574 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4578 * @brief Wrapper function for BUS_CHILD_PNPINFO().
4580 * This function simply calls the BUS_CHILD_PNPINFO() method of the parent of @p
4584 bus_child_pnpinfo(device_t child, struct sbuf *sb)
4588 parent = device_get_parent(child);
4591 return (BUS_CHILD_PNPINFO(parent, child, sb));
4595 * @brief Generic implementation that does nothing for bus_child_pnpinfo
4597 * This function has the right signature and returns 0 since the sbuf is passed
4598 * to us to append to.
4601 bus_generic_child_pnpinfo(device_t dev, device_t child, struct sbuf *sb)
4607 * @brief Wrapper function for BUS_CHILD_LOCATION().
4609 * This function simply calls the BUS_CHILD_LOCATION() method of the parent of
4613 bus_child_location(device_t child, struct sbuf *sb)
4617 parent = device_get_parent(child);
4620 return (BUS_CHILD_LOCATION(parent, child, sb));
4624 * @brief Generic implementation that does nothing for bus_child_location
4626 * This function has the right signature and returns 0 since the sbuf is passed
4627 * to us to append to.
4630 bus_generic_child_location(device_t dev, device_t child, struct sbuf *sb)
4636 * @brief Wrapper function for BUS_GET_CPUS().
4638 * This function simply calls the BUS_GET_CPUS() method of the
4642 bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, cpuset_t *cpuset)
4646 parent = device_get_parent(dev);
4649 return (BUS_GET_CPUS(parent, dev, op, setsize, cpuset));
4653 * @brief Wrapper function for BUS_GET_DMA_TAG().
4655 * This function simply calls the BUS_GET_DMA_TAG() method of the
4659 bus_get_dma_tag(device_t dev)
4663 parent = device_get_parent(dev);
4666 return (BUS_GET_DMA_TAG(parent, dev));
4670 * @brief Wrapper function for BUS_GET_BUS_TAG().
4672 * This function simply calls the BUS_GET_BUS_TAG() method of the
4676 bus_get_bus_tag(device_t dev)
4680 parent = device_get_parent(dev);
4682 return ((bus_space_tag_t)0);
4683 return (BUS_GET_BUS_TAG(parent, dev));
4687 * @brief Wrapper function for BUS_GET_DOMAIN().
4689 * This function simply calls the BUS_GET_DOMAIN() method of the
4693 bus_get_domain(device_t dev, int *domain)
4695 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain));
4698 /* Resume all devices and then notify userland that we're up again. */
4700 root_resume(device_t dev)
4704 error = bus_generic_resume(dev);
4706 devctl_notify("kern", "power", "resume", NULL); /* Deprecated gone in 14 */
4707 devctl_notify("kernel", "power", "resume", NULL);
4713 root_print_child(device_t dev, device_t child)
4717 retval += bus_print_child_header(dev, child);
4718 retval += printf("\n");
4724 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4725 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4728 * If an interrupt mapping gets to here something bad has happened.
4730 panic("root_setup_intr");
4734 * If we get here, assume that the device is permanent and really is
4735 * present in the system. Removable bus drivers are expected to intercept
4736 * this call long before it gets here. We return -1 so that drivers that
4737 * really care can check vs -1 or some ERRNO returned higher in the food
4741 root_child_present(device_t dev, device_t child)
4747 root_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
4752 /* Default to returning the set of all CPUs. */
4753 if (setsize != sizeof(cpuset_t))
4762 static kobj_method_t root_methods[] = {
4763 /* Device interface */
4764 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4765 KOBJMETHOD(device_suspend, bus_generic_suspend),
4766 KOBJMETHOD(device_resume, root_resume),
4769 KOBJMETHOD(bus_print_child, root_print_child),
4770 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4771 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4772 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4773 KOBJMETHOD(bus_child_present, root_child_present),
4774 KOBJMETHOD(bus_get_cpus, root_get_cpus),
4779 static driver_t root_driver = {
4786 devclass_t root_devclass;
4789 root_bus_module_handler(module_t mod, int what, void* arg)
4793 TAILQ_INIT(&bus_data_devices);
4794 kobj_class_compile((kobj_class_t) &root_driver);
4795 root_bus = make_device(NULL, "root", 0);
4796 root_bus->desc = "System root bus";
4797 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4798 root_bus->driver = &root_driver;
4799 root_bus->state = DS_ATTACHED;
4800 root_devclass = devclass_find_internal("root", NULL, FALSE);
4805 device_shutdown(root_bus);
4808 return (EOPNOTSUPP);
4814 static moduledata_t root_bus_mod = {
4816 root_bus_module_handler,
4819 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4822 * @brief Automatically configure devices
4824 * This function begins the autoconfiguration process by calling
4825 * device_probe_and_attach() for each child of the @c root0 device.
4828 root_bus_configure(void)
4832 /* Eventually this will be split up, but this is sufficient for now. */
4833 bus_set_pass(BUS_PASS_DEFAULT);
4837 * @brief Module handler for registering device drivers
4839 * This module handler is used to automatically register device
4840 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4841 * devclass_add_driver() for the driver described by the
4842 * driver_module_data structure pointed to by @p arg
4845 driver_module_handler(module_t mod, int what, void *arg)
4847 struct driver_module_data *dmd;
4848 devclass_t bus_devclass;
4849 kobj_class_t driver;
4852 dmd = (struct driver_module_data *)arg;
4853 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4858 if (dmd->dmd_chainevh)
4859 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4861 pass = dmd->dmd_pass;
4862 driver = dmd->dmd_driver;
4863 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4864 DRIVERNAME(driver), dmd->dmd_busname, pass));
4865 error = devclass_add_driver(bus_devclass, driver, pass,
4870 PDEBUG(("Unloading module: driver %s from bus %s",
4871 DRIVERNAME(dmd->dmd_driver),
4873 error = devclass_delete_driver(bus_devclass,
4876 if (!error && dmd->dmd_chainevh)
4877 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4880 PDEBUG(("Quiesce module: driver %s from bus %s",
4881 DRIVERNAME(dmd->dmd_driver),
4883 error = devclass_quiesce_driver(bus_devclass,
4886 if (!error && dmd->dmd_chainevh)
4887 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4898 * @brief Enumerate all hinted devices for this bus.
4900 * Walks through the hints for this bus and calls the bus_hinted_child
4901 * routine for each one it fines. It searches first for the specific
4902 * bus that's being probed for hinted children (eg isa0), and then for
4903 * generic children (eg isa).
4905 * @param dev bus device to enumerate
4908 bus_enumerate_hinted_children(device_t bus)
4911 const char *dname, *busname;
4915 * enumerate all devices on the specific bus
4917 busname = device_get_nameunit(bus);
4919 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4920 BUS_HINTED_CHILD(bus, dname, dunit);
4923 * and all the generic ones.
4925 busname = device_get_name(bus);
4927 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4928 BUS_HINTED_CHILD(bus, dname, dunit);
4933 /* the _short versions avoid iteration by not calling anything that prints
4934 * more than oneliners. I love oneliners.
4938 print_device_short(device_t dev, int indent)
4943 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4944 dev->unit, dev->desc,
4945 (dev->parent? "":"no "),
4946 (TAILQ_EMPTY(&dev->children)? "no ":""),
4947 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4948 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4949 (dev->flags&DF_WILDCARD? "wildcard,":""),
4950 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4951 (dev->flags&DF_SUSPENDED? "suspended,":""),
4952 (dev->ivars? "":"no "),
4953 (dev->softc? "":"no "),
4958 print_device(device_t dev, int indent)
4963 print_device_short(dev, indent);
4965 indentprintf(("Parent:\n"));
4966 print_device_short(dev->parent, indent+1);
4967 indentprintf(("Driver:\n"));
4968 print_driver_short(dev->driver, indent+1);
4969 indentprintf(("Devclass:\n"));
4970 print_devclass_short(dev->devclass, indent+1);
4974 print_device_tree_short(device_t dev, int indent)
4975 /* print the device and all its children (indented) */
4982 print_device_short(dev, indent);
4984 TAILQ_FOREACH(child, &dev->children, link) {
4985 print_device_tree_short(child, indent+1);
4990 print_device_tree(device_t dev, int indent)
4991 /* print the device and all its children (indented) */
4998 print_device(dev, indent);
5000 TAILQ_FOREACH(child, &dev->children, link) {
5001 print_device_tree(child, indent+1);
5006 print_driver_short(driver_t *driver, int indent)
5011 indentprintf(("driver %s: softc size = %zd\n",
5012 driver->name, driver->size));
5016 print_driver(driver_t *driver, int indent)
5021 print_driver_short(driver, indent);
5025 print_driver_list(driver_list_t drivers, int indent)
5027 driverlink_t driver;
5029 TAILQ_FOREACH(driver, &drivers, link) {
5030 print_driver(driver->driver, indent);
5035 print_devclass_short(devclass_t dc, int indent)
5040 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
5044 print_devclass(devclass_t dc, int indent)
5051 print_devclass_short(dc, indent);
5052 indentprintf(("Drivers:\n"));
5053 print_driver_list(dc->drivers, indent+1);
5055 indentprintf(("Devices:\n"));
5056 for (i = 0; i < dc->maxunit; i++)
5058 print_device(dc->devices[i], indent+1);
5062 print_devclass_list_short(void)
5066 printf("Short listing of devclasses, drivers & devices:\n");
5067 TAILQ_FOREACH(dc, &devclasses, link) {
5068 print_devclass_short(dc, 0);
5073 print_devclass_list(void)
5077 printf("Full listing of devclasses, drivers & devices:\n");
5078 TAILQ_FOREACH(dc, &devclasses, link) {
5079 print_devclass(dc, 0);
5086 * User-space access to the device tree.
5088 * We implement a small set of nodes:
5090 * hw.bus Single integer read method to obtain the
5091 * current generation count.
5092 * hw.bus.devices Reads the entire device tree in flat space.
5093 * hw.bus.rman Resource manager interface
5095 * We might like to add the ability to scan devclasses and/or drivers to
5096 * determine what else is currently loaded/available.
5100 sysctl_bus_info(SYSCTL_HANDLER_ARGS)
5102 struct u_businfo ubus;
5104 ubus.ub_version = BUS_USER_VERSION;
5105 ubus.ub_generation = bus_data_generation;
5107 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
5109 SYSCTL_PROC(_hw_bus, OID_AUTO, info, CTLTYPE_STRUCT | CTLFLAG_RD |
5110 CTLFLAG_MPSAFE, NULL, 0, sysctl_bus_info, "S,u_businfo",
5111 "bus-related data");
5114 sysctl_devices(SYSCTL_HANDLER_ARGS)
5117 int *name = (int *)arg1;
5118 u_int namelen = arg2;
5121 struct u_device *udev;
5127 if (bus_data_generation_check(name[0]))
5133 * Scan the list of devices, looking for the requested index.
5135 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5143 * Populate the return item, careful not to overflow the buffer.
5145 udev = malloc(sizeof(*udev), M_BUS, M_WAITOK | M_ZERO);
5148 udev->dv_handle = (uintptr_t)dev;
5149 udev->dv_parent = (uintptr_t)dev->parent;
5150 udev->dv_devflags = dev->devflags;
5151 udev->dv_flags = dev->flags;
5152 udev->dv_state = dev->state;
5153 sbuf_new(&sb, udev->dv_fields, sizeof(udev->dv_fields), SBUF_FIXEDLEN);
5154 if (dev->nameunit != NULL)
5155 sbuf_cat(&sb, dev->nameunit);
5156 sbuf_putc(&sb, '\0');
5157 if (dev->desc != NULL)
5158 sbuf_cat(&sb, dev->desc);
5159 sbuf_putc(&sb, '\0');
5160 if (dev->driver != NULL)
5161 sbuf_cat(&sb, dev->driver->name);
5162 sbuf_putc(&sb, '\0');
5163 bus_child_pnpinfo(dev, &sb);
5164 sbuf_putc(&sb, '\0');
5165 bus_child_location(dev, &sb);
5166 sbuf_putc(&sb, '\0');
5167 error = sbuf_finish(&sb);
5169 error = SYSCTL_OUT(req, udev, sizeof(*udev));
5175 SYSCTL_NODE(_hw_bus, OID_AUTO, devices,
5176 CTLFLAG_RD | CTLFLAG_NEEDGIANT, sysctl_devices,
5177 "system device tree");
5180 bus_data_generation_check(int generation)
5182 if (generation != bus_data_generation)
5185 /* XXX generate optimised lists here? */
5190 bus_data_generation_update(void)
5192 atomic_add_int(&bus_data_generation, 1);
5196 bus_free_resource(device_t dev, int type, struct resource *r)
5200 return (bus_release_resource(dev, type, rman_get_rid(r), r));
5204 device_lookup_by_name(const char *name)
5208 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5209 if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0)
5216 * /dev/devctl2 implementation. The existing /dev/devctl device has
5217 * implicit semantics on open, so it could not be reused for this.
5218 * Another option would be to call this /dev/bus?
5221 find_device(struct devreq *req, device_t *devp)
5226 * First, ensure that the name is nul terminated.
5228 if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL)
5232 * Second, try to find an attached device whose name matches
5235 dev = device_lookup_by_name(req->dr_name);
5241 /* Finally, give device enumerators a chance. */
5243 EVENTHANDLER_DIRECT_INVOKE(dev_lookup, req->dr_name, &dev);
5251 driver_exists(device_t bus, const char *driver)
5255 for (dc = bus->devclass; dc != NULL; dc = dc->parent) {
5256 if (devclass_find_driver_internal(dc, driver) != NULL)
5263 device_gen_nomatch(device_t dev)
5267 if (dev->flags & DF_NEEDNOMATCH &&
5268 dev->state == DS_NOTPRESENT) {
5269 device_handle_nomatch(dev);
5271 dev->flags &= ~DF_NEEDNOMATCH;
5272 TAILQ_FOREACH(child, &dev->children, link) {
5273 device_gen_nomatch(child);
5278 device_do_deferred_actions(void)
5284 * Walk through the devclasses to find all the drivers we've tagged as
5285 * deferred during the freeze and call the driver added routines. They
5286 * have already been added to the lists in the background, so the driver
5287 * added routines that trigger a probe will have all the right bidders
5288 * for the probe auction.
5290 TAILQ_FOREACH(dc, &devclasses, link) {
5291 TAILQ_FOREACH(dl, &dc->drivers, link) {
5292 if (dl->flags & DL_DEFERRED_PROBE) {
5293 devclass_driver_added(dc, dl->driver);
5294 dl->flags &= ~DL_DEFERRED_PROBE;
5300 * We also defer no-match events during a freeze. Walk the tree and
5301 * generate all the pent-up events that are still relevant.
5303 device_gen_nomatch(root_bus);
5304 bus_data_generation_update();
5308 device_get_path(device_t dev, const char *locator, struct sbuf *sb)
5313 KASSERT(sb != NULL, ("sb is NULL"));
5314 parent = device_get_parent(dev);
5315 if (parent == NULL) {
5316 error = sbuf_printf(sb, "/");
5318 error = BUS_GET_DEVICE_PATH(parent, dev, locator, sb);
5320 error = sbuf_error(sb);
5321 if (error == 0 && sbuf_len(sb) <= 1)
5330 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
5337 /* Locate the device to control. */
5339 req = (struct devreq *)data;
5347 case DEV_SET_DRIVER:
5348 case DEV_CLEAR_DRIVER:
5352 error = priv_check(td, PRIV_DRIVER);
5354 error = find_device(req, &dev);
5358 error = priv_check(td, PRIV_DRIVER);
5361 error = find_device(req, &dev);
5372 /* Perform the requested operation. */
5375 if (device_is_attached(dev))
5377 else if (!device_is_enabled(dev))
5380 error = device_probe_and_attach(dev);
5383 if (!device_is_attached(dev)) {
5387 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5388 error = device_quiesce(dev);
5392 error = device_detach(dev);
5395 if (device_is_enabled(dev)) {
5401 * If the device has been probed but not attached (e.g.
5402 * when it has been disabled by a loader hint), just
5403 * attach the device rather than doing a full probe.
5406 if (device_is_alive(dev)) {
5408 * If the device was disabled via a hint, clear
5411 if (resource_disabled(dev->driver->name, dev->unit))
5412 resource_unset_value(dev->driver->name,
5413 dev->unit, "disabled");
5414 error = device_attach(dev);
5416 error = device_probe_and_attach(dev);
5419 if (!device_is_enabled(dev)) {
5424 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5425 error = device_quiesce(dev);
5431 * Force DF_FIXEDCLASS on around detach to preserve
5432 * the existing name.
5435 dev->flags |= DF_FIXEDCLASS;
5436 error = device_detach(dev);
5437 if (!(old & DF_FIXEDCLASS))
5438 dev->flags &= ~DF_FIXEDCLASS;
5440 device_disable(dev);
5443 if (device_is_suspended(dev)) {
5447 if (device_get_parent(dev) == NULL) {
5451 error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev);
5454 if (!device_is_suspended(dev)) {
5458 if (device_get_parent(dev) == NULL) {
5462 error = BUS_RESUME_CHILD(device_get_parent(dev), dev);
5464 case DEV_SET_DRIVER: {
5468 error = copyinstr(req->dr_data, driver, sizeof(driver), NULL);
5471 if (driver[0] == '\0') {
5475 if (dev->devclass != NULL &&
5476 strcmp(driver, dev->devclass->name) == 0)
5477 /* XXX: Could possibly force DF_FIXEDCLASS on? */
5481 * Scan drivers for this device's bus looking for at
5482 * least one matching driver.
5484 if (dev->parent == NULL) {
5488 if (!driver_exists(dev->parent, driver)) {
5492 dc = devclass_create(driver);
5498 /* Detach device if necessary. */
5499 if (device_is_attached(dev)) {
5500 if (req->dr_flags & DEVF_SET_DRIVER_DETACH)
5501 error = device_detach(dev);
5508 /* Clear any previously-fixed device class and unit. */
5509 if (dev->flags & DF_FIXEDCLASS)
5510 devclass_delete_device(dev->devclass, dev);
5511 dev->flags |= DF_WILDCARD;
5514 /* Force the new device class. */
5515 error = devclass_add_device(dc, dev);
5518 dev->flags |= DF_FIXEDCLASS;
5519 error = device_probe_and_attach(dev);
5522 case DEV_CLEAR_DRIVER:
5523 if (!(dev->flags & DF_FIXEDCLASS)) {
5527 if (device_is_attached(dev)) {
5528 if (req->dr_flags & DEVF_CLEAR_DRIVER_DETACH)
5529 error = device_detach(dev);
5536 dev->flags &= ~DF_FIXEDCLASS;
5537 dev->flags |= DF_WILDCARD;
5538 devclass_delete_device(dev->devclass, dev);
5539 error = device_probe_and_attach(dev);
5542 if (!device_is_attached(dev)) {
5546 error = BUS_RESCAN(dev);
5551 parent = device_get_parent(dev);
5552 if (parent == NULL) {
5556 if (!(req->dr_flags & DEVF_FORCE_DELETE)) {
5557 if (bus_child_present(dev) != 0) {
5563 error = device_delete_child(parent, dev);
5570 device_frozen = true;
5576 device_do_deferred_actions();
5577 device_frozen = false;
5581 if ((req->dr_flags & ~(DEVF_RESET_DETACH)) != 0) {
5585 error = BUS_RESET_CHILD(device_get_parent(dev), dev,
5588 case DEV_GET_PATH: {
5593 error = copyinstr(req->dr_buffer.buffer, locator,
5594 sizeof(locator), NULL);
5597 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND |
5598 SBUF_INCLUDENUL /* | SBUF_WAITOK */);
5599 error = device_get_path(dev, locator, sb);
5602 if (req->dr_buffer.length < len) {
5603 error = ENAMETOOLONG;
5605 error = copyout(sbuf_data(sb),
5606 req->dr_buffer.buffer, len);
5608 req->dr_buffer.length = len;
5618 static struct cdevsw devctl2_cdevsw = {
5619 .d_version = D_VERSION,
5620 .d_ioctl = devctl2_ioctl,
5621 .d_name = "devctl2",
5627 make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL,
5628 UID_ROOT, GID_WHEEL, 0644, "devctl2");
5632 * For maintaining device 'at' location info to avoid recomputing it
5634 struct device_location_node {
5635 const char *dln_locator;
5636 const char *dln_path;
5637 TAILQ_ENTRY(device_location_node) dln_link;
5639 typedef TAILQ_HEAD(device_location_list, device_location_node) device_location_list_t;
5641 struct device_location_cache {
5642 device_location_list_t dlc_list;
5647 * Location cache for wired devices.
5649 device_location_cache_t *
5650 dev_wired_cache_init(void)
5652 device_location_cache_t *dcp;
5654 dcp = malloc(sizeof(*dcp), M_BUS, M_WAITOK | M_ZERO);
5655 TAILQ_INIT(&dcp->dlc_list);
5661 dev_wired_cache_fini(device_location_cache_t *dcp)
5663 struct device_location_node *dln, *tdln;
5665 TAILQ_FOREACH_SAFE(dln, &dcp->dlc_list, dln_link, tdln) {
5671 static struct device_location_node *
5672 dev_wired_cache_lookup(device_location_cache_t *dcp, const char *locator)
5674 struct device_location_node *dln;
5676 TAILQ_FOREACH(dln, &dcp->dlc_list, dln_link) {
5677 if (strcmp(locator, dln->dln_locator) == 0)
5684 static struct device_location_node *
5685 dev_wired_cache_add(device_location_cache_t *dcp, const char *locator, const char *path)
5687 struct device_location_node *dln;
5688 size_t loclen, pathlen;
5690 loclen = strlen(locator) + 1;
5691 pathlen = strlen(path) + 1;
5692 dln = malloc(sizeof(*dln) + loclen + pathlen, M_BUS, M_WAITOK | M_ZERO);
5693 dln->dln_locator = (char *)(dln + 1);
5694 memcpy(__DECONST(char *, dln->dln_locator), locator, loclen);
5695 dln->dln_path = dln->dln_locator + loclen;
5696 memcpy(__DECONST(char *, dln->dln_path), path, pathlen);
5697 TAILQ_INSERT_HEAD(&dcp->dlc_list, dln, dln_link);
5703 dev_wired_cache_match(device_location_cache_t *dcp, device_t dev,
5710 struct device_location_node *res;
5712 cp = strchr(at, ':');
5716 if (len > sizeof(locator) - 1) /* Skip too long locator */
5718 memcpy(locator, at, len);
5719 locator[len] = '\0';
5723 /* maybe cache this inside device_t and look that up, but not yet */
5724 res = dev_wired_cache_lookup(dcp, locator);
5726 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND |
5727 SBUF_INCLUDENUL | SBUF_NOWAIT);
5729 error = device_get_path(dev, locator, sb);
5731 res = dev_wired_cache_add(dcp, locator,
5737 if (error != 0 || res == NULL || res->dln_path == NULL)
5740 return (strcmp(res->dln_path, cp) == 0);
5744 * APIs to manage deprecation and obsolescence.
5746 static int obsolete_panic = 0;
5747 SYSCTL_INT(_debug, OID_AUTO, obsolete_panic, CTLFLAG_RWTUN, &obsolete_panic, 0,
5748 "Panic when obsolete features are used (0 = never, 1 = if obsolete, "
5749 "2 = if deprecated)");
5752 gone_panic(int major, int running, const char *msg)
5754 switch (obsolete_panic)
5759 if (running < major)
5768 _gone_in(int major, const char *msg)
5770 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
5771 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
5772 printf("Obsolete code will be removed soon: %s\n", msg);
5774 printf("Deprecated code (to be removed in FreeBSD %d): %s\n",
5779 _gone_in_dev(device_t dev, int major, const char *msg)
5781 gone_panic(major, P_OSREL_MAJOR(__FreeBSD_version), msg);
5782 if (P_OSREL_MAJOR(__FreeBSD_version) >= major)
5784 "Obsolete code will be removed soon: %s\n", msg);
5787 "Deprecated code (to be removed in FreeBSD %d): %s\n",
5792 DB_SHOW_COMMAND(device, db_show_device)
5799 dev = (device_t)addr;
5801 db_printf("name: %s\n", device_get_nameunit(dev));
5802 db_printf(" driver: %s\n", DRIVERNAME(dev->driver));
5803 db_printf(" class: %s\n", DEVCLANAME(dev->devclass));
5804 db_printf(" addr: %p\n", dev);
5805 db_printf(" parent: %p\n", dev->parent);
5806 db_printf(" softc: %p\n", dev->softc);
5807 db_printf(" ivars: %p\n", dev->ivars);
5810 DB_SHOW_ALL_COMMAND(devices, db_show_all_devices)
5814 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5815 db_show_device((db_expr_t)dev, true, count, modif);