Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
device.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_DEVICE_H_
8#define ZEPHYR_INCLUDE_DEVICE_H_
9
29#include <init.h>
30#include <linker/sections.h>
31#include <sys/device_mmio.h>
32#include <sys/util.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
54
60#define DEVICE_HANDLE_SEP INT16_MIN
61
67#define DEVICE_HANDLE_ENDS INT16_MAX
68
70#define DEVICE_HANDLE_NULL 0
71
72#define Z_DEVICE_MAX_NAME_LEN 48
73
96#define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
97
110#define SYS_DEVICE_DEFINE(drv_name, init_fn, level, prio) \
111 __DEPRECATED_MACRO SYS_INIT(init_fn, level, prio)
112
113/* Node paths can exceed the maximum size supported by device_get_binding() in user mode,
114 * so synthesize a unique dev_name from the devicetree node.
115 *
116 * The ordinal used in this name can be mapped to the path by
117 * examining zephyr/include/generated/device_extern.h header. If the
118 * format of this conversion changes, gen_defines should be updated to
119 * match it.
120 */
121#define Z_DEVICE_DT_DEV_NAME(node_id) _CONCAT(dts_ord_, DT_DEP_ORD(node_id))
122
123/* Synthesize a unique name for the device state associated with
124 * dev_name.
125 */
126#define Z_DEVICE_STATE_NAME(dev_name) _CONCAT(__devstate_, dev_name)
127
134#define Z_DEVICE_STATE_DEFINE(node_id, dev_name) \
135 static struct device_state Z_DEVICE_STATE_NAME(dev_name) \
136 __attribute__((__section__(".z_devstate")));
137
181#define DEVICE_DEFINE(dev_name, drv_name, init_fn, pm_device, \
182 data_ptr, cfg_ptr, level, prio, api_ptr) \
183 Z_DEVICE_STATE_DEFINE(DT_INVALID_NODE, dev_name) \
184 Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_name, drv_name, init_fn, \
185 pm_device, \
186 data_ptr, cfg_ptr, level, prio, api_ptr, \
187 &Z_DEVICE_STATE_NAME(dev_name))
188
202#define DEVICE_DT_NAME(node_id) \
203 DT_PROP_OR(node_id, label, DT_NODE_FULL_NAME(node_id))
204
248#define DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
249 data_ptr, cfg_ptr, level, prio, \
250 api_ptr, ...) \
251 Z_DEVICE_STATE_DEFINE(node_id, Z_DEVICE_DT_DEV_NAME(node_id)) \
252 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_NAME(node_id), \
253 DEVICE_DT_NAME(node_id), init_fn, \
254 pm_device, \
255 data_ptr, cfg_ptr, level, prio, \
256 api_ptr, \
257 &Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_NAME(node_id)), \
258 __VA_ARGS__)
259
271#define DEVICE_DT_INST_DEFINE(inst, ...) \
272 DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
273
291#define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_NAME(node_id))
292
311#define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
312
323#define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
324
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))), \
346 (NULL))
347
367#define DEVICE_DT_GET_ONE(compat) \
368 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
369 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
370 (ZERO_OR_COMPILE_ERROR(0)))
371
386#define DEVICE_DT_GET_OR_NULL(node_id) \
387 COND_CODE_1(DT_NODE_HAS_STATUS(node_id, okay), \
388 (DEVICE_DT_GET(node_id)), (NULL))
389
402#define DEVICE_GET(name) (&DEVICE_NAME_GET(name))
403
419#define DEVICE_DECLARE(name) static const struct device DEVICE_NAME_GET(name)
420
437 unsigned int init_res : 8;
438
442 bool initialized : 1;
443};
444
445struct pm_device;
446
450struct device {
452 const char *name;
454 const void *config;
456 const void *api;
458 struct device_state * const state;
460 void * const data;
469#ifdef CONFIG_PM_DEVICE
471 struct pm_device * const pm;
472#endif
473};
474
483static inline device_handle_t
484device_handle_get(const struct device *dev)
485{
487 extern const struct device __device_start[];
488
489 /* TODO: If/when devices can be constructed that are not part of the
490 * fixed sequence we'll need another solution.
491 */
492 if (dev != NULL) {
493 ret = 1 + (device_handle_t)(dev - __device_start);
494 }
495
496 return ret;
497}
498
507static inline const struct device *
509{
510 extern const struct device __device_start[];
511 extern const struct device __device_end[];
512 const struct device *dev = NULL;
513 size_t numdev = __device_end - __device_start;
514
515 if ((dev_handle > 0) && ((size_t)dev_handle <= numdev)) {
516 dev = &__device_start[dev_handle - 1];
517 }
518
519 return dev;
520}
521
541typedef int (*device_visitor_callback_t)(const struct device *dev, void *context);
542
563static inline const device_handle_t *
565 size_t *count)
566{
567 const device_handle_t *rv = dev->handles;
568
569 if (rv != NULL) {
570 size_t i = 0;
571
572 while ((rv[i] != DEVICE_HANDLE_ENDS)
573 && (rv[i] != DEVICE_HANDLE_SEP)) {
574 ++i;
575 }
576 *count = i;
577 }
578
579 return rv;
580}
581
603static inline const device_handle_t *
605 size_t *count)
606{
607 const device_handle_t *rv = dev->handles;
608 size_t region = 0;
609 size_t i = 0;
610
611 if (rv != NULL) {
612 /* Fast forward to supporting devices */
613 while (region != 2) {
614 if (*rv == DEVICE_HANDLE_SEP) {
615 region++;
616 }
617 rv++;
618 }
619 /* Count supporting devices */
620 while (rv[i] != DEVICE_HANDLE_ENDS) {
621 ++i;
622 }
623 *count = i;
624 }
625
626 return rv;
627}
628
662int device_required_foreach(const struct device *dev,
663 device_visitor_callback_t visitor_cb,
664 void *context);
665
698int device_supported_foreach(const struct device *dev,
699 device_visitor_callback_t visitor_cb,
700 void *context);
701
724__syscall const struct device *device_get_binding(const char *name);
725
734size_t z_device_get_all_static(const struct device * *devices);
735
750bool z_device_is_ready(const struct device *dev);
751
767__syscall bool device_is_ready(const struct device *dev);
768
769static inline bool z_impl_device_is_ready(const struct device *dev)
770{
771 return z_device_is_ready(dev);
772}
773
787__deprecated static inline int z_device_usable_check(const struct device *dev)
788{
789 return z_device_is_ready(dev) ? 0 : -ENODEV;
790}
791
802__deprecated static inline int device_usable_check(const struct device *dev)
803{
804 return device_is_ready(dev) ? 0 : -ENODEV;
805}
806
811/* Synthesize the name of the object that holds device ordinal and
812 * dependency data. If the object doesn't come from a devicetree
813 * node, use dev_name.
814 */
815#define Z_DEVICE_HANDLE_NAME(node_id, dev_name) \
816 _CONCAT(__devicehdl_, \
817 COND_CODE_1(DT_NODE_EXISTS(node_id), \
818 (node_id), \
819 (dev_name)))
820
821#define Z_DEVICE_EXTRA_HANDLES(...) \
822 FOR_EACH_NONEMPTY_TERM(IDENTITY, (,), __VA_ARGS__)
823
824/*
825 * Utility macro to define and initialize the device state.
826 *
827 * @param node_id Devicetree node id of the device.
828 * @param dev_name Device name.
829 */
830#define Z_DEVICE_STATE_DEFINE(node_id, dev_name) \
831 static struct device_state Z_DEVICE_STATE_NAME(dev_name) \
832 __attribute__((__section__(".z_devstate")));
833
834/* Construct objects that are referenced from struct device. These
835 * include power management and dependency handles.
836 */
837#define Z_DEVICE_DEFINE_PRE(node_id, dev_name, ...) \
838 Z_DEVICE_DEFINE_HANDLES(node_id, dev_name, __VA_ARGS__)
839
840/* Initial build provides a record that associates the device object
841 * with its devicetree ordinal, and provides the dependency ordinals.
842 * These are provided as weak definitions (to prevent the reference
843 * from being captured when the original object file is compiled), and
844 * in a distinct pass1 section (which will be replaced by
845 * postprocessing).
846 *
847 * Before processing in gen_handles.py, the array format is:
848 * {
849 * DEVICE_ORDINAL (or DEVICE_HANDLE_NULL if not a devicetree node),
850 * List of devicetree dependency ordinals (if any),
851 * DEVICE_HANDLE_SEP,
852 * List of injected dependency ordinals (if any),
853 * DEVICE_HANDLE_SEP,
854 * List of devicetree supporting ordinals (if any),
855 * }
856 *
857 * After processing in gen_handles.py, the format is updated to:
858 * {
859 * List of existing devicetree dependency handles (if any),
860 * DEVICE_HANDLE_SEP,
861 * List of injected dependency ordinals (if any),
862 * DEVICE_HANDLE_SEP,
863 * List of existing devicetree support handles (if any),
864 * DEVICE_HANDLE_NULL
865 * }
866 *
867 * It is also (experimentally) necessary to provide explicit alignment
868 * on each object. Otherwise x86-64 builds will introduce padding
869 * between objects in the same input section in individual object
870 * files, which will be retained in subsequent links both wasting
871 * space and resulting in aggregate size changes relative to pass2
872 * when all objects will be in the same input section.
873 *
874 * The build assert will fail if device_handle_t changes size, which
875 * means the alignment directives in the linker scripts and in
876 * `gen_handles.py` must be updated.
877 */
878BUILD_ASSERT(sizeof(device_handle_t) == 2, "fix the linker scripts");
879#define Z_DEVICE_DEFINE_HANDLES(node_id, dev_name, ...) \
880 extern const device_handle_t \
881 Z_DEVICE_HANDLE_NAME(node_id, dev_name)[]; \
882 const device_handle_t \
883 __aligned(sizeof(device_handle_t)) \
884 __attribute__((__weak__, \
885 __section__(".__device_handles_pass1"))) \
886 Z_DEVICE_HANDLE_NAME(node_id, dev_name)[] = { \
887 COND_CODE_1(DT_NODE_EXISTS(node_id), ( \
888 DT_DEP_ORD(node_id), \
889 DT_REQUIRES_DEP_ORDS(node_id) \
890 ), ( \
891 DEVICE_HANDLE_NULL, \
892 )) \
893 DEVICE_HANDLE_SEP, \
894 Z_DEVICE_EXTRA_HANDLES(__VA_ARGS__) \
895 DEVICE_HANDLE_SEP, \
896 COND_CODE_1(DT_NODE_EXISTS(node_id), \
897 (DT_SUPPORTS_DEP_ORDS(node_id)), ()) \
898 };
899
900#define Z_DEVICE_DEFINE_INIT(node_id, dev_name) \
901 .handles = Z_DEVICE_HANDLE_NAME(node_id, dev_name),
902
903/* Like DEVICE_DEFINE but takes a node_id AND a dev_name, and trailing
904 * dependency handles that come from outside devicetree.
905 */
906#define Z_DEVICE_DEFINE(node_id, dev_name, drv_name, init_fn, pm_device,\
907 data_ptr, cfg_ptr, level, prio, api_ptr, state_ptr, ...) \
908 Z_DEVICE_DEFINE_PRE(node_id, dev_name, __VA_ARGS__) \
909 COND_CODE_1(DT_NODE_EXISTS(node_id), (), (static)) \
910 const Z_DECL_ALIGN(struct device) \
911 DEVICE_NAME_GET(dev_name) __used \
912 __attribute__((__section__(".z_device_" #level STRINGIFY(prio)"_"))) = { \
913 .name = drv_name, \
914 .config = (cfg_ptr), \
915 .api = (api_ptr), \
916 .state = (state_ptr), \
917 .data = (data_ptr), \
918 COND_CODE_1(CONFIG_PM_DEVICE, (.pm = pm_device,), ()) \
919 Z_DEVICE_DEFINE_INIT(node_id, dev_name) \
920 }; \
921 BUILD_ASSERT(sizeof(Z_STRINGIFY(drv_name)) <= Z_DEVICE_MAX_NAME_LEN, \
922 Z_STRINGIFY(DEVICE_NAME_GET(drv_name)) " too long"); \
923 Z_INIT_ENTRY_DEFINE(DEVICE_NAME_GET(dev_name), init_fn, \
924 (&DEVICE_NAME_GET(dev_name)), level, prio)
925
926#ifdef __cplusplus
927}
928#endif
929
930/* device_extern is generated based on devicetree nodes */
931#include <device_extern.h>
932
933#include <syscalls/device.h>
934
935#endif /* ZEPHYR_INCLUDE_DEVICE_H_ */
ZTEST_BMEM int count
Definition: main.c:33
volatile int rv
Definition: main.c:45
const struct device * device_get_binding(const char *name)
Get a const struct device* from its name field.
int16_t device_handle_t
Type used to represent a "handle" for a device.
Definition: device.h:53
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:564
static int device_usable_check(const struct device *dev)
Determine whether a device is ready for use.
Definition: device.h:802
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:604
static device_handle_t device_handle_get(const struct device *dev)
Get the handle for a given device.
Definition: device.h:484
#define DEVICE_HANDLE_NULL
Flag value used to identify an unknown device.
Definition: device.h:70
#define DEVICE_HANDLE_SEP
Flag value used in lists of device handles to separate distinct groups.
Definition: device.h:60
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:508
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:541
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
#define DEVICE_HANDLE_ENDS
Flag value used in lists of device handles to indicate the end of the list.
Definition: device.h:67
int device_supported_foreach(const struct device *dev, device_visitor_callback_t visitor_cb, void *context)
Visit every device that dev directly supports.
#define ENODEV
Definition: errno.h:58
static ZTEST_BMEM volatile int ret
Definition: k_float_disable.c:28
Definitions of various linker Sections.
__INT16_TYPE__ int16_t
Definition: stdint.h:43
Runtime device dynamic structure (in RAM) per driver instance.
Definition: device.h:429
bool initialized
Definition: device.h:442
unsigned int init_res
Definition: device.h:437
Runtime device structure (in ROM) per driver instance.
Definition: device.h:450
const char * name
Definition: device.h:452
const void * api
Definition: device.h:456
struct device_state *const state
Definition: device.h:458
const device_handle_t *const handles
Definition: device.h:468
void *const data
Definition: device.h:460
const void * config
Definition: device.h:454
Misc utilities.