diff options
author | Hauke Mehrtens <hauke@hauke-m.de> | 2018-12-01 18:13:13 +0100 |
---|---|---|
committer | Hauke Mehrtens <hauke@hauke-m.de> | 2018-12-06 22:54:27 +0100 |
commit | 43cd654d47939da85a6f280f4a4124b67e80c648 (patch) | |
tree | 064a04cfd464e75fedbdc127cc701d33291ed50e /backport | |
parent | c27e2f124d0186cc6e943bd09fe2f68a2d1276e0 (diff) |
backports: Add device_get_mac_address()
This was added in upstream commit 4c96b7dc0d39 ("Add a matching set of
device_ functions for determining mac/phy") and is used by ath10k now.
In the mainline kernel it uses device_property_read_u8_array(), but that
was only introduced in kernel 3.18, use of_property_read_u8_array() only
on all kernel versions where this backport is needed which was added
much earlier.
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
Diffstat (limited to 'backport')
-rw-r--r-- | backport/backport-include/linux/property.h | 15 | ||||
-rw-r--r-- | backport/compat/backport-4.3.c | 57 |
2 files changed, 72 insertions, 0 deletions
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); |