From patchwork Sun Feb 18 22:20:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Ehrhardt X-Patchwork-Id: 13562090 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 1300AC48BC4 for ; Sun, 18 Feb 2024 22:32:12 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=XXljNxHoOQteJ8zMwfGamoy+l3+oeP4uXDymSPwHe3U=; b=pI/UhcmhKKK+m9 Snb2LQfHFLSWy1CeNvK2q/dpbRH+C7X3DmZIPK4MJSvoPQWYrg6ISanRjfyB//9dauvNB2fwDjagw 8XAtderF0imInqw57Q0PZS400p7iwRVTHuqaJqxP5UJEWTho0l3EtdhfY4gxtoe0xYjjZH+/Yb7eM j7r+iFCbUqjQwyByX66WrcShqHQv5y1qmndZYJUAwUdnXFVMybmJskYdjNz+BRotJ13bF3wTlx/gB IUQSzaLSbb7DD3cByX3T5IpuomGGMmPp/SE6xhZh2glU0O4CV+qJ2pJDSBS9WJ89CeLpoD/ZiEugh dCROmCiOAa/N3U27pinw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbphY-00000008YnS-1Dq6; Sun, 18 Feb 2024 22:32:00 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbphX-00000008Ymo-0GmH for linux-arm-kernel@bombadil.infradead.org; Sun, 18 Feb 2024 22:31:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=gPgo9NBk+JUVwV+wS1nZASCgTChJyClnALoBoFmZOSg=; b=OmjCzdViaaNvcmj6/qwX+ZX/mP QpFJv0g9wuE30Q6KtR/M2g03pVi9X+OlNeGNVNxBSZVvOXKRfjkE0q2m+6zfqvuQlsORfoTxOgZG1 4Gy1XOKwJuy28HR8DVl5EQcMJSXW9Cpfkc2aRA+2b03we3qf1N8Qh1dyjhNV+sPo8h6qgvCcGVH06 5RX35/xwHDhdF/b5Mf7U6VObIqt357nDZVclq8QoBxL8t2yHMDISVUR3x71j/f7hpK5bBeJtQRBlC a28nKRh7BliE39aDDittxqfY3M+qw4QH8hTV2LGUuV+EqpqskfKJTESZnXsmrzVewqZp6ruulq4Mc von1IAkw==; Received: from cae.in-ulm.de ([217.10.14.231]) by desiato.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbphT-00000000xYe-3XmX for linux-arm-kernel@lists.infradead.org; Sun, 18 Feb 2024 22:31:57 +0000 Received: by cae.in-ulm.de (Postfix, from userid 1000) id E687C14028D; Sun, 18 Feb 2024 23:21:04 +0100 (CET) From: "Christian A. Ehrhardt" To: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Christian A. Ehrhardt" , Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , =?utf-8?q?Samuel_?= =?utf-8?q?=C4=8Cavoj?= , Hans de Goede , Neil Armstrong , Prashanth K , Dmitry Baryshkov , Saranya Gopal , Haotien Hsu , Andy Shevchenko , Utkarsh Patel , Bjorn Andersson , Luca Weiss , Min-Hua Chen , Rob Herring , Rajaram Regupathy , linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, Abhishek Pandit-Subedi Subject: [RFC PATCH 1/6] usb: ucsi_glink: Fix endianness issues Date: Sun, 18 Feb 2024 23:20:34 +0100 Message-Id: <20240218222039.822040-2-lk@c--e.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240218222039.822040-1-lk@c--e.de> References: <20240218222039.822040-1-lk@c--e.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240218_223156_161985_6399DA48 X-CRM114-Status: UNSURE ( 9.03 ) X-CRM114-Notice: Please train this message. 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 Sparse complains that some little endian header fields are assigned from plain integer values. Add the proper cpu_to_le32() calls. Signed-off-by: Christian A. Ehrhardt --- drivers/usb/typec/ucsi/ucsi_glink.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi_glink.c b/drivers/usb/typec/ucsi/ucsi_glink.c index 0bd3f6dee678..0a0d08467c56 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -85,9 +85,9 @@ static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset, unsigned long left; int ret; - req.hdr.owner = PMIC_GLINK_OWNER_USBC; - req.hdr.type = MSG_TYPE_REQ_RESP; - req.hdr.opcode = UC_UCSI_READ_BUF_REQ; + req.hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_USBC); + req.hdr.type = cpu_to_le32(MSG_TYPE_REQ_RESP); + req.hdr.opcode = cpu_to_le32(UC_UCSI_READ_BUF_REQ); mutex_lock(&ucsi->lock); memset(ucsi->read_buf, 0, sizeof(ucsi->read_buf)); @@ -122,9 +122,9 @@ static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned i unsigned long left; int ret; - req.hdr.owner = PMIC_GLINK_OWNER_USBC; - req.hdr.type = MSG_TYPE_REQ_RESP; - req.hdr.opcode = UC_UCSI_WRITE_BUF_REQ; + req.hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_USBC); + req.hdr.type = cpu_to_le32(MSG_TYPE_REQ_RESP); + req.hdr.opcode = cpu_to_le32(UC_UCSI_WRITE_BUF_REQ); memcpy(&req.buf[offset], val, val_len); reinit_completion(&ucsi->write_ack); From patchwork Sun Feb 18 22:20:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Ehrhardt X-Patchwork-Id: 13562085 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 C438EC48BC4 for ; Sun, 18 Feb 2024 22:31:11 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=OrVWDJZ47/JVK4e3eqAne7+wUl6vOf+F4pMkZFjawjA=; b=dGMK0x4V2Dfy7k q0Z7Pu2DdW49F9eCdPwJ4MPoA2rGzfejpMANqCesEslKYODA/jcGj7PwOKiS9hf8oYRtG45A3IWms KkEk2Cn/gNhWBdLgl+60+nf5U/K2X67t8Wh8inCTsmEx1exveavJDvKrmy608b3DV3ixCzpc4C+/w LPaozQlAOoP6jKhk2yqeJjh/qoM5ExdAIee4USWQuoF4aMG9Jcxd9+0kG7rwLW4Ob6wb5rB6GYjSc IiS6KHvZhX92w6qkVMFLoUV4/MhnBG+fDz02kLrhYYllUb31KA8vMk7Yww1OIYoQ2KoL8LxDkXLWE c1IAqj/aO7ocQwxk8mJQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgd-00000008YTF-2MXr; Sun, 18 Feb 2024 22:31:03 +0000 Received: from cae.in-ulm.de ([217.10.14.231]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgW-00000008YRJ-0hCV for linux-arm-kernel@lists.infradead.org; Sun, 18 Feb 2024 22:30:57 +0000 Received: by cae.in-ulm.de (Postfix, from userid 1000) id CE2C414033C; Sun, 18 Feb 2024 23:21:05 +0100 (CET) From: "Christian A. Ehrhardt" To: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Christian A. Ehrhardt" , Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , =?utf-8?q?Samuel_?= =?utf-8?q?=C4=8Cavoj?= , Hans de Goede , Neil Armstrong , Prashanth K , Dmitry Baryshkov , Saranya Gopal , Haotien Hsu , Andy Shevchenko , Utkarsh Patel , Bjorn Andersson , Luca Weiss , Min-Hua Chen , Rob Herring , Rajaram Regupathy , linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, Abhishek Pandit-Subedi Subject: [RFC PATCH 2/6] ucsi_ccg: Cleanup endianness confusion Date: Sun, 18 Feb 2024 23:20:35 +0100 Message-Id: <20240218222039.822040-3-lk@c--e.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240218222039.822040-1-lk@c--e.de> References: <20240218222039.822040-1-lk@c--e.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240218_143056_370899_24576B6D X-CRM114-Status: GOOD ( 15.88 ) 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 When storing the cci value in the op_region struct it is converted from host to little endian. However, the value is read from hardware that is little endian according to the spec and it is never converted to host byte order. However, the value is used as if it where in host byte order. Additionally, the message_in buffer is a byte array. Any endian interpretation depends on the current command and must be done in the ocntext of that command. While all the UCSI world seems to be little endian and there are many other endian issues if this is not true, this particular value is treated with endian awareness, so it should at least be done correctly. Add the missing conversion from little endian to host byte order when reading the CCI value from hardware. Additionally, make the message_in buffer an u8 array and adjust the size macro accordingly. Signed-off-by: Christian A. Ehrhardt --- drivers/usb/typec/ucsi/ucsi_ccg.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index dda7c7c94e08..709295948c65 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -192,10 +192,10 @@ struct ucsi_ccg_altmode { bool checked; } __packed; -#define CCGX_MESSAGE_IN_MAX 4 +#define CCGX_MESSAGE_IN_MAX 16 struct op_region { __le32 cci; - __le32 message_in[CCGX_MESSAGE_IN_MAX]; + u8 message_in[CCGX_MESSAGE_IN_MAX]; }; struct ucsi_ccg { @@ -678,6 +678,7 @@ static irqreturn_t ccg_irq_handler(int irq, void *data) u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_CCI); struct ucsi_ccg *uc = data; u8 intr_reg; + __le32 __cci; u32 cci = 0; int ret = 0; @@ -690,9 +691,10 @@ static irqreturn_t ccg_irq_handler(int irq, void *data) else if (!(intr_reg & UCSI_READ_INT)) goto err_clear_irq; - ret = ccg_read(uc, reg, (void *)&cci, sizeof(cci)); + ret = ccg_read(uc, reg, (void *)&__cci, sizeof(__cci)); if (ret) goto err_clear_irq; + cci = le32_to_cpu(__cci); if (UCSI_CCI_CONNECTOR(cci)) ucsi_connector_change(uc->ucsi, UCSI_CCI_CONNECTOR(cci)); From patchwork Sun Feb 18 22:20:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Ehrhardt X-Patchwork-Id: 13562086 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 DB033C48BF6 for ; Sun, 18 Feb 2024 22:31:14 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=bfn2GWB/IQjwvEw9Fp1DmrPTeoFxz6vuJoPqSb/Lwd0=; b=RUXoA5zHQemHBQ PfkY+scqnYWK0gGOXivI9I1uUpXM5tQk5KOA9q6vwQGgn7aT5vcxzXxIWulLOgSBZtv4JZ6BVGoCT Tx8FkbvV0erf4ru8t6hWmF6a/l/JhknAwoXumGSg483ntXkRRTECuKggjGupkSgOwUSQNhuCy++8i oX0nolcxUb9eQCmI9bzoy0yVT8cBRSsrAASdyUOurXyRg/u7W/nzZIGUIi2N+7I2S2b7l5eaADJfv 7eJKfrVJw1W/DpPtTaTTj8uCVUWeXuNcq969n5F4+kZTTFl5pkJBDeO0nT69qOUYeH5dHPje0CjI1 wBdri7+C32xWmmmb3u4w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgc-00000008YSw-45ti; Sun, 18 Feb 2024 22:31:02 +0000 Received: from cae.in-ulm.de ([217.10.14.231]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgU-00000008YQL-2KCx for linux-arm-kernel@lists.infradead.org; Sun, 18 Feb 2024 22:30:55 +0000 Received: by cae.in-ulm.de (Postfix, from userid 1000) id 92A2E140406; Sun, 18 Feb 2024 23:21:06 +0100 (CET) From: "Christian A. Ehrhardt" To: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Christian A. Ehrhardt" , Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , =?utf-8?q?Samuel_?= =?utf-8?q?=C4=8Cavoj?= , Hans de Goede , Neil Armstrong , Prashanth K , Dmitry Baryshkov , Saranya Gopal , Haotien Hsu , Andy Shevchenko , Utkarsh Patel , Bjorn Andersson , Luca Weiss , Min-Hua Chen , Rob Herring , Rajaram Regupathy , linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, Abhishek Pandit-Subedi Subject: [RFC PATCH 3/6] usb: typec: ucsi: Make Version a parameter to ucsi_register Date: Sun, 18 Feb 2024 23:20:36 +0100 Message-Id: <20240218222039.822040-4-lk@c--e.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240218222039.822040-1-lk@c--e.de> References: <20240218222039.822040-1-lk@c--e.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240218_143054_759898_60BD7E21 X-CRM114-Status: GOOD ( 17.05 ) 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 Reading UCSI_VERSION is a special case as there is no notification that syncs the data into host memory. Read UCSI_VERSION only once during initialization and provide it as a parameter to ucsi_register(). Signed-off-by: Christian A. Ehrhardt --- drivers/usb/typec/ucsi/ucsi.c | 13 ++++--------- drivers/usb/typec/ucsi/ucsi.h | 2 +- drivers/usb/typec/ucsi/ucsi_acpi.c | 8 +++++++- drivers/usb/typec/ucsi/ucsi_ccg.c | 12 ++++++++++-- drivers/usb/typec/ucsi/ucsi_glink.c | 11 ++++++++++- drivers/usb/typec/ucsi/ucsi_stm32g0.c | 26 ++++++++++++++++++++++---- 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index ae105383e69e..eb630447ba80 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -1581,18 +1581,13 @@ EXPORT_SYMBOL_GPL(ucsi_destroy); /** * ucsi_register - Register UCSI interface * @ucsi: UCSI instance + * @version: The revision of the UCSI spec */ -int ucsi_register(struct ucsi *ucsi) +int ucsi_register(struct ucsi *ucsi, u16 version) { - int ret; - - ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version, - sizeof(ucsi->version)); - if (ret) - return ret; - - if (!ucsi->version) + if (!version) return -ENODEV; + ucsi->version = version; /* * Version format is JJ.M.N (JJ = Major version, M = Minor version, diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 7e35ffbe0a6f..100e16b49814 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -77,7 +77,7 @@ struct ucsi_operations { struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops); void ucsi_destroy(struct ucsi *ucsi); -int ucsi_register(struct ucsi *ucsi); +int ucsi_register(struct ucsi *ucsi, u16 version); void ucsi_unregister(struct ucsi *ucsi); void *ucsi_get_drvdata(struct ucsi *ucsi); void ucsi_set_drvdata(struct ucsi *ucsi, void *data); diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/ucsi_acpi.c index 928eacbeb21a..710ddfc03ed0 100644 --- a/drivers/usb/typec/ucsi/ucsi_acpi.c +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c @@ -226,6 +226,7 @@ static int ucsi_acpi_probe(struct platform_device *pdev) const struct dmi_system_id *id; struct ucsi_acpi *ua; struct resource *res; + u16 version; acpi_status status; int ret; @@ -272,7 +273,12 @@ static int ucsi_acpi_probe(struct platform_device *pdev) return -ENODEV; } - ret = ucsi_register(ua->ucsi); + ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ); + if (ret) + return ret; + version = le16_to_cpu(*(__le16 *)(ua->base + UCSI_VERSION)); + + ret = ucsi_register(ua->ucsi, version); if (ret) { acpi_remove_notify_handler(ACPI_HANDLE(&pdev->dev), ACPI_DEVICE_NOTIFY, diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index 709295948c65..d47f5e31c98a 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -1356,7 +1356,7 @@ static int ccg_restart(struct ucsi_ccg *uc) return status; } - status = ucsi_register(uc->ucsi); + status = ucsi_register(uc->ucsi, uc->ucsi->version); if (status) { dev_err(uc->dev, "failed to register the interface\n"); return status; @@ -1422,6 +1422,7 @@ static int ucsi_ccg_probe(struct i2c_client *client) struct ucsi_ccg *uc; const char *fw_name; int status; + __le16 version; uc = devm_kzalloc(dev, sizeof(*uc), GFP_KERNEL); if (!uc) @@ -1477,7 +1478,14 @@ static int ucsi_ccg_probe(struct i2c_client *client) goto out_ucsi_destroy; } - status = ucsi_register(uc->ucsi); + status = ccg_read(uc, CCGX_RAB_UCSI_DATA_BLOCK(UCSI_VERSION), + (u8 *)&version, sizeof(version)); + if (status < 0) { + dev_err(uc->dev, "cannot read UCSI version - %d\n", status); + return status; + } + + status = ucsi_register(uc->ucsi, le16_to_cpu(version)); if (status) goto out_free_irq; diff --git a/drivers/usb/typec/ucsi/ucsi_glink.c b/drivers/usb/typec/ucsi/ucsi_glink.c index 0a0d08467c56..7a0b28536abd 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -255,8 +255,17 @@ static void pmic_glink_ucsi_notify(struct work_struct *work) static void pmic_glink_ucsi_register(struct work_struct *work) { struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, register_work); + __le16 version; + int ret; + + ret = pmic_glink_ucsi_read(ucsi->ucsi, UCSI_VERSION, &version, + sizeof(version)); + if (ret < 0) { + dev_err(ucsi->dev, "cannot read version: %d\n", ret); + return; + } - ucsi_register(ucsi->ucsi); + ucsi_register(ucsi->ucsi, le16_to_cpu(version)); } static void pmic_glink_ucsi_callback(const void *data, size_t len, void *priv) diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c index 93d7806681cf..112692c7a158 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -325,10 +325,10 @@ static int ucsi_stm32g0_fw_rcv(struct ucsi *ucsi, void *data, size_t len) return ucsi_stm32g0_bl_rcv_woack(ucsi, data, len); } -/* UCSI ops */ -static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t len) +static int ucsi_stm32g0_read_from_hw(struct ucsi_stm32g0 *g0, + unsigned int offset, + void *val, size_t len) { - struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); struct i2c_client *client = g0->client; u8 reg = offset; struct i2c_msg msg[] = { @@ -357,6 +357,15 @@ static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val, return 0; } +/* UCSI ops */ +static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, + void *val, size_t len) +{ + struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); + + return ucsi_stm32g0_read_from_hw(g0, offset, val, len); +} + static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, const void *val, size_t len) { @@ -445,6 +454,7 @@ static int ucsi_stm32g0_register(struct ucsi *ucsi) { struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); struct i2c_client *client = g0->client; + __le16 version; int ret; /* Request alert interrupt */ @@ -455,7 +465,15 @@ static int ucsi_stm32g0_register(struct ucsi *ucsi) return ret; } - ret = ucsi_register(ucsi); + ret = ucsi_stm32g0_read_from_hw(g0, UCSI_VERSION, &version, + sizeof(version)); + if (ret) { + dev_err(g0->dev, "failed to read version number: %d\n", ret); + free_irq(client->irq, g0); + return ret; + } + + ret = ucsi_register(ucsi, le16_to_cpu(version)); if (ret) { dev_err_probe(g0->dev, ret, "ucsi_register failed\n"); free_irq(client->irq, g0); From patchwork Sun Feb 18 22:20:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Ehrhardt X-Patchwork-Id: 13562087 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 05471C48BC4 for ; Sun, 18 Feb 2024 22:31:35 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Tn+OxJblvNFbpJByhvQSPytYpH/CqcWTHdEoPQyPdZk=; b=kISQkvubFz692s QQU9OZayFO88y3jJn3aUIt7cQ6FKFVypimnu1vpeU6hi5zvJXy93JeNg1eR/zt2n01Svw8U3sdCUR 8690Tgz91sYeUsfzn5fSTNQJm+icDYNS+sASJkh3rJ/igJVOibQzRzvGGWESwoNpyx49y2EWInZnF 4+/w9MnLN8z3/t4mPM/CSaTDg5qKRlQSCtj1/yB7DNjwcnQ8WchzLXN3sy628fjFQndVoauMydbJG prtGpiZYRwBJuDuEe1IYaygjWVG5FVHhmUIDj++E6xZrgKsXLusk5QptOl6Bv+x2YnJ1s5psaIy3K PSiay2xWBRxTWGpVNv3A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgy-00000008YZt-42ZJ; Sun, 18 Feb 2024 22:31:24 +0000 Received: from cae.in-ulm.de ([217.10.14.231]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgv-00000008YXw-13QF for linux-arm-kernel@lists.infradead.org; Sun, 18 Feb 2024 22:31:23 +0000 Received: by cae.in-ulm.de (Postfix, from userid 1000) id 4BB4E140929; Sun, 18 Feb 2024 23:21:07 +0100 (CET) From: "Christian A. Ehrhardt" To: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Christian A. Ehrhardt" , Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , =?utf-8?q?Samuel_?= =?utf-8?q?=C4=8Cavoj?= , Hans de Goede , Neil Armstrong , Prashanth K , Dmitry Baryshkov , Saranya Gopal , Haotien Hsu , Andy Shevchenko , Utkarsh Patel , Bjorn Andersson , Luca Weiss , Min-Hua Chen , Rob Herring , Rajaram Regupathy , linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, Abhishek Pandit-Subedi Subject: [RFC PATCH 4/6] usb: typec: ucsi: Cache current CCI value in struct ucsi Date: Sun, 18 Feb 2024 23:20:37 +0100 Message-Id: <20240218222039.822040-5-lk@c--e.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240218222039.822040-1-lk@c--e.de> References: <20240218222039.822040-1-lk@c--e.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240218_143121_591130_5CBEC97B X-CRM114-Status: GOOD ( 27.27 ) 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 All notification handlers already read the CCI value. Reading it again from hardware in ucsi_execute_command() opens races and it is not clear that this really works. The zenbook quirk in ucsi_acpi.c is one case where a re-read causes problems. In the ACPI case it is not even necessary to sync the mailbox memory. According to the intel whitepaper the ACPI handler does this before it notifies the OS. Thus simply read the CCI value in the notification handler without any ACPI method calls. During reset of the PPM, it may be necessary to poll UCSI_CCI from hardware as the reset completion may not send a notification. Provide a new ->poll_cci API function that actively updates the CCI value from hardware for this case. While there take into account that the raw CCI value read from hardware is little endian and convert it to host byte order explicitly. Signed-off-by: Christian A. Ehrhardt --- drivers/usb/typec/ucsi/ucsi.c | 11 +++--- drivers/usb/typec/ucsi/ucsi.h | 5 +++ drivers/usb/typec/ucsi/ucsi_acpi.c | 53 ++++++++------------------- drivers/usb/typec/ucsi/ucsi_ccg.c | 38 +++++++------------ drivers/usb/typec/ucsi/ucsi_glink.c | 48 +++++++++++++++++++++--- drivers/usb/typec/ucsi/ucsi_stm32g0.c | 22 ++++++++++- 6 files changed, 101 insertions(+), 76 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index eb630447ba80..a4ae3d5966a0 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -140,10 +140,7 @@ static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd)); if (ret) return ret; - - ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); - if (ret) - return ret; + cci = READ_ONCE(ucsi->cci); if (cmd != UCSI_CANCEL && cci & UCSI_CCI_BUSY) return ucsi_exec_command(ucsi, UCSI_CANCEL); @@ -1037,9 +1034,10 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) goto out; } - ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ucsi->ops->poll_cci(ucsi); if (ret) goto out; + cci = READ_ONCE(ucsi->cci); /* If the PPM is still doing something else, reset it again. */ if (cci & ~UCSI_CCI_RESET_COMPLETE) { @@ -1550,7 +1548,8 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) { struct ucsi *ucsi; - if (!ops || !ops->read || !ops->sync_write || !ops->async_write) + if (!ops || !ops->poll_cci || !ops->read || !ops->sync_write || + !ops->async_write) return ERR_PTR(-EINVAL); ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL); diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 100e16b49814..55e5c5a09b32 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -55,6 +55,7 @@ struct dentry; /** * struct ucsi_operations - UCSI I/O operations + * @poll_cci: Update the cached CCI value from hardware. Required for reset. * @read: Read operation * @sync_write: Blocking write operation * @async_write: Non-blocking write operation @@ -65,6 +66,7 @@ struct dentry; * return immediately after sending the data to the PPM. */ struct ucsi_operations { + int (*poll_cci)(struct ucsi *ucsi); int (*read)(struct ucsi *ucsi, unsigned int offset, void *val, size_t val_len); int (*sync_write)(struct ucsi *ucsi, unsigned int offset, @@ -371,6 +373,9 @@ struct ucsi { /* The latest "Notification Enable" bits (SET_NOTIFICATION_ENABLE) */ u64 ntfy; + /* The current value of the CCI field. Synced by notifications. */ + u32 cci; + /* PPM communication flags */ unsigned long flags; #define EVENT_PENDING 0 diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/ucsi_acpi.c index 710ddfc03ed0..3aedfe78fb65 100644 --- a/drivers/usb/typec/ucsi/ucsi_acpi.c +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c @@ -24,7 +24,6 @@ struct ucsi_acpi { struct completion complete; unsigned long flags; guid_t guid; - u64 cmd; bool dell_quirk_probed; bool dell_quirk_active; }; @@ -45,8 +44,7 @@ static int ucsi_acpi_dsm(struct ucsi_acpi *ua, int func) return 0; } -static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_acpi_poll_cci(struct ucsi *ucsi) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); int ret; @@ -55,6 +53,16 @@ static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset, if (ret) return ret; + WRITE_ONCE(ucsi->cci, le32_to_cpu(*(__le32 *)(ua->base + UCSI_CCI))); + + return 0; +} + +static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset, + void *val, size_t val_len) +{ + struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); + memcpy(val, ua->base + offset, val_len); return 0; @@ -66,7 +74,6 @@ static int ucsi_acpi_async_write(struct ucsi *ucsi, unsigned int offset, struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); memcpy(ua->base + offset, val, val_len); - ua->cmd = *(u64 *)val; return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE); } @@ -100,34 +107,12 @@ static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset, } static const struct ucsi_operations ucsi_acpi_ops = { + .poll_cci = ucsi_acpi_poll_cci, .read = ucsi_acpi_read, .sync_write = ucsi_acpi_sync_write, .async_write = ucsi_acpi_async_write }; -static int -ucsi_zenbook_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t val_len) -{ - struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); - int ret; - - if (offset == UCSI_VERSION || UCSI_COMMAND(ua->cmd) == UCSI_PPM_RESET) { - ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ); - if (ret) - return ret; - } - - memcpy(val, ua->base + offset, val_len); - - return 0; -} - -static const struct ucsi_operations ucsi_zenbook_ops = { - .read = ucsi_zenbook_read, - .sync_write = ucsi_acpi_sync_write, - .async_write = ucsi_acpi_async_write -}; - /* * Some Dell laptops expect that an ACK command with the * UCSI_ACK_CONNECTOR_CHANGE bit set is followed by a (separate) @@ -177,19 +162,13 @@ ucsi_dell_sync_write(struct ucsi *ucsi, unsigned int offset, } static const struct ucsi_operations ucsi_dell_ops = { + .poll_cci = ucsi_acpi_poll_cci, .read = ucsi_acpi_read, .sync_write = ucsi_dell_sync_write, .async_write = ucsi_acpi_async_write }; static const struct dmi_system_id ucsi_acpi_quirks[] = { - { - .matches = { - DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."), - DMI_MATCH(DMI_PRODUCT_NAME, "ZenBook UX325UA_UM325UA"), - }, - .driver_data = (void *)&ucsi_zenbook_ops, - }, { .matches = { DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), @@ -203,11 +182,9 @@ static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data) { struct ucsi_acpi *ua = data; u32 cci; - int ret; - ret = ua->ucsi->ops->read(ua->ucsi, UCSI_CCI, &cci, sizeof(cci)); - if (ret) - return; + cci = le32_to_cpu(*(__le32 *)(ua->base + UCSI_CCI)); + WRITE_ONCE(ua->ucsi->cci, cci); if (UCSI_CCI_CONNECTOR(cci)) ucsi_connector_change(ua->ucsi, UCSI_CCI_CONNECTOR(cci)); diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index d47f5e31c98a..55d0fe5324f4 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -194,7 +194,6 @@ struct ucsi_ccg_altmode { #define CCGX_MESSAGE_IN_MAX 16 struct op_region { - __le32 cci; u8 message_in[CCGX_MESSAGE_IN_MAX]; }; @@ -338,7 +337,6 @@ static int ccg_op_region_update(struct ucsi_ccg *uc, u32 cci) } spin_lock(&uc->op_lock); - data->cci = cpu_to_le32(cci); if (UCSI_CCI_LENGTH(cci)) memcpy(&data->message_in, buf, size); spin_unlock(&uc->op_lock); @@ -556,32 +554,24 @@ static void ucsi_ccg_nvidia_altmode(struct ucsi_ccg *uc, } } +static int ucsi_ccg_poll_cci(struct ucsi *ucsi) +{ + return 0; +} + static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t val_len) { struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); - u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(offset); struct ucsi_capability *cap; struct ucsi_altmode *alt; - int ret = 0; - - if (offset == UCSI_CCI) { - spin_lock(&uc->op_lock); - memcpy(val, &(uc->op_data).cci, val_len); - spin_unlock(&uc->op_lock); - } else if (offset == UCSI_MESSAGE_IN) { - spin_lock(&uc->op_lock); - memcpy(val, &(uc->op_data).message_in, val_len); - spin_unlock(&uc->op_lock); - } else { - ret = ccg_read(uc, reg, val, val_len); - } - - if (ret) - return ret; if (offset != UCSI_MESSAGE_IN) - return ret; + return -EINVAL; + + spin_lock(&uc->op_lock); + memcpy(val, &uc->op_data.message_in, val_len); + spin_unlock(&uc->op_lock); switch (UCSI_COMMAND(uc->last_cmd_sent)) { case UCSI_GET_CURRENT_CAM: @@ -607,7 +597,7 @@ static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, } uc->last_cmd_sent = 0; - return ret; + return 0; } static int ucsi_ccg_async_write(struct ucsi *ucsi, unsigned int offset, @@ -620,9 +610,7 @@ static int ucsi_ccg_async_write(struct ucsi *ucsi, unsigned int offset, * UCSI may read CCI instantly after async_write, * clear CCI to avoid caller getting wrong data before we get CCI from ISR */ - spin_lock(&uc->op_lock); - uc->op_data.cci = 0; - spin_unlock(&uc->op_lock); + WRITE_ONCE(ucsi->cci, 0); return ccg_write(uc, reg, val, val_len); } @@ -667,6 +655,7 @@ static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset, } static const struct ucsi_operations ucsi_ccg_ops = { + .poll_cci = ucsi_ccg_poll_cci, .read = ucsi_ccg_read, .sync_write = ucsi_ccg_sync_write, .async_write = ucsi_ccg_async_write, @@ -695,6 +684,7 @@ static irqreturn_t ccg_irq_handler(int irq, void *data) if (ret) goto err_clear_irq; cci = le32_to_cpu(__cci); + WRITE_ONCE(uc->ucsi->cci, cci); if (UCSI_CCI_CONNECTOR(cci)) ucsi_connector_change(uc->ucsi, UCSI_CCI_CONNECTOR(cci)); diff --git a/drivers/usb/typec/ucsi/ucsi_glink.c b/drivers/usb/typec/ucsi/ucsi_glink.c index 7a0b28536abd..d65ab81b4ed6 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -77,10 +77,8 @@ struct pmic_glink_ucsi { u8 read_buf[UCSI_BUF_SIZE]; }; -static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset, - void *val, size_t val_len) +static int pmic_glink_sync_read_buf(struct pmic_glink_ucsi *ucsi) { - struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); struct ucsi_read_buf_req_msg req = {}; unsigned long left; int ret; @@ -106,7 +104,6 @@ static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset, goto out_unlock; } - memcpy(val, &ucsi->read_buf[offset], val_len); ret = 0; out_unlock: @@ -115,6 +112,35 @@ static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset, return ret; } +static int pmic_glink_ucsi_poll_cci(struct ucsi *__ucsi) +{ + struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); + int ret; + + ret = pmic_glink_sync_read_buf(ucsi); + if (ret) + return ret; + + mutex_lock(&ucsi->lock); + WRITE_ONCE(__ucsi->cci, + le32_to_cpu(*(__le32 *)&ucsi->read_buf[UCSI_CCI])); + mutex_unlock(&ucsi->lock); + + return 0; +} + +static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset, + void *val, size_t val_len) +{ + struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); + + mutex_lock(&ucsi->lock); + memcpy(val, &ucsi->read_buf[offset], val_len); + mutex_unlock(&ucsi->lock); + + return 0; +} + static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned int offset, const void *val, size_t val_len) { @@ -187,6 +213,7 @@ static int pmic_glink_ucsi_sync_write(struct ucsi *__ucsi, unsigned int offset, } static const struct ucsi_operations pmic_glink_ucsi_ops = { + .poll_cci = pmic_glink_ucsi_poll_cci, .read = pmic_glink_ucsi_read, .sync_write = pmic_glink_ucsi_sync_write, .async_write = pmic_glink_ucsi_async_write @@ -221,11 +248,15 @@ static void pmic_glink_ucsi_notify(struct work_struct *work) u32 cci; int ret; - ret = pmic_glink_ucsi_read(ucsi->ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = pmic_glink_sync_read_buf(ucsi); if (ret) { - dev_err(ucsi->dev, "failed to read CCI on notification\n"); + dev_err(ucsi->dev, "failed to sync read buf\n"); return; } + mutex_lock(&ucsi->lock); + cci = le32_to_cpu(*(__le32 *)&ucsi->read_buf[UCSI_CCI]); + mutex_unlock(&ucsi->lock); + WRITE_ONCE(ucsi->ucsi->cci, cci); con_num = UCSI_CCI_CONNECTOR(cci); if (con_num) { @@ -258,6 +289,11 @@ static void pmic_glink_ucsi_register(struct work_struct *work) __le16 version; int ret; + ret = pmic_glink_sync_read_buf(ucsi); + if (ret < 0) { + dev_err(ucsi->dev, "cannot sync read buf: %d\n", ret); + return; + } ret = pmic_glink_ucsi_read(ucsi->ucsi, UCSI_VERSION, &version, sizeof(version)); if (ret < 0) { diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c index 112692c7a158..bab2363b73f4 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -358,6 +358,20 @@ static int ucsi_stm32g0_read_from_hw(struct ucsi_stm32g0 *g0, } /* UCSI ops */ +static int ucsi_stm32g0_poll_cci(struct ucsi *ucsi) +{ + struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); + __le32 cci; + int ret; + + ret = ucsi_stm32g0_read_from_hw(g0, UCSI_CCI, &cci, sizeof(cci)); + if (ret) + return ret; + WRITE_ONCE(ucsi->cci, le32_to_cpu(cci)); + + return 0; +} + static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t len) { @@ -424,15 +438,18 @@ static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset, const static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) { struct ucsi_stm32g0 *g0 = data; + __le32 __cci; u32 cci; int ret; if (g0->suspended) g0->wakeup_event = true; - ret = ucsi_stm32g0_read(g0->ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ucsi_stm32g0_read_from_hw(g0, UCSI_CCI, &__cci, sizeof(__cci)); if (ret) return IRQ_NONE; + cci = le32_to_cpu(__cci); + WRITE_ONCE(g0->ucsi->cci, cci); if (UCSI_CCI_CONNECTOR(cci)) ucsi_connector_change(g0->ucsi, UCSI_CCI_CONNECTOR(cci)); @@ -445,6 +462,7 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) } static const struct ucsi_operations ucsi_stm32g0_ops = { + .poll_cci = ucsi_stm32g0_poll_cci, .read = ucsi_stm32g0_read, .sync_write = ucsi_stm32g0_sync_write, .async_write = ucsi_stm32g0_async_write, @@ -626,7 +644,7 @@ static int ucsi_stm32g0_probe_bootloader(struct ucsi *ucsi) * Try to guess if the STM32G0 is running a UCSI firmware. First probe the UCSI FW at its * i2c address. Fallback to bootloader i2c address only if firmware-name is specified. */ - ret = ucsi_stm32g0_read(ucsi, UCSI_VERSION, &ucsi_version, sizeof(ucsi_version)); + ret = ucsi_stm32g0_read_from_hw(g0, UCSI_VERSION, &ucsi_version, sizeof(ucsi_version)); if (!ret || !g0->fw_name) return ret; From patchwork Sun Feb 18 22:20:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Ehrhardt X-Patchwork-Id: 13562088 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 0669AC48BF6 for ; Sun, 18 Feb 2024 22:31:36 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=IxO9dRqzZlsUDQxdOhpJXSp10rk/PA1+mGIN/fULdCE=; b=ztRIccWn0JVUts 2VYfgfVhw8GIIXT2j764BYAZG5uvt7zQFlj3Yzt6ptmWgQ1shY+YWZKTFUV75ylqTsrJ54JoGkdUA a1t/Yj7iWtliK6Qgs6Bm5LBlTFv4jknLXVIW6TDcNBw2oQ/s848ehizZ4NHrhX6E/0WgmYNMHUN9J jKulxPj+P7PJtEgp3F/N9psqlbMwrZrgY21oNBXttly2p/8r8MM/ObKkfstQTManiGvCFoDt9en7G 5KckWTA8xCfseEsJ9Pp+B9FlEg/mtZb/D7N3ATo6KzceKAqwJEPVf30V0Kr8kaJEVh2k8d3nyX4D+ QuUfHaQvOC4AZJd8zYEg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgx-00000008YZR-3zRR; Sun, 18 Feb 2024 22:31:23 +0000 Received: from cae.in-ulm.de ([217.10.14.231]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgv-00000008YXv-13am for linux-arm-kernel@lists.infradead.org; Sun, 18 Feb 2024 22:31:22 +0000 Received: by cae.in-ulm.de (Postfix, from userid 1000) id 0456414092E; Sun, 18 Feb 2024 23:21:07 +0100 (CET) From: "Christian A. Ehrhardt" To: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Christian A. Ehrhardt" , Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , =?utf-8?q?Samuel_?= =?utf-8?q?=C4=8Cavoj?= , Hans de Goede , Neil Armstrong , Prashanth K , Dmitry Baryshkov , Saranya Gopal , Haotien Hsu , Andy Shevchenko , Utkarsh Patel , Bjorn Andersson , Luca Weiss , Min-Hua Chen , Rob Herring , Rajaram Regupathy , linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, Abhishek Pandit-Subedi Subject: [RFC PATCH 5/6] usb: typec: ucsi: Introdcue ->read_data and ->write_data Date: Sun, 18 Feb 2024 23:20:38 +0100 Message-Id: <20240218222039.822040-6-lk@c--e.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240218222039.822040-1-lk@c--e.de> References: <20240218222039.822040-1-lk@c--e.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240218_143121_460587_287346D0 X-CRM114-Status: GOOD ( 16.99 ) 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 With the previous refactoring ->read() is only used to read from UCSI_MESSAGE_IN. So rename ->read() to ->read_data(). Before this change there is (at least for some backends) no way to write to UCSI_MESSAGE_OUT without starting a command. As a result it is not possible to send a command with additional data on these backends. Currently, the UCSI core does not send such commands but they are defined in the spec and presumably will be required. Introduce ->write_data() to write to UCSI_MESSAGE_OUT without starting a command. This is symmetrical to ->read_data(). Signed-off-by: Christian A. Ehrhardt --- drivers/usb/typec/ucsi/ucsi.c | 6 ++--- drivers/usb/typec/ucsi/ucsi.h | 7 ++--- drivers/usb/typec/ucsi/ucsi_acpi.c | 21 +++++++++++---- drivers/usb/typec/ucsi/ucsi_ccg.c | 18 ++++++++----- drivers/usb/typec/ucsi/ucsi_glink.c | 37 +++++++++++++++++---------- drivers/usb/typec/ucsi/ucsi_stm32g0.c | 30 +++++++++++++++++----- 6 files changed, 82 insertions(+), 37 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index a4ae3d5966a0..df3fe04cb9cd 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -46,7 +46,7 @@ static int ucsi_read_message_in(struct ucsi *ucsi, void *buf, if (ucsi->version <= UCSI_VERSION_1_2) buf_size = clamp(buf_size, 0, 16); - return ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, buf, buf_size); + return ucsi->ops->read_data(ucsi, buf, buf_size); } static int ucsi_acknowledge_command(struct ucsi *ucsi) @@ -1548,8 +1548,8 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) { struct ucsi *ucsi; - if (!ops || !ops->poll_cci || !ops->read || !ops->sync_write || - !ops->async_write) + if (!ops || !ops->poll_cci || !ops->read_data || !ops->write_data || + !ops->sync_write || !ops->async_write) return ERR_PTR(-EINVAL); ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL); diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 55e5c5a09b32..2ad68124511b 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -56,7 +56,8 @@ struct dentry; /** * struct ucsi_operations - UCSI I/O operations * @poll_cci: Update the cached CCI value from hardware. Required for reset. - * @read: Read operation + * @read_data: Read MESSAGE_IN data + * @write_data: Write MESSAGE_OUT data * @sync_write: Blocking write operation * @async_write: Non-blocking write operation * @update_altmodes: Squashes duplicate DP altmodes @@ -67,8 +68,8 @@ struct dentry; */ struct ucsi_operations { int (*poll_cci)(struct ucsi *ucsi); - int (*read)(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len); + int (*read_data)(struct ucsi *ucsi, void *val, size_t val_len); + int (*write_data)(struct ucsi *ucsi, const void *val, size_t val_len); int (*sync_write)(struct ucsi *ucsi, unsigned int offset, const void *val, size_t val_len); int (*async_write)(struct ucsi *ucsi, unsigned int offset, diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/ucsi_acpi.c index 3aedfe78fb65..79b47b433e35 100644 --- a/drivers/usb/typec/ucsi/ucsi_acpi.c +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c @@ -58,12 +58,21 @@ static int ucsi_acpi_poll_cci(struct ucsi *ucsi) return 0; } -static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_acpi_read_data(struct ucsi *ucsi, void *val, size_t val_len) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); - memcpy(val, ua->base + offset, val_len); + memcpy(val, ua->base + UCSI_MESSAGE_IN, val_len); + + return 0; +} + +static int ucsi_acpi_write_data(struct ucsi *ucsi, const void *val, + size_t val_len) +{ + struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); + + memcpy(ua->base + UCSI_MESSAGE_OUT, val, val_len); return 0; } @@ -108,7 +117,8 @@ static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset, static const struct ucsi_operations ucsi_acpi_ops = { .poll_cci = ucsi_acpi_poll_cci, - .read = ucsi_acpi_read, + .read_data = ucsi_acpi_read_data, + .write_data = ucsi_acpi_write_data, .sync_write = ucsi_acpi_sync_write, .async_write = ucsi_acpi_async_write }; @@ -163,7 +173,8 @@ ucsi_dell_sync_write(struct ucsi *ucsi, unsigned int offset, static const struct ucsi_operations ucsi_dell_ops = { .poll_cci = ucsi_acpi_poll_cci, - .read = ucsi_acpi_read, + .read_data = ucsi_acpi_read_data, + .write_data = ucsi_acpi_write_data, .sync_write = ucsi_dell_sync_write, .async_write = ucsi_acpi_async_write }; diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index 55d0fe5324f4..d6026f61a0ed 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -559,16 +559,12 @@ static int ucsi_ccg_poll_cci(struct ucsi *ucsi) return 0; } -static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_ccg_read_data(struct ucsi *ucsi, void *val, size_t val_len) { struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); struct ucsi_capability *cap; struct ucsi_altmode *alt; - if (offset != UCSI_MESSAGE_IN) - return -EINVAL; - spin_lock(&uc->op_lock); memcpy(val, &uc->op_data.message_in, val_len); spin_unlock(&uc->op_lock); @@ -600,6 +596,15 @@ static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, return 0; } +static int ucsi_ccg_write_data(struct ucsi *ucsi, const void *val, + size_t val_len) +{ + struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); + u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_MESSAGE_OUT); + + return ccg_write(uc, reg, val, val_len); +} + static int ucsi_ccg_async_write(struct ucsi *ucsi, unsigned int offset, const void *val, size_t val_len) { @@ -656,7 +661,8 @@ static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset, static const struct ucsi_operations ucsi_ccg_ops = { .poll_cci = ucsi_ccg_poll_cci, - .read = ucsi_ccg_read, + .read_data = ucsi_ccg_read_data, + .write_data = ucsi_ccg_write_data, .sync_write = ucsi_ccg_sync_write, .async_write = ucsi_ccg_async_write, .update_altmodes = ucsi_ccg_update_altmodes diff --git a/drivers/usb/typec/ucsi/ucsi_glink.c b/drivers/usb/typec/ucsi/ucsi_glink.c index d65ab81b4ed6..9dab1b428ad9 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -75,6 +75,7 @@ struct pmic_glink_ucsi { struct work_struct register_work; u8 read_buf[UCSI_BUF_SIZE]; + u8 write_buf[UCSI_BUF_SIZE]; }; static int pmic_glink_sync_read_buf(struct pmic_glink_ucsi *ucsi) @@ -129,18 +130,28 @@ static int pmic_glink_ucsi_poll_cci(struct ucsi *__ucsi) return 0; } -static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset, - void *val, size_t val_len) +static int pmic_glink_ucsi_read_data(struct ucsi *__ucsi, + void *val, size_t val_len) { struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); mutex_lock(&ucsi->lock); - memcpy(val, &ucsi->read_buf[offset], val_len); + memcpy(val, &ucsi->read_buf[UCSI_MESSAGE_IN], val_len); mutex_unlock(&ucsi->lock); return 0; } +static int pmic_glink_ucsi_write_data(struct ucsi *__ucsi, + const void *val, size_t val_len) +{ + struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); + + memcpy(&ucsi->write_buf[UCSI_MESSAGE_OUT], val, val_len); + + return 0; +} + static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned int offset, const void *val, size_t val_len) { @@ -148,10 +159,15 @@ static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned i unsigned long left; int ret; + if (offset != UCSI_CONTROL || val_len != sizeof(u64)) + return -EINVAL; + memcpy(ucsi->write_buf + UCSI_CONTROL, val, val_len); + req.hdr.owner = cpu_to_le32(PMIC_GLINK_OWNER_USBC); req.hdr.type = cpu_to_le32(MSG_TYPE_REQ_RESP); req.hdr.opcode = cpu_to_le32(UC_UCSI_WRITE_BUF_REQ); - memcpy(&req.buf[offset], val, val_len); + memcpy(req.buf, ucsi->write_buf, UCSI_BUF_SIZE); + memset(ucsi->write_buf, 0, sizeof(ucsi->write_buf)); reinit_completion(&ucsi->write_ack); @@ -214,7 +230,8 @@ static int pmic_glink_ucsi_sync_write(struct ucsi *__ucsi, unsigned int offset, static const struct ucsi_operations pmic_glink_ucsi_ops = { .poll_cci = pmic_glink_ucsi_poll_cci, - .read = pmic_glink_ucsi_read, + .read_data = pmic_glink_ucsi_read_data, + .write_data = pmic_glink_ucsi_write_data, .sync_write = pmic_glink_ucsi_sync_write, .async_write = pmic_glink_ucsi_async_write }; @@ -286,7 +303,6 @@ static void pmic_glink_ucsi_notify(struct work_struct *work) static void pmic_glink_ucsi_register(struct work_struct *work) { struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, register_work); - __le16 version; int ret; ret = pmic_glink_sync_read_buf(ucsi); @@ -294,14 +310,9 @@ static void pmic_glink_ucsi_register(struct work_struct *work) dev_err(ucsi->dev, "cannot sync read buf: %d\n", ret); return; } - ret = pmic_glink_ucsi_read(ucsi->ucsi, UCSI_VERSION, &version, - sizeof(version)); - if (ret < 0) { - dev_err(ucsi->dev, "cannot read version: %d\n", ret); - return; - } - ucsi_register(ucsi->ucsi, le16_to_cpu(version)); + ucsi_register(ucsi->ucsi, + le16_to_cpu(*(__le16 *)(ucsi->read_buf + UCSI_VERSION))); } static void pmic_glink_ucsi_callback(const void *data, size_t len, void *priv) diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c index bab2363b73f4..d68aca118e41 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -372,18 +372,17 @@ static int ucsi_stm32g0_poll_cci(struct ucsi *ucsi) return 0; } -static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t len) +static int ucsi_stm32g0_read_data(struct ucsi *ucsi, void *val, size_t len) { struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); - return ucsi_stm32g0_read_from_hw(g0, offset, val, len); + return ucsi_stm32g0_read_from_hw(g0, UCSI_MESSAGE_IN, val, len); } -static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, const void *val, - size_t len) +static int ucsi_stm32g0_write_to_hw(struct ucsi_stm32g0 *g0, + unsigned int offset, + const void *val, size_t len) { - struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); struct i2c_client *client = g0->client; struct i2c_msg msg[] = { { @@ -414,6 +413,22 @@ static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, cons return 0; } +static int ucsi_stm32g0_write_data(struct ucsi *ucsi, + const void *val, size_t len) +{ + struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); + + return ucsi_stm32g0_write_to_hw(g0, UCSI_MESSAGE_OUT, val, len); +} + +static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, + const void *val, size_t len) +{ + struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); + + return ucsi_stm32g0_write_to_hw(g0, offset, val, len); +} + static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset, const void *val, size_t len) { @@ -463,7 +478,8 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) static const struct ucsi_operations ucsi_stm32g0_ops = { .poll_cci = ucsi_stm32g0_poll_cci, - .read = ucsi_stm32g0_read, + .read_data = ucsi_stm32g0_read_data, + .write_data = ucsi_stm32g0_write_data, .sync_write = ucsi_stm32g0_sync_write, .async_write = ucsi_stm32g0_async_write, }; From patchwork Sun Feb 18 22:20:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Ehrhardt X-Patchwork-Id: 13562084 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 81858C48BF6 for ; Sun, 18 Feb 2024 22:31:10 +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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=idl7UuptsLLfTR+LG8tibDjNPRU3CEpxeojvoa/Hv6A=; b=3S5p8NmxruMG9D GFEAeidhwWvw4qkPJ+c4gDV9na09IlOixGGXbMjsyFzk1AcU9xd6yfeJZ47DDpXaPQsqSsxvCgO21 FdOJ0ik79ZHRBvKJIawusKSI7YZ0XA78M9xX2X7VwM1oDYSrh1rRqhyueBdTYCHEud9C1cMdwf34g sXcfuWO/axU2xajzJ3fBogj1/jYG8u/KeGbHmij+QfGOkqYEYFOIPF5x83bJS0cYm/f9Ct0ZhZ6Im o1qcV+CZij1EGh2k3hV7FxEk9XmzUseBI9ESwqydCewcVNiV1qWv/5h0bBDQxzZyDi10BvzJ1XPKr qTBhoqtWYvS/lm2hiuMg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgW-00000008YRo-28JA; Sun, 18 Feb 2024 22:30:56 +0000 Received: from cae.in-ulm.de ([217.10.14.231]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rbpgT-00000008YQM-027g for linux-arm-kernel@lists.infradead.org; Sun, 18 Feb 2024 22:30:55 +0000 Received: by cae.in-ulm.de (Postfix, from userid 1000) id B2FCA140938; Sun, 18 Feb 2024 23:21:09 +0100 (CET) From: "Christian A. Ehrhardt" To: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Christian A. Ehrhardt" , Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , =?utf-8?q?Samuel_?= =?utf-8?q?=C4=8Cavoj?= , Hans de Goede , Neil Armstrong , Prashanth K , Dmitry Baryshkov , Saranya Gopal , Haotien Hsu , Andy Shevchenko , Utkarsh Patel , Bjorn Andersson , Luca Weiss , Min-Hua Chen , Rob Herring , Rajaram Regupathy , linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, Abhishek Pandit-Subedi Subject: [RFC PATCH 6/6] usb: typec: ucsi: Convert a?sync_write to a?sync_cmd Date: Sun, 18 Feb 2024 23:20:39 +0100 Message-Id: <20240218222039.822040-7-lk@c--e.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240218222039.822040-1-lk@c--e.de> References: <20240218222039.822040-1-lk@c--e.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240218_143053_347806_62715AF5 X-CRM114-Status: GOOD ( 22.28 ) 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 The ->sync_write an ->async_write callbacks are only used to write to the UCSI_CONTROL field and start a command, now. Rename them accordingly and remove parameters and code that are no longer used. While there note that the command passed in from the UCSI core is in host byte order but the command sent to UCSI must be in little endian byte order. Signed-off-by: Christian A. Ehrhardt --- drivers/usb/typec/ucsi/ucsi.c | 29 ++++++------------- drivers/usb/typec/ucsi/ucsi.h | 14 ++++----- drivers/usb/typec/ucsi/ucsi_acpi.c | 41 ++++++++++++--------------- drivers/usb/typec/ucsi/ucsi_ccg.c | 35 +++++++++++------------ drivers/usb/typec/ucsi/ucsi_glink.c | 18 ++++++------ drivers/usb/typec/ucsi/ucsi_stm32g0.c | 16 +++++------ 6 files changed, 67 insertions(+), 86 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index df3fe04cb9cd..5bec98381be6 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -51,22 +51,16 @@ static int ucsi_read_message_in(struct ucsi *ucsi, void *buf, static int ucsi_acknowledge_command(struct ucsi *ucsi) { - u64 ctrl; + u64 cmd = UCSI_ACK_CC_CI | UCSI_ACK_COMMAND_COMPLETE; - ctrl = UCSI_ACK_CC_CI; - ctrl |= UCSI_ACK_COMMAND_COMPLETE; - - return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl)); + return ucsi->ops->sync_cmd(ucsi, cmd); } static int ucsi_acknowledge_connector_change(struct ucsi *ucsi) { - u64 ctrl; - - ctrl = UCSI_ACK_CC_CI; - ctrl |= UCSI_ACK_CONNECTOR_CHANGE; + u64 cmd = UCSI_ACK_CC_CI | UCSI_ACK_CONNECTOR_CHANGE; - return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl)); + return ucsi->ops->sync_cmd(ucsi, cmd); } static int ucsi_exec_command(struct ucsi *ucsi, u64 command); @@ -137,7 +131,7 @@ static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) u32 cci; int ret; - ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd)); + ret = ucsi->ops->sync_cmd(ucsi, cmd); if (ret) return ret; cci = READ_ONCE(ucsi->cci); @@ -1014,15 +1008,13 @@ static int ucsi_reset_connector(struct ucsi_connector *con, bool hard) static int ucsi_reset_ppm(struct ucsi *ucsi) { - u64 command = UCSI_PPM_RESET; unsigned long tmo; u32 cci; int ret; mutex_lock(&ucsi->ppm_lock); - ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command, - sizeof(command)); + ret = ucsi->ops->async_cmd(ucsi, UCSI_PPM_RESET); if (ret < 0) goto out; @@ -1041,9 +1033,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) /* If the PPM is still doing something else, reset it again. */ if (cci & ~UCSI_CCI_RESET_COMPLETE) { - ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, - &command, - sizeof(command)); + ret = ucsi->ops->async_cmd(ucsi, UCSI_PPM_RESET); if (ret < 0) goto out; } @@ -1549,7 +1539,7 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) struct ucsi *ucsi; if (!ops || !ops->poll_cci || !ops->read_data || !ops->write_data || - !ops->sync_write || !ops->async_write) + !ops->sync_cmd || !ops->async_cmd) return ERR_PTR(-EINVAL); ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL); @@ -1612,7 +1602,6 @@ EXPORT_SYMBOL_GPL(ucsi_register); */ void ucsi_unregister(struct ucsi *ucsi) { - u64 cmd = UCSI_SET_NOTIFICATION_ENABLE; int i; /* Make sure that we are not in the middle of driver initialization */ @@ -1620,7 +1609,7 @@ void ucsi_unregister(struct ucsi *ucsi) cancel_work_sync(&ucsi->resume_work); /* Disable notifications */ - ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd)); + ucsi->ops->async_cmd(ucsi, UCSI_SET_NOTIFICATION_ENABLE); if (!ucsi->connector) return; diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 2ad68124511b..3cce83d1e70c 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -58,22 +58,20 @@ struct dentry; * @poll_cci: Update the cached CCI value from hardware. Required for reset. * @read_data: Read MESSAGE_IN data * @write_data: Write MESSAGE_OUT data - * @sync_write: Blocking write operation - * @async_write: Non-blocking write operation + * @sync_cmd: Blocking command execution + * @async_cmd: Non-blocking command execution * @update_altmodes: Squashes duplicate DP altmodes * - * Read and write routines for UCSI interface. @sync_write must wait for the - * Command Completion Event from the PPM before returning, and @async_write must + * Read and write routines for UCSI interface. @sync_cmd must wait for the + * Command Completion Event from the PPM before returning, and @async_cmd must * return immediately after sending the data to the PPM. */ struct ucsi_operations { int (*poll_cci)(struct ucsi *ucsi); int (*read_data)(struct ucsi *ucsi, void *val, size_t val_len); int (*write_data)(struct ucsi *ucsi, const void *val, size_t val_len); - int (*sync_write)(struct ucsi *ucsi, unsigned int offset, - const void *val, size_t val_len); - int (*async_write)(struct ucsi *ucsi, unsigned int offset, - const void *val, size_t val_len); + int (*sync_cmd)(struct ucsi *ucsi, u64 cmd); + int (*async_cmd)(struct ucsi *ucsi, u64 cmd); bool (*update_altmodes)(struct ucsi *ucsi, struct ucsi_altmode *orig, struct ucsi_altmode *updated); }; diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/ucsi_acpi.c index 79b47b433e35..e6f67aa102d2 100644 --- a/drivers/usb/typec/ucsi/ucsi_acpi.c +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c @@ -77,21 +77,20 @@ static int ucsi_acpi_write_data(struct ucsi *ucsi, const void *val, return 0; } -static int ucsi_acpi_async_write(struct ucsi *ucsi, unsigned int offset, - const void *val, size_t val_len) +static int ucsi_acpi_async_cmd(struct ucsi *ucsi, u64 cmd) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); + __le64 __cmd = cpu_to_le64(cmd); - memcpy(ua->base + offset, val, val_len); + memcpy(ua->base + UCSI_CONTROL, &__cmd, sizeof(__cmd)); return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE); } -static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset, - const void *val, size_t val_len) +static int ucsi_acpi_sync_cmd(struct ucsi *ucsi, u64 cmd) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); - bool ack = UCSI_COMMAND(*(u64 *)val) == UCSI_ACK_CC_CI; + bool ack = UCSI_COMMAND(cmd) == UCSI_ACK_CC_CI; int ret; if (ack) @@ -99,7 +98,7 @@ static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset, else set_bit(COMMAND_PENDING, &ua->flags); - ret = ucsi_acpi_async_write(ucsi, offset, val, val_len); + ret = ucsi_acpi_async_cmd(ucsi, cmd); if (ret) goto out_clear_bit; @@ -119,8 +118,8 @@ static const struct ucsi_operations ucsi_acpi_ops = { .poll_cci = ucsi_acpi_poll_cci, .read_data = ucsi_acpi_read_data, .write_data = ucsi_acpi_write_data, - .sync_write = ucsi_acpi_sync_write, - .async_write = ucsi_acpi_async_write + .sync_cmd = ucsi_acpi_sync_cmd, + .async_cmd = ucsi_acpi_async_cmd }; /* @@ -131,32 +130,28 @@ static const struct ucsi_operations ucsi_acpi_ops = { * subsequent commands will timeout. */ static int -ucsi_dell_sync_write(struct ucsi *ucsi, unsigned int offset, - const void *val, size_t val_len) +ucsi_dell_sync_cmd(struct ucsi *ucsi, u64 cmd) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); - u64 cmd = *(u64 *)val, ack = 0; + u64 ack = 0; int ret; if (UCSI_COMMAND(cmd) == UCSI_ACK_CC_CI && cmd & UCSI_ACK_CONNECTOR_CHANGE) ack = UCSI_ACK_CC_CI | UCSI_ACK_COMMAND_COMPLETE; - ret = ucsi_acpi_sync_write(ucsi, offset, val, val_len); + ret = ucsi_acpi_sync_cmd(ucsi, cmd); if (ret != 0) return ret; if (ack == 0) - return ret; + return 0; if (!ua->dell_quirk_probed) { ua->dell_quirk_probed = true; - cmd = UCSI_GET_CAPABILITY; - ret = ucsi_acpi_sync_write(ucsi, UCSI_CONTROL, &cmd, - sizeof(cmd)); + ret = ucsi_acpi_sync_cmd(ucsi, UCSI_GET_CAPABILITY); if (ret == 0) - return ucsi_acpi_sync_write(ucsi, UCSI_CONTROL, - &ack, sizeof(ack)); + return ucsi_acpi_sync_cmd(ucsi, ack); if (ret != -ETIMEDOUT) return ret; @@ -166,17 +161,17 @@ ucsi_dell_sync_write(struct ucsi *ucsi, unsigned int offset, } if (!ua->dell_quirk_active) - return ret; + return 0; - return ucsi_acpi_sync_write(ucsi, UCSI_CONTROL, &ack, sizeof(ack)); + return ucsi_acpi_sync_cmd(ucsi, ack); } static const struct ucsi_operations ucsi_dell_ops = { .poll_cci = ucsi_acpi_poll_cci, .read_data = ucsi_acpi_read_data, .write_data = ucsi_acpi_write_data, - .sync_write = ucsi_dell_sync_write, - .async_write = ucsi_acpi_async_write + .sync_cmd = ucsi_dell_sync_cmd, + .async_cmd = ucsi_acpi_async_cmd }; static const struct dmi_system_id ucsi_acpi_quirks[] = { diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index d6026f61a0ed..5c60816e608f 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -605,23 +605,22 @@ static int ucsi_ccg_write_data(struct ucsi *ucsi, const void *val, return ccg_write(uc, reg, val, val_len); } -static int ucsi_ccg_async_write(struct ucsi *ucsi, unsigned int offset, - const void *val, size_t val_len) +static int ucsi_ccg_async_cmd(struct ucsi *ucsi, u64 cmd) { struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); - u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(offset); + u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_CONTROL); + __le64 __cmd = cpu_to_le64(cmd); /* - * UCSI may read CCI instantly after async_write, + * UCSI may read CCI instantly after async_cmd, * clear CCI to avoid caller getting wrong data before we get CCI from ISR */ WRITE_ONCE(ucsi->cci, 0); - return ccg_write(uc, reg, val, val_len); + return ccg_write(uc, reg, (u8 *)&__cmd, sizeof(__cmd)); } -static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset, - const void *val, size_t val_len) +static int ucsi_ccg_sync_cmd(struct ucsi *ucsi, u64 cmd) { struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); struct ucsi_connector *con; @@ -632,19 +631,17 @@ static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset, pm_runtime_get_sync(uc->dev); set_bit(DEV_CMD_PENDING, &uc->flags); - if (offset == UCSI_CONTROL && val_len == sizeof(uc->last_cmd_sent)) { - uc->last_cmd_sent = *(u64 *)val; + uc->last_cmd_sent = cmd; - if (UCSI_COMMAND(uc->last_cmd_sent) == UCSI_SET_NEW_CAM && - uc->has_multiple_dp) { - con_index = (uc->last_cmd_sent >> 16) & - UCSI_CMD_CONNECTOR_MASK; - con = &uc->ucsi->connector[con_index - 1]; - ucsi_ccg_update_set_new_cam_cmd(uc, con, (u64 *)val); - } + if (UCSI_COMMAND(uc->last_cmd_sent) == UCSI_SET_NEW_CAM && + uc->has_multiple_dp) { + con_index = (uc->last_cmd_sent >> 16) & + UCSI_CMD_CONNECTOR_MASK; + con = &uc->ucsi->connector[con_index - 1]; + ucsi_ccg_update_set_new_cam_cmd(uc, con, &cmd); } - ret = ucsi_ccg_async_write(ucsi, offset, val, val_len); + ret = ucsi_ccg_async_cmd(ucsi, cmd); if (ret) goto err_clear_bit; @@ -663,8 +660,8 @@ static const struct ucsi_operations ucsi_ccg_ops = { .poll_cci = ucsi_ccg_poll_cci, .read_data = ucsi_ccg_read_data, .write_data = ucsi_ccg_write_data, - .sync_write = ucsi_ccg_sync_write, - .async_write = ucsi_ccg_async_write, + .sync_cmd = ucsi_ccg_sync_cmd, + .async_cmd = ucsi_ccg_async_cmd, .update_altmodes = ucsi_ccg_update_altmodes }; diff --git a/drivers/usb/typec/ucsi/ucsi_glink.c b/drivers/usb/typec/ucsi/ucsi_glink.c index 9dab1b428ad9..1535877a9a41 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -186,23 +186,24 @@ static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned i return 0; } -static int pmic_glink_ucsi_async_write(struct ucsi *__ucsi, unsigned int offset, - const void *val, size_t val_len) +static int pmic_glink_ucsi_async_cmd(struct ucsi *__ucsi, u64 cmd) { struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); + __le64 __cmd = cpu_to_le64(cmd); int ret; mutex_lock(&ucsi->lock); - ret = pmic_glink_ucsi_locked_write(ucsi, offset, val, val_len); + ret = pmic_glink_ucsi_locked_write(ucsi, UCSI_CONTROL, + &__cmd, sizeof(__cmd)); mutex_unlock(&ucsi->lock); return ret; } -static int pmic_glink_ucsi_sync_write(struct ucsi *__ucsi, unsigned int offset, - const void *val, size_t val_len) +static int pmic_glink_ucsi_sync_cmd(struct ucsi *__ucsi, u64 cmd) { struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); + __le64 __cmd = cpu_to_le64(cmd); unsigned long left; int ret; @@ -212,7 +213,8 @@ static int pmic_glink_ucsi_sync_write(struct ucsi *__ucsi, unsigned int offset, ucsi->sync_val = 0; reinit_completion(&ucsi->sync_ack); ucsi->sync_pending = true; - ret = pmic_glink_ucsi_locked_write(ucsi, offset, val, val_len); + ret = pmic_glink_ucsi_locked_write(ucsi, UCSI_CONTROL, + &__cmd, sizeof(__cmd)); mutex_unlock(&ucsi->lock); left = wait_for_completion_timeout(&ucsi->sync_ack, 5 * HZ); @@ -232,8 +234,8 @@ static const struct ucsi_operations pmic_glink_ucsi_ops = { .poll_cci = pmic_glink_ucsi_poll_cci, .read_data = pmic_glink_ucsi_read_data, .write_data = pmic_glink_ucsi_write_data, - .sync_write = pmic_glink_ucsi_sync_write, - .async_write = pmic_glink_ucsi_async_write + .sync_cmd = pmic_glink_ucsi_sync_cmd, + .async_cmd = pmic_glink_ucsi_async_cmd }; static void pmic_glink_ucsi_read_ack(struct pmic_glink_ucsi *ucsi, const void *data, int len) diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c index d68aca118e41..0847e00163e8 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -421,23 +421,23 @@ static int ucsi_stm32g0_write_data(struct ucsi *ucsi, return ucsi_stm32g0_write_to_hw(g0, UCSI_MESSAGE_OUT, val, len); } -static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, - const void *val, size_t len) +static int ucsi_stm32g0_async_cmd(struct ucsi *ucsi, u64 cmd) { struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); + __le64 __cmd = cpu_to_le64(cmd); - return ucsi_stm32g0_write_to_hw(g0, offset, val, len); + return ucsi_stm32g0_write_to_hw(g0, UCSI_CONTROL, + &__cmd, sizeof(__cmd)); } -static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset, const void *val, - size_t len) +static int ucsi_stm32g0_sync_cmd(struct ucsi *ucsi, u64 cmd) { struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); int ret; set_bit(COMMAND_PENDING, &g0->flags); - ret = ucsi_stm32g0_async_write(ucsi, offset, val, len); + ret = ucsi_stm32g0_async_cmd(ucsi, cmd); if (ret) goto out_clear_bit; @@ -480,8 +480,8 @@ static const struct ucsi_operations ucsi_stm32g0_ops = { .poll_cci = ucsi_stm32g0_poll_cci, .read_data = ucsi_stm32g0_read_data, .write_data = ucsi_stm32g0_write_data, - .sync_write = ucsi_stm32g0_sync_write, - .async_write = ucsi_stm32g0_async_write, + .sync_cmd = ucsi_stm32g0_sync_cmd, + .async_cmd = ucsi_stm32g0_async_cmd, }; static int ucsi_stm32g0_register(struct ucsi *ucsi)