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 Stack                                                          */
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_host_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_host_stack_transfer_request_abort               PORTABLE C      */
36 /*                                                           6.2.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function aborts a pending transfer request that has been       */
44 /*    previously submitted. This function only cancels the specific       */
45 /*    transfer request.                                                   */
46 /*                                                                        */
47 /*    The call back to the function will have the                         */
48 /*    UX_TRANSFER_STATUS_ABORT status.                                    */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    transfer_request                      Transfer request structure    */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                     If UX_SUCCESS, transfer was   */
57 /*                                            successfully aborted        */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    HCD Entry Function                                                  */
62 /*    Transfer Completion Function                                        */
63 /*    _ux_utility_semaphore_put             Put semaphore                 */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Application                                                         */
68 /*    USBX Components                                                     */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
75 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            optimized based on compile  */
77 /*                                            definitions,                */
78 /*                                            resulting in version 6.1    */
79 /*  06-02-2021     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            fixed trace enabled error,  */
81 /*                                            resulting in version 6.1.7  */
82 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
83 /*                                            added standalone support,   */
84 /*                                            resulting in version 6.1.10 */
85 /*  10-31-2022     Chaoqiong Xiao           Modified comment(s),          */
86 /*                                            resulting in version 6.2.0  */
87 /*                                                                        */
88 /**************************************************************************/
_ux_host_stack_transfer_request_abort(UX_TRANSFER * transfer_request)89 UINT  _ux_host_stack_transfer_request_abort(UX_TRANSFER *transfer_request)
90 {
91 
92 UX_HCD          *hcd;
93 ULONG           completion_code;
94 
95 
96     /* If trace is enabled, insert this event into the trace buffer.  */
97     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_TRANSFER_REQUEST_ABORT,
98         transfer_request -> ux_transfer_request_endpoint -> ux_endpoint_device,
99         transfer_request -> ux_transfer_request_endpoint,
100         transfer_request, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
101 
102     /* With the device we have the pointer to the HCD.  */
103     hcd = UX_DEVICE_HCD_GET(transfer_request -> ux_transfer_request_endpoint -> ux_endpoint_device);
104 
105     /* Check pending transaction.  */
106     if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
107     {
108 
109         /* Send the abort command to the controller.  */
110         hcd -> ux_hcd_entry_function(hcd, UX_HCD_TRANSFER_ABORT, transfer_request);
111 
112         /* Save the completion code since we're about to set it to ABORT. The
113            reason we can't just assume its value is PENDING is that in between
114            the completion code check and this line, it's possible that the transfer
115            completed (by HCD function call below, or ISR), which would've
116            changed the completion code to SUCCESS and put the semaphore.
117            Even it's recommended to keep completion code untouched to let things
118            changed later here.
119            Such a case is valid, and we want to make sure we don't put() the
120            transfer request's semaphore again.  */
121         completion_code =  transfer_request -> ux_transfer_request_completion_code;
122 
123         /* Set the transfer_request status to abort.  */
124         transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_STATUS_ABORT;
125 
126         /* We need to inform the class that owns this transfer_request of the
127            abort if there is a call back mechanism.  */
128         if (transfer_request -> ux_transfer_request_completion_function != UX_NULL)
129             transfer_request -> ux_transfer_request_completion_function(transfer_request);
130 
131         /* Is a thread waiting on the semaphore?  */
132         if (/* Is the transfer pending?  */
133             completion_code == UX_TRANSFER_STATUS_PENDING &&
134 #if !defined(UX_HOST_STANDALONE)
135             /* Is the thread waiting not this one? (clearly we're not waiting!)  */
136             transfer_request -> ux_transfer_request_thread_pending != _ux_utility_thread_identify() &&
137 #endif
138             /* Does the transfer request not have a completion function?  */
139             transfer_request -> ux_transfer_request_completion_function == UX_NULL)
140 
141             /* Wake up the semaphore for this request.  */
142             _ux_host_semaphore_put(&transfer_request -> ux_transfer_request_semaphore);
143     }
144 
145     /* This function never fails!  */
146     return(UX_SUCCESS);
147 }
148 
149 
150 /**************************************************************************/
151 /*                                                                        */
152 /*  FUNCTION                                               RELEASE        */
153 /*                                                                        */
154 /*    _uxe_host_stack_transfer_request_abort              PORTABLE C      */
155 /*                                                           6.3.0        */
156 /*  AUTHOR                                                                */
157 /*                                                                        */
158 /*    Chaoqiong Xiao, Microsoft Corporation                               */
159 /*                                                                        */
160 /*  DESCRIPTION                                                           */
161 /*                                                                        */
162 /*    This function checks errors in host stack transfer abort function   */
163 /*    call.                                                               */
164 /*                                                                        */
165 /*  INPUT                                                                 */
166 /*                                                                        */
167 /*    transfer_request                      Pointer to transfer           */
168 /*                                                                        */
169 /*  OUTPUT                                                                */
170 /*                                                                        */
171 /*    None                                                                */
172 /*                                                                        */
173 /*  CALLS                                                                 */
174 /*                                                                        */
175 /*    _ux_host_stack_transfer_request_abort Transfer abort                */
176 /*                                                                        */
177 /*  CALLED BY                                                             */
178 /*                                                                        */
179 /*    Application                                                         */
180 /*                                                                        */
181 /*  RELEASE HISTORY                                                       */
182 /*                                                                        */
183 /*    DATE              NAME                      DESCRIPTION             */
184 /*                                                                        */
185 /*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
186 /*                                                                        */
187 /**************************************************************************/
_uxe_host_stack_transfer_request_abort(UX_TRANSFER * transfer_request)188 UINT  _uxe_host_stack_transfer_request_abort(UX_TRANSFER *transfer_request)
189 {
190 
191     /* Sanity checks.  */
192     if (transfer_request == UX_NULL)
193         return(UX_INVALID_PARAMETER);
194     if (transfer_request -> ux_transfer_request_endpoint == UX_NULL)
195         return(UX_ENDPOINT_HANDLE_UNKNOWN);
196     if (transfer_request -> ux_transfer_request_endpoint -> ux_endpoint_device == UX_NULL)
197         return(UX_DEVICE_HANDLE_UNKNOWN);
198     if (UX_DEVICE_HCD_GET(transfer_request -> ux_transfer_request_endpoint -> ux_endpoint_device) == UX_NULL)
199         return(UX_INVALID_PARAMETER);
200 
201     /* Invoke transfer abort function.  */
202     return(_ux_host_stack_transfer_request_abort(transfer_request));
203 }
204