一、配置开发板USB Gadget模式为rndis
1.内核配置:
Device Drivers --->
[*] USB support --->
<*> USB Gadget Support --->
<*> USB Gadget Drivers (USB functions configurable through configfs) --->
[*] Generic serial bulk in/out
[*] Abstract Control Model (CDC ACM)
[*] Object Exchange Model (CDC OBEX)
[ ] Network Control Model (CDC NCM)
[ ] Ethernet Control Model (CDC ECM)
[ ] Ethernet Control Model (CDC ECM) subset
[*] RNDIS
[ ] Ethernet Emulation Model (EEM)
[*] Mass storage
[ ] Loopback and sourcesink function (for testing)
[*] Function filesystem (FunctionFS)
[ ] MTP gadget
[*] Uevent notification of Gadget state
[*] Audio Class 1.0
[ ] Audio Class 2.0
[ ] MIDI function
[*] HID function
[*] USB Webcam function
[*] Printer function
2.配置USB Gadget模式为rndis
可以在/etc/init.d目录下添加一个启动脚本:
#!/bin/sh
#
# Start rndis
#
export CONFIGFS_HOME=/sys/kernel/config
case "$1" in
start)
echo "Starting usb devices ..."
# usb common configs.
mount -t configfs none $CONFIGFS_HOME
cd $CONFIGFS_HOME/usb_gadget
mkdir -p demo/strings/0x409
echo 0x18d1 > demo/idVendor
echo 0xd002 > demo/idProduct
echo 0x200 > demo/bcdUSB
echo 0x100 > demo/bcdDevice
echo "xxx" > demo/strings/0x409/manufacturer
echo "composite-demo" > demo/strings/0x409/product
echo "0123456789ABCDEF" > demo/strings/0x409/serialnumbermkdir -p demo/configs/c.1/strings/0x409
echo "compsite-config" > demo/configs/c.1/strings/0x409/configuration
echo 120 > demo/configs/c.1/MaxPower
# config device
/etc/init.d/usb/rndis $1
# enable UDC.
echo 13500000.otg > demo/UDC #13500000.otg根据/sys/class/udc/目录下文件得到
;;
stop)
echo "Stop usb devices ..."
echo none > demo/UDC
/etc/init.d/usb/rndis $1
;;
restart|reload)
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?
/etc/init.d/usb/rndis脚本内容如下:
# cat /etc/init.d/usb/rndis
#!/bin/sh
#
# Start rndis....
#
cd $CONFIGFS_HOME/usb_gadget
case "$1" in
start)
echo "Starting usb rndis devices ..."
mkdir -p demo/functions/rndis.0
if [ $? != 0 ]; then
echo "unable to create function rndis, check kernel config!"
exit 1
fi
ln -s demo/functions/rndis.0 demo/configs/c.1/
;;
stop)
echo "Stop usb rndis devices ..."
if [ ! -d demo/configs/c.1/rndis.0 ];then
exit 0
fi
rm demo/configs/c.1/rndis.0
rm demo/functions/rndis.0
;;
restart|reload)
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?
关于上述配置USB Gadget功能的解释,参见:通过configfs配置的Linux USB gadget 。
二、Linux主机端操作
使用如下命令给usb0分配IP地址:
ifconfig usb0 up ifconfig usb0 192.168.1.2
使能Linux主机端网络转发功能:
#! /bin/sh
echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -P FORWARD ACCEPT
iptables -A POSTROUTING -t nat -j MASQUERADE -s 192.168.1.0/24
通过上面配置 iptables -A POSTROUTING -t nat -j MASQUERADE -s 192.168.1.0/24 可知允许192.168.1.0~255 IP地址的网络转发。
注意:
如果Linux主机系统自带的网络管理程序在运行,网络管理程序检测到主机端usb0的网络不通时,可能会清除上面手动给usb0设置的IP地址。这时需要我们关掉主机端的网络管理程序,如果是ubuntu系统,执行 /etc/init.d/network-manager stop 即可。
三、arm linux开发板网络配置
配置开发板上usb0 IP:
ifconfig usb0 up
ifconfig usb0 192.168.1.1
route add deafult gw 192.168.1.2 dev usb0
192.168.1.2为Linux主机端usb0网卡的IP地址。
使用 route -n 或者 netstat -r 查看一下Kernel IP routing table,如果有如下一条,表示路由OK:
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.2 0.0.0.0 UG 0 0 0 usb0
192.168.1.2为Linux主机上面给usb0网卡设置的IP地址。
这时使用 ping www.baidu.com -I usb0 可以看到arm linux开发板已经可以通过USB桥接Linux主机的网络去访问公网了。