1 From: Yousong Zhou <yszhou4tech@gmail.com>
2 Subject: [PATCH] ath79: add nvmem cell mac-address-ascii support
4 This is needed for devices with mac address stored in ascii format, e.g.
5 HiWiFi HC6361 to be ported in the following patch.
7 Submitted-by: Yousong Zhou <yszhou4tech@gmail.com>
9 net/ethernet/eth.c | 83 ++++++++++++------
10 1 files changed, 72 insertions(+), 11 deletions(-)
12 --- a/net/ethernet/eth.c
13 +++ b/net/ethernet/eth.c
14 @@ -545,6 +545,63 @@ int eth_platform_get_mac_address(struct
16 EXPORT_SYMBOL(eth_platform_get_mac_address);
18 +static void *nvmem_cell_get_mac_address(struct nvmem_cell *cell)
23 + mac = nvmem_cell_read(cell, &len);
25 + return PTR_ERR(mac);
26 + if (len != ETH_ALEN) {
28 + return ERR_PTR(-EINVAL);
33 +static void *nvmem_cell_get_mac_address_ascii(struct nvmem_cell *cell)
40 + mac_ascii = nvmem_cell_read(cell, &len);
41 + if (IS_ERR(mac_ascii))
42 + return PTR_ERR(mac_ascii);
43 + if (len != ETH_ALEN*2+5) {
45 + return ERR_PTR(-EINVAL);
47 + mac = kmalloc(ETH_ALEN, GFP_KERNEL);
50 + return ERR_PTR(-ENOMEM);
52 + ret = sscanf(mac_ascii, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
53 + &mac[0], &mac[1], &mac[2],
54 + &mac[3], &mac[4], &mac[5]);
56 + if (ret == ETH_ALEN)
59 + return ERR_PTR(-EINVAL);
62 +static struct nvmem_cell_mac_address_property {
64 + void *(*read)(struct nvmem_cell *);
65 +} nvmem_cell_mac_address_properties[] = {
67 + .name = "mac-address",
68 + .read = nvmem_cell_get_mac_address,
70 + .name = "mac-address-ascii",
71 + .read = nvmem_cell_get_mac_address_ascii,
76 * Obtain the MAC address from an nvmem cell named 'mac-address' associated
78 @@ -558,19 +615,23 @@ int nvmem_get_mac_address(struct device
80 struct nvmem_cell *cell;
83 + struct nvmem_cell_mac_address_property *property;
86 - cell = nvmem_cell_get(dev, "mac-address");
88 - return PTR_ERR(cell);
90 - mac = nvmem_cell_read(cell, &len);
91 - nvmem_cell_put(cell);
94 - return PTR_ERR(mac);
95 + for (i = 0; i < ARRAY_SIZE(nvmem_cell_mac_address_properties); i++) {
96 + property = &nvmem_cell_mac_address_properties[i];
97 + cell = nvmem_cell_get(dev, property->name);
99 + if (i == ARRAY_SIZE(nvmem_cell_mac_address_properties) - 1)
100 + return PTR_ERR(cell);
103 + mac = property->read(cell);
104 + nvmem_cell_put(cell);
108 - if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {
109 + if (!is_valid_ether_addr(mac)) {