* [PATCH v3] iio: light: bh1745: fix 16-bit register accesses
@ 2026-11-18 10:45 Arnav Kapoor
2026-11-22 0:35 ` Jonathan Cameron
0 siblings, 1 reply; 2+ messages in thread
From: Arnav Kapoor @ 2026-11-18 10:45 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Mudit Sharma, Andy Shevchenko, Matti Vaittinen, David Lechner,
Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel, 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")
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
Changes in v3:
- Include linux/types.h for __le16 (Andy)
- Add Matti's Reviewed-by
- Rebase on the current fixes-togreg
v2: https://lore.kernel.org/all/20261117110327.198084-1-kapoorarnav43@gmail.com/
v1: https://lore.kernel.org/all/20261116194214.45174-1-kapoorarnav43@gmail.com/
Compile-tested only (W=1, checkpatch --strict clean); I don't have
BH1745 hardware.
drivers/iio/light/bh1745.c | 32 ++++++++++++++++++++++----------
1 file changed, 22 insertions(+), 10 deletions(-)
diff --git a/drivers/iio/light/bh1745.c b/drivers/iio/light/bh1745.c
index 2b8ff36071b8..d312ea278f2b 100644
--- a/drivers/iio/light/bh1745.c
+++ b/drivers/iio/light/bh1745.c
@@ -16,6 +16,9 @@
#include <linux/regmap.h>
#include <linux/bits.h>
#include <linux/bitfield.h>
+#include <linux/types.h>
+
+#include <asm/byteorder.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
@@ -421,19 +424,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 +507,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 +515,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 +556,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 +564,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 +577,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 +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));
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
--
2.53.0
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH v3] iio: light: bh1745: fix 16-bit register accesses
2026-11-18 10:45 [PATCH v3] iio: light: bh1745: fix 16-bit register accesses Arnav Kapoor
@ 2026-11-22 0:35 ` Jonathan Cameron
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2026-11-22 0:35 UTC (permalink / raw)
To: Arnav Kapoor
Cc: Mudit Sharma, Andy Shevchenko, Matti Vaittinen, David Lechner,
Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel
On Wed, 18 Nov 2026 16:15:32 +0530
Arnav Kapoor <kapoorarnav43@gmail.com> 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@intel.com>
> Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>
> Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
> @@ -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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-22 0:35 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 10:45 [PATCH v3] iio: light: bh1745: fix 16-bit register accesses Arnav Kapoor
2026-11-22 0:35 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®