open with O_DIRECT only works when on USB or A/C power

60 views
Skip to first unread message

kllrnohj

unread,
Sep 21, 2009, 6:21:08 PM9/21/09
to android-ndk
I am developing an Android application that communicates with a custom
micro SD chip. To talk properly to the custom chip, communication must
be opened with the flag O_DIRECT. I assure you guys that this *is*
necessary for what I am trying to accomplish. So what I did was create
a JNI library that makes the appropriate calls to open, write, read,
close, etc... so that I could do unbuffered I/O using the O_DIRECT
flag. Basically the flow is that I open a file in O_DIRECT mode, write
to it, wait a little bit, and then read from it (and the custom chip
will have altered the file).

When connected via USB to a computer or to A/C power, it works
perfectly. No problems, everything is great. However, when on battery
power the call to read() returns with -1 and errno is 5 (EIO) - which
means that the calls to open and write were successful. After that
happens the SD card no longer works. Android reports that an SD card
is inserted, but any attempt to read or write from it fails regardless
of the method (in other apps, using standard java, from a shell,
etc...). It even sometimes shows up as having a size of 00 in the
settings. However, removing the card and re-inserting it fixes the
problem, so the file system isn't actually being corrupted.

So far I have tested the code on both an HTC Magic and a T-Mobile G1,
both experience the exact same issue. I should also probably mention
that the custom chip works with other devices/PCs, so I am positive
that it isn't the culprit.

The only thing I can think of is some sort of power setting that
changes, but I don't have the slightest idea on where or how to change
it. Any thoughts or ideas would be most appreciated, thank you.

Tim Hutt

unread,
Sep 22, 2009, 3:35:57 AM9/22/09
to andro...@googlegroups.com
> The only thing I can think of is some sort of power setting that
> changes, but I don't have the slightest idea on where or how to change
> it. Any thoughts or ideas would be most appreciated, thank you.

Maybe you could hook up a scope to the power supply pins on the SD
card. It could just be that the card draws too much power and then
you'll see a voltage drop.

kllrnohj

unread,
Sep 22, 2009, 11:19:01 AM9/22/09
to android-ndk
I think it is a power draw issue, as my code works with a regular SD
card on battery (obviously it isn't getting the responses it should,
but no strange errors either)

Given that it works on A/C and USB power, I'm inclined to think it is
a software change (lowering USB power or something of that sort) and
not a hardware limitation. Unfortunately I really can't just lower the
power usage of the chip I'm using, so any suggestions on how or where
to look to convince the system to give more power to the card reader?
I have used this same chip in other phones without any power related
issues, so it doesn't seem to be using an extreme amount of power or
anything like that, just a bit more than a regular SD card (I believe
the custom chip uses ~6 mA on top of the power draw of the regular
microsd chip.)

fadden

unread,
Sep 22, 2009, 3:37:43 PM9/22/09
to android-ndk
That's really peculiar.

What state is the device in -- is the Android framework running? Is
the behavior different when the device is asleep vs. active (e.g.
screen blank vs. scrolling around on the home screen)?

Can you file a bug on b.android.com with the details? If you have a
small sample program that demonstrates the problem, please attach that
as well.

kllrnohj

unread,
Sep 22, 2009, 4:19:02 PM9/22/09
to android-ndk
The device is active. Full screen brightness, etc.. I have a simple
test program that opens the file, does a few read/writes, closes the
file, and then displays the results. So after launching the app the
file is only open for ~500ms or so, during which the device is fully
awake (as I just launched an app)

The problem with providing a sample program is that this is only
happening with custom hardware. The MicroSD card I'm using has a
custom chip in it and has an ever so slightly higher power draw, which
is what seems to be causing the problem.

I guess I'll file a bug report, but I'm not sure how useful it will
be.

Chris Stratton

unread,
Sep 23, 2009, 11:31:13 AM9/23/09
to android-ndk
On Sep 21, 6:21 pm, kllrnohj <kllrn...@gmail.com> wrote:
> I am developing an Android application that communicates with a custom
> micro SD chip. To talk properly to the custom chip, communication must
> be opened with the flag O_DIRECT.

While power issues are certainly an area that needs investigating, you
might also see if the underlying operation of the card by its linux
driver is any different in battery vs. external power modes. I could
imagine something different might be done with respect to write
caching, either out of concern for unexpected power loss or out of
trying to optimize for performance when the card might be mounted for
usb mass storage, vs for power consumption when on batteries.

kllrnohj

unread,
Sep 23, 2009, 5:59:58 PM9/23/09
to android-ndk
O_DIRECT is supposed to bypass all kernel caching and buffering and
write directly to the device synchronously. All of my write calls are
also followed by a call to fsync(). However, if the write does get
buffered my code should throw an error simply stating that it didn't
get a response and the SD card should still continue to work. What is
happening now is that I suddenly stop being able to read from it part
way through my application (so after 1 or 2 successful write/read
combinations) and Android claims that the SD card is now corrupt.

Oh, and here is a snippet from dmesg after read returned error EIO

<3>[175078.338409] mmc1: Command timeout
<3>[175078.338928] mmc1: DMA channel flushed (0x80000004)
<3>[175078.339385] Flush data: 0000c003 161f3000 00000000 00400040
00080008 00000003
<6>[175078.340179] mmc1: Worked around bug 1535304
<3>[175078.340972] mmcblk0: error -110 sending read/write command
<3>[175078.341430] mmcblk0: error -5 transferring data
<3>[175078.341857] end_request: I/O error, dev mmcblk0, sector 1376
<3>[175078.437652] mmc1: Command timeout
<3>[175078.443115] mmcblk0: error -110 sending read/write command
<3>[175078.443664] mmc1: Command timeout
<3>[175078.448944] mmc1: Command timeout
<3>[175078.454223] mmc1: Command timeout
<3>[175078.459747] mmc1: Command timeout
<3>[175078.465026] mmc1: Command timeout
<3>[175078.470306] mmc1: Command timeout
<3>[175078.476989] mmcblk0: error -110 requesting status
<3>[175078.477569] mmc1: Command timeout
<3>[175078.482971] end_request: I/O error, dev mmcblk0, sector 1376
<3>[175078.526855] mmc1: Command timeout
<3>[175078.532226] mmcblk0: error -110 sending read/write command
<3>[175078.532775] mmc1: Command timeout
<3>[175078.538085] mmc1: Command timeout
<3>[175078.543365] mmc1: Command timeout
<3>[175078.548645] mmc1: Command timeout
<3>[175078.554168] mmc1: Command timeout
<3>[175078.559448] mmc1: Command timeout
<3>[175078.565490] mmcblk0: error -110 requesting status
<3>[175078.566345] mmc1: Command timeout
<3>[175078.571807] end_request: I/O error, dev mmcblk0, sector 736
<3>[175078.572265] Buffer I/O error on device mmcblk0p1, logical block
487
<4>[175078.572723] lost page write due to I/O error on mmcblk0p1

Chris Stratton

unread,
Sep 23, 2009, 10:38:28 PM9/23/09
to android-ndk
Can you try with O_DIRECT and an ordinary SD memory card?

kllrnohj

unread,
Sep 24, 2009, 11:14:52 AM9/24/09
to android-ndk
O_DIRECT with an ordinary SD card does work.

Chris Stratton

unread,
Sep 24, 2009, 12:49:07 PM9/24/09
to android-ndk
On Sep 24, 11:14 am, kllrnohj <kllrn...@gmail.com> wrote:
> O_DIRECT with an ordinary SD card does work.

Okay, in that case I'll vote with those who recommended looking at the
power supply
Reply all
Reply to author
Forward
0 new messages