1 /*
2  * Copyright (c) 2015-2016 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <device.h>
9 #include <sys/atomic.h>
10 #include <syscall_handler.h>
11 
12 extern const struct init_entry __init_start[];
13 extern const struct init_entry __init_PRE_KERNEL_1_start[];
14 extern const struct init_entry __init_PRE_KERNEL_2_start[];
15 extern const struct init_entry __init_POST_KERNEL_start[];
16 extern const struct init_entry __init_APPLICATION_start[];
17 extern const struct init_entry __init_end[];
18 
19 #ifdef CONFIG_SMP
20 extern const struct init_entry __init_SMP_start[];
21 #endif
22 
23 extern const struct device __device_start[];
24 extern const struct device __device_end[];
25 
26 extern uint32_t __device_init_status_start[];
27 
28 /**
29  * @brief Initialize state for all static devices.
30  *
31  * The state object is always zero-initialized, but this may not be
32  * sufficient.
33  */
z_device_state_init(void)34 void z_device_state_init(void)
35 {
36 	const struct device *dev = __device_start;
37 
38 	while (dev < __device_end) {
39 		z_object_init(dev);
40 		++dev;
41 	}
42 }
43 
44 /**
45  * @brief Execute all the init entry initialization functions at a given level
46  *
47  * @details Invokes the initialization routine for each init entry object
48  * created by the INIT_ENTRY_DEFINE() macro using the specified level.
49  * The linker script places the init entry objects in memory in the order
50  * they need to be invoked, with symbols indicating where one level leaves
51  * off and the next one begins.
52  *
53  * @param level init level to run.
54  */
z_sys_init_run_level(int32_t level)55 void z_sys_init_run_level(int32_t level)
56 {
57 	static const struct init_entry *levels[] = {
58 		__init_PRE_KERNEL_1_start,
59 		__init_PRE_KERNEL_2_start,
60 		__init_POST_KERNEL_start,
61 		__init_APPLICATION_start,
62 #ifdef CONFIG_SMP
63 		__init_SMP_start,
64 #endif
65 		/* End marker */
66 		__init_end,
67 	};
68 	const struct init_entry *entry;
69 
70 	for (entry = levels[level]; entry < levels[level+1]; entry++) {
71 		const struct device *dev = entry->dev;
72 		int rc = entry->init(dev);
73 
74 		if (dev != NULL) {
75 			/* Mark device initialized.  If initialization
76 			 * failed, record the error condition.
77 			 */
78 			if (rc != 0) {
79 				if (rc < 0) {
80 					rc = -rc;
81 				}
82 				if (rc > UINT8_MAX) {
83 					rc = UINT8_MAX;
84 				}
85 				dev->state->init_res = rc;
86 			}
87 			dev->state->initialized = true;
88 		}
89 	}
90 }
91 
z_impl_device_get_binding(const char * name)92 const struct device *z_impl_device_get_binding(const char *name)
93 {
94 	const struct device *dev;
95 
96 	/* A null string identifies no device.  So does an empty
97 	 * string.
98 	 */
99 	if ((name == NULL) || (name[0] == '\0')) {
100 		return NULL;
101 	}
102 
103 	/* Split the search into two loops: in the common scenario, where
104 	 * device names are stored in ROM (and are referenced by the user
105 	 * with CONFIG_* macros), only cheap pointer comparisons will be
106 	 * performed. Reserve string comparisons for a fallback.
107 	 */
108 	for (dev = __device_start; dev != __device_end; dev++) {
109 		if (z_device_ready(dev) && (dev->name == name)) {
110 			return dev;
111 		}
112 	}
113 
114 	for (dev = __device_start; dev != __device_end; dev++) {
115 		if (z_device_ready(dev) && (strcmp(name, dev->name) == 0)) {
116 			return dev;
117 		}
118 	}
119 
120 	return NULL;
121 }
122 
123 #ifdef CONFIG_USERSPACE
z_vrfy_device_get_binding(const char * name)124 static inline const struct device *z_vrfy_device_get_binding(const char *name)
125 {
126 	char name_copy[Z_DEVICE_MAX_NAME_LEN];
127 
128 	if (z_user_string_copy(name_copy, (char *)name, sizeof(name_copy))
129 	    != 0) {
130 		return NULL;
131 	}
132 
133 	return z_impl_device_get_binding(name_copy);
134 }
135 #include <syscalls/device_get_binding_mrsh.c>
136 
z_vrfy_device_usable_check(const struct device * dev)137 static inline int z_vrfy_device_usable_check(const struct device *dev)
138 {
139 	Z_OOPS(Z_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY));
140 
141 	return z_impl_device_usable_check(dev);
142 }
143 #include <syscalls/device_usable_check_mrsh.c>
144 #endif /* CONFIG_USERSPACE */
145 
z_device_get_all_static(struct device const ** devices)146 size_t z_device_get_all_static(struct device const **devices)
147 {
148 	*devices = __device_start;
149 	return __device_end - __device_start;
150 }
151 
z_device_ready(const struct device * dev)152 bool z_device_ready(const struct device *dev)
153 {
154 	/*
155 	 * if an invalid device pointer is passed as argument, this call
156 	 * reports the `device` as not ready for usage.
157 	 */
158 	if (dev == NULL) {
159 		return false;
160 	}
161 
162 	return dev->state->initialized && (dev->state->init_res == 0U);
163 }
164 
device_required_foreach(const struct device * dev,device_visitor_callback_t visitor_cb,void * context)165 int device_required_foreach(const struct device *dev,
166 			  device_visitor_callback_t visitor_cb,
167 			  void *context)
168 {
169 	size_t handle_count = 0;
170 	const device_handle_t *handles =
171 		device_required_handles_get(dev, &handle_count);
172 
173 	/* Iterate over fixed devices */
174 	for (size_t i = 0; i < handle_count; ++i) {
175 		device_handle_t dh = handles[i];
176 		const struct device *rdev = device_from_handle(dh);
177 		int rc = visitor_cb(rdev, context);
178 
179 		if (rc < 0) {
180 			return rc;
181 		}
182 	}
183 
184 	return handle_count;
185 }
186