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