From patchwork Sun Jun 2 23:24:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 13683131 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D2236C27C50 for ; Sun, 2 Jun 2024 23:25:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=1gpq7TwHRqRc/HEm6xTA7V3/tldr48ttscFaxoY8ZlA=; b=D9hcAo1lYDL0LP C+FJi53ccEH/0u+QRcraipnAG5z19gb9JUh64+aUkQBUV4lvczv1EH0pnB9r53H3zEOwvP4jYgfj0 VM/soxWEDq5y36As28ral3s2/9/ctVC7iRgq9wDQrq42ASu/jQzfjR2C3kkQstgaySMHZuMxLA6Gk 1kVK0IGg8FvIrXsZUgT+rI1Of+EEdroAycildKPWe/fLPL9K7opMDQsu9HYZch/OdDXIvrXJjOraf +I+zBeI98c88a84FbcesOHmk1EzColh9tznI2tlzm20ATqSdHr1/HXIHCWfDlsM03fx2983bid9ah nx4eKgAhCS9Vu9pTml/w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZY-0000000F67e-1EQ2; Sun, 02 Jun 2024 23:25:08 +0000 Received: from mail-lf1-x12b.google.com ([2a00:1450:4864:20::12b]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZS-0000000F63k-1MR9 for linux-arm-kernel@lists.infradead.org; Sun, 02 Jun 2024 23:25:04 +0000 Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-52b912198f1so2020388e87.0 for ; Sun, 02 Jun 2024 16:25:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717370700; x=1717975500; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=RS0RRS8kWTy5YtdarUDO9BTC5OYKf3Jm1kS68cq467k=; b=K2xTKfkLxfhHVmRcnbj6jP91tViKOkc7ChjP0ljkW9BUq7+p/4aMIfEdCPzCI7ciBa obmfPEXOfJG5ERPp1qhJMjBpm+Vkdbo0mAC6FtqrYrKxGzOfxTKqq96N93X/QMYz+BiL sqtXoShwE9gzzOUyU36hejZygBSOlcbQ8kJFMzBbHS7EN5sWmkbxOO7YpZDIag/wWXtV FM+/btQ3V7KUHMpl5htgSv0ssDy+w7gDje76XBUJNrPFuWN/zOxXNaLo4MI7XX6CuNaC s4qnWu7G8yFy0qUNoe9vZamC9O6XgOeIK8TFyKAZJn69lwhokkMjM14rDADsuCc+k/ki z6sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717370700; x=1717975500; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RS0RRS8kWTy5YtdarUDO9BTC5OYKf3Jm1kS68cq467k=; b=NRCWZRHHzm9BHC8T3NYfW2+cYGDISD4NOt92kLanG1r1nTJ+CC/Pt7yvHsDMqmqdnD QTyzKwn3PXPb6UlA7SjGJJ1WqTe9XMR2dnQovOWxlDu1hfPMDo1Vklv+yyjR89FxDBgd DVdHyy+gCBmfhyyjAGtlgweMrMjtsQe1y8qsY0K0Qzw8muqkLS7ooR5TOQI+h9DF52Hl HTUiXoT3pKK9WLzjoPHtMCVDSo3fr9uYdU3/GCcCRvUJRHMLRI5rAnAZ4E4IRnum3OSn u9hGI7I4KlgO09yFZJkmqrvZ3WQxrth+b73oVUalWma/t35PVDsIGA0iMEoIyCt/5DQZ iIaQ== X-Forwarded-Encrypted: i=1; AJvYcCUYBwwRjBBjLO5l9bN7p7lqUcS6pDTipYXnjFgwiM39o4ytQDdK8MVH9B2jIgID10Xe3n6XbRkD75/5hx5bK8YeTyyC6ehUH70ilo1Od/FjLj8RbAM= X-Gm-Message-State: AOJu0YwgRDJ34MJFcle8HOWjiGhueJygt+w3Pvg4+7jx/5GvgBj0ZXA3 nf+GFQxJudDId3HtrSUVnd/P1fCsSAcGqeTLZFXygn5ud/lkOhLsPkxgoLBpzvk= X-Google-Smtp-Source: AGHT+IF+7ECElpfPWCZ815PFEw8gDVd7yShai1/+oSnWq0vMjhxFgQHiWSjlGgySRRjvFrPegfiCDA== X-Received: by 2002:ac2:4296:0:b0:52b:8363:34ef with SMTP id 2adb3069b0e04-52b8957535amr4327645e87.15.1717370699513; Sun, 02 Jun 2024 16:24:59 -0700 (PDT) Received: from umbar.lan ([192.130.178.91]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-52b959008aasm392369e87.269.2024.06.02.16.24.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Jun 2024 16:24:59 -0700 (PDT) From: Dmitry Baryshkov Date: Mon, 03 Jun 2024 02:24:57 +0300 Subject: [PATCH RFC 4/7] usb: typec: ucsi: rework command execution functions MIME-Version: 1.0 Message-Id: <20240603-ucsi-rework-interface-v1-4-99a6d544cec8@linaro.org> References: <20240603-ucsi-rework-interface-v1-0-99a6d544cec8@linaro.org> In-Reply-To: <20240603-ucsi-rework-interface-v1-0-99a6d544cec8@linaro.org> To: Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue Cc: Nikita Travkin , Neil Armstrong , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4449; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=Qlvm0O1XPt3pR+fGT5Ihsrd6DrRUvL18urSNodiPM/A=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmXP9HRhpoJUkVmxwoBwDeeIJduNwYx81xqhxun KdkqwkkPk2JATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZlz/RwAKCRCLPIo+Aiko 1bUZB/4xcmd1cxqLd8b97oPAYAEfMP/CdrDSsChcE9bqtNRiHX+0qrVNseJxk2CFl+ROSR64LNn gKZw+qyiXaDDk+k04/3Y0HJ8fTmy0um3lkCN9mY5Hesh8+FQILSfOLL4+cy4uY/2bVV0vV90jVl kBGQrqD/MKruFqGbPxGl6upEXpsHCL4V4Fs3fkzxrq+jmPFCFcIAUFGFsP+3vPX89pqpvHV/xFE 7w4bCyjn2Wr+lJtHMFX/4pzVTyiMsHgaBIw/H7Xb6XUR5y0AaIjsMHlaYMivSjwnTZIkTL3g85a QkfXmhOg5YtUNjAzRk3cO6D+I0uIt0OKIF/Sn+OvRrpYJFMA X-Developer-Key: i=dmitry.baryshkov@linaro.org; a=openpgp; fpr=8F88381DD5C873E4AE487DA5199BF1243632046A X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240602_162502_521981_42E7ABDE X-CRM114-Status: GOOD ( 15.13 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Rework command execution code to remove recursive calls of ucsi_exec_command. This also streamlines the sync_control / read(CCI) read (MESSAGE_IN) sequence, allowing further rework of the command code. Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 134 ++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 70 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index a3a29ea4e325..54a2c505442f 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -63,25 +63,74 @@ static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) return ucsi->ops->sync_control(ucsi, ctrl); } -static int ucsi_exec_command(struct ucsi *ucsi, u64 command); - -static int ucsi_read_error(struct ucsi *ucsi) +static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, + void *data, size_t size, bool conn_ack) { - u16 error; int ret; - ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS); - if (ret < 0) + *cci = 0; + + ret = ucsi->ops->sync_control(ucsi, command); + if (ret) return ret; - ret = ucsi_read_message_in(ucsi, &error, sizeof(error)); + ret = ucsi->ops->read_cci(ucsi, cci); if (ret) return ret; - ret = ucsi_acknowledge(ucsi, false); + if (*cci & UCSI_CCI_BUSY) + return -EBUSY; + + if (!(*cci & UCSI_CCI_COMMAND_COMPLETE)) + return -EIO; + + if (*cci & UCSI_CCI_NOT_SUPPORTED) { + if (ucsi_acknowledge(ucsi, false) < 0) + dev_err(ucsi->dev, + "ACK of unsupported command failed\n"); + return -EOPNOTSUPP; + } + + if (*cci & UCSI_CCI_ERROR) { + /* Acknowledge the command that failed */ + ret = ucsi_acknowledge(ucsi, false); + return ret ? ret : -EIO; + } + + if (data) { + ret = ucsi_read_message_in(ucsi, data, size); + if (ret) + return ret; + } + + ret = ucsi_acknowledge(ucsi, conn_ack); if (ret) return ret; + return 0; +} + +static int ucsi_read_error(struct ucsi *ucsi) +{ + u16 error; + u32 cci; + int ret; + + ret = ucsi_run_command(ucsi, UCSI_GET_ERROR_STATUS, &cci, + &error, sizeof(error), false); + + if (cci & UCSI_CCI_BUSY) { + ret = ucsi_run_command(ucsi, UCSI_CANCEL, &cci, NULL, 0, false); + + return ret ? ret : -EBUSY; + } + + if (ret < 0) + return ret; + + if (cci & UCSI_CCI_ERROR) + return -EIO; + switch (error) { case UCSI_ERROR_INCOMPATIBLE_PARTNER: return -EOPNOTSUPP; @@ -121,78 +170,23 @@ static int ucsi_read_error(struct ucsi *ucsi) return -EIO; } -static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) -{ - u32 cci; - int ret; - - ret = ucsi->ops->sync_control(ucsi, cmd); - if (ret) - return ret; - - ret = ucsi->ops->read_cci(ucsi, &cci); - if (ret) - return ret; - - if (cmd != UCSI_CANCEL && cci & UCSI_CCI_BUSY) - return ucsi_exec_command(ucsi, UCSI_CANCEL); - - if (!(cci & UCSI_CCI_COMMAND_COMPLETE)) - return -EIO; - - if (cci & UCSI_CCI_NOT_SUPPORTED) { - if (ucsi_acknowledge(ucsi, false) < 0) - dev_err(ucsi->dev, - "ACK of unsupported command failed\n"); - return -EOPNOTSUPP; - } - - if (cci & UCSI_CCI_ERROR) { - /* Acknowledge the command that failed */ - ret = ucsi_acknowledge(ucsi, false); - if (ret) - return ret; - - if (cmd == UCSI_GET_ERROR_STATUS) - return -EIO; - - return ucsi_read_error(ucsi); - } - - if (cmd == UCSI_CANCEL && cci & UCSI_CCI_CANCEL_COMPLETE) { - ret = ucsi_acknowledge(ucsi, false); - return ret ? ret : -EBUSY; - } - - return UCSI_CCI_LENGTH(cci); -} - static int ucsi_send_command_common(struct ucsi *ucsi, u64 command, void *data, size_t size, bool conn_ack) { - u8 length; + u32 cci; int ret; mutex_lock(&ucsi->ppm_lock); - ret = ucsi_exec_command(ucsi, command); - if (ret < 0) - goto out; - - length = ret; - - if (data) { - ret = ucsi_read_message_in(ucsi, data, size); - if (ret) - goto out; + ret = ucsi_run_command(ucsi, command, &cci, data, size, conn_ack); + if (cci & UCSI_CCI_BUSY) { + ret = ucsi_run_command(ucsi, UCSI_CANCEL, &cci, NULL, 0, false); + return ret ? ret : -EBUSY; } - ret = ucsi_acknowledge(ucsi, conn_ack); - if (ret) - goto out; + if (cci & UCSI_CCI_ERROR) + return ucsi_read_error(ucsi); - ret = length; -out: mutex_unlock(&ucsi->ppm_lock); return ret; }