Skip to content

Commit 5f510dd

Browse files
brglclaudiubeznea
authored andcommitted
net: ethernet: provide nvmem_get_mac_address()
We already have of_get_nvmem_mac_address() but some non-DT systems want to read the MAC address from NVMEM too. Implement a generalized routine that takes struct device as argument. Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a53da2d commit 5f510dd

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

include/linux/etherdevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
struct device;
3333
int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr);
3434
unsigned char *arch_get_platform_mac_address(void);
35+
int nvmem_get_mac_address(struct device *dev, void *addrbuf);
3536
u32 eth_get_headlen(void *data, unsigned int max_len);
3637
__be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
3738
extern const struct header_ops eth_header_ops;

net/ethernet/eth.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <linux/inet.h>
4848
#include <linux/ip.h>
4949
#include <linux/netdevice.h>
50+
#include <linux/nvmem-consumer.h>
5051
#include <linux/etherdevice.h>
5152
#include <linux/skbuff.h>
5253
#include <linux/errno.h>
@@ -548,3 +549,40 @@ int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)
548549
return 0;
549550
}
550551
EXPORT_SYMBOL(eth_platform_get_mac_address);
552+
553+
/**
554+
* Obtain the MAC address from an nvmem cell named 'mac-address' associated
555+
* with given device.
556+
*
557+
* @dev: Device with which the mac-address cell is associated.
558+
* @addrbuf: Buffer to which the MAC address will be copied on success.
559+
*
560+
* Returns 0 on success or a negative error number on failure.
561+
*/
562+
int nvmem_get_mac_address(struct device *dev, void *addrbuf)
563+
{
564+
struct nvmem_cell *cell;
565+
const void *mac;
566+
size_t len;
567+
568+
cell = nvmem_cell_get(dev, "mac-address");
569+
if (IS_ERR(cell))
570+
return PTR_ERR(cell);
571+
572+
mac = nvmem_cell_read(cell, &len);
573+
nvmem_cell_put(cell);
574+
575+
if (IS_ERR(mac))
576+
return PTR_ERR(mac);
577+
578+
if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {
579+
kfree(mac);
580+
return -EINVAL;
581+
}
582+
583+
ether_addr_copy(addrbuf, mac);
584+
kfree(mac);
585+
586+
return 0;
587+
}
588+
EXPORT_SYMBOL(nvmem_get_mac_address);

0 commit comments

Comments
 (0)