Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp4315323ybc; Fri, 22 Nov 2019 00:28:38 -0800 (PST) X-Google-Smtp-Source: APXvYqyVDcEkWs0kVHIhWOssWtu8CZ2yNtX7GGOVM+bik2tKs6mmrkvVAkoyNiUVr2jVOalM1kfr X-Received: by 2002:a17:906:4697:: with SMTP id a23mr20555783ejr.322.1574411318866; Fri, 22 Nov 2019 00:28:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574411318; cv=none; d=google.com; s=arc-20160816; b=fwqypEZ1WgnhpNU3oHtsUOvEcOF8Js9uaTu3umMKm41L7UiG+3iQhJnL2AYTGqUyYQ S16HUmMhkP7nadp0D85WsyIZwQOP7QIuDk4X71AZpEFxaY6iuOdTOMSAz5Cy/0WZrm+6 UjDVHVGCgARlNPnAy5pm2O921TsZkdfERGpnoPbT+GdT8dVbt8gbQ/pUxtoMLIspiB6E UOozUdCeaEIV+rdHl03/O3bP59fRJjM8bKeKreqQ4Pkjf7qg+ILES552UraxHoSfdkYG i6BKwknfeaDbBgtmuEfwRzt1Nl7OvkWh6s6UWYvM17wrER31DIz3FX4pStI/dyBCQWnm EdiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:ironport-sdr:ironport-sdr; bh=6fPvObI9SA0lQ4YX3wme2TzAVIz5qJ238IViHiDjGCY=; b=T9WGoHep6Xc+M6pFZ/TyT1QpOacaDDGqkJaIuzqikE05kTiCY+YMFkQqQchn4wXo8S Ge9kBrDV2YzN0Q1p9OAyyYgaMGo29YSt0i7+lKU8xdyp+UXIkNgppTjW+7gNozGyypqU 8+TtMdh+doR5QA9JEWZSEeFiu9J3ODjXPSsTngS53q3IPT/pRM1+rkDu3NCwoNRmfe9W Yr5MJvxIpAk/GLlH6b80UFcRufTNJa/8iAyV/nBLUp4QZUIfh2mMhf3Y70DafGpMzkkI Q/S1aMHjD9EPotjS4C7QrIGBMWajJ6ELq8+ZR34T7j5PUq5rYOF0186xV0MshneRg0YR /B0A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h13si825184ejq.54.2019.11.22.00.28.15; Fri, 22 Nov 2019 00:28:38 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726540AbfKVI0W (ORCPT + 99 others); Fri, 22 Nov 2019 03:26:22 -0500 Received: from esa4.mentor.iphmx.com ([68.232.137.252]:62590 "EHLO esa4.mentor.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726554AbfKVI0V (ORCPT ); Fri, 22 Nov 2019 03:26:21 -0500 IronPort-SDR: yIzYNJRli41nIe4nEjz/Nxu1GACo24BisGR/HvPoZr4syCav1HxQx+pcTd60npUOX+9MGNaSrs jKsuL0bBK8gt3l6x2TcMYQZXNpQjOsRKuScHKz3NbnrsZBStqvEmr8DgVJK9H6JVto5BLv32a9 lEKYE2TiFskDzHzeGihrWQRXRJlaKJNi8tELclGvubFV2W55Njkzq2WDubaNK/vKfmA4RQYabl zPTj0ZID0siOUfRAGUoSf6bF6Fa+udrboxHIxhgsKOt+AXytikRa/dSKP35Z3r8S4+3K9EuaPP fEg= X-IronPort-AV: E=Sophos;i="5.69,229,1571731200"; d="scan'208";a="43457626" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa4.mentor.iphmx.com with ESMTP; 22 Nov 2019 00:26:20 -0800 IronPort-SDR: t6jf3W1i1xNTlb/PWrS7U5etSeJoOSh81wdIz+EGWUnkTiwi1t0MYBgxbw6JD+o8r9T/snKyDt s7PV7ifRqBE6xxqoDDUdhDPqLW1vKspP+IUoPOn6kNAriFEpMM3lnKJi4/2z+jG+YoP0+cRf+I dX86cJiiZb1FZBhaD4U8zmDph6LGbTkX2XNG3+LusKBUBIYsqboIUWRvAdQQmSfRbKVZ57qEUk 9TVttOvh6CjKabg0l3Q+B8MphFugI79DI5eVrydgMjbSVRMVo+4Czc64TLw1oeuKZKTIhI6sGk 3jM= From: Jiada Wang To: , , , , CC: , , , , Subject: [PATCH v6 36/48] Input: atmel_mxt_ts: Limit the max bytes transferred in an i2c transaction Date: Fri, 22 Nov 2019 17:23:50 +0900 Message-ID: <20191122082402.18173-37-jiada_wang@mentor.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191122082402.18173-1-jiada_wang@mentor.com> References: <20191122082402.18173-1-jiada_wang@mentor.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Balasubramani Vivekanandan In mxt_process_messages_until_invalid() function, driver tries to read all possible reportid in a single i2c transaction. Number of bytes read is limited by the max_reportid parameter. If the max_reportid is a very large value, then a large chunk of bytes will be requested from the controller in a single i2c transaction. This transaction can fail due to timeout. This is visible when the Atmel controller is connected to the SOC via a i2c mux hardware. mxt_process_messages_t44() reads the T44 message which contains the pending T5 message count. If the number of pending T5 messages returned by T44 message is too high then there is a risk of i2c transaction timeout while reading T5 messages in mxt_process_messages_t44(). New property 'atmel,mtu' is created. This property limits the maximum number of bytes that can read/transferred in an i2c transcation Signed-off-by: Balasubramani Vivekanandan Signed-off-by: George G. Davis Signed-off-by: Jiada Wang --- .../bindings/input/atmel,maxtouch.txt | 3 + drivers/input/touchscreen/atmel_mxt_ts.c | 65 +++++++++++++++---- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt index d7db16920083..62c93d94bc5d 100644 --- a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt +++ b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt @@ -42,6 +42,8 @@ Optional properties for main touchpad device: - atmel,input_name: Override name of input device from the default. +- atmel,mtu: Maximum number of bytes that can read/transferred in an i2c transaction + Example: touch@4b { @@ -49,4 +51,5 @@ Example: reg = <0x4b>; interrupt-parent = <&gpio>; interrupts = ; + atmel,mtu = <200> }; diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c b/drivers/input/touchscreen/atmel_mxt_ts.c index 74866015572f..054aacc768b8 100644 --- a/drivers/input/touchscreen/atmel_mxt_ts.c +++ b/drivers/input/touchscreen/atmel_mxt_ts.c @@ -417,6 +417,7 @@ struct mxt_data { /* Indicates whether device is updating configuration */ bool updating_config; + unsigned int mtu; bool t25_status; }; @@ -1465,11 +1466,32 @@ static int mxt_read_and_process_messages(struct mxt_data *data, u8 count) return num_valid; } +static u8 mxt_max_msg_read_count(struct mxt_data *data, u8 max_T5_msg_count) +{ + u8 T5_msg_count_limit = data->mtu / data->T5_msg_size; + + if (!data->mtu) + return max_T5_msg_count; + + if (data->mtu < data->T5_msg_size) { + WARN(1, "mtu set is lesser than the T5 message size\n"); + /* Return count of 1, as fallback */ + return 1; + } + /* + * Return maximum number of T5 messages in single i2c transaction + * based on "atmel,mtu" property. + */ + return min(T5_msg_count_limit, max_T5_msg_count); +} + static irqreturn_t mxt_process_messages_t44(struct mxt_data *data) { struct device *dev = &data->client->dev; int ret; - u8 count, num_left; + u8 T5_msg_count, total_pending; + u8 total_processed = 0; + u8 processed_valid = 0; /* Read T44 and T5 together */ ret = __mxt_read_reg(data->client, data->T44_address, @@ -1479,18 +1501,19 @@ static irqreturn_t mxt_process_messages_t44(struct mxt_data *data) return IRQ_NONE; } - count = data->msg_buf[0]; + T5_msg_count = data->msg_buf[0]; /* * This condition may be caused by the CHG line being configured in * Mode 0. It results in unnecessary I2C operations but it is benign. */ - if (count == 0) + if (!T5_msg_count) return IRQ_NONE; - if (count > data->max_reportid) { - dev_warn(dev, "T44 count %d exceeded max report id\n", count); - count = data->max_reportid; + if (T5_msg_count > data->max_reportid) { + dev_warn(dev, "T44 count %d exceeded max report id\n", + T5_msg_count); + T5_msg_count = data->max_reportid; } /* Process first message */ @@ -1500,16 +1523,25 @@ static irqreturn_t mxt_process_messages_t44(struct mxt_data *data) return IRQ_NONE; } - num_left = count - 1; + total_pending = T5_msg_count - 1; + if (!total_pending) + goto end; /* Process remaining messages if necessary */ - if (num_left) { - ret = mxt_read_and_process_messages(data, num_left); + T5_msg_count = mxt_max_msg_read_count(data, total_pending); + + do { + if ((total_pending - total_processed) < T5_msg_count) + T5_msg_count = total_pending - total_processed; + ret = mxt_read_and_process_messages(data, T5_msg_count); if (ret < 0) goto end; - else if (ret != num_left) - dev_warn(dev, "Unexpected invalid message\n"); - } + total_processed += T5_msg_count; + processed_valid += ret; + } while (total_processed < total_pending); + + if (processed_valid != total_pending) + dev_warn(dev, "Unexpected invalid message\n"); end: if (data->update_input) { @@ -1524,9 +1556,10 @@ static int mxt_process_messages_until_invalid(struct mxt_data *data) { struct device *dev = &data->client->dev; int count, read; - u8 tries = 2; + int tries; - count = data->max_reportid; + count = mxt_max_msg_read_count(data, data->max_reportid); + tries = (data->max_reportid / count) + 1; /* Read messages until we force an invalid */ do { @@ -4112,6 +4145,10 @@ static int mxt_parse_device_properties(struct mxt_data *data) } device_property_read_u32(dev, "atmel,suspend-mode", &data->suspend_mode); + device_property_read_u32(dev, "atmel,mtu", &data->mtu); + if (data->mtu) + dev_dbg(dev, "mtu is set as %d\n", data->mtu); + return 0; } -- 2.17.1