Zephyr Project API  3.2.0
A Scalable Open Source RTOS
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
38#include <zephyr/devicetree.h>
39#include <zephyr/init.h>
42#include <zephyr/sys/util.h>
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
64
70#define DEVICE_HANDLE_SEP INT16_MIN
71
77#define DEVICE_HANDLE_ENDS INT16_MAX
78
80#define DEVICE_HANDLE_NULL 0
81
82#define Z_DEVICE_MAX_NAME_LEN 48
83
104#define DEVICE_NAME_GET(name) _CONCAT(__device_, name)
105
106/* Node paths can exceed the maximum size supported by
107 * device_get_binding() in user mode; this macro synthesizes a unique
108 * dev_name from a devicetree node while staying within this maximum
109 * size.
110 *
111 * The ordinal used in this name can be mapped to the path by
112 * examining zephyr/include/generated/devicetree_generated.h.
113 */
114#define Z_DEVICE_DT_DEV_NAME(node_id) _CONCAT(dts_ord_, DT_DEP_ORD(node_id))
115
116/* Synthesize a unique name for the device state associated with
117 * dev_name.
118 */
119#define Z_DEVICE_STATE_NAME(dev_name) _CONCAT(__devstate_, dev_name)
120
162#define DEVICE_DEFINE(dev_name, drv_name, init_fn, pm_device, \
163 data_ptr, cfg_ptr, level, prio, api_ptr) \
164 Z_DEVICE_STATE_DEFINE(DT_INVALID_NODE, dev_name) \
165 Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_name, drv_name, init_fn, \
166 pm_device, \
167 data_ptr, cfg_ptr, level, prio, api_ptr, \
168 &Z_DEVICE_STATE_NAME(dev_name))
169
181#define DEVICE_DT_NAME(node_id) \
182 DT_PROP_OR(node_id, label, DT_NODE_FULL_NAME(node_id))
183
225#define DEVICE_DT_DEFINE(node_id, init_fn, pm_device, \
226 data_ptr, cfg_ptr, level, prio, \
227 api_ptr, ...) \
228 Z_DEVICE_STATE_DEFINE(node_id, Z_DEVICE_DT_DEV_NAME(node_id)) \
229 Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_NAME(node_id), \
230 DEVICE_DT_NAME(node_id), init_fn, \
231 pm_device, \
232 data_ptr, cfg_ptr, level, prio, \
233 api_ptr, \
234 &Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_NAME(node_id)), \
235 __VA_ARGS__)
236
246#define DEVICE_DT_INST_DEFINE(inst, ...) \
247 DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
248
264#define DEVICE_DT_NAME_GET(node_id) DEVICE_NAME_GET(Z_DEVICE_DT_DEV_NAME(node_id))
265
282#define DEVICE_DT_GET(node_id) (&DEVICE_DT_NAME_GET(node_id))
283
293#define DEVICE_DT_INST_GET(inst) DEVICE_DT_GET(DT_DRV_INST(inst))
294
311#define DEVICE_DT_GET_ANY(compat) \
312 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
313 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
314 (NULL))
315
333#define DEVICE_DT_GET_ONE(compat) \
334 COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
335 (DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
336 (ZERO_OR_COMPILE_ERROR(0)))
337
350#define DEVICE_DT_GET_OR_NULL(node_id) \
351 COND_CODE_1(DT_NODE_HAS_STATUS(node_id, okay), \
352 (DEVICE_DT_GET(node_id)), (NULL))
353
364#define DEVICE_GET(name) (&DEVICE_NAME_GET(name))
365
380#define DEVICE_DECLARE(name) static const struct device DEVICE_NAME_GET(name)
381
389#define DEVICE_INIT_DT_GET(node_id) (&Z_INIT_ENTRY_NAME(DEVICE_DT_NAME_GET(node_id)))
390
398#define DEVICE_INIT_GET(name) (&Z_INIT_ENTRY_NAME(DEVICE_NAME_GET(name)))
399
416 unsigned int init_res : 8;
417
421 bool initialized : 1;
422};
423
424struct pm_device;
425
426#ifdef CONFIG_HAS_DYNAMIC_DEVICE_HANDLES
427#define Z_DEVICE_HANDLES_CONST
428#else
429#define Z_DEVICE_HANDLES_CONST const
430#endif
431
435struct device {
437 const char *name;
439 const void *config;
441 const void *api;
445 void *data;
453 Z_DEVICE_HANDLES_CONST device_handle_t *handles;
454
455#ifdef CONFIG_PM_DEVICE
457 struct pm_device *pm;
458#endif
459};
460
469static inline device_handle_t
470device_handle_get(const struct device *dev)
471{
473 extern const struct device __device_start[];
474
475 /* TODO: If/when devices can be constructed that are not part of the
476 * fixed sequence we'll need another solution.
477 */
478 if (dev != NULL) {
479 ret = 1 + (device_handle_t)(dev - __device_start);
480 }
481
482 return ret;
483}
484
493static inline const struct device *
495{
496 extern const struct device __device_start[];
497 extern const struct device __device_end[];
498 const struct device *dev = NULL;
499 size_t numdev = __device_end - __device_start;
500
501 if ((dev_handle > 0) && ((size_t)dev_handle <= numdev)) {
502 dev = &__device_start[dev_handle - 1];
503 }
504
505 return dev;
506}
507
527typedef int (*device_visitor_callback_t)(const struct device *dev, void *context);
528
549static inline const device_handle_t *
551 size_t *count)
552{
553 const device_handle_t *rv = dev->handles;
554
555 if (rv != NULL) {
556 size_t i = 0;
557
558 while ((rv[i] != DEVICE_HANDLE_ENDS)
559 && (rv[i] != DEVICE_HANDLE_SEP)) {
560 ++i;
561 }
562 *count = i;
563 }
564
565 return rv;
566}
567
588static inline const device_handle_t *
590 size_t *count)
591{
592 const device_handle_t *rv = dev->handles;
593 size_t region = 0;
594 size_t i = 0;
595
596 if (rv != NULL) {
597 /* Fast forward to injected devices */
598 while (region != 1) {
599 if (*rv == DEVICE_HANDLE_SEP) {
600 region++;
601 }
602 rv++;
603 }
604 while ((rv[i] != DEVICE_HANDLE_ENDS)
605 && (rv[i] != DEVICE_HANDLE_SEP)) {
606 ++i;
607 }
608 *count = i;
609 }
610
611 return rv;
612}
613
635static inline const device_handle_t *
637 size_t *count)
638{
639 const device_handle_t *rv = dev->handles;
640 size_t region = 0;
641 size_t i = 0;
642
643 if (rv != NULL) {
644 /* Fast forward to supporting devices */
645 while (region != 2) {
646 if (*rv == DEVICE_HANDLE_SEP) {
647 region++;
648 }
649 rv++;
650 }
651 /* Count supporting devices */
652 while (rv[i] != DEVICE_HANDLE_ENDS) {
653 ++i;
654 }
655 *count = i;
656 }
657
658 return rv;
659}
660
694int device_required_foreach(const struct device *dev,
695 device_visitor_callback_t visitor_cb,
696 void *context);
697
730int device_supported_foreach(const struct device *dev,
731 device_visitor_callback_t visitor_cb,
732 void *context);
733
756__syscall const struct device *device_get_binding(const char *name);
757
766size_t z_device_get_all_static(const struct device * *devices);
767
782bool z_device_is_ready(const struct device *dev);
783
799__syscall bool device_is_ready(const struct device *dev);
800
801static inline bool z_impl_device_is_ready(const struct device *dev)
802{
803 return z_device_is_ready(dev);
804}
805
810/* Synthesize the name of the object that holds device ordinal and
811 * dependency data. If the object doesn't come from a devicetree
812 * node, use dev_name.
813 */
814#define Z_DEVICE_HANDLE_NAME(node_id, dev_name) \
815 _CONCAT(__devicehdl_, \
816 COND_CODE_1(DT_NODE_EXISTS(node_id), \
817 (node_id), \
818 (dev_name)))
819
820#define Z_DEVICE_EXTRA_HANDLES(...) \
821 FOR_EACH_NONEMPTY_TERM(IDENTITY, (,), __VA_ARGS__)
822
823/*
824 * Utility macro to define and initialize the device state.
825 *
826 * @param node_id Devicetree node id of the device.
827 * @param dev_name Device name.
828 */
829#define Z_DEVICE_STATE_DEFINE(node_id, dev_name) \
830 static struct device_state Z_DEVICE_STATE_NAME(dev_name) \
831 __attribute__((__section__(".z_devstate")));
832
833/* Construct objects that are referenced from struct device. These
834 * include power management and dependency handles.
835 */
836#define Z_DEVICE_DEFINE_PRE(node_id, dev_name, ...) \
837 Z_DEVICE_DEFINE_HANDLES(node_id, dev_name, __VA_ARGS__)
838
839/* Initial build provides a record that associates the device object
840 * with its devicetree ordinal, and provides the dependency ordinals.
841 * These are provided as weak definitions (to prevent the reference
842 * from being captured when the original object file is compiled), and
843 * in a distinct pass1 section (which will be replaced by
844 * postprocessing).
845 *
846 * Before processing in gen_handles.py, the array format is:
847 * {
848 * DEVICE_ORDINAL (or DEVICE_HANDLE_NULL if not a devicetree node),
849 * List of devicetree dependency ordinals (if any),
850 * DEVICE_HANDLE_SEP,
851 * List of injected dependency ordinals (if any),
852 * DEVICE_HANDLE_SEP,
853 * List of devicetree supporting ordinals (if any),
854 * }
855 *
856 * After processing in gen_handles.py, the format is updated to:
857 * {
858 * List of existing devicetree dependency handles (if any),
859 * DEVICE_HANDLE_SEP,
860 * List of injected devicetree dependency handles (if any),
861 * DEVICE_HANDLE_SEP,
862 * List of existing devicetree support handles (if any),
863 * DEVICE_HANDLE_NULL
864 * }
865 *
866 * It is also (experimentally) necessary to provide explicit alignment
867 * on each object. Otherwise x86-64 builds will introduce padding
868 * between objects in the same input section in individual object
869 * files, which will be retained in subsequent links both wasting
870 * space and resulting in aggregate size changes relative to pass2
871 * when all objects will be in the same input section.
872 *
873 * The build assert will fail if device_handle_t changes size, which
874 * means the alignment directives in the linker scripts and in
875 * `gen_handles.py` must be updated.
876 */
877BUILD_ASSERT(sizeof(device_handle_t) == 2, "fix the linker scripts");
878#define Z_DEVICE_DEFINE_HANDLES(node_id, dev_name, ...) \
879 extern Z_DEVICE_HANDLES_CONST device_handle_t \
880 Z_DEVICE_HANDLE_NAME(node_id, dev_name)[]; \
881 Z_DEVICE_HANDLES_CONST device_handle_t \
882 __aligned(sizeof(device_handle_t)) \
883 __attribute__((__weak__, \
884 __section__(".__device_handles_pass1"))) \
885 Z_DEVICE_HANDLE_NAME(node_id, dev_name)[] = { \
886 COND_CODE_1(DT_NODE_EXISTS(node_id), ( \
887 DT_DEP_ORD(node_id), \
888 DT_REQUIRES_DEP_ORDS(node_id) \
889 ), ( \
890 DEVICE_HANDLE_NULL, \
891 )) \
892 DEVICE_HANDLE_SEP, \
893 Z_DEVICE_EXTRA_HANDLES(__VA_ARGS__) \
894 DEVICE_HANDLE_SEP, \
895 COND_CODE_1(DT_NODE_EXISTS(node_id), \
896 (DT_SUPPORTS_DEP_ORDS(node_id)), ()) \
897 };
898
899#define Z_DEVICE_DEFINE_INIT(node_id, dev_name) \
900 .handles = Z_DEVICE_HANDLE_NAME(node_id, dev_name),
901
902/* Like DEVICE_DEFINE but takes a node_id AND a dev_name, and trailing
903 * dependency handles that come from outside devicetree.
904 */
905#define Z_DEVICE_DEFINE(node_id, dev_name, drv_name, init_fn, pm_device,\
906 data_ptr, cfg_ptr, level, prio, api_ptr, state_ptr, ...) \
907 Z_DEVICE_DEFINE_PRE(node_id, dev_name, __VA_ARGS__) \
908 COND_CODE_1(DT_NODE_EXISTS(node_id), (), (static)) \
909 const Z_DECL_ALIGN(struct device) \
910 DEVICE_NAME_GET(dev_name) __used \
911 __attribute__((__section__(".z_device_" #level STRINGIFY(prio)"_"))) = { \
912 .name = drv_name, \
913 .config = (cfg_ptr), \
914 .api = (api_ptr), \
915 .state = (state_ptr), \
916 .data = (data_ptr), \
917 IF_ENABLED(CONFIG_PM_DEVICE, (.pm = pm_device,)) \
918 Z_DEVICE_DEFINE_INIT(node_id, dev_name) \
919 }; \
920 BUILD_ASSERT(sizeof(Z_STRINGIFY(drv_name)) <= Z_DEVICE_MAX_NAME_LEN, \
921 Z_STRINGIFY(DEVICE_NAME_GET(drv_name)) " too long"); \
922 Z_INIT_ENTRY_DEFINE(DEVICE_NAME_GET(dev_name), init_fn, \
923 (&DEVICE_NAME_GET(dev_name)), level, prio)
924
925#if CONFIG_HAS_DTS
926/*
927 * Declare a device for each status "okay" devicetree node. (Disabled
928 * nodes should not result in devices, so not predeclaring these keeps
929 * drivers honest.)
930 *
931 * This is only "maybe" a device because some nodes have status "okay",
932 * but don't have a corresponding struct device allocated. There's no way
933 * to figure that out until after we've built the zephyr image,
934 * though.
935 */
936#define Z_MAYBE_DEVICE_DECLARE_INTERNAL(node_id) \
937 extern const struct device DEVICE_DT_NAME_GET(node_id);
938
939DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_DEVICE_DECLARE_INTERNAL)
940#endif /* CONFIG_HAS_DTS */
941
942#ifdef __cplusplus
943}
944#endif
945
946
947#include <syscalls/device.h>
948
949#endif /* ZEPHYR_INCLUDE_DEVICE_H_ */
ZTEST_BMEM int count
Definition: main.c:33
Devicetree main header.
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:63
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:550
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:636
static device_handle_t device_handle_get(const struct device *dev)
Get the handle for a given device.
Definition: device.h:470
#define DEVICE_HANDLE_NULL
Flag value used to identify an unknown device.
Definition: device.h:80
#define DEVICE_HANDLE_SEP
Flag value used in lists of device handles to separate distinct groups.
Definition: device.h:70
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:494
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:527
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:77
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:589
int device_supported_foreach(const struct device *dev, device_visitor_callback_t visitor_cb, void *context)
Visit every device that dev directly supports.
DT_FOREACH_STATUS_OKAY_NODE(Z_MAYBE_EMUL_DECLARE_INTERNAL)
static ZTEST_BMEM volatile int ret
Definition: k_float_disable.c:28
Definitions of various linker Sections.
__INT16_TYPE__ int16_t
Definition: stdint.h:73
Runtime device dynamic structure (in RAM) per driver instance.
Definition: device.h:408
bool initialized
Definition: device.h:421
unsigned int init_res
Definition: device.h:416
Runtime device structure (in ROM) per driver instance.
Definition: device.h:435
const char * name
Definition: device.h:437
void * data
Definition: device.h:445
const device_handle_t * handles
Definition: device.h:453
const void * api
Definition: device.h:441
struct device_state * state
Definition: device.h:443
const void * config
Definition: device.h:439
Misc utilities.