7#ifndef ZEPHYR_INCLUDE_DEVICE_H_
8#define ZEPHYR_INCLUDE_DEVICE_H_
36#define Z_DEVICE_DEPS_SEP INT16_MIN
42#define Z_DEVICE_DEPS_ENDS INT16_MAX
64#define DEVICE_HANDLE_NULL 0
85#define DEVICE_NAME_GET(dev_id) _CONCAT(__device_, dev_id)
95#define Z_DEVICE_DT_DEV_ID(node_id) _CONCAT(dts_ord_, DT_DEP_ORD(node_id))
127#define DEVICE_DEFINE(dev_id, name, init_fn, pm, data, config, level, prio, \
129 Z_DEVICE_STATE_DEFINE(dev_id); \
130 Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, name, init_fn, pm, data, \
131 config, level, prio, api, \
132 &Z_DEVICE_STATE_NAME(dev_id))
145#define DEVICE_DT_NAME(node_id) \
146 DT_PROP_OR(node_id, label, DT_NODE_FULL_NAME(node_id))
178#define DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, prio, api, \
180 Z_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
181 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
182 DEVICE_DT_NAME(node_id), init_fn, pm, data, config, \
184 &Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)), \
195#define DEVICE_DT_INST_DEFINE(inst, ...) \
196 DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
212#define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_ID(node_id))
229#define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
240#define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
258#define DEVICE_DT_GET_ANY(compat) \
259 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
260 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
279#define DEVICE_DT_GET_ONE(compat) \
280 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
281 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
282 (ZERO_OR_COMPILE_ERROR(0)))
294#define DEVICE_DT_GET_OR_NULL(node_id) \
295 COND_CODE_1(DT_NODE_HAS_STATUS(node_id, okay), \
296 (DEVICE_DT_GET(node_id)), (NULL))
308#define DEVICE_GET(dev_id) (&DEVICE_NAME_GET(dev_id))
324#define DEVICE_DECLARE(dev_id) \
325 static const struct device DEVICE_NAME_GET(dev_id)
334#define DEVICE_INIT_DT_GET(node_id) \
335 (&Z_INIT_ENTRY_NAME(DEVICE_DT_NAME_GET(node_id)))
344#define DEVICE_INIT_GET(dev_id) (&Z_INIT_ENTRY_NAME(DEVICE_NAME_GET(dev_id)))
372#ifdef CONFIG_DEVICE_DEPS_DYNAMIC
373#define Z_DEVICE_DEPS_CONST
375#define Z_DEVICE_DEPS_CONST const
392#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
403#if defined(CONFIG_PM_DEVICE) || defined(__DOXYGEN__)
443static inline const struct device *
447 const struct device *dev = NULL;
452 if ((dev_handle > 0) && ((
size_t)dev_handle <= numdev)) {
459#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
508 while ((rv[i] != Z_DEVICE_DEPS_ENDS) &&
509 (rv[i] != Z_DEVICE_DEPS_SEP)) {
545 while (region != 1) {
546 if (*rv == Z_DEVICE_DEPS_SEP) {
551 while ((rv[i] != Z_DEVICE_DEPS_ENDS) &&
552 (rv[i] != Z_DEVICE_DEPS_SEP)) {
589 while (region != 2) {
590 if (*rv == Z_DEVICE_DEPS_SEP) {
599 while ((rv[i] != Z_DEVICE_DEPS_ENDS) &&
708size_t z_device_get_all_static(
const struct device **devices);
724bool z_device_is_ready(
const struct device *dev);
744static inline bool z_impl_device_is_ready(
const struct device *dev)
746 return z_device_is_ready(dev);
759#define Z_DEVICE_STATE_NAME(dev_id) _CONCAT(__devstate_, dev_id)
766#define Z_DEVICE_STATE_DEFINE(dev_id) \
767 static Z_DECL_ALIGN(struct device_state) Z_DEVICE_STATE_NAME(dev_id) \
768 __attribute__((__section__(".z_devstate")))
770#if defined(CONFIG_DEVICE_DEPS) || defined(__DOXYGEN__)
778#define Z_DEVICE_DEPS_NAME(dev_id) _CONCAT(__devicedeps_, dev_id)
785#define Z_DEVICE_EXTRA_DEPS(...) \
786 FOR_EACH_NONEMPTY_TERM(IDENTITY, (,), __VA_ARGS__)
789#define Z_DEVICE_DEPS_SECTION \
790 __attribute__((__section__(".__device_deps_pass1")))
793#define Z_DEVICE_DEPS_EXTERN extern
795#define Z_DEVICE_DEPS_EXTERN
833#define Z_DEVICE_DEPS_DEFINE(node_id, dev_id, ...) \
834 extern Z_DEVICE_DEPS_CONST device_handle_t Z_DEVICE_DEPS_NAME( \
836 Z_DEVICE_DEPS_CONST Z_DECL_ALIGN(device_handle_t) \
837 Z_DEVICE_DEPS_SECTION Z_DEVICE_DEPS_EXTERN __weak \
838 Z_DEVICE_DEPS_NAME(dev_id)[] = { \
840 DT_NODE_EXISTS(node_id), \
841 (DT_DEP_ORD(node_id), DT_REQUIRES_DEP_ORDS(node_id)), \
842 (DEVICE_HANDLE_NULL,)) \
844 Z_DEVICE_EXTRA_DEPS(__VA_ARGS__) \
846 COND_CODE_1(DT_NODE_EXISTS(node_id), \
847 (DT_SUPPORTS_DEP_ORDS(node_id)), ()) \
859#define Z_DEVICE_INIT_SUB_PRIO(node_id) \
860 COND_CODE_1(DT_NODE_EXISTS(node_id), \
861 (DT_DEP_ORD_STR_SORTABLE(node_id)), (0))
869#define Z_DEVICE_MAX_NAME_LEN 48U
876#define Z_DEVICE_NAME_CHECK(name) \
877 BUILD_ASSERT(sizeof(Z_STRINGIFY(name)) <= Z_DEVICE_MAX_NAME_LEN, \
878 Z_STRINGIFY(DEVICE_NAME_GET(name)) " too long")
891#define Z_DEVICE_INIT(name_, pm_, data_, config_, api_, state_, deps_) \
894 .config = (config_), \
898 IF_ENABLED(CONFIG_DEVICE_DEPS, (.deps = (deps_),)) \
899 IF_ENABLED(CONFIG_PM_DEVICE, (.pm = (pm_),)) \
908#define Z_DEVICE_SECTION_NAME(level, prio) \
909 _CONCAT(INIT_LEVEL_ORD(level), _##prio)
927#define Z_DEVICE_BASE_DEFINE(node_id, dev_id, name, pm, data, config, level, \
928 prio, api, state, deps) \
929 COND_CODE_1(DT_NODE_EXISTS(node_id), (), (static)) \
930 const STRUCT_SECTION_ITERABLE_NAMED(device, \
931 Z_DEVICE_SECTION_NAME(level, prio), \
932 DEVICE_NAME_GET(dev_id)) = \
933 Z_DEVICE_INIT(name, pm, data, config, api, state, deps)
936#define Z_DEVICE_LEVEL_DEPRECATED_EARLY \
937 __WARN("EARLY device driver level is deprecated")
938#define Z_DEVICE_LEVEL_DEPRECATED_PRE_KERNEL_1
939#define Z_DEVICE_LEVEL_DEPRECATED_PRE_KERNEL_2
940#define Z_DEVICE_LEVEL_DEPRECATED_POST_KERNEL
941#define Z_DEVICE_LEVEL_DEPRECATED_APPLICATION \
942 __WARN("APPLICATION device driver level is deprecated")
943#define Z_DEVICE_LEVEL_DEPRECATED_SMP \
944 __WARN("SMP device driver level is deprecated")
951#define Z_DEVICE_LEVEL_CHECK_DEPRECATED_LEVEL(level) \
952 Z_DEVICE_LEVEL_DEPRECATED_##level
964#define Z_DEVICE_INIT_ENTRY_DEFINE(node_id, dev_id, init_fn_, level, prio) \
965 Z_DEVICE_LEVEL_CHECK_DEPRECATED_LEVEL(level) \
967 static const Z_DECL_ALIGN(struct init_entry) __used __noasan \
968 Z_INIT_ENTRY_SECTION(level, prio, \
969 Z_DEVICE_INIT_SUB_PRIO(node_id)) \
970 Z_INIT_ENTRY_NAME(DEVICE_NAME_GET(dev_id)) = { \
971 .init_fn = {.dev = (init_fn_)}, \
972 .dev = &DEVICE_NAME_GET(dev_id), \
996#define Z_DEVICE_DEFINE(node_id, dev_id, name, init_fn, pm, data, config, \
997 level, prio, api, state, ...) \
998 Z_DEVICE_NAME_CHECK(name); \
1000 IF_ENABLED(CONFIG_DEVICE_DEPS, \
1001 (Z_DEVICE_DEPS_DEFINE(node_id, dev_id, __VA_ARGS__);)) \
1003 Z_DEVICE_BASE_DEFINE(node_id, dev_id, name, pm, data, config, level, \
1004 prio, api, state, Z_DEVICE_DEPS_NAME(dev_id)); \
1006 Z_DEVICE_INIT_ENTRY_DEFINE(node_id, dev_id, init_fn, level, prio)
1008#if defined(CONFIG_HAS_DTS) || defined(__DOXYGEN__)
1019#define Z_MAYBE_DEVICE_DECLARE_INTERNAL(node_id) \
1020 extern const struct device DEVICE_DT_NAME_GET(node_id);
1031#include <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:61
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:501
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:581
static device_handle_t device_handle_get(const struct device *dev)
Get the handle for a given device.
Definition: device.h:420
#define DEVICE_HANDLE_NULL
Flag value used to identify an unknown device.
Definition: device.h:64
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:444
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:479
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
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:537
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:2498
#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:281
Definitions of various linker Sections.
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__INT16_TYPE__ int16_t
Definition: stdint.h:73
Runtime device dynamic structure (in RAM) per driver instance.
Definition: device.h:354
bool initialized
Indicates the device initialization function has been invoked.
Definition: device.h:367
uint8_t init_res
Device initialization return code (positive errno value).
Definition: device.h:362
Runtime device structure (in ROM) per driver instance.
Definition: device.h:381
const device_handle_t * deps
Optional pointer to dependencies associated with the device.
Definition: device.h:401
const char * name
Name of the device instance.
Definition: device.h:383
struct pm_device * pm
Reference to the device PM resources (only available if <code>CONFIG_PM_DEVICE</code> <verbatim>em...
Definition: device.h:408
void * data
Address of the device instance private data.
Definition: device.h:391
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:387
struct device_state * state
Address of the common device state.
Definition: device.h:389
const void * config
Address of device instance config information.
Definition: device.h:385
Device PM info.
Definition: device.h:126
const struct device * dev
Pointer to the device.
Definition: device.h:129