iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain
cover iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain
The remaining sx93xx fixes I had, sent as one series and grouped by driver, as asked by Jonathan.
1: sx_common: a raw read can return on a stale completion (left by an
interrupted read, or by conversions while another device uses the sensor's trigger), and it then turns off the CONVDONE interrupt the trigger depends on. The interrupted-read case was reported by Sashiko on Zhang Jie's sx9324 resolution patch.
2, 5: sx9324/sx9360 read_thresh() reports code r as r * r / 2, but
write_thresh() inverts it with int_sqrt(2 * val), so writing back a value that was just read lowers odd codes by one; some negative values also wrap around instead of being rejected.
3, 6: sx9324/sx9360 write_hysteresis() turns negative values into the
largest hysteresis.
4: the sx9360 gain encoding fix from the RFC, now a patch on top of
"iio: proximity: sx9360: reject out-of-range hardware gain values" (in iio testing), as suggested by Jonathan. It uses the encoding sx9324 got in 108e4d4de2b5 and that Semtech's own SX9360 driver uses (codes 1..4 = x1..x8, 0 reserved).
Based on iio testing with the patches applied there since it was last pushed (sx9324 reserved gain on read, sx9310 proxraw-strength = <0>, sx9360 out-of-range gain).
Changes to patch 4 since the RFC:
- Rebased on the applied out-of-range gain patch; GAIN_8 becomes 0x4 and the range check uses ilog2(val) + 1
- Commit message cites the Semtech vendor driver
RFC: https://lore.kernel.org/all/[email protected]/
The threshold change was checked against a userspace copy of both
conversions: all 256 codes round-trip, and every write picks the largest code whose reported threshold doesn't exceed the request. Compile-tested only (W=1); I don't have SX93xx hardware.
Arnav Kapoor (6):
iio: proximity: sx_common: fix raw reads while the trigger is in use
iio: proximity: sx9324: fix proximity threshold writes
iio: proximity: sx9324: reject negative hysteresis values
iio: proximity: sx9360: fix hardware gain encoding
iio: proximity: sx9360: fix proximity threshold writes
iio: proximity: sx9360: reject negative hysteresis values
drivers/iio/proximity/sx9324.c | 9 ++++++++- drivers/iio/proximity/sx9360.c | 29 ++++++++++++++++++++++------- drivers/iio/proximity/sx_common.c | 25 +++++++++++++++++++------ 3 files changed, 49 insertions(+), 14 deletions(-)
base-commit: 8fda275449b68a96f0a38a61daa35cc39b0fb9d8
Signature · 1 line
2.53.0
PATCH 1/6 iio: proximity: sx_common: fix raw reads while the trigger is in use
sx_common_read_proximity() only reinitialises the completion after waiting on it, so a complete() that arrives when no read is waiting is consumed by the next read, which then returns old data without waiting for a conversion that includes the channel it just enabled. This happens in two ways:
- The device trigger has no validate_device(), so another IIO device can use it. While it does, set_trigger_state() keeps the CONVDONE interrupt enabled and the IRQ thread calls complete() for every conversion.
- A read interrupted by a signal reinitialises the completion and returns, but a CONVDONE interrupt already raised can still complete it once the mutex is dropped.
Also, when it is done, the read disables the CONVDONE interrupt unconditionally, which stops the trigger for the other device.
Reinitialise the completion before enabling the interrupt, and leave the interrupt enabled when the trigger is, as set_trigger_state() already does for pending raw reads.
The interrupted-wait case was reported by the Sashiko AI reviewer on an unrelated sx9324 patch.
--- drivers/iio/proximity/sx_common.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
drivers/iio/proximity/sx_common.c +19 -6
@@ -167,6 +167,15 @@ static int sx_common_put_event_channel(struct sx_common_data *data, int channel) data->chan_event & ~BIT(channel)); } +/* The trigger needs the CONVDONE interrupt too, leave it on while in use. */ +static int sx_common_put_convdone_irq (struct sx_common_data *data) +{ +if (data->trigger_enabled) +return 0 ; + +return sx_common_disable_irq (data,SX_COMMON_CONVDONE_IRQ ); +} +/** * sx_common_read_proximity() - Read raw proximity value. * @data: Internal data
@@ -188,18 +197,22 @@ int sx_common_read_proximity(struct sx_common_data *data,if (ret) goto out; +/* +* Forget conversions that completed before this read, e.g. while the +* trigger was in use, so that we wait for a fresh one. +*/ +reinit_completion (&data->completion); + ret =sx_common_enable_irq (data,SX_COMMON_CONVDONE_IRQ );if (ret) goto out_put_channel;mutex_unlock (&data->mutex); -if (data->client->irq) { +if (data->client->irq) ret =wait_for_completion_interruptible (&data->completion); -reinit_completion (&data->completion); - }else { +else ret = data->chip_info->ops.wait_for_sample (data); - }mutex_lock (&data->mutex);
@@ -212,7 +225,7 @@ int sx_common_read_proximity(struct sx_common_data *data, *val =sign_extend32 (be16_to_cpu (rawval), chan->scan_type.realbits -1 ); - ret =sx_common_disable_irq (data,SX_COMMON_CONVDONE_IRQ ); + ret =sx_common_put_convdone_irq (data);if (ret) goto out_put_channel;
@@ -225,7 +238,7 @@ int sx_common_read_proximity(struct sx_common_data *data,return IIO_VAL_INT ; out_disable_irq: -sx_common_disable_irq (data,SX_COMMON_CONVDONE_IRQ ); +sx_common_put_convdone_irq (data); out_put_channel:sx_common_put_read_channel (data, chan->channel); out:
Signature · 1 line
2.53.0
PATCH 2/6 iio: proximity: sx9324: fix proximity threshold writes
In reply to Arnav Kapoorsx9324_read_thresh() reports a PROX_THRESH code r as r for r <= 1 and as r * r / 2 otherwise, but sx9324_write_thresh() converts back with int_sqrt(2 * val). For odd r the read value is (r * r - 1) / 2, so writing back the value that was just read programs r - 1: setting the threshold to the value it already has lowers it, for 127 of the 256 codes (3, 5, 7, ...).
The int to unsigned int conversion also lets some negative values
through: 2 * val wraps, so e.g. INT_MIN is written as 0 and values just above INT_MIN as small thresholds instead of being rejected.
Reject negative values and use int_sqrt(2 * val + 1), which picks the largest code whose reported threshold does not exceed the requested value and makes read_thresh() and write_thresh() inverses of each other for all 256 codes.
--- drivers/iio/proximity/sx9324.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/iio/proximity/sx9324.c +5 -1
@@ -617,8 +617,12 @@ static int sx9324_write_thresh(struct sx_common_data *data, reg =SX9324_REG_PROX_CTRL6 + chan->channel /2 ; +if (_val <0 ) +return -EINVAL ; + +/* Inverse of read_thresh(), so that writing back a read value is a no-op */ if (val >=1 ) - val =int_sqrt (2 * val); + val =int_sqrt (2 * val +1 );if (val >0xff )return -EINVAL ;
Signature · 1 line
2.53.0
Re: [PATCH 2/6] iio: proximity: sx9324: fix proximity threshold writes
Quoted 5 lines · Arnav Kapoor
sx9324_read_thresh()reports aPROX_THRESHcode r as r for r <= 1 and as r * r / 2 otherwise, butsx9324_write_thresh()converts back withint_sqrt(2 * val).For odd r the read value is (r * r - 1) / 2,
No, it's r²/2 but in integer arithmetics. That's why for odd number squares it gives 1/2 that becomes 0.
Quoted 4 lines
so writing back the value that was just read programs r - 1: setting the threshold to the value it already has lowers it, for 127 of the 256 codes (3, 5, 7, ...).
Quoted 8 lines
The int to unsigned int conversion also lets some negative values
through: 2 * val wraps, so e.g.INT_MINis written as 0 and values just aboveINT_MINas small thresholds instead of being rejected.Reject negative values and use
int_sqrt(2 * val + 1), which picks the largest code whose reported threshold does not exceed the requested value and makesread_thresh()andwrite_thresh()inverses of each other for all 256 codes.
So, for the read part
reg value 0 0 1 1 2 2 3 4 4 8 5 12 6 18 7 24 ....
For the write part (the negative is problematic only on 32-bit case IIUC)
value reg -INT_MIN 0 -INT_MIN + 1 1 -INT_MIN + 2 2 -INT_MIN + 3 2 ... 0 0 1 1 2 2 3 2 4 2 // has to be 3 5 3 6 3 7 3 8 4 9 4 ... 12 4 // has to be 5 13 5 ... 18 6 ... 24 6 // has to be 7 ...
Yep, what we see is off-by-one for full squares. This is because division by 2 in integer arithmetics makes it floor number. So, what you want to have is a correction for all odd numbers.
...
Quoted 3 lines
static int sx9324_write_thresh(struct
sx_common_data*data,reg =
SX9324_REG_PROX_CTRL6+chan->channel/ 2;
This one is not used in the conditionals, so the new check may well be done before it.
drivers/iio/proximity/sx9324.c static int sx9324_write_thresh(struct sx_common_data *data, · line 620
Quoted 1 line
drivers/iio/proximity/sx9324.c +1
-0+if (_val <0 )
Checking _val after assigning val to it is confusing.
drivers/iio/proximity/sx9324.c static int sx9324_write_thresh(struct sx_common_data *data, · line 624
Quoted 4 lines
drivers/iio/proximity/sx9324.c +3
-0+return -EINVAL ; + +/* Inverse of read_thresh(), so that writing back a read value is a no-op */ if (val >=1 )
First of all, I would drop the '=' here.
drivers/iio/proximity/sx9324.c static int sx9324_write_thresh(struct sx_common_data *data, · line 625
Quoted 2 lines
drivers/iio/proximity/sx9324.c +1
-1- val =int_sqrt (2 * val); + val =int_sqrt (2 * val +1 );
And here I would rather use (val % 2) instead of 1. See above why.
Quoted 2 lines
drivers/iio/proximity/sx9324.c +0
-0if (val >0xff )return -EINVAL ;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 2/6] iio: proximity: sx9324: fix proximity threshold writes
Quoted 4 lines · Andy Shevchenko
Quoted 1 line
For odd r the read value is (r * r - 1) / 2,
No, it's r²/2 but in integer arithmetics. That's why for odd number squares it gives 1/2 that becomes 0.
Same value, but your wording is clearer. I'll say in v2 that r * r / 2 rounds down for odd r.
Quoted 5 lines
Yep, what we see is off-by-one for full squares. This is because division by 2 in integer arithmetics makes it floor number. So, what you want to have is a correction for all odd numbers.
And here I would rather use (val % 2) instead of 1. See above why.
I don't think val % 2 works, because the correction depends on whether the register code r is odd, not whether val is. Odd codes read back as even values too, as in your table:
reg read value int_sqrt(2 * val + val % 2) int_sqrt(2 * val + 1) 3 4 int_sqrt(8) = 2 int_sqrt(9) = 3 5 12 int_sqrt(24) = 4 int_sqrt(25) = 5 7 24 int_sqrt(48) = 6 int_sqrt(49) = 7
With + 1: an odd r reads back as (r * r - 1) / 2, so 2 * val + 1 is exactly r * r. An even r reads back as r * r / 2, so 2 * val + 1 is r * r + 1, and int_sqrt() still gives r. Checking all 256 codes, + 1 round-trips every one of them, while val % 2 still fails on the same 127 codes as the current int_sqrt(2 * val). For values in between,
+ 1 also picks the largest code whose threshold does not exceed the requested value.
Quoted 1 line
For the write part (the negative is problematic only on 32-bit case IIUC)
I think it is on 64-bit too: val is an unsigned int, so 2 * val wraps at 32 bits before int_sqrt() sees it, whatever the size of long.
Quoted 5 lines
Quoted 1 line
reg = SX9324_REG_PROX_CTRL6 + chan->channel / 2;This one is not used in the conditionals, so the new check may well be done before it.
Quoted 7 lines
drivers/iio/proximity/sx9324.c static int sx9324_write_thresh(struct sx_common_data *data, · line 620
Quoted 1 line
drivers/iio/proximity/sx9324.c +1
-0+if (_val <0 )Checking
_valafter assigning val to it is confusing.Quoted 1 line
if (val >= 1)First of all, I would drop the '=' here.
Agreed on all three. In v2 the check on _val will come first, before val and reg are assigned, and the condition will be val > 1. I'll make the same changes in 5/6, which has the same code for sx9360.
Thanks,
Arnav
Re: [PATCH 2/6] iio: proximity: sx9324: fix proximity threshold writes
Quoted 9 lines · Arnav Kapoor
Quoted 4 lines · Andy Shevchenko
Quoted 1 line
For odd r the read value is (r * r - 1) / 2,
No, it's r²/2 but in integer arithmetics. That's why for odd number squares it gives 1/2 that becomes 0.
Same value, but your wording is clearer. I'll say in v2 that r * r / 2 rounds down for odd r.
Quoted 8 lines
Quoted 5 lines
Yep, what we see is off-by-one for full squares. This is because division by 2 in integer arithmetics makes it floor number. So, what you want to have is a correction for all odd numbers.
And here I would rather use (val % 2) instead of 1. See above why.
I don't think val % 2 works, because the correction depends on whether the register code r is odd, not whether val is.
Ah, yes, you are right. I was thinking about it, but got sideways and wrote that.
Quoted 8 lines
Odd codes read back as even values too, as in your table:
reg read value int_sqrt(2 * val + val % 2) int_sqrt(2 * val + 1) 3 4 int_sqrt(8) = 2 int_sqrt(9) = 3 5 12 int_sqrt(24) = 4 int_sqrt(25) = 5 7 24 int_sqrt(48) = 6 int_sqrt(49) = 7With + 1: an odd r reads back as (r * r - 1) / 2,
Nope, this reads as usual, the / 2 makes it floor because of integer arithmetics. While mathematically (in integer arithmetics) it gives the same result the reasoning is wrong.
Quoted 2 lines
so 2 * val + 1 is
exactly r * r.
From value point of view, yes for the full squares, but it's wrong for other cases.
Quoted 2 lines
An even r reads back as r * r / 2, so 2 * val + 1 is
r * r + 1, andint_sqrt()still gives r. Checking all 256 codes,
For the sake of the correctness the whole set of 64k has to be checked. And in most cases the + 1 there will be wrong. The only correction we need is on the edges of the full squares, id est when val == r² - 1.
Quoted 4 lines
+ 1 round-trips every one of them, while val % 2 still fails on the same 127 codes as the current
int_sqrt(2 * val). For values in between,
+ 1 also picks the largest code whose threshold does not exceed the requested value.
At bare minimum the commit message needs to be corrected and explained these aspects that while the + 1 gives wrong result for most of the cases, it's still okay to use as we work only in integer arithmetics. Also the formula (r² - 1) / 2 has to be dropped as it's incorrect from mathematics point of view. The floor() happens on / 2 and has nothing to do with (- 1) correction.
Quoted 4 lines
Quoted 1 line
For the write part (the negative is problematic only on 32-bit case IIUC)
I think it is on 64-bit too: val is an unsigned int, so 2 * val wraps at 32 bits before
int_sqrt()sees it, whatever the size of long.
OK, thanks for checking.
Quoted 16 lines
Quoted 5 lines
Quoted 1 line
reg = SX9324_REG_PROX_CTRL6 + chan->channel / 2;This one is not used in the conditionals, so the new check may well be done before it.
Quoted 7 lines
drivers/iio/proximity/sx9324.c static int sx9324_write_thresh(struct sx_common_data *data, · line 620
Quoted 1 line
drivers/iio/proximity/sx9324.c +1
-0+if (_val <0 )Checking
_valafter assigning val to it is confusing.Quoted 1 line
if (val >= 1)First of all, I would drop the '=' here.
Agreed on all three. In v2 the check on
_valwill come first, before val and reg are assigned, and the condition will be val > 1. I'll make the same changes in 5/6, which has the same code for sx9360.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 2/6] iio: proximity: sx9324: fix proximity threshold writes
Quoted 3 lines · Andy Shevchenko
For the sake of the correctness the whole set of 64k has to be checked. And in most cases the + 1 there will be wrong. The only correction we need is on the edges of the full squares, id est when val == r² - 1.
I ran the v2 logic (reject val < 0, int_sqrt(2 * val + 1) for val > 1, reject results above 0xff) over every val from 0 to 65535. Values up to 32767 are accepted, and for each of them, apart from val == 0:
code = largest r such that r * r / 2 <= val
val == 0 stays at code 0, which reads back as 0 like code 1 does.
The + 1 only changes the result where you say the correction is needed. int_sqrt(2 * val + 1) differs from int_sqrt(2 * val) for 127 values of val, and those are exactly the ones with 2 * val == r² - 1 for an odd r (val = 4, 12, 24, ..., 32512). For every other val both give the same code, so the + 1 has no effect there.
The reason is that, in integer arithmetic:
r * r / 2 <= val <=> r * r < 2 * val + 2 <=> r * r <= 2 * val + 1
Quoted 5 lines
At bare minimum the commit message needs to be corrected and explained these aspects that while the + 1 gives wrong result for most of the cases, it's still okay to use as we work only in integer arithmetics. Also the formula (r² - 1) / 2 has to be dropped as it's incorrect from mathematics point of view. The
floor()happens on / 2 and has nothing to do with (- 1) correction.
Agreed on dropping the formula. For v2 I plan to replace that part of the commit message with:
read_thresh() reports code r as r * r / 2, and the division rounds down for odd r. write_thresh() inverts that with int_sqrt(2 * val), which is one code too low when 2 * val == r * r - 1, so writing back the value read from an odd code lowers the code by one.
Use int_sqrt(2 * val + 1), which gives the largest code whose threshold does not exceed val: in integer arithmetic,
r * r / 2 <= val is the same as r * r <= 2 * val + 1. The result only differs from int_sqrt(2 * val) on those edges.
The same goes for 5/6. Does that cover what you had in mind?
Thanks,
Arnav
Re: [PATCH 2/6] iio: proximity: sx9324: fix proximity threshold writes
Quoted 19 lines · Arnav Kapoor
Quoted 3 lines · Andy Shevchenko
For the sake of the correctness the whole set of 64k has to be checked. And in most cases the + 1 there will be wrong. The only correction we need is on the edges of the full squares, id est when val == r² - 1.
I ran the v2 logic (reject val < 0,
int_sqrt(2 * val + 1)for val > 1, reject results above0xff) over every val from 0 to 65535. Values up to 32767 are accepted, and for each of them, apart from val == 0:code = largest r such that r * r / 2 <= val
val == 0 stays at code 0, which reads back as 0 like code 1 does.
The + 1 only changes the result where you say the correction is needed.
int_sqrt(2 * val + 1)differs fromint_sqrt(2 * val)for 127 values of val, and those are exactly the ones with 2 * val == r² - 1 for an odd r (val = 4, 12, 24, ..., 32512). For every other val both give the same code, so the + 1 has no effect there.
Quoted 3 lines
The reason is that, in integer arithmetic:
r * r / 2 <= val <=> r * r < 2 * val + 2 <=> r * r <= 2 * val + 1
Yes, for r > 1.
Quoted 14 lines
Quoted 5 lines
At bare minimum the commit message needs to be corrected and explained these aspects that while the + 1 gives wrong result for most of the cases, it's still okay to use as we work only in integer arithmetics. Also the formula (r² - 1) / 2 has to be dropped as it's incorrect from mathematics point of view. The
floor()happens on / 2 and has nothing to do with (- 1) correction.Agreed on dropping the formula. For v2 I plan to replace that part of the commit message with:
read_thresh()reports code r as r * r / 2, and the division rounds down for odd r.write_thresh()inverts that withint_sqrt(2 * val), which is one code too low when 2 * val == r * r - 1, so writing back the value read from an odd code lowers the code by one.
Quoted 4 lines
Use
int_sqrt(2 * val + 1), which gives the largest code whose threshold does not exceed val: in integer arithmetic,
r * r / 2 <= val is the same as r * r <= 2 * val + 1. The result only differs fromint_sqrt(2 * val)on those edges.
s/code/value/g ?
Quoted 1 line
The same goes for 5/6. Does that cover what you had in mind?
Yes. This is much clearer, thanks!
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH 3/6 iio: proximity: sx9324: reject negative hysteresis values
In reply to Arnav Kapoorsx9324_write_hysteresis() copies the value into an unsigned int before comparing it with the threshold, so a negative value becomes a large one, passes the "val >= pthresh >> 2" check and sets the largest hysteresis instead of being rejected. Return -EINVAL for negative values.
--- drivers/iio/proximity/sx9324.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/iio/proximity/sx9324.c +3 -0
@@ -638,6 +638,9 @@ static int sx9324_write_hysteresis(struct sx_common_data *data,unsigned int hyst, val = _val;int ret, pthresh; +if (_val <0 ) +return -EINVAL ; + ret =sx9324_read_thresh (data, chan, &pthresh);if (ret <0 )return ret;
Signature · 1 line
2.53.0
Re: [PATCH 3/6] iio: proximity: sx9324: reject negative hysteresis values
Quoted 5 lines · Arnav Kapoor
sx9324_write_hysteresis()copies the value into an unsigned int before comparing it with the threshold, so a negative value becomes a large one, passes the "val >= pthresh >> 2" check and sets the largest hysteresis instead of being rejected. Return-EINVALfor negative values.
As noticed earlier not all cases might have this issue, please elaborate if this is for 32- and 64-bit cases or only for the former.
...
drivers/iio/proximity/sx9324.c static int sx9324_write_hysteresis(struct sx_common_data *data, · line 642
Quoted 5 lines
drivers/iio/proximity/sx9324.c +2
-0unsigned int hyst, val = _val;int ret, pthresh; +if (_val <0 ) +return -EINVAL ;
Same (semantic) issue, you are checking variable after its use.
...
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 3/6] iio: proximity: sx9324: reject negative hysteresis values
Quoted 2 lines · Andy Shevchenko
As noticed earlier not all cases might have this issue, please elaborate if this is for 32- and 64-bit cases or only for the former.
Both. val is an unsigned int and pthresh an int, so in
"val >= pthresh >> 2" the right side is converted to unsigned int and the comparison is done in 32 bits; long doesn't come into it. A negative _val such as -1 becomes 0xffffffff, which is >= any threshold, so it sets the largest hysteresis on 32-bit and 64-bit alike. I'll spell that out in the v2 commit message.
Quoted 7 lines
drivers/iio/proximity/sx9324.c static int sx9324_write_hysteresis(struct sx_common_data *data, · line 642
Quoted 5 lines
drivers/iio/proximity/sx9324.c +2
-0unsigned int hyst, val = _val;int ret, pthresh; +if (_val <0 ) +return -EINVAL ;Same (semantic) issue, you are checking variable after its use.
Agreed. In v2 the check comes first and val is assigned after it, here and in 6/6. Your comments on 5/6 will get the same changes as 2/6.
Thanks,
Arnav
PATCH 4/6 iio: proximity: sx9360: fix hardware gain encoding
In reply to Arnav KapoorThe sx9360 PROX_CTRL0 gain handling was copied from the original sx9324 driver and has the same bug that commit 108e4d4de2b5 ("iio:proximity:sx9324: Fix hardware gain read/write") fixed there:
- The gain field holds ilog2(<gain>) + 1, with 0 reserved, so a gain of 1 is 0x1 and a gain of 8 is 0x4. read_gain() and write_gain() use ilog2(<gain>) instead, so every gain is off by one: writing 1 programs the reserved code 0 and writing 8 programs a gain of 4.
- SX9360_REG_PROX_CTRL0_GAIN_1 is 0x80, which sets the reserved bit 7 and leaves the gain field at the reserved code 0, instead of 0x1 << 3.
Semtech's own SX9360 driver (shipped in Samsung Galaxy kernels) decodes the field the same way, codes 1..4 as x1..x8 and 0, 5..7 as reserved, and programs 0x09 (gain x1, bit 7 clear) into both PROX_CTRL0 registers.
Use the same encoding as sx9324: add one when writing, subtract one when reading, return -EINVAL for the reserved codes on read, and build the default with FIELD_PREP_CONST(). SX9360_REG_PROX_CTRL0_GAIN_8 used by the range check in write_gain() becomes 0x4 accordingly.
--- drivers/iio/proximity/sx9360.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/iio/proximity/sx9360.c +14 -6
@@ -64,8 +64,9 @@#define SX9360_REG_PROX_CTRL0_PHR 0x40#define SX9360_REG_PROX_CTRL0_PHM 0x41#define SX9360_REG_PROX_CTRL0_GAIN_MASK GENMASK(5, 3) -#define SX9360_REG_PROX_CTRL0_GAIN_1 0x80 -#define SX9360_REG_PROX_CTRL0_GAIN_8 0x3 +#define SX9360_REG_PROX_CTRL0_GAIN_RSVD 0x0 +#define SX9360_REG_PROX_CTRL0_GAIN_1 0x1 +#define SX9360_REG_PROX_CTRL0_GAIN_8 0x4#define SX9360_REG_PROX_CTRL0_RAWFILT_MASK GENMASK(2, 0)#define SX9360_REG_PROX_CTRL0_RAWFILT_1P50 0x01#define SX9360_REG_PROX_CTRL1 0x42
@@ -289,7 +290,12 @@ static int sx9360_read_gain(struct sx_common_data *data,if (ret)return ret; - *val =1 <<FIELD_GET (SX9360_REG_PROX_CTRL0_GAIN_MASK , regval); + regval =FIELD_GET (SX9360_REG_PROX_CTRL0_GAIN_MASK , regval); +if (regval ==SX9360_REG_PROX_CTRL0_GAIN_RSVD || + regval >SX9360_REG_PROX_CTRL0_GAIN_8 ) +return -EINVAL ; + + *val =BIT (regval -1 );return IIO_VAL_INT ; }
@@ -612,7 +618,7 @@ static int sx9360_write_gain(struct sx_common_data *data,if (val <=0 )return -ERANGE ; - gain =ilog2 (val); + gain =ilog2 (val) +1 ;if (gain >SX9360_REG_PROX_CTRL0_GAIN_8 )return -ERANGE ;
@@ -663,9 +669,11 @@ static const struct sx_common_reg_default sx9360_default_regs[] = { {SX9360_REG_AFE_PARAM1_PHM ,SX9360_REG_AFE_PARAM1_AGAIN_PHM_6PF | SX9360_REG_AFE_PARAM1_FREQ_83_33HZ, "afe_param1_phm" }, - { SX9360_REG_PROX_CTRL0_PHR, SX9360_REG_PROX_CTRL0_GAIN_1 | + { SX9360_REG_PROX_CTRL0_PHR, +FIELD_PREP_CONST (SX9360_REG_PROX_CTRL0_GAIN_MASK ,SX9360_REG_PROX_CTRL0_GAIN_1 ) | SX9360_REG_PROX_CTRL0_RAWFILT_1P50, "prox_ctrl0_phr" }, - { SX9360_REG_PROX_CTRL0_PHM, SX9360_REG_PROX_CTRL0_GAIN_1 | + { SX9360_REG_PROX_CTRL0_PHM, +FIELD_PREP_CONST (SX9360_REG_PROX_CTRL0_GAIN_MASK ,SX9360_REG_PROX_CTRL0_GAIN_1 ) | SX9360_REG_PROX_CTRL0_RAWFILT_1P50, "prox_ctrl0_phm" }, { SX9360_REG_PROX_CTRL1, SX9360_REG_PROX_CTRL1_AVGNEG_THRESH_16K, "prox_ctrl1" }, { SX9360_REG_PROX_CTRL2, SX9360_REG_PROX_CTRL2_AVGDEB_2SAMPLES |
Signature · 1 line
2.53.0
PATCH 5/6 iio: proximity: sx9360: fix proximity threshold writes
In reply to Arnav Kapoorsx9360_read_thresh() reports a PROX_THRESH code r as r for r <= 1 and as r * r / 2 otherwise, but sx9360_write_thresh() converts back with int_sqrt(2 * val). For odd r the read value is (r * r - 1) / 2, so writing back the value that was just read programs r - 1: setting the threshold to the value it already has lowers it, for 127 of the 256 codes (3, 5, 7, ...).
The int to unsigned int conversion also lets some negative values
through: 2 * val wraps, so e.g. INT_MIN is written as 0 and values just above INT_MIN as small thresholds instead of being rejected.
Reject negative values and use int_sqrt(2 * val + 1), which picks the largest code whose reported threshold does not exceed the requested value and makes read_thresh() and write_thresh() inverses of each other for all 256 codes.
--- drivers/iio/proximity/sx9360.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/iio/proximity/sx9360.c +5 -1
@@ -510,8 +510,12 @@ static int sx9360_write_thresh(struct sx_common_data *data, int _val) {unsigned int val = _val; +if (_val <0 ) +return -EINVAL ; + +/* Inverse of read_thresh(), so that writing back a read value is a no-op */ if (val >=1 ) - val =int_sqrt (2 * val); + val =int_sqrt (2 * val +1 );if (val >0xff )return -EINVAL ;
Signature · 1 line
2.53.0
Re: [PATCH 5/6] iio: proximity: sx9360: fix proximity threshold writes
Quoted 7 lines · Arnav Kapoor
sx9360_read_thresh()reports aPROX_THRESHcode r as r for r <= 1 and as r * r / 2 otherwise, butsx9360_write_thresh()converts back withint_sqrt(2 * val). For odd r the read value is (r * r - 1) / 2, so writing back the value that was just read programs r - 1: setting the threshold to the value it already has lowers it, for 127 of the 256 codes (3, 5, 7, ...).
Quoted 8 lines
The int to unsigned int conversion also lets some negative values
through: 2 * val wraps, so e.g.INT_MINis written as 0 and values just aboveINT_MINas small thresholds instead of being rejected.Reject negative values and use
int_sqrt(2 * val + 1), which picks the largest code whose reported threshold does not exceed the requested value and makesread_thresh()andwrite_thresh()inverses of each other for all 256 codes.
All the same issues with this patch as the other one with sqrt() operation.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH 6/6 iio: proximity: sx9360: reject negative hysteresis values
In reply to Arnav Kapoorsx9360_write_hysteresis() copies the value into an unsigned int before comparing it with the threshold, so a negative value becomes a large one, passes the "val >= pthresh >> 2" check and sets the largest hysteresis instead of being rejected. Return -EINVAL for negative values.
--- drivers/iio/proximity/sx9360.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/iio/proximity/sx9360.c +3 -0
@@ -529,6 +529,9 @@ static int sx9360_write_hysteresis(struct sx_common_data *data, int _val)unsigned int hyst, val = _val;int ret, pthresh; +if (_val <0 ) +return -EINVAL ; + ret =sx9360_read_thresh (data, &pthresh);if (ret <0 )return ret;
Signature · 1 line
2.53.0
Re: [PATCH 6/6] iio: proximity: sx9360: reject negative hysteresis values
Quoted 5 lines · Arnav Kapoor
sx9360_write_hysteresis()copies the value into an unsigned int before comparing it with the threshold, so a negative value becomes a large one, passes the "val >= pthresh >> 2" check and sets the largest hysteresis instead of being rejected. Return-EINVALfor negative values.
...
drivers/iio/proximity/sx9360.c static int sx9360_write_hysteresis(struct sx_common_data *data, int _val) · line 533
Quoted 5 lines
drivers/iio/proximity/sx9360.c +2
-0unsigned int hyst, val = _val;int ret, pthresh; +if (_val <0 ) +return -EINVAL ;
Same issue, checked variable which value has already been used.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerQuoted 2 lines · On Tue, 17 Nov 2026 02:23:08 +0530 Arnav Kapoor <[email protected]> wrote:
The remaining sx93xx fixes I had, sent as one series and grouped by driver, as asked by Jonathan.
I briefly looked at this lot and the discussion you and Andy have had. Looks like everything is heading in a good direction so I'll wait for v2.
Thanks,
Jonathan