iio: proximity: sx9324: reject reserved gain values on read
PATCH 1/1 iio: proximity: sx9324: reject reserved gain values on read
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.
---
Compile-tested only (W=1); I don't have SX9324 hardware.
drivers/iio/proximity/sx9324.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/iio/proximity/sx9324.c +3 -5
@@ -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
Signature · 1 line
2.53.0
Quoted 11 lines · On Wed, 4 Nov 2026 23:29:18 +0530 Arnav Kapoor <[email protected]> wrote:
sx9324_read_gain()is meant to return-EINVALfor the reservedPROX_CTRL0gain 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 whichwrite_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 <[email protected]>---
Makes sense. A stable tag is probably needed.
Signature · 2 lines
Kind regards, Joshua Crofts
Jonathan Cameron
maintainerQuoted 11 lines · On Thu, 5 Nov 2026 14:15:04 +0100 Joshua Crofts <[email protected]> wrote:
Quoted 8 lines · On Wed, 4 Nov 2026 23:29:18 +0530 Arnav Kapoor <[email protected]> wrote:
sx9324_read_gain()is meant to return-EINVALfor the reservedPROX_CTRL0gain 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 whichwrite_raw()rejects.Check both reserved ranges before converting the code to a gain.
Quoted 8 lines
Quoted 3 lines
Fixes 108e4d4de2b5 ("iio:proximity:sx9324: Fix hardware gain read/write")Signed-off-by Arnav Kapoor <[email protected]>---
Makes sense. A stable tag is probably needed.
Reviewed-by Joshua Crofts <[email protected]>
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
Jonathan Cameron
maintainerQuoted 18 lines · On Sat, 7 Nov 2026 20:35:08 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 15 lines · On Thu, 5 Nov 2026 14:15:04 +0100 Joshua Crofts <[email protected]> wrote:
Quoted 11 lines · On Wed, 4 Nov 2026 23:29:18 +0530 Arnav Kapoor <[email protected]> wrote:
sx9324_read_gain()is meant to return-EINVALfor the reservedPROX_CTRL0gain 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 whichwrite_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 <[email protected]>---
Quoted 6 lines
Quoted 4 lines
Makes sense. A stable tag is probably needed.
Reviewed-by Joshua Crofts <[email protected]>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
Quoted 6 lines
Thanks,
Jonathan