[PATCH v5 7/7] usb: typec: ucsi: Enforce mode selection for cros_ec_ucsi

Andrei Kuchynski posted 7 patches 3 weeks ago
[PATCH v5 7/7] usb: typec: ucsi: Enforce mode selection for cros_ec_ucsi
Posted by Andrei Kuchynski 3 weeks ago
The mode selection sequence is initiated by the driver after all partner
alternate modes have been successfully registered.
When a partner is disconnected, the driver also stops the mode selection
process and releases resources via `typec_mode_selection_delete`.

Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
---
Changes in V5:
- Use the no_mode_control field instead of
  con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE

 drivers/usb/typec/ucsi/cros_ec_ucsi.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/drivers/usb/typec/ucsi/cros_ec_ucsi.c b/drivers/usb/typec/ucsi/cros_ec_ucsi.c
index eed2a7d0ebc63..6bca2dce211cd 100644
--- a/drivers/usb/typec/ucsi/cros_ec_ucsi.c
+++ b/drivers/usb/typec/ucsi/cros_ec_ucsi.c
@@ -16,6 +16,7 @@
 #include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/wait.h>
+#include <linux/usb/typec_altmode.h>
 
 #include "ucsi.h"
 
@@ -33,6 +34,11 @@
 /* Number of times to attempt recovery from a write timeout before giving up. */
 #define WRITE_TMO_CTR_MAX	5
 
+/* Delay between mode entry/exit attempts, ms */
+static const unsigned int mode_selection_delay = 1000;
+/* Timeout for a mode entry attempt, ms */
+static const unsigned int mode_selection_timeout = 4000;
+
 struct cros_ucsi_data {
 	struct device *dev;
 	struct ucsi *ucsi;
@@ -134,6 +140,20 @@ static int cros_ucsi_sync_control(struct ucsi *ucsi, u64 cmd, u32 *cci,
 	return ret;
 }
 
+static void cros_ucsi_add_partner_altmodes(struct ucsi_connector *con)
+{
+	if (!con->typec_cap.no_mode_control)
+		typec_mode_selection_start(con->partner,
+					   mode_selection_delay,
+					   mode_selection_timeout);
+}
+
+static void cros_ucsi_remove_partner_altmodes(struct ucsi_connector *con)
+{
+	if (!con->typec_cap.no_mode_control)
+		typec_mode_selection_delete(con->partner);
+}
+
 static const struct ucsi_operations cros_ucsi_ops = {
 	.read_version = cros_ucsi_read_version,
 	.read_cci = cros_ucsi_read_cci,
@@ -141,6 +161,8 @@ static const struct ucsi_operations cros_ucsi_ops = {
 	.read_message_in = cros_ucsi_read_message_in,
 	.async_control = cros_ucsi_async_control,
 	.sync_control = cros_ucsi_sync_control,
+	.add_partner_altmodes = cros_ucsi_add_partner_altmodes,
+	.remove_partner_altmodes = cros_ucsi_remove_partner_altmodes,
 };
 
 static void cros_ucsi_work(struct work_struct *work)
-- 
2.52.0.457.g6b5491de43-goog
Re: [PATCH v5 7/7] usb: typec: ucsi: Enforce mode selection for cros_ec_ucsi
Posted by Heikki Krogerus 2 weeks, 5 days ago
Mon, Jan 19, 2026 at 01:18:24PM +0000, Andrei Kuchynski kirjoitti:
> The mode selection sequence is initiated by the driver after all partner
> alternate modes have been successfully registered.
> When a partner is disconnected, the driver also stops the mode selection
> process and releases resources via `typec_mode_selection_delete`.
> 
> Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>

Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

> ---
> Changes in V5:
> - Use the no_mode_control field instead of
>   con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE
> 
>  drivers/usb/typec/ucsi/cros_ec_ucsi.c | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/drivers/usb/typec/ucsi/cros_ec_ucsi.c b/drivers/usb/typec/ucsi/cros_ec_ucsi.c
> index eed2a7d0ebc63..6bca2dce211cd 100644
> --- a/drivers/usb/typec/ucsi/cros_ec_ucsi.c
> +++ b/drivers/usb/typec/ucsi/cros_ec_ucsi.c
> @@ -16,6 +16,7 @@
>  #include <linux/platform_device.h>
>  #include <linux/slab.h>
>  #include <linux/wait.h>
> +#include <linux/usb/typec_altmode.h>
>  
>  #include "ucsi.h"
>  
> @@ -33,6 +34,11 @@
>  /* Number of times to attempt recovery from a write timeout before giving up. */
>  #define WRITE_TMO_CTR_MAX	5
>  
> +/* Delay between mode entry/exit attempts, ms */
> +static const unsigned int mode_selection_delay = 1000;
> +/* Timeout for a mode entry attempt, ms */
> +static const unsigned int mode_selection_timeout = 4000;
> +
>  struct cros_ucsi_data {
>  	struct device *dev;
>  	struct ucsi *ucsi;
> @@ -134,6 +140,20 @@ static int cros_ucsi_sync_control(struct ucsi *ucsi, u64 cmd, u32 *cci,
>  	return ret;
>  }
>  
> +static void cros_ucsi_add_partner_altmodes(struct ucsi_connector *con)
> +{
> +	if (!con->typec_cap.no_mode_control)
> +		typec_mode_selection_start(con->partner,
> +					   mode_selection_delay,
> +					   mode_selection_timeout);
> +}
> +
> +static void cros_ucsi_remove_partner_altmodes(struct ucsi_connector *con)
> +{
> +	if (!con->typec_cap.no_mode_control)
> +		typec_mode_selection_delete(con->partner);
> +}
> +
>  static const struct ucsi_operations cros_ucsi_ops = {
>  	.read_version = cros_ucsi_read_version,
>  	.read_cci = cros_ucsi_read_cci,
> @@ -141,6 +161,8 @@ static const struct ucsi_operations cros_ucsi_ops = {
>  	.read_message_in = cros_ucsi_read_message_in,
>  	.async_control = cros_ucsi_async_control,
>  	.sync_control = cros_ucsi_sync_control,
> +	.add_partner_altmodes = cros_ucsi_add_partner_altmodes,
> +	.remove_partner_altmodes = cros_ucsi_remove_partner_altmodes,
>  };
>  
>  static void cros_ucsi_work(struct work_struct *work)
> -- 
> 2.52.0.457.g6b5491de43-goog

-- 
heikki