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