OTG做从机,实现使用RNIDS功能访问网络,实现模拟成U盘PC可访问。
最后实现OTG的软件主从切换。
一、RNDIS
设备树的otg模式先设置为:peripheral
&usb_otg {
dr_mode = "peripheral"; /* otg host peripheral */
status = "okay";
};
配置设备树
Device Drivers --->
[*] USB support --->
<*> Inventra Highspeed Dual Role Controller
MUSB Mode Selection (Dual Role mode) ---> (这里要配置为Dual Role mode)
*** Platform Glue Layer ***
<*> Allwinner (sunxi)
*** MUSB DMA mode ***
[ ] Disable DMA (always use PIO)
USB Physical Layer drivers --->
<*> NOP USB Transceiver Driver
<*> USB Gadget Support --->
<*> USB Gadget functions configurable through configfs
[*] Ethernet Control Model (CDC ECM)
[*] Ethernet Control Model (CDC ECM) subset
[*] RNDIS
[*] Ethernet Emulation Model (EEM)
[*] Function filesystem (FunctionFS)
USB Gadget precomposed configurations --->
<*> Ethernet Gadget (with CDC Ethernet support)
[*] RNDIS support
<*> CDC Composite Device (Ethernet and ACM)
编译之后得到:
g_ether.ko
加载驱动:
root@wang-virtual-machine:~# insmod /lib/modules/5.4.99/g_ether.ko
[ 92.595156] using random self ethernet address
[ 92.600049] using random host ethernet address
[ 92.606687] usb0: HOST MAC 22:4d:ac:16:33:5e
[ 92.611525] usb0: MAC 56:52:9c:6c:93:2b
[ 92.615966] using random self ethernet address
[ 92.620822] using random host ethernet address
[ 92.625927] g_ether gadget: Ethernet Gadget, version: Memorial Day 2008
[ 92.633205] g_ether gadget: g_ether ready
重新插拔usb识别到端口,需要改驱动。下载kindle_rndis.inf_amd64-v1.0.0.1.zip
- Download & Unzip attachment kindle_rndis.inf_amd64-v1.0.0.1.zip
- R-click “5-runasadmin_register-CA-cer.cmd” and “Run as administrator”*
- In Device Manager, expand “Ports (COM & LPT)”, R-click “Serial USB device (COM3)” > Update Driver Software…
- Browse for my computer for driver software > Select extract folder
第4步选择的是下载的文件夹。
驱动已正常。
回到板子。执行
ifconfig usb0 up
ifconfig usb0 192.168.31.100
然后pc这边来到 控制面板 --> 网络和Internet --> 网络连接,亦可直接搜索查看网络连接
开发板与PC可互相ping通。
共享pc网络给板子
pc中选中一个有效的网络连接, 然后右键属性 - 共享,勾选允许其他网络用户通过此计算机的Internet连接来连接
,选择usb网卡
vmware的虚拟机的网卡会有冲突,先禁用网卡解决,后期把虚拟机的网段改了。。
然后回到板子,执行
root@wang-virtual-machine:~# udhcpc -i usb0
udhcpc: sending discover
udhcpc: sending discover
udhcpc: sending discover
udhcpc: sending discover
udhcpc: sending select for 192.168.137.199
udhcpc: lease of 192.168.137.199 obtained, lease time 604800
root@wang-virtual-machine:~#
root@wang-virtual-machine:~#
root@wang-virtual-machine:~#
root@wang-virtual-machine:~# ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 8 bytes 824 (824.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 824 (824.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.137.199 netmask 255.255.255.0 broadcast 192.168.137.255
ether 56:52:9c:6c:93:2b txqueuelen 1000 (Ethernet)
RX packets 2692 bytes 230931 (225.5 KiB)
RX errors 0 dropped 1062 overruns 0 frame 0
TX packets 61 bytes 21142 (20.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@wang-virtual-machine:~# ping www.baidu.com
ping: socket: Address family not supported by protocol
PING www.a.shifen.com (39.156.66.14) 56(84) bytes of data.
64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=1 ttl=52 time=73.8 ms
64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=3 ttl=52 time=16.3 ms
^C64 bytes from 39.156.66.14: icmp_seq=4 ttl=52 time=11.6 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 3171ms
rtt min/avg/max/mdev = 11.561/33.889/73.757/28.258 ms
自启动脚本,添加/etc/init.d/S51usbifup
,增加755
权限,脚本如下,
#!/bin/sh
#
# usbifup Starts usb gadget rndis.
#
umask 077
start() {
printf "Starting USB Gadget RNDIS: "
modprobe g_ether
ifconfig usb0 up
udhcpc -i usb0
echo "OK"
}
stop() {
printf "Stopping USB Gadget RNDIS: "
rmmod g_ether
echo "OK"
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
restart
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?
https://blog.csdn.net/GJF712/article/details/126574807
https://blog.csdn.net/wangzhen209/article/details/39344703
详细步骤参考:https://www.cnblogs.com/hfwz/p/16053528.html
二、模拟U盘
设备树
需要的驱动文件
drivers/usb/gadget/libcomposite.ko
drivers/usb/gadget/function/usb_f_mass_storage.ko
drivers/usb/gadget/legacy/g_mass_storage.ko
我这里将libcomposite和usb_f_mass_storage编译进内核。
编译结果:
CC [M] drivers/input/touchscreen/goodix.mod.o
LD [M] drivers/input/touchscreen/goodix.ko
CC [M] drivers/media/i2c/ov2640.mod.o
LD [M] drivers/media/i2c/ov2640.ko
CC [M] drivers/media/i2c/ov5640.mod.o
LD [M] drivers/media/i2c/ov5640.ko
CC [M] drivers/staging/rtl8188eu/r8188eu.mod.o
LD [M] drivers/staging/rtl8188eu/r8188eu.ko
CC [M] drivers/usb/gadget/legacy/g_cdc.mod.o
LD [M] drivers/usb/gadget/legacy/g_cdc.ko
CC [M] drivers/usb/gadget/legacy/g_ether.mod.o
LD [M] drivers/usb/gadget/legacy/g_ether.ko
CC [M] drivers/usb/gadget/legacy/g_mass_storage.mod.o
LD [M] drivers/usb/gadget/legacy/g_mass_storage.ko
只需要g_mass_storage.ko,重新拷贝内核和设备树后,执行
insmod g_mass_storage.ko file=/dev/mmcblk0p2 removable=1
PC端没法查看,因为文件系统格式为ext4
创建fat32文件系统测试
apt-get install dosfstools
内核增加回环设备驱动。最后有给出设置的位置。
mkdir /usbfile
dd if=/dev/zero of=/usbfile/udisk.img bs=1k count=32768
mkfs.vfat /usbfile/udisk.img
sudo losetup /dev/loop7 /usbfile/udisk.img
sudo mount /dev/loop7 /usbfile/files
在/usbfile/files下即可操作文件系统了
sudo /lib/modules/5.4.99/insmod g_mass_storage.ko file=/dev/loop7 removable=1
在CP机下也可操作文件系统了
如果插着USB线,随着“sudo insmod g_mass_storage.ko file=/dev/loop7 removable=1”的执行,就可以听到PC电脑“叮咚”的声音,PC机找到了U盘,表示U盘创建成功!
losetup -a
losetup -l
参考:
https://blog.csdn.net/stevenqian/article/details/127224086
https://blog.csdn.net/weixin_39902545/article/details/116842999
https://blog.csdn.net/zounan909/article/details/82775362?spm=1001.2101.3001.6650.14&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-14-82775362-blog-123422949.235%5Ev43%5Epc_blog_bottom_relevance_base5&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-14-82775362-blog-123422949.235%5Ev43%5Epc_blog_bottom_relevance_base5&utm_relevant_index=24
https://blog.csdn.net/weixin_39836530/article/details/116842996
https://blog.csdn.net/zhuguanlin121/article/details/129846018
http://wiki.lcmaker.com/index.php?title=LC-PI-200S
三、虚拟串口
没有调试。
四、软件主从切换
设备树的otg模式先设置为:otg
&usb_otg {
dr_mode = "otg"; /* otg host peripheral */
status = "okay";
};
echo host > /sys/devices/platform/soc/1c13000.usb/musb-hdrc.1.auto/mode
echo peripheral > /sys/devices/platform/soc/1c13000.usb/musb-hdrc.1.auto/mode
cat /sys/devices/platform/soc/1c13000.usb/musb-hdrc.1.auto/mode
接入USB再切换。
接入遥控接收器:
root@wangpi:/usbfile/files/a#echo host > /sys/devices/platform/soc/1c13000.usb/musb-hdrc.1.auto/mode
[ 495.164369] phy phy-1c13400.phy.0: Changing dr_mode to 1form/soc/1c13000.usb/musb-hdrc.1.auto/mode
root@wangpi:/usbfile/files/a# [ 495.822510] usb 1-1: new full-speed USB device number 2 using musb-hdrc
[ 496.034270] input: Controller as /devices/platform/soc/1c13000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.0/0003:0079:181C.0001/input/input1
[ 496.050486] hid-generic 0003:0079:181C.0001: input: USB HID v1.11 Gamepad [Controller] on usb-musb-hdrc.1.auto-1/input0
[ 496.095969] input: Controller System Control as /devices/platform/soc/1c13000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.1/0003:0079:181C.0002/input/input2
[ 496.173860] input: Controller Consumer Control as /devices/platform/soc/1c13000.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.1/0003:0079:181C.0002/input/input3
[ 496.197874] hid-generic 0003:0079:181C.0002: input: USB HID v1.11 Device [Controller] on usb-musb-hdrc.1.auto-1/input1
USB线接入PC:
root@wangpi:~# echo peripheral > /sys/devices/platform/soc/1c13000.usb/musb-hdrc.1.auto/mode
root@wangpi:~# sudo insmod g^Cass_storage.ko file=/dev/loop7 removable=1
root@wangpi:~#
root@wangpi:~#
root@wangpi:~#
root@wangpi:~# cd /lib/modules/5.4.99/
root@wangpi:/lib/modules/5.4.99#
root@wangpi:/lib/modules/5.4.99# ls
8188eu.ko g_cdc.ko g_ether.ko g_mass_storage.ko rtl8188eufw.bin
root@wangpi:/lib/modules/5.4.99#
root@wangpi:/lib/modules/5.4.99#
root@wangpi:/lib/modules/5.4.99# sudo insmod g_mass_storage.ko file=/dev/loop7 removable=1
[ 384.593174] Mass Storage Function, version: 2009/09/11ge.ko file=/dev/loop7 removable=1
[ 384.598824] LUN: removable file: (no medium)
[ 384.604042] LUN: removable file: /dev/loop7
[ 384.608624] Number of LUNs=1
[ 384.615341] g_mass_storage gadget: Mass Storage Gadget, version: 2009/09/11
[ 384.623161] g_mass_storage gadget: userspace failed to provide iSerialNumber
[ 384.630822] g_mass_storage gadget: g_mass_storage ready
[ 384.637180] phy phy-1c13400.phy.0: Changing dr_mode to 2
五、其他
问题:
sudo: unable to resolve host wang-virtual-machine: Temporary failure in name resolution
修改 /etc/hostname 为 wangpi
修改 /etc/hosts 在localhost后面添加一个自己的用户名如wangpi
问题:
增加回环设备驱动看看能不能解决mount的问题。