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 /** Host Stack */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23
24 /* Include necessary system files. */
25
26 #define UX_SOURCE_CODE
27
28 #include "ux_api.h"
29 #include "ux_host_stack.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _ux_host_stack_transfer_request PORTABLE C */
37 /* 6.1.10 */
38 /* AUTHOR */
39 /* */
40 /* Chaoqiong Xiao, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function performs a USB transaction. On entry the transfer */
45 /* request gives the endpoint pipe selected for this transaction and */
46 /* the parameters associated with the transfer (data payload, length */
47 /* of transaction) */
48 /* */
49 /* For Control pipe, the transaction is blocking and will only return */
50 /* when the 3 phases of the control transfer have been completed or if */
51 /* there is a previous error. For other pipes, the USB stack will */
52 /* schedule the transaction on the USB but will not wait for its */
53 /* completion. Each request for non blocking pipes has to specify a */
54 /* completion routine. */
55 /* */
56 /* INPUT */
57 /* */
58 /* transfer_request Transfer request structure */
59 /* */
60 /* OUTPUT */
61 /* */
62 /* Completion Status If UX_SUCCESS, transfer was */
63 /* successfully started */
64 /* */
65 /* CALLS */
66 /* */
67 /* HCD Entry Function */
68 /* _ux_utility_semaphore_put Put semaphore */
69 /* _ux_utility_semaphore_get Get semaphore */
70 /* */
71 /* CALLED BY */
72 /* */
73 /* Application */
74 /* USBX Components */
75 /* */
76 /* RELEASE HISTORY */
77 /* */
78 /* DATE NAME DESCRIPTION */
79 /* */
80 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
81 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
82 /* optimized based on compile */
83 /* definitions, used UX prefix */
84 /* to refer to TX symbols */
85 /* instead of using them */
86 /* directly, */
87 /* resulting in version 6.1 */
88 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
89 /* added standalone support, */
90 /* resulting in version 6.1.10 */
91 /* */
92 /**************************************************************************/
_ux_host_stack_transfer_request(UX_TRANSFER * transfer_request)93 UINT _ux_host_stack_transfer_request(UX_TRANSFER *transfer_request)
94 {
95 #if defined(UX_HOST_STANDALONE)
96 UINT status;
97
98 UX_TRANSFER_STATE_RESET(transfer_request);
99 _ux_host_stack_transfer_run(transfer_request);
100 if ((transfer_request -> ux_transfer_request_flags & UX_TRANSFER_FLAG_AUTO_WAIT))
101 {
102 while(1)
103 {
104
105 /* Allow tasks running during waiting. */
106 _ux_system_host_tasks_run();
107
108 if (transfer_request -> ux_transfer_request_state <= UX_STATE_NEXT)
109 break;
110 }
111 status = transfer_request -> ux_transfer_request_completion_code;
112 }
113 else
114 {
115
116 /* In this mode, transfer pending is a success started case. */
117 if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
118 status = UX_SUCCESS;
119 else
120 status = transfer_request -> ux_transfer_request_completion_code;
121 }
122
123 /* Return transfer completion status. */
124 return(status);
125 #else
126 UX_INTERRUPT_SAVE_AREA
127
128 UX_ENDPOINT *endpoint;
129 UX_DEVICE *device;
130 UX_HCD *hcd;
131 UINT status;
132
133
134 /* Get the endpoint container from the transfer_request */
135 endpoint = transfer_request -> ux_transfer_request_endpoint;
136
137 /* Get the device container from the endpoint. */
138 device = endpoint -> ux_endpoint_device;
139
140 /* Ensure we are not preempted by the enum thread while we check the device
141 state and set the transfer status. */
142 UX_DISABLE
143
144 /* We can only transfer when the device is ATTACHED, ADDRESSED OR CONFIGURED. */
145 if ((device -> ux_device_state == UX_DEVICE_ATTACHED) || (device -> ux_device_state == UX_DEVICE_ADDRESSED)
146 || (device -> ux_device_state == UX_DEVICE_CONFIGURED))
147 {
148
149 /* Set the transfer to pending. */
150 transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_STATUS_PENDING;
151 #if !defined(UX_HOST_STANDALONE)
152 /* Save the thread making this transfer. If we're under interrupt, this
153 will be null. */
154 transfer_request -> ux_transfer_request_thread_pending = _ux_utility_thread_identify();
155 #endif
156 }
157 else
158 {
159
160 /* The device is in an invalid state. Restore interrupts and return error. */
161 UX_RESTORE
162
163 /* Check if this is endpoint 0. */
164 if ((endpoint -> ux_endpoint_descriptor.bEndpointAddress & (UINT)~UX_ENDPOINT_DIRECTION) == 0)
165 {
166
167 /* Check if the class has already protected it. */
168 if (!_ux_host_semaphore_waiting(&device -> ux_device_protection_semaphore))
169 {
170
171 /* Class is using endpoint 0. Unprotect semaphore. */
172 _ux_host_semaphore_put(&device -> ux_device_protection_semaphore);
173 }
174 }
175
176 return(UX_TRANSFER_NOT_READY);
177 }
178
179 /* Restore interrupts. */
180 UX_RESTORE
181
182 /* If trace is enabled, insert this event into the trace buffer. */
183 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_TRANSFER_REQUEST, device, endpoint, transfer_request, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
184
185 /* With the device we have the pointer to the HCD. */
186 hcd = UX_DEVICE_HCD_GET(device);
187
188 /* If this is endpoint 0, we protect the endpoint from a possible re-entry. */
189 if ((endpoint -> ux_endpoint_descriptor.bEndpointAddress & (UINT)~UX_ENDPOINT_DIRECTION) == 0)
190 {
191
192 /* Check if the class has already protected it. */
193 if (_ux_host_semaphore_waiting(&device -> ux_device_protection_semaphore))
194 {
195
196 /* We are using endpoint 0. Protect with semaphore. */
197 status = _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
198
199 /* Check for status. */
200 if (status != UX_SUCCESS)
201
202 /* Something went wrong. */
203 return(status);
204 }
205 }
206
207 /* Send the command to the controller. */
208 status = hcd -> ux_hcd_entry_function(hcd, UX_HCD_TRANSFER_REQUEST, transfer_request);
209
210 /* If this is endpoint 0, we unprotect the endpoint. */
211 if ((endpoint -> ux_endpoint_descriptor.bEndpointAddress & (UINT)~UX_ENDPOINT_DIRECTION) == 0)
212
213 /* We are using endpoint 0. Unprotect with semaphore. */
214 _ux_host_semaphore_put(&device -> ux_device_protection_semaphore);
215
216 /* And return the status. */
217 return(status);
218 #endif
219 }
220