User avatar
thagrol
Posts: 1711
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Howto: ethernet gadget on Pi4B USB C

Mon Jul 15, 2019 3:04 pm

In case anyone wants to try it, here's how I got a 4B running as a USB ethenet gadget.

Firstly, this only works on the USB C port not on the USB 2 or USB 3 ports. Because of this you may have to find another way to get power into the 4B.
  1. Requirements:
    • Raspberry Pi 4B (any RAM size) and asociated cables, SD card etc.
    • USB A male to USB C male cable.
    • Another computer to act as the USB host. I've not tried it but I guess you could loop back to the USB 2/3 ports on the 4B
  2. Procedure:
    1. On the 4B, edit /boot/config.txt and add

      Code: Select all

      dtoverlay=dwc2
    2. On the 4B, edit /boot/cmdline.txt adding

      Code: Select all

       modules-load=dwc2,g_ether
    3. Reboot
    4. Log in to the 4B and run

      Code: Select all

      ifconfig -a
      You should see a new usb0 network interface.
    5. If you haven't already, connect to your USB host with the USB A to USB C cable. The USB host will show a new USB network adapter.
    6. Configure both enternet interfaces as required. DHCP, static IP addresses, bridging, routing, etc
  3. Gotchas:
    • MAC addresses used for each end of the USB link are randomly generated each time the g_ether module is started. This will not cause problems if using static IP addresses but will when using DCHP assigned one (especially if your DHCP server sets address based on the MAC address).

      Fixed MAC addresses can be set by appending this to /boot/cmdline.txt:

      Code: Select all

       g_ether.host_addr=<MAC addres> g_ether.dev_addr=<MAC addres>
      MAC addresses are expected to be unique to an interface.

      I have a script here: https://github.com/thagrol/usb-gadget that can generate suitable MAC addresses based on the serial number of the Pi it's running on. RUn as

      Code: Select all

      sudo ./set_id.py --test
      and copy the generated MAC addresses into /boot/cmdline.txt
    • The content of /boot/cmdline.txt must be on a single line with a linux line ending. It's best to edit it on a Pi with nano/vi/leafpad/etc rather than on windows.
    • From a network/ethernet point of view it doesn't matter which end of the link is running as a USB master or slave. You can have DHCP servers etc running on the 4B and have them available to the USB host.
    • The normal (USB A) ports on the 4B are unaffected by this.
    • If the 4B does not switch to device mode, this can be forced by changing your config.txt entry to

      Code: Select all

      dtoverlay=dwc2,dr_mode=peripheral
      and rebooting.
[Editied to fix a coupls of spelling errors and typos]
This space unintentionally left blank.

ejolson
Posts: 3252
Joined: Tue Mar 18, 2014 11:47 am

Re: Howto: ethernet gadget on Pi4B USB C

Mon Jul 15, 2019 10:47 pm

thagrol wrote:
Mon Jul 15, 2019 3:04 pm
In case anyone wants to try it, here's how I got a 4B running as a USB ethenet gadget.

Firstly, this only works on the USB C port not on the USB 2 or USB 3 ports. Because of this you may have to find another way to get power into the 4B.
  1. Requirements:
    • Raspberry Pi 4B (any RAM size) and asociated cables, SD card etc.
    • USB A male to USB C male cable.
    • Another computer to act as the USB host. I've not tried it but I guess you could loop back to the USB 2/3 ports on the 4B
  2. Procedure:
    1. On the 4B, edit /boot/config.txt and add

      Code: Select all

      dtoverlay=dwc2
    2. On the 4B, edit /boot/cmdline.txt adding

      Code: Select all

       modules-load=dwc2,g_ether
    3. Reboot
    4. Log in to the 4B and run

      Code: Select all

      ifconfig -a
      You should see a new usb0 network interface.
    5. If you haven't already, connect to your USB host with the USB A to USB C cable. The USB host will show a new USB network adapter.
    6. Configure both enternet interfaces as required. DHCP, static IP addresses, bridging, routing, etc
  3. Gotchas:
    • MAC addresses used for each end of the USB link are randomly generated each time the g_ether module is started. This will not cause problems if using static IP addresses but will when using DCHP assigned one (especially if your DHCP server sets address based on the MAC address).

      Fixed MAC addresses can be set by appending this to /boot/cmdline.txt:

      Code: Select all

       g_ether.host_addr=<MAC addres> g_ether.dev_addr=<MAC addres>
      MAC addresses are expected to be unique to an interface.

      I have a script here: https://github.com/thagrol/usb-gadget that can generate suitable MAC addresses based on the serial number of the Pi it's running on. RUn as

      Code: Select all

      sudo ./set_id.py --test
      and copy the generated MAC addresses into /boot/cmdline.txt
    • The content of /boot/cmdline.txt must be on a single line with a linux line ending. It's best to edit it on a Pi with nano/vi/leafpad/etc rather than on windows.
    • From a network/ethernet point of view it doesn't matter which end of the link is running as a USB master or slave. You can have DHCP servers etc running on the 4B and have them available to the USB host.
    • The normal (USB A) ports on the 4B are unaffected by this.
    • If the 4B does not switch to device mode, this can be forced by changing your config.txt entry to

      Code: Select all

      dtoverlay=dwc2,dr_mode=peripheral
      and rebooting.
[Editied to fix a coupls of spelling errors and typos]
Did you need to cut any power wires in the USB C cable to prevent backpowering from the Pi up to the host port?

User avatar
thagrol
Posts: 1711
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Howto: ethernet gadget on Pi4B USB C

Mon Jul 15, 2019 11:02 pm

ejolson wrote:
Mon Jul 15, 2019 10:47 pm
Did you need to cut any power wires in the USB C cable to prevent backpowering from the Pi up to the host port?
I haven't done so but see my post here: https://www.raspberrypi.org/forums/view ... 8#p1500930
This space unintentionally left blank.

Notupus
Posts: 30
Joined: Tue Jun 19, 2018 9:57 am

Re: Howto: ethernet gadget on Pi4B USB C

Tue Jul 16, 2019 7:26 am

Can I do other USB gadgets such as a hid keyboard or a flash drive?

User avatar
thagrol
Posts: 1711
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Howto: ethernet gadget on Pi4B USB C

Tue Jul 16, 2019 11:49 am

Notupus wrote:
Tue Jul 16, 2019 7:26 am
Can I do other USB gadgets such as a hid keyboard or a flash drive?
I've not done so but I would expect them to work.
This space unintentionally left blank.

Return to “Networking and servers”