Skip to contentSkip to conversation

mfd: Correct WM8350 I2C return code usage

PATCH In review Mark Brown 3 participants 6 messages Nov 10, 2008 – Nov 16, 2008

Subsystems (2)
lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/1 mfd: Correct WM8350 I2C return code usage

The vendor BSP used for the WM8350 development provided an I2C driver which incorrectly returned zero on succesful sends rather than the number of transmitted bytes, an error which was then propagated into the WM8350 I2C accessors.

---
 drivers/mfd/wm8350-i2c.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
drivers/mfd/wm8350-i2c.c +13 -2
index 8dfe21b..3e0ce0e 100644
@@ -30,7 +30,12 @@ static int wm8350_i2c_read_device(struct wm8350 *wm8350, char reg,
 	ret = i2c_master_send(wm8350->i2c_client, &reg, 1);
 	if (ret < 0)
 		return ret;
-	return i2c_master_recv(wm8350->i2c_client, dest, bytes);
+	ret = i2c_master_recv(wm8350->i2c_client, dest, bytes);
+	if (ret < 0)
+		return ret;
+	if (ret != bytes)
+		return -EIO;
+	return 0;
 }
 
 static int wm8350_i2c_write_device(struct wm8350 *wm8350, char reg,
@@ -38,13 +43,19 @@ static int wm8350_i2c_write_device(struct wm8350 *wm8350, char reg,
 {
 	/* we add 1 byte for device register */
 	u8 msg[(WM8350_MAX_REGISTER << 1) + 1];
+	int ret;
 
 	if (bytes > ((WM8350_MAX_REGISTER << 1) + 1))
 		return -EINVAL;
 
 	msg[0] = reg;
 	memcpy(&msg[1], src, bytes);
-	return i2c_master_send(wm8350->i2c_client, msg, bytes + 1);
+	ret = i2c_master_send(wm8350->i2c_client, msg, bytes + 1);
+	if (ret < 0)
+		return ret;
+	if (ret != bytes + 1)
+		return -EIO;
+	return 0;
 }
 
 static int wm8350_i2c_probe(struct i2c_client *i2c,
Signature · 1 line
1.5.6.5
#1 lore raw

Hi Mark,

Quoted 4 lines · Mark Brown

The vendor BSP used for the WM8350 development provided an I2C driver which incorrectly returned zero on succesful sends rather than the number of transmitted bytes, an error which was then propagated into the WM8350 I2C accessors.

Shouldnt we fix the accessors behaviour instead ?
Currently, that would mean fixing some of the wm8350-core static functions. Slightly bigger patch, but that would keep the i2c interface consistent. What do you think ?

Cheers,
Samuel.

Quoted 47 lines
---
 drivers/mfd/wm8350-i2c.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
drivers/mfd/wm8350-i2c.c +13 -2
index 8dfe21b..3e0ce0e 100644
@@ -30,7 +30,12 @@ static int wm8350_i2c_read_device(struct wm8350 *wm8350, char reg,
 	ret = i2c_master_send(wm8350->i2c_client, &reg, 1);
 	if (ret < 0)
 		return ret;
-	return i2c_master_recv(wm8350->i2c_client, dest, bytes);
+	ret = i2c_master_recv(wm8350->i2c_client, dest, bytes);
+	if (ret < 0)
+		return ret;
+	if (ret != bytes)
+		return -EIO;
+	return 0;
 }
 
 static int wm8350_i2c_write_device(struct wm8350 *wm8350, char reg,
@@ -38,13 +43,19 @@ static int wm8350_i2c_write_device(struct wm8350 *wm8350, char reg,
 {
 	/* we add 1 byte for device register */
 	u8 msg[(WM8350_MAX_REGISTER << 1) + 1];
+	int ret;
 
 	if (bytes > ((WM8350_MAX_REGISTER << 1) + 1))
 		return -EINVAL;
 
 	msg[0] = reg;
 	memcpy(&msg[1], src, bytes);
-	return i2c_master_send(wm8350->i2c_client, msg, bytes + 1);
+	ret = i2c_master_send(wm8350->i2c_client, msg, bytes + 1);
+	if (ret < 0)
+		return ret;
+	if (ret != bytes + 1)
+		return -EIO;
+	return 0;
 }
 
 static int wm8350_i2c_probe(struct i2c_client *i2c,
Signature · 1 line
1.5.6.5
Signature · 2 lines
Intel Open Source Technology Centre
http://oss.intel.com/
#2 lore raw
Quoted 10 lines · Samuel Ortiz
Quoted 4 lines · Mark Brown

The vendor BSP used for the WM8350 development provided an I2C driver which incorrectly returned zero on succesful sends rather than the number of transmitted bytes, an error which was then propagated into the WM8350 I2C accessors.

Shouldnt we fix the accessors behaviour instead ?
Currently, that would mean fixing some of the wm8350-core static functions. Slightly bigger patch, but that would keep the i2c interface consistent.

I don't really understand what you mean by "keep the i2c interface consistent" here? The purpose of this abstraction is to abstract away the control interface used to communicate with the chip since it supports both I2C and SPI.

Quoted 1 line

What do you think ?

That would expose the details of the I2C API and the wire format used to access the device over I2C to the WM8350 core which would mean the SPI control code would have to jump through hoops to emulate it. If the device only had I2C control it would be better to just remove this abstraction entirely and use the I2C API directly in the register access functions.

#3 lore raw
Quoted 8 lines · Mark Brown
Quoted 6 lines · Samuel Ortiz
Quoted 4 lines · Mark Brown

The vendor BSP used for the WM8350 development provided an I2C driver which incorrectly returned zero on succesful sends rather than the number of transmitted bytes, an error which was then propagated into the WM8350 I2C accessors.

Quoted 8 lines
Quoted 3 lines

Shouldnt we fix the accessors behaviour instead ?
Currently, that would mean fixing some of the wm8350-core static functions. Slightly bigger patch, but that would keep the i2c interface consistent.

I don't really understand what you mean by "keep the i2c interface consistent" here? The purpose of this abstraction is to abstract away the control interface used to communicate with the chip since it supports both I2C and SPI.

I understand that. I'm just saying that I would prefer wm8350->read_dev() to return the actual bytes read, be it for SPI or I2C. Same for write_dev(), of course.
With this patch you're breaking that expectation because the read|write_dev() callers basically expect it to return 0 when the you've read|written the right number of bytes.
I'd prefer to fix the callers code, so that we keep the expected semantics for your read|write_dev() routines. For example with wm8350_clear_bits():

drivers/mfd/wm8350-core.c +8 -5
index 0d47fb9..12ff3a6 100644
@@ -199,19 +199,22 @@ static int wm8350_write(struct wm8350 *wm8350, u8 reg, int num_regs, u16 *src)
 int wm8350_clear_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
 {
 	u16 data;
-	int err;
+	int err = 0, ret;
 
 	mutex_lock(&io_mutex);
-	err = wm8350_read(wm8350, reg, 1, &data);
-	if (err) {
+	ret = wm8350_read(wm8350, reg, 1, &data);
+	if (ret != 1) {
 		dev_err(wm8350->dev, "read from reg R%d failed\n", reg);
+		err = -EIO;
 		goto out;
 	}
 
 	data &= ~mask;
-	err = wm8350_write(wm8350, reg, 1, &data);
-	if (err)
+	ret = wm8350_write(wm8350, reg, 1, &data);
+	if (ret != 1) {
 		dev_err(wm8350->dev, "write to reg R%d failed\n", reg);
+		err = -EIO;
+	}
 out:
 	mutex_unlock(&io_mutex);
 	return err;
Signature · 2 lines
Intel Open Source Technology Centre
http://oss.intel.com/
#4 lore raw
Quoted 3 lines · Samuel Ortiz

I understand that. I'm just saying that I would prefer wm8350->read_dev() to return the actual bytes read, be it for SPI or I2C. Same for write_dev(), of course.

Hrm. That's never been the case, even with the buggy code since the register address was included in the physical access for at least the writes.

Quoted 2 lines

With this patch you're breaking that expectation because the read|write_dev() I'd prefer to fix the callers code, so that we keep the expected semantics

Just to clarify, when you say "expectation" and "fix" are you talking about your preference that the hardware access functions return the number of bytes read or written or is there something else going on here?

drivers/mfd/wm8350-core.c static int wm8350_write(struct wm8350 *wm8350, u8 reg, int num_regs, u16 *src) · line 208

Quoted 7 lines

for your read|write_dev() routines. For example with wm8350_clear_bits():

drivers/mfd/wm8350-core.c +3 -1
-	if (err) {
+	ret = wm8350_read(wm8350, reg, 1, &data);
+	if (ret != 1) {
 		dev_err(wm8350->dev, "read from reg R%d failed\n", reg);
+		err = -EIO;

Note that wm8350_read() works a level up, only calling the physical read function if the register cache can't satisfy the access. Changing that would be another layer of alteration.

To be honest I'm really not sure it's worth changing this - I'm having a hard time thinking of a user that would be able to do anything useful with a short access. It also seems like it'd be a rather invasive change for an -rc.

#5 lore raw
Quoted 7 lines · Mark Brown
Quoted 2 lines

With this patch you're breaking that expectation because the read|write_dev() I'd prefer to fix the callers code, so that we keep the expected semantics

Just to clarify, when you say "expectation" and "fix" are you talking about your preference that the hardware access functions return the number of bytes read or written or is there something else going on here?

No, definitely just my preference. I personnaly expect this kind of IO routines to return the number of bytes read/written.

Quoted 3 lines

To be honest I'm really not sure it's worth changing this - I'm having a hard time thinking of a user that would be able to do anything useful with a short access.

I agree with that, yes.

Quoted 2 lines

It also seems like it'd be a rather invasive
change for an -rc.

Fair enough, I applied your patch, and sent Linus a pull request.

Cheers,
Samuel.

Signature · 2 lines
Intel Open Source Technology Centre
http://oss.intel.com/