Skip to contentSkip to conversation

i2c: core: ACPI: Properly set status byte to 0 for multi-byte writes

PATCH Applied Hans de Goede 4 participants 5 messages Aug 12, 2018 – Aug 20, 2018

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

PATCH v2 1/2 i2c: core: ACPI: Properly set status byte to 0 for multi-byte writes

acpi_gsb_i2c_write_bytes() returns i2c_transfer()'s return value, which is the number of transfers executed on success, so 1.

The ACPI code expects us to store 0 in gsb->status for success, not 1.

Specifically this breaks the following code in the Thinkpad 8 DSDT:

            ECWR = I2CW = ECWR /* \_SB_.I2C1.BAT0.ECWR */
            If ((ECST == Zero))
            {
                ECRD = I2CR /* \_SB_.I2C1.I2CR */
            }

Before this commit we set ECST to 1, causing the read to never happen breaking battery monitoring on the Thinkpad 8.

This commit makes acpi_gsb_i2c_write_bytes() return 0 when i2c_transfer() returns 1, so the single write transfer completed successfully, and makes it return -EIO on for other (unexpected) return values >= 0.

---
Changes in v2:
-Modify the value which acpi_gsb_i2c_write_bytes() returns instead of
checking + modifying the return value in its caller

---
 drivers/i2c/i2c-core-acpi.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/i2c/i2c-core-acpi.c +8 -3
index 7c3b4740b94b..b8f303dea305 100644
@@ -482,11 +482,16 @@ static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
 	msgs[0].buf = buffer;
 
 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
-	if (ret < 0)
-		dev_err(&client->adapter->dev, "i2c write failed\n");
 
 	kfree(buffer);
-	return ret;
+
+	if (ret < 0) {
+		dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
+		return ret;
+	}
+
+	/* 1 transfer must have completed successfully */
+	return (ret == 1) ? 0 : -EIO;
 }
 
 static acpi_status
Signature · 1 line
2.18.0
#1 lore raw
Quoted 13 lines · Hans de Goede

acpi_gsb_i2c_write_bytes() returns i2c_transfer()'s return value, which is the number of transfers executed on success, so 1.

The ACPI code expects us to store 0 in gsb->status for success, not 1.

Specifically this breaks the following code in the Thinkpad 8 DSDT:

            ECWR = I2CW = ECWR /* \_SB_.I2C1.BAT0.ECWR */
            If ((ECST == Zero))
            {
                ECRD = I2CR /* \_SB_.I2C1.I2CR */
            }
Quoted 6 lines

Before this commit we set ECST to 1, causing the read to never happen breaking battery monitoring on the Thinkpad 8.

This commit makes acpi_gsb_i2c_write_bytes() return 0 when i2c_transfer() returns 1, so the single write transfer completed successfully, and makes it return -EIO on for other (unexpected) return values >= 0.

I'm wondering if this might be fallout from one of
35cd67a0caf7 ("i2c: viperboard: return message count on master_xfer success") de9a8634f1cb ("i2c: pmcmsp: return message count on master_xfer success")

But I have no idea what i2c driver these Thinkpads are using, so it is not unlikely that I'm way off...

Cheers,
Peter

Quoted 6 lines

---
Changes in v2:
-Modify the value which acpi_gsb_i2c_write_bytes() returns instead of
checking + modifying the return value in its caller

#2 lore raw

Hi,

Quoted 17 lines · Peter Rosin
Quoted 16 lines · Hans de Goede

acpi_gsb_i2c_write_bytes() returns i2c_transfer()'s return value, which is the number of transfers executed on success, so 1.

The ACPI code expects us to store 0 in gsb->status for success, not 1.

Specifically this breaks the following code in the Thinkpad 8 DSDT:

             ECWR = I2CW = ECWR /* \_SB_.I2C1.BAT0.ECWR */
             If ((ECST == Zero))
             {
                 ECRD = I2CR /* \_SB_.I2C1.I2CR */
             }

Before this commit we set ECST to 1, causing the read to never happen breaking battery monitoring on the Thinkpad 8.

Quoted 7 lines
Quoted 3 lines

This commit makes acpi_gsb_i2c_write_bytes() return 0 when i2c_transfer() returns 1, so the single write transfer completed successfully, and makes it return -EIO on for other (unexpected) return values >= 0.

I'm wondering if this might be fallout from one of
35cd67a0caf7 ("i2c: viperboard: return message count on master_xfer success") de9a8634f1cb ("i2c: pmcmsp: return message count on master_xfer success")

No, these are using the designware driver, so these commits are not the cause, this simply is a long standing bug.

Regards,

Hans

Acked-by on v2 1/2
#3 lore raw
In reply to Hans de Goede
Quoted 16 lines · Hans de Goede

acpi_gsb_i2c_write_bytes() returns i2c_transfer()'s return value, which is the number of transfers executed on success, so 1.

The ACPI code expects us to store 0 in gsb->status for success, not 1.

Specifically this breaks the following code in the Thinkpad 8 DSDT:

            ECWR = I2CW = ECWR /* \_SB_.I2C1.BAT0.ECWR */
            If ((ECST == Zero))
            {
                ECRD = I2CR /* \_SB_.I2C1.I2CR */
            }

Before this commit we set ECST to 1, causing the read to never happen breaking battery monitoring on the Thinkpad 8.

Quoted 6 lines

This commit makes acpi_gsb_i2c_write_bytes() return 0 when i2c_transfer() returns 1, so the single write transfer completed successfully, and makes it return -EIO on for other (unexpected) return values >= 0.

Applied
#4 lore raw
In reply to Hans de Goede
Quoted 16 lines · Hans de Goede

acpi_gsb_i2c_write_bytes() returns i2c_transfer()'s return value, which is the number of transfers executed on success, so 1.

The ACPI code expects us to store 0 in gsb->status for success, not 1.

Specifically this breaks the following code in the Thinkpad 8 DSDT:

            ECWR = I2CW = ECWR /* \_SB_.I2C1.BAT0.ECWR */
            If ((ECST == Zero))
            {
                ECRD = I2CR /* \_SB_.I2C1.I2CR */
            }

Before this commit we set ECST to 1, causing the read to never happen breaking battery monitoring on the Thinkpad 8.

Quoted 6 lines

This commit makes acpi_gsb_i2c_write_bytes() return 0 when i2c_transfer() returns 1, so the single write transfer completed successfully, and makes it return -EIO on for other (unexpected) return values >= 0.

Applied to for-next, thanks!