13#ifndef ZEPHYR_INCLUDE_DEVICE_H_
14#define ZEPHYR_INCLUDE_DEVICE_H_
49#define Z_DEVICE_DEPS_SEP INT16_MIN
55#define Z_DEVICE_DEPS_ENDS INT16_MAX
58#define Z_DEVICE_IS_MUTABLE(node_id) \
59 COND_CODE_1(IS_ENABLED(CONFIG_DEVICE_MUTABLE), (DT_PROP(node_id, zephyr_mutable)), (0))
81#define DEVICE_HANDLE_NULL 0
102#define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
110#define Z_DEVICE_DT_DEP_ORD(node_id) _CONCAT(dts_ord_, DT_DEP_ORD(node_id))
117#define Z_DEVICE_DT_HASH(node_id) _CONCAT(dts_, DT_NODE_HASH(node_id))
128#if defined(LL_EXTENSION_BUILD) && defined(CONFIG_LLEXT_EXPORT_DEV_IDS_BY_HASH)
129#define Z_DEVICE_DT_DEV_ID(node_id) Z_DEVICE_DT_HASH(node_id)
131#define Z_DEVICE_DT_DEV_ID(node_id) Z_DEVICE_DT_DEP_ORD(node_id)
134#if defined(CONFIG_LLEXT_EXPORT_DEV_IDS_BY_HASH)
136#define Z_DEVICE_EXPORT(node_id) \
137 EXPORT_GROUP_SYMBOL_NAMED(DEVICE, \
138 DEVICE_DT_NAME_GET(node_id), \
139 DEVICE_NAME_GET(Z_DEVICE_DT_HASH(node_id)))
142#define Z_DEVICE_EXPORT(node_id) \
143 EXPORT_GROUP_SYMBOL(DEVICE, DEVICE_DT_NAME_GET(node_id))
182#define DEVICE_DEINIT_DEFINE(dev_id, name, init_fn, deinit_fn, pm, data, \
183 config, level, prio, api) \
184 Z_DEVICE_STATE_DEFINE(dev_id); \
185 Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, name, init_fn, deinit_fn, 0U, \
186 pm, data, config, level, prio, api, \
187 &Z_DEVICE_STATE_NAME(dev_id))
194#define DEVICE_DEFINE(dev_id, name, init_fn, pm, data, config, level, prio, \
196 DEVICE_DEINIT_DEFINE(dev_id, name, init_fn, NULL, pm, data, config, \
210#define DEVICE_DT_NAME(node_id) \
211 DT_PROP_OR(node_id, label, DT_NODE_FULL_NAME(node_id))
249#define DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, data, config, \
250 level, prio, api, ...) \
251 Z_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
252 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
253 DEVICE_DT_NAME(node_id), init_fn, deinit_fn, \
254 Z_DEVICE_DT_FLAGS(node_id), pm, data, config, level, \
256 &Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)), \
265#define DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, prio, api, \
267 DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, NULL, pm, data, config, \
268 level, prio, api, __VA_ARGS__)
278#define DEVICE_DT_INST_DEINIT_DEFINE(inst, ...) \
279 DEVICE_DT_DEINIT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
289#define DEVICE_DT_INST_DEFINE(inst, ...) \
290 DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
306#define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_ID(node_id))
323#define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
335#define DEVICE_DT_GET_COMMA(node_id) DEVICE_DT_GET(node_id),
346#define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
358#define DEVICE_DT_INST_GET_COMMA(inst) DEVICE_DT_INST_GET(inst),
376#define DEVICE_DT_GET_ANY(compat) \
377 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
378 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
397#define DEVICE_DT_GET_ONE(compat) \
398 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
399 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
400 (ZERO_OR_COMPILE_ERROR(0)))
412#define DEVICE_DT_GET_OR_NULL(node_id) \
413 COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(node_id), \
414 (DEVICE_DT_GET(node_id)), (NULL))
429#define DEVICE_DT_GET_BY_IDX(node_id, prop, idx) \
430 DEVICE_DT_GET(DT_PHANDLE_BY_IDX(node_id, prop, idx))
442#define DEVICE_GET(dev_id) (&DEVICE_NAME_GET(dev_id))
458#define DEVICE_DECLARE(dev_id) \
459 static const struct device DEVICE_NAME_GET(dev_id)
468#define DEVICE_INIT_DT_GET(node_id) \
469 (&Z_INIT_ENTRY_NAME(DEVICE_DT_NAME_GET(node_id)))
478#define DEVICE_INIT_GET(dev_id) (&Z_INIT_ENTRY_NAME(DEVICE_NAME_GET(dev_id)))
507#if defined(CONFIG_DEVICE_DT_METADATA) || defined(__DOXYGEN__)
508struct device_dt_metadata;
511#ifdef CONFIG_DEVICE_DEPS_DYNAMIC
512#define Z_DEVICE_DEPS_CONST
514#define Z_DEVICE_DEPS_CONST const
526#define DEVICE_FLAG_INIT_DEFERRED BIT(0)
534#ifdef CONFIG_DEVICE_DEINIT_SUPPORT
536 int (*deinit)(
const struct device *dev);
558#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
569#if defined(CONFIG_PM_DEVICE) || defined(__DOXYGEN__)
583#if defined(CONFIG_DEVICE_DT_METADATA) || defined(__DOXYGEN__)
623static inline const struct device *
632 if ((dev_handle > 0) && ((
size_t)dev_handle <= numdev)) {
639#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
688 while ((rv[i] != Z_DEVICE_DEPS_ENDS) &&
689 (rv[i] != Z_DEVICE_DEPS_SEP)) {
725 while (region != 1) {
726 if (*rv == Z_DEVICE_DEPS_SEP) {
731 while ((rv[i] != Z_DEVICE_DEPS_ENDS) &&
732 (rv[i] != Z_DEVICE_DEPS_SEP)) {
769 while (region != 2) {
770 if (*rv == Z_DEVICE_DEPS_SEP) {
779 while ((rv[i] != Z_DEVICE_DEPS_ENDS) &&
888size_t z_device_get_all_static(
const struct device **devices);
916#define LOG_WRN_DEVICE_NOT_READY(dev) \
917 LOG_WRN("%s device not ready", (dev) ? (dev)->name : "(null)")
927#define LOG_ERR_DEVICE_NOT_READY(dev) \
928 LOG_ERR("%s device not ready", (dev) ? (dev)->name : "(null)")
939#define LOG_INST_WRN_DEVICE_NOT_READY(_log_inst, dev) \
940 LOG_INST_WRN(_log_inst, "%s device not ready", (dev) ? (dev)->name : "(null)")
951#define LOG_INST_ERR_DEVICE_NOT_READY(_log_inst, dev) \
952 LOG_INST_ERR(_log_inst, "%s device not ready", (dev) ? (dev)->name : "(null)")
1001#define Z_DEVICE_STATE_NAME(dev_id) _CONCAT(__devstate_, dev_id)
1008#define Z_DEVICE_STATE_DEFINE(dev_id) \
1009 static Z_DECL_ALIGN(struct device_state) Z_DEVICE_STATE_NAME(dev_id) \
1010 __attribute__((__section__(".z_devstate")))
1017#define Z_DEVICE_DT_FLAGS(node_id) \
1018 (DT_PROP_OR(node_id, zephyr_deferred_init, 0U) * DEVICE_FLAG_INIT_DEFERRED)
1020#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
1028#define Z_DEVICE_DEPS_NAME(dev_id) _CONCAT(__devicedeps_, dev_id)
1035#define Z_DEVICE_EXTRA_DEPS(...) \
1036 FOR_EACH_NONEMPTY_TERM(IDENTITY, (,), __VA_ARGS__)
1039#define Z_DEVICE_DEPS_SECTION \
1040 __attribute__((__section__(".__device_deps_pass1")))
1043#define Z_DEVICE_DEPS_EXTERN extern
1045#define Z_DEVICE_DEPS_EXTERN
1083#define Z_DEVICE_DEPS_DEFINE(node_id, dev_id, ...) \
1084 extern Z_DEVICE_DEPS_CONST device_handle_t Z_DEVICE_DEPS_NAME( \
1086 Z_DEVICE_DEPS_CONST Z_DECL_ALIGN(device_handle_t) \
1087 Z_DEVICE_DEPS_SECTION Z_DEVICE_DEPS_EXTERN __weak \
1088 Z_DEVICE_DEPS_NAME(dev_id)[] = { \
1090 DT_NODE_EXISTS(node_id), \
1091 (DT_DEP_ORD(node_id), DT_REQUIRES_DEP_ORDS(node_id)), \
1092 (DEVICE_HANDLE_NULL,)) \
1093 Z_DEVICE_DEPS_SEP, \
1094 Z_DEVICE_EXTRA_DEPS(__VA_ARGS__) \
1095 Z_DEVICE_DEPS_SEP, \
1096 IF_ENABLED(DT_NODE_EXISTS(node_id), \
1097 (DT_SUPPORTS_DEP_ORDS(node_id))) \
1101#if defined(CONFIG_DEVICE_DT_METADATA) || defined(__DOXYGEN__)
1105struct device_dt_nodelabels {
1107 size_t num_nodelabels;
1111 const char *nodelabels[];
1121struct device_dt_metadata {
1126 const struct device_dt_nodelabels *nl;
1147__syscall
const struct device *device_get_by_dt_nodelabel(
const char *nodelabel);
1154static inline const struct device_dt_nodelabels *
1155device_get_dt_nodelabels(
const struct device *dev)
1169#define Z_DEVICE_MAX_NODELABEL_LEN Z_DEVICE_MAX_NAME_LEN
1175#define Z_DEVICE_DT_METADATA_NAME_GET(dev_id) UTIL_CAT(__dev_dt_meta_, dev_id)
1181#define Z_DEVICE_DT_NODELABELS_NAME_GET(dev_id) UTIL_CAT(__dev_dt_nodelabels_, dev_id)
1189#define Z_DEVICE_DT_METADATA_DEFINE(node_id, dev_id) \
1190 static const struct device_dt_nodelabels \
1191 Z_DEVICE_DT_NODELABELS_NAME_GET(dev_id) = { \
1192 .num_nodelabels = DT_NUM_NODELABELS(node_id), \
1193 .nodelabels = DT_NODELABEL_STRING_ARRAY(node_id), \
1196 static const struct device_dt_metadata \
1197 Z_DEVICE_DT_METADATA_NAME_GET(dev_id) = { \
1198 .nl = &Z_DEVICE_DT_NODELABELS_NAME_GET(dev_id), \
1209#define Z_DEVICE_INIT_SUB_PRIO(node_id) \
1210 COND_CODE_1(DT_NODE_EXISTS(node_id), \
1211 (DT_DEP_ORD_STR_SORTABLE(node_id)), (0))
1219#define Z_DEVICE_MAX_NAME_LEN 48U
1226#define Z_DEVICE_NAME_CHECK(name) \
1227 BUILD_ASSERT(sizeof(Z_STRINGIFY(name)) <= Z_DEVICE_MAX_NAME_LEN, \
1228 Z_STRINGIFY(name) " too long")
1236#define Z_DEVICE_OPS(init_fn_, deinit_fn_) \
1238 .init = (init_fn_), \
1239 IF_ENABLED(CONFIG_DEVICE_DEINIT_SUPPORT, \
1240 (.deinit = (deinit_fn_),)) \
1259#define Z_DEVICE_INIT(name_, init_fn_, deinit_fn_, flags_, pm_, data_, config_, api_, \
1260 state_, deps_, node_id_, dev_id_) \
1263 .config = (config_), \
1265 .state = (state_), \
1267 .ops = Z_DEVICE_OPS(init_fn_, deinit_fn_), \
1268 .flags = (flags_), \
1269 IF_ENABLED(CONFIG_DEVICE_DEPS, (.deps = (deps_),)) \
1270 IF_ENABLED(CONFIG_PM_DEVICE, Z_DEVICE_INIT_PM_BASE(pm_)) \
1271 IF_ENABLED(CONFIG_DEVICE_DT_METADATA, \
1272 (IF_ENABLED(DT_NODE_EXISTS(node_id_), \
1273 (.dt_meta = &Z_DEVICE_DT_METADATA_NAME_GET( \
1282#if defined(__STDC_VERSION__) && (__STDC_VERSION__) < 201100
1283# define Z_DEVICE_INIT_PM_BASE(pm_) ({ .pm_base = (pm_),},)
1285# define Z_DEVICE_INIT_PM_BASE(pm_) (.pm_base = (pm_),)
1294#define Z_DEVICE_SECTION_NAME(level, prio) \
1295 _CONCAT(INIT_LEVEL_ORD(level), _##prio)
1316#define Z_DEVICE_BASE_DEFINE(node_id, dev_id, name, init_fn, deinit_fn, flags, pm, data, config, \
1317 level, prio, api, state, deps) \
1318 IF_DISABLED(DT_NODE_EXISTS(node_id), (static)) \
1319 IF_DISABLED(Z_DEVICE_IS_MUTABLE(node_id), (const)) \
1320 STRUCT_SECTION_ITERABLE_NAMED_ALTERNATE( \
1321 device, COND_CODE_1(Z_DEVICE_IS_MUTABLE(node_id), (device_mutable), (device)), \
1322 Z_DEVICE_SECTION_NAME(level, prio), DEVICE_NAME_GET(dev_id)) = \
1323 Z_DEVICE_INIT(name, init_fn, deinit_fn, flags, pm, data, config, api, state, deps, \
1331#define Z_DEVICE_CHECK_INIT_LEVEL(level) \
1332 COND_CASE_1(Z_INIT_PRE_KERNEL_1_##level, (), \
1333 Z_INIT_PRE_KERNEL_2_##level, (), \
1334 Z_INIT_POST_KERNEL_##level, (), \
1335 (ZERO_OR_COMPILE_ERROR(0)))
1346#define Z_DEVICE_INIT_ENTRY_DEFINE(node_id, dev_id, level, prio) \
1347 Z_DEVICE_CHECK_INIT_LEVEL(level) \
1349 static const Z_DECL_ALIGN(struct init_entry) __used __noasan Z_INIT_ENTRY_SECTION( \
1350 level, prio, Z_DEVICE_INIT_SUB_PRIO(node_id)) \
1351 Z_INIT_ENTRY_NAME(DEVICE_NAME_GET(dev_id)) = { \
1353 .dev = (const struct device *)&DEVICE_NAME_GET(dev_id), \
1378#define Z_DEVICE_DEFINE(node_id, dev_id, name, init_fn, deinit_fn, flags, pm, \
1379 data, config, level, prio, api, state, ...) \
1380 Z_DEVICE_NAME_CHECK(name); \
1382 IF_ENABLED(CONFIG_DEVICE_DEPS, \
1383 (Z_DEVICE_DEPS_DEFINE(node_id, dev_id, __VA_ARGS__);)) \
1385 IF_ENABLED(CONFIG_DEVICE_DT_METADATA, \
1386 (IF_ENABLED(DT_NODE_EXISTS(node_id), \
1387 (Z_DEVICE_DT_METADATA_DEFINE(node_id, dev_id);))))\
1389 Z_DEVICE_BASE_DEFINE(node_id, dev_id, name, init_fn, deinit_fn, flags, \
1390 pm, data, config, level, prio, api, state, \
1391 Z_DEVICE_DEPS_NAME(dev_id)); \
1393 Z_DEVICE_INIT_ENTRY_DEFINE(node_id, dev_id, level, prio); \
1395 IF_ENABLED(CONFIG_LLEXT, \
1396 (IF_ENABLED(DT_NODE_EXISTS(node_id), \
1397 (Z_DEVICE_EXPORT(node_id);))))
1409#define Z_MAYBE_DEVICE_DECLARE_INTERNAL(node_id) \
1410 extern IF_DISABLED(Z_DEVICE_IS_MUTABLE(node_id), (const)) \
1411 struct device DEVICE_DT_NAME_GET(node_id);
1416#define Z_DEVICE_API_TYPE(_class) _CONCAT(_class, _driver_api)
1419#define Z_DEVICE_API_GET(_class, _dev) ((const struct Z_DEVICE_API_TYPE(_class) *)_dev->api)
1422#define Z_DEVICE_API_EXT_END(_struct_type) CONCAT(_, _struct_type, _ext_end)
1432#define DEVICE_API(_class, _name) const STRUCT_SECTION_ITERABLE(Z_DEVICE_API_TYPE(_class), _name)
1451#define DEVICE_API_EXTENDS(_child, _parent, _member) \
1452 CONTAINER_OF_VALIDATE((struct Z_DEVICE_API_TYPE(_parent) *)0, \
1453 struct Z_DEVICE_API_TYPE(_child), _member) \
1454 BUILD_ASSERT(offsetof(struct Z_DEVICE_API_TYPE(_child), _member) == 0, \
1455 "Parent API struct must be the first member of child API struct")
1467#define DEVICE_API_IS(_class, _dev) \
1469 STRUCT_SECTION_START_EXTERN(Z_DEVICE_API_TYPE(_class)); \
1470 extern const uint8_t Z_DEVICE_API_EXT_END(Z_DEVICE_API_TYPE(_class))[]; \
1472 ((const uint8_t *)Z_DEVICE_API_GET(_class, _dev) < \
1473 Z_DEVICE_API_EXT_END(Z_DEVICE_API_TYPE(_class)) && \
1474 (const uint8_t *)Z_DEVICE_API_GET(_class, _dev) >= \
1475 (const uint8_t *)STRUCT_SECTION_START(Z_DEVICE_API_TYPE(_class))); \
1486#define DEVICE_API_GET(_class, _dev) \
1488 IF_ENABLED(CONFIG_DEVICE_API_ASSERT, ( \
1489 __ASSERT(_dev != NULL, "device is NULL"); \
1490 __ASSERT(DEVICE_API_IS(_class, _dev), "device API is not %s", STRINGIFY(_class)); \
1492 Z_DEVICE_API_GET(_class, _dev); \
1499#include <zephyr/syscalls/device.h>
const struct device * device_get_binding(const char *name)
Get a device reference from its device::name field.
int16_t device_handle_t
Type used to represent a "handle" for a device.
Definition device.h:78
static const device_handle_t * device_required_handles_get(const struct device *dev, size_t *count)
Get the device handles for devicetree dependencies of this device.
Definition device.h:681
static const device_handle_t * device_supported_handles_get(const struct device *dev, size_t *count)
Get the set of handles that this device supports.
Definition device.h:761
static device_handle_t device_handle_get(const struct device *dev)
Get the handle for a given device.
Definition device.h:600
#define DEVICE_HANDLE_NULL
Flag value used to identify an unknown device.
Definition device.h:81
int device_required_foreach(const struct device *dev, device_visitor_callback_t visitor_cb, void *context)
Visit every device that dev directly requires.
static const struct device * device_from_handle(device_handle_t dev_handle)
Get the device corresponding to a handle.
Definition device.h:624
int device_deinit(const struct device *dev)
De-initialize a device.
int(* device_visitor_callback_t)(const struct device *dev, void *context)
Prototype for functions used when iterating over a set of devices.
Definition device.h:659
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
uint8_t device_flags_t
Device flags.
Definition device.h:518
static const device_handle_t * device_injected_handles_get(const struct device *dev, size_t *count)
Get the device handles for injected dependencies of this device.
Definition device.h:717
int device_init(const struct device *dev)
Initialize a device.
int device_supported_foreach(const struct device *dev, device_visitor_callback_t visitor_cb, void *context)
Visit every device that dev directly supports.
#define DT_FOREACH_STATUS_OKAY_NODE(fn)
Invokes fn for every status okay node in the tree.
Definition devicetree.h:3593
#define STRUCT_SECTION_START_EXTERN(struct_type)
iterable section extern for start symbol for a struct
Definition iterable_sections.h:187
#define STRUCT_SECTION_START(struct_type)
iterable section start symbol for a struct type
Definition iterable_sections.h:177
#define STRUCT_SECTION_COUNT(struct_type, dst)
Count elements in a section.
Definition iterable_sections.h:348
#define NULL
Definition iar_missing_defs.h:20
Public APIs for the system initialization infrastructure.
Definitions of various linker Sections.
Header file for System Power Management state definitions.
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__INT16_TYPE__ int16_t
Definition stdint.h:73
Device operations.
Definition device.h:531
int(* init)(const struct device *dev)
Initialization function.
Definition device.h:533
Runtime device dynamic structure (in RAM) per driver instance.
Definition device.h:488
bool initialized
Indicates the device initialization function has been invoked.
Definition device.h:501
uint8_t init_res
Device initialization return code (positive errno value).
Definition device.h:496
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
struct pm_device_base * pm_base
Info common to all device PM variants.
Definition device.h:576
const device_handle_t * deps
Optional pointer to dependencies associated with the device.
Definition device.h:567
struct pm_device_isr * pm_isr
Info for a device using synchronous PM.
Definition device.h:580
const char * name
Name of the device instance.
Definition device.h:545
struct pm_device * pm
Info for a device using generic PM.
Definition device.h:578
void * data
Address of the device instance private data.
Definition device.h:553
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:549
device_flags_t flags
Device flags.
Definition device.h:557
struct device_ops ops
Device operations.
Definition device.h:555
struct device_state * state
Address of the common device state.
Definition device.h:551
const void * config
Address of device instance config information.
Definition device.h:547
const struct device_dt_metadata * dt_meta
Devicetree metadata associated with the device (only available if.
Definition device.h:588
Device PM info.
Definition device.h:213
Runtime PM info for device with synchronous PM.
Definition device.h:261
Runtime PM info for device with generic PM.
Definition device.h:237
Linkable loadable extension symbol definitions.
Iterable sections helpers.