Pi4 USB-C Gadget

Pi4 Gadget

I’ve previously blogged about using Pi Zero (and Zero W) devices as USB Gadgets. This allows them to be powered and accessed via one of the micro USB sockets and it shows up as both a CD-Drive and a ethernet device.

A recent update to the Raspberry Pi 4 bootloader not only enables the low power mode for the USB hardware, allows the enabling of Network boot and enables data over the USB-C port. The lower power means it should run (without any hats) with the power supplied from a laptop.

Details of how to check/update the bootloader can be found here.

Given that the Pi4 has a Gigabit Ethernet adapter, WiFi and 4 USB sockets (need to keep the power draw low to be safe) and up to 4Gb RAM to go with it’s 4 x 1.5Ghz core processor it makes for a very attractive plugin compute device.

With this enabled all the same script from the Pi Zero’s should just work but here is the updated version for Raspbian Buster.

  • Add dtoverlay=dwc2 to the /boot/config.txt
  • Add modules-load=dwc2 to the end of /boot/cmdline.txt
  • Add libcomposite to /etc/modules
  • Add denyinterfaces usb0 to /etc/dhcpcd.conf
  • Install dnsmasq with sudo apt-get install dnsmasq
  • Create /etc/dnsmasq.d/usb with following content
interface=usb0
dhcp-range=10.55.0.2,10.55.0.6,10.55.0.0,255.255.255.248,1h
dhcp-option=3
leasefile-ro
  • Create /etc/network/interfaces.d/usb0 with the following content
auto usb0
allow-hotplug usb0
iface usb0 inet static
  address 10.55.0.1
  netmask 255.255.255.248
  • Create /root/usb.sh
#!/bin/bash
cd /sys/kernel/config/usb_gadget/
mkdir -p pi4
cd pi4
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
echo 0xEF > bDeviceClass
echo 0x02 > bDeviceSubClass
echo 0x01 > bDeviceProtocol
mkdir -p strings/0x409
echo "fedcba9876543211" > strings/0x409/serialnumber
echo "Ben Hardill" > strings/0x409/manufacturer
echo "PI4 USB Device" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower
# Add functions here
# see gadget configurations below
# End functions
mkdir -p functions/ecm.usb0
HOST="00:dc:c8:f7:75:14" # "HostPC"
SELF="00:dd:dc:eb:6d:a1" # "BadUSB"
echo $HOST > functions/ecm.usb0/host_addr
echo $SELF > functions/ecm.usb0/dev_addr
ln -s functions/ecm.usb0 configs/c.1/
udevadm settle -t 5 || :
ls /sys/class/udc > UDC
ifup usb0
service dnsmasq restart
  • Add /root/usb.sh to /etc/rc.local before exit 0

With this setup the Pi4 will show up as a ethernet device with an IP address of 10.55.0.1 and will assign the device you plug it into an IP address via DHCP. This means you can just ssh to pi@10.55.0.1 to start using it.

8 thoughts on “Pi4 USB-C Gadget”

  1. You can usually skip the static ip/dhcp steps by using MDNS, upon connection both devices should get a link local address (in the 169 range) and the pi will be available via raspberrypi.local hostname.

    1. Yeah, but Windows doesn’t come with a mDNS resolver so you have no way of finding out what link local IP address anything has picked.

        1. I’m waiting for mdns on windows for years, somehow it just doesn’t seem to happen. Your link got my hopes up, yet unfortunately it just explains how to disable the dysfunctional mdns rudiment that comes with windows, in order to allow Apple Bonjour to work again :/
          I wish we were finally over DHCP, but this is actually a neat setup. Thank you, Ben!

  2. That’s pretty cool, Ben. Is there a way to make the Pi be both an Ethernet gadget and a thumbdrive gadget? Like is there a hub gadget that you can attach other gadgets to?

    1. You can build what is known as a composite device that can have multiple capabilities. The only problem is that when you add a USB Mass Storage option it can not be safely mounted on both the Pi and the Host computer at the same time.

      I have built versions that are ethernet/serial port/CD-ROM before. Where the CD-ROM image holds the docs and the .inf files for windows to get the ethernet adapter working.

  3. Hey, I’m checking the linked article on updating the Raspberry Pi 4 boot EEPROM and I’m seeing this:

    “Network and USB boot

    Support for these additional bootmodes will be added in the future via optional bootloader updates. The current schedule is to release network boot first, then USB boot.”

    Is this incorrect? Do I need to opt in to a beta version of the bootloader to get these features?

    Thanks

    1. No, those features are not what we are taking about here (but will be great when they arrive).

      You need to make sure that you are on the latest version of the release bootloader as that reduces the power usage of the USB stack which means that powering the Pi4 from a laptop less likely to be a problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.