1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
24
25
26
27
28
29
30
31
32
35
36
37
41
42
45
48
49
50
53
54
57
58
59
62
65
66
67
70
71
72
75
76
77
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
106
107
108
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
130
131
132
133
137
141
145
149
153
157
161
165
169
173
177
181
185
189
193
198
199
200
201
202
203
204
211
212
213
214
215
216
220
221
222
228
229
235
236
243
244
245
246
247
248
249
256
257
258
259
260
261
262
266
267
272
273
274
291
292
293
294
295
296
297
298
302
303
304
305
306
307
308
309
310
317
318
319
320
321
322
323
327
328
329
330
331
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
361
362
363
364
365
366
367
368
372
373
374
375
376
381
382
383
384
385
386
387
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
418
419
420
421
422
423
424
425
426
427
431
432
433
434
439
440
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
469
470
471
472
473
474
475
479
480
481
485
486
490
491
492
493
494
495
496
497
504
505
506
507
508
509
510
514
515
516
520
521
525
526
527
528
529
530
531
532
539
540
541
542
543
544
545
549
550
555
556
560
561
562
563
564
565
566
567
568
569
570
571
575
576
577
578
579
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
606
607
608
609
610
611
612
616
617
618
619
626
627
628
629
630
631
635
636
642
643
644
647
648
651
652
655
659
660
661
662
663
664
665
672
673
674
675
676
677
681
682
686
690
691
692
693
694
695
696
697
704
705
706
707
708
712
713
714
715
716
721
722
728
729
734
735
736
737
738
739
740
741
742
747
748
749
754
755
756
757
758
759
760
761
762
763
770
771
772
773
774
778
779
780
781
782
787
788
793
794
799
800
801
802
803
804
805
806
807
808
809
810
815
816
817
822
823
824
825
826
827
828
829
830
831
838
839
840
841
842
843
847
848
849
851
852
853
854
855
860
861
862
867
868
869
874
875
876
881
882
883
884
885
886
887
888
889
890
891
896
897
898
899
900
905
906
907
908
909
910
911
912
916
917
918
919
920
921
928
929
930
931
932
933
937
938
940
941
942
943
944
949
950
951
956
957
958
964
965
966
972
973
974
975
976
977
978
979
980
981
982
983
984
989
990
991
992
993
998
999
1000
1001
1002
1003
1004
1005
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1029
1030
1035
1036
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1063
1064
1069
1070
1071
1072
1073
1079
1080
1081
1082
1083
1084
1088
1089
1090
1091
1092
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1118
1119
1120
1121
1122
1128
1129
1130
1131
1132
1133
1137
1138
1139
1140
1141
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1194
1195
1196
1197
1202
1203
1204
1205
1208
1209
1210
1213
1214
1215
1218
/* ... */
/* ... */
#include "usbd_msc_bot.h"
#include "usbd_msc_scsi.h"
#include "usbd_msc.h"
#include "usbd_msc_data.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
extern uint8_t MSCInEpAdd;
extern uint8_t MSCOutEpAdd;
/* ... */
/* ... */
static int8_t SCSI_TestUnitReady(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_ReadCapacity16(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_RequestSense(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_AllowPreventRemovable(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_ModeSense6(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_ModeSense10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_Write10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_Write12(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_Read12(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_Verify10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params);
static int8_t SCSI_CheckAddressRange(USBD_HandleTypeDef *pdev, uint8_t lun,
uint32_t blk_offset, uint32_t blk_nbr);
static int8_t SCSI_ProcessRead(USBD_HandleTypeDef *pdev, uint8_t lun);
static int8_t SCSI_ProcessWrite(USBD_HandleTypeDef *pdev, uint8_t lun);
static int8_t SCSI_UpdateBotData(USBD_MSC_BOT_HandleTypeDef *hmsc,
uint8_t *pBuff, uint16_t length);
/* ... */
/* ... */
/* ... */
int8_t SCSI_ProcessCmd(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *cmd)
{
int8_t ret;
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
switch (cmd[0])
{
case SCSI_TEST_UNIT_READY:
ret = SCSI_TestUnitReady(pdev, lun, cmd);
break;
case SCSI_TEST_UNIT_READY:
case SCSI_REQUEST_SENSE:
ret = SCSI_RequestSense(pdev, lun, cmd);
break;
case SCSI_REQUEST_SENSE:
case SCSI_INQUIRY:
ret = SCSI_Inquiry(pdev, lun, cmd);
break;
case SCSI_INQUIRY:
case SCSI_START_STOP_UNIT:
ret = SCSI_StartStopUnit(pdev, lun, cmd);
break;
case SCSI_START_STOP_UNIT:
case SCSI_ALLOW_MEDIUM_REMOVAL:
ret = SCSI_AllowPreventRemovable(pdev, lun, cmd);
break;
case SCSI_ALLOW_MEDIUM_REMOVAL:
case SCSI_MODE_SENSE6:
ret = SCSI_ModeSense6(pdev, lun, cmd);
break;
case SCSI_MODE_SENSE6:
case SCSI_MODE_SENSE10:
ret = SCSI_ModeSense10(pdev, lun, cmd);
break;
case SCSI_MODE_SENSE10:
case SCSI_READ_FORMAT_CAPACITIES:
ret = SCSI_ReadFormatCapacity(pdev, lun, cmd);
break;
case SCSI_READ_FORMAT_CAPACITIES:
case SCSI_READ_CAPACITY10:
ret = SCSI_ReadCapacity10(pdev, lun, cmd);
break;
case SCSI_READ_CAPACITY10:
case SCSI_READ_CAPACITY16:
ret = SCSI_ReadCapacity16(pdev, lun, cmd);
break;
case SCSI_READ_CAPACITY16:
case SCSI_READ10:
ret = SCSI_Read10(pdev, lun, cmd);
break;
case SCSI_READ10:
case SCSI_READ12:
ret = SCSI_Read12(pdev, lun, cmd);
break;
case SCSI_READ12:
case SCSI_WRITE10:
ret = SCSI_Write10(pdev, lun, cmd);
break;
case SCSI_WRITE10:
case SCSI_WRITE12:
ret = SCSI_Write12(pdev, lun, cmd);
break;
case SCSI_WRITE12:
case SCSI_VERIFY10:
ret = SCSI_Verify10(pdev, lun, cmd);
break;
case SCSI_VERIFY10:
default:
SCSI_SenseCode(pdev, lun, ILLEGAL_REQUEST, INVALID_CDB);
hmsc->bot_status = USBD_BOT_STATUS_ERROR;
ret = -1;
break;default
}switch (cmd[0]) { ... }
return ret;
}{ ... }
/* ... */
static int8_t SCSI_TestUnitReady(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
UNUSED(params);
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if (hmsc->cbw.dDataLength != 0U)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if (hmsc->cbw.dDataLength != 0U) { ... }
if (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED)
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
hmsc->bot_state = USBD_BOT_NO_DATA;
return -1;
}if (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED) { ... }
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsReady(lun) != 0)
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
hmsc->bot_state = USBD_BOT_NO_DATA;
return -1;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsReady(lun) != 0) { ... }
hmsc->bot_data_length = 0U;
return 0;
}{ ... }
/* ... */
static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
uint8_t *pPage;
uint16_t len;
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if (hmsc->cbw.dDataLength == 0U)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if (hmsc->cbw.dDataLength == 0U) { ... }
if ((params[1] & 0x01U) != 0U)
{
if (params[2] == 0U)
{
(void)SCSI_UpdateBotData(hmsc, MSC_Page00_Inquiry_Data, LENGTH_INQUIRY_PAGE00);
...}
else if (params[2] == 0x80U)
{
(void)SCSI_UpdateBotData(hmsc, MSC_Page80_Inquiry_Data, LENGTH_INQUIRY_PAGE80);
...}
else
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST,
INVALID_FIELED_IN_COMMAND);
return -1;
}else { ... }
...}
else
{
pPage = (uint8_t *) & ((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId]) \
->pInquiry[lun * STANDARD_INQUIRY_DATA_LEN];
len = (uint16_t)pPage[4] + 5U;
if (params[4] <= len)
{
len = params[4];
}if (params[4] <= len) { ... }
(void)SCSI_UpdateBotData(hmsc, pPage, len);
}else { ... }
return 0;
}{ ... }
/* ... */
static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
UNUSED(params);
int8_t ret;
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
ret = ((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->GetCapacity(lun, &hmsc->scsi_blk_nbr,
&hmsc->scsi_blk_size);
if ((ret != 0) || (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED))
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
return -1;
}if ((ret != 0) || (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED)) { ... }
hmsc->bot_data[0] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 24);
hmsc->bot_data[1] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 16);
hmsc->bot_data[2] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 8);
hmsc->bot_data[3] = (uint8_t)(hmsc->scsi_blk_nbr - 1U);
hmsc->bot_data[4] = (uint8_t)(hmsc->scsi_blk_size >> 24);
hmsc->bot_data[5] = (uint8_t)(hmsc->scsi_blk_size >> 16);
hmsc->bot_data[6] = (uint8_t)(hmsc->scsi_blk_size >> 8);
hmsc->bot_data[7] = (uint8_t)(hmsc->scsi_blk_size);
hmsc->bot_data_length = 8U;
return 0;
}{ ... }
/* ... */
static int8_t SCSI_ReadCapacity16(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
UNUSED(params);
uint32_t idx;
int8_t ret;
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
ret = ((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->GetCapacity(lun, &hmsc->scsi_blk_nbr,
&hmsc->scsi_blk_size);
if ((ret != 0) || (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED))
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
return -1;
}if ((ret != 0) || (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED)) { ... }
hmsc->bot_data_length = ((uint32_t)params[10] << 24) |
((uint32_t)params[11] << 16) |
((uint32_t)params[12] << 8) |
(uint32_t)params[13];
for (idx = 0U; idx < hmsc->bot_data_length; idx++)
{
hmsc->bot_data[idx] = 0U;
}for (idx = 0U; idx < hmsc->bot_data_length; idx++) { ... }
hmsc->bot_data[4] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 24);
hmsc->bot_data[5] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 16);
hmsc->bot_data[6] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 8);
hmsc->bot_data[7] = (uint8_t)(hmsc->scsi_blk_nbr - 1U);
hmsc->bot_data[8] = (uint8_t)(hmsc->scsi_blk_size >> 24);
hmsc->bot_data[9] = (uint8_t)(hmsc->scsi_blk_size >> 16);
hmsc->bot_data[10] = (uint8_t)(hmsc->scsi_blk_size >> 8);
hmsc->bot_data[11] = (uint8_t)(hmsc->scsi_blk_size);
hmsc->bot_data_length = ((uint32_t)params[10] << 24) |
((uint32_t)params[11] << 16) |
((uint32_t)params[12] << 8) |
(uint32_t)params[13];
return 0;
}{ ... }
/* ... */
static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
UNUSED(params);
uint16_t blk_size;
uint32_t blk_nbr;
uint16_t i;
int8_t ret;
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
ret = ((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->GetCapacity(lun, &blk_nbr, &blk_size);
if ((ret != 0) || (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED))
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
return -1;
}if ((ret != 0) || (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED)) { ... }
for (i = 0U; i < 12U ; i++)
{
hmsc->bot_data[i] = 0U;
}for (i = 0U; i < 12U ; i++) { ... }
hmsc->bot_data[3] = 0x08U;
hmsc->bot_data[4] = (uint8_t)((blk_nbr - 1U) >> 24);
hmsc->bot_data[5] = (uint8_t)((blk_nbr - 1U) >> 16);
hmsc->bot_data[6] = (uint8_t)((blk_nbr - 1U) >> 8);
hmsc->bot_data[7] = (uint8_t)(blk_nbr - 1U);
hmsc->bot_data[8] = 0x02U;
hmsc->bot_data[9] = (uint8_t)(blk_size >> 16);
hmsc->bot_data[10] = (uint8_t)(blk_size >> 8);
hmsc->bot_data[11] = (uint8_t)(blk_size);
hmsc->bot_data_length = 12U;
return 0;
}{ ... }
/* ... */
static int8_t SCSI_ModeSense6(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
UNUSED(lun);
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
uint16_t len = MODE_SENSE6_LEN;
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsWriteProtected(lun) != 0)
{
MSC_Mode_Sense6_data[2] |= 0x80U;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsWriteProtected(lun) != 0) { ... }
if (params[4] <= len)
{
len = params[4];
}if (params[4] <= len) { ... }
(void)SCSI_UpdateBotData(hmsc, MSC_Mode_Sense6_data, len);
return 0;
}{ ... }
/* ... */
static int8_t SCSI_ModeSense10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
UNUSED(lun);
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
uint16_t len = MODE_SENSE10_LEN;
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsWriteProtected(lun) != 0)
{
MSC_Mode_Sense10_data[3] |= 0x80U;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsWriteProtected(lun) != 0) { ... }
if (params[8] <= len)
{
len = params[8];
}if (params[8] <= len) { ... }
(void)SCSI_UpdateBotData(hmsc, MSC_Mode_Sense10_data, len);
return 0;
}{ ... }
/* ... */
static int8_t SCSI_RequestSense(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
UNUSED(lun);
uint8_t i;
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if (hmsc->cbw.dDataLength == 0U)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if (hmsc->cbw.dDataLength == 0U) { ... }
for (i = 0U; i < REQUEST_SENSE_DATA_LEN; i++)
{
hmsc->bot_data[i] = 0U;
}for (i = 0U; i < REQUEST_SENSE_DATA_LEN; i++) { ... }
hmsc->bot_data[0] = 0x70U;
hmsc->bot_data[7] = REQUEST_SENSE_DATA_LEN - 6U;
if ((hmsc->scsi_sense_head != hmsc->scsi_sense_tail))
{
hmsc->bot_data[2] = (uint8_t)hmsc->scsi_sense[hmsc->scsi_sense_head].Skey;
hmsc->bot_data[12] = (uint8_t)hmsc->scsi_sense[hmsc->scsi_sense_head].w.b.ASC;
hmsc->bot_data[13] = (uint8_t)hmsc->scsi_sense[hmsc->scsi_sense_head].w.b.ASCQ;
hmsc->scsi_sense_head++;
if (hmsc->scsi_sense_head == SENSE_LIST_DEEPTH)
{
hmsc->scsi_sense_head = 0U;
}if (hmsc->scsi_sense_head == SENSE_LIST_DEEPTH) { ... }
}if ((hmsc->scsi_sense_head != hmsc->scsi_sense_tail)) { ... }
hmsc->bot_data_length = REQUEST_SENSE_DATA_LEN;
if (params[4] <= REQUEST_SENSE_DATA_LEN)
{
hmsc->bot_data_length = params[4];
}if (params[4] <= REQUEST_SENSE_DATA_LEN) { ... }
return 0;
}{ ... }
/* ... */
void SCSI_SenseCode(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t sKey, uint8_t ASC)
{
UNUSED(lun);
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return;
}if (hmsc == NULL) { ... }
hmsc->scsi_sense[hmsc->scsi_sense_tail].Skey = sKey;
hmsc->scsi_sense[hmsc->scsi_sense_tail].w.b.ASC = ASC;
hmsc->scsi_sense[hmsc->scsi_sense_tail].w.b.ASCQ = 0U;
hmsc->scsi_sense_tail++;
if (hmsc->scsi_sense_tail == SENSE_LIST_DEEPTH)
{
hmsc->scsi_sense_tail = 0U;
}if (hmsc->scsi_sense_tail == SENSE_LIST_DEEPTH) { ... }
}{ ... }
/* ... */
static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
UNUSED(lun);
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if ((hmsc->scsi_medium_state == SCSI_MEDIUM_LOCKED) && ((params[4] & 0x3U) == 2U))
{
SCSI_SenseCode(pdev, lun, ILLEGAL_REQUEST, INVALID_FIELED_IN_COMMAND);
return -1;
}if ((hmsc->scsi_medium_state == SCSI_MEDIUM_LOCKED) && ((params[4] & 0x3U) == 2U)) { ... }
if ((params[4] & 0x3U) == 0x1U)
{
hmsc->scsi_medium_state = SCSI_MEDIUM_UNLOCKED;
...}
else if ((params[4] & 0x3U) == 0x2U)
{
hmsc->scsi_medium_state = SCSI_MEDIUM_EJECTED;
...}
else if ((params[4] & 0x3U) == 0x3U)
{
hmsc->scsi_medium_state = SCSI_MEDIUM_UNLOCKED;
...}
else
{
}else { ... }
hmsc->bot_data_length = 0U;
return 0;
}{ ... }
/* ... */
static int8_t SCSI_AllowPreventRemovable(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
UNUSED(lun);
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if (params[4] == 0U)
{
hmsc->scsi_medium_state = SCSI_MEDIUM_UNLOCKED;
}if (params[4] == 0U) { ... }
else
{
hmsc->scsi_medium_state = SCSI_MEDIUM_LOCKED;
}else { ... }
hmsc->bot_data_length = 0U;
return 0;
}{ ... }
/* ... */
static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if (hmsc->bot_state == USBD_BOT_IDLE)
{
if ((hmsc->cbw.bmFlags & 0x80U) != 0x80U)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if ((hmsc->cbw.bmFlags & 0x80U) != 0x80U) { ... }
if (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED)
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
return -1;
}if (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED) { ... }
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsReady(lun) != 0)
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
return -1;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsReady(lun) != 0) { ... }
hmsc->scsi_blk_addr = ((uint32_t)params[2] << 24) |
((uint32_t)params[3] << 16) |
((uint32_t)params[4] << 8) |
(uint32_t)params[5];
hmsc->scsi_blk_len = ((uint32_t)params[7] << 8) | (uint32_t)params[8];
if (SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr,
hmsc->scsi_blk_len) < 0)
{
return -1;
}if (SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, hmsc->scsi_blk_len) < 0) { ... }
if (hmsc->cbw.dDataLength != (hmsc->scsi_blk_len * hmsc->scsi_blk_size))
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if (hmsc->cbw.dDataLength != (hmsc->scsi_blk_len * hmsc->scsi_blk_size)) { ... }
hmsc->bot_state = USBD_BOT_DATA_IN;
...}
hmsc->bot_data_length = MSC_MEDIA_PACKET;
return SCSI_ProcessRead(pdev, lun);
}{ ... }
/* ... */
static int8_t SCSI_Read12(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if (hmsc->bot_state == USBD_BOT_IDLE)
{
if ((hmsc->cbw.bmFlags & 0x80U) != 0x80U)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if ((hmsc->cbw.bmFlags & 0x80U) != 0x80U) { ... }
if (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED)
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
return -1;
}if (hmsc->scsi_medium_state == SCSI_MEDIUM_EJECTED) { ... }
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsReady(lun) != 0)
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
return -1;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsReady(lun) != 0) { ... }
hmsc->scsi_blk_addr = ((uint32_t)params[2] << 24) |
((uint32_t)params[3] << 16) |
((uint32_t)params[4] << 8) |
(uint32_t)params[5];
hmsc->scsi_blk_len = ((uint32_t)params[6] << 24) |
((uint32_t)params[7] << 16) |
((uint32_t)params[8] << 8) |
(uint32_t)params[9];
if (SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr,
hmsc->scsi_blk_len) < 0)
{
return -1;
}if (SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, hmsc->scsi_blk_len) < 0) { ... }
if (hmsc->cbw.dDataLength != (hmsc->scsi_blk_len * hmsc->scsi_blk_size))
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if (hmsc->cbw.dDataLength != (hmsc->scsi_blk_len * hmsc->scsi_blk_size)) { ... }
hmsc->bot_state = USBD_BOT_DATA_IN;
...}
hmsc->bot_data_length = MSC_MEDIA_PACKET;
return SCSI_ProcessRead(pdev, lun);
}{ ... }
/* ... */
static int8_t SCSI_Write10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
uint32_t len;
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
#ifdef USE_USBD_COMPOSITE
MSCOutEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_OUT, USBD_EP_TYPE_BULK, (uint8_t)pdev->classId);/* ... */
#endif
if (hmsc->bot_state == USBD_BOT_IDLE)
{
if (hmsc->cbw.dDataLength == 0U)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if (hmsc->cbw.dDataLength == 0U) { ... }
if ((hmsc->cbw.bmFlags & 0x80U) == 0x80U)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if ((hmsc->cbw.bmFlags & 0x80U) == 0x80U) { ... }
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsReady(lun) != 0)
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
return -1;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsReady(lun) != 0) { ... }
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsWriteProtected(lun) != 0)
{
SCSI_SenseCode(pdev, lun, NOT_READY, WRITE_PROTECTED);
return -1;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsWriteProtected(lun) != 0) { ... }
hmsc->scsi_blk_addr = ((uint32_t)params[2] << 24) |
((uint32_t)params[3] << 16) |
((uint32_t)params[4] << 8) |
(uint32_t)params[5];
hmsc->scsi_blk_len = ((uint32_t)params[7] << 8) |
(uint32_t)params[8];
if (SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr,
hmsc->scsi_blk_len) < 0)
{
return -1;
}if (SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, hmsc->scsi_blk_len) < 0) { ... }
len = hmsc->scsi_blk_len * hmsc->scsi_blk_size;
if (hmsc->cbw.dDataLength != len)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if (hmsc->cbw.dDataLength != len) { ... }
len = MIN(len, MSC_MEDIA_PACKET);
hmsc->bot_state = USBD_BOT_DATA_OUT;
(void)USBD_LL_PrepareReceive(pdev, MSCOutEpAdd, hmsc->bot_data, len);
...}
else
{
return SCSI_ProcessWrite(pdev, lun);
}else { ... }
return 0;
}{ ... }
/* ... */
static int8_t SCSI_Write12(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
uint32_t len;
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
#ifdef USE_USBD_COMPOSITE
MSCOutEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_OUT, USBD_EP_TYPE_BULK, (uint8_t)pdev->classId);/* ... */
#endif
if (hmsc->bot_state == USBD_BOT_IDLE)
{
if (hmsc->cbw.dDataLength == 0U)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if (hmsc->cbw.dDataLength == 0U) { ... }
if ((hmsc->cbw.bmFlags & 0x80U) == 0x80U)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if ((hmsc->cbw.bmFlags & 0x80U) == 0x80U) { ... }
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsReady(lun) != 0)
{
SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT);
hmsc->bot_state = USBD_BOT_NO_DATA;
return -1;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsReady(lun) != 0) { ... }
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsWriteProtected(lun) != 0)
{
SCSI_SenseCode(pdev, lun, NOT_READY, WRITE_PROTECTED);
hmsc->bot_state = USBD_BOT_NO_DATA;
return -1;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->IsWriteProtected(lun) != 0) { ... }
hmsc->scsi_blk_addr = ((uint32_t)params[2] << 24) |
((uint32_t)params[3] << 16) |
((uint32_t)params[4] << 8) |
(uint32_t)params[5];
hmsc->scsi_blk_len = ((uint32_t)params[6] << 24) |
((uint32_t)params[7] << 16) |
((uint32_t)params[8] << 8) |
(uint32_t)params[9];
if (SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr,
hmsc->scsi_blk_len) < 0)
{
return -1;
}if (SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, hmsc->scsi_blk_len) < 0) { ... }
len = hmsc->scsi_blk_len * hmsc->scsi_blk_size;
if (hmsc->cbw.dDataLength != len)
{
SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB);
return -1;
}if (hmsc->cbw.dDataLength != len) { ... }
len = MIN(len, MSC_MEDIA_PACKET);
hmsc->bot_state = USBD_BOT_DATA_OUT;
(void)USBD_LL_PrepareReceive(pdev, MSCOutEpAdd, hmsc->bot_data, len);
...}
else
{
return SCSI_ProcessWrite(pdev, lun);
}else { ... }
return 0;
}{ ... }
/* ... */
static int8_t SCSI_Verify10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params)
{
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if ((params[1] & 0x02U) == 0x02U)
{
SCSI_SenseCode(pdev, lun, ILLEGAL_REQUEST, INVALID_FIELED_IN_COMMAND);
return -1;
}if ((params[1] & 0x02U) == 0x02U) { ... }
if (SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, hmsc->scsi_blk_len) < 0)
{
return -1;
}if (SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, hmsc->scsi_blk_len) < 0) { ... }
hmsc->bot_data_length = 0U;
return 0;
}{ ... }
/* ... */
static int8_t SCSI_CheckAddressRange(USBD_HandleTypeDef *pdev, uint8_t lun,
uint32_t blk_offset, uint32_t blk_nbr)
{
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr)
{
SCSI_SenseCode(pdev, lun, ILLEGAL_REQUEST, ADDRESS_OUT_OF_RANGE);
return -1;
}if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr) { ... }
return 0;
}{ ... }
/* ... */
static int8_t SCSI_ProcessRead(USBD_HandleTypeDef *pdev, uint8_t lun)
{
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
uint32_t len;
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
len = hmsc->scsi_blk_len * hmsc->scsi_blk_size;
#ifdef USE_USBD_COMPOSITE
MSCInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_BULK, (uint8_t)pdev->classId);/* ... */
#endif
len = MIN(len, MSC_MEDIA_PACKET);
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->Read(lun, hmsc->bot_data,
hmsc->scsi_blk_addr,
(len / hmsc->scsi_blk_size)) < 0)
{
SCSI_SenseCode(pdev, lun, HARDWARE_ERROR, UNRECOVERED_READ_ERROR);
return -1;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->Read(lun, hmsc->bot_data, hmsc->scsi_blk_addr, (len / hmsc->scsi_blk_size)) < 0) { ... }
(void)USBD_LL_Transmit(pdev, MSCInEpAdd, hmsc->bot_data, len);
hmsc->scsi_blk_addr += (len / hmsc->scsi_blk_size);
hmsc->scsi_blk_len -= (len / hmsc->scsi_blk_size);
hmsc->csw.dDataResidue -= len;
if (hmsc->scsi_blk_len == 0U)
{
hmsc->bot_state = USBD_BOT_LAST_DATA_IN;
}if (hmsc->scsi_blk_len == 0U) { ... }
return 0;
}{ ... }
/* ... */
static int8_t SCSI_ProcessWrite(USBD_HandleTypeDef *pdev, uint8_t lun)
{
USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
uint32_t len;
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
len = hmsc->scsi_blk_len * hmsc->scsi_blk_size;
#ifdef USE_USBD_COMPOSITE
MSCOutEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_OUT, USBD_EP_TYPE_BULK, (uint8_t)pdev->classId);/* ... */
#endif
len = MIN(len, MSC_MEDIA_PACKET);
if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->Write(lun, hmsc->bot_data,
hmsc->scsi_blk_addr,
(len / hmsc->scsi_blk_size)) < 0)
{
SCSI_SenseCode(pdev, lun, HARDWARE_ERROR, WRITE_FAULT);
return -1;
}if (((USBD_StorageTypeDef *)pdev->pUserData[pdev->classId])->Write(lun, hmsc->bot_data, hmsc->scsi_blk_addr, (len / hmsc->scsi_blk_size)) < 0) { ... }
hmsc->scsi_blk_addr += (len / hmsc->scsi_blk_size);
hmsc->scsi_blk_len -= (len / hmsc->scsi_blk_size);
hmsc->csw.dDataResidue -= len;
if (hmsc->scsi_blk_len == 0U)
{
MSC_BOT_SendCSW(pdev, USBD_CSW_CMD_PASSED);
}if (hmsc->scsi_blk_len == 0U) { ... }
else
{
len = MIN((hmsc->scsi_blk_len * hmsc->scsi_blk_size), MSC_MEDIA_PACKET);
(void)USBD_LL_PrepareReceive(pdev, MSCOutEpAdd, hmsc->bot_data, len);
}else { ... }
return 0;
}{ ... }
/* ... */
static int8_t SCSI_UpdateBotData(USBD_MSC_BOT_HandleTypeDef *hmsc,
uint8_t *pBuff, uint16_t length)
{
uint16_t len = length;
if (hmsc == NULL)
{
return -1;
}if (hmsc == NULL) { ... }
hmsc->bot_data_length = len;
while (len != 0U)
{
len--;
hmsc->bot_data[len] = pBuff[len];
}while (len != 0U) { ... }
return 0;
}{ ... }
/* ... */
/* ... */
/* ... */
Includes