Skip to content

Instantly share code, notes, and snippets.

@chrisdiana
Last active March 18, 2025 12:48
Show Gist options
  • Save chrisdiana/ce318d9a9093b93d9162fcca9b7143fe to your computer and use it in GitHub Desktop.
Save chrisdiana/ce318d9a9093b93d9162fcca9b7143fe to your computer and use it in GitHub Desktop.
Raspberry Pi USB-C Gadget

Raspberry Pi USB-C Gadget

For use with Mac

  1. Add dtoverlay=dwc2 to the /boot/config.txt
  2. Add modules-load=dwc2 to the end of /boot/firmware/cmdline.txt
  3. If you have not already enabled ssh then create a empty file called ssh in /boot/firmware/
  4. Add libcomposite to /etc/modules
  5. Add denyinterfaces usb0 to /etc/dhcp/dhclient.conf
  6. Install `dnsmasq with sudo apt-get install dnsmasq
  7. Create /etc/dnsmasq.d/usb with following content
interface=usb0
dhcp-range=10.55.0.2,10.55.0.6,255.255.255.248,1h
dhcp-option=3
leasefile-ro
  1. 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
  1. 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
  1. Make /root/usb.sh executable with chmod +x /root/usb.sh
  2. Add /root/usb.sh to /etc/rc.local (create if not exists) before exit 0 With this setup the Pi 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 [email protected] to start using it. Test by stopping wlan0 sudo ip link set wlan0 down

Modified from:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment