Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 673dc4b7 authored by Xu Yang's avatar Xu Yang Committed by Greg Kroah-Hartman
Browse files

usb: roles: don't get/set_role() when usb_role_switch is unregistered



commit b787a3e781759026a6212736ef8e52cf83d1821a upstream.

There is a possibility that usb_role_switch device is unregistered before
the user put usb_role_switch. In this case, the user may still want to
get/set_role() since the user can't sense the changes of usb_role_switch.

This will add a flag to show if usb_role_switch is already registered and
avoid unwanted behaviors.

Fixes: fde0aa6c ("usb: common: Small class for USB role switches")
cc: stable@vger.kernel.org
Signed-off-by: default avatarXu Yang <xu.yang_2@nxp.com>
Acked-by: default avatarHeikki Krogerus <heikki.krogerus@linux.intel.com>
Link: https://lore.kernel.org/r/20240129093739.2371530-2-xu.yang_2@nxp.com


Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 059285e0
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ struct usb_role_switch {
	struct device dev;
	struct mutex lock; /* device lock*/
	enum usb_role role;
	bool registered;

	/* From descriptor */
	struct device *usb2_port;
@@ -45,6 +46,9 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
	if (IS_ERR_OR_NULL(sw))
		return 0;

	if (!sw->registered)
		return -EOPNOTSUPP;

	mutex_lock(&sw->lock);

	ret = sw->set(sw->dev.parent, role);
@@ -68,7 +72,7 @@ enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
{
	enum usb_role role;

	if (IS_ERR_OR_NULL(sw))
	if (IS_ERR_OR_NULL(sw) || !sw->registered)
		return USB_ROLE_NONE;

	mutex_lock(&sw->lock);
@@ -276,6 +280,8 @@ usb_role_switch_register(struct device *parent,
		return ERR_PTR(ret);
	}

	sw->registered = true;

	/* TODO: Symlinks for the host port and the device controller. */

	return sw;
@@ -290,9 +296,11 @@ EXPORT_SYMBOL_GPL(usb_role_switch_register);
 */
void usb_role_switch_unregister(struct usb_role_switch *sw)
{
	if (!IS_ERR_OR_NULL(sw))
	if (!IS_ERR_OR_NULL(sw)) {
		sw->registered = false;
		device_unregister(&sw->dev);
	}
}
EXPORT_SYMBOL_GPL(usb_role_switch_unregister);

static int __init usb_roles_init(void)