我目前正在尝试使 USB 大容量存储小工具在树莓派零 W 上工作。
我的主要目标是将 Mass_storage 和 mtp 响应器功能实现到一个小工具中,但稍后会实现。
我遵循了这个文档g_mass_storage 与 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 设备控制器绑定到这个小工具。我在互联网上进行了很多搜索,以找到可能有类似问题的人,但我找不到任何东西。
答案1
最后的问题是必须加载模块 dwc2。
所以我dtoverlay=dwc2
在/boot/config.txt
和的dwc2
末尾添加了/etc/modules
在启动时自动加载它。