Manual USB OTG working

11 posts
Thanks Meter: 174
By CaptainMuon, Junior Member on 16th February 2013, 05:45 PM
Post Reply Email Thread

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):
$ 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:
$ adb connect
(or whatever your phone's IP is)
$ adb shell
Now you should be on your phone. Do
[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:
[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:
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!
The Following 148 Users Say Thank You to CaptainMuon For This Useful Post: [ View ] Gift CaptainMuon Ad-Free
16th February 2013, 05:59 PM |#2  
Senior Member
Flag Montreal
Thanks Meter: 893
Donate to Me
Re: Manual USB OTG working
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
16th February 2013, 08:22 PM |#3  
Senior Member
Thanks Meter: 69
You are a genius.
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 !!!

The Following 32 Users Say Thank You to scorpio16v For This Useful Post: [ View ] Gift scorpio16v Ad-Free
16th February 2013, 11:28 PM |#4  
parker09's Avatar
Senior Member
Thanks Meter: 175
Awesome development, one of my biggest gripes was the lack of USB OTG, I just assumed it was included as stock, that's why part of the reason I purchased this device.
The Following 3 Users Say Thank You to parker09 For This Useful Post: [ View ] Gift parker09 Ad-Free
16th February 2013, 11:44 PM |#5  
Senior Member
Thanks Meter: 1,563
Donate to Me
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.
The Following User Says Thank You to ziddey For This Useful Post: [ View ] Gift ziddey Ad-Free
16th February 2013, 11:55 PM |#6  
Thanks Meter: 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.
17th February 2013, 12:20 AM |#7  
Thanks Meter: 16
17th February 2013, 12:22 AM |#8  
Senior Member
Flag London
Thanks Meter: 102
Re: Manual USB OTG working
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
17th February 2013, 12:28 AM |#9  
Senior Member
Thanks Meter: 36
Originally Posted by Flynny75

Even miracast + mouse/keyboard would be awesome

we've already got that, haven't we?
17th February 2013, 01:14 AM |#10  
luckylui's Avatar
Senior Member
Flag Chi-Town
Thanks Meter: 496
Donate to Me
Re: Manual USB OTG working
Finally! Now I will get to use my 1TB Portable HDD!!

My 8 gig is FREE! Lol

Sent from my Nexus 4 using Tapatalk 2
17th February 2013, 01:18 AM |#11  
Senior Member
Thanks Meter: 111
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.

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.
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes