fe3f04ed379a1c7bd7b0b2aed06533372c9e2b5d
[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,39 @@ 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 +int of_get_mac_address_mtd(struct device_node *np, void *mac)
32 +{
33 + struct device_node *mtd_np = NULL;
34 + size_t retlen;
35 + int size, ret;
36 + struct mtd_info *mtd;
37 + const char *part;
38 + const __be32 *list;
39 + phandle phandle;
40 +
41 + list = of_get_property(np, "mtd-mac-address", &size);
42 + if (!list || (size != (2 * sizeof(*list))))
43 + return -ENOENT;
44 +
45 + phandle = be32_to_cpup(list++);
46 + if (phandle)
47 + mtd_np = of_find_node_by_phandle(phandle);
48 +
49 + if (!mtd_np)
50 + return -ENOENT;
51 +
52 + part = of_get_property(mtd_np, "label", NULL);
53 + if (!part)
54 + part = mtd_np->name;
55 +
56 + mtd = get_mtd_device_nm(part);
57 + if (IS_ERR(mtd))
58 + return PTR_ERR(mtd);
59 +
60 + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, (u_char *) mac);
61 + put_mtd_device(mtd);
62 +
63 + return ret;
64 +}
65 +EXPORT_SYMBOL_GPL(of_get_mac_address_mtd);
66 --- a/include/linux/of_net.h
67 +++ b/include/linux/of_net.h
68 @@ -13,6 +13,7 @@
69 struct net_device;
70 extern int of_get_phy_mode(struct device_node *np);
71 extern const void *of_get_mac_address(struct device_node *np);
72 +extern int of_get_mac_address_mtd(struct device_node *np, void *mac);
73 extern struct net_device *of_find_net_device_by_node(struct device_node *np);
74 #else
75 static inline int of_get_phy_mode(struct device_node *np)