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

From: Pengyu Luo

Date: Thu Dec 03 2026 - 10:41:19 EST


On Tue, Dec 1, 2026 at 6:52 PM Lee Youngjoon <getfeus@xxxxxxxxx> wrote:
>

Hi, Youngjoon. Patch did not apply cleanly with the base commit you provided.

> A failed port-register read returns the same sentinel as an empty update
> mask. The USB notification handler then acknowledges an update without
> valid register contents. Refresh also overwrites the port count used to
> bound the array allocated at probe.
>
> Validate the port count reported by the EC against the space for port
> records in the D3 reply. Use that count for the lifetime of the device.
> Reject inconsistent counts and out-of-range update bits before using a
> port. On a connector timeout, consume only an update for that connector.
>

Please use the register name or 0xd3, I was just a bit confused about
the only _D3_.

> Build the PAN acknowledgment mask in the UCSI driver after selecting a
> valid port. Write zero for a genuinely empty USB event. This also avoids
> shifting by the negative no-update sentinel in the EC driver. Passing
> the mask to the EC driver leaves it with no need for a second port count
> or another D3 read.
>
> 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@xxxxxxxxxxxxxxx
> Signed-off-by: Lee Youngjoon <getfeus@xxxxxxxxx>
> ---
> drivers/platform/arm64/huawei-gaokun-ec.c | 16 ++---
> drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 64 ++++++++++++-------
> .../linux/platform_data/huawei-gaokun-ec.h | 4 +-
> 3 files changed, 47 insertions(+), 37 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);
>

Then should you update your another series? This conflicts with your
another series. BTW, I didn't read the cover, it is overly verbose, I
think this series is not so complicated, could you please write a
concise one?

> /* --------------------------------------------------------------------------
> */
> /* EC Sysfs */
> diff --git a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c
> index 56f8bbab9..ebb58d4f4 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;
>
> @@ -273,23 +276,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);

ureg => reg, this adds three lines dummy changes. Please revert it.

> 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;
>

I think this is over-checked, is the i2c communication so unreliable?

> - 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)
> @@ -305,7 +314,7 @@ static void gaokun_ucsi_handle_usb_mode(struct
> gaokun_ucsi_port *port)
> port->state.mode = port->mode;
> port->state.alt = &port->dp_alt;
>
> - if (idx >= uec->num_ports) {
> + if (idx < 0 || idx >= uec->num_ports) {
> dev_warn(uec->dev, "altmode port out of range: %d\n", idx);
> return;
> }
> @@ -321,23 +330,23 @@ 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 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_ANY_PORT)
> + gaokun_ec_ucsi_write_pan_ack(uec->ec, 0);

I hardly remember the hardware behaviors. So the empty event is only
passed from the USB event? You can explain this in the commit message
in concise instead of the verbose count and range check explanation.

> + else if (num > 0)
> + gaokun_ucsi_handle_usb_mode(&uec->ports[num - 1]);
> }
>
> /*
> @@ -366,10 +375,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;
> +

If we validate the data from EC all the time, then why don't we write
a wrapper and put the basic check in it.

Best wishes,
Pengyu

> 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 +395,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:
> @@ -411,8 +423,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__ */