[PATCH v4 7/8] usb: typec: ucsi: Support mode selection to activate altmodes

Andrei Kuchynski posted 8 patches 3 weeks, 6 days ago
There is a newer version of this series
[PATCH v4 7/8] usb: typec: ucsi: Support mode selection to activate altmodes
Posted by Andrei Kuchynski 3 weeks, 6 days ago
If the ucsi port driver supports modes selection, it should implement
`add_partner_altmodes` and `remove_partner_altmodes` ucsi operations. With
these operations the driver can manage the mode selection process.
Once partner altmodes are registered, `add_partner_altmodes` is called to
start the mode selection. When the partner is unregistered,
`remove_partner_altmodes` is supposed to stop any ongoing processes and
clean up the resources.

`typec_altmode_state_update` informes mode selection about the current mode
of the Type-C connector.

Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
---
 drivers/usb/typec/ucsi/ucsi.c | 11 +++++++++++
 drivers/usb/typec/ucsi/ucsi.h |  4 ++++
 2 files changed, 15 insertions(+)

diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index deb210c066cb5..4a6e23b55b10c 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -314,6 +314,7 @@ void ucsi_altmode_update_active(struct ucsi_connector *con)
 {
 	const struct typec_altmode *altmode = NULL;
 	u64 command;
+	u16 svid = 0;
 	int ret;
 	u8 cur;
 	int i;
@@ -335,6 +336,10 @@ void ucsi_altmode_update_active(struct ucsi_connector *con)
 	for (i = 0; con->partner_altmode[i]; i++)
 		typec_altmode_update_active(con->partner_altmode[i],
 					    con->partner_altmode[i] == altmode);
+
+	if (altmode)
+		svid = altmode->svid;
+	typec_altmode_state_update(con->partner, svid, 0);
 }
 
 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
@@ -609,6 +614,8 @@ static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
 			desc.vdo = alt[j].mid;
 			desc.svid = alt[j].svid;
 			desc.roles = TYPEC_PORT_DRD;
+			desc.mode_selection = con->ucsi->ops->add_partner_altmodes &&
+					con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE;
 
 			ret = ucsi_register_altmode(con, &desc, recipient);
 			if (ret)
@@ -831,6 +838,8 @@ static int ucsi_check_altmodes(struct ucsi_connector *con)
 	if (con->partner_altmode[0]) {
 		num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
 		typec_partner_set_num_altmodes(con->partner, num_partner_am);
+		if (con->ucsi->ops->add_partner_altmodes)
+			con->ucsi->ops->add_partner_altmodes(con);
 		ucsi_altmode_update_active(con);
 		return 0;
 	} else {
@@ -1119,6 +1128,8 @@ static void ucsi_unregister_partner(struct ucsi_connector *con)
 		return;
 
 	typec_set_mode(con->port, TYPEC_STATE_SAFE);
+	if (con->ucsi->ops->remove_partner_altmodes)
+		con->ucsi->ops->remove_partner_altmodes(con);
 
 	typec_partner_set_usb_power_delivery(con->partner, NULL);
 	ucsi_unregister_partner_pdos(con);
diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h
index 410389ef173ab..4797b4aa1e35b 100644
--- a/drivers/usb/typec/ucsi/ucsi.h
+++ b/drivers/usb/typec/ucsi/ucsi.h
@@ -70,6 +70,8 @@ struct dentry;
  * @update_altmodes: Squashes duplicate DP altmodes
  * @update_connector: Update connector capabilities before registering
  * @connector_status: Updates connector status, called holding connector lock
+ * @add_partner_altmodes: Start mode selection
+ * @remove_partner_altmodes: Clean mode selection
  *
  * Read and write routines for UCSI interface. @sync_write must wait for the
  * Command Completion Event from the PPM before returning, and @async_write must
@@ -88,6 +90,8 @@ struct ucsi_operations {
 				struct ucsi_altmode *updated);
 	void (*update_connector)(struct ucsi_connector *con);
 	void (*connector_status)(struct ucsi_connector *con);
+	void (*add_partner_altmodes)(struct ucsi_connector *con);
+	void (*remove_partner_altmodes)(struct ucsi_connector *con);
 };
 
 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops);
-- 
2.52.0.457.g6b5491de43-goog
Re: [PATCH v4 7/8] usb: typec: ucsi: Support mode selection to activate altmodes
Posted by Heikki Krogerus 3 weeks, 4 days ago
Tue, Jan 13, 2026 at 01:05:35PM +0000, Andrei Kuchynski kirjoitti:
> If the ucsi port driver supports modes selection, it should implement
> `add_partner_altmodes` and `remove_partner_altmodes` ucsi operations. With
> these operations the driver can manage the mode selection process.
> Once partner altmodes are registered, `add_partner_altmodes` is called to
> start the mode selection. When the partner is unregistered,
> `remove_partner_altmodes` is supposed to stop any ongoing processes and
> clean up the resources.
> 
> `typec_altmode_state_update` informes mode selection about the current mode
> of the Type-C connector.
> 
> Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> ---
>  drivers/usb/typec/ucsi/ucsi.c | 11 +++++++++++
>  drivers/usb/typec/ucsi/ucsi.h |  4 ++++
>  2 files changed, 15 insertions(+)
> 
> diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
> index deb210c066cb5..4a6e23b55b10c 100644
> --- a/drivers/usb/typec/ucsi/ucsi.c
> +++ b/drivers/usb/typec/ucsi/ucsi.c
> @@ -314,6 +314,7 @@ void ucsi_altmode_update_active(struct ucsi_connector *con)
>  {
>  	const struct typec_altmode *altmode = NULL;
>  	u64 command;
> +	u16 svid = 0;
>  	int ret;
>  	u8 cur;
>  	int i;
> @@ -335,6 +336,10 @@ void ucsi_altmode_update_active(struct ucsi_connector *con)
>  	for (i = 0; con->partner_altmode[i]; i++)
>  		typec_altmode_update_active(con->partner_altmode[i],
>  					    con->partner_altmode[i] == altmode);
> +
> +	if (altmode)
> +		svid = altmode->svid;
> +	typec_altmode_state_update(con->partner, svid, 0);
>  }
>  
>  static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
> @@ -609,6 +614,8 @@ static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
>  			desc.vdo = alt[j].mid;
>  			desc.svid = alt[j].svid;
>  			desc.roles = TYPEC_PORT_DRD;
> +			desc.mode_selection = con->ucsi->ops->add_partner_altmodes &&
> +					con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE;

Can't you just use that con->typec_cap.no_mode_control flag here?

Maybe also consider squashing that patch 3/8 into this one while at it.

>  			ret = ucsi_register_altmode(con, &desc, recipient);
>  			if (ret)
> @@ -831,6 +838,8 @@ static int ucsi_check_altmodes(struct ucsi_connector *con)
>  	if (con->partner_altmode[0]) {
>  		num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
>  		typec_partner_set_num_altmodes(con->partner, num_partner_am);
> +		if (con->ucsi->ops->add_partner_altmodes)
> +			con->ucsi->ops->add_partner_altmodes(con);
>  		ucsi_altmode_update_active(con);
>  		return 0;
>  	} else {
> @@ -1119,6 +1128,8 @@ static void ucsi_unregister_partner(struct ucsi_connector *con)
>  		return;
>  
>  	typec_set_mode(con->port, TYPEC_STATE_SAFE);
> +	if (con->ucsi->ops->remove_partner_altmodes)
> +		con->ucsi->ops->remove_partner_altmodes(con);
>  
>  	typec_partner_set_usb_power_delivery(con->partner, NULL);
>  	ucsi_unregister_partner_pdos(con);

thanks,

-- 
heikki
Re: [PATCH v4 7/8] usb: typec: ucsi: Support mode selection to activate altmodes
Posted by Andrei Kuchynski 3 weeks, 1 day ago
On Thu, Jan 15, 2026 at 3:59 PM Heikki Krogerus
<heikki.krogerus@linux.intel.com> wrote:
>
> Tue, Jan 13, 2026 at 01:05:35PM +0000, Andrei Kuchynski kirjoitti:
> > If the ucsi port driver supports modes selection, it should implement
> > `add_partner_altmodes` and `remove_partner_altmodes` ucsi operations. With
> > these operations the driver can manage the mode selection process.
> > Once partner altmodes are registered, `add_partner_altmodes` is called to
> > start the mode selection. When the partner is unregistered,
> > `remove_partner_altmodes` is supposed to stop any ongoing processes and
> > clean up the resources.
> >
> > `typec_altmode_state_update` informes mode selection about the current mode
> > of the Type-C connector.
> >
> > Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> > ---
> >  drivers/usb/typec/ucsi/ucsi.c | 11 +++++++++++
> >  drivers/usb/typec/ucsi/ucsi.h |  4 ++++
> >  2 files changed, 15 insertions(+)
> >
> > diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
> > index deb210c066cb5..4a6e23b55b10c 100644
> > --- a/drivers/usb/typec/ucsi/ucsi.c
> > +++ b/drivers/usb/typec/ucsi/ucsi.c
> > @@ -314,6 +314,7 @@ void ucsi_altmode_update_active(struct ucsi_connector *con)
> >  {
> >       const struct typec_altmode *altmode = NULL;
> >       u64 command;
> > +     u16 svid = 0;
> >       int ret;
> >       u8 cur;
> >       int i;
> > @@ -335,6 +336,10 @@ void ucsi_altmode_update_active(struct ucsi_connector *con)
> >       for (i = 0; con->partner_altmode[i]; i++)
> >               typec_altmode_update_active(con->partner_altmode[i],
> >                                           con->partner_altmode[i] == altmode);
> > +
> > +     if (altmode)
> > +             svid = altmode->svid;
> > +     typec_altmode_state_update(con->partner, svid, 0);
> >  }
> >
> >  static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
> > @@ -609,6 +614,8 @@ static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
> >                       desc.vdo = alt[j].mid;
> >                       desc.svid = alt[j].svid;
> >                       desc.roles = TYPEC_PORT_DRD;
> > +                     desc.mode_selection = con->ucsi->ops->add_partner_altmodes &&
> > +                                     con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE;
>
> Can't you just use that con->typec_cap.no_mode_control flag here?
>

Thanks. It looks much better.

> Maybe also consider squashing that patch 3/8 into this one while at it.
>

I agree. Patch 3/8 was part of a mode priority series. Now there's no
point in leaving a single line of code in a separate patch.

Thank you for your review of the series!
I will send the subsequent series with all the changes.

Andrei