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