Skip to content

Instantly share code, notes, and snippets.

@marcsello
Created March 2, 2026 10:07
Show Gist options
  • Select an option

  • Save marcsello/987c979ff0d9ceafac6513f375fdd6fa to your computer and use it in GitHub Desktop.

Select an option

Save marcsello/987c979ff0d9ceafac6513f375fdd6fa to your computer and use it in GitHub Desktop.
Simple script for testing USB gadget on Linux devices. Tested on F1C100s SoC, should work on Pi Pico as well
#!/bin/bash
# Usage
# ./usb_gadget_setup.sh mass_storage <device> -> set up a mass storage gadget exposing <device> (it can be a block device, or a file). MAKE SURE IT'S NOT MOUNTED!!!
# ./usb_gadget_setup.sh eth -> setup an ethernet gadget
#
# /sys/kernel/config should be already mounted
mkdir /sys/kernel/config/usb_gadget/g1
pushd /sys/kernel/config/usb_gadget/g1
echo 0xabcd > idVendor
echo 0x1234 > idProduct
mkdir strings/0x409
echo 420 > strings/0x409/serialnumber
echo marcsello > strings/0x409/manufacturer
echo turbomeme > strings/0x409/product
mkdir configs/c.1
echo 120 > configs/c.1/MaxPower
FUNC=""
if [[ "$1" == "mass_storage" ]]; then
# https://popovicu.com/posts/make-your-own-usb-storage-embedded-linux/
mkdir functions/mass_storage.0
# mkdir functions/mass_storage.0/lun.0 # should be already created
echo "$2" > functions/mass_storage.0/lun.0/file
FUNC="functions/mass_storage.0"
fi
if [[ "$1" == "eth" ]]; then
# https://developer.ridgerun.com/wiki/index.php/How_to_create_USB_Ethernet_gadget_for_Jetson_through_configfs
mkdir functions/ecm.usb0
FUNC="functions/ecm.usb0"
fi
# bind the config to the function
ln -s $FUNC configs/c.1
echo "musb-hdrc.1.auto" > UDC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment