ramips: select kmod-rt2800-soc by default (if available)
[openwrt.git] / target / linux / ramips / patches-3.10 / 0110-NET-add-of_get_mac_address_mtd.patch
1 From 2a41724b2d0af9b4444572c4302570a3af377715 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 14 Jul 2013 23:26:15 +0200
4 Subject: [PATCH 15/33] NET: add of_get_mac_address_mtd()
5
6 Many embedded devices have information such as mac addresses stored inside mtd
7 devices. This patch allows us to add a property inside a node describing a
8 network interface. The new property points at a mtd partition with an offset
9 where the mac address can be found.
10
11 Signed-off-by: John Crispin <blogic@openwrt.org>
12 ---
13  drivers/of/of_net.c    |   37 +++++++++++++++++++++++++++++++++++++
14  include/linux/of_net.h |    1 +
15  2 files changed, 38 insertions(+)
16
17 --- a/drivers/of/of_net.c
18 +++ b/drivers/of/of_net.c
19 @@ -10,6 +10,7 @@
20  #include <linux/of_net.h>
21  #include <linux/phy.h>
22  #include <linux/export.h>
23 +#include <linux/mtd/mtd.h>
24  
25  /**
26   * It maps 'enum phy_interface_t' found in include/linux/phy.h
27 @@ -92,3 +93,39 @@ const void *of_get_mac_address(struct de
28         return NULL;
29  }
30  EXPORT_SYMBOL(of_get_mac_address);
31 +
32 +int of_get_mac_address_mtd(struct device_node *np, void *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 +
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, (u_char *) mac);
62 +       put_mtd_device(mtd);
63 +
64 +       return ret;
65 +}
66 +EXPORT_SYMBOL_GPL(of_get_mac_address_mtd);
67 --- a/include/linux/of_net.h
68 +++ b/include/linux/of_net.h
69 @@ -11,6 +11,7 @@
70  #include <linux/of.h>
71  extern const int of_get_phy_mode(struct device_node *np);
72  extern const void *of_get_mac_address(struct device_node *np);
73 +extern int of_get_mac_address_mtd(struct device_node *np, void *mac);
74  #else
75  static inline const int of_get_phy_mode(struct device_node *np)
76  {