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) {