Skip to contentSkip to conversation

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

PATCH Superseded Arnav Kapoor 2 participants 4 messages Nov 16 – Nov 17

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 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() stores the raw bytes in a u16 although the scan type is IIO_CPU.

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")

---
Compile-tested only (W=1, checkpatch --strict clean); I don't have BH1745 hardware. On little-endian the raw read currently returns the register value plus whatever was on the stack in the upper 16 bits of the uninitialised int; the threshold read has the same problem through iio_ev_value_show(), whose val is not initialised either.

 drivers/iio/light/bh1745.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
drivers/iio/light/bh1745.c +19 -10
index 2b8ff36071b8..9c14499f848a 100644
@@ -421,19 +421,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 +504,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 +512,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 +553,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 +561,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 +574,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 +750,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
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 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.

Quoted 2 lines

- bh1745_trigger_handler() stores the raw bytes in a u16 although the scan type is IIO_CPU.

I didn't get this, what's the problem here?

Quoted 2 lines

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

Otherwise seems legit to me.

And you also need to add

asm/byteorder.h.

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

- bh1745_trigger_handler() stores the raw bytes in a u16 although the scan type is IIO_CPU.

I didn't get this, what's the problem here?

The handler does regmap_bulk_read(..., &value, 2) into a u16 and puts that straight into scan.chans[], so the buffer gets the register bytes as they come off the bus, LSB first. The channels declare .endianness = IIO_CPU, so user space reads each sample as a native u16. That is right on little-endian, but on big-endian every sample comes out byte-swapped. The patch reads into an __le16 and stores le16_to_cpu(value), so the data matches IIO_CPU on both.

I'll reword that bullet in v2 to say this directly.

Quoted 2 lines

Otherwise seems legit to me.

Thanks!

Quoted 3 lines

And you also need to add

asm/byteorder.h.

Will do in v2.

Thanks,
Arnav

#3 lore raw
Quoted 2 lines · Arnav Kapoor
Quoted 1 line · Andy Shevchenko

On Tue, Nov 17, 2026 at 01:12:10AM +0530, Arnav Kapoor wrote:

...

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

- bh1745_trigger_handler() stores the raw bytes in a u16 although the scan type is IIO_CPU.

I didn't get this, what's the problem here?

The handler does regmap_bulk_read(..., &value, 2) into a u16 and puts

Quoted 8 lines

that straight into scan.chans[], so the buffer gets the register bytes as they come off the bus, LSB first. The channels declare .endianness = IIO_CPU, so user space reads each sample as a native u16. That is right on little-endian, but on big-endian every sample comes out byte-swapped. The patch reads into an __le16 and stores le16_to_cpu(value), so the data matches IIO_CPU on both.

I'll reword that bullet in v2 to say this directly.

Yes, please do.

Signature · 2 lines
With Best Regards,
Andy Shevchenko