How to use USB device networking

From RidgeRun Developer Wiki
Revision as of 16:35, 6 June 2011 by Tfischer (talk | contribs)

The Linux USB gadget sub-system supports USB device functionality, including USB networking.

Configure hardware for USB OTG or USB device support

Depending on your hardware, you can use either USB OTG or USB device to enable support for USB networking.

Build USB Ethernet network gadget driver

The USB Ethernet network gadget driver caused the device to appear to be a USB network dongle when connected to a host computer. Generally it is best to build USB gadget drivers as modules instead of building them into the kernel so you can unload one and load another.

Symbol: USB_ETH [=m]
   Prompt: Ethernet Gadget (with CDC Ethernet support)
     Defined at drivers/usb/gadget/Kconfig:628
     Depends on: <choice> && NET
     Location:
       -> Kernel configuration
         -> Device Drivers
           -> USB support (USB_SUPPORT [=y])
             -> USB Gadget Support (USB_GADGET [=y])
               -> USB Gadget Drivers (<choice> [=m])

Build and install the kernel to the target hardware.

Load USB Ethernet network gadget driver

After booting the target hardware to a shell prompt, run:

modprobe g_ether

and you should see output similar to:

g_ether gadget: using random self ethernet address
g_ether gadget: using random host ethernet address
usb0: MAC be:b5:85:ef:48:33
usb0: HOST MAC 1a:b2:c3:43:8a:6e
g_ether gadget: Ethernet Gadget, version: Memorial Day 2008
g_ether gadget: g_ether ready

You can then verify the usb0 network interface exists:

ifconfig usb0

with output similar to:

usb0      Link encap:Ethernet  HWaddr BE:B5:85:EF:48:33  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Connect device to host PC

Before connecting the target device to the host PC, watch the syslog, by running the following command on the host PC:

sudo tail -f /var/log/messages

Connect the USB cable between the target device and the host PC. The messages file on the host PC should indicate the new USB device was detected.

Jun  6 10:15:29 contra-lx kernel: [1638241.377831] usb 1-4.4.2: new high speed USB device using ehci_hcd and address 47
Jun  6 10:15:29 contra-lx kernel: [1638241.510787] cdc_subset: probe of 1-4.4.2:1.0 failed with error -22
Jun  6 10:15:29 contra-lx kernel: [1638241.513437] cdc_subset 1-4.4.2:1.1: usb0: register 'cdc_subset' at usb-0000:00:02.1-4.4.2, Linux Device, 82:13:56:20:b4:cb
Jun  6 10:15:29 contra-lx kernel: [1638241.513490] usbcore: registered new interface driver cdc_subset
Jun  6 10:15:29 contra-lx kernel: [1638241.533442] cdc_ether: probe of 1-4.4.2:1.0 failed with error -16
Jun  6 10:15:29 contra-lx kernel: [1638241.533619] usbcore: registered new interface driver cdc_ether

On the host you can verify the new usb0 interface exists by running:

ifconfig usb0

with the output being similar to:

usb0      Link encap:Ethernet  HWaddr 82:13:56:20:b4:cb  
          inet6 addr: fe80::8013:56ff:fe20:b4cb/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:78 (78.0 B)

When you connect the cable, in the target device console, you should see:

g_ether gadget: high speed config #1: CDC Ethernet (ECM)

Assigning IP addresses

For this simple example, fixed IP addresses are assigned to the both the host PC USB network interface and the target device USB network interface.

On the host

HOST_USB_IP=10.0.1.1
sudo ifconfig usb0 $HOST_USB_IP netmask 255.255.255.0
route

On the target device

TARGET_USB_IP=10.0.1.2
ifconfig usb0 $TARGET_USB_IP netmask 255.255.255.0
route

with the route showing the default gateway is

Verifying network connectivity

On the target device, ping the host PC:

ping 10.0.1.1 -c 5

On the host PC, verify the number of packet sent and received is not zero

ifconfig usb0

Look at the values of RX packets: and TX packets:87.

Bridging host PC to allow device to reach the Internet

If your host PC is connected to the Internet (through WiFi or Ethernet), then you can allow the target device to share the host PC's Internet connection.