usb: typec: ucsi: Role swapping fixes
Subsystems (3)
Heikki Krogerus
maintainercover 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
Heikki Krogerus
maintainerPATCH 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.
--- drivers/usb/typec/ucsi/ucsi.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/usb/typec/ucsi/ucsi.c +4 -0
@@ -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
Heikki Krogerus
maintainerPATCH 2/2 usb: typec: ucsi: Fix role swapping
In reply to Heikki KrogerusAll 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.
--- drivers/usb/typec/ucsi/ucsi.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/usb/typec/ucsi/ucsi.c +13 -7
@@ -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