Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1458394rdb; Wed, 24 Jan 2024 16:45:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IGnbvOrFJcYGCkrzLzi7CY/OKBkouwSiyArOWPd14K0ie3V5E0p6i1tXkftsVpdnGjwMrJ+ X-Received: by 2002:a05:6214:d69:b0:686:abe8:7272 with SMTP id 9-20020a0562140d6900b00686abe87272mr173003qvs.97.1706143541151; Wed, 24 Jan 2024 16:45:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706143541; cv=pass; d=google.com; s=arc-20160816; b=GjynxnrvpktcUEqqLce72HYziEVNsgikDvTR+drW8pe7FUfVcpsoPJiidkEESIOsqC BNa2BPkkU3m2awIdCisRHfYxgq/vVeNZZKPNXLfZDBXRsAGroEZUyVxcwXMBDm+t4xgK vvoQgoYBH3XqHTOSMgCGe9pvtKP292VmPPWx/vslfPKZGjhVesF3F1ny7nW3hvDp+mSa Ti47J3XfDWSauUe6y2lqpf27fpceM3Ggx8q0en9gJe00RHkHTh1GYCtQdRKdAbo1rWBC 7DwXJiZME/6lHgTX9MZoMvb1RLjl9DIeQO8XWzfCQBAqp6igGh9b4UMIvXvOo1pNw+pb Lk+g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=EY+HU/Fe9TNSb7NTRiVg3VzIL1w66/uXrMVPpg/61b4=; fh=4kKco7q+9OfdY4sNU+cxaUgUwGDemo0xbJaub7TfQmo=; b=aEowiMztaB7uRmOgrz3pPQX0Ua0LTkP9rQeTu5qXZUpB/6rNW0Ac5Ue/n26bJxCErp czoqqAsaKIteJZmOdOgyfDFRgmLlNTmN8Y364dJRlp6+4gaAHZDEajoUwJIcnbFiIZqw RBraAEIH3IAOY19U4VwOBoVuRxOoDxmoZ48YGFYytzVVT6d9KNE/QzC8VUfCraU8ZyOl VH0IGKczRkTfDThK9Qov8jElmtmMtiMKEDT9Aysfe1WCLPJCaQ6HfbUpdIKT8moMwmM+ t1TP94dCVSCeHRZ570PrxhnXkf3390v7kPtihJ2aU3DeBrfy/VFZf8WQQ+4a9fda2O06 H3Eg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DWQx2SE1; arc=pass (i=1 spf=pass spfdomain=flex--abhishekpandit.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-37832-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37832-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id w17-20020a0cdf91000000b0067f99bbc781si11482259qvl.109.2024.01.24.16.45.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 16:45:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-37832-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=DWQx2SE1; arc=pass (i=1 spf=pass spfdomain=flex--abhishekpandit.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-37832-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-37832-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 72B751C23D50 for ; Thu, 25 Jan 2024 00:45:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D845A612B; Thu, 25 Jan 2024 00:45:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DWQx2SE1" Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 987D8A3D for ; Thu, 25 Jan 2024 00:45:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706143504; cv=none; b=kYg1LkmNAhvexirpWUIrlWzFZhUHJ/fU4Jim8mZn+jQbTjx6hdTgriengWtfS0j8K3r1GFGRB65233c9FlWvg19oN5onLpNoeZ2wa8WTtyq6iwadWdJMly2GEke1w3WUVqVOoFTdfjWkE0vP2afrmCsuvyiqXzVCzNaBAqhfmYE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706143504; c=relaxed/simple; bh=r6Ufy8qCjB3wudrJmJt1hWXKnwVM0p8XJJRB7q9J22Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aHp9bT60A1BVUq2iY7B/xltL2GfkhVkNjuRqoXM66x4/gxYSUewZ6u65hm9DWkW0uOx8IwBjJs6xqlu8hs7kKQx2cCK8gbXbGhG/CE+ilRfcE8VvIsUKBoGnArBZIdilUChE4Yv0Qi3FZx1hkwINub68tflJevcnWXHFy0tRk6I= 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=DWQx2SE1; arc=none smtp.client-ip=209.85.215.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 Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-5d65aae8f8fso120682a12.2 for ; Wed, 24 Jan 2024 16:45:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706143502; x=1706748302; 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=EY+HU/Fe9TNSb7NTRiVg3VzIL1w66/uXrMVPpg/61b4=; b=DWQx2SE1BSrj4RwXVuMA0avN+K9LETETDUfL/C+b5Px/CNQQUTNHLOvQiG4QnIdoBB 7zkJiUow2ce01+K1UfRtMoV8TLPY/PdvBrVB3z0Qjsjh0dUD+YCy/xT3MxPpfsf1Voos rghh7Ip01EyxmQnlmeQ52hCMvqIEOthzUq0s58ppFjxGj3E61nDfwrN/gwIYWR0EvkxZ 1ghGMl9uuHjivMTAhyX07/5Acwe0UjB5ioOePqkGQmCXH/C42wzkGd1GZ5xZ6EuPlwie UfWtRZ8ZKv7aJvaxke7rWwjQgrgVjiJEZeMO4T1lYFHqHJJz6JyuJ6iBj8e0jE9zP9AU NNWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706143502; x=1706748302; 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=EY+HU/Fe9TNSb7NTRiVg3VzIL1w66/uXrMVPpg/61b4=; b=AKwPGmkE4SlKCH6ptiNXH13N2atS62/S5aDDvk5EBf0iFPSdVIfmxKIPn2mmPCkJSW a168WNRMIHtp8pSoLI3rw870Bbh5EPoWGn8M4nMOU64J4LnDvRnAFxxxsJPNRuKxbYVV EjkZ7hXChuPLg3N7fpsrauOaTWb/WdVqEeVQCudT2KD7kYZoKkcdsOAanh7BsC8gbOHo xCXEFSnJLPq1JUynTNSI2MC0ZSGZIS0thHPpBd4brhILOui3YIISAU6vFK4W8cvZLKnQ mvD3rM/6/tfs0aij2387pAbwHMsXe08Xk8ujO9hLh5iTBzP1xJUTS4x7jpKGzUyI5j6J QTxA== X-Gm-Message-State: AOJu0YyTnU9r06aJKKiyKKHbAGD0lzP6y18+jpEcpN6Bi0Cj/ZdLO6Ey fuWRqo+U0ZPKok/0bwb2/SSye2L+IwwNjNCKAAn609VxIax12Sc0wZAwZYQHmY7IvXGeOgJ01Mo taZk1PNVPtADcdb4+TFIFsKw+sdIno1GMtQ== X-Received: from abps.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:9b4]) (user=abhishekpandit job=sendgmr) by 2002:a65:6856:0:b0:5ca:3a8e:7505 with SMTP id q22-20020a656856000000b005ca3a8e7505mr223pgt.2.1706143501636; Wed, 24 Jan 2024 16:45:01 -0800 (PST) Date: Wed, 24 Jan 2024 16:44:52 -0800 In-Reply-To: <20240125004456.575891-1-abhishekpandit@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240125004456.575891-1-abhishekpandit@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240124164443.v2.1.Iacf5570a66b82b73ef03daa6557e2fc0db10266a@changeid> Subject: [PATCH v2 1/3] usb: typec: ucsi: Limit read size on v1.2 From: Abhishek Pandit-Subedi To: Heikki Krogerus , linux-usb@vger.kernel.org Cc: jthies@google.com, pmalani@chromium.org, Abhishek Pandit-Subedi , Bjorn Andersson , Dmitry Baryshkov , Fabrice Gasnier , Greg Kroah-Hartman , Hans de Goede , Neil Armstrong , Saranya Gopal , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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 --- 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 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) -- 2.43.0.429.g432eaa2c6b-goog