diff mbox series

[05/12] backports: Add device_get_mac_address()

Message ID 20181202115226.3594-6-hauke@hauke-m.de (mailing list archive)
State Accepted
Headers show
Series backports: Multiple fixes for kernel 4.20 | expand

Commit Message

Hauke Mehrtens Dec. 2, 2018, 11:52 a.m. UTC
This was added in commit 4c96b7dc0d39 ("Add a matching set of device_
functions for determining mac/phy") and is used by ath10k now. On kernel
< 3.18 call of_property_read_u8_array() only because the device_
function was only added in kernel 3.18.

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
---
 backport/backport-include/linux/property.h | 15 ++++++++
 backport/compat/backport-4.3.c             | 57 ++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
 create mode 100644 backport/backport-include/linux/property.h
diff mbox series

Patch

diff --git a/backport/backport-include/linux/property.h b/backport/backport-include/linux/property.h
new file mode 100644
index 00000000..a0d7bed1
--- /dev/null
+++ b/backport/backport-include/linux/property.h
@@ -0,0 +1,15 @@ 
+#ifndef __BACKPORT_LINUX_PROPERTY_H_
+#define __BACKPORT_LINUX_PROPERTY_H_
+#include <linux/version.h>
+#if LINUX_VERSION_IS_GEQ(3,18,17)
+#include_next <linux/property.h>
+#endif
+
+#if LINUX_VERSION_IS_LESS(4,3,0)
+
+#define device_get_mac_address LINUX_BACKPORT(device_get_mac_address)
+void *device_get_mac_address(struct device *dev, char *addr, int alen);
+
+#endif /* < 4.3 */
+
+#endif /* __BACKPORT_LINUX_PROPERTY_H_ */
diff --git a/backport/compat/backport-4.3.c b/backport/compat/backport-4.3.c
index 88be5720..04698ad3 100644
--- a/backport/compat/backport-4.3.c
+++ b/backport/compat/backport-4.3.c
@@ -15,6 +15,9 @@ 
 #include <linux/printk.h>
 #include <linux/thermal.h>
 #include <linux/slab.h>
+#include <linux/property.h>
+#include <linux/etherdevice.h>
+#include <linux/of.h>
 
 #ifdef CONFIG_THERMAL
 #if LINUX_VERSION_IS_GEQ(3,8,0)
@@ -245,3 +248,57 @@  ssize_t strscpy(char *dest, const char *src, size_t count)
 	return -E2BIG;
 }
 EXPORT_SYMBOL_GPL(strscpy);
+
+static void *device_get_mac_addr(struct device *dev,
+				 const char *name, char *addr,
+				 int alen)
+{
+#if LINUX_VERSION_IS_GEQ(3,18,0)
+	int ret = device_property_read_u8_array(dev, name, addr, alen);
+#else
+	int ret = of_property_read_u8_array(dev->of_node, name, addr, alen);
+#endif
+
+	if (ret == 0 && alen == ETH_ALEN && is_valid_ether_addr(addr))
+		return addr;
+	return NULL;
+}
+
+/**
+ * device_get_mac_address - Get the MAC for a given device
+ * @dev:	Pointer to the device
+ * @addr:	Address of buffer to store the MAC in
+ * @alen:	Length of the buffer pointed to by addr, should be ETH_ALEN
+ *
+ * Search the firmware node for the best MAC address to use.  'mac-address' is
+ * checked first, because that is supposed to contain to "most recent" MAC
+ * address. If that isn't set, then 'local-mac-address' is checked next,
+ * because that is the default address.  If that isn't set, then the obsolete
+ * 'address' is checked, just in case we're using an old device tree.
+ *
+ * Note that the 'address' property is supposed to contain a virtual address of
+ * the register set, but some DTS files have redefined that property to be the
+ * MAC address.
+ *
+ * All-zero MAC addresses are rejected, because those could be properties that
+ * exist in the firmware tables, but were not updated by the firmware.  For
+ * example, the DTS could define 'mac-address' and 'local-mac-address', with
+ * zero MAC addresses.  Some older U-Boots only initialized 'local-mac-address'.
+ * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
+ * exists but is all zeros.
+*/
+void *device_get_mac_address(struct device *dev, char *addr, int alen)
+{
+	char *res;
+
+	res = device_get_mac_addr(dev, "mac-address", addr, alen);
+	if (res)
+		return res;
+
+	res = device_get_mac_addr(dev, "local-mac-address", addr, alen);
+	if (res)
+		return res;
+
+	return device_get_mac_addr(dev, "address", addr, alen);
+}
+EXPORT_SYMBOL_GPL(device_get_mac_address);