[OpenWrt-Devel] [PATCH 2/5] net: ethernet: mediatek: use devm_mdiobus_alloc instead of mdiobus_alloc inside mtk_mdio_init

Rosen Penev rosenp at gmail.com
Wed Jul 4 19:20:05 EDT 2018


From: Sean Wang <sean.wang at mediatek.com>

a lot of parts in the driver uses devm_* APIs to gain benefits from the
device resource management, so devm_mdiobus_alloc is also used instead
of mdiobus_alloc to have more elegant code flow.

Using common code provided by the devm_* helps to
1) have simplified the code flow as [1] says
2) decrease the risk of incorrect error handling by human
3) only a few drivers used it since it was proposed on linux 3.16,
so just hope to promote for this.

Ref:
[1] https://patchwork.ozlabs.org/patch/344093/

Signed-off-by: Sean Wang <sean.wang at mediatek.com>
Reviewed-by: Andrew Lunn <andrew at lunn.ch>
Signed-off-by: David S. Miller <davem at davemloft.net>
---
 .../drivers/net/ethernet/mediatek/mdio.c      | 21 ++++++-------------
 1 file changed, 6 insertions(+), 15 deletions(-)

diff --git a/target/linux/ramips/files-4.14/drivers/net/ethernet/mediatek/mdio.c b/target/linux/ramips/files-4.14/drivers/net/ethernet/mediatek/mdio.c
index bdfdf7a432..06f217af10 100644
--- a/target/linux/ramips/files-4.14/drivers/net/ethernet/mediatek/mdio.c
+++ b/target/linux/ramips/files-4.14/drivers/net/ethernet/mediatek/mdio.c
@@ -202,7 +202,7 @@ static struct fe_phy phy_ralink = {
 int fe_mdio_init(struct fe_priv *priv)
 {
 	struct device_node *mii_np;
-	int err;
+	int ret;
 
 	if (!priv->soc->mdio_read || !priv->soc->mdio_write)
 		return 0;
@@ -217,13 +217,13 @@ int fe_mdio_init(struct fe_priv *priv)
 	}
 
 	if (!of_device_is_available(mii_np)) {
-		err = 0;
+		ret = 0;
 		goto err_put_node;
 	}
 
-	priv->mii_bus = mdiobus_alloc();
+	priv->mii_bus = devm_mdiobus_alloc(priv->dev);
 	if (!priv->mii_bus) {
-		err = -ENOMEM;
+		ret = -ENOMEM;
 		goto err_put_node;
 	}
 
@@ -235,18 +235,11 @@ int fe_mdio_init(struct fe_priv *priv)
 	priv->mii_bus->parent = priv->dev;
 
 	snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
-	err = of_mdiobus_register(priv->mii_bus, mii_np);
-	if (err)
-		goto err_free_bus;
+	ret = of_mdiobus_register(priv->mii_bus, mii_np);
 
-	return 0;
-
-err_free_bus:
-	kfree(priv->mii_bus);
 err_put_node:
 	of_node_put(mii_np);
-	priv->mii_bus = NULL;
-	return err;
+	return ret;
 }
 
 void fe_mdio_cleanup(struct fe_priv *priv)
@@ -255,6 +248,4 @@ void fe_mdio_cleanup(struct fe_priv *priv)
 		return;
 
 	mdiobus_unregister(priv->mii_bus);
-	of_node_put(priv->mii_bus->dev.of_node);
-	kfree(priv->mii_bus);
 }
-- 
2.17.1


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list