From patchwork Wed Oct 25 03:55:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hamish Martin X-Patchwork-Id: 739450 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5E16DC25B71 for ; Wed, 25 Oct 2023 03:55:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232503AbjJYDzk (ORCPT ); Tue, 24 Oct 2023 23:55:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232386AbjJYDzh (ORCPT ); Tue, 24 Oct 2023 23:55:37 -0400 Received: from gate2.alliedtelesis.co.nz (gate2.alliedtelesis.co.nz [202.36.163.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2320DC for ; Tue, 24 Oct 2023 20:55:32 -0700 (PDT) Received: from svr-chch-seg1.atlnz.lc (mmarshal3.atlnz.lc [10.32.18.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by gate2.alliedtelesis.co.nz (Postfix) with ESMTPS id 7DB192C011D; Wed, 25 Oct 2023 16:55:31 +1300 (NZDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail181024; t=1698206131; bh=iVWFkB0EmAPELYtaE02YvjsdW301G7v+jsZLafFdS1s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZIKKhX/KDqz1enYskvI5VIOkCvZeOaAaBlA1bVq2FqHS6yCOGbMS3x2RAvjG8Leo4 i+CM6r0wDMag7jU8RiojUApSCe5JncH3J0W/cMK5pkKXNYMK/R/gNQE+7kEInQq1ty QK4rcJSbDUgNLRap/7VBJ1P4KJbjA/eLBBS6jyj91BQbOWFxX1Ib60gemlaMohy/Ys +MQ8bDVLS01QePg0l53kqcgjSq3orE19XzvcG5xxAyH8RBgPJ/t2LlkFmzra2zjC89 WUiJZHLRzNXW4vtKbc8Zqdfg47FKW7QIaIwf2/jAHSf2scgMgkjPLIy0aLWmgoFswb iWNlJD3yP9bEA== Received: from pat.atlnz.lc (Not Verified[10.32.16.33]) by svr-chch-seg1.atlnz.lc with Trustwave SEG (v8,2,6,11305) id ; Wed, 25 Oct 2023 16:55:31 +1300 Received: from hamishm-dl.ws.atlnz.lc (hamishm-dl.ws.atlnz.lc [10.33.24.11]) by pat.atlnz.lc (Postfix) with ESMTP id 034B013EDA9; Wed, 25 Oct 2023 16:55:31 +1300 (NZDT) Received: by hamishm-dl.ws.atlnz.lc (Postfix, from userid 1133) id 018C2242FEC; Wed, 25 Oct 2023 16:55:30 +1300 (NZDT) From: Hamish Martin To: gupt21@gmail.com, jikos@kernel.org, benjamin.tissoires@redhat.com, Enrik.Berkhan@inka.de, sven.zuehlsdorf@vigem.de Cc: linux-i2c@vger.kernel.org, linux-input@vger.kernel.org, Hamish Martin Subject: [PATCH 1/5] HID: mcp2221: Set driver data before I2C adapter add Date: Wed, 25 Oct 2023 16:55:10 +1300 Message-ID: <20231025035514.3450123-2-hamish.martin@alliedtelesis.co.nz> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231025035514.3450123-1-hamish.martin@alliedtelesis.co.nz> References: <20231025035514.3450123-1-hamish.martin@alliedtelesis.co.nz> MIME-Version: 1.0 X-SEG-SpamProfiler-Analysis: v=2.3 cv=L6ZjvNb8 c=1 sm=1 tr=0 a=KLBiSEs5mFS1a/PbTCJxuA==:117 a=bhdUkHdE2iEA:10 a=VwQbUJbxAAAA:8 a=wCWeksEBtQYof55t2KQA:9 a=AjGcO6oz07-iQ99wixmX:22 X-SEG-SpamProfiler-Score: 0 x-atlnz-ls: pat Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org The process of adding an I2C adapter can invoke I2C accesses on that new adapter (see i2c_detect()). Ensure we have set the adapter's driver data to avoid null pointer dereferences in the xfer functions during the adapter add. This has been noted in the past and the same fix proposed but not completed. See: https://lore.kernel.org/lkml/ef597e73-ed71-168e-52af-0d19b03734ac@vigem.de/ Signed-off-by: Hamish Martin --- drivers/hid/hid-mcp2221.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index 72883e0ce757..b95f31cf0fa2 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -1157,12 +1157,12 @@ static int mcp2221_probe(struct hid_device *hdev, snprintf(mcp->adapter.name, sizeof(mcp->adapter.name), "MCP2221 usb-i2c bridge"); + i2c_set_adapdata(&mcp->adapter, mcp); ret = devm_i2c_add_adapter(&hdev->dev, &mcp->adapter); if (ret) { hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret); return ret; } - i2c_set_adapdata(&mcp->adapter, mcp); #if IS_REACHABLE(CONFIG_GPIOLIB) /* Setup GPIO chip */ From patchwork Wed Oct 25 03:55:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hamish Martin X-Patchwork-Id: 738055 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE11EC25B6F for ; Wed, 25 Oct 2023 03:55:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232323AbjJYDzj (ORCPT ); Tue, 24 Oct 2023 23:55:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232465AbjJYDzh (ORCPT ); Tue, 24 Oct 2023 23:55:37 -0400 Received: from gate2.alliedtelesis.co.nz (gate2.alliedtelesis.co.nz [IPv6:2001:df5:b000:5::4]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0661F10E for ; Tue, 24 Oct 2023 20:55:34 -0700 (PDT) Received: from svr-chch-seg1.atlnz.lc (mmarshal3.atlnz.lc [10.32.18.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by gate2.alliedtelesis.co.nz (Postfix) with ESMTPS id AD13E2C062E; Wed, 25 Oct 2023 16:55:32 +1300 (NZDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail181024; t=1698206132; bh=qw4Yh2MrWZ7ldiOY9hquUs4lryJE4IdwX6/hlSqnHi8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TKCKpRy1CFI7iTNFeMV+xl0wkOAPe6L21fjeW92aRcGJtpw0m6ZQwoZKsJPpK8up+ B/bbf75OhB57dWavKf/kY5GSMeUpsWiMwEeOD6BXF/XeN4NkzgtpL3IhsjoWP3jJiQ 0MMVydvePEb2x/ggXmkEs+nQtGKRuAUJnsurWDa+Uc//MHlktJbcM+9woX9j23SwLM RgDTTzL53Pr4tL+7qnjVx0n8oanq3O4rLvLT+ESN5Dxl6FcpyIMHe0WnDUnPOHzJMj MIUEBUcdQHvVsy2nF4LpvuigECrxwiv2DZ79GiJQntOUiyfi3mVmO3GVbl/halLccF EktmHFW6u93yA== Received: from pat.atlnz.lc (Not Verified[10.32.16.33]) by svr-chch-seg1.atlnz.lc with Trustwave SEG (v8,2,6,11305) id ; Wed, 25 Oct 2023 16:55:32 +1300 Received: from hamishm-dl.ws.atlnz.lc (hamishm-dl.ws.atlnz.lc [10.33.24.11]) by pat.atlnz.lc (Postfix) with ESMTP id 5C72113EDA9; Wed, 25 Oct 2023 16:55:32 +1300 (NZDT) Received: by hamishm-dl.ws.atlnz.lc (Postfix, from userid 1133) id 5ACE9242FEC; Wed, 25 Oct 2023 16:55:32 +1300 (NZDT) From: Hamish Martin To: gupt21@gmail.com, jikos@kernel.org, benjamin.tissoires@redhat.com, Enrik.Berkhan@inka.de, sven.zuehlsdorf@vigem.de Cc: linux-i2c@vger.kernel.org, linux-input@vger.kernel.org, Hamish Martin Subject: [PATCH 2/5] HID: mcp2221: Allow IO to start during probe Date: Wed, 25 Oct 2023 16:55:11 +1300 Message-ID: <20231025035514.3450123-3-hamish.martin@alliedtelesis.co.nz> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231025035514.3450123-1-hamish.martin@alliedtelesis.co.nz> References: <20231025035514.3450123-1-hamish.martin@alliedtelesis.co.nz> MIME-Version: 1.0 X-SEG-SpamProfiler-Analysis: v=2.3 cv=L6ZjvNb8 c=1 sm=1 tr=0 a=KLBiSEs5mFS1a/PbTCJxuA==:117 a=bhdUkHdE2iEA:10 a=VwQbUJbxAAAA:8 a=hpi-KvaB3YVD6pHwyWgA:9 a=AjGcO6oz07-iQ99wixmX:22 X-SEG-SpamProfiler-Score: 0 x-atlnz-ls: pat Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org During the probe we add an I2C adapter and as soon as we add that adapter it may be used for a transfer (e.g via the code in i2cdetect()). Those transfers are not able to complete and time out. This is because the HID raw_event callback (mcp2221_raw_event) will not be invoked until the HID device's 'driver_input_lock' is marked up at the completion of the probe in hid_device_probe(). This starves the driver of the responses it is waiting for. In order to allow the I2C transfers to complete while we are still in the probe, start the IO once we have completed init of the HID device. This issue seems to have been seen before and a patch was submitted but it seems it was never accepted. See: https://lore.kernel.org/all/20221103222714.21566-3-Enrik.Berkhan@inka.de/ Signed-off-by: Hamish Martin --- drivers/hid/hid-mcp2221.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index b95f31cf0fa2..aef0785c91cc 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -1142,6 +1142,8 @@ static int mcp2221_probe(struct hid_device *hdev, if (ret) return ret; + hid_device_io_start(hdev); + /* Set I2C bus clock diviser */ if (i2c_clk_freq > 400) i2c_clk_freq = 400; From patchwork Wed Oct 25 03:55:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hamish Martin X-Patchwork-Id: 738054 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16DEDC27C46 for ; Wed, 25 Oct 2023 03:55:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232665AbjJYDzl (ORCPT ); Tue, 24 Oct 2023 23:55:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232535AbjJYDzh (ORCPT ); Tue, 24 Oct 2023 23:55:37 -0400 Received: from gate2.alliedtelesis.co.nz (gate2.alliedtelesis.co.nz [IPv6:2001:df5:b000:5::4]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B59CB0 for ; Tue, 24 Oct 2023 20:55:35 -0700 (PDT) Received: from svr-chch-seg1.atlnz.lc (mmarshal3.atlnz.lc [10.32.18.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by gate2.alliedtelesis.co.nz (Postfix) with ESMTPS id DD0B22C06A0; Wed, 25 Oct 2023 16:55:33 +1300 (NZDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail181024; t=1698206133; bh=k2Z4c3jfAi6Xiz4C0xNUsXl4Y0M6FQPwo3UXBWP78bQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AXctCMLLU/Ip2/xOQUzVlWfedwTryfIrkOfI/ajxOg96GiIDIrLwoy7dQIoAUSKhz OH7X1gh160v/tIRf1p4/2Axc25V2NVkwPByILzq8/QKTx8og3ql5Rte6qSrn06g+eS VXFfQJSRjXDConBv350rut20qT6jmFV1tsx+Ob+KpDoETNX6Yh4FXdVTRPnO2DEtVC LoVVr6ngm14KYIfPtNpLIUVlqjkP4GwfXFvQVYvZo23oCXJI6WUznoVmQwMduZ0crL 7I2ejJXS+ODfxFpk0F0sCKFQ/uISQEIpn3cpLMR88sJyyX2k2UdVWDr3h/5WkEfEZR d0zNeUaU/f9pw== Received: from pat.atlnz.lc (Not Verified[10.32.16.33]) by svr-chch-seg1.atlnz.lc with Trustwave SEG (v8,2,6,11305) id ; Wed, 25 Oct 2023 16:55:33 +1300 Received: from hamishm-dl.ws.atlnz.lc (hamishm-dl.ws.atlnz.lc [10.33.24.11]) by pat.atlnz.lc (Postfix) with ESMTP id 829A413EDA9; Wed, 25 Oct 2023 16:55:33 +1300 (NZDT) Received: by hamishm-dl.ws.atlnz.lc (Postfix, from userid 1133) id 80F2D242FEC; Wed, 25 Oct 2023 16:55:33 +1300 (NZDT) From: Hamish Martin To: gupt21@gmail.com, jikos@kernel.org, benjamin.tissoires@redhat.com, Enrik.Berkhan@inka.de, sven.zuehlsdorf@vigem.de Cc: linux-i2c@vger.kernel.org, linux-input@vger.kernel.org, Hamish Martin Subject: [PATCH 3/5] HID: mcp2221: Set ACPI companion Date: Wed, 25 Oct 2023 16:55:12 +1300 Message-ID: <20231025035514.3450123-4-hamish.martin@alliedtelesis.co.nz> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231025035514.3450123-1-hamish.martin@alliedtelesis.co.nz> References: <20231025035514.3450123-1-hamish.martin@alliedtelesis.co.nz> MIME-Version: 1.0 X-SEG-SpamProfiler-Analysis: v=2.3 cv=L6ZjvNb8 c=1 sm=1 tr=0 a=KLBiSEs5mFS1a/PbTCJxuA==:117 a=bhdUkHdE2iEA:10 a=BpYdKR3naCp1qFSn3UAA:9 X-SEG-SpamProfiler-Score: 0 x-atlnz-ls: pat Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org In scenarios where an I2C device tree is defined in ACPI and exists off the MCP2221 I2C bus, the devices could not be instantiated. Mark the USB port that the MCP2221 is connected to as its ACPI companion so that the USB device can be bound to the ACPI tree when enumerated. With this change the downstream I2C tree devices can be instantiated on ACPI systems. Signed-off-by: Hamish Martin --- drivers/hid/hid-mcp2221.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index aef0785c91cc..a219cd2e3309 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -1156,6 +1156,7 @@ static int mcp2221_probe(struct hid_device *hdev, mcp->adapter.algo = &mcp_i2c_algo; mcp->adapter.retries = 1; mcp->adapter.dev.parent = &hdev->dev; + ACPI_COMPANION_SET(&mcp->adapter.dev, ACPI_COMPANION(hdev->dev.parent)); snprintf(mcp->adapter.name, sizeof(mcp->adapter.name), "MCP2221 usb-i2c bridge"); From patchwork Wed Oct 25 03:55:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hamish Martin X-Patchwork-Id: 739449 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2623C27C47 for ; Wed, 25 Oct 2023 03:55:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232688AbjJYDzl (ORCPT ); Tue, 24 Oct 2023 23:55:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232667AbjJYDzi (ORCPT ); Tue, 24 Oct 2023 23:55:38 -0400 Received: from gate2.alliedtelesis.co.nz (gate2.alliedtelesis.co.nz [IPv6:2001:df5:b000:5::4]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C24812A for ; Tue, 24 Oct 2023 20:55:36 -0700 (PDT) Received: from svr-chch-seg1.atlnz.lc (mmarshal3.atlnz.lc [10.32.18.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by gate2.alliedtelesis.co.nz (Postfix) with ESMTPS id CBAA62C0880; Wed, 25 Oct 2023 16:55:34 +1300 (NZDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail181024; t=1698206134; bh=T1rEcP7WERAfCaboA9zydj0UHF5JwfRPsC5OV0NENQk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L+dZs5uthGiQaXqRn9TRixW+FoucJu9Dq6BJllzl2TG3mOb+SFC5NwTYeJUOmEa4l 6ivGAnjNyCUatebNvGioQBvlSKSSSWnbvQmklq/JPzi4gXnQA1lZIw1uizh9ivUE5s gfd8ZCxX7ZUyOtU7a/v2LLO47TZUQASS2W/U6F2xiaLTGYiUT4PBh7Y3YAWLgGSOa/ Uwxvd4KydwcsS58x7Kz6b83Jwfmv484Jjx+Fx+4uurOSlF2NfrEqHC79tm+bsaxEY3 /3d5JaCEd/pw55megoUNyrRx+4dOxMbAfLCrdF5bVfF0bbDqBzgfOXdXDfCp49z38v ze1cZIH4BY0ZQ== Received: from pat.atlnz.lc (Not Verified[10.32.16.33]) by svr-chch-seg1.atlnz.lc with Trustwave SEG (v8,2,6,11305) id ; Wed, 25 Oct 2023 16:55:34 +1300 Received: from hamishm-dl.ws.atlnz.lc (hamishm-dl.ws.atlnz.lc [10.33.24.11]) by pat.atlnz.lc (Postfix) with ESMTP id AB77013EDA9; Wed, 25 Oct 2023 16:55:34 +1300 (NZDT) Received: by hamishm-dl.ws.atlnz.lc (Postfix, from userid 1133) id A9BF1242FEC; Wed, 25 Oct 2023 16:55:34 +1300 (NZDT) From: Hamish Martin To: gupt21@gmail.com, jikos@kernel.org, benjamin.tissoires@redhat.com, Enrik.Berkhan@inka.de, sven.zuehlsdorf@vigem.de Cc: linux-i2c@vger.kernel.org, linux-input@vger.kernel.org, Hamish Martin Subject: [PATCH 4/5] HID: mcp2221: Don't set bus speed on every transfer Date: Wed, 25 Oct 2023 16:55:13 +1300 Message-ID: <20231025035514.3450123-5-hamish.martin@alliedtelesis.co.nz> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231025035514.3450123-1-hamish.martin@alliedtelesis.co.nz> References: <20231025035514.3450123-1-hamish.martin@alliedtelesis.co.nz> MIME-Version: 1.0 X-SEG-SpamProfiler-Analysis: v=2.3 cv=L6ZjvNb8 c=1 sm=1 tr=0 a=KLBiSEs5mFS1a/PbTCJxuA==:117 a=bhdUkHdE2iEA:10 a=tGeSMbNTF7bCNvFDaJ4A:9 X-SEG-SpamProfiler-Score: 0 x-atlnz-ls: pat Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Since the initial commit of this driver the I2C bus speed has been reconfigured for every single transfer. This is despite the fact that we never change the speed and it is never "lost" by the chip. Upon investigation we find that what was really happening was that the setting of the bus speed had the side effect of cancelling a previous failed command if there was one, thereby freeing the bus. This is the part that was actually required to keep the bus operational in the face of failed commands. Instead of always setting the speed, we now correctly cancel any failed commands as they are detected. This means we can just set the bus speed at probe time and remove the previous speed sets on each transfer. This has the effect of improving performance and reducing the number of commands required to complete transfers. Signed-off-by: Hamish Martin --- drivers/hid/hid-mcp2221.c | 41 ++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index a219cd2e3309..d0dd14cb4156 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -187,6 +187,25 @@ static int mcp_cancel_last_cmd(struct mcp2221 *mcp) return mcp_send_data_req_status(mcp, mcp->txbuf, 8); } +/* Check if the last command succeeded or failed and return the result. + * If the command did fail, cancel that command which will free the i2c bus. + */ +static int mcp_chk_last_cmd_status_free_bus(struct mcp2221 *mcp) +{ + int ret; + + ret = mcp_chk_last_cmd_status(mcp); + if (ret) { + /* The last command was a failure. + * Send a cancel which will also free the bus. + */ + usleep_range(980, 1000); + mcp_cancel_last_cmd(mcp); + } + + return ret; +} + static int mcp_set_i2c_speed(struct mcp2221 *mcp) { int ret; @@ -241,7 +260,7 @@ static int mcp_i2c_write(struct mcp2221 *mcp, usleep_range(980, 1000); if (last_status) { - ret = mcp_chk_last_cmd_status(mcp); + ret = mcp_chk_last_cmd_status_free_bus(mcp); if (ret) return ret; } @@ -308,7 +327,7 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp, if (ret) return ret; - ret = mcp_chk_last_cmd_status(mcp); + ret = mcp_chk_last_cmd_status_free_bus(mcp); if (ret) return ret; @@ -328,11 +347,6 @@ static int mcp_i2c_xfer(struct i2c_adapter *adapter, mutex_lock(&mcp->lock); - /* Setting speed before every transaction is required for mcp2221 */ - ret = mcp_set_i2c_speed(mcp); - if (ret) - goto exit; - if (num == 1) { if (msgs->flags & I2C_M_RD) { ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA, @@ -417,9 +431,7 @@ static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr, if (last_status) { usleep_range(980, 1000); - ret = mcp_chk_last_cmd_status(mcp); - if (ret) - return ret; + ret = mcp_chk_last_cmd_status_free_bus(mcp); } return ret; @@ -437,10 +449,6 @@ static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, mutex_lock(&mcp->lock); - ret = mcp_set_i2c_speed(mcp); - if (ret) - goto exit; - switch (size) { case I2C_SMBUS_QUICK: @@ -1150,6 +1158,11 @@ static int mcp2221_probe(struct hid_device *hdev, if (i2c_clk_freq < 50) i2c_clk_freq = 50; mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3; + ret = mcp_set_i2c_speed(mcp); + if (ret) { + hid_err(hdev, "can't set i2c speed: %d\n", ret); + return ret; + } mcp->adapter.owner = THIS_MODULE; mcp->adapter.class = I2C_CLASS_HWMON; From patchwork Wed Oct 25 03:55:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hamish Martin X-Patchwork-Id: 738053 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB330C25B72 for ; Wed, 25 Oct 2023 03:55:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232667AbjJYDzm (ORCPT ); Tue, 24 Oct 2023 23:55:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232706AbjJYDzk (ORCPT ); Tue, 24 Oct 2023 23:55:40 -0400 Received: from gate2.alliedtelesis.co.nz (gate2.alliedtelesis.co.nz [202.36.163.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B123C0 for ; Tue, 24 Oct 2023 20:55:38 -0700 (PDT) Received: from svr-chch-seg1.atlnz.lc (mmarshal3.atlnz.lc [10.32.18.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by gate2.alliedtelesis.co.nz (Postfix) with ESMTPS id CDC1D2C08C9; Wed, 25 Oct 2023 16:55:36 +1300 (NZDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail181024; t=1698206136; bh=w6roZGHMb0LOJxdVMG+K5YrKTIEQmrJ1Sow/7D1pHGI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EU4IE1Sst4kQR7sBGOifbRQZsc59YAPS/8kIVqTiOqUC2LgIG7Ur3P3kTxhmBst/a n3MvSHpYlPimpoEqdVL5pjwwFAuaqOJU2Dn9WMXYHz4Zfh6lzaGwPFs9BB5WxWlSLR mthwJtBA1ZxKFeZfVMQQ3NcSmCVcpgg/t0hggthKVNxVFzMETt+g+Cs5eotbEc2i+0 +hb6Qj4NdI3H2Nuf7shItTIqquFsbAj0RvxW+f6uXsB4an73KRJ89NMBALlPj+h5Zc +A05B9PFtYtL59Z/jnTrywDC5+Z3H85Bul3gUBLZT26J8Ax2HZkrjL+HYgeQ1BckUB 598xhOMVg3Zxw== Received: from pat.atlnz.lc (Not Verified[10.32.16.33]) by svr-chch-seg1.atlnz.lc with Trustwave SEG (v8,2,6,11305) id ; Wed, 25 Oct 2023 16:55:36 +1300 Received: from hamishm-dl.ws.atlnz.lc (hamishm-dl.ws.atlnz.lc [10.33.24.11]) by pat.atlnz.lc (Postfix) with ESMTP id 9532F13EDA9; Wed, 25 Oct 2023 16:55:36 +1300 (NZDT) Received: by hamishm-dl.ws.atlnz.lc (Postfix, from userid 1133) id 93835242FEC; Wed, 25 Oct 2023 16:55:36 +1300 (NZDT) From: Hamish Martin To: gupt21@gmail.com, jikos@kernel.org, benjamin.tissoires@redhat.com, Enrik.Berkhan@inka.de, sven.zuehlsdorf@vigem.de Cc: linux-i2c@vger.kernel.org, linux-input@vger.kernel.org, Hamish Martin Subject: [PATCH 5/5] HID: mcp2221: Handle reads greater than 60 bytes Date: Wed, 25 Oct 2023 16:55:14 +1300 Message-ID: <20231025035514.3450123-6-hamish.martin@alliedtelesis.co.nz> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231025035514.3450123-1-hamish.martin@alliedtelesis.co.nz> References: <20231025035514.3450123-1-hamish.martin@alliedtelesis.co.nz> MIME-Version: 1.0 X-SEG-SpamProfiler-Analysis: v=2.3 cv=L6ZjvNb8 c=1 sm=1 tr=0 a=KLBiSEs5mFS1a/PbTCJxuA==:117 a=bhdUkHdE2iEA:10 a=9W1-fauBzXCwDuBwIPUA:9 X-SEG-SpamProfiler-Score: 0 x-atlnz-ls: pat Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org When a user requests more than 60 bytes of data the MCP2221 must chunk the data in chunks up to 60 bytes long (see command/response code 0x40 in the datasheet). In order to signal that the device has more data the (undocumented) byte at byte index 2 of the Get I2C Data response uses the value 0x54. This contrasts with the case for the final data chunk where the value returned is 0x55 (MCP2221_I2C_READ_COMPL). The fact that 0x55 was not returned in the response was interpreted by the driver as a failure meaning that all reads of more than 60 bytes would fail. Add support for reads that are split over multiple chunks by looking for the response code indicating that more data is expected and continuing the read as the code intended. Some timing delays are required to ensure the chip has time to refill its FIFO as data is read in from the I2C bus. This timing has been tested in my system when configured for bus speeds of 50KHz, 100KHz, and 400KHz and operates well. Signed-off-by: Hamish Martin --- drivers/hid/hid-mcp2221.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index d0dd14cb4156..f9cceaeffd08 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -49,6 +49,7 @@ enum { MCP2221_I2C_MASK_ADDR_NACK = 0x40, MCP2221_I2C_WRADDRL_SEND = 0x21, MCP2221_I2C_ADDR_NACK = 0x25, + MCP2221_I2C_READ_PARTIAL = 0x54, MCP2221_I2C_READ_COMPL = 0x55, MCP2221_ALT_F_NOT_GPIOV = 0xEE, MCP2221_ALT_F_NOT_GPIOD = 0xEF, @@ -297,6 +298,7 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp, { int ret; u16 total_len; + int retries = 0; mcp->txbuf[0] = type; if (msg) { @@ -320,20 +322,31 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp, mcp->rxbuf_idx = 0; do { + /* Wait for the data to be read by the device */ + usleep_range(980, 1000); + memset(mcp->txbuf, 0, 4); mcp->txbuf[0] = MCP2221_I2C_GET_DATA; ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); - if (ret) - return ret; - - ret = mcp_chk_last_cmd_status_free_bus(mcp); - if (ret) - return ret; - - usleep_range(980, 1000); + if (ret) { + if (retries < 5) { + /* The data wasn't ready to read. + * Wait a bit longer and try again. + */ + usleep_range(90, 100); + retries++; + } else { + return ret; + } + } else { + retries = 0; + } } while (mcp->rxbuf_idx < total_len); + usleep_range(980, 1000); + ret = mcp_chk_last_cmd_status_free_bus(mcp); + return ret; } @@ -799,7 +812,8 @@ static int mcp2221_raw_event(struct hid_device *hdev, mcp->status = -EIO; break; } - if (data[2] == MCP2221_I2C_READ_COMPL) { + if (data[2] == MCP2221_I2C_READ_COMPL || + data[2] == MCP2221_I2C_READ_PARTIAL) { buf = mcp->rxbuf; memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]); mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];