linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] Input: resistive-adc-touch - fix division by zero error on z1 == 0
@ 2021-10-07  9:57 Oleksij Rempel
  2021-10-16  4:16 ` Dmitry Torokhov
  0 siblings, 1 reply; 2+ messages in thread
From: Oleksij Rempel @ 2021-10-07  9:57 UTC (permalink / raw)
  To: Dmitry Torokhov, Eugen Hristev
  Cc: Oleksij Rempel, kernel, linux-kernel, linux-input, David Jander

For proper pressure calculation we need at least x and z1 to be non
zero. Even worse, in case z1 we may run in to division by zero
error.

Fixes: 60b7db914ddd ("Input: resistive-adc-touch - rework mapping of channels")
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 .../input/touchscreen/resistive-adc-touch.c   | 29 ++++++++++---------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/drivers/input/touchscreen/resistive-adc-touch.c b/drivers/input/touchscreen/resistive-adc-touch.c
index 744544a723b7..6f754a8d30b1 100644
--- a/drivers/input/touchscreen/resistive-adc-touch.c
+++ b/drivers/input/touchscreen/resistive-adc-touch.c
@@ -71,19 +71,22 @@ static int grts_cb(const void *data, void *private)
 		unsigned int z2 = touch_info[st->ch_map[GRTS_CH_Z2]];
 		unsigned int Rt;
 
-		Rt = z2;
-		Rt -= z1;
-		Rt *= st->x_plate_ohms;
-		Rt = DIV_ROUND_CLOSEST(Rt, 16);
-		Rt *= x;
-		Rt /= z1;
-		Rt = DIV_ROUND_CLOSEST(Rt, 256);
-		/*
-		 * On increased pressure the resistance (Rt) is decreasing
-		 * so, convert values to make it looks as real pressure.
-		 */
-		if (Rt < GRTS_DEFAULT_PRESSURE_MAX)
-			press = GRTS_DEFAULT_PRESSURE_MAX - Rt;
+		if (likely(x && z1)) {
+			Rt = z2;
+			Rt -= z1;
+			Rt *= st->x_plate_ohms;
+			Rt = DIV_ROUND_CLOSEST(Rt, 16);
+			Rt *= x;
+			Rt /= z1;
+			Rt = DIV_ROUND_CLOSEST(Rt, 256);
+			/*
+			 * On increased pressure the resistance (Rt) is
+			 * decreasing so, convert values to make it looks as
+			 * real pressure.
+			 */
+			if (Rt < GRTS_DEFAULT_PRESSURE_MAX)
+				press = GRTS_DEFAULT_PRESSURE_MAX - Rt;
+		}
 	}
 
 	if ((!x && !y) || (st->pressure && (press < st->pressure_min))) {
-- 
2.30.2


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

* Re: [PATCH v1] Input: resistive-adc-touch - fix division by zero error on z1 == 0
  2021-10-07  9:57 [PATCH v1] Input: resistive-adc-touch - fix division by zero error on z1 == 0 Oleksij Rempel
@ 2021-10-16  4:16 ` Dmitry Torokhov
  0 siblings, 0 replies; 2+ messages in thread
From: Dmitry Torokhov @ 2021-10-16  4:16 UTC (permalink / raw)
  To: Oleksij Rempel
  Cc: Eugen Hristev, kernel, linux-kernel, linux-input, David Jander

On Thu, Oct 07, 2021 at 11:57:27AM +0200, Oleksij Rempel wrote:
> For proper pressure calculation we need at least x and z1 to be non
> zero. Even worse, in case z1 we may run in to division by zero
> error.
> 
> Fixes: 60b7db914ddd ("Input: resistive-adc-touch - rework mapping of channels")
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>

Applied, thank you.

-- 
Dmitry

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

end of thread, other threads:[~2021-10-16  4:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-07  9:57 [PATCH v1] Input: resistive-adc-touch - fix division by zero error on z1 == 0 Oleksij Rempel
2021-10-16  4:16 ` Dmitry Torokhov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).