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