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