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