Skip to contentSkip to conversation

i2c: ocores: generate stop condition after timeout in polling mode

PATCH Applied Matthias Schiffer 5 participants 5 messages Apr 13, 2023

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

PATCH v2 1/1 i2c: ocores: generate stop condition after timeout in polling mode

From: Gregor Herburger <[email protected]>

In polling mode, no stop condition is generated after a timeout. This causes SCL to remain low and thereby block the bus. If this happens during a transfer it can cause slaves to misinterpret the subsequent transfer and return wrong values.

To solve this, pass the ETIMEDOUT error up from ocores_process_polling() instead of setting STATE_ERROR directly. The caller is adjusted to call ocores_process_timeout() on error both in polling and in IRQ mode, which will set STATE_ERROR and generate a stop condition.

Fixes 69c8c0c0efa8 ("i2c: ocores: add polling interface")

---

v2: style improvements based on feedback from Federico and Andrew. I went
with a slightly different solution than Andrew suggested to avoid using the ret variable for two different kinds of returns.

 drivers/i2c/busses/i2c-ocores.c | 35 ++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 16 deletions(-)
drivers/i2c/busses/i2c-ocores.c +19 -16
index a0af027db04c1..2e575856c5cd5 100644
@@ -342,18 +342,18 @@ static int ocores_poll_wait(struct ocores_i2c *i2c)
  * ocores_isr(), we just add our polling code around it.
  *
  * It can run in atomic context
+ *
+ * Return: 0 on success, -ETIMEDOUT on timeout
  */
-static void ocores_process_polling(struct ocores_i2c *i2c)
+static int ocores_process_polling(struct ocores_i2c *i2c)
 {
-	while (1) {
-		irqreturn_t ret;
-		int err;
+	irqreturn_t ret;
+	int err = 0;
 
+	while (1) {
 		err = ocores_poll_wait(i2c);
-		if (err) {
-			i2c->state = STATE_ERROR;
+		if (err)
 			break; /* timeout */
-		}
 
 		ret = ocores_isr(-1, i2c);
 		if (ret == IRQ_NONE)
@@ -364,13 +364,15 @@ static void ocores_process_polling(struct ocores_i2c *i2c)
 					break;
 		}
 	}
+
+	return err;
 }
 
 static int ocores_xfer_core(struct ocores_i2c *i2c,
 			    struct i2c_msg *msgs, int num,
 			    bool polling)
 {
-	int ret;
+	int ret = 0;
 	u8 ctrl;
 
 	ctrl = oc_getreg(i2c, OCI2C_CONTROL);
@@ -388,15 +390,16 @@ static int ocores_xfer_core(struct ocores_i2c *i2c,
 	oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START);
 
 	if (polling) {
-		ocores_process_polling(i2c);
+		ret = ocores_process_polling(i2c);
 	} else {
-		ret = wait_event_timeout(i2c->wait,
-					 (i2c->state == STATE_ERROR) ||
-					 (i2c->state == STATE_DONE), HZ);
-		if (ret == 0) {
-			ocores_process_timeout(i2c);
-			return -ETIMEDOUT;
-		}
+		if (wait_event_timeout(i2c->wait,
+				       (i2c->state == STATE_ERROR) ||
+				       (i2c->state == STATE_DONE), HZ) == 0)
+			ret = -ETIMEDOUT;
+	}
+	if (ret) {
+		ocores_process_timeout(i2c);
+		return ret;
 	}
 
 	return (i2c->state == STATE_DONE) ? num : -EIO;
Signature · 4 lines
TQ-Systems GmbH | Mühlstraße 2, Gut Delling | 82229 Seefeld, Germany
Amtsgericht München, HRB 105018
Geschäftsführer: Detlef Schneider, Rüdiger Stahl, Stefan Schneider
http://www.tq-group.com/
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 13 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Matthias" == Matthias Schiffer <[email protected]> writes:

From: Gregor Herburger <[email protected]>
In polling mode, no stop condition is generated after a timeout. This causes SCL to remain low and thereby block the bus. If this happens during a transfer it can cause slaves to misinterpret the subsequent transfer and return wrong values.

To solve this, pass the ETIMEDOUT error up from ocores_process_polling() instead of setting STATE_ERROR directly. The caller is adjusted to call ocores_process_timeout() on error both in polling and in IRQ mode, which will set STATE_ERROR and generate a stop condition.

Quoted 8 lines
Fixes 69c8c0c0efa8 ("i2c: ocores: add polling interface")

---

v2: style improvements based on feedback from Federico and Andrew. I went
with a slightly different solution than Andrew suggested to avoid using the ret variable for two different kinds of returns.

Signature · 1 line
Bye, Peter Korsgaard
maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Matthias Schiffer
Quoted 7 lines · Matthias Schiffer

From: Gregor Herburger <[email protected]>

In polling mode, no stop condition is generated after a timeout. This causes SCL to remain low and thereby block the bus. If this happens during a transfer it can cause slaves to misinterpret the subsequent transfer and return wrong values.

Quoted 8 lines

To solve this, pass the ETIMEDOUT error up from ocores_process_polling() instead of setting STATE_ERROR directly. The caller is adjusted to call ocores_process_timeout() on error both in polling and in IRQ mode, which will set STATE_ERROR and generate a stop condition.

Fixes 69c8c0c0efa8 ("i2c: ocores: add polling interface")

Andrew

Reviewed-by on v2 1/1
#3 lore raw
In reply to Matthias Schiffer
Quoted 15 lines · Matthias Schiffer

From: Gregor Herburger <[email protected]>

In polling mode, no stop condition is generated after a timeout. This causes SCL to remain low and thereby block the bus. If this happens during a transfer it can cause slaves to misinterpret the subsequent transfer and return wrong values.

To solve this, pass the ETIMEDOUT error up from ocores_process_polling() instead of setting STATE_ERROR directly. The caller is adjusted to call ocores_process_timeout() on error both in polling and in IRQ mode, which will set STATE_ERROR and generate a stop condition.

Fixes 69c8c0c0efa8 ("i2c: ocores: add polling interface")
Quoted 8 lines

---

v2: style improvements based on feedback from Federico and Andrew. I went
with a slightly different solution than Andrew suggested to avoid using the ret variable for two different kinds of returns.

drivers/i2c/busses/i2c-ocores.c | 35 ++++++++++++++++++---------------

1 file changed, 19 insertions(+), 16 deletions(-)
Signature · 1 line
Federico Vaga - CERN BE-CEM-EDL
Applied
#4 lore raw
In reply to Matthias Schiffer
Quoted 7 lines · Matthias Schiffer

From: Gregor Herburger <[email protected]>

In polling mode, no stop condition is generated after a timeout. This causes SCL to remain low and thereby block the bus. If this happens during a transfer it can cause slaves to misinterpret the subsequent transfer and return wrong values.

Quoted 8 lines

To solve this, pass the ETIMEDOUT error up from ocores_process_polling() instead of setting STATE_ERROR directly. The caller is adjusted to call ocores_process_timeout() on error both in polling and in IRQ mode, which will set STATE_ERROR and generate a stop condition.

Fixes 69c8c0c0efa8 ("i2c: ocores: add polling interface")

Applied to for-current, thanks everyone!