This is not a question about code as much as correct SCSI procedures.
I'm using an open source Android USB library which works fine, but I'm trying to solve some issues with a device I'm having.
When a device is connected the library goes through all its LUNs and calls the following SCSI functions to initialize each one:
INQUIRY
TEST UNIT READY
READ CAPACITY
This works fine on most devices but on my device there is an issue. The device is an SD card reader with two LUNs. One is empty and this results in TEST UNIT READY
reporting an error when called. REQUEST SENSE
is then called which reports it as MEDIUM NOT PRESENT
which is logical and handled by throwing an exception and thereby stopping before READ CAPACITY
is reached.
The second LUN with an SD card in it also reports an error in TEST UNIT READY
; NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED
(Sense code 06h, ASC 28h, ASCQ 00h). I think it essentially means "card inserted". My question is what to do at this point to continue initializing the LUN?
I have tried:
- Calling
TEST UNIT READY
again - results in the exact same error reappearing inTEST UNIT READY
each time - Retrying the entire init procedure starting with a new
INQUIRY
- same result as above - Reading the error and continuing with
READ CAPACITY
- results in the receivingUsbDeviceConnection.bulkTransfer()
forREAD CAPACITY
to return -1.
My conclusion is that either something is wrong with this reader (which seems unlikely) or I'm not handling the further communication with the device correctly after this type of SENSE result is returned. I've been looking through all documentation I can find on google but can't find anything that can help me with this particular issue. I assume some kind of reset or something has to be done? Some way to acknowledge that the medium has changed?
More observations:
- The issue seems to arise only in a certain SD card reader, and only with a certain SD card. Using another SD card in the reader works and using the first SD card in another reader also works.
- When the USB device is initially opened
UsbDeviceConnection.claimInterface(interface, true)
is called. When this takes very long time (1-2 minutes) the issue always arises, but if it has the more normal almost instantaneous delay then the issue doesn't arise.
This leads me to conclude that either this particular SD card and reader doesn't like each other very much, or there is some issue in claimInterface with this particular SD card reader.
The device might be running with
UNIT ATTENTION
interlock. In this mode, it supports autosense, returning the sense data in response to each command, but it doesn't clear the sense code. It keeps returning the same one.If this is what's going on, there are two solutions:
You may be able to change behavior this with a
MODE SELECT
of the control mode page by setting theUA_INTLCK_CTRL
field to 0. This turns off UA interlock, so that the device clears the status code after reporting it.REQUEST SENSE
reads and clears a check condition. You can grind through the status conditions withREQUEST SENSE
commands until everything is clear.