Last active
March 13, 2025 22:16
-
-
Save nv-h/972ece47ba28c07f825f0b272b71d3c6 to your computer and use it in GitHub Desktop.
USB Gadget Ether & Mass Storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment