Received: by 2002:a25:d783:0:0:0:0:0 with SMTP id o125csp750568ybg; Thu, 19 Mar 2020 08:07:47 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuaad94UVS0463G6U5uinOVAZtlJmfZevviEtwXjjgak5vosiSeGNxxUtHSax14nryDC0IH X-Received: by 2002:a9d:7696:: with SMTP id j22mr2855944otl.188.1584630466984; Thu, 19 Mar 2020 08:07:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584630466; cv=none; d=google.com; s=arc-20160816; b=ZP+QEcY/Ls8F9I4Fr7UJvKpqjm/zjKbsYwd/erL+9VH0Kb9hvROSpBj7VQZoNTNJ53 7gVfm305JuWVL72Pk+K7ohYL+t02/FzVhcR58iBPtJeXKy5boMQc5vgjA/Di6fg2zG8C XnRh0pupDyJaB8FUUZM2AlbNYfU8wIaWVakGV3+418utja/f867q/Wb74boCdlJREjP4 ZYE13s4HK/zSVXYOtyTpnvDQc5TIocPc5aiZGq410Eyy9a68gc3PgSPBD6ouNLpJMnbA jlvoCYUMYN8lpVkzhUhngBbfvLqaw2sVOVXsqKiu61W8AokSOtB+3xX5yoETKc/J5cNm dDRA== 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=YN+LUskqttSjFc3j1k5NW3l2eKa3punPq77cF84TDF0=; b=u8w8/Kvh+3aGsoGRTSvebv503mkFmUG6vT+xFqzXuZme/5shrU+j/39Bmk5vQ6b2uf DQU3qTGHGLAo1UFfpIXilWV4exAlN7oY8tmeBBeuwonm8MmJymgzeQh/9Yyx5yFpbt3d WnQiZ6hGr1SZ2jytONW8+QKIP72X1XhhBPNpKj1qP7igtkBpQZ6oNDEMATSGDF043WLb tyTMv6bgkQV8Nc2iUVnVxb/X6i8PD/lMtu1+nTjUCr5zjZJD8ixH98IIYBuS8z2f+Q2M 5poKG4YymkzY8NK/rZPI0JwXBUdEcaoO2PwFxeOVREDdbYFqqWZBzmXhVcUtoHFm9/4/ hKaQ== 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 s199si1162395oih.169.2020.03.19.08.07.02; Thu, 19 Mar 2020 08:07:46 -0700 (PDT) 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 S1727724AbgCSPD3 (ORCPT + 99 others); Thu, 19 Mar 2020 11:03:29 -0400 Received: from esa3.mentor.iphmx.com ([68.232.137.180]:32379 "EHLO esa3.mentor.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728317AbgCSPC4 (ORCPT ); Thu, 19 Mar 2020 11:02:56 -0400 IronPort-SDR: jF1Ppi4WRtyGgQrY9zFZyrc3eF9GValARb77kH460p3ISRNWrHQvJaf9ceI6iqWc4yD4/JYYnm nJc26cO6pXjaT76mfucmrV2OfsVLfpLYllbuSbRGgiivV/mFArDH1BNW62aZtXMAxkmQRJQYLT W6KGPftuh55kyyC22A57zjcJYfoQIgHaK/n3y7kYNwe8PaKQ6tCh/lZHxWdyYDwu1enJ+4Pp/2 cGpTTF6S7qiUa0epSdO7WQ9a0uSgBMsOPeYHtAr6gdyeLsIGhZhvt1ZBrp8yDcv6XYp8hHu5hG 0Dc= X-IronPort-AV: E=Sophos;i="5.70,572,1574150400"; d="scan'208";a="46891019" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa3.mentor.iphmx.com with ESMTP; 19 Mar 2020 07:02:55 -0800 IronPort-SDR: exR7hdURaWid+agzLLL3WwkP2pkKXpn8VpscSoPFDYktUs3ulb7pb3Ne7N5aDGUNNSYJ1PCKo9 S3RlyXE5xHS68+R77OKjwt3+AcTyKlqAGDXVDOXWTXIiDSoQbWfIbPO6OQ1V/hWvmFB7UZ8Lp7 9EhQAvPtW+hoRtQlQolX1bzpePMSCuAFEEV7qIi35MxuCxv5XGL1ExChWUmengSbeZHTFv6WGv xpoOJsqwOxe7ACdGO+AP/EsazzYxARTrYV7Jecf28n4JDNRGEwBPzNd78a/eTCH5MZpMGbVewA 1YA= From: Jiada Wang To: , , , , , CC: , , , , Subject: [PATCH v8 39/52] Input: atmel_mxt_ts: Limit the max bytes transferred in an i2c transaction Date: Thu, 19 Mar 2020 08:00:03 -0700 Message-ID: <20200319150016.61398-40-jiada_wang@mentor.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200319150016.61398-1-jiada_wang@mentor.com> References: <20200319150016.61398-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 --- drivers/input/touchscreen/atmel_mxt_ts.c | 65 +++++++++++++++++++----- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c b/drivers/input/touchscreen/atmel_mxt_ts.c index fe646d7edccf..13258da70305 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; }; @@ -1462,11 +1463,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, @@ -1476,18 +1498,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 */ @@ -1497,16 +1520,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) { @@ -1521,9 +1553,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