Move usb_phy_generic_register() function call to the top, to simplify
error handling.

Replace kzalloc() with devm_kzalloc().

After platform_device_add(), if we error out, we must do
platform_device_unregister(), which also does the put. So lets move
devm_kzalloc() to simplify error handling and avoid
calling of platform_device_unregister().

Reviewed-by: Wei Yongjun <weiyongj...@huawei.com>
Signed-off-by: Vardan Mikayelyan <mvar...@synopsys.com>
---
Changes in v2:
 - Remove kfree(glue) as suggested Janusz Dziedzic.

 drivers/usb/dwc2/pci.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)

diff --git a/drivers/usb/dwc2/pci.c b/drivers/usb/dwc2/pci.c
index fdeb8c7..70b0b9c 100644
--- a/drivers/usb/dwc2/pci.c
+++ b/drivers/usb/dwc2/pci.c
@@ -82,7 +82,6 @@ static void dwc2_pci_remove(struct pci_dev *pci)
 
        platform_device_unregister(glue->dwc2);
        usb_phy_generic_unregister(glue->phy);
-       kfree(glue);
        pci_set_drvdata(pci, NULL);
 }
 
@@ -104,10 +103,17 @@ static int dwc2_pci_probe(struct pci_dev *pci,
 
        pci_set_master(pci);
 
+       phy = usb_phy_generic_register();
+       if (IS_ERR(phy)) {
+               dev_err(dev, "error registering generic PHY (%ld)\n",
+                       PTR_ERR(phy));
+               return PTR_ERR(phy);
+       }
+
        dwc2 = platform_device_alloc("dwc2", PLATFORM_DEVID_AUTO);
        if (!dwc2) {
                dev_err(dev, "couldn't allocate dwc2 device\n");
-               return -ENOMEM;
+               goto err;
        }
 
        memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
@@ -124,32 +130,25 @@ static int dwc2_pci_probe(struct pci_dev *pci,
        ret = platform_device_add_resources(dwc2, res, ARRAY_SIZE(res));
        if (ret) {
                dev_err(dev, "couldn't add resources to dwc2 device\n");
-               return ret;
+               goto err;
        }
 
        dwc2->dev.parent = dev;
 
-       phy = usb_phy_generic_register();
-       if (IS_ERR(phy)) {
-               dev_err(dev, "error registering generic PHY (%ld)\n",
-                       PTR_ERR(phy));
-               return PTR_ERR(phy);
-       }
-
        ret = dwc2_pci_quirks(pci, dwc2);
        if (ret)
                goto err;
 
+       glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
+       if (!glue)
+               goto err;
+
        ret = platform_device_add(dwc2);
        if (ret) {
                dev_err(dev, "failed to register dwc2 device\n");
                goto err;
        }
 
-       glue = kzalloc(sizeof(*glue), GFP_KERNEL);
-       if (!glue)
-               return -ENOMEM;
-
        glue->phy = phy;
        glue->dwc2 = dwc2;
        pci_set_drvdata(pci, glue);
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to