xnux.eu - site map - news

USB OTG Gadget (Peripheral) Mode

You can make Allwinner devices emulate various USB peripherals using the Linux USB gadget drivers. There are drivers for emulating serial ports, network interfaces, storage devices, video and audio devices, etc.

It's possible to emulate multiple devices at once, so when you connect your device to the PC's USB port, you'll see a new network interface, get access to the serial console on the device over USB and also be able to export the SD/eMMC as two USB storage devices to the PC.

The above is an extreme example.

The USB gadget device setup can be done via configfs by creating directories and manipulatin­g files.

Sample configuration

Here's a script that I use to emulate a USB mass storage device and the network link between the device and the host.

cd /sys/kernel/config/usb_gadget/

# Create a new gadget:
mkdir g1
cd g1

# Setup identifying information for the USB bus for our emulated
# USB device as a whole:
echo 0x1d6b > idVendor
echo 0x2000 > idProduct
mkdir strings/0x409
echo "11111111111" > strings/0x409/serialnumber
echo "megous.com" > strings/0x409/manufacturer
echo "Oranbge Pi PC" > strings/0x409/product

# Create a new configuration (groups multiple USB functions together):
mkdir configs/c.1/
mkdir configs/c.1/strings/0x409
echo "Oranbge Pi Storage" > configs/c.1/strings/0x409/configuration

# Create a mass storage function and link it to the configuration:
# backing file or block device for the mass storage must not be mounted
# or used by anything else, while the configuration is active
umount /dev/mmcblk0
mkdir functions/mass_storage.1/
echo /dev/mmcblk0 > functions/mass_storage.1/lun.0/file
ln -s functions/mass_storage.1/ configs/c.1/

# Create a rndis (something like ethernet link) function:
mkdir functions/rndis.1/
ln -s functions/rndis.1/ configs/c.1/

# Activate the gadget by selecting the UDC device that should be
# used for emulation:
echo musb-hdrc.1.auto > UDC

To use the network link, you need to setup network interfaces on both end, either manually or via DHCP.