1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2021 Intel Corporation
6 * Copyright 2023 NXP
7 */
8
9 #include <linux/property.h>
10
11 #include <net/bluetooth/bluetooth.h>
12 #include <net/bluetooth/hci_core.h>
13 #include <net/bluetooth/mgmt.h>
14
15 #include "hci_codec.h"
16 #include "hci_debugfs.h"
17 #include "smp.h"
18 #include "eir.h"
19 #include "msft.h"
20 #include "aosp.h"
21 #include "leds.h"
22
hci_cmd_sync_complete(struct hci_dev * hdev,u8 result,u16 opcode,struct sk_buff * skb)23 static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
24 struct sk_buff *skb)
25 {
26 bt_dev_dbg(hdev, "result 0x%2.2x", result);
27
28 if (hdev->req_status != HCI_REQ_PEND)
29 return;
30
31 hdev->req_result = result;
32 hdev->req_status = HCI_REQ_DONE;
33
34 /* Free the request command so it is not used as response */
35 kfree_skb(hdev->req_skb);
36 hdev->req_skb = NULL;
37
38 if (skb) {
39 struct sock *sk = hci_skb_sk(skb);
40
41 /* Drop sk reference if set */
42 if (sk)
43 sock_put(sk);
44
45 hdev->req_rsp = skb_get(skb);
46 }
47
48 wake_up_interruptible(&hdev->req_wait_q);
49 }
50
hci_cmd_sync_alloc(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,struct sock * sk)51 struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode, u32 plen,
52 const void *param, struct sock *sk)
53 {
54 int len = HCI_COMMAND_HDR_SIZE + plen;
55 struct hci_command_hdr *hdr;
56 struct sk_buff *skb;
57
58 skb = bt_skb_alloc(len, GFP_ATOMIC);
59 if (!skb)
60 return NULL;
61
62 hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
63 hdr->opcode = cpu_to_le16(opcode);
64 hdr->plen = plen;
65
66 if (plen)
67 skb_put_data(skb, param, plen);
68
69 bt_dev_dbg(hdev, "skb len %d", skb->len);
70
71 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
72 hci_skb_opcode(skb) = opcode;
73
74 /* Grab a reference if command needs to be associated with a sock (e.g.
75 * likely mgmt socket that initiated the command).
76 */
77 if (sk) {
78 hci_skb_sk(skb) = sk;
79 sock_hold(sk);
80 }
81
82 return skb;
83 }
84
hci_cmd_sync_add(struct hci_request * req,u16 opcode,u32 plen,const void * param,u8 event,struct sock * sk)85 static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
86 const void *param, u8 event, struct sock *sk)
87 {
88 struct hci_dev *hdev = req->hdev;
89 struct sk_buff *skb;
90
91 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
92
93 /* If an error occurred during request building, there is no point in
94 * queueing the HCI command. We can simply return.
95 */
96 if (req->err)
97 return;
98
99 skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
100 if (!skb) {
101 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
102 opcode);
103 req->err = -ENOMEM;
104 return;
105 }
106
107 if (skb_queue_empty(&req->cmd_q))
108 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
109
110 hci_skb_event(skb) = event;
111
112 skb_queue_tail(&req->cmd_q, skb);
113 }
114
hci_req_sync_run(struct hci_request * req)115 static int hci_req_sync_run(struct hci_request *req)
116 {
117 struct hci_dev *hdev = req->hdev;
118 struct sk_buff *skb;
119 unsigned long flags;
120
121 bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
122
123 /* If an error occurred during request building, remove all HCI
124 * commands queued on the HCI request queue.
125 */
126 if (req->err) {
127 skb_queue_purge(&req->cmd_q);
128 return req->err;
129 }
130
131 /* Do not allow empty requests */
132 if (skb_queue_empty(&req->cmd_q))
133 return -ENODATA;
134
135 skb = skb_peek_tail(&req->cmd_q);
136 bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
137 bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
138
139 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
140 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
141 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
142
143 queue_work(hdev->workqueue, &hdev->cmd_work);
144
145 return 0;
146 }
147
hci_request_init(struct hci_request * req,struct hci_dev * hdev)148 static void hci_request_init(struct hci_request *req, struct hci_dev *hdev)
149 {
150 skb_queue_head_init(&req->cmd_q);
151 req->hdev = hdev;
152 req->err = 0;
153 }
154
155 /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync_sk(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u8 event,u32 timeout,struct sock * sk)156 struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
157 const void *param, u8 event, u32 timeout,
158 struct sock *sk)
159 {
160 struct hci_request req;
161 struct sk_buff *skb;
162 int err = 0;
163
164 bt_dev_dbg(hdev, "Opcode 0x%4.4x", opcode);
165
166 hci_request_init(&req, hdev);
167
168 hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
169
170 hdev->req_status = HCI_REQ_PEND;
171
172 err = hci_req_sync_run(&req);
173 if (err < 0)
174 return ERR_PTR(err);
175
176 err = wait_event_interruptible_timeout(hdev->req_wait_q,
177 hdev->req_status != HCI_REQ_PEND,
178 timeout);
179
180 if (err == -ERESTARTSYS)
181 return ERR_PTR(-EINTR);
182
183 switch (hdev->req_status) {
184 case HCI_REQ_DONE:
185 err = -bt_to_errno(hdev->req_result);
186 break;
187
188 case HCI_REQ_CANCELED:
189 err = -hdev->req_result;
190 break;
191
192 default:
193 err = -ETIMEDOUT;
194 break;
195 }
196
197 hdev->req_status = 0;
198 hdev->req_result = 0;
199 skb = hdev->req_rsp;
200 hdev->req_rsp = NULL;
201
202 bt_dev_dbg(hdev, "end: err %d", err);
203
204 if (err < 0) {
205 kfree_skb(skb);
206 return ERR_PTR(err);
207 }
208
209 /* If command return a status event skb will be set to NULL as there are
210 * no parameters.
211 */
212 if (!skb)
213 return ERR_PTR(-ENODATA);
214
215 return skb;
216 }
217 EXPORT_SYMBOL(__hci_cmd_sync_sk);
218
219 /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)220 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
221 const void *param, u32 timeout)
222 {
223 return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
224 }
225 EXPORT_SYMBOL(__hci_cmd_sync);
226
227 /* Send HCI command and wait for command complete event */
hci_cmd_sync(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)228 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
229 const void *param, u32 timeout)
230 {
231 struct sk_buff *skb;
232
233 if (!test_bit(HCI_UP, &hdev->flags))
234 return ERR_PTR(-ENETDOWN);
235
236 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
237
238 hci_req_sync_lock(hdev);
239 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
240 hci_req_sync_unlock(hdev);
241
242 return skb;
243 }
244 EXPORT_SYMBOL(hci_cmd_sync);
245
246 /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync_ev(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u8 event,u32 timeout)247 struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
248 const void *param, u8 event, u32 timeout)
249 {
250 return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
251 NULL);
252 }
253 EXPORT_SYMBOL(__hci_cmd_sync_ev);
254
255 /* This function requires the caller holds hdev->req_lock. */
__hci_cmd_sync_status_sk(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u8 event,u32 timeout,struct sock * sk)256 int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
257 const void *param, u8 event, u32 timeout,
258 struct sock *sk)
259 {
260 struct sk_buff *skb;
261 u8 status;
262
263 skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
264
265 /* If command return a status event, skb will be set to -ENODATA */
266 if (skb == ERR_PTR(-ENODATA))
267 return 0;
268
269 if (IS_ERR(skb)) {
270 if (!event)
271 bt_dev_err(hdev, "Opcode 0x%4.4x failed: %ld", opcode,
272 PTR_ERR(skb));
273 return PTR_ERR(skb);
274 }
275
276 status = skb->data[0];
277
278 kfree_skb(skb);
279
280 return status;
281 }
282 EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
283
__hci_cmd_sync_status(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)284 int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
285 const void *param, u32 timeout)
286 {
287 return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
288 NULL);
289 }
290 EXPORT_SYMBOL(__hci_cmd_sync_status);
291
hci_cmd_sync_status(struct hci_dev * hdev,u16 opcode,u32 plen,const void * param,u32 timeout)292 int hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
293 const void *param, u32 timeout)
294 {
295 int err;
296
297 hci_req_sync_lock(hdev);
298 err = __hci_cmd_sync_status(hdev, opcode, plen, param, timeout);
299 hci_req_sync_unlock(hdev);
300
301 return err;
302 }
303 EXPORT_SYMBOL(hci_cmd_sync_status);
304
hci_cmd_sync_work(struct work_struct * work)305 static void hci_cmd_sync_work(struct work_struct *work)
306 {
307 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
308
309 bt_dev_dbg(hdev, "");
310
311 /* Dequeue all entries and run them */
312 while (1) {
313 struct hci_cmd_sync_work_entry *entry;
314
315 mutex_lock(&hdev->cmd_sync_work_lock);
316 entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
317 struct hci_cmd_sync_work_entry,
318 list);
319 if (entry)
320 list_del(&entry->list);
321 mutex_unlock(&hdev->cmd_sync_work_lock);
322
323 if (!entry)
324 break;
325
326 bt_dev_dbg(hdev, "entry %p", entry);
327
328 if (entry->func) {
329 int err;
330
331 hci_req_sync_lock(hdev);
332 err = entry->func(hdev, entry->data);
333 if (entry->destroy)
334 entry->destroy(hdev, entry->data, err);
335 hci_req_sync_unlock(hdev);
336 }
337
338 kfree(entry);
339 }
340 }
341
hci_cmd_sync_cancel_work(struct work_struct * work)342 static void hci_cmd_sync_cancel_work(struct work_struct *work)
343 {
344 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
345
346 cancel_delayed_work_sync(&hdev->cmd_timer);
347 cancel_delayed_work_sync(&hdev->ncmd_timer);
348 atomic_set(&hdev->cmd_cnt, 1);
349
350 wake_up_interruptible(&hdev->req_wait_q);
351 }
352
353 static int hci_scan_disable_sync(struct hci_dev *hdev);
scan_disable_sync(struct hci_dev * hdev,void * data)354 static int scan_disable_sync(struct hci_dev *hdev, void *data)
355 {
356 return hci_scan_disable_sync(hdev);
357 }
358
interleaved_inquiry_sync(struct hci_dev * hdev,void * data)359 static int interleaved_inquiry_sync(struct hci_dev *hdev, void *data)
360 {
361 return hci_inquiry_sync(hdev, DISCOV_INTERLEAVED_INQUIRY_LEN, 0);
362 }
363
le_scan_disable(struct work_struct * work)364 static void le_scan_disable(struct work_struct *work)
365 {
366 struct hci_dev *hdev = container_of(work, struct hci_dev,
367 le_scan_disable.work);
368 int status;
369
370 bt_dev_dbg(hdev, "");
371 hci_dev_lock(hdev);
372
373 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
374 goto _return;
375
376 status = hci_cmd_sync_queue(hdev, scan_disable_sync, NULL, NULL);
377 if (status) {
378 bt_dev_err(hdev, "failed to disable LE scan: %d", status);
379 goto _return;
380 }
381
382 /* If we were running LE only scan, change discovery state. If
383 * we were running both LE and BR/EDR inquiry simultaneously,
384 * and BR/EDR inquiry is already finished, stop discovery,
385 * otherwise BR/EDR inquiry will stop discovery when finished.
386 * If we will resolve remote device name, do not change
387 * discovery state.
388 */
389
390 if (hdev->discovery.type == DISCOV_TYPE_LE)
391 goto discov_stopped;
392
393 if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
394 goto _return;
395
396 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
397 if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
398 hdev->discovery.state != DISCOVERY_RESOLVING)
399 goto discov_stopped;
400
401 goto _return;
402 }
403
404 status = hci_cmd_sync_queue(hdev, interleaved_inquiry_sync, NULL, NULL);
405 if (status) {
406 bt_dev_err(hdev, "inquiry failed: status %d", status);
407 goto discov_stopped;
408 }
409
410 goto _return;
411
412 discov_stopped:
413 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
414
415 _return:
416 hci_dev_unlock(hdev);
417 }
418
419 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
420 u8 filter_dup);
421
reenable_adv_sync(struct hci_dev * hdev,void * data)422 static int reenable_adv_sync(struct hci_dev *hdev, void *data)
423 {
424 bt_dev_dbg(hdev, "");
425
426 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
427 list_empty(&hdev->adv_instances))
428 return 0;
429
430 if (hdev->cur_adv_instance) {
431 return hci_schedule_adv_instance_sync(hdev,
432 hdev->cur_adv_instance,
433 true);
434 } else {
435 if (ext_adv_capable(hdev)) {
436 hci_start_ext_adv_sync(hdev, 0x00);
437 } else {
438 hci_update_adv_data_sync(hdev, 0x00);
439 hci_update_scan_rsp_data_sync(hdev, 0x00);
440 hci_enable_advertising_sync(hdev);
441 }
442 }
443
444 return 0;
445 }
446
reenable_adv(struct work_struct * work)447 static void reenable_adv(struct work_struct *work)
448 {
449 struct hci_dev *hdev = container_of(work, struct hci_dev,
450 reenable_adv_work);
451 int status;
452
453 bt_dev_dbg(hdev, "");
454
455 hci_dev_lock(hdev);
456
457 status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL);
458 if (status)
459 bt_dev_err(hdev, "failed to reenable ADV: %d", status);
460
461 hci_dev_unlock(hdev);
462 }
463
cancel_adv_timeout(struct hci_dev * hdev)464 static void cancel_adv_timeout(struct hci_dev *hdev)
465 {
466 if (hdev->adv_instance_timeout) {
467 hdev->adv_instance_timeout = 0;
468 cancel_delayed_work(&hdev->adv_instance_expire);
469 }
470 }
471
472 /* For a single instance:
473 * - force == true: The instance will be removed even when its remaining
474 * lifetime is not zero.
475 * - force == false: the instance will be deactivated but kept stored unless
476 * the remaining lifetime is zero.
477 *
478 * For instance == 0x00:
479 * - force == true: All instances will be removed regardless of their timeout
480 * setting.
481 * - force == false: Only instances that have a timeout will be removed.
482 */
hci_clear_adv_instance_sync(struct hci_dev * hdev,struct sock * sk,u8 instance,bool force)483 int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk,
484 u8 instance, bool force)
485 {
486 struct adv_info *adv_instance, *n, *next_instance = NULL;
487 int err;
488 u8 rem_inst;
489
490 /* Cancel any timeout concerning the removed instance(s). */
491 if (!instance || hdev->cur_adv_instance == instance)
492 cancel_adv_timeout(hdev);
493
494 /* Get the next instance to advertise BEFORE we remove
495 * the current one. This can be the same instance again
496 * if there is only one instance.
497 */
498 if (instance && hdev->cur_adv_instance == instance)
499 next_instance = hci_get_next_instance(hdev, instance);
500
501 if (instance == 0x00) {
502 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
503 list) {
504 if (!(force || adv_instance->timeout))
505 continue;
506
507 rem_inst = adv_instance->instance;
508 err = hci_remove_adv_instance(hdev, rem_inst);
509 if (!err)
510 mgmt_advertising_removed(sk, hdev, rem_inst);
511 }
512 } else {
513 adv_instance = hci_find_adv_instance(hdev, instance);
514
515 if (force || (adv_instance && adv_instance->timeout &&
516 !adv_instance->remaining_time)) {
517 /* Don't advertise a removed instance. */
518 if (next_instance &&
519 next_instance->instance == instance)
520 next_instance = NULL;
521
522 err = hci_remove_adv_instance(hdev, instance);
523 if (!err)
524 mgmt_advertising_removed(sk, hdev, instance);
525 }
526 }
527
528 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
529 return 0;
530
531 if (next_instance && !ext_adv_capable(hdev))
532 return hci_schedule_adv_instance_sync(hdev,
533 next_instance->instance,
534 false);
535
536 return 0;
537 }
538
adv_timeout_expire_sync(struct hci_dev * hdev,void * data)539 static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data)
540 {
541 u8 instance = *(u8 *)data;
542
543 kfree(data);
544
545 hci_clear_adv_instance_sync(hdev, NULL, instance, false);
546
547 if (list_empty(&hdev->adv_instances))
548 return hci_disable_advertising_sync(hdev);
549
550 return 0;
551 }
552
adv_timeout_expire(struct work_struct * work)553 static void adv_timeout_expire(struct work_struct *work)
554 {
555 u8 *inst_ptr;
556 struct hci_dev *hdev = container_of(work, struct hci_dev,
557 adv_instance_expire.work);
558
559 bt_dev_dbg(hdev, "");
560
561 hci_dev_lock(hdev);
562
563 hdev->adv_instance_timeout = 0;
564
565 if (hdev->cur_adv_instance == 0x00)
566 goto unlock;
567
568 inst_ptr = kmalloc(1, GFP_KERNEL);
569 if (!inst_ptr)
570 goto unlock;
571
572 *inst_ptr = hdev->cur_adv_instance;
573 hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL);
574
575 unlock:
576 hci_dev_unlock(hdev);
577 }
578
is_interleave_scanning(struct hci_dev * hdev)579 static bool is_interleave_scanning(struct hci_dev *hdev)
580 {
581 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
582 }
583
584 static int hci_passive_scan_sync(struct hci_dev *hdev);
585
interleave_scan_work(struct work_struct * work)586 static void interleave_scan_work(struct work_struct *work)
587 {
588 struct hci_dev *hdev = container_of(work, struct hci_dev,
589 interleave_scan.work);
590 unsigned long timeout;
591
592 if (hdev->interleave_scan_state == INTERLEAVE_SCAN_ALLOWLIST) {
593 timeout = msecs_to_jiffies(hdev->advmon_allowlist_duration);
594 } else if (hdev->interleave_scan_state == INTERLEAVE_SCAN_NO_FILTER) {
595 timeout = msecs_to_jiffies(hdev->advmon_no_filter_duration);
596 } else {
597 bt_dev_err(hdev, "unexpected error");
598 return;
599 }
600
601 hci_passive_scan_sync(hdev);
602
603 hci_dev_lock(hdev);
604
605 switch (hdev->interleave_scan_state) {
606 case INTERLEAVE_SCAN_ALLOWLIST:
607 bt_dev_dbg(hdev, "next state: allowlist");
608 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
609 break;
610 case INTERLEAVE_SCAN_NO_FILTER:
611 bt_dev_dbg(hdev, "next state: no filter");
612 hdev->interleave_scan_state = INTERLEAVE_SCAN_ALLOWLIST;
613 break;
614 case INTERLEAVE_SCAN_NONE:
615 bt_dev_err(hdev, "unexpected error");
616 }
617
618 hci_dev_unlock(hdev);
619
620 /* Don't continue interleaving if it was canceled */
621 if (is_interleave_scanning(hdev))
622 queue_delayed_work(hdev->req_workqueue,
623 &hdev->interleave_scan, timeout);
624 }
625
hci_cmd_sync_init(struct hci_dev * hdev)626 void hci_cmd_sync_init(struct hci_dev *hdev)
627 {
628 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
629 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
630 mutex_init(&hdev->cmd_sync_work_lock);
631 mutex_init(&hdev->unregister_lock);
632
633 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
634 INIT_WORK(&hdev->reenable_adv_work, reenable_adv);
635 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable);
636 INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
637 INIT_DELAYED_WORK(&hdev->interleave_scan, interleave_scan_work);
638 }
639
_hci_cmd_sync_cancel_entry(struct hci_dev * hdev,struct hci_cmd_sync_work_entry * entry,int err)640 static void _hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
641 struct hci_cmd_sync_work_entry *entry,
642 int err)
643 {
644 if (entry->destroy)
645 entry->destroy(hdev, entry->data, err);
646
647 list_del(&entry->list);
648 kfree(entry);
649 }
650
hci_cmd_sync_clear(struct hci_dev * hdev)651 void hci_cmd_sync_clear(struct hci_dev *hdev)
652 {
653 struct hci_cmd_sync_work_entry *entry, *tmp;
654
655 cancel_work_sync(&hdev->cmd_sync_work);
656 cancel_work_sync(&hdev->reenable_adv_work);
657
658 mutex_lock(&hdev->cmd_sync_work_lock);
659 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list)
660 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
661 mutex_unlock(&hdev->cmd_sync_work_lock);
662 }
663
hci_cmd_sync_cancel(struct hci_dev * hdev,int err)664 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
665 {
666 bt_dev_dbg(hdev, "err 0x%2.2x", err);
667
668 if (hdev->req_status == HCI_REQ_PEND) {
669 hdev->req_result = err;
670 hdev->req_status = HCI_REQ_CANCELED;
671
672 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
673 }
674 }
675 EXPORT_SYMBOL(hci_cmd_sync_cancel);
676
677 /* Cancel ongoing command request synchronously:
678 *
679 * - Set result and mark status to HCI_REQ_CANCELED
680 * - Wakeup command sync thread
681 */
hci_cmd_sync_cancel_sync(struct hci_dev * hdev,int err)682 void hci_cmd_sync_cancel_sync(struct hci_dev *hdev, int err)
683 {
684 bt_dev_dbg(hdev, "err 0x%2.2x", err);
685
686 if (hdev->req_status == HCI_REQ_PEND) {
687 /* req_result is __u32 so error must be positive to be properly
688 * propagated.
689 */
690 hdev->req_result = err < 0 ? -err : err;
691 hdev->req_status = HCI_REQ_CANCELED;
692
693 wake_up_interruptible(&hdev->req_wait_q);
694 }
695 }
696 EXPORT_SYMBOL(hci_cmd_sync_cancel_sync);
697
698 /* Submit HCI command to be run in as cmd_sync_work:
699 *
700 * - hdev must _not_ be unregistered
701 */
hci_cmd_sync_submit(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)702 int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
703 void *data, hci_cmd_sync_work_destroy_t destroy)
704 {
705 struct hci_cmd_sync_work_entry *entry;
706 int err = 0;
707
708 mutex_lock(&hdev->unregister_lock);
709 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
710 err = -ENODEV;
711 goto unlock;
712 }
713
714 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
715 if (!entry) {
716 err = -ENOMEM;
717 goto unlock;
718 }
719 entry->func = func;
720 entry->data = data;
721 entry->destroy = destroy;
722
723 mutex_lock(&hdev->cmd_sync_work_lock);
724 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
725 mutex_unlock(&hdev->cmd_sync_work_lock);
726
727 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
728
729 unlock:
730 mutex_unlock(&hdev->unregister_lock);
731 return err;
732 }
733 EXPORT_SYMBOL(hci_cmd_sync_submit);
734
735 /* Queue HCI command:
736 *
737 * - hdev must be running
738 */
hci_cmd_sync_queue(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)739 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
740 void *data, hci_cmd_sync_work_destroy_t destroy)
741 {
742 /* Only queue command if hdev is running which means it had been opened
743 * and is either on init phase or is already up.
744 */
745 if (!test_bit(HCI_RUNNING, &hdev->flags))
746 return -ENETDOWN;
747
748 return hci_cmd_sync_submit(hdev, func, data, destroy);
749 }
750 EXPORT_SYMBOL(hci_cmd_sync_queue);
751
752 static struct hci_cmd_sync_work_entry *
_hci_cmd_sync_lookup_entry(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)753 _hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
754 void *data, hci_cmd_sync_work_destroy_t destroy)
755 {
756 struct hci_cmd_sync_work_entry *entry, *tmp;
757
758 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
759 if (func && entry->func != func)
760 continue;
761
762 if (data && entry->data != data)
763 continue;
764
765 if (destroy && entry->destroy != destroy)
766 continue;
767
768 return entry;
769 }
770
771 return NULL;
772 }
773
774 /* Queue HCI command entry once:
775 *
776 * - Lookup if an entry already exist and only if it doesn't creates a new entry
777 * and queue it.
778 */
hci_cmd_sync_queue_once(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)779 int hci_cmd_sync_queue_once(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
780 void *data, hci_cmd_sync_work_destroy_t destroy)
781 {
782 if (hci_cmd_sync_lookup_entry(hdev, func, data, destroy))
783 return 0;
784
785 return hci_cmd_sync_queue(hdev, func, data, destroy);
786 }
787 EXPORT_SYMBOL(hci_cmd_sync_queue_once);
788
789 /* Run HCI command:
790 *
791 * - hdev must be running
792 * - if on cmd_sync_work then run immediately otherwise queue
793 */
hci_cmd_sync_run(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)794 int hci_cmd_sync_run(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
795 void *data, hci_cmd_sync_work_destroy_t destroy)
796 {
797 /* Only queue command if hdev is running which means it had been opened
798 * and is either on init phase or is already up.
799 */
800 if (!test_bit(HCI_RUNNING, &hdev->flags))
801 return -ENETDOWN;
802
803 /* If on cmd_sync_work then run immediately otherwise queue */
804 if (current_work() == &hdev->cmd_sync_work)
805 return func(hdev, data);
806
807 return hci_cmd_sync_submit(hdev, func, data, destroy);
808 }
809 EXPORT_SYMBOL(hci_cmd_sync_run);
810
811 /* Run HCI command entry once:
812 *
813 * - Lookup if an entry already exist and only if it doesn't creates a new entry
814 * and run it.
815 * - if on cmd_sync_work then run immediately otherwise queue
816 */
hci_cmd_sync_run_once(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)817 int hci_cmd_sync_run_once(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
818 void *data, hci_cmd_sync_work_destroy_t destroy)
819 {
820 if (hci_cmd_sync_lookup_entry(hdev, func, data, destroy))
821 return 0;
822
823 return hci_cmd_sync_run(hdev, func, data, destroy);
824 }
825 EXPORT_SYMBOL(hci_cmd_sync_run_once);
826
827 /* Lookup HCI command entry:
828 *
829 * - Return first entry that matches by function callback or data or
830 * destroy callback.
831 */
832 struct hci_cmd_sync_work_entry *
hci_cmd_sync_lookup_entry(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)833 hci_cmd_sync_lookup_entry(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
834 void *data, hci_cmd_sync_work_destroy_t destroy)
835 {
836 struct hci_cmd_sync_work_entry *entry;
837
838 mutex_lock(&hdev->cmd_sync_work_lock);
839 entry = _hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
840 mutex_unlock(&hdev->cmd_sync_work_lock);
841
842 return entry;
843 }
844 EXPORT_SYMBOL(hci_cmd_sync_lookup_entry);
845
846 /* Cancel HCI command entry */
hci_cmd_sync_cancel_entry(struct hci_dev * hdev,struct hci_cmd_sync_work_entry * entry)847 void hci_cmd_sync_cancel_entry(struct hci_dev *hdev,
848 struct hci_cmd_sync_work_entry *entry)
849 {
850 mutex_lock(&hdev->cmd_sync_work_lock);
851 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
852 mutex_unlock(&hdev->cmd_sync_work_lock);
853 }
854 EXPORT_SYMBOL(hci_cmd_sync_cancel_entry);
855
856 /* Dequeue one HCI command entry:
857 *
858 * - Lookup and cancel first entry that matches.
859 */
hci_cmd_sync_dequeue_once(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)860 bool hci_cmd_sync_dequeue_once(struct hci_dev *hdev,
861 hci_cmd_sync_work_func_t func,
862 void *data, hci_cmd_sync_work_destroy_t destroy)
863 {
864 struct hci_cmd_sync_work_entry *entry;
865
866 entry = hci_cmd_sync_lookup_entry(hdev, func, data, destroy);
867 if (!entry)
868 return false;
869
870 hci_cmd_sync_cancel_entry(hdev, entry);
871
872 return true;
873 }
874 EXPORT_SYMBOL(hci_cmd_sync_dequeue_once);
875
876 /* Dequeue HCI command entry:
877 *
878 * - Lookup and cancel any entry that matches by function callback or data or
879 * destroy callback.
880 */
hci_cmd_sync_dequeue(struct hci_dev * hdev,hci_cmd_sync_work_func_t func,void * data,hci_cmd_sync_work_destroy_t destroy)881 bool hci_cmd_sync_dequeue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
882 void *data, hci_cmd_sync_work_destroy_t destroy)
883 {
884 struct hci_cmd_sync_work_entry *entry;
885 bool ret = false;
886
887 mutex_lock(&hdev->cmd_sync_work_lock);
888 while ((entry = _hci_cmd_sync_lookup_entry(hdev, func, data,
889 destroy))) {
890 _hci_cmd_sync_cancel_entry(hdev, entry, -ECANCELED);
891 ret = true;
892 }
893 mutex_unlock(&hdev->cmd_sync_work_lock);
894
895 return ret;
896 }
897 EXPORT_SYMBOL(hci_cmd_sync_dequeue);
898
hci_update_eir_sync(struct hci_dev * hdev)899 int hci_update_eir_sync(struct hci_dev *hdev)
900 {
901 struct hci_cp_write_eir cp;
902
903 bt_dev_dbg(hdev, "");
904
905 if (!hdev_is_powered(hdev))
906 return 0;
907
908 if (!lmp_ext_inq_capable(hdev))
909 return 0;
910
911 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
912 return 0;
913
914 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
915 return 0;
916
917 memset(&cp, 0, sizeof(cp));
918
919 eir_create(hdev, cp.data);
920
921 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
922 return 0;
923
924 memcpy(hdev->eir, cp.data, sizeof(cp.data));
925
926 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
927 HCI_CMD_TIMEOUT);
928 }
929
get_service_classes(struct hci_dev * hdev)930 static u8 get_service_classes(struct hci_dev *hdev)
931 {
932 struct bt_uuid *uuid;
933 u8 val = 0;
934
935 list_for_each_entry(uuid, &hdev->uuids, list)
936 val |= uuid->svc_hint;
937
938 return val;
939 }
940
hci_update_class_sync(struct hci_dev * hdev)941 int hci_update_class_sync(struct hci_dev *hdev)
942 {
943 u8 cod[3];
944
945 bt_dev_dbg(hdev, "");
946
947 if (!hdev_is_powered(hdev))
948 return 0;
949
950 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
951 return 0;
952
953 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
954 return 0;
955
956 cod[0] = hdev->minor_class;
957 cod[1] = hdev->major_class;
958 cod[2] = get_service_classes(hdev);
959
960 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
961 cod[1] |= 0x20;
962
963 if (memcmp(cod, hdev->dev_class, 3) == 0)
964 return 0;
965
966 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
967 sizeof(cod), cod, HCI_CMD_TIMEOUT);
968 }
969
is_advertising_allowed(struct hci_dev * hdev,bool connectable)970 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
971 {
972 /* If there is no connection we are OK to advertise. */
973 if (hci_conn_num(hdev, LE_LINK) == 0)
974 return true;
975
976 /* Check le_states if there is any connection in peripheral role. */
977 if (hdev->conn_hash.le_num_peripheral > 0) {
978 /* Peripheral connection state and non connectable mode
979 * bit 20.
980 */
981 if (!connectable && !(hdev->le_states[2] & 0x10))
982 return false;
983
984 /* Peripheral connection state and connectable mode bit 38
985 * and scannable bit 21.
986 */
987 if (connectable && (!(hdev->le_states[4] & 0x40) ||
988 !(hdev->le_states[2] & 0x20)))
989 return false;
990 }
991
992 /* Check le_states if there is any connection in central role. */
993 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
994 /* Central connection state and non connectable mode bit 18. */
995 if (!connectable && !(hdev->le_states[2] & 0x02))
996 return false;
997
998 /* Central connection state and connectable mode bit 35 and
999 * scannable 19.
1000 */
1001 if (connectable && (!(hdev->le_states[4] & 0x08) ||
1002 !(hdev->le_states[2] & 0x08)))
1003 return false;
1004 }
1005
1006 return true;
1007 }
1008
adv_use_rpa(struct hci_dev * hdev,uint32_t flags)1009 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
1010 {
1011 /* If privacy is not enabled don't use RPA */
1012 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
1013 return false;
1014
1015 /* If basic privacy mode is enabled use RPA */
1016 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
1017 return true;
1018
1019 /* If limited privacy mode is enabled don't use RPA if we're
1020 * both discoverable and bondable.
1021 */
1022 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
1023 hci_dev_test_flag(hdev, HCI_BONDABLE))
1024 return false;
1025
1026 /* We're neither bondable nor discoverable in the limited
1027 * privacy mode, therefore use RPA.
1028 */
1029 return true;
1030 }
1031
hci_set_random_addr_sync(struct hci_dev * hdev,bdaddr_t * rpa)1032 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
1033 {
1034 /* If a random_addr has been set we're advertising or initiating an LE
1035 * connection we can't go ahead and change the random address at this
1036 * time. This is because the eventual initiator address used for the
1037 * subsequently created connection will be undefined (some
1038 * controllers use the new address and others the one we had
1039 * when the operation started).
1040 *
1041 * In this kind of scenario skip the update and let the random
1042 * address be updated at the next cycle.
1043 */
1044 if (bacmp(&hdev->random_addr, BDADDR_ANY) &&
1045 (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
1046 hci_lookup_le_connect(hdev))) {
1047 bt_dev_dbg(hdev, "Deferring random address update");
1048 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
1049 return 0;
1050 }
1051
1052 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
1053 6, rpa, HCI_CMD_TIMEOUT);
1054 }
1055
hci_update_random_address_sync(struct hci_dev * hdev,bool require_privacy,bool rpa,u8 * own_addr_type)1056 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
1057 bool rpa, u8 *own_addr_type)
1058 {
1059 int err;
1060
1061 /* If privacy is enabled use a resolvable private address. If
1062 * current RPA has expired or there is something else than
1063 * the current RPA in use, then generate a new one.
1064 */
1065 if (rpa) {
1066 /* If Controller supports LL Privacy use own address type is
1067 * 0x03
1068 */
1069 if (ll_privacy_capable(hdev))
1070 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
1071 else
1072 *own_addr_type = ADDR_LE_DEV_RANDOM;
1073
1074 /* Check if RPA is valid */
1075 if (rpa_valid(hdev))
1076 return 0;
1077
1078 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
1079 if (err < 0) {
1080 bt_dev_err(hdev, "failed to generate new RPA");
1081 return err;
1082 }
1083
1084 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
1085 if (err)
1086 return err;
1087
1088 return 0;
1089 }
1090
1091 /* In case of required privacy without resolvable private address,
1092 * use an non-resolvable private address. This is useful for active
1093 * scanning and non-connectable advertising.
1094 */
1095 if (require_privacy) {
1096 bdaddr_t nrpa;
1097
1098 while (true) {
1099 /* The non-resolvable private address is generated
1100 * from random six bytes with the two most significant
1101 * bits cleared.
1102 */
1103 get_random_bytes(&nrpa, 6);
1104 nrpa.b[5] &= 0x3f;
1105
1106 /* The non-resolvable private address shall not be
1107 * equal to the public address.
1108 */
1109 if (bacmp(&hdev->bdaddr, &nrpa))
1110 break;
1111 }
1112
1113 *own_addr_type = ADDR_LE_DEV_RANDOM;
1114
1115 return hci_set_random_addr_sync(hdev, &nrpa);
1116 }
1117
1118 /* If forcing static address is in use or there is no public
1119 * address use the static address as random address (but skip
1120 * the HCI command if the current random address is already the
1121 * static one.
1122 *
1123 * In case BR/EDR has been disabled on a dual-mode controller
1124 * and a static address has been configured, then use that
1125 * address instead of the public BR/EDR address.
1126 */
1127 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
1128 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
1129 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
1130 bacmp(&hdev->static_addr, BDADDR_ANY))) {
1131 *own_addr_type = ADDR_LE_DEV_RANDOM;
1132 if (bacmp(&hdev->static_addr, &hdev->random_addr))
1133 return hci_set_random_addr_sync(hdev,
1134 &hdev->static_addr);
1135 return 0;
1136 }
1137
1138 /* Neither privacy nor static address is being used so use a
1139 * public address.
1140 */
1141 *own_addr_type = ADDR_LE_DEV_PUBLIC;
1142
1143 return 0;
1144 }
1145
hci_disable_ext_adv_instance_sync(struct hci_dev * hdev,u8 instance)1146 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1147 {
1148 struct hci_cp_le_set_ext_adv_enable *cp;
1149 struct hci_cp_ext_adv_set *set;
1150 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1151 u8 size;
1152 struct adv_info *adv = NULL;
1153
1154 /* If request specifies an instance that doesn't exist, fail */
1155 if (instance > 0) {
1156 adv = hci_find_adv_instance(hdev, instance);
1157 if (!adv)
1158 return -EINVAL;
1159
1160 /* If not enabled there is nothing to do */
1161 if (!adv->enabled)
1162 return 0;
1163 }
1164
1165 memset(data, 0, sizeof(data));
1166
1167 cp = (void *)data;
1168 set = (void *)cp->data;
1169
1170 /* Instance 0x00 indicates all advertising instances will be disabled */
1171 cp->num_of_sets = !!instance;
1172 cp->enable = 0x00;
1173
1174 set->handle = adv ? adv->handle : instance;
1175
1176 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
1177
1178 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1179 size, data, HCI_CMD_TIMEOUT);
1180 }
1181
hci_set_adv_set_random_addr_sync(struct hci_dev * hdev,u8 instance,bdaddr_t * random_addr)1182 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
1183 bdaddr_t *random_addr)
1184 {
1185 struct hci_cp_le_set_adv_set_rand_addr cp;
1186 int err;
1187
1188 if (!instance) {
1189 /* Instance 0x00 doesn't have an adv_info, instead it uses
1190 * hdev->random_addr to track its address so whenever it needs
1191 * to be updated this also set the random address since
1192 * hdev->random_addr is shared with scan state machine.
1193 */
1194 err = hci_set_random_addr_sync(hdev, random_addr);
1195 if (err)
1196 return err;
1197 }
1198
1199 memset(&cp, 0, sizeof(cp));
1200
1201 cp.handle = instance;
1202 bacpy(&cp.bdaddr, random_addr);
1203
1204 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1205 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1206 }
1207
hci_setup_ext_adv_instance_sync(struct hci_dev * hdev,u8 instance)1208 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
1209 {
1210 struct hci_cp_le_set_ext_adv_params cp;
1211 bool connectable;
1212 u32 flags;
1213 bdaddr_t random_addr;
1214 u8 own_addr_type;
1215 int err;
1216 struct adv_info *adv;
1217 bool secondary_adv;
1218
1219 if (instance > 0) {
1220 adv = hci_find_adv_instance(hdev, instance);
1221 if (!adv)
1222 return -EINVAL;
1223 } else {
1224 adv = NULL;
1225 }
1226
1227 /* Updating parameters of an active instance will return a
1228 * Command Disallowed error, so we must first disable the
1229 * instance if it is active.
1230 */
1231 if (adv && !adv->pending) {
1232 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1233 if (err)
1234 return err;
1235 }
1236
1237 flags = hci_adv_instance_flags(hdev, instance);
1238
1239 /* If the "connectable" instance flag was not set, then choose between
1240 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1241 */
1242 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1243 mgmt_get_connectable(hdev);
1244
1245 if (!is_advertising_allowed(hdev, connectable))
1246 return -EPERM;
1247
1248 /* Set require_privacy to true only when non-connectable
1249 * advertising is used. In that case it is fine to use a
1250 * non-resolvable private address.
1251 */
1252 err = hci_get_random_address(hdev, !connectable,
1253 adv_use_rpa(hdev, flags), adv,
1254 &own_addr_type, &random_addr);
1255 if (err < 0)
1256 return err;
1257
1258 memset(&cp, 0, sizeof(cp));
1259
1260 if (adv) {
1261 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
1262 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
1263 cp.tx_power = adv->tx_power;
1264 cp.sid = adv->sid;
1265 } else {
1266 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
1267 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
1268 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
1269 cp.sid = 0x00;
1270 }
1271
1272 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
1273
1274 if (connectable) {
1275 if (secondary_adv)
1276 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
1277 else
1278 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
1279 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
1280 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
1281 if (secondary_adv)
1282 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
1283 else
1284 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
1285 } else {
1286 if (secondary_adv)
1287 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
1288 else
1289 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
1290 }
1291
1292 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
1293 * contains the peer’s Identity Address and the Peer_Address_Type
1294 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
1295 * These parameters are used to locate the corresponding local IRK in
1296 * the resolving list; this IRK is used to generate their own address
1297 * used in the advertisement.
1298 */
1299 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
1300 hci_copy_identity_address(hdev, &cp.peer_addr,
1301 &cp.peer_addr_type);
1302
1303 cp.own_addr_type = own_addr_type;
1304 cp.channel_map = hdev->le_adv_channel_map;
1305 cp.handle = adv ? adv->handle : instance;
1306
1307 if (flags & MGMT_ADV_FLAG_SEC_2M) {
1308 cp.primary_phy = HCI_ADV_PHY_1M;
1309 cp.secondary_phy = HCI_ADV_PHY_2M;
1310 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
1311 cp.primary_phy = HCI_ADV_PHY_CODED;
1312 cp.secondary_phy = HCI_ADV_PHY_CODED;
1313 } else {
1314 /* In all other cases use 1M */
1315 cp.primary_phy = HCI_ADV_PHY_1M;
1316 cp.secondary_phy = HCI_ADV_PHY_1M;
1317 }
1318
1319 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
1320 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1321 if (err)
1322 return err;
1323
1324 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
1325 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
1326 bacmp(&random_addr, BDADDR_ANY)) {
1327 /* Check if random address need to be updated */
1328 if (adv) {
1329 if (!bacmp(&random_addr, &adv->random_addr))
1330 return 0;
1331 } else {
1332 if (!bacmp(&random_addr, &hdev->random_addr))
1333 return 0;
1334 }
1335
1336 return hci_set_adv_set_random_addr_sync(hdev, instance,
1337 &random_addr);
1338 }
1339
1340 return 0;
1341 }
1342
hci_set_ext_scan_rsp_data_sync(struct hci_dev * hdev,u8 instance)1343 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1344 {
1345 DEFINE_FLEX(struct hci_cp_le_set_ext_scan_rsp_data, pdu, data, length,
1346 HCI_MAX_EXT_AD_LENGTH);
1347 u8 len;
1348 struct adv_info *adv = NULL;
1349 int err;
1350
1351 if (instance) {
1352 adv = hci_find_adv_instance(hdev, instance);
1353 if (!adv || !adv->scan_rsp_changed)
1354 return 0;
1355 }
1356
1357 len = eir_create_scan_rsp(hdev, instance, pdu->data);
1358
1359 pdu->handle = adv ? adv->handle : instance;
1360 pdu->length = len;
1361 pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
1362 pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1363
1364 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
1365 struct_size(pdu, data, len), pdu,
1366 HCI_CMD_TIMEOUT);
1367 if (err)
1368 return err;
1369
1370 if (adv) {
1371 adv->scan_rsp_changed = false;
1372 } else {
1373 memcpy(hdev->scan_rsp_data, pdu->data, len);
1374 hdev->scan_rsp_data_len = len;
1375 }
1376
1377 return 0;
1378 }
1379
__hci_set_scan_rsp_data_sync(struct hci_dev * hdev,u8 instance)1380 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1381 {
1382 struct hci_cp_le_set_scan_rsp_data cp;
1383 u8 len;
1384
1385 memset(&cp, 0, sizeof(cp));
1386
1387 len = eir_create_scan_rsp(hdev, instance, cp.data);
1388
1389 if (hdev->scan_rsp_data_len == len &&
1390 !memcmp(cp.data, hdev->scan_rsp_data, len))
1391 return 0;
1392
1393 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
1394 hdev->scan_rsp_data_len = len;
1395
1396 cp.length = len;
1397
1398 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
1399 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1400 }
1401
hci_update_scan_rsp_data_sync(struct hci_dev * hdev,u8 instance)1402 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
1403 {
1404 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1405 return 0;
1406
1407 if (ext_adv_capable(hdev))
1408 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
1409
1410 return __hci_set_scan_rsp_data_sync(hdev, instance);
1411 }
1412
hci_enable_ext_advertising_sync(struct hci_dev * hdev,u8 instance)1413 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
1414 {
1415 struct hci_cp_le_set_ext_adv_enable *cp;
1416 struct hci_cp_ext_adv_set *set;
1417 u8 data[sizeof(*cp) + sizeof(*set) * 1];
1418 struct adv_info *adv;
1419
1420 if (instance > 0) {
1421 adv = hci_find_adv_instance(hdev, instance);
1422 if (!adv)
1423 return -EINVAL;
1424 /* If already enabled there is nothing to do */
1425 if (adv->enabled)
1426 return 0;
1427 } else {
1428 adv = NULL;
1429 }
1430
1431 cp = (void *)data;
1432 set = (void *)cp->data;
1433
1434 memset(cp, 0, sizeof(*cp));
1435
1436 cp->enable = 0x01;
1437 cp->num_of_sets = 0x01;
1438
1439 memset(set, 0, sizeof(*set));
1440
1441 set->handle = adv ? adv->handle : instance;
1442
1443 /* Set duration per instance since controller is responsible for
1444 * scheduling it.
1445 */
1446 if (adv && adv->timeout) {
1447 u16 duration = adv->timeout * MSEC_PER_SEC;
1448
1449 /* Time = N * 10 ms */
1450 set->duration = cpu_to_le16(duration / 10);
1451 }
1452
1453 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1454 sizeof(*cp) +
1455 sizeof(*set) * cp->num_of_sets,
1456 data, HCI_CMD_TIMEOUT);
1457 }
1458
hci_start_ext_adv_sync(struct hci_dev * hdev,u8 instance)1459 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
1460 {
1461 int err;
1462
1463 err = hci_setup_ext_adv_instance_sync(hdev, instance);
1464 if (err)
1465 return err;
1466
1467 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
1468 if (err)
1469 return err;
1470
1471 return hci_enable_ext_advertising_sync(hdev, instance);
1472 }
1473
hci_disable_per_advertising_sync(struct hci_dev * hdev,u8 instance)1474 int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1475 {
1476 struct hci_cp_le_set_per_adv_enable cp;
1477 struct adv_info *adv = NULL;
1478
1479 /* If periodic advertising already disabled there is nothing to do. */
1480 adv = hci_find_adv_instance(hdev, instance);
1481 if (!adv || !adv->periodic || !adv->enabled)
1482 return 0;
1483
1484 memset(&cp, 0, sizeof(cp));
1485
1486 cp.enable = 0x00;
1487 cp.handle = instance;
1488
1489 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1490 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1491 }
1492
hci_set_per_adv_params_sync(struct hci_dev * hdev,u8 instance,u16 min_interval,u16 max_interval)1493 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
1494 u16 min_interval, u16 max_interval)
1495 {
1496 struct hci_cp_le_set_per_adv_params cp;
1497
1498 memset(&cp, 0, sizeof(cp));
1499
1500 if (!min_interval)
1501 min_interval = DISCOV_LE_PER_ADV_INT_MIN;
1502
1503 if (!max_interval)
1504 max_interval = DISCOV_LE_PER_ADV_INT_MAX;
1505
1506 cp.handle = instance;
1507 cp.min_interval = cpu_to_le16(min_interval);
1508 cp.max_interval = cpu_to_le16(max_interval);
1509 cp.periodic_properties = 0x0000;
1510
1511 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS,
1512 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1513 }
1514
hci_set_per_adv_data_sync(struct hci_dev * hdev,u8 instance)1515 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance)
1516 {
1517 DEFINE_FLEX(struct hci_cp_le_set_per_adv_data, pdu, data, length,
1518 HCI_MAX_PER_AD_LENGTH);
1519 u8 len;
1520 struct adv_info *adv = NULL;
1521
1522 if (instance) {
1523 adv = hci_find_adv_instance(hdev, instance);
1524 if (!adv || !adv->periodic)
1525 return 0;
1526 }
1527
1528 len = eir_create_per_adv_data(hdev, instance, pdu->data);
1529
1530 pdu->length = len;
1531 pdu->handle = adv ? adv->handle : instance;
1532 pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
1533
1534 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA,
1535 struct_size(pdu, data, len), pdu,
1536 HCI_CMD_TIMEOUT);
1537 }
1538
hci_enable_per_advertising_sync(struct hci_dev * hdev,u8 instance)1539 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
1540 {
1541 struct hci_cp_le_set_per_adv_enable cp;
1542 struct adv_info *adv = NULL;
1543
1544 /* If periodic advertising already enabled there is nothing to do. */
1545 adv = hci_find_adv_instance(hdev, instance);
1546 if (adv && adv->periodic && adv->enabled)
1547 return 0;
1548
1549 memset(&cp, 0, sizeof(cp));
1550
1551 cp.enable = 0x01;
1552 cp.handle = instance;
1553
1554 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
1555 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1556 }
1557
1558 /* Checks if periodic advertising data contains a Basic Announcement and if it
1559 * does generates a Broadcast ID and add Broadcast Announcement.
1560 */
hci_adv_bcast_annoucement(struct hci_dev * hdev,struct adv_info * adv)1561 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv)
1562 {
1563 u8 bid[3];
1564 u8 ad[HCI_MAX_EXT_AD_LENGTH];
1565 u8 len;
1566
1567 /* Skip if NULL adv as instance 0x00 is used for general purpose
1568 * advertising so it cannot used for the likes of Broadcast Announcement
1569 * as it can be overwritten at any point.
1570 */
1571 if (!adv)
1572 return 0;
1573
1574 /* Check if PA data doesn't contains a Basic Audio Announcement then
1575 * there is nothing to do.
1576 */
1577 if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len,
1578 0x1851, NULL))
1579 return 0;
1580
1581 /* Check if advertising data already has a Broadcast Announcement since
1582 * the process may want to control the Broadcast ID directly and in that
1583 * case the kernel shall no interfere.
1584 */
1585 if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852,
1586 NULL))
1587 return 0;
1588
1589 /* Generate Broadcast ID */
1590 get_random_bytes(bid, sizeof(bid));
1591 len = eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid));
1592 memcpy(ad + len, adv->adv_data, adv->adv_data_len);
1593 hci_set_adv_instance_data(hdev, adv->instance, len + adv->adv_data_len,
1594 ad, 0, NULL);
1595
1596 return hci_update_adv_data_sync(hdev, adv->instance);
1597 }
1598
hci_start_per_adv_sync(struct hci_dev * hdev,u8 instance,u8 sid,u8 data_len,u8 * data,u32 flags,u16 min_interval,u16 max_interval,u16 sync_interval)1599 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 sid,
1600 u8 data_len, u8 *data, u32 flags, u16 min_interval,
1601 u16 max_interval, u16 sync_interval)
1602 {
1603 struct adv_info *adv = NULL;
1604 int err;
1605 bool added = false;
1606
1607 hci_disable_per_advertising_sync(hdev, instance);
1608
1609 if (instance) {
1610 adv = hci_find_adv_instance(hdev, instance);
1611 if (adv) {
1612 if (sid != HCI_SID_INVALID && adv->sid != sid) {
1613 /* If the SID don't match attempt to find by
1614 * SID.
1615 */
1616 adv = hci_find_adv_sid(hdev, sid);
1617 if (!adv) {
1618 bt_dev_err(hdev,
1619 "Unable to find adv_info");
1620 return -EINVAL;
1621 }
1622 }
1623
1624 /* Turn it into periodic advertising */
1625 adv->periodic = true;
1626 adv->per_adv_data_len = data_len;
1627 if (data)
1628 memcpy(adv->per_adv_data, data, data_len);
1629 adv->flags = flags;
1630 } else if (!adv) {
1631 /* Create an instance if that could not be found */
1632 adv = hci_add_per_instance(hdev, instance, sid, flags,
1633 data_len, data,
1634 sync_interval,
1635 sync_interval);
1636 if (IS_ERR(adv))
1637 return PTR_ERR(adv);
1638 adv->pending = false;
1639 added = true;
1640 }
1641 }
1642
1643 /* Start advertising */
1644 err = hci_start_ext_adv_sync(hdev, instance);
1645 if (err < 0)
1646 goto fail;
1647
1648 err = hci_adv_bcast_annoucement(hdev, adv);
1649 if (err < 0)
1650 goto fail;
1651
1652 err = hci_set_per_adv_params_sync(hdev, instance, min_interval,
1653 max_interval);
1654 if (err < 0)
1655 goto fail;
1656
1657 err = hci_set_per_adv_data_sync(hdev, instance);
1658 if (err < 0)
1659 goto fail;
1660
1661 err = hci_enable_per_advertising_sync(hdev, instance);
1662 if (err < 0)
1663 goto fail;
1664
1665 return 0;
1666
1667 fail:
1668 if (added)
1669 hci_remove_adv_instance(hdev, instance);
1670
1671 return err;
1672 }
1673
hci_start_adv_sync(struct hci_dev * hdev,u8 instance)1674 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
1675 {
1676 int err;
1677
1678 if (ext_adv_capable(hdev))
1679 return hci_start_ext_adv_sync(hdev, instance);
1680
1681 err = hci_update_adv_data_sync(hdev, instance);
1682 if (err)
1683 return err;
1684
1685 err = hci_update_scan_rsp_data_sync(hdev, instance);
1686 if (err)
1687 return err;
1688
1689 return hci_enable_advertising_sync(hdev);
1690 }
1691
hci_enable_advertising_sync(struct hci_dev * hdev)1692 int hci_enable_advertising_sync(struct hci_dev *hdev)
1693 {
1694 struct adv_info *adv_instance;
1695 struct hci_cp_le_set_adv_param cp;
1696 u8 own_addr_type, enable = 0x01;
1697 bool connectable;
1698 u16 adv_min_interval, adv_max_interval;
1699 u32 flags;
1700 u8 status;
1701
1702 if (ext_adv_capable(hdev))
1703 return hci_enable_ext_advertising_sync(hdev,
1704 hdev->cur_adv_instance);
1705
1706 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1707 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1708
1709 /* If the "connectable" instance flag was not set, then choose between
1710 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1711 */
1712 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1713 mgmt_get_connectable(hdev);
1714
1715 if (!is_advertising_allowed(hdev, connectable))
1716 return -EINVAL;
1717
1718 status = hci_disable_advertising_sync(hdev);
1719 if (status)
1720 return status;
1721
1722 /* Clear the HCI_LE_ADV bit temporarily so that the
1723 * hci_update_random_address knows that it's safe to go ahead
1724 * and write a new random address. The flag will be set back on
1725 * as soon as the SET_ADV_ENABLE HCI command completes.
1726 */
1727 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1728
1729 /* Set require_privacy to true only when non-connectable
1730 * advertising is used. In that case it is fine to use a
1731 * non-resolvable private address.
1732 */
1733 status = hci_update_random_address_sync(hdev, !connectable,
1734 adv_use_rpa(hdev, flags),
1735 &own_addr_type);
1736 if (status)
1737 return status;
1738
1739 memset(&cp, 0, sizeof(cp));
1740
1741 if (adv_instance) {
1742 adv_min_interval = adv_instance->min_interval;
1743 adv_max_interval = adv_instance->max_interval;
1744 } else {
1745 adv_min_interval = hdev->le_adv_min_interval;
1746 adv_max_interval = hdev->le_adv_max_interval;
1747 }
1748
1749 if (connectable) {
1750 cp.type = LE_ADV_IND;
1751 } else {
1752 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1753 cp.type = LE_ADV_SCAN_IND;
1754 else
1755 cp.type = LE_ADV_NONCONN_IND;
1756
1757 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1758 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1759 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1760 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1761 }
1762 }
1763
1764 cp.min_interval = cpu_to_le16(adv_min_interval);
1765 cp.max_interval = cpu_to_le16(adv_max_interval);
1766 cp.own_address_type = own_addr_type;
1767 cp.channel_map = hdev->le_adv_channel_map;
1768
1769 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1770 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1771 if (status)
1772 return status;
1773
1774 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1775 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1776 }
1777
enable_advertising_sync(struct hci_dev * hdev,void * data)1778 static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1779 {
1780 return hci_enable_advertising_sync(hdev);
1781 }
1782
hci_enable_advertising(struct hci_dev * hdev)1783 int hci_enable_advertising(struct hci_dev *hdev)
1784 {
1785 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1786 list_empty(&hdev->adv_instances))
1787 return 0;
1788
1789 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1790 }
1791
hci_remove_ext_adv_instance_sync(struct hci_dev * hdev,u8 instance,struct sock * sk)1792 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1793 struct sock *sk)
1794 {
1795 int err;
1796
1797 if (!ext_adv_capable(hdev))
1798 return 0;
1799
1800 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1801 if (err)
1802 return err;
1803
1804 /* If request specifies an instance that doesn't exist, fail */
1805 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1806 return -EINVAL;
1807
1808 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1809 sizeof(instance), &instance, 0,
1810 HCI_CMD_TIMEOUT, sk);
1811 }
1812
hci_le_terminate_big_sync(struct hci_dev * hdev,u8 handle,u8 reason)1813 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason)
1814 {
1815 struct hci_cp_le_term_big cp;
1816
1817 memset(&cp, 0, sizeof(cp));
1818 cp.handle = handle;
1819 cp.reason = reason;
1820
1821 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG,
1822 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1823 }
1824
hci_set_ext_adv_data_sync(struct hci_dev * hdev,u8 instance)1825 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1826 {
1827 DEFINE_FLEX(struct hci_cp_le_set_ext_adv_data, pdu, data, length,
1828 HCI_MAX_EXT_AD_LENGTH);
1829 u8 len;
1830 struct adv_info *adv = NULL;
1831 int err;
1832
1833 if (instance) {
1834 adv = hci_find_adv_instance(hdev, instance);
1835 if (!adv || !adv->adv_data_changed)
1836 return 0;
1837 }
1838
1839 len = eir_create_adv_data(hdev, instance, pdu->data,
1840 HCI_MAX_EXT_AD_LENGTH);
1841
1842 pdu->length = len;
1843 pdu->handle = adv ? adv->handle : instance;
1844 pdu->operation = LE_SET_ADV_DATA_OP_COMPLETE;
1845 pdu->frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1846
1847 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1848 struct_size(pdu, data, len), pdu,
1849 HCI_CMD_TIMEOUT);
1850 if (err)
1851 return err;
1852
1853 /* Update data if the command succeed */
1854 if (adv) {
1855 adv->adv_data_changed = false;
1856 } else {
1857 memcpy(hdev->adv_data, pdu->data, len);
1858 hdev->adv_data_len = len;
1859 }
1860
1861 return 0;
1862 }
1863
hci_set_adv_data_sync(struct hci_dev * hdev,u8 instance)1864 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1865 {
1866 struct hci_cp_le_set_adv_data cp;
1867 u8 len;
1868
1869 memset(&cp, 0, sizeof(cp));
1870
1871 len = eir_create_adv_data(hdev, instance, cp.data, sizeof(cp.data));
1872
1873 /* There's nothing to do if the data hasn't changed */
1874 if (hdev->adv_data_len == len &&
1875 memcmp(cp.data, hdev->adv_data, len) == 0)
1876 return 0;
1877
1878 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1879 hdev->adv_data_len = len;
1880
1881 cp.length = len;
1882
1883 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1884 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1885 }
1886
hci_update_adv_data_sync(struct hci_dev * hdev,u8 instance)1887 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1888 {
1889 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1890 return 0;
1891
1892 if (ext_adv_capable(hdev))
1893 return hci_set_ext_adv_data_sync(hdev, instance);
1894
1895 return hci_set_adv_data_sync(hdev, instance);
1896 }
1897
hci_schedule_adv_instance_sync(struct hci_dev * hdev,u8 instance,bool force)1898 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1899 bool force)
1900 {
1901 struct adv_info *adv = NULL;
1902 u16 timeout;
1903
1904 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
1905 return -EPERM;
1906
1907 if (hdev->adv_instance_timeout)
1908 return -EBUSY;
1909
1910 adv = hci_find_adv_instance(hdev, instance);
1911 if (!adv)
1912 return -ENOENT;
1913
1914 /* A zero timeout means unlimited advertising. As long as there is
1915 * only one instance, duration should be ignored. We still set a timeout
1916 * in case further instances are being added later on.
1917 *
1918 * If the remaining lifetime of the instance is more than the duration
1919 * then the timeout corresponds to the duration, otherwise it will be
1920 * reduced to the remaining instance lifetime.
1921 */
1922 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1923 timeout = adv->duration;
1924 else
1925 timeout = adv->remaining_time;
1926
1927 /* The remaining time is being reduced unless the instance is being
1928 * advertised without time limit.
1929 */
1930 if (adv->timeout)
1931 adv->remaining_time = adv->remaining_time - timeout;
1932
1933 /* Only use work for scheduling instances with legacy advertising */
1934 if (!ext_adv_capable(hdev)) {
1935 hdev->adv_instance_timeout = timeout;
1936 queue_delayed_work(hdev->req_workqueue,
1937 &hdev->adv_instance_expire,
1938 secs_to_jiffies(timeout));
1939 }
1940
1941 /* If we're just re-scheduling the same instance again then do not
1942 * execute any HCI commands. This happens when a single instance is
1943 * being advertised.
1944 */
1945 if (!force && hdev->cur_adv_instance == instance &&
1946 hci_dev_test_flag(hdev, HCI_LE_ADV))
1947 return 0;
1948
1949 hdev->cur_adv_instance = instance;
1950
1951 return hci_start_adv_sync(hdev, instance);
1952 }
1953
hci_clear_adv_sets_sync(struct hci_dev * hdev,struct sock * sk)1954 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1955 {
1956 int err;
1957
1958 if (!ext_adv_capable(hdev))
1959 return 0;
1960
1961 /* Disable instance 0x00 to disable all instances */
1962 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1963 if (err)
1964 return err;
1965
1966 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1967 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1968 }
1969
hci_clear_adv_sync(struct hci_dev * hdev,struct sock * sk,bool force)1970 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1971 {
1972 struct adv_info *adv, *n;
1973 int err = 0;
1974
1975 if (ext_adv_capable(hdev))
1976 /* Remove all existing sets */
1977 err = hci_clear_adv_sets_sync(hdev, sk);
1978 if (ext_adv_capable(hdev))
1979 return err;
1980
1981 /* This is safe as long as there is no command send while the lock is
1982 * held.
1983 */
1984 hci_dev_lock(hdev);
1985
1986 /* Cleanup non-ext instances */
1987 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1988 u8 instance = adv->instance;
1989 int err;
1990
1991 if (!(force || adv->timeout))
1992 continue;
1993
1994 err = hci_remove_adv_instance(hdev, instance);
1995 if (!err)
1996 mgmt_advertising_removed(sk, hdev, instance);
1997 }
1998
1999 hci_dev_unlock(hdev);
2000
2001 return 0;
2002 }
2003
hci_remove_adv_sync(struct hci_dev * hdev,u8 instance,struct sock * sk)2004 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
2005 struct sock *sk)
2006 {
2007 int err = 0;
2008
2009 /* If we use extended advertising, instance has to be removed first. */
2010 if (ext_adv_capable(hdev))
2011 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk);
2012 if (ext_adv_capable(hdev))
2013 return err;
2014
2015 /* This is safe as long as there is no command send while the lock is
2016 * held.
2017 */
2018 hci_dev_lock(hdev);
2019
2020 err = hci_remove_adv_instance(hdev, instance);
2021 if (!err)
2022 mgmt_advertising_removed(sk, hdev, instance);
2023
2024 hci_dev_unlock(hdev);
2025
2026 return err;
2027 }
2028
2029 /* For a single instance:
2030 * - force == true: The instance will be removed even when its remaining
2031 * lifetime is not zero.
2032 * - force == false: the instance will be deactivated but kept stored unless
2033 * the remaining lifetime is zero.
2034 *
2035 * For instance == 0x00:
2036 * - force == true: All instances will be removed regardless of their timeout
2037 * setting.
2038 * - force == false: Only instances that have a timeout will be removed.
2039 */
hci_remove_advertising_sync(struct hci_dev * hdev,struct sock * sk,u8 instance,bool force)2040 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
2041 u8 instance, bool force)
2042 {
2043 struct adv_info *next = NULL;
2044 int err;
2045
2046 /* Cancel any timeout concerning the removed instance(s). */
2047 if (!instance || hdev->cur_adv_instance == instance)
2048 cancel_adv_timeout(hdev);
2049
2050 /* Get the next instance to advertise BEFORE we remove
2051 * the current one. This can be the same instance again
2052 * if there is only one instance.
2053 */
2054 if (hdev->cur_adv_instance == instance)
2055 next = hci_get_next_instance(hdev, instance);
2056
2057 if (!instance) {
2058 err = hci_clear_adv_sync(hdev, sk, force);
2059 if (err)
2060 return err;
2061 } else {
2062 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
2063
2064 if (force || (adv && adv->timeout && !adv->remaining_time)) {
2065 /* Don't advertise a removed instance. */
2066 if (next && next->instance == instance)
2067 next = NULL;
2068
2069 err = hci_remove_adv_sync(hdev, instance, sk);
2070 if (err)
2071 return err;
2072 }
2073 }
2074
2075 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
2076 return 0;
2077
2078 if (next && !ext_adv_capable(hdev))
2079 hci_schedule_adv_instance_sync(hdev, next->instance, false);
2080
2081 return 0;
2082 }
2083
hci_read_rssi_sync(struct hci_dev * hdev,__le16 handle)2084 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
2085 {
2086 struct hci_cp_read_rssi cp;
2087
2088 cp.handle = handle;
2089 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
2090 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2091 }
2092
hci_read_clock_sync(struct hci_dev * hdev,struct hci_cp_read_clock * cp)2093 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
2094 {
2095 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
2096 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
2097 }
2098
hci_read_tx_power_sync(struct hci_dev * hdev,__le16 handle,u8 type)2099 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
2100 {
2101 struct hci_cp_read_tx_power cp;
2102
2103 cp.handle = handle;
2104 cp.type = type;
2105 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
2106 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2107 }
2108
hci_disable_advertising_sync(struct hci_dev * hdev)2109 int hci_disable_advertising_sync(struct hci_dev *hdev)
2110 {
2111 u8 enable = 0x00;
2112 int err = 0;
2113
2114 /* If controller is not advertising we are done. */
2115 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
2116 return 0;
2117
2118 if (ext_adv_capable(hdev))
2119 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
2120 if (ext_adv_capable(hdev))
2121 return err;
2122
2123 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
2124 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
2125 }
2126
hci_le_set_ext_scan_enable_sync(struct hci_dev * hdev,u8 val,u8 filter_dup)2127 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
2128 u8 filter_dup)
2129 {
2130 struct hci_cp_le_set_ext_scan_enable cp;
2131
2132 memset(&cp, 0, sizeof(cp));
2133 cp.enable = val;
2134
2135 if (hci_dev_test_flag(hdev, HCI_MESH))
2136 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2137 else
2138 cp.filter_dup = filter_dup;
2139
2140 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
2141 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2142 }
2143
hci_le_set_scan_enable_sync(struct hci_dev * hdev,u8 val,u8 filter_dup)2144 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
2145 u8 filter_dup)
2146 {
2147 struct hci_cp_le_set_scan_enable cp;
2148
2149 if (use_ext_scan(hdev))
2150 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
2151
2152 memset(&cp, 0, sizeof(cp));
2153 cp.enable = val;
2154
2155 if (val && hci_dev_test_flag(hdev, HCI_MESH))
2156 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
2157 else
2158 cp.filter_dup = filter_dup;
2159
2160 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
2161 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2162 }
2163
hci_le_set_addr_resolution_enable_sync(struct hci_dev * hdev,u8 val)2164 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
2165 {
2166 if (!ll_privacy_capable(hdev))
2167 return 0;
2168
2169 /* If controller is not/already resolving we are done. */
2170 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2171 return 0;
2172
2173 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2174 sizeof(val), &val, HCI_CMD_TIMEOUT);
2175 }
2176
hci_scan_disable_sync(struct hci_dev * hdev)2177 static int hci_scan_disable_sync(struct hci_dev *hdev)
2178 {
2179 int err;
2180
2181 /* If controller is not scanning we are done. */
2182 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2183 return 0;
2184
2185 if (hdev->scanning_paused) {
2186 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2187 return 0;
2188 }
2189
2190 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
2191 if (err) {
2192 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
2193 return err;
2194 }
2195
2196 return err;
2197 }
2198
scan_use_rpa(struct hci_dev * hdev)2199 static bool scan_use_rpa(struct hci_dev *hdev)
2200 {
2201 return hci_dev_test_flag(hdev, HCI_PRIVACY);
2202 }
2203
hci_start_interleave_scan(struct hci_dev * hdev)2204 static void hci_start_interleave_scan(struct hci_dev *hdev)
2205 {
2206 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
2207 queue_delayed_work(hdev->req_workqueue,
2208 &hdev->interleave_scan, 0);
2209 }
2210
cancel_interleave_scan(struct hci_dev * hdev)2211 static void cancel_interleave_scan(struct hci_dev *hdev)
2212 {
2213 bt_dev_dbg(hdev, "cancelling interleave scan");
2214
2215 cancel_delayed_work_sync(&hdev->interleave_scan);
2216
2217 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
2218 }
2219
2220 /* Return true if interleave_scan wasn't started until exiting this function,
2221 * otherwise, return false
2222 */
hci_update_interleaved_scan_sync(struct hci_dev * hdev)2223 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
2224 {
2225 /* Do interleaved scan only if all of the following are true:
2226 * - There is at least one ADV monitor
2227 * - At least one pending LE connection or one device to be scanned for
2228 * - Monitor offloading is not supported
2229 * If so, we should alternate between allowlist scan and one without
2230 * any filters to save power.
2231 */
2232 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
2233 !(list_empty(&hdev->pend_le_conns) &&
2234 list_empty(&hdev->pend_le_reports)) &&
2235 hci_get_adv_monitor_offload_ext(hdev) ==
2236 HCI_ADV_MONITOR_EXT_NONE;
2237 bool is_interleaving = is_interleave_scanning(hdev);
2238
2239 if (use_interleaving && !is_interleaving) {
2240 hci_start_interleave_scan(hdev);
2241 bt_dev_dbg(hdev, "starting interleave scan");
2242 return true;
2243 }
2244
2245 if (!use_interleaving && is_interleaving)
2246 cancel_interleave_scan(hdev);
2247
2248 return false;
2249 }
2250
2251 /* Removes connection to resolve list if needed.*/
hci_le_del_resolve_list_sync(struct hci_dev * hdev,bdaddr_t * bdaddr,u8 bdaddr_type)2252 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
2253 bdaddr_t *bdaddr, u8 bdaddr_type)
2254 {
2255 struct hci_cp_le_del_from_resolv_list cp;
2256 struct bdaddr_list_with_irk *entry;
2257
2258 if (!ll_privacy_capable(hdev))
2259 return 0;
2260
2261 /* Check if the IRK has been programmed */
2262 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
2263 bdaddr_type);
2264 if (!entry)
2265 return 0;
2266
2267 cp.bdaddr_type = bdaddr_type;
2268 bacpy(&cp.bdaddr, bdaddr);
2269
2270 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2271 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2272 }
2273
hci_le_del_accept_list_sync(struct hci_dev * hdev,bdaddr_t * bdaddr,u8 bdaddr_type)2274 static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
2275 bdaddr_t *bdaddr, u8 bdaddr_type)
2276 {
2277 struct hci_cp_le_del_from_accept_list cp;
2278 int err;
2279
2280 /* Check if device is on accept list before removing it */
2281 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
2282 return 0;
2283
2284 cp.bdaddr_type = bdaddr_type;
2285 bacpy(&cp.bdaddr, bdaddr);
2286
2287 /* Ignore errors when removing from resolving list as that is likely
2288 * that the device was never added.
2289 */
2290 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2291
2292 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
2293 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2294 if (err) {
2295 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
2296 return err;
2297 }
2298
2299 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
2300 cp.bdaddr_type);
2301
2302 return 0;
2303 }
2304
2305 struct conn_params {
2306 bdaddr_t addr;
2307 u8 addr_type;
2308 hci_conn_flags_t flags;
2309 u8 privacy_mode;
2310 };
2311
2312 /* Adds connection to resolve list if needed.
2313 * Setting params to NULL programs local hdev->irk
2314 */
hci_le_add_resolve_list_sync(struct hci_dev * hdev,struct conn_params * params)2315 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
2316 struct conn_params *params)
2317 {
2318 struct hci_cp_le_add_to_resolv_list cp;
2319 struct smp_irk *irk;
2320 struct bdaddr_list_with_irk *entry;
2321 struct hci_conn_params *p;
2322
2323 if (!ll_privacy_capable(hdev))
2324 return 0;
2325
2326 /* Attempt to program local identity address, type and irk if params is
2327 * NULL.
2328 */
2329 if (!params) {
2330 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
2331 return 0;
2332
2333 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
2334 memcpy(cp.peer_irk, hdev->irk, 16);
2335 goto done;
2336 } else if (!(params->flags & HCI_CONN_FLAG_ADDRESS_RESOLUTION))
2337 return 0;
2338
2339 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2340 if (!irk)
2341 return 0;
2342
2343 /* Check if the IK has _not_ been programmed yet. */
2344 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
2345 ¶ms->addr,
2346 params->addr_type);
2347 if (entry)
2348 return 0;
2349
2350 cp.bdaddr_type = params->addr_type;
2351 bacpy(&cp.bdaddr, ¶ms->addr);
2352 memcpy(cp.peer_irk, irk->val, 16);
2353
2354 /* Default privacy mode is always Network */
2355 params->privacy_mode = HCI_NETWORK_PRIVACY;
2356
2357 rcu_read_lock();
2358 p = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2359 ¶ms->addr, params->addr_type);
2360 if (!p)
2361 p = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2362 ¶ms->addr, params->addr_type);
2363 if (p)
2364 WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY);
2365 rcu_read_unlock();
2366
2367 done:
2368 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
2369 memcpy(cp.local_irk, hdev->irk, 16);
2370 else
2371 memset(cp.local_irk, 0, 16);
2372
2373 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2374 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2375 }
2376
2377 /* Set Device Privacy Mode. */
hci_le_set_privacy_mode_sync(struct hci_dev * hdev,struct conn_params * params)2378 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
2379 struct conn_params *params)
2380 {
2381 struct hci_cp_le_set_privacy_mode cp;
2382 struct smp_irk *irk;
2383
2384 if (!ll_privacy_capable(hdev) ||
2385 !(params->flags & HCI_CONN_FLAG_ADDRESS_RESOLUTION))
2386 return 0;
2387
2388 /* If device privacy mode has already been set there is nothing to do */
2389 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
2390 return 0;
2391
2392 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
2393 * indicates that LL Privacy has been enabled and
2394 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
2395 */
2396 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
2397 return 0;
2398
2399 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type);
2400 if (!irk)
2401 return 0;
2402
2403 memset(&cp, 0, sizeof(cp));
2404 cp.bdaddr_type = irk->addr_type;
2405 bacpy(&cp.bdaddr, &irk->bdaddr);
2406 cp.mode = HCI_DEVICE_PRIVACY;
2407
2408 /* Note: params->privacy_mode is not updated since it is a copy */
2409
2410 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
2411 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2412 }
2413
2414 /* Adds connection to allow list if needed, if the device uses RPA (has IRK)
2415 * this attempts to program the device in the resolving list as well and
2416 * properly set the privacy mode.
2417 */
hci_le_add_accept_list_sync(struct hci_dev * hdev,struct conn_params * params,u8 * num_entries)2418 static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
2419 struct conn_params *params,
2420 u8 *num_entries)
2421 {
2422 struct hci_cp_le_add_to_accept_list cp;
2423 int err;
2424
2425 /* During suspend, only wakeable devices can be in acceptlist */
2426 if (hdev->suspended &&
2427 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) {
2428 hci_le_del_accept_list_sync(hdev, ¶ms->addr,
2429 params->addr_type);
2430 return 0;
2431 }
2432
2433 /* Select filter policy to accept all advertising */
2434 if (*num_entries >= hdev->le_accept_list_size)
2435 return -ENOSPC;
2436
2437 /* Attempt to program the device in the resolving list first to avoid
2438 * having to rollback in case it fails since the resolving list is
2439 * dynamic it can probably be smaller than the accept list.
2440 */
2441 err = hci_le_add_resolve_list_sync(hdev, params);
2442 if (err) {
2443 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
2444 return err;
2445 }
2446
2447 /* Set Privacy Mode */
2448 err = hci_le_set_privacy_mode_sync(hdev, params);
2449 if (err) {
2450 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
2451 return err;
2452 }
2453
2454 /* Check if already in accept list */
2455 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr,
2456 params->addr_type))
2457 return 0;
2458
2459 *num_entries += 1;
2460 cp.bdaddr_type = params->addr_type;
2461 bacpy(&cp.bdaddr, ¶ms->addr);
2462
2463 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
2464 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2465 if (err) {
2466 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
2467 /* Rollback the device from the resolving list */
2468 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
2469 return err;
2470 }
2471
2472 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
2473 cp.bdaddr_type);
2474
2475 return 0;
2476 }
2477
2478 /* This function disables/pause all advertising instances */
hci_pause_advertising_sync(struct hci_dev * hdev)2479 static int hci_pause_advertising_sync(struct hci_dev *hdev)
2480 {
2481 int err;
2482 int old_state;
2483
2484 /* If already been paused there is nothing to do. */
2485 if (hdev->advertising_paused)
2486 return 0;
2487
2488 bt_dev_dbg(hdev, "Pausing directed advertising");
2489
2490 /* Stop directed advertising */
2491 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
2492 if (old_state) {
2493 /* When discoverable timeout triggers, then just make sure
2494 * the limited discoverable flag is cleared. Even in the case
2495 * of a timeout triggered from general discoverable, it is
2496 * safe to unconditionally clear the flag.
2497 */
2498 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2499 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
2500 hdev->discov_timeout = 0;
2501 }
2502
2503 bt_dev_dbg(hdev, "Pausing advertising instances");
2504
2505 /* Call to disable any advertisements active on the controller.
2506 * This will succeed even if no advertisements are configured.
2507 */
2508 err = hci_disable_advertising_sync(hdev);
2509 if (err)
2510 return err;
2511
2512 /* If we are using software rotation, pause the loop */
2513 if (!ext_adv_capable(hdev))
2514 cancel_adv_timeout(hdev);
2515
2516 hdev->advertising_paused = true;
2517 hdev->advertising_old_state = old_state;
2518
2519 return 0;
2520 }
2521
2522 /* This function enables all user advertising instances */
hci_resume_advertising_sync(struct hci_dev * hdev)2523 static int hci_resume_advertising_sync(struct hci_dev *hdev)
2524 {
2525 struct adv_info *adv, *tmp;
2526 int err;
2527
2528 /* If advertising has not been paused there is nothing to do. */
2529 if (!hdev->advertising_paused)
2530 return 0;
2531
2532 /* Resume directed advertising */
2533 hdev->advertising_paused = false;
2534 if (hdev->advertising_old_state) {
2535 hci_dev_set_flag(hdev, HCI_ADVERTISING);
2536 hdev->advertising_old_state = 0;
2537 }
2538
2539 bt_dev_dbg(hdev, "Resuming advertising instances");
2540
2541 if (ext_adv_capable(hdev)) {
2542 /* Call for each tracked instance to be re-enabled */
2543 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
2544 err = hci_enable_ext_advertising_sync(hdev,
2545 adv->instance);
2546 if (!err)
2547 continue;
2548
2549 /* If the instance cannot be resumed remove it */
2550 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
2551 NULL);
2552 }
2553 } else {
2554 /* Schedule for most recent instance to be restarted and begin
2555 * the software rotation loop
2556 */
2557 err = hci_schedule_adv_instance_sync(hdev,
2558 hdev->cur_adv_instance,
2559 true);
2560 }
2561
2562 hdev->advertising_paused = false;
2563
2564 return err;
2565 }
2566
hci_pause_addr_resolution(struct hci_dev * hdev)2567 static int hci_pause_addr_resolution(struct hci_dev *hdev)
2568 {
2569 int err;
2570
2571 if (!ll_privacy_capable(hdev))
2572 return 0;
2573
2574 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
2575 return 0;
2576
2577 /* Cannot disable addr resolution if scanning is enabled or
2578 * when initiating an LE connection.
2579 */
2580 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2581 hci_lookup_le_connect(hdev)) {
2582 bt_dev_err(hdev, "Command not allowed when scan/LE connect");
2583 return -EPERM;
2584 }
2585
2586 /* Cannot disable addr resolution if advertising is enabled. */
2587 err = hci_pause_advertising_sync(hdev);
2588 if (err) {
2589 bt_dev_err(hdev, "Pause advertising failed: %d", err);
2590 return err;
2591 }
2592
2593 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2594 if (err)
2595 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
2596 err);
2597
2598 /* Return if address resolution is disabled and RPA is not used. */
2599 if (!err && scan_use_rpa(hdev))
2600 return 0;
2601
2602 hci_resume_advertising_sync(hdev);
2603 return err;
2604 }
2605
hci_read_local_oob_data_sync(struct hci_dev * hdev,bool extended,struct sock * sk)2606 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
2607 bool extended, struct sock *sk)
2608 {
2609 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
2610 HCI_OP_READ_LOCAL_OOB_DATA;
2611
2612 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
2613 }
2614
conn_params_copy(struct list_head * list,size_t * n)2615 static struct conn_params *conn_params_copy(struct list_head *list, size_t *n)
2616 {
2617 struct hci_conn_params *params;
2618 struct conn_params *p;
2619 size_t i;
2620
2621 rcu_read_lock();
2622
2623 i = 0;
2624 list_for_each_entry_rcu(params, list, action)
2625 ++i;
2626 *n = i;
2627
2628 rcu_read_unlock();
2629
2630 p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL);
2631 if (!p)
2632 return NULL;
2633
2634 rcu_read_lock();
2635
2636 i = 0;
2637 list_for_each_entry_rcu(params, list, action) {
2638 /* Racing adds are handled in next scan update */
2639 if (i >= *n)
2640 break;
2641
2642 /* No hdev->lock, but: addr, addr_type are immutable.
2643 * privacy_mode is only written by us or in
2644 * hci_cc_le_set_privacy_mode that we wait for.
2645 * We should be idempotent so MGMT updating flags
2646 * while we are processing is OK.
2647 */
2648 bacpy(&p[i].addr, ¶ms->addr);
2649 p[i].addr_type = params->addr_type;
2650 p[i].flags = READ_ONCE(params->flags);
2651 p[i].privacy_mode = READ_ONCE(params->privacy_mode);
2652 ++i;
2653 }
2654
2655 rcu_read_unlock();
2656
2657 *n = i;
2658 return p;
2659 }
2660
2661 /* Clear LE Accept List */
hci_le_clear_accept_list_sync(struct hci_dev * hdev)2662 static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
2663 {
2664 if (!(hdev->commands[26] & 0x80))
2665 return 0;
2666
2667 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
2668 HCI_CMD_TIMEOUT);
2669 }
2670
2671 /* Device must not be scanning when updating the accept list.
2672 *
2673 * Update is done using the following sequence:
2674 *
2675 * ll_privacy_capable((Disable Advertising) -> Disable Resolving List) ->
2676 * Remove Devices From Accept List ->
2677 * (has IRK && ll_privacy_capable(Remove Devices From Resolving List))->
2678 * Add Devices to Accept List ->
2679 * (has IRK && ll_privacy_capable(Remove Devices From Resolving List)) ->
2680 * ll_privacy_capable(Enable Resolving List -> (Enable Advertising)) ->
2681 * Enable Scanning
2682 *
2683 * In case of failure advertising shall be restored to its original state and
2684 * return would disable accept list since either accept or resolving list could
2685 * not be programmed.
2686 *
2687 */
hci_update_accept_list_sync(struct hci_dev * hdev)2688 static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
2689 {
2690 struct conn_params *params;
2691 struct bdaddr_list *b, *t;
2692 u8 num_entries = 0;
2693 bool pend_conn, pend_report;
2694 u8 filter_policy;
2695 size_t i, n;
2696 int err;
2697
2698 /* Pause advertising if resolving list can be used as controllers
2699 * cannot accept resolving list modifications while advertising.
2700 */
2701 if (ll_privacy_capable(hdev)) {
2702 err = hci_pause_advertising_sync(hdev);
2703 if (err) {
2704 bt_dev_err(hdev, "pause advertising failed: %d", err);
2705 return 0x00;
2706 }
2707 }
2708
2709 /* Disable address resolution while reprogramming accept list since
2710 * devices that do have an IRK will be programmed in the resolving list
2711 * when LL Privacy is enabled.
2712 */
2713 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
2714 if (err) {
2715 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
2716 goto done;
2717 }
2718
2719 /* Force address filtering if PA Sync is in progress */
2720 if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2721 struct hci_conn *conn;
2722
2723 conn = hci_conn_hash_lookup_create_pa_sync(hdev);
2724 if (conn) {
2725 struct conn_params pa;
2726
2727 memset(&pa, 0, sizeof(pa));
2728
2729 bacpy(&pa.addr, &conn->dst);
2730 pa.addr_type = conn->dst_type;
2731
2732 /* Clear first since there could be addresses left
2733 * behind.
2734 */
2735 hci_le_clear_accept_list_sync(hdev);
2736
2737 num_entries = 1;
2738 err = hci_le_add_accept_list_sync(hdev, &pa,
2739 &num_entries);
2740 goto done;
2741 }
2742 }
2743
2744 /* Go through the current accept list programmed into the
2745 * controller one by one and check if that address is connected or is
2746 * still in the list of pending connections or list of devices to
2747 * report. If not present in either list, then remove it from
2748 * the controller.
2749 */
2750 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
2751 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
2752 continue;
2753
2754 /* Pointers not dereferenced, no locks needed */
2755 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
2756 &b->bdaddr,
2757 b->bdaddr_type);
2758 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
2759 &b->bdaddr,
2760 b->bdaddr_type);
2761
2762 /* If the device is not likely to connect or report,
2763 * remove it from the acceptlist.
2764 */
2765 if (!pend_conn && !pend_report) {
2766 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
2767 b->bdaddr_type);
2768 continue;
2769 }
2770
2771 num_entries++;
2772 }
2773
2774 /* Since all no longer valid accept list entries have been
2775 * removed, walk through the list of pending connections
2776 * and ensure that any new device gets programmed into
2777 * the controller.
2778 *
2779 * If the list of the devices is larger than the list of
2780 * available accept list entries in the controller, then
2781 * just abort and return filer policy value to not use the
2782 * accept list.
2783 *
2784 * The list and params may be mutated while we wait for events,
2785 * so make a copy and iterate it.
2786 */
2787
2788 params = conn_params_copy(&hdev->pend_le_conns, &n);
2789 if (!params) {
2790 err = -ENOMEM;
2791 goto done;
2792 }
2793
2794 for (i = 0; i < n; ++i) {
2795 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2796 &num_entries);
2797 if (err) {
2798 kvfree(params);
2799 goto done;
2800 }
2801 }
2802
2803 kvfree(params);
2804
2805 /* After adding all new pending connections, walk through
2806 * the list of pending reports and also add these to the
2807 * accept list if there is still space. Abort if space runs out.
2808 */
2809
2810 params = conn_params_copy(&hdev->pend_le_reports, &n);
2811 if (!params) {
2812 err = -ENOMEM;
2813 goto done;
2814 }
2815
2816 for (i = 0; i < n; ++i) {
2817 err = hci_le_add_accept_list_sync(hdev, ¶ms[i],
2818 &num_entries);
2819 if (err) {
2820 kvfree(params);
2821 goto done;
2822 }
2823 }
2824
2825 kvfree(params);
2826
2827 /* Use the allowlist unless the following conditions are all true:
2828 * - We are not currently suspending
2829 * - There are 1 or more ADV monitors registered and it's not offloaded
2830 * - Interleaved scanning is not currently using the allowlist
2831 */
2832 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
2833 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
2834 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
2835 err = -EINVAL;
2836
2837 done:
2838 filter_policy = err ? 0x00 : 0x01;
2839
2840 /* Enable address resolution when LL Privacy is enabled. */
2841 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2842 if (err)
2843 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
2844
2845 /* Resume advertising if it was paused */
2846 if (ll_privacy_capable(hdev))
2847 hci_resume_advertising_sync(hdev);
2848
2849 /* Select filter policy to use accept list */
2850 return filter_policy;
2851 }
2852
hci_le_scan_phy_params(struct hci_cp_le_scan_phy_params * cp,u8 type,u16 interval,u16 window)2853 static void hci_le_scan_phy_params(struct hci_cp_le_scan_phy_params *cp,
2854 u8 type, u16 interval, u16 window)
2855 {
2856 cp->type = type;
2857 cp->interval = cpu_to_le16(interval);
2858 cp->window = cpu_to_le16(window);
2859 }
2860
hci_le_set_ext_scan_param_sync(struct hci_dev * hdev,u8 type,u16 interval,u16 window,u8 own_addr_type,u8 filter_policy)2861 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
2862 u16 interval, u16 window,
2863 u8 own_addr_type, u8 filter_policy)
2864 {
2865 struct hci_cp_le_set_ext_scan_params *cp;
2866 struct hci_cp_le_scan_phy_params *phy;
2867 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2868 u8 num_phy = 0x00;
2869
2870 cp = (void *)data;
2871 phy = (void *)cp->data;
2872
2873 memset(data, 0, sizeof(data));
2874
2875 cp->own_addr_type = own_addr_type;
2876 cp->filter_policy = filter_policy;
2877
2878 /* Check if PA Sync is in progress then select the PHY based on the
2879 * hci_conn.iso_qos.
2880 */
2881 if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
2882 struct hci_cp_le_add_to_accept_list *sent;
2883
2884 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
2885 if (sent) {
2886 struct hci_conn *conn;
2887
2888 conn = hci_conn_hash_lookup_ba(hdev, BIS_LINK,
2889 &sent->bdaddr);
2890 if (conn) {
2891 struct bt_iso_qos *qos = &conn->iso_qos;
2892
2893 if (qos->bcast.in.phy & BT_ISO_PHY_1M ||
2894 qos->bcast.in.phy & BT_ISO_PHY_2M) {
2895 cp->scanning_phys |= LE_SCAN_PHY_1M;
2896 hci_le_scan_phy_params(phy, type,
2897 interval,
2898 window);
2899 num_phy++;
2900 phy++;
2901 }
2902
2903 if (qos->bcast.in.phy & BT_ISO_PHY_CODED) {
2904 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2905 hci_le_scan_phy_params(phy, type,
2906 interval * 3,
2907 window * 3);
2908 num_phy++;
2909 phy++;
2910 }
2911
2912 if (num_phy)
2913 goto done;
2914 }
2915 }
2916 }
2917
2918 if (scan_1m(hdev) || scan_2m(hdev)) {
2919 cp->scanning_phys |= LE_SCAN_PHY_1M;
2920 hci_le_scan_phy_params(phy, type, interval, window);
2921 num_phy++;
2922 phy++;
2923 }
2924
2925 if (scan_coded(hdev)) {
2926 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2927 hci_le_scan_phy_params(phy, type, interval * 3, window * 3);
2928 num_phy++;
2929 phy++;
2930 }
2931
2932 done:
2933 if (!num_phy)
2934 return -EINVAL;
2935
2936 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2937 sizeof(*cp) + sizeof(*phy) * num_phy,
2938 data, HCI_CMD_TIMEOUT);
2939 }
2940
hci_le_set_scan_param_sync(struct hci_dev * hdev,u8 type,u16 interval,u16 window,u8 own_addr_type,u8 filter_policy)2941 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2942 u16 interval, u16 window,
2943 u8 own_addr_type, u8 filter_policy)
2944 {
2945 struct hci_cp_le_set_scan_param cp;
2946
2947 if (use_ext_scan(hdev))
2948 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2949 window, own_addr_type,
2950 filter_policy);
2951
2952 memset(&cp, 0, sizeof(cp));
2953 cp.type = type;
2954 cp.interval = cpu_to_le16(interval);
2955 cp.window = cpu_to_le16(window);
2956 cp.own_address_type = own_addr_type;
2957 cp.filter_policy = filter_policy;
2958
2959 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2960 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2961 }
2962
hci_start_scan_sync(struct hci_dev * hdev,u8 type,u16 interval,u16 window,u8 own_addr_type,u8 filter_policy,u8 filter_dup)2963 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
2964 u16 window, u8 own_addr_type, u8 filter_policy,
2965 u8 filter_dup)
2966 {
2967 int err;
2968
2969 if (hdev->scanning_paused) {
2970 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2971 return 0;
2972 }
2973
2974 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2975 own_addr_type, filter_policy);
2976 if (err)
2977 return err;
2978
2979 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
2980 }
2981
hci_passive_scan_sync(struct hci_dev * hdev)2982 static int hci_passive_scan_sync(struct hci_dev *hdev)
2983 {
2984 u8 own_addr_type;
2985 u8 filter_policy;
2986 u16 window, interval;
2987 u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE;
2988 int err;
2989
2990 if (hdev->scanning_paused) {
2991 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2992 return 0;
2993 }
2994
2995 err = hci_scan_disable_sync(hdev);
2996 if (err) {
2997 bt_dev_err(hdev, "disable scanning failed: %d", err);
2998 return err;
2999 }
3000
3001 /* Set require_privacy to false since no SCAN_REQ are send
3002 * during passive scanning. Not using an non-resolvable address
3003 * here is important so that peer devices using direct
3004 * advertising with our address will be correctly reported
3005 * by the controller.
3006 */
3007 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
3008 &own_addr_type))
3009 return 0;
3010
3011 if (hdev->enable_advmon_interleave_scan &&
3012 hci_update_interleaved_scan_sync(hdev))
3013 return 0;
3014
3015 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
3016
3017 /* Adding or removing entries from the accept list must
3018 * happen before enabling scanning. The controller does
3019 * not allow accept list modification while scanning.
3020 */
3021 filter_policy = hci_update_accept_list_sync(hdev);
3022
3023 /* If suspended and filter_policy set to 0x00 (no acceptlist) then
3024 * passive scanning cannot be started since that would require the host
3025 * to be woken up to process the reports.
3026 */
3027 if (hdev->suspended && !filter_policy) {
3028 /* Check if accept list is empty then there is no need to scan
3029 * while suspended.
3030 */
3031 if (list_empty(&hdev->le_accept_list))
3032 return 0;
3033
3034 /* If there are devices is the accept_list that means some
3035 * devices could not be programmed which in non-suspended case
3036 * means filter_policy needs to be set to 0x00 so the host needs
3037 * to filter, but since this is treating suspended case we
3038 * can ignore device needing host to filter to allow devices in
3039 * the acceptlist to be able to wakeup the system.
3040 */
3041 filter_policy = 0x01;
3042 }
3043
3044 /* When the controller is using random resolvable addresses and
3045 * with that having LE privacy enabled, then controllers with
3046 * Extended Scanner Filter Policies support can now enable support
3047 * for handling directed advertising.
3048 *
3049 * So instead of using filter polices 0x00 (no acceptlist)
3050 * and 0x01 (acceptlist enabled) use the new filter policies
3051 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
3052 */
3053 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
3054 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
3055 filter_policy |= 0x02;
3056
3057 if (hdev->suspended) {
3058 window = hdev->le_scan_window_suspend;
3059 interval = hdev->le_scan_int_suspend;
3060 } else if (hci_is_le_conn_scanning(hdev)) {
3061 window = hdev->le_scan_window_connect;
3062 interval = hdev->le_scan_int_connect;
3063 } else if (hci_is_adv_monitoring(hdev)) {
3064 window = hdev->le_scan_window_adv_monitor;
3065 interval = hdev->le_scan_int_adv_monitor;
3066
3067 /* Disable duplicates filter when scanning for advertisement
3068 * monitor for the following reasons.
3069 *
3070 * For HW pattern filtering (ex. MSFT), Realtek and Qualcomm
3071 * controllers ignore RSSI_Sampling_Period when the duplicates
3072 * filter is enabled.
3073 *
3074 * For SW pattern filtering, when we're not doing interleaved
3075 * scanning, it is necessary to disable duplicates filter,
3076 * otherwise hosts can only receive one advertisement and it's
3077 * impossible to know if a peer is still in range.
3078 */
3079 filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
3080 } else {
3081 window = hdev->le_scan_window;
3082 interval = hdev->le_scan_interval;
3083 }
3084
3085 /* Disable all filtering for Mesh */
3086 if (hci_dev_test_flag(hdev, HCI_MESH)) {
3087 filter_policy = 0;
3088 filter_dups = LE_SCAN_FILTER_DUP_DISABLE;
3089 }
3090
3091 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
3092
3093 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
3094 own_addr_type, filter_policy, filter_dups);
3095 }
3096
3097 /* This function controls the passive scanning based on hdev->pend_le_conns
3098 * list. If there are pending LE connection we start the background scanning,
3099 * otherwise we stop it in the following sequence:
3100 *
3101 * If there are devices to scan:
3102 *
3103 * Disable Scanning -> Update Accept List ->
3104 * ll_privacy_capable((Disable Advertising) -> Disable Resolving List ->
3105 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
3106 * Enable Scanning
3107 *
3108 * Otherwise:
3109 *
3110 * Disable Scanning
3111 */
hci_update_passive_scan_sync(struct hci_dev * hdev)3112 int hci_update_passive_scan_sync(struct hci_dev *hdev)
3113 {
3114 int err;
3115
3116 if (!test_bit(HCI_UP, &hdev->flags) ||
3117 test_bit(HCI_INIT, &hdev->flags) ||
3118 hci_dev_test_flag(hdev, HCI_SETUP) ||
3119 hci_dev_test_flag(hdev, HCI_CONFIG) ||
3120 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
3121 hci_dev_test_flag(hdev, HCI_UNREGISTER))
3122 return 0;
3123
3124 /* No point in doing scanning if LE support hasn't been enabled */
3125 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3126 return 0;
3127
3128 /* If discovery is active don't interfere with it */
3129 if (hdev->discovery.state != DISCOVERY_STOPPED)
3130 return 0;
3131
3132 /* Reset RSSI and UUID filters when starting background scanning
3133 * since these filters are meant for service discovery only.
3134 *
3135 * The Start Discovery and Start Service Discovery operations
3136 * ensure to set proper values for RSSI threshold and UUID
3137 * filter list. So it is safe to just reset them here.
3138 */
3139 hci_discovery_filter_clear(hdev);
3140
3141 bt_dev_dbg(hdev, "ADV monitoring is %s",
3142 hci_is_adv_monitoring(hdev) ? "on" : "off");
3143
3144 if (!hci_dev_test_flag(hdev, HCI_MESH) &&
3145 list_empty(&hdev->pend_le_conns) &&
3146 list_empty(&hdev->pend_le_reports) &&
3147 !hci_is_adv_monitoring(hdev) &&
3148 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
3149 /* If there is no pending LE connections or devices
3150 * to be scanned for or no ADV monitors, we should stop the
3151 * background scanning.
3152 */
3153
3154 bt_dev_dbg(hdev, "stopping background scanning");
3155
3156 err = hci_scan_disable_sync(hdev);
3157 if (err)
3158 bt_dev_err(hdev, "stop background scanning failed: %d",
3159 err);
3160 } else {
3161 /* If there is at least one pending LE connection, we should
3162 * keep the background scan running.
3163 */
3164
3165 /* If controller is connecting, we should not start scanning
3166 * since some controllers are not able to scan and connect at
3167 * the same time.
3168 */
3169 if (hci_lookup_le_connect(hdev))
3170 return 0;
3171
3172 bt_dev_dbg(hdev, "start background scanning");
3173
3174 err = hci_passive_scan_sync(hdev);
3175 if (err)
3176 bt_dev_err(hdev, "start background scanning failed: %d",
3177 err);
3178 }
3179
3180 return err;
3181 }
3182
update_scan_sync(struct hci_dev * hdev,void * data)3183 static int update_scan_sync(struct hci_dev *hdev, void *data)
3184 {
3185 return hci_update_scan_sync(hdev);
3186 }
3187
hci_update_scan(struct hci_dev * hdev)3188 int hci_update_scan(struct hci_dev *hdev)
3189 {
3190 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL);
3191 }
3192
update_passive_scan_sync(struct hci_dev * hdev,void * data)3193 static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
3194 {
3195 return hci_update_passive_scan_sync(hdev);
3196 }
3197
hci_update_passive_scan(struct hci_dev * hdev)3198 int hci_update_passive_scan(struct hci_dev *hdev)
3199 {
3200 /* Only queue if it would have any effect */
3201 if (!test_bit(HCI_UP, &hdev->flags) ||
3202 test_bit(HCI_INIT, &hdev->flags) ||
3203 hci_dev_test_flag(hdev, HCI_SETUP) ||
3204 hci_dev_test_flag(hdev, HCI_CONFIG) ||
3205 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
3206 hci_dev_test_flag(hdev, HCI_UNREGISTER))
3207 return 0;
3208
3209 return hci_cmd_sync_queue_once(hdev, update_passive_scan_sync, NULL,
3210 NULL);
3211 }
3212
hci_write_sc_support_sync(struct hci_dev * hdev,u8 val)3213 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
3214 {
3215 int err;
3216
3217 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
3218 return 0;
3219
3220 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3221 sizeof(val), &val, HCI_CMD_TIMEOUT);
3222
3223 if (!err) {
3224 if (val) {
3225 hdev->features[1][0] |= LMP_HOST_SC;
3226 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
3227 } else {
3228 hdev->features[1][0] &= ~LMP_HOST_SC;
3229 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
3230 }
3231 }
3232
3233 return err;
3234 }
3235
hci_write_ssp_mode_sync(struct hci_dev * hdev,u8 mode)3236 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
3237 {
3238 int err;
3239
3240 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3241 lmp_host_ssp_capable(hdev))
3242 return 0;
3243
3244 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
3245 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
3246 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3247 }
3248
3249 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3250 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3251 if (err)
3252 return err;
3253
3254 return hci_write_sc_support_sync(hdev, 0x01);
3255 }
3256
hci_write_le_host_supported_sync(struct hci_dev * hdev,u8 le,u8 simul)3257 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
3258 {
3259 struct hci_cp_write_le_host_supported cp;
3260
3261 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
3262 !lmp_bredr_capable(hdev))
3263 return 0;
3264
3265 /* Check first if we already have the right host state
3266 * (host features set)
3267 */
3268 if (le == lmp_host_le_capable(hdev) &&
3269 simul == lmp_host_le_br_capable(hdev))
3270 return 0;
3271
3272 memset(&cp, 0, sizeof(cp));
3273
3274 cp.le = le;
3275 cp.simul = simul;
3276
3277 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3278 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3279 }
3280
hci_powered_update_adv_sync(struct hci_dev * hdev)3281 static int hci_powered_update_adv_sync(struct hci_dev *hdev)
3282 {
3283 struct adv_info *adv, *tmp;
3284 int err;
3285
3286 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
3287 return 0;
3288
3289 /* If RPA Resolution has not been enable yet it means the
3290 * resolving list is empty and we should attempt to program the
3291 * local IRK in order to support using own_addr_type
3292 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
3293 */
3294 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
3295 hci_le_add_resolve_list_sync(hdev, NULL);
3296 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
3297 }
3298
3299 /* Make sure the controller has a good default for
3300 * advertising data. This also applies to the case
3301 * where BR/EDR was toggled during the AUTO_OFF phase.
3302 */
3303 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
3304 list_empty(&hdev->adv_instances)) {
3305 if (ext_adv_capable(hdev)) {
3306 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
3307 if (!err)
3308 hci_update_scan_rsp_data_sync(hdev, 0x00);
3309 } else {
3310 err = hci_update_adv_data_sync(hdev, 0x00);
3311 if (!err)
3312 hci_update_scan_rsp_data_sync(hdev, 0x00);
3313 }
3314
3315 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
3316 hci_enable_advertising_sync(hdev);
3317 }
3318
3319 /* Call for each tracked instance to be scheduled */
3320 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
3321 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
3322
3323 return 0;
3324 }
3325
hci_write_auth_enable_sync(struct hci_dev * hdev)3326 static int hci_write_auth_enable_sync(struct hci_dev *hdev)
3327 {
3328 u8 link_sec;
3329
3330 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
3331 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
3332 return 0;
3333
3334 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
3335 sizeof(link_sec), &link_sec,
3336 HCI_CMD_TIMEOUT);
3337 }
3338
hci_write_fast_connectable_sync(struct hci_dev * hdev,bool enable)3339 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
3340 {
3341 struct hci_cp_write_page_scan_activity cp;
3342 u8 type;
3343 int err = 0;
3344
3345 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3346 return 0;
3347
3348 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3349 return 0;
3350
3351 memset(&cp, 0, sizeof(cp));
3352
3353 if (enable) {
3354 type = PAGE_SCAN_TYPE_INTERLACED;
3355
3356 /* 160 msec page scan interval */
3357 cp.interval = cpu_to_le16(0x0100);
3358 } else {
3359 type = hdev->def_page_scan_type;
3360 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
3361 }
3362
3363 cp.window = cpu_to_le16(hdev->def_page_scan_window);
3364
3365 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
3366 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
3367 err = __hci_cmd_sync_status(hdev,
3368 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
3369 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3370 if (err)
3371 return err;
3372 }
3373
3374 if (hdev->page_scan_type != type)
3375 err = __hci_cmd_sync_status(hdev,
3376 HCI_OP_WRITE_PAGE_SCAN_TYPE,
3377 sizeof(type), &type,
3378 HCI_CMD_TIMEOUT);
3379
3380 return err;
3381 }
3382
disconnected_accept_list_entries(struct hci_dev * hdev)3383 static bool disconnected_accept_list_entries(struct hci_dev *hdev)
3384 {
3385 struct bdaddr_list *b;
3386
3387 list_for_each_entry(b, &hdev->accept_list, list) {
3388 struct hci_conn *conn;
3389
3390 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
3391 if (!conn)
3392 return true;
3393
3394 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3395 return true;
3396 }
3397
3398 return false;
3399 }
3400
hci_write_scan_enable_sync(struct hci_dev * hdev,u8 val)3401 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
3402 {
3403 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
3404 sizeof(val), &val,
3405 HCI_CMD_TIMEOUT);
3406 }
3407
hci_update_scan_sync(struct hci_dev * hdev)3408 int hci_update_scan_sync(struct hci_dev *hdev)
3409 {
3410 u8 scan;
3411
3412 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3413 return 0;
3414
3415 if (!hdev_is_powered(hdev))
3416 return 0;
3417
3418 if (mgmt_powering_down(hdev))
3419 return 0;
3420
3421 if (hdev->scanning_paused)
3422 return 0;
3423
3424 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
3425 disconnected_accept_list_entries(hdev))
3426 scan = SCAN_PAGE;
3427 else
3428 scan = SCAN_DISABLED;
3429
3430 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
3431 scan |= SCAN_INQUIRY;
3432
3433 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
3434 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
3435 return 0;
3436
3437 return hci_write_scan_enable_sync(hdev, scan);
3438 }
3439
hci_update_name_sync(struct hci_dev * hdev)3440 int hci_update_name_sync(struct hci_dev *hdev)
3441 {
3442 struct hci_cp_write_local_name cp;
3443
3444 memset(&cp, 0, sizeof(cp));
3445
3446 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
3447
3448 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
3449 sizeof(cp), &cp,
3450 HCI_CMD_TIMEOUT);
3451 }
3452
3453 /* This function perform powered update HCI command sequence after the HCI init
3454 * sequence which end up resetting all states, the sequence is as follows:
3455 *
3456 * HCI_SSP_ENABLED(Enable SSP)
3457 * HCI_LE_ENABLED(Enable LE)
3458 * HCI_LE_ENABLED(ll_privacy_capable(Add local IRK to Resolving List) ->
3459 * Update adv data)
3460 * Enable Authentication
3461 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
3462 * Set Name -> Set EIR)
3463 * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address)
3464 */
hci_powered_update_sync(struct hci_dev * hdev)3465 int hci_powered_update_sync(struct hci_dev *hdev)
3466 {
3467 int err;
3468
3469 /* Register the available SMP channels (BR/EDR and LE) only when
3470 * successfully powering on the controller. This late
3471 * registration is required so that LE SMP can clearly decide if
3472 * the public address or static address is used.
3473 */
3474 smp_register(hdev);
3475
3476 err = hci_write_ssp_mode_sync(hdev, 0x01);
3477 if (err)
3478 return err;
3479
3480 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
3481 if (err)
3482 return err;
3483
3484 err = hci_powered_update_adv_sync(hdev);
3485 if (err)
3486 return err;
3487
3488 err = hci_write_auth_enable_sync(hdev);
3489 if (err)
3490 return err;
3491
3492 if (lmp_bredr_capable(hdev)) {
3493 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
3494 hci_write_fast_connectable_sync(hdev, true);
3495 else
3496 hci_write_fast_connectable_sync(hdev, false);
3497 hci_update_scan_sync(hdev);
3498 hci_update_class_sync(hdev);
3499 hci_update_name_sync(hdev);
3500 hci_update_eir_sync(hdev);
3501 }
3502
3503 /* If forcing static address is in use or there is no public
3504 * address use the static address as random address (but skip
3505 * the HCI command if the current random address is already the
3506 * static one.
3507 *
3508 * In case BR/EDR has been disabled on a dual-mode controller
3509 * and a static address has been configured, then use that
3510 * address instead of the public BR/EDR address.
3511 */
3512 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3513 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
3514 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) {
3515 if (bacmp(&hdev->static_addr, BDADDR_ANY))
3516 return hci_set_random_addr_sync(hdev,
3517 &hdev->static_addr);
3518 }
3519
3520 return 0;
3521 }
3522
3523 /**
3524 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
3525 * (BD_ADDR) for a HCI device from
3526 * a firmware node property.
3527 * @hdev: The HCI device
3528 *
3529 * Search the firmware node for 'local-bd-address'.
3530 *
3531 * All-zero BD addresses are rejected, because those could be properties
3532 * that exist in the firmware tables, but were not updated by the firmware. For
3533 * example, the DTS could define 'local-bd-address', with zero BD addresses.
3534 */
hci_dev_get_bd_addr_from_property(struct hci_dev * hdev)3535 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
3536 {
3537 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
3538 bdaddr_t ba;
3539 int ret;
3540
3541 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
3542 (u8 *)&ba, sizeof(ba));
3543 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
3544 return;
3545
3546 if (test_bit(HCI_QUIRK_BDADDR_PROPERTY_BROKEN, &hdev->quirks))
3547 baswap(&hdev->public_addr, &ba);
3548 else
3549 bacpy(&hdev->public_addr, &ba);
3550 }
3551
3552 struct hci_init_stage {
3553 int (*func)(struct hci_dev *hdev);
3554 };
3555
3556 /* Run init stage NULL terminated function table */
hci_init_stage_sync(struct hci_dev * hdev,const struct hci_init_stage * stage)3557 static int hci_init_stage_sync(struct hci_dev *hdev,
3558 const struct hci_init_stage *stage)
3559 {
3560 size_t i;
3561
3562 for (i = 0; stage[i].func; i++) {
3563 int err;
3564
3565 err = stage[i].func(hdev);
3566 if (err)
3567 return err;
3568 }
3569
3570 return 0;
3571 }
3572
3573 /* Read Local Version */
hci_read_local_version_sync(struct hci_dev * hdev)3574 static int hci_read_local_version_sync(struct hci_dev *hdev)
3575 {
3576 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
3577 0, NULL, HCI_CMD_TIMEOUT);
3578 }
3579
3580 /* Read BD Address */
hci_read_bd_addr_sync(struct hci_dev * hdev)3581 static int hci_read_bd_addr_sync(struct hci_dev *hdev)
3582 {
3583 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
3584 0, NULL, HCI_CMD_TIMEOUT);
3585 }
3586
3587 #define HCI_INIT(_func) \
3588 { \
3589 .func = _func, \
3590 }
3591
3592 static const struct hci_init_stage hci_init0[] = {
3593 /* HCI_OP_READ_LOCAL_VERSION */
3594 HCI_INIT(hci_read_local_version_sync),
3595 /* HCI_OP_READ_BD_ADDR */
3596 HCI_INIT(hci_read_bd_addr_sync),
3597 {}
3598 };
3599
hci_reset_sync(struct hci_dev * hdev)3600 int hci_reset_sync(struct hci_dev *hdev)
3601 {
3602 int err;
3603
3604 set_bit(HCI_RESET, &hdev->flags);
3605
3606 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
3607 HCI_CMD_TIMEOUT);
3608 if (err)
3609 return err;
3610
3611 return 0;
3612 }
3613
hci_init0_sync(struct hci_dev * hdev)3614 static int hci_init0_sync(struct hci_dev *hdev)
3615 {
3616 int err;
3617
3618 bt_dev_dbg(hdev, "");
3619
3620 /* Reset */
3621 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3622 err = hci_reset_sync(hdev);
3623 if (err)
3624 return err;
3625 }
3626
3627 return hci_init_stage_sync(hdev, hci_init0);
3628 }
3629
hci_unconf_init_sync(struct hci_dev * hdev)3630 static int hci_unconf_init_sync(struct hci_dev *hdev)
3631 {
3632 int err;
3633
3634 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3635 return 0;
3636
3637 err = hci_init0_sync(hdev);
3638 if (err < 0)
3639 return err;
3640
3641 if (hci_dev_test_flag(hdev, HCI_SETUP))
3642 hci_debugfs_create_basic(hdev);
3643
3644 return 0;
3645 }
3646
3647 /* Read Local Supported Features. */
hci_read_local_features_sync(struct hci_dev * hdev)3648 static int hci_read_local_features_sync(struct hci_dev *hdev)
3649 {
3650 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
3651 0, NULL, HCI_CMD_TIMEOUT);
3652 }
3653
3654 /* BR Controller init stage 1 command sequence */
3655 static const struct hci_init_stage br_init1[] = {
3656 /* HCI_OP_READ_LOCAL_FEATURES */
3657 HCI_INIT(hci_read_local_features_sync),
3658 /* HCI_OP_READ_LOCAL_VERSION */
3659 HCI_INIT(hci_read_local_version_sync),
3660 /* HCI_OP_READ_BD_ADDR */
3661 HCI_INIT(hci_read_bd_addr_sync),
3662 {}
3663 };
3664
3665 /* Read Local Commands */
hci_read_local_cmds_sync(struct hci_dev * hdev)3666 static int hci_read_local_cmds_sync(struct hci_dev *hdev)
3667 {
3668 /* All Bluetooth 1.2 and later controllers should support the
3669 * HCI command for reading the local supported commands.
3670 *
3671 * Unfortunately some controllers indicate Bluetooth 1.2 support,
3672 * but do not have support for this command. If that is the case,
3673 * the driver can quirk the behavior and skip reading the local
3674 * supported commands.
3675 */
3676 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
3677 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
3678 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
3679 0, NULL, HCI_CMD_TIMEOUT);
3680
3681 return 0;
3682 }
3683
hci_init1_sync(struct hci_dev * hdev)3684 static int hci_init1_sync(struct hci_dev *hdev)
3685 {
3686 int err;
3687
3688 bt_dev_dbg(hdev, "");
3689
3690 /* Reset */
3691 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
3692 err = hci_reset_sync(hdev);
3693 if (err)
3694 return err;
3695 }
3696
3697 return hci_init_stage_sync(hdev, br_init1);
3698 }
3699
3700 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
hci_read_buffer_size_sync(struct hci_dev * hdev)3701 static int hci_read_buffer_size_sync(struct hci_dev *hdev)
3702 {
3703 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
3704 0, NULL, HCI_CMD_TIMEOUT);
3705 }
3706
3707 /* Read Class of Device */
hci_read_dev_class_sync(struct hci_dev * hdev)3708 static int hci_read_dev_class_sync(struct hci_dev *hdev)
3709 {
3710 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
3711 0, NULL, HCI_CMD_TIMEOUT);
3712 }
3713
3714 /* Read Local Name */
hci_read_local_name_sync(struct hci_dev * hdev)3715 static int hci_read_local_name_sync(struct hci_dev *hdev)
3716 {
3717 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
3718 0, NULL, HCI_CMD_TIMEOUT);
3719 }
3720
3721 /* Read Voice Setting */
hci_read_voice_setting_sync(struct hci_dev * hdev)3722 static int hci_read_voice_setting_sync(struct hci_dev *hdev)
3723 {
3724 if (!read_voice_setting_capable(hdev))
3725 return 0;
3726
3727 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
3728 0, NULL, HCI_CMD_TIMEOUT);
3729 }
3730
3731 /* Read Number of Supported IAC */
hci_read_num_supported_iac_sync(struct hci_dev * hdev)3732 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
3733 {
3734 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
3735 0, NULL, HCI_CMD_TIMEOUT);
3736 }
3737
3738 /* Read Current IAC LAP */
hci_read_current_iac_lap_sync(struct hci_dev * hdev)3739 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
3740 {
3741 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
3742 0, NULL, HCI_CMD_TIMEOUT);
3743 }
3744
hci_set_event_filter_sync(struct hci_dev * hdev,u8 flt_type,u8 cond_type,bdaddr_t * bdaddr,u8 auto_accept)3745 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
3746 u8 cond_type, bdaddr_t *bdaddr,
3747 u8 auto_accept)
3748 {
3749 struct hci_cp_set_event_filter cp;
3750
3751 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
3752 return 0;
3753
3754 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3755 return 0;
3756
3757 memset(&cp, 0, sizeof(cp));
3758 cp.flt_type = flt_type;
3759
3760 if (flt_type != HCI_FLT_CLEAR_ALL) {
3761 cp.cond_type = cond_type;
3762 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
3763 cp.addr_conn_flt.auto_accept = auto_accept;
3764 }
3765
3766 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
3767 flt_type == HCI_FLT_CLEAR_ALL ?
3768 sizeof(cp.flt_type) : sizeof(cp), &cp,
3769 HCI_CMD_TIMEOUT);
3770 }
3771
hci_clear_event_filter_sync(struct hci_dev * hdev)3772 static int hci_clear_event_filter_sync(struct hci_dev *hdev)
3773 {
3774 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
3775 return 0;
3776
3777 /* In theory the state machine should not reach here unless
3778 * a hci_set_event_filter_sync() call succeeds, but we do
3779 * the check both for parity and as a future reminder.
3780 */
3781 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
3782 return 0;
3783
3784 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
3785 BDADDR_ANY, 0x00);
3786 }
3787
3788 /* Connection accept timeout ~20 secs */
hci_write_ca_timeout_sync(struct hci_dev * hdev)3789 static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
3790 {
3791 __le16 param = cpu_to_le16(0x7d00);
3792
3793 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
3794 sizeof(param), ¶m, HCI_CMD_TIMEOUT);
3795 }
3796
3797 /* Enable SCO flow control if supported */
hci_write_sync_flowctl_sync(struct hci_dev * hdev)3798 static int hci_write_sync_flowctl_sync(struct hci_dev *hdev)
3799 {
3800 struct hci_cp_write_sync_flowctl cp;
3801 int err;
3802
3803 /* Check if the controller supports SCO and HCI_OP_WRITE_SYNC_FLOWCTL */
3804 if (!lmp_sco_capable(hdev) || !(hdev->commands[10] & BIT(4)) ||
3805 !test_bit(HCI_QUIRK_SYNC_FLOWCTL_SUPPORTED, &hdev->quirks))
3806 return 0;
3807
3808 memset(&cp, 0, sizeof(cp));
3809 cp.enable = 0x01;
3810
3811 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SYNC_FLOWCTL,
3812 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3813 if (!err)
3814 hci_dev_set_flag(hdev, HCI_SCO_FLOWCTL);
3815
3816 return err;
3817 }
3818
3819 /* BR Controller init stage 2 command sequence */
3820 static const struct hci_init_stage br_init2[] = {
3821 /* HCI_OP_READ_BUFFER_SIZE */
3822 HCI_INIT(hci_read_buffer_size_sync),
3823 /* HCI_OP_READ_CLASS_OF_DEV */
3824 HCI_INIT(hci_read_dev_class_sync),
3825 /* HCI_OP_READ_LOCAL_NAME */
3826 HCI_INIT(hci_read_local_name_sync),
3827 /* HCI_OP_READ_VOICE_SETTING */
3828 HCI_INIT(hci_read_voice_setting_sync),
3829 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
3830 HCI_INIT(hci_read_num_supported_iac_sync),
3831 /* HCI_OP_READ_CURRENT_IAC_LAP */
3832 HCI_INIT(hci_read_current_iac_lap_sync),
3833 /* HCI_OP_SET_EVENT_FLT */
3834 HCI_INIT(hci_clear_event_filter_sync),
3835 /* HCI_OP_WRITE_CA_TIMEOUT */
3836 HCI_INIT(hci_write_ca_timeout_sync),
3837 /* HCI_OP_WRITE_SYNC_FLOWCTL */
3838 HCI_INIT(hci_write_sync_flowctl_sync),
3839 {}
3840 };
3841
hci_write_ssp_mode_1_sync(struct hci_dev * hdev)3842 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
3843 {
3844 u8 mode = 0x01;
3845
3846 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3847 return 0;
3848
3849 /* When SSP is available, then the host features page
3850 * should also be available as well. However some
3851 * controllers list the max_page as 0 as long as SSP
3852 * has not been enabled. To achieve proper debugging
3853 * output, force the minimum max_page to 1 at least.
3854 */
3855 hdev->max_page = 0x01;
3856
3857 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
3858 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3859 }
3860
hci_write_eir_sync(struct hci_dev * hdev)3861 static int hci_write_eir_sync(struct hci_dev *hdev)
3862 {
3863 struct hci_cp_write_eir cp;
3864
3865 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
3866 return 0;
3867
3868 memset(hdev->eir, 0, sizeof(hdev->eir));
3869 memset(&cp, 0, sizeof(cp));
3870
3871 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
3872 HCI_CMD_TIMEOUT);
3873 }
3874
hci_write_inquiry_mode_sync(struct hci_dev * hdev)3875 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
3876 {
3877 u8 mode;
3878
3879 if (!lmp_inq_rssi_capable(hdev) &&
3880 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3881 return 0;
3882
3883 /* If Extended Inquiry Result events are supported, then
3884 * they are clearly preferred over Inquiry Result with RSSI
3885 * events.
3886 */
3887 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
3888
3889 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
3890 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
3891 }
3892
hci_read_inq_rsp_tx_power_sync(struct hci_dev * hdev)3893 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
3894 {
3895 if (!lmp_inq_tx_pwr_capable(hdev))
3896 return 0;
3897
3898 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
3899 0, NULL, HCI_CMD_TIMEOUT);
3900 }
3901
hci_read_local_ext_features_sync(struct hci_dev * hdev,u8 page)3902 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
3903 {
3904 struct hci_cp_read_local_ext_features cp;
3905
3906 if (!lmp_ext_feat_capable(hdev))
3907 return 0;
3908
3909 memset(&cp, 0, sizeof(cp));
3910 cp.page = page;
3911
3912 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
3913 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3914 }
3915
hci_read_local_ext_features_1_sync(struct hci_dev * hdev)3916 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
3917 {
3918 return hci_read_local_ext_features_sync(hdev, 0x01);
3919 }
3920
3921 /* HCI Controller init stage 2 command sequence */
3922 static const struct hci_init_stage hci_init2[] = {
3923 /* HCI_OP_READ_LOCAL_COMMANDS */
3924 HCI_INIT(hci_read_local_cmds_sync),
3925 /* HCI_OP_WRITE_SSP_MODE */
3926 HCI_INIT(hci_write_ssp_mode_1_sync),
3927 /* HCI_OP_WRITE_EIR */
3928 HCI_INIT(hci_write_eir_sync),
3929 /* HCI_OP_WRITE_INQUIRY_MODE */
3930 HCI_INIT(hci_write_inquiry_mode_sync),
3931 /* HCI_OP_READ_INQ_RSP_TX_POWER */
3932 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
3933 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3934 HCI_INIT(hci_read_local_ext_features_1_sync),
3935 /* HCI_OP_WRITE_AUTH_ENABLE */
3936 HCI_INIT(hci_write_auth_enable_sync),
3937 {}
3938 };
3939
3940 /* Read LE Buffer Size */
hci_le_read_buffer_size_sync(struct hci_dev * hdev)3941 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3942 {
3943 /* Use Read LE Buffer Size V2 if supported */
3944 if (iso_capable(hdev) && hdev->commands[41] & 0x20)
3945 return __hci_cmd_sync_status(hdev,
3946 HCI_OP_LE_READ_BUFFER_SIZE_V2,
3947 0, NULL, HCI_CMD_TIMEOUT);
3948
3949 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3950 0, NULL, HCI_CMD_TIMEOUT);
3951 }
3952
3953 /* Read LE Local Supported Features */
hci_le_read_local_features_sync(struct hci_dev * hdev)3954 static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3955 {
3956 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3957 0, NULL, HCI_CMD_TIMEOUT);
3958 }
3959
3960 /* Read LE Supported States */
hci_le_read_supported_states_sync(struct hci_dev * hdev)3961 static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3962 {
3963 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3964 0, NULL, HCI_CMD_TIMEOUT);
3965 }
3966
3967 /* LE Controller init stage 2 command sequence */
3968 static const struct hci_init_stage le_init2[] = {
3969 /* HCI_OP_LE_READ_LOCAL_FEATURES */
3970 HCI_INIT(hci_le_read_local_features_sync),
3971 /* HCI_OP_LE_READ_BUFFER_SIZE */
3972 HCI_INIT(hci_le_read_buffer_size_sync),
3973 /* HCI_OP_LE_READ_SUPPORTED_STATES */
3974 HCI_INIT(hci_le_read_supported_states_sync),
3975 {}
3976 };
3977
hci_init2_sync(struct hci_dev * hdev)3978 static int hci_init2_sync(struct hci_dev *hdev)
3979 {
3980 int err;
3981
3982 bt_dev_dbg(hdev, "");
3983
3984 err = hci_init_stage_sync(hdev, hci_init2);
3985 if (err)
3986 return err;
3987
3988 if (lmp_bredr_capable(hdev)) {
3989 err = hci_init_stage_sync(hdev, br_init2);
3990 if (err)
3991 return err;
3992 } else {
3993 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3994 }
3995
3996 if (lmp_le_capable(hdev)) {
3997 err = hci_init_stage_sync(hdev, le_init2);
3998 if (err)
3999 return err;
4000 /* LE-only controllers have LE implicitly enabled */
4001 if (!lmp_bredr_capable(hdev))
4002 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
4003 }
4004
4005 return 0;
4006 }
4007
hci_set_event_mask_sync(struct hci_dev * hdev)4008 static int hci_set_event_mask_sync(struct hci_dev *hdev)
4009 {
4010 /* The second byte is 0xff instead of 0x9f (two reserved bits
4011 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
4012 * command otherwise.
4013 */
4014 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
4015
4016 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
4017 * any event mask for pre 1.2 devices.
4018 */
4019 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
4020 return 0;
4021
4022 if (lmp_bredr_capable(hdev)) {
4023 events[4] |= 0x01; /* Flow Specification Complete */
4024
4025 /* Don't set Disconnect Complete and mode change when
4026 * suspended as that would wakeup the host when disconnecting
4027 * due to suspend.
4028 */
4029 if (hdev->suspended) {
4030 events[0] &= 0xef;
4031 events[2] &= 0xf7;
4032 }
4033 } else {
4034 /* Use a different default for LE-only devices */
4035 memset(events, 0, sizeof(events));
4036 events[1] |= 0x20; /* Command Complete */
4037 events[1] |= 0x40; /* Command Status */
4038 events[1] |= 0x80; /* Hardware Error */
4039
4040 /* If the controller supports the Disconnect command, enable
4041 * the corresponding event. In addition enable packet flow
4042 * control related events.
4043 */
4044 if (hdev->commands[0] & 0x20) {
4045 /* Don't set Disconnect Complete when suspended as that
4046 * would wakeup the host when disconnecting due to
4047 * suspend.
4048 */
4049 if (!hdev->suspended)
4050 events[0] |= 0x10; /* Disconnection Complete */
4051 events[2] |= 0x04; /* Number of Completed Packets */
4052 events[3] |= 0x02; /* Data Buffer Overflow */
4053 }
4054
4055 /* If the controller supports the Read Remote Version
4056 * Information command, enable the corresponding event.
4057 */
4058 if (hdev->commands[2] & 0x80)
4059 events[1] |= 0x08; /* Read Remote Version Information
4060 * Complete
4061 */
4062
4063 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
4064 events[0] |= 0x80; /* Encryption Change */
4065 events[5] |= 0x80; /* Encryption Key Refresh Complete */
4066 }
4067 }
4068
4069 if (lmp_inq_rssi_capable(hdev) ||
4070 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
4071 events[4] |= 0x02; /* Inquiry Result with RSSI */
4072
4073 if (lmp_ext_feat_capable(hdev))
4074 events[4] |= 0x04; /* Read Remote Extended Features Complete */
4075
4076 if (lmp_esco_capable(hdev)) {
4077 events[5] |= 0x08; /* Synchronous Connection Complete */
4078 events[5] |= 0x10; /* Synchronous Connection Changed */
4079 }
4080
4081 if (lmp_sniffsubr_capable(hdev))
4082 events[5] |= 0x20; /* Sniff Subrating */
4083
4084 if (lmp_pause_enc_capable(hdev))
4085 events[5] |= 0x80; /* Encryption Key Refresh Complete */
4086
4087 if (lmp_ext_inq_capable(hdev))
4088 events[5] |= 0x40; /* Extended Inquiry Result */
4089
4090 if (lmp_no_flush_capable(hdev))
4091 events[7] |= 0x01; /* Enhanced Flush Complete */
4092
4093 if (lmp_lsto_capable(hdev))
4094 events[6] |= 0x80; /* Link Supervision Timeout Changed */
4095
4096 if (lmp_ssp_capable(hdev)) {
4097 events[6] |= 0x01; /* IO Capability Request */
4098 events[6] |= 0x02; /* IO Capability Response */
4099 events[6] |= 0x04; /* User Confirmation Request */
4100 events[6] |= 0x08; /* User Passkey Request */
4101 events[6] |= 0x10; /* Remote OOB Data Request */
4102 events[6] |= 0x20; /* Simple Pairing Complete */
4103 events[7] |= 0x04; /* User Passkey Notification */
4104 events[7] |= 0x08; /* Keypress Notification */
4105 events[7] |= 0x10; /* Remote Host Supported
4106 * Features Notification
4107 */
4108 }
4109
4110 if (lmp_le_capable(hdev))
4111 events[7] |= 0x20; /* LE Meta-Event */
4112
4113 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
4114 sizeof(events), events, HCI_CMD_TIMEOUT);
4115 }
4116
hci_read_stored_link_key_sync(struct hci_dev * hdev)4117 static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
4118 {
4119 struct hci_cp_read_stored_link_key cp;
4120
4121 if (!(hdev->commands[6] & 0x20) ||
4122 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4123 return 0;
4124
4125 memset(&cp, 0, sizeof(cp));
4126 bacpy(&cp.bdaddr, BDADDR_ANY);
4127 cp.read_all = 0x01;
4128
4129 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
4130 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4131 }
4132
hci_setup_link_policy_sync(struct hci_dev * hdev)4133 static int hci_setup_link_policy_sync(struct hci_dev *hdev)
4134 {
4135 struct hci_cp_write_def_link_policy cp;
4136 u16 link_policy = 0;
4137
4138 if (!(hdev->commands[5] & 0x10))
4139 return 0;
4140
4141 memset(&cp, 0, sizeof(cp));
4142
4143 if (lmp_rswitch_capable(hdev))
4144 link_policy |= HCI_LP_RSWITCH;
4145 if (lmp_hold_capable(hdev))
4146 link_policy |= HCI_LP_HOLD;
4147 if (lmp_sniff_capable(hdev))
4148 link_policy |= HCI_LP_SNIFF;
4149 if (lmp_park_capable(hdev))
4150 link_policy |= HCI_LP_PARK;
4151
4152 cp.policy = cpu_to_le16(link_policy);
4153
4154 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
4155 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4156 }
4157
hci_read_page_scan_activity_sync(struct hci_dev * hdev)4158 static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
4159 {
4160 if (!(hdev->commands[8] & 0x01))
4161 return 0;
4162
4163 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
4164 0, NULL, HCI_CMD_TIMEOUT);
4165 }
4166
hci_read_def_err_data_reporting_sync(struct hci_dev * hdev)4167 static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
4168 {
4169 if (!(hdev->commands[18] & 0x04) ||
4170 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4171 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4172 return 0;
4173
4174 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
4175 0, NULL, HCI_CMD_TIMEOUT);
4176 }
4177
hci_read_page_scan_type_sync(struct hci_dev * hdev)4178 static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
4179 {
4180 /* Some older Broadcom based Bluetooth 1.2 controllers do not
4181 * support the Read Page Scan Type command. Check support for
4182 * this command in the bit mask of supported commands.
4183 */
4184 if (!(hdev->commands[13] & 0x01) ||
4185 test_bit(HCI_QUIRK_BROKEN_READ_PAGE_SCAN_TYPE, &hdev->quirks))
4186 return 0;
4187
4188 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
4189 0, NULL, HCI_CMD_TIMEOUT);
4190 }
4191
4192 /* Read features beyond page 1 if available */
hci_read_local_ext_features_all_sync(struct hci_dev * hdev)4193 static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
4194 {
4195 u8 page;
4196 int err;
4197
4198 if (!lmp_ext_feat_capable(hdev))
4199 return 0;
4200
4201 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
4202 page++) {
4203 err = hci_read_local_ext_features_sync(hdev, page);
4204 if (err)
4205 return err;
4206 }
4207
4208 return 0;
4209 }
4210
4211 /* HCI Controller init stage 3 command sequence */
4212 static const struct hci_init_stage hci_init3[] = {
4213 /* HCI_OP_SET_EVENT_MASK */
4214 HCI_INIT(hci_set_event_mask_sync),
4215 /* HCI_OP_READ_STORED_LINK_KEY */
4216 HCI_INIT(hci_read_stored_link_key_sync),
4217 /* HCI_OP_WRITE_DEF_LINK_POLICY */
4218 HCI_INIT(hci_setup_link_policy_sync),
4219 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
4220 HCI_INIT(hci_read_page_scan_activity_sync),
4221 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
4222 HCI_INIT(hci_read_def_err_data_reporting_sync),
4223 /* HCI_OP_READ_PAGE_SCAN_TYPE */
4224 HCI_INIT(hci_read_page_scan_type_sync),
4225 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
4226 HCI_INIT(hci_read_local_ext_features_all_sync),
4227 {}
4228 };
4229
hci_le_set_event_mask_sync(struct hci_dev * hdev)4230 static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
4231 {
4232 u8 events[8];
4233
4234 if (!lmp_le_capable(hdev))
4235 return 0;
4236
4237 memset(events, 0, sizeof(events));
4238
4239 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
4240 events[0] |= 0x10; /* LE Long Term Key Request */
4241
4242 /* If controller supports the Connection Parameters Request
4243 * Link Layer Procedure, enable the corresponding event.
4244 */
4245 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
4246 /* LE Remote Connection Parameter Request */
4247 events[0] |= 0x20;
4248
4249 /* If the controller supports the Data Length Extension
4250 * feature, enable the corresponding event.
4251 */
4252 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
4253 events[0] |= 0x40; /* LE Data Length Change */
4254
4255 /* If the controller supports LL Privacy feature or LE Extended Adv,
4256 * enable the corresponding event.
4257 */
4258 if (use_enhanced_conn_complete(hdev))
4259 events[1] |= 0x02; /* LE Enhanced Connection Complete */
4260
4261 /* Mark Device Privacy if Privacy Mode is supported */
4262 if (privacy_mode_capable(hdev))
4263 hdev->conn_flags |= HCI_CONN_FLAG_DEVICE_PRIVACY;
4264
4265 /* Mark Address Resolution if LL Privacy is supported */
4266 if (ll_privacy_capable(hdev))
4267 hdev->conn_flags |= HCI_CONN_FLAG_ADDRESS_RESOLUTION;
4268
4269 /* If the controller supports Extended Scanner Filter
4270 * Policies, enable the corresponding event.
4271 */
4272 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
4273 events[1] |= 0x04; /* LE Direct Advertising Report */
4274
4275 /* If the controller supports Channel Selection Algorithm #2
4276 * feature, enable the corresponding event.
4277 */
4278 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
4279 events[2] |= 0x08; /* LE Channel Selection Algorithm */
4280
4281 /* If the controller supports the LE Set Scan Enable command,
4282 * enable the corresponding advertising report event.
4283 */
4284 if (hdev->commands[26] & 0x08)
4285 events[0] |= 0x02; /* LE Advertising Report */
4286
4287 /* If the controller supports the LE Create Connection
4288 * command, enable the corresponding event.
4289 */
4290 if (hdev->commands[26] & 0x10)
4291 events[0] |= 0x01; /* LE Connection Complete */
4292
4293 /* If the controller supports the LE Connection Update
4294 * command, enable the corresponding event.
4295 */
4296 if (hdev->commands[27] & 0x04)
4297 events[0] |= 0x04; /* LE Connection Update Complete */
4298
4299 /* If the controller supports the LE Read Remote Used Features
4300 * command, enable the corresponding event.
4301 */
4302 if (hdev->commands[27] & 0x20)
4303 /* LE Read Remote Used Features Complete */
4304 events[0] |= 0x08;
4305
4306 /* If the controller supports the LE Read Local P-256
4307 * Public Key command, enable the corresponding event.
4308 */
4309 if (hdev->commands[34] & 0x02)
4310 /* LE Read Local P-256 Public Key Complete */
4311 events[0] |= 0x80;
4312
4313 /* If the controller supports the LE Generate DHKey
4314 * command, enable the corresponding event.
4315 */
4316 if (hdev->commands[34] & 0x04)
4317 events[1] |= 0x01; /* LE Generate DHKey Complete */
4318
4319 /* If the controller supports the LE Set Default PHY or
4320 * LE Set PHY commands, enable the corresponding event.
4321 */
4322 if (hdev->commands[35] & (0x20 | 0x40))
4323 events[1] |= 0x08; /* LE PHY Update Complete */
4324
4325 /* If the controller supports LE Set Extended Scan Parameters
4326 * and LE Set Extended Scan Enable commands, enable the
4327 * corresponding event.
4328 */
4329 if (use_ext_scan(hdev))
4330 events[1] |= 0x10; /* LE Extended Advertising Report */
4331
4332 /* If the controller supports the LE Extended Advertising
4333 * command, enable the corresponding event.
4334 */
4335 if (ext_adv_capable(hdev))
4336 events[2] |= 0x02; /* LE Advertising Set Terminated */
4337
4338 if (cis_capable(hdev)) {
4339 events[3] |= 0x01; /* LE CIS Established */
4340 if (cis_peripheral_capable(hdev))
4341 events[3] |= 0x02; /* LE CIS Request */
4342 }
4343
4344 if (bis_capable(hdev)) {
4345 events[1] |= 0x20; /* LE PA Report */
4346 events[1] |= 0x40; /* LE PA Sync Established */
4347 events[3] |= 0x04; /* LE Create BIG Complete */
4348 events[3] |= 0x08; /* LE Terminate BIG Complete */
4349 events[3] |= 0x10; /* LE BIG Sync Established */
4350 events[3] |= 0x20; /* LE BIG Sync Loss */
4351 events[4] |= 0x02; /* LE BIG Info Advertising Report */
4352 }
4353
4354 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
4355 sizeof(events), events, HCI_CMD_TIMEOUT);
4356 }
4357
4358 /* Read LE Advertising Channel TX Power */
hci_le_read_adv_tx_power_sync(struct hci_dev * hdev)4359 static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
4360 {
4361 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
4362 /* HCI TS spec forbids mixing of legacy and extended
4363 * advertising commands wherein READ_ADV_TX_POWER is
4364 * also included. So do not call it if extended adv
4365 * is supported otherwise controller will return
4366 * COMMAND_DISALLOWED for extended commands.
4367 */
4368 return __hci_cmd_sync_status(hdev,
4369 HCI_OP_LE_READ_ADV_TX_POWER,
4370 0, NULL, HCI_CMD_TIMEOUT);
4371 }
4372
4373 return 0;
4374 }
4375
4376 /* Read LE Min/Max Tx Power*/
hci_le_read_tx_power_sync(struct hci_dev * hdev)4377 static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
4378 {
4379 if (!(hdev->commands[38] & 0x80) ||
4380 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
4381 return 0;
4382
4383 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
4384 0, NULL, HCI_CMD_TIMEOUT);
4385 }
4386
4387 /* Read LE Accept List Size */
hci_le_read_accept_list_size_sync(struct hci_dev * hdev)4388 static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
4389 {
4390 if (!(hdev->commands[26] & 0x40))
4391 return 0;
4392
4393 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4394 0, NULL, HCI_CMD_TIMEOUT);
4395 }
4396
4397 /* Read LE Resolving List Size */
hci_le_read_resolv_list_size_sync(struct hci_dev * hdev)4398 static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
4399 {
4400 if (!(hdev->commands[34] & 0x40))
4401 return 0;
4402
4403 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
4404 0, NULL, HCI_CMD_TIMEOUT);
4405 }
4406
4407 /* Clear LE Resolving List */
hci_le_clear_resolv_list_sync(struct hci_dev * hdev)4408 static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
4409 {
4410 if (!(hdev->commands[34] & 0x20))
4411 return 0;
4412
4413 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
4414 HCI_CMD_TIMEOUT);
4415 }
4416
4417 /* Set RPA timeout */
hci_le_set_rpa_timeout_sync(struct hci_dev * hdev)4418 static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
4419 {
4420 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
4421
4422 if (!(hdev->commands[35] & 0x04) ||
4423 test_bit(HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT, &hdev->quirks))
4424 return 0;
4425
4426 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
4427 sizeof(timeout), &timeout,
4428 HCI_CMD_TIMEOUT);
4429 }
4430
4431 /* Read LE Maximum Data Length */
hci_le_read_max_data_len_sync(struct hci_dev * hdev)4432 static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
4433 {
4434 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4435 return 0;
4436
4437 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
4438 HCI_CMD_TIMEOUT);
4439 }
4440
4441 /* Read LE Suggested Default Data Length */
hci_le_read_def_data_len_sync(struct hci_dev * hdev)4442 static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
4443 {
4444 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4445 return 0;
4446
4447 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
4448 HCI_CMD_TIMEOUT);
4449 }
4450
4451 /* Read LE Number of Supported Advertising Sets */
hci_le_read_num_support_adv_sets_sync(struct hci_dev * hdev)4452 static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
4453 {
4454 if (!ext_adv_capable(hdev))
4455 return 0;
4456
4457 return __hci_cmd_sync_status(hdev,
4458 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4459 0, NULL, HCI_CMD_TIMEOUT);
4460 }
4461
4462 /* Write LE Host Supported */
hci_set_le_support_sync(struct hci_dev * hdev)4463 static int hci_set_le_support_sync(struct hci_dev *hdev)
4464 {
4465 struct hci_cp_write_le_host_supported cp;
4466
4467 /* LE-only devices do not support explicit enablement */
4468 if (!lmp_bredr_capable(hdev))
4469 return 0;
4470
4471 memset(&cp, 0, sizeof(cp));
4472
4473 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
4474 cp.le = 0x01;
4475 cp.simul = 0x00;
4476 }
4477
4478 if (cp.le == lmp_host_le_capable(hdev))
4479 return 0;
4480
4481 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
4482 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4483 }
4484
4485 /* LE Set Host Feature */
hci_le_set_host_feature_sync(struct hci_dev * hdev)4486 static int hci_le_set_host_feature_sync(struct hci_dev *hdev)
4487 {
4488 struct hci_cp_le_set_host_feature cp;
4489
4490 if (!cis_capable(hdev))
4491 return 0;
4492
4493 memset(&cp, 0, sizeof(cp));
4494
4495 /* Connected Isochronous Channels (Host Support) */
4496 cp.bit_number = 32;
4497 cp.bit_value = 1;
4498
4499 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_HOST_FEATURE,
4500 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4501 }
4502
4503 /* LE Controller init stage 3 command sequence */
4504 static const struct hci_init_stage le_init3[] = {
4505 /* HCI_OP_LE_SET_EVENT_MASK */
4506 HCI_INIT(hci_le_set_event_mask_sync),
4507 /* HCI_OP_LE_READ_ADV_TX_POWER */
4508 HCI_INIT(hci_le_read_adv_tx_power_sync),
4509 /* HCI_OP_LE_READ_TRANSMIT_POWER */
4510 HCI_INIT(hci_le_read_tx_power_sync),
4511 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
4512 HCI_INIT(hci_le_read_accept_list_size_sync),
4513 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
4514 HCI_INIT(hci_le_clear_accept_list_sync),
4515 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
4516 HCI_INIT(hci_le_read_resolv_list_size_sync),
4517 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
4518 HCI_INIT(hci_le_clear_resolv_list_sync),
4519 /* HCI_OP_LE_SET_RPA_TIMEOUT */
4520 HCI_INIT(hci_le_set_rpa_timeout_sync),
4521 /* HCI_OP_LE_READ_MAX_DATA_LEN */
4522 HCI_INIT(hci_le_read_max_data_len_sync),
4523 /* HCI_OP_LE_READ_DEF_DATA_LEN */
4524 HCI_INIT(hci_le_read_def_data_len_sync),
4525 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
4526 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
4527 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
4528 HCI_INIT(hci_set_le_support_sync),
4529 /* HCI_OP_LE_SET_HOST_FEATURE */
4530 HCI_INIT(hci_le_set_host_feature_sync),
4531 {}
4532 };
4533
hci_init3_sync(struct hci_dev * hdev)4534 static int hci_init3_sync(struct hci_dev *hdev)
4535 {
4536 int err;
4537
4538 bt_dev_dbg(hdev, "");
4539
4540 err = hci_init_stage_sync(hdev, hci_init3);
4541 if (err)
4542 return err;
4543
4544 if (lmp_le_capable(hdev))
4545 return hci_init_stage_sync(hdev, le_init3);
4546
4547 return 0;
4548 }
4549
hci_delete_stored_link_key_sync(struct hci_dev * hdev)4550 static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
4551 {
4552 struct hci_cp_delete_stored_link_key cp;
4553
4554 /* Some Broadcom based Bluetooth controllers do not support the
4555 * Delete Stored Link Key command. They are clearly indicating its
4556 * absence in the bit mask of supported commands.
4557 *
4558 * Check the supported commands and only if the command is marked
4559 * as supported send it. If not supported assume that the controller
4560 * does not have actual support for stored link keys which makes this
4561 * command redundant anyway.
4562 *
4563 * Some controllers indicate that they support handling deleting
4564 * stored link keys, but they don't. The quirk lets a driver
4565 * just disable this command.
4566 */
4567 if (!(hdev->commands[6] & 0x80) ||
4568 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
4569 return 0;
4570
4571 memset(&cp, 0, sizeof(cp));
4572 bacpy(&cp.bdaddr, BDADDR_ANY);
4573 cp.delete_all = 0x01;
4574
4575 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
4576 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4577 }
4578
hci_set_event_mask_page_2_sync(struct hci_dev * hdev)4579 static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
4580 {
4581 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
4582 bool changed = false;
4583
4584 /* Set event mask page 2 if the HCI command for it is supported */
4585 if (!(hdev->commands[22] & 0x04))
4586 return 0;
4587
4588 /* If Connectionless Peripheral Broadcast central role is supported
4589 * enable all necessary events for it.
4590 */
4591 if (lmp_cpb_central_capable(hdev)) {
4592 events[1] |= 0x40; /* Triggered Clock Capture */
4593 events[1] |= 0x80; /* Synchronization Train Complete */
4594 events[2] |= 0x08; /* Truncated Page Complete */
4595 events[2] |= 0x20; /* CPB Channel Map Change */
4596 changed = true;
4597 }
4598
4599 /* If Connectionless Peripheral Broadcast peripheral role is supported
4600 * enable all necessary events for it.
4601 */
4602 if (lmp_cpb_peripheral_capable(hdev)) {
4603 events[2] |= 0x01; /* Synchronization Train Received */
4604 events[2] |= 0x02; /* CPB Receive */
4605 events[2] |= 0x04; /* CPB Timeout */
4606 events[2] |= 0x10; /* Peripheral Page Response Timeout */
4607 changed = true;
4608 }
4609
4610 /* Enable Authenticated Payload Timeout Expired event if supported */
4611 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
4612 events[2] |= 0x80;
4613 changed = true;
4614 }
4615
4616 /* Some Broadcom based controllers indicate support for Set Event
4617 * Mask Page 2 command, but then actually do not support it. Since
4618 * the default value is all bits set to zero, the command is only
4619 * required if the event mask has to be changed. In case no change
4620 * to the event mask is needed, skip this command.
4621 */
4622 if (!changed)
4623 return 0;
4624
4625 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
4626 sizeof(events), events, HCI_CMD_TIMEOUT);
4627 }
4628
4629 /* Read local codec list if the HCI command is supported */
hci_read_local_codecs_sync(struct hci_dev * hdev)4630 static int hci_read_local_codecs_sync(struct hci_dev *hdev)
4631 {
4632 if (hdev->commands[45] & 0x04)
4633 hci_read_supported_codecs_v2(hdev);
4634 else if (hdev->commands[29] & 0x20)
4635 hci_read_supported_codecs(hdev);
4636
4637 return 0;
4638 }
4639
4640 /* Read local pairing options if the HCI command is supported */
hci_read_local_pairing_opts_sync(struct hci_dev * hdev)4641 static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
4642 {
4643 if (!(hdev->commands[41] & 0x08))
4644 return 0;
4645
4646 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
4647 0, NULL, HCI_CMD_TIMEOUT);
4648 }
4649
4650 /* Get MWS transport configuration if the HCI command is supported */
hci_get_mws_transport_config_sync(struct hci_dev * hdev)4651 static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
4652 {
4653 if (!mws_transport_config_capable(hdev))
4654 return 0;
4655
4656 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
4657 0, NULL, HCI_CMD_TIMEOUT);
4658 }
4659
4660 /* Check for Synchronization Train support */
hci_read_sync_train_params_sync(struct hci_dev * hdev)4661 static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
4662 {
4663 if (!lmp_sync_train_capable(hdev))
4664 return 0;
4665
4666 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
4667 0, NULL, HCI_CMD_TIMEOUT);
4668 }
4669
4670 /* Enable Secure Connections if supported and configured */
hci_write_sc_support_1_sync(struct hci_dev * hdev)4671 static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
4672 {
4673 u8 support = 0x01;
4674
4675 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
4676 !bredr_sc_enabled(hdev))
4677 return 0;
4678
4679 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
4680 sizeof(support), &support,
4681 HCI_CMD_TIMEOUT);
4682 }
4683
4684 /* Set erroneous data reporting if supported to the wideband speech
4685 * setting value
4686 */
hci_set_err_data_report_sync(struct hci_dev * hdev)4687 static int hci_set_err_data_report_sync(struct hci_dev *hdev)
4688 {
4689 struct hci_cp_write_def_err_data_reporting cp;
4690 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
4691
4692 if (!(hdev->commands[18] & 0x08) ||
4693 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING) ||
4694 test_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks))
4695 return 0;
4696
4697 if (enabled == hdev->err_data_reporting)
4698 return 0;
4699
4700 memset(&cp, 0, sizeof(cp));
4701 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
4702 ERR_DATA_REPORTING_DISABLED;
4703
4704 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4705 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4706 }
4707
4708 static const struct hci_init_stage hci_init4[] = {
4709 /* HCI_OP_DELETE_STORED_LINK_KEY */
4710 HCI_INIT(hci_delete_stored_link_key_sync),
4711 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
4712 HCI_INIT(hci_set_event_mask_page_2_sync),
4713 /* HCI_OP_READ_LOCAL_CODECS */
4714 HCI_INIT(hci_read_local_codecs_sync),
4715 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
4716 HCI_INIT(hci_read_local_pairing_opts_sync),
4717 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
4718 HCI_INIT(hci_get_mws_transport_config_sync),
4719 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
4720 HCI_INIT(hci_read_sync_train_params_sync),
4721 /* HCI_OP_WRITE_SC_SUPPORT */
4722 HCI_INIT(hci_write_sc_support_1_sync),
4723 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
4724 HCI_INIT(hci_set_err_data_report_sync),
4725 {}
4726 };
4727
4728 /* Set Suggested Default Data Length to maximum if supported */
hci_le_set_write_def_data_len_sync(struct hci_dev * hdev)4729 static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
4730 {
4731 struct hci_cp_le_write_def_data_len cp;
4732
4733 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
4734 return 0;
4735
4736 memset(&cp, 0, sizeof(cp));
4737 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
4738 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
4739
4740 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
4741 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4742 }
4743
4744 /* Set Default PHY parameters if command is supported, enables all supported
4745 * PHYs according to the LE Features bits.
4746 */
hci_le_set_default_phy_sync(struct hci_dev * hdev)4747 static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
4748 {
4749 struct hci_cp_le_set_default_phy cp;
4750
4751 if (!(hdev->commands[35] & 0x20)) {
4752 /* If the command is not supported it means only 1M PHY is
4753 * supported.
4754 */
4755 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
4756 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
4757 return 0;
4758 }
4759
4760 memset(&cp, 0, sizeof(cp));
4761 cp.all_phys = 0x00;
4762 cp.tx_phys = HCI_LE_SET_PHY_1M;
4763 cp.rx_phys = HCI_LE_SET_PHY_1M;
4764
4765 /* Enables 2M PHY if supported */
4766 if (le_2m_capable(hdev)) {
4767 cp.tx_phys |= HCI_LE_SET_PHY_2M;
4768 cp.rx_phys |= HCI_LE_SET_PHY_2M;
4769 }
4770
4771 /* Enables Coded PHY if supported */
4772 if (le_coded_capable(hdev)) {
4773 cp.tx_phys |= HCI_LE_SET_PHY_CODED;
4774 cp.rx_phys |= HCI_LE_SET_PHY_CODED;
4775 }
4776
4777 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4778 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4779 }
4780
4781 static const struct hci_init_stage le_init4[] = {
4782 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
4783 HCI_INIT(hci_le_set_write_def_data_len_sync),
4784 /* HCI_OP_LE_SET_DEFAULT_PHY */
4785 HCI_INIT(hci_le_set_default_phy_sync),
4786 {}
4787 };
4788
hci_init4_sync(struct hci_dev * hdev)4789 static int hci_init4_sync(struct hci_dev *hdev)
4790 {
4791 int err;
4792
4793 bt_dev_dbg(hdev, "");
4794
4795 err = hci_init_stage_sync(hdev, hci_init4);
4796 if (err)
4797 return err;
4798
4799 if (lmp_le_capable(hdev))
4800 return hci_init_stage_sync(hdev, le_init4);
4801
4802 return 0;
4803 }
4804
hci_init_sync(struct hci_dev * hdev)4805 static int hci_init_sync(struct hci_dev *hdev)
4806 {
4807 int err;
4808
4809 err = hci_init1_sync(hdev);
4810 if (err < 0)
4811 return err;
4812
4813 if (hci_dev_test_flag(hdev, HCI_SETUP))
4814 hci_debugfs_create_basic(hdev);
4815
4816 err = hci_init2_sync(hdev);
4817 if (err < 0)
4818 return err;
4819
4820 err = hci_init3_sync(hdev);
4821 if (err < 0)
4822 return err;
4823
4824 err = hci_init4_sync(hdev);
4825 if (err < 0)
4826 return err;
4827
4828 /* This function is only called when the controller is actually in
4829 * configured state. When the controller is marked as unconfigured,
4830 * this initialization procedure is not run.
4831 *
4832 * It means that it is possible that a controller runs through its
4833 * setup phase and then discovers missing settings. If that is the
4834 * case, then this function will not be called. It then will only
4835 * be called during the config phase.
4836 *
4837 * So only when in setup phase or config phase, create the debugfs
4838 * entries and register the SMP channels.
4839 */
4840 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4841 !hci_dev_test_flag(hdev, HCI_CONFIG))
4842 return 0;
4843
4844 if (hci_dev_test_and_set_flag(hdev, HCI_DEBUGFS_CREATED))
4845 return 0;
4846
4847 hci_debugfs_create_common(hdev);
4848
4849 if (lmp_bredr_capable(hdev))
4850 hci_debugfs_create_bredr(hdev);
4851
4852 if (lmp_le_capable(hdev))
4853 hci_debugfs_create_le(hdev);
4854
4855 return 0;
4856 }
4857
4858 #define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
4859
4860 static const struct {
4861 unsigned long quirk;
4862 const char *desc;
4863 } hci_broken_table[] = {
4864 HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
4865 "HCI Read Local Supported Commands not supported"),
4866 HCI_QUIRK_BROKEN(STORED_LINK_KEY,
4867 "HCI Delete Stored Link Key command is advertised, "
4868 "but not supported."),
4869 HCI_QUIRK_BROKEN(ERR_DATA_REPORTING,
4870 "HCI Read Default Erroneous Data Reporting command is "
4871 "advertised, but not supported."),
4872 HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
4873 "HCI Read Transmit Power Level command is advertised, "
4874 "but not supported."),
4875 HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
4876 "HCI Set Event Filter command not supported."),
4877 HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
4878 "HCI Enhanced Setup Synchronous Connection command is "
4879 "advertised, but not supported."),
4880 HCI_QUIRK_BROKEN(SET_RPA_TIMEOUT,
4881 "HCI LE Set Random Private Address Timeout command is "
4882 "advertised, but not supported."),
4883 HCI_QUIRK_BROKEN(EXT_CREATE_CONN,
4884 "HCI LE Extended Create Connection command is "
4885 "advertised, but not supported."),
4886 HCI_QUIRK_BROKEN(WRITE_AUTH_PAYLOAD_TIMEOUT,
4887 "HCI WRITE AUTH PAYLOAD TIMEOUT command leads "
4888 "to unexpected SMP errors when pairing "
4889 "and will not be used."),
4890 HCI_QUIRK_BROKEN(LE_CODED,
4891 "HCI LE Coded PHY feature bit is set, "
4892 "but its usage is not supported.")
4893 };
4894
4895 /* This function handles hdev setup stage:
4896 *
4897 * Calls hdev->setup
4898 * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
4899 */
hci_dev_setup_sync(struct hci_dev * hdev)4900 static int hci_dev_setup_sync(struct hci_dev *hdev)
4901 {
4902 int ret = 0;
4903 bool invalid_bdaddr;
4904 size_t i;
4905
4906 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4907 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
4908 return 0;
4909
4910 bt_dev_dbg(hdev, "");
4911
4912 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
4913
4914 if (hdev->setup)
4915 ret = hdev->setup(hdev);
4916
4917 for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
4918 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
4919 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
4920 }
4921
4922 /* The transport driver can set the quirk to mark the
4923 * BD_ADDR invalid before creating the HCI device or in
4924 * its setup callback.
4925 */
4926 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
4927 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
4928 if (!ret) {
4929 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks) &&
4930 !bacmp(&hdev->public_addr, BDADDR_ANY))
4931 hci_dev_get_bd_addr_from_property(hdev);
4932
4933 if (invalid_bdaddr && bacmp(&hdev->public_addr, BDADDR_ANY) &&
4934 hdev->set_bdaddr) {
4935 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4936 if (!ret)
4937 invalid_bdaddr = false;
4938 }
4939 }
4940
4941 /* The transport driver can set these quirks before
4942 * creating the HCI device or in its setup callback.
4943 *
4944 * For the invalid BD_ADDR quirk it is possible that
4945 * it becomes a valid address if the bootloader does
4946 * provide it (see above).
4947 *
4948 * In case any of them is set, the controller has to
4949 * start up as unconfigured.
4950 */
4951 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
4952 invalid_bdaddr)
4953 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
4954
4955 /* For an unconfigured controller it is required to
4956 * read at least the version information provided by
4957 * the Read Local Version Information command.
4958 *
4959 * If the set_bdaddr driver callback is provided, then
4960 * also the original Bluetooth public device address
4961 * will be read using the Read BD Address command.
4962 */
4963 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4964 return hci_unconf_init_sync(hdev);
4965
4966 return ret;
4967 }
4968
4969 /* This function handles hdev init stage:
4970 *
4971 * Calls hci_dev_setup_sync to perform setup stage
4972 * Calls hci_init_sync to perform HCI command init sequence
4973 */
hci_dev_init_sync(struct hci_dev * hdev)4974 static int hci_dev_init_sync(struct hci_dev *hdev)
4975 {
4976 int ret;
4977
4978 bt_dev_dbg(hdev, "");
4979
4980 atomic_set(&hdev->cmd_cnt, 1);
4981 set_bit(HCI_INIT, &hdev->flags);
4982
4983 ret = hci_dev_setup_sync(hdev);
4984
4985 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
4986 /* If public address change is configured, ensure that
4987 * the address gets programmed. If the driver does not
4988 * support changing the public address, fail the power
4989 * on procedure.
4990 */
4991 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
4992 hdev->set_bdaddr)
4993 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
4994 else
4995 ret = -EADDRNOTAVAIL;
4996 }
4997
4998 if (!ret) {
4999 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
5000 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5001 ret = hci_init_sync(hdev);
5002 if (!ret && hdev->post_init)
5003 ret = hdev->post_init(hdev);
5004 }
5005 }
5006
5007 /* If the HCI Reset command is clearing all diagnostic settings,
5008 * then they need to be reprogrammed after the init procedure
5009 * completed.
5010 */
5011 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
5012 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5013 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
5014 ret = hdev->set_diag(hdev, true);
5015
5016 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5017 msft_do_open(hdev);
5018 aosp_do_open(hdev);
5019 }
5020
5021 clear_bit(HCI_INIT, &hdev->flags);
5022
5023 return ret;
5024 }
5025
hci_dev_open_sync(struct hci_dev * hdev)5026 int hci_dev_open_sync(struct hci_dev *hdev)
5027 {
5028 int ret;
5029
5030 bt_dev_dbg(hdev, "");
5031
5032 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
5033 ret = -ENODEV;
5034 goto done;
5035 }
5036
5037 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
5038 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
5039 /* Check for rfkill but allow the HCI setup stage to
5040 * proceed (which in itself doesn't cause any RF activity).
5041 */
5042 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
5043 ret = -ERFKILL;
5044 goto done;
5045 }
5046
5047 /* Check for valid public address or a configured static
5048 * random address, but let the HCI setup proceed to
5049 * be able to determine if there is a public address
5050 * or not.
5051 *
5052 * In case of user channel usage, it is not important
5053 * if a public address or static random address is
5054 * available.
5055 */
5056 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5057 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5058 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
5059 ret = -EADDRNOTAVAIL;
5060 goto done;
5061 }
5062 }
5063
5064 if (test_bit(HCI_UP, &hdev->flags)) {
5065 ret = -EALREADY;
5066 goto done;
5067 }
5068
5069 if (hdev->open(hdev)) {
5070 ret = -EIO;
5071 goto done;
5072 }
5073
5074 hci_devcd_reset(hdev);
5075
5076 set_bit(HCI_RUNNING, &hdev->flags);
5077 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
5078
5079 ret = hci_dev_init_sync(hdev);
5080 if (!ret) {
5081 hci_dev_hold(hdev);
5082 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
5083 hci_adv_instances_set_rpa_expired(hdev, true);
5084 set_bit(HCI_UP, &hdev->flags);
5085 hci_sock_dev_event(hdev, HCI_DEV_UP);
5086 hci_leds_update_powered(hdev, true);
5087 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
5088 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
5089 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
5090 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5091 hci_dev_test_flag(hdev, HCI_MGMT)) {
5092 ret = hci_powered_update_sync(hdev);
5093 mgmt_power_on(hdev, ret);
5094 }
5095 } else {
5096 /* Init failed, cleanup */
5097 flush_work(&hdev->tx_work);
5098
5099 /* Since hci_rx_work() is possible to awake new cmd_work
5100 * it should be flushed first to avoid unexpected call of
5101 * hci_cmd_work()
5102 */
5103 flush_work(&hdev->rx_work);
5104 flush_work(&hdev->cmd_work);
5105
5106 skb_queue_purge(&hdev->cmd_q);
5107 skb_queue_purge(&hdev->rx_q);
5108
5109 if (hdev->flush)
5110 hdev->flush(hdev);
5111
5112 if (hdev->sent_cmd) {
5113 cancel_delayed_work_sync(&hdev->cmd_timer);
5114 kfree_skb(hdev->sent_cmd);
5115 hdev->sent_cmd = NULL;
5116 }
5117
5118 if (hdev->req_skb) {
5119 kfree_skb(hdev->req_skb);
5120 hdev->req_skb = NULL;
5121 }
5122
5123 clear_bit(HCI_RUNNING, &hdev->flags);
5124 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5125
5126 hdev->close(hdev);
5127 hdev->flags &= BIT(HCI_RAW);
5128 }
5129
5130 done:
5131 return ret;
5132 }
5133
5134 /* This function requires the caller holds hdev->lock */
hci_pend_le_actions_clear(struct hci_dev * hdev)5135 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
5136 {
5137 struct hci_conn_params *p;
5138
5139 list_for_each_entry(p, &hdev->le_conn_params, list) {
5140 hci_pend_le_list_del_init(p);
5141 if (p->conn) {
5142 hci_conn_drop(p->conn);
5143 hci_conn_put(p->conn);
5144 p->conn = NULL;
5145 }
5146 }
5147
5148 BT_DBG("All LE pending actions cleared");
5149 }
5150
hci_dev_shutdown(struct hci_dev * hdev)5151 static int hci_dev_shutdown(struct hci_dev *hdev)
5152 {
5153 int err = 0;
5154 /* Similar to how we first do setup and then set the exclusive access
5155 * bit for userspace, we must first unset userchannel and then clean up.
5156 * Otherwise, the kernel can't properly use the hci channel to clean up
5157 * the controller (some shutdown routines require sending additional
5158 * commands to the controller for example).
5159 */
5160 bool was_userchannel =
5161 hci_dev_test_and_clear_flag(hdev, HCI_USER_CHANNEL);
5162
5163 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
5164 test_bit(HCI_UP, &hdev->flags)) {
5165 /* Execute vendor specific shutdown routine */
5166 if (hdev->shutdown)
5167 err = hdev->shutdown(hdev);
5168 }
5169
5170 if (was_userchannel)
5171 hci_dev_set_flag(hdev, HCI_USER_CHANNEL);
5172
5173 return err;
5174 }
5175
hci_dev_close_sync(struct hci_dev * hdev)5176 int hci_dev_close_sync(struct hci_dev *hdev)
5177 {
5178 bool auto_off;
5179 int err = 0;
5180
5181 bt_dev_dbg(hdev, "");
5182
5183 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
5184 disable_delayed_work(&hdev->power_off);
5185 disable_delayed_work(&hdev->ncmd_timer);
5186 disable_delayed_work(&hdev->le_scan_disable);
5187 } else {
5188 cancel_delayed_work(&hdev->power_off);
5189 cancel_delayed_work(&hdev->ncmd_timer);
5190 cancel_delayed_work(&hdev->le_scan_disable);
5191 }
5192
5193 hci_cmd_sync_cancel_sync(hdev, ENODEV);
5194
5195 cancel_interleave_scan(hdev);
5196
5197 if (hdev->adv_instance_timeout) {
5198 cancel_delayed_work_sync(&hdev->adv_instance_expire);
5199 hdev->adv_instance_timeout = 0;
5200 }
5201
5202 err = hci_dev_shutdown(hdev);
5203
5204 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
5205 cancel_delayed_work_sync(&hdev->cmd_timer);
5206 return err;
5207 }
5208
5209 hci_leds_update_powered(hdev, false);
5210
5211 /* Flush RX and TX works */
5212 flush_work(&hdev->tx_work);
5213 flush_work(&hdev->rx_work);
5214
5215 if (hdev->discov_timeout > 0) {
5216 hdev->discov_timeout = 0;
5217 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5218 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
5219 }
5220
5221 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
5222 cancel_delayed_work(&hdev->service_cache);
5223
5224 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
5225 struct adv_info *adv_instance;
5226
5227 cancel_delayed_work_sync(&hdev->rpa_expired);
5228
5229 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
5230 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
5231 }
5232
5233 /* Avoid potential lockdep warnings from the *_flush() calls by
5234 * ensuring the workqueue is empty up front.
5235 */
5236 drain_workqueue(hdev->workqueue);
5237
5238 hci_dev_lock(hdev);
5239
5240 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
5241
5242 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
5243
5244 if (!auto_off && !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
5245 hci_dev_test_flag(hdev, HCI_MGMT))
5246 __mgmt_power_off(hdev);
5247
5248 hci_inquiry_cache_flush(hdev);
5249 hci_pend_le_actions_clear(hdev);
5250 hci_conn_hash_flush(hdev);
5251 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
5252 smp_unregister(hdev);
5253 hci_dev_unlock(hdev);
5254
5255 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
5256
5257 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
5258 aosp_do_close(hdev);
5259 msft_do_close(hdev);
5260 }
5261
5262 if (hdev->flush)
5263 hdev->flush(hdev);
5264
5265 /* Reset device */
5266 skb_queue_purge(&hdev->cmd_q);
5267 atomic_set(&hdev->cmd_cnt, 1);
5268 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
5269 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
5270 set_bit(HCI_INIT, &hdev->flags);
5271 hci_reset_sync(hdev);
5272 clear_bit(HCI_INIT, &hdev->flags);
5273 }
5274
5275 /* flush cmd work */
5276 flush_work(&hdev->cmd_work);
5277
5278 /* Drop queues */
5279 skb_queue_purge(&hdev->rx_q);
5280 skb_queue_purge(&hdev->cmd_q);
5281 skb_queue_purge(&hdev->raw_q);
5282
5283 /* Drop last sent command */
5284 if (hdev->sent_cmd) {
5285 cancel_delayed_work_sync(&hdev->cmd_timer);
5286 kfree_skb(hdev->sent_cmd);
5287 hdev->sent_cmd = NULL;
5288 }
5289
5290 /* Drop last request */
5291 if (hdev->req_skb) {
5292 kfree_skb(hdev->req_skb);
5293 hdev->req_skb = NULL;
5294 }
5295
5296 clear_bit(HCI_RUNNING, &hdev->flags);
5297 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
5298
5299 /* After this point our queues are empty and no tasks are scheduled. */
5300 hdev->close(hdev);
5301
5302 /* Clear flags */
5303 hdev->flags &= BIT(HCI_RAW);
5304 hci_dev_clear_volatile_flags(hdev);
5305
5306 memset(hdev->eir, 0, sizeof(hdev->eir));
5307 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
5308 bacpy(&hdev->random_addr, BDADDR_ANY);
5309 hci_codec_list_clear(&hdev->local_codecs);
5310
5311 hci_dev_put(hdev);
5312 return err;
5313 }
5314
5315 /* This function perform power on HCI command sequence as follows:
5316 *
5317 * If controller is already up (HCI_UP) performs hci_powered_update_sync
5318 * sequence otherwise run hci_dev_open_sync which will follow with
5319 * hci_powered_update_sync after the init sequence is completed.
5320 */
hci_power_on_sync(struct hci_dev * hdev)5321 static int hci_power_on_sync(struct hci_dev *hdev)
5322 {
5323 int err;
5324
5325 if (test_bit(HCI_UP, &hdev->flags) &&
5326 hci_dev_test_flag(hdev, HCI_MGMT) &&
5327 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
5328 cancel_delayed_work(&hdev->power_off);
5329 return hci_powered_update_sync(hdev);
5330 }
5331
5332 err = hci_dev_open_sync(hdev);
5333 if (err < 0)
5334 return err;
5335
5336 /* During the HCI setup phase, a few error conditions are
5337 * ignored and they need to be checked now. If they are still
5338 * valid, it is important to return the device back off.
5339 */
5340 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
5341 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
5342 (!bacmp(&hdev->bdaddr, BDADDR_ANY) &&
5343 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
5344 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
5345 hci_dev_close_sync(hdev);
5346 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
5347 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
5348 HCI_AUTO_OFF_TIMEOUT);
5349 }
5350
5351 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
5352 /* For unconfigured devices, set the HCI_RAW flag
5353 * so that userspace can easily identify them.
5354 */
5355 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5356 set_bit(HCI_RAW, &hdev->flags);
5357
5358 /* For fully configured devices, this will send
5359 * the Index Added event. For unconfigured devices,
5360 * it will send Unconfigued Index Added event.
5361 *
5362 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
5363 * and no event will be send.
5364 */
5365 mgmt_index_added(hdev);
5366 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
5367 /* When the controller is now configured, then it
5368 * is important to clear the HCI_RAW flag.
5369 */
5370 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
5371 clear_bit(HCI_RAW, &hdev->flags);
5372
5373 /* Powering on the controller with HCI_CONFIG set only
5374 * happens with the transition from unconfigured to
5375 * configured. This will send the Index Added event.
5376 */
5377 mgmt_index_added(hdev);
5378 }
5379
5380 return 0;
5381 }
5382
hci_remote_name_cancel_sync(struct hci_dev * hdev,bdaddr_t * addr)5383 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
5384 {
5385 struct hci_cp_remote_name_req_cancel cp;
5386
5387 memset(&cp, 0, sizeof(cp));
5388 bacpy(&cp.bdaddr, addr);
5389
5390 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
5391 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5392 }
5393
hci_stop_discovery_sync(struct hci_dev * hdev)5394 int hci_stop_discovery_sync(struct hci_dev *hdev)
5395 {
5396 struct discovery_state *d = &hdev->discovery;
5397 struct inquiry_entry *e;
5398 int err;
5399
5400 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
5401
5402 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
5403 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
5404 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
5405 0, NULL, HCI_CMD_TIMEOUT);
5406 if (err)
5407 return err;
5408 }
5409
5410 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5411 cancel_delayed_work(&hdev->le_scan_disable);
5412
5413 err = hci_scan_disable_sync(hdev);
5414 if (err)
5415 return err;
5416 }
5417
5418 } else {
5419 err = hci_scan_disable_sync(hdev);
5420 if (err)
5421 return err;
5422 }
5423
5424 /* Resume advertising if it was paused */
5425 if (ll_privacy_capable(hdev))
5426 hci_resume_advertising_sync(hdev);
5427
5428 /* No further actions needed for LE-only discovery */
5429 if (d->type == DISCOV_TYPE_LE)
5430 return 0;
5431
5432 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
5433 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
5434 NAME_PENDING);
5435 if (!e)
5436 return 0;
5437
5438 /* Ignore cancel errors since it should interfere with stopping
5439 * of the discovery.
5440 */
5441 hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
5442 }
5443
5444 return 0;
5445 }
5446
hci_disconnect_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5447 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
5448 u8 reason)
5449 {
5450 struct hci_cp_disconnect cp;
5451
5452 if (test_bit(HCI_CONN_BIG_CREATED, &conn->flags)) {
5453 /* This is a BIS connection, hci_conn_del will
5454 * do the necessary cleanup.
5455 */
5456 hci_dev_lock(hdev);
5457 hci_conn_failed(conn, reason);
5458 hci_dev_unlock(hdev);
5459
5460 return 0;
5461 }
5462
5463 memset(&cp, 0, sizeof(cp));
5464 cp.handle = cpu_to_le16(conn->handle);
5465 cp.reason = reason;
5466
5467 /* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5468 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5469 * used when suspending or powering off, where we don't want to wait
5470 * for the peer's response.
5471 */
5472 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5473 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
5474 sizeof(cp), &cp,
5475 HCI_EV_DISCONN_COMPLETE,
5476 HCI_CMD_TIMEOUT, NULL);
5477
5478 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
5479 HCI_CMD_TIMEOUT);
5480 }
5481
hci_le_connect_cancel_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5482 static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
5483 struct hci_conn *conn, u8 reason)
5484 {
5485 /* Return reason if scanning since the connection shall probably be
5486 * cleanup directly.
5487 */
5488 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
5489 return reason;
5490
5491 if (conn->role == HCI_ROLE_SLAVE ||
5492 test_and_set_bit(HCI_CONN_CANCEL, &conn->flags))
5493 return 0;
5494
5495 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
5496 0, NULL, HCI_CMD_TIMEOUT);
5497 }
5498
hci_connect_cancel_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5499 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn,
5500 u8 reason)
5501 {
5502 if (conn->type == LE_LINK)
5503 return hci_le_connect_cancel_sync(hdev, conn, reason);
5504
5505 if (conn->type == CIS_LINK) {
5506 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
5507 * page 1857:
5508 *
5509 * If this command is issued for a CIS on the Central and the
5510 * CIS is successfully terminated before being established,
5511 * then an HCI_LE_CIS_Established event shall also be sent for
5512 * this CIS with the Status Operation Cancelled by Host (0x44).
5513 */
5514 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
5515 return hci_disconnect_sync(hdev, conn, reason);
5516
5517 /* CIS with no Create CIS sent have nothing to cancel */
5518 return HCI_ERROR_LOCAL_HOST_TERM;
5519 }
5520
5521 if (conn->type == BIS_LINK) {
5522 /* There is no way to cancel a BIS without terminating the BIG
5523 * which is done later on connection cleanup.
5524 */
5525 return 0;
5526 }
5527
5528 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5529 return 0;
5530
5531 /* Wait for HCI_EV_CONN_COMPLETE, not HCI_EV_CMD_STATUS, when the
5532 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is
5533 * used when suspending or powering off, where we don't want to wait
5534 * for the peer's response.
5535 */
5536 if (reason != HCI_ERROR_REMOTE_POWER_OFF)
5537 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL,
5538 6, &conn->dst,
5539 HCI_EV_CONN_COMPLETE,
5540 HCI_CMD_TIMEOUT, NULL);
5541
5542 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
5543 6, &conn->dst, HCI_CMD_TIMEOUT);
5544 }
5545
hci_reject_sco_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5546 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
5547 u8 reason)
5548 {
5549 struct hci_cp_reject_sync_conn_req cp;
5550
5551 memset(&cp, 0, sizeof(cp));
5552 bacpy(&cp.bdaddr, &conn->dst);
5553 cp.reason = reason;
5554
5555 /* SCO rejection has its own limited set of
5556 * allowed error values (0x0D-0x0F).
5557 */
5558 if (reason < 0x0d || reason > 0x0f)
5559 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
5560
5561 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
5562 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5563 }
5564
hci_le_reject_cis_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5565 static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn,
5566 u8 reason)
5567 {
5568 struct hci_cp_le_reject_cis cp;
5569
5570 memset(&cp, 0, sizeof(cp));
5571 cp.handle = cpu_to_le16(conn->handle);
5572 cp.reason = reason;
5573
5574 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS,
5575 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5576 }
5577
hci_reject_conn_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5578 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
5579 u8 reason)
5580 {
5581 struct hci_cp_reject_conn_req cp;
5582
5583 if (conn->type == CIS_LINK)
5584 return hci_le_reject_cis_sync(hdev, conn, reason);
5585
5586 if (conn->type == BIS_LINK)
5587 return -EINVAL;
5588
5589 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
5590 return hci_reject_sco_sync(hdev, conn, reason);
5591
5592 memset(&cp, 0, sizeof(cp));
5593 bacpy(&cp.bdaddr, &conn->dst);
5594 cp.reason = reason;
5595
5596 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
5597 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5598 }
5599
hci_abort_conn_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 reason)5600 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
5601 {
5602 int err = 0;
5603 u16 handle = conn->handle;
5604 bool disconnect = false;
5605 struct hci_conn *c;
5606
5607 switch (conn->state) {
5608 case BT_CONNECTED:
5609 case BT_CONFIG:
5610 err = hci_disconnect_sync(hdev, conn, reason);
5611 break;
5612 case BT_CONNECT:
5613 err = hci_connect_cancel_sync(hdev, conn, reason);
5614 break;
5615 case BT_CONNECT2:
5616 err = hci_reject_conn_sync(hdev, conn, reason);
5617 break;
5618 case BT_OPEN:
5619 case BT_BOUND:
5620 break;
5621 default:
5622 disconnect = true;
5623 break;
5624 }
5625
5626 hci_dev_lock(hdev);
5627
5628 /* Check if the connection has been cleaned up concurrently */
5629 c = hci_conn_hash_lookup_handle(hdev, handle);
5630 if (!c || c != conn) {
5631 err = 0;
5632 goto unlock;
5633 }
5634
5635 /* Cleanup hci_conn object if it cannot be cancelled as it
5636 * likelly means the controller and host stack are out of sync
5637 * or in case of LE it was still scanning so it can be cleanup
5638 * safely.
5639 */
5640 if (disconnect) {
5641 conn->state = BT_CLOSED;
5642 hci_disconn_cfm(conn, reason);
5643 hci_conn_del(conn);
5644 } else {
5645 hci_conn_failed(conn, reason);
5646 }
5647
5648 unlock:
5649 hci_dev_unlock(hdev);
5650 return err;
5651 }
5652
hci_disconnect_all_sync(struct hci_dev * hdev,u8 reason)5653 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
5654 {
5655 struct list_head *head = &hdev->conn_hash.list;
5656 struct hci_conn *conn;
5657
5658 rcu_read_lock();
5659 while ((conn = list_first_or_null_rcu(head, struct hci_conn, list))) {
5660 /* Make sure the connection is not freed while unlocking */
5661 conn = hci_conn_get(conn);
5662 rcu_read_unlock();
5663 /* Disregard possible errors since hci_conn_del shall have been
5664 * called even in case of errors had occurred since it would
5665 * then cause hci_conn_failed to be called which calls
5666 * hci_conn_del internally.
5667 */
5668 hci_abort_conn_sync(hdev, conn, reason);
5669 hci_conn_put(conn);
5670 rcu_read_lock();
5671 }
5672 rcu_read_unlock();
5673
5674 return 0;
5675 }
5676
5677 /* This function perform power off HCI command sequence as follows:
5678 *
5679 * Clear Advertising
5680 * Stop Discovery
5681 * Disconnect all connections
5682 * hci_dev_close_sync
5683 */
hci_power_off_sync(struct hci_dev * hdev)5684 static int hci_power_off_sync(struct hci_dev *hdev)
5685 {
5686 int err;
5687
5688 /* If controller is already down there is nothing to do */
5689 if (!test_bit(HCI_UP, &hdev->flags))
5690 return 0;
5691
5692 hci_dev_set_flag(hdev, HCI_POWERING_DOWN);
5693
5694 if (test_bit(HCI_ISCAN, &hdev->flags) ||
5695 test_bit(HCI_PSCAN, &hdev->flags)) {
5696 err = hci_write_scan_enable_sync(hdev, 0x00);
5697 if (err)
5698 goto out;
5699 }
5700
5701 err = hci_clear_adv_sync(hdev, NULL, false);
5702 if (err)
5703 goto out;
5704
5705 err = hci_stop_discovery_sync(hdev);
5706 if (err)
5707 goto out;
5708
5709 /* Terminated due to Power Off */
5710 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5711 if (err)
5712 goto out;
5713
5714 err = hci_dev_close_sync(hdev);
5715
5716 out:
5717 hci_dev_clear_flag(hdev, HCI_POWERING_DOWN);
5718 return err;
5719 }
5720
hci_set_powered_sync(struct hci_dev * hdev,u8 val)5721 int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
5722 {
5723 if (val)
5724 return hci_power_on_sync(hdev);
5725
5726 return hci_power_off_sync(hdev);
5727 }
5728
hci_write_iac_sync(struct hci_dev * hdev)5729 static int hci_write_iac_sync(struct hci_dev *hdev)
5730 {
5731 struct hci_cp_write_current_iac_lap cp;
5732
5733 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
5734 return 0;
5735
5736 memset(&cp, 0, sizeof(cp));
5737
5738 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
5739 /* Limited discoverable mode */
5740 cp.num_iac = min_t(u8, hdev->num_iac, 2);
5741 cp.iac_lap[0] = 0x00; /* LIAC */
5742 cp.iac_lap[1] = 0x8b;
5743 cp.iac_lap[2] = 0x9e;
5744 cp.iac_lap[3] = 0x33; /* GIAC */
5745 cp.iac_lap[4] = 0x8b;
5746 cp.iac_lap[5] = 0x9e;
5747 } else {
5748 /* General discoverable mode */
5749 cp.num_iac = 1;
5750 cp.iac_lap[0] = 0x33; /* GIAC */
5751 cp.iac_lap[1] = 0x8b;
5752 cp.iac_lap[2] = 0x9e;
5753 }
5754
5755 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
5756 (cp.num_iac * 3) + 1, &cp,
5757 HCI_CMD_TIMEOUT);
5758 }
5759
hci_update_discoverable_sync(struct hci_dev * hdev)5760 int hci_update_discoverable_sync(struct hci_dev *hdev)
5761 {
5762 int err = 0;
5763
5764 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5765 err = hci_write_iac_sync(hdev);
5766 if (err)
5767 return err;
5768
5769 err = hci_update_scan_sync(hdev);
5770 if (err)
5771 return err;
5772
5773 err = hci_update_class_sync(hdev);
5774 if (err)
5775 return err;
5776 }
5777
5778 /* Advertising instances don't use the global discoverable setting, so
5779 * only update AD if advertising was enabled using Set Advertising.
5780 */
5781 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
5782 err = hci_update_adv_data_sync(hdev, 0x00);
5783 if (err)
5784 return err;
5785
5786 /* Discoverable mode affects the local advertising
5787 * address in limited privacy mode.
5788 */
5789 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
5790 if (ext_adv_capable(hdev))
5791 err = hci_start_ext_adv_sync(hdev, 0x00);
5792 else
5793 err = hci_enable_advertising_sync(hdev);
5794 }
5795 }
5796
5797 return err;
5798 }
5799
update_discoverable_sync(struct hci_dev * hdev,void * data)5800 static int update_discoverable_sync(struct hci_dev *hdev, void *data)
5801 {
5802 return hci_update_discoverable_sync(hdev);
5803 }
5804
hci_update_discoverable(struct hci_dev * hdev)5805 int hci_update_discoverable(struct hci_dev *hdev)
5806 {
5807 /* Only queue if it would have any effect */
5808 if (hdev_is_powered(hdev) &&
5809 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5810 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
5811 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
5812 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
5813 NULL);
5814
5815 return 0;
5816 }
5817
hci_update_connectable_sync(struct hci_dev * hdev)5818 int hci_update_connectable_sync(struct hci_dev *hdev)
5819 {
5820 int err;
5821
5822 err = hci_update_scan_sync(hdev);
5823 if (err)
5824 return err;
5825
5826 /* If BR/EDR is not enabled and we disable advertising as a
5827 * by-product of disabling connectable, we need to update the
5828 * advertising flags.
5829 */
5830 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
5831 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
5832
5833 /* Update the advertising parameters if necessary */
5834 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5835 !list_empty(&hdev->adv_instances)) {
5836 if (ext_adv_capable(hdev))
5837 err = hci_start_ext_adv_sync(hdev,
5838 hdev->cur_adv_instance);
5839 else
5840 err = hci_enable_advertising_sync(hdev);
5841
5842 if (err)
5843 return err;
5844 }
5845
5846 return hci_update_passive_scan_sync(hdev);
5847 }
5848
hci_inquiry_sync(struct hci_dev * hdev,u8 length,u8 num_rsp)5849 int hci_inquiry_sync(struct hci_dev *hdev, u8 length, u8 num_rsp)
5850 {
5851 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
5852 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
5853 struct hci_cp_inquiry cp;
5854
5855 bt_dev_dbg(hdev, "");
5856
5857 if (test_bit(HCI_INQUIRY, &hdev->flags))
5858 return 0;
5859
5860 hci_dev_lock(hdev);
5861 hci_inquiry_cache_flush(hdev);
5862 hci_dev_unlock(hdev);
5863
5864 memset(&cp, 0, sizeof(cp));
5865
5866 if (hdev->discovery.limited)
5867 memcpy(&cp.lap, liac, sizeof(cp.lap));
5868 else
5869 memcpy(&cp.lap, giac, sizeof(cp.lap));
5870
5871 cp.length = length;
5872 cp.num_rsp = num_rsp;
5873
5874 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
5875 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5876 }
5877
hci_active_scan_sync(struct hci_dev * hdev,uint16_t interval)5878 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
5879 {
5880 u8 own_addr_type;
5881 /* Accept list is not used for discovery */
5882 u8 filter_policy = 0x00;
5883 /* Default is to enable duplicates filter */
5884 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
5885 int err;
5886
5887 bt_dev_dbg(hdev, "");
5888
5889 /* If controller is scanning, it means the passive scanning is
5890 * running. Thus, we should temporarily stop it in order to set the
5891 * discovery scanning parameters.
5892 */
5893 err = hci_scan_disable_sync(hdev);
5894 if (err) {
5895 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
5896 return err;
5897 }
5898
5899 cancel_interleave_scan(hdev);
5900
5901 /* Pause address resolution for active scan and stop advertising if
5902 * privacy is enabled.
5903 */
5904 err = hci_pause_addr_resolution(hdev);
5905 if (err)
5906 goto failed;
5907
5908 /* All active scans will be done with either a resolvable private
5909 * address (when privacy feature has been enabled) or non-resolvable
5910 * private address.
5911 */
5912 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
5913 &own_addr_type);
5914 if (err < 0)
5915 own_addr_type = ADDR_LE_DEV_PUBLIC;
5916
5917 if (hci_is_adv_monitoring(hdev) ||
5918 (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
5919 hdev->discovery.result_filtering)) {
5920 /* Duplicate filter should be disabled when some advertisement
5921 * monitor is activated, otherwise AdvMon can only receive one
5922 * advertisement for one peer(*) during active scanning, and
5923 * might report loss to these peers.
5924 *
5925 * If controller does strict duplicate filtering and the
5926 * discovery requires result filtering disables controller based
5927 * filtering since that can cause reports that would match the
5928 * host filter to not be reported.
5929 */
5930 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
5931 }
5932
5933 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
5934 hdev->le_scan_window_discovery,
5935 own_addr_type, filter_policy, filter_dup);
5936 if (!err)
5937 return err;
5938
5939 failed:
5940 /* Resume advertising if it was paused */
5941 if (ll_privacy_capable(hdev))
5942 hci_resume_advertising_sync(hdev);
5943
5944 /* Resume passive scanning */
5945 hci_update_passive_scan_sync(hdev);
5946 return err;
5947 }
5948
hci_start_interleaved_discovery_sync(struct hci_dev * hdev)5949 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
5950 {
5951 int err;
5952
5953 bt_dev_dbg(hdev, "");
5954
5955 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
5956 if (err)
5957 return err;
5958
5959 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN, 0);
5960 }
5961
hci_start_discovery_sync(struct hci_dev * hdev)5962 int hci_start_discovery_sync(struct hci_dev *hdev)
5963 {
5964 unsigned long timeout;
5965 int err;
5966
5967 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
5968
5969 switch (hdev->discovery.type) {
5970 case DISCOV_TYPE_BREDR:
5971 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN, 0);
5972 case DISCOV_TYPE_INTERLEAVED:
5973 /* When running simultaneous discovery, the LE scanning time
5974 * should occupy the whole discovery time sine BR/EDR inquiry
5975 * and LE scanning are scheduled by the controller.
5976 *
5977 * For interleaving discovery in comparison, BR/EDR inquiry
5978 * and LE scanning are done sequentially with separate
5979 * timeouts.
5980 */
5981 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
5982 &hdev->quirks)) {
5983 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5984 /* During simultaneous discovery, we double LE scan
5985 * interval. We must leave some time for the controller
5986 * to do BR/EDR inquiry.
5987 */
5988 err = hci_start_interleaved_discovery_sync(hdev);
5989 break;
5990 }
5991
5992 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
5993 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5994 break;
5995 case DISCOV_TYPE_LE:
5996 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
5997 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
5998 break;
5999 default:
6000 return -EINVAL;
6001 }
6002
6003 if (err)
6004 return err;
6005
6006 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
6007
6008 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
6009 timeout);
6010 return 0;
6011 }
6012
hci_suspend_monitor_sync(struct hci_dev * hdev)6013 static void hci_suspend_monitor_sync(struct hci_dev *hdev)
6014 {
6015 switch (hci_get_adv_monitor_offload_ext(hdev)) {
6016 case HCI_ADV_MONITOR_EXT_MSFT:
6017 msft_suspend_sync(hdev);
6018 break;
6019 default:
6020 return;
6021 }
6022 }
6023
6024 /* This function disables discovery and mark it as paused */
hci_pause_discovery_sync(struct hci_dev * hdev)6025 static int hci_pause_discovery_sync(struct hci_dev *hdev)
6026 {
6027 int old_state = hdev->discovery.state;
6028 int err;
6029
6030 /* If discovery already stopped/stopping/paused there nothing to do */
6031 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
6032 hdev->discovery_paused)
6033 return 0;
6034
6035 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
6036 err = hci_stop_discovery_sync(hdev);
6037 if (err)
6038 return err;
6039
6040 hdev->discovery_paused = true;
6041 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
6042
6043 return 0;
6044 }
6045
hci_update_event_filter_sync(struct hci_dev * hdev)6046 static int hci_update_event_filter_sync(struct hci_dev *hdev)
6047 {
6048 struct bdaddr_list_with_flags *b;
6049 u8 scan = SCAN_DISABLED;
6050 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
6051 int err;
6052
6053 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
6054 return 0;
6055
6056 /* Some fake CSR controllers lock up after setting this type of
6057 * filter, so avoid sending the request altogether.
6058 */
6059 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
6060 return 0;
6061
6062 /* Always clear event filter when starting */
6063 hci_clear_event_filter_sync(hdev);
6064
6065 list_for_each_entry(b, &hdev->accept_list, list) {
6066 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
6067 continue;
6068
6069 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
6070
6071 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
6072 HCI_CONN_SETUP_ALLOW_BDADDR,
6073 &b->bdaddr,
6074 HCI_CONN_SETUP_AUTO_ON);
6075 if (err)
6076 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
6077 &b->bdaddr);
6078 else
6079 scan = SCAN_PAGE;
6080 }
6081
6082 if (scan && !scanning)
6083 hci_write_scan_enable_sync(hdev, scan);
6084 else if (!scan && scanning)
6085 hci_write_scan_enable_sync(hdev, scan);
6086
6087 return 0;
6088 }
6089
6090 /* This function disables scan (BR and LE) and mark it as paused */
hci_pause_scan_sync(struct hci_dev * hdev)6091 static int hci_pause_scan_sync(struct hci_dev *hdev)
6092 {
6093 if (hdev->scanning_paused)
6094 return 0;
6095
6096 /* Disable page scan if enabled */
6097 if (test_bit(HCI_PSCAN, &hdev->flags))
6098 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
6099
6100 hci_scan_disable_sync(hdev);
6101
6102 hdev->scanning_paused = true;
6103
6104 return 0;
6105 }
6106
6107 /* This function performs the HCI suspend procedures in the follow order:
6108 *
6109 * Pause discovery (active scanning/inquiry)
6110 * Pause Directed Advertising/Advertising
6111 * Pause Scanning (passive scanning in case discovery was not active)
6112 * Disconnect all connections
6113 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
6114 * otherwise:
6115 * Update event mask (only set events that are allowed to wake up the host)
6116 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
6117 * Update passive scanning (lower duty cycle)
6118 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
6119 */
hci_suspend_sync(struct hci_dev * hdev)6120 int hci_suspend_sync(struct hci_dev *hdev)
6121 {
6122 int err;
6123
6124 /* If marked as suspended there nothing to do */
6125 if (hdev->suspended)
6126 return 0;
6127
6128 /* Mark device as suspended */
6129 hdev->suspended = true;
6130
6131 /* Pause discovery if not already stopped */
6132 hci_pause_discovery_sync(hdev);
6133
6134 /* Pause other advertisements */
6135 hci_pause_advertising_sync(hdev);
6136
6137 /* Suspend monitor filters */
6138 hci_suspend_monitor_sync(hdev);
6139
6140 /* Prevent disconnects from causing scanning to be re-enabled */
6141 hci_pause_scan_sync(hdev);
6142
6143 if (hci_conn_count(hdev)) {
6144 /* Soft disconnect everything (power off) */
6145 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
6146 if (err) {
6147 /* Set state to BT_RUNNING so resume doesn't notify */
6148 hdev->suspend_state = BT_RUNNING;
6149 hci_resume_sync(hdev);
6150 return err;
6151 }
6152
6153 /* Update event mask so only the allowed event can wakeup the
6154 * host.
6155 */
6156 hci_set_event_mask_sync(hdev);
6157 }
6158
6159 /* Only configure accept list if disconnect succeeded and wake
6160 * isn't being prevented.
6161 */
6162 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
6163 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
6164 return 0;
6165 }
6166
6167 /* Unpause to take care of updating scanning params */
6168 hdev->scanning_paused = false;
6169
6170 /* Enable event filter for paired devices */
6171 hci_update_event_filter_sync(hdev);
6172
6173 /* Update LE passive scan if enabled */
6174 hci_update_passive_scan_sync(hdev);
6175
6176 /* Pause scan changes again. */
6177 hdev->scanning_paused = true;
6178
6179 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
6180
6181 return 0;
6182 }
6183
6184 /* This function resumes discovery */
hci_resume_discovery_sync(struct hci_dev * hdev)6185 static int hci_resume_discovery_sync(struct hci_dev *hdev)
6186 {
6187 int err;
6188
6189 /* If discovery not paused there nothing to do */
6190 if (!hdev->discovery_paused)
6191 return 0;
6192
6193 hdev->discovery_paused = false;
6194
6195 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6196
6197 err = hci_start_discovery_sync(hdev);
6198
6199 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
6200 DISCOVERY_FINDING);
6201
6202 return err;
6203 }
6204
hci_resume_monitor_sync(struct hci_dev * hdev)6205 static void hci_resume_monitor_sync(struct hci_dev *hdev)
6206 {
6207 switch (hci_get_adv_monitor_offload_ext(hdev)) {
6208 case HCI_ADV_MONITOR_EXT_MSFT:
6209 msft_resume_sync(hdev);
6210 break;
6211 default:
6212 return;
6213 }
6214 }
6215
6216 /* This function resume scan and reset paused flag */
hci_resume_scan_sync(struct hci_dev * hdev)6217 static int hci_resume_scan_sync(struct hci_dev *hdev)
6218 {
6219 if (!hdev->scanning_paused)
6220 return 0;
6221
6222 hdev->scanning_paused = false;
6223
6224 hci_update_scan_sync(hdev);
6225
6226 /* Reset passive scanning to normal */
6227 hci_update_passive_scan_sync(hdev);
6228
6229 return 0;
6230 }
6231
6232 /* This function performs the HCI suspend procedures in the follow order:
6233 *
6234 * Restore event mask
6235 * Clear event filter
6236 * Update passive scanning (normal duty cycle)
6237 * Resume Directed Advertising/Advertising
6238 * Resume discovery (active scanning/inquiry)
6239 */
hci_resume_sync(struct hci_dev * hdev)6240 int hci_resume_sync(struct hci_dev *hdev)
6241 {
6242 /* If not marked as suspended there nothing to do */
6243 if (!hdev->suspended)
6244 return 0;
6245
6246 hdev->suspended = false;
6247
6248 /* Restore event mask */
6249 hci_set_event_mask_sync(hdev);
6250
6251 /* Clear any event filters and restore scan state */
6252 hci_clear_event_filter_sync(hdev);
6253
6254 /* Resume scanning */
6255 hci_resume_scan_sync(hdev);
6256
6257 /* Resume monitor filters */
6258 hci_resume_monitor_sync(hdev);
6259
6260 /* Resume other advertisements */
6261 hci_resume_advertising_sync(hdev);
6262
6263 /* Resume discovery */
6264 hci_resume_discovery_sync(hdev);
6265
6266 return 0;
6267 }
6268
conn_use_rpa(struct hci_conn * conn)6269 static bool conn_use_rpa(struct hci_conn *conn)
6270 {
6271 struct hci_dev *hdev = conn->hdev;
6272
6273 return hci_dev_test_flag(hdev, HCI_PRIVACY);
6274 }
6275
hci_le_ext_directed_advertising_sync(struct hci_dev * hdev,struct hci_conn * conn)6276 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
6277 struct hci_conn *conn)
6278 {
6279 struct hci_cp_le_set_ext_adv_params cp;
6280 int err;
6281 bdaddr_t random_addr;
6282 u8 own_addr_type;
6283
6284 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6285 &own_addr_type);
6286 if (err)
6287 return err;
6288
6289 /* Set require_privacy to false so that the remote device has a
6290 * chance of identifying us.
6291 */
6292 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
6293 &own_addr_type, &random_addr);
6294 if (err)
6295 return err;
6296
6297 memset(&cp, 0, sizeof(cp));
6298
6299 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
6300 cp.channel_map = hdev->le_adv_channel_map;
6301 cp.tx_power = HCI_TX_POWER_INVALID;
6302 cp.primary_phy = HCI_ADV_PHY_1M;
6303 cp.secondary_phy = HCI_ADV_PHY_1M;
6304 cp.handle = 0x00; /* Use instance 0 for directed adv */
6305 cp.own_addr_type = own_addr_type;
6306 cp.peer_addr_type = conn->dst_type;
6307 bacpy(&cp.peer_addr, &conn->dst);
6308
6309 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
6310 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
6311 * does not supports advertising data when the advertising set already
6312 * contains some, the controller shall return erroc code 'Invalid
6313 * HCI Command Parameters(0x12).
6314 * So it is required to remove adv set for handle 0x00. since we use
6315 * instance 0 for directed adv.
6316 */
6317 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
6318 if (err)
6319 return err;
6320
6321 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
6322 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6323 if (err)
6324 return err;
6325
6326 /* Check if random address need to be updated */
6327 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
6328 bacmp(&random_addr, BDADDR_ANY) &&
6329 bacmp(&random_addr, &hdev->random_addr)) {
6330 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
6331 &random_addr);
6332 if (err)
6333 return err;
6334 }
6335
6336 return hci_enable_ext_advertising_sync(hdev, 0x00);
6337 }
6338
hci_le_directed_advertising_sync(struct hci_dev * hdev,struct hci_conn * conn)6339 static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
6340 struct hci_conn *conn)
6341 {
6342 struct hci_cp_le_set_adv_param cp;
6343 u8 status;
6344 u8 own_addr_type;
6345 u8 enable;
6346
6347 if (ext_adv_capable(hdev))
6348 return hci_le_ext_directed_advertising_sync(hdev, conn);
6349
6350 /* Clear the HCI_LE_ADV bit temporarily so that the
6351 * hci_update_random_address knows that it's safe to go ahead
6352 * and write a new random address. The flag will be set back on
6353 * as soon as the SET_ADV_ENABLE HCI command completes.
6354 */
6355 hci_dev_clear_flag(hdev, HCI_LE_ADV);
6356
6357 /* Set require_privacy to false so that the remote device has a
6358 * chance of identifying us.
6359 */
6360 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6361 &own_addr_type);
6362 if (status)
6363 return status;
6364
6365 memset(&cp, 0, sizeof(cp));
6366
6367 /* Some controllers might reject command if intervals are not
6368 * within range for undirected advertising.
6369 * BCM20702A0 is known to be affected by this.
6370 */
6371 cp.min_interval = cpu_to_le16(0x0020);
6372 cp.max_interval = cpu_to_le16(0x0020);
6373
6374 cp.type = LE_ADV_DIRECT_IND;
6375 cp.own_address_type = own_addr_type;
6376 cp.direct_addr_type = conn->dst_type;
6377 bacpy(&cp.direct_addr, &conn->dst);
6378 cp.channel_map = hdev->le_adv_channel_map;
6379
6380 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
6381 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6382 if (status)
6383 return status;
6384
6385 enable = 0x01;
6386
6387 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
6388 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
6389 }
6390
set_ext_conn_params(struct hci_conn * conn,struct hci_cp_le_ext_conn_param * p)6391 static void set_ext_conn_params(struct hci_conn *conn,
6392 struct hci_cp_le_ext_conn_param *p)
6393 {
6394 struct hci_dev *hdev = conn->hdev;
6395
6396 memset(p, 0, sizeof(*p));
6397
6398 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6399 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6400 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6401 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6402 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
6403 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6404 p->min_ce_len = cpu_to_le16(0x0000);
6405 p->max_ce_len = cpu_to_le16(0x0000);
6406 }
6407
hci_le_ext_create_conn_sync(struct hci_dev * hdev,struct hci_conn * conn,u8 own_addr_type)6408 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
6409 struct hci_conn *conn, u8 own_addr_type)
6410 {
6411 struct hci_cp_le_ext_create_conn *cp;
6412 struct hci_cp_le_ext_conn_param *p;
6413 u8 data[sizeof(*cp) + sizeof(*p) * 3];
6414 u32 plen;
6415
6416 cp = (void *)data;
6417 p = (void *)cp->data;
6418
6419 memset(cp, 0, sizeof(*cp));
6420
6421 bacpy(&cp->peer_addr, &conn->dst);
6422 cp->peer_addr_type = conn->dst_type;
6423 cp->own_addr_type = own_addr_type;
6424
6425 plen = sizeof(*cp);
6426
6427 if (scan_1m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_1M ||
6428 conn->le_adv_sec_phy == HCI_ADV_PHY_1M)) {
6429 cp->phys |= LE_SCAN_PHY_1M;
6430 set_ext_conn_params(conn, p);
6431
6432 p++;
6433 plen += sizeof(*p);
6434 }
6435
6436 if (scan_2m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_2M ||
6437 conn->le_adv_sec_phy == HCI_ADV_PHY_2M)) {
6438 cp->phys |= LE_SCAN_PHY_2M;
6439 set_ext_conn_params(conn, p);
6440
6441 p++;
6442 plen += sizeof(*p);
6443 }
6444
6445 if (scan_coded(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_CODED ||
6446 conn->le_adv_sec_phy == HCI_ADV_PHY_CODED)) {
6447 cp->phys |= LE_SCAN_PHY_CODED;
6448 set_ext_conn_params(conn, p);
6449
6450 plen += sizeof(*p);
6451 }
6452
6453 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
6454 plen, data,
6455 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
6456 conn->conn_timeout, NULL);
6457 }
6458
hci_le_create_conn_sync(struct hci_dev * hdev,void * data)6459 static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
6460 {
6461 struct hci_cp_le_create_conn cp;
6462 struct hci_conn_params *params;
6463 u8 own_addr_type;
6464 int err;
6465 struct hci_conn *conn = data;
6466
6467 if (!hci_conn_valid(hdev, conn))
6468 return -ECANCELED;
6469
6470 bt_dev_dbg(hdev, "conn %p", conn);
6471
6472 clear_bit(HCI_CONN_SCANNING, &conn->flags);
6473 conn->state = BT_CONNECT;
6474
6475 /* If requested to connect as peripheral use directed advertising */
6476 if (conn->role == HCI_ROLE_SLAVE) {
6477 /* If we're active scanning and simultaneous roles is not
6478 * enabled simply reject the attempt.
6479 */
6480 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6481 hdev->le_scan_type == LE_SCAN_ACTIVE &&
6482 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
6483 hci_conn_del(conn);
6484 return -EBUSY;
6485 }
6486
6487 /* Pause advertising while doing directed advertising. */
6488 hci_pause_advertising_sync(hdev);
6489
6490 err = hci_le_directed_advertising_sync(hdev, conn);
6491 goto done;
6492 }
6493
6494 /* Disable advertising if simultaneous roles is not in use. */
6495 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
6496 hci_pause_advertising_sync(hdev);
6497
6498 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
6499 if (params) {
6500 conn->le_conn_min_interval = params->conn_min_interval;
6501 conn->le_conn_max_interval = params->conn_max_interval;
6502 conn->le_conn_latency = params->conn_latency;
6503 conn->le_supv_timeout = params->supervision_timeout;
6504 } else {
6505 conn->le_conn_min_interval = hdev->le_conn_min_interval;
6506 conn->le_conn_max_interval = hdev->le_conn_max_interval;
6507 conn->le_conn_latency = hdev->le_conn_latency;
6508 conn->le_supv_timeout = hdev->le_supv_timeout;
6509 }
6510
6511 /* If controller is scanning, we stop it since some controllers are
6512 * not able to scan and connect at the same time. Also set the
6513 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
6514 * handler for scan disabling knows to set the correct discovery
6515 * state.
6516 */
6517 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6518 hci_scan_disable_sync(hdev);
6519 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6520 }
6521
6522 /* Update random address, but set require_privacy to false so
6523 * that we never connect with an non-resolvable address.
6524 */
6525 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
6526 &own_addr_type);
6527 if (err)
6528 goto done;
6529 /* Send command LE Extended Create Connection if supported */
6530 if (use_ext_conn(hdev)) {
6531 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
6532 goto done;
6533 }
6534
6535 memset(&cp, 0, sizeof(cp));
6536
6537 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
6538 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
6539
6540 bacpy(&cp.peer_addr, &conn->dst);
6541 cp.peer_addr_type = conn->dst_type;
6542 cp.own_address_type = own_addr_type;
6543 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
6544 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
6545 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
6546 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
6547 cp.min_ce_len = cpu_to_le16(0x0000);
6548 cp.max_ce_len = cpu_to_le16(0x0000);
6549
6550 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
6551 *
6552 * If this event is unmasked and the HCI_LE_Connection_Complete event
6553 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
6554 * sent when a new connection has been created.
6555 */
6556 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
6557 sizeof(cp), &cp,
6558 use_enhanced_conn_complete(hdev) ?
6559 HCI_EV_LE_ENHANCED_CONN_COMPLETE :
6560 HCI_EV_LE_CONN_COMPLETE,
6561 conn->conn_timeout, NULL);
6562
6563 done:
6564 if (err == -ETIMEDOUT)
6565 hci_le_connect_cancel_sync(hdev, conn, 0x00);
6566
6567 /* Re-enable advertising after the connection attempt is finished. */
6568 hci_resume_advertising_sync(hdev);
6569 return err;
6570 }
6571
hci_le_create_cis_sync(struct hci_dev * hdev)6572 int hci_le_create_cis_sync(struct hci_dev *hdev)
6573 {
6574 DEFINE_FLEX(struct hci_cp_le_create_cis, cmd, cis, num_cis, 0x1f);
6575 size_t aux_num_cis = 0;
6576 struct hci_conn *conn;
6577 u8 cig = BT_ISO_QOS_CIG_UNSET;
6578
6579 /* The spec allows only one pending LE Create CIS command at a time. If
6580 * the command is pending now, don't do anything. We check for pending
6581 * connections after each CIS Established event.
6582 *
6583 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6584 * page 2566:
6585 *
6586 * If the Host issues this command before all the
6587 * HCI_LE_CIS_Established events from the previous use of the
6588 * command have been generated, the Controller shall return the
6589 * error code Command Disallowed (0x0C).
6590 *
6591 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
6592 * page 2567:
6593 *
6594 * When the Controller receives the HCI_LE_Create_CIS command, the
6595 * Controller sends the HCI_Command_Status event to the Host. An
6596 * HCI_LE_CIS_Established event will be generated for each CIS when it
6597 * is established or if it is disconnected or considered lost before
6598 * being established; until all the events are generated, the command
6599 * remains pending.
6600 */
6601
6602 hci_dev_lock(hdev);
6603
6604 rcu_read_lock();
6605
6606 /* Wait until previous Create CIS has completed */
6607 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6608 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
6609 goto done;
6610 }
6611
6612 /* Find CIG with all CIS ready */
6613 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6614 struct hci_conn *link;
6615
6616 if (hci_conn_check_create_cis(conn))
6617 continue;
6618
6619 cig = conn->iso_qos.ucast.cig;
6620
6621 list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) {
6622 if (hci_conn_check_create_cis(link) > 0 &&
6623 link->iso_qos.ucast.cig == cig &&
6624 link->state != BT_CONNECTED) {
6625 cig = BT_ISO_QOS_CIG_UNSET;
6626 break;
6627 }
6628 }
6629
6630 if (cig != BT_ISO_QOS_CIG_UNSET)
6631 break;
6632 }
6633
6634 if (cig == BT_ISO_QOS_CIG_UNSET)
6635 goto done;
6636
6637 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
6638 struct hci_cis *cis = &cmd->cis[aux_num_cis];
6639
6640 if (hci_conn_check_create_cis(conn) ||
6641 conn->iso_qos.ucast.cig != cig)
6642 continue;
6643
6644 set_bit(HCI_CONN_CREATE_CIS, &conn->flags);
6645 cis->acl_handle = cpu_to_le16(conn->parent->handle);
6646 cis->cis_handle = cpu_to_le16(conn->handle);
6647 aux_num_cis++;
6648
6649 if (aux_num_cis >= cmd->num_cis)
6650 break;
6651 }
6652 cmd->num_cis = aux_num_cis;
6653
6654 done:
6655 rcu_read_unlock();
6656
6657 hci_dev_unlock(hdev);
6658
6659 if (!aux_num_cis)
6660 return 0;
6661
6662 /* Wait for HCI_LE_CIS_Established */
6663 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS,
6664 struct_size(cmd, cis, cmd->num_cis),
6665 cmd, HCI_EVT_LE_CIS_ESTABLISHED,
6666 conn->conn_timeout, NULL);
6667 }
6668
hci_le_remove_cig_sync(struct hci_dev * hdev,u8 handle)6669 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle)
6670 {
6671 struct hci_cp_le_remove_cig cp;
6672
6673 memset(&cp, 0, sizeof(cp));
6674 cp.cig_id = handle;
6675
6676 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp),
6677 &cp, HCI_CMD_TIMEOUT);
6678 }
6679
hci_le_big_terminate_sync(struct hci_dev * hdev,u8 handle)6680 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle)
6681 {
6682 struct hci_cp_le_big_term_sync cp;
6683
6684 memset(&cp, 0, sizeof(cp));
6685 cp.handle = handle;
6686
6687 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC,
6688 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6689 }
6690
hci_le_pa_terminate_sync(struct hci_dev * hdev,u16 handle)6691 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
6692 {
6693 struct hci_cp_le_pa_term_sync cp;
6694
6695 memset(&cp, 0, sizeof(cp));
6696 cp.handle = cpu_to_le16(handle);
6697
6698 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
6699 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6700 }
6701
hci_get_random_address(struct hci_dev * hdev,bool require_privacy,bool use_rpa,struct adv_info * adv_instance,u8 * own_addr_type,bdaddr_t * rand_addr)6702 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
6703 bool use_rpa, struct adv_info *adv_instance,
6704 u8 *own_addr_type, bdaddr_t *rand_addr)
6705 {
6706 int err;
6707
6708 bacpy(rand_addr, BDADDR_ANY);
6709
6710 /* If privacy is enabled use a resolvable private address. If
6711 * current RPA has expired then generate a new one.
6712 */
6713 if (use_rpa) {
6714 /* If Controller supports LL Privacy use own address type is
6715 * 0x03
6716 */
6717 if (ll_privacy_capable(hdev))
6718 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
6719 else
6720 *own_addr_type = ADDR_LE_DEV_RANDOM;
6721
6722 if (adv_instance) {
6723 if (adv_rpa_valid(adv_instance))
6724 return 0;
6725 } else {
6726 if (rpa_valid(hdev))
6727 return 0;
6728 }
6729
6730 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6731 if (err < 0) {
6732 bt_dev_err(hdev, "failed to generate new RPA");
6733 return err;
6734 }
6735
6736 bacpy(rand_addr, &hdev->rpa);
6737
6738 return 0;
6739 }
6740
6741 /* In case of required privacy without resolvable private address,
6742 * use an non-resolvable private address. This is useful for
6743 * non-connectable advertising.
6744 */
6745 if (require_privacy) {
6746 bdaddr_t nrpa;
6747
6748 while (true) {
6749 /* The non-resolvable private address is generated
6750 * from random six bytes with the two most significant
6751 * bits cleared.
6752 */
6753 get_random_bytes(&nrpa, 6);
6754 nrpa.b[5] &= 0x3f;
6755
6756 /* The non-resolvable private address shall not be
6757 * equal to the public address.
6758 */
6759 if (bacmp(&hdev->bdaddr, &nrpa))
6760 break;
6761 }
6762
6763 *own_addr_type = ADDR_LE_DEV_RANDOM;
6764 bacpy(rand_addr, &nrpa);
6765
6766 return 0;
6767 }
6768
6769 /* No privacy so use a public address. */
6770 *own_addr_type = ADDR_LE_DEV_PUBLIC;
6771
6772 return 0;
6773 }
6774
_update_adv_data_sync(struct hci_dev * hdev,void * data)6775 static int _update_adv_data_sync(struct hci_dev *hdev, void *data)
6776 {
6777 u8 instance = PTR_UINT(data);
6778
6779 return hci_update_adv_data_sync(hdev, instance);
6780 }
6781
hci_update_adv_data(struct hci_dev * hdev,u8 instance)6782 int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
6783 {
6784 return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
6785 UINT_PTR(instance), NULL);
6786 }
6787
hci_acl_create_conn_sync(struct hci_dev * hdev,void * data)6788 static int hci_acl_create_conn_sync(struct hci_dev *hdev, void *data)
6789 {
6790 struct hci_conn *conn = data;
6791 struct inquiry_entry *ie;
6792 struct hci_cp_create_conn cp;
6793 int err;
6794
6795 if (!hci_conn_valid(hdev, conn))
6796 return -ECANCELED;
6797
6798 /* Many controllers disallow HCI Create Connection while it is doing
6799 * HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create
6800 * Connection. This may cause the MGMT discovering state to become false
6801 * without user space's request but it is okay since the MGMT Discovery
6802 * APIs do not promise that discovery should be done forever. Instead,
6803 * the user space monitors the status of MGMT discovering and it may
6804 * request for discovery again when this flag becomes false.
6805 */
6806 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
6807 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 0,
6808 NULL, HCI_CMD_TIMEOUT);
6809 if (err)
6810 bt_dev_warn(hdev, "Failed to cancel inquiry %d", err);
6811 }
6812
6813 conn->state = BT_CONNECT;
6814 conn->out = true;
6815 conn->role = HCI_ROLE_MASTER;
6816
6817 conn->attempt++;
6818
6819 conn->link_policy = hdev->link_policy;
6820
6821 memset(&cp, 0, sizeof(cp));
6822 bacpy(&cp.bdaddr, &conn->dst);
6823 cp.pscan_rep_mode = 0x02;
6824
6825 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
6826 if (ie) {
6827 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
6828 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
6829 cp.pscan_mode = ie->data.pscan_mode;
6830 cp.clock_offset = ie->data.clock_offset |
6831 cpu_to_le16(0x8000);
6832 }
6833
6834 memcpy(conn->dev_class, ie->data.dev_class, 3);
6835 }
6836
6837 cp.pkt_type = cpu_to_le16(conn->pkt_type);
6838 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
6839 cp.role_switch = 0x01;
6840 else
6841 cp.role_switch = 0x00;
6842
6843 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN,
6844 sizeof(cp), &cp,
6845 HCI_EV_CONN_COMPLETE,
6846 conn->conn_timeout, NULL);
6847 }
6848
hci_connect_acl_sync(struct hci_dev * hdev,struct hci_conn * conn)6849 int hci_connect_acl_sync(struct hci_dev *hdev, struct hci_conn *conn)
6850 {
6851 return hci_cmd_sync_queue_once(hdev, hci_acl_create_conn_sync, conn,
6852 NULL);
6853 }
6854
create_le_conn_complete(struct hci_dev * hdev,void * data,int err)6855 static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err)
6856 {
6857 struct hci_conn *conn = data;
6858
6859 bt_dev_dbg(hdev, "err %d", err);
6860
6861 if (err == -ECANCELED)
6862 return;
6863
6864 hci_dev_lock(hdev);
6865
6866 if (!hci_conn_valid(hdev, conn))
6867 goto done;
6868
6869 if (!err) {
6870 hci_connect_le_scan_cleanup(conn, 0x00);
6871 goto done;
6872 }
6873
6874 /* Check if connection is still pending */
6875 if (conn != hci_lookup_le_connect(hdev))
6876 goto done;
6877
6878 /* Flush to make sure we send create conn cancel command if needed */
6879 flush_delayed_work(&conn->le_conn_timeout);
6880 hci_conn_failed(conn, bt_status(err));
6881
6882 done:
6883 hci_dev_unlock(hdev);
6884 }
6885
hci_connect_le_sync(struct hci_dev * hdev,struct hci_conn * conn)6886 int hci_connect_le_sync(struct hci_dev *hdev, struct hci_conn *conn)
6887 {
6888 return hci_cmd_sync_queue_once(hdev, hci_le_create_conn_sync, conn,
6889 create_le_conn_complete);
6890 }
6891
hci_cancel_connect_sync(struct hci_dev * hdev,struct hci_conn * conn)6892 int hci_cancel_connect_sync(struct hci_dev *hdev, struct hci_conn *conn)
6893 {
6894 if (conn->state != BT_OPEN)
6895 return -EINVAL;
6896
6897 switch (conn->type) {
6898 case ACL_LINK:
6899 return !hci_cmd_sync_dequeue_once(hdev,
6900 hci_acl_create_conn_sync,
6901 conn, NULL);
6902 case LE_LINK:
6903 return !hci_cmd_sync_dequeue_once(hdev, hci_le_create_conn_sync,
6904 conn, create_le_conn_complete);
6905 }
6906
6907 return -ENOENT;
6908 }
6909
hci_le_conn_update_sync(struct hci_dev * hdev,struct hci_conn * conn,struct hci_conn_params * params)6910 int hci_le_conn_update_sync(struct hci_dev *hdev, struct hci_conn *conn,
6911 struct hci_conn_params *params)
6912 {
6913 struct hci_cp_le_conn_update cp;
6914
6915 memset(&cp, 0, sizeof(cp));
6916 cp.handle = cpu_to_le16(conn->handle);
6917 cp.conn_interval_min = cpu_to_le16(params->conn_min_interval);
6918 cp.conn_interval_max = cpu_to_le16(params->conn_max_interval);
6919 cp.conn_latency = cpu_to_le16(params->conn_latency);
6920 cp.supervision_timeout = cpu_to_le16(params->supervision_timeout);
6921 cp.min_ce_len = cpu_to_le16(0x0000);
6922 cp.max_ce_len = cpu_to_le16(0x0000);
6923
6924 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CONN_UPDATE,
6925 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
6926 }
6927
create_pa_complete(struct hci_dev * hdev,void * data,int err)6928 static void create_pa_complete(struct hci_dev *hdev, void *data, int err)
6929 {
6930 struct hci_conn *conn = data;
6931 struct hci_conn *pa_sync;
6932
6933 bt_dev_dbg(hdev, "err %d", err);
6934
6935 if (err == -ECANCELED)
6936 return;
6937
6938 hci_dev_lock(hdev);
6939
6940 hci_dev_clear_flag(hdev, HCI_PA_SYNC);
6941
6942 if (!hci_conn_valid(hdev, conn))
6943 clear_bit(HCI_CONN_CREATE_PA_SYNC, &conn->flags);
6944
6945 if (!err)
6946 goto unlock;
6947
6948 /* Add connection to indicate PA sync error */
6949 pa_sync = hci_conn_add_unset(hdev, BIS_LINK, BDADDR_ANY,
6950 HCI_ROLE_SLAVE);
6951
6952 if (IS_ERR(pa_sync))
6953 goto unlock;
6954
6955 set_bit(HCI_CONN_PA_SYNC_FAILED, &pa_sync->flags);
6956
6957 /* Notify iso layer */
6958 hci_connect_cfm(pa_sync, bt_status(err));
6959
6960 unlock:
6961 hci_dev_unlock(hdev);
6962 }
6963
hci_le_pa_create_sync(struct hci_dev * hdev,void * data)6964 static int hci_le_pa_create_sync(struct hci_dev *hdev, void *data)
6965 {
6966 struct hci_cp_le_pa_create_sync cp;
6967 struct hci_conn *conn = data;
6968 struct bt_iso_qos *qos = &conn->iso_qos;
6969 int err;
6970
6971 if (!hci_conn_valid(hdev, conn))
6972 return -ECANCELED;
6973
6974 if (conn->sync_handle != HCI_SYNC_HANDLE_INVALID)
6975 return -EINVAL;
6976
6977 if (hci_dev_test_and_set_flag(hdev, HCI_PA_SYNC))
6978 return -EBUSY;
6979
6980 /* Stop scanning if SID has not been set and active scanning is enabled
6981 * so we use passive scanning which will be scanning using the allow
6982 * list programmed to contain only the connection address.
6983 */
6984 if (conn->sid == HCI_SID_INVALID &&
6985 hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
6986 hci_scan_disable_sync(hdev);
6987 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
6988 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
6989 }
6990
6991 /* Mark HCI_CONN_CREATE_PA_SYNC so hci_update_passive_scan_sync can
6992 * program the address in the allow list so PA advertisements can be
6993 * received.
6994 */
6995 set_bit(HCI_CONN_CREATE_PA_SYNC, &conn->flags);
6996
6997 hci_update_passive_scan_sync(hdev);
6998
6999 /* SID has not been set listen for HCI_EV_LE_EXT_ADV_REPORT to update
7000 * it.
7001 */
7002 if (conn->sid == HCI_SID_INVALID)
7003 __hci_cmd_sync_status_sk(hdev, HCI_OP_NOP, 0, NULL,
7004 HCI_EV_LE_EXT_ADV_REPORT,
7005 conn->conn_timeout, NULL);
7006
7007 memset(&cp, 0, sizeof(cp));
7008 cp.options = qos->bcast.options;
7009 cp.sid = conn->sid;
7010 cp.addr_type = conn->dst_type;
7011 bacpy(&cp.addr, &conn->dst);
7012 cp.skip = cpu_to_le16(qos->bcast.skip);
7013 cp.sync_timeout = cpu_to_le16(qos->bcast.sync_timeout);
7014 cp.sync_cte_type = qos->bcast.sync_cte_type;
7015
7016 /* The spec allows only one pending LE Periodic Advertising Create
7017 * Sync command at a time so we forcefully wait for PA Sync Established
7018 * event since cmd_work can only schedule one command at a time.
7019 *
7020 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
7021 * page 2493:
7022 *
7023 * If the Host issues this command when another HCI_LE_Periodic_
7024 * Advertising_Create_Sync command is pending, the Controller shall
7025 * return the error code Command Disallowed (0x0C).
7026 */
7027 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_PA_CREATE_SYNC,
7028 sizeof(cp), &cp,
7029 HCI_EV_LE_PA_SYNC_ESTABLISHED,
7030 conn->conn_timeout, NULL);
7031 if (err == -ETIMEDOUT)
7032 __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_CREATE_SYNC_CANCEL,
7033 0, NULL, HCI_CMD_TIMEOUT);
7034
7035 return err;
7036 }
7037
hci_connect_pa_sync(struct hci_dev * hdev,struct hci_conn * conn)7038 int hci_connect_pa_sync(struct hci_dev *hdev, struct hci_conn *conn)
7039 {
7040 return hci_cmd_sync_queue_once(hdev, hci_le_pa_create_sync, conn,
7041 create_pa_complete);
7042 }
7043
create_big_complete(struct hci_dev * hdev,void * data,int err)7044 static void create_big_complete(struct hci_dev *hdev, void *data, int err)
7045 {
7046 struct hci_conn *conn = data;
7047
7048 bt_dev_dbg(hdev, "err %d", err);
7049
7050 if (err == -ECANCELED)
7051 return;
7052
7053 if (hci_conn_valid(hdev, conn))
7054 clear_bit(HCI_CONN_CREATE_BIG_SYNC, &conn->flags);
7055 }
7056
hci_le_big_create_sync(struct hci_dev * hdev,void * data)7057 static int hci_le_big_create_sync(struct hci_dev *hdev, void *data)
7058 {
7059 DEFINE_FLEX(struct hci_cp_le_big_create_sync, cp, bis, num_bis, 0x11);
7060 struct hci_conn *conn = data;
7061 struct bt_iso_qos *qos = &conn->iso_qos;
7062 int err;
7063
7064 if (!hci_conn_valid(hdev, conn))
7065 return -ECANCELED;
7066
7067 set_bit(HCI_CONN_CREATE_BIG_SYNC, &conn->flags);
7068
7069 memset(cp, 0, sizeof(*cp));
7070 cp->handle = qos->bcast.big;
7071 cp->sync_handle = cpu_to_le16(conn->sync_handle);
7072 cp->encryption = qos->bcast.encryption;
7073 memcpy(cp->bcode, qos->bcast.bcode, sizeof(cp->bcode));
7074 cp->mse = qos->bcast.mse;
7075 cp->timeout = cpu_to_le16(qos->bcast.timeout);
7076 cp->num_bis = conn->num_bis;
7077 memcpy(cp->bis, conn->bis, conn->num_bis);
7078
7079 /* The spec allows only one pending LE BIG Create Sync command at
7080 * a time, so we forcefully wait for BIG Sync Established event since
7081 * cmd_work can only schedule one command at a time.
7082 *
7083 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E
7084 * page 2586:
7085 *
7086 * If the Host sends this command when the Controller is in the
7087 * process of synchronizing to any BIG, i.e. the HCI_LE_BIG_Sync_
7088 * Established event has not been generated, the Controller shall
7089 * return the error code Command Disallowed (0x0C).
7090 */
7091 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_BIG_CREATE_SYNC,
7092 struct_size(cp, bis, cp->num_bis), cp,
7093 HCI_EVT_LE_BIG_SYNC_ESTABLISHED,
7094 conn->conn_timeout, NULL);
7095 if (err == -ETIMEDOUT)
7096 hci_le_big_terminate_sync(hdev, cp->handle);
7097
7098 return err;
7099 }
7100
hci_connect_big_sync(struct hci_dev * hdev,struct hci_conn * conn)7101 int hci_connect_big_sync(struct hci_dev *hdev, struct hci_conn *conn)
7102 {
7103 return hci_cmd_sync_queue_once(hdev, hci_le_big_create_sync, conn,
7104 create_big_complete);
7105 }
7106