Skip to content

Commit fe0a7e3

Browse files
oleremdtor
authored andcommitted
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: 60b7db9 ("Input: resistive-adc-touch - rework mapping of channels") Signed-off-by: Oleksij Rempel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent d997cc1 commit fe0a7e3

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

drivers/input/touchscreen/resistive-adc-touch.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,22 @@ static int grts_cb(const void *data, void *private)
7171
unsigned int z2 = touch_info[st->ch_map[GRTS_CH_Z2]];
7272
unsigned int Rt;
7373

74-
Rt = z2;
75-
Rt -= z1;
76-
Rt *= st->x_plate_ohms;
77-
Rt = DIV_ROUND_CLOSEST(Rt, 16);
78-
Rt *= x;
79-
Rt /= z1;
80-
Rt = DIV_ROUND_CLOSEST(Rt, 256);
81-
/*
82-
* On increased pressure the resistance (Rt) is decreasing
83-
* so, convert values to make it looks as real pressure.
84-
*/
85-
if (Rt < GRTS_DEFAULT_PRESSURE_MAX)
86-
press = GRTS_DEFAULT_PRESSURE_MAX - Rt;
74+
if (likely(x && z1)) {
75+
Rt = z2;
76+
Rt -= z1;
77+
Rt *= st->x_plate_ohms;
78+
Rt = DIV_ROUND_CLOSEST(Rt, 16);
79+
Rt *= x;
80+
Rt /= z1;
81+
Rt = DIV_ROUND_CLOSEST(Rt, 256);
82+
/*
83+
* On increased pressure the resistance (Rt) is
84+
* decreasing so, convert values to make it looks as
85+
* real pressure.
86+
*/
87+
if (Rt < GRTS_DEFAULT_PRESSURE_MAX)
88+
press = GRTS_DEFAULT_PRESSURE_MAX - Rt;
89+
}
8790
}
8891

8992
if ((!x && !y) || (st->pressure && (press < st->pressure_min))) {

0 commit comments

Comments
 (0)