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