Skip to content

Commit

Permalink
Input: resistive-adc-touch - fix division by zero error on z1 == 0
Browse files Browse the repository at this point in the history
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 <o.rempel@pengutronix.de>
Link: https://lore.kernel.org/r/20211007095727.29579-1-o.rempel@pengutronix.de
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
  • Loading branch information
Oleksij Rempel authored and Dmitry Torokhov committed Oct 16, 2021
1 parent d997cc1 commit fe0a7e3
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions drivers/input/touchscreen/resistive-adc-touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down

0 comments on commit fe0a7e3

Please sign in to comment.