f956c587378ef391610098eb20173c8dd3ecf2d9
[openwrt.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 --- a/drivers/of/of_net.c
12 +++ b/drivers/of/of_net.c
13 @@ -10,6 +10,7 @@
14  #include <linux/of_net.h>
15  #include <linux/phy.h>
16  #include <linux/export.h>
17 +#include <linux/mtd/mtd.h>
18  
19  /**
20   * It maps 'enum phy_interface_t' found in include/linux/phy.h
21 @@ -55,6 +56,103 @@ const int of_get_phy_mode(struct device_
22  }
23  EXPORT_SYMBOL_GPL(of_get_phy_mode);
24  
25 +static const void *of_get_mac_address_mtd(struct device_node *np)
26 +{
27 +       const __be32 *list;
28 +       int size, ret;
29 +       size_t ret_len;
30 +       phandle phandle;
31 +       struct device_node *part_node;
32 +       struct mtd_info *mtd;
33 +       const char *part;
34 +       u8 mac[ETH_ALEN];
35 +       u64 offset;
36 +       bool parse_mac = false;
37 +
38 +       if (!IS_ENABLED(CONFIG_MTD))
39 +               return NULL;
40 +
41 +       list = of_get_property(np, "mtd-mac-address", &size);
42 +
43 +       if (!list) {
44 +               list = of_get_property(np, "mtd-mac-address-string", &size);
45 +               parse_mac = true;
46 +       }
47 +
48 +       if (!list)
49 +               return NULL;
50 +
51 +       if (size != sizeof(*list) * 2)
52 +               return NULL;
53 +
54 +       phandle = be32_to_cpup(list++);
55 +       if (!phandle)
56 +               return NULL;
57 +
58 +       part_node = of_find_node_by_phandle(phandle);
59 +       if (!part_node)
60 +               return NULL;
61 +
62 +       offset = be32_to_cpup(list++);
63 +
64 +       part = of_get_property(part_node, "label", NULL);
65 +       if (!part)
66 +               part = part_node->name;
67 +
68 +       mtd = get_mtd_device_nm(part);
69 +
70 +       of_node_put(part_node);
71 +
72 +       if (IS_ERR(mtd))
73 +               return NULL;
74 +
75 +       if (parse_mac) {
76 +               u8 mac_str[18];
77 +
78 +               ret = mtd_read(mtd, offset, 18, &ret_len, (u_char *)&mac_str);
79 +               if (!ret && ret_len != 18)
80 +                       ret = -EIO;
81 +
82 +               if (!ret)
83 +                       ret_len = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
84 +                                        mac, mac + 1, mac + 2, mac + 3,
85 +                                        mac + 4, mac + 5);
86 +
87 +       } else {
88 +               ret = mtd_read(mtd, offset, ETH_ALEN, &ret_len, (u_char *)&mac);
89 +       }
90 +
91 +       put_mtd_device(mtd);
92 +
93 +       if (!ret && ret_len == ETH_ALEN && is_valid_ether_addr(mac)) {
94 +               struct property *pp = kzalloc(sizeof(*pp), GFP_KERNEL);
95 +
96 +               if (!pp)
97 +                       return NULL;
98 +
99 +               pp->name = kstrdup("mac-address", GFP_KERNEL);
100 +               pp->value = kzalloc(ETH_ALEN, GFP_KERNEL);
101 +
102 +               if (!pp->name || !pp->value) {
103 +                       kfree(pp->name);
104 +                       kfree(pp);
105 +                       return NULL;
106 +               }
107 +
108 +               memcpy(pp->value, mac, ETH_ALEN);
109 +               pp->length = ETH_ALEN;
110 +
111 +               if (of_find_property(np, "mac-address", NULL))
112 +                       of_update_property(np, pp);
113 +               else
114 +                       of_add_property(np, pp);
115 +
116 +               return pp->value;
117 +       }
118 +
119 +       return NULL;
120 +}
121 +
122  /**
123   * Search the device tree for the best MAC address to use.  'mac-address' is
124   * checked first, because that is supposed to contain to "most recent" MAC
125 @@ -89,6 +187,6 @@ const void *of_get_mac_address(struct de
126         if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
127                 return pp->value;
128  
129 -       return NULL;
130 +       return of_get_mac_address_mtd(np);
131  }
132  EXPORT_SYMBOL(of_get_mac_address);