From nobody Tue Dec 16 13:11:34 2025 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91C4B2F5483 for ; Fri, 5 Dec 2025 07:07:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764918488; cv=none; b=QMWDnXq1atmJN0jQ1DnPZcixoM3TB8Z40l+2O5zlqRsPnaPiBG72MKHBLYLnKA1Zk5uHseH8IPDl5PbIbaff2hFGO0NMZSwzJeaVGdg2h3qo1/gD2AeCk+icrRubAt+syWJPQSV8+IUcSHnDXOomytbn9/QHWONYPUSVdHMRQzE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764918488; c=relaxed/simple; bh=sdKnm2OkALQygwLRJTEqcD/dBsuHmlcpo8J9y667oQc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=bmnNg9816qMsrk9SvniK3hApCY0A8WYKE+AXwA3M/mFLOwn/g0cegfEC75Kp2dNq62l2PAIRjPHZjB1hz22tcIjLNKhtLf2CYRHb2X+daWi4tWKOINhBQ7QADP93Vjs5Tr0i1UflE2YTTnmjkSJMAanxnuoH2CchFiqeaLTg2n8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=oDn9CHCb; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="oDn9CHCb" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-3438d4ae152so1642185a91.1 for ; Thu, 04 Dec 2025 23:07:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1764918470; x=1765523270; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=NiL8q2/o/Fz5xCoSWc8aZjZWPIN03XsSDmO6PNCHKhE=; b=oDn9CHCbva7+QQczE1BWXuk0cgrULd/GzCLjQ9BTJavvG4HspyA+HBEUbIVbavm3h7 aIY67fTEWONq0Dh7cdVu5wAHiaAUwA7V/RHZEc8wONbiSMKjXoruObPoRWJdopNo8YIc V3sYvkZBBZBDfURnndr6iFpcqxRAbBCnoziYs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764918470; x=1765523270; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=NiL8q2/o/Fz5xCoSWc8aZjZWPIN03XsSDmO6PNCHKhE=; b=H9yqE5n/jDOH/E6I1+mFUeU6EhbimrLf4Dk9W/99cIqxkaPDUSlSymA2rxhE8ndabP F+aCYSZvpG6UDdL4SVx2EGnkKDqiSRKLhCP2sE10MAlNEp0E3IM7BZstMN2ptPjuLpek o9CwLT4teIN+zgVS+BcUJMmmpz6TNuC7Trf4F3T0O3Ltt1iLV5ZkV7l+AmQ5N4jmmgTd /xv11Aoe+gYJ+34Yl7eRIVJeQIRQ7Yoy1x/+wbrQpGQ6dPd/iAquICdEruxCpmu4HW5u pOykECMA87SDjoOFRtUKNbOWS0a1/6v8+LrAtk2mV9f/4K7TwH+QcOJDCdvVojlodfY6 ZaFw== X-Forwarded-Encrypted: i=1; AJvYcCWytVsVowrirVhRHDUoxDtF/KRBoRotJKwa+aPAO5p/EP0Qq8w5UOmSi+Qiq9WjEHcLvLUBGf91PPChLTM=@vger.kernel.org X-Gm-Message-State: AOJu0Yxib/gn92tvPDHm4fQ92R3obkinLB4UBXa2bFvjTjSFXJbhiG3x N67ETCiBOnFjChwXo5K9E+uY35RmTmSMfO36lFM5ClJbh1HO8qhCVvompSISsDbDW2QpTlFv1Nq J7Mg= X-Gm-Gg: ASbGnctuwheGJcRakK2uC9S6+s+Q6V5MZS6Baci2bg3VdKb3aBGguVOgreXLt0X267Q KlAZfvbtn1T+xg7tG6VhHcCO6MtjbU/M3zr66BI5bxDtlvkTlp262O9nA+0/EFyNehdK87FfhO7 psn0FXrDEPPy29Jt4D31cruKyWPi7tbC+uQdYhXcBiI01Oq3UX5Zs/rt3l/Dy9D3FtAVabAFEt5 HVYeLYFwF4J/xxaxXWDDcGtmKM1kwM/IK52dSVkxG+NEePU2huK4Z1OnjyM1WwvgEh9L9S7GJxk oKfQ5qEzoWajUKDzq7pchgeSXU9BI8/S+2STk3mXVO5BTbrhGeNmht+jRucaILdCsEeyTQimwIk 9soyHykaKdsR7N5XABdeL34eL8rG7NwbV3kiyHwFEtDuL8lPoBFzj8faN2CwOywIdqXgDi8zkXx k4hfplEqv09zWTA3usYZbwNeWSsRyqMr5ymlxceko7h44oEv+8+cNYWz0MiM5B/CJx6qbkD5SFr sOLrW8aYFVYZQ== X-Google-Smtp-Source: AGHT+IFpqi/iSMlgQvabJbXmMYjdnfKr+Pb9KtvCHoPQ+Ze8nZa8cMYKLLfbcLFvhdvvsiksuXaa3g== X-Received: by 2002:a17:90a:c883:b0:33b:d74b:179 with SMTP id 98e67ed59e1d1-349126e0e1cmr10402463a91.27.1764918469904; Thu, 04 Dec 2025 23:07:49 -0800 (PST) Received: from yuanhsinte-p620-1.tpe.corp.google.com ([2a00:79e0:201d:8:84c9:5640:4342:99a4]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-bf6a30f363bsm3872830a12.33.2025.12.04.23.07.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Dec 2025 23:07:49 -0800 (PST) From: Hsin-Te Yuan Date: Fri, 05 Dec 2025 15:07:46 +0800 Subject: [PATCH v6] usb: typec: ucsi: Get connector status after enable notifications Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251205-ucsi-v6-1-e2ad16550242@chromium.org> X-B4-Tracking: v=1; b=H4sIAMGEMmkC/23NwU7DMAwG4FeZcqYodpI25cR7IA6J4605bJ0SV m2a+u54UxEVVD79lr/fd1W5ZK7qbXdXhadc83iS0L7sFA3hdOAmJ8kKNToA6JoL1dwQpj170rp NnZLTc+F9vj5rPj4lD7l+jeX2bJ3gsf1TMEEjkyiS49BajO80lPGYL8fXsRzUo2PCtfOLQ3HJg E1OG2MYN5xZOYTFGXERtO0omA6i33B27dzirDhPvXWtTykEt+Hcr0P945w46z1H30fq//2b5/k baKBRz3sBAAA= X-Change-ID: 20251117-ucsi-c2dfe8c006d7 To: Heikki Krogerus , Greg Kroah-Hartman , Guenter Roeck Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Hsin-Te Yuan X-Mailer: b4 0.14.2 Originally, the notification for connector change will be enabled after the first read of the connector status. Therefore, if the event happens during this window, it will be missing and make the status unsynced. Get the connector status only after enabling the notification for connector change to ensure the status is synced. Fixes: c1b0bc2dabfa ("usb: typec: Add support for UCSI interface") Cc: stable@vger.kernel.org # v4.13+ Signed-off-by: Hsin-Te Yuan Reviewed-by: Heikki Krogerus --- Changes in v6: - Free the locks in error path. - Link to v5: https://lore.kernel.org/r/20251205-ucsi-v5-1-488eb89bc9b8@chr= omium.org Changes in v5: - Hold the lock of each connector during the initialization to avoid race condition between initialization and other event handler - Add Fixes tag - Link to v4: https://lore.kernel.org/r/20251125-ucsi-v4-1-8c94568ddaa5@chr= omium.org Changes in v4: - Handle a single connector in ucsi_init_port() and call it in a loop - Link to v3: https://lore.kernel.org/r/20251121-ucsi-v3-1-b1047ca371b8@chr= omium.org Changes in v3: - Seperate the status checking part into a new function called ucsi_init_port() and call it after enabling the notifications - Link to v2: https://lore.kernel.org/r/20251118-ucsi-v2-1-d314d50333e2@chr= omium.org Changes in v2: - Remove unnecessary braces. - Link to v1: https://lore.kernel.org/r/20251117-ucsi-v1-1-1dcbc5ea642b@chr= omium.org --- drivers/usb/typec/ucsi/ucsi.c | 131 +++++++++++++++++++++++---------------= ---- 1 file changed, 73 insertions(+), 58 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 3f568f790f39b0271667e80816270274b8dd3008..3a0471fa4cc980c0512bc71776e= 3984e6cd2cdb7 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -1560,11 +1560,70 @@ static struct fwnode_handle *ucsi_find_fwnode(struc= t ucsi_connector *con) return NULL; } =20 +static void ucsi_init_port(struct ucsi *ucsi, struct ucsi_connector *con) +{ + enum usb_role u_role =3D USB_ROLE_NONE; + int ret; + + /* Get the status */ + ret =3D ucsi_get_connector_status(con, false); + if (ret) { + dev_err(ucsi->dev, "con%d: failed to get status\n", con->num); + return; + } + + if (ucsi->ops->connector_status) + ucsi->ops->connector_status(con); + + switch (UCSI_CONSTAT(con, PARTNER_TYPE)) { + case UCSI_CONSTAT_PARTNER_TYPE_UFP: + case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: + u_role =3D USB_ROLE_HOST; + fallthrough; + case UCSI_CONSTAT_PARTNER_TYPE_CABLE: + typec_set_data_role(con->port, TYPEC_HOST); + break; + case UCSI_CONSTAT_PARTNER_TYPE_DFP: + u_role =3D USB_ROLE_DEVICE; + typec_set_data_role(con->port, TYPEC_DEVICE); + break; + default: + break; + } + + /* Check if there is already something connected */ + if (UCSI_CONSTAT(con, CONNECTED)) { + typec_set_pwr_role(con->port, UCSI_CONSTAT(con, PWR_DIR)); + ucsi_register_partner(con); + ucsi_pwr_opmode_change(con); + ucsi_port_psy_changed(con); + if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) + ucsi_get_partner_identity(con); + if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS) + ucsi_check_cable(con); + } + + /* Only notify USB controller if partner supports USB data */ + if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB))) + u_role =3D USB_ROLE_NONE; + + ret =3D usb_role_switch_set_role(con->usb_role_sw, u_role); + if (ret) + dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n", + con->num, u_role); + + if (con->partner && UCSI_CONSTAT(con, PWR_OPMODE) =3D=3D UCSI_CONSTAT_PWR= _OPMODE_PD) { + ucsi_register_device_pdos(con); + ucsi_get_src_pdos(con); + ucsi_check_altmodes(con); + ucsi_check_connector_capability(con); + } +} + static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *co= n) { struct typec_capability *cap =3D &con->typec_cap; enum typec_accessory *accessory =3D cap->accessory; - enum usb_role u_role =3D USB_ROLE_NONE; u64 command; char *name; int ret; @@ -1659,62 +1718,6 @@ static int ucsi_register_port(struct ucsi *ucsi, str= uct ucsi_connector *con) goto out; } =20 - /* Get the status */ - ret =3D ucsi_get_connector_status(con, false); - if (ret) { - dev_err(ucsi->dev, "con%d: failed to get status\n", con->num); - goto out; - } - - if (ucsi->ops->connector_status) - ucsi->ops->connector_status(con); - - switch (UCSI_CONSTAT(con, PARTNER_TYPE)) { - case UCSI_CONSTAT_PARTNER_TYPE_UFP: - case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: - u_role =3D USB_ROLE_HOST; - fallthrough; - case UCSI_CONSTAT_PARTNER_TYPE_CABLE: - typec_set_data_role(con->port, TYPEC_HOST); - break; - case UCSI_CONSTAT_PARTNER_TYPE_DFP: - u_role =3D USB_ROLE_DEVICE; - typec_set_data_role(con->port, TYPEC_DEVICE); - break; - default: - break; - } - - /* Check if there is already something connected */ - if (UCSI_CONSTAT(con, CONNECTED)) { - typec_set_pwr_role(con->port, UCSI_CONSTAT(con, PWR_DIR)); - ucsi_register_partner(con); - ucsi_pwr_opmode_change(con); - ucsi_port_psy_changed(con); - if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) - ucsi_get_partner_identity(con); - if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS) - ucsi_check_cable(con); - } - - /* Only notify USB controller if partner supports USB data */ - if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB))) - u_role =3D USB_ROLE_NONE; - - ret =3D usb_role_switch_set_role(con->usb_role_sw, u_role); - if (ret) { - dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n", - con->num, u_role); - ret =3D 0; - } - - if (con->partner && UCSI_CONSTAT(con, PWR_OPMODE) =3D=3D UCSI_CONSTAT_PWR= _OPMODE_PD) { - ucsi_register_device_pdos(con); - ucsi_get_src_pdos(con); - ucsi_check_altmodes(con); - ucsi_check_connector_capability(con); - } - trace_ucsi_register_port(con->num, con); =20 out: @@ -1823,16 +1826,28 @@ static int ucsi_init(struct ucsi *ucsi) goto err_unregister; } =20 + /* Delay other interactions with each connector until ucsi_init_port is d= one */ + for (i =3D 0; i < ucsi->cap.num_connectors; i++) + mutex_lock(&connector[i].lock); + /* Enable all supported notifications */ ntfy =3D ucsi_get_supported_notifications(ucsi); command =3D UCSI_SET_NOTIFICATION_ENABLE | ntfy; ret =3D ucsi_send_command(ucsi, command, NULL, 0); - if (ret < 0) + if (ret < 0) { + for (i =3D 0; i < ucsi->cap.num_connectors; i++) + mutex_unlock(&connector[i].lock); goto err_unregister; + } =20 ucsi->connector =3D connector; ucsi->ntfy =3D ntfy; =20 + for (i =3D 0; i < ucsi->cap.num_connectors; i++) { + ucsi_init_port(ucsi, &connector[i]); + mutex_unlock(&connector[i].lock); + } + mutex_lock(&ucsi->ppm_lock); ret =3D ucsi->ops->read_cci(ucsi, &cci); mutex_unlock(&ucsi->ppm_lock); --- base-commit: 2061f18ad76ecaddf8ed17df81b8611ea88dbddd change-id: 20251117-ucsi-c2dfe8c006d7 Best regards, --=20 Hsin-Te Yuan