* [PATCH v4 0/2] usb: typec: ucsi: gaokun: Validate and defer port updates @ 2026-12-03 18:58 getfeus 2026-12-03 19:09 ` [PATCH v4 1/2] usb: typec: ucsi: gaokun: Distinguish empty updates from EC errors Lee Youngjoon 2026-12-03 19:10 ` [PATCH v4 2/2] usb: typec: ucsi: gaokun: Defer missing USB events per connector Lee Youngjoon 0 siblings, 2 replies; 5+ messages in thread From: getfeus @ 2026-12-03 18:58 UTC (permalink / raw) To: Pengyu Luo, Heikki Krogerus, Greg Kroah-Hartman Cc: Hans de Goede, Ilpo Järvinen, Bryan O'Donoghue, platform-driver-x86, linux-usb, linux-kernel, stable Hi, These patches distinguish a failed EC UCSI_REG_READ (0xd3) from a successful read with an empty update mask, and move the connector fallback out of the EC IRQ notifier so a later USB event can run. Changes since v3: - Remove redundant runtime count/mask checks and an unrelated rename. - Explain when an empty USB event requires a zero PAN acknowledgment. - Regenerate and apply both patches against the stated usb-next base. Patch 1 supersedes the PAN ACK patch (2/2) in the earlier platform series [1]; please do not apply that older patch. Its short I2C transfer patch (1/2) remains independent and applies with this series. Both commits cross-built as an arm64 Image and modules. Host tests cover EC read failures, empty and matching updates, per-port fallbacks and the PAN ACK bytes, also with the short-transfer fix. This revision has not been tested on hardware. OpenAI Codex assisted with source review, tests and drafting. [1] https://lore.kernel.org/r/CA+m4KrC4-oEkg2_+_B=kn5ObqAd1ZB+f+hpe53Cmqx47LHnGDQ@mail.gmail.com/ 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/platform/arm64/huawei-gaokun-ec.c | 16 +-- drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 135 +++++++++++------- .../linux/platform_data/huawei-gaokun-ec.h | 4 +- 3 files changed, 89 insertions(+), 66 deletions(-) base-commit: 9357829ac604c28510b71b154cb6630af81226ff ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 1/2] usb: typec: ucsi: gaokun: Distinguish empty updates from EC errors 2026-12-03 18:58 [PATCH v4 0/2] usb: typec: ucsi: gaokun: Validate and defer port updates getfeus @ 2026-12-03 19:09 ` Lee Youngjoon 2026-12-04 13:15 ` Ilpo Järvinen 2026-12-03 19:10 ` [PATCH v4 2/2] usb: typec: ucsi: gaokun: Defer missing USB events per connector Lee Youngjoon 1 sibling, 1 reply; 5+ messages in thread From: Lee Youngjoon @ 2026-12-03 19:09 UTC (permalink / raw) To: Pengyu Luo, Heikki Krogerus, Greg Kroah-Hartman Cc: Hans de Goede, Ilpo Järvinen, Bryan O'Donoghue, platform-driver-x86, linux-usb, linux-kernel, stable The EC UCSI_REG_READ (0xd3) response contains a port count, an update mask and per-port state. A failed read currently looks like an empty update, so the USB event handler sends a zero PAN acknowledgment without having read a valid response. Keep read errors separate from an empty mask. Send a zero acknowledgment only when a USB event's successful read reports no update. A connector timeout with no matching update sends no acknowledgment. Bound the port array at probe by the space available for per-port state in the response, and select only a port within that array. Construct the acknowledgment mask in the UCSI driver after selecting a port and pass the mask to the EC writer. This also avoids shifting by the negative no-update sentinel in the EC driver. 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 | 52 ++++++++++++------- .../linux/platform_data/huawei-gaokun-ec.h | 4 +- 3 files changed, 39 insertions(+), 33 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 56f8bbab9..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; @@ -273,23 +276,25 @@ 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; int ret, idx; ret = gaokun_ec_ucsi_get_reg(uec->ec, &ureg); if (ret) - return GAOKUN_UCSI_NO_PORT_UPDATE; + return ret < 0 ? ret : -EIO; - uec->num_ports = ureg.num_ports; - idx = GET_IDX(ureg.port_updt); + if (!ureg.port_updt) + return 0; - if (idx < 0 || idx >= ureg.num_ports) - return GAOKUN_UCSI_NO_PORT_UPDATE; + 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); - return idx; + return idx + 1; } static void gaokun_ucsi_handle_usb_mode(struct gaokun_ucsi_port *port) @@ -321,23 +326,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); + else if (num > 0) + gaokun_ucsi_handle_usb_mode(&uec->ports[num - 1]); } /* @@ -366,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); } } @@ -383,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: @@ -411,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 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v4 1/2] usb: typec: ucsi: gaokun: Distinguish empty updates from EC errors 2026-12-03 19:09 ` [PATCH v4 1/2] usb: typec: ucsi: gaokun: Distinguish empty updates from EC errors Lee Youngjoon @ 2026-12-04 13:15 ` Ilpo Järvinen 0 siblings, 0 replies; 5+ messages in thread From: Ilpo Järvinen @ 2026-12-04 13:15 UTC (permalink / raw) To: Lee Youngjoon Cc: Pengyu Luo, Heikki Krogerus, Greg Kroah-Hartman, Hans de Goede, Bryan O'Donoghue, platform-driver-x86, linux-usb, LKML, stable On Thu, 3 Dec 2026, Lee Youngjoon wrote: > The EC UCSI_REG_READ (0xd3) response contains a port count, an update > mask and per-port state. A failed read currently looks like an empty > update, so the USB event handler sends a zero PAN acknowledgment without > having read a valid response. > > Keep read errors separate from an empty mask. Send a zero acknowledgment > only when a USB event's successful read reports no update. A connector > timeout with no matching update sends no acknowledgment. Bound the port > array at probe by the space available for per-port state in the response, > and select only a port within that array. > > Construct the acknowledgment mask in the UCSI driver after selecting a > port and pass the mask to the EC writer. This also avoids shifting by > the negative no-update sentinel in the EC driver. > > 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 | 52 ++++++++++++------- > .../linux/platform_data/huawei-gaokun-ec.h | 4 +- > 3 files changed, 39 insertions(+), 33 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 56f8bbab9..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; > > @@ -273,23 +276,25 @@ 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; > int ret, idx; > > ret = gaokun_ec_ucsi_get_reg(uec->ec, &ureg); > if (ret) > - return GAOKUN_UCSI_NO_PORT_UPDATE; > + return ret < 0 ? ret : -EIO; > > - uec->num_ports = ureg.num_ports; > - idx = GET_IDX(ureg.port_updt); > + if (!ureg.port_updt) > + return 0; > > - if (idx < 0 || idx >= ureg.num_ports) > - return GAOKUN_UCSI_NO_PORT_UPDATE; > + 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); > - return idx; > + return idx + 1; > } > > static void gaokun_ucsi_handle_usb_mode(struct gaokun_ucsi_port *port) > @@ -321,23 +326,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); > + else if (num > 0) > + gaokun_ucsi_handle_usb_mode(&uec->ports[num - 1]); > } > > /* > @@ -366,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); > } > } > > @@ -383,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: > @@ -411,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__ */ > The patch is corrupted somewhere. This look also quite messy. I wonder if the return value conversion have to be part of it or if they could be put into a separate change in the series? -- i. ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 2/2] usb: typec: ucsi: gaokun: Defer missing USB events per connector 2026-12-03 18:58 [PATCH v4 0/2] usb: typec: ucsi: gaokun: Validate and defer port updates getfeus 2026-12-03 19:09 ` [PATCH v4 1/2] usb: typec: ucsi: gaokun: Distinguish empty updates from EC errors Lee Youngjoon @ 2026-12-03 19:10 ` Lee Youngjoon 2026-12-04 13:18 ` Ilpo Järvinen 1 sibling, 1 reply; 5+ messages in thread From: Lee Youngjoon @ 2026-12-03 19:10 UTC (permalink / raw) To: Pengyu Luo, Heikki Krogerus, Greg Kroah-Hartman Cc: Hans de Goede, Ilpo Järvinen, Bryan O'Donoghue, platform-driver-x86, linux-usb, linux-kernel, stable 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 | 99 ++++++++++++--------- 1 file changed, 58 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..3ed39c90e 100644 --- a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c +++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c @@ -8,9 +8,10 @@ #include <drm/bridge/aux-bridge.h> #include <linux/auxiliary_bus.h> #include <linux/bitops.h> -#include <linux/completion.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 +20,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 +83,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 +110,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 +335,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 +403,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 +413,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 +448,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 +534,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 +560,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 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v4 2/2] usb: typec: ucsi: gaokun: Defer missing USB events per connector 2026-12-03 19:10 ` [PATCH v4 2/2] usb: typec: ucsi: gaokun: Defer missing USB events per connector Lee Youngjoon @ 2026-12-04 13:18 ` Ilpo Järvinen 0 siblings, 0 replies; 5+ messages in thread From: Ilpo Järvinen @ 2026-12-04 13:18 UTC (permalink / raw) To: Lee Youngjoon Cc: Pengyu Luo, Heikki Krogerus, Greg Kroah-Hartman, Hans de Goede, Bryan O'Donoghue, platform-driver-x86, linux-usb, LKML, stable On Thu, 3 Dec 2026, Lee Youngjoon wrote: > 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 | 99 ++++++++++++--------- > 1 file changed, 58 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..3ed39c90e 100644 > --- a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c > +++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c > @@ -8,9 +8,10 @@ > #include <drm/bridge/aux-bridge.h> > #include <linux/auxiliary_bus.h> > #include <linux/bitops.h> > -#include <linux/completion.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 +20,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 +83,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 +110,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 +335,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)) Add include. This patch too looked somehow corrupted. -- i. > 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 +403,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 +413,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 +448,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 +534,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 +560,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); > ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 13:18 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-03 18:58 [PATCH v4 0/2] usb: typec: ucsi: gaokun: Validate and defer port updates getfeus 2026-12-03 19:09 ` [PATCH v4 1/2] usb: typec: ucsi: gaokun: Distinguish empty updates from EC errors Lee Youngjoon 2026-12-04 13:15 ` Ilpo Järvinen 2026-12-03 19:10 ` [PATCH v4 2/2] usb: typec: ucsi: gaokun: Defer missing USB events per connector Lee Youngjoon 2026-12-04 13:18 ` Ilpo Järvinen
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox