Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define UX_SOURCE_CODE
#include "ux_api.h"
#include "ux_host_class_storage.h"
#include "ux_host_stack.h"
...
...
_ux_host_class_storage_transport_bo(UX_HOST_CLASS_STORAGE *, UCHAR *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesusbxcommon/usbx_host_classes/src/ux_host_class_storage_transport_bo.c
 
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** USBX Component */ /** */ /** Storage Class */ /** */... /**************************************************************************/ /**************************************************************************/ /* Include necessary system files. */ #define UX_SOURCE_CODE #include "ux_api.h" #include "ux_host_class_storage.h" #include "ux_host_stack.h" #if !defined(UX_HOST_STANDALONE)... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _ux_host_class_storage_transport_bo PORTABLE C */ /* 6.1.10 */ /* AUTHOR */ /* */ /* Chaoqiong Xiao, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is the transport layer for the Bulk Only protocol. */ /* */ /* INPUT */ /* */ /* storage Pointer to storage class */ /* data_pointer Pointer to data */ /* */ /* OUTPUT */ /* */ /* The transfer completion status. It's possible for the transfer to */ /* succeed but for the command to fail. The CSW must be checked to */ /* determine command status. */ /* */ /* CALLS */ /* */ /* _ux_host_class_storage_device_reset Reset mass storage */ /* _ux_host_stack_endpoint_reset Reset endpoint */ /* _ux_host_stack_transfer_request Process host stack transfer */ /* _ux_host_stack_transfer_request_abort Abort transfer request */ /* _ux_utility_memory_allocate Allocate memory */ /* _ux_utility_memory_free Free memory */ /* _ux_utility_long_get Get 32-bit word */ /* _ux_host_semaphore_get Get semaphore */ /* */ /* CALLED BY */ /* */ /* Storage Class */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ /* prefixed UX to MS_TO_TICK, */ /* resulting in version 6.1 */ /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */ /* refined macros names, */ /* resulting in version 6.1.10 */ /* */... /**************************************************************************/ UINT _ux_host_class_storage_transport_bo(UX_HOST_CLASS_STORAGE *storage, UCHAR *data_pointer) { UX_TRANSFER *transfer_request; UINT status; UCHAR *cbw; UINT retry; ULONG data_phase_requested_length; ULONG data_phase_transfer_size; UCHAR *get_status_response; /* Get the pointer to the transfer request. */ transfer_request = &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request; /* Use a pointer for the cbw, easier to manipulate. */ cbw = (UCHAR *) storage -> ux_host_class_storage_cbw; /* Fill in the transfer request parameters. */ transfer_request -> ux_transfer_request_data_pointer = cbw; transfer_request -> ux_transfer_request_requested_length = UX_HOST_CLASS_STORAGE_CBW_LENGTH; /* It's possible for the bulk out endpoint to stall; in that case, we clear it and try again. *//* ... */ for (retry = 0; ; retry++) { /* Send the CBW on the bulk out endpoint. */ status = _ux_host_stack_transfer_request(transfer_request); /* Check the status of the command (NB, the command is not sent yet since the bulk transport is non blocking). *//* ... */ if (status != UX_SUCCESS) return(status); /* Wait for the completion of the transfer request. */ status = _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT)); /* If the semaphore did not succeed we probably have a time out. */ if (status != UX_SUCCESS) { /* 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; /* Error trap. */ _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_TIMEOUT); /* If trace is enabled, insert this event into the trace buffer. */ UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_TIMEOUT, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0) /* There was an error, return to the caller. */ return(UX_TRANSFER_TIMEOUT); }if (status != UX_SUCCESS) { ... } /* Did we successfully send the CBW? */ if (transfer_request -> ux_transfer_request_completion_code == UX_SUCCESS) /* Jump to the data stage. */ break; /* The transfer stalled. Is this the first time? */ if (retry == 0) { /* We must do a reset recovery. */ _ux_host_class_storage_device_reset(storage); /* In virtually all cases, the reset should've fixed it, so just resend the command. *//* ... */ }if (retry == 0) { ... } else { /* Well, we tried! */ return(transfer_request -> ux_transfer_request_completion_code); }else { ... } }for (retry = 0; ; retry++) { ... } /* Get the length of the data payload. */ data_phase_requested_length = _ux_utility_long_get(cbw + UX_HOST_CLASS_STORAGE_CBW_DATA_LENGTH); /* Reset the data phase memory size. */ storage -> ux_host_class_storage_data_phase_length = 0; /* Perform the data stage - if there is any. */ while (data_phase_requested_length != 0) { /* Check if we can finish the transaction with one data phase. */ if (data_phase_requested_length > UX_HOST_CLASS_STORAGE_MAX_TRANSFER_SIZE) /* We have too much data to send in one phase. Split into smaller chunks. */ data_phase_transfer_size = UX_HOST_CLASS_STORAGE_MAX_TRANSFER_SIZE; else /* The transfer size can be the requested length. */ data_phase_transfer_size = data_phase_requested_length; /* Check the direction and determine which endpoint to use. */ if (*(cbw + UX_HOST_CLASS_STORAGE_CBW_FLAGS) == UX_HOST_CLASS_STORAGE_DATA_IN) transfer_request = &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request; /* Fill in the transfer request data payload buffer. */ transfer_request -> ux_transfer_request_data_pointer = data_pointer; /* Store the requested length in the transfer request. */ transfer_request -> ux_transfer_request_requested_length = data_phase_transfer_size; /* Perform data payload transfer (in or out). */ status = _ux_host_stack_transfer_request(transfer_request); /* Check the status of the data payload. */ if (status == UX_SUCCESS) /* Wait for the completion of the transfer request. */ status = _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT)); /* If the semaphore did not succeed we may have a time out or if we had a problem during the preparation of the transaction we should abort the SCSI transaction. *//* ... */ if (status != UX_SUCCESS) { /* 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; /* Error trap. */ _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_TIMEOUT); /* If trace is enabled, insert this event into the trace buffer. */ UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_TIMEOUT, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0) /* There was an error that cannot be recovered, return to the caller. */ return(UX_TRANSFER_TIMEOUT); }if (status != UX_SUCCESS) { ... } /* Check the transfer status. If there is a transport error, we still need to read the CSW and let the upper layer retry. *//* ... */ if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS) { /* This is most likely a STALL. We must clear it and go straight to reading the CSW. Note this doesn't necessarily mean the transfer failed completely failed. For example, if this was a read, it could mean the device had less data to send than we requested, but we still received data. *//* ... */ /* Check the direction and determine which endpoint to reset. */ if (*(cbw + UX_HOST_CLASS_STORAGE_CBW_FLAGS) == UX_HOST_CLASS_STORAGE_DATA_IN) _ux_host_stack_endpoint_reset(storage -> ux_host_class_storage_bulk_in_endpoint); else _ux_host_stack_endpoint_reset(storage -> ux_host_class_storage_bulk_out_endpoint); /* We need to read the CSW now. */ break; }if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS) { ... } /* Adjust the total size that was requested. */ data_phase_requested_length -= data_phase_transfer_size; /* And the data pointer. */ data_pointer += data_phase_transfer_size; }while (data_phase_requested_length != 0) { ... } /* Now perform the status phase, i.e. try to get the CSW from the device. According to the spec, if there is a failure the first time, we need to retry once before reporting an error. *//* ... */ /* Get the pointer to the transfer request, on the bulk in endpoint. */ transfer_request = &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request; /* Fill in the transfer_request parameters. */ transfer_request -> ux_transfer_request_data_pointer = (UCHAR *) &storage -> ux_host_class_storage_csw; transfer_request -> ux_transfer_request_requested_length = UX_HOST_CLASS_STORAGE_CSW_LENGTH; /* Retry loop, we have 2 tries here. */ for (retry = 0; retry < 2; retry++) { /* Get the CSW on the bulk in endpoint. */ status = _ux_host_stack_transfer_request(transfer_request); if (status != UX_SUCCESS) return(status); /* Wait for the completion of the transfer request. */ status = _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT)); /* Check semaphore status. */ if (status != UX_SUCCESS) { /* 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; /* Error trap. */ _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_TIMEOUT); /* If trace is enabled, insert this event into the trace buffer. */ UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_TIMEOUT, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0) /* There was an error, return to the caller. */ return(UX_TRANSFER_TIMEOUT); }if (status != UX_SUCCESS) { ... } /* Did the transfer succeed? */ if (transfer_request -> ux_transfer_request_completion_code == UX_SUCCESS) { /* The PASSED and COMMAND_FAILED error codes are nearly interchangeable according to the spec, so we treat them similarly. *//* ... */ if (storage -> ux_host_class_storage_csw[UX_HOST_CLASS_STORAGE_CSW_STATUS] == UX_HOST_CLASS_STORAGE_CSW_PASSED || storage -> ux_host_class_storage_csw[UX_HOST_CLASS_STORAGE_CSW_STATUS] == UX_HOST_CLASS_STORAGE_CSW_FAILED) { /* Was this an OUT transport? */ if (*(cbw + UX_HOST_CLASS_STORAGE_CBW_FLAGS) == UX_HOST_CLASS_STORAGE_DATA_OUT) { /* Did the command fail, or succeed with non-zero data residue? */ if (storage -> ux_host_class_storage_csw[UX_HOST_CLASS_STORAGE_CSW_STATUS] == UX_HOST_CLASS_STORAGE_CSW_FAILED || _ux_utility_long_get(storage -> ux_host_class_storage_csw + UX_HOST_CLASS_STORAGE_CSW_DATA_RESIDUE) != 0) { /* It's possible the bulk out endpoint is stalled. This happens when 1) the device expects less data than the host wants to send and 2) the endpoint stalls after the last packet was sent (otherwise, we'd have seen the stall during the data stage). Query its status before clearing the stall. *//* ... */ /* Allocate memory for Get Status response. */ get_status_response = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, 2); if (get_status_response == UX_NULL) return(UX_MEMORY_INSUFFICIENT); /* Get the control endpoint's transfer request. */ transfer_request = &storage -> ux_host_class_storage_device -> ux_device_control_endpoint.ux_endpoint_transfer_request; /* Setup transfer request for Get Status command. */ transfer_request -> ux_transfer_request_data_pointer = get_status_response; transfer_request -> ux_transfer_request_requested_length = 0x02; transfer_request -> ux_transfer_request_function = UX_GET_STATUS; transfer_request -> ux_transfer_request_type = UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_ENDPOINT; transfer_request -> ux_transfer_request_value = 0; transfer_request -> ux_transfer_request_index = storage -> ux_host_class_storage_bulk_out_endpoint-> ux_endpoint_descriptor.bEndpointAddress; /* Send transfer request. */ status = _ux_host_stack_transfer_request(transfer_request); /* Check status of transfer. */ if (status == UX_SUCCESS) { /* Check the Halt bit. */ if (get_status_response[0] & 0x01) { /* Clear the halt. */ status = _ux_host_stack_endpoint_reset(storage -> ux_host_class_storage_bulk_out_endpoint); }if (get_status_response[0] & 0x01) { ... } }if (status == UX_SUCCESS) { ... } /* Free the get status memory. */ _ux_utility_memory_free(get_status_response); /* Check result of Get Status and Clear Feature commands. */ if (status != UX_SUCCESS) return(status); }if (storage -> ux_host_class_storage_csw[UX_HOST_CLASS_STORAGE_CSW_STATUS] == UX_HOST_CLASS_STORAGE_CSW_FAILED || _ux_utility_long_get(storage -> ux_host_class_storage_csw + UX_HOST_CLASS_STORAGE_CSW_DATA_RESIDUE) != 0) { ... } }if (*(cbw + UX_HOST_CLASS_STORAGE_CBW_FLAGS) == UX_HOST_CLASS_STORAGE_DATA_OUT) { ... } /* Save the amount of relevant data. */ storage -> ux_host_class_storage_data_phase_length = _ux_utility_long_get(cbw + UX_HOST_CLASS_STORAGE_CBW_DATA_LENGTH) - _ux_utility_long_get(storage -> ux_host_class_storage_csw + UX_HOST_CLASS_STORAGE_CSW_DATA_RESIDUE); }if (storage -> ux_host_class_storage_csw[UX_HOST_CLASS_STORAGE_CSW_STATUS] == UX_HOST_CLASS_STORAGE_CSW_PASSED || storage -> ux_host_class_storage_csw[UX_HOST_CLASS_STORAGE_CSW_STATUS] == UX_HOST_CLASS_STORAGE_CSW_FAILED) { ... } else { /* Must be phase error. Need to reset the device. */ _ux_host_class_storage_device_reset(storage); }else { ... } /* Return success since the transfer succeeded. The caller should look at the CSW to determine if the command's status. *//* ... */ return(UX_SUCCESS); }if (transfer_request -> ux_transfer_request_completion_code == UX_SUCCESS) { ... } /* The transfer stalled. We must clear the stall and attempt to read the CSW again. *//* ... */ _ux_host_stack_endpoint_reset(storage -> ux_host_class_storage_bulk_in_endpoint); }for (retry = 0; retry < 2; retry++) { ... } /* If we get here, the CSW transfer stalled twice. We must reset the device. */ _ux_host_class_storage_device_reset(storage); /* Return the error. */ return(transfer_request -> ux_transfer_request_completion_code); }{ ... } #endif...