Re: [PATCH v3] usb: dwc2: dwc2_vbus_supply_init: fix error check

From: Tomeu Vizoso
Date: Thu Mar 22 2018 - 09:34:23 EST


On 03/22/2018 02:26 PM, Heiko StÃbner wrote:
Am Donnerstag, 22. MÃrz 2018, 14:14:51 CET schrieb Tomeu Vizoso:
devm_regulator_get_optional returns -ENODEV if the regulator isn't
there, so if that's the case we have to make sure not to leave -ENODEV
in the regulator pointer.

Also, make sure we return 0 in that case, but correctly propagate any
other errors. Also propagate the error from _dwc2_hcd_start.

Fixes: 531ef5ebea96 ("usb: dwc2: add support for host mode external vbus
supply") Cc: Amelie Delaunay <amelie.delaunay@xxxxxx>
Signed-off-by: Tomeu Vizoso <tomeu.vizoso@xxxxxxxxxxxxx>

---

v2: Only overwrite the error in the pointer after checking it (Heiko
StÃbner <heiko@xxxxxxxxx>)
v3: Remove hunks that shouldn't be in this patch
---
drivers/usb/dwc2/hcd.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index dcfda5eb4cac..863aed20517f 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -359,8 +359,13 @@ static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
{
hsotg->vbus_supply = devm_regulator_get_optional(hsotg->dev, "vbus");
- if (IS_ERR(hsotg->vbus_supply))
+ if (PTR_ERR(hsotg->vbus_supply) == -ENODEV) {
+ hsotg->vbus_supply = NULL;
return 0;
+ } else if (IS_ERR(hsotg->vbus_supply)) {
+ hsotg->vbus_supply = NULL;
+ return PTR_ERR(hsotg->vbus_supply);
+ }

my personal cluelessnes, but can you use PTR_ERR without checking for
IS_ERR first?

It's just a cast, so it should be fine.

I would've expected something along the line of

if (IS_ERR(hsotg->vbus_supply)) {
if (PTR_ERR(hsotg->vbus_supply) == -ENODEV) {
hsotg->vbus_supply = NULL;
return 0;
} else {
return PTR_ERR(hsotg->vbus_supply);
}
}

I kind of liked to avoid one indentation level. Also wanted to play safe and NULLify the pointer in both code paths.

Thanks,

Tomeu