Usb-Drive

使用 configfs 的 USB 大容量儲存小工具沒有 UDC 顯示

  • August 19, 2019

我目前正在嘗試使 USB 大容量儲存小工具在樹莓派零 W 上工作。

我的主要目標是將 mass_storage 和 mtp 響應器功能實現到一個小工具中,但稍後會出現。

我按照此文件Equivalent of g_mass_storage with configfs編寫了以下腳本,該腳本通過 configfs 和 functionfs 初始化 USB 大容量儲存小工具的配置。

#!/bin/sh

set -e

CONFIGFS="/sys/kernel/config"
GADGET="$CONFIGFS/usb_gadget"
VID="0x0525"
PID="0xa4a2"
SERIAL="0123456789"
MANUF="Myself"
PRODUCT="MyProduct"

case "$1" in
   start)
       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;
       else
           echo "OK"
       fi

       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"
       ;;
   stop)
       echo "Stopping the USB gadget"

       cd $GADGET/g1

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

       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}"
esac

在“綁定 USB 設備控制器”步驟之前,一切似乎都有效。配置完所有內容後,仍然沒有任何內容/sys/class/udc/

在啟動時,該文件夾/sys/class/udc/不存在,它在我執行腳本後出現,sudo ./mass-storage-load start但它保持為空。configfs 分區也在引導時掛載。

執行腳本後,載入了三個新模組

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

但是,/sys/class/udc我不知道應該將哪個 USB 設備控制器綁定到這個小工具。我在網際網路上進行了很多搜尋,以找到可能有類似問題的人,但我找不到任何東西。

最後的問題是必須載入模組 dwc2。

所以我在啟動時添加並dtoverlay=dwc2在末尾自動載入它。/boot/config.txt``dwc2``/etc/modules

引用自:https://unix.stackexchange.com/questions/388304