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 =

This allows your devices act as a USB mass storage like external hard drive or thumb drive.

Kernel support
Currently, the g_mass_storage module is not compiled as part of default kernel configuration.

To enable this, follow same kernel building information as previous section USB Ethernet support but instead of compiling "Ethernet Gadget", select the following to "m": Device Drivers ---> USB support ---> USB Gadget Support --->     Mass Storage Gadget

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

Preparing shared storage device
See "Backing Storage for the Mass Storage Gadget" for full instructions and recommendations.

Be aware that shared storage cannot be used in "read write" mode if both systems (device and host) are using it at same time except if you accept to corrupt your data...

An existing physical partition or a logical volume can be used as shared storage device; another option is to use a flat file as shared storage.

Creating a sparse file
Use "dd" command's "count" and "seek" options to create a 1GB file that will not use storage until it is used:

-rw-r--r-- 1 root root 1073741824 Feb 15 16:40 /mass_storage 0      /mass_storage
 * 1) dd if=/dev/zero of=/mass_storage bs=1M seek=1024 count=0
 * 2) ls -l /mass_storage
 * 1) du -hs /mass_storage

Partitioning and formatting sparse file
To be recognized by most Operating Systems, create a single FAT type partition and format it as DOS filesystem using Linux loop device driver.

Create a single partition
,,c EOT
 * 1) cat <<EOT | sfdisk --in-order -L -uM /mass_storage

Find partition offset

 * 1) fdisk -lu /mass_storage

Disk /mass_storage: 1073 MB, 1073741824 bytes 139 heads, 8 sectors/track, 1885 cylinders, total 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000

Device Boot     Start         End      Blocks   Id  System /mass_storage1              1     2097151     1048575+   c  W95 FAT32 (LBA)

First partition starts at first sector: offset = 1 * 512 = 512 bytes

Set up loop device
/dev/loop0: [b302]:14867 (/mass_storage), offset 512
 * 1) losetup -o512 /dev/loop0 /mass_storage
 * 2) losetup -a

Format device

 * 1) apt-get install dosfstools
 * 2) mkdosfs /dev/loop0

Test device
Filesystem     Size  Used Avail Use% Mounted on /dev/loop0     1022M  4.0K 1022M   1% /mnt /dev/loop0 on /mnt type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=ascii,shortname=mixed,errors=remount-ro)
 * 1) mount -t vfat /dev/loop0 /mnt/
 * 2) df -h /mnt
 * 1) mount | grep mnt

Release device

 * 1) umount  /mnt/
 * 2) losetup -d /dev/loop0
 * 3) losetup -a

As we can see below, 1GB sparse file is only using 2.1MB storage (size of filesystem metadata):

2.1M   /mass_storage -rw-r--r-- 1 root root 1.0G Feb 15 16:54 /mass_storage
 * 1) du -sh /mass_storage
 * 1) ls -lh /mass_storage

Exporting mass storage / Loading the driver (on the device)
It looks like g_mass_storage conflicts with g_ether, remove g_ether first (if previously loaded):


 * 1) modprobe -r g_ether

Load g_mass_storage specifying storage to share (can also be a physical partition or a logical volume):


 * 1) modprobe g_mass_storage file=/mass_storage

We can now plug device to another host and use it as USB connected storage.

Notes: Surprisingly it works well my Mac OS hosts (device automatically appears in Finder, I can copy and read files) but not with my Linux hosts (device appears and disappears constantly and cannot be mounted). Not tested with Windows hosts.

= 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. You can specify the index via the module options, simply choose the next free index: modprobe g_midi index=1

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.