From: Sasha Levin <sashal@kernel.org>
To: stable@vger.kernel.org
Cc: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>,
Heikki Krogerus <heikki.krogerus@linux.intel.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Sasha Levin <sashal@kernel.org>
Subject: [PATCH 6.6.y 6/7] usb: typec: ucsi: extract common code for command handling
Date: Fri, 4 Sep 2026 11:58:45 -0400 [thread overview]
Message-ID: <20260904155846.1071149-6-sashal@kernel.org> (raw)
In-Reply-To: <20260904155846.1071149-1-sashal@kernel.org>
From: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
[ Upstream commit 584e8df58942338602c70686b451b5c3093543a1 ]
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.
Tested-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
Link: https://lore.kernel.org/r/20240627-ucsi-rework-interface-v4-6-289ddc6874c7@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[ sashal: Reduced backport -- upstream 584e8df589423 touches 7 file(s), this
backport carries 6. Not backported here:
drivers/usb/typec/ucsi/ucsi_yoga_c630.c
This note is generated from the file lists only; see the resolution record
for the reasoning. ]
Stable-dep-of: eb4573cf2fd8 ("usb: typec: ucsi: use UCSI_TIMEOUT_MS for sync command completion")
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
drivers/usb/typec/ucsi/ucsi.c | 43 +++++++++++++++++++++++
drivers/usb/typec/ucsi/ucsi.h | 5 +++
drivers/usb/typec/ucsi/ucsi_acpi.c | 50 ++++-----------------------
drivers/usb/typec/ucsi/ucsi_ccg.c | 20 ++---------
drivers/usb/typec/ucsi/ucsi_glink.c | 47 ++-----------------------
drivers/usb/typec/ucsi/ucsi_stm32g0.c | 43 ++---------------------
6 files changed, 61 insertions(+), 147 deletions(-)
diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index db45d3034aae0..9f736a34b4f1c 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;
@@ -1683,6 +1725,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 386b06bc7a1a0..04356bbeca0ff 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 <linux/bitops.h>
+#include <linux/completion.h>
#include <linux/device.h>
#include <linux/power_supply.h>
#include <linux/types.h>
@@ -336,6 +337,7 @@ struct ucsi {
#define EVENT_PENDING 0
#define COMMAND_PENDING 1
#define ACK_PENDING 2
+ struct completion complete;
};
#define UCSI_MAX_SVID 5
@@ -391,6 +393,9 @@ void ucsi_altmode_update_active(struct ucsi_connector *con);
int ucsi_suspend(struct ucsi *ucsi);
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 c0c5746117343..d3080aef34b9b 100644
--- a/drivers/usb/typec/ucsi/ucsi_acpi.c
+++ b/drivers/usb/typec/ucsi/ucsi_acpi.c
@@ -21,11 +21,8 @@ struct ucsi_acpi {
struct device *dev;
struct ucsi *ucsi;
void *base;
- struct completion complete;
unsigned long flags;
#define UCSI_ACPI_SUPPRESS_EVENT 0
-#define UCSI_ACPI_COMMAND_PENDING 1
-#define UCSI_ACPI_ACK_PENDING 2
guid_t guid;
u64 cmd;
};
@@ -98,38 +95,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
};
@@ -165,7 +135,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
};
@@ -194,14 +164,14 @@ ucsi_dell_sync_control(struct ucsi *ucsi, u64 command)
* still report the very event that the core is trying to clear.
*/
set_bit(UCSI_ACPI_SUPPRESS_EVENT, &ua->flags);
- ret = ucsi_acpi_sync_control(ucsi, dummycmd);
+ ret = ucsi_sync_control_common(ucsi, dummycmd);
clear_bit(UCSI_ACPI_SUPPRESS_EVENT, &ua->flags);
if (ret < 0)
return ret;
}
- return ucsi_acpi_sync_control(ucsi, cmd);
+ return ucsi_sync_control_common(ucsi, cmd);
}
static const struct ucsi_operations ucsi_dell_ops = {
@@ -239,15 +209,10 @@ static void ucsi_acpi_notify(acpi_handle handle, u32 event, void *data)
if (ret)
return;
- if (UCSI_CCI_CONNECTOR(cci) &&
- !test_bit(UCSI_ACPI_SUPPRESS_EVENT, &ua->flags))
- ucsi_connector_change(ua->ucsi, UCSI_CCI_CONNECTOR(cci));
+ if (test_bit(UCSI_ACPI_SUPPRESS_EVENT, &ua->flags))
+ cci &= ~GENMASK(7, 1);
- if (cci & UCSI_CCI_ACK_COMPLETE && test_bit(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)
@@ -281,7 +246,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 185d67745a540..b8722874525cc 100644
--- a/drivers/usb/typec/ucsi/ucsi_ccg.c
+++ b/drivers/usb/typec/ucsi/ucsi_ccg.c
@@ -216,8 +216,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];
@@ -587,7 +585,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;
if (UCSI_COMMAND(uc->last_cmd_sent) == UCSI_SET_NEW_CAM &&
@@ -602,15 +599,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);
err_put:
pm_runtime_put_sync(uc->dev);
mutex_unlock(&uc->lock);
@@ -643,12 +633,7 @@ 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));
-
- if (test_bit(DEV_CMD_PENDING, &uc->flags) &&
- cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))
- complete(&uc->complete);
+ ucsi_notify_common(uc->ucsi, cci);
err_clear_irq:
ccg_write(uc, CCGX_RAB_INTR_REG, &intr_reg, sizeof(intr_reg));
@@ -1375,7 +1360,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 9427693b9593c..fc232e8c149fe 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;
spinlock_t state_lock;
@@ -173,35 +169,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);
@@ -234,7 +201,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,
@@ -258,14 +225,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;
@@ -275,14 +240,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);
spin_lock_init(&ucsi->state_lock);
mutex_init(&ucsi->lock);
diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c
index e39a3ef662268..d948c3f579e18 100644
--- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c
+++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c
@@ -61,10 +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 ACK_PENDING 2
const char *fw_name;
struct ucsi *ucsi;
bool suspended;
@@ -406,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;
@@ -448,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;
}
@@ -463,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,
};
@@ -664,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);
--
2.53.0
next prev parent reply other threads:[~2026-09-04 15:58 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-03 12:40 FAILED: patch "[PATCH] usb: typec: ucsi: use UCSI_TIMEOUT_MS for sync command" failed to apply to 6.6-stable tree gregkh
2026-09-04 15:58 ` [PATCH 6.6.y 1/7] usb: typec: ucsi: move ucsi_acknowledge() from ucsi_read_error() Sasha Levin
2026-09-04 15:58 ` [PATCH 6.6.y 2/7] usb: typec: ucsi: simplify command sending API Sasha Levin
2026-09-04 15:58 ` [PATCH 6.6.y 3/7] usb: typec: ucsi: split read operation Sasha Levin
2026-09-04 15:58 ` [PATCH 6.6.y 4/7] usb: typec: ucsi: rework command execution functions Sasha Levin
2026-09-04 15:58 ` [PATCH 6.6.y 5/7] usb: typec: ucsi: inline ucsi_read_message_in Sasha Levin
2026-09-04 15:58 ` Sasha Levin [this message]
2026-09-04 15:58 ` [PATCH 6.6.y 7/7] usb: typec: ucsi: use UCSI_TIMEOUT_MS for sync command completion Sasha Levin
2026-09-10 1:36 ` [PATCH 6.6.y] " Huang Wei
2026-09-11 11:21 ` Sasha Levin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260904155846.1071149-6-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=dmitry.baryshkov@linaro.org \
--cc=gregkh@linuxfoundation.org \
--cc=heikki.krogerus@linux.intel.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox