From patchwork Fri Jan 26 18:39:07 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: 766812 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (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 EA0BE1DDEE for ; Fri, 26 Jan 2024 18:39:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706294380; cv=none; b=pRpzueqz9w6b20t5BfNu2fR59Dm6RwtSq0Jrn+3/mWePSXmtWkw5/Yq2MBde1MYiKlqrZcNg9ujdBOrkkcg6JSLLD9ULcTZFFzxQ7CQBSrhor84SQ0i1dMX49mym6stikj7Nkah7apGsVL4CWCnHcYtOWaRgtKesft0IJxeqqow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706294380; c=relaxed/simple; bh=Zf4po2lIj74L/2H1x9dpz7ZypE07tj/ZkQxhvMSDimI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UCzvACm6F9tP2U9ETOVWWcLfUEeZ2X1zRcM3SN8DJHWTP6lFOTgfyClcez67C9alLdQZFwZMLxpnf6FaGMUG35WyxdivZQaFAWWClFi5TK04qhIh4DWa+n/3gLmxm4lBAvOaTbN2Vq92rmLV5JD/3GA9WzcUfSj9RACVDmKtSBI= 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=CtTVhxSm; arc=none smtp.client-ip=209.85.215.180 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="CtTVhxSm" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-5d691a971ecso530118a12.1 for ; Fri, 26 Jan 2024 10:39:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1706294378; x=1706899178; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AHV9AzCH6l6u75fGidc3n6qwYGzVGl876orPWZA6mpw=; b=CtTVhxSmoYzNOeFeQe4PY1bFGrB51bfn+jTAhsBDPIkJD24qPJ2xQpazxmlUr5fpR2 QkaKZZod5ieorCm+wykv/S96kILXJpb4GZZ+vPGrMyUQgCM/0+T7uULz/NCMTo0eeGtl uAZFG8bzRhoc3aT16eEiuJtrzLt8nTEWvIFJk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706294378; x=1706899178; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AHV9AzCH6l6u75fGidc3n6qwYGzVGl876orPWZA6mpw=; b=teaOnmZc4aKvfgm/spk/xNFoZDtSttRPlxNI7/bMCkkMM6zVZxMGJNIq+76AcIWaRw 43eqIoY2JnIoZub4kGj6IFsr1wrUxu4ERP7dKBkMMrXfuk8gk7jysc0kbaMvmzf8NYRk OUCWR6sjBZa3w9DfMKm9vQliSfch72MumQtYVJxOhqx10zJQF/avcRiAkbj378ZeJFtH p2JuZ4po+brjuqt8fiYgc+/q4jaK+KuRqCd0MIhYygETo77rd9VM1SdRZ/JX7OdEmEzr nnB0F+kEzSPmbXH2hilK1tsuGPUJQ3WqmymOaM1NwEQuFu2Ukhqc4maHXXsyiwKq0REK l9/A== X-Gm-Message-State: AOJu0YywIbLbIBv0GPTDVgbOy/5A/n7lMqaLO1I0J0ju9UMWulo5LzQS wxUa43O21iemRxJywu1E4DiNR9a8IcCfQL6TOB2CaCzsYNoi1GAN+mtq6bjMsA== X-Google-Smtp-Source: AGHT+IGJzKM/7LHkDWwhdEcMQvj3Y2OcI7SiCfcl/LNcdq7TRPU82AhEx8wDF2COYeF11jlgQ5pZKw== X-Received: by 2002:a05:6a20:6a94:b0:19c:881c:8cf8 with SMTP id bi20-20020a056a206a9400b0019c881c8cf8mr117328pzb.95.1706294378274; Fri, 26 Jan 2024 10:39:38 -0800 (PST) Received: from localhost (209.148.168.34.bc.googleusercontent.com. [34.168.148.209]) by smtp.gmail.com with UTF8SMTPSA id y17-20020a62f251000000b006dd8643d468sm1382561pfl.193.2024.01.26.10.39.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Jan 2024 10:39:37 -0800 (PST) From: Abhishek Pandit-Subedi To: Heikki Krogerus , linux-usb@vger.kernel.org Cc: pmalani@chromium.org, jthies@google.com, Abhishek Pandit-Subedi , Dmitry Baryshkov , Fabrice Gasnier , Greg Kroah-Hartman , Hans de Goede , Neil Armstrong , Prashanth K , Rajaram Regupathy , Saranya Gopal , linux-kernel@vger.kernel.org Subject: [PATCH v3 1/3] usb: typec: ucsi: Limit read size on v1.2 Date: Fri, 26 Jan 2024 10:39:07 -0800 Message-ID: <20240126103859.v3.1.Iacf5570a66b82b73ef03daa6557e2fc0db10266a@changeid> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog In-Reply-To: <20240126183930.1170845-1-abhishekpandit@chromium.org> References: <20240126183930.1170845-1-abhishekpandit@chromium.org> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 Reviewed-by: Heikki Krogerus --- 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 (no changes since v2) Changes in v2: - Changed log message to DEBUG 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..a35056ee3e96 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_dbg(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)