|
@@ -26,6 +26,7 @@ extern "C" {
|
26
|
26
|
#include "usbhw.h"
|
27
|
27
|
#include "usbcore.h"
|
28
|
28
|
#include "mscuser.h"
|
|
29
|
+#include "lpc17xx_wdt.h"
|
29
|
30
|
|
30
|
31
|
#include "../chanfs/diskio.h"
|
31
|
32
|
#include <debug_frmwrk.h>
|
|
@@ -44,13 +45,71 @@ uint8_t BulkStage; /* Bulk Stage */
|
44
|
45
|
uint8_t BulkBuf[MSC_MAX_PACKET]; /* Bulk In/Out Buffer */
|
45
|
46
|
uint8_t block_cache[MSC_BLOCK_SIZE];
|
46
|
47
|
uint8_t BulkLen; /* Bulk In/Out Length */
|
|
48
|
+Sense sense_data;
|
47
|
49
|
|
48
|
50
|
MSC_CBW CBW; /* Command Block Wrapper */
|
49
|
51
|
MSC_CSW CSW; /* Command Status Wrapper */
|
50
|
|
-uint8_t media_lock = 0;
|
|
52
|
+volatile uint8_t media_lock = 0;
|
|
53
|
+volatile bool device_wants_lock = false;
|
|
54
|
+
|
|
55
|
+#define NO_LOCK 0
|
|
56
|
+#define HOST_LOCK 1
|
|
57
|
+#define DEVICE_LOCK 2
|
|
58
|
+
|
|
59
|
+extern uint32_t millis();
|
|
60
|
+extern void _delay_ms(int delay);
|
|
61
|
+
|
|
62
|
+uint32_t MSC_Aquire_Lock() {
|
|
63
|
+ NVIC_DisableIRQ(USB_IRQn);
|
|
64
|
+ device_wants_lock = true;
|
|
65
|
+ uint32_t end_millis = millis() + 1000;
|
|
66
|
+ if(media_lock == HOST_LOCK) {
|
|
67
|
+ NVIC_EnableIRQ(USB_IRQn);
|
|
68
|
+ while(media_lock == HOST_LOCK) {
|
|
69
|
+ if(((long)(end_millis - (millis())) < 0)) {
|
|
70
|
+ _DBG("No signal from Host, Assume success\n");
|
|
71
|
+ break;
|
|
72
|
+ }
|
|
73
|
+ WDT_Feed();
|
|
74
|
+ }
|
|
75
|
+ }
|
|
76
|
+ NVIC_DisableIRQ(USB_IRQn);
|
|
77
|
+ media_lock = DEVICE_LOCK;
|
|
78
|
+ NVIC_EnableIRQ(USB_IRQn);
|
|
79
|
+ _DBG("Device MSC Lock\n");
|
|
80
|
+ device_wants_lock = false;
|
|
81
|
+ return 0;
|
|
82
|
+}
|
|
83
|
+
|
|
84
|
+uint32_t MSC_Release_Lock() {
|
|
85
|
+ if(media_lock != DEVICE_LOCK) {
|
|
86
|
+ return 0; // Didn't have lock
|
|
87
|
+ }
|
|
88
|
+ media_lock = NO_LOCK;
|
|
89
|
+ if(disk_status(0) != STA_NOINIT) disk_ioctl(0, GET_SECTOR_COUNT, (void *)(&MSC_BlockCount));
|
|
90
|
+ _DBG("Device MSC Unlock\n");
|
|
91
|
+ NVIC_DisableIRQ(USB_IRQn);
|
|
92
|
+ sense_data.set(Sense_KEY::UNIT_ATTENTION, Sense_ASC::MEDIA_CHANGED);
|
|
93
|
+ NVIC_EnableIRQ(USB_IRQn);
|
|
94
|
+ return 0; // Released
|
|
95
|
+}
|
51
|
96
|
|
52
|
97
|
uint32_t MSC_SD_Lock() {
|
53
|
|
- media_lock = CBW.CB[4]; //0x1 - lock, 0x0 - unlock
|
|
98
|
+ if(media_lock == DEVICE_LOCK || (device_wants_lock && CBW.CB[4])) {
|
|
99
|
+ CSW.bStatus = CSW_CMD_FAILED;
|
|
100
|
+ sense_data.set(Sense_KEY::NOT_READY, Sense_ASC::LOGICAL_UNIT_NOT_READY, Sense_ASCQ::DEVICE_IS_BUSY);
|
|
101
|
+ MSC_SetCSW();
|
|
102
|
+ _DBG("Device has Lock (or is waiting for lock) cannot Lock..\n");
|
|
103
|
+ return 1;
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ if(CBW.CB[4]) {
|
|
107
|
+ media_lock = HOST_LOCK;
|
|
108
|
+ _DBG("OS MSC Lock\n");
|
|
109
|
+ } else {
|
|
110
|
+ media_lock = NO_LOCK;
|
|
111
|
+ _DBG("OS MSC Unlock\n");
|
|
112
|
+ }
|
54
|
113
|
// logical_unit = CBW.CB[1] & 0xE0;
|
55
|
114
|
CSW.bStatus = CSW_CMD_PASSED;
|
56
|
115
|
MSC_SetCSW();
|
|
@@ -78,20 +137,28 @@ void MSC_StartStopUnit() {
|
78
|
137
|
switch (CBW.CB[4] & 0x03) {
|
79
|
138
|
case STARTSTOP_EJECT:
|
80
|
139
|
MSC_SD_Release(0);
|
|
140
|
+ media_lock = NO_LOCK;
|
|
141
|
+ _DBG("OS Media Ejected UNLOCK\n");
|
81
|
142
|
break;
|
82
|
143
|
case STARTSTOP_LOAD:
|
83
|
144
|
if(MSC_BlockCount == 0) {
|
84
|
145
|
if(MSC_SD_Init(0) != 0) {
|
85
|
146
|
CSW.bStatus = CSW_CMD_FAILED;
|
|
147
|
+ sense_data.set(Sense_KEY::NOT_READY, Sense_ASC::MEDIUM_NOT_PRESENT, Sense_ASCQ::MANUAL_INTERVENTION_REQUIRED);
|
86
|
148
|
MSC_SetCSW();
|
87
|
149
|
return;
|
88
|
150
|
}
|
89
|
151
|
}
|
|
152
|
+ media_lock = HOST_LOCK;
|
|
153
|
+ _DBG("OS Media Mount LOCKED\n");
|
90
|
154
|
break;
|
91
|
155
|
default:
|
92
|
|
- _DBG("MSC_StartStopUnit unknown startstopunit sub command\n");
|
|
156
|
+ _DBG("MSC_StartStopUnit unknown startstopunit sub command: ");
|
|
157
|
+ _DBH(CBW.CB[4] & 0x03);
|
|
158
|
+ _DBG("\n");
|
93
|
159
|
}
|
94
|
160
|
CSW.bStatus = CSW_CMD_PASSED;
|
|
161
|
+ sense_data.reset();
|
95
|
162
|
MSC_SetCSW();
|
96
|
163
|
}
|
97
|
164
|
|
|
@@ -122,6 +189,18 @@ uint32_t MSC_GetMaxLUN (void) {
|
122
|
189
|
}
|
123
|
190
|
|
124
|
191
|
|
|
192
|
+bool host_get_lock(void) {
|
|
193
|
+ if(media_lock != DEVICE_LOCK && !device_wants_lock) {
|
|
194
|
+ media_lock = HOST_LOCK;
|
|
195
|
+ return true;
|
|
196
|
+ } else {
|
|
197
|
+ CSW.bStatus = CSW_CMD_FAILED;
|
|
198
|
+ sense_data.set(Sense_KEY::NOT_READY, Sense_ASC::LOGICAL_UNIT_NOT_READY, Sense_ASCQ::DEVICE_IS_BUSY);
|
|
199
|
+ MSC_SetCSW();
|
|
200
|
+ return false;
|
|
201
|
+ }
|
|
202
|
+}
|
|
203
|
+
|
125
|
204
|
/*
|
126
|
205
|
* MSC Memory Read Callback
|
127
|
206
|
* Called automatically on Memory Read Event
|
|
@@ -130,6 +209,12 @@ uint32_t MSC_GetMaxLUN (void) {
|
130
|
209
|
*/
|
131
|
210
|
|
132
|
211
|
void MSC_MemoryRead (void) {
|
|
212
|
+ if(!host_get_lock()) {
|
|
213
|
+ _DBG("Auto Lock Fail Permission Denied Device has Lock\n");
|
|
214
|
+ return;
|
|
215
|
+ }
|
|
216
|
+ WDT_Feed();
|
|
217
|
+
|
133
|
218
|
uint32_t n = (length > MSC_MAX_PACKET) ? MSC_MAX_PACKET : length;
|
134
|
219
|
|
135
|
220
|
if (lba > MSC_BlockCount) {
|
|
@@ -158,6 +243,7 @@ void MSC_MemoryRead (void) {
|
158
|
243
|
|
159
|
244
|
if (BulkStage != MSC_BS_DATA_IN) {
|
160
|
245
|
CSW.bStatus = CSW_CMD_PASSED;
|
|
246
|
+ sense_data.reset();
|
161
|
247
|
}
|
162
|
248
|
}
|
163
|
249
|
|
|
@@ -170,6 +256,11 @@ void MSC_MemoryRead (void) {
|
170
|
256
|
*/
|
171
|
257
|
|
172
|
258
|
void MSC_MemoryWrite (void) {
|
|
259
|
+ if(!host_get_lock()) {
|
|
260
|
+ _DBG("Auto Lock Fail Permission Denied Device has Lock\n");
|
|
261
|
+ return;
|
|
262
|
+ }
|
|
263
|
+ WDT_Feed();
|
173
|
264
|
|
174
|
265
|
for (uint32_t n = 0; n < BulkLen; n++) {
|
175
|
266
|
block_cache[block_offset + n] = BulkBuf[n];
|
|
@@ -192,6 +283,7 @@ void MSC_MemoryWrite (void) {
|
192
|
283
|
|
193
|
284
|
if ((length == 0) || (BulkStage == MSC_BS_CSW)) {
|
194
|
285
|
CSW.bStatus = CSW_CMD_PASSED;
|
|
286
|
+ sense_data.reset();
|
195
|
287
|
MSC_SetCSW();
|
196
|
288
|
}
|
197
|
289
|
}
|
|
@@ -205,6 +297,11 @@ void MSC_MemoryWrite (void) {
|
205
|
297
|
*/
|
206
|
298
|
|
207
|
299
|
void MSC_MemoryVerify (void) {
|
|
300
|
+ if(!host_get_lock()) {
|
|
301
|
+ _DBG("Auto Lock Fail Permission Denied Device has Lock\n");
|
|
302
|
+ return;
|
|
303
|
+ }
|
|
304
|
+ WDT_Feed();
|
208
|
305
|
|
209
|
306
|
if(!block_offset) {
|
210
|
307
|
disk_read(0, block_cache, lba, 1);
|
|
@@ -222,7 +319,13 @@ void MSC_MemoryVerify (void) {
|
222
|
319
|
CSW.dDataResidue -= BulkLen;
|
223
|
320
|
|
224
|
321
|
if ((length == 0) || (BulkStage == MSC_BS_CSW)) {
|
225
|
|
- CSW.bStatus = (MemOK) ? CSW_CMD_PASSED : CSW_CMD_FAILED;
|
|
322
|
+ if(MemOK) {
|
|
323
|
+ CSW.bStatus = CSW_CMD_PASSED;
|
|
324
|
+ sense_data.reset();
|
|
325
|
+ } else {
|
|
326
|
+ CSW.bStatus = CSW_CMD_FAILED;
|
|
327
|
+ sense_data.set(Sense_KEY::MEDIUM_ERROR);
|
|
328
|
+ }
|
226
|
329
|
MSC_SetCSW();
|
227
|
330
|
}
|
228
|
331
|
}
|
|
@@ -321,10 +424,15 @@ void MSC_TestUnitReady (void) {
|
321
|
424
|
}
|
322
|
425
|
}
|
323
|
426
|
|
324
|
|
- if(MSC_BlockCount > 0) {
|
|
427
|
+ if(device_wants_lock) {
|
|
428
|
+ sense_data.set(Sense_KEY::NOT_READY, Sense_ASC::MEDIUM_NOT_PRESENT, Sense_ASCQ::REASON_UNKNOWN);
|
|
429
|
+ CSW.bStatus = CSW_CMD_FAILED;
|
|
430
|
+ } else if(MSC_BlockCount > 0) {
|
|
431
|
+ sense_data.reset();
|
325
|
432
|
CSW.bStatus = CSW_CMD_PASSED;
|
326
|
433
|
} else {
|
327
|
434
|
CSW.bStatus = CSW_CMD_FAILED;
|
|
435
|
+ sense_data.set(Sense_KEY::NOT_READY, Sense_ASC::MEDIUM_NOT_PRESENT, Sense_ASCQ::LOADABLE);
|
328
|
436
|
}
|
329
|
437
|
|
330
|
438
|
MSC_SetCSW();
|
|
@@ -341,9 +449,13 @@ void MSC_RequestSense (void) {
|
341
|
449
|
|
342
|
450
|
if (!DataInFormat()) return;
|
343
|
451
|
|
|
452
|
+ if(media_lock == DEVICE_LOCK || device_wants_lock) {
|
|
453
|
+ sense_data.set(Sense_KEY::NOT_READY, Sense_ASC::MEDIUM_NOT_PRESENT, Sense_ASCQ::REASON_UNKNOWN);
|
|
454
|
+ }
|
|
455
|
+
|
344
|
456
|
BulkBuf[ 0] = 0x70; /* Response Code */
|
345
|
457
|
BulkBuf[ 1] = 0x00;
|
346
|
|
- BulkBuf[ 2] = static_cast<uint8_t>(Sense_KEY::ILLEGAL_REQUEST);
|
|
458
|
+ BulkBuf[ 2] = static_cast<uint8_t>(sense_data.key);
|
347
|
459
|
BulkBuf[ 3] = 0x00;
|
348
|
460
|
BulkBuf[ 4] = 0x00;
|
349
|
461
|
BulkBuf[ 5] = 0x00;
|
|
@@ -354,17 +466,17 @@ void MSC_RequestSense (void) {
|
354
|
466
|
BulkBuf[ 9] = 0x00;
|
355
|
467
|
BulkBuf[10] = 0x00;
|
356
|
468
|
BulkBuf[11] = 0x00;
|
357
|
|
- BulkBuf[12] = static_cast<uint8_t>(Sense_ASC::CANNOT_READ_MEDIUM);
|
358
|
|
- BulkBuf[13] = static_cast<uint8_t>(Sense_ASCQ::UNKNOWN_FORMAT);
|
|
469
|
+ BulkBuf[12] = static_cast<uint8_t>(sense_data.asc);
|
|
470
|
+ BulkBuf[13] = static_cast<uint8_t>(sense_data.ascq);
|
359
|
471
|
BulkBuf[14] = 0x00;
|
360
|
472
|
BulkBuf[15] = 0x00;
|
361
|
473
|
BulkBuf[16] = 0x00;
|
362
|
474
|
BulkBuf[17] = 0x00;
|
363
|
475
|
|
364
|
|
- if (MSC_BlockCount == 0) {
|
365
|
|
- BulkBuf[ 2] = static_cast<uint8_t>(Sense_KEY::NOT_READY);
|
366
|
|
- BulkBuf[12] = static_cast<uint8_t>(Sense_ASC::MEDIUM_NOT_PRESENT);
|
367
|
|
- BulkBuf[13] = static_cast<uint8_t>(Sense_ASCQ::LOADABLE);
|
|
476
|
+ if(sense_data.has_sense()){
|
|
477
|
+ _DBG("Sent Response to SenseRequest: ");
|
|
478
|
+ _DBH(static_cast<uint8_t>(sense_data.key));
|
|
479
|
+ _DBG("\n");
|
368
|
480
|
}
|
369
|
481
|
|
370
|
482
|
BulkLen = 18;
|
|
@@ -423,10 +535,6 @@ void MSC_Inquiry (void) {
|
423
|
535
|
BulkBuf[34] = '0';
|
424
|
536
|
BulkBuf[35] = ' ';
|
425
|
537
|
|
426
|
|
- if(MSC_BlockCount == 0) {
|
427
|
|
- BulkBuf[0] = 0x20; // Direct Access Device usually available but not currently
|
428
|
|
- }
|
429
|
|
-
|
430
|
538
|
BulkLen = 36;
|
431
|
539
|
DataInTransfer();
|
432
|
540
|
}
|
|
@@ -553,8 +661,9 @@ void MSC_GetCBW (void) {
|
553
|
661
|
CSW.dDataResidue = CBW.dDataLength;
|
554
|
662
|
if ((CBW.bLUN != 0) || (CBW.bCBLength < 1) || CBW.bCBLength > 16) {
|
555
|
663
|
fail: CSW.bStatus = CSW_CMD_FAILED;
|
|
664
|
+ sense_data.set(Sense_KEY::ILLEGAL_REQUEST);
|
556
|
665
|
MSC_SetCSW();
|
557
|
|
- _DBG("Failed SCSI OP code ");
|
|
666
|
+ _DBG("Unsupported SCSI OP code ");
|
558
|
667
|
_DBH(CBW.CB[0]);
|
559
|
668
|
_DBG("\n");
|
560
|
669
|
} else {
|
|
@@ -627,6 +736,20 @@ fail: CSW.bStatus = CSW_CMD_FAILED;
|
627
|
736
|
}
|
628
|
737
|
}
|
629
|
738
|
break;
|
|
739
|
+ case 0x35: // SCSI_SYNCHRONIZECACHE10
|
|
740
|
+ _DBG("SCSI_SYNCHRONIZECACHE10 Unsupported\n");
|
|
741
|
+ CSW.bStatus = CSW_CMD_FAILED;
|
|
742
|
+ sense_data.set(Sense_KEY::ILLEGAL_REQUEST);
|
|
743
|
+ MSC_SetCSW();
|
|
744
|
+ break;
|
|
745
|
+ case 0x9E: // SCSI_SERVICEACTIONIN16
|
|
746
|
+ _DBG("ServiceAction(16) Action: ");
|
|
747
|
+ _DBH(CBW.CB[1]);
|
|
748
|
+ _DBG(" Unsupported\n");
|
|
749
|
+ CSW.bStatus = CSW_CMD_FAILED;
|
|
750
|
+ sense_data.set(Sense_KEY::ILLEGAL_REQUEST);
|
|
751
|
+ MSC_SetCSW();
|
|
752
|
+ break;
|
630
|
753
|
default:
|
631
|
754
|
goto fail;
|
632
|
755
|
}
|
|
@@ -647,7 +770,6 @@ fail: CSW.bStatus = CSW_CMD_FAILED;
|
647
|
770
|
*/
|
648
|
771
|
|
649
|
772
|
void MSC_SetCSW (void) {
|
650
|
|
-
|
651
|
773
|
CSW.dSignature = MSC_CSW_Signature;
|
652
|
774
|
USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW));
|
653
|
775
|
BulkStage = MSC_BS_CSW;
|
|
@@ -661,7 +783,6 @@ void MSC_SetCSW (void) {
|
661
|
783
|
*/
|
662
|
784
|
|
663
|
785
|
void MSC_BulkIn (void) {
|
664
|
|
-
|
665
|
786
|
switch (BulkStage) {
|
666
|
787
|
case MSC_BS_DATA_IN:
|
667
|
788
|
switch (CBW.CB[0]) {
|
|
@@ -691,7 +812,6 @@ void MSC_BulkIn (void) {
|
691
|
812
|
*/
|
692
|
813
|
|
693
|
814
|
void MSC_BulkOut (void) {
|
694
|
|
-
|
695
|
815
|
BulkLen = (uint8_t)USB_ReadEP(MSC_EP_OUT, BulkBuf);
|
696
|
816
|
switch (BulkStage) {
|
697
|
817
|
case MSC_BS_CBW:
|