backports.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hauke Mehrtens <hauke@hauke-m.de>
To: johannes@sipsolutions.net
Cc: backports@vger.kernel.org, Hauke Mehrtens <hauke@hauke-m.de>
Subject: [PATCH 05/12] backports: Add device_get_mac_address()
Date: Sun,  2 Dec 2018 12:52:19 +0100	[thread overview]
Message-ID: <20181202115226.3594-6-hauke@hauke-m.de> (raw)
In-Reply-To: <20181202115226.3594-1-hauke@hauke-m.de>

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 --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);
-- 
2.11.0

--
To unsubscribe from this list: send the line "unsubscribe backports" in

  parent reply	other threads:[~2018-12-02 11:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-02 11:52 [PATCH 00/12] backports: Multiple fixes for kernel 4.20 Hauke Mehrtens
2018-12-02 11:52 ` [PATCH 01/12] backports: Refresh patches on kernel 4.20-rc4 Hauke Mehrtens
2018-12-02 11:52 ` [PATCH 02/12] backports: Add page_ref_count() and page_ref_sub_and_test() Hauke Mehrtens
2018-12-02 11:52 ` [PATCH 03/12] backports: Add page_frag_alloc() and __page_frag_cache_drain() Hauke Mehrtens
2018-12-02 11:52 ` [PATCH 04/12] backports: Add missing include for mt76 Hauke Mehrtens
2018-12-02 11:52 ` Hauke Mehrtens [this message]
2018-12-02 11:52 ` [PATCH 06/12] backports: Remove BPAUTO_CRYPTO_SKCIPHER Hauke Mehrtens
2018-12-02 11:52 ` [PATCH 07/12] backports: Fix pci_alloc_irq_vectors() backport Hauke Mehrtens
2018-12-02 11:52 ` [PATCH 08/12] backports: Revert "dependencies: Make WIL6210 depend on kernel >= 4.8" Hauke Mehrtens
2018-12-02 11:52 ` [PATCH 09/12] backports: Make TI WLCORE depend on kernel >= 4.2 Hauke Mehrtens
2018-12-02 11:52 ` [PATCH 10/12] backports: Remove unused code from backport-public_key.h Hauke Mehrtens
2018-12-02 11:52 ` [PATCH 11/12] backports: Remove unused default configurations Hauke Mehrtens
2018-12-02 11:52 ` [PATCH 12/12] backports: Update wifi default configuration Hauke Mehrtens
2018-12-07 20:01 ` [PATCH 00/12] backports: Multiple fixes for kernel 4.20 Hauke Mehrtens

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181202115226.3594-6-hauke@hauke-m.de \
    --to=hauke@hauke-m.de \
    --cc=backports@vger.kernel.org \
    --cc=johannes@sipsolutions.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).