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

From: Matti Vaittinen

Date: Wed Nov 18 2026 - 05:31:23 EST


On 17/11/2026 13:03, Arnav Kapoor 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.

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>
Signed-off-by: Arnav Kapoor <kapoorarnav43@xxxxxxxxx>

Reviewed-by: Matti Vaittinen <mazziesaccount@xxxxxxxxx>

Thanks a lot for hunting down and fixing this Arnav!

Yours,
-- Matti

--
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~