From patchwork Sun Jun 2 23:24:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 13683128 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 0DC1CC25B74 for ; Sun, 2 Jun 2024 23:25:15 +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=cjwNF4rLuioh0DTyOboyi+ZcUrbamG3gnck168L/hlU=; b=omqpfw3fvITt2E 722C+LmGc1UidUgzXgEZ9Ogxnc0n7UJrFa7pNOqFWypChVuf8cNPpFDQ3sz25d2A/Id9FtphMw6o7 XApZqxPmjBY0xYYMWWhTrN4NUrwiiaMhUg1OAM2pjrlo8lU7UNyQXjX5povWzgOXxeBkslY/v/FI0 MpaM1BYWKNEQz9QMFNxGy7xGkEb3LYHddkirL2kruTBgjUMYE7zAf6SfOZSEv1WK/NPJaN3bHAVnU 9cLkl8YPdu8bqT5pZwWjgwuZ3YPdtUd9swEJLl+YULm3RzjZ6S5vdzx93Mv7bRiamL1+9BAmNbEWY z7RZMXwQQWO30hxxl4TQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZS-0000000F64s-3WJA; Sun, 02 Jun 2024 23:25:02 +0000 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZQ-0000000F63F-02MW for linux-arm-kernel@lists.infradead.org; Sun, 02 Jun 2024 23:25:01 +0000 Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-52b93370ad0so1654606e87.2 for ; Sun, 02 Jun 2024 16:24:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717370698; x=1717975498; 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=/H9omkcKfSMWKhyxE8ouaq15vBQZtQgKk2aVI95FWUU=; b=XKvQtyFRuWfS5DN+eaMJD9aHV9dk2cLijHKvhu2kItqH1EhYVsNqPutURNONJpcCZI oGoVgQLSOZFLpPZ/VJ+AAeAYzeSc6o0jCfsMccqws7+SiMpxm/ECZLzMQCBRMMsk4HJA kcPvw9SX0GeQQrMTNgtevJtreYr1TpvZomYZYbrCvEwJdAmiiaxnsi0CeMqAqnKnytQP kh3w+3e8Zx2hkmYgoGXDpr3ft1yu052rC3UpoB/4MnVz7c9I5vFDI7hQVW2pT4HsNVjG o4e66QuvzhK5qy5OV5Jc2qser2VmXhS5IDhYO57tiHwthAxQBxm2IULxFxUVAmYE4tsM Rqxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717370698; x=1717975498; 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=/H9omkcKfSMWKhyxE8ouaq15vBQZtQgKk2aVI95FWUU=; b=d7oTOdaENkJFbGD4pjgQQLBGw7gOU6gaDhr/He7jGQLMjKnFbT7sSNXoGEjeDPisYl ND9uMHWcECGrHZGojNTebkGIgBhHXldcxzdiutGN/gmI6qv9NikqLvWiaKVq0U3VFFw0 LfGuxFMvvzWFAOrs+MhSo5g1qgP/YHS8AT21ZJPTrZ1sC8Q9Swk7Kwy3TJC7VB4WkPtn OHJB+endK9SWF+aed0LMXIMcH5rIk/212/s2LxUhRhP6V6GPPnzPQ2JmJYI6i50/0VQo 3xT6impNa58AHJ6GRJ1V7ScP66oP8AlK4Vn4emo5Ps8gxOsWZP9URsduOCBJt4fuWIkj pu4A== X-Forwarded-Encrypted: i=1; AJvYcCU6/s4ioYKlLsT7j9spyEdZ+Fs4iH9ba3lcK8uaO1qvjIDUgJukSDukkD5zyZAljixKl8ojbVfXFfo+3gfHpPGrjAi64qn1u5J26aXZewBUJi7itq4= X-Gm-Message-State: AOJu0YzuGvPHw1rbliLEHU3yyB2B+afP6w0kKoNToeuMegfj6Y1+Halb 3sIMfuuFjn/JWNRtkppZ5bpqxRfPQCzuHoLrq5hZbxtpxo4ctMX2S/ybpobloic= X-Google-Smtp-Source: AGHT+IE2CkuVfuEeWibh2syHVvCT2BYHUAnYl4Dc3HsXN2GwvEWq+MuVV84p30lXkZjv/MWuXo6EyQ== X-Received: by 2002:a19:2d14:0:b0:52b:9046:c44c with SMTP id 2adb3069b0e04-52b9046c59amr4014987e87.58.1717370697587; Sun, 02 Jun 2024 16:24:57 -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.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Jun 2024 16:24:57 -0700 (PDT) From: Dmitry Baryshkov Date: Mon, 03 Jun 2024 02:24:54 +0300 Subject: [PATCH RFC 1/7] usb: typec: ucsi: move ucsi_acknowledge() from ucsi_read_error() MIME-Version: 1.0 Message-Id: <20240603-ucsi-rework-interface-v1-1-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=1294; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=J3HQOCyfSVpr1YUtYQZItLe+bN2QWt7En48aq2hwcDQ=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmXP9H/f3ba1SNbSVuQnOo1uz7VMfWjPKCe7tix noDzdNH6tGJATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZlz/RwAKCRCLPIo+Aiko 1d7IB/9MbaRNYHyugOOeccBgAd/6xgYFC8GC5EoakGOk+XO8fa4kUcKuCnypn9hKPcv5XYKagzt bPXUTZq47XUYgE1J2OW32LsWJ8dMV6Cw2Ue2hziAmhcMgc89R453njTxTc3eKF40ARb3FTptROQ xs3VZx2ERK741nPOXeoCPU79RmeSG7uMAZT9UFFi2yHZrrTfXV2g/IgAAmuNd0jQLCUQvGyFOcc G5i9jn4wNWURcR3K/mq8HTwfFr/kw5JW8Jtpeu3j1UTNSiRxVZMEjOH57d8d6thIm5tobzMtC0P e0YbXHJ8z8bldV5HBiq8FkRuTzcIgMVoIFCmG4pGI6WE2/w8 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_162500_102356_E5C10F04 X-CRM114-Status: GOOD ( 11.56 ) 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 As a preparation for reworking UCSI command handling, move ucsi_acknowledge() for the failed command from ucsi_read_error() to ucsi_exec_command(). Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 2cc7aedd490f..953196ca35a3 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -70,11 +70,6 @@ static int ucsi_read_error(struct ucsi *ucsi) u16 error; int ret; - /* Acknowledge the command that failed */ - ret = ucsi_acknowledge(ucsi, false); - if (ret) - return ret; - ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS); if (ret < 0) return ret; @@ -153,13 +148,14 @@ static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) } if (cci & UCSI_CCI_ERROR) { - if (cmd == UCSI_GET_ERROR_STATUS) { - ret = ucsi_acknowledge(ucsi, false); - if (ret) - return ret; + /* 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); } From patchwork Sun Jun 2 23:24:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 13683130 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 D0737C25B74 for ; Sun, 2 Jun 2024 23:25:17 +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=hQHQVryMQKanUPVaMLqPdLU67dwNKbJJAVp2n/lQf5g=; b=g3Duq5adpALcIy WcKeLcuZ16/zwJOLfakm1sZAt1VALIH7BTnDVVf/XEWvrPpjTmSXTT25j/wHuffSdLI9+yLqnur9X AnQ7EOCS0QzawuBB6rRtQK+ieouWmTDSOW3Xbq+7xcvrx0JDrfdbZAjV+vjsjov4NToRsxO8THHvF MfZbMQ9fx5FqK0+2bkK1NpYe/tgjsKz04XedjNldMuIykmXrJvEL/Sr5U9e/drE6LU+vRQa+DYahh XtYAVoHQCSSY86d8AKbnfBkfuzNEjhMWHt+ebXOR5jQvXJMpgHvS5lBO1YxsxzcUGBScN02YwRyIO OC78SveJfJpvwLU9PbuA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZW-0000000F66u-2Jvk; Sun, 02 Jun 2024 23:25:06 +0000 Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZR-0000000F63G-0Tss for linux-arm-kernel@lists.infradead.org; Sun, 02 Jun 2024 23:25:03 +0000 Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-52b8b638437so2870204e87.3 for ; Sun, 02 Jun 2024 16:24:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717370698; x=1717975498; 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=vehF8oKD1FhtE9feRp5zr7W3EX4UPLlYPHbRSRvEcqw=; b=Z0ALORCZeStSsbgrhMr492BX5tcZavKXqT2XjNiMi6TYgdX0UX7+s/iuIsjQhY0h8J 6SDWSwGPVQ+vRhmNtaIHQKTiM1yOFO1Uob7e9NhzuYVJ2nsjUGD7IH13vVcNcb1pwdZN mr96H8eQqcXZUkMy/tXj9E1/jcpWRKvBKEr1CZ8La6+gbFD3+I/bCKEsjeMsra7oYLCE yTYDrvqwoveqV6UPP1xtxP/d5anGdae+mP+nASwc1vquVk+GyeTZVAgttN5hS5mglZvb sHlb2eBe1ZBsH9A7Eg9QW2XWUpFPM5f1bx8RE7q8YYbmSp4JzGGoXTlPW3JBzq+kL+mn zVbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717370698; x=1717975498; 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=vehF8oKD1FhtE9feRp5zr7W3EX4UPLlYPHbRSRvEcqw=; b=GE/0d1KghGyJQ2z6ZZsmszRyY5YBQiBTRvrCu3fz2zv4qip0lBbRytODtsWnYD7Hd9 sD6pn8kjtql4CBqM0ypeqpYNGMEYHB0We4GByh1x3yzJI5eJKtAbMwaBNEVR9ziky0k3 0/rnaVujYjgJmwkJbxgPfq8JW91kAxfZ86I0hpltGR7BaG+2jEMKcANp78kk5NMGPQZl tswuFHS/cHhow/VgAxAa4exZTRP0vhq8UlGIcp2XnXlouk3vriOv8XjK0J6gC8+rbgJW uUaq15D+zyA9f7PdJQn+JFCA8yHOZFn+E7C8IQTEvxqPTzAz9zVrCbi/k98r7J9FJA6M OBaw== X-Forwarded-Encrypted: i=1; AJvYcCXvVIjGNug+SSizYIM0Ez51ajpsq5YWDR7HotxajLxStUfv8v2VUAFscIzDyfuRhbVtox40SkRs/PPU6raio+GLdLQSwLMZF4Ij+qojSWQp7KFXRZ8= X-Gm-Message-State: AOJu0YzJldglAzT9n+B0an+jwOfVBpiywyl84fF1zm1/IS8BnkUmzuI4 NVcN1a65poKB2xZJBOtV+4UX7AAOHPdZRcqMR/QLpmnshYDm/AAKJ2AMQDGWz0I= X-Google-Smtp-Source: AGHT+IHEYyxLjKGI4/aduNv7WoI4hAskzxqmMj+bh4agJWyuOSeELf3fC6jtRbYSOCpnJ8PVUxaizQ== X-Received: by 2002:ac2:5a4c:0:b0:523:294f:7fe6 with SMTP id 2adb3069b0e04-52b8970bf73mr4765319e87.44.1717370698240; Sun, 02 Jun 2024 16:24:58 -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.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Jun 2024 16:24:57 -0700 (PDT) From: Dmitry Baryshkov Date: Mon, 03 Jun 2024 02:24:55 +0300 Subject: [PATCH RFC 2/7] usb: typec: ucsi: simplify command sending API MIME-Version: 1.0 Message-Id: <20240603-ucsi-rework-interface-v1-2-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=14289; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=FBcTYsqPsFc93PcwEeFHfR5zjPtqmb2eWpdZiIr3dx8=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmXP9HpZmNsITXLReSiYqcvni6cvSi0MzwbZU9e a8wO2rGeHmJATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZlz/RwAKCRCLPIo+Aiko 1eiIB/9xyW6Xoy5tHiGAFg2QRyFUjGQZVSDU8ucyU9RN8Svw9k/JkCRTpWhJTXSg5VEW1Wgld3X 1Y6tu/FWZg29MKsZdhEHHwWI+LPQoMgso7QLU3cx2h1O03UglZmc189693E0mao6wv8i6mZo+sv 2ycbZCEYWA/xToPgXnvT/NzkraOvbtt2JOXT/704hoY6Dyke7JKB5spgHGL0gkM/LGFbQ0VSpcS G2daguTxfiIKsiDKGe+zm5za5HEbvItr9kAHmmRTtEEADA0mFb8gOcoRfsfYuMBBDIqZ3jgECN5 uJ+tNo6kDswVGrMAeQbFL6YmEjKmeIW3VI2DZkmPuicIy1Lf 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_162501_205896_C9C55227 X-CRM114-Status: GOOD ( 15.87 ) 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 and async_write are used only for writing UCSI commands to the UCSI_CONTROL offsets. Rename sync_write and async_write operations to sync_control and async_control accordingly. Drop the offset and length fields and pass u64 command instead. Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 18 +++++++----------- drivers/usb/typec/ucsi/ucsi.h | 10 ++++------ drivers/usb/typec/ucsi/ucsi_acpi.c | 22 ++++++++++------------ drivers/usb/typec/ucsi/ucsi_ccg.c | 34 +++++++++++++++------------------- drivers/usb/typec/ucsi/ucsi_glink.c | 14 ++++++-------- drivers/usb/typec/ucsi/ucsi_stm32g0.c | 24 +++++++++++------------- 6 files changed, 53 insertions(+), 69 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 953196ca35a3..f58908a76e1d 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -60,7 +60,7 @@ static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) ctrl |= UCSI_ACK_CONNECTOR_CHANGE; } - return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl)); + return ucsi->ops->sync_control(ucsi, ctrl); } static int ucsi_exec_command(struct ucsi *ucsi, u64 command); @@ -126,7 +126,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_control(ucsi, cmd); if (ret) return ret; @@ -1306,8 +1306,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) */ if (cci & UCSI_CCI_RESET_COMPLETE) { command = UCSI_SET_NOTIFICATION_ENABLE; - ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command, - sizeof(command)); + ret = ucsi->ops->async_control(ucsi, command); if (ret < 0) goto out; @@ -1328,8 +1327,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) } command = UCSI_PPM_RESET; - ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command, - sizeof(command)); + ret = ucsi->ops->async_control(ucsi, command); if (ret < 0) goto out; @@ -1350,9 +1348,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_control(ucsi, command); if (ret < 0) goto out; } @@ -1863,7 +1859,7 @@ 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->read || !ops->sync_control || !ops->async_control) return ERR_PTR(-EINVAL); ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL); @@ -1939,7 +1935,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_control(ucsi, cmd); if (!ucsi->connector) return; diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index c4d103db9d0f..94ea54d3d47d 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -57,8 +57,8 @@ struct dentry; /** * struct ucsi_operations - UCSI I/O operations * @read: Read operation - * @sync_write: Blocking write operation - * @async_write: Non-blocking write operation + * @sync_control: Blocking control operation + * @async_control: Non-blocking control operation * @update_altmodes: Squashes duplicate DP altmodes * @update_connector: Update connector capabilities before registering * @connector_status: Updates connector status, called holding connector lock @@ -70,10 +70,8 @@ struct dentry; struct ucsi_operations { int (*read)(struct ucsi *ucsi, unsigned int offset, 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_control)(struct ucsi *ucsi, u64 command); + int (*async_control)(struct ucsi *ucsi, u64 command); bool (*update_altmodes)(struct ucsi *ucsi, struct ucsi_altmode *orig, struct ucsi_altmode *updated); void (*update_connector)(struct ucsi_connector *con); diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/ucsi_acpi.c index 8d112c3edae5..feccbfc8acbe 100644 --- a/drivers/usb/typec/ucsi/ucsi_acpi.c +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c @@ -60,22 +60,20 @@ static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset, 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_control(struct ucsi *ucsi, u64 command) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); - memcpy(ua->base + offset, val, val_len); - ua->cmd = *(u64 *)val; + memcpy(ua->base + UCSI_CONTROL, &command, sizeof(command)); + ua->cmd = command; 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_control(struct ucsi *ucsi, u64 command) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); - bool ack = UCSI_COMMAND(*(u64 *)val) == UCSI_ACK_CC_CI; + bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI; int ret; if (ack) @@ -83,7 +81,7 @@ static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset, else set_bit(UCSI_ACPI_COMMAND_PENDING, &ua->flags); - ret = ucsi_acpi_async_write(ucsi, offset, val, val_len); + ret = ucsi_acpi_async_control(ucsi, command); if (ret) goto out_clear_bit; @@ -101,8 +99,8 @@ static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset, static const struct ucsi_operations ucsi_acpi_ops = { .read = ucsi_acpi_read, - .sync_write = ucsi_acpi_sync_write, - .async_write = ucsi_acpi_async_write + .sync_control = ucsi_acpi_sync_control, + .async_control = ucsi_acpi_async_control }; static int @@ -124,8 +122,8 @@ ucsi_zenbook_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t val_ static const struct ucsi_operations ucsi_zenbook_ops = { .read = ucsi_zenbook_read, - .sync_write = ucsi_acpi_sync_write, - .async_write = ucsi_acpi_async_write + .sync_control = ucsi_acpi_sync_control, + .async_control = ucsi_acpi_async_control }; 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 dda7c7c94e08..76b39bb9762d 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -610,25 +610,23 @@ static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, return ret; } -static int ucsi_ccg_async_write(struct ucsi *ucsi, unsigned int offset, - const void *val, size_t val_len) +static int ucsi_ccg_async_control(struct ucsi *ucsi, u64 command) { 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); /* - * UCSI may read CCI instantly after async_write, + * UCSI may read CCI instantly after async_control, * 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); - return ccg_write(uc, reg, val, val_len); + return ccg_write(uc, reg, (u8 *)&command, sizeof(command)); } -static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset, - const void *val, size_t val_len) +static int ucsi_ccg_sync_control(struct ucsi *ucsi, u64 command) { struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); struct ucsi_connector *con; @@ -639,19 +637,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 = command; - 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, &command); } - ret = ucsi_ccg_async_write(ucsi, offset, val, val_len); + ret = ucsi_ccg_async_control(ucsi, command); if (ret) goto err_clear_bit; @@ -668,8 +664,8 @@ static int ucsi_ccg_sync_write(struct ucsi *ucsi, unsigned int offset, static const struct ucsi_operations ucsi_ccg_ops = { .read = ucsi_ccg_read, - .sync_write = ucsi_ccg_sync_write, - .async_write = ucsi_ccg_async_write, + .sync_control = ucsi_ccg_sync_control, + .async_control = ucsi_ccg_async_control, .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 f7546bb488c3..047998f33274 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -143,21 +143,19 @@ 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_control(struct ucsi *__ucsi, u64 command) { struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); 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, &command, sizeof(command)); 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_control(struct ucsi *__ucsi, u64 command) { struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); unsigned long left; @@ -169,7 +167,7 @@ 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, &command, sizeof(command)); mutex_unlock(&ucsi->lock); left = wait_for_completion_timeout(&ucsi->sync_ack, 5 * HZ); @@ -217,8 +215,8 @@ static void pmic_glink_ucsi_connector_status(struct ucsi_connector *con) static const struct ucsi_operations pmic_glink_ucsi_ops = { .read = pmic_glink_ucsi_read, - .sync_write = pmic_glink_ucsi_sync_write, - .async_write = pmic_glink_ucsi_async_write, + .sync_control = pmic_glink_ucsi_sync_control, + .async_control = pmic_glink_ucsi_async_control, .update_connector = pmic_glink_ucsi_update_connector, .connector_status = pmic_glink_ucsi_connector_status, }; diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c index 9b2af32b7874..f97dc5a075e2 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -359,8 +359,7 @@ static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val, return 0; } -static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, const void *val, - size_t len) +static int ucsi_stm32g0_async_control(struct ucsi *ucsi, u64 command) { struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); struct i2c_client *client = g0->client; @@ -373,19 +372,19 @@ static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, cons unsigned char *buf; int ret; - buf = kmalloc(len + 1, GFP_KERNEL); + buf = kmalloc(sizeof(command) + 1, GFP_KERNEL); if (!buf) return -ENOMEM; - buf[0] = offset; - memcpy(&buf[1], val, len); - msg[0].len = len + 1; + buf[0] = UCSI_CONTROL; + memcpy(&buf[1], &command, sizeof(command)); + msg[0].len = sizeof(command) + 1; msg[0].buf = buf; ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); kfree(buf); if (ret != ARRAY_SIZE(msg)) { - dev_err(g0->dev, "i2c write %02x, %02x error: %d\n", client->addr, offset, ret); + dev_err(g0->dev, "i2c write %02x, %02x error: %d\n", client->addr, UCSI_CONTROL, ret); return ret < 0 ? ret : -EIO; } @@ -393,11 +392,10 @@ static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, cons return 0; } -static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset, const void *val, - size_t len) +static int ucsi_stm32g0_sync_control(struct ucsi *ucsi, u64 command) { struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); - bool ack = UCSI_COMMAND(*(u64 *)val) == UCSI_ACK_CC_CI; + bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI; int ret; if (ack) @@ -405,7 +403,7 @@ static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset, const else set_bit(COMMAND_PENDING, &g0->flags); - ret = ucsi_stm32g0_async_write(ucsi, offset, val, len); + ret = ucsi_stm32g0_async_control(ucsi, command); if (ret) goto out_clear_bit; @@ -449,8 +447,8 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) static const struct ucsi_operations ucsi_stm32g0_ops = { .read = ucsi_stm32g0_read, - .sync_write = ucsi_stm32g0_sync_write, - .async_write = ucsi_stm32g0_async_write, + .sync_control = ucsi_stm32g0_sync_control, + .async_control = ucsi_stm32g0_async_control, }; static int ucsi_stm32g0_register(struct ucsi *ucsi) From patchwork Sun Jun 2 23:24:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 13683132 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 94193C27C52 for ; Sun, 2 Jun 2024 23:25:20 +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=fQ/ZgRmnOBXXnVBb7oaa7NOJw9eM43qmTHuqbr3fNG8=; b=ncgZA+jYAXKCi6 xXlCletlUqLTNRiuXLwMGsVNJnsU4N4TirF6YYUhtmyN9EN0/PKEP3tqksX+V76+VdR5byOdDQ/ZS dtKxgR1AqDPIwf/8kUp83WKXpcMGRkdfvEm5KpnOFhscT03xNwsf8tZ2KAa+mLru+nPG1Cd5dCf2z szZD8zbj2pQLpEI4DMb1zdkc7mZGhl7oxwBWF9ZLkWwuGOPSLnY/x0y06zMAngF/EW0oxZa8pC6G3 TDWMNUWPrWhviOLmHPxnAoiemItCg8pMD83sAK3/xmZUpVbYFrOViqtfQq3cnrbfW2w2ighO7lELo ZKHaNTAHWQTwfmGluBtg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZX-0000000F67F-2AaC; Sun, 02 Jun 2024 23:25:07 +0000 Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZR-0000000F63H-2TW6 for linux-arm-kernel@lists.infradead.org; Sun, 02 Jun 2024 23:25:04 +0000 Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-52962423ed8so4229402e87.2 for ; Sun, 02 Jun 2024 16:25:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717370699; x=1717975499; 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=xABSaJig708zsQYcdtLiZTJ10Q7uZbqt3rPdrspBKnQ=; b=q19i5yjyKVboGROaePapM95A30Lw6M0hkmjUQEeu+gmbE07Sj16eZPj8hSPFIDD4Sz iJTJgkI93ORniEk1SatR4/25EXNJTxLAMDiwkHFXhwSiaqLg4DKSRfA5ticYakapIPjd rBms8c3lhKYLFlvrksGy5U26QINBcd2Rs5+SCa3y/Y4xRVVd2bPhlkHCCVEpVYyCZAui lsnHaFs1Nmi18pWLa9Oy2fYhHzNV9K8+AcJRCmj13cTgEzOgL/7v/ZB/8gyF9qt0gm02 d0AqnAnPgO7rtusv1u72LtLlg97msnaaqQzwpgi9mrXOVBtNodP6RB54ax+0s1mBhLfF xXlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717370699; x=1717975499; 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=xABSaJig708zsQYcdtLiZTJ10Q7uZbqt3rPdrspBKnQ=; b=Lyd6+8LDLzy8CcodZnmtl40Y73iaQNQtP5IoxJTwRqlthf+DKu5lHBHg7FenTE2Rhi Ve4JWjR/SikS2Vi5QQj4HKFdsD7nsAC1YD33EkgGAQJDk8Lf00hcPuSdf0JiB/VQ/4CK 98QPAN+6/hKcc9lZoQRmxrqb4dz53nZl/mQGEpqpRDUDpWWowoBmXKl1/1Ev3lIKGUHe gPALhsfiT5LAPUQg81zspvQusyObv9u4b+/hA/aZxt9izY05WwBO42WxyfeyGgVy8HyP PcV4S06VaEnHNp52Jawj/MMzg4kfso0ocd0DKD1vk68clIyaUlOnb2nlnJkEaLvl2CAH qe4Q== X-Forwarded-Encrypted: i=1; AJvYcCVqrsYTo+817VlEyj/St3I5dRtH1+Z+J2NNatQrGnVhzZeA/qh8M4NFF1CmOUvdE85LXxEP3P8RUDTMqSqIASalVeJOe6byzHCUbJ9+Qxu8w/Teaes= X-Gm-Message-State: AOJu0YwUdY+zmOBWWZLo4TurbvEGBV9O148vgt2x8yjD9dIehij81Mp9 846ZN59RR2kS/hB4k2zJyN+6a5vKGJzK326U+hTEo4DrmeH68SbFOc2UksFYsHc= X-Google-Smtp-Source: AGHT+IESdwnWd/AeFs2C1vWz2zVCpBQMzXyc/yU/VLJnkNC5D664gUK44ENV/NgiVlgM4czIZjNqMQ== X-Received: by 2002:a05:6512:3599:b0:52b:825d:7b71 with SMTP id 2adb3069b0e04-52b89576a5bmr4989638e87.16.1717370698873; Sun, 02 Jun 2024 16:24:58 -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:58 -0700 (PDT) From: Dmitry Baryshkov Date: Mon, 03 Jun 2024 02:24:56 +0300 Subject: [PATCH RFC 3/7] usb: typec: ucsi: split read operation MIME-Version: 1.0 Message-Id: <20240603-ucsi-rework-interface-v1-3-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=13058; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=wSWkqsUHUlMJRLwQ6jCjMEtugMyxWzK8PjGp0/lysG8=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmXP9HGMv+GJ1xoq0DFfiAtBojgEr4/bewOc7dD 2LOyh22kW6JATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZlz/RwAKCRCLPIo+Aiko 1XlRB/4tJwOm93LS5SCn6vEEzKibvBsK4/Wq3umcPpXRt7Xq/5XQXZ4Ki4iL2UEXOek+AQQe/ZS TQcc/e7qMy3GsO2ASokhwy0lYCQ91tlIRXXzyWHUPHLluq/JlIWyEiTSzia3K7WjSrMEZjzHvSl EvYhD1J8GELyhAaP1x7PZzig3VdeU6L5nEaBctzKQR00PEVsKOLqT1fCujzLRnKClb+2daWltwe ZvM/rFJeKCIMLI9k9NIN8QFLkPQxzwC93/1QMk3/t087wlJQpkJI2JiD4PoucBl3zzEogjQ/1jK QM0o3m16l4sOH7dooo23Lfsto3xlw3Zw0RgvGXSgJy9fIqVN 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_162501_735689_0893EFB8 X-CRM114-Status: GOOD ( 19.39 ) 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 read operation is only used to read fixed data at fixed offsets (UCSI_VERSION, UCSI_CCI, UCSI_MESSAGE_IN). In some cases drivers apply offset-specific overrides. Split the read() operation into three operations, read_version(), read_cci(), read_message_in(). Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 20 ++++++------ drivers/usb/typec/ucsi/ucsi.h | 9 ++++-- drivers/usb/typec/ucsi/ucsi_acpi.c | 60 +++++++++++++++++++++++++++++------ drivers/usb/typec/ucsi/ucsi_ccg.c | 50 +++++++++++++++-------------- drivers/usb/typec/ucsi/ucsi_glink.c | 19 ++++++++++- drivers/usb/typec/ucsi/ucsi_stm32g0.c | 19 ++++++++++- 6 files changed, 130 insertions(+), 47 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index f58908a76e1d..a3a29ea4e325 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_message_in(ucsi, buf, buf_size); } static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) @@ -130,7 +130,7 @@ static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) if (ret) return ret; - ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ucsi->ops->read_cci(ucsi, &cci); if (ret) return ret; @@ -1294,7 +1294,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) mutex_lock(&ucsi->ppm_lock); - ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ucsi->ops->read_cci(ucsi, &cci); if (ret < 0) goto out; @@ -1312,8 +1312,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS); do { - ret = ucsi->ops->read(ucsi, UCSI_CCI, - &cci, sizeof(cci)); + ret = ucsi->ops->read_cci(ucsi, &cci); if (ret < 0) goto out; if (cci & UCSI_CCI_COMMAND_COMPLETE) @@ -1342,7 +1341,7 @@ static int ucsi_reset_ppm(struct ucsi *ucsi) /* Give the PPM time to process a reset before reading CCI */ msleep(20); - ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ucsi->ops->read_cci(ucsi, &cci); if (ret) goto out; @@ -1745,7 +1744,7 @@ static int ucsi_init(struct ucsi *ucsi) ucsi->ntfy = ntfy; mutex_lock(&ucsi->ppm_lock); - ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ucsi->ops->read_cci(ucsi, &cci); mutex_unlock(&ucsi->ppm_lock); if (ret) return ret; @@ -1859,7 +1858,9 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) { struct ucsi *ucsi; - if (!ops || !ops->read || !ops->sync_control || !ops->async_control) + if (!ops || + !ops->read_version || !ops->read_cci || !ops->read_message_in || + !ops->sync_control || !ops->async_control) return ERR_PTR(-EINVAL); ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL); @@ -1895,8 +1896,7 @@ int ucsi_register(struct ucsi *ucsi) { int ret; - ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version, - sizeof(ucsi->version)); + ret = ucsi->ops->read_version(ucsi, &ucsi->version); if (ret) return ret; diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 94ea54d3d47d..f770e1070424 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -56,7 +56,9 @@ struct dentry; /** * struct ucsi_operations - UCSI I/O operations - * @read: Read operation + * @read_version: Read implemented UCSI version + * @read_cci: Read CCI register + * @read_message_in: Read message data from UCSI * @sync_control: Blocking control operation * @async_control: Non-blocking control operation * @update_altmodes: Squashes duplicate DP altmodes @@ -68,8 +70,9 @@ struct dentry; * return immediately after sending the data to the PPM. */ struct ucsi_operations { - int (*read)(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len); + int (*read_version)(struct ucsi *ucsi, u16 *version); + int (*read_cci)(struct ucsi *ucsi, u32 *cci); + int (*read_message_in)(struct ucsi *ucsi, void *val, size_t val_len); int (*sync_control)(struct ucsi *ucsi, u64 command); int (*async_control)(struct ucsi *ucsi, u64 command); bool (*update_altmodes)(struct ucsi *ucsi, struct ucsi_altmode *orig, diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/ucsi_acpi.c index feccbfc8acbe..61dd28dae3a4 100644 --- a/drivers/usb/typec/ucsi/ucsi_acpi.c +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c @@ -45,8 +45,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_read_version(struct ucsi *ucsi, u16 *version) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); int ret; @@ -55,7 +54,35 @@ static int ucsi_acpi_read(struct ucsi *ucsi, unsigned int offset, if (ret) return ret; - memcpy(val, ua->base + offset, val_len); + memcpy(version, ua->base + UCSI_VERSION, sizeof(*version)); + + return 0; +} + +static int ucsi_acpi_read_cci(struct ucsi *ucsi, u32 *cci) +{ + struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); + int ret; + + ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ); + if (ret) + return ret; + + memcpy(cci, ua->base + UCSI_CCI, sizeof(*cci)); + + return 0; +} + +static int ucsi_acpi_read_message_in(struct ucsi *ucsi, void *val, size_t val_len) +{ + struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); + int ret; + + ret = ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_READ); + if (ret) + return ret; + + memcpy(val, ua->base + UCSI_MESSAGE_IN, val_len); return 0; } @@ -98,30 +125,45 @@ static int ucsi_acpi_sync_control(struct ucsi *ucsi, u64 command) } static const struct ucsi_operations ucsi_acpi_ops = { - .read = ucsi_acpi_read, + .read_version = ucsi_acpi_read_version, + .read_cci = ucsi_acpi_read_cci, + .read_message_in = ucsi_acpi_read_message_in, .sync_control = ucsi_acpi_sync_control, .async_control = ucsi_acpi_async_control }; static int -ucsi_zenbook_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t val_len) +ucsi_zenbook_read_cci(struct ucsi *ucsi, u32 *cci) { struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); int ret; - if (offset == UCSI_VERSION || UCSI_COMMAND(ua->cmd) == UCSI_PPM_RESET) { + if (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); + memcpy(cci, ua->base + UCSI_CCI, sizeof(*cci)); + + return 0; +} + +static int +ucsi_zenbook_read_message_in(struct ucsi *ucsi, void *val, size_t val_len) +{ + struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); + + /* UCSI_MESSAGE_IN is never read for PPM_RESET, return stored data */ + memcpy(val, ua->base + UCSI_MESSAGE_IN, val_len); return 0; } static const struct ucsi_operations ucsi_zenbook_ops = { - .read = ucsi_zenbook_read, + .read_version = ucsi_acpi_read_version, + .read_cci = ucsi_zenbook_read_cci, + .read_message_in = ucsi_zenbook_read_message_in, .sync_control = ucsi_acpi_sync_control, .async_control = ucsi_acpi_async_control }; @@ -143,7 +185,7 @@ static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data) u32 cci; int ret; - ret = ua->ucsi->ops->read(ua->ucsi, UCSI_CCI, &cci, sizeof(cci)); + ret = ua->ucsi->ops->read_cci(ua->ucsi, &cci); if (ret) return; diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index 76b39bb9762d..6ccc394f268e 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -556,32 +556,34 @@ static void ucsi_ccg_nvidia_altmode(struct ucsi_ccg *uc, } } -static int ucsi_ccg_read(struct ucsi *ucsi, unsigned int offset, - void *val, size_t val_len) +static int ucsi_ccg_read_version(struct ucsi *ucsi, u16 *version) { 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; + u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(UCSI_VERSION); - 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); - } + return ccg_read(uc, reg, (u8 *)version, sizeof(*version)); +} - if (ret) - return ret; +static int ucsi_ccg_read_cci(struct ucsi *ucsi, u32 *cci) +{ + struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi); - if (offset != UCSI_MESSAGE_IN) - return ret; + spin_lock(&uc->op_lock); + *cci = uc->op_data.cci; + spin_unlock(&uc->op_lock); + + return 0; +} + +static int ucsi_ccg_read_message_in(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; + + 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 +609,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_control(struct ucsi *ucsi, u64 command) @@ -663,7 +665,9 @@ static int ucsi_ccg_sync_control(struct ucsi *ucsi, u64 command) } static const struct ucsi_operations ucsi_ccg_ops = { - .read = ucsi_ccg_read, + .read_version = ucsi_ccg_read_version, + .read_cci = ucsi_ccg_read_cci, + .read_message_in = ucsi_ccg_read_message_in, .sync_control = ucsi_ccg_sync_control, .async_control = ucsi_ccg_async_control, .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 047998f33274..7f6a48624ee5 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -114,6 +114,21 @@ static int pmic_glink_ucsi_read(struct ucsi *__ucsi, unsigned int offset, return ret; } +static int pmic_glink_ucsi_read_version(struct ucsi *ucsi, u16 *version) +{ + return pmic_glink_ucsi_read(ucsi, UCSI_VERSION, version, sizeof(*version)); +} + +static int pmic_glink_ucsi_read_cci(struct ucsi *ucsi, u32 *cci) +{ + return pmic_glink_ucsi_read(ucsi, UCSI_CCI, cci, sizeof(*cci)); +} + +static int pmic_glink_ucsi_read_message_in(struct ucsi *ucsi, void *val, size_t val_len) +{ + return pmic_glink_ucsi_read(ucsi, UCSI_MESSAGE_IN, val, val_len); +} + static int pmic_glink_ucsi_locked_write(struct pmic_glink_ucsi *ucsi, unsigned int offset, const void *val, size_t val_len) { @@ -214,7 +229,9 @@ static void pmic_glink_ucsi_connector_status(struct ucsi_connector *con) } static const struct ucsi_operations pmic_glink_ucsi_ops = { - .read = pmic_glink_ucsi_read, + .read_version = pmic_glink_ucsi_read_version, + .read_cci = pmic_glink_ucsi_read_cci, + .read_message_in = pmic_glink_ucsi_read_message_in, .sync_control = pmic_glink_ucsi_sync_control, .async_control = pmic_glink_ucsi_async_control, .update_connector = pmic_glink_ucsi_update_connector, diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c index f97dc5a075e2..e3cfcb258e43 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -359,6 +359,21 @@ static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val, return 0; } +static int ucsi_stm32g0_read_version(struct ucsi *ucsi, u16 *version) +{ + return ucsi_stm32g0_read(ucsi, UCSI_VERSION, version, sizeof(*version)); +} + +static int ucsi_stm32g0_read_cci(struct ucsi *ucsi, u32 *cci) +{ + return ucsi_stm32g0_read(ucsi, UCSI_CCI, cci, sizeof(*cci)); +} + +static int ucsi_stm32g0_read_message_in(struct ucsi *ucsi, void *val, size_t len) +{ + return ucsi_stm32g0_read(ucsi, UCSI_MESSAGE_IN, val, len); +} + static int ucsi_stm32g0_async_control(struct ucsi *ucsi, u64 command) { struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); @@ -446,7 +461,9 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) } static const struct ucsi_operations ucsi_stm32g0_ops = { - .read = ucsi_stm32g0_read, + .read_version = ucsi_stm32g0_read_version, + .read_cci = ucsi_stm32g0_read_cci, + .read_message_in = ucsi_stm32g0_read_message_in, .sync_control = ucsi_stm32g0_sync_control, .async_control = ucsi_stm32g0_async_control, }; 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; } From patchwork Sun Jun 2 23:24:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 13683133 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 7ADF4C27C44 for ; Sun, 2 Jun 2024 23:25:22 +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=EEPxZiMEFGRAgMMrTT93BvhD6EuxiyJJX+or4fgq7x0=; b=NSf1UycZ5hMAg5 SiGCMo5itsKAO1mIxMKCHdMYEoboLwPIOjxUFly+tSF6Pl56wFgC+Y04TqrpxQwPvx9y/q0+05Chs UpZXOuM4Dd5uKnCh3yErvHsw5PV9gw7H66u9/4YUdzCRmSUrNcekfC7IGIWaUQnxx8gCHX8yM6AUr 5uPm6i2Wt4y64h5N/0qqTV/NufiIwr+XB5PMM6meLzhH9GBvsnCOWlKKvYwG3gL88T9t8+d02TkNF PB9cNmHlYptAUT5uF/pbksykEct/nNkafQYFWVrTq2aqVxOFtOpGPZg3vJY+NWt6cQsHxQtHaifI5 tkQt7IOn5dDIW3PA7HXQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZb-0000000F691-0v6r; Sun, 02 Jun 2024 23:25:11 +0000 Received: from mail-lf1-x12f.google.com ([2a00:1450:4864:20::12f]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZS-0000000F63n-24Cr for linux-arm-kernel@lists.infradead.org; Sun, 02 Jun 2024 23:25:05 +0000 Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-52b7ffd9f6eso3446564e87.3 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=uMHRLla4ILsm5OyTHPffYpll4rFbwg7rZ0xiqfrtlAk=; b=R+Pe/aiNNhwmT7VtXRTFwhS0Yv+CKGR3g85RtJ+eDth/cAF2EzYwuvY2W384MjtFAX iGP6uu/fE51NaADuCjyb5uQA24NKCKZ5bCtAyUj9sUlaKJiRw3Hn5xr+A2xJTfDdenEi KAIVzaykQTGmYSiZa8ok7e3CjYIS++Z7p6tHlWnb98WmJlaVwp2u+qQabbkPFOxnpWZF vTjveXY40eSz3VHya7me6WMEXiJjyLwAmtUDgq1OPo7wEbIGaqpbeEwtTiEGJy5HakU8 VBKCQeA+xodQl5sVJbxeDZwnV9I4rat64FS7I0NtM41CQkdxXqikbw4/CPS0NG3Sjpy8 3qhw== 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=uMHRLla4ILsm5OyTHPffYpll4rFbwg7rZ0xiqfrtlAk=; b=LqEkVywk0D+DuQOiGB2bOIekIoQiAz+rhjBA+LeI7y8bgGN+P5ijrqPLBYCrF/6TEw O8XJ5rDNHuuPlgQjkXWsn+xvFgWHGRSYnTZc3THnIbWOpuMOSiuppzA4t7mnAFS9wS/r OfzdMrI6A1O4x2QZVbMZI4QH9neG4+jmgQF4ddsAZF7bfEnPL9NZvQ2aRoVsWFMf113Z nJ3edvEo4ep/YgKX6WVhl9iYgtbydQRIBUeDcsIYytUd7ik6R249CCyAFq39XrNamvoV QSWR9Yq3OO9S009mD94PRmot95ZHTyPEMphtKSwdKSf1PUf9h0zFhI8rLxRcx+lyI2rv BXoA== X-Forwarded-Encrypted: i=1; AJvYcCUK6UtN0nYJ0Y4mX8LIf6uEtMxvoMicskVHeaJjoGkqIUdl7uLLI8/obtpSfIfaaCka2SV5tjC/KglOJIRROv5POGzZbywwzFJ2Bl+5o7VT8IfqIYU= X-Gm-Message-State: AOJu0Ywlhv3fyAAwcCEiMf+IY+tme4e+miajT9uxfqOexYWt1PW/AwPm qtaDvOh8QDptV3AmUM6HJuWjFq0z1iDeEEZR0Z2E/Pk51KbjWv/Z2DqGfMtZYlQ= X-Google-Smtp-Source: AGHT+IEinySEY+8mUF5JebFptkGUEBHH1p6v8JTLm5BkjmrOYM/dSR6lKjEU8RJ2VRVXxIay7X04zA== X-Received: by 2002:ac2:5927:0:b0:523:b19a:2602 with SMTP id 2adb3069b0e04-52b896c1583mr4583720e87.54.1717370700174; Sun, 02 Jun 2024 16:25:00 -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.59 (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:58 +0300 Subject: [PATCH RFC 5/7] usb: typec: ucsi: inline ucsi_read_message_in MIME-Version: 1.0 Message-Id: <20240603-ucsi-rework-interface-v1-5-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=1606; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=mrPf66+/PzNa3/0SVv6o28YNwb3VM608GVeFzUihyTU=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmXP9HAufR5oVjQ0LWVWAxSmYfDH5vS6wJiYuZ1 dHGzcQ447iJATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZlz/RwAKCRCLPIo+Aiko 1WYeB/9JZcDHr+OF40i0unPYw6KFSmRQ5469nPCT2YTGknUhZXQSaNZ5MjbUZg7XLCeUYccokgJ y5EPZdDTaWFO+eXLDilcoFpoNzbs4Kowo86yEfvPVm2YzydiXpGCkvHSnQcEskFxHbUnI0wEQSt zspOCM051maHPSZLoGtbJ6mt/sZ8KJiTBfc0iQi+XER2c5/lwFA0rcfEXQXEfqECNWB8tn7HF52 W23c3lkftFGNTtNSGwh46VWLlcJQ00eVChhByC4oPoyqS+taGwm4IM5ROUGdRbI8RhVxx7BcVF7 mLr41IHkns9dAUC5Ut/0i3z9vQBkNOX6Jz1pOI6DlKLYaPg3 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_723872_04D306D9 X-CRM114-Status: GOOD ( 12.72 ) 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 There is no need to have a separate wrapper for reading MESSAGE_IN data, inline it to ucsi_run_command(). Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 54a2c505442f..ac4bd86b3fae 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -36,19 +36,6 @@ */ #define UCSI_SWAP_TIMEOUT_MS 5000 -static int ucsi_read_message_in(struct ucsi *ucsi, void *buf, - size_t buf_size) -{ - /* - * Below UCSI 2.0, MESSAGE_IN was limited to 16 bytes. Truncate the - * reads here. - */ - if (ucsi->version <= UCSI_VERSION_1_2) - buf_size = clamp(buf_size, 0, 16); - - return ucsi->ops->read_message_in(ucsi, buf, buf_size); -} - static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) { u64 ctrl; @@ -70,6 +57,13 @@ static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, *cci = 0; + /* + * Below UCSI 2.0, MESSAGE_IN was limited to 16 bytes. Truncate the + * reads here. + */ + if (ucsi->version <= UCSI_VERSION_1_2) + size = clamp(size, 0, 16); + ret = ucsi->ops->sync_control(ucsi, command); if (ret) return ret; @@ -98,7 +92,7 @@ static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, } if (data) { - ret = ucsi_read_message_in(ucsi, data, size); + ret = ucsi->ops->read_message_in(ucsi, data, size); if (ret) return ret; } From patchwork Sun Jun 2 23:24:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 13683135 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 4AC3CC27C44 for ; Sun, 2 Jun 2024 23:25:27 +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=3NcqQPVKgFKOSY4+HU7fejCYhS58+Udh1Ow6F16SLWI=; b=VCOJHMmGAD6cjq wUEaBBGRu2VKJiE8K3mHPwopouBkGZhSNlmP9vt5d0ebWCX6d8mJy8eB0DKX5PsXEJB2WJK0Tnh71 /X3CG6lJQ4upF013cb7ERFPwf29knrKg9NTrAiCM0xSiI5Mkv+xUV1anFcf/28J+7Vugi7/daxYA8 ruq3jnd6jB2QJOvPBAriQhgncaReJ9lY1Da36J4C3h8LKyvEGiVn+OKw/EfTiGkQuqmb8Zq8uJCeD RYjksaiss+ac70Cs6HcrRwnNiu0Co75vqZRlCYixglfBCj+eJYg9Plagv49umf7tedbDQU46Q1aJo +Cm+fA/CkRHhzP397Yww==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZd-0000000F6AL-0m0y; Sun, 02 Jun 2024 23:25:13 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZT-0000000F642-0C1H for linux-arm-kernel@lists.infradead.org; Sun, 02 Jun 2024 23:25:05 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-52965199234so4220357e87.2 for ; Sun, 02 Jun 2024 16:25:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717370701; x=1717975501; 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=fLTVeDBunzWZL7NXj62dRVbdmskQ6vEkyJgavqsHljg=; b=yHYDURdHZPBQcujBzy6t7RAdNy2qwYpI7j8LtE5FO/g6zyQjtZw23g0EZIEI7h4TB5 MGY+B0WABp7LMR/t+HHKCbCxGuz/30Dsyrei304t+TgZXMJAayem8At+87i7g40n0Kmn +D4zTME7Y186uAKidxFWuwWdrgQ7ImYif2jJGG6x4Lgu6o9HAvWQZwOylz4Oolc+bIz1 IunaFDCYpmmSP1zMensELivJezPAXTGIGe4mj2kUge1m0R8S3wuhuc8Dv4dfEks7HE5X pBYh+aVAZy3Ldbrc2trBYMebDooVbOgbYkstykndR9zYt44P4iTlMoMqI22gHvz4xaY1 lnzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717370701; x=1717975501; 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=fLTVeDBunzWZL7NXj62dRVbdmskQ6vEkyJgavqsHljg=; b=YJS9K6ll0DdISz15W1T5OvIA+KEyfLgpZLFmILah7SBltDPrgIVNgltJm44BBN82qJ do2/zQIR/7BjfgDSoZbMCuCC0RYhiO9BtO8Gwa7MlDeJTzMvbMInWi13uTQvHUNtHO7l yS1RR1aGLGXZ6688nQkNassnegmq/9WjB4WEl92RYTQcpnE9LpAI6IGyaWi/0XlNo1W/ yPKxmyImBuTFDKBbNZvzf0WHw5WeNpFTSN56Crx68xnkMRPDbNXqp2nBAe4GmdmEF/5Q WV+lHx3HfXGsXmvKlKf81+KAwCPLSA/IYTFFFuSjCwI8n1Okyj3GyTm7MSBZkpvv6bNy I7cA== X-Forwarded-Encrypted: i=1; AJvYcCXY5GKPGChYSJxpFzsMZFqK4sdYPFR4byhS/CooaiybWbLxrkbXUy1JabrFGr9x6VAvhx5MnrKjR5MKptRxA06gWxauXHG3A8UfA65laUsrsP6up0w= X-Gm-Message-State: AOJu0YxPsKQbzvgjPhrXCeSutwNAS1I51L6DP2Nn/p03trZ3fUx7nwrh R08rodLCU2QdNxsbEx5She/JYzQ/w3nyio1F4AEosYEII7sspy0rMWymWEgAPso= X-Google-Smtp-Source: AGHT+IHY6hl3fA/aTU7/5Qs5jHutGO3eYNynfRaMh7gFJxG/9+xPnjuSt6dd6ciGptalIyviyk3Uvw== X-Received: by 2002:a05:6512:1152:b0:52b:9c8a:735a with SMTP id 2adb3069b0e04-52b9c8a74fcmr205388e87.40.1717370700848; Sun, 02 Jun 2024 16:25:00 -0700 (PDT) Received: from umbar.lan ([192.130.178.91]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-52b959008aasm392369e87.269.2024.06.02.16.25.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Jun 2024 16:25:00 -0700 (PDT) From: Dmitry Baryshkov Date: Mon, 03 Jun 2024 02:24:59 +0300 Subject: [PATCH RFC 6/7] usb: typec: ucsi: extract common code for command handling MIME-Version: 1.0 Message-Id: <20240603-ucsi-rework-interface-v1-6-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=14654; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=xsVlzflUb/Qp1trYi1IqtQRk3wSvC2VlMHb1s/cNflQ=; b=owGbwMvMwMXYbdNlx6SpcZXxtFoSQ1rMf3fZKVbMvl/MvA4YZCjfVVleW3U9nju+f0XV2tjJe 17ZW27rZDRmYWDkYpAVU2TxKWiZGrMpOezDjqn1MINYmUCmMHBxCsBEFHdxMMyLKbipKsrvU3b1 XdwubZecG8LTp9RNfLIn4AHP2+7vP63uPZD5J8+cyiOrxngkznOJWV1RumBn6NeZf1ezipzkfb1 HnHftHiWnLc4PdVXFVwhs1P0t92qflabtpI37/VimvE80fLrGfHuDn72PfbJNb4e/6h/v566PGf 23Bh3fossgZrVWLD80J39fr0Ju6u34vy31dU9sD8dzZ0gt3HPDqsFb606YXsSX7bZ+Dd8dG76Jz TcyXSRgzWBk3F56Y01K8fR16ayKhmKl+afNzVdctzqm8W2L+6P/1xp+7T7Oocx7+5X/e50i///7 vk07cmS/f1uWe9mxh1qqEXph7wp3sYRmds50cnz1zkoTAA== 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_162503_238995_AC975680 X-CRM114-Status: GOOD ( 19.70 ) 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 Extract common functions to handle command sending and to handle events from UCSI. This ensures that all UCSI glue drivers handle the ACKs in the same way. The CCG driver used DEV_CMD_PENDING both for internal firmware-related commands and for UCSI control handling. Leave the former use case intact. Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 43 ++++++++++++++++++++++++++++++++ drivers/usb/typec/ucsi/ucsi.h | 7 ++++++ drivers/usb/typec/ucsi/ucsi_acpi.c | 46 +++------------------------------- drivers/usb/typec/ucsi/ucsi_ccg.c | 21 +++------------- drivers/usb/typec/ucsi/ucsi_glink.c | 47 ++--------------------------------- drivers/usb/typec/ucsi/ucsi_stm32g0.c | 44 ++------------------------------ 6 files changed, 60 insertions(+), 148 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index ac4bd86b3fae..4d36a04dc63b 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -36,6 +36,48 @@ */ #define UCSI_SWAP_TIMEOUT_MS 5000 +void ucsi_notify_common(struct ucsi *ucsi, u32 cci) +{ + if (UCSI_CCI_CONNECTOR(cci)) + ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci)); + + if (cci & UCSI_CCI_ACK_COMPLETE && + test_bit(ACK_PENDING, &ucsi->flags)) + complete(&ucsi->complete); + + if (cci & UCSI_CCI_COMMAND_COMPLETE && + test_bit(COMMAND_PENDING, &ucsi->flags)) + complete(&ucsi->complete); +} +EXPORT_SYMBOL_GPL(ucsi_notify_common); + +int ucsi_sync_control_common(struct ucsi *ucsi, u64 command) +{ + bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI; + int ret; + + if (ack) + set_bit(ACK_PENDING, &ucsi->flags); + else + set_bit(COMMAND_PENDING, &ucsi->flags); + + ret = ucsi->ops->async_control(ucsi, command); + if (ret) + goto out_clear_bit; + + if (!wait_for_completion_timeout(&ucsi->complete, 5 * HZ)) + ret = -ETIMEDOUT; + +out_clear_bit: + if (ack) + clear_bit(ACK_PENDING, &ucsi->flags); + else + clear_bit(COMMAND_PENDING, &ucsi->flags); + + return ret; +} +EXPORT_SYMBOL_GPL(ucsi_sync_control_common); + static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) { u64 ctrl; @@ -1858,6 +1900,7 @@ struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) INIT_WORK(&ucsi->resume_work, ucsi_resume_work); INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work); mutex_init(&ucsi->ppm_lock); + init_completion(&ucsi->complete); ucsi->dev = dev; ucsi->ops = ops; diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index f770e1070424..b08393417fa9 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -4,6 +4,7 @@ #define __DRIVER_USB_TYPEC_UCSI_H #include +#include #include #include #include @@ -409,6 +410,9 @@ struct ucsi { /* PPM communication flags */ unsigned long flags; #define EVENT_PENDING 0 +#define COMMAND_PENDING 1 +#define ACK_PENDING 2 + struct completion complete; unsigned long quirks; #define UCSI_NO_PARTNER_PDOS BIT(0) /* Don't read partner's PDOs */ @@ -473,6 +477,9 @@ int ucsi_send_command(struct ucsi *ucsi, u64 command, void ucsi_altmode_update_active(struct ucsi_connector *con); int ucsi_resume(struct ucsi *ucsi); +void ucsi_notify_common(struct ucsi *ucsi, u32 cci); +int ucsi_sync_control_common(struct ucsi *ucsi, u64 command); + #if IS_ENABLED(CONFIG_POWER_SUPPLY) int ucsi_register_port_psy(struct ucsi_connector *con); void ucsi_unregister_port_psy(struct ucsi_connector *con); diff --git a/drivers/usb/typec/ucsi/ucsi_acpi.c b/drivers/usb/typec/ucsi/ucsi_acpi.c index 61dd28dae3a4..e8402fac70c8 100644 --- a/drivers/usb/typec/ucsi/ucsi_acpi.c +++ b/drivers/usb/typec/ucsi/ucsi_acpi.c @@ -21,10 +21,6 @@ struct ucsi_acpi { struct device *dev; struct ucsi *ucsi; void *base; - struct completion complete; - unsigned long flags; -#define UCSI_ACPI_COMMAND_PENDING 1 -#define UCSI_ACPI_ACK_PENDING 2 guid_t guid; u64 cmd; }; @@ -97,38 +93,11 @@ static int ucsi_acpi_async_control(struct ucsi *ucsi, u64 command) return ucsi_acpi_dsm(ua, UCSI_DSM_FUNC_WRITE); } -static int ucsi_acpi_sync_control(struct ucsi *ucsi, u64 command) -{ - struct ucsi_acpi *ua = ucsi_get_drvdata(ucsi); - bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI; - int ret; - - if (ack) - set_bit(UCSI_ACPI_ACK_PENDING, &ua->flags); - else - set_bit(UCSI_ACPI_COMMAND_PENDING, &ua->flags); - - ret = ucsi_acpi_async_control(ucsi, command); - if (ret) - goto out_clear_bit; - - if (!wait_for_completion_timeout(&ua->complete, 5 * HZ)) - ret = -ETIMEDOUT; - -out_clear_bit: - if (ack) - clear_bit(UCSI_ACPI_ACK_PENDING, &ua->flags); - else - clear_bit(UCSI_ACPI_COMMAND_PENDING, &ua->flags); - - return ret; -} - static const struct ucsi_operations ucsi_acpi_ops = { .read_version = ucsi_acpi_read_version, .read_cci = ucsi_acpi_read_cci, .read_message_in = ucsi_acpi_read_message_in, - .sync_control = ucsi_acpi_sync_control, + .sync_control = ucsi_sync_control_common, .async_control = ucsi_acpi_async_control }; @@ -164,7 +133,7 @@ static const struct ucsi_operations ucsi_zenbook_ops = { .read_version = ucsi_acpi_read_version, .read_cci = ucsi_zenbook_read_cci, .read_message_in = ucsi_zenbook_read_message_in, - .sync_control = ucsi_acpi_sync_control, + .sync_control = ucsi_sync_control_common, .async_control = ucsi_acpi_async_control }; @@ -189,15 +158,7 @@ static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data) if (ret) return; - if (UCSI_CCI_CONNECTOR(cci)) - ucsi_connector_change(ua->ucsi, UCSI_CCI_CONNECTOR(cci)); - - if (cci & UCSI_CCI_ACK_COMPLETE && - test_bit(UCSI_ACPI_ACK_PENDING, &ua->flags)) - complete(&ua->complete); - if (cci & UCSI_CCI_COMMAND_COMPLETE && - test_bit(UCSI_ACPI_COMMAND_PENDING, &ua->flags)) - complete(&ua->complete); + ucsi_notify_common(ua->ucsi, cci); } static int ucsi_acpi_probe(struct platform_device *pdev) @@ -231,7 +192,6 @@ static int ucsi_acpi_probe(struct platform_device *pdev) if (ret) return ret; - init_completion(&ua->complete); ua->dev = &pdev->dev; id = dmi_first_match(ucsi_acpi_quirks); diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c index 6ccc394f268e..ba4db2310a05 100644 --- a/drivers/usb/typec/ucsi/ucsi_ccg.c +++ b/drivers/usb/typec/ucsi/ucsi_ccg.c @@ -222,8 +222,6 @@ struct ucsi_ccg { u16 fw_build; struct work_struct pm_work; - struct completion complete; - u64 last_cmd_sent; bool has_multiple_dp; struct ucsi_ccg_altmode orig[UCSI_MAX_ALTMODES]; @@ -637,7 +635,6 @@ static int ucsi_ccg_sync_control(struct ucsi *ucsi, u64 command) mutex_lock(&uc->lock); pm_runtime_get_sync(uc->dev); - set_bit(DEV_CMD_PENDING, &uc->flags); uc->last_cmd_sent = command; @@ -649,15 +646,8 @@ static int ucsi_ccg_sync_control(struct ucsi *ucsi, u64 command) ucsi_ccg_update_set_new_cam_cmd(uc, con, &command); } - ret = ucsi_ccg_async_control(ucsi, command); - if (ret) - goto err_clear_bit; - - if (!wait_for_completion_timeout(&uc->complete, msecs_to_jiffies(5000))) - ret = -ETIMEDOUT; + ret = ucsi_sync_control_common(ucsi, command); -err_clear_bit: - clear_bit(DEV_CMD_PENDING, &uc->flags); pm_runtime_put_sync(uc->dev); mutex_unlock(&uc->lock); @@ -694,9 +684,6 @@ static irqreturn_t ccg_irq_handler(int irq, void *data) if (ret) goto err_clear_irq; - if (UCSI_CCI_CONNECTOR(cci)) - ucsi_connector_change(uc->ucsi, UCSI_CCI_CONNECTOR(cci)); - /* * As per CCGx UCSI interface guide, copy CCI and MESSAGE_IN * to the OpRegion before clear the UCSI interrupt @@ -708,9 +695,8 @@ static irqreturn_t ccg_irq_handler(int irq, void *data) err_clear_irq: ccg_write(uc, CCGX_RAB_INTR_REG, &intr_reg, sizeof(intr_reg)); - if (!ret && test_bit(DEV_CMD_PENDING, &uc->flags) && - cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE)) - complete(&uc->complete); + if (!ret) + ucsi_notify_common(uc->ucsi, cci); return IRQ_HANDLED; } @@ -1429,7 +1415,6 @@ static int ucsi_ccg_probe(struct i2c_client *client) uc->client = client; uc->irq = client->irq; mutex_init(&uc->lock); - init_completion(&uc->complete); INIT_WORK(&uc->work, ccg_update_firmware); INIT_WORK(&uc->pm_work, ccg_pm_workaround_work); diff --git a/drivers/usb/typec/ucsi/ucsi_glink.c b/drivers/usb/typec/ucsi/ucsi_glink.c index 7f6a48624ee5..8b74da38d283 100644 --- a/drivers/usb/typec/ucsi/ucsi_glink.c +++ b/drivers/usb/typec/ucsi/ucsi_glink.c @@ -64,12 +64,8 @@ struct pmic_glink_ucsi { struct ucsi *ucsi; struct completion read_ack; struct completion write_ack; - struct completion sync_ack; - bool sync_pending; struct mutex lock; /* protects concurrent access to PMIC Glink interface */ - int sync_val; - struct work_struct notify_work; struct work_struct register_work; @@ -170,35 +166,6 @@ static int pmic_glink_ucsi_async_control(struct ucsi *__ucsi, u64 command) return ret; } -static int pmic_glink_ucsi_sync_control(struct ucsi *__ucsi, u64 command) -{ - struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(__ucsi); - unsigned long left; - int ret; - - /* TOFIX: Downstream forces recipient to CON when UCSI_GET_ALTERNATE_MODES command */ - - mutex_lock(&ucsi->lock); - ucsi->sync_val = 0; - reinit_completion(&ucsi->sync_ack); - ucsi->sync_pending = true; - ret = pmic_glink_ucsi_locked_write(ucsi, UCSI_CONTROL, &command, sizeof(command)); - mutex_unlock(&ucsi->lock); - - left = wait_for_completion_timeout(&ucsi->sync_ack, 5 * HZ); - if (!left) { - dev_err(ucsi->dev, "timeout waiting for UCSI sync write response\n"); - /* return 0 here and let core UCSI code handle the CCI_BUSY */ - ret = 0; - } else if (ucsi->sync_val) { - dev_err(ucsi->dev, "sync write returned: %d\n", ucsi->sync_val); - } - - ucsi->sync_pending = false; - - return ret; -} - static void pmic_glink_ucsi_update_connector(struct ucsi_connector *con) { struct pmic_glink_ucsi *ucsi = ucsi_get_drvdata(con->ucsi); @@ -232,7 +199,7 @@ static const struct ucsi_operations pmic_glink_ucsi_ops = { .read_version = pmic_glink_ucsi_read_version, .read_cci = pmic_glink_ucsi_read_cci, .read_message_in = pmic_glink_ucsi_read_message_in, - .sync_control = pmic_glink_ucsi_sync_control, + .sync_control = ucsi_sync_control_common, .async_control = pmic_glink_ucsi_async_control, .update_connector = pmic_glink_ucsi_update_connector, .connector_status = pmic_glink_ucsi_connector_status, @@ -256,14 +223,12 @@ static void pmic_glink_ucsi_write_ack(struct pmic_glink_ucsi *ucsi, const void * if (resp->ret_code) return; - ucsi->sync_val = resp->ret_code; complete(&ucsi->write_ack); } static void pmic_glink_ucsi_notify(struct work_struct *work) { struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, notify_work); - unsigned int con_num; u32 cci; int ret; @@ -273,14 +238,7 @@ static void pmic_glink_ucsi_notify(struct work_struct *work) return; } - con_num = UCSI_CCI_CONNECTOR(cci); - if (con_num) - ucsi_connector_change(ucsi->ucsi, con_num); - - if (ucsi->sync_pending && - (cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))) { - complete(&ucsi->sync_ack); - } + ucsi_notify_common(ucsi->ucsi, cci); } static void pmic_glink_ucsi_register(struct work_struct *work) @@ -362,7 +320,6 @@ static int pmic_glink_ucsi_probe(struct auxiliary_device *adev, INIT_WORK(&ucsi->register_work, pmic_glink_ucsi_register); init_completion(&ucsi->read_ack); init_completion(&ucsi->write_ack); - init_completion(&ucsi->sync_ack); mutex_init(&ucsi->lock); ucsi->ucsi = ucsi_create(dev, &pmic_glink_ucsi_ops); diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c index e3cfcb258e43..d948c3f579e1 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -61,11 +61,7 @@ struct ucsi_stm32g0 { struct i2c_client *i2c_bl; bool in_bootloader; u8 bl_version; - struct completion complete; struct device *dev; - unsigned long flags; -#define COMMAND_PENDING 1 -#define ACK_PENDING 2 const char *fw_name; struct ucsi *ucsi; bool suspended; @@ -407,35 +403,6 @@ static int ucsi_stm32g0_async_control(struct ucsi *ucsi, u64 command) return 0; } -static int ucsi_stm32g0_sync_control(struct ucsi *ucsi, u64 command) -{ - struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); - bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI; - int ret; - - if (ack) - set_bit(ACK_PENDING, &g0->flags); - else - set_bit(COMMAND_PENDING, &g0->flags); - - ret = ucsi_stm32g0_async_control(ucsi, command); - if (ret) - goto out_clear_bit; - - if (!wait_for_completion_timeout(&g0->complete, msecs_to_jiffies(5000))) - ret = -ETIMEDOUT; - else - return 0; - -out_clear_bit: - if (ack) - clear_bit(ACK_PENDING, &g0->flags); - else - clear_bit(COMMAND_PENDING, &g0->flags); - - return ret; -} - static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) { struct ucsi_stm32g0 *g0 = data; @@ -449,13 +416,7 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) if (ret) return IRQ_NONE; - if (UCSI_CCI_CONNECTOR(cci)) - ucsi_connector_change(g0->ucsi, UCSI_CCI_CONNECTOR(cci)); - - if (cci & UCSI_CCI_ACK_COMPLETE && test_and_clear_bit(ACK_PENDING, &g0->flags)) - complete(&g0->complete); - if (cci & UCSI_CCI_COMMAND_COMPLETE && test_and_clear_bit(COMMAND_PENDING, &g0->flags)) - complete(&g0->complete); + ucsi_notify_common(g0->ucsi, cci); return IRQ_HANDLED; } @@ -464,7 +425,7 @@ static const struct ucsi_operations ucsi_stm32g0_ops = { .read_version = ucsi_stm32g0_read_version, .read_cci = ucsi_stm32g0_read_cci, .read_message_in = ucsi_stm32g0_read_message_in, - .sync_control = ucsi_stm32g0_sync_control, + .sync_control = ucsi_sync_control_common, .async_control = ucsi_stm32g0_async_control, }; @@ -665,7 +626,6 @@ static int ucsi_stm32g0_probe(struct i2c_client *client) g0->dev = dev; g0->client = client; - init_completion(&g0->complete); i2c_set_clientdata(client, g0); g0->ucsi = ucsi_create(dev, &ucsi_stm32g0_ops); From patchwork Sun Jun 2 23:25:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 13683134 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 564EAC27C50 for ; Sun, 2 Jun 2024 23:25:24 +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=IZtRESOfT/tTcH3edULUFd5Va9IpeWPWAjpQh+8CDuo=; b=KJ84dYJ9/GcYWv MXBMs085J/uz9S5HxHCBKaObqwmRbOyopFMo5Nrw2eFCsWznfyz6lueJjVwRa/KFu1sKRkXy6Ta/7 ewA9xXXzcjD1yxEz1NGBZDKi+11+0IFIx1re+xHZfSNWB5qwOBlAId4NdCDKdrsyqbnqRlXOZt6EE XtPD7kDs18ywCiAulfVoQLMrPEn9w93bz8UVdj6Ti95eyWoANjxOsYWXtwSzsoQHJcy98PonXy/kL CNWcMo0b1+6lFb2Ox7+kLqnc0h5LVVqCXRCEa3mf4Tpj7eIZFCoHQz4Y343P5jBhKwB8jLgr0wWWg pn+2hEi/HoCrBYt4pQXQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZe-0000000F6B7-0l5S; Sun, 02 Jun 2024 23:25:14 +0000 Received: from mail-lf1-x12c.google.com ([2a00:1450:4864:20::12c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sDuZT-0000000F64f-2YVl for linux-arm-kernel@lists.infradead.org; Sun, 02 Jun 2024 23:25:05 +0000 Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-52b93370ad0so1654652e87.2 for ; Sun, 02 Jun 2024 16:25:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1717370701; x=1717975501; 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=r8Iq+3RgEig8Cx0iz/mpoUKg07pqVjSqpR5cr51J2sg=; b=mp2V1xVRxTqjf9VtzISmVuQ6DvSt+CjVT9Qqf3EnE05V216oaf9QPM0GYLD5OrZiYF bWuiW5SfFnZ0dnORsnXsej8xwaVfCkLb4cnh+OePoOPIudyA7WjA/gQHbL6jhESscPAN n7rGWUoi+c+sw3P5Zupn67+n6nDvSIzJJWmZwzH45i360dL9Co+1YsWK81vIp/k21gfh /WMCT9VH9tslbB1tp1P4lJBa88Ra/hJ6kxjSmsnyyEA4UIjbSyTtrvR6f/Gf+XSLAFkp e6gxRPTF/z9pCo79veR+2KFsBOmG+B/HBzaALkCBimXJOz3BJ3OI9f2GGi6aKWoBTH7h 0LFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717370701; x=1717975501; 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=r8Iq+3RgEig8Cx0iz/mpoUKg07pqVjSqpR5cr51J2sg=; b=bTDphWNzyNWsP+RPk8nDGpBrxI0m4r2/Ypob9rvZM1tKUpRDcL63swtRkJ3ioEH0E/ BKwj6EKbQKe3tDdNAYGLaxNleVS7KMpzoBXlZ3Jy261NlkYEjc+beA9BbWctSlCAfbiT BOSq3+SznDp4e9OVfLz7fm4SAvbR79uJ1AsQG0oebblNjlte6+XtO0DE8IXIPvZ79WRm rB6C4zLeP0fz4m518fL63K0fk9GHErZZFY9SagKafZ+Uha5guqPJEf3ro7TwavCMVB8U znwERadQyZzgRvwCeazuIHXq06juHwpuQ+M1IQGXvL0jRH/nQuyfToJeol9+IKGPhEE6 /HMg== X-Forwarded-Encrypted: i=1; AJvYcCViETUjNB/R4FZLgIt1/Q1pOG2qKn67L9d7qJ1JB8Y7yrQDIb8StZYalSsXrRZffuQHrakSxFAF7h16H4niXLFoFgwsEn7HOYM/onnXuCfREuUPmds= X-Gm-Message-State: AOJu0Yw66TRcyFFQcdjpOlUdOJz6cG/PC2F/QWK4RhBUShX3OyhmXn8W MXC5rdx0JtxYorYLRyaGdRDfYe2TVf/+Ep9TRTPjuj8n8wqBA79SkYblyk7gD7I= X-Google-Smtp-Source: AGHT+IERXsZJtbptbub4OPpmYt4izzdy6vVE3d9VwxncjVETVSXMGoMmwAAaAqx2FFQoGX/3InI1uA== X-Received: by 2002:ac2:58e7:0:b0:52b:88ba:da88 with SMTP id 2adb3069b0e04-52b8954ea17mr5935410e87.26.1717370701481; Sun, 02 Jun 2024 16:25:01 -0700 (PDT) Received: from umbar.lan ([192.130.178.91]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-52b959008aasm392369e87.269.2024.06.02.16.25.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Jun 2024 16:25:01 -0700 (PDT) From: Dmitry Baryshkov Date: Mon, 03 Jun 2024 02:25:00 +0300 Subject: [PATCH RFC 7/7] usb: typec: ucsi: reorder operations in ucsi_run_command() MIME-Version: 1.0 Message-Id: <20240603-ucsi-rework-interface-v1-7-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=2070; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=qwhd20pcT6mKmuOmdJvReDADwvss13IfbnCS0P5O90U=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmXP9I9UoE+huJlSrwgEUi7zxKYLl7ib9SuXtkX fxqsDm1/vWJATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZlz/SAAKCRCLPIo+Aiko 1ZPnB/wJiXKUWKNGHNVrmNmaiYUeTXAzLFk/CckWZubxbjYiTn17Iv99ejxUhjRVFoYE2cwU8lE SxN7gerADf7/9A3HXwgOOU40QkmSFfc+FAo2sjoBUw8Nq7Lisst2c7LvRypbflv6wXu44KyYh+R cq4NC5KGZPlvD1io3PThZep4n5w7OrgFBl2deD0gg14CCl9W6NvMoYWarHh6jGN3G3t9Xez6JjP 8iQ8r/aWZvXmBySF+7nS+Ohc6U0dC6AE4R3VSB8FCKMUUEACMEVU0mQlf4693IcOC0KvJRopr3x KVVbA1SK1EL9fbV1BpZSHlFPziFH+A+NPxqCFOlMsw/GUGwy 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_162503_798118_7055512D X-CRM114-Status: GOOD ( 15.42 ) 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 Streamline control stream of ucsi_run_command(). Reorder operations so that there is only one call to ucsi_acknowledge(), making sure that all complete commands are acknowledged. This also makes sure that the command is acknowledged even if reading MESSAGE_IN data returns an error. Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 4d36a04dc63b..4666dec005a6 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -95,7 +95,7 @@ static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, void *data, size_t size, bool conn_ack) { - int ret; + int ret, err; *cci = 0; @@ -120,30 +120,24 @@ static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, 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 (*cci & UCSI_CCI_NOT_SUPPORTED) + err = -EOPNOTSUPP; + else if (*cci & UCSI_CCI_ERROR) + err = -EIO; + else + err = 0; - if (data) { - ret = ucsi->ops->read_message_in(ucsi, data, size); - if (ret) - return ret; - } + if (!err && data && UCSI_CCI_LENGTH(*cci)) + err = ucsi->ops->read_message_in(ucsi, data, size); - ret = ucsi_acknowledge(ucsi, conn_ack); + /* + * Don't ACK connection change if there was an error. + */ + ret = ucsi_acknowledge(ucsi, err ? false : conn_ack); if (ret) return ret; - return 0; + return err; } static int ucsi_read_error(struct ucsi *ucsi)