Skip to content

Instantly share code, notes, and snippets.

@oskar456
Created July 5, 2020 17:23
Show Gist options
  • Save oskar456/4a39f2e3e3ab580bc0bd47d3254a38a6 to your computer and use it in GitHub Desktop.
Save oskar456/4a39f2e3e3ab580bc0bd47d3254a38a6 to your computer and use it in GitHub Desktop.
Raspberry Pi Zero USB gadget mode with ethernet and serial
#!/bin/sh
# Enable gadget mode
echo "dtoverlay=dwc2,dr_mode=peripheral" >> /boot/config.txt
systemctl enable setupgadget.service
# alternatively: ln -s /etc/systemd/system/setupgadget.service /etc/systemd/system/basic.target.wants/setupgadget.service
# Enable getty on the virtual serial line
systemctl enable serial-getty@ttyGS0.service
# alternatively: ln -s /lib/systemd/system/serial-getty@.service /etc/systemd/system/getty.target.wants/serial-getty@ttyGS0.service
[Unit]
Description=Setup USB gadget mode
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/setupgadget.sh
RemainAfterExit=true
[Install]
WantedBy=basic.target
#!/bin/bash
modprobe libcomposite
# Basically a rip-off of Raspberry Pi Zero GPIO expander firmware
# https://github.com/raspberrypi/gpioexpander/blob/master/gpioexpand/board/overlay/etc/init.d/S99gpioext
GADGET=/sys/kernel/config/usb_gadget/g1
SERIAL=`cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2`
if [ -n "$SERIAL" ]; then
MAC1="fa:${SERIAL:6:2}:${SERIAL:8:2}:${SERIAL:10:2}:${SERIAL:12:2}:${SERIAL:14:2}"
MAC2="fe:${SERIAL:6:2}:${SERIAL:8:2}:${SERIAL:10:2}:${SERIAL:12:2}:${SERIAL:14:2}"
else
MAC1="fa:31:43:14:31:43"
MAC2="fe:31:43:14:31:43"
fi
mkdir -p $GADGET
(cd $GADGET
# FIXME: obtain proper USB ID instead of using f055 (FOSS)
echo 0xf055 > idVendor
echo 0xcafe > idProduct
mkdir strings/0x409
echo $SERIAL > strings/0x409/serialnumber
echo "Raspberry Pi" > strings/0x409/manufacturer
echo "Pi Zero - Camera" > strings/0x409/product
mkdir configs/c.1
mkdir configs/c.1/strings/0x409
echo "Config 1" > configs/c.1/strings/0x409/configuration
echo 500 > configs/c.1/MaxPower
mkdir functions/acm.usb0
ln -s functions/acm.usb0 configs/c.1
mkdir functions/ecm.usb0
echo $MAC1 > functions/ecm.usb0/host_addr
echo $MAC2 > functions/ecm.usb0/dev_addr
ln -s functions/ecm.usb0 configs/c.1
# Assuming there is only ever going to be one UDC
ls /sys/class/udc > UDC
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment