Skip to contentSkip to conversation

i2c: Fix SMBus read transactions to avoid double events

PATCH Superseded [email protected] 3 participants 4 messages Jun 27, 2016 – Jun 28, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: Fix SMBus read transactions to avoid double events

From: Corey Minyard <[email protected]>

Change 2293c27faddf (i2c: implement broadcast write) added broadcast capability to the I2C bus, but it broke SMBus read transactions. An SMBus read transaction does two i2c_start_transaction() calls without an intervening i2c_end_transfer() call. This will result in i2c_start_transfer() adding the same device to the current_devs list twice, and then the ->event() for the same device gets called twice in the second call to i2c_start_transfer(), resulting in the smbus code getting confused.

This fix adds a third state to the i2c_start_transfer() recv parameter, a read continued that will not scan for devices and add them to current_devs. It also adds #defines for all the values for the recv parameter.

This also deletes the empty check from the top of i2c_end_transfer(). It's unnecessary, and it prevents the broadcast variable from being set to false at the end of the transaction if no devices were on the bus.

Cc KONRAD Frederic <[email protected]>
Cc Alistair Francis <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Peter Maydell <[email protected]>
---
 hw/i2c/core.c        | 24 +++++++++++-------------
 hw/i2c/smbus.c       | 22 +++++++++++-----------
 include/hw/i2c/i2c.h |  9 +++++++++
 3 files changed, 31 insertions(+), 24 deletions(-)

I considered a couple of ways to do this. I thought about adding a separate function to do a "intermediate end" of the transaction, but that seemed like too much work. I also thought about adding a bool saing whether you are currently in a transaction and not rescan the bus if you are. However, that would require that the bool be in the vmstate, and that would be complicated.

On that note, the current_devs list is not in the vmstate. That means that a migrate done in the middle of an I2C transaction will cause the I2C transaction to fail, right? Maybe this whole broadcast thing is a bad idea, or needs a different implementation?

hw/i2c/core.c +11 -13
index abb3efb..53069dd 100644
@@ -101,15 +101,17 @@ int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
         bus->broadcast = true;
     }
 
-    QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
-        DeviceState *qdev = kid->child;
-        I2CSlave *candidate = I2C_SLAVE(qdev);
-        if ((candidate->address == address) || (bus->broadcast)) {
-            node = g_malloc(sizeof(struct I2CNode));
-            node->elt = candidate;
-            QLIST_INSERT_HEAD(&bus->current_devs, node, next);
-            if (!bus->broadcast) {
-                break;
+    if (recv != I2C_START_CONTINUED_READ_TRANSFER) {
+        QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
+            DeviceState *qdev = kid->child;
+            I2CSlave *candidate = I2C_SLAVE(qdev);
+            if ((candidate->address == address) || (bus->broadcast)) {
+                node = g_malloc(sizeof(struct I2CNode));
+                node->elt = candidate;
+                QLIST_INSERT_HEAD(&bus->current_devs, node, next);
+                if (!bus->broadcast) {
+                    break;
+                }
             }
         }
     }
@@ -134,10 +136,6 @@ void i2c_end_transfer(I2CBus *bus)
     I2CSlaveClass *sc;
     I2CNode *node, *next;
 
-    if (QLIST_EMPTY(&bus->current_devs)) {
-        return;
-    }
-
     QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
         sc = I2C_SLAVE_GET_CLASS(node->elt);
         if (sc->event) {
hw/i2c/smbus.c +11 -11
index 3979b3d..f63799d 100644
@@ -222,7 +222,7 @@ int smbus_receive_byte(I2CBus *bus, uint8_t addr)
 {
     uint8_t data;
 
-    if (i2c_start_transfer(bus, addr, 1)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_READ_TRANSFER)) {
         return -1;
     }
     data = i2c_recv(bus);
@@ -233,7 +233,7 @@ int smbus_receive_byte(I2CBus *bus, uint8_t addr)
 
 int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
 {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, data);
@@ -244,11 +244,11 @@ int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
 int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
 {
     uint8_t data;
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
     data = i2c_recv(bus);
     i2c_nack(bus);
     i2c_end_transfer(bus);
@@ -257,7 +257,7 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
 
 int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
 {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
@@ -269,11 +269,11 @@ int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
 int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
 {
     uint16_t data;
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
     data = i2c_recv(bus);
     data |= i2c_recv(bus) << 8;
     i2c_nack(bus);
@@ -283,7 +283,7 @@ int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
 
 int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data)
 {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
@@ -298,11 +298,11 @@ int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data)
     int len;
     int i;
 
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
     len = i2c_recv(bus);
     if (len > 32) {
         len = 0;
@@ -323,7 +323,7 @@ int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
     if (len > 32)
         len = 32;
 
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
include/hw/i2c/i2c.h +9 -0
index c4085aa..16c910e 100644
@@ -50,6 +50,15 @@ struct I2CSlave
     uint8_t address;
 };
 
+/* For the recv value in i2c_start_transfer.  The first two values
+   correspond to false and true for the recv value.  The third is a
+   special value that is used to tell i2c_start_transfer that this is
+   a continuation of the previous transfer, so don't rescan the bus
+   for devices to send to, continue with the current set of devices. */
+#define I2C_START_WRITE_TRANSFER          0
+#define I2C_START_READ_TRANSFER           1
+#define I2C_START_CONTINUED_READ_TRANSFER 2
+
 I2CBus *i2c_init_bus(DeviceState *parent, const char *name);
 void i2c_set_slave_address(I2CSlave *dev, uint8_t address);
 int i2c_bus_busy(I2CBus *bus);
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 183 lines · On Mon, Jun 27, 2016 at 3:04 PM, <[email protected]> wrote:

From: Corey Minyard <[email protected]>

Change 2293c27faddf (i2c: implement broadcast write) added broadcast capability to the I2C bus, but it broke SMBus read transactions. An SMBus read transaction does two i2c_start_transaction() calls without an intervening i2c_end_transfer() call. This will result in i2c_start_transfer() adding the same device to the current_devs list twice, and then the ->event() for the same device gets called twice in the second call to i2c_start_transfer(), resulting in the smbus code getting confused.

This fix adds a third state to the i2c_start_transfer() recv parameter, a read continued that will not scan for devices and add them to current_devs. It also adds #defines for all the values for the recv parameter.

This also deletes the empty check from the top of i2c_end_transfer(). It's unnecessary, and it prevents the broadcast variable from being set to false at the end of the transaction if no devices were on the bus.

Cc KONRAD Frederic <[email protected]>
Cc Alistair Francis <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Peter Maydell <[email protected]>
---
 hw/i2c/core.c        | 24 +++++++++++-------------
 hw/i2c/smbus.c       | 22 +++++++++++-----------
 include/hw/i2c/i2c.h |  9 +++++++++
 3 files changed, 31 insertions(+), 24 deletions(-)

I considered a couple of ways to do this. I thought about adding a separate function to do a "intermediate end" of the transaction, but that seemed like too much work. I also thought about adding a bool saing whether you are currently in a transaction and not rescan the bus if you are. However, that would require that the bool be in the vmstate, and that would be complicated.

On that note, the current_devs list is not in the vmstate. That means that a migrate done in the middle of an I2C transaction will cause the I2C transaction to fail, right? Maybe this whole broadcast thing is a bad idea, or needs a different implementation?

hw/i2c/core.c +11 -13
index abb3efb..53069dd 100644
@@ -101,15 +101,17 @@ int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
         bus->broadcast = true;
     }
 
-    QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
-        DeviceState *qdev = kid->child;
-        I2CSlave *candidate = I2C_SLAVE(qdev);
-        if ((candidate->address == address) || (bus->broadcast)) {
-            node = g_malloc(sizeof(struct I2CNode));
-            node->elt = candidate;
-            QLIST_INSERT_HEAD(&bus->current_devs, node, next);
-            if (!bus->broadcast) {
-                break;
+    if (recv != I2C_START_CONTINUED_READ_TRANSFER) {
+        QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
+            DeviceState *qdev = kid->child;
+            I2CSlave *candidate = I2C_SLAVE(qdev);
+            if ((candidate->address == address) || (bus->broadcast)) {
+                node = g_malloc(sizeof(struct I2CNode));
+                node->elt = candidate;
+                QLIST_INSERT_HEAD(&bus->current_devs, node, next);
+                if (!bus->broadcast) {
+                    break;
+                }
             }
         }
     }
@@ -134,10 +136,6 @@ void i2c_end_transfer(I2CBus *bus)
     I2CSlaveClass *sc;
     I2CNode *node, *next;
 
-    if (QLIST_EMPTY(&bus->current_devs)) {
-        return;
-    }
-
     QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
         sc = I2C_SLAVE_GET_CLASS(node->elt);
         if (sc->event) {
hw/i2c/smbus.c +11 -11
index 3979b3d..f63799d 100644
@@ -222,7 +222,7 @@ int smbus_receive_byte(I2CBus *bus, uint8_t addr)
 {
     uint8_t data;
 
-    if (i2c_start_transfer(bus, addr, 1)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_READ_TRANSFER)) {
         return -1;
     }
     data = i2c_recv(bus);
@@ -233,7 +233,7 @@ int smbus_receive_byte(I2CBus *bus, uint8_t addr)
 
 int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
 {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, data);
@@ -244,11 +244,11 @@ int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
 int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
 {
     uint8_t data;
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
     data = i2c_recv(bus);
     i2c_nack(bus);
     i2c_end_transfer(bus);
@@ -257,7 +257,7 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
 
 int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
 {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
@@ -269,11 +269,11 @@ int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
 int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
 {
     uint16_t data;
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
     data = i2c_recv(bus);
     data |= i2c_recv(bus) << 8;
     i2c_nack(bus);
@@ -283,7 +283,7 @@ int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
 
 int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data)
 {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
@@ -298,11 +298,11 @@ int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data)
     int len;
     int i;
 
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
     len = i2c_recv(bus);
     if (len > 32) {
         len = 0;
@@ -323,7 +323,7 @@ int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
     if (len > 32)
         len = 32;
 
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
         return -1;
     }
     i2c_send(bus, command);
include/hw/i2c/i2c.h +0 -0
index c4085aa..16c910e 100644

include/hw/i2c/i2c.h struct I2CSlave · line 57

Quoted 8 lines
include/hw/i2c/i2c.h +5 -0
@@ -50,6 +50,15 @@ struct I2CSlave
     uint8_t address;
 };
 
+/* For the recv value in i2c_start_transfer.  The first two values
+   correspond to false and true for the recv value.  The third is a
+   special value that is used to tell i2c_start_transfer that this is
+   a continuation of the previous transfer, so don't rescan the bus
+   for devices to send to, continue with the current set of devices. */

This comment is a little confusing, I don't think you need to explain what the read/write values correspond to but you clear up the explanation about the continued read value. Something like this I like is clearer:

The continued read is a special value that is used to tell the i2c_start_transfer() function that this is a continuation of the previous transfer so we don't rescan the bus for devices to send to and instead just continue with the current set of devices.

Otherwise:

Thanks,

Alistair

Quoted 11 lines
include/hw/i2c/i2c.h +4 -1
+#define I2C_START_WRITE_TRANSFER          0
+#define I2C_START_READ_TRANSFER           1
+#define I2C_START_CONTINUED_READ_TRANSFER 2
+
 I2CBus *i2c_init_bus(DeviceState *parent, const char *name);
 void i2c_set_slave_address(I2CSlave *dev, uint8_t address);
 int i2c_bus_busy(I2CBus *bus);
--

2.7.4

#2 lore raw
Quoted 41 lines · Alistair Francis
Quoted 40 lines · On Mon, Jun 27, 2016 at 3:04 PM, <[email protected]> wrote:

From: Corey Minyard <[email protected]>

Change 2293c27faddf (i2c: implement broadcast write) added broadcast capability to the I2C bus, but it broke SMBus read transactions. An SMBus read transaction does two i2c_start_transaction() calls without an intervening i2c_end_transfer() call. This will result in i2c_start_transfer() adding the same device to the current_devs list twice, and then the ->event() for the same device gets called twice in the second call to i2c_start_transfer(), resulting in the smbus code getting confused.

This fix adds a third state to the i2c_start_transfer() recv parameter, a read continued that will not scan for devices and add them to current_devs. It also adds #defines for all the values for the recv parameter.

This also deletes the empty check from the top of i2c_end_transfer(). It's unnecessary, and it prevents the broadcast variable from being set to false at the end of the transaction if no devices were on the bus.

Cc KONRAD Frederic <[email protected]>
Cc Alistair Francis <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Peter Maydell <[email protected]>
---
  hw/i2c/core.c        | 24 +++++++++++-------------
  hw/i2c/smbus.c       | 22 +++++++++++-----------
  include/hw/i2c/i2c.h |  9 +++++++++
  3 files changed, 31 insertions(+), 24 deletions(-)

I considered a couple of ways to do this. I thought about adding a separate function to do a "intermediate end" of the transaction, but that seemed like too much work. I also thought about adding a bool saing whether you are currently in a transaction and not rescan the bus if you are. However, that would require that the bool be in the vmstate, and that would be complicated.

Quoted 4 lines
Quoted 4 lines

On that note, the current_devs list is not in the vmstate. That means that a migrate done in the middle of an I2C transaction will cause the I2C transaction to fail, right? Maybe this whole broadcast thing is a bad idea, or needs a different implementation?

Looking at it a little closer, migration does appear to be handled correctly. So I'll stick with this patch.

Quoted 151 lines
Quoted 138 lines
hw/i2c/core.c +11 -13
index abb3efb..53069dd 100644
@@ -101,15 +101,17 @@ int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
          bus->broadcast = true;
      }
 
-    QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
-        DeviceState *qdev = kid->child;
-        I2CSlave *candidate = I2C_SLAVE(qdev);
-        if ((candidate->address == address) || (bus->broadcast)) {
-            node = g_malloc(sizeof(struct I2CNode));
-            node->elt = candidate;
-            QLIST_INSERT_HEAD(&bus->current_devs, node, next);
-            if (!bus->broadcast) {
-                break;
+    if (recv != I2C_START_CONTINUED_READ_TRANSFER) {
+        QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
+            DeviceState *qdev = kid->child;
+            I2CSlave *candidate = I2C_SLAVE(qdev);
+            if ((candidate->address == address) || (bus->broadcast)) {
+                node = g_malloc(sizeof(struct I2CNode));
+                node->elt = candidate;
+                QLIST_INSERT_HEAD(&bus->current_devs, node, next);
+                if (!bus->broadcast) {
+                    break;
+                }
              }
          }
      }
@@ -134,10 +136,6 @@ void i2c_end_transfer(I2CBus *bus)
      I2CSlaveClass *sc;
      I2CNode *node, *next;
 
-    if (QLIST_EMPTY(&bus->current_devs)) {
-        return;
-    }
-
      QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
          sc = I2C_SLAVE_GET_CLASS(node->elt);
          if (sc->event) {
hw/i2c/smbus.c +11 -11
index 3979b3d..f63799d 100644
@@ -222,7 +222,7 @@ int smbus_receive_byte(I2CBus *bus, uint8_t addr)
  {
      uint8_t data;
 
-    if (i2c_start_transfer(bus, addr, 1)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_READ_TRANSFER)) {
          return -1;
      }
      data = i2c_recv(bus);
@@ -233,7 +233,7 @@ int smbus_receive_byte(I2CBus *bus, uint8_t addr)
 
  int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
  {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, data);
@@ -244,11 +244,11 @@ int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
  int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
  {
      uint8_t data;
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
      data = i2c_recv(bus);
      i2c_nack(bus);
      i2c_end_transfer(bus);
@@ -257,7 +257,7 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
 
  int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
  {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
@@ -269,11 +269,11 @@ int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t data)
  int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
  {
      uint16_t data;
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
      data = i2c_recv(bus);
      data |= i2c_recv(bus) << 8;
      i2c_nack(bus);
@@ -283,7 +283,7 @@ int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
 
  int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, uint16_t data)
  {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
@@ -298,11 +298,11 @@ int smbus_read_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data)
      int len;
      int i;
 
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
      len = i2c_recv(bus);
      if (len > 32) {
          len = 0;
@@ -323,7 +323,7 @@ int smbus_write_block(I2CBus *bus, uint8_t addr, uint8_t command, uint8_t *data,
      if (len > 32)
          len = 32;
 
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
include/hw/i2c/i2c.h +0 -0
index c4085aa..16c910e 100644

include/hw/i2c/i2c.h struct I2CSlave · line 57

Quoted 8 lines
include/hw/i2c/i2c.h +5 -0
@@ -50,6 +50,15 @@ struct I2CSlave
      uint8_t address;
  };
 
+/* For the recv value in i2c_start_transfer.  The first two values
+   correspond to false and true for the recv value.  The third is a
+   special value that is used to tell i2c_start_transfer that this is
+   a continuation of the previous transfer, so don't rescan the bus
+   for devices to send to, continue with the current set of devices. */

This comment is a little confusing, I don't think you need to explain what the read/write values correspond to but you clear up the explanation about the continued read value. Something like this I like is clearer:

Quoted 4 lines

The continued read is a special value that is used to tell the i2c_start_transfer() function that this is a continuation of the previous transfer so we don't rescan the bus for devices to send to and instead just continue with the current set of devices.

I did think about doing it this way, but it seemed clearer to me the other way. But I think you are right.

-corey

Quoted 19 lines

Otherwise:

Thanks,

Alistair

Quoted 11 lines
include/hw/i2c/i2c.h +4 -1
+#define I2C_START_WRITE_TRANSFER          0
+#define I2C_START_READ_TRANSFER           1
+#define I2C_START_CONTINUED_READ_TRANSFER 2
+
  I2CBus *i2c_init_bus(DeviceState *parent, const char *name);
  void i2c_set_slave_address(I2CSlave *dev, uint8_t address);
  int i2c_bus_busy(I2CBus *bus);
--

2.7.4

#3 lore raw

I just realized that this won't work for true I2C devices. When simulating an SMBus transaction on top of I2C, it will start the transaction twice without an end. So something else will need to be done.

-corey

Quoted 241 lines · Corey Minyard
Quoted 41 lines · Alistair Francis
Quoted 40 lines · On Mon, Jun 27, 2016 at 3:04 PM, <[email protected]> wrote:

From: Corey Minyard <[email protected]>

Change 2293c27faddf (i2c: implement broadcast write) added broadcast capability to the I2C bus, but it broke SMBus read transactions. An SMBus read transaction does two i2c_start_transaction() calls without an intervening i2c_end_transfer() call. This will result in i2c_start_transfer() adding the same device to the current_devs list twice, and then the ->event() for the same device gets called twice in the second call to i2c_start_transfer(), resulting in the smbus code getting confused.

This fix adds a third state to the i2c_start_transfer() recv parameter, a read continued that will not scan for devices and add them to current_devs. It also adds #defines for all the values for the recv parameter.

This also deletes the empty check from the top of i2c_end_transfer(). It's unnecessary, and it prevents the broadcast variable from being set to false at the end of the transaction if no devices were on the bus.

Cc KONRAD Frederic <[email protected]>
Cc Alistair Francis <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Peter Maydell <[email protected]>
---
  hw/i2c/core.c        | 24 +++++++++++-------------
  hw/i2c/smbus.c       | 22 +++++++++++-----------
  include/hw/i2c/i2c.h |  9 +++++++++
  3 files changed, 31 insertions(+), 24 deletions(-)

I considered a couple of ways to do this. I thought about adding a separate function to do a "intermediate end" of the transaction, but that seemed like too much work. I also thought about adding a bool saing whether you are currently in a transaction and not rescan the bus if you are. However, that would require that the bool be in the vmstate, and that would be complicated.

Quoted 4 lines
Quoted 4 lines

On that note, the current_devs list is not in the vmstate. That means that a migrate done in the middle of an I2C transaction will cause the I2C transaction to fail, right? Maybe this whole broadcast thing is a bad idea, or needs a different implementation?

Looking at it a little closer, migration does appear to be handled correctly. So I'll stick with this patch.

Quoted 161 lines
Quoted 155 lines
hw/i2c/core.c +11 -9
index abb3efb..53069dd 100644
@@ -101,15 +101,17 @@ int i2c_start_transfer(I2CBus *bus, uint8_t 
 address, int recv)
          bus->broadcast = true;
      }
 
-    QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
-        DeviceState *qdev = kid->child;
-        I2CSlave *candidate = I2C_SLAVE(qdev);
-        if ((candidate->address == address) || (bus->broadcast)) {
-            node = g_malloc(sizeof(struct I2CNode));
-            node->elt = candidate;
-            QLIST_INSERT_HEAD(&bus->current_devs, node, next);
-            if (!bus->broadcast) {
-                break;
+    if (recv != I2C_START_CONTINUED_READ_TRANSFER) {
+        QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
+            DeviceState *qdev = kid->child;
+            I2CSlave *candidate = I2C_SLAVE(qdev);
+            if ((candidate->address == address) || (bus->broadcast)) {
+                node = g_malloc(sizeof(struct I2CNode));
+                node->elt = candidate;
+                QLIST_INSERT_HEAD(&bus->current_devs, node, next);
+                if (!bus->broadcast) {
+                    break;
+                }
              }
          }
      }
hw/i2c/core.c +0 -4
@@ -134,10 +136,6 @@ void i2c_end_transfer(I2CBus *bus)
      I2CSlaveClass *sc;
      I2CNode *node, *next;
 
-    if (QLIST_EMPTY(&bus->current_devs)) {
-        return;
-    }
-
      QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
          sc = I2C_SLAVE_GET_CLASS(node->elt);
          if (sc->event) {
hw/i2c/smbus.c +4 -4
index 3979b3d..f63799d 100644
@@ -222,7 +222,7 @@ int smbus_receive_byte(I2CBus *bus, uint8_t addr)
  {
      uint8_t data;
 
-    if (i2c_start_transfer(bus, addr, 1)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_READ_TRANSFER)) {
          return -1;
      }
      data = i2c_recv(bus);
@@ -233,7 +233,7 @@ int smbus_receive_byte(I2CBus *bus, uint8_t addr)
 
  int smbus_send_byte(I2CBus *bus, uint8_t addr, uint8_t data)
  {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, data);
@@ -244,11 +244,11 @@ int smbus_send_byte(I2CBus *bus, uint8_t addr, 
 uint8_t data)
  int smbus_read_byte(I2CBus *bus, uint8_t addr, uint8_t command)
  {
      uint8_t data;
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
      data = i2c_recv(bus);
      i2c_nack(bus);
      i2c_end_transfer(bus);
hw/i2c/smbus.c +1 -1
@@ -257,7 +257,7 @@ int smbus_read_byte(I2CBus *bus, uint8_t addr, 
 uint8_t command)
 
  int smbus_write_byte(I2CBus *bus, uint8_t addr, uint8_t command, 
 uint8_t data)
  {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
hw/i2c/smbus.c +2 -2
@@ -269,11 +269,11 @@ int smbus_write_byte(I2CBus *bus, uint8_t 
 addr, uint8_t command, uint8_t data)
  int smbus_read_word(I2CBus *bus, uint8_t addr, uint8_t command)
  {
      uint16_t data;
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
      data = i2c_recv(bus);
      data |= i2c_recv(bus) << 8;
      i2c_nack(bus);
hw/i2c/smbus.c +1 -1
@@ -283,7 +283,7 @@ int smbus_read_word(I2CBus *bus, uint8_t addr, 
 uint8_t command)
 
  int smbus_write_word(I2CBus *bus, uint8_t addr, uint8_t command, 
 uint16_t data)
  {
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
hw/i2c/smbus.c +2 -2
@@ -298,11 +298,11 @@ int smbus_read_block(I2CBus *bus, uint8_t 
 addr, uint8_t command, uint8_t *data)
      int len;
      int i;
 
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
-    i2c_start_transfer(bus, addr, 1);
+    i2c_start_transfer(bus, addr, I2C_START_CONTINUED_READ_TRANSFER);
      len = i2c_recv(bus);
      if (len > 32) {
          len = 0;
hw/i2c/smbus.c +1 -1
@@ -323,7 +323,7 @@ int smbus_write_block(I2CBus *bus, uint8_t addr, 
 uint8_t command, uint8_t *data,
      if (len > 32)
          len = 32;
 
-    if (i2c_start_transfer(bus, addr, 0)) {
+    if (i2c_start_transfer(bus, addr, I2C_START_WRITE_TRANSFER)) {
          return -1;
      }
      i2c_send(bus, command);
include/hw/i2c/i2c.h +5 -0
index c4085aa..16c910e 100644
@@ -50,6 +50,15 @@ struct I2CSlave
      uint8_t address;
  };
 
+/* For the recv value in i2c_start_transfer.  The first two values
+   correspond to false and true for the recv value.  The third is a
+   special value that is used to tell i2c_start_transfer that this is
+   a continuation of the previous transfer, so don't rescan the bus
+   for devices to send to, continue with the current set of 
Quoted 1 line

devices. */

This comment is a little confusing, I don't think you need to explain what the read/write values correspond to but you clear up the explanation about the continued read value. Something like this I like is clearer:

Quoted 4 lines

The continued read is a special value that is used to tell the i2c_start_transfer() function that this is a continuation of the previous transfer so we don't rescan the bus for devices to send to and instead just continue with the current set of devices.

I did think about doing it this way, but it seemed clearer to me the other way. But I think you are right.

-corey

Quoted 19 lines

Otherwise:

Thanks,

Alistair

Quoted 11 lines
include/hw/i2c/i2c.h +4 -0
+#define I2C_START_WRITE_TRANSFER          0
+#define I2C_START_READ_TRANSFER           1
+#define I2C_START_CONTINUED_READ_TRANSFER 2
+
  I2CBus *i2c_init_bus(DeviceState *parent, const char *name);
  void i2c_set_slave_address(I2CSlave *dev, uint8_t address);
  int i2c_bus_busy(I2CBus *bus);
Signature · 1 line
2.7.4