mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Arnav Kapoor <kapoorarnav43@gmail.com>
Cc: "Mudit Sharma" <muditsharma.info@gmail.com>,
	"Andy Shevchenko" <andriy.shevchenko@intel.com>,
	"Matti Vaittinen" <mazziesaccount@gmail.com>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] iio: light: bh1745: fix 16-bit register accesses
Date: Sun, 22 Nov 2026 00:35:38 +0000	[thread overview]
Message-ID: <20261122003538.507b6800@jic23-hlaptop> (raw)
In-Reply-To: <20261118104534.159876-1-kapoorarnav43@gmail.com>

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


      reply	other threads:[~2026-11-22  0:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 10:45 Arnav Kapoor
2026-11-22  0:35 ` Jonathan Cameron [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261122003538.507b6800@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=kapoorarnav43@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mazziesaccount@gmail.com \
    --cc=muditsharma.info@gmail.com \
    --cc=nuno.sa@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®