]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/commitdiff
Input: ads7846 - fix integer overflow on Rt calculation
authorOleksij Rempel <o.rempel@pengutronix.de>
Tue, 17 Nov 2020 23:33:24 +0000 (15:33 -0800)
committerKleber Sacilotto de Souza <kleber.souza@canonical.com>
Wed, 20 Jan 2021 13:25:44 +0000 (14:25 +0100)
BugLink: https://bugs.launchpad.net/bugs/1910822
[ Upstream commit 820830ec918f6c3dcd77a54a1c6198ab57407916 ]

In some rare cases the 32 bit Rt value will overflow if z2 and x is max,
z1 is minimal value and x_plate_ohms is relatively high (for example 800
ohm). This would happen on some screen age with low pressure.

There are two possible fixes:
- make Rt 64bit
- reorder calculation to avoid overflow

The second variant seems to be preferable, since 64 bit calculation on
32 bit system is a bit more expensive.

Fixes: ffa458c1bd9b6f653008d450f337602f3d52a646 ("spi: ads7846 driver")
Co-developed-by: David Jander <david@protonic.nl>
Signed-off-by: David Jander <david@protonic.nl>
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
Link: https://lore.kernel.org/r/20201113112240.1360-1-o.rempel@pengutronix.de
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Kelsey Skunberg <kelsey.skunberg@canonical.com>
drivers/input/touchscreen/ads7846.c

index 1e19af0da13a3a78347cbf5c07d975446abf0295..a5b1b41464f994a481f3166d8e4d547beeb27291 100644 (file)
@@ -801,10 +801,11 @@ static void ads7846_report_state(struct ads7846 *ts)
                /* compute touch pressure resistance using equation #2 */
                Rt = z2;
                Rt -= z1;
-               Rt *= x;
                Rt *= ts->x_plate_ohms;
+               Rt = DIV_ROUND_CLOSEST(Rt, 16);
+               Rt *= x;
                Rt /= z1;
-               Rt = (Rt + 2047) >> 12;
+               Rt = DIV_ROUND_CLOSEST(Rt, 256);
        } else {
                Rt = 0;
        }