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