From patchwork Thu Nov 3 22:27:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Enrik Berkhan X-Patchwork-Id: 15229 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp30315wru; Thu, 3 Nov 2022 15:32:17 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Q3qS28ePKEB05sTskcPmmFGXeHD/49nFSG7aUSk/rHvFgwZiEjrjNJXzOO1NBPZJexfRo X-Received: by 2002:a17:90b:d88:b0:211:4d8:1c93 with SMTP id bg8-20020a17090b0d8800b0021104d81c93mr50392737pjb.41.1667514737297; Thu, 03 Nov 2022 15:32:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667514737; cv=none; d=google.com; s=arc-20160816; b=gdCsl2hBKRgVyXieuslHpQU7vMyHXYcCyb0AjtCD4Iwho3rCHW+aaCaLJK0FljJx7P Yiz5O+SPRQV/WRZlklE3mibCdzJrEADMGL8ix4qIdGQ4eKVr13tAkZlUgDwn9JZo6Cij WXVKgUBS24rF9Hp0jZmVuzDY9XxoB+V6TDxIPUp3I/0nYEmNj0PX8PMZRTXuy/2r+bOk dAw05m8QoaftsKmp93yUE5I19wpAfqCh5EyuWJ+q/Jdy6xL2O0NlQMvqIL5pMg1KKzyx zQmUY+hJ84E2sVHp8l5FhyYGHDtP+156YhtGfd75sEtpZHkQbC8gk8+JMmEJ2vlkQJ6l 478Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=2V9i7oQTNHUJPEiwVERmKoGE2858xzC1NoyImBvZTpE=; b=JrzQvorOkhYaNFjXSgBsgurl3nK2303gYqVMbNFEbTAZwcI95xFpziVlfYmzey6tyJ Ccpja1SCv1fQQZXuOPMUFqMdPQoykqJUlcL0tsV21TnkM7WEu1c/2KmyqhDPRQz0moYo EOBFUVIeDewCe4bbRQkYuxfwWxg8a9IvIah2Yy7XNT+xRZ2Al++gyFoiyRyabOFVZSUJ OLelA2hAwlb6dlJfbViAPh06FGWh1PA38Xh3aQ/5vTc7piHJzqqtGVkkQGcg2S5AAjzr 4mf6CROmHTHr+OYp1cPeR33QKIFHTqGjKWVASDEAlgBOxbyBq7v49yTHsO8vMvve6fTb vDZA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n5-20020a63f805000000b0043946964302si2277265pgh.173.2022.11.03.15.31.57; Thu, 03 Nov 2022 15:32:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231725AbiKCW1e (ORCPT + 99 others); Thu, 3 Nov 2022 18:27:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231349AbiKCW13 (ORCPT ); Thu, 3 Nov 2022 18:27:29 -0400 Received: from mail.inka.de (mail.inka.de [IPv6:2a04:c9c7:0:1073:217:a4ff:fe3b:e77c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BEFC21274; Thu, 3 Nov 2022 15:27:27 -0700 (PDT) Received: from mail3.berkhan-weisser.de ([2a03:4000:54:b9a::4]) by mail.inka.de with esmtpsa id 1oqigH-005aIe-ST; Thu, 03 Nov 2022 23:27:25 +0100 Received: from 127.0.0.1 (helo=localhost.localdomain) by mail3.berkhan-weisser.de with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.94.2) (envelope-from ) id 1oqigH-005Ggn-FY; Thu, 03 Nov 2022 23:27:25 +0100 From: Enrik Berkhan To: linux-input@vger.kernel.org Cc: linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, Rishi Gupta , Enrik Berkhan Subject: [PATCH v2 1/3] HID: mcp2221: don't connect hidraw Date: Thu, 3 Nov 2022 23:27:12 +0100 Message-Id: <20221103222714.21566-2-Enrik.Berkhan@inka.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221103222714.21566-1-Enrik.Berkhan@inka.de> References: <20220926202239.16379-1-Enrik.Berkhan@inka.de> <20221103222714.21566-1-Enrik.Berkhan@inka.de> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748515933482452555?= X-GMAIL-MSGID: =?utf-8?q?1748515933482452555?= The MCP2221 driver should not connect to the hidraw userspace interface, as it needs exclusive access to the chip. If you want to use /dev/hidrawX with the MCP2221, you need to avoid binding this driver to the device and use the hid generic driver instead (e.g. using udev rules). Signed-off-by: Enrik Berkhan Reported-by: Sven Zühlsdorf --- drivers/hid/hid-mcp2221.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index 5886543b17f3..e61dd039354b 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -1110,12 +1110,19 @@ static int mcp2221_probe(struct hid_device *hdev, return ret; } - ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); + /* + * This driver uses the .raw_event callback and therefore does not need any + * HID_CONNECT_xxx flags. + */ + ret = hid_hw_start(hdev, 0); if (ret) { hid_err(hdev, "can't start hardware\n"); return ret; } + hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8, + hdev->version & 0xff, hdev->name, hdev->phys); + ret = hid_hw_open(hdev); if (ret) { hid_err(hdev, "can't open device\n"); @@ -1145,8 +1152,7 @@ static int mcp2221_probe(struct hid_device *hdev, mcp->adapter.retries = 1; mcp->adapter.dev.parent = &hdev->dev; snprintf(mcp->adapter.name, sizeof(mcp->adapter.name), - "MCP2221 usb-i2c bridge on hidraw%d", - ((struct hidraw *)hdev->hidraw)->minor); + "MCP2221 usb-i2c bridge"); ret = devm_i2c_add_adapter(&hdev->dev, &mcp->adapter); if (ret) { From patchwork Thu Nov 3 22:27:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Enrik Berkhan X-Patchwork-Id: 15230 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp30366wru; Thu, 3 Nov 2022 15:32:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4wMAaRyMHIz+iZL3YP8ByS81lkpQ7+uFXfRAn3oHs3CFWDV66VxM618BeOlVObXTVAaLlB X-Received: by 2002:a17:902:f54c:b0:187:1974:2298 with SMTP id h12-20020a170902f54c00b0018719742298mr25191936plf.50.1667514748311; Thu, 03 Nov 2022 15:32:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667514748; cv=none; d=google.com; s=arc-20160816; b=00B2YNz7mCRYdiMP3k197o7qUgHoVemhlcFVLC/QhbaIQ3WmiwGs5WTBiAhfzwahNP 99J9SsXQVxmCAXm9c4+uwyxhiVj+KPIJYnlkd19jeFCU6dojMHVi75LrCH29f8D1LZyU d8TzYXZH35aGA7wh2JkH7kguLcA1OJX060Z/1Gf/L0v+OKzvvHAwSNTKkZjMg04F2SJB O7hESRkMqfj0te3eVgDo8E293YCoah8qeVV0fh2Ma02IVvrHIcBLqgxPvbXSDcWBoCw7 u/KOE5g351FNOWTm7B+OeAubFhAltGY1qMDwVL9xncIjPN7TSTCA8YjsfBP05piPaxsK fjNQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=59FBwAeroTKaIV20DkKsMcitTiSHpuFKo/q1qhLyBe8=; b=AIwNLQsx4T4YyZ/4NQIcSI2E7OByk8M3UzyaCNLrF5obVWW98yESc3NiMQZGxLsT07 zFs9vVR2pa/YJ+Uc7p/dRIZ8bmNK0/I9XuhZxsDhB1FygbjxrRsfmCr6AsVh8l4vgdfs 3i24Xy5UdAlqaT1ck60sBa1pyW0ngqdOqa/4xTnZJ2OJaLUv2Jqf7RTaofj9in7DfBcE Sh5Z0hgkh/xsZqJUQgzTS6IFKqNXk9T0YIEn1J4MGRqjn/AmYqvLU4t1YBNpLqvpaKUa yYEfoa8yO1mxfQGqrAQ4b+fBfC7OsK1at/+0wFEqLnHAUC82/eIukodV8yPnpiRmaWtT 17dg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 14-20020a63060e000000b00438854c876esi2371212pgg.419.2022.11.03.15.32.09; Thu, 03 Nov 2022 15:32:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229567AbiKCW1j (ORCPT + 99 others); Thu, 3 Nov 2022 18:27:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231708AbiKCW13 (ORCPT ); Thu, 3 Nov 2022 18:27:29 -0400 Received: from mail.inka.de (mail.inka.de [IPv6:2a04:c9c7:0:1073:217:a4ff:fe3b:e77c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7453120358; Thu, 3 Nov 2022 15:27:28 -0700 (PDT) Received: from mail3.berkhan-weisser.de ([2a03:4000:54:b9a::4]) by mail.inka.de with esmtpsa id 1oqigI-005aIq-38; Thu, 03 Nov 2022 23:27:26 +0100 Received: from 127.0.0.1 (helo=localhost.localdomain) by mail3.berkhan-weisser.de with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.94.2) (envelope-from ) id 1oqigH-005Ggn-MS; Thu, 03 Nov 2022 23:27:25 +0100 From: Enrik Berkhan To: linux-input@vger.kernel.org Cc: linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, Rishi Gupta , Enrik Berkhan , Tobias Junghans Subject: [PATCH v2 2/3] HID: mcp2221: enable HID I/O during GPIO probe Date: Thu, 3 Nov 2022 23:27:13 +0100 Message-Id: <20221103222714.21566-3-Enrik.Berkhan@inka.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221103222714.21566-1-Enrik.Berkhan@inka.de> References: <20220926202239.16379-1-Enrik.Berkhan@inka.de> <20221103222714.21566-1-Enrik.Berkhan@inka.de> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748515944486099271?= X-GMAIL-MSGID: =?utf-8?q?1748515944486099271?= As soon as the GPIO driver part will be enabled in mcp2221_probe(), the first HID reports will be exchanged with the chip because the GPIO driver immediately calls mcp_gpio_get_direction(). HID I/O has to be enabled explicitly during mcp2221_probe() to receive response reports. Otherwise, all four mcp_gpio_get_direction() calls will run into the four second timeout of mcp_send_report(), which will block the driver for about 16s during startup. A very similar patch appeared some time ago in https://lore.kernel.org/r/20210818152743.163929-1-tobias.junghans@inhub.de which obviously got lost somehow. CC: Tobias Junghans Signed-off-by: Enrik Berkhan --- drivers/hid/hid-mcp2221.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index e61dd039354b..0705526231ec 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -1178,6 +1178,9 @@ static int mcp2221_probe(struct hid_device *hdev, mcp->gc->can_sleep = 1; mcp->gc->parent = &hdev->dev; + /* Enable reception of HID reports during GPIO initialization */ + hid_device_io_start(hdev); + ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp); if (ret) return ret; From patchwork Thu Nov 3 22:27:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Enrik Berkhan X-Patchwork-Id: 15231 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp30499wru; Thu, 3 Nov 2022 15:32:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5pWS8fLDIOHCCzuErfpZSgxCsXmijDxeDALLbmoDP0oCXg+qQTTqF33VeLWcKEvClZMSNA X-Received: by 2002:a17:902:d512:b0:181:f1f4:fcb4 with SMTP id b18-20020a170902d51200b00181f1f4fcb4mr32681116plg.102.1667514769224; Thu, 03 Nov 2022 15:32:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667514769; cv=none; d=google.com; s=arc-20160816; b=oJWe57u8g//tMmKPxwesdBCQeQD2PcjUhGpewvqRHe+iR0NIe7ot2IfFCrN1WxqKUm C4ZQ7RRtzJlM8CPzfm0CNnJz+4C98F7V+lbHcDg7lsqFfxxmsMNCTEWiyW2AvRsVpdBW +IstYvMc203NiGOH2B/7FTXz+vYtomI+ObFpI45I8GlHwTlB2I2TUsvpAszzIbi8YJ83 C9FTPO1nyARYo61BrnQoV5y2miAL+Ypz2L88ILlpa3YY+dogWcJJv7hFavzgFC01bjvt i6gPM9+sbNOY2LTYA50M0cEJCvA7+BHUBdXRUxupkZusc1Dldq+LfK0KKG+V92/qBafX vkhA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=MOW3UqpnjbZ8eKitUtFAjk6RDpZrpVOdyCW90p9jrZk=; b=MpZW1C+C1r1ytHQ+AFD/xVXFarXERLFe058i6ohPKYeYZS6IdP1WTlnjCU3NlN/k29 kD8TPh7cve4OypBQuT6INi9xagaK/OxfsrTkplw4KbncnXE3J15alvzegnhZ13qiA9wB g1qfehSYVJ7k6pVdAFTWfuTNAZkuUxCnvfHvk3JOQw6S8ABlQLj6OdyM4Onv+6tphlEl rDzLzR3+sbbMejTx2OvNkhGnc0Jmj2Lt12dVO/Lszn+KICp5poXUAFXq/z2Wsui/xhHc t20KMHQJ7lydHdhlSTUcYJzW1kyJ8s+qOiiy49K/9OIAGc6G/+zP6tu/QRoY/xr5CBhZ XXkw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k128-20020a633d86000000b0044d72a10ab0si2350626pga.342.2022.11.03.15.32.32; Thu, 03 Nov 2022 15:32:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231728AbiKCW1n (ORCPT + 99 others); Thu, 3 Nov 2022 18:27:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231707AbiKCW13 (ORCPT ); Thu, 3 Nov 2022 18:27:29 -0400 Received: from mail.inka.de (mail.inka.de [IPv6:2a04:c9c7:0:1073:217:a4ff:fe3b:e77c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C21F2127D; Thu, 3 Nov 2022 15:27:27 -0700 (PDT) Received: from mail3.berkhan-weisser.de ([2a03:4000:54:b9a::4]) by mail.inka.de with esmtpsa id 1oqigI-005aIv-8o; Thu, 03 Nov 2022 23:27:26 +0100 Received: from 127.0.0.1 (helo=localhost.localdomain) by mail3.berkhan-weisser.de with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.94.2) (envelope-from ) id 1oqigH-005Ggn-T5; Thu, 03 Nov 2022 23:27:25 +0100 From: Enrik Berkhan To: linux-input@vger.kernel.org Cc: linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, Rishi Gupta , Enrik Berkhan Subject: [PATCH v2 3/3] HID: mcp2221: avoid stale rxbuf pointer Date: Thu, 3 Nov 2022 23:27:14 +0100 Message-Id: <20221103222714.21566-4-Enrik.Berkhan@inka.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221103222714.21566-1-Enrik.Berkhan@inka.de> References: <20220926202239.16379-1-Enrik.Berkhan@inka.de> <20221103222714.21566-1-Enrik.Berkhan@inka.de> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748515966732486539?= X-GMAIL-MSGID: =?utf-8?q?1748515966732486539?= In case the MCP2221 driver receives an unexpected read complete report from the device, the data should not be copied to mcp->rxbuf. The pointer might be NULL or even stale, having been set during an earlier transaction. Further, some bounds checking has been added. Signed-off-by: Enrik Berkhan --- drivers/hid/hid-mcp2221.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c index 0705526231ec..471b29c3c501 100644 --- a/drivers/hid/hid-mcp2221.c +++ b/drivers/hid/hid-mcp2221.c @@ -98,6 +98,7 @@ struct mcp2221 { u8 *rxbuf; u8 txbuf[64]; int rxbuf_idx; + int rxbuf_len; int status; u8 cur_i2c_clk_div; struct gpio_chip *gc; @@ -294,11 +295,12 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp, mcp->rxbuf = smbus_buf; } + mcp->rxbuf_len = total_len; + mcp->rxbuf_idx = 0; + ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4); if (ret) - return ret; - - mcp->rxbuf_idx = 0; + goto out_invalidate_rxbuf; do { memset(mcp->txbuf, 0, 4); @@ -306,15 +308,20 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp, ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); if (ret) - return ret; + goto out_invalidate_rxbuf; ret = mcp_chk_last_cmd_status(mcp); if (ret) - return ret; + goto out_invalidate_rxbuf; usleep_range(980, 1000); } while (mcp->rxbuf_idx < total_len); +out_invalidate_rxbuf: + mcp->rxbuf = NULL; + mcp->rxbuf_len = 0; + mcp->rxbuf_idx = 0; + return ret; } @@ -499,8 +506,12 @@ static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, mcp->rxbuf_idx = 0; mcp->rxbuf = data->block; + mcp->rxbuf_len = sizeof(data->block); mcp->txbuf[0] = MCP2221_I2C_GET_DATA; ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); + mcp->rxbuf_idx = 0; + mcp->rxbuf = NULL; + mcp->rxbuf_len = 0; if (ret) goto exit; } else { @@ -522,8 +533,12 @@ static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, mcp->rxbuf_idx = 0; mcp->rxbuf = data->block; + mcp->rxbuf_len = sizeof(data->block); mcp->txbuf[0] = MCP2221_I2C_GET_DATA; ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); + mcp->rxbuf_idx = 0; + mcp->rxbuf = NULL; + mcp->rxbuf_len = 0; if (ret) goto exit; } else { @@ -734,6 +749,7 @@ static int mcp2221_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size) { u8 *buf; + int len; struct mcp2221 *mcp = hid_get_drvdata(hdev); switch (data[0]) { @@ -792,9 +808,15 @@ static int mcp2221_raw_event(struct hid_device *hdev, break; } if (data[2] == MCP2221_I2C_READ_COMPL) { + if (mcp->rxbuf == NULL || mcp->rxbuf_idx >= mcp->rxbuf_len) + return 1; /* no complete() in this case */ + buf = mcp->rxbuf; - memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]); - mcp->rxbuf_idx = mcp->rxbuf_idx + data[3]; + len = data[3]; + if (len > mcp->rxbuf_len - mcp->rxbuf_idx) + len = mcp->rxbuf_len - mcp->rxbuf_idx; + memcpy(&buf[mcp->rxbuf_idx], &data[4], len); + mcp->rxbuf_idx = mcp->rxbuf_idx + len; mcp->status = 0; break; }