[Nexus 4 Original Android Development] ziddey:
Externally Powered USB OTG - Nexus 4

This is an all-in-one patch to enable externally powered OTG (technically usb host mode) support. It's built off either the stock kernel or Franco's kernel sources, and should work with any ROM (that these kernels otherwise support). Refer to the second post for details on modifications and additions.

Again, power MUST be supplied externally, as there is no way for the phone to provide it.

Requirements:

Power MUST be supplied to both the USB device and phone. The easiest way would be by using an OTG Y-cable:

If using a traditional OTG cable, a USB Y-cable can be used:

Some powered USB hubs also send power up to the host and can be used directly with a regular OTG cable.

I am not endorsing any specific product or seller. Links are provided solely as examples, and are by no means definitive. As long as the phone and device both get 5V (charger, computer, etc.), and the data pins are connected, host mode will work (provided enough current can be supplied).

Installation:

Simply install the zip in recovery. Script will automatically install/patch necessary files. Must reinstall any time ROM is updated.

To uninstall, simply reflash your ROM. Data wipe is not necessary. If for some reason that's not an option, use the flashable unmod script to remove ROM-side modifications. Flash your kernel of choice afterwards (must flash "reset" kernel first if flashing an "anykernel").

Recovery:

(Optional)

For support in recovery, I've created a sort of "any-any" script. It replaces the recovery's kernel with the boot one. Therefore, by flashing this after the main patch, OTG will effectively be enabled in recovery (after a reboot). However, it is on the actual recovery itself to provide support for usb drives-- TWRP does. Otherwise, you'll have to manually mount any drives via linux console commands.

For your own safety/sanity, ensure the main patch works before flashing this. If recovery fails to boot after flashing, it can easily be replaced by using GooManager or similar. Worst case scenario, a new recovery can always be flashed via fastboot.

Downloads:

(Changelog at end of second post)

MAKE SURE TO DOWNLOAD THE RIGHT VERSION FOR YOUR ROM.

I don't keep track of all the different ROMs so it's on you guys to figure out which one is appropriate. The -CM builds have the two "CAF" commits that are now required for CM and its derivatives (unless they have specifically reverted the associated commits).

Franco-CM builds make use of a ramdisk mod script, which may have unpredictable results. Be ready in case it doesn't boot.

Current:

4.4.x: 2013.12.11 1604ET: [fk r199] [fk r199-CM]

4.4.x: 2013.12.06 1445ET: [aosp r7]

4.4: 2013.12.03 1522ET: [fk r197] [fk r197-CM]

4.4: 2013.12.01 1855ET: [aosp r6]

recoverymod.zip

Old:

4.3: 2013.10.27 1518ET Franco r193: [JWR] [JSS/JLS] [JWR-CM] [JSS/JLS-CM]

4.3: 2013.10.17 1957ET STOCK: [JWR] Stock kernel with OTG -- Only for stock rom

4.2: 2013.07.29 1101ET Franco r165: [JDQ]

ziddey-otg-unmod-20131002.zip

Bugs / Notes:

An OTG cable has the ID pin grounded out, which is used to trigger usb host mode. However, ID pin detection is broken in the Nexus 4 (although working for Slimport detection). Instead, we rely on detection of a "proprietary" charger (voltage on the data pins) in order to determine when to enable host mode.

Self-powered devices (e.g. digital cameras) don't send power to the phone. This will cause the device to not be detectable. Therefore, external power is still required.

Slimport cannot work concurrently with usb data due to hardware limitations (Slimport takes over the usb data pins).

USB drive will automatically mount at /storage/usbdisk0 (also accessible at /usbdisk and /mnt/usbdisk). Media scanning should occur automatically. Make sure to unmount before removal to avoid data loss.

Stock Android only supports FAT for storage. NTFS/exFAT/ext4 partitions may require the use of a third party app like StickMount (CM now supports these partitions natively!).

There appears to be a minor bug in the AOSP code that prevents available space from being reported in Settings->Storage->USB Storage. The screenshot is of CM10.1, which has this fixed

Current builds do not allow for host mode without charging. Use this as a workaround:

Standard Disclaimer-- Flashing this patch is at your own risk, and carries no warranty or liability on my part. The assumption is that you will perform due diligence before flashing and make any necessary backups if required.

Screenshots:

Credits:

CaptainMuon, for proving that host mode is possible on the Nexus 4.

Franco, for his kernel, which this patch is based off.

garyd9, for his command to patch platform.xml.

Chainfire, for his usb host wisdom, and article on secondary storage write permissions.

arpruss, for his compiled zip-for-android

All you guys for testing!