Skip to contentSkip to conversation

i2c: diolan-u2c: Use get_unaligned_le32() to read serial number

PATCH Superseded Felix Huang 2 participants 4 messages Nov 28

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 i2c: diolan-u2c: Use get_unaligned_le32() to read serial number

Sparse reports:

drivers/i2c/busses/i2c-diolan-u2c.c:290:26: warning:
cast to restricted __le32

Replace the explicit cast and le32_to_cpu() conversion with get_unaligned_le32(). This preserves the existing conversion semantics and silences the sparse warning.

---
Changes in v2:
- Reword the commit message to accurately describe the motivation.
- Do not claim that the existing access may be unaligned; the
serial-number buffer is naturally aligned.
- Focus on fixing the sparse "cast to restricted __le32" warning.
- Link to v1: https://patch.msgid.link/[email protected]

To: Guenter Roeck <[email protected]>
To: Andi Shyti <[email protected]>
Cc: [email protected]

---
 drivers/i2c/busses/i2c-diolan-u2c.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-diolan-u2c.c +3 -3
index d502f6e47..24db6d26c 100644
@@ -16,6 +16,7 @@
 #include <linux/slab.h>
 #include <linux/usb.h>
 #include <linux/i2c.h>
+#include <linux/unaligned.h>
 
 #define DRIVER_NAME		"i2c-diolan-u2c"
 
@@ -283,13 +284,12 @@ static void diolan_fw_version(struct i2c_diolan_u2c *dev)
 static void diolan_get_serial(struct i2c_diolan_u2c *dev)
 {
 	int ret;
-	u32 serial;
 
 	ret = diolan_usb_cmd(dev, CMD_GET_SERIAL, true);
 	if (ret >= 4) {
-		serial = le32_to_cpu(*(u32 *)dev->ibuffer);
 		dev_info(&dev->interface->dev,
-			 "Diolan U2C serial number %u\n", serial);
+			 "Diolan U2C serial number %u\n",
+			 get_unaligned_le32(dev->ibuffer));
 	}
 }
 

---
base-commit: f4585067150b85e33f60876149a6e742928da75e
change-id: 20261127-i2c-diolan-u2c-unaligned-2599ede88a92

Best regards,

Signature · 1 line
Felix Huang <[email protected]>
maintainer
#1 lore raw
Quoted 11 lines · On 11/27/26 23:06, Felix Huang via B4 Relay wrote:

From: Felix Huang <[email protected]>

Sparse reports:

drivers/i2c/busses/i2c-diolan-u2c.c:290:26: warning:
cast to restricted __le32

Replace the explicit cast and le32_to_cpu() conversion with get_unaligned_le32(). This preserves the existing conversion semantics and silences the sparse warning.

Quoted 6 lines

---
Changes in v2:
- Reword the commit message to accurately describe the motivation.
- Do not claim that the existing access may be unaligned; the serial-number buffer is naturally aligned.

That does not match the reality of subject, description, and code.

Guenter

Quoted 46 lines

- Focus on fixing the sparse "cast to restricted __le32" warning.
- Link to v1: https://patch.msgid.link/[email protected]

To: Guenter Roeck <[email protected]>
To: Andi Shyti <[email protected]>
Cc: [email protected]

---
  drivers/i2c/busses/i2c-diolan-u2c.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-diolan-u2c.c +3 -4
index d502f6e47..24db6d26c 100644
@@ -16,6 +16,7 @@
  #include <linux/slab.h>
  #include <linux/usb.h>
  #include <linux/i2c.h>
+#include <linux/unaligned.h>
 
  #define DRIVER_NAME		"i2c-diolan-u2c"
 
@@ -283,13 +284,12 @@ static void diolan_fw_version(struct i2c_diolan_u2c *dev)
  static void diolan_get_serial(struct i2c_diolan_u2c *dev)
  {
  	int ret;
-	u32 serial;
 
  	ret = diolan_usb_cmd(dev, CMD_GET_SERIAL, true);
  	if (ret >= 4) {
-		serial = le32_to_cpu(*(u32 *)dev->ibuffer);
  		dev_info(&dev->interface->dev,
-			 "Diolan U2C serial number %u\n", serial);
+			 "Diolan U2C serial number %u\n",
+			 get_unaligned_le32(dev->ibuffer));
  	}
  }
 
 
---

base-commit: f4585067150b85e33f60876149a6e742928da75e
change-id: 20261127-i2c-diolan-u2c-unaligned-2599ede88a92

Best regards,
--
Felix Huang <[email protected]>

maintainer
#2 lore raw
In reply to Felix Huang
Quoted 11 lines · On 11/27/26 23:06, Felix Huang via B4 Relay wrote:

From: Felix Huang <[email protected]>

Sparse reports:

drivers/i2c/busses/i2c-diolan-u2c.c:290:26: warning:
cast to restricted __le32

Replace the explicit cast and le32_to_cpu() conversion with get_unaligned_le32(). This preserves the existing conversion semantics and silences the sparse warning.

I had a closer look.

This is pure nonsense, sorry. The real difference is that the parameter of get_unaligned_le32() is unrestricted, so sparse doesn't blow up on it. This is not an argument for using an unaligned access function.

The problem is the use of the "*(u32 *)" type cast. The parameter of le32_to_cpu() is restricted and must be __le32, so sparse complains. Try replacing "u32" with "__le32" instead, and leave the 'serial' variable alone.

Guenter

Quoted 52 lines

---
Changes in v2:
- Reword the commit message to accurately describe the motivation.
- Do not claim that the existing access may be unaligned; the
serial-number buffer is naturally aligned.
- Focus on fixing the sparse "cast to restricted __le32" warning.
- Link to v1: https://patch.msgid.link/[email protected]

To: Guenter Roeck <[email protected]>
To: Andi Shyti <[email protected]>
Cc: [email protected]

---
  drivers/i2c/busses/i2c-diolan-u2c.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-diolan-u2c.c +3 -4
index d502f6e47..24db6d26c 100644
@@ -16,6 +16,7 @@
  #include <linux/slab.h>
  #include <linux/usb.h>
  #include <linux/i2c.h>
+#include <linux/unaligned.h>
 
  #define DRIVER_NAME		"i2c-diolan-u2c"
 
@@ -283,13 +284,12 @@ static void diolan_fw_version(struct i2c_diolan_u2c *dev)
  static void diolan_get_serial(struct i2c_diolan_u2c *dev)
  {
  	int ret;
-	u32 serial;
 
  	ret = diolan_usb_cmd(dev, CMD_GET_SERIAL, true);
  	if (ret >= 4) {
-		serial = le32_to_cpu(*(u32 *)dev->ibuffer);
  		dev_info(&dev->interface->dev,
-			 "Diolan U2C serial number %u\n", serial);
+			 "Diolan U2C serial number %u\n",
+			 get_unaligned_le32(dev->ibuffer));
  	}
  }
 
 
---

base-commit: f4585067150b85e33f60876149a6e742928da75e
change-id: 20261127-i2c-diolan-u2c-unaligned-2599ede88a92

Best regards,
--
Felix Huang <[email protected]>

#3 lore raw

Thanks for taking a closer look and for the explanation.

You are right, I misunderstood the cause of the sparse warning.

I will send a v3 that keeps the serial variable, removes the get_unaligned_le32() change, and casts the buffer to __le32 before passing it to le32_to_cpu(), as you suggested.

Sorry for the confusion.

Best regards,
Felix

Quoted 79 lines · Guenter Roeck
Quoted 11 lines · On 11/27/26 23:06, Felix Huang via B4 Relay wrote:

From: Felix Huang <[email protected]>

Sparse reports:

drivers/i2c/busses/i2c-diolan-u2c.c:290:26: warning:
cast to restricted __le32

Replace the explicit cast and le32_to_cpu() conversion with get_unaligned_le32(). This preserves the existing conversion semantics and silences the sparse warning.

I had a closer look.

This is pure nonsense, sorry. The real difference is that the parameter of get_unaligned_le32() is unrestricted, so sparse doesn't blow up on it. This is not an argument for using an unaligned access function.

The problem is the use of the "*(u32 *)" type cast. The parameter of le32_to_cpu() is restricted and must be __le32, so sparse complains. Try replacing "u32" with "__le32" instead, and leave the 'serial' variable alone.

Guenter

Quoted 52 lines

---
Changes in v2:
- Reword the commit message to accurately describe the motivation.
- Do not claim that the existing access may be unaligned; the
serial-number buffer is naturally aligned.
- Focus on fixing the sparse "cast to restricted __le32" warning.
- Link to v1: https://patch.msgid.link/[email protected]

To: Guenter Roeck <[email protected]>
To: Andi Shyti <[email protected]>
Cc: [email protected]

---
  drivers/i2c/busses/i2c-diolan-u2c.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-diolan-u2c.c +3 -4
index d502f6e47..24db6d26c 100644
@@ -16,6 +16,7 @@
  #include <linux/slab.h>
  #include <linux/usb.h>
  #include <linux/i2c.h>
+#include <linux/unaligned.h>
 
  #define DRIVER_NAME         "i2c-diolan-u2c"
 
@@ -283,13 +284,12 @@ static void diolan_fw_version(struct i2c_diolan_u2c *dev)
  static void diolan_get_serial(struct i2c_diolan_u2c *dev)
  {
      int ret;
-     u32 serial;
 
      ret = diolan_usb_cmd(dev, CMD_GET_SERIAL, true);
      if (ret >= 4) {
-             serial = le32_to_cpu(*(u32 *)dev->ibuffer);
              dev_info(&dev->interface->dev,
-                      "Diolan U2C serial number %u\n", serial);
+                      "Diolan U2C serial number %u\n",
+                      get_unaligned_le32(dev->ibuffer));
      }
  }
 
 
---

base-commit: f4585067150b85e33f60876149a6e742928da75e
change-id: 20261127-i2c-diolan-u2c-unaligned-2599ede88a92

Best regards,
--
Felix Huang <[email protected]>