[PATCH v1] Bluetooth: Fix crash in mgmt_add_adv_patterns_monitor_complete

2021-02-02 Thread Howard Chung
If hci_add_adv_monitor is a pending command(e.g. forward to
msft_add_monitor_pattern), it is possible that
mgmt_add_adv_patterns_monitor_complete gets called before
cmd->user_data gets set, which will cause a crash when we
try to get the moniter handle through cmd->user_data in
mgmt_add_adv_patterns_monitor_complete.

This moves the cmd->user_data assignment earlier than
hci_add_adv_monitor.

RIP: 0010:mgmt_add_adv_patterns_monitor_complete+0x82/0x187 [bluetooth]
Code: 1e bf 03 00 00 00 be 52 00 00 00 4c 89 ea e8 9e
e4 02 00 49 89 c6 48 85 c0 0f 84 06 01 00 00 48 89 5d b8 4c 89 fb 4d 8b
7e 30 <41> 0f b7 47 18 66 89 45 c0 45 84 e4 75 5a 4d 8b 56 28 48 8d 4d
c8
RSP: 0018:ae81807dbcb8 EFLAGS: 00010286
RAX: 91c4bdf723c0 RBX:  RCX: 91c4e5da5b80
RDX: 91c40568 RSI: 0052 RDI: 91c49d654c00
RBP: ae81807dbd00 R08: 91c49fb157e0 R09: 91c49fb157e0
R10: 0002a4f0 R11: c0819cfd R12: 
R13: 91c40568 R14: 91c4bdf723c0 R15: 
FS:  () GS:91c4ea30()
knlGS:
CS:  0010 DS:  ES:  CR0: 80050033
CR2: 0018 CR3: 000133612002 CR4:
003606e0
Call Trace:
 ? msft_le_monitor_advertisement_cb+0x111/0x141
[bluetooth]
 hci_event_packet+0x425e/0x631c [bluetooth]
 ? printk+0x59/0x73
 ? __switch_to_asm+0x41/0x70
 ?
msft_le_set_advertisement_filter_enable_cb+0xa6/0xa6 [bluetooth]
 ? bt_dbg+0xb4/0xbb [bluetooth]
 ? __switch_to_asm+0x41/0x70
 hci_rx_work+0x101/0x319 [bluetooth]
 process_one_work+0x257/0x506
 worker_thread+0x10d/0x284
 kthread+0x14c/0x154
 ? process_one_work+0x506/0x506
 ? kthread_blkcg+0x2c/0x2c
 ret_from_fork+0x1f/0x40

Reviewed-by: Miao-chen Chou 
Reviewed-by: Manish Mandlik 
Reviewed-by: Archie Pusaka 
Signed-off-by: Howard Chung 
---

 net/bluetooth/mgmt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 8ff9c4bb43d11..74971b4bd4570 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -4303,6 +4303,7 @@ static int __add_adv_patterns_monitor(struct sock *sk, 
struct hci_dev *hdev,
goto unlock;
}
 
+   cmd->user_data = m;
pending = hci_add_adv_monitor(hdev, m, );
if (err) {
if (err == -ENOSPC || err == -ENOMEM)
@@ -4330,7 +4331,6 @@ static int __add_adv_patterns_monitor(struct sock *sk, 
struct hci_dev *hdev,
 
hci_dev_unlock(hdev);
 
-   cmd->user_data = m;
return 0;
 
 unlock:
-- 
2.30.0.365.g02bc693789-goog



[Bluez PATCH v1] Bluetooth: Fix crash in mgmt_add_adv_patterns_monitor_complete

2021-02-02 Thread Howard Chung
If hci_add_adv_monitor is a pending command(e.g. forward to
msft_add_monitor_pattern), it is possible that
mgmt_add_adv_patterns_monitor_complete gets called before
cmd->user_data gets set, which will cause a crash when we
try to get the moniter handle through cmd->user_data in
mgmt_add_adv_patterns_monitor_complete.

This moves the cmd->user_data assignment earlier than
hci_add_adv_monitor.

RIP: 0010:mgmt_add_adv_patterns_monitor_complete+0x82/0x187 [bluetooth]
Code: 1e bf 03 00 00 00 be 52 00 00 00 4c 89 ea e8 9e
e4 02 00 49 89 c6 48 85 c0 0f 84 06 01 00 00 48 89 5d b8 4c 89 fb 4d 8b
7e 30 <41> 0f b7 47 18 66 89 45 c0 45 84 e4 75 5a 4d 8b 56 28 48 8d 4d
c8
RSP: 0018:ae81807dbcb8 EFLAGS: 00010286
RAX: 91c4bdf723c0 RBX:  RCX: 91c4e5da5b80
RDX: 91c40568 RSI: 0052 RDI: 91c49d654c00
RBP: ae81807dbd00 R08: 91c49fb157e0 R09: 91c49fb157e0
R10: 0002a4f0 R11: c0819cfd R12: 
R13: 91c40568 R14: 91c4bdf723c0 R15: 
FS:  () GS:91c4ea30()
knlGS:
CS:  0010 DS:  ES:  CR0: 80050033
CR2: 0018 CR3: 000133612002 CR4:
003606e0
Call Trace:
 ? msft_le_monitor_advertisement_cb+0x111/0x141
[bluetooth]
 hci_event_packet+0x425e/0x631c [bluetooth]
 ? printk+0x59/0x73
 ? __switch_to_asm+0x41/0x70
 ?
msft_le_set_advertisement_filter_enable_cb+0xa6/0xa6 [bluetooth]
 ? bt_dbg+0xb4/0xbb [bluetooth]
 ? __switch_to_asm+0x41/0x70
 hci_rx_work+0x101/0x319 [bluetooth]
 process_one_work+0x257/0x506
 worker_thread+0x10d/0x284
 kthread+0x14c/0x154
 ? process_one_work+0x506/0x506
 ? kthread_blkcg+0x2c/0x2c
 ret_from_fork+0x1f/0x40

Reviewed-by: Miao-chen Chou 
Reviewed-by: Manish Mandlik 
Reviewed-by: Archie Pusaka 
Signed-off-by: Howard Chung 
---

 net/bluetooth/mgmt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 8ff9c4bb43d11..74971b4bd4570 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -4303,6 +4303,7 @@ static int __add_adv_patterns_monitor(struct sock *sk, 
struct hci_dev *hdev,
goto unlock;
}
 
+   cmd->user_data = m;
pending = hci_add_adv_monitor(hdev, m, );
if (err) {
if (err == -ENOSPC || err == -ENOMEM)
@@ -4330,7 +4331,6 @@ static int __add_adv_patterns_monitor(struct sock *sk, 
struct hci_dev *hdev,
 
hci_dev_unlock(hdev);
 
-   cmd->user_data = m;
return 0;
 
 unlock:
-- 
2.30.0.365.g02bc693789-goog



[PATCH v1] Bluetooth: Set missing suspend task bits

2020-12-03 Thread Howard Chung
From: Abhishek Pandit-Subedi 

When suspending, mark SUSPEND_SCAN_ENABLE and SUSPEND_SCAN_DISABLE tasks
correctly when either classic or le scanning is modified.

Signed-off-by: Abhishek Pandit-Subedi 
Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---

 net/bluetooth/hci_request.c | 8 
 1 file changed, 8 insertions(+)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 80dc451d6e124..71bffd7454720 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -707,6 +707,9 @@ void hci_req_add_le_scan_disable(struct hci_request *req, 
bool rpa_le_conn)
return;
}
 
+   if (hdev->suspended)
+   set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
+
if (use_ext_scan(hdev)) {
struct hci_cp_le_set_ext_scan_enable cp;
 
@@ -1159,6 +1162,11 @@ static void hci_req_set_event_filter(struct hci_request 
*req)
scan = SCAN_PAGE;
}
 
+   if (scan)
+   set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks);
+   else
+   set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
+
hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, );
 }
 
-- 
2.29.2.576.ga3fc446d84-goog



[PATCH v11 5/5] Bluetooth: Add toggle to switch off interleave scan

2020-11-25 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---

(no changes since v9)

Changes in v9:
- Update and rename the macro TLV_GET_LE8

Changes in v7:
- Fix bt_dev_warn arguemnt type warning

Changes in v6:
- Set EnableAdvMonInterleaveScan to 1 byte long

Changes in v4:
- Set EnableAdvMonInterleaveScan default to Disable
- Fix 80 chars limit in mgmt_config.c

 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_core.c |  1 +
 net/bluetooth/hci_request.c  |  3 ++-
 net/bluetooth/mgmt_config.c  | 41 +---
 4 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index cfede18709d8f..63c6d656564a1 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u8enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 20506b31492d6..8cfcf43eb08fd 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3594,6 +3594,7 @@ struct hci_dev *hci_alloc_dev(void)
 
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x00; /* Default to disable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 0c326e32e240c..d0d0fbbb3fa57 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1057,7 +1057,8 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
  _addr_type))
return;
 
-   if (__hci_update_interleaved_scan(hdev))
+   if (hdev->enable_advmon_interleave_scan &&
+   __hci_update_interleaved_scan(hdev))
return;
 
bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 282fbf82f3192..1deb0ca7a9297 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -17,12 +17,24 @@
__le16 value; \
} __packed _param_name_
 
+#define HDEV_PARAM_U8(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __u8 value; \
+   } __packed _param_name_
+
 #define TLV_SET_U16(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
cpu_to_le16(hdev->_param_name_) \
}
 
+#define TLV_SET_U8(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u8) }, \
+   hdev->_param_name_ \
+   }
+
 #define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
@@ -65,6 +77,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
HDEV_PARAM_U16(def_le_autoconnect_timeout);
HDEV_PARAM_U16(advmon_allowlist_duration);
HDEV_PARAM_U16(advmon_no_filter_duration);
+   HDEV_PARAM_U8(enable_advmon_interleave_scan);
} __packed rp = {
TLV_SET_U16(0x, def_page_scan_type),
TLV_SET_U16(0x0001, def_page_scan_int),
@@ -97,6 +110,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 def_le_autoconnect_timeout),
TLV_SET_U16(0x001d, advmon_allowlist_duration),
TLV_SET_U16(0x001e, advmon_no_filter_duration),
+   TLV_SET_U8(0x001f, enable_advmon_interleave_scan),
};
 
bt_dev_dbg(hdev, "sock %p", sk);
@@ -109,6 +123,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 
 #define TO_TLV(x)  ((struct mgmt_tlv *)(x))
 #define TLV_GET_LE16(tlv)  le16_to_cpu(*((__le16 *)(TO_TLV(tlv)->value)))
+#define TLV_GET_U8(tlv)(*((__u8 *)(TO_TLV(tlv)->value)))
 
 int set_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
  u16 data_len)
@@ -125,6 +140,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
/* First pass to validate the tlv */
while (buffer_left >= sizeof(struct mgmt_tlv)) {
const u8 len = TO_TLV(buffer)->length;
+   size_t exp_type_len;
const u16 exp_len = sizeof(struct mgmt_tlv) +
 

[PATCH v11 4/5] Bluetooth: Refactor read default sys config for various types

2020-11-25 Thread Howard Chung
Refactor read default system configuration function so that it's capable
of returning different types than u16

Signed-off-by: Howard Chung 
---

(no changes since v8)

Changes in v8:
- Update the commit title and message

 net/bluetooth/mgmt_config.c | 140 +---
 1 file changed, 84 insertions(+), 56 deletions(-)

diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 2d3ad288c78ac..282fbf82f3192 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -11,72 +11,100 @@
 #include "mgmt_util.h"
 #include "mgmt_config.h"
 
-#define HDEV_PARAM_U16(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(hdev->_param_name_) } \
-}
+#define HDEV_PARAM_U16(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __le16 value; \
+   } __packed _param_name_
 
-#define HDEV_PARAM_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) } \
-}
+#define TLV_SET_U16(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+   cpu_to_le16(hdev->_param_name_) \
+   }
+
+#define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+   cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) \
+   }
 
 int read_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
   u16 data_len)
 {
-   struct {
-   struct mgmt_tlv entry;
-   union {
-   /* This is a simplification for now since all values
-* are 16 bits.  In the future, this code may need
-* refactoring to account for variable length values
-* and properly calculate the required buffer size.
-*/
-   __le16 value;
-   };
-   } __packed params[] = {
+   int ret;
+   struct mgmt_rp_read_def_system_config {
/* Please see mgmt-api.txt for documentation of these values */
-   HDEV_PARAM_U16(0x, def_page_scan_type),
-   HDEV_PARAM_U16(0x0001, def_page_scan_int),
-   HDEV_PARAM_U16(0x0002, def_page_scan_window),
-   HDEV_PARAM_U16(0x0003, def_inq_scan_type),
-   HDEV_PARAM_U16(0x0004, def_inq_scan_int),
-   HDEV_PARAM_U16(0x0005, def_inq_scan_window),
-   HDEV_PARAM_U16(0x0006, def_br_lsto),
-   HDEV_PARAM_U16(0x0007, def_page_timeout),
-   HDEV_PARAM_U16(0x0008, sniff_min_interval),
-   HDEV_PARAM_U16(0x0009, sniff_max_interval),
-   HDEV_PARAM_U16(0x000a, le_adv_min_interval),
-   HDEV_PARAM_U16(0x000b, le_adv_max_interval),
-   HDEV_PARAM_U16(0x000c, def_multi_adv_rotation_duration),
-   HDEV_PARAM_U16(0x000d, le_scan_interval),
-   HDEV_PARAM_U16(0x000e, le_scan_window),
-   HDEV_PARAM_U16(0x000f, le_scan_int_suspend),
-   HDEV_PARAM_U16(0x0010, le_scan_window_suspend),
-   HDEV_PARAM_U16(0x0011, le_scan_int_discovery),
-   HDEV_PARAM_U16(0x0012, le_scan_window_discovery),
-   HDEV_PARAM_U16(0x0013, le_scan_int_adv_monitor),
-   HDEV_PARAM_U16(0x0014, le_scan_window_adv_monitor),
-   HDEV_PARAM_U16(0x0015, le_scan_int_connect),
-   HDEV_PARAM_U16(0x0016, le_scan_window_connect),
-   HDEV_PARAM_U16(0x0017, le_conn_min_interval),
-   HDEV_PARAM_U16(0x0018, le_conn_max_interval),
-   HDEV_PARAM_U16(0x0019, le_conn_latency),
-   HDEV_PARAM_U16(0x001a, le_supv_timeout),
-   HDEV_PARAM_U16_JIFFIES_TO_MSECS(0x001b,
-   def_le_autoconnect_timeout),
-   HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
-   HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(def_page_scan_type);
+   HDEV_PARAM_U16(def_page_scan_int);
+   HDEV_PARAM_U16(def_page_scan_window);
+   HDEV_PARAM_U16(def_inq_scan_type);
+   HDEV_PARAM_U16(def_inq_scan_int);
+   HDEV_PARAM_U16(def_inq_scan_window);
+   HDEV_PARAM_U16(def_br_lsto);
+   HDEV_PARAM_U16(def_page_timeout);
+   HDEV_PARAM_U16(sniff_min_interval);
+   HDEV_PARAM_U16(sniff_max_interval);
+   HDEV_PARAM_U16(le_adv_min_interval);
+   HDEV_PARAM_U16(le_adv_max_interval);
+   HDEV_PARAM_U16(def_multi_adv_rotation_duration);
+   HDEV_PARAM_U16(le_scan_interval)

[PATCH v11 2/5] Bluetooth: Handle system suspend resume case

2020-11-25 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v5)

Changes in v5:
- Remove the change in hci_req_config_le_suspend_scan

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d6bf1517ddaec..8addb94560013 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1294,8 +1294,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.29.2.454.gaff20da3a2-goog



[PATCH v11 3/5] Bluetooth: Handle active scan case

2020-11-25 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 8addb94560013..0c326e32e240c 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3083,8 +3083,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.29.2.454.gaff20da3a2-goog



[PATCH v11 1/5] Bluetooth: Interleave with allowlist scan

2020-11-25 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

Changes in v11:
- Add signed-off-by lines in patch 4/5, 5/5

Changes in v10:
- remove comment about setting default values
- rename should_interleaving to use_interleaving
- rebase on new bluetooth-next/master (previous patch was applied)

Changes in v9:
- Fix compile warning on patch 6/6

Changes in v8:
- Simplified logic in __hci_update_interleaved_scan
- Remove hdev->name when calling bt_dev_dbg
- Remove 'default' in hci_req_add_le_interleaved_scan switch block
- Remove {} around :1915
- Update commit message and title in v7 4/5
- Add a cleanup patch for replacing BT_DBG with bt_dev_dbg

Changes in v7:
- Fix bt_dev_warn argument type warning

Changes in v6:
- Set parameter EnableAdvMonInterleaveScan to 1 byte long

Changes in v5:
- Rename 'adv_monitor' from many functions/variables
- Move __hci_update_interleaved_scan into hci_req_add_le_passive_scan
- Update the logic of update_adv_monitor_scan_state

Changes in v4:
- Rebase to bluetooth-next/master (previous 2 patches are applied)
- Fix over 80 chars limit in mgmt_config.c
- Set EnableAdvMonInterleaveScan default to Disable

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   3 +
 net/bluetooth/hci_request.c  | 128 +--
 net/bluetooth/mgmt_config.c  |  10 +++
 4 files changed, 144 insertions(+), 7 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..cfede18709d8f 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   INTERLEAVE_SCAN_NONE,
+   INTERLEAVE_SCAN_NO_FILTER,
+   INTERLEAVE_SCAN_ALLOWLIST
+   } interleave_scan_state;
+
+   struct delayed_work interleave_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index c4aa2cbb92697..20506b31492d6 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,9 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index fb87882fb71a1..d6bf1517ddaec 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,53 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "cancelling interleave scan");
+
+   cancel_delayed_work_sync(>interleave_scan);
+
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
+}
+
+/* Return true if interleave_scan wasn't started until exiting this function,
+ * otherwise, return false
+ */
+static bool __hci_update_interleaved_scan(struct hci_dev *hdev)
+{
+   /* If there is at least one ADV monitors and one pending LE connection
+* or one device to be scanned for, we should alternate between
+* allowlist scan and one without any filters to save power.
+*/
+   bool use_interleaving = hci_is_adv_monitoring(hdev) &&
+   !(list_empty(>pend_le_conns) &&
+ list_empty(>pend_l

[PATCH v10 4/5] Bluetooth: Refactor read default sys config for various types

2020-11-11 Thread Howard Chung
Refactor read default system configuration function so that it's capable
of returning different types than u16

Signed-off-by: Howard Chung 
---

(no changes since v8)

Changes in v8:
- Update the commit title and message

 net/bluetooth/mgmt_config.c | 140 +---
 1 file changed, 84 insertions(+), 56 deletions(-)

diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 2d3ad288c78ac..282fbf82f3192 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -11,72 +11,100 @@
 #include "mgmt_util.h"
 #include "mgmt_config.h"
 
-#define HDEV_PARAM_U16(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(hdev->_param_name_) } \
-}
+#define HDEV_PARAM_U16(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __le16 value; \
+   } __packed _param_name_
 
-#define HDEV_PARAM_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) } \
-}
+#define TLV_SET_U16(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+   cpu_to_le16(hdev->_param_name_) \
+   }
+
+#define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+   cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) \
+   }
 
 int read_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
   u16 data_len)
 {
-   struct {
-   struct mgmt_tlv entry;
-   union {
-   /* This is a simplification for now since all values
-* are 16 bits.  In the future, this code may need
-* refactoring to account for variable length values
-* and properly calculate the required buffer size.
-*/
-   __le16 value;
-   };
-   } __packed params[] = {
+   int ret;
+   struct mgmt_rp_read_def_system_config {
/* Please see mgmt-api.txt for documentation of these values */
-   HDEV_PARAM_U16(0x, def_page_scan_type),
-   HDEV_PARAM_U16(0x0001, def_page_scan_int),
-   HDEV_PARAM_U16(0x0002, def_page_scan_window),
-   HDEV_PARAM_U16(0x0003, def_inq_scan_type),
-   HDEV_PARAM_U16(0x0004, def_inq_scan_int),
-   HDEV_PARAM_U16(0x0005, def_inq_scan_window),
-   HDEV_PARAM_U16(0x0006, def_br_lsto),
-   HDEV_PARAM_U16(0x0007, def_page_timeout),
-   HDEV_PARAM_U16(0x0008, sniff_min_interval),
-   HDEV_PARAM_U16(0x0009, sniff_max_interval),
-   HDEV_PARAM_U16(0x000a, le_adv_min_interval),
-   HDEV_PARAM_U16(0x000b, le_adv_max_interval),
-   HDEV_PARAM_U16(0x000c, def_multi_adv_rotation_duration),
-   HDEV_PARAM_U16(0x000d, le_scan_interval),
-   HDEV_PARAM_U16(0x000e, le_scan_window),
-   HDEV_PARAM_U16(0x000f, le_scan_int_suspend),
-   HDEV_PARAM_U16(0x0010, le_scan_window_suspend),
-   HDEV_PARAM_U16(0x0011, le_scan_int_discovery),
-   HDEV_PARAM_U16(0x0012, le_scan_window_discovery),
-   HDEV_PARAM_U16(0x0013, le_scan_int_adv_monitor),
-   HDEV_PARAM_U16(0x0014, le_scan_window_adv_monitor),
-   HDEV_PARAM_U16(0x0015, le_scan_int_connect),
-   HDEV_PARAM_U16(0x0016, le_scan_window_connect),
-   HDEV_PARAM_U16(0x0017, le_conn_min_interval),
-   HDEV_PARAM_U16(0x0018, le_conn_max_interval),
-   HDEV_PARAM_U16(0x0019, le_conn_latency),
-   HDEV_PARAM_U16(0x001a, le_supv_timeout),
-   HDEV_PARAM_U16_JIFFIES_TO_MSECS(0x001b,
-   def_le_autoconnect_timeout),
-   HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
-   HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(def_page_scan_type);
+   HDEV_PARAM_U16(def_page_scan_int);
+   HDEV_PARAM_U16(def_page_scan_window);
+   HDEV_PARAM_U16(def_inq_scan_type);
+   HDEV_PARAM_U16(def_inq_scan_int);
+   HDEV_PARAM_U16(def_inq_scan_window);
+   HDEV_PARAM_U16(def_br_lsto);
+   HDEV_PARAM_U16(def_page_timeout);
+   HDEV_PARAM_U16(sniff_min_interval);
+   HDEV_PARAM_U16(sniff_max_interval);
+   HDEV_PARAM_U16(le_adv_min_interval);
+   HDEV_PARAM_U16(le_adv_max_interval);
+   HDEV_PARAM_U16(def_multi_adv_rotation_duration);
+   HDEV_PARAM_U16(le_scan_interval)

[PATCH v10 3/5] Bluetooth: Handle active scan case

2020-11-11 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 3bb0d2a98e82c..2368b050c17f1 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3099,8 +3099,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.29.2.222.g5d2a92d10f8-goog



[PATCH v10 5/5] Bluetooth: Add toggle to switch off interleave scan

2020-11-11 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Reviewed-by: Alain Michaud 
Signed-off-by: Howard Chung 
---

(no changes since v9)

Changes in v9:
- Update and rename the macro TLV_GET_LE8

Changes in v7:
- Fix bt_dev_warn arguemnt type warning

Changes in v6:
- Set EnableAdvMonInterleaveScan to 1 byte long

Changes in v4:
- Set EnableAdvMonInterleaveScan default to Disable
- Fix 80 chars limit in mgmt_config.c

 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_core.c |  1 +
 net/bluetooth/hci_request.c  |  3 ++-
 net/bluetooth/mgmt_config.c  | 41 +---
 4 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index cfede18709d8f..63c6d656564a1 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u8enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 28ca419dcda6f..5b3f3f92a2e43 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3594,6 +3594,7 @@ struct hci_dev *hci_alloc_dev(void)
 
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x00; /* Default to disable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 2368b050c17f1..e669988a3785c 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1057,7 +1057,8 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
  _addr_type))
return;
 
-   if (__hci_update_interleaved_scan(hdev))
+   if (hdev->enable_advmon_interleave_scan &&
+   __hci_update_interleaved_scan(hdev))
return;
 
bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 282fbf82f3192..1deb0ca7a9297 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -17,12 +17,24 @@
__le16 value; \
} __packed _param_name_
 
+#define HDEV_PARAM_U8(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __u8 value; \
+   } __packed _param_name_
+
 #define TLV_SET_U16(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
cpu_to_le16(hdev->_param_name_) \
}
 
+#define TLV_SET_U8(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u8) }, \
+   hdev->_param_name_ \
+   }
+
 #define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
@@ -65,6 +77,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
HDEV_PARAM_U16(def_le_autoconnect_timeout);
HDEV_PARAM_U16(advmon_allowlist_duration);
HDEV_PARAM_U16(advmon_no_filter_duration);
+   HDEV_PARAM_U8(enable_advmon_interleave_scan);
} __packed rp = {
TLV_SET_U16(0x, def_page_scan_type),
TLV_SET_U16(0x0001, def_page_scan_int),
@@ -97,6 +110,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 def_le_autoconnect_timeout),
TLV_SET_U16(0x001d, advmon_allowlist_duration),
TLV_SET_U16(0x001e, advmon_no_filter_duration),
+   TLV_SET_U8(0x001f, enable_advmon_interleave_scan),
};
 
bt_dev_dbg(hdev, "sock %p", sk);
@@ -109,6 +123,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 
 #define TO_TLV(x)  ((struct mgmt_tlv *)(x))
 #define TLV_GET_LE16(tlv)  le16_to_cpu(*((__le16 *)(TO_TLV(tlv)->value)))
+#define TLV_GET_U8(tlv)(*((__u8 *)(TO_TLV(tlv)->value)))
 
 int set_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
  u16 data_len)
@@ -125,6 +140,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
/* First pass to validate the tlv */
while (buffer_left >= sizeof(struct mgmt_tlv)) {
const u8 len = TO_TLV(buffer)->length;
+   size_t exp_type_len;
const u16 exp_len = sizeof(struct mgmt_tlv) +
 

[PATCH v10 1/5] Bluetooth: Interleave with allowlist scan

2020-11-11 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

Changes in v10:
- remove comment about setting default values
- rename should_interleaving to use_interleaving
- rebase on new bluetooth-next/master (previous patch was applied)

Changes in v9:
- Fix compile warning on patch 6/6

Changes in v8:
- Simplified logic in __hci_update_interleaved_scan
- Remove hdev->name when calling bt_dev_dbg
- Remove 'default' in hci_req_add_le_interleaved_scan switch block
- Remove {} around :1915
- Update commit message and title in v7 4/5
- Add a cleanup patch for replacing BT_DBG with bt_dev_dbg

Changes in v7:
- Fix bt_dev_warn argument type warning

Changes in v6:
- Set parameter EnableAdvMonInterleaveScan to 1 byte long

Changes in v5:
- Rename 'adv_monitor' from many functions/variables
- Move __hci_update_interleaved_scan into hci_req_add_le_passive_scan
- Update the logic of update_adv_monitor_scan_state

Changes in v4:
- Rebase to bluetooth-next/master (previous 2 patches are applied)
- Fix over 80 chars limit in mgmt_config.c
- Set EnableAdvMonInterleaveScan default to Disable

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   3 +
 net/bluetooth/hci_request.c  | 128 +--
 net/bluetooth/mgmt_config.c  |  10 +++
 4 files changed, 144 insertions(+), 7 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..cfede18709d8f 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   INTERLEAVE_SCAN_NONE,
+   INTERLEAVE_SCAN_NO_FILTER,
+   INTERLEAVE_SCAN_ALLOWLIST
+   } interleave_scan_state;
+
+   struct delayed_work interleave_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 502552d6e9aff..28ca419dcda6f 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,9 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 048d4db9d4ea5..51cc237b7ce60 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,53 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "cancelling interleave scan");
+
+   cancel_delayed_work_sync(>interleave_scan);
+
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
+}
+
+/* Return true if interleave_scan wasn't started until exiting this function,
+ * otherwise, return false
+ */
+static bool __hci_update_interleaved_scan(struct hci_dev *hdev)
+{
+   /* If there is at least one ADV monitors and one pending LE connection
+* or one device to be scanned for, we should alternate between
+* allowlist scan and one without any filters to save power.
+*/
+   bool use_interleaving = hci_is_adv_monitoring(hdev) &&
+   !(list_empty(>pend_le_conns) &&
+ list_empty(>pend_le_reports));
+   bool is_interleaving = is_interl

[PATCH v10 2/5] Bluetooth: Handle system suspend resume case

2020-11-11 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v5)

Changes in v5:
- Remove the change in hci_req_config_le_suspend_scan

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 51cc237b7ce60..3bb0d2a98e82c 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1293,8 +1293,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.29.2.222.g5d2a92d10f8-goog



[PATCH v9 4/6] Bluetooth: Handle active scan case

2020-11-10 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d943ad2885aa0..172ccbf4f0cd2 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3099,8 +3099,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.29.2.222.g5d2a92d10f8-goog



[PATCH v9 5/6] Bluetooth: Refactor read default sys config for various types

2020-11-10 Thread Howard Chung
Refactor read default system configuration function so that it's capable
of returning different types than u16

Signed-off-by: Howard Chung 
---

(no changes since v8)

Changes in v8:
- Update the commit title and message

 net/bluetooth/mgmt_config.c | 140 +---
 1 file changed, 84 insertions(+), 56 deletions(-)

diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 2d3ad288c78ac..282fbf82f3192 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -11,72 +11,100 @@
 #include "mgmt_util.h"
 #include "mgmt_config.h"
 
-#define HDEV_PARAM_U16(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(hdev->_param_name_) } \
-}
+#define HDEV_PARAM_U16(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __le16 value; \
+   } __packed _param_name_
 
-#define HDEV_PARAM_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) } \
-}
+#define TLV_SET_U16(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+   cpu_to_le16(hdev->_param_name_) \
+   }
+
+#define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+   cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) \
+   }
 
 int read_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
   u16 data_len)
 {
-   struct {
-   struct mgmt_tlv entry;
-   union {
-   /* This is a simplification for now since all values
-* are 16 bits.  In the future, this code may need
-* refactoring to account for variable length values
-* and properly calculate the required buffer size.
-*/
-   __le16 value;
-   };
-   } __packed params[] = {
+   int ret;
+   struct mgmt_rp_read_def_system_config {
/* Please see mgmt-api.txt for documentation of these values */
-   HDEV_PARAM_U16(0x, def_page_scan_type),
-   HDEV_PARAM_U16(0x0001, def_page_scan_int),
-   HDEV_PARAM_U16(0x0002, def_page_scan_window),
-   HDEV_PARAM_U16(0x0003, def_inq_scan_type),
-   HDEV_PARAM_U16(0x0004, def_inq_scan_int),
-   HDEV_PARAM_U16(0x0005, def_inq_scan_window),
-   HDEV_PARAM_U16(0x0006, def_br_lsto),
-   HDEV_PARAM_U16(0x0007, def_page_timeout),
-   HDEV_PARAM_U16(0x0008, sniff_min_interval),
-   HDEV_PARAM_U16(0x0009, sniff_max_interval),
-   HDEV_PARAM_U16(0x000a, le_adv_min_interval),
-   HDEV_PARAM_U16(0x000b, le_adv_max_interval),
-   HDEV_PARAM_U16(0x000c, def_multi_adv_rotation_duration),
-   HDEV_PARAM_U16(0x000d, le_scan_interval),
-   HDEV_PARAM_U16(0x000e, le_scan_window),
-   HDEV_PARAM_U16(0x000f, le_scan_int_suspend),
-   HDEV_PARAM_U16(0x0010, le_scan_window_suspend),
-   HDEV_PARAM_U16(0x0011, le_scan_int_discovery),
-   HDEV_PARAM_U16(0x0012, le_scan_window_discovery),
-   HDEV_PARAM_U16(0x0013, le_scan_int_adv_monitor),
-   HDEV_PARAM_U16(0x0014, le_scan_window_adv_monitor),
-   HDEV_PARAM_U16(0x0015, le_scan_int_connect),
-   HDEV_PARAM_U16(0x0016, le_scan_window_connect),
-   HDEV_PARAM_U16(0x0017, le_conn_min_interval),
-   HDEV_PARAM_U16(0x0018, le_conn_max_interval),
-   HDEV_PARAM_U16(0x0019, le_conn_latency),
-   HDEV_PARAM_U16(0x001a, le_supv_timeout),
-   HDEV_PARAM_U16_JIFFIES_TO_MSECS(0x001b,
-   def_le_autoconnect_timeout),
-   HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
-   HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(def_page_scan_type);
+   HDEV_PARAM_U16(def_page_scan_int);
+   HDEV_PARAM_U16(def_page_scan_window);
+   HDEV_PARAM_U16(def_inq_scan_type);
+   HDEV_PARAM_U16(def_inq_scan_int);
+   HDEV_PARAM_U16(def_inq_scan_window);
+   HDEV_PARAM_U16(def_br_lsto);
+   HDEV_PARAM_U16(def_page_timeout);
+   HDEV_PARAM_U16(sniff_min_interval);
+   HDEV_PARAM_U16(sniff_max_interval);
+   HDEV_PARAM_U16(le_adv_min_interval);
+   HDEV_PARAM_U16(le_adv_max_interval);
+   HDEV_PARAM_U16(def_multi_adv_rotation_duration);
+   HDEV_PARAM_U16(le_scan_interval)

[PATCH v9 6/6] Bluetooth: Add toggle to switch off interleave scan

2020-11-10 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Reviewed-by: Alain Michaud 
Signed-off-by: Howard Chung 
---

Changes in v9:
- Update and rename the macro TLV_GET_LE8

Changes in v7:
- Fix bt_dev_warn arguemnt type warning

Changes in v6:
- Set EnableAdvMonInterleaveScan to 1 byte long

Changes in v4:
- Set EnableAdvMonInterleaveScan default to Disable
- Fix 80 chars limit in mgmt_config.c

 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_core.c |  1 +
 net/bluetooth/hci_request.c  |  3 ++-
 net/bluetooth/mgmt_config.c  | 41 +---
 4 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index cfede18709d8f..63c6d656564a1 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u8enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 65b7b74baba4c..b7cb7bfe250bd 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x00; /* Default to disable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 172ccbf4f0cd2..28520c4d2d229 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1057,7 +1057,8 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
  _addr_type))
return;
 
-   if (__hci_update_interleaved_scan(hdev))
+   if (hdev->enable_advmon_interleave_scan &&
+   __hci_update_interleaved_scan(hdev))
return;
 
bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 282fbf82f3192..1deb0ca7a9297 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -17,12 +17,24 @@
__le16 value; \
} __packed _param_name_
 
+#define HDEV_PARAM_U8(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __u8 value; \
+   } __packed _param_name_
+
 #define TLV_SET_U16(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
cpu_to_le16(hdev->_param_name_) \
}
 
+#define TLV_SET_U8(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u8) }, \
+   hdev->_param_name_ \
+   }
+
 #define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
@@ -65,6 +77,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
HDEV_PARAM_U16(def_le_autoconnect_timeout);
HDEV_PARAM_U16(advmon_allowlist_duration);
HDEV_PARAM_U16(advmon_no_filter_duration);
+   HDEV_PARAM_U8(enable_advmon_interleave_scan);
} __packed rp = {
TLV_SET_U16(0x, def_page_scan_type),
TLV_SET_U16(0x0001, def_page_scan_int),
@@ -97,6 +110,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 def_le_autoconnect_timeout),
TLV_SET_U16(0x001d, advmon_allowlist_duration),
TLV_SET_U16(0x001e, advmon_no_filter_duration),
+   TLV_SET_U8(0x001f, enable_advmon_interleave_scan),
};
 
bt_dev_dbg(hdev, "sock %p", sk);
@@ -109,6 +123,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 
 #define TO_TLV(x)  ((struct mgmt_tlv *)(x))
 #define TLV_GET_LE16(tlv)  le16_to_cpu(*((__le16 *)(TO_TLV(tlv)->value)))
+#define TLV_GET_U8(tlv)(*((__u8 *)(TO_TLV(tlv)->value)))
 
 int set_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
  u16 data_len)
@@ -125,6 +140,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
/* First pass to validate the tlv */
while (buffer_left >= sizeof(struct mgmt_tlv)) {
const u8 len = TO_TLV(buffer)->length;
+   size_t exp_type_len;
const u16 exp_len 

[PATCH v9 3/6] Bluetooth: Handle system suspend resume case

2020-11-10 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v5)

Changes in v5:
- Remove the change in hci_req_config_le_suspend_scan

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 2fd56ee21d31f..d943ad2885aa0 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1293,8 +1293,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.29.2.222.g5d2a92d10f8-goog



[PATCH v9 2/6] Bluetooth: Interleave with allowlist scan

2020-11-10 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v8)

Changes in v8:
- Simplified logic in __hci_update_interleaved_scan
- remove hdev->name when calling bt_dev_dbg
- remove 'default' in hci_req_add_le_interleaved_scan switch block
- remove {} around :1915

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 128 +--
 net/bluetooth/mgmt_config.c  |  10 +++
 4 files changed, 145 insertions(+), 7 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..cfede18709d8f 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   INTERLEAVE_SCAN_NONE,
+   INTERLEAVE_SCAN_NO_FILTER,
+   INTERLEAVE_SCAN_ALLOWLIST
+   } interleave_scan_state;
+
+   struct delayed_work interleave_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 502552d6e9aff..65b7b74baba4c 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 048d4db9d4ea5..2fd56ee21d31f 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,53 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "cancelling interleave scan");
+
+   cancel_delayed_work_sync(>interleave_scan);
+
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
+}
+
+/* Return true if interleave_scan wasn't started until exiting this function,
+ * otherwise, return false
+ */
+static bool __hci_update_interleaved_scan(struct hci_dev *hdev)
+{
+   /* If there is at least one ADV monitors and one pending LE connection
+* or one device to be scanned for, we should alternate between
+* allowlist scan and one without any filters to save power.
+*/
+   bool should_interleaving = hci_is_adv_monitoring(hdev) &&
+  !(list_empty(>pend_le_conns) &&
+list_empty(>pend_le_reports));
+   bool is_interleaving = is_interleave_scanning(hdev);
+
+   if (should_interleaving && !is_interleaving) {
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "starting interleave scan");
+   return true;
+   }
+
+   if (!should_interleaving && is_interleaving)
+   cancel_interleave_scan(hdev);
+
+   return false;
+}
+
 /* This function controls the background scanning based on hdev->pend_le_conns
  * list. If there are pending LE connection we start the background scanning,
  * otherwise we stop it.
@@ -450,8 +497,7 @@ static void __hci_update_background_scan(struct hci_request 
*req)
hci_req_add_le_scan_disable(req, false);
 
hci_req_add_le_passive_scan(req);
-
-   BT_DBG("%s starting background scanning", hd

[PATCH v9 1/6] Bluetooth: Replace BT_DBG with bt_dev_dbg in HCI request

2020-11-10 Thread Howard Chung
This replaces the BT_DBG function to bt_dev_dbg as it is cleaner to show
the controller index in the debug message.

Signed-off-by: Howard Chung 
---

Changes in v9:
- Fix compile warning on patch 6/6

Changes in v8:
- Simplified logic in __hci_update_interleaved_scan
- Remove hdev->name when calling bt_dev_dbg
- Remove 'default' in hci_req_add_le_interleaved_scan switch block
- Remove {} around :1915
- Update commit message and title in v7 4/5
- Add a cleanup patch for replacing BT_DBG with bt_dev_dbg

Changes in v7:
- Fix bt_dev_warn argument type warning

Changes in v6:
- Set parameter EnableAdvMonInterleaveScan to 1 byte long

Changes in v5:
- Rename 'adv_monitor' from many functions/variables
- Move __hci_update_interleaved_scan into hci_req_add_le_passive_scan
- Update the logic of update_adv_monitor_scan_state

Changes in v4:
- Rebase to bluetooth-next/master (previous 2 patches are applied)
- Fix over 80 chars limit in mgmt_config.c
- Set EnableAdvMonInterleaveScan default to Disable

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 net/bluetooth/hci_request.c | 52 ++---
 1 file changed, 26 insertions(+), 26 deletions(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 6a74097c50d34..048d4db9d4ea5 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -58,7 +58,7 @@ static int req_run(struct hci_request *req, 
hci_req_complete_t complete,
struct sk_buff *skb;
unsigned long flags;
 
-   BT_DBG("length %u", skb_queue_len(>cmd_q));
+   bt_dev_dbg(hdev, "length %u", skb_queue_len(>cmd_q));
 
/* If an error occurred during request building, remove all HCI
 * commands queued on the HCI request queue.
@@ -102,7 +102,7 @@ int hci_req_run_skb(struct hci_request *req, 
hci_req_complete_skb_t complete)
 static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
  struct sk_buff *skb)
 {
-   BT_DBG("%s result 0x%2.2x", hdev->name, result);
+   bt_dev_dbg(hdev, "result 0x%2.2x", result);
 
if (hdev->req_status == HCI_REQ_PEND) {
hdev->req_result = result;
@@ -115,7 +115,7 @@ static void hci_req_sync_complete(struct hci_dev *hdev, u8 
result, u16 opcode,
 
 void hci_req_sync_cancel(struct hci_dev *hdev, int err)
 {
-   BT_DBG("%s err 0x%2.2x", hdev->name, err);
+   bt_dev_dbg(hdev, "err 0x%2.2x", err);
 
if (hdev->req_status == HCI_REQ_PEND) {
hdev->req_result = err;
@@ -131,7 +131,7 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 
opcode, u32 plen,
struct sk_buff *skb;
int err = 0;
 
-   BT_DBG("%s", hdev->name);
+   bt_dev_dbg(hdev, "");
 
hci_req_init(, hdev);
 
@@ -167,7 +167,7 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 
opcode, u32 plen,
skb = hdev->req_skb;
hdev->req_skb = NULL;
 
-   BT_DBG("%s end: err %d", hdev->name, err);
+   bt_dev_dbg(hdev, "end: err %d", err);
 
if (err < 0) {
kfree_skb(skb);
@@ -196,7 +196,7 @@ int __hci_req_sync(struct hci_dev *hdev, int (*func)(struct 
hci_request *req,
struct hci_request req;
int err = 0;
 
-   BT_DBG("%s start", hdev->name);
+   bt_dev_dbg(hdev, "start");
 
hci_req_init(, hdev);
 
@@ -260,7 +260,7 @@ int __hci_req_sync(struct hci_dev *hdev, int (*func)(struct 
hci_request *req,
hdev->req_skb = NULL;
hdev->req_status = hdev->req_result = 0;
 
-   BT_DBG("%s end: err %d", hdev->name, err);
+   bt_dev_dbg(hdev, "end: err %d", err);
 
return err;
 }
@@ -300,7 +300,7 @@ struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 
opcode, u32 plen,
if (plen)
skb_put_data(skb, param, plen);
 
-   BT_DBG("skb len %d", skb->len);
+   bt_dev_dbg(hdev, "skb len %d", skb->len);
 
hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
hci_skb_opcode(skb) = opcode;
@@ -315,7 +315,7 @@ void hci_req_add_ev(struct hci_request *req, u16 opcode, 
u32 plen,
struct hci_dev *hdev = req->hdev;
struct sk_buff *skb;
 
-   BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
+   bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
 
/* If an error occurred during request building, there is no point in
 * queueing the HCI command. We can simply return.
@@ -413,8 +413,8 @@ static void __hci_update_background_scan(struct hci_request 
*req)
 */
hci_discovery_filter_clear(hdev);
 
-   BT_DBG("%s ADV monitoring is %s", hdev->name,
-  hci_is_adv_moni

[PATCH v8 4/6] Bluetooth: Handle active scan case

2020-11-10 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d943ad2885aa0..172ccbf4f0cd2 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3099,8 +3099,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.29.2.222.g5d2a92d10f8-goog



[PATCH v8 3/6] Bluetooth: Handle system suspend resume case

2020-11-10 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v5)

Changes in v5:
- Remove the change in hci_req_config_le_suspend_scan

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 2fd56ee21d31f..d943ad2885aa0 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1293,8 +1293,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.29.2.222.g5d2a92d10f8-goog



[PATCH v8 6/6] Bluetooth: Add toggle to switch off interleave scan

2020-11-10 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Reviewed-by: Alain Michaud 
Signed-off-by: Howard Chung 
---

(no changes since v6)

Changes in v6:
- Set EnableAdvMonInterleaveScan to 1 byte long

Changes in v4:
- Set EnableAdvMonInterleaveScan default to Disable
- Fix 80 chars limit in mgmt_config.c

 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_core.c |  1 +
 net/bluetooth/hci_request.c  |  3 ++-
 net/bluetooth/mgmt_config.c  | 41 +---
 4 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index cfede18709d8f..63c6d656564a1 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u8enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 65b7b74baba4c..b7cb7bfe250bd 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x00; /* Default to disable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 172ccbf4f0cd2..28520c4d2d229 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1057,7 +1057,8 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
  _addr_type))
return;
 
-   if (__hci_update_interleaved_scan(hdev))
+   if (hdev->enable_advmon_interleave_scan &&
+   __hci_update_interleaved_scan(hdev))
return;
 
bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 282fbf82f3192..3cdcb66ccac38 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -17,12 +17,24 @@
__le16 value; \
} __packed _param_name_
 
+#define HDEV_PARAM_U8(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __u8 value; \
+   } __packed _param_name_
+
 #define TLV_SET_U16(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
cpu_to_le16(hdev->_param_name_) \
}
 
+#define TLV_SET_U8(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u8) }, \
+   hdev->_param_name_ \
+   }
+
 #define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
@@ -65,6 +77,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
HDEV_PARAM_U16(def_le_autoconnect_timeout);
HDEV_PARAM_U16(advmon_allowlist_duration);
HDEV_PARAM_U16(advmon_no_filter_duration);
+   HDEV_PARAM_U8(enable_advmon_interleave_scan);
} __packed rp = {
TLV_SET_U16(0x, def_page_scan_type),
TLV_SET_U16(0x0001, def_page_scan_int),
@@ -97,6 +110,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 def_le_autoconnect_timeout),
TLV_SET_U16(0x001d, advmon_allowlist_duration),
TLV_SET_U16(0x001e, advmon_no_filter_duration),
+   TLV_SET_U8(0x001f, enable_advmon_interleave_scan),
};
 
bt_dev_dbg(hdev, "sock %p", sk);
@@ -109,6 +123,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 
 #define TO_TLV(x)  ((struct mgmt_tlv *)(x))
 #define TLV_GET_LE16(tlv)  le16_to_cpu(*((__le16 *)(TO_TLV(tlv)->value)))
+#define TLV_GET_LE8(tlv)   le16_to_cpu(*((__u8 *)(TO_TLV(tlv)->value)))
 
 int set_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
  u16 data_len)
@@ -125,6 +140,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
/* First pass to validate the tlv */
while (buffer_left >= sizeof(struct mgmt_tlv)) {
const u8 len = TO_TLV(buffer)->length;
+   u8 exp_type_len;
const u16 exp_len = sizeof(struct mgmt_tlv) +
len;
const u16 

[PATCH v8 5/6] Bluetooth: Refactor read default sys config for various types

2020-11-10 Thread Howard Chung
Refactor read default system configuration function so that it's capable
of returning different types than u16

Signed-off-by: Howard Chung 
---

Changes in v8:
- Update the commit title and message

 net/bluetooth/mgmt_config.c | 140 +---
 1 file changed, 84 insertions(+), 56 deletions(-)

diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 2d3ad288c78ac..282fbf82f3192 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -11,72 +11,100 @@
 #include "mgmt_util.h"
 #include "mgmt_config.h"
 
-#define HDEV_PARAM_U16(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(hdev->_param_name_) } \
-}
+#define HDEV_PARAM_U16(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __le16 value; \
+   } __packed _param_name_
 
-#define HDEV_PARAM_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) } \
-}
+#define TLV_SET_U16(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+   cpu_to_le16(hdev->_param_name_) \
+   }
+
+#define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+   cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) \
+   }
 
 int read_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
   u16 data_len)
 {
-   struct {
-   struct mgmt_tlv entry;
-   union {
-   /* This is a simplification for now since all values
-* are 16 bits.  In the future, this code may need
-* refactoring to account for variable length values
-* and properly calculate the required buffer size.
-*/
-   __le16 value;
-   };
-   } __packed params[] = {
+   int ret;
+   struct mgmt_rp_read_def_system_config {
/* Please see mgmt-api.txt for documentation of these values */
-   HDEV_PARAM_U16(0x, def_page_scan_type),
-   HDEV_PARAM_U16(0x0001, def_page_scan_int),
-   HDEV_PARAM_U16(0x0002, def_page_scan_window),
-   HDEV_PARAM_U16(0x0003, def_inq_scan_type),
-   HDEV_PARAM_U16(0x0004, def_inq_scan_int),
-   HDEV_PARAM_U16(0x0005, def_inq_scan_window),
-   HDEV_PARAM_U16(0x0006, def_br_lsto),
-   HDEV_PARAM_U16(0x0007, def_page_timeout),
-   HDEV_PARAM_U16(0x0008, sniff_min_interval),
-   HDEV_PARAM_U16(0x0009, sniff_max_interval),
-   HDEV_PARAM_U16(0x000a, le_adv_min_interval),
-   HDEV_PARAM_U16(0x000b, le_adv_max_interval),
-   HDEV_PARAM_U16(0x000c, def_multi_adv_rotation_duration),
-   HDEV_PARAM_U16(0x000d, le_scan_interval),
-   HDEV_PARAM_U16(0x000e, le_scan_window),
-   HDEV_PARAM_U16(0x000f, le_scan_int_suspend),
-   HDEV_PARAM_U16(0x0010, le_scan_window_suspend),
-   HDEV_PARAM_U16(0x0011, le_scan_int_discovery),
-   HDEV_PARAM_U16(0x0012, le_scan_window_discovery),
-   HDEV_PARAM_U16(0x0013, le_scan_int_adv_monitor),
-   HDEV_PARAM_U16(0x0014, le_scan_window_adv_monitor),
-   HDEV_PARAM_U16(0x0015, le_scan_int_connect),
-   HDEV_PARAM_U16(0x0016, le_scan_window_connect),
-   HDEV_PARAM_U16(0x0017, le_conn_min_interval),
-   HDEV_PARAM_U16(0x0018, le_conn_max_interval),
-   HDEV_PARAM_U16(0x0019, le_conn_latency),
-   HDEV_PARAM_U16(0x001a, le_supv_timeout),
-   HDEV_PARAM_U16_JIFFIES_TO_MSECS(0x001b,
-   def_le_autoconnect_timeout),
-   HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
-   HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(def_page_scan_type);
+   HDEV_PARAM_U16(def_page_scan_int);
+   HDEV_PARAM_U16(def_page_scan_window);
+   HDEV_PARAM_U16(def_inq_scan_type);
+   HDEV_PARAM_U16(def_inq_scan_int);
+   HDEV_PARAM_U16(def_inq_scan_window);
+   HDEV_PARAM_U16(def_br_lsto);
+   HDEV_PARAM_U16(def_page_timeout);
+   HDEV_PARAM_U16(sniff_min_interval);
+   HDEV_PARAM_U16(sniff_max_interval);
+   HDEV_PARAM_U16(le_adv_min_interval);
+   HDEV_PARAM_U16(le_adv_max_interval);
+   HDEV_PARAM_U16(def_multi_adv_rotation_duration);
+   HDEV_PARAM_U16(le_scan_interval);
+

[PATCH v8 2/6] Bluetooth: Interleave with allowlist scan

2020-11-10 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

Changes in v8:
- Simplified logic in __hci_update_interleaved_scan
- remove hdev->name when calling bt_dev_dbg
- remove 'default' in hci_req_add_le_interleaved_scan switch block
- remove {} around :1915

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 128 +--
 net/bluetooth/mgmt_config.c  |  10 +++
 4 files changed, 145 insertions(+), 7 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..cfede18709d8f 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   INTERLEAVE_SCAN_NONE,
+   INTERLEAVE_SCAN_NO_FILTER,
+   INTERLEAVE_SCAN_ALLOWLIST
+   } interleave_scan_state;
+
+   struct delayed_work interleave_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 502552d6e9aff..65b7b74baba4c 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 048d4db9d4ea5..2fd56ee21d31f 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,53 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "cancelling interleave scan");
+
+   cancel_delayed_work_sync(>interleave_scan);
+
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
+}
+
+/* Return true if interleave_scan wasn't started until exiting this function,
+ * otherwise, return false
+ */
+static bool __hci_update_interleaved_scan(struct hci_dev *hdev)
+{
+   /* If there is at least one ADV monitors and one pending LE connection
+* or one device to be scanned for, we should alternate between
+* allowlist scan and one without any filters to save power.
+*/
+   bool should_interleaving = hci_is_adv_monitoring(hdev) &&
+  !(list_empty(>pend_le_conns) &&
+list_empty(>pend_le_reports));
+   bool is_interleaving = is_interleave_scanning(hdev);
+
+   if (should_interleaving && !is_interleaving) {
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "starting interleave scan");
+   return true;
+   }
+
+   if (!should_interleaving && is_interleaving)
+   cancel_interleave_scan(hdev);
+
+   return false;
+}
+
 /* This function controls the background scanning based on hdev->pend_le_conns
  * list. If there are pending LE connection we start the background scanning,
  * otherwise we stop it.
@@ -450,8 +497,7 @@ static void __hci_update_background_scan(struct hci_request 
*req)
hci_req_add_le_scan_disable(req, false);
 
hci_req_add_le_passive_scan(req);
-
-   BT_DBG("%s starting background scanning", hdev->name);
+   b

[PATCH v8 1/6] Bluetooth: Replace BT_DBG with bt_dev_dbg in HCI request

2020-11-10 Thread Howard Chung
This replaces the BT_DBG function to bt_dev_dbg as it is cleaner to show
the controller index in the debug message.

Signed-off-by: Howard Chung 
---

Changes in v8:
- Simplified logic in __hci_update_interleaved_scan
- Remove hdev->name when calling bt_dev_dbg
- Remove 'default' in hci_req_add_le_interleaved_scan switch block
- Remove {} around :1915
- Update commit message and title in v7 4/5
- Add a cleanup patch for replacing BT_DBG with bt_dev_dbg

Changes in v7:
- Fix bt_dev_warn argument type warning

Changes in v6:
- Set parameter EnableAdvMonInterleaveScan to 1 byte long

Changes in v5:
- Rename 'adv_monitor' from many functions/variables
- Move __hci_update_interleaved_scan into hci_req_add_le_passive_scan
- Update the logic of update_adv_monitor_scan_state

Changes in v4:
- Rebase to bluetooth-next/master (previous 2 patches are applied)
- Fix over 80 chars limit in mgmt_config.c
- Set EnableAdvMonInterleaveScan default to Disable

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 net/bluetooth/hci_request.c | 52 ++---
 1 file changed, 26 insertions(+), 26 deletions(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 6a74097c50d34..048d4db9d4ea5 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -58,7 +58,7 @@ static int req_run(struct hci_request *req, 
hci_req_complete_t complete,
struct sk_buff *skb;
unsigned long flags;
 
-   BT_DBG("length %u", skb_queue_len(>cmd_q));
+   bt_dev_dbg(hdev, "length %u", skb_queue_len(>cmd_q));
 
/* If an error occurred during request building, remove all HCI
 * commands queued on the HCI request queue.
@@ -102,7 +102,7 @@ int hci_req_run_skb(struct hci_request *req, 
hci_req_complete_skb_t complete)
 static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
  struct sk_buff *skb)
 {
-   BT_DBG("%s result 0x%2.2x", hdev->name, result);
+   bt_dev_dbg(hdev, "result 0x%2.2x", result);
 
if (hdev->req_status == HCI_REQ_PEND) {
hdev->req_result = result;
@@ -115,7 +115,7 @@ static void hci_req_sync_complete(struct hci_dev *hdev, u8 
result, u16 opcode,
 
 void hci_req_sync_cancel(struct hci_dev *hdev, int err)
 {
-   BT_DBG("%s err 0x%2.2x", hdev->name, err);
+   bt_dev_dbg(hdev, "err 0x%2.2x", err);
 
if (hdev->req_status == HCI_REQ_PEND) {
hdev->req_result = err;
@@ -131,7 +131,7 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 
opcode, u32 plen,
struct sk_buff *skb;
int err = 0;
 
-   BT_DBG("%s", hdev->name);
+   bt_dev_dbg(hdev, "");
 
hci_req_init(, hdev);
 
@@ -167,7 +167,7 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 
opcode, u32 plen,
skb = hdev->req_skb;
hdev->req_skb = NULL;
 
-   BT_DBG("%s end: err %d", hdev->name, err);
+   bt_dev_dbg(hdev, "end: err %d", err);
 
if (err < 0) {
kfree_skb(skb);
@@ -196,7 +196,7 @@ int __hci_req_sync(struct hci_dev *hdev, int (*func)(struct 
hci_request *req,
struct hci_request req;
int err = 0;
 
-   BT_DBG("%s start", hdev->name);
+   bt_dev_dbg(hdev, "start");
 
hci_req_init(, hdev);
 
@@ -260,7 +260,7 @@ int __hci_req_sync(struct hci_dev *hdev, int (*func)(struct 
hci_request *req,
hdev->req_skb = NULL;
hdev->req_status = hdev->req_result = 0;
 
-   BT_DBG("%s end: err %d", hdev->name, err);
+   bt_dev_dbg(hdev, "end: err %d", err);
 
return err;
 }
@@ -300,7 +300,7 @@ struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 
opcode, u32 plen,
if (plen)
skb_put_data(skb, param, plen);
 
-   BT_DBG("skb len %d", skb->len);
+   bt_dev_dbg(hdev, "skb len %d", skb->len);
 
hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
hci_skb_opcode(skb) = opcode;
@@ -315,7 +315,7 @@ void hci_req_add_ev(struct hci_request *req, u16 opcode, 
u32 plen,
struct hci_dev *hdev = req->hdev;
struct sk_buff *skb;
 
-   BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
+   bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
 
/* If an error occurred during request building, there is no point in
 * queueing the HCI command. We can simply return.
@@ -413,8 +413,8 @@ static void __hci_update_background_scan(struct hci_request 
*req)
 */
hci_discovery_filter_clear(hdev);
 
-   BT_DBG("%s ADV monitoring is %s", hdev->name,
-  hci_is_adv_monitoring(hdev) ? "on" : "off");
+   

[PATCH v7 3/5] Bluetooth: Handle active scan case

2020-11-08 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index b615b981be9d6..396960ef54a13 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3092,8 +3092,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.29.2.222.g5d2a92d10f8-goog



[PATCH v7 4/5] mgmt: Add supports of variable length parameter in mgmt_config

2020-11-08 Thread Howard Chung
This adds support of variable length parameter in mgmt_config.

Signed-off-by: Howard Chung 
---

(no changes since v1)

 net/bluetooth/mgmt_config.c | 140 +---
 1 file changed, 84 insertions(+), 56 deletions(-)

diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 2d3ad288c78ac..b735e59c7fd51 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -11,72 +11,100 @@
 #include "mgmt_util.h"
 #include "mgmt_config.h"
 
-#define HDEV_PARAM_U16(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(hdev->_param_name_) } \
-}
+#define HDEV_PARAM_U16(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __le16 value; \
+   } __packed _param_name_
 
-#define HDEV_PARAM_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) } \
-}
+#define TLV_SET_U16(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+ cpu_to_le16(hdev->_param_name_) \
+   }
+
+#define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+ cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) \
+   }
 
 int read_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
   u16 data_len)
 {
-   struct {
-   struct mgmt_tlv entry;
-   union {
-   /* This is a simplification for now since all values
-* are 16 bits.  In the future, this code may need
-* refactoring to account for variable length values
-* and properly calculate the required buffer size.
-*/
-   __le16 value;
-   };
-   } __packed params[] = {
+   int ret;
+   struct mgmt_rp_read_def_system_config {
/* Please see mgmt-api.txt for documentation of these values */
-   HDEV_PARAM_U16(0x, def_page_scan_type),
-   HDEV_PARAM_U16(0x0001, def_page_scan_int),
-   HDEV_PARAM_U16(0x0002, def_page_scan_window),
-   HDEV_PARAM_U16(0x0003, def_inq_scan_type),
-   HDEV_PARAM_U16(0x0004, def_inq_scan_int),
-   HDEV_PARAM_U16(0x0005, def_inq_scan_window),
-   HDEV_PARAM_U16(0x0006, def_br_lsto),
-   HDEV_PARAM_U16(0x0007, def_page_timeout),
-   HDEV_PARAM_U16(0x0008, sniff_min_interval),
-   HDEV_PARAM_U16(0x0009, sniff_max_interval),
-   HDEV_PARAM_U16(0x000a, le_adv_min_interval),
-   HDEV_PARAM_U16(0x000b, le_adv_max_interval),
-   HDEV_PARAM_U16(0x000c, def_multi_adv_rotation_duration),
-   HDEV_PARAM_U16(0x000d, le_scan_interval),
-   HDEV_PARAM_U16(0x000e, le_scan_window),
-   HDEV_PARAM_U16(0x000f, le_scan_int_suspend),
-   HDEV_PARAM_U16(0x0010, le_scan_window_suspend),
-   HDEV_PARAM_U16(0x0011, le_scan_int_discovery),
-   HDEV_PARAM_U16(0x0012, le_scan_window_discovery),
-   HDEV_PARAM_U16(0x0013, le_scan_int_adv_monitor),
-   HDEV_PARAM_U16(0x0014, le_scan_window_adv_monitor),
-   HDEV_PARAM_U16(0x0015, le_scan_int_connect),
-   HDEV_PARAM_U16(0x0016, le_scan_window_connect),
-   HDEV_PARAM_U16(0x0017, le_conn_min_interval),
-   HDEV_PARAM_U16(0x0018, le_conn_max_interval),
-   HDEV_PARAM_U16(0x0019, le_conn_latency),
-   HDEV_PARAM_U16(0x001a, le_supv_timeout),
-   HDEV_PARAM_U16_JIFFIES_TO_MSECS(0x001b,
-   def_le_autoconnect_timeout),
-   HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
-   HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(def_page_scan_type);
+   HDEV_PARAM_U16(def_page_scan_int);
+   HDEV_PARAM_U16(def_page_scan_window);
+   HDEV_PARAM_U16(def_inq_scan_type);
+   HDEV_PARAM_U16(def_inq_scan_int);
+   HDEV_PARAM_U16(def_inq_scan_window);
+   HDEV_PARAM_U16(def_br_lsto);
+   HDEV_PARAM_U16(def_page_timeout);
+   HDEV_PARAM_U16(sniff_min_interval);
+   HDEV_PARAM_U16(sniff_max_interval);
+   HDEV_PARAM_U16(le_adv_min_interval);
+   HDEV_PARAM_U16(le_adv_max_interval);
+   HDEV_PARAM_U16(def_multi_adv_rotation_duration);
+   HDEV_PARAM_U16(le_scan_interval);
+   HDEV_PARAM_U16(le_scan_window);
+   HDEV_PARAM_U16(le_scan_int_suspend)

[PATCH v7 5/5] Bluetooth: Add toggle to switch off interleave scan

2020-11-08 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Reviewed-by: Alain Michaud 
Signed-off-by: Howard Chung 
---

Changes in v7:
- Fix bt_dev_warn arguemnt type warning

Changes in v6:
- Set EnableAdvMonInterleaveScan to 1 byte long

Changes in v4:
- Set EnableAdvMonInterleaveScan default to Disable
- Fix 80 chars limit in mgmt_config.c

 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_core.c |  1 +
 net/bluetooth/hci_request.c  |  3 ++-
 net/bluetooth/mgmt_config.c  | 41 +---
 4 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index cfede18709d8f..63c6d656564a1 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u8enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 65b7b74baba4c..b7cb7bfe250bd 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x00; /* Default to disable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 396960ef54a13..85948c73c72b3 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1059,7 +1059,8 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
  _addr_type))
return;
 
-   if (__hci_update_interleaved_scan(hdev))
+   if (hdev->enable_advmon_interleave_scan &&
+   __hci_update_interleaved_scan(hdev))
return;
 
bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index b735e59c7fd51..a3dfcdcb3b78d 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -17,12 +17,24 @@
__le16 value; \
} __packed _param_name_
 
+#define HDEV_PARAM_U8(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __u8 value; \
+   } __packed _param_name_
+
 #define TLV_SET_U16(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
  cpu_to_le16(hdev->_param_name_) \
}
 
+#define TLV_SET_U8(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u8) }, \
+ hdev->_param_name_ \
+   }
+
 #define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
@@ -65,6 +77,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
HDEV_PARAM_U16(def_le_autoconnect_timeout);
HDEV_PARAM_U16(advmon_allowlist_duration);
HDEV_PARAM_U16(advmon_no_filter_duration);
+   HDEV_PARAM_U8(enable_advmon_interleave_scan);
} __packed rp = {
TLV_SET_U16(0x, def_page_scan_type),
TLV_SET_U16(0x0001, def_page_scan_int),
@@ -97,6 +110,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 def_le_autoconnect_timeout),
TLV_SET_U16(0x001d, advmon_allowlist_duration),
TLV_SET_U16(0x001e, advmon_no_filter_duration),
+   TLV_SET_U8(0x001f, enable_advmon_interleave_scan),
};
 
bt_dev_dbg(hdev, "sock %p", sk);
@@ -109,6 +123,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 
 #define TO_TLV(x)  ((struct mgmt_tlv *)(x))
 #define TLV_GET_LE16(tlv)  le16_to_cpu(*((__le16 *)(TO_TLV(tlv)->value)))
+#define TLV_GET_LE8(tlv)   le16_to_cpu(*((__u8 *)(TO_TLV(tlv)->value)))
 
 int set_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
  u16 data_len)
@@ -125,6 +140,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
/* First pass to validate the tlv */
while (buffer_left >= sizeof(struct mgmt_tlv)) {
const u8 len = TO_TLV(buffer)->length;
+   size_t exp_type_len;
const u16 exp_len = sizeof(struct mgmt_tlv) +
len

[PATCH v7 1/5] Bluetooth: Interleave with allowlist scan

2020-11-08 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

Changes in v7:
- Fix bt_dev_warn argument type warning

Changes in v6:
- Set parameter EnableAdvMonInterleaveScan to 1 byte long

Changes in v5:
- Rename 'adv_monitor' from many functions/variables
- Move __hci_update_interleaved_scan into hci_req_add_le_passive_scan
- Update the logic of update_adv_monitor_scan_state

Changes in v4:
- Rebase to bluetooth-next/master (previous 2 patches are applied)
- Fix over 80 chars limit in mgmt_config.c
- Set EnableAdvMonInterleaveScan default to Disable

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 136 +--
 net/bluetooth/mgmt_config.c  |  10 +++
 4 files changed, 153 insertions(+), 7 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..cfede18709d8f 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   INTERLEAVE_SCAN_NONE,
+   INTERLEAVE_SCAN_NO_FILTER,
+   INTERLEAVE_SCAN_ALLOWLIST
+   } interleave_scan_state;
+
+   struct delayed_work interleave_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 502552d6e9aff..65b7b74baba4c 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 6f12bab4d2fa6..70ea126f56282 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,58 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "%s cancelling interleave scan", hdev->name);
+
+   cancel_delayed_work_sync(>interleave_scan);
+
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
+}
+
+/* Return true if interleave_scan wasn't started until exiting this function,
+ * otherwise, return false
+ */
+static bool __hci_update_interleaved_scan(struct hci_dev *hdev)
+{
+   if (hci_is_adv_monitoring(hdev) &&
+   !(list_empty(>pend_le_conns) &&
+ list_empty(>pend_le_reports))) {
+   if (!is_interleave_scanning(hdev)) {
+   /* If there is at least one ADV monitors and one pending
+* LE connection or one device to be scanned for, we
+* should alternate between allowlist scan and one
+* without any filters to save power.
+*/
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "%s starting interleave scan",
+  hdev->name);
+   return true;
+   }
+   }
+
+   if (is_interleave_scanning(hdev)) {
+   /* If the interleave condition no longer holds, cancel
+* the existed interleave scan.
+ 

[PATCH v7 2/5] Bluetooth: Handle system suspend resume case

2020-11-08 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Commit-changes 5:
- Remove the change in hci_req_config_le_suspend_scan

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 70ea126f56282..b615b981be9d6 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1284,8 +1284,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.29.2.222.g5d2a92d10f8-goog



[PATCH v6 4/5] mgmt: Add supports of variable length parameter in mgmt_config

2020-10-30 Thread Howard Chung
This adds support of variable length parameter in mgmt_config.

Signed-off-by: Howard Chung 
---

(no changes since v1)

 net/bluetooth/mgmt_config.c | 140 +---
 1 file changed, 84 insertions(+), 56 deletions(-)

diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 2d3ad288c78ac..b735e59c7fd51 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -11,72 +11,100 @@
 #include "mgmt_util.h"
 #include "mgmt_config.h"
 
-#define HDEV_PARAM_U16(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(hdev->_param_name_) } \
-}
+#define HDEV_PARAM_U16(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __le16 value; \
+   } __packed _param_name_
 
-#define HDEV_PARAM_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) } \
-}
+#define TLV_SET_U16(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+ cpu_to_le16(hdev->_param_name_) \
+   }
+
+#define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+ cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) \
+   }
 
 int read_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
   u16 data_len)
 {
-   struct {
-   struct mgmt_tlv entry;
-   union {
-   /* This is a simplification for now since all values
-* are 16 bits.  In the future, this code may need
-* refactoring to account for variable length values
-* and properly calculate the required buffer size.
-*/
-   __le16 value;
-   };
-   } __packed params[] = {
+   int ret;
+   struct mgmt_rp_read_def_system_config {
/* Please see mgmt-api.txt for documentation of these values */
-   HDEV_PARAM_U16(0x, def_page_scan_type),
-   HDEV_PARAM_U16(0x0001, def_page_scan_int),
-   HDEV_PARAM_U16(0x0002, def_page_scan_window),
-   HDEV_PARAM_U16(0x0003, def_inq_scan_type),
-   HDEV_PARAM_U16(0x0004, def_inq_scan_int),
-   HDEV_PARAM_U16(0x0005, def_inq_scan_window),
-   HDEV_PARAM_U16(0x0006, def_br_lsto),
-   HDEV_PARAM_U16(0x0007, def_page_timeout),
-   HDEV_PARAM_U16(0x0008, sniff_min_interval),
-   HDEV_PARAM_U16(0x0009, sniff_max_interval),
-   HDEV_PARAM_U16(0x000a, le_adv_min_interval),
-   HDEV_PARAM_U16(0x000b, le_adv_max_interval),
-   HDEV_PARAM_U16(0x000c, def_multi_adv_rotation_duration),
-   HDEV_PARAM_U16(0x000d, le_scan_interval),
-   HDEV_PARAM_U16(0x000e, le_scan_window),
-   HDEV_PARAM_U16(0x000f, le_scan_int_suspend),
-   HDEV_PARAM_U16(0x0010, le_scan_window_suspend),
-   HDEV_PARAM_U16(0x0011, le_scan_int_discovery),
-   HDEV_PARAM_U16(0x0012, le_scan_window_discovery),
-   HDEV_PARAM_U16(0x0013, le_scan_int_adv_monitor),
-   HDEV_PARAM_U16(0x0014, le_scan_window_adv_monitor),
-   HDEV_PARAM_U16(0x0015, le_scan_int_connect),
-   HDEV_PARAM_U16(0x0016, le_scan_window_connect),
-   HDEV_PARAM_U16(0x0017, le_conn_min_interval),
-   HDEV_PARAM_U16(0x0018, le_conn_max_interval),
-   HDEV_PARAM_U16(0x0019, le_conn_latency),
-   HDEV_PARAM_U16(0x001a, le_supv_timeout),
-   HDEV_PARAM_U16_JIFFIES_TO_MSECS(0x001b,
-   def_le_autoconnect_timeout),
-   HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
-   HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(def_page_scan_type);
+   HDEV_PARAM_U16(def_page_scan_int);
+   HDEV_PARAM_U16(def_page_scan_window);
+   HDEV_PARAM_U16(def_inq_scan_type);
+   HDEV_PARAM_U16(def_inq_scan_int);
+   HDEV_PARAM_U16(def_inq_scan_window);
+   HDEV_PARAM_U16(def_br_lsto);
+   HDEV_PARAM_U16(def_page_timeout);
+   HDEV_PARAM_U16(sniff_min_interval);
+   HDEV_PARAM_U16(sniff_max_interval);
+   HDEV_PARAM_U16(le_adv_min_interval);
+   HDEV_PARAM_U16(le_adv_max_interval);
+   HDEV_PARAM_U16(def_multi_adv_rotation_duration);
+   HDEV_PARAM_U16(le_scan_interval);
+   HDEV_PARAM_U16(le_scan_window);
+   HDEV_PARAM_U16(le_scan_int_suspend)

[PATCH v6 5/5] Bluetooth: Add toggle to switch off interleave scan

2020-10-30 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Reviewed-by: Alain Michaud 
Signed-off-by: Howard Chung 
---

Changes in v6:
- Set EnableAdvMonInterleaveScan to 1 byte long

Changes in v4:
- Set EnableAdvMonInterleaveScan default to Disable
- Fix 80 chars limit in mgmt_config.c

 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_core.c |  1 +
 net/bluetooth/hci_request.c  |  3 ++-
 net/bluetooth/mgmt_config.c  | 41 +---
 4 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index cfede18709d8f..63c6d656564a1 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u8enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 65b7b74baba4c..b7cb7bfe250bd 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x00; /* Default to disable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 396960ef54a13..85948c73c72b3 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1059,7 +1059,8 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
  _addr_type))
return;
 
-   if (__hci_update_interleaved_scan(hdev))
+   if (hdev->enable_advmon_interleave_scan &&
+   __hci_update_interleaved_scan(hdev))
return;
 
bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index b735e59c7fd51..a6a6f0338be2e 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -17,12 +17,24 @@
__le16 value; \
} __packed _param_name_
 
+#define HDEV_PARAM_U8(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __u8 value; \
+   } __packed _param_name_
+
 #define TLV_SET_U16(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
  cpu_to_le16(hdev->_param_name_) \
}
 
+#define TLV_SET_U8(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u8) }, \
+ hdev->_param_name_ \
+   }
+
 #define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
{ \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
@@ -65,6 +77,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
HDEV_PARAM_U16(def_le_autoconnect_timeout);
HDEV_PARAM_U16(advmon_allowlist_duration);
HDEV_PARAM_U16(advmon_no_filter_duration);
+   HDEV_PARAM_U8(enable_advmon_interleave_scan);
} __packed rp = {
TLV_SET_U16(0x, def_page_scan_type),
TLV_SET_U16(0x0001, def_page_scan_int),
@@ -97,6 +110,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 def_le_autoconnect_timeout),
TLV_SET_U16(0x001d, advmon_allowlist_duration),
TLV_SET_U16(0x001e, advmon_no_filter_duration),
+   TLV_SET_U8(0x001f, enable_advmon_interleave_scan),
};
 
bt_dev_dbg(hdev, "sock %p", sk);
@@ -109,6 +123,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 
 #define TO_TLV(x)  ((struct mgmt_tlv *)(x))
 #define TLV_GET_LE16(tlv)  le16_to_cpu(*((__le16 *)(TO_TLV(tlv)->value)))
+#define TLV_GET_LE8(tlv)   le16_to_cpu(*((__u8 *)(TO_TLV(tlv)->value)))
 
 int set_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
  u16 data_len)
@@ -125,6 +140,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
/* First pass to validate the tlv */
while (buffer_left >= sizeof(struct mgmt_tlv)) {
const u8 len = TO_TLV(buffer)->length;
+   u8 exp_type_len;
const u16 exp_len = sizeof(struct mgmt_tlv) +
len;
const u16 type = le16_to_cpu(TO_TLV(buffer)->

[PATCH v6 3/5] Bluetooth: Handle active scan case

2020-10-30 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index b615b981be9d6..396960ef54a13 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3092,8 +3092,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.29.1.341.ge80a0c044ae-goog



[PATCH v6 2/5] Bluetooth: Handle system suspend resume case

2020-10-30 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Commit-changes 5:
- Remove the change in hci_req_config_le_suspend_scan

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 70ea126f56282..b615b981be9d6 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1284,8 +1284,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.29.1.341.ge80a0c044ae-goog



[PATCH v6 1/5] Bluetooth: Interleave with allowlist scan

2020-10-30 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

Changes in v6:
- Set parameter EnableAdvMonInterleaveScan to 1 byte long

Changes in v5:
- Rename 'adv_monitor' from many functions/variables
- Move __hci_update_interleaved_scan into hci_req_add_le_passive_scan
- Update the logic of update_adv_monitor_scan_state

Changes in v4:
- Rebase to bluetooth-next/master (previous 2 patches are applied)
- Fix over 80 chars limit in mgmt_config.c
- Set EnableAdvMonInterleaveScan default to Disable

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 136 +--
 net/bluetooth/mgmt_config.c  |  10 +++
 4 files changed, 153 insertions(+), 7 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..cfede18709d8f 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   INTERLEAVE_SCAN_NONE,
+   INTERLEAVE_SCAN_NO_FILTER,
+   INTERLEAVE_SCAN_ALLOWLIST
+   } interleave_scan_state;
+
+   struct delayed_work interleave_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 502552d6e9aff..65b7b74baba4c 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 6f12bab4d2fa6..70ea126f56282 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,58 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "%s cancelling interleave scan", hdev->name);
+
+   cancel_delayed_work_sync(>interleave_scan);
+
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
+}
+
+/* Return true if interleave_scan wasn't started until exiting this function,
+ * otherwise, return false
+ */
+static bool __hci_update_interleaved_scan(struct hci_dev *hdev)
+{
+   if (hci_is_adv_monitoring(hdev) &&
+   !(list_empty(>pend_le_conns) &&
+ list_empty(>pend_le_reports))) {
+   if (!is_interleave_scanning(hdev)) {
+   /* If there is at least one ADV monitors and one pending
+* LE connection or one device to be scanned for, we
+* should alternate between allowlist scan and one
+* without any filters to save power.
+*/
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "%s starting interleave scan",
+  hdev->name);
+   return true;
+   }
+   }
+
+   if (is_interleave_scanning(hdev)) {
+   /* If the interleave condition no longer holds, cancel
+* the existed interleave scan.
+*/
+   cancel_interleave_scan(hdev);
+

[PATCH] mgmt: Add supports of variable length parameter in mgmt_config

2020-10-14 Thread Howard Chung
This adds support of variable length parameter in mgmt_config.

Signed-off-by: Howard Chung 

Reviewed-by: Alain Michaud 

---

 net/bluetooth/mgmt_config.c | 134 +---
 1 file changed, 80 insertions(+), 54 deletions(-)

diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index b30b571f8caf8..1e3cbeacb09b4 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -11,70 +11,96 @@
 #include "mgmt_util.h"
 #include "mgmt_config.h"
 
-#define HDEV_PARAM_U16(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(hdev->_param_name_) } \
-}
+#define HDEV_PARAM_U16(_param_name_) \
+   struct {\
+   struct mgmt_tlv entry; \
+   __le16 value; \
+   } __packed _param_name_
 
-#define HDEV_PARAM_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
-{ \
-   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) } \
-}
+#define TLV_SET_U16(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+   cpu_to_le16(hdev->_param_name_) \
+   }
+
+#define TLV_SET_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
+   { \
+   { cpu_to_le16(_param_code_), sizeof(__u16) }, \
+   cpu_to_le16(jiffies_to_msecs(hdev->_param_name_)) \
+   }
 
 int read_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
   u16 data_len)
 {
-   struct {
-   struct mgmt_tlv entry;
-   union {
-   /* This is a simplification for now since all values
-* are 16 bits.  In the future, this code may need
-* refactoring to account for variable length values
-* and properly calculate the required buffer size.
-*/
-   __le16 value;
-   };
-   } __packed params[] = {
+   int ret;
+   struct mgmt_rp_read_def_system_config {
/* Please see mgmt-api.txt for documentation of these values */
-   HDEV_PARAM_U16(0x, def_page_scan_type),
-   HDEV_PARAM_U16(0x0001, def_page_scan_int),
-   HDEV_PARAM_U16(0x0002, def_page_scan_window),
-   HDEV_PARAM_U16(0x0003, def_inq_scan_type),
-   HDEV_PARAM_U16(0x0004, def_inq_scan_int),
-   HDEV_PARAM_U16(0x0005, def_inq_scan_window),
-   HDEV_PARAM_U16(0x0006, def_br_lsto),
-   HDEV_PARAM_U16(0x0007, def_page_timeout),
-   HDEV_PARAM_U16(0x0008, sniff_min_interval),
-   HDEV_PARAM_U16(0x0009, sniff_max_interval),
-   HDEV_PARAM_U16(0x000a, le_adv_min_interval),
-   HDEV_PARAM_U16(0x000b, le_adv_max_interval),
-   HDEV_PARAM_U16(0x000c, def_multi_adv_rotation_duration),
-   HDEV_PARAM_U16(0x000d, le_scan_interval),
-   HDEV_PARAM_U16(0x000e, le_scan_window),
-   HDEV_PARAM_U16(0x000f, le_scan_int_suspend),
-   HDEV_PARAM_U16(0x0010, le_scan_window_suspend),
-   HDEV_PARAM_U16(0x0011, le_scan_int_discovery),
-   HDEV_PARAM_U16(0x0012, le_scan_window_discovery),
-   HDEV_PARAM_U16(0x0013, le_scan_int_adv_monitor),
-   HDEV_PARAM_U16(0x0014, le_scan_window_adv_monitor),
-   HDEV_PARAM_U16(0x0015, le_scan_int_connect),
-   HDEV_PARAM_U16(0x0016, le_scan_window_connect),
-   HDEV_PARAM_U16(0x0017, le_conn_min_interval),
-   HDEV_PARAM_U16(0x0018, le_conn_max_interval),
-   HDEV_PARAM_U16(0x0019, le_conn_latency),
-   HDEV_PARAM_U16(0x001a, le_supv_timeout),
-   HDEV_PARAM_U16_JIFFIES_TO_MSECS(0x001b,
-   def_le_autoconnect_timeout),
+   HDEV_PARAM_U16(def_page_scan_type);
+   HDEV_PARAM_U16(def_page_scan_int);
+   HDEV_PARAM_U16(def_page_scan_window);
+   HDEV_PARAM_U16(def_inq_scan_type);
+   HDEV_PARAM_U16(def_inq_scan_int);
+   HDEV_PARAM_U16(def_inq_scan_window);
+   HDEV_PARAM_U16(def_br_lsto);
+   HDEV_PARAM_U16(def_page_timeout);
+   HDEV_PARAM_U16(sniff_min_interval);
+   HDEV_PARAM_U16(sniff_max_interval);
+   HDEV_PARAM_U16(le_adv_min_interval);
+   HDEV_PARAM_U16(le_adv_max_interval);
+   HDEV_PARAM_U16(def_multi_adv_rotation_duration);
+   HDEV_PARAM_U16(le_scan_interval);
+   HDEV_PARAM_U16(le_scan_window);
+   HDEV_PARAM_U16(le_scan_int_suspend);
+   HDEV_PARAM_U16(le_scan_window_suspend);
+   HDEV_PARAM_U16(le_scan_int_discovery);
+   HDEV_PA

[PATCH v7 3/4] Bluetooth: Handle active scan case

2020-09-29 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index db44680fbe9c9..4048c82d4257f 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3083,8 +3083,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.28.0.709.gb0816b6eb0-goog



[PATCH v7 4/4] Bluetooth: Add toggle to switch off interleave scan

2020-09-29 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---

Changes in v7:
- Fix test bot warning

Changes in v6:
- Change the type of enable_advmon_interleave_scan to u8

Changes in v4:
- Set EnableAdvMonInterleaveScan default to Disable
- Fix 80 chars limit in mgmt_config.c

 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_core.c |  1 +
 net/bluetooth/hci_request.c  |  3 ++-
 net/bluetooth/mgmt_config.c  | 41 ++--
 4 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index cfede18709d8f..63c6d656564a1 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u8enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 6c8850149265a..c37b2d5395abc 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x00; /* Default to disable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 4048c82d4257f..23381f263678b 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1057,7 +1057,8 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
  _addr_type))
return;
 
-   if (__hci_update_interleaved_scan(hdev))
+   if (hdev->enable_advmon_interleave_scan &&
+   __hci_update_interleaved_scan(hdev))
return;
 
/* Adding or removing entries from the white list must
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 2d3ad288c78ac..a9d580b4aaad1 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -14,7 +14,13 @@
 #define HDEV_PARAM_U16(_param_code_, _param_name_) \
 { \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
-   { cpu_to_le16(hdev->_param_name_) } \
+   { .value_le16 = cpu_to_le16(hdev->_param_name_) } \
+}
+
+#define HDEV_PARAM_U8(_param_code_, _param_name_) \
+{ \
+   { (_param_code_), sizeof(__u8) }, \
+   { .value_u8 = hdev->_param_name_ } \
 }
 
 #define HDEV_PARAM_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
@@ -30,11 +36,12 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
struct mgmt_tlv entry;
union {
/* This is a simplification for now since all values
-* are 16 bits.  In the future, this code may need
+* are fixed bits.  In the future, this code may need
 * refactoring to account for variable length values
 * and properly calculate the required buffer size.
 */
-   __le16 value;
+   __le16 value_le16;
+   __u8 value_u8;
};
} __packed params[] = {
/* Please see mgmt-api.txt for documentation of these values */
@@ -69,6 +76,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
def_le_autoconnect_timeout),
HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U8(0x001f, enable_advmon_interleave_scan),
};
struct mgmt_rp_read_def_system_config *rp = (void *)params;
 
@@ -81,7 +89,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 
 #define TO_TLV(x)  ((struct mgmt_tlv *)(x))
 #define TLV_GET_LE16(tlv)  le16_to_cpu(*((__le16 *)(TO_TLV(tlv)->value)))
-
+#define TLV_GET_U8(tlv)(*((__u8 *)(TO_TLV(tlv)->value)))
 int set_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
  u16 data_len)
 {
@@ -100,6 +108,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
const u16 exp_len = sizeof(struct mgmt_tlv) +
len;
const u16 type = le16_to_cpu(TO_TLV(buffer)->type);
+   size_t exp_data_len = 0;
 
if (buffer_left <

[PATCH v7 1/4] Bluetooth: Interleave with allowlist scan

2020-09-29 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

Changes in v7:
- Fix test bot warning

Changes in v6:
- Change the type of enable_advmon_interleave_scan to u8

Changes in v5:
- Rename 'adv_monitor' from many functions/variables
- Move __hci_update_interleaved_scan into hci_req_add_le_passive_scan
- Update the logic of update_adv_monitor_scan_state

Changes in v4:
- Rebase to bluetooth-next/master (previous 2 patches are applied)
- Fix over 80 chars limit in mgmt_config.c
- Set EnableAdvMonInterleaveScan default to Disable

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 133 +--
 net/bluetooth/mgmt_config.c  |  10 +++
 4 files changed, 150 insertions(+), 7 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..cfede18709d8f 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   INTERLEAVE_SCAN_NONE,
+   INTERLEAVE_SCAN_NO_FILTER,
+   INTERLEAVE_SCAN_ALLOWLIST
+   } interleave_scan_state;
+
+   struct delayed_work interleave_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index f30a1f5950e15..6c8850149265a 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d2b06f5c93804..ba3016cc0b573 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,56 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "%s cancelling interleave scan", hdev->name);
+
+   cancel_delayed_work_sync(>interleave_scan);
+
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
+}
+
+/* Return true if interleave_scan wasn't started until exiting this function,
+ * otherwise, return false
+ */
+static bool __hci_update_interleaved_scan(struct hci_dev *hdev)
+{
+   if (hci_is_adv_monitoring(hdev) &&
+   !(list_empty(>pend_le_conns) &&
+ list_empty(>pend_le_reports))) {
+   if (!is_interleave_scanning(hdev)) {
+   /* If there is at least one ADV monitors and one pending
+* LE connection or one device to be scanned for, we
+* should alternate between allowlist scan and one
+* without any filters to save power.
+*/
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "%s starting interleave scan",
+  hdev->name);
+   return true;
+   }
+   } else if (is_interleave_scanning(hdev)) {
+   /* If the interleave condition no longer holds, cancel
+* the existed interleave scan.
+*/
+   ca

[PATCH v7 2/4] Bluetooth: Handle system suspend resume case

2020-09-29 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v5)

Changes in v5:
- Remove the change in hci_req_config_le_suspend_scan

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index ba3016cc0b573..db44680fbe9c9 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1281,8 +1281,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.28.0.709.gb0816b6eb0-goog



[PATCH v6 1/4] Bluetooth: Interleave with allowlist scan

2020-09-28 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

Changes in v6:
- Change the type of enable_advmon_interleave_scan to u8

Changes in v5:
- Rename 'adv_monitor' from many functions/variables
- Move __hci_update_interleaved_scan into hci_req_add_le_passive_scan
- Update the logic of update_adv_monitor_scan_state

Changes in v4:
- Rebase to bluetooth-next/master (previous 2 patches are applied)
- Fix over 80 chars limit in mgmt_config.c
- Set EnableAdvMonInterleaveScan default to Disable

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 133 +--
 net/bluetooth/mgmt_config.c  |  10 +++
 4 files changed, 150 insertions(+), 7 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..cfede18709d8f 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   INTERLEAVE_SCAN_NONE,
+   INTERLEAVE_SCAN_NO_FILTER,
+   INTERLEAVE_SCAN_ALLOWLIST
+   } interleave_scan_state;
+
+   struct delayed_work interleave_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index f30a1f5950e15..6c8850149265a 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d2b06f5c93804..ba3016cc0b573 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,56 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "%s cancelling interleave scan", hdev->name);
+
+   cancel_delayed_work_sync(>interleave_scan);
+
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
+}
+
+/* Return true if interleave_scan wasn't started until exiting this function,
+ * otherwise, return false
+ */
+static bool __hci_update_interleaved_scan(struct hci_dev *hdev)
+{
+   if (hci_is_adv_monitoring(hdev) &&
+   !(list_empty(>pend_le_conns) &&
+ list_empty(>pend_le_reports))) {
+   if (!is_interleave_scanning(hdev)) {
+   /* If there is at least one ADV monitors and one pending
+* LE connection or one device to be scanned for, we
+* should alternate between allowlist scan and one
+* without any filters to save power.
+*/
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "%s starting interleave scan",
+  hdev->name);
+   return true;
+   }
+   } else if (is_interleave_scanning(hdev)) {
+   /* If the interleave condition no longer holds, cancel
+* the existed interleave scan.
+*/
+   cancel_interleave_scan(hdev);
+   }
+

[PATCH v6 4/4] Bluetooth: Add toggle to switch off interleave scan

2020-09-28 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---

Changes in v6:
- Change the type of enable_advmon_interleave_scan to u8

Changes in v4:
- Set EnableAdvMonInterleaveScan default to Disable
- Fix 80 chars limit in mgmt_config.c

 include/net/bluetooth/hci_core.h |  1 +
 net/bluetooth/hci_core.c |  1 +
 net/bluetooth/hci_request.c  |  3 ++-
 net/bluetooth/mgmt_config.c  | 39 +++-
 4 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index cfede18709d8f..63c6d656564a1 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u8enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 6c8850149265a..c37b2d5395abc 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x00; /* Default to disable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 4048c82d4257f..23381f263678b 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1057,7 +1057,8 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
  _addr_type))
return;
 
-   if (__hci_update_interleaved_scan(hdev))
+   if (hdev->enable_advmon_interleave_scan &&
+   __hci_update_interleaved_scan(hdev))
return;
 
/* Adding or removing entries from the white list must
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 2d3ad288c78ac..f7906c5d7f01a 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -17,6 +17,12 @@
{ cpu_to_le16(hdev->_param_name_) } \
 }
 
+#define HDEV_PARAM_U8(_param_code_, _param_name_) \
+{ \
+   { (_param_code_), sizeof(__u8) }, \
+   { hdev->_param_name_ } \
+}
+
 #define HDEV_PARAM_U16_JIFFIES_TO_MSECS(_param_code_, _param_name_) \
 { \
{ cpu_to_le16(_param_code_), sizeof(__u16) }, \
@@ -30,11 +36,12 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
struct mgmt_tlv entry;
union {
/* This is a simplification for now since all values
-* are 16 bits.  In the future, this code may need
+* are fixed bits.  In the future, this code may need
 * refactoring to account for variable length values
 * and properly calculate the required buffer size.
 */
-   __le16 value;
+   __le16 value_le16;
+   __u8 value_u8;
};
} __packed params[] = {
/* Please see mgmt-api.txt for documentation of these values */
@@ -69,6 +76,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
def_le_autoconnect_timeout),
HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U8(0x001f, enable_advmon_interleave_scan),
};
struct mgmt_rp_read_def_system_config *rp = (void *)params;
 
@@ -81,7 +89,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
 
 #define TO_TLV(x)  ((struct mgmt_tlv *)(x))
 #define TLV_GET_LE16(tlv)  le16_to_cpu(*((__le16 *)(TO_TLV(tlv)->value)))
-
+#define TLV_GET_U8(tlv)(*((__u8 *)(TO_TLV(tlv)->value)))
 int set_def_system_config(struct sock *sk, struct hci_dev *hdev, void *data,
  u16 data_len)
 {
@@ -100,6 +108,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
const u16 exp_len = sizeof(struct mgmt_tlv) +
len;
const u16 type = le16_to_cpu(TO_TLV(buffer)->type);
+   u8 exp_data_len;
 
if (buffer_left < exp_len) {
bt_dev_warn(hdev, "invalid len left %d, exp >= %d",
@@ -142,20 +151,25 @@ int set_def_system_config(struct sock *sk, struc

[PATCH v6 3/4] Bluetooth: Handle active scan case

2020-09-28 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index db44680fbe9c9..4048c82d4257f 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3083,8 +3083,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v6 2/4] Bluetooth: Handle system suspend resume case

2020-09-28 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v5)

Changes in v5:
- Remove the change in hci_req_config_le_suspend_scan

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index ba3016cc0b573..db44680fbe9c9 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1281,8 +1281,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v5 3/4] Bluetooth: Handle active scan case

2020-09-23 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index db44680fbe9c9..4048c82d4257f 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3083,8 +3083,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v5 2/4] Bluetooth: Handle system suspend resume case

2020-09-23 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

Changes in v5:
- Remove the change in hci_req_config_le_suspend_scan

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index ba3016cc0b573..db44680fbe9c9 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1281,8 +1281,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v5 4/4] Bluetooth: Add toggle to switch off interleave scan

2020-09-23 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---

(no changes since v4)

Changes in v4:
- Set EnableAdvMonInterleaveScan default to Disable
- Fix 80 chars limit in mgmt_config.c

 include/net/bluetooth/hci_core.h | 1 +
 net/bluetooth/hci_core.c | 1 +
 net/bluetooth/hci_request.c  | 3 ++-
 net/bluetooth/mgmt_config.c  | 5 +
 4 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index cfede18709d8f..b0225b80152cc 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u16   enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 6c8850149265a..d5769ae682893 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x;   /* Default to disable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 4048c82d4257f..23381f263678b 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1057,7 +1057,8 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
  _addr_type))
return;
 
-   if (__hci_update_interleaved_scan(hdev))
+   if (hdev->enable_advmon_interleave_scan &&
+   __hci_update_interleaved_scan(hdev))
return;
 
/* Adding or removing entries from the white list must
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 2d3ad288c78ac..34585ab4680b5 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -69,6 +69,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
def_le_autoconnect_timeout),
HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(0x001f, enable_advmon_interleave_scan),
};
struct mgmt_rp_read_def_system_config *rp = (void *)params;
 
@@ -142,6 +143,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
case 0x001b:
case 0x001d:
case 0x001e:
+   case 0x001f:
if (len != sizeof(u16)) {
bt_dev_warn(hdev, "invalid length %d, exp %zu 
for type %d",
len, sizeof(u16), type);
@@ -261,6 +263,9 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
case 0x0001e:
hdev->advmon_no_filter_duration = TLV_GET_LE16(buffer);
break;
+   case 0x0001f:
+   hdev->enable_advmon_interleave_scan = 
TLV_GET_LE16(buffer);
+   break;
default:
bt_dev_warn(hdev, "unsupported parameter %u", type);
break;
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v5 1/4] Bluetooth: Interleave with allowlist scan

2020-09-23 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

Changes in v5:
- Rename 'adv_monitor' from many functions/variables
- Move __hci_update_interleaved_scan into hci_req_add_le_passive_scan
- Update the logic of update_adv_monitor_scan_state

Changes in v4:
- Rebase to bluetooth-next/master (previous 2 patches are applied)
- Fix over 80 chars limit in mgmt_config.c
- Set EnableAdvMonInterleaveScan default to Disable

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 133 +--
 net/bluetooth/mgmt_config.c  |  10 +++
 4 files changed, 150 insertions(+), 7 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..cfede18709d8f 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   INTERLEAVE_SCAN_NONE,
+   INTERLEAVE_SCAN_NO_FILTER,
+   INTERLEAVE_SCAN_ALLOWLIST
+   } interleave_scan_state;
+
+   struct delayed_work interleave_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index f30a1f5950e15..6c8850149265a 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d2b06f5c93804..ba3016cc0b573 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,56 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "%s cancelling interleave scan", hdev->name);
+
+   cancel_delayed_work_sync(>interleave_scan);
+
+   hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
+}
+
+/* Return true if interleave_scan wasn't started until exiting this function,
+ * otherwise, return false
+ */
+static bool __hci_update_interleaved_scan(struct hci_dev *hdev)
+{
+   if (hci_is_adv_monitoring(hdev) &&
+   !(list_empty(>pend_le_conns) &&
+ list_empty(>pend_le_reports))) {
+   if (!is_interleave_scanning(hdev)) {
+   /* If there is at least one ADV monitors and one pending
+* LE connection or one device to be scanned for, we
+* should alternate between allowlist scan and one
+* without any filters to save power.
+*/
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "%s starting interleave scan",
+  hdev->name);
+   return true;
+   }
+   } else if (is_interleave_scanning(hdev)) {
+   /* If the interleave condition no longer holds, cancel
+* the existed interleave scan.
+*/
+   cancel_interleave_scan(hdev);
+   }
+
+   return false;
+}
+
 /* This function controls the background s

[PATCH v4 4/4] Bluetooth: Add toggle to switch off interleave scan

2020-09-20 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---
Changes in v4:
- Set EnableAdvMonInterleaveScan default to Disable
- Fix 80 chars limit in mgmt_config.c

 include/net/bluetooth/hci_core.h | 1 +
 net/bluetooth/hci_core.c | 1 +
 net/bluetooth/hci_request.c  | 3 ++-
 net/bluetooth/mgmt_config.c  | 5 +
 4 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 179350f869fdb..c3253f1cac0c2 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u16   enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 6c8850149265a..d5769ae682893 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x;   /* Default to disable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 1fcf6736811e4..bb38e1dead68f 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -500,7 +500,8 @@ static void __hci_update_background_scan(struct hci_request 
*req)
if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
hci_req_add_le_scan_disable(req, false);
 
-   if (!update_adv_monitor_scan_state(hdev)) {
+   if (!hdev->enable_advmon_interleave_scan ||
+   !update_adv_monitor_scan_state(hdev)) {
hci_req_add_le_passive_scan(req);
bt_dev_dbg(hdev, "%s starting background scanning",
   hdev->name);
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 2d3ad288c78ac..34585ab4680b5 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -69,6 +69,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
def_le_autoconnect_timeout),
HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(0x001f, enable_advmon_interleave_scan),
};
struct mgmt_rp_read_def_system_config *rp = (void *)params;
 
@@ -142,6 +143,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
case 0x001b:
case 0x001d:
case 0x001e:
+   case 0x001f:
if (len != sizeof(u16)) {
bt_dev_warn(hdev, "invalid length %d, exp %zu 
for type %d",
len, sizeof(u16), type);
@@ -261,6 +263,9 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
case 0x0001e:
hdev->advmon_no_filter_duration = TLV_GET_LE16(buffer);
break;
+   case 0x0001f:
+   hdev->enable_advmon_interleave_scan = 
TLV_GET_LE16(buffer);
+   break;
default:
bt_dev_warn(hdev, "unsupported parameter %u", type);
break;
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v4 3/4] Bluetooth: Handle active scan case

2020-09-20 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d9082019b6386..1fcf6736811e4 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3085,8 +3085,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v4 2/4] Bluetooth: Handle system suspend resume case

2020-09-20 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 11 +++
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 89443b48d90ce..d9082019b6386 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1081,6 +1081,9 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
filter_policy |= 0x02;
 
if (hdev->suspended) {
+   /* Block suspend notifier on response */
+   set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks);
+
window = hdev->le_scan_window_suspend;
interval = hdev->le_scan_int_suspend;
} else if (hci_is_le_conn_scanning(hdev)) {
@@ -1167,10 +1170,8 @@ static void hci_req_config_le_suspend_scan(struct 
hci_request *req)
hci_req_add_le_scan_disable(req, false);
 
/* Configure params and enable scanning */
-   hci_req_add_le_passive_scan(req);
+   __hci_update_background_scan(req);
 
-   /* Block suspend notifier on response */
-   set_bit(SUSPEND_SCAN_ENABLE, req->hdev->suspend_tasks);
 }
 
 static void cancel_adv_timeout(struct hci_dev *hdev)
@@ -1282,8 +1283,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v4 1/4] Bluetooth: Interleave with allowlist scan

2020-09-20 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

Changes in v4:
- Rebase to bluetooth-next/master (previous 2 patches are applied)
- Fix over 80 chars limit in mgmt_config.c
- Set EnableAdvMonInterleaveScan default to Disable

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 137 +--
 net/bluetooth/mgmt_config.c  |  10 +++
 4 files changed, 153 insertions(+), 8 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..179350f869fdb 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   ADV_MONITOR_SCAN_NONE,
+   ADV_MONITOR_SCAN_NO_FILTER,
+   ADV_MONITOR_SCAN_ALLOWLIST
+   } adv_monitor_scan_state;
+
+   struct delayed_work interleave_adv_monitor_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index f30a1f5950e15..6c8850149265a 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d2b06f5c93804..89443b48d90ce 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,57 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->adv_monitor_scan_state = ADV_MONITOR_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_adv_monitor_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->adv_monitor_scan_state != ADV_MONITOR_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "%s cancelling interleave scan", hdev->name);
+
+   cancel_delayed_work_sync(>interleave_adv_monitor_scan);
+
+   hdev->adv_monitor_scan_state = ADV_MONITOR_SCAN_NONE;
+}
+
+/* Return true if interleave_scan is running after exiting this function,
+ * otherwise, return false
+ */
+static bool update_adv_monitor_scan_state(struct hci_dev *hdev)
+{
+   if (!hci_is_adv_monitoring(hdev) ||
+   (list_empty(>pend_le_conns) &&
+list_empty(>pend_le_reports))) {
+   if (is_interleave_scanning(hdev)) {
+   /* If the interleave condition no longer holds, cancel
+* the existed interleave scan.
+*/
+   cancel_interleave_scan(hdev);
+   }
+   return false;
+   }
+
+   if (!is_interleave_scanning(hdev)) {
+   /* If there is at least one ADV monitors and one pending LE
+* connection or one device to be scanned for, we should
+* alternate between allowlist scan and one without any filters
+* to save power.
+*/
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "%s starting interleave scan", hdev->name);
+   }
+
+   return true;
+}
+
 /* This function controls the background scanning based on hdev->pend_le_conns
  * list. If there are pending LE connection we start the background scanning,
  * otherwise we stop it.
@@ -449,9 +500,11 @@ static void __hci_update_background_scan(st

[PATCH v3 3/6] Bluetooth: Interleave with allowlist scan

2020-09-17 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v2)

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 137 +--
 net/bluetooth/mgmt_config.c  |  12 +++
 4 files changed, 155 insertions(+), 8 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..179350f869fdb 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   ADV_MONITOR_SCAN_NONE,
+   ADV_MONITOR_SCAN_NO_FILTER,
+   ADV_MONITOR_SCAN_ALLOWLIST
+   } adv_monitor_scan_state;
+
+   struct delayed_work interleave_adv_monitor_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index f30a1f5950e15..6c8850149265a 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d2b06f5c93804..89443b48d90ce 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,57 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->adv_monitor_scan_state = ADV_MONITOR_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_adv_monitor_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->adv_monitor_scan_state != ADV_MONITOR_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "%s cancelling interleave scan", hdev->name);
+
+   cancel_delayed_work_sync(>interleave_adv_monitor_scan);
+
+   hdev->adv_monitor_scan_state = ADV_MONITOR_SCAN_NONE;
+}
+
+/* Return true if interleave_scan is running after exiting this function,
+ * otherwise, return false
+ */
+static bool update_adv_monitor_scan_state(struct hci_dev *hdev)
+{
+   if (!hci_is_adv_monitoring(hdev) ||
+   (list_empty(>pend_le_conns) &&
+list_empty(>pend_le_reports))) {
+   if (is_interleave_scanning(hdev)) {
+   /* If the interleave condition no longer holds, cancel
+* the existed interleave scan.
+*/
+   cancel_interleave_scan(hdev);
+   }
+   return false;
+   }
+
+   if (!is_interleave_scanning(hdev)) {
+   /* If there is at least one ADV monitors and one pending LE
+* connection or one device to be scanned for, we should
+* alternate between allowlist scan and one without any filters
+* to save power.
+*/
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "%s starting interleave scan", hdev->name);
+   }
+
+   return true;
+}
+
 /* This function controls the background scanning based on hdev->pend_le_conns
  * list. If there are pending LE connection we start the background scanning,
  * otherwise we stop it.
@@ -449,9 +500,11 @@ static void __hci_update_background_scan(struct 
hci_request *req)
if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
hci_req_add_le_scan_disable(req, false);
 
-   hci_req_add_le_passive_sc

[PATCH v3 4/6] Bluetooth: Handle system suspend resume case

2020-09-17 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 11 +++
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 89443b48d90ce..d9082019b6386 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1081,6 +1081,9 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
filter_policy |= 0x02;
 
if (hdev->suspended) {
+   /* Block suspend notifier on response */
+   set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks);
+
window = hdev->le_scan_window_suspend;
interval = hdev->le_scan_int_suspend;
} else if (hci_is_le_conn_scanning(hdev)) {
@@ -1167,10 +1170,8 @@ static void hci_req_config_le_suspend_scan(struct 
hci_request *req)
hci_req_add_le_scan_disable(req, false);
 
/* Configure params and enable scanning */
-   hci_req_add_le_passive_scan(req);
+   __hci_update_background_scan(req);
 
-   /* Block suspend notifier on response */
-   set_bit(SUSPEND_SCAN_ENABLE, req->hdev->suspend_tasks);
 }
 
 static void cancel_adv_timeout(struct hci_dev *hdev)
@@ -1282,8 +1283,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v3 1/6] Bluetooth: Update Adv monitor count upon removal

2020-09-17 Thread Howard Chung
From: Miao-chen Chou 

This fixes the count of Adv monitor upon monitor removal.

The following test was performed.
- Start two btmgmt consoles, issue a btmgmt advmon-remove command on one
console and observe a MGMT_EV_ADV_MONITOR_REMOVED event on the other.

Signed-off-by: Miao-chen Chou 
Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---

Changes in v3:
- Remove 'Bluez' prefix

Changes in v2:
- delete 'case 0x001c' in mgmt_config.c

 net/bluetooth/hci_core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 8a2645a833013..f30a1f5950e15 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3061,6 +3061,7 @@ static int free_adv_monitor(int id, void *ptr, void *data)
 
idr_remove(>adv_monitors_idr, monitor->handle);
hci_free_adv_monitor(monitor);
+   hdev->adv_monitors_cnt--;
 
return 0;
 }
@@ -3077,6 +3078,7 @@ int hci_remove_adv_monitor(struct hci_dev *hdev, u16 
handle)
 
idr_remove(>adv_monitors_idr, monitor->handle);
hci_free_adv_monitor(monitor);
+   hdev->adv_monitors_cnt--;
} else {
/* Remove all monitors if handle is 0. */
idr_for_each(>adv_monitors_idr, _adv_monitor, hdev);
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v3 6/6] Bluetooth: Add toggle to switch off interleave scan

2020-09-17 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---

(no changes since v1)

 include/net/bluetooth/hci_core.h | 1 +
 net/bluetooth/hci_core.c | 1 +
 net/bluetooth/hci_request.c  | 3 ++-
 net/bluetooth/mgmt_config.c  | 6 ++
 4 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 179350f869fdb..c3253f1cac0c2 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u16   enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 6c8850149265a..4608715860cce 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x0001;   /* Default to enable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 1fcf6736811e4..bb38e1dead68f 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -500,7 +500,8 @@ static void __hci_update_background_scan(struct hci_request 
*req)
if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
hci_req_add_le_scan_disable(req, false);
 
-   if (!update_adv_monitor_scan_state(hdev)) {
+   if (!hdev->enable_advmon_interleave_scan ||
+   !update_adv_monitor_scan_state(hdev)) {
hci_req_add_le_passive_scan(req);
bt_dev_dbg(hdev, "%s starting background scanning",
   hdev->name);
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 1802f7023158c..b4198c33a1b72 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -69,6 +69,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
def_le_autoconnect_timeout),
HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(0x001f, enable_advmon_interleave_scan),
};
struct mgmt_rp_read_def_system_config *rp = (void *)params;
 
@@ -142,6 +143,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
case 0x001b:
case 0x001d:
case 0x001e:
+   case 0x001f:
if (len != sizeof(u16)) {
bt_dev_warn(hdev, "invalid length %d, exp %zu 
for type %d",
len, sizeof(u16), type);
@@ -263,6 +265,10 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
hdev->advmon_no_filter_duration =
TLV_GET_LE16(buffer);
break;
+   case 0x0001f:
+   hdev->enable_advmon_interleave_scan =
+   TLV_GET_LE16(buffer);
+   break;
default:
bt_dev_warn(hdev, "unsupported parameter %u", type);
break;
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v3 2/6] Bluetooth: Set scan parameters for ADV Monitor

2020-09-17 Thread Howard Chung
Set scan parameters when there is at least one Advertisement monitor.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 413e3a5aabf54..d2b06f5c93804 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1027,6 +1027,9 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
} else if (hci_is_le_conn_scanning(hdev)) {
window = hdev->le_scan_window_connect;
interval = hdev->le_scan_int_connect;
+   } else if (hci_is_adv_monitoring(hdev)) {
+   window = hdev->le_scan_window_adv_monitor;
+   interval = hdev->le_scan_int_adv_monitor;
} else {
window = hdev->le_scan_window;
interval = hdev->le_scan_interval;
-- 
2.28.0.681.g6f77f65b4e-goog



[PATCH v3 5/6] Bluetooth: Handle active scan case

2020-09-17 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d9082019b6386..1fcf6736811e4 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3085,8 +3085,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.28.0.681.g6f77f65b4e-goog



[BlueZ PATCH v2 4/6] Bluetooth: Handle system suspend resume case

2020-09-17 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 11 +++
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 89443b48d90ce..d9082019b6386 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1081,6 +1081,9 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
filter_policy |= 0x02;
 
if (hdev->suspended) {
+   /* Block suspend notifier on response */
+   set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks);
+
window = hdev->le_scan_window_suspend;
interval = hdev->le_scan_int_suspend;
} else if (hci_is_le_conn_scanning(hdev)) {
@@ -1167,10 +1170,8 @@ static void hci_req_config_le_suspend_scan(struct 
hci_request *req)
hci_req_add_le_scan_disable(req, false);
 
/* Configure params and enable scanning */
-   hci_req_add_le_passive_scan(req);
+   __hci_update_background_scan(req);
 
-   /* Block suspend notifier on response */
-   set_bit(SUSPEND_SCAN_ENABLE, req->hdev->suspend_tasks);
 }
 
 static void cancel_adv_timeout(struct hci_dev *hdev)
@@ -1282,8 +1283,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.28.0.618.gf4bc123cb7-goog



[BlueZ PATCH v2 6/6] Bluetooth: Add toggle to switch off interleave scan

2020-09-17 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---

(no changes since v1)

 include/net/bluetooth/hci_core.h | 1 +
 net/bluetooth/hci_core.c | 1 +
 net/bluetooth/hci_request.c  | 3 ++-
 net/bluetooth/mgmt_config.c  | 6 ++
 4 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 179350f869fdb..c3253f1cac0c2 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u16   enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 6c8850149265a..4608715860cce 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x0001;   /* Default to enable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 1fcf6736811e4..bb38e1dead68f 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -500,7 +500,8 @@ static void __hci_update_background_scan(struct hci_request 
*req)
if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
hci_req_add_le_scan_disable(req, false);
 
-   if (!update_adv_monitor_scan_state(hdev)) {
+   if (!hdev->enable_advmon_interleave_scan ||
+   !update_adv_monitor_scan_state(hdev)) {
hci_req_add_le_passive_scan(req);
bt_dev_dbg(hdev, "%s starting background scanning",
   hdev->name);
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 1802f7023158c..b4198c33a1b72 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -69,6 +69,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
def_le_autoconnect_timeout),
HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(0x001f, enable_advmon_interleave_scan),
};
struct mgmt_rp_read_def_system_config *rp = (void *)params;
 
@@ -142,6 +143,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
case 0x001b:
case 0x001d:
case 0x001e:
+   case 0x001f:
if (len != sizeof(u16)) {
bt_dev_warn(hdev, "invalid length %d, exp %zu 
for type %d",
len, sizeof(u16), type);
@@ -263,6 +265,10 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
hdev->advmon_no_filter_duration =
TLV_GET_LE16(buffer);
break;
+   case 0x0001f:
+   hdev->enable_advmon_interleave_scan =
+   TLV_GET_LE16(buffer);
+   break;
default:
bt_dev_warn(hdev, "unsupported parameter %u", type);
break;
-- 
2.28.0.618.gf4bc123cb7-goog



[BlueZ PATCH v2 3/6] Bluetooth: Interleave with allowlist scan

2020-09-17 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

Changes in v2:
- remove 'case 0x001c' in mgmt_config.c

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 137 +--
 net/bluetooth/mgmt_config.c  |  12 +++
 4 files changed, 155 insertions(+), 8 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..179350f869fdb 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   ADV_MONITOR_SCAN_NONE,
+   ADV_MONITOR_SCAN_NO_FILTER,
+   ADV_MONITOR_SCAN_ALLOWLIST
+   } adv_monitor_scan_state;
+
+   struct delayed_work interleave_adv_monitor_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index f30a1f5950e15..6c8850149265a 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d2b06f5c93804..89443b48d90ce 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,57 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->adv_monitor_scan_state = ADV_MONITOR_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_adv_monitor_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->adv_monitor_scan_state != ADV_MONITOR_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "%s cancelling interleave scan", hdev->name);
+
+   cancel_delayed_work_sync(>interleave_adv_monitor_scan);
+
+   hdev->adv_monitor_scan_state = ADV_MONITOR_SCAN_NONE;
+}
+
+/* Return true if interleave_scan is running after exiting this function,
+ * otherwise, return false
+ */
+static bool update_adv_monitor_scan_state(struct hci_dev *hdev)
+{
+   if (!hci_is_adv_monitoring(hdev) ||
+   (list_empty(>pend_le_conns) &&
+list_empty(>pend_le_reports))) {
+   if (is_interleave_scanning(hdev)) {
+   /* If the interleave condition no longer holds, cancel
+* the existed interleave scan.
+*/
+   cancel_interleave_scan(hdev);
+   }
+   return false;
+   }
+
+   if (!is_interleave_scanning(hdev)) {
+   /* If there is at least one ADV monitors and one pending LE
+* connection or one device to be scanned for, we should
+* alternate between allowlist scan and one without any filters
+* to save power.
+*/
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "%s starting interleave scan", hdev->name);
+   }
+
+   return true;
+}
+
 /* This function controls the background scanning based on hdev->pend_le_conns
  * list. If there are pending LE connection we start the background scanning,
  * otherwise we stop it.
@@ -449,9 +500,11 @@ static void __hci_update_background_scan(struct 
hci_request *req)
if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
hci_req_add_le_scan_disable(req, false);
 
-   hci_req_add_le_passive_sc

[BlueZ PATCH v2 5/6] Bluetooth: Handle active scan case

2020-09-17 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d9082019b6386..1fcf6736811e4 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3085,8 +3085,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.28.0.618.gf4bc123cb7-goog



[BlueZ PATCH v2 2/6] Bluetooth: Set scan parameters for ADV Monitor

2020-09-17 Thread Howard Chung
Set scan parameters when there is at least one Advertisement monitor.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

(no changes since v1)

 net/bluetooth/hci_request.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 413e3a5aabf54..d2b06f5c93804 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1027,6 +1027,9 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
} else if (hci_is_le_conn_scanning(hdev)) {
window = hdev->le_scan_window_connect;
interval = hdev->le_scan_int_connect;
+   } else if (hci_is_adv_monitoring(hdev)) {
+   window = hdev->le_scan_window_adv_monitor;
+   interval = hdev->le_scan_int_adv_monitor;
} else {
window = hdev->le_scan_window;
interval = hdev->le_scan_interval;
-- 
2.28.0.618.gf4bc123cb7-goog



[BlueZ PATCH v2 1/6] Bluetooth: Update Adv monitor count upon removal

2020-09-17 Thread Howard Chung
From: Miao-chen Chou 

This fixes the count of Adv monitor upon monitor removal.

The following test was performed.
- Start two btmgmt consoles, issue a btmgmt advmon-remove command on one
console and observe a MGMT_EV_ADV_MONITOR_REMOVED event on the other.

Signed-off-by: Miao-chen Chou 
Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---

Changes in v2:
- delete 'case 0x001c' in mgmt_config.c

 net/bluetooth/hci_core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 8a2645a833013..f30a1f5950e15 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3061,6 +3061,7 @@ static int free_adv_monitor(int id, void *ptr, void *data)
 
idr_remove(>adv_monitors_idr, monitor->handle);
hci_free_adv_monitor(monitor);
+   hdev->adv_monitors_cnt--;
 
return 0;
 }
@@ -3077,6 +3078,7 @@ int hci_remove_adv_monitor(struct hci_dev *hdev, u16 
handle)
 
idr_remove(>adv_monitors_idr, monitor->handle);
hci_free_adv_monitor(monitor);
+   hdev->adv_monitors_cnt--;
} else {
/* Remove all monitors if handle is 0. */
idr_for_each(>adv_monitors_idr, _adv_monitor, hdev);
-- 
2.28.0.618.gf4bc123cb7-goog



[BlueZ PATCH 4/6] Bluetooth: Handle system suspend resume case

2020-09-16 Thread Howard Chung
This patch adds code to handle the system suspension during interleave
scan. The interleave scan will be canceled when the system is going to
sleep, and will be restarted after waking up.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

 net/bluetooth/hci_request.c | 11 +++
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 89443b48d90ce..d9082019b6386 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1081,6 +1081,9 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
filter_policy |= 0x02;
 
if (hdev->suspended) {
+   /* Block suspend notifier on response */
+   set_bit(SUSPEND_SCAN_ENABLE, hdev->suspend_tasks);
+
window = hdev->le_scan_window_suspend;
interval = hdev->le_scan_int_suspend;
} else if (hci_is_le_conn_scanning(hdev)) {
@@ -1167,10 +1170,8 @@ static void hci_req_config_le_suspend_scan(struct 
hci_request *req)
hci_req_add_le_scan_disable(req, false);
 
/* Configure params and enable scanning */
-   hci_req_add_le_passive_scan(req);
+   __hci_update_background_scan(req);
 
-   /* Block suspend notifier on response */
-   set_bit(SUSPEND_SCAN_ENABLE, req->hdev->suspend_tasks);
 }
 
 static void cancel_adv_timeout(struct hci_dev *hdev)
@@ -1282,8 +1283,10 @@ void hci_req_prepare_suspend(struct hci_dev *hdev, enum 
suspended_state next)
hci_req_add(, HCI_OP_WRITE_SCAN_ENABLE, 1, _scan);
 
/* Disable LE passive scan if enabled */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
+   cancel_interleave_scan(hdev);
hci_req_add_le_scan_disable(, false);
+   }
 
/* Mark task needing completion */
set_bit(SUSPEND_SCAN_DISABLE, hdev->suspend_tasks);
-- 
2.28.0.618.gf4bc123cb7-goog



[BlueZ PATCH 6/6] Bluetooth: Add toggle to switch off interleave scan

2020-09-16 Thread Howard Chung
This patch add a configurable parameter to switch off the interleave
scan feature.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
---

 include/net/bluetooth/hci_core.h | 1 +
 net/bluetooth/hci_core.c | 1 +
 net/bluetooth/hci_request.c  | 3 ++-
 net/bluetooth/mgmt_config.c  | 6 ++
 4 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 179350f869fdb..c3253f1cac0c2 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -363,6 +363,7 @@ struct hci_dev {
__u32   clock;
__u16   advmon_allowlist_duration;
__u16   advmon_no_filter_duration;
+   __u16   enable_advmon_interleave_scan;
 
__u16   devid_source;
__u16   devid_vendor;
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 6c8850149265a..4608715860cce 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3595,6 +3595,7 @@ struct hci_dev *hci_alloc_dev(void)
/* The default values will be chosen in the future */
hdev->advmon_allowlist_duration = 300;
hdev->advmon_no_filter_duration = 500;
+   hdev->enable_advmon_interleave_scan = 0x0001;   /* Default to enable */
 
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 1fcf6736811e4..bb38e1dead68f 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -500,7 +500,8 @@ static void __hci_update_background_scan(struct hci_request 
*req)
if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
hci_req_add_le_scan_disable(req, false);
 
-   if (!update_adv_monitor_scan_state(hdev)) {
+   if (!hdev->enable_advmon_interleave_scan ||
+   !update_adv_monitor_scan_state(hdev)) {
hci_req_add_le_passive_scan(req);
bt_dev_dbg(hdev, "%s starting background scanning",
   hdev->name);
diff --git a/net/bluetooth/mgmt_config.c b/net/bluetooth/mgmt_config.c
index 6dc3e43dcaa9f..4df6de44ee438 100644
--- a/net/bluetooth/mgmt_config.c
+++ b/net/bluetooth/mgmt_config.c
@@ -69,6 +69,7 @@ int read_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
def_le_autoconnect_timeout),
HDEV_PARAM_U16(0x001d, advmon_allowlist_duration),
HDEV_PARAM_U16(0x001e, advmon_no_filter_duration),
+   HDEV_PARAM_U16(0x001f, enable_advmon_interleave_scan),
};
struct mgmt_rp_read_def_system_config *rp = (void *)params;
 
@@ -143,6 +144,7 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
case 0x001c:
case 0x001d:
case 0x001e:
+   case 0x001f:
if (len != sizeof(u16)) {
bt_dev_warn(hdev, "invalid length %d, exp %zu 
for type %d",
len, sizeof(u16), type);
@@ -264,6 +266,10 @@ int set_def_system_config(struct sock *sk, struct hci_dev 
*hdev, void *data,
hdev->advmon_no_filter_duration =
TLV_GET_LE16(buffer);
break;
+   case 0x0001f:
+   hdev->enable_advmon_interleave_scan =
+   TLV_GET_LE16(buffer);
+   break;
default:
bt_dev_warn(hdev, "unsupported parameter %u", type);
break;
-- 
2.28.0.618.gf4bc123cb7-goog



[BlueZ PATCH 2/6] Bluetooth: Set scan parameters for ADV Monitor

2020-09-16 Thread Howard Chung
Set scan parameters when there is at least one Advertisement monitor.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
Reviewed-by: Abhishek Pandit-Subedi 
Reviewed-by: Miao-chen Chou 
---

 net/bluetooth/hci_request.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index 413e3a5aabf54..d2b06f5c93804 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -1027,6 +1027,9 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
} else if (hci_is_le_conn_scanning(hdev)) {
window = hdev->le_scan_window_connect;
interval = hdev->le_scan_int_connect;
+   } else if (hci_is_adv_monitoring(hdev)) {
+   window = hdev->le_scan_window_adv_monitor;
+   interval = hdev->le_scan_int_adv_monitor;
} else {
window = hdev->le_scan_window;
interval = hdev->le_scan_interval;
-- 
2.28.0.618.gf4bc123cb7-goog



[BlueZ PATCH 3/6] Bluetooth: Interleave with allowlist scan

2020-09-16 Thread Howard Chung
This patch implements the interleaving between allowlist scan and
no-filter scan. It'll be used to save power when at least one monitor is
registered and at least one pending connection or one device to be
scanned for.

The durations of the allowlist scan and the no-filter scan are
controlled by MGMT command: Set Default System Configuration. The
default values are set randomly for now.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

 include/net/bluetooth/hci_core.h |  10 +++
 net/bluetooth/hci_core.c |   4 +
 net/bluetooth/hci_request.c  | 137 +--
 net/bluetooth/mgmt_config.c  |  13 +++
 4 files changed, 156 insertions(+), 8 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 9873e1c8cd163..179350f869fdb 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -361,6 +361,8 @@ struct hci_dev {
__u8ssp_debug_mode;
__u8hw_error_code;
__u32   clock;
+   __u16   advmon_allowlist_duration;
+   __u16   advmon_no_filter_duration;
 
__u16   devid_source;
__u16   devid_vendor;
@@ -542,6 +544,14 @@ struct hci_dev {
struct delayed_work rpa_expired;
bdaddr_trpa;
 
+   enum {
+   ADV_MONITOR_SCAN_NONE,
+   ADV_MONITOR_SCAN_NO_FILTER,
+   ADV_MONITOR_SCAN_ALLOWLIST
+   } adv_monitor_scan_state;
+
+   struct delayed_work interleave_adv_monitor_scan;
+
 #if IS_ENABLED(CONFIG_BT_LEDS)
struct led_trigger  *power_led;
 #endif
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index f30a1f5950e15..6c8850149265a 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3592,6 +3592,10 @@ struct hci_dev *hci_alloc_dev(void)
hdev->cur_adv_instance = 0x00;
hdev->adv_instance_timeout = 0;
 
+   /* The default values will be chosen in the future */
+   hdev->advmon_allowlist_duration = 300;
+   hdev->advmon_no_filter_duration = 500;
+
hdev->sniff_max_interval = 800;
hdev->sniff_min_interval = 80;
 
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d2b06f5c93804..89443b48d90ce 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -378,6 +378,57 @@ void __hci_req_write_fast_connectable(struct hci_request 
*req, bool enable)
hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, );
 }
 
+static void start_interleave_scan(struct hci_dev *hdev)
+{
+   hdev->adv_monitor_scan_state = ADV_MONITOR_SCAN_NO_FILTER;
+   queue_delayed_work(hdev->req_workqueue,
+  >interleave_adv_monitor_scan, 0);
+}
+
+static bool is_interleave_scanning(struct hci_dev *hdev)
+{
+   return hdev->adv_monitor_scan_state != ADV_MONITOR_SCAN_NONE;
+}
+
+static void cancel_interleave_scan(struct hci_dev *hdev)
+{
+   bt_dev_dbg(hdev, "%s cancelling interleave scan", hdev->name);
+
+   cancel_delayed_work_sync(>interleave_adv_monitor_scan);
+
+   hdev->adv_monitor_scan_state = ADV_MONITOR_SCAN_NONE;
+}
+
+/* Return true if interleave_scan is running after exiting this function,
+ * otherwise, return false
+ */
+static bool update_adv_monitor_scan_state(struct hci_dev *hdev)
+{
+   if (!hci_is_adv_monitoring(hdev) ||
+   (list_empty(>pend_le_conns) &&
+list_empty(>pend_le_reports))) {
+   if (is_interleave_scanning(hdev)) {
+   /* If the interleave condition no longer holds, cancel
+* the existed interleave scan.
+*/
+   cancel_interleave_scan(hdev);
+   }
+   return false;
+   }
+
+   if (!is_interleave_scanning(hdev)) {
+   /* If there is at least one ADV monitors and one pending LE
+* connection or one device to be scanned for, we should
+* alternate between allowlist scan and one without any filters
+* to save power.
+*/
+   start_interleave_scan(hdev);
+   bt_dev_dbg(hdev, "%s starting interleave scan", hdev->name);
+   }
+
+   return true;
+}
+
 /* This function controls the background scanning based on hdev->pend_le_conns
  * list. If there are pending LE connection we start the background scanning,
  * otherwise we stop it.
@@ -449,9 +500,11 @@ static void __hci_update_background_scan(struct 
hci_request *req)
if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
hci_req_add_le_scan_disable(req, false);
 
-   hci_req_add_le_passive_scan(req);
-
-   BT_DBG("%s starting backgr

[BlueZ PATCH 5/6] Bluetooth: Handle active scan case

2020-09-16 Thread Howard Chung
This patch adds code to handle the active scan during interleave
scan. The interleave scan will be canceled when users start active scan,
and it will be restarted after active scan stopped.

Signed-off-by: Howard Chung 
Reviewed-by: Alain Michaud 
Reviewed-by: Manish Mandlik 
---

 net/bluetooth/hci_request.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index d9082019b6386..1fcf6736811e4 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -3085,8 +3085,10 @@ static int active_scan(struct hci_request *req, unsigned 
long opt)
 * running. Thus, we should temporarily stop it in order to set the
 * discovery scanning parameters.
 */
-   if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
+   if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
hci_req_add_le_scan_disable(req, false);
+   cancel_interleave_scan(hdev);
+   }
 
/* All active scans will be done with either a resolvable private
 * address (when privacy feature has been enabled) or non-resolvable
-- 
2.28.0.618.gf4bc123cb7-goog



[BlueZ PATCH 1/6] Bluetooth: Update Adv monitor count upon removal

2020-09-16 Thread Howard Chung
From: Miao-chen Chou 

This fixes the count of Adv monitor upon monitor removal.

The following test was performed.
- Start two btmgmt consoles, issue a btmgmt advmon-remove command on one
console and observe a MGMT_EV_ADV_MONITOR_REMOVED event on the other.

Signed-off-by: Howard Chung 
Signed-off-by: Miao-chen Chou 
Reviewed-by: Alain Michaud 
---

 net/bluetooth/hci_core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 8a2645a833013..f30a1f5950e15 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3061,6 +3061,7 @@ static int free_adv_monitor(int id, void *ptr, void *data)
 
idr_remove(>adv_monitors_idr, monitor->handle);
hci_free_adv_monitor(monitor);
+   hdev->adv_monitors_cnt--;
 
return 0;
 }
@@ -3077,6 +3078,7 @@ int hci_remove_adv_monitor(struct hci_dev *hdev, u16 
handle)
 
idr_remove(>adv_monitors_idr, monitor->handle);
hci_free_adv_monitor(monitor);
+   hdev->adv_monitors_cnt--;
} else {
/* Remove all monitors if handle is 0. */
idr_for_each(>adv_monitors_idr, _adv_monitor, hdev);
-- 
2.28.0.618.gf4bc123cb7-goog