Skip to contentSkip to conversation

wifi: rtl8xxxu: Support new chip RTL8710BU aka RTL8188GU

PATCH Superseded Bitterblue Smith 3 participants 5 messages Mar 8, 2023 – Mar 13, 2023 RTL8XXXU wireless driver (rtl8xxxu)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 wifi: rtl8xxxu: Support new chip RTL8710BU aka RTL8188GU

This chip is found in cheap "free driver" USB adapters from Aliexpress. Initially they pretend to be a CD-ROM containing the driver for Windows. "Ejecting" switches the device to wifi mode.

Features: 2.4 GHz, b/g/n mode, 1T1R, 150 Mbps.

This chip is more unique than other Realtek chips:

* The registers at addresses 0x0-0xff, which all the other chips use, can't be used here. New registers at 0x8000-0x80ff must be used instead. And it's not a simple matter of adding 0x8000: 0x2 (REG_SYS_FUNC) became 0x8004, 0x80 (REG_MCU_FW_DL) became 0x8090, etc.

* Also there are a few new registers which must be accessed indirectly because their addresses don't fit in 16 bits. No other chips seem to have these.

* The vendor driver compiles to 8188gu.ko, but the code calls the chip RTL8710B(U) pretty much everywhere, including messages visible to the user.

Another difference compared to the other chips supported by rtl8xxxu is that it has a new PHY status struct, or three of them actually, from which we extract the RSSI, among other things. This is not unique, though, just new. The chips supported by rtw88 also use it.

---
v2:
- Suggestions from Ping-Ke Shih:
- Add comma after the last member of enum rtl8xxxu_rtl_chip.
- Add comment about struct mutex syson_indirect_access_mutex.
- Declare variables in reverse Christmas tree order in

     rtl8710b_read_efuse().

- Remove unnecessary variable ret from rtl8710bu_identify_chip().
- Add definition for register 0xaac.
- Use the existing macros REG_SYS_FUNC, SYS_FUNC_BBRSTB, and SYS_FUNC_BB_GLB_RSTN instead of magic numbers in rtl8710bu_active_to_lps().
- Declare reg_mcu_fw_dl separately in rtl8xxxu_download_firmware().
- Add spaces after /* and before */ in some comments.
- Rearrange the declarations in rtl8710b_read_efuse8() as well.
- Load the right firmware based on the chip manufacturer (UMC/SMIC).
- Use the mask 0xc0 instead of 0xf0 to detect the chip manufacturer in rtl8710bu_identify_chip(). There was an extra shift in the vendor driver which I missed.
- Make the vid and pid fields of struct rtl8710bu_efuse two bytes each, and the filler field res7 one byte shorter.

- I was lazy and didn't do some things the right way in v1. I thought surely there are no more chips to support. But since then I discovered that the RTL8192FU can be bought from Aliexpress for 6.66 $. :) It will need the same PHY status parsing as the RTL8710BU, which is why there are these extra changes:
- Initialise priv->cck_new_agc in rtl8xxxu_init_device() always, regardless of the chip family.
- Pass the PHY status structs to the CCK RSSI functions.
- Move the "old AGC" CCK RSSI calculation from rtl8710bu_rx_parse_phystats_type0() to a new function rtl8710b_cck_rssi().
- Rename the functions rtl8710bu_rx_parse_phystats* to jaguar2_rx_parse_phystats* and move them to rtl8xxxu_core.c.
- Modify the functions jaguar2_rx_parse_phystats_type{1,2} to handle 2T2R chips as well.

---
 drivers/net/wireless/realtek/rtl8xxxu/Kconfig |    2 +-
 .../net/wireless/realtek/rtl8xxxu/Makefile    |    2 +-
 .../net/wireless/realtek/rtl8xxxu/rtl8xxxu.h  |  313 ++-
 .../realtek/rtl8xxxu/rtl8xxxu_8188e.c         |    5 +-
 .../realtek/rtl8xxxu/rtl8xxxu_8188f.c         |    7 +-
 .../realtek/rtl8xxxu/rtl8xxxu_8192c.c         |    2 +
 .../realtek/rtl8xxxu/rtl8xxxu_8192e.c         |    5 +-
 .../realtek/rtl8xxxu/rtl8xxxu_8710b.c         | 1878 +++++++++++++++++
 .../realtek/rtl8xxxu/rtl8xxxu_8723a.c         |    5 +-
 .../realtek/rtl8xxxu/rtl8xxxu_8723b.c         |    5 +-
 .../wireless/realtek/rtl8xxxu/rtl8xxxu_core.c |  275 ++-
 .../wireless/realtek/rtl8xxxu/rtl8xxxu_regs.h |   44 +
 12 files changed, 2481 insertions(+), 62 deletions(-)
 create mode 100644 drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c
drivers/net/wireless/realtek/rtl8xxxu/Kconfig +1 -1
index 2eed20b0988c..82bcaf44a65f 100644
@@ -11,7 +11,7 @@ config RTL8XXXU
 	  parts written to utilize the Linux mac80211 stack.
 	  The driver is known to work with a number of RTL8723AU,
 	  RL8188CU, RTL8188RU, RTL8191CU, RTL8192CU, RTL8723BU, RTL8192EU,
-	  RTL8188FU, and RTL8188EU devices.
+	  RTL8188FU, RTL8188EU, and RTL8710BU (aka RTL8188GU) devices.
 
 	  This driver is under development and has a limited feature
 	  set. In particular it does not yet support 40MHz channels
drivers/net/wireless/realtek/rtl8xxxu/Makefile +1 -1
index 0cb58fb30228..1bf083c15dcd 100644
@@ -3,4 +3,4 @@ obj-$(CONFIG_RTL8XXXU)	+= rtl8xxxu.o
 
 rtl8xxxu-y	:= rtl8xxxu_core.o rtl8xxxu_8192e.o rtl8xxxu_8723b.o \
 		   rtl8xxxu_8723a.o rtl8xxxu_8192c.o rtl8xxxu_8188f.o \
-		   rtl8xxxu_8188e.o
+		   rtl8xxxu_8188e.o rtl8xxxu_8710b.o
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu.h +310 -3

400 lines not on this page · raw message

drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8188e.c +4 -1
index a99ddb41cd24..8fc9c8f9accd 100644
@@ -1326,13 +1326,14 @@ static void rtl8188e_usb_quirks(struct rtl8xxxu_priv *priv)
 	rtl8xxxu_write8(priv, REG_EARLY_MODE_CONTROL_8188E + 3, 0x01);
 }
 
-static s8 rtl8188e_cck_rssi(struct rtl8xxxu_priv *priv, u8 cck_agc_rpt)
+static s8 rtl8188e_cck_rssi(struct rtl8xxxu_priv *priv, struct rtl8723au_phy_stats *phy_stats)
 {
 	/* only use lna 0/1/2/3/7 */
 	static const s8 lna_gain_table_0[8] = {17, -1, -13, -29, -32, -35, -38, -41};
 	/* only use lna 3/7 */
 	static const s8 lna_gain_table_1[8] = {29, 20, 12, 3, -6, -15, -24, -33};
 
+	u8 cck_agc_rpt = phy_stats->cck_agc_rpt_ofdm_cfosho_a;
 	s8 rx_pwr_all = 0x00;
 	u8 vga_idx, lna_idx;
 	s8 lna_gain = 0;
@@ -1856,6 +1857,7 @@ struct rtl8xxxu_fileops rtl8188eu_fops = {
 	.load_firmware = rtl8188eu_load_firmware,
 	.power_on = rtl8188eu_power_on,
 	.power_off = rtl8188eu_power_off,
+	.read_efuse = rtl8xxxu_read_efuse,
 	.reset_8051 = rtl8188eu_reset_8051,
 	.llt_init = rtl8xxxu_init_llt_table,
 	.init_phy_bb = rtl8188eu_init_phy_bb,
@@ -1864,6 +1866,7 @@ struct rtl8xxxu_fileops rtl8188eu_fops = {
 	.phy_iq_calibrate = rtl8188eu_phy_iq_calibrate,
 	.config_channel = rtl8188eu_config_channel,
 	.parse_rx_desc = rtl8xxxu_parse_rxdesc16,
+	.parse_phystats = rtl8723au_rx_parse_phystats,
 	.init_aggregation = rtl8188eu_init_aggregation,
 	.enable_rf = rtl8188e_enable_rf,
 	.disable_rf = rtl8188e_disable_rf,
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8188f.c +5 -2
index af6e2c8a5025..82dee1fed477 100644
@@ -791,7 +791,7 @@ static int rtl8188fu_init_phy_rf(struct rtl8xxxu_priv *priv)
 	return ret;
 }
 
-static void rtl8188f_phy_lc_calibrate(struct rtl8xxxu_priv *priv)
+void rtl8188f_phy_lc_calibrate(struct rtl8xxxu_priv *priv)
 {
 	u32 val32;
 	u32 rf_amode, lstf;
@@ -1677,8 +1677,9 @@ void rtl8188f_set_crystal_cap(struct rtl8xxxu_priv *priv, u8 crystal_cap)
 	cfo->crystal_cap = crystal_cap;
 }
 
-static s8 rtl8188f_cck_rssi(struct rtl8xxxu_priv *priv, u8 cck_agc_rpt)
+static s8 rtl8188f_cck_rssi(struct rtl8xxxu_priv *priv, struct rtl8723au_phy_stats *phy_stats)
 {
+	u8 cck_agc_rpt = phy_stats->cck_agc_rpt_ofdm_cfosho_a;
 	s8 rx_pwr_all = 0x00;
 	u8 vga_idx, lna_idx;
 
@@ -1714,6 +1715,7 @@ struct rtl8xxxu_fileops rtl8188fu_fops = {
 	.load_firmware = rtl8188fu_load_firmware,
 	.power_on = rtl8188fu_power_on,
 	.power_off = rtl8188fu_power_off,
+	.read_efuse = rtl8xxxu_read_efuse,
 	.reset_8051 = rtl8xxxu_reset_8051,
 	.llt_init = rtl8xxxu_auto_llt_table,
 	.init_phy_bb = rtl8188fu_init_phy_bb,
@@ -1723,6 +1725,7 @@ struct rtl8xxxu_fileops rtl8188fu_fops = {
 	.phy_iq_calibrate = rtl8188fu_phy_iq_calibrate,
 	.config_channel = rtl8188fu_config_channel,
 	.parse_rx_desc = rtl8xxxu_parse_rxdesc24,
+	.parse_phystats = rtl8723au_rx_parse_phystats,
 	.init_aggregation = rtl8188fu_init_aggregation,
 	.init_statistics = rtl8188fu_init_statistics,
 	.init_burst = rtl8xxxu_init_burst,
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8192c.c +2 -0
index e61d65c3579b..caeba56241fc 100644
@@ -594,6 +594,7 @@ struct rtl8xxxu_fileops rtl8192cu_fops = {
 	.load_firmware = rtl8192cu_load_firmware,
 	.power_on = rtl8192cu_power_on,
 	.power_off = rtl8xxxu_power_off,
+	.read_efuse = rtl8xxxu_read_efuse,
 	.reset_8051 = rtl8xxxu_reset_8051,
 	.llt_init = rtl8xxxu_init_llt_table,
 	.init_phy_bb = rtl8xxxu_gen1_init_phy_bb,
@@ -602,6 +603,7 @@ struct rtl8xxxu_fileops rtl8192cu_fops = {
 	.phy_iq_calibrate = rtl8xxxu_gen1_phy_iq_calibrate,
 	.config_channel = rtl8xxxu_gen1_config_channel,
 	.parse_rx_desc = rtl8xxxu_parse_rxdesc16,
+	.parse_phystats = rtl8723au_rx_parse_phystats,
 	.init_aggregation = rtl8xxxu_gen1_init_aggregation,
 	.enable_rf = rtl8xxxu_gen1_enable_rf,
 	.disable_rf = rtl8xxxu_gen1_disable_rf,
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8192e.c +4 -1
index 5cfc00237f42..0bd1b527b420 100644
@@ -1742,11 +1742,12 @@ static void rtl8192e_enable_rf(struct rtl8xxxu_priv *priv)
 	rtl8xxxu_write8(priv, REG_TXPAUSE, 0x00);
 }
 
-static s8 rtl8192e_cck_rssi(struct rtl8xxxu_priv *priv, u8 cck_agc_rpt)
+static s8 rtl8192e_cck_rssi(struct rtl8xxxu_priv *priv, struct rtl8723au_phy_stats *phy_stats)
 {
 	static const s8 lna_gain_table_0[8] = {15, 9, -10, -21, -23, -27, -43, -44};
 	static const s8 lna_gain_table_1[8] = {24, 18, 13, -4, -11, -18, -31, -36};
 
+	u8 cck_agc_rpt = phy_stats->cck_agc_rpt_ofdm_cfosho_a;
 	s8 rx_pwr_all = 0x00;
 	u8 vga_idx, lna_idx;
 	s8 lna_gain = 0;
@@ -1793,6 +1794,7 @@ struct rtl8xxxu_fileops rtl8192eu_fops = {
 	.load_firmware = rtl8192eu_load_firmware,
 	.power_on = rtl8192eu_power_on,
 	.power_off = rtl8192eu_power_off,
+	.read_efuse = rtl8xxxu_read_efuse,
 	.reset_8051 = rtl8xxxu_reset_8051,
 	.llt_init = rtl8xxxu_auto_llt_table,
 	.init_phy_bb = rtl8192eu_init_phy_bb,
@@ -1801,6 +1803,7 @@ struct rtl8xxxu_fileops rtl8192eu_fops = {
 	.phy_iq_calibrate = rtl8192eu_phy_iq_calibrate,
 	.config_channel = rtl8xxxu_gen2_config_channel,
 	.parse_rx_desc = rtl8xxxu_parse_rxdesc24,
+	.parse_phystats = rtl8723au_rx_parse_phystats,
 	.enable_rf = rtl8192e_enable_rf,
 	.disable_rf = rtl8xxxu_gen2_disable_rf,
 	.usb_quirks = rtl8xxxu_gen2_usb_quirks,
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +1878 -0

1,878 lines not on this page · raw message

drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8723a.c +4 -1
index 5e7b58d395ba..d219be19d07f 100644
@@ -435,8 +435,9 @@ void rtl8723a_set_crystal_cap(struct rtl8xxxu_priv *priv, u8 crystal_cap)
 	cfo->crystal_cap = crystal_cap;
 }
 
-s8 rtl8723a_cck_rssi(struct rtl8xxxu_priv *priv, u8 cck_agc_rpt)
+s8 rtl8723a_cck_rssi(struct rtl8xxxu_priv *priv, struct rtl8723au_phy_stats *phy_stats)
 {
+	u8 cck_agc_rpt = phy_stats->cck_agc_rpt_ofdm_cfosho_a;
 	s8 rx_pwr_all = 0x00;
 
 	switch (cck_agc_rpt & 0xc0) {
@@ -487,6 +488,7 @@ struct rtl8xxxu_fileops rtl8723au_fops = {
 	.load_firmware = rtl8723au_load_firmware,
 	.power_on = rtl8723au_power_on,
 	.power_off = rtl8xxxu_power_off,
+	.read_efuse = rtl8xxxu_read_efuse,
 	.reset_8051 = rtl8xxxu_reset_8051,
 	.llt_init = rtl8xxxu_init_llt_table,
 	.init_phy_bb = rtl8xxxu_gen1_init_phy_bb,
@@ -495,6 +497,7 @@ struct rtl8xxxu_fileops rtl8723au_fops = {
 	.phy_iq_calibrate = rtl8xxxu_gen1_phy_iq_calibrate,
 	.config_channel = rtl8xxxu_gen1_config_channel,
 	.parse_rx_desc = rtl8xxxu_parse_rxdesc16,
+	.parse_phystats = rtl8723au_rx_parse_phystats,
 	.init_aggregation = rtl8xxxu_gen1_init_aggregation,
 	.enable_rf = rtl8xxxu_gen1_enable_rf,
 	.disable_rf = rtl8xxxu_gen1_disable_rf,
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8723b.c +4 -1
index 21613d60dc22..d99538eb8398 100644
@@ -1675,8 +1675,9 @@ static void rtl8723bu_init_statistics(struct rtl8xxxu_priv *priv)
 	rtl8xxxu_write32(priv, REG_OFDM0_FA_RSTC, val32);
 }
 
-static s8 rtl8723b_cck_rssi(struct rtl8xxxu_priv *priv, u8 cck_agc_rpt)
+static s8 rtl8723b_cck_rssi(struct rtl8xxxu_priv *priv, struct rtl8723au_phy_stats *phy_stats)
 {
+	u8 cck_agc_rpt = phy_stats->cck_agc_rpt_ofdm_cfosho_a;
 	s8 rx_pwr_all = 0x00;
 	u8 vga_idx, lna_idx;
 
@@ -1709,6 +1710,7 @@ struct rtl8xxxu_fileops rtl8723bu_fops = {
 	.load_firmware = rtl8723bu_load_firmware,
 	.power_on = rtl8723bu_power_on,
 	.power_off = rtl8723bu_power_off,
+	.read_efuse = rtl8xxxu_read_efuse,
 	.reset_8051 = rtl8723bu_reset_8051,
 	.llt_init = rtl8xxxu_auto_llt_table,
 	.init_phy_bb = rtl8723bu_init_phy_bb,
@@ -1718,6 +1720,7 @@ struct rtl8xxxu_fileops rtl8723bu_fops = {
 	.phy_iq_calibrate = rtl8723bu_phy_iq_calibrate,
 	.config_channel = rtl8xxxu_gen2_config_channel,
 	.parse_rx_desc = rtl8xxxu_parse_rxdesc24,
+	.parse_phystats = rtl8723au_rx_parse_phystats,
 	.init_aggregation = rtl8723bu_init_aggregation,
 	.init_statistics = rtl8723bu_init_statistics,
 	.init_burst = rtl8xxxu_init_burst,
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_core.c +224 -51

559 lines not on this page · raw message

drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_regs.h +44 -0

44 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.39.2

Part of the diff is not shown here. View the raw message.

Tested-by on v2 1/1
#1 lore raw
Quoted 22 lines · Bitterblue Smith

This chip is found in cheap "free driver" USB adapters from Aliexpress. Initially they pretend to be a CD-ROM containing the driver for Windows. "Ejecting" switches the device to wifi mode.

Features: 2.4 GHz, b/g/n mode, 1T1R, 150 Mbps.

This chip is more unique than other Realtek chips:

* The registers at addresses 0x0-0xff, which all the other chips use, can't be used here. New registers at 0x8000-0x80ff must be used instead. And it's not a simple matter of adding 0x8000: 0x2 (REG_SYS_FUNC) became 0x8004, 0x80 (REG_MCU_FW_DL) became 0x8090, etc.

* Also there are a few new registers which must be accessed indirectly because their addresses don't fit in 16 bits. No other chips seem to have these.

* The vendor driver compiles to 8188gu.ko, but the code calls the chip RTL8710B(U) pretty much everywhere, including messages visible to the user.

Quoted 6 lines

Another difference compared to the other chips supported by rtl8xxxu is that it has a new PHY status struct, or three of them actually, from which we extract the RSSI, among other things. This is not unique, though, just new. The chips supported by rtw88 also use it.

struct usb_anchor tx_anchor;

Tested Chip: RTL8188EU

#2 lore raw
In reply to Bitterblue Smith
Quoted 75 lines

-----Original Message-----
From: Bitterblue Smith <[email protected]>
Sent: Thursday, March 9, 2023 5:28 AM
To: [email protected]
Cc: Jes Sorensen <[email protected]>; Ping-Ke Shih <[email protected]>; Jiajie Chen <[email protected]>
Subject: [PATCH v2] wifi: rtl8xxxu: Support new chip RTL8710BU aka RTL8188GU

This chip is found in cheap "free driver" USB adapters from Aliexpress. Initially they pretend to be a CD-ROM containing the driver for Windows. "Ejecting" switches the device to wifi mode.

Features: 2.4 GHz, b/g/n mode, 1T1R, 150 Mbps.

This chip is more unique than other Realtek chips:

* The registers at addresses 0x0-0xff, which all the other chips use, can't be used here. New registers at 0x8000-0x80ff must be used instead. And it's not a simple matter of adding 0x8000: 0x2 (REG_SYS_FUNC) became 0x8004, 0x80 (REG_MCU_FW_DL) became 0x8090, etc.

* Also there are a few new registers which must be accessed indirectly because their addresses don't fit in 16 bits. No other chips seem to have these.

* The vendor driver compiles to 8188gu.ko, but the code calls the chip RTL8710B(U) pretty much everywhere, including messages visible to the user.

Another difference compared to the other chips supported by rtl8xxxu is that it has a new PHY status struct, or three of them actually, from which we extract the RSSI, among other things. This is not unique, though, just new. The chips supported by rtw88 also use it.

---
v2:
- Suggestions from Ping-Ke Shih:
- Add comma after the last member of enum rtl8xxxu_rtl_chip.
- Add comment about struct mutex syson_indirect_access_mutex.
- Declare variables in reverse Christmas tree order in

     rtl8710b_read_efuse().

- Remove unnecessary variable ret from rtl8710bu_identify_chip().
- Add definition for register 0xaac.
- Use the existing macros REG_SYS_FUNC, SYS_FUNC_BBRSTB, and SYS_FUNC_BB_GLB_RSTN instead of magic numbers in rtl8710bu_active_to_lps().
- Declare reg_mcu_fw_dl separately in rtl8xxxu_download_firmware().
- Add spaces after /* and before */ in some comments.
- Rearrange the declarations in rtl8710b_read_efuse8() as well.
- Load the right firmware based on the chip manufacturer (UMC/SMIC).
- Use the mask 0xc0 instead of 0xf0 to detect the chip manufacturer in rtl8710bu_identify_chip(). There was an extra shift in the vendor driver which I missed.
- Make the vid and pid fields of struct rtl8710bu_efuse two bytes each, and the filler field res7 one byte shorter.

- I was lazy and didn't do some things the right way in v1. I thought surely there are no more chips to support. But since then I discovered that the RTL8192FU can be bought from Aliexpress for 6.66 $. :) It will need the same PHY status parsing as the RTL8710BU, which is why there are these extra changes:
- Initialise priv->cck_new_agc in rtl8xxxu_init_device() always, regardless of the chip family.
- Pass the PHY status structs to the CCK RSSI functions.
- Move the "old AGC" CCK RSSI calculation from rtl8710bu_rx_parse_phystats_type0() to a new function rtl8710b_cck_rssi().
- Rename the functions rtl8710bu_rx_parse_phystats* to jaguar2_rx_parse_phystats* and move them to rtl8xxxu_core.c.
- Modify the functions jaguar2_rx_parse_phystats_type{1,2} to handle 2T2R chips as well.
---

[...]

drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c line 485

Quoted 7 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +7 -0
+static u32 rtl8710b_indirect_read32(struct rtl8xxxu_priv *priv, u32 addr)
+{
+       struct device *dev = &priv->udev->dev;
+       u32 val32, value = 0xffffffff;
+       u8 polling_count = 0xff;
+
+       if (addr & 3) {

if (!IS_ALIGNED(addr, 4))

drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c line 498

Quoted 13 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +13 -0
+               dev_warn(dev, "%s: Aborting because 0x%x is not a multiple of 4.\n",
+                        __func__, addr);
+               return value;
+       }
+
+       mutex_lock(&priv->syson_indirect_access_mutex);
+
+       rtl8xxxu_write32(priv, REG_USB_HOST_INDIRECT_ADDR_8710B, addr);
+       rtl8xxxu_write32(priv, REG_EFUSE_INDIRECT_CTRL_8710B, NORMAL_REG_READ_OFFSET);
+
+       do
+               val32 = rtl8xxxu_read32(priv, REG_EFUSE_INDIRECT_CTRL_8710B);
+       while ((val32 & BIT(31)) && (--polling_count > 0));

Add brace is allowed for this case. Not sure if you prefer this, or miss comment before.

Quoted 12 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +12 -0
+
+       if (polling_count == 0)
+               dev_warn(dev, "%s: Failed to read from 0x%x, 0x806c = 0x%x\n",
+                        __func__, addr, val32);
+       else
+               value = rtl8xxxu_read32(priv, REG_USB_HOST_INDIRECT_DATA_8710B);
+
+       mutex_unlock(&priv->syson_indirect_access_mutex);
+
+       if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
+               dev_info(dev, "%s(%04x) = 0x%08x\n", __func__, addr, value);
+

drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c line 520

Quoted 10 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +10 -0
+       return value;
+}
+
+static void rtl8710b_indirect_write32(struct rtl8xxxu_priv *priv, u32 addr, u32 val)
+{
+       struct device *dev = &priv->udev->dev;
+       u8 polling_count = 0xff;
+       u32 val32;
+
+       if (addr & 3) {

if (!IS_ALIGNED(addr, 4))

Quoted 14 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +14 -0
+               dev_warn(dev, "%s: Aborting because 0x%x is not a multiple of 4.\n",
+                        __func__, addr);
+               return;
+       }
+
+       mutex_lock(&priv->syson_indirect_access_mutex);
+
+       rtl8xxxu_write32(priv, REG_USB_HOST_INDIRECT_ADDR_8710B, addr);
+       rtl8xxxu_write32(priv, REG_USB_HOST_INDIRECT_DATA_8710B, val);
+       rtl8xxxu_write32(priv, REG_EFUSE_INDIRECT_CTRL_8710B, NORMAL_REG_WRITE_OFFSET);
+
+       do
+               val32 = rtl8xxxu_read32(priv, REG_EFUSE_INDIRECT_CTRL_8710B);
+       while ((val32 & BIT(31)) && (--polling_count > 0));

drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c line 544

Quoted 10 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +10 -0
+
+       if (polling_count == 0)
+               dev_warn(dev, "%s: Failed to write 0x%x to 0x%x, 0x806c = 0x%x\n",
+                        __func__, val, addr, val32);
+
+       mutex_unlock(&priv->syson_indirect_access_mutex);
+
+       if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
+               dev_info(dev, "%s(%04x) = 0x%08x\n", __func__, addr, val);
+}

[...]

Only two minor comments, and v2 looks good to me. So, I run sparse and smatch to check this patch, and it reports two warnings:

1. drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c:742 rtl8710bu_config_channel() error: uninitialized symbol 'sec_ch_above'.

This looks like a false-alarm, because 'sec_ch_above' must be set if 'ht40' is true. But, this should reference back much to know this.
Maybe, we can set 'sec_ch_above = 0' initially.

2. drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c:1487 rtl8710bu_phy_iq_calibrate() error: uninitialized symbol 'reg_e94'.

This could be a false-alarm too. 'reg_e94' must be set if 'candidate >= 0', but original statement causes smatch hard to determine:

   if (reg_e94 && candidate >= 0)

swap the expressions to fix the warning:

  if (candidate >= 0 && reg_e94)

Ping-Ke

#3 lore raw
Quoted 78 lines · Ping-Ke Shih
Quoted 75 lines

-----Original Message-----
From: Bitterblue Smith <[email protected]>
Sent: Thursday, March 9, 2023 5:28 AM
To: [email protected]
Cc: Jes Sorensen <[email protected]>; Ping-Ke Shih <[email protected]>; Jiajie Chen <[email protected]>
Subject: [PATCH v2] wifi: rtl8xxxu: Support new chip RTL8710BU aka RTL8188GU

This chip is found in cheap "free driver" USB adapters from Aliexpress. Initially they pretend to be a CD-ROM containing the driver for Windows. "Ejecting" switches the device to wifi mode.

Features: 2.4 GHz, b/g/n mode, 1T1R, 150 Mbps.

This chip is more unique than other Realtek chips:

* The registers at addresses 0x0-0xff, which all the other chips use, can't be used here. New registers at 0x8000-0x80ff must be used instead. And it's not a simple matter of adding 0x8000: 0x2 (REG_SYS_FUNC) became 0x8004, 0x80 (REG_MCU_FW_DL) became 0x8090, etc.

* Also there are a few new registers which must be accessed indirectly because their addresses don't fit in 16 bits. No other chips seem to have these.

* The vendor driver compiles to 8188gu.ko, but the code calls the chip RTL8710B(U) pretty much everywhere, including messages visible to the user.

Another difference compared to the other chips supported by rtl8xxxu is that it has a new PHY status struct, or three of them actually, from which we extract the RSSI, among other things. This is not unique, though, just new. The chips supported by rtw88 also use it.

---
v2:
- Suggestions from Ping-Ke Shih:
- Add comma after the last member of enum rtl8xxxu_rtl_chip.
- Add comment about struct mutex syson_indirect_access_mutex.
- Declare variables in reverse Christmas tree order in

     rtl8710b_read_efuse().

- Remove unnecessary variable ret from rtl8710bu_identify_chip().
- Add definition for register 0xaac.
- Use the existing macros REG_SYS_FUNC, SYS_FUNC_BBRSTB, and SYS_FUNC_BB_GLB_RSTN instead of magic numbers in rtl8710bu_active_to_lps().
- Declare reg_mcu_fw_dl separately in rtl8xxxu_download_firmware().
- Add spaces after /* and before */ in some comments.
- Rearrange the declarations in rtl8710b_read_efuse8() as well.
- Load the right firmware based on the chip manufacturer (UMC/SMIC).
- Use the mask 0xc0 instead of 0xf0 to detect the chip manufacturer in rtl8710bu_identify_chip(). There was an extra shift in the vendor driver which I missed.
- Make the vid and pid fields of struct rtl8710bu_efuse two bytes each, and the filler field res7 one byte shorter.

- I was lazy and didn't do some things the right way in v1. I thought surely there are no more chips to support. But since then I discovered that the RTL8192FU can be bought from Aliexpress for 6.66 $. :) It will need the same PHY status parsing as the RTL8710BU, which is why there are these extra changes:
- Initialise priv->cck_new_agc in rtl8xxxu_init_device() always, regardless of the chip family.
- Pass the PHY status structs to the CCK RSSI functions.
- Move the "old AGC" CCK RSSI calculation from rtl8710bu_rx_parse_phystats_type0() to a new function rtl8710b_cck_rssi().
- Rename the functions rtl8710bu_rx_parse_phystats* to jaguar2_rx_parse_phystats* and move them to rtl8xxxu_core.c.
- Modify the functions jaguar2_rx_parse_phystats_type{1,2} to handle 2T2R chips as well.
---

[...]

Quoted 10 lines

drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c line 485

Quoted 7 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +7 -0
+static u32 rtl8710b_indirect_read32(struct rtl8xxxu_priv *priv, u32 addr)
+{
+       struct device *dev = &priv->udev->dev;
+       u32 val32, value = 0xffffffff;
+       u8 polling_count = 0xff;
+
+       if (addr & 3) {

if (!IS_ALIGNED(addr, 4))

Nice, that's more readable.

Quoted 8 lines
Quoted 8 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +8 -0
+               dev_warn(dev, "%s: Aborting because 0x%x is not a multiple of 4.\n",
+                        __func__, addr);
+               return value;
+       }
+
+       mutex_lock(&priv->syson_indirect_access_mutex);
+
+       rtl8xxxu_write32(priv, REG_USB_HOST_INDIRECT_ADDR_8710B, addr);
Quoted 9 lines

drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c line 498

Quoted 5 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +5 -0
+       rtl8xxxu_write32(priv, REG_EFUSE_INDIRECT_CTRL_8710B, NORMAL_REG_READ_OFFSET);
+
+       do
+               val32 = rtl8xxxu_read32(priv, REG_EFUSE_INDIRECT_CTRL_8710B);
+       while ((val32 & BIT(31)) && (--polling_count > 0));

Add brace is allowed for this case. Not sure if you prefer this, or miss comment before.

Yes, I prefer it without the braces.

Quoted 52 lines
Quoted 12 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +12 -0
+
+       if (polling_count == 0)
+               dev_warn(dev, "%s: Failed to read from 0x%x, 0x806c = 0x%x\n",
+                        __func__, addr, val32);
+       else
+               value = rtl8xxxu_read32(priv, REG_USB_HOST_INDIRECT_DATA_8710B);
+
+       mutex_unlock(&priv->syson_indirect_access_mutex);
+
+       if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_READ)
+               dev_info(dev, "%s(%04x) = 0x%08x\n", __func__, addr, value);
+

drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c line 520

Quoted 10 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +10 -0
+       return value;
+}
+
+static void rtl8710b_indirect_write32(struct rtl8xxxu_priv *priv, u32 addr, u32 val)
+{
+       struct device *dev = &priv->udev->dev;
+       u8 polling_count = 0xff;
+       u32 val32;
+
+       if (addr & 3) {

if (!IS_ALIGNED(addr, 4))

Quoted 24 lines
drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c new file +24 -0
+               dev_warn(dev, "%s: Aborting because 0x%x is not a multiple of 4.\n",
+                        __func__, addr);
+               return;
+       }
+
+       mutex_lock(&priv->syson_indirect_access_mutex);
+
+       rtl8xxxu_write32(priv, REG_USB_HOST_INDIRECT_ADDR_8710B, addr);
+       rtl8xxxu_write32(priv, REG_USB_HOST_INDIRECT_DATA_8710B, val);
+       rtl8xxxu_write32(priv, REG_EFUSE_INDIRECT_CTRL_8710B, NORMAL_REG_WRITE_OFFSET);
+
+       do
+               val32 = rtl8xxxu_read32(priv, REG_EFUSE_INDIRECT_CTRL_8710B);
+       while ((val32 & BIT(31)) && (--polling_count > 0));
+
+       if (polling_count == 0)
+               dev_warn(dev, "%s: Failed to write 0x%x to 0x%x, 0x806c = 0x%x\n",
+                        __func__, val, addr, val32);
+
+       mutex_unlock(&priv->syson_indirect_access_mutex);
+
+       if (rtl8xxxu_debug & RTL8XXXU_DEBUG_REG_WRITE)
+               dev_info(dev, "%s(%04x) = 0x%08x\n", __func__, addr, val);
+}

[...]

Quoted 9 lines

Only two minor comments, and v2 looks good to me. So, I run sparse and smatch to check this patch, and it reports two warnings:

1. drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c:742 rtl8710bu_config_channel() error: uninitialized symbol 'sec_ch_above'.

This looks like a false-alarm, because 'sec_ch_above' must be set if 'ht40' is true. But, this should reference back much to know this.
Maybe, we can set 'sec_ch_above = 0' initially.

I will initialise it.

Quoted 12 lines

2. drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c:1487 rtl8710bu_phy_iq_calibrate() error: uninitialized symbol 'reg_e94'.

This could be a false-alarm too. 'reg_e94' must be set if 'candidate >= 0', but original statement causes smatch hard to determine:

   if (reg_e94 && candidate >= 0)

swap the expressions to fix the warning:

  if (candidate >= 0 && reg_e94)

Moving "if (reg_e94)" inside the previous "if (candidate >= 0)" should also fix it, I think.

	if (candidate >= 0) {
		reg_e94 = result[candidate][0];
		reg_e9c = result[candidate][1];
		reg_ea4 = result[candidate][2];
		reg_eac = result[candidate][3];

		dev_dbg(dev, "%s: candidate is %x\n", __func__, candidate);
		dev_dbg(dev, "%s: e94=%x e9c=%x ea4=%x eac=%x\n",
			__func__, reg_e94, reg_e9c, reg_ea4, reg_eac);

		path_a_ok = true;

		if (reg_e94)
			rtl8xxxu_fill_iqk_matrix_a(priv, path_a_ok, result,
						   candidate, (reg_ea4 == 0));
	}
Quoted 4 lines

Ping-Ke

#4 lore raw
Quoted 27 lines

-----Original Message-----
From: Bitterblue Smith <[email protected]>
Sent: Saturday, March 11, 2023 1:53 AM
To: Ping-Ke Shih <[email protected]>; [email protected]
Cc: Jes Sorensen <[email protected]>; Jiajie Chen <[email protected]>
Subject: Re: [PATCH v2] wifi: rtl8xxxu: Support new chip RTL8710BU aka RTL8188GU

Quoted 2 lines · Ping-Ke Shih

2. drivers/net/wireless/realtek/rtl8xxxu/rtl8xxxu_8710b.c:1487 rtl8710bu_phy_iq_calibrate() error:

uninitialized symbol 'reg_e94'.

Quoted 10 lines

This could be a false-alarm too. 'reg_e94' must be set if 'candidate >= 0', but original statement causes smatch hard to determine:

   if (reg_e94 && candidate >= 0)

swap the expressions to fix the warning:

  if (candidate >= 0 && reg_e94)

Moving "if (reg_e94)" inside the previous "if (candidate >= 0)" should also fix it, I think.

Quoted 17 lines
        if (candidate >= 0) {
                reg_e94 = result[candidate][0];
                reg_e9c = result[candidate][1];
                reg_ea4 = result[candidate][2];
                reg_eac = result[candidate][3];

                dev_dbg(dev, "%s: candidate is %x\n", __func__, candidate);
                dev_dbg(dev, "%s: e94=%x e9c=%x ea4=%x eac=%x\n",
                        __func__, reg_e94, reg_e9c, reg_ea4, reg_eac);

                path_a_ok = true;

                if (reg_e94)
                        rtl8xxxu_fill_iqk_matrix_a(priv, path_a_ok, result,
                                                   candidate, (reg_ea4 == 0));
        }

Indeed, I have confirmed it works.