USB Gadget

We can do all sorts of nifty things with a USB OTG connector. Here are some of them.

= USB Ethernet support =

This allows ethernet emulation over USB, allowing for all sorts of nifty things like SSH and NFS in one go plus charging over the same wire, at higher speeds than most Wifi connections.

Kernel support
Currently, the g_ether module is not compiled as part of our kernel configuration.

To enable this, follow the kernel building information of our manual build howto. But then after making defconfig, either run: make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig

Then trawl down the options and set the "Ethernet Gadget" to "m": Device Drivers ---> USB support ---> <*>   USB Gadget Support  --->    Ethernet Gadget (with CDC Ethernet support) <*>   NOP USB Transceiver Driver [*]   SUNXI USB2.0 Dual Role Controller support ---> [*]   Sunxi USB2.0 Manager USB0 Controller support (otg support) ---> (*) otg support Or just run: ./scripts/config -m CONFIG_USB_ETH -e CONFIG_USB_ETH_RNDIS -e CONFIG_USB_OTG_UTILS -e CONFIG_NOP_USB_XCEIV -e CONFIG_USB_SW_SUNXI_USB -e CONFIG_USB_SW_SUNXI_USB_MANAGER -e CONFIG_USB_SW_SUNXI_USB0_OTG

You can now continue following our manual build howto to continue kernel compilation and installation.

Note that for sun4i devices (A10), you also need to enable SoftWinner SUNXI USB peripheral controller in order to enable high-speed operation for gadget- otherwise it will be limited to full-speed only.

Device Drivers ---> USB support ---> <*>    USB Gadget Support --->  SoftWinner SUNXI USB Peripheral Controller

Loading the driver (on the device)
We should now be able to run: modprobe g_ether successfully. We can then make this module autoload by adding it to /etc/modules.

Now, so that g_ether doesn't randomly generate a new id every reboot, stick the following in /etc/modprobe.d/g_ether.conf: options g_ether host_addr=00:11:22:33:44:55

g_ether should've just generated a pair of addresses for you, so replace 00:11:22:33:44:55 with the outcome of: dmesg | grep "HOST MAC"

We can also use dev_addr option to set device MAC address.

Without network manager
Stop networkmanager: stop network-manager

To prevent network-manager from starting, run: echo "manual" > /etc/init/network-manager.override

Now add the following to /etc/network/interfaces: auto usb0 iface usb0 inet static address 192.168.0.2 netmask 255.255.255.0

To manually activate the interface and set an IP (NOTE: if configured with ip from iproute2 it will not work): ifconfig usb0 up     ifconfig usb0 192.168.0.2

Setting up the host
You can convince networkmanager to connect automatically to a specific MAC address, and then you need to hardcode the address to 192.168.0.1 for this connection.

If all goes well, you should now be able to just plug in the USB cable.

= USB storage support =

TODO.

= USB MIDI support =

This allows your device to act as a MIDI USB client. The connection is set up as a separate ALSA sound card with MIDI port.

Kernel Configuration
Enable the module in the kernel config (it's not enabled by default): Device Drivers ---> USB support ---> <*>   USB Gadget Support  --->    MIDI Gadget (EXPERIMENTAL)

Make sure you also have the ALSA MIDI seqencer support enabled: Device Drivers ---> Sound Card support ---> Advanced Linux Sound Architecture --->   Sequencer support

Loading the driver
Load the midi gadget driver with: modprobe g_midi

If it responds with a "device or resource busy" message, then you probably already have an ALSA sound card registered with index 0. Currently, the g_midi driver ignores the index parameter and always assumes index=0. But as soon as that is fixed, you can specify the index with: modprobe g_midi index=1

Simply try increasing the index until registration succeeds.

Known Error Messages
If you see the following message WRN:L2385(drivers/usb/sunxi_usb/udc/sw_udc.c):ERR: sw_udc_queue: inval 2 it means that you attempted to send a MIDI message via the MIDI gadget without it being connected to a host. Just plug in the USB cable (or unplug and retry if the first registration failed) and the error should go away.