Skip to contentSkip to conversation

gpio: pca953x: remove incorrect le16_to_cpu calls

PATCH In review Andy Shevchenko 3 participants 4 messages Jul 18, 2017 – Jul 19, 2017 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 gpio: pca953x: remove incorrect le16_to_cpu calls

i2c_smbus commands handle the correct byte order for smbus transactions internally. This will currently result in incorrect operation on big endian systems.

---
- add RB tag

 drivers/gpio/gpio-pca953x.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpio/gpio-pca953x.c +3 -5
index 4c9e21300a26..1b9dbf691ae7 100644
@@ -187,10 +187,9 @@ static int pca953x_write_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
 
 static int pca953x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
 {
-	__le16 word = cpu_to_le16(get_unaligned((u16 *)val));
+	u16 word = get_unaligned((u16 *)val);
 
-	return i2c_smbus_write_word_data(chip->client,
-					 reg << 1, (__force u16)word);
+	return i2c_smbus_write_word_data(chip->client, reg << 1, word);
 }
 
 static int pca957x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
@@ -241,8 +240,7 @@ static int pca953x_read_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
 	int ret;
 
 	ret = i2c_smbus_read_word_data(chip->client, reg << 1);
-	val[0] = (u16)ret & 0xFF;
-	val[1] = (u16)ret >> 8;
+	put_unaligned(ret, (u16 *)val);
 
 	return ret;
 }
Signature · 1 line
2.11.0
#1 lore raw

Hi Andy,

Quoted 2 lines · Andy Shevchenko

i2c_smbus commands handle the correct byte order for smbus transactions

I am not sure of this claim, I didn't find any cpu_to_lexx in the sbmus function.

Quoted 2 lines

internally. This will currently result in incorrect operation on big endian systems.

Did you managed to test this driver in a big endian system and to check that with this patch the driver worked as expected?

Thanks,

Gregory

Quoted 39 lines

---
- add RB tag

 drivers/gpio/gpio-pca953x.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpio/gpio-pca953x.c +3 -5
index 4c9e21300a26..1b9dbf691ae7 100644
@@ -187,10 +187,9 @@ static int pca953x_write_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
 
 static int pca953x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
 {
-	__le16 word = cpu_to_le16(get_unaligned((u16 *)val));
+	u16 word = get_unaligned((u16 *)val);
 
-	return i2c_smbus_write_word_data(chip->client,
-					 reg << 1, (__force u16)word);
+	return i2c_smbus_write_word_data(chip->client, reg << 1, word);
 }
 
 static int pca957x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
@@ -241,8 +240,7 @@ static int pca953x_read_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
 	int ret;
 
 	ret = i2c_smbus_read_word_data(chip->client, reg << 1);
-	val[0] = (u16)ret & 0xFF;
-	val[1] = (u16)ret >> 8;
+	put_unaligned(ret, (u16 *)val);
 
 	return ret;
 }
Signature · 1 line
2.11.0
Signature · 4 lines
Gregory Clement, Free Electrons
Kernel, drivers, real-time and embedded Linux
development, consulting, training and support.
http://free-electrons.com
#2 lore raw
Quoted 11 lines · Gregory CLEMENT

Hi Andy,

Quoted 2 lines · Andy Shevchenko

i2c_smbus commands handle the correct byte order for smbus transactions

I am not sure of this claim, I didn't find any cpu_to_lexx in the sbmus
function.

Of course you didn't (I didn't either on the first glance, then I read the code more carefully).

It does that byte-by-byte.

That explains why people do not notice it AFAIU Sebastian's claim.

Quoted 7 lines
Quoted 3 lines

internally. This will currently result in incorrect operation on big endian systems.

Did you managed to test this driver in a big endian system and to check
that with this patch the driver worked as expected?

Unfortunately no. I have big endian systems, though I have no such device connected to them.

Quoted 54 lines

Thanks,

Gregory

Quoted 46 lines

---
- add RB tag
 drivers/gpio/gpio-pca953x.c | 8 +++-----

 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpio/gpio- renamed from drivers/gpio/gpio-pca953x.c +0 -0

pca953x.c
index 4c9e21300a26..1b9dbf691ae7 100644

drivers/gpio/gpio-pca953x.c +0 -0
@@ -187,10 +187,9 @@ static int pca953x_write_regs_8(struct
 pca953x_chip *chip, int reg, u8 *val)


 static int pca953x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
 {

drivers/gpio/gpio-pca953x.c +1 -1
-	__le16 word = cpu_to_le16(get_unaligned((u16 *)val));
+	u16 word = get_unaligned((u16 *)val);

drivers/gpio/gpio-pca953x.c +0 -2
-	return i2c_smbus_write_word_data(chip->client,
-					 reg << 1, (__force

u16)word);

drivers/gpio/gpio-pca953x.c +1 -0
+	return i2c_smbus_write_word_data(chip->client, reg << 1,

word);
 }

 static int pca957x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)

drivers/gpio/gpio-pca953x.c +0 -0
@@ -241,8 +240,7 @@ static int pca953x_read_regs_16(struct
 pca953x_chip *chip, int reg, u8 *val)
  	int ret;


  ret = i2c_smbus_read_word_data(chip->client, reg << 1);

drivers/gpio/gpio-pca953x.c +1 -2
-	val[0] = (u16)ret & 0xFF;
-	val[1] = (u16)ret >> 8;
+	put_unaligned(ret, (u16 *)val);


  return ret;
 }
--
2.11.0

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
Reviewed-by
#3 lore raw
In reply to Gregory CLEMENT

G'day Gregory,

Quoted 9 lines · Gregory CLEMENT

Hi Andy,

Quoted 2 lines · Andy Shevchenko

i2c_smbus commands handle the correct byte order for smbus transactions

I am not sure of this claim, I didn't find any cpu_to_lexx in the sbmus function.

For example see: i2c_smbus_xfer_emulated
The I2C_SMBUS_WORD_DATA case. byte copies are done to do the cpu to le conversion.

     msgbuf0[1] = data->word & 0xff;
     msgbuf0[2] = data->word >> 8;

The patch looks good to me.

Quoted 52 lines
Quoted 3 lines

internally. This will currently result in incorrect operation on big endian systems.

Did you managed to test this driver in a big endian system and to check that with this patch the driver worked as expected?

Thanks,

Gregory

Quoted 39 lines

---
- add RB tag

  drivers/gpio/gpio-pca953x.c | 8 +++-----
  1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpio/gpio-pca953x.c +3 -5
index 4c9e21300a26..1b9dbf691ae7 100644
@@ -187,10 +187,9 @@ static int pca953x_write_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
 
  static int pca953x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
  {
-	__le16 word = cpu_to_le16(get_unaligned((u16 *)val));
+	u16 word = get_unaligned((u16 *)val);
 
-	return i2c_smbus_write_word_data(chip->client,
-					 reg << 1, (__force u16)word);
+	return i2c_smbus_write_word_data(chip->client, reg << 1, word);
  }
 
  static int pca957x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
@@ -241,8 +240,7 @@ static int pca953x_read_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
  	int ret;
 
  	ret = i2c_smbus_read_word_data(chip->client, reg << 1);
-	val[0] = (u16)ret & 0xFF;
-	val[1] = (u16)ret >> 8;
+	put_unaligned(ret, (u16 *)val);
 
  	return ret;
  }
Signature · 1 line
2.11.0
Signature · 11 lines
Regards
Phil Reid

ElectroMagnetic Imaging Technology Pty Ltd
Development of Geophysical Instrumentation & Software
www.electromag.com.au

3 The Avenue, Midland WA 6056, AUSTRALIA
Ph: +61 8 9250 8100
Fax: +61 8 9250 7100
Email: [email protected]