linux gadget u盤(Mass storage)

crazy_baoli發表於2021-01-04

 

1. kernel config

Device Drivers  --->
    [*] USB support  --->
        <*> USB Gadget Support
             <*>   USB Gadget functions configurable through configfs
                [*]     Mass storage

CONFIG_USB_CONFIGFS_MASS_STORAGE=y

CONFIG_USB_F_MASS_STORAGE=y

 

The Mass Storage Gadget acts as a USB Mass Storage disk drive. 

As its storage repository it can use a regular file or a block 

device (in much the same way as the "loop" device driver),             

specified as a module parameter or sysfs option.

 

 

2. prepare a disk partition or loop device

2.1 loop device

dd if=/dev/zero of=/virtualfs bs=1024 count=307200 //300M

losetup /dev/loop0 /virtualfs

 

we create loop device on rootfs and choose loop device to test usb speed, since roofs is mounted on DDR and DDR speed is very fast.

 

 

3. start usb-storage

mkdir /etc/configfs
export CONFIGFS_HOME=/etc/configfs
mount none $CONFIGFS_HOME -t configfs

mkdir $CONFIGFS_HOME/usb_gadget/g2
cd $CONFIGFS_HOME/usb_gadget/g2

echo 0x2222 > idVendor
echo 0x1111 > idProduct

mkdir strings/0x409
echo 20200210 > strings/0x409/serialnumber
echo 999 > strings/0x409/manufacturer
echo 888 > strings/0x409/product

mkdir configs/c.1

mkdir configs/c.1/strings/0x409
echo mass_storage > configs/c.1/strings/0x409/configuration
echo 120 > configs/c.1/MaxPowerN

mkdir functions/mass_storage.usb0
echo /dev/loop0 >functions/mass_storage.usb0/lun.0/file

ln -s functions/mass_storage.usb0 configs/c.1

echo 500000.dwc3 > UDC

Note, if use disk partition

echo /dev/mmcblk0 >functions/mass_storage.usb0/lun.0/file

 

 

4. test

4.1 start usb storage

# ./usb-storage.sh

500000+0 records in

500000+0 records out

[   88.768811] Mass Storage Function, version: 2009/09/11

[   88.793798] LUN: removable file: (no medium)

#

# [  143.501920] configfs-gadget gadget: super-speed config #1: c

 

 

4.2 plugin usb device into PC

format it,

 

 

 

 

 

相關文章