Skip to content

Commit 060019f

Browse files
committed
shared/usb: per-LUN response to PREVENT_ALLOW_MEDIUM_REMOVAL
Respond with "unsupported" for the SD LUN (removable media) and OK for internal flash / SAVES LUNs (non-removable). Responding OK for a removable LUN tells macOS the medium is always present, and macOS then skips TEST_UNIT_READY polling. If the SD isn't ready at the single enumeration probe, macOS never re-checks and LUN 1 fails to publish an IOMedia node. Hathach documented this behavior back in #6555; the SD case wasn't differentiated at the time. Fixes #10965.
1 parent abb3cbb commit 060019f

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

supervisor/shared/usb/usb_msc_flash.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,22 @@ int32_t tud_msc_scsi_cb(uint8_t lun, const uint8_t scsi_cmd[16], void *buffer, u
214214

215215
switch (scsi_cmd[0]) {
216216
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
217-
// Host is about to read/write etc ... better not to disconnect disk
218-
resplen = 0;
217+
#ifdef SDCARD_LUN
218+
if (lun == SDCARD_LUN) {
219+
// Removable media (SD card). Respond "unsupported" so macOS
220+
// keeps sending TEST_UNIT_READY periodically to detect card
221+
// insertion/removal. Responding OK here causes macOS to skip
222+
// TUR polling and miss media-present events on the SD LUN.
223+
// See the discussion in adafruit/circuitpython#6555.
224+
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
225+
resplen = -1;
226+
} else
227+
#endif
228+
{
229+
// Non-removable media (internal flash, SAVES). OK is fine;
230+
// host assumes medium always present and skips TUR polling.
231+
resplen = 0;
232+
}
219233
break;
220234

221235
default:

0 commit comments

Comments
 (0)