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