Edit online

rtl8821CS

4 Dec 2024
Read time: 8 minute(s)

本节描述了在 Linux 内核中添加和配置 Realtek RTL8821CS SDIO WiFi 驱动程序的详细步骤。

添加源码

Luban 的 Kernel 版本为 5.10, 建议获取官方最新驱动版本,适用于 Kernel 5.10 或更高版本。

  1. source/linux-5.10/drivers/net/wireless/realtek 中创建 rtl8821CS 目录,并复制驱动源码到此目录。

  2. source/linux-5.10/drivers/net/wireless/realtek/Kconfig 中添加对 rtl8821CS 模块的索引:
    source "drivers/net/wireless/realtek/rtl8821/Kconfig"
  3. source/linux-5.10/drivers/net/wireless/realtek/Makefile 中添加对 rtl8821CS 模块的索引:
    obj-$(CONFIG_RTL8821CS)                += rtl8821CS/

修改 Kconfig

drivers/net/wireless/realtek/rtl8821/Kconfig 中,调整 help 字段以匹配 Luban 的解析格式:
config RTL8821CS
    tristate "Realtek 8821C SDIO WiFi"
-    ---help---
+       help

基于版本的差异,Luban 对 Kconfig 中 help 的字段解析可能和驱动原生的格式有差异。

Kconfig 修改正确后,在 make kernel_menuconfig 配置界面中可以看到 rtl8821CS 模块。勾选 rtl8821CS 模块后,可以尝试解决编译错误。

添加include 目录

Makefile 中添加额外的头文件目录引用,确保编译时能找到所需的 .h 文件。

大部分 WiFi 驱动为了增强兼容性,驱动中会有一些存放 .h 文件的目录,在 Makefile 中进行引用。因为版本的差异, 此类目录的引用方法可能不一样,从而导致出错。以下为一种报错的示例及解决方法:
故障:
出错现象:
fatal error: drv_types.h: No such file or directory
17 | #include <drv_types.h>

解决方案:

source/linux-5.10/drivers/net/wireless/realtek/rtl8821cs/Makefile 中修改如下:
  1. 修改引用方式,添加 srctree 前缀。

  2. 添加额外的目录:

    -EXTRA_CFLAGS += -I$(src)/include
    +EXTRA_CFLAGS += -I$(srctree)/$(src)/include
    +EXTRA_CFLAGS += -I$(srctree)/$(src)/hal/phydm
    +EXTRA_CFLAGS += -I$(srctree)/$(src)/hal/btc
    +EXTRA_CFLAGS += -I$(srctree)/$(src)/platform

添加 Makefile 编译优化

source/linux-5.10/drivers/net/wireless/realtek/rtl8821cs/Makefile 中,将编译优化标志从 -O1 改为 -Os,以提高代码的运行效率:

-EXTRA_CFLAGS += -O1
+EXTRA_CFLAGS += -Os

内核配置

menuconfig 功能配置界面,进行如下功能配置:
  1. 无线配置

    如需使用 WiFi ,必须要在 Kernel 中打开 cfg80211mac80211 的支持:

    Networking support > Wireless
    
    --- Wireless
    <*>   cfg80211 - wireless configuration API
    [ ]     nl80211 testmode command (NEW)
    [ ]     enable developer warnings (NEW)
    [ ]     cfg80211 certification onus (NEW)
    [*]     enable powersave by default(NEW)
    [ ]     cfg80211 DebugFS entries (NEW)
    [*]     support CRDA (NEW)
    [*]     cfg80211 wireless extensions compatibility
    <*>   Generic IEEE 802.11 Networking Stack (mac80211)
    [*]   Minstrel (NEW)
          Default rate control algorithm (Minstrel)  --->
    [ ]   Enable mac80211 mesh networking support (NEW)
    [ ]   Export mac80211 internals in DebugFS (NEW)
    [ ]   Trace all mac80211 debug messages (NEW)
    [ ]   Select mac80211 debugging features (NEW)
    
  2. 蓝牙配置

    BT 的使用必须要在 Kernel 中打开 bluetooth 子系统相关配置:

    Networking support > Bluetooth subsystem support > Bluetooth device drivers
    
    < > HCI USB driver
    < > HCI SDIO driver
    <*> HCI UART driver
    [*]   UART (H4) protocol support
    < >   UART Nokia H4+ protocol support
    [ ]   BCSP protocol support
    [ ]   Atheros AR300x serial support
    [ ] HCILL protocol support
    -*- Three-wire UART (H5) protocol support
    [ ] Intel protocol support
    [ ] Broadcom protocol support
    [*] Realtek protocol support
    [ ] Qualcomm Atheros protocol support
    [ ] Intel AG6XX protocol support
    [ ] Marvell protocol support
    < > HCI BCM203x USB driver
    < > HCI BPA10x USB driver
    < > HCI BlueFRITZ! USB driver
    < > HCI VHCI (Virtual HCI device) driver
    < > Marvell Bluetooth driver support
    < > MediaTek HCI SDIO driver
    < > MediaTek HCI UART driver
    
  3. RFKILL 配置

    RFKILL 的使用必须要在 Kernel 中打开相关配置

    Networking support > RF switch subsystem support
    
     --- RF switch subsystem support
     [ ]   RF switch input support
     <*>   GPIO RFKILL driver
    
  4. 驱动选择

    要使用 8821C 驱动,需要在 Kernel 中打开该驱动

    Device Drivers > Network device support > Wireless LAN
    
    [*]   Realtek devices
    < >     Realtek 8187 and 8187B USB support
    < >     Realtek rtlwifi family of devices  ----
    < >     RTL8723AU/RTL8188[CR]U/RTL819[12]CU (mac80211) support
    < >     Realtek 802.11ac wireless chips support  ----
    <*>     Realtek 8821C SDIO WiFi
    

DTS 配置

board.dts 中配置各个子节点:
  1. WIFI
    1. 打开对应的 SDMC:
      &sdmc1 {
          pinctrl-names = "default";
          pinctrl-0 = <&sdmc1_pins_a>;//核对引脚是否和原理图一致
          bus-width = <4>;
          no-mmc;
          no-sd;
          non-removalbe;
          cap-sdio-irq;
          status = "okay";
      };
    2. 配置控制引脚:
      rfkill_wlan {
          compatible = "rfkill-gpio";
          rfkill-name = "wlan";
          rfkill-type = <1>;
          reset-gpios = <&gpio_e 4 GPIO_ACTIVE_HIGH>;//模组 WiFi 使能引脚,根据实际原理图配置
          shutdown-gpios = <&gpio_d 8 GPIO_ACTIVE_LOW>;//电源控制引脚,根据实际原理图配置
          status = "okay";
      };
  2. BT
    1. 打开对应 UART:
      &uart6 {
          pinctrl-names = "default";
          pinctrl-0 = <&uart6_pins_a>, <&uart6_rts_pins_a>, <&uart6_rts_pins_b>;//须和原理图保持一致
         status = "okay";
      };
    2. 配置控制引脚:
      rfkill_bt {
          compatible = "rfkill-gpio";
          rfkill-name = "bluetooth";
          rfkill-type = <2>;
          reset-gpios = <&gpio_c 6 GPIO_ACTIVE_HIGH>;//模组蓝牙使能引脚,须和原理图保持一致
          status = "okay";
      };

模组配置文件

蓝牙固件和下载工具,可以从模组厂获取,存放于 overylay 目录。目录结构如下所示:
├── lib
│   └── firmware
│       └── rtlbt
│           ├── rtl8821c_config//模组配置文件,最好从模组厂获取
│           └── rtl8821c_fw//模组固件,最好从模组厂获取
├── usr
│   └── bin
│       └── rtk_hciattach//模组蓝牙固件下载工具,最好从模组厂获取

功能测试和调试

  1. 在 menuconfig 进行功能配置,添加相关工具包:
    [*] wireless tools  --->
    [*] bluez-utils  --->
        [ ]   use prebuilt binary instead of building from source
        [ ]   build OBEX support
        [ ]   build CLI client
        [ ]   build monitor utility
        [*]   build tools
        [*]     install deprecated tools
        [ ]   build experimental tools
        [ ]   build audio plugins (a2dp and avrcp)
        [ ]   build health plugin
        [ ]   build hid plugin
        [ ]   build hog plugin
        [ ]   build mesh plugin
        [ ]   build midi plugin
        [*]   build network plugin
        [ ]   build nfc plugin
        [ ]   build sap plugin
              *** sixaxis plugin needs udev /dev management ***
        [ ]   install test scripts
              *** hid2hci tool needs udev /dev management ***
    
  2. 测试
    1. WiFi
      insmod rtl8821.ko
      ifconfig wlan0 up
      iwlist wlan0 scan
      
    2. BT
      echo0 > /sys/class/rfkill/rfkill0/state
      echo1 > /sys/class/rfkill/rfkill0/state
      rtk_hciattach -n -s 115200 /dev/ttyS6 rtk_h5 &
      hciconfig hci0 up
      hcitool scan
      
  3. 调试
    • 核对并测试硬件信号的连通性。

    • 查看系统启动日志和模组驱动加载日志是否有异常并排查。

    • WIFI 模组驱动日志配置方法如下,在 Makefile 中:
      CONFIG_RTW_DEBUG = n//debug 开关
      CONFIG_RTW_LOG_LEVEL = 4//debug level