Skip to contentSkip to conversation

iio: light: bh1745: fix 16-bit register accesses

PATCH Superseded Arnav Kapoor 3 participants 9 messages Nov 17 – Nov 19

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: light: bh1745: fix 16-bit register accesses

The 16-bit colour data and threshold registers are little-endian LSB/MSB pairs, but the driver accesses them through regmap_bulk_read() and regmap_bulk_write() with an int or u16 buffer and no byte-order conversion:

- bh1745_read_raw() reads 2 bytes into an uninitialised int, so the upper 16 bits returned to user space are stack contents.
- bh1745_read_thresh() reads 2 bytes straight into *val, which the IIO core leaves uninitialised, with the same result for the event threshold attributes.
- bh1745_write_thresh() writes the first 2 bytes of an int, which are the upper (zero) bytes on big-endian.
- bh1745_trigger_handler() pushes the raw register bytes, which are little-endian, in a u16 to a buffer whose scan type is IIO_CPU, so user space gets the samples byte-swapped on big-endian.

Use an __le16 buffer for all of them and convert with le16_to_cpu() / cpu_to_le16().

Fixes eab35358aae7 ("iio: light: ROHM BH1745 colour sensor")

---
Changes in v2:
- Include asm/byteorder.h for le16_to_cpu() / cpu_to_le16() (Andy)
- Reword the trigger handler bullet: the samples are byte-swapped on big-endian because the scan type is IIO_CPU (Andy)
- Add Andy's Reviewed-by

v1: https://lore.kernel.org/all/[email protected]/

Compile-tested only (W=1, checkpatch --strict clean); I don't have BH1745 hardware.

 drivers/iio/light/bh1745.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
drivers/iio/light/bh1745.c +21 -10
index 2b8ff36071b8..e2c03cd0d1e3 100644
@@ -17,6 +17,8 @@
 #include <linux/bits.h>
 #include <linux/bitfield.h>
 
+#include <asm/byteorder.h>
+
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 #include <linux/iio/trigger.h>
@@ -421,19 +423,20 @@ static int bh1745_read_raw(struct iio_dev *indio_dev,
 			   int *val, int *val2, long mask)
 {
 	struct bh1745_data *data = iio_priv(indio_dev);
+	__le16 value;
 	int ret;
-	int value;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		if (!iio_device_claim_direct(indio_dev))
 			return -EBUSY;
 
-		ret = regmap_bulk_read(data->regmap, chan->address, &value, 2);
+		ret = regmap_bulk_read(data->regmap, chan->address, &value,
+				       sizeof(value));
 		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
-		*val = value;
+		*val = le16_to_cpu(value);
 
 		return IIO_VAL_INT;
 
@@ -503,6 +506,7 @@ static int bh1745_read_thresh(struct iio_dev *indio_dev,
 			      enum iio_event_info info, int *val, int *val2)
 {
 	struct bh1745_data *data = iio_priv(indio_dev);
+	__le16 value;
 	int ret;
 
 	switch (info) {
@@ -510,18 +514,22 @@ static int bh1745_read_thresh(struct iio_dev *indio_dev,
 		switch (dir) {
 		case IIO_EV_DIR_RISING:
 			ret = regmap_bulk_read(data->regmap, BH1745_TH_LSB,
-					       val, 2);
+					       &value, sizeof(value));
 			if (ret)
 				return ret;
 
+			*val = le16_to_cpu(value);
+
 			return IIO_VAL_INT;
 
 		case IIO_EV_DIR_FALLING:
 			ret = regmap_bulk_read(data->regmap, BH1745_TL_LSB,
-					       val, 2);
+					       &value, sizeof(value));
 			if (ret)
 				return ret;
 
+			*val = le16_to_cpu(value);
+
 			return IIO_VAL_INT;
 
 		default:
@@ -547,6 +555,7 @@ static int bh1745_write_thresh(struct iio_dev *indio_dev,
 			       enum iio_event_info info, int val, int val2)
 {
 	struct bh1745_data *data = iio_priv(indio_dev);
+	__le16 value;
 	int ret;
 
 	switch (info) {
@@ -554,10 +563,12 @@ static int bh1745_write_thresh(struct iio_dev *indio_dev,
 		if (val < 0x0 || val > 0xFFFF)
 			return -EINVAL;
 
+		value = cpu_to_le16(val);
+
 		switch (dir) {
 		case IIO_EV_DIR_RISING:
 			ret = regmap_bulk_write(data->regmap, BH1745_TH_LSB,
-						&val, 2);
+						&value, sizeof(value));
 			if (ret)
 				return ret;
 
@@ -565,7 +576,7 @@ static int bh1745_write_thresh(struct iio_dev *indio_dev,
 
 		case IIO_EV_DIR_FALLING:
 			ret = regmap_bulk_write(data->regmap, BH1745_TL_LSB,
-						&val, 2);
+						&value, sizeof(value));
 			if (ret)
 				return ret;
 
@@ -741,18 +752,18 @@ static irqreturn_t bh1745_trigger_handler(int interrupt, void *p)
 		u16 chans[4];
 		aligned_s64 timestamp;
 	} scan = { };
-	u16 value;
+	__le16 value;
 	int ret;
 	int i;
 	int j = 0;
 
 	iio_for_each_active_channel(indio_dev, i) {
 		ret = regmap_bulk_read(data->regmap, BH1745_RED_LSB + 2 * i,
-				       &value, 2);
+				       &value, sizeof(value));
 		if (ret)
 			goto err;
 
-		scan.chans[j++] = value;
+		scan.chans[j++] = le16_to_cpu(value);
 	}
 
 	iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan),

base-commit: a6d9114f68c4b5507b2d945bd6d1e37415f913c5

Signature · 1 line
2.53.0
#1 lore raw

+ Cc: Matti (even he is not listed as maintainer) for possible another pair of eyes and/or having an answer why this was missed at the time of the driver submission (from my perspective this patch is correct).

Quoted 162 lines · Arnav Kapoor

The 16-bit colour data and threshold registers are little-endian LSB/MSB pairs, but the driver accesses them through regmap_bulk_read() and regmap_bulk_write() with an int or u16 buffer and no byte-order conversion:

- bh1745_read_raw() reads 2 bytes into an uninitialised int, so the upper 16 bits returned to user space are stack contents.
- bh1745_read_thresh() reads 2 bytes straight into *val, which the IIO core leaves uninitialised, with the same result for the event threshold attributes.
- bh1745_write_thresh() writes the first 2 bytes of an int, which are the upper (zero) bytes on big-endian.
- bh1745_trigger_handler() pushes the raw register bytes, which are little-endian, in a u16 to a buffer whose scan type is IIO_CPU, so user space gets the samples byte-swapped on big-endian.

Use an __le16 buffer for all of them and convert with le16_to_cpu() / cpu_to_le16().

Fixes eab35358aae7 ("iio: light: ROHM BH1745 colour sensor")

---
Changes in v2:
- Include asm/byteorder.h for le16_to_cpu() / cpu_to_le16() (Andy)
- Reword the trigger handler bullet: the samples are byte-swapped on big-endian because the scan type is IIO_CPU (Andy)
- Add Andy's Reviewed-by

v1: https://lore.kernel.org/all/[email protected]/

Compile-tested only (W=1, checkpatch --strict clean); I don't have BH1745 hardware.

 drivers/iio/light/bh1745.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
drivers/iio/light/bh1745.c +21 -10
index 2b8ff36071b8..e2c03cd0d1e3 100644
@@ -17,6 +17,8 @@
 #include <linux/bits.h>
 #include <linux/bitfield.h>
 
+#include <asm/byteorder.h>
+
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 #include <linux/iio/trigger.h>
@@ -421,19 +423,20 @@ static int bh1745_read_raw(struct iio_dev *indio_dev,
 			   int *val, int *val2, long mask)
 {
 	struct bh1745_data *data = iio_priv(indio_dev);
+	__le16 value;
 	int ret;
-	int value;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
 		if (!iio_device_claim_direct(indio_dev))
 			return -EBUSY;
 
-		ret = regmap_bulk_read(data->regmap, chan->address, &value, 2);
+		ret = regmap_bulk_read(data->regmap, chan->address, &value,
+				       sizeof(value));
 		iio_device_release_direct(indio_dev);
 		if (ret)
 			return ret;
-		*val = value;
+		*val = le16_to_cpu(value);
 
 		return IIO_VAL_INT;
 
@@ -503,6 +506,7 @@ static int bh1745_read_thresh(struct iio_dev *indio_dev,
 			      enum iio_event_info info, int *val, int *val2)
 {
 	struct bh1745_data *data = iio_priv(indio_dev);
+	__le16 value;
 	int ret;
 
 	switch (info) {
@@ -510,18 +514,22 @@ static int bh1745_read_thresh(struct iio_dev *indio_dev,
 		switch (dir) {
 		case IIO_EV_DIR_RISING:
 			ret = regmap_bulk_read(data->regmap, BH1745_TH_LSB,
-					       val, 2);
+					       &value, sizeof(value));
 			if (ret)
 				return ret;
 
+			*val = le16_to_cpu(value);
+
 			return IIO_VAL_INT;
 
 		case IIO_EV_DIR_FALLING:
 			ret = regmap_bulk_read(data->regmap, BH1745_TL_LSB,
-					       val, 2);
+					       &value, sizeof(value));
 			if (ret)
 				return ret;
 
+			*val = le16_to_cpu(value);
+
 			return IIO_VAL_INT;
 
 		default:
@@ -547,6 +555,7 @@ static int bh1745_write_thresh(struct iio_dev *indio_dev,
 			       enum iio_event_info info, int val, int val2)
 {
 	struct bh1745_data *data = iio_priv(indio_dev);
+	__le16 value;
 	int ret;
 
 	switch (info) {
@@ -554,10 +563,12 @@ static int bh1745_write_thresh(struct iio_dev *indio_dev,
 		if (val < 0x0 || val > 0xFFFF)
 			return -EINVAL;
 
+		value = cpu_to_le16(val);
+
 		switch (dir) {
 		case IIO_EV_DIR_RISING:
 			ret = regmap_bulk_write(data->regmap, BH1745_TH_LSB,
-						&val, 2);
+						&value, sizeof(value));
 			if (ret)
 				return ret;
 
@@ -565,7 +576,7 @@ static int bh1745_write_thresh(struct iio_dev *indio_dev,
 
 		case IIO_EV_DIR_FALLING:
 			ret = regmap_bulk_write(data->regmap, BH1745_TL_LSB,
-						&val, 2);
+						&value, sizeof(value));
 			if (ret)
 				return ret;
 
@@ -741,18 +752,18 @@ static irqreturn_t bh1745_trigger_handler(int interrupt, void *p)
 		u16 chans[4];
 		aligned_s64 timestamp;
 	} scan = { };
-	u16 value;
+	__le16 value;
 	int ret;
 	int i;
 	int j = 0;
 
 	iio_for_each_active_channel(indio_dev, i) {
 		ret = regmap_bulk_read(data->regmap, BH1745_RED_LSB + 2 * i,
-				       &value, 2);
+				       &value, sizeof(value));
 		if (ret)
 			goto err;
 
-		scan.chans[j++] = value;
+		scan.chans[j++] = le16_to_cpu(value);
 	}
 
 	iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan),

base-commit: a6d9114f68c4b5507b2d945bd6d1e37415f913c5

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 5 lines · Andy Shevchenko

+ Cc: Matti (even he is not listed as maintainer) for possible another pair of eyes and/or having an answer why this was missed at the time of the driver submission (from my perspective this patch is correct).

On Tue, Nov 17, 2026 at 04:33:25PM +0530, Arnav Kapoor wrote:

...

Quoted 2 lines
Quoted 2 lines

#include <linux/bits.h>
#include <linux/bitfield.h>

Looking again at this, I just realise that we also need

+ types.h here.

Quoted 2 lines
Quoted 2 lines

+#include <asm/byteorder.h>

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
In reply to Andy Shevchenko
Quoted 3 lines · Andy Shevchenko

+ Cc: Matti (even he is not listed as maintainer) for possible another pair of eyes and/or having an answer why this was missed at the time of the driver submission (from my perspective this patch is correct).

Thanks for the heads-up Andy. Unfortunately, I have never worked with this IC, and I don't think we have it in the ROHM Finland office (but I'll check this just in case. It'd be nice to have this covered in my test setup).

What comes to missing this in reviews... Well, I suppose the reviewers are just humans, or if they're not, then they might have concentrated on other important things, like styling, instead of the real functionality.

On my own behalf, I was away for 1.5 months when the series was applied, and I only looked at the GTS-helpers usage when I did my shallow and badly belated review. (Furthermore, I am 100% human, so I might have just missed this even if I had all the time in the world to do the review).

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#4 lore raw
Quoted 9 lines · Matti Vaittinen
Quoted 3 lines · Andy Shevchenko

+ Cc: Matti (even he is not listed as maintainer) for possible another pair of eyes and/or having an answer why this was missed at the time of the driver submission (from my perspective this patch is correct).

Thanks for the heads-up Andy. Unfortunately, I have never worked with this IC, and I don't think we have it in the ROHM Finland office (but I'll check this just in case. It'd be nice to have this covered in my test setup).

Quoted 8 lines

What comes to missing this in reviews... Well, I suppose the reviewers are just humans, or if they're not, then they might have concentrated on other important things, like styling, instead of the real functionality.

On my own behalf, I was away for 1.5 months when the series was applied, and I only looked at the GTS-helpers usage when I did my shallow and badly belated review. (Furthermore, I am 100% human, so I might have just missed this even if I had all the time in the world to do the review).

Yeah, I don't know how ROHM is big, but in other companies usually there is an internal mailing list is set for reviews. Maybe that helps?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 14 lines · Andy Shevchenko
Quoted 13 lines · Matti Vaittinen
Quoted 3 lines · Andy Shevchenko

+ Cc: Matti (even he is not listed as maintainer) for possible another pair of eyes and/or having an answer why this was missed at the time of the driver submission (from my perspective this patch is correct).

Thanks for the heads-up Andy. Unfortunately, I have never worked with this IC, and I don't think we have it in the ROHM Finland office (but I'll check this just in case. It'd be nice to have this covered in my test setup).

What comes to missing this in reviews... Well, I suppose the reviewers are just humans, or if they're not, then they might have concentrated on other important things, like styling, instead of the real functionality.

Quoted 7 lines
Quoted 4 lines

On my own behalf, I was away for 1.5 months when the series was applied, and I only looked at the GTS-helpers usage when I did my shallow and badly belated review. (Furthermore, I am 100% human, so I might have just missed this even if I had all the time in the world to do the review).

Yeah, I don't know how ROHM is big, but in other companies usually there is an internal mailing list is set for reviews. Maybe that helps?

I took a look to the Wikipedia. ROHM isn't that big. 2017 it has been around 21k people, and I have a feeling (no facts) that size is still about the same.

List might help, if we had more than 1 upstream Linux developers... :)

(I actually did request a list when I started at ROHM - and we had something for a couple of years. Unfortunately, I was the only one ever using that. After a company IT-change accidentally killed the list I never requested a new one. Still, even when we had the list, the bd1745 patches wouldn't probably have emerged to it. The get_maintainers.pl folks use to get relevant reviewers, wouldn't have known to add that list for a new driver with no 'rohm' -prefix.)

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#6 lore raw
Quoted 20 lines · Matti Vaittinen
Quoted 19 lines · Andy Shevchenko
Quoted 17 lines · Matti Vaittinen
Quoted 3 lines · Andy Shevchenko

+ Cc: Matti (even he is not listed as maintainer) for possible another pair of eyes and/or having an answer why this was missed at the time of the driver submission (from my perspective this patch is correct).

Thanks for the heads-up Andy. Unfortunately, I have never worked with this IC, and I don't think we have it in the ROHM Finland office (but I'll check this just in case. It'd be nice to have this covered in my test setup).

What comes to missing this in reviews... Well, I suppose the reviewers are just humans, or if they're not, then they might have concentrated on other important things, like styling, instead of the real functionality.

On my own behalf, I was away for 1.5 months when the series was applied, and I only looked at the GTS-helpers usage when I did my shallow and badly belated review. (Furthermore, I am 100% human, so I might have just missed this even if I had all the time in the world to do the review).

Quoted 8 lines
Quoted 2 lines

Yeah, I don't know how ROHM is big, but in other companies usually there is an internal mailing list is set for reviews. Maybe that helps?

I took a look to the Wikipedia. ROHM isn't that big. 2017 it has been around 21k
people, and I have a feeling (no facts) that size is still about the same.

List might help, if we had more than 1 upstream Linux developers... :)

I see. Let's survive with the current tools!

Quoted 7 lines

(I actually did request a list when I started at ROHM - and we had something for a couple of years. Unfortunately, I was the only one ever using that. After a company IT-change accidentally killed the list I never requested a new one. Still, even when we had the list, the bd1745 patches wouldn't probably have emerged to it. The get_maintainers.pl folks use to get relevant reviewers, wouldn't have known to add that list for a new driver with no 'rohm' -prefix.)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v2 1/1
#7 lore raw
In reply to Arnav Kapoor
Quoted 16 lines · Arnav Kapoor

The 16-bit colour data and threshold registers are little-endian LSB/MSB pairs, but the driver accesses them through regmap_bulk_read() and regmap_bulk_write() with an int or u16 buffer and no byte-order conversion:

- bh1745_read_raw() reads 2 bytes into an uninitialised int, so the upper 16 bits returned to user space are stack contents.
- bh1745_read_thresh() reads 2 bytes straight into *val, which the IIO core leaves uninitialised, with the same result for the event threshold attributes.
- bh1745_write_thresh() writes the first 2 bytes of an int, which are the upper (zero) bytes on big-endian.
- bh1745_trigger_handler() pushes the raw register bytes, which are little-endian, in a u16 to a buffer whose scan type is IIO_CPU, so user space gets the samples byte-swapped on big-endian.

Quoted 6 lines

Use an __le16 buffer for all of them and convert with le16_to_cpu() / cpu_to_le16().

Fixes eab35358aae7 ("iio: light: ROHM BH1745 colour sensor")

Thanks a lot for hunting down and fixing this Arnav!

Yours,
-- Matti

Signature · 5 lines
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~
#8 lore raw
Quoted 3 lines · Matti Vaittinen

Thanks a lot for hunting down and fixing this Arnav!

Thank you for the review, Matti!

I'll send a v3 with the linux/types.h include Andy asked for, and I'll carry your tag.

Thanks,
Arnav