1 /*
2 * Copyright (c) 2015-2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <string.h>
8 #include <zephyr/device.h>
9 #include <zephyr/sys/atomic.h>
10 #include <zephyr/sys/iterable_sections.h>
11 #include <zephyr/sys/kobject.h>
12 #include <zephyr/syscall_handler.h>
13 #include <zephyr/toolchain.h>
14
15 /**
16 * @brief Initialize state for all static devices.
17 *
18 * The state object is always zero-initialized, but this may not be
19 * sufficient.
20 */
z_device_state_init(void)21 void z_device_state_init(void)
22 {
23 STRUCT_SECTION_FOREACH(device, dev) {
24 z_object_init(dev);
25 }
26 }
27
z_impl_device_get_binding(const char * name)28 const struct device *z_impl_device_get_binding(const char *name)
29 {
30 /* A null string identifies no device. So does an empty
31 * string.
32 */
33 if ((name == NULL) || (name[0] == '\0')) {
34 return NULL;
35 }
36
37 /* Split the search into two loops: in the common scenario, where
38 * device names are stored in ROM (and are referenced by the user
39 * with CONFIG_* macros), only cheap pointer comparisons will be
40 * performed. Reserve string comparisons for a fallback.
41 */
42 STRUCT_SECTION_FOREACH(device, dev) {
43 if (z_device_is_ready(dev) && (dev->name == name)) {
44 return dev;
45 }
46 }
47
48 STRUCT_SECTION_FOREACH(device, dev) {
49 if (z_device_is_ready(dev) && (strcmp(name, dev->name) == 0)) {
50 return dev;
51 }
52 }
53
54 return NULL;
55 }
56
57 #ifdef CONFIG_USERSPACE
z_vrfy_device_get_binding(const char * name)58 static inline const struct device *z_vrfy_device_get_binding(const char *name)
59 {
60 char name_copy[Z_DEVICE_MAX_NAME_LEN];
61
62 if (z_user_string_copy(name_copy, (char *)name, sizeof(name_copy))
63 != 0) {
64 return NULL;
65 }
66
67 return z_impl_device_get_binding(name_copy);
68 }
69 #include <syscalls/device_get_binding_mrsh.c>
70
z_vrfy_device_is_ready(const struct device * dev)71 static inline bool z_vrfy_device_is_ready(const struct device *dev)
72 {
73 Z_OOPS(Z_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY));
74
75 return z_impl_device_is_ready(dev);
76 }
77 #include <syscalls/device_is_ready_mrsh.c>
78 #endif /* CONFIG_USERSPACE */
79
z_device_get_all_static(struct device const ** devices)80 size_t z_device_get_all_static(struct device const **devices)
81 {
82 size_t cnt;
83
84 STRUCT_SECTION_GET(device, 0, devices);
85 STRUCT_SECTION_COUNT(device, &cnt);
86
87 return cnt;
88 }
89
z_device_is_ready(const struct device * dev)90 bool z_device_is_ready(const struct device *dev)
91 {
92 /*
93 * if an invalid device pointer is passed as argument, this call
94 * reports the `device` as not ready for usage.
95 */
96 if (dev == NULL) {
97 return false;
98 }
99
100 return dev->state->initialized && (dev->state->init_res == 0U);
101 }
102
103 #ifdef CONFIG_DEVICE_DEPS
104
device_visitor(const device_handle_t * handles,size_t handle_count,device_visitor_callback_t visitor_cb,void * context)105 static int device_visitor(const device_handle_t *handles,
106 size_t handle_count,
107 device_visitor_callback_t visitor_cb,
108 void *context)
109 {
110 /* Iterate over fixed devices */
111 for (size_t i = 0; i < handle_count; ++i) {
112 device_handle_t dh = handles[i];
113 const struct device *rdev = device_from_handle(dh);
114 int rc = visitor_cb(rdev, context);
115
116 if (rc < 0) {
117 return rc;
118 }
119 }
120
121 return handle_count;
122 }
123
device_required_foreach(const struct device * dev,device_visitor_callback_t visitor_cb,void * context)124 int device_required_foreach(const struct device *dev,
125 device_visitor_callback_t visitor_cb,
126 void *context)
127 {
128 size_t handle_count = 0;
129 const device_handle_t *handles = device_required_handles_get(dev, &handle_count);
130
131 return device_visitor(handles, handle_count, visitor_cb, context);
132 }
133
device_supported_foreach(const struct device * dev,device_visitor_callback_t visitor_cb,void * context)134 int device_supported_foreach(const struct device *dev,
135 device_visitor_callback_t visitor_cb,
136 void *context)
137 {
138 size_t handle_count = 0;
139 const device_handle_t *handles = device_supported_handles_get(dev, &handle_count);
140
141 return device_visitor(handles, handle_count, visitor_cb, context);
142 }
143
144 #endif /* CONFIG_DEVICE_DEPS */
145