Skip to content

Commit c6fb4a3

Browse files
Merge pull request #116 from phdussud/main
Added handling of host lock and release
2 parents 9ee581a + 5857d9d commit c6fb4a3

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

msc_disk.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ static volatile bool cmd_ack = false;
6262

6363
static volatile bool writable = true;
6464
static volatile bool host_ejected = false;
65+
static volatile bool host_lock = false;
66+
static volatile bool no_more_host_lock = false;
6567

6668
enum
6769
{
@@ -318,12 +320,18 @@ bool tud_msc_prevent_allow_medium_removal_cb(uint8_t lun, uint8_t prohibit_remov
318320
{
319321
(void)lun;
320322
if (prohibit_removal != 0) {
321-
//This prevents the host OS (Windows) from caching state about the file system.
322-
//However it isn't verified. In any case, this isn't a promise we can keep.
323-
//the BP5 could be removed anytime.
324-
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x24, 0x0);
325-
return false;
323+
if (writable && !no_more_host_lock) {
324+
host_lock = true;
325+
tud_msc_set_sense(lun, 0, 0, 0);
326+
return true;
327+
} else {
328+
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x24, 0x0);
329+
return false;
330+
}
326331
} else {
332+
host_lock = false;
333+
//sync the medium
334+
disk_ioctl(0, CTRL_SYNC, 0);
327335
tud_msc_set_sense(lun, 0, 0, 0);
328336
return true;
329337
}
@@ -403,7 +411,12 @@ bool insert_or_eject_usbmsdrive(bool insert)
403411

404412
void eject_usbmsdrive(void)
405413
{
414+
no_more_host_lock = true;
415+
while (host_lock) {
416+
sleep_ms(1);
417+
}
406418
insert_or_eject_usbmsdrive(false);
419+
no_more_host_lock = false;
407420
}
408421
void insert_usbmsdrive(void)
409422
{

0 commit comments

Comments
 (0)