Subject: [PATCH 1/2] of: net: Add helper function of_get_ethdev_label()

From: Pali Rohár <[email-masked]>
Date: Fri, 7 Jan 2022 17:12:21 +0100

Adds a new helper function of_get_ethdev_label() which sets initial name of
specified netdev interface based on DT "label" property. It is same what is
doing DSA function dsa_port_parse_of() for DSA ports.

This helper function can be useful for drivers to make consistency between
DSA and netdev interface names.

Signed-off-by: Pali Rohár <[email-masked]>
---
 include/linux/of_net.h |  6 ++++++
 net/core/of_net.c      | 15 +++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/include/linux/of_net.h b/include/linux/of_net.h
index 0484b613ca64..532d88127b42 100644
--- a/include/linux/of_net.h
+++ b/include/linux/of_net.h
@@ -15,6 +15,7 @@ struct net_device;
 extern int of_get_phy_mode(struct device_node *np, phy_interface_t *interface);
 extern int of_get_mac_address(struct device_node *np, u8 *mac);
 int of_get_ethdev_address(struct device_node *np, struct net_device *dev);
+int of_get_ethdev_label(struct device_node *np, struct net_device *dev);
 extern struct net_device *of_find_net_device_by_node(struct device_node *np);
 #else
 static inline int of_get_phy_mode(struct device_node *np,
@@ -33,6 +34,11 @@ static inline int of_get_ethdev_address(struct device_node *np, struct net_devic
 	return -ENODEV;
 }
 
+static inline int of_get_ethdev_label(struct device_node *np, struct net_device *dev)
+{
+	return -ENODEV;
+}
+
 static inline struct net_device *of_find_net_device_by_node(struct device_node *np)
 {
 	return NULL;
diff --git a/net/core/of_net.c b/net/core/of_net.c
index f1a9bf7578e7..18fc99c42461 100644
--- a/net/core/of_net.c
+++ b/net/core/of_net.c
@@ -168,3 +168,18 @@ int of_get_ethdev_address(struct device_node *np, struct net_device *dev)
 	return ret;
 }
 EXPORT_SYMBOL(of_get_ethdev_address);
+
+int of_get_ethdev_label(struct device_node *np, struct net_device *dev)
+{
+	const char *name = of_get_property(np, "label", NULL);
+
+	if (!name)
+		return -ENOENT;
+
+	if (strlen(name) >= sizeof(dev->name))
+		return -ENAMETOOLONG;
+
+	strcpy(dev->name, name);
+	return 0;
+}
+EXPORT_SYMBOL(of_get_ethdev_label);
-- 
2.20.1