Skip to content

Instantly share code, notes, and snippets.

@nichtsfrei
Last active October 24, 2022 13:29
Show Gist options
  • Save nichtsfrei/09098dbb9afa4f132159210e3c29f759 to your computer and use it in GitHub Desktop.
Save nichtsfrei/09098dbb9afa4f132159210e3c29f759 to your computer and use it in GitHub Desktop.
sets up manjaro raspberry pi 4 as a usb_gadget
#!/bin/sh
# shamelessly stolen from https://www.hardill.me.uk/wordpress/2019/11/02/pi4-usb-c-gadget/
# main difference is that I use ip to set the device and don't use dnsmasq.
# Instead I setup 'Raspberry PI4 USB Device' within iPad (or whatever you use) manual to
# 10.55.0.2 255.255.255.248
# so that I don't have to deal with dnsmasq at all.
# I also use modprobe in the initialization script so that I don't load the modules when I don't execute usb-network.sh
echo 'dtoverlay=dwc2' >> /boot/config.txt
cat >/usr/local/bin/usb-network.sh <<EOL
#!/bin/bash
# needed to identify as usb_gadget
modprobe libcomposite
modprobe dwc2
cd /sys/kernel/config/usb_gadget/
mkdir -p display-pi
cd display-pi
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
mkdir -p strings/0x409
echo "fedcba9876543210" > strings/0x409/serialnumber
echo "raspberry pi" > strings/0x409/manufacturer
echo "Raspberri 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
mkdir -p functions/ecm.usb0
echo "00:dc:c8:f7:75:15" > functions/ecm.usb0/host_addr
echo "00:dd:dc:eb:6d:a1" > functions/ecm.usb0/dev_addr
ln -s functions/ecm.usb0 configs/c.1/
mkdir -p functions/acm.usb0
ln -s functions/acm.usb0 configs/c.1/
udevadm settle -t 5 || :
ls /sys/class/udc > UDC
ip address add 10.55.0.1/29 broadcast + dev usb0
ip link set dev usb0 up
EOL
chmod +x /usr/local/bin/usb-network.sh
cat > /lib/systemd/system/usbnetwork.service <<EOL
[Unit]
Description=My USB gadget
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/usb-network.sh
[Install]
WantedBy=sysinit.target
EOL
systemctl enable usbnetwork.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment