Skip to contentSkip to conversation

fix i2c_pca_pf_waitforcompletion() return value

PATCH In review Yegor Yefremov 2 participants 3 messages Sep 21, 2010 – Sep 22, 2010 2 messages not in the archive I2C subsystem host drivers

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fix i2c_pca_pf_waitforcompletion() return value

ret is still -1, if during the polling read_byte() returns at once with I2C_PCA_CON_SI set. So ret > 0 would lead i2c_pca_pf_waitforcompletion() to return 0, in spite of the proper behavior.

Solution: get rid of ret and make immediate return where possible.

drivers/i2c/busses/i2c-pca-platform.c +10 -7
Index: b/drivers/i2c/busses/i2c-pca-platform.c
===================================================================
@@ -80,23 +80,26 @@
 static int i2c_pca_pf_waitforcompletion(void *pd)
 {
 	struct i2c_pca_pf_data *i2c = pd;
-	long ret = ~0;
 	unsigned long timeout;
 
 	if (i2c->irq) {
-		ret = wait_event_timeout(i2c->wait,
+		return wait_event_timeout(i2c->wait,
 			i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
 			& I2C_PCA_CON_SI, i2c->adap.timeout);
 	} else {
 		/* Do polling */
 		timeout = jiffies + i2c->adap.timeout;
-		while (((i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
-				& I2C_PCA_CON_SI) == 0)
-				&& (ret = time_before(jiffies, timeout)))
-			udelay(100);
+		while (!(i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
+				& I2C_PCA_CON_SI))
+		{
+			if(time_before(jiffies, timeout))
+				udelay(100);
+			else
+				return 0;
+		}
 	}
 
-	return ret > 0;
+	return ~0;
 }
 
 static void i2c_pca_pf_dummyreset(void *pd)
#2 lore raw
In reply to a message not in the archive

Hello Yegor,

Quoted 37 lines · Yegor Yefremov

ret is still -1, if during the polling read_byte() returns at once with I2C_PCA_CON_SI set. So ret > 0 would lead i2c_pca_pf_waitforcompletion() to return 0, in spite of the proper behavior.

Solution: get rid of ret and make immediate return where possible.

Index: b/drivers/i2c/busses/i2c-pca-platform.c
===================================================================

drivers/i2c/busses/i2c-pca-platform.c +6 -6
@@ -80,23 +80,26 @@
 static int i2c_pca_pf_waitforcompletion(void *pd)
 {
 	struct i2c_pca_pf_data *i2c = pd;
-	long ret = ~0;
 	unsigned long timeout;
 
 	if (i2c->irq) {
-		ret = wait_event_timeout(i2c->wait,
+		return wait_event_timeout(i2c->wait,
 			i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
 			& I2C_PCA_CON_SI, i2c->adap.timeout);
 	} else {
 		/* Do polling */
 		timeout = jiffies + i2c->adap.timeout;
-		while (((i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
-				& I2C_PCA_CON_SI) == 0)
-				&& (ret = time_before(jiffies, timeout)))
-			udelay(100);
+		while (!(i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
+				& I2C_PCA_CON_SI))
+		{
+			if(time_before(jiffies, timeout))
+				udelay(100);

drivers/i2c/busses/i2c-pca-platform.c line 105

Quoted 10 lines
drivers/i2c/busses/i2c-pca-platform.c +4 -1
+			else
+				return 0;
+		}
 	}
 
-	return ret > 0;
+	return ~0;
 }
 
 static void i2c_pca_pf_dummyreset(void *pd)

Hmm, I think I'd prefer a centralized exit point. What about this? I think it might be easier to read. Only compile tested, though. The final patch needs to be applied to the isa version, too. BTW, good debugging there, fixing this old issue you reported. Thanks a lot!

Regards,

Wolfram

drivers/i2c/busses/i2c-pca-platform.c +7 -4
index ef5c784..a13aecb 100644
@@ -80,8 +80,8 @@ static void i2c_pca_pf_writebyte32(void *pd, int reg, int val)
 static int i2c_pca_pf_waitforcompletion(void *pd)
 {
 	struct i2c_pca_pf_data *i2c = pd;
-	long ret = ~0;
 	unsigned long timeout;
+	long ret;
 
 	if (i2c->irq) {
 		ret = wait_event_timeout(i2c->wait,
@@ -90,10 +90,13 @@ static int i2c_pca_pf_waitforcompletion(void *pd)
 	} else {
 		/* Do polling */
 		timeout = jiffies + i2c->adap.timeout;
-		while (((i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
-				& I2C_PCA_CON_SI) == 0)
-				&& (ret = time_before(jiffies, timeout)))
+		do {
+			ret = time_before(jiffies, timeout);
+			if (i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
+				& I2C_PCA_CON_SI)
+				break;
 			udelay(100);
+		} while (ret);
 	}
 
 	return ret > 0;
Signature · 2 lines
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#4 lore raw
In reply to a message not in the archive

Hello Wolfram,

Quoted 4 lines

Hmm, I think I'd prefer a centralized exit point. What about this? I think it might be easier to read. Only compile tested, though. The final patch needs to be applied to the isa version, too. BTW, good debugging there, fixing this old issue you reported. Thanks a lot!

Everything is working as expected. I'll send the whole patch for both isa and platform flavor. I think we should then change the patch name to something like:

fix PCA waitforcompletion() return value

Best regards,
Yegor