All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Input: rotary_encoder - default steps to 24 to avoid divide by zero
@ 2026-12-05 11:28 Shirong Zhao
  2026-12-06  4:36 ` Dmitry Torokhov
  2026-12-06  4:43 ` [PATCH v2] Input: rotary-encoder - honour the documented default step count Shirong Zhao
  0 siblings, 2 replies; 3+ messages in thread
From: Shirong Zhao @ 2026-12-05 11:28 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: linux-input, stable, Shirong Zhao

The driver reads the device-tree property "rotary-encoder,steps" but
ignores the return value and applies no default.  If the property is
absent (or explicitly 0), steps stays 0 after the kzalloc'd structure is
filled, and the IRQ handler does

	if (encoder->rollover)
		pos %= encoder->steps;

i.e. a modulo by zero when "rotary-encoder,rollover" is set.  The binding
documented a default of 24; honour it and also treat a failing read as
using the default.

Fixes: 80c99bcd2861 ("Input: rotary-encoder - add DT bindings")
Cc: stable@vger.kernel.org
Signed-off-by: Shirong Zhao <shxzhaosr@163.com>
---
 drivers/input/misc/rotary_encoder.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c
index 6c932e4..440ac27 100644
--- a/drivers/input/misc/rotary_encoder.c
+++ b/drivers/input/misc/rotary_encoder.c
@@ -30,6 +30,8 @@ enum rotary_encoder_encoding {
 	ROTENC_BINARY,
 };
 
+#define ROTARY_ENCODER_DEFAULT_STEPS	24
+
 struct rotary_encoder {
 	struct input_dev *input;
 
@@ -191,7 +193,9 @@ static int rotary_encoder_probe(struct platform_device *pdev)
 
 	mutex_init(&encoder->access_mutex);
 
-	device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps);
+	if (device_property_read_u32(dev, "rotary-encoder,steps",
+				     &encoder->steps) || !encoder->steps)
+		encoder->steps = ROTARY_ENCODER_DEFAULT_STEPS;
 
 	err = device_property_read_u32(dev, "rotary-encoder,steps-per-period",
 				       &steps_per_period);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Input: rotary_encoder - default steps to 24 to avoid divide by zero
  2026-12-05 11:28 [PATCH] Input: rotary_encoder - default steps to 24 to avoid divide by zero Shirong Zhao
@ 2026-12-06  4:36 ` Dmitry Torokhov
  2026-12-06  4:43 ` [PATCH v2] Input: rotary-encoder - honour the documented default step count Shirong Zhao
  1 sibling, 0 replies; 3+ messages in thread
From: Dmitry Torokhov @ 2026-12-06  4:36 UTC (permalink / raw)
  To: Shirong Zhao, Rob Herring; +Cc: linux-input, stable

Hi Shirong,

On Sat, Dec 05, 2026 at 07:28:55PM +0800, Shirong Zhao wrote:
> The driver reads the device-tree property "rotary-encoder,steps" but
> ignores the return value and applies no default.  If the property is
> absent (or explicitly 0), steps stays 0 after the kzalloc'd structure is
> filled, and the IRQ handler does
> 
> 	if (encoder->rollover)
> 		pos %= encoder->steps;
> 
> i.e. a modulo by zero when "rotary-encoder,rollover" is set.  The binding
> documented a default of 24; honour it and also treat a failing read as
> using the default.

I can see using the default if the schema says its an optional property,
but if somebody trues to specify 0 why not reject it?

> 
> Fixes: 80c99bcd2861 ("Input: rotary-encoder - add DT bindings")
> Cc: stable@vger.kernel.org

In any case, I do not see the reason for this to be in stable. DT data
it is not an untrusted input.

I really am not sure how much we want to validate in kernel vs relying
on schema validation. Rob, anything from your side?

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2] Input: rotary-encoder - honour the documented default step count
  2026-12-05 11:28 [PATCH] Input: rotary_encoder - default steps to 24 to avoid divide by zero Shirong Zhao
  2026-12-06  4:36 ` Dmitry Torokhov
@ 2026-12-06  4:43 ` Shirong Zhao
  1 sibling, 0 replies; 3+ messages in thread
From: Shirong Zhao @ 2026-12-06  4:43 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: linux-input, Rob Herring, Shirong Zhao

The driver reads "rotary-encoder,steps" but ignores the return value.  The
binding documents the property as optional with a default of 24, yet the
driver leaves steps at 0 when the property is absent, and the IRQ handler
then does

	if (encoder->rollover)
		pos %= encoder->steps;

i.e. a modulo by zero when rollover is also set.  A zero step count also
makes input_set_abs_params() advertise an empty range.

Use the documented default when the property is absent, and reject an
explicit value of 0 because a full turnaround cannot have zero steps.

Fixes: 80c99bcd2861 ("Input: rotary-encoder - add DT bindings")
Signed-off-by: Shirong Zhao <shxzhaosr@163.com>
---
v2:
 - Use the default only when the property is absent and reject an
   explicit zero with -EINVAL, as requested by Dmitry Torokhov.
 - Drop Cc: stable; DT data is not untrusted input.

 drivers/input/misc/rotary_encoder.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c
index 6c932e4..9bf5472 100644
--- a/drivers/input/misc/rotary_encoder.c
+++ b/drivers/input/misc/rotary_encoder.c
@@ -30,6 +30,8 @@ enum rotary_encoder_encoding {
 	ROTENC_BINARY,
 };
 
+#define ROTARY_ENCODER_DEFAULT_STEPS	24
+
 struct rotary_encoder {
 	struct input_dev *input;
 
@@ -191,7 +193,14 @@ static int rotary_encoder_probe(struct platform_device *pdev)
 
 	mutex_init(&encoder->access_mutex);
 
-	device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps);
+	err = device_property_read_u32(dev, "rotary-encoder,steps", &encoder->steps);
+	if (err) {
+		/* The binding documents a default of 24 steps. */
+		encoder->steps = ROTARY_ENCODER_DEFAULT_STEPS;
+	} else if (!encoder->steps) {
+		dev_err(dev, "rotary-encoder,steps must not be zero\n");
+		return -EINVAL;
+	}
 
 	err = device_property_read_u32(dev, "rotary-encoder,steps-per-period",
 				       &steps_per_period);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-06  4:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 11:28 [PATCH] Input: rotary_encoder - default steps to 24 to avoid divide by zero Shirong Zhao
2026-12-06  4:36 ` Dmitry Torokhov
2026-12-06  4:43 ` [PATCH v2] Input: rotary-encoder - honour the documented default step count Shirong Zhao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.