All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 0/2] usb: typec: ucsi: Role swapping fixes
@ 2022-04-05 13:48 Heikki Krogerus
  2022-04-05 13:48 ` [PATCH v1 1/2] usb: typec: ucsi: Fix reuse of completion structure Heikki Krogerus
  2022-04-05 13:48 ` [PATCH v1 2/2] usb: typec: ucsi: Fix role swapping Heikki Krogerus
  0 siblings, 2 replies; 3+ messages in thread
From: Heikki Krogerus @ 2022-04-05 13:48 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: Jia-Ju Bai, Jack Pham, linux-usb

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(-)

-- 
2.35.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v1 1/2] usb: typec: ucsi: Fix reuse of completion structure
  2022-04-05 13:48 [PATCH v1 0/2] usb: typec: ucsi: Role swapping fixes Heikki Krogerus
@ 2022-04-05 13:48 ` Heikki Krogerus
  2022-04-05 13:48 ` [PATCH v1 2/2] usb: typec: ucsi: Fix role swapping Heikki Krogerus
  1 sibling, 0 replies; 3+ messages in thread
From: Heikki Krogerus @ 2022-04-05 13:48 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: Jia-Ju Bai, Jack Pham, linux-usb, stable

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.

Link: https://lore.kernel.org/linux-usb/20220325203959.GA19752@jackp-linux.qualcomm.com/
Fixes: 6df475f804e6 ("usb: typec: ucsi: Start using struct typec_operations")
Reported-and-suggested-by: Jack Pham <quic_jackp@quicinc.com>
Cc: stable@vger.kernel.org
Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/ucsi/ucsi.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index f0c2fa19f3e0f..576cb0e68596f 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -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;
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v1 2/2] usb: typec: ucsi: Fix role swapping
  2022-04-05 13:48 [PATCH v1 0/2] usb: typec: ucsi: Role swapping fixes Heikki Krogerus
  2022-04-05 13:48 ` [PATCH v1 1/2] usb: typec: ucsi: Fix reuse of completion structure Heikki Krogerus
@ 2022-04-05 13:48 ` Heikki Krogerus
  1 sibling, 0 replies; 3+ messages in thread
From: Heikki Krogerus @ 2022-04-05 13:48 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: Jia-Ju Bai, Jack Pham, linux-usb, stable

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.

Link: https://lore.kernel.org/linux-usb/037de7ac-e210-bdf5-ec7a-8c0c88a0be20@gmail.com/
Fixes: ad74b8649bea ("usb: typec: ucsi: Preliminary support for alternate modes")
Reported-and-tested-by: Jia-Ju Bai <baijiaju1990@gmail.com>
Cc: stable@vger.kernel.org
Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/ucsi/ucsi.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index 576cb0e68596f..a6045aef0d04f 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -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) !=
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-04-05 23:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-05 13:48 [PATCH v1 0/2] usb: typec: ucsi: Role swapping fixes Heikki Krogerus
2022-04-05 13:48 ` [PATCH v1 1/2] usb: typec: ucsi: Fix reuse of completion structure Heikki Krogerus
2022-04-05 13:48 ` [PATCH v1 2/2] usb: typec: ucsi: Fix role swapping Heikki Krogerus

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.