From nobody Tue Dec 2 01:28:18 2025 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 A550330F93D for ; Fri, 21 Nov 2025 12:33:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763728434; cv=none; b=DMe2BnZbkIFUfSIg+kudCs2z3HFJyZS5goWUd0t8DF8vXHOF8l/GUaOOgPkdMIV7sCG8a3J7GuvYmUZHS7KhsoAj9Kxoz6NpXrflSTN8dXxKPN+CCyl0GXWY/4Kcu0s3Kl7fmTsmEEgl/DzQewVhetjDcdiSFyFsPsSXuteqa7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763728434; c=relaxed/simple; bh=D30w+lFkSuEsWEZWd5TmRaDaakz9t8FAnSsNiECeRDE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=SPzB1fhWpX/XtYI2+Kr7WGXAEGMX89xFCOKX6yIRtEXeFprt6pYSojhgcL5zpt0fnxgQNg0QYsV0DwP71pItERktpF/eK562Yax/vLG6JT4ulfLfD/KGTbXtqiB92Y657P+XrCHNX4AYvv124ilVu2p8wglhCUypxlE2yQkE7O4= 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=mur8wDdT; arc=none smtp.client-ip=209.85.214.169 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="mur8wDdT" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-299d40b0845so31151995ad.3 for ; Fri, 21 Nov 2025 04:33:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1763728431; x=1764333231; 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=b1O/LUZs/EwmOPh3OAPG6JZTIgwsjUzvu5nYGtCSaFI=; b=mur8wDdTJFK70dcSFhDuSpu9yPHCT8vGj8akbF+zJR+xCtzK2PC8SdsXyMaFkFku4m LXf06I1zZy9aJSMLchCTcF7SwezSkIFQmshgIzkZ3LcuD1nbeU1TMWvkIowrg5i+CFqO /4iV+/wFS+7XyqXp0WO5dBfSiYWEpJnvIP6sw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763728431; x=1764333231; 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=b1O/LUZs/EwmOPh3OAPG6JZTIgwsjUzvu5nYGtCSaFI=; b=BZ9GwrdA9R8jGrK4GZp5OULLZJUluVlayawjValQNfEi9nbOyzP/2WDd2TAdalHX5O RK1E93fiA0ptJSpYWOExHiD41HGdRUlSAfOnyIk+GeL3Emzdce5Z89Q1XZVYI0waress vcxE6H2sB51rsbarL3+OFTXnA/2RzHSdiuXDtsgjB0zsiJqllIj+aNaTneJqcnSr6itq vgRu8PbzSG72tK0C1PkC+2uTaKKvy+c9jtmQXaKU0J8aaXxr+wgucScoYzdWmIirJtn6 q4BinlMol8inxfHTgIIUEC+3Zt8yoKgFbcpRGeKRZk/aOkEKe/qMfqtoFmJuqYmPJje7 i6vw== X-Forwarded-Encrypted: i=1; AJvYcCVl1zVrqL7+8UEUZhkMc5335N1PrxlM3FhBAHa8f2+kXtWt4IQdZhDH/3LtFRo7PTuV7ktaiTRziAD7+Ks=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2JprOmaYvTeUQniM6QPAQ52Y6JwFxUioGnbfDfVTBzslmO+1X uSy6JNTEHXcMf4QnbFzhYfZReOnDFRbRkUIeomtyUqjh15q+MRSCGzCEzMcMsKyxZg== X-Gm-Gg: ASbGnctrBl/Wy6P29bIBKRmkoMVfjzURA2zgJq07GhxF+N2guP3qqmsF2trUmd+sbez PxugRMe7K51JLMPCHpOiR+/cUwLMxH0Q4YkI6NEtIUwcBbTV7Ubg2jnyHMbX6i5PwX5ocsXj9ZF H4yG1TOW9XTQL6oZWNJlRXAPRvXPs5xuSkhnnqNrnHYSIWKfK5DcPlsI7jxqMD4sWHkxFMYMced vEULafmaeSTzF3UvtwzE2ilsOxra062LcKMxZvmESvVgfoN2mHkZUg3X9JxdKqJVA1BVdRXpgcW WrJUyGzII3hTEUvVyGUOesufMFvvnwJWK1RDaRfTF9q6yLJR26kqCOWGC0XnK/or+n85WrA7vdu fFVePPzp/Iyads+V3o/Wz4l+s1ko5VefecjzMnwNesBNZoSV7ds1gDb4peEm8QYkoNtOg59og7q WK7rd/xxoDNPe3qkKe0xN04qE/0qJVmj7UkU374pt8AIGam7MY+3pz1gpy1bUtTK3b/i0FCf/nr kStcNyNoZmCcw== X-Google-Smtp-Source: AGHT+IHThFTOdK037ehJoYrNFdjm9e8BasA3ZCW6CMwPcYSKEv+vFO8S2pmztks1dh/SEJkSUFiJTA== X-Received: by 2002:a17:903:3a8b:b0:272:dee1:c133 with SMTP id d9443c01a7336-29b6bead617mr29646895ad.22.1763728430931; Fri, 21 Nov 2025 04:33:50 -0800 (PST) Received: from yuanhsinte-p620-1.tpe.corp.google.com ([2a00:79e0:201d:8:9b9a:95f9:c689:d44e]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29b5b25deefsm56795315ad.56.2025.11.21.04.33.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Nov 2025 04:33:50 -0800 (PST) From: Hsin-Te Yuan Date: Fri, 21 Nov 2025 20:33:48 +0800 Subject: [PATCH v3] 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: <20251121-ucsi-v3-1-b1047ca371b8@chromium.org> X-B4-Tracking: v=1; b=H4sIACtcIGkC/12MvQ6DMAwGXwV5bqrE4U+deI+qAyQGPECqpEStE O/egDpUyNNZ390KgTxTgFu2gqfIgd2cQF8yMGM7DyTYJgaUWCilKrGYwMKg7ak2Upa2gjR9eur 5fWTuj8Qjh5fzn6Ma1f49BaIS6azpTEFtmWPXmNG7iZfp6vwAeyPiv1f/PEye1Sq3hdRaE568b du+xOTV+9MAAAA= X-Change-ID: 20251117-ucsi-c2dfe8c006d7 To: Heikki Krogerus , Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, linux-kernel@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 conenctor change to ensure the status is synced. Signed-off-by: Hsin-Te Yuan --- 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 | 124 +++++++++++++++++++++++---------------= ---- 1 file changed, 67 insertions(+), 57 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 3f568f790f39b0271667e80816270274b8dd3008..fbda94bfd5ed12ad8cfaedceb47= ad15fee271378 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -1560,11 +1560,75 @@ static struct fwnode_handle *ucsi_find_fwnode(struc= t ucsi_connector *con) return NULL; } =20 +static void ucsi_init_port(struct ucsi *ucsi) +{ + enum usb_role u_role; + int ret, i; + struct ucsi_connector *con; + + for (i =3D 0; i < ucsi->cap.num_connectors; i++) { + u_role =3D USB_ROLE_NONE; + con =3D &ucsi->connector[i]; + /* 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); + continue; + } + + 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_PW= R_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 +1723,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: @@ -1833,6 +1841,8 @@ static int ucsi_init(struct ucsi *ucsi) ucsi->connector =3D connector; ucsi->ntfy =3D ntfy; =20 + ucsi_init_port(ucsi); + mutex_lock(&ucsi->ppm_lock); ret =3D ucsi->ops->read_cci(ucsi, &cci); mutex_unlock(&ucsi->ppm_lock); --- base-commit: 6a23ae0a96a600d1d12557add110e0bb6e32730c change-id: 20251117-ucsi-c2dfe8c006d7 Best regards, --=20 Hsin-Te Yuan