Skip to contentSkip to conversation

iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain

PATCH New Arnav Kapoor 1 participant 7 messages Nov 22 IIO

lore
Share & tools
lore
#0 lore raw

v2 cover iio: proximity: sx93xx: fix raw reads, thresholds, hysteresis and gain

The remaining sx93xx fixes, grouped by driver.

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.
2, 5: sx9324/sx9360 write_thresh() lowers the threshold by one register
value when the value just read from an odd register value is written back, and lets some negative values through.
3, 6: sx9324/sx9360 write_hysteresis() turns negative values into the
largest hysteresis.
4: sx9360 gain encoding, as in Semtech's own SX9360 driver and as
sx9324 got in 108e4d4de2b5.

Changes in v2:
- Rebase on the current iio testing, which now has all the patches v1 depended on
- 2, 5: check _val before it is used, and before reg is assigned; val > 1 instead of val >= 1 (Andy)
- 2, 5: commit message rewritten as agreed with Andy: drop the (r * r - 1) / 2 formula, explain why int_sqrt(2 * val + 1) is right in integer arithmetic, and say that the negative value wrap happens on both 32-bit and 64-bit
- 3, 6: check _val before it is used (Andy); say in the commit message that it happens on both 32-bit and 64-bit (Andy)

v1: https://lore.kernel.org/all/[email protected]/

The threshold change was checked against a userspace copy of both conversions for every val from 0 to 65535: all 256 register values round-trip, and every write picks the largest register value whose reported threshold doesn't exceed the request. Compile-tested only (W=1, checkpatch --strict clean); 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    | 18 ++++++++++----
 drivers/iio/proximity/sx9360.c    | 39 +++++++++++++++++++++++--------
 drivers/iio/proximity/sx_common.c | 25 +++++++++++++++-----
 3 files changed, 62 insertions(+), 20 deletions(-)

base-commit: 42d25ad999bd60b96b227c2e54391c82cb03d00d

Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 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.

Fixes 72ad02b15d63 ("iio: Add SEMTECH SX9310/9311 sensor driver")
---
 drivers/iio/proximity/sx_common.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
drivers/iio/proximity/sx_common.c +19 -6
index 4d4937395d9a..66d06435da05 100644
@@ -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
#2 lore raw

PATCH v2 2/6 iio: proximity: sx9324: fix proximity threshold writes

In reply to Arnav Kapoor

sx9324_read_thresh() reports a PROX_THRESH register value r as r for r <= 1 and as r * r / 2 otherwise, and the division rounds down for odd r. sx9324_write_thresh() inverts that with int_sqrt(2 * val), which is one register value too low when 2 * val == r * r - 1, so writing back the threshold read from an odd register value lowers it by one:
setting the threshold to the value it already has changes it, for 127 of the 256 register values (3, 5, 7, ...).

Use int_sqrt(2 * val + 1), which gives the largest register value whose threshold does not exceed val: for r > 1, 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. Checked for every val from 0 to 65535.

Also reject negative values. They are converted to unsigned int, and 2 * val wraps at 32 bits on both 32-bit and 64-bit architectures, so e.g. INT_MIN is written as 0 and values just above INT_MIN as small thresholds instead of being rejected.

Fixes 4c18a890dff8 ("iio:proximity:sx9324: Add SX9324 support")
---
 drivers/iio/proximity/sx9324.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/iio/proximity/sx9324.c +8 -3
index 93f9577bbda7..57fe78089167 100644
@@ -613,12 +613,17 @@ static int sx9324_read_event_val(struct iio_dev *indio_dev,
 static int sx9324_write_thresh(struct sx_common_data *data,
 			       const struct iio_chan_spec *chan, int _val)
 {
-	unsigned int reg, val = _val;
+	unsigned int reg, val;
+
+	if (_val < 0)
+		return -EINVAL;
 
 	reg = SX9324_REG_PROX_CTRL6 + chan->channel / 2;
+	val = _val;
 
-	if (val >= 1)
-		val = int_sqrt(2 * val);
+	/* Inverse of read_thresh(), so that writing back a read value is a no-op */
+	if (val > 1)
+		val = int_sqrt(2 * val + 1);
 
 	if (val > 0xff)
 		return -EINVAL;
Signature · 1 line
2.53.0
#3 lore raw

PATCH v2 3/6 iio: proximity: sx9324: reject negative hysteresis values

In reply to 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. The comparison is done in unsigned int, so this happens on both 32-bit and 64-bit architectures.

Return -EINVAL for negative values, before converting them.

Fixes 4c18a890dff8 ("iio:proximity:sx9324: Add SX9324 support")
---
 drivers/iio/proximity/sx9324.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/proximity/sx9324.c +6 -1
index 57fe78089167..35f0cca5ede3 100644
@@ -636,9 +636,14 @@ static int sx9324_write_thresh(struct sx_common_data *data,
 static int sx9324_write_hysteresis(struct sx_common_data *data,
 				   const struct iio_chan_spec *chan, int _val)
 {
-	unsigned int hyst, val = _val;
+	unsigned int hyst, val;
 	int ret, pthresh;
 
+	if (_val < 0)
+		return -EINVAL;
+
+	val = _val;
+
 	ret = sx9324_read_thresh(data, chan, &pthresh);
 	if (ret < 0)
 		return ret;
Signature · 1 line
2.53.0
#4 lore raw

PATCH v2 4/6 iio: proximity: sx9360: fix hardware gain encoding

In reply to Arnav Kapoor

The 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.

Fixes 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support")
---
 drivers/iio/proximity/sx9360.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/iio/proximity/sx9360.c +14 -6
index d102e20d4084..9ba967a33c98 100644
@@ -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
#5 lore raw

PATCH v2 5/6 iio: proximity: sx9360: fix proximity threshold writes

In reply to Arnav Kapoor

sx9360_read_thresh() reports a PROX_THRESH register value r as r for r <= 1 and as r * r / 2 otherwise, and the division rounds down for odd r. sx9360_write_thresh() inverts that with int_sqrt(2 * val), which is one register value too low when 2 * val == r * r - 1, so writing back the threshold read from an odd register value lowers it by one:
setting the threshold to the value it already has changes it, for 127 of the 256 register values (3, 5, 7, ...).

Use int_sqrt(2 * val + 1), which gives the largest register value whose threshold does not exceed val: for r > 1, 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. Checked for every val from 0 to 65535.

Also reject negative values. They are converted to unsigned int, and 2 * val wraps at 32 bits on both 32-bit and 64-bit architectures, so e.g. INT_MIN is written as 0 and values just above INT_MIN as small thresholds instead of being rejected.

Fixes 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support")
---
 drivers/iio/proximity/sx9360.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/iio/proximity/sx9360.c +9 -3
index 9ba967a33c98..9a2796f67949 100644
@@ -508,10 +508,16 @@ static int sx9360_read_event_val(struct iio_dev *indio_dev,
 
 static int sx9360_write_thresh(struct sx_common_data *data, int _val)
 {
-	unsigned int val = _val;
+	unsigned int val;
+
+	if (_val < 0)
+		return -EINVAL;
+
+	val = _val;
 
-	if (val >= 1)
-		val = int_sqrt(2 * val);
+	/* Inverse of read_thresh(), so that writing back a read value is a no-op */
+	if (val > 1)
+		val = int_sqrt(2 * val + 1);
 
 	if (val > 0xff)
 		return -EINVAL;
Signature · 1 line
2.53.0
#6 lore raw

PATCH v2 6/6 iio: proximity: sx9360: reject negative hysteresis values

In reply to 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. The comparison is done in unsigned int, so this happens on both 32-bit and 64-bit architectures.

Return -EINVAL for negative values, before converting them.

Fixes 1cdb4c47f7f5 ("iio:proximity:sx9360: Add sx9360 support")
---
 drivers/iio/proximity/sx9360.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/proximity/sx9360.c +6 -1
index 9a2796f67949..21c1801cc2ff 100644
@@ -528,9 +528,14 @@ static int sx9360_write_thresh(struct sx_common_data *data, int _val)
 
 static int sx9360_write_hysteresis(struct sx_common_data *data, int _val)
 {
-	unsigned int hyst, val = _val;
+	unsigned int hyst, val;
 	int ret, pthresh;
 
+	if (_val < 0)
+		return -EINVAL;
+
+	val = _val;
+
 	ret = sx9360_read_thresh(data, &pthresh);
 	if (ret < 0)
 		return ret;
Signature · 1 line
2.53.0