Skip to content

Commit c7f5d10

Browse files
committed
net: Add eth_platform_get_mac_address() helper.
A repeating pattern in drivers has become to use OF node information and, if not found, platform specific host information to extract the ethernet address for a given device. Currently this is done with a call to of_get_mac_address() and then some ifdef'd stuff for SPARC. Consolidate this into a portable routine, and provide the arch_get_platform_mac_address() weak function hook for all architectures to implement if they want. Signed-off-by: David S. Miller <[email protected]>
1 parent cdba756 commit c7f5d10

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

arch/sparc/kernel/idprom.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/types.h>
1010
#include <linux/init.h>
1111
#include <linux/export.h>
12+
#include <linux/etherdevice.h>
1213

1314
#include <asm/oplib.h>
1415
#include <asm/idprom.h>
@@ -60,6 +61,12 @@ static void __init display_system_type(unsigned char machtype)
6061
{
6162
}
6263
#endif
64+
65+
unsigned char *arch_get_platform_mac_address(void)
66+
{
67+
return idprom->id_ethaddr;
68+
}
69+
6370
/* Calculate the IDPROM checksum (xor of the data bytes). */
6471
static unsigned char __init calc_idprom_cksum(struct idprom *idprom)
6572
{

include/linux/etherdevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#include <asm/bitsperlong.h>
3030

3131
#ifdef __KERNEL__
32+
struct device;
33+
int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr);
34+
unsigned char *arch_get_platform_get_mac_address(void);
3235
u32 eth_get_headlen(void *data, unsigned int max_len);
3336
__be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
3437
extern const struct header_ops eth_header_ops;

net/ethernet/eth.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#include <linux/errno.h>
5353
#include <linux/init.h>
5454
#include <linux/if_ether.h>
55+
#include <linux/of_net.h>
56+
#include <linux/pci.h>
5557
#include <net/dst.h>
5658
#include <net/arp.h>
5759
#include <net/sock.h>
@@ -485,3 +487,32 @@ static int __init eth_offload_init(void)
485487
}
486488

487489
fs_initcall(eth_offload_init);
490+
491+
unsigned char * __weak arch_get_platform_mac_address(void)
492+
{
493+
return NULL;
494+
}
495+
496+
int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)
497+
{
498+
const unsigned char *addr;
499+
struct device_node *dp;
500+
501+
if (dev_is_pci(dev))
502+
dp = pci_device_to_OF_node(to_pci_dev(dev));
503+
else
504+
dp = dev->of_node;
505+
506+
addr = NULL;
507+
if (dp)
508+
addr = of_get_mac_address(dp);
509+
if (!addr)
510+
addr = arch_get_platform_mac_address();
511+
512+
if (!addr)
513+
return -ENODEV;
514+
515+
ether_addr_copy(mac_addr, addr);
516+
return 0;
517+
}
518+
EXPORT_SYMBOL(eth_platform_get_mac_address);

0 commit comments

Comments
 (0)