* [PATCH v3 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq
2026-12-04 16:27 [PATCH v3 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
@ 2026-12-04 16:27 ` Arnav Kapoor
2026-12-04 16:27 ` [PATCH v3 2/6] iio: proximity: hx9023s: check the sampling frequency index Arnav Kapoor
` (4 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Arnav Kapoor @ 2026-12-04 16:27 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
linux-kernel, Arnav Kapoor
hx9023s_set_samp_freq() passes val * MEGA + val2 to div_u64(), whose
divisor is a u32, so the sum is truncated to 32 bits. MEGA is an
unsigned long, so on 64-bit the product itself does not overflow, but
the truncation does, and on 32-bit the product already wraps. Either
way, any value for which val * MEGA + val2 is a multiple of 2^32 gives a
zero divisor, e.g.
echo 67108864 > sampling_frequency
(67108864 * 10^6 = 15625 * 2^32) triggers a divide error, on both 32-bit
and 64-bit architectures. Negative values wrap the same way (-67108864
also gives 0), which the check added by commit a318cfc08537 ("iio:
proximity: hx9023s: Protect against division by zero in set_samp_freq")
does not catch, as it only rejects val == 0 && val2 == 0.
Reject negative values, and reject frequencies of 1 kHz and above: they
give a period of at most 1 ms, below the smallest entry (2 ms) of
hx9023s_samp_freq_table[], so they never matched anyway. With them gone
the divisor stays below 10^9 and fits in 32 bits, so a plain division is
enough. Spell the constants by their meaning: val2 is in micro-units, so
the frequency is counted in uHz and the period is
MSEC_PER_SEC * MICRO / (val * MICRO + val2).
Fixes: 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index 2a6da557f22e..47b5d2675401 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -25,6 +25,7 @@
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
+#include <linux/time.h>
#include <linux/types.h>
#include <linux/units.h>
@@ -718,10 +719,22 @@ static int hx9023s_set_samp_freq(struct hx9023s_data *data, int val, int val2)
struct device *dev = regmap_get_device(data->regmap);
unsigned int i, period_ms;
- if (!val && !val2)
+ if (val < 0 || val2 < 0 || (val == 0 && val2 == 0))
return -EINVAL;
- period_ms = div_u64(NANO, (val * MEGA + val2));
+ /*
+ * 1 kHz or more gives a period below the smallest table entry (2 ms),
+ * so it never matches. Rejecting it also keeps the divisor below 10^9,
+ * so it fits in 32 bits.
+ */
+ if (val >= 1 * HZ_PER_KHZ)
+ return -EINVAL;
+
+ /*
+ * The frequency is val + val2 / 10^6 Hz, so work in uHz to keep val2:
+ * period_ms = MSEC_PER_SEC / f = MSEC_PER_SEC * MICRO / f_uHz.
+ */
+ period_ms = MSEC_PER_SEC * MICRO / (val * MICRO + val2);
for (i = 0; i < ARRAY_SIZE(hx9023s_samp_freq_table); i++) {
if (period_ms == hx9023s_samp_freq_table[i])
--
2.53.0
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v3 2/6] iio: proximity: hx9023s: check the sampling frequency index
2026-12-04 16:27 [PATCH v3 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
2026-12-04 16:27 ` [PATCH v3 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
@ 2026-12-04 16:27 ` Arnav Kapoor
2026-12-04 16:28 ` [PATCH v3 3/6] iio: proximity: hx9023s: re-enable interrupts on resume for events Arnav Kapoor
` (3 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Arnav Kapoor @ 2026-12-04 16:27 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
linux-kernel, Arnav Kapoor
hx9023s_get_samp_freq() uses the PRF_CFG register value as an index
into hx9023s_samp_freq_table[], which has 32 entries, without checking
it. The driver only writes valid indexes itself, but the configuration
file loaded by hx9023s_bin_load() writes arbitrary register/value pairs,
so PRF_CFG can hold any 8-bit value. Reading sampling_frequency then
reads past the end of the table and divides KILO by whatever it finds
there, which may be zero.
Return -EINVAL if the index is outside the table.
Fixes: e9ed97be4fcc ("iio: proximity: hx9023s: Added firmware file parsing functionality")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index 47b5d2675401..75873a09e5fe 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -682,6 +682,9 @@ static int hx9023s_get_samp_freq(struct hx9023s_data *data, int *val, int *val2)
if (ret)
return ret;
+ if (index >= ARRAY_SIZE(hx9023s_samp_freq_table))
+ return -EINVAL;
+
odr = hx9023s_samp_freq_table[index];
*val = KILO / odr;
*val2 = div_u64((KILO % odr) * MICRO, odr);
--
2.53.0
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v3 3/6] iio: proximity: hx9023s: re-enable interrupts on resume for events
2026-12-04 16:27 [PATCH v3 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
2026-12-04 16:27 ` [PATCH v3 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
2026-12-04 16:27 ` [PATCH v3 2/6] iio: proximity: hx9023s: check the sampling frequency index Arnav Kapoor
@ 2026-12-04 16:28 ` Arnav Kapoor
2026-12-04 16:28 ` [PATCH v3 4/6] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
` (2 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Arnav Kapoor @ 2026-12-04 16:28 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
linux-kernel, Arnav Kapoor
The chip interrupts are enabled at probe (INTERRUPT_CFG = 0xff in the
register defaults) and stay enabled while only threshold events are in
use. hx9023s_suspend() disables them, but hx9023s_resume() re-enables
them only if the data-ready trigger is enabled. With events enabled and
no buffer running, the interrupts therefore stay off after the first
suspend/resume cycle and no proximity event is reported any more, until
a buffer is started.
Re-enable the interrupts on resume if any event is enabled as well.
Fixes: 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index 75873a09e5fe..ad1944bf6f6d 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -1198,7 +1198,7 @@ static int hx9023s_resume(struct device *dev)
struct hx9023s_data *data = iio_priv(dev_get_drvdata(dev));
guard(mutex)(&data->mutex);
- if (data->trigger_enabled)
+ if (data->trigger_enabled || data->chan_event)
hx9023s_interrupt_enable(data);
return 0;
--
2.53.0
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v3 4/6] iio: proximity: hx9023s: reject out-of-range threshold values
2026-12-04 16:27 [PATCH v3 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
` (2 preceding siblings ...)
2026-12-04 16:28 ` [PATCH v3 3/6] iio: proximity: hx9023s: re-enable interrupts on resume for events Arnav Kapoor
@ 2026-12-04 16:28 ` Arnav Kapoor
2026-12-04 16:28 ` [PATCH v3 5/6] iio: proximity: hx9023s: reject out-of-range debounce values Arnav Kapoor
2026-12-04 16:28 ` [PATCH v3 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq Arnav Kapoor
5 siblings, 0 replies; 8+ messages in thread
From: Arnav Kapoor @ 2026-12-04 16:28 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
linux-kernel, Arnav Kapoor
The proximity thresholds are 10-bit register fields in units of 32.
hx9023s_set_thres_near() and hx9023s_set_thres_far() mask val / 32 with
GENMASK(9, 0) instead of checking it, so a value out of range is
silently written as a different threshold: 32768 becomes 0, the most
sensitive threshold, and -32 becomes 1023 * 32 = 32736, the least
sensitive one.
Return -EINVAL for negative values and values above 32767, the largest
value that still maps to the field. val >= BIT(15) catches both, as
BIT() is an unsigned long. The low 5 bits are still dropped, since the
field is in units of 32; add a comment saying so.
Fixes: 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index ad1944bf6f6d..b534d3c9f10b 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -442,10 +442,16 @@ static int hx9023s_get_thres_far(struct hx9023s_data *data, u8 ch, int *val)
static int hx9023s_set_thres_near(struct hx9023s_data *data, u8 ch, int val)
{
- __le16 val_le16 = cpu_to_le16((val / 32) & GENMASK(9, 0));
unsigned int reg;
+ __le16 val_le16;
- data->ch_data[ch].thres.near = ((val / 32) & GENMASK(9, 0)) * 32;
+ if (val >= BIT(15))
+ return -EINVAL;
+
+ val_le16 = cpu_to_le16(val / 32);
+
+ /* The field is in units of 32, so the low 5 bits are dropped */
+ data->ch_data[ch].thres.near = val & ~GENMASK(4, 0);
reg = (ch == 4) ? HX9023S_PROX_HIGH_DIFF_CFG_CH4_0 :
HX9023S_PROX_HIGH_DIFF_CFG_CH0_0 + (ch * 2);
@@ -454,10 +460,16 @@ static int hx9023s_set_thres_near(struct hx9023s_data *data, u8 ch, int val)
static int hx9023s_set_thres_far(struct hx9023s_data *data, u8 ch, int val)
{
- __le16 val_le16 = cpu_to_le16((val / 32) & GENMASK(9, 0));
unsigned int reg;
+ __le16 val_le16;
+
+ if (val >= BIT(15))
+ return -EINVAL;
+
+ val_le16 = cpu_to_le16(val / 32);
- data->ch_data[ch].thres.far = ((val / 32) & GENMASK(9, 0)) * 32;
+ /* The field is in units of 32, so the low 5 bits are dropped */
+ data->ch_data[ch].thres.far = val & ~GENMASK(4, 0);
reg = (ch == 4) ? HX9023S_PROX_LOW_DIFF_CFG_CH4_0 :
HX9023S_PROX_LOW_DIFF_CFG_CH0_0 + (ch * 2);
--
2.53.0
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v3 5/6] iio: proximity: hx9023s: reject out-of-range debounce values
2026-12-04 16:27 [PATCH v3 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
` (3 preceding siblings ...)
2026-12-04 16:28 ` [PATCH v3 4/6] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
@ 2026-12-04 16:28 ` Arnav Kapoor
2026-12-04 16:28 ` [PATCH v3 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq Arnav Kapoor
5 siblings, 0 replies; 8+ messages in thread
From: Arnav Kapoor @ 2026-12-04 16:28 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
linux-kernel, Arnav Kapoor
The near and far debounce counts are 4-bit fields.
hx9023s_write_far_debounce() and hx9023s_write_near_debounce() pass the
value through FIELD_GET(), which only masks it here because the field
starts at bit 0, so an out-of-range value is silently written as a
different one: 16 becomes 0 (no debounce) and -1 becomes 15.
Return -EINVAL for values outside 0..15, and use FIELD_PREP(), which is
the right helper for building a register value.
Fixes: 60df548277b7 ("iio: proximity: Add driver support for TYHX's HX9023S capacitive proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index b534d3c9f10b..e10463359522 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -360,18 +360,24 @@ static int hx9023s_ch_cfg(struct hx9023s_data *data)
static int hx9023s_write_far_debounce(struct hx9023s_data *data, int val)
{
+ if (val < 0 || val > FIELD_MAX(HX9023S_PROX_DEBOUNCE_MASK))
+ return -EINVAL;
+
guard(mutex)(&data->mutex);
return regmap_update_bits(data->regmap, HX9023S_PROX_INT_LOW_CFG,
HX9023S_PROX_DEBOUNCE_MASK,
- FIELD_GET(HX9023S_PROX_DEBOUNCE_MASK, val));
+ FIELD_PREP(HX9023S_PROX_DEBOUNCE_MASK, val));
}
static int hx9023s_write_near_debounce(struct hx9023s_data *data, int val)
{
+ if (val < 0 || val > FIELD_MAX(HX9023S_PROX_DEBOUNCE_MASK))
+ return -EINVAL;
+
guard(mutex)(&data->mutex);
return regmap_update_bits(data->regmap, HX9023S_PROX_INT_HIGH_CFG,
HX9023S_PROX_DEBOUNCE_MASK,
- FIELD_GET(HX9023S_PROX_DEBOUNCE_MASK, val));
+ FIELD_PREP(HX9023S_PROX_DEBOUNCE_MASK, val));
}
static int hx9023s_read_far_debounce(struct hx9023s_data *data, int *val)
--
2.53.0
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH v3 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq
2026-12-04 16:27 [PATCH v3 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
` (4 preceding siblings ...)
2026-12-04 16:28 ` [PATCH v3 5/6] iio: proximity: hx9023s: reject out-of-range debounce values Arnav Kapoor
@ 2026-12-04 16:28 ` Arnav Kapoor
2026-12-04 17:44 ` Andy Shevchenko
5 siblings, 1 reply; 8+ messages in thread
From: Arnav Kapoor @ 2026-12-04 16:28 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, Andy Shevchenko, Yasin Lee, linux-iio,
linux-kernel, Arnav Kapoor
hx9023s_get_samp_freq() computes the fractional part of the sampling
frequency with div_u64(), but nothing here needs 64 bits: KILO % odr
is at most 1000, so the dividend is at most 10^9 and fits in an
unsigned long on 32-bit as well. Use a plain division, and drop the
linux/math64.h include, which has no other user left after the
previous patches.
No functional change.
Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
drivers/iio/proximity/hx9023s.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/drivers/iio/proximity/hx9023s.c b/drivers/iio/proximity/hx9023s.c
index e10463359522..5eb9f07f125a 100644
--- a/drivers/iio/proximity/hx9023s.c
+++ b/drivers/iio/proximity/hx9023s.c
@@ -18,7 +18,6 @@
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/irqreturn.h>
-#include <linux/math64.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/pm.h>
@@ -705,7 +704,7 @@ static int hx9023s_get_samp_freq(struct hx9023s_data *data, int *val, int *val2)
odr = hx9023s_samp_freq_table[index];
*val = KILO / odr;
- *val2 = div_u64((KILO % odr) * MICRO, odr);
+ *val2 = (KILO % odr) * MICRO / odr;
return IIO_VAL_INT_PLUS_MICRO;
}
--
2.53.0
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH v3 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq
2026-12-04 16:28 ` [PATCH v3 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq Arnav Kapoor
@ 2026-12-04 17:44 ` Andy Shevchenko
0 siblings, 0 replies; 8+ messages in thread
From: Andy Shevchenko @ 2026-12-04 17:44 UTC (permalink / raw)
To: Arnav Kapoor
Cc: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Yasin Lee, linux-iio, linux-kernel
On Fri, Dec 04, 2026 at 09:58:03PM +0530, Arnav Kapoor wrote:
> hx9023s_get_samp_freq() computes the fractional part of the sampling
> frequency with div_u64(), but nothing here needs 64 bits: KILO % odr
> is at most 1000, so the dividend is at most 10^9 and fits in an
> unsigned long on 32-bit as well. Use a plain division, and drop the
> linux/math64.h include, which has no other user left after the
> previous patches.
>
> No functional change.
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 8+ messages in thread