[PATCH v3 2/2] device property: Add device_get_bd_address() and fwnode_get_bd_address()

From: Matthias Kaehlcke
Date: Tue Sep 25 2018 - 14:54:09 EST


Provide an API for Bluetooth drivers to retrieve the Bluetooth Device
address (BD_ADDR) for a device. If the device node has a property
'local-bd-address' the BD address is read from this property.

The definition of bdaddr_t is moved to types.h to make it visible in
property.h without having to include (the mostly unrelated) bluetooth.h

Signed-off-by: Matthias Kaehlcke <mka@xxxxxxxxxxxx>
Reviewed-by: Andy Shevchenko <andy.shevchenko@xxxxxxxxx>
---
Changes in v3:
- move definition of bdaddr_t to types.h to avoid include of
bluetooth.h from property.h
- add stubs for the new functions

Changes in v2:
- use bdaddr_t instead of byte pointer + len
- use EXPORT_SYMBOL_GPL for the new functions instead of EXPORT_SYMBOL
- put new functions inside #if IS_ENABLED(CONFIG_BT)
- some new line juggling in property.h
- added 'Reviewed-by: Andy Shevchenko <andy.shevchenko@xxxxxxxxx>' tag
---
drivers/base/property.c | 46 +++++++++++++++++++++++++++++++
include/linux/property.h | 18 ++++++++++++
include/linux/types.h | 5 ++++
include/net/bluetooth/bluetooth.h | 5 ----
4 files changed, 69 insertions(+), 5 deletions(-)

diff --git a/drivers/base/property.c b/drivers/base/property.c
index 240ab5230ff6..afe412133188 100644
--- a/drivers/base/property.c
+++ b/drivers/base/property.c
@@ -1315,6 +1315,52 @@ void *device_get_mac_address(struct device *dev, char *addr, int alen)
}
EXPORT_SYMBOL(device_get_mac_address);

+#if IS_ENABLED(CONFIG_BT)
+
+/**
+ * fwnode_get_bd_address - Get the Bluetooth Device Address (BD_ADDR) from the
+ * firmware node
+ * @fwnode: Pointer to the firmware node
+ * @bd_addr: Pointer to struct to store the BD address in
+ *
+ * Search the firmware node for 'local-bd-address'.
+ *
+ * All-zero BD 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 'local-bd-address', with zero BD addresses.
+ */
+int fwnode_get_bd_address(struct fwnode_handle *fwnode, bdaddr_t *bd_addr)
+{
+ bdaddr_t ba;
+ int ret;
+
+ ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
+ (u8 *)&ba, sizeof(bdaddr_t));
+ if (ret < 0)
+ return ret;
+ if (is_zero_ether_addr((u8 *)&ba))
+ return -ENODATA;
+
+ memcpy(bd_addr, &ba, sizeof(bdaddr_t));
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(fwnode_get_bd_address);
+
+/**
+ * device_get_bd_address - Get the Bluetooth Device Address (BD_ADDR) for a
+ * given device
+ * @dev: Pointer to the device
+ * @bd_addr: Pointer to struct to store the BD address in
+ */
+int device_get_bd_address(struct device *dev, bdaddr_t *bd_addr)
+{
+ return fwnode_get_bd_address(dev_fwnode(dev), bd_addr);
+}
+EXPORT_SYMBOL_GPL(device_get_bd_address);
+
+#endif
+
/**
* fwnode_irq_get - Get IRQ directly from a fwnode
* @fwnode: Pointer to the firmware node
diff --git a/include/linux/property.h b/include/linux/property.h
index ac8a1ebc4c1b..5df4e0bd8c83 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -286,10 +286,28 @@ const void *device_get_match_data(struct device *dev);
int device_get_phy_mode(struct device *dev);

void *device_get_mac_address(struct device *dev, char *addr, int alen);
+#if IS_ENABLED(CONFIG_BT)
+int device_get_bd_address(struct device *dev, bdaddr_t *bd_addr);
+#else
+static inline int device_get_bd_address(struct device *dev,
+ bdaddr_t *bd_addr)
+{
+ return -ENOTSUPP;
+}
+#endif

int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
char *addr, int alen);
+#if IS_ENABLED(CONFIG_BT)
+int fwnode_get_bd_address(struct fwnode_handle *fwnode, bdaddr_t *bd_addr);
+#else
+static inline int fwnode_get_bd_address(struct fwnode_handle *fwnode,
+ bdaddr_t *bd_addr)
+{
+ return -ENOTSUPP;
+}
+#endif
struct fwnode_handle *fwnode_graph_get_next_endpoint(
const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
struct fwnode_handle *
diff --git a/include/linux/types.h b/include/linux/types.h
index 9834e90aa010..ff6984a00a3b 100644
--- a/include/linux/types.h
+++ b/include/linux/types.h
@@ -230,5 +230,10 @@ struct callback_head {
typedef void (*rcu_callback_t)(struct rcu_head *head);
typedef void (*call_rcu_func_t)(struct rcu_head *head, rcu_callback_t func);

+/* Bluetooth Device Address (BD_ADDR) */
+typedef struct {
+ __u8 b[6];
+} __packed bdaddr_t;
+
#endif /* __ASSEMBLY__ */
#endif /* _LINUX_TYPES_H */
diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h
index ec9d6bc65855..9401c7431a8f 100644
--- a/include/net/bluetooth/bluetooth.h
+++ b/include/net/bluetooth/bluetooth.h
@@ -190,11 +190,6 @@ static inline const char *state_to_string(int state)
return "invalid state";
}

-/* BD Address */
-typedef struct {
- __u8 b[6];
-} __packed bdaddr_t;
-
/* BD Address type */
#define BDADDR_BREDR 0x00
#define BDADDR_LE_PUBLIC 0x01
--
2.19.0.605.g01d371f741-goog