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 2/3] usb: typec: ucsi: gaokun: Validate port updates before acknowledging them
Date: Sat, 5 Dec 2026 09:29:22 +0900 [thread overview]
Message-ID: <20261205002923.1370-3-getfeus@gmail.com> (raw)
In-Reply-To: <20261205002923.1370-1-getfeus@gmail.com>
The UCSI driver allocates its port array at probe but overwrites the
port count on each refresh. A later EC response can therefore select a
port outside that array. The connector timeout can also consume an
update for another port or send an empty acknowledgment.
Check the probe read and limit the port count to the DCC/DDI pairs in
the EC response. Use that count for the lifetime of the array. On a
connector timeout, handle only an update for that connector. A USB
event still needs a zero acknowledgment when its update mask is empty.
Pass the selected bit or zero to the EC writer. This avoids shifting
by the negative no-update sentinel when writing an empty acknowledgment.
Fixes: 00327d7f2c8c ("usb: typec: ucsi: add Huawei Matebook E Go ucsi driver")
Fixes: 7636f090d02e ("platform: arm64: add Huawei Matebook E Go EC driver")
Cc: stable@vger.kernel.org
Signed-off-by: Lee Youngjoon <getfeus@gmail.com>
---
drivers/platform/arm64/huawei-gaokun-ec.c | 16 +++-----
drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 37 ++++++++++++-------
.../linux/platform_data/huawei-gaokun-ec.h | 4 +-
3 files changed, 30 insertions(+), 27 deletions(-)
diff --git a/drivers/platform/arm64/huawei-gaokun-ec.c b/drivers/platform/arm64/huawei-gaokun-ec.c
index ac21f732d..ed9a1deee 100644
--- a/drivers/platform/arm64/huawei-gaokun-ec.c
+++ b/drivers/platform/arm64/huawei-gaokun-ec.c
@@ -445,27 +445,21 @@ int gaokun_ec_ucsi_get_reg(struct gaokun_ec *ec, struct gaokun_ucsi_reg *ureg)
EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_get_reg);
/**
- * gaokun_ec_ucsi_pan_ack - Ack pin assignment notifications from EC
+ * gaokun_ec_ucsi_write_pan_ack - Ack pin assignment notifications from EC
* @ec: The gaokun_ec structure
- * @port_id: The port id receiving and handling the notifications
+ * @mask: Bit of the port being acknowledged, or zero for an empty update
*
* Ack pin assignment notifications (used by UCSI subdriver).
*
* Return: 0 on success or negative error code.
*/
-int gaokun_ec_ucsi_pan_ack(struct gaokun_ec *ec, int port_id)
+int gaokun_ec_ucsi_write_pan_ack(struct gaokun_ec *ec, u8 mask)
{
- u8 ec_req[] = MKREQ(0x03, UCSI_REG_WRITE, 1);
- u8 data = 1 << port_id;
-
- if (port_id == GAOKUN_UCSI_NO_PORT_UPDATE)
- data = 0;
-
- refill_req_byte(ec_req, &data);
+ u8 ec_req[] = MKREQ(0x03, UCSI_REG_WRITE, 1, mask);
return gaokun_ec_write(ec, ec_req);
}
-EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_pan_ack);
+EXPORT_SYMBOL_GPL(gaokun_ec_ucsi_write_pan_ack);
/* -------------------------------------------------------------------------- */
/* EC Sysfs */
diff --git a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
index 8b322ec4a..22ffc13c4 100644
--- a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
+++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
@@ -33,6 +33,9 @@
#define GAOKUN_HPD_STATE_MASK BIT(4)
#define GAOKUN_HPD_IRQ_MASK BIT(5)
+#define GAOKUN_UCSI_PORT_DATA_SIZE 2 /* DCC and DDI bytes per port */
+#define GAOKUN_UCSI_ANY_PORT (-1)
+
#define GET_IDX(updt) (ffs(updt) - 1)
#define CCX_TO_ORI(ccx) (++(ccx) % 3) /* convert ccx to enum typec_orientation */
@@ -217,7 +220,7 @@ static void gaokun_ucsi_port_update(struct gaokun_ucsi_port *port,
const u8 *port_data)
{
struct gaokun_ucsi *uec = port->ucsi;
- int offset = port->idx * 2; /* every port has 2 Bytes data */
+ int offset = port->idx * GAOKUN_UCSI_PORT_DATA_SIZE;
unsigned long flags;
u8 dcc, ddi;
@@ -274,7 +277,7 @@ static void gaokun_ucsi_port_update(struct gaokun_ucsi_port *port,
}
/* Return a one-based connector number, zero for an empty mask, or an errno. */
-static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec)
+static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec, int expected)
{
struct gaokun_ucsi_reg ureg;
int ret, idx;
@@ -283,12 +286,11 @@ static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec)
if (ret)
return ret < 0 ? ret : -EIO;
- uec->num_ports = ureg.num_ports;
if (!ureg.port_updt)
return 0;
- idx = GET_IDX(ureg.port_updt);
- if (idx >= ureg.num_ports)
+ idx = expected >= 0 ? expected : GET_IDX(ureg.port_updt);
+ if (idx >= uec->num_ports || !(ureg.port_updt & BIT(idx)))
return -ENODATA;
gaokun_ucsi_port_update(&uec->ports[idx], ureg.port_data);
@@ -324,10 +326,10 @@ static void gaokun_ucsi_handle_usb_mode(struct gaokun_ucsi_port *port)
connector_status_connected :
connector_status_disconnected);
- gaokun_ec_ucsi_pan_ack(uec->ec, port->idx);
+ gaokun_ec_ucsi_write_pan_ack(uec->ec, BIT(idx));
}
-static void gaokun_ucsi_usb_notify_ind(struct gaokun_ucsi *uec)
+static void gaokun_ucsi_usb_notify_ind(struct gaokun_ucsi *uec, int expected)
{
int num;
@@ -336,9 +338,9 @@ static void gaokun_ucsi_usb_notify_ind(struct gaokun_ucsi *uec)
return;
}
- num = gaokun_ucsi_refresh(uec);
- if (!num)
- gaokun_ec_ucsi_pan_ack(uec->ec, GAOKUN_UCSI_NO_PORT_UPDATE);
+ num = gaokun_ucsi_refresh(uec, expected);
+ if (!num && expected == GAOKUN_UCSI_ANY_PORT)
+ gaokun_ec_ucsi_write_pan_ack(uec->ec, 0);
else if (num > 0)
gaokun_ucsi_handle_usb_mode(&uec->ports[num - 1]);
}
@@ -369,10 +371,13 @@ static void gaokun_ucsi_handle_no_usb_event(struct gaokun_ucsi *uec, int idx)
{
struct gaokun_ucsi_port *port;
+ if (idx < 0 || idx >= uec->num_ports)
+ 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);
+ gaokun_ucsi_usb_notify_ind(uec, idx);
}
}
@@ -386,7 +391,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_usb_notify_ind(uec, GAOKUN_UCSI_ANY_PORT);
return NOTIFY_OK;
case EC_EVENT_UCSI:
@@ -414,8 +419,14 @@ static int gaokun_ucsi_ports_init(struct gaokun_ucsi *uec)
int i, ret, num_ports;
u32 port;
- gaokun_ec_ucsi_get_reg(uec->ec, &ureg);
+ ret = gaokun_ec_ucsi_get_reg(uec->ec, &ureg);
+ if (ret)
+ return ret < 0 ? ret : -EIO;
num_ports = ureg.num_ports;
+ if (!num_ports || num_ports > ARRAY_SIZE(ureg.port_data) / GAOKUN_UCSI_PORT_DATA_SIZE)
+ return -EINVAL;
+ uec->num_ports = num_ports;
+
uec->ports = devm_kzalloc_objs(dev, *uec->ports, num_ports);
if (!uec->ports)
return -ENOMEM;
diff --git a/include/linux/platform_data/huawei-gaokun-ec.h b/include/linux/platform_data/huawei-gaokun-ec.h
index faa15d315..9e7b8ab93 100644
--- a/include/linux/platform_data/huawei-gaokun-ec.h
+++ b/include/linux/platform_data/huawei-gaokun-ec.h
@@ -13,8 +13,6 @@
#define GAOKUN_UCSI_READ_SIZE (GAOKUN_UCSI_CCI_SIZE + GAOKUN_UCSI_MSGI_SIZE)
#define GAOKUN_UCSI_WRITE_SIZE 24 /* 8B CTRL, 16B MSGO */
-#define GAOKUN_UCSI_NO_PORT_UPDATE (-1)
-
#define GAOKUN_SMART_CHARGE_DATA_SIZE 4 /* mode, delay, start, end */
/* -------------------------------------------------------------------------- */
@@ -74,6 +72,6 @@ int gaokun_ec_ucsi_write(struct gaokun_ec *ec,
const u8 req[GAOKUN_UCSI_WRITE_SIZE]);
int gaokun_ec_ucsi_get_reg(struct gaokun_ec *ec, struct gaokun_ucsi_reg *ureg);
-int gaokun_ec_ucsi_pan_ack(struct gaokun_ec *ec, int port_id);
+int gaokun_ec_ucsi_write_pan_ack(struct gaokun_ec *ec, u8 mask);
#endif /* __HUAWEI_GAOKUN_EC_H__ */
--
2.53.0
next prev parent reply other threads:[~2026-12-05 1:36 UTC|newest]
Thread overview: 5+ 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 ` Lee Youngjoon [this message]
2026-12-05 0:29 ` [PATCH v5 3/3] usb: typec: ucsi: gaokun: Defer missing USB events per connector Lee Youngjoon
2026-12-05 1:49 ` [PATCH v5 0/3] usb: typec: ucsi: gaokun: Validate and defer port updates sashiko-bot
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-3-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.