|
@@ -1,6 +1,8 @@
|
1
|
1
|
/*
|
2
|
|
- * The MIT License (MIT)
|
|
2
|
+ * Extended from TinyUSB example code.
|
|
3
|
+ * Copyright (c) 2022 Thomas Buck (thomas@xythobuz.de)
|
3
|
4
|
*
|
|
5
|
+ * The MIT License (MIT)
|
4
|
6
|
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
5
|
7
|
*
|
6
|
8
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
@@ -20,7 +22,6 @@
|
20
|
22
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
23
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
24
|
* THE SOFTWARE.
|
23
|
|
- *
|
24
|
25
|
*/
|
25
|
26
|
|
26
|
27
|
#include "bsp/board.h"
|
|
@@ -63,13 +64,12 @@ void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8],
|
63
|
64
|
// Invoked when received Test Unit Ready command.
|
64
|
65
|
// return true allowing host to read/write this LUN e.g SD card inserted
|
65
|
66
|
bool tud_msc_test_unit_ready_cb(uint8_t lun) {
|
66
|
|
- if (ejected || !medium_available) {
|
|
67
|
+ if (!medium_available) {
|
67
|
68
|
// Additional Sense 3A-00 is NOT_FOUND
|
68
|
69
|
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
|
69
|
|
- return false;
|
70
|
70
|
}
|
71
|
71
|
|
72
|
|
- return true;
|
|
72
|
+ return medium_available;
|
73
|
73
|
}
|
74
|
74
|
|
75
|
75
|
// Invoked when received SCSI_CMD_READ_CAPACITY_10 and
|
|
@@ -78,8 +78,13 @@ bool tud_msc_test_unit_ready_cb(uint8_t lun) {
|
78
|
78
|
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) {
|
79
|
79
|
(void) lun;
|
80
|
80
|
|
81
|
|
- *block_count = DISK_BLOCK_COUNT;
|
82
|
|
- *block_size = DISK_BLOCK_SIZE;
|
|
81
|
+ if (!medium_available) {
|
|
82
|
+ *block_count = 0;
|
|
83
|
+ *block_size = 0;
|
|
84
|
+ } else {
|
|
85
|
+ *block_count = DISK_BLOCK_COUNT;
|
|
86
|
+ *block_size = DISK_BLOCK_SIZE;
|
|
87
|
+ }
|
83
|
88
|
}
|
84
|
89
|
|
85
|
90
|
// Invoked when received Start Stop Unit command
|
|
@@ -96,7 +101,9 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition,
|
96
|
101
|
} else {
|
97
|
102
|
// unload disk storage
|
98
|
103
|
debug("unload disk storage %d", load_eject);
|
99
|
|
- ejected = true;
|
|
104
|
+ if (load_eject) {
|
|
105
|
+ medium_available = false;
|
|
106
|
+ }
|
100
|
107
|
}
|
101
|
108
|
|
102
|
109
|
return true;
|
|
@@ -158,6 +165,28 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16],
|
158
|
165
|
bool in_xfer = true;
|
159
|
166
|
|
160
|
167
|
switch (scsi_cmd[0]) {
|
|
168
|
+ case 0x1E:
|
|
169
|
+ // Prevent/Allow Medium Removal
|
|
170
|
+ if (scsi_cmd[4] & 0x01) {
|
|
171
|
+ // Prevent medium removal
|
|
172
|
+ if (!medium_available) {
|
|
173
|
+ debug("Host wants to lock non-existing medium. Not supported.");
|
|
174
|
+ resplen = -1;
|
|
175
|
+ } else {
|
|
176
|
+ debug("Host wants to lock medium.");
|
|
177
|
+ }
|
|
178
|
+ } else {
|
|
179
|
+ // Allow medium removal
|
|
180
|
+ if (medium_available) {
|
|
181
|
+ debug("Host ejected medium. Unplugging disk.");
|
|
182
|
+ medium_available = false;
|
|
183
|
+ } else {
|
|
184
|
+ debug("host ejected non-existing medium. Not supported.");
|
|
185
|
+ resplen = -1;
|
|
186
|
+ }
|
|
187
|
+ }
|
|
188
|
+ break;
|
|
189
|
+
|
161
|
190
|
default:
|
162
|
191
|
// Set Sense = Invalid Command Operation
|
163
|
192
|
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
|