1 |
|
|
/*************************************************************************** |
2 |
|
|
* Copyright (c) 2024 Microsoft Corporation |
3 |
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors |
4 |
|
|
* |
5 |
|
|
* This program and the accompanying materials are made available under the |
6 |
|
|
* terms of the MIT License which is available at |
7 |
|
|
* https://opensource.org/licenses/MIT. |
8 |
|
|
* |
9 |
|
|
* SPDX-License-Identifier: MIT |
10 |
|
|
**************************************************************************/ |
11 |
|
|
|
12 |
|
|
|
13 |
|
|
/**************************************************************************/ |
14 |
|
|
/**************************************************************************/ |
15 |
|
|
/** */ |
16 |
|
|
/** USBX Component */ |
17 |
|
|
/** */ |
18 |
|
|
/** System */ |
19 |
|
|
/** */ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
/**************************************************************************/ |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
/* Define UX_SYSTEM_INIT to bring in the USBX version ID string. */ |
25 |
|
|
|
26 |
|
|
#define UX_SYSTEM_INIT |
27 |
|
|
|
28 |
|
|
|
29 |
|
|
/* Include necessary system files. */ |
30 |
|
|
|
31 |
|
|
#define UX_SOURCE_CODE |
32 |
|
|
|
33 |
|
|
#include "ux_api.h" |
34 |
|
|
#include "ux_system.h" |
35 |
|
|
|
36 |
|
|
/* Define the USBX system data structure. */ |
37 |
|
|
|
38 |
|
|
UX_SYSTEM *_ux_system; |
39 |
|
|
UX_SYSTEM_OTG *_ux_system_otg; |
40 |
|
|
|
41 |
|
|
/* Define names of all the packed descriptors in USBX. */ |
42 |
|
|
|
43 |
|
|
UCHAR _ux_system_endpoint_descriptor_structure[] = {1,1,1,1,2,1 }; |
44 |
|
|
UCHAR _ux_system_device_descriptor_structure[] = {1,1,2,1,1,1,1,2,2,2,1,1,1,1}; |
45 |
|
|
UCHAR _ux_system_configuration_descriptor_structure[] = {1,1,2,1,1,1,1,1}; |
46 |
|
|
UCHAR _ux_system_interface_descriptor_structure[] = {1,1,1,1,1,1,1,1,1}; |
47 |
|
|
UCHAR _ux_system_interface_association_descriptor_structure[] = {1,1,1,1,1,1,1,1}; |
48 |
|
|
UCHAR _ux_system_string_descriptor_structure[] = {1,1,2}; |
49 |
|
|
UCHAR _ux_system_dfu_functional_descriptor_structure[] = {1,1,1,2,2,2}; |
50 |
|
|
UCHAR _ux_system_class_audio_interface_descriptor_structure[] = {1,1,1,1,1,1,1,1}; |
51 |
|
|
UCHAR _ux_system_class_audio_input_terminal_descriptor_structure[] = {1,1,1,1,2,1,1,2,1,1}; |
52 |
|
|
UCHAR _ux_system_class_audio_output_terminal_descriptor_structure[] = {1,1,1,1,2,1,1,1}; |
53 |
|
|
UCHAR _ux_system_class_audio_feature_unit_descriptor_structure[] = {1,1,1,1,1,1,1}; |
54 |
|
|
UCHAR _ux_system_class_audio_streaming_interface_descriptor_structure[] = {1,1,1,1,1,1}; |
55 |
|
|
UCHAR _ux_system_class_audio_streaming_endpoint_descriptor_structure[] = {1,1,1,1,1,1}; |
56 |
|
|
UCHAR _ux_system_hub_descriptor_structure[] = {1,1,1,2,1,1,1,1}; |
57 |
|
|
UCHAR _ux_system_hid_descriptor_structure[] = {1,1,2,1,1,1,2}; |
58 |
|
|
UCHAR _ux_system_class_pima_storage_structure[] = {2,2,2,4,4,4,4,4}; |
59 |
|
|
UCHAR _ux_system_class_pima_object_structure[] = {4,2,2,4,2,4,4,4,4,4,4,4,2,4,4}; |
60 |
|
|
UCHAR _ux_system_ecm_interface_descriptor_structure[] = {1,1,1,1,4,2,2,1}; |
61 |
|
|
|
62 |
|
|
UCHAR _ux_system_bos_descriptor_structure[] = {1,1,2,1}; |
63 |
|
|
UCHAR _ux_system_usb_2_0_extension_descriptor_structure[] = {1,1,1,4}; |
64 |
|
|
UCHAR _ux_system_container_id_descriptor_structure[] = {1,1,1,1,4,4,4,4}; |
65 |
|
|
|
66 |
|
|
|
67 |
|
|
/**************************************************************************/ |
68 |
|
|
/* */ |
69 |
|
|
/* FUNCTION RELEASE */ |
70 |
|
|
/* */ |
71 |
|
|
/* _ux_system_initialize PORTABLE C */ |
72 |
|
|
/* 6.3.0 */ |
73 |
|
|
/* AUTHOR */ |
74 |
|
|
/* */ |
75 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
76 |
|
|
/* */ |
77 |
|
|
/* DESCRIPTION */ |
78 |
|
|
/* */ |
79 |
|
|
/* This function initializes the various control data structures for */ |
80 |
|
|
/* the USBX system. */ |
81 |
|
|
/* */ |
82 |
|
|
/* INPUT */ |
83 |
|
|
/* */ |
84 |
|
|
/* regular_memory_pool_start Start of non cached memory pool */ |
85 |
|
|
/* regular_memory_size Size of non cached memory pool */ |
86 |
|
|
/* cache_safe_memory_pool_start Start of cached memory pool */ |
87 |
|
|
/* cache_safe_memory_size Size of cached memory pool */ |
88 |
|
|
/* */ |
89 |
|
|
/* OUTPUT */ |
90 |
|
|
/* */ |
91 |
|
|
/* None */ |
92 |
|
|
/* */ |
93 |
|
|
/* CALLS */ |
94 |
|
|
/* */ |
95 |
|
|
/* _ux_utility_memory_allocate Allocate memory */ |
96 |
|
|
/* _ux_utility_memory_set Set memory */ |
97 |
|
|
/* _ux_utility_mutex_create Create mutex */ |
98 |
|
|
/* */ |
99 |
|
|
/* CALLED BY */ |
100 |
|
|
/* */ |
101 |
|
|
/* Application */ |
102 |
|
|
/* */ |
103 |
|
|
/**************************************************************************/ |
104 |
|
406 |
UINT _ux_system_initialize(VOID *regular_memory_pool_start, ULONG regular_memory_size, |
105 |
|
|
VOID *cache_safe_memory_pool_start, ULONG cache_safe_memory_size) |
106 |
|
|
{ |
107 |
|
|
ALIGN_TYPE int_memory_pool_start; |
108 |
|
|
VOID *regular_memory_pool_end; |
109 |
|
|
VOID *cache_safe_memory_pool_end; |
110 |
|
|
ULONG memory_pool_offset; |
111 |
|
|
#if !defined(UX_STANDALONE) |
112 |
|
|
UINT status; |
113 |
|
|
#endif |
114 |
|
|
ULONG pool_size; |
115 |
|
|
|
116 |
|
|
/* Check if the regular memory pool is valid. */ |
117 |
✓✗✗✓
|
406 |
if ((regular_memory_pool_start == UX_NULL) || (regular_memory_size == 0)) |
118 |
|
|
return(UX_INVALID_PARAMETER); |
119 |
|
|
|
120 |
|
|
/* Reset memory block */ |
121 |
|
406 |
_ux_utility_memory_set(regular_memory_pool_start, 0, regular_memory_size); /* Use case of memset is verified. */ |
122 |
|
|
|
123 |
|
|
/* Set the _ux_system structure at the start of our regular memory */ |
124 |
|
406 |
_ux_system = (UX_SYSTEM *) regular_memory_pool_start; |
125 |
|
|
|
126 |
|
|
/* Add to the memory offset the size of the allocated block. */ |
127 |
|
406 |
memory_pool_offset = sizeof(UX_SYSTEM); |
128 |
|
|
|
129 |
|
|
#ifndef UX_DEVICE_SIDE_ONLY |
130 |
|
|
|
131 |
|
|
/* Set the _ux_system_host structure. */ |
132 |
|
406 |
_ux_system_host = (UX_SYSTEM_HOST *) (((UCHAR *) regular_memory_pool_start) + memory_pool_offset); |
133 |
|
|
|
134 |
|
|
/* Add to the memory offset the size of the allocated block. */ |
135 |
|
406 |
memory_pool_offset += (ULONG)sizeof(UX_SYSTEM_HOST); |
136 |
|
|
|
137 |
|
|
#endif |
138 |
|
|
|
139 |
|
|
#ifndef UX_HOST_SIDE_ONLY |
140 |
|
|
|
141 |
|
|
/* Set the _ux_system_slave structure. */ |
142 |
|
406 |
_ux_system_slave = (UX_SYSTEM_SLAVE *) (((UCHAR *) regular_memory_pool_start) + memory_pool_offset); |
143 |
|
|
|
144 |
|
|
/* Add to the memory offset the size of the allocated block. */ |
145 |
|
406 |
memory_pool_offset += (ULONG)sizeof(UX_SYSTEM_SLAVE); |
146 |
|
|
|
147 |
|
|
#endif |
148 |
|
|
|
149 |
|
|
|
150 |
|
|
#ifdef UX_OTG_SUPPORT |
151 |
|
|
|
152 |
|
|
/* Set the _ux_system_otg structure. */ |
153 |
|
|
_ux_system_otg = (UX_SYSTEM_OTG *) (((UCHAR *) regular_memory_pool_start) + memory_pool_offset); |
154 |
|
|
|
155 |
|
|
/* Add to the memory offset the size of the allocated block. */ |
156 |
|
|
memory_pool_offset += (ULONG)sizeof(UX_SYSTEM_OTG); |
157 |
|
|
#endif |
158 |
|
|
|
159 |
|
|
/* Set the regular memory pool structure. */ |
160 |
|
406 |
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] = (UX_MEMORY_BYTE_POOL *) (((UCHAR *) regular_memory_pool_start) + memory_pool_offset); |
161 |
|
|
|
162 |
|
|
/* Add to the memory offset the size of the allocated block. */ |
163 |
|
406 |
memory_pool_offset += (ULONG)sizeof(UX_MEMORY_BYTE_POOL); |
164 |
|
|
|
165 |
|
|
/* Check if the cache save memory pool is valid. */ |
166 |
✓✓✓✗
|
406 |
if ((cache_safe_memory_pool_start != UX_NULL) && (cache_safe_memory_size != 0)) |
167 |
|
|
{ |
168 |
|
|
|
169 |
|
|
/* Set the cache safe memory pool structure. */ |
170 |
|
11 |
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] = (UX_MEMORY_BYTE_POOL *) (((UCHAR *) regular_memory_pool_start) + memory_pool_offset); |
171 |
|
|
|
172 |
|
|
/* Add to the memory offset the size of the allocated block. */ |
173 |
|
11 |
memory_pool_offset += (ULONG)sizeof(UX_MEMORY_BYTE_POOL); |
174 |
|
|
} |
175 |
|
|
else |
176 |
|
|
{ |
177 |
|
|
|
178 |
|
|
/* Set the cache safe memory pool structure to regular pool. */ |
179 |
|
395 |
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR]; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
/* Make sure the regular memory pool is aligned properly */ |
183 |
|
406 |
int_memory_pool_start = (ALIGN_TYPE) (((UCHAR *) regular_memory_pool_start) + memory_pool_offset); |
184 |
|
406 |
int_memory_pool_start += UX_ALIGN_MIN; |
185 |
|
406 |
int_memory_pool_start &= ~((ALIGN_TYPE)UX_ALIGN_MIN); |
186 |
|
|
|
187 |
|
|
/* Set the end of the regular memory pool. */ |
188 |
|
406 |
regular_memory_pool_end = (void *) (((UCHAR *) regular_memory_pool_start) + regular_memory_size); |
189 |
|
|
|
190 |
|
|
/* Check if we have memory available. */ |
191 |
✓✓ |
406 |
if (int_memory_pool_start >= (ALIGN_TYPE)regular_memory_pool_end) |
192 |
|
|
{ |
193 |
|
|
|
194 |
|
|
/* No memory available. */ |
195 |
|
1 |
return(UX_MEMORY_INSUFFICIENT); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
/* get the regular memory pool size. */ |
199 |
|
405 |
pool_size = (ULONG) (((ALIGN_TYPE) regular_memory_pool_end) - int_memory_pool_start); |
200 |
|
|
|
201 |
|
|
/* Create the regular memory pool. */ |
202 |
|
405 |
_ux_utility_memory_byte_pool_create(_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR], |
203 |
|
|
(UX_MEMORY_BYTE_POOL *)int_memory_pool_start, |
204 |
|
|
pool_size); |
205 |
|
|
|
206 |
|
|
/* Check the definition of the cache safe pool. If the application or controller do not require any cache safe memory, |
207 |
|
|
define the cached safe memory region as the regular memory region. */ |
208 |
✓✓✓✗
|
405 |
if ((cache_safe_memory_pool_start != UX_NULL) && (cache_safe_memory_size != 0)) |
209 |
|
|
{ |
210 |
|
|
|
211 |
|
|
/* Reset this memory block */ |
212 |
|
11 |
_ux_utility_memory_set(cache_safe_memory_pool_start, 0, cache_safe_memory_size); /* Use case of memset is verified. */ |
213 |
|
|
|
214 |
|
|
/* Make sure the cache safe memory pool is aligned properly */ |
215 |
|
11 |
int_memory_pool_start = (ALIGN_TYPE) cache_safe_memory_pool_start; |
216 |
|
11 |
int_memory_pool_start += UX_ALIGN_MIN; |
217 |
|
11 |
int_memory_pool_start &= ~((ALIGN_TYPE)UX_ALIGN_MIN); |
218 |
|
|
|
219 |
|
11 |
cache_safe_memory_pool_end = (void *) (((UCHAR *) cache_safe_memory_pool_start) + cache_safe_memory_size); |
220 |
|
|
|
221 |
|
|
/* Check if we have memory available. */ |
222 |
✗✓ |
11 |
if (int_memory_pool_start >= (ALIGN_TYPE) cache_safe_memory_pool_end) |
223 |
|
|
{ |
224 |
|
|
|
225 |
|
|
/* No memory available. */ |
226 |
|
|
return(UX_MEMORY_INSUFFICIENT); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
11 |
pool_size = (ULONG) (((ALIGN_TYPE) cache_safe_memory_pool_end) - int_memory_pool_start); |
230 |
|
|
|
231 |
|
11 |
_ux_utility_memory_byte_pool_create(_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE], |
232 |
|
|
(UX_MEMORY_BYTE_POOL *)int_memory_pool_start, pool_size); |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
#ifdef UX_ENABLE_MEMORY_STATISTICS |
236 |
|
|
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_min_free = |
237 |
|
|
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available; |
238 |
|
|
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] -> ux_byte_pool_min_free = |
239 |
|
|
_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] -> ux_byte_pool_available; |
240 |
|
|
|
241 |
|
|
/* Other fields are kept zero. */ |
242 |
|
|
#endif |
243 |
|
|
|
244 |
|
|
#if !defined(UX_STANDALONE) |
245 |
|
|
|
246 |
|
|
/* Create the Mutex object used by USBX to control critical sections. */ |
247 |
|
405 |
status = _ux_system_mutex_create(&_ux_system -> ux_system_mutex, "ux_system_mutex"); |
248 |
✓✓ |
405 |
if(status != UX_SUCCESS) |
249 |
|
1 |
return(UX_MUTEX_ERROR); |
250 |
|
|
#endif |
251 |
|
|
|
252 |
|
|
#ifdef UX_ENABLE_DEBUG_LOG |
253 |
|
|
|
254 |
|
|
/* Obtain memory for storing the debug log. */ |
255 |
|
|
_ux_system -> ux_system_debug_log_buffer = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, UX_DEBUG_LOG_SIZE); |
256 |
|
|
if (_ux_system -> ux_system_debug_log_buffer == UX_NULL) |
257 |
|
|
return(UX_MEMORY_INSUFFICIENT); |
258 |
|
|
|
259 |
|
|
/* Setup the head and tail pointers. */ |
260 |
|
|
_ux_system -> ux_system_debug_log_head = _ux_system -> ux_system_debug_log_buffer; |
261 |
|
|
_ux_system -> ux_system_debug_log_tail = _ux_system -> ux_system_debug_log_buffer; |
262 |
|
|
|
263 |
|
|
/* Keep the size in system structure variable. */ |
264 |
|
|
_ux_system -> ux_system_debug_log_size = UX_DEBUG_LOG_SIZE; |
265 |
|
|
|
266 |
|
|
#endif |
267 |
|
|
|
268 |
|
404 |
return(UX_SUCCESS); |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
|
272 |
|
|
/**************************************************************************/ |
273 |
|
|
/* */ |
274 |
|
|
/* FUNCTION RELEASE */ |
275 |
|
|
/* */ |
276 |
|
|
/* _uxe_system_initialize PORTABLE C */ |
277 |
|
|
/* 6.3.0 */ |
278 |
|
|
/* AUTHOR */ |
279 |
|
|
/* */ |
280 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
281 |
|
|
/* */ |
282 |
|
|
/* DESCRIPTION */ |
283 |
|
|
/* */ |
284 |
|
|
/* This function checks errors in system initialization function call. */ |
285 |
|
|
/* */ |
286 |
|
|
/* INPUT */ |
287 |
|
|
/* */ |
288 |
|
|
/* regular_memory_pool_start Start of non cached memory pool */ |
289 |
|
|
/* regular_memory_size Size of non cached memory pool */ |
290 |
|
|
/* cache_safe_memory_pool_start Start of cached memory pool */ |
291 |
|
|
/* cache_safe_memory_size Size of cached memory pool */ |
292 |
|
|
/* */ |
293 |
|
|
/* OUTPUT */ |
294 |
|
|
/* */ |
295 |
|
|
/* None */ |
296 |
|
|
/* */ |
297 |
|
|
/* CALLS */ |
298 |
|
|
/* */ |
299 |
|
|
/* _ux_system_initialize Get encoded feedback */ |
300 |
|
|
/* */ |
301 |
|
|
/* CALLED BY */ |
302 |
|
|
/* */ |
303 |
|
|
/* Application */ |
304 |
|
|
/* */ |
305 |
|
|
/**************************************************************************/ |
306 |
|
|
UINT _uxe_system_initialize(VOID *regular_memory_pool_start, ULONG regular_memory_size, |
307 |
|
|
VOID *cache_safe_memory_pool_start, ULONG cache_safe_memory_size) |
308 |
|
|
{ |
309 |
|
|
|
310 |
|
|
/* Compiling option check of descriptors structs. */ |
311 |
|
|
UX_ASSERT((_ux_utility_descriptor_parse_size(_ux_system_endpoint_descriptor_structure, UX_ENDPOINT_DESCRIPTOR_ENTRIES, 0x3u)) == sizeof(UX_ENDPOINT_DESCRIPTOR)); |
312 |
|
|
UX_ASSERT((_ux_utility_descriptor_parse_size(_ux_system_device_descriptor_structure, UX_DEVICE_DESCRIPTOR_ENTRIES, 0x3u)) == sizeof(UX_DEVICE_DESCRIPTOR)); |
313 |
|
|
UX_ASSERT((_ux_utility_descriptor_parse_size(_ux_system_configuration_descriptor_structure, UX_CONFIGURATION_DESCRIPTOR_ENTRIES, 0x3u)) == sizeof(UX_CONFIGURATION_DESCRIPTOR)); |
314 |
|
|
UX_ASSERT((_ux_utility_descriptor_parse_size(_ux_system_interface_descriptor_structure, UX_INTERFACE_DESCRIPTOR_ENTRIES, 0x3u)) == sizeof(UX_INTERFACE_DESCRIPTOR)); |
315 |
|
|
UX_ASSERT((_ux_utility_descriptor_parse_size(_ux_system_interface_association_descriptor_structure, UX_INTERFACE_ASSOCIATION_DESCRIPTOR_ENTRIES, 0x3u)) == sizeof(UX_INTERFACE_ASSOCIATION_DESCRIPTOR)); |
316 |
|
|
UX_ASSERT((_ux_utility_descriptor_parse_size(_ux_system_string_descriptor_structure, UX_STRING_DESCRIPTOR_ENTRIES, 0x3u)) == sizeof(UX_STRING_DESCRIPTOR)); |
317 |
|
|
UX_ASSERT((_ux_utility_descriptor_parse_size(_ux_system_dfu_functional_descriptor_structure, UX_DFU_FUNCTIONAL_DESCRIPTOR_ENTRIES, 0x3u)) == sizeof(UX_DFU_FUNCTIONAL_DESCRIPTOR)); |
318 |
|
|
UX_ASSERT((_ux_utility_descriptor_parse_size(_ux_system_bos_descriptor_structure, UX_BOS_DESCRIPTOR_ENTRIES, 0x3u)) == sizeof(UX_BOS_DESCRIPTOR)); |
319 |
|
|
UX_ASSERT((_ux_utility_descriptor_parse_size(_ux_system_usb_2_0_extension_descriptor_structure, UX_USB_2_0_EXTENSION_DESCRIPTOR_ENTRIES, 0x3u)) == sizeof(UX_USB_2_0_EXTENSION_DESCRIPTOR)); |
320 |
|
|
UX_ASSERT((_ux_utility_descriptor_parse_size(_ux_system_container_id_descriptor_structure, UX_CONTAINER_ID_DESCRIPTOR_ENTRIES, 0x3u)) == sizeof(UX_CONTAINER_ID_DESCRIPTOR)); |
321 |
|
|
|
322 |
|
|
|
323 |
|
|
/* Sanity check. */ |
324 |
|
|
if ((regular_memory_pool_start == UX_NULL) || (regular_memory_size == 0)) |
325 |
|
|
return(UX_INVALID_PARAMETER); |
326 |
|
|
|
327 |
|
|
/* Invoke system initialization function. */ |
328 |
|
|
return(_ux_system_initialize(regular_memory_pool_start, regular_memory_size, |
329 |
|
|
cache_safe_memory_pool_start, cache_safe_memory_size)); |
330 |
|
|
} |