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 /**   Host Simulator Controller Driver                                    */
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_hcd_sim_host.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_hcd_sim_host_uninitialize                       PORTABLE C      */
36 /*                                                           6.3.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function will uninitialize the simulated host controller.      */
44 /*    The controller will release all its resources (memory, IO ...).     */
45 /*    After this, the controller will not send SOF any longer.            */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    hcd_sim_host                          Pointer to host controller    */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_utility_memory_free               Free memory block             */
58 /*    _ux_utility_timer_delete              Delete timer                  */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Host Simulator Controller Driver                                    */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  11-09-2020     Chaoqiong Xiao           Initial Version 6.1.2         */
69 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            added standalone support,   */
71 /*                                            resulting in version 6.1.10 */
72 /*  10-31-2023     Yajun Xia                Modified comment(s),          */
73 /*                                            refined memory management,  */
74 /*                                            resulting in version 6.3.0  */
75 /*                                                                        */
76 /**************************************************************************/
_ux_hcd_sim_host_uninitialize(UX_HCD_SIM_HOST * hcd_sim_host)77 UINT  _ux_hcd_sim_host_uninitialize(UX_HCD_SIM_HOST *hcd_sim_host)
78 {
79 
80 UX_HCD                  *hcd = hcd_sim_host -> ux_hcd_sim_host_hcd_owner;
81 #if defined(UX_HOST_STANDALONE)
82 UX_HCD_SIM_HOST_TD      *td;
83 UINT                    td_index;
84 #endif
85 
86     /* Set the state of the controller to HALTED first.  */
87     hcd -> ux_hcd_status =  UX_HCD_STATUS_HALTED;
88 
89     /* Get simulated host controller.  */
90     hcd_sim_host = (UX_HCD_SIM_HOST *)hcd -> ux_hcd_controller_hardware;
91 
92     /* Delete timer.  */
93     _ux_host_timer_delete(&hcd_sim_host -> ux_hcd_sim_host_timer);
94 
95 #if defined(UX_HOST_STANDALONE)
96 
97     /* Check if there is pending SETUP TD, free buffers of them.  */
98     for (td_index = 0; td_index < _ux_system_host -> ux_system_host_max_td; td_index++)
99     {
100         td = &hcd_sim_host -> ux_hcd_sim_host_td_list[td_index];
101 
102         /* Skip free TDs.  */
103         if (td -> ux_sim_host_td_status == UX_UNUSED)
104             continue;
105 
106         /* Skip TDs not for setup.  */
107         if ((td -> ux_sim_host_td_status &  UX_HCD_SIM_HOST_TD_SETUP_PHASE) == 0)
108             continue;
109 
110         /* Skip TDs already freed.  */
111         if (td -> ux_sim_host_td_buffer == UX_NULL)
112             continue;
113 
114         /* Free the TD buffer.  */
115         _ux_utility_memory_free(td -> ux_sim_host_td_buffer);
116     }
117 #endif
118 
119     /* Free TD/ED memories.  */
120     if (hcd_sim_host -> ux_hcd_sim_host_iso_td_list)
121         _ux_utility_memory_free(hcd_sim_host -> ux_hcd_sim_host_iso_td_list);
122 
123     if (hcd_sim_host -> ux_hcd_sim_host_td_list)
124         _ux_utility_memory_free(hcd_sim_host -> ux_hcd_sim_host_td_list);
125 
126     if (hcd_sim_host -> ux_hcd_sim_host_ed_list)
127         _ux_utility_memory_free(hcd_sim_host -> ux_hcd_sim_host_ed_list);
128 
129     /* Free simulated host controller memory.  */
130     _ux_utility_memory_free(hcd_sim_host);
131     hcd -> ux_hcd_controller_hardware = UX_NULL;
132 
133     /* Set the state of the controller to UNUSED first.  */
134     hcd -> ux_hcd_status =  UX_UNUSED;
135 
136     /* Return successful completion.  */
137     return(UX_SUCCESS);
138 }
139