Skip to contentSkip to conversation

Input: resistive-adc-touch - fix division by zero error on z1 == 0

PATCH Applied Oleksij Rempel 2 participants 2 messages Oct 7, 2021 – Oct 16, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Input: resistive-adc-touch - fix division by zero error on z1 == 0

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")
---
 .../input/touchscreen/resistive-adc-touch.c   | 29 ++++++++++---------
 1 file changed, 16 insertions(+), 13 deletions(-)
drivers/input/touchscreen/resistive-adc-touch.c +16 -13
index 744544a723b7..6f754a8d30b1 100644
@@ -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))) {
Signature · 1 line
2.30.2
maintainer Applied
#1 lore raw
Quoted 6 lines · Oleksij Rempel

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

Applied, thank you.

Signature · 1 line
Dmitry