Skip to contentSkip to conversation

Bluetooth: Page scan fixes & improvements

PATCH New [email protected] 1 participant 6 messages Jul 28, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Bluetooth: Page scan fixes & improvements

Hi,

Patches 1 and 2 are the same as I already sent as a two-patch set. The rest are new. Out of these 5/5 is probably the single most important one since that's an actual bug fix to the current bluetooth-next tree which should go to mainline before 3.17-rc1 (even if the other patches get postponed for 3.18).

Johan

----------------------------------------------------------------
Johan Hedberg (5):
Bluetooth: Add convenience function to check for pending power off
Bluetooth: Disable page scan if all whitelisted devices are connected
Bluetooth: Fix updating whitelist state for existing connections
Bluetooth: Fix redundant scan mode updates
Bluetooth: Fix incorrectly disabling page scan when toggling connectable

 include/net/bluetooth/hci_core.h |   2 +
 net/bluetooth/hci_event.c        |  35 +++++++++++--
 net/bluetooth/mgmt.c             | 101 ++++++++++++++++++++++++++----------
 3 files changed, 107 insertions(+), 31 deletions(-)
#1 lore raw

PATCH v2 1/5 Bluetooth: Add convenience function to check for pending power off

From: Johan Hedberg <[email protected]>

There are several situations where we're interested in knowing whether we're currently in the process of powering off an adapter. This patch adds a convenience function for the purpose and makes it public since we'll soon need to access it from hci_event.c as well.

---
 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/mgmt.c             | 52 +++++++++++++++++++++-------------------
 2 files changed, 29 insertions(+), 24 deletions(-)
include/net/bluetooth/hci_core.h +1 -0
index b5d5af3aa469..8394abc4fd87 100644
@@ -1351,6 +1351,7 @@ void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
 void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
 		      u8 addr_type, s8 rssi, u8 *name, u8 name_len);
 void mgmt_discovering(struct hci_dev *hdev, u8 discovering);
+bool mgmt_powering_down(struct hci_dev *hdev);
 void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent);
 void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk);
 void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
net/bluetooth/mgmt.c +28 -24

72 lines not on this page · raw message

Signature · 1 line
1.9.3
#2 lore raw

PATCH v2 2/5 Bluetooth: Disable page scan if all whitelisted devices are connected

In reply to [email protected]

From: Johan Hedberg <[email protected]>

When we're not connectable and all whitelisted (BR/EDR) devices are connected it doesn't make sense to keep page scan enabled. This patch adds code to remove entries from the whitelist upon connection complete and to put them back into it upon disconnection complete. A new flag is also needed so that we know which devices need to be re-entered into the whitelist.

---
 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_event.c        | 35 ++++++++++++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 3 deletions(-)
include/net/bluetooth/hci_core.h +1 -0
index 8394abc4fd87..48b372a415ce 100644
@@ -551,6 +551,7 @@ enum {
 	HCI_CONN_FIPS,
 	HCI_CONN_STK_ENCRYPT,
 	HCI_CONN_AUTH_INITIATOR,
+	HCI_CONN_WHITELIST,
 };
 
 static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
net/bluetooth/hci_event.c +32 -3
index ba26fbfe481a..493a4bfb45e3 100644
@@ -2071,6 +2071,20 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 			cp.handle = ev->handle;
 			hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
 				     sizeof(cp), &cp);
+
+			if (!hci_bdaddr_list_del(&hdev->whitelist, &ev->bdaddr,
+						 BDADDR_BREDR)) {
+				set_bit(HCI_CONN_WHITELIST, &conn->flags);
+
+				if (list_empty(&hdev->whitelist) &&
+				    !test_bit(HCI_CONNECTABLE,
+					      &hdev->dev_flags)) {
+					u8 scan = SCAN_DISABLED;
+					hci_send_cmd(hdev,
+						     HCI_OP_WRITE_SCAN_ENABLE,
+						     1, &scan);
+				}
+			}
 		}
 
 		/* Set packet type for incoming connection */
@@ -2247,9 +2261,24 @@ static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 	mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
 				reason, mgmt_connected);
 
-	if (conn->type == ACL_LINK &&
-	    test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
-		hci_remove_link_key(hdev, &conn->dst);
+	if (conn->type == ACL_LINK) {
+		if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
+			hci_remove_link_key(hdev, &conn->dst);
+
+		if (test_bit(HCI_CONN_WHITELIST, &conn->flags)) {
+			bool was_empty = list_empty(&hdev->whitelist);
+
+			hci_bdaddr_list_add(&hdev->whitelist, &conn->dst,
+					    BDADDR_BREDR);
+
+			if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags) &&
+			    was_empty && !mgmt_powering_down(hdev)) {
+				u8 scan = SCAN_PAGE;
+				hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1,
+					     &scan);
+			}
+		}
+	}
 
 	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
 	if (params) {
Signature · 1 line
1.9.3
#3 lore raw

PATCH v2 3/5 Bluetooth: Fix updating whitelist state for existing connections

In reply to [email protected]

From: Johan Hedberg <[email protected]>

If Add/Remove device is called while we have an existing connection we should update the corresponding flag for that connection instead of modifying hdev->whitelist. This patch adds the necessary code to the respective mgmt command handlers along with a helper function to update the whitelist flag for any existing connection.

---
 net/bluetooth/mgmt.c | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)
net/bluetooth/mgmt.c +34 -3
index 3824975c3486..4f56855c1fbb 100644
@@ -5223,6 +5223,26 @@ unlock:
 	return err;
 }
 
+static bool set_conn_whitelist(struct hci_dev *hdev, bdaddr_t *bdaddr,
+			       bool set)
+{
+	struct hci_conn *conn;
+
+	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr);
+	if (!conn)
+		return false;
+
+	if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
+		return false;
+
+	if (set)
+		set_bit(HCI_CONN_WHITELIST, &conn->flags);
+	else
+		clear_bit(HCI_CONN_WHITELIST, &conn->flags);
+
+	return true;
+}
+
 /* Helper for Add/Remove Device commands */
 static void update_page_scan(struct hci_dev *hdev, u8 scan)
 {
@@ -5289,10 +5309,16 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,
 			goto unlock;
 		}
 
-		update_scan = list_empty(&hdev->whitelist);
+		if (set_conn_whitelist(hdev, &cp->addr.bdaddr, true)) {
+			update_scan = false;
+			err = 0;
+		} else {
+			update_scan = list_empty(&hdev->whitelist);
+			err = hci_bdaddr_list_add(&hdev->whitelist,
+						  &cp->addr.bdaddr,
+						  cp->addr.type);
+		}
 
-		err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
-					  cp->addr.type);
 		if (err)
 			goto unlock;
 
@@ -5372,6 +5398,11 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,
 			err = hci_bdaddr_list_del(&hdev->whitelist,
 						  &cp->addr.bdaddr,
 						  cp->addr.type);
+
+			if (err && set_conn_whitelist(hdev, &cp->addr.bdaddr,
+						      false))
+				err = 0;
+
 			if (err) {
 				err = cmd_complete(sk, hdev->id,
 						   MGMT_OP_REMOVE_DEVICE,
Signature · 1 line
1.9.3
#4 lore raw

PATCH v2 4/5 Bluetooth: Fix redundant scan mode updates

In reply to [email protected]

From: Johan Hedberg <[email protected]>

Now that whitelist information is temporarily held in hci_conn objects the calls leading to update_page_scan() will occasionally happen even though page scanning is already in the state we want it to be. To avoid redundant changes to it simply check the HCI_PSCAN flag before sending any HCI command.

---
 net/bluetooth/mgmt.c | 4 ++++
 1 file changed, 4 insertions(+)
net/bluetooth/mgmt.c +4 -0
index 4f56855c1fbb..4d96913484cd 100644
@@ -5258,6 +5258,10 @@ static void update_page_scan(struct hci_dev *hdev, u8 scan)
 	if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
 		return;
 
+	/* Don't bother updating page scan if it's already what we want */
+	if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE))
+		return;
+
 	if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
 		scan |= SCAN_INQUIRY;
 
Signature · 1 line
1.9.3
#5 lore raw

PATCH v2 5/5 Bluetooth: Fix incorrectly disabling page scan when toggling connectable

In reply to [email protected]

From: Johan Hedberg <[email protected]>

If we have entries in the whitelist we shouldn't disable page scanning when disabling connectable mode. This patch adds the necessary check to the Set Connectable command handler.

---
 net/bluetooth/mgmt.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
net/bluetooth/mgmt.c +7 -1
index 4d96913484cd..b2741db957f6 100644
@@ -1881,7 +1881,12 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
 		if (cp->val) {
 			scan = SCAN_PAGE;
 		} else {
-			scan = 0;
+			if (list_empty(&hdev->whitelist))
+				scan = SCAN_DISABLED;
+			else if (test_bit(HCI_ISCAN, &hdev->flags))
+				scan = SCAN_PAGE;
+			else
+				goto no_scan_update;
 
 			if (test_bit(HCI_ISCAN, &hdev->flags) &&
 			    hdev->discov_timeout > 0)
@@ -1891,6 +1896,7 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
 		hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
 	}
 
+no_scan_update:
 	/* If we're going from non-connectable to connectable or
 	 * vice-versa when fast connectable is enabled ensure that fast
 	 * connectable gets disabled. write_fast_connectable won't do
Signature · 1 line
1.9.3