I'm currently trying to make USB mass storage gadget work on a raspberry pi zero W.

My main objective is to implement both mass_storage and mtp responder functions into one gadget but it'll come later.

I followed this documentation Equivalent of g_mass_storage with configfs to write the following script which initializes the configuration for USB mass storage gadget through configfs and functionfs.


set -e


case "$1" in
        echo "Creating the USB gadget"
        echo "Loading composite module"
        modprobe libcomposite

        echo "Creating gadget directory g1"
        mkdir -p $GADGET/g1

        cd $GADGET/g1
        if [ $? -ne 0 ]; then
            echo "Error creating usb gadget in configfs"
            exit 1;
            echo "OK"

        echo "Creating Mass Storage interface"
        echo "\tCreating backing file"
        dd if=/dev/zero of=/home/pi/backingFile.img bs=1024 count=1024 > /dev/null 2>&1
        mkdosfs /home/pi/backingFile.img > /dev/null 2>&1
        echo "\tOK"

        echo "\tCreating gadget functionality"
        mkdir functions/mass_storage.0
        echo 1 > functions/mass_storage.0/stall
        echo "/home/pi/backingFile.img" > functions/mass_storage.0/lun.0/file
        echo 1 > functions/mass_storage.0/lun.0/removable
        echo 0 > functions/mass_storage.0/lun.0/cdrom
        mkdir configs/c.1
        mkdir configs/c.1/strings/0x409
        ln -s functions/mass_storage.0 configs/c.1
        echo "\tOK"
        echo "OK"

        echo "Setting Vendor and Product ID's"
        echo $VID > idVendor
        echo $PID > idProduct
        echo "OK"

        echo "Setting English strings"
        mkdir -p strings/0x409
        echo $SERIAL > strings/0x409/serialnumber
        echo $MANUF > strings/0x409/manufacturer
        echo $PRODUCT > strings/0x409/product
        echo "OK"

        echo "Binding USB Device Controller"
        echo `ls /sys/class/udc` > UDC
        echo "OK"
        echo "Stopping the USB gadget"

        cd $GADGET/g1

        if [ $? -ne 0 ]; then
            echo "Error: no configfs gadget found" 
            exit 1;

        echo "Unbinding USB Device Controller"
        echo "" > UDC
        echo "OK"

        echo "Removing Mass Storage interface"
        rm -f configs/c.1/mass_storage.0
        rm -f /home/pi/backingFile.img
        rmdir functions/mass_storage.0
        echo "OK"

        echo "Clearing English strings"
        rmdir strings/0x409
        echo "OK"

        echo "Cleaning up configuration"
        rmdir configs/c.1/strings/0x409
        rmdir configs/c.1
        echo "OK"

        echo "Removing gadget directory"
        cd $GADGET
        rmdir g1
        cd /
        echo "OK"

        echo "Disable composite USB gadgets"
        modprobe -r libcomposite
        echo "OK"
        echo "Usage : $0 {start|stop}"

Everything seems to work until the "Binding USB Device Controller" step. After having configured everything, there is still nothing in /sys/class/udc/.

At boot, the folder /sys/class/udc/ doesn't exist and it appear after I run the script with sudo ./mass-storage-load start but it stay empty. The configfs partition is also mounted at boot.

After I ran the script, three new modules are loaded

pi@raspberrypi:~ $ lsmod
Module                  Size  Used by
usb_f_mass_storage     44899  2 
libcomposite           52156  10 usb_f_mass_storage
udc_core               40853  2 usb_f_mass_storage,libcomposite

However with nothing in /sys/class/udc I don't know which usb device controller I should bind to this gadget. I searched a lot on the internet to find someone that could have a similar issue but I'm not able to find something.


Finally the problem was that the module dwc2 had to be loaded.

So I added dtoverlay=dwc2 to /boot/config.txt and dwc2 at the end of /etc/modules to load it automatically at boot.

| improve this answer | |
  • Which OS? dwc2 gets loaded on Arch Linux without any change to /etc/modules or /etc/modules.load.d. Also, I think you have a typo with the module name. modinfo dwc2 works, modinfo dcw2 does not. – Jonathan Komar Aug 18 '19 at 19:05
  • It was on raspbian but I don't remember which version, certainly on raspbian stretch lite as it's what I'm usually using on rpi zero. I fixed the typo mistake ;) – Arkaik Aug 19 '19 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.