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