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 --- a/drivers/of/of_net.c
12 +++ b/drivers/of/of_net.c
14 #include <linux/of_net.h>
15 #include <linux/phy.h>
16 #include <linux/export.h>
17 +#include <linux/mtd/mtd.h>
20 * It maps 'enum phy_interface_t' found in include/linux/phy.h
21 @@ -55,6 +56,103 @@ const int of_get_phy_mode(struct device_
23 EXPORT_SYMBOL_GPL(of_get_phy_mode);
25 +static const void *of_get_mac_address_mtd(struct device_node *np)
31 + struct device_node *part_node;
32 + struct mtd_info *mtd;
36 + bool parse_mac = false;
38 + if (!IS_ENABLED(CONFIG_MTD))
41 + list = of_get_property(np, "mtd-mac-address", &size);
44 + list = of_get_property(np, "mtd-mac-address-string", &size);
51 + if (size != sizeof(*list) * 2)
54 + phandle = be32_to_cpup(list++);
58 + part_node = of_find_node_by_phandle(phandle);
62 + offset = be32_to_cpup(list++);
64 + part = of_get_property(part_node, "label", NULL);
66 + part = part_node->name;
68 + mtd = get_mtd_device_nm(part);
70 + of_node_put(part_node);
78 + ret = mtd_read(mtd, offset, 18, &ret_len, (u_char *)&mac_str);
79 + if (!ret && ret_len != 18)
83 + ret_len = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
84 + mac, mac + 1, mac + 2, mac + 3,
88 + ret = mtd_read(mtd, offset, ETH_ALEN, &ret_len, (u_char *)&mac);
91 + put_mtd_device(mtd);
93 + if (!ret && ret_len == ETH_ALEN && is_valid_ether_addr(mac)) {
94 + struct property *pp = kzalloc(sizeof(*pp), GFP_KERNEL);
99 + pp->name = kstrdup("mac-address", GFP_KERNEL);
100 + pp->value = kzalloc(ETH_ALEN, GFP_KERNEL);
102 + if (!pp->name || !pp->value) {
108 + memcpy(pp->value, mac, ETH_ALEN);
109 + pp->length = ETH_ALEN;
111 + if (of_find_property(np, "mac-address", NULL))
112 + of_update_property(np, pp);
114 + of_add_property(np, pp);
123 * Search the device tree for the best MAC address to use. 'mac-address' is
124 * checked first, because that is supposed to contain to "most recent" MAC
125 @@ -89,6 +187,6 @@ const void *of_get_mac_address(struct de
126 if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
130 + return of_get_mac_address_mtd(np);
132 EXPORT_SYMBOL(of_get_mac_address);