3

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?

nsayer
  • 16,925
  • 3
  • 33
  • 51
  • What are your hosts? I've had good luck with MEDIUM_NOT_PRESENT on both Linux and Windows 7+. If memory serves me, it may have been important to reply to ALL commands consistently with MEDIUM_NOT_PRESENT, not just read and write - like TEST_UNIT_READY should get it too. – Mike Andrews Apr 19 '17 at 21:23
  • I'm testing with Linux (Raspbian) and a mac. I try adding it into TEST_UNIT_READY, but if that works, you might consider converting your comment into an answer. – nsayer Apr 20 '17 at 01:08
  • I've added the same check/response into UNIT_TEST_READY, but that didn't seem to help. – nsayer Apr 20 '17 at 01:17
  • Ah, your last paragraph just finally penetrated into my brain. You're not ejecting the device, like with the `eject` command, getting a START_STOP_UNIT EJECT? In that case, you might want to yank the LUN out, instead of acting like the media was just gone. Could experiment with issuing a UNIT_ATTENTION with REPORTED_LUNS_DATA_HAS_CHANGED, then refusing to report the LUN anymore? – Mike Andrews Apr 20 '17 at 14:45
  • What's the value for REPORTED_LUNS_DATA_HAS_CHANGED? I don't see that. Is it an additional sense code value? – nsayer Apr 20 '17 at 16:01
  • I found values to try for the unit attention, but that didn't seem to help. – nsayer Apr 20 '17 at 17:30
  • I guess what's remaining is: what's the behavior? Do you have nasty-looking messages in /var/log/messages (or journalctl)? – Mike Andrews Apr 20 '17 at 18:51
  • The behavior is... nothing at all. I can yank out a card and the host will act like nothing happened. I/O will just hang. I haven't waited long enough to see how long the timeout is. – nsayer Apr 20 '17 at 22:31

1 Answers1

1

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.

nsayer
  • 16,925
  • 3
  • 33
  • 51