Skip to contentSkip to conversation

Add API to retrieve the Bluetooth Device Address (BD_ADDR)

PATCH Superseded Matthias Kaehlcke 2 participants 6 messages Sep 24, 2018

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add API to retrieve the Bluetooth Device Address (BD_ADDR)

On some systems the Bluetooth Device Address (BD_ADDR) isn't stored on the Bluetooth chip itself. One way to configure the BD address is through the device tree. The btqcomsmd driver is an example, it can read the BD address from the DT property 'local-bd-address'. It is also planned to extend the hci_qca driver to support setting the BD address through the DT.

To avoid redundant open-coded reading of 'local-bd-address' and error handling this series adds an API to retrieve the BD address of a device and adapts the btqcomsmd driver to use this API.

Matthias Kaehlcke (2):
device property: Add device_get_bd_address() and
fwnode_get_bd_address()
Bluetooth: btqcomsmd: Get the BD address with device_get_bd_address()

 drivers/base/property.c       | 46 +++++++++++++++++++++++++++++++++++
 drivers/bluetooth/btqcomsmd.c |  4 +--
 include/linux/property.h      |  3 +++
 3 files changed, 50 insertions(+), 3 deletions(-)
Signature · 1 line
2.19.0.444.g18242da7ef-goog
#1 lore raw

PATCH v2 1/2 device property: Add device_get_bd_address() and fwnode_get_bd_address()

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.

---
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 <[email protected]>' tag

---
 drivers/base/property.c  | 46 ++++++++++++++++++++++++++++++++++++++++
 include/linux/property.h |  3 +++
 2 files changed, 49 insertions(+)
drivers/base/property.c +46 -0
index 240ab5230ff6..afe412133188 100644
@@ -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
include/linux/property.h +3 -0
index ac8a1ebc4c1b..8926cf95d27e 100644
@@ -15,6 +15,7 @@
 
 #include <linux/fwnode.h>
 #include <linux/types.h>
+#include <net/bluetooth/bluetooth.h>
 
 struct device;
 
@@ -286,10 +287,12 @@ 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);
+int device_get_bd_address(struct device *dev, bdaddr_t *bd_addr);
 
 int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
 void *fwnode_get_mac_address(struct fwnode_handle *fwnode,
 			     char *addr, int alen);
+int fwnode_get_bd_address(struct fwnode_handle *fwnode, bdaddr_t *bd_addr);
 struct fwnode_handle *fwnode_graph_get_next_endpoint(
 	const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
 struct fwnode_handle *
Signature · 1 line
2.19.0.444.g18242da7ef-goog
#2 lore raw

Re: [PATCH v2 1/2] device property: Add device_get_bd_address() and fwnode_get_bd_address()

Quoted 10 lines · Matthias Kaehlcke

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.

include/linux/property.h +0 -0
index ac8a1ebc4c1b..8926cf95d27e 100644

include/linux/property.h line 19

Quoted 5 lines
include/linux/property.h +1 -0
@@ -15,6 +15,7 @@
 
 #include <linux/fwnode.h>
 #include <linux/types.h>
 
+#include <net/bluetooth/bluetooth.h>

Hmm... I don't understand why we need this complete header here. If you are retrieving bdaddr_t type from it, so, better to move it to types.h first.

Sorry I didn't notice this before.

include/linux/property.h const void *device_get_match_data(struct device *dev); · line 295

Quoted 5 lines
include/linux/property.h +2 -0
 struct device;
 
+int device_get_bd_address(struct device *dev, bdaddr_t *bd_addr);
 
+int fwnode_get_bd_address(struct fwnode_handle *fwnode, bdaddr_t *bd_addr);

I don't remember if we discussed below...
Since you put implementations under #ifdef, I'm not sure we can leave header w/o stubs for !CONFIG_BT case.
I can imagine the case where some driver might use BT functionality optionally in which case this enforces ugly #ifdef in the driver.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v2 1/2] device property: Add device_get_bd_address() and fwnode_get_bd_address()

Hi Andy,

thanks for your feedback!

Quoted 12 lines · Andy Shevchenko
Quoted 11 lines · Matthias Kaehlcke

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.

include/linux/property.h +0 -0
index ac8a1ebc4c1b..8926cf95d27e 100644
@@ -15,6 +15,7 @@
 
Quoted 6 lines

include/linux/property.h

Quoted 4 lines
include/linux/property.h +1 -0
 #include <linux/fwnode.h>
 #include <linux/types.h>
 
+#include <net/bluetooth/bluetooth.h>

Hmm... I don't understand why we need this complete header here.

Yes, this is what deterred me from passing bdaddr_t in the first place.

Quoted 2 lines

If you are retrieving bdaddr_t type from it, so, better to move it to types.h first.

Sounds good to me if it is an acceptable solution to put something bluetoothy in types.h.

Quoted 6 lines

Sorry I didn't notice this before.

Quoted 3 lines
include/linux/property.h +1 -0
 struct device;
 
+int device_get_bd_address(struct device *dev, bdaddr_t *bd_addr);
Quoted 7 lines
Quoted 1 line

+int fwnode_get_bd_address(struct fwnode_handle *fwnode, bdaddr_t *bd_addr);

I don't remember if we discussed below...
Since you put implementations under #ifdef, I'm not sure we can leave header w/o stubs for !CONFIG_BT case.
I can imagine the case where some driver might use BT functionality optionally in which case this enforces ugly #ifdef in the driver.

To avoid another possible re-spin: do you prefer a single #ifdef, for both device_get_bd_address() and fwnode_get_bd_address(), or keep the strict grouping of device_*() and fwnode_*() functions?

Cheers

Matthias

#4 lore raw

PATCH v2 2/2 Bluetooth: btqcomsmd: Get the BD address with device_get_bd_address()

In reply to Matthias Kaehlcke

Use the new API to get the BD address instead of reading it directly from the device tree.

Also remove an unncessary pair of braces in the same area of code.

---
Changes in v2:
- pass bdaddr_t instead of byte pointer + len

---
 drivers/bluetooth/btqcomsmd.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/bluetooth/btqcomsmd.c +1 -3
index 7df3eed1ef5e..ff74d2c46991 100644
@@ -172,11 +172,9 @@ static int btqcomsmd_probe(struct platform_device *pdev)
 	/* The local-bd-address property is usually injected by the
 	 * bootloader which has access to the allocated BD address.
 	 */
-	if (!of_property_read_u8_array(pdev->dev.of_node, "local-bd-address",
-				       (u8 *)&btq->bdaddr, sizeof(bdaddr_t))) {
+	if (!device_get_bd_address(&pdev->dev, &btq->bdaddr))
 		dev_info(&pdev->dev, "BD address %pMR retrieved from device-tree",
 			 &btq->bdaddr);
-	}
 
 	hdev = hci_alloc_dev();
 	if (!hdev)
Signature · 1 line
2.19.0.444.g18242da7ef-goog
Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] Bluetooth: btqcomsmd: Get the BD address with device_get_bd_address()

Quoted 6 lines · Matthias Kaehlcke

Use the new API to get the BD address instead of reading it directly from the device tree.

Also remove an unncessary pair of braces in the same area of code.

This looks nice!

Quoted 28 lines

---
Changes in v2:
- pass bdaddr_t instead of byte pointer + len

---
 drivers/bluetooth/btqcomsmd.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/bluetooth/btqcomsmd.c +1 -4
index 7df3eed1ef5e..ff74d2c46991 100644
@@ -172,11 +172,9 @@ static int btqcomsmd_probe(struct platform_device *pdev)
        /* The local-bd-address property is usually injected by the
         * bootloader which has access to the allocated BD address.
         */
-       if (!of_property_read_u8_array(pdev->dev.of_node, "local-bd-address",
-                                      (u8 *)&btq->bdaddr, sizeof(bdaddr_t))) {
+       if (!device_get_bd_address(&pdev->dev, &btq->bdaddr))
                dev_info(&pdev->dev, "BD address %pMR retrieved from device-tree",
                         &btq->bdaddr);
-       }
 
        hdev = hci_alloc_dev();
        if (!hdev)
--

2.19.0.444.g18242da7ef-goog

Signature · 2 lines
With Best Regards,
Andy Shevchenko