Skip to contentSkip to conversation

staging: r8188eu: fix and clean up some firmware code

PATCH In review Michael Straube 2 participants 10 messages Apr 17, 2022 Staging

lore
Share & tools
lore
#0 lore raw

v4 cover staging: r8188eu: fix and clean up some firmware code

This series fixes wrong size of struct rt_firmware_hdr in the first patch and does some cleanups in rtl8188e_firmware_download() in the other patches.

Tested on x86_64 with Inter-Tech DMG-02.

v4:
- Keep the in-line comments of struct rt_firmware_hdr.

v3:
- Splitted the first patch into two separate patches.
- Added back logging the firmware version only once.
- Included the compile time check for size of rt_firmware_hdr from patch 8/8 of v2 in the patch that replaces the hardcoded size.

v2:
- Added a patch to check size of struct rt_firmware_hdr at compile time.

Michael Straube (8):
staging: r8188eu: fix struct rt_firmware_hdr
staging: r8188eu: convert u32 fields of rt_firmware_hdr to __le32
staging: r8188eu: clean up comments in struct rt_firmware_hdr
staging: r8188eu: rename fields of struct rt_firmware_hdr
staging: r8188eu: use sizeof instead of hardcoded firmware header size
staging: r8188eu: remove variables from rtl8188e_firmware_download()
staging: r8188eu: use pr_info_once() to log the firmware version
staging: r8188eu: check firmware header existence before access

 drivers/staging/r8188eu/core/rtw_fw.c | 84 +++++++++++----------------
 1 file changed, 34 insertions(+), 50 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v4 1/8 staging: r8188eu: fix struct rt_firmware_hdr

The size of struct rt_firmware_hdr is 36 bytes.

$ pahole -C rt_firmware_hdr drivers/staging/r8188eu/r8188eu.o
struct rt_firmware_hdr {
        __le16                     Signature;            /*     0     2 */
        u8                         Category;             /*     2     1 */
        u8                         Function;             /*     3     1 */
        __le16                     Version;              /*     4     2 */
        u8                         Subversion;           /*     6     1 */

/* XXX 1 byte hole, try to pack */

u16 Rsvd1; /* 8 2 */
u8 Month; /* 10 1 */
u8 Date; /* 11 1 */
u8 Hour; /* 12 1 */
u8 Minute; /* 13 1 */
__le16 RamCodeSize; /* 14 2 */
u8 Foundry; /* 16 1 */
u8 Rsvd2; /* 17 1 */

/* XXX 2 bytes hole, try to pack */

__le32 SvnIdx; /* 20 4 */
u32 Rsvd3; /* 24 4 */
u32 Rsvd4; /* 28 4 */
u32 Rsvd5; /* 32 4 */

/* size: 36, cachelines: 1, members: 17 */ /* sum members: 33, holes: 2, sum holes: 3 */ /* last cacheline: 36 bytes */
};

But the header in the firmware file is only 32 bytes long.

The hexdump of rtl8188eufw.bin shows that the field Rsvd1 should be u8 instead of __le16.

OFFSET rtl8188eufw.bin
-----------------------------------------------------------
0x00000000 E1 88 10 00 0B 00 01 00 01 21 11 27 30 36 00 00 0x00000010 2D 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00

0x00000000  E1 88 10 00 0B 00 01  00     01     21    11    27 30 36 00 00
                              ^   ^      ^      ^     ^     ^
                     Subversion   Rsvd1  Month  Date  Hour  Minute

With the change of field Rsvd1 from __le16 to u8 the structure has the correct size 32.

$ pahole -C rt_firmware_hdr drivers/staging/r8188eu/r8188eu.o
struct rt_firmware_hdr {
        __le16                     Signature;            /*     0     2 */
        u8                         Category;             /*     2     1 */
        u8                         Function;             /*     3     1 */
        __le16                     Version;              /*     4     2 */
        u8                         Subversion;           /*     6     1 */
        u8                         Rsvd1;                /*     7     1 */
        u8                         Month;                /*     8     1 */
        u8                         Date;                 /*     9     1 */
        u8                         Hour;                 /*    10     1 */
        u8                         Minute;               /*    11     1 */
        __le16                     RamCodeSize;          /*    12     2 */
        u8                         Foundry;              /*    14     1 */
        u8                         Rsvd2;                /*    15     1 */
        __le32                     SvnIdx;               /*    16     4 */
        u32                        Rsvd3;                /*    20     4 */
        u32                        Rsvd4;                /*    24     4 */
        u32                        Rsvd5;                /*    28     4 */
        /* size: 32, cachelines: 1, members: 17 */
        /* last cacheline: 32 bytes */

The wrong size had no effect because the header size is hardcoded to 32 where it is used in the code and the fields after Subversion are not used.

Fixes 7884fc0a1473 ("staging: r8188eu: introduce new include dir for RTL8188eu driver")

---
v4:
- no changes

v3:
- updated the commit message
- moved the u32 to __le32 conversions to a separate patch

v2:
- no changes

 drivers/staging/r8188eu/core/rtw_fw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/r8188eu/core/rtw_fw.c +1 -1
index 8620f3c92b52..eb4ab11f6b28 100644
@@ -29,7 +29,7 @@ struct rt_firmware_hdr {
 					 *  FW for different conditions */
 	__le16		Version;	/*  FW Version */
 	u8		Subversion;	/*  FW Subversion, default 0x00 */
-	u16		Rsvd1;
+	u8		Rsvd1;
 
 	/*  LONG WORD 1 ---- */
 	u8		Month;	/*  Release time Month field */
Signature · 1 line
2.35.1
#2 lore raw

PATCH v4 2/8 staging: r8188eu: convert u32 fields of rt_firmware_hdr to __le32

In reply to Michael Straube

Convert the u32 fields of struct rt_firmware_hdr to __le32 for consistency.

---
v4:
- no changes

v3:
- this patch was part of patch 1/8 in v2

 drivers/staging/r8188eu/core/rtw_fw.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +3 -3
index eb4ab11f6b28..7cd08268f3b9 100644
@@ -42,11 +42,11 @@ struct rt_firmware_hdr {
 
 	/*  LONG WORD 2 ---- */
 	__le32		SvnIdx;	/*  The SVN entry index */
-	u32		Rsvd3;
+	__le32		Rsvd3;
 
 	/*  LONG WORD 3 ---- */
-	u32		Rsvd4;
-	u32		Rsvd5;
+	__le32		Rsvd4;
+	__le32		Rsvd5;
 };
 
 static void fw_download_enable(struct adapter *padapter, bool enable)
Signature · 1 line
2.35.1
#3 lore raw

PATCH v4 3/8 staging: r8188eu: clean up comments in struct rt_firmware_hdr

In reply to Michael Straube

Remove unnecessary comments from struct rt_firmware_hdr. While at it align the in-line comments.

---
v4:
- keep the in-line comments

v3:
- no changes

v2:
- no changes

 drivers/staging/r8188eu/core/rtw_fw.c | 36 ++++++++++-----------------
 1 file changed, 13 insertions(+), 23 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +13 -23
index 7cd08268f3b9..0fa27b36bb8e 100644
@@ -14,37 +14,27 @@
 	(le16_to_cpu(_fwhdr->Signature) & 0xFFF0) == 0x2300 ||	\
 	(le16_to_cpu(_fwhdr->Signature) & 0xFFF0) == 0x88E0)
 
-/*  This structure must be careful with byte-ordering */
-
 struct rt_firmware_hdr {
-	/*  8-byte alinment required */
-	/*  LONG WORD 0 ---- */
 	__le16		Signature;	/* 92C0: test chip; 92C,
 					 * 88C0: test chip; 88C1: MP A-cut;
 					 * 92C1: MP A-cut */
-	u8		Category;	/*  AP/NIC and USB/PCI */
-	u8		Function;	/*  Reserved for different FW function
-					 *  indcation, for further use when
-					 *  driver needs to download different
-					 *  FW for different conditions */
-	__le16		Version;	/*  FW Version */
-	u8		Subversion;	/*  FW Subversion, default 0x00 */
+	u8		Category;	/* AP/NIC and USB/PCI */
+	u8		Function;	/* Reserved for different FW function
+					 * indcation, for further use when
+					 * driver needs to download different
+					 * FW for different conditions */
+	__le16		Version;	/* FW Version */
+	u8		Subversion;	/* FW Subversion, default 0x00 */
 	u8		Rsvd1;
-
-	/*  LONG WORD 1 ---- */
-	u8		Month;	/*  Release time Month field */
-	u8		Date;	/*  Release time Date field */
-	u8		Hour;	/*  Release time Hour field */
-	u8		Minute;	/*  Release time Minute field */
-	__le16		RamCodeSize;	/*  The size of RAM code */
+	u8		Month;		/* Release time Month field */
+	u8		Date;		/* Release time Date field */
+	u8		Hour;		/* Release time Hour field */
+	u8		Minute;		/* Release time Minute field */
+	__le16		RamCodeSize;	/* The size of RAM code */
 	u8		Foundry;
 	u8		Rsvd2;
-
-	/*  LONG WORD 2 ---- */
-	__le32		SvnIdx;	/*  The SVN entry index */
+	__le32		SvnIdx;		/* The SVN entry index */
 	__le32		Rsvd3;
-
-	/*  LONG WORD 3 ---- */
 	__le32		Rsvd4;
 	__le32		Rsvd5;
 };
Signature · 1 line
2.35.1
#4 lore raw

PATCH v4 4/8 staging: r8188eu: rename fields of struct rt_firmware_hdr

In reply to Michael Straube

Rename the fields of struct rt_firmware_hdr to avoid camel case.

---
v4:
- no changes

v3:
- no changes

v2:
- no changes

 drivers/staging/r8188eu/core/rtw_fw.c | 58 +++++++++++++--------------
 1 file changed, 29 insertions(+), 29 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +29 -29
index 0fa27b36bb8e..a80cc7fa3a53 100644
@@ -9,34 +9,34 @@
 #define MAX_PAGE_SIZE		4096
 
 #define IS_FW_HEADER_EXIST(_fwhdr)				\
-	((le16_to_cpu(_fwhdr->Signature) & 0xFFF0) == 0x92C0 ||	\
-	(le16_to_cpu(_fwhdr->Signature) & 0xFFF0) == 0x88C0 ||	\
-	(le16_to_cpu(_fwhdr->Signature) & 0xFFF0) == 0x2300 ||	\
-	(le16_to_cpu(_fwhdr->Signature) & 0xFFF0) == 0x88E0)
+	((le16_to_cpu(_fwhdr->signature) & 0xFFF0) == 0x92C0 ||	\
+	(le16_to_cpu(_fwhdr->signature) & 0xFFF0) == 0x88C0 ||	\
+	(le16_to_cpu(_fwhdr->signature) & 0xFFF0) == 0x2300 ||	\
+	(le16_to_cpu(_fwhdr->signature) & 0xFFF0) == 0x88E0)
 
 struct rt_firmware_hdr {
-	__le16		Signature;	/* 92C0: test chip; 92C,
-					 * 88C0: test chip; 88C1: MP A-cut;
-					 * 92C1: MP A-cut */
-	u8		Category;	/* AP/NIC and USB/PCI */
-	u8		Function;	/* Reserved for different FW function
-					 * indcation, for further use when
-					 * driver needs to download different
-					 * FW for different conditions */
-	__le16		Version;	/* FW Version */
-	u8		Subversion;	/* FW Subversion, default 0x00 */
-	u8		Rsvd1;
-	u8		Month;		/* Release time Month field */
-	u8		Date;		/* Release time Date field */
-	u8		Hour;		/* Release time Hour field */
-	u8		Minute;		/* Release time Minute field */
-	__le16		RamCodeSize;	/* The size of RAM code */
-	u8		Foundry;
-	u8		Rsvd2;
-	__le32		SvnIdx;		/* The SVN entry index */
-	__le32		Rsvd3;
-	__le32		Rsvd4;
-	__le32		Rsvd5;
+	__le16	signature;	/* 92C0: test chip; 92C,
+				 * 88C0: test chip; 88C1: MP A-cut;
+				 * 92C1: MP A-cut */
+	u8	category;	/* AP/NIC and USB/PCI */
+	u8	function;	/* Reserved for different FW function
+				 * indcation, for further use when
+				 * driver needs to download different
+				 * FW for different conditions */
+	__le16	version;	/* FW Version */
+	u8	subversion;	/* FW Subversion, default 0x00 */
+	u8	rsvd1;
+	u8	month;		/* Release time Month field */
+	u8	date;		/* Release time Date field */
+	u8	hour;		/* Release time Hour field */
+	u8	minute;		/* Release time Minute field */
+	__le16	ramcodesize;	/* The size of RAM code */
+	u8	foundry;
+	u8	rsvd2;
+	__le32	svnidx;		/* The SVN entry index */
+	__le32	rsvd3;
+	__le32	rsvd4;
+	__le32	rsvd5;
 };
 
 static void fw_download_enable(struct adapter *padapter, bool enable)
@@ -259,9 +259,9 @@ int rtl8188e_firmware_download(struct adapter *padapter)
 	/*  To Check Fw header. Added by tynli. 2009.12.04. */
 	fwhdr = (struct rt_firmware_hdr *)dvobj->firmware.data;
 
-	fw_version = le16_to_cpu(fwhdr->Version);
-	fw_subversion = fwhdr->Subversion;
-	fw_signature = le16_to_cpu(fwhdr->Signature);
+	fw_version = le16_to_cpu(fwhdr->version);
+	fw_subversion = fwhdr->subversion;
+	fw_signature = le16_to_cpu(fwhdr->signature);
 
 	if (!log_version++)
 		pr_info("%sFirmware Version %d, SubVersion %d, Signature 0x%x\n",
Signature · 1 line
2.35.1
#5 lore raw

PATCH v4 5/8 staging: r8188eu: use sizeof instead of hardcoded firmware header size

In reply to Michael Straube

Use sizeof() instead of hardcoding the firmware header size and add a compile time check to ensure struct rt_firmware_hdr has the correct size.

---
v4:
- no changes

v3:
- added the compile time size check from patch 8/8 of v2

v2:
- no changes

 drivers/staging/r8188eu/core/rtw_fw.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +4 -3
index a80cc7fa3a53..42b36505cf8b 100644
@@ -39,6 +39,8 @@ struct rt_firmware_hdr {
 	__le32	rsvd5;
 };
 
+static_assert(sizeof(struct rt_firmware_hdr) == 32);
+
 static void fw_download_enable(struct adapter *padapter, bool enable)
 {
 	u8 tmp;
@@ -268,9 +270,8 @@ int rtl8188e_firmware_download(struct adapter *padapter)
 			DRIVER_PREFIX, fw_version, fw_subversion, fw_signature);
 
 	if (IS_FW_HEADER_EXIST(fwhdr)) {
-		/*  Shift 32 bytes for FW header */
-		fw_data = fw_data + 32;
-		fw_size = fw_size - 32;
+		fw_data = fw_data + sizeof(struct rt_firmware_hdr);
+		fw_size = fw_size - sizeof(struct rt_firmware_hdr);
 	}
 
 	/*  Suggested by Filen. If 8051 is running in RAM code, driver should inform Fw to reset by itself, */
Signature · 1 line
2.35.1
#6 lore raw

PATCH v4 6/8 staging: r8188eu: remove variables from rtl8188e_firmware_download()

In reply to Michael Straube

The local variables fw_version, fw_subversion, fw_signature in rtl8188e_firmware_download() are only used in one place. Use the assigned values directly and remove the variables to make the code shorter and cleaner.

---
v4:
- no changes

v3:
- no changes

v2:
- no changes

 drivers/staging/r8188eu/core/rtw_fw.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +2 -6
index 42b36505cf8b..7fa985cc787a 100644
@@ -244,7 +244,6 @@ int rtl8188e_firmware_download(struct adapter *padapter)
 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
 	struct device *device = dvobj_to_dev(dvobj);
 	struct rt_firmware_hdr *fwhdr = NULL;
-	u16 fw_version, fw_subversion, fw_signature;
 	u8 *fw_data;
 	u32 fw_size;
 	static int log_version;
@@ -261,13 +260,10 @@ int rtl8188e_firmware_download(struct adapter *padapter)
 	/*  To Check Fw header. Added by tynli. 2009.12.04. */
 	fwhdr = (struct rt_firmware_hdr *)dvobj->firmware.data;
 
-	fw_version = le16_to_cpu(fwhdr->version);
-	fw_subversion = fwhdr->subversion;
-	fw_signature = le16_to_cpu(fwhdr->signature);
-
 	if (!log_version++)
 		pr_info("%sFirmware Version %d, SubVersion %d, Signature 0x%x\n",
-			DRIVER_PREFIX, fw_version, fw_subversion, fw_signature);
+			DRIVER_PREFIX, le16_to_cpu(fwhdr->version), fwhdr->subversion,
+			le16_to_cpu(fwhdr->signature));
 
 	if (IS_FW_HEADER_EXIST(fwhdr)) {
 		fw_data = fw_data + sizeof(struct rt_firmware_hdr);
Signature · 1 line
2.35.1
#7 lore raw

PATCH v4 7/8 staging: r8188eu: use pr_info_once() to log the firmware version

In reply to Michael Straube

Use pr_info_once() instead of a static variable and an if statement to log the firmware version only once.

---
v4:
- no chages

v3:
- added back logging the firmware version only once, using pr_info_once() instead of a static variable and if statement

v2:
- no changes

 drivers/staging/r8188eu/core/rtw_fw.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +3 -5
index 7fa985cc787a..7c0653bb3bbc 100644
@@ -246,7 +246,6 @@ int rtl8188e_firmware_download(struct adapter *padapter)
 	struct rt_firmware_hdr *fwhdr = NULL;
 	u8 *fw_data;
 	u32 fw_size;
-	static int log_version;
 
 	if (!dvobj->firmware.data)
 		ret = load_firmware(&dvobj->firmware, device);
@@ -260,10 +259,9 @@ int rtl8188e_firmware_download(struct adapter *padapter)
 	/*  To Check Fw header. Added by tynli. 2009.12.04. */
 	fwhdr = (struct rt_firmware_hdr *)dvobj->firmware.data;
 
-	if (!log_version++)
-		pr_info("%sFirmware Version %d, SubVersion %d, Signature 0x%x\n",
-			DRIVER_PREFIX, le16_to_cpu(fwhdr->version), fwhdr->subversion,
-			le16_to_cpu(fwhdr->signature));
+	pr_info_once("%sFirmware Version %d, SubVersion %d, Signature 0x%x\n",
+		     DRIVER_PREFIX, le16_to_cpu(fwhdr->version), fwhdr->subversion,
+		     le16_to_cpu(fwhdr->signature));
 
 	if (IS_FW_HEADER_EXIST(fwhdr)) {
 		fw_data = fw_data + sizeof(struct rt_firmware_hdr);
Signature · 1 line
2.35.1
#8 lore raw

PATCH v4 8/8 staging: r8188eu: check firmware header existence before access

In reply to Michael Straube

We should access the fields of fwhdr only if the check for firmware header existence is true. Move the affected code into the if block that checks firmware header existence.

---
v4:
- no changes

v3:
- no real changes,
just the pr_info() -> pr_info_once() from patch 7/8 is different

v2:
- no changes

 drivers/staging/r8188eu/core/rtw_fw.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +4 -5
index 7c0653bb3bbc..bf077876ed3d 100644
@@ -256,14 +256,13 @@ int rtl8188e_firmware_download(struct adapter *padapter)
 	fw_data = dvobj->firmware.data;
 	fw_size = dvobj->firmware.size;
 
-	/*  To Check Fw header. Added by tynli. 2009.12.04. */
 	fwhdr = (struct rt_firmware_hdr *)dvobj->firmware.data;
 
-	pr_info_once("%sFirmware Version %d, SubVersion %d, Signature 0x%x\n",
-		     DRIVER_PREFIX, le16_to_cpu(fwhdr->version), fwhdr->subversion,
-		     le16_to_cpu(fwhdr->signature));
-
 	if (IS_FW_HEADER_EXIST(fwhdr)) {
+		pr_info_once("%sFirmware Version %d, SubVersion %d, Signature 0x%x\n",
+			     DRIVER_PREFIX, le16_to_cpu(fwhdr->version), fwhdr->subversion,
+			     le16_to_cpu(fwhdr->signature));
+
 		fw_data = fw_data + sizeof(struct rt_firmware_hdr);
 		fw_size = fw_size - sizeof(struct rt_firmware_hdr);
 	}
Signature · 1 line
2.35.1
Acked-by on the v4 series
#9 lore raw
In reply to Michael Straube
Quoted 28 lines · Michael Straube

This series fixes wrong size of struct rt_firmware_hdr in the first patch and does some cleanups in rtl8188e_firmware_download() in the other patches.

Tested on x86_64 with Inter-Tech DMG-02.

v4:
- Keep the in-line comments of struct rt_firmware_hdr.

v3:
- Splitted the first patch into two separate patches.
- Added back logging the firmware version only once.
- Included the compile time check for size of rt_firmware_hdr from patch 8/8 of v2 in the patch that replaces the hardcoded size.

v2:
- Added a patch to check size of struct rt_firmware_hdr at compile time.

Michael Straube (8):
staging: r8188eu: fix struct rt_firmware_hdr
staging: r8188eu: convert u32 fields of rt_firmware_hdr to __le32
staging: r8188eu: clean up comments in struct rt_firmware_hdr
staging: r8188eu: rename fields of struct rt_firmware_hdr
staging: r8188eu: use sizeof instead of hardcoded firmware header size
staging: r8188eu: remove variables from rtl8188e_firmware_download()
staging: r8188eu: use pr_info_once() to log the firmware version
staging: r8188eu: check firmware header existence before access

Quoted 2 lines
  drivers/staging/r8188eu/core/rtw_fw.c | 84 +++++++++++----------------
  1 file changed, 34 insertions(+), 50 deletions(-)

It has been a bit of effort, but I have no comments on this version.

Big improvement,

Larry