staging: r8188eu: fix and clean up some firmware code
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
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 MinuteWith 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.
---
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
@@ -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
PATCH v4 2/8 staging: r8188eu: convert u32 fields of rt_firmware_hdr to __le32
In reply to Michael StraubeConvert 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
@@ -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
PATCH v4 3/8 staging: r8188eu: clean up comments in struct rt_firmware_hdr
In reply to Michael StraubeRemove 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
@@ -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
PATCH v4 4/8 staging: r8188eu: rename fields of struct rt_firmware_hdr
In reply to Michael StraubeRename 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
@@ -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
PATCH v4 5/8 staging: r8188eu: use sizeof instead of hardcoded firmware header size
In reply to Michael StraubeUse 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
@@ -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
PATCH v4 6/8 staging: r8188eu: remove variables from rtl8188e_firmware_download()
In reply to Michael StraubeThe 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
@@ -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
PATCH v4 7/8 staging: r8188eu: use pr_info_once() to log the firmware version
In reply to Michael StraubeUse 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
@@ -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
PATCH v4 8/8 staging: r8188eu: check firmware header existence before access
In reply to Michael StraubeWe 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
@@ -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
Quoted 28 lines · Michael Straube
This series fixes wrong size of struct
rt_firmware_hdrin the first patch and does some cleanups inrtl8188e_firmware_download()in the other patches.Tested on
x86_64with Inter-Tech DMG-02.v4:
- Keep the in-line comments of structrt_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 ofrt_firmware_hdrfrom patch 8/8 of v2 in the patch that replaces the hardcoded size.v2:
- Added a patch to check size of structrt_firmware_hdrat compile time.Michael Straube (8):
staging: r8188eu: fix structrt_firmware_hdr
staging: r8188eu: convert u32 fields ofrt_firmware_hdrto__le32
staging: r8188eu: clean up comments in structrt_firmware_hdr
staging: r8188eu: rename fields of structrt_firmware_hdr
staging: r8188eu: use sizeof instead of hardcoded firmware header size
staging: r8188eu: remove variables fromrtl8188e_firmware_download()
staging: r8188eu: usepr_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