Skip to contentSkip to conversation

i2c: bug fix for

PATCH New Mark A. Greer 2 participants 5 messages Sep 2, 2005 – Sep 7, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: bug fix for

When an i2c transfer is successful, an incorrect value may be returned. This patch fixes that.

--

drivers/i2c/busses/i2c-mv64xxx.c +1 -1
diff -Nur linux-2.6.13-mm1/drivers/i2c/busses/i2c-mv64xxx.c linux-2.6.13-mm1-mag/drivers/i2c/busses/i2c-mv64xxx.c
@@ -429,7 +429,7 @@
 		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) != 0)
 			break;
 
-	return rc;
+	return (rc < 0) ? rc : num;
 }
 
 static struct i2c_algorithm mv64xxx_i2c_algo = {
#1 lore raw

PATCH 1/1 i2c: bug fix for

Hi Mark,

Quoted 2 lines

When an i2c transfer is successful, an incorrect value may be returned. This patch fixes that.

I would make this "is returned", as I can't think of a non-degenerated successful case where a correct value would be returned.

drivers/i2c/busses/i2c-mv64xxx.c line 435

Quoted 12 lines

diff -Nur linux-2.6.13-mm1/drivers/i2c/busses/i2c-mv64xxx.c linux-2.6.13-mm1-mag/drivers/i2c/busses/i2c-mv64xxx.c

drivers/i2c/busses/i2c-mv64xxx.c +1 -1
@@ -429,7 +429,7 @@
 		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) != 0)
 			break;
 
-	return rc;
+	return (rc < 0) ? rc : num;
 }
 
 static struct i2c_algorithm mv64xxx_i2c_algo = {

Looks less than optimal to me. What about:

When an i2c transfer is successful, an incorrect value is returned. This patch fixes that.

 drivers/i2c/busses/i2c-mv64xxx.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-mv64xxx.c +4 -4
@@ -423,13 +423,13 @@
 mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
 {
 	struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
-	int	i, rc = 0;
+	int	i, rc;
 
 	for (i=0; i<num; i++)
-		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) != 0)
-			break;
+		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) < 0)
+			return rc;
 
-	return rc;
+	return num;
 }
 
 static struct i2c_algorithm mv64xxx_i2c_algo = {

Note that the change from "!= 0" to "< 0" is purely for safety, as mv64xxx_i2c_execute_msg is not supposed to return stricly positive values anyway.

I found that i2c-algo-sgi is incorrect and returns 0 on success too. I'll send a patch.

Also, i2c-algo-ite is too broken to comment on but is certainly not correct; is anyone really using this thing?

As a last note, I really wonder why this strange return convention was chosen in the first place. I can't think of a case where maxter_xfer would transfer less messages that it was asked to, and would still not want to return an error.

Thanks,

Signature · 1 line
Jean Delvare
#2 lore raw

PATCH 1/1 i2c: bug fix for

In reply to Mark A. Greer

Hi Jean,

Quoted 19 lines · Jean Delvare

Hi Mark,

Quoted 2 lines

When an i2c transfer is successful, an incorrect value may be returned. This patch fixes that.

I would make this "is returned", as I can't think of a non-degenerated successful case where a correct value would be returned.

Quoted 11 lines

diff -Nur linux-2.6.13-mm1/drivers/i2c/busses/i2c-mv64xxx.c linux-2.6.13-mm1-mag/drivers/i2c/busses/i2c-mv64xxx.c

drivers/i2c/busses/i2c-mv64xxx.c +1 -1
@@ -429,7 +429,7 @@
 		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) != 0)
 			break;
 
-	return rc;
+	return (rc < 0) ? rc : num;
 }
 
Quoted 6 lines

drivers/i2c/busses/i2c-mv64xxx.c line 435

Quoted 1 line
drivers/i2c/busses/i2c-mv64xxx.c +0 -0
 static struct i2c_algorithm mv64xxx_i2c_algo = {

Looks less than optimal to me. What about:

When an i2c transfer is successful, an incorrect value is returned. This patch fixes that.

Yes, that's better.

Quoted 27 lines
 drivers/i2c/busses/i2c-mv64xxx.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-mv64xxx.c +4 -4
@@ -423,13 +423,13 @@
 mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
 {
 	struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
-	int	i, rc = 0;
+	int	i, rc;
 
 	for (i=0; i<num; i++)
-		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) != 0)
-			break;
+		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) < 0)
+			return rc;
 
-	return rc;
+	return num;
 }
 
 static struct i2c_algorithm mv64xxx_i2c_algo = {
Quoted 3 lines

Note that the change from "!= 0" to "< 0" is purely for safety, as mv64xxx_i2c_execute_msg is not supposed to return stricly positive values anyway.

Agreed. However, I think we still need to init 'rc' to 0 in case 'num' is ever 0 to prevent random data from being returned.

Thanks for taking time for this.

Mark

#3 lore raw

PATCH 1/1 i2c: bug fix for

In reply to Mark A. Greer

Hi Mark,

Quoted 12 lines
Quoted 12 lines

mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) {

(quoted diff) +3 -3
 	struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
-	int	i, rc = 0;
+	int	i, rc;
 
 	for (i=0; i<num; i++)
-		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) != 0)
-			break;
+		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) < 0)
+			return rc;
Quoted 6 lines

drivers/i2c/busses/i2c-mv64xxx.c line 433

Quoted 3 lines
drivers/i2c/busses/i2c-mv64xxx.c +1 -1
-	return rc;
+	return num;
 }

Agreed. However, I think we still need to init 'rc' to 0 in case 'num' is ever 0 to prevent random data from being returned.

If num is 0, the for loop is never entered and num, not rc, is returned. Thus I don't see why initializing rc to 0 would be needed. Or am I missing something obvious?

Signature · 1 line
Jean Delvare
#4 lore raw

PATCH 1/1 i2c: bug fix for

In reply to Mark A. Greer
Quoted 21 lines · Jean Delvare

Hi Mark,

Quoted 12 lines
Quoted 12 lines

mv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) {

(quoted diff) +3 -3
 	struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
-	int	i, rc = 0;
+	int	i, rc;
 
 	for (i=0; i<num; i++)
-		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) != 0)
-			break;
+		if ((rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[i])) < 0)
+			return rc;
Quoted 6 lines

drivers/i2c/busses/i2c-mv64xxx.c line 433

Quoted 3 lines
drivers/i2c/busses/i2c-mv64xxx.c +1 -1
-	return rc;
+	return num;
 }

Agreed. However, I think we still need to init 'rc' to 0 in case 'num' is ever 0 to prevent random data from being returned.

Quoted 3 lines

If num is 0, the for loop is never entered and num, not rc, is returned. Thus I don't see why initializing rc to 0 would be needed. Or am I missing something obvious?

Ahh, nope, I missed something obvious.
Your patch looks good to me.

Mark