[U-Boot] [PATCH] i2c: Fix chip address.0

Joakim Tjernlund Joakim.Tjernlund at transmode.se
Fri Jan 28 09:22:57 CET 2011


According to the i2c command alen 0 should be allowed
but this is not allowed in the implementation, fix.
Also cleanup all cmd_usage cases while at it. It is cleaner
to return cmd_usage(cmdtp); instead of
{
  cmd_usage(cmdtp);
  return 1;
}

Signed-off-by: Joakim Tjernlund <Joakim.Tjernlund at transmode.se>
---
 common/cmd_i2c.c |  107 +++++++++++++++++++++---------------------------------
 1 files changed, 41 insertions(+), 66 deletions(-)

diff --git a/common/cmd_i2c.c b/common/cmd_i2c.c
index 8b9c2c9..a7d0dd8 100644
--- a/common/cmd_i2c.c
+++ b/common/cmd_i2c.c
@@ -88,7 +88,7 @@
  */
 static uchar	i2c_dp_last_chip;
 static uint	i2c_dp_last_addr;
-static uint	i2c_dp_last_alen;
+static int	i2c_dp_last_alen;
 static uint	i2c_dp_last_length = 0x10;
 
 static uchar	i2c_mm_last_chip;
@@ -154,7 +154,7 @@ int i2c_set_bus_speed(unsigned int)
  * get_alen: small parser helper function to get address length
  * returns the address length,or 0 on error
  */
-static uint get_alen(char *arg)
+static int get_alen(char *arg)
 {
 	int	j;
 	int	alen;
@@ -164,7 +164,7 @@ static uint get_alen(char *arg)
 		if (arg[j] == '.') {
 			alen = arg[j+1] - '0';
 			if (alen > 3) {
-				return 0;
+				return -1;
 			}
 			break;
 		} else if (arg[j] == '\0')
@@ -181,13 +181,12 @@ static uint get_alen(char *arg)
 static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 {
 	u_char	chip;
-	uint	devaddr, alen, length;
+	uint	devaddr, length;
+	int	alen;
 	u_char  *memaddr;
 
-	if (argc != 5) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if (argc != 5)
+		return cmd_usage(cmdtp);
 
 	/*
 	 * I2C chip address
@@ -200,10 +199,8 @@ static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 	 */
 	devaddr = simple_strtoul(argv[2], NULL, 16);
 	alen = get_alen(argv[2]);
-	if (alen == 0) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if (alen < 0)
+		return cmd_usage(cmdtp);
 
 	/*
 	 * Length is the number of objects, not number of bytes.
@@ -229,7 +226,8 @@ static int do_i2c_read ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 {
 	u_char	chip;
-	uint	addr, alen, length;
+	uint	addr, length;
+	int	alen;
 	int	j, nbytes, linebytes;
 
 	/* We use the last specified parameters, unless new ones are
@@ -240,10 +238,8 @@ static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 	alen   = i2c_dp_last_alen;
 	length = i2c_dp_last_length;
 
-	if (argc < 3) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if (argc < 3)
+		return cmd_usage(cmdtp);
 
 	if ((flag & CMD_FLAG_REPEAT) == 0) {
 		/*
@@ -261,10 +257,8 @@ static int do_i2c_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 		 */
 		addr = simple_strtoul(argv[2], NULL, 16);
 		alen = get_alen(argv[2]);
-		if (alen == 0) {
-			cmd_usage(cmdtp);
-			return 1;
-		}
+		if (alen < 0)
+			return cmd_usage(cmdtp);
 
 		/*
 		 * If another parameter, it is the length to display.
@@ -328,14 +322,12 @@ static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 {
 	uchar	chip;
 	ulong	addr;
-	uint	alen;
+	int	alen;
 	uchar	byte;
 	int	count;
 
-	if ((argc < 4) || (argc > 5)) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if ((argc < 4) || (argc > 5))
+		return cmd_usage(cmdtp);
 
 	/*
 	 * Chip is always specified.
@@ -347,10 +339,8 @@ static int do_i2c_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 	 */
 	addr = simple_strtoul(argv[2], NULL, 16);
 	alen = get_alen(argv[2]);
-	if (alen == 0) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if (alen < 0)
+		return cmd_usage(cmdtp);
 
 	/*
 	 * Value to write is always specified.
@@ -392,16 +382,14 @@ static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 {
 	uchar	chip;
 	ulong	addr;
-	uint	alen;
+	int	alen;
 	int	count;
 	uchar	byte;
 	ulong	crc;
 	ulong	err;
 
-	if (argc < 4) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if (argc < 4)
+		return cmd_usage(cmdtp);
 
 	/*
 	 * Chip is always specified.
@@ -413,10 +401,8 @@ static int do_i2c_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 	 */
 	addr = simple_strtoul(argv[2], NULL, 16);
 	alen = get_alen(argv[2]);
-	if (alen == 0) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if (alen < 0)
+		return cmd_usage(cmdtp);
 
 	/*
 	 * Count is always specified
@@ -456,16 +442,14 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
 {
 	uchar	chip;
 	ulong	addr;
-	uint	alen;
+	int	alen;
 	ulong	data;
 	int	size = 1;
 	int	nbytes;
 	extern char console_buffer[];
 
-	if (argc != 3) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if (argc != 3)
+		return cmd_usage(cmdtp);
 
 #ifdef CONFIG_BOOT_RETRY_TIME
 	reset_cmd_timeout();	/* got a good command to get here */
@@ -495,10 +479,8 @@ mod_i2c_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
 		 */
 		addr = simple_strtoul(argv[2], NULL, 16);
 		alen = get_alen(argv[2]);
-		if (alen == 0) {
-			cmd_usage(cmdtp);
-			return 1;
-		}
+		if (alen < 0)
+			return cmd_usage(cmdtp);
 	}
 
 	/*
@@ -622,16 +604,14 @@ static int do_i2c_probe (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 {
 	u_char	chip;
-	ulong	alen;
+	int	alen;
 	uint	addr;
 	uint	length;
 	u_char	bytes[16];
 	int	delay;
 
-	if (argc < 3) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if (argc < 3)
+		return cmd_usage(cmdtp);
 
 	/*
 	 * Chip is always specified.
@@ -643,10 +623,8 @@ static int do_i2c_loop(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 	 */
 	addr = simple_strtoul(argv[2], NULL, 16);
 	alen = get_alen(argv[2]);
-	if (alen == 0) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if (alen < 0)
+		return cmd_usage(cmdtp);
 
 	/*
 	 * Length is the number of objects, not number of bytes.
@@ -784,10 +762,9 @@ static int do_sdram (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 		"32 MiB", "16 MiB", "8 MiB", "4 MiB"
 	};
 
-	if (argc < 2) {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	if (argc < 2)
+		return cmd_usage(cmdtp);
+
 	/*
 	 * Chip is always specified.
 	 */
@@ -1322,12 +1299,10 @@ static int do_i2c(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 
 	c = find_cmd_tbl(argv[0], &cmd_i2c_sub[0], ARRAY_SIZE(cmd_i2c_sub));
 
-	if (c) {
+	if (c)
 		return  c->cmd(cmdtp, flag, argc, argv);
-	} else {
-		cmd_usage(cmdtp);
-		return 1;
-	}
+	else
+		return cmd_usage(cmdtp);
 }
 
 /***************************************************/
-- 
1.7.3.4



More information about the U-Boot mailing list