Re: [PATCH v3] iio: light: bh1745: fix 16-bit register accesses

From: Jonathan Cameron

Date: Sat Nov 21 2026 - 19:35:47 EST


On Wed, 18 Nov 2026 16:15:32 +0530
Arnav Kapoor <kapoorarnav43@xxxxxxxxx> wrote:

> 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.

Why not flip the channel type endian marking to IIO_LE and push them out
as that? Normally I'd think this was risk reduction but either the userspace
code is well behaved in which case all fine. Or it isn't and we are on
a little endian platform. No problem. Or finally it isn't and we are
on a big endian platform. In this case bytes flipped anyway :(

So I think we take no more risk in moving to standard IIO approach
of doing as little as possible in the buffered path and leaving
it to userspace. So I think we should flip the channel type, change then
entry type in scan and push the thing little endian.

The rest looks great and your fix is correct, just a tiny bit
away from what I'd prefer. One other whilst you are here
thing inline.

Jonathan



>
> 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")
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxx>
> Reviewed-by: Matti Vaittinen <mazziesaccount@xxxxxxxxx>
> Signed-off-by: Arnav Kapoor <kapoorarnav43@xxxxxxxxx>

> @@ -741,18 +753,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));

Can just do the bulk read directly into &scan.chans[j++].

> 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: 51b83d4cb0f5c1342868c7aa1edbf0c4867477aa