-
Notifications
You must be signed in to change notification settings - Fork 76
/
usb_mass.c
438 lines (379 loc) · 13.9 KB
/
usb_mass.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
396
397
398
399
400
401
402
403
404
405
406
407
408
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
#include <string.h>
#include "usb_generic.h"
#include "usb_mass.h"
#include "usb_scsi.h"
#include "usb_mass_internal.h"
#include <libmaple/usb.h>
#include <libmaple/delay.h>
/* Private headers */
#include "usb_lib_globals.h"
#include "usb_reg_map.h"
#include "usb_regs.h"
/* usb_lib headers */
#include "usb_type.h"
#include "usb_core.h"
#include "usb_def.h"
static void usb_mass_bot_cbw_decode();
static void usb_mass_set_configuration();
static void usb_mass_clear_feature();
static RESULT usb_mass_data_setup(uint8 request, uint8 interface, uint8 requestType, uint8 wValue0, uint8 wValue1, uint16 wIndex, uint16 wLength);
static RESULT usb_mass_no_data_setup(uint8 request, uint8 interface, uint8 requestType, uint8 wValue0, uint8 wValue1, uint16 wIndex);
static void usb_mass_reset();
static uint8_t* usb_mass_get_max_lun(uint16_t Length);
static void usb_mass_in(void);
static void usb_mass_out(void);
uint32_t usb_mass_sil_write(uint8_t* pBufferPointer, uint32_t wBufferSize);
uint32_t usb_mass_sil_read(uint8_t* pBufferPointer);
//#define MASS_INTERFACE_OFFSET 0x00
//#define MASS_INTERFACE_NUMBER (MASS_INTERFACE_OFFSET+usbMassPart.startInterface)
#define LUN_DATA_LENGTH 1
static uint32_t maxLun = 0;
static uint32_t deviceState = DEVICE_STATE_UNCONNECTED;
uint8_t usb_mass_botState = BOT_STATE_IDLE;
BulkOnlyCBW usb_mass_CBW;
BulkOnlyCSW usb_mass_CSW;
uint8_t usb_mass_bulkDataBuff[MAX_BULK_PACKET_SIZE];
uint16_t usb_mass_dataLength;
static uint8_t inRequestPending;
static uint8_t outRequestPending;
typedef struct mass_descriptor_config {
// usb_descriptor_config_header Config_Header;
usb_descriptor_interface MASS_Interface;
usb_descriptor_endpoint DataInEndpoint;
usb_descriptor_endpoint DataOutEndpoint;
} __packed mass_descriptor_config;
#define MAX_POWER (500 >> 1)
const mass_descriptor_config usbMassConfigDescriptor = {
/*.Config_Header =
{
.bLength = sizeof (usb_descriptor_config_header),
.bDescriptorType = USB_DESCRIPTOR_TYPE_CONFIGURATION,
.wTotalLength = sizeof (usb_descriptor_config),
.bNumInterfaces = 0x01,
.bConfigurationValue = 0x01,
.iConfiguration = 0x00,
.bmAttributes = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELF_POWERED),
.bMaxPower = MAX_POWER,
}, */
.MASS_Interface =
{
.bLength = sizeof (usb_descriptor_interface),
.bDescriptorType = USB_DESCRIPTOR_TYPE_INTERFACE,
.bInterfaceNumber = 0x00, // PATCH
.bAlternateSetting = 0x00,
.bNumEndpoints = 0x02,
.bInterfaceClass = 8, // mass storage
.bInterfaceSubClass = 6, // SCSI
.bInterfaceProtocol = 0x50, // Bulk-Only
.iInterface = 0,
},
.DataInEndpoint =
{
.bLength = sizeof (usb_descriptor_endpoint),
.bDescriptorType = USB_DESCRIPTOR_TYPE_ENDPOINT,
.bEndpointAddress = (USB_DESCRIPTOR_ENDPOINT_IN | 0), // PATCH: USB_MASS_TX_ENDP
.bmAttributes = USB_EP_TYPE_BULK,
.wMaxPacketSize = MAX_BULK_PACKET_SIZE,
.bInterval = 0,
},
.DataOutEndpoint =
{
.bLength = sizeof (usb_descriptor_endpoint),
.bDescriptorType = USB_DESCRIPTOR_TYPE_ENDPOINT,
.bEndpointAddress = (USB_DESCRIPTOR_ENDPOINT_OUT | 0), // PATCH: USB_MASS_RX_ENDP
.bmAttributes = USB_EP_TYPE_BULK,
.wMaxPacketSize = MAX_BULK_PACKET_SIZE,
.bInterval = 1,
}
};
USBEndpointInfo usbMassEndpoints[2] = {
{
.callback = usb_mass_in,
.pmaSize = MAX_BULK_PACKET_SIZE,
.type = USB_GENERIC_ENDPOINT_TYPE_BULK,
.tx = 1,
},
{
.callback = usb_mass_out,
.pmaSize = MAX_BULK_PACKET_SIZE,
.type = USB_GENERIC_ENDPOINT_TYPE_BULK,
.tx = 0,
},
};
#define OUT_BYTE(s,v) out[(uint8*)&(s.v)-(uint8*)&s]
static void getMassPartDescriptor(uint8* out) {
memcpy(out, &usbMassConfigDescriptor, sizeof(mass_descriptor_config));
// patch to reflect where the part goes in the descriptor
OUT_BYTE(usbMassConfigDescriptor, MASS_Interface.bInterfaceNumber) += usbMassPart.startInterface;
OUT_BYTE(usbMassConfigDescriptor, DataInEndpoint.bEndpointAddress) += USB_MASS_TX_ENDP;
OUT_BYTE(usbMassConfigDescriptor, DataOutEndpoint.bEndpointAddress) += USB_MASS_RX_ENDP;
}
USBCompositePart usbMassPart = {
.numInterfaces = 1,
.numEndpoints = sizeof(usbMassEndpoints)/sizeof(*usbMassEndpoints),
.descriptorSize = sizeof(mass_descriptor_config),
.getPartDescriptor = getMassPartDescriptor,
.usbInit = NULL,
.usbReset = usb_mass_reset,
.usbDataSetup = usb_mass_data_setup,
.usbNoDataSetup = usb_mass_no_data_setup,
.usbClearFeature = usb_mass_clear_feature,
.usbSetConfiguration = usb_mass_set_configuration,
.endpoints = usbMassEndpoints
};
static void usb_mass_reset(void) {
usb_mass_mal_init(0);
pInformation->Current_Configuration = 0; // TODO: remove?
/* current feature is current bmAttributes */
pInformation->Current_Feature = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELF_POWERED); // usbMassConfigDescriptor.Config_Header.bmAttributes; // TODO: remove?
deviceState = DEVICE_STATE_ATTACHED;
usb_mass_CBW.dSignature = BOT_CBW_SIGNATURE;
usb_mass_botState = BOT_STATE_IDLE;
}
static void usb_mass_set_configuration(void) {
if (pInformation->Current_Configuration != 0) {
deviceState = USB_CONFIGURED;
usb_mass_botState = BOT_STATE_IDLE;
}
}
static void usb_mass_clear_feature(void) {
/* when the host send a usb_mass_CBW with invalid signature or invalid length the two
Endpoints (IN & OUT) shall stall until receiving a Mass Storage Reset */
if (usb_mass_CBW.dSignature != BOT_CBW_SIGNATURE) {
usb_mass_bot_abort(BOT_DIR_BOTH);
}
}
#pragma GCC diagnostic ignored "-Wunused-parameter"
static RESULT usb_mass_data_setup(uint8 request, uint8 interface, uint8 requestType, uint8 wValue0, uint8 wValue1, uint16 wIndex, uint16 wLength) {
(void)interface; // only one interface
uint8_t * (*copy_routine)(uint16_t);
copy_routine = NULL;
if ((requestType & (REQUEST_TYPE | RECIPIENT)) == (CLASS_REQUEST | INTERFACE_RECIPIENT) && request == REQUEST_GET_MAX_LUN && wValue0 == 0 && wValue1 == 0 && wLength == 0x01) {
copy_routine = usb_mass_get_max_lun;
} else {
return USB_UNSUPPORT;
}
if (copy_routine == NULL) {
return USB_UNSUPPORT;
}
pInformation->Ctrl_Info.CopyData = copy_routine;
pInformation->Ctrl_Info.Usb_wOffset = 0;
(*copy_routine)(0);
return USB_SUCCESS;
}
static uint8_t* usb_mass_get_max_lun(uint16_t length) {
if (length == 0) {
pInformation->Ctrl_Info.Usb_wLength = LUN_DATA_LENGTH;
return 0;
} else {
return ((uint8_t*) (&maxLun));
}
}
static RESULT usb_mass_no_data_setup(uint8 request, uint8 interface, uint8 requestType, uint8 wValue0, uint8 wValue1, uint16 wIndex) {
if ((requestType & (REQUEST_TYPE | RECIPIENT)) == (CLASS_REQUEST | INTERFACE_RECIPIENT) && request == REQUEST_MASS_STORAGE_RESET && wValue0 == 0 && wValue1 == 0) {
/*initialize the usb_mass_CBW signature to enable the clear feature*/
usb_mass_CBW.dSignature = BOT_CBW_SIGNATURE;
usb_mass_botState = BOT_STATE_IDLE;
return USB_SUCCESS;
}
return USB_UNSUPPORT;
}
void usb_mass_loop() {
if (inRequestPending) {
inRequestPending = 0;
switch (usb_mass_botState) {
case BOT_STATE_CSW_Send:
case BOT_STATE_ERROR:
usb_mass_botState = BOT_STATE_IDLE;
usb_generic_enable_rx(USB_MASS_RX_ENDPOINT_INFO); /* enable the Endpoint to receive the next cmd*/
break;
case BOT_STATE_DATA_IN:
switch (usb_mass_CBW.CB[0]) {
case SCSI_READ10:
scsi_read10_cmd(usb_mass_CBW.bLUN, SCSI_lba, SCSI_blkLen);
break;
}
break;
case BOT_STATE_DATA_IN_LAST:
usb_mass_bot_set_csw(BOT_CSW_CMD_PASSED, BOT_SEND_CSW_ENABLE);
SetEPRxStatus(USB_MASS_RX_ENDP, USB_EP_ST_RX_VAL);
break;
default:
break;
}
}
if (outRequestPending) {
outRequestPending = 0;
uint8_t CMD;
CMD = usb_mass_CBW.CB[0];
switch (usb_mass_botState) {
case BOT_STATE_IDLE:
usb_mass_bot_cbw_decode();
break;
case BOT_STATE_DATA_OUT:
if (CMD == SCSI_WRITE10) {
scsi_write10_cmd(usb_mass_CBW.bLUN, SCSI_lba, SCSI_blkLen);
break;
}
usb_mass_bot_abort(BOT_DIR_OUT);
scsi_set_sense_data(usb_mass_CBW.bLUN, SCSI_ILLEGAL_REQUEST, SCSI_INVALID_FIELED_IN_COMMAND);
usb_mass_bot_set_csw(BOT_CSW_PHASE_ERROR, BOT_SEND_CSW_DISABLE);
break;
default:
usb_mass_bot_abort(BOT_DIR_BOTH);
scsi_set_sense_data(usb_mass_CBW.bLUN, SCSI_ILLEGAL_REQUEST, SCSI_INVALID_FIELED_IN_COMMAND);
usb_mass_bot_set_csw(BOT_CSW_PHASE_ERROR, BOT_SEND_CSW_DISABLE);
break;
}
}
}
/*
* IN
*/
static void usb_mass_in(void) {
inRequestPending = 1;
}
/*
* OUT
*/
static void usb_mass_out(void) {
usb_mass_dataLength = usb_mass_sil_read(usb_mass_bulkDataBuff);
outRequestPending = 1;
}
static void usb_mass_bot_cbw_decode() {
uint32_t counter;
for (counter = 0; counter < usb_mass_dataLength; counter++) {
*((uint8_t *) & usb_mass_CBW + counter) = usb_mass_bulkDataBuff[counter];
}
usb_mass_CSW.dTag = usb_mass_CBW.dTag;
usb_mass_CSW.dDataResidue = usb_mass_CBW.dDataLength;
if (usb_mass_dataLength != BOT_CBW_PACKET_LENGTH) {
usb_mass_bot_abort(BOT_DIR_BOTH);
/* reset the usb_mass_CBW.dSignature to disable the clear feature until receiving a Mass storage reset*/
usb_mass_CBW.dSignature = 0;
scsi_set_sense_data(usb_mass_CBW.bLUN, SCSI_ILLEGAL_REQUEST, SCSI_PARAMETER_LIST_LENGTH_ERROR);
usb_mass_bot_set_csw(BOT_CSW_CMD_FAILED, BOT_SEND_CSW_DISABLE);
return;
}
if ((usb_mass_CBW.CB[0] == SCSI_READ10) || (usb_mass_CBW.CB[0] == SCSI_WRITE10)) {
/* Calculate Logical Block Address */
SCSI_lba = (usb_mass_CBW.CB[2] << 24) | (usb_mass_CBW.CB[3] << 16) | (usb_mass_CBW.CB[4] << 8) | usb_mass_CBW.CB[5];
/* Calculate the Number of Blocks to transfer */
SCSI_blkLen = (usb_mass_CBW.CB[7] << 8) | usb_mass_CBW.CB[8];
}
if (usb_mass_CBW.dSignature == BOT_CBW_SIGNATURE) {
/* Valid usb_mass_CBW */
if ((usb_mass_CBW.bLUN > maxLun) || (usb_mass_CBW.bCBLength < 1) || (usb_mass_CBW.bCBLength > 16)) {
usb_mass_bot_abort(BOT_DIR_BOTH);
scsi_set_sense_data(usb_mass_CBW.bLUN, SCSI_ILLEGAL_REQUEST, SCSI_INVALID_FIELED_IN_COMMAND);
usb_mass_bot_set_csw(BOT_CSW_CMD_FAILED, BOT_SEND_CSW_DISABLE);
} else {
switch (usb_mass_CBW.CB[0]) {
case SCSI_REQUEST_SENSE:
scsi_request_sense_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_INQUIRY:
scsi_inquiry_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_START_STOP_UNIT:
scsi_start_stop_unit_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_ALLOW_MEDIUM_REMOVAL:
scsi_start_stop_unit_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_MODE_SENSE6:
scsi_mode_sense6_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_MODE_SENSE10:
scsi_mode_sense10_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_READ_FORMAT_CAPACITIES:
scsi_read_format_capacity_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_READ_CAPACITY10:
scsi_read_capacity10_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_TEST_UNIT_READY:
scsi_test_unit_ready_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_READ10:
scsi_read10_cmd(usb_mass_CBW.bLUN, SCSI_lba, SCSI_blkLen);
break;
case SCSI_WRITE10:
scsi_write10_cmd(usb_mass_CBW.bLUN, SCSI_lba, SCSI_blkLen);
break;
case SCSI_VERIFY10:
scsi_verify10_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_FORMAT_UNIT:
scsi_format_cmd(usb_mass_CBW.bLUN);
break;
case SCSI_MODE_SELECT10:
case SCSI_MODE_SELECT6:
case SCSI_SEND_DIAGNOSTIC:
case SCSI_READ6:
case SCSI_READ12:
case SCSI_READ16:
case SCSI_READ_CAPACITY16:
case SCSI_WRITE6:
case SCSI_WRITE12:
case SCSI_VERIFY12:
case SCSI_VERIFY16:
case SCSI_WRITE16:
scsi_invalid_cmd(usb_mass_CBW.bLUN);
break;
default:
{
usb_mass_bot_abort(BOT_DIR_BOTH);
scsi_set_sense_data(usb_mass_CBW.bLUN, SCSI_ILLEGAL_REQUEST, SCSI_INVALID_COMMAND);
usb_mass_bot_set_csw(BOT_CSW_CMD_FAILED, BOT_SEND_CSW_DISABLE);
}
}
}
} else {
/* Invalid usb_mass_CBW */
usb_mass_bot_abort(BOT_DIR_BOTH);
scsi_set_sense_data(usb_mass_CBW.bLUN, SCSI_ILLEGAL_REQUEST, SCSI_INVALID_COMMAND);
usb_mass_bot_set_csw(BOT_CSW_CMD_FAILED, BOT_SEND_CSW_DISABLE);
}
}
void usb_mass_bot_abort(uint8_t direction) {
switch (direction) {
case BOT_DIR_IN:
usb_generic_stall_tx(USB_MASS_TX_ENDPOINT_INFO);
break;
case BOT_DIR_OUT:
usb_generic_stall_rx(USB_MASS_RX_ENDPOINT_INFO);
break;
case BOT_DIR_BOTH:
usb_generic_stall_rx(USB_MASS_RX_ENDPOINT_INFO);
usb_generic_stall_tx(USB_MASS_TX_ENDPOINT_INFO);
break;
default:
break;
}
}
void usb_mass_transfer_data_request(uint8_t* dataPointer, uint16_t dataLen) {
usb_mass_sil_write(dataPointer, dataLen);
usb_mass_botState = BOT_STATE_DATA_IN_LAST;
usb_mass_CSW.dDataResidue -= dataLen;
usb_mass_CSW.bStatus = BOT_CSW_CMD_PASSED;
}
void usb_mass_bot_set_csw(uint8_t status, uint8_t sendPermission) {
usb_mass_CSW.dSignature = BOT_CSW_SIGNATURE;
usb_mass_CSW.bStatus = status;
if (sendPermission) {
usb_mass_botState = BOT_STATE_CSW_Send;
usb_mass_sil_write(((uint8_t *) & usb_mass_CSW), BOT_CSW_DATA_LENGTH);
}
else {
usb_mass_botState = BOT_STATE_ERROR;
}
}
uint32_t usb_mass_sil_write(uint8_t* pBufferPointer, uint32_t wBufferSize) {
usb_generic_send_from_buffer(USB_MASS_TX_ENDPOINT_INFO, pBufferPointer, wBufferSize);
return 0;
}
uint32_t usb_mass_sil_read(uint8_t* pBufferPointer) {
return usb_generic_read_to_buffer(USB_MASS_RX_ENDPOINT_INFO, pBufferPointer, USB_GENERIC_UNLIMITED_BUFFER);
}