1
10
13
14
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
256
261
266
271
276
281
286
291
296
301
302
307
313
319
325
331
337
338
339
340
341
342
347
352
357
362
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
396
398
399
400
401
402
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
453
454
455
456
457
458
459
461
462
463
464
...
...
...
#define UX_SOURCE_CODE
#include "ux_api.h"
#include "ux_device_class_storage.h"
#include "ux_device_stack.h"
#if !defined(UX_DEVICE_STANDALONE)...
...
VOID _ux_device_class_storage_thread(ULONG storage_class)
{
UX_SLAVE_CLASS *class;
UX_SLAVE_CLASS_STORAGE *storage;
UX_SLAVE_TRANSFER *transfer_request;
UX_SLAVE_DEVICE *device;
UX_SLAVE_INTERFACE *interface;
UX_SLAVE_ENDPOINT *endpoint_in;
UX_SLAVE_ENDPOINT *endpoint_out;
UINT status;
ULONG length;
ULONG cbwcb_length;
ULONG lun;
UCHAR *scsi_command;
UCHAR *cbw_cb;
while(1)
{
UX_THREAD_EXTENSION_PTR_GET(class, UX_SLAVE_CLASS, storage_class)
storage = (UX_SLAVE_CLASS_STORAGE *) class -> ux_slave_class_instance;
device = &_ux_system_slave -> ux_system_slave_device;
while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
{
status = UX_ERROR;
interface = storage -> ux_slave_class_storage_interface;
status = UX_ERROR;
endpoint_in = interface -> ux_slave_interface_first_endpoint;
if ((endpoint_in -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN)
{
endpoint_out = endpoint_in;
endpoint_in = endpoint_out -> ux_slave_endpoint_next_endpoint;
}if ((endpoint_in -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN) { ... }
else
{
endpoint_out = endpoint_in -> ux_slave_endpoint_next_endpoint;
}else { ... }
transfer_request = &endpoint_out -> ux_slave_endpoint_transfer_request;
if (endpoint_out -> ux_slave_endpoint_state == UX_ENDPOINT_RESET &&
(UCHAR)storage -> ux_slave_class_storage_csw_status != UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR)
{
status = _ux_device_stack_transfer_request(transfer_request, 64, 64);
}if (endpoint_out -> ux_slave_endpoint_state == UX_ENDPOINT_RESET && (UCHAR)storage -> ux_slave_class_storage_csw_status != UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR) { ... }
/* ... */
if (status == UX_SUCCESS)
{
length = transfer_request -> ux_slave_transfer_request_actual_length;
scsi_command = transfer_request -> ux_slave_transfer_request_data_pointer;
lun = (ULONG) *(scsi_command + UX_SLAVE_CLASS_STORAGE_CBW_LUN);
storage -> ux_slave_class_storage_cbw_lun = (UCHAR)lun;
storage -> ux_slave_class_storage_scsi_tag = _ux_utility_long_get(scsi_command + UX_SLAVE_CLASS_STORAGE_CBW_TAG);
storage -> ux_slave_class_storage_host_length = _ux_utility_long_get(scsi_command + UX_SLAVE_CLASS_STORAGE_CBW_DATA_LENGTH);
storage -> ux_slave_class_storage_cbw_flags = *(scsi_command + UX_SLAVE_CLASS_STORAGE_CBW_FLAGS);
storage -> ux_slave_class_storage_csw_residue = 0;
storage -> ux_slave_class_storage_csw_status = 0;
/* ... */
if ((lun < storage -> ux_slave_class_storage_number_lun) && (length == UX_SLAVE_CLASS_STORAGE_CBW_LENGTH))
{
if (_ux_utility_long_get(scsi_command) == UX_SLAVE_CLASS_STORAGE_CBW_SIGNATURE_MASK)
{
cbwcb_length = (ULONG) *(scsi_command + UX_SLAVE_CLASS_STORAGE_CBW_CB_LENGTH);
if (cbwcb_length != 0)
{
cbw_cb = scsi_command + UX_SLAVE_CLASS_STORAGE_CBW_CB;
switch (*(cbw_cb))
{
case UX_SLAVE_CLASS_STORAGE_SCSI_TEST_READY:
_ux_device_class_storage_test_ready(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_TEST_READY:
case UX_SLAVE_CLASS_STORAGE_SCSI_REQUEST_SENSE:
_ux_device_class_storage_request_sense(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_REQUEST_SENSE:
case UX_SLAVE_CLASS_STORAGE_SCSI_FORMAT:
_ux_device_class_storage_format(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_FORMAT:
case UX_SLAVE_CLASS_STORAGE_SCSI_INQUIRY:
_ux_device_class_storage_inquiry(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_INQUIRY:
case UX_SLAVE_CLASS_STORAGE_SCSI_START_STOP:
_ux_device_class_storage_start_stop(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_START_STOP:
case UX_SLAVE_CLASS_STORAGE_SCSI_PREVENT_ALLOW_MEDIA_REMOVAL:
_ux_device_class_storage_prevent_allow_media_removal(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_PREVENT_ALLOW_MEDIA_REMOVAL:
case UX_SLAVE_CLASS_STORAGE_SCSI_READ_FORMAT_CAPACITY:
_ux_device_class_storage_read_format_capacity(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_READ_FORMAT_CAPACITY:
case UX_SLAVE_CLASS_STORAGE_SCSI_READ_CAPACITY:
_ux_device_class_storage_read_capacity(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_READ_CAPACITY:
case UX_SLAVE_CLASS_STORAGE_SCSI_VERIFY:
_ux_device_class_storage_verify(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_VERIFY:
case UX_SLAVE_CLASS_STORAGE_SCSI_MODE_SELECT:
_ux_device_class_storage_mode_select(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_MODE_SELECT:
case UX_SLAVE_CLASS_STORAGE_SCSI_MODE_SENSE_SHORT:
case UX_SLAVE_CLASS_STORAGE_SCSI_MODE_SENSE:
_ux_device_class_storage_mode_sense(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_MODE_SENSE:
case UX_SLAVE_CLASS_STORAGE_SCSI_READ32:
_ux_device_class_storage_read(storage, lun, endpoint_in, endpoint_out, cbw_cb,
UX_SLAVE_CLASS_STORAGE_SCSI_READ32);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_READ32:
case UX_SLAVE_CLASS_STORAGE_SCSI_READ16:
_ux_device_class_storage_read(storage, lun, endpoint_in, endpoint_out, cbw_cb,
UX_SLAVE_CLASS_STORAGE_SCSI_READ16);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_READ16:
case UX_SLAVE_CLASS_STORAGE_SCSI_WRITE32:
_ux_device_class_storage_write(storage, lun, endpoint_in, endpoint_out, cbw_cb,
UX_SLAVE_CLASS_STORAGE_SCSI_WRITE32);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_WRITE32:
case UX_SLAVE_CLASS_STORAGE_SCSI_WRITE16:
_ux_device_class_storage_write(storage, lun, endpoint_in, endpoint_out, cbw_cb,
UX_SLAVE_CLASS_STORAGE_SCSI_WRITE16);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_WRITE16:
case UX_SLAVE_CLASS_STORAGE_SCSI_SYNCHRONIZE_CACHE:
_ux_device_class_storage_synchronize_cache(storage, lun, endpoint_in, endpoint_out, cbw_cb, *(cbw_cb));
break;
#ifdef UX_SLAVE_CLASS_STORAGE_INCLUDE_MMCcase UX_SLAVE_CLASS_STORAGE_SCSI_SYNCHRONIZE_CACHE:
case UX_SLAVE_CLASS_STORAGE_SCSI_GET_STATUS_NOTIFICATION:
_ux_device_class_storage_get_status_notification(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_GET_STATUS_NOTIFICATION:
case UX_SLAVE_CLASS_STORAGE_SCSI_GET_CONFIGURATION:
_ux_device_class_storage_get_configuration(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_GET_CONFIGURATION:
case UX_SLAVE_CLASS_STORAGE_SCSI_READ_DISK_INFORMATION:
_ux_device_class_storage_read_disk_information(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_READ_DISK_INFORMATION:
case UX_SLAVE_CLASS_STORAGE_SCSI_REPORT_KEY:
_ux_device_class_storage_report_key(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_REPORT_KEY:
case UX_SLAVE_CLASS_STORAGE_SCSI_GET_PERFORMANCE:
_ux_device_class_storage_get_performance(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_GET_PERFORMANCE:
case UX_SLAVE_CLASS_STORAGE_SCSI_READ_DVD_STRUCTURE:
_ux_device_class_storage_read_dvd_structure(storage, lun, endpoint_in, endpoint_out, cbw_cb);
break;
case UX_SLAVE_CLASS_STORAGE_SCSI_READ_DVD_STRUCTURE:
case UX_SLAVE_CLASS_STORAGE_SCSI_READ_TOC:
status = _ux_device_class_storage_read_toc(storage, lun, endpoint_in, endpoint_out, cbw_cb);
if (status == UX_SUCCESS)
break;/* ... */
#endif
default:
if (storage -> ux_slave_class_storage_host_length > 0 &&
((storage -> ux_slave_class_storage_cbw_flags & 0x80) == 0))
_ux_device_stack_endpoint_stall(endpoint_out);
else
_ux_device_stack_endpoint_stall(endpoint_in);
storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status =
UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(UX_SLAVE_CLASS_STORAGE_SENSE_KEY_ILLEGAL_REQUEST,
UX_SLAVE_CLASS_STORAGE_ASC_KEY_INVALID_COMMAND,0);
/* ... */
while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
{
if (endpoint_in -> ux_slave_endpoint_state == UX_ENDPOINT_RESET)
{
storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
break;
}if (endpoint_in -> ux_slave_endpoint_state == UX_ENDPOINT_RESET) { ... }
else
_ux_device_thread_relinquish();
}while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED) { ... }
break;default
}switch (*(cbw_cb)) { ... }
status = _ux_device_class_storage_csw_send(storage, lun, endpoint_in, 0 );
if (status != UX_SUCCESS)
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
}if (cbwcb_length != 0) { ... }
else
storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR;
}if (_ux_utility_long_get(scsi_command) == UX_SLAVE_CLASS_STORAGE_CBW_SIGNATURE_MASK) { ... }
else
storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR;
}if ((lun < storage -> ux_slave_class_storage_number_lun) && (length == UX_SLAVE_CLASS_STORAGE_CBW_LENGTH)) { ... }
else
storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR;
}if (status == UX_SUCCESS) { ... }
else
{
if ((UCHAR)storage -> ux_slave_class_storage_csw_status == UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR)
{
_ux_device_stack_endpoint_stall(endpoint_out);
_ux_device_stack_endpoint_stall(endpoint_in);
}if ((UCHAR)storage -> ux_slave_class_storage_csw_status == UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR) { ... }
_ux_utility_delay_ms(2);
}else { ... }
}while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED) { ... }
/* ... */
_ux_device_thread_suspend(&class -> ux_slave_class_thread);
}while (1) { ... }
}{ ... }
#endif...