Linux – Detects USB mass storage ejection/unmount in Linux

Detects USB mass storage ejection/unmount in Linux… here is a solution to the problem.

Detects USB mass storage ejection/unmount in Linux

I use g_file_storage gadgets in Linux to export partitions as removable USB mass storage devices to host PCs (Windows, Linux, MAC).

All of these host operating systems (all can) automatically mount the removable disk and access it without problems, I’m looking for a way to detect if the host has ejected/unmounted the disk.

So far, the only methods I’ve found are limited to Windows XP. When the user selects “Safely Remove Hardware”, XP puts the USB device into suspend mode, which can be detected by the remote end. For some reason, this does not work in Vista.

Solution

I’ve had this issue too.

My embedded USB read-only mass storage project monitors SCSI commands from the host. FOR MAC OS X (10.5): ON PLUG-INS, THE OS X DRIVER DOES A VERY GOOD JOB OF TELLING THE DEVICE TO PREVENT MEDIA REMOVAL (SCSI COMMAND PREVENT/ALLOW MEDIUM REMOVAL). My device accepts this command. LATER, WHEN THE USER SIGNALS THAT THE DEVICE SHOULD POP UP, OS X AGAIN NICELY SENDS A PREVENT/ALLOW REMOVAL WITH THE ALLOW FLAG, FOLLOWED BY A SCSI START/STOP INDICATION TO STOP (OR POP UP, DON’T REMEMBER). Beautiful.

HOWEVER, WINDOWS XP SEEMS TO SIMPLY STOP SENDING SCSI COMMANDS TEST UNIT READY. I’ve seen it; SAFE REMOVE HARDWARE DOESN’T SEND ANY SCSI SIGNALS, IT JUST CAUSES THE TEST UNIT READY SIGNAL TO STOP APPEARING, WHICH IT DOES EVERY SECOND OR SO. Basically, Windows just keeps silent.

Test unit readiness timeouts work on Windows, but it’s a bad way to detect device stop conditions. Also, it doesn’t work with OS X because OS X doesn’t ping my device every second like Windows does; I would get false positives.

Updated November 15, 2009

Sondergaard reports here.

Much depends on whether the device’s response to the USB mass storage-level SCSI “query” command is self-recognized as “removable” or “fixed.”

If the flag shows as removable, the only way I can tell if a Windows XP user “stopped” my device is to test the unit ready command to exit every second. If the flag doesn’t say movable, there is no regular test unit ready command flow. Neither can expect USB hangs, and in about 1/4 of my “safe removal” tests, USB hangs were not sent. Not sure why it’s inconsistent. SO IF YOU’RE LOOKING FOR SURE, THE BEST ADVICE I CAN MAKE FOR WINDOWS XP-ONLY COMPATIBILITY IS TO RESPOND INQUIRY AS “REMOVABLE,” LOOK FOR REGULAR TEST UNIT READY COMMANDS, AND THEN WHEN THEY STOP, CONSIDER THAT YOU’VE SAFELY REMOVED.

OS X (10.6) is another story. As I wrote above, it’s more polite. It sends SCSI START/STOP to speed up or stop the device, sometimes completely independent and without user input. But this means that if you interpret STOP as if the user has passed, and disconnect the internal connection of the device in any ugly way, OS X becomes very confused when trying to boot the disk again. If your device is marked as removable, OS X will send an ALLOW REMOVAL command before STOP (or in this case EJECT?). ), it would be a good signal that it has passed.

I didn’t try different configurations exhaustively. My application is read-only, has two partitions, and must be identified as non-removable because Windows XP refuses to acknowledge more than one partition for removable USB drives (OS X has no problems). So most of my testing was done in this configuration.

Hope this helps you.

Related Problems and Solutions