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