ar71xx: fix legacy image build error
[openwrt/openwrt.git] / target / linux / generic / patches-4.1 / 681-NET-add-of_get_mac_address_mtd.patch
1 From: John Crispin <blogic@openwrt.org>
2 Date: Sun, 27 Jul 2014 09:40:01 +0100
3 Subject: NET: add of_get_mac_address_mtd()
4
5 Many embedded devices have information such as mac addresses stored inside mtd
6 devices. This patch allows us to add a property inside a node describing a
7 network interface. The new property points at a mtd partition with an offset
8 where the mac address can be found.
9
10 Signed-off-by: John Crispin <blogic@openwrt.org>
11 ---
12 drivers/of/of_net.c | 37 +++++++++++++++++++++++++++++++++++++
13 include/linux/of_net.h | 1 +
14 2 files changed, 38 insertions(+)
15
16 --- a/drivers/of/of_net.c
17 +++ b/drivers/of/of_net.c
18 @@ -10,6 +10,7 @@
19 #include <linux/of_net.h>
20 #include <linux/phy.h>
21 #include <linux/export.h>
22 +#include <linux/mtd/mtd.h>
23
24 /**
25 * of_get_phy_mode - Get phy mode for given device_node
26 @@ -80,3 +81,45 @@ const void *of_get_mac_address(struct de
27 return of_get_mac_addr(np, "address");
28 }
29 EXPORT_SYMBOL(of_get_mac_address);
30 +
31 +#ifdef CONFIG_MTD
32 +int of_get_mac_address_mtd(struct device_node *np, unsigned char *mac)
33 +{
34 + struct device_node *mtd_np = NULL;
35 + size_t retlen;
36 + int size, ret;
37 + struct mtd_info *mtd;
38 + const char *part;
39 + const __be32 *list;
40 + phandle phandle;
41 + u32 mac_inc = 0;
42 +
43 + list = of_get_property(np, "mtd-mac-address", &size);
44 + if (!list || (size != (2 * sizeof(*list))))
45 + return -ENOENT;
46 +
47 + phandle = be32_to_cpup(list++);
48 + if (phandle)
49 + mtd_np = of_find_node_by_phandle(phandle);
50 +
51 + if (!mtd_np)
52 + return -ENOENT;
53 +
54 + part = of_get_property(mtd_np, "label", NULL);
55 + if (!part)
56 + part = mtd_np->name;
57 +
58 + mtd = get_mtd_device_nm(part);
59 + if (IS_ERR(mtd))
60 + return PTR_ERR(mtd);
61 +
62 + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
63 + put_mtd_device(mtd);
64 +
65 + if (!of_property_read_u32(np, "mtd-mac-address-increment", &mac_inc))
66 + mac[5] += mac_inc;
67 +
68 + return ret;
69 +}
70 +EXPORT_SYMBOL_GPL(of_get_mac_address_mtd);
71 +#endif
72 --- a/include/linux/of_net.h
73 +++ b/include/linux/of_net.h
74 @@ -13,6 +13,14 @@
75 struct net_device;
76 extern int of_get_phy_mode(struct device_node *np);
77 extern const void *of_get_mac_address(struct device_node *np);
78 +#ifdef CONFIG_MTD
79 +extern int of_get_mac_address_mtd(struct device_node *np, unsigned char *mac);
80 +#else
81 +static inline int of_get_mac_address_mtd(struct device_node *np, unsigned char *mac)
82 +{
83 + return -ENOENT;
84 +}
85 +#endif
86 extern struct net_device *of_find_net_device_by_node(struct device_node *np);
87 #else
88 static inline int of_get_phy_mode(struct device_node *np)