* [PATCH 1/3] usb: typec: tcpm: Keep the Soft_Reset target apart from the VDM one
2026-11-17 11:15 [PATCH 0/3] usb: typec: tcpm: Fixes for talking to the cable plug as a VCONN source Alexey Charkov
@ 2026-11-17 11:15 ` Alexey Charkov
2026-11-17 11:30 ` sashiko-bot
2026-11-17 11:15 ` [PATCH 2/3] usb: typec: tcpm: Leave SRC_VDM_IDENTITY_REQUEST whatever the cable answers Alexey Charkov
2026-11-17 11:15 ` [PATCH 3/3] usb: typec: tcpm: Keep cable plug messages away from the port partner Alexey Charkov
2 siblings, 1 reply; 7+ messages in thread
From: Alexey Charkov @ 2026-11-17 11:15 UTC (permalink / raw)
To: Badhri Jagan Sridharan, Heikki Krogerus, Greg Kroah-Hartman,
RD Babiera
Cc: Sebastian Reichel, linux-usb, Alexey Charkov, stable
tx_sop_type holds the SOP* of the VDM being sent, for its retries, and
SOFT_RESET_SEND also reads it to tell a Soft_Reset for the cable plug,
after a VCONN Swap, from one for the port partner. Nothing sets it back
after a VDM, so once a VDM has gone to the cable plug, such as Discover
Identity on SOP' before the first contract, every Soft_Reset meant for
the port partner goes to the cable plug instead. The cable plug accepts
it and the port goes back to its ready state, but the port partner never
sees the Soft_Reset: a source whose Source_Capabilities got Not_Supported
is then left without a contract. The VCONN Swap, in turn, overwrites the
SOP* of a VDM that may still be pending. This left a FUSB302B-based
source without a contract until it was unplugged, after its partner
missed a message following Discover Identity to an e-marked cable.
Keep one target per context: vdm_sop_type for the VDM and its retries,
and soft_reset_sop_type for the next Soft_Reset. Point the latter at the
cable plug only for the VCONN Swap, only once, and back at SOP whenever
the protocol layer starts over or a Soft_Reset of the port partner
starts, along with the SOP' MessageID counters.
Fixes: 6bd181ba60e1 ("usb: typec: tcpm: add control message support to sop'")
Cc: stable@vger.kernel.org
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/usb/typec/tcpm/tcpm.c | 28 +++++++++++++++++-----------
1 file changed, 17 insertions(+), 11 deletions(-)
diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index bb90211660ca..8c5be904d772 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -675,12 +675,13 @@ struct tcpm_port {
/* SOP* Related Fields */
/*
- * tx_sop_type determines which SOP* a message is being sent on.
- * For messages that are queued and not sent immediately such as in
- * tcpm_queue_message or messages that send after state changes,
- * the tx_sop_type is set accordingly.
+ * Messages that are not sent right away carry their SOP* separately,
+ * one per context so that one cannot redirect another:
+ * vdm_sop_type for the VDM being sent, including its retries, and
+ * soft_reset_sop_type for the next Soft_Reset.
*/
- enum tcpm_transmit_type tx_sop_type;
+ enum tcpm_transmit_type vdm_sop_type;
+ enum tcpm_transmit_type soft_reset_sop_type;
/*
* Prior to discovering the port partner's Specification Revision, the
* Vconn source and cable plug will use the lower of their two revisions.
@@ -2003,7 +2004,7 @@ static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
port->vdm_state = VDM_STATE_READY;
port->vdm_sm_running = true;
- port->tx_sop_type = tx_sop_type;
+ port->vdm_sop_type = tx_sop_type;
mod_vdm_delayed_work(port, 0);
}
@@ -3065,7 +3066,7 @@ static void vdm_run_state_machine(struct tcpm_port *port)
} else {
tcpm_ams_finish(port);
port->vdo_data[0] = 0;
- if (port->tx_sop_type == TCPC_TX_SOP)
+ if (port->vdm_sop_type == TCPC_TX_SOP)
break;
/* Handle SOP' Transmission Errors */
switch (PD_VDO_CMD(vdo_hdr)) {
@@ -3102,7 +3103,7 @@ static void vdm_run_state_machine(struct tcpm_port *port)
case VDM_STATE_SEND_MESSAGE:
/* Prepare and send VDM */
memset(&msg, 0, sizeof(msg));
- if (port->tx_sop_type == TCPC_TX_SOP_PRIME) {
+ if (port->vdm_sop_type == TCPC_TX_SOP_PRIME) {
msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
0, /* Cable Plug Indicator for DFP/UFP */
0, /* Reserved */
@@ -3119,7 +3120,7 @@ static void vdm_run_state_machine(struct tcpm_port *port)
}
for (i = 0; i < port->vdo_count; i++)
msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
- res = tcpm_pd_transmit(port, port->tx_sop_type, &msg);
+ res = tcpm_pd_transmit(port, port->vdm_sop_type, &msg);
if (res < 0) {
port->vdm_state = VDM_STATE_ERR_SEND;
} else {
@@ -5529,6 +5530,7 @@ static void run_state_machine(struct tcpm_port *port)
port->message_id_prime = 0;
port->rx_msgid = -1;
port->rx_msgid_prime = -1;
+ port->soft_reset_sop_type = TCPC_TX_SOP;
port->explicit_contract = false;
/* SNK -> SRC POWER/FAST_ROLE_SWAP finished */
if (port->ams == POWER_ROLE_SWAP ||
@@ -5801,6 +5803,7 @@ static void run_state_machine(struct tcpm_port *port)
port->message_id_prime = 0;
port->rx_msgid = -1;
port->rx_msgid_prime = -1;
+ port->soft_reset_sop_type = TCPC_TX_SOP;
port->explicit_contract = false;
if (port->ams == POWER_ROLE_SWAP ||
@@ -6193,6 +6196,7 @@ static void run_state_machine(struct tcpm_port *port)
case SOFT_RESET:
port->message_id = 0;
port->rx_msgid = -1;
+ port->soft_reset_sop_type = TCPC_TX_SOP;
/* remove existing capabilities */
tcpm_partner_source_caps_reset(port);
tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP);
@@ -6210,6 +6214,7 @@ static void run_state_machine(struct tcpm_port *port)
case SNK_SOFT_RESET:
if (port->ams != NONE_AMS)
tcpm_ams_finish(port);
+ port->soft_reset_sop_type = TCPC_TX_SOP;
port->upcoming_state = SOFT_RESET_SEND;
tcpm_ams_start(port, SOFT_RESET_AMS);
break;
@@ -6220,7 +6225,8 @@ static void run_state_machine(struct tcpm_port *port)
* A Soft_Reset Message Shall be targeted at a specific entity
* depending on the type of SOP* packet used.
*/
- if (port->tx_sop_type == TCPC_TX_SOP_PRIME) {
+ if (port->soft_reset_sop_type == TCPC_TX_SOP_PRIME) {
+ port->soft_reset_sop_type = TCPC_TX_SOP;
port->message_id_prime = 0;
port->rx_msgid_prime = -1;
tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET, TCPC_TX_SOP_PRIME);
@@ -6439,7 +6445,7 @@ static void run_state_machine(struct tcpm_port *port)
case VCONN_SWAP_SEND_SOFT_RESET:
tcpm_swap_complete(port, port->swap_status);
if (tcpm_can_communicate_sop_prime(port)) {
- port->tx_sop_type = TCPC_TX_SOP_PRIME;
+ port->soft_reset_sop_type = TCPC_TX_SOP_PRIME;
port->upcoming_state = SOFT_RESET_SEND;
tcpm_ams_start(port, SOFT_RESET_AMS);
} else {
--
2.56.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 1/3] usb: typec: tcpm: Keep the Soft_Reset target apart from the VDM one
2026-11-17 11:15 ` [PATCH 1/3] usb: typec: tcpm: Keep the Soft_Reset target apart from the VDM one Alexey Charkov
@ 2026-11-17 11:30 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-17 11:30 UTC (permalink / raw)
To: Alexey Charkov; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: typec: tcpm: stale upcoming_state in run_state_machine()
sends SOP reset
--
commit 2100338173ee82724a16da9e2eef51514fc95f54
Author: Alexey Charkov <alchark@flipper.net>
usb: typec: tcpm: Keep the Soft_Reset target apart from the VDM one
This commit splits tx_sop_type into vdm_sop_type and soft_reset_sop_type so
that VDM transmissions and Soft_Reset messages do not overwrite each other's
SOP* target.
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index bb90211660cae..8c5be904d7725 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
[ ... ]
> @@ -6220,7 +6225,8 @@ static void run_state_machine(struct tcpm_port *port)
> * A Soft_Reset Message Shall be targeted at a specific entity
> * depending on the type of SOP* packet used.
> */
> - if (port->tx_sop_type == TCPC_TX_SOP_PRIME) {
> + if (port->soft_reset_sop_type == TCPC_TX_SOP_PRIME) {
> + port->soft_reset_sop_type = TCPC_TX_SOP;
> port->message_id_prime = 0;
> port->rx_msgid_prime = -1;
> tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET, TCPC_TX_SOP_PRIME);
> @@ -6439,7 +6445,7 @@ static void run_state_machine(struct tcpm_port *port)
> case VCONN_SWAP_SEND_SOFT_RESET:
> tcpm_swap_complete(port, port->swap_status);
> if (tcpm_can_communicate_sop_prime(port)) {
> - port->tx_sop_type = TCPC_TX_SOP_PRIME;
> + port->soft_reset_sop_type = TCPC_TX_SOP_PRIME;
> port->upcoming_state = SOFT_RESET_SEND;
> tcpm_ams_start(port, SOFT_RESET_AMS);
> } else {
[Severity: Medium]
When port->pwr_role is TYPEC_SOURCE, can resetting port->soft_reset_sop_type
to TCPC_TX_SOP in SOFT_RESET_SEND cause an unintended SOP Soft_Reset to be
sent to the port partner right after the SOP' Soft_Reset finishes?
In VCONN_SWAP_SEND_SOFT_RESET, port->upcoming_state is set to
SOFT_RESET_SEND before calling tcpm_ams_start(port, SOFT_RESET_AMS). When
port->pwr_role == TYPEC_SOURCE, tcpm_ams_start() transitions directly to
SOFT_RESET_SEND without going through AMS_START, leaving
port->upcoming_state set to SOFT_RESET_SEND:
drivers/usb/typec/tcpm/tcpm.c:tcpm_ams_start() {
...
} else if (ams == SOFT_RESET_AMS) {
if (!port->explicit_contract)
tcpm_set_cc(port, tcpm_rp_cc(port));
tcpm_set_state(port, SOFT_RESET_SEND, 0);
return ret;
}
...
}
Once the SOP' Soft_Reset completes and the state machine returns to
SRC_READY, SRC_READY consumes the leftover port->upcoming_state and
re-enters SOFT_RESET_SEND:
drivers/usb/typec/tcpm/tcpm.c:run_state_machine() {
...
if (port->upcoming_state != INVALID_STATE) {
upcoming_state = port->upcoming_state;
port->upcoming_state = INVALID_STATE;
tcpm_set_state(port, upcoming_state, 0);
break;
}
...
}
Because the first pass through SOFT_RESET_SEND already reset
port->soft_reset_sop_type to TCPC_TX_SOP, the second pass takes the else
branch, clears the partner source capabilities and SOP message IDs, and
sends an unintended PD_CTRL_SOFT_RESET on TCPC_TX_SOP to the port partner,
forcing a full contract renegotiation.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-tcpm-soft-reset-sop-v1-0-c7e1543828c0@flipper.net?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/3] usb: typec: tcpm: Leave SRC_VDM_IDENTITY_REQUEST whatever the cable answers
2026-11-17 11:15 [PATCH 0/3] usb: typec: tcpm: Fixes for talking to the cable plug as a VCONN source Alexey Charkov
2026-11-17 11:15 ` [PATCH 1/3] usb: typec: tcpm: Keep the Soft_Reset target apart from the VDM one Alexey Charkov
@ 2026-11-17 11:15 ` Alexey Charkov
2026-11-17 11:39 ` sashiko-bot
2026-11-17 11:15 ` [PATCH 3/3] usb: typec: tcpm: Keep cable plug messages away from the port partner Alexey Charkov
2 siblings, 1 reply; 7+ messages in thread
From: Alexey Charkov @ 2026-11-17 11:15 UTC (permalink / raw)
To: Badhri Jagan Sridharan, Heikki Krogerus, Greg Kroah-Hartman,
RD Babiera
Cc: Sebastian Reichel, linux-usb, Alexey Charkov, stable
When its first Source_Capabilities goes unacknowledged, a source that is
the VCONN source asks the cable plug for its identity in
SRC_VDM_IDENTITY_REQUEST. Entering that state sets upcoming_state to
SRC_SEND_CAPABILITIES, but the ACK and send error paths go there
directly and leave upcoming_state set, so the first SRC_READY after the
contract sends Source_Capabilities again. The second negotiation can cut
into the sink's own Discover Identity, and once ended in a Hard Reset
between two FUSB302B ports. If the cable plug acknowledges the request
but never answers, or NAKs it, nothing leaves the state at all: the
source sends no Source_Capabilities until the sink gives up with a Hard
Reset, which cycles VBUS.
Schedule the move to SRC_SEND_CAPABILITIES on entering the state, after
tTypeCSendSourceCap, the interval between Source_Capabilities, instead of
setting upcoming_state. Keep the ACK and send error paths moving on at
once, and a message from the sink interrupting as before.
Fixes: af8b627046bc ("usb: typec: tcpm: add state machine support for SRC_VDM_IDENTITY_REQUEST")
Cc: stable@vger.kernel.org
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/usb/typec/tcpm/tcpm.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 8c5be904d772..947fca918414 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -6553,7 +6553,11 @@ static void run_state_machine(struct tcpm_port *port)
/* Cable states */
case SRC_VDM_IDENTITY_REQUEST:
mod_vdm_discovery_delayed_work(port, 0);
- port->upcoming_state = SRC_SEND_CAPABILITIES;
+ /*
+ * Go on with Source_Capabilities whatever becomes of Discover
+ * Identity, unless the cable plug answers sooner
+ */
+ tcpm_set_state(port, SRC_SEND_CAPABILITIES, PD_T_SEND_SOURCE_CAP);
break;
default:
--
2.56.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 2/3] usb: typec: tcpm: Leave SRC_VDM_IDENTITY_REQUEST whatever the cable answers
2026-11-17 11:15 ` [PATCH 2/3] usb: typec: tcpm: Leave SRC_VDM_IDENTITY_REQUEST whatever the cable answers Alexey Charkov
@ 2026-11-17 11:39 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-17 11:39 UTC (permalink / raw)
To: Alexey Charkov; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] usb: typec: tcpm: cable NAK in tcpm_pd_svdm() skips partner
discovery
- [Medium] usb: typec: tcpm: uncancelled VDM in SRC_VDM_IDENTITY_REQUEST
- [Medium] usb: typec: tcpm: sink message in tcpm_pd_handle_msg()
strands port
--
commit 5097e0ac5463844ca0f58a3e399840651ccf8014
Author: Alexey Charkov <alchark@flipper.net>
usb: typec: tcpm: Leave SRC_VDM_IDENTITY_REQUEST whatever the cable answers
This commit replaces the upcoming_state assignment in
SRC_VDM_IDENTITY_REQUEST with a delayed state transition to
SRC_SEND_CAPABILITIES after PD_T_SEND_SOURCE_CAP.
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 8c5be904d7725..947fca918414e 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -6553,7 +6553,11 @@ static void run_state_machine(struct tcpm_port *port)
> /* Cable states */
> case SRC_VDM_IDENTITY_REQUEST:
> mod_vdm_discovery_delayed_work(port, 0);
> - port->upcoming_state = SRC_SEND_CAPABILITIES;
> + /*
> + * Go on with Source_Capabilities whatever becomes of Discover
> + * Identity, unless the cable plug answers sooner
> + */
> + tcpm_set_state(port, SRC_SEND_CAPABILITIES, PD_T_SEND_SOURCE_CAP);
> break;
>
> default:
[Severity: Medium]
If the cable plug replies with CMDT_RSP_NAK in SRC_VDM_IDENTITY_REQUEST,
does leaving the state after PD_T_SEND_SOURCE_CAP skip all SOP partner VDM
discovery once the explicit contract is established?
When CMDT_RSP_NAK arrives over TCPC_TX_SOP_PRIME, tcpm_pd_svdm() assumes
the NAK came from the partner over SOP and marks discovery complete:
drivers/usb/typec/tcpm/tcpm.c:tcpm_pd_svdm() {
...
case CMDT_RSP_NAK:
tcpm_ams_finish(port);
switch (cmd) {
/*
* The cable is not allowed to respond with NAK so this must've happened over SOP
*/
case CMD_DISCOVER_IDENT:
case CMD_DISCOVER_SVID:
tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE);
break;
...
}
When the timer then moves the port to SRC_SEND_CAPABILITIES and later
SRC_READY under an explicit contract, port->vdm_discovery_state is already
VDM_DISCOVERY_COMPLETE instead of VDM_DISCOVERY_UNKNOWN. SRC_READY skips
tcpm_set_initial_svdm_version(), and tcpm_vdm_discovery_work() returns
immediately without discovering the partner's identity, SVIDs, or alternate
modes.
[Severity: Medium]
Can an in-flight SOP' VDM exchange outlive the PD_T_SEND_SOURCE_CAP timeout
and corrupt partner discovery after the port moves to
SRC_SEND_CAPABILITIES?
If the cable plug responds with CMDT_RSP_BUSY, tcpm_handle_vdm_request()
sets VDM_STATE_WAIT_RSP_BUSY and schedules a retry after PD_T_VDM_BUSY
(100 ms). When that timer fires, port->state is still
SRC_VDM_IDENTITY_REQUEST, so vdm_run_state_machine() queues a new SOP'
transmission across PD_T_SINK_TX (16 ms) and PD_T_VDM_SNDR_RSP (30 ms),
which extends past the 150 ms PD_T_SEND_SOURCE_CAP timeout.
When PD_T_SEND_SOURCE_CAP expires and transitions port->state to
SRC_SEND_CAPABILITIES, the active VDM state machine and AMS are not
cancelled. A subsequent SOP' CMDT_RSP_ACK in tcpm_pd_svdm() or TX failure
in vdm_run_state_machine() then sees port->state !=
SRC_VDM_IDENTITY_REQUEST, advances port->vdm_discovery_state to
VDM_DISCOVERY_CABLE_IDENT, and queues CMD_DISCOVER_SVID on SOP before an
explicit contract or partner identity discovery has happened.
[Severity: Medium]
Does dropping port->upcoming_state = SRC_SEND_CAPABILITIES cause a sink
message handled via tcpm_pd_handle_msg() to strand the port in SRC_READY
without an explicit contract?
While in SRC_VDM_IDENTITY_REQUEST, tcpm_ams_interruptible(port) is true.
If the sink sends a message that takes the default interruptible path in
tcpm_pd_handle_msg() (such as an unsupported message or
PD_CTRL_GET_SINK_CAP), tcpm_set_state(port, ready_state(port), 0) clears
port->delayed_state and cancels the pending PD_T_SEND_SOURCE_CAP timer:
drivers/usb/typec/tcpm/tcpm.c:tcpm_pd_handle_msg() {
...
} else {
port->next_ams = ams;
tcpm_set_state(port, ready_state(port), 0);
/* 6.8.1 process the Message */
tcpm_queue_message(port, message);
}
...
}
Once the queued reply is sent, run_state_machine() enters SRC_READY with
port->upcoming_state == INVALID_STATE and port->explicit_contract == false,
so it never returns to SRC_SEND_CAPABILITIES.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-tcpm-soft-reset-sop-v1-0-c7e1543828c0@flipper.net?part=2
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/3] usb: typec: tcpm: Keep cable plug messages away from the port partner
2026-11-17 11:15 [PATCH 0/3] usb: typec: tcpm: Fixes for talking to the cable plug as a VCONN source Alexey Charkov
2026-11-17 11:15 ` [PATCH 1/3] usb: typec: tcpm: Keep the Soft_Reset target apart from the VDM one Alexey Charkov
2026-11-17 11:15 ` [PATCH 2/3] usb: typec: tcpm: Leave SRC_VDM_IDENTITY_REQUEST whatever the cable answers Alexey Charkov
@ 2026-11-17 11:15 ` Alexey Charkov
2026-11-17 11:42 ` sashiko-bot
2 siblings, 1 reply; 7+ messages in thread
From: Alexey Charkov @ 2026-11-17 11:15 UTC (permalink / raw)
To: Badhri Jagan Sridharan, Heikki Krogerus, Greg Kroah-Hartman,
RD Babiera
Cc: Sebastian Reichel, linux-usb, Alexey Charkov
When the port is the VCONN source, messages from the cable plug on SOP'
go through the port partner's handlers, which act on them and answer on
SOP: an unrecognized message gets Not_Supported on SOP, which the partner
answers with a Soft_Reset, and an unexpected Accept soft resets the
partner. SOP'' and the debug SOP* types, which TCPM does not support,
take the same path and shadow the partner's last MessageID.
A cable plug only answers the VCONN source and never starts an AMS (USB
PD r3.3, section 8.5 and table 7.5). Pass on its VDMs, its Accept to a
Soft_Reset sent to it, and its Reject or Not_Supported to a VDM in the
ready state, the only one that takes them as the VDM's answer. Drop
anything else on SOP', and drop other SOP* types before they touch the
MessageID tracking.
This hardens TCPM against cable plugs that break the rules: it was found
with a port posing as a cable plug, and no cable or TCPC driver is known
to trigger it.
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/usb/typec/tcpm/tcpm.c | 62 ++++++++++++++++++++++++++++++++++++++++---
1 file changed, 58 insertions(+), 4 deletions(-)
diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index 947fca918414..2a909ca96811 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -682,6 +682,8 @@ struct tcpm_port {
*/
enum tcpm_transmit_type vdm_sop_type;
enum tcpm_transmit_type soft_reset_sop_type;
+ /* The Soft_Reset in SOFT_RESET_SEND went to the cable plug */
+ bool soft_reset_sent_prime;
/*
* Prior to discovering the port partner's Specification Revision, the
* Vconn source and cable plug will use the lower of their two revisions.
@@ -4164,6 +4166,52 @@ static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
}
}
+/* Whether a cable plug control message responds to what the port sent it */
+static bool tcpm_cable_ctrl_msg_expected(struct tcpm_port *port,
+ enum pd_ctrl_msg_type type)
+{
+ switch (type) {
+ case PD_CTRL_ACCEPT:
+ /* the Soft_Reset after a VCONN Swap */
+ return port->state == SOFT_RESET_SEND &&
+ port->soft_reset_sent_prime;
+ case PD_CTRL_REJECT:
+ case PD_CTRL_NOT_SUPP:
+ /* a VDM, in the ready state, which takes it as the answer */
+ return port->vdm_sop_type == TCPC_TX_SOP_PRIME &&
+ port->vdm_state > VDM_STATE_READY &&
+ port->state == ready_state(port);
+ default:
+ return false;
+ }
+}
+
+/*
+ * A cable plug only responds to the VCONN source: to its VDMs, and to its
+ * Soft_Reset. Hand those to the usual handlers, which tell SOP' apart where
+ * it matters, and drop anything else on SOP': it comes from a cable plug
+ * that breaks the rules, and no sequence has the port answer a cable plug.
+ */
+static void tcpm_pd_cable_msg_request(struct tcpm_port *port,
+ const struct pd_message *msg)
+{
+ unsigned int type = pd_header_type_le(msg->header);
+ bool ext = le16_to_cpu(msg->header) & PD_HEADER_EXT_HDR;
+ bool ctrl = !ext && !pd_header_cnt_le(msg->header);
+
+ if (!ext && !ctrl && type == PD_DATA_VENDOR_DEF) {
+ tcpm_pd_data_request(port, msg, TCPC_TX_SOP_PRIME);
+ return;
+ }
+ if (ctrl && tcpm_cable_ctrl_msg_expected(port, type)) {
+ tcpm_pd_ctrl_request(port, msg, TCPC_TX_SOP_PRIME);
+ return;
+ }
+
+ tcpm_log(port, "Dropping unexpected SOP' message, header: %#x",
+ le16_to_cpu(msg->header));
+}
+
static void tcpm_pd_rx_handler(struct kthread_work *work)
{
struct pd_rx_event *event = container_of(work,
@@ -4204,13 +4252,17 @@ static void tcpm_pd_rx_handler(struct kthread_work *work)
if (msgid == port->rx_msgid_prime)
goto done;
port->rx_msgid_prime = msgid;
- break;
+ tcpm_pd_cable_msg_request(port, msg);
+ goto done;
case TCPC_TX_SOP:
- default:
if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
goto done;
port->rx_msgid = msgid;
break;
+ default:
+ /* Neither for the port partner nor for a supported cable plug */
+ tcpm_log(port, "Dropping message on SOP* type %d", rx_sop_type);
+ goto done;
}
/*
@@ -4218,7 +4270,7 @@ static void tcpm_pd_rx_handler(struct kthread_work *work)
* mismatch.
*/
if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
- (port->data_role == TYPEC_HOST) && rx_sop_type == TCPC_TX_SOP) {
+ (port->data_role == TYPEC_HOST)) {
tcpm_log(port,
"Data role mismatch, initiating error recovery");
tcpm_set_state(port, ERROR_RECOVERY, 0);
@@ -6225,7 +6277,9 @@ static void run_state_machine(struct tcpm_port *port)
* A Soft_Reset Message Shall be targeted at a specific entity
* depending on the type of SOP* packet used.
*/
- if (port->soft_reset_sop_type == TCPC_TX_SOP_PRIME) {
+ port->soft_reset_sent_prime =
+ port->soft_reset_sop_type == TCPC_TX_SOP_PRIME;
+ if (port->soft_reset_sent_prime) {
port->soft_reset_sop_type = TCPC_TX_SOP;
port->message_id_prime = 0;
port->rx_msgid_prime = -1;
--
2.56.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 3/3] usb: typec: tcpm: Keep cable plug messages away from the port partner
2026-11-17 11:15 ` [PATCH 3/3] usb: typec: tcpm: Keep cable plug messages away from the port partner Alexey Charkov
@ 2026-11-17 11:42 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-17 11:42 UTC (permalink / raw)
To: Alexey Charkov; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] usb: typec: tcpm: SOP' VDMs in tcpm_pd_cable_msg_request()
affect partner
- [Medium] usb: typec: tcpm: SOP' PD_CTRL_REJECT in
tcpm_pd_ctrl_request() resets partner
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] usb: typec: tcpm: infinite discovery retry loop in
tcpm_pd_ctrl_request() [1]
--
commit a17153f1f38f59679f1ba169d5aec3d715a00c73
Author: Alexey Charkov <alchark@flipper.net>
usb: typec: tcpm: Keep cable plug messages away from the port partner
This commit filters incoming SOP' messages in tcpm_pd_rx_handler() through
tcpm_pd_cable_msg_request() so that only VDMs and expected control responses
from a cable plug are processed, while dropping unsupported SOP* types before
updating MessageID tracking.
> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index 947fca918414e..2a909ca96811d 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
[ ... ]
> @@ -4164,6 +4166,52 @@ static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
> }
> }
>
> +/* Whether a cable plug control message responds to what the port sent it */
> +static bool tcpm_cable_ctrl_msg_expected(struct tcpm_port *port,
> + enum pd_ctrl_msg_type type)
> +{
> + switch (type) {
> + case PD_CTRL_ACCEPT:
> + /* the Soft_Reset after a VCONN Swap */
> + return port->state == SOFT_RESET_SEND &&
> + port->soft_reset_sent_prime;
> + case PD_CTRL_REJECT:
> + case PD_CTRL_NOT_SUPP:
> + /* a VDM, in the ready state, which takes it as the answer */
> + return port->vdm_sop_type == TCPC_TX_SOP_PRIME &&
> + port->vdm_state > VDM_STATE_READY &&
> + port->state == ready_state(port);
> + default:
> + return false;
> + }
> +}
[Severity: Medium]
When a cable plug replies to a structured VDM with PD_CTRL_REJECT in the
ready state, does forwarding it to tcpm_pd_ctrl_request() still soft-reset
the port partner?
At the top of tcpm_pd_ctrl_request(), any control message other than
PD_CTRL_NOT_SUPP and PD_CTRL_GOOD_CRC aborts an active VDM AMS before the
switch statement:
drivers/usb/typec/tcpm/tcpm.c:tcpm_pd_ctrl_request() {
...
if (tcpm_vdm_ams(port) && type != PD_CTRL_NOT_SUPP && type != PD_CTRL_GOOD_CRC) {
port->vdm_state = VDM_STATE_ERR_BUSY;
tcpm_ams_finish(port);
mod_vdm_delayed_work(port, 0);
}
...
}
Because port->vdm_state is overwritten with VDM_STATE_ERR_BUSY (-3), the
subsequent port->vdm_state > VDM_STATE_READY check in the SRC_READY and
SNK_READY cases evaluates to false, falling through to the default branch
and triggering a soft reset against the port partner on SOP.
> +
> +/*
> + * A cable plug only responds to the VCONN source: to its VDMs, and to its
> + * Soft_Reset. Hand those to the usual handlers, which tell SOP' apart where
> + * it matters, and drop anything else on SOP': it comes from a cable plug
> + * that breaks the rules, and no sequence has the port answer a cable plug.
> + */
> +static void tcpm_pd_cable_msg_request(struct tcpm_port *port,
> + const struct pd_message *msg)
> +{
> + unsigned int type = pd_header_type_le(msg->header);
> + bool ext = le16_to_cpu(msg->header) & PD_HEADER_EXT_HDR;
> + bool ctrl = !ext && !pd_header_cnt_le(msg->header);
> +
> + if (!ext && !ctrl && type == PD_DATA_VENDOR_DEF) {
> + tcpm_pd_data_request(port, msg, TCPC_TX_SOP_PRIME);
> + return;
> + }
[Severity: Medium]
Can an unexpected or malformed SOP' VDM passed to tcpm_pd_data_request()
still cause the port to send messages to or reset the port partner on SOP?
Unlike tcpm_cable_ctrl_msg_expected(), this path does not check whether the
port is waiting for an SOP' VDM response. In tcpm_handle_vdm_request(), an
unstructured SOP' VDM (or an SVDM when no VDM AMS, partner altmode, or sink
VDO is active) hits the else branch and calls
tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS), which transmits
Not_Supported on SOP or initiates a soft or hard reset of the port partner:
drivers/usb/typec/tcpm/tcpm.c:tcpm_handle_vdm_request() {
...
if (PD_VDO_SVDM(p[0]) && (adev || tcpm_vdm_ams(port) || port->nr_snk_vdo)) {
port->vdm_sm_running = true;
rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action,
rx_sop_type, &response_tx_sop_type);
} else {
if (port->negotiated_rev >= PD_REV30)
tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
}
...
}
In addition, if the port is waiting for an SOP VDM response from the partner
(port->vdm_state == VDM_STATE_BUSY && port->vdm_sop_type == TCPC_TX_SOP), an
unsolicited SOP' VDM mutates port->vdm_state and port->vdo_retry for the
partner VDM state machine. Finally, an SOP' SVDM that hits a CMDT_RSP_NAK
path in tcpm_pd_svdm() leaves response_tx_sop_type at its default of
TCPC_TX_SOP and queues the VDM NAK to the port partner.
> + if (ctrl && tcpm_cable_ctrl_msg_expected(port, type)) {
> + tcpm_pd_ctrl_request(port, msg, TCPC_TX_SOP_PRIME);
> + return;
> + }
[Severity: High]
This problem wasn't introduced by this patch, but when
tcpm_pd_ctrl_request() handles PD_CTRL_NOT_SUPP or PD_CTRL_REJECT for an
in-flight VDM in SRC_READY or SNK_READY [1], does it leave
port->vdm_discovery_state unadvanced and cause an infinite discovery retry
loop?
In tcpm_pd_ctrl_request(), the SRC_READY and SNK_READY handler sets
port->vdm_state = VDM_STATE_DONE and finishes the VDM AMS without updating
port->vdm_discovery_state:
drivers/usb/typec/tcpm/tcpm.c:tcpm_pd_ctrl_request() {
...
case SRC_READY:
case SNK_READY:
if (port->vdm_state > VDM_STATE_READY) {
port->vdm_state = VDM_STATE_DONE;
if (tcpm_vdm_ams(port))
tcpm_ams_finish(port);
mod_vdm_delayed_work(port, 0);
break;
}
...
}
When a discovery command such as DISCOVER_SVIDS or DISCOVER_MODES is
answered with PD_CTRL_NOT_SUPP, port->vdm_discovery_state remains in its
intermediate phase instead of advancing, so subsequent state machine runs in
SRC_READY or SNK_READY that schedule mod_vdm_discovery_delayed_work(port, 0)
repeatedly re-send the same rejected VDM command.
[1] https://sashiko.dev/#/bug/linux-2c1789d2-5c2d-407e-af88-42956bee42c7
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-tcpm-soft-reset-sop-v1-0-c7e1543828c0@flipper.net?part=3
^ permalink raw reply [flat|nested] 7+ messages in thread