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%4.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%4.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 void le_scan_restart(struct work_struct *work) 417 { 418 struct hci_dev *hdev = container_of(work, struct hci_dev, 419 le_scan_restart.work); 420 unsigned long timeout, duration, scan_start, now; 421 int status; 422 423 bt_dev_dbg(hdev, ""); 424 425 status = hci_le_scan_restart_sync(hdev); 426 if (status) { 427 bt_dev_err(hdev, "failed to restart LE scan: status %d", 428 status); 429 return; 430 } 431 432 hci_dev_lock(hdev); 433 434 if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) || 435 !hdev->discovery.scan_start) 436 goto unlock; 437 438 /* When the scan was started, hdev->le_scan_disable has been queued 439 * after duration from scan_start. During scan restart this job 440 * has been canceled, and we need to queue it again after proper 441 * timeout, to make sure that scan does not run indefinitely. 442 */ 443 duration = hdev->discovery.scan_duration; 444 scan_start = hdev->discovery.scan_start; 445 now = jiffies; 446 if (now - scan_start <= duration) { 447 int elapsed; 448 449 if (now >= scan_start) 450 elapsed = now - scan_start; 451 else 452 elapsed = ULONG_MAX - scan_start + now; 453 454 timeout = duration - elapsed; 455 } else { 456 timeout = 0; 457 } 458 459 queue_delayed_work(hdev->req_workqueue, 460 &hdev->le_scan_disable, timeout); 461 462 unlock: 463 hci_dev_unlock(hdev); 464 } 465 466 static int reenable_adv_sync(struct hci_dev *hdev, void *data) 467 { 468 bt_dev_dbg(hdev, ""); 469 470 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) && 471 list_empty(&hdev->adv_instances)) 472 return 0; 473 474 if (hdev->cur_adv_instance) { 475 return hci_schedule_adv_instance_sync(hdev, 476 hdev->cur_adv_instance, 477 true); 478 } else { 479 if (ext_adv_capable(hdev)) { 480 hci_start_ext_adv_sync(hdev, 0x00); 481 } else { 482 hci_update_adv_data_sync(hdev, 0x00); 483 hci_update_scan_rsp_data_sync(hdev, 0x00); 484 hci_enable_advertising_sync(hdev); 485 } 486 } 487 488 return 0; 489 } 490 491 static void reenable_adv(struct work_struct *work) 492 { 493 struct hci_dev *hdev = container_of(work, struct hci_dev, 494 reenable_adv_work); 495 int status; 496 497 bt_dev_dbg(hdev, ""); 498 499 hci_dev_lock(hdev); 500 501 status = hci_cmd_sync_queue(hdev, reenable_adv_sync, NULL, NULL); 502 if (status) 503 bt_dev_err(hdev, "failed to reenable ADV: %d", status); 504 505 hci_dev_unlock(hdev); 506 } 507 508 static void cancel_adv_timeout(struct hci_dev *hdev) 509 { 510 if (hdev->adv_instance_timeout) { 511 hdev->adv_instance_timeout = 0; 512 cancel_delayed_work(&hdev->adv_instance_expire); 513 } 514 } 515 516 /* For a single instance: 517 * - force == true: The instance will be removed even when its remaining 518 * lifetime is not zero. 519 * - force == false: the instance will be deactivated but kept stored unless 520 * the remaining lifetime is zero. 521 * 522 * For instance == 0x00: 523 * - force == true: All instances will be removed regardless of their timeout 524 * setting. 525 * - force == false: Only instances that have a timeout will be removed. 526 */ 527 int hci_clear_adv_instance_sync(struct hci_dev *hdev, struct sock *sk, 528 u8 instance, bool force) 529 { 530 struct adv_info *adv_instance, *n, *next_instance = NULL; 531 int err; 532 u8 rem_inst; 533 534 /* Cancel any timeout concerning the removed instance(s). */ 535 if (!instance || hdev->cur_adv_instance == instance) 536 cancel_adv_timeout(hdev); 537 538 /* Get the next instance to advertise BEFORE we remove 539 * the current one. This can be the same instance again 540 * if there is only one instance. 541 */ 542 if (instance && hdev->cur_adv_instance == instance) 543 next_instance = hci_get_next_instance(hdev, instance); 544 545 if (instance == 0x00) { 546 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, 547 list) { 548 if (!(force || adv_instance->timeout)) 549 continue; 550 551 rem_inst = adv_instance->instance; 552 err = hci_remove_adv_instance(hdev, rem_inst); 553 if (!err) 554 mgmt_advertising_removed(sk, hdev, rem_inst); 555 } 556 } else { 557 adv_instance = hci_find_adv_instance(hdev, instance); 558 559 if (force || (adv_instance && adv_instance->timeout && 560 !adv_instance->remaining_time)) { 561 /* Don't advertise a removed instance. */ 562 if (next_instance && 563 next_instance->instance == instance) 564 next_instance = NULL; 565 566 err = hci_remove_adv_instance(hdev, instance); 567 if (!err) 568 mgmt_advertising_removed(sk, hdev, instance); 569 } 570 } 571 572 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING)) 573 return 0; 574 575 if (next_instance && !ext_adv_capable(hdev)) 576 return hci_schedule_adv_instance_sync(hdev, 577 next_instance->instance, 578 false); 579 580 return 0; 581 } 582 583 static int adv_timeout_expire_sync(struct hci_dev *hdev, void *data) 584 { 585 u8 instance = *(u8 *)data; 586 587 kfree(data); 588 589 hci_clear_adv_instance_sync(hdev, NULL, instance, false); 590 591 if (list_empty(&hdev->adv_instances)) 592 return hci_disable_advertising_sync(hdev); 593 594 return 0; 595 } 596 597 static void adv_timeout_expire(struct work_struct *work) 598 { 599 u8 *inst_ptr; 600 struct hci_dev *hdev = container_of(work, struct hci_dev, 601 adv_instance_expire.work); 602 603 bt_dev_dbg(hdev, ""); 604 605 hci_dev_lock(hdev); 606 607 hdev->adv_instance_timeout = 0; 608 609 if (hdev->cur_adv_instance == 0x00) 610 goto unlock; 611 612 inst_ptr = kmalloc(1, GFP_KERNEL); 613 if (!inst_ptr) 614 goto unlock; 615 616 *inst_ptr = hdev->cur_adv_instance; 617 hci_cmd_sync_queue(hdev, adv_timeout_expire_sync, inst_ptr, NULL); 618 619 unlock: 620 hci_dev_unlock(hdev); 621 } 622 623 void hci_cmd_sync_init(struct hci_dev *hdev) 624 { 625 INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work); 626 INIT_LIST_HEAD(&hdev->cmd_sync_work_list); 627 mutex_init(&hdev->cmd_sync_work_lock); 628 mutex_init(&hdev->unregister_lock); 629 630 INIT_WORK(&hdev->cmd_sync_cancel_work, hci_cmd_sync_cancel_work); 631 INIT_WORK(&hdev->reenable_adv_work, reenable_adv); 632 INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable); 633 INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart); 634 INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire); 635 } 636 637 void hci_cmd_sync_clear(struct hci_dev *hdev) 638 { 639 struct hci_cmd_sync_work_entry *entry, *tmp; 640 641 cancel_work_sync(&hdev->cmd_sync_work); 642 cancel_work_sync(&hdev->reenable_adv_work); 643 644 mutex_lock(&hdev->cmd_sync_work_lock); 645 list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) { 646 if (entry->destroy) 647 entry->destroy(hdev, entry->data, -ECANCELED); 648 649 list_del(&entry->list); 650 kfree(entry); 651 } 652 mutex_unlock(&hdev->cmd_sync_work_lock); 653 } 654 655 void __hci_cmd_sync_cancel(struct hci_dev *hdev, int err) 656 { 657 bt_dev_dbg(hdev, "err 0x%2.2x", err); 658 659 if (hdev->req_status == HCI_REQ_PEND) { 660 hdev->req_result = err; 661 hdev->req_status = HCI_REQ_CANCELED; 662 663 cancel_delayed_work_sync(&hdev->cmd_timer); 664 cancel_delayed_work_sync(&hdev->ncmd_timer); 665 atomic_set(&hdev->cmd_cnt, 1); 666 667 wake_up_interruptible(&hdev->req_wait_q); 668 } 669 } 670 671 void hci_cmd_sync_cancel(struct hci_dev *hdev, int err) 672 { 673 bt_dev_dbg(hdev, "err 0x%2.2x", err); 674 675 if (hdev->req_status == HCI_REQ_PEND) { 676 hdev->req_result = err; 677 hdev->req_status = HCI_REQ_CANCELED; 678 679 queue_work(hdev->workqueue, &hdev->cmd_sync_cancel_work); 680 } 681 } 682 EXPORT_SYMBOL(hci_cmd_sync_cancel); 683 684 /* Submit HCI command to be run in as cmd_sync_work: 685 * 686 * - hdev must _not_ be unregistered 687 */ 688 int hci_cmd_sync_submit(struct hci_dev *hdev, hci_cmd_sync_work_func_t func, 689 void *data, hci_cmd_sync_work_destroy_t destroy) 690 { 691 struct hci_cmd_sync_work_entry *entry; 692 int err = 0; 693 694 mutex_lock(&hdev->unregister_lock); 695 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) { 696 err = -ENODEV; 697 goto unlock; 698 } 699 700 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 701 if (!entry) { 702 err = -ENOMEM; 703 goto unlock; 704 } 705 entry->func = func; 706 entry->data = data; 707 entry->destroy = destroy; 708 709 mutex_lock(&hdev->cmd_sync_work_lock); 710 list_add_tail(&entry->list, &hdev->cmd_sync_work_list); 711 mutex_unlock(&hdev->cmd_sync_work_lock); 712 713 queue_work(hdev->req_workqueue, &hdev->cmd_sync_work); 714 715 unlock: 716 mutex_unlock(&hdev->unregister_lock); 717 return err; 718 } 719 EXPORT_SYMBOL(hci_cmd_sync_submit); 720 721 /* Queue HCI command: 722 * 723 * - hdev must be running 724 */ 725 int hci_cmd_sync_queue(struct hci_dev *hdev, hci_cmd_sync_work_func_t func, 726 void *data, hci_cmd_sync_work_destroy_t destroy) 727 { 728 /* Only queue command if hdev is running which means it had been opened 729 * and is either on init phase or is already up. 730 */ 731 if (!test_bit(HCI_RUNNING, &hdev->flags)) 732 return -ENETDOWN; 733 734 return hci_cmd_sync_submit(hdev, func, data, destroy); 735 } 736 EXPORT_SYMBOL(hci_cmd_sync_queue); 737 738 int hci_update_eir_sync(struct hci_dev *hdev) 739 { 740 struct hci_cp_write_eir cp; 741 742 bt_dev_dbg(hdev, ""); 743 744 if (!hdev_is_powered(hdev)) 745 return 0; 746 747 if (!lmp_ext_inq_capable(hdev)) 748 return 0; 749 750 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) 751 return 0; 752 753 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) 754 return 0; 755 756 memset(&cp, 0, sizeof(cp)); 757 758 eir_create(hdev, cp.data); 759 760 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) 761 return 0; 762 763 memcpy(hdev->eir, cp.data, sizeof(cp.data)); 764 765 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp, 766 HCI_CMD_TIMEOUT); 767 } 768 769 static u8 get_service_classes(struct hci_dev *hdev) 770 { 771 struct bt_uuid *uuid; 772 u8 val = 0; 773 774 list_for_each_entry(uuid, &hdev->uuids, list) 775 val |= uuid->svc_hint; 776 777 return val; 778 } 779 780 int hci_update_class_sync(struct hci_dev *hdev) 781 { 782 u8 cod[3]; 783 784 bt_dev_dbg(hdev, ""); 785 786 if (!hdev_is_powered(hdev)) 787 return 0; 788 789 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 790 return 0; 791 792 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) 793 return 0; 794 795 cod[0] = hdev->minor_class; 796 cod[1] = hdev->major_class; 797 cod[2] = get_service_classes(hdev); 798 799 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) 800 cod[1] |= 0x20; 801 802 if (memcmp(cod, hdev->dev_class, 3) == 0) 803 return 0; 804 805 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CLASS_OF_DEV, 806 sizeof(cod), cod, HCI_CMD_TIMEOUT); 807 } 808 809 static bool is_advertising_allowed(struct hci_dev *hdev, bool connectable) 810 { 811 /* If there is no connection we are OK to advertise. */ 812 if (hci_conn_num(hdev, LE_LINK) == 0) 813 return true; 814 815 /* Check le_states if there is any connection in peripheral role. */ 816 if (hdev->conn_hash.le_num_peripheral > 0) { 817 /* Peripheral connection state and non connectable mode 818 * bit 20. 819 */ 820 if (!connectable && !(hdev->le_states[2] & 0x10)) 821 return false; 822 823 /* Peripheral connection state and connectable mode bit 38 824 * and scannable bit 21. 825 */ 826 if (connectable && (!(hdev->le_states[4] & 0x40) || 827 !(hdev->le_states[2] & 0x20))) 828 return false; 829 } 830 831 /* Check le_states if there is any connection in central role. */ 832 if (hci_conn_num(hdev, LE_LINK) != hdev->conn_hash.le_num_peripheral) { 833 /* Central connection state and non connectable mode bit 18. */ 834 if (!connectable && !(hdev->le_states[2] & 0x02)) 835 return false; 836 837 /* Central connection state and connectable mode bit 35 and 838 * scannable 19. 839 */ 840 if (connectable && (!(hdev->le_states[4] & 0x08) || 841 !(hdev->le_states[2] & 0x08))) 842 return false; 843 } 844 845 return true; 846 } 847 848 static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags) 849 { 850 /* If privacy is not enabled don't use RPA */ 851 if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) 852 return false; 853 854 /* If basic privacy mode is enabled use RPA */ 855 if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) 856 return true; 857 858 /* If limited privacy mode is enabled don't use RPA if we're 859 * both discoverable and bondable. 860 */ 861 if ((flags & MGMT_ADV_FLAG_DISCOV) && 862 hci_dev_test_flag(hdev, HCI_BONDABLE)) 863 return false; 864 865 /* We're neither bondable nor discoverable in the limited 866 * privacy mode, therefore use RPA. 867 */ 868 return true; 869 } 870 871 static int hci_set_random_addr_sync(struct hci_dev *hdev, bdaddr_t *rpa) 872 { 873 /* If we're advertising or initiating an LE connection we can't 874 * go ahead and change the random address at this time. This is 875 * because the eventual initiator address used for the 876 * subsequently created connection will be undefined (some 877 * controllers use the new address and others the one we had 878 * when the operation started). 879 * 880 * In this kind of scenario skip the update and let the random 881 * address be updated at the next cycle. 882 */ 883 if (hci_dev_test_flag(hdev, HCI_LE_ADV) || 884 hci_lookup_le_connect(hdev)) { 885 bt_dev_dbg(hdev, "Deferring random address update"); 886 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); 887 return 0; 888 } 889 890 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_RANDOM_ADDR, 891 6, rpa, HCI_CMD_TIMEOUT); 892 } 893 894 int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy, 895 bool rpa, u8 *own_addr_type) 896 { 897 int err; 898 899 /* If privacy is enabled use a resolvable private address. If 900 * current RPA has expired or there is something else than 901 * the current RPA in use, then generate a new one. 902 */ 903 if (rpa) { 904 /* If Controller supports LL Privacy use own address type is 905 * 0x03 906 */ 907 if (use_ll_privacy(hdev)) 908 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED; 909 else 910 *own_addr_type = ADDR_LE_DEV_RANDOM; 911 912 /* Check if RPA is valid */ 913 if (rpa_valid(hdev)) 914 return 0; 915 916 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa); 917 if (err < 0) { 918 bt_dev_err(hdev, "failed to generate new RPA"); 919 return err; 920 } 921 922 err = hci_set_random_addr_sync(hdev, &hdev->rpa); 923 if (err) 924 return err; 925 926 return 0; 927 } 928 929 /* In case of required privacy without resolvable private address, 930 * use an non-resolvable private address. This is useful for active 931 * scanning and non-connectable advertising. 932 */ 933 if (require_privacy) { 934 bdaddr_t nrpa; 935 936 while (true) { 937 /* The non-resolvable private address is generated 938 * from random six bytes with the two most significant 939 * bits cleared. 940 */ 941 get_random_bytes(&nrpa, 6); 942 nrpa.b[5] &= 0x3f; 943 944 /* The non-resolvable private address shall not be 945 * equal to the public address. 946 */ 947 if (bacmp(&hdev->bdaddr, &nrpa)) 948 break; 949 } 950 951 *own_addr_type = ADDR_LE_DEV_RANDOM; 952 953 return hci_set_random_addr_sync(hdev, &nrpa); 954 } 955 956 /* If forcing static address is in use or there is no public 957 * address use the static address as random address (but skip 958 * the HCI command if the current random address is already the 959 * static one. 960 * 961 * In case BR/EDR has been disabled on a dual-mode controller 962 * and a static address has been configured, then use that 963 * address instead of the public BR/EDR address. 964 */ 965 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || 966 !bacmp(&hdev->bdaddr, BDADDR_ANY) || 967 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && 968 bacmp(&hdev->static_addr, BDADDR_ANY))) { 969 *own_addr_type = ADDR_LE_DEV_RANDOM; 970 if (bacmp(&hdev->static_addr, &hdev->random_addr)) 971 return hci_set_random_addr_sync(hdev, 972 &hdev->static_addr); 973 return 0; 974 } 975 976 /* Neither privacy nor static address is being used so use a 977 * public address. 978 */ 979 *own_addr_type = ADDR_LE_DEV_PUBLIC; 980 981 return 0; 982 } 983 984 static int hci_disable_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) 985 { 986 struct hci_cp_le_set_ext_adv_enable *cp; 987 struct hci_cp_ext_adv_set *set; 988 u8 data[sizeof(*cp) + sizeof(*set) * 1]; 989 u8 size; 990 991 /* If request specifies an instance that doesn't exist, fail */ 992 if (instance > 0) { 993 struct adv_info *adv; 994 995 adv = hci_find_adv_instance(hdev, instance); 996 if (!adv) 997 return -EINVAL; 998 999 /* If not enabled there is nothing to do */ 1000 if (!adv->enabled) 1001 return 0; 1002 } 1003 1004 memset(data, 0, sizeof(data)); 1005 1006 cp = (void *)data; 1007 set = (void *)cp->data; 1008 1009 /* Instance 0x00 indicates all advertising instances will be disabled */ 1010 cp->num_of_sets = !!instance; 1011 cp->enable = 0x00; 1012 1013 set->handle = instance; 1014 1015 size = sizeof(*cp) + sizeof(*set) * cp->num_of_sets; 1016 1017 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, 1018 size, data, HCI_CMD_TIMEOUT); 1019 } 1020 1021 static int hci_set_adv_set_random_addr_sync(struct hci_dev *hdev, u8 instance, 1022 bdaddr_t *random_addr) 1023 { 1024 struct hci_cp_le_set_adv_set_rand_addr cp; 1025 int err; 1026 1027 if (!instance) { 1028 /* Instance 0x00 doesn't have an adv_info, instead it uses 1029 * hdev->random_addr to track its address so whenever it needs 1030 * to be updated this also set the random address since 1031 * hdev->random_addr is shared with scan state machine. 1032 */ 1033 err = hci_set_random_addr_sync(hdev, random_addr); 1034 if (err) 1035 return err; 1036 } 1037 1038 memset(&cp, 0, sizeof(cp)); 1039 1040 cp.handle = instance; 1041 bacpy(&cp.bdaddr, random_addr); 1042 1043 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR, 1044 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1045 } 1046 1047 int hci_setup_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance) 1048 { 1049 struct hci_cp_le_set_ext_adv_params cp; 1050 bool connectable; 1051 u32 flags; 1052 bdaddr_t random_addr; 1053 u8 own_addr_type; 1054 int err; 1055 struct adv_info *adv; 1056 bool secondary_adv; 1057 1058 if (instance > 0) { 1059 adv = hci_find_adv_instance(hdev, instance); 1060 if (!adv) 1061 return -EINVAL; 1062 } else { 1063 adv = NULL; 1064 } 1065 1066 /* Updating parameters of an active instance will return a 1067 * Command Disallowed error, so we must first disable the 1068 * instance if it is active. 1069 */ 1070 if (adv && !adv->pending) { 1071 err = hci_disable_ext_adv_instance_sync(hdev, instance); 1072 if (err) 1073 return err; 1074 } 1075 1076 flags = hci_adv_instance_flags(hdev, instance); 1077 1078 /* If the "connectable" instance flag was not set, then choose between 1079 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. 1080 */ 1081 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || 1082 mgmt_get_connectable(hdev); 1083 1084 if (!is_advertising_allowed(hdev, connectable)) 1085 return -EPERM; 1086 1087 /* Set require_privacy to true only when non-connectable 1088 * advertising is used. In that case it is fine to use a 1089 * non-resolvable private address. 1090 */ 1091 err = hci_get_random_address(hdev, !connectable, 1092 adv_use_rpa(hdev, flags), adv, 1093 &own_addr_type, &random_addr); 1094 if (err < 0) 1095 return err; 1096 1097 memset(&cp, 0, sizeof(cp)); 1098 1099 if (adv) { 1100 hci_cpu_to_le24(adv->min_interval, cp.min_interval); 1101 hci_cpu_to_le24(adv->max_interval, cp.max_interval); 1102 cp.tx_power = adv->tx_power; 1103 } else { 1104 hci_cpu_to_le24(hdev->le_adv_min_interval, cp.min_interval); 1105 hci_cpu_to_le24(hdev->le_adv_max_interval, cp.max_interval); 1106 cp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; 1107 } 1108 1109 secondary_adv = (flags & MGMT_ADV_FLAG_SEC_MASK); 1110 1111 if (connectable) { 1112 if (secondary_adv) 1113 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_CONN_IND); 1114 else 1115 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_IND); 1116 } else if (hci_adv_instance_is_scannable(hdev, instance) || 1117 (flags & MGMT_ADV_PARAM_SCAN_RSP)) { 1118 if (secondary_adv) 1119 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_SCAN_IND); 1120 else 1121 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_SCAN_IND); 1122 } else { 1123 if (secondary_adv) 1124 cp.evt_properties = cpu_to_le16(LE_EXT_ADV_NON_CONN_IND); 1125 else 1126 cp.evt_properties = cpu_to_le16(LE_LEGACY_NONCONN_IND); 1127 } 1128 1129 /* If Own_Address_Type equals 0x02 or 0x03, the Peer_Address parameter 1130 * contains the peer’s Identity Address and the Peer_Address_Type 1131 * parameter contains the peer’s Identity Type (i.e., 0x00 or 0x01). 1132 * These parameters are used to locate the corresponding local IRK in 1133 * the resolving list; this IRK is used to generate their own address 1134 * used in the advertisement. 1135 */ 1136 if (own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) 1137 hci_copy_identity_address(hdev, &cp.peer_addr, 1138 &cp.peer_addr_type); 1139 1140 cp.own_addr_type = own_addr_type; 1141 cp.channel_map = hdev->le_adv_channel_map; 1142 cp.handle = instance; 1143 1144 if (flags & MGMT_ADV_FLAG_SEC_2M) { 1145 cp.primary_phy = HCI_ADV_PHY_1M; 1146 cp.secondary_phy = HCI_ADV_PHY_2M; 1147 } else if (flags & MGMT_ADV_FLAG_SEC_CODED) { 1148 cp.primary_phy = HCI_ADV_PHY_CODED; 1149 cp.secondary_phy = HCI_ADV_PHY_CODED; 1150 } else { 1151 /* In all other cases use 1M */ 1152 cp.primary_phy = HCI_ADV_PHY_1M; 1153 cp.secondary_phy = HCI_ADV_PHY_1M; 1154 } 1155 1156 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, 1157 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1158 if (err) 1159 return err; 1160 1161 if ((own_addr_type == ADDR_LE_DEV_RANDOM || 1162 own_addr_type == ADDR_LE_DEV_RANDOM_RESOLVED) && 1163 bacmp(&random_addr, BDADDR_ANY)) { 1164 /* Check if random address need to be updated */ 1165 if (adv) { 1166 if (!bacmp(&random_addr, &adv->random_addr)) 1167 return 0; 1168 } else { 1169 if (!bacmp(&random_addr, &hdev->random_addr)) 1170 return 0; 1171 } 1172 1173 return hci_set_adv_set_random_addr_sync(hdev, instance, 1174 &random_addr); 1175 } 1176 1177 return 0; 1178 } 1179 1180 static int hci_set_ext_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) 1181 { 1182 struct { 1183 struct hci_cp_le_set_ext_scan_rsp_data cp; 1184 u8 data[HCI_MAX_EXT_AD_LENGTH]; 1185 } pdu; 1186 u8 len; 1187 struct adv_info *adv = NULL; 1188 int err; 1189 1190 memset(&pdu, 0, sizeof(pdu)); 1191 1192 if (instance) { 1193 adv = hci_find_adv_instance(hdev, instance); 1194 if (!adv || !adv->scan_rsp_changed) 1195 return 0; 1196 } 1197 1198 len = eir_create_scan_rsp(hdev, instance, pdu.data); 1199 1200 pdu.cp.handle = instance; 1201 pdu.cp.length = len; 1202 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; 1203 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; 1204 1205 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_RSP_DATA, 1206 sizeof(pdu.cp) + len, &pdu.cp, 1207 HCI_CMD_TIMEOUT); 1208 if (err) 1209 return err; 1210 1211 if (adv) { 1212 adv->scan_rsp_changed = false; 1213 } else { 1214 memcpy(hdev->scan_rsp_data, pdu.data, len); 1215 hdev->scan_rsp_data_len = len; 1216 } 1217 1218 return 0; 1219 } 1220 1221 static int __hci_set_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) 1222 { 1223 struct hci_cp_le_set_scan_rsp_data cp; 1224 u8 len; 1225 1226 memset(&cp, 0, sizeof(cp)); 1227 1228 len = eir_create_scan_rsp(hdev, instance, cp.data); 1229 1230 if (hdev->scan_rsp_data_len == len && 1231 !memcmp(cp.data, hdev->scan_rsp_data, len)) 1232 return 0; 1233 1234 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); 1235 hdev->scan_rsp_data_len = len; 1236 1237 cp.length = len; 1238 1239 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_RSP_DATA, 1240 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1241 } 1242 1243 int hci_update_scan_rsp_data_sync(struct hci_dev *hdev, u8 instance) 1244 { 1245 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 1246 return 0; 1247 1248 if (ext_adv_capable(hdev)) 1249 return hci_set_ext_scan_rsp_data_sync(hdev, instance); 1250 1251 return __hci_set_scan_rsp_data_sync(hdev, instance); 1252 } 1253 1254 int hci_enable_ext_advertising_sync(struct hci_dev *hdev, u8 instance) 1255 { 1256 struct hci_cp_le_set_ext_adv_enable *cp; 1257 struct hci_cp_ext_adv_set *set; 1258 u8 data[sizeof(*cp) + sizeof(*set) * 1]; 1259 struct adv_info *adv; 1260 1261 if (instance > 0) { 1262 adv = hci_find_adv_instance(hdev, instance); 1263 if (!adv) 1264 return -EINVAL; 1265 /* If already enabled there is nothing to do */ 1266 if (adv->enabled) 1267 return 0; 1268 } else { 1269 adv = NULL; 1270 } 1271 1272 cp = (void *)data; 1273 set = (void *)cp->data; 1274 1275 memset(cp, 0, sizeof(*cp)); 1276 1277 cp->enable = 0x01; 1278 cp->num_of_sets = 0x01; 1279 1280 memset(set, 0, sizeof(*set)); 1281 1282 set->handle = instance; 1283 1284 /* Set duration per instance since controller is responsible for 1285 * scheduling it. 1286 */ 1287 if (adv && adv->timeout) { 1288 u16 duration = adv->timeout * MSEC_PER_SEC; 1289 1290 /* Time = N * 10 ms */ 1291 set->duration = cpu_to_le16(duration / 10); 1292 } 1293 1294 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, 1295 sizeof(*cp) + 1296 sizeof(*set) * cp->num_of_sets, 1297 data, HCI_CMD_TIMEOUT); 1298 } 1299 1300 int hci_start_ext_adv_sync(struct hci_dev *hdev, u8 instance) 1301 { 1302 int err; 1303 1304 err = hci_setup_ext_adv_instance_sync(hdev, instance); 1305 if (err) 1306 return err; 1307 1308 err = hci_set_ext_scan_rsp_data_sync(hdev, instance); 1309 if (err) 1310 return err; 1311 1312 return hci_enable_ext_advertising_sync(hdev, instance); 1313 } 1314 1315 static int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance) 1316 { 1317 struct hci_cp_le_set_per_adv_enable cp; 1318 struct adv_info *adv = NULL; 1319 1320 /* If periodic advertising already disabled there is nothing to do. */ 1321 adv = hci_find_adv_instance(hdev, instance); 1322 if (!adv || !adv->periodic || !adv->enabled) 1323 return 0; 1324 1325 memset(&cp, 0, sizeof(cp)); 1326 1327 cp.enable = 0x00; 1328 cp.handle = instance; 1329 1330 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE, 1331 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1332 } 1333 1334 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance, 1335 u16 min_interval, u16 max_interval) 1336 { 1337 struct hci_cp_le_set_per_adv_params cp; 1338 1339 memset(&cp, 0, sizeof(cp)); 1340 1341 if (!min_interval) 1342 min_interval = DISCOV_LE_PER_ADV_INT_MIN; 1343 1344 if (!max_interval) 1345 max_interval = DISCOV_LE_PER_ADV_INT_MAX; 1346 1347 cp.handle = instance; 1348 cp.min_interval = cpu_to_le16(min_interval); 1349 cp.max_interval = cpu_to_le16(max_interval); 1350 cp.periodic_properties = 0x0000; 1351 1352 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS, 1353 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1354 } 1355 1356 static int hci_set_per_adv_data_sync(struct hci_dev *hdev, u8 instance) 1357 { 1358 struct { 1359 struct hci_cp_le_set_per_adv_data cp; 1360 u8 data[HCI_MAX_PER_AD_LENGTH]; 1361 } pdu; 1362 u8 len; 1363 1364 memset(&pdu, 0, sizeof(pdu)); 1365 1366 if (instance) { 1367 struct adv_info *adv = hci_find_adv_instance(hdev, instance); 1368 1369 if (!adv || !adv->periodic) 1370 return 0; 1371 } 1372 1373 len = eir_create_per_adv_data(hdev, instance, pdu.data); 1374 1375 pdu.cp.length = len; 1376 pdu.cp.handle = instance; 1377 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; 1378 1379 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_DATA, 1380 sizeof(pdu.cp) + len, &pdu, 1381 HCI_CMD_TIMEOUT); 1382 } 1383 1384 static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance) 1385 { 1386 struct hci_cp_le_set_per_adv_enable cp; 1387 struct adv_info *adv = NULL; 1388 1389 /* If periodic advertising already enabled there is nothing to do. */ 1390 adv = hci_find_adv_instance(hdev, instance); 1391 if (adv && adv->periodic && adv->enabled) 1392 return 0; 1393 1394 memset(&cp, 0, sizeof(cp)); 1395 1396 cp.enable = 0x01; 1397 cp.handle = instance; 1398 1399 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE, 1400 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1401 } 1402 1403 /* Checks if periodic advertising data contains a Basic Announcement and if it 1404 * does generates a Broadcast ID and add Broadcast Announcement. 1405 */ 1406 static int hci_adv_bcast_annoucement(struct hci_dev *hdev, struct adv_info *adv) 1407 { 1408 u8 bid[3]; 1409 u8 ad[4 + 3]; 1410 1411 /* Skip if NULL adv as instance 0x00 is used for general purpose 1412 * advertising so it cannot used for the likes of Broadcast Announcement 1413 * as it can be overwritten at any point. 1414 */ 1415 if (!adv) 1416 return 0; 1417 1418 /* Check if PA data doesn't contains a Basic Audio Announcement then 1419 * there is nothing to do. 1420 */ 1421 if (!eir_get_service_data(adv->per_adv_data, adv->per_adv_data_len, 1422 0x1851, NULL)) 1423 return 0; 1424 1425 /* Check if advertising data already has a Broadcast Announcement since 1426 * the process may want to control the Broadcast ID directly and in that 1427 * case the kernel shall no interfere. 1428 */ 1429 if (eir_get_service_data(adv->adv_data, adv->adv_data_len, 0x1852, 1430 NULL)) 1431 return 0; 1432 1433 /* Generate Broadcast ID */ 1434 get_random_bytes(bid, sizeof(bid)); 1435 eir_append_service_data(ad, 0, 0x1852, bid, sizeof(bid)); 1436 hci_set_adv_instance_data(hdev, adv->instance, sizeof(ad), ad, 0, NULL); 1437 1438 return hci_update_adv_data_sync(hdev, adv->instance); 1439 } 1440 1441 int hci_start_per_adv_sync(struct hci_dev *hdev, u8 instance, u8 data_len, 1442 u8 *data, u32 flags, u16 min_interval, 1443 u16 max_interval, u16 sync_interval) 1444 { 1445 struct adv_info *adv = NULL; 1446 int err; 1447 bool added = false; 1448 1449 hci_disable_per_advertising_sync(hdev, instance); 1450 1451 if (instance) { 1452 adv = hci_find_adv_instance(hdev, instance); 1453 /* Create an instance if that could not be found */ 1454 if (!adv) { 1455 adv = hci_add_per_instance(hdev, instance, flags, 1456 data_len, data, 1457 sync_interval, 1458 sync_interval); 1459 if (IS_ERR(adv)) 1460 return PTR_ERR(adv); 1461 adv->pending = false; 1462 added = true; 1463 } 1464 } 1465 1466 /* Start advertising */ 1467 err = hci_start_ext_adv_sync(hdev, instance); 1468 if (err < 0) 1469 goto fail; 1470 1471 err = hci_adv_bcast_annoucement(hdev, adv); 1472 if (err < 0) 1473 goto fail; 1474 1475 err = hci_set_per_adv_params_sync(hdev, instance, min_interval, 1476 max_interval); 1477 if (err < 0) 1478 goto fail; 1479 1480 err = hci_set_per_adv_data_sync(hdev, instance); 1481 if (err < 0) 1482 goto fail; 1483 1484 err = hci_enable_per_advertising_sync(hdev, instance); 1485 if (err < 0) 1486 goto fail; 1487 1488 return 0; 1489 1490 fail: 1491 if (added) 1492 hci_remove_adv_instance(hdev, instance); 1493 1494 return err; 1495 } 1496 1497 static int hci_start_adv_sync(struct hci_dev *hdev, u8 instance) 1498 { 1499 int err; 1500 1501 if (ext_adv_capable(hdev)) 1502 return hci_start_ext_adv_sync(hdev, instance); 1503 1504 err = hci_update_adv_data_sync(hdev, instance); 1505 if (err) 1506 return err; 1507 1508 err = hci_update_scan_rsp_data_sync(hdev, instance); 1509 if (err) 1510 return err; 1511 1512 return hci_enable_advertising_sync(hdev); 1513 } 1514 1515 int hci_enable_advertising_sync(struct hci_dev *hdev) 1516 { 1517 struct adv_info *adv_instance; 1518 struct hci_cp_le_set_adv_param cp; 1519 u8 own_addr_type, enable = 0x01; 1520 bool connectable; 1521 u16 adv_min_interval, adv_max_interval; 1522 u32 flags; 1523 u8 status; 1524 1525 if (ext_adv_capable(hdev)) 1526 return hci_enable_ext_advertising_sync(hdev, 1527 hdev->cur_adv_instance); 1528 1529 flags = hci_adv_instance_flags(hdev, hdev->cur_adv_instance); 1530 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); 1531 1532 /* If the "connectable" instance flag was not set, then choose between 1533 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting. 1534 */ 1535 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) || 1536 mgmt_get_connectable(hdev); 1537 1538 if (!is_advertising_allowed(hdev, connectable)) 1539 return -EINVAL; 1540 1541 status = hci_disable_advertising_sync(hdev); 1542 if (status) 1543 return status; 1544 1545 /* Clear the HCI_LE_ADV bit temporarily so that the 1546 * hci_update_random_address knows that it's safe to go ahead 1547 * and write a new random address. The flag will be set back on 1548 * as soon as the SET_ADV_ENABLE HCI command completes. 1549 */ 1550 hci_dev_clear_flag(hdev, HCI_LE_ADV); 1551 1552 /* Set require_privacy to true only when non-connectable 1553 * advertising is used. In that case it is fine to use a 1554 * non-resolvable private address. 1555 */ 1556 status = hci_update_random_address_sync(hdev, !connectable, 1557 adv_use_rpa(hdev, flags), 1558 &own_addr_type); 1559 if (status) 1560 return status; 1561 1562 memset(&cp, 0, sizeof(cp)); 1563 1564 if (adv_instance) { 1565 adv_min_interval = adv_instance->min_interval; 1566 adv_max_interval = adv_instance->max_interval; 1567 } else { 1568 adv_min_interval = hdev->le_adv_min_interval; 1569 adv_max_interval = hdev->le_adv_max_interval; 1570 } 1571 1572 if (connectable) { 1573 cp.type = LE_ADV_IND; 1574 } else { 1575 if (hci_adv_instance_is_scannable(hdev, hdev->cur_adv_instance)) 1576 cp.type = LE_ADV_SCAN_IND; 1577 else 1578 cp.type = LE_ADV_NONCONN_IND; 1579 1580 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE) || 1581 hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { 1582 adv_min_interval = DISCOV_LE_FAST_ADV_INT_MIN; 1583 adv_max_interval = DISCOV_LE_FAST_ADV_INT_MAX; 1584 } 1585 } 1586 1587 cp.min_interval = cpu_to_le16(adv_min_interval); 1588 cp.max_interval = cpu_to_le16(adv_max_interval); 1589 cp.own_address_type = own_addr_type; 1590 cp.channel_map = hdev->le_adv_channel_map; 1591 1592 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM, 1593 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1594 if (status) 1595 return status; 1596 1597 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, 1598 sizeof(enable), &enable, HCI_CMD_TIMEOUT); 1599 } 1600 1601 static int enable_advertising_sync(struct hci_dev *hdev, void *data) 1602 { 1603 return hci_enable_advertising_sync(hdev); 1604 } 1605 1606 int hci_enable_advertising(struct hci_dev *hdev) 1607 { 1608 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) && 1609 list_empty(&hdev->adv_instances)) 1610 return 0; 1611 1612 return hci_cmd_sync_queue(hdev, enable_advertising_sync, NULL, NULL); 1613 } 1614 1615 int hci_remove_ext_adv_instance_sync(struct hci_dev *hdev, u8 instance, 1616 struct sock *sk) 1617 { 1618 int err; 1619 1620 if (!ext_adv_capable(hdev)) 1621 return 0; 1622 1623 err = hci_disable_ext_adv_instance_sync(hdev, instance); 1624 if (err) 1625 return err; 1626 1627 /* If request specifies an instance that doesn't exist, fail */ 1628 if (instance > 0 && !hci_find_adv_instance(hdev, instance)) 1629 return -EINVAL; 1630 1631 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_REMOVE_ADV_SET, 1632 sizeof(instance), &instance, 0, 1633 HCI_CMD_TIMEOUT, sk); 1634 } 1635 1636 static int remove_ext_adv_sync(struct hci_dev *hdev, void *data) 1637 { 1638 struct adv_info *adv = data; 1639 u8 instance = 0; 1640 1641 if (adv) 1642 instance = adv->instance; 1643 1644 return hci_remove_ext_adv_instance_sync(hdev, instance, NULL); 1645 } 1646 1647 int hci_remove_ext_adv_instance(struct hci_dev *hdev, u8 instance) 1648 { 1649 struct adv_info *adv = NULL; 1650 1651 if (instance) { 1652 adv = hci_find_adv_instance(hdev, instance); 1653 if (!adv) 1654 return -EINVAL; 1655 } 1656 1657 return hci_cmd_sync_queue(hdev, remove_ext_adv_sync, adv, NULL); 1658 } 1659 1660 int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason) 1661 { 1662 struct hci_cp_le_term_big cp; 1663 1664 memset(&cp, 0, sizeof(cp)); 1665 cp.handle = handle; 1666 cp.reason = reason; 1667 1668 return __hci_cmd_sync_status(hdev, HCI_OP_LE_TERM_BIG, 1669 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1670 } 1671 1672 static int hci_set_ext_adv_data_sync(struct hci_dev *hdev, u8 instance) 1673 { 1674 struct { 1675 struct hci_cp_le_set_ext_adv_data cp; 1676 u8 data[HCI_MAX_EXT_AD_LENGTH]; 1677 } pdu; 1678 u8 len; 1679 struct adv_info *adv = NULL; 1680 int err; 1681 1682 memset(&pdu, 0, sizeof(pdu)); 1683 1684 if (instance) { 1685 adv = hci_find_adv_instance(hdev, instance); 1686 if (!adv || !adv->adv_data_changed) 1687 return 0; 1688 } 1689 1690 len = eir_create_adv_data(hdev, instance, pdu.data); 1691 1692 pdu.cp.length = len; 1693 pdu.cp.handle = instance; 1694 pdu.cp.operation = LE_SET_ADV_DATA_OP_COMPLETE; 1695 pdu.cp.frag_pref = LE_SET_ADV_DATA_NO_FRAG; 1696 1697 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_DATA, 1698 sizeof(pdu.cp) + len, &pdu.cp, 1699 HCI_CMD_TIMEOUT); 1700 if (err) 1701 return err; 1702 1703 /* Update data if the command succeed */ 1704 if (adv) { 1705 adv->adv_data_changed = false; 1706 } else { 1707 memcpy(hdev->adv_data, pdu.data, len); 1708 hdev->adv_data_len = len; 1709 } 1710 1711 return 0; 1712 } 1713 1714 static int hci_set_adv_data_sync(struct hci_dev *hdev, u8 instance) 1715 { 1716 struct hci_cp_le_set_adv_data cp; 1717 u8 len; 1718 1719 memset(&cp, 0, sizeof(cp)); 1720 1721 len = eir_create_adv_data(hdev, instance, cp.data); 1722 1723 /* There's nothing to do if the data hasn't changed */ 1724 if (hdev->adv_data_len == len && 1725 memcmp(cp.data, hdev->adv_data, len) == 0) 1726 return 0; 1727 1728 memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); 1729 hdev->adv_data_len = len; 1730 1731 cp.length = len; 1732 1733 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_DATA, 1734 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1735 } 1736 1737 int hci_update_adv_data_sync(struct hci_dev *hdev, u8 instance) 1738 { 1739 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 1740 return 0; 1741 1742 if (ext_adv_capable(hdev)) 1743 return hci_set_ext_adv_data_sync(hdev, instance); 1744 1745 return hci_set_adv_data_sync(hdev, instance); 1746 } 1747 1748 int hci_schedule_adv_instance_sync(struct hci_dev *hdev, u8 instance, 1749 bool force) 1750 { 1751 struct adv_info *adv = NULL; 1752 u16 timeout; 1753 1754 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) && !ext_adv_capable(hdev)) 1755 return -EPERM; 1756 1757 if (hdev->adv_instance_timeout) 1758 return -EBUSY; 1759 1760 adv = hci_find_adv_instance(hdev, instance); 1761 if (!adv) 1762 return -ENOENT; 1763 1764 /* A zero timeout means unlimited advertising. As long as there is 1765 * only one instance, duration should be ignored. We still set a timeout 1766 * in case further instances are being added later on. 1767 * 1768 * If the remaining lifetime of the instance is more than the duration 1769 * then the timeout corresponds to the duration, otherwise it will be 1770 * reduced to the remaining instance lifetime. 1771 */ 1772 if (adv->timeout == 0 || adv->duration <= adv->remaining_time) 1773 timeout = adv->duration; 1774 else 1775 timeout = adv->remaining_time; 1776 1777 /* The remaining time is being reduced unless the instance is being 1778 * advertised without time limit. 1779 */ 1780 if (adv->timeout) 1781 adv->remaining_time = adv->remaining_time - timeout; 1782 1783 /* Only use work for scheduling instances with legacy advertising */ 1784 if (!ext_adv_capable(hdev)) { 1785 hdev->adv_instance_timeout = timeout; 1786 queue_delayed_work(hdev->req_workqueue, 1787 &hdev->adv_instance_expire, 1788 msecs_to_jiffies(timeout * 1000)); 1789 } 1790 1791 /* If we're just re-scheduling the same instance again then do not 1792 * execute any HCI commands. This happens when a single instance is 1793 * being advertised. 1794 */ 1795 if (!force && hdev->cur_adv_instance == instance && 1796 hci_dev_test_flag(hdev, HCI_LE_ADV)) 1797 return 0; 1798 1799 hdev->cur_adv_instance = instance; 1800 1801 return hci_start_adv_sync(hdev, instance); 1802 } 1803 1804 static int hci_clear_adv_sets_sync(struct hci_dev *hdev, struct sock *sk) 1805 { 1806 int err; 1807 1808 if (!ext_adv_capable(hdev)) 1809 return 0; 1810 1811 /* Disable instance 0x00 to disable all instances */ 1812 err = hci_disable_ext_adv_instance_sync(hdev, 0x00); 1813 if (err) 1814 return err; 1815 1816 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CLEAR_ADV_SETS, 1817 0, NULL, 0, HCI_CMD_TIMEOUT, sk); 1818 } 1819 1820 static int hci_clear_adv_sync(struct hci_dev *hdev, struct sock *sk, bool force) 1821 { 1822 struct adv_info *adv, *n; 1823 int err = 0; 1824 1825 if (ext_adv_capable(hdev)) 1826 /* Remove all existing sets */ 1827 err = hci_clear_adv_sets_sync(hdev, sk); 1828 if (ext_adv_capable(hdev)) 1829 return err; 1830 1831 /* This is safe as long as there is no command send while the lock is 1832 * held. 1833 */ 1834 hci_dev_lock(hdev); 1835 1836 /* Cleanup non-ext instances */ 1837 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) { 1838 u8 instance = adv->instance; 1839 int err; 1840 1841 if (!(force || adv->timeout)) 1842 continue; 1843 1844 err = hci_remove_adv_instance(hdev, instance); 1845 if (!err) 1846 mgmt_advertising_removed(sk, hdev, instance); 1847 } 1848 1849 hci_dev_unlock(hdev); 1850 1851 return 0; 1852 } 1853 1854 static int hci_remove_adv_sync(struct hci_dev *hdev, u8 instance, 1855 struct sock *sk) 1856 { 1857 int err = 0; 1858 1859 /* If we use extended advertising, instance has to be removed first. */ 1860 if (ext_adv_capable(hdev)) 1861 err = hci_remove_ext_adv_instance_sync(hdev, instance, sk); 1862 if (ext_adv_capable(hdev)) 1863 return err; 1864 1865 /* This is safe as long as there is no command send while the lock is 1866 * held. 1867 */ 1868 hci_dev_lock(hdev); 1869 1870 err = hci_remove_adv_instance(hdev, instance); 1871 if (!err) 1872 mgmt_advertising_removed(sk, hdev, instance); 1873 1874 hci_dev_unlock(hdev); 1875 1876 return err; 1877 } 1878 1879 /* For a single instance: 1880 * - force == true: The instance will be removed even when its remaining 1881 * lifetime is not zero. 1882 * - force == false: the instance will be deactivated but kept stored unless 1883 * the remaining lifetime is zero. 1884 * 1885 * For instance == 0x00: 1886 * - force == true: All instances will be removed regardless of their timeout 1887 * setting. 1888 * - force == false: Only instances that have a timeout will be removed. 1889 */ 1890 int hci_remove_advertising_sync(struct hci_dev *hdev, struct sock *sk, 1891 u8 instance, bool force) 1892 { 1893 struct adv_info *next = NULL; 1894 int err; 1895 1896 /* Cancel any timeout concerning the removed instance(s). */ 1897 if (!instance || hdev->cur_adv_instance == instance) 1898 cancel_adv_timeout(hdev); 1899 1900 /* Get the next instance to advertise BEFORE we remove 1901 * the current one. This can be the same instance again 1902 * if there is only one instance. 1903 */ 1904 if (hdev->cur_adv_instance == instance) 1905 next = hci_get_next_instance(hdev, instance); 1906 1907 if (!instance) { 1908 err = hci_clear_adv_sync(hdev, sk, force); 1909 if (err) 1910 return err; 1911 } else { 1912 struct adv_info *adv = hci_find_adv_instance(hdev, instance); 1913 1914 if (force || (adv && adv->timeout && !adv->remaining_time)) { 1915 /* Don't advertise a removed instance. */ 1916 if (next && next->instance == instance) 1917 next = NULL; 1918 1919 err = hci_remove_adv_sync(hdev, instance, sk); 1920 if (err) 1921 return err; 1922 } 1923 } 1924 1925 if (!hdev_is_powered(hdev) || hci_dev_test_flag(hdev, HCI_ADVERTISING)) 1926 return 0; 1927 1928 if (next && !ext_adv_capable(hdev)) 1929 hci_schedule_adv_instance_sync(hdev, next->instance, false); 1930 1931 return 0; 1932 } 1933 1934 int hci_read_rssi_sync(struct hci_dev *hdev, __le16 handle) 1935 { 1936 struct hci_cp_read_rssi cp; 1937 1938 cp.handle = handle; 1939 return __hci_cmd_sync_status(hdev, HCI_OP_READ_RSSI, 1940 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1941 } 1942 1943 int hci_read_clock_sync(struct hci_dev *hdev, struct hci_cp_read_clock *cp) 1944 { 1945 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLOCK, 1946 sizeof(*cp), cp, HCI_CMD_TIMEOUT); 1947 } 1948 1949 int hci_read_tx_power_sync(struct hci_dev *hdev, __le16 handle, u8 type) 1950 { 1951 struct hci_cp_read_tx_power cp; 1952 1953 cp.handle = handle; 1954 cp.type = type; 1955 return __hci_cmd_sync_status(hdev, HCI_OP_READ_TX_POWER, 1956 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1957 } 1958 1959 int hci_disable_advertising_sync(struct hci_dev *hdev) 1960 { 1961 u8 enable = 0x00; 1962 int err = 0; 1963 1964 /* If controller is not advertising we are done. */ 1965 if (!hci_dev_test_flag(hdev, HCI_LE_ADV)) 1966 return 0; 1967 1968 if (ext_adv_capable(hdev)) 1969 err = hci_disable_ext_adv_instance_sync(hdev, 0x00); 1970 if (ext_adv_capable(hdev)) 1971 return err; 1972 1973 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, 1974 sizeof(enable), &enable, HCI_CMD_TIMEOUT); 1975 } 1976 1977 static int hci_le_set_ext_scan_enable_sync(struct hci_dev *hdev, u8 val, 1978 u8 filter_dup) 1979 { 1980 struct hci_cp_le_set_ext_scan_enable cp; 1981 1982 memset(&cp, 0, sizeof(cp)); 1983 cp.enable = val; 1984 1985 if (hci_dev_test_flag(hdev, HCI_MESH)) 1986 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE; 1987 else 1988 cp.filter_dup = filter_dup; 1989 1990 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE, 1991 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 1992 } 1993 1994 static int hci_le_set_scan_enable_sync(struct hci_dev *hdev, u8 val, 1995 u8 filter_dup) 1996 { 1997 struct hci_cp_le_set_scan_enable cp; 1998 1999 if (use_ext_scan(hdev)) 2000 return hci_le_set_ext_scan_enable_sync(hdev, val, filter_dup); 2001 2002 memset(&cp, 0, sizeof(cp)); 2003 cp.enable = val; 2004 2005 if (val && hci_dev_test_flag(hdev, HCI_MESH)) 2006 cp.filter_dup = LE_SCAN_FILTER_DUP_DISABLE; 2007 else 2008 cp.filter_dup = filter_dup; 2009 2010 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_ENABLE, 2011 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2012 } 2013 2014 static int hci_le_set_addr_resolution_enable_sync(struct hci_dev *hdev, u8 val) 2015 { 2016 if (!use_ll_privacy(hdev)) 2017 return 0; 2018 2019 /* If controller is not/already resolving we are done. */ 2020 if (val == hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) 2021 return 0; 2022 2023 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, 2024 sizeof(val), &val, HCI_CMD_TIMEOUT); 2025 } 2026 2027 static int hci_scan_disable_sync(struct hci_dev *hdev) 2028 { 2029 int err; 2030 2031 /* If controller is not scanning we are done. */ 2032 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) 2033 return 0; 2034 2035 if (hdev->scanning_paused) { 2036 bt_dev_dbg(hdev, "Scanning is paused for suspend"); 2037 return 0; 2038 } 2039 2040 err = hci_le_set_scan_enable_sync(hdev, LE_SCAN_DISABLE, 0x00); 2041 if (err) { 2042 bt_dev_err(hdev, "Unable to disable scanning: %d", err); 2043 return err; 2044 } 2045 2046 return err; 2047 } 2048 2049 static bool scan_use_rpa(struct hci_dev *hdev) 2050 { 2051 return hci_dev_test_flag(hdev, HCI_PRIVACY); 2052 } 2053 2054 static void hci_start_interleave_scan(struct hci_dev *hdev) 2055 { 2056 hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER; 2057 queue_delayed_work(hdev->req_workqueue, 2058 &hdev->interleave_scan, 0); 2059 } 2060 2061 static bool is_interleave_scanning(struct hci_dev *hdev) 2062 { 2063 return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE; 2064 } 2065 2066 static void cancel_interleave_scan(struct hci_dev *hdev) 2067 { 2068 bt_dev_dbg(hdev, "cancelling interleave scan"); 2069 2070 cancel_delayed_work_sync(&hdev->interleave_scan); 2071 2072 hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE; 2073 } 2074 2075 /* Return true if interleave_scan wasn't started until exiting this function, 2076 * otherwise, return false 2077 */ 2078 static bool hci_update_interleaved_scan_sync(struct hci_dev *hdev) 2079 { 2080 /* Do interleaved scan only if all of the following are true: 2081 * - There is at least one ADV monitor 2082 * - At least one pending LE connection or one device to be scanned for 2083 * - Monitor offloading is not supported 2084 * If so, we should alternate between allowlist scan and one without 2085 * any filters to save power. 2086 */ 2087 bool use_interleaving = hci_is_adv_monitoring(hdev) && 2088 !(list_empty(&hdev->pend_le_conns) && 2089 list_empty(&hdev->pend_le_reports)) && 2090 hci_get_adv_monitor_offload_ext(hdev) == 2091 HCI_ADV_MONITOR_EXT_NONE; 2092 bool is_interleaving = is_interleave_scanning(hdev); 2093 2094 if (use_interleaving && !is_interleaving) { 2095 hci_start_interleave_scan(hdev); 2096 bt_dev_dbg(hdev, "starting interleave scan"); 2097 return true; 2098 } 2099 2100 if (!use_interleaving && is_interleaving) 2101 cancel_interleave_scan(hdev); 2102 2103 return false; 2104 } 2105 2106 /* Removes connection to resolve list if needed.*/ 2107 static int hci_le_del_resolve_list_sync(struct hci_dev *hdev, 2108 bdaddr_t *bdaddr, u8 bdaddr_type) 2109 { 2110 struct hci_cp_le_del_from_resolv_list cp; 2111 struct bdaddr_list_with_irk *entry; 2112 2113 if (!use_ll_privacy(hdev)) 2114 return 0; 2115 2116 /* Check if the IRK has been programmed */ 2117 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, bdaddr, 2118 bdaddr_type); 2119 if (!entry) 2120 return 0; 2121 2122 cp.bdaddr_type = bdaddr_type; 2123 bacpy(&cp.bdaddr, bdaddr); 2124 2125 return __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST, 2126 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2127 } 2128 2129 static int hci_le_del_accept_list_sync(struct hci_dev *hdev, 2130 bdaddr_t *bdaddr, u8 bdaddr_type) 2131 { 2132 struct hci_cp_le_del_from_accept_list cp; 2133 int err; 2134 2135 /* Check if device is on accept list before removing it */ 2136 if (!hci_bdaddr_list_lookup(&hdev->le_accept_list, bdaddr, bdaddr_type)) 2137 return 0; 2138 2139 cp.bdaddr_type = bdaddr_type; 2140 bacpy(&cp.bdaddr, bdaddr); 2141 2142 /* Ignore errors when removing from resolving list as that is likely 2143 * that the device was never added. 2144 */ 2145 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type); 2146 2147 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST, 2148 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2149 if (err) { 2150 bt_dev_err(hdev, "Unable to remove from allow list: %d", err); 2151 return err; 2152 } 2153 2154 bt_dev_dbg(hdev, "Remove %pMR (0x%x) from allow list", &cp.bdaddr, 2155 cp.bdaddr_type); 2156 2157 return 0; 2158 } 2159 2160 struct conn_params { 2161 bdaddr_t addr; 2162 u8 addr_type; 2163 hci_conn_flags_t flags; 2164 u8 privacy_mode; 2165 }; 2166 2167 /* Adds connection to resolve list if needed. 2168 * Setting params to NULL programs local hdev->irk 2169 */ 2170 static int hci_le_add_resolve_list_sync(struct hci_dev *hdev, 2171 struct conn_params *params) 2172 { 2173 struct hci_cp_le_add_to_resolv_list cp; 2174 struct smp_irk *irk; 2175 struct bdaddr_list_with_irk *entry; 2176 struct hci_conn_params *p; 2177 2178 if (!use_ll_privacy(hdev)) 2179 return 0; 2180 2181 /* Attempt to program local identity address, type and irk if params is 2182 * NULL. 2183 */ 2184 if (!params) { 2185 if (!hci_dev_test_flag(hdev, HCI_PRIVACY)) 2186 return 0; 2187 2188 hci_copy_identity_address(hdev, &cp.bdaddr, &cp.bdaddr_type); 2189 memcpy(cp.peer_irk, hdev->irk, 16); 2190 goto done; 2191 } 2192 2193 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type); 2194 if (!irk) 2195 return 0; 2196 2197 /* Check if the IK has _not_ been programmed yet. */ 2198 entry = hci_bdaddr_list_lookup_with_irk(&hdev->le_resolv_list, 2199 ¶ms->addr, 2200 params->addr_type); 2201 if (entry) 2202 return 0; 2203 2204 cp.bdaddr_type = params->addr_type; 2205 bacpy(&cp.bdaddr, ¶ms->addr); 2206 memcpy(cp.peer_irk, irk->val, 16); 2207 2208 /* Default privacy mode is always Network */ 2209 params->privacy_mode = HCI_NETWORK_PRIVACY; 2210 2211 rcu_read_lock(); 2212 p = hci_pend_le_action_lookup(&hdev->pend_le_conns, 2213 ¶ms->addr, params->addr_type); 2214 if (!p) 2215 p = hci_pend_le_action_lookup(&hdev->pend_le_reports, 2216 ¶ms->addr, params->addr_type); 2217 if (p) 2218 WRITE_ONCE(p->privacy_mode, HCI_NETWORK_PRIVACY); 2219 rcu_read_unlock(); 2220 2221 done: 2222 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) 2223 memcpy(cp.local_irk, hdev->irk, 16); 2224 else 2225 memset(cp.local_irk, 0, 16); 2226 2227 return __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST, 2228 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2229 } 2230 2231 /* Set Device Privacy Mode. */ 2232 static int hci_le_set_privacy_mode_sync(struct hci_dev *hdev, 2233 struct conn_params *params) 2234 { 2235 struct hci_cp_le_set_privacy_mode cp; 2236 struct smp_irk *irk; 2237 2238 /* If device privacy mode has already been set there is nothing to do */ 2239 if (params->privacy_mode == HCI_DEVICE_PRIVACY) 2240 return 0; 2241 2242 /* Check if HCI_CONN_FLAG_DEVICE_PRIVACY has been set as it also 2243 * indicates that LL Privacy has been enabled and 2244 * HCI_OP_LE_SET_PRIVACY_MODE is supported. 2245 */ 2246 if (!(params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY)) 2247 return 0; 2248 2249 irk = hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type); 2250 if (!irk) 2251 return 0; 2252 2253 memset(&cp, 0, sizeof(cp)); 2254 cp.bdaddr_type = irk->addr_type; 2255 bacpy(&cp.bdaddr, &irk->bdaddr); 2256 cp.mode = HCI_DEVICE_PRIVACY; 2257 2258 /* Note: params->privacy_mode is not updated since it is a copy */ 2259 2260 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PRIVACY_MODE, 2261 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2262 } 2263 2264 /* Adds connection to allow list if needed, if the device uses RPA (has IRK) 2265 * this attempts to program the device in the resolving list as well and 2266 * properly set the privacy mode. 2267 */ 2268 static int hci_le_add_accept_list_sync(struct hci_dev *hdev, 2269 struct conn_params *params, 2270 u8 *num_entries) 2271 { 2272 struct hci_cp_le_add_to_accept_list cp; 2273 int err; 2274 2275 /* During suspend, only wakeable devices can be in acceptlist */ 2276 if (hdev->suspended && 2277 !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) { 2278 hci_le_del_accept_list_sync(hdev, ¶ms->addr, 2279 params->addr_type); 2280 return 0; 2281 } 2282 2283 /* Select filter policy to accept all advertising */ 2284 if (*num_entries >= hdev->le_accept_list_size) 2285 return -ENOSPC; 2286 2287 /* Accept list can not be used with RPAs */ 2288 if (!use_ll_privacy(hdev) && 2289 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type)) 2290 return -EINVAL; 2291 2292 /* Attempt to program the device in the resolving list first to avoid 2293 * having to rollback in case it fails since the resolving list is 2294 * dynamic it can probably be smaller than the accept list. 2295 */ 2296 err = hci_le_add_resolve_list_sync(hdev, params); 2297 if (err) { 2298 bt_dev_err(hdev, "Unable to add to resolve list: %d", err); 2299 return err; 2300 } 2301 2302 /* Set Privacy Mode */ 2303 err = hci_le_set_privacy_mode_sync(hdev, params); 2304 if (err) { 2305 bt_dev_err(hdev, "Unable to set privacy mode: %d", err); 2306 return err; 2307 } 2308 2309 /* Check if already in accept list */ 2310 if (hci_bdaddr_list_lookup(&hdev->le_accept_list, ¶ms->addr, 2311 params->addr_type)) 2312 return 0; 2313 2314 *num_entries += 1; 2315 cp.bdaddr_type = params->addr_type; 2316 bacpy(&cp.bdaddr, ¶ms->addr); 2317 2318 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST, 2319 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2320 if (err) { 2321 bt_dev_err(hdev, "Unable to add to allow list: %d", err); 2322 /* Rollback the device from the resolving list */ 2323 hci_le_del_resolve_list_sync(hdev, &cp.bdaddr, cp.bdaddr_type); 2324 return err; 2325 } 2326 2327 bt_dev_dbg(hdev, "Add %pMR (0x%x) to allow list", &cp.bdaddr, 2328 cp.bdaddr_type); 2329 2330 return 0; 2331 } 2332 2333 /* This function disables/pause all advertising instances */ 2334 static int hci_pause_advertising_sync(struct hci_dev *hdev) 2335 { 2336 int err; 2337 int old_state; 2338 2339 /* If already been paused there is nothing to do. */ 2340 if (hdev->advertising_paused) 2341 return 0; 2342 2343 bt_dev_dbg(hdev, "Pausing directed advertising"); 2344 2345 /* Stop directed advertising */ 2346 old_state = hci_dev_test_flag(hdev, HCI_ADVERTISING); 2347 if (old_state) { 2348 /* When discoverable timeout triggers, then just make sure 2349 * the limited discoverable flag is cleared. Even in the case 2350 * of a timeout triggered from general discoverable, it is 2351 * safe to unconditionally clear the flag. 2352 */ 2353 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 2354 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); 2355 hdev->discov_timeout = 0; 2356 } 2357 2358 bt_dev_dbg(hdev, "Pausing advertising instances"); 2359 2360 /* Call to disable any advertisements active on the controller. 2361 * This will succeed even if no advertisements are configured. 2362 */ 2363 err = hci_disable_advertising_sync(hdev); 2364 if (err) 2365 return err; 2366 2367 /* If we are using software rotation, pause the loop */ 2368 if (!ext_adv_capable(hdev)) 2369 cancel_adv_timeout(hdev); 2370 2371 hdev->advertising_paused = true; 2372 hdev->advertising_old_state = old_state; 2373 2374 return 0; 2375 } 2376 2377 /* This function enables all user advertising instances */ 2378 static int hci_resume_advertising_sync(struct hci_dev *hdev) 2379 { 2380 struct adv_info *adv, *tmp; 2381 int err; 2382 2383 /* If advertising has not been paused there is nothing to do. */ 2384 if (!hdev->advertising_paused) 2385 return 0; 2386 2387 /* Resume directed advertising */ 2388 hdev->advertising_paused = false; 2389 if (hdev->advertising_old_state) { 2390 hci_dev_set_flag(hdev, HCI_ADVERTISING); 2391 hdev->advertising_old_state = 0; 2392 } 2393 2394 bt_dev_dbg(hdev, "Resuming advertising instances"); 2395 2396 if (ext_adv_capable(hdev)) { 2397 /* Call for each tracked instance to be re-enabled */ 2398 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) { 2399 err = hci_enable_ext_advertising_sync(hdev, 2400 adv->instance); 2401 if (!err) 2402 continue; 2403 2404 /* If the instance cannot be resumed remove it */ 2405 hci_remove_ext_adv_instance_sync(hdev, adv->instance, 2406 NULL); 2407 } 2408 } else { 2409 /* Schedule for most recent instance to be restarted and begin 2410 * the software rotation loop 2411 */ 2412 err = hci_schedule_adv_instance_sync(hdev, 2413 hdev->cur_adv_instance, 2414 true); 2415 } 2416 2417 hdev->advertising_paused = false; 2418 2419 return err; 2420 } 2421 2422 static int hci_pause_addr_resolution(struct hci_dev *hdev) 2423 { 2424 int err; 2425 2426 if (!use_ll_privacy(hdev)) 2427 return 0; 2428 2429 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) 2430 return 0; 2431 2432 /* Cannot disable addr resolution if scanning is enabled or 2433 * when initiating an LE connection. 2434 */ 2435 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) || 2436 hci_lookup_le_connect(hdev)) { 2437 bt_dev_err(hdev, "Command not allowed when scan/LE connect"); 2438 return -EPERM; 2439 } 2440 2441 /* Cannot disable addr resolution if advertising is enabled. */ 2442 err = hci_pause_advertising_sync(hdev); 2443 if (err) { 2444 bt_dev_err(hdev, "Pause advertising failed: %d", err); 2445 return err; 2446 } 2447 2448 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00); 2449 if (err) 2450 bt_dev_err(hdev, "Unable to disable Address Resolution: %d", 2451 err); 2452 2453 /* Return if address resolution is disabled and RPA is not used. */ 2454 if (!err && scan_use_rpa(hdev)) 2455 return 0; 2456 2457 hci_resume_advertising_sync(hdev); 2458 return err; 2459 } 2460 2461 struct sk_buff *hci_read_local_oob_data_sync(struct hci_dev *hdev, 2462 bool extended, struct sock *sk) 2463 { 2464 u16 opcode = extended ? HCI_OP_READ_LOCAL_OOB_EXT_DATA : 2465 HCI_OP_READ_LOCAL_OOB_DATA; 2466 2467 return __hci_cmd_sync_sk(hdev, opcode, 0, NULL, 0, HCI_CMD_TIMEOUT, sk); 2468 } 2469 2470 static struct conn_params *conn_params_copy(struct list_head *list, size_t *n) 2471 { 2472 struct hci_conn_params *params; 2473 struct conn_params *p; 2474 size_t i; 2475 2476 rcu_read_lock(); 2477 2478 i = 0; 2479 list_for_each_entry_rcu(params, list, action) 2480 ++i; 2481 *n = i; 2482 2483 rcu_read_unlock(); 2484 2485 p = kvcalloc(*n, sizeof(struct conn_params), GFP_KERNEL); 2486 if (!p) 2487 return NULL; 2488 2489 rcu_read_lock(); 2490 2491 i = 0; 2492 list_for_each_entry_rcu(params, list, action) { 2493 /* Racing adds are handled in next scan update */ 2494 if (i >= *n) 2495 break; 2496 2497 /* No hdev->lock, but: addr, addr_type are immutable. 2498 * privacy_mode is only written by us or in 2499 * hci_cc_le_set_privacy_mode that we wait for. 2500 * We should be idempotent so MGMT updating flags 2501 * while we are processing is OK. 2502 */ 2503 bacpy(&p[i].addr, ¶ms->addr); 2504 p[i].addr_type = params->addr_type; 2505 p[i].flags = READ_ONCE(params->flags); 2506 p[i].privacy_mode = READ_ONCE(params->privacy_mode); 2507 ++i; 2508 } 2509 2510 rcu_read_unlock(); 2511 2512 *n = i; 2513 return p; 2514 } 2515 2516 /* Device must not be scanning when updating the accept list. 2517 * 2518 * Update is done using the following sequence: 2519 * 2520 * use_ll_privacy((Disable Advertising) -> Disable Resolving List) -> 2521 * Remove Devices From Accept List -> 2522 * (has IRK && use_ll_privacy(Remove Devices From Resolving List))-> 2523 * Add Devices to Accept List -> 2524 * (has IRK && use_ll_privacy(Remove Devices From Resolving List)) -> 2525 * use_ll_privacy(Enable Resolving List -> (Enable Advertising)) -> 2526 * Enable Scanning 2527 * 2528 * In case of failure advertising shall be restored to its original state and 2529 * return would disable accept list since either accept or resolving list could 2530 * not be programmed. 2531 * 2532 */ 2533 static u8 hci_update_accept_list_sync(struct hci_dev *hdev) 2534 { 2535 struct conn_params *params; 2536 struct bdaddr_list *b, *t; 2537 u8 num_entries = 0; 2538 bool pend_conn, pend_report; 2539 u8 filter_policy; 2540 size_t i, n; 2541 int err; 2542 2543 /* Pause advertising if resolving list can be used as controllers 2544 * cannot accept resolving list modifications while advertising. 2545 */ 2546 if (use_ll_privacy(hdev)) { 2547 err = hci_pause_advertising_sync(hdev); 2548 if (err) { 2549 bt_dev_err(hdev, "pause advertising failed: %d", err); 2550 return 0x00; 2551 } 2552 } 2553 2554 /* Disable address resolution while reprogramming accept list since 2555 * devices that do have an IRK will be programmed in the resolving list 2556 * when LL Privacy is enabled. 2557 */ 2558 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x00); 2559 if (err) { 2560 bt_dev_err(hdev, "Unable to disable LL privacy: %d", err); 2561 goto done; 2562 } 2563 2564 /* Go through the current accept list programmed into the 2565 * controller one by one and check if that address is connected or is 2566 * still in the list of pending connections or list of devices to 2567 * report. If not present in either list, then remove it from 2568 * the controller. 2569 */ 2570 list_for_each_entry_safe(b, t, &hdev->le_accept_list, list) { 2571 if (hci_conn_hash_lookup_le(hdev, &b->bdaddr, b->bdaddr_type)) 2572 continue; 2573 2574 /* Pointers not dereferenced, no locks needed */ 2575 pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns, 2576 &b->bdaddr, 2577 b->bdaddr_type); 2578 pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports, 2579 &b->bdaddr, 2580 b->bdaddr_type); 2581 2582 /* If the device is not likely to connect or report, 2583 * remove it from the acceptlist. 2584 */ 2585 if (!pend_conn && !pend_report) { 2586 hci_le_del_accept_list_sync(hdev, &b->bdaddr, 2587 b->bdaddr_type); 2588 continue; 2589 } 2590 2591 num_entries++; 2592 } 2593 2594 /* Since all no longer valid accept list entries have been 2595 * removed, walk through the list of pending connections 2596 * and ensure that any new device gets programmed into 2597 * the controller. 2598 * 2599 * If the list of the devices is larger than the list of 2600 * available accept list entries in the controller, then 2601 * just abort and return filer policy value to not use the 2602 * accept list. 2603 * 2604 * The list and params may be mutated while we wait for events, 2605 * so make a copy and iterate it. 2606 */ 2607 2608 params = conn_params_copy(&hdev->pend_le_conns, &n); 2609 if (!params) { 2610 err = -ENOMEM; 2611 goto done; 2612 } 2613 2614 for (i = 0; i < n; ++i) { 2615 err = hci_le_add_accept_list_sync(hdev, ¶ms[i], 2616 &num_entries); 2617 if (err) { 2618 kvfree(params); 2619 goto done; 2620 } 2621 } 2622 2623 kvfree(params); 2624 2625 /* After adding all new pending connections, walk through 2626 * the list of pending reports and also add these to the 2627 * accept list if there is still space. Abort if space runs out. 2628 */ 2629 2630 params = conn_params_copy(&hdev->pend_le_reports, &n); 2631 if (!params) { 2632 err = -ENOMEM; 2633 goto done; 2634 } 2635 2636 for (i = 0; i < n; ++i) { 2637 err = hci_le_add_accept_list_sync(hdev, ¶ms[i], 2638 &num_entries); 2639 if (err) { 2640 kvfree(params); 2641 goto done; 2642 } 2643 } 2644 2645 kvfree(params); 2646 2647 /* Use the allowlist unless the following conditions are all true: 2648 * - We are not currently suspending 2649 * - There are 1 or more ADV monitors registered and it's not offloaded 2650 * - Interleaved scanning is not currently using the allowlist 2651 */ 2652 if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended && 2653 hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE && 2654 hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST) 2655 err = -EINVAL; 2656 2657 done: 2658 filter_policy = err ? 0x00 : 0x01; 2659 2660 /* Enable address resolution when LL Privacy is enabled. */ 2661 err = hci_le_set_addr_resolution_enable_sync(hdev, 0x01); 2662 if (err) 2663 bt_dev_err(hdev, "Unable to enable LL privacy: %d", err); 2664 2665 /* Resume advertising if it was paused */ 2666 if (use_ll_privacy(hdev)) 2667 hci_resume_advertising_sync(hdev); 2668 2669 /* Select filter policy to use accept list */ 2670 return filter_policy; 2671 } 2672 2673 static int hci_le_set_ext_scan_param_sync(struct hci_dev *hdev, u8 type, 2674 u16 interval, u16 window, 2675 u8 own_addr_type, u8 filter_policy) 2676 { 2677 struct hci_cp_le_set_ext_scan_params *cp; 2678 struct hci_cp_le_scan_phy_params *phy; 2679 u8 data[sizeof(*cp) + sizeof(*phy) * 2]; 2680 u8 num_phy = 0; 2681 2682 cp = (void *)data; 2683 phy = (void *)cp->data; 2684 2685 memset(data, 0, sizeof(data)); 2686 2687 cp->own_addr_type = own_addr_type; 2688 cp->filter_policy = filter_policy; 2689 2690 if (scan_1m(hdev) || scan_2m(hdev)) { 2691 cp->scanning_phys |= LE_SCAN_PHY_1M; 2692 2693 phy->type = type; 2694 phy->interval = cpu_to_le16(interval); 2695 phy->window = cpu_to_le16(window); 2696 2697 num_phy++; 2698 phy++; 2699 } 2700 2701 if (scan_coded(hdev)) { 2702 cp->scanning_phys |= LE_SCAN_PHY_CODED; 2703 2704 phy->type = type; 2705 phy->interval = cpu_to_le16(interval); 2706 phy->window = cpu_to_le16(window); 2707 2708 num_phy++; 2709 phy++; 2710 } 2711 2712 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS, 2713 sizeof(*cp) + sizeof(*phy) * num_phy, 2714 data, HCI_CMD_TIMEOUT); 2715 } 2716 2717 static int hci_le_set_scan_param_sync(struct hci_dev *hdev, u8 type, 2718 u16 interval, u16 window, 2719 u8 own_addr_type, u8 filter_policy) 2720 { 2721 struct hci_cp_le_set_scan_param cp; 2722 2723 if (use_ext_scan(hdev)) 2724 return hci_le_set_ext_scan_param_sync(hdev, type, interval, 2725 window, own_addr_type, 2726 filter_policy); 2727 2728 memset(&cp, 0, sizeof(cp)); 2729 cp.type = type; 2730 cp.interval = cpu_to_le16(interval); 2731 cp.window = cpu_to_le16(window); 2732 cp.own_address_type = own_addr_type; 2733 cp.filter_policy = filter_policy; 2734 2735 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_SCAN_PARAM, 2736 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 2737 } 2738 2739 static int hci_start_scan_sync(struct hci_dev *hdev, u8 type, u16 interval, 2740 u16 window, u8 own_addr_type, u8 filter_policy, 2741 u8 filter_dup) 2742 { 2743 int err; 2744 2745 if (hdev->scanning_paused) { 2746 bt_dev_dbg(hdev, "Scanning is paused for suspend"); 2747 return 0; 2748 } 2749 2750 err = hci_le_set_scan_param_sync(hdev, type, interval, window, 2751 own_addr_type, filter_policy); 2752 if (err) 2753 return err; 2754 2755 return hci_le_set_scan_enable_sync(hdev, LE_SCAN_ENABLE, filter_dup); 2756 } 2757 2758 static int hci_passive_scan_sync(struct hci_dev *hdev) 2759 { 2760 u8 own_addr_type; 2761 u8 filter_policy; 2762 u16 window, interval; 2763 u8 filter_dups = LE_SCAN_FILTER_DUP_ENABLE; 2764 int err; 2765 2766 if (hdev->scanning_paused) { 2767 bt_dev_dbg(hdev, "Scanning is paused for suspend"); 2768 return 0; 2769 } 2770 2771 err = hci_scan_disable_sync(hdev); 2772 if (err) { 2773 bt_dev_err(hdev, "disable scanning failed: %d", err); 2774 return err; 2775 } 2776 2777 /* Set require_privacy to false since no SCAN_REQ are send 2778 * during passive scanning. Not using an non-resolvable address 2779 * here is important so that peer devices using direct 2780 * advertising with our address will be correctly reported 2781 * by the controller. 2782 */ 2783 if (hci_update_random_address_sync(hdev, false, scan_use_rpa(hdev), 2784 &own_addr_type)) 2785 return 0; 2786 2787 if (hdev->enable_advmon_interleave_scan && 2788 hci_update_interleaved_scan_sync(hdev)) 2789 return 0; 2790 2791 bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state); 2792 2793 /* Adding or removing entries from the accept list must 2794 * happen before enabling scanning. The controller does 2795 * not allow accept list modification while scanning. 2796 */ 2797 filter_policy = hci_update_accept_list_sync(hdev); 2798 2799 /* When the controller is using random resolvable addresses and 2800 * with that having LE privacy enabled, then controllers with 2801 * Extended Scanner Filter Policies support can now enable support 2802 * for handling directed advertising. 2803 * 2804 * So instead of using filter polices 0x00 (no acceptlist) 2805 * and 0x01 (acceptlist enabled) use the new filter policies 2806 * 0x02 (no acceptlist) and 0x03 (acceptlist enabled). 2807 */ 2808 if (hci_dev_test_flag(hdev, HCI_PRIVACY) && 2809 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)) 2810 filter_policy |= 0x02; 2811 2812 if (hdev->suspended) { 2813 window = hdev->le_scan_window_suspend; 2814 interval = hdev->le_scan_int_suspend; 2815 } else if (hci_is_le_conn_scanning(hdev)) { 2816 window = hdev->le_scan_window_connect; 2817 interval = hdev->le_scan_int_connect; 2818 } else if (hci_is_adv_monitoring(hdev)) { 2819 window = hdev->le_scan_window_adv_monitor; 2820 interval = hdev->le_scan_int_adv_monitor; 2821 } else { 2822 window = hdev->le_scan_window; 2823 interval = hdev->le_scan_interval; 2824 } 2825 2826 /* Disable all filtering for Mesh */ 2827 if (hci_dev_test_flag(hdev, HCI_MESH)) { 2828 filter_policy = 0; 2829 filter_dups = LE_SCAN_FILTER_DUP_DISABLE; 2830 } 2831 2832 bt_dev_dbg(hdev, "LE passive scan with acceptlist = %d", filter_policy); 2833 2834 return hci_start_scan_sync(hdev, LE_SCAN_PASSIVE, interval, window, 2835 own_addr_type, filter_policy, filter_dups); 2836 } 2837 2838 /* This function controls the passive scanning based on hdev->pend_le_conns 2839 * list. If there are pending LE connection we start the background scanning, 2840 * otherwise we stop it in the following sequence: 2841 * 2842 * If there are devices to scan: 2843 * 2844 * Disable Scanning -> Update Accept List -> 2845 * use_ll_privacy((Disable Advertising) -> Disable Resolving List -> 2846 * Update Resolving List -> Enable Resolving List -> (Enable Advertising)) -> 2847 * Enable Scanning 2848 * 2849 * Otherwise: 2850 * 2851 * Disable Scanning 2852 */ 2853 int hci_update_passive_scan_sync(struct hci_dev *hdev) 2854 { 2855 int err; 2856 2857 if (!test_bit(HCI_UP, &hdev->flags) || 2858 test_bit(HCI_INIT, &hdev->flags) || 2859 hci_dev_test_flag(hdev, HCI_SETUP) || 2860 hci_dev_test_flag(hdev, HCI_CONFIG) || 2861 hci_dev_test_flag(hdev, HCI_AUTO_OFF) || 2862 hci_dev_test_flag(hdev, HCI_UNREGISTER)) 2863 return 0; 2864 2865 /* No point in doing scanning if LE support hasn't been enabled */ 2866 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 2867 return 0; 2868 2869 /* If discovery is active don't interfere with it */ 2870 if (hdev->discovery.state != DISCOVERY_STOPPED) 2871 return 0; 2872 2873 /* Reset RSSI and UUID filters when starting background scanning 2874 * since these filters are meant for service discovery only. 2875 * 2876 * The Start Discovery and Start Service Discovery operations 2877 * ensure to set proper values for RSSI threshold and UUID 2878 * filter list. So it is safe to just reset them here. 2879 */ 2880 hci_discovery_filter_clear(hdev); 2881 2882 bt_dev_dbg(hdev, "ADV monitoring is %s", 2883 hci_is_adv_monitoring(hdev) ? "on" : "off"); 2884 2885 if (!hci_dev_test_flag(hdev, HCI_MESH) && 2886 list_empty(&hdev->pend_le_conns) && 2887 list_empty(&hdev->pend_le_reports) && 2888 !hci_is_adv_monitoring(hdev) && 2889 !hci_dev_test_flag(hdev, HCI_PA_SYNC)) { 2890 /* If there is no pending LE connections or devices 2891 * to be scanned for or no ADV monitors, we should stop the 2892 * background scanning. 2893 */ 2894 2895 bt_dev_dbg(hdev, "stopping background scanning"); 2896 2897 err = hci_scan_disable_sync(hdev); 2898 if (err) 2899 bt_dev_err(hdev, "stop background scanning failed: %d", 2900 err); 2901 } else { 2902 /* If there is at least one pending LE connection, we should 2903 * keep the background scan running. 2904 */ 2905 2906 /* If controller is connecting, we should not start scanning 2907 * since some controllers are not able to scan and connect at 2908 * the same time. 2909 */ 2910 if (hci_lookup_le_connect(hdev)) 2911 return 0; 2912 2913 bt_dev_dbg(hdev, "start background scanning"); 2914 2915 err = hci_passive_scan_sync(hdev); 2916 if (err) 2917 bt_dev_err(hdev, "start background scanning failed: %d", 2918 err); 2919 } 2920 2921 return err; 2922 } 2923 2924 static int update_scan_sync(struct hci_dev *hdev, void *data) 2925 { 2926 return hci_update_scan_sync(hdev); 2927 } 2928 2929 int hci_update_scan(struct hci_dev *hdev) 2930 { 2931 return hci_cmd_sync_queue(hdev, update_scan_sync, NULL, NULL); 2932 } 2933 2934 static int update_passive_scan_sync(struct hci_dev *hdev, void *data) 2935 { 2936 return hci_update_passive_scan_sync(hdev); 2937 } 2938 2939 int hci_update_passive_scan(struct hci_dev *hdev) 2940 { 2941 /* Only queue if it would have any effect */ 2942 if (!test_bit(HCI_UP, &hdev->flags) || 2943 test_bit(HCI_INIT, &hdev->flags) || 2944 hci_dev_test_flag(hdev, HCI_SETUP) || 2945 hci_dev_test_flag(hdev, HCI_CONFIG) || 2946 hci_dev_test_flag(hdev, HCI_AUTO_OFF) || 2947 hci_dev_test_flag(hdev, HCI_UNREGISTER)) 2948 return 0; 2949 2950 return hci_cmd_sync_queue(hdev, update_passive_scan_sync, NULL, NULL); 2951 } 2952 2953 int hci_write_sc_support_sync(struct hci_dev *hdev, u8 val) 2954 { 2955 int err; 2956 2957 if (!bredr_sc_enabled(hdev) || lmp_host_sc_capable(hdev)) 2958 return 0; 2959 2960 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SC_SUPPORT, 2961 sizeof(val), &val, HCI_CMD_TIMEOUT); 2962 2963 if (!err) { 2964 if (val) { 2965 hdev->features[1][0] |= LMP_HOST_SC; 2966 hci_dev_set_flag(hdev, HCI_SC_ENABLED); 2967 } else { 2968 hdev->features[1][0] &= ~LMP_HOST_SC; 2969 hci_dev_clear_flag(hdev, HCI_SC_ENABLED); 2970 } 2971 } 2972 2973 return err; 2974 } 2975 2976 int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode) 2977 { 2978 int err; 2979 2980 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED) || 2981 lmp_host_ssp_capable(hdev)) 2982 return 0; 2983 2984 if (!mode && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) { 2985 __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, 2986 sizeof(mode), &mode, HCI_CMD_TIMEOUT); 2987 } 2988 2989 err = __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE, 2990 sizeof(mode), &mode, HCI_CMD_TIMEOUT); 2991 if (err) 2992 return err; 2993 2994 return hci_write_sc_support_sync(hdev, 0x01); 2995 } 2996 2997 int hci_write_le_host_supported_sync(struct hci_dev *hdev, u8 le, u8 simul) 2998 { 2999 struct hci_cp_write_le_host_supported cp; 3000 3001 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) || 3002 !lmp_bredr_capable(hdev)) 3003 return 0; 3004 3005 /* Check first if we already have the right host state 3006 * (host features set) 3007 */ 3008 if (le == lmp_host_le_capable(hdev) && 3009 simul == lmp_host_le_br_capable(hdev)) 3010 return 0; 3011 3012 memset(&cp, 0, sizeof(cp)); 3013 3014 cp.le = le; 3015 cp.simul = simul; 3016 3017 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, 3018 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 3019 } 3020 3021 static int hci_powered_update_adv_sync(struct hci_dev *hdev) 3022 { 3023 struct adv_info *adv, *tmp; 3024 int err; 3025 3026 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 3027 return 0; 3028 3029 /* If RPA Resolution has not been enable yet it means the 3030 * resolving list is empty and we should attempt to program the 3031 * local IRK in order to support using own_addr_type 3032 * ADDR_LE_DEV_RANDOM_RESOLVED (0x03). 3033 */ 3034 if (!hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) { 3035 hci_le_add_resolve_list_sync(hdev, NULL); 3036 hci_le_set_addr_resolution_enable_sync(hdev, 0x01); 3037 } 3038 3039 /* Make sure the controller has a good default for 3040 * advertising data. This also applies to the case 3041 * where BR/EDR was toggled during the AUTO_OFF phase. 3042 */ 3043 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || 3044 list_empty(&hdev->adv_instances)) { 3045 if (ext_adv_capable(hdev)) { 3046 err = hci_setup_ext_adv_instance_sync(hdev, 0x00); 3047 if (!err) 3048 hci_update_scan_rsp_data_sync(hdev, 0x00); 3049 } else { 3050 err = hci_update_adv_data_sync(hdev, 0x00); 3051 if (!err) 3052 hci_update_scan_rsp_data_sync(hdev, 0x00); 3053 } 3054 3055 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) 3056 hci_enable_advertising_sync(hdev); 3057 } 3058 3059 /* Call for each tracked instance to be scheduled */ 3060 list_for_each_entry_safe(adv, tmp, &hdev->adv_instances, list) 3061 hci_schedule_adv_instance_sync(hdev, adv->instance, true); 3062 3063 return 0; 3064 } 3065 3066 static int hci_write_auth_enable_sync(struct hci_dev *hdev) 3067 { 3068 u8 link_sec; 3069 3070 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY); 3071 if (link_sec == test_bit(HCI_AUTH, &hdev->flags)) 3072 return 0; 3073 3074 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_AUTH_ENABLE, 3075 sizeof(link_sec), &link_sec, 3076 HCI_CMD_TIMEOUT); 3077 } 3078 3079 int hci_write_fast_connectable_sync(struct hci_dev *hdev, bool enable) 3080 { 3081 struct hci_cp_write_page_scan_activity cp; 3082 u8 type; 3083 int err = 0; 3084 3085 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 3086 return 0; 3087 3088 if (hdev->hci_ver < BLUETOOTH_VER_1_2) 3089 return 0; 3090 3091 memset(&cp, 0, sizeof(cp)); 3092 3093 if (enable) { 3094 type = PAGE_SCAN_TYPE_INTERLACED; 3095 3096 /* 160 msec page scan interval */ 3097 cp.interval = cpu_to_le16(0x0100); 3098 } else { 3099 type = hdev->def_page_scan_type; 3100 cp.interval = cpu_to_le16(hdev->def_page_scan_int); 3101 } 3102 3103 cp.window = cpu_to_le16(hdev->def_page_scan_window); 3104 3105 if (__cpu_to_le16(hdev->page_scan_interval) != cp.interval || 3106 __cpu_to_le16(hdev->page_scan_window) != cp.window) { 3107 err = __hci_cmd_sync_status(hdev, 3108 HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, 3109 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 3110 if (err) 3111 return err; 3112 } 3113 3114 if (hdev->page_scan_type != type) 3115 err = __hci_cmd_sync_status(hdev, 3116 HCI_OP_WRITE_PAGE_SCAN_TYPE, 3117 sizeof(type), &type, 3118 HCI_CMD_TIMEOUT); 3119 3120 return err; 3121 } 3122 3123 static bool disconnected_accept_list_entries(struct hci_dev *hdev) 3124 { 3125 struct bdaddr_list *b; 3126 3127 list_for_each_entry(b, &hdev->accept_list, list) { 3128 struct hci_conn *conn; 3129 3130 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr); 3131 if (!conn) 3132 return true; 3133 3134 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) 3135 return true; 3136 } 3137 3138 return false; 3139 } 3140 3141 static int hci_write_scan_enable_sync(struct hci_dev *hdev, u8 val) 3142 { 3143 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SCAN_ENABLE, 3144 sizeof(val), &val, 3145 HCI_CMD_TIMEOUT); 3146 } 3147 3148 int hci_update_scan_sync(struct hci_dev *hdev) 3149 { 3150 u8 scan; 3151 3152 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 3153 return 0; 3154 3155 if (!hdev_is_powered(hdev)) 3156 return 0; 3157 3158 if (mgmt_powering_down(hdev)) 3159 return 0; 3160 3161 if (hdev->scanning_paused) 3162 return 0; 3163 3164 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) || 3165 disconnected_accept_list_entries(hdev)) 3166 scan = SCAN_PAGE; 3167 else 3168 scan = SCAN_DISABLED; 3169 3170 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) 3171 scan |= SCAN_INQUIRY; 3172 3173 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) && 3174 test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY)) 3175 return 0; 3176 3177 return hci_write_scan_enable_sync(hdev, scan); 3178 } 3179 3180 int hci_update_name_sync(struct hci_dev *hdev) 3181 { 3182 struct hci_cp_write_local_name cp; 3183 3184 memset(&cp, 0, sizeof(cp)); 3185 3186 memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); 3187 3188 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_LOCAL_NAME, 3189 sizeof(cp), &cp, 3190 HCI_CMD_TIMEOUT); 3191 } 3192 3193 /* This function perform powered update HCI command sequence after the HCI init 3194 * sequence which end up resetting all states, the sequence is as follows: 3195 * 3196 * HCI_SSP_ENABLED(Enable SSP) 3197 * HCI_LE_ENABLED(Enable LE) 3198 * HCI_LE_ENABLED(use_ll_privacy(Add local IRK to Resolving List) -> 3199 * Update adv data) 3200 * Enable Authentication 3201 * lmp_bredr_capable(Set Fast Connectable -> Set Scan Type -> Set Class -> 3202 * Set Name -> Set EIR) 3203 * HCI_FORCE_STATIC_ADDR | BDADDR_ANY && !HCI_BREDR_ENABLED (Set Static Address) 3204 */ 3205 int hci_powered_update_sync(struct hci_dev *hdev) 3206 { 3207 int err; 3208 3209 /* Register the available SMP channels (BR/EDR and LE) only when 3210 * successfully powering on the controller. This late 3211 * registration is required so that LE SMP can clearly decide if 3212 * the public address or static address is used. 3213 */ 3214 smp_register(hdev); 3215 3216 err = hci_write_ssp_mode_sync(hdev, 0x01); 3217 if (err) 3218 return err; 3219 3220 err = hci_write_le_host_supported_sync(hdev, 0x01, 0x00); 3221 if (err) 3222 return err; 3223 3224 err = hci_powered_update_adv_sync(hdev); 3225 if (err) 3226 return err; 3227 3228 err = hci_write_auth_enable_sync(hdev); 3229 if (err) 3230 return err; 3231 3232 if (lmp_bredr_capable(hdev)) { 3233 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) 3234 hci_write_fast_connectable_sync(hdev, true); 3235 else 3236 hci_write_fast_connectable_sync(hdev, false); 3237 hci_update_scan_sync(hdev); 3238 hci_update_class_sync(hdev); 3239 hci_update_name_sync(hdev); 3240 hci_update_eir_sync(hdev); 3241 } 3242 3243 /* If forcing static address is in use or there is no public 3244 * address use the static address as random address (but skip 3245 * the HCI command if the current random address is already the 3246 * static one. 3247 * 3248 * In case BR/EDR has been disabled on a dual-mode controller 3249 * and a static address has been configured, then use that 3250 * address instead of the public BR/EDR address. 3251 */ 3252 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || 3253 (!bacmp(&hdev->bdaddr, BDADDR_ANY) && 3254 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))) { 3255 if (bacmp(&hdev->static_addr, BDADDR_ANY)) 3256 return hci_set_random_addr_sync(hdev, 3257 &hdev->static_addr); 3258 } 3259 3260 return 0; 3261 } 3262 3263 /** 3264 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address 3265 * (BD_ADDR) for a HCI device from 3266 * a firmware node property. 3267 * @hdev: The HCI device 3268 * 3269 * Search the firmware node for 'local-bd-address'. 3270 * 3271 * All-zero BD addresses are rejected, because those could be properties 3272 * that exist in the firmware tables, but were not updated by the firmware. For 3273 * example, the DTS could define 'local-bd-address', with zero BD addresses. 3274 */ 3275 static void hci_dev_get_bd_addr_from_property(struct hci_dev *hdev) 3276 { 3277 struct fwnode_handle *fwnode = dev_fwnode(hdev->dev.parent); 3278 bdaddr_t ba; 3279 int ret; 3280 3281 ret = fwnode_property_read_u8_array(fwnode, "local-bd-address", 3282 (u8 *)&ba, sizeof(ba)); 3283 if (ret < 0 || !bacmp(&ba, BDADDR_ANY)) 3284 return; 3285 3286 bacpy(&hdev->public_addr, &ba); 3287 } 3288 3289 struct hci_init_stage { 3290 int (*func)(struct hci_dev *hdev); 3291 }; 3292 3293 /* Run init stage NULL terminated function table */ 3294 static int hci_init_stage_sync(struct hci_dev *hdev, 3295 const struct hci_init_stage *stage) 3296 { 3297 size_t i; 3298 3299 for (i = 0; stage[i].func; i++) { 3300 int err; 3301 3302 err = stage[i].func(hdev); 3303 if (err) 3304 return err; 3305 } 3306 3307 return 0; 3308 } 3309 3310 /* Read Local Version */ 3311 static int hci_read_local_version_sync(struct hci_dev *hdev) 3312 { 3313 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_VERSION, 3314 0, NULL, HCI_CMD_TIMEOUT); 3315 } 3316 3317 /* Read BD Address */ 3318 static int hci_read_bd_addr_sync(struct hci_dev *hdev) 3319 { 3320 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BD_ADDR, 3321 0, NULL, HCI_CMD_TIMEOUT); 3322 } 3323 3324 #define HCI_INIT(_func) \ 3325 { \ 3326 .func = _func, \ 3327 } 3328 3329 static const struct hci_init_stage hci_init0[] = { 3330 /* HCI_OP_READ_LOCAL_VERSION */ 3331 HCI_INIT(hci_read_local_version_sync), 3332 /* HCI_OP_READ_BD_ADDR */ 3333 HCI_INIT(hci_read_bd_addr_sync), 3334 {} 3335 }; 3336 3337 int hci_reset_sync(struct hci_dev *hdev) 3338 { 3339 int err; 3340 3341 set_bit(HCI_RESET, &hdev->flags); 3342 3343 err = __hci_cmd_sync_status(hdev, HCI_OP_RESET, 0, NULL, 3344 HCI_CMD_TIMEOUT); 3345 if (err) 3346 return err; 3347 3348 return 0; 3349 } 3350 3351 static int hci_init0_sync(struct hci_dev *hdev) 3352 { 3353 int err; 3354 3355 bt_dev_dbg(hdev, ""); 3356 3357 /* Reset */ 3358 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { 3359 err = hci_reset_sync(hdev); 3360 if (err) 3361 return err; 3362 } 3363 3364 return hci_init_stage_sync(hdev, hci_init0); 3365 } 3366 3367 static int hci_unconf_init_sync(struct hci_dev *hdev) 3368 { 3369 int err; 3370 3371 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 3372 return 0; 3373 3374 err = hci_init0_sync(hdev); 3375 if (err < 0) 3376 return err; 3377 3378 if (hci_dev_test_flag(hdev, HCI_SETUP)) 3379 hci_debugfs_create_basic(hdev); 3380 3381 return 0; 3382 } 3383 3384 /* Read Local Supported Features. */ 3385 static int hci_read_local_features_sync(struct hci_dev *hdev) 3386 { 3387 /* Not all AMP controllers support this command */ 3388 if (hdev->dev_type == HCI_AMP && !(hdev->commands[14] & 0x20)) 3389 return 0; 3390 3391 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_FEATURES, 3392 0, NULL, HCI_CMD_TIMEOUT); 3393 } 3394 3395 /* BR Controller init stage 1 command sequence */ 3396 static const struct hci_init_stage br_init1[] = { 3397 /* HCI_OP_READ_LOCAL_FEATURES */ 3398 HCI_INIT(hci_read_local_features_sync), 3399 /* HCI_OP_READ_LOCAL_VERSION */ 3400 HCI_INIT(hci_read_local_version_sync), 3401 /* HCI_OP_READ_BD_ADDR */ 3402 HCI_INIT(hci_read_bd_addr_sync), 3403 {} 3404 }; 3405 3406 /* Read Local Commands */ 3407 static int hci_read_local_cmds_sync(struct hci_dev *hdev) 3408 { 3409 /* All Bluetooth 1.2 and later controllers should support the 3410 * HCI command for reading the local supported commands. 3411 * 3412 * Unfortunately some controllers indicate Bluetooth 1.2 support, 3413 * but do not have support for this command. If that is the case, 3414 * the driver can quirk the behavior and skip reading the local 3415 * supported commands. 3416 */ 3417 if (hdev->hci_ver > BLUETOOTH_VER_1_1 && 3418 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks)) 3419 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_COMMANDS, 3420 0, NULL, HCI_CMD_TIMEOUT); 3421 3422 return 0; 3423 } 3424 3425 /* Read Local AMP Info */ 3426 static int hci_read_local_amp_info_sync(struct hci_dev *hdev) 3427 { 3428 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 3429 0, NULL, HCI_CMD_TIMEOUT); 3430 } 3431 3432 /* Read Data Blk size */ 3433 static int hci_read_data_block_size_sync(struct hci_dev *hdev) 3434 { 3435 return __hci_cmd_sync_status(hdev, HCI_OP_READ_DATA_BLOCK_SIZE, 3436 0, NULL, HCI_CMD_TIMEOUT); 3437 } 3438 3439 /* Read Flow Control Mode */ 3440 static int hci_read_flow_control_mode_sync(struct hci_dev *hdev) 3441 { 3442 return __hci_cmd_sync_status(hdev, HCI_OP_READ_FLOW_CONTROL_MODE, 3443 0, NULL, HCI_CMD_TIMEOUT); 3444 } 3445 3446 /* Read Location Data */ 3447 static int hci_read_location_data_sync(struct hci_dev *hdev) 3448 { 3449 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCATION_DATA, 3450 0, NULL, HCI_CMD_TIMEOUT); 3451 } 3452 3453 /* AMP Controller init stage 1 command sequence */ 3454 static const struct hci_init_stage amp_init1[] = { 3455 /* HCI_OP_READ_LOCAL_VERSION */ 3456 HCI_INIT(hci_read_local_version_sync), 3457 /* HCI_OP_READ_LOCAL_COMMANDS */ 3458 HCI_INIT(hci_read_local_cmds_sync), 3459 /* HCI_OP_READ_LOCAL_AMP_INFO */ 3460 HCI_INIT(hci_read_local_amp_info_sync), 3461 /* HCI_OP_READ_DATA_BLOCK_SIZE */ 3462 HCI_INIT(hci_read_data_block_size_sync), 3463 /* HCI_OP_READ_FLOW_CONTROL_MODE */ 3464 HCI_INIT(hci_read_flow_control_mode_sync), 3465 /* HCI_OP_READ_LOCATION_DATA */ 3466 HCI_INIT(hci_read_location_data_sync), 3467 {} 3468 }; 3469 3470 static int hci_init1_sync(struct hci_dev *hdev) 3471 { 3472 int err; 3473 3474 bt_dev_dbg(hdev, ""); 3475 3476 /* Reset */ 3477 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { 3478 err = hci_reset_sync(hdev); 3479 if (err) 3480 return err; 3481 } 3482 3483 switch (hdev->dev_type) { 3484 case HCI_PRIMARY: 3485 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED; 3486 return hci_init_stage_sync(hdev, br_init1); 3487 case HCI_AMP: 3488 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; 3489 return hci_init_stage_sync(hdev, amp_init1); 3490 default: 3491 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type); 3492 break; 3493 } 3494 3495 return 0; 3496 } 3497 3498 /* AMP Controller init stage 2 command sequence */ 3499 static const struct hci_init_stage amp_init2[] = { 3500 /* HCI_OP_READ_LOCAL_FEATURES */ 3501 HCI_INIT(hci_read_local_features_sync), 3502 {} 3503 }; 3504 3505 /* Read Buffer Size (ACL mtu, max pkt, etc.) */ 3506 static int hci_read_buffer_size_sync(struct hci_dev *hdev) 3507 { 3508 return __hci_cmd_sync_status(hdev, HCI_OP_READ_BUFFER_SIZE, 3509 0, NULL, HCI_CMD_TIMEOUT); 3510 } 3511 3512 /* Read Class of Device */ 3513 static int hci_read_dev_class_sync(struct hci_dev *hdev) 3514 { 3515 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CLASS_OF_DEV, 3516 0, NULL, HCI_CMD_TIMEOUT); 3517 } 3518 3519 /* Read Local Name */ 3520 static int hci_read_local_name_sync(struct hci_dev *hdev) 3521 { 3522 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_NAME, 3523 0, NULL, HCI_CMD_TIMEOUT); 3524 } 3525 3526 /* Read Voice Setting */ 3527 static int hci_read_voice_setting_sync(struct hci_dev *hdev) 3528 { 3529 return __hci_cmd_sync_status(hdev, HCI_OP_READ_VOICE_SETTING, 3530 0, NULL, HCI_CMD_TIMEOUT); 3531 } 3532 3533 /* Read Number of Supported IAC */ 3534 static int hci_read_num_supported_iac_sync(struct hci_dev *hdev) 3535 { 3536 return __hci_cmd_sync_status(hdev, HCI_OP_READ_NUM_SUPPORTED_IAC, 3537 0, NULL, HCI_CMD_TIMEOUT); 3538 } 3539 3540 /* Read Current IAC LAP */ 3541 static int hci_read_current_iac_lap_sync(struct hci_dev *hdev) 3542 { 3543 return __hci_cmd_sync_status(hdev, HCI_OP_READ_CURRENT_IAC_LAP, 3544 0, NULL, HCI_CMD_TIMEOUT); 3545 } 3546 3547 static int hci_set_event_filter_sync(struct hci_dev *hdev, u8 flt_type, 3548 u8 cond_type, bdaddr_t *bdaddr, 3549 u8 auto_accept) 3550 { 3551 struct hci_cp_set_event_filter cp; 3552 3553 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 3554 return 0; 3555 3556 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks)) 3557 return 0; 3558 3559 memset(&cp, 0, sizeof(cp)); 3560 cp.flt_type = flt_type; 3561 3562 if (flt_type != HCI_FLT_CLEAR_ALL) { 3563 cp.cond_type = cond_type; 3564 bacpy(&cp.addr_conn_flt.bdaddr, bdaddr); 3565 cp.addr_conn_flt.auto_accept = auto_accept; 3566 } 3567 3568 return __hci_cmd_sync_status(hdev, HCI_OP_SET_EVENT_FLT, 3569 flt_type == HCI_FLT_CLEAR_ALL ? 3570 sizeof(cp.flt_type) : sizeof(cp), &cp, 3571 HCI_CMD_TIMEOUT); 3572 } 3573 3574 static int hci_clear_event_filter_sync(struct hci_dev *hdev) 3575 { 3576 if (!hci_dev_test_flag(hdev, HCI_EVENT_FILTER_CONFIGURED)) 3577 return 0; 3578 3579 /* In theory the state machine should not reach here unless 3580 * a hci_set_event_filter_sync() call succeeds, but we do 3581 * the check both for parity and as a future reminder. 3582 */ 3583 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks)) 3584 return 0; 3585 3586 return hci_set_event_filter_sync(hdev, HCI_FLT_CLEAR_ALL, 0x00, 3587 BDADDR_ANY, 0x00); 3588 } 3589 3590 /* Connection accept timeout ~20 secs */ 3591 static int hci_write_ca_timeout_sync(struct hci_dev *hdev) 3592 { 3593 __le16 param = cpu_to_le16(0x7d00); 3594 3595 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CA_TIMEOUT, 3596 sizeof(param), ¶m, HCI_CMD_TIMEOUT); 3597 } 3598 3599 /* BR Controller init stage 2 command sequence */ 3600 static const struct hci_init_stage br_init2[] = { 3601 /* HCI_OP_READ_BUFFER_SIZE */ 3602 HCI_INIT(hci_read_buffer_size_sync), 3603 /* HCI_OP_READ_CLASS_OF_DEV */ 3604 HCI_INIT(hci_read_dev_class_sync), 3605 /* HCI_OP_READ_LOCAL_NAME */ 3606 HCI_INIT(hci_read_local_name_sync), 3607 /* HCI_OP_READ_VOICE_SETTING */ 3608 HCI_INIT(hci_read_voice_setting_sync), 3609 /* HCI_OP_READ_NUM_SUPPORTED_IAC */ 3610 HCI_INIT(hci_read_num_supported_iac_sync), 3611 /* HCI_OP_READ_CURRENT_IAC_LAP */ 3612 HCI_INIT(hci_read_current_iac_lap_sync), 3613 /* HCI_OP_SET_EVENT_FLT */ 3614 HCI_INIT(hci_clear_event_filter_sync), 3615 /* HCI_OP_WRITE_CA_TIMEOUT */ 3616 HCI_INIT(hci_write_ca_timeout_sync), 3617 {} 3618 }; 3619 3620 static int hci_write_ssp_mode_1_sync(struct hci_dev *hdev) 3621 { 3622 u8 mode = 0x01; 3623 3624 if (!lmp_ssp_capable(hdev) || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) 3625 return 0; 3626 3627 /* When SSP is available, then the host features page 3628 * should also be available as well. However some 3629 * controllers list the max_page as 0 as long as SSP 3630 * has not been enabled. To achieve proper debugging 3631 * output, force the minimum max_page to 1 at least. 3632 */ 3633 hdev->max_page = 0x01; 3634 3635 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_SSP_MODE, 3636 sizeof(mode), &mode, HCI_CMD_TIMEOUT); 3637 } 3638 3639 static int hci_write_eir_sync(struct hci_dev *hdev) 3640 { 3641 struct hci_cp_write_eir cp; 3642 3643 if (!lmp_ssp_capable(hdev) || hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) 3644 return 0; 3645 3646 memset(hdev->eir, 0, sizeof(hdev->eir)); 3647 memset(&cp, 0, sizeof(cp)); 3648 3649 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp, 3650 HCI_CMD_TIMEOUT); 3651 } 3652 3653 static int hci_write_inquiry_mode_sync(struct hci_dev *hdev) 3654 { 3655 u8 mode; 3656 3657 if (!lmp_inq_rssi_capable(hdev) && 3658 !test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) 3659 return 0; 3660 3661 /* If Extended Inquiry Result events are supported, then 3662 * they are clearly preferred over Inquiry Result with RSSI 3663 * events. 3664 */ 3665 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01; 3666 3667 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_INQUIRY_MODE, 3668 sizeof(mode), &mode, HCI_CMD_TIMEOUT); 3669 } 3670 3671 static int hci_read_inq_rsp_tx_power_sync(struct hci_dev *hdev) 3672 { 3673 if (!lmp_inq_tx_pwr_capable(hdev)) 3674 return 0; 3675 3676 return __hci_cmd_sync_status(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, 3677 0, NULL, HCI_CMD_TIMEOUT); 3678 } 3679 3680 static int hci_read_local_ext_features_sync(struct hci_dev *hdev, u8 page) 3681 { 3682 struct hci_cp_read_local_ext_features cp; 3683 3684 if (!lmp_ext_feat_capable(hdev)) 3685 return 0; 3686 3687 memset(&cp, 0, sizeof(cp)); 3688 cp.page = page; 3689 3690 return __hci_cmd_sync_status(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES, 3691 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 3692 } 3693 3694 static int hci_read_local_ext_features_1_sync(struct hci_dev *hdev) 3695 { 3696 return hci_read_local_ext_features_sync(hdev, 0x01); 3697 } 3698 3699 /* HCI Controller init stage 2 command sequence */ 3700 static const struct hci_init_stage hci_init2[] = { 3701 /* HCI_OP_READ_LOCAL_COMMANDS */ 3702 HCI_INIT(hci_read_local_cmds_sync), 3703 /* HCI_OP_WRITE_SSP_MODE */ 3704 HCI_INIT(hci_write_ssp_mode_1_sync), 3705 /* HCI_OP_WRITE_EIR */ 3706 HCI_INIT(hci_write_eir_sync), 3707 /* HCI_OP_WRITE_INQUIRY_MODE */ 3708 HCI_INIT(hci_write_inquiry_mode_sync), 3709 /* HCI_OP_READ_INQ_RSP_TX_POWER */ 3710 HCI_INIT(hci_read_inq_rsp_tx_power_sync), 3711 /* HCI_OP_READ_LOCAL_EXT_FEATURES */ 3712 HCI_INIT(hci_read_local_ext_features_1_sync), 3713 /* HCI_OP_WRITE_AUTH_ENABLE */ 3714 HCI_INIT(hci_write_auth_enable_sync), 3715 {} 3716 }; 3717 3718 /* Read LE Buffer Size */ 3719 static int hci_le_read_buffer_size_sync(struct hci_dev *hdev) 3720 { 3721 /* Use Read LE Buffer Size V2 if supported */ 3722 if (iso_capable(hdev) && hdev->commands[41] & 0x20) 3723 return __hci_cmd_sync_status(hdev, 3724 HCI_OP_LE_READ_BUFFER_SIZE_V2, 3725 0, NULL, HCI_CMD_TIMEOUT); 3726 3727 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 3728 0, NULL, HCI_CMD_TIMEOUT); 3729 } 3730 3731 /* Read LE Local Supported Features */ 3732 static int hci_le_read_local_features_sync(struct hci_dev *hdev) 3733 { 3734 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_LOCAL_FEATURES, 3735 0, NULL, HCI_CMD_TIMEOUT); 3736 } 3737 3738 /* Read LE Supported States */ 3739 static int hci_le_read_supported_states_sync(struct hci_dev *hdev) 3740 { 3741 return __hci_cmd_sync_status(hdev, HCI_OP_LE_READ_SUPPORTED_STATES, 3742 0, NULL, HCI_CMD_TIMEOUT); 3743 } 3744 3745 /* LE Controller init stage 2 command sequence */ 3746 static const struct hci_init_stage le_init2[] = { 3747 /* HCI_OP_LE_READ_LOCAL_FEATURES */ 3748 HCI_INIT(hci_le_read_local_features_sync), 3749 /* HCI_OP_LE_READ_BUFFER_SIZE */ 3750 HCI_INIT(hci_le_read_buffer_size_sync), 3751 /* HCI_OP_LE_READ_SUPPORTED_STATES */ 3752 HCI_INIT(hci_le_read_supported_states_sync), 3753 {} 3754 }; 3755 3756 static int hci_init2_sync(struct hci_dev *hdev) 3757 { 3758 int err; 3759 3760 bt_dev_dbg(hdev, ""); 3761 3762 if (hdev->dev_type == HCI_AMP) 3763 return hci_init_stage_sync(hdev, amp_init2); 3764 3765 err = hci_init_stage_sync(hdev, hci_init2); 3766 if (err) 3767 return err; 3768 3769 if (lmp_bredr_capable(hdev)) { 3770 err = hci_init_stage_sync(hdev, br_init2); 3771 if (err) 3772 return err; 3773 } else { 3774 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); 3775 } 3776 3777 if (lmp_le_capable(hdev)) { 3778 err = hci_init_stage_sync(hdev, le_init2); 3779 if (err) 3780 return err; 3781 /* LE-only controllers have LE implicitly enabled */ 3782 if (!lmp_bredr_capable(hdev)) 3783 hci_dev_set_flag(hdev, HCI_LE_ENABLED); 3784 } 3785 3786 return 0; 3787 } 3788 3789 static int hci_set_event_mask_sync(struct hci_dev *hdev) 3790 { 3791 /* The second byte is 0xff instead of 0x9f (two reserved bits 3792 * disabled) since a Broadcom 1.2 dongle doesn't respond to the 3793 * command otherwise. 3794 */ 3795 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 }; 3796 3797 /* CSR 1.1 dongles does not accept any bitfield so don't try to set 3798 * any event mask for pre 1.2 devices. 3799 */ 3800 if (hdev->hci_ver < BLUETOOTH_VER_1_2) 3801 return 0; 3802 3803 if (lmp_bredr_capable(hdev)) { 3804 events[4] |= 0x01; /* Flow Specification Complete */ 3805 3806 /* Don't set Disconnect Complete and mode change when 3807 * suspended as that would wakeup the host when disconnecting 3808 * due to suspend. 3809 */ 3810 if (hdev->suspended) { 3811 events[0] &= 0xef; 3812 events[2] &= 0xf7; 3813 } 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 hci_codec_list_clear(&hdev->local_codecs); 5083 5084 hci_dev_put(hdev); 5085 return err; 5086 } 5087 5088 /* This function perform power on HCI command sequence as follows: 5089 * 5090 * If controller is already up (HCI_UP) performs hci_powered_update_sync 5091 * sequence otherwise run hci_dev_open_sync which will follow with 5092 * hci_powered_update_sync after the init sequence is completed. 5093 */ 5094 static int hci_power_on_sync(struct hci_dev *hdev) 5095 { 5096 int err; 5097 5098 if (test_bit(HCI_UP, &hdev->flags) && 5099 hci_dev_test_flag(hdev, HCI_MGMT) && 5100 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) { 5101 cancel_delayed_work(&hdev->power_off); 5102 return hci_powered_update_sync(hdev); 5103 } 5104 5105 err = hci_dev_open_sync(hdev); 5106 if (err < 0) 5107 return err; 5108 5109 /* During the HCI setup phase, a few error conditions are 5110 * ignored and they need to be checked now. If they are still 5111 * valid, it is important to return the device back off. 5112 */ 5113 if (hci_dev_test_flag(hdev, HCI_RFKILLED) || 5114 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) || 5115 (hdev->dev_type == HCI_PRIMARY && 5116 !bacmp(&hdev->bdaddr, BDADDR_ANY) && 5117 !bacmp(&hdev->static_addr, BDADDR_ANY))) { 5118 hci_dev_clear_flag(hdev, HCI_AUTO_OFF); 5119 hci_dev_close_sync(hdev); 5120 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) { 5121 queue_delayed_work(hdev->req_workqueue, &hdev->power_off, 5122 HCI_AUTO_OFF_TIMEOUT); 5123 } 5124 5125 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) { 5126 /* For unconfigured devices, set the HCI_RAW flag 5127 * so that userspace can easily identify them. 5128 */ 5129 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 5130 set_bit(HCI_RAW, &hdev->flags); 5131 5132 /* For fully configured devices, this will send 5133 * the Index Added event. For unconfigured devices, 5134 * it will send Unconfigued Index Added event. 5135 * 5136 * Devices with HCI_QUIRK_RAW_DEVICE are ignored 5137 * and no event will be send. 5138 */ 5139 mgmt_index_added(hdev); 5140 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) { 5141 /* When the controller is now configured, then it 5142 * is important to clear the HCI_RAW flag. 5143 */ 5144 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 5145 clear_bit(HCI_RAW, &hdev->flags); 5146 5147 /* Powering on the controller with HCI_CONFIG set only 5148 * happens with the transition from unconfigured to 5149 * configured. This will send the Index Added event. 5150 */ 5151 mgmt_index_added(hdev); 5152 } 5153 5154 return 0; 5155 } 5156 5157 static int hci_remote_name_cancel_sync(struct hci_dev *hdev, bdaddr_t *addr) 5158 { 5159 struct hci_cp_remote_name_req_cancel cp; 5160 5161 memset(&cp, 0, sizeof(cp)); 5162 bacpy(&cp.bdaddr, addr); 5163 5164 return __hci_cmd_sync_status(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL, 5165 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 5166 } 5167 5168 int hci_stop_discovery_sync(struct hci_dev *hdev) 5169 { 5170 struct discovery_state *d = &hdev->discovery; 5171 struct inquiry_entry *e; 5172 int err; 5173 5174 bt_dev_dbg(hdev, "state %u", hdev->discovery.state); 5175 5176 if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) { 5177 if (test_bit(HCI_INQUIRY, &hdev->flags)) { 5178 err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 5179 0, NULL, HCI_CMD_TIMEOUT); 5180 if (err) 5181 return err; 5182 } 5183 5184 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { 5185 cancel_delayed_work(&hdev->le_scan_disable); 5186 cancel_delayed_work(&hdev->le_scan_restart); 5187 5188 err = hci_scan_disable_sync(hdev); 5189 if (err) 5190 return err; 5191 } 5192 5193 } else { 5194 err = hci_scan_disable_sync(hdev); 5195 if (err) 5196 return err; 5197 } 5198 5199 /* Resume advertising if it was paused */ 5200 if (use_ll_privacy(hdev)) 5201 hci_resume_advertising_sync(hdev); 5202 5203 /* No further actions needed for LE-only discovery */ 5204 if (d->type == DISCOV_TYPE_LE) 5205 return 0; 5206 5207 if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) { 5208 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, 5209 NAME_PENDING); 5210 if (!e) 5211 return 0; 5212 5213 return hci_remote_name_cancel_sync(hdev, &e->data.bdaddr); 5214 } 5215 5216 return 0; 5217 } 5218 5219 static int hci_disconnect_phy_link_sync(struct hci_dev *hdev, u16 handle, 5220 u8 reason) 5221 { 5222 struct hci_cp_disconn_phy_link cp; 5223 5224 memset(&cp, 0, sizeof(cp)); 5225 cp.phy_handle = HCI_PHY_HANDLE(handle); 5226 cp.reason = reason; 5227 5228 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONN_PHY_LINK, 5229 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 5230 } 5231 5232 static int hci_disconnect_sync(struct hci_dev *hdev, struct hci_conn *conn, 5233 u8 reason) 5234 { 5235 struct hci_cp_disconnect cp; 5236 5237 if (conn->type == AMP_LINK) 5238 return hci_disconnect_phy_link_sync(hdev, conn->handle, reason); 5239 5240 memset(&cp, 0, sizeof(cp)); 5241 cp.handle = cpu_to_le16(conn->handle); 5242 cp.reason = reason; 5243 5244 /* Wait for HCI_EV_DISCONN_COMPLETE, not HCI_EV_CMD_STATUS, when the 5245 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is 5246 * used when suspending or powering off, where we don't want to wait 5247 * for the peer's response. 5248 */ 5249 if (reason != HCI_ERROR_REMOTE_POWER_OFF) 5250 return __hci_cmd_sync_status_sk(hdev, HCI_OP_DISCONNECT, 5251 sizeof(cp), &cp, 5252 HCI_EV_DISCONN_COMPLETE, 5253 HCI_CMD_TIMEOUT, NULL); 5254 5255 return __hci_cmd_sync_status(hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp, 5256 HCI_CMD_TIMEOUT); 5257 } 5258 5259 static int hci_le_connect_cancel_sync(struct hci_dev *hdev, 5260 struct hci_conn *conn, u8 reason) 5261 { 5262 /* Return reason if scanning since the connection shall probably be 5263 * cleanup directly. 5264 */ 5265 if (test_bit(HCI_CONN_SCANNING, &conn->flags)) 5266 return reason; 5267 5268 if (conn->role == HCI_ROLE_SLAVE || 5269 test_and_set_bit(HCI_CONN_CANCEL, &conn->flags)) 5270 return 0; 5271 5272 return __hci_cmd_sync_status(hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 5273 0, NULL, HCI_CMD_TIMEOUT); 5274 } 5275 5276 static int hci_connect_cancel_sync(struct hci_dev *hdev, struct hci_conn *conn, 5277 u8 reason) 5278 { 5279 if (conn->type == LE_LINK) 5280 return hci_le_connect_cancel_sync(hdev, conn, reason); 5281 5282 if (conn->type == ISO_LINK) { 5283 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E 5284 * page 1857: 5285 * 5286 * If this command is issued for a CIS on the Central and the 5287 * CIS is successfully terminated before being established, 5288 * then an HCI_LE_CIS_Established event shall also be sent for 5289 * this CIS with the Status Operation Cancelled by Host (0x44). 5290 */ 5291 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) 5292 return hci_disconnect_sync(hdev, conn, reason); 5293 5294 /* CIS with no Create CIS sent have nothing to cancel */ 5295 if (bacmp(&conn->dst, BDADDR_ANY)) 5296 return HCI_ERROR_LOCAL_HOST_TERM; 5297 5298 /* There is no way to cancel a BIS without terminating the BIG 5299 * which is done later on connection cleanup. 5300 */ 5301 return 0; 5302 } 5303 5304 if (hdev->hci_ver < BLUETOOTH_VER_1_2) 5305 return 0; 5306 5307 /* Wait for HCI_EV_CONN_COMPLETE, not HCI_EV_CMD_STATUS, when the 5308 * reason is anything but HCI_ERROR_REMOTE_POWER_OFF. This reason is 5309 * used when suspending or powering off, where we don't want to wait 5310 * for the peer's response. 5311 */ 5312 if (reason != HCI_ERROR_REMOTE_POWER_OFF) 5313 return __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN_CANCEL, 5314 6, &conn->dst, 5315 HCI_EV_CONN_COMPLETE, 5316 HCI_CMD_TIMEOUT, NULL); 5317 5318 return __hci_cmd_sync_status(hdev, HCI_OP_CREATE_CONN_CANCEL, 5319 6, &conn->dst, HCI_CMD_TIMEOUT); 5320 } 5321 5322 static int hci_reject_sco_sync(struct hci_dev *hdev, struct hci_conn *conn, 5323 u8 reason) 5324 { 5325 struct hci_cp_reject_sync_conn_req cp; 5326 5327 memset(&cp, 0, sizeof(cp)); 5328 bacpy(&cp.bdaddr, &conn->dst); 5329 cp.reason = reason; 5330 5331 /* SCO rejection has its own limited set of 5332 * allowed error values (0x0D-0x0F). 5333 */ 5334 if (reason < 0x0d || reason > 0x0f) 5335 cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES; 5336 5337 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_SYNC_CONN_REQ, 5338 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 5339 } 5340 5341 static int hci_le_reject_cis_sync(struct hci_dev *hdev, struct hci_conn *conn, 5342 u8 reason) 5343 { 5344 struct hci_cp_le_reject_cis cp; 5345 5346 memset(&cp, 0, sizeof(cp)); 5347 cp.handle = cpu_to_le16(conn->handle); 5348 cp.reason = reason; 5349 5350 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REJECT_CIS, 5351 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 5352 } 5353 5354 static int hci_reject_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, 5355 u8 reason) 5356 { 5357 struct hci_cp_reject_conn_req cp; 5358 5359 if (conn->type == ISO_LINK) 5360 return hci_le_reject_cis_sync(hdev, conn, reason); 5361 5362 if (conn->type == SCO_LINK || conn->type == ESCO_LINK) 5363 return hci_reject_sco_sync(hdev, conn, reason); 5364 5365 memset(&cp, 0, sizeof(cp)); 5366 bacpy(&cp.bdaddr, &conn->dst); 5367 cp.reason = reason; 5368 5369 return __hci_cmd_sync_status(hdev, HCI_OP_REJECT_CONN_REQ, 5370 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 5371 } 5372 5373 int hci_abort_conn_sync(struct hci_dev *hdev, struct hci_conn *conn, u8 reason) 5374 { 5375 int err = 0; 5376 u16 handle = conn->handle; 5377 bool disconnect = false; 5378 struct hci_conn *c; 5379 5380 switch (conn->state) { 5381 case BT_CONNECTED: 5382 case BT_CONFIG: 5383 err = hci_disconnect_sync(hdev, conn, reason); 5384 break; 5385 case BT_CONNECT: 5386 err = hci_connect_cancel_sync(hdev, conn, reason); 5387 break; 5388 case BT_CONNECT2: 5389 err = hci_reject_conn_sync(hdev, conn, reason); 5390 break; 5391 case BT_OPEN: 5392 hci_dev_lock(hdev); 5393 5394 /* Cleanup bis or pa sync connections */ 5395 if (test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags) || 5396 test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags)) { 5397 hci_conn_failed(conn, reason); 5398 } else if (test_bit(HCI_CONN_PA_SYNC, &conn->flags) || 5399 test_bit(HCI_CONN_BIG_SYNC, &conn->flags)) { 5400 conn->state = BT_CLOSED; 5401 hci_disconn_cfm(conn, reason); 5402 hci_conn_del(conn); 5403 } 5404 5405 hci_dev_unlock(hdev); 5406 return 0; 5407 case BT_BOUND: 5408 break; 5409 default: 5410 disconnect = true; 5411 break; 5412 } 5413 5414 hci_dev_lock(hdev); 5415 5416 /* Check if the connection has been cleaned up concurrently */ 5417 c = hci_conn_hash_lookup_handle(hdev, handle); 5418 if (!c || c != conn) { 5419 err = 0; 5420 goto unlock; 5421 } 5422 5423 /* Cleanup hci_conn object if it cannot be cancelled as it 5424 * likelly means the controller and host stack are out of sync 5425 * or in case of LE it was still scanning so it can be cleanup 5426 * safely. 5427 */ 5428 if (disconnect) { 5429 conn->state = BT_CLOSED; 5430 hci_disconn_cfm(conn, reason); 5431 hci_conn_del(conn); 5432 } else { 5433 hci_conn_failed(conn, reason); 5434 } 5435 5436 unlock: 5437 hci_dev_unlock(hdev); 5438 return err; 5439 } 5440 5441 static int hci_disconnect_all_sync(struct hci_dev *hdev, u8 reason) 5442 { 5443 struct list_head *head = &hdev->conn_hash.list; 5444 struct hci_conn *conn; 5445 5446 rcu_read_lock(); 5447 while ((conn = list_first_or_null_rcu(head, struct hci_conn, list))) { 5448 /* Make sure the connection is not freed while unlocking */ 5449 conn = hci_conn_get(conn); 5450 rcu_read_unlock(); 5451 /* Disregard possible errors since hci_conn_del shall have been 5452 * called even in case of errors had occurred since it would 5453 * then cause hci_conn_failed to be called which calls 5454 * hci_conn_del internally. 5455 */ 5456 hci_abort_conn_sync(hdev, conn, reason); 5457 hci_conn_put(conn); 5458 rcu_read_lock(); 5459 } 5460 rcu_read_unlock(); 5461 5462 return 0; 5463 } 5464 5465 /* This function perform power off HCI command sequence as follows: 5466 * 5467 * Clear Advertising 5468 * Stop Discovery 5469 * Disconnect all connections 5470 * hci_dev_close_sync 5471 */ 5472 static int hci_power_off_sync(struct hci_dev *hdev) 5473 { 5474 int err; 5475 5476 /* If controller is already down there is nothing to do */ 5477 if (!test_bit(HCI_UP, &hdev->flags)) 5478 return 0; 5479 5480 if (test_bit(HCI_ISCAN, &hdev->flags) || 5481 test_bit(HCI_PSCAN, &hdev->flags)) { 5482 err = hci_write_scan_enable_sync(hdev, 0x00); 5483 if (err) 5484 return err; 5485 } 5486 5487 err = hci_clear_adv_sync(hdev, NULL, false); 5488 if (err) 5489 return err; 5490 5491 err = hci_stop_discovery_sync(hdev); 5492 if (err) 5493 return err; 5494 5495 /* Terminated due to Power Off */ 5496 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF); 5497 if (err) 5498 return err; 5499 5500 return hci_dev_close_sync(hdev); 5501 } 5502 5503 int hci_set_powered_sync(struct hci_dev *hdev, u8 val) 5504 { 5505 if (val) 5506 return hci_power_on_sync(hdev); 5507 5508 return hci_power_off_sync(hdev); 5509 } 5510 5511 static int hci_write_iac_sync(struct hci_dev *hdev) 5512 { 5513 struct hci_cp_write_current_iac_lap cp; 5514 5515 if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) 5516 return 0; 5517 5518 memset(&cp, 0, sizeof(cp)); 5519 5520 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) { 5521 /* Limited discoverable mode */ 5522 cp.num_iac = min_t(u8, hdev->num_iac, 2); 5523 cp.iac_lap[0] = 0x00; /* LIAC */ 5524 cp.iac_lap[1] = 0x8b; 5525 cp.iac_lap[2] = 0x9e; 5526 cp.iac_lap[3] = 0x33; /* GIAC */ 5527 cp.iac_lap[4] = 0x8b; 5528 cp.iac_lap[5] = 0x9e; 5529 } else { 5530 /* General discoverable mode */ 5531 cp.num_iac = 1; 5532 cp.iac_lap[0] = 0x33; /* GIAC */ 5533 cp.iac_lap[1] = 0x8b; 5534 cp.iac_lap[2] = 0x9e; 5535 } 5536 5537 return __hci_cmd_sync_status(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP, 5538 (cp.num_iac * 3) + 1, &cp, 5539 HCI_CMD_TIMEOUT); 5540 } 5541 5542 int hci_update_discoverable_sync(struct hci_dev *hdev) 5543 { 5544 int err = 0; 5545 5546 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 5547 err = hci_write_iac_sync(hdev); 5548 if (err) 5549 return err; 5550 5551 err = hci_update_scan_sync(hdev); 5552 if (err) 5553 return err; 5554 5555 err = hci_update_class_sync(hdev); 5556 if (err) 5557 return err; 5558 } 5559 5560 /* Advertising instances don't use the global discoverable setting, so 5561 * only update AD if advertising was enabled using Set Advertising. 5562 */ 5563 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) { 5564 err = hci_update_adv_data_sync(hdev, 0x00); 5565 if (err) 5566 return err; 5567 5568 /* Discoverable mode affects the local advertising 5569 * address in limited privacy mode. 5570 */ 5571 if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) { 5572 if (ext_adv_capable(hdev)) 5573 err = hci_start_ext_adv_sync(hdev, 0x00); 5574 else 5575 err = hci_enable_advertising_sync(hdev); 5576 } 5577 } 5578 5579 return err; 5580 } 5581 5582 static int update_discoverable_sync(struct hci_dev *hdev, void *data) 5583 { 5584 return hci_update_discoverable_sync(hdev); 5585 } 5586 5587 int hci_update_discoverable(struct hci_dev *hdev) 5588 { 5589 /* Only queue if it would have any effect */ 5590 if (hdev_is_powered(hdev) && 5591 hci_dev_test_flag(hdev, HCI_ADVERTISING) && 5592 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && 5593 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) 5594 return hci_cmd_sync_queue(hdev, update_discoverable_sync, NULL, 5595 NULL); 5596 5597 return 0; 5598 } 5599 5600 int hci_update_connectable_sync(struct hci_dev *hdev) 5601 { 5602 int err; 5603 5604 err = hci_update_scan_sync(hdev); 5605 if (err) 5606 return err; 5607 5608 /* If BR/EDR is not enabled and we disable advertising as a 5609 * by-product of disabling connectable, we need to update the 5610 * advertising flags. 5611 */ 5612 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 5613 err = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance); 5614 5615 /* Update the advertising parameters if necessary */ 5616 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || 5617 !list_empty(&hdev->adv_instances)) { 5618 if (ext_adv_capable(hdev)) 5619 err = hci_start_ext_adv_sync(hdev, 5620 hdev->cur_adv_instance); 5621 else 5622 err = hci_enable_advertising_sync(hdev); 5623 5624 if (err) 5625 return err; 5626 } 5627 5628 return hci_update_passive_scan_sync(hdev); 5629 } 5630 5631 static int hci_inquiry_sync(struct hci_dev *hdev, u8 length) 5632 { 5633 const u8 giac[3] = { 0x33, 0x8b, 0x9e }; 5634 const u8 liac[3] = { 0x00, 0x8b, 0x9e }; 5635 struct hci_cp_inquiry cp; 5636 5637 bt_dev_dbg(hdev, ""); 5638 5639 if (test_bit(HCI_INQUIRY, &hdev->flags)) 5640 return 0; 5641 5642 hci_dev_lock(hdev); 5643 hci_inquiry_cache_flush(hdev); 5644 hci_dev_unlock(hdev); 5645 5646 memset(&cp, 0, sizeof(cp)); 5647 5648 if (hdev->discovery.limited) 5649 memcpy(&cp.lap, liac, sizeof(cp.lap)); 5650 else 5651 memcpy(&cp.lap, giac, sizeof(cp.lap)); 5652 5653 cp.length = length; 5654 5655 return __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY, 5656 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 5657 } 5658 5659 static int hci_active_scan_sync(struct hci_dev *hdev, uint16_t interval) 5660 { 5661 u8 own_addr_type; 5662 /* Accept list is not used for discovery */ 5663 u8 filter_policy = 0x00; 5664 /* Default is to enable duplicates filter */ 5665 u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE; 5666 int err; 5667 5668 bt_dev_dbg(hdev, ""); 5669 5670 /* If controller is scanning, it means the passive scanning is 5671 * running. Thus, we should temporarily stop it in order to set the 5672 * discovery scanning parameters. 5673 */ 5674 err = hci_scan_disable_sync(hdev); 5675 if (err) { 5676 bt_dev_err(hdev, "Unable to disable scanning: %d", err); 5677 return err; 5678 } 5679 5680 cancel_interleave_scan(hdev); 5681 5682 /* Pause address resolution for active scan and stop advertising if 5683 * privacy is enabled. 5684 */ 5685 err = hci_pause_addr_resolution(hdev); 5686 if (err) 5687 goto failed; 5688 5689 /* All active scans will be done with either a resolvable private 5690 * address (when privacy feature has been enabled) or non-resolvable 5691 * private address. 5692 */ 5693 err = hci_update_random_address_sync(hdev, true, scan_use_rpa(hdev), 5694 &own_addr_type); 5695 if (err < 0) 5696 own_addr_type = ADDR_LE_DEV_PUBLIC; 5697 5698 if (hci_is_adv_monitoring(hdev)) { 5699 /* Duplicate filter should be disabled when some advertisement 5700 * monitor is activated, otherwise AdvMon can only receive one 5701 * advertisement for one peer(*) during active scanning, and 5702 * might report loss to these peers. 5703 * 5704 * Note that different controllers have different meanings of 5705 * |duplicate|. Some of them consider packets with the same 5706 * address as duplicate, and others consider packets with the 5707 * same address and the same RSSI as duplicate. Although in the 5708 * latter case we don't need to disable duplicate filter, but 5709 * it is common to have active scanning for a short period of 5710 * time, the power impact should be neglectable. 5711 */ 5712 filter_dup = LE_SCAN_FILTER_DUP_DISABLE; 5713 } 5714 5715 err = hci_start_scan_sync(hdev, LE_SCAN_ACTIVE, interval, 5716 hdev->le_scan_window_discovery, 5717 own_addr_type, filter_policy, filter_dup); 5718 if (!err) 5719 return err; 5720 5721 failed: 5722 /* Resume advertising if it was paused */ 5723 if (use_ll_privacy(hdev)) 5724 hci_resume_advertising_sync(hdev); 5725 5726 /* Resume passive scanning */ 5727 hci_update_passive_scan_sync(hdev); 5728 return err; 5729 } 5730 5731 static int hci_start_interleaved_discovery_sync(struct hci_dev *hdev) 5732 { 5733 int err; 5734 5735 bt_dev_dbg(hdev, ""); 5736 5737 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery * 2); 5738 if (err) 5739 return err; 5740 5741 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN); 5742 } 5743 5744 int hci_start_discovery_sync(struct hci_dev *hdev) 5745 { 5746 unsigned long timeout; 5747 int err; 5748 5749 bt_dev_dbg(hdev, "type %u", hdev->discovery.type); 5750 5751 switch (hdev->discovery.type) { 5752 case DISCOV_TYPE_BREDR: 5753 return hci_inquiry_sync(hdev, DISCOV_BREDR_INQUIRY_LEN); 5754 case DISCOV_TYPE_INTERLEAVED: 5755 /* When running simultaneous discovery, the LE scanning time 5756 * should occupy the whole discovery time sine BR/EDR inquiry 5757 * and LE scanning are scheduled by the controller. 5758 * 5759 * For interleaving discovery in comparison, BR/EDR inquiry 5760 * and LE scanning are done sequentially with separate 5761 * timeouts. 5762 */ 5763 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, 5764 &hdev->quirks)) { 5765 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); 5766 /* During simultaneous discovery, we double LE scan 5767 * interval. We must leave some time for the controller 5768 * to do BR/EDR inquiry. 5769 */ 5770 err = hci_start_interleaved_discovery_sync(hdev); 5771 break; 5772 } 5773 5774 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); 5775 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery); 5776 break; 5777 case DISCOV_TYPE_LE: 5778 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); 5779 err = hci_active_scan_sync(hdev, hdev->le_scan_int_discovery); 5780 break; 5781 default: 5782 return -EINVAL; 5783 } 5784 5785 if (err) 5786 return err; 5787 5788 bt_dev_dbg(hdev, "timeout %u ms", jiffies_to_msecs(timeout)); 5789 5790 /* When service discovery is used and the controller has a 5791 * strict duplicate filter, it is important to remember the 5792 * start and duration of the scan. This is required for 5793 * restarting scanning during the discovery phase. 5794 */ 5795 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) && 5796 hdev->discovery.result_filtering) { 5797 hdev->discovery.scan_start = jiffies; 5798 hdev->discovery.scan_duration = timeout; 5799 } 5800 5801 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable, 5802 timeout); 5803 return 0; 5804 } 5805 5806 static void hci_suspend_monitor_sync(struct hci_dev *hdev) 5807 { 5808 switch (hci_get_adv_monitor_offload_ext(hdev)) { 5809 case HCI_ADV_MONITOR_EXT_MSFT: 5810 msft_suspend_sync(hdev); 5811 break; 5812 default: 5813 return; 5814 } 5815 } 5816 5817 /* This function disables discovery and mark it as paused */ 5818 static int hci_pause_discovery_sync(struct hci_dev *hdev) 5819 { 5820 int old_state = hdev->discovery.state; 5821 int err; 5822 5823 /* If discovery already stopped/stopping/paused there nothing to do */ 5824 if (old_state == DISCOVERY_STOPPED || old_state == DISCOVERY_STOPPING || 5825 hdev->discovery_paused) 5826 return 0; 5827 5828 hci_discovery_set_state(hdev, DISCOVERY_STOPPING); 5829 err = hci_stop_discovery_sync(hdev); 5830 if (err) 5831 return err; 5832 5833 hdev->discovery_paused = true; 5834 hdev->discovery_old_state = old_state; 5835 hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 5836 5837 return 0; 5838 } 5839 5840 static int hci_update_event_filter_sync(struct hci_dev *hdev) 5841 { 5842 struct bdaddr_list_with_flags *b; 5843 u8 scan = SCAN_DISABLED; 5844 bool scanning = test_bit(HCI_PSCAN, &hdev->flags); 5845 int err; 5846 5847 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 5848 return 0; 5849 5850 /* Some fake CSR controllers lock up after setting this type of 5851 * filter, so avoid sending the request altogether. 5852 */ 5853 if (test_bit(HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL, &hdev->quirks)) 5854 return 0; 5855 5856 /* Always clear event filter when starting */ 5857 hci_clear_event_filter_sync(hdev); 5858 5859 list_for_each_entry(b, &hdev->accept_list, list) { 5860 if (!(b->flags & HCI_CONN_FLAG_REMOTE_WAKEUP)) 5861 continue; 5862 5863 bt_dev_dbg(hdev, "Adding event filters for %pMR", &b->bdaddr); 5864 5865 err = hci_set_event_filter_sync(hdev, HCI_FLT_CONN_SETUP, 5866 HCI_CONN_SETUP_ALLOW_BDADDR, 5867 &b->bdaddr, 5868 HCI_CONN_SETUP_AUTO_ON); 5869 if (err) 5870 bt_dev_dbg(hdev, "Failed to set event filter for %pMR", 5871 &b->bdaddr); 5872 else 5873 scan = SCAN_PAGE; 5874 } 5875 5876 if (scan && !scanning) 5877 hci_write_scan_enable_sync(hdev, scan); 5878 else if (!scan && scanning) 5879 hci_write_scan_enable_sync(hdev, scan); 5880 5881 return 0; 5882 } 5883 5884 /* This function disables scan (BR and LE) and mark it as paused */ 5885 static int hci_pause_scan_sync(struct hci_dev *hdev) 5886 { 5887 if (hdev->scanning_paused) 5888 return 0; 5889 5890 /* Disable page scan if enabled */ 5891 if (test_bit(HCI_PSCAN, &hdev->flags)) 5892 hci_write_scan_enable_sync(hdev, SCAN_DISABLED); 5893 5894 hci_scan_disable_sync(hdev); 5895 5896 hdev->scanning_paused = true; 5897 5898 return 0; 5899 } 5900 5901 /* This function performs the HCI suspend procedures in the follow order: 5902 * 5903 * Pause discovery (active scanning/inquiry) 5904 * Pause Directed Advertising/Advertising 5905 * Pause Scanning (passive scanning in case discovery was not active) 5906 * Disconnect all connections 5907 * Set suspend_status to BT_SUSPEND_DISCONNECT if hdev cannot wakeup 5908 * otherwise: 5909 * Update event mask (only set events that are allowed to wake up the host) 5910 * Update event filter (with devices marked with HCI_CONN_FLAG_REMOTE_WAKEUP) 5911 * Update passive scanning (lower duty cycle) 5912 * Set suspend_status to BT_SUSPEND_CONFIGURE_WAKE 5913 */ 5914 int hci_suspend_sync(struct hci_dev *hdev) 5915 { 5916 int err; 5917 5918 /* If marked as suspended there nothing to do */ 5919 if (hdev->suspended) 5920 return 0; 5921 5922 /* Mark device as suspended */ 5923 hdev->suspended = true; 5924 5925 /* Pause discovery if not already stopped */ 5926 hci_pause_discovery_sync(hdev); 5927 5928 /* Pause other advertisements */ 5929 hci_pause_advertising_sync(hdev); 5930 5931 /* Suspend monitor filters */ 5932 hci_suspend_monitor_sync(hdev); 5933 5934 /* Prevent disconnects from causing scanning to be re-enabled */ 5935 hci_pause_scan_sync(hdev); 5936 5937 if (hci_conn_count(hdev)) { 5938 /* Soft disconnect everything (power off) */ 5939 err = hci_disconnect_all_sync(hdev, HCI_ERROR_REMOTE_POWER_OFF); 5940 if (err) { 5941 /* Set state to BT_RUNNING so resume doesn't notify */ 5942 hdev->suspend_state = BT_RUNNING; 5943 hci_resume_sync(hdev); 5944 return err; 5945 } 5946 5947 /* Update event mask so only the allowed event can wakeup the 5948 * host. 5949 */ 5950 hci_set_event_mask_sync(hdev); 5951 } 5952 5953 /* Only configure accept list if disconnect succeeded and wake 5954 * isn't being prevented. 5955 */ 5956 if (!hdev->wakeup || !hdev->wakeup(hdev)) { 5957 hdev->suspend_state = BT_SUSPEND_DISCONNECT; 5958 return 0; 5959 } 5960 5961 /* Unpause to take care of updating scanning params */ 5962 hdev->scanning_paused = false; 5963 5964 /* Enable event filter for paired devices */ 5965 hci_update_event_filter_sync(hdev); 5966 5967 /* Update LE passive scan if enabled */ 5968 hci_update_passive_scan_sync(hdev); 5969 5970 /* Pause scan changes again. */ 5971 hdev->scanning_paused = true; 5972 5973 hdev->suspend_state = BT_SUSPEND_CONFIGURE_WAKE; 5974 5975 return 0; 5976 } 5977 5978 /* This function resumes discovery */ 5979 static int hci_resume_discovery_sync(struct hci_dev *hdev) 5980 { 5981 int err; 5982 5983 /* If discovery not paused there nothing to do */ 5984 if (!hdev->discovery_paused) 5985 return 0; 5986 5987 hdev->discovery_paused = false; 5988 5989 hci_discovery_set_state(hdev, DISCOVERY_STARTING); 5990 5991 err = hci_start_discovery_sync(hdev); 5992 5993 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED : 5994 DISCOVERY_FINDING); 5995 5996 return err; 5997 } 5998 5999 static void hci_resume_monitor_sync(struct hci_dev *hdev) 6000 { 6001 switch (hci_get_adv_monitor_offload_ext(hdev)) { 6002 case HCI_ADV_MONITOR_EXT_MSFT: 6003 msft_resume_sync(hdev); 6004 break; 6005 default: 6006 return; 6007 } 6008 } 6009 6010 /* This function resume scan and reset paused flag */ 6011 static int hci_resume_scan_sync(struct hci_dev *hdev) 6012 { 6013 if (!hdev->scanning_paused) 6014 return 0; 6015 6016 hdev->scanning_paused = false; 6017 6018 hci_update_scan_sync(hdev); 6019 6020 /* Reset passive scanning to normal */ 6021 hci_update_passive_scan_sync(hdev); 6022 6023 return 0; 6024 } 6025 6026 /* This function performs the HCI suspend procedures in the follow order: 6027 * 6028 * Restore event mask 6029 * Clear event filter 6030 * Update passive scanning (normal duty cycle) 6031 * Resume Directed Advertising/Advertising 6032 * Resume discovery (active scanning/inquiry) 6033 */ 6034 int hci_resume_sync(struct hci_dev *hdev) 6035 { 6036 /* If not marked as suspended there nothing to do */ 6037 if (!hdev->suspended) 6038 return 0; 6039 6040 hdev->suspended = false; 6041 6042 /* Restore event mask */ 6043 hci_set_event_mask_sync(hdev); 6044 6045 /* Clear any event filters and restore scan state */ 6046 hci_clear_event_filter_sync(hdev); 6047 6048 /* Resume scanning */ 6049 hci_resume_scan_sync(hdev); 6050 6051 /* Resume monitor filters */ 6052 hci_resume_monitor_sync(hdev); 6053 6054 /* Resume other advertisements */ 6055 hci_resume_advertising_sync(hdev); 6056 6057 /* Resume discovery */ 6058 hci_resume_discovery_sync(hdev); 6059 6060 return 0; 6061 } 6062 6063 static bool conn_use_rpa(struct hci_conn *conn) 6064 { 6065 struct hci_dev *hdev = conn->hdev; 6066 6067 return hci_dev_test_flag(hdev, HCI_PRIVACY); 6068 } 6069 6070 static int hci_le_ext_directed_advertising_sync(struct hci_dev *hdev, 6071 struct hci_conn *conn) 6072 { 6073 struct hci_cp_le_set_ext_adv_params cp; 6074 int err; 6075 bdaddr_t random_addr; 6076 u8 own_addr_type; 6077 6078 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn), 6079 &own_addr_type); 6080 if (err) 6081 return err; 6082 6083 /* Set require_privacy to false so that the remote device has a 6084 * chance of identifying us. 6085 */ 6086 err = hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL, 6087 &own_addr_type, &random_addr); 6088 if (err) 6089 return err; 6090 6091 memset(&cp, 0, sizeof(cp)); 6092 6093 cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND); 6094 cp.channel_map = hdev->le_adv_channel_map; 6095 cp.tx_power = HCI_TX_POWER_INVALID; 6096 cp.primary_phy = HCI_ADV_PHY_1M; 6097 cp.secondary_phy = HCI_ADV_PHY_1M; 6098 cp.handle = 0x00; /* Use instance 0 for directed adv */ 6099 cp.own_addr_type = own_addr_type; 6100 cp.peer_addr_type = conn->dst_type; 6101 bacpy(&cp.peer_addr, &conn->dst); 6102 6103 /* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for 6104 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND 6105 * does not supports advertising data when the advertising set already 6106 * contains some, the controller shall return erroc code 'Invalid 6107 * HCI Command Parameters(0x12). 6108 * So it is required to remove adv set for handle 0x00. since we use 6109 * instance 0 for directed adv. 6110 */ 6111 err = hci_remove_ext_adv_instance_sync(hdev, cp.handle, NULL); 6112 if (err) 6113 return err; 6114 6115 err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS, 6116 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 6117 if (err) 6118 return err; 6119 6120 /* Check if random address need to be updated */ 6121 if (own_addr_type == ADDR_LE_DEV_RANDOM && 6122 bacmp(&random_addr, BDADDR_ANY) && 6123 bacmp(&random_addr, &hdev->random_addr)) { 6124 err = hci_set_adv_set_random_addr_sync(hdev, 0x00, 6125 &random_addr); 6126 if (err) 6127 return err; 6128 } 6129 6130 return hci_enable_ext_advertising_sync(hdev, 0x00); 6131 } 6132 6133 static int hci_le_directed_advertising_sync(struct hci_dev *hdev, 6134 struct hci_conn *conn) 6135 { 6136 struct hci_cp_le_set_adv_param cp; 6137 u8 status; 6138 u8 own_addr_type; 6139 u8 enable; 6140 6141 if (ext_adv_capable(hdev)) 6142 return hci_le_ext_directed_advertising_sync(hdev, conn); 6143 6144 /* Clear the HCI_LE_ADV bit temporarily so that the 6145 * hci_update_random_address knows that it's safe to go ahead 6146 * and write a new random address. The flag will be set back on 6147 * as soon as the SET_ADV_ENABLE HCI command completes. 6148 */ 6149 hci_dev_clear_flag(hdev, HCI_LE_ADV); 6150 6151 /* Set require_privacy to false so that the remote device has a 6152 * chance of identifying us. 6153 */ 6154 status = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn), 6155 &own_addr_type); 6156 if (status) 6157 return status; 6158 6159 memset(&cp, 0, sizeof(cp)); 6160 6161 /* Some controllers might reject command if intervals are not 6162 * within range for undirected advertising. 6163 * BCM20702A0 is known to be affected by this. 6164 */ 6165 cp.min_interval = cpu_to_le16(0x0020); 6166 cp.max_interval = cpu_to_le16(0x0020); 6167 6168 cp.type = LE_ADV_DIRECT_IND; 6169 cp.own_address_type = own_addr_type; 6170 cp.direct_addr_type = conn->dst_type; 6171 bacpy(&cp.direct_addr, &conn->dst); 6172 cp.channel_map = hdev->le_adv_channel_map; 6173 6174 status = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_PARAM, 6175 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 6176 if (status) 6177 return status; 6178 6179 enable = 0x01; 6180 6181 return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_ADV_ENABLE, 6182 sizeof(enable), &enable, HCI_CMD_TIMEOUT); 6183 } 6184 6185 static void set_ext_conn_params(struct hci_conn *conn, 6186 struct hci_cp_le_ext_conn_param *p) 6187 { 6188 struct hci_dev *hdev = conn->hdev; 6189 6190 memset(p, 0, sizeof(*p)); 6191 6192 p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect); 6193 p->scan_window = cpu_to_le16(hdev->le_scan_window_connect); 6194 p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval); 6195 p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval); 6196 p->conn_latency = cpu_to_le16(conn->le_conn_latency); 6197 p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout); 6198 p->min_ce_len = cpu_to_le16(0x0000); 6199 p->max_ce_len = cpu_to_le16(0x0000); 6200 } 6201 6202 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev, 6203 struct hci_conn *conn, u8 own_addr_type) 6204 { 6205 struct hci_cp_le_ext_create_conn *cp; 6206 struct hci_cp_le_ext_conn_param *p; 6207 u8 data[sizeof(*cp) + sizeof(*p) * 3]; 6208 u32 plen; 6209 6210 cp = (void *)data; 6211 p = (void *)cp->data; 6212 6213 memset(cp, 0, sizeof(*cp)); 6214 6215 bacpy(&cp->peer_addr, &conn->dst); 6216 cp->peer_addr_type = conn->dst_type; 6217 cp->own_addr_type = own_addr_type; 6218 6219 plen = sizeof(*cp); 6220 6221 if (scan_1m(hdev)) { 6222 cp->phys |= LE_SCAN_PHY_1M; 6223 set_ext_conn_params(conn, p); 6224 6225 p++; 6226 plen += sizeof(*p); 6227 } 6228 6229 if (scan_2m(hdev)) { 6230 cp->phys |= LE_SCAN_PHY_2M; 6231 set_ext_conn_params(conn, p); 6232 6233 p++; 6234 plen += sizeof(*p); 6235 } 6236 6237 if (scan_coded(hdev)) { 6238 cp->phys |= LE_SCAN_PHY_CODED; 6239 set_ext_conn_params(conn, p); 6240 6241 plen += sizeof(*p); 6242 } 6243 6244 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_EXT_CREATE_CONN, 6245 plen, data, 6246 HCI_EV_LE_ENHANCED_CONN_COMPLETE, 6247 conn->conn_timeout, NULL); 6248 } 6249 6250 int hci_le_create_conn_sync(struct hci_dev *hdev, struct hci_conn *conn) 6251 { 6252 struct hci_cp_le_create_conn cp; 6253 struct hci_conn_params *params; 6254 u8 own_addr_type; 6255 int err; 6256 6257 /* If requested to connect as peripheral use directed advertising */ 6258 if (conn->role == HCI_ROLE_SLAVE) { 6259 /* If we're active scanning and simultaneous roles is not 6260 * enabled simply reject the attempt. 6261 */ 6262 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && 6263 hdev->le_scan_type == LE_SCAN_ACTIVE && 6264 !hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) { 6265 hci_conn_del(conn); 6266 return -EBUSY; 6267 } 6268 6269 /* Pause advertising while doing directed advertising. */ 6270 hci_pause_advertising_sync(hdev); 6271 6272 err = hci_le_directed_advertising_sync(hdev, conn); 6273 goto done; 6274 } 6275 6276 /* Disable advertising if simultaneous roles is not in use. */ 6277 if (!hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) 6278 hci_pause_advertising_sync(hdev); 6279 6280 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); 6281 if (params) { 6282 conn->le_conn_min_interval = params->conn_min_interval; 6283 conn->le_conn_max_interval = params->conn_max_interval; 6284 conn->le_conn_latency = params->conn_latency; 6285 conn->le_supv_timeout = params->supervision_timeout; 6286 } else { 6287 conn->le_conn_min_interval = hdev->le_conn_min_interval; 6288 conn->le_conn_max_interval = hdev->le_conn_max_interval; 6289 conn->le_conn_latency = hdev->le_conn_latency; 6290 conn->le_supv_timeout = hdev->le_supv_timeout; 6291 } 6292 6293 /* If controller is scanning, we stop it since some controllers are 6294 * not able to scan and connect at the same time. Also set the 6295 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete 6296 * handler for scan disabling knows to set the correct discovery 6297 * state. 6298 */ 6299 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { 6300 hci_scan_disable_sync(hdev); 6301 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED); 6302 } 6303 6304 /* Update random address, but set require_privacy to false so 6305 * that we never connect with an non-resolvable address. 6306 */ 6307 err = hci_update_random_address_sync(hdev, false, conn_use_rpa(conn), 6308 &own_addr_type); 6309 if (err) 6310 goto done; 6311 6312 if (use_ext_conn(hdev)) { 6313 err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type); 6314 goto done; 6315 } 6316 6317 memset(&cp, 0, sizeof(cp)); 6318 6319 cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect); 6320 cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect); 6321 6322 bacpy(&cp.peer_addr, &conn->dst); 6323 cp.peer_addr_type = conn->dst_type; 6324 cp.own_address_type = own_addr_type; 6325 cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval); 6326 cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval); 6327 cp.conn_latency = cpu_to_le16(conn->le_conn_latency); 6328 cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout); 6329 cp.min_ce_len = cpu_to_le16(0x0000); 6330 cp.max_ce_len = cpu_to_le16(0x0000); 6331 6332 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261: 6333 * 6334 * If this event is unmasked and the HCI_LE_Connection_Complete event 6335 * is unmasked, only the HCI_LE_Enhanced_Connection_Complete event is 6336 * sent when a new connection has been created. 6337 */ 6338 err = __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CONN, 6339 sizeof(cp), &cp, 6340 use_enhanced_conn_complete(hdev) ? 6341 HCI_EV_LE_ENHANCED_CONN_COMPLETE : 6342 HCI_EV_LE_CONN_COMPLETE, 6343 conn->conn_timeout, NULL); 6344 6345 done: 6346 if (err == -ETIMEDOUT) 6347 hci_le_connect_cancel_sync(hdev, conn, 0x00); 6348 6349 /* Re-enable advertising after the connection attempt is finished. */ 6350 hci_resume_advertising_sync(hdev); 6351 return err; 6352 } 6353 6354 int hci_le_create_cis_sync(struct hci_dev *hdev) 6355 { 6356 struct { 6357 struct hci_cp_le_create_cis cp; 6358 struct hci_cis cis[0x1f]; 6359 } cmd; 6360 struct hci_conn *conn; 6361 u8 cig = BT_ISO_QOS_CIG_UNSET; 6362 6363 /* The spec allows only one pending LE Create CIS command at a time. If 6364 * the command is pending now, don't do anything. We check for pending 6365 * connections after each CIS Established event. 6366 * 6367 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E 6368 * page 2566: 6369 * 6370 * If the Host issues this command before all the 6371 * HCI_LE_CIS_Established events from the previous use of the 6372 * command have been generated, the Controller shall return the 6373 * error code Command Disallowed (0x0C). 6374 * 6375 * BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E 6376 * page 2567: 6377 * 6378 * When the Controller receives the HCI_LE_Create_CIS command, the 6379 * Controller sends the HCI_Command_Status event to the Host. An 6380 * HCI_LE_CIS_Established event will be generated for each CIS when it 6381 * is established or if it is disconnected or considered lost before 6382 * being established; until all the events are generated, the command 6383 * remains pending. 6384 */ 6385 6386 memset(&cmd, 0, sizeof(cmd)); 6387 6388 hci_dev_lock(hdev); 6389 6390 rcu_read_lock(); 6391 6392 /* Wait until previous Create CIS has completed */ 6393 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) { 6394 if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) 6395 goto done; 6396 } 6397 6398 /* Find CIG with all CIS ready */ 6399 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) { 6400 struct hci_conn *link; 6401 6402 if (hci_conn_check_create_cis(conn)) 6403 continue; 6404 6405 cig = conn->iso_qos.ucast.cig; 6406 6407 list_for_each_entry_rcu(link, &hdev->conn_hash.list, list) { 6408 if (hci_conn_check_create_cis(link) > 0 && 6409 link->iso_qos.ucast.cig == cig && 6410 link->state != BT_CONNECTED) { 6411 cig = BT_ISO_QOS_CIG_UNSET; 6412 break; 6413 } 6414 } 6415 6416 if (cig != BT_ISO_QOS_CIG_UNSET) 6417 break; 6418 } 6419 6420 if (cig == BT_ISO_QOS_CIG_UNSET) 6421 goto done; 6422 6423 list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) { 6424 struct hci_cis *cis = &cmd.cis[cmd.cp.num_cis]; 6425 6426 if (hci_conn_check_create_cis(conn) || 6427 conn->iso_qos.ucast.cig != cig) 6428 continue; 6429 6430 set_bit(HCI_CONN_CREATE_CIS, &conn->flags); 6431 cis->acl_handle = cpu_to_le16(conn->parent->handle); 6432 cis->cis_handle = cpu_to_le16(conn->handle); 6433 cmd.cp.num_cis++; 6434 6435 if (cmd.cp.num_cis >= ARRAY_SIZE(cmd.cis)) 6436 break; 6437 } 6438 6439 done: 6440 rcu_read_unlock(); 6441 6442 hci_dev_unlock(hdev); 6443 6444 if (!cmd.cp.num_cis) 6445 return 0; 6446 6447 /* Wait for HCI_LE_CIS_Established */ 6448 return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CREATE_CIS, 6449 sizeof(cmd.cp) + sizeof(cmd.cis[0]) * 6450 cmd.cp.num_cis, &cmd, 6451 HCI_EVT_LE_CIS_ESTABLISHED, 6452 conn->conn_timeout, NULL); 6453 } 6454 6455 int hci_le_remove_cig_sync(struct hci_dev *hdev, u8 handle) 6456 { 6457 struct hci_cp_le_remove_cig cp; 6458 6459 memset(&cp, 0, sizeof(cp)); 6460 cp.cig_id = handle; 6461 6462 return __hci_cmd_sync_status(hdev, HCI_OP_LE_REMOVE_CIG, sizeof(cp), 6463 &cp, HCI_CMD_TIMEOUT); 6464 } 6465 6466 int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle) 6467 { 6468 struct hci_cp_le_big_term_sync cp; 6469 6470 memset(&cp, 0, sizeof(cp)); 6471 cp.handle = handle; 6472 6473 return __hci_cmd_sync_status(hdev, HCI_OP_LE_BIG_TERM_SYNC, 6474 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 6475 } 6476 6477 int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle) 6478 { 6479 struct hci_cp_le_pa_term_sync cp; 6480 6481 memset(&cp, 0, sizeof(cp)); 6482 cp.handle = cpu_to_le16(handle); 6483 6484 return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC, 6485 sizeof(cp), &cp, HCI_CMD_TIMEOUT); 6486 } 6487 6488 int hci_get_random_address(struct hci_dev *hdev, bool require_privacy, 6489 bool use_rpa, struct adv_info *adv_instance, 6490 u8 *own_addr_type, bdaddr_t *rand_addr) 6491 { 6492 int err; 6493 6494 bacpy(rand_addr, BDADDR_ANY); 6495 6496 /* If privacy is enabled use a resolvable private address. If 6497 * current RPA has expired then generate a new one. 6498 */ 6499 if (use_rpa) { 6500 /* If Controller supports LL Privacy use own address type is 6501 * 0x03 6502 */ 6503 if (use_ll_privacy(hdev)) 6504 *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED; 6505 else 6506 *own_addr_type = ADDR_LE_DEV_RANDOM; 6507 6508 if (adv_instance) { 6509 if (adv_rpa_valid(adv_instance)) 6510 return 0; 6511 } else { 6512 if (rpa_valid(hdev)) 6513 return 0; 6514 } 6515 6516 err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa); 6517 if (err < 0) { 6518 bt_dev_err(hdev, "failed to generate new RPA"); 6519 return err; 6520 } 6521 6522 bacpy(rand_addr, &hdev->rpa); 6523 6524 return 0; 6525 } 6526 6527 /* In case of required privacy without resolvable private address, 6528 * use an non-resolvable private address. This is useful for 6529 * non-connectable advertising. 6530 */ 6531 if (require_privacy) { 6532 bdaddr_t nrpa; 6533 6534 while (true) { 6535 /* The non-resolvable private address is generated 6536 * from random six bytes with the two most significant 6537 * bits cleared. 6538 */ 6539 get_random_bytes(&nrpa, 6); 6540 nrpa.b[5] &= 0x3f; 6541 6542 /* The non-resolvable private address shall not be 6543 * equal to the public address. 6544 */ 6545 if (bacmp(&hdev->bdaddr, &nrpa)) 6546 break; 6547 } 6548 6549 *own_addr_type = ADDR_LE_DEV_RANDOM; 6550 bacpy(rand_addr, &nrpa); 6551 6552 return 0; 6553 } 6554 6555 /* No privacy so use a public address. */ 6556 *own_addr_type = ADDR_LE_DEV_PUBLIC; 6557 6558 return 0; 6559 } 6560 6561 static int _update_adv_data_sync(struct hci_dev *hdev, void *data) 6562 { 6563 u8 instance = PTR_UINT(data); 6564 6565 return hci_update_adv_data_sync(hdev, instance); 6566 } 6567 6568 int hci_update_adv_data(struct hci_dev *hdev, u8 instance) 6569 { 6570 return hci_cmd_sync_queue(hdev, _update_adv_data_sync, 6571 UINT_PTR(instance), NULL); 6572 } 6573