Skip to contentSkip to conversation

staging: r8188eu: clean up block_write() in rtw_fw.c

PATCH In review Michael Straube 2 participants 9 messages Apr 12, 2022 – Apr 13, 2022 Staging

lore
Share & tools
lore
#0 lore raw

cover staging: r8188eu: clean up block_write() in rtw_fw.c

This series cleans up the block_write() function in rtw_fw.c.

I wondered if removing the part that writes 8-byte blocks makes any notable difference. On my system it took about two seconds longer to init the device. So I left that as is.

Tested on x86_64 with Inter-Tech DMG-02.

Michael Straube (6):
staging: r8188eu: correct macro spelling mistake
staging: r8188eu: rename parameter of block_write()
staging: r8188eu: change void parameters to u8
staging: r8188eu: reduce variables in block_write()
staging: r8188eu: remove unneeded initializations
staging: r8188eu: clean up long lines in block_write()

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

PATCH 1/6 staging: r8188eu: correct macro spelling mistake

Correct a macro spelling mistake in rtw_fw.c.

MAX_REG_BOLCK_SIZE -> MAX_REG_BLOCK_SIZE

---
 drivers/staging/r8188eu/core/rtw_fw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +2 -2
index 7a0997b9bac5..0905384bafc7 100644
@@ -4,7 +4,7 @@
 #include <linux/firmware.h>
 #include "../include/rtw_fw.h"
 
-#define MAX_REG_BOLCK_SIZE	196
+#define MAX_REG_BLOCK_SIZE	196
 #define FW_8188E_START_ADDRESS	0x1000
 #define MAX_PAGE_SIZE		4096
 
@@ -82,7 +82,7 @@ static int block_write(struct adapter *padapter, void *buffer, u32 buffSize)
 	u8 *bufferPtr	= (u8 *)buffer;
 	u32	i = 0, offset = 0;
 
-	blockSize_p1 = MAX_REG_BOLCK_SIZE;
+	blockSize_p1 = MAX_REG_BLOCK_SIZE;
 
 	/* 3 Phase #1 */
 	blockCount_p1 = buffSize / blockSize_p1;
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/6 staging: r8188eu: rename parameter of block_write()

In reply to Michael Straube

Rename the parameter buffSize of block_write() to avoid camel case.

buffSize -> size

---
 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 0905384bafc7..5f59acfd8b5d 100644
@@ -71,7 +71,7 @@ static void fw_download_enable(struct adapter *padapter, bool enable)
 	}
 }
 
-static int block_write(struct adapter *padapter, void *buffer, u32 buffSize)
+static int block_write(struct adapter *padapter, void *buffer, u32 size)
 {
 	int ret = _SUCCESS;
 	u32	blockSize_p1 = 4;	/*  (Default) Phase #1 : PCI muse use 4-byte write to download FW */
@@ -85,8 +85,8 @@ static int block_write(struct adapter *padapter, void *buffer, u32 buffSize)
 	blockSize_p1 = MAX_REG_BLOCK_SIZE;
 
 	/* 3 Phase #1 */
-	blockCount_p1 = buffSize / blockSize_p1;
-	remainSize_p1 = buffSize % blockSize_p1;
+	blockCount_p1 = size / blockSize_p1;
+	remainSize_p1 = size % blockSize_p1;
 
 	for (i = 0; i < blockCount_p1; i++) {
 		ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + i * blockSize_p1), blockSize_p1, (bufferPtr + i * blockSize_p1));
Signature · 1 line
2.35.1
#5 lore raw

PATCH 3/6 staging: r8188eu: change void parameters to u8

In reply to Michael Straube

The second parameter of write_fw(), page_write() and block_write() is a void pointer, but we always pass an u8 pointer. We can convert this parameter to an u8 pointer. The pointer is not changed in the functions, so we can safely remove the local variable bufferPtr.

---
 drivers/staging/r8188eu/core/rtw_fw.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +8 -10
index 5f59acfd8b5d..3cea4e41ab13 100644
@@ -71,7 +71,7 @@ static void fw_download_enable(struct adapter *padapter, bool enable)
 	}
 }
 
-static int block_write(struct adapter *padapter, void *buffer, u32 size)
+static int block_write(struct adapter *padapter, u8 *buffer, u32 size)
 {
 	int ret = _SUCCESS;
 	u32	blockSize_p1 = 4;	/*  (Default) Phase #1 : PCI muse use 4-byte write to download FW */
@@ -79,7 +79,6 @@ static int block_write(struct adapter *padapter, void *buffer, u32 size)
 	u32	blockSize_p3 = 1;	/*  Phase #3 : Use 1-byte, the remnant of FW image. */
 	u32	blockCount_p1 = 0, blockCount_p2 = 0, blockCount_p3 = 0;
 	u32	remainSize_p1 = 0, remainSize_p2 = 0;
-	u8 *bufferPtr	= (u8 *)buffer;
 	u32	i = 0, offset = 0;
 
 	blockSize_p1 = MAX_REG_BLOCK_SIZE;
@@ -89,7 +88,7 @@ static int block_write(struct adapter *padapter, void *buffer, u32 size)
 	remainSize_p1 = size % blockSize_p1;
 
 	for (i = 0; i < blockCount_p1; i++) {
-		ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + i * blockSize_p1), blockSize_p1, (bufferPtr + i * blockSize_p1));
+		ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + i * blockSize_p1), blockSize_p1, (buffer + i * blockSize_p1));
 		if (ret == _FAIL)
 			goto exit;
 	}
@@ -102,7 +101,7 @@ static int block_write(struct adapter *padapter, void *buffer, u32 size)
 		remainSize_p2 = remainSize_p1 % blockSize_p2;
 
 		for (i = 0; i < blockCount_p2; i++) {
-			ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + offset + i * blockSize_p2), blockSize_p2, (bufferPtr + offset + i * blockSize_p2));
+			ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + offset + i * blockSize_p2), blockSize_p2, (buffer + offset + i * blockSize_p2));
 
 			if (ret == _FAIL)
 				goto exit;
@@ -116,7 +115,7 @@ static int block_write(struct adapter *padapter, void *buffer, u32 size)
 		blockCount_p3 = remainSize_p2 / blockSize_p3;
 
 		for (i = 0; i < blockCount_p3; i++) {
-			ret = rtw_write8(padapter, (FW_8188E_START_ADDRESS + offset + i), *(bufferPtr + offset + i));
+			ret = rtw_write8(padapter, (FW_8188E_START_ADDRESS + offset + i), *(buffer + offset + i));
 
 			if (ret == _FAIL)
 				goto exit;
@@ -127,7 +126,7 @@ static int block_write(struct adapter *padapter, void *buffer, u32 size)
 	return ret;
 }
 
-static int page_write(struct adapter *padapter, u32 page, void *buffer, u32 size)
+static int page_write(struct adapter *padapter, u32 page, u8 *buffer, u32 size)
 {
 	u8 value8;
 	u8 u8Page = (u8)(page & 0x07);
@@ -138,21 +137,20 @@ static int page_write(struct adapter *padapter, u32 page, void *buffer, u32 size
 	return block_write(padapter, buffer, size);
 }
 
-static int write_fw(struct adapter *padapter, void *buffer, u32 size)
+static int write_fw(struct adapter *padapter, u8 *buffer, u32 size)
 {
 	/*  Since we need dynamic decide method of dwonload fw, so we call this function to get chip version. */
 	/*  We can remove _ReadChipVersion from ReadpadapterInfo8192C later. */
 	int ret = _SUCCESS;
 	u32	pageNums, remainSize;
 	u32	page, offset;
-	u8 *bufferPtr = (u8 *)buffer;
 
 	pageNums = size / MAX_PAGE_SIZE;
 	remainSize = size % MAX_PAGE_SIZE;
 
 	for (page = 0; page < pageNums; page++) {
 		offset = page * MAX_PAGE_SIZE;
-		ret = page_write(padapter, page, bufferPtr + offset, MAX_PAGE_SIZE);
+		ret = page_write(padapter, page, buffer + offset, MAX_PAGE_SIZE);
 
 		if (ret == _FAIL)
 			goto exit;
@@ -160,7 +158,7 @@ static int write_fw(struct adapter *padapter, void *buffer, u32 size)
 	if (remainSize) {
 		offset = pageNums * MAX_PAGE_SIZE;
 		page = pageNums;
-		ret = page_write(padapter, page, bufferPtr + offset, remainSize);
+		ret = page_write(padapter, page, buffer + offset, remainSize);
 
 		if (ret == _FAIL)
 			goto exit;
Signature · 1 line
2.35.1
#6 lore raw

PATCH 4/6 staging: r8188eu: reduce variables in block_write()

In reply to Michael Straube

For cleaner code and improved readability we can reduce the number of local variables in block_write(). Use a single variable for block size, number of blocks and remaining size.

---
 drivers/staging/r8188eu/core/rtw_fw.c | 43 +++++++++++----------------
 1 file changed, 18 insertions(+), 25 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +18 -25
index 3cea4e41ab13..b4ab050a6f23 100644
@@ -74,49 +74,42 @@ static void fw_download_enable(struct adapter *padapter, bool enable)
 static int block_write(struct adapter *padapter, u8 *buffer, u32 size)
 {
 	int ret = _SUCCESS;
-	u32	blockSize_p1 = 4;	/*  (Default) Phase #1 : PCI muse use 4-byte write to download FW */
-	u32	blockSize_p2 = 8;	/*  Phase #2 : Use 8-byte, if Phase#1 use big size to write FW. */
-	u32	blockSize_p3 = 1;	/*  Phase #3 : Use 1-byte, the remnant of FW image. */
-	u32	blockCount_p1 = 0, blockCount_p2 = 0, blockCount_p3 = 0;
-	u32	remainSize_p1 = 0, remainSize_p2 = 0;
+	u32 blocks, block_size, remain;
 	u32	i = 0, offset = 0;
 
-	blockSize_p1 = MAX_REG_BLOCK_SIZE;
+	block_size = MAX_REG_BLOCK_SIZE;
 
-	/* 3 Phase #1 */
-	blockCount_p1 = size / blockSize_p1;
-	remainSize_p1 = size % blockSize_p1;
+	blocks = size / block_size;
+	remain = size % block_size;
 
-	for (i = 0; i < blockCount_p1; i++) {
-		ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + i * blockSize_p1), blockSize_p1, (buffer + i * blockSize_p1));
+	for (i = 0; i < blocks; i++) {
+		ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + i * block_size), block_size, (buffer + i * block_size));
 		if (ret == _FAIL)
 			goto exit;
 	}
 
-	/* 3 Phase #2 */
-	if (remainSize_p1) {
-		offset = blockCount_p1 * blockSize_p1;
+	if (remain) {
+		offset = blocks * block_size;
+		block_size = 8;
 
-		blockCount_p2 = remainSize_p1 / blockSize_p2;
-		remainSize_p2 = remainSize_p1 % blockSize_p2;
-
-		for (i = 0; i < blockCount_p2; i++) {
-			ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + offset + i * blockSize_p2), blockSize_p2, (buffer + offset + i * blockSize_p2));
+		blocks = remain / block_size;
+		remain = remain % block_size;
 
+		for (i = 0; i < blocks; i++) {
+			ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + offset + i * block_size), block_size, (buffer + offset + i * block_size));
 			if (ret == _FAIL)
 				goto exit;
 		}
 	}
 
-	/* 3 Phase #3 */
-	if (remainSize_p2) {
-		offset = (blockCount_p1 * blockSize_p1) + (blockCount_p2 * blockSize_p2);
+	if (remain) {
+		offset += blocks * block_size;
 
-		blockCount_p3 = remainSize_p2 / blockSize_p3;
+		/* block size 1 */
+		blocks = remain;
 
-		for (i = 0; i < blockCount_p3; i++) {
+		for (i = 0; i < blocks; i++) {
 			ret = rtw_write8(padapter, (FW_8188E_START_ADDRESS + offset + i), *(buffer + offset + i));
-
 			if (ret == _FAIL)
 				goto exit;
 		}
Signature · 1 line
2.35.1
#7 lore raw

PATCH 5/6 staging: r8188eu: remove unneeded initializations

In reply to Michael Straube

The variables i and offset in block_write() are set in the code before they are used. Remove the unneeded initializations.

---
 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 b4ab050a6f23..1e930799a0b3 100644
@@ -75,7 +75,7 @@ static int block_write(struct adapter *padapter, u8 *buffer, u32 size)
 {
 	int ret = _SUCCESS;
 	u32 blocks, block_size, remain;
-	u32	i = 0, offset = 0;
+	u32 i, offset;
 
 	block_size = MAX_REG_BLOCK_SIZE;
 
Signature · 1 line
2.35.1
#8 lore raw

PATCH 6/6 staging: r8188eu: clean up long lines in block_write()

In reply to Michael Straube

Store the address and the data pointer for calls to rtw_writeN() and rtw_write8() in local variables. This avoids long lines and improves readability.

---
 drivers/staging/r8188eu/core/rtw_fw.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/staging/r8188eu/core/rtw_fw.c +14 -4
index 1e930799a0b3..8620f3c92b52 100644
@@ -75,7 +75,8 @@ static int block_write(struct adapter *padapter, u8 *buffer, u32 size)
 {
 	int ret = _SUCCESS;
 	u32 blocks, block_size, remain;
-	u32 i, offset;
+	u32 i, offset, addr;
+	u8 *data;
 
 	block_size = MAX_REG_BLOCK_SIZE;
 
@@ -83,7 +84,10 @@ static int block_write(struct adapter *padapter, u8 *buffer, u32 size)
 	remain = size % block_size;
 
 	for (i = 0; i < blocks; i++) {
-		ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + i * block_size), block_size, (buffer + i * block_size));
+		addr = FW_8188E_START_ADDRESS + i * block_size;
+		data = buffer + i * block_size;
+
+		ret = rtw_writeN(padapter, addr, block_size, data);
 		if (ret == _FAIL)
 			goto exit;
 	}
@@ -96,7 +100,10 @@ static int block_write(struct adapter *padapter, u8 *buffer, u32 size)
 		remain = remain % block_size;
 
 		for (i = 0; i < blocks; i++) {
-			ret = rtw_writeN(padapter, (FW_8188E_START_ADDRESS + offset + i * block_size), block_size, (buffer + offset + i * block_size));
+			addr = FW_8188E_START_ADDRESS + offset + i * block_size;
+			data = buffer + offset + i * block_size;
+
+			ret = rtw_writeN(padapter, addr, block_size, data);
 			if (ret == _FAIL)
 				goto exit;
 		}
@@ -109,7 +116,10 @@ static int block_write(struct adapter *padapter, u8 *buffer, u32 size)
 		blocks = remain;
 
 		for (i = 0; i < blocks; i++) {
-			ret = rtw_write8(padapter, (FW_8188E_START_ADDRESS + offset + i), *(buffer + offset + i));
+			addr = FW_8188E_START_ADDRESS + offset + i;
+			data = buffer + offset + i;
+
+			ret = rtw_write8(padapter, addr, *data);
 			if (ret == _FAIL)
 				goto exit;
 		}
Signature · 1 line
2.35.1
maintainer
#3 lore raw

Re: [PATCH 2/6] staging: r8188eu: rename parameter of block_write()

In reply to Michael Straube
Quoted 3 lines · Michael Straube

Rename the parameter buffSize of block_write() to avoid camel case.

buffSize -> size

How about "buffSize -> buffer_size"?

That makes more sense, right?

thanks,

greg k-h

maintainer
#4 lore raw

Re: [PATCH 2/6] staging: r8188eu: rename parameter of block_write()

Quoted 8 lines · Greg KH
Quoted 3 lines · Michael Straube

Rename the parameter buffSize of block_write() to avoid camel case.

buffSize -> size

How about "buffSize -> buffer_size"?

That makes more sense, right?

Ah nevermind, it's fine as-is.