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 /**   Device Stack                                                        */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define UX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "ux_api.h"
29 #include "ux_device_stack.h"
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_stack_uninitialize                       PORTABLE C      */
36 /*                                                           6.1.10       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function uninitializes the generic portion of the device side  */
44 /*    of USBX.                                                            */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    Completion Status                                                   */
51 /*                                                                        */
52 /*  CALLS                                                                 */
53 /*                                                                        */
54 /*    _ux_utility_memory_free               Free                          */
55 /*    _ux_utility_semaphore_delete          Delete semaphore              */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    Application                                                         */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
66 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
67 /*                                            resulting in version 6.1    */
68 /*  04-02-2021     Chaoqiong Xiao           Modified comment(s),          */
69 /*                                            fixed uninitialize in case  */
70 /*                                            there is no EP except EP0,  */
71 /*                                            resulting in version 6.1.6  */
72 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            added standalone support,   */
74 /*                                            resulting in version 6.1.10 */
75 /*                                                                        */
76 /**************************************************************************/
_ux_device_stack_uninitialize(VOID)77 UINT  _ux_device_stack_uninitialize(VOID)
78 {
79 UX_SLAVE_DEVICE                 *device;
80 UX_SLAVE_ENDPOINT               *endpoints_pool;
81 UX_SLAVE_TRANSFER               *transfer_request;
82 ULONG                           endpoints_found;
83 
84     /* If trace is enabled, insert this event into the trace buffer.  */
85     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_INITIALIZE, 0, 0, 0, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0)
86 
87     /* Get the pointer to the device. */
88     device =  &_ux_system_slave -> ux_system_slave_device;
89 
90     /* Free class memory. */
91     _ux_utility_memory_free(_ux_system_slave -> ux_system_slave_class_array);
92 
93     /* Allocate some memory for the Control Endpoint.  First get the address of the transfer request for the
94        control endpoint. */
95     transfer_request =  &device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;
96 
97     /* Free memory for the control endpoint buffer.  */
98     _ux_utility_memory_free(transfer_request -> ux_slave_transfer_request_data_pointer);
99 
100     /* Get the number of endpoints found in the device framework.  */
101     endpoints_found = device -> ux_slave_device_endpoints_pool_number;
102 
103     /* Get the endpoint pool address in the device container.  */
104     endpoints_pool =  device -> ux_slave_device_endpoints_pool;
105 
106     /* Parse all endpoints and fee memory and semaphore. */
107     while (endpoints_found-- != 0)
108     {
109         /* Free the memory for endpoint data pointer.  */
110         _ux_utility_memory_free(endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer);
111 
112         /* Remove the TX semaphore for the endpoint.  */
113         _ux_device_semaphore_delete(&endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_semaphore);
114 
115         /* Next endpoint.  */
116         endpoints_pool++;
117     }
118 
119     /* Free the endpoint pool address in the device container.  */
120     if (device -> ux_slave_device_endpoints_pool)
121         _ux_utility_memory_free(device -> ux_slave_device_endpoints_pool);
122 
123     /* Free memory for interface pool.  */
124     _ux_utility_memory_free(device -> ux_slave_device_interfaces_pool);
125 
126     /* Return successful completion.  */
127     return(UX_SUCCESS);
128 }
129 
130 
131 
132 
133