Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: core: add of_find_net_device_by_node()

Add a helper function which allows getting the struct net_device pointer
associated with a given struct device_node pointer. This is useful for
instance for DSA Ethernet devices not backed by a platform_device, but a PCI
device.

Since we need to access net_class which is not accessible outside of
net/core/net-sysfs.c, this helper function is also added here and gated
with CONFIG_OF_NET.

Network devices initialized with SET_NETDEV_DEV() are also taken into
account by checking for dev->parent first and then falling back to
checking the device pointer within struct net_device.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Florian Fainelli and committed by
David S. Miller
aa836df9 3cef5c5b

+33
+8
include/linux/of_net.h
··· 9 9 10 10 #ifdef CONFIG_OF_NET 11 11 #include <linux/of.h> 12 + 13 + struct net_device; 12 14 extern int of_get_phy_mode(struct device_node *np); 13 15 extern const void *of_get_mac_address(struct device_node *np); 16 + extern struct net_device *of_find_net_device_by_node(struct device_node *np); 14 17 #else 15 18 static inline int of_get_phy_mode(struct device_node *np) 16 19 { ··· 21 18 } 22 19 23 20 static inline const void *of_get_mac_address(struct device_node *np) 21 + { 22 + return NULL; 23 + } 24 + 25 + static inline struct net_device *of_find_net_device_by_node(struct device_node *np) 24 26 { 25 27 return NULL; 26 28 }
+25
net/core/net-sysfs.c
··· 23 23 #include <linux/export.h> 24 24 #include <linux/jiffies.h> 25 25 #include <linux/pm_runtime.h> 26 + #include <linux/of.h> 26 27 27 28 #include "net-sysfs.h" 28 29 ··· 1374 1373 .ns_type = &net_ns_type_operations, 1375 1374 .namespace = net_namespace, 1376 1375 }; 1376 + 1377 + #ifdef CONFIG_OF_NET 1378 + static int of_dev_node_match(struct device *dev, const void *data) 1379 + { 1380 + int ret = 0; 1381 + 1382 + if (dev->parent) 1383 + ret = dev->parent->of_node == data; 1384 + 1385 + return ret == 0 ? dev->of_node == data : ret; 1386 + } 1387 + 1388 + struct net_device *of_find_net_device_by_node(struct device_node *np) 1389 + { 1390 + struct device *dev; 1391 + 1392 + dev = class_find_device(&net_class, NULL, np, of_dev_node_match); 1393 + if (!dev) 1394 + return NULL; 1395 + 1396 + return to_net_dev(dev); 1397 + } 1398 + EXPORT_SYMBOL(of_find_net_device_by_node); 1399 + #endif 1377 1400 1378 1401 /* Delete sysfs entries but hold kobject reference until after all 1379 1402 * netdev references are gone.