From nobody Sun Feb 8 13:11:09 2026 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) (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 A905921A447 for ; Fri, 6 Feb 2026 11:58:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770379085; cv=none; b=NuQfRwBATeScxl628jBzcrLSZOPioBJI3UqKFxmcX1xOWbeb/uvMzr22+xcZtCRRyQJ7RDpcadSf3ewQdn0nR/wk/35rZl8314ycKsUmjQ41OJNT+aHhUE5juhzg5ugvQRwEnb+XBPx0xWeZSwZj3CQKFcuAZOwRwxYIau0bYQ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770379085; c=relaxed/simple; bh=rUIhMipKm3DSYi4rMpTxaSLCerFl3MWcIQdR3tTgN6M=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=ukoFhBqlihMO9GPKl1IWx3nloSw/lRMnaVIer0JMkK/PqCMFchzmFeasY7/hos80jdVlP9XJYK1jKyjl4/uOej3uUHBzVKaS39ga3VuTQ6RJOn0QzztgZx6KfTSkltSXSG9UATR8zX5U4BPLtK2FaEO3Q9jKrmB9VEc0I9nQQQU= 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=Hpx+MOhv; arc=none smtp.client-ip=209.85.208.47 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="Hpx+MOhv" Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-64b92abe63aso3878395a12.0 for ; Fri, 06 Feb 2026 03:58:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1770379083; x=1770983883; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=2hyTsbP3mhP9Lt+xVfNMhvW7SHAmDYkzNc2qvfLi+XU=; b=Hpx+MOhv6tr+iCPtI5zakPVlZdI5aNLWdwy4PRwJXaVXneSoIqfKNkwkiuNeVww50O H+3ysrFAyFoyU8AeqPPjl1gQNcHPpjEBI7WkQ0CYwij8pPLPbdHrDd1etr4aMX9GeUcI y/xp2QyMvrrP8gu4QK9W7OBnDQgv4vgEwLRNM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770379083; x=1770983883; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=2hyTsbP3mhP9Lt+xVfNMhvW7SHAmDYkzNc2qvfLi+XU=; b=E46vf3iFa0zQI19BFOqz+H+wHVDruroBtKtX9TXXHcPm3ytaFIHVzEQJAUSefqNt5F xGOGTeRWjS8VjbyLJeiXi30Rg/UFCrP9GxfMQzK4WiZMEBf9Zs9H6ACakMoW1qjKaTLw uMw8xefc1EEzuObC5bvnKXkXQ0nece5wDTP9PYn4Q1Ev3fKTIfzc3v8kV0RB/VM/BvQ6 cGUn/gzHV913l7imfwANMq527DqyGfGSnkU5TPbz4Elso11ZE9yB0VvFMppMCCyWNHMy YTgLKvxUfqwASnWwrBOhouQrvEWUFWq7n5N9gUZ118RsPmZYV0PI0u9nsabMcEctW5tb u7Qg== X-Forwarded-Encrypted: i=1; AJvYcCWmY/U4vmJ3yf5TSEufCxNQMM1Whh74GI9QhxcRRrvrEei1h6rDsldJj7ElNTjCaPijuJDWDheabmKlIpU=@vger.kernel.org X-Gm-Message-State: AOJu0YyEpnECmg5rKpz7D8+FYYMFviCXK51My75BXXfPhhXVDEqzIA3Z 8zXA4uZEWdWmE7K/w0eeDxQeZOKBuy3YIY+9p69FFht/IxxuZoPwaA1hOxOeiHF/Gw== X-Gm-Gg: AZuq6aJaY2aSP1nabLi3nOHvyvICMpklQm+fnAakzVYlIbUtdPfxK7IGkq32hxfIm7j u0BvAg5zeXaMEt6BAevpI9lq06kYv5azVJ1kRNRXsb0Gtq0Ew0t/lfXTBtcJVIfH/EKVm7fim9E vX7pdxarpawltLSG9upC/3Mmz0sXpNMxMx3/JEUZ3dHQxDuuUfQfyWfCcsx+A59hxWVH5VPxuy3 aoZQgamtGHF+1GBec6fZ7u62sAwiJ1azagNH5oukNg4tufLalMrMyc+OjnBJPa7SLgXGgTjOTiw YmIrjVUduS4FsefdtpQ1CGPYc2cqm4Kl74+cwAUWas9Qiv3UPJjdlaSYt9tVBY/0WcA7WlvmikM OhQKPUWP2YWaO51USwxZ/wI6uYSq3DC+cQm/Pd1IXmFw/V1/LPQocAD2GE4Xim3dunrWwTmddGU ENGhjMrpt0l9uRFFRcq6FoegctCT86hXdxg8rMchGMjsDI/NMqNfR0gIL6BVLNn/q7PGrD/PLTu Vc= X-Received: by 2002:a05:6402:2706:b0:658:377e:f05d with SMTP id 4fb4d7f45d1cf-6598417d31emr1061015a12.24.1770379082989; Fri, 06 Feb 2026 03:58:02 -0800 (PST) Received: from akuchynski.c.googlers.com.com (6.187.91.34.bc.googleusercontent.com. [34.91.187.6]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-65983eb68cfsm491210a12.12.2026.02.06.03.58.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Feb 2026 03:58:01 -0800 (PST) From: Andrei Kuchynski To: Heikki Krogerus , Jameson Thies , Benson Leung , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Abhishek Pandit-Subedi , Greg Kroah-Hartman , Dmitry Baryshkov , Pooja Katiyar , Johan Hovold , Hsin-Te Yuan , Madhu M , Venkat Jayaraman , Pengyu Luo , Fedor Pchelkin , Andrei Kuchynski Subject: [PATCH v2] usb: typec: ucsi: Add Thunderbolt alternate mode support Date: Fri, 6 Feb 2026 11:57:54 +0000 Message-ID: <20260206115754.828954-1-akuchynski@chromium.org> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce support for Thunderbolt (TBT) alternate mode to the UCSI driver. This allows the driver to manage the entry and exit of TBT altmode by providing the necessary typec_altmode_ops. ucsi_altmode_update_active() is invoked when the Connector Partner Changed bit is set in the GET_CONNECTOR_STATUS data. This ensures that the alternate mode's active state is synchronized with the current mode the connector is operating in. Signed-off-by: Andrei Kuchynski Reviewed-by: Heikki Krogerus --- Changes in V2: - Update copyright year to 2026. - Explicitly include all required headers. - No functional changes compared to V1. drivers/usb/typec/ucsi/Makefile | 4 + drivers/usb/typec/ucsi/thunderbolt.c | 212 +++++++++++++++++++++++++++ drivers/usb/typec/ucsi/ucsi.c | 18 ++- drivers/usb/typec/ucsi/ucsi.h | 20 +++ 4 files changed, 249 insertions(+), 5 deletions(-) create mode 100644 drivers/usb/typec/ucsi/thunderbolt.c diff --git a/drivers/usb/typec/ucsi/Makefile b/drivers/usb/typec/ucsi/Makef= ile index dbc571763eff6..c7e38bf01350d 100644 --- a/drivers/usb/typec/ucsi/Makefile +++ b/drivers/usb/typec/ucsi/Makefile @@ -17,6 +17,10 @@ ifneq ($(CONFIG_TYPEC_DP_ALTMODE),) typec_ucsi-y +=3D displayport.o endif =20 +ifneq ($(CONFIG_TYPEC_TBT_ALTMODE),) + typec_ucsi-y +=3D thunderbolt.o +endif + obj-$(CONFIG_UCSI_ACPI) +=3D ucsi_acpi.o obj-$(CONFIG_UCSI_CCG) +=3D ucsi_ccg.o obj-$(CONFIG_UCSI_STM32G0) +=3D ucsi_stm32g0.o diff --git a/drivers/usb/typec/ucsi/thunderbolt.c b/drivers/usb/typec/ucsi/= thunderbolt.c new file mode 100644 index 0000000000000..434d3d8ea5b6e --- /dev/null +++ b/drivers/usb/typec/ucsi/thunderbolt.c @@ -0,0 +1,212 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * UCSI Thunderbolt Alternate Mode Support + * + * Copyright 2026 Google LLC + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ucsi.h" + +/** + * struct ucsi_tbt - Thunderbolt Alternate Mode private data structure + * @con: Pointer to UCSI connector structure + * @alt: Pointer to typec altmode structure + * @work: Work structure + * @cam: An offset into the list of alternate modes supported by the PPM + * @header: VDO header + */ +struct ucsi_tbt { + struct ucsi_connector *con; + struct typec_altmode *alt; + struct work_struct work; + int cam; + u32 header; +}; + +static void ucsi_thunderbolt_work(struct work_struct *work) +{ + struct ucsi_tbt *tbt =3D container_of(work, struct ucsi_tbt, work); + + if (typec_altmode_vdm(tbt->alt, tbt->header, NULL, 0)) + dev_err(&tbt->alt->dev, "VDM 0x%x failed\n", tbt->header); + + tbt->header =3D 0; +} + +static int ucsi_thunderbolt_set_altmode(struct ucsi_tbt *tbt, + bool enter, u32 vdo) +{ + int svdm_version; + int cmd; + int ret; + u64 command =3D UCSI_SET_NEW_CAM | + UCSI_CONNECTOR_NUMBER(tbt->con->num) | + UCSI_SET_NEW_CAM_SET_AM(tbt->cam) | + ((u64)vdo << 32); + + if (enter) + command |=3D (1 << 23); + + ret =3D ucsi_send_command(tbt->con->ucsi, command, NULL, 0); + if (ret < 0) + return ret; + + svdm_version =3D typec_altmode_get_svdm_version(tbt->alt); + if (svdm_version < 0) + return svdm_version; + + if (enter) + cmd =3D CMD_ENTER_MODE; + else + cmd =3D CMD_EXIT_MODE; + tbt->header =3D VDO(USB_TYPEC_TBT_SID, 1, svdm_version, cmd); + tbt->header |=3D VDO_OPOS(TYPEC_TBT_MODE); + tbt->header |=3D VDO_CMDT(CMDT_RSP_ACK); + + schedule_work(&tbt->work); + + return 0; +} + +static int ucsi_thunderbolt_enter(struct typec_altmode *alt, u32 *vdo) +{ + struct ucsi_tbt *tbt =3D typec_altmode_get_drvdata(alt); + struct ucsi_connector *con =3D tbt->con; + u64 command; + u8 cur =3D 0; + int ret; + + if (!ucsi_con_mutex_lock(con)) + return -ENOTCONN; + + command =3D UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num); + ret =3D ucsi_send_command(con->ucsi, command, &cur, sizeof(cur)); + if (ret < 0) { + if (con->ucsi->version > 0x0100) + goto err_unlock; + cur =3D 0xff; + } + + if (cur !=3D 0xff) { + if (cur >=3D UCSI_MAX_ALTMODES || con->port_altmode[cur] !=3D alt) + ret =3D -EBUSY; + else + ret =3D 0; + goto err_unlock; + } + + ret =3D ucsi_thunderbolt_set_altmode(tbt, true, *vdo); + ucsi_altmode_update_active(tbt->con); + +err_unlock: + ucsi_con_mutex_unlock(con); + + return ret; +} + +static int ucsi_thunderbolt_exit(struct typec_altmode *alt) +{ + struct ucsi_tbt *tbt =3D typec_altmode_get_drvdata(alt); + int ret; + + if (!ucsi_con_mutex_lock(tbt->con)) + return -ENOTCONN; + + ret =3D ucsi_thunderbolt_set_altmode(tbt, false, 0); + + ucsi_con_mutex_unlock(tbt->con); + + return ret; +} + +static int ucsi_thunderbolt_vdm(struct typec_altmode *alt, + u32 header, const u32 *data, int count) +{ + struct ucsi_tbt *tbt =3D typec_altmode_get_drvdata(alt); + int cmd_type =3D PD_VDO_CMDT(header); + int cmd =3D PD_VDO_CMD(header); + int svdm_version; + + if (!ucsi_con_mutex_lock(tbt->con)) + return -ENOTCONN; + + svdm_version =3D typec_altmode_get_svdm_version(alt); + if (svdm_version < 0) { + ucsi_con_mutex_unlock(tbt->con); + return svdm_version; + } + + switch (cmd_type) { + case CMDT_INIT: + if (PD_VDO_SVDM_VER(header) < svdm_version) { + svdm_version =3D PD_VDO_SVDM_VER(header); + typec_partner_set_svdm_version(tbt->con->partner, svdm_version); + } + tbt->header =3D VDO(USB_TYPEC_TBT_SID, 1, svdm_version, cmd); + tbt->header |=3D VDO_OPOS(TYPEC_TBT_MODE); + tbt->header |=3D VDO_CMDT(CMDT_RSP_ACK); + + schedule_work(&tbt->work); + break; + default: + break; + } + + ucsi_con_mutex_unlock(tbt->con); + + return 0; +} + +static const struct typec_altmode_ops ucsi_thunderbolt_ops =3D { + .enter =3D ucsi_thunderbolt_enter, + .exit =3D ucsi_thunderbolt_exit, + .vdm =3D ucsi_thunderbolt_vdm, +}; + +struct typec_altmode *ucsi_register_thunderbolt(struct ucsi_connector *con, + bool override, int offset, + struct typec_altmode_desc *desc) +{ + struct typec_altmode *alt; + struct ucsi_tbt *tbt; + + alt =3D typec_port_register_altmode(con->port, desc); + if (IS_ERR(alt) || !override) + return alt; + + tbt =3D devm_kzalloc(&alt->dev, sizeof(*tbt), GFP_KERNEL); + if (!tbt) { + typec_unregister_altmode(alt); + return ERR_PTR(-ENOMEM); + } + + tbt->cam =3D offset; + tbt->con =3D con; + tbt->alt =3D alt; + INIT_WORK(&tbt->work, ucsi_thunderbolt_work); + typec_altmode_set_drvdata(alt, tbt); + typec_altmode_set_ops(alt, &ucsi_thunderbolt_ops); + + return alt; +} + +void ucsi_thunderbolt_remove_partner(struct typec_altmode *alt) +{ + struct ucsi_tbt *tbt; + + if (alt) { + tbt =3D typec_altmode_get_drvdata(alt); + if (tbt) + cancel_work_sync(&tbt->work); + } +} diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 251990475faa7..91b6c71dd7396 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -13,6 +13,7 @@ #include #include #include +#include =20 #include "ucsi.h" #include "trace.h" @@ -417,6 +418,9 @@ static int ucsi_register_altmode(struct ucsi_connector = *con, alt =3D ucsi_register_displayport(con, override, i, desc); break; + case USB_TYPEC_TBT_SID: + alt =3D ucsi_register_thunderbolt(con, override, i, desc); + break; default: alt =3D typec_port_register_altmode(con->port, desc); break; @@ -647,12 +651,15 @@ static void ucsi_unregister_altmodes(struct ucsi_conn= ector *con, u8 recipient) } =20 while (adev[i]) { - if (recipient =3D=3D UCSI_RECIPIENT_SOP && - (adev[i]->svid =3D=3D USB_TYPEC_DP_SID || - (adev[i]->svid =3D=3D USB_TYPEC_NVIDIA_VLINK_SID && - adev[i]->vdo !=3D USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) { + if (recipient =3D=3D UCSI_RECIPIENT_SOP) { pdev =3D typec_altmode_get_partner(adev[i]); - ucsi_displayport_remove_partner((void *)pdev); + + if (adev[i]->svid =3D=3D USB_TYPEC_DP_SID || + (adev[i]->svid =3D=3D USB_TYPEC_NVIDIA_VLINK_SID && + adev[i]->vdo !=3D USB_TYPEC_NVIDIA_VLINK_DBG_VDO)) + ucsi_displayport_remove_partner((void *)pdev); + else if (adev[i]->svid =3D=3D USB_TYPEC_TBT_SID) + ucsi_thunderbolt_remove_partner((void *)pdev); } typec_unregister_altmode(adev[i]); adev[i++] =3D NULL; @@ -1318,6 +1325,7 @@ static void ucsi_handle_connector_change(struct work_= struct *work) =20 if (con->partner && (change & UCSI_CONSTAT_PARTNER_CHANGE)) { ucsi_partner_change(con); + ucsi_altmode_update_active(con); =20 /* Complete pending data role swap */ if (!completion_done(&con->complete)) diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h index 4797b4aa1e35b..43a0d01ade8ff 100644 --- a/drivers/usb/typec/ucsi/ucsi.h +++ b/drivers/usb/typec/ucsi/ucsi.h @@ -600,6 +600,26 @@ static inline void ucsi_displayport_remove_partner(struct typec_altmode *adev) { } #endif /* CONFIG_TYPEC_DP_ALTMODE */ =20 +#if IS_ENABLED(CONFIG_TYPEC_TBT_ALTMODE) +struct typec_altmode * +ucsi_register_thunderbolt(struct ucsi_connector *con, + bool override, int offset, + struct typec_altmode_desc *desc); + +void ucsi_thunderbolt_remove_partner(struct typec_altmode *adev); +#else +static inline struct typec_altmode * +ucsi_register_thunderbolt(struct ucsi_connector *con, + bool override, int offset, + struct typec_altmode_desc *desc) +{ + return typec_port_register_altmode(con->port, desc); +} + +static inline void +ucsi_thunderbolt_remove_partner(struct typec_altmode *adev) { } +#endif /* CONFIG_TYPEC_TBT_ALTMODE */ + #ifdef CONFIG_DEBUG_FS void ucsi_debugfs_init(void); void ucsi_debugfs_exit(void); --=20 2.53.0.rc2.204.g2597b5adb4-goog