From 5eea5323a7709b2b78624d77b61497c693676cce Mon Sep 17 00:00:00 2001 From: Praveen Kumar Thuppa Date: Thu, 3 Dec 2026 00:13:10 +0530 Subject: [PATCH] FROMLIST: usb: typec: ucsi: Do not override an active alternate mode with USB mode 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. Link: https://lore.kernel.org/all/20261203-sm8750-dp-issue-v1-1-a519848958ad@oss.qualcomm.com/ Signed-off-by: Praveen Kumar Thuppa Signed-off-by: Krishna Kurapati --- 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 54c7810b563be..1ced394cd44e8 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 afa6fc1813978..7f0ac09a07aab 100644 --- a/drivers/usb/typec/mux.c +++ b/drivers/usb/typec/mux.c @@ -13,6 +13,7 @@ #include #include #include +#include #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 58f0f28b6dc84..ed9b2435d3818 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 +#include #include 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 c1450639c7956..50a1857cd3fdc 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 10a783b738efd..02a7a325f8688 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 aa9ebb7e2fe0b..05b066c0f8882 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) {