diff mbox series

[v2,3/3] Revert "usb: common: usb-conn-gpio: Make VBUS supply optional"

Message ID 1621406386-18838-3-git-send-email-chunfeng.yun@mediatek.com (mailing list archive)
State New, archived
Headers show
Series [v2,1/3] usb: common: usb-conn-gpio: fix NULL pointer dereference of charger | expand

Commit Message

Chunfeng Yun (云春峰) May 19, 2021, 6:39 a.m. UTC
Vbus is already an optional supply, if the vbus-supply is not
provided in DTS, will use a dummy regulator,
the warning log is as below:
"supply vbus not found, using dummy regulator"

This reverts commit 4ddf1ac79e5f082451cd549283d2eb7559ab6ca9.

Cc: Thierry Reding <treding@nvidia.com>
Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
v2: remove unused variable "need_vbus"
---
 drivers/usb/common/usb-conn-gpio.c | 30 ++++++------------------------
 1 file changed, 6 insertions(+), 24 deletions(-)

Comments

Thierry Reding May 21, 2021, 1:20 p.m. UTC | #1
On Wed, May 19, 2021 at 02:39:46PM +0800, Chunfeng Yun wrote:
> Vbus is already an optional supply, if the vbus-supply is not
> provided in DTS, will use a dummy regulator,

That statement is not entirely correct. The dummy regulator is
substituted only if the supply is in fact not optional. The idea behind
that is to allow DTS files that don't specify all required regulators to
get away with it, based on the assumption that the supply is one of
those always-on supplies that are often not described in DTS.

> the warning log is as below:
> "supply vbus not found, using dummy regulator"

And the reason why we get that warning is to point out that the DTS has
a bug and that it should be fixed (by adding a proper regulator to take
the place of the dummy).

> This reverts commit 4ddf1ac79e5f082451cd549283d2eb7559ab6ca9.

But if you read the description of that commit, the purpose of that
patch was in fact to make the supply completely optional in the case
where we already have the VBUS supply specified for the USB port that
the connector is parented to.

So in that case the DTS doesn't have the bug because the VBUS supply is
already specified for the USB port and therefore it doesn't have to be
specified in the USB connector again. In fact, specifying it twice can
lead to a situation where the USB port may not be able to switch the
VBUS supply on or off because the setting might conflict with that of
the USB connector.

So unless there's a real reason why this is needed, I don't think this
should be applied.

Thierry

> Cc: Thierry Reding <treding@nvidia.com>
> Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
> ---
> v2: remove unused variable "need_vbus"
> ---
>  drivers/usb/common/usb-conn-gpio.c | 30 ++++++------------------------
>  1 file changed, 6 insertions(+), 24 deletions(-)
> 
> diff --git a/drivers/usb/common/usb-conn-gpio.c b/drivers/usb/common/usb-conn-gpio.c
> index dfbbc4f51ed6..65d89140cd19 100644
> --- a/drivers/usb/common/usb-conn-gpio.c
> +++ b/drivers/usb/common/usb-conn-gpio.c
> @@ -91,14 +91,14 @@ static void usb_conn_detect_cable(struct work_struct *work)
>  		return;
>  	}
>  
> -	if (info->last_role == USB_ROLE_HOST && info->vbus)
> +	if (info->last_role == USB_ROLE_HOST)
>  		regulator_disable(info->vbus);
>  
>  	ret = usb_role_switch_set_role(info->role_sw, role);
>  	if (ret)
>  		dev_err(info->dev, "failed to set role: %d\n", ret);
>  
> -	if (role == USB_ROLE_HOST && info->vbus) {
> +	if (role == USB_ROLE_HOST) {
>  		ret = regulator_enable(info->vbus);
>  		if (ret)
>  			dev_err(info->dev, "enable vbus regulator failed\n");
> @@ -106,9 +106,8 @@ static void usb_conn_detect_cable(struct work_struct *work)
>  
>  	info->last_role = role;
>  
> -	if (info->vbus)
> -		dev_dbg(info->dev, "vbus regulator is %s\n",
> -			regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
> +	dev_dbg(info->dev, "vbus regulator is %s\n",
> +		regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
>  
>  	power_supply_changed(info->charger);
>  }
> @@ -175,7 +174,6 @@ static int usb_conn_probe(struct platform_device *pdev)
>  {
>  	struct device *dev = &pdev->dev;
>  	struct usb_conn_info *info;
> -	bool need_vbus = true;
>  	int ret = 0;
>  
>  	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
> @@ -205,23 +203,7 @@ static int usb_conn_probe(struct platform_device *pdev)
>  
>  	INIT_DELAYED_WORK(&info->dw_det, usb_conn_detect_cable);
>  
> -	/*
> -	 * If the USB connector is a child of a USB port and that port already provides the VBUS
> -	 * supply, there's no need for the USB connector to provide it again.
> -	 */
> -	if (dev->parent && dev->parent->of_node) {
> -		if (of_find_property(dev->parent->of_node, "vbus-supply", NULL))
> -			need_vbus = false;
> -	}
> -
> -	if (!need_vbus) {
> -		info->vbus = devm_regulator_get_optional(dev, "vbus");
> -		if (PTR_ERR(info->vbus) == -ENODEV)
> -			info->vbus = NULL;
> -	} else {
> -		info->vbus = devm_regulator_get(dev, "vbus");
> -	}
> -
> +	info->vbus = devm_regulator_get(dev, "vbus");
>  	if (IS_ERR(info->vbus)) {
>  		ret = PTR_ERR(info->vbus);
>  		return dev_err_probe(dev, ret, "failed to get vbus :%d\n", ret);
> @@ -288,7 +270,7 @@ static int usb_conn_remove(struct platform_device *pdev)
>  
>  	cancel_delayed_work_sync(&info->dw_det);
>  
> -	if (info->last_role == USB_ROLE_HOST && info->vbus)
> +	if (info->last_role == USB_ROLE_HOST)
>  		regulator_disable(info->vbus);
>  
>  	usb_role_switch_put(info->role_sw);
> -- 
> 2.18.0
>
Greg KH May 21, 2021, 6:06 p.m. UTC | #2
On Fri, May 21, 2021 at 03:20:23PM +0200, Thierry Reding wrote:
> On Wed, May 19, 2021 at 02:39:46PM +0800, Chunfeng Yun wrote:
> > Vbus is already an optional supply, if the vbus-supply is not
> > provided in DTS, will use a dummy regulator,
> 
> That statement is not entirely correct. The dummy regulator is
> substituted only if the supply is in fact not optional. The idea behind
> that is to allow DTS files that don't specify all required regulators to
> get away with it, based on the assumption that the supply is one of
> those always-on supplies that are often not described in DTS.
> 
> > the warning log is as below:
> > "supply vbus not found, using dummy regulator"
> 
> And the reason why we get that warning is to point out that the DTS has
> a bug and that it should be fixed (by adding a proper regulator to take
> the place of the dummy).
> 
> > This reverts commit 4ddf1ac79e5f082451cd549283d2eb7559ab6ca9.
> 
> But if you read the description of that commit, the purpose of that
> patch was in fact to make the supply completely optional in the case
> where we already have the VBUS supply specified for the USB port that
> the connector is parented to.
> 
> So in that case the DTS doesn't have the bug because the VBUS supply is
> already specified for the USB port and therefore it doesn't have to be
> specified in the USB connector again. In fact, specifying it twice can
> lead to a situation where the USB port may not be able to switch the
> VBUS supply on or off because the setting might conflict with that of
> the USB connector.
> 
> So unless there's a real reason why this is needed, I don't think this
> should be applied.

I've dropped this from my tree now, thanks.

greg k-h
Chunfeng Yun (云春峰) May 24, 2021, 5:51 a.m. UTC | #3
On Fri, 2021-05-21 at 15:20 +0200, Thierry Reding wrote:
> On Wed, May 19, 2021 at 02:39:46PM +0800, Chunfeng Yun wrote:
> > Vbus is already an optional supply, if the vbus-supply is not
> > provided in DTS, will use a dummy regulator,
> 
> That statement is not entirely correct. The dummy regulator is
> substituted only if the supply is in fact not optional. The idea behind
> that is to allow DTS files that don't specify all required regulators to
> get away with it, based on the assumption that the supply is one of
> those always-on supplies that are often not described in DTS.
Yes, you are right.
But from the point of result, it indeed can help to handle the absent
regulator.
> 
> > the warning log is as below:
> > "supply vbus not found, using dummy regulator"
> 
> And the reason why we get that warning is to point out that the DTS has
> a bug and that it should be fixed (by adding a proper regulator to take
> the place of the dummy).
> 
> > This reverts commit 4ddf1ac79e5f082451cd549283d2eb7559ab6ca9.
> 
> But if you read the description of that commit, the purpose of that
> patch was in fact to make the supply completely optional in the case
> where we already have the VBUS supply specified for the USB port that
> the connector is parented to.
Could you please give an example you mentioned?

It seems prefer to provide vbus supply in connector instead of port
according to dt-binding

Thanks

> 
> So in that case the DTS doesn't have the bug because the VBUS supply is
> already specified for the USB port and therefore it doesn't have to be
> specified in the USB connector again. In fact, specifying it twice can
> lead to a situation where the USB port may not be able to switch the
> VBUS supply on or off because the setting might conflict with that of
> the USB connector.

> 
> So unless there's a real reason why this is needed, I don't think this
> should be applied.
> 
> Thierry
> 
> > Cc: Thierry Reding <treding@nvidia.com>
> > Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
> > ---
> > v2: remove unused variable "need_vbus"
> > ---
> >  drivers/usb/common/usb-conn-gpio.c | 30 ++++++------------------------
> >  1 file changed, 6 insertions(+), 24 deletions(-)
> > 
> > diff --git a/drivers/usb/common/usb-conn-gpio.c b/drivers/usb/common/usb-conn-gpio.c
> > index dfbbc4f51ed6..65d89140cd19 100644
> > --- a/drivers/usb/common/usb-conn-gpio.c
> > +++ b/drivers/usb/common/usb-conn-gpio.c
> > @@ -91,14 +91,14 @@ static void usb_conn_detect_cable(struct work_struct *work)
> >  		return;
> >  	}
> >  
> > -	if (info->last_role == USB_ROLE_HOST && info->vbus)
> > +	if (info->last_role == USB_ROLE_HOST)
> >  		regulator_disable(info->vbus);
> >  
> >  	ret = usb_role_switch_set_role(info->role_sw, role);
> >  	if (ret)
> >  		dev_err(info->dev, "failed to set role: %d\n", ret);
> >  
> > -	if (role == USB_ROLE_HOST && info->vbus) {
> > +	if (role == USB_ROLE_HOST) {
> >  		ret = regulator_enable(info->vbus);
> >  		if (ret)
> >  			dev_err(info->dev, "enable vbus regulator failed\n");
> > @@ -106,9 +106,8 @@ static void usb_conn_detect_cable(struct work_struct *work)
> >  
> >  	info->last_role = role;
> >  
> > -	if (info->vbus)
> > -		dev_dbg(info->dev, "vbus regulator is %s\n",
> > -			regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
> > +	dev_dbg(info->dev, "vbus regulator is %s\n",
> > +		regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
> >  
> >  	power_supply_changed(info->charger);
> >  }
> > @@ -175,7 +174,6 @@ static int usb_conn_probe(struct platform_device *pdev)
> >  {
> >  	struct device *dev = &pdev->dev;
> >  	struct usb_conn_info *info;
> > -	bool need_vbus = true;
> >  	int ret = 0;
> >  
> >  	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
> > @@ -205,23 +203,7 @@ static int usb_conn_probe(struct platform_device *pdev)
> >  
> >  	INIT_DELAYED_WORK(&info->dw_det, usb_conn_detect_cable);
> >  
> > -	/*
> > -	 * If the USB connector is a child of a USB port and that port already provides the VBUS
> > -	 * supply, there's no need for the USB connector to provide it again.
> > -	 */
> > -	if (dev->parent && dev->parent->of_node) {
> > -		if (of_find_property(dev->parent->of_node, "vbus-supply", NULL))
> > -			need_vbus = false;
> > -	}
> > -
> > -	if (!need_vbus) {
> > -		info->vbus = devm_regulator_get_optional(dev, "vbus");
> > -		if (PTR_ERR(info->vbus) == -ENODEV)
> > -			info->vbus = NULL;
> > -	} else {
> > -		info->vbus = devm_regulator_get(dev, "vbus");
> > -	}
> > -
> > +	info->vbus = devm_regulator_get(dev, "vbus");
> >  	if (IS_ERR(info->vbus)) {
> >  		ret = PTR_ERR(info->vbus);
> >  		return dev_err_probe(dev, ret, "failed to get vbus :%d\n", ret);
> > @@ -288,7 +270,7 @@ static int usb_conn_remove(struct platform_device *pdev)
> >  
> >  	cancel_delayed_work_sync(&info->dw_det);
> >  
> > -	if (info->last_role == USB_ROLE_HOST && info->vbus)
> > +	if (info->last_role == USB_ROLE_HOST)
> >  		regulator_disable(info->vbus);
> >  
> >  	usb_role_switch_put(info->role_sw);
> > -- 
> > 2.18.0
> >
Thierry Reding May 25, 2021, 11:36 a.m. UTC | #4
On Mon, May 24, 2021 at 01:51:51PM +0800, Chunfeng Yun wrote:
> On Fri, 2021-05-21 at 15:20 +0200, Thierry Reding wrote:
> > On Wed, May 19, 2021 at 02:39:46PM +0800, Chunfeng Yun wrote:
> > > Vbus is already an optional supply, if the vbus-supply is not
> > > provided in DTS, will use a dummy regulator,
> > 
> > That statement is not entirely correct. The dummy regulator is
> > substituted only if the supply is in fact not optional. The idea behind
> > that is to allow DTS files that don't specify all required regulators to
> > get away with it, based on the assumption that the supply is one of
> > those always-on supplies that are often not described in DTS.
> Yes, you are right.
> But from the point of result, it indeed can help to handle the absent
> regulator.
> > 
> > > the warning log is as below:
> > > "supply vbus not found, using dummy regulator"
> > 
> > And the reason why we get that warning is to point out that the DTS has
> > a bug and that it should be fixed (by adding a proper regulator to take
> > the place of the dummy).
> > 
> > > This reverts commit 4ddf1ac79e5f082451cd549283d2eb7559ab6ca9.
> > 
> > But if you read the description of that commit, the purpose of that
> > patch was in fact to make the supply completely optional in the case
> > where we already have the VBUS supply specified for the USB port that
> > the connector is parented to.
> Could you please give an example you mentioned?

You can find examples of this in these:

	arch/arm64/boot/dts/nvidia/tegra210-p2597.dtsi
	arch/arm64/boot/dts/nvidia/tegra210-p3450-0000.dts
	arch/arm64/boot/dts/nvidia/tegra186-p2771-0000.dts

> It seems prefer to provide vbus supply in connector instead of port
> according to dt-binding

My recollection is that the above (or at least some of them) predate USB
connectors.

It's possible that we could convert the above to have the VBUS supply
listed in the connector instead of the port. However, since we have to
preserve backwards compatibility with older device trees, we can't
revert the commit anyway.

Thierry
Chunfeng Yun (云春峰) May 26, 2021, 1:44 a.m. UTC | #5
On Tue, 2021-05-25 at 13:36 +0200, Thierry Reding wrote:
> On Mon, May 24, 2021 at 01:51:51PM +0800, Chunfeng Yun wrote:
> > On Fri, 2021-05-21 at 15:20 +0200, Thierry Reding wrote:
> > > On Wed, May 19, 2021 at 02:39:46PM +0800, Chunfeng Yun wrote:
> > > > Vbus is already an optional supply, if the vbus-supply is not
> > > > provided in DTS, will use a dummy regulator,
> > > 
> > > That statement is not entirely correct. The dummy regulator is
> > > substituted only if the supply is in fact not optional. The idea behind
> > > that is to allow DTS files that don't specify all required regulators to
> > > get away with it, based on the assumption that the supply is one of
> > > those always-on supplies that are often not described in DTS.
> > Yes, you are right.
> > But from the point of result, it indeed can help to handle the absent
> > regulator.
> > > 
> > > > the warning log is as below:
> > > > "supply vbus not found, using dummy regulator"
> > > 
> > > And the reason why we get that warning is to point out that the DTS has
> > > a bug and that it should be fixed (by adding a proper regulator to take
> > > the place of the dummy).
> > > 
> > > > This reverts commit 4ddf1ac79e5f082451cd549283d2eb7559ab6ca9.
> > > 
> > > But if you read the description of that commit, the purpose of that
> > > patch was in fact to make the supply completely optional in the case
> > > where we already have the VBUS supply specified for the USB port that
> > > the connector is parented to.
> > Could you please give an example you mentioned?
> 
> You can find examples of this in these:
> 
> 	arch/arm64/boot/dts/nvidia/tegra210-p2597.dtsi
> 	arch/arm64/boot/dts/nvidia/tegra210-p3450-0000.dts
> 	arch/arm64/boot/dts/nvidia/tegra186-p2771-0000.dts
> 
> > It seems prefer to provide vbus supply in connector instead of port
> > according to dt-binding
> 
> My recollection is that the above (or at least some of them) predate USB
> connectors.
> 
> It's possible that we could convert the above to have the VBUS supply
> listed in the connector instead of the port. However, since we have to
> preserve backwards compatibility with older device trees, we can't
> revert the commit anyway.
Got it, thanks a lot

> 
> Thierry
diff mbox series

Patch

diff --git a/drivers/usb/common/usb-conn-gpio.c b/drivers/usb/common/usb-conn-gpio.c
index dfbbc4f51ed6..65d89140cd19 100644
--- a/drivers/usb/common/usb-conn-gpio.c
+++ b/drivers/usb/common/usb-conn-gpio.c
@@ -91,14 +91,14 @@  static void usb_conn_detect_cable(struct work_struct *work)
 		return;
 	}
 
-	if (info->last_role == USB_ROLE_HOST && info->vbus)
+	if (info->last_role == USB_ROLE_HOST)
 		regulator_disable(info->vbus);
 
 	ret = usb_role_switch_set_role(info->role_sw, role);
 	if (ret)
 		dev_err(info->dev, "failed to set role: %d\n", ret);
 
-	if (role == USB_ROLE_HOST && info->vbus) {
+	if (role == USB_ROLE_HOST) {
 		ret = regulator_enable(info->vbus);
 		if (ret)
 			dev_err(info->dev, "enable vbus regulator failed\n");
@@ -106,9 +106,8 @@  static void usb_conn_detect_cable(struct work_struct *work)
 
 	info->last_role = role;
 
-	if (info->vbus)
-		dev_dbg(info->dev, "vbus regulator is %s\n",
-			regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
+	dev_dbg(info->dev, "vbus regulator is %s\n",
+		regulator_is_enabled(info->vbus) ? "enabled" : "disabled");
 
 	power_supply_changed(info->charger);
 }
@@ -175,7 +174,6 @@  static int usb_conn_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct usb_conn_info *info;
-	bool need_vbus = true;
 	int ret = 0;
 
 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
@@ -205,23 +203,7 @@  static int usb_conn_probe(struct platform_device *pdev)
 
 	INIT_DELAYED_WORK(&info->dw_det, usb_conn_detect_cable);
 
-	/*
-	 * If the USB connector is a child of a USB port and that port already provides the VBUS
-	 * supply, there's no need for the USB connector to provide it again.
-	 */
-	if (dev->parent && dev->parent->of_node) {
-		if (of_find_property(dev->parent->of_node, "vbus-supply", NULL))
-			need_vbus = false;
-	}
-
-	if (!need_vbus) {
-		info->vbus = devm_regulator_get_optional(dev, "vbus");
-		if (PTR_ERR(info->vbus) == -ENODEV)
-			info->vbus = NULL;
-	} else {
-		info->vbus = devm_regulator_get(dev, "vbus");
-	}
-
+	info->vbus = devm_regulator_get(dev, "vbus");
 	if (IS_ERR(info->vbus)) {
 		ret = PTR_ERR(info->vbus);
 		return dev_err_probe(dev, ret, "failed to get vbus :%d\n", ret);
@@ -288,7 +270,7 @@  static int usb_conn_remove(struct platform_device *pdev)
 
 	cancel_delayed_work_sync(&info->dw_det);
 
-	if (info->last_role == USB_ROLE_HOST && info->vbus)
+	if (info->last_role == USB_ROLE_HOST)
 		regulator_disable(info->vbus);
 
 	usb_role_switch_put(info->role_sw);