From patchwork Tue Jan 23 22:30:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Pandit-Subedi X-Patchwork-Id: 13528227 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 A527E53E04 for ; Tue, 23 Jan 2024 22:30:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049047; cv=none; b=MeDWS91FwodPSQqvH3+4BvzYcQHf5hEavSOxQRz9ofW+eR3ttXFpWHg1c/RQDmwF5dZZ858d635EeeUhMdH960NZtoK+fPaA/74/891bgpr3r4D3jaheqDDNhtb9HPJVsaZN580387HsiEugPHxOKPqoM0wjEHm0CKnEChMKHaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049047; c=relaxed/simple; bh=nFPGi5xF7bCjge58x5dvSr31/w9RSEp6a3QqQMX9YTM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=u5CIrTWcoU9DiqYGkffz9GfH5AzWyxCsy5ip3/AZajtdik560gXxnPo/3xyEqNoA17PtASbxaZr3+TaapZuzAs9uOJGzOlen3DgN8rOP9CkrY0bbRDlmX87MWvM1tIIeD9um1syv/cXeX2E0Yw6iwLxAlLG3UqqxBj3IHhm+Ajs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--abhishekpandit.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PF29t11Z; arc=none smtp.client-ip=209.85.210.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--abhishekpandit.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PF29t11Z" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-6dd7359e8adso1309659b3a.1 for ; Tue, 23 Jan 2024 14:30:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706049044; x=1706653844; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=zJA66Po5lANNBlrllEE6mRzYHuJjBVUPPi7meiLS2z8=; b=PF29t11ZvpdaDOU6JpHiK5NjuIzDEtoAHCOvsX3eF1GtF+4d16Tr+qyOzoypToxBAW 7esPFouEonmt4iOqDld4OOvH2spOrKZVWOi5CnQBCubgsKL1cPtuUtGNqKoFgJRuxHku TmbYt1BPpYIVI1jzctDgoDxLQG9rGhEjqD0jWHJRLDfGKQKZSdL5hh+Gbi3psQytAG+s miEmOf9ieCFVZjfgvQppoafVVzj6dUB6llfEzV+M0od6eTolb21B7S+BU6zr/2mDX32Z yZRnc1F+cBvkG+AEeX7tylIcwC2oy7R8FeqkyA1UAJYYit5rhJeku18EYquEm7yYlrac qjVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706049044; x=1706653844; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zJA66Po5lANNBlrllEE6mRzYHuJjBVUPPi7meiLS2z8=; b=D3H7kJtemyOZifXG6X3Jt50mV9lqcpUtqH+GxJg353LD8a+hrrcx404T6cXSuQpR0V c92qw2R8tYwx5rYRPBtYqZwXWH7aqu0MS7Kn157OiHt2bZj9L8RV/a6HeATuxqRfCpuO HaJb7u9oRk33drI+JhD/NOYgdJLqEJReIDtKhdJfbHMonrmORhTysRQEb7iUQ1qS/siD A50kmAheNxfY64RhnXwmynGHNJ4GF2p+oKtSN5miRMoimY5QNmxBYOUGh0H6jPSNvP/S p0vu2m8oPW3++ig4KYO+XJMnO8U2TCO8UTj9MJjVjnR63pLO1Q/4vz0UBXBgqX7uho/Z lzKQ== X-Gm-Message-State: AOJu0YxkZ8TtPP4mDh9kLMw19nkuXnXEl43lb48gpFemEVxHoWCEjYgZ cffOqWE9S0G38aiuO1R/48yARUzTP6IsbUSoMZ41MnBkDCJm2cUyECiyIunwlj0GuHDVMvpTJMS gkQnZdyVEPbNsVxcrc83DOqdGuWt4Bfw3xA== X-Google-Smtp-Source: AGHT+IGO/pnHmieSUEyGfPKEd1eFTxoStKRA06ggz/E09iPERH89CJecxMFIF4JhR5LwT5OtMED52HAPwMZVPh05zR8ERw== X-Received: from abps.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:9b4]) (user=abhishekpandit job=sendgmr) by 2002:a05:6a00:3993:b0:6db:c63d:dc77 with SMTP id fi19-20020a056a00399300b006dbc63ddc77mr806983pfb.5.1706049044004; Tue, 23 Jan 2024 14:30:44 -0800 (PST) Date: Tue, 23 Jan 2024 14:30:34 -0800 In-Reply-To: <20240123223039.1471557-1-abhishekpandit@google.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240123223039.1471557-1-abhishekpandit@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123143026.v1.1.Iacf5570a66b82b73ef03daa6557e2fc0db10266a@changeid> Subject: [PATCH v1 1/3] usb: typec: ucsi: Limit read size on v1.2 From: Abhishek Pandit-Subedi To: Heikki Krogerus , linux-usb@vger.kernel.org Cc: pmalani@chromium.org, jthies@google.com, Abhishek Pandit-Subedi , Bjorn Andersson , Dmitry Baryshkov , Fabrice Gasnier , Greg Kroah-Hartman , Hans de Goede , Neil Armstrong , Saranya Gopal , linux-kernel@vger.kernel.org From: Abhishek Pandit-Subedi Between UCSI 1.2 and UCSI 2.0, the size of the MESSAGE_IN region was increased from 16 to 256. In order to avoid overflowing reads for older systems, add a mechanism to use the read UCSI version to truncate read sizes on UCSI v1.2. Signed-off-by: Abhishek Pandit-Subedi Reviewed-by: Prashant Malani --- Tested on 6.6 kernel. Dmesg output from this change: [ 105.058162] ucsi_um_test ucsi_um_test_device.0: Registered UCSI interface with version 3.0.0 drivers/usb/typec/ucsi/ucsi.c | 26 ++++++++++++++++++++++++-- drivers/usb/typec/ucsi/ucsi.h | 11 +++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 5392ec698959..4edf785d203b 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -36,6 +36,19 @@ */ #define UCSI_SWAP_TIMEOUT_MS 5000 +static int ucsi_read_message_in(struct ucsi *ucsi, void *buf, + size_t buf_size) +{ + /* + * Below UCSI 2.0, MESSAGE_IN was limited to 16 bytes. Truncate the + * reads here. + */ + if (ucsi->version <= UCSI_VERSION_1_2) + buf_size = min_t(size_t, 16, buf_size); + + return ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, buf, buf_size); +} + static int ucsi_acknowledge_command(struct ucsi *ucsi) { u64 ctrl; @@ -72,7 +85,7 @@ static int ucsi_read_error(struct ucsi *ucsi) if (ret < 0) return ret; - ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, &error, sizeof(error)); + ret = ucsi_read_message_in(ucsi, &error, sizeof(error)); if (ret) return ret; @@ -170,7 +183,7 @@ int ucsi_send_command(struct ucsi *ucsi, u64 command, length = ret; if (data) { - ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size); + ret = ucsi_read_message_in(ucsi, data, size); if (ret) goto out; } @@ -1556,6 +1569,15 @@ int ucsi_register(struct ucsi *ucsi) if (!ucsi->version) return -ENODEV; + /* + * Version format is JJ.M.N (JJ = Major version, M = Minor version, + * N = sub-minor version). + */ + dev_info(ucsi->dev, "Registered UCSI interface with version %x.%x.%x", + UCSI_BCD_GET_MAJOR(ucsi->version), + UCSI_BCD_GET_MINOR(ucsi->version), + UCSI_BCD_GET_SUBMINOR(ucsi->version)); + queue_delayed_work(system_long_wq, &ucsi->work, 0); ucsi_debugfs_register(ucsi); diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 6478016d5cb8..bec920fa6b8a 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -23,6 +23,17 @@ struct dentry; #define UCSI_CONTROL 8 #define UCSI_MESSAGE_IN 16 #define UCSI_MESSAGE_OUT 32 +#define UCSIv2_MESSAGE_OUT 272 + +/* UCSI versions */ +#define UCSI_VERSION_1_2 0x0120 +#define UCSI_VERSION_2_0 0x0200 +#define UCSI_VERSION_2_1 0x0210 +#define UCSI_VERSION_3_0 0x0300 + +#define UCSI_BCD_GET_MAJOR(_v_) (((_v_) >> 8) & 0xFF) +#define UCSI_BCD_GET_MINOR(_v_) (((_v_) >> 4) & 0x0F) +#define UCSI_BCD_GET_SUBMINOR(_v_) ((_v_) & 0x0F) /* Command Status and Connector Change Indication (CCI) bits */ #define UCSI_CCI_CONNECTOR(_c_) (((_c_) & GENMASK(7, 1)) >> 1) From patchwork Tue Jan 23 22:30:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Pandit-Subedi X-Patchwork-Id: 13528228 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 0D0965465E for ; Tue, 23 Jan 2024 22:30:46 +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=1706049048; cv=none; b=MFAaRxYFpN/bRYlFemkNhIsQJwZ+4TS3wDJ1EDwO8P98H80JgE/2SUWmcF/pl5djWWbAOjz4tO8E7rQI/b5IZnqhA8Xht7uEPHjPNmkA3BqNYu8kr+vpeS1yCpdx4HcHCjo8osUo/p9rOoUQhOgEI+wn5aPtTzAC/YSfmpb2kis= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049048; c=relaxed/simple; bh=kf7yJI0PSynYy3En1UbYNrF5sIujOUcOGcuO2Brc3lg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KnKpUUHw0zjzucm5Ze16MZzL3E0KUnR5qy3lYnOp4gPv507ErBmZWgUvdpmd4UX31KOD6K+qX7xpmwzJ1fKyVjzFISHjHHkP695EB6GIDiYXj/0aXlH1i6mJPWx6ySQF9zYnGJcOGVMO8fBGVRQVEU6EwH5J3ijGA/mejVF5KqI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--abhishekpandit.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=b4OD7+G2; 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--abhishekpandit.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="b4OD7+G2" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc2470bc0bdso6529968276.1 for ; Tue, 23 Jan 2024 14:30:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706049046; x=1706653846; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2vpNRulCzZMNU5JocFm5dd+td45+IWqD0hs2FPi7+Eo=; b=b4OD7+G2XdgXMPZrHTuh9Hd0lRAiN3RdB9dNXfAeUVANpKrATRyxQoHAq4BgV5F1uN fpBEsgIJEcI4wWw3R2+zZdloBYVNY/fA3lw/XhCuWKAN/G9brUKmQG5QsvLQbn7LiH94 Le0ZZbuqRkKZVYIMR7a5qjJzBP2ZLHaLHI7qI2PCp5Isf5x3GLPoOc9YiKrH7+4MQDxI MjkAgKtJnWclP1R6neEYD7C9WLcpdAbMDwfgIjPZ3dPRMrZ2gAcvUv/hGZYKzSErGDmc E0dXigVpPREi3GsLituRmZYETj8eDjWY4MaFslCbhFWomJdSUdCiRv0gf0zR3VYhOwOQ vhMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706049046; x=1706653846; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2vpNRulCzZMNU5JocFm5dd+td45+IWqD0hs2FPi7+Eo=; b=T3imJiZlpFYnCDuGBCx++MHUq84Z58G3yZRSx9vfmvNYqvTc8kHK05Ku24MjzQ9OQ8 1i45foVnKEFTPHmklGGw5XmD5DFFfp+V0zF2sYKSJ72+d1Swu8th+8xGdFVg9RAYTZsU vdid31si0V6iB/L7dIrcKL/wVhOxrOWf/XIDZLR7s/DIA1mCeKdFxrng+tliuvnoh6Xe vfV95xxrfOvKUn4iKA6BsltvY76BIxZehmcV2DuS1LWUGQcBnQV2ZU+HSDR2incYR7BQ ioRLJ6z2/F4mgJsXhl8b5bxXP43rNP0BZjvHm/EsDsa0WHgb1nEnTsSMrxcpQ8Q3CsL6 gtUQ== X-Gm-Message-State: AOJu0YxBcl09FmqRuWcEYTu3ysBmxpd0l8y1X4mVjnPzDAJawPC63qQ2 uS/XBLKLJAlFf+pO2Irqj6KL+WXpz+4dF/4lXhPq1kbl4BM2eTy1bPaDAzLgegYtmuym2aP3AqW p7BEY/6hrkCNve654R6GPLrTZ4uqOc/eH9A== X-Google-Smtp-Source: AGHT+IEKXaLwcIhuIIjt7ZY+gcERRVNWvf7dQiKd6y1V/MnqABqCPAsRs6xT4Xi282+JqMW7qgWOSI3RmS+eQFHX9BvmuQ== X-Received: from abps.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:9b4]) (user=abhishekpandit job=sendgmr) by 2002:a05:6902:230e:b0:dc2:57c9:b462 with SMTP id do14-20020a056902230e00b00dc257c9b462mr408887ybb.9.1706049046103; Tue, 23 Jan 2024 14:30:46 -0800 (PST) Date: Tue, 23 Jan 2024 14:30:35 -0800 In-Reply-To: <20240123223039.1471557-1-abhishekpandit@google.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240123223039.1471557-1-abhishekpandit@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123143026.v1.2.I3d909e3c9a200621e3034686f068a3307945fd87@changeid> Subject: [PATCH v1 2/3] usb: typec: ucsi: Update connector cap and status From: Abhishek Pandit-Subedi To: Heikki Krogerus , linux-usb@vger.kernel.org Cc: pmalani@chromium.org, jthies@google.com, Abhishek Pandit-Subedi , Bjorn Andersson , Dmitry Baryshkov , Greg Kroah-Hartman , Saranya Gopal , linux-kernel@vger.kernel.org From: Abhishek Pandit-Subedi Update the data structures for ucsi_connector_capability and ucsi_connector_status to UCSIv3. Signed-off-by: Abhishek Pandit-Subedi Reviewed-by: Heikki Krogerus Reviewed-by: Prashant Malani --- Connector status has several unaligned bitfields (16-bit) that result in difficult to maintain macros. It may be better if we simply re-define these structs as u8[] and add bit range macros to access and cast these values. i.e. struct ucsi_connector_status { u8 raw_data[18]; ... \#define UCSI_CONSTAT_CONNECTOR_STATUS FIELD(u16, 15, 0) \#define UCSI_CONSTAT_BCD_PD_VER_OPER_MODE FIELD(u16, 85, 70) } GET_UCSI_FIELD(con->status, UCSI_CONSTAT_CONNECTOR_STATUS); SET_UCSI_FIELD(con->status, UCSI_CONSTAT_CONNECTOR_STATUS, 0); I didn't find a clear example of an existing mechanism to do this. Would love some pointers here if it already exists and some feedback from the maintainer if this is a direction you want to go. drivers/usb/typec/ucsi/ucsi.h | 50 ++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index bec920fa6b8a..94b373378f63 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -3,6 +3,7 @@ #ifndef __DRIVER_USB_TYPEC_UCSI_H #define __DRIVER_USB_TYPEC_UCSI_H +#include #include #include #include @@ -214,9 +215,29 @@ struct ucsi_connector_capability { #define UCSI_CONCAP_OPMODE_USB2 BIT(5) #define UCSI_CONCAP_OPMODE_USB3 BIT(6) #define UCSI_CONCAP_OPMODE_ALT_MODE BIT(7) - u8 flags; + u32 flags; #define UCSI_CONCAP_FLAG_PROVIDER BIT(0) #define UCSI_CONCAP_FLAG_CONSUMER BIT(1) +#define UCSI_CONCAP_FLAG_SWAP_TO_DFP BIT(2) +#define UCSI_CONCAP_FLAG_SWAP_TO_UFP BIT(3) +#define UCSI_CONCAP_FLAG_SWAP_TO_SRC BIT(4) +#define UCSI_CONCAP_FLAG_SWAP_TO_SINK BIT(5) +#define UCSI_CONCAP_FLAG_EX_OP_MODE(_f_) \ + (((_f_) & GENMASK(13, 6)) >> 6) +#define UCSI_CONCAP_EX_OP_MODE_USB4_GEN2 BIT(0) +#define UCSI_CONCAP_EX_OP_MODE_EPR_SRC BIT(1) +#define UCSI_CONCAP_EX_OP_MODE_EPR_SINK BIT(2) +#define UCSI_CONCAP_EX_OP_MODE_USB4_GEN3 BIT(3) +#define UCSI_CONCAP_EX_OP_MODE_USB4_GEN4 BIT(4) +#define UCSI_CONCAP_FLAG_MISC_CAPS(_f_) \ + (((_f_) & GENMASK(17, 14)) >> 14) +#define UCSI_CONCAP_MISC_CAP_FW_UPDATE BIT(0) +#define UCSI_CONCAP_MISC_CAP_SECURITY BIT(1) +#define UCSI_CONCAP_FLAG_REV_CURR_PROT_SUPPORT BIT(18) +#define UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV(_f_) \ + (((_f_) & GENMASK(20, 19)) >> 19) +#define UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(_f_) \ + (UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV(_f_) << 8) } __packed; struct ucsi_altmode { @@ -276,15 +297,36 @@ struct ucsi_connector_status { #define UCSI_CONSTAT_PARTNER_TYPE_DEBUG 5 #define UCSI_CONSTAT_PARTNER_TYPE_AUDIO 6 u32 request_data_obj; - u8 pwr_status; -#define UCSI_CONSTAT_BC_STATUS(_p_) ((_p_) & GENMASK(2, 0)) + + u8 pwr_status[3]; +#define UCSI_CONSTAT_BC_STATUS(_p_) ((_p_[0]) & GENMASK(1, 0)) #define UCSI_CONSTAT_BC_NOT_CHARGING 0 #define UCSI_CONSTAT_BC_NOMINAL_CHARGING 1 #define UCSI_CONSTAT_BC_SLOW_CHARGING 2 #define UCSI_CONSTAT_BC_TRICKLE_CHARGING 3 -#define UCSI_CONSTAT_PROVIDER_CAP_LIMIT(_p_) (((_p_) & GENMASK(6, 3)) >> 3) +#define UCSI_CONSTAT_PROVIDER_CAP_LIMIT(_p_) (((_p_[0]) & GENMASK(5, 2)) >> 2) #define UCSI_CONSTAT_CAP_PWR_LOWERED 0 #define UCSI_CONSTAT_CAP_PWR_BUDGET_LIMIT 1 +#define UCSI_CONSTAT_PROVIDER_PD_VERSION_OPER_MODE(_p_) \ + ((get_unaligned_le32(_p_) & GENMASK(21, 6)) >> 6) +#define UCSI_CONSTAT_ORIENTATION(_p_) (((_p_[2]) & GENMASK(6, 6)) >> 6) +#define UCSI_CONSTAT_ORIENTATION_DIRECT 0 +#define UCSI_CONSTAT_ORIENTATION_FLIPPED 1 +#define UCSI_CONSTAT_SINK_PATH_STATUS(_p_) (((_p_[2]) & GENMASK(7, 7)) >> 7) +#define UCSI_CONSTAT_SINK_PATH_DISABLED 0 +#define UCSI_CONSTAT_SINK_PATH_ENABLED 1 + u8 pwr_readings[9]; +#define UCSI_CONSTAT_REV_CURR_PROT_STATUS(_p_) ((_p_[0]) & 0x1) +#define UCSI_CONSTAT_PWR_READING_VALID(_p_) (((_p_[0]) & GENMASK(1, 1)) >> 1) +#define UCSI_CONSTAT_CURRENT_SCALE(_p_) (((_p_[0]) & GENMASK(4, 2)) >> 2) +#define UCSI_CONSTAT_PEAK_CURRENT(_p_) \ + ((get_unaligned_le32(_p_) & GENMASK(20, 5)) >> 5) +#define UCSI_CONSTAT_AVG_CURRENT(_p_) \ + ((get_unaligned_le32(&(_p_)[2]) & GENMASK(20, 5)) >> 5) +#define UCSI_CONSTAT_VOLTAGE_SCALE(_p_) \ + ((get_unaligned_le16(&(_p_)[4]) & GENMASK(8, 5)) >> 5) +#define UCSI_CONSTAT_VOLTAGE_READING(_p_) \ + ((get_unaligned_le32(&(_p_)[5]) & GENMASK(16, 1)) >> 1) } __packed; /* -------------------------------------------------------------------------- */ From patchwork Tue Jan 23 22:30:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Pandit-Subedi X-Patchwork-Id: 13528229 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 6DFAF54BCE for ; Tue, 23 Jan 2024 22:30:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049049; cv=none; b=mE8QOj6tUljWGka+OYotCkICysMQ2bbhHTm7EtcjB4zbIMC7cOofyJpiCBKMz4dCBPdi9OQj6WEat8HN3M416Baw1TdnH6ObwMzidvFEYsj2GR5HK/xSFzQFJN55f26QOWzVvbeBh96ZIs4meI3hefAOSH3YHD6ywmWGC2/PYSk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049049; c=relaxed/simple; bh=56wzal+YXRtHQbn7T/LWphhUaXqmgxriKJa43MLSexQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SMkO7NcnrzmYKGNoqAhRL57shqPeFAIAjMPgMovxGus2guYzIfssFXMjNWoqRL8wlzza3w4Tv/og1RF+vpc9w9aGgnIFBkmqSPUVwsm8uvt9A86PQevh+DmYetKG0fo66xC7nzmt97lEdpd7eQMjOukut9j0uCpRQlbUAydrMIg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--abhishekpandit.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TGXJzSl8; arc=none smtp.client-ip=209.85.210.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--abhishekpandit.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TGXJzSl8" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-6dd834c5fe7so473646b3a.2 for ; Tue, 23 Jan 2024 14:30:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706049048; x=1706653848; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KK7B2Hpvjem/TGRGJEqxUJ5QCI/gm35D+XD8uCyWzO0=; b=TGXJzSl8thIqjktun7+24Jdfz3Is/4acLKMif0NBATu7HoQ2f/j3wM6vHvpw79dB6x Ndx0ev+nghQxAoRlnwmg5IELEkuVSxdies5SyA0svatxm5gTubq4Onexjg/q1bH6ueJW mifsV+oeWKk2BDsjj1JLZVE4Udn2Wd9q+VmzA+J0h3MvanCUZBeG4zCi/fCcrcNeeSWt IeJ++qFV+XDmuBI480XN7sLogNoAQ+ep3Jus7cvFEU/rKJYWFMf5S17/kAhmUDyT6U2m igjXAB63t4IhuE46sjiXhoMgfC6KAXEs2+2AvUYIPmO+IOVq8OYhDVLCozlrc9v96ReY wC4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706049048; x=1706653848; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KK7B2Hpvjem/TGRGJEqxUJ5QCI/gm35D+XD8uCyWzO0=; b=SrCXdxe5w5rW4D6ceC0QiQRqCGJiJBtngzhBiC8hayCgKn7QiHiOb0vCNMkoD2fwqY uUqEU7aGQ5bQ3nTCdyzUNKbW9xMb1mQsuiUMnMu8vNJuLQzou8l5Ff/01i6tABkNRM7P 8k6ISoWnZ3Jnr13jQSXQ0Caukvnh4YMYX4i6ucoKPhP1dr1BOvKEXdg4l2gEtihKepvu Vun/cc/QUx5cW3ipZtPTBCJ4wuG5wLMQ4zobzxiVi85EGd5YKBTswsfi+D3AwLB12s7b ECQWOJZrWPeR6rNbAT8PbvbjqPY+Pn5Z9AY3kOu1SXsERfwpUvuoQKz5D+83JVJru8fz qteg== X-Gm-Message-State: AOJu0Yzb1lyawVmMwl8BWaH4D5kanit+DvLf1XHzxwHm7iCoJKrWgTe3 NEuYXWKOJmZSemh0iM7Of0qqJrZLnjFP1A+e4wRtI7WYsHUGPjcJAsOlZX5UfMOpjL24GyWMWjY vF81HciZgQX7C0AxGFFfPOPAmIF+kDfyD/g== X-Google-Smtp-Source: AGHT+IEnuDdXdhLH74mIfU2LZBClUJix1OyeyVi9+KxSoc17xKuUBC9JdyNf43QVD0DklslMm62r6qbMEKDk348MegIQ9w== X-Received: from abps.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:9b4]) (user=abhishekpandit job=sendgmr) by 2002:a05:6a00:985:b0:6db:d97b:6c98 with SMTP id u5-20020a056a00098500b006dbd97b6c98mr617462pfg.2.1706049047859; Tue, 23 Jan 2024 14:30:47 -0800 (PST) Date: Tue, 23 Jan 2024 14:30:36 -0800 In-Reply-To: <20240123223039.1471557-1-abhishekpandit@google.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240123223039.1471557-1-abhishekpandit@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123143026.v1.3.Idf7d373c3cbb54058403cb951d644f1f09973d15@changeid> Subject: [PATCH v1 3/3] usb: typec: ucsi: Get PD revision for partner From: Abhishek Pandit-Subedi To: Heikki Krogerus , linux-usb@vger.kernel.org Cc: pmalani@chromium.org, jthies@google.com, Abhishek Pandit-Subedi , Andy Shevchenko , Bjorn Andersson , Dmitry Baryshkov , Fabrice Gasnier , Greg Kroah-Hartman , Hans de Goede , Neil Armstrong , Saranya Gopal , linux-kernel@vger.kernel.org From: Abhishek Pandit-Subedi PD major revision for the port partner is described in GET_CONNECTOR_CAPABILITY and is only valid on UCSI 2.0 and newer. Update the pd_revision on the partner if the UCSI version is 2.0 or newer. Signed-off-by: Abhishek Pandit-Subedi --- $ cat /sys/class/typec/port2-partner/usb_power_delivery_revision 3.0 drivers/usb/typec/ucsi/ucsi.c | 25 +++++++++++++++++++++++++ drivers/usb/typec/ucsi/ucsi.h | 3 +++ 2 files changed, 28 insertions(+) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 4edf785d203b..8e0a512853ba 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -782,6 +782,8 @@ static int ucsi_register_partner(struct ucsi_connector *con) } 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); partner = typec_register_partner(con->port, &desc); if (IS_ERR(partner)) { @@ -856,6 +858,28 @@ static void ucsi_partner_change(struct ucsi_connector *con) con->num, u_role); } +static int ucsi_check_connector_capability(struct ucsi_connector *con) +{ + u64 command; + int ret; + + if (!con->partner && !IS_MIN_VERSION_2_0(con->ucsi)) + return 0; + + command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num); + ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap)); + if (ret < 0) { + dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret); + return ret; + } + + typec_partner_set_pd_revision( + con->partner, + UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(con->cap.flags)); + + return ret; +} + static int ucsi_check_connection(struct ucsi_connector *con) { u8 prev_flags = con->status.flags; @@ -925,6 +949,7 @@ static void ucsi_handle_connector_change(struct work_struct *work) if (con->status.flags & UCSI_CONSTAT_CONNECTED) { ucsi_register_partner(con); ucsi_partner_task(con, ucsi_check_connection, 1, HZ); + ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ); if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) == UCSI_CONSTAT_PWR_OPMODE_PD) diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 94b373378f63..5e60328f398e 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -36,6 +36,9 @@ struct dentry; #define UCSI_BCD_GET_MINOR(_v_) (((_v_) >> 4) & 0x0F) #define UCSI_BCD_GET_SUBMINOR(_v_) ((_v_) & 0x0F) +#define IS_MIN_VERSION(ucsi, min_ver) ((ucsi)->version >= min_ver) +#define IS_MIN_VERSION_2_0(ucsi) IS_MIN_VERSION(ucsi, UCSI_VERSION_2_0) + /* Command Status and Connector Change Indication (CCI) bits */ #define UCSI_CCI_CONNECTOR(_c_) (((_c_) & GENMASK(7, 1)) >> 1) #define UCSI_CCI_LENGTH(_c_) (((_c_) & GENMASK(15, 8)) >> 8)