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