From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795909808; b=q/V9lJbKKUwh5kmVH4LX17IbAu8QLhwGoRfzWq6ijgHCIpQwqWeZa4pdjv4mvKtdUBIb HjT2h6MzHkGEll0nf3wReD8nwTUQ06BAROQVn/FD7HAup5iXSXoBzKVDQ5rF4VL4mI078 KvxQFKtAEcNs+HX/6+eAWmOW3D126HtyCGOaki3SHS/1rfxHf8hx7H7zFNmwf+ZlA33GX bZkOKveAJwarWx2ZQz4jPmxf2hQVD/4ZlCk2WRXW/9GQhwa8raScVhhw9HxH73+sR5p6E Fvm8ylc+3NNyvWIb4eyAFhBxQiyGROWJyIPcxtWV4Ls9gH6htUBSq+cFuKoTkqb2caA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795909808; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: In-Reply-To:References:X-Mailer:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=fyobUhE9QxokvJroQNB1aaJ9Jh0Y+dKovYH/NYJ9Gek=; b=Fn3tRp4Y/7Tj5vxwacuaWfhS+dPTsqUDve/m52I6P/FfykGYbEEeUs1ZPHJVDYS787ZW V8eGWsT/WkBAc17beHybIex8fL8ellKES0wxQrPuZo6b/F/wj4Qj4hB2sG/Y6PNG9PGVw jdhUqTW/Ez8Z5oNIepBzX7cnuB3TfHNjj/a6L+MWjz6quqtNCgh2O3vOfWQPyI31WhkRa OW/KGDq6jxhYNs76jU4kMEKGHClquYy4AjutLlhYjdst/88+2QgdhgD1FVRdYr2OYGXgM WmF7tm2pjK3c+GUZKSTYg4x88Y/yyAQhYlzsADt27EPdMnA3vXi8nlp3/zlbPZebXxQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YLP17ukq; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C0E5C606A31C; Sat, 28 Nov 2026 23:50:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YLP17ukq Received: by smtp.kernel.org (Postfix) with ESMTPSA id 585151F00897; Sat, 28 Nov 2026 23:50:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795909808; bh=fyobUhE9QxokvJroQNB1aaJ9Jh0Y+dKovYH/NYJ9Gek=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=YLP17ukqKpy1jlRrLJuAjDdc/r/aZQ/KEpI+5SUshwaHpbXTOBBTRf52NAhh3K0yu BIt1MZ5BSXfxjPRgEBK7sAEPwuoXNWmkOoO9DLk025oh05MLQe9u68RaUhiAos0CE9 rdH+K0mClF3UYbLKP9XC5WWEFc4fQJH7ZEx3qeF8OfY9fbCktTz/ZLwl+XvljrJP1I YJQTVg6akdBIIZOctCxx2BeTxR4a3541bOxS14rLL15cizM6PMFIBQoylXi/HoaCCj kOy65h5xF/Q+AztJHUxWRQKqR+z5b6JSoa2VBxUKlt08beD6Cx7ebYsuXtAII4gB1b 3QwL9+/xVIh3Q== Date: Sat, 28 Nov 2026 23:50:02 +0000 From: Jonathan Cameron To: Arnav Kapoor Cc: Mudit Sharma , Andy Shevchenko , Matti Vaittinen , David Lechner , Nuno =?UTF-8?B?U8Oh?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4] iio: light: bh1745: fix 16-bit register accesses Message-ID: <20261128235002.4d2914da@jic23-hlaptop> In-Reply-To: <20261122032415.20042-1-kapoorarnav43@gmail.com> References: <20261122032415.20042-1-kapoorarnav43@gmail.com> X-Mailer: Claws Mail 4.4.0 (GTK 3.24.52; x86_64-pc-linux-gnu) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 22 Nov 2026 08:54:13 +0530 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, to a buffer whose scan type is IIO_CPU, so user space > gets the samples byte-swapped on big-endian. > > Use an __le16 for the sysfs paths and convert with le16_to_cpu() / > cpu_to_le16(). For the buffer, read straight into the scan and mark the > channels IIO_LE, leaving the conversion to user space as usual. > > Fixes: eab35358aae7 ("iio: light: ROHM BH1745 colour sensor") > Reviewed-by: Andy Shevchenko > Reviewed-by: Matti Vaittinen > Signed-off-by: Arnav Kapoor > --- Applied to the fixes-togreg branch of iio.git Thanks, Jonathan > Changes in v4: > - Mark the channels IIO_LE and push the samples little-endian instead > of converting them in the trigger handler (Jonathan) > - Read straight into scan.chans[] (Jonathan) > > v3: https://lore.kernel.org/all/20261118104534.159876-1-kapoorarnav43@gmail.com/ > 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 | 35 ++++++++++++++++++++++------------- > 1 file changed, 22 insertions(+), 13 deletions(-) > > diff --git a/drivers/iio/light/bh1745.c b/drivers/iio/light/bh1745.c > index 2b8ff36071b8..68baf5cc14b5 100644 > --- a/drivers/iio/light/bh1745.c > +++ b/drivers/iio/light/bh1745.c > @@ -16,6 +16,9 @@ > #include > #include > #include > +#include > + > +#include > > #include > #include > @@ -210,7 +213,7 @@ static const struct iio_event_spec bh1745_event_spec[] = { > .sign = 'u', \ > .realbits = 16, \ > .storagebits = 16, \ > - .endianness = IIO_CPU, \ > + .endianness = IIO_LE, \ > }, \ > } > > @@ -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; > > @@ -738,21 +750,18 @@ static irqreturn_t bh1745_trigger_handler(int interrupt, void *p) > struct iio_dev *indio_dev = pf->indio_dev; > struct bh1745_data *data = iio_priv(indio_dev); > struct { > - u16 chans[4]; > + __le16 chans[4]; > aligned_s64 timestamp; > } scan = { }; > - u16 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); > + &scan.chans[j++], sizeof(scan.chans[0])); > if (ret) > goto err; > - > - scan.chans[j++] = value; > } > > iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), > > base-commit: 51b83d4cb0f5c1342868c7aa1edbf0c4867477aa