Skip to content

Commit f80c5da

Browse files
jprvitaholtmann
authored andcommitted
Bluetooth: Ignore CC events not matching the last HCI command
This commit makes the kernel not send the next queued HCI command until a command complete arrives for the last HCI command sent to the controller. This change avoids a problem with some buggy controllers (seen on two SKUs of QCA9377) that send an extra command complete event for the previous command after the kernel had already sent a new HCI command to the controller. The problem was reproduced when starting an active scanning procedure, where an extra command complete event arrives for the LE_SET_RANDOM_ADDR command. When this happends the kernel ends up not processing the command complete for the following commmand, LE_SET_SCAN_PARAM, and ultimately behaving as if a passive scanning procedure was being performed, when in fact controller is performing an active scanning procedure. This makes it impossible to discover BLE devices as no device found events are sent to userspace. This problem is reproducible on 100% of the attempts on the affected controllers. The extra command complete event can be seen at timestamp 27.420131 on the btmon logs bellow. Bluetooth monitor ver 5.50 = Note: Linux version 5.0.0+ (x86_64) 0.352340 = Note: Bluetooth subsystem version 2.22 0.352343 = New Index: 80:C5:F2:8F:87:84 (Primary,USB,hci0) [hci0] 0.352344 = Open Index: 80:C5:F2:8F:87:84 [hci0] 0.352345 = Index Info: 80:C5:F2:8F:87:84 (Qualcomm) [hci0] 0.352346 @ MGMT Open: bluetoothd (privileged) version 1.14 {0x0001} 0.352347 @ MGMT Open: btmon (privileged) version 1.14 {0x0002} 0.352366 @ MGMT Open: btmgmt (privileged) version 1.14 {0x0003} 27.302164 @ MGMT Command: Start Discovery (0x0023) plen 1 {0x0003} [hci0] 27.302310 Address type: 0x06 LE Public LE Random < HCI Command: LE Set Random Address (0x08|0x0005) plen 6 #1 [hci0] 27.302496 Address: 15:60:F2:91:B2:24 (Non-Resolvable) > HCI Event: Command Complete (0x0e) plen 4 #2 [hci0] 27.419117 LE Set Random Address (0x08|0x0005) ncmd 1 Status: Success (0x00) < HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7 #3 [hci0] 27.419244 Type: Active (0x01) Interval: 11.250 msec (0x0012) Window: 11.250 msec (0x0012) Own address type: Random (0x01) Filter policy: Accept all advertisement (0x00) > HCI Event: Command Complete (0x0e) plen 4 #4 [hci0] 27.420131 LE Set Random Address (0x08|0x0005) ncmd 1 Status: Success (0x00) < HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2 #5 [hci0] 27.420259 Scanning: Enabled (0x01) Filter duplicates: Enabled (0x01) > HCI Event: Command Complete (0x0e) plen 4 torvalds#6 [hci0] 27.420969 LE Set Scan Parameters (0x08|0x000b) ncmd 1 Status: Success (0x00) > HCI Event: Command Complete (0x0e) plen 4 #7 [hci0] 27.421983 LE Set Scan Enable (0x08|0x000c) ncmd 1 Status: Success (0x00) @ MGMT Event: Command Complete (0x0001) plen 4 {0x0003} [hci0] 27.422059 Start Discovery (0x0023) plen 1 Status: Success (0x00) Address type: 0x06 LE Public LE Random @ MGMT Event: Discovering (0x0013) plen 2 {0x0003} [hci0] 27.422067 Address type: 0x06 LE Public LE Random Discovery: Enabled (0x01) @ MGMT Event: Discovering (0x0013) plen 2 {0x0002} [hci0] 27.422067 Address type: 0x06 LE Public LE Random Discovery: Enabled (0x01) @ MGMT Event: Discovering (0x0013) plen 2 {0x0001} [hci0] 27.422067 Address type: 0x06 LE Public LE Random Discovery: Enabled (0x01) Signed-off-by: João Paulo Rechi Vita <[email protected]> Signed-off-by: Marcel Holtmann <[email protected]>
1 parent 300926b commit f80c5da

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

include/net/bluetooth/hci.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ enum {
282282
HCI_FORCE_BREDR_SMP,
283283
HCI_FORCE_STATIC_ADDR,
284284
HCI_LL_RPA_RESOLUTION,
285+
HCI_CMD_PENDING,
285286

286287
__HCI_NUM_FLAGS,
287288
};

net/bluetooth/hci_core.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4381,6 +4381,9 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status,
43814381
return;
43824382
}
43834383

4384+
/* If we reach this point this event matches the last command sent */
4385+
hci_dev_clear_flag(hdev, HCI_CMD_PENDING);
4386+
43844387
/* If the command succeeded and there's still more commands in
43854388
* this request the request is not yet complete.
43864389
*/
@@ -4491,6 +4494,8 @@ static void hci_cmd_work(struct work_struct *work)
44914494

44924495
hdev->sent_cmd = skb_clone(skb, GFP_KERNEL);
44934496
if (hdev->sent_cmd) {
4497+
if (hci_req_status_pend(hdev))
4498+
hci_dev_set_flag(hdev, HCI_CMD_PENDING);
44944499
atomic_dec(&hdev->cmd_cnt);
44954500
hci_send_frame(hdev, skb);
44964501
if (test_bit(HCI_RESET, &hdev->flags))

net/bluetooth/hci_event.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,6 +3404,12 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
34043404
hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
34053405
req_complete_skb);
34063406

3407+
if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
3408+
bt_dev_err(hdev,
3409+
"unexpected event for opcode 0x%4.4x", *opcode);
3410+
return;
3411+
}
3412+
34073413
if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
34083414
queue_work(hdev->workqueue, &hdev->cmd_work);
34093415
}
@@ -3511,6 +3517,12 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb,
35113517
hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
35123518
req_complete_skb);
35133519

3520+
if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
3521+
bt_dev_err(hdev,
3522+
"unexpected event for opcode 0x%4.4x", *opcode);
3523+
return;
3524+
}
3525+
35143526
if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
35153527
queue_work(hdev->workqueue, &hdev->cmd_work);
35163528
}

net/bluetooth/hci_request.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ void hci_req_purge(struct hci_request *req)
4646
skb_queue_purge(&req->cmd_q);
4747
}
4848

49+
bool hci_req_status_pend(struct hci_dev *hdev)
50+
{
51+
return hdev->req_status == HCI_REQ_PEND;
52+
}
53+
4954
static int req_run(struct hci_request *req, hci_req_complete_t complete,
5055
hci_req_complete_skb_t complete_skb)
5156
{

net/bluetooth/hci_request.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct hci_request {
3737

3838
void hci_req_init(struct hci_request *req, struct hci_dev *hdev);
3939
void hci_req_purge(struct hci_request *req);
40+
bool hci_req_status_pend(struct hci_dev *hdev);
4041
int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
4142
int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete);
4243
void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,

0 commit comments

Comments
 (0)