From patchwork Thu Sep 17 05:22:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sultan Alsawaf X-Patchwork-Id: 254977 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01C2CC2BBD1 for ; Thu, 17 Sep 2020 05:23:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B14C320936 for ; Thu, 17 Sep 2020 05:23:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="csZiyQVY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726156AbgIQFXB (ORCPT ); Thu, 17 Sep 2020 01:23:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726126AbgIQFXB (ORCPT ); Thu, 17 Sep 2020 01:23:01 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C40D4C06174A; Wed, 16 Sep 2020 22:23:00 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id d9so491131pfd.3; Wed, 16 Sep 2020 22:23:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=S8OVV1mZhFttZGVKsQ9JByMqrc+T8zXoOJFR2iwsG4g=; b=csZiyQVYfnEgwlEZ9kS/DEtPg3WS1RIfpF0DBEL8GWXMXfDDMCPigCDbhzVUmQy9Z3 +Xyf+LdqcXV8ZYAPetqHUqpn4AIZB9eLImehPcHNvpWRa4jbnkBZESrua7BZ6DiO0HnR NwIUXrYkCNtyR0lf+t4pCmxMdY5piTPrlIavN9hbVxnf1YGtCqhVgUBRR5fIT9iqAg2Y Wv/9zGTUNHSTUhjGtnsGdRUFXd4nVwPZdEbqEvxkGlhU7UTV2S2uTafqBI3M698XVbJd cBfwkRqUQq2Bzzqc7DJtO0tdBoorLPLRuWKTH0JH4KuUfNc5hKqIBEn73Adr7yxKtCBe hqLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=S8OVV1mZhFttZGVKsQ9JByMqrc+T8zXoOJFR2iwsG4g=; b=gRXWwJkghLrglLr6Wrxar1CjbtBzJoMSotfyLgWc2fpP/MmAarldo+AQkMUHvfO/yM SWcw5zIw7zvvbaJrYbIARSq/Guh4TOJK1jkSlq1z+6hdisCwNnb8MTXPt5fxCkZGIc0N ERLyAR8H/Z8CsGhrCr2/Vnsg8spWMcKJV1Z/3xO3CcDlVwVFXy7xU/gwPRVb7VB7HccO f5fU8GTeTxCmwaSyTI0nb/4JPs+D4mX9QColFPyWPAnALyOIxFMwQkWiukdL9vHwuXok KyHjbqDfyQexhsNfm8rVdczWZFoWvdyksBOcSMpD8IqcprjoaqjYY69QJ0+H9zMdfc4B TV+A== X-Gm-Message-State: AOAM530VduBSYJ8ZMxLNttxPxwJriWES14G2IMjhNATPi1QcG0VUqVz5 uwDayBIFw/1I2602qCkUO1fTH17QFGEvyQ== X-Google-Smtp-Source: ABdhPJwRiiZC9FoLVuFtA6sythUYEh5Tocm0uFHYXvuvsa86NUhl1m2QIEbHD1xVx6udreMMVpi04Q== X-Received: by 2002:a62:52d3:0:b029:142:2501:3a00 with SMTP id g202-20020a6252d30000b029014225013a00mr9360192pfb.79.1600320180046; Wed, 16 Sep 2020 22:23:00 -0700 (PDT) Received: from sultan-book.localdomain ([104.200.129.212]) by smtp.gmail.com with ESMTPSA id i62sm18705619pfe.140.2020.09.16.22.22.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Sep 2020 22:22:59 -0700 (PDT) From: Sultan Alsawaf X-Google-Original-From: Sultan Alsawaf To: linux-i2c@vger.kernel.org Cc: jikos@kernel.org, aaron.ma@canonical.com, admin@kryma.net, andriy.shevchenko@linux.intel.com, benjamin.tissoires@redhat.com, hdegoede@redhat.com, hn.chen@weidahitech.com, jarkko.nikula@linux.intel.com, kai.heng.feng@canonical.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, mika.westerberg@linux.intel.com, vicamo.yang@canonical.com, wsa@kernel.org, Sultan Alsawaf Subject: [PATCH v2 1/4] i2c: designware: Fix transfer failures for invalid SMBus block reads Date: Wed, 16 Sep 2020 22:22:53 -0700 Message-Id: <20200917052256.5770-2-sultan@kerneltoast.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200917052256.5770-1-sultan@kerneltoast.com> References: <20200917052256.5770-1-sultan@kerneltoast.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org From: Sultan Alsawaf SMBus block reads can be broken because the read function will just skip over bytes it doesn't like until reaching a byte that conforms to the length restrictions for block reads. This is problematic when it isn't known if the incoming payload is indeed a conforming block read. According to the SMBus specification, block reads will only send the payload length in the first byte, so we can fix this by only considering the first byte in a sequence for block read length purposes. In addition, when the length byte is invalid, the original transfer length still needs to be adjusted to avoid a controller timeout. Fixes: c3ae106050b9 ("i2c: designware: Implement support for SMBus block read and write") Signed-off-by: Sultan Alsawaf --- drivers/i2c/busses/i2c-designware-master.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/i2c/busses/i2c-designware-master.c b/drivers/i2c/busses/i2c-designware-master.c index d6425ad6e6a3..d78f48ca4886 100644 --- a/drivers/i2c/busses/i2c-designware-master.c +++ b/drivers/i2c/busses/i2c-designware-master.c @@ -430,10 +430,12 @@ i2c_dw_read(struct dw_i2c_dev *dev) u32 flags = msgs[dev->msg_read_idx].flags; regmap_read(dev->map, DW_IC_DATA_CMD, &tmp); - /* Ensure length byte is a valid value */ - if (flags & I2C_M_RECV_LEN && - tmp <= I2C_SMBUS_BLOCK_MAX && tmp > 0) { - len = i2c_dw_recv_len(dev, tmp); + if (flags & I2C_M_RECV_LEN) { + /* Ensure length byte is a valid value */ + if (tmp <= I2C_SMBUS_BLOCK_MAX && tmp > 0) + len = i2c_dw_recv_len(dev, tmp); + else + len = i2c_dw_recv_len(dev, len); } *buf++ = tmp; dev->rx_outstanding--; From patchwork Thu Sep 17 05:22:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sultan Alsawaf X-Patchwork-Id: 254975 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA794C43461 for ; Thu, 17 Sep 2020 05:23:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A7CA0208E4 for ; Thu, 17 Sep 2020 05:23:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jfykg3GQ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725267AbgIQFXm (ORCPT ); Thu, 17 Sep 2020 01:23:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726169AbgIQFXD (ORCPT ); Thu, 17 Sep 2020 01:23:03 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19171C06174A; Wed, 16 Sep 2020 22:23:02 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id l71so680247pge.4; Wed, 16 Sep 2020 22:23:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=H9VSuNM4l/40vayIg5SAmxrV3Ky0F4a/ev4/YbXR38I=; b=jfykg3GQwbbnkRd4mHs2T0pRUdzz6Qg04xQDWonYE+62vUyPxk2CSLjNyOuwQCK0wQ kUaQ8/9tGinqIg9HVGsDRNH6jabj28nmPIwstqFlvS6NsEZ/loOvs/JlEfSnQrlt0TUv riOpGWHsnRPnO1Dht3mtFK6Ge7oJALV93RpiDKnage3a9TapArQxpoR9EkToOJwoZGmg kkEAiBqlnuCdonIX3vCrWzrli6uuZdrpxpmBW/pxRXWNcnMB6YpNk/E5lVt/7rO+TqRm uLSMc4LtD9P2wavTLkjV+i3S0Nd3eQnJDFKgmPEc0HHyoCP+C1lS8QSTHxhlgomcEzNj oXUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=H9VSuNM4l/40vayIg5SAmxrV3Ky0F4a/ev4/YbXR38I=; b=FXoiZMlyebz0KIAfDUWB3Qgcy/xKvQwMRYdSn+NEEuQUhPCJXr74M8XQsI3+9ElhpT NIhXECTpQuz+aJY/xRyPquq5/m5UJOEs1mGIEaJmGKdvwgjiGYCEKJIKoiqm8PAHRu7Q 4Z86Tcqu1Y49EI4jV9kz51XXlG4Z+ItSF84Feut9fPvMJ6sHLBBnCQyO5FvrKm62/YgT b6c6xsUGtVGN4vQjEc1zCmiikHMsYQwVJZ/lqAa5bj+IAhKg11yUlJGLoOMJwZ7QtY+U m2z5l54//8O0Z2q1sxIZMojCcB/+LZZL2YyYZJHv0kaLJYDNZOO2TCbTj9Z0WFzrCLn8 1Dgw== X-Gm-Message-State: AOAM5323ifwaT3cRS6jfMKodiDbWoCv2/roSNP+pe27a8cZ4Uh98fKJQ clVzq516dvPCnBlWYRtsIlmsvKO5dDxTVQ== X-Google-Smtp-Source: ABdhPJyQj2W93OVJdgG0z4BQfii7Jc+Bx08sqfPo7FS4qcFN/sVD4CD3GQSjhe2kx8kuKnZazAVFgg== X-Received: by 2002:a62:fb1a:0:b029:142:2501:39f9 with SMTP id x26-20020a62fb1a0000b0290142250139f9mr9134883pfm.72.1600320181402; Wed, 16 Sep 2020 22:23:01 -0700 (PDT) Received: from sultan-book.localdomain ([104.200.129.212]) by smtp.gmail.com with ESMTPSA id i62sm18705619pfe.140.2020.09.16.22.23.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Sep 2020 22:23:00 -0700 (PDT) From: Sultan Alsawaf X-Google-Original-From: Sultan Alsawaf To: linux-i2c@vger.kernel.org Cc: jikos@kernel.org, aaron.ma@canonical.com, admin@kryma.net, andriy.shevchenko@linux.intel.com, benjamin.tissoires@redhat.com, hdegoede@redhat.com, hn.chen@weidahitech.com, jarkko.nikula@linux.intel.com, kai.heng.feng@canonical.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, mika.westerberg@linux.intel.com, vicamo.yang@canonical.com, wsa@kernel.org, Sultan Alsawaf Subject: [PATCH v2 2/4] i2c: designware: Ensure tx_buf_len is nonzero for SMBus block reads Date: Wed, 16 Sep 2020 22:22:54 -0700 Message-Id: <20200917052256.5770-3-sultan@kerneltoast.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200917052256.5770-1-sultan@kerneltoast.com> References: <20200917052256.5770-1-sultan@kerneltoast.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org From: Sultan Alsawaf The point of adding a byte to len in i2c_dw_recv_len() is to make sure that tx_buf_len is nonzero, so that i2c_dw_xfer_msg() can let the i2c controller know that the i2c transaction can end. Otherwise, the i2c controller will think that the transaction can never end for block reads, which results in the stop-detection bit never being set and thus the transaction timing out. Adding a byte to len is not a reliable way to do this though; sometimes it lets tx_buf_len become zero, which results in the scenario described above. Therefore, just directly ensure tx_buf_len cannot be zero to fix the issue. Fixes: c3ae106050b9 ("i2c: designware: Implement support for SMBus block read and write") Signed-off-by: Sultan Alsawaf --- drivers/i2c/busses/i2c-designware-master.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/i2c/busses/i2c-designware-master.c b/drivers/i2c/busses/i2c-designware-master.c index d78f48ca4886..22f28516bca7 100644 --- a/drivers/i2c/busses/i2c-designware-master.c +++ b/drivers/i2c/busses/i2c-designware-master.c @@ -395,8 +395,9 @@ i2c_dw_recv_len(struct dw_i2c_dev *dev, u8 len) * Adjust the buffer length and mask the flag * after receiving the first byte. */ - len += (flags & I2C_CLIENT_PEC) ? 2 : 1; - dev->tx_buf_len = len - min_t(u8, len, dev->rx_outstanding); + if (flags & I2C_CLIENT_PEC) + len++; + dev->tx_buf_len = len - min_t(u8, len - 1, dev->rx_outstanding); msgs[dev->msg_read_idx].len = len; msgs[dev->msg_read_idx].flags &= ~I2C_M_RECV_LEN; From patchwork Thu Sep 17 05:22:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sultan Alsawaf X-Patchwork-Id: 293947 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7177C433E2 for ; Thu, 17 Sep 2020 05:23:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6D5E4208E4 for ; Thu, 17 Sep 2020 05:23:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="teYJl6x5" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726199AbgIQFXG (ORCPT ); Thu, 17 Sep 2020 01:23:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726180AbgIQFXE (ORCPT ); Thu, 17 Sep 2020 01:23:04 -0400 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5817FC061756; Wed, 16 Sep 2020 22:23:03 -0700 (PDT) Received: by mail-pf1-x444.google.com with SMTP id z19so475171pfn.8; Wed, 16 Sep 2020 22:23:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WDZd8tsRsFWv5ihYfraNwY2+UbRPuk2ibJ4Vl5wi/pY=; b=teYJl6x5RIDP4Alc+XCeMh7yCiyF2cARRceeq+i17641JSN7U0OU5n93u8IZZtVpJi V2T8CjpZl28JlqCE/md0a60Fc4F1MUqdbbOd8RyT9Fjb3iInQaQ+nP2PGD3DFov4eQgm szNGCpEZP9y2rz/kQYi4+zO8MtYmsa46IejNRPhHEonc5QafOS7lMBNXddTMN4XuG3TG DicSWOAGXyj26lPRmeeWAdwUK7ju/GZIBSVnl4pqIK2Uy/Q8mJnjo+EFWeMSCgEVVoVt M8O44yIcPN4YWe+Ag1eNNQOepmbttBhMPdFOm0SKY4syLzTEisqvHcHqWo+Komajk2cP dEog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=WDZd8tsRsFWv5ihYfraNwY2+UbRPuk2ibJ4Vl5wi/pY=; b=gCxElnuDQ2Ua8QBQQ0xMi7w+vBMFuvWAbdZN/0+oHLNyCzUwIWBiNnmOPuIp7IbbGg kI2aM3kU+5TVCAtQzC05Wi92e5fZ86nOLVxgGn7ckyaSn9+oQ7ePcd6mG/BrkzizfFdc kc9rbNEZLtY4X4FyJNRNKZGPQAD69jAmdBUSGIiwmSbesPGKu0wXSJFzyaf/KYkrwG1z pBPGdG5qd4dKGSZCBgOBxIuRmuqgAFFTFmHkWMeRfs/qJeQItWUkfizF05WiWooXUDt8 GR0AxXyipknN4tH1053bDFzlh7ge1fcegV7m8n6ZAyT+71yjEW8XFAMXfc39ozyOA5gw tFzw== X-Gm-Message-State: AOAM530fJiuwp/EZx9CrfweUPSUTSXq3pUSquXyW2D1gwWVa7wiZIXZp s4tw5TECTAI6N7gjVjvJ8v03tSwtxT6xsg== X-Google-Smtp-Source: ABdhPJy2yKyMB98G1d9fOVe9cyIU4MU6rtz+y+tWiJJCWV9cCeeHDV1+2ZamQ2v/ihfI1xB0w2VS9A== X-Received: by 2002:a63:516:: with SMTP id 22mr21793976pgf.316.1600320182593; Wed, 16 Sep 2020 22:23:02 -0700 (PDT) Received: from sultan-book.localdomain ([104.200.129.212]) by smtp.gmail.com with ESMTPSA id i62sm18705619pfe.140.2020.09.16.22.23.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Sep 2020 22:23:02 -0700 (PDT) From: Sultan Alsawaf X-Google-Original-From: Sultan Alsawaf To: linux-i2c@vger.kernel.org Cc: jikos@kernel.org, aaron.ma@canonical.com, admin@kryma.net, andriy.shevchenko@linux.intel.com, benjamin.tissoires@redhat.com, hdegoede@redhat.com, hn.chen@weidahitech.com, jarkko.nikula@linux.intel.com, kai.heng.feng@canonical.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, mika.westerberg@linux.intel.com, vicamo.yang@canonical.com, wsa@kernel.org, Sultan Alsawaf Subject: [PATCH v2 3/4] i2c: designware: Allow SMBus block reads up to 255 bytes in length Date: Wed, 16 Sep 2020 22:22:55 -0700 Message-Id: <20200917052256.5770-4-sultan@kerneltoast.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200917052256.5770-1-sultan@kerneltoast.com> References: <20200917052256.5770-1-sultan@kerneltoast.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org From: Sultan Alsawaf According to the SMBus 3.0 protocol specification, block transfer limits were increased from 32 bytes to 255 bytes. Remove the obsolete 32-byte limitation. Signed-off-by: Sultan Alsawaf --- drivers/i2c/busses/i2c-designware-master.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/i2c/busses/i2c-designware-master.c b/drivers/i2c/busses/i2c-designware-master.c index 22f28516bca7..5bd64bd17d94 100644 --- a/drivers/i2c/busses/i2c-designware-master.c +++ b/drivers/i2c/busses/i2c-designware-master.c @@ -433,7 +433,7 @@ i2c_dw_read(struct dw_i2c_dev *dev) regmap_read(dev->map, DW_IC_DATA_CMD, &tmp); if (flags & I2C_M_RECV_LEN) { /* Ensure length byte is a valid value */ - if (tmp <= I2C_SMBUS_BLOCK_MAX && tmp > 0) + if (tmp > 0) len = i2c_dw_recv_len(dev, tmp); else len = i2c_dw_recv_len(dev, len); From patchwork Thu Sep 17 05:22:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sultan Alsawaf X-Patchwork-Id: 254976 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98BE5C43461 for ; Thu, 17 Sep 2020 05:23:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 591A320936 for ; Thu, 17 Sep 2020 05:23:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DOZ3yizR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726191AbgIQFXG (ORCPT ); Thu, 17 Sep 2020 01:23:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726185AbgIQFXE (ORCPT ); Thu, 17 Sep 2020 01:23:04 -0400 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F895C06174A; Wed, 16 Sep 2020 22:23:04 -0700 (PDT) Received: by mail-pg1-x542.google.com with SMTP id l71so680294pge.4; Wed, 16 Sep 2020 22:23:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=m1D0o1H2R+Sauu9vaW7hv3HhUwelHNtIl/gUcqmOKIE=; b=DOZ3yizRJb0zGyGIthPbPkYOMjI9b5lcmvm/pATc/12B+ttb/W764so3l1jQN5DGrZ 1gyikWe/cuL52lEpzHerQ6maxn0TE2PO1y4AqwQD/3rydpYkMWzKEU0lApTIK40nCs1q r8fsBTXK9vtLLV/jv9QDO2g5juxwOb3QJcAgNnIL6HMnZHmxg4Snoz6BYhwcHRUAdoE0 xREILatfI0mUW0mZISLsHTmwHibgBaEbnlC21zUsReDh/jhQUSsTm6p5WPZGJrjDEOqG 9QaZ3GmGtBVRNUSBp0gbfxqkx5vQkwY2qS8FbG+lJDPPFXQIu4bxwSJimVusE05PSoJS nF1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=m1D0o1H2R+Sauu9vaW7hv3HhUwelHNtIl/gUcqmOKIE=; b=cTCLQx3vAIUSSfsgomWJqTG64MkuNqGwAdhaPHwZQTmLjy1p0o2DOOCy9ow3RLP4qN l/SBG7Xei5JJakewv77YH874ex4528OFgAITPVyKDpWus5e8Pk+T3UyiyoMZs5bIkjOk zGSfDFV0QSxtQJrPFE8eNgVuK6qID+LXgJ/8EFhdCofRFsAxytYJAJUcTNaNrd8st1zm WqDC5ql6BCRNqB7W7lyTx06TmS8zG+fz5vbb2L9pVMls8TBStQhrRusgp8F0iox2b1yA rQugE84dCGL6gVdyZj5Qskh+yxfo7/D64w8bjUSG8kcXxyFBG9PdzBIZaFpOQOxy3jkV wIyA== X-Gm-Message-State: AOAM532dTZWeVmseA8V8Et2yXmkaztbtvkQ1NvzHehvmLQxu4OIYXrM5 r6IuMs4eObTTVJLp54QgxsFRHTj3pw394w== X-Google-Smtp-Source: ABdhPJx+jY1AqwMdKGjuu5DjZw2rWg53c7FLzz03VBoukncRje4/3N6NDoIEHMaM8Fwcy9SjZLvP6g== X-Received: by 2002:a62:828b:0:b029:142:2501:35d6 with SMTP id w133-20020a62828b0000b0290142250135d6mr9861750pfd.54.1600320183895; Wed, 16 Sep 2020 22:23:03 -0700 (PDT) Received: from sultan-book.localdomain ([104.200.129.212]) by smtp.gmail.com with ESMTPSA id i62sm18705619pfe.140.2020.09.16.22.23.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Sep 2020 22:23:03 -0700 (PDT) From: Sultan Alsawaf X-Google-Original-From: Sultan Alsawaf To: linux-i2c@vger.kernel.org Cc: jikos@kernel.org, aaron.ma@canonical.com, admin@kryma.net, andriy.shevchenko@linux.intel.com, benjamin.tissoires@redhat.com, hdegoede@redhat.com, hn.chen@weidahitech.com, jarkko.nikula@linux.intel.com, kai.heng.feng@canonical.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, mika.westerberg@linux.intel.com, vicamo.yang@canonical.com, wsa@kernel.org, Sultan Alsawaf , Jiri Kosina Subject: [PATCH v2 4/4] HID: i2c-hid: Use block reads when possible to save power Date: Wed, 16 Sep 2020 22:22:56 -0700 Message-Id: <20200917052256.5770-5-sultan@kerneltoast.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200917052256.5770-1-sultan@kerneltoast.com> References: <20200917052256.5770-1-sultan@kerneltoast.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org From: Sultan Alsawaf We have no way of knowing how large an incoming payload is going to be, so the only strategy available up until now has been to always retrieve the maximum possible report length over i2c, which can be quite inefficient. For devices that send reports in block read format, the i2c controller driver can read the payload length on the fly and terminate the i2c transaction early, resulting in considerable power savings. On a Dell Precision 15 5540 with an i9-9880H, resting my finger on the touchpad causes psys power readings to go up by about 4W and hover there until I remove my finger. With this patch, my psys readings go from 4.7W down to 3.1W, yielding about 1.6W in savings. This is because my touchpad's max report length is 60 bytes, but all of the regular reports it sends for touch events are only 32 bytes, so the i2c transfer is roughly halved for the common case. Acked-by: Jiri Kosina Signed-off-by: Sultan Alsawaf --- drivers/hid/i2c-hid/i2c-hid-core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/hid/i2c-hid/i2c-hid-core.c b/drivers/hid/i2c-hid/i2c-hid-core.c index dbd04492825d..66950f472122 100644 --- a/drivers/hid/i2c-hid/i2c-hid-core.c +++ b/drivers/hid/i2c-hid/i2c-hid-core.c @@ -476,11 +476,14 @@ static void i2c_hid_get_input(struct i2c_hid *ihid) int ret; u32 ret_size; int size = le16_to_cpu(ihid->hdesc.wMaxInputLength); + u16 flags; if (size > ihid->bufsize) size = ihid->bufsize; - ret = i2c_master_recv(ihid->client, ihid->inbuf, size); + /* Try to do a block read if the size fits in one byte */ + flags = size > 255 ? I2C_M_RD : I2C_M_RD | I2C_M_RECV_LEN; + ret = i2c_transfer_buffer_flags(ihid->client, ihid->inbuf, size, flags); if (ret != size) { if (ret < 0) return;