Manual USB OTG working

Search This thread

CaptainMuon

Member
Feb 3, 2013
11
173
Hi,

As you know, the Nexus 4 doesn't support USB OTG, meaning you can't use it with USB drives, gamepads, etc.. One reason is that the device doesn't seem to provide the neccessary 5V, the other reason is that it's not supported by the kernel.

So, I've been digging a bit, and now I can claim partial success! It's possible to supply the 5V yourself, and then activate USB host mode manually. It's a bit cumbersome, but for the beginning better than nothing. I tried a mouse and USB drives.

I should warn you that this is just a proof of concept and nothing that you'll want to use on a daily basis. Regular USB doesn't work properly while running this kernel, and I haven't checked battery consumption. Also this is based on Android 4.2.1, and if you have 4.2.2 you might not be able to make calls. That shouldn't be a problem as this is for testing only, though. Just let me say, I can't guarantee for anything, and I'm not responsible if you brick or blow up your phone.

You'll need:

- A USB OTG cable
- A USB Y cable to provide the 5V (It has two USB-A plugs and one USB-A socket.)
- My custom kernel
- Unlocked bootloader
- Android developer tools (adb, fastboot)

Load the boot image on your phone (this is not permantent):
Code:
$ fastboot boot myboot.img

Connect everything as follows (should be obvious):
- Y cable power plug (typically red) to the PC or adapter
- Other Y cable plug into the OTG cable
- OTG cable into the phone
- USB device into the Y cable socket

You need a remote shell on your device. Use adbWireless or similar to enable ADB over WiFi, then do:
Code:
$ adb connect 192.168.0.108:5555
(or whatever your phone's IP is)
$ adb shell
Now you should be on your phone. Do
Code:
[email protected]:/ $ ls /sys/kernel/debug/msm_otg/
and you should see 'mode' if the new kernel works. Now become superuser and enable USB host mode:
Code:
[email protected]:/ $ su
[email protected]:/ $ echo host > /sys/kernel/debug/msm_otg/mode
To disable USB host mode, use "none" or "peripheral". Note that at this point, I couldn't get it to go into regular USB mode. This means that MTP and adb over USB doesn't work yet.

Connect a device, and you should see more than two entries under "lsusb". Congratulations, your Nexus 4 has just recognized it's first USB device! If you install Stickmount (you have to sideload it since it's marked as not compatible), you can mount USB flash drives. Nexus Media Importer doesn't seem to work, though.

How does this work?
Basically, I just added the following line to the beginning of msm_otg_debugfs_init in msm_otg.c:
Code:
motg->pdata->otg_control = OTG_USER_CONTROL;

This enables the mode file in debugfs I use for mode switching. The next step would be to enable manual mode switching while keeping regular USB support, which shouldn't be too hard. Later down the line we can think about switching automatically on cable insertion, and maybe event getting 5V out of the device, but that would be a lot harder.

So, try it out, let me know if it works for you, and feel free to tinker around and improve what I've got!
 
Last edited:

karendar

Senior Member
Aug 9, 2010
2,214
883
Montreal
Nice to see some progress going. Only thing that irks me is the Y cable though, kinda defeats the purpose of OTG to me...

Thanks a bunch for your work though!

Sent from my Nexus 4 using xda premium
 

ziddey

Senior Member
Aug 26, 2007
1,829
1,573
This is awesome. Going to have to order some cables now. Since we have to supply power anyway, is it possible to support usb-otg + charging? Figure the phone is "too thin" anyway... I'll probably be making a 64gb microsdxc + battery case for the phone. Hello extended battery life.
 
  • Like
Reactions: lucky.lu

eiriklf

Senior Member
Dec 26, 2010
85
13
Great work, I was just looking for this yesterday. Would it work with a USB dac (Assuming I had a powered usb hub)? Just asking because I think usb audio is often overlooked, and I would really like to have it.
 

Flynny75

Senior Member
Jun 11, 2010
371
101
London
It would also be interesting to see if the slimport adapter allows otg pass through (their website says it does)... Then that's HDMI +charging +mouse/keyboard. Even miracast + mouse/keyboard would be awesome

Sent from my Nexus 4 using Tapatalk 2
 

sga999

Senior Member
Mar 13, 2012
817
113
I'd like to be sure I'm getting a good USB Y cable. I see a few on Amazon, some have the word 'Power' in them, some don't. Most don't have a red plug. Are there some specific characteristics I should look for in the description to know that it's suitable for this purpose?

Here's one example that does NOT say 'Power' in its description.
http://www.amazon.com/OEM-UTStarcom-USB-Adapter-Cable/dp/B002W8EDOM/ref=pd_sim_e_2

Thanks, CaptainMuon, for your terrific work!

EDIT: After reading Scorpio16v"s post (and watching his video...thank you!), I thought I would try with a powered hub, since I don't have the Y cable. Also, I used the terminal emulator instead of adb, as he suggested. YES! It worked with a USB micro sd card reader adapter (I had bought it from ebay specifically for this Nexus 4 since I knew there was no sd card slot). That required stickmount, as CaptainMuon mentioned. Then I tried a USB mouse, and that worked also. This is great!

I'd still like to know more about the Y cable, as I asked above, even though this other method worked well for me.
 
Last edited:

Cice96

Senior Member
May 9, 2012
183
129
R: Manual USB OTG working

Can the USB hub be non powered?

Inviato dal mio Nexus 4
 

Arunabh

Senior Member
Oct 7, 2012
178
68
Greaaatt news and excellent development on this front sir.
I was eagerly waiting for someone to come up with a solution to this topic.
But coming back I've read in so many places that a kernel can be tweaked so as to supply 5V to the socket!!(I'm considering that is the problem here. With power supply its working).
??

Sent from my Nexus 4 using xda app-developers app
 

therkr

Senior Member
Mar 2, 2011
256
16
Awesome, my one gripe with the phone is now (almost) gone!:good:
Any chance it it'll work with non-OTG cables/adapters (The plain USB host ones which don't have that extra pin)?
 

ScuzUK

Senior Member
Jun 25, 2011
110
18
Just posting to thank you for all the time and effort you must of put in.
 

CaptainMuon

Member
Feb 3, 2013
11
173
This is awesome. Going to have to order some cables now. Since we have to supply power anyway, is it possible to support usb-otg + charging? Figure the phone is "too thin" anyway... I'll probably be making a 64gb microsdxc + battery case for the phone. Hello extended battery life.

It seems to be charging in USB host mode. (In my Y cable, the +5V line connects all three plugs, so the phone gets juice too. Not sure if that's always the case.) We have to do some testing to see if it charges at normal speed, etc.

Great work, I was just looking for this yesterday. Would it work with a USB dac (Assuming I had a powered usb hub)? Just asking because I think usb audio is often overlooked, and I would really like to have it.
I guess, if it is supported on any other android device, it should be possible here. Would be great if someone with a DAC would try it!

It would also be interesting to see if the slimport adapter allows otg pass through (their website says it does)... Then that's HDMI +charging +mouse/keyboard. Even miracast + mouse/keyboard would be awesome
Dito. If someone has such an adapter, I'd be excited to know if it works.

I'd like to be sure I'm getting a good USB Y cable. I see a few on Amazon, some have the word 'Power' in them, some don't. Most don't have a red plug. Are there some specific characteristics I should look for in the description to know that it's suitable for this purpose?

Here's one example that does NOT say 'Power' in its description.
http://www.amazon.com/OEM-UTStarcom-USB-Adapter-Cable/dp/B002W8EDOM/ref=pd_sim_e_2
Hmm, I don't know how you could make a Y cable that doesn't provide power from one end, and data from the other :). It should work, but I know there are a lot of scam/poor quality cables out there, so I can't guarantee it. I've got this one:
http://www.amazon.de/Delock-Adapter...=sr_1_4?s=ce-de&ie=UTF8&qid=1361096352&sr=1-4
 
  • Like
Reactions: aled99

rejanmanis

Senior Member
Feb 17, 2011
452
63
Jakarta
you should post this to development section ;

let's see if other dev can also contribute to make a nice working kernel with these stuff;
without any bugs you had mention above.

BTW ;

Nice work Bro. :good:

by the way ; i can use my powerbanks as the power source. so i don't need any land electric sources.

not yet try it, i will need to find the XY cable first ( non powered USB Hub will do fine, and powered by the 11000Mah Powerbanks )
 

Top Liked Posts

  • There are no posts matching your filters.
  • 147
    Hi,

    As you know, the Nexus 4 doesn't support USB OTG, meaning you can't use it with USB drives, gamepads, etc.. One reason is that the device doesn't seem to provide the neccessary 5V, the other reason is that it's not supported by the kernel.

    So, I've been digging a bit, and now I can claim partial success! It's possible to supply the 5V yourself, and then activate USB host mode manually. It's a bit cumbersome, but for the beginning better than nothing. I tried a mouse and USB drives.

    I should warn you that this is just a proof of concept and nothing that you'll want to use on a daily basis. Regular USB doesn't work properly while running this kernel, and I haven't checked battery consumption. Also this is based on Android 4.2.1, and if you have 4.2.2 you might not be able to make calls. That shouldn't be a problem as this is for testing only, though. Just let me say, I can't guarantee for anything, and I'm not responsible if you brick or blow up your phone.

    You'll need:

    - A USB OTG cable
    - A USB Y cable to provide the 5V (It has two USB-A plugs and one USB-A socket.)
    - My custom kernel
    - Unlocked bootloader
    - Android developer tools (adb, fastboot)

    Load the boot image on your phone (this is not permantent):
    Code:
    $ fastboot boot myboot.img

    Connect everything as follows (should be obvious):
    - Y cable power plug (typically red) to the PC or adapter
    - Other Y cable plug into the OTG cable
    - OTG cable into the phone
    - USB device into the Y cable socket

    You need a remote shell on your device. Use adbWireless or similar to enable ADB over WiFi, then do:
    Code:
    $ adb connect 192.168.0.108:5555
    (or whatever your phone's IP is)
    $ adb shell
    Now you should be on your phone. Do
    Code:
    [email protected]:/ $ ls /sys/kernel/debug/msm_otg/
    and you should see 'mode' if the new kernel works. Now become superuser and enable USB host mode:
    Code:
    [email protected]:/ $ su
    [email protected]:/ $ echo host > /sys/kernel/debug/msm_otg/mode
    To disable USB host mode, use "none" or "peripheral". Note that at this point, I couldn't get it to go into regular USB mode. This means that MTP and adb over USB doesn't work yet.

    Connect a device, and you should see more than two entries under "lsusb". Congratulations, your Nexus 4 has just recognized it's first USB device! If you install Stickmount (you have to sideload it since it's marked as not compatible), you can mount USB flash drives. Nexus Media Importer doesn't seem to work, though.

    How does this work?
    Basically, I just added the following line to the beginning of msm_otg_debugfs_init in msm_otg.c:
    Code:
    motg->pdata->otg_control = OTG_USER_CONTROL;

    This enables the mode file in debugfs I use for mode switching. The next step would be to enable manual mode switching while keeping regular USB support, which shouldn't be too hard. Later down the line we can think about switching automatically on cable insertion, and maybe event getting 5V out of the device, but that would be a lot harder.

    So, try it out, let me know if it works for you, and feel free to tinker around and improve what I've got!
    32
    You are a genius. :good:
    Just connected a powered USB-HUB and a mouse to my OTG cable, flashed the kernel and typed the commands directly with a terminal app on the device.
    It simply works !!!

    20
    you should post this to development section ;
    Didn't have enought posts to post there :). Next kernel will be in the development section.

    So what did you change in the kernel?
    Basically only the one line in the OP, and I used the default config. The kernel also writes a bit of debug info to dmesg (lines with '(jm)'). It does nothing functional, but helped me figure out what to edit. If you want the diff, I can post it here, but it's really not so interesting.

    I'm pretty busy with work the next couple of days, but hope I'll be able to continue this soon.

    (Oh, and @ everybody, thanks for the thanks. Nice to see my work is appreciated.)
    9
    This is so awesome! It's working perfectly with the Y-cable seen here. I was able to mount a USB flash drive with Stickmount, and play back a .flac file from the drive via Poweramp. The Nexus 4 also says that it's charging, and I'm pretty sure that it actually is. My phone was at 9% battery life when I flashed an older PA 4.2.1 build, and then booted OP's kernel. If the Y-cable wasn't supplying any power, my phone would be dead.

    Anyway, here's a picture of my current set up. It's great to see some progress.





    RHjtjM4l.png
    4
    I tried a different method of creating a boot.img but got the same results.

    From how I understand it, it should be possible to simply fastboot boot zImage.
    Code:
    fastboot boot zImage
    creating boot image...
    creating boot image - 4421632 bytes
    downloading 'boot.img'...
    OKAY [  0.272s]
    booting...
    OKAY [  0.121s]
    finished. total time: 0.393s
    fastboot takes the zImage and creates a boot.img, and especially since it's simply being booted, it should be able to use the existing ramdisk?

    I believe Franco uses Google's 4.7 toolchain, which is what I've been trying now. With regards to the mismatch:

    "Yes. Go to arch/arm/kernel/auto_hotplug.c check for __cpuinit string in some of the function declarations (just for a ctrl+f and search for __cpuinit) remove those strings, and you're good to go. "


    I removed __cpuinit from auto_hotplug_late_resume() and it compiled successfully seemingly.. I did kill __cpuinit from all function declarations but that threw errors for all the other functions..

    I'm using android-ndk-r8d/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86/bin for my toolchain which I'm assuming is the right one?

    Also, my size values reported in the last post were the resulting "boot.img" created by fastboot. The size seems appropriate (my zImage + ramdisk pulled from official franco is approximately franco's boot.img).

    It stays on the Google screen..


    Will try to experiment with abootimg, but I seem to be missing blkid.h even though I have e2fsprogs. Will look into it further tomorrow.

    edit: hah didn't realize it was in the repository.. looks like we may have a successful boot.


    edit: we have lift off. seems to be extremely glitchy as of now, but all I've done is remove the condition motg->pdata->otg_control == OTG_USER_CONTROL originally required for the debug mode.



    Code:
    [email protected]:/ # df
    Filesystem               Size     Used     Free   Blksize
    /dev                   936.2M    32K   936.2M   4096
    /mnt/secure            936.2M     0K   936.2M   4096
    /mnt/asec              936.2M     0K   936.2M   4096
    /mnt/obb               936.2M     0K   936.2M   4096
    /system                827.8M   397.8M   430M   4096
    /cache                 551.7M    70.6M   481.2M   4096
    /data                    5.7G     3.8G     1.8G   4096
    /persist                15.8M     4.2M    11.6M   4096
    /firmware               64M    53.2M    10.8M   16384
    /mnt/shell/emulated      5.7G     3.8G     1.8G   4096
    /mnt/shell/emulated/0    14.6G    24K    14.6G   8192
    [email protected]:/ # mount | grep emulated
    /dev/fuse /mnt/shell/emulated fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0
    /dev/block/vold/8:1 /mnt/shell/emulated/0 vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0

    Not sure how it got automatically mounted at /mnt/shell/emulated/0, but it obviously causes chaos (although /dev/block/vold/8:1 is internal?? which is so strange why/how it was actually the sd card).

    Unmounting it (/mnt/shell/emulated/0) and using Stickmount to remount results in:

    Code:
    /dev/block/sda1 /data/media/0/usbStorage/sda1 vfat rw,dirsync,nosuid,nodev,relatime,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0


    http://d-h.st/4Qb

    Obviously as much a test build as OP and only worth flashing for proof-of-concept purposes, but figure there's some of you out there running cm10.1 and can't use OP's kernel.


    Will start to dig in more tomorrow hopefully. It's almost 9am now. Goodnight.