WhyCan Forum

人过留名,雁过留声,感谢各位朋友不离不弃地支持。 QQ: 516333132 (挖坑网/填坑网) admin@whycan.cn

您尚未登录。

#1 2019-09-18 09:16:54

firstman
会员
注册时间: 2019-04-06
累计积分: 174

找到一篇关于Linux usb gadget 非常使用的说明和使用教程

在线

#2 2019-09-18 09:21:17

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

实现两个独立的U盘:

# mount configfs
mount -t configfs none /sys/kernel/config
# load libcomposite module
modprobe libcomposite
# create a gadget
mkdir /sys/kernel/config/usb_gadget/g1
# cd to its configfs node
cd /sys/kernel/config/usb_gadget/g1
# configure it (vid/pid can be anything if USB Class is used for driver compat)
echo 0xabcd > idVendor
echo 0x1234 > idProduct
# configure its serial/mfg/product
mkdir strings/0x409
echo myserial > strings/0x409/serialnumber
echo mymfg > strings/0x409/manufacturer
echo myproduct > strings/0x409/product
# create configs
mkdir configs/c.1
mkdir configs/c.2
mkdir configs/c.3
# configure them with attributes if needed
echo 120 > configs/c.1/MaxPower
echo 120 > configs/c.2/MaxPower
echo 120 > configs/c.2/MaxPower
# ensure function is loaded
modprobe usb_f_mass_storage
# create the function (name must match a usb_f_<name> module such as 'acm')
mkdir functions/mass_storage.0
# create backing store(s): in this example 2 LUN's 16MB each
dd bs=1M count=16 if=/dev/zero of=/tmp/lun0.img # 16MB
dd bs=1M count=16 if=/dev/zero of=/tmp/lun1.img # 16MB
# associate with partitions
mkdir functions/mass_storage.0/lun.0
echo /tmp/lun0.img > functions/mass_storage.0/lun.0/file
mkdir functions/mass_storage.0/lun.1
echo /tmp/lun1.img > functions/mass_storage.0/lun.1/file
# associate function with config
ln -s functions/mass_storage.0 configs/c.1
# enable gadget by binding it to a UDC from /sys/class/udc
echo 0000:01:00.0 > UDC
# to unbind it: echo "" UDC; sleep 1; rm -rf /sys/kernel/config/usb_gadget/g1

在线

#3 2019-09-18 09:30:21

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

我的理解,  drivers/usb/gadget/function/ 目录下面的驱动程序可以任意组合,
drivers/usb/gadget/legacy/ 这些驱动程序只能选一个,所以这里面有很多组合功能的 gadget驱动。

在线

#4 2019-09-18 10:43:33

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

https://www.spinics.net/lists/linux-usb/msg155033.html

mount -t debugfs debugfs /mnt/
echo "device" > /mnt/488d0000.usb/mode
modprobe -v configfs
modprobe -v libcomposite
sleep 2
cd /sys/kernel/config/usb_gadget
mkdir g1
cd g1
echo "0x1d6b" > idVendor
echo "0x0104" > idProduct
mkdir strings/0x409
echo "0123456789" > strings/0x409/serialnumber
echo "TI Inc." > strings/0x409/manufacturer
echo "MS+UVC gadget" > strings/0x409/product
modprobe -v usb_f_mass_storage
mkdir functions/mass_storage.ms0
# create backing store(s): in this example 2 LUN's 16MB each
dd bs=1M count=16 if=/dev/zero of=/tmp/lun0.img # 16MB
chmod 777 /tmp/lun0.img
modprobe -v usb_f_uvc
mkdir functions/uvc.usb0
mkdir configs/c.1
mkdir configs/c.1/strings/0x409
echo "mass-storage + uvc" > configs/c.1/strings/0x409/configuration
echo 120 > configs/c.1/MaxPower
mkdir functions/uvc.usb0/control/header/h
cd functions/uvc.usb0/control/
ln -s header/h class/fs
ln -s header/h class/ss
cd ../../../
mkdir -p functions/uvc.usb0/streaming/uncompressed/u/360p
cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/360p/dwFrameInterval
666666
1000000
5000000
EOF
mkdir functions/uvc.usb0/streaming/header/h
cd functions/uvc.usb0/streaming/header/h
ln -s ../../uncompressed/u
cd ../../class/fs
ln -s ../../header/h
cd ../../class/hs
ln -s ../../header/h
cd ../../../control
cd ../../../
echo /tmp/lun0.img > functions/mass_storage.ms0/lun.0/file
echo 1 > functions/mass_storage.ms0/lun.0/removable
ln -s functions/mass_storage.ms0 configs/c.1/
ln -s functions/uvc.usb0 configs/c.1/
echo "488d0000.usb" > UDC

mass_storage 和 uvc 摄像头的复合设备

在线

#5 2019-09-18 11:07:24

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

在线

#6 2019-09-18 17:20:26

Jmhh247
会员
注册时间: 2018-12-21
累计积分: 141

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

多谢分享,准备试试UVC

离线

#7 2019-09-18 17:35:47

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

Jmhh247 说:

多谢分享,准备试试UVC

不用谢, 一起学习.

https://www.kernel.org/doc/Documentation/usb/gadget-testing.txt

  # mkdir functions/uvc.usb0/control/header/h
  # cd functions/uvc.usb0/control/
  # ln -s header/h class/fs
  # ln -s header/h class/ss
  # mkdir -p functions/uvc.usb0/streaming/uncompressed/u/360p
  # cat <<EOF > functions/uvc.usb0/streaming/uncompressed/u/360p/dwFrameInterval
  666666
  1000000
  5000000
  EOF
  # cd $GADGET_CONFIGFS_ROOT
  # mkdir functions/uvc.usb0/streaming/header/h
  # cd functions/uvc.usb0/streaming/header/h
  # ln -s ../../uncompressed/u
  # cd ../../class/fs
  # ln -s ../../header/h
  # cd ../../class/hs
  # ln -s ../../header/h
  # cd ../../class/ss
  # ln -s ../../header/h


Testing the UVC function
------------------------

device: run the gadget, modprobe vivid::

  # uvc-gadget -u /dev/video<uvc video node #> -v /dev/video<vivid video node #>

where uvc-gadget is this program:
	http://git.ideasonboard.org/uvc-gadget.git

with these patches:

	http://www.spinics.net/lists/linux-usb/msg99220.html

host::

	luvcview -f yuv

把板载摄像头转为USB摄像头.

最近编辑记录 firstman (2019-09-18 17:36:22)

在线

#8 2019-09-18 17:44:07

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

xawtv5.png

Linux摄像头驱动学习第一篇,对虚拟视频驱动Virtual Video Driver(vivid)进行测试、分析、编写。

https://hceng.cn/2018/03/08/Linux摄像头驱动1——vivid/

在线

#9 2019-09-18 23:31:18

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

在线

#10 2019-09-19 11:37:28

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

2019-09-19_113155.png

linux usb gadget驱动简直太好用, 随便配置一下就是一个复合USB设备, 这是我的 S5PV210 开发板。

https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt

mount -t configfs none /sys/kernel/config

modprobe libcomposite
modprobe usb_f_serial
modprobe usb_f_fs
modprobe usb_f_hid

modprobe usb_f_acm
sleep 2
cd /sys/kernel/config/usb_gadget/

if [ ! -d /sys/kernel/config/usb_gadget/g1 ]; then
	mkdir g1
	cd g1
	echo 0x8087 > idVendor
	echo 0x101e > idProduct
	mkdir strings/0x409
	echo 012345678 > strings/0x409/serialnumber
	echo Intel > strings/0x409/manufacturer
	echo IoTplatform > strings/0x409/product
	mkdir functions/acm.GS0
	mkdir functions/ffs.adb
	mkdir functions/hid.usb0

	
	# setup hid parameters for the keyboard function
	echo 1 > functions/hid.usb0/protocol
	echo 1 > functions/hid.usb0/subclass
	echo 8 > functions/hid.usb0/report_length
	echo -ne "\x05\x01\x09\x06\xA1\x01\x05\x07\x19\xE0\x29\xE7\x15\x00\x25\x01\x75\x01\x95\x08\x81\x02\x95\x01\x75\x08\x81\x03\x95\x05\x75\x01\x05\x08\x19\x01\x29\x05\x91\x02\x95\x01\x75\x03\x91\x03\x95\x06\x75\x08\x15\x00\x25\x65\x05\x07\x19\x00\x29\x65\x81\x00\xC0" > functions/hid.usb0/report_desc

	
	mkdir configs/c.1
	mkdir configs/c.1/strings/0x409
	echo "adb+cdc" > configs/c.1/strings/0x409/configuration
	echo 120 > configs/c.1/MaxPower
	ln -s functions/acm.GS0 configs/c.1
	ln -s functions/ffs.adb/ configs/c.1
	ln -s functions/hid.usb0 configs/c.1/
	

	
	mkdir /dev/usb-ffs
	mkdir /dev/usb-ffs/adb
	mount -t functionfs adb /dev/usb-ffs/adb
	adbd &

fi

echo s3c-hsotg > UDC

在线

#11 2019-09-19 14:00:39

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

接楼上, 测试USB输入设备命令:

echo -ne "\0\0\x4\0\0\0\0\0" > /dev/hidg0;echo -ne "\0\0\x0\0\0\0\0\0" > /dev/hidg0

也可以测试 adb:

adb shell

在线

#12 2019-09-19 14:11:23

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

usb gadget configfs 验证: http://wowothink.com/a64c6a27/

按照上一篇文章对gadget_configfs.txt的翻译,以imx8qxp mek的板子做为验证,配置为mass storage进行验证。

内核打开对应的宏
1、必须打开CONFIG_CONFIGFS_FS和CONFIG_USB_LIBCOMPOSITE的宏,前者为用户空间提供访问配置内核驱动的configfs文件系统,后者提供usb gadget composite框架;
2、必须打开UDC(USB Device Controller)的配置,这个是配置硬件控制器。我这里使用CONFIG_USB_CHIPIDEA和CONFIG_USB_CHIPIDEA_UDC;
3、如果使用mass storage功能,需要打开USB_CONFIGFS_MASS_STORAGE和USB_F_MASS_STORAGE,对应于usb_f_mass_storage.ko驱动。

配置步骤
挂载configfs:mount -t configfs none /config/,在这之后,/configfs/目录下就会生成usb_gadget/目录;
创建gadget:mkdir usb_gadget/g1,创建g1/目录之后,该目录下会生成很多配置目录,这里的g1表示gadget 1,一个UDC对应一个gadget,如果你的SOC上有多个gadget,可以创建多个gx目录。

# ll usb_gadget/g1/
total 0
-rw-r--r-- 1 root root 4096 1999-11-30 00:02 UDC
-rw-r--r-- 1 root root 4096 1999-11-30 00:02 bDeviceClass
-rw-r--r-- 1 root root 4096 1999-11-30 00:02 bDeviceProtocol
-rw-r--r-- 1 root root 4096 1999-11-30 00:02 bDeviceSubClass
-rw-r--r-- 1 root root 4096 1999-11-30 00:02 bMaxPacketSize0
-rw-r--r-- 1 root root 4096 1999-11-30 00:02 bcdDevice
-rw-r--r-- 1 root root 4096 1999-11-30 00:02 bcdUSB
drwxr-xr-x 2 root root    0 1999-11-30 00:02 configs
drwxr-xr-x 2 root root    0 1999-11-30 00:02 functions
-rw-r--r-- 1 root root 4096 1999-11-30 00:02 idProduct
-rw-r--r-- 1 root root 4096 1999-11-30 00:02 idVendor
drwxr-xr-x 2 root root    0 1999-11-30 00:02 os_desc
drwxr-xr-x 2 root root    0 1999-11-30 00:02 strings

配置PID和VID

# echo 0x18d1 > usb_gadget/g1/idVendor
# echo 0x4ee2 > usb_gadget/g1/idProduct

创建并配置string字目录

# mkdir usb_gadget/g1/strings/0x409
# echo "123456789" > usb_gadget/g1/strings/0x409/serialnumber
# echo "xxx" > usb_gadget/g1/strings/0x409/manufacturer
# echo "xxx_product" > usb_gadget/g1/strings/0x409/product

创建configuration和字符串

# mkdir usb_gadget/g1/configs/c.1
# mkdir usb_gadget/g1/configs/c.1/strings/0x409
# echo "mass_storage" > usb_gadget/g1/configs/c.1/strings/0x409/configuration

创建functions

# mkdir usb_gadget/g1/functions/mass_storage.0
[  108.582976]  [1:          mkdir: 1976] Mass Storage Function, version: 2009/09/11
[  108.592679]  [1:          mkdir: 1976] LUN: removable file: (no medium)

这里的mass_storage的名字不能随便起,需要根据insmod的function驱动usb_f_mass_storage.ko来决定。否则会出现如下错误:

1|@android:/config # mkdir usb_gadget/g1/functions/msg.0
mkdir: 'usb_gadget/g1/functions/msg.0': No such file or directory

将functions和configuration关联起来
# ln -s usb_gadget/g1/functions/mass_storage.0 usb_gadget/g1/configs/c.1
上述配置之后,得到的结果为:

127|@android:/config # find .
.
./usb_gadget
./usb_gadget/g1
./usb_gadget/g1/os_desc
./usb_gadget/g1/os_desc/qw_sign
./usb_gadget/g1/os_desc/b_vendor_code
./usb_gadget/g1/os_desc/use
./usb_gadget/g1/strings
./usb_gadget/g1/strings/0x409
./usb_gadget/g1/strings/0x409/serialnumber
./usb_gadget/g1/strings/0x409/product
./usb_gadget/g1/strings/0x409/manufacturer
./usb_gadget/g1/configs
./usb_gadget/g1/configs/c.1
./usb_gadget/g1/configs/c.1/mass_storage.0
./usb_gadget/g1/configs/c.1/strings
./usb_gadget/g1/configs/c.1/strings/0x409
./usb_gadget/g1/configs/c.1/strings/0x409/configuration
./usb_gadget/g1/configs/c.1/bmAttributes
./usb_gadget/g1/configs/c.1/MaxPower
./usb_gadget/g1/functions
./usb_gadget/g1/functions/mass_storage.0
./usb_gadget/g1/functions/mass_storage.0/lun.0
./usb_gadget/g1/functions/mass_storage.0/lun.0/inquiry_string
./usb_gadget/g1/functions/mass_storage.0/lun.0/nofua
./usb_gadget/g1/functions/mass_storage.0/lun.0/cdrom
./usb_gadget/g1/functions/mass_storage.0/lun.0/removable
./usb_gadget/g1/functions/mass_storage.0/lun.0/ro
./usb_gadget/g1/functions/mass_storage.0/lun.0/file
./usb_gadget/g1/functions/mass_storage.0/stall
./usb_gadget/g1/UDC
./usb_gadget/g1/bcdUSB
./usb_gadget/g1/bcdDevice
./usb_gadget/g1/idProduct
./usb_gadget/g1/idVendor
./usb_gadget/g1/bMaxPacketSize0
./usb_gadget/g1/bDeviceProtocol
./usb_gadget/g1/bDeviceSubClass
./usb_gadget/g1/bDeviceClass

查看当前的UDC
可见当前的板子上有两个UDC,ci_hdrc.0和gadget-cdns3。

130|@android:/config # ll /sys/class/udc/
total 0
lrwxrwxrwx 1 root root 0 1999-11-30 00:09 ci_hdrc.0 -> ../../devices/platform/5b0d0000.usb/ci_hdrc.0/udc/ci_hdrc.0
lrwxrwxrwx 1 root root 0 1999-11-30 00:09 gadget-cdns3 -> ../../devices/platform/5b110000.usb3/gadget-cdns3/udc/gadget-cdns3

绑定到UDC,使能gadget
将UDC切换至device模式,这里使用OTG ID pin来实现切换。

# echo ci_hdrc.0 > usb_gadget/g1/UDC
# echo 1 > /sys/class/gpio/gpio358/value

验证结果
将配置好的开发板与ubuntu连接,从ubuntu上的串口可以看到如下信息,表示已将开发板配置成一个mass storage的设备了。

[95705.390004] usb 1-11: USB disconnect, device number 28
[95708.606717] usb 1-11: new high-speed USB device number 29 using xhci_hcd
[95708.756934] usb 1-11: New USB device found, idVendor=18d1, idProduct=4ee2
[95708.756941] usb 1-11: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[95708.756946] usb 1-11: Product: xxx_product
[95708.756950] usb 1-11: Manufacturer: xxx
[95708.756953] usb 1-11: SerialNumber: 123456789
[95708.766595] usb-storage 1-11:1.0: USB Mass Storage device detected
[95708.766798] scsi host6: usb-storage 1-11:1.0

当然,上述的结果是ubuntu只识别到一个mass storage的设备,但是没有识别看分区表,所以没有看到相应的磁盘信息。想要看到磁盘信息,可以更进一步的设置mass_storage的backen file属性。

定制分区
在上面创建functions步骤之后,执行以下的命令,为mass storage创建2个分区,分区的backing file是开发板上的/dev/block/mmcblk0p1和/dev/block/mmcblk0p2,也就是开发板上的system分区和data分区。

mkdir usb_gadget/g1/functions/mass_storage.0/partition.2
mkdir usb_gadget/g1/functions/mass_storage.0/lun.1
echo /dev/block/mmcblk0p1 > usb_gadget/g1/functions/mass_storage.0/lun.0/file
echo /dev/block/mmcblk0p2 > usb_gadget/g1/functions/mass_storage.0/lun.1/file

所有配置完成之后,插入到ubuntu后,可以看到能识别去新的分区信息:

130 victor@victor-HP:/mnt⟫ sudo dmesg -c
[40873.009026] EXT4-fs (sdc): mounted filesystem with ordered data mode. Opts: (null)
[41094.127318] usb 1-11: USB disconnect, device number 23
[41094.128072] print_req_error: I/O error, dev sdc, sector 0
[41094.131049] sd 6:0:0:0: [sdc] Synchronizing SCSI cache
[41094.131118] sd 6:0:0:0: [sdc] Synchronize Cache(10) failed: Result: hostbyte=DID_NO_CONNECT driverbyte=DRIVER_OK
[41466.480635] usb 1-11: new high-speed USB device number 24 using xhci_hcd
[41466.630904] usb 1-11: New USB device found, idVendor=18d1, idProduct=4ee2
[41466.630911] usb 1-11: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[41466.630915] usb 1-11: Product: xxx_product
[41466.630919] usb 1-11: Manufacturer: xxx
[41466.630923] usb 1-11: SerialNumber: 123456789
[41466.640789] usb-storage 1-11:1.0: USB Mass Storage device detected
[41466.641062] scsi host7: usb-storage 1-11:1.0
[41467.645699] scsi 7:0:0:0: Direct-Access     Linux    File-Stor Gadget 0414 PQ: 0 ANSI: 2
[41467.646291] scsi 7:0:0:1: Direct-Access     Linux    File-Stor Gadget 0414 PQ: 0 ANSI: 2
[41467.646826] scsi 7:0:0:2: Direct-Access     Linux    File-Stor Gadget 0414 PQ: 0 ANSI: 2
[41467.647387] sd 7:0:0:0: Attached scsi generic sg2 type 0
[41467.647764] sd 7:0:0:1: Attached scsi generic sg3 type 0
[41467.648144] sd 7:0:0:0: Power-on or device reset occurred
[41467.648173] sd 7:0:0:2: Attached scsi generic sg4 type 0
[41467.649026] sd 7:0:0:1: Power-on or device reset occurred
[41467.649555] sd 7:0:0:2: Power-on or device reset occurred
[41467.650006] sd 7:0:0:0: [sdd] 1048576 512-byte logical blocks: (537 MB/512 MiB)
[41467.650766] sd 7:0:0:1: [sde] 524288 512-byte logical blocks: (268 MB/256 MiB)
[41467.651726] sd 7:0:0:0: [sdd] Write Protect is off
[41467.651731] sd 7:0:0:0: [sdd] Mode Sense: 0f 00 00 00
[41467.651929] sd 7:0:0:1: [sde] Write Protect is off
[41467.651933] sd 7:0:0:1: [sde] Mode Sense: 0f 00 00 00
[41467.652327] sd 7:0:0:2: [sdf] Attached SCSI removable disk
[41467.652581] sd 7:0:0:0: [sdd] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[41467.652837] sd 7:0:0:1: [sde] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[41467.673305] sd 7:0:0:1: [sde] Attached SCSI removable disk
[41467.674915] sd 7:0:0:0: [sdd] Attached SCSI removable disk

参考
上述的配置过程太繁琐了,在Android上,基于configfs有专门的init.rc在启动过程中去配置对应的configuration和function,比如说init.usb.rc,里面包含了一系列gadget的配置方法。在后面的文章中,将详细讲述每条命令背后的实现原理。

on boot
    write /config/usb_gadget/g1/strings/0x409/serialnumber ${ro.serialno}
    write /config/usb_gadget/g1/strings/0x409/manufacturer ${ro.product.manufacturer}
    write /config/usb_gadget/g1/strings/0x409/product ${ro.product.model}

    setprop sys.usb.configfs 1

    # OS DESCRIPTORS
    #===============
    # OS STRING
    #----------
    write /config/usb_gadget/g1/os_desc/b_vendor_code 1
    write /config/usb_gadget/g1/os_desc/qw_sign "MSFT100"
    # use os desc or not is up to each usb functions respectively
    # write /config/usb_gadget/g1/os_desc/use 1

    # MAKE b.1 THE ONE ASSOCIATED WITH OS DESCRIPTORS
    #------------------------------------------------
    symlink /config/usb_gadget/g1/configs/b.1 /config/usb_gadget/g1/os_desc/b1

    # ci_hdrc.0 is the fixed UDC name
    setprop sys.usb.controller ci_hdrc.0

on fs
    mkdir /dev/usb-ffs 0770 shell shell
    mkdir /dev/usb-ffs/adb 0770 shell shell
    # mount the configfs on /config
    mount configfs none /config mode=0755
    mkdir /config/usb_gadget/g1
    mkdir /config/usb_gadget/g1/configs/b.1
    # mkdir for functions needed
    # this will call each gadget's alloc_inst()
    mkdir /config/usb_gadget/g1/functions/ffs.adb
    mkdir /config/usb_gadget/g1/functions/mtp.gs0
    mkdir /config/usb_gadget/g1/functions/ptp.gs1
    mkdir /config/usb_gadget/g1/functions/accessory.gs2
    mkdir /config/usb_gadget/g1/functions/audio_source.gs3
    mkdir /config/usb_gadget/g1/functions/rndis.gs4
    mkdir /config/usb_gadget/g1/functions/midi.gs5
    mkdir /config/usb_gadget/g1/strings/0x409
    mkdir /config/usb_gadget/g1/configs/b.1/strings/0x409
    # The mount of functionfs for adb must be put AFTER the mkdir for functions in configfs

on property:sys.usb.config=none && property:sys.usb.configfs=1
    write /config/usb_gadget/g1/os_desc/use 0

on property:sys.usb.ffs.ready=1 && property:sys.usb.config=adb && property:sys.usb.configfs=1
    write /config/usb_gadget/g1/idProduct 0x4ee7
    write /config/usb_gadget/g1/idVendor 0x18d1

on property:sys.usb.config=mtp && property:sys.usb.configfs=1
    write /config/usb_gadget/g1/functions/mtp.gs0/os_desc/interface.MTP/compatible_id "MTP"
    write /config/usb_gadget/g1/os_desc/use 1
    write /config/usb_gadget/g1/idProduct 0x4ee1
    write /config/usb_gadget/g1/idVendor 0x18d1

在线

#13 2019-09-19 15:00:50

firstman
会员
注册时间: 2019-04-06
累计积分: 174

Re: 找到一篇关于Linux usb gadget 非常使用的说明和使用教程

试了一下, 这个挂载TF卡成U盘, 卸载U盘挺好用的: https://gist.github.com/nv-h/972ece47ba28c07f825f0b272b71d3c6

usb_config_multi.sh

#!/bin/sh

# https:/github.com/torvalds/linux/blob/master/Documentation/usb/gadget_configfs.txt
# http:/irq5.io/2016/12/22/raspberry-pi-zero-as-multiple-usb-gadgets/
# 上記2つを参考に同時に二つのガジェットを有効化する。
#   1. mass storageで、SDカードをUSBメモリとして見せる
#   2. rndisで、USBをetherデバイスとして見せる

# ドライバを依存関係含めてロード
modprobe usb_f_rndis
modprobe usb_f_mass_storage

# configfsをマウント
mount -t configfs none /sys/kernel/config

# usb_gadgetの下に任意のディレクトリを生成する
g=/sys/kernel/config/usb_gadget/multi
mkdir ${g}

# USBの各種設定(VID/PIDは必須)
echo "64"     > ${g}/bMaxPacketSize0
echo "0x200"  > ${g}/bcdUSB    # USB2.0
echo "0x100"  > ${g}/bcdDevice # 適当
echo "0x03FD" > ${g}/idVendor  # Xilinx
echo "0x0104" > ${g}/idProduct # Multifunction Composite Gadget

# 複数functionのcomposite USB向けの設定
# refer: https://msdn.microsoft.com/en-us/library/windows/hardware/ff540054.aspx
echo "0xEF"   > ${g}/bDeviceClass
echo "0x02"   > ${g}/bDeviceSubClass
echo "0x01"   > ${g}/bDeviceProtocol

# functionsに登録
mkdir ${g}/functions/rndis.rn0
mkdir ${g}/functions/mass_storage.ms0

# rndis固有の設定(設定しないとランダムなmacアドレスを生成する)
# echo "${dev_mac}"  > ${g}/functions/rndis.rn0/dev_addr
# echo "${host_mac}" > ${g}/functions/rndis.rn0/host_addr

# mass storage固有の設定
# fileにストレージとして見せるデバイスを指定する
echo /dev/mmcblk0p1 > ${g}/functions/mass_storage.ms0/lun.0/file
echo 1              > ${g}/functions/mass_storage.ms0/lun.0/removable

# functionとconfigを関連付け
mkdir ${g}/configs/c.1
ln -s ${g}/functions/rndis.rn0        ${g}/configs/c.1/
ln -s ${g}/functions/mass_storage.ms0 ${g}/configs/c.1/

# rndisをwindowsで見えるようにするための設定
echo "1"       > ${g}/os_desc/use
echo "0xcd"    > ${g}/os_desc/b_vendor_code
echo "MSFT100" > ${g}/os_desc/qw_sign
echo "RNDIS"   > ${g}/functions/rndis.rn0/os_desc/interface.rndis/compatible_id
echo "5162001" > ${g}/functions/rndis.rn0/os_desc/interface.rndis/sub_compatible_id
ln -s ${g}/configs/c.1 ${g}/os_desc

# デバイス有効化
echo "ci_hdrc.0" > ${g}/UDC

usb_remove_multi.sh

#!/bin/sh

# https://github.com/torvalds/linux/blob/master/Documentation/usb/gadget_configfs.txt
# http://irq5.io/2016/12/22/raspberry-pi-zero-as-multiple-usb-gadgets/
# 上記を参考にusb_config_multi.shで追加したガジェット2つを無効化する。

# ガジェットを無効化
g=/sys/kernel/config/usb_gadget/multi
echo "" > ${g}/UDC

# functionsのsimlink先を削除
rm ${g}/os_desc/c.1
rm ${g}/configs/c.1/rndis.rn0
rm ${g}/configs/c.1/mass_storage.ms0

# Configurationsを削除
rmdir ${g}/configs/c.1/

# functionsのsimlink元を削除
rmdir ${g}/functions/rndis.rn0
rmdir ${g}/functions/mass_storage.ms0

# gadget本体を削除
rmdir ${g}

# ドライバを依存関係含めてアンロード
modprobe -r usb_f_rndis
modprobe -r usb_f_mass_storage

# configfsをアンマウント
umount /sys/kernel/config

挂载好用, 卸载也是干干净净的。

在线

页脚