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: 192757 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2395:b0:106:343:edcb with SMTP id gw21csp79746dyb; Fri, 26 Jan 2024 10:52:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IHn6FGkH0IvZzXUynq6BuZDKQZg1J3Jo/UbfTK08NMATZryay6yyWZO85B4gBT8nmw/gG29 X-Received: by 2002:a05:600c:1e24:b0:40e:4278:fce1 with SMTP id ay36-20020a05600c1e2400b0040e4278fce1mr186321wmb.26.1706295121345; Fri, 26 Jan 2024 10:52:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706295121; cv=pass; d=google.com; s=arc-20160816; b=b1Xcr5xrIdTZA8BSgSTJkLPHM8CHHOs7wH7MdxxN+nMHR3zSIINSiqXYxW2Ggxd8lV NQM/k0bcAnDLc0OI1YqmywS3/WpGg4Sq0YKKE12tcvEHv7QtC8u2dDdhC9GmXBwmbKJO 8qh7C9jOtg8Lnd21H/QbKZL8A1F/PWROV5IVcrR/ufGxh4W9LDRohYKSwURFqPb4alQo oDVuHPErSydKDiXMA6440T7H+8Ym66hutp3zbR50bvlBQSAtJROPn3ooQSRtlBFUXh5d Rc75taikK0EqSSQdNwkmYizSp0m6m8IGim/za4YWdjlP9CW8jE9Ua4KiEzH96ew0MpeO lvsw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=AHV9AzCH6l6u75fGidc3n6qwYGzVGl876orPWZA6mpw=; fh=w2MPdyaAybOrB6cGMU/0uPA7W/+Kk8Q6Hs6NxRFb5kg=; b=IB10irtSQssvpYeqESBj41FHIABc8Er/E96qUfUyj93UM9SOwQ2DHGyaQfbmXTaUue JjA5VHBtzj49yRvExqnz8joGGofb/YnVd/nBwRDkBsYQVzz+DnO/uJhzVePBi6XlQQOp YrhuZeNQ3Pl7coqjK+AYdPxTqWiMcuPU1rU/W/bEv3mUJUrgFiFV8gjx1aHlZ+fGDTg6 Bj8Vgry9DUxAwiX7WZW6QZma2XmKdiyo4mDMS1maV+vEcYlkD7vMrfEA9ewkqTvHiWl5 3BC2XVdqulfwHFfHIZ+FinXaIQGL/56HLH2pUIcVV63695UTxKNl4RmH5m/pyVoFeM+D D8yg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=CtTVhxSm; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-40516-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40516-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id z5-20020a056402274500b0055ca92fb6c6si984858edd.34.2024.01.26.10.52.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 10:52:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-40516-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=CtTVhxSm; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-40516-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40516-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 69BB41F27613 for ; Fri, 26 Jan 2024 18:43:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8E20F55793; Fri, 26 Jan 2024 18:39:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="CtTVhxSm" Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (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 F05FF1DDFC for ; Fri, 26 Jan 2024 18:39:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706294380; cv=none; b=LZEgydHaOXFt1GlNhA/avZUfb1zjaUb8zYpSwFH22kW91YAtv6bfFLXmD6AfrzqnXL2xG0bYY8Rm0OoGUh1i6EelCCw9vMdUk87taSvYTw+dCLqjjoB0/U+pfo3YufjRd1Xwdy3JX+36uuFW1/MqPUCQYSqIVC9NbJYuv9ciyXk= 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.177 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 Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-5c65ca2e1eeso452474a12.2 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=SYY+/USIhwXXnGRoOWywd0xrr6XQyfzfWmbd7686xuBEiQxR+yvG662UA7nJgl3Bhg YgJqW+jd9cfeuUl7fFcA0IAyBxYl3cnVgg5IC8DqiK3fNqJBEzbVd7N8RDX4dAFq9axM H8sW/XGITvq1y8qwj27wGSrxdVUAgklfTK+fg+2AgPv7H2oNyLAPukkUwlPit8uM6vuS SajD1Z+Z8Hymayh26i/dpkDO9p1g1bSgHsnvv3Rs6KbJ/O6FFASgM9n5PEJYQKBUVxtU ok6AV9qrxmxh3caNeWyoUJOfzfluV5Im6HZsV4Jl7KC6yJTRqXAwrMfxtP8ML1SjztuZ Eomg== X-Gm-Message-State: AOJu0YxrM84u8wRe49hRVTAp8CKHSahCLXQiR5odUobIqBLc5VSwUOVv 5bRfOaebrg8ZPKbgM3hsvwEY4WVGJImr/8MQnzu1OUKnNPMOCL3Kqwi9KxYRCw== 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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789180112689932126 X-GMAIL-MSGID: 1789180112689932126 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) From patchwork Fri Jan 26 18:39:09 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: 192750 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2395:b0:106:343:edcb with SMTP id gw21csp75860dyb; Fri, 26 Jan 2024 10:44:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IErw/blQrQmWWuxxCR9fCZKrMDRvhACUp/lsubxK5h9A7bxwBiDi0IyKoDCO/RK8hSvTLzl X-Received: by 2002:a0c:ca8c:0:b0:68a:c1d8:4395 with SMTP id a12-20020a0cca8c000000b0068ac1d84395mr2136386qvk.3.1706294651215; Fri, 26 Jan 2024 10:44:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706294651; cv=pass; d=google.com; s=arc-20160816; b=hGiMV0Na1NPH58ollzojHQ5Ngp8+0JTnyij5RnyZxZLGCiA1KyuMo8CK++7GBD7zAv f5+G7WET3V+fWbPvKmeMKSo/QgOdpDqIKH1mlfldTxVe60d/7B2zb4NHDA2pNirEnZc7 ixU6+g8EOtPAy5YXvjlYhUotEv4yRCiuiKvoljtLqDJLpXweZVf7QqtxTY58a4PdrDZU h+CsywvKxBrtoZ8tmi4ASvqRfQiaCOcelwTCjmiklYsGuinYHHInx3iwC/zlwdPXjrlx Ep92LMxxYqH3qNfee0/Kz2IWxaPYEKku1tqaENtHMQsKn0nMpiLLtGNq8fL4yIHd4ji3 LDwA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=2QQrn5zdRS2vcBUSW5uiCOjM9d8+vCQMueTf6c01I2Q=; fh=kSArWcDrdhNNVZGpbY6aYsaq5fuUnfufylfOj7rN9T4=; b=eO+uY2MOdKvSsw8vXeeiAHGcnKqA70LJx2uSZ3AqeVPgzJr6MSBp7nlAnfTtnfskn8 /QuNQ2HShpNZibBy0b+0tI1B7RbG053kkr7FwWrXrFc7qKwbmWSNbrkZJcdiZSfX12Gd /Qa9xNFJ04m91XM91JvQtnCuPXbCsRf3XW5D+qQTHoSoXl1yw7nshFfNGnjLKucIc58r /uhaW/HVk/sQEj2eTmq/93wbEP3CxFo+PrJIbj7NkQUHNpH8HD79zJOc0sE/YXmDmc+C PP5Vr7cWvBqbgzxS/ObAUSqDdXVNd/EyaQT16MBp40myVNacKYHfLJpuw8tw7YnTKz1c kx1Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GM59ius0; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-40518-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40518-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id ed12-20020ad44eac000000b00681922a623dsi1854930qvb.315.2024.01.26.10.44.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 10:44:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-40518-ouuuleilei=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=@chromium.org header.s=google header.b=GM59ius0; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-40518-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-40518-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org 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 ECF581C22BD2 for ; Fri, 26 Jan 2024 18:44:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 86F1F56766; Fri, 26 Jan 2024 18:39:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="GM59ius0" Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 174B65467D for ; Fri, 26 Jan 2024 18:39:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706294383; cv=none; b=SYfv1spTB0gIXex72cMrQZEUApmISa72oqFKcR5NK72aP8JR7gliaQTnMl1umDUKpUUPlksi6qIVGPhDBYKFNU5X+qiVXW4apgN21pJrgqD/7AWyFt50Q9LOK/UCbqQk27MxW60lrYFRVearuYmyMrKSyufADqNS4nt24Ix0HVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706294383; c=relaxed/simple; bh=qQ20mYMsYljxRmFVBXSfi1rqbWNANcAPCdD/+60IY8U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eGl4SVjKyJitnTA+5ZqtfCvq0PotQheLX4+HQFmSpSwmpIvQgS/sEMJ+xecETJRQFhZyVzdPMzdhdhwAyfcTjkwQXFN8NdzQrpcuroJtO1E8DATE/8QtYhWbQ2GPrIL5r/lDnot2lQ8zBROSREJfyBJdtTDyz/BthAaEofV7f7c= 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=GM59ius0; arc=none smtp.client-ip=209.85.210.172 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 Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6de0ba30994so220334b3a.1 for ; Fri, 26 Jan 2024 10:39:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1706294381; x=1706899181; 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=2QQrn5zdRS2vcBUSW5uiCOjM9d8+vCQMueTf6c01I2Q=; b=GM59ius04vzCGUnLQqoAZYoXawSxOLreTdynV8WlIX6u/2ngpd+RqUUfnCbNs31OAd ubXnSzMA8bAH1LcXFnq8oSRnw1vOxgEyA9KOnAx3+zMiZLZVYurhiB/5Gj/7gil9iRii leZ/yVFHuScREQQ7O1MM8j1RyuQlTsB8UyJPE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706294381; x=1706899181; 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=2QQrn5zdRS2vcBUSW5uiCOjM9d8+vCQMueTf6c01I2Q=; b=Q3fogOmdzpsHC+2l+YsGpRVFT3//SUS5lzMO1MC5viZyd/uIJfmcpeT81ETluw8eTo gIa5xO0UJkmQjENadhNKErlCKiQOtxhJx2w3H+3+F9D+My5JLJU8z3Hm8H0BuqCl77hQ jcmZ8vqgwJPF6ltEcJJLH1kqMoly79lXJJs1Vfd6s+XwzefLX6J47JjHEBOfNbznrLlV 6pK6+kwVcTdhut+ZHuIV079GVqzvQ3bGwOofSCAW3I7JbZEBX1G+4pf2dAZN1DOzjLm/ FKwUx4hSEdsob9vy4fNcc/ISNrdw/IVkHLbB7c9Qkstip0L5+ephbc5S9D8sPqX17Qlx kLxw== X-Gm-Message-State: AOJu0YzFUEmjaojYUxpStzm4zfYQ9/id0bOICv1K+QrGeudJlgVkSZOK Ta+cqhBbcLoeOCLD/dSGWsBToFVz8E7NX/0+aeTYkQapDCZJev1lGKlJJS5Axw== X-Received: by 2002:aa7:8b97:0:b0:6da:af47:95f0 with SMTP id r23-20020aa78b97000000b006daaf4795f0mr272875pfd.27.1706294380577; Fri, 26 Jan 2024 10:39:40 -0800 (PST) Received: from localhost (209.148.168.34.bc.googleusercontent.com. [34.168.148.209]) by smtp.gmail.com with UTF8SMTPSA id r25-20020aa78b99000000b006dbda9a4e6bsm1410678pfd.44.2024.01.26.10.39.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Jan 2024 10:39:40 -0800 (PST) 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 , Rajaram Regupathy , Saranya Gopal , linux-kernel@vger.kernel.org Subject: [PATCH v3 3/3] usb: typec: ucsi: Get PD revision for partner Date: Fri, 26 Jan 2024 10:39:09 -0800 Message-ID: <20240126103859.v3.3.Idf7d373c3cbb54058403cb951d644f1f09973d15@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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789179619953079426 X-GMAIL-MSGID: 1789179619953079426 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 Reviewed-by: Heikki Krogerus Reviewed-by: Prashant Malani --- $ cat /sys/class/typec/port2-partner/usb_power_delivery_revision 3.0 (no changes since v2) Changes in v2: - Formatting changes and update macro to use brackets. - Fix incorrect guard condition when checking connector capability. drivers/usb/typec/ucsi/ucsi.c | 23 +++++++++++++++++++++++ drivers/usb/typec/ucsi/ucsi.h | 3 +++ 2 files changed, 26 insertions(+) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index a35056ee3e96..2b7983d2fdae 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -782,6 +782,7 @@ 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 +857,27 @@ 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 +947,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 1bae4cf8ecdc..d1d0e11b0704 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)