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