kirkwood: add linux 3.10 support
[openwrt/staging/yousong.git] / target / linux / kirkwood / patches-3.10 / 0050-of-add-support-for-parsing-mac-addresses-from-mtd.patch
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
5
6 Signed-off-by: Jonas Gorski <jogo@openwrt.org>
7 ---
8 drivers/of/of_net.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++-
9 1 file changed, 99 insertions(+), 1 deletion(-)
10
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
15 @@ -10,6 +10,7 @@
16 #include <linux/of_net.h>
17 #include <linux/phy.h>
18 #include <linux/export.h>
19 +#include <linux/mtd/mtd.h>
20
21 /**
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)
24 }
25 EXPORT_SYMBOL_GPL(of_get_phy_mode);
26
27 +static const void *of_get_mac_address_mtd(struct device_node *np)
28 +{
29 + const __be32 *list;
30 + int size, ret;
31 + size_t ret_len;
32 + phandle phandle;
33 + struct device_node *part_node;
34 + struct mtd_info *mtd;
35 + const char *part;
36 + u8 mac[ETH_ALEN];
37 + u64 offset;
38 + bool parse_mac = false;
39 +
40 + if (!IS_ENABLED(CONFIG_MTD))
41 + return NULL;
42 +
43 + list = of_get_property(np, "mtd-mac-address", &size);
44 +
45 + if (!list) {
46 + list = of_get_property(np, "mtd-mac-address-string", &size);
47 + parse_mac = true;
48 + }
49 +
50 + if (!list)
51 + return NULL;
52 +
53 + if (size != sizeof(*list) * 2)
54 + return NULL;
55 +
56 + phandle = be32_to_cpup(list++);
57 + if (!phandle)
58 + return NULL;
59 +
60 + part_node = of_find_node_by_phandle(phandle);
61 + if (!part_node)
62 + return NULL;
63 +
64 + offset = be32_to_cpup(list++);
65 +
66 + part = of_get_property(part_node, "label", NULL);
67 + if (!part)
68 + part = part_node->name;
69 +
70 + mtd = get_mtd_device_nm(part);
71 +
72 + of_node_put(part_node);
73 +
74 + if (IS_ERR(mtd))
75 + return NULL;
76 +
77 + if (parse_mac) {
78 + u8 mac_str[18];
79 +
80 + ret = mtd_read(mtd, offset, 18, &ret_len, (u_char *)&mac_str);
81 + if (!ret && ret_len != 18)
82 + ret = -EIO;
83 +
84 + if (!ret)
85 + ret_len = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
86 + mac, mac + 1, mac + 2, mac + 3,
87 + mac + 4, mac + 5);
88 +
89 + } else {
90 + ret = mtd_read(mtd, offset, ETH_ALEN, &ret_len, (u_char *)&mac);
91 + }
92 +
93 + put_mtd_device(mtd);
94 +
95 + if (!ret && ret_len == ETH_ALEN && is_valid_ether_addr(mac)) {
96 + struct property *pp = kzalloc(sizeof(*pp), GFP_KERNEL);
97 +
98 + if (!pp)
99 + return NULL;
100 +
101 + pp->name = kstrdup("mac-address", GFP_KERNEL);
102 + pp->value = kzalloc(ETH_ALEN, GFP_KERNEL);
103 +
104 + if (!pp->name || !pp->value) {
105 + kfree(pp->name);
106 + kfree(pp);
107 + return NULL;
108 + }
109 +
110 + memcpy(pp->value, mac, ETH_ALEN);
111 + pp->length = ETH_ALEN;
112 +
113 + if (of_find_property(np, "mac-address", NULL))
114 + of_update_property(np, pp);
115 + else
116 + of_add_property(np, pp);
117 +
118 + return pp->value;
119 + }
120 +
121 + return NULL;
122 +}
123 +
124 /**
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))
129 return pp->value;
130
131 - return NULL;
132 + return of_get_mac_address_mtd(np);
133 }
134 EXPORT_SYMBOL(of_get_mac_address);
135 --
136 1.8.4.rc1
137