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