1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2026-present Eclipse ThreadX contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** USBX Component */
/** */
/** Host Stack */
/** */
/**************************************************************************/
/**************************************************************************/
/* Include necessary system files. */
#define UX_SOURCE_CODE
#include "ux_api.h"
#include "ux_host_stack.h"
#if defined(UX_HOST_STANDALONE)
#define UX_HOST_STACK_TRANSFER_LIST_IS_NULL 0
#define UX_HOST_STACK_TRANSFER_NOT_IN_LIST 1
#define UX_HOST_STACK_TRANSFER_AT_LIST_HEAD 2
#define UX_HOST_STACK_TRANSFER_IN_LIST 3
static inline UINT _ux_host_stack_transfer_locate(UX_TRANSFER *transfer, UX_TRANSFER **previous);
static inline void _ux_host_stack_transfer_retire(UX_TRANSFER *transfer);
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_host_stack_transfer_run PORTABLE C */
/* 6.1.10 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function performs a USB transaction. On entry the transfer */
/* request gives the endpoint pipe selected for this transaction and */
/* the parameters associated with the transfer (data payload, length */
/* of transaction) */
/* */
/* Note after transfer is done, it's in error or idle state. To ensure */
/* transfer is ready for next round, use UX_TRANSFER_STATE_RESET(). */
/* */
/* It's for standalone mode. */
/* */
/* INPUT */
/* */
/* transfer_request Transfer request structure */
/* */
/* OUTPUT */
/* */
/* State machine Status to check */
/* UX_STATE_NEXT Transfer done, to next state */
/* UX_STATE_EXIT Abnormal, to reset state */
/* (others) Keep running, waiting */
/* */
/* CALLS */
/* */
/* HCD Entry Function */
/* */
/* CALLED BY */
/* */
/* Application */
/* USBX Components */
/* */
/**************************************************************************/
UINT _ux_host_stack_transfer_run(UX_TRANSFER *transfer_request)
{
UX_INTERRUPT_SAVE_AREA
UX_ENDPOINT *endpoint;
UX_DEVICE *device;
UX_HCD *hcd;
ULONG endpoint_type;
UINT status;
/* Get the endpoint container from the transfer_request */
endpoint = transfer_request -> ux_transfer_request_endpoint;
/* Sanity check. */
if (endpoint == UX_NULL)
return(UX_STATE_EXIT);
/* Get the endpoint type. */
endpoint_type = endpoint -> ux_endpoint_descriptor.bmAttributes;
endpoint_type &= UX_MASK_ENDPOINT_TYPE;
/* Get the device container from the endpoint. */
device = endpoint -> ux_endpoint_device;
/* Sanity check. */
if (device == UX_NULL)
return(UX_STATE_EXIT);
/* Ensure we are not preempted by the enum thread while we check the device
state and set the transfer status. */
UX_DISABLE
/* Check if it's default endpoint 0. */
if (endpoint -> ux_endpoint_descriptor.bEndpointAddress & ~UX_ENDPOINT_DIRECTION)
{
/* It's not endpoint 0, check device state. */
if (device -> ux_device_state != UX_DEVICE_ATTACHED &&
device -> ux_device_state != UX_DEVICE_ADDRESSED &&
device -> ux_device_state != UX_DEVICE_CONFIGURED)
{
transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_NOT_READY;
transfer_request -> ux_transfer_request_state = UX_STATE_EXIT;
_ux_host_stack_transfer_retire(transfer_request);
/* Restore interrupts. */
UX_RESTORE
return(UX_STATE_EXIT);
}
}
/* With the device we have the pointer to the HCD. */
hcd = UX_DEVICE_HCD_GET(device);
/* Process states. */
switch(transfer_request -> ux_transfer_request_state)
{
case UX_STATE_RESET:
/* Initialize request fields. */
transfer_request -> ux_transfer_request_actual_length = 0;
transfer_request -> ux_transfer_request_status = UX_TRANSFER_STATUS_NOT_PENDING;
transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_STATUS_PENDING;
transfer_request -> ux_transfer_request_state = UX_STATE_WAIT;
transfer_request -> ux_transfer_request_time_start = _ux_utility_time_get();
/* Add request to system pending request list. Note request may be kept
if transfer callback is used. */
if (_ux_host_stack_transfer_locate(transfer_request, UX_NULL) <
UX_HOST_STACK_TRANSFER_AT_LIST_HEAD)
{
transfer_request -> ux_transfer_request_next_pending =
_ux_system_host -> ux_system_host_pending_transfers;
_ux_system_host -> ux_system_host_pending_transfers = transfer_request;
}
/* Do immediate HCD call to start transfer in background. */
/* Fall through. */
case UX_STATE_WAIT:
UX_RESTORE
{
/* Send the command to the controller. */
status = hcd -> ux_hcd_entry_function(hcd, UX_HCD_TRANSFER_RUN, transfer_request);
}
/* Any error or end: simplify to idle. */
if (status < UX_STATE_WAIT)
{
UX_DISABLE
UX_TRANSFER_STATE_IDLE(transfer_request);
_ux_host_stack_transfer_retire(transfer_request);
UX_RESTORE
return(status);
}
/* Timeout check. */
if (transfer_request -> ux_transfer_request_timeout_value != UX_WAIT_FOREVER)
{
if (transfer_request -> ux_transfer_request_timeout_value <
_ux_utility_time_elapsed(transfer_request -> ux_transfer_request_time_start,
_ux_utility_time_get()))
{
/* All transfers pending need to abort. There may have been a partial transfer. */
_ux_host_stack_transfer_request_abort(transfer_request);
/* Set the completion code. */
transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_TIMEOUT;
/* There was an error: simplify to idle. */
UX_DISABLE
UX_TRANSFER_STATE_IDLE(transfer_request);
_ux_host_stack_transfer_retire(transfer_request);
UX_RESTORE
return(UX_STATE_ERROR);
}
}
/* And return the status. */
return(status);
case UX_STATE_EXIT:
UX_RESTORE
return(UX_STATE_EXIT);
case UX_STATE_IDLE:
UX_RESTORE
return(UX_STATE_IDLE);
default: /* Error case, return EXIT. */
break;
}
/* Error case, return EXIT. */
transfer_request -> ux_transfer_request_state = UX_STATE_RESET;
_ux_host_stack_transfer_retire(transfer_request);
UX_RESTORE
return(UX_STATE_EXIT);
}
static inline UINT _ux_host_stack_transfer_locate(UX_TRANSFER *transfer, UX_TRANSFER **previous)
{
UX_TRANSFER *prev;
/* Case 0: pending list is NULL. */
if (_ux_system_host -> ux_system_host_pending_transfers == UX_NULL)
return(UX_HOST_STACK_TRANSFER_LIST_IS_NULL);
/* Case 1: it's list head. */
if (_ux_system_host -> ux_system_host_pending_transfers == transfer)
return(UX_HOST_STACK_TRANSFER_AT_LIST_HEAD);
/* Case 2: scan list. */
prev = _ux_system_host -> ux_system_host_pending_transfers;
do
{
if (prev -> ux_transfer_request_next_pending == transfer)
{
if (previous)
*previous = prev;
return(UX_HOST_STACK_TRANSFER_IN_LIST);
}
prev = prev -> ux_transfer_request_next_pending;
} while (prev);
/* Case 3: not found. */
return(UX_HOST_STACK_TRANSFER_NOT_IN_LIST);
}
static inline void _ux_host_stack_transfer_retire(UX_TRANSFER *transfer)
{
ULONG flags;
UX_TRANSFER *previous;
/* Locate the request in pending list. */
switch(_ux_host_stack_transfer_locate(transfer, &previous))
{
case UX_HOST_STACK_TRANSFER_AT_LIST_HEAD:
/* Unlink from pending transfer list head. */
_ux_system_host -> ux_system_host_pending_transfers =
transfer -> ux_transfer_request_next_pending;
break;
case UX_HOST_STACK_TRANSFER_IN_LIST:
/* Unlink from pending transfer list. */
previous -> ux_transfer_request_next_pending =
transfer -> ux_transfer_request_next_pending;
break;
default:
break;
}
/* Process transfer flags. */
flags = transfer -> ux_transfer_request_flags;
transfer -> ux_transfer_request_flags &=
~(UX_TRANSFER_FLAG_LOCK | UX_TRANSFER_FLAG_AUTO_DEVICE_UNLOCK);
if (flags & UX_TRANSFER_FLAG_AUTO_DEVICE_UNLOCK)
{
transfer -> ux_transfer_request_endpoint ->
ux_endpoint_device -> ux_device_flags &= ~UX_DEVICE_FLAG_LOCK;
}
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _uxe_host_stack_transfer_run PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function checks errors in host stack transfer function call. */
/* */
/* INPUT */
/* */
/* transfer_request Pointer to transfer */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* _ux_host_stack_transfer_run Run a transfer request */
/* */
/* CALLED BY */
/* */
/* Application */
/* */
/**************************************************************************/
UINT _uxe_host_stack_transfer_run(UX_TRANSFER *transfer_request)
{
/* Sanity checks. */
if (transfer_request == UX_NULL)
return(UX_INVALID_PARAMETER);
if (transfer_request -> ux_transfer_request_endpoint == UX_NULL)
return(UX_ENDPOINT_HANDLE_UNKNOWN);
if (transfer_request -> ux_transfer_request_endpoint -> ux_endpoint_device == UX_NULL)
return(UX_DEVICE_HANDLE_UNKNOWN);
if (UX_DEVICE_HCD_GET(transfer_request -> ux_transfer_request_endpoint -> ux_endpoint_device) == UX_NULL)
return(UX_INVALID_PARAMETER);
/* Invoke transfer request function. */
return(_ux_host_stack_transfer_run(transfer_request));
}
#endif
|