MT7688 wifi 调试

最近公司要用到MT7688的内置wifi,查了许多资料终于调通,过程也比较坎坷,在这里整理一下。

1.先看官方文档:

MT7688 wifi 调试_第1张图片

坑1:git://git.openwrt.org/15.05/openwrt.git 失效

root@localhost:~# git clone git://git.openwrt.org/15.05/openwrt.git
正克隆到 'openwrt'...
fatal: remote error: access denied or repository not exported: /15.05/openwrt.git
root@localhost:~# 

问题原因:2018年 git://git.openwrt.org/15.05/openwrt.git 已经转移至 git://github.com/openwrt/chaos_calmer.git
解决办法:git clone git://github.com/openwrt/chaos_calmer.git
特别说明:这个代码一直在更新,要取得原始15.05版本的话需要 git checkout v15.05

2.继续按官方文档步骤

MT7688 wifi 调试_第2张图片

坑2:Subtarget 里没有MT7688 base boards这个选项。

MT7688 wifi 调试_第3张图片

问题原因:缺少MT7688 相关target文件
解决办法1:git checkout v15.05.1 (更新到v15.05.1)
v15.05.1 版本里有MT7688支持项
解决办法2:从较新的版本合入mt7688 相关target (本人因基于v15.05已经打了一些内核(linux-3.18.20)补丁,如果切到v15.05.1版本,内核版本变更为linux-3.18.23,与3.18.20相比补丁冲突还是太大,所以我查看change log,找到内核变更为 linux-3.18.21 时的版本,再与之合并,这样只需修改极少数补丁就行了)
特别说明:这里内核版本必须是(3.18.21 ~ 3.18.44) ,feeds/linkit/README.md 里有说明,摘录如下:

LinkIt Smart 7688 uses the MediaTek proprietary Wi-Fi driver. While OpenWrt Chaos Calmer is evolving, only selected Linux kernel versions of it are supported by the Wi-Fi driver (3.18.21 ~ 3.18.44). If you are looking for a non-proprietary Wi-Fi driver, the mt76 project would be a reference of the choice.

3.编译 make V=s -j9

-j9 表示启用9个进程来编译,这样可以加快编译速度,不然那真的要四五个小时,网上一些资料说N = cpu个数+1时,效果最好,我的主机是8核(可通过cat /proc/cpuinfo 查看cpu个数),所以我这里用-j9.
编译过程中可能报错,出错时 make V=s -j1 可以查看出错具体原因。
其它说明:
make clean 用于删除 ./bin and ./build_dir 文件夹,删除后内核和所有包都要重编
make target/linux/clean clean内核
make target/linux/{clean,compile,install} 内核重新编译
make package/luci/clean 删除luci包,类似的替换成其它包名即可
参考:OpenWRT build usage
编译完后,得到sysupgrage.bin文件,使用web页面进行升级

4.wifi 验证

  1. ifconfig -a

ra0       Link encap:Ethernet  HWaddr 00:00:00:00:00:00  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:11396 errors:0 dropped:0 overruns:0 frame:0
          TX packets:198 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2480013 (2.3 MiB)  TX bytes:1926 (1.8 KiB)
          Interrupt:6 

有ra0 网卡说明驱动已经生效了

  1. ifconfig ra0 up
  2. iwlist ra0 scan
root@MiniTel:~# iwlist ra0 scan
[  572.630000] /openwrt/CC/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7688/mt7628-4.0.1.3/mt_wifi_ap/../mt_wifi/embedded/os/linux/ap_ioctl.c:rt28xx_ap_ioctl[265]

ra0       Failed to read scan data : Operation not supported

坑3: iwlist ra0 scan 扫描wifi 失败

问题原因:驱动不兼容,不支持iwlist iwconfig 等wifi配置工具
解决办法1:mt_wifi.ko 这个驱动是闭源的,没有源码,没法更改,不支持就不支持,But ,还可以使用iwinfo

root@MiniTel:~# iwinfo ra0 scan
Cell 01 - Address: XX:XX:XX:XX:XX:XX
          ESSID: "TP-LINK_1101"
          Mode: Master  Channel: 1
          Signal: -256 dBm  Quality: 100/100
          Encryption: WPA2 PSK (AES-OCB)

Cell 02 - Address: XX:XX:XX:XX:XX:XX
          ESSID: "E-caretech-1"
          Mode: Master  Channel: 1
          Signal: -256 dBm  Quality: 20/100
          Encryption: WPA2 PSK (AES-OCB)

Cell 03 - Address: XX:XX:XX:XX:XX:XX
          ESSID: "MDR6A9A"
          Mode: Master  Channel: 1
          Signal: -256 dBm  Quality: 0/100
          Encryption: WPA2 PSK (AES-OCB)     

解决办法2:这里还有另外一种方法,使用 iwpriv 命令

AP scanning
Use iwpriv ra0 set SiteSurvey=1 to enable access point scanning. Note that it takes a while to scan nearby APs.
Use iwpriv ra0 get_site_survey ra0 to collect the scan results.

root@MiniTel:~# iwpriv ra0 set SiteSurvey=1
root@MiniTel:~# iwpriv ra0 get_site_survey ra0
ra0       get_site_survey:
Ch  SSID                             BSSID               Security               Siganl(%)W-Mode  ExtCH  NT WPS DPID
1   TP-LINK_1101                    XX:XX:XX:XX:XX:XX   WPA1PSKWPA2PSK/AES     100      11b/g   NONE   In  NO     
1   E-caretech-1                     XX:XX:XX:XX:XX:XX   WPA1PSKWPA2PSK/AES     18       11b/g/n ABOVE  In  NO     
4   ChinaNet-iEWP                    XX:XX:XX:XX:XX:XX   WPA1PSKWPA2PSK/TKIPAES 57       11b/g/n NONE   In YES   
  1. wifi 连接配置
    前面说了,mt7688的官方驱动不支持iwconfig 命令配置,那wifi 要怎么使用呢?
    我找到了一点资料,这里我梳理一下步骤:

4.1 启用radio0

uci set wireless.radio0.disabled=0

4,2 wan 口设置成 apcli0

uci set network.wan.ifname=apcli0

4.3 配置wifi

uci set wireless.sta.ssid=SampleAP
uci set wireless.sta.key=12345678
uci set wireless.sta.encryption=psk2
uci set wireless.sta.disabled=0
uci set wireless.radio0.linkit_mode=sta
uci commit
/etc/init.d/network restart

这里是通过uci 命令进行配置,uci commit 是将配置保存,最终保存在 /etc/config/wireless , /etc/config/network 等文件里,也可以手动直接修改 /etc/config/wireless , /etc/config/network 文件,然后/etc/init.d/network restart 重启
修正
实测发现多次使用 /etc/init.d/network restart 会导致/sbin/ap_client 进程有多个,导致系统卡死,改用wifi down wifi up

uci set wireless.sta.ssid=SampleAP
uci set wireless.sta.key=12345678
uci set wireless.sta.encryption=psk2
uci set wireless.sta.disabled=0
uci set wireless.radio0.linkit_mode=sta
uci commit
wifi down
wifi up

4.5 查看wifi 连接情况

root@MiniTel:~# ifconfig -a
apcli0    Link encap:Ethernet  HWaddr 02:00:00:00:00:00  
          inet addr:192.168.1.199  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::ff:fe00:0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:300 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

获取到 ip 说明已经连接上wifi.

root@MiniTel:~# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 apcli0
192.168.1.0     *               255.255.255.0   U     0      0        0 apcli0
192.168.1.1     *               255.255.255.255 UH    0      0        0 apcli0
192.168.100.0   *               255.255.255.0   U     0      0        0 br-lan
root@MiniTel:~# ping baidu.com
PING baidu.com (123.125.115.110): 56 data bytes
64 bytes from 123.125.115.110: seq=0 ttl=50 time=47.667 ms
64 bytes from 123.125.115.110: seq=1 ttl=50 time=50.156 ms
64 bytes from 123.125.115.110: seq=2 ttl=50 time=48.550 ms
64 bytes from 123.125.115.110: seq=3 ttl=50 time=49.293 ms
64 bytes from 123.125.115.110: seq=4 ttl=50 time=48.932 ms
64 bytes from 123.125.115.110: seq=5 ttl=50 time=48.085 ms
64 bytes from 123.125.115.110: seq=6 ttl=50 time=49.235 ms
64 bytes from 123.125.115.110: seq=7 ttl=50 time=48.213 ms
^C
--- baidu.com ping statistics ---
8 packets transmitted, 8 packets received, 0% packet loss
round-trip min/avg/max = 47.667/48.766/50.156 ms

至此,mt7688内置wifi 调试成功。
参考资料:
1.MT7688 build-the-firmware-from-source-codes
2.MT7688 switch-to-station-mode
3.MT7688 iwpriv-commands

5.疑难杂症

1.没有 /etc/config/wireless 文件?
参考这篇博文,了解/etc/config/wireless 文件生成的过程。
openwrt无线uci文件生成流程 - geshifei的博客 - CSDN博客
https://blog.csdn.net/geshifei/article/details/80980583

系统开机启动,执行/etc/init.d/boot(源码:package/base-files/etc/init.d/boot)
boot() {

/sbin/wifi detect > /tmp/wireless.tmp

[ -s /tmp/wireless.tmp ] && {

#在路由器/etc/config目录中生成uci配置文件wireless

cat /tmp/wireless.tmp >> /etc/config/wireless

}

rm -f /tmp/wireless.tmp

}

可以看出 /etc/init.d/boot 调用 /sbin/wifi detec 生成内容并最终写入/etc/config/wireless

追踪/sbin/wifi

...
wifi_detect() {
        for driver in ${2:-$DRIVERS}; do (
                if eval "type detect_$driver" 2>/dev/null >/dev/null; then
                        eval "detect_$driver" || echo "$driver: Detect failed" >&2
                else
                        echo "$driver: Hardware detection not supported" >&2
                fi
        ); done
}
...
DEVICES=
DRIVERS=
include /lib/wifi    #该目录下有ralink.sh 将DRIVERS赋值为ralink
scan_wifi

case "$1" in
        down) wifi_updown "disable" "$2";;
        detect) wifi_detect "$2";;
        status) ubus_wifi_cmd "status" "$2";;
        reload) wifi_reload "$2";;
        reload_legacy) wifi_reload_legacy "$2";;
        --help|help) usage;;
        *) ubus call network reload; wifi_updown "enable" "$2";;
esac

可知 wifi_detect 最终调用 detect_ralink ,detect_ralink 在/lib/wifi/ralink.sh (源码:feeds/linkit/mtk-sdk-wifi/files/lib/wifi/ralink.sh )下有定义

detect_ralink() {
	[ -z "$(uci get wireless.@wifi-device[-1].type 2> /dev/null)" ] || return 0

	cpu=$(awk 'BEGIN{FS="[ \t]+: MediaTek[ \t]"} /system type/ {print $2}' /proc/cpuinfo | cut -d" " -f1)
	case $cpu in
	MT7688)
		write_ralink mt_wifi mt7628 ra0 11g 7
		;;
	esac

	return 0
}

这里会判断cpu型号是不是MT7688,

 ~# cat /proc/cpuinfo 
system type             : MediaTek MT7628AN ver:1 eco:2
machine                 : mymachine
processor               : 0
cpu model               : MIPS 24KEc V5.5
BogoMIPS                : 385.84
...

问题就出在这了,我这读出的型号居然是MT7628AN !!,而我这芯片确实是MT7688的啊,先不管为什么了,改天研究一下/proc/cpuinfo 是怎么生成的。
解决办法是修改feeds/linkit/mtk-sdk-wifi/files/lib/wifi/ralink.sh

detect_ralink() {
	[ -z "$(uci get wireless.@wifi-device[-1].type 2> /dev/null)" ] || return 0

	cpu=$(awk 'BEGIN{FS="[ \t]+: MediaTek[ \t]"} /system type/ {print $2}' /proc/cpuinfo | cut -d" " -f1)
	case $cpu in
	MT7688 | MT7628AN)             #添加MT7628AN
		write_ralink mt_wifi mt7628 ra0 11g 7
		;;
	esac

	return 0
}

你可能感兴趣的:(Linux,OpenWrt)