usb: roles: don't get/set_role() when usb_role_switch is unregistered
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: fde0aa6c17
("usb: common: Small class for USB role switches")
cc: stable@vger.kernel.org
Signed-off-by: Xu Yang <xu.yang_2@nxp.com>
Acked-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Link: https://lore.kernel.org/r/20240129093739.2371530-2-xu.yang_2@nxp.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
parent
1c9be13846
commit
b787a3e781
1 changed files with 10 additions and 2 deletions
|
@ -23,6 +23,7 @@ struct usb_role_switch {
|
||||||
struct mutex lock; /* device lock*/
|
struct mutex lock; /* device lock*/
|
||||||
struct module *module; /* the module this device depends on */
|
struct module *module; /* the module this device depends on */
|
||||||
enum usb_role role;
|
enum usb_role role;
|
||||||
|
bool registered;
|
||||||
|
|
||||||
/* From descriptor */
|
/* From descriptor */
|
||||||
struct device *usb2_port;
|
struct device *usb2_port;
|
||||||
|
@ -49,6 +50,9 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
|
||||||
if (IS_ERR_OR_NULL(sw))
|
if (IS_ERR_OR_NULL(sw))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
if (!sw->registered)
|
||||||
|
return -EOPNOTSUPP;
|
||||||
|
|
||||||
mutex_lock(&sw->lock);
|
mutex_lock(&sw->lock);
|
||||||
|
|
||||||
ret = sw->set(sw, role);
|
ret = sw->set(sw, role);
|
||||||
|
@ -74,7 +78,7 @@ enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
|
||||||
{
|
{
|
||||||
enum usb_role role;
|
enum usb_role role;
|
||||||
|
|
||||||
if (IS_ERR_OR_NULL(sw))
|
if (IS_ERR_OR_NULL(sw) || !sw->registered)
|
||||||
return USB_ROLE_NONE;
|
return USB_ROLE_NONE;
|
||||||
|
|
||||||
mutex_lock(&sw->lock);
|
mutex_lock(&sw->lock);
|
||||||
|
@ -357,6 +361,8 @@ usb_role_switch_register(struct device *parent,
|
||||||
return ERR_PTR(ret);
|
return ERR_PTR(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sw->registered = true;
|
||||||
|
|
||||||
/* TODO: Symlinks for the host port and the device controller. */
|
/* TODO: Symlinks for the host port and the device controller. */
|
||||||
|
|
||||||
return sw;
|
return sw;
|
||||||
|
@ -371,9 +377,11 @@ EXPORT_SYMBOL_GPL(usb_role_switch_register);
|
||||||
*/
|
*/
|
||||||
void usb_role_switch_unregister(struct usb_role_switch *sw)
|
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);
|
device_unregister(&sw->dev);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
|
EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue