* [PATCH] usb: typec: ucsi: Do not override an active alternate mode with USB mode
@ 2026-12-02 18:43 Krishna Kurapati
2026-12-02 18:51 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Krishna Kurapati @ 2026-12-02 18:43 UTC (permalink / raw)
To: Heikki Krogerus, Greg Kroah-Hartman
Cc: linux-usb, Praveen Kumar Thuppa, Krishna Kurapati
From: Praveen Kumar Thuppa <pthuppa@qti.qualcomm.com>
ucsi_partner_change() switches the port to USB mode whenever the
connector status reports a USB partner. An alternate mode can be entered
through a path other than the UCSI alternate mode negotiation, for example
pmic glink altmodedriver that programs muxes from firmware events. UCSI
does not know about such an alternate mode, so a partner change handled
after it is up takes the muxes back to USB.
On SM8750, when the Qualcomm PMIC GLINK altmode driver enters DisplayPort,
the USB mode update from ucsi switches the wcd939x-usbss mux, which carries
the DisplayPort AUX lines, away from DisplayPort shortly after the hotplug
handling has read the sink capabilities and EDID successfully. The next AUX
read times out with -110 and link training never runs.
The check cannot be done in UCSI or in the driver that entered the altmode,
as neither knows what the other did. It is done in the mux layer, where the
state is applied. Record in struct typec_mux_dev whether the last state
accepted by the set callback was an alternate mode, and add
mux_set_unless_altmode and set_mode_unless_altmode, which behave like
typec_mux_set and typec_set_mode but leave mux devices that are in an alt
mode untouched and report success for them. Use the latter for the USB
mode update in ucsi_partner_change(). This also applies to alt modes that
UCSI entered itself: a later USB mode update from a partner change no
longer takes them back to USB. Any other accepted state clears the record.
Hold a per mux device mutex around the set() callback and the update of the
record, so that the check cannot interleave with an alternate mode entry
coming from another path. Give every mutex its own lockdep class, muxes
such as wcd939x-usbss forward a state to a downstream mux from their set
callback and must be able to nest.
Ports without an active alternate mode get the same states and error
handling as before; the set() calls are now serialized per mux
device.
Signed-off-by: Praveen Kumar Thuppa <pthuppa@qti.qualcomm.com>
Signed-off-by: Krishna Kurapati <krishna.kurapati@oss.qualcomm.com>
---
drivers/usb/typec/class.c | 22 ++++++++++++
drivers/usb/typec/mux.c | 78 +++++++++++++++++++++++++++++++++++++++----
drivers/usb/typec/mux.h | 5 +++
drivers/usb/typec/ucsi/ucsi.c | 3 +-
include/linux/usb/typec.h | 1 +
include/linux/usb/typec_mux.h | 8 +++++
6 files changed, 109 insertions(+), 8 deletions(-)
diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 34ced6d1c224..38c16518f674 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -2506,6 +2506,28 @@ int typec_set_mode(struct typec_port *port, int mode)
}
EXPORT_SYMBOL_GPL(typec_set_mode);
+/**
+ * typec_set_mode_unless_altmode - Set mode of operation, keeping alternate modes
+ * @port: USB Type-C connector
+ * @mode: Accessory Mode, USB Operation or Safe State
+ *
+ * Like typec_set_mode(), but muxes that are currently in an alternate mode are
+ * left untouched, no matter which driver entered that alternate mode. For
+ * generic mode updates that must not override an alternate mode.
+ *
+ * Context: May sleep.
+ * Return: 0 on success, or a negative error code from the multiplexer.
+ */
+int typec_set_mode_unless_altmode(struct typec_port *port, int mode)
+{
+ struct typec_mux_state state = { };
+
+ state.mode = mode;
+
+ return typec_mux_set_unless_altmode(port->mux, &state);
+}
+EXPORT_SYMBOL_GPL(typec_set_mode_unless_altmode);
+
/* --------------------------------------- */
/**
diff --git a/drivers/usb/typec/mux.c b/drivers/usb/typec/mux.c
index 57149f537692..f520bd2861ee 100644
--- a/drivers/usb/typec/mux.c
+++ b/drivers/usb/typec/mux.c
@@ -13,6 +13,7 @@
#include <linux/mutex.h>
#include <linux/property.h>
#include <linux/slab.h>
+#include <linux/usb/typec_altmode.h>
#include "class.h"
#include "mux.h"
@@ -390,9 +391,42 @@ void typec_mux_put(struct typec_mux *mux)
}
EXPORT_SYMBOL_GPL(typec_mux_put);
-int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
+static int typec_mux_dev_set(struct typec_mux_dev *mux_dev,
+ struct typec_mux_state *state, bool keep_altmode)
+{
+ bool altmode;
+ int ret;
+
+ guard(mutex)(&mux_dev->lock);
+
+ if (keep_altmode && mux_dev->altmode_active) {
+ dev_dbg(&mux_dev->dev, "alternate mode active, ignoring mode %lu\n",
+ state->mode);
+ return 0;
+ }
+
+ ret = mux_dev->set(mux_dev, state);
+ /* A state the mux does not support leaves the mux and the record as is */
+ if (ret == -EOPNOTSUPP)
+ return 0;
+
+ /*
+ * After a failed set() the state of the mux is unknown: drop the record
+ * instead of keeping later generic mode updates out indefinitely.
+ */
+ altmode = !ret && state->alt && state->mode >= TYPEC_STATE_MODAL;
+ if (altmode != mux_dev->altmode_active) {
+ dev_dbg(&mux_dev->dev, "alternate mode %s\n",
+ altmode ? "entered" : "left");
+ mux_dev->altmode_active = altmode;
+ }
+
+ return ret;
+}
+
+static int typec_mux_set_devs(struct typec_mux *mux,
+ struct typec_mux_state *state, bool keep_altmode)
{
- struct typec_mux_dev *mux_dev;
unsigned int i;
int ret;
@@ -400,20 +434,48 @@ int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
return 0;
for (i = 0; i < mux->num_mux_devs; i++) {
- mux_dev = mux->mux_devs[i];
-
- ret = mux_dev->set(mux_dev, state);
- if (ret && ret != -EOPNOTSUPP)
+ ret = typec_mux_dev_set(mux->mux_devs[i], state, keep_altmode);
+ if (ret)
return ret;
}
return 0;
}
+
+int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state)
+{
+ return typec_mux_set_devs(mux, state, false);
+}
EXPORT_SYMBOL_GPL(typec_mux_set);
+/**
+ * typec_mux_set_unless_altmode - Set a multiplexer state, keeping alternate modes
+ * @mux: USB Type-C Connector Multiplexer/DeMultiplexer
+ * @state: Multiplexer state
+ *
+ * Like typec_mux_set(), but a multiplexer device that is currently in an
+ * alternate mode is left untouched and counts as success. The check and the
+ * update are done under the device's lock, so a concurrent alternate mode
+ * entry is never overridden, whichever driver performs it.
+ *
+ * Context: May sleep. Takes the multiplexer device lock and calls the set()
+ * callback of the multiplexer.
+ * Return: 0 on success, or a negative error code from the multiplexer.
+ */
+int typec_mux_set_unless_altmode(struct typec_mux *mux,
+ struct typec_mux_state *state)
+{
+ return typec_mux_set_devs(mux, state, true);
+}
+EXPORT_SYMBOL_GPL(typec_mux_set_unless_altmode);
+
static void typec_mux_release(struct device *dev)
{
- kfree(to_typec_mux_dev(dev));
+ struct typec_mux_dev *mux_dev = to_typec_mux_dev(dev);
+
+ mutex_destroy(&mux_dev->lock);
+ lockdep_unregister_key(&mux_dev->lock_key);
+ kfree(mux_dev);
}
const struct device_type typec_mux_dev_type = {
@@ -445,6 +507,8 @@ typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
return ERR_PTR(-ENOMEM);
mux_dev->set = desc->set;
+ lockdep_register_key(&mux_dev->lock_key);
+ __mutex_init(&mux_dev->lock, "typec_mux_dev->lock", &mux_dev->lock_key);
device_initialize(&mux_dev->dev);
mux_dev->dev.parent = parent;
diff --git a/drivers/usb/typec/mux.h b/drivers/usb/typec/mux.h
index 58f0f28b6dc8..ed9b2435d381 100644
--- a/drivers/usb/typec/mux.h
+++ b/drivers/usb/typec/mux.h
@@ -3,6 +3,8 @@
#ifndef __USB_TYPEC_MUX__
#define __USB_TYPEC_MUX__
+#include <linux/lockdep.h>
+#include <linux/mutex.h>
#include <linux/usb/typec_mux.h>
struct typec_switch_dev {
@@ -13,6 +15,9 @@ struct typec_switch_dev {
struct typec_mux_dev {
struct device dev;
typec_mux_set_fn_t set;
+ struct mutex lock;
+ struct lock_class_key lock_key;
+ bool altmode_active;
};
#define to_typec_switch_dev(_dev_) container_of(_dev_, struct typec_switch_dev, dev)
diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index 9e453d216c16..8709c69d1452 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -1357,7 +1357,8 @@ static void ucsi_partner_change(struct ucsi_connector *con)
break;
default:
if (UCSI_CONSTAT(con, PARTNER_FLAG_USB))
- typec_set_mode(con->port, TYPEC_STATE_USB);
+ typec_set_mode_unless_altmode(con->port,
+ TYPEC_STATE_USB);
}
if (((con->ucsi->version >= UCSI_VERSION_3_0 &&
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 10a783b738ef..02a7a325f868 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -353,6 +353,7 @@ int typec_set_orientation(struct typec_port *port,
enum typec_orientation orientation);
enum typec_orientation typec_get_orientation(struct typec_port *port);
int typec_set_mode(struct typec_port *port, int mode);
+int typec_set_mode_unless_altmode(struct typec_port *port, int mode);
void *typec_get_drvdata(struct typec_port *port);
diff --git a/include/linux/usb/typec_mux.h b/include/linux/usb/typec_mux.h
index aa9ebb7e2fe0..05b066c0f888 100644
--- a/include/linux/usb/typec_mux.h
+++ b/include/linux/usb/typec_mux.h
@@ -99,6 +99,8 @@ struct typec_mux_desc {
struct typec_mux *fwnode_typec_mux_get(struct fwnode_handle *fwnode);
void typec_mux_put(struct typec_mux *mux);
int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *state);
+int typec_mux_set_unless_altmode(struct typec_mux *mux,
+ struct typec_mux_state *state);
struct typec_mux_dev *
typec_mux_register(struct device *parent, const struct typec_mux_desc *desc);
@@ -121,6 +123,12 @@ static inline int typec_mux_set(struct typec_mux *mux, struct typec_mux_state *s
return 0;
}
+static inline int typec_mux_set_unless_altmode(struct typec_mux *mux,
+ struct typec_mux_state *state)
+{
+ return 0;
+}
+
static inline struct typec_mux_dev *
typec_mux_register(struct device *parent, const struct typec_mux_desc *desc)
{
---
base-commit: 9357829ac604c28510b71b154cb6630af81226ff
change-id: 20261202-sm8750-dp-issue-82f6b26eb9bd
Best regards,
--
Krishna Kurapati <krishna.kurapati@oss.qualcomm.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] usb: typec: ucsi: Do not override an active alternate mode with USB mode
2026-12-02 18:43 [PATCH] usb: typec: ucsi: Do not override an active alternate mode with USB mode Krishna Kurapati
@ 2026-12-02 18:51 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 18:51 UTC (permalink / raw)
To: Krishna Kurapati; +Cc: linux-usb
> From: Praveen Kumar Thuppa <pthuppa@qti.qualcomm.com>
>
> ucsi_partner_change() switches the port to USB mode whenever the
> connector status reports a USB partner. An alternate mode can be entered
> through a path other than the UCSI alternate mode negotiation, for example
> pmic glink altmodedriver that programs muxes from firmware events. UCSI
> does not know about such an alternate mode, so a partner change handled
> after it is up takes the muxes back to USB.
>
> On SM8750, when the Qualcomm PMIC GLINK altmode driver enters DisplayPort,
> the USB mode update from ucsi switches the wcd939x-usbss mux, which carries
> the DisplayPort AUX lines, away from DisplayPort shortly after the hotplug
> handling has read the sink capabilities and EDID successfully. The next AUX
> read times out with -110 and link training never runs.
>
> [ ... ]
> handling as before; the set() calls are now serialized per mux
> device.
>
> Signed-off-by: Praveen Kumar Thuppa <pthuppa@qti.qualcomm.com>
> Signed-off-by: Krishna Kurapati <krishna.kurapati@oss.qualcomm.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-sm8750-dp-issue-v1-1-a519848958ad@oss.qualcomm.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 18:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 18:43 [PATCH] usb: typec: ucsi: Do not override an active alternate mode with USB mode Krishna Kurapati
2026-12-02 18:51 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox