Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2175271rwl; Thu, 13 Apr 2023 02:43:58 -0700 (PDT) X-Google-Smtp-Source: AKy350acKfu2nDAhl/UyJoDcd/q/kwaFFGYsYD7+HuaVib82KQvDXViP/yclR8UGkfxyKqDzff8P X-Received: by 2002:a17:907:9905:b0:94d:4920:762c with SMTP id ka5-20020a170907990500b0094d4920762cmr2536912ejc.63.1681379038289; Thu, 13 Apr 2023 02:43:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681379038; cv=none; d=google.com; s=arc-20160816; b=tatuCH2AR7iL0n8yxzOkG9NYV7G8JYvC6aKCRLsH+ABQ442g8qiZ24+qnbK80qRQth VfQbhF2C1vzK/dNFBBQYB8DAovSojBZM/KTeQGoQWLtot7VE9Xtz9dH+3rH06kR1OoII 7Hd//TDDaeD+clwKDzahCk0znSUSlLhTKYtyWZ2p45HvqGA0OOhMYw9118FvEUijhSAr pMBf4JYTqd+h0TOu9E5NW/SW7XKAmC+GZMFBavOegkXf/Oy+k9H/m8va6QO5+TqOgeBg Tm9XFDlfuHyg7BtZJqGONNeLun8+Z5w7OzH2PANebWaPJhzHx+fbx8nOoZSYol9RjFrK tjrw== 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 :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature; bh=GTkgi9DEYv2y6KPQY0Pk1iavkXW4SL1GDX6CwlELQMI=; b=QkOOwLUa8Ybx1mM2UNw6Xk8GnutqY2zzrOzwJt4VpYo8cPuS+Zks/MIyQYd7CT8dn9 p4AJPiAvNtweUVuuvKI5/Kim76QHwxNs8AQuGFzfuTpJY/E8llIgMo7dmm8cI0zIqXbG eHs32X1EVhdAl0qoVE/N1NnGq3bGnAKSmQLxiCZXkulMVIwdLG/u25MyW2OjqAU8Fxdm ze7WlgsoJArsnEB2chjKLfYdetvAxHhHgvZp3PvqdzM6H5dDeZs382lo8WncM4V3vrxn l+5AQ16XW98DpqyylucYmgXYolekTeSS5shOZueISEmLDozKnRgaJKq2JzQN6lfempEv og3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tq-group.com header.s=key1 header.b=lKpAdEMf; dkim=pass header.i=@tq-group.com header.s=key1 header.b="V/bPdRtF"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=tq-group.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k4-20020a1709065fc400b0094e559b4266si1515521ejv.867.2023.04.13.02.43.33; Thu, 13 Apr 2023 02:43:58 -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; dkim=pass header.i=@tq-group.com header.s=key1 header.b=lKpAdEMf; dkim=pass header.i=@tq-group.com header.s=key1 header.b="V/bPdRtF"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=tq-group.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229704AbjDMJmA (ORCPT + 99 others); Thu, 13 Apr 2023 05:42:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229498AbjDMJl7 (ORCPT ); Thu, 13 Apr 2023 05:41:59 -0400 Received: from mx1.tq-group.com (mx1.tq-group.com [93.104.207.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B5C610C2; Thu, 13 Apr 2023 02:41:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1681378917; x=1712914917; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=GTkgi9DEYv2y6KPQY0Pk1iavkXW4SL1GDX6CwlELQMI=; b=lKpAdEMfw4ASWTAmPIb+9+AQTa6spusmwB8l1K6/TNge1k3+cREXarYV Ptg/6JWyAU4Vc3fKIrrrK4Pku1j2nDSUlRoE5ep89LJL3rYwR9wsL3C5u 9w2I00tezldMV4CVP1RaHgmdZtEGp6QXtFlgougQCaPKISla9kJyxN9C3 qgwiYNSvNMISuhudh7A48UZco3FDf0glRoeOnwFQgrYftWNCuWlRSMHf+ MtVzNjiu2SoShNHFex2afvvXw40nRfOr14g2ozst1AucbwQ6Inz0EbQyN vvwoP73PW7njoe0EMpR81g0x8HEtSH0wexKgHG0l6X9I4ttAQM6zEb3jJ g==; X-IronPort-AV: E=Sophos;i="5.98,341,1673910000"; d="scan'208";a="30301482" Received: from unknown (HELO tq-pgp-pr1.tq-net.de) ([192.168.6.15]) by mx1-pgp.tq-group.com with ESMTP; 13 Apr 2023 11:41:55 +0200 Received: from mx1.tq-group.com ([192.168.6.7]) by tq-pgp-pr1.tq-net.de (PGP Universal service); Thu, 13 Apr 2023 11:41:55 +0200 X-PGP-Universal: processed; by tq-pgp-pr1.tq-net.de on Thu, 13 Apr 2023 11:41:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1681378915; x=1712914915; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=GTkgi9DEYv2y6KPQY0Pk1iavkXW4SL1GDX6CwlELQMI=; b=V/bPdRtF8bE9tV1RLYx7bexUigLICd3rCt9QZCDjwaN28heBW8M9vnex BfZ7qalF4ryANR9dySTLR0OcpwIOfK2CUZJb0ThIJhOiWrH0+pda5qzKq QiXdpk8BZwNZg0MIOhm8Xdm/8ag/sPuPH6ky9BlF7IaY7nHle8+VkxJUt 1NNsqCORRVlrT9WweL8ALZF+fs9MLguudRJwVjOHGfRO1ghGHnXtQaerF jgjRJyUkM0X3JuxIvZBwO1OwmxEj1cHMI4mXGcjSO5D5kz+kTByd+S/Z5 qlGPsUIiFrV5q1h8c8ZgaQ5zSFBykiXViR5Il72CAYU5FUOyQDgeV40OJ A==; X-IronPort-AV: E=Sophos;i="5.98,341,1673910000"; d="scan'208";a="30301481" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 13 Apr 2023 11:41:54 +0200 Received: from localhost.localdomain (SCHIFFERM-M2.tq-net.de [10.121.49.20]) by vtuxmail01.tq-net.de (Postfix) with ESMTPA id 6579B280056; Thu, 13 Apr 2023 11:41:54 +0200 (CEST) From: Matthias Schiffer To: Peter Korsgaard , Andrew Lunn Cc: Federico Vaga , Wolfram Sang , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, linux@ew.tq-group.com, Gregor Herburger , Matthias Schiffer Subject: [PATCH v2] i2c: ocores: generate stop condition after timeout in polling mode Date: Thu, 13 Apr 2023 11:37:37 +0200 Message-Id: <20230413093737.15303-1-matthias.schiffer@ew.tq-group.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 From: Gregor Herburger In polling mode, no stop condition is generated after a timeout. This causes SCL to remain low and thereby block the bus. If this happens during a transfer it can cause slaves to misinterpret the subsequent transfer and return wrong values. To solve this, pass the ETIMEDOUT error up from ocores_process_polling() instead of setting STATE_ERROR directly. The caller is adjusted to call ocores_process_timeout() on error both in polling and in IRQ mode, which will set STATE_ERROR and generate a stop condition. Fixes: 69c8c0c0efa8 ("i2c: ocores: add polling interface") Signed-off-by: Gregor Herburger Signed-off-by: Matthias Schiffer --- v2: style improvements based on feedback from Federico and Andrew. I went with a slightly different solution than Andrew suggested to avoid using the ret variable for two different kinds of returns. drivers/i2c/busses/i2c-ocores.c | 35 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-ocores.c index a0af027db04c1..2e575856c5cd5 100644 --- a/drivers/i2c/busses/i2c-ocores.c +++ b/drivers/i2c/busses/i2c-ocores.c @@ -342,18 +342,18 @@ static int ocores_poll_wait(struct ocores_i2c *i2c) * ocores_isr(), we just add our polling code around it. * * It can run in atomic context + * + * Return: 0 on success, -ETIMEDOUT on timeout */ -static void ocores_process_polling(struct ocores_i2c *i2c) +static int ocores_process_polling(struct ocores_i2c *i2c) { - while (1) { - irqreturn_t ret; - int err; + irqreturn_t ret; + int err = 0; + while (1) { err = ocores_poll_wait(i2c); - if (err) { - i2c->state = STATE_ERROR; + if (err) break; /* timeout */ - } ret = ocores_isr(-1, i2c); if (ret == IRQ_NONE) @@ -364,13 +364,15 @@ static void ocores_process_polling(struct ocores_i2c *i2c) break; } } + + return err; } static int ocores_xfer_core(struct ocores_i2c *i2c, struct i2c_msg *msgs, int num, bool polling) { - int ret; + int ret = 0; u8 ctrl; ctrl = oc_getreg(i2c, OCI2C_CONTROL); @@ -388,15 +390,16 @@ static int ocores_xfer_core(struct ocores_i2c *i2c, oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); if (polling) { - ocores_process_polling(i2c); + ret = ocores_process_polling(i2c); } else { - ret = wait_event_timeout(i2c->wait, - (i2c->state == STATE_ERROR) || - (i2c->state == STATE_DONE), HZ); - if (ret == 0) { - ocores_process_timeout(i2c); - return -ETIMEDOUT; - } + if (wait_event_timeout(i2c->wait, + (i2c->state == STATE_ERROR) || + (i2c->state == STATE_DONE), HZ) == 0) + ret = -ETIMEDOUT; + } + if (ret) { + ocores_process_timeout(i2c); + return ret; } return (i2c->state == STATE_DONE) ? num : -EIO; -- TQ-Systems GmbH | Mühlstraße 2, Gut Delling | 82229 Seefeld, Germany Amtsgericht München, HRB 105018 Geschäftsführer: Detlef Schneider, Rüdiger Stahl, Stefan Schneider http://www.tq-group.com/