kernel: bump 5.4 to 5.4.133
[openwrt/openwrt.git] / target / linux / generic / pending-5.4 / 682-of_net-add-mac-address-increment-support.patch
1 From 639dba857aa554f2a78572adc4cf3c32de9ec2e2 Mon Sep 17 00:00:00 2001
2 From: Ansuel Smith <ansuelsmth@gmail.com>
3 Date: Tue, 30 Mar 2021 18:21:14 +0200
4 Subject: [PATCH 2/2] of_net: add mac-address-increment support
5
6 Lots of embedded devices use the mac-address of other interface
7 extracted from nvmem cells and increments it by one or two. Add two
8 bindings to integrate this and directly use the right mac-address for
9 the interface. Some example are some routers that use the gmac
10 mac-address stored in the art partition and increments it by one for the
11 wifi. mac-address-increment-byte bindings is used to tell what byte of
12 the mac-address has to be increased (if not defined the last byte is
13 increased) and mac-address-increment tells how much the byte decided
14 early has to be increased.
15
16 Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
17 ---
18 drivers/of/of_net.c | 59 ++++++++++++++++++++++++++++++++++-----------
19 1 file changed, 45 insertions(+), 14 deletions(-)
20
21 --- a/drivers/of/of_net.c
22 +++ b/drivers/of/of_net.c
23 @@ -49,31 +49,36 @@ static void *of_get_mac_addr(struct devi
24 return NULL;
25 }
26
27 -static const void *of_get_mac_addr_nvmem(struct device_node *np)
28 +static void *of_get_mac_addr_nvmem(struct device_node *np, int *err)
29 {
30 int ret;
31 - const void *mac;
32 + void *mac;
33 u8 nvmem_mac[ETH_ALEN];
34 struct platform_device *pdev = of_find_device_by_node(np);
35
36 - if (!pdev)
37 - return ERR_PTR(-ENODEV);
38 + if (!pdev) {
39 + *err = -ENODEV;
40 + return NULL;
41 + }
42
43 ret = nvmem_get_mac_address(&pdev->dev, &nvmem_mac);
44 if (ret) {
45 put_device(&pdev->dev);
46 - return ERR_PTR(ret);
47 + *err = ret;
48 + return NULL;
49 }
50
51 mac = devm_kmemdup(&pdev->dev, nvmem_mac, ETH_ALEN, GFP_KERNEL);
52 put_device(&pdev->dev);
53 - if (!mac)
54 - return ERR_PTR(-ENOMEM);
55 + if (!mac) {
56 + *err = -ENOMEM;
57 + return NULL;
58 + }
59
60 return mac;
61 }
62
63 -static const void *of_get_mac_address_mtd(struct device_node *np)
64 +static void *of_get_mac_address_mtd(struct device_node *np)
65 {
66 #ifdef CONFIG_MTD
67 struct device_node *mtd_np = NULL;
68 @@ -161,28 +166,54 @@ free:
69 * If a mtd-mac-address property exists, try to fetch the MAC address from the
70 * specified mtd device, and store it as a 'mac-address' property
71 *
72 + * DT can tell the system to increment the mac-address after is extracted by
73 + * using:
74 + * - mac-address-increment-byte to decide what byte to increase
75 + * (if not defined is increased the last byte)
76 + * - mac-address-increment to decide how much to increase. The value will
77 + * not overflow to other bytes if the increment is over 255.
78 + * (example 00:01:02:03:04:ff + 1 == 00:01:02:03:04:00)
79 + *
80 * Return: Will be a valid pointer on success and ERR_PTR in case of error.
81 */
82 const void *of_get_mac_address(struct device_node *np)
83 {
84 - const void *addr;
85 + u32 inc_idx, mac_inc;
86 + int ret = 0;
87 + u8 *addr;
88 +
89 + /* Check first if the increment byte is present and valid.
90 + * If not set assume to increment the last byte if found.
91 + */
92 + if (of_property_read_u32(np, "mac-address-increment-byte", &inc_idx))
93 + inc_idx = 5;
94 + if (inc_idx < 3 || inc_idx > 5)
95 + return ERR_PTR(-EINVAL);
96
97 addr = of_get_mac_addr(np, "mac-address");
98 if (addr)
99 - return addr;
100 + goto found;
101
102 addr = of_get_mac_addr(np, "local-mac-address");
103 if (addr)
104 - return addr;
105 + goto found;
106
107 addr = of_get_mac_addr(np, "address");
108 if (addr)
109 - return addr;
110 + goto found;
111
112 addr = of_get_mac_address_mtd(np);
113 if (addr)
114 - return addr;
115 + goto found;
116 +
117 + addr = of_get_mac_addr_nvmem(np, &ret);
118 + if (ret)
119 + return ERR_PTR(ret);
120 +
121 +found:
122 + if (!of_property_read_u32(np, "mac-address-increment", &mac_inc))
123 + addr[inc_idx] += mac_inc;
124
125 - return of_get_mac_addr_nvmem(np);
126 + return addr;
127 }
128 EXPORT_SYMBOL(of_get_mac_address);