Manual USB OTG working
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.
- 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 192.168.0.108:5555
(or whatever your phone's IP is)
$ adb shell
Now you should be on your phone. Do
shell@android:/ $ ls /sys/kernel/debug/msm_otg/
and you should see 'mode' if the new kernel works. Now become superuser and enable USB host mode:
shell@android:/ $ su
root@android:/ $ 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!