[PATCH 4/7] staging/wilc1000: Sanitize linux_spi_write

Janosch Frank kernel at janosch-frank.de
Sun Jan 10 17:48:05 UTC 2016


Removed unneeded newlines.
Moved variable definitions to the top.
Improved control flow to get rid of indents.
Replaced while with for.
Added additional error message and improved existing ones.

Signed-off-by: Janosch Frank <kernel at janosch-frank.de>
---
 drivers/staging/wilc1000/linux_wlan_spi.c | 87 ++++++++++++++-----------------
 1 file changed, 40 insertions(+), 47 deletions(-)

diff --git a/drivers/staging/wilc1000/linux_wlan_spi.c b/drivers/staging/wilc1000/linux_wlan_spi.c
index b0dd486..8ad5084 100644
--- a/drivers/staging/wilc1000/linux_wlan_spi.c
+++ b/drivers/staging/wilc1000/linux_wlan_spi.c
@@ -137,78 +137,71 @@ static void linux_spi_msg_init(struct spi_message *msg, struct spi_transfer *tr,
 
 int linux_spi_write(u8 *b, u32 len)
 {
-	int ret;
+	int ret, i;
+	int blk = len / TXRX_PHASE_SIZE;
+	int remainder = len % TXRX_PHASE_SIZE;
 	struct spi_message msg;
 	struct spi_transfer tr;
 
-	if (len > 0 && b != NULL) {
-		int i = 0;
-		int blk = len / TXRX_PHASE_SIZE;
-		int remainder = len % TXRX_PHASE_SIZE;
-
-		if (blk) {
-			while (i < blk)	{
-				linux_spi_msg_init(&msg, &tr, TXRX_PHASE_SIZE,
-						   b + (i * TXRX_PHASE_SIZE),
-						   NULL);
+	if (!len) {
+		PRINT_ER("Zero length write.\n");
+		return 0;
+	}
 
-				ret = spi_sync(wilc_spi_dev, &msg);
-				if (ret < 0) {
-					PRINT_ER("SPI transaction failed\n");
-				}
-				i++;
+	if (!b) {
+		PRINT_ER("Write buffer NULL.\n");
+		return 0;
+	}
 
-			}
-		}
-		if (remainder) {
-			linux_spi_msg_init(&msg, &tr, remainder,
-					   b + (blk * TXRX_PHASE_SIZE),
-					   NULL);
-
-			ret = spi_sync(wilc_spi_dev, &msg);
-			if (ret < 0) {
-				PRINT_ER("SPI transaction failed\n");
-			}
+	for (i = 0; i < blk; i++) {
+		linux_spi_msg_init(&msg, &tr, TXRX_PHASE_SIZE,
+				   b + (i * TXRX_PHASE_SIZE),
+				   NULL);
+		ret = spi_sync(wilc_spi_dev, &msg);
+		if (ret < 0) {
+			PRINT_ER("SPI sync failed and returned %d.\n", ret);
+			return 0;
 		}
-	} else {
-		PRINT_ER("can't write data with the following length: %d\n", len);
-		PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
-		ret = -1;
+	}
+
+	if (remainder) {
+		linux_spi_msg_init(&msg, &tr, remainder,
+				   b + (blk * TXRX_PHASE_SIZE),
+				   NULL);
+		ret = spi_sync(wilc_spi_dev, &msg);
+		if (ret < 0)
+			PRINT_ER("SPI sync failed and returned %d.\n", ret);
 	}
 
 	/* change return value to match WILC interface */
 	(ret < 0) ? (ret = 0) : (ret = 1);
-
 	return ret;
-
 }
 
 #else
 int linux_spi_write(u8 *b, u32 len)
 {
-
 	int ret;
 	struct spi_message msg;
 	struct spi_transfer tr;
 
-	if (len > 0 && b != NULL) {
-		linux_spi_msg_init(&msg, &tr, len, b, NULL);
-
-		ret = spi_sync(wilc_spi_dev, &msg);
-		if (ret < 0) {
-			PRINT_ER("SPI transaction failed\n");
-		}
+	if (!len) {
+		PRINT_ER("Zero length write.\n");
+		return 0;
+	}
 
-	} else {
-		PRINT_ER("can't write data with the following length: %d\n", len);
-		PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
-		ret = -1;
+	if (!b) {
+		PRINT_ER("Write buffer NULL.\n");
+		return 0;
 	}
 
+	linux_spi_msg_init(&msg, &tr, len, b, NULL);
+	ret = spi_sync(wilc_spi_dev, &msg);
+	if (ret < 0)
+		PRINT_ER("SPI sync failed and returned %d.\n", ret);
+
 	/* change return value to match WILC interface */
 	(ret < 0) ? (ret = 0) : (ret = 1);
-
-
 	return ret;
 }
 
-- 
2.5.0



More information about the devel mailing list