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>
This commit is contained in:
Florian Fainelli 2015-03-09 14:31:20 -07:00 committed by David S. Miller
parent 3cef5c5b0b
commit aa836df958
2 changed files with 33 additions and 0 deletions

View file

@ -9,8 +9,11 @@
#ifdef CONFIG_OF_NET
#include <linux/of.h>
struct net_device;
extern int of_get_phy_mode(struct device_node *np);
extern const void *of_get_mac_address(struct device_node *np);
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)
{
@ -21,6 +24,11 @@ static inline const void *of_get_mac_address(struct device_node *np)
{
return NULL;
}
static inline struct net_device *of_find_net_device_by_node(struct device_node *np)
{
return NULL;
}
#endif
#endif /* __LINUX_OF_NET_H */

View file

@ -23,6 +23,7 @@
#include <linux/export.h>
#include <linux/jiffies.h>
#include <linux/pm_runtime.h>
#include <linux/of.h>
#include "net-sysfs.h"
@ -1374,6 +1375,30 @@ static struct class net_class = {
.namespace = net_namespace,
};
#ifdef CONFIG_OF_NET
static int of_dev_node_match(struct device *dev, const void *data)
{
int ret = 0;
if (dev->parent)
ret = dev->parent->of_node == data;
return ret == 0 ? dev->of_node == data : ret;
}
struct net_device *of_find_net_device_by_node(struct device_node *np)
{
struct device *dev;
dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
if (!dev)
return NULL;
return to_net_dev(dev);
}
EXPORT_SYMBOL(of_find_net_device_by_node);
#endif
/* Delete sysfs entries but hold kobject reference until after all
* netdev references are gone.
*/