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