樹莓派CM4 wifi頻繁斷開連接


CM4核心板上自帶的wifi模塊連接上華為/小米的路由器5G熱點以后,會頻繁斷開連接,斷開連接前使用 iw dev 查看wifi信息如下:

phy#0
        Unnamed/non-netdev interface
                wdev 0x2
                addr de:a6:32:fe:d2:**
                type P2P-device
                txpower 31.00 dBm
        Interface wlan0
                ifindex 6
                wdev 0x1
                addr dc:a6:32:fe:d2:**
                ssid HUAWEI-***_HiLink_5G
                type managed
                channel 153 (5765 MHz), width: 80 MHz, center1: 5775 MHz
                txpower 31.00 dBm

使用 iw event 命令監控原因,在斷開連接時看到如下信息:

  wlan0 (phy #0): scan finished: 2412 2417 2422 2427 2432 2437 2442 2447 2452 2457 2462 5180 5200 5220 5240 5260 5280 5300 5320 5500 5520 5540 5560 5580 5600 5620 5640 5660 5680 5700 5720 5745 5765 5785 5805 5825, ""
  wlan0 (phy #0): connected to 24:da:33:4c:11:22
  phy #0: regulatory domain change: set to CN by a country IE request on phy0  //wifi連接上時的log

wlan0 (phy #0): disconnected (local request) reason: 3: Deauthenticated because sending station is leaving (or has left) the IBSS or ESS //過了一會wifi斷開連接時的log
wlan0 (phy #0): scan started
wlan0 (phy #0): scan finished: 2412 2417 2422 2427 2432 2437 2442 2447 2452 2457 2462 2467 2472 5745 5765 5785 5805 5825, ""
wlan0 (phy #0): connected to 24:da:33:4c:11:22

斷開連接后會重新連接上,這時再用 iw dev 查看wifi信息如下:

phy#0
        Unnamed/non-netdev interface
                wdev 0x2
                addr de:a6:32:fe:d2:**
                type P2P-device
                txpower 31.00 dBm
        Interface wlan0
                ifindex 6
                wdev 0x1
                addr dc:a6:32:fe:d2:**
                ssid HUAWEI-**_HiLink_5G
                type managed
                channel 153 (5765 MHz), width: 40 MHz, center1: 5755 MHz
                txpower 31.00 dBm

這時會發線wifi模塊工作再40MHz頻寬上,這是為什么呢?通過 iw event 的信息來看,可疑點也只有“phy #0: regulatory domain change: set to CN by a country IE request on phy0”這句話了,因為在wpa_supplicant的配置文件中我設置了 country=US ,但是通過 iw reg get 查看發現國家碼確實被改成CN了。為什么國家碼會被改成CN呢?

通過查閱資料得到,如果路由器本身使能了 ieee80211d=1 ,那么wifi模塊的驅動會通過country IE 重新獲取國家碼,那么中國這邊獲取的國家碼為CN,然后就把國家碼設置到wifi模塊上了。

為了驗證上述查找資料得到的信息,本人特地找了另一個arm開發板,打開arm開發板的AP功能,並在hostapd的配置文件里加上 country_code=CN 和 ieee80211d=1 兩項,CM4 wifi模塊連接arm開發板的熱點之前,先 iw reg get 確保CM4系統里的國家碼為US,如果不是US的話, iw reg set US 設為US。然后在CM4 wifi連接上arm開發板的熱點時,在CM4所在的系統上可以看到 iw event 的日志中出現了“phy #0: regulatory domain change: set to CN by a country IE request on phy0”。

這一切也都很正常,但是問題在於樹莓派核心板上wifi模塊在國家碼設置為CN的時候,不支持80MHz頻寬和36、40、44、48等5G信道,所以會斷開重新連接AP,因為我們wifi連接工具wpa_supplicant的配置文件設置的國家碼為US,所以wifi模塊連接上AP以后如果還是工作在80MHz信道寬度,會又重復上面斷開連接的情況。如果wifi模塊連接上AP以后工作在40MHz頻寬(如上面所示),那么可以穩定工作在40MHz頻寬,不會再斷開連接。

至於為什么有時候斷開連接又重新連接在80MHz頻寬(設置的US國家碼生效),有時候斷開連接后重新連接在40MHz頻寬(設置的CN國家碼生效),可能是重新連接時wpa_cli 執行了reconfigure命令,原因暫不追究,我們來想辦法避免重復斷開連接的情況。

解決方法1:

國家碼做成可配置,不同的區域設置不同的國家碼。

即在wpa_supplicant的配置文件里加上 country=CN 。

優點:

在中國使用的話,國家碼設為CN以后,對於149~165信道的5G AP來說,wifi模塊連接上以后工作在40MHz頻寬上,穩定,不會掉線。

缺點:

不能支持36~48信道的5G AP,由於36~48信道在很多年之前在中國已經開放了,越來越多的路由器是支持36~48信道的,缺點很明顯。令外如果有些AP不支持40MHz頻寬的話(只支持5G AP 只支持80MHz頻寬),也是連接不上的。

解決方法2:

更改wifi驅動,通過模塊參數可設置強制wifi模塊只工作在US國家碼下,從而既讓wifi模塊支持80MHz頻寬的5G信道, 又支持36~48等5G信道。

修改 drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c 文件, 如下:

1. 添加模塊參數 regulatory_domain_force_us:

static int regulatory_domain_force_us = 0; 
module_param(regulatory_domain_force_us, int, S_IRUGO);
MODULE_PARM_DESC(regulatory_domain_force_us, "force set regulatory domain to US.");

2. 修改 static void brcmf_cfg80211_reg_notifier(struct wiphy *wiphy, struct regulatory_request *req) 函數:

 1 static void brcmf_cfg80211_reg_notifier(struct wiphy *wiphy,
 2                     struct regulatory_request *req)
 3 {
 4     struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
 5     struct brcmf_if *ifp = brcmf_get_ifp(cfg->pub, 0);
 6     struct brcmf_pub *drvr = cfg->pub;
 7     struct brcmf_fil_country_le ccreq;
 8     char *alpha2;
 9     s32 err; 
10     int i;
11     char reg_code[3] = "US"; 12 
13     err = brcmf_fil_iovar_data_get(ifp, "country", &ccreq, sizeof(ccreq));
14     if (err) {
15         bphy_err(drvr, "Country code iovar returned err = %d\n", err);
16         return;
17     }    
18 
19     /* The country code gets set to "00" by default at boot - substitute
20      * any saved ccode from the nvram file unless there is a valid code
21      * already set.
22      */
23     if (regulatory_domain_force_us) 24         alpha2 = reg_code; 25     else 
26         alpha2 = req->alpha2; 27     
28     if (alpha2[0] == '0' && alpha2[1] == '0') {
29         extern char saved_ccode[2];
30 
31         if ((isupper(ccreq.country_abbrev[0]) &&
32              isupper(ccreq.country_abbrev[1])) ||
33             !saved_ccode[0])
34             return;
35         alpha2 = saved_ccode;
36         pr_debug("brcmfmac: substituting saved ccode %c%c\n",

第11,23,24,25,26行為添加的內容,重新編譯驅動並替換原有驅動。並在系統里添加/etc/modprobe.d/brcmfmac.conf文件,內容如下:

options brcmfmac regulatory_domain_force_us=1

這樣在wifi驅動brcmfmac.ko加載的時候會自動傳入模塊參數 regulatory_domain_force_us=1,強制指定國家碼為US。可通過 cat /sys/module/brcmfmac/parameters/regulatory_domain_force_us 查看 regulatory_domain_force_us 的值。

優點:

wifi模塊既能支持36~48信道的AP,又能解決wifi模塊工作在80MHz頻寬頻繁掉線的問題。

注意:

  此修改只是強制讓wifi模塊本身固定工作在US國家碼下,即使系統層面通過 iw reg set CN 把國家碼設為CN,wifi驅動設置的卻為US,從而提高wifi模塊的兼容性,系統里通過 iw reg get 得到的信息並不一定等於wifi模塊實際工作的信息。

 


免責聲明!

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



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