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 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_hcd_sim_host.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_hcd_sim_host_transfer_abort                     PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*     This function will abort transactions attached to a transfer       */
45 /*     request.                                                           */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    hcd_sim_host                         Pointer to host controller     */
50 /*    transfer_request                     Pointer to transfer request    */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_utility_delay_ms                  Delay                         */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Host Simulator Controller Driver                                    */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            added standalone support,   */
73 /*                                            resulting in version 6.1.10 */
74 /*                                                                        */
75 /**************************************************************************/
_ux_hcd_sim_host_transfer_abort(UX_HCD_SIM_HOST * hcd_sim_host,UX_TRANSFER * transfer_request)76 UINT  _ux_hcd_sim_host_transfer_abort(UX_HCD_SIM_HOST *hcd_sim_host, UX_TRANSFER *transfer_request)
77 {
78 
79 UX_ENDPOINT             *endpoint;
80 UX_HCD_SIM_HOST_ED      *ed;
81 UX_HCD_SIM_HOST_TD      *head_td;
82 UX_HCD_SIM_HOST_TD      *tail_td;
83 
84     UX_PARAMETER_NOT_USED(hcd_sim_host);
85 
86     /* Get the pointer to the endpoint associated with the transfer request.  */
87     endpoint =  (UX_ENDPOINT *) transfer_request -> ux_transfer_request_endpoint;
88 
89     /* From the endpoint container, get the address of the physical endpoint.  */
90     ed =  (UX_HCD_SIM_HOST_ED *) endpoint -> ux_endpoint_ed;
91 
92     /* Check if this physical endpoint has been initialized properly!  */
93     if (ed == UX_NULL)
94     {
95 
96         /* Error trap. */
97         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_HCD, UX_ENDPOINT_HANDLE_UNKNOWN);
98 
99         /* If trace is enabled, insert this event into the trace buffer.  */
100         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0)
101 
102         return(UX_ENDPOINT_HANDLE_UNKNOWN);
103     }
104 
105 #if defined(UX_HOST_STANDALONE)
106     ed -> ux_sim_host_ed_status |= UX_HCD_SIM_HOST_ED_SKIP;
107 #else
108     /* The endpoint may be active. If so, set the skip bit.  */
109     ed -> ux_sim_host_ed_status |=  UX_HCD_SIM_HOST_ED_SKIP;
110 
111     /* Wait for the controller to finish the current frame processing.  */
112     _ux_utility_delay_ms(1);
113 #endif
114 
115     /* Remove all the TDs from this ED and leave the head and tail pointing to the dummy TD.  */
116     head_td =  ed -> ux_sim_host_ed_head_td;
117     tail_td =  ed -> ux_sim_host_ed_tail_td;
118 
119     /* Free all TDs attached to the ED.  */
120     while (head_td != tail_td)
121     {
122 
123         /* Mark the current head TD as free. */
124         head_td -> ux_sim_host_td_status =  UX_UNUSED;
125 
126         /* Update the head TD with the next TD.  */
127         ed -> ux_sim_host_ed_head_td =  head_td -> ux_sim_host_td_next_td;
128 
129         /* Now the new head TD is the next TD in the chain.  */
130         head_td =  ed -> ux_sim_host_ed_head_td;
131     }
132 
133 #if defined(UX_HOST_STANDALONE)
134     /* Remove the skip and transfer bits in the ED.  */
135     ed -> ux_sim_host_ed_status &= ~(UX_HCD_SIM_HOST_ED_SKIP|UX_HCD_SIM_HOST_ED_TRANSFER);
136 #else
137     /* Remove the reset bit in the ED.  */
138     ed -> ux_sim_host_ed_status &=  (ULONG)~UX_HCD_SIM_HOST_ED_SKIP;
139 #endif
140 
141     /* Return successful completion.  */
142     return(UX_SUCCESS);
143 }
144 
145