@@ -62,6 +62,8 @@ static volatile bool cmd_ack = false;
62
62
63
63
static volatile bool writable = true;
64
64
static volatile bool host_ejected = false;
65
+ static volatile bool host_lock = false;
66
+ static volatile bool no_more_host_lock = false;
65
67
66
68
enum
67
69
{
@@ -318,12 +320,18 @@ bool tud_msc_prevent_allow_medium_removal_cb(uint8_t lun, uint8_t prohibit_remov
318
320
{
319
321
(void )lun ;
320
322
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
+ }
326
331
} else {
332
+ host_lock = false;
333
+ //sync the medium
334
+ disk_ioctl (0 , CTRL_SYNC , 0 );
327
335
tud_msc_set_sense (lun , 0 , 0 , 0 );
328
336
return true;
329
337
}
@@ -403,7 +411,12 @@ bool insert_or_eject_usbmsdrive(bool insert)
403
411
404
412
void eject_usbmsdrive (void )
405
413
{
414
+ no_more_host_lock = true;
415
+ while (host_lock ) {
416
+ sleep_ms (1 );
417
+ }
406
418
insert_or_eject_usbmsdrive (false);
419
+ no_more_host_lock = false;
407
420
}
408
421
void insert_usbmsdrive (void )
409
422
{
0 commit comments