I'm developing a USB mass storage device. I got the device basically working (it's too slow, but that's another conversation). What isn't working as well as I'd like is handling device removal - not from the USB end, but the USB device presenting the correct responses to the host when the USB device is forced offline by (forcible) media removal.
I've tried responding to read/write requests with the NOT_READY sense key with MEDIUM_NOT_PRESENT additional information, but the hosts don't seem to react to that in the same way they do to disconnection of the USB cable.
What is the expected behavior for USB devices with removable media when the media is yanked out during operation?
Thanks go to Dean Camera of LUFA for solving this (I don't know if he has an identity here or not). The solution was to not change the LUN count, but rather to respond to PREVENT_ALLOW_MEDIUM_REMOVAL with an ILLEGAL_REQUEST sense key, and to respond to TEST_UNIT_READY appropriately for the state of the device (either success or NOT_READY/MEDIUM_NOT_PRESENT). I also generate a UNIT_ATTENTION for the first command after any change in status. And now the device operates exactly correctly in the face of removal and insertion of the media.