generic: Allow configuring an increment with mtd-mac-address
[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,43 @@ 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, unsigned char *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 + u32 mac_inc = 0;
41 +
42 + list = of_get_property(np, "mtd-mac-address", &size);
43 + if (!list || (size != (2 * sizeof(*list))))
44 + return -ENOENT;
45 +
46 + phandle = be32_to_cpup(list++);
47 + if (phandle)
48 + mtd_np = of_find_node_by_phandle(phandle);
49 +
50 + if (!mtd_np)
51 + return -ENOENT;
52 +
53 + part = of_get_property(mtd_np, "label", NULL);
54 + if (!part)
55 + part = mtd_np->name;
56 +
57 + mtd = get_mtd_device_nm(part);
58 + if (IS_ERR(mtd))
59 + return PTR_ERR(mtd);
60 +
61 + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
62 + put_mtd_device(mtd);
63 +
64 + if (!of_property_read_u32(np, "mtd-mac-address-increment", &mac_inc))
65 + mac[5] += mac_inc;
66 +
67 + return ret;
68 +}
69 +EXPORT_SYMBOL_GPL(of_get_mac_address_mtd);
70 --- a/include/linux/of_net.h
71 +++ b/include/linux/of_net.h
72 @@ -13,6 +13,7 @@
73 struct net_device;
74 extern int of_get_phy_mode(struct device_node *np);
75 extern const void *of_get_mac_address(struct device_node *np);
76 +extern int of_get_mac_address_mtd(struct device_node *np, unsigned char *mac);
77 extern struct net_device *of_find_net_device_by_node(struct device_node *np);
78 #else
79 static inline int of_get_phy_mode(struct device_node *np)