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