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