Patchwork [v3,3/6] tmp105: Fix I2C protocol bug

login
register
mail settings
Submitter Andreas Färber
Date Jan. 7, 2013, 10:42 p.m.
Message ID <1357598544-4804-4-git-send-email-andreas.faerber@web.de>
Download mbox | patch
Permalink /patch/210262/
State New
Headers show

Comments

Andreas Färber - Jan. 7, 2013, 10:42 p.m.
An early length postincrement in the TMP105's I2C TX path led to
transfers of more than one byte to place the second byte in the third
byte's place within the buffer and the third byte to get discarded.

Fix this by explictly incrementing the length after the checks but
before the callback is called, which again checks the length.

Adjust the Coding Style while at it.

Signed-off-by: Alex Horn <alex.horn@cs.ox.ac.uk>
Signed-off-by: Andreas Färber <andreas.faerber@web.de>
Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
---
 hw/tmp105.c |    9 ++++++---
 1 Datei geändert, 6 Zeilen hinzugefügt(+), 3 Zeilen entfernt(-)

Patch

diff --git a/hw/tmp105.c b/hw/tmp105.c
index 9c67e64..ff9f28b 100644
--- a/hw/tmp105.c
+++ b/hw/tmp105.c
@@ -153,11 +153,14 @@  static int tmp105_tx(I2CSlave *i2c, uint8_t data)
 {
     TMP105State *s = (TMP105State *) i2c;
 
-    if (!s->len ++)
+    if (s->len == 0) {
         s->pointer = data;
-    else {
-        if (s->len <= 2)
+        s->len++;
+    } else {
+        if (s->len <= 2) {
             s->buf[s->len - 1] = data;
+        }
+        s->len++;
         tmp105_write(s);
     }