Skip to content

Instantly share code, notes, and snippets.

@larsimmisch
Created July 5, 2023 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsimmisch/5cc90ffe13869f63d4edb90ace35214d to your computer and use it in GitHub Desktop.
Save larsimmisch/5cc90ffe13869f63d4edb90ace35214d to your computer and use it in GitHub Desktop.
USB gadget on a Raspberry Pi 4 for iPad
#!/bin/sh
# I wanted to use a Raspberry Pi as a USB gadget for audio and ethernet, but got error messages on the iPad
# when I extended the camera connection kit with a Lightning extender.
#
# I managed to avoid this by making sure that the USB gadget announces itself as self-powered (see configs/c.1/bmAttributes)
# With this, the Pi 4 and a Hifiberry Amp supplies audio out and ethernet.
#
# For audio out, alsaloop or similar must also be running.
# Load libcomposite
modprobe libcomposite
SERIAL=`cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2`
GADGET_DIR="audio+ethernet"
start() {
# Create a gadget called $GAGDET_DIR
cd /sys/kernel/config/usb_gadget/
mkdir -p ${GADGET_DIR}
cd ${GADGET_DIR}
# Configure our gadget details
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 ${SERIAL} > strings/0x409/serialnumber
echo "ibp" > strings/0x409/manufacturer
echo "kuechenradio-usb" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
# 0xc0 is USB_CONFIG_ATT_SELFPOWER
echo 0xc0 > configs/c.1/bmAttributes
echo 0 > configs/c.1/MaxPower
# Ethernet gadget
mkdir -p functions/ecm.usb0
# first byte of address must be even
HOST="32:70:05:18:ff:7a" # "HostPC"
SELF="32:70:05:18:ff:7b" # "Ethernet Gadget"
echo $HOST > functions/ecm.usb0/host_addr
echo $SELF > functions/ecm.usb0/dev_addr
ln -s functions/ecm.usb0 configs/c.1/
# UAC2 (audio) gadget
mkdir -p functions/uac2.usb0
echo "44100,48000" > functions/uac2.usb0/c_srate
echo "2" > functions/uac2.usb0/c_ssize
echo "44100,48000" > functions/uac2.usb0/p_srate
echo "2" > functions/uac2.usb0/p_ssize
ln -s functions/uac2.usb0 configs/c.1
# End functions
ls /sys/class/udc > UDC
brctl addbr br0
brctl addif br0 eth0
brctl addif br0 usb0
}
stop() {
ifconfig br0 down
brctl delif br0 usb0
brctl delif br0 eth0
brctl delbr br0
echo "" > UDC
cd /sys/kernel/config/usb_gadget/${GADGET_DIR}
rm configs/c.1/ecm.usb0
rm configs/c.1/uac2.usb0
rmdir configs/c.1/strings/0x409
rmdir configs/c.1/
rmdir functions/ecm.usb0
rmdir functions/uac2.usb0
rmdir strings/0x409
cd ..
rmdir ${GADGET_DIR}
}
case $1 in
start)
start;;
stop)
stop;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment