Re: [PATCH 4/5] iio: proximity: hx9023s: reject out-of-range threshold values
From: Andy Shevchenko
Date: Mon Nov 23 2026 - 14:26:16 EST
On Mon, Nov 23, 2026 at 10:22:50PM +0530, Arnav Kapoor wrote:
> 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.
...
> 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));
> + __le16 val_le16;
> unsigned int reg;
>
> - data->ch_data[ch].thres.near = ((val / 32) & GENMASK(9, 0)) * 32;
> + if (val < 0 || val / 32 > GENMASK(9, 0))
It's too complicated way of saying this
val >= BIT(15)
> + return -EINVAL;
> +
> + val_le16 = cpu_to_le16(val / 32);
> + data->ch_data[ch].thres.near = val / 32 * 32;
& ~GENMASK(4, 0)
In any case it might need a short comment explaining why these bits are
dropped.
> reg = (ch == 4) ? HX9023S_PROX_HIGH_DIFF_CFG_CH4_0 :
> HX9023S_PROX_HIGH_DIFF_CFG_CH0_0 + (ch * 2);
--
With Best Regards,
Andy Shevchenko