linux主機通過USB使用arm linux開發板網絡


一、配置開發板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 。

 二、arm linux開發板網絡配置

在/etc/init.d目錄下寫一個腳本啟動dnsmasq服務:

#!/bin/sh

[ -f /etc/dnsmasq.conf ] || exit 0

case "$1" in
        start)
                printf "Starting dnsmasq: "
                start-stop-daemon -S -x /usr/sbin/dnsmasq
                [ $? = 0 ] && echo "OK" || echo "FAIL"
                ;;
        stop)
                printf "Stopping dnsmasq: "
                start-stop-daemon -K -q -x /usr/sbin/dnsmasq
                [ $? = 0 ] && echo "OK" || echo "FAIL"
                ;;
        restart|reload)
                $0 stop
                $0 start
                ;;
        *)
                echo "Usage: $0 {start|stop|restart}"
                exit 1
esac

exit 0

dnsmasq配置文件/etc/dnsmasq.conf內容如下:

dhcp-range=192.168.1.2,192.168.1.25,12h

配置開發板上usb0 IP:

ifconfig usb0 up

ifconfig usb0 192.168.1.1

使能開發板上網絡轉發功能:

echo "1" > /proc/sys/net/ipv4/ip_forward
#echo "net.ipv4.ip_forward = 1" > /etc/sysctl.conf
#sysctl -p
iptables -F
iptables -t nat -F
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

通過上面配置 iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 可以知道使用的是arm開發板上eth0網卡進行轉發。

如何配置arm linux開發板kernel層面支持ip_forward,參考:WIFI AP模式通過LAN網線上網 。

三、Linux主機端操作

使用如下命令給usb0分配IP地址:

ifconfig usb0 up
udhcpc -i usb0

這時Linux主機端usb0拿到的IP地址理論上在192.168.1.2~25之間。

使用 route -n 或者 netstat -r 查看一下Kernel IP routing table,如果有如下一行,表示路由OK:

root@allen:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 usb0

否則需要手動添加:

route add default gw 192.168.1.1 dev usb0

192.168.1.1為arm linux開發板上面給usb0網卡設置的IP地址。

這時使用 ping www.baidu.com -I usb0 可以看到Linux主機已經可以通過USB使用arm linux開發板的網絡去訪問公網了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM