The EC UCSI_REG_READ (0xd3) response contains a port count, an update mask and per-port state. gaokun_ucsi_refresh() returns the same sentinel for a failed read and for a successful read with an empty mask. The caller sends an empty PAN acknowledgment in both cases. Return a one-based connector number for a valid update. This leaves zero for an empty mask and negative values for errors. Propagate a negative read error, map a positive EC status to -EIO, and return -ENODATA when the update names a port outside the reported range. Only acknowledge an empty update when the read actually returned one. Fixes: 00327d7f2c8c ("usb: typec: ucsi: add Huawei Matebook E Go ucsi driver") Cc: stable@vger.kernel.org Signed-off-by: Lee Youngjoon --- drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c | 25 ++++++++++++--------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c index 56f8bbab9..8b322ec4a 100644 --- a/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c +++ b/drivers/usb/typec/ucsi/ucsi_huawei_gaokun.c @@ -273,6 +273,7 @@ static void gaokun_ucsi_port_update(struct gaokun_ucsi_port *port, spin_unlock_irqrestore(&port->lock, flags); } +/* Return a one-based connector number, zero for an empty mask, or an errno. */ static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec) { struct gaokun_ucsi_reg ureg; @@ -280,16 +281,18 @@ static int gaokun_ucsi_refresh(struct gaokun_ucsi *uec) 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 = GET_IDX(ureg.port_updt); + if (idx >= ureg.num_ports) + 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) @@ -326,18 +329,18 @@ static void gaokun_ucsi_handle_usb_mode(struct gaokun_ucsi_port *port) static void gaokun_ucsi_usb_notify_ind(struct gaokun_ucsi *uec) { - 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); + if (!num) + 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]); } /* -- 2.53.0