Skip to contentSkip to conversation

usb: typec: ucsi: Role swapping fixes

PATCH New Heikki Krogerus 1 participant 3 messages Apr 5, 2022

Subsystems (3)
lore
Share & tools
lore

Heikki Krogerus

author maintainer
#0 lore raw

cover usb: typec: ucsi: Role swapping fixes

Hi,

It appears these role swapping routines have been broken for a while. The role swap UCSI commands always actually did work, and the roles were most likely always swapped, but these operations just always appeared to fail because of these bugs.

I'm guessing that has kept the problem hidden. Nobody cared about the return values from these operations. It was enough that the roles were swapped in the end.

thanks,

Heikki Krogerus (2):
usb: typec: ucsi: Fix reuse of completion structure
usb: typec: ucsi: Fix role swapping

 drivers/usb/typec/ucsi/ucsi.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
Signature · 1 line
2.35.1
author maintainer
#1 lore raw

PATCH 1/2 usb: typec: ucsi: Fix reuse of completion structure

The role swapping completion variable is reused, so it needs to be reinitialised every time. Otherwise it will be marked as done after the first time it's used and completing immediately.

Fixes 6df475f804e6 ("usb: typec: ucsi: Start using struct typec_operations")
Reported-and-suggested-by Jack Pham <[email protected]>
---
 drivers/usb/typec/ucsi/ucsi.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/typec/ucsi/ucsi.c +4 -0
index f0c2fa19f3e0f..576cb0e68596f 100644
@@ -949,6 +949,8 @@ static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
 	     role == TYPEC_HOST))
 		goto out_unlock;
 
+	reinit_completion(&con->complete);
+
 	command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
 	command |= UCSI_SET_UOR_ROLE(role);
 	command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
@@ -985,6 +987,8 @@ static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
 	if (cur_role == role)
 		goto out_unlock;
 
+	reinit_completion(&con->complete);
+
 	command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
 	command |= UCSI_SET_PDR_ROLE(role);
 	command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
Signature · 1 line
2.35.1
author maintainer
#2 lore raw

PATCH 2/2 usb: typec: ucsi: Fix role swapping

In reply to Heikki Krogerus

All attempts to swap the roles timed out because the completion was done without releasing the port lock. Fixing that by releasing the lock before starting to wait for the completion.

Fixes ad74b8649bea ("usb: typec: ucsi: Preliminary support for alternate modes")
Reported-and-tested-by Jia-Ju Bai <[email protected]>
---
 drivers/usb/typec/ucsi/ucsi.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/usb/typec/ucsi/ucsi.c +13 -7
index 576cb0e68596f..a6045aef0d04f 100644
@@ -958,14 +958,18 @@ static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
 	if (ret < 0)
 		goto out_unlock;
 
+	mutex_unlock(&con->lock);
+
 	if (!wait_for_completion_timeout(&con->complete,
-					msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
-		ret = -ETIMEDOUT;
+					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
+		return -ETIMEDOUT;
+
+	return 0;
 
 out_unlock:
 	mutex_unlock(&con->lock);
 
-	return ret < 0 ? ret : 0;
+	return ret;
 }
 
 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
@@ -996,11 +1000,13 @@ static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
 	if (ret < 0)
 		goto out_unlock;
 
+	mutex_unlock(&con->lock);
+
 	if (!wait_for_completion_timeout(&con->complete,
-				msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) {
-		ret = -ETIMEDOUT;
-		goto out_unlock;
-	}
+					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
+		return -ETIMEDOUT;
+
+	mutex_lock(&con->lock);
 
 	/* Something has gone wrong while swapping the role */
 	if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
Signature · 1 line
2.35.1