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 /** Host Stack */
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_stack.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _ux_host_stack_device_resources_free PORTABLE C */
37 /* 6.1.12 */
38 /* AUTHOR */
39 /* */
40 /* Chaoqiong Xiao, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function will free all the device resources allocated. */
45 /* */
46 /* INPUT */
47 /* */
48 /* device Device pointer */
49 /* */
50 /* OUTPUT */
51 /* */
52 /* Completion Status */
53 /* */
54 /* CALLS */
55 /* */
56 /* _ux_host_stack_endpoint_transfer_abort */
57 /* Abort transfer */
58 /* _ux_host_stack_endpoint_instance_delete */
59 /* Delete endpoint instance */
60 /* _ux_utility_memory_free Free memory block */
61 /* _ux_utility_memory_set Set memory with a value */
62 /* _ux_utility_semaphore_delete Semaphore delete */
63 /* _ux_utility_thread_schedule_other Sleep thread to let others */
64 /* run */
65 /* (ux_hcd_entry_function) HCD entry function */
66 /* */
67 /* CALLED BY */
68 /* */
69 /* USBX Components */
70 /* */
71 /* RELEASE HISTORY */
72 /* */
73 /* DATE NAME DESCRIPTION */
74 /* */
75 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
76 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
77 /* optimized based on compile */
78 /* definitions, verified */
79 /* memset and memcpy cases, */
80 /* resulting in version 6.1 */
81 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
82 /* added standalone support, */
83 /* resulting in version 6.1.10 */
84 /* 07-29-2022 Chaoqiong Xiao Modified comment(s), */
85 /* fixed parameter/variable */
86 /* names conflict C++ keyword, */
87 /* fixed standalone enum free, */
88 /* freed shared device config */
89 /* descriptor for enum scan, */
90 /* resulting in version 6.1.12 */
91 /* */
92 /**************************************************************************/
_ux_host_stack_device_resources_free(UX_DEVICE * device)93 UINT _ux_host_stack_device_resources_free(UX_DEVICE *device)
94 {
95
96 UX_CONFIGURATION *configuration;
97 UX_INTERFACE *interface_ptr;
98 UX_ENDPOINT *endpoint;
99 VOID *container;
100 ULONG current_alternate_setting;
101 UX_HCD *hcd;
102 #if UX_MAX_DEVICES > 1
103 UINT device_address_byte_index;
104 UINT device_address_bit_index;
105 UCHAR device_address_byte;
106 #endif
107 #if defined(UX_HOST_STANDALONE)
108 UX_DEVICE *enum_next;
109 #endif
110
111 /* If trace is enabled, insert this event into the trace buffer. */
112 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_DEVICE_RESOURCE_FREE, device, 0, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
113
114 #if defined(UX_HOST_STANDALONE)
115
116 /* Free possible allocated enumeration resources. */
117 if (device -> ux_device_flags & UX_DEVICE_FLAG_ENUM)
118 {
119
120 /* If transfer buffer is not freed, free it. */
121 if (device -> ux_device_enum_trans &&
122 device -> ux_device_enum_trans -> ux_transfer_request_data_pointer)
123 {
124 _ux_utility_memory_free(device -> ux_device_enum_trans ->
125 ux_transfer_request_data_pointer);
126 }
127
128 /* If configuration is not attached, free it. */
129 if ((device -> ux_device_enum_state == UX_HOST_STACK_ENUM_CONFIG_DESCR_PARSE) ||
130 ((device -> ux_device_enum_state == UX_HOST_STACK_ENUM_TRANS_WAIT) &&
131 (device -> ux_device_enum_next_state == UX_HOST_STACK_ENUM_CONFIG_DESCR_PARSE)))
132 {
133 if (device -> ux_device_enum_inst.configuration &&
134 device -> ux_device_enum_inst.configuration ->
135 ux_configuration_device == UX_NULL)
136 {
137 _ux_utility_memory_free(device -> ux_device_enum_inst.ptr);
138 }
139 }
140 }
141
142 /* Reset device flags. */
143 device -> ux_device_flags = 0;
144
145 #endif
146
147 /* Set the alternate setting to zero. */
148 current_alternate_setting = 0;
149
150 /* Get the first configuration registered to the device. */
151 configuration = device -> ux_device_first_configuration;
152
153 /* Parse all the configurations, remove all resources for the possible configuration. */
154 while (configuration != UX_NULL)
155 {
156
157 /* We have the correct configuration, search the interface(s). */
158 interface_ptr = configuration -> ux_configuration_first_interface;
159
160 /* Parse all the interfaces. */
161 while (interface_ptr != UX_NULL)
162 {
163
164 /* The alternate setting 0 has the selected alternate setting value. */
165 if (interface_ptr -> ux_interface_descriptor.bAlternateSetting == 0)
166 current_alternate_setting = interface_ptr -> ux_interface_current_alternate_setting;
167
168 /* If this is the selected interface, we need to free all the endpoints
169 attached to the alternate setting for this interface. */
170 endpoint = interface_ptr -> ux_interface_first_endpoint;
171
172 /* Parse all the endpoints. */
173 while (endpoint != UX_NULL)
174 {
175
176 /* Check if this is the selected interface. */
177 if (interface_ptr -> ux_interface_descriptor.bAlternateSetting == current_alternate_setting)
178 {
179
180 /* Delete the endpoint instance first. */
181 _ux_host_stack_endpoint_instance_delete(endpoint);
182 }
183
184 /* Memorize the endpoint container address. */
185 container = (VOID *) endpoint;
186
187 /* Get the next endpoint. */
188 endpoint = endpoint -> ux_endpoint_next_endpoint;
189
190 /* Delete the endpoint container. */
191 _ux_utility_memory_free(container);
192 }
193
194
195 /* Memorize the interface container address. */
196 container = (VOID *) interface_ptr;
197
198 /* Get the next interface. */
199 interface_ptr = interface_ptr -> ux_interface_next_interface;
200
201 /* Delete the interface container. */
202 _ux_utility_memory_free(container);
203 }
204
205 /* Memorize this configuration address before we free it. */
206 container = (VOID *) configuration;
207
208 /* Move to the next configuration in the list. */
209 configuration = configuration -> ux_configuration_next_configuration;
210
211 /* Free the configuration. */
212 _ux_utility_memory_free(container);
213 }
214
215 /* If there was a copy of packed descriptor, free it. */
216 if (device -> ux_device_packed_configuration)
217 {
218 _ux_utility_memory_free(device -> ux_device_packed_configuration);
219
220 /* Pointer and keep count is set NULL later while reseting instance memory. */
221 }
222
223 /* We need the HCD address for the control endpoint removal and to free
224 the device address. */
225 hcd = UX_DEVICE_HCD_GET(device);
226
227 /* Was the control endpoint already created ? */
228 if (device -> ux_device_control_endpoint.ux_endpoint_state != 0)
229 {
230
231 /* There may be pending transactions on the control endpoint. They need to be aborted. */
232 _ux_host_stack_endpoint_transfer_abort(&device -> ux_device_control_endpoint);
233
234 /* The enumeration thread needs to sleep a while to allow the application or the class that may be using
235 the control endpoint to exit properly. */
236 _ux_host_thread_schedule_other(UX_THREAD_PRIORITY_ENUM);
237
238 /* The control endpoint should be destroyed at the HCD level. */
239 hcd -> ux_hcd_entry_function(hcd, UX_HCD_DESTROY_ENDPOINT, (VOID *) &device -> ux_device_control_endpoint);
240 }
241
242 /* The semaphore attached to the control endpoint must be destroyed. */
243 _ux_host_semaphore_delete(&device -> ux_device_control_endpoint.ux_endpoint_transfer_request.ux_transfer_request_semaphore);
244
245 #if UX_MAX_DEVICES > 1
246 /* Check if the device had an assigned address. */
247 if (device -> ux_device_address != 0)
248 {
249
250 /* The USB address of this device can now be returned to the pool
251 We need the HCD pointer for this operation. */
252
253 /* Calculate in which byte index the device address belongs. */
254 device_address_byte_index = (UINT) (device -> ux_device_address-1)/8;
255
256 /* Now calculate the amount left in the byte index in bit. */
257 device_address_bit_index = (UINT) (device -> ux_device_address-1)%8;
258
259 /* Build the mask for the address. */
260 device_address_byte = (UCHAR)(1 << device_address_bit_index);
261
262 /* Free the address. */
263 hcd -> ux_hcd_address[device_address_byte_index] &= (UCHAR)~device_address_byte;
264 }
265 #endif
266
267 /* The semaphore for endpoint 0 protection must be destroyed. */
268 _ux_host_semaphore_delete(&device -> ux_device_protection_semaphore);
269
270 /* Now this device can be free and its container return to the pool. */
271 #if defined(UX_HOST_STANDALONE)
272 enum_next = device -> ux_device_enum_next;
273 _ux_utility_memory_set(device, 0, sizeof(UX_DEVICE)); /* Use case of memset is verified. */
274 device -> ux_device_enum_next = enum_next;
275 #else
276
277 _ux_utility_memory_set(device, 0, sizeof(UX_DEVICE)); /* Use case of memset is verified. */
278 #endif
279
280 /* Mark the device handle as unused. */
281 device -> ux_device_handle = UX_UNUSED;
282
283 /* Return successful completion. */
284 return(UX_SUCCESS);
285 }
286
287