mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Arnav Kapoor <kapoorarnav43@gmail.com>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	"Yasin Lee" <yasin.lee.x@gmail.com>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 4/6] iio: proximity: hx9023s: reject out-of-range threshold values
Date: Fri, 27 Nov 2026 13:21:38 +0200	[thread overview]
Message-ID: <awlnwvtXj-K09MJI@ashevche-desk.local> (raw)
In-Reply-To: <20261126213710.1047676-5-kapoorarnav43@gmail.com>

On Fri, Nov 27, 2026 at 03:06:49AM +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. 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.

...

>  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;

Keep reversed xmas tree order.

> -	data->ch_data[ch].thres.near = ((val / 32) & GENMASK(9, 0)) * 32;
> +	if (val >= BIT(15))
> +		return -EINVAL;
> +
> +	/* The field is in units of 32, so the low 5 bits are dropped */
> +	val_le16 = cpu_to_le16(val / 32);
> +	data->ch_data[ch].thres.near = val & ~GENMASK(4, 0);

This comment seem detached from the actual expression. I don't see anything
wrong in having the

	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);

...

In the similar way in the second case.

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2026-11-27 11:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 21:36 [PATCH v2 0/6] iio: proximity: hx9023s: fix sampling frequency, resume and event value handling Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 1/6] iio: proximity: hx9023s: fix division by zero in set_samp_freq Arnav Kapoor
2026-11-27 11:17   ` Andy Shevchenko
2026-11-26 21:36 ` [PATCH v2 2/6] iio: proximity: hx9023s: check the sampling frequency index Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 3/6] iio: proximity: hx9023s: re-enable interrupts on resume for events Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 4/6] iio: proximity: hx9023s: reject out-of-range threshold values Arnav Kapoor
2026-11-27 11:21   ` Andy Shevchenko [this message]
2026-11-26 21:36 ` [PATCH v2 5/6] iio: proximity: hx9023s: reject out-of-range debounce values Arnav Kapoor
2026-11-26 21:36 ` [PATCH v2 6/6] iio: proximity: hx9023s: use plain division in get_samp_freq Arnav Kapoor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=awlnwvtXj-K09MJI@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=kapoorarnav43@gmail.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=yasin.lee.x@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®