Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp1201378pxb; Thu, 7 Oct 2021 03:00:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdCVr+rP3QFr0URsgFPqXM++uNnedKXhNCCzIm9hM7bP9ya6Djt5pDCpFlAkl7RfzbbUqs X-Received: by 2002:a50:9b06:: with SMTP id o6mr4963964edi.284.1633600849752; Thu, 07 Oct 2021 03:00:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633600849; cv=none; d=google.com; s=arc-20160816; b=JCXxyH/Im9dD6XfcRQKIIgQ/SFvIa68buQkAUb4WFwRQjQnPJ3gHFMaojAufSYCCaA pvIsCR7u4DY5zFlYedE9hT+YY3OBO3fEpK7U0ggmAGfBAAPYyVpuM/yh229TICwY/PRx TDnEaFhGfUL3XYm81yT1u0vT8+qphutFgpnu3RmT4CD2tabm1RRKhEXfcX1Ank3Kv0ON Kuv148Ach2o6WvP6gqya/RdNuWLhwz0GP8CJiTK2cj6SjnoitLudNjWIZnLb7FxYlLq2 1NY6FEfbT1+/8oSy7QxFxSSdydcSc3SXQfs5usdlXLc20PYh6jY4+uJ+zbLQ/jOMu9t6 +pgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=H7SoFqwmIFc95zQ3BZ0urjDiSsphx8obIT83n4yKCNE=; b=iRy6Vv9wpUlKmw7rCG9kB9TfGBtSov/PHINOsZCfGdI3USNBKun47HxS1geKJzsBSt eDBeOdP/JR6H/v5pubtm9OsCxEltGMzzds2iqSJOPliLg3MICDb3bN9ITL9xF6PD4ND4 EjzYm9WFMkHtTN+I60CLro2csd9v31uqiMbpcqRuXbfMMm8vyQuQMGfvysYTEe6//3tJ 6c6pdw+ZE3pDJuCosNKd5w8ZOATRI39GzUe2v2c6MgVSSsaWk3OL99AH1NA1fBetEL7y hen/5QsXTXuDfoHhWs3zY/a4c9Sr8PYUaIzPh/0dimjyQPJ56gw2WCOsQFsj1zT813B/ qm3Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id eb8si589004edb.579.2021.10.07.03.00.22; Thu, 07 Oct 2021 03:00:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232678AbhJGJ73 (ORCPT + 99 others); Thu, 7 Oct 2021 05:59:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232638AbhJGJ70 (ORCPT ); Thu, 7 Oct 2021 05:59:26 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA8D8C061760 for ; Thu, 7 Oct 2021 02:57:32 -0700 (PDT) Received: from dude.hi.pengutronix.de ([2001:67c:670:100:1d::7]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mYQ9a-0001oY-EZ; Thu, 07 Oct 2021 11:57:30 +0200 Received: from ore by dude.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1mYQ9Z-0007hx-Gq; Thu, 07 Oct 2021 11:57:29 +0200 From: Oleksij Rempel To: Dmitry Torokhov , Eugen Hristev Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, David Jander Subject: [PATCH v1] Input: resistive-adc-touch - fix division by zero error on z1 == 0 Date: Thu, 7 Oct 2021 11:57:27 +0200 Message-Id: <20211007095727.29579-1-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::7 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- .../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