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