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