Bluetooth: Remove dead code from hci_request.c
[linux-block.git] / net / bluetooth / hci_sync.c
CommitLineData
6a98e383
MH
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2021 Intel Corporation
6 */
7
d0b13706
LAD
8#include <linux/property.h>
9
6a98e383
MH
10#include <net/bluetooth/bluetooth.h>
11#include <net/bluetooth/hci_core.h>
12#include <net/bluetooth/mgmt.h>
13
14#include "hci_request.h"
d0b13706 15#include "hci_debugfs.h"
6a98e383 16#include "smp.h"
161510cc 17#include "eir.h"
d0b13706
LAD
18#include "msft.h"
19#include "aosp.h"
20#include "leds.h"
6a98e383
MH
21
22static void hci_cmd_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
23 struct sk_buff *skb)
24{
25 bt_dev_dbg(hdev, "result 0x%2.2x", result);
26
27 if (hdev->req_status != HCI_REQ_PEND)
28 return;
29
30 hdev->req_result = result;
31 hdev->req_status = HCI_REQ_DONE;
32
cba6b758
LAD
33 if (skb) {
34 struct sock *sk = hci_skb_sk(skb);
35
36 /* Drop sk reference if set */
37 if (sk)
38 sock_put(sk);
39
40 hdev->req_skb = skb_get(skb);
41 }
42
6a98e383
MH
43 wake_up_interruptible(&hdev->req_wait_q);
44}
45
46static struct sk_buff *hci_cmd_sync_alloc(struct hci_dev *hdev, u16 opcode,
47 u32 plen, const void *param,
48 struct sock *sk)
49{
50 int len = HCI_COMMAND_HDR_SIZE + plen;
51 struct hci_command_hdr *hdr;
52 struct sk_buff *skb;
53
54 skb = bt_skb_alloc(len, GFP_ATOMIC);
55 if (!skb)
56 return NULL;
57
58 hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
59 hdr->opcode = cpu_to_le16(opcode);
60 hdr->plen = plen;
61
62 if (plen)
63 skb_put_data(skb, param, plen);
64
65 bt_dev_dbg(hdev, "skb len %d", skb->len);
66
67 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
68 hci_skb_opcode(skb) = opcode;
69
cba6b758
LAD
70 /* Grab a reference if command needs to be associated with a sock (e.g.
71 * likely mgmt socket that initiated the command).
72 */
73 if (sk) {
74 hci_skb_sk(skb) = sk;
75 sock_hold(sk);
76 }
77
6a98e383
MH
78 return skb;
79}
80
81static void hci_cmd_sync_add(struct hci_request *req, u16 opcode, u32 plen,
82 const void *param, u8 event, struct sock *sk)
83{
84 struct hci_dev *hdev = req->hdev;
85 struct sk_buff *skb;
86
87 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
88
89 /* If an error occurred during request building, there is no point in
90 * queueing the HCI command. We can simply return.
91 */
92 if (req->err)
93 return;
94
95 skb = hci_cmd_sync_alloc(hdev, opcode, plen, param, sk);
96 if (!skb) {
97 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
98 opcode);
99 req->err = -ENOMEM;
100 return;
101 }
102
103 if (skb_queue_empty(&req->cmd_q))
104 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
105
85b56857 106 hci_skb_event(skb) = event;
6a98e383
MH
107
108 skb_queue_tail(&req->cmd_q, skb);
109}
110
111static int hci_cmd_sync_run(struct hci_request *req)
112{
113 struct hci_dev *hdev = req->hdev;
114 struct sk_buff *skb;
115 unsigned long flags;
116
117 bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
118
119 /* If an error occurred during request building, remove all HCI
120 * commands queued on the HCI request queue.
121 */
122 if (req->err) {
123 skb_queue_purge(&req->cmd_q);
124 return req->err;
125 }
126
127 /* Do not allow empty requests */
128 if (skb_queue_empty(&req->cmd_q))
129 return -ENODATA;
130
131 skb = skb_peek_tail(&req->cmd_q);
132 bt_cb(skb)->hci.req_complete_skb = hci_cmd_sync_complete;
133 bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
134
135 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
136 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
137 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
138
139 queue_work(hdev->workqueue, &hdev->cmd_work);
140
141 return 0;
142}
143
144/* This function requires the caller holds hdev->req_lock. */
145struct sk_buff *__hci_cmd_sync_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
146 const void *param, u8 event, u32 timeout,
147 struct sock *sk)
148{
149 struct hci_request req;
150 struct sk_buff *skb;
151 int err = 0;
152
d0b13706 153 bt_dev_dbg(hdev, "Opcode 0x%4x", opcode);
6a98e383
MH
154
155 hci_req_init(&req, hdev);
156
157 hci_cmd_sync_add(&req, opcode, plen, param, event, sk);
158
159 hdev->req_status = HCI_REQ_PEND;
160
161 err = hci_cmd_sync_run(&req);
162 if (err < 0)
163 return ERR_PTR(err);
164
165 err = wait_event_interruptible_timeout(hdev->req_wait_q,
166 hdev->req_status != HCI_REQ_PEND,
167 timeout);
168
169 if (err == -ERESTARTSYS)
170 return ERR_PTR(-EINTR);
171
172 switch (hdev->req_status) {
173 case HCI_REQ_DONE:
174 err = -bt_to_errno(hdev->req_result);
175 break;
176
177 case HCI_REQ_CANCELED:
178 err = -hdev->req_result;
179 break;
180
181 default:
182 err = -ETIMEDOUT;
183 break;
184 }
185
186 hdev->req_status = 0;
187 hdev->req_result = 0;
188 skb = hdev->req_skb;
189 hdev->req_skb = NULL;
190
191 bt_dev_dbg(hdev, "end: err %d", err);
192
193 if (err < 0) {
194 kfree_skb(skb);
195 return ERR_PTR(err);
196 }
197
6a98e383
MH
198 return skb;
199}
200EXPORT_SYMBOL(__hci_cmd_sync_sk);
201
202/* This function requires the caller holds hdev->req_lock. */
203struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
204 const void *param, u32 timeout)
205{
206 return __hci_cmd_sync_sk(hdev, opcode, plen, param, 0, timeout, NULL);
207}
208EXPORT_SYMBOL(__hci_cmd_sync);
209
210/* Send HCI command and wait for command complete event */
211struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
212 const void *param, u32 timeout)
213{
214 struct sk_buff *skb;
215
216 if (!test_bit(HCI_UP, &hdev->flags))
217 return ERR_PTR(-ENETDOWN);
218
219 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
220
221 hci_req_sync_lock(hdev);
222 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
223 hci_req_sync_unlock(hdev);
224
225 return skb;
226}
227EXPORT_SYMBOL(hci_cmd_sync);
228
229/* This function requires the caller holds hdev->req_lock. */
230struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
231 const void *param, u8 event, u32 timeout)
232{
233 return __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout,
234 NULL);
235}
236EXPORT_SYMBOL(__hci_cmd_sync_ev);
237
238/* This function requires the caller holds hdev->req_lock. */
239int __hci_cmd_sync_status_sk(struct hci_dev *hdev, u16 opcode, u32 plen,
240 const void *param, u8 event, u32 timeout,
241 struct sock *sk)
242{
243 struct sk_buff *skb;
244 u8 status;
245
246 skb = __hci_cmd_sync_sk(hdev, opcode, plen, param, event, timeout, sk);
abfeea47 247 if (IS_ERR(skb)) {
6a98e383
MH
248 bt_dev_err(hdev, "Opcode 0x%4x failed: %ld", opcode,
249 PTR_ERR(skb));
250 return PTR_ERR(skb);
251 }
252
abfeea47
LAD
253 /* If command return a status event skb will be set to NULL as there are
254 * no parameters, in case of failure IS_ERR(skb) would have be set to
255 * the actual error would be found with PTR_ERR(skb).
256 */
257 if (!skb)
258 return 0;
259
6a98e383
MH
260 status = skb->data[0];
261
262 kfree_skb(skb);
263
264 return status;
265}
266EXPORT_SYMBOL(__hci_cmd_sync_status_sk);
267
268int __hci_cmd_sync_status(struct hci_dev *hdev, u16 opcode, u32 plen,
269 const void *param, u32 timeout)
270{
271 return __hci_cmd_sync_status_sk(hdev, opcode, plen, param, 0, timeout,
272 NULL);
273}
274EXPORT_SYMBOL(__hci_cmd_sync_status);
275
276static void hci_cmd_sync_work(struct work_struct *work)
277{
278 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work);
6a98e383
MH
279
280 bt_dev_dbg(hdev, "");
281
008ee9eb
LAD
282 /* Dequeue all entries and run them */
283 while (1) {
284 struct hci_cmd_sync_work_entry *entry;
6a98e383 285
008ee9eb
LAD
286 mutex_lock(&hdev->cmd_sync_work_lock);
287 entry = list_first_entry_or_null(&hdev->cmd_sync_work_list,
288 struct hci_cmd_sync_work_entry,
289 list);
290 if (entry)
291 list_del(&entry->list);
292 mutex_unlock(&hdev->cmd_sync_work_lock);
6a98e383 293
008ee9eb
LAD
294 if (!entry)
295 break;
6a98e383 296
008ee9eb 297 bt_dev_dbg(hdev, "entry %p", entry);
6a98e383 298
008ee9eb
LAD
299 if (entry->func) {
300 int err;
6a98e383 301
008ee9eb
LAD
302 hci_req_sync_lock(hdev);
303 err = entry->func(hdev, entry->data);
304 if (entry->destroy)
305 entry->destroy(hdev, entry->data, err);
306 hci_req_sync_unlock(hdev);
307 }
308
309 kfree(entry);
6a98e383
MH
310 }
311}
312
744451c1
BB
313static void hci_cmd_sync_cancel_work(struct work_struct *work)
314{
315 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_cancel_work);
316
317 cancel_delayed_work_sync(&hdev->cmd_timer);
318 cancel_delayed_work_sync(&hdev->ncmd_timer);
319 atomic_set(&hdev->cmd_cnt, 1);
320
321 wake_up_interruptible(&hdev->req_wait_q);
322}
323
6a98e383
MH
324void hci_cmd_sync_init(struct hci_dev *hdev)
325{
326 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work);
327 INIT_LIST_HEAD(&hdev->cmd_sync_work_list);
328 mutex_init(&hdev->cmd_sync_work_lock);
744451c1
BB
329
330 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work);
6a98e383
MH
331}
332
333void hci_cmd_sync_clear(struct hci_dev *hdev)
334{
335 struct hci_cmd_sync_work_entry *entry, *tmp;
336
337 cancel_work_sync(&hdev->cmd_sync_work);
338
339 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) {
340 if (entry->destroy)
341 entry->destroy(hdev, entry->data, -ECANCELED);
342
343 list_del(&entry->list);
344 kfree(entry);
345 }
346}
347
744451c1 348void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
914b08b3
BB
349{
350 bt_dev_dbg(hdev, "err 0x%2.2x", err);
351
352 if (hdev->req_status == HCI_REQ_PEND) {
353 hdev->req_result = err;
354 hdev->req_status = HCI_REQ_CANCELED;
355
356 cancel_delayed_work_sync(&hdev->cmd_timer);
357 cancel_delayed_work_sync(&hdev->ncmd_timer);
358 atomic_set(&hdev->cmd_cnt, 1);
359
360 wake_up_interruptible(&hdev->req_wait_q);
361 }
362}
744451c1
BB
363
364void hci_cmd_sync_cancel(struct hci_dev *hdev, int err)
365{
366 bt_dev_dbg(hdev, "err 0x%2.2x", err);
367
368 if (hdev->req_status == HCI_REQ_PEND) {
369 hdev->req_result = err;
370 hdev->req_status = HCI_REQ_CANCELED;
371
372 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work);
373 }
374}
914b08b3
BB
375EXPORT_SYMBOL(hci_cmd_sync_cancel);
376
6a98e383
MH
377int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func,
378 void *data, hci_cmd_sync_work_destroy_t destroy)
379{
380 struct hci_cmd_sync_work_entry *entry;
381
0b94f265
LAD
382 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
383 return -ENODEV;
384
6a98e383
MH
385 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
386 if (!entry)
387 return -ENOMEM;
388
389 entry->func = func;
390 entry->data = data;
391 entry->destroy = destroy;
392
393 mutex_lock(&hdev->cmd_sync_work_lock);
394 list_add_tail(&entry->list, &hdev->cmd_sync_work_list);
395 mutex_unlock(&hdev->cmd_sync_work_lock);
396
397 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work);
398
399 return 0;
400}
401EXPORT_SYMBOL(hci_cmd_sync_queue);
161510cc
LAD
402
403int hci_update_eir_sync(struct hci_dev *hdev)
404{
405 struct hci_cp_write_eir cp;
406
407 bt_dev_dbg(hdev, "");
408
409 if (!hdev_is_powered(hdev))
410 return 0;
411
412 if (!lmp_ext_inq_capable(hdev))
413 return 0;
414
415 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
416 return 0;
417
418 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
419 return 0;
420
421 memset(&cp, 0, sizeof(cp));
422
423 eir_create(hdev, cp.data);
424
425 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
426 return 0;
427
428 memcpy(hdev->eir, cp.data, sizeof(cp.data));
429
430 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
431 HCI_CMD_TIMEOUT);
432}
433
434static u8 get_service_classes(struct hci_dev *hdev)
435{
436 struct bt_uuid *uuid;
437 u8 val = 0;
438
439 list_for_each_entry(uuid, &hdev->uuids, list)
440 val |= uuid->svc_hint;
441
442 return val;
443}
444
445int hci_update_class_sync(struct hci_dev *hdev)
446{
447 u8 cod[3];
448
449 bt_dev_dbg(hdev, "");
450
451 if (!hdev_is_powered(hdev))
452 return 0;
453
454 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
455 return 0;
456
457 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
458 return 0;
459
460 cod[0] = hdev->minor_class;
461 cod[1] = hdev->major_class;
462 cod[2] = get_service_classes(hdev);
463
464 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
465 cod[1] |= 0x20;
466
467 if (memcmp(cod, hdev->dev_class, 3) == 0)
468 return 0;
469
470 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV,
471 sizeof(cod), cod, HCI_CMD_TIMEOUT);
472}
cba6b758
LAD
473
474static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable)
475{
476 /* If there is no connection we are OK to advertise. */
477 if (hci_conn_num(hdev, LE_LINK) == 0)
478 return true;
479
480 /* Check le_states if there is any connection in peripheral role. */
481 if (hdev->conn_hash.le_num_peripheral > 0) {
482 /* Peripheral connection state and non connectable mode
483 * bit 20.
484 */
485 if (!connectable && !(hdev->le_states[2] & 0x10))
486 return false;
487
488 /* Peripheral connection state and connectable mode bit 38
489 * and scannable bit 21.
490 */
491 if (connectable && (!(hdev->le_states[4] & 0x40) ||
492 !(hdev->le_states[2] & 0x20)))
493 return false;
494 }
495
496 /* Check le_states if there is any connection in central role. */
497 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) {
498 /* Central connection state and non connectable mode bit 18. */
499 if (!connectable && !(hdev->le_states[2] & 0x02))
500 return false;
501
502 /* Central connection state and connectable mode bit 35 and
503 * scannable 19.
504 */
505 if (connectable && (!(hdev->le_states[4] & 0x08) ||
506 !(hdev->le_states[2] & 0x08)))
507 return false;
508 }
509
510 return true;
511}
512
513static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
514{
515 /* If privacy is not enabled don't use RPA */
516 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
517 return false;
518
519 /* If basic privacy mode is enabled use RPA */
520 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
521 return true;
522
523 /* If limited privacy mode is enabled don't use RPA if we're
524 * both discoverable and bondable.
525 */
526 if ((flags & MGMT_ADV_FLAG_DISCOV) &&
527 hci_dev_test_flag(hdev, HCI_BONDABLE))
528 return false;
529
530 /* We're neither bondable nor discoverable in the limited
531 * privacy mode, therefore use RPA.
532 */
533 return true;
534}
535
536static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa)
537{
538 /* If we're advertising or initiating an LE connection we can't
539 * go ahead and change the random address at this time. This is
540 * because the eventual initiator address used for the
541 * subsequently created connection will be undefined (some
542 * controllers use the new address and others the one we had
543 * when the operation started).
544 *
545 * In this kind of scenario skip the update and let the random
546 * address be updated at the next cycle.
547 */
548 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
549 hci_lookup_le_connect(hdev)) {
550 bt_dev_dbg(hdev, "Deferring random address update");
551 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
552 return 0;
553 }
554
555 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR,
556 6, rpa, HCI_CMD_TIMEOUT);
557}
558
559int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
560 bool rpa, u8 *own_addr_type)
561{
562 int err;
563
564 /* If privacy is enabled use a resolvable private address. If
565 * current RPA has expired or there is something else than
566 * the current RPA in use, then generate a new one.
567 */
568 if (rpa) {
569 /* If Controller supports LL Privacy use own address type is
570 * 0x03
571 */
ad383c2c 572 if (use_ll_privacy(hdev))
cba6b758
LAD
573 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
574 else
575 *own_addr_type = ADDR_LE_DEV_RANDOM;
576
577 /* Check if RPA is valid */
578 if (rpa_valid(hdev))
579 return 0;
580
581 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
582 if (err < 0) {
583 bt_dev_err(hdev, "failed to generate new RPA");
584 return err;
585 }
586
587 err = hci_set_random_addr_sync(hdev, &hdev->rpa);
588 if (err)
589 return err;
590
591 return 0;
592 }
593
594 /* In case of required privacy without resolvable private address,
595 * use an non-resolvable private address. This is useful for active
596 * scanning and non-connectable advertising.
597 */
598 if (require_privacy) {
599 bdaddr_t nrpa;
600
601 while (true) {
602 /* The non-resolvable private address is generated
603 * from random six bytes with the two most significant
604 * bits cleared.
605 */
606 get_random_bytes(&nrpa, 6);
607 nrpa.b[5] &= 0x3f;
608
609 /* The non-resolvable private address shall not be
610 * equal to the public address.
611 */
612 if (bacmp(&hdev->bdaddr, &nrpa))
613 break;
614 }
615
616 *own_addr_type = ADDR_LE_DEV_RANDOM;
617
618 return hci_set_random_addr_sync(hdev, &nrpa);
619 }
620
621 /* If forcing static address is in use or there is no public
622 * address use the static address as random address (but skip
623 * the HCI command if the current random address is already the
624 * static one.
625 *
626 * In case BR/EDR has been disabled on a dual-mode controller
627 * and a static address has been configured, then use that
628 * address instead of the public BR/EDR address.
629 */
630 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
631 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
632 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
633 bacmp(&hdev->static_addr, BDADDR_ANY))) {
634 *own_addr_type = ADDR_LE_DEV_RANDOM;
635 if (bacmp(&hdev->static_addr, &hdev->random_addr))
636 return hci_set_random_addr_sync(hdev,
637 &hdev->static_addr);
638 return 0;
639 }
640
641 /* Neither privacy nor static address is being used so use a
642 * public address.
643 */
644 *own_addr_type = ADDR_LE_DEV_PUBLIC;
645
646 return 0;
647}
648
649static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
650{
651 struct hci_cp_le_set_ext_adv_enable *cp;
652 struct hci_cp_ext_adv_set *set;
653 u8 data[sizeof(*cp) + sizeof(*set) * 1];
654 u8 size;
655
656 /* If request specifies an instance that doesn't exist, fail */
657 if (instance > 0) {
658 struct adv_info *adv;
659
660 adv = hci_find_adv_instance(hdev, instance);
661 if (!adv)
662 return -EINVAL;
663
664 /* If not enabled there is nothing to do */
665 if (!adv->enabled)
666 return 0;
667 }
668
669 memset(data, 0, sizeof(data));
670
671 cp = (void *)data;
672 set = (void *)cp->data;
673
674 /* Instance 0x00 indicates all advertising instances will be disabled */
675 cp->num_of_sets = !!instance;
676 cp->enable = 0x00;
677
678 set->handle = instance;
679
680 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets;
681
682 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
683 size, data, HCI_CMD_TIMEOUT);
684}
685
686static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance,
687 bdaddr_t *random_addr)
688{
689 struct hci_cp_le_set_adv_set_rand_addr cp;
690 int err;
691
692 if (!instance) {
693 /* Instance 0x00 doesn't have an adv_info, instead it uses
694 * hdev->random_addr to track its address so whenever it needs
695 * to be updated this also set the random address since
696 * hdev->random_addr is shared with scan state machine.
697 */
698 err = hci_set_random_addr_sync(hdev, random_addr);
699 if (err)
700 return err;
701 }
702
703 memset(&cp, 0, sizeof(cp));
704
705 cp.handle = instance;
706 bacpy(&cp.bdaddr, random_addr);
707
708 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
709 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
710}
711
712int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance)
713{
714 struct hci_cp_le_set_ext_adv_params cp;
715 bool connectable;
716 u32 flags;
717 bdaddr_t random_addr;
718 u8 own_addr_type;
719 int err;
720 struct adv_info *adv;
721 bool secondary_adv;
722
723 if (instance > 0) {
724 adv = hci_find_adv_instance(hdev, instance);
725 if (!adv)
726 return -EINVAL;
727 } else {
728 adv = NULL;
729 }
730
731 /* Updating parameters of an active instance will return a
732 * Command Disallowed error, so we must first disable the
733 * instance if it is active.
734 */
735 if (adv && !adv->pending) {
736 err = hci_disable_ext_adv_instance_sync(hdev, instance);
737 if (err)
738 return err;
739 }
740
741 flags = hci_adv_instance_flags(hdev, instance);
742
743 /* If the "connectable" instance flag was not set, then choose between
744 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
745 */
746 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
747 mgmt_get_connectable(hdev);
748
749 if (!is_advertising_allowed(hdev, connectable))
750 return -EPERM;
751
752 /* Set require_privacy to true only when non-connectable
753 * advertising is used. In that case it is fine to use a
754 * non-resolvable private address.
755 */
756 err = hci_get_random_address(hdev, !connectable,
757 adv_use_rpa(hdev, flags), adv,
758 &own_addr_type, &random_addr);
759 if (err < 0)
760 return err;
761
762 memset(&cp, 0, sizeof(cp));
763
764 if (adv) {
765 hci_cpu_to_le24(adv->min_interval, cp.min_interval);
766 hci_cpu_to_le24(adv->max_interval, cp.max_interval);
767 cp.tx_power = adv->tx_power;
768 } else {
769 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval);
770 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval);
771 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
772 }
773
774 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK);
775
776 if (connectable) {
777 if (secondary_adv)
778 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND);
779 else
780 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND);
781 } else if (hci_adv_instance_is_scannable(hdev, instance) ||
782 (flags & MGMT_ADV_PARAM_SCAN_RSP)) {
783 if (secondary_adv)
784 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND);
785 else
786 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND);
787 } else {
788 if (secondary_adv)
789 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND);
790 else
791 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND);
792 }
793
cf75ad8b
LAD
794 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter
795 * contains the peer’s Identity Address and the Peer_Address_Type
796 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01).
797 * These parameters are used to locate the corresponding local IRK in
798 * the resolving list; this IRK is used to generate their own address
799 * used in the advertisement.
800 */
801 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED)
802 hci_copy_identity_address(hdev, &cp.peer_addr,
803 &cp.peer_addr_type);
804
cba6b758
LAD
805 cp.own_addr_type = own_addr_type;
806 cp.channel_map = hdev->le_adv_channel_map;
807 cp.handle = instance;
808
809 if (flags & MGMT_ADV_FLAG_SEC_2M) {
810 cp.primary_phy = HCI_ADV_PHY_1M;
811 cp.secondary_phy = HCI_ADV_PHY_2M;
812 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) {
813 cp.primary_phy = HCI_ADV_PHY_CODED;
814 cp.secondary_phy = HCI_ADV_PHY_CODED;
815 } else {
816 /* In all other cases use 1M */
817 cp.primary_phy = HCI_ADV_PHY_1M;
818 cp.secondary_phy = HCI_ADV_PHY_1M;
819 }
820
821 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
822 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
823 if (err)
824 return err;
825
826 if ((own_addr_type == ADDR_LE_DEV_RANDOM ||
827 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) &&
828 bacmp(&random_addr, BDADDR_ANY)) {
829 /* Check if random address need to be updated */
830 if (adv) {
831 if (!bacmp(&random_addr, &adv->random_addr))
832 return 0;
833 } else {
834 if (!bacmp(&random_addr, &hdev->random_addr))
835 return 0;
836 }
837
838 return hci_set_adv_set_random_addr_sync(hdev, instance,
839 &random_addr);
840 }
841
842 return 0;
843}
844
845static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
846{
847 struct {
848 struct hci_cp_le_set_ext_scan_rsp_data cp;
849 u8 data[HCI_MAX_EXT_AD_LENGTH];
850 } pdu;
851 u8 len;
34a718bc
LAD
852 struct adv_info *adv = NULL;
853 int err;
cba6b758
LAD
854
855 memset(&pdu, 0, sizeof(pdu));
856
34a718bc
LAD
857 if (instance) {
858 adv = hci_find_adv_instance(hdev, instance);
859 if (!adv || !adv->scan_rsp_changed)
860 return 0;
861 }
cba6b758 862
34a718bc 863 len = eir_create_scan_rsp(hdev, instance, pdu.data);
cba6b758
LAD
864
865 pdu.cp.handle = instance;
866 pdu.cp.length = len;
867 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
868 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
869
34a718bc
LAD
870 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA,
871 sizeof(pdu.cp) + len, &pdu.cp,
872 HCI_CMD_TIMEOUT);
873 if (err)
874 return err;
875
876 if (adv) {
877 adv->scan_rsp_changed = false;
878 } else {
879 memcpy(hdev->scan_rsp_data, pdu.data, len);
880 hdev->scan_rsp_data_len = len;
881 }
882
883 return 0;
cba6b758
LAD
884}
885
886static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
887{
888 struct hci_cp_le_set_scan_rsp_data cp;
889 u8 len;
890
891 memset(&cp, 0, sizeof(cp));
892
893 len = eir_create_scan_rsp(hdev, instance, cp.data);
894
895 if (hdev->scan_rsp_data_len == len &&
896 !memcmp(cp.data, hdev->scan_rsp_data, len))
897 return 0;
898
899 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
900 hdev->scan_rsp_data_len = len;
901
902 cp.length = len;
903
904 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA,
905 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
906}
907
908int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance)
909{
910 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
911 return 0;
912
913 if (ext_adv_capable(hdev))
914 return hci_set_ext_scan_rsp_data_sync(hdev, instance);
915
916 return __hci_set_scan_rsp_data_sync(hdev, instance);
917}
918
919int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance)
920{
921 struct hci_cp_le_set_ext_adv_enable *cp;
922 struct hci_cp_ext_adv_set *set;
923 u8 data[sizeof(*cp) + sizeof(*set) * 1];
924 struct adv_info *adv;
925
926 if (instance > 0) {
927 adv = hci_find_adv_instance(hdev, instance);
928 if (!adv)
929 return -EINVAL;
930 /* If already enabled there is nothing to do */
931 if (adv->enabled)
932 return 0;
933 } else {
934 adv = NULL;
935 }
936
937 cp = (void *)data;
938 set = (void *)cp->data;
939
940 memset(cp, 0, sizeof(*cp));
941
942 cp->enable = 0x01;
943 cp->num_of_sets = 0x01;
944
945 memset(set, 0, sizeof(*set));
946
947 set->handle = instance;
948
949 /* Set duration per instance since controller is responsible for
950 * scheduling it.
951 */
f16a491c 952 if (adv && adv->timeout) {
cba6b758
LAD
953 u16 duration = adv->timeout * MSEC_PER_SEC;
954
955 /* Time = N * 10 ms */
956 set->duration = cpu_to_le16(duration / 10);
957 }
958
959 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE,
960 sizeof(*cp) +
961 sizeof(*set) * cp->num_of_sets,
962 data, HCI_CMD_TIMEOUT);
963}
964
965int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance)
966{
967 int err;
968
969 err = hci_setup_ext_adv_instance_sync(hdev, instance);
970 if (err)
971 return err;
972
973 err = hci_set_ext_scan_rsp_data_sync(hdev, instance);
974 if (err)
975 return err;
976
977 return hci_enable_ext_advertising_sync(hdev, instance);
978}
979
980static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance)
981{
982 int err;
983
984 if (ext_adv_capable(hdev))
985 return hci_start_ext_adv_sync(hdev, instance);
986
987 err = hci_update_adv_data_sync(hdev, instance);
988 if (err)
989 return err;
990
991 err = hci_update_scan_rsp_data_sync(hdev, instance);
992 if (err)
993 return err;
994
995 return hci_enable_advertising_sync(hdev);
996}
997
998int hci_enable_advertising_sync(struct hci_dev *hdev)
999{
1000 struct adv_info *adv_instance;
1001 struct hci_cp_le_set_adv_param cp;
1002 u8 own_addr_type, enable = 0x01;
1003 bool connectable;
1004 u16 adv_min_interval, adv_max_interval;
1005 u32 flags;
1006 u8 status;
1007
ad383c2c
LAD
1008 if (ext_adv_capable(hdev))
1009 return hci_enable_ext_advertising_sync(hdev,
1010 hdev->cur_adv_instance);
1011
cba6b758
LAD
1012 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance);
1013 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
1014
1015 /* If the "connectable" instance flag was not set, then choose between
1016 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1017 */
1018 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1019 mgmt_get_connectable(hdev);
1020
1021 if (!is_advertising_allowed(hdev, connectable))
1022 return -EINVAL;
1023
ad383c2c
LAD
1024 status = hci_disable_advertising_sync(hdev);
1025 if (status)
1026 return status;
cba6b758
LAD
1027
1028 /* Clear the HCI_LE_ADV bit temporarily so that the
1029 * hci_update_random_address knows that it's safe to go ahead
1030 * and write a new random address. The flag will be set back on
1031 * as soon as the SET_ADV_ENABLE HCI command completes.
1032 */
1033 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1034
1035 /* Set require_privacy to true only when non-connectable
1036 * advertising is used. In that case it is fine to use a
1037 * non-resolvable private address.
1038 */
1039 status = hci_update_random_address_sync(hdev, !connectable,
1040 adv_use_rpa(hdev, flags),
1041 &own_addr_type);
1042 if (status)
1043 return status;
1044
1045 memset(&cp, 0, sizeof(cp));
1046
1047 if (adv_instance) {
1048 adv_min_interval = adv_instance->min_interval;
1049 adv_max_interval = adv_instance->max_interval;
1050 } else {
1051 adv_min_interval = hdev->le_adv_min_interval;
1052 adv_max_interval = hdev->le_adv_max_interval;
1053 }
1054
1055 if (connectable) {
1056 cp.type = LE_ADV_IND;
1057 } else {
1058 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance))
1059 cp.type = LE_ADV_SCAN_IND;
1060 else
1061 cp.type = LE_ADV_NONCONN_IND;
1062
1063 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) ||
1064 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
1065 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN;
1066 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX;
1067 }
1068 }
1069
1070 cp.min_interval = cpu_to_le16(adv_min_interval);
1071 cp.max_interval = cpu_to_le16(adv_max_interval);
1072 cp.own_address_type = own_addr_type;
1073 cp.channel_map = hdev->le_adv_channel_map;
1074
1075 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
1076 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1077 if (status)
1078 return status;
1079
1080 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1081 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1082}
1083
abfeea47
LAD
1084static int enable_advertising_sync(struct hci_dev *hdev, void *data)
1085{
1086 return hci_enable_advertising_sync(hdev);
1087}
1088
1089int hci_enable_advertising(struct hci_dev *hdev)
1090{
1091 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1092 list_empty(&hdev->adv_instances))
1093 return 0;
1094
1095 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL);
1096}
1097
1098int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1099 struct sock *sk)
cba6b758
LAD
1100{
1101 int err;
1102
1103 if (!ext_adv_capable(hdev))
1104 return 0;
1105
1106 err = hci_disable_ext_adv_instance_sync(hdev, instance);
1107 if (err)
1108 return err;
1109
1110 /* If request specifies an instance that doesn't exist, fail */
1111 if (instance > 0 && !hci_find_adv_instance(hdev, instance))
1112 return -EINVAL;
1113
1114 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET,
1115 sizeof(instance), &instance, 0,
1116 HCI_CMD_TIMEOUT, sk);
1117}
1118
1119static void cancel_adv_timeout(struct hci_dev *hdev)
1120{
1121 if (hdev->adv_instance_timeout) {
1122 hdev->adv_instance_timeout = 0;
1123 cancel_delayed_work(&hdev->adv_instance_expire);
1124 }
1125}
1126
1127static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance)
1128{
1129 struct {
1130 struct hci_cp_le_set_ext_adv_data cp;
1131 u8 data[HCI_MAX_EXT_AD_LENGTH];
1132 } pdu;
1133 u8 len;
34a718bc
LAD
1134 struct adv_info *adv = NULL;
1135 int err;
cba6b758
LAD
1136
1137 memset(&pdu, 0, sizeof(pdu));
1138
34a718bc
LAD
1139 if (instance) {
1140 adv = hci_find_adv_instance(hdev, instance);
1141 if (!adv || !adv->adv_data_changed)
1142 return 0;
1143 }
cba6b758 1144
34a718bc 1145 len = eir_create_adv_data(hdev, instance, pdu.data);
cba6b758
LAD
1146
1147 pdu.cp.length = len;
1148 pdu.cp.handle = instance;
1149 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE;
1150 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG;
1151
34a718bc
LAD
1152 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA,
1153 sizeof(pdu.cp) + len, &pdu.cp,
1154 HCI_CMD_TIMEOUT);
1155 if (err)
1156 return err;
1157
1158 /* Update data if the command succeed */
1159 if (adv) {
1160 adv->adv_data_changed = false;
1161 } else {
1162 memcpy(hdev->adv_data, pdu.data, len);
1163 hdev->adv_data_len = len;
1164 }
1165
1166 return 0;
cba6b758
LAD
1167}
1168
1169static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance)
1170{
1171 struct hci_cp_le_set_adv_data cp;
1172 u8 len;
1173
1174 memset(&cp, 0, sizeof(cp));
1175
1176 len = eir_create_adv_data(hdev, instance, cp.data);
1177
1178 /* There's nothing to do if the data hasn't changed */
1179 if (hdev->adv_data_len == len &&
1180 memcmp(cp.data, hdev->adv_data, len) == 0)
1181 return 0;
1182
1183 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1184 hdev->adv_data_len = len;
1185
1186 cp.length = len;
1187
1188 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA,
1189 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1190}
1191
1192int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance)
1193{
1194 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1195 return 0;
1196
1197 if (ext_adv_capable(hdev))
1198 return hci_set_ext_adv_data_sync(hdev, instance);
1199
1200 return hci_set_adv_data_sync(hdev, instance);
1201}
1202
1203int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance,
1204 bool force)
1205{
1206 struct adv_info *adv = NULL;
1207 u16 timeout;
1208
cf75ad8b 1209 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev))
cba6b758
LAD
1210 return -EPERM;
1211
1212 if (hdev->adv_instance_timeout)
1213 return -EBUSY;
1214
1215 adv = hci_find_adv_instance(hdev, instance);
1216 if (!adv)
1217 return -ENOENT;
1218
1219 /* A zero timeout means unlimited advertising. As long as there is
1220 * only one instance, duration should be ignored. We still set a timeout
1221 * in case further instances are being added later on.
1222 *
1223 * If the remaining lifetime of the instance is more than the duration
1224 * then the timeout corresponds to the duration, otherwise it will be
1225 * reduced to the remaining instance lifetime.
1226 */
1227 if (adv->timeout == 0 || adv->duration <= adv->remaining_time)
1228 timeout = adv->duration;
1229 else
1230 timeout = adv->remaining_time;
1231
1232 /* The remaining time is being reduced unless the instance is being
1233 * advertised without time limit.
1234 */
1235 if (adv->timeout)
1236 adv->remaining_time = adv->remaining_time - timeout;
1237
1238 /* Only use work for scheduling instances with legacy advertising */
1239 if (!ext_adv_capable(hdev)) {
1240 hdev->adv_instance_timeout = timeout;
1241 queue_delayed_work(hdev->req_workqueue,
1242 &hdev->adv_instance_expire,
1243 msecs_to_jiffies(timeout * 1000));
1244 }
1245
1246 /* If we're just re-scheduling the same instance again then do not
1247 * execute any HCI commands. This happens when a single instance is
1248 * being advertised.
1249 */
1250 if (!force && hdev->cur_adv_instance == instance &&
1251 hci_dev_test_flag(hdev, HCI_LE_ADV))
1252 return 0;
1253
1254 hdev->cur_adv_instance = instance;
1255
1256 return hci_start_adv_sync(hdev, instance);
1257}
1258
1259static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk)
1260{
1261 int err;
1262
1263 if (!ext_adv_capable(hdev))
1264 return 0;
1265
1266 /* Disable instance 0x00 to disable all instances */
1267 err = hci_disable_ext_adv_instance_sync(hdev, 0x00);
1268 if (err)
1269 return err;
1270
1271 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS,
1272 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1273}
1274
1275static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force)
1276{
1277 struct adv_info *adv, *n;
1278
1279 if (ext_adv_capable(hdev))
1280 /* Remove all existing sets */
1281 return hci_clear_adv_sets_sync(hdev, sk);
1282
1283 /* This is safe as long as there is no command send while the lock is
1284 * held.
1285 */
1286 hci_dev_lock(hdev);
1287
1288 /* Cleanup non-ext instances */
1289 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1290 u8 instance = adv->instance;
1291 int err;
1292
1293 if (!(force || adv->timeout))
1294 continue;
1295
1296 err = hci_remove_adv_instance(hdev, instance);
1297 if (!err)
1298 mgmt_advertising_removed(sk, hdev, instance);
1299 }
1300
1301 hci_dev_unlock(hdev);
1302
1303 return 0;
1304}
1305
1306static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance,
1307 struct sock *sk)
1308{
1309 int err;
1310
1311 /* If we use extended advertising, instance has to be removed first. */
1312 if (ext_adv_capable(hdev))
1313 return hci_remove_ext_adv_instance_sync(hdev, instance, sk);
1314
1315 /* This is safe as long as there is no command send while the lock is
1316 * held.
1317 */
1318 hci_dev_lock(hdev);
1319
1320 err = hci_remove_adv_instance(hdev, instance);
1321 if (!err)
1322 mgmt_advertising_removed(sk, hdev, instance);
1323
1324 hci_dev_unlock(hdev);
1325
1326 return err;
1327}
1328
1329/* For a single instance:
1330 * - force == true: The instance will be removed even when its remaining
1331 * lifetime is not zero.
1332 * - force == false: the instance will be deactivated but kept stored unless
1333 * the remaining lifetime is zero.
1334 *
1335 * For instance == 0x00:
1336 * - force == true: All instances will be removed regardless of their timeout
1337 * setting.
1338 * - force == false: Only instances that have a timeout will be removed.
1339 */
1340int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk,
1341 u8 instance, bool force)
1342{
1343 struct adv_info *next = NULL;
1344 int err;
1345
1346 /* Cancel any timeout concerning the removed instance(s). */
1347 if (!instance || hdev->cur_adv_instance == instance)
1348 cancel_adv_timeout(hdev);
1349
1350 /* Get the next instance to advertise BEFORE we remove
1351 * the current one. This can be the same instance again
1352 * if there is only one instance.
1353 */
1354 if (hdev->cur_adv_instance == instance)
1355 next = hci_get_next_instance(hdev, instance);
1356
1357 if (!instance) {
1358 err = hci_clear_adv_sync(hdev, sk, force);
1359 if (err)
1360 return err;
1361 } else {
1362 struct adv_info *adv = hci_find_adv_instance(hdev, instance);
1363
1364 if (force || (adv && adv->timeout && !adv->remaining_time)) {
1365 /* Don't advertise a removed instance. */
1366 if (next && next->instance == instance)
1367 next = NULL;
1368
1369 err = hci_remove_adv_sync(hdev, instance, sk);
1370 if (err)
1371 return err;
1372 }
1373 }
1374
1375 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING))
1376 return 0;
1377
1378 if (next && !ext_adv_capable(hdev))
1379 hci_schedule_adv_instance_sync(hdev, next->instance, false);
1380
1381 return 0;
1382}
1383
47db6b42
BG
1384int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle)
1385{
1386 struct hci_cp_read_rssi cp;
1387
1388 cp.handle = handle;
1389 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI,
1390 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1391}
1392
5a750137
BG
1393int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp)
1394{
1395 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK,
1396 sizeof(*cp), cp, HCI_CMD_TIMEOUT);
1397}
1398
47db6b42
BG
1399int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type)
1400{
1401 struct hci_cp_read_tx_power cp;
1402
1403 cp.handle = handle;
1404 cp.type = type;
1405 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER,
1406 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1407}
1408
cba6b758
LAD
1409int hci_disable_advertising_sync(struct hci_dev *hdev)
1410{
1411 u8 enable = 0x00;
1412
ad383c2c
LAD
1413 /* If controller is not advertising we are done. */
1414 if (!hci_dev_test_flag(hdev, HCI_LE_ADV))
1415 return 0;
1416
cba6b758
LAD
1417 if (ext_adv_capable(hdev))
1418 return hci_disable_ext_adv_instance_sync(hdev, 0x00);
1419
1420 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
1421 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
1422}
e8907f76
LAD
1423
1424static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val,
1425 u8 filter_dup)
1426{
1427 struct hci_cp_le_set_ext_scan_enable cp;
1428
1429 memset(&cp, 0, sizeof(cp));
1430 cp.enable = val;
1431 cp.filter_dup = filter_dup;
1432
1433 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1434 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1435}
1436
1437static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val,
1438 u8 filter_dup)
1439{
1440 struct hci_cp_le_set_scan_enable cp;
1441
1442 if (use_ext_scan(hdev))
1443 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup);
1444
1445 memset(&cp, 0, sizeof(cp));
1446 cp.enable = val;
1447 cp.filter_dup = filter_dup;
1448
1449 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE,
1450 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1451}
1452
1453static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val)
1454{
ad383c2c
LAD
1455 if (!use_ll_privacy(hdev))
1456 return 0;
1457
1458 /* If controller is not/already resolving we are done. */
1459 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
e8907f76
LAD
1460 return 0;
1461
1462 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
1463 sizeof(val), &val, HCI_CMD_TIMEOUT);
1464}
1465
27592ca1 1466static int hci_scan_disable_sync(struct hci_dev *hdev)
e8907f76
LAD
1467{
1468 int err;
1469
1470 /* If controller is not scanning we are done. */
1471 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
1472 return 0;
1473
1474 if (hdev->scanning_paused) {
1475 bt_dev_dbg(hdev, "Scanning is paused for suspend");
1476 return 0;
1477 }
1478
e8907f76
LAD
1479 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00);
1480 if (err) {
1481 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
1482 return err;
1483 }
1484
e8907f76
LAD
1485 return err;
1486}
1487
1488static bool scan_use_rpa(struct hci_dev *hdev)
1489{
1490 return hci_dev_test_flag(hdev, HCI_PRIVACY);
1491}
1492
1493static void hci_start_interleave_scan(struct hci_dev *hdev)
1494{
1495 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
1496 queue_delayed_work(hdev->req_workqueue,
1497 &hdev->interleave_scan, 0);
1498}
1499
1500static bool is_interleave_scanning(struct hci_dev *hdev)
1501{
1502 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
1503}
1504
1505static void cancel_interleave_scan(struct hci_dev *hdev)
1506{
1507 bt_dev_dbg(hdev, "cancelling interleave scan");
1508
1509 cancel_delayed_work_sync(&hdev->interleave_scan);
1510
1511 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
1512}
1513
1514/* Return true if interleave_scan wasn't started until exiting this function,
1515 * otherwise, return false
1516 */
1517static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev)
1518{
1519 /* Do interleaved scan only if all of the following are true:
1520 * - There is at least one ADV monitor
1521 * - At least one pending LE connection or one device to be scanned for
1522 * - Monitor offloading is not supported
1523 * If so, we should alternate between allowlist scan and one without
1524 * any filters to save power.
1525 */
1526 bool use_interleaving = hci_is_adv_monitoring(hdev) &&
1527 !(list_empty(&hdev->pend_le_conns) &&
1528 list_empty(&hdev->pend_le_reports)) &&
1529 hci_get_adv_monitor_offload_ext(hdev) ==
1530 HCI_ADV_MONITOR_EXT_NONE;
1531 bool is_interleaving = is_interleave_scanning(hdev);
1532
1533 if (use_interleaving && !is_interleaving) {
1534 hci_start_interleave_scan(hdev);
1535 bt_dev_dbg(hdev, "starting interleave scan");
1536 return true;
1537 }
1538
1539 if (!use_interleaving && is_interleaving)
1540 cancel_interleave_scan(hdev);
1541
1542 return false;
1543}
1544
1545/* Removes connection to resolve list if needed.*/
1546static int hci_le_del_resolve_list_sync(struct hci_dev *hdev,
1547 bdaddr_t *bdaddr, u8 bdaddr_type)
1548{
1549 struct hci_cp_le_del_from_resolv_list cp;
1550 struct bdaddr_list_with_irk *entry;
1551
ad383c2c 1552 if (!use_ll_privacy(hdev))
e8907f76
LAD
1553 return 0;
1554
1555 /* Check if the IRK has been programmed */
1556 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr,
1557 bdaddr_type);
1558 if (!entry)
1559 return 0;
1560
1561 cp.bdaddr_type = bdaddr_type;
1562 bacpy(&cp.bdaddr, bdaddr);
1563
1564 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
1565 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1566}
1567
1568static int hci_le_del_accept_list_sync(struct hci_dev *hdev,
1569 bdaddr_t *bdaddr, u8 bdaddr_type)
1570{
1571 struct hci_cp_le_del_from_accept_list cp;
1572 int err;
1573
1574 /* Check if device is on accept list before removing it */
1575 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type))
1576 return 0;
1577
1578 cp.bdaddr_type = bdaddr_type;
1579 bacpy(&cp.bdaddr, bdaddr);
1580
ad383c2c
LAD
1581 /* Ignore errors when removing from resolving list as that is likely
1582 * that the device was never added.
1583 */
1584 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
1585
e8907f76
LAD
1586 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
1587 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1588 if (err) {
1589 bt_dev_err(hdev, "Unable to remove from allow list: %d", err);
1590 return err;
1591 }
1592
1593 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr,
1594 cp.bdaddr_type);
1595
ad383c2c 1596 return 0;
e8907f76
LAD
1597}
1598
cf75ad8b
LAD
1599/* Adds connection to resolve list if needed.
1600 * Setting params to NULL programs local hdev->irk
1601 */
e8907f76
LAD
1602static int hci_le_add_resolve_list_sync(struct hci_dev *hdev,
1603 struct hci_conn_params *params)
1604{
1605 struct hci_cp_le_add_to_resolv_list cp;
1606 struct smp_irk *irk;
1607 struct bdaddr_list_with_irk *entry;
1608
ad383c2c 1609 if (!use_ll_privacy(hdev))
e8907f76
LAD
1610 return 0;
1611
cf75ad8b
LAD
1612 /* Attempt to program local identity address, type and irk if params is
1613 * NULL.
1614 */
1615 if (!params) {
1616 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
1617 return 0;
1618
1619 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type);
1620 memcpy(cp.peer_irk, hdev->irk, 16);
1621 goto done;
1622 }
1623
e8907f76
LAD
1624 irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
1625 if (!irk)
1626 return 0;
1627
1628 /* Check if the IK has _not_ been programmed yet. */
1629 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list,
1630 &params->addr,
1631 params->addr_type);
1632 if (entry)
1633 return 0;
1634
1635 cp.bdaddr_type = params->addr_type;
1636 bacpy(&cp.bdaddr, &params->addr);
1637 memcpy(cp.peer_irk, irk->val, 16);
1638
0900b1c6
LAD
1639 /* Default privacy mode is always Network */
1640 params->privacy_mode = HCI_NETWORK_PRIVACY;
1641
cf75ad8b 1642done:
e8907f76
LAD
1643 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
1644 memcpy(cp.local_irk, hdev->irk, 16);
1645 else
1646 memset(cp.local_irk, 0, 16);
1647
1648 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST,
1649 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1650}
1651
853b70b5
LAD
1652/* Set Device Privacy Mode. */
1653static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev,
1654 struct hci_conn_params *params)
1655{
1656 struct hci_cp_le_set_privacy_mode cp;
1657 struct smp_irk *irk;
1658
1659 /* If device privacy mode has already been set there is nothing to do */
1660 if (params->privacy_mode == HCI_DEVICE_PRIVACY)
1661 return 0;
1662
1663 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also
1664 * indicates that LL Privacy has been enabled and
1665 * HCI_OP_LE_SET_PRIVACY_MODE is supported.
1666 */
e1cff700 1667 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY))
853b70b5
LAD
1668 return 0;
1669
1670 irk = hci_find_irk_by_addr(hdev, &params->addr, params->addr_type);
1671 if (!irk)
1672 return 0;
1673
1674 memset(&cp, 0, sizeof(cp));
1675 cp.bdaddr_type = irk->addr_type;
1676 bacpy(&cp.bdaddr, &irk->bdaddr);
1677 cp.mode = HCI_DEVICE_PRIVACY;
1678
1679 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE,
1680 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1681}
1682
e8907f76 1683/* Adds connection to allow list if needed, if the device uses RPA (has IRK)
853b70b5
LAD
1684 * this attempts to program the device in the resolving list as well and
1685 * properly set the privacy mode.
e8907f76
LAD
1686 */
1687static int hci_le_add_accept_list_sync(struct hci_dev *hdev,
1688 struct hci_conn_params *params,
ad383c2c 1689 u8 *num_entries)
e8907f76
LAD
1690{
1691 struct hci_cp_le_add_to_accept_list cp;
1692 int err;
1693
3b420553
LAD
1694 /* During suspend, only wakeable devices can be in acceptlist */
1695 if (hdev->suspended &&
e1cff700 1696 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
3b420553
LAD
1697 return 0;
1698
e8907f76
LAD
1699 /* Select filter policy to accept all advertising */
1700 if (*num_entries >= hdev->le_accept_list_size)
1701 return -ENOSPC;
1702
1703 /* Accept list can not be used with RPAs */
ad383c2c 1704 if (!use_ll_privacy(hdev) &&
3b420553 1705 hci_find_irk_by_addr(hdev, &params->addr, params->addr_type))
e8907f76 1706 return -EINVAL;
e8907f76 1707
ad383c2c
LAD
1708 /* Attempt to program the device in the resolving list first to avoid
1709 * having to rollback in case it fails since the resolving list is
1710 * dynamic it can probably be smaller than the accept list.
1711 */
1712 err = hci_le_add_resolve_list_sync(hdev, params);
1713 if (err) {
1714 bt_dev_err(hdev, "Unable to add to resolve list: %d", err);
1715 return err;
1716 }
1717
853b70b5
LAD
1718 /* Set Privacy Mode */
1719 err = hci_le_set_privacy_mode_sync(hdev, params);
1720 if (err) {
1721 bt_dev_err(hdev, "Unable to set privacy mode: %d", err);
1722 return err;
1723 }
1724
1725 /* Check if already in accept list */
1726 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
1727 params->addr_type))
1728 return 0;
1729
e8907f76
LAD
1730 *num_entries += 1;
1731 cp.bdaddr_type = params->addr_type;
1732 bacpy(&cp.bdaddr, &params->addr);
1733
1734 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
1735 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
1736 if (err) {
1737 bt_dev_err(hdev, "Unable to add to allow list: %d", err);
ad383c2c
LAD
1738 /* Rollback the device from the resolving list */
1739 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type);
e8907f76
LAD
1740 return err;
1741 }
1742
1743 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr,
1744 cp.bdaddr_type);
1745
ad383c2c
LAD
1746 return 0;
1747}
1748
182ee45d 1749/* This function disables/pause all advertising instances */
ad383c2c
LAD
1750static int hci_pause_advertising_sync(struct hci_dev *hdev)
1751{
1752 int err;
182ee45d 1753 int old_state;
ad383c2c 1754
9446bdde
LAD
1755 /* If already been paused there is nothing to do. */
1756 if (hdev->advertising_paused)
ad383c2c
LAD
1757 return 0;
1758
182ee45d
LAD
1759 bt_dev_dbg(hdev, "Pausing directed advertising");
1760
1761 /* Stop directed advertising */
1762 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING);
1763 if (old_state) {
1764 /* When discoverable timeout triggers, then just make sure
1765 * the limited discoverable flag is cleared. Even in the case
1766 * of a timeout triggered from general discoverable, it is
1767 * safe to unconditionally clear the flag.
1768 */
1769 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1770 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1771 hdev->discov_timeout = 0;
1772 }
1773
ad383c2c
LAD
1774 bt_dev_dbg(hdev, "Pausing advertising instances");
1775
1776 /* Call to disable any advertisements active on the controller.
1777 * This will succeed even if no advertisements are configured.
1778 */
1779 err = hci_disable_advertising_sync(hdev);
1780 if (err)
1781 return err;
1782
1783 /* If we are using software rotation, pause the loop */
1784 if (!ext_adv_capable(hdev))
1785 cancel_adv_timeout(hdev);
1786
1787 hdev->advertising_paused = true;
182ee45d 1788 hdev->advertising_old_state = old_state;
ad383c2c
LAD
1789
1790 return 0;
e8907f76
LAD
1791}
1792
182ee45d 1793/* This function enables all user advertising instances */
ad383c2c
LAD
1794static int hci_resume_advertising_sync(struct hci_dev *hdev)
1795{
1796 struct adv_info *adv, *tmp;
1797 int err;
1798
1799 /* If advertising has not been paused there is nothing to do. */
1800 if (!hdev->advertising_paused)
1801 return 0;
1802
182ee45d
LAD
1803 /* Resume directed advertising */
1804 hdev->advertising_paused = false;
1805 if (hdev->advertising_old_state) {
1806 hci_dev_set_flag(hdev, HCI_ADVERTISING);
182ee45d
LAD
1807 hdev->advertising_old_state = 0;
1808 }
1809
ad383c2c
LAD
1810 bt_dev_dbg(hdev, "Resuming advertising instances");
1811
1812 if (ext_adv_capable(hdev)) {
1813 /* Call for each tracked instance to be re-enabled */
1814 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) {
1815 err = hci_enable_ext_advertising_sync(hdev,
1816 adv->instance);
1817 if (!err)
1818 continue;
1819
1820 /* If the instance cannot be resumed remove it */
1821 hci_remove_ext_adv_instance_sync(hdev, adv->instance,
1822 NULL);
1823 }
1824 } else {
1825 /* Schedule for most recent instance to be restarted and begin
1826 * the software rotation loop
1827 */
1828 err = hci_schedule_adv_instance_sync(hdev,
1829 hdev->cur_adv_instance,
1830 true);
1831 }
1832
1833 hdev->advertising_paused = false;
1834
1835 return err;
1836}
1837
f892244b
BG
1838struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev,
1839 bool extended, struct sock *sk)
1840{
1841 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA :
1842 HCI_OP_READ_LOCAL_OOB_DATA;
1843
1844 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk);
1845}
1846
ad383c2c
LAD
1847/* Device must not be scanning when updating the accept list.
1848 *
1849 * Update is done using the following sequence:
1850 *
1851 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) ->
1852 * Remove Devices From Accept List ->
1853 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))->
1854 * Add Devices to Accept List ->
1855 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) ->
1856 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) ->
1857 * Enable Scanning
1858 *
1859 * In case of failure advertising shall be restored to its original state and
1860 * return would disable accept list since either accept or resolving list could
1861 * not be programmed.
1862 *
1863 */
e8907f76
LAD
1864static u8 hci_update_accept_list_sync(struct hci_dev *hdev)
1865{
1866 struct hci_conn_params *params;
1867 struct bdaddr_list *b, *t;
1868 u8 num_entries = 0;
1869 bool pend_conn, pend_report;
80740ebb 1870 u8 filter_policy;
ad383c2c
LAD
1871 int err;
1872
1873 /* Pause advertising if resolving list can be used as controllers are
1874 * cannot accept resolving list modifications while advertising.
e8907f76 1875 */
ad383c2c
LAD
1876 if (use_ll_privacy(hdev)) {
1877 err = hci_pause_advertising_sync(hdev);
1878 if (err) {
1879 bt_dev_err(hdev, "pause advertising failed: %d", err);
1880 return 0x00;
1881 }
1882 }
e8907f76 1883
ad383c2c
LAD
1884 /* Disable address resolution while reprogramming accept list since
1885 * devices that do have an IRK will be programmed in the resolving list
1886 * when LL Privacy is enabled.
1887 */
1888 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
1889 if (err) {
1890 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err);
1891 goto done;
1892 }
e8907f76
LAD
1893
1894 /* Go through the current accept list programmed into the
6828b583
LAD
1895 * controller one by one and check if that address is connected or is
1896 * still in the list of pending connections or list of devices to
e8907f76
LAD
1897 * report. If not present in either list, then remove it from
1898 * the controller.
1899 */
1900 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) {
6828b583
LAD
1901 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type))
1902 continue;
1903
e8907f76
LAD
1904 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
1905 &b->bdaddr,
1906 b->bdaddr_type);
1907 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
1908 &b->bdaddr,
1909 b->bdaddr_type);
1910
1911 /* If the device is not likely to connect or report,
1912 * remove it from the acceptlist.
1913 */
1914 if (!pend_conn && !pend_report) {
1915 hci_le_del_accept_list_sync(hdev, &b->bdaddr,
1916 b->bdaddr_type);
1917 continue;
1918 }
1919
e8907f76
LAD
1920 num_entries++;
1921 }
1922
1923 /* Since all no longer valid accept list entries have been
1924 * removed, walk through the list of pending connections
1925 * and ensure that any new device gets programmed into
1926 * the controller.
1927 *
1928 * If the list of the devices is larger than the list of
1929 * available accept list entries in the controller, then
1930 * just abort and return filer policy value to not use the
1931 * accept list.
1932 */
1933 list_for_each_entry(params, &hdev->pend_le_conns, action) {
ad383c2c
LAD
1934 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
1935 if (err)
1936 goto done;
e8907f76
LAD
1937 }
1938
1939 /* After adding all new pending connections, walk through
1940 * the list of pending reports and also add these to the
1941 * accept list if there is still space. Abort if space runs out.
1942 */
1943 list_for_each_entry(params, &hdev->pend_le_reports, action) {
ad383c2c
LAD
1944 err = hci_le_add_accept_list_sync(hdev, params, &num_entries);
1945 if (err)
1946 goto done;
e8907f76
LAD
1947 }
1948
1949 /* Use the allowlist unless the following conditions are all true:
1950 * - We are not currently suspending
1951 * - There are 1 or more ADV monitors registered and it's not offloaded
1952 * - Interleaved scanning is not currently using the allowlist
1953 */
1954 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
1955 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
1956 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
ad383c2c
LAD
1957 err = -EINVAL;
1958
1959done:
80740ebb
LAD
1960 filter_policy = err ? 0x00 : 0x01;
1961
ad383c2c
LAD
1962 /* Enable address resolution when LL Privacy is enabled. */
1963 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
1964 if (err)
1965 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err);
1966
1967 /* Resume advertising if it was paused */
1968 if (use_ll_privacy(hdev))
1969 hci_resume_advertising_sync(hdev);
e8907f76
LAD
1970
1971 /* Select filter policy to use accept list */
80740ebb 1972 return filter_policy;
e8907f76
LAD
1973}
1974
1975/* Returns true if an le connection is in the scanning state */
1976static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
1977{
1978 struct hci_conn_hash *h = &hdev->conn_hash;
1979 struct hci_conn *c;
1980
1981 rcu_read_lock();
1982
1983 list_for_each_entry_rcu(c, &h->list, list) {
1984 if (c->type == LE_LINK && c->state == BT_CONNECT &&
1985 test_bit(HCI_CONN_SCANNING, &c->flags)) {
1986 rcu_read_unlock();
1987 return true;
1988 }
1989 }
1990
1991 rcu_read_unlock();
1992
1993 return false;
1994}
1995
1996static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type,
1997 u16 interval, u16 window,
1998 u8 own_addr_type, u8 filter_policy)
1999{
2000 struct hci_cp_le_set_ext_scan_params *cp;
2001 struct hci_cp_le_scan_phy_params *phy;
2002 u8 data[sizeof(*cp) + sizeof(*phy) * 2];
2003 u8 num_phy = 0;
2004
2005 cp = (void *)data;
2006 phy = (void *)cp->data;
2007
2008 memset(data, 0, sizeof(data));
2009
2010 cp->own_addr_type = own_addr_type;
2011 cp->filter_policy = filter_policy;
2012
2013 if (scan_1m(hdev) || scan_2m(hdev)) {
2014 cp->scanning_phys |= LE_SCAN_PHY_1M;
2015
2016 phy->type = type;
2017 phy->interval = cpu_to_le16(interval);
2018 phy->window = cpu_to_le16(window);
2019
2020 num_phy++;
2021 phy++;
2022 }
2023
2024 if (scan_coded(hdev)) {
2025 cp->scanning_phys |= LE_SCAN_PHY_CODED;
2026
2027 phy->type = type;
2028 phy->interval = cpu_to_le16(interval);
2029 phy->window = cpu_to_le16(window);
2030
2031 num_phy++;
2032 phy++;
2033 }
2034
2035 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
2036 sizeof(*cp) + sizeof(*phy) * num_phy,
2037 data, HCI_CMD_TIMEOUT);
2038}
2039
2040static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type,
2041 u16 interval, u16 window,
2042 u8 own_addr_type, u8 filter_policy)
2043{
2044 struct hci_cp_le_set_scan_param cp;
2045
2046 if (use_ext_scan(hdev))
2047 return hci_le_set_ext_scan_param_sync(hdev, type, interval,
2048 window, own_addr_type,
2049 filter_policy);
2050
2051 memset(&cp, 0, sizeof(cp));
2052 cp.type = type;
2053 cp.interval = cpu_to_le16(interval);
2054 cp.window = cpu_to_le16(window);
2055 cp.own_address_type = own_addr_type;
2056 cp.filter_policy = filter_policy;
2057
2058 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM,
2059 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2060}
2061
2062static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval,
abfeea47
LAD
2063 u16 window, u8 own_addr_type, u8 filter_policy,
2064 u8 filter_dup)
e8907f76
LAD
2065{
2066 int err;
2067
2068 if (hdev->scanning_paused) {
2069 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2070 return 0;
2071 }
2072
e8907f76
LAD
2073 err = hci_le_set_scan_param_sync(hdev, type, interval, window,
2074 own_addr_type, filter_policy);
2075 if (err)
2076 return err;
2077
abfeea47 2078 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup);
e8907f76
LAD
2079}
2080
27592ca1 2081static int hci_passive_scan_sync(struct hci_dev *hdev)
e8907f76
LAD
2082{
2083 u8 own_addr_type;
2084 u8 filter_policy;
2085 u16 window, interval;
ad383c2c 2086 int err;
e8907f76
LAD
2087
2088 if (hdev->scanning_paused) {
2089 bt_dev_dbg(hdev, "Scanning is paused for suspend");
2090 return 0;
2091 }
2092
ad383c2c
LAD
2093 err = hci_scan_disable_sync(hdev);
2094 if (err) {
2095 bt_dev_err(hdev, "disable scanning failed: %d", err);
2096 return err;
2097 }
2098
e8907f76
LAD
2099 /* Set require_privacy to false since no SCAN_REQ are send
2100 * during passive scanning. Not using an non-resolvable address
2101 * here is important so that peer devices using direct
2102 * advertising with our address will be correctly reported
2103 * by the controller.
2104 */
2105 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev),
2106 &own_addr_type))
2107 return 0;
2108
2109 if (hdev->enable_advmon_interleave_scan &&
2110 hci_update_interleaved_scan_sync(hdev))
2111 return 0;
2112
2113 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
ad383c2c 2114
e8907f76
LAD
2115 /* Adding or removing entries from the accept list must
2116 * happen before enabling scanning. The controller does
2117 * not allow accept list modification while scanning.
2118 */
2119 filter_policy = hci_update_accept_list_sync(hdev);
2120
2121 /* When the controller is using random resolvable addresses and
2122 * with that having LE privacy enabled, then controllers with
2123 * Extended Scanner Filter Policies support can now enable support
2124 * for handling directed advertising.
2125 *
2126 * So instead of using filter polices 0x00 (no acceptlist)
2127 * and 0x01 (acceptlist enabled) use the new filter policies
2128 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled).
2129 */
2130 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
2131 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
2132 filter_policy |= 0x02;
2133
2134 if (hdev->suspended) {
2135 window = hdev->le_scan_window_suspend;
2136 interval = hdev->le_scan_int_suspend;
e8907f76
LAD
2137 } else if (hci_is_le_conn_scanning(hdev)) {
2138 window = hdev->le_scan_window_connect;
2139 interval = hdev->le_scan_int_connect;
2140 } else if (hci_is_adv_monitoring(hdev)) {
2141 window = hdev->le_scan_window_adv_monitor;
2142 interval = hdev->le_scan_int_adv_monitor;
2143 } else {
2144 window = hdev->le_scan_window;
2145 interval = hdev->le_scan_interval;
2146 }
2147
2148 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy);
2149
2150 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window,
abfeea47
LAD
2151 own_addr_type, filter_policy,
2152 LE_SCAN_FILTER_DUP_ENABLE);
e8907f76
LAD
2153}
2154
2155/* This function controls the passive scanning based on hdev->pend_le_conns
2156 * list. If there are pending LE connection we start the background scanning,
ad383c2c
LAD
2157 * otherwise we stop it in the following sequence:
2158 *
2159 * If there are devices to scan:
2160 *
2161 * Disable Scanning -> Update Accept List ->
2162 * use_ll_privacy((Disable Advertising) -> Disable Resolving List ->
2163 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) ->
2164 * Enable Scanning
2165 *
2166 * Otherwise:
2167 *
2168 * Disable Scanning
e8907f76
LAD
2169 */
2170int hci_update_passive_scan_sync(struct hci_dev *hdev)
2171{
2172 int err;
2173
2174 if (!test_bit(HCI_UP, &hdev->flags) ||
2175 test_bit(HCI_INIT, &hdev->flags) ||
2176 hci_dev_test_flag(hdev, HCI_SETUP) ||
2177 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2178 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2179 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2180 return 0;
2181
2182 /* No point in doing scanning if LE support hasn't been enabled */
2183 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2184 return 0;
2185
2186 /* If discovery is active don't interfere with it */
2187 if (hdev->discovery.state != DISCOVERY_STOPPED)
2188 return 0;
2189
2190 /* Reset RSSI and UUID filters when starting background scanning
2191 * since these filters are meant for service discovery only.
2192 *
2193 * The Start Discovery and Start Service Discovery operations
2194 * ensure to set proper values for RSSI threshold and UUID
2195 * filter list. So it is safe to just reset them here.
2196 */
2197 hci_discovery_filter_clear(hdev);
2198
2199 bt_dev_dbg(hdev, "ADV monitoring is %s",
2200 hci_is_adv_monitoring(hdev) ? "on" : "off");
2201
2202 if (list_empty(&hdev->pend_le_conns) &&
2203 list_empty(&hdev->pend_le_reports) &&
2204 !hci_is_adv_monitoring(hdev)) {
2205 /* If there is no pending LE connections or devices
2206 * to be scanned for or no ADV monitors, we should stop the
2207 * background scanning.
2208 */
2209
2210 bt_dev_dbg(hdev, "stopping background scanning");
2211
ad383c2c 2212 err = hci_scan_disable_sync(hdev);
e8907f76
LAD
2213 if (err)
2214 bt_dev_err(hdev, "stop background scanning failed: %d",
2215 err);
2216 } else {
2217 /* If there is at least one pending LE connection, we should
2218 * keep the background scan running.
2219 */
2220
2221 /* If controller is connecting, we should not start scanning
2222 * since some controllers are not able to scan and connect at
2223 * the same time.
2224 */
2225 if (hci_lookup_le_connect(hdev))
2226 return 0;
2227
e8907f76
LAD
2228 bt_dev_dbg(hdev, "start background scanning");
2229
2230 err = hci_passive_scan_sync(hdev);
2231 if (err)
2232 bt_dev_err(hdev, "start background scanning failed: %d",
2233 err);
2234 }
2235
2236 return err;
2237}
ad383c2c
LAD
2238
2239static int update_passive_scan_sync(struct hci_dev *hdev, void *data)
2240{
2241 return hci_update_passive_scan_sync(hdev);
2242}
2243
2244int hci_update_passive_scan(struct hci_dev *hdev)
2245{
5bee2fd6
LAD
2246 /* Only queue if it would have any effect */
2247 if (!test_bit(HCI_UP, &hdev->flags) ||
2248 test_bit(HCI_INIT, &hdev->flags) ||
2249 hci_dev_test_flag(hdev, HCI_SETUP) ||
2250 hci_dev_test_flag(hdev, HCI_CONFIG) ||
2251 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
2252 hci_dev_test_flag(hdev, HCI_UNREGISTER))
2253 return 0;
2254
ad383c2c
LAD
2255 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL);
2256}
cf75ad8b 2257
2f2eb0c9 2258int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val)
cf75ad8b 2259{
2f2eb0c9
BG
2260 int err;
2261
cf75ad8b
LAD
2262 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev))
2263 return 0;
2264
2f2eb0c9 2265 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
cf75ad8b 2266 sizeof(val), &val, HCI_CMD_TIMEOUT);
2f2eb0c9
BG
2267
2268 if (!err) {
2269 if (val) {
2270 hdev->features[1][0] |= LMP_HOST_SC;
2271 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
2272 } else {
2273 hdev->features[1][0] &= ~LMP_HOST_SC;
2274 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
2275 }
2276 }
2277
2278 return err;
cf75ad8b
LAD
2279}
2280
3244845c 2281int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode)
cf75ad8b
LAD
2282{
2283 int err;
2284
2285 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
2286 lmp_host_ssp_capable(hdev))
2287 return 0;
2288
3244845c
BG
2289 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
2290 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2291 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2292 }
2293
cf75ad8b
LAD
2294 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2295 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2296 if (err)
2297 return err;
2298
2299 return hci_write_sc_support_sync(hdev, 0x01);
2300}
2301
d81a494c 2302int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul)
cf75ad8b
LAD
2303{
2304 struct hci_cp_write_le_host_supported cp;
2305
2306 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
2307 !lmp_bredr_capable(hdev))
2308 return 0;
2309
2310 /* Check first if we already have the right host state
2311 * (host features set)
2312 */
2313 if (le == lmp_host_le_capable(hdev) &&
2314 simul == lmp_host_le_br_capable(hdev))
2315 return 0;
2316
2317 memset(&cp, 0, sizeof(cp));
2318
2319 cp.le = le;
2320 cp.simul = simul;
2321
2322 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
2323 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2324}
2325
2326static int hci_powered_update_adv_sync(struct hci_dev *hdev)
2327{
2328 struct adv_info *adv, *tmp;
2329 int err;
2330
2331 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2332 return 0;
2333
2334 /* If RPA Resolution has not been enable yet it means the
2335 * resolving list is empty and we should attempt to program the
2336 * local IRK in order to support using own_addr_type
2337 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03).
2338 */
2339 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
2340 hci_le_add_resolve_list_sync(hdev, NULL);
2341 hci_le_set_addr_resolution_enable_sync(hdev, 0x01);
2342 }
2343
2344 /* Make sure the controller has a good default for
2345 * advertising data. This also applies to the case
2346 * where BR/EDR was toggled during the AUTO_OFF phase.
2347 */
2348 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2349 list_empty(&hdev->adv_instances)) {
2350 if (ext_adv_capable(hdev)) {
2351 err = hci_setup_ext_adv_instance_sync(hdev, 0x00);
2352 if (!err)
2353 hci_update_scan_rsp_data_sync(hdev, 0x00);
2354 } else {
2355 err = hci_update_adv_data_sync(hdev, 0x00);
2356 if (!err)
2357 hci_update_scan_rsp_data_sync(hdev, 0x00);
2358 }
2359
2360 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
2361 hci_enable_advertising_sync(hdev);
2362 }
2363
2364 /* Call for each tracked instance to be scheduled */
2365 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list)
2366 hci_schedule_adv_instance_sync(hdev, adv->instance, true);
2367
2368 return 0;
2369}
2370
2371static int hci_write_auth_enable_sync(struct hci_dev *hdev)
2372{
2373 u8 link_sec;
2374
2375 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
2376 if (link_sec == test_bit(HCI_AUTH, &hdev->flags))
2377 return 0;
2378
2379 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE,
2380 sizeof(link_sec), &link_sec,
2381 HCI_CMD_TIMEOUT);
2382}
2383
353a0249 2384int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable)
cf75ad8b
LAD
2385{
2386 struct hci_cp_write_page_scan_activity cp;
2387 u8 type;
2388 int err = 0;
2389
2390 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2391 return 0;
2392
2393 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
2394 return 0;
2395
2396 memset(&cp, 0, sizeof(cp));
2397
2398 if (enable) {
2399 type = PAGE_SCAN_TYPE_INTERLACED;
2400
2401 /* 160 msec page scan interval */
2402 cp.interval = cpu_to_le16(0x0100);
2403 } else {
2404 type = hdev->def_page_scan_type;
2405 cp.interval = cpu_to_le16(hdev->def_page_scan_int);
2406 }
2407
2408 cp.window = cpu_to_le16(hdev->def_page_scan_window);
2409
2410 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval ||
2411 __cpu_to_le16(hdev->page_scan_window) != cp.window) {
2412 err = __hci_cmd_sync_status(hdev,
2413 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
2414 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2415 if (err)
2416 return err;
2417 }
2418
2419 if (hdev->page_scan_type != type)
2420 err = __hci_cmd_sync_status(hdev,
2421 HCI_OP_WRITE_PAGE_SCAN_TYPE,
2422 sizeof(type), &type,
2423 HCI_CMD_TIMEOUT);
2424
2425 return err;
2426}
2427
2428static bool disconnected_accept_list_entries(struct hci_dev *hdev)
2429{
2430 struct bdaddr_list *b;
2431
2432 list_for_each_entry(b, &hdev->accept_list, list) {
2433 struct hci_conn *conn;
2434
2435 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
2436 if (!conn)
2437 return true;
2438
2439 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2440 return true;
2441 }
2442
2443 return false;
2444}
2445
2446static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val)
2447{
2448 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE,
2449 sizeof(val), &val,
2450 HCI_CMD_TIMEOUT);
2451}
2452
451d95a9 2453int hci_update_scan_sync(struct hci_dev *hdev)
cf75ad8b
LAD
2454{
2455 u8 scan;
2456
2457 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
2458 return 0;
2459
2460 if (!hdev_is_powered(hdev))
2461 return 0;
2462
2463 if (mgmt_powering_down(hdev))
2464 return 0;
2465
2466 if (hdev->scanning_paused)
2467 return 0;
2468
2469 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
2470 disconnected_accept_list_entries(hdev))
2471 scan = SCAN_PAGE;
2472 else
2473 scan = SCAN_DISABLED;
2474
2475 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
2476 scan |= SCAN_INQUIRY;
2477
2478 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
2479 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
2480 return 0;
2481
2482 return hci_write_scan_enable_sync(hdev, scan);
2483}
2484
6f6ff38a 2485int hci_update_name_sync(struct hci_dev *hdev)
cf75ad8b
LAD
2486{
2487 struct hci_cp_write_local_name cp;
2488
2489 memset(&cp, 0, sizeof(cp));
2490
2491 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
2492
2493 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME,
2494 sizeof(cp), &cp,
2495 HCI_CMD_TIMEOUT);
2496}
2497
2498/* This function perform powered update HCI command sequence after the HCI init
2499 * sequence which end up resetting all states, the sequence is as follows:
2500 *
2501 * HCI_SSP_ENABLED(Enable SSP)
2502 * HCI_LE_ENABLED(Enable LE)
2503 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) ->
2504 * Update adv data)
2505 * Enable Authentication
2506 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class ->
2507 * Set Name -> Set EIR)
2508 */
2509int hci_powered_update_sync(struct hci_dev *hdev)
2510{
2511 int err;
2512
2513 /* Register the available SMP channels (BR/EDR and LE) only when
2514 * successfully powering on the controller. This late
2515 * registration is required so that LE SMP can clearly decide if
2516 * the public address or static address is used.
2517 */
2518 smp_register(hdev);
2519
2520 err = hci_write_ssp_mode_sync(hdev, 0x01);
2521 if (err)
2522 return err;
2523
2524 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00);
2525 if (err)
2526 return err;
2527
2528 err = hci_powered_update_adv_sync(hdev);
2529 if (err)
2530 return err;
2531
2532 err = hci_write_auth_enable_sync(hdev);
2533 if (err)
2534 return err;
2535
2536 if (lmp_bredr_capable(hdev)) {
2537 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
2538 hci_write_fast_connectable_sync(hdev, true);
2539 else
2540 hci_write_fast_connectable_sync(hdev, false);
2541 hci_update_scan_sync(hdev);
2542 hci_update_class_sync(hdev);
2543 hci_update_name_sync(hdev);
2544 hci_update_eir_sync(hdev);
2545 }
2546
2547 return 0;
2548}
2549
d0b13706
LAD
2550/**
2551 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
2552 * (BD_ADDR) for a HCI device from
2553 * a firmware node property.
2554 * @hdev: The HCI device
cf75ad8b 2555 *
d0b13706
LAD
2556 * Search the firmware node for 'local-bd-address'.
2557 *
2558 * All-zero BD addresses are rejected, because those could be properties
2559 * that exist in the firmware tables, but were not updated by the firmware. For
2560 * example, the DTS could define 'local-bd-address', with zero BD addresses.
cf75ad8b 2561 */
d0b13706 2562static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev)
cf75ad8b 2563{
d0b13706
LAD
2564 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent);
2565 bdaddr_t ba;
2566 int ret;
cf75ad8b 2567
d0b13706
LAD
2568 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address",
2569 (u8 *)&ba, sizeof(ba));
2570 if (ret < 0 || !bacmp(&ba, BDADDR_ANY))
2571 return;
cf75ad8b 2572
d0b13706
LAD
2573 bacpy(&hdev->public_addr, &ba);
2574}
cf75ad8b 2575
d0b13706
LAD
2576struct hci_init_stage {
2577 int (*func)(struct hci_dev *hdev);
2578};
cf75ad8b 2579
d0b13706
LAD
2580/* Run init stage NULL terminated function table */
2581static int hci_init_stage_sync(struct hci_dev *hdev,
2582 const struct hci_init_stage *stage)
2583{
2584 size_t i;
cf75ad8b 2585
d0b13706
LAD
2586 for (i = 0; stage[i].func; i++) {
2587 int err;
cf75ad8b 2588
d0b13706
LAD
2589 err = stage[i].func(hdev);
2590 if (err)
2591 return err;
cf75ad8b
LAD
2592 }
2593
2594 return 0;
2595}
2596
d0b13706
LAD
2597/* Read Local Version */
2598static int hci_read_local_version_sync(struct hci_dev *hdev)
cf75ad8b 2599{
d0b13706
LAD
2600 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION,
2601 0, NULL, HCI_CMD_TIMEOUT);
2602}
cf75ad8b 2603
d0b13706
LAD
2604/* Read BD Address */
2605static int hci_read_bd_addr_sync(struct hci_dev *hdev)
2606{
2607 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR,
2608 0, NULL, HCI_CMD_TIMEOUT);
2609}
cf75ad8b 2610
d0b13706
LAD
2611#define HCI_INIT(_func) \
2612{ \
2613 .func = _func, \
cf75ad8b
LAD
2614}
2615
d0b13706
LAD
2616static const struct hci_init_stage hci_init0[] = {
2617 /* HCI_OP_READ_LOCAL_VERSION */
2618 HCI_INIT(hci_read_local_version_sync),
2619 /* HCI_OP_READ_BD_ADDR */
2620 HCI_INIT(hci_read_bd_addr_sync),
2621 {}
2622};
2623
2624int hci_reset_sync(struct hci_dev *hdev)
cf75ad8b 2625{
cf75ad8b
LAD
2626 int err;
2627
d0b13706 2628 set_bit(HCI_RESET, &hdev->flags);
cf75ad8b 2629
d0b13706
LAD
2630 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL,
2631 HCI_CMD_TIMEOUT);
2632 if (err)
2633 return err;
cf75ad8b 2634
d0b13706
LAD
2635 return 0;
2636}
cf75ad8b 2637
d0b13706
LAD
2638static int hci_init0_sync(struct hci_dev *hdev)
2639{
2640 int err;
cf75ad8b 2641
d0b13706
LAD
2642 bt_dev_dbg(hdev, "");
2643
2644 /* Reset */
2645 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
2646 err = hci_reset_sync(hdev);
cf75ad8b
LAD
2647 if (err)
2648 return err;
2649 }
2650
d0b13706
LAD
2651 return hci_init_stage_sync(hdev, hci_init0);
2652}
abfeea47 2653
d0b13706
LAD
2654static int hci_unconf_init_sync(struct hci_dev *hdev)
2655{
2656 int err;
2657
2658 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
cf75ad8b
LAD
2659 return 0;
2660
d0b13706
LAD
2661 err = hci_init0_sync(hdev);
2662 if (err < 0)
2663 return err;
cf75ad8b 2664
d0b13706
LAD
2665 if (hci_dev_test_flag(hdev, HCI_SETUP))
2666 hci_debugfs_create_basic(hdev);
cf75ad8b
LAD
2667
2668 return 0;
2669}
2670
d0b13706
LAD
2671/* Read Local Supported Features. */
2672static int hci_read_local_features_sync(struct hci_dev *hdev)
cf75ad8b 2673{
d0b13706
LAD
2674 /* Not all AMP controllers support this command */
2675 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20))
2676 return 0;
cf75ad8b 2677
d0b13706
LAD
2678 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES,
2679 0, NULL, HCI_CMD_TIMEOUT);
cf75ad8b
LAD
2680}
2681
d0b13706
LAD
2682/* BR Controller init stage 1 command sequence */
2683static const struct hci_init_stage br_init1[] = {
2684 /* HCI_OP_READ_LOCAL_FEATURES */
2685 HCI_INIT(hci_read_local_features_sync),
2686 /* HCI_OP_READ_LOCAL_VERSION */
2687 HCI_INIT(hci_read_local_version_sync),
2688 /* HCI_OP_READ_BD_ADDR */
2689 HCI_INIT(hci_read_bd_addr_sync),
2690 {}
2691};
2692
2693/* Read Local Commands */
2694static int hci_read_local_cmds_sync(struct hci_dev *hdev)
cf75ad8b 2695{
d0b13706
LAD
2696 /* All Bluetooth 1.2 and later controllers should support the
2697 * HCI command for reading the local supported commands.
2698 *
2699 * Unfortunately some controllers indicate Bluetooth 1.2 support,
2700 * but do not have support for this command. If that is the case,
2701 * the driver can quirk the behavior and skip reading the local
2702 * supported commands.
2703 */
2704 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
2705 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
2706 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS,
2707 0, NULL, HCI_CMD_TIMEOUT);
cf75ad8b 2708
d0b13706 2709 return 0;
cf75ad8b
LAD
2710}
2711
d0b13706
LAD
2712/* Read Local AMP Info */
2713static int hci_read_local_amp_info_sync(struct hci_dev *hdev)
cf75ad8b 2714{
d0b13706
LAD
2715 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO,
2716 0, NULL, HCI_CMD_TIMEOUT);
cf75ad8b
LAD
2717}
2718
d0b13706
LAD
2719/* Read Data Blk size */
2720static int hci_read_data_block_size_sync(struct hci_dev *hdev)
cf75ad8b 2721{
d0b13706
LAD
2722 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE,
2723 0, NULL, HCI_CMD_TIMEOUT);
2724}
cf75ad8b 2725
d0b13706
LAD
2726/* Read Flow Control Mode */
2727static int hci_read_flow_control_mode_sync(struct hci_dev *hdev)
2728{
2729 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE,
2730 0, NULL, HCI_CMD_TIMEOUT);
2731}
cf75ad8b 2732
d0b13706
LAD
2733/* Read Location Data */
2734static int hci_read_location_data_sync(struct hci_dev *hdev)
2735{
2736 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA,
2737 0, NULL, HCI_CMD_TIMEOUT);
cf75ad8b
LAD
2738}
2739
d0b13706
LAD
2740/* AMP Controller init stage 1 command sequence */
2741static const struct hci_init_stage amp_init1[] = {
2742 /* HCI_OP_READ_LOCAL_VERSION */
2743 HCI_INIT(hci_read_local_version_sync),
2744 /* HCI_OP_READ_LOCAL_COMMANDS */
2745 HCI_INIT(hci_read_local_cmds_sync),
2746 /* HCI_OP_READ_LOCAL_AMP_INFO */
2747 HCI_INIT(hci_read_local_amp_info_sync),
2748 /* HCI_OP_READ_DATA_BLOCK_SIZE */
2749 HCI_INIT(hci_read_data_block_size_sync),
2750 /* HCI_OP_READ_FLOW_CONTROL_MODE */
2751 HCI_INIT(hci_read_flow_control_mode_sync),
2752 /* HCI_OP_READ_LOCATION_DATA */
2753 HCI_INIT(hci_read_location_data_sync),
2754};
2755
2756static int hci_init1_sync(struct hci_dev *hdev)
cf75ad8b 2757{
d0b13706 2758 int err;
cf75ad8b 2759
d0b13706 2760 bt_dev_dbg(hdev, "");
cf75ad8b 2761
d0b13706
LAD
2762 /* Reset */
2763 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
2764 err = hci_reset_sync(hdev);
2765 if (err)
2766 return err;
2767 }
cf75ad8b 2768
d0b13706
LAD
2769 switch (hdev->dev_type) {
2770 case HCI_PRIMARY:
2771 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
2772 return hci_init_stage_sync(hdev, br_init1);
2773 case HCI_AMP:
2774 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
2775 return hci_init_stage_sync(hdev, amp_init1);
2776 default:
2777 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
2778 break;
2779 }
2780
2781 return 0;
cf75ad8b
LAD
2782}
2783
d0b13706
LAD
2784/* AMP Controller init stage 2 command sequence */
2785static const struct hci_init_stage amp_init2[] = {
2786 /* HCI_OP_READ_LOCAL_FEATURES */
2787 HCI_INIT(hci_read_local_features_sync),
2788};
2789
2790/* Read Buffer Size (ACL mtu, max pkt, etc.) */
2791static int hci_read_buffer_size_sync(struct hci_dev *hdev)
cf75ad8b 2792{
d0b13706
LAD
2793 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE,
2794 0, NULL, HCI_CMD_TIMEOUT);
2795}
cf75ad8b 2796
d0b13706
LAD
2797/* Read Class of Device */
2798static int hci_read_dev_class_sync(struct hci_dev *hdev)
2799{
2800 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV,
2801 0, NULL, HCI_CMD_TIMEOUT);
2802}
cf75ad8b 2803
d0b13706
LAD
2804/* Read Local Name */
2805static int hci_read_local_name_sync(struct hci_dev *hdev)
2806{
2807 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME,
2808 0, NULL, HCI_CMD_TIMEOUT);
2809}
cf75ad8b 2810
d0b13706
LAD
2811/* Read Voice Setting */
2812static int hci_read_voice_setting_sync(struct hci_dev *hdev)
2813{
2814 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING,
2815 0, NULL, HCI_CMD_TIMEOUT);
cf75ad8b
LAD
2816}
2817
d0b13706
LAD
2818/* Read Number of Supported IAC */
2819static int hci_read_num_supported_iac_sync(struct hci_dev *hdev)
cf75ad8b 2820{
d0b13706
LAD
2821 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC,
2822 0, NULL, HCI_CMD_TIMEOUT);
2823}
cf75ad8b 2824
d0b13706
LAD
2825/* Read Current IAC LAP */
2826static int hci_read_current_iac_lap_sync(struct hci_dev *hdev)
2827{
2828 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP,
2829 0, NULL, HCI_CMD_TIMEOUT);
cf75ad8b
LAD
2830}
2831
d0b13706
LAD
2832static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type,
2833 u8 cond_type, bdaddr_t *bdaddr,
2834 u8 auto_accept)
cf75ad8b 2835{
d0b13706 2836 struct hci_cp_set_event_filter cp;
cf75ad8b 2837
d0b13706 2838 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
cf75ad8b
LAD
2839 return 0;
2840
0eaecfb2
IFM
2841 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
2842 return 0;
2843
d0b13706
LAD
2844 memset(&cp, 0, sizeof(cp));
2845 cp.flt_type = flt_type;
cf75ad8b 2846
d0b13706
LAD
2847 if (flt_type != HCI_FLT_CLEAR_ALL) {
2848 cp.cond_type = cond_type;
2849 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr);
2850 cp.addr_conn_flt.auto_accept = auto_accept;
cf75ad8b
LAD
2851 }
2852
d0b13706
LAD
2853 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT,
2854 flt_type == HCI_FLT_CLEAR_ALL ?
2855 sizeof(cp.flt_type) : sizeof(cp), &cp,
2856 HCI_CMD_TIMEOUT);
cf75ad8b
LAD
2857}
2858
d0b13706 2859static int hci_clear_event_filter_sync(struct hci_dev *hdev)
cf75ad8b 2860{
d0b13706
LAD
2861 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED))
2862 return 0;
2863
0eaecfb2
IFM
2864 /* In theory the state machine should not reach here unless
2865 * a hci_set_event_filter_sync() call succeeds, but we do
2866 * the check both for parity and as a future reminder.
2867 */
2868 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
2869 return 0;
2870
d0b13706
LAD
2871 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00,
2872 BDADDR_ANY, 0x00);
2873}
2874
2875/* Connection accept timeout ~20 secs */
2876static int hci_write_ca_timeout_sync(struct hci_dev *hdev)
2877{
2878 __le16 param = cpu_to_le16(0x7d00);
2879
2880 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT,
2881 sizeof(param), &param, HCI_CMD_TIMEOUT);
2882}
2883
2884/* BR Controller init stage 2 command sequence */
2885static const struct hci_init_stage br_init2[] = {
2886 /* HCI_OP_READ_BUFFER_SIZE */
2887 HCI_INIT(hci_read_buffer_size_sync),
2888 /* HCI_OP_READ_CLASS_OF_DEV */
2889 HCI_INIT(hci_read_dev_class_sync),
2890 /* HCI_OP_READ_LOCAL_NAME */
2891 HCI_INIT(hci_read_local_name_sync),
2892 /* HCI_OP_READ_VOICE_SETTING */
2893 HCI_INIT(hci_read_voice_setting_sync),
2894 /* HCI_OP_READ_NUM_SUPPORTED_IAC */
2895 HCI_INIT(hci_read_num_supported_iac_sync),
2896 /* HCI_OP_READ_CURRENT_IAC_LAP */
2897 HCI_INIT(hci_read_current_iac_lap_sync),
2898 /* HCI_OP_SET_EVENT_FLT */
2899 HCI_INIT(hci_clear_event_filter_sync),
2900 /* HCI_OP_WRITE_CA_TIMEOUT */
2901 HCI_INIT(hci_write_ca_timeout_sync),
2902 {}
2903};
2904
2905static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev)
2906{
2907 u8 mode = 0x01;
2908
2909 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2910 return 0;
2911
2912 /* When SSP is available, then the host features page
2913 * should also be available as well. However some
2914 * controllers list the max_page as 0 as long as SSP
2915 * has not been enabled. To achieve proper debugging
2916 * output, force the minimum max_page to 1 at least.
2917 */
2918 hdev->max_page = 0x01;
2919
2920 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE,
2921 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2922}
2923
2924static int hci_write_eir_sync(struct hci_dev *hdev)
2925{
2926 struct hci_cp_write_eir cp;
2927
2928 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2929 return 0;
2930
2931 memset(hdev->eir, 0, sizeof(hdev->eir));
2932 memset(&cp, 0, sizeof(cp));
2933
2934 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp,
2935 HCI_CMD_TIMEOUT);
2936}
2937
2938static int hci_write_inquiry_mode_sync(struct hci_dev *hdev)
2939{
2940 u8 mode;
2941
2942 if (!lmp_inq_rssi_capable(hdev) &&
2943 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
2944 return 0;
2945
2946 /* If Extended Inquiry Result events are supported, then
2947 * they are clearly preferred over Inquiry Result with RSSI
2948 * events.
2949 */
2950 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
2951
2952 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE,
2953 sizeof(mode), &mode, HCI_CMD_TIMEOUT);
2954}
2955
2956static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev)
2957{
2958 if (!lmp_inq_tx_pwr_capable(hdev))
2959 return 0;
2960
2961 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
2962 0, NULL, HCI_CMD_TIMEOUT);
2963}
2964
2965static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page)
2966{
2967 struct hci_cp_read_local_ext_features cp;
2968
2969 if (!lmp_ext_feat_capable(hdev))
2970 return 0;
2971
2972 memset(&cp, 0, sizeof(cp));
2973 cp.page = page;
2974
2975 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES,
2976 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
2977}
2978
2979static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev)
2980{
2981 return hci_read_local_ext_features_sync(hdev, 0x01);
2982}
2983
2984/* HCI Controller init stage 2 command sequence */
2985static const struct hci_init_stage hci_init2[] = {
2986 /* HCI_OP_READ_LOCAL_COMMANDS */
2987 HCI_INIT(hci_read_local_cmds_sync),
2988 /* HCI_OP_WRITE_SSP_MODE */
2989 HCI_INIT(hci_write_ssp_mode_1_sync),
2990 /* HCI_OP_WRITE_EIR */
2991 HCI_INIT(hci_write_eir_sync),
2992 /* HCI_OP_WRITE_INQUIRY_MODE */
2993 HCI_INIT(hci_write_inquiry_mode_sync),
2994 /* HCI_OP_READ_INQ_RSP_TX_POWER */
2995 HCI_INIT(hci_read_inq_rsp_tx_power_sync),
2996 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
2997 HCI_INIT(hci_read_local_ext_features_1_sync),
2998 /* HCI_OP_WRITE_AUTH_ENABLE */
2999 HCI_INIT(hci_write_auth_enable_sync),
3000 {}
3001};
3002
3003/* Read LE Buffer Size */
3004static int hci_le_read_buffer_size_sync(struct hci_dev *hdev)
3005{
3006 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE,
3007 0, NULL, HCI_CMD_TIMEOUT);
3008}
3009
3010/* Read LE Local Supported Features */
3011static int hci_le_read_local_features_sync(struct hci_dev *hdev)
3012{
3013 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES,
3014 0, NULL, HCI_CMD_TIMEOUT);
3015}
3016
3017/* Read LE Supported States */
3018static int hci_le_read_supported_states_sync(struct hci_dev *hdev)
3019{
3020 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES,
3021 0, NULL, HCI_CMD_TIMEOUT);
3022}
3023
3024/* LE Controller init stage 2 command sequence */
3025static const struct hci_init_stage le_init2[] = {
3026 /* HCI_OP_LE_READ_BUFFER_SIZE */
3027 HCI_INIT(hci_le_read_buffer_size_sync),
3028 /* HCI_OP_LE_READ_LOCAL_FEATURES */
3029 HCI_INIT(hci_le_read_local_features_sync),
3030 /* HCI_OP_LE_READ_SUPPORTED_STATES */
3031 HCI_INIT(hci_le_read_supported_states_sync),
3032 {}
3033};
3034
3035static int hci_init2_sync(struct hci_dev *hdev)
3036{
3037 int err;
3038
3039 bt_dev_dbg(hdev, "");
3040
3041 if (hdev->dev_type == HCI_AMP)
3042 return hci_init_stage_sync(hdev, amp_init2);
3043
3044 if (lmp_bredr_capable(hdev)) {
3045 err = hci_init_stage_sync(hdev, br_init2);
3046 if (err)
3047 return err;
3048 } else {
3049 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
3050 }
3051
3052 if (lmp_le_capable(hdev)) {
3053 err = hci_init_stage_sync(hdev, le_init2);
3054 if (err)
3055 return err;
3056 /* LE-only controllers have LE implicitly enabled */
3057 if (!lmp_bredr_capable(hdev))
3058 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
3059 }
3060
3061 return hci_init_stage_sync(hdev, hci_init2);
3062}
3063
3064static int hci_set_event_mask_sync(struct hci_dev *hdev)
3065{
3066 /* The second byte is 0xff instead of 0x9f (two reserved bits
3067 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
3068 * command otherwise.
3069 */
3070 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
3071
3072 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
3073 * any event mask for pre 1.2 devices.
3074 */
3075 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
3076 return 0;
3077
3078 if (lmp_bredr_capable(hdev)) {
3079 events[4] |= 0x01; /* Flow Specification Complete */
182ee45d
LAD
3080
3081 /* Don't set Disconnect Complete when suspended as that
3082 * would wakeup the host when disconnecting due to
3083 * suspend.
3084 */
3085 if (hdev->suspended)
3086 events[0] &= 0xef;
d0b13706
LAD
3087 } else {
3088 /* Use a different default for LE-only devices */
3089 memset(events, 0, sizeof(events));
3090 events[1] |= 0x20; /* Command Complete */
3091 events[1] |= 0x40; /* Command Status */
3092 events[1] |= 0x80; /* Hardware Error */
3093
3094 /* If the controller supports the Disconnect command, enable
3095 * the corresponding event. In addition enable packet flow
3096 * control related events.
3097 */
3098 if (hdev->commands[0] & 0x20) {
182ee45d
LAD
3099 /* Don't set Disconnect Complete when suspended as that
3100 * would wakeup the host when disconnecting due to
3101 * suspend.
3102 */
3103 if (!hdev->suspended)
3104 events[0] |= 0x10; /* Disconnection Complete */
d0b13706
LAD
3105 events[2] |= 0x04; /* Number of Completed Packets */
3106 events[3] |= 0x02; /* Data Buffer Overflow */
3107 }
3108
3109 /* If the controller supports the Read Remote Version
3110 * Information command, enable the corresponding event.
3111 */
3112 if (hdev->commands[2] & 0x80)
3113 events[1] |= 0x08; /* Read Remote Version Information
3114 * Complete
3115 */
3116
3117 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
3118 events[0] |= 0x80; /* Encryption Change */
3119 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3120 }
3121 }
3122
3123 if (lmp_inq_rssi_capable(hdev) ||
3124 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
3125 events[4] |= 0x02; /* Inquiry Result with RSSI */
3126
3127 if (lmp_ext_feat_capable(hdev))
3128 events[4] |= 0x04; /* Read Remote Extended Features Complete */
3129
3130 if (lmp_esco_capable(hdev)) {
3131 events[5] |= 0x08; /* Synchronous Connection Complete */
3132 events[5] |= 0x10; /* Synchronous Connection Changed */
3133 }
3134
3135 if (lmp_sniffsubr_capable(hdev))
3136 events[5] |= 0x20; /* Sniff Subrating */
3137
3138 if (lmp_pause_enc_capable(hdev))
3139 events[5] |= 0x80; /* Encryption Key Refresh Complete */
3140
3141 if (lmp_ext_inq_capable(hdev))
3142 events[5] |= 0x40; /* Extended Inquiry Result */
3143
3144 if (lmp_no_flush_capable(hdev))
3145 events[7] |= 0x01; /* Enhanced Flush Complete */
3146
3147 if (lmp_lsto_capable(hdev))
3148 events[6] |= 0x80; /* Link Supervision Timeout Changed */
3149
3150 if (lmp_ssp_capable(hdev)) {
3151 events[6] |= 0x01; /* IO Capability Request */
3152 events[6] |= 0x02; /* IO Capability Response */
3153 events[6] |= 0x04; /* User Confirmation Request */
3154 events[6] |= 0x08; /* User Passkey Request */
3155 events[6] |= 0x10; /* Remote OOB Data Request */
3156 events[6] |= 0x20; /* Simple Pairing Complete */
3157 events[7] |= 0x04; /* User Passkey Notification */
3158 events[7] |= 0x08; /* Keypress Notification */
3159 events[7] |= 0x10; /* Remote Host Supported
3160 * Features Notification
3161 */
3162 }
3163
3164 if (lmp_le_capable(hdev))
3165 events[7] |= 0x20; /* LE Meta-Event */
3166
3167 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK,
3168 sizeof(events), events, HCI_CMD_TIMEOUT);
3169}
3170
3171static int hci_read_stored_link_key_sync(struct hci_dev *hdev)
3172{
3173 struct hci_cp_read_stored_link_key cp;
3174
3175 if (!(hdev->commands[6] & 0x20) ||
3176 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3177 return 0;
3178
3179 memset(&cp, 0, sizeof(cp));
3180 bacpy(&cp.bdaddr, BDADDR_ANY);
3181 cp.read_all = 0x01;
3182
3183 return __hci_cmd_sync_status(hdev, HCI_OP_READ_STORED_LINK_KEY,
3184 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3185}
3186
3187static int hci_setup_link_policy_sync(struct hci_dev *hdev)
3188{
3189 struct hci_cp_write_def_link_policy cp;
3190 u16 link_policy = 0;
3191
3192 if (!(hdev->commands[5] & 0x10))
3193 return 0;
3194
3195 memset(&cp, 0, sizeof(cp));
3196
3197 if (lmp_rswitch_capable(hdev))
3198 link_policy |= HCI_LP_RSWITCH;
3199 if (lmp_hold_capable(hdev))
3200 link_policy |= HCI_LP_HOLD;
3201 if (lmp_sniff_capable(hdev))
3202 link_policy |= HCI_LP_SNIFF;
3203 if (lmp_park_capable(hdev))
3204 link_policy |= HCI_LP_PARK;
3205
3206 cp.policy = cpu_to_le16(link_policy);
3207
3208 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
3209 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3210}
3211
3212static int hci_read_page_scan_activity_sync(struct hci_dev *hdev)
3213{
3214 if (!(hdev->commands[8] & 0x01))
3215 return 0;
3216
3217 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
3218 0, NULL, HCI_CMD_TIMEOUT);
3219}
3220
3221static int hci_read_def_err_data_reporting_sync(struct hci_dev *hdev)
3222{
3223 if (!(hdev->commands[18] & 0x04) ||
766ae242 3224 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING))
d0b13706
LAD
3225 return 0;
3226
3227 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
3228 0, NULL, HCI_CMD_TIMEOUT);
3229}
3230
3231static int hci_read_page_scan_type_sync(struct hci_dev *hdev)
3232{
3233 /* Some older Broadcom based Bluetooth 1.2 controllers do not
3234 * support the Read Page Scan Type command. Check support for
3235 * this command in the bit mask of supported commands.
3236 */
3237 if (!(hdev->commands[13] & 0x01))
3238 return 0;
3239
3240 return __hci_cmd_sync_status(hdev, HCI_OP_READ_PAGE_SCAN_TYPE,
3241 0, NULL, HCI_CMD_TIMEOUT);
3242}
3243
3244/* Read features beyond page 1 if available */
3245static int hci_read_local_ext_features_all_sync(struct hci_dev *hdev)
3246{
3247 u8 page;
3248 int err;
3249
3250 if (!lmp_ext_feat_capable(hdev))
3251 return 0;
3252
3253 for (page = 2; page < HCI_MAX_PAGES && page <= hdev->max_page;
3254 page++) {
3255 err = hci_read_local_ext_features_sync(hdev, page);
3256 if (err)
3257 return err;
3258 }
3259
3260 return 0;
3261}
3262
3263/* HCI Controller init stage 3 command sequence */
3264static const struct hci_init_stage hci_init3[] = {
3265 /* HCI_OP_SET_EVENT_MASK */
3266 HCI_INIT(hci_set_event_mask_sync),
3267 /* HCI_OP_READ_STORED_LINK_KEY */
3268 HCI_INIT(hci_read_stored_link_key_sync),
3269 /* HCI_OP_WRITE_DEF_LINK_POLICY */
3270 HCI_INIT(hci_setup_link_policy_sync),
3271 /* HCI_OP_READ_PAGE_SCAN_ACTIVITY */
3272 HCI_INIT(hci_read_page_scan_activity_sync),
3273 /* HCI_OP_READ_DEF_ERR_DATA_REPORTING */
3274 HCI_INIT(hci_read_def_err_data_reporting_sync),
3275 /* HCI_OP_READ_PAGE_SCAN_TYPE */
3276 HCI_INIT(hci_read_page_scan_type_sync),
3277 /* HCI_OP_READ_LOCAL_EXT_FEATURES */
3278 HCI_INIT(hci_read_local_ext_features_all_sync),
3279 {}
3280};
3281
3282static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
3283{
3284 u8 events[8];
3285
3286 if (!lmp_le_capable(hdev))
3287 return 0;
3288
3289 memset(events, 0, sizeof(events));
3290
3291 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
3292 events[0] |= 0x10; /* LE Long Term Key Request */
3293
3294 /* If controller supports the Connection Parameters Request
3295 * Link Layer Procedure, enable the corresponding event.
3296 */
3297 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
3298 /* LE Remote Connection Parameter Request */
3299 events[0] |= 0x20;
3300
3301 /* If the controller supports the Data Length Extension
3302 * feature, enable the corresponding event.
3303 */
3304 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
3305 events[0] |= 0x40; /* LE Data Length Change */
3306
a56a1138
LAD
3307 /* If the controller supports LL Privacy feature or LE Extended Adv,
3308 * enable the corresponding event.
d0b13706 3309 */
a56a1138 3310 if (use_enhanced_conn_complete(hdev))
d0b13706
LAD
3311 events[1] |= 0x02; /* LE Enhanced Connection Complete */
3312
3313 /* If the controller supports Extended Scanner Filter
3314 * Policies, enable the corresponding event.
3315 */
3316 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
3317 events[1] |= 0x04; /* LE Direct Advertising Report */
3318
3319 /* If the controller supports Channel Selection Algorithm #2
3320 * feature, enable the corresponding event.
3321 */
3322 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
3323 events[2] |= 0x08; /* LE Channel Selection Algorithm */
3324
3325 /* If the controller supports the LE Set Scan Enable command,
3326 * enable the corresponding advertising report event.
3327 */
3328 if (hdev->commands[26] & 0x08)
3329 events[0] |= 0x02; /* LE Advertising Report */
3330
3331 /* If the controller supports the LE Create Connection
3332 * command, enable the corresponding event.
3333 */
3334 if (hdev->commands[26] & 0x10)
3335 events[0] |= 0x01; /* LE Connection Complete */
3336
3337 /* If the controller supports the LE Connection Update
3338 * command, enable the corresponding event.
3339 */
3340 if (hdev->commands[27] & 0x04)
3341 events[0] |= 0x04; /* LE Connection Update Complete */
3342
3343 /* If the controller supports the LE Read Remote Used Features
3344 * command, enable the corresponding event.
3345 */
3346 if (hdev->commands[27] & 0x20)
3347 /* LE Read Remote Used Features Complete */
3348 events[0] |= 0x08;
3349
3350 /* If the controller supports the LE Read Local P-256
3351 * Public Key command, enable the corresponding event.
3352 */
3353 if (hdev->commands[34] & 0x02)
3354 /* LE Read Local P-256 Public Key Complete */
3355 events[0] |= 0x80;
3356
3357 /* If the controller supports the LE Generate DHKey
3358 * command, enable the corresponding event.
3359 */
3360 if (hdev->commands[34] & 0x04)
3361 events[1] |= 0x01; /* LE Generate DHKey Complete */
3362
3363 /* If the controller supports the LE Set Default PHY or
3364 * LE Set PHY commands, enable the corresponding event.
3365 */
3366 if (hdev->commands[35] & (0x20 | 0x40))
3367 events[1] |= 0x08; /* LE PHY Update Complete */
3368
3369 /* If the controller supports LE Set Extended Scan Parameters
3370 * and LE Set Extended Scan Enable commands, enable the
3371 * corresponding event.
3372 */
3373 if (use_ext_scan(hdev))
3374 events[1] |= 0x10; /* LE Extended Advertising Report */
3375
3376 /* If the controller supports the LE Extended Advertising
3377 * command, enable the corresponding event.
3378 */
3379 if (ext_adv_capable(hdev))
3380 events[2] |= 0x02; /* LE Advertising Set Terminated */
3381
3382 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EVENT_MASK,
3383 sizeof(events), events, HCI_CMD_TIMEOUT);
3384}
3385
3386/* Read LE Advertising Channel TX Power */
3387static int hci_le_read_adv_tx_power_sync(struct hci_dev *hdev)
3388{
3389 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
3390 /* HCI TS spec forbids mixing of legacy and extended
3391 * advertising commands wherein READ_ADV_TX_POWER is
3392 * also included. So do not call it if extended adv
3393 * is supported otherwise controller will return
3394 * COMMAND_DISALLOWED for extended commands.
3395 */
3396 return __hci_cmd_sync_status(hdev,
3397 HCI_OP_LE_READ_ADV_TX_POWER,
3398 0, NULL, HCI_CMD_TIMEOUT);
3399 }
3400
3401 return 0;
3402}
3403
3404/* Read LE Min/Max Tx Power*/
3405static int hci_le_read_tx_power_sync(struct hci_dev *hdev)
3406{
d2f8114f
AG
3407 if (!(hdev->commands[38] & 0x80) ||
3408 test_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks))
d0b13706
LAD
3409 return 0;
3410
3411 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_TRANSMIT_POWER,
3412 0, NULL, HCI_CMD_TIMEOUT);
3413}
3414
3415/* Read LE Accept List Size */
3416static int hci_le_read_accept_list_size_sync(struct hci_dev *hdev)
3417{
3418 if (!(hdev->commands[26] & 0x40))
3419 return 0;
3420
3421 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
3422 0, NULL, HCI_CMD_TIMEOUT);
3423}
3424
3425/* Clear LE Accept List */
3426static int hci_le_clear_accept_list_sync(struct hci_dev *hdev)
3427{
3428 if (!(hdev->commands[26] & 0x80))
3429 return 0;
3430
3431 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, 0, NULL,
3432 HCI_CMD_TIMEOUT);
3433}
3434
3435/* Read LE Resolving List Size */
3436static int hci_le_read_resolv_list_size_sync(struct hci_dev *hdev)
3437{
3438 if (!(hdev->commands[34] & 0x40))
3439 return 0;
3440
3441 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
3442 0, NULL, HCI_CMD_TIMEOUT);
3443}
3444
3445/* Clear LE Resolving List */
3446static int hci_le_clear_resolv_list_sync(struct hci_dev *hdev)
3447{
3448 if (!(hdev->commands[34] & 0x20))
3449 return 0;
3450
3451 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL,
3452 HCI_CMD_TIMEOUT);
3453}
3454
3455/* Set RPA timeout */
3456static int hci_le_set_rpa_timeout_sync(struct hci_dev *hdev)
3457{
3458 __le16 timeout = cpu_to_le16(hdev->rpa_timeout);
3459
3460 if (!(hdev->commands[35] & 0x04))
3461 return 0;
3462
3463 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RPA_TIMEOUT,
3464 sizeof(timeout), &timeout,
3465 HCI_CMD_TIMEOUT);
3466}
3467
3468/* Read LE Maximum Data Length */
3469static int hci_le_read_max_data_len_sync(struct hci_dev *hdev)
3470{
3471 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3472 return 0;
3473
3474 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL,
3475 HCI_CMD_TIMEOUT);
3476}
3477
3478/* Read LE Suggested Default Data Length */
3479static int hci_le_read_def_data_len_sync(struct hci_dev *hdev)
3480{
3481 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3482 return 0;
3483
3484 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL,
3485 HCI_CMD_TIMEOUT);
3486}
3487
3488/* Read LE Number of Supported Advertising Sets */
3489static int hci_le_read_num_support_adv_sets_sync(struct hci_dev *hdev)
3490{
3491 if (!ext_adv_capable(hdev))
3492 return 0;
3493
3494 return __hci_cmd_sync_status(hdev,
3495 HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
3496 0, NULL, HCI_CMD_TIMEOUT);
3497}
3498
3499/* Write LE Host Supported */
3500static int hci_set_le_support_sync(struct hci_dev *hdev)
3501{
3502 struct hci_cp_write_le_host_supported cp;
3503
3504 /* LE-only devices do not support explicit enablement */
3505 if (!lmp_bredr_capable(hdev))
3506 return 0;
3507
3508 memset(&cp, 0, sizeof(cp));
3509
3510 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
3511 cp.le = 0x01;
3512 cp.simul = 0x00;
3513 }
3514
3515 if (cp.le == lmp_host_le_capable(hdev))
3516 return 0;
3517
3518 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED,
3519 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3520}
3521
3522/* LE Controller init stage 3 command sequence */
3523static const struct hci_init_stage le_init3[] = {
3524 /* HCI_OP_LE_SET_EVENT_MASK */
3525 HCI_INIT(hci_le_set_event_mask_sync),
3526 /* HCI_OP_LE_READ_ADV_TX_POWER */
3527 HCI_INIT(hci_le_read_adv_tx_power_sync),
3528 /* HCI_OP_LE_READ_TRANSMIT_POWER */
3529 HCI_INIT(hci_le_read_tx_power_sync),
3530 /* HCI_OP_LE_READ_ACCEPT_LIST_SIZE */
3531 HCI_INIT(hci_le_read_accept_list_size_sync),
3532 /* HCI_OP_LE_CLEAR_ACCEPT_LIST */
3533 HCI_INIT(hci_le_clear_accept_list_sync),
3534 /* HCI_OP_LE_READ_RESOLV_LIST_SIZE */
3535 HCI_INIT(hci_le_read_resolv_list_size_sync),
3536 /* HCI_OP_LE_CLEAR_RESOLV_LIST */
3537 HCI_INIT(hci_le_clear_resolv_list_sync),
3538 /* HCI_OP_LE_SET_RPA_TIMEOUT */
3539 HCI_INIT(hci_le_set_rpa_timeout_sync),
3540 /* HCI_OP_LE_READ_MAX_DATA_LEN */
3541 HCI_INIT(hci_le_read_max_data_len_sync),
3542 /* HCI_OP_LE_READ_DEF_DATA_LEN */
3543 HCI_INIT(hci_le_read_def_data_len_sync),
3544 /* HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS */
3545 HCI_INIT(hci_le_read_num_support_adv_sets_sync),
3546 /* HCI_OP_WRITE_LE_HOST_SUPPORTED */
3547 HCI_INIT(hci_set_le_support_sync),
3548 {}
3549};
3550
3551static int hci_init3_sync(struct hci_dev *hdev)
3552{
3553 int err;
3554
3555 bt_dev_dbg(hdev, "");
3556
3557 err = hci_init_stage_sync(hdev, hci_init3);
3558 if (err)
3559 return err;
3560
3561 if (lmp_le_capable(hdev))
3562 return hci_init_stage_sync(hdev, le_init3);
3563
3564 return 0;
3565}
3566
3567static int hci_delete_stored_link_key_sync(struct hci_dev *hdev)
3568{
3569 struct hci_cp_delete_stored_link_key cp;
3570
3571 /* Some Broadcom based Bluetooth controllers do not support the
3572 * Delete Stored Link Key command. They are clearly indicating its
3573 * absence in the bit mask of supported commands.
3574 *
3575 * Check the supported commands and only if the command is marked
3576 * as supported send it. If not supported assume that the controller
3577 * does not have actual support for stored link keys which makes this
3578 * command redundant anyway.
3579 *
3580 * Some controllers indicate that they support handling deleting
3581 * stored link keys, but they don't. The quirk lets a driver
3582 * just disable this command.
3583 */
3584 if (!(hdev->commands[6] & 0x80) ||
3585 test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks))
3586 return 0;
3587
3588 memset(&cp, 0, sizeof(cp));
3589 bacpy(&cp.bdaddr, BDADDR_ANY);
3590 cp.delete_all = 0x01;
3591
3592 return __hci_cmd_sync_status(hdev, HCI_OP_DELETE_STORED_LINK_KEY,
3593 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3594}
3595
3596static int hci_set_event_mask_page_2_sync(struct hci_dev *hdev)
3597{
3598 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3599 bool changed = false;
3600
3601 /* Set event mask page 2 if the HCI command for it is supported */
3602 if (!(hdev->commands[22] & 0x04))
3603 return 0;
3604
3605 /* If Connectionless Peripheral Broadcast central role is supported
3606 * enable all necessary events for it.
3607 */
3608 if (lmp_cpb_central_capable(hdev)) {
3609 events[1] |= 0x40; /* Triggered Clock Capture */
3610 events[1] |= 0x80; /* Synchronization Train Complete */
0feb8af0 3611 events[2] |= 0x08; /* Truncated Page Complete */
d0b13706
LAD
3612 events[2] |= 0x20; /* CPB Channel Map Change */
3613 changed = true;
3614 }
3615
3616 /* If Connectionless Peripheral Broadcast peripheral role is supported
3617 * enable all necessary events for it.
3618 */
3619 if (lmp_cpb_peripheral_capable(hdev)) {
3620 events[2] |= 0x01; /* Synchronization Train Received */
3621 events[2] |= 0x02; /* CPB Receive */
3622 events[2] |= 0x04; /* CPB Timeout */
0feb8af0 3623 events[2] |= 0x10; /* Peripheral Page Response Timeout */
d0b13706
LAD
3624 changed = true;
3625 }
3626
3627 /* Enable Authenticated Payload Timeout Expired event if supported */
3628 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
3629 events[2] |= 0x80;
3630 changed = true;
3631 }
3632
3633 /* Some Broadcom based controllers indicate support for Set Event
3634 * Mask Page 2 command, but then actually do not support it. Since
3635 * the default value is all bits set to zero, the command is only
3636 * required if the event mask has to be changed. In case no change
3637 * to the event mask is needed, skip this command.
3638 */
3639 if (!changed)
3640 return 0;
3641
3642 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_MASK_PAGE_2,
3643 sizeof(events), events, HCI_CMD_TIMEOUT);
3644}
3645
3646/* Read local codec list if the HCI command is supported */
3647static int hci_read_local_codecs_sync(struct hci_dev *hdev)
3648{
3649 if (!(hdev->commands[29] & 0x20))
3650 return 0;
3651
3652 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_CODECS, 0, NULL,
3653 HCI_CMD_TIMEOUT);
3654}
3655
3656/* Read local pairing options if the HCI command is supported */
3657static int hci_read_local_pairing_opts_sync(struct hci_dev *hdev)
3658{
3659 if (!(hdev->commands[41] & 0x08))
3660 return 0;
3661
3662 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_PAIRING_OPTS,
3663 0, NULL, HCI_CMD_TIMEOUT);
3664}
3665
3666/* Get MWS transport configuration if the HCI command is supported */
3667static int hci_get_mws_transport_config_sync(struct hci_dev *hdev)
3668{
3669 if (!(hdev->commands[30] & 0x08))
3670 return 0;
3671
3672 return __hci_cmd_sync_status(hdev, HCI_OP_GET_MWS_TRANSPORT_CONFIG,
3673 0, NULL, HCI_CMD_TIMEOUT);
3674}
3675
3676/* Check for Synchronization Train support */
3677static int hci_read_sync_train_params_sync(struct hci_dev *hdev)
3678{
3679 if (!lmp_sync_train_capable(hdev))
3680 return 0;
3681
3682 return __hci_cmd_sync_status(hdev, HCI_OP_READ_SYNC_TRAIN_PARAMS,
3683 0, NULL, HCI_CMD_TIMEOUT);
3684}
3685
3686/* Enable Secure Connections if supported and configured */
3687static int hci_write_sc_support_1_sync(struct hci_dev *hdev)
3688{
3689 u8 support = 0x01;
3690
3691 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) ||
3692 !bredr_sc_enabled(hdev))
3693 return 0;
3694
3695 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT,
3696 sizeof(support), &support,
3697 HCI_CMD_TIMEOUT);
3698}
3699
3700/* Set erroneous data reporting if supported to the wideband speech
3701 * setting value
3702 */
3703static int hci_set_err_data_report_sync(struct hci_dev *hdev)
3704{
3705 struct hci_cp_write_def_err_data_reporting cp;
3706 bool enabled = hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED);
3707
3708 if (!(hdev->commands[18] & 0x08) ||
766ae242 3709 !(hdev->features[0][6] & LMP_ERR_DATA_REPORTING))
d0b13706
LAD
3710 return 0;
3711
3712 if (enabled == hdev->err_data_reporting)
3713 return 0;
3714
3715 memset(&cp, 0, sizeof(cp));
3716 cp.err_data_reporting = enabled ? ERR_DATA_REPORTING_ENABLED :
3717 ERR_DATA_REPORTING_DISABLED;
3718
3719 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
3720 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3721}
3722
3723static const struct hci_init_stage hci_init4[] = {
3724 /* HCI_OP_DELETE_STORED_LINK_KEY */
3725 HCI_INIT(hci_delete_stored_link_key_sync),
3726 /* HCI_OP_SET_EVENT_MASK_PAGE_2 */
3727 HCI_INIT(hci_set_event_mask_page_2_sync),
3728 /* HCI_OP_READ_LOCAL_CODECS */
3729 HCI_INIT(hci_read_local_codecs_sync),
3730 /* HCI_OP_READ_LOCAL_PAIRING_OPTS */
3731 HCI_INIT(hci_read_local_pairing_opts_sync),
3732 /* HCI_OP_GET_MWS_TRANSPORT_CONFIG */
3733 HCI_INIT(hci_get_mws_transport_config_sync),
3734 /* HCI_OP_READ_SYNC_TRAIN_PARAMS */
3735 HCI_INIT(hci_read_sync_train_params_sync),
3736 /* HCI_OP_WRITE_SC_SUPPORT */
3737 HCI_INIT(hci_write_sc_support_1_sync),
3738 /* HCI_OP_WRITE_DEF_ERR_DATA_REPORTING */
3739 HCI_INIT(hci_set_err_data_report_sync),
3740 {}
3741};
3742
3743/* Set Suggested Default Data Length to maximum if supported */
3744static int hci_le_set_write_def_data_len_sync(struct hci_dev *hdev)
3745{
3746 struct hci_cp_le_write_def_data_len cp;
3747
3748 if (!(hdev->le_features[0] & HCI_LE_DATA_LEN_EXT))
3749 return 0;
3750
3751 memset(&cp, 0, sizeof(cp));
3752 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
3753 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
3754
3755 return __hci_cmd_sync_status(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN,
3756 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3757}
3758
3759/* Set Default PHY parameters if command is supported */
3760static int hci_le_set_default_phy_sync(struct hci_dev *hdev)
3761{
3762 struct hci_cp_le_set_default_phy cp;
3763
3764 if (!(hdev->commands[35] & 0x20))
3765 return 0;
3766
3767 memset(&cp, 0, sizeof(cp));
3768 cp.all_phys = 0x00;
3769 cp.tx_phys = hdev->le_tx_def_phys;
3770 cp.rx_phys = hdev->le_rx_def_phys;
3771
3772 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
3773 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
3774}
3775
3776static const struct hci_init_stage le_init4[] = {
3777 /* HCI_OP_LE_WRITE_DEF_DATA_LEN */
3778 HCI_INIT(hci_le_set_write_def_data_len_sync),
3779 /* HCI_OP_LE_SET_DEFAULT_PHY */
3780 HCI_INIT(hci_le_set_default_phy_sync),
3781 {}
3782};
3783
3784static int hci_init4_sync(struct hci_dev *hdev)
3785{
3786 int err;
3787
3788 bt_dev_dbg(hdev, "");
3789
3790 err = hci_init_stage_sync(hdev, hci_init4);
3791 if (err)
3792 return err;
3793
3794 if (lmp_le_capable(hdev))
3795 return hci_init_stage_sync(hdev, le_init4);
3796
3797 return 0;
3798}
3799
3800static int hci_init_sync(struct hci_dev *hdev)
3801{
3802 int err;
3803
3804 err = hci_init1_sync(hdev);
3805 if (err < 0)
3806 return err;
3807
3808 if (hci_dev_test_flag(hdev, HCI_SETUP))
3809 hci_debugfs_create_basic(hdev);
3810
3811 err = hci_init2_sync(hdev);
3812 if (err < 0)
3813 return err;
3814
3815 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
3816 * BR/EDR/LE type controllers. AMP controllers only need the
3817 * first two stages of init.
3818 */
3819 if (hdev->dev_type != HCI_PRIMARY)
3820 return 0;
3821
3822 err = hci_init3_sync(hdev);
3823 if (err < 0)
3824 return err;
3825
3826 err = hci_init4_sync(hdev);
3827 if (err < 0)
3828 return err;
3829
3830 /* This function is only called when the controller is actually in
3831 * configured state. When the controller is marked as unconfigured,
3832 * this initialization procedure is not run.
3833 *
3834 * It means that it is possible that a controller runs through its
3835 * setup phase and then discovers missing settings. If that is the
3836 * case, then this function will not be called. It then will only
3837 * be called during the config phase.
3838 *
3839 * So only when in setup phase or config phase, create the debugfs
3840 * entries and register the SMP channels.
3841 */
3842 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
3843 !hci_dev_test_flag(hdev, HCI_CONFIG))
3844 return 0;
3845
3846 hci_debugfs_create_common(hdev);
3847
3848 if (lmp_bredr_capable(hdev))
3849 hci_debugfs_create_bredr(hdev);
3850
3851 if (lmp_le_capable(hdev))
3852 hci_debugfs_create_le(hdev);
3853
3854 return 0;
3855}
3856
6b5c1cda
LAD
3857#define HCI_QUIRK_BROKEN(_quirk, _desc) { HCI_QUIRK_BROKEN_##_quirk, _desc }
3858
3859static const struct {
3860 unsigned long quirk;
3861 const char *desc;
3862} hci_broken_table[] = {
3863 HCI_QUIRK_BROKEN(LOCAL_COMMANDS,
3864 "HCI Read Local Supported Commands not supported"),
3865 HCI_QUIRK_BROKEN(STORED_LINK_KEY,
3866 "HCI Delete Stored Link Key command is advertised, "
3867 "but not supported."),
6b5c1cda
LAD
3868 HCI_QUIRK_BROKEN(READ_TRANSMIT_POWER,
3869 "HCI Read Transmit Power Level command is advertised, "
3870 "but not supported."),
3871 HCI_QUIRK_BROKEN(FILTER_CLEAR_ALL,
3872 "HCI Set Event Filter command not supported."),
3873 HCI_QUIRK_BROKEN(ENHANCED_SETUP_SYNC_CONN,
3874 "HCI Enhanced Setup Synchronous Connection command is "
3875 "advertised, but not supported.")
3876};
3877
1bbf4023
LAD
3878/* This function handles hdev setup stage:
3879 *
3880 * Calls hdev->setup
3881 * Setup address if HCI_QUIRK_USE_BDADDR_PROPERTY is set.
3882 */
3883static int hci_dev_setup_sync(struct hci_dev *hdev)
d0b13706
LAD
3884{
3885 int ret = 0;
1bbf4023
LAD
3886 bool invalid_bdaddr;
3887 size_t i;
d0b13706
LAD
3888
3889 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
1bbf4023
LAD
3890 !test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks))
3891 return 0;
d0b13706 3892
1bbf4023 3893 bt_dev_dbg(hdev, "");
d0b13706 3894
1bbf4023 3895 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
d0b13706 3896
1bbf4023
LAD
3897 if (hdev->setup)
3898 ret = hdev->setup(hdev);
6b5c1cda 3899
1bbf4023
LAD
3900 for (i = 0; i < ARRAY_SIZE(hci_broken_table); i++) {
3901 if (test_bit(hci_broken_table[i].quirk, &hdev->quirks))
3902 bt_dev_warn(hdev, "%s", hci_broken_table[i].desc);
3903 }
d0b13706 3904
1bbf4023
LAD
3905 /* The transport driver can set the quirk to mark the
3906 * BD_ADDR invalid before creating the HCI device or in
3907 * its setup callback.
3908 */
3909 invalid_bdaddr = test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
d0b13706 3910
1bbf4023 3911 if (!ret) {
d0b13706
LAD
3912 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) {
3913 if (!bacmp(&hdev->public_addr, BDADDR_ANY))
3914 hci_dev_get_bd_addr_from_property(hdev);
3915
3916 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
3917 hdev->set_bdaddr) {
3918 ret = hdev->set_bdaddr(hdev,
3919 &hdev->public_addr);
3920
3921 /* If setting of the BD_ADDR from the device
3922 * property succeeds, then treat the address
3923 * as valid even if the invalid BD_ADDR
3924 * quirk indicates otherwise.
3925 */
3926 if (!ret)
3927 invalid_bdaddr = false;
3928 }
3929 }
1bbf4023 3930 }
d0b13706 3931
1bbf4023
LAD
3932 /* The transport driver can set these quirks before
3933 * creating the HCI device or in its setup callback.
3934 *
3935 * For the invalid BD_ADDR quirk it is possible that
3936 * it becomes a valid address if the bootloader does
3937 * provide it (see above).
3938 *
3939 * In case any of them is set, the controller has to
3940 * start up as unconfigured.
3941 */
3942 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
3943 invalid_bdaddr)
3944 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
d0b13706 3945
1bbf4023
LAD
3946 /* For an unconfigured controller it is required to
3947 * read at least the version information provided by
3948 * the Read Local Version Information command.
3949 *
3950 * If the set_bdaddr driver callback is provided, then
3951 * also the original Bluetooth public device address
3952 * will be read using the Read BD Address command.
3953 */
3954 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
3955 return hci_unconf_init_sync(hdev);
3956
3957 return ret;
3958}
3959
3960/* This function handles hdev init stage:
3961 *
3962 * Calls hci_dev_setup_sync to perform setup stage
3963 * Calls hci_init_sync to perform HCI command init sequence
3964 */
3965static int hci_dev_init_sync(struct hci_dev *hdev)
3966{
3967 int ret;
3968
3969 bt_dev_dbg(hdev, "");
3970
3971 atomic_set(&hdev->cmd_cnt, 1);
3972 set_bit(HCI_INIT, &hdev->flags);
3973
3974 ret = hci_dev_setup_sync(hdev);
d0b13706
LAD
3975
3976 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
3977 /* If public address change is configured, ensure that
3978 * the address gets programmed. If the driver does not
3979 * support changing the public address, fail the power
3980 * on procedure.
3981 */
3982 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
3983 hdev->set_bdaddr)
3984 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
3985 else
3986 ret = -EADDRNOTAVAIL;
3987 }
3988
3989 if (!ret) {
3990 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
3991 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
3992 ret = hci_init_sync(hdev);
3993 if (!ret && hdev->post_init)
3994 ret = hdev->post_init(hdev);
3995 }
3996 }
3997
3998 /* If the HCI Reset command is clearing all diagnostic settings,
3999 * then they need to be reprogrammed after the init procedure
4000 * completed.
4001 */
4002 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
4003 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4004 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
4005 ret = hdev->set_diag(hdev, true);
4006
385315de
JM
4007 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4008 msft_do_open(hdev);
4009 aosp_do_open(hdev);
4010 }
d0b13706
LAD
4011
4012 clear_bit(HCI_INIT, &hdev->flags);
4013
1bbf4023
LAD
4014 return ret;
4015}
4016
4017int hci_dev_open_sync(struct hci_dev *hdev)
4018{
4019 int ret;
4020
4021 bt_dev_dbg(hdev, "");
4022
4023 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
4024 ret = -ENODEV;
4025 goto done;
4026 }
4027
4028 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4029 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
4030 /* Check for rfkill but allow the HCI setup stage to
4031 * proceed (which in itself doesn't cause any RF activity).
4032 */
4033 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
4034 ret = -ERFKILL;
4035 goto done;
4036 }
4037
4038 /* Check for valid public address or a configured static
4039 * random address, but let the HCI setup proceed to
4040 * be able to determine if there is a public address
4041 * or not.
4042 *
4043 * In case of user channel usage, it is not important
4044 * if a public address or static random address is
4045 * available.
4046 *
4047 * This check is only valid for BR/EDR controllers
4048 * since AMP controllers do not have an address.
4049 */
4050 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4051 hdev->dev_type == HCI_PRIMARY &&
4052 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4053 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
4054 ret = -EADDRNOTAVAIL;
4055 goto done;
4056 }
4057 }
4058
4059 if (test_bit(HCI_UP, &hdev->flags)) {
4060 ret = -EALREADY;
4061 goto done;
4062 }
4063
4064 if (hdev->open(hdev)) {
4065 ret = -EIO;
4066 goto done;
4067 }
4068
4069 set_bit(HCI_RUNNING, &hdev->flags);
4070 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
4071
4072 ret = hci_dev_init_sync(hdev);
d0b13706
LAD
4073 if (!ret) {
4074 hci_dev_hold(hdev);
4075 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
4076 hci_adv_instances_set_rpa_expired(hdev, true);
4077 set_bit(HCI_UP, &hdev->flags);
4078 hci_sock_dev_event(hdev, HCI_DEV_UP);
4079 hci_leds_update_powered(hdev, true);
4080 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
4081 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
4082 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
4083 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4084 hci_dev_test_flag(hdev, HCI_MGMT) &&
4085 hdev->dev_type == HCI_PRIMARY) {
4086 ret = hci_powered_update_sync(hdev);
4087 }
4088 } else {
4089 /* Init failed, cleanup */
4090 flush_work(&hdev->tx_work);
4091
4092 /* Since hci_rx_work() is possible to awake new cmd_work
4093 * it should be flushed first to avoid unexpected call of
4094 * hci_cmd_work()
4095 */
4096 flush_work(&hdev->rx_work);
4097 flush_work(&hdev->cmd_work);
4098
4099 skb_queue_purge(&hdev->cmd_q);
4100 skb_queue_purge(&hdev->rx_q);
4101
4102 if (hdev->flush)
4103 hdev->flush(hdev);
4104
4105 if (hdev->sent_cmd) {
4106 kfree_skb(hdev->sent_cmd);
4107 hdev->sent_cmd = NULL;
4108 }
4109
4110 clear_bit(HCI_RUNNING, &hdev->flags);
4111 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4112
4113 hdev->close(hdev);
4114 hdev->flags &= BIT(HCI_RAW);
4115 }
4116
4117done:
4118 return ret;
4119}
4120
4121/* This function requires the caller holds hdev->lock */
4122static void hci_pend_le_actions_clear(struct hci_dev *hdev)
4123{
4124 struct hci_conn_params *p;
4125
4126 list_for_each_entry(p, &hdev->le_conn_params, list) {
4127 if (p->conn) {
4128 hci_conn_drop(p->conn);
4129 hci_conn_put(p->conn);
4130 p->conn = NULL;
4131 }
4132 list_del_init(&p->action);
4133 }
4134
4135 BT_DBG("All LE pending actions cleared");
4136}
4137
4138int hci_dev_close_sync(struct hci_dev *hdev)
4139{
4140 bool auto_off;
4141 int err = 0;
4142
4143 bt_dev_dbg(hdev, "");
4144
4145 cancel_delayed_work(&hdev->power_off);
4146 cancel_delayed_work(&hdev->ncmd_timer);
4147
4148 hci_request_cancel_all(hdev);
4149
4150 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
4151 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4152 test_bit(HCI_UP, &hdev->flags)) {
4153 /* Execute vendor specific shutdown routine */
4154 if (hdev->shutdown)
4155 err = hdev->shutdown(hdev);
4156 }
4157
4158 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
4159 cancel_delayed_work_sync(&hdev->cmd_timer);
4160 return err;
4161 }
4162
4163 hci_leds_update_powered(hdev, false);
4164
4165 /* Flush RX and TX works */
4166 flush_work(&hdev->tx_work);
4167 flush_work(&hdev->rx_work);
4168
4169 if (hdev->discov_timeout > 0) {
4170 hdev->discov_timeout = 0;
4171 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4172 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
4173 }
4174
4175 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
4176 cancel_delayed_work(&hdev->service_cache);
4177
4178 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4179 struct adv_info *adv_instance;
4180
4181 cancel_delayed_work_sync(&hdev->rpa_expired);
4182
4183 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
4184 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
4185 }
4186
4187 /* Avoid potential lockdep warnings from the *_flush() calls by
4188 * ensuring the workqueue is empty up front.
4189 */
4190 drain_workqueue(hdev->workqueue);
4191
4192 hci_dev_lock(hdev);
4193
4194 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4195
4196 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
4197
4198 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
4199 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4200 hci_dev_test_flag(hdev, HCI_MGMT))
4201 __mgmt_power_off(hdev);
4202
4203 hci_inquiry_cache_flush(hdev);
4204 hci_pend_le_actions_clear(hdev);
4205 hci_conn_hash_flush(hdev);
fa78d2d1 4206 /* Prevent data races on hdev->smp_data or hdev->smp_bredr_data */
d0b13706 4207 smp_unregister(hdev);
fa78d2d1 4208 hci_dev_unlock(hdev);
d0b13706
LAD
4209
4210 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
4211
385315de
JM
4212 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4213 aosp_do_close(hdev);
4214 msft_do_close(hdev);
4215 }
d0b13706
LAD
4216
4217 if (hdev->flush)
4218 hdev->flush(hdev);
4219
4220 /* Reset device */
4221 skb_queue_purge(&hdev->cmd_q);
4222 atomic_set(&hdev->cmd_cnt, 1);
4223 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
4224 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4225 set_bit(HCI_INIT, &hdev->flags);
4226 hci_reset_sync(hdev);
4227 clear_bit(HCI_INIT, &hdev->flags);
4228 }
4229
4230 /* flush cmd work */
4231 flush_work(&hdev->cmd_work);
4232
4233 /* Drop queues */
4234 skb_queue_purge(&hdev->rx_q);
4235 skb_queue_purge(&hdev->cmd_q);
4236 skb_queue_purge(&hdev->raw_q);
4237
4238 /* Drop last sent command */
4239 if (hdev->sent_cmd) {
4240 cancel_delayed_work_sync(&hdev->cmd_timer);
4241 kfree_skb(hdev->sent_cmd);
4242 hdev->sent_cmd = NULL;
4243 }
4244
4245 clear_bit(HCI_RUNNING, &hdev->flags);
4246 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
4247
d0b13706
LAD
4248 /* After this point our queues are empty and no tasks are scheduled. */
4249 hdev->close(hdev);
4250
4251 /* Clear flags */
4252 hdev->flags &= BIT(HCI_RAW);
4253 hci_dev_clear_volatile_flags(hdev);
4254
4255 /* Controller radio is available but is currently powered down */
4256 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
4257
4258 memset(hdev->eir, 0, sizeof(hdev->eir));
4259 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
4260 bacpy(&hdev->random_addr, BDADDR_ANY);
4261
4262 hci_dev_put(hdev);
4263 return err;
4264}
4265
4266/* This function perform power on HCI command sequence as follows:
4267 *
4268 * If controller is already up (HCI_UP) performs hci_powered_update_sync
4269 * sequence otherwise run hci_dev_open_sync which will follow with
4270 * hci_powered_update_sync after the init sequence is completed.
4271 */
4272static int hci_power_on_sync(struct hci_dev *hdev)
4273{
4274 int err;
4275
4276 if (test_bit(HCI_UP, &hdev->flags) &&
4277 hci_dev_test_flag(hdev, HCI_MGMT) &&
4278 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
4279 cancel_delayed_work(&hdev->power_off);
4280 return hci_powered_update_sync(hdev);
4281 }
4282
4283 err = hci_dev_open_sync(hdev);
4284 if (err < 0)
4285 return err;
4286
4287 /* During the HCI setup phase, a few error conditions are
4288 * ignored and they need to be checked now. If they are still
4289 * valid, it is important to return the device back off.
4290 */
4291 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
4292 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
4293 (hdev->dev_type == HCI_PRIMARY &&
4294 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
4295 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
4296 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
4297 hci_dev_close_sync(hdev);
4298 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
4299 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
4300 HCI_AUTO_OFF_TIMEOUT);
4301 }
4302
4303 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
4304 /* For unconfigured devices, set the HCI_RAW flag
4305 * so that userspace can easily identify them.
4306 */
4307 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4308 set_bit(HCI_RAW, &hdev->flags);
4309
4310 /* For fully configured devices, this will send
4311 * the Index Added event. For unconfigured devices,
4312 * it will send Unconfigued Index Added event.
4313 *
4314 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
4315 * and no event will be send.
4316 */
4317 mgmt_index_added(hdev);
4318 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
4319 /* When the controller is now configured, then it
4320 * is important to clear the HCI_RAW flag.
4321 */
4322 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
4323 clear_bit(HCI_RAW, &hdev->flags);
4324
4325 /* Powering on the controller with HCI_CONFIG set only
4326 * happens with the transition from unconfigured to
4327 * configured. This will send the Index Added event.
4328 */
4329 mgmt_index_added(hdev);
4330 }
4331
4332 return 0;
4333}
4334
4335static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr)
4336{
4337 struct hci_cp_remote_name_req_cancel cp;
4338
4339 memset(&cp, 0, sizeof(cp));
4340 bacpy(&cp.bdaddr, addr);
4341
4342 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
4343 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4344}
4345
4346int hci_stop_discovery_sync(struct hci_dev *hdev)
4347{
4348 struct discovery_state *d = &hdev->discovery;
4349 struct inquiry_entry *e;
4350 int err;
4351
4352 bt_dev_dbg(hdev, "state %u", hdev->discovery.state);
4353
4354 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
4355 if (test_bit(HCI_INQUIRY, &hdev->flags)) {
4356 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL,
4357 0, NULL, HCI_CMD_TIMEOUT);
4358 if (err)
4359 return err;
4360 }
4361
4362 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
4363 cancel_delayed_work(&hdev->le_scan_disable);
4364 cancel_delayed_work(&hdev->le_scan_restart);
4365
4366 err = hci_scan_disable_sync(hdev);
4367 if (err)
4368 return err;
4369 }
4370
4371 } else {
4372 err = hci_scan_disable_sync(hdev);
4373 if (err)
4374 return err;
4375 }
4376
4377 /* Resume advertising if it was paused */
4378 if (use_ll_privacy(hdev))
4379 hci_resume_advertising_sync(hdev);
4380
4381 /* No further actions needed for LE-only discovery */
4382 if (d->type == DISCOV_TYPE_LE)
4383 return 0;
4384
4385 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
4386 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
4387 NAME_PENDING);
4388 if (!e)
4389 return 0;
4390
4391 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr);
4392 }
4393
4394 return 0;
4395}
4396
4397static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle,
4398 u8 reason)
4399{
4400 struct hci_cp_disconn_phy_link cp;
4401
4402 memset(&cp, 0, sizeof(cp));
4403 cp.phy_handle = HCI_PHY_HANDLE(handle);
4404 cp.reason = reason;
4405
4406 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK,
4407 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4408}
4409
4410static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn,
4411 u8 reason)
4412{
4413 struct hci_cp_disconnect cp;
4414
4415 if (conn->type == AMP_LINK)
4416 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason);
4417
4418 memset(&cp, 0, sizeof(cp));
4419 cp.handle = cpu_to_le16(conn->handle);
4420 cp.reason = reason;
4421
4422 /* Wait for HCI_EV_DISCONN_COMPLETE not HCI_EV_CMD_STATUS when not
4423 * suspending.
4424 */
4425 if (!hdev->suspended)
4426 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT,
4427 sizeof(cp), &cp,
4428 HCI_EV_DISCONN_COMPLETE,
4429 HCI_CMD_TIMEOUT, NULL);
4430
4431 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp,
4432 HCI_CMD_TIMEOUT);
4433}
4434
4435static int hci_le_connect_cancel_sync(struct hci_dev *hdev,
4436 struct hci_conn *conn)
4437{
4438 if (test_bit(HCI_CONN_SCANNING, &conn->flags))
4439 return 0;
4440
4441 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL,
4442 6, &conn->dst, HCI_CMD_TIMEOUT);
4443}
4444
4445static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn)
4446{
4447 if (conn->type == LE_LINK)
4448 return hci_le_connect_cancel_sync(hdev, conn);
4449
4450 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
4451 return 0;
4452
4453 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL,
4454 6, &conn->dst, HCI_CMD_TIMEOUT);
4455}
4456
4457static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn,
4458 u8 reason)
4459{
4460 struct hci_cp_reject_sync_conn_req cp;
4461
4462 memset(&cp, 0, sizeof(cp));
4463 bacpy(&cp.bdaddr, &conn->dst);
4464 cp.reason = reason;
4465
4466 /* SCO rejection has its own limited set of
4467 * allowed error values (0x0D-0x0F).
4468 */
4469 if (reason < 0x0d || reason > 0x0f)
4470 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
4471
4472 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ,
4473 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4474}
4475
4476static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn,
4477 u8 reason)
4478{
4479 struct hci_cp_reject_conn_req cp;
4480
4481 if (conn->type == SCO_LINK || conn->type == ESCO_LINK)
4482 return hci_reject_sco_sync(hdev, conn, reason);
4483
4484 memset(&cp, 0, sizeof(cp));
4485 bacpy(&cp.bdaddr, &conn->dst);
4486 cp.reason = reason;
4487
4488 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ,
4489 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4490}
4491
1f7435c8 4492int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason)
d0b13706 4493{
9b3628d7
LAD
4494 int err;
4495
d0b13706
LAD
4496 switch (conn->state) {
4497 case BT_CONNECTED:
4498 case BT_CONFIG:
4499 return hci_disconnect_sync(hdev, conn, reason);
4500 case BT_CONNECT:
9b3628d7
LAD
4501 err = hci_connect_cancel_sync(hdev, conn);
4502 /* Cleanup hci_conn object if it cannot be cancelled as it
4503 * likelly means the controller and host stack are out of sync.
4504 */
4505 if (err)
4506 hci_conn_failed(conn, err);
4507
4508 return err;
d0b13706
LAD
4509 case BT_CONNECT2:
4510 return hci_reject_conn_sync(hdev, conn, reason);
4511 default:
4512 conn->state = BT_CLOSED;
4513 break;
4514 }
4515
4516 return 0;
4517}
4518
182ee45d
LAD
4519static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason)
4520{
4521 struct hci_conn *conn, *tmp;
4522 int err;
4523
4524 list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
4525 err = hci_abort_conn_sync(hdev, conn, reason);
4526 if (err)
4527 return err;
4528 }
4529
8cd3c55c 4530 return 0;
182ee45d
LAD
4531}
4532
d0b13706
LAD
4533/* This function perform power off HCI command sequence as follows:
4534 *
4535 * Clear Advertising
4536 * Stop Discovery
4537 * Disconnect all connections
4538 * hci_dev_close_sync
4539 */
4540static int hci_power_off_sync(struct hci_dev *hdev)
4541{
d0b13706
LAD
4542 int err;
4543
4544 /* If controller is already down there is nothing to do */
4545 if (!test_bit(HCI_UP, &hdev->flags))
4546 return 0;
4547
4548 if (test_bit(HCI_ISCAN, &hdev->flags) ||
4549 test_bit(HCI_PSCAN, &hdev->flags)) {
4550 err = hci_write_scan_enable_sync(hdev, 0x00);
4551 if (err)
4552 return err;
4553 }
4554
4555 err = hci_clear_adv_sync(hdev, NULL, false);
4556 if (err)
4557 return err;
4558
4559 err = hci_stop_discovery_sync(hdev);
4560 if (err)
4561 return err;
4562
182ee45d
LAD
4563 /* Terminated due to Power Off */
4564 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
4565 if (err)
4566 return err;
d0b13706
LAD
4567
4568 return hci_dev_close_sync(hdev);
4569}
4570
4571int hci_set_powered_sync(struct hci_dev *hdev, u8 val)
4572{
4573 if (val)
4574 return hci_power_on_sync(hdev);
cf75ad8b
LAD
4575
4576 return hci_power_off_sync(hdev);
4577}
abfeea47 4578
2bd1b237
LAD
4579static int hci_write_iac_sync(struct hci_dev *hdev)
4580{
4581 struct hci_cp_write_current_iac_lap cp;
4582
4583 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
4584 return 0;
4585
4586 memset(&cp, 0, sizeof(cp));
4587
4588 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
4589 /* Limited discoverable mode */
4590 cp.num_iac = min_t(u8, hdev->num_iac, 2);
4591 cp.iac_lap[0] = 0x00; /* LIAC */
4592 cp.iac_lap[1] = 0x8b;
4593 cp.iac_lap[2] = 0x9e;
4594 cp.iac_lap[3] = 0x33; /* GIAC */
4595 cp.iac_lap[4] = 0x8b;
4596 cp.iac_lap[5] = 0x9e;
4597 } else {
4598 /* General discoverable mode */
4599 cp.num_iac = 1;
4600 cp.iac_lap[0] = 0x33; /* GIAC */
4601 cp.iac_lap[1] = 0x8b;
4602 cp.iac_lap[2] = 0x9e;
4603 }
4604
4605 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP,
4606 (cp.num_iac * 3) + 1, &cp,
4607 HCI_CMD_TIMEOUT);
4608}
4609
4610int hci_update_discoverable_sync(struct hci_dev *hdev)
4611{
4612 int err = 0;
4613
4614 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
4615 err = hci_write_iac_sync(hdev);
4616 if (err)
4617 return err;
4618
4619 err = hci_update_scan_sync(hdev);
4620 if (err)
4621 return err;
4622
4623 err = hci_update_class_sync(hdev);
4624 if (err)
4625 return err;
4626 }
4627
4628 /* Advertising instances don't use the global discoverable setting, so
4629 * only update AD if advertising was enabled using Set Advertising.
4630 */
4631 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
4632 err = hci_update_adv_data_sync(hdev, 0x00);
4633 if (err)
4634 return err;
4635
4636 /* Discoverable mode affects the local advertising
4637 * address in limited privacy mode.
4638 */
4639 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) {
4640 if (ext_adv_capable(hdev))
4641 err = hci_start_ext_adv_sync(hdev, 0x00);
4642 else
4643 err = hci_enable_advertising_sync(hdev);
4644 }
4645 }
4646
4647 return err;
4648}
4649
4650static int update_discoverable_sync(struct hci_dev *hdev, void *data)
4651{
4652 return hci_update_discoverable_sync(hdev);
4653}
4654
4655int hci_update_discoverable(struct hci_dev *hdev)
4656{
4657 /* Only queue if it would have any effect */
4658 if (hdev_is_powered(hdev) &&
4659 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4660 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
4661 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
4662 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL,
4663 NULL);
4664
4665 return 0;
4666}
4667
f056a657
LAD
4668int hci_update_connectable_sync(struct hci_dev *hdev)
4669{
4670 int err;
4671
4672 err = hci_update_scan_sync(hdev);
4673 if (err)
4674 return err;
4675
4676 /* If BR/EDR is not enabled and we disable advertising as a
4677 * by-product of disabling connectable, we need to update the
4678 * advertising flags.
4679 */
4680 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4681 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
4682
4683 /* Update the advertising parameters if necessary */
4684 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4685 !list_empty(&hdev->adv_instances)) {
4686 if (ext_adv_capable(hdev))
4687 err = hci_start_ext_adv_sync(hdev,
4688 hdev->cur_adv_instance);
4689 else
4690 err = hci_enable_advertising_sync(hdev);
4691
4692 if (err)
4693 return err;
4694 }
4695
4696 return hci_update_passive_scan_sync(hdev);
4697}
4698
abfeea47
LAD
4699static int hci_inquiry_sync(struct hci_dev *hdev, u8 length)
4700{
4701 const u8 giac[3] = { 0x33, 0x8b, 0x9e };
4702 const u8 liac[3] = { 0x00, 0x8b, 0x9e };
4703 struct hci_cp_inquiry cp;
4704
4705 bt_dev_dbg(hdev, "");
4706
4707 if (hci_dev_test_flag(hdev, HCI_INQUIRY))
4708 return 0;
4709
4710 hci_dev_lock(hdev);
4711 hci_inquiry_cache_flush(hdev);
4712 hci_dev_unlock(hdev);
4713
4714 memset(&cp, 0, sizeof(cp));
4715
4716 if (hdev->discovery.limited)
4717 memcpy(&cp.lap, liac, sizeof(cp.lap));
4718 else
4719 memcpy(&cp.lap, giac, sizeof(cp.lap));
4720
4721 cp.length = length;
4722
4723 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY,
4724 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
4725}
4726
4727static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval)
4728{
4729 u8 own_addr_type;
4730 /* Accept list is not used for discovery */
4731 u8 filter_policy = 0x00;
4732 /* Default is to enable duplicates filter */
4733 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
4734 int err;
4735
4736 bt_dev_dbg(hdev, "");
4737
4738 /* If controller is scanning, it means the passive scanning is
4739 * running. Thus, we should temporarily stop it in order to set the
4740 * discovery scanning parameters.
4741 */
4742 err = hci_scan_disable_sync(hdev);
4743 if (err) {
4744 bt_dev_err(hdev, "Unable to disable scanning: %d", err);
4745 return err;
4746 }
4747
4748 cancel_interleave_scan(hdev);
4749
4750 /* Pause advertising since active scanning disables address resolution
4751 * which advertising depend on in order to generate its RPAs.
4752 */
4753 if (use_ll_privacy(hdev)) {
4754 err = hci_pause_advertising_sync(hdev);
4755 if (err) {
4756 bt_dev_err(hdev, "pause advertising failed: %d", err);
4757 goto failed;
4758 }
4759 }
4760
4761 /* Disable address resolution while doing active scanning since the
4762 * accept list shall not be used and all reports shall reach the host
4763 * anyway.
4764 */
4765 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00);
4766 if (err) {
4767 bt_dev_err(hdev, "Unable to disable Address Resolution: %d",
4768 err);
4769 goto failed;
4770 }
4771
4772 /* All active scans will be done with either a resolvable private
4773 * address (when privacy feature has been enabled) or non-resolvable
4774 * private address.
4775 */
4776 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev),
4777 &own_addr_type);
4778 if (err < 0)
4779 own_addr_type = ADDR_LE_DEV_PUBLIC;
4780
4781 if (hci_is_adv_monitoring(hdev)) {
4782 /* Duplicate filter should be disabled when some advertisement
4783 * monitor is activated, otherwise AdvMon can only receive one
4784 * advertisement for one peer(*) during active scanning, and
4785 * might report loss to these peers.
4786 *
4787 * Note that different controllers have different meanings of
4788 * |duplicate|. Some of them consider packets with the same
4789 * address as duplicate, and others consider packets with the
4790 * same address and the same RSSI as duplicate. Although in the
4791 * latter case we don't need to disable duplicate filter, but
4792 * it is common to have active scanning for a short period of
4793 * time, the power impact should be neglectable.
4794 */
4795 filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
4796 }
4797
4798 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval,
4799 hdev->le_scan_window_discovery,
4800 own_addr_type, filter_policy, filter_dup);
4801 if (!err)
4802 return err;
4803
4804failed:
4805 /* Resume advertising if it was paused */
4806 if (use_ll_privacy(hdev))
4807 hci_resume_advertising_sync(hdev);
4808
4809 /* Resume passive scanning */
4810 hci_update_passive_scan_sync(hdev);
4811 return err;
4812}
4813
4814static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev)
4815{
4816 int err;
4817
4818 bt_dev_dbg(hdev, "");
4819
4820 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2);
4821 if (err)
4822 return err;
4823
4824 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
4825}
4826
4827int hci_start_discovery_sync(struct hci_dev *hdev)
4828{
4829 unsigned long timeout;
4830 int err;
4831
4832 bt_dev_dbg(hdev, "type %u", hdev->discovery.type);
4833
4834 switch (hdev->discovery.type) {
4835 case DISCOV_TYPE_BREDR:
4836 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN);
4837 case DISCOV_TYPE_INTERLEAVED:
4838 /* When running simultaneous discovery, the LE scanning time
4839 * should occupy the whole discovery time sine BR/EDR inquiry
4840 * and LE scanning are scheduled by the controller.
4841 *
4842 * For interleaving discovery in comparison, BR/EDR inquiry
4843 * and LE scanning are done sequentially with separate
4844 * timeouts.
4845 */
4846 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
4847 &hdev->quirks)) {
4848 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4849 /* During simultaneous discovery, we double LE scan
4850 * interval. We must leave some time for the controller
4851 * to do BR/EDR inquiry.
4852 */
4853 err = hci_start_interleaved_discovery_sync(hdev);
4854 break;
4855 }
4856
4857 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
4858 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
4859 break;
4860 case DISCOV_TYPE_LE:
4861 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4862 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery);
4863 break;
4864 default:
4865 return -EINVAL;
4866 }
4867
4868 if (err)
4869 return err;
4870
4871 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout));
4872
4873 /* When service discovery is used and the controller has a
4874 * strict duplicate filter, it is important to remember the
4875 * start and duration of the scan. This is required for
4876 * restarting scanning during the discovery phase.
4877 */
4878 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
4879 hdev->discovery.result_filtering) {
4880 hdev->discovery.scan_start = jiffies;
4881 hdev->discovery.scan_duration = timeout;
4882 }
4883
4884 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
4885 timeout);
abfeea47
LAD
4886 return 0;
4887}
182ee45d
LAD
4888
4889static void hci_suspend_monitor_sync(struct hci_dev *hdev)
4890{
4891 switch (hci_get_adv_monitor_offload_ext(hdev)) {
4892 case HCI_ADV_MONITOR_EXT_MSFT:
4893 msft_suspend_sync(hdev);
4894 break;
4895 default:
4896 return;
4897 }
4898}
4899
4900/* This function disables discovery and mark it as paused */
4901static int hci_pause_discovery_sync(struct hci_dev *hdev)
4902{
4903 int old_state = hdev->discovery.state;
4904 int err;
4905
4906 /* If discovery already stopped/stopping/paused there nothing to do */
4907 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING ||
4908 hdev->discovery_paused)
4909 return 0;
4910
4911 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
4912 err = hci_stop_discovery_sync(hdev);
4913 if (err)
4914 return err;
4915
4916 hdev->discovery_paused = true;
4917 hdev->discovery_old_state = old_state;
4918 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4919
4920 return 0;
4921}
4922
4923static int hci_update_event_filter_sync(struct hci_dev *hdev)
4924{
4925 struct bdaddr_list_with_flags *b;
4926 u8 scan = SCAN_DISABLED;
4927 bool scanning = test_bit(HCI_PSCAN, &hdev->flags);
4928 int err;
4929
4930 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
4931 return 0;
4932
0eaecfb2
IFM
4933 /* Some fake CSR controllers lock up after setting this type of
4934 * filter, so avoid sending the request altogether.
4935 */
4936 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks))
4937 return 0;
4938
182ee45d
LAD
4939 /* Always clear event filter when starting */
4940 hci_clear_event_filter_sync(hdev);
4941
4942 list_for_each_entry(b, &hdev->accept_list, list) {
e1cff700 4943 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
182ee45d
LAD
4944 continue;
4945
4946 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr);
4947
4948 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP,
4949 HCI_CONN_SETUP_ALLOW_BDADDR,
4950 &b->bdaddr,
4951 HCI_CONN_SETUP_AUTO_ON);
4952 if (err)
4953 bt_dev_dbg(hdev, "Failed to set event filter for %pMR",
4954 &b->bdaddr);
4955 else
4956 scan = SCAN_PAGE;
4957 }
4958
4959 if (scan && !scanning)
4960 hci_write_scan_enable_sync(hdev, scan);
4961 else if (!scan && scanning)
4962 hci_write_scan_enable_sync(hdev, scan);
4963
4964 return 0;
4965}
4966
3b420553
LAD
4967/* This function disables scan (BR and LE) and mark it as paused */
4968static int hci_pause_scan_sync(struct hci_dev *hdev)
4969{
4970 if (hdev->scanning_paused)
4971 return 0;
4972
4973 /* Disable page scan if enabled */
4974 if (test_bit(HCI_PSCAN, &hdev->flags))
4975 hci_write_scan_enable_sync(hdev, SCAN_DISABLED);
4976
4977 hci_scan_disable_sync(hdev);
4978
4979 hdev->scanning_paused = true;
4980
4981 return 0;
4982}
4983
182ee45d
LAD
4984/* This function performs the HCI suspend procedures in the follow order:
4985 *
4986 * Pause discovery (active scanning/inquiry)
4987 * Pause Directed Advertising/Advertising
3b420553 4988 * Pause Scanning (passive scanning in case discovery was not active)
182ee45d
LAD
4989 * Disconnect all connections
4990 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup
4991 * otherwise:
4992 * Update event mask (only set events that are allowed to wake up the host)
4993 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP)
4994 * Update passive scanning (lower duty cycle)
4995 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE
4996 */
4997int hci_suspend_sync(struct hci_dev *hdev)
4998{
4999 int err;
5000
5001 /* If marked as suspended there nothing to do */
5002 if (hdev->suspended)
5003 return 0;
5004
5005 /* Mark device as suspended */
5006 hdev->suspended = true;
5007
5008 /* Pause discovery if not already stopped */
5009 hci_pause_discovery_sync(hdev);
5010
5011 /* Pause other advertisements */
5012 hci_pause_advertising_sync(hdev);
5013
182ee45d
LAD
5014 /* Suspend monitor filters */
5015 hci_suspend_monitor_sync(hdev);
5016
5017 /* Prevent disconnects from causing scanning to be re-enabled */
3b420553 5018 hci_pause_scan_sync(hdev);
182ee45d
LAD
5019
5020 /* Soft disconnect everything (power off) */
5021 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF);
5022 if (err) {
5023 /* Set state to BT_RUNNING so resume doesn't notify */
5024 hdev->suspend_state = BT_RUNNING;
5025 hci_resume_sync(hdev);
5026 return err;
5027 }
5028
5029 /* Only configure accept list if disconnect succeeded and wake
5030 * isn't being prevented.
5031 */
5032 if (!hdev->wakeup || !hdev->wakeup(hdev)) {
5033 hdev->suspend_state = BT_SUSPEND_DISCONNECT;
5034 return 0;
5035 }
5036
5037 /* Unpause to take care of updating scanning params */
5038 hdev->scanning_paused = false;
5039
5040 /* Update event mask so only the allowed event can wakeup the host */
5041 hci_set_event_mask_sync(hdev);
5042
5043 /* Enable event filter for paired devices */
5044 hci_update_event_filter_sync(hdev);
5045
5046 /* Update LE passive scan if enabled */
5047 hci_update_passive_scan_sync(hdev);
5048
5049 /* Pause scan changes again. */
5050 hdev->scanning_paused = true;
5051
5052 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE;
5053
5054 return 0;
5055}
5056
5057/* This function resumes discovery */
5058static int hci_resume_discovery_sync(struct hci_dev *hdev)
5059{
5060 int err;
5061
5062 /* If discovery not paused there nothing to do */
5063 if (!hdev->discovery_paused)
5064 return 0;
5065
5066 hdev->discovery_paused = false;
5067
5068 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
5069
5070 err = hci_start_discovery_sync(hdev);
5071
5072 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED :
5073 DISCOVERY_FINDING);
5074
5075 return err;
5076}
5077
5078static void hci_resume_monitor_sync(struct hci_dev *hdev)
5079{
5080 switch (hci_get_adv_monitor_offload_ext(hdev)) {
5081 case HCI_ADV_MONITOR_EXT_MSFT:
5082 msft_resume_sync(hdev);
5083 break;
5084 default:
5085 return;
5086 }
5087}
5088
3b420553
LAD
5089/* This function resume scan and reset paused flag */
5090static int hci_resume_scan_sync(struct hci_dev *hdev)
5091{
5092 if (!hdev->scanning_paused)
5093 return 0;
5094
68253f3c
ZJ
5095 hdev->scanning_paused = false;
5096
3b420553
LAD
5097 hci_update_scan_sync(hdev);
5098
5099 /* Reset passive scanning to normal */
5100 hci_update_passive_scan_sync(hdev);
5101
3b420553
LAD
5102 return 0;
5103}
5104
182ee45d
LAD
5105/* This function performs the HCI suspend procedures in the follow order:
5106 *
5107 * Restore event mask
5108 * Clear event filter
5109 * Update passive scanning (normal duty cycle)
5110 * Resume Directed Advertising/Advertising
5111 * Resume discovery (active scanning/inquiry)
5112 */
5113int hci_resume_sync(struct hci_dev *hdev)
5114{
5115 /* If not marked as suspended there nothing to do */
5116 if (!hdev->suspended)
5117 return 0;
5118
5119 hdev->suspended = false;
182ee45d
LAD
5120
5121 /* Restore event mask */
5122 hci_set_event_mask_sync(hdev);
5123
5124 /* Clear any event filters and restore scan state */
5125 hci_clear_event_filter_sync(hdev);
182ee45d 5126
3b420553
LAD
5127 /* Resume scanning */
5128 hci_resume_scan_sync(hdev);
182ee45d
LAD
5129
5130 /* Resume monitor filters */
5131 hci_resume_monitor_sync(hdev);
5132
5133 /* Resume other advertisements */
5134 hci_resume_advertising_sync(hdev);
5135
5136 /* Resume discovery */
5137 hci_resume_discovery_sync(hdev);
5138
5139 return 0;
5140}
8e8b92ee
LAD
5141
5142static bool conn_use_rpa(struct hci_conn *conn)
5143{
5144 struct hci_dev *hdev = conn->hdev;
5145
5146 return hci_dev_test_flag(hdev, HCI_PRIVACY);
5147}
5148
5149static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev,
5150 struct hci_conn *conn)
5151{
5152 struct hci_cp_le_set_ext_adv_params cp;
5153 int err;
5154 bdaddr_t random_addr;
5155 u8 own_addr_type;
5156
5157 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5158 &own_addr_type);
5159 if (err)
5160 return err;
5161
5162 /* Set require_privacy to false so that the remote device has a
5163 * chance of identifying us.
5164 */
5165 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
5166 &own_addr_type, &random_addr);
5167 if (err)
5168 return err;
5169
5170 memset(&cp, 0, sizeof(cp));
5171
5172 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
5173 cp.own_addr_type = own_addr_type;
5174 cp.channel_map = hdev->le_adv_channel_map;
5175 cp.tx_power = HCI_TX_POWER_INVALID;
5176 cp.primary_phy = HCI_ADV_PHY_1M;
5177 cp.secondary_phy = HCI_ADV_PHY_1M;
5178 cp.handle = 0x00; /* Use instance 0 for directed adv */
5179 cp.own_addr_type = own_addr_type;
5180 cp.peer_addr_type = conn->dst_type;
5181 bacpy(&cp.peer_addr, &conn->dst);
5182
5183 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
5184 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
5185 * does not supports advertising data when the advertising set already
5186 * contains some, the controller shall return erroc code 'Invalid
5187 * HCI Command Parameters(0x12).
5188 * So it is required to remove adv set for handle 0x00. since we use
5189 * instance 0 for directed adv.
5190 */
5191 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL);
5192 if (err)
5193 return err;
5194
5195 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS,
5196 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5197 if (err)
5198 return err;
5199
5200 /* Check if random address need to be updated */
5201 if (own_addr_type == ADDR_LE_DEV_RANDOM &&
5202 bacmp(&random_addr, BDADDR_ANY) &&
5203 bacmp(&random_addr, &hdev->random_addr)) {
5204 err = hci_set_adv_set_random_addr_sync(hdev, 0x00,
5205 &random_addr);
5206 if (err)
5207 return err;
5208 }
5209
5210 return hci_enable_ext_advertising_sync(hdev, 0x00);
5211}
5212
5213static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
5214 struct hci_conn *conn)
5215{
5216 struct hci_cp_le_set_adv_param cp;
5217 u8 status;
5218 u8 own_addr_type;
5219 u8 enable;
5220
5221 if (ext_adv_capable(hdev))
5222 return hci_le_ext_directed_advertising_sync(hdev, conn);
5223
5224 /* Clear the HCI_LE_ADV bit temporarily so that the
5225 * hci_update_random_address knows that it's safe to go ahead
5226 * and write a new random address. The flag will be set back on
5227 * as soon as the SET_ADV_ENABLE HCI command completes.
5228 */
5229 hci_dev_clear_flag(hdev, HCI_LE_ADV);
5230
5231 /* Set require_privacy to false so that the remote device has a
5232 * chance of identifying us.
5233 */
5234 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5235 &own_addr_type);
5236 if (status)
5237 return status;
5238
5239 memset(&cp, 0, sizeof(cp));
5240
5241 /* Some controllers might reject command if intervals are not
5242 * within range for undirected advertising.
5243 * BCM20702A0 is known to be affected by this.
5244 */
5245 cp.min_interval = cpu_to_le16(0x0020);
5246 cp.max_interval = cpu_to_le16(0x0020);
5247
5248 cp.type = LE_ADV_DIRECT_IND;
5249 cp.own_address_type = own_addr_type;
5250 cp.direct_addr_type = conn->dst_type;
5251 bacpy(&cp.direct_addr, &conn->dst);
5252 cp.channel_map = hdev->le_adv_channel_map;
5253
5254 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM,
5255 sizeof(cp), &cp, HCI_CMD_TIMEOUT);
5256 if (status)
5257 return status;
5258
5259 enable = 0x01;
5260
5261 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE,
5262 sizeof(enable), &enable, HCI_CMD_TIMEOUT);
5263}
5264
5265static void set_ext_conn_params(struct hci_conn *conn,
5266 struct hci_cp_le_ext_conn_param *p)
5267{
5268 struct hci_dev *hdev = conn->hdev;
5269
5270 memset(p, 0, sizeof(*p));
5271
5272 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
5273 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
5274 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
5275 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
5276 p->conn_latency = cpu_to_le16(conn->le_conn_latency);
5277 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
5278 p->min_ce_len = cpu_to_le16(0x0000);
5279 p->max_ce_len = cpu_to_le16(0x0000);
5280}
5281
89a0b8b9
LAD
5282static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
5283 struct hci_conn *conn, u8 own_addr_type)
8e8b92ee
LAD
5284{
5285 struct hci_cp_le_ext_create_conn *cp;
5286 struct hci_cp_le_ext_conn_param *p;
5287 u8 data[sizeof(*cp) + sizeof(*p) * 3];
5288 u32 plen;
5289
5290 cp = (void *)data;
5291 p = (void *)cp->data;
5292
5293 memset(cp, 0, sizeof(*cp));
5294
5295 bacpy(&cp->peer_addr, &conn->dst);
5296 cp->peer_addr_type = conn->dst_type;
5297 cp->own_addr_type = own_addr_type;
5298
5299 plen = sizeof(*cp);
5300
5301 if (scan_1m(hdev)) {
5302 cp->phys |= LE_SCAN_PHY_1M;
5303 set_ext_conn_params(conn, p);
5304
5305 p++;
5306 plen += sizeof(*p);
5307 }
5308
5309 if (scan_2m(hdev)) {
5310 cp->phys |= LE_SCAN_PHY_2M;
5311 set_ext_conn_params(conn, p);
5312
5313 p++;
5314 plen += sizeof(*p);
5315 }
5316
5317 if (scan_coded(hdev)) {
5318 cp->phys |= LE_SCAN_PHY_CODED;
5319 set_ext_conn_params(conn, p);
5320
5321 plen += sizeof(*p);
5322 }
5323
6cd29ec6
LAD
5324 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN,
5325 plen, data,
5326 HCI_EV_LE_ENHANCED_CONN_COMPLETE,
a56a1138 5327 conn->conn_timeout, NULL);
8e8b92ee
LAD
5328}
5329
5330int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn)
5331{
5332 struct hci_cp_le_create_conn cp;
5333 struct hci_conn_params *params;
5334 u8 own_addr_type;
5335 int err;
5336
8e8b92ee
LAD
5337 /* If requested to connect as peripheral use directed advertising */
5338 if (conn->role == HCI_ROLE_SLAVE) {
76d0685b
LAD
5339 /* If we're active scanning and simultaneous roles is not
5340 * enabled simply reject the attempt.
8e8b92ee
LAD
5341 */
5342 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
4fc9857a 5343 hdev->le_scan_type == LE_SCAN_ACTIVE &&
76d0685b 5344 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) {
8e8b92ee
LAD
5345 hci_conn_del(conn);
5346 return -EBUSY;
5347 }
5348
4fc9857a
LAD
5349 /* Pause advertising while doing directed advertising. */
5350 hci_pause_advertising_sync(hdev);
5351
8e8b92ee
LAD
5352 err = hci_le_directed_advertising_sync(hdev, conn);
5353 goto done;
5354 }
5355
76d0685b
LAD
5356 /* Disable advertising if simultaneous roles is not in use. */
5357 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
4fc9857a
LAD
5358 hci_pause_advertising_sync(hdev);
5359
8e8b92ee
LAD
5360 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
5361 if (params) {
5362 conn->le_conn_min_interval = params->conn_min_interval;
5363 conn->le_conn_max_interval = params->conn_max_interval;
5364 conn->le_conn_latency = params->conn_latency;
5365 conn->le_supv_timeout = params->supervision_timeout;
5366 } else {
5367 conn->le_conn_min_interval = hdev->le_conn_min_interval;
5368 conn->le_conn_max_interval = hdev->le_conn_max_interval;
5369 conn->le_conn_latency = hdev->le_conn_latency;
5370 conn->le_supv_timeout = hdev->le_supv_timeout;
5371 }
5372
5373 /* If controller is scanning, we stop it since some controllers are
5374 * not able to scan and connect at the same time. Also set the
5375 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
5376 * handler for scan disabling knows to set the correct discovery
5377 * state.
5378 */
5379 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
5380 hci_scan_disable_sync(hdev);
5381 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
5382 }
5383
5384 /* Update random address, but set require_privacy to false so
5385 * that we never connect with an non-resolvable address.
5386 */
5387 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn),
5388 &own_addr_type);
5389 if (err)
5390 goto done;
5391
5392 if (use_ext_conn(hdev)) {
5393 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
5394 goto done;
5395 }
5396
5397 memset(&cp, 0, sizeof(cp));
5398
5399 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
5400 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
5401
5402 bacpy(&cp.peer_addr, &conn->dst);
5403 cp.peer_addr_type = conn->dst_type;
5404 cp.own_address_type = own_addr_type;
5405 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
5406 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
5407 cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
5408 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
5409 cp.min_ce_len = cpu_to_le16(0x0000);
5410 cp.max_ce_len = cpu_to_le16(0x0000);
5411
a56a1138
LAD
5412 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
5413 *
5414 * If this event is unmasked and the HCI_LE_Connection_Complete event
5415 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is
5416 * sent when a new connection has been created.
5417 */
6cd29ec6 5418 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN,
a56a1138
LAD
5419 sizeof(cp), &cp,
5420 use_enhanced_conn_complete(hdev) ?
5421 HCI_EV_LE_ENHANCED_CONN_COMPLETE :
5422 HCI_EV_LE_CONN_COMPLETE,
5423 conn->conn_timeout, NULL);
8e8b92ee
LAD
5424
5425done:
4fc9857a 5426 /* Re-enable advertising after the connection attempt is finished. */
8e8b92ee
LAD
5427 hci_resume_advertising_sync(hdev);
5428 return err;
5429}