1 /*
2 * Copyright (c) 2015-2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stddef.h>
8 #include <string.h>
9 #include <zephyr/device.h>
10 #include <zephyr/sys/atomic.h>
11 #include <zephyr/sys/iterable_sections.h>
12 #include <zephyr/sys/kobject.h>
13 #include <zephyr/internal/syscall_handler.h>
14 #include <zephyr/toolchain.h>
15
16 /**
17 * @brief Initialize state for all static devices.
18 *
19 * The state object is always zero-initialized, but this may not be
20 * sufficient.
21 */
z_device_state_init(void)22 void z_device_state_init(void)
23 {
24 STRUCT_SECTION_FOREACH(device, dev) {
25 k_object_init(dev);
26 }
27 }
28
z_impl_device_get_binding(const char * name)29 const struct device *z_impl_device_get_binding(const char *name)
30 {
31 /* A null string identifies no device. So does an empty
32 * string.
33 */
34 if ((name == NULL) || (name[0] == '\0')) {
35 return NULL;
36 }
37
38 /* Return NULL if the device matching 'name' is not ready. */
39 STRUCT_SECTION_FOREACH(device, dev) {
40 if ((dev->name == name) || (strcmp(name, dev->name) == 0)) {
41 return z_impl_device_is_ready(dev) ? dev : NULL;
42 }
43 }
44
45 return NULL;
46 }
47
48 #ifdef CONFIG_USERSPACE
z_vrfy_device_get_binding(const char * name)49 static inline const struct device *z_vrfy_device_get_binding(const char *name)
50 {
51 char name_copy[Z_DEVICE_MAX_NAME_LEN];
52
53 if (k_usermode_string_copy(name_copy, name, sizeof(name_copy))
54 != 0) {
55 return NULL;
56 }
57
58 return z_impl_device_get_binding(name_copy);
59 }
60 #include <zephyr/syscalls/device_get_binding_mrsh.c>
61
z_vrfy_device_is_ready(const struct device * dev)62 static inline bool z_vrfy_device_is_ready(const struct device *dev)
63 {
64 K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY));
65
66 return z_impl_device_is_ready(dev);
67 }
68 #include <zephyr/syscalls/device_is_ready_mrsh.c>
69 #endif /* CONFIG_USERSPACE */
70
71 #ifdef CONFIG_DEVICE_DT_METADATA
z_impl_device_get_by_dt_nodelabel(const char * nodelabel)72 const struct device *z_impl_device_get_by_dt_nodelabel(const char *nodelabel)
73 {
74 /* For consistency with device_get_binding(). */
75 if ((nodelabel == NULL) || (nodelabel[0] == '\0')) {
76 return NULL;
77 }
78
79 /* Unlike device_get_binding(), which has a history of being
80 * used in application code, we don't expect
81 * device_get_by_dt_nodelabel() to be used outside of
82 * scenarios where a human is in the loop. The shell is the
83 * main expected use case. Therefore, nodelabel is probably
84 * not the same pointer as any of the entry->nodelabel
85 * elements. We therefore skip the pointer comparison that
86 * device_get_binding() does.
87 */
88 STRUCT_SECTION_FOREACH(device, dev) {
89 const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev);
90
91 if (!z_impl_device_is_ready(dev) || nl == NULL) {
92 continue;
93 }
94
95 for (size_t i = 0; i < nl->num_nodelabels; i++) {
96 const char *dev_nodelabel = nl->nodelabels[i];
97
98 if (strcmp(nodelabel, dev_nodelabel) == 0) {
99 return dev;
100 }
101 }
102 }
103
104 return NULL;
105 }
106
107 #ifdef CONFIG_USERSPACE
z_vrfy_device_get_by_dt_nodelabel(const char * nodelabel)108 static inline const struct device *z_vrfy_device_get_by_dt_nodelabel(const char *nodelabel)
109 {
110 char nl_copy[Z_DEVICE_MAX_NODELABEL_LEN];
111
112 if (k_usermode_string_copy(nl_copy, (char *)nodelabel, sizeof(nl_copy)) != 0) {
113 return NULL;
114 }
115
116 return z_impl_device_get_by_dt_nodelabel(nl_copy);
117 }
118 #include <zephyr/syscalls/device_get_by_dt_nodelabel_mrsh.c>
119 #endif /* CONFIG_USERSPACE */
120 #endif /* CONFIG_DEVICE_DT_METADATA */
121
z_device_get_all_static(struct device const ** devices)122 size_t z_device_get_all_static(struct device const **devices)
123 {
124 size_t cnt;
125
126 STRUCT_SECTION_GET(device, 0, devices);
127 STRUCT_SECTION_COUNT(device, &cnt);
128
129 return cnt;
130 }
131
z_impl_device_is_ready(const struct device * dev)132 bool z_impl_device_is_ready(const struct device *dev)
133 {
134 /*
135 * if an invalid device pointer is passed as argument, this call
136 * reports the `device` as not ready for usage.
137 */
138 if (dev == NULL) {
139 return false;
140 }
141
142 return dev->state->initialized && (dev->state->init_res == 0U);
143 }
144
145 #ifdef CONFIG_DEVICE_DEPS
146
device_visitor(const device_handle_t * handles,size_t handle_count,device_visitor_callback_t visitor_cb,void * context)147 static int device_visitor(const device_handle_t *handles,
148 size_t handle_count,
149 device_visitor_callback_t visitor_cb,
150 void *context)
151 {
152 /* Iterate over fixed devices */
153 for (size_t i = 0; i < handle_count; ++i) {
154 device_handle_t dh = handles[i];
155 const struct device *rdev = device_from_handle(dh);
156 int rc = visitor_cb(rdev, context);
157
158 if (rc < 0) {
159 return rc;
160 }
161 }
162
163 return handle_count;
164 }
165
device_required_foreach(const struct device * dev,device_visitor_callback_t visitor_cb,void * context)166 int device_required_foreach(const struct device *dev,
167 device_visitor_callback_t visitor_cb,
168 void *context)
169 {
170 size_t handle_count = 0;
171 const device_handle_t *handles = device_required_handles_get(dev, &handle_count);
172
173 return device_visitor(handles, handle_count, visitor_cb, context);
174 }
175
device_supported_foreach(const struct device * dev,device_visitor_callback_t visitor_cb,void * context)176 int device_supported_foreach(const struct device *dev,
177 device_visitor_callback_t visitor_cb,
178 void *context)
179 {
180 size_t handle_count = 0;
181 const device_handle_t *handles = device_supported_handles_get(dev, &handle_count);
182
183 return device_visitor(handles, handle_count, visitor_cb, context);
184 }
185
186 #endif /* CONFIG_DEVICE_DEPS */
187