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