1 From f7d3f7790b75b098e1c92530dc3d28ba3b40b5a4 Mon Sep 17 00:00:00 2001
2 From: Jonas Gorski <jogo@openwrt.org>
3 Date: Sat, 10 Aug 2013 12:48:51 +0200
4 Subject: [PATCH 28/29] of: add support for parsing mac addresses from mtd
6 Signed-off-by: Jonas Gorski <jogo@openwrt.org>
8 drivers/of/of_net.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++-
9 1 file changed, 99 insertions(+), 1 deletion(-)
11 diff --git a/drivers/of/of_net.c b/drivers/of/of_net.c
12 index ffab033..8b40ac6 100644
13 --- a/drivers/of/of_net.c
14 +++ b/drivers/of/of_net.c
16 #include <linux/of_net.h>
17 #include <linux/phy.h>
18 #include <linux/export.h>
19 +#include <linux/mtd/mtd.h>
22 * It maps 'enum phy_interface_t' found in include/linux/phy.h
23 @@ -55,6 +56,103 @@ const int of_get_phy_mode(struct device_node *np)
25 EXPORT_SYMBOL_GPL(of_get_phy_mode);
27 +static const void *of_get_mac_address_mtd(struct device_node *np)
33 + struct device_node *part_node;
34 + struct mtd_info *mtd;
38 + bool parse_mac = false;
40 + if (!IS_ENABLED(CONFIG_MTD))
43 + list = of_get_property(np, "mtd-mac-address", &size);
46 + list = of_get_property(np, "mtd-mac-address-string", &size);
53 + if (size != sizeof(*list) * 2)
56 + phandle = be32_to_cpup(list++);
60 + part_node = of_find_node_by_phandle(phandle);
64 + offset = be32_to_cpup(list++);
66 + part = of_get_property(part_node, "label", NULL);
68 + part = part_node->name;
70 + mtd = get_mtd_device_nm(part);
72 + of_node_put(part_node);
80 + ret = mtd_read(mtd, offset, 18, &ret_len, (u_char *)&mac_str);
81 + if (!ret && ret_len != 18)
85 + ret_len = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
86 + mac, mac + 1, mac + 2, mac + 3,
90 + ret = mtd_read(mtd, offset, ETH_ALEN, &ret_len, (u_char *)&mac);
93 + put_mtd_device(mtd);
95 + if (!ret && ret_len == ETH_ALEN && is_valid_ether_addr(mac)) {
96 + struct property *pp = kzalloc(sizeof(*pp), GFP_KERNEL);
101 + pp->name = kstrdup("mac-address", GFP_KERNEL);
102 + pp->value = kzalloc(ETH_ALEN, GFP_KERNEL);
104 + if (!pp->name || !pp->value) {
110 + memcpy(pp->value, mac, ETH_ALEN);
111 + pp->length = ETH_ALEN;
113 + if (of_find_property(np, "mac-address", NULL))
114 + of_update_property(np, pp);
116 + of_add_property(np, pp);
125 * Search the device tree for the best MAC address to use. 'mac-address' is
126 * checked first, because that is supposed to contain to "most recent" MAC
127 @@ -89,6 +187,6 @@ const void *of_get_mac_address(struct device_node *np)
128 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
132 + return of_get_mac_address_mtd(np);
134 EXPORT_SYMBOL(of_get_mac_address);