mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] iio: proximity: sx9324: reject reserved gain values on read
@ 2026-11-04 17:59 Arnav Kapoor
  2026-11-05 13:15 ` Joshua Crofts
  0 siblings, 1 reply; 4+ messages in thread
From: Arnav Kapoor @ 2026-11-04 17:59 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Gwendal Grignou, Stephen Boyd,
	linux-iio, linux-kernel, Arnav Kapoor

sx9324_read_gain() is meant to return -EINVAL for the reserved
PROX_CTRL0 gain codes, 0 and 5 to 7. But the range check sits in the
else branch of "if (regval)", where regval is always 0, so codes 5 to 7
are never caught and are reported as gains of 16, 32 and 64, which the
sensor doesn't support and which write_raw() rejects.

Check both reserved ranges before converting the code to a gain.

Fixes: 108e4d4de2b5 ("iio:proximity:sx9324: Fix hardware gain read/write")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
Compile-tested only (W=1); I don't have SX9324 hardware.

 drivers/iio/proximity/sx9324.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
index 1c8ba28c030a..95e7315314f8 100644
--- a/drivers/iio/proximity/sx9324.c
+++ b/drivers/iio/proximity/sx9324.c
@@ -404,13 +404,11 @@ static int sx9324_read_gain(struct sx_common_data *data,
 		return ret;
 
 	regval = FIELD_GET(SX9324_REG_PROX_CTRL0_GAIN_MASK, regval);
-	if (regval)
-		regval--;
-	else if (regval == SX9324_REG_PROX_CTRL0_GAIN_RSVD ||
-		 regval > SX9324_REG_PROX_CTRL0_GAIN_8)
+	if (regval == SX9324_REG_PROX_CTRL0_GAIN_RSVD ||
+	    regval > SX9324_REG_PROX_CTRL0_GAIN_8)
 		return -EINVAL;
 
-	*val = 1 << regval;
+	*val = 1 << (regval - 1);
 
 	return IIO_VAL_INT;
 }

base-commit: 340d44bdf9db0f095f99845b5ad8628781d79b6f
-- 
2.53.0


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

* Re: [PATCH] iio: proximity: sx9324: reject reserved gain values on read
  2026-11-04 17:59 [PATCH] iio: proximity: sx9324: reject reserved gain values on read Arnav Kapoor
@ 2026-11-05 13:15 ` Joshua Crofts
  2026-11-07 20:35   ` Jonathan Cameron
  0 siblings, 1 reply; 4+ messages in thread
From: Joshua Crofts @ 2026-11-05 13:15 UTC (permalink / raw)
  To: Arnav Kapoor
  Cc: Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Gwendal Grignou, Stephen Boyd,
	linux-iio, linux-kernel

On Wed,  4 Nov 2026 23:29:18 +0530
Arnav Kapoor <kapoorarnav43@gmail.com> wrote:

> sx9324_read_gain() is meant to return -EINVAL for the reserved
> PROX_CTRL0 gain codes, 0 and 5 to 7. But the range check sits in the
> else branch of "if (regval)", where regval is always 0, so codes 5 to 7
> are never caught and are reported as gains of 16, 32 and 64, which the
> sensor doesn't support and which write_raw() rejects.
> 
> Check both reserved ranges before converting the code to a gain.
> 
> Fixes: 108e4d4de2b5 ("iio:proximity:sx9324: Fix hardware gain read/write")
> Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
> ---

Makes sense. A stable tag is probably needed.

Reviewed-by: Joshua Crofts <crofts@kernel.org>

-- 
Kind regards,
Joshua Crofts

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

* Re: [PATCH] iio: proximity: sx9324: reject reserved gain values on read
  2026-11-05 13:15 ` Joshua Crofts
@ 2026-11-07 20:35   ` Jonathan Cameron
  2026-11-15  0:31     ` Jonathan Cameron
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Cameron @ 2026-11-07 20:35 UTC (permalink / raw)
  To: Joshua Crofts
  Cc: Arnav Kapoor, David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Gwendal Grignou, Stephen Boyd,
	linux-iio, linux-kernel

On Thu, 5 Nov 2026 14:15:04 +0100
Joshua Crofts <crofts@kernel.org> wrote:

> On Wed,  4 Nov 2026 23:29:18 +0530
> Arnav Kapoor <kapoorarnav43@gmail.com> wrote:
> 
> > sx9324_read_gain() is meant to return -EINVAL for the reserved
> > PROX_CTRL0 gain codes, 0 and 5 to 7. But the range check sits in the
> > else branch of "if (regval)", where regval is always 0, so codes 5 to 7
> > are never caught and are reported as gains of 16, 32 and 64, which the
> > sensor doesn't support and which write_raw() rejects.
> > 
> > Check both reserved ranges before converting the code to a gain.
> > 
> > Fixes: 108e4d4de2b5 ("iio:proximity:sx9324: Fix hardware gain read/write")
> > Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
> > ---  
> 
> Makes sense. A stable tag is probably needed.
> 
> Reviewed-by: Joshua Crofts <crofts@kernel.org>
> 
Seems correct and I can add the stable tag.  I'll leave this on list
for now so that perhaps Stephen has time to take a look.

Thanks,

Jonathan


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

* Re: [PATCH] iio: proximity: sx9324: reject reserved gain values on read
  2026-11-07 20:35   ` Jonathan Cameron
@ 2026-11-15  0:31     ` Jonathan Cameron
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2026-11-15  0:31 UTC (permalink / raw)
  To: Joshua Crofts
  Cc: Arnav Kapoor, David Lechner, Nuno Sá,
	Andy Shevchenko, Andy Shevchenko, Gwendal Grignou, Stephen Boyd,
	linux-iio, linux-kernel

On Sat, 7 Nov 2026 20:35:08 +0000
Jonathan Cameron <jic23@kernel.org> wrote:

> On Thu, 5 Nov 2026 14:15:04 +0100
> Joshua Crofts <crofts@kernel.org> wrote:
> 
> > On Wed,  4 Nov 2026 23:29:18 +0530
> > Arnav Kapoor <kapoorarnav43@gmail.com> wrote:
> >   
> > > sx9324_read_gain() is meant to return -EINVAL for the reserved
> > > PROX_CTRL0 gain codes, 0 and 5 to 7. But the range check sits in the
> > > else branch of "if (regval)", where regval is always 0, so codes 5 to 7
> > > are never caught and are reported as gains of 16, 32 and 64, which the
> > > sensor doesn't support and which write_raw() rejects.
> > > 
> > > Check both reserved ranges before converting the code to a gain.
> > > 
> > > Fixes: 108e4d4de2b5 ("iio:proximity:sx9324: Fix hardware gain read/write")
> > > Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
> > > ---    
> > 
> > Makes sense. A stable tag is probably needed.
> > 
> > Reviewed-by: Joshua Crofts <crofts@kernel.org>
> >   
> Seems correct and I can add the stable tag.  I'll leave this on list
> for now so that perhaps Stephen has time to take a look.

Given nothing back from Stephen I'm going to apply this, but not
rush it in. So applied to the testing branch of iio.git which is lining
stuff up for the next merge cycle.

Thanks,

Jonathan
> 
> Thanks,
> 
> Jonathan
> 
> 


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

end of thread, other threads:[~2026-11-15  0:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04 17:59 [PATCH] iio: proximity: sx9324: reject reserved gain values on read Arnav Kapoor
2026-11-05 13:15 ` Joshua Crofts
2026-11-07 20:35   ` Jonathan Cameron
2026-11-15  0:31     ` 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®