wiki:USB_OTG

Version 2 (modified by Tim Harvey, 7 years ago) ( diff )

fixed missing images

USB On-The-Go (OTG)

USB On-The-Go, often abbreviated USB OTG or just OTG, is a specification that allows USB devices to act as either a host or a device. In host mode, you can connect a USB device (ie a flash stick/drive, phone, music player, keyboard, etc). In device mode, you can connect to a host (ie PC) and behave as a specific type of device.

The typical 4-pin USB signalling (D+, D-, VUSB, GND) is augmented with a 5th pin called 'ID' which floats to indicate device mode, and is grounded to indicate host mode. A dual-role controller will sense the state of the ID pin and select its mode taking care to provide VUSB when it is in host mode.

There are a variety of USB OTG connectors in both mini and micro form (micro is more popular today where-as mini USB connectors prevailed before 2010).

Many of the Gateworks Ventana product family single board computers all have a USB OTG micro-B connector allowing two types of cable connections:

  • a micro-A Adapter allows connection to standard-A USB cables, coming from peripherals (ie phone, flash stick etc)
  • a micro-B Adapter is more common and allows connection to standard-B USB cables, going to host controllers such as a PC

Product information can be found at http://www.gateworks.com

Host Mode

A Host mode cable allows connecting to a USB device. No special configuration is necessary for this.

https://shop.gateworks.com/image/cache/catalog/GW10089-500x500.png

Device Mode

A Device mode cable allows connection to a USB host such as a PC.

https://shop.gateworks.com/image/cache/catalog/GW10086-500x500.png

When used in this mode, the device (the Ventana board) needs to have a 'Gadget driver' loaded which impelemnts the personality of the device type you want. There are several Linux gadget drivers in today's linux kernel:

  • g_ether - behave as a 'Communication Device Class' (CDC) Ethernet device to create a 'USB to Ethernet' network connection.
  • g_file_storage - acts as a USB Mass Storage disk drive using either a regular file or a block device as its backing store specified as a module parameter or sysfs option
  • g_serial - behave as a ACM Serial device to create a 'USB to Serial' connection
  • g_cdc - provides two functions in one configuration: a CDC Ethernet (ECM) link, and a CDC ACM (serial port) link
  • g_multi - A multifunction composite gadget that can provide Ethernet (RNDIS and/or CDC), mass storage, and ACM serial link interfaces
  • g_hid - A Human Interface Device (HID) gadget that provides a generic interface for things such as keyboards, mice, touchscreens
  • g_webcam - A Webcam Device
  • g_ncm - USB CDC NCM subclass for Ethernet encapsulation allowing grouping of several ethernet frames into one USB transfer with varying alignment possibilities

Note that only one gadget driver (device personality) can be loaded at a time but there are some 'composite' gadget drivers that behave as 'composite devices' meaning they have multiple endpoints per USB device. This will seem familiar if you think of how a modern smart-phone works. Take an Android phone for example: When plugged into a host PC via micro-USB OTG, it will behave as a storage device (MTP), however if you want to have it behave as a serial debug device you have to go into the developer menu and select this option. Note that modern smartphones no longer behave as 'USB Mass Storage' devices as this protocol does not allow the device OS to access the filesystem at the same time the host PC does - instead these devices act as an MTP (Media Transfer Protocol) device.

Note that the Vendor ID (VID) and Device ID (DID) that is presented to the USB host is configuable (see here for details)

Reference:

g_ether Gadget

The g_ether gadget driver behaves as a USB-to-Ethernet dongle. Once loaded the device-mode system will add a 'usb<n>' network device which can be used the same as any other network device. On the USB host system, a similar network device will appear as long as a driver supporting the 'CDC Ethernet' standard is available.

This module can be built with additional support:

  • EEM: CDC Ethernet Emulation Model (EEM) is a newer standard that has a a simpler interface that can be used by more USB host hardware.
  • RNDIS: RNDIS support is an additional option (more CPU intensive) that may be more compatible with Windows drivers.

Example:

  • on target device (Gateworks board with OTG controller):
    modprobe g_ether
    
    • usb0 network interface appears on target (treat like any other network interface)
  • on host device (ie PC) a device with VID:PID 0525:a4a2 will appear conforming to the CDC Ethernet standard
    • usb0 appears on Linux host (using the cdc_ether driver)
  • module parameters can specify the VID, PID, device version, manufacturer string, product string, serialnumber
  • module parameters can specify the device and host ethernet address and whether or not to use CDC EEM mode

Linux Host Notes:

  • cdc_ether driver supports this and will create a 'usb<n>' device on the USB host

Windows Host Notes:

  • the g_ether driver is typically built with RNDIS support enabled which will make it compatible with drivers in Windows7 and above which will appear in the device manager as a 'USB Ethernet/RNDIS Gadget' and can be configured just as any other network interface.

Reference:

g_file_storage - USB Mass Storage Device

The g_file_storage driver behaves as a USB Mass Storage device such as a USB hard-disk or USB flash drive. You can decide whether to use a 'file' as a backing store, or a block device (ie a flash partition, or a physical disk). The file/device is provided to the module via the 'file' module parameter.

Example (using the first physical disk (/dev/sda) as a backing store):

  • on target device (Gateworks board with OTG controller):
    • load module specifying block device (ie /dev/sda the first physical disk device)
      modprobe g_file_storage file=/dev/sda
      
  • on host device (ie PC) a USB Mass Storage device (VID:PID 0525:a4a5 by default) will appear and behave as any other USB Mass Storage device (ie flash stick) would.
  • module parameters can specify the serialnumber, number of luns to support as well as some other low-level features (see 'modinfo g_file_storage' for details)

Using a file as a backing store:

  • Create backing storage file (done once, before you load the gadget module)
    dd bs=1M count=64 if=/dev/zero of=/tmp/backing_file # create empty file
    
    • you can choose to partition it (ie 'printf "83\n" | sfdisk /tmp/backing_file' for a single linux partition) or partition/format it from the host after connection
    • make sure you choose a partition/filesystem scheme that is compatible with the USB Host PC you are going to use (ie, Windows doesn't know how to deal with ext2/ext3/ext4 partitions, so you may want to use VFAT or NTFS)
    • specify the path to the backing store via the file parameter (ie 'modprobe g_mass_storage file=/tmp/backing_file')

Reference:

g_serial - Serial Device Gadget

The Serial Gadget supports CDC-ACM and CDC-OBEX which can inter-operate with the MS-Windows hosts or with Linux hosts using the 'cdc-acm' driver to create a 'USB-to-Serial' connection.

Example:

  • on target device (Gateworks board with OTG controller):
    • load module
      modprobe g_serial
      
  • on host device (ie PC) a USB CDC ACM device (VID:PID 0525:a4a7 by default) will appear and behave as a serial device
  • module parameters can specify the VID, PID, device version, manufacturer string, product string, serialnumber
  • module parameters can specify whether or not to use CDC ACM, CDC OBEX, and the number of ports to create

Linux USB Host notes:

  • the cdc_acm driver will enumerate this device as '/dev/ttyACM<n>'

Windows USB Host notes:

Reference:

g_cdc - Composite Ethernet + Serial Gadget

The g_cdc gadget supports two functions in one configuration:

  • a CDC Ethernet (ECM) link (USB-to-Ethernet connection)
  • a CDC ACM (serial port) link (USB-to-Serial connection)

Example:

  • on target device (Gateworks board with OTG controller):
    • load module
      modprobe g_cdc
      
  • on host device (ie PC) a USB CDC ACM device (VID:PID 0525:a4aa) will appear

Linux USB Host notes:

  • the cdc_acm driver will enumerate this device as '/dev/ttyACM<n>'
  • the cdc_ether driver will enumerate this device as a 'usb<n>' network device

Windows USB Host notes:

  • A CDC Composite Gadget device will appear in Device Manager
  • TODO: Is there a driver available that can use this in Windows? See g_multi below

g_multi - Composite Ethernet + Serial + Mass Storage Gadget

The g_multi gadget supports multiple functions in one configuration:

  • a CDC Ethernet (ECM) link
  • a CDC ACM (serial port) link
  • a USB Mass Storage device

Example:

  • on target device (Gateworks board with OTG controller):
    • load module
      modprobe g_cdc
      
  • on host device (ie PC) a USB CDC ACM device (VID:PID 1d6b:0104 by default) will appear
  • module parameters can specify the VID, PID, device version, manufacturer string, product string, serialnumber
  • module parameters can specify the ethernet device and host address and queue length multiplier at high speed

Linux USB Host notes:

  • the cdc_acm driver will enumerate this device as '/dev/ttyACM<n>'
  • the cdc_ether driver will enumerate this device as a 'usb<n>' network device
  • the usb-storage driver will provide the USB Mass Storage feature

Windows USB Host notes:

  • A Multifunction Composite Gadget device will appear in Device Manager
  • see here for details on Windows configuration

References:

g_hid - Human Interface Device (HID) Gadget

The HID gadget driver provides generic emulation of USB Human Interface Devices (HID), for example keyboards, mice, touchscreens, etc

Example:

  • on target device (Gateworks board with OTG controller):
    • load module
      modprobe g_hid
      
  • module parameters can specify the VID, PID, device version, manufacturer string, product string, serialnumber

References:

g_webcam - Composite USB Audio and Video Class Gadget

The g_webcam gadget driver provides a Composite USB Audio and Video Class device.

Example:

  • on target device (Gateworks board with OTG controller):
    • load module
      modprobe g_webcam
      
  • on host device (ie PC) a 'Linux Foundation Webcam Gadget' device (VID:PID 1d6b:0102 by default) will appear
  • on target device (Gateworks board) a /dev/video<n> device will be created and avialable as a Video4Linux output device supporting 320/240 YUYV video
  • module parameters can specify the VID, PID, device version, manufacturer string, product string, serialnumber

Linux USB Host notes:

  • the uvcvideo driver will enumerate the device and create a /dev/video<n> video capture device

Windows USB Host notes:

  • A USB Composite device will appear in Device Manager
  • A UVC Camera device will appear under Imaging devices in the device manager and be available to capture video

g_ncm - USB CDC NCM subclass Ethernet Gadget

The g_ncm gadget driver provides a a USB CDC NCM subclass. NCM is an advanced protocol for Ethernet encapsulation, allowing grouping of several ethernet frames into one USB transfer with various alignment possibilities.

Example:

  • on target device (Gateworks board with OTG controller):
    • load module
      modprobe g_ncm
      
  • on host device (ie PC) a 'Linux-USB Ethernet Gadget' device (VID:PID 0525:a4a1 by default) will appear
  • on target device (Gateworks board) a usb<n> network device will be created
  • module parameters can specify the VID, PID, device version, manufacturer string, product string, serialnumber
  • module parameters can specify the device and host ethernet addresses and the queue length multiplier used at high speeds

Linux USB Host notes:

  • the cdc_ncm driver will enumerate the device and create a /dev/video<n> video capture device

Windows USB Host notes:

  • A NCM Gadget device will appear in Device Manager
  • see here for details about a Windows CDC NCM driver

OpenWrt OTG

Use the make kernel_menuconfig to add gadget modules.

They are located in Device Drivers > USB support > USB Gadget Support

Note: See TracWiki for help on using the wiki.