How to Use USB Gadget Ethernet

Summary

An Ethernet controller can add lots of complexity and cost to an embedded board, and consume a large amount of PCB real-estate. Most consumer embedded systems omit an Ethernet port completely, as do some development boards (such as the Atmel AT91SAM9RL-EK board). While testing a product or designing a system, it may be desirable to use Ethernet to transmit data to and from the device. Fortunately, the Linux USB gadget subsystem provides a way to leverage existing hardware (many devices have a USB device port) to use utilities such as NFS, SSH, and FTP. This is accomplished by using the g_ether driver.

The g_ether driver can be built into the kernel, or loaded as a module. Once the driver is loaded, it creates an interface called usb0 on the target. Once the USB cable is connected to the host, it creates an interface known as usb0 on the host machine. The host can be either Windows or Linux, but an extra driver is required to see the device on a Windows host. Once the interfaces exist, they can be treated as if they were normal Ethernet interfaces.

Prerequisites

  • Kernel built with the following options:
    • CONFIG_USB_GADGET=y
    • CONFIG_USB_ETH=m
    • CONFIG_USB_ETH_RNDIS=y (If connecting to Windows Host)
    • CONFIG_INET=y
  • RFS containing module-init-tools

Procedure

  1. Load g_ether module on target system, OR build the driver into the kernel.
  2. Bring up interface on the target.
  3. Bring up interface on the host.

Configuring the target

  1. On the target, probe g_ether module. You can specify a device MAC address and host MAC address using the parameters dev_addr and host_addr respectively.
    # modprobe g_ether dev_addr=12:34:56:78:9a:bc host_addr=12:34:56:78:9a:bd
  2. Alternatively, if the g_ether driver was built into the kernel, you can specify parameters on the kernel command line:
    g_ether.dev_addr=12:34:56:78:9a:bc g_ether.host_addr=12:34:56:78:9a:bd
  3. Connect the target to the host using a USB cable.
  4. On the target, bring up the usb0 interface using ifconfig.
    # ifconfig usb0 192.168.0.100 netmask 255.255.255.0
  5. For all intents and purposes, the usb0 interface can now be treated just like any other network interface (e.g. eth0).

Configuring a Linux host

  1. Connect the target to the host using a USB cable.
  2. Use ifconfig to configure the usb0 interface.
    # ifconfig usb0 192.168.0.101 netmask 255.255.255.0
  3. For all intents and purposes, the usb0 interface can now be treated just like any other network interface (e.g. eth0).

Target -> Host: Ping 192.168.0.101
Host -> Target: Ping 192.168.0.100

Configuring a Windows host

g_ether Module Parameters

Parameter Name Function Type Notes
idVendor USB Vendor ID ushort -
idProduct USB Product ID ushort -
bcdDevice USB Device version (BCD) ushort -
iManufacturer USB Manufacturer string char* -
iProduct USB Product string char* -
iSerialNumber Serial Number char* -
dev_addr Device Ethernet Address char* Generated randomly if blank
host_addr Host Ethernet Address char* Generated randomly if blank

References