本帖最后由 zhouruixi 于 2020-1-12 17:11 编辑
我家云的usb2.0接口支持OTG,我想在开机后把usb2.0接口设置为gadget网卡,然后使用双公头的usb线连接到台式机上,实现台式机和我家云之间通过usb实现网络互联。即达到跟手机的USB网络共享一样效果。
20200112更新:自己整理修改了一下脚本,加了中文注释,使之更好用。利用systemd开机自动创建gadget,并启用串口控制台(debian 10默认不允许root登录)。
######################################################################################################################################
基础知识USB的三种模式(dr_mode) - peripheral
- otg(默认为device)
- host
内核要求修改DeviceTree中USB接口的模式 1、将dtb转为dts,修改usb@ff580000下面的dr_mode为peripheral或者otg - dtc -I dtb rk3328-rock64.dtb -O dts -o rk3328-rock64.dts
复制代码- usb@ff580000 {
- compatible = "rockchip,rk3328-usb\0rockchip,rk3066-usb\0snps,dwc2";
- reg = < 0x00 0xff580000 0x00 0x40000 >;
- interrupts = < 0x00 0x17 0x04 >;
- clocks = < 0x02 0x14d >;
- clock-names = "otg";
- dr_mode = "peripheral";
- g-np-tx-fifo-size = < 0x10 >;
- g-rx-fifo-size = < 0x118 >;
- g-tx-fifo-size = < 0x100 0x80 0x80 0x40 0x20 0x10 >;
- g-use-dma;
- phys = < 0x53 >;
- phy-names = "usb2-phy";
- status = "okay";
- phandle = < 0x9a >;
- };
复制代码 2、重新将dts编译为dtb
- dtc -I dts rk3328-rock64.dts -O dtb -o rk3328-rock64.dtb
复制代码 3、开机后检查是否存在/sys/class/udc/ff580000.usb
- # ls /sys/class/udc/ff580000.usb -l
- /sys/class/udc/ff580000.usb -> ../../devices/platform/ff580000.usb/udc/ff580000.usb
复制代码 方法一:使用g_ether等模块(将对应的module名称加入/etc/modules中即可实现开机自动启动)1、Win10自动安装驱动,但是显示只有百兆
- modprobe g_ether idVendor=0x15ec idProduct=0xd041
复制代码 2、默认win10识别为串口,需手动安装驱动。3、大容量存储 - modprobe g_mass_storage file=/piusb.bin stall=0
复制代码 4、串口Modulesincluded
- Serial (g_serial)
- Ethernet (g_ether)
- Mass storage (g_mass_storage)
- MIDI (g_midi)
- Audio (g_audio)
- Keyboard/Mouse (g_hid)
- Mass storage and Serial (g_acm_ms)
- Ethernet and Serial (g_cdc)
- Multi (g_multi) - Allows you to configure 2 from Ethernet, Mass storage and Serial
In addition to the above modules, a few other (less useful) modules are included. - Webcam (g_webcam)
- Printer (g_printer)
- Gadget tester (g_zero)
来自 <https://gist.github.com/gbaman/50b6cca61dd1c3f88f41>
方法二:使用libcomposite和configfs执行modprobe libcomposite,查看是否出现/sys/kernel/config/usb_gadget/
开启串口+USB网卡+大容量存储(U盘模式) - #!/bin/bash
- # 加载 libcomposite 模块
- if [ ! -d /sys/kernel/config/usb_gadget ]; then
- modprobe libcomposite
- fi
- # 若存在 gadget g1 则先删除
- if [ -d /sys/kernel/config/usb_gadget/g1 ]; then
- echo ">>>>>>>>>>>>g1 is exist! delete it!"
- systemctl stop getty@ttyGS0.service
- cd /sys/kernel/config/usb_gadget/g1
- echo '' > UDC
- #remove all links
- find . -type l -exec rm -v {} \;
- #rmdir configs/c.1/strings/0x409
- find configs -name 'strings' -exec rmdir -v {}/0x409 \;
- #rmdir configs/c.1
- ls -d configs/* | xargs rmdir -v
- #rmdir strings/0x409
- ls -d strings/* | xargs rmdir -v
- #rmdir functions/hid.usb0
- ls -d functions/* | xargs rmdir -v
- cd ..
- rmdir -v g1
- modprobe -r libcomposite > /dev/null 2>&1
- echo ">>>>>>>>>>>>>Delete success, Run script again to create the gadget!!"
- exit 0
- fi
-
- # 定义一些变量
- HOST="48:6f:73:74:50:43"
- RNDIS="42:61:64:55:53:42"
- # 若指定块设备不存在则创建一个测试用镜像
- MASS_FILE=/root/usbdisk.img #用户直接修改为要使用的块设备路径即可
- if [ ! -e $MASS_FILE ] ; then
- echo "Create $MASS_FILE format with vfat for Mass Storage......"
- dd if=/dev/zero of=$MASS_FILE bs=1M count=8
- mkfs.vfat $MASS_FILE
- fi
-
- ###############开始创建gadget################
- echo ">>>>>>>>>>>>>Starting create gadget......"
- cd /sys/kernel/config/usb_gadget/
- mkdir g1
- cd g1
- # 可以让Windows 10 自动安装RNDIS驱动
- #ID_VENDOR="0x1d6b"
- #ID_PRODUCT="0x0104"
- echo 0x1d6b > idVendor
- echo 0x0104 > idProduct
- echo 0x3066 > bcdDevice
- echo 0x0200 > bcdUSB
- # echo 0x02 > bDeviceClass
- echo 0x00 > bDeviceClass
- echo 0x00 > bDeviceSubClass
- mkdir -p strings/0x409
- echo `cat /proc/device-tree/serial-number` > strings/0x409/serialnumber
- echo `uname -r` > strings/0x409/manufacturer
- echo `hostname -s` > strings/0x409/product
- #-----------------------------------
- # 创建RNDIS functions
- echo ">>>>>>>>Create RNDIS ......"
- mkdir -p functions/rndis.usb0
- echo $RNDIS > functions/rndis.usb0/dev_addr
- echo $HOST > functions/rndis.usb0/host_addr
- echo "RNDIS" > functions/rndis.usb0/os_desc/interface.rndis/compatible_id
- echo "5162001" > functions/rndis.usb0/os_desc/interface.rndis/sub_compatible_id
- # 创建串口function
- echo ">>>>>>>>Create serial ......"
- mkdir -p functions/acm.gs0
- # 创建mass_storage function
- echo ">>>>>>>>Create Mass_Storage ......"
- mkdir -p functions/mass_storage.usb0
- echo 1 > functions/mass_storage.usb0/stall
- echo 0 > functions/mass_storage.usb0/lun.0/cdrom
- echo 0 > functions/mass_storage.usb0/lun.0/ro
- echo 0 > functions/mass_storage.usb0/lun.0/nofua
- echo 1 > functions/mass_storage.usb0/lun.0/removable
- echo $MASS_FILE > functions/mass_storage.usb0/lun.0/file
- # 创建config1
- echo ">>>>>>>>>>>Create config1: ACM + Mass_Storage + RNDIS ......"
- mkdir -p configs/c.1/strings/0x409
- echo "0x80" > configs/c.1/bmAttributes
- echo 250 > configs/c.1/MaxPower
- echo "config1: ACM + Mass_Storage + RNDIS" > configs/c.1/strings/0x409/configuration
- # 将对应function软连到c.1下面, 即应用该function
- ln -s functions/rndis.usb0 configs/c.1/
- ln -s functions/acm.gs0 configs/c.1/
- ln -s functions/mass_storage.usb0 configs/c.1/
- ## 开启os_desc // 有必要吗?
- echo "1" > os_desc/use
- echo "0xcd" > os_desc/b_vendor_code
- echo "MSFT100" > os_desc/qw_sign
- # 将对应的config软连到os_desc下即可
- ln -s configs/c.1 os_desc/
- # 将gadget绑定到USB接
- echo ">>>>>>>>>>>Bind config1 to $(ls /sys/class/udc)"
- ls /sys/class/udc > UDC
- echo "All is done, Run script again to delete the gadget!"
复制代码
参考: https://gist.github.com/Gadgetoid/c52ee2e04f1cd1c0854c3e77360011e2 https://github.com/ckuethe/usbarmory/wiki/USB-Gadgets https://github.com/dpavlin/linux-usb-otg
利用systemd让其开机自动启动 | # cat /etc/systemd/system/usb-gadget.service [Unit] Description=USB gadeget After=sys-kernel-config.mount Before=network-pre.target
[Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/local/sbin/gadget_start.sh ExecStop=/usr/local/sbin/gadget_stop.sh
[Install] WantedBy=multi-user.target | # systemctl enable usb-gadget Created symlink /etc/systemd/system/multi-user.target.wants/usb-gadget.service → /etc/systemd/system/usb-gadget.service. | #启用串口控制台 Created symlink /etc/systemd/system/getty.target.wants/getty@ttyGS0.service → /lib/systemd/system/getty@.service. | |
|