From patchwork Wed Feb 28 22:02:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jameson Thies X-Patchwork-Id: 13576001 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 9639F1361AA for ; Wed, 28 Feb 2024 22:03:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709157790; cv=none; b=kf8VkrNQtjcgjwQplfZOWAe94QJtS1AlA6iiNPyWbyG7B2sLl3Xv9VnuonlTsYhl89jOAG7Ml6SAm9hCCW764ruVPPOMTiS+btTyFoOLRm1BnfDx+Q9X3J+9uRT5mKh2B9MqSgXg7sMh4MbupMPiJskTJbqTE+bII4v1UotXx1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709157790; c=relaxed/simple; bh=Ybd/HW3xor3RSSR0F37+S3Wo7Gc0r4PsvTMb4I8HHCI=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=Z2cUuFSoiHateAClnH0XJdC13DhERcx1YoRsLhhqBgileIdwqyETLw/CL9h2WvNwQe+MmgBK4UsbYNgRo5lr1hVeSdVpi/ins1LHNRMUtib8CuJYaWWTxm8NTrxTKmEDPNfBFSlozLUlEVuH2MIzkMrcBQNwKF3wog4aL635DgU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthies.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vKQoa0a+; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthies.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vKQoa0a+" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcc15b03287so382102276.3 for ; Wed, 28 Feb 2024 14:03:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709157787; x=1709762587; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=EOALInjeSh3HTsFKo9WeEXTAmZVBSHgGCLq/ONkSsHE=; b=vKQoa0a+MuYFP7qu+/L7cVNB6QPL2QiKG6k0ejUKLLGASeEjtHXTizcOx4mHfzvHB4 9VUNIUoBKkJjiLQEse12DknNAVLyLps8yN030mrxeoqt/9cY4caXfiDHrubjIZX1v1gc jRz12nVBlxe3s8WMMf9ZIjDmV1omJegZNDbRMyjBTc3CRjx8OhYMsLBlyELliakCjFQA K8piN8tcsJlLeus+fFhTCupKF+cJdnvCd+wKy7pjAptu8Jvyt8RlFW9sOlHj/Qkmtk0n 7qiWTDOVs7XYRcHxKugbATK1Pq3aaKYSxM77L1x42P0NWTpDUIqQTyD0xoe8qqhq4tab Ytew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709157787; x=1709762587; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=EOALInjeSh3HTsFKo9WeEXTAmZVBSHgGCLq/ONkSsHE=; b=VIqRbiNnUUsEYB6yvxuf5+Mb2ceDGLgfVVFaXjUkZS3VAsPmowosjmHi7h6nksOa96 ppqUgK335nhEvF3ECz68q8QOA/uGjSMt/qKxXrZlOV6kwiC0rR81LdXOmbANexNx97sZ 90sUaOnPEda69hxhSpfE54dnteuNNlHKtJe3u6IhMJ31S1RJaC0T3tStn4rAW76KoY8w 9sfFDJXsJhWTqw0B8mPp8UK0LYiYh1BRRZ7gwlhbubZH0DJpPqLY7jRwhq9uYSiXPqRj bIwhmyxxFLbZjdwDlxv0nq8qcrymkQVnz9L6r8BeYKRvAWpL1j5iFBpt3QOIhHwk3eUX hwCQ== X-Forwarded-Encrypted: i=1; AJvYcCX8rUZZ3wPw7iRoGtCtrcNtQAEk2PI7aadNBePUpIgfPhcN4Yt+8mitk5/b9rZ7kTv1hxddlK/XpepDaRM8TM25YDDfmut4cZsp X-Gm-Message-State: AOJu0YxU93FJxH6hMzUIc0PfFBcGwxSompYPrFsNvO656sZ+Rl14DDz/ T/8dvSJcsRcw2CqZEPT/nFhSlj6PQISw3PK+7Y/i1o6ufoQ67+8ygCEYioG0i8oWeSjtgtjvQZm LKA== X-Google-Smtp-Source: AGHT+IGMJv0UhUZ/qmZRP7/6yeugTC9G/Lx9cGOcVKv1FCV+MmpktOD6lzu7jHBhZw5mSTxD3Jq60w0kvRg= X-Received: from jthies.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:23db]) (user=jthies job=sendgmr) by 2002:a05:6902:1142:b0:dce:5218:c89b with SMTP id p2-20020a056902114200b00dce5218c89bmr31900ybu.5.1709157787604; Wed, 28 Feb 2024 14:03:07 -0800 (PST) Date: Wed, 28 Feb 2024 22:02:51 +0000 Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog Message-ID: <20240228220251.3613424-1-jthies@google.com> Subject: [PATCH v2 1/4] usb: typec: ucsi: Clean up UCSI_CABLE_PROP macros From: Jameson Thies To: heikki.krogerus@linux.intel.com, linux-usb@vger.kernel.org Cc: jthies@google.com, pmalani@chromium.org, bleung@google.com, abhishekpandit@chromium.org, andersson@kernel.org, dmitry.baryshkov@linaro.org, fabrice.gasnier@foss.st.com, gregkh@linuxfoundation.org, hdegoede@redhat.com, neil.armstrong@linaro.org, rajaram.regupathy@intel.com, saranya.gopal@intel.com, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Benson Leung Clean up UCSI_CABLE_PROP macros by fixing a bitmask shifting error for plug type and updating the modal support macro for consistent naming. Fixes: 3cf657f07918 ("usb: typec: ucsi: Remove all bit-fields") Reviewed-by: Benson Leung Reviewed-by: Prashant Malani Reviewed-by: Dmitry Baryshkov Signed-off-by: Jameson Thies --- Changes in v2: - Tested on usb-testing branch merged with chromeOS 6.8-rc2 kernel. drivers/usb/typec/ucsi/ucsi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 7e35ffbe0a6f2..469a2baf472e4 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -259,12 +259,12 @@ struct ucsi_cable_property { #define UCSI_CABLE_PROP_FLAG_VBUS_IN_CABLE BIT(0) #define UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE BIT(1) #define UCSI_CABLE_PROP_FLAG_DIRECTIONALITY BIT(2) -#define UCSI_CABLE_PROP_FLAG_PLUG_TYPE(_f_) ((_f_) & GENMASK(3, 0)) +#define UCSI_CABLE_PROP_FLAG_PLUG_TYPE(_f_) (((_f_) & GENMASK(4, 3)) >> 3) #define UCSI_CABLE_PROPERTY_PLUG_TYPE_A 0 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_B 1 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_C 2 #define UCSI_CABLE_PROPERTY_PLUG_OTHER 3 -#define UCSI_CABLE_PROP_MODE_SUPPORT BIT(5) +#define UCSI_CABLE_PROP_FLAG_MODE_SUPPORT BIT(5) u8 latency; } __packed; From patchwork Wed Feb 28 22:03:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jameson Thies X-Patchwork-Id: 13576002 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 E8CAA1361C0 for ; Wed, 28 Feb 2024 22:03:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709157825; cv=none; b=qCPahCHgY/+mybCERt77FyPjnqhq8Af2WI3aGjRxDA7IQYQNfP0YDM8q9Gv/bzBPgQFffSAAtX9uLwfnDWbVXm3unje+auiKW4a45EIdKuA9C6saL1z2+ZvllOpUHACkgMUv5AwBjehp5oS8ngNQpMCkNa/4JmWpYyrCmKrKfyk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709157825; c=relaxed/simple; bh=ryVPktQRnYCrMHD3Ujbd8hCR3CnuJmpQVQb/Vo2KVQE=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=GKL6JYUv0fLQQmnfpQWhowK2iF8KRQ0WGuF/ovWoU2OxhWYZC0YLoLK5bA/Lkx740GxRRv228iJIn8M/rHCtrE5spM7xYFRvYmFpthAx48znPYPeDhbVlEeTmamPDJ6rb3tBxBbAsAr1u+SmqW+HFpK+/YYpIbA2eLf+wP5hnhg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthies.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=T0RTPi+U; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthies.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="T0RTPi+U" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-608d6ffc64eso3911287b3.0 for ; Wed, 28 Feb 2024 14:03:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709157823; x=1709762623; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=yFg4YW/Z3HZfFTzuAYbv+SLduBl2JOv/FXcutS6mDuE=; b=T0RTPi+UEGCOI1/7M8mLrEcVq5UTpoilBoa96pK7u50SFnP8a9FQ3pKeV21lW3lq3L T4TIYMRvutkN4M7dxhowx77zS1EY/xytitgVUS0dIJC2jD6peeNJyJdO0arLcv8UNXrQ DyqMhc8BTLQtPZ2u8yA7GnPtZFBHpr87kSedxDVyNQrl0TUz+t/Pd3M4Uvhc+w1XSLSc UYD8fM7gqbdmpuZhxfkjCoRrQZr9kVGFVpM3XBuMLpsOLhaK74hYBWospbf+JRC7pVPH M4YQKSlidhfOjKNcbAhXmUphxf+aW1aaz0vbESoTXQRQUqy7oBfvj3TFwm/dTCa6RhjZ 66lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709157823; x=1709762623; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=yFg4YW/Z3HZfFTzuAYbv+SLduBl2JOv/FXcutS6mDuE=; b=S3yxkfLfHFEn64+RuC8FMBqs9SoYhNC+GRrWUNKGQNHW3qOTNnW+4Dg+13xrRDDQR5 mAn2NrSSVkOQ5bSm26/LiJVNBQjT4D32t+aYM39b9YTznXee+QVY8RjhHEPPQTiXlHtN Nm1fR8HhZ/SmOKZzzmUKWVFxa4vWrn02k1PlPuUJw/qn34fPl1/J5I7kIdJ0H3r0bEea tWmoV2TpeZvWd+04xSRlBe+/vUivfsFhlDxccL7bJi7sxkbdVJREKzKP0NTFOzIaEU7X D+s/GIjL9EHRs2GMsrXRxp09htatvd6PbwF7whuzPXk9FY4OT+Jw3HYL0eSv4Ut3H9/Y iT4A== X-Forwarded-Encrypted: i=1; AJvYcCVSGqhs+15lWL58xlFXNzZrJvZrsIhrBlJegTp6tUIaBsJcImiBoe4jblFmrth4uZ14qFNcMTx1Am3SspDYkE1Fo07uL1fm6o9i X-Gm-Message-State: AOJu0YxM8/68eO5uIAZuOM9MglbGXkblFI7kWbhEf3V7h1M4mQVQVtek WNPOn4GOs8CTEDcyZwp37RW6wXpHF+/Ry3RcALUzVdJvpEXEDqP9+JLvy44axLWMnPAjqoJ0J32 QwQ== X-Google-Smtp-Source: AGHT+IH3gXdkNlGOCUOpQn9NRG+QodN+v1KpsdgX7hK/n1lG8ZbyY5XCY/TKkiNrtNGu6ZUG5H0TZT9YIp8= X-Received: from jthies.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:23db]) (user=jthies job=sendgmr) by 2002:a05:690c:5:b0:609:3a33:bacc with SMTP id bc5-20020a05690c000500b006093a33baccmr2310ywb.5.1709157822956; Wed, 28 Feb 2024 14:03:42 -0800 (PST) Date: Wed, 28 Feb 2024 22:03:39 +0000 Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog Message-ID: <20240228220339.3613870-1-jthies@google.com> Subject: [PATCH v2 2/4] usb: typec: ucsi: Register cables based on GET_CABLE_PROPERTY From: Jameson Thies To: heikki.krogerus@linux.intel.com, linux-usb@vger.kernel.org Cc: jthies@google.com, pmalani@chromium.org, bleung@google.com, abhishekpandit@chromium.org, andersson@kernel.org, dmitry.baryshkov@linaro.org, fabrice.gasnier@foss.st.com, gregkh@linuxfoundation.org, hdegoede@redhat.com, neil.armstrong@linaro.org, rajaram.regupathy@intel.com, saranya.gopal@intel.com, linux-kernel@vger.kernel.org, Benson Leung Register cables with the Type-C Connector Class in the UCSI driver based on the PPM response to GET_CABLE_PROPERTY. Registered cable properties include plug type, cable type and major revision. Reviewed-by: Benson Leung Reviewed-by: Prashant Malani Reviewed-by: Dmitry Baryshkov Reviewed-by: Heikki Krogerus Signed-off-by: Jameson Thies --- Expected cable properties populate the USB Type-C connector class sysfs paths: nospike-rev4 /sys/class/typec # ls port0-cable device identity plug_type port0-plug0 power subsystem type uevent usb_power_delivery_revision Changes in v2: - Shortened lines to within 80 characters. - Tested on usb-testing branch merged with chromeOS 6.8-rc2 kernel. drivers/usb/typec/ucsi/ucsi.c | 73 +++++++++++++++++++++++++++++++++++ drivers/usb/typec/ucsi/ucsi.h | 5 +++ 2 files changed, 78 insertions(+) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index ae105383e69e7..7c84687b5d1a3 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -734,6 +734,52 @@ static void ucsi_unregister_partner_pdos(struct ucsi_connector *con) con->partner_pd = NULL; } +static int ucsi_register_cable(struct ucsi_connector *con) +{ + struct typec_cable *cable; + struct typec_cable_desc desc = {}; + + switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(con->cable_prop.flags)) { + case UCSI_CABLE_PROPERTY_PLUG_TYPE_A: + desc.type = USB_PLUG_TYPE_A; + break; + case UCSI_CABLE_PROPERTY_PLUG_TYPE_B: + desc.type = USB_PLUG_TYPE_B; + break; + case UCSI_CABLE_PROPERTY_PLUG_TYPE_C: + desc.type = USB_PLUG_TYPE_C; + break; + default: + desc.type = USB_PLUG_NONE; + break; + } + + desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE & + con->cable_prop.flags); + desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD( + con->cable_prop.flags); + + cable = typec_register_cable(con->port, &desc); + if (IS_ERR(cable)) { + dev_err(con->ucsi->dev, + "con%d: failed to register cable (%ld)\n", con->num, + PTR_ERR(cable)); + return PTR_ERR(cable); + } + + con->cable = cable; + return 0; +} + +static void ucsi_unregister_cable(struct ucsi_connector *con) +{ + if (!con->cable) + return; + + typec_unregister_cable(con->cable); + con->cable = NULL; +} + static void ucsi_pwr_opmode_change(struct ucsi_connector *con) { switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) { @@ -807,6 +853,7 @@ static void ucsi_unregister_partner(struct ucsi_connector *con) typec_partner_set_usb_power_delivery(con->partner, NULL); ucsi_unregister_partner_pdos(con); ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP); + ucsi_unregister_cable(con); typec_unregister_partner(con->partner); con->partner = NULL; } @@ -907,6 +954,30 @@ static int ucsi_check_connection(struct ucsi_connector *con) return 0; } +static int ucsi_check_cable(struct ucsi_connector *con) +{ + u64 command; + int ret; + + if (con->cable) + return 0; + + command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num); + ret = ucsi_send_command(con->ucsi, command, &con->cable_prop, + sizeof(con->cable_prop)); + if (ret < 0) { + dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n", + ret); + return ret; + } + + ret = ucsi_register_cable(con); + if (ret < 0) + return ret; + + return 0; +} + static void ucsi_handle_connector_change(struct work_struct *work) { struct ucsi_connector *con = container_of(work, struct ucsi_connector, @@ -948,6 +1019,7 @@ static void ucsi_handle_connector_change(struct work_struct *work) ucsi_register_partner(con); ucsi_partner_task(con, ucsi_check_connection, 1, HZ); ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ); + ucsi_partner_task(con, ucsi_check_cable, 1, HZ); if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) == UCSI_CONSTAT_PWR_OPMODE_PD) @@ -1346,6 +1418,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con) ucsi_register_partner(con); ucsi_pwr_opmode_change(con); ucsi_port_psy_changed(con); + ucsi_check_cable(con); } /* Only notify USB controller if partner supports USB data */ diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 469a2baf472e4..f0aabef0b7c64 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -265,6 +265,9 @@ struct ucsi_cable_property { #define UCSI_CABLE_PROPERTY_PLUG_TYPE_C 2 #define UCSI_CABLE_PROPERTY_PLUG_OTHER 3 #define UCSI_CABLE_PROP_FLAG_MODE_SUPPORT BIT(5) +#define UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV(_f_) (((_f_) & GENMASK(7, 6)) >> 6) +#define UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(_f_) \ + UCSI_SPEC_REVISION_TO_BCD(UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV(_f_)) u8 latency; } __packed; @@ -400,6 +403,7 @@ struct ucsi_connector { struct typec_port *port; struct typec_partner *partner; + struct typec_cable *cable; struct typec_altmode *port_altmode[UCSI_MAX_ALTMODES]; struct typec_altmode *partner_altmode[UCSI_MAX_ALTMODES]; @@ -408,6 +412,7 @@ struct ucsi_connector { struct ucsi_connector_status status; struct ucsi_connector_capability cap; + struct ucsi_cable_property cable_prop; struct power_supply *psy; struct power_supply_desc psy_desc; u32 rdo; From patchwork Wed Feb 28 22:03:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jameson Thies X-Patchwork-Id: 13576003 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 DAEF27292A for ; Wed, 28 Feb 2024 22:03:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709157839; cv=none; b=BIt0ZeEPTdWgNwAKDrdts1Ejs59hEkaRBeyqhS8pnIjvJEDYQKpnBg38H7qqLgZE+Y/rPkZklL0rvuK4fJyYMKWtJVZODpwXeBf3AfnPyTOri2Frsd10HwlblaMuOccWEmf1ne1pWP2d/FzeZp3fLqe72UJhQ8iC87be3rTs9LE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709157839; c=relaxed/simple; bh=Z/DkHcQWucfnp59Rztm+Uc4+DaWNClDgYlOSekDwPTQ=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=kRNxtm1mZQHB1ZIz8a57sjjMcOjeKuTQmciUUAvx6bxRR19jbuQnSe2W36wyzLeQdihnhkLMz/apKJ9604Fz0hCbbesJYGdXTk4L1WOPl7MC0NgB0iH8Wbtt4/y1nfv7RS/c9E3hN80fpEwVVfKUkVm6C/MRF9rOJiWFh4gy6Pg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthies.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=zpN+EVC2; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthies.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zpN+EVC2" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-608ad239f8fso4390587b3.0 for ; Wed, 28 Feb 2024 14:03:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709157836; x=1709762636; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=4CA4tg/c3dgG9UMrMK9e+dv6nPpGjSIEc2b658y+GNA=; b=zpN+EVC24EQZrjwmgNTLgae8Zkq27I+8j61fASCgvGS7TW4PCCZiFau8qG040J3bzl cvDDD46ZQBXBqPRq/xPMInh7D+4pboET7Rvv6r7w2LIERoWwKb0NTxtSGAyEZ8mgBzIJ s7BQbX/TVm6Mi+3OaZhpY7t9Hn2DCh62PP+/Dkl1C4Eh+D7BavjaHSsZKyq/P6a/aP4H +NR60LYjN6UI/JC3asFVSA2bC2QhB/cVojOeYOIgB8X5NRDfwICvD++C58v4+EdXYsnx KolRw2DlWR/hxOGQZD+f2QOgDGQcMFAWfjke684/6JHLNHfkOm80rCvTsaAlvx05TKiZ Sbug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709157836; x=1709762636; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=4CA4tg/c3dgG9UMrMK9e+dv6nPpGjSIEc2b658y+GNA=; b=Xlsmqol9uYQZSjaFlH+e7kzbVKjbnqEhx2twIYC35E/tlHIsMSLgy72OEA7ooAs5K4 0zDNPRDUo+5Z/BsF8Cq2BF7GZPM2EAWi5ZDxkEJ5+SVf9HQ+rT3rFRrCbI+/Kx2XTCRm 38PTYXz3fkp9WEBCHyrSrvjwe1titnQAlSom6TcjoxvV5O0A95dBwHyKXwf/t/YTrL3m 3s3Xo4MSRp838SYRf9MkbxNCZ92SGCgKNesxIN7NQtVX3bJyY4YJ6mj2sqvIMlYc02Ud NxE4tRfjbgRMj/5c8ZTazrZbTBm73jwbbX3qfIFH86OkGdMWkXqV8igyk3sFjswwszpJ 3iXg== X-Forwarded-Encrypted: i=1; AJvYcCWPOFqb6Ayln+yQEfOHJtbX4QZQiu3tXRuQjlAaKIFqWe3UHclCk9NXgot4wATSCIMK13G6nbEBdSGvr4FK+adC3YDwqCMCIMtr X-Gm-Message-State: AOJu0Yzmqk3i927AA9BfFH9InBDHtDv8IRX7H8WqUxQlTeQgRC/r/yPQ 6Vz74z8EjIm994gZ93Ug2u+LDGJRhxxuuUiAkQK7OoMeYK5cejzIILdGEpIdyFIvHDGX/VLBPEW EWA== X-Google-Smtp-Source: AGHT+IFSN6eTVjh2nuUsso4wnthmSJyKwtgJjIgbQmEbaPQoEMkg8ZkUJb+bD8sdTS5Muzbt/mfPnTH0ulM= X-Received: from jthies.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:23db]) (user=jthies job=sendgmr) by 2002:a81:9858:0:b0:609:2faf:7dca with SMTP id p85-20020a819858000000b006092faf7dcamr71912ywg.8.1709157835923; Wed, 28 Feb 2024 14:03:55 -0800 (PST) Date: Wed, 28 Feb 2024 22:03:52 +0000 Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog Message-ID: <20240228220352.3614262-1-jthies@google.com> Subject: [PATCH v2 3/4] usb: typec: ucsi: Register SOP/SOP' Discover Identity Responses From: Jameson Thies To: heikki.krogerus@linux.intel.com, linux-usb@vger.kernel.org Cc: jthies@google.com, pmalani@chromium.org, bleung@google.com, abhishekpandit@chromium.org, andersson@kernel.org, dmitry.baryshkov@linaro.org, fabrice.gasnier@foss.st.com, gregkh@linuxfoundation.org, hdegoede@redhat.com, neil.armstrong@linaro.org, rajaram.regupathy@intel.com, saranya.gopal@intel.com, linux-kernel@vger.kernel.org Register SOP and SOP' Discover Identity responses with the USB Type-C Connector Class as partner and cable identities, respectively. Discover Identity responses are requested from the PPM using the GET_PD_MESSAGE UCSI command. Signed-off-by: Jameson Thies --- GET_PD_MESSAGE responses from the PPM populate partner and cable identity in sysfs: nospike-rev4 /sys/class/typec # ls port0-partner/identity/ cert_stat id_header product product_type_vdo1 product_type_vdo2 product_type_vdo3 nospike-rev4 /sys/class/typec # ls port0-cable/identity/ cert_stat id_header product product_type_vdo1 product_type_vdo2 product_type_vdo3 Changes in v2: - Re-ordered memset call and null assignment when unregistering partners and cables. - Supports registering partner and cable identity with UCSI versions before v2.0. - Shortened lines to within 80 characters with the exception of two error log lines with three indentations. - Tested on usb-testing branch merged with chromeOS 6.8-rc2 kernel. drivers/usb/typec/ucsi/ucsi.c | 125 ++++++++++++++++++++++++++++++++++ drivers/usb/typec/ucsi/ucsi.h | 29 ++++++++ 2 files changed, 154 insertions(+) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 7c84687b5d1a3..4088422b33c74 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -646,6 +646,121 @@ static int ucsi_get_src_pdos(struct ucsi_connector *con) return ret; } +static int ucsi_read_identity(struct ucsi_connector *con, u8 recipient, + struct usb_pd_identity *id) +{ + struct ucsi *ucsi = con->ucsi; + struct ucsi_pd_message_disc_id resp = {}; + u64 command; + int ret; + + if (ucsi->version < UCSI_VERSION_2_0) { + /* + * Before UCSI v2.0, MESSAGE_IN is 16 bytes which cannot fit + * the 28 byte identity response including the VDM header. + * First request the VDM header, ID Header VDO, Cert Stat VDO + * and Product VDO. + */ + command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | + UCSI_CONNECTOR_NUMBER(con->num); + command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient); + command |= UCSI_GET_PD_MESSAGE_OFFSET(0); + command |= UCSI_GET_PD_MESSAGE_BYTES(0x10); + command |= UCSI_GET_PD_MESSAGE_TYPE( + UCSI_GET_PD_MESSAGE_TYPE_IDENTITY); + + ret = ucsi_send_command(ucsi, command, &resp, 0x10); + if (ret < 0) { + dev_err(ucsi->dev, + "UCSI_GET_PD_MESSAGE v1.2 failed first request (%d)\n", + ret); + return ret; + } + + /* Then request Product Type VDO1 through Product Type VDO3. */ + command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | + UCSI_CONNECTOR_NUMBER(con->num); + command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient); + command |= UCSI_GET_PD_MESSAGE_OFFSET(0x10); + command |= UCSI_GET_PD_MESSAGE_BYTES(0xc); + command |= UCSI_GET_PD_MESSAGE_TYPE( + UCSI_GET_PD_MESSAGE_TYPE_IDENTITY); + + ret = ucsi_send_command(ucsi, command, &resp.vdo[0], 0xc); + if (ret < 0) { + dev_err(ucsi->dev, + "UCSI_GET_PD_MESSAGE v1.2 failed second request (%d)\n", + ret); + return ret; + } + } else { + /* + * In UCSI v2.0 and after, MESSAGE_IN is large enough to request + * the large enough to request the full Discover Identity + * response at once. + */ + command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | + UCSI_CONNECTOR_NUMBER(con->num); + command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient); + /* VDM Header + 6 VDOs (0x1c bytes) without an offset */ + command |= UCSI_GET_PD_MESSAGE_OFFSET(0); + command |= UCSI_GET_PD_MESSAGE_BYTES(0x1c); + command |= UCSI_GET_PD_MESSAGE_TYPE( + UCSI_GET_PD_MESSAGE_TYPE_IDENTITY); + + ret = ucsi_send_command(ucsi, command, &resp, sizeof(resp)); + if (ret < 0) { + dev_err(ucsi->dev, "UCSI_GET_PD_MESSAGE failed (%d)\n", + ret); + return ret; + } + } + + id->id_header = resp.id_header; + id->cert_stat = resp.cert_stat; + id->product = resp.product; + id->vdo[0] = resp.vdo[0]; + id->vdo[1] = resp.vdo[1]; + id->vdo[2] = resp.vdo[2]; + return 0; +} + +static int ucsi_get_partner_identity(struct ucsi_connector *con) +{ + int ret; + + ret = ucsi_read_identity(con, UCSI_RECIPIENT_SOP, + &con->partner_identity); + if (ret < 0) + return ret; + + ret = typec_partner_set_identity(con->partner); + if (ret < 0) { + dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n", + ret); + } + + return ret; +} + +static int ucsi_get_cable_identity(struct ucsi_connector *con) +{ + int ret; + + ret = ucsi_read_identity(con, UCSI_RECIPIENT_SOP_P, + &con->cable_identity); + if (ret < 0) + return ret; + + ret = typec_cable_set_identity(con->cable); + if (ret < 0) { + dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n", + ret); + } + + return ret; +} + static int ucsi_check_altmodes(struct ucsi_connector *con) { int ret, num_partner_am; @@ -754,6 +869,7 @@ static int ucsi_register_cable(struct ucsi_connector *con) break; } + desc.identity = &con->cable_identity; desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE & con->cable_prop.flags); desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD( @@ -777,6 +893,7 @@ static void ucsi_unregister_cable(struct ucsi_connector *con) return; typec_unregister_cable(con->cable); + memset(&con->cable_identity, 0, sizeof(con->cable_identity)); con->cable = NULL; } @@ -827,6 +944,7 @@ static int ucsi_register_partner(struct ucsi_connector *con) break; } + desc.identity = &con->partner_identity; desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD; desc.pd_revision = UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(con->cap.flags); @@ -855,6 +973,7 @@ static void ucsi_unregister_partner(struct ucsi_connector *con) ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP); ucsi_unregister_cable(con); typec_unregister_partner(con->partner); + memset(&con->partner_identity, 0, sizeof(con->partner_identity)); con->partner = NULL; } @@ -975,6 +1094,10 @@ static int ucsi_check_cable(struct ucsi_connector *con) if (ret < 0) return ret; + ret = ucsi_get_cable_identity(con); + if (ret < 0) + return ret; + return 0; } @@ -1019,6 +1142,7 @@ static void ucsi_handle_connector_change(struct work_struct *work) ucsi_register_partner(con); ucsi_partner_task(con, ucsi_check_connection, 1, HZ); ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ); + ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ); ucsi_partner_task(con, ucsi_check_cable, 1, HZ); if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) == @@ -1418,6 +1542,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con) ucsi_register_partner(con); ucsi_pwr_opmode_change(con); ucsi_port_psy_changed(con); + ucsi_get_partner_identity(con); ucsi_check_cable(con); } diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index f0aabef0b7c64..b89fae82e8ce7 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -106,6 +106,7 @@ void ucsi_connector_change(struct ucsi *ucsi, u8 num); #define UCSI_GET_CABLE_PROPERTY 0x11 #define UCSI_GET_CONNECTOR_STATUS 0x12 #define UCSI_GET_ERROR_STATUS 0x13 +#define UCSI_GET_PD_MESSAGE 0x15 #define UCSI_CONNECTOR_NUMBER(_num_) ((u64)(_num_) << 16) #define UCSI_COMMAND(_cmd_) ((_cmd_) & 0xff) @@ -159,6 +160,18 @@ void ucsi_connector_change(struct ucsi *ucsi, u8 num); #define UCSI_MAX_PDOS (4) #define UCSI_GET_PDOS_SRC_PDOS ((u64)1 << 34) +/* GET_PD_MESSAGE command bits */ +#define UCSI_GET_PD_MESSAGE_RECIPIENT(_r_) ((u64)(_r_) << 23) +#define UCSI_GET_PD_MESSAGE_OFFSET(_r_) ((u64)(_r_) << 26) +#define UCSI_GET_PD_MESSAGE_BYTES(_r_) ((u64)(_r_) << 34) +#define UCSI_GET_PD_MESSAGE_TYPE(_r_) ((u64)(_r_) << 42) +#define UCSI_GET_PD_MESSAGE_TYPE_SNK_CAP_EXT 0 +#define UCSI_GET_PD_MESSAGE_TYPE_SRC_CAP_EXT 1 +#define UCSI_GET_PD_MESSAGE_TYPE_BAT_CAP 2 +#define UCSI_GET_PD_MESSAGE_TYPE_BAT_STAT 3 +#define UCSI_GET_PD_MESSAGE_TYPE_IDENTITY 4 +#define UCSI_GET_PD_MESSAGE_TYPE_REVISION 5 + /* -------------------------------------------------------------------------- */ /* Error information returned by PPM in response to GET_ERROR_STATUS command. */ @@ -338,6 +351,18 @@ struct ucsi_connector_status { ((get_unaligned_le32(&(_p_)[5]) & GENMASK(16, 1)) >> 1) } __packed; +/* + * Data structure filled by PPM in response to GET_PD_MESSAGE command with the + * Response Message Type set to Discover Identity Response. + */ +struct ucsi_pd_message_disc_id { + u32 vdm_header; + u32 id_header; + u32 cert_stat; + u32 product; + u32 vdo[3]; +} __packed; + /* -------------------------------------------------------------------------- */ struct ucsi_debugfs_entry { @@ -428,6 +453,10 @@ struct ucsi_connector { struct usb_power_delivery_capabilities *partner_sink_caps; struct usb_role_switch *usb_role_sw; + + /* USB PD identity */ + struct usb_pd_identity partner_identity; + struct usb_pd_identity cable_identity; }; int ucsi_send_command(struct ucsi *ucsi, u64 command, From patchwork Wed Feb 28 22:04:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jameson Thies X-Patchwork-Id: 13576004 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 E03231361B0 for ; Wed, 28 Feb 2024 22:04:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709157849; cv=none; b=uxbya3/KRbQ4jAwZqyVV/+vSBGj5kIdKCcDoNCRUNg+Uj3aN1ewkxugX2wRrVBgXJ0OBvp0MQq9QmkWk/SoaUbMfJYufGdqKR8Phad2n9Fde0nxVUUxf+fvRtuGv9KSwc7w/xbJgQGELk4QjneeGtoLn5xM99J8wMH50EydD8xA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709157849; c=relaxed/simple; bh=Ck20RD7SWLmT3wN/hjyZSTuoMvNa2KJPDTQ2bpCa8rU=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=IBPNta+yXDz9zmM4x3pe6HtPmuYGmRCKHyl0ukI6srwg1wi2cSQnAahDnzcVPjaYJLkIhUDqsATuo1WRENweDfzrGPEtKzN4JIWmz35QWmcolRqhfYBw5WMAuy5ZwK87bur5Fw+uANNqDbOZ69362l6O8b0o6uBveY/rnvmcJQA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthies.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=EdIfd3dW; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthies.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EdIfd3dW" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d9a541b720aso517040276.0 for ; Wed, 28 Feb 2024 14:04:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709157846; x=1709762646; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=jb2gwN6bxiLt3LtO6maFpO1+dlXHorrZhIAqEy+dyf0=; b=EdIfd3dWgZ+phOaF12ToGLCveiEDgRPqBtkW1VAVQpFh+v7MRCW/9e2ePDD6Qi3uZG o8CLvN5p0LP/DBMyyeHKFjkyK45rEhN3k0IJWa0E2IjDaq2U00krCVzAnqUHSG8kN5cD Gisuau13EutHypOu7zGPTYgESX/HWHSoCaiWOdxn6NIvuvncTyn1CoONAfjxnlHiWkDp j6CGfC0jM8dNrV/0TcHwnaH1LtnZEC1TRJMxonedv7ThC95r1PcOrepnbIf83OKWzkgG joS4XMVIYP2Kv52/NoUQ64DpcWhrnElhCxRmi4/oFal5kbwm0jTsAqWqwMkACs2jq6V8 SBtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709157846; x=1709762646; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=jb2gwN6bxiLt3LtO6maFpO1+dlXHorrZhIAqEy+dyf0=; b=ca+pEwMGXBBtLvXMpNeSVX+RK3ARTvGQrQAMmEZe+/yPtDp1h9nDjjw1aWirwZFuA5 TIeniJc8H+B4dam+Jq/f1tSvKnx63IuC1HR3S/t17P8NiIjyvgPA2CrGBo23JBf30b9K jeSCx9IraQnwWHivN4YVC6wofGc1Z6GGgAMuxpShAruD00KQsos0L8Rp9ZcXe5wXxJS3 TyatCBYIXf9sJDHSRzzIYEuwmh3aGTMTRu6Dj0xsilKwYHlaWT3aQ/CVpjNPh30cSa3+ KAZQQlgp4Va3o0WD2XOpuiQsQ6yJaGYWMOa9fNgU3bJDk0z8Go4I490h5PGPeGEcTdRE exmg== X-Forwarded-Encrypted: i=1; AJvYcCUWSSN6HDzNV1sp8+cWYQEiKHOZbXyhHMcRej7ajG8bQOTSV42fVNbNBfhF2s1pqQWVE0Mc0YFN5b5BWrsRPu0M0NlbKzfeVLcO X-Gm-Message-State: AOJu0YxtsMuKLNTOgMxhImnX+EOfQwvgvc6UtM8rrAT47fbvcaD3xk+P 1SaJPBO335pJ2V9msTm6tNtUK8RObHwwlg3fRaCPJM55fQHRIvz7cGYZw7qoJ+OV08OknoeyyEI KRw== X-Google-Smtp-Source: AGHT+IGMObvTbyRYVynt1WQ166gAI0nZBfbogex59iyxp3MG5aS6oyV+QzKMWgqsF735JJkDDkfnULmb9+M= X-Received: from jthies.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:23db]) (user=jthies job=sendgmr) by 2002:a05:6902:150f:b0:dc6:207e:e8b1 with SMTP id q15-20020a056902150f00b00dc6207ee8b1mr125822ybu.2.1709157845820; Wed, 28 Feb 2024 14:04:05 -0800 (PST) Date: Wed, 28 Feb 2024 22:04:02 +0000 Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog Message-ID: <20240228220403.3614604-1-jthies@google.com> Subject: [PATCH v2 4/4] usb: typec: ucsi: Register SOP' alternate modes with cable plug From: Jameson Thies To: heikki.krogerus@linux.intel.com, linux-usb@vger.kernel.org Cc: jthies@google.com, pmalani@chromium.org, bleung@google.com, abhishekpandit@chromium.org, andersson@kernel.org, dmitry.baryshkov@linaro.org, fabrice.gasnier@foss.st.com, gregkh@linuxfoundation.org, hdegoede@redhat.com, neil.armstrong@linaro.org, rajaram.regupathy@intel.com, saranya.gopal@intel.com, linux-kernel@vger.kernel.org, Benson Leung Register SOP' alternate modes with a Type-C Connector Class cable plug. Alternate modes are queried from the PPM using the GET_ALTERNATE_MODES command with recipient set to SOP'. Reviewed-by: Benson Leung Reviewed-by: Prashant Malani Reviewed-by: Heikki Krogerus Signed-off-by: Jameson Thies --- SOP' GET_ALTERNATE_MODE responses from the PPM are correctly registered to the cable plug. nospike-rev4 /sys/class/typec # ls port0-cable/port0-plug0/ device port0-plug0.0 port0-plug0.1 power subsystem uevent Changes in v2: - Shortened lines to within 80 characters. - Tested on usb-testing branch merged with chromeOS 6.8-rc2 kernel. drivers/usb/typec/ucsi/ucsi.c | 60 +++++++++++++++++++++++++++++++++++ drivers/usb/typec/ucsi/ucsi.h | 2 ++ 2 files changed, 62 insertions(+) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 4088422b33c74..281954fe9d855 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -399,6 +399,27 @@ static int ucsi_register_altmode(struct ucsi_connector *con, con->partner_altmode[i] = alt; break; + case UCSI_RECIPIENT_SOP_P: + i = ucsi_next_altmode(con->plug_altmode); + if (i < 0) { + ret = i; + goto err; + } + + ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid); + if (ret < 0) + return ret; + + desc->mode = ret; + + alt = typec_plug_register_altmode(con->plug, desc); + if (IS_ERR(alt)) { + ret = PTR_ERR(alt); + goto err; + } + + con->plug_altmode[i] = alt; + break; default: return -EINVAL; } @@ -566,6 +587,9 @@ static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient) case UCSI_RECIPIENT_SOP: adev = con->partner_altmode; break; + case UCSI_RECIPIENT_SOP_P: + adev = con->plug_altmode; + break; default: return; } @@ -849,6 +873,33 @@ static void ucsi_unregister_partner_pdos(struct ucsi_connector *con) con->partner_pd = NULL; } +static int ucsi_register_plug(struct ucsi_connector *con) +{ + struct typec_plug *plug; + struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P}; + + plug = typec_register_plug(con->cable, &desc); + if (IS_ERR(plug)) { + dev_err(con->ucsi->dev, + "con%d: failed to register plug (%ld)\n", con->num, + PTR_ERR(plug)); + return PTR_ERR(plug); + } + + con->plug = plug; + return 0; +} + +static void ucsi_unregister_plug(struct ucsi_connector *con) +{ + if (!con->plug) + return; + + ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P); + typec_unregister_plug(con->plug); + con->plug = NULL; +} + static int ucsi_register_cable(struct ucsi_connector *con) { struct typec_cable *cable; @@ -892,6 +943,7 @@ static void ucsi_unregister_cable(struct ucsi_connector *con) if (!con->cable) return; + ucsi_unregister_plug(con); typec_unregister_cable(con->cable); memset(&con->cable_identity, 0, sizeof(con->cable_identity)); con->cable = NULL; @@ -1098,6 +1150,14 @@ static int ucsi_check_cable(struct ucsi_connector *con) if (ret < 0) return ret; + ret = ucsi_register_plug(con); + if (ret < 0) + return ret; + + ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P); + if (ret < 0) + return ret; + return 0; } diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index b89fae82e8ce7..32daf5f586505 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -429,9 +429,11 @@ struct ucsi_connector { struct typec_port *port; struct typec_partner *partner; struct typec_cable *cable; + struct typec_plug *plug; struct typec_altmode *port_altmode[UCSI_MAX_ALTMODES]; struct typec_altmode *partner_altmode[UCSI_MAX_ALTMODES]; + struct typec_altmode *plug_altmode[UCSI_MAX_ALTMODES]; struct typec_capability typec_cap;