diff mbox series

[v1,1/1] usb: dwc3: Don't switch OTG -> peripheral if extcon is present

Message ID 20221017233510.53336-1-andriy.shevchenko@linux.intel.com (mailing list archive)
State Accepted
Commit d182c2e1bc92084c038b44c618f29589a4de9f66
Headers show
Series [v1,1/1] usb: dwc3: Don't switch OTG -> peripheral if extcon is present | expand

Commit Message

Andy Shevchenko Oct. 17, 2022, 11:35 p.m. UTC
From: Andrey Smirnov <andrew.smirnov@gmail.com>

If the extcon device exists, get the mode from the extcon device. If
the controller is DRD and the driver is unable to determine the mode,
only then default the dr_mode to USB_DR_MODE_PERIPHERAL.

Reported-by: Steev Klimaszewski <steev@kali.org>
Fixes: 7a84e7353e23 ("Revert "usb: dwc3: Don't switch OTG -> peripheral if extcon is present"")
Reviewed-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/usb/dwc3/core.c | 49 +++++++++++++++++++++++++++++++++++++++-
 drivers/usb/dwc3/drd.c  | 50 -----------------------------------------
 2 files changed, 48 insertions(+), 51 deletions(-)

Comments

Steev Klimaszewski Oct. 18, 2022, 2:04 a.m. UTC | #1
Hi Andy,

On 10/17/22 6:35 PM, Andy Shevchenko wrote:
> From: Andrey Smirnov <andrew.smirnov@gmail.com>
>
> If the extcon device exists, get the mode from the extcon device. If
> the controller is DRD and the driver is unable to determine the mode,
> only then default the dr_mode to USB_DR_MODE_PERIPHERAL.
>
> Reported-by: Steev Klimaszewski <steev@kali.org>
> Fixes: 7a84e7353e23 ("Revert "usb: dwc3: Don't switch OTG -> peripheral if extcon is present"")
> Reviewed-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
> Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>   drivers/usb/dwc3/core.c | 49 +++++++++++++++++++++++++++++++++++++++-
>   drivers/usb/dwc3/drd.c  | 50 -----------------------------------------
>   2 files changed, 48 insertions(+), 51 deletions(-)
>
> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> index ea51624461b5..c0e7c76dc5c8 100644
> --- a/drivers/usb/dwc3/core.c
> +++ b/drivers/usb/dwc3/core.c
> @@ -23,6 +23,7 @@
>   #include <linux/delay.h>
>   #include <linux/dma-mapping.h>
>   #include <linux/of.h>
> +#include <linux/of_graph.h>
>   #include <linux/acpi.h>
>   #include <linux/pinctrl/consumer.h>
>   #include <linux/reset.h>
> @@ -85,7 +86,7 @@ static int dwc3_get_dr_mode(struct dwc3 *dwc)
>   		 * mode. If the controller supports DRD but the dr_mode is not
>   		 * specified or set to OTG, then set the mode to peripheral.
>   		 */
> -		if (mode == USB_DR_MODE_OTG &&
> +		if (mode == USB_DR_MODE_OTG && !dwc->edev &&
>   		    (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
>   		     !device_property_read_bool(dwc->dev, "usb-role-switch")) &&
>   		    !DWC3_VER_IS_PRIOR(DWC3, 330A))
> @@ -1690,6 +1691,46 @@ static void dwc3_check_params(struct dwc3 *dwc)
>   	}
>   }
>   
> +static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
> +{
> +	struct device *dev = dwc->dev;
> +	struct device_node *np_phy;
> +	struct extcon_dev *edev = NULL;
> +	const char *name;
> +
> +	if (device_property_read_bool(dev, "extcon"))
> +		return extcon_get_edev_by_phandle(dev, 0);
> +
> +	/*
> +	 * Device tree platforms should get extcon via phandle.
> +	 * On ACPI platforms, we get the name from a device property.
> +	 * This device property is for kernel internal use only and
> +	 * is expected to be set by the glue code.
> +	 */
> +	if (device_property_read_string(dev, "linux,extcon-name", &name) == 0)
> +		return extcon_get_extcon_dev(name);
> +
> +	/*
> +	 * Try to get an extcon device from the USB PHY controller's "port"
> +	 * node. Check if it has the "port" node first, to avoid printing the
> +	 * error message from underlying code, as it's a valid case: extcon
> +	 * device (and "port" node) may be missing in case of "usb-role-switch"
> +	 * or OTG mode.
> +	 */
> +	np_phy = of_parse_phandle(dev->of_node, "phys", 0);
> +	if (of_graph_is_present(np_phy)) {
> +		struct device_node *np_conn;
> +
> +		np_conn = of_graph_get_remote_node(np_phy, -1, -1);
> +		if (np_conn)
> +			edev = extcon_find_edev_by_node(np_conn);
> +		of_node_put(np_conn);
> +	}
> +	of_node_put(np_phy);
> +
> +	return edev;
> +}
> +
>   static int dwc3_probe(struct platform_device *pdev)
>   {
>   	struct device		*dev = &pdev->dev;
> @@ -1840,6 +1881,12 @@ static int dwc3_probe(struct platform_device *pdev)
>   		goto err2;
>   	}
>   
> +	dwc->edev = dwc3_get_extcon(dwc);
> +	if (IS_ERR(dwc->edev)) {
> +		ret = dev_err_probe(dwc->dev, PTR_ERR(dwc->edev), "failed to get extcon\n");
> +		goto err3;
> +	}
> +
>   	ret = dwc3_get_dr_mode(dwc);
>   	if (ret)
>   		goto err3;
> diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c
> index 8cad9e7d3368..039bf241769a 100644
> --- a/drivers/usb/dwc3/drd.c
> +++ b/drivers/usb/dwc3/drd.c
> @@ -8,7 +8,6 @@
>    */
>   
>   #include <linux/extcon.h>
> -#include <linux/of_graph.h>
>   #include <linux/of_platform.h>
>   #include <linux/platform_device.h>
>   #include <linux/property.h>
> @@ -439,51 +438,6 @@ static int dwc3_drd_notifier(struct notifier_block *nb,
>   	return NOTIFY_DONE;
>   }
>   
> -static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
> -{
> -	struct device *dev = dwc->dev;
> -	struct device_node *np_phy;
> -	struct extcon_dev *edev = NULL;
> -	const char *name;
> -
> -	if (device_property_read_bool(dev, "extcon"))
> -		return extcon_get_edev_by_phandle(dev, 0);
> -
> -	/*
> -	 * Device tree platforms should get extcon via phandle.
> -	 * On ACPI platforms, we get the name from a device property.
> -	 * This device property is for kernel internal use only and
> -	 * is expected to be set by the glue code.
> -	 */
> -	if (device_property_read_string(dev, "linux,extcon-name", &name) == 0) {
> -		edev = extcon_get_extcon_dev(name);
> -		if (!edev)
> -			return ERR_PTR(-EPROBE_DEFER);
> -
> -		return edev;
> -	}
> -
> -	/*
> -	 * Try to get an extcon device from the USB PHY controller's "port"
> -	 * node. Check if it has the "port" node first, to avoid printing the
> -	 * error message from underlying code, as it's a valid case: extcon
> -	 * device (and "port" node) may be missing in case of "usb-role-switch"
> -	 * or OTG mode.
> -	 */
> -	np_phy = of_parse_phandle(dev->of_node, "phys", 0);
> -	if (of_graph_is_present(np_phy)) {
> -		struct device_node *np_conn;
> -
> -		np_conn = of_graph_get_remote_node(np_phy, -1, -1);
> -		if (np_conn)
> -			edev = extcon_find_edev_by_node(np_conn);
> -		of_node_put(np_conn);
> -	}
> -	of_node_put(np_phy);
> -
> -	return edev;
> -}
> -
>   #if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
>   #define ROLE_SWITCH 1
>   static int dwc3_usb_role_switch_set(struct usb_role_switch *sw,
> @@ -588,10 +542,6 @@ int dwc3_drd_init(struct dwc3 *dwc)
>   	    device_property_read_bool(dwc->dev, "usb-role-switch"))
>   		return dwc3_setup_role_switch(dwc);
>   
> -	dwc->edev = dwc3_get_extcon(dwc);
> -	if (IS_ERR(dwc->edev))
> -		return PTR_ERR(dwc->edev);
> -
>   	if (dwc->edev) {
>   		dwc->edev_nb.notifier_call = dwc3_drd_notifier;
>   		ret = extcon_register_notifier(dwc->edev, EXTCON_USB_HOST,

Thank you for the quick fix! Confirmed that this works for me on my 
Lenovo Yoga C630

Tested-by: Steev Klimaszewski <steev@kali.org>
Thinh Nguyen Oct. 18, 2022, 7:02 p.m. UTC | #2
On Tue, Oct 18, 2022, Andy Shevchenko wrote:
> From: Andrey Smirnov <andrew.smirnov@gmail.com>
> 
> If the extcon device exists, get the mode from the extcon device. If
> the controller is DRD and the driver is unable to determine the mode,
> only then default the dr_mode to USB_DR_MODE_PERIPHERAL.
> 
> Reported-by: Steev Klimaszewski <steev@kali.org>
> Fixes: 7a84e7353e23 ("Revert "usb: dwc3: Don't switch OTG -> peripheral if extcon is present"")
> Reviewed-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
> Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/usb/dwc3/core.c | 49 +++++++++++++++++++++++++++++++++++++++-
>  drivers/usb/dwc3/drd.c  | 50 -----------------------------------------
>  2 files changed, 48 insertions(+), 51 deletions(-)
> 
> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> index ea51624461b5..c0e7c76dc5c8 100644
> --- a/drivers/usb/dwc3/core.c
> +++ b/drivers/usb/dwc3/core.c
> @@ -23,6 +23,7 @@
>  #include <linux/delay.h>
>  #include <linux/dma-mapping.h>
>  #include <linux/of.h>
> +#include <linux/of_graph.h>
>  #include <linux/acpi.h>
>  #include <linux/pinctrl/consumer.h>
>  #include <linux/reset.h>
> @@ -85,7 +86,7 @@ static int dwc3_get_dr_mode(struct dwc3 *dwc)
>  		 * mode. If the controller supports DRD but the dr_mode is not
>  		 * specified or set to OTG, then set the mode to peripheral.
>  		 */
> -		if (mode == USB_DR_MODE_OTG &&
> +		if (mode == USB_DR_MODE_OTG && !dwc->edev &&
>  		    (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
>  		     !device_property_read_bool(dwc->dev, "usb-role-switch")) &&
>  		    !DWC3_VER_IS_PRIOR(DWC3, 330A))
> @@ -1690,6 +1691,46 @@ static void dwc3_check_params(struct dwc3 *dwc)
>  	}
>  }
>  
> +static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
> +{
> +	struct device *dev = dwc->dev;
> +	struct device_node *np_phy;
> +	struct extcon_dev *edev = NULL;
> +	const char *name;
> +
> +	if (device_property_read_bool(dev, "extcon"))
> +		return extcon_get_edev_by_phandle(dev, 0);
> +
> +	/*
> +	 * Device tree platforms should get extcon via phandle.
> +	 * On ACPI platforms, we get the name from a device property.
> +	 * This device property is for kernel internal use only and
> +	 * is expected to be set by the glue code.
> +	 */
> +	if (device_property_read_string(dev, "linux,extcon-name", &name) == 0)
> +		return extcon_get_extcon_dev(name);
> +
> +	/*
> +	 * Try to get an extcon device from the USB PHY controller's "port"
> +	 * node. Check if it has the "port" node first, to avoid printing the
> +	 * error message from underlying code, as it's a valid case: extcon
> +	 * device (and "port" node) may be missing in case of "usb-role-switch"
> +	 * or OTG mode.
> +	 */
> +	np_phy = of_parse_phandle(dev->of_node, "phys", 0);
> +	if (of_graph_is_present(np_phy)) {
> +		struct device_node *np_conn;
> +
> +		np_conn = of_graph_get_remote_node(np_phy, -1, -1);
> +		if (np_conn)
> +			edev = extcon_find_edev_by_node(np_conn);
> +		of_node_put(np_conn);
> +	}
> +	of_node_put(np_phy);
> +
> +	return edev;
> +}
> +
>  static int dwc3_probe(struct platform_device *pdev)
>  {
>  	struct device		*dev = &pdev->dev;
> @@ -1840,6 +1881,12 @@ static int dwc3_probe(struct platform_device *pdev)
>  		goto err2;
>  	}
>  
> +	dwc->edev = dwc3_get_extcon(dwc);
> +	if (IS_ERR(dwc->edev)) {
> +		ret = dev_err_probe(dwc->dev, PTR_ERR(dwc->edev), "failed to get extcon\n");
> +		goto err3;
> +	}
> +
>  	ret = dwc3_get_dr_mode(dwc);
>  	if (ret)
>  		goto err3;
> diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c
> index 8cad9e7d3368..039bf241769a 100644
> --- a/drivers/usb/dwc3/drd.c
> +++ b/drivers/usb/dwc3/drd.c
> @@ -8,7 +8,6 @@
>   */
>  
>  #include <linux/extcon.h>
> -#include <linux/of_graph.h>
>  #include <linux/of_platform.h>
>  #include <linux/platform_device.h>
>  #include <linux/property.h>
> @@ -439,51 +438,6 @@ static int dwc3_drd_notifier(struct notifier_block *nb,
>  	return NOTIFY_DONE;
>  }
>  
> -static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
> -{
> -	struct device *dev = dwc->dev;
> -	struct device_node *np_phy;
> -	struct extcon_dev *edev = NULL;
> -	const char *name;
> -
> -	if (device_property_read_bool(dev, "extcon"))
> -		return extcon_get_edev_by_phandle(dev, 0);
> -
> -	/*
> -	 * Device tree platforms should get extcon via phandle.
> -	 * On ACPI platforms, we get the name from a device property.
> -	 * This device property is for kernel internal use only and
> -	 * is expected to be set by the glue code.
> -	 */
> -	if (device_property_read_string(dev, "linux,extcon-name", &name) == 0) {
> -		edev = extcon_get_extcon_dev(name);
> -		if (!edev)
> -			return ERR_PTR(-EPROBE_DEFER);
> -
> -		return edev;
> -	}
> -
> -	/*
> -	 * Try to get an extcon device from the USB PHY controller's "port"
> -	 * node. Check if it has the "port" node first, to avoid printing the
> -	 * error message from underlying code, as it's a valid case: extcon
> -	 * device (and "port" node) may be missing in case of "usb-role-switch"
> -	 * or OTG mode.
> -	 */
> -	np_phy = of_parse_phandle(dev->of_node, "phys", 0);
> -	if (of_graph_is_present(np_phy)) {
> -		struct device_node *np_conn;
> -
> -		np_conn = of_graph_get_remote_node(np_phy, -1, -1);
> -		if (np_conn)
> -			edev = extcon_find_edev_by_node(np_conn);
> -		of_node_put(np_conn);
> -	}
> -	of_node_put(np_phy);
> -
> -	return edev;
> -}
> -
>  #if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
>  #define ROLE_SWITCH 1
>  static int dwc3_usb_role_switch_set(struct usb_role_switch *sw,
> @@ -588,10 +542,6 @@ int dwc3_drd_init(struct dwc3 *dwc)
>  	    device_property_read_bool(dwc->dev, "usb-role-switch"))
>  		return dwc3_setup_role_switch(dwc);
>  
> -	dwc->edev = dwc3_get_extcon(dwc);
> -	if (IS_ERR(dwc->edev))
> -		return PTR_ERR(dwc->edev);
> -
>  	if (dwc->edev) {
>  		dwc->edev_nb.notifier_call = dwc3_drd_notifier;
>  		ret = extcon_register_notifier(dwc->edev, EXTCON_USB_HOST,
> -- 
> 2.35.1
> 

Acked-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>

Thanks,
Thinh
diff mbox series

Patch

diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index ea51624461b5..c0e7c76dc5c8 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -23,6 +23,7 @@ 
 #include <linux/delay.h>
 #include <linux/dma-mapping.h>
 #include <linux/of.h>
+#include <linux/of_graph.h>
 #include <linux/acpi.h>
 #include <linux/pinctrl/consumer.h>
 #include <linux/reset.h>
@@ -85,7 +86,7 @@  static int dwc3_get_dr_mode(struct dwc3 *dwc)
 		 * mode. If the controller supports DRD but the dr_mode is not
 		 * specified or set to OTG, then set the mode to peripheral.
 		 */
-		if (mode == USB_DR_MODE_OTG &&
+		if (mode == USB_DR_MODE_OTG && !dwc->edev &&
 		    (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
 		     !device_property_read_bool(dwc->dev, "usb-role-switch")) &&
 		    !DWC3_VER_IS_PRIOR(DWC3, 330A))
@@ -1690,6 +1691,46 @@  static void dwc3_check_params(struct dwc3 *dwc)
 	}
 }
 
+static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
+{
+	struct device *dev = dwc->dev;
+	struct device_node *np_phy;
+	struct extcon_dev *edev = NULL;
+	const char *name;
+
+	if (device_property_read_bool(dev, "extcon"))
+		return extcon_get_edev_by_phandle(dev, 0);
+
+	/*
+	 * Device tree platforms should get extcon via phandle.
+	 * On ACPI platforms, we get the name from a device property.
+	 * This device property is for kernel internal use only and
+	 * is expected to be set by the glue code.
+	 */
+	if (device_property_read_string(dev, "linux,extcon-name", &name) == 0)
+		return extcon_get_extcon_dev(name);
+
+	/*
+	 * Try to get an extcon device from the USB PHY controller's "port"
+	 * node. Check if it has the "port" node first, to avoid printing the
+	 * error message from underlying code, as it's a valid case: extcon
+	 * device (and "port" node) may be missing in case of "usb-role-switch"
+	 * or OTG mode.
+	 */
+	np_phy = of_parse_phandle(dev->of_node, "phys", 0);
+	if (of_graph_is_present(np_phy)) {
+		struct device_node *np_conn;
+
+		np_conn = of_graph_get_remote_node(np_phy, -1, -1);
+		if (np_conn)
+			edev = extcon_find_edev_by_node(np_conn);
+		of_node_put(np_conn);
+	}
+	of_node_put(np_phy);
+
+	return edev;
+}
+
 static int dwc3_probe(struct platform_device *pdev)
 {
 	struct device		*dev = &pdev->dev;
@@ -1840,6 +1881,12 @@  static int dwc3_probe(struct platform_device *pdev)
 		goto err2;
 	}
 
+	dwc->edev = dwc3_get_extcon(dwc);
+	if (IS_ERR(dwc->edev)) {
+		ret = dev_err_probe(dwc->dev, PTR_ERR(dwc->edev), "failed to get extcon\n");
+		goto err3;
+	}
+
 	ret = dwc3_get_dr_mode(dwc);
 	if (ret)
 		goto err3;
diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c
index 8cad9e7d3368..039bf241769a 100644
--- a/drivers/usb/dwc3/drd.c
+++ b/drivers/usb/dwc3/drd.c
@@ -8,7 +8,6 @@ 
  */
 
 #include <linux/extcon.h>
-#include <linux/of_graph.h>
 #include <linux/of_platform.h>
 #include <linux/platform_device.h>
 #include <linux/property.h>
@@ -439,51 +438,6 @@  static int dwc3_drd_notifier(struct notifier_block *nb,
 	return NOTIFY_DONE;
 }
 
-static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc)
-{
-	struct device *dev = dwc->dev;
-	struct device_node *np_phy;
-	struct extcon_dev *edev = NULL;
-	const char *name;
-
-	if (device_property_read_bool(dev, "extcon"))
-		return extcon_get_edev_by_phandle(dev, 0);
-
-	/*
-	 * Device tree platforms should get extcon via phandle.
-	 * On ACPI platforms, we get the name from a device property.
-	 * This device property is for kernel internal use only and
-	 * is expected to be set by the glue code.
-	 */
-	if (device_property_read_string(dev, "linux,extcon-name", &name) == 0) {
-		edev = extcon_get_extcon_dev(name);
-		if (!edev)
-			return ERR_PTR(-EPROBE_DEFER);
-
-		return edev;
-	}
-
-	/*
-	 * Try to get an extcon device from the USB PHY controller's "port"
-	 * node. Check if it has the "port" node first, to avoid printing the
-	 * error message from underlying code, as it's a valid case: extcon
-	 * device (and "port" node) may be missing in case of "usb-role-switch"
-	 * or OTG mode.
-	 */
-	np_phy = of_parse_phandle(dev->of_node, "phys", 0);
-	if (of_graph_is_present(np_phy)) {
-		struct device_node *np_conn;
-
-		np_conn = of_graph_get_remote_node(np_phy, -1, -1);
-		if (np_conn)
-			edev = extcon_find_edev_by_node(np_conn);
-		of_node_put(np_conn);
-	}
-	of_node_put(np_phy);
-
-	return edev;
-}
-
 #if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
 #define ROLE_SWITCH 1
 static int dwc3_usb_role_switch_set(struct usb_role_switch *sw,
@@ -588,10 +542,6 @@  int dwc3_drd_init(struct dwc3 *dwc)
 	    device_property_read_bool(dwc->dev, "usb-role-switch"))
 		return dwc3_setup_role_switch(dwc);
 
-	dwc->edev = dwc3_get_extcon(dwc);
-	if (IS_ERR(dwc->edev))
-		return PTR_ERR(dwc->edev);
-
 	if (dwc->edev) {
 		dwc->edev_nb.notifier_call = dwc3_drd_notifier;
 		ret = extcon_register_notifier(dwc->edev, EXTCON_USB_HOST,