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