ARM Sunxi Platform Development
 help / color / mirror / Atom feed
* [PATCH v2] iio: adc: sun20i-gpadc: support non-contiguous channel lookups
@ 2026-05-14  3:19 Michal Piekos
  2026-05-14 12:16 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Michal Piekos @ 2026-05-14  3:19 UTC (permalink / raw)
  To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
	Chen-Yu Tsai, Jernej Skrabec, Samuel Holland
  Cc: linux-iio, linux-arm-kernel, linux-sunxi, linux-kernel,
	Michal Piekos, Nathan Chancellor, Nick Desaulniers, Bill Wendling,
	Justin Stitt, llvm

Using consumer driver like iio-hwmon which resolve channels thorugh
io-channels phandles will fail for sparse channels because IIO core by
default threats phandle argument as index into channel array.
        eg. <&gpadc 1> will fail if there is only channel@1 specified

Add .fwnode_xlate() which maps DT phandle to the registered channel
whose chan->channel matches the hardware channel number. It allows
sparse channel maps to be consumed by drivers like iio-hwmon.

Tested on Radxa Cubie A5E.

Signed-off-by: Michal Piekos <michal.piekos@mmpsystems.pl>
---
Changes in v2:
- Move loop variable declaration into the for statement
- Fix indentation using clang-format
- Correct commit wording
- Link to v1: https://patch.msgid.link/20260513-fix-sunxi-gpadc-sparse-channels-v1-1-6c21e290bcee@mmpsystems.pl

To: Jonathan Cameron <jic23@kernel.org>
To: David Lechner <dlechner@baylibre.com>
To: Nuno Sá <nuno.sa@analog.com>
To: Andy Shevchenko <andy@kernel.org>
To: Chen-Yu Tsai <wens@kernel.org>
To: Jernej Skrabec <jernej.skrabec@gmail.com>
To: Samuel Holland <samuel@sholland.org>
To: Nathan Chancellor <nathan@kernel.org>
To: Nick Desaulniers <nick.desaulniers+lkml@gmail.com>
To: Bill Wendling <morbo@google.com>
To: Justin Stitt <justinstitt@google.com>
Cc: linux-iio@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-sunxi@lists.linux.dev
Cc: linux-kernel@vger.kernel.org
Cc: llvm@lists.linux.dev
---
 drivers/iio/adc/sun20i-gpadc-iio.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/iio/adc/sun20i-gpadc-iio.c b/drivers/iio/adc/sun20i-gpadc-iio.c
index 861c14da75ad..78c9a52f38df 100644
--- a/drivers/iio/adc/sun20i-gpadc-iio.c
+++ b/drivers/iio/adc/sun20i-gpadc-iio.c
@@ -139,8 +139,20 @@ static irqreturn_t sun20i_gpadc_irq_handler(int irq, void *data)
 	return IRQ_HANDLED;
 }
 
+static int
+sun20i_gpadc_fwnode_xlate(struct iio_dev *indio_dev,
+			  const struct fwnode_reference_args *iiospec)
+{
+	for (unsigned int i = 0; i < indio_dev->num_channels; i++)
+		if (indio_dev->channels[i].channel == iiospec->args[0])
+			return i;
+
+	return -EINVAL;
+}
+
 static const struct iio_info sun20i_gpadc_iio_info = {
 	.read_raw = sun20i_gpadc_read_raw,
+	.fwnode_xlate = sun20i_gpadc_fwnode_xlate,
 };
 
 static void sun20i_gpadc_reset_assert(void *data)

---
base-commit: 1d5dcaa3bd65f2e8c9baa14a393d3a2dc5db7524
change-id: 20260513-fix-sunxi-gpadc-sparse-channels-2b6b2063bd49

Best regards,
--  
Michal Piekos <michal.piekos@mmpsystems.pl>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] iio: adc: sun20i-gpadc: support non-contiguous channel lookups
  2026-05-14  3:19 [PATCH v2] iio: adc: sun20i-gpadc: support non-contiguous channel lookups Michal Piekos
@ 2026-05-14 12:16 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-05-14 12:16 UTC (permalink / raw)
  To: Michal Piekos; +Cc: linux-sunxi

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Missing validation of iiospec->nargs leads to uninitialized stack memory read
- [Low] Spelling and grammar errors in the commit message
--

commit 0da362a2628891f0ec84a4906fcbc3b1521286d2
Author: Michal Piekos <michal.piekos@mmpsystems.pl>

iio: adc: sun20i-gpadc: support non-contiguous channel lookups

This commit adds a fwnode_xlate callback to support sparse channel maps
by mapping the devicetree phandle argument to the registered channel whose
ID matches the hardware channel number.

> Using consumer driver like iio-hwmon which resolve channels thorugh
> io-channels phandles will fail for sparse channels because IIO core by
> default threats phandle argument as index into channel array.

This isn't a bug, but there are typos in the commit message: "thorugh"
instead of "through", and "threats" instead of "treats".

> diff --git a/drivers/iio/adc/sun20i-gpadc-iio.c b/drivers/iio/adc/sun20i-gpadc-iio.c
> index 861c14da75add..78c9a52f38df0 100644
> --- a/drivers/iio/adc/sun20i-gpadc-iio.c
> +++ b/drivers/iio/adc/sun20i-gpadc-iio.c

[ ... ]

> @@ -139,8 +139,20 @@ static irqreturn_t sun20i_gpadc_irq_handler(int irq, void *data)
>  	return IRQ_HANDLED;
>  }
>  
> +static int
> +sun20i_gpadc_fwnode_xlate(struct iio_dev *indio_dev,
> +			  const struct fwnode_reference_args *iiospec)
> +{
> +	for (unsigned int i = 0; i < indio_dev->num_channels; i++)
> +		if (indio_dev->channels[i].channel == iiospec->args[0])

Is it possible to read uninitialized stack memory here?

The IIO core function __fwnode_iio_channel_get() allocates iiospec on the
stack without zero-initializing it. When parsing a reference from a
malformed devicetree node (e.g., #io-channel-cells = <0>), an ACPI table,
or a software node, fwnode_property_get_reference_args() returns success
and sets iiospec->nargs = 0 but leaves the args array unmodified.

If iiospec->nargs is not verified, this evaluates iiospec->args[0]
using uninitialized memory. If this garbage data happens to match an
existing channel ID, the driver could spuriously map a random channel.

Should this function check if iiospec->nargs < 1 before accessing the
arguments array?

> +			return i;
> +
> +	return -EINVAL;
> +}
> +

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260514-fix-sunxi-gpadc-sparse-channels-v2-1-d4a66b70c7a7@mmpsystems.pl?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-05-14 12:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-14  3:19 [PATCH v2] iio: adc: sun20i-gpadc: support non-contiguous channel lookups Michal Piekos
2026-05-14 12:16 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox