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