Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Lee Youngjoon <getfeus@gmail.com>
To: Pengyu Luo <mitltlatltl@gmail.com>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Hans de Goede" <hansg@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Bryan O'Donoghue" <bryan.odonoghue@linaro.org>,
	platform-driver-x86@vger.kernel.org, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH v5 3/3] usb: typec: ucsi: gaokun: Defer missing USB events per connector
Date: Sat,  5 Dec 2026 09:29:23 +0900	[thread overview]
Message-ID: <20261205002923.1370-4-getfeus@gmail.com> (raw)
In-Reply-To: <20261205002923.1370-1-getfeus@gmail.com>

The EC IRQ thread dispatches both connector and USB events through a
blocking notifier. Waiting in the connector callback holds up the IRQ
thread that would deliver the USB event. The first USB event also
completes every port's wait, and those completions are never rearmed.

Schedule the existing two-second fallback on a freezable workqueue and
return from the notifier. Track each connector independently. A USB
update cancels only the matching fallback. A newer connector change
moves its deadline even if the older worker is already running.

Serialize mode updates and acknowledgments from the two paths. Stop
notifications and drain per-port work before unregistering UCSI.

Fixes: 00327d7f2c8c ("usb: typec: ucsi: add Huawei Matebook E Go ucsi driver")
Cc: stable@vger.kernel.org
Signed-off-by: Lee Youngjoon <getfeus@gmail.com>
---
 drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 100 ++++++++++++--------
 1 file changed, 59 insertions(+), 41 deletions(-)

diff --git a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
index 22ffc13c4..6a9d50c1c 100644
--- a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
+++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
@@ -8,9 +8,11 @@
 #include <drm/bridge/aux-bridge.h>
 #include <linux/auxiliary_bus.h>
 #include <linux/bitops.h>
-#include <linux/completion.h>
+#include <linux/compiler.h>
 #include <linux/container_of.h>
+#include <linux/jiffies.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/notifier.h>
 #include <linux/of.h>
 #include <linux/platform_data/huawei-gaokun-ec.h>
@@ -19,13 +21,15 @@
 #include <linux/usb/typec_altmode.h>
 #include <linux/usb/typec_dp.h>
 #include <linux/usb/typec_mux.h>
-#include <linux/workqueue_types.h>
+#include <linux/workqueue.h>
 
 #include "ucsi.h"
 
 #define EC_EVENT_UCSI	0x21
 #define EC_EVENT_USB	0x22
 
+#define USB_EVENT_TIMEOUT	(2 * HZ)
+
 #define GAOKUN_CCX_MASK		GENMASK(1, 0)
 #define GAOKUN_MUX_MASK		GENMASK(3, 2)
 
@@ -80,7 +84,9 @@ struct gaokun_ucsi_reg {
 } __packed;
 
 struct gaokun_ucsi_port {
-	struct completion usb_ack;
+	struct delayed_work usb_fallback;
+	unsigned long usb_fallback_deadline;
+	bool usb_fallback_pending;
 	spinlock_t lock; /* serializing port resource access */
 
 	struct gaokun_ucsi *ucsi;
@@ -105,6 +111,7 @@ struct gaokun_ucsi {
 	struct gaokun_ucsi_port *ports;
 	struct device *dev;
 	struct delayed_work work;
+	struct mutex usb_lock; /* serialize EC mode and PAN operations */
 	struct notifier_block nb;
 	u16 version;
 	u8 num_ports;
@@ -329,56 +336,63 @@ static void gaokun_ucsi_handle_usb_mode(struct gaokun_ucsi_port *port)
 	gaokun_ec_ucsi_write_pan_ack(uec->ec, BIT(idx));
 }
 
-static void gaokun_ucsi_usb_notify_ind(struct gaokun_ucsi *uec, int expected)
+static void gaokun_ucsi_usb_event(struct gaokun_ucsi *uec)
 {
+	struct gaokun_ucsi_port *port;
 	int num;
 
-	if (!uec->ucsi->connector) { /* slow to register */
-		dev_err_ratelimited(uec->dev, "ucsi connector is not initialized yet\n");
+	guard(mutex)(&uec->usb_lock);
+	if (!READ_ONCE(uec->ucsi->connector))
 		return;
-	}
 
-	num = gaokun_ucsi_refresh(uec, expected);
-	if (!num && expected == GAOKUN_UCSI_ANY_PORT)
+	num = gaokun_ucsi_refresh(uec, GAOKUN_UCSI_ANY_PORT);
+	if (!num)
 		gaokun_ec_ucsi_write_pan_ack(uec->ec, 0);
-	else if (num > 0)
-		gaokun_ucsi_handle_usb_mode(&uec->ports[num - 1]);
+	if (num <= 0)
+		return;
+
+	port = &uec->ports[num - 1];
+	gaokun_ucsi_handle_usb_mode(port);
+	port->usb_fallback_pending = false;
+	cancel_delayed_work(&port->usb_fallback);
 }
 
-/*
- * When inserting, 2 UCSI events(connector change) are followed by USB events.
- * If we received one USB event, that means USB events are not blocked, so we
- * can complelte for all ports, and we should signal all events.
- */
-static void gaokun_ucsi_complete_usb_ack(struct gaokun_ucsi *uec)
+static void gaokun_ucsi_usb_fallback(struct work_struct *work)
 {
-	struct gaokun_ucsi_port *port;
-	int idx = 0;
+	struct gaokun_ucsi_port *port =
+		container_of(to_delayed_work(work), struct gaokun_ucsi_port, usb_fallback);
+	struct gaokun_ucsi *uec = port->ucsi;
+	unsigned long now;
 
-	while (idx < uec->num_ports) {
-		port = &uec->ports[idx++];
-		if (!completion_done(&port->usb_ack))
-			complete_all(&port->usb_ack);
+	guard(mutex)(&uec->usb_lock);
+	if (!port->usb_fallback_pending || !READ_ONCE(uec->ucsi->connector))
+		return;
+
+	/* A newer connector notification may have moved the deadline. */
+	now = jiffies;
+	if (time_before(now, port->usb_fallback_deadline)) {
+		mod_delayed_work(system_freezable_wq, &port->usb_fallback,
+				 port->usb_fallback_deadline - now);
+		return;
 	}
+
+	port->usb_fallback_pending = false;
+	if (gaokun_ucsi_refresh(uec, port->idx) > 0)
+		gaokun_ucsi_handle_usb_mode(port);
 }
 
-/*
- * USB event is necessary for enabling altmode, the event should follow
- * UCSI event, if not after timeout(this notify may be disabled somehow),
- * then force to enable altmode.
- */
-static void gaokun_ucsi_handle_no_usb_event(struct gaokun_ucsi *uec, int idx)
+static void gaokun_ucsi_queue_usb_fallback(struct gaokun_ucsi *uec, unsigned int num)
 {
 	struct gaokun_ucsi_port *port;
 
-	if (idx < 0 || idx >= uec->num_ports)
+	if (!num || num > uec->num_ports || !READ_ONCE(uec->ucsi->connector))
 		return;
 
-	port = &uec->ports[idx];
-	if (!wait_for_completion_timeout(&port->usb_ack, 2 * HZ)) {
-		dev_warn(uec->dev, "No USB EVENT, triggered by UCSI EVENT");
-		gaokun_ucsi_usb_notify_ind(uec, idx);
-	}
+	guard(mutex)(&uec->usb_lock);
+	port = &uec->ports[num - 1];
+	port->usb_fallback_deadline = jiffies + USB_EVENT_TIMEOUT;
+	port->usb_fallback_pending = true;
+	mod_delayed_work(system_freezable_wq, &port->usb_fallback, USB_EVENT_TIMEOUT);
 }
 
 static int gaokun_ucsi_notify(struct notifier_block *nb,
@@ -390,8 +404,7 @@ static int gaokun_ucsi_notify(struct notifier_block *nb,
 
 	switch (action) {
 	case EC_EVENT_USB:
-		gaokun_ucsi_complete_usb_ack(uec);
-		gaokun_ucsi_usb_notify_ind(uec, GAOKUN_UCSI_ANY_PORT);
+		gaokun_ucsi_usb_event(uec);
 		return NOTIFY_OK;
 
 	case EC_EVENT_UCSI:
@@ -401,7 +414,7 @@ static int gaokun_ucsi_notify(struct notifier_block *nb,
 
 		ucsi_notify_common(uec->ucsi, cci);
 		if (UCSI_CCI_CONNECTOR(cci))
-			gaokun_ucsi_handle_no_usb_event(uec, UCSI_CCI_CONNECTOR(cci) - 1);
+			gaokun_ucsi_queue_usb_fallback(uec, UCSI_CCI_CONNECTOR(cci));
 
 		return NOTIFY_OK;
 
@@ -436,7 +449,7 @@ static int gaokun_ucsi_ports_init(struct gaokun_ucsi *uec)
 		ucsi_port->ccx = USBC_CCX_NONE;
 		ucsi_port->idx = i;
 		ucsi_port->ucsi = uec;
-		init_completion(&ucsi_port->usb_ack);
+		INIT_DELAYED_WORK(&ucsi_port->usb_fallback, gaokun_ucsi_usb_fallback);
 		spin_lock_init(&ucsi_port->lock);
 	}
 
@@ -522,6 +535,7 @@ static int gaokun_ucsi_probe(struct auxiliary_device *adev,
 	uec->version = UCSI_VERSION_1_0;
 	uec->nb.notifier_call = gaokun_ucsi_notify;
 
+	mutex_init(&uec->usb_lock);
 	INIT_DELAYED_WORK(&uec->work, gaokun_ucsi_register_worker);
 
 	ret = gaokun_ucsi_ports_init(uec);
@@ -547,10 +561,14 @@ static void gaokun_ucsi_remove(struct auxiliary_device *adev)
 	int i;
 
 	disable_delayed_work_sync(&uec->work);
-	if (uec->registered) {
+	if (uec->registered)
 		gaokun_ec_unregister_notify(uec->ec, &uec->nb);
+
+	for (i = 0; i < uec->num_ports; i++)
+		disable_delayed_work_sync(&uec->ports[i].usb_fallback);
+
+	if (uec->registered)
 		ucsi_unregister(uec->ucsi);
-	}
 
 	for (i = 0; i < uec->num_ports; ++i)
 		typec_mux_put(uec->ports[i].typec_mux);
-- 
2.53.0


      parent reply	other threads:[~2026-12-05  1:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  0:29 [PATCH v5 0/3] usb: typec: ucsi: gaokun: Validate and defer port updates Lee Youngjoon
2026-12-05  0:29 ` [PATCH v5 1/3] usb: typec: ucsi: gaokun: Return connector numbers from refresh Lee Youngjoon
2026-12-05  0:29 ` [PATCH v5 2/3] usb: typec: ucsi: gaokun: Validate port updates before acknowledging them Lee Youngjoon
2026-12-05  0:29 ` Lee Youngjoon [this message]

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=20261205002923.1370-4-getfeus@gmail.com \
    --to=getfeus@gmail.com \
    --cc=bryan.odonoghue@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=hansg@kernel.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mitltlatltl@gmail.com \
    --cc=platform-driver-x86@vger.kernel.org \
    --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