Re: [PATCH v2 1/2] usb: typec: ucsi: gaokun: Distinguish empty updates from EC errors

From: Pengyu Luo

Date: Tue Dec 01 2026 - 04:26:27 EST


On Mon, Nov 30, 2026 at 9:42 PM <getfeus@xxxxxxxxx> wrote:
>
> A failed port-register read returns the same sentinel as an empty update
> mask. The USB notification handler then acknowledges an update even
> though it has no valid register contents. Refresh also overwrites the
> port count that bounds the array allocated at probe.
>
> Check the initial port count against the register layout and retain that
> count for the lifetime of the device. Reject inconsistent counts and
> out-of-range mask bits before updating a port. Return a connector number
> for a valid update, zero for an empty mask, or a negative error.
>

This is a quick review, I will conduct a full review later when I am free.

You hardcoded the number of ports as 2 in the main EC module

https://lore.kernel.org/platform-driver-x86/CA+m4KrC4-oEkg2_+_B=kn5ObqAd1ZB+f+hpe53Cmqx47LHnGDQ@xxxxxxxxxxxxxx/

Inconsistent with here. Should we change the logic in the ucsi module?
Either hardcode or reading from registers.

> Keep the empty acknowledgment for a genuine USB event. When handling a
> connector timeout, consume an update only if it belongs to that connector.
>
> Fixes: 00327d7f2c8c ("usb: typec: ucsi: add Huawei Matebook E Go ucsi driver")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Lee Youngjoon <getfeus@xxxxxxxxx>
> ---
> Changes in v2:
> - Add Cc: stable@xxxxxxxxxxxxxxx, as requested by Heikki Krogerus.
>
> drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 55 +++++++++++++--------
> 1 file changed, 35 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> index 56f8bbab9..d4c66de48 100644
> --- a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> +++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> @@ -273,23 +273,29 @@ static void gaokun_ucsi_port_update(struct
> gaokun_ucsi_port *port,
> spin_unlock_irqrestore(&port->lock, flags);
> }
>
> -static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec)
> +/* Return a one-based connector number, zero for an empty mask, or an errno. */
> +static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec, int expected)
> {
> - struct gaokun_ucsi_reg ureg;
> + struct gaokun_ucsi_reg reg;
> int ret, idx;
>
> - ret = gaokun_ec_ucsi_get_reg(uec->ec, &ureg);
> + ret = gaokun_ec_ucsi_get_reg(uec->ec, &reg);
> if (ret)
> - return GAOKUN_UCSI_NO_PORT_UPDATE;
> + return ret < 0 ? ret : -EIO;
> + if (reg.num_ports != uec->num_ports)
> + return -EIO;
>
> - uec->num_ports = ureg.num_ports;
> - idx = GET_IDX(ureg.port_updt);
> + if (!reg.port_updt)
> + return 0;
> + if (reg.port_updt & ~(BIT(reg.num_ports) - 1))
> + return -EPROTO;
>
> - if (idx < 0 || idx >= ureg.num_ports)
> - return GAOKUN_UCSI_NO_PORT_UPDATE;
> + idx = expected >= 0 ? expected : GET_IDX(reg.port_updt);
> + if (idx < 0 || idx >= uec->num_ports || !(reg.port_updt & BIT(idx)))
> + return -ENODATA;
>
> - gaokun_ucsi_port_update(&uec->ports[idx], ureg.port_data);
> - return idx;
> + gaokun_ucsi_port_update(&uec->ports[idx], reg.port_data);
> + return idx + 1;
> }
>
> static void gaokun_ucsi_handle_usb_mode(struct gaokun_ucsi_port *port)
> @@ -324,20 +330,20 @@ static void gaokun_ucsi_handle_usb_mode(struct
> gaokun_ucsi_port *port)
> gaokun_ec_ucsi_pan_ack(uec->ec, port->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 idx;
> + int num;
>
> if (!uec->ucsi->connector) { /* slow to register */
> dev_err_ratelimited(uec->dev, "ucsi connector is not initialized yet\n");
> return;
> }
>
> - idx = gaokun_ucsi_refresh(uec);
> - if (idx == GAOKUN_UCSI_NO_PORT_UPDATE)
> - gaokun_ec_ucsi_pan_ack(uec->ec, idx); /* ack directly if no update */
> - else
> - gaokun_ucsi_handle_usb_mode(&uec->ports[idx]);
> + num = gaokun_ucsi_refresh(uec, expected);
> + if (!num && expected == GAOKUN_UCSI_NO_PORT_UPDATE)
> + gaokun_ec_ucsi_pan_ack(uec->ec, GAOKUN_UCSI_NO_PORT_UPDATE);
> + else if (num > 0)
> + gaokun_ucsi_handle_usb_mode(&uec->ports[num - 1]);
> }
>
> /*
> @@ -366,10 +372,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);
> }
> }
>
> @@ -383,7 +392,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_NO_PORT_UPDATE);
> return NOTIFY_OK;
>
> case EC_EVENT_UCSI:
> @@ -411,8 +420,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) / 2)
> + return -EINVAL;
> + uec->num_ports = num_ports;
> +
> uec->ports = devm_kzalloc_objs(dev, *uec->ports, num_ports);
> if (!uec->ports)
> return -ENOMEM;
>

You don't need to quote and reply to the last message.

Best wishes,
Pengyu

>
> On Mon, 30 Nov 2026 07:41:58 -0600, getfeus@xxxxxxxxx wrote:
> > Hi,
> >
> > Changes in v2:
> > - Add Cc: stable@xxxxxxxxxxxxxxx to both patches, as requested by
> > Heikki Krogerus. No code changes.
> >
> >
> > On the Huawei MateBook E Go the EC reports UCSI connector changes (0x21)
> > and USB port updates (0x22) on the same threaded IRQ. The connector
> > callback currently waits up to two seconds for an event that this IRQ
> > thread would dispatch. Also, a failed port-register read is handled like
> > a valid empty mask and can generate a PAN acknowledgment.
> >
> > Patch 1 validates the EC reply before selecting a port or sending an
> > acknowledgment. Patch 2 moves the missing-event recheck to per-connector
> > delayed work. A real USB update retires only the matching recheck. The
> > existing two-second interval and the first-set-bit policy for a real USB
> > event are preserved.
> >
> > The IRQ wait and the mutable port-count bound were also raised in this
> > earlier review:
> > https://lore.kernel.org/r/20260607214621.9586A1F00893@xxxxxxxxxxxxxxx
> >
> > Testing was done against usb-next, building an arm64 Image and modules
> > at each commit. To exercise the firmware-facing behavior without moving
> > cables, substitute the D3 register reply at gaokun_ec_ucsi_get_reg()
> > and capture the connector argument to gaokun_ec_ucsi_pan_ack(). For a
> > two-port reply, use update mask 01 and port data 04 00 02 00. Queue a
> > right-port connector change and let the timeout expire: there should be
> > one D3 read and no D2 write. Next deliver a real 0x22 with update mask
> > 02 and port data 04 00 09 19: the driver should process the right port
> > and request an ACK for port index 1. A valid zero mask on a real 0x22
> > retains the no-port ACK. These cases replay EC outputs on the host and
> > observe the resulting firmware inputs. They do not emulate the EC's
> > decision to generate a new interrupt.
> >
> > The two patches use the existing EC API. The separate EC transport fix
> > is needed for the parent to report all short I2C transfers reliably.
> > This series does not add speculative firmware ACKs or change DP training.
> > The isolated series has been tested in host-side replay. System suspend
> > and resume on hardware remain unverified.
> >
> > OpenAI Codex assisted with source inspection, the fixes, regression tests
> > and drafting this series.
> >
> > Thanks,
> > Lee Youngjoon
> >
> > Lee Youngjoon (2):
> > usb: typec: ucsi: gaokun: Distinguish empty updates from EC errors
> > usb: typec: ucsi: gaokun: Defer missing USB events per connector
> >
> > drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 138 ++++++++++++--------
> > 1 file changed, 85 insertions(+), 53 deletions(-)
> >
> >
> > base-commit: 90d6263e867be7089b3d3323bef8d2accd327c77