7#ifndef ZEPHYR_INCLUDE_DEVICE_H_
8#define ZEPHYR_INCLUDE_DEVICE_H_
43#define Z_DEVICE_DEPS_SEP INT16_MIN
49#define Z_DEVICE_DEPS_ENDS INT16_MAX
52#define Z_DEVICE_IS_MUTABLE(node_id) \
53 COND_CODE_1(IS_ENABLED(CONFIG_DEVICE_MUTABLE), (DT_PROP(node_id, zephyr_mutable)), (0))
75#define DEVICE_HANDLE_NULL 0
96#define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
104#define Z_DEVICE_DT_DEP_ORD(node_id) _CONCAT(dts_ord_, DT_DEP_ORD(node_id))
111#define Z_DEVICE_DT_HASH(node_id) _CONCAT(dts_, DT_NODE_HASH(node_id))
121#if defined(LL_EXTENSION_BUILD) && defined(CONFIG_LLEXT_EXPORT_DEV_IDS_BY_HASH)
122#define Z_DEVICE_DT_DEV_ID(node_id) Z_DEVICE_DT_HASH(node_id)
124#define Z_DEVICE_DT_DEV_ID(node_id) Z_DEVICE_DT_DEP_ORD(node_id)
127#if defined(CONFIG_LLEXT_EXPORT_DEV_IDS_BY_HASH)
129#define Z_DEVICE_EXPORT(node_id) \
130 EXPORT_SYMBOL_NAMED(DEVICE_DT_NAME_GET(node_id), \
131 DEVICE_NAME_GET(Z_DEVICE_DT_HASH(node_id)))
132#elif defined(CONFIG_LLEXT_EXPORT_DEVICES)
134#define Z_DEVICE_EXPORT(node_id) EXPORT_SYMBOL(DEVICE_DT_NAME_GET(node_id))
173#define DEVICE_DEINIT_DEFINE(dev_id, name, init_fn, deinit_fn, pm, data, \
174 config, level, prio, api) \
175 Z_DEVICE_STATE_DEFINE(dev_id); \
176 Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, name, init_fn, deinit_fn, 0U, \
177 pm, data, config, level, prio, api, \
178 &Z_DEVICE_STATE_NAME(dev_id))
185#define DEVICE_DEFINE(dev_id, name, init_fn, pm, data, config, level, prio, \
187 DEVICE_DEINIT_DEFINE(dev_id, name, init_fn, NULL, pm, data, config, \
201#define DEVICE_DT_NAME(node_id) \
202 DT_PROP_OR(node_id, label, DT_NODE_FULL_NAME(node_id))
201#define DEVICE_DT_NAME(node_id) \ …
240#define DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, data, config, \
241 level, prio, api, ...) \
242 Z_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
243 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
244 DEVICE_DT_NAME(node_id), init_fn, deinit_fn, \
245 Z_DEVICE_DT_FLAGS(node_id), pm, data, config, level, \
247 &Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)), \
256#define DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, prio, api, \
258 DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, NULL, pm, data, config, \
259 level, prio, api, __VA_ARGS__)
269#define DEVICE_DT_INST_DEINIT_DEFINE(inst, ...) \
270 DEVICE_DT_DEINIT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
269#define DEVICE_DT_INST_DEINIT_DEFINE(inst, ...) \ …
280#define DEVICE_DT_INST_DEFINE(inst, ...) \
281 DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
280#define DEVICE_DT_INST_DEFINE(inst, ...) \ …
297#define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_ID(node_id))
314#define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
325#define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
343#define DEVICE_DT_GET_ANY(compat) \
344 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
345 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
343#define DEVICE_DT_GET_ANY(compat) \ …
364#define DEVICE_DT_GET_ONE(compat) \
365 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
366 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
367 (ZERO_OR_COMPILE_ERROR(0)))
364#define DEVICE_DT_GET_ONE(compat) \ …
379#define DEVICE_DT_GET_OR_NULL(node_id) \
380 COND_CODE_1(DT_NODE_HAS_STATUS_OKAY(node_id), \
381 (DEVICE_DT_GET(node_id)), (NULL))
379#define DEVICE_DT_GET_OR_NULL(node_id) \ …
396#define DEVICE_DT_GET_BY_IDX(node_id, prop, idx) \
397 DEVICE_DT_GET(DT_PHANDLE_BY_IDX(node_id, prop, idx))
396#define DEVICE_DT_GET_BY_IDX(node_id, prop, idx) \ …
409#define DEVICE_GET(dev_id) (&DEVICE_NAME_GET(dev_id))
425#define DEVICE_DECLARE(dev_id) \
426 static const struct device DEVICE_NAME_GET(dev_id)
425#define DEVICE_DECLARE(dev_id) \ …
435#define DEVICE_INIT_DT_GET(node_id) \
436 (&Z_INIT_ENTRY_NAME(DEVICE_DT_NAME_GET(node_id)))
435#define DEVICE_INIT_DT_GET(node_id) \ …
445#define DEVICE_INIT_GET(dev_id) (&Z_INIT_ENTRY_NAME(DEVICE_NAME_GET(dev_id)))
474#if defined(CONFIG_DEVICE_DT_METADATA) || defined(__DOXYGEN__)
475struct device_dt_metadata;
478#ifdef CONFIG_DEVICE_DEPS_DYNAMIC
479#define Z_DEVICE_DEPS_CONST
481#define Z_DEVICE_DEPS_CONST const
493#define DEVICE_FLAG_INIT_DEFERRED BIT(0)
501#ifdef CONFIG_DEVICE_DEINIT_SUPPORT
503 int (*deinit)(
const struct device *dev);
525#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
536#if defined(CONFIG_PM_DEVICE) || defined(__DOXYGEN__)
547#if defined(CONFIG_DEVICE_DT_METADATA) || defined(__DOXYGEN__)
583static inline const struct device *
592 if ((dev_handle > 0) && ((
size_t)dev_handle <= numdev)) {
599#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
648 while ((rv[i] != Z_DEVICE_DEPS_ENDS) &&
649 (rv[i] != Z_DEVICE_DEPS_SEP)) {
685 while (region != 1) {
686 if (*rv == Z_DEVICE_DEPS_SEP) {
691 while ((rv[i] != Z_DEVICE_DEPS_ENDS) &&
692 (rv[i] != Z_DEVICE_DEPS_SEP)) {
729 while (region != 2) {
730 if (*rv == Z_DEVICE_DEPS_SEP) {
739 while ((rv[i] != Z_DEVICE_DEPS_ENDS) &&
848size_t z_device_get_all_static(
const struct device **devices);
915#define Z_DEVICE_STATE_NAME(dev_id) _CONCAT(__devstate_, dev_id)
922#define Z_DEVICE_STATE_DEFINE(dev_id) \
923 static Z_DECL_ALIGN(struct device_state) Z_DEVICE_STATE_NAME(dev_id) \
924 __attribute__((__section__(".z_devstate")))
931#define Z_DEVICE_DT_FLAGS(node_id) \
932 (DT_PROP_OR(node_id, zephyr_deferred_init, 0U) * DEVICE_FLAG_INIT_DEFERRED)
934#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
942#define Z_DEVICE_DEPS_NAME(dev_id) _CONCAT(__devicedeps_, dev_id)
949#define Z_DEVICE_EXTRA_DEPS(...) \
950 FOR_EACH_NONEMPTY_TERM(IDENTITY, (,), __VA_ARGS__)
953#define Z_DEVICE_DEPS_SECTION \
954 __attribute__((__section__(".__device_deps_pass1")))
957#define Z_DEVICE_DEPS_EXTERN extern
959#define Z_DEVICE_DEPS_EXTERN
997#define Z_DEVICE_DEPS_DEFINE(node_id, dev_id, ...) \
998 extern Z_DEVICE_DEPS_CONST device_handle_t Z_DEVICE_DEPS_NAME( \
1000 Z_DEVICE_DEPS_CONST Z_DECL_ALIGN(device_handle_t) \
1001 Z_DEVICE_DEPS_SECTION Z_DEVICE_DEPS_EXTERN __weak \
1002 Z_DEVICE_DEPS_NAME(dev_id)[] = { \
1004 DT_NODE_EXISTS(node_id), \
1005 (DT_DEP_ORD(node_id), DT_REQUIRES_DEP_ORDS(node_id)), \
1006 (DEVICE_HANDLE_NULL,)) \
1007 Z_DEVICE_DEPS_SEP, \
1008 Z_DEVICE_EXTRA_DEPS(__VA_ARGS__) \
1009 Z_DEVICE_DEPS_SEP, \
1010 COND_CODE_1(DT_NODE_EXISTS(node_id), \
1011 (DT_SUPPORTS_DEP_ORDS(node_id)), ()) \
1015#if defined(CONFIG_DEVICE_DT_METADATA) || defined(__DOXYGEN__)
1019struct device_dt_nodelabels {
1021 size_t num_nodelabels;
1025 const char *nodelabels[];
1035struct device_dt_metadata {
1040 const struct device_dt_nodelabels *nl;
1061__syscall
const struct device *device_get_by_dt_nodelabel(
const char *nodelabel);
1068static inline const struct device_dt_nodelabels *
1069device_get_dt_nodelabels(
const struct device *dev)
1083#define Z_DEVICE_MAX_NODELABEL_LEN Z_DEVICE_MAX_NAME_LEN
1089#define Z_DEVICE_DT_METADATA_NAME_GET(dev_id) UTIL_CAT(__dev_dt_meta_, dev_id)
1095#define Z_DEVICE_DT_NODELABELS_NAME_GET(dev_id) UTIL_CAT(__dev_dt_nodelabels_, dev_id)
1103#define Z_DEVICE_DT_METADATA_DEFINE(node_id, dev_id) \
1104 static const struct device_dt_nodelabels \
1105 Z_DEVICE_DT_NODELABELS_NAME_GET(dev_id) = { \
1106 .num_nodelabels = DT_NUM_NODELABELS(node_id), \
1107 .nodelabels = DT_NODELABEL_STRING_ARRAY(node_id), \
1110 static const struct device_dt_metadata \
1111 Z_DEVICE_DT_METADATA_NAME_GET(dev_id) = { \
1112 .nl = &Z_DEVICE_DT_NODELABELS_NAME_GET(dev_id), \
1123#define Z_DEVICE_INIT_SUB_PRIO(node_id) \
1124 COND_CODE_1(DT_NODE_EXISTS(node_id), \
1125 (DT_DEP_ORD_STR_SORTABLE(node_id)), (0))
1133#define Z_DEVICE_MAX_NAME_LEN 48U
1140#define Z_DEVICE_NAME_CHECK(name) \
1141 BUILD_ASSERT(sizeof(Z_STRINGIFY(name)) <= Z_DEVICE_MAX_NAME_LEN, \
1142 Z_STRINGIFY(name) " too long")
1150#define Z_DEVICE_OPS(init_fn_, deinit_fn_) \
1152 .init = (init_fn_), \
1153 IF_ENABLED(CONFIG_DEVICE_DEINIT_SUPPORT, \
1154 (.deinit = (deinit_fn_),)) \
1173#define Z_DEVICE_INIT(name_, init_fn_, deinit_fn_, flags_, pm_, data_, config_, api_, \
1174 state_, deps_, node_id_, dev_id_) \
1177 .config = (config_), \
1179 .state = (state_), \
1181 .ops = Z_DEVICE_OPS(init_fn_, deinit_fn_), \
1182 .flags = (flags_), \
1183 IF_ENABLED(CONFIG_DEVICE_DEPS, (.deps = (deps_),)) \
1184 IF_ENABLED(CONFIG_PM_DEVICE, Z_DEVICE_INIT_PM_BASE(pm_)) \
1185 IF_ENABLED(CONFIG_DEVICE_DT_METADATA, \
1186 (IF_ENABLED(DT_NODE_EXISTS(node_id_), \
1187 (.dt_meta = &Z_DEVICE_DT_METADATA_NAME_GET( \
1196#if defined(__STDC_VERSION__) && (__STDC_VERSION__) < 201100
1197# define Z_DEVICE_INIT_PM_BASE(pm_) ({ .pm_base = (pm_),},)
1199# define Z_DEVICE_INIT_PM_BASE(pm_) (.pm_base = (pm_),)
1208#define Z_DEVICE_SECTION_NAME(level, prio) \
1209 _CONCAT(INIT_LEVEL_ORD(level), _##prio)
1230#define Z_DEVICE_BASE_DEFINE(node_id, dev_id, name, init_fn, deinit_fn, flags, pm, data, config, \
1231 level, prio, api, state, deps) \
1232 COND_CODE_1(DT_NODE_EXISTS(node_id), (), (static)) \
1233 COND_CODE_1(Z_DEVICE_IS_MUTABLE(node_id), (), (const)) \
1234 STRUCT_SECTION_ITERABLE_NAMED_ALTERNATE( \
1235 device, COND_CODE_1(Z_DEVICE_IS_MUTABLE(node_id), (device_mutable), (device)), \
1236 Z_DEVICE_SECTION_NAME(level, prio), DEVICE_NAME_GET(dev_id)) = \
1237 Z_DEVICE_INIT(name, init_fn, deinit_fn, flags, pm, data, config, api, state, deps, \
1245#define Z_DEVICE_CHECK_INIT_LEVEL(level) \
1246 COND_CODE_1(Z_INIT_PRE_KERNEL_1_##level, (), \
1247 (COND_CODE_1(Z_INIT_PRE_KERNEL_2_##level, (), \
1248 (COND_CODE_1(Z_INIT_POST_KERNEL_##level, (), \
1249 (ZERO_OR_COMPILE_ERROR(0)))))))
1260#define Z_DEVICE_INIT_ENTRY_DEFINE(node_id, dev_id, level, prio) \
1261 Z_DEVICE_CHECK_INIT_LEVEL(level) \
1263 static const Z_DECL_ALIGN(struct init_entry) __used __noasan Z_INIT_ENTRY_SECTION( \
1264 level, prio, Z_DEVICE_INIT_SUB_PRIO(node_id)) \
1265 Z_INIT_ENTRY_NAME(DEVICE_NAME_GET(dev_id)) = { \
1267 .dev = (const struct device *)&DEVICE_NAME_GET(dev_id), \
1292#define Z_DEVICE_DEFINE(node_id, dev_id, name, init_fn, deinit_fn, flags, pm, \
1293 data, config, level, prio, api, state, ...) \
1294 Z_DEVICE_NAME_CHECK(name); \
1296 IF_ENABLED(CONFIG_DEVICE_DEPS, \
1297 (Z_DEVICE_DEPS_DEFINE(node_id, dev_id, __VA_ARGS__);)) \
1299 IF_ENABLED(CONFIG_DEVICE_DT_METADATA, \
1300 (IF_ENABLED(DT_NODE_EXISTS(node_id), \
1301 (Z_DEVICE_DT_METADATA_DEFINE(node_id, dev_id);))))\
1303 Z_DEVICE_BASE_DEFINE(node_id, dev_id, name, init_fn, deinit_fn, flags, \
1304 pm, data, config, level, prio, api, state, \
1305 Z_DEVICE_DEPS_NAME(dev_id)); \
1307 Z_DEVICE_INIT_ENTRY_DEFINE(node_id, dev_id, level, prio); \
1309 IF_ENABLED(CONFIG_LLEXT_EXPORT_DEVICES, \
1310 (IF_ENABLED(DT_NODE_EXISTS(node_id), \
1311 (Z_DEVICE_EXPORT(node_id);))))
1323#define Z_MAYBE_DEVICE_DECLARE_INTERNAL(node_id) \
1324 extern COND_CODE_1(Z_DEVICE_IS_MUTABLE(node_id), (), \
1325 (const)) struct device DEVICE_DT_NAME_GET(node_id);
1330#define Z_DEVICE_API_TYPE(_class) _CONCAT(_class, _driver_api)
1340#define DEVICE_API(_class, _name) const STRUCT_SECTION_ITERABLE(Z_DEVICE_API_TYPE(_class), _name)
1350#define DEVICE_API_GET(_class, _dev) ((const struct Z_DEVICE_API_TYPE(_class) *)_dev->api)
1362#define DEVICE_API_IS(_class, _dev) \
1364 STRUCT_SECTION_START_EXTERN(Z_DEVICE_API_TYPE(_class)); \
1365 STRUCT_SECTION_END_EXTERN(Z_DEVICE_API_TYPE(_class)); \
1366 (DEVICE_API_GET(_class, _dev) < STRUCT_SECTION_END(Z_DEVICE_API_TYPE(_class)) && \
1367 DEVICE_API_GET(_class, _dev) >= STRUCT_SECTION_START(Z_DEVICE_API_TYPE(_class))); \
1362#define DEVICE_API_IS(_class, _dev) \ …
1374#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:72
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:641
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:721
static device_handle_t device_handle_get(const struct device *dev)
Get the handle for a given device.
Definition device.h:560
#define DEVICE_HANDLE_NULL
Flag value used to identify an unknown device.
Definition device.h:75
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:584
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:619
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:485
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:677
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:3000
#define STRUCT_SECTION_START_EXTERN(struct_type)
iterable section extern for start symbol for a struct
Definition iterable_sections.h:159
#define STRUCT_SECTION_START(struct_type)
iterable section start symbol for a struct type
Definition iterable_sections.h:149
#define STRUCT_SECTION_COUNT(struct_type, dst)
Count elements in a section.
Definition iterable_sections.h:291
#define NULL
Definition iar_missing_defs.h:20
Definitions of various linker Sections.
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__INT16_TYPE__ int16_t
Definition stdint.h:73
Device operations.
Definition device.h:498
int(* init)(const struct device *dev)
Initialization function.
Definition device.h:500
Runtime device dynamic structure (in RAM) per driver instance.
Definition device.h:455
bool initialized
Indicates the device initialization function has been invoked.
Definition device.h:468
uint8_t init_res
Device initialization return code (positive errno value).
Definition device.h:463
Runtime device structure (in ROM) per driver instance.
Definition device.h:510
struct pm_device_base * pm_base
Definition device.h:542
const device_handle_t * deps
Optional pointer to dependencies associated with the device.
Definition device.h:534
struct pm_device_isr * pm_isr
Definition device.h:544
const char * name
Name of the device instance.
Definition device.h:512
struct pm_device * pm
Definition device.h:543
void * data
Address of the device instance private data.
Definition device.h:520
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516
device_flags_t flags
Device flags.
Definition device.h:524
struct device_ops ops
Device operations.
Definition device.h:522
struct device_state * state
Address of the common device state.
Definition device.h:518
const void * config
Address of device instance config information.
Definition device.h:514
const struct device_dt_metadata * dt_meta
Definition device.h:548
Device PM info.
Definition device.h:139
Runtime PM info for device with synchronous PM.
Definition device.h:187
Runtime PM info for device with generic PM.
Definition device.h:163
Linkable loadable extension symbol definitions.