1 /**************************************************************************/
2 /* */
3 /* Copyright (c) Microsoft Corporation. All rights reserved. */
4 /* */
5 /* This software is licensed under the Microsoft Software License */
6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */
7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
8 /* and in the root directory of this software. */
9 /* */
10 /**************************************************************************/
11
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /** */
16 /** USBX Component */
17 /** */
18 /** Storage Class */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23
24 /* Include necessary system files. */
25
26 #define UX_SOURCE_CODE
27
28 #include "ux_api.h"
29 #include "ux_host_class_storage.h"
30 #include "ux_host_stack.h"
31
32
33 #if defined(UX_HOST_STANDALONE)
34 /**************************************************************************/
35 /* */
36 /* FUNCTION RELEASE */
37 /* */
38 /* _ux_host_class_storage_lock PORTABLE C */
39 /* 6.2.1 */
40 /* AUTHOR */
41 /* */
42 /* Chaoqiong Xiao, Microsoft Corporation */
43 /* */
44 /* DESCRIPTION */
45 /* */
46 /* This function locks storage for further operations. */
47 /* */
48 /* It's valid only in standalone mode. */
49 /* It's non-blocking if wait is zero, otherwise it blocks. */
50 /* */
51 /* INPUT */
52 /* */
53 /* storage_media Pointer to storage to operate */
54 /* wait Wait option */
55 /* */
56 /* OUTPUT */
57 /* */
58 /* Completion Status */
59 /* UX_TIMEOUT Time out */
60 /* UX_REENTRY Reentry in storage task */
61 /* */
62 /* CALLS */
63 /* */
64 /* */
65 /* CALLED BY */
66 /* */
67 /* Storage Class */
68 /* */
69 /* RELEASE HISTORY */
70 /* */
71 /* DATE NAME DESCRIPTION */
72 /* */
73 /* 01-31-2022 Chaoqiong Xiao Initial Version 6.1.10 */
74 /* 03-08-2023 Chaoqiong Xiao Modified comment(s), */
75 /* checked device state, */
76 /* resulting in version 6.2.1 */
77 /* */
78 /**************************************************************************/
_ux_host_class_storage_lock(UX_HOST_CLASS_STORAGE * storage,ULONG wait)79 UINT _ux_host_class_storage_lock(UX_HOST_CLASS_STORAGE *storage, ULONG wait)
80 {
81 UX_INTERRUPT_SAVE_AREA
82 ULONG t0, t1;
83 UX_DEVICE *device;
84
85 /* Get device. */
86 device = storage -> ux_host_class_storage_device;
87
88 t0 = _ux_utility_time_get();
89 while(1)
90 {
91 UX_DISABLE
92
93 /* Check instance lock. */
94 if (storage -> ux_host_class_storage_flags & UX_HOST_CLASS_STORAGE_FLAG_LOCK)
95 {
96
97 /* Check once, just return busy. */
98 if (wait == 0)
99 {
100 UX_RESTORE
101 return(UX_TIMEOUT);
102 }
103
104 /* Check timeout. */
105 if (wait != UX_WAIT_FOREVER)
106 {
107 t1 = _ux_utility_time_get();
108 t1 = _ux_utility_time_elapsed(t0, t1);
109 if (t1 >= wait)
110 {
111 UX_RESTORE
112 return(UX_TIMEOUT);
113 }
114 }
115 }
116 else
117 {
118 /* It's free, time to lock it. */
119 break;
120 }
121
122 /* Check task reentry. */
123 if (storage -> ux_host_class_storage_flags & UX_HOST_CLASS_STORAGE_FLAG_PROTECT)
124 {
125 UX_RESTORE
126
127 /* Error trap. */
128 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_REENTRY);
129
130 /* If trace is enabled, insert this event into the trace buffer. */
131 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_REENTRY, storage, 0, 0, UX_TRACE_ERRORS, 0, 0)
132
133 return(UX_REENTRY);
134 }
135 UX_RESTORE
136
137 /* Run stack tasks. */
138 _ux_system_host_tasks_run();
139
140 /* Check if device is still available. */
141 if (device -> ux_device_state != UX_DEVICE_CONFIGURED)
142 {
143
144 /* Instance should have been destroyed, just return. */
145 return(UX_STATE_EXIT);
146 }
147 }
148
149 /* Lock storage. */
150 storage -> ux_host_class_storage_flags |= UX_HOST_CLASS_STORAGE_FLAG_LOCK;
151
152 /* Stop main state machine. */
153 storage -> ux_host_class_storage_state_state = UX_STATE_IDLE;
154
155 /* Reset operation state machine. */
156 storage -> ux_host_class_storage_op_state = UX_STATE_RESET;
157
158 UX_RESTORE
159 return(UX_SUCCESS);
160 }
161
162
163 /**************************************************************************/
164 /* */
165 /* FUNCTION RELEASE */
166 /* */
167 /* _uxe_host_class_storage_lock PORTABLE C */
168 /* 6.3.0 */
169 /* AUTHOR */
170 /* */
171 /* Chaoqiong Xiao, Microsoft Corporation */
172 /* */
173 /* DESCRIPTION */
174 /* */
175 /* This function checks errors in storage lock function call. */
176 /* */
177 /* INPUT */
178 /* */
179 /* storage Pointer to storage to operate */
180 /* wait Wait option */
181 /* */
182 /* OUTPUT */
183 /* */
184 /* Status */
185 /* */
186 /* CALLS */
187 /* */
188 /* _ux_host_class_storage_lock Lock storage */
189 /* */
190 /* CALLED BY */
191 /* */
192 /* Application */
193 /* */
194 /* RELEASE HISTORY */
195 /* */
196 /* DATE NAME DESCRIPTION */
197 /* */
198 /* 10-31-2023 Chaoqiong Xiao Initial Version 6.3.0 */
199 /* */
200 /**************************************************************************/
_uxe_host_class_storage_lock(UX_HOST_CLASS_STORAGE * storage,ULONG wait)201 UINT _uxe_host_class_storage_lock(UX_HOST_CLASS_STORAGE *storage, ULONG wait)
202 {
203
204 /* Sanity check. */
205 if (storage == UX_NULL)
206 return(UX_INVALID_PARAMETER);
207
208 /* Invoke storage lock function. */
209 return(_ux_host_class_storage_lock(storage, wait));
210 }
211 #endif
212